1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
3 */
4 #ifndef _LINUX_SPMI_H
5 #define _LINUX_SPMI_H
6
7 #include <linux/types.h>
8 #include <linux/device.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/android_kabi.h>
11
12 /* Maximum slave identifier */
13 #define SPMI_MAX_SLAVE_ID 16
14
15 /* SPMI Commands */
16 #define SPMI_CMD_EXT_WRITE 0x00
17 #define SPMI_CMD_RESET 0x10
18 #define SPMI_CMD_SLEEP 0x11
19 #define SPMI_CMD_SHUTDOWN 0x12
20 #define SPMI_CMD_WAKEUP 0x13
21 #define SPMI_CMD_AUTHENTICATE 0x14
22 #define SPMI_CMD_MSTR_READ 0x15
23 #define SPMI_CMD_MSTR_WRITE 0x16
24 #define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A
25 #define SPMI_CMD_DDB_MASTER_READ 0x1B
26 #define SPMI_CMD_DDB_SLAVE_READ 0x1C
27 #define SPMI_CMD_EXT_READ 0x20
28 #define SPMI_CMD_EXT_WRITEL 0x30
29 #define SPMI_CMD_EXT_READL 0x38
30 #define SPMI_CMD_WRITE 0x40
31 #define SPMI_CMD_READ 0x60
32 #define SPMI_CMD_ZERO_WRITE 0x80
33
34 /**
35 * struct spmi_device - Basic representation of an SPMI device
36 * @dev: Driver model representation of the device.
37 * @ctrl: SPMI controller managing the bus hosting this device.
38 * @usid: This devices' Unique Slave IDentifier.
39 */
40 struct spmi_device {
41 struct device dev;
42 struct spmi_controller *ctrl;
43 u8 usid;
44 };
45
to_spmi_device(struct device * d)46 static inline struct spmi_device *to_spmi_device(struct device *d)
47 {
48 return container_of(d, struct spmi_device, dev);
49 }
50
spmi_device_get_drvdata(const struct spmi_device * sdev)51 static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev)
52 {
53 return dev_get_drvdata(&sdev->dev);
54 }
55
spmi_device_set_drvdata(struct spmi_device * sdev,void * data)56 static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data)
57 {
58 dev_set_drvdata(&sdev->dev, data);
59 }
60
61 struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl);
62
spmi_device_put(struct spmi_device * sdev)63 static inline void spmi_device_put(struct spmi_device *sdev)
64 {
65 if (sdev)
66 put_device(&sdev->dev);
67 }
68
69 int spmi_device_add(struct spmi_device *sdev);
70
71 void spmi_device_remove(struct spmi_device *sdev);
72
73 /**
74 * struct spmi_controller - interface to the SPMI master controller
75 * @dev: Driver model representation of the device.
76 * @nr: board-specific number identifier for this controller/bus
77 * @cmd: sends a non-data command sequence on the SPMI bus.
78 * @read_cmd: sends a register read command sequence on the SPMI bus.
79 * @write_cmd: sends a register write command sequence on the SPMI bus.
80 */
81 struct spmi_controller {
82 struct device dev;
83 unsigned int nr;
84 int (*cmd)(struct spmi_controller *ctrl, u8 opcode, u8 sid);
85 int (*read_cmd)(struct spmi_controller *ctrl, u8 opcode,
86 u8 sid, u16 addr, u8 *buf, size_t len);
87 int (*write_cmd)(struct spmi_controller *ctrl, u8 opcode,
88 u8 sid, u16 addr, const u8 *buf, size_t len);
89 ANDROID_KABI_RESERVE(1);
90 };
91
to_spmi_controller(struct device * d)92 static inline struct spmi_controller *to_spmi_controller(struct device *d)
93 {
94 return container_of(d, struct spmi_controller, dev);
95 }
96
97 static inline
spmi_controller_get_drvdata(const struct spmi_controller * ctrl)98 void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl)
99 {
100 return dev_get_drvdata(&ctrl->dev);
101 }
102
spmi_controller_set_drvdata(struct spmi_controller * ctrl,void * data)103 static inline void spmi_controller_set_drvdata(struct spmi_controller *ctrl,
104 void *data)
105 {
106 dev_set_drvdata(&ctrl->dev, data);
107 }
108
109 struct spmi_controller *spmi_controller_alloc(struct device *parent,
110 size_t size);
111
112 /**
113 * spmi_controller_put() - decrement controller refcount
114 * @ctrl SPMI controller.
115 */
spmi_controller_put(struct spmi_controller * ctrl)116 static inline void spmi_controller_put(struct spmi_controller *ctrl)
117 {
118 if (ctrl)
119 put_device(&ctrl->dev);
120 }
121
122 int spmi_controller_add(struct spmi_controller *ctrl);
123 void spmi_controller_remove(struct spmi_controller *ctrl);
124
125 /**
126 * struct spmi_driver - SPMI slave device driver
127 * @driver: SPMI device drivers should initialize name and owner field of
128 * this structure.
129 * @probe: binds this driver to a SPMI device.
130 * @remove: unbinds this driver from the SPMI device.
131 *
132 * If PM runtime support is desired for a slave, a device driver can call
133 * pm_runtime_put() from their probe() routine (and a balancing
134 * pm_runtime_get() in remove()). PM runtime support for a slave is
135 * implemented by issuing a SLEEP command to the slave on runtime_suspend(),
136 * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP
137 * command is sent to the slave to bring it back to ACTIVE.
138 */
139 struct spmi_driver {
140 struct device_driver driver;
141 int (*probe)(struct spmi_device *sdev);
142 void (*remove)(struct spmi_device *sdev);
143 ANDROID_KABI_RESERVE(1);
144 };
145
to_spmi_driver(struct device_driver * d)146 static inline struct spmi_driver *to_spmi_driver(struct device_driver *d)
147 {
148 return container_of(d, struct spmi_driver, driver);
149 }
150
151 #define spmi_driver_register(sdrv) \
152 __spmi_driver_register(sdrv, THIS_MODULE)
153 int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner);
154
155 /**
156 * spmi_driver_unregister() - unregister an SPMI client driver
157 * @sdrv: the driver to unregister
158 */
spmi_driver_unregister(struct spmi_driver * sdrv)159 static inline void spmi_driver_unregister(struct spmi_driver *sdrv)
160 {
161 if (sdrv)
162 driver_unregister(&sdrv->driver);
163 }
164
165 #define module_spmi_driver(__spmi_driver) \
166 module_driver(__spmi_driver, spmi_driver_register, \
167 spmi_driver_unregister)
168
169 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf);
170 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf,
171 size_t len);
172 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf,
173 size_t len);
174 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data);
175 int spmi_register_zero_write(struct spmi_device *sdev, u8 data);
176 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr,
177 const u8 *buf, size_t len);
178 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr,
179 const u8 *buf, size_t len);
180 int spmi_command_reset(struct spmi_device *sdev);
181 int spmi_command_sleep(struct spmi_device *sdev);
182 int spmi_command_wakeup(struct spmi_device *sdev);
183 int spmi_command_shutdown(struct spmi_device *sdev);
184
185 #endif
186