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.
137 * @shutdown: standard shutdown callback used during powerdown/halt.
138 * @suspend: standard suspend callback used during system suspend.
139 * @resume: standard resume callback used during system resume.
141 * If PM runtime support is desired for a slave, a device driver can call
142 * pm_runtime_put() from their probe() routine (and a balancing
143 * pm_runtime_get() in remove()). PM runtime support for a slave is
144 * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
145 * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
146 * command is sent to the slave to bring it back to ACTIVE.
149 struct device_driver driver;
150 int (*probe)(struct spmi_device *sdev);
151 void (*remove)(struct spmi_device *sdev);
154 static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
156 return container_of(d, struct spmi_driver, driver);
159 int spmi_driver_register(struct spmi_driver *sdrv);
162 * spmi_driver_unregister() - unregister an SPMI client driver
163 * @sdrv: the driver to unregister
165 static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
168 driver_unregister(&sdrv->driver);
171 #define module_spmi_driver(__spmi_driver) \
172 module_driver(__spmi_driver, spmi_driver_register, \
173 spmi_driver_unregister)
175 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
176 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
178 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
180 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
181 int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
182 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
183 const u8 *buf, size_t len);
184 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
185 const u8 *buf, size_t len);
186 int spmi_command_reset(struct spmi_device *sdev);
187 int spmi_command_sleep(struct spmi_device *sdev);
188 int spmi_command_wakeup(struct spmi_device *sdev);
189 int spmi_command_shutdown(struct spmi_device *sdev);