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