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