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