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