1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. 4 */ 5#include <linux/kernel.h> 6#include <linux/errno.h> 7#include <linux/idr.h> 8#include <linux/slab.h> 9#include <linux/module.h> 10#include <linux/of.h> 11#include <linux/of_device.h> 12#include <linux/platform_device.h> 13#include <linux/spmi.h> 14#include <linux/pm_runtime.h> 15 16#include <dt-bindings/spmi/spmi.h> 17#define CREATE_TRACE_POINTS 18#include <trace/events/spmi.h> 19 20static bool is_registered; 21static DEFINE_IDA(ctrl_ida); 22 23static void spmi_dev_release(struct device *dev) 24{ 25 struct spmi_device *sdev = to_spmi_device(dev); 26 kfree(sdev); 27} 28 29static const struct device_type spmi_dev_type = { 30 .release = spmi_dev_release, 31}; 32 33static void spmi_ctrl_release(struct device *dev) 34{ 35 struct spmi_controller *ctrl = to_spmi_controller(dev); 36 ida_simple_remove(&ctrl_ida, ctrl->nr); 37 kfree(ctrl); 38} 39 40static const struct device_type spmi_ctrl_type = { 41 .release = spmi_ctrl_release, 42}; 43 44static int spmi_device_match(struct device *dev, struct device_driver *drv) 45{ 46 if (of_driver_match_device(dev, drv)) 47 return 1; 48 49 if (drv->name) 50 return strncmp(dev_name(dev), drv->name, 51 SPMI_NAME_SIZE) == 0; 52 53 return 0; 54} 55 56/** 57 * spmi_device_add() - add a device previously constructed via spmi_device_alloc() 58 * @sdev: spmi_device to be added 59 */ 60int spmi_device_add(struct spmi_device *sdev) 61{ 62 struct spmi_controller *ctrl = sdev->ctrl; 63 int err; 64 65 dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid); 66 67 err = device_add(&sdev->dev); 68 if (err < 0) { 69 dev_err(&sdev->dev, "Can't add %s, status %d\n", 70 dev_name(&sdev->dev), err); 71 goto err_device_add; 72 } 73 74 dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev)); 75 76err_device_add: 77 return err; 78} 79EXPORT_SYMBOL_GPL(spmi_device_add); 80 81/** 82 * spmi_device_remove(): remove an SPMI device 83 * @sdev: spmi_device to be removed 84 */ 85void spmi_device_remove(struct spmi_device *sdev) 86{ 87 device_unregister(&sdev->dev); 88} 89EXPORT_SYMBOL_GPL(spmi_device_remove); 90 91static inline int 92spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid) 93{ 94 int ret; 95 96 if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type) 97 return -EINVAL; 98 99 ret = ctrl->cmd(ctrl, opcode, sid); 100 trace_spmi_cmd(opcode, sid, ret); 101 return ret; 102} 103 104static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode, 105 u8 sid, u16 addr, u8 *buf, size_t len) 106{ 107 int ret; 108 109 if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type) 110 return -EINVAL; 111 112 trace_spmi_read_begin(opcode, sid, addr); 113 ret = ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len); 114 trace_spmi_read_end(opcode, sid, addr, ret, len, buf); 115 return ret; 116} 117 118static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode, 119 u8 sid, u16 addr, const u8 *buf, size_t len) 120{ 121 int ret; 122 123 if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type) 124 return -EINVAL; 125 126 trace_spmi_write_begin(opcode, sid, addr, len, buf); 127 ret = ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len); 128 trace_spmi_write_end(opcode, sid, addr, ret); 129 return ret; 130} 131 132/** 133 * spmi_register_read() - register read 134 * @sdev: SPMI device. 135 * @addr: slave register address (5-bit address). 136 * @buf: buffer to be populated with data from the Slave. 137 * 138 * Reads 1 byte of data from a Slave device register. 139 */ 140int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf) 141{ 142 /* 5-bit register address */ 143 if (addr > 0x1F) 144 return -EINVAL; 145 146 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr, 147 buf, 1); 148} 149EXPORT_SYMBOL_GPL(spmi_register_read); 150 151/** 152 * spmi_ext_register_read() - extended register read 153 * @sdev: SPMI device. 154 * @addr: slave register address (8-bit address). 155 * @buf: buffer to be populated with data from the Slave. 156 * @len: the request number of bytes to read (up to 16 bytes). 157 * 158 * Reads up to 16 bytes of data from the extended register space on a 159 * Slave device. 160 */ 161int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf, 162 size_t len) 163{ 164 /* 8-bit register address, up to 16 bytes */ 165 if (len == 0 || len > 16) 166 return -EINVAL; 167 168 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr, 169 buf, len); 170} 171EXPORT_SYMBOL_GPL(spmi_ext_register_read); 172 173/** 174 * spmi_ext_register_readl() - extended register read long 175 * @sdev: SPMI device. 176 * @addr: slave register address (16-bit address). 177 * @buf: buffer to be populated with data from the Slave. 178 * @len: the request number of bytes to read (up to 8 bytes). 179 * 180 * Reads up to 8 bytes of data from the extended register space on a 181 * Slave device using 16-bit address. 182 */ 183int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf, 184 size_t len) 185{ 186 /* 16-bit register address, up to 8 bytes */ 187 if (len == 0 || len > 8) 188 return -EINVAL; 189 190 return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr, 191 buf, len); 192} 193EXPORT_SYMBOL_GPL(spmi_ext_register_readl); 194 195/** 196 * spmi_register_write() - register write 197 * @sdev: SPMI device 198 * @addr: slave register address (5-bit address). 199 * @data: buffer containing the data to be transferred to the Slave. 200 * 201 * Writes 1 byte of data to a Slave device register. 202 */ 203int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data) 204{ 205 /* 5-bit register address */ 206 if (addr > 0x1F) 207 return -EINVAL; 208 209 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr, 210 &data, 1); 211} 212EXPORT_SYMBOL_GPL(spmi_register_write); 213 214/** 215 * spmi_register_zero_write() - register zero write 216 * @sdev: SPMI device. 217 * @data: the data to be written to register 0 (7-bits). 218 * 219 * Writes data to register 0 of the Slave device. 220 */ 221int spmi_register_zero_write(struct spmi_device *sdev, u8 data) 222{ 223 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0, 224 &data, 1); 225} 226EXPORT_SYMBOL_GPL(spmi_register_zero_write); 227 228/** 229 * spmi_ext_register_write() - extended register write 230 * @sdev: SPMI device. 231 * @addr: slave register address (8-bit address). 232 * @buf: buffer containing the data to be transferred to the Slave. 233 * @len: the request number of bytes to read (up to 16 bytes). 234 * 235 * Writes up to 16 bytes of data to the extended register space of a 236 * Slave device. 237 */ 238int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf, 239 size_t len) 240{ 241 /* 8-bit register address, up to 16 bytes */ 242 if (len == 0 || len > 16) 243 return -EINVAL; 244 245 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr, 246 buf, len); 247} 248EXPORT_SYMBOL_GPL(spmi_ext_register_write); 249 250/** 251 * spmi_ext_register_writel() - extended register write long 252 * @sdev: SPMI device. 253 * @addr: slave register address (16-bit address). 254 * @buf: buffer containing the data to be transferred to the Slave. 255 * @len: the request number of bytes to read (up to 8 bytes). 256 * 257 * Writes up to 8 bytes of data to the extended register space of a 258 * Slave device using 16-bit address. 259 */ 260int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf, 261 size_t len) 262{ 263 /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */ 264 if (len == 0 || len > 8) 265 return -EINVAL; 266 267 return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid, 268 addr, buf, len); 269} 270EXPORT_SYMBOL_GPL(spmi_ext_register_writel); 271 272/** 273 * spmi_command_reset() - sends RESET command to the specified slave 274 * @sdev: SPMI device. 275 * 276 * The Reset command initializes the Slave and forces all registers to 277 * their reset values. The Slave shall enter the STARTUP state after 278 * receiving a Reset command. 279 */ 280int spmi_command_reset(struct spmi_device *sdev) 281{ 282 return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid); 283} 284EXPORT_SYMBOL_GPL(spmi_command_reset); 285 286/** 287 * spmi_command_sleep() - sends SLEEP command to the specified SPMI device 288 * @sdev: SPMI device. 289 * 290 * The Sleep command causes the Slave to enter the user defined SLEEP state. 291 */ 292int spmi_command_sleep(struct spmi_device *sdev) 293{ 294 return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid); 295} 296EXPORT_SYMBOL_GPL(spmi_command_sleep); 297 298/** 299 * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device 300 * @sdev: SPMI device. 301 * 302 * The Wakeup command causes the Slave to move from the SLEEP state to 303 * the ACTIVE state. 304 */ 305int spmi_command_wakeup(struct spmi_device *sdev) 306{ 307 return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid); 308} 309EXPORT_SYMBOL_GPL(spmi_command_wakeup); 310 311/** 312 * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device 313 * @sdev: SPMI device. 314 * 315 * The Shutdown command causes the Slave to enter the SHUTDOWN state. 316 */ 317int spmi_command_shutdown(struct spmi_device *sdev) 318{ 319 return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid); 320} 321EXPORT_SYMBOL_GPL(spmi_command_shutdown); 322 323static int spmi_drv_probe(struct device *dev) 324{ 325 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver); 326 struct spmi_device *sdev = to_spmi_device(dev); 327 int err; 328 329 pm_runtime_get_noresume(dev); 330 pm_runtime_set_active(dev); 331 pm_runtime_enable(dev); 332 333 err = sdrv->probe(sdev); 334 if (err) 335 goto fail_probe; 336 337 return 0; 338 339fail_probe: 340 pm_runtime_disable(dev); 341 pm_runtime_set_suspended(dev); 342 pm_runtime_put_noidle(dev); 343 return err; 344} 345 346static int spmi_drv_remove(struct device *dev) 347{ 348 const struct spmi_driver *sdrv = to_spmi_driver(dev->driver); 349 350 pm_runtime_get_sync(dev); 351 if (sdrv->remove) 352 sdrv->remove(to_spmi_device(dev)); 353 pm_runtime_put_noidle(dev); 354 355 pm_runtime_disable(dev); 356 pm_runtime_set_suspended(dev); 357 pm_runtime_put_noidle(dev); 358 return 0; 359} 360 361static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env) 362{ 363 int ret; 364 365 ret = of_device_uevent_modalias(dev, env); 366 if (ret != -ENODEV) 367 return ret; 368 369 return 0; 370} 371 372static struct bus_type spmi_bus_type = { 373 .name = "spmi", 374 .match = spmi_device_match, 375 .probe = spmi_drv_probe, 376 .remove = spmi_drv_remove, 377 .uevent = spmi_drv_uevent, 378}; 379 380/** 381 * spmi_controller_alloc() - Allocate a new SPMI device 382 * @ctrl: associated controller 383 * 384 * Caller is responsible for either calling spmi_device_add() to add the 385 * newly allocated controller, or calling spmi_device_put() to discard it. 386 */ 387struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl) 388{ 389 struct spmi_device *sdev; 390 391 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 392 if (!sdev) 393 return NULL; 394 395 sdev->ctrl = ctrl; 396 device_initialize(&sdev->dev); 397 sdev->dev.parent = &ctrl->dev; 398 sdev->dev.bus = &spmi_bus_type; 399 sdev->dev.type = &spmi_dev_type; 400 return sdev; 401} 402EXPORT_SYMBOL_GPL(spmi_device_alloc); 403 404/** 405 * spmi_controller_alloc() - Allocate a new SPMI controller 406 * @parent: parent device 407 * @size: size of private data 408 * 409 * Caller is responsible for either calling spmi_controller_add() to add the 410 * newly allocated controller, or calling spmi_controller_put() to discard it. 411 * The allocated private data region may be accessed via 412 * spmi_controller_get_drvdata() 413 */ 414struct spmi_controller *spmi_controller_alloc(struct device *parent, 415 size_t size) 416{ 417 struct spmi_controller *ctrl; 418 int id; 419 420 if (WARN_ON(!parent)) 421 return NULL; 422 423 ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL); 424 if (!ctrl) 425 return NULL; 426 427 device_initialize(&ctrl->dev); 428 ctrl->dev.type = &spmi_ctrl_type; 429 ctrl->dev.bus = &spmi_bus_type; 430 ctrl->dev.parent = parent; 431 ctrl->dev.of_node = parent->of_node; 432 spmi_controller_set_drvdata(ctrl, &ctrl[1]); 433 434 id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL); 435 if (id < 0) { 436 dev_err(parent, 437 "unable to allocate SPMI controller identifier.\n"); 438 spmi_controller_put(ctrl); 439 return NULL; 440 } 441 442 ctrl->nr = id; 443 dev_set_name(&ctrl->dev, "spmi-%d", id); 444 445 dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id); 446 return ctrl; 447} 448EXPORT_SYMBOL_GPL(spmi_controller_alloc); 449 450static void of_spmi_register_devices(struct spmi_controller *ctrl) 451{ 452 struct device_node *node; 453 int err; 454 455 if (!ctrl->dev.of_node) 456 return; 457 458 for_each_available_child_of_node(ctrl->dev.of_node, node) { 459 struct spmi_device *sdev; 460 u32 reg[2]; 461 462 dev_dbg(&ctrl->dev, "adding child %pOF\n", node); 463 464 err = of_property_read_u32_array(node, "reg", reg, 2); 465 if (err) { 466 dev_err(&ctrl->dev, 467 "node %pOF err (%d) does not have 'reg' property\n", 468 node, err); 469 continue; 470 } 471 472 if (reg[1] != SPMI_USID) { 473 dev_err(&ctrl->dev, 474 "node %pOF contains unsupported 'reg' entry\n", 475 node); 476 continue; 477 } 478 479 if (reg[0] >= SPMI_MAX_SLAVE_ID) { 480 dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node); 481 continue; 482 } 483 484 dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]); 485 486 sdev = spmi_device_alloc(ctrl); 487 if (!sdev) 488 continue; 489 490 sdev->dev.of_node = node; 491 sdev->usid = (u8) reg[0]; 492 493 err = spmi_device_add(sdev); 494 if (err) { 495 dev_err(&sdev->dev, 496 "failure adding device. status %d\n", err); 497 spmi_device_put(sdev); 498 } 499 } 500} 501 502/** 503 * spmi_controller_add() - Add an SPMI controller 504 * @ctrl: controller to be registered. 505 * 506 * Register a controller previously allocated via spmi_controller_alloc() with 507 * the SPMI core. 508 */ 509int spmi_controller_add(struct spmi_controller *ctrl) 510{ 511 int ret; 512 513 /* Can't register until after driver model init */ 514 if (WARN_ON(!is_registered)) 515 return -EAGAIN; 516 517 ret = device_add(&ctrl->dev); 518 if (ret) 519 return ret; 520 521 if (IS_ENABLED(CONFIG_OF)) 522 of_spmi_register_devices(ctrl); 523 524 dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n", 525 ctrl->nr, &ctrl->dev); 526 527 return 0; 528}; 529EXPORT_SYMBOL_GPL(spmi_controller_add); 530 531/* Remove a device associated with a controller */ 532static int spmi_ctrl_remove_device(struct device *dev, void *data) 533{ 534 struct spmi_device *spmidev = to_spmi_device(dev); 535 if (dev->type == &spmi_dev_type) 536 spmi_device_remove(spmidev); 537 return 0; 538} 539 540/** 541 * spmi_controller_remove(): remove an SPMI controller 542 * @ctrl: controller to remove 543 * 544 * Remove a SPMI controller. Caller is responsible for calling 545 * spmi_controller_put() to discard the allocated controller. 546 */ 547void spmi_controller_remove(struct spmi_controller *ctrl) 548{ 549 int dummy; 550 551 if (!ctrl) 552 return; 553 554 dummy = device_for_each_child(&ctrl->dev, NULL, 555 spmi_ctrl_remove_device); 556 device_del(&ctrl->dev); 557} 558EXPORT_SYMBOL_GPL(spmi_controller_remove); 559 560/** 561 * spmi_driver_register() - Register client driver with SPMI core 562 * @sdrv: client driver to be associated with client-device. 563 * 564 * This API will register the client driver with the SPMI framework. 565 * It is typically called from the driver's module-init function. 566 */ 567int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner) 568{ 569 sdrv->driver.bus = &spmi_bus_type; 570 sdrv->driver.owner = owner; 571 return driver_register(&sdrv->driver); 572} 573EXPORT_SYMBOL_GPL(__spmi_driver_register); 574 575static void __exit spmi_exit(void) 576{ 577 bus_unregister(&spmi_bus_type); 578} 579module_exit(spmi_exit); 580 581static int __init spmi_init(void) 582{ 583 int ret; 584 585 ret = bus_register(&spmi_bus_type); 586 if (ret) 587 return ret; 588 589 is_registered = true; 590 return 0; 591} 592postcore_initcall(spmi_init); 593 594MODULE_LICENSE("GPL v2"); 595MODULE_DESCRIPTION("SPMI module"); 596MODULE_ALIAS("platform:spmi"); 597