1 // 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/delay.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/pm_runtime.h>
8 #include <linux/soundwire/sdw_registers.h>
9 #include <linux/soundwire/sdw.h>
10 #include "bus.h"
11 #include "sysfs_local.h"
12
13 static DEFINE_IDA(sdw_ida);
14
sdw_get_id(struct sdw_bus * bus)15 static int sdw_get_id(struct sdw_bus *bus)
16 {
17 int rc = ida_alloc(&sdw_ida, GFP_KERNEL);
18
19 if (rc < 0)
20 return rc;
21
22 bus->id = rc;
23 return 0;
24 }
25
26 /**
27 * sdw_bus_master_add() - add a bus Master instance
28 * @bus: bus instance
29 * @parent: parent device
30 * @fwnode: firmware node handle
31 *
32 * Initializes the bus instance, read properties and create child
33 * devices.
34 */
sdw_bus_master_add(struct sdw_bus * bus,struct device * parent,struct fwnode_handle * fwnode)35 int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
36 struct fwnode_handle *fwnode)
37 {
38 struct sdw_master_prop *prop = NULL;
39 int ret;
40
41 if (!parent) {
42 pr_err("SoundWire parent device is not set\n");
43 return -ENODEV;
44 }
45
46 ret = sdw_get_id(bus);
47 if (ret) {
48 dev_err(parent, "Failed to get bus id\n");
49 return ret;
50 }
51
52 ret = sdw_master_device_add(bus, parent, fwnode);
53 if (ret) {
54 dev_err(parent, "Failed to add master device at link %d\n",
55 bus->link_id);
56 return ret;
57 }
58
59 if (!bus->ops) {
60 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
61 return -EINVAL;
62 }
63
64 if (!bus->compute_params) {
65 dev_err(bus->dev,
66 "Bandwidth allocation not configured, compute_params no set\n");
67 return -EINVAL;
68 }
69
70 mutex_init(&bus->msg_lock);
71 mutex_init(&bus->bus_lock);
72 INIT_LIST_HEAD(&bus->slaves);
73 INIT_LIST_HEAD(&bus->m_rt_list);
74
75 /*
76 * Initialize multi_link flag
77 * TODO: populate this flag by reading property from FW node
78 */
79 bus->multi_link = false;
80 if (bus->ops->read_prop) {
81 ret = bus->ops->read_prop(bus);
82 if (ret < 0) {
83 dev_err(bus->dev,
84 "Bus read properties failed:%d\n", ret);
85 return ret;
86 }
87 }
88
89 sdw_bus_debugfs_init(bus);
90
91 /*
92 * Device numbers in SoundWire are 0 through 15. Enumeration device
93 * number (0), Broadcast device number (15), Group numbers (12 and
94 * 13) and Master device number (14) are not used for assignment so
95 * mask these and other higher bits.
96 */
97
98 /* Set higher order bits */
99 *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
100
101 /* Set enumuration device number and broadcast device number */
102 set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
103 set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
104
105 /* Set group device numbers and master device number */
106 set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
107 set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
108 set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
109
110 /*
111 * SDW is an enumerable bus, but devices can be powered off. So,
112 * they won't be able to report as present.
113 *
114 * Create Slave devices based on Slaves described in
115 * the respective firmware (ACPI/DT)
116 */
117 if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
118 ret = sdw_acpi_find_slaves(bus);
119 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
120 ret = sdw_of_find_slaves(bus);
121 else
122 ret = -ENOTSUPP; /* No ACPI/DT so error out */
123
124 if (ret) {
125 dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
126 return ret;
127 }
128
129 /*
130 * Initialize clock values based on Master properties. The max
131 * frequency is read from max_clk_freq property. Current assumption
132 * is that the bus will start at highest clock frequency when
133 * powered on.
134 *
135 * Default active bank will be 0 as out of reset the Slaves have
136 * to start with bank 0 (Table 40 of Spec)
137 */
138 prop = &bus->prop;
139 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
140 bus->params.curr_dr_freq = bus->params.max_dr_freq;
141 bus->params.curr_bank = SDW_BANK0;
142 bus->params.next_bank = SDW_BANK1;
143
144 return 0;
145 }
146 EXPORT_SYMBOL(sdw_bus_master_add);
147
sdw_delete_slave(struct device * dev,void * data)148 static int sdw_delete_slave(struct device *dev, void *data)
149 {
150 struct sdw_slave *slave = dev_to_sdw_dev(dev);
151 struct sdw_bus *bus = slave->bus;
152
153 pm_runtime_disable(dev);
154
155 sdw_slave_debugfs_exit(slave);
156
157 mutex_lock(&bus->bus_lock);
158
159 if (slave->dev_num) /* clear dev_num if assigned */
160 clear_bit(slave->dev_num, bus->assigned);
161
162 list_del_init(&slave->node);
163 mutex_unlock(&bus->bus_lock);
164
165 device_unregister(dev);
166 return 0;
167 }
168
169 /**
170 * sdw_bus_master_delete() - delete the bus master instance
171 * @bus: bus to be deleted
172 *
173 * Remove the instance, delete the child devices.
174 */
sdw_bus_master_delete(struct sdw_bus * bus)175 void sdw_bus_master_delete(struct sdw_bus *bus)
176 {
177 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
178 sdw_master_device_del(bus);
179
180 sdw_bus_debugfs_exit(bus);
181 ida_free(&sdw_ida, bus->id);
182 }
183 EXPORT_SYMBOL(sdw_bus_master_delete);
184
185 /*
186 * SDW IO Calls
187 */
188
find_response_code(enum sdw_command_response resp)189 static inline int find_response_code(enum sdw_command_response resp)
190 {
191 switch (resp) {
192 case SDW_CMD_OK:
193 return 0;
194
195 case SDW_CMD_IGNORED:
196 return -ENODATA;
197
198 case SDW_CMD_TIMEOUT:
199 return -ETIMEDOUT;
200
201 default:
202 return -EIO;
203 }
204 }
205
do_transfer(struct sdw_bus * bus,struct sdw_msg * msg)206 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
207 {
208 int retry = bus->prop.err_threshold;
209 enum sdw_command_response resp;
210 int ret = 0, i;
211
212 for (i = 0; i <= retry; i++) {
213 resp = bus->ops->xfer_msg(bus, msg);
214 ret = find_response_code(resp);
215
216 /* if cmd is ok or ignored return */
217 if (ret == 0 || ret == -ENODATA)
218 return ret;
219 }
220
221 return ret;
222 }
223
do_transfer_defer(struct sdw_bus * bus,struct sdw_msg * msg,struct sdw_defer * defer)224 static inline int do_transfer_defer(struct sdw_bus *bus,
225 struct sdw_msg *msg,
226 struct sdw_defer *defer)
227 {
228 int retry = bus->prop.err_threshold;
229 enum sdw_command_response resp;
230 int ret = 0, i;
231
232 defer->msg = msg;
233 defer->length = msg->len;
234 init_completion(&defer->complete);
235
236 for (i = 0; i <= retry; i++) {
237 resp = bus->ops->xfer_msg_defer(bus, msg, defer);
238 ret = find_response_code(resp);
239 /* if cmd is ok or ignored return */
240 if (ret == 0 || ret == -ENODATA)
241 return ret;
242 }
243
244 return ret;
245 }
246
sdw_reset_page(struct sdw_bus * bus,u16 dev_num)247 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
248 {
249 int retry = bus->prop.err_threshold;
250 enum sdw_command_response resp;
251 int ret = 0, i;
252
253 for (i = 0; i <= retry; i++) {
254 resp = bus->ops->reset_page_addr(bus, dev_num);
255 ret = find_response_code(resp);
256 /* if cmd is ok or ignored return */
257 if (ret == 0 || ret == -ENODATA)
258 return ret;
259 }
260
261 return ret;
262 }
263
sdw_transfer_unlocked(struct sdw_bus * bus,struct sdw_msg * msg)264 static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
265 {
266 int ret;
267
268 ret = do_transfer(bus, msg);
269 if (ret != 0 && ret != -ENODATA)
270 dev_err(bus->dev, "trf on Slave %d failed:%d\n",
271 msg->dev_num, ret);
272
273 if (msg->page)
274 sdw_reset_page(bus, msg->dev_num);
275
276 return ret;
277 }
278
279 /**
280 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
281 * @bus: SDW bus
282 * @msg: SDW message to be xfered
283 */
sdw_transfer(struct sdw_bus * bus,struct sdw_msg * msg)284 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
285 {
286 int ret;
287
288 mutex_lock(&bus->msg_lock);
289
290 ret = sdw_transfer_unlocked(bus, msg);
291
292 mutex_unlock(&bus->msg_lock);
293
294 return ret;
295 }
296
297 /**
298 * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
299 * @bus: SDW bus
300 * @msg: SDW message to be xfered
301 * @defer: Defer block for signal completion
302 *
303 * Caller needs to hold the msg_lock lock while calling this
304 */
sdw_transfer_defer(struct sdw_bus * bus,struct sdw_msg * msg,struct sdw_defer * defer)305 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
306 struct sdw_defer *defer)
307 {
308 int ret;
309
310 if (!bus->ops->xfer_msg_defer)
311 return -ENOTSUPP;
312
313 ret = do_transfer_defer(bus, msg, defer);
314 if (ret != 0 && ret != -ENODATA)
315 dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
316 msg->dev_num, ret);
317
318 if (msg->page)
319 sdw_reset_page(bus, msg->dev_num);
320
321 return ret;
322 }
323
sdw_fill_msg(struct sdw_msg * msg,struct sdw_slave * slave,u32 addr,size_t count,u16 dev_num,u8 flags,u8 * buf)324 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
325 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
326 {
327 memset(msg, 0, sizeof(*msg));
328 msg->addr = addr; /* addr is 16 bit and truncated here */
329 msg->len = count;
330 msg->dev_num = dev_num;
331 msg->flags = flags;
332 msg->buf = buf;
333
334 if (addr < SDW_REG_NO_PAGE) /* no paging area */
335 return 0;
336
337 if (addr >= SDW_REG_MAX) { /* illegal addr */
338 pr_err("SDW: Invalid address %x passed\n", addr);
339 return -EINVAL;
340 }
341
342 if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
343 if (slave && !slave->prop.paging_support)
344 return 0;
345 /* no need for else as that will fall-through to paging */
346 }
347
348 /* paging mandatory */
349 if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
350 pr_err("SDW: Invalid device for paging :%d\n", dev_num);
351 return -EINVAL;
352 }
353
354 if (!slave) {
355 pr_err("SDW: No slave for paging addr\n");
356 return -EINVAL;
357 }
358
359 if (!slave->prop.paging_support) {
360 dev_err(&slave->dev,
361 "address %x needs paging but no support\n", addr);
362 return -EINVAL;
363 }
364
365 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
366 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
367 msg->addr |= BIT(15);
368 msg->page = true;
369
370 return 0;
371 }
372
373 /*
374 * Read/Write IO functions.
375 * no_pm versions can only be called by the bus, e.g. while enumerating or
376 * handling suspend-resume sequences.
377 * all clients need to use the pm versions
378 */
379
380 static int
sdw_nread_no_pm(struct sdw_slave * slave,u32 addr,size_t count,u8 * val)381 sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
382 {
383 struct sdw_msg msg;
384 int ret;
385
386 ret = sdw_fill_msg(&msg, slave, addr, count,
387 slave->dev_num, SDW_MSG_FLAG_READ, val);
388 if (ret < 0)
389 return ret;
390
391 return sdw_transfer(slave->bus, &msg);
392 }
393
394 static int
sdw_nwrite_no_pm(struct sdw_slave * slave,u32 addr,size_t count,u8 * val)395 sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
396 {
397 struct sdw_msg msg;
398 int ret;
399
400 ret = sdw_fill_msg(&msg, slave, addr, count,
401 slave->dev_num, SDW_MSG_FLAG_WRITE, val);
402 if (ret < 0)
403 return ret;
404
405 return sdw_transfer(slave->bus, &msg);
406 }
407
sdw_write_no_pm(struct sdw_slave * slave,u32 addr,u8 value)408 int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
409 {
410 return sdw_nwrite_no_pm(slave, addr, 1, &value);
411 }
412 EXPORT_SYMBOL(sdw_write_no_pm);
413
414 static int
sdw_bread_no_pm(struct sdw_bus * bus,u16 dev_num,u32 addr)415 sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
416 {
417 struct sdw_msg msg;
418 u8 buf;
419 int ret;
420
421 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
422 SDW_MSG_FLAG_READ, &buf);
423 if (ret)
424 return ret;
425
426 ret = sdw_transfer(bus, &msg);
427 if (ret < 0)
428 return ret;
429
430 return buf;
431 }
432
433 static int
sdw_bwrite_no_pm(struct sdw_bus * bus,u16 dev_num,u32 addr,u8 value)434 sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
435 {
436 struct sdw_msg msg;
437 int ret;
438
439 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
440 SDW_MSG_FLAG_WRITE, &value);
441 if (ret)
442 return ret;
443
444 return sdw_transfer(bus, &msg);
445 }
446
sdw_bread_no_pm_unlocked(struct sdw_bus * bus,u16 dev_num,u32 addr)447 int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
448 {
449 struct sdw_msg msg;
450 u8 buf;
451 int ret;
452
453 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
454 SDW_MSG_FLAG_READ, &buf);
455 if (ret)
456 return ret;
457
458 ret = sdw_transfer_unlocked(bus, &msg);
459 if (ret < 0)
460 return ret;
461
462 return buf;
463 }
464 EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
465
sdw_bwrite_no_pm_unlocked(struct sdw_bus * bus,u16 dev_num,u32 addr,u8 value)466 int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
467 {
468 struct sdw_msg msg;
469 int ret;
470
471 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
472 SDW_MSG_FLAG_WRITE, &value);
473 if (ret)
474 return ret;
475
476 return sdw_transfer_unlocked(bus, &msg);
477 }
478 EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
479
sdw_read_no_pm(struct sdw_slave * slave,u32 addr)480 int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
481 {
482 u8 buf;
483 int ret;
484
485 ret = sdw_nread_no_pm(slave, addr, 1, &buf);
486 if (ret < 0)
487 return ret;
488 else
489 return buf;
490 }
491 EXPORT_SYMBOL(sdw_read_no_pm);
492
sdw_update_no_pm(struct sdw_slave * slave,u32 addr,u8 mask,u8 val)493 static int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
494 {
495 int tmp;
496
497 tmp = sdw_read_no_pm(slave, addr);
498 if (tmp < 0)
499 return tmp;
500
501 tmp = (tmp & ~mask) | val;
502 return sdw_write_no_pm(slave, addr, tmp);
503 }
504
505 /**
506 * sdw_nread() - Read "n" contiguous SDW Slave registers
507 * @slave: SDW Slave
508 * @addr: Register address
509 * @count: length
510 * @val: Buffer for values to be read
511 */
sdw_nread(struct sdw_slave * slave,u32 addr,size_t count,u8 * val)512 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
513 {
514 int ret;
515
516 ret = pm_runtime_get_sync(&slave->dev);
517 if (ret < 0 && ret != -EACCES) {
518 pm_runtime_put_noidle(&slave->dev);
519 return ret;
520 }
521
522 ret = sdw_nread_no_pm(slave, addr, count, val);
523
524 pm_runtime_mark_last_busy(&slave->dev);
525 pm_runtime_put(&slave->dev);
526
527 return ret;
528 }
529 EXPORT_SYMBOL(sdw_nread);
530
531 /**
532 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
533 * @slave: SDW Slave
534 * @addr: Register address
535 * @count: length
536 * @val: Buffer for values to be read
537 */
sdw_nwrite(struct sdw_slave * slave,u32 addr,size_t count,u8 * val)538 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
539 {
540 int ret;
541
542 ret = pm_runtime_get_sync(&slave->dev);
543 if (ret < 0 && ret != -EACCES) {
544 pm_runtime_put_noidle(&slave->dev);
545 return ret;
546 }
547
548 ret = sdw_nwrite_no_pm(slave, addr, count, val);
549
550 pm_runtime_mark_last_busy(&slave->dev);
551 pm_runtime_put(&slave->dev);
552
553 return ret;
554 }
555 EXPORT_SYMBOL(sdw_nwrite);
556
557 /**
558 * sdw_read() - Read a SDW Slave register
559 * @slave: SDW Slave
560 * @addr: Register address
561 */
sdw_read(struct sdw_slave * slave,u32 addr)562 int sdw_read(struct sdw_slave *slave, u32 addr)
563 {
564 u8 buf;
565 int ret;
566
567 ret = sdw_nread(slave, addr, 1, &buf);
568 if (ret < 0)
569 return ret;
570
571 return buf;
572 }
573 EXPORT_SYMBOL(sdw_read);
574
575 /**
576 * sdw_write() - Write a SDW Slave register
577 * @slave: SDW Slave
578 * @addr: Register address
579 * @value: Register value
580 */
sdw_write(struct sdw_slave * slave,u32 addr,u8 value)581 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
582 {
583 return sdw_nwrite(slave, addr, 1, &value);
584 }
585 EXPORT_SYMBOL(sdw_write);
586
587 /*
588 * SDW alert handling
589 */
590
591 /* called with bus_lock held */
sdw_get_slave(struct sdw_bus * bus,int i)592 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
593 {
594 struct sdw_slave *slave = NULL;
595
596 list_for_each_entry(slave, &bus->slaves, node) {
597 if (slave->dev_num == i)
598 return slave;
599 }
600
601 return NULL;
602 }
603
sdw_compare_devid(struct sdw_slave * slave,struct sdw_slave_id id)604 static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
605 {
606 if (slave->id.mfg_id != id.mfg_id ||
607 slave->id.part_id != id.part_id ||
608 slave->id.class_id != id.class_id ||
609 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
610 slave->id.unique_id != id.unique_id))
611 return -ENODEV;
612
613 return 0;
614 }
615
616 /* called with bus_lock held */
sdw_get_device_num(struct sdw_slave * slave)617 static int sdw_get_device_num(struct sdw_slave *slave)
618 {
619 int bit;
620
621 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
622 if (bit == SDW_MAX_DEVICES) {
623 bit = -ENODEV;
624 goto err;
625 }
626
627 /*
628 * Do not update dev_num in Slave data structure here,
629 * Update once program dev_num is successful
630 */
631 set_bit(bit, slave->bus->assigned);
632
633 err:
634 return bit;
635 }
636
sdw_assign_device_num(struct sdw_slave * slave)637 static int sdw_assign_device_num(struct sdw_slave *slave)
638 {
639 int ret, dev_num;
640 bool new_device = false;
641
642 /* check first if device number is assigned, if so reuse that */
643 if (!slave->dev_num) {
644 if (!slave->dev_num_sticky) {
645 mutex_lock(&slave->bus->bus_lock);
646 dev_num = sdw_get_device_num(slave);
647 mutex_unlock(&slave->bus->bus_lock);
648 if (dev_num < 0) {
649 dev_err(slave->bus->dev, "Get dev_num failed: %d\n",
650 dev_num);
651 return dev_num;
652 }
653 slave->dev_num = dev_num;
654 slave->dev_num_sticky = dev_num;
655 new_device = true;
656 } else {
657 slave->dev_num = slave->dev_num_sticky;
658 }
659 }
660
661 if (!new_device)
662 dev_dbg(slave->bus->dev,
663 "Slave already registered, reusing dev_num:%d\n",
664 slave->dev_num);
665
666 /* Clear the slave->dev_num to transfer message on device 0 */
667 dev_num = slave->dev_num;
668 slave->dev_num = 0;
669
670 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
671 if (ret < 0) {
672 dev_err(&slave->dev, "Program device_num %d failed: %d\n",
673 dev_num, ret);
674 return ret;
675 }
676
677 /* After xfer of msg, restore dev_num */
678 slave->dev_num = slave->dev_num_sticky;
679
680 return 0;
681 }
682
sdw_extract_slave_id(struct sdw_bus * bus,u64 addr,struct sdw_slave_id * id)683 void sdw_extract_slave_id(struct sdw_bus *bus,
684 u64 addr, struct sdw_slave_id *id)
685 {
686 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
687
688 id->sdw_version = SDW_VERSION(addr);
689 id->unique_id = SDW_UNIQUE_ID(addr);
690 id->mfg_id = SDW_MFG_ID(addr);
691 id->part_id = SDW_PART_ID(addr);
692 id->class_id = SDW_CLASS_ID(addr);
693
694 dev_dbg(bus->dev,
695 "SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x\n",
696 id->class_id, id->part_id, id->mfg_id,
697 id->unique_id, id->sdw_version);
698 }
699
sdw_program_device_num(struct sdw_bus * bus)700 static int sdw_program_device_num(struct sdw_bus *bus)
701 {
702 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
703 struct sdw_slave *slave, *_s;
704 struct sdw_slave_id id;
705 struct sdw_msg msg;
706 bool found;
707 int count = 0, ret;
708 u64 addr;
709
710 /* No Slave, so use raw xfer api */
711 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
712 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
713 if (ret < 0)
714 return ret;
715
716 do {
717 ret = sdw_transfer(bus, &msg);
718 if (ret == -ENODATA) { /* end of device id reads */
719 dev_dbg(bus->dev, "No more devices to enumerate\n");
720 ret = 0;
721 break;
722 }
723 if (ret < 0) {
724 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
725 break;
726 }
727
728 /*
729 * Construct the addr and extract. Cast the higher shift
730 * bits to avoid truncation due to size limit.
731 */
732 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
733 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
734 ((u64)buf[0] << 40);
735
736 sdw_extract_slave_id(bus, addr, &id);
737
738 found = false;
739 /* Now compare with entries */
740 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
741 if (sdw_compare_devid(slave, id) == 0) {
742 found = true;
743
744 /*
745 * Assign a new dev_num to this Slave and
746 * not mark it present. It will be marked
747 * present after it reports ATTACHED on new
748 * dev_num
749 */
750 ret = sdw_assign_device_num(slave);
751 if (ret) {
752 dev_err(slave->bus->dev,
753 "Assign dev_num failed:%d\n",
754 ret);
755 return ret;
756 }
757
758 break;
759 }
760 }
761
762 if (!found) {
763 /* TODO: Park this device in Group 13 */
764
765 /*
766 * add Slave device even if there is no platform
767 * firmware description. There will be no driver probe
768 * but the user/integration will be able to see the
769 * device, enumeration status and device number in sysfs
770 */
771 sdw_slave_add(bus, &id, NULL);
772
773 dev_err(bus->dev, "Slave Entry not found\n");
774 }
775
776 count++;
777
778 /*
779 * Check till error out or retry (count) exhausts.
780 * Device can drop off and rejoin during enumeration
781 * so count till twice the bound.
782 */
783
784 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
785
786 return ret;
787 }
788
sdw_modify_slave_status(struct sdw_slave * slave,enum sdw_slave_status status)789 static void sdw_modify_slave_status(struct sdw_slave *slave,
790 enum sdw_slave_status status)
791 {
792 mutex_lock(&slave->bus->bus_lock);
793
794 dev_vdbg(&slave->dev,
795 "%s: changing status slave %d status %d new status %d\n",
796 __func__, slave->dev_num, slave->status, status);
797
798 if (status == SDW_SLAVE_UNATTACHED) {
799 dev_dbg(&slave->dev,
800 "%s: initializing completion for Slave %d\n",
801 __func__, slave->dev_num);
802
803 init_completion(&slave->enumeration_complete);
804 init_completion(&slave->initialization_complete);
805
806 } else if ((status == SDW_SLAVE_ATTACHED) &&
807 (slave->status == SDW_SLAVE_UNATTACHED)) {
808 dev_dbg(&slave->dev,
809 "%s: signaling completion for Slave %d\n",
810 __func__, slave->dev_num);
811
812 complete(&slave->enumeration_complete);
813 }
814 slave->status = status;
815 mutex_unlock(&slave->bus->bus_lock);
816 }
817
sdw_get_clk_stop_mode(struct sdw_slave * slave)818 static enum sdw_clk_stop_mode sdw_get_clk_stop_mode(struct sdw_slave *slave)
819 {
820 enum sdw_clk_stop_mode mode;
821
822 /*
823 * Query for clock stop mode if Slave implements
824 * ops->get_clk_stop_mode, else read from property.
825 */
826 if (slave->ops && slave->ops->get_clk_stop_mode) {
827 mode = slave->ops->get_clk_stop_mode(slave);
828 } else {
829 if (slave->prop.clk_stop_mode1)
830 mode = SDW_CLK_STOP_MODE1;
831 else
832 mode = SDW_CLK_STOP_MODE0;
833 }
834
835 return mode;
836 }
837
sdw_slave_clk_stop_callback(struct sdw_slave * slave,enum sdw_clk_stop_mode mode,enum sdw_clk_stop_type type)838 static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
839 enum sdw_clk_stop_mode mode,
840 enum sdw_clk_stop_type type)
841 {
842 int ret;
843
844 if (slave->ops && slave->ops->clk_stop) {
845 ret = slave->ops->clk_stop(slave, mode, type);
846 if (ret < 0) {
847 dev_err(&slave->dev,
848 "Clk Stop type =%d failed: %d\n", type, ret);
849 return ret;
850 }
851 }
852
853 return 0;
854 }
855
sdw_slave_clk_stop_prepare(struct sdw_slave * slave,enum sdw_clk_stop_mode mode,bool prepare)856 static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
857 enum sdw_clk_stop_mode mode,
858 bool prepare)
859 {
860 bool wake_en;
861 u32 val = 0;
862 int ret;
863
864 wake_en = slave->prop.wake_capable;
865
866 if (prepare) {
867 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
868
869 if (mode == SDW_CLK_STOP_MODE1)
870 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
871
872 if (wake_en)
873 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
874 } else {
875 val = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
876
877 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
878 }
879
880 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
881
882 if (ret != 0)
883 dev_err(&slave->dev,
884 "Clock Stop prepare failed for slave: %d", ret);
885
886 return ret;
887 }
888
sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus * bus,u16 dev_num)889 static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
890 {
891 int retry = bus->clk_stop_timeout;
892 int val;
893
894 do {
895 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT) &
896 SDW_SCP_STAT_CLK_STP_NF;
897 if (!val) {
898 dev_info(bus->dev, "clock stop prep/de-prep done slave:%d",
899 dev_num);
900 return 0;
901 }
902
903 usleep_range(1000, 1500);
904 retry--;
905 } while (retry);
906
907 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d",
908 dev_num);
909
910 return -ETIMEDOUT;
911 }
912
913 /**
914 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
915 *
916 * @bus: SDW bus instance
917 *
918 * Query Slave for clock stop mode and prepare for that mode.
919 */
sdw_bus_prep_clk_stop(struct sdw_bus * bus)920 int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
921 {
922 enum sdw_clk_stop_mode slave_mode;
923 bool simple_clk_stop = true;
924 struct sdw_slave *slave;
925 bool is_slave = false;
926 int ret = 0;
927
928 /*
929 * In order to save on transition time, prepare
930 * each Slave and then wait for all Slave(s) to be
931 * prepared for clock stop.
932 */
933 list_for_each_entry(slave, &bus->slaves, node) {
934 if (!slave->dev_num)
935 continue;
936
937 if (slave->status != SDW_SLAVE_ATTACHED &&
938 slave->status != SDW_SLAVE_ALERT)
939 continue;
940
941 /* Identify if Slave(s) are available on Bus */
942 is_slave = true;
943
944 slave_mode = sdw_get_clk_stop_mode(slave);
945 slave->curr_clk_stop_mode = slave_mode;
946
947 ret = sdw_slave_clk_stop_callback(slave, slave_mode,
948 SDW_CLK_PRE_PREPARE);
949 if (ret < 0) {
950 dev_err(&slave->dev,
951 "pre-prepare failed:%d", ret);
952 return ret;
953 }
954
955 ret = sdw_slave_clk_stop_prepare(slave,
956 slave_mode, true);
957 if (ret < 0) {
958 dev_err(&slave->dev,
959 "pre-prepare failed:%d", ret);
960 return ret;
961 }
962
963 if (slave_mode == SDW_CLK_STOP_MODE1)
964 simple_clk_stop = false;
965 }
966
967 if (is_slave && !simple_clk_stop) {
968 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
969 SDW_BROADCAST_DEV_NUM);
970 if (ret < 0)
971 return ret;
972 }
973
974 /* Don't need to inform slaves if there is no slave attached */
975 if (!is_slave)
976 return ret;
977
978 /* Inform slaves that prep is done */
979 list_for_each_entry(slave, &bus->slaves, node) {
980 if (!slave->dev_num)
981 continue;
982
983 if (slave->status != SDW_SLAVE_ATTACHED &&
984 slave->status != SDW_SLAVE_ALERT)
985 continue;
986
987 slave_mode = slave->curr_clk_stop_mode;
988
989 if (slave_mode == SDW_CLK_STOP_MODE1) {
990 ret = sdw_slave_clk_stop_callback(slave,
991 slave_mode,
992 SDW_CLK_POST_PREPARE);
993
994 if (ret < 0) {
995 dev_err(&slave->dev,
996 "post-prepare failed:%d", ret);
997 }
998 }
999 }
1000
1001 return ret;
1002 }
1003 EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1004
1005 /**
1006 * sdw_bus_clk_stop: stop bus clock
1007 *
1008 * @bus: SDW bus instance
1009 *
1010 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1011 * write to SCP_CTRL register.
1012 */
sdw_bus_clk_stop(struct sdw_bus * bus)1013 int sdw_bus_clk_stop(struct sdw_bus *bus)
1014 {
1015 int ret;
1016
1017 /*
1018 * broadcast clock stop now, attached Slaves will ACK this,
1019 * unattached will ignore
1020 */
1021 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1022 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1023 if (ret < 0) {
1024 if (ret == -ENODATA)
1025 dev_dbg(bus->dev,
1026 "ClockStopNow Broadcast msg ignored %d", ret);
1027 else
1028 dev_err(bus->dev,
1029 "ClockStopNow Broadcast msg failed %d", ret);
1030 return ret;
1031 }
1032
1033 return 0;
1034 }
1035 EXPORT_SYMBOL(sdw_bus_clk_stop);
1036
1037 /**
1038 * sdw_bus_exit_clk_stop: Exit clock stop mode
1039 *
1040 * @bus: SDW bus instance
1041 *
1042 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1043 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1044 * back.
1045 */
sdw_bus_exit_clk_stop(struct sdw_bus * bus)1046 int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1047 {
1048 enum sdw_clk_stop_mode mode;
1049 bool simple_clk_stop = true;
1050 struct sdw_slave *slave;
1051 bool is_slave = false;
1052 int ret;
1053
1054 /*
1055 * In order to save on transition time, de-prepare
1056 * each Slave and then wait for all Slave(s) to be
1057 * de-prepared after clock resume.
1058 */
1059 list_for_each_entry(slave, &bus->slaves, node) {
1060 if (!slave->dev_num)
1061 continue;
1062
1063 if (slave->status != SDW_SLAVE_ATTACHED &&
1064 slave->status != SDW_SLAVE_ALERT)
1065 continue;
1066
1067 /* Identify if Slave(s) are available on Bus */
1068 is_slave = true;
1069
1070 mode = slave->curr_clk_stop_mode;
1071
1072 if (mode == SDW_CLK_STOP_MODE1) {
1073 simple_clk_stop = false;
1074 continue;
1075 }
1076
1077 ret = sdw_slave_clk_stop_callback(slave, mode,
1078 SDW_CLK_PRE_DEPREPARE);
1079 if (ret < 0)
1080 dev_warn(&slave->dev,
1081 "clk stop deprep failed:%d", ret);
1082
1083 ret = sdw_slave_clk_stop_prepare(slave, mode,
1084 false);
1085
1086 if (ret < 0)
1087 dev_warn(&slave->dev,
1088 "clk stop deprep failed:%d", ret);
1089 }
1090
1091 if (is_slave && !simple_clk_stop)
1092 sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1093
1094 /*
1095 * Don't need to call slave callback function if there is no slave
1096 * attached
1097 */
1098 if (!is_slave)
1099 return 0;
1100
1101 list_for_each_entry(slave, &bus->slaves, node) {
1102 if (!slave->dev_num)
1103 continue;
1104
1105 if (slave->status != SDW_SLAVE_ATTACHED &&
1106 slave->status != SDW_SLAVE_ALERT)
1107 continue;
1108
1109 mode = slave->curr_clk_stop_mode;
1110 sdw_slave_clk_stop_callback(slave, mode,
1111 SDW_CLK_POST_DEPREPARE);
1112 }
1113
1114 return 0;
1115 }
1116 EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1117
sdw_configure_dpn_intr(struct sdw_slave * slave,int port,bool enable,int mask)1118 int sdw_configure_dpn_intr(struct sdw_slave *slave,
1119 int port, bool enable, int mask)
1120 {
1121 u32 addr;
1122 int ret;
1123 u8 val = 0;
1124
1125 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1126 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1127 enable ? "on" : "off");
1128 mask |= SDW_DPN_INT_TEST_FAIL;
1129 }
1130
1131 addr = SDW_DPN_INTMASK(port);
1132
1133 /* Set/Clear port ready interrupt mask */
1134 if (enable) {
1135 val |= mask;
1136 val |= SDW_DPN_INT_PORT_READY;
1137 } else {
1138 val &= ~(mask);
1139 val &= ~SDW_DPN_INT_PORT_READY;
1140 }
1141
1142 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1143 if (ret < 0)
1144 dev_err(slave->bus->dev,
1145 "SDW_DPN_INTMASK write failed:%d\n", val);
1146
1147 return ret;
1148 }
1149
sdw_slave_set_frequency(struct sdw_slave * slave)1150 static int sdw_slave_set_frequency(struct sdw_slave *slave)
1151 {
1152 u32 mclk_freq = slave->bus->prop.mclk_freq;
1153 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1154 unsigned int scale;
1155 u8 scale_index;
1156 u8 base;
1157 int ret;
1158
1159 /*
1160 * frequency base and scale registers are required for SDCA
1161 * devices. They may also be used for 1.2+/non-SDCA devices,
1162 * but we will need a DisCo property to cover this case
1163 */
1164 if (!slave->id.class_id)
1165 return 0;
1166
1167 if (!mclk_freq) {
1168 dev_err(&slave->dev,
1169 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1170 return -EINVAL;
1171 }
1172
1173 /*
1174 * map base frequency using Table 89 of SoundWire 1.2 spec.
1175 * The order of the tests just follows the specification, this
1176 * is not a selection between possible values or a search for
1177 * the best value but just a mapping. Only one case per platform
1178 * is relevant.
1179 * Some BIOS have inconsistent values for mclk_freq but a
1180 * correct root so we force the mclk_freq to avoid variations.
1181 */
1182 if (!(19200000 % mclk_freq)) {
1183 mclk_freq = 19200000;
1184 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1185 } else if (!(24000000 % mclk_freq)) {
1186 mclk_freq = 24000000;
1187 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1188 } else if (!(24576000 % mclk_freq)) {
1189 mclk_freq = 24576000;
1190 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1191 } else if (!(22579200 % mclk_freq)) {
1192 mclk_freq = 22579200;
1193 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1194 } else if (!(32000000 % mclk_freq)) {
1195 mclk_freq = 32000000;
1196 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1197 } else {
1198 dev_err(&slave->dev,
1199 "Unsupported clock base, mclk %d\n",
1200 mclk_freq);
1201 return -EINVAL;
1202 }
1203
1204 if (mclk_freq % curr_freq) {
1205 dev_err(&slave->dev,
1206 "mclk %d is not multiple of bus curr_freq %d\n",
1207 mclk_freq, curr_freq);
1208 return -EINVAL;
1209 }
1210
1211 scale = mclk_freq / curr_freq;
1212
1213 /*
1214 * map scale to Table 90 of SoundWire 1.2 spec - and check
1215 * that the scale is a power of two and maximum 64
1216 */
1217 scale_index = ilog2(scale);
1218
1219 if (BIT(scale_index) != scale || scale_index > 6) {
1220 dev_err(&slave->dev,
1221 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1222 scale, mclk_freq, curr_freq);
1223 return -EINVAL;
1224 }
1225 scale_index++;
1226
1227 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
1228 if (ret < 0) {
1229 dev_err(&slave->dev,
1230 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1231 return ret;
1232 }
1233
1234 /* initialize scale for both banks */
1235 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
1236 if (ret < 0) {
1237 dev_err(&slave->dev,
1238 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1239 return ret;
1240 }
1241 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
1242 if (ret < 0)
1243 dev_err(&slave->dev,
1244 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1245
1246 dev_dbg(&slave->dev,
1247 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1248 base, scale_index, mclk_freq, curr_freq);
1249
1250 return ret;
1251 }
1252
sdw_initialize_slave(struct sdw_slave * slave)1253 static int sdw_initialize_slave(struct sdw_slave *slave)
1254 {
1255 struct sdw_slave_prop *prop = &slave->prop;
1256 int ret;
1257 u8 val;
1258
1259 ret = sdw_slave_set_frequency(slave);
1260 if (ret < 0)
1261 return ret;
1262
1263 /*
1264 * Set SCP_INT1_MASK register, typically bus clash and
1265 * implementation-defined interrupt mask. The Parity detection
1266 * may not always be correct on startup so its use is
1267 * device-dependent, it might e.g. only be enabled in
1268 * steady-state after a couple of frames.
1269 */
1270 val = slave->prop.scp_int1_mask;
1271
1272 /* Enable SCP interrupts */
1273 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
1274 if (ret < 0) {
1275 dev_err(slave->bus->dev,
1276 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
1277 return ret;
1278 }
1279
1280 /* No need to continue if DP0 is not present */
1281 if (!slave->prop.dp0_prop)
1282 return 0;
1283
1284 /* Enable DP0 interrupts */
1285 val = prop->dp0_prop->imp_def_interrupts;
1286 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1287
1288 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
1289 if (ret < 0)
1290 dev_err(slave->bus->dev,
1291 "SDW_DP0_INTMASK read failed:%d\n", ret);
1292 return ret;
1293 }
1294
sdw_handle_dp0_interrupt(struct sdw_slave * slave,u8 * slave_status)1295 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1296 {
1297 u8 clear = 0, impl_int_mask;
1298 int status, status2, ret, count = 0;
1299
1300 status = sdw_read(slave, SDW_DP0_INT);
1301 if (status < 0) {
1302 dev_err(slave->bus->dev,
1303 "SDW_DP0_INT read failed:%d\n", status);
1304 return status;
1305 }
1306
1307 do {
1308 if (status & SDW_DP0_INT_TEST_FAIL) {
1309 dev_err(&slave->dev, "Test fail for port 0\n");
1310 clear |= SDW_DP0_INT_TEST_FAIL;
1311 }
1312
1313 /*
1314 * Assumption: PORT_READY interrupt will be received only for
1315 * ports implementing Channel Prepare state machine (CP_SM)
1316 */
1317
1318 if (status & SDW_DP0_INT_PORT_READY) {
1319 complete(&slave->port_ready[0]);
1320 clear |= SDW_DP0_INT_PORT_READY;
1321 }
1322
1323 if (status & SDW_DP0_INT_BRA_FAILURE) {
1324 dev_err(&slave->dev, "BRA failed\n");
1325 clear |= SDW_DP0_INT_BRA_FAILURE;
1326 }
1327
1328 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1329 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1330
1331 if (status & impl_int_mask) {
1332 clear |= impl_int_mask;
1333 *slave_status = clear;
1334 }
1335
1336 /* clear the interrupt */
1337 ret = sdw_write(slave, SDW_DP0_INT, clear);
1338 if (ret < 0) {
1339 dev_err(slave->bus->dev,
1340 "SDW_DP0_INT write failed:%d\n", ret);
1341 return ret;
1342 }
1343
1344 /* Read DP0 interrupt again */
1345 status2 = sdw_read(slave, SDW_DP0_INT);
1346 if (status2 < 0) {
1347 dev_err(slave->bus->dev,
1348 "SDW_DP0_INT read failed:%d\n", status2);
1349 return status2;
1350 }
1351 status &= status2;
1352
1353 count++;
1354
1355 /* we can get alerts while processing so keep retrying */
1356 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1357
1358 if (count == SDW_READ_INTR_CLEAR_RETRY)
1359 dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read\n");
1360
1361 return ret;
1362 }
1363
sdw_handle_port_interrupt(struct sdw_slave * slave,int port,u8 * slave_status)1364 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
1365 int port, u8 *slave_status)
1366 {
1367 u8 clear = 0, impl_int_mask;
1368 int status, status2, ret, count = 0;
1369 u32 addr;
1370
1371 if (port == 0)
1372 return sdw_handle_dp0_interrupt(slave, slave_status);
1373
1374 addr = SDW_DPN_INT(port);
1375 status = sdw_read(slave, addr);
1376 if (status < 0) {
1377 dev_err(slave->bus->dev,
1378 "SDW_DPN_INT read failed:%d\n", status);
1379
1380 return status;
1381 }
1382
1383 do {
1384 if (status & SDW_DPN_INT_TEST_FAIL) {
1385 dev_err(&slave->dev, "Test fail for port:%d\n", port);
1386 clear |= SDW_DPN_INT_TEST_FAIL;
1387 }
1388
1389 /*
1390 * Assumption: PORT_READY interrupt will be received only
1391 * for ports implementing CP_SM.
1392 */
1393 if (status & SDW_DPN_INT_PORT_READY) {
1394 complete(&slave->port_ready[port]);
1395 clear |= SDW_DPN_INT_PORT_READY;
1396 }
1397
1398 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1399 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1400
1401 if (status & impl_int_mask) {
1402 clear |= impl_int_mask;
1403 *slave_status = clear;
1404 }
1405
1406 /* clear the interrupt */
1407 ret = sdw_write(slave, addr, clear);
1408 if (ret < 0) {
1409 dev_err(slave->bus->dev,
1410 "SDW_DPN_INT write failed:%d\n", ret);
1411 return ret;
1412 }
1413
1414 /* Read DPN interrupt again */
1415 status2 = sdw_read(slave, addr);
1416 if (status2 < 0) {
1417 dev_err(slave->bus->dev,
1418 "SDW_DPN_INT read failed:%d\n", status2);
1419 return status2;
1420 }
1421 status &= status2;
1422
1423 count++;
1424
1425 /* we can get alerts while processing so keep retrying */
1426 } while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1427
1428 if (count == SDW_READ_INTR_CLEAR_RETRY)
1429 dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
1430
1431 return ret;
1432 }
1433
sdw_handle_slave_alerts(struct sdw_slave * slave)1434 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1435 {
1436 struct sdw_slave_intr_status slave_intr;
1437 u8 clear = 0, bit, port_status[15] = {0};
1438 int port_num, stat, ret, count = 0;
1439 unsigned long port;
1440 bool slave_notify = false;
1441 u8 buf, buf2[2], _buf, _buf2[2];
1442 bool parity_check;
1443 bool parity_quirk;
1444
1445 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1446
1447 ret = pm_runtime_get_sync(&slave->dev);
1448 if (ret < 0 && ret != -EACCES) {
1449 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1450 pm_runtime_put_noidle(&slave->dev);
1451 return ret;
1452 }
1453
1454 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
1455 ret = sdw_read(slave, SDW_SCP_INT1);
1456 if (ret < 0) {
1457 dev_err(slave->bus->dev,
1458 "SDW_SCP_INT1 read failed:%d\n", ret);
1459 goto io_err;
1460 }
1461 buf = ret;
1462
1463 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, buf2);
1464 if (ret < 0) {
1465 dev_err(slave->bus->dev,
1466 "SDW_SCP_INT2/3 read failed:%d\n", ret);
1467 goto io_err;
1468 }
1469
1470 do {
1471 /*
1472 * Check parity, bus clash and Slave (impl defined)
1473 * interrupt
1474 */
1475 if (buf & SDW_SCP_INT1_PARITY) {
1476 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1477 parity_quirk = !slave->first_interrupt_done &&
1478 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1479
1480 if (parity_check && !parity_quirk)
1481 dev_err(&slave->dev, "Parity error detected\n");
1482 clear |= SDW_SCP_INT1_PARITY;
1483 }
1484
1485 if (buf & SDW_SCP_INT1_BUS_CLASH) {
1486 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1487 dev_err(&slave->dev, "Bus clash detected\n");
1488 clear |= SDW_SCP_INT1_BUS_CLASH;
1489 }
1490
1491 /*
1492 * When bus clash or parity errors are detected, such errors
1493 * are unlikely to be recoverable errors.
1494 * TODO: In such scenario, reset bus. Make this configurable
1495 * via sysfs property with bus reset being the default.
1496 */
1497
1498 if (buf & SDW_SCP_INT1_IMPL_DEF) {
1499 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1500 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1501 slave_notify = true;
1502 }
1503 clear |= SDW_SCP_INT1_IMPL_DEF;
1504 }
1505
1506 /* Check port 0 - 3 interrupts */
1507 port = buf & SDW_SCP_INT1_PORT0_3;
1508
1509 /* To get port number corresponding to bits, shift it */
1510 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
1511 for_each_set_bit(bit, &port, 8) {
1512 sdw_handle_port_interrupt(slave, bit,
1513 &port_status[bit]);
1514 }
1515
1516 /* Check if cascade 2 interrupt is present */
1517 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1518 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1519 for_each_set_bit(bit, &port, 8) {
1520 /* scp2 ports start from 4 */
1521 port_num = bit + 3;
1522 sdw_handle_port_interrupt(slave,
1523 port_num,
1524 &port_status[port_num]);
1525 }
1526 }
1527
1528 /* now check last cascade */
1529 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1530 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1531 for_each_set_bit(bit, &port, 8) {
1532 /* scp3 ports start from 11 */
1533 port_num = bit + 10;
1534 sdw_handle_port_interrupt(slave,
1535 port_num,
1536 &port_status[port_num]);
1537 }
1538 }
1539
1540 /* Update the Slave driver */
1541 if (slave_notify && slave->ops &&
1542 slave->ops->interrupt_callback) {
1543 slave_intr.control_port = clear;
1544 memcpy(slave_intr.port, &port_status,
1545 sizeof(slave_intr.port));
1546
1547 slave->ops->interrupt_callback(slave, &slave_intr);
1548 }
1549
1550 /* Ack interrupt */
1551 ret = sdw_write(slave, SDW_SCP_INT1, clear);
1552 if (ret < 0) {
1553 dev_err(slave->bus->dev,
1554 "SDW_SCP_INT1 write failed:%d\n", ret);
1555 goto io_err;
1556 }
1557
1558 /* at this point all initial interrupt sources were handled */
1559 slave->first_interrupt_done = true;
1560
1561 /*
1562 * Read status again to ensure no new interrupts arrived
1563 * while servicing interrupts.
1564 */
1565 ret = sdw_read(slave, SDW_SCP_INT1);
1566 if (ret < 0) {
1567 dev_err(slave->bus->dev,
1568 "SDW_SCP_INT1 read failed:%d\n", ret);
1569 goto io_err;
1570 }
1571 _buf = ret;
1572
1573 ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, _buf2);
1574 if (ret < 0) {
1575 dev_err(slave->bus->dev,
1576 "SDW_SCP_INT2/3 read failed:%d\n", ret);
1577 goto io_err;
1578 }
1579
1580 /* Make sure no interrupts are pending */
1581 buf &= _buf;
1582 buf2[0] &= _buf2[0];
1583 buf2[1] &= _buf2[1];
1584 stat = buf || buf2[0] || buf2[1];
1585
1586 /*
1587 * Exit loop if Slave is continuously in ALERT state even
1588 * after servicing the interrupt multiple times.
1589 */
1590 count++;
1591
1592 /* we can get alerts while processing so keep retrying */
1593 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1594
1595 if (count == SDW_READ_INTR_CLEAR_RETRY)
1596 dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read\n");
1597
1598 io_err:
1599 pm_runtime_mark_last_busy(&slave->dev);
1600 pm_runtime_put_autosuspend(&slave->dev);
1601
1602 return ret;
1603 }
1604
sdw_update_slave_status(struct sdw_slave * slave,enum sdw_slave_status status)1605 static int sdw_update_slave_status(struct sdw_slave *slave,
1606 enum sdw_slave_status status)
1607 {
1608 unsigned long time;
1609
1610 if (!slave->probed) {
1611 /*
1612 * the slave status update is typically handled in an
1613 * interrupt thread, which can race with the driver
1614 * probe, e.g. when a module needs to be loaded.
1615 *
1616 * make sure the probe is complete before updating
1617 * status.
1618 */
1619 time = wait_for_completion_timeout(&slave->probe_complete,
1620 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT));
1621 if (!time) {
1622 dev_err(&slave->dev, "Probe not complete, timed out\n");
1623 return -ETIMEDOUT;
1624 }
1625 }
1626
1627 if (!slave->ops || !slave->ops->update_status)
1628 return 0;
1629
1630 return slave->ops->update_status(slave, status);
1631 }
1632
1633 /**
1634 * sdw_handle_slave_status() - Handle Slave status
1635 * @bus: SDW bus instance
1636 * @status: Status for all Slave(s)
1637 */
sdw_handle_slave_status(struct sdw_bus * bus,enum sdw_slave_status status[])1638 int sdw_handle_slave_status(struct sdw_bus *bus,
1639 enum sdw_slave_status status[])
1640 {
1641 enum sdw_slave_status prev_status;
1642 struct sdw_slave *slave;
1643 bool attached_initializing;
1644 int i, ret = 0;
1645
1646 /* first check if any Slaves fell off the bus */
1647 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1648 mutex_lock(&bus->bus_lock);
1649 if (test_bit(i, bus->assigned) == false) {
1650 mutex_unlock(&bus->bus_lock);
1651 continue;
1652 }
1653 mutex_unlock(&bus->bus_lock);
1654
1655 slave = sdw_get_slave(bus, i);
1656 if (!slave)
1657 continue;
1658
1659 if (status[i] == SDW_SLAVE_UNATTACHED &&
1660 slave->status != SDW_SLAVE_UNATTACHED)
1661 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1662 }
1663
1664 if (status[0] == SDW_SLAVE_ATTACHED) {
1665 dev_dbg(bus->dev, "Slave attached, programming device number\n");
1666 ret = sdw_program_device_num(bus);
1667 if (ret)
1668 dev_err(bus->dev, "Slave attach failed: %d\n", ret);
1669 /*
1670 * programming a device number will have side effects,
1671 * so we deal with other devices at a later time
1672 */
1673 return ret;
1674 }
1675
1676 /* Continue to check other slave statuses */
1677 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1678 mutex_lock(&bus->bus_lock);
1679 if (test_bit(i, bus->assigned) == false) {
1680 mutex_unlock(&bus->bus_lock);
1681 continue;
1682 }
1683 mutex_unlock(&bus->bus_lock);
1684
1685 slave = sdw_get_slave(bus, i);
1686 if (!slave)
1687 continue;
1688
1689 attached_initializing = false;
1690
1691 switch (status[i]) {
1692 case SDW_SLAVE_UNATTACHED:
1693 if (slave->status == SDW_SLAVE_UNATTACHED)
1694 break;
1695
1696 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1697 break;
1698
1699 case SDW_SLAVE_ALERT:
1700 ret = sdw_handle_slave_alerts(slave);
1701 if (ret)
1702 dev_err(bus->dev,
1703 "Slave %d alert handling failed: %d\n",
1704 i, ret);
1705 break;
1706
1707 case SDW_SLAVE_ATTACHED:
1708 if (slave->status == SDW_SLAVE_ATTACHED)
1709 break;
1710
1711 prev_status = slave->status;
1712 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1713
1714 if (prev_status == SDW_SLAVE_ALERT)
1715 break;
1716
1717 attached_initializing = true;
1718
1719 ret = sdw_initialize_slave(slave);
1720 if (ret)
1721 dev_err(bus->dev,
1722 "Slave %d initialization failed: %d\n",
1723 i, ret);
1724
1725 break;
1726
1727 default:
1728 dev_err(bus->dev, "Invalid slave %d status:%d\n",
1729 i, status[i]);
1730 break;
1731 }
1732
1733 ret = sdw_update_slave_status(slave, status[i]);
1734 if (ret)
1735 dev_err(slave->bus->dev,
1736 "Update Slave status failed:%d\n", ret);
1737 if (attached_initializing)
1738 complete(&slave->initialization_complete);
1739 }
1740
1741 return ret;
1742 }
1743 EXPORT_SYMBOL(sdw_handle_slave_status);
1744
sdw_clear_slave_status(struct sdw_bus * bus,u32 request)1745 void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1746 {
1747 struct sdw_slave *slave;
1748 int i;
1749
1750 /* Check all non-zero devices */
1751 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1752 mutex_lock(&bus->bus_lock);
1753 if (test_bit(i, bus->assigned) == false) {
1754 mutex_unlock(&bus->bus_lock);
1755 continue;
1756 }
1757 mutex_unlock(&bus->bus_lock);
1758
1759 slave = sdw_get_slave(bus, i);
1760 if (!slave)
1761 continue;
1762
1763 if (slave->status != SDW_SLAVE_UNATTACHED) {
1764 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1765 slave->first_interrupt_done = false;
1766 }
1767
1768 /* keep track of request, used in pm_runtime resume */
1769 slave->unattach_request = request;
1770 }
1771 }
1772 EXPORT_SYMBOL(sdw_clear_slave_status);
1773