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/mod_devicetable.h>
6 #include <linux/pm_runtime.h>
7 #include <linux/soundwire/sdw_registers.h>
8 #include <linux/soundwire/sdw.h>
9 #include "bus.h"
10 
11 /**
12  * sdw_add_bus_master() - add a bus Master instance
13  * @bus: bus instance
14  *
15  * Initializes the bus instance, read properties and create child
16  * devices.
17  */
sdw_add_bus_master(struct sdw_bus * bus)18 int sdw_add_bus_master(struct sdw_bus *bus)
19 {
20 	struct sdw_master_prop *prop = NULL;
21 	int ret;
22 
23 	if (!bus->dev) {
24 		pr_err("SoundWire bus has no device");
25 		return -ENODEV;
26 	}
27 
28 	if (!bus->ops) {
29 		dev_err(bus->dev, "SoundWire Bus ops are not set");
30 		return -EINVAL;
31 	}
32 
33 	mutex_init(&bus->msg_lock);
34 	mutex_init(&bus->bus_lock);
35 	INIT_LIST_HEAD(&bus->slaves);
36 	INIT_LIST_HEAD(&bus->m_rt_list);
37 
38 	if (bus->ops->read_prop) {
39 		ret = bus->ops->read_prop(bus);
40 		if (ret < 0) {
41 			dev_err(bus->dev, "Bus read properties failed:%d", ret);
42 			return ret;
43 		}
44 	}
45 
46 	/*
47 	 * Device numbers in SoundWire are 0 thru 15. Enumeration device
48 	 * number (0), Broadcast device number (15), Group numbers (12 and
49 	 * 13) and Master device number (14) are not used for assignment so
50 	 * mask these and other higher bits.
51 	 */
52 
53 	/* Set higher order bits */
54 	*bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
55 
56 	/* Set enumuration device number and broadcast device number */
57 	set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
58 	set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
59 
60 	/* Set group device numbers and master device number */
61 	set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
62 	set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
63 	set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
64 
65 	/*
66 	 * SDW is an enumerable bus, but devices can be powered off. So,
67 	 * they won't be able to report as present.
68 	 *
69 	 * Create Slave devices based on Slaves described in
70 	 * the respective firmware (ACPI/DT)
71 	 */
72 	if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
73 		ret = sdw_acpi_find_slaves(bus);
74 	else
75 		ret = -ENOTSUPP; /* No ACPI/DT so error out */
76 
77 	if (ret) {
78 		dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
79 		return ret;
80 	}
81 
82 	/*
83 	 * Initialize clock values based on Master properties. The max
84 	 * frequency is read from max_freq property. Current assumption
85 	 * is that the bus will start at highest clock frequency when
86 	 * powered on.
87 	 *
88 	 * Default active bank will be 0 as out of reset the Slaves have
89 	 * to start with bank 0 (Table 40 of Spec)
90 	 */
91 	prop = &bus->prop;
92 	bus->params.max_dr_freq = prop->max_freq * SDW_DOUBLE_RATE_FACTOR;
93 	bus->params.curr_dr_freq = bus->params.max_dr_freq;
94 	bus->params.curr_bank = SDW_BANK0;
95 	bus->params.next_bank = SDW_BANK1;
96 
97 	return 0;
98 }
99 EXPORT_SYMBOL(sdw_add_bus_master);
100 
sdw_delete_slave(struct device * dev,void * data)101 static int sdw_delete_slave(struct device *dev, void *data)
102 {
103 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
104 	struct sdw_bus *bus = slave->bus;
105 
106 	mutex_lock(&bus->bus_lock);
107 
108 	if (slave->dev_num) /* clear dev_num if assigned */
109 		clear_bit(slave->dev_num, bus->assigned);
110 
111 	list_del_init(&slave->node);
112 	mutex_unlock(&bus->bus_lock);
113 
114 	device_unregister(dev);
115 	return 0;
116 }
117 
118 /**
119  * sdw_delete_bus_master() - delete the bus master instance
120  * @bus: bus to be deleted
121  *
122  * Remove the instance, delete the child devices.
123  */
sdw_delete_bus_master(struct sdw_bus * bus)124 void sdw_delete_bus_master(struct sdw_bus *bus)
125 {
126 	device_for_each_child(bus->dev, NULL, sdw_delete_slave);
127 }
128 EXPORT_SYMBOL(sdw_delete_bus_master);
129 
130 /*
131  * SDW IO Calls
132  */
133 
find_response_code(enum sdw_command_response resp)134 static inline int find_response_code(enum sdw_command_response resp)
135 {
136 	switch (resp) {
137 	case SDW_CMD_OK:
138 		return 0;
139 
140 	case SDW_CMD_IGNORED:
141 		return -ENODATA;
142 
143 	case SDW_CMD_TIMEOUT:
144 		return -ETIMEDOUT;
145 
146 	default:
147 		return -EIO;
148 	}
149 }
150 
do_transfer(struct sdw_bus * bus,struct sdw_msg * msg)151 static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
152 {
153 	int retry = bus->prop.err_threshold;
154 	enum sdw_command_response resp;
155 	int ret = 0, i;
156 
157 	for (i = 0; i <= retry; i++) {
158 		resp = bus->ops->xfer_msg(bus, msg);
159 		ret = find_response_code(resp);
160 
161 		/* if cmd is ok or ignored return */
162 		if (ret == 0 || ret == -ENODATA)
163 			return ret;
164 	}
165 
166 	return ret;
167 }
168 
do_transfer_defer(struct sdw_bus * bus,struct sdw_msg * msg,struct sdw_defer * defer)169 static inline int do_transfer_defer(struct sdw_bus *bus,
170 			struct sdw_msg *msg, struct sdw_defer *defer)
171 {
172 	int retry = bus->prop.err_threshold;
173 	enum sdw_command_response resp;
174 	int ret = 0, i;
175 
176 	defer->msg = msg;
177 	defer->length = msg->len;
178 	init_completion(&defer->complete);
179 
180 	for (i = 0; i <= retry; i++) {
181 		resp = bus->ops->xfer_msg_defer(bus, msg, defer);
182 		ret = find_response_code(resp);
183 		/* if cmd is ok or ignored return */
184 		if (ret == 0 || ret == -ENODATA)
185 			return ret;
186 	}
187 
188 	return ret;
189 }
190 
sdw_reset_page(struct sdw_bus * bus,u16 dev_num)191 static int sdw_reset_page(struct sdw_bus *bus, u16 dev_num)
192 {
193 	int retry = bus->prop.err_threshold;
194 	enum sdw_command_response resp;
195 	int ret = 0, i;
196 
197 	for (i = 0; i <= retry; i++) {
198 		resp = bus->ops->reset_page_addr(bus, dev_num);
199 		ret = find_response_code(resp);
200 		/* if cmd is ok or ignored return */
201 		if (ret == 0 || ret == -ENODATA)
202 			return ret;
203 	}
204 
205 	return ret;
206 }
207 
208 /**
209  * sdw_transfer() - Synchronous transfer message to a SDW Slave device
210  * @bus: SDW bus
211  * @msg: SDW message to be xfered
212  */
sdw_transfer(struct sdw_bus * bus,struct sdw_msg * msg)213 int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
214 {
215 	int ret;
216 
217 	mutex_lock(&bus->msg_lock);
218 
219 	ret = do_transfer(bus, msg);
220 	if (ret != 0 && ret != -ENODATA)
221 		dev_err(bus->dev, "trf on Slave %d failed:%d\n",
222 				msg->dev_num, ret);
223 
224 	if (msg->page)
225 		sdw_reset_page(bus, msg->dev_num);
226 
227 	mutex_unlock(&bus->msg_lock);
228 
229 	return ret;
230 }
231 
232 /**
233  * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
234  * @bus: SDW bus
235  * @msg: SDW message to be xfered
236  * @defer: Defer block for signal completion
237  *
238  * Caller needs to hold the msg_lock lock while calling this
239  */
sdw_transfer_defer(struct sdw_bus * bus,struct sdw_msg * msg,struct sdw_defer * defer)240 int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
241 				struct sdw_defer *defer)
242 {
243 	int ret;
244 
245 	if (!bus->ops->xfer_msg_defer)
246 		return -ENOTSUPP;
247 
248 	ret = do_transfer_defer(bus, msg, defer);
249 	if (ret != 0 && ret != -ENODATA)
250 		dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
251 				msg->dev_num, ret);
252 
253 	if (msg->page)
254 		sdw_reset_page(bus, msg->dev_num);
255 
256 	return ret;
257 }
258 
259 
sdw_fill_msg(struct sdw_msg * msg,struct sdw_slave * slave,u32 addr,size_t count,u16 dev_num,u8 flags,u8 * buf)260 int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
261 		u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
262 {
263 	memset(msg, 0, sizeof(*msg));
264 	msg->addr = addr; /* addr is 16 bit and truncated here */
265 	msg->len = count;
266 	msg->dev_num = dev_num;
267 	msg->flags = flags;
268 	msg->buf = buf;
269 	msg->ssp_sync = false;
270 	msg->page = false;
271 
272 	if (addr < SDW_REG_NO_PAGE) { /* no paging area */
273 		return 0;
274 	} else if (addr >= SDW_REG_MAX) { /* illegal addr */
275 		pr_err("SDW: Invalid address %x passed\n", addr);
276 		return -EINVAL;
277 	}
278 
279 	if (addr < SDW_REG_OPTIONAL_PAGE) { /* 32k but no page */
280 		if (slave && !slave->prop.paging_support)
281 			return 0;
282 		/* no need for else as that will fall thru to paging */
283 	}
284 
285 	/* paging mandatory */
286 	if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
287 		pr_err("SDW: Invalid device for paging :%d\n", dev_num);
288 		return -EINVAL;
289 	}
290 
291 	if (!slave) {
292 		pr_err("SDW: No slave for paging addr\n");
293 		return -EINVAL;
294 	} else if (!slave->prop.paging_support) {
295 		dev_err(&slave->dev,
296 			"address %x needs paging but no support", addr);
297 		return -EINVAL;
298 	}
299 
300 	msg->addr_page1 = (addr >> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE1_MASK));
301 	msg->addr_page2 = (addr >> SDW_REG_SHIFT(SDW_SCP_ADDRPAGE2_MASK));
302 	msg->addr |= BIT(15);
303 	msg->page = true;
304 
305 	return 0;
306 }
307 
308 /**
309  * sdw_nread() - Read "n" contiguous SDW Slave registers
310  * @slave: SDW Slave
311  * @addr: Register address
312  * @count: length
313  * @val: Buffer for values to be read
314  */
sdw_nread(struct sdw_slave * slave,u32 addr,size_t count,u8 * val)315 int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
316 {
317 	struct sdw_msg msg;
318 	int ret;
319 
320 	ret = sdw_fill_msg(&msg, slave, addr, count,
321 			slave->dev_num, SDW_MSG_FLAG_READ, val);
322 	if (ret < 0)
323 		return ret;
324 
325 	ret = pm_runtime_get_sync(slave->bus->dev);
326 	if (ret < 0)
327 		return ret;
328 
329 	ret = sdw_transfer(slave->bus, &msg);
330 	pm_runtime_put(slave->bus->dev);
331 
332 	return ret;
333 }
334 EXPORT_SYMBOL(sdw_nread);
335 
336 /**
337  * sdw_nwrite() - Write "n" contiguous SDW Slave registers
338  * @slave: SDW Slave
339  * @addr: Register address
340  * @count: length
341  * @val: Buffer for values to be read
342  */
sdw_nwrite(struct sdw_slave * slave,u32 addr,size_t count,u8 * val)343 int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
344 {
345 	struct sdw_msg msg;
346 	int ret;
347 
348 	ret = sdw_fill_msg(&msg, slave, addr, count,
349 			slave->dev_num, SDW_MSG_FLAG_WRITE, val);
350 	if (ret < 0)
351 		return ret;
352 
353 	ret = pm_runtime_get_sync(slave->bus->dev);
354 	if (ret < 0)
355 		return ret;
356 
357 	ret = sdw_transfer(slave->bus, &msg);
358 	pm_runtime_put(slave->bus->dev);
359 
360 	return ret;
361 }
362 EXPORT_SYMBOL(sdw_nwrite);
363 
364 /**
365  * sdw_read() - Read a SDW Slave register
366  * @slave: SDW Slave
367  * @addr: Register address
368  */
sdw_read(struct sdw_slave * slave,u32 addr)369 int sdw_read(struct sdw_slave *slave, u32 addr)
370 {
371 	u8 buf;
372 	int ret;
373 
374 	ret = sdw_nread(slave, addr, 1, &buf);
375 	if (ret < 0)
376 		return ret;
377 	else
378 		return buf;
379 }
380 EXPORT_SYMBOL(sdw_read);
381 
382 /**
383  * sdw_write() - Write a SDW Slave register
384  * @slave: SDW Slave
385  * @addr: Register address
386  * @value: Register value
387  */
sdw_write(struct sdw_slave * slave,u32 addr,u8 value)388 int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
389 {
390 	return sdw_nwrite(slave, addr, 1, &value);
391 
392 }
393 EXPORT_SYMBOL(sdw_write);
394 
395 /*
396  * SDW alert handling
397  */
398 
399 /* called with bus_lock held */
sdw_get_slave(struct sdw_bus * bus,int i)400 static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
401 {
402 	struct sdw_slave *slave = NULL;
403 
404 	list_for_each_entry(slave, &bus->slaves, node) {
405 		if (slave->dev_num == i)
406 			return slave;
407 	}
408 
409 	return NULL;
410 }
411 
sdw_compare_devid(struct sdw_slave * slave,struct sdw_slave_id id)412 static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
413 {
414 
415 	if ((slave->id.unique_id != id.unique_id) ||
416 	    (slave->id.mfg_id != id.mfg_id) ||
417 	    (slave->id.part_id != id.part_id) ||
418 	    (slave->id.class_id != id.class_id))
419 		return -ENODEV;
420 
421 	return 0;
422 }
423 
424 /* called with bus_lock held */
sdw_get_device_num(struct sdw_slave * slave)425 static int sdw_get_device_num(struct sdw_slave *slave)
426 {
427 	int bit;
428 
429 	bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
430 	if (bit == SDW_MAX_DEVICES) {
431 		bit = -ENODEV;
432 		goto err;
433 	}
434 
435 	/*
436 	 * Do not update dev_num in Slave data structure here,
437 	 * Update once program dev_num is successful
438 	 */
439 	set_bit(bit, slave->bus->assigned);
440 
441 err:
442 	return bit;
443 }
444 
sdw_assign_device_num(struct sdw_slave * slave)445 static int sdw_assign_device_num(struct sdw_slave *slave)
446 {
447 	int ret, dev_num;
448 
449 	/* check first if device number is assigned, if so reuse that */
450 	if (!slave->dev_num) {
451 		mutex_lock(&slave->bus->bus_lock);
452 		dev_num = sdw_get_device_num(slave);
453 		mutex_unlock(&slave->bus->bus_lock);
454 		if (dev_num < 0) {
455 			dev_err(slave->bus->dev, "Get dev_num failed: %d",
456 								dev_num);
457 			return dev_num;
458 		}
459 	} else {
460 		dev_info(slave->bus->dev,
461 				"Slave already registered dev_num:%d",
462 				slave->dev_num);
463 
464 		/* Clear the slave->dev_num to transfer message on device 0 */
465 		dev_num = slave->dev_num;
466 		slave->dev_num = 0;
467 
468 	}
469 
470 	ret = sdw_write(slave, SDW_SCP_DEVNUMBER, dev_num);
471 	if (ret < 0) {
472 		dev_err(&slave->dev, "Program device_num failed: %d", ret);
473 		return ret;
474 	}
475 
476 	/* After xfer of msg, restore dev_num */
477 	slave->dev_num = dev_num;
478 
479 	return 0;
480 }
481 
sdw_extract_slave_id(struct sdw_bus * bus,u64 addr,struct sdw_slave_id * id)482 void sdw_extract_slave_id(struct sdw_bus *bus,
483 			u64 addr, struct sdw_slave_id *id)
484 {
485 	dev_dbg(bus->dev, "SDW Slave Addr: %llx", addr);
486 
487 	/*
488 	 * Spec definition
489 	 *   Register		Bit	Contents
490 	 *   DevId_0 [7:4]	47:44	sdw_version
491 	 *   DevId_0 [3:0]	43:40	unique_id
492 	 *   DevId_1		39:32	mfg_id [15:8]
493 	 *   DevId_2		31:24	mfg_id [7:0]
494 	 *   DevId_3		23:16	part_id [15:8]
495 	 *   DevId_4		15:08	part_id [7:0]
496 	 *   DevId_5		07:00	class_id
497 	 */
498 	id->sdw_version = (addr >> 44) & GENMASK(3, 0);
499 	id->unique_id = (addr >> 40) & GENMASK(3, 0);
500 	id->mfg_id = (addr >> 24) & GENMASK(15, 0);
501 	id->part_id = (addr >> 8) & GENMASK(15, 0);
502 	id->class_id = addr & GENMASK(7, 0);
503 
504 	dev_dbg(bus->dev,
505 		"SDW Slave class_id %x, part_id %x, mfg_id %x, unique_id %x, version %x",
506 				id->class_id, id->part_id, id->mfg_id,
507 				id->unique_id, id->sdw_version);
508 
509 }
510 
sdw_program_device_num(struct sdw_bus * bus)511 static int sdw_program_device_num(struct sdw_bus *bus)
512 {
513 	u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
514 	struct sdw_slave *slave, *_s;
515 	struct sdw_slave_id id;
516 	struct sdw_msg msg;
517 	bool found;
518 	int count = 0, ret;
519 	u64 addr;
520 
521 	/* No Slave, so use raw xfer api */
522 	ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
523 			SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
524 	if (ret < 0)
525 		return ret;
526 
527 	do {
528 		ret = sdw_transfer(bus, &msg);
529 		if (ret == -ENODATA) { /* end of device id reads */
530 			ret = 0;
531 			break;
532 		}
533 		if (ret < 0) {
534 			dev_err(bus->dev, "DEVID read fail:%d\n", ret);
535 			break;
536 		}
537 
538 		/*
539 		 * Construct the addr and extract. Cast the higher shift
540 		 * bits to avoid truncation due to size limit.
541 		 */
542 		addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
543 			((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
544 			((u64)buf[0] << 40);
545 
546 		sdw_extract_slave_id(bus, addr, &id);
547 
548 		found = false;
549 		/* Now compare with entries */
550 		list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
551 			if (sdw_compare_devid(slave, id) == 0) {
552 				found = true;
553 
554 				/*
555 				 * Assign a new dev_num to this Slave and
556 				 * not mark it present. It will be marked
557 				 * present after it reports ATTACHED on new
558 				 * dev_num
559 				 */
560 				ret = sdw_assign_device_num(slave);
561 				if (ret) {
562 					dev_err(slave->bus->dev,
563 						"Assign dev_num failed:%d",
564 						ret);
565 					return ret;
566 				}
567 
568 				break;
569 			}
570 		}
571 
572 		if (found == false) {
573 			/* TODO: Park this device in Group 13 */
574 			dev_err(bus->dev, "Slave Entry not found");
575 		}
576 
577 		count++;
578 
579 		/*
580 		 * Check till error out or retry (count) exhausts.
581 		 * Device can drop off and rejoin during enumeration
582 		 * so count till twice the bound.
583 		 */
584 
585 	} while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
586 
587 	return ret;
588 }
589 
sdw_modify_slave_status(struct sdw_slave * slave,enum sdw_slave_status status)590 static void sdw_modify_slave_status(struct sdw_slave *slave,
591 				enum sdw_slave_status status)
592 {
593 	mutex_lock(&slave->bus->bus_lock);
594 	slave->status = status;
595 	mutex_unlock(&slave->bus->bus_lock);
596 }
597 
sdw_configure_dpn_intr(struct sdw_slave * slave,int port,bool enable,int mask)598 int sdw_configure_dpn_intr(struct sdw_slave *slave,
599 			int port, bool enable, int mask)
600 {
601 	u32 addr;
602 	int ret;
603 	u8 val = 0;
604 
605 	addr = SDW_DPN_INTMASK(port);
606 
607 	/* Set/Clear port ready interrupt mask */
608 	if (enable) {
609 		val |= mask;
610 		val |= SDW_DPN_INT_PORT_READY;
611 	} else {
612 		val &= ~(mask);
613 		val &= ~SDW_DPN_INT_PORT_READY;
614 	}
615 
616 	ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
617 	if (ret < 0)
618 		dev_err(slave->bus->dev,
619 				"SDW_DPN_INTMASK write failed:%d", val);
620 
621 	return ret;
622 }
623 
sdw_initialize_slave(struct sdw_slave * slave)624 static int sdw_initialize_slave(struct sdw_slave *slave)
625 {
626 	struct sdw_slave_prop *prop = &slave->prop;
627 	int ret;
628 	u8 val;
629 
630 	/*
631 	 * Set bus clash, parity and SCP implementation
632 	 * defined interrupt mask
633 	 * TODO: Read implementation defined interrupt mask
634 	 * from Slave property
635 	 */
636 	val = SDW_SCP_INT1_IMPL_DEF | SDW_SCP_INT1_BUS_CLASH |
637 					SDW_SCP_INT1_PARITY;
638 
639 	/* Enable SCP interrupts */
640 	ret = sdw_update(slave, SDW_SCP_INTMASK1, val, val);
641 	if (ret < 0) {
642 		dev_err(slave->bus->dev,
643 				"SDW_SCP_INTMASK1 write failed:%d", ret);
644 		return ret;
645 	}
646 
647 	/* No need to continue if DP0 is not present */
648 	if (!slave->prop.dp0_prop)
649 		return 0;
650 
651 	/* Enable DP0 interrupts */
652 	val = prop->dp0_prop->device_interrupts;
653 	val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
654 
655 	ret = sdw_update(slave, SDW_DP0_INTMASK, val, val);
656 	if (ret < 0) {
657 		dev_err(slave->bus->dev,
658 				"SDW_DP0_INTMASK read failed:%d", ret);
659 		return val;
660 	}
661 
662 	return 0;
663 }
664 
sdw_handle_dp0_interrupt(struct sdw_slave * slave,u8 * slave_status)665 static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
666 {
667 	u8 clear = 0, impl_int_mask;
668 	int status, status2, ret, count = 0;
669 
670 	status = sdw_read(slave, SDW_DP0_INT);
671 	if (status < 0) {
672 		dev_err(slave->bus->dev,
673 				"SDW_DP0_INT read failed:%d", status);
674 		return status;
675 	}
676 
677 	do {
678 
679 		if (status & SDW_DP0_INT_TEST_FAIL) {
680 			dev_err(&slave->dev, "Test fail for port 0");
681 			clear |= SDW_DP0_INT_TEST_FAIL;
682 		}
683 
684 		/*
685 		 * Assumption: PORT_READY interrupt will be received only for
686 		 * ports implementing Channel Prepare state machine (CP_SM)
687 		 */
688 
689 		if (status & SDW_DP0_INT_PORT_READY) {
690 			complete(&slave->port_ready[0]);
691 			clear |= SDW_DP0_INT_PORT_READY;
692 		}
693 
694 		if (status & SDW_DP0_INT_BRA_FAILURE) {
695 			dev_err(&slave->dev, "BRA failed");
696 			clear |= SDW_DP0_INT_BRA_FAILURE;
697 		}
698 
699 		impl_int_mask = SDW_DP0_INT_IMPDEF1 |
700 			SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
701 
702 		if (status & impl_int_mask) {
703 			clear |= impl_int_mask;
704 			*slave_status = clear;
705 		}
706 
707 		/* clear the interrupt */
708 		ret = sdw_write(slave, SDW_DP0_INT, clear);
709 		if (ret < 0) {
710 			dev_err(slave->bus->dev,
711 				"SDW_DP0_INT write failed:%d", ret);
712 			return ret;
713 		}
714 
715 		/* Read DP0 interrupt again */
716 		status2 = sdw_read(slave, SDW_DP0_INT);
717 		if (status2 < 0) {
718 			dev_err(slave->bus->dev,
719 				"SDW_DP0_INT read failed:%d", status2);
720 			return status2;
721 		}
722 		status &= status2;
723 
724 		count++;
725 
726 		/* we can get alerts while processing so keep retrying */
727 	} while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
728 
729 	if (count == SDW_READ_INTR_CLEAR_RETRY)
730 		dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read");
731 
732 	return ret;
733 }
734 
sdw_handle_port_interrupt(struct sdw_slave * slave,int port,u8 * slave_status)735 static int sdw_handle_port_interrupt(struct sdw_slave *slave,
736 		int port, u8 *slave_status)
737 {
738 	u8 clear = 0, impl_int_mask;
739 	int status, status2, ret, count = 0;
740 	u32 addr;
741 
742 	if (port == 0)
743 		return sdw_handle_dp0_interrupt(slave, slave_status);
744 
745 	addr = SDW_DPN_INT(port);
746 	status = sdw_read(slave, addr);
747 	if (status < 0) {
748 		dev_err(slave->bus->dev,
749 				"SDW_DPN_INT read failed:%d", status);
750 
751 		return status;
752 	}
753 
754 	do {
755 
756 		if (status & SDW_DPN_INT_TEST_FAIL) {
757 			dev_err(&slave->dev, "Test fail for port:%d", port);
758 			clear |= SDW_DPN_INT_TEST_FAIL;
759 		}
760 
761 		/*
762 		 * Assumption: PORT_READY interrupt will be received only
763 		 * for ports implementing CP_SM.
764 		 */
765 		if (status & SDW_DPN_INT_PORT_READY) {
766 			complete(&slave->port_ready[port]);
767 			clear |= SDW_DPN_INT_PORT_READY;
768 		}
769 
770 		impl_int_mask = SDW_DPN_INT_IMPDEF1 |
771 			SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
772 
773 
774 		if (status & impl_int_mask) {
775 			clear |= impl_int_mask;
776 			*slave_status = clear;
777 		}
778 
779 		/* clear the interrupt */
780 		ret = sdw_write(slave, addr, clear);
781 		if (ret < 0) {
782 			dev_err(slave->bus->dev,
783 					"SDW_DPN_INT write failed:%d", ret);
784 			return ret;
785 		}
786 
787 		/* Read DPN interrupt again */
788 		status2 = sdw_read(slave, addr);
789 		if (status2 < 0) {
790 			dev_err(slave->bus->dev,
791 					"SDW_DPN_INT read failed:%d", status2);
792 			return status2;
793 		}
794 		status &= status2;
795 
796 		count++;
797 
798 		/* we can get alerts while processing so keep retrying */
799 	} while (status != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
800 
801 	if (count == SDW_READ_INTR_CLEAR_RETRY)
802 		dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
803 
804 	return ret;
805 }
806 
sdw_handle_slave_alerts(struct sdw_slave * slave)807 static int sdw_handle_slave_alerts(struct sdw_slave *slave)
808 {
809 	struct sdw_slave_intr_status slave_intr;
810 	u8 clear = 0, bit, port_status[15] = {0};
811 	int port_num, stat, ret, count = 0;
812 	unsigned long port;
813 	bool slave_notify = false;
814 	u8 buf, buf2[2], _buf, _buf2[2];
815 
816 	sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
817 
818 	/* Read Instat 1, Instat 2 and Instat 3 registers */
819 	buf = ret = sdw_read(slave, SDW_SCP_INT1);
820 	if (ret < 0) {
821 		dev_err(slave->bus->dev,
822 					"SDW_SCP_INT1 read failed:%d", ret);
823 		return ret;
824 	}
825 
826 	ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, buf2);
827 	if (ret < 0) {
828 		dev_err(slave->bus->dev,
829 					"SDW_SCP_INT2/3 read failed:%d", ret);
830 		return ret;
831 	}
832 
833 	do {
834 		/*
835 		 * Check parity, bus clash and Slave (impl defined)
836 		 * interrupt
837 		 */
838 		if (buf & SDW_SCP_INT1_PARITY) {
839 			dev_err(&slave->dev, "Parity error detected");
840 			clear |= SDW_SCP_INT1_PARITY;
841 		}
842 
843 		if (buf & SDW_SCP_INT1_BUS_CLASH) {
844 			dev_err(&slave->dev, "Bus clash error detected");
845 			clear |= SDW_SCP_INT1_BUS_CLASH;
846 		}
847 
848 		/*
849 		 * When bus clash or parity errors are detected, such errors
850 		 * are unlikely to be recoverable errors.
851 		 * TODO: In such scenario, reset bus. Make this configurable
852 		 * via sysfs property with bus reset being the default.
853 		 */
854 
855 		if (buf & SDW_SCP_INT1_IMPL_DEF) {
856 			dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
857 			clear |= SDW_SCP_INT1_IMPL_DEF;
858 			slave_notify = true;
859 		}
860 
861 		/* Check port 0 - 3 interrupts */
862 		port = buf & SDW_SCP_INT1_PORT0_3;
863 
864 		/* To get port number corresponding to bits, shift it */
865 		port = port >> SDW_REG_SHIFT(SDW_SCP_INT1_PORT0_3);
866 		for_each_set_bit(bit, &port, 8) {
867 			sdw_handle_port_interrupt(slave, bit,
868 						&port_status[bit]);
869 
870 		}
871 
872 		/* Check if cascade 2 interrupt is present */
873 		if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
874 			port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
875 			for_each_set_bit(bit, &port, 8) {
876 				/* scp2 ports start from 4 */
877 				port_num = bit + 3;
878 				sdw_handle_port_interrupt(slave,
879 						port_num,
880 						&port_status[port_num]);
881 			}
882 		}
883 
884 		/* now check last cascade */
885 		if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
886 			port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
887 			for_each_set_bit(bit, &port, 8) {
888 				/* scp3 ports start from 11 */
889 				port_num = bit + 10;
890 				sdw_handle_port_interrupt(slave,
891 						port_num,
892 						&port_status[port_num]);
893 			}
894 		}
895 
896 		/* Update the Slave driver */
897 		if (slave_notify && (slave->ops) &&
898 					(slave->ops->interrupt_callback)) {
899 			slave_intr.control_port = clear;
900 			memcpy(slave_intr.port, &port_status,
901 						sizeof(slave_intr.port));
902 
903 			slave->ops->interrupt_callback(slave, &slave_intr);
904 		}
905 
906 		/* Ack interrupt */
907 		ret = sdw_write(slave, SDW_SCP_INT1, clear);
908 		if (ret < 0) {
909 			dev_err(slave->bus->dev,
910 					"SDW_SCP_INT1 write failed:%d", ret);
911 			return ret;
912 		}
913 
914 		/*
915 		 * Read status again to ensure no new interrupts arrived
916 		 * while servicing interrupts.
917 		 */
918 		_buf = ret = sdw_read(slave, SDW_SCP_INT1);
919 		if (ret < 0) {
920 			dev_err(slave->bus->dev,
921 					"SDW_SCP_INT1 read failed:%d", ret);
922 			return ret;
923 		}
924 
925 		ret = sdw_nread(slave, SDW_SCP_INTSTAT2, 2, _buf2);
926 		if (ret < 0) {
927 			dev_err(slave->bus->dev,
928 					"SDW_SCP_INT2/3 read failed:%d", ret);
929 			return ret;
930 		}
931 
932 		/* Make sure no interrupts are pending */
933 		buf &= _buf;
934 		buf2[0] &= _buf2[0];
935 		buf2[1] &= _buf2[1];
936 		stat = buf || buf2[0] || buf2[1];
937 
938 		/*
939 		 * Exit loop if Slave is continuously in ALERT state even
940 		 * after servicing the interrupt multiple times.
941 		 */
942 		count++;
943 
944 		/* we can get alerts while processing so keep retrying */
945 	} while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
946 
947 	if (count == SDW_READ_INTR_CLEAR_RETRY)
948 		dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read");
949 
950 	return ret;
951 }
952 
sdw_update_slave_status(struct sdw_slave * slave,enum sdw_slave_status status)953 static int sdw_update_slave_status(struct sdw_slave *slave,
954 				enum sdw_slave_status status)
955 {
956 	if ((slave->ops) && (slave->ops->update_status))
957 		return slave->ops->update_status(slave, status);
958 
959 	return 0;
960 }
961 
962 /**
963  * sdw_handle_slave_status() - Handle Slave status
964  * @bus: SDW bus instance
965  * @status: Status for all Slave(s)
966  */
sdw_handle_slave_status(struct sdw_bus * bus,enum sdw_slave_status status[])967 int sdw_handle_slave_status(struct sdw_bus *bus,
968 			enum sdw_slave_status status[])
969 {
970 	enum sdw_slave_status prev_status;
971 	struct sdw_slave *slave;
972 	int i, ret = 0;
973 
974 	if (status[0] == SDW_SLAVE_ATTACHED) {
975 		ret = sdw_program_device_num(bus);
976 		if (ret)
977 			dev_err(bus->dev, "Slave attach failed: %d", ret);
978 	}
979 
980 	/* Continue to check other slave statuses */
981 	for (i = 1; i <= SDW_MAX_DEVICES; i++) {
982 		mutex_lock(&bus->bus_lock);
983 		if (test_bit(i, bus->assigned) == false) {
984 			mutex_unlock(&bus->bus_lock);
985 			continue;
986 		}
987 		mutex_unlock(&bus->bus_lock);
988 
989 		slave = sdw_get_slave(bus, i);
990 		if (!slave)
991 			continue;
992 
993 		switch (status[i]) {
994 		case SDW_SLAVE_UNATTACHED:
995 			if (slave->status == SDW_SLAVE_UNATTACHED)
996 				break;
997 
998 			sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
999 			break;
1000 
1001 		case SDW_SLAVE_ALERT:
1002 			ret = sdw_handle_slave_alerts(slave);
1003 			if (ret)
1004 				dev_err(bus->dev,
1005 					"Slave %d alert handling failed: %d",
1006 					i, ret);
1007 			break;
1008 
1009 		case SDW_SLAVE_ATTACHED:
1010 			if (slave->status == SDW_SLAVE_ATTACHED)
1011 				break;
1012 
1013 			prev_status = slave->status;
1014 			sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1015 
1016 			if (prev_status == SDW_SLAVE_ALERT)
1017 				break;
1018 
1019 			ret = sdw_initialize_slave(slave);
1020 			if (ret)
1021 				dev_err(bus->dev,
1022 					"Slave %d initialization failed: %d",
1023 					i, ret);
1024 
1025 			break;
1026 
1027 		default:
1028 			dev_err(bus->dev, "Invalid slave %d status:%d",
1029 							i, status[i]);
1030 			break;
1031 		}
1032 
1033 		ret = sdw_update_slave_status(slave, status[i]);
1034 		if (ret)
1035 			dev_err(slave->bus->dev,
1036 				"Update Slave status failed:%d", ret);
1037 
1038 	}
1039 
1040 	return ret;
1041 }
1042 EXPORT_SYMBOL(sdw_handle_slave_status);
1043