blob: 1b115734a8f6b9f091914e7df67a8184e18453a0 [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);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +080047 if (ret < 0) {
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);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +080053 if (ret < 0) {
Pierre-Louis Bossart7ceaa402020-05-19 01:43:21 +080054 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
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +0800124 if (ret < 0) {
Vinod Koul7c3cd182017-12-14 11:19:34 +0530125 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 Bossart7fae3cf2021-07-14 11:22:06 +0800393 ret = sdw_transfer(slave->bus, &msg);
394 if (slave->is_mockup_device)
395 ret = 0;
396 return ret;
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600397}
398
399static int
Richard Fitzgerald031e6682021-06-16 15:59:01 +0100400sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600401{
402 struct sdw_msg msg;
403 int ret;
404
405 ret = sdw_fill_msg(&msg, slave, addr, count,
Richard Fitzgerald031e6682021-06-16 15:59:01 +0100406 slave->dev_num, SDW_MSG_FLAG_WRITE, (u8 *)val);
Shreyas NCc22c0ae2018-01-09 10:15:46 +0530407 if (ret < 0)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530408 return ret;
409
Pierre-Louis Bossart7fae3cf2021-07-14 11:22:06 +0800410 ret = sdw_transfer(slave->bus, &msg);
411 if (slave->is_mockup_device)
412 ret = 0;
413 return ret;
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600414}
415
Bard Liao167790a2021-01-22 15:06:29 +0800416int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600417{
418 return sdw_nwrite_no_pm(slave, addr, 1, &value);
419}
Bard Liao167790a2021-01-22 15:06:29 +0800420EXPORT_SYMBOL(sdw_write_no_pm);
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600421
Rander Wang02314532020-01-14 18:08:43 -0600422static int
423sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
424{
425 struct sdw_msg msg;
426 u8 buf;
427 int ret;
428
429 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
430 SDW_MSG_FLAG_READ, &buf);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +0800431 if (ret < 0)
Rander Wang02314532020-01-14 18:08:43 -0600432 return ret;
433
434 ret = sdw_transfer(bus, &msg);
435 if (ret < 0)
436 return ret;
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200437
438 return buf;
Rander Wang02314532020-01-14 18:08:43 -0600439}
440
441static int
442sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
443{
444 struct sdw_msg msg;
445 int ret;
446
447 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
448 SDW_MSG_FLAG_WRITE, &value);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +0800449 if (ret < 0)
Rander Wang02314532020-01-14 18:08:43 -0600450 return ret;
451
452 return sdw_transfer(bus, &msg);
453}
454
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800455int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
456{
457 struct sdw_msg msg;
458 u8 buf;
459 int ret;
460
461 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
462 SDW_MSG_FLAG_READ, &buf);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +0800463 if (ret < 0)
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800464 return ret;
465
466 ret = sdw_transfer_unlocked(bus, &msg);
467 if (ret < 0)
468 return ret;
469
470 return buf;
471}
472EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
473
474int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
475{
476 struct sdw_msg msg;
477 int ret;
478
479 ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
480 SDW_MSG_FLAG_WRITE, &value);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +0800481 if (ret < 0)
Pierre-Louis Bossarta350aff2020-09-08 21:45:20 +0800482 return ret;
483
484 return sdw_transfer_unlocked(bus, &msg);
485}
486EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
487
Bard Liao167790a2021-01-22 15:06:29 +0800488int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
Rander Wang02314532020-01-14 18:08:43 -0600489{
490 u8 buf;
491 int ret;
492
493 ret = sdw_nread_no_pm(slave, addr, 1, &buf);
494 if (ret < 0)
495 return ret;
496 else
497 return buf;
498}
Bard Liao167790a2021-01-22 15:06:29 +0800499EXPORT_SYMBOL(sdw_read_no_pm);
Rander Wang02314532020-01-14 18:08:43 -0600500
Pierre-Louis Bossartd38ebaf2021-06-14 13:08:11 -0500501int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +0800502{
503 int tmp;
504
505 tmp = sdw_read_no_pm(slave, addr);
506 if (tmp < 0)
507 return tmp;
508
509 tmp = (tmp & ~mask) | val;
510 return sdw_write_no_pm(slave, addr, tmp);
511}
Pierre-Louis Bossartd38ebaf2021-06-14 13:08:11 -0500512EXPORT_SYMBOL(sdw_update_no_pm);
513
514/* Read-Modify-Write Slave register */
515int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
516{
517 int tmp;
518
519 tmp = sdw_read(slave, addr);
520 if (tmp < 0)
521 return tmp;
522
523 tmp = (tmp & ~mask) | val;
524 return sdw_write(slave, addr, tmp);
525}
526EXPORT_SYMBOL(sdw_update);
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +0800527
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600528/**
529 * sdw_nread() - Read "n" contiguous SDW Slave registers
530 * @slave: SDW Slave
531 * @addr: Register address
532 * @count: length
533 * @val: Buffer for values to be read
534 */
535int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
536{
537 int ret;
538
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +0800539 ret = pm_runtime_get_sync(&slave->dev);
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600540 if (ret < 0 && ret != -EACCES) {
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +0800541 pm_runtime_put_noidle(&slave->dev);
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600542 return ret;
543 }
544
545 ret = sdw_nread_no_pm(slave, addr, count, val);
546
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +0800547 pm_runtime_mark_last_busy(&slave->dev);
548 pm_runtime_put(&slave->dev);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530549
550 return ret;
551}
552EXPORT_SYMBOL(sdw_nread);
553
554/**
555 * sdw_nwrite() - Write "n" contiguous SDW Slave registers
556 * @slave: SDW Slave
557 * @addr: Register address
558 * @count: length
Richard Fitzgerald031e6682021-06-16 15:59:01 +0100559 * @val: Buffer for values to be written
Vinod Koul9d715fa2017-12-14 11:19:37 +0530560 */
Richard Fitzgerald031e6682021-06-16 15:59:01 +0100561int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
Vinod Koul9d715fa2017-12-14 11:19:37 +0530562{
Vinod Koul9d715fa2017-12-14 11:19:37 +0530563 int ret;
564
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +0800565 ret = pm_runtime_get_sync(&slave->dev);
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600566 if (ret < 0 && ret != -EACCES) {
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +0800567 pm_runtime_put_noidle(&slave->dev);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530568 return ret;
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600569 }
Vinod Koul9d715fa2017-12-14 11:19:37 +0530570
Pierre-Louis Bossart60ee9be2020-01-14 18:08:38 -0600571 ret = sdw_nwrite_no_pm(slave, addr, count, val);
572
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +0800573 pm_runtime_mark_last_busy(&slave->dev);
574 pm_runtime_put(&slave->dev);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530575
576 return ret;
577}
578EXPORT_SYMBOL(sdw_nwrite);
579
580/**
581 * sdw_read() - Read a SDW Slave register
582 * @slave: SDW Slave
583 * @addr: Register address
584 */
585int sdw_read(struct sdw_slave *slave, u32 addr)
586{
587 u8 buf;
588 int ret;
589
590 ret = sdw_nread(slave, addr, 1, &buf);
591 if (ret < 0)
592 return ret;
Guennadi Liakhovetskif779ad02020-05-08 02:30:45 +0200593
594 return buf;
Vinod Koul9d715fa2017-12-14 11:19:37 +0530595}
596EXPORT_SYMBOL(sdw_read);
597
598/**
599 * sdw_write() - Write a SDW Slave register
600 * @slave: SDW Slave
601 * @addr: Register address
602 * @value: Register value
603 */
604int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
605{
606 return sdw_nwrite(slave, addr, 1, &value);
Vinod Koul9d715fa2017-12-14 11:19:37 +0530607}
608EXPORT_SYMBOL(sdw_write);
609
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530610/*
611 * SDW alert handling
612 */
613
614/* called with bus_lock held */
615static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
616{
Pierre-Louis Bossart1429cc22021-03-02 17:11:17 +0800617 struct sdw_slave *slave;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530618
619 list_for_each_entry(slave, &bus->slaves, node) {
620 if (slave->dev_num == i)
621 return slave;
622 }
623
624 return NULL;
625}
626
Srinivas Kandagatla01ad4442021-03-30 15:47:17 +0100627int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530628{
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500629 if (slave->id.mfg_id != id.mfg_id ||
Pierre-Louis Bossart09830d52019-05-01 10:57:29 -0500630 slave->id.part_id != id.part_id ||
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500631 slave->id.class_id != id.class_id ||
632 (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
633 slave->id.unique_id != id.unique_id))
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530634 return -ENODEV;
635
636 return 0;
637}
Srinivas Kandagatla01ad4442021-03-30 15:47:17 +0100638EXPORT_SYMBOL(sdw_compare_devid);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530639
640/* called with bus_lock held */
641static int sdw_get_device_num(struct sdw_slave *slave)
642{
643 int bit;
644
645 bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
646 if (bit == SDW_MAX_DEVICES) {
647 bit = -ENODEV;
648 goto err;
649 }
650
651 /*
652 * Do not update dev_num in Slave data structure here,
653 * Update once program dev_num is successful
654 */
655 set_bit(bit, slave->bus->assigned);
656
657err:
658 return bit;
659}
660
661static int sdw_assign_device_num(struct sdw_slave *slave)
662{
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800663 struct sdw_bus *bus = slave->bus;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530664 int ret, dev_num;
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600665 bool new_device = false;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530666
667 /* check first if device number is assigned, if so reuse that */
668 if (!slave->dev_num) {
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600669 if (!slave->dev_num_sticky) {
670 mutex_lock(&slave->bus->bus_lock);
671 dev_num = sdw_get_device_num(slave);
672 mutex_unlock(&slave->bus->bus_lock);
673 if (dev_num < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800674 dev_err(bus->dev, "Get dev_num failed: %d\n",
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600675 dev_num);
676 return dev_num;
677 }
678 slave->dev_num = dev_num;
679 slave->dev_num_sticky = dev_num;
680 new_device = true;
681 } else {
682 slave->dev_num = slave->dev_num_sticky;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530683 }
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600684 }
685
686 if (!new_device)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800687 dev_dbg(bus->dev,
Pierre-Louis Bossartf48f4fd2020-04-20 02:51:17 +0800688 "Slave already registered, reusing dev_num:%d\n",
689 slave->dev_num);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530690
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600691 /* Clear the slave->dev_num to transfer message on device 0 */
692 dev_num = slave->dev_num;
693 slave->dev_num = 0;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530694
Pierre-Louis Bossartd300de4f2020-01-14 18:08:39 -0600695 ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, dev_num);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530696 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800697 dev_err(bus->dev, "Program device_num %d failed: %d\n",
Pierre-Louis Bossart6e0ac6a2019-08-05 19:55:09 -0500698 dev_num, ret);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530699 return ret;
700 }
701
702 /* After xfer of msg, restore dev_num */
Pierre-Louis Bossartfd6a3ac2020-01-13 16:56:37 -0600703 slave->dev_num = slave->dev_num_sticky;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530704
705 return 0;
706}
707
Vinod Koul7c3cd182017-12-14 11:19:34 +0530708void sdw_extract_slave_id(struct sdw_bus *bus,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500709 u64 addr, struct sdw_slave_id *id)
Vinod Koul7c3cd182017-12-14 11:19:34 +0530710{
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500711 dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530712
Pierre-Louis Bossart2c6cff62020-02-25 11:00:41 -0600713 id->sdw_version = SDW_VERSION(addr);
714 id->unique_id = SDW_UNIQUE_ID(addr);
715 id->mfg_id = SDW_MFG_ID(addr);
716 id->part_id = SDW_PART_ID(addr);
717 id->class_id = SDW_CLASS_ID(addr);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530718
719 dev_dbg(bus->dev,
Pierre-Louis Bossartc397efb2021-01-15 13:37:34 +0800720 "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
721 id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
Vinod Koul7c3cd182017-12-14 11:19:34 +0530722}
Srinivas Kandagatla01ad4442021-03-30 15:47:17 +0100723EXPORT_SYMBOL(sdw_extract_slave_id);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530724
725static int sdw_program_device_num(struct sdw_bus *bus)
726{
727 u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
728 struct sdw_slave *slave, *_s;
729 struct sdw_slave_id id;
730 struct sdw_msg msg;
Srinivas Kandagatlaf03690f2021-03-09 10:48:16 +0000731 bool found;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530732 int count = 0, ret;
733 u64 addr;
734
735 /* No Slave, so use raw xfer api */
736 ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500737 SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530738 if (ret < 0)
739 return ret;
740
741 do {
742 ret = sdw_transfer(bus, &msg);
743 if (ret == -ENODATA) { /* end of device id reads */
Pierre-Louis Bossart6e0ac6a2019-08-05 19:55:09 -0500744 dev_dbg(bus->dev, "No more devices to enumerate\n");
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530745 ret = 0;
746 break;
747 }
748 if (ret < 0) {
749 dev_err(bus->dev, "DEVID read fail:%d\n", ret);
750 break;
751 }
752
753 /*
754 * Construct the addr and extract. Cast the higher shift
755 * bits to avoid truncation due to size limit.
756 */
757 addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
Colin Ian King0132af052018-01-08 22:22:42 +0530758 ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
759 ((u64)buf[0] << 40);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530760
761 sdw_extract_slave_id(bus, addr, &id);
762
Srinivas Kandagatlaf03690f2021-03-09 10:48:16 +0000763 found = false;
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530764 /* Now compare with entries */
765 list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
766 if (sdw_compare_devid(slave, id) == 0) {
767 found = true;
768
769 /*
770 * Assign a new dev_num to this Slave and
771 * not mark it present. It will be marked
772 * present after it reports ATTACHED on new
773 * dev_num
774 */
775 ret = sdw_assign_device_num(slave);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +0800776 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800777 dev_err(bus->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500778 "Assign dev_num failed:%d\n",
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530779 ret);
780 return ret;
781 }
782
783 break;
784 }
785 }
786
Pierre-Louis Bossartd7b956b2019-05-01 10:57:30 -0500787 if (!found) {
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530788 /* TODO: Park this device in Group 13 */
Srinivas Kandagatlafcb9d732020-09-24 14:44:29 -0500789
790 /*
791 * add Slave device even if there is no platform
792 * firmware description. There will be no driver probe
793 * but the user/integration will be able to see the
794 * device, enumeration status and device number in sysfs
795 */
796 sdw_slave_add(bus, &id, NULL);
797
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -0500798 dev_err(bus->dev, "Slave Entry not found\n");
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530799 }
800
801 count++;
802
803 /*
804 * Check till error out or retry (count) exhausts.
805 * Device can drop off and rejoin during enumeration
806 * so count till twice the bound.
807 */
808
809 } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
810
811 return ret;
812}
813
814static void sdw_modify_slave_status(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -0500815 enum sdw_slave_status status)
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530816{
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800817 struct sdw_bus *bus = slave->bus;
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600818
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800819 mutex_lock(&bus->bus_lock);
820
821 dev_vdbg(bus->dev,
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600822 "%s: changing status slave %d status %d new status %d\n",
823 __func__, slave->dev_num, slave->status, status);
824
825 if (status == SDW_SLAVE_UNATTACHED) {
826 dev_dbg(&slave->dev,
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +0800827 "%s: initializing enumeration and init completion for Slave %d\n",
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600828 __func__, slave->dev_num);
829
830 init_completion(&slave->enumeration_complete);
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -0600831 init_completion(&slave->initialization_complete);
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600832
833 } else if ((status == SDW_SLAVE_ATTACHED) &&
834 (slave->status == SDW_SLAVE_UNATTACHED)) {
835 dev_dbg(&slave->dev,
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +0800836 "%s: signaling enumeration completion for Slave %d\n",
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -0600837 __func__, slave->dev_num);
838
839 complete(&slave->enumeration_complete);
840 }
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530841 slave->status = status;
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +0800842 mutex_unlock(&bus->bus_lock);
Sanyog Kaled52d7a12017-12-14 11:19:39 +0530843}
844
Rander Wang02314532020-01-14 18:08:43 -0600845static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
846 enum sdw_clk_stop_mode mode,
847 enum sdw_clk_stop_type type)
848{
849 int ret;
850
851 if (slave->ops && slave->ops->clk_stop) {
852 ret = slave->ops->clk_stop(slave, mode, type);
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800853 if (ret < 0)
Rander Wang02314532020-01-14 18:08:43 -0600854 return ret;
Rander Wang02314532020-01-14 18:08:43 -0600855 }
856
857 return 0;
858}
859
860static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
861 enum sdw_clk_stop_mode mode,
862 bool prepare)
863{
864 bool wake_en;
865 u32 val = 0;
866 int ret;
867
868 wake_en = slave->prop.wake_capable;
869
870 if (prepare) {
871 val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
872
873 if (mode == SDW_CLK_STOP_MODE1)
874 val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
875
876 if (wake_en)
877 val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
878 } else {
Pierre-Louis Bossart665cf212021-03-02 17:11:13 +0800879 ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
880 if (ret < 0) {
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800881 if (ret != -ENODATA)
882 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
Pierre-Louis Bossart665cf212021-03-02 17:11:13 +0800883 return ret;
884 }
885 val = ret;
Rander Wang02314532020-01-14 18:08:43 -0600886 val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
887 }
888
889 ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
890
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800891 if (ret < 0 && ret != -ENODATA)
892 dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
Rander Wang02314532020-01-14 18:08:43 -0600893
894 return ret;
895}
896
897static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num)
898{
899 int retry = bus->clk_stop_timeout;
900 int val;
901
902 do {
Pierre-Louis Bossart665cf212021-03-02 17:11:13 +0800903 val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
904 if (val < 0) {
Pierre-Louis Bossart9f9bc7d2021-07-14 09:42:09 +0800905 if (val != -ENODATA)
906 dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
Pierre-Louis Bossart665cf212021-03-02 17:11:13 +0800907 return val;
908 }
909 val &= SDW_SCP_STAT_CLK_STP_NF;
Rander Wang02314532020-01-14 18:08:43 -0600910 if (!val) {
Pierre-Louis Bossart54a6ca42021-05-11 11:00:48 +0800911 dev_dbg(bus->dev, "clock stop prep/de-prep done slave:%d\n",
Pierre-Louis Bossartaf7254b2021-03-02 17:11:15 +0800912 dev_num);
Rander Wang02314532020-01-14 18:08:43 -0600913 return 0;
914 }
915
916 usleep_range(1000, 1500);
917 retry--;
918 } while (retry);
919
Pierre-Louis Bossart54a6ca42021-05-11 11:00:48 +0800920 dev_err(bus->dev, "clock stop prep/de-prep failed slave:%d\n",
Rander Wang02314532020-01-14 18:08:43 -0600921 dev_num);
922
923 return -ETIMEDOUT;
924}
925
926/**
927 * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
928 *
929 * @bus: SDW bus instance
930 *
931 * Query Slave for clock stop mode and prepare for that mode.
932 */
933int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
934{
Rander Wang02314532020-01-14 18:08:43 -0600935 bool simple_clk_stop = true;
936 struct sdw_slave *slave;
937 bool is_slave = false;
938 int ret = 0;
939
940 /*
941 * In order to save on transition time, prepare
942 * each Slave and then wait for all Slave(s) to be
943 * prepared for clock stop.
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800944 * If one of the Slave devices has lost sync and
945 * replies with Command Ignored/-ENODATA, we continue
946 * the loop
Rander Wang02314532020-01-14 18:08:43 -0600947 */
948 list_for_each_entry(slave, &bus->slaves, node) {
949 if (!slave->dev_num)
950 continue;
951
Rander Wang02314532020-01-14 18:08:43 -0600952 if (slave->status != SDW_SLAVE_ATTACHED &&
953 slave->status != SDW_SLAVE_ALERT)
954 continue;
955
Bard Liao929cfee2020-05-31 23:18:06 +0800956 /* Identify if Slave(s) are available on Bus */
957 is_slave = true;
958
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +0800959 ret = sdw_slave_clk_stop_callback(slave,
960 SDW_CLK_STOP_MODE0,
Rander Wang02314532020-01-14 18:08:43 -0600961 SDW_CLK_PRE_PREPARE);
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800962 if (ret < 0 && ret != -ENODATA) {
963 dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
Rander Wang02314532020-01-14 18:08:43 -0600964 return ret;
965 }
966
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +0800967 /* Only prepare a Slave device if needed */
968 if (!slave->prop.simple_clk_stop_capable) {
Rander Wang02314532020-01-14 18:08:43 -0600969 simple_clk_stop = false;
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +0800970
971 ret = sdw_slave_clk_stop_prepare(slave,
972 SDW_CLK_STOP_MODE0,
973 true);
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800974 if (ret < 0 && ret != -ENODATA) {
975 dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +0800976 return ret;
977 }
978 }
Rander Wang02314532020-01-14 18:08:43 -0600979 }
980
Chao Song18de2f722021-01-26 16:54:39 +0800981 /* Skip remaining clock stop preparation if no Slave is attached */
982 if (!is_slave)
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800983 return 0;
Chao Song18de2f722021-01-26 16:54:39 +0800984
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +0800985 /*
986 * Don't wait for all Slaves to be ready if they follow the simple
987 * state machine
988 */
Chao Song18de2f722021-01-26 16:54:39 +0800989 if (!simple_clk_stop) {
Rander Wang02314532020-01-14 18:08:43 -0600990 ret = sdw_bus_wait_for_clk_prep_deprep(bus,
991 SDW_BROADCAST_DEV_NUM);
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +0800992 /*
993 * if there are no Slave devices present and the reply is
994 * Command_Ignored/-ENODATA, we don't need to continue with the
995 * flow and can just return here. The error code is not modified
996 * and its handling left as an exercise for the caller.
997 */
Rander Wang02314532020-01-14 18:08:43 -0600998 if (ret < 0)
999 return ret;
1000 }
1001
1002 /* Inform slaves that prep is done */
1003 list_for_each_entry(slave, &bus->slaves, node) {
1004 if (!slave->dev_num)
1005 continue;
1006
1007 if (slave->status != SDW_SLAVE_ATTACHED &&
1008 slave->status != SDW_SLAVE_ALERT)
1009 continue;
1010
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +08001011 ret = sdw_slave_clk_stop_callback(slave,
1012 SDW_CLK_STOP_MODE0,
1013 SDW_CLK_POST_PREPARE);
Rander Wang02314532020-01-14 18:08:43 -06001014
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001015 if (ret < 0 && ret != -ENODATA) {
1016 dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
1017 return ret;
Rander Wang02314532020-01-14 18:08:43 -06001018 }
1019 }
1020
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001021 return 0;
Rander Wang02314532020-01-14 18:08:43 -06001022}
1023EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
1024
1025/**
1026 * sdw_bus_clk_stop: stop bus clock
1027 *
1028 * @bus: SDW bus instance
1029 *
1030 * After preparing the Slaves for clock stop, stop the clock by broadcasting
1031 * write to SCP_CTRL register.
1032 */
1033int sdw_bus_clk_stop(struct sdw_bus *bus)
1034{
1035 int ret;
1036
1037 /*
1038 * broadcast clock stop now, attached Slaves will ACK this,
1039 * unattached will ignore
1040 */
1041 ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
1042 SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
1043 if (ret < 0) {
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001044 if (ret != -ENODATA)
1045 dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
Rander Wang02314532020-01-14 18:08:43 -06001046 return ret;
1047 }
1048
1049 return 0;
1050}
1051EXPORT_SYMBOL(sdw_bus_clk_stop);
1052
1053/**
1054 * sdw_bus_exit_clk_stop: Exit clock stop mode
1055 *
1056 * @bus: SDW bus instance
1057 *
1058 * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
1059 * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
1060 * back.
1061 */
1062int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
1063{
Rander Wang02314532020-01-14 18:08:43 -06001064 bool simple_clk_stop = true;
1065 struct sdw_slave *slave;
1066 bool is_slave = false;
1067 int ret;
1068
1069 /*
1070 * In order to save on transition time, de-prepare
1071 * each Slave and then wait for all Slave(s) to be
1072 * de-prepared after clock resume.
1073 */
1074 list_for_each_entry(slave, &bus->slaves, node) {
1075 if (!slave->dev_num)
1076 continue;
1077
Rander Wang02314532020-01-14 18:08:43 -06001078 if (slave->status != SDW_SLAVE_ATTACHED &&
1079 slave->status != SDW_SLAVE_ALERT)
1080 continue;
1081
Bard Liao929cfee2020-05-31 23:18:06 +08001082 /* Identify if Slave(s) are available on Bus */
1083 is_slave = true;
1084
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +08001085 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
Rander Wang02314532020-01-14 18:08:43 -06001086 SDW_CLK_PRE_DEPREPARE);
1087 if (ret < 0)
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001088 dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
Rander Wang02314532020-01-14 18:08:43 -06001089
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +08001090 /* Only de-prepare a Slave device if needed */
1091 if (!slave->prop.simple_clk_stop_capable) {
1092 simple_clk_stop = false;
Rander Wang02314532020-01-14 18:08:43 -06001093
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +08001094 ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
1095 false);
1096
1097 if (ret < 0)
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001098 dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +08001099 }
Rander Wang02314532020-01-14 18:08:43 -06001100 }
1101
Chao Song18de2f722021-01-26 16:54:39 +08001102 /* Skip remaining clock stop de-preparation if no Slave is attached */
Bard Liao929cfee2020-05-31 23:18:06 +08001103 if (!is_slave)
1104 return 0;
1105
Pierre-Louis Bossart345e9f52021-05-11 11:00:45 +08001106 /*
1107 * Don't wait for all Slaves to be ready if they follow the simple
1108 * state machine
1109 */
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001110 if (!simple_clk_stop) {
1111 ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM);
1112 if (ret < 0)
1113 dev_warn(&slave->dev, "clock stop deprepare wait failed:%d\n", ret);
1114 }
Chao Song18de2f722021-01-26 16:54:39 +08001115
Rander Wang02314532020-01-14 18:08:43 -06001116 list_for_each_entry(slave, &bus->slaves, node) {
1117 if (!slave->dev_num)
1118 continue;
1119
1120 if (slave->status != SDW_SLAVE_ATTACHED &&
1121 slave->status != SDW_SLAVE_ALERT)
1122 continue;
1123
Pierre-Louis Bossartb50bb8b2021-05-11 11:00:47 +08001124 ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
1125 SDW_CLK_POST_DEPREPARE);
1126 if (ret < 0)
1127 dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
Rander Wang02314532020-01-14 18:08:43 -06001128 }
1129
1130 return 0;
1131}
1132EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
1133
Sanyog Kale79df15b2018-04-26 18:38:23 +05301134int sdw_configure_dpn_intr(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001135 int port, bool enable, int mask)
Sanyog Kale79df15b2018-04-26 18:38:23 +05301136{
1137 u32 addr;
1138 int ret;
1139 u8 val = 0;
1140
Pierre-Louis Bossartdd87a722020-09-21 03:32:05 +08001141 if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
1142 dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
1143 enable ? "on" : "off");
1144 mask |= SDW_DPN_INT_TEST_FAIL;
1145 }
1146
Sanyog Kale79df15b2018-04-26 18:38:23 +05301147 addr = SDW_DPN_INTMASK(port);
1148
1149 /* Set/Clear port ready interrupt mask */
1150 if (enable) {
1151 val |= mask;
1152 val |= SDW_DPN_INT_PORT_READY;
1153 } else {
1154 val &= ~(mask);
1155 val &= ~SDW_DPN_INT_PORT_READY;
1156 }
1157
1158 ret = sdw_update(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
1159 if (ret < 0)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001160 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001161 "SDW_DPN_INTMASK write failed:%d\n", val);
Sanyog Kale79df15b2018-04-26 18:38:23 +05301162
1163 return ret;
1164}
1165
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001166static int sdw_slave_set_frequency(struct sdw_slave *slave)
1167{
1168 u32 mclk_freq = slave->bus->prop.mclk_freq;
1169 u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
1170 unsigned int scale;
1171 u8 scale_index;
1172 u8 base;
1173 int ret;
1174
1175 /*
1176 * frequency base and scale registers are required for SDCA
1177 * devices. They may also be used for 1.2+/non-SDCA devices,
1178 * but we will need a DisCo property to cover this case
1179 */
1180 if (!slave->id.class_id)
1181 return 0;
1182
1183 if (!mclk_freq) {
1184 dev_err(&slave->dev,
1185 "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
1186 return -EINVAL;
1187 }
1188
1189 /*
1190 * map base frequency using Table 89 of SoundWire 1.2 spec.
1191 * The order of the tests just follows the specification, this
1192 * is not a selection between possible values or a search for
1193 * the best value but just a mapping. Only one case per platform
1194 * is relevant.
1195 * Some BIOS have inconsistent values for mclk_freq but a
1196 * correct root so we force the mclk_freq to avoid variations.
1197 */
1198 if (!(19200000 % mclk_freq)) {
1199 mclk_freq = 19200000;
1200 base = SDW_SCP_BASE_CLOCK_19200000_HZ;
1201 } else if (!(24000000 % mclk_freq)) {
1202 mclk_freq = 24000000;
1203 base = SDW_SCP_BASE_CLOCK_24000000_HZ;
1204 } else if (!(24576000 % mclk_freq)) {
1205 mclk_freq = 24576000;
1206 base = SDW_SCP_BASE_CLOCK_24576000_HZ;
1207 } else if (!(22579200 % mclk_freq)) {
1208 mclk_freq = 22579200;
1209 base = SDW_SCP_BASE_CLOCK_22579200_HZ;
1210 } else if (!(32000000 % mclk_freq)) {
1211 mclk_freq = 32000000;
1212 base = SDW_SCP_BASE_CLOCK_32000000_HZ;
1213 } else {
1214 dev_err(&slave->dev,
1215 "Unsupported clock base, mclk %d\n",
1216 mclk_freq);
1217 return -EINVAL;
1218 }
1219
1220 if (mclk_freq % curr_freq) {
1221 dev_err(&slave->dev,
1222 "mclk %d is not multiple of bus curr_freq %d\n",
1223 mclk_freq, curr_freq);
1224 return -EINVAL;
1225 }
1226
1227 scale = mclk_freq / curr_freq;
1228
1229 /*
1230 * map scale to Table 90 of SoundWire 1.2 spec - and check
1231 * that the scale is a power of two and maximum 64
1232 */
1233 scale_index = ilog2(scale);
1234
1235 if (BIT(scale_index) != scale || scale_index > 6) {
1236 dev_err(&slave->dev,
1237 "No match found for scale %d, bus mclk %d curr_freq %d\n",
1238 scale, mclk_freq, curr_freq);
1239 return -EINVAL;
1240 }
1241 scale_index++;
1242
Pierre-Louis Bossart299e9782021-01-22 15:06:27 +08001243 ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001244 if (ret < 0) {
1245 dev_err(&slave->dev,
1246 "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
1247 return ret;
1248 }
1249
1250 /* initialize scale for both banks */
Pierre-Louis Bossart299e9782021-01-22 15:06:27 +08001251 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001252 if (ret < 0) {
1253 dev_err(&slave->dev,
1254 "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
1255 return ret;
1256 }
Pierre-Louis Bossart299e9782021-01-22 15:06:27 +08001257 ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001258 if (ret < 0)
1259 dev_err(&slave->dev,
1260 "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
1261
1262 dev_dbg(&slave->dev,
1263 "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
1264 base, scale_index, mclk_freq, curr_freq);
1265
1266 return ret;
1267}
1268
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301269static int sdw_initialize_slave(struct sdw_slave *slave)
1270{
1271 struct sdw_slave_prop *prop = &slave->prop;
Bard Liao6b8caa62021-03-02 16:27:19 +08001272 int status;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301273 int ret;
1274 u8 val;
1275
Pierre-Louis Bossart29d158f2020-06-09 04:54:36 +08001276 ret = sdw_slave_set_frequency(slave);
1277 if (ret < 0)
1278 return ret;
1279
Bard Liao6b8caa62021-03-02 16:27:19 +08001280 if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
1281 /* Clear bus clash interrupt before enabling interrupt mask */
1282 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1283 if (status < 0) {
1284 dev_err(&slave->dev,
1285 "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
1286 return status;
1287 }
1288 if (status & SDW_SCP_INT1_BUS_CLASH) {
1289 dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
1290 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
1291 if (ret < 0) {
1292 dev_err(&slave->dev,
1293 "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
1294 return ret;
1295 }
1296 }
1297 }
1298 if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
1299 !(slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
1300 /* Clear parity interrupt before enabling interrupt mask */
1301 status = sdw_read_no_pm(slave, SDW_SCP_INT1);
1302 if (status < 0) {
1303 dev_err(&slave->dev,
1304 "SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
1305 return status;
1306 }
1307 if (status & SDW_SCP_INT1_PARITY) {
1308 dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
1309 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
1310 if (ret < 0) {
1311 dev_err(&slave->dev,
1312 "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
1313 return ret;
1314 }
1315 }
1316 }
1317
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301318 /*
Pierre-Louis Bossart2acd30b2020-09-08 21:45:15 +08001319 * Set SCP_INT1_MASK register, typically bus clash and
1320 * implementation-defined interrupt mask. The Parity detection
1321 * may not always be correct on startup so its use is
1322 * device-dependent, it might e.g. only be enabled in
1323 * steady-state after a couple of frames.
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301324 */
Pierre-Louis Bossart2acd30b2020-09-08 21:45:15 +08001325 val = slave->prop.scp_int1_mask;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301326
1327 /* Enable SCP interrupts */
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +08001328 ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301329 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001330 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001331 "SDW_SCP_INTMASK1 write failed:%d\n", ret);
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301332 return ret;
1333 }
1334
1335 /* No need to continue if DP0 is not present */
1336 if (!slave->prop.dp0_prop)
1337 return 0;
1338
1339 /* Enable DP0 interrupts */
Pierre-Louis Bossart8acbbfe2019-05-22 14:47:25 -05001340 val = prop->dp0_prop->imp_def_interrupts;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301341 val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
1342
Pierre-Louis Bossartb04c9752021-01-22 15:06:26 +08001343 ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
Bard Liao5de79ba2020-02-27 16:09:49 -06001344 if (ret < 0)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001345 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001346 "SDW_DP0_INTMASK read failed:%d\n", ret);
Bard Liao5de79ba2020-02-27 16:09:49 -06001347 return ret;
Sanyog Kaled52d7a12017-12-14 11:19:39 +05301348}
Vinod Koulb0a9c372017-12-14 11:19:40 +05301349
1350static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
1351{
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001352 u8 clear, impl_int_mask;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301353 int status, status2, ret, count = 0;
1354
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001355 status = sdw_read_no_pm(slave, SDW_DP0_INT);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301356 if (status < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001357 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001358 "SDW_DP0_INT read failed:%d\n", status);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301359 return status;
1360 }
1361
1362 do {
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001363 clear = status & ~SDW_DP0_INTERRUPTS;
1364
Vinod Koulb0a9c372017-12-14 11:19:40 +05301365 if (status & SDW_DP0_INT_TEST_FAIL) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001366 dev_err(&slave->dev, "Test fail for port 0\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301367 clear |= SDW_DP0_INT_TEST_FAIL;
1368 }
1369
1370 /*
1371 * Assumption: PORT_READY interrupt will be received only for
1372 * ports implementing Channel Prepare state machine (CP_SM)
1373 */
1374
1375 if (status & SDW_DP0_INT_PORT_READY) {
1376 complete(&slave->port_ready[0]);
1377 clear |= SDW_DP0_INT_PORT_READY;
1378 }
1379
1380 if (status & SDW_DP0_INT_BRA_FAILURE) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001381 dev_err(&slave->dev, "BRA failed\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301382 clear |= SDW_DP0_INT_BRA_FAILURE;
1383 }
1384
1385 impl_int_mask = SDW_DP0_INT_IMPDEF1 |
1386 SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
1387
1388 if (status & impl_int_mask) {
1389 clear |= impl_int_mask;
1390 *slave_status = clear;
1391 }
1392
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001393 /* clear the interrupts but don't touch reserved and SDCA_CASCADE fields */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001394 ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301395 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001396 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001397 "SDW_DP0_INT write failed:%d\n", ret);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301398 return ret;
1399 }
1400
1401 /* Read DP0 interrupt again */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001402 status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301403 if (status2 < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001404 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001405 "SDW_DP0_INT read failed:%d\n", status2);
Wei Yongjun80cd8f02018-01-08 22:22:44 +05301406 return status2;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301407 }
Pierre-Louis Bossart6e06a852020-11-24 09:33:14 +08001408 /* filter to limit loop to interrupts identified in the first status read */
Vinod Koulb0a9c372017-12-14 11:19:40 +05301409 status &= status2;
1410
1411 count++;
1412
1413 /* we can get alerts while processing so keep retrying */
Pierre-Louis Bossartb35991d2020-11-24 09:33:17 +08001414 } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
Vinod Koulb0a9c372017-12-14 11:19:40 +05301415
1416 if (count == SDW_READ_INTR_CLEAR_RETRY)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001417 dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301418
1419 return ret;
1420}
1421
1422static int sdw_handle_port_interrupt(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001423 int port, u8 *slave_status)
Vinod Koulb0a9c372017-12-14 11:19:40 +05301424{
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001425 u8 clear, impl_int_mask;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301426 int status, status2, ret, count = 0;
1427 u32 addr;
1428
1429 if (port == 0)
1430 return sdw_handle_dp0_interrupt(slave, slave_status);
1431
1432 addr = SDW_DPN_INT(port);
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001433 status = sdw_read_no_pm(slave, addr);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301434 if (status < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001435 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001436 "SDW_DPN_INT read failed:%d\n", status);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301437
1438 return status;
1439 }
1440
1441 do {
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001442 clear = status & ~SDW_DPN_INTERRUPTS;
1443
Vinod Koulb0a9c372017-12-14 11:19:40 +05301444 if (status & SDW_DPN_INT_TEST_FAIL) {
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001445 dev_err(&slave->dev, "Test fail for port:%d\n", port);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301446 clear |= SDW_DPN_INT_TEST_FAIL;
1447 }
1448
1449 /*
1450 * Assumption: PORT_READY interrupt will be received only
1451 * for ports implementing CP_SM.
1452 */
1453 if (status & SDW_DPN_INT_PORT_READY) {
1454 complete(&slave->port_ready[port]);
1455 clear |= SDW_DPN_INT_PORT_READY;
1456 }
1457
1458 impl_int_mask = SDW_DPN_INT_IMPDEF1 |
1459 SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
1460
Vinod Koulb0a9c372017-12-14 11:19:40 +05301461 if (status & impl_int_mask) {
1462 clear |= impl_int_mask;
1463 *slave_status = clear;
1464 }
1465
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001466 /* clear the interrupt but don't touch reserved fields */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001467 ret = sdw_write_no_pm(slave, addr, clear);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301468 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001469 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001470 "SDW_DPN_INT write failed:%d\n", ret);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301471 return ret;
1472 }
1473
1474 /* Read DPN interrupt again */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001475 status2 = sdw_read_no_pm(slave, addr);
Wei Yongjun80cd8f02018-01-08 22:22:44 +05301476 if (status2 < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001477 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001478 "SDW_DPN_INT read failed:%d\n", status2);
Wei Yongjun80cd8f02018-01-08 22:22:44 +05301479 return status2;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301480 }
Pierre-Louis Bossart6e06a852020-11-24 09:33:14 +08001481 /* filter to limit loop to interrupts identified in the first status read */
Vinod Koulb0a9c372017-12-14 11:19:40 +05301482 status &= status2;
1483
1484 count++;
1485
1486 /* we can get alerts while processing so keep retrying */
Pierre-Louis Bossart47b85202020-11-24 09:33:18 +08001487 } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
Vinod Koulb0a9c372017-12-14 11:19:40 +05301488
1489 if (count == SDW_READ_INTR_CLEAR_RETRY)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001490 dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301491
1492 return ret;
1493}
1494
1495static int sdw_handle_slave_alerts(struct sdw_slave *slave)
1496{
1497 struct sdw_slave_intr_status slave_intr;
Bard Liaof1fac632019-08-30 02:11:35 +08001498 u8 clear = 0, bit, port_status[15] = {0};
Vinod Koulb0a9c372017-12-14 11:19:40 +05301499 int port_num, stat, ret, count = 0;
1500 unsigned long port;
Pierre-Louis Bossart7ffaba02020-11-24 09:33:15 +08001501 bool slave_notify;
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001502 u8 sdca_cascade = 0;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301503 u8 buf, buf2[2], _buf, _buf2[2];
Pierre-Louis Bossart4724f122020-09-08 21:45:18 +08001504 bool parity_check;
1505 bool parity_quirk;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301506
1507 sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
1508
Rander Wangaa792932020-01-14 18:08:42 -06001509 ret = pm_runtime_get_sync(&slave->dev);
1510 if (ret < 0 && ret != -EACCES) {
1511 dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
Pierre-Louis Bossart973794e2021-01-22 15:06:33 +08001512 pm_runtime_put_noidle(&slave->dev);
Rander Wangaa792932020-01-14 18:08:42 -06001513 return ret;
1514 }
1515
Pierre-Louis Bossartf8d01682020-08-18 06:09:33 +08001516 /* Read Intstat 1, Intstat 2 and Intstat 3 registers */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001517 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301518 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001519 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001520 "SDW_SCP_INT1 read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001521 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301522 }
Vinod Koul72b16d4a2019-05-02 16:29:26 +05301523 buf = ret;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301524
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001525 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301526 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001527 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001528 "SDW_SCP_INT2/3 read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001529 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301530 }
1531
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001532 if (slave->prop.is_sdca) {
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001533 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001534 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001535 dev_err(&slave->dev,
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001536 "SDW_DP0_INT read failed:%d\n", ret);
1537 goto io_err;
1538 }
1539 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1540 }
1541
Vinod Koulb0a9c372017-12-14 11:19:40 +05301542 do {
Pierre-Louis Bossart7ffaba02020-11-24 09:33:15 +08001543 slave_notify = false;
1544
Vinod Koulb0a9c372017-12-14 11:19:40 +05301545 /*
1546 * Check parity, bus clash and Slave (impl defined)
1547 * interrupt
1548 */
1549 if (buf & SDW_SCP_INT1_PARITY) {
Pierre-Louis Bossart4724f122020-09-08 21:45:18 +08001550 parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
1551 parity_quirk = !slave->first_interrupt_done &&
1552 (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
1553
1554 if (parity_check && !parity_quirk)
Pierre-Louis Bossart310f6dc2020-09-08 21:45:16 +08001555 dev_err(&slave->dev, "Parity error detected\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301556 clear |= SDW_SCP_INT1_PARITY;
1557 }
1558
1559 if (buf & SDW_SCP_INT1_BUS_CLASH) {
Pierre-Louis Bossart310f6dc2020-09-08 21:45:16 +08001560 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
1561 dev_err(&slave->dev, "Bus clash detected\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301562 clear |= SDW_SCP_INT1_BUS_CLASH;
1563 }
1564
1565 /*
1566 * When bus clash or parity errors are detected, such errors
1567 * are unlikely to be recoverable errors.
1568 * TODO: In such scenario, reset bus. Make this configurable
1569 * via sysfs property with bus reset being the default.
1570 */
1571
1572 if (buf & SDW_SCP_INT1_IMPL_DEF) {
Pierre-Louis Bossart310f6dc2020-09-08 21:45:16 +08001573 if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
1574 dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
1575 slave_notify = true;
1576 }
Vinod Koulb0a9c372017-12-14 11:19:40 +05301577 clear |= SDW_SCP_INT1_IMPL_DEF;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301578 }
1579
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001580 /* the SDCA interrupts are cleared in the codec driver .interrupt_callback() */
1581 if (sdca_cascade)
1582 slave_notify = true;
1583
Vinod Koulb0a9c372017-12-14 11:19:40 +05301584 /* Check port 0 - 3 interrupts */
1585 port = buf & SDW_SCP_INT1_PORT0_3;
1586
1587 /* To get port number corresponding to bits, shift it */
Vinod Kould5826a4b2020-09-03 17:14:57 +05301588 port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301589 for_each_set_bit(bit, &port, 8) {
1590 sdw_handle_port_interrupt(slave, bit,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001591 &port_status[bit]);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301592 }
1593
1594 /* Check if cascade 2 interrupt is present */
1595 if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
1596 port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
1597 for_each_set_bit(bit, &port, 8) {
1598 /* scp2 ports start from 4 */
1599 port_num = bit + 3;
1600 sdw_handle_port_interrupt(slave,
1601 port_num,
1602 &port_status[port_num]);
1603 }
1604 }
1605
1606 /* now check last cascade */
1607 if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
1608 port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
1609 for_each_set_bit(bit, &port, 8) {
1610 /* scp3 ports start from 11 */
1611 port_num = bit + 10;
1612 sdw_handle_port_interrupt(slave,
1613 port_num,
1614 &port_status[port_num]);
1615 }
1616 }
1617
1618 /* Update the Slave driver */
Pierre-Louis Bossart09830d52019-05-01 10:57:29 -05001619 if (slave_notify && slave->ops &&
1620 slave->ops->interrupt_callback) {
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001621 slave_intr.sdca_cascade = sdca_cascade;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301622 slave_intr.control_port = clear;
1623 memcpy(slave_intr.port, &port_status,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001624 sizeof(slave_intr.port));
Vinod Koulb0a9c372017-12-14 11:19:40 +05301625
1626 slave->ops->interrupt_callback(slave, &slave_intr);
1627 }
1628
1629 /* Ack interrupt */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001630 ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301631 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001632 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001633 "SDW_SCP_INT1 write failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001634 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301635 }
1636
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001637 /* at this point all initial interrupt sources were handled */
1638 slave->first_interrupt_done = true;
1639
Vinod Koulb0a9c372017-12-14 11:19:40 +05301640 /*
1641 * Read status again to ensure no new interrupts arrived
1642 * while servicing interrupts.
1643 */
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001644 ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301645 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001646 dev_err(&slave->dev,
Pierre-Louis Bossartb5001272021-03-02 17:11:16 +08001647 "SDW_SCP_INT1 recheck read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001648 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301649 }
Vinod Koul72b16d4a2019-05-02 16:29:26 +05301650 _buf = ret;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301651
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001652 ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, _buf2);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301653 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001654 dev_err(&slave->dev,
Pierre-Louis Bossartb5001272021-03-02 17:11:16 +08001655 "SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
Rander Wangaa792932020-01-14 18:08:42 -06001656 goto io_err;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301657 }
1658
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001659 if (slave->prop.is_sdca) {
Pierre-Louis Bossartc30b63e2021-01-22 15:06:28 +08001660 ret = sdw_read_no_pm(slave, SDW_DP0_INT);
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001661 if (ret < 0) {
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001662 dev_err(&slave->dev,
Pierre-Louis Bossartb5001272021-03-02 17:11:16 +08001663 "SDW_DP0_INT recheck read failed:%d\n", ret);
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001664 goto io_err;
1665 }
1666 sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
1667 }
1668
Pierre-Louis Bossart6e06a852020-11-24 09:33:14 +08001669 /*
1670 * Make sure no interrupts are pending, but filter to limit loop
1671 * to interrupts identified in the first status read
1672 */
Vinod Koulb0a9c372017-12-14 11:19:40 +05301673 buf &= _buf;
1674 buf2[0] &= _buf2[0];
1675 buf2[1] &= _buf2[1];
Pierre-Louis Bossartb7cab9b2020-11-04 23:23:58 +08001676 stat = buf || buf2[0] || buf2[1] || sdca_cascade;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301677
1678 /*
1679 * Exit loop if Slave is continuously in ALERT state even
1680 * after servicing the interrupt multiple times.
1681 */
1682 count++;
1683
1684 /* we can get alerts while processing so keep retrying */
1685 } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
1686
1687 if (count == SDW_READ_INTR_CLEAR_RETRY)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001688 dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301689
Rander Wangaa792932020-01-14 18:08:42 -06001690io_err:
1691 pm_runtime_mark_last_busy(&slave->dev);
1692 pm_runtime_put_autosuspend(&slave->dev);
1693
Vinod Koulb0a9c372017-12-14 11:19:40 +05301694 return ret;
1695}
1696
1697static int sdw_update_slave_status(struct sdw_slave *slave,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001698 enum sdw_slave_status status)
Vinod Koulb0a9c372017-12-14 11:19:40 +05301699{
Pierre-Louis Bossart2140b662020-01-14 18:08:35 -06001700 unsigned long time;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301701
Pierre-Louis Bossart2140b662020-01-14 18:08:35 -06001702 if (!slave->probed) {
1703 /*
1704 * the slave status update is typically handled in an
1705 * interrupt thread, which can race with the driver
1706 * probe, e.g. when a module needs to be loaded.
1707 *
1708 * make sure the probe is complete before updating
1709 * status.
1710 */
1711 time = wait_for_completion_timeout(&slave->probe_complete,
1712 msecs_to_jiffies(DEFAULT_PROBE_TIMEOUT));
1713 if (!time) {
1714 dev_err(&slave->dev, "Probe not complete, timed out\n");
1715 return -ETIMEDOUT;
1716 }
1717 }
1718
1719 if (!slave->ops || !slave->ops->update_status)
1720 return 0;
1721
1722 return slave->ops->update_status(slave, status);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301723}
1724
1725/**
1726 * sdw_handle_slave_status() - Handle Slave status
1727 * @bus: SDW bus instance
1728 * @status: Status for all Slave(s)
1729 */
1730int sdw_handle_slave_status(struct sdw_bus *bus,
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001731 enum sdw_slave_status status[])
Vinod Koulb0a9c372017-12-14 11:19:40 +05301732{
1733 enum sdw_slave_status prev_status;
1734 struct sdw_slave *slave;
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001735 bool attached_initializing;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301736 int i, ret = 0;
1737
Pierre-Louis Bossart61061902020-01-10 15:57:31 -06001738 /* first check if any Slaves fell off the bus */
1739 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1740 mutex_lock(&bus->bus_lock);
1741 if (test_bit(i, bus->assigned) == false) {
1742 mutex_unlock(&bus->bus_lock);
1743 continue;
1744 }
1745 mutex_unlock(&bus->bus_lock);
1746
1747 slave = sdw_get_slave(bus, i);
1748 if (!slave)
1749 continue;
1750
1751 if (status[i] == SDW_SLAVE_UNATTACHED &&
1752 slave->status != SDW_SLAVE_UNATTACHED)
1753 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1754 }
1755
Vinod Koulb0a9c372017-12-14 11:19:40 +05301756 if (status[0] == SDW_SLAVE_ATTACHED) {
Pierre-Louis Bossart6e0ac6a2019-08-05 19:55:09 -05001757 dev_dbg(bus->dev, "Slave attached, programming device number\n");
Vinod Koulb0a9c372017-12-14 11:19:40 +05301758 ret = sdw_program_device_num(bus);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +08001759 if (ret < 0)
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001760 dev_err(bus->dev, "Slave attach failed: %d\n", ret);
Pierre-Louis Bossart15ed3ea2019-07-25 18:40:10 -05001761 /*
1762 * programming a device number will have side effects,
1763 * so we deal with other devices at a later time
1764 */
1765 return ret;
Vinod Koulb0a9c372017-12-14 11:19:40 +05301766 }
1767
1768 /* Continue to check other slave statuses */
1769 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1770 mutex_lock(&bus->bus_lock);
1771 if (test_bit(i, bus->assigned) == false) {
1772 mutex_unlock(&bus->bus_lock);
1773 continue;
1774 }
1775 mutex_unlock(&bus->bus_lock);
1776
1777 slave = sdw_get_slave(bus, i);
1778 if (!slave)
1779 continue;
1780
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001781 attached_initializing = false;
1782
Vinod Koulb0a9c372017-12-14 11:19:40 +05301783 switch (status[i]) {
1784 case SDW_SLAVE_UNATTACHED:
1785 if (slave->status == SDW_SLAVE_UNATTACHED)
1786 break;
1787
1788 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
1789 break;
1790
1791 case SDW_SLAVE_ALERT:
1792 ret = sdw_handle_slave_alerts(slave);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +08001793 if (ret < 0)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001794 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001795 "Slave %d alert handling failed: %d\n",
Vinod Koulb0a9c372017-12-14 11:19:40 +05301796 i, ret);
1797 break;
1798
1799 case SDW_SLAVE_ATTACHED:
1800 if (slave->status == SDW_SLAVE_ATTACHED)
1801 break;
1802
1803 prev_status = slave->status;
1804 sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
1805
1806 if (prev_status == SDW_SLAVE_ALERT)
1807 break;
1808
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001809 attached_initializing = true;
1810
Vinod Koulb0a9c372017-12-14 11:19:40 +05301811 ret = sdw_initialize_slave(slave);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +08001812 if (ret < 0)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001813 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001814 "Slave %d initialization failed: %d\n",
Vinod Koulb0a9c372017-12-14 11:19:40 +05301815 i, ret);
1816
1817 break;
1818
1819 default:
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001820 dev_err(&slave->dev, "Invalid slave %d status:%d\n",
Pierre-Louis Bossart73ede042019-05-01 10:57:27 -05001821 i, status[i]);
Vinod Koulb0a9c372017-12-14 11:19:40 +05301822 break;
1823 }
1824
1825 ret = sdw_update_slave_status(slave, status[i]);
Pierre-Louis Bossarta5759f12021-03-02 17:11:14 +08001826 if (ret < 0)
Pierre-Louis Bossart6d7a1ff2021-01-22 15:06:34 +08001827 dev_err(&slave->dev,
Pierre-Louis Bossart17ed5be2019-05-01 10:57:45 -05001828 "Update Slave status failed:%d\n", ret);
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +08001829 if (attached_initializing) {
1830 dev_dbg(&slave->dev,
1831 "%s: signaling initialization completion for Slave %d\n",
1832 __func__, slave->dev_num);
1833
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -06001834 complete(&slave->initialization_complete);
Pierre-Louis Bossartf1b69022021-01-26 16:54:02 +08001835 }
Vinod Koulb0a9c372017-12-14 11:19:40 +05301836 }
1837
1838 return ret;
1839}
1840EXPORT_SYMBOL(sdw_handle_slave_status);
Pierre-Louis Bossart3ab2ca42020-01-14 18:08:40 -06001841
1842void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
1843{
1844 struct sdw_slave *slave;
1845 int i;
1846
1847 /* Check all non-zero devices */
1848 for (i = 1; i <= SDW_MAX_DEVICES; i++) {
1849 mutex_lock(&bus->bus_lock);
1850 if (test_bit(i, bus->assigned) == false) {
1851 mutex_unlock(&bus->bus_lock);
1852 continue;
1853 }
1854 mutex_unlock(&bus->bus_lock);
1855
1856 slave = sdw_get_slave(bus, i);
1857 if (!slave)
1858 continue;
1859
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001860 if (slave->status != SDW_SLAVE_UNATTACHED) {
Pierre-Louis Bossart3ab2ca42020-01-14 18:08:40 -06001861 sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001862 slave->first_interrupt_done = false;
Bard Liao899a7502021-07-14 11:36:09 +08001863 sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +08001864 }
Pierre-Louis Bossart3ab2ca42020-01-14 18:08:40 -06001865
1866 /* keep track of request, used in pm_runtime resume */
1867 slave->unattach_request = request;
1868 }
1869}
1870EXPORT_SYMBOL(sdw_clear_slave_status);