blob: 084bf71b2b87b3236feafdd9623b9468177cc5df [file] [log] [blame]
Vinod Koul7c3cd182017-12-14 11:19:34 +05301// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4#include <linux/acpi.h>
5#include <linux/mod_devicetable.h>
Vinod Koul9d715fa2017-12-14 11:19:37 +05306#include <linux/pm_runtime.h>
7#include <linux/soundwire/sdw_registers.h>
Vinod Koul7c3cd182017-12-14 11:19:34 +05308#include <linux/soundwire/sdw.h>
9#include "bus.h"
10
11/**
12 * sdw_add_bus_master() - add a bus Master instance
13 * @bus: bus instance
14 *
15 * Initializes the bus instance, read properties and create child
16 * devices.
17 */
18int sdw_add_bus_master(struct sdw_bus *bus)
19{
20 int ret;
21
22 if (!bus->dev) {
23 pr_err("SoundWire bus has no device");
24 return -ENODEV;
25 }
26
Vinod Koul9d715fa2017-12-14 11:19:37 +053027 if (!bus->ops) {
28 dev_err(bus->dev, "SoundWire Bus ops are not set");
29 return -EINVAL;
30 }
31
32 mutex_init(&bus->msg_lock);
Vinod Koul7c3cd182017-12-14 11:19:34 +053033 mutex_init(&bus->bus_lock);
34 INIT_LIST_HEAD(&bus->slaves);
Sanyog Kale89e59052018-04-26 18:38:08 +053035 INIT_LIST_HEAD(&bus->m_rt_list);
Vinod Koul7c3cd182017-12-14 11:19:34 +053036
Vinod Koul56d4fe32017-12-14 11:19:35 +053037 if (bus->ops->read_prop) {
38 ret = bus->ops->read_prop(bus);
39 if (ret < 0) {
40 dev_err(bus->dev, "Bus read properties failed:%d", ret);
41 return ret;
42 }
43 }
44
Vinod Koul7c3cd182017-12-14 11:19:34 +053045 /*
46 * Device numbers in SoundWire are 0 thru 15. Enumeration device
47 * number (0), Broadcast device number (15), Group numbers (12 and
48 * 13) and Master device number (14) are not used for assignment so
49 * mask these and other higher bits.
50 */
51
52 /* Set higher order bits */
53 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
54
55 /* Set enumuration device number and broadcast device number */
56 set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
57 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
58
59 /* Set group device numbers and master device number */
60 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
61 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
62 set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
63
64 /*
65 * SDW is an enumerable bus, but devices can be powered off. So,
66 * they won't be able to report as present.
67 *
68 * Create Slave devices based on Slaves described in
69 * the respective firmware (ACPI/DT)
70 */
71 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
72 ret = sdw_acpi_find_slaves(bus);
73 else
74 ret = -ENOTSUPP; /* No ACPI/DT so error out */
75
76 if (ret) {
77 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
78 return ret;
79 }
80
Sanyog Kale99b8a5d2018-04-26 18:38:28 +053081 /*
82 * Default active bank will be 0 as out of reset the Slaves have
83 * to start with bank 0 (Table 40 of Spec)
84 */
85 bus->params.curr_bank = SDW_BANK0;
86 bus->params.next_bank = SDW_BANK1;
87
Vinod Koul7c3cd182017-12-14 11:19:34 +053088 return 0;
89}
90EXPORT_SYMBOL(sdw_add_bus_master);
91
92static int sdw_delete_slave(struct device *dev, void *data)
93{
94 struct sdw_slave *slave = dev_to_sdw_dev(dev);
95 struct sdw_bus *bus = slave->bus;
96
97 mutex_lock(&bus->bus_lock);
98
99 if (slave->dev_num) /* clear dev_num if assigned */
100 clear_bit(slave->dev_num, bus->assigned);
101
102 list_del_init(&slave->node);
103 mutex_unlock(&bus->bus_lock);
104
105 device_unregister(dev);
106 return 0;
107}
108
109/**
110 * sdw_delete_bus_master() - delete the bus master instance
111 * @bus: bus to be deleted
112 *
113 * Remove the instance, delete the child devices.
114 */
115void sdw_delete_bus_master(struct sdw_bus *bus)
116{
117 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
118}
119EXPORT_SYMBOL(sdw_delete_bus_master);
120
Vinod Koul9d715fa2017-12-14 11:19:37 +0530121/*
122 * SDW IO Calls
123 */
124
125static inline int find_response_code(enum sdw_command_response resp)
126{
127 switch (resp) {
128 case SDW_CMD_OK:
129 return 0;
130
131 case SDW_CMD_IGNORED:
132 return -ENODATA;
133
134 case SDW_CMD_TIMEOUT:
135 return -ETIMEDOUT;
136
137 default:
138 return -EIO;
139 }
140}
141
142static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
143{
144 int retry = bus->prop.err_threshold;
145 enum sdw_command_response resp;
146 int ret = 0, i;
147
148 for (i = 0; i <= retry; i++) {
149 resp = bus->ops->xfer_msg(bus, msg);
150 ret = find_response_code(resp);
151
152 /* if cmd is ok or ignored return */
153 if (ret == 0 || ret == -ENODATA)
154 return ret;
155 }
156
157 return ret;
158}
159
160static inline int do_transfer_defer(struct sdw_bus *bus,
161 struct sdw_msg *msg, struct sdw_defer *defer)
162{
163 int retry = bus->prop.err_threshold;
164 enum sdw_command_response resp;
165 int ret = 0, i;
166
167 defer->msg = msg;
168 defer->length = msg->len;
169
170 for (i = 0; i <= retry; i++) {
171 resp = bus->ops->xfer_msg_defer(bus, msg, defer);
172 ret = find_response_code(resp);
173 /* if cmd is ok or ignored return */
174 if (ret == 0 || ret == -ENODATA)
175 return ret;
176 }
177
178 return ret;
179}
180
181static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
182{
183 int retry = bus->prop.err_threshold;
184 enum sdw_command_response resp;
185 int ret = 0, i;
186
187 for (i = 0; i <= retry; i++) {
188 resp = bus->ops->reset_page_addr(bus, dev_num);
189 ret = find_response_code(resp);
190 /* if cmd is ok or ignored return */
191 if (ret == 0 || ret == -ENODATA)
192 return ret;
193 }
194
195 return ret;
196}
197
198/**
199 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
200 * @bus: SDW bus
201 * @msg: SDW message to be xfered
202 */
203int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
204{
205 int ret;
206
207 mutex_lock(&bus->msg_lock);
208
209 ret = do_transfer(bus, msg);
210 if (ret != 0 && ret != -ENODATA)
211 dev_err(bus->dev, "trf on Slave %d failed:%d\n",
212 msg->dev_num, ret);
213
214 if (msg->page)
215 sdw_reset_page(bus, msg->dev_num);
216
217 mutex_unlock(&bus->msg_lock);
218
219 return ret;
220}
221
222/**
223 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
224 * @bus: SDW bus
225 * @msg: SDW message to be xfered
226 * @defer: Defer block for signal completion
227 *
228 * Caller needs to hold the msg_lock lock while calling this
229 */
230int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
231 struct sdw_defer *defer)
232{
233 int ret;
234
235 if (!bus->ops->xfer_msg_defer)
236 return -ENOTSUPP;
237
238 ret = do_transfer_defer(bus, msg, defer);
239 if (ret != 0 && ret != -ENODATA)
240 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
241 msg->dev_num, ret);
242
243 if (msg->page)
244 sdw_reset_page(bus, msg->dev_num);
245
246 return ret;
247}
248
249
250int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
251 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
252{
253 memset(msg, 0, sizeof(*msg));
254 msg->addr = addr; /* addr is 16 bit and truncated here */
255 msg->len = count;
256 msg->dev_num = dev_num;
257 msg->flags = flags;
258 msg->buf = buf;
259 msg->ssp_sync = false;
260 msg->page = false;
261
262 if (addr < SDW_REG_NO_PAGE) { /* no paging area */
263 return 0;
264 } else if (addr >= SDW_REG_MAX) { /* illegal addr */
265 pr_err("SDW: Invalid address %x passed\n", addr);
266 return -EINVAL;
267 }
268
269 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
270 if (slave && !slave->prop.paging_support)
271 return 0;
272 /* no need for else as that will fall thru to paging */
273 }
274
275 /* paging mandatory */
276 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
277 pr_err("SDW: Invalid device for paging :%d\n", dev_num);
278 return -EINVAL;
279 }
280
281 if (!slave) {
282 pr_err("SDW: No slave for paging addr\n");
283 return -EINVAL;
284 } else if (!slave->prop.paging_support) {
285 dev_err(&slave->dev,
286 "address %x needs paging but no support", addr);
287 return -EINVAL;
288 }
289
290 msg->addr_page1 = (addr >> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE1_MASK));
291 msg->addr_page2 = (addr >> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE2_MASK));
292 msg->addr |= BIT(15);
293 msg->page = true;
294
295 return 0;
296}
297
298/**
299 * sdw_nread() - Read "n" contiguous SDW Slave registers
300 * @slave: SDW Slave
301 * @addr: Register address
302 * @count: length
303 * @val: Buffer for values to be read
304 */
305int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
306{
307 struct sdw_msg msg;
308 int ret;
309
310 ret = sdw_fill_msg(&msg, slave, addr, count,
311 slave->dev_num, SDW_MSG_FLAG_READ, val);
312 if (ret < 0)
313 return ret;
314
315 ret = pm_runtime_get_sync(slave->bus->dev);
Shreyas NCc22c0ae2018-01-09 10:15:46 +0530316 if (ret < 0)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530317 return ret;
318
319 ret = sdw_transfer(slave->bus, &msg);
320 pm_runtime_put(slave->bus->dev);
321
322 return ret;
323}
324EXPORT_SYMBOL(sdw_nread);
325
326/**
327 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
328 * @slave: SDW Slave
329 * @addr: Register address
330 * @count: length
331 * @val: Buffer for values to be read
332 */
333int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
334{
335 struct sdw_msg msg;
336 int ret;
337
338 ret = sdw_fill_msg(&msg, slave, addr, count,
339 slave->dev_num, SDW_MSG_FLAG_WRITE, val);
340 if (ret < 0)
341 return ret;
342
343 ret = pm_runtime_get_sync(slave->bus->dev);
Shreyas NCc22c0ae2018-01-09 10:15:46 +0530344 if (ret < 0)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530345 return ret;
346
347 ret = sdw_transfer(slave->bus, &msg);
348 pm_runtime_put(slave->bus->dev);
349
350 return ret;
351}
352EXPORT_SYMBOL(sdw_nwrite);
353
354/**
355 * sdw_read() - Read a SDW Slave register
356 * @slave: SDW Slave
357 * @addr: Register address
358 */
359int sdw_read(struct sdw_slave *slave, u32 addr)
360{
361 u8 buf;
362 int ret;
363
364 ret = sdw_nread(slave, addr, 1, &buf);
365 if (ret < 0)
366 return ret;
367 else
368 return buf;
369}
370EXPORT_SYMBOL(sdw_read);
371
372/**
373 * sdw_write() - Write a SDW Slave register
374 * @slave: SDW Slave
375 * @addr: Register address
376 * @value: Register value
377 */
378int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
379{
380 return sdw_nwrite(slave, addr, 1, &value);
381
382}
383EXPORT_SYMBOL(sdw_write);
384
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530385/*
386 * SDW alert handling
387 */
388
389/* called with bus_lock held */
390static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
391{
392 struct sdw_slave *slave = NULL;
393
394 list_for_each_entry(slave, &bus->slaves, node) {
395 if (slave->dev_num == i)
396 return slave;
397 }
398
399 return NULL;
400}
401
402static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
403{
404
405 if ((slave->id.unique_id != id.unique_id) ||
406 (slave->id.mfg_id != id.mfg_id) ||
407 (slave->id.part_id != id.part_id) ||
408 (slave->id.class_id != id.class_id))
409 return -ENODEV;
410
411 return 0;
412}
413
414/* called with bus_lock held */
415static int sdw_get_device_num(struct sdw_slave *slave)
416{
417 int bit;
418
419 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
420 if (bit == SDW_MAX_DEVICES) {
421 bit = -ENODEV;
422 goto err;
423 }
424
425 /*
426 * Do not update dev_num in Slave data structure here,
427 * Update once program dev_num is successful
428 */
429 set_bit(bit, slave->bus->assigned);
430
431err:
432 return bit;
433}
434
435static int sdw_assign_device_num(struct sdw_slave *slave)
436{
437 int ret, dev_num;
438
439 /* check first if device number is assigned, if so reuse that */
440 if (!slave->dev_num) {
441 mutex_lock(&slave->bus->bus_lock);
442 dev_num = sdw_get_device_num(slave);
443 mutex_unlock(&slave->bus->bus_lock);
444 if (dev_num < 0) {
445 dev_err(slave->bus->dev, "Get dev_num failed: %d",
446 dev_num);
447 return dev_num;
448 }
449 } else {
450 dev_info(slave->bus->dev,
451 "Slave already registered dev_num:%d",
452 slave->dev_num);
453
454 /* Clear the slave->dev_num to transfer message on device 0 */
455 dev_num = slave->dev_num;
456 slave->dev_num = 0;
457
458 }
459
460 ret = sdw_write(slave, SDW_SCP_DEVNUMBER, dev_num);
461 if (ret < 0) {
462 dev_err(&slave->dev, "Program device_num failed: %d", ret);
463 return ret;
464 }
465
466 /* After xfer of msg, restore dev_num */
467 slave->dev_num = dev_num;
468
469 return 0;
470}
471
Vinod Koul7c3cd182017-12-14 11:19:34 +0530472void sdw_extract_slave_id(struct sdw_bus *bus,
473 u64 addr, struct sdw_slave_id *id)
474{
475 dev_dbg(bus->dev, "SDW Slave Addr: %llx", addr);
476
477 /*
478 * Spec definition
479 * Register Bit Contents
480 * DevId_0 [7:4] 47:44 sdw_version
481 * DevId_0 [3:0] 43:40 unique_id
482 * DevId_1 39:32 mfg_id [15:8]
483 * DevId_2 31:24 mfg_id [7:0]
484 * DevId_3 23:16 part_id [15:8]
485 * DevId_4 15:08 part_id [7:0]
486 * DevId_5 07:00 class_id
487 */
488 id->sdw_version = (addr >> 44) & GENMASK(3, 0);
489 id->unique_id = (addr >> 40) & GENMASK(3, 0);
490 id->mfg_id = (addr >> 24) & GENMASK(15, 0);
491 id->part_id = (addr >> 8) & GENMASK(15, 0);
492 id->class_id = addr & GENMASK(7, 0);
493
494 dev_dbg(bus->dev,
495 "SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x",
496 id->class_id, id->part_id, id->mfg_id,
497 id->unique_id, id->sdw_version);
498
499}
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530500
501static int sdw_program_device_num(struct sdw_bus *bus)
502{
503 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
504 struct sdw_slave *slave, *_s;
505 struct sdw_slave_id id;
506 struct sdw_msg msg;
507 bool found = false;
508 int count = 0, ret;
509 u64 addr;
510
511 /* No Slave, so use raw xfer api */
512 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
513 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
514 if (ret < 0)
515 return ret;
516
517 do {
518 ret = sdw_transfer(bus, &msg);
519 if (ret == -ENODATA) { /* end of device id reads */
520 ret = 0;
521 break;
522 }
523 if (ret < 0) {
524 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
525 break;
526 }
527
528 /*
529 * Construct the addr and extract. Cast the higher shift
530 * bits to avoid truncation due to size limit.
531 */
532 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
Colin Ian King0132af052018-01-08 22:22:42 +0530533 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
534 ((u64)buf[0] << 40);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530535
536 sdw_extract_slave_id(bus, addr, &id);
537
538 /* Now compare with entries */
539 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
540 if (sdw_compare_devid(slave, id) == 0) {
541 found = true;
542
543 /*
544 * Assign a new dev_num to this Slave and
545 * not mark it present. It will be marked
546 * present after it reports ATTACHED on new
547 * dev_num
548 */
549 ret = sdw_assign_device_num(slave);
550 if (ret) {
551 dev_err(slave->bus->dev,
552 "Assign dev_num failed:%d",
553 ret);
554 return ret;
555 }
556
557 break;
558 }
559 }
560
561 if (found == false) {
562 /* TODO: Park this device in Group 13 */
563 dev_err(bus->dev, "Slave Entry not found");
564 }
565
566 count++;
567
568 /*
569 * Check till error out or retry (count) exhausts.
570 * Device can drop off and rejoin during enumeration
571 * so count till twice the bound.
572 */
573
574 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
575
576 return ret;
577}
578
579static void sdw_modify_slave_status(struct sdw_slave *slave,
580 enum sdw_slave_status status)
581{
582 mutex_lock(&slave->bus->bus_lock);
583 slave->status = status;
584 mutex_unlock(&slave->bus->bus_lock);
585}
586
Sanyog Kale79df15b2018-04-26 18:38:23 +0530587int sdw_configure_dpn_intr(struct sdw_slave *slave,
588 int port, bool enable, int mask)
589{
590 u32 addr;
591 int ret;
592 u8 val = 0;
593
594 addr = SDW_DPN_INTMASK(port);
595
596 /* Set/Clear port ready interrupt mask */
597 if (enable) {
598 val |= mask;
599 val |= SDW_DPN_INT_PORT_READY;
600 } else {
601 val &= ~(mask);
602 val &= ~SDW_DPN_INT_PORT_READY;
603 }
604
605 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
606 if (ret < 0)
607 dev_err(slave->bus->dev,
608 "SDW_DPN_INTMASK write failed:%d", val);
609
610 return ret;
611}
612
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530613static int sdw_initialize_slave(struct sdw_slave *slave)
614{
615 struct sdw_slave_prop *prop = &slave->prop;
616 int ret;
617 u8 val;
618
619 /*
620 * Set bus clash, parity and SCP implementation
621 * defined interrupt mask
622 * TODO: Read implementation defined interrupt mask
623 * from Slave property
624 */
625 val = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
626 SDW_SCP_INT1_PARITY;
627
628 /* Enable SCP interrupts */
629 ret = sdw_update(slave, SDW_SCP_INTMASK1, val, val);
630 if (ret < 0) {
631 dev_err(slave->bus->dev,
632 "SDW_SCP_INTMASK1 write failed:%d", ret);
633 return ret;
634 }
635
636 /* No need to continue if DP0 is not present */
637 if (!slave->prop.dp0_prop)
638 return 0;
639
640 /* Enable DP0 interrupts */
641 val = prop->dp0_prop->device_interrupts;
642 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
643
644 ret = sdw_update(slave, SDW_DP0_INTMASK, val, val);
645 if (ret < 0) {
646 dev_err(slave->bus->dev,
647 "SDW_DP0_INTMASK read failed:%d", ret);
648 return val;
649 }
650
651 return 0;
652}
Vinod Koulb0a9c372017-12-14 11:19:40 +0530653
654static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
655{
656 u8 clear = 0, impl_int_mask;
657 int status, status2, ret, count = 0;
658
659 status = sdw_read(slave, SDW_DP0_INT);
660 if (status < 0) {
661 dev_err(slave->bus->dev,
662 "SDW_DP0_INT read failed:%d", status);
663 return status;
664 }
665
666 do {
667
668 if (status & SDW_DP0_INT_TEST_FAIL) {
669 dev_err(&slave->dev, "Test fail for port 0");
670 clear |= SDW_DP0_INT_TEST_FAIL;
671 }
672
673 /*
674 * Assumption: PORT_READY interrupt will be received only for
675 * ports implementing Channel Prepare state machine (CP_SM)
676 */
677
678 if (status & SDW_DP0_INT_PORT_READY) {
679 complete(&slave->port_ready[0]);
680 clear |= SDW_DP0_INT_PORT_READY;
681 }
682
683 if (status & SDW_DP0_INT_BRA_FAILURE) {
684 dev_err(&slave->dev, "BRA failed");
685 clear |= SDW_DP0_INT_BRA_FAILURE;
686 }
687
688 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
689 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
690
691 if (status & impl_int_mask) {
692 clear |= impl_int_mask;
693 *slave_status = clear;
694 }
695
696 /* clear the interrupt */
697 ret = sdw_write(slave, SDW_DP0_INT, clear);
698 if (ret < 0) {
699 dev_err(slave->bus->dev,
700 "SDW_DP0_INT write failed:%d", ret);
701 return ret;
702 }
703
704 /* Read DP0 interrupt again */
705 status2 = sdw_read(slave, SDW_DP0_INT);
706 if (status2 < 0) {
707 dev_err(slave->bus->dev,
Wei Yongjun80cd8f02018-01-08 22:22:44 +0530708 "SDW_DP0_INT read failed:%d", status2);
709 return status2;
Vinod Koulb0a9c372017-12-14 11:19:40 +0530710 }
711 status &= status2;
712
713 count++;
714
715 /* we can get alerts while processing so keep retrying */
716 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
717
718 if (count == SDW_READ_INTR_CLEAR_RETRY)
719 dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read");
720
721 return ret;
722}
723
724static int sdw_handle_port_interrupt(struct sdw_slave *slave,
725 int port, u8 *slave_status)
726{
727 u8 clear = 0, impl_int_mask;
728 int status, status2, ret, count = 0;
729 u32 addr;
730
731 if (port == 0)
732 return sdw_handle_dp0_interrupt(slave, slave_status);
733
734 addr = SDW_DPN_INT(port);
735 status = sdw_read(slave, addr);
736 if (status < 0) {
737 dev_err(slave->bus->dev,
738 "SDW_DPN_INT read failed:%d", status);
739
740 return status;
741 }
742
743 do {
744
745 if (status & SDW_DPN_INT_TEST_FAIL) {
746 dev_err(&slave->dev, "Test fail for port:%d", port);
747 clear |= SDW_DPN_INT_TEST_FAIL;
748 }
749
750 /*
751 * Assumption: PORT_READY interrupt will be received only
752 * for ports implementing CP_SM.
753 */
754 if (status & SDW_DPN_INT_PORT_READY) {
755 complete(&slave->port_ready[port]);
756 clear |= SDW_DPN_INT_PORT_READY;
757 }
758
759 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
760 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
761
762
763 if (status & impl_int_mask) {
764 clear |= impl_int_mask;
765 *slave_status = clear;
766 }
767
768 /* clear the interrupt */
769 ret = sdw_write(slave, addr, clear);
770 if (ret < 0) {
771 dev_err(slave->bus->dev,
772 "SDW_DPN_INT write failed:%d", ret);
773 return ret;
774 }
775
776 /* Read DPN interrupt again */
777 status2 = sdw_read(slave, addr);
Wei Yongjun80cd8f02018-01-08 22:22:44 +0530778 if (status2 < 0) {
Vinod Koulb0a9c372017-12-14 11:19:40 +0530779 dev_err(slave->bus->dev,
Wei Yongjun80cd8f02018-01-08 22:22:44 +0530780 "SDW_DPN_INT read failed:%d", status2);
781 return status2;
Vinod Koulb0a9c372017-12-14 11:19:40 +0530782 }
783 status &= status2;
784
785 count++;
786
787 /* we can get alerts while processing so keep retrying */
788 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
789
790 if (count == SDW_READ_INTR_CLEAR_RETRY)
791 dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
792
793 return ret;
794}
795
796static int sdw_handle_slave_alerts(struct sdw_slave *slave)
797{
798 struct sdw_slave_intr_status slave_intr;
799 u8 clear = 0, bit, port_status[15];
800 int port_num, stat, ret, count = 0;
801 unsigned long port;
802 bool slave_notify = false;
803 u8 buf, buf2[2], _buf, _buf2[2];
804
805 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
806
807 /* Read Instat 1, Instat 2 and Instat 3 registers */
Dan Carpenter51c26652018-01-21 10:26:31 +0530808 buf = ret = sdw_read(slave, SDW_SCP_INT1);
Vinod Koulb0a9c372017-12-14 11:19:40 +0530809 if (ret < 0) {
810 dev_err(slave->bus->dev,
811 "SDW_SCP_INT1 read failed:%d", ret);
812 return ret;
813 }
814
815 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, buf2);
816 if (ret < 0) {
817 dev_err(slave->bus->dev,
818 "SDW_SCP_INT2/3 read failed:%d", ret);
819 return ret;
820 }
821
822 do {
823 /*
824 * Check parity, bus clash and Slave (impl defined)
825 * interrupt
826 */
827 if (buf & SDW_SCP_INT1_PARITY) {
828 dev_err(&slave->dev, "Parity error detected");
829 clear |= SDW_SCP_INT1_PARITY;
830 }
831
832 if (buf & SDW_SCP_INT1_BUS_CLASH) {
833 dev_err(&slave->dev, "Bus clash error detected");
834 clear |= SDW_SCP_INT1_BUS_CLASH;
835 }
836
837 /*
838 * When bus clash or parity errors are detected, such errors
839 * are unlikely to be recoverable errors.
840 * TODO: In such scenario, reset bus. Make this configurable
841 * via sysfs property with bus reset being the default.
842 */
843
844 if (buf & SDW_SCP_INT1_IMPL_DEF) {
845 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
846 clear |= SDW_SCP_INT1_IMPL_DEF;
847 slave_notify = true;
848 }
849
850 /* Check port 0 - 3 interrupts */
851 port = buf & SDW_SCP_INT1_PORT0_3;
852
853 /* To get port number corresponding to bits, shift it */
854 port = port >> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3);
855 for_each_set_bit(bit, &port, 8) {
856 sdw_handle_port_interrupt(slave, bit,
857 &port_status[bit]);
858
859 }
860
861 /* Check if cascade 2 interrupt is present */
862 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
863 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
864 for_each_set_bit(bit, &port, 8) {
865 /* scp2 ports start from 4 */
866 port_num = bit + 3;
867 sdw_handle_port_interrupt(slave,
868 port_num,
869 &port_status[port_num]);
870 }
871 }
872
873 /* now check last cascade */
874 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
875 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
876 for_each_set_bit(bit, &port, 8) {
877 /* scp3 ports start from 11 */
878 port_num = bit + 10;
879 sdw_handle_port_interrupt(slave,
880 port_num,
881 &port_status[port_num]);
882 }
883 }
884
885 /* Update the Slave driver */
886 if (slave_notify && (slave->ops) &&
887 (slave->ops->interrupt_callback)) {
888 slave_intr.control_port = clear;
889 memcpy(slave_intr.port, &port_status,
890 sizeof(slave_intr.port));
891
892 slave->ops->interrupt_callback(slave, &slave_intr);
893 }
894
895 /* Ack interrupt */
896 ret = sdw_write(slave, SDW_SCP_INT1, clear);
897 if (ret < 0) {
898 dev_err(slave->bus->dev,
899 "SDW_SCP_INT1 write failed:%d", ret);
900 return ret;
901 }
902
903 /*
904 * Read status again to ensure no new interrupts arrived
905 * while servicing interrupts.
906 */
Dan Carpenter51c26652018-01-21 10:26:31 +0530907 _buf = ret = sdw_read(slave, SDW_SCP_INT1);
Vinod Koulb0a9c372017-12-14 11:19:40 +0530908 if (ret < 0) {
909 dev_err(slave->bus->dev,
910 "SDW_SCP_INT1 read failed:%d", ret);
911 return ret;
912 }
913
914 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, _buf2);
915 if (ret < 0) {
916 dev_err(slave->bus->dev,
917 "SDW_SCP_INT2/3 read failed:%d", ret);
918 return ret;
919 }
920
921 /* Make sure no interrupts are pending */
922 buf &= _buf;
923 buf2[0] &= _buf2[0];
924 buf2[1] &= _buf2[1];
925 stat = buf || buf2[0] || buf2[1];
926
927 /*
928 * Exit loop if Slave is continuously in ALERT state even
929 * after servicing the interrupt multiple times.
930 */
931 count++;
932
933 /* we can get alerts while processing so keep retrying */
934 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
935
936 if (count == SDW_READ_INTR_CLEAR_RETRY)
937 dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read");
938
939 return ret;
940}
941
942static int sdw_update_slave_status(struct sdw_slave *slave,
943 enum sdw_slave_status status)
944{
945 if ((slave->ops) && (slave->ops->update_status))
946 return slave->ops->update_status(slave, status);
947
948 return 0;
949}
950
951/**
952 * sdw_handle_slave_status() - Handle Slave status
953 * @bus: SDW bus instance
954 * @status: Status for all Slave(s)
955 */
956int sdw_handle_slave_status(struct sdw_bus *bus,
957 enum sdw_slave_status status[])
958{
959 enum sdw_slave_status prev_status;
960 struct sdw_slave *slave;
961 int i, ret = 0;
962
963 if (status[0] == SDW_SLAVE_ATTACHED) {
964 ret = sdw_program_device_num(bus);
965 if (ret)
966 dev_err(bus->dev, "Slave attach failed: %d", ret);
967 }
968
969 /* Continue to check other slave statuses */
970 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
971 mutex_lock(&bus->bus_lock);
972 if (test_bit(i, bus->assigned) == false) {
973 mutex_unlock(&bus->bus_lock);
974 continue;
975 }
976 mutex_unlock(&bus->bus_lock);
977
978 slave = sdw_get_slave(bus, i);
979 if (!slave)
980 continue;
981
982 switch (status[i]) {
983 case SDW_SLAVE_UNATTACHED:
984 if (slave->status == SDW_SLAVE_UNATTACHED)
985 break;
986
987 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
988 break;
989
990 case SDW_SLAVE_ALERT:
991 ret = sdw_handle_slave_alerts(slave);
992 if (ret)
993 dev_err(bus->dev,
994 "Slave %d alert handling failed: %d",
995 i, ret);
996 break;
997
998 case SDW_SLAVE_ATTACHED:
999 if (slave->status == SDW_SLAVE_ATTACHED)
1000 break;
1001
1002 prev_status = slave->status;
1003 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1004
1005 if (prev_status == SDW_SLAVE_ALERT)
1006 break;
1007
1008 ret = sdw_initialize_slave(slave);
1009 if (ret)
1010 dev_err(bus->dev,
1011 "Slave %d initialization failed: %d",
1012 i, ret);
1013
1014 break;
1015
1016 default:
1017 dev_err(bus->dev, "Invalid slave %d status:%d",
1018 i, status[i]);
1019 break;
1020 }
1021
1022 ret = sdw_update_slave_status(slave, status[i]);
1023 if (ret)
1024 dev_err(slave->bus->dev,
1025 "Update Slave status failed:%d", ret);
1026
1027 }
1028
1029 return ret;
1030}
1031EXPORT_SYMBOL(sdw_handle_slave_status);