blob: 45e9fc9f472a14964c97a5d6134d4e626aa52331 [file] [log] [blame]
Vinod Koul7c3cd182017-12-14 11:19:34 +05301// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4#include <linux/acpi.h>
Rander Wang02314532020-01-14 18:08:43 -06005#include <linux/delay.h>
Vinod Koul7c3cd182017-12-14 11:19:34 +05306#include <linux/mod_devicetable.h>
Vinod Koul9d715fa2017-12-14 11:19:37 +05307#include <linux/pm_runtime.h>
8#include <linux/soundwire/sdw_registers.h>
Vinod Koul7c3cd182017-12-14 11:19:34 +05309#include <linux/soundwire/sdw.h>
10#include "bus.h"
Pierre-Louis Bossartbcac5902020-05-19 04:35:51 +080011#include "sysfs_local.h"
Vinod Koul7c3cd182017-12-14 11:19:34 +053012
Bard Liaodbb50c72020-05-19 01:43:20 +080013static DEFINE_IDA(sdw_ida);
14
15static 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
Vinod Koul7c3cd182017-12-14 11:19:34 +053026/**
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +080027 * sdw_bus_master_add() - add a bus Master instance
Vinod Koul7c3cd182017-12-14 11:19:34 +053028 * @bus: bus instance
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +080029 * @parent: parent device
30 * @fwnode: firmware node handle
Vinod Koul7c3cd182017-12-14 11:19:34 +053031 *
32 * Initializes the bus instance, read properties and create child
33 * devices.
34 */
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +080035int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
36 struct fwnode_handle *fwnode)
Vinod Koul7c3cd182017-12-14 11:19:34 +053037{
Sanyog Kale5c3eb9f2018-04-26 18:38:33 +053038 struct sdw_master_prop *prop = NULL;
Vinod Koul7c3cd182017-12-14 11:19:34 +053039 int ret;
40
Pierre-Louis Bossart7ceaa402020-05-19 01:43:21 +080041 if (!parent) {
42 pr_err("SoundWire parent device is not set\n");
Vinod Koul7c3cd182017-12-14 11:19:34 +053043 return -ENODEV;
44 }
45
Bard Liaodbb50c72020-05-19 01:43:20 +080046 ret = sdw_get_id(bus);
47 if (ret) {
Pierre-Louis Bossart7ceaa402020-05-19 01:43:21 +080048 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);
Bard Liaodbb50c72020-05-19 01:43:20 +080056 return ret;
57 }
58
Vinod Koul9d715fa2017-12-14 11:19:37 +053059 if (!bus->ops) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -050060 dev_err(bus->dev, "SoundWire Bus ops are not set\n");
Vinod Koul9d715fa2017-12-14 11:19:37 +053061 return -EINVAL;
62 }
63
Bard Liao90261182020-09-08 21:15:20 +080064 if (!bus->compute_params) {
65 dev_err(bus->dev,
66 "Bandwidth allocation not configured, compute_params no set\n");
67 return -EINVAL;
68 }
69
Vinod Koul9d715fa2017-12-14 11:19:37 +053070 mutex_init(&bus->msg_lock);
Vinod Koul7c3cd182017-12-14 11:19:34 +053071 mutex_init(&bus->bus_lock);
72 INIT_LIST_HEAD(&bus->slaves);
Sanyog Kale89e59052018-04-26 18:38:08 +053073 INIT_LIST_HEAD(&bus->m_rt_list);
Vinod Koul7c3cd182017-12-14 11:19:34 +053074
Shreyas NCce6e74d2018-07-27 14:44:16 +053075 /*
76 * Initialize multi_link flag
77 * TODO: populate this flag by reading property from FW node
78 */
79 bus->multi_link = false;
Vinod Koul56d4fe32017-12-14 11:19:35 +053080 if (bus->ops->read_prop) {
81 ret = bus->ops->read_prop(bus);
82 if (ret < 0) {
Vinod Koul62f0cec2019-05-02 16:29:24 +053083 dev_err(bus->dev,
84 "Bus read properties failed:%d\n", ret);
Vinod Koul56d4fe32017-12-14 11:19:35 +053085 return ret;
86 }
87 }
88
Pierre-Louis Bossartbf034732019-08-21 13:58:18 -050089 sdw_bus_debugfs_init(bus);
90
Vinod Koul7c3cd182017-12-14 11:19:34 +053091 /*
Pierre-Louis Bossart21c2de22019-05-01 10:57:28 -050092 * Device numbers in SoundWire are 0 through 15. Enumeration device
Vinod Koul7c3cd182017-12-14 11:19:34 +053093 * 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);
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +0100119 else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
120 ret = sdw_of_find_slaves(bus);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530121 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
Sanyog Kale99b8a5d2018-04-26 18:38:28 +0530129 /*
Sanyog Kale5c3eb9f2018-04-26 18:38:33 +0530130 * Initialize clock values based on Master properties. The max
Pierre-Louis Bossart34243052019-05-22 14:47:22 -0500131 * frequency is read from max_clk_freq property. Current assumption
Sanyog Kale5c3eb9f2018-04-26 18:38:33 +0530132 * is that the bus will start at highest clock frequency when
133 * powered on.
134 *
Sanyog Kale99b8a5d2018-04-26 18:38:28 +0530135 * Default active bank will be 0 as out of reset the Slaves have
136 * to start with bank 0 (Table 40 of Spec)
137 */
Sanyog Kale5c3eb9f2018-04-26 18:38:33 +0530138 prop = &bus->prop;
Pierre-Louis Bossart34243052019-05-22 14:47:22 -0500139 bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
Sanyog Kale5c3eb9f2018-04-26 18:38:33 +0530140 bus->params.curr_dr_freq = bus->params.max_dr_freq;
Sanyog Kale99b8a5d2018-04-26 18:38:28 +0530141 bus->params.curr_bank = SDW_BANK0;
142 bus->params.next_bank = SDW_BANK1;
143
Vinod Koul7c3cd182017-12-14 11:19:34 +0530144 return 0;
145}
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800146EXPORT_SYMBOL(sdw_bus_master_add);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530147
148static 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
Pierre-Louis Bossartdff70572020-01-14 18:08:41 -0600153 pm_runtime_disable(dev);
154
Pierre-Louis Bossartbf034732019-08-21 13:58:18 -0500155 sdw_slave_debugfs_exit(slave);
156
Vinod Koul7c3cd182017-12-14 11:19:34 +0530157 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/**
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800170 * sdw_bus_master_delete() - delete the bus master instance
Vinod Koul7c3cd182017-12-14 11:19:34 +0530171 * @bus: bus to be deleted
172 *
173 * Remove the instance, delete the child devices.
174 */
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800175void sdw_bus_master_delete(struct sdw_bus *bus)
Vinod Koul7c3cd182017-12-14 11:19:34 +0530176{
177 device_for_each_child(bus->dev, NULL, sdw_delete_slave);
Pierre-Louis Bossart7ceaa402020-05-19 01:43:21 +0800178 sdw_master_device_del(bus);
Pierre-Louis Bossartbf034732019-08-21 13:58:18 -0500179
180 sdw_bus_debugfs_exit(bus);
Bard Liaodbb50c72020-05-19 01:43:20 +0800181 ida_free(&sdw_ida, bus->id);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530182}
Pierre-Louis Bossart5cab3ff2020-05-19 01:43:18 +0800183EXPORT_SYMBOL(sdw_bus_master_delete);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530184
Vinod Koul9d715fa2017-12-14 11:19:37 +0530185/*
186 * SDW IO Calls
187 */
188
189static 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
206static 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
224static inline int do_transfer_defer(struct sdw_bus *bus,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500225 struct sdw_msg *msg,
226 struct sdw_defer *defer)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530227{
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;
Shreyas NCa306a0e2018-07-27 14:44:12 +0530234 init_completion(&defer->complete);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530235
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
247static 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
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800264static 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)
Bard Liaoec475182021-01-15 13:37:36 +0800270 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);
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800274
275 if (msg->page)
276 sdw_reset_page(bus, msg->dev_num);
277
278 return ret;
279}
280
Vinod Koul9d715fa2017-12-14 11:19:37 +0530281/**
282 * sdw_transfer() - Synchronous transfer message to a SDW Slave device
283 * @bus: SDW bus
284 * @msg: SDW message to be xfered
285 */
286int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
287{
288 int ret;
289
290 mutex_lock(&bus->msg_lock);
291
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800292 ret = sdw_transfer_unlocked(bus, msg);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530293
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 */
307int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500308 struct sdw_defer *defer)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530309{
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",
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500318 msg->dev_num, ret);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530319
320 if (msg->page)
321 sdw_reset_page(bus, msg->dev_num);
322
323 return ret;
324}
325
Vinod Koul9d715fa2017-12-14 11:19:37 +0530326int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500327 u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530328{
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;
Vinod Koul9d715fa2017-12-14 11:19:37 +0530335
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200336 if (addr < SDW_REG_NO_PAGE) /* no paging area */
Vinod Koul9d715fa2017-12-14 11:19:37 +0530337 return 0;
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200338
339 if (addr >= SDW_REG_MAX) { /* illegal addr */
Vinod Koul9d715fa2017-12-14 11:19:37 +0530340 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;
Pierre-Louis Bossart21c2de22019-05-01 10:57:28 -0500347 /* no need for else as that will fall-through to paging */
Vinod Koul9d715fa2017-12-14 11:19:37 +0530348 }
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;
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200359 }
360
361 if (!slave->prop.paging_support) {
Vinod Koul9d715fa2017-12-14 11:19:37 +0530362 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500363 "address %x needs paging but no support\n", addr);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530364 return -EINVAL;
365 }
366
Vinod Kould5826a4b2020-09-03 17:14:57 +0530367 msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
368 msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530369 msg->addr |= BIT(15);
370 msg->page = true;
371
372 return 0;
373}
374
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600375/*
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
Vinod Koul9d715fa2017-12-14 11:19:37 +0530380 */
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600381
382static int
383sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530384{
385 struct sdw_msg msg;
386 int ret;
387
388 ret = sdw_fill_msg(&msg, slave, addr, count,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500389 slave->dev_num, SDW_MSG_FLAG_READ, val);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530390 if (ret < 0)
391 return ret;
392
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600393 return sdw_transfer(slave->bus, &msg);
394}
395
396static int
397sdw_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);
Shreyas NCc22c0ae2018-01-09 10:15:46 +0530404 if (ret < 0)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530405 return ret;
406
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600407 return sdw_transfer(slave->bus, &msg);
408}
409
Bard Liao167790a2021-01-22 15:06:29 +0800410int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600411{
412 return sdw_nwrite_no_pm(slave, addr, 1, &value);
413}
Bard Liao167790a2021-01-22 15:06:29 +0800414EXPORT_SYMBOL(sdw_write_no_pm);
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600415
Rander Wang02314532020-01-14 18:08:43 -0600416static int
417sdw_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;
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200431
432 return buf;
Rander Wang02314532020-01-14 18:08:43 -0600433}
434
435static int
436sdw_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
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800449int 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}
466EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
467
468int 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}
480EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
481
Bard Liao167790a2021-01-22 15:06:29 +0800482int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
Rander Wang02314532020-01-14 18:08:43 -0600483{
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}
Bard Liao167790a2021-01-22 15:06:29 +0800493EXPORT_SYMBOL(sdw_read_no_pm);
Rander Wang02314532020-01-14 18:08:43 -0600494
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +0800495static 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
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600507/**
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 */
514int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
515{
516 int ret;
517
518 ret = pm_runtime_get_sync(slave->bus->dev);
519 if (ret < 0 && ret != -EACCES) {
520 pm_runtime_put_noidle(slave->bus->dev);
521 return ret;
522 }
523
524 ret = sdw_nread_no_pm(slave, addr, count, val);
525
526 pm_runtime_mark_last_busy(slave->bus->dev);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530527 pm_runtime_put(slave->bus->dev);
528
529 return ret;
530}
531EXPORT_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 */
540int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
541{
Vinod Koul9d715fa2017-12-14 11:19:37 +0530542 int ret;
543
Vinod Koul9d715fa2017-12-14 11:19:37 +0530544 ret = pm_runtime_get_sync(slave->bus->dev);
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600545 if (ret < 0 && ret != -EACCES) {
546 pm_runtime_put_noidle(slave->bus->dev);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530547 return ret;
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600548 }
Vinod Koul9d715fa2017-12-14 11:19:37 +0530549
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600550 ret = sdw_nwrite_no_pm(slave, addr, count, val);
551
552 pm_runtime_mark_last_busy(slave->bus->dev);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530553 pm_runtime_put(slave->bus->dev);
554
555 return ret;
556}
557EXPORT_SYMBOL(sdw_nwrite);
558
559/**
560 * sdw_read() - Read a SDW Slave register
561 * @slave: SDW Slave
562 * @addr: Register address
563 */
564int 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;
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200572
573 return buf;
Vinod Koul9d715fa2017-12-14 11:19:37 +0530574}
575EXPORT_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 */
583int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
584{
585 return sdw_nwrite(slave, addr, 1, &value);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530586}
587EXPORT_SYMBOL(sdw_write);
588
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530589/*
590 * SDW alert handling
591 */
592
593/* called with bus_lock held */
594static 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
606static int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
607{
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500608 if (slave->id.mfg_id != id.mfg_id ||
Pierre-Louis Bossart09830d52019-05-01 10:57:29 -0500609 slave->id.part_id != id.part_id ||
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500610 slave->id.class_id != id.class_id ||
611 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
612 slave->id.unique_id != id.unique_id))
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530613 return -ENODEV;
614
615 return 0;
616}
617
618/* called with bus_lock held */
619static 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
635err:
636 return bit;
637}
638
639static int sdw_assign_device_num(struct sdw_slave *slave)
640{
641 int ret, dev_num;
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600642 bool new_device = false;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530643
644 /* check first if device number is assigned, if so reuse that */
645 if (!slave->dev_num) {
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600646 if (!slave->dev_num_sticky) {
647 mutex_lock(&slave->bus->bus_lock);
648 dev_num = sdw_get_device_num(slave);
649 mutex_unlock(&slave->bus->bus_lock);
650 if (dev_num < 0) {
651 dev_err(slave->bus->dev, "Get dev_num failed: %d\n",
652 dev_num);
653 return dev_num;
654 }
655 slave->dev_num = dev_num;
656 slave->dev_num_sticky = dev_num;
657 new_device = true;
658 } else {
659 slave->dev_num = slave->dev_num_sticky;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530660 }
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600661 }
662
663 if (!new_device)
Pierre-Louis Bossartf48f4fd2020-04-20 02:51:17 +0800664 dev_dbg(slave->bus->dev,
665 "Slave already registered, reusing dev_num:%d\n",
666 slave->dev_num);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530667
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600668 /* Clear the slave->dev_num to transfer message on device 0 */
669 dev_num = slave->dev_num;
670 slave->dev_num = 0;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530671
Pierre-Louis Bossartd300de4f2020-01-14 18:08:39 -0600672 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530673 if (ret < 0) {
Pierre-Louis Bossart6e0ac6a2019-08-05 19:55:09 -0500674 dev_err(&slave->dev, "Program device_num %d failed: %d\n",
675 dev_num, ret);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530676 return ret;
677 }
678
679 /* After xfer of msg, restore dev_num */
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600680 slave->dev_num = slave->dev_num_sticky;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530681
682 return 0;
683}
684
Vinod Koul7c3cd182017-12-14 11:19:34 +0530685void sdw_extract_slave_id(struct sdw_bus *bus,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500686 u64 addr, struct sdw_slave_id *id)
Vinod Koul7c3cd182017-12-14 11:19:34 +0530687{
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500688 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530689
Pierre-Louis Bossart2c6cff62020-02-25 11:00:41 -0600690 id->sdw_version = SDW_VERSION(addr);
691 id->unique_id = SDW_UNIQUE_ID(addr);
692 id->mfg_id = SDW_MFG_ID(addr);
693 id->part_id = SDW_PART_ID(addr);
694 id->class_id = SDW_CLASS_ID(addr);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530695
696 dev_dbg(bus->dev,
Pierre-Louis Bossartc397efb2021-01-15 13:37:34 +0800697 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
698 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530699}
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530700
701static int sdw_program_device_num(struct sdw_bus *bus)
702{
703 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
704 struct sdw_slave *slave, *_s;
705 struct sdw_slave_id id;
706 struct sdw_msg msg;
707 bool found = false;
708 int count = 0, ret;
709 u64 addr;
710
711 /* No Slave, so use raw xfer api */
712 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500713 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530714 if (ret < 0)
715 return ret;
716
717 do {
718 ret = sdw_transfer(bus, &msg);
719 if (ret == -ENODATA) { /* end of device id reads */
Pierre-Louis Bossart6e0ac6a2019-08-05 19:55:09 -0500720 dev_dbg(bus->dev, "No more devices to enumerate\n");
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530721 ret = 0;
722 break;
723 }
724 if (ret < 0) {
725 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
726 break;
727 }
728
729 /*
730 * Construct the addr and extract. Cast the higher shift
731 * bits to avoid truncation due to size limit.
732 */
733 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
Colin Ian King0132af052018-01-08 22:22:42 +0530734 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
735 ((u64)buf[0] << 40);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530736
737 sdw_extract_slave_id(bus, addr, &id);
738
739 /* Now compare with entries */
740 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
741 if (sdw_compare_devid(slave, id) == 0) {
742 found = true;
743
744 /*
745 * Assign a new dev_num to this Slave and
746 * not mark it present. It will be marked
747 * present after it reports ATTACHED on new
748 * dev_num
749 */
750 ret = sdw_assign_device_num(slave);
751 if (ret) {
752 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500753 "Assign dev_num failed:%d\n",
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530754 ret);
755 return ret;
756 }
757
758 break;
759 }
760 }
761
Pierre-Louis Bossartd7b956b2019-05-01 10:57:30 -0500762 if (!found) {
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530763 /* TODO: Park this device in Group 13 */
Srinivas Kandagatlafcb9d732020-09-24 14:44:29 -0500764
765 /*
766 * add Slave device even if there is no platform
767 * firmware description. There will be no driver probe
768 * but the user/integration will be able to see the
769 * device, enumeration status and device number in sysfs
770 */
771 sdw_slave_add(bus, &id, NULL);
772
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500773 dev_err(bus->dev, "Slave Entry not found\n");
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530774 }
775
776 count++;
777
778 /*
779 * Check till error out or retry (count) exhausts.
780 * Device can drop off and rejoin during enumeration
781 * so count till twice the bound.
782 */
783
784 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
785
786 return ret;
787}
788
789static void sdw_modify_slave_status(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500790 enum sdw_slave_status status)
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530791{
792 mutex_lock(&slave->bus->bus_lock);
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600793
794 dev_vdbg(&slave->dev,
795 "%s: changing status slave %d status %d new status %d\n",
796 __func__, slave->dev_num, slave->status, status);
797
798 if (status == SDW_SLAVE_UNATTACHED) {
799 dev_dbg(&slave->dev,
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +0800800 "%s: initializing enumeration and init completion for Slave %d\n",
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600801 __func__, slave->dev_num);
802
803 init_completion(&slave->enumeration_complete);
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -0600804 init_completion(&slave->initialization_complete);
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600805
806 } else if ((status == SDW_SLAVE_ATTACHED) &&
807 (slave->status == SDW_SLAVE_UNATTACHED)) {
808 dev_dbg(&slave->dev,
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +0800809 "%s: signaling enumeration completion for Slave %d\n",
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600810 __func__, slave->dev_num);
811
812 complete(&slave->enumeration_complete);
813 }
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530814 slave->status = status;
815 mutex_unlock(&slave->bus->bus_lock);
816}
817
Rander Wang02314532020-01-14 18:08:43 -0600818static enum sdw_clk_stop_mode sdw_get_clk_stop_mode(struct sdw_slave *slave)
819{
820 enum sdw_clk_stop_mode mode;
821
822 /*
823 * Query for clock stop mode if Slave implements
824 * ops->get_clk_stop_mode, else read from property.
825 */
826 if (slave->ops && slave->ops->get_clk_stop_mode) {
827 mode = slave->ops->get_clk_stop_mode(slave);
828 } else {
829 if (slave->prop.clk_stop_mode1)
830 mode = SDW_CLK_STOP_MODE1;
831 else
832 mode = SDW_CLK_STOP_MODE0;
833 }
834
835 return mode;
836}
837
838static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
839 enum sdw_clk_stop_mode mode,
840 enum sdw_clk_stop_type type)
841{
842 int ret;
843
844 if (slave->ops && slave->ops->clk_stop) {
845 ret = slave->ops->clk_stop(slave, mode, type);
846 if (ret < 0) {
847 dev_err(&slave->dev,
848 "Clk Stop type =%d failed: %d\n", type, ret);
849 return ret;
850 }
851 }
852
853 return 0;
854}
855
856static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
857 enum sdw_clk_stop_mode mode,
858 bool prepare)
859{
860 bool wake_en;
861 u32 val = 0;
862 int ret;
863
864 wake_en = slave->prop.wake_capable;
865
866 if (prepare) {
867 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
868
869 if (mode == SDW_CLK_STOP_MODE1)
870 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
871
872 if (wake_en)
873 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
874 } else {
875 val = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
876
877 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
878 }
879
880 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
881
882 if (ret != 0)
883 dev_err(&slave->dev,
884 "Clock Stop prepare failed for slave: %d", ret);
885
886 return ret;
887}
888
889static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
890{
891 int retry = bus->clk_stop_timeout;
892 int val;
893
894 do {
895 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT) &
896 SDW_SCP_STAT_CLK_STP_NF;
897 if (!val) {
898 dev_info(bus->dev, "clock stop prep/de-prep done slave:%d",
899 dev_num);
900 return 0;
901 }
902
903 usleep_range(1000, 1500);
904 retry--;
905 } while (retry);
906
907 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d",
908 dev_num);
909
910 return -ETIMEDOUT;
911}
912
913/**
914 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
915 *
916 * @bus: SDW bus instance
917 *
918 * Query Slave for clock stop mode and prepare for that mode.
919 */
920int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
921{
922 enum sdw_clk_stop_mode slave_mode;
923 bool simple_clk_stop = true;
924 struct sdw_slave *slave;
925 bool is_slave = false;
926 int ret = 0;
927
928 /*
929 * In order to save on transition time, prepare
930 * each Slave and then wait for all Slave(s) to be
931 * prepared for clock stop.
932 */
933 list_for_each_entry(slave, &bus->slaves, node) {
934 if (!slave->dev_num)
935 continue;
936
Rander Wang02314532020-01-14 18:08:43 -0600937 if (slave->status != SDW_SLAVE_ATTACHED &&
938 slave->status != SDW_SLAVE_ALERT)
939 continue;
940
Bard Liao929cfee2020-05-31 23:18:06 +0800941 /* Identify if Slave(s) are available on Bus */
942 is_slave = true;
943
Rander Wang02314532020-01-14 18:08:43 -0600944 slave_mode = sdw_get_clk_stop_mode(slave);
945 slave->curr_clk_stop_mode = slave_mode;
946
947 ret = sdw_slave_clk_stop_callback(slave, slave_mode,
948 SDW_CLK_PRE_PREPARE);
949 if (ret < 0) {
950 dev_err(&slave->dev,
951 "pre-prepare failed:%d", ret);
952 return ret;
953 }
954
955 ret = sdw_slave_clk_stop_prepare(slave,
956 slave_mode, true);
957 if (ret < 0) {
958 dev_err(&slave->dev,
959 "pre-prepare failed:%d", ret);
960 return ret;
961 }
962
963 if (slave_mode == SDW_CLK_STOP_MODE1)
964 simple_clk_stop = false;
965 }
966
Chao Song18de2f722021-01-26 16:54:39 +0800967 /* Skip remaining clock stop preparation if no Slave is attached */
968 if (!is_slave)
969 return ret;
970
971 if (!simple_clk_stop) {
Rander Wang02314532020-01-14 18:08:43 -0600972 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
973 SDW_BROADCAST_DEV_NUM);
974 if (ret < 0)
975 return ret;
976 }
977
978 /* Inform slaves that prep is done */
979 list_for_each_entry(slave, &bus->slaves, node) {
980 if (!slave->dev_num)
981 continue;
982
983 if (slave->status != SDW_SLAVE_ATTACHED &&
984 slave->status != SDW_SLAVE_ALERT)
985 continue;
986
987 slave_mode = slave->curr_clk_stop_mode;
988
989 if (slave_mode == SDW_CLK_STOP_MODE1) {
990 ret = sdw_slave_clk_stop_callback(slave,
991 slave_mode,
992 SDW_CLK_POST_PREPARE);
993
994 if (ret < 0) {
995 dev_err(&slave->dev,
996 "post-prepare failed:%d", ret);
997 }
998 }
999 }
1000
1001 return ret;
1002}
1003EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1004
1005/**
1006 * sdw_bus_clk_stop: stop bus clock
1007 *
1008 * @bus: SDW bus instance
1009 *
1010 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1011 * write to SCP_CTRL register.
1012 */
1013int sdw_bus_clk_stop(struct sdw_bus *bus)
1014{
1015 int ret;
1016
1017 /*
1018 * broadcast clock stop now, attached Slaves will ACK this,
1019 * unattached will ignore
1020 */
1021 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1022 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1023 if (ret < 0) {
Pierre-Louis Bossartdde73532020-01-14 18:08:44 -06001024 if (ret == -ENODATA)
1025 dev_dbg(bus->dev,
1026 "ClockStopNow Broadcast msg ignored %d", ret);
1027 else
1028 dev_err(bus->dev,
1029 "ClockStopNow Broadcast msg failed %d", ret);
Rander Wang02314532020-01-14 18:08:43 -06001030 return ret;
1031 }
1032
1033 return 0;
1034}
1035EXPORT_SYMBOL(sdw_bus_clk_stop);
1036
1037/**
1038 * sdw_bus_exit_clk_stop: Exit clock stop mode
1039 *
1040 * @bus: SDW bus instance
1041 *
1042 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1043 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1044 * back.
1045 */
1046int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1047{
1048 enum sdw_clk_stop_mode mode;
1049 bool simple_clk_stop = true;
1050 struct sdw_slave *slave;
1051 bool is_slave = false;
1052 int ret;
1053
1054 /*
1055 * In order to save on transition time, de-prepare
1056 * each Slave and then wait for all Slave(s) to be
1057 * de-prepared after clock resume.
1058 */
1059 list_for_each_entry(slave, &bus->slaves, node) {
1060 if (!slave->dev_num)
1061 continue;
1062
Rander Wang02314532020-01-14 18:08:43 -06001063 if (slave->status != SDW_SLAVE_ATTACHED &&
1064 slave->status != SDW_SLAVE_ALERT)
1065 continue;
1066
Bard Liao929cfee2020-05-31 23:18:06 +08001067 /* Identify if Slave(s) are available on Bus */
1068 is_slave = true;
1069
Rander Wang02314532020-01-14 18:08:43 -06001070 mode = slave->curr_clk_stop_mode;
1071
1072 if (mode == SDW_CLK_STOP_MODE1) {
1073 simple_clk_stop = false;
1074 continue;
1075 }
1076
1077 ret = sdw_slave_clk_stop_callback(slave, mode,
1078 SDW_CLK_PRE_DEPREPARE);
1079 if (ret < 0)
1080 dev_warn(&slave->dev,
1081 "clk stop deprep failed:%d", ret);
1082
1083 ret = sdw_slave_clk_stop_prepare(slave, mode,
1084 false);
1085
1086 if (ret < 0)
1087 dev_warn(&slave->dev,
1088 "clk stop deprep failed:%d", ret);
1089 }
1090
Chao Song18de2f722021-01-26 16:54:39 +08001091 /* Skip remaining clock stop de-preparation if no Slave is attached */
Bard Liao929cfee2020-05-31 23:18:06 +08001092 if (!is_slave)
1093 return 0;
1094
Chao Song18de2f722021-01-26 16:54:39 +08001095 if (!simple_clk_stop)
1096 sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1097
Rander Wang02314532020-01-14 18:08:43 -06001098 list_for_each_entry(slave, &bus->slaves, node) {
1099 if (!slave->dev_num)
1100 continue;
1101
1102 if (slave->status != SDW_SLAVE_ATTACHED &&
1103 slave->status != SDW_SLAVE_ALERT)
1104 continue;
1105
1106 mode = slave->curr_clk_stop_mode;
1107 sdw_slave_clk_stop_callback(slave, mode,
1108 SDW_CLK_POST_DEPREPARE);
1109 }
1110
1111 return 0;
1112}
1113EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1114
Sanyog Kale79df15b2018-04-26 18:38:23 +05301115int sdw_configure_dpn_intr(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001116 int port, bool enable, int mask)
Sanyog Kale79df15b2018-04-26 18:38:23 +05301117{
1118 u32 addr;
1119 int ret;
1120 u8 val = 0;
1121
Pierre-Louis Bossartdd87a722020-09-21 03:32:05 +08001122 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1123 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1124 enable ? "on" : "off");
1125 mask |= SDW_DPN_INT_TEST_FAIL;
1126 }
1127
Sanyog Kale79df15b2018-04-26 18:38:23 +05301128 addr = SDW_DPN_INTMASK(port);
1129
1130 /* Set/Clear port ready interrupt mask */
1131 if (enable) {
1132 val |= mask;
1133 val |= SDW_DPN_INT_PORT_READY;
1134 } else {
1135 val &= ~(mask);
1136 val &= ~SDW_DPN_INT_PORT_READY;
1137 }
1138
1139 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1140 if (ret < 0)
1141 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001142 "SDW_DPN_INTMASK write failed:%d\n", val);
Sanyog Kale79df15b2018-04-26 18:38:23 +05301143
1144 return ret;
1145}
1146
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001147static int sdw_slave_set_frequency(struct sdw_slave *slave)
1148{
1149 u32 mclk_freq = slave->bus->prop.mclk_freq;
1150 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1151 unsigned int scale;
1152 u8 scale_index;
1153 u8 base;
1154 int ret;
1155
1156 /*
1157 * frequency base and scale registers are required for SDCA
1158 * devices. They may also be used for 1.2+/non-SDCA devices,
1159 * but we will need a DisCo property to cover this case
1160 */
1161 if (!slave->id.class_id)
1162 return 0;
1163
1164 if (!mclk_freq) {
1165 dev_err(&slave->dev,
1166 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1167 return -EINVAL;
1168 }
1169
1170 /*
1171 * map base frequency using Table 89 of SoundWire 1.2 spec.
1172 * The order of the tests just follows the specification, this
1173 * is not a selection between possible values or a search for
1174 * the best value but just a mapping. Only one case per platform
1175 * is relevant.
1176 * Some BIOS have inconsistent values for mclk_freq but a
1177 * correct root so we force the mclk_freq to avoid variations.
1178 */
1179 if (!(19200000 % mclk_freq)) {
1180 mclk_freq = 19200000;
1181 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1182 } else if (!(24000000 % mclk_freq)) {
1183 mclk_freq = 24000000;
1184 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1185 } else if (!(24576000 % mclk_freq)) {
1186 mclk_freq = 24576000;
1187 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1188 } else if (!(22579200 % mclk_freq)) {
1189 mclk_freq = 22579200;
1190 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1191 } else if (!(32000000 % mclk_freq)) {
1192 mclk_freq = 32000000;
1193 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1194 } else {
1195 dev_err(&slave->dev,
1196 "Unsupported clock base, mclk %d\n",
1197 mclk_freq);
1198 return -EINVAL;
1199 }
1200
1201 if (mclk_freq % curr_freq) {
1202 dev_err(&slave->dev,
1203 "mclk %d is not multiple of bus curr_freq %d\n",
1204 mclk_freq, curr_freq);
1205 return -EINVAL;
1206 }
1207
1208 scale = mclk_freq / curr_freq;
1209
1210 /*
1211 * map scale to Table 90 of SoundWire 1.2 spec - and check
1212 * that the scale is a power of two and maximum 64
1213 */
1214 scale_index = ilog2(scale);
1215
1216 if (BIT(scale_index) != scale || scale_index > 6) {
1217 dev_err(&slave->dev,
1218 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1219 scale, mclk_freq, curr_freq);
1220 return -EINVAL;
1221 }
1222 scale_index++;
1223
Pierre-Louis Bossart299e9782021-01-22 15:06:27 +08001224 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001225 if (ret < 0) {
1226 dev_err(&slave->dev,
1227 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1228 return ret;
1229 }
1230
1231 /* initialize scale for both banks */
Pierre-Louis Bossart299e9782021-01-22 15:06:27 +08001232 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001233 if (ret < 0) {
1234 dev_err(&slave->dev,
1235 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1236 return ret;
1237 }
Pierre-Louis Bossart299e9782021-01-22 15:06:27 +08001238 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001239 if (ret < 0)
1240 dev_err(&slave->dev,
1241 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1242
1243 dev_dbg(&slave->dev,
1244 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1245 base, scale_index, mclk_freq, curr_freq);
1246
1247 return ret;
1248}
1249
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301250static int sdw_initialize_slave(struct sdw_slave *slave)
1251{
1252 struct sdw_slave_prop *prop = &slave->prop;
1253 int ret;
1254 u8 val;
1255
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001256 ret = sdw_slave_set_frequency(slave);
1257 if (ret < 0)
1258 return ret;
1259
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301260 /*
Pierre-Louis Bossart2acd30b2020-09-08 21:45:15 +08001261 * Set SCP_INT1_MASK register, typically bus clash and
1262 * implementation-defined interrupt mask. The Parity detection
1263 * may not always be correct on startup so its use is
1264 * device-dependent, it might e.g. only be enabled in
1265 * steady-state after a couple of frames.
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301266 */
Pierre-Louis Bossart2acd30b2020-09-08 21:45:15 +08001267 val = slave->prop.scp_int1_mask;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301268
1269 /* Enable SCP interrupts */
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +08001270 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301271 if (ret < 0) {
1272 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001273 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301274 return ret;
1275 }
1276
1277 /* No need to continue if DP0 is not present */
1278 if (!slave->prop.dp0_prop)
1279 return 0;
1280
1281 /* Enable DP0 interrupts */
Pierre-Louis Bossart8acbbfe2019-05-22 14:47:25 -05001282 val = prop->dp0_prop->imp_def_interrupts;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301283 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1284
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +08001285 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
Bard Liao5de79ba2020-02-27 16:09:49 -06001286 if (ret < 0)
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301287 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001288 "SDW_DP0_INTMASK read failed:%d\n", ret);
Bard Liao5de79ba2020-02-27 16:09:49 -06001289 return ret;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301290}
Vinod Koulb0a9c372017-12-14 11:19:40 +05301291
1292static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1293{
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001294 u8 clear, impl_int_mask;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301295 int status, status2, ret, count = 0;
1296
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001297 status = sdw_read_no_pm(slave, SDW_DP0_INT);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301298 if (status < 0) {
1299 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001300 "SDW_DP0_INT read failed:%d\n", status);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301301 return status;
1302 }
1303
1304 do {
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001305 clear = status & ~SDW_DP0_INTERRUPTS;
1306
Vinod Koulb0a9c372017-12-14 11:19:40 +05301307 if (status & SDW_DP0_INT_TEST_FAIL) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001308 dev_err(&slave->dev, "Test fail for port 0\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301309 clear |= SDW_DP0_INT_TEST_FAIL;
1310 }
1311
1312 /*
1313 * Assumption: PORT_READY interrupt will be received only for
1314 * ports implementing Channel Prepare state machine (CP_SM)
1315 */
1316
1317 if (status & SDW_DP0_INT_PORT_READY) {
1318 complete(&slave->port_ready[0]);
1319 clear |= SDW_DP0_INT_PORT_READY;
1320 }
1321
1322 if (status & SDW_DP0_INT_BRA_FAILURE) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001323 dev_err(&slave->dev, "BRA failed\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301324 clear |= SDW_DP0_INT_BRA_FAILURE;
1325 }
1326
1327 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1328 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1329
1330 if (status & impl_int_mask) {
1331 clear |= impl_int_mask;
1332 *slave_status = clear;
1333 }
1334
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001335 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001336 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301337 if (ret < 0) {
1338 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001339 "SDW_DP0_INT write failed:%d\n", ret);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301340 return ret;
1341 }
1342
1343 /* Read DP0 interrupt again */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001344 status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301345 if (status2 < 0) {
1346 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001347 "SDW_DP0_INT read failed:%d\n", status2);
Wei Yongjun80cd8f02018-01-08 22:22:44 +05301348 return status2;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301349 }
Pierre-Louis Bossart6e06a852020-11-24 09:33:14 +08001350 /* filter to limit loop to interrupts identified in the first status read */
Vinod Koulb0a9c372017-12-14 11:19:40 +05301351 status &= status2;
1352
1353 count++;
1354
1355 /* we can get alerts while processing so keep retrying */
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001356 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
Vinod Koulb0a9c372017-12-14 11:19:40 +05301357
1358 if (count == SDW_READ_INTR_CLEAR_RETRY)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001359 dev_warn(slave->bus->dev, "Reached MAX_RETRY on DP0 read\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301360
1361 return ret;
1362}
1363
1364static int sdw_handle_port_interrupt(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001365 int port, u8 *slave_status)
Vinod Koulb0a9c372017-12-14 11:19:40 +05301366{
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001367 u8 clear, impl_int_mask;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301368 int status, status2, ret, count = 0;
1369 u32 addr;
1370
1371 if (port == 0)
1372 return sdw_handle_dp0_interrupt(slave, slave_status);
1373
1374 addr = SDW_DPN_INT(port);
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001375 status = sdw_read_no_pm(slave, addr);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301376 if (status < 0) {
1377 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001378 "SDW_DPN_INT read failed:%d\n", status);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301379
1380 return status;
1381 }
1382
1383 do {
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001384 clear = status & ~SDW_DPN_INTERRUPTS;
1385
Vinod Koulb0a9c372017-12-14 11:19:40 +05301386 if (status & SDW_DPN_INT_TEST_FAIL) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001387 dev_err(&slave->dev, "Test fail for port:%d\n", port);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301388 clear |= SDW_DPN_INT_TEST_FAIL;
1389 }
1390
1391 /*
1392 * Assumption: PORT_READY interrupt will be received only
1393 * for ports implementing CP_SM.
1394 */
1395 if (status & SDW_DPN_INT_PORT_READY) {
1396 complete(&slave->port_ready[port]);
1397 clear |= SDW_DPN_INT_PORT_READY;
1398 }
1399
1400 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1401 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1402
Vinod Koulb0a9c372017-12-14 11:19:40 +05301403 if (status & impl_int_mask) {
1404 clear |= impl_int_mask;
1405 *slave_status = clear;
1406 }
1407
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001408 /* clear the interrupt but don't touch reserved fields */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001409 ret = sdw_write_no_pm(slave, addr, clear);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301410 if (ret < 0) {
1411 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001412 "SDW_DPN_INT write failed:%d\n", ret);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301413 return ret;
1414 }
1415
1416 /* Read DPN interrupt again */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001417 status2 = sdw_read_no_pm(slave, addr);
Wei Yongjun80cd8f02018-01-08 22:22:44 +05301418 if (status2 < 0) {
Vinod Koulb0a9c372017-12-14 11:19:40 +05301419 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001420 "SDW_DPN_INT read failed:%d\n", status2);
Wei Yongjun80cd8f02018-01-08 22:22:44 +05301421 return status2;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301422 }
Pierre-Louis Bossart6e06a852020-11-24 09:33:14 +08001423 /* filter to limit loop to interrupts identified in the first status read */
Vinod Koulb0a9c372017-12-14 11:19:40 +05301424 status &= status2;
1425
1426 count++;
1427
1428 /* we can get alerts while processing so keep retrying */
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001429 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
Vinod Koulb0a9c372017-12-14 11:19:40 +05301430
1431 if (count == SDW_READ_INTR_CLEAR_RETRY)
1432 dev_warn(slave->bus->dev, "Reached MAX_RETRY on port read");
1433
1434 return ret;
1435}
1436
1437static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1438{
1439 struct sdw_slave_intr_status slave_intr;
Bard Liaof1fac632019-08-30 02:11:35 +08001440 u8 clear = 0, bit, port_status[15] = {0};
Vinod Koulb0a9c372017-12-14 11:19:40 +05301441 int port_num, stat, ret, count = 0;
1442 unsigned long port;
Pierre-Louis Bossart7ffaba02020-11-24 09:33:15 +08001443 bool slave_notify;
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001444 u8 sdca_cascade = 0;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301445 u8 buf, buf2[2], _buf, _buf2[2];
Pierre-Louis Bossart4724f122020-09-08 21:45:18 +08001446 bool parity_check;
1447 bool parity_quirk;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301448
1449 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1450
Rander Wangaa792932020-01-14 18:08:42 -06001451 ret = pm_runtime_get_sync(&slave->dev);
1452 if (ret < 0 && ret != -EACCES) {
1453 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
1454 pm_runtime_put_noidle(slave->bus->dev);
1455 return ret;
1456 }
1457
Pierre-Louis Bossartf8d01682020-08-18 06:09:33 +08001458 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001459 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301460 if (ret < 0) {
1461 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001462 "SDW_SCP_INT1 read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001463 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301464 }
Vinod Koul72b16d4a2019-05-02 16:29:26 +05301465 buf = ret;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301466
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001467 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301468 if (ret < 0) {
1469 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001470 "SDW_SCP_INT2/3 read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001471 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301472 }
1473
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001474 if (slave->prop.is_sdca) {
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001475 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001476 if (ret < 0) {
1477 dev_err(slave->bus->dev,
1478 "SDW_DP0_INT read failed:%d\n", ret);
1479 goto io_err;
1480 }
1481 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1482 }
1483
Vinod Koulb0a9c372017-12-14 11:19:40 +05301484 do {
Pierre-Louis Bossart7ffaba02020-11-24 09:33:15 +08001485 slave_notify = false;
1486
Vinod Koulb0a9c372017-12-14 11:19:40 +05301487 /*
1488 * Check parity, bus clash and Slave (impl defined)
1489 * interrupt
1490 */
1491 if (buf & SDW_SCP_INT1_PARITY) {
Pierre-Louis Bossart4724f122020-09-08 21:45:18 +08001492 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1493 parity_quirk = !slave->first_interrupt_done &&
1494 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1495
1496 if (parity_check && !parity_quirk)
Pierre-Louis Bossart310f6dc2020-09-08 21:45:16 +08001497 dev_err(&slave->dev, "Parity error detected\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301498 clear |= SDW_SCP_INT1_PARITY;
1499 }
1500
1501 if (buf & SDW_SCP_INT1_BUS_CLASH) {
Pierre-Louis Bossart310f6dc2020-09-08 21:45:16 +08001502 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1503 dev_err(&slave->dev, "Bus clash detected\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301504 clear |= SDW_SCP_INT1_BUS_CLASH;
1505 }
1506
1507 /*
1508 * When bus clash or parity errors are detected, such errors
1509 * are unlikely to be recoverable errors.
1510 * TODO: In such scenario, reset bus. Make this configurable
1511 * via sysfs property with bus reset being the default.
1512 */
1513
1514 if (buf & SDW_SCP_INT1_IMPL_DEF) {
Pierre-Louis Bossart310f6dc2020-09-08 21:45:16 +08001515 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1516 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1517 slave_notify = true;
1518 }
Vinod Koulb0a9c372017-12-14 11:19:40 +05301519 clear |= SDW_SCP_INT1_IMPL_DEF;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301520 }
1521
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001522 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1523 if (sdca_cascade)
1524 slave_notify = true;
1525
Vinod Koulb0a9c372017-12-14 11:19:40 +05301526 /* Check port 0 - 3 interrupts */
1527 port = buf & SDW_SCP_INT1_PORT0_3;
1528
1529 /* To get port number corresponding to bits, shift it */
Vinod Kould5826a4b2020-09-03 17:14:57 +05301530 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301531 for_each_set_bit(bit, &port, 8) {
1532 sdw_handle_port_interrupt(slave, bit,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001533 &port_status[bit]);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301534 }
1535
1536 /* Check if cascade 2 interrupt is present */
1537 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1538 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1539 for_each_set_bit(bit, &port, 8) {
1540 /* scp2 ports start from 4 */
1541 port_num = bit + 3;
1542 sdw_handle_port_interrupt(slave,
1543 port_num,
1544 &port_status[port_num]);
1545 }
1546 }
1547
1548 /* now check last cascade */
1549 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1550 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1551 for_each_set_bit(bit, &port, 8) {
1552 /* scp3 ports start from 11 */
1553 port_num = bit + 10;
1554 sdw_handle_port_interrupt(slave,
1555 port_num,
1556 &port_status[port_num]);
1557 }
1558 }
1559
1560 /* Update the Slave driver */
Pierre-Louis Bossart09830d52019-05-01 10:57:29 -05001561 if (slave_notify && slave->ops &&
1562 slave->ops->interrupt_callback) {
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001563 slave_intr.sdca_cascade = sdca_cascade;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301564 slave_intr.control_port = clear;
1565 memcpy(slave_intr.port, &port_status,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001566 sizeof(slave_intr.port));
Vinod Koulb0a9c372017-12-14 11:19:40 +05301567
1568 slave->ops->interrupt_callback(slave, &slave_intr);
1569 }
1570
1571 /* Ack interrupt */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001572 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301573 if (ret < 0) {
1574 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001575 "SDW_SCP_INT1 write failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001576 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301577 }
1578
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001579 /* at this point all initial interrupt sources were handled */
1580 slave->first_interrupt_done = true;
1581
Vinod Koulb0a9c372017-12-14 11:19:40 +05301582 /*
1583 * Read status again to ensure no new interrupts arrived
1584 * while servicing interrupts.
1585 */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001586 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301587 if (ret < 0) {
1588 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001589 "SDW_SCP_INT1 read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001590 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301591 }
Vinod Koul72b16d4a2019-05-02 16:29:26 +05301592 _buf = ret;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301593
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001594 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, _buf2);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301595 if (ret < 0) {
1596 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001597 "SDW_SCP_INT2/3 read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001598 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301599 }
1600
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001601 if (slave->prop.is_sdca) {
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001602 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001603 if (ret < 0) {
1604 dev_err(slave->bus->dev,
1605 "SDW_DP0_INT read failed:%d\n", ret);
1606 goto io_err;
1607 }
1608 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1609 }
1610
Pierre-Louis Bossart6e06a852020-11-24 09:33:14 +08001611 /*
1612 * Make sure no interrupts are pending, but filter to limit loop
1613 * to interrupts identified in the first status read
1614 */
Vinod Koulb0a9c372017-12-14 11:19:40 +05301615 buf &= _buf;
1616 buf2[0] &= _buf2[0];
1617 buf2[1] &= _buf2[1];
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001618 stat = buf || buf2[0] || buf2[1] || sdca_cascade;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301619
1620 /*
1621 * Exit loop if Slave is continuously in ALERT state even
1622 * after servicing the interrupt multiple times.
1623 */
1624 count++;
1625
1626 /* we can get alerts while processing so keep retrying */
1627 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1628
1629 if (count == SDW_READ_INTR_CLEAR_RETRY)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001630 dev_warn(slave->bus->dev, "Reached MAX_RETRY on alert read\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301631
Rander Wangaa792932020-01-14 18:08:42 -06001632io_err:
1633 pm_runtime_mark_last_busy(&slave->dev);
1634 pm_runtime_put_autosuspend(&slave->dev);
1635
Vinod Koulb0a9c372017-12-14 11:19:40 +05301636 return ret;
1637}
1638
1639static int sdw_update_slave_status(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001640 enum sdw_slave_status status)
Vinod Koulb0a9c372017-12-14 11:19:40 +05301641{
Pierre-Louis Bossart2140b662020-01-14 18:08:35 -06001642 unsigned long time;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301643
Pierre-Louis Bossart2140b662020-01-14 18:08:35 -06001644 if (!slave->probed) {
1645 /*
1646 * the slave status update is typically handled in an
1647 * interrupt thread, which can race with the driver
1648 * probe, e.g. when a module needs to be loaded.
1649 *
1650 * make sure the probe is complete before updating
1651 * status.
1652 */
1653 time = wait_for_completion_timeout(&slave->probe_complete,
1654 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT));
1655 if (!time) {
1656 dev_err(&slave->dev, "Probe not complete, timed out\n");
1657 return -ETIMEDOUT;
1658 }
1659 }
1660
1661 if (!slave->ops || !slave->ops->update_status)
1662 return 0;
1663
1664 return slave->ops->update_status(slave, status);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301665}
1666
1667/**
1668 * sdw_handle_slave_status() - Handle Slave status
1669 * @bus: SDW bus instance
1670 * @status: Status for all Slave(s)
1671 */
1672int sdw_handle_slave_status(struct sdw_bus *bus,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001673 enum sdw_slave_status status[])
Vinod Koulb0a9c372017-12-14 11:19:40 +05301674{
1675 enum sdw_slave_status prev_status;
1676 struct sdw_slave *slave;
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001677 bool attached_initializing;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301678 int i, ret = 0;
1679
Pierre-Louis Bossart61061902020-01-10 15:57:31 -06001680 /* first check if any Slaves fell off the bus */
1681 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1682 mutex_lock(&bus->bus_lock);
1683 if (test_bit(i, bus->assigned) == false) {
1684 mutex_unlock(&bus->bus_lock);
1685 continue;
1686 }
1687 mutex_unlock(&bus->bus_lock);
1688
1689 slave = sdw_get_slave(bus, i);
1690 if (!slave)
1691 continue;
1692
1693 if (status[i] == SDW_SLAVE_UNATTACHED &&
1694 slave->status != SDW_SLAVE_UNATTACHED)
1695 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1696 }
1697
Vinod Koulb0a9c372017-12-14 11:19:40 +05301698 if (status[0] == SDW_SLAVE_ATTACHED) {
Pierre-Louis Bossart6e0ac6a2019-08-05 19:55:09 -05001699 dev_dbg(bus->dev, "Slave attached, programming device number\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301700 ret = sdw_program_device_num(bus);
1701 if (ret)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001702 dev_err(bus->dev, "Slave attach failed: %d\n", ret);
Pierre-Louis Bossart15ed3ea2019-07-25 18:40:10 -05001703 /*
1704 * programming a device number will have side effects,
1705 * so we deal with other devices at a later time
1706 */
1707 return ret;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301708 }
1709
1710 /* Continue to check other slave statuses */
1711 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1712 mutex_lock(&bus->bus_lock);
1713 if (test_bit(i, bus->assigned) == false) {
1714 mutex_unlock(&bus->bus_lock);
1715 continue;
1716 }
1717 mutex_unlock(&bus->bus_lock);
1718
1719 slave = sdw_get_slave(bus, i);
1720 if (!slave)
1721 continue;
1722
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001723 attached_initializing = false;
1724
Vinod Koulb0a9c372017-12-14 11:19:40 +05301725 switch (status[i]) {
1726 case SDW_SLAVE_UNATTACHED:
1727 if (slave->status == SDW_SLAVE_UNATTACHED)
1728 break;
1729
1730 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1731 break;
1732
1733 case SDW_SLAVE_ALERT:
1734 ret = sdw_handle_slave_alerts(slave);
1735 if (ret)
1736 dev_err(bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001737 "Slave %d alert handling failed: %d\n",
Vinod Koulb0a9c372017-12-14 11:19:40 +05301738 i, ret);
1739 break;
1740
1741 case SDW_SLAVE_ATTACHED:
1742 if (slave->status == SDW_SLAVE_ATTACHED)
1743 break;
1744
1745 prev_status = slave->status;
1746 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1747
1748 if (prev_status == SDW_SLAVE_ALERT)
1749 break;
1750
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001751 attached_initializing = true;
1752
Vinod Koulb0a9c372017-12-14 11:19:40 +05301753 ret = sdw_initialize_slave(slave);
1754 if (ret)
1755 dev_err(bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001756 "Slave %d initialization failed: %d\n",
Vinod Koulb0a9c372017-12-14 11:19:40 +05301757 i, ret);
1758
1759 break;
1760
1761 default:
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001762 dev_err(bus->dev, "Invalid slave %d status:%d\n",
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001763 i, status[i]);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301764 break;
1765 }
1766
1767 ret = sdw_update_slave_status(slave, status[i]);
1768 if (ret)
1769 dev_err(slave->bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001770 "Update Slave status failed:%d\n", ret);
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +08001771 if (attached_initializing) {
1772 dev_dbg(&slave->dev,
1773 "%s: signaling initialization completion for Slave %d\n",
1774 __func__, slave->dev_num);
1775
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001776 complete(&slave->initialization_complete);
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +08001777 }
Vinod Koulb0a9c372017-12-14 11:19:40 +05301778 }
1779
1780 return ret;
1781}
1782EXPORT_SYMBOL(sdw_handle_slave_status);
Pierre-Louis Bossart3ab2ca42020-01-14 18:08:40 -06001783
1784void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1785{
1786 struct sdw_slave *slave;
1787 int i;
1788
1789 /* Check all non-zero devices */
1790 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1791 mutex_lock(&bus->bus_lock);
1792 if (test_bit(i, bus->assigned) == false) {
1793 mutex_unlock(&bus->bus_lock);
1794 continue;
1795 }
1796 mutex_unlock(&bus->bus_lock);
1797
1798 slave = sdw_get_slave(bus, i);
1799 if (!slave)
1800 continue;
1801
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001802 if (slave->status != SDW_SLAVE_UNATTACHED) {
Pierre-Louis Bossart3ab2ca42020-01-14 18:08:40 -06001803 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001804 slave->first_interrupt_done = false;
1805 }
Pierre-Louis Bossart3ab2ca42020-01-14 18:08:40 -06001806
1807 /* keep track of request, used in pm_runtime resume */
1808 slave->unattach_request = request;
1809 }
1810}
1811EXPORT_SYMBOL(sdw_clear_slave_status);