Kenneth Heitke | ee44ade | 2012-02-08 13:45:33 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/idr.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/of_device.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/spmi.h> |
| 22 | |
| 23 | struct spmii_boardinfo { |
| 24 | struct list_head list; |
| 25 | struct spmi_boardinfo board_info; |
| 26 | }; |
| 27 | |
| 28 | static DEFINE_MUTEX(board_lock); |
| 29 | static LIST_HEAD(board_list); |
| 30 | static LIST_HEAD(spmi_ctrl_list); |
| 31 | static DEFINE_IDR(ctrl_idr); |
| 32 | static struct device_type spmi_ctrl_type = { 0 }; |
| 33 | |
| 34 | #define to_spmi(dev) platform_get_drvdata(to_platform_device(dev)) |
| 35 | |
| 36 | /* Forward declarations */ |
| 37 | struct bus_type spmi_bus_type; |
| 38 | static int spmi_register_controller(struct spmi_controller *ctrl); |
| 39 | |
| 40 | /** |
| 41 | * spmi_busnum_to_ctrl: Map bus number to controller |
| 42 | * @busnum: bus number |
| 43 | * Returns controller representing this bus number |
| 44 | */ |
| 45 | struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num) |
| 46 | { |
| 47 | struct spmi_controller *ctrl; |
| 48 | |
| 49 | mutex_lock(&board_lock); |
| 50 | list_for_each_entry(ctrl, &spmi_ctrl_list, list) { |
| 51 | if (bus_num == ctrl->nr) { |
| 52 | mutex_unlock(&board_lock); |
| 53 | return ctrl; |
| 54 | } |
| 55 | } |
| 56 | mutex_unlock(&board_lock); |
| 57 | return NULL; |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(spmi_busnum_to_ctrl); |
| 60 | |
| 61 | /** |
| 62 | * spmi_add_controller: Controller bring-up. |
| 63 | * @ctrl: controller to be registered. |
| 64 | * A controller is registered with the framework using this API. ctrl->nr is the |
| 65 | * desired number with which SPMI framework registers the controller. |
| 66 | * Function will return -EBUSY if the number is in use. |
| 67 | */ |
| 68 | int spmi_add_controller(struct spmi_controller *ctrl) |
| 69 | { |
| 70 | int id; |
| 71 | int status; |
| 72 | |
| 73 | pr_debug("adding controller for bus %d (0x%p)\n", ctrl->nr, ctrl); |
| 74 | |
| 75 | if (ctrl->nr & ~MAX_ID_MASK) { |
| 76 | pr_err("invalid bus identifier %d\n", ctrl->nr); |
| 77 | return -EINVAL; |
| 78 | } |
| 79 | |
| 80 | retry: |
| 81 | if (idr_pre_get(&ctrl_idr, GFP_KERNEL) == 0) { |
| 82 | pr_err("no free memory for idr\n"); |
| 83 | return -ENOMEM; |
| 84 | } |
| 85 | |
| 86 | mutex_lock(&board_lock); |
| 87 | status = idr_get_new_above(&ctrl_idr, ctrl, ctrl->nr, &id); |
| 88 | if (status == 0 && id != ctrl->nr) { |
| 89 | status = -EAGAIN; |
| 90 | idr_remove(&ctrl_idr, id); |
| 91 | } |
| 92 | mutex_unlock(&board_lock); |
| 93 | if (status == -EAGAIN) |
| 94 | goto retry; |
| 95 | |
| 96 | if (status == 0) |
| 97 | status = spmi_register_controller(ctrl); |
| 98 | return status; |
| 99 | } |
| 100 | EXPORT_SYMBOL_GPL(spmi_add_controller); |
| 101 | |
| 102 | /** |
| 103 | * spmi_del_controller: Controller tear-down. |
| 104 | * @ctrl: controller to which this device is to be added to. |
| 105 | * |
| 106 | * Controller added with the above API is torn down using this API. |
| 107 | */ |
| 108 | int spmi_del_controller(struct spmi_controller *ctrl) |
| 109 | { |
| 110 | return -ENXIO; |
| 111 | } |
| 112 | EXPORT_SYMBOL_GPL(spmi_del_controller); |
| 113 | |
| 114 | #define spmi_device_attr_gr NULL |
| 115 | #define spmi_device_uevent NULL |
| 116 | static void spmi_dev_release(struct device *dev) |
| 117 | { |
| 118 | struct spmi_device *spmidev = to_spmi_device(dev); |
| 119 | kfree(spmidev); |
| 120 | } |
| 121 | |
| 122 | static struct device_type spmi_dev_type = { |
| 123 | .groups = spmi_device_attr_gr, |
| 124 | .uevent = spmi_device_uevent, |
| 125 | .release = spmi_dev_release, |
| 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * spmi_alloc_device: Allocate a new SPMI devices. |
| 130 | * @ctrl: controller to which this device is to be added to. |
| 131 | * Context: can sleep |
| 132 | * |
| 133 | * Allows a driver to allocate and initialize a SPMI device without |
| 134 | * registering it immediately. This allows a driver to directly fill |
| 135 | * the spmi_device structure before calling spmi_add_device(). |
| 136 | * |
| 137 | * Caller is responsible to call spmi_add_device() on the returned |
| 138 | * spmi_device. If the caller needs to discard the spmi_device without |
| 139 | * adding it, then spmi_dev_put() should be called. |
| 140 | */ |
| 141 | struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl) |
| 142 | { |
| 143 | struct spmi_device *spmidev; |
| 144 | |
| 145 | if (!ctrl) { |
| 146 | pr_err("Missing SPMI controller\n"); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | spmidev = kzalloc(sizeof(*spmidev), GFP_KERNEL); |
| 151 | if (!spmidev) { |
| 152 | dev_err(&ctrl->dev, "unable to allocate spmi_device\n"); |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | spmidev->ctrl = ctrl; |
| 157 | spmidev->dev.parent = ctrl->dev.parent; |
| 158 | spmidev->dev.bus = &spmi_bus_type; |
| 159 | spmidev->dev.type = &spmi_dev_type; |
| 160 | device_initialize(&spmidev->dev); |
| 161 | |
| 162 | return spmidev; |
| 163 | } |
| 164 | EXPORT_SYMBOL_GPL(spmi_alloc_device); |
| 165 | |
| 166 | /* Validate the SPMI device structure */ |
| 167 | static struct device *get_valid_device(struct spmi_device *spmidev) |
| 168 | { |
| 169 | struct device *dev; |
| 170 | |
| 171 | if (!spmidev) |
| 172 | return NULL; |
| 173 | |
| 174 | dev = &spmidev->dev; |
| 175 | if (dev->bus != &spmi_bus_type || dev->type != &spmi_dev_type) |
| 176 | return NULL; |
| 177 | |
| 178 | return dev; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * spmi_add_device: Add a new device without register board info. |
| 183 | * @ctrl: controller to which this device is to be added to. |
| 184 | * |
| 185 | * Called when device doesn't have an explicit client-driver to be probed, or |
| 186 | * the client-driver is a module installed dynamically. |
| 187 | */ |
| 188 | int spmi_add_device(struct spmi_device *spmidev) |
| 189 | { |
| 190 | int rc; |
| 191 | struct device *dev = get_valid_device(spmidev); |
| 192 | |
| 193 | if (!dev) { |
| 194 | pr_err("%s: invalid SPMI device\n", __func__); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | /* Set the device name */ |
| 199 | dev_set_name(dev, "%s-%p", spmidev->name, spmidev); |
| 200 | |
| 201 | /* Device may be bound to an active driver when this returns */ |
| 202 | rc = device_add(dev); |
| 203 | |
| 204 | if (rc < 0) |
| 205 | dev_err(dev, "Can't add %s, status %d\n", dev_name(dev), rc); |
| 206 | else |
| 207 | dev_dbg(dev, "device %s registered\n", dev_name(dev)); |
| 208 | |
| 209 | return rc; |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(spmi_add_device); |
| 212 | |
| 213 | /** |
| 214 | * spmi_new_device: Instantiates a new SPMI device |
| 215 | * @ctrl: controller to which this device is to be added to. |
| 216 | * @info: board information for this device. |
| 217 | * |
| 218 | * Returns the new device or NULL. |
| 219 | */ |
| 220 | struct spmi_device *spmi_new_device(struct spmi_controller *ctrl, |
| 221 | struct spmi_boardinfo const *info) |
| 222 | { |
| 223 | struct spmi_device *spmidev; |
| 224 | int rc; |
| 225 | |
| 226 | if (!ctrl || !info) |
| 227 | return NULL; |
| 228 | |
| 229 | spmidev = spmi_alloc_device(ctrl); |
| 230 | if (!spmidev) |
| 231 | return NULL; |
| 232 | |
| 233 | spmidev->name = info->name; |
| 234 | spmidev->sid = info->slave_id; |
| 235 | spmidev->dev.of_node = info->of_node; |
Kenneth Heitke | ee44ade | 2012-02-08 13:45:33 -0700 | [diff] [blame] | 236 | spmidev->dev.platform_data = (void *)info->platform_data; |
Michael Bohan | 86622b3 | 2012-02-08 16:59:00 -0800 | [diff] [blame] | 237 | spmidev->num_dev_node = info->num_dev_node; |
| 238 | spmidev->dev_node = info->dev_node; |
Kenneth Heitke | ee44ade | 2012-02-08 13:45:33 -0700 | [diff] [blame] | 239 | |
| 240 | rc = spmi_add_device(spmidev); |
| 241 | if (rc < 0) { |
| 242 | spmi_dev_put(spmidev); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | return spmidev; |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(spmi_new_device); |
| 249 | |
| 250 | /* spmi_remove_device: Remove the effect of spmi_add_device() */ |
| 251 | void spmi_remove_device(struct spmi_device *spmi_dev) |
| 252 | { |
| 253 | device_unregister(&spmi_dev->dev); |
| 254 | } |
| 255 | EXPORT_SYMBOL_GPL(spmi_remove_device); |
| 256 | |
| 257 | /* If controller is not present, only add to boards list */ |
| 258 | static void spmi_match_ctrl_to_boardinfo(struct spmi_controller *ctrl, |
| 259 | struct spmi_boardinfo *bi) |
| 260 | { |
| 261 | struct spmi_device *spmidev; |
| 262 | |
| 263 | spmidev = spmi_new_device(ctrl, bi); |
| 264 | if (!spmidev) |
| 265 | dev_err(ctrl->dev.parent, "can't create new device for %s\n", |
| 266 | bi->name); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * spmi_register_board_info: Board-initialization routine. |
| 271 | * @bus_num: controller number (bus) on which this device will sit. |
| 272 | * @info: list of all devices on all controllers present on the board. |
| 273 | * @n: number of entries. |
| 274 | * API enumerates respective devices on corresponding controller. |
| 275 | * Called from board-init function. |
| 276 | */ |
| 277 | int spmi_register_board_info(int busnum, |
| 278 | struct spmi_boardinfo const *info, unsigned n) |
| 279 | { |
| 280 | int i; |
| 281 | struct spmii_boardinfo *bi; |
| 282 | |
| 283 | bi = kzalloc(n * sizeof(*bi), GFP_KERNEL); |
| 284 | if (!bi) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | for (i = 0; i < n; i++, bi++, info++) { |
| 288 | struct spmi_controller *ctrl; |
| 289 | |
| 290 | memcpy(&bi->board_info, info, sizeof(*info)); |
| 291 | mutex_lock(&board_lock); |
| 292 | list_add_tail(&bi->list, &board_list); |
| 293 | list_for_each_entry(ctrl, &spmi_ctrl_list, list) |
| 294 | if (ctrl->nr == busnum) |
| 295 | spmi_match_ctrl_to_boardinfo(ctrl, |
| 296 | &bi->board_info); |
| 297 | mutex_unlock(&board_lock); |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | EXPORT_SYMBOL_GPL(spmi_register_board_info); |
| 302 | |
| 303 | /* ------------------------------------------------------------------------- */ |
| 304 | |
| 305 | static inline int |
| 306 | spmi_cmd(struct spmi_controller *ctrl, u8 opcode, u8 sid) |
| 307 | { |
| 308 | BUG_ON(!ctrl || !ctrl->cmd); |
| 309 | return ctrl->cmd(ctrl, opcode, sid); |
| 310 | } |
| 311 | |
| 312 | static inline int spmi_read_cmd(struct spmi_controller *ctrl, |
| 313 | u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf) |
| 314 | { |
| 315 | BUG_ON(!ctrl || !ctrl->read_cmd); |
| 316 | return ctrl->read_cmd(ctrl, opcode, sid, addr, bc, buf); |
| 317 | } |
| 318 | |
| 319 | static inline int spmi_write_cmd(struct spmi_controller *ctrl, |
| 320 | u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf) |
| 321 | { |
| 322 | BUG_ON(!ctrl || !ctrl->write_cmd); |
| 323 | return ctrl->write_cmd(ctrl, opcode, sid, addr, bc, buf); |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * register read/write: 5-bit address, 1 byte of data |
| 328 | * extended register read/write: 8-bit address, up to 16 bytes of data |
| 329 | * extended register read/write long: 16-bit address, up to 8 bytes of data |
| 330 | */ |
| 331 | |
| 332 | /** |
| 333 | * spmi_register_read() - register read |
| 334 | * @dev: SPMI device. |
| 335 | * @sid: slave identifier. |
| 336 | * @ad: slave register address (5-bit address). |
| 337 | * @buf: buffer to be populated with data from the Slave. |
| 338 | * |
| 339 | * Reads 1 byte of data from a Slave device register. |
| 340 | */ |
| 341 | int spmi_register_read(struct spmi_controller *ctrl, u8 sid, u8 addr, u8 *buf) |
| 342 | { |
| 343 | /* 4-bit Slave Identifier, 5-bit register address */ |
| 344 | if (sid > SPMI_MAX_SLAVE_ID || addr > 0x1F) |
| 345 | return -EINVAL; |
| 346 | |
| 347 | return spmi_read_cmd(ctrl, SPMI_CMD_READ, sid, addr, 0, buf); |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(spmi_register_read); |
| 350 | |
| 351 | /** |
| 352 | * spmi_ext_register_read() - extended register read |
| 353 | * @dev: SPMI device. |
| 354 | * @sid: slave identifier. |
| 355 | * @ad: slave register address (8-bit address). |
| 356 | * @len: the request number of bytes to read (up to 16 bytes). |
| 357 | * @buf: buffer to be populated with data from the Slave. |
| 358 | * |
| 359 | * Reads up to 16 bytes of data from the extended register space on a |
| 360 | * Slave device. |
| 361 | */ |
| 362 | int spmi_ext_register_read(struct spmi_controller *ctrl, |
| 363 | u8 sid, u8 addr, u8 *buf, int len) |
| 364 | { |
| 365 | /* 4-bit Slave Identifier, 8-bit register address, up to 16 bytes */ |
| 366 | if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 16) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | return spmi_read_cmd(ctrl, SPMI_CMD_EXT_READ, sid, addr, len - 1, buf); |
| 370 | } |
| 371 | EXPORT_SYMBOL_GPL(spmi_ext_register_read); |
| 372 | |
| 373 | /** |
| 374 | * spmi_ext_register_readl() - extended register read long |
| 375 | * @dev: SPMI device. |
| 376 | * @sid: slave identifier. |
| 377 | * @ad: slave register address (16-bit address). |
| 378 | * @len: the request number of bytes to read (up to 8 bytes). |
| 379 | * @buf: buffer to be populated with data from the Slave. |
| 380 | * |
| 381 | * Reads up to 8 bytes of data from the extended register space on a |
| 382 | * Slave device using 16-bit address. |
| 383 | */ |
| 384 | int spmi_ext_register_readl(struct spmi_controller *ctrl, |
| 385 | u8 sid, u16 addr, u8 *buf, int len) |
| 386 | { |
| 387 | /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */ |
| 388 | if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | return spmi_read_cmd(ctrl, SPMI_CMD_EXT_READL, sid, addr, len - 1, buf); |
| 392 | } |
| 393 | EXPORT_SYMBOL_GPL(spmi_ext_register_readl); |
| 394 | |
| 395 | /** |
| 396 | * spmi_register_write() - register write |
| 397 | * @dev: SPMI device. |
| 398 | * @sid: slave identifier. |
| 399 | * @ad: slave register address (5-bit address). |
| 400 | * @buf: buffer containing the data to be transferred to the Slave. |
| 401 | * |
| 402 | * Writes 1 byte of data to a Slave device register. |
| 403 | */ |
| 404 | int spmi_register_write(struct spmi_controller *ctrl, u8 sid, u8 addr, u8 *buf) |
| 405 | { |
| 406 | u8 op = SPMI_CMD_WRITE; |
| 407 | |
| 408 | /* 4-bit Slave Identifier, 5-bit register address */ |
| 409 | if (sid > SPMI_MAX_SLAVE_ID || addr > 0x1F) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | return spmi_write_cmd(ctrl, op, sid, addr, 0, buf); |
| 413 | } |
| 414 | EXPORT_SYMBOL_GPL(spmi_register_write); |
| 415 | |
| 416 | /** |
| 417 | * spmi_register_zero_write() - register zero write |
| 418 | * @dev: SPMI device. |
| 419 | * @sid: slave identifier. |
| 420 | * @data: the data to be written to register 0 (7-bits). |
| 421 | * |
| 422 | * Writes data to register 0 of the Slave device. |
| 423 | */ |
| 424 | int spmi_register_zero_write(struct spmi_controller *ctrl, u8 sid, u8 data) |
| 425 | { |
| 426 | u8 op = SPMI_CMD_ZERO_WRITE; |
| 427 | |
| 428 | /* 4-bit Slave Identifier, 5-bit register address */ |
| 429 | if (sid > SPMI_MAX_SLAVE_ID) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | return spmi_write_cmd(ctrl, op, sid, 0, 0, &data); |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(spmi_register_zero_write); |
| 435 | |
| 436 | /** |
| 437 | * spmi_ext_register_write() - extended register write |
| 438 | * @dev: SPMI device. |
| 439 | * @sid: slave identifier. |
| 440 | * @ad: slave register address (8-bit address). |
| 441 | * @buf: buffer containing the data to be transferred to the Slave. |
| 442 | * @len: the request number of bytes to read (up to 16 bytes). |
| 443 | * |
| 444 | * Writes up to 16 bytes of data to the extended register space of a |
| 445 | * Slave device. |
| 446 | */ |
| 447 | int spmi_ext_register_write(struct spmi_controller *ctrl, |
| 448 | u8 sid, u8 addr, u8 *buf, int len) |
| 449 | { |
| 450 | u8 op = SPMI_CMD_EXT_WRITE; |
| 451 | |
| 452 | /* 4-bit Slave Identifier, 8-bit register address, up to 16 bytes */ |
| 453 | if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 16) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | return spmi_write_cmd(ctrl, op, sid, addr, len - 1, buf); |
| 457 | } |
| 458 | EXPORT_SYMBOL_GPL(spmi_ext_register_write); |
| 459 | |
| 460 | /** |
| 461 | * spmi_ext_register_writel() - extended register write long |
| 462 | * @dev: SPMI device. |
| 463 | * @sid: slave identifier. |
| 464 | * @ad: slave register address (16-bit address). |
| 465 | * @buf: buffer containing the data to be transferred to the Slave. |
| 466 | * @len: the request number of bytes to read (up to 8 bytes). |
| 467 | * |
| 468 | * Writes up to 8 bytes of data to the extended register space of a |
| 469 | * Slave device using 16-bit address. |
| 470 | */ |
| 471 | int spmi_ext_register_writel(struct spmi_controller *ctrl, |
| 472 | u8 sid, u16 addr, u8 *buf, int len) |
| 473 | { |
| 474 | u8 op = SPMI_CMD_EXT_WRITEL; |
| 475 | |
| 476 | /* 4-bit Slave Identifier, 16-bit register address, up to 8 bytes */ |
| 477 | if (sid > SPMI_MAX_SLAVE_ID || len <= 0 || len > 8) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | return spmi_write_cmd(ctrl, op, sid, addr, len - 1, buf); |
| 481 | } |
| 482 | EXPORT_SYMBOL_GPL(spmi_ext_register_writel); |
| 483 | |
| 484 | /** |
| 485 | * spmi_command_reset() - sends RESET command to the specified slave |
| 486 | * @dev: SPMI device. |
| 487 | * @sid: slave identifier. |
| 488 | * |
| 489 | * The Reset command initializes the Slave and forces all registers to |
| 490 | * their reset values. The Slave shall enter the STARTUP state after |
| 491 | * receiving a Reset command. |
| 492 | * |
| 493 | * Returns |
| 494 | * -EINVAL for invalid Slave Identifier. |
| 495 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 496 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 497 | * -ETIMEDOUT if the SPMI transaction times out. |
| 498 | */ |
| 499 | int spmi_command_reset(struct spmi_controller *ctrl, u8 sid) |
| 500 | { |
| 501 | if (sid > SPMI_MAX_SLAVE_ID) |
| 502 | return -EINVAL; |
| 503 | return spmi_cmd(ctrl, SPMI_CMD_RESET, sid); |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(spmi_command_reset); |
| 506 | |
| 507 | /** |
| 508 | * spmi_command_sleep() - sends SLEEP command to the specified slave |
| 509 | * @dev: SPMI device. |
| 510 | * @sid: slave identifier. |
| 511 | * |
| 512 | * The Sleep command causes the Slave to enter the user defined SLEEP state. |
| 513 | * |
| 514 | * Returns |
| 515 | * -EINVAL for invalid Slave Identifier. |
| 516 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 517 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 518 | * -ETIMEDOUT if the SPMI transaction times out. |
| 519 | */ |
| 520 | int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid) |
| 521 | { |
| 522 | if (sid > SPMI_MAX_SLAVE_ID) |
| 523 | return -EINVAL; |
| 524 | return spmi_cmd(ctrl, SPMI_CMD_SLEEP, sid); |
| 525 | } |
| 526 | EXPORT_SYMBOL_GPL(spmi_command_sleep); |
| 527 | |
| 528 | /** |
| 529 | * spmi_command_wakeup() - sends WAKEUP command to the specified slave |
| 530 | * @dev: SPMI device. |
| 531 | * @sid: slave identifier. |
| 532 | * |
| 533 | * The Wakeup command causes the Slave to move from the SLEEP state to |
| 534 | * the ACTIVE state. |
| 535 | * |
| 536 | * Returns |
| 537 | * -EINVAL for invalid Slave Identifier. |
| 538 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 539 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 540 | * -ETIMEDOUT if the SPMI transaction times out. |
| 541 | */ |
| 542 | int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid) |
| 543 | { |
| 544 | if (sid > SPMI_MAX_SLAVE_ID) |
| 545 | return -EINVAL; |
| 546 | return spmi_cmd(ctrl, SPMI_CMD_WAKEUP, sid); |
| 547 | } |
| 548 | EXPORT_SYMBOL_GPL(spmi_command_wakeup); |
| 549 | |
| 550 | /** |
| 551 | * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave |
| 552 | * @dev: SPMI device. |
| 553 | * @sid: slave identifier. |
| 554 | * |
| 555 | * The Shutdown command causes the Slave to enter the SHUTDOWN state. |
| 556 | * |
| 557 | * Returns |
| 558 | * -EINVAL for invalid Slave Identifier. |
| 559 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 560 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 561 | * -ETIMEDOUT if the SPMI transaction times out. |
| 562 | */ |
| 563 | int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid) |
| 564 | { |
| 565 | if (sid > SPMI_MAX_SLAVE_ID) |
| 566 | return -EINVAL; |
| 567 | return spmi_cmd(ctrl, SPMI_CMD_SHUTDOWN, sid); |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(spmi_command_shutdown); |
| 570 | |
| 571 | /* ------------------------------------------------------------------------- */ |
| 572 | |
| 573 | static const struct spmi_device_id *spmi_match(const struct spmi_device_id *id, |
| 574 | const struct spmi_device *spmi_dev) |
| 575 | { |
| 576 | while (id->name[0]) { |
| 577 | if (strncmp(spmi_dev->name, id->name, SPMI_NAME_SIZE) == 0) |
| 578 | return id; |
| 579 | id++; |
| 580 | } |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | static int spmi_device_match(struct device *dev, struct device_driver *drv) |
| 585 | { |
| 586 | struct spmi_device *spmi_dev; |
| 587 | struct spmi_driver *sdrv = to_spmi_driver(drv); |
| 588 | |
| 589 | if (dev->type == &spmi_dev_type) |
| 590 | spmi_dev = to_spmi_device(dev); |
| 591 | else |
| 592 | return 0; |
| 593 | |
| 594 | /* Attempt an OF style match */ |
| 595 | if (of_driver_match_device(dev, drv)) |
| 596 | return 1; |
| 597 | |
| 598 | if (sdrv->id_table) |
| 599 | return spmi_match(sdrv->id_table, spmi_dev) != NULL; |
| 600 | |
| 601 | if (drv->name) |
| 602 | return strncmp(spmi_dev->name, drv->name, SPMI_NAME_SIZE) == 0; |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | #ifdef CONFIG_PM_SLEEP |
| 607 | static int spmi_legacy_suspend(struct device *dev, pm_message_t mesg) |
| 608 | { |
| 609 | struct spmi_device *spmi_dev = NULL; |
| 610 | struct spmi_driver *driver; |
| 611 | if (dev->type == &spmi_dev_type) |
| 612 | spmi_dev = to_spmi_device(dev); |
| 613 | |
| 614 | if (!spmi_dev || !dev->driver) |
| 615 | return 0; |
| 616 | |
| 617 | driver = to_spmi_driver(dev->driver); |
| 618 | if (!driver->suspend) |
| 619 | return 0; |
| 620 | |
| 621 | return driver->suspend(spmi_dev, mesg); |
| 622 | } |
| 623 | |
| 624 | static int spmi_legacy_resume(struct device *dev) |
| 625 | { |
| 626 | struct spmi_device *spmi_dev = NULL; |
| 627 | struct spmi_driver *driver; |
| 628 | if (dev->type == &spmi_dev_type) |
| 629 | spmi_dev = to_spmi_device(dev); |
| 630 | |
| 631 | if (!spmi_dev || !dev->driver) |
| 632 | return 0; |
| 633 | |
| 634 | driver = to_spmi_driver(dev->driver); |
| 635 | if (!driver->resume) |
| 636 | return 0; |
| 637 | |
| 638 | return driver->resume(spmi_dev); |
| 639 | } |
| 640 | |
| 641 | static int spmi_pm_suspend(struct device *dev) |
| 642 | { |
| 643 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 644 | |
| 645 | if (pm) |
| 646 | return pm_generic_suspend(dev); |
| 647 | else |
| 648 | return spmi_legacy_suspend(dev, PMSG_SUSPEND); |
| 649 | } |
| 650 | |
| 651 | static int spmi_pm_resume(struct device *dev) |
| 652 | { |
| 653 | const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL; |
| 654 | |
| 655 | if (pm) |
| 656 | return pm_generic_resume(dev); |
| 657 | else |
| 658 | return spmi_legacy_resume(dev); |
| 659 | } |
| 660 | |
| 661 | #else |
| 662 | #define spmi_pm_suspend NULL |
| 663 | #define spmi_pm_resume NULL |
| 664 | #endif |
| 665 | |
| 666 | static const struct dev_pm_ops spmi_pm_ops = { |
| 667 | .suspend = spmi_pm_suspend, |
| 668 | .resume = spmi_pm_resume, |
| 669 | SET_RUNTIME_PM_OPS( |
| 670 | pm_generic_suspend, |
| 671 | pm_generic_resume, |
| 672 | pm_generic_runtime_idle |
| 673 | ) |
| 674 | }; |
| 675 | struct bus_type spmi_bus_type = { |
| 676 | .name = "spmi", |
| 677 | .match = spmi_device_match, |
| 678 | .pm = &spmi_pm_ops, |
| 679 | }; |
| 680 | EXPORT_SYMBOL_GPL(spmi_bus_type); |
| 681 | |
| 682 | struct device spmi_dev = { |
| 683 | .init_name = "spmi", |
| 684 | }; |
| 685 | |
| 686 | static int spmi_drv_probe(struct device *dev) |
| 687 | { |
| 688 | const struct spmi_driver *sdrv = to_spmi_driver(dev->driver); |
| 689 | |
| 690 | return sdrv->probe(to_spmi_device(dev)); |
| 691 | } |
| 692 | |
| 693 | static int spmi_drv_remove(struct device *dev) |
| 694 | { |
| 695 | const struct spmi_driver *sdrv = to_spmi_driver(dev->driver); |
| 696 | |
| 697 | return sdrv->remove(to_spmi_device(dev)); |
| 698 | } |
| 699 | |
| 700 | static void spmi_drv_shutdown(struct device *dev) |
| 701 | { |
| 702 | const struct spmi_driver *sdrv = to_spmi_driver(dev->driver); |
| 703 | |
| 704 | sdrv->shutdown(to_spmi_device(dev)); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * spmi_driver_register: Client driver registration with SPMI framework. |
| 709 | * @drv: client driver to be associated with client-device. |
| 710 | * |
| 711 | * This API will register the client driver with the SPMI framework. |
| 712 | * It is called from the driver's module-init function. |
| 713 | */ |
| 714 | int spmi_driver_register(struct spmi_driver *drv) |
| 715 | { |
| 716 | drv->driver.bus = &spmi_bus_type; |
| 717 | |
| 718 | if (drv->probe) |
| 719 | drv->driver.probe = spmi_drv_probe; |
| 720 | |
| 721 | if (drv->remove) |
| 722 | drv->driver.remove = spmi_drv_remove; |
| 723 | |
| 724 | if (drv->shutdown) |
| 725 | drv->driver.shutdown = spmi_drv_shutdown; |
| 726 | |
| 727 | return driver_register(&drv->driver); |
| 728 | } |
| 729 | EXPORT_SYMBOL_GPL(spmi_driver_register); |
| 730 | |
| 731 | static int spmi_register_controller(struct spmi_controller *ctrl) |
| 732 | { |
| 733 | int ret = 0; |
| 734 | |
| 735 | /* Can't register until after driver model init */ |
| 736 | if (WARN_ON(!spmi_bus_type.p)) { |
| 737 | ret = -EAGAIN; |
| 738 | goto exit; |
| 739 | } |
| 740 | |
| 741 | dev_set_name(&ctrl->dev, "spmi-%d", ctrl->nr); |
| 742 | ctrl->dev.bus = &spmi_bus_type; |
| 743 | ctrl->dev.type = &spmi_ctrl_type; |
| 744 | ret = device_register(&ctrl->dev); |
| 745 | if (ret) |
| 746 | goto exit; |
| 747 | |
| 748 | dev_dbg(&ctrl->dev, "Bus spmi-%d registered: dev:%x\n", |
| 749 | ctrl->nr, (u32)&ctrl->dev); |
| 750 | |
| 751 | mutex_lock(&board_lock); |
| 752 | list_add_tail(&ctrl->list, &spmi_ctrl_list); |
| 753 | mutex_unlock(&board_lock); |
| 754 | |
| 755 | return 0; |
| 756 | |
| 757 | exit: |
| 758 | mutex_lock(&board_lock); |
| 759 | idr_remove(&ctrl_idr, ctrl->nr); |
| 760 | mutex_unlock(&board_lock); |
| 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | static void __exit spmi_exit(void) |
| 765 | { |
| 766 | device_unregister(&spmi_dev); |
| 767 | bus_unregister(&spmi_bus_type); |
| 768 | } |
| 769 | |
| 770 | static int __init spmi_init(void) |
| 771 | { |
| 772 | int retval; |
| 773 | |
| 774 | retval = bus_register(&spmi_bus_type); |
| 775 | if (!retval) |
| 776 | retval = device_register(&spmi_dev); |
| 777 | |
| 778 | if (retval) |
| 779 | bus_unregister(&spmi_bus_type); |
| 780 | |
| 781 | return retval; |
| 782 | } |
| 783 | postcore_initcall(spmi_init); |
| 784 | module_exit(spmi_exit); |
| 785 | |
| 786 | MODULE_LICENSE("GPL v2"); |
| 787 | MODULE_VERSION("1.0"); |
| 788 | MODULE_DESCRIPTION("SPMI module"); |
| 789 | MODULE_ALIAS("platform:spmi"); |