linux/drivers/spmi/spmi.c
<<
>>
Prefs
   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        sdrv->remove(to_spmi_device(dev));
 352        pm_runtime_put_noidle(dev);
 353
 354        pm_runtime_disable(dev);
 355        pm_runtime_set_suspended(dev);
 356        pm_runtime_put_noidle(dev);
 357        return 0;
 358}
 359
 360static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
 361{
 362        int ret;
 363
 364        ret = of_device_uevent_modalias(dev, env);
 365        if (ret != -ENODEV)
 366                return ret;
 367
 368        return 0;
 369}
 370
 371static struct bus_type spmi_bus_type = {
 372        .name           = "spmi",
 373        .match          = spmi_device_match,
 374        .probe          = spmi_drv_probe,
 375        .remove         = spmi_drv_remove,
 376        .uevent         = spmi_drv_uevent,
 377};
 378
 379/**
 380 * spmi_controller_alloc() - Allocate a new SPMI device
 381 * @ctrl:       associated controller
 382 *
 383 * Caller is responsible for either calling spmi_device_add() to add the
 384 * newly allocated controller, or calling spmi_device_put() to discard it.
 385 */
 386struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
 387{
 388        struct spmi_device *sdev;
 389
 390        sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
 391        if (!sdev)
 392                return NULL;
 393
 394        sdev->ctrl = ctrl;
 395        device_initialize(&sdev->dev);
 396        sdev->dev.parent = &ctrl->dev;
 397        sdev->dev.bus = &spmi_bus_type;
 398        sdev->dev.type = &spmi_dev_type;
 399        return sdev;
 400}
 401EXPORT_SYMBOL_GPL(spmi_device_alloc);
 402
 403/**
 404 * spmi_controller_alloc() - Allocate a new SPMI controller
 405 * @parent:     parent device
 406 * @size:       size of private data
 407 *
 408 * Caller is responsible for either calling spmi_controller_add() to add the
 409 * newly allocated controller, or calling spmi_controller_put() to discard it.
 410 * The allocated private data region may be accessed via
 411 * spmi_controller_get_drvdata()
 412 */
 413struct spmi_controller *spmi_controller_alloc(struct device *parent,
 414                                              size_t size)
 415{
 416        struct spmi_controller *ctrl;
 417        int id;
 418
 419        if (WARN_ON(!parent))
 420                return NULL;
 421
 422        ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
 423        if (!ctrl)
 424                return NULL;
 425
 426        device_initialize(&ctrl->dev);
 427        ctrl->dev.type = &spmi_ctrl_type;
 428        ctrl->dev.bus = &spmi_bus_type;
 429        ctrl->dev.parent = parent;
 430        ctrl->dev.of_node = parent->of_node;
 431        spmi_controller_set_drvdata(ctrl, &ctrl[1]);
 432
 433        id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
 434        if (id < 0) {
 435                dev_err(parent,
 436                        "unable to allocate SPMI controller identifier.\n");
 437                spmi_controller_put(ctrl);
 438                return NULL;
 439        }
 440
 441        ctrl->nr = id;
 442        dev_set_name(&ctrl->dev, "spmi-%d", id);
 443
 444        dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
 445        return ctrl;
 446}
 447EXPORT_SYMBOL_GPL(spmi_controller_alloc);
 448
 449static void of_spmi_register_devices(struct spmi_controller *ctrl)
 450{
 451        struct device_node *node;
 452        int err;
 453
 454        if (!ctrl->dev.of_node)
 455                return;
 456
 457        for_each_available_child_of_node(ctrl->dev.of_node, node) {
 458                struct spmi_device *sdev;
 459                u32 reg[2];
 460
 461                dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
 462
 463                err = of_property_read_u32_array(node, "reg", reg, 2);
 464                if (err) {
 465                        dev_err(&ctrl->dev,
 466                                "node %pOF err (%d) does not have 'reg' property\n",
 467                                node, err);
 468                        continue;
 469                }
 470
 471                if (reg[1] != SPMI_USID) {
 472                        dev_err(&ctrl->dev,
 473                                "node %pOF contains unsupported 'reg' entry\n",
 474                                node);
 475                        continue;
 476                }
 477
 478                if (reg[0] >= SPMI_MAX_SLAVE_ID) {
 479                        dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
 480                        continue;
 481                }
 482
 483                dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
 484
 485                sdev = spmi_device_alloc(ctrl);
 486                if (!sdev)
 487                        continue;
 488
 489                sdev->dev.of_node = node;
 490                sdev->usid = (u8) reg[0];
 491
 492                err = spmi_device_add(sdev);
 493                if (err) {
 494                        dev_err(&sdev->dev,
 495                                "failure adding device. status %d\n", err);
 496                        spmi_device_put(sdev);
 497                }
 498        }
 499}
 500
 501/**
 502 * spmi_controller_add() - Add an SPMI controller
 503 * @ctrl:       controller to be registered.
 504 *
 505 * Register a controller previously allocated via spmi_controller_alloc() with
 506 * the SPMI core.
 507 */
 508int spmi_controller_add(struct spmi_controller *ctrl)
 509{
 510        int ret;
 511
 512        /* Can't register until after driver model init */
 513        if (WARN_ON(!is_registered))
 514                return -EAGAIN;
 515
 516        ret = device_add(&ctrl->dev);
 517        if (ret)
 518                return ret;
 519
 520        if (IS_ENABLED(CONFIG_OF))
 521                of_spmi_register_devices(ctrl);
 522
 523        dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
 524                ctrl->nr, &ctrl->dev);
 525
 526        return 0;
 527};
 528EXPORT_SYMBOL_GPL(spmi_controller_add);
 529
 530/* Remove a device associated with a controller */
 531static int spmi_ctrl_remove_device(struct device *dev, void *data)
 532{
 533        struct spmi_device *spmidev = to_spmi_device(dev);
 534        if (dev->type == &spmi_dev_type)
 535                spmi_device_remove(spmidev);
 536        return 0;
 537}
 538
 539/**
 540 * spmi_controller_remove(): remove an SPMI controller
 541 * @ctrl:       controller to remove
 542 *
 543 * Remove a SPMI controller.  Caller is responsible for calling
 544 * spmi_controller_put() to discard the allocated controller.
 545 */
 546void spmi_controller_remove(struct spmi_controller *ctrl)
 547{
 548        int dummy;
 549
 550        if (!ctrl)
 551                return;
 552
 553        dummy = device_for_each_child(&ctrl->dev, NULL,
 554                                      spmi_ctrl_remove_device);
 555        device_del(&ctrl->dev);
 556}
 557EXPORT_SYMBOL_GPL(spmi_controller_remove);
 558
 559/**
 560 * spmi_driver_register() - Register client driver with SPMI core
 561 * @sdrv:       client driver to be associated with client-device.
 562 *
 563 * This API will register the client driver with the SPMI framework.
 564 * It is typically called from the driver's module-init function.
 565 */
 566int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
 567{
 568        sdrv->driver.bus = &spmi_bus_type;
 569        sdrv->driver.owner = owner;
 570        return driver_register(&sdrv->driver);
 571}
 572EXPORT_SYMBOL_GPL(__spmi_driver_register);
 573
 574static void __exit spmi_exit(void)
 575{
 576        bus_unregister(&spmi_bus_type);
 577}
 578module_exit(spmi_exit);
 579
 580static int __init spmi_init(void)
 581{
 582        int ret;
 583
 584        ret = bus_register(&spmi_bus_type);
 585        if (ret)
 586                return ret;
 587
 588        is_registered = true;
 589        return 0;
 590}
 591postcore_initcall(spmi_init);
 592
 593MODULE_LICENSE("GPL v2");
 594MODULE_DESCRIPTION("SPMI module");
 595MODULE_ALIAS("platform:spmi");
 596