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