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 | #ifndef _LINUX_SPMI_H |
| 14 | #define _LINUX_SPMI_H |
| 15 | |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/mod_devicetable.h> |
| 19 | |
| 20 | /* Maximum slave identifier */ |
| 21 | #define SPMI_MAX_SLAVE_ID 16 |
| 22 | |
| 23 | /* SPMI Commands */ |
| 24 | enum spmi_commands { |
| 25 | SPMI_CMD_EXT_WRITE = 0x00, |
| 26 | SPMI_CMD_RESET = 0x10, |
| 27 | SPMI_CMD_SLEEP = 0x11, |
| 28 | SPMI_CMD_SHUTDOWN = 0x12, |
| 29 | SPMI_CMD_WAKEUP = 0x13, |
| 30 | SPMI_CMD_AUTHENTICATE = 0x14, |
| 31 | SPMI_CMD_MSTR_READ = 0x15, |
| 32 | SPMI_CMD_MSTR_WRITE = 0x16, |
| 33 | SPMI_CMD_TRANSFER_BUS_OWNERSHIP = 0x1A, |
| 34 | SPMI_CMD_DDB_MASTER_READ = 0x1B, |
| 35 | SPMI_CMD_DDB_SLAVE_READ = 0x1C, |
| 36 | SPMI_CMD_EXT_READ = 0x20, |
| 37 | SPMI_CMD_EXT_WRITEL = 0x30, |
| 38 | SPMI_CMD_EXT_READL = 0x38, |
| 39 | SPMI_CMD_WRITE = 0x40, |
| 40 | SPMI_CMD_READ = 0x60, |
| 41 | SPMI_CMD_ZERO_WRITE = 0x80, |
| 42 | }; |
| 43 | |
| 44 | struct spmi_device; |
| 45 | |
| 46 | /** |
| 47 | * struct spmi_controller: interface to the SPMI master controller |
| 48 | * @nr: board-specific number identifier for this controller/bus |
| 49 | * @name: name for this controller |
| 50 | * @cmd: sends a non-data command sequence on the SPMI bus. |
| 51 | * @read_cmd: sends a register read command sequence on the SPMI bus. |
| 52 | * @write_cmd: sends a register write command sequence on the SPMI bus. |
| 53 | */ |
| 54 | struct spmi_controller { |
| 55 | struct device dev; |
| 56 | unsigned int nr; |
| 57 | struct list_head list; |
| 58 | int (*cmd)(struct spmi_controller *, u8 opcode, u8 sid); |
| 59 | int (*read_cmd)(struct spmi_controller *, |
| 60 | u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf); |
| 61 | int (*write_cmd)(struct spmi_controller *, |
| 62 | u8 opcode, u8 sid, u16 addr, u8 bc, u8 *buf); |
| 63 | }; |
| 64 | #define to_spmi_controller(d) container_of(d, struct spmi_controller, dev) |
| 65 | |
| 66 | /** |
| 67 | * struct spmi_driver: Manage SPMI generic/slave device driver |
| 68 | * @probe: binds this driver to a SPMI device. |
| 69 | * @remove: unbinds this driver from the SPMI device. |
| 70 | * @shutdown: standard shutdown callback used during powerdown/halt. |
| 71 | * @suspend: standard suspend callback used during system suspend |
| 72 | * @resume: standard resume callback used during system resume |
| 73 | * @driver: SPMI device drivers should initialize name and owner field of |
| 74 | * this structure |
| 75 | * @id_table: list of SPMI devices supported by this driver |
| 76 | */ |
| 77 | struct spmi_driver { |
| 78 | int (*probe)(struct spmi_device *dev); |
| 79 | int (*remove)(struct spmi_device *dev); |
| 80 | void (*shutdown)(struct spmi_device *dev); |
| 81 | int (*suspend)(struct spmi_device *dev, |
| 82 | pm_message_t pmesg); |
| 83 | int (*resume)(struct spmi_device *dev); |
| 84 | |
| 85 | struct device_driver driver; |
| 86 | const struct spmi_device_id *id_table; |
| 87 | }; |
| 88 | #define to_spmi_driver(d) container_of(d, struct spmi_driver, driver) |
| 89 | |
| 90 | /** |
| 91 | * Client/device handle (struct spmi_device): |
| 92 | * ------------------------------------------ |
| 93 | * This is the client/device handle returned when a SPMI device |
| 94 | * is registered with a controller. |
| 95 | * Pointer to this structure is used by client-driver as a handle. |
| 96 | * @dev: driver model representation of the device. |
| 97 | * @name: name of driver to use with this device. |
| 98 | * @ctrl: SPMI controller managing the bus hosting this device. |
| 99 | * @resource: array of resources for this device_node |
| 100 | * @num_resources: number of resources for this device node |
| 101 | * @sid: slave identifier. |
| 102 | */ |
| 103 | struct spmi_device { |
| 104 | struct device dev; |
| 105 | const char *name; |
| 106 | struct spmi_controller *ctrl; |
| 107 | struct resource *resource; |
| 108 | u32 num_resources; |
| 109 | u8 sid; |
| 110 | }; |
| 111 | #define to_spmi_device(d) container_of(d, struct spmi_device, dev) |
| 112 | |
| 113 | /** |
| 114 | * struct spmi_boardinfo: Declare board info for SPMI device bringup. |
| 115 | * @slave_id: slave identifier. |
| 116 | * @spmi_device: device to be registered with the SPMI framework. |
| 117 | * @of_node: pointer to the OpenFirmware device node. |
| 118 | * @num_resources: number of resources for this device node |
| 119 | * @resource: array of resources for this device_node |
| 120 | * @platform_data: goes to spmi_device.dev.platform_data |
| 121 | */ |
| 122 | struct spmi_boardinfo { |
| 123 | char name[SPMI_NAME_SIZE]; |
| 124 | uint8_t slave_id; |
| 125 | struct device_node *of_node; |
| 126 | u32 num_resources; |
| 127 | struct resource *resource; |
| 128 | const void *platform_data; |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * spmi_driver_register: Client driver registration with SPMI framework. |
| 133 | * @drv: client driver to be associated with client-device. |
| 134 | * |
| 135 | * This API will register the client driver with the SPMI framework. |
| 136 | * It is called from the driver's module-init function. |
| 137 | */ |
| 138 | extern int spmi_driver_register(struct spmi_driver *drv); |
| 139 | |
| 140 | /** |
| 141 | * spmi_driver_unregister - reverse effect of spmi_driver_register |
| 142 | * @sdrv: the driver to unregister |
| 143 | * Context: can sleep |
| 144 | */ |
| 145 | static inline void spmi_driver_unregister(struct spmi_driver *sdrv) |
| 146 | { |
| 147 | if (sdrv) |
| 148 | driver_unregister(&sdrv->driver); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * spmi_add_controller: Controller bring-up. |
| 153 | * @ctrl: controller to be registered. |
| 154 | * |
| 155 | * A controller is registered with the framework using this API. ctrl->nr is the |
| 156 | * desired number with which SPMI framework registers the controller. |
| 157 | * Function will return -EBUSY if the number is in use. |
| 158 | */ |
| 159 | extern int spmi_add_controller(struct spmi_controller *ctrl); |
| 160 | |
| 161 | /** |
| 162 | * spmi_del_controller: Controller tear-down. |
| 163 | * Controller added with the above API is teared down using this API. |
| 164 | */ |
| 165 | extern int spmi_del_controller(struct spmi_controller *ctrl); |
| 166 | |
| 167 | /** |
| 168 | * spmi_busnum_to_ctrl: Map bus number to controller |
| 169 | * @busnum: bus number |
| 170 | * |
| 171 | * Returns controller device representing this bus number |
| 172 | */ |
| 173 | extern struct spmi_controller *spmi_busnum_to_ctrl(u32 bus_num); |
| 174 | |
| 175 | /** |
| 176 | * spmi_alloc_device: Allocate a new SPMI devices. |
| 177 | * @ctrl: controller to which this device is to be added to. |
| 178 | * Context: can sleep |
| 179 | * |
| 180 | * Allows a driver to allocate and initialize a SPMI device without |
| 181 | * registering it immediately. This allows a driver to directly fill |
| 182 | * the spmi_device structure before calling spmi_add_device(). |
| 183 | * |
| 184 | * Caller is responsible to call spmi_add_device() on the returned |
| 185 | * spmi_device. If the caller needs to discard the spmi_device without |
| 186 | * adding it, then spmi_dev_put() should be called. |
| 187 | */ |
| 188 | extern struct spmi_device *spmi_alloc_device(struct spmi_controller *ctrl); |
| 189 | |
| 190 | /** |
| 191 | * spmi_add_device: Add spmi_device allocated with spmi_alloc_device(). |
| 192 | * @spmi_dev: spmi_device to be added (registered). |
| 193 | */ |
| 194 | extern int spmi_add_device(struct spmi_device *spmi_dev); |
| 195 | |
| 196 | /** |
| 197 | * spmi_new_device: Instantiates a new SPMI device |
| 198 | * @ctrl: controller to which this device is to be added to. |
| 199 | * @info: board information for this device. |
| 200 | * |
| 201 | * Returns the new device or NULL. |
| 202 | */ |
| 203 | extern struct spmi_device *spmi_new_device(struct spmi_controller *ctrl, |
| 204 | struct spmi_boardinfo const *info); |
| 205 | |
| 206 | /* spmi_remove_device: Remove the effect of spmi_add_device() */ |
| 207 | extern void spmi_remove_device(struct spmi_device *spmi_dev); |
| 208 | |
| 209 | #ifdef CONFIG_SPMI |
| 210 | /** |
| 211 | * spmi_register_board_info: Board-initialization routine. |
| 212 | * @bus_num: controller number (bus) on which this device will sit. |
| 213 | * @info: list of all devices on all controllers present on the board. |
| 214 | * @n: number of entries. |
| 215 | * |
| 216 | * API enumerates respective devices on corresponding controller. |
| 217 | * Called from board-init function. |
| 218 | */ |
| 219 | extern int spmi_register_board_info(int busnum, |
| 220 | struct spmi_boardinfo const *info, unsigned n); |
| 221 | #else |
| 222 | static inline int spmi_register_board_info(int busnum, |
| 223 | struct spmi_boardinfo const *info, unsigned n) |
| 224 | { |
| 225 | return 0; |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | static inline void *spmi_get_ctrldata(const struct spmi_controller *ctrl) |
| 230 | { |
| 231 | return dev_get_drvdata(&ctrl->dev); |
| 232 | } |
| 233 | |
| 234 | static inline void spmi_set_ctrldata(struct spmi_controller *ctrl, void *data) |
| 235 | { |
| 236 | dev_set_drvdata(&ctrl->dev, data); |
| 237 | } |
| 238 | |
| 239 | static inline void *spmi_get_devicedata(const struct spmi_device *dev) |
| 240 | { |
| 241 | return dev_get_drvdata(&dev->dev); |
| 242 | } |
| 243 | |
| 244 | static inline void spmi_set_devicedata(struct spmi_device *dev, void *data) |
| 245 | { |
| 246 | dev_set_drvdata(&dev->dev, data); |
| 247 | } |
| 248 | |
| 249 | static inline void spmi_dev_put(struct spmi_device *spmidev) |
| 250 | { |
| 251 | if (spmidev) |
| 252 | put_device(&spmidev->dev); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * spmi_register_read() - register read |
| 257 | * @ctrl: SPMI controller. |
| 258 | * @sid: slave identifier. |
| 259 | * @ad: slave register address (5-bit address). |
| 260 | * @buf: buffer to be populated with data from the Slave. |
| 261 | * |
| 262 | * Reads 1 byte of data from a Slave device register. |
| 263 | */ |
| 264 | extern int spmi_register_read(struct spmi_controller *ctrl, |
| 265 | u8 sid, u8 ad, u8 *buf); |
| 266 | |
| 267 | /** |
| 268 | * spmi_ext_register_read() - extended register read |
| 269 | * @ctrl: SPMI controller. |
| 270 | * @sid: slave identifier. |
| 271 | * @ad: slave register address (8-bit address). |
| 272 | * @len: the request number of bytes to read (up to 16 bytes). |
| 273 | * @buf: buffer to be populated with data from the Slave. |
| 274 | * |
| 275 | * Reads up to 16 bytes of data from the extended register space on a |
| 276 | * Slave device. |
| 277 | */ |
| 278 | extern int spmi_ext_register_read(struct spmi_controller *ctrl, |
| 279 | u8 sid, u8 ad, u8 *buf, int len); |
| 280 | |
| 281 | /** |
| 282 | * spmi_ext_register_readl() - extended register read long |
| 283 | * @ctrl: SPMI controller. |
| 284 | * @sid: slave identifier. |
| 285 | * @ad: slave register address (16-bit address). |
| 286 | * @len: the request number of bytes to read (up to 8 bytes). |
| 287 | * @buf: buffer to be populated with data from the Slave. |
| 288 | * |
| 289 | * Reads up to 8 bytes of data from the extended register space on a |
| 290 | * Slave device using 16-bit address. |
| 291 | */ |
| 292 | extern int spmi_ext_register_readl(struct spmi_controller *ctrl, |
| 293 | u8 sid, u16 ad, u8 *buf, int len); |
| 294 | |
| 295 | /** |
| 296 | * spmi_register_write() - register write |
| 297 | * @ctrl: SPMI controller. |
| 298 | * @sid: slave identifier. |
| 299 | * @ad: slave register address (5-bit address). |
| 300 | * @buf: buffer containing the data to be transferred to the Slave. |
| 301 | * |
| 302 | * Writes 1 byte of data to a Slave device register. |
| 303 | */ |
| 304 | extern int spmi_register_write(struct spmi_controller *ctrl, |
| 305 | u8 sid, u8 ad, u8 *buf); |
| 306 | |
| 307 | /** |
| 308 | * spmi_register_zero_write() - register zero write |
| 309 | * @ctrl: SPMI controller. |
| 310 | * @sid: slave identifier. |
| 311 | * @data: the data to be written to register 0 (7-bits). |
| 312 | * |
| 313 | * Writes data to register 0 of the Slave device. |
| 314 | */ |
| 315 | extern int spmi_register_zero_write(struct spmi_controller *ctrl, |
| 316 | u8 sid, u8 data); |
| 317 | |
| 318 | /** |
| 319 | * spmi_ext_register_write() - extended register write |
| 320 | * @ctrl: SPMI controller. |
| 321 | * @sid: slave identifier. |
| 322 | * @ad: slave register address (8-bit address). |
| 323 | * @buf: buffer containing the data to be transferred to the Slave. |
| 324 | * @len: the request number of bytes to read (up to 16 bytes). |
| 325 | * |
| 326 | * Writes up to 16 bytes of data to the extended register space of a |
| 327 | * Slave device. |
| 328 | */ |
| 329 | extern int spmi_ext_register_write(struct spmi_controller *ctrl, |
| 330 | u8 sid, u8 ad, u8 *buf, int len); |
| 331 | |
| 332 | /** |
| 333 | * spmi_ext_register_writel() - extended register write long |
| 334 | * @ctrl: SPMI controller. |
| 335 | * @sid: slave identifier. |
| 336 | * @ad: slave register address (16-bit address). |
| 337 | * @buf: buffer containing the data to be transferred to the Slave. |
| 338 | * @len: the request number of bytes to read (up to 8 bytes). |
| 339 | * |
| 340 | * Writes up to 8 bytes of data to the extended register space of a |
| 341 | * Slave device using 16-bit address. |
| 342 | */ |
| 343 | extern int spmi_ext_register_writel(struct spmi_controller *ctrl, |
| 344 | u8 sid, u16 ad, u8 *buf, int len); |
| 345 | |
| 346 | /** |
| 347 | * spmi_command_reset() - sends RESET command to the specified slave |
| 348 | * @ctrl: SPMI controller. |
| 349 | * @sid: slave identifier. |
| 350 | * |
| 351 | * The Reset command initializes the Slave and forces all registers to |
| 352 | * their reset values. The Slave shall enter the STARTUP state after |
| 353 | * receiving a Reset command. |
| 354 | * |
| 355 | * Returns |
| 356 | * -EINVAL for invalid slave identifier. |
| 357 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 358 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 359 | * -ETIMEDOUT if the SPMI transaction times out. |
| 360 | */ |
| 361 | extern int spmi_command_reset(struct spmi_controller *ctrl, u8 sid); |
| 362 | |
| 363 | /** |
| 364 | * spmi_command_sleep() - sends SLEEP command to the specified slave |
| 365 | * @ctrl: SPMI controller. |
| 366 | * @sid: slave identifier. |
| 367 | * |
| 368 | * The Sleep command causes the Slave to enter the user defined SLEEP state. |
| 369 | * |
| 370 | * Returns |
| 371 | * -EINVAL for invalid slave identifier. |
| 372 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 373 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 374 | * -ETIMEDOUT if the SPMI transaction times out. |
| 375 | */ |
| 376 | extern int spmi_command_sleep(struct spmi_controller *ctrl, u8 sid); |
| 377 | |
| 378 | /** |
| 379 | * spmi_command_wakeup() - sends WAKEUP command to the specified slave |
| 380 | * @ctrl: SPMI controller. |
| 381 | * @sid: slave identifier. |
| 382 | * |
| 383 | * The Wakeup command causes the Slave to move from the SLEEP state to |
| 384 | * the ACTIVE state. |
| 385 | * |
| 386 | * Returns |
| 387 | * -EINVAL for invalid slave identifier. |
| 388 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 389 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 390 | * -ETIMEDOUT if the SPMI transaction times out. |
| 391 | */ |
| 392 | extern int spmi_command_wakeup(struct spmi_controller *ctrl, u8 sid); |
| 393 | |
| 394 | /** |
| 395 | * spmi_command_shutdown() - sends SHUTDOWN command to the specified slave |
| 396 | * @ctrl: SPMI controller. |
| 397 | * @sid: slave identifier. |
| 398 | * |
| 399 | * The Shutdown command causes the Slave to enter the SHUTDOWN state. |
| 400 | * |
| 401 | * Returns |
| 402 | * -EINVAL for invalid slave identifier. |
| 403 | * -EPERM if the SPMI transaction is denied due to permission issues. |
| 404 | * -EIO if the SPMI transaction fails (parity errors, etc). |
| 405 | * -ETIMEDOUT if the SPMI transaction times out. |
| 406 | */ |
| 407 | extern int spmi_command_shutdown(struct spmi_controller *ctrl, u8 sid); |
| 408 | #endif |