1 /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
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.
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.
15 #include <linux/types.h>
16 #include <linux/device.h>
17 #include <linux/mod_devicetable.h>
19 /* Maximum slave identifier */
20 #define SPMI_MAX_SLAVE_ID 16
23 #define SPMI_CMD_EXT_WRITE 0x00
24 #define SPMI_CMD_RESET 0x10
25 #define SPMI_CMD_SLEEP 0x11
26 #define SPMI_CMD_SHUTDOWN 0x12
27 #define SPMI_CMD_WAKEUP 0x13
28 #define SPMI_CMD_AUTHENTICATE 0x14
29 #define SPMI_CMD_MSTR_READ 0x15
30 #define SPMI_CMD_MSTR_WRITE 0x16
31 #define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A
32 #define SPMI_CMD_DDB_MASTER_READ 0x1B
33 #define SPMI_CMD_DDB_SLAVE_READ 0x1C
34 #define SPMI_CMD_EXT_READ 0x20
35 #define SPMI_CMD_EXT_WRITEL 0x30
36 #define SPMI_CMD_EXT_READL 0x38
37 #define SPMI_CMD_WRITE 0x40
38 #define SPMI_CMD_READ 0x60
39 #define SPMI_CMD_ZERO_WRITE 0x80
42 * struct spmi_device - Basic representation of an SPMI device
43 * @dev: Driver model representation of the device.
44 * @ctrl: SPMI controller managing the bus hosting this device.
45 * @usid: This devices' Unique Slave IDentifier.
49 struct spmi_controller *ctrl;
53 static inline struct spmi_device *to_spmi_device(struct device *d)
55 return container_of(d, struct spmi_device, dev);
58 static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
60 return dev_get_drvdata(&sdev->dev);
63 static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
65 dev_set_drvdata(&sdev->dev, data);
68 struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
70 static inline void spmi_device_put(struct spmi_device *sdev)
73 put_device(&sdev->dev);
76 int spmi_device_add(struct spmi_device *sdev);
78 void spmi_device_remove(struct spmi_device *sdev);
81 * struct spmi_controller - interface to the SPMI master controller
82 * @dev: Driver model representation of the device.
83 * @nr: board-specific number identifier for this controller/bus
84 * @cmd: sends a non-data command sequence on the SPMI bus.
85 * @read_cmd: sends a register read command sequence on the SPMI bus.
86 * @write_cmd: sends a register write command sequence on the SPMI bus.
88 struct spmi_controller {
91 int (*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
92 int (*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
93 u8 sid, u16 addr, u8 *buf, size_t len);
94 int (*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
95 u8 sid, u16 addr, const u8 *buf, size_t len);
98 static inline struct spmi_controller *to_spmi_controller(struct device *d)
100 return container_of(d, struct spmi_controller, dev);
104 void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
106 return dev_get_drvdata(&ctrl->dev);
109 static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
112 dev_set_drvdata(&ctrl->dev, data);
115 struct spmi_controller *spmi_controller_alloc(struct device *parent,
119 * spmi_controller_put() - decrement controller refcount
120 * @ctrl SPMI controller.
122 static inline void spmi_controller_put(struct spmi_controller *ctrl)
125 put_device(&ctrl->dev);
128 int spmi_controller_add(struct spmi_controller *ctrl);
129 void spmi_controller_remove(struct spmi_controller *ctrl);
132 * struct spmi_driver - SPMI slave device driver
133 * @driver: SPMI device drivers should initialize name and owner field of
135 * @probe: binds this driver to a SPMI device.
136 * @remove: unbinds this driver from the SPMI device.
138 * If PM runtime support is desired for a slave, a device driver can call
139 * pm_runtime_put() from their probe() routine (and a balancing
140 * pm_runtime_get() in remove()). PM runtime support for a slave is
141 * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
142 * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
143 * command is sent to the slave to bring it back to ACTIVE.
146 struct device_driver driver;
147 int (*probe)(struct spmi_device *sdev);
148 void (*remove)(struct spmi_device *sdev);
151 static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
153 return container_of(d, struct spmi_driver, driver);
156 #define spmi_driver_register(sdrv) \
157 __spmi_driver_register(sdrv, THIS_MODULE)
158 int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner);
161 * spmi_driver_unregister() - unregister an SPMI client driver
162 * @sdrv: the driver to unregister
164 static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
167 driver_unregister(&sdrv->driver);
170 #define module_spmi_driver(__spmi_driver) \
171 module_driver(__spmi_driver, spmi_driver_register, \
172 spmi_driver_unregister)
174 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
175 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
177 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
179 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
180 int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
181 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
182 const u8 *buf, size_t len);
183 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
184 const u8 *buf, size_t len);
185 int spmi_command_reset(struct spmi_device *sdev);
186 int spmi_command_sleep(struct spmi_device *sdev);
187 int spmi_command_wakeup(struct spmi_device *sdev);
188 int spmi_command_shutdown(struct spmi_device *sdev);