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