Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved.
0004  */
0005 #include <linux/kernel.h>
0006 #include <linux/errno.h>
0007 #include <linux/idr.h>
0008 #include <linux/slab.h>
0009 #include <linux/module.h>
0010 #include <linux/of.h>
0011 #include <linux/of_device.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/spmi.h>
0014 #include <linux/pm_runtime.h>
0015 
0016 #include <dt-bindings/spmi/spmi.h>
0017 #define CREATE_TRACE_POINTS
0018 #include <trace/events/spmi.h>
0019 
0020 static bool is_registered;
0021 static DEFINE_IDA(ctrl_ida);
0022 
0023 static void spmi_dev_release(struct device *dev)
0024 {
0025     struct spmi_device *sdev = to_spmi_device(dev);
0026 
0027     kfree(sdev);
0028 }
0029 
0030 static const struct device_type spmi_dev_type = {
0031     .release    = spmi_dev_release,
0032 };
0033 
0034 static void spmi_ctrl_release(struct device *dev)
0035 {
0036     struct spmi_controller *ctrl = to_spmi_controller(dev);
0037 
0038     ida_simple_remove(&ctrl_ida, ctrl->nr);
0039     kfree(ctrl);
0040 }
0041 
0042 static const struct device_type spmi_ctrl_type = {
0043     .release    = spmi_ctrl_release,
0044 };
0045 
0046 static int spmi_device_match(struct device *dev, struct device_driver *drv)
0047 {
0048     if (of_driver_match_device(dev, drv))
0049         return 1;
0050 
0051     if (drv->name)
0052         return strncmp(dev_name(dev), drv->name,
0053                    SPMI_NAME_SIZE) == 0;
0054 
0055     return 0;
0056 }
0057 
0058 /**
0059  * spmi_device_add() - add a device previously constructed via spmi_device_alloc()
0060  * @sdev:   spmi_device to be added
0061  */
0062 int spmi_device_add(struct spmi_device *sdev)
0063 {
0064     struct spmi_controller *ctrl = sdev->ctrl;
0065     int err;
0066 
0067     dev_set_name(&sdev->dev, "%d-%02x", ctrl->nr, sdev->usid);
0068 
0069     err = device_add(&sdev->dev);
0070     if (err < 0) {
0071         dev_err(&sdev->dev, "Can't add %s, status %d\n",
0072             dev_name(&sdev->dev), err);
0073         goto err_device_add;
0074     }
0075 
0076     dev_dbg(&sdev->dev, "device %s registered\n", dev_name(&sdev->dev));
0077 
0078 err_device_add:
0079     return err;
0080 }
0081 EXPORT_SYMBOL_GPL(spmi_device_add);
0082 
0083 /**
0084  * spmi_device_remove(): remove an SPMI device
0085  * @sdev:   spmi_device to be removed
0086  */
0087 void spmi_device_remove(struct spmi_device *sdev)
0088 {
0089     device_unregister(&sdev->dev);
0090 }
0091 EXPORT_SYMBOL_GPL(spmi_device_remove);
0092 
0093 static inline int
0094 spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid)
0095 {
0096     int ret;
0097 
0098     if (!ctrl || !ctrl->cmd || ctrl->dev.type != &spmi_ctrl_type)
0099         return -EINVAL;
0100 
0101     ret = ctrl->cmd(ctrl, opcode, sid);
0102     trace_spmi_cmd(opcode, sid, ret);
0103     return ret;
0104 }
0105 
0106 static inline int spmi_read_cmd(struct spmi_controller *ctrl, u8 opcode,
0107                 u8 sid, u16 addr, u8 *buf, size_t len)
0108 {
0109     int ret;
0110 
0111     if (!ctrl || !ctrl->read_cmd || ctrl->dev.type != &spmi_ctrl_type)
0112         return -EINVAL;
0113 
0114     trace_spmi_read_begin(opcode, sid, addr);
0115     ret = ctrl->read_cmd(ctrl, opcode, sid, addr, buf, len);
0116     trace_spmi_read_end(opcode, sid, addr, ret, len, buf);
0117     return ret;
0118 }
0119 
0120 static inline int spmi_write_cmd(struct spmi_controller *ctrl, u8 opcode,
0121                  u8 sid, u16 addr, const u8 *buf, size_t len)
0122 {
0123     int ret;
0124 
0125     if (!ctrl || !ctrl->write_cmd || ctrl->dev.type != &spmi_ctrl_type)
0126         return -EINVAL;
0127 
0128     trace_spmi_write_begin(opcode, sid, addr, len, buf);
0129     ret = ctrl->write_cmd(ctrl, opcode, sid, addr, buf, len);
0130     trace_spmi_write_end(opcode, sid, addr, ret);
0131     return ret;
0132 }
0133 
0134 /**
0135  * spmi_register_read() - register read
0136  * @sdev:   SPMI device.
0137  * @addr:   slave register address (5-bit address).
0138  * @buf:    buffer to be populated with data from the Slave.
0139  *
0140  * Reads 1 byte of data from a Slave device register.
0141  */
0142 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf)
0143 {
0144     /* 5-bit register address */
0145     if (addr > 0x1F)
0146         return -EINVAL;
0147 
0148     return spmi_read_cmd(sdev->ctrl, SPMI_CMD_READ, sdev->usid, addr,
0149                  buf, 1);
0150 }
0151 EXPORT_SYMBOL_GPL(spmi_register_read);
0152 
0153 /**
0154  * spmi_ext_register_read() - extended register read
0155  * @sdev:   SPMI device.
0156  * @addr:   slave register address (8-bit address).
0157  * @buf:    buffer to be populated with data from the Slave.
0158  * @len:    the request number of bytes to read (up to 16 bytes).
0159  *
0160  * Reads up to 16 bytes of data from the extended register space on a
0161  * Slave device.
0162  */
0163 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
0164                size_t len)
0165 {
0166     /* 8-bit register address, up to 16 bytes */
0167     if (len == 0 || len > 16)
0168         return -EINVAL;
0169 
0170     return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READ, sdev->usid, addr,
0171                  buf, len);
0172 }
0173 EXPORT_SYMBOL_GPL(spmi_ext_register_read);
0174 
0175 /**
0176  * spmi_ext_register_readl() - extended register read long
0177  * @sdev:   SPMI device.
0178  * @addr:   slave register address (16-bit address).
0179  * @buf:    buffer to be populated with data from the Slave.
0180  * @len:    the request number of bytes to read (up to 8 bytes).
0181  *
0182  * Reads up to 8 bytes of data from the extended register space on a
0183  * Slave device using 16-bit address.
0184  */
0185 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
0186                 size_t len)
0187 {
0188     /* 16-bit register address, up to 8 bytes */
0189     if (len == 0 || len > 8)
0190         return -EINVAL;
0191 
0192     return spmi_read_cmd(sdev->ctrl, SPMI_CMD_EXT_READL, sdev->usid, addr,
0193                  buf, len);
0194 }
0195 EXPORT_SYMBOL_GPL(spmi_ext_register_readl);
0196 
0197 /**
0198  * spmi_register_write() - register write
0199  * @sdev:   SPMI device
0200  * @addr:   slave register address (5-bit address).
0201  * @data:   buffer containing the data to be transferred to the Slave.
0202  *
0203  * Writes 1 byte of data to a Slave device register.
0204  */
0205 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data)
0206 {
0207     /* 5-bit register address */
0208     if (addr > 0x1F)
0209         return -EINVAL;
0210 
0211     return spmi_write_cmd(sdev->ctrl, SPMI_CMD_WRITE, sdev->usid, addr,
0212                   &data, 1);
0213 }
0214 EXPORT_SYMBOL_GPL(spmi_register_write);
0215 
0216 /**
0217  * spmi_register_zero_write() - register zero write
0218  * @sdev:   SPMI device.
0219  * @data:   the data to be written to register 0 (7-bits).
0220  *
0221  * Writes data to register 0 of the Slave device.
0222  */
0223 int spmi_register_zero_write(struct spmi_device *sdev, u8 data)
0224 {
0225     return spmi_write_cmd(sdev->ctrl, SPMI_CMD_ZERO_WRITE, sdev->usid, 0,
0226                   &data, 1);
0227 }
0228 EXPORT_SYMBOL_GPL(spmi_register_zero_write);
0229 
0230 /**
0231  * spmi_ext_register_write() - extended register write
0232  * @sdev:   SPMI device.
0233  * @addr:   slave register address (8-bit address).
0234  * @buf:    buffer containing the data to be transferred to the Slave.
0235  * @len:    the request number of bytes to read (up to 16 bytes).
0236  *
0237  * Writes up to 16 bytes of data to the extended register space of a
0238  * Slave device.
0239  */
0240 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, const u8 *buf,
0241                 size_t len)
0242 {
0243     /* 8-bit register address, up to 16 bytes */
0244     if (len == 0 || len > 16)
0245         return -EINVAL;
0246 
0247     return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITE, sdev->usid, addr,
0248                   buf, len);
0249 }
0250 EXPORT_SYMBOL_GPL(spmi_ext_register_write);
0251 
0252 /**
0253  * spmi_ext_register_writel() - extended register write long
0254  * @sdev:   SPMI device.
0255  * @addr:   slave register address (16-bit address).
0256  * @buf:    buffer containing the data to be transferred to the Slave.
0257  * @len:    the request number of bytes to read (up to 8 bytes).
0258  *
0259  * Writes up to 8 bytes of data to the extended register space of a
0260  * Slave device using 16-bit address.
0261  */
0262 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, const u8 *buf,
0263                  size_t len)
0264 {
0265     /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */
0266     if (len == 0 || len > 8)
0267         return -EINVAL;
0268 
0269     return spmi_write_cmd(sdev->ctrl, SPMI_CMD_EXT_WRITEL, sdev->usid,
0270                   addr, buf, len);
0271 }
0272 EXPORT_SYMBOL_GPL(spmi_ext_register_writel);
0273 
0274 /**
0275  * spmi_command_reset() - sends RESET command to the specified slave
0276  * @sdev:   SPMI device.
0277  *
0278  * The Reset command initializes the Slave and forces all registers to
0279  * their reset values. The Slave shall enter the STARTUP state after
0280  * receiving a Reset command.
0281  */
0282 int spmi_command_reset(struct spmi_device *sdev)
0283 {
0284     return spmi_cmd(sdev->ctrl, SPMI_CMD_RESET, sdev->usid);
0285 }
0286 EXPORT_SYMBOL_GPL(spmi_command_reset);
0287 
0288 /**
0289  * spmi_command_sleep() - sends SLEEP command to the specified SPMI device
0290  * @sdev:   SPMI device.
0291  *
0292  * The Sleep command causes the Slave to enter the user defined SLEEP state.
0293  */
0294 int spmi_command_sleep(struct spmi_device *sdev)
0295 {
0296     return spmi_cmd(sdev->ctrl, SPMI_CMD_SLEEP, sdev->usid);
0297 }
0298 EXPORT_SYMBOL_GPL(spmi_command_sleep);
0299 
0300 /**
0301  * spmi_command_wakeup() - sends WAKEUP command to the specified SPMI device
0302  * @sdev:   SPMI device.
0303  *
0304  * The Wakeup command causes the Slave to move from the SLEEP state to
0305  * the ACTIVE state.
0306  */
0307 int spmi_command_wakeup(struct spmi_device *sdev)
0308 {
0309     return spmi_cmd(sdev->ctrl, SPMI_CMD_WAKEUP, sdev->usid);
0310 }
0311 EXPORT_SYMBOL_GPL(spmi_command_wakeup);
0312 
0313 /**
0314  * spmi_command_shutdown() - sends SHUTDOWN command to the specified SPMI device
0315  * @sdev:   SPMI device.
0316  *
0317  * The Shutdown command causes the Slave to enter the SHUTDOWN state.
0318  */
0319 int spmi_command_shutdown(struct spmi_device *sdev)
0320 {
0321     return spmi_cmd(sdev->ctrl, SPMI_CMD_SHUTDOWN, sdev->usid);
0322 }
0323 EXPORT_SYMBOL_GPL(spmi_command_shutdown);
0324 
0325 static int spmi_drv_probe(struct device *dev)
0326 {
0327     const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
0328     struct spmi_device *sdev = to_spmi_device(dev);
0329     int err;
0330 
0331     pm_runtime_get_noresume(dev);
0332     pm_runtime_set_active(dev);
0333     pm_runtime_enable(dev);
0334 
0335     err = sdrv->probe(sdev);
0336     if (err)
0337         goto fail_probe;
0338 
0339     return 0;
0340 
0341 fail_probe:
0342     pm_runtime_disable(dev);
0343     pm_runtime_set_suspended(dev);
0344     pm_runtime_put_noidle(dev);
0345     return err;
0346 }
0347 
0348 static void spmi_drv_remove(struct device *dev)
0349 {
0350     const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
0351 
0352     pm_runtime_get_sync(dev);
0353     sdrv->remove(to_spmi_device(dev));
0354     pm_runtime_put_noidle(dev);
0355 
0356     pm_runtime_disable(dev);
0357     pm_runtime_set_suspended(dev);
0358     pm_runtime_put_noidle(dev);
0359 }
0360 
0361 static void spmi_drv_shutdown(struct device *dev)
0362 {
0363     const struct spmi_driver *sdrv = to_spmi_driver(dev->driver);
0364 
0365     if (sdrv && sdrv->shutdown)
0366         sdrv->shutdown(to_spmi_device(dev));
0367 }
0368 
0369 static int spmi_drv_uevent(struct device *dev, struct kobj_uevent_env *env)
0370 {
0371     int ret;
0372 
0373     ret = of_device_uevent_modalias(dev, env);
0374     if (ret != -ENODEV)
0375         return ret;
0376 
0377     return 0;
0378 }
0379 
0380 static struct bus_type spmi_bus_type = {
0381     .name       = "spmi",
0382     .match      = spmi_device_match,
0383     .probe      = spmi_drv_probe,
0384     .remove     = spmi_drv_remove,
0385     .shutdown   = spmi_drv_shutdown,
0386     .uevent     = spmi_drv_uevent,
0387 };
0388 
0389 /**
0390  * spmi_device_from_of() - get the associated SPMI device from a device node
0391  *
0392  * @np:     device node
0393  *
0394  * Returns the struct spmi_device associated with a device node or NULL.
0395  */
0396 struct spmi_device *spmi_device_from_of(struct device_node *np)
0397 {
0398     struct device *dev = bus_find_device_by_of_node(&spmi_bus_type, np);
0399 
0400     if (dev)
0401         return to_spmi_device(dev);
0402     return NULL;
0403 }
0404 EXPORT_SYMBOL_GPL(spmi_device_from_of);
0405 
0406 /**
0407  * spmi_controller_alloc() - Allocate a new SPMI device
0408  * @ctrl:   associated controller
0409  *
0410  * Caller is responsible for either calling spmi_device_add() to add the
0411  * newly allocated controller, or calling spmi_device_put() to discard it.
0412  */
0413 struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl)
0414 {
0415     struct spmi_device *sdev;
0416 
0417     sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
0418     if (!sdev)
0419         return NULL;
0420 
0421     sdev->ctrl = ctrl;
0422     device_initialize(&sdev->dev);
0423     sdev->dev.parent = &ctrl->dev;
0424     sdev->dev.bus = &spmi_bus_type;
0425     sdev->dev.type = &spmi_dev_type;
0426     return sdev;
0427 }
0428 EXPORT_SYMBOL_GPL(spmi_device_alloc);
0429 
0430 /**
0431  * spmi_controller_alloc() - Allocate a new SPMI controller
0432  * @parent: parent device
0433  * @size:   size of private data
0434  *
0435  * Caller is responsible for either calling spmi_controller_add() to add the
0436  * newly allocated controller, or calling spmi_controller_put() to discard it.
0437  * The allocated private data region may be accessed via
0438  * spmi_controller_get_drvdata()
0439  */
0440 struct spmi_controller *spmi_controller_alloc(struct device *parent,
0441                           size_t size)
0442 {
0443     struct spmi_controller *ctrl;
0444     int id;
0445 
0446     if (WARN_ON(!parent))
0447         return NULL;
0448 
0449     ctrl = kzalloc(sizeof(*ctrl) + size, GFP_KERNEL);
0450     if (!ctrl)
0451         return NULL;
0452 
0453     device_initialize(&ctrl->dev);
0454     ctrl->dev.type = &spmi_ctrl_type;
0455     ctrl->dev.bus = &spmi_bus_type;
0456     ctrl->dev.parent = parent;
0457     ctrl->dev.of_node = parent->of_node;
0458     spmi_controller_set_drvdata(ctrl, &ctrl[1]);
0459 
0460     id = ida_simple_get(&ctrl_ida, 0, 0, GFP_KERNEL);
0461     if (id < 0) {
0462         dev_err(parent,
0463             "unable to allocate SPMI controller identifier.\n");
0464         spmi_controller_put(ctrl);
0465         return NULL;
0466     }
0467 
0468     ctrl->nr = id;
0469     dev_set_name(&ctrl->dev, "spmi-%d", id);
0470 
0471     dev_dbg(&ctrl->dev, "allocated controller 0x%p id %d\n", ctrl, id);
0472     return ctrl;
0473 }
0474 EXPORT_SYMBOL_GPL(spmi_controller_alloc);
0475 
0476 static void of_spmi_register_devices(struct spmi_controller *ctrl)
0477 {
0478     struct device_node *node;
0479     int err;
0480 
0481     if (!ctrl->dev.of_node)
0482         return;
0483 
0484     for_each_available_child_of_node(ctrl->dev.of_node, node) {
0485         struct spmi_device *sdev;
0486         u32 reg[2];
0487 
0488         dev_dbg(&ctrl->dev, "adding child %pOF\n", node);
0489 
0490         err = of_property_read_u32_array(node, "reg", reg, 2);
0491         if (err) {
0492             dev_err(&ctrl->dev,
0493                 "node %pOF err (%d) does not have 'reg' property\n",
0494                 node, err);
0495             continue;
0496         }
0497 
0498         if (reg[1] != SPMI_USID) {
0499             dev_err(&ctrl->dev,
0500                 "node %pOF contains unsupported 'reg' entry\n",
0501                 node);
0502             continue;
0503         }
0504 
0505         if (reg[0] >= SPMI_MAX_SLAVE_ID) {
0506             dev_err(&ctrl->dev, "invalid usid on node %pOF\n", node);
0507             continue;
0508         }
0509 
0510         dev_dbg(&ctrl->dev, "read usid %02x\n", reg[0]);
0511 
0512         sdev = spmi_device_alloc(ctrl);
0513         if (!sdev)
0514             continue;
0515 
0516         sdev->dev.of_node = node;
0517         sdev->usid = (u8)reg[0];
0518 
0519         err = spmi_device_add(sdev);
0520         if (err) {
0521             dev_err(&sdev->dev,
0522                 "failure adding device. status %d\n", err);
0523             spmi_device_put(sdev);
0524         }
0525     }
0526 }
0527 
0528 /**
0529  * spmi_controller_add() - Add an SPMI controller
0530  * @ctrl:   controller to be registered.
0531  *
0532  * Register a controller previously allocated via spmi_controller_alloc() with
0533  * the SPMI core.
0534  */
0535 int spmi_controller_add(struct spmi_controller *ctrl)
0536 {
0537     int ret;
0538 
0539     /* Can't register until after driver model init */
0540     if (WARN_ON(!is_registered))
0541         return -EAGAIN;
0542 
0543     ret = device_add(&ctrl->dev);
0544     if (ret)
0545         return ret;
0546 
0547     if (IS_ENABLED(CONFIG_OF))
0548         of_spmi_register_devices(ctrl);
0549 
0550     dev_dbg(&ctrl->dev, "spmi-%d registered: dev:%p\n",
0551         ctrl->nr, &ctrl->dev);
0552 
0553     return 0;
0554 };
0555 EXPORT_SYMBOL_GPL(spmi_controller_add);
0556 
0557 /* Remove a device associated with a controller */
0558 static int spmi_ctrl_remove_device(struct device *dev, void *data)
0559 {
0560     struct spmi_device *spmidev = to_spmi_device(dev);
0561 
0562     if (dev->type == &spmi_dev_type)
0563         spmi_device_remove(spmidev);
0564     return 0;
0565 }
0566 
0567 /**
0568  * spmi_controller_remove(): remove an SPMI controller
0569  * @ctrl:   controller to remove
0570  *
0571  * Remove a SPMI controller.  Caller is responsible for calling
0572  * spmi_controller_put() to discard the allocated controller.
0573  */
0574 void spmi_controller_remove(struct spmi_controller *ctrl)
0575 {
0576     if (!ctrl)
0577         return;
0578 
0579     device_for_each_child(&ctrl->dev, NULL, spmi_ctrl_remove_device);
0580     device_del(&ctrl->dev);
0581 }
0582 EXPORT_SYMBOL_GPL(spmi_controller_remove);
0583 
0584 /**
0585  * spmi_driver_register() - Register client driver with SPMI core
0586  * @sdrv:   client driver to be associated with client-device.
0587  *
0588  * This API will register the client driver with the SPMI framework.
0589  * It is typically called from the driver's module-init function.
0590  */
0591 int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner)
0592 {
0593     sdrv->driver.bus = &spmi_bus_type;
0594     sdrv->driver.owner = owner;
0595     return driver_register(&sdrv->driver);
0596 }
0597 EXPORT_SYMBOL_GPL(__spmi_driver_register);
0598 
0599 static void __exit spmi_exit(void)
0600 {
0601     bus_unregister(&spmi_bus_type);
0602 }
0603 module_exit(spmi_exit);
0604 
0605 static int __init spmi_init(void)
0606 {
0607     int ret;
0608 
0609     ret = bus_register(&spmi_bus_type);
0610     if (ret)
0611         return ret;
0612 
0613     is_registered = true;
0614     return 0;
0615 }
0616 postcore_initcall(spmi_init);
0617 
0618 MODULE_LICENSE("GPL v2");
0619 MODULE_DESCRIPTION("SPMI module");
0620 MODULE_ALIAS("platform:spmi");