| 1 | |
| 2 | |
| 3 |
|
| 4 | #include <linux/acpi.h>
|
| 5 | #include <linux/delay.h>
|
| 6 | #include <linux/pm_runtime.h>
|
| 7 | #include <linux/soundwire/sdw_registers.h>
|
| 8 | #include <linux/soundwire/sdw.h>
|
| 9 | #include <linux/soundwire/sdw_type.h>
|
| 10 | #include <linux/string_choices.h>
|
| 11 | #include "bus.h"
|
| 12 | #include "irq.h"
|
| 13 | #include "sysfs_local.h"
|
| 14 |
|
| 15 | static DEFINE_IDA(sdw_bus_ida);
|
| 16 |
|
| 17 | static int sdw_get_id(struct sdw_bus *bus)
|
| 18 | {
|
| 19 | int rc = ida_alloc(&sdw_bus_ida, GFP_KERNEL);
|
| 20 |
|
| 21 | if (rc < 0)
|
| 22 | return rc;
|
| 23 |
|
| 24 | bus->id = rc;
|
| 25 |
|
| 26 | if (bus->controller_id == -1)
|
| 27 | bus->controller_id = rc;
|
| 28 |
|
| 29 | return 0;
|
| 30 | }
|
| 31 |
|
| 32 | |
| 33 | * sdw_bus_master_add() - add a bus Master instance
|
| 34 | * @bus: bus instance
|
| 35 | * @parent: parent device
|
| 36 | * @fwnode: firmware node handle
|
| 37 | *
|
| 38 | * Initializes the bus instance, read properties and create child
|
| 39 | * devices.
|
| 40 |
|
| 41 | int sdw_bus_master_add(struct sdw_bus *bus, struct device *parent,
|
| 42 | struct fwnode_handle *fwnode)
|
| 43 | {
|
| 44 | struct sdw_master_prop *prop = NULL;
|
| 45 | int ret;
|
| 46 |
|
| 47 | if (!parent) {
|
| 48 | pr_err("SoundWire parent device is not set\n");
|
| 49 | return -ENODEV;
|
| 50 | }
|
| 51 |
|
| 52 | ret = sdw_get_id(bus);
|
| 53 | if (ret < 0) {
|
| 54 | dev_err(parent, "Failed to get bus id\n");
|
| 55 | return ret;
|
| 56 | }
|
| 57 |
|
| 58 | ida_init(&bus->slave_ida);
|
| 59 |
|
| 60 | ret = sdw_master_device_add(bus, parent, fwnode);
|
| 61 | if (ret < 0) {
|
| 62 | dev_err(parent, "Failed to add master device at link %d\n",
|
| 63 | bus->link_id);
|
| 64 | return ret;
|
| 65 | }
|
| 66 |
|
| 67 | if (!bus->ops) {
|
| 68 | dev_err(bus->dev, "SoundWire Bus ops are not set\n");
|
| 69 | return -EINVAL;
|
| 70 | }
|
| 71 |
|
| 72 | if (!bus->compute_params) {
|
| 73 | dev_err(bus->dev,
|
| 74 | "Bandwidth allocation not configured, compute_params no set\n");
|
| 75 | return -EINVAL;
|
| 76 | }
|
| 77 |
|
| 78 | |
| 79 | * Give each bus_lock and msg_lock a unique key so that lockdep won't
|
| 80 | * trigger a deadlock warning when the locks of several buses are
|
| 81 | * grabbed during configuration of a multi-bus stream.
|
| 82 |
|
| 83 | lockdep_register_key(&bus->msg_lock_key);
|
| 84 | __mutex_init(&bus->msg_lock, "msg_lock", &bus->msg_lock_key);
|
| 85 |
|
| 86 | lockdep_register_key(&bus->bus_lock_key);
|
| 87 | __mutex_init(&bus->bus_lock, "bus_lock", &bus->bus_lock_key);
|
| 88 |
|
| 89 | INIT_LIST_HEAD(&bus->slaves);
|
| 90 | INIT_LIST_HEAD(&bus->m_rt_list);
|
| 91 |
|
| 92 | |
| 93 | * Initialize multi_link flag
|
| 94 |
|
| 95 | bus->multi_link = false;
|
| 96 | if (bus->ops->read_prop) {
|
| 97 | ret = bus->ops->read_prop(bus);
|
| 98 | if (ret < 0) {
|
| 99 | dev_err(bus->dev,
|
| 100 | "Bus read properties failed:%d\n", ret);
|
| 101 | return ret;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | sdw_bus_debugfs_init(bus);
|
| 106 |
|
| 107 | |
| 108 | * Device numbers in SoundWire are 0 through 15. Enumeration device
|
| 109 | * number (0), Broadcast device number (15), Group numbers (12 and
|
| 110 | * 13) and Master device number (14) are not used for assignment so
|
| 111 | * mask these and other higher bits.
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 | *bus->assigned = ~GENMASK(SDW_BROADCAST_DEV_NUM, SDW_ENUM_DEV_NUM);
|
| 116 |
|
| 117 |
|
| 118 | set_bit(SDW_ENUM_DEV_NUM, bus->assigned);
|
| 119 | set_bit(SDW_BROADCAST_DEV_NUM, bus->assigned);
|
| 120 |
|
| 121 |
|
| 122 | set_bit(SDW_GROUP12_DEV_NUM, bus->assigned);
|
| 123 | set_bit(SDW_GROUP13_DEV_NUM, bus->assigned);
|
| 124 | set_bit(SDW_MASTER_DEV_NUM, bus->assigned);
|
| 125 |
|
| 126 | ret = sdw_irq_create(bus, fwnode);
|
| 127 | if (ret)
|
| 128 | return ret;
|
| 129 |
|
| 130 | |
| 131 | * SDW is an enumerable bus, but devices can be powered off. So,
|
| 132 | * they won't be able to report as present.
|
| 133 | *
|
| 134 | * Create Slave devices based on Slaves described in
|
| 135 | * the respective firmware (ACPI/DT)
|
| 136 |
|
| 137 | if (IS_ENABLED(CONFIG_ACPI) && ACPI_HANDLE(bus->dev))
|
| 138 | ret = sdw_acpi_find_slaves(bus);
|
| 139 | else if (IS_ENABLED(CONFIG_OF) && bus->dev->of_node)
|
| 140 | ret = sdw_of_find_slaves(bus);
|
| 141 | else
|
| 142 | ret = -ENOTSUPP;
|
| 143 |
|
| 144 | if (ret < 0) {
|
| 145 | dev_err(bus->dev, "Finding slaves failed:%d\n", ret);
|
| 146 | sdw_irq_delete(bus);
|
| 147 | return ret;
|
| 148 | }
|
| 149 |
|
| 150 | |
| 151 | * Initialize clock values based on Master properties. The max
|
| 152 | * frequency is read from max_clk_freq property. Current assumption
|
| 153 | * is that the bus will start at highest clock frequency when
|
| 154 | * powered on.
|
| 155 | *
|
| 156 | * Default active bank will be 0 as out of reset the Slaves have
|
| 157 | * to start with bank 0 (Table 40 of Spec)
|
| 158 |
|
| 159 | prop = &bus->prop;
|
| 160 | bus->params.max_dr_freq = prop->max_clk_freq * SDW_DOUBLE_RATE_FACTOR;
|
| 161 | bus->params.curr_dr_freq = bus->params.max_dr_freq;
|
| 162 | bus->params.curr_bank = SDW_BANK0;
|
| 163 | bus->params.next_bank = SDW_BANK1;
|
| 164 |
|
| 165 | return 0;
|
| 166 | }
|
| 167 | EXPORT_SYMBOL(sdw_bus_master_add);
|
| 168 |
|
| 169 | static int sdw_delete_slave(struct device *dev, void *data)
|
| 170 | {
|
| 171 | struct sdw_slave *slave = dev_to_sdw_dev(dev);
|
| 172 | struct sdw_bus *bus = slave->bus;
|
| 173 |
|
| 174 | pm_runtime_disable(dev);
|
| 175 |
|
| 176 | sdw_slave_debugfs_exit(slave);
|
| 177 |
|
| 178 | mutex_lock(&bus->bus_lock);
|
| 179 |
|
| 180 | if (slave->dev_num) {
|
| 181 | clear_bit(slave->dev_num, bus->assigned);
|
| 182 | if (bus->ops && bus->ops->put_device_num)
|
| 183 | bus->ops->put_device_num(bus, slave);
|
| 184 | }
|
| 185 | list_del_init(&slave->node);
|
| 186 | mutex_unlock(&bus->bus_lock);
|
| 187 |
|
| 188 | device_unregister(dev);
|
| 189 | return 0;
|
| 190 | }
|
| 191 |
|
| 192 | |
| 193 | * sdw_bus_master_delete() - delete the bus master instance
|
| 194 | * @bus: bus to be deleted
|
| 195 | *
|
| 196 | * Remove the instance, delete the child devices.
|
| 197 |
|
| 198 | void sdw_bus_master_delete(struct sdw_bus *bus)
|
| 199 | {
|
| 200 | device_for_each_child(bus->dev, NULL, sdw_delete_slave);
|
| 201 |
|
| 202 | sdw_irq_delete(bus);
|
| 203 |
|
| 204 | sdw_master_device_del(bus);
|
| 205 |
|
| 206 | sdw_bus_debugfs_exit(bus);
|
| 207 | lockdep_unregister_key(&bus->bus_lock_key);
|
| 208 | lockdep_unregister_key(&bus->msg_lock_key);
|
| 209 | ida_free(&sdw_bus_ida, bus->id);
|
| 210 | }
|
| 211 | EXPORT_SYMBOL(sdw_bus_master_delete);
|
| 212 |
|
| 213 | |
| 214 | * SDW IO Calls
|
| 215 |
|
| 216 |
|
| 217 | static inline int find_response_code(enum sdw_command_response resp)
|
| 218 | {
|
| 219 | switch (resp) {
|
| 220 | case SDW_CMD_OK:
|
| 221 | return 0;
|
| 222 |
|
| 223 | case SDW_CMD_IGNORED:
|
| 224 | return -ENODATA;
|
| 225 |
|
| 226 | case SDW_CMD_TIMEOUT:
|
| 227 | return -ETIMEDOUT;
|
| 228 |
|
| 229 | default:
|
| 230 | return -EIO;
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | static inline int do_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
|
| 235 | {
|
| 236 | int retry = bus->prop.err_threshold;
|
| 237 | enum sdw_command_response resp;
|
| 238 | int ret = 0, i;
|
| 239 |
|
| 240 | for (i = 0; i <= retry; i++) {
|
| 241 | resp = bus->ops->xfer_msg(bus, msg);
|
| 242 | ret = find_response_code(resp);
|
| 243 |
|
| 244 |
|
| 245 | if (ret == 0 || ret == -ENODATA)
|
| 246 | return ret;
|
| 247 | }
|
| 248 |
|
| 249 | return ret;
|
| 250 | }
|
| 251 |
|
| 252 | static inline int do_transfer_defer(struct sdw_bus *bus,
|
| 253 | struct sdw_msg *msg)
|
| 254 | {
|
| 255 | struct sdw_defer *defer = &bus->defer_msg;
|
| 256 | int retry = bus->prop.err_threshold;
|
| 257 | enum sdw_command_response resp;
|
| 258 | int ret = 0, i;
|
| 259 |
|
| 260 | defer->msg = msg;
|
| 261 | defer->length = msg->len;
|
| 262 | init_completion(&defer->complete);
|
| 263 |
|
| 264 | for (i = 0; i <= retry; i++) {
|
| 265 | resp = bus->ops->xfer_msg_defer(bus);
|
| 266 | ret = find_response_code(resp);
|
| 267 |
|
| 268 | if (ret == 0 || ret == -ENODATA)
|
| 269 | return ret;
|
| 270 | }
|
| 271 |
|
| 272 | return ret;
|
| 273 | }
|
| 274 |
|
| 275 | static int sdw_transfer_unlocked(struct sdw_bus *bus, struct sdw_msg *msg)
|
| 276 | {
|
| 277 | int ret;
|
| 278 |
|
| 279 | ret = do_transfer(bus, msg);
|
| 280 | if (ret != 0 && ret != -ENODATA)
|
| 281 | dev_err(bus->dev, "trf on Slave %d failed:%d %s addr %x count %d\n",
|
| 282 | msg->dev_num, ret,
|
| 283 | str_write_read(msg->flags & SDW_MSG_FLAG_WRITE),
|
| 284 | msg->addr, msg->len);
|
| 285 |
|
| 286 | return ret;
|
| 287 | }
|
| 288 |
|
| 289 | |
| 290 | * sdw_transfer() - Synchronous transfer message to a SDW Slave device
|
| 291 | * @bus: SDW bus
|
| 292 | * @msg: SDW message to be xfered
|
| 293 |
|
| 294 | int sdw_transfer(struct sdw_bus *bus, struct sdw_msg *msg)
|
| 295 | {
|
| 296 | int ret;
|
| 297 |
|
| 298 | mutex_lock(&bus->msg_lock);
|
| 299 |
|
| 300 | ret = sdw_transfer_unlocked(bus, msg);
|
| 301 |
|
| 302 | mutex_unlock(&bus->msg_lock);
|
| 303 |
|
| 304 | return ret;
|
| 305 | }
|
| 306 |
|
| 307 | |
| 308 | * sdw_show_ping_status() - Direct report of PING status, to be used by Peripheral drivers
|
| 309 | * @bus: SDW bus
|
| 310 | * @sync_delay: Delay before reading status
|
| 311 |
|
| 312 | void sdw_show_ping_status(struct sdw_bus *bus, bool sync_delay)
|
| 313 | {
|
| 314 | u32 status;
|
| 315 |
|
| 316 | if (!bus->ops->read_ping_status)
|
| 317 | return;
|
| 318 |
|
| 319 | |
| 320 | * wait for peripheral to sync if desired. 10-15ms should be more than
|
| 321 | * enough in most cases.
|
| 322 |
|
| 323 | if (sync_delay)
|
| 324 | usleep_range(10000, 15000);
|
| 325 |
|
| 326 | mutex_lock(&bus->msg_lock);
|
| 327 |
|
| 328 | status = bus->ops->read_ping_status(bus);
|
| 329 |
|
| 330 | mutex_unlock(&bus->msg_lock);
|
| 331 |
|
| 332 | if (!status)
|
| 333 | dev_warn(bus->dev, "%s: no peripherals attached\n", __func__);
|
| 334 | else
|
| 335 | dev_dbg(bus->dev, "PING status: %#x\n", status);
|
| 336 | }
|
| 337 | EXPORT_SYMBOL(sdw_show_ping_status);
|
| 338 |
|
| 339 | |
| 340 | * sdw_transfer_defer() - Asynchronously transfer message to a SDW Slave device
|
| 341 | * @bus: SDW bus
|
| 342 | * @msg: SDW message to be xfered
|
| 343 | *
|
| 344 | * Caller needs to hold the msg_lock lock while calling this
|
| 345 |
|
| 346 | int sdw_transfer_defer(struct sdw_bus *bus, struct sdw_msg *msg)
|
| 347 | {
|
| 348 | int ret;
|
| 349 |
|
| 350 | if (!bus->ops->xfer_msg_defer)
|
| 351 | return -ENOTSUPP;
|
| 352 |
|
| 353 | ret = do_transfer_defer(bus, msg);
|
| 354 | if (ret != 0 && ret != -ENODATA)
|
| 355 | dev_err(bus->dev, "Defer trf on Slave %d failed:%d\n",
|
| 356 | msg->dev_num, ret);
|
| 357 |
|
| 358 | return ret;
|
| 359 | }
|
| 360 |
|
| 361 | int sdw_fill_msg(struct sdw_msg *msg, struct sdw_slave *slave,
|
| 362 | u32 addr, size_t count, u16 dev_num, u8 flags, u8 *buf)
|
| 363 | {
|
| 364 | memset(msg, 0, sizeof(*msg));
|
| 365 | msg->addr = addr;
|
| 366 | msg->len = count;
|
| 367 | msg->dev_num = dev_num;
|
| 368 | msg->flags = flags;
|
| 369 | msg->buf = buf;
|
| 370 |
|
| 371 | if (addr < SDW_REG_NO_PAGE)
|
| 372 | return 0;
|
| 373 |
|
| 374 | if (addr >= SDW_REG_MAX) {
|
| 375 | pr_err("SDW: Invalid address %x passed\n", addr);
|
| 376 | return -EINVAL;
|
| 377 | }
|
| 378 |
|
| 379 | if (addr < SDW_REG_OPTIONAL_PAGE) {
|
| 380 | if (slave && !slave->prop.paging_support)
|
| 381 | return 0;
|
| 382 |
|
| 383 | }
|
| 384 |
|
| 385 |
|
| 386 | if (dev_num == SDW_ENUM_DEV_NUM || dev_num == SDW_BROADCAST_DEV_NUM) {
|
| 387 | pr_err("SDW: Invalid device for paging :%d\n", dev_num);
|
| 388 | return -EINVAL;
|
| 389 | }
|
| 390 |
|
| 391 | if (!slave) {
|
| 392 | pr_err("SDW: No slave for paging addr\n");
|
| 393 | return -EINVAL;
|
| 394 | }
|
| 395 |
|
| 396 | if (!slave->prop.paging_support) {
|
| 397 | dev_err(&slave->dev,
|
| 398 | "address %x needs paging but no support\n", addr);
|
| 399 | return -EINVAL;
|
| 400 | }
|
| 401 |
|
| 402 | msg->addr_page1 = FIELD_GET(SDW_SCP_ADDRPAGE1_MASK, addr);
|
| 403 | msg->addr_page2 = FIELD_GET(SDW_SCP_ADDRPAGE2_MASK, addr);
|
| 404 | msg->addr |= BIT(15);
|
| 405 | msg->page = true;
|
| 406 |
|
| 407 | return 0;
|
| 408 | }
|
| 409 |
|
| 410 | |
| 411 | * Read/Write IO functions.
|
| 412 |
|
| 413 |
|
| 414 | static int sdw_ntransfer_no_pm(struct sdw_slave *slave, u32 addr, u8 flags,
|
| 415 | size_t count, u8 *val)
|
| 416 | {
|
| 417 | struct sdw_msg msg;
|
| 418 | size_t size;
|
| 419 | int ret;
|
| 420 |
|
| 421 | while (count) {
|
| 422 | |
| 423 | size = min_t(size_t, count, (SDW_REGADDR + 1) - (addr & SDW_REGADDR));
|
| 424 |
|
| 425 | ret = sdw_fill_msg(&msg, slave, addr, size, slave->dev_num, flags, val);
|
| 426 | if (ret < 0)
|
| 427 | return ret;
|
| 428 |
|
| 429 | ret = sdw_transfer(slave->bus, &msg);
|
| 430 | if (ret < 0 && !slave->is_mockup_device)
|
| 431 | return ret;
|
| 432 |
|
| 433 | addr += size;
|
| 434 | val += size;
|
| 435 | count -= size;
|
| 436 | }
|
| 437 |
|
| 438 | return 0;
|
| 439 | }
|
| 440 |
|
| 441 | |
| 442 | * sdw_nread_no_pm() - Read "n" contiguous SDW Slave registers with no PM
|
| 443 | * @slave: SDW Slave
|
| 444 | * @addr: Register address
|
| 445 | * @count: length
|
| 446 | * @val: Buffer for values to be read
|
| 447 | *
|
| 448 | * Note that if the message crosses a page boundary each page will be
|
| 449 | * transferred under a separate invocation of the msg_lock.
|
| 450 |
|
| 451 | int sdw_nread_no_pm(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
|
| 452 | {
|
| 453 | return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_READ, count, val);
|
| 454 | }
|
| 455 | EXPORT_SYMBOL(sdw_nread_no_pm);
|
| 456 |
|
| 457 | |
| 458 | * sdw_nwrite_no_pm() - Write "n" contiguous SDW Slave registers with no PM
|
| 459 | * @slave: SDW Slave
|
| 460 | * @addr: Register address
|
| 461 | * @count: length
|
| 462 | * @val: Buffer for values to be written
|
| 463 | *
|
| 464 | * Note that if the message crosses a page boundary each page will be
|
| 465 | * transferred under a separate invocation of the msg_lock.
|
| 466 |
|
| 467 | int sdw_nwrite_no_pm(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
|
| 468 | {
|
| 469 | return sdw_ntransfer_no_pm(slave, addr, SDW_MSG_FLAG_WRITE, count, (u8 *)val);
|
| 470 | }
|
| 471 | EXPORT_SYMBOL(sdw_nwrite_no_pm);
|
| 472 |
|
| 473 | |
| 474 | * sdw_write_no_pm() - Write a SDW Slave register with no PM
|
| 475 | * @slave: SDW Slave
|
| 476 | * @addr: Register address
|
| 477 | * @value: Register value
|
| 478 |
|
| 479 | int sdw_write_no_pm(struct sdw_slave *slave, u32 addr, u8 value)
|
| 480 | {
|
| 481 | return sdw_nwrite_no_pm(slave, addr, 1, &value);
|
| 482 | }
|
| 483 | EXPORT_SYMBOL(sdw_write_no_pm);
|
| 484 |
|
| 485 | static int
|
| 486 | sdw_bread_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr)
|
| 487 | {
|
| 488 | struct sdw_msg msg;
|
| 489 | u8 buf;
|
| 490 | int ret;
|
| 491 |
|
| 492 | ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
| 493 | SDW_MSG_FLAG_READ, &buf);
|
| 494 | if (ret < 0)
|
| 495 | return ret;
|
| 496 |
|
| 497 | ret = sdw_transfer(bus, &msg);
|
| 498 | if (ret < 0)
|
| 499 | return ret;
|
| 500 |
|
| 501 | return buf;
|
| 502 | }
|
| 503 |
|
| 504 | static int
|
| 505 | sdw_bwrite_no_pm(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
|
| 506 | {
|
| 507 | struct sdw_msg msg;
|
| 508 | int ret;
|
| 509 |
|
| 510 | ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
| 511 | SDW_MSG_FLAG_WRITE, &value);
|
| 512 | if (ret < 0)
|
| 513 | return ret;
|
| 514 |
|
| 515 | return sdw_transfer(bus, &msg);
|
| 516 | }
|
| 517 |
|
| 518 | int sdw_bread_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr)
|
| 519 | {
|
| 520 | struct sdw_msg msg;
|
| 521 | u8 buf;
|
| 522 | int ret;
|
| 523 |
|
| 524 | ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
| 525 | SDW_MSG_FLAG_READ, &buf);
|
| 526 | if (ret < 0)
|
| 527 | return ret;
|
| 528 |
|
| 529 | ret = sdw_transfer_unlocked(bus, &msg);
|
| 530 | if (ret < 0)
|
| 531 | return ret;
|
| 532 |
|
| 533 | return buf;
|
| 534 | }
|
| 535 | EXPORT_SYMBOL(sdw_bread_no_pm_unlocked);
|
| 536 |
|
| 537 | int sdw_bwrite_no_pm_unlocked(struct sdw_bus *bus, u16 dev_num, u32 addr, u8 value)
|
| 538 | {
|
| 539 | struct sdw_msg msg;
|
| 540 | int ret;
|
| 541 |
|
| 542 | ret = sdw_fill_msg(&msg, NULL, addr, 1, dev_num,
|
| 543 | SDW_MSG_FLAG_WRITE, &value);
|
| 544 | if (ret < 0)
|
| 545 | return ret;
|
| 546 |
|
| 547 | return sdw_transfer_unlocked(bus, &msg);
|
| 548 | }
|
| 549 | EXPORT_SYMBOL(sdw_bwrite_no_pm_unlocked);
|
| 550 |
|
| 551 | |
| 552 | * sdw_read_no_pm() - Read a SDW Slave register with no PM
|
| 553 | * @slave: SDW Slave
|
| 554 | * @addr: Register address
|
| 555 |
|
| 556 | int sdw_read_no_pm(struct sdw_slave *slave, u32 addr)
|
| 557 | {
|
| 558 | u8 buf;
|
| 559 | int ret;
|
| 560 |
|
| 561 | ret = sdw_nread_no_pm(slave, addr, 1, &buf);
|
| 562 | if (ret < 0)
|
| 563 | return ret;
|
| 564 | else
|
| 565 | return buf;
|
| 566 | }
|
| 567 | EXPORT_SYMBOL(sdw_read_no_pm);
|
| 568 |
|
| 569 | int sdw_update_no_pm(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
|
| 570 | {
|
| 571 | int tmp;
|
| 572 |
|
| 573 | tmp = sdw_read_no_pm(slave, addr);
|
| 574 | if (tmp < 0)
|
| 575 | return tmp;
|
| 576 |
|
| 577 | tmp = (tmp & ~mask) | val;
|
| 578 | return sdw_write_no_pm(slave, addr, tmp);
|
| 579 | }
|
| 580 | EXPORT_SYMBOL(sdw_update_no_pm);
|
| 581 |
|
| 582 |
|
| 583 | int sdw_update(struct sdw_slave *slave, u32 addr, u8 mask, u8 val)
|
| 584 | {
|
| 585 | int tmp;
|
| 586 |
|
| 587 | tmp = sdw_read(slave, addr);
|
| 588 | if (tmp < 0)
|
| 589 | return tmp;
|
| 590 |
|
| 591 | tmp = (tmp & ~mask) | val;
|
| 592 | return sdw_write(slave, addr, tmp);
|
| 593 | }
|
| 594 | EXPORT_SYMBOL(sdw_update);
|
| 595 |
|
| 596 | |
| 597 | * sdw_nread() - Read "n" contiguous SDW Slave registers
|
| 598 | * @slave: SDW Slave
|
| 599 | * @addr: Register address
|
| 600 | * @count: length
|
| 601 | * @val: Buffer for values to be read
|
| 602 | *
|
| 603 | * This version of the function will take a PM reference to the slave
|
| 604 | * device.
|
| 605 | * Note that if the message crosses a page boundary each page will be
|
| 606 | * transferred under a separate invocation of the msg_lock.
|
| 607 |
|
| 608 | int sdw_nread(struct sdw_slave *slave, u32 addr, size_t count, u8 *val)
|
| 609 | {
|
| 610 | int ret;
|
| 611 |
|
| 612 | ret = pm_runtime_get_sync(&slave->dev);
|
| 613 | if (ret < 0 && ret != -EACCES) {
|
| 614 | pm_runtime_put_noidle(&slave->dev);
|
| 615 | return ret;
|
| 616 | }
|
| 617 |
|
| 618 | ret = sdw_nread_no_pm(slave, addr, count, val);
|
| 619 |
|
| 620 | pm_runtime_mark_last_busy(&slave->dev);
|
| 621 | pm_runtime_put(&slave->dev);
|
| 622 |
|
| 623 | return ret;
|
| 624 | }
|
| 625 | EXPORT_SYMBOL(sdw_nread);
|
| 626 |
|
| 627 | |
| 628 | * sdw_nwrite() - Write "n" contiguous SDW Slave registers
|
| 629 | * @slave: SDW Slave
|
| 630 | * @addr: Register address
|
| 631 | * @count: length
|
| 632 | * @val: Buffer for values to be written
|
| 633 | *
|
| 634 | * This version of the function will take a PM reference to the slave
|
| 635 | * device.
|
| 636 | * Note that if the message crosses a page boundary each page will be
|
| 637 | * transferred under a separate invocation of the msg_lock.
|
| 638 |
|
| 639 | int sdw_nwrite(struct sdw_slave *slave, u32 addr, size_t count, const u8 *val)
|
| 640 | {
|
| 641 | int ret;
|
| 642 |
|
| 643 | ret = pm_runtime_get_sync(&slave->dev);
|
| 644 | if (ret < 0 && ret != -EACCES) {
|
| 645 | pm_runtime_put_noidle(&slave->dev);
|
| 646 | return ret;
|
| 647 | }
|
| 648 |
|
| 649 | ret = sdw_nwrite_no_pm(slave, addr, count, val);
|
| 650 |
|
| 651 | pm_runtime_mark_last_busy(&slave->dev);
|
| 652 | pm_runtime_put(&slave->dev);
|
| 653 |
|
| 654 | return ret;
|
| 655 | }
|
| 656 | EXPORT_SYMBOL(sdw_nwrite);
|
| 657 |
|
| 658 | |
| 659 | * sdw_read() - Read a SDW Slave register
|
| 660 | * @slave: SDW Slave
|
| 661 | * @addr: Register address
|
| 662 | *
|
| 663 | * This version of the function will take a PM reference to the slave
|
| 664 | * device.
|
| 665 |
|
| 666 | int sdw_read(struct sdw_slave *slave, u32 addr)
|
| 667 | {
|
| 668 | u8 buf;
|
| 669 | int ret;
|
| 670 |
|
| 671 | ret = sdw_nread(slave, addr, 1, &buf);
|
| 672 | if (ret < 0)
|
| 673 | return ret;
|
| 674 |
|
| 675 | return buf;
|
| 676 | }
|
| 677 | EXPORT_SYMBOL(sdw_read);
|
| 678 |
|
| 679 | |
| 680 | * sdw_write() - Write a SDW Slave register
|
| 681 | * @slave: SDW Slave
|
| 682 | * @addr: Register address
|
| 683 | * @value: Register value
|
| 684 | *
|
| 685 | * This version of the function will take a PM reference to the slave
|
| 686 | * device.
|
| 687 |
|
| 688 | int sdw_write(struct sdw_slave *slave, u32 addr, u8 value)
|
| 689 | {
|
| 690 | return sdw_nwrite(slave, addr, 1, &value);
|
| 691 | }
|
| 692 | EXPORT_SYMBOL(sdw_write);
|
| 693 |
|
| 694 | |
| 695 | * SDW alert handling
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 | static struct sdw_slave *sdw_get_slave(struct sdw_bus *bus, int i)
|
| 700 | {
|
| 701 | struct sdw_slave *slave;
|
| 702 |
|
| 703 | list_for_each_entry(slave, &bus->slaves, node) {
|
| 704 | if (slave->dev_num == i)
|
| 705 | return slave;
|
| 706 | }
|
| 707 |
|
| 708 | return NULL;
|
| 709 | }
|
| 710 |
|
| 711 | int sdw_compare_devid(struct sdw_slave *slave, struct sdw_slave_id id)
|
| 712 | {
|
| 713 | if (slave->id.mfg_id != id.mfg_id ||
|
| 714 | slave->id.part_id != id.part_id ||
|
| 715 | slave->id.class_id != id.class_id ||
|
| 716 | (slave->id.unique_id != SDW_IGNORED_UNIQUE_ID &&
|
| 717 | slave->id.unique_id != id.unique_id))
|
| 718 | return -ENODEV;
|
| 719 |
|
| 720 | return 0;
|
| 721 | }
|
| 722 | EXPORT_SYMBOL(sdw_compare_devid);
|
| 723 |
|
| 724 |
|
| 725 | static int sdw_get_device_num(struct sdw_slave *slave)
|
| 726 | {
|
| 727 | struct sdw_bus *bus = slave->bus;
|
| 728 | int bit;
|
| 729 |
|
| 730 | if (bus->ops && bus->ops->get_device_num) {
|
| 731 | bit = bus->ops->get_device_num(bus, slave);
|
| 732 | if (bit < 0)
|
| 733 | goto err;
|
| 734 | } else {
|
| 735 | bit = find_first_zero_bit(bus->assigned, SDW_MAX_DEVICES);
|
| 736 | if (bit == SDW_MAX_DEVICES) {
|
| 737 | bit = -ENODEV;
|
| 738 | goto err;
|
| 739 | }
|
| 740 | }
|
| 741 |
|
| 742 | |
| 743 | * Do not update dev_num in Slave data structure here,
|
| 744 | * Update once program dev_num is successful
|
| 745 |
|
| 746 | set_bit(bit, bus->assigned);
|
| 747 |
|
| 748 | err:
|
| 749 | return bit;
|
| 750 | }
|
| 751 |
|
| 752 | static int sdw_assign_device_num(struct sdw_slave *slave)
|
| 753 | {
|
| 754 | struct sdw_bus *bus = slave->bus;
|
| 755 | struct device *dev = bus->dev;
|
| 756 | int ret;
|
| 757 |
|
| 758 |
|
| 759 | if (!slave->dev_num) {
|
| 760 | if (!slave->dev_num_sticky) {
|
| 761 | int dev_num;
|
| 762 |
|
| 763 | mutex_lock(&slave->bus->bus_lock);
|
| 764 | dev_num = sdw_get_device_num(slave);
|
| 765 | mutex_unlock(&slave->bus->bus_lock);
|
| 766 | if (dev_num < 0) {
|
| 767 | dev_err(dev, "Get dev_num failed: %d\n", dev_num);
|
| 768 | return dev_num;
|
| 769 | }
|
| 770 |
|
| 771 | slave->dev_num_sticky = dev_num;
|
| 772 | } else {
|
| 773 | dev_dbg(dev, "Slave already registered, reusing dev_num: %d\n",
|
| 774 | slave->dev_num_sticky);
|
| 775 | }
|
| 776 | }
|
| 777 |
|
| 778 |
|
| 779 | slave->dev_num = 0;
|
| 780 |
|
| 781 | ret = sdw_write_no_pm(slave, SDW_SCP_DEVNUMBER, slave->dev_num_sticky);
|
| 782 | if (ret < 0) {
|
| 783 | dev_err(dev, "Program device_num %d failed: %d\n",
|
| 784 | slave->dev_num_sticky, ret);
|
| 785 | return ret;
|
| 786 | }
|
| 787 |
|
| 788 |
|
| 789 | slave->dev_num = slave->dev_num_sticky;
|
| 790 |
|
| 791 | if (bus->ops && bus->ops->new_peripheral_assigned)
|
| 792 | bus->ops->new_peripheral_assigned(bus, slave, slave->dev_num);
|
| 793 |
|
| 794 | return 0;
|
| 795 | }
|
| 796 |
|
| 797 | void sdw_extract_slave_id(struct sdw_bus *bus,
|
| 798 | u64 addr, struct sdw_slave_id *id)
|
| 799 | {
|
| 800 | dev_dbg(bus->dev, "SDW Slave Addr: %llx\n", addr);
|
| 801 |
|
| 802 | id->sdw_version = SDW_VERSION(addr);
|
| 803 | id->unique_id = SDW_UNIQUE_ID(addr);
|
| 804 | id->mfg_id = SDW_MFG_ID(addr);
|
| 805 | id->part_id = SDW_PART_ID(addr);
|
| 806 | id->class_id = SDW_CLASS_ID(addr);
|
| 807 |
|
| 808 | dev_dbg(bus->dev,
|
| 809 | "SDW Slave class_id 0x%02x, mfg_id 0x%04x, part_id 0x%04x, unique_id 0x%x, version 0x%x\n",
|
| 810 | id->class_id, id->mfg_id, id->part_id, id->unique_id, id->sdw_version);
|
| 811 | }
|
| 812 | EXPORT_SYMBOL(sdw_extract_slave_id);
|
| 813 |
|
| 814 | bool is_clock_scaling_supported_by_slave(struct sdw_slave *slave)
|
| 815 | {
|
| 816 | |
| 817 | * Dynamic scaling is a defined by SDCA. However, some devices expose the class ID but
|
| 818 | * can't support dynamic scaling. We might need a quirk to handle such devices.
|
| 819 | * The clock base and scale registers themselves are SoundWire 1.2, so a device
|
| 820 | * may implement them without setting the class field; the driver says so with
|
| 821 | * clock_reg_supported.
|
| 822 |
|
| 823 | return slave->id.class_id || slave->prop.clock_reg_supported;
|
| 824 | }
|
| 825 | EXPORT_SYMBOL(is_clock_scaling_supported_by_slave);
|
| 826 |
|
| 827 | static int sdw_program_device_num(struct sdw_bus *bus, bool *programmed)
|
| 828 | {
|
| 829 | u8 buf[SDW_NUM_DEV_ID_REGISTERS] = {0};
|
| 830 | struct sdw_slave *slave, *_s;
|
| 831 | struct sdw_slave_id id;
|
| 832 | struct sdw_msg msg;
|
| 833 | bool found;
|
| 834 | int count = 0, ret;
|
| 835 | u64 addr;
|
| 836 |
|
| 837 | *programmed = false;
|
| 838 |
|
| 839 |
|
| 840 | ret = sdw_fill_msg(&msg, NULL, SDW_SCP_DEVID_0,
|
| 841 | SDW_NUM_DEV_ID_REGISTERS, 0, SDW_MSG_FLAG_READ, buf);
|
| 842 | if (ret < 0)
|
| 843 | return ret;
|
| 844 |
|
| 845 | do {
|
| 846 | ret = sdw_transfer(bus, &msg);
|
| 847 | if (ret == -ENODATA) {
|
| 848 | dev_dbg(bus->dev, "No more devices to enumerate\n");
|
| 849 | ret = 0;
|
| 850 | break;
|
| 851 | }
|
| 852 | if (ret < 0) {
|
| 853 | dev_err(bus->dev, "DEVID read fail:%d\n", ret);
|
| 854 | break;
|
| 855 | }
|
| 856 |
|
| 857 | |
| 858 | * Construct the addr and extract. Cast the higher shift
|
| 859 | * bits to avoid truncation due to size limit.
|
| 860 |
|
| 861 | addr = buf[5] | (buf[4] << 8) | (buf[3] << 16) |
|
| 862 | ((u64)buf[2] << 24) | ((u64)buf[1] << 32) |
|
| 863 | ((u64)buf[0] << 40);
|
| 864 |
|
| 865 | sdw_extract_slave_id(bus, addr, &id);
|
| 866 |
|
| 867 | found = false;
|
| 868 |
|
| 869 | list_for_each_entry_safe(slave, _s, &bus->slaves, node) {
|
| 870 | if (sdw_compare_devid(slave, id) == 0) {
|
| 871 | found = true;
|
| 872 |
|
| 873 | |
| 874 | * To prevent skipping state-machine stages don't
|
| 875 | * program a device until we've seen it UNATTACH.
|
| 876 | * Must return here because no other device on #0
|
| 877 | * can be detected until this one has been
|
| 878 | * assigned a device ID.
|
| 879 |
|
| 880 | if (slave->status != SDW_SLAVE_UNATTACHED)
|
| 881 | return 0;
|
| 882 |
|
| 883 | |
| 884 | * Assign a new dev_num to this Slave and
|
| 885 | * not mark it present. It will be marked
|
| 886 | * present after it reports ATTACHED on new
|
| 887 | * dev_num
|
| 888 |
|
| 889 | ret = sdw_assign_device_num(slave);
|
| 890 | if (ret < 0) {
|
| 891 | dev_err(bus->dev,
|
| 892 | "Assign dev_num failed:%d\n",
|
| 893 | ret);
|
| 894 | return ret;
|
| 895 | }
|
| 896 |
|
| 897 | *programmed = true;
|
| 898 |
|
| 899 | break;
|
| 900 | }
|
| 901 | }
|
| 902 |
|
| 903 | if (!found) {
|
| 904 |
|
| 905 |
|
| 906 | |
| 907 | * add Slave device even if there is no platform
|
| 908 | * firmware description. There will be no driver probe
|
| 909 | * but the user/integration will be able to see the
|
| 910 | * device, enumeration status and device number in sysfs
|
| 911 |
|
| 912 | sdw_slave_add(bus, &id, NULL);
|
| 913 |
|
| 914 | dev_err(bus->dev, "Slave Entry not found\n");
|
| 915 | }
|
| 916 |
|
| 917 | count++;
|
| 918 |
|
| 919 | |
| 920 | * Check till error out or retry (count) exhausts.
|
| 921 | * Device can drop off and rejoin during enumeration
|
| 922 | * so count till twice the bound.
|
| 923 |
|
| 924 |
|
| 925 | } while (ret == 0 && count < (SDW_MAX_DEVICES * 2));
|
| 926 |
|
| 927 | return ret;
|
| 928 | }
|
| 929 |
|
| 930 | static void sdw_modify_slave_status(struct sdw_slave *slave,
|
| 931 | enum sdw_slave_status status)
|
| 932 | {
|
| 933 | struct sdw_bus *bus = slave->bus;
|
| 934 |
|
| 935 | mutex_lock(&bus->bus_lock);
|
| 936 |
|
| 937 | dev_vdbg(bus->dev,
|
| 938 | "changing status slave %d status %d new status %d\n",
|
| 939 | slave->dev_num, slave->status, status);
|
| 940 |
|
| 941 | if (status == SDW_SLAVE_UNATTACHED) {
|
| 942 | dev_dbg(&slave->dev,
|
| 943 | "initializing enumeration and init completion for Slave %d\n",
|
| 944 | slave->dev_num);
|
| 945 |
|
| 946 | reinit_completion(&slave->enumeration_complete);
|
| 947 | reinit_completion(&slave->initialization_complete);
|
| 948 |
|
| 949 | } else if ((status == SDW_SLAVE_ATTACHED) &&
|
| 950 | (slave->status == SDW_SLAVE_UNATTACHED)) {
|
| 951 | dev_dbg(&slave->dev,
|
| 952 | "signaling enumeration completion for Slave %d\n",
|
| 953 | slave->dev_num);
|
| 954 |
|
| 955 | complete_all(&slave->enumeration_complete);
|
| 956 | }
|
| 957 | slave->status = status;
|
| 958 | mutex_unlock(&bus->bus_lock);
|
| 959 | }
|
| 960 |
|
| 961 | static int sdw_slave_clk_stop_callback(struct sdw_slave *slave,
|
| 962 | enum sdw_clk_stop_mode mode,
|
| 963 | enum sdw_clk_stop_type type)
|
| 964 | {
|
| 965 | int ret = 0;
|
| 966 |
|
| 967 | mutex_lock(&slave->sdw_dev_lock);
|
| 968 |
|
| 969 | if (slave->probed) {
|
| 970 | struct device *dev = &slave->dev;
|
| 971 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
| 972 |
|
| 973 | if (drv->ops && drv->ops->clk_stop)
|
| 974 | ret = drv->ops->clk_stop(slave, mode, type);
|
| 975 | }
|
| 976 |
|
| 977 | mutex_unlock(&slave->sdw_dev_lock);
|
| 978 |
|
| 979 | return ret;
|
| 980 | }
|
| 981 |
|
| 982 | static int sdw_slave_clk_stop_prepare(struct sdw_slave *slave,
|
| 983 | enum sdw_clk_stop_mode mode,
|
| 984 | bool prepare)
|
| 985 | {
|
| 986 | bool wake_en;
|
| 987 | u32 val = 0;
|
| 988 | int ret;
|
| 989 |
|
| 990 | wake_en = slave->prop.wake_capable;
|
| 991 |
|
| 992 | if (prepare) {
|
| 993 | val = SDW_SCP_SYSTEMCTRL_CLK_STP_PREP;
|
| 994 |
|
| 995 | if (mode == SDW_CLK_STOP_MODE1)
|
| 996 | val |= SDW_SCP_SYSTEMCTRL_CLK_STP_MODE1;
|
| 997 |
|
| 998 | if (wake_en)
|
| 999 | val |= SDW_SCP_SYSTEMCTRL_WAKE_UP_EN;
|
| 1000 | } else {
|
| 1001 | ret = sdw_read_no_pm(slave, SDW_SCP_SYSTEMCTRL);
|
| 1002 | if (ret < 0) {
|
| 1003 | if (ret != -ENODATA)
|
| 1004 | dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL read failed:%d\n", ret);
|
| 1005 | return ret;
|
| 1006 | }
|
| 1007 | val = ret;
|
| 1008 | val &= ~(SDW_SCP_SYSTEMCTRL_CLK_STP_PREP);
|
| 1009 | }
|
| 1010 |
|
| 1011 | ret = sdw_write_no_pm(slave, SDW_SCP_SYSTEMCTRL, val);
|
| 1012 |
|
| 1013 | if (ret < 0 && ret != -ENODATA)
|
| 1014 | dev_err(&slave->dev, "SDW_SCP_SYSTEMCTRL write failed:%d\n", ret);
|
| 1015 |
|
| 1016 | return ret;
|
| 1017 | }
|
| 1018 |
|
| 1019 | static int sdw_bus_wait_for_clk_prep_deprep(struct sdw_bus *bus, u16 dev_num, bool prepare)
|
| 1020 | {
|
| 1021 | int retry = bus->clk_stop_timeout;
|
| 1022 | int val;
|
| 1023 |
|
| 1024 | do {
|
| 1025 | val = sdw_bread_no_pm(bus, dev_num, SDW_SCP_STAT);
|
| 1026 | if (val < 0) {
|
| 1027 | if (val != -ENODATA)
|
| 1028 | dev_err(bus->dev, "SDW_SCP_STAT bread failed:%d\n", val);
|
| 1029 | return val;
|
| 1030 | }
|
| 1031 | val &= SDW_SCP_STAT_CLK_STP_NF;
|
| 1032 | if (!val) {
|
| 1033 | dev_dbg(bus->dev, "clock stop %s done slave:%d\n",
|
| 1034 | prepare ? "prepare" : "deprepare",
|
| 1035 | dev_num);
|
| 1036 | return 0;
|
| 1037 | }
|
| 1038 |
|
| 1039 | usleep_range(1000, 1500);
|
| 1040 | retry--;
|
| 1041 | } while (retry);
|
| 1042 |
|
| 1043 | dev_dbg(bus->dev, "clock stop %s did not complete for slave:%d\n",
|
| 1044 | prepare ? "prepare" : "deprepare",
|
| 1045 | dev_num);
|
| 1046 |
|
| 1047 | return -ETIMEDOUT;
|
| 1048 | }
|
| 1049 |
|
| 1050 | |
| 1051 | * sdw_bus_prep_clk_stop: prepare Slave(s) for clock stop
|
| 1052 | *
|
| 1053 | * @bus: SDW bus instance
|
| 1054 | *
|
| 1055 | * Query Slave for clock stop mode and prepare for that mode.
|
| 1056 |
|
| 1057 | int sdw_bus_prep_clk_stop(struct sdw_bus *bus)
|
| 1058 | {
|
| 1059 | bool simple_clk_stop = true;
|
| 1060 | struct sdw_slave *slave;
|
| 1061 | bool is_slave = false;
|
| 1062 | int ret = 0;
|
| 1063 |
|
| 1064 | |
| 1065 | * In order to save on transition time, prepare
|
| 1066 | * each Slave and then wait for all Slave(s) to be
|
| 1067 | * prepared for clock stop.
|
| 1068 | * If one of the Slave devices has lost sync and
|
| 1069 | * replies with Command Ignored/-ENODATA, we continue
|
| 1070 | * the loop
|
| 1071 |
|
| 1072 | list_for_each_entry(slave, &bus->slaves, node) {
|
| 1073 | if (!slave->dev_num)
|
| 1074 | continue;
|
| 1075 |
|
| 1076 | if (slave->status != SDW_SLAVE_ATTACHED &&
|
| 1077 | slave->status != SDW_SLAVE_ALERT)
|
| 1078 | continue;
|
| 1079 |
|
| 1080 |
|
| 1081 | is_slave = true;
|
| 1082 |
|
| 1083 | ret = sdw_slave_clk_stop_callback(slave,
|
| 1084 | SDW_CLK_STOP_MODE0,
|
| 1085 | SDW_CLK_PRE_PREPARE);
|
| 1086 | if (ret < 0 && ret != -ENODATA) {
|
| 1087 | dev_err(&slave->dev, "clock stop pre-prepare cb failed:%d\n", ret);
|
| 1088 | return ret;
|
| 1089 | }
|
| 1090 |
|
| 1091 |
|
| 1092 | if (!slave->prop.simple_clk_stop_capable) {
|
| 1093 | simple_clk_stop = false;
|
| 1094 |
|
| 1095 | ret = sdw_slave_clk_stop_prepare(slave,
|
| 1096 | SDW_CLK_STOP_MODE0,
|
| 1097 | true);
|
| 1098 | if (ret < 0 && ret != -ENODATA) {
|
| 1099 | dev_err(&slave->dev, "clock stop prepare failed:%d\n", ret);
|
| 1100 | return ret;
|
| 1101 | }
|
| 1102 | }
|
| 1103 | }
|
| 1104 |
|
| 1105 |
|
| 1106 | if (!is_slave)
|
| 1107 | return 0;
|
| 1108 |
|
| 1109 | |
| 1110 | * Don't wait for all Slaves to be ready if they follow the simple
|
| 1111 | * state machine
|
| 1112 |
|
| 1113 | if (!simple_clk_stop) {
|
| 1114 | ret = sdw_bus_wait_for_clk_prep_deprep(bus,
|
| 1115 | SDW_BROADCAST_DEV_NUM, true);
|
| 1116 | |
| 1117 | * if there are no Slave devices present and the reply is
|
| 1118 | * Command_Ignored/-ENODATA, we don't need to continue with the
|
| 1119 | * flow and can just return here. The error code is not modified
|
| 1120 | * and its handling left as an exercise for the caller.
|
| 1121 |
|
| 1122 | if (ret < 0)
|
| 1123 | return ret;
|
| 1124 | }
|
| 1125 |
|
| 1126 |
|
| 1127 | list_for_each_entry(slave, &bus->slaves, node) {
|
| 1128 | if (!slave->dev_num)
|
| 1129 | continue;
|
| 1130 |
|
| 1131 | if (slave->status != SDW_SLAVE_ATTACHED &&
|
| 1132 | slave->status != SDW_SLAVE_ALERT)
|
| 1133 | continue;
|
| 1134 |
|
| 1135 | ret = sdw_slave_clk_stop_callback(slave,
|
| 1136 | SDW_CLK_STOP_MODE0,
|
| 1137 | SDW_CLK_POST_PREPARE);
|
| 1138 |
|
| 1139 | if (ret < 0 && ret != -ENODATA) {
|
| 1140 | dev_err(&slave->dev, "clock stop post-prepare cb failed:%d\n", ret);
|
| 1141 | return ret;
|
| 1142 | }
|
| 1143 | }
|
| 1144 |
|
| 1145 | return 0;
|
| 1146 | }
|
| 1147 | EXPORT_SYMBOL(sdw_bus_prep_clk_stop);
|
| 1148 |
|
| 1149 | |
| 1150 | * sdw_bus_clk_stop: stop bus clock
|
| 1151 | *
|
| 1152 | * @bus: SDW bus instance
|
| 1153 | *
|
| 1154 | * After preparing the Slaves for clock stop, stop the clock by broadcasting
|
| 1155 | * write to SCP_CTRL register.
|
| 1156 |
|
| 1157 | int sdw_bus_clk_stop(struct sdw_bus *bus)
|
| 1158 | {
|
| 1159 | int ret;
|
| 1160 |
|
| 1161 | |
| 1162 | * broadcast clock stop now, attached Slaves will ACK this,
|
| 1163 | * unattached will ignore
|
| 1164 |
|
| 1165 | ret = sdw_bwrite_no_pm(bus, SDW_BROADCAST_DEV_NUM,
|
| 1166 | SDW_SCP_CTRL, SDW_SCP_CTRL_CLK_STP_NOW);
|
| 1167 | if (ret < 0) {
|
| 1168 | if (ret != -ENODATA)
|
| 1169 | dev_err(bus->dev, "ClockStopNow Broadcast msg failed %d\n", ret);
|
| 1170 | return ret;
|
| 1171 | }
|
| 1172 |
|
| 1173 | return 0;
|
| 1174 | }
|
| 1175 | EXPORT_SYMBOL(sdw_bus_clk_stop);
|
| 1176 |
|
| 1177 | |
| 1178 | * sdw_bus_exit_clk_stop: Exit clock stop mode
|
| 1179 | *
|
| 1180 | * @bus: SDW bus instance
|
| 1181 | *
|
| 1182 | * This De-prepares the Slaves by exiting Clock Stop Mode 0. For the Slaves
|
| 1183 | * exiting Clock Stop Mode 1, they will be de-prepared after they enumerate
|
| 1184 | * back.
|
| 1185 |
|
| 1186 | int sdw_bus_exit_clk_stop(struct sdw_bus *bus)
|
| 1187 | {
|
| 1188 | bool simple_clk_stop = true;
|
| 1189 | struct sdw_slave *slave;
|
| 1190 | bool is_slave = false;
|
| 1191 | int ret;
|
| 1192 |
|
| 1193 | |
| 1194 | * In order to save on transition time, de-prepare
|
| 1195 | * each Slave and then wait for all Slave(s) to be
|
| 1196 | * de-prepared after clock resume.
|
| 1197 |
|
| 1198 | list_for_each_entry(slave, &bus->slaves, node) {
|
| 1199 | if (!slave->dev_num)
|
| 1200 | continue;
|
| 1201 |
|
| 1202 | if (slave->status != SDW_SLAVE_ATTACHED &&
|
| 1203 | slave->status != SDW_SLAVE_ALERT)
|
| 1204 | continue;
|
| 1205 |
|
| 1206 |
|
| 1207 | is_slave = true;
|
| 1208 |
|
| 1209 | ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
|
| 1210 | SDW_CLK_PRE_DEPREPARE);
|
| 1211 | if (ret < 0)
|
| 1212 | dev_warn(&slave->dev, "clock stop pre-deprepare cb failed:%d\n", ret);
|
| 1213 |
|
| 1214 |
|
| 1215 | if (!slave->prop.simple_clk_stop_capable) {
|
| 1216 | simple_clk_stop = false;
|
| 1217 |
|
| 1218 | ret = sdw_slave_clk_stop_prepare(slave, SDW_CLK_STOP_MODE0,
|
| 1219 | false);
|
| 1220 |
|
| 1221 | if (ret < 0)
|
| 1222 | dev_warn(&slave->dev, "clock stop deprepare failed:%d\n", ret);
|
| 1223 | }
|
| 1224 | }
|
| 1225 |
|
| 1226 |
|
| 1227 | if (!is_slave)
|
| 1228 | return 0;
|
| 1229 |
|
| 1230 | |
| 1231 | * Don't wait for all Slaves to be ready if they follow the simple
|
| 1232 | * state machine
|
| 1233 |
|
| 1234 | if (!simple_clk_stop) {
|
| 1235 | ret = sdw_bus_wait_for_clk_prep_deprep(bus, SDW_BROADCAST_DEV_NUM, false);
|
| 1236 | if (ret < 0)
|
| 1237 | dev_warn(bus->dev, "clock stop deprepare wait failed:%d\n", ret);
|
| 1238 | }
|
| 1239 |
|
| 1240 | list_for_each_entry(slave, &bus->slaves, node) {
|
| 1241 | if (!slave->dev_num)
|
| 1242 | continue;
|
| 1243 |
|
| 1244 | if (slave->status != SDW_SLAVE_ATTACHED &&
|
| 1245 | slave->status != SDW_SLAVE_ALERT)
|
| 1246 | continue;
|
| 1247 |
|
| 1248 | ret = sdw_slave_clk_stop_callback(slave, SDW_CLK_STOP_MODE0,
|
| 1249 | SDW_CLK_POST_DEPREPARE);
|
| 1250 | if (ret < 0)
|
| 1251 | dev_warn(&slave->dev, "clock stop post-deprepare cb failed:%d\n", ret);
|
| 1252 | }
|
| 1253 |
|
| 1254 | return 0;
|
| 1255 | }
|
| 1256 | EXPORT_SYMBOL(sdw_bus_exit_clk_stop);
|
| 1257 |
|
| 1258 | int sdw_configure_dpn_intr(struct sdw_slave *slave,
|
| 1259 | int port, bool enable, int mask)
|
| 1260 | {
|
| 1261 | u32 addr;
|
| 1262 | int ret;
|
| 1263 | u8 val = 0;
|
| 1264 |
|
| 1265 | if (slave->bus->params.s_data_mode != SDW_PORT_DATA_MODE_NORMAL) {
|
| 1266 | dev_dbg(&slave->dev, "TEST FAIL interrupt %s\n",
|
| 1267 | str_on_off(enable));
|
| 1268 | mask |= SDW_DPN_INT_TEST_FAIL;
|
| 1269 | }
|
| 1270 |
|
| 1271 | addr = SDW_DPN_INTMASK(port);
|
| 1272 |
|
| 1273 |
|
| 1274 | if (enable) {
|
| 1275 | val |= mask;
|
| 1276 | val |= SDW_DPN_INT_PORT_READY;
|
| 1277 | } else {
|
| 1278 | val &= ~(mask);
|
| 1279 | val &= ~SDW_DPN_INT_PORT_READY;
|
| 1280 | }
|
| 1281 |
|
| 1282 | ret = sdw_update_no_pm(slave, addr, (mask | SDW_DPN_INT_PORT_READY), val);
|
| 1283 | if (ret < 0)
|
| 1284 | dev_err(&slave->dev,
|
| 1285 | "SDW_DPN_INTMASK write failed:%d\n", val);
|
| 1286 |
|
| 1287 | return ret;
|
| 1288 | }
|
| 1289 |
|
| 1290 | int sdw_slave_get_scale_index(struct sdw_slave *slave, u8 *base)
|
| 1291 | {
|
| 1292 | u32 mclk_freq = slave->bus->prop.mclk_freq;
|
| 1293 | u32 curr_freq = slave->bus->params.curr_dr_freq >> 1;
|
| 1294 | unsigned int scale;
|
| 1295 | u8 scale_index;
|
| 1296 |
|
| 1297 | if (!mclk_freq) {
|
| 1298 | dev_err(&slave->dev,
|
| 1299 | "no bus MCLK, cannot set SDW_SCP_BUS_CLOCK_BASE\n");
|
| 1300 | return -EINVAL;
|
| 1301 | }
|
| 1302 |
|
| 1303 | |
| 1304 | * map base frequency using Table 89 of SoundWire 1.2 spec.
|
| 1305 | * The order of the tests just follows the specification, this
|
| 1306 | * is not a selection between possible values or a search for
|
| 1307 | * the best value but just a mapping. Only one case per platform
|
| 1308 | * is relevant.
|
| 1309 | * Some BIOS have inconsistent values for mclk_freq but a
|
| 1310 | * correct root so we force the mclk_freq to avoid variations.
|
| 1311 |
|
| 1312 | if (!(19200000 % mclk_freq)) {
|
| 1313 | mclk_freq = 19200000;
|
| 1314 | *base = SDW_SCP_BASE_CLOCK_19200000_HZ;
|
| 1315 | } else if (!(22579200 % mclk_freq)) {
|
| 1316 | mclk_freq = 22579200;
|
| 1317 | *base = SDW_SCP_BASE_CLOCK_22579200_HZ;
|
| 1318 | } else if (!(24576000 % mclk_freq)) {
|
| 1319 | mclk_freq = 24576000;
|
| 1320 | *base = SDW_SCP_BASE_CLOCK_24576000_HZ;
|
| 1321 | } else if (!(32000000 % mclk_freq)) {
|
| 1322 | mclk_freq = 32000000;
|
| 1323 | *base = SDW_SCP_BASE_CLOCK_32000000_HZ;
|
| 1324 | } else if (!(96000000 % mclk_freq)) {
|
| 1325 | mclk_freq = 24000000;
|
| 1326 | *base = SDW_SCP_BASE_CLOCK_24000000_HZ;
|
| 1327 | } else {
|
| 1328 | dev_err(&slave->dev,
|
| 1329 | "Unsupported clock base, mclk %d\n",
|
| 1330 | mclk_freq);
|
| 1331 | return -EINVAL;
|
| 1332 | }
|
| 1333 |
|
| 1334 | if (mclk_freq % curr_freq) {
|
| 1335 | dev_err(&slave->dev,
|
| 1336 | "mclk %d is not multiple of bus curr_freq %d\n",
|
| 1337 | mclk_freq, curr_freq);
|
| 1338 | return -EINVAL;
|
| 1339 | }
|
| 1340 |
|
| 1341 | scale = mclk_freq / curr_freq;
|
| 1342 |
|
| 1343 | |
| 1344 | * map scale to Table 90 of SoundWire 1.2 spec - and check
|
| 1345 | * that the scale is a power of two and maximum 64
|
| 1346 |
|
| 1347 | scale_index = ilog2(scale);
|
| 1348 |
|
| 1349 | if (BIT(scale_index) != scale || scale_index > 6) {
|
| 1350 | dev_err(&slave->dev,
|
| 1351 | "No match found for scale %d, bus mclk %d curr_freq %d\n",
|
| 1352 | scale, mclk_freq, curr_freq);
|
| 1353 | return -EINVAL;
|
| 1354 | }
|
| 1355 | scale_index++;
|
| 1356 |
|
| 1357 | dev_dbg(&slave->dev,
|
| 1358 | "Configured bus base %d, scale %d, mclk %d, curr_freq %d\n",
|
| 1359 | *base, scale_index, mclk_freq, curr_freq);
|
| 1360 |
|
| 1361 | return scale_index;
|
| 1362 | }
|
| 1363 | EXPORT_SYMBOL(sdw_slave_get_scale_index);
|
| 1364 |
|
| 1365 | int sdw_slave_get_current_bank(struct sdw_slave *slave)
|
| 1366 | {
|
| 1367 | int tmp;
|
| 1368 |
|
| 1369 | tmp = sdw_read(slave, SDW_SCP_CTRL);
|
| 1370 | if (tmp < 0)
|
| 1371 | return tmp;
|
| 1372 |
|
| 1373 | return FIELD_GET(SDW_SCP_STAT_CURR_BANK, tmp);
|
| 1374 | }
|
| 1375 | EXPORT_SYMBOL_GPL(sdw_slave_get_current_bank);
|
| 1376 |
|
| 1377 | static int sdw_slave_set_frequency(struct sdw_slave *slave)
|
| 1378 | {
|
| 1379 | int scale_index;
|
| 1380 | u8 base;
|
| 1381 | int ret;
|
| 1382 |
|
| 1383 | |
| 1384 | * frequency base and scale registers are required for SDCA
|
| 1385 | * devices. They may also be used for 1.2+/non-SDCA devices.
|
| 1386 | * Driver can set the property directly, for now there's no
|
| 1387 | * DisCo property to discover support for the scaling registers
|
| 1388 | * from platform firmware.
|
| 1389 |
|
| 1390 | if (!is_clock_scaling_supported_by_slave(slave))
|
| 1391 | return 0;
|
| 1392 |
|
| 1393 | scale_index = sdw_slave_get_scale_index(slave, &base);
|
| 1394 | if (scale_index < 0)
|
| 1395 | return scale_index;
|
| 1396 |
|
| 1397 | ret = sdw_write_no_pm(slave, SDW_SCP_BUS_CLOCK_BASE, base);
|
| 1398 | if (ret < 0) {
|
| 1399 | dev_err(&slave->dev,
|
| 1400 | "SDW_SCP_BUS_CLOCK_BASE write failed:%d\n", ret);
|
| 1401 | return ret;
|
| 1402 | }
|
| 1403 |
|
| 1404 |
|
| 1405 | ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B0, scale_index);
|
| 1406 | if (ret < 0) {
|
| 1407 | dev_err(&slave->dev,
|
| 1408 | "SDW_SCP_BUSCLOCK_SCALE_B0 write failed:%d\n", ret);
|
| 1409 | return ret;
|
| 1410 | }
|
| 1411 | ret = sdw_write_no_pm(slave, SDW_SCP_BUSCLOCK_SCALE_B1, scale_index);
|
| 1412 | if (ret < 0)
|
| 1413 | dev_err(&slave->dev,
|
| 1414 | "SDW_SCP_BUSCLOCK_SCALE_B1 write failed:%d\n", ret);
|
| 1415 |
|
| 1416 | return ret;
|
| 1417 | }
|
| 1418 |
|
| 1419 | static int sdw_initialize_slave(struct sdw_slave *slave)
|
| 1420 | {
|
| 1421 | struct sdw_slave_prop *prop = &slave->prop;
|
| 1422 | int status;
|
| 1423 | int ret;
|
| 1424 | u8 val;
|
| 1425 |
|
| 1426 | ret = sdw_slave_set_frequency(slave);
|
| 1427 | if (ret < 0)
|
| 1428 | return ret;
|
| 1429 |
|
| 1430 | if (slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH) {
|
| 1431 |
|
| 1432 | status = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
| 1433 | if (status < 0) {
|
| 1434 | dev_err(&slave->dev,
|
| 1435 | "SDW_SCP_INT1 (BUS_CLASH) read failed:%d\n", status);
|
| 1436 | return status;
|
| 1437 | }
|
| 1438 | if (status & SDW_SCP_INT1_BUS_CLASH) {
|
| 1439 | dev_warn(&slave->dev, "Bus clash detected before INT mask is enabled\n");
|
| 1440 | ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_BUS_CLASH);
|
| 1441 | if (ret < 0) {
|
| 1442 | dev_err(&slave->dev,
|
| 1443 | "SDW_SCP_INT1 (BUS_CLASH) write failed:%d\n", ret);
|
| 1444 | return ret;
|
| 1445 | }
|
| 1446 | }
|
| 1447 | }
|
| 1448 | if ((slave->bus->prop.quirks & SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY) &&
|
| 1449 | !(prop->quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY)) {
|
| 1450 |
|
| 1451 | status = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
| 1452 | if (status < 0) {
|
| 1453 | dev_err(&slave->dev,
|
| 1454 | "SDW_SCP_INT1 (PARITY) read failed:%d\n", status);
|
| 1455 | return status;
|
| 1456 | }
|
| 1457 | if (status & SDW_SCP_INT1_PARITY) {
|
| 1458 | dev_warn(&slave->dev, "PARITY error detected before INT mask is enabled\n");
|
| 1459 | ret = sdw_write_no_pm(slave, SDW_SCP_INT1, SDW_SCP_INT1_PARITY);
|
| 1460 | if (ret < 0) {
|
| 1461 | dev_err(&slave->dev,
|
| 1462 | "SDW_SCP_INT1 (PARITY) write failed:%d\n", ret);
|
| 1463 | return ret;
|
| 1464 | }
|
| 1465 | }
|
| 1466 | }
|
| 1467 |
|
| 1468 | |
| 1469 | * Set SCP_INT1_MASK register, typically bus clash and
|
| 1470 | * implementation-defined interrupt mask. The Parity detection
|
| 1471 | * may not always be correct on startup so its use is
|
| 1472 | * device-dependent, it might e.g. only be enabled in
|
| 1473 | * steady-state after a couple of frames.
|
| 1474 |
|
| 1475 | val = prop->scp_int1_mask;
|
| 1476 |
|
| 1477 |
|
| 1478 | ret = sdw_update_no_pm(slave, SDW_SCP_INTMASK1, val, val);
|
| 1479 | if (ret < 0) {
|
| 1480 | dev_err(&slave->dev,
|
| 1481 | "SDW_SCP_INTMASK1 write failed:%d\n", ret);
|
| 1482 | return ret;
|
| 1483 | }
|
| 1484 |
|
| 1485 |
|
| 1486 | if (!prop->dp0_prop)
|
| 1487 | return 0;
|
| 1488 |
|
| 1489 |
|
| 1490 | val = prop->dp0_prop->imp_def_interrupts;
|
| 1491 | val |= SDW_DP0_INT_PORT_READY | SDW_DP0_INT_BRA_FAILURE;
|
| 1492 |
|
| 1493 | ret = sdw_update_no_pm(slave, SDW_DP0_INTMASK, val, val);
|
| 1494 | if (ret < 0)
|
| 1495 | dev_err(&slave->dev,
|
| 1496 | "SDW_DP0_INTMASK read failed:%d\n", ret);
|
| 1497 | return ret;
|
| 1498 | }
|
| 1499 |
|
| 1500 | static int sdw_handle_dp0_interrupt(struct sdw_slave *slave, u8 *slave_status)
|
| 1501 | {
|
| 1502 | u8 clear, impl_int_mask;
|
| 1503 | int status, status2, ret, count = 0;
|
| 1504 |
|
| 1505 | status = sdw_read_no_pm(slave, SDW_DP0_INT);
|
| 1506 | if (status < 0) {
|
| 1507 | dev_err(&slave->dev,
|
| 1508 | "SDW_DP0_INT read failed:%d\n", status);
|
| 1509 | return status;
|
| 1510 | }
|
| 1511 |
|
| 1512 | do {
|
| 1513 | clear = status & ~(SDW_DP0_INTERRUPTS | SDW_DP0_SDCA_CASCADE);
|
| 1514 |
|
| 1515 | if (status & SDW_DP0_INT_TEST_FAIL) {
|
| 1516 | dev_err(&slave->dev, "Test fail for port 0\n");
|
| 1517 | clear |= SDW_DP0_INT_TEST_FAIL;
|
| 1518 | }
|
| 1519 |
|
| 1520 | |
| 1521 | * Assumption: PORT_READY interrupt will be received only for
|
| 1522 | * ports implementing Channel Prepare state machine (CP_SM)
|
| 1523 |
|
| 1524 |
|
| 1525 | if (status & SDW_DP0_INT_PORT_READY) {
|
| 1526 | complete(&slave->port_ready[0]);
|
| 1527 | clear |= SDW_DP0_INT_PORT_READY;
|
| 1528 | }
|
| 1529 |
|
| 1530 | if (status & SDW_DP0_INT_BRA_FAILURE) {
|
| 1531 | dev_err(&slave->dev, "BRA failed\n");
|
| 1532 | clear |= SDW_DP0_INT_BRA_FAILURE;
|
| 1533 | }
|
| 1534 |
|
| 1535 | impl_int_mask = SDW_DP0_INT_IMPDEF1 |
|
| 1536 | SDW_DP0_INT_IMPDEF2 | SDW_DP0_INT_IMPDEF3;
|
| 1537 |
|
| 1538 | if (status & impl_int_mask) {
|
| 1539 | clear |= impl_int_mask;
|
| 1540 | *slave_status = clear;
|
| 1541 | }
|
| 1542 |
|
| 1543 |
|
| 1544 | ret = sdw_write_no_pm(slave, SDW_DP0_INT, clear);
|
| 1545 | if (ret < 0) {
|
| 1546 | dev_err(&slave->dev,
|
| 1547 | "SDW_DP0_INT write failed:%d\n", ret);
|
| 1548 | return ret;
|
| 1549 | }
|
| 1550 |
|
| 1551 |
|
| 1552 | status2 = sdw_read_no_pm(slave, SDW_DP0_INT);
|
| 1553 | if (status2 < 0) {
|
| 1554 | dev_err(&slave->dev,
|
| 1555 | "SDW_DP0_INT read failed:%d\n", status2);
|
| 1556 | return status2;
|
| 1557 | }
|
| 1558 |
|
| 1559 | status &= status2;
|
| 1560 |
|
| 1561 | count++;
|
| 1562 |
|
| 1563 |
|
| 1564 | } while ((status & SDW_DP0_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
|
| 1565 |
|
| 1566 | if (count == SDW_READ_INTR_CLEAR_RETRY)
|
| 1567 | dev_warn(&slave->dev, "Reached MAX_RETRY on DP0 read\n");
|
| 1568 |
|
| 1569 | return ret;
|
| 1570 | }
|
| 1571 |
|
| 1572 | static int sdw_handle_port_interrupt(struct sdw_slave *slave,
|
| 1573 | int port, u8 *slave_status)
|
| 1574 | {
|
| 1575 | u8 clear, impl_int_mask;
|
| 1576 | int status, status2, ret, count = 0;
|
| 1577 | u32 addr;
|
| 1578 |
|
| 1579 | if (port == 0)
|
| 1580 | return sdw_handle_dp0_interrupt(slave, slave_status);
|
| 1581 |
|
| 1582 | addr = SDW_DPN_INT(port);
|
| 1583 | status = sdw_read_no_pm(slave, addr);
|
| 1584 | if (status < 0) {
|
| 1585 | dev_err(&slave->dev,
|
| 1586 | "SDW_DPN_INT read failed:%d\n", status);
|
| 1587 |
|
| 1588 | return status;
|
| 1589 | }
|
| 1590 |
|
| 1591 | do {
|
| 1592 | clear = status & ~SDW_DPN_INTERRUPTS;
|
| 1593 |
|
| 1594 | if (status & SDW_DPN_INT_TEST_FAIL) {
|
| 1595 | dev_err(&slave->dev, "Test fail for port:%d\n", port);
|
| 1596 | clear |= SDW_DPN_INT_TEST_FAIL;
|
| 1597 | }
|
| 1598 |
|
| 1599 | |
| 1600 | * Assumption: PORT_READY interrupt will be received only
|
| 1601 | * for ports implementing CP_SM.
|
| 1602 |
|
| 1603 | if (status & SDW_DPN_INT_PORT_READY) {
|
| 1604 | complete(&slave->port_ready[port]);
|
| 1605 | clear |= SDW_DPN_INT_PORT_READY;
|
| 1606 | }
|
| 1607 |
|
| 1608 | impl_int_mask = SDW_DPN_INT_IMPDEF1 |
|
| 1609 | SDW_DPN_INT_IMPDEF2 | SDW_DPN_INT_IMPDEF3;
|
| 1610 |
|
| 1611 | if (status & impl_int_mask) {
|
| 1612 | clear |= impl_int_mask;
|
| 1613 | *slave_status = clear;
|
| 1614 | }
|
| 1615 |
|
| 1616 |
|
| 1617 | ret = sdw_write_no_pm(slave, addr, clear);
|
| 1618 | if (ret < 0) {
|
| 1619 | dev_err(&slave->dev,
|
| 1620 | "SDW_DPN_INT write failed:%d\n", ret);
|
| 1621 | return ret;
|
| 1622 | }
|
| 1623 |
|
| 1624 |
|
| 1625 | status2 = sdw_read_no_pm(slave, addr);
|
| 1626 | if (status2 < 0) {
|
| 1627 | dev_err(&slave->dev,
|
| 1628 | "SDW_DPN_INT read failed:%d\n", status2);
|
| 1629 | return status2;
|
| 1630 | }
|
| 1631 |
|
| 1632 | status &= status2;
|
| 1633 |
|
| 1634 | count++;
|
| 1635 |
|
| 1636 |
|
| 1637 | } while ((status & SDW_DPN_INTERRUPTS) && (count < SDW_READ_INTR_CLEAR_RETRY));
|
| 1638 |
|
| 1639 | if (count == SDW_READ_INTR_CLEAR_RETRY)
|
| 1640 | dev_warn(&slave->dev, "Reached MAX_RETRY on port read");
|
| 1641 |
|
| 1642 | return ret;
|
| 1643 | }
|
| 1644 |
|
| 1645 | static int sdw_handle_slave_alerts(struct sdw_slave *slave)
|
| 1646 | {
|
| 1647 | struct sdw_slave_intr_status slave_intr;
|
| 1648 | u8 clear = 0, bit, port_status[15] = {0};
|
| 1649 | int port_num, stat, ret, count = 0;
|
| 1650 | unsigned long port;
|
| 1651 | bool slave_notify;
|
| 1652 | u8 sdca_cascade = 0;
|
| 1653 | u8 buf, buf2[2];
|
| 1654 | bool parity_check;
|
| 1655 | bool parity_quirk;
|
| 1656 |
|
| 1657 | sdw_modify_slave_status(slave, SDW_SLAVE_ALERT);
|
| 1658 |
|
| 1659 | ret = pm_runtime_get_sync(&slave->dev);
|
| 1660 | if (ret < 0 && ret != -EACCES) {
|
| 1661 | dev_err(&slave->dev, "Failed to resume device: %d\n", ret);
|
| 1662 | pm_runtime_put_noidle(&slave->dev);
|
| 1663 | return ret;
|
| 1664 | }
|
| 1665 |
|
| 1666 |
|
| 1667 | ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
| 1668 | if (ret < 0) {
|
| 1669 | dev_err(&slave->dev,
|
| 1670 | "SDW_SCP_INT1 read failed:%d\n", ret);
|
| 1671 | goto io_err;
|
| 1672 | }
|
| 1673 | buf = ret;
|
| 1674 |
|
| 1675 | ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
|
| 1676 | if (ret < 0) {
|
| 1677 | dev_err(&slave->dev,
|
| 1678 | "SDW_SCP_INT2/3 read failed:%d\n", ret);
|
| 1679 | goto io_err;
|
| 1680 | }
|
| 1681 |
|
| 1682 | if (slave->id.class_id) {
|
| 1683 | ret = sdw_read_no_pm(slave, SDW_DP0_INT);
|
| 1684 | if (ret < 0) {
|
| 1685 | dev_err(&slave->dev,
|
| 1686 | "SDW_DP0_INT read failed:%d\n", ret);
|
| 1687 | goto io_err;
|
| 1688 | }
|
| 1689 | sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
|
| 1690 | }
|
| 1691 |
|
| 1692 | do {
|
| 1693 | slave_notify = false;
|
| 1694 |
|
| 1695 | |
| 1696 | * Check parity, bus clash and Slave (impl defined)
|
| 1697 | * interrupt
|
| 1698 |
|
| 1699 | if (buf & SDW_SCP_INT1_PARITY) {
|
| 1700 | parity_check = slave->prop.scp_int1_mask & SDW_SCP_INT1_PARITY;
|
| 1701 | parity_quirk = !slave->first_interrupt_done &&
|
| 1702 | (slave->prop.quirks & SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY);
|
| 1703 |
|
| 1704 | if (parity_check && !parity_quirk)
|
| 1705 | dev_err(&slave->dev, "Parity error detected\n");
|
| 1706 | clear |= SDW_SCP_INT1_PARITY;
|
| 1707 | }
|
| 1708 |
|
| 1709 | if (buf & SDW_SCP_INT1_BUS_CLASH) {
|
| 1710 | if (slave->prop.scp_int1_mask & SDW_SCP_INT1_BUS_CLASH)
|
| 1711 | dev_err(&slave->dev, "Bus clash detected\n");
|
| 1712 | clear |= SDW_SCP_INT1_BUS_CLASH;
|
| 1713 | }
|
| 1714 |
|
| 1715 | |
| 1716 | * When bus clash or parity errors are detected, such errors
|
| 1717 | * are unlikely to be recoverable errors.
|
| 1718 | * TODO: In such scenario, reset bus. Make this configurable
|
| 1719 | * via sysfs property with bus reset being the default.
|
| 1720 |
|
| 1721 |
|
| 1722 | if (buf & SDW_SCP_INT1_IMPL_DEF) {
|
| 1723 | if (slave->prop.scp_int1_mask & SDW_SCP_INT1_IMPL_DEF) {
|
| 1724 | dev_dbg(&slave->dev, "Slave impl defined interrupt\n");
|
| 1725 | slave_notify = true;
|
| 1726 | }
|
| 1727 | clear |= SDW_SCP_INT1_IMPL_DEF;
|
| 1728 | }
|
| 1729 |
|
| 1730 |
|
| 1731 | if (sdca_cascade)
|
| 1732 | slave_notify = true;
|
| 1733 |
|
| 1734 |
|
| 1735 | port = buf & SDW_SCP_INT1_PORT0_3;
|
| 1736 |
|
| 1737 |
|
| 1738 | port = FIELD_GET(SDW_SCP_INT1_PORT0_3, port);
|
| 1739 | for_each_set_bit(bit, &port, 8) {
|
| 1740 | sdw_handle_port_interrupt(slave, bit,
|
| 1741 | &port_status[bit]);
|
| 1742 | }
|
| 1743 |
|
| 1744 |
|
| 1745 | if (buf & SDW_SCP_INT1_SCP2_CASCADE) {
|
| 1746 | port = buf2[0] & SDW_SCP_INTSTAT2_PORT4_10;
|
| 1747 | for_each_set_bit(bit, &port, 8) {
|
| 1748 |
|
| 1749 | port_num = bit + 4;
|
| 1750 | sdw_handle_port_interrupt(slave,
|
| 1751 | port_num,
|
| 1752 | &port_status[port_num]);
|
| 1753 | }
|
| 1754 | }
|
| 1755 |
|
| 1756 |
|
| 1757 | if (buf2[0] & SDW_SCP_INTSTAT2_SCP3_CASCADE) {
|
| 1758 | port = buf2[1] & SDW_SCP_INTSTAT3_PORT11_14;
|
| 1759 | for_each_set_bit(bit, &port, 8) {
|
| 1760 |
|
| 1761 | port_num = bit + 11;
|
| 1762 | sdw_handle_port_interrupt(slave,
|
| 1763 | port_num,
|
| 1764 | &port_status[port_num]);
|
| 1765 | }
|
| 1766 | }
|
| 1767 |
|
| 1768 |
|
| 1769 | if (slave_notify) {
|
| 1770 | if (slave->prop.use_domain_irq && slave->irq)
|
| 1771 | handle_nested_irq(slave->irq);
|
| 1772 |
|
| 1773 | mutex_lock(&slave->sdw_dev_lock);
|
| 1774 |
|
| 1775 | if (slave->probed) {
|
| 1776 | struct device *dev = &slave->dev;
|
| 1777 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
| 1778 |
|
| 1779 | if (drv->ops && drv->ops->interrupt_callback) {
|
| 1780 | slave_intr.sdca_cascade = sdca_cascade;
|
| 1781 | slave_intr.control_port = clear;
|
| 1782 | memcpy(slave_intr.port, &port_status,
|
| 1783 | sizeof(slave_intr.port));
|
| 1784 |
|
| 1785 | drv->ops->interrupt_callback(slave, &slave_intr);
|
| 1786 | }
|
| 1787 | }
|
| 1788 |
|
| 1789 | mutex_unlock(&slave->sdw_dev_lock);
|
| 1790 | }
|
| 1791 |
|
| 1792 |
|
| 1793 | ret = sdw_write_no_pm(slave, SDW_SCP_INT1, clear);
|
| 1794 | if (ret < 0) {
|
| 1795 | dev_err(&slave->dev,
|
| 1796 | "SDW_SCP_INT1 write failed:%d\n", ret);
|
| 1797 | goto io_err;
|
| 1798 | }
|
| 1799 |
|
| 1800 |
|
| 1801 | slave->first_interrupt_done = true;
|
| 1802 |
|
| 1803 | |
| 1804 | * Read status again to ensure no new interrupts arrived
|
| 1805 | * while servicing interrupts.
|
| 1806 |
|
| 1807 | ret = sdw_read_no_pm(slave, SDW_SCP_INT1);
|
| 1808 | if (ret < 0) {
|
| 1809 | dev_err(&slave->dev,
|
| 1810 | "SDW_SCP_INT1 recheck read failed:%d\n", ret);
|
| 1811 | goto io_err;
|
| 1812 | }
|
| 1813 | buf = ret;
|
| 1814 |
|
| 1815 | ret = sdw_nread_no_pm(slave, SDW_SCP_INTSTAT2, 2, buf2);
|
| 1816 | if (ret < 0) {
|
| 1817 | dev_err(&slave->dev,
|
| 1818 | "SDW_SCP_INT2/3 recheck read failed:%d\n", ret);
|
| 1819 | goto io_err;
|
| 1820 | }
|
| 1821 |
|
| 1822 | if (slave->id.class_id) {
|
| 1823 | ret = sdw_read_no_pm(slave, SDW_DP0_INT);
|
| 1824 | if (ret < 0) {
|
| 1825 | dev_err(&slave->dev,
|
| 1826 | "SDW_DP0_INT recheck read failed:%d\n", ret);
|
| 1827 | goto io_err;
|
| 1828 | }
|
| 1829 | sdca_cascade = ret & SDW_DP0_SDCA_CASCADE;
|
| 1830 | }
|
| 1831 |
|
| 1832 | |
| 1833 | * Make sure no interrupts are pending
|
| 1834 |
|
| 1835 | stat = buf || buf2[0] || buf2[1] || sdca_cascade;
|
| 1836 |
|
| 1837 | |
| 1838 | * Exit loop if Slave is continuously in ALERT state even
|
| 1839 | * after servicing the interrupt multiple times.
|
| 1840 |
|
| 1841 | count++;
|
| 1842 |
|
| 1843 |
|
| 1844 | } while (stat != 0 && count < SDW_READ_INTR_CLEAR_RETRY);
|
| 1845 |
|
| 1846 | if (count == SDW_READ_INTR_CLEAR_RETRY)
|
| 1847 | dev_warn(&slave->dev, "Reached MAX_RETRY on alert read\n");
|
| 1848 |
|
| 1849 | io_err:
|
| 1850 | pm_runtime_mark_last_busy(&slave->dev);
|
| 1851 | pm_runtime_put_autosuspend(&slave->dev);
|
| 1852 |
|
| 1853 | return ret;
|
| 1854 | }
|
| 1855 |
|
| 1856 | static int sdw_update_slave_status(struct sdw_slave *slave,
|
| 1857 | enum sdw_slave_status status)
|
| 1858 | {
|
| 1859 | int ret = 0;
|
| 1860 |
|
| 1861 | mutex_lock(&slave->sdw_dev_lock);
|
| 1862 |
|
| 1863 | if (slave->probed) {
|
| 1864 | struct device *dev = &slave->dev;
|
| 1865 | struct sdw_driver *drv = drv_to_sdw_driver(dev->driver);
|
| 1866 |
|
| 1867 | if (drv->ops && drv->ops->update_status)
|
| 1868 | ret = drv->ops->update_status(slave, status);
|
| 1869 | }
|
| 1870 |
|
| 1871 | mutex_unlock(&slave->sdw_dev_lock);
|
| 1872 |
|
| 1873 | return ret;
|
| 1874 | }
|
| 1875 |
|
| 1876 | |
| 1877 | * sdw_handle_slave_status() - Handle Slave status
|
| 1878 | * @bus: SDW bus instance
|
| 1879 | * @status: Status for all Slave(s)
|
| 1880 |
|
| 1881 | int sdw_handle_slave_status(struct sdw_bus *bus,
|
| 1882 | enum sdw_slave_status status[])
|
| 1883 | {
|
| 1884 | enum sdw_slave_status prev_status;
|
| 1885 | struct sdw_slave *slave;
|
| 1886 | bool attached_initializing, id_programmed;
|
| 1887 | int i, ret = 0;
|
| 1888 |
|
| 1889 |
|
| 1890 | for (i = 1; i <= SDW_MAX_DEVICES; i++) {
|
| 1891 | mutex_lock(&bus->bus_lock);
|
| 1892 | if (test_bit(i, bus->assigned) == false) {
|
| 1893 | mutex_unlock(&bus->bus_lock);
|
| 1894 | continue;
|
| 1895 | }
|
| 1896 | mutex_unlock(&bus->bus_lock);
|
| 1897 |
|
| 1898 | slave = sdw_get_slave(bus, i);
|
| 1899 | if (!slave)
|
| 1900 | continue;
|
| 1901 |
|
| 1902 | if (status[i] == SDW_SLAVE_UNATTACHED &&
|
| 1903 | slave->status != SDW_SLAVE_UNATTACHED) {
|
| 1904 | dev_dbg(&slave->dev, "Slave %d state check1: UNATTACHED, status was %d\n",
|
| 1905 | i, slave->status);
|
| 1906 | sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
| 1907 |
|
| 1908 |
|
| 1909 | ret = sdw_update_slave_status(slave, status[i]);
|
| 1910 | if (ret < 0)
|
| 1911 | dev_warn(&slave->dev, "Update Slave status failed:%d\n", ret);
|
| 1912 | }
|
| 1913 | }
|
| 1914 |
|
| 1915 | if (status[0] == SDW_SLAVE_ATTACHED) {
|
| 1916 | dev_dbg(bus->dev, "Slave attached, programming device number\n");
|
| 1917 |
|
| 1918 | |
| 1919 | * Programming a device number will have side effects,
|
| 1920 | * so we deal with other devices at a later time.
|
| 1921 | * This relies on those devices reporting ATTACHED, which will
|
| 1922 | * trigger another call to this function. This will only
|
| 1923 | * happen if at least one device ID was programmed.
|
| 1924 | * Error returns from sdw_program_device_num() are currently
|
| 1925 | * ignored because there's no useful recovery that can be done.
|
| 1926 | * Returning the error here could result in the current status
|
| 1927 | * of other devices not being handled, because if no device IDs
|
| 1928 | * were programmed there's nothing to guarantee a status change
|
| 1929 | * to trigger another call to this function.
|
| 1930 |
|
| 1931 | sdw_program_device_num(bus, &id_programmed);
|
| 1932 | if (id_programmed)
|
| 1933 | return 0;
|
| 1934 | }
|
| 1935 |
|
| 1936 |
|
| 1937 | for (i = 1; i <= SDW_MAX_DEVICES; i++) {
|
| 1938 | mutex_lock(&bus->bus_lock);
|
| 1939 | if (test_bit(i, bus->assigned) == false) {
|
| 1940 | mutex_unlock(&bus->bus_lock);
|
| 1941 | continue;
|
| 1942 | }
|
| 1943 | mutex_unlock(&bus->bus_lock);
|
| 1944 |
|
| 1945 | slave = sdw_get_slave(bus, i);
|
| 1946 | if (!slave)
|
| 1947 | continue;
|
| 1948 |
|
| 1949 | attached_initializing = false;
|
| 1950 |
|
| 1951 | switch (status[i]) {
|
| 1952 | case SDW_SLAVE_UNATTACHED:
|
| 1953 | if (slave->status == SDW_SLAVE_UNATTACHED)
|
| 1954 | break;
|
| 1955 |
|
| 1956 | dev_dbg(&slave->dev, "Slave %d state check2: UNATTACHED, status was %d\n",
|
| 1957 | i, slave->status);
|
| 1958 |
|
| 1959 | sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
| 1960 | break;
|
| 1961 |
|
| 1962 | case SDW_SLAVE_ALERT:
|
| 1963 | if (slave->status != SDW_SLAVE_ATTACHED &&
|
| 1964 | slave->status != SDW_SLAVE_ALERT)
|
| 1965 | continue;
|
| 1966 |
|
| 1967 | ret = sdw_handle_slave_alerts(slave);
|
| 1968 | if (ret < 0)
|
| 1969 | dev_err(&slave->dev,
|
| 1970 | "Slave %d alert handling failed: %d\n",
|
| 1971 | i, ret);
|
| 1972 | break;
|
| 1973 |
|
| 1974 | case SDW_SLAVE_ATTACHED:
|
| 1975 | if (slave->status == SDW_SLAVE_ATTACHED)
|
| 1976 | break;
|
| 1977 |
|
| 1978 | prev_status = slave->status;
|
| 1979 | sdw_modify_slave_status(slave, SDW_SLAVE_ATTACHED);
|
| 1980 |
|
| 1981 | if (prev_status == SDW_SLAVE_ALERT)
|
| 1982 | break;
|
| 1983 |
|
| 1984 | attached_initializing = true;
|
| 1985 |
|
| 1986 | ret = sdw_initialize_slave(slave);
|
| 1987 | if (ret < 0)
|
| 1988 | dev_err(&slave->dev,
|
| 1989 | "Slave %d initialization failed: %d\n",
|
| 1990 | i, ret);
|
| 1991 |
|
| 1992 | break;
|
| 1993 |
|
| 1994 | default:
|
| 1995 | dev_err(&slave->dev, "Invalid slave %d status:%d\n",
|
| 1996 | i, status[i]);
|
| 1997 | break;
|
| 1998 | }
|
| 1999 |
|
| 2000 | ret = sdw_update_slave_status(slave, status[i]);
|
| 2001 | if (ret < 0)
|
| 2002 | dev_err(&slave->dev,
|
| 2003 | "Update Slave status failed:%d\n", ret);
|
| 2004 | if (attached_initializing) {
|
| 2005 | dev_dbg(&slave->dev,
|
| 2006 | "signaling initialization completion for Slave %d\n",
|
| 2007 | slave->dev_num);
|
| 2008 |
|
| 2009 | complete_all(&slave->initialization_complete);
|
| 2010 |
|
| 2011 | |
| 2012 | * If the manager became pm_runtime active, the peripherals will be
|
| 2013 | * restarted and attach, but their pm_runtime status may remain
|
| 2014 | * suspended. If the 'update_slave_status' callback initiates
|
| 2015 | * any sort of deferred processing, this processing would not be
|
| 2016 | * cancelled on pm_runtime suspend.
|
| 2017 | * To avoid such zombie states, we queue a request to resume.
|
| 2018 | * This would be a no-op in case the peripheral was being resumed
|
| 2019 | * by e.g. the ALSA/ASoC framework.
|
| 2020 |
|
| 2021 | pm_request_resume(&slave->dev);
|
| 2022 | }
|
| 2023 | }
|
| 2024 |
|
| 2025 | return ret;
|
| 2026 | }
|
| 2027 | EXPORT_SYMBOL(sdw_handle_slave_status);
|
| 2028 |
|
| 2029 | void sdw_clear_slave_status(struct sdw_bus *bus, u32 request)
|
| 2030 | {
|
| 2031 | struct sdw_slave *slave;
|
| 2032 | int i;
|
| 2033 |
|
| 2034 |
|
| 2035 | for (i = 1; i <= SDW_MAX_DEVICES; i++) {
|
| 2036 | mutex_lock(&bus->bus_lock);
|
| 2037 | if (test_bit(i, bus->assigned) == false) {
|
| 2038 | mutex_unlock(&bus->bus_lock);
|
| 2039 | continue;
|
| 2040 | }
|
| 2041 | mutex_unlock(&bus->bus_lock);
|
| 2042 |
|
| 2043 | slave = sdw_get_slave(bus, i);
|
| 2044 | if (!slave)
|
| 2045 | continue;
|
| 2046 |
|
| 2047 | if (slave->status != SDW_SLAVE_UNATTACHED) {
|
| 2048 | sdw_modify_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
| 2049 | slave->first_interrupt_done = false;
|
| 2050 | sdw_update_slave_status(slave, SDW_SLAVE_UNATTACHED);
|
| 2051 | }
|
| 2052 |
|
| 2053 |
|
| 2054 | slave->unattach_request = request;
|
| 2055 | }
|
| 2056 | }
|
| 2057 | EXPORT_SYMBOL(sdw_clear_slave_status);
|
| 2058 |
|
| 2059 | int sdw_bpt_send_async(struct sdw_bus *bus, struct sdw_slave *slave, struct sdw_bpt_msg *msg)
|
| 2060 | {
|
| 2061 | int len = 0;
|
| 2062 | int i;
|
| 2063 |
|
| 2064 | for (i = 0; i < msg->sections; i++)
|
| 2065 | len += msg->sec[i].len;
|
| 2066 |
|
| 2067 | if (len > SDW_BPT_MSG_MAX_BYTES) {
|
| 2068 | dev_err(bus->dev, "Invalid BPT message length %d\n", len);
|
| 2069 | return -EINVAL;
|
| 2070 | }
|
| 2071 |
|
| 2072 |
|
| 2073 | if (slave->dev_num == SDW_ENUM_DEV_NUM ||
|
| 2074 | slave->dev_num > SDW_MAX_DEVICES) {
|
| 2075 | dev_err(&slave->dev, "Invalid device number %d\n", slave->dev_num);
|
| 2076 | return -ENODEV;
|
| 2077 | }
|
| 2078 |
|
| 2079 |
|
| 2080 | if (!bus->ops->bpt_send_async ||
|
| 2081 | !bus->ops->bpt_wait) {
|
| 2082 | dev_err(bus->dev, "BPT callbacks not defined\n");
|
| 2083 | return -EOPNOTSUPP;
|
| 2084 | }
|
| 2085 |
|
| 2086 | return bus->ops->bpt_send_async(bus, slave, msg);
|
| 2087 | }
|
| 2088 | EXPORT_SYMBOL(sdw_bpt_send_async);
|
| 2089 |
|
| 2090 | int sdw_bpt_wait(struct sdw_bus *bus, struct sdw_slave *slave, struct sdw_bpt_msg *msg)
|
| 2091 | {
|
| 2092 | return bus->ops->bpt_wait(bus, slave, msg);
|
| 2093 | }
|
| 2094 | EXPORT_SYMBOL(sdw_bpt_wait);
|
| 2095 |
|
| 2096 | int sdw_bpt_send_sync(struct sdw_bus *bus, struct sdw_slave *slave, struct sdw_bpt_msg *msg)
|
| 2097 | {
|
| 2098 | int ret;
|
| 2099 |
|
| 2100 | ret = sdw_bpt_send_async(bus, slave, msg);
|
| 2101 | if (ret < 0)
|
| 2102 | return ret;
|
| 2103 |
|
| 2104 | return sdw_bpt_wait(bus, slave, msg);
|
| 2105 | }
|
| 2106 | EXPORT_SYMBOL(sdw_bpt_send_sync);
|