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