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 11 /* Maximum slave identifier */ 12 #define SPMI_MAX_SLAVE_ID 16 13 14 /* SPMI Commands */ 15 #define SPMI_CMD_EXT_WRITE 0x00 16 #define SPMI_CMD_RESET 0x10 17 #define SPMI_CMD_SLEEP 0x11 18 #define SPMI_CMD_SHUTDOWN 0x12 19 #define SPMI_CMD_WAKEUP 0x13 20 #define SPMI_CMD_AUTHENTICATE 0x14 21 #define SPMI_CMD_MSTR_READ 0x15 22 #define SPMI_CMD_MSTR_WRITE 0x16 23 #define SPMI_CMD_TRANSFER_BUS_OWNERSHIP 0x1A 24 #define SPMI_CMD_DDB_MASTER_READ 0x1B 25 #define SPMI_CMD_DDB_SLAVE_READ 0x1C 26 #define SPMI_CMD_EXT_READ 0x20 27 #define SPMI_CMD_EXT_WRITEL 0x30 28 #define SPMI_CMD_EXT_READL 0x38 29 #define SPMI_CMD_WRITE 0x40 30 #define SPMI_CMD_READ 0x60 31 #define SPMI_CMD_ZERO_WRITE 0x80 32 33 /** 34 * struct spmi_device - Basic representation of an SPMI device 35 * @dev: Driver model representation of the device. 36 * @ctrl: SPMI controller managing the bus hosting this device. 37 * @usid: This devices' Unique Slave IDentifier. 38 */ 39 struct spmi_device { 40 struct device dev; 41 struct spmi_controller *ctrl; 42 u8 usid; 43 }; 44 45 static inline struct spmi_device *to_spmi_device(struct device *d) 46 { 47 return container_of(d, struct spmi_device, dev); 48 } 49 50 static inline void *spmi_device_get_drvdata(const struct spmi_device *sdev) 51 { 52 return dev_get_drvdata(&sdev->dev); 53 } 54 55 static inline void spmi_device_set_drvdata(struct spmi_device *sdev, void *data) 56 { 57 dev_set_drvdata(&sdev->dev, data); 58 } 59 60 struct spmi_device *spmi_device_alloc(struct spmi_controller *ctrl); 61 62 static inline void spmi_device_put(struct spmi_device *sdev) 63 { 64 if (sdev) 65 put_device(&sdev->dev); 66 } 67 68 int spmi_device_add(struct spmi_device *sdev); 69 70 void spmi_device_remove(struct spmi_device *sdev); 71 72 /** 73 * struct spmi_controller - interface to the SPMI master controller 74 * @dev: Driver model representation of the device. 75 * @nr: board-specific number identifier for this controller/bus 76 * @cmd: sends a non-data command sequence on the SPMI bus. 77 * @read_cmd: sends a register read command sequence on the SPMI bus. 78 * @write_cmd: sends a register write command sequence on the SPMI bus. 79 * @priv: array of private data. 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 u8 priv[]; 90 }; 91 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 98 void *spmi_controller_get_drvdata(const struct spmi_controller *ctrl) 99 { 100 return dev_get_drvdata(&ctrl->dev); 101 } 102 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 */ 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 struct spmi_controller *devm_spmi_controller_alloc(struct device *parent, size_t size); 126 int devm_spmi_controller_add(struct device *parent, struct spmi_controller *ctrl); 127 128 /** 129 * struct spmi_driver - SPMI slave device driver 130 * @driver: SPMI device drivers should initialize name and owner field of 131 * this structure. 132 * @probe: binds this driver to a SPMI device. 133 * @remove: unbinds this driver from the SPMI device. 134 * @shutdown: shuts down this driver. 135 * 136 * If PM runtime support is desired for a slave, a device driver can call 137 * pm_runtime_put() from their probe() routine (and a balancing 138 * pm_runtime_get() in remove()). PM runtime support for a slave is 139 * implemented by issuing a SLEEP command to the slave on runtime_suspend(), 140 * transitioning the slave into the SLEEP state. On runtime_resume(), a WAKEUP 141 * command is sent to the slave to bring it back to ACTIVE. 142 */ 143 struct spmi_driver { 144 struct device_driver driver; 145 int (*probe)(struct spmi_device *sdev); 146 void (*remove)(struct spmi_device *sdev); 147 void (*shutdown)(struct spmi_device *sdev); 148 }; 149 150 static inline struct spmi_driver *to_spmi_driver(struct device_driver *d) 151 { 152 return container_of(d, struct spmi_driver, driver); 153 } 154 155 #define spmi_driver_register(sdrv) \ 156 __spmi_driver_register(sdrv, THIS_MODULE) 157 int __spmi_driver_register(struct spmi_driver *sdrv, struct module *owner); 158 159 /** 160 * spmi_driver_unregister() - unregister an SPMI client driver 161 * @sdrv: the driver to unregister 162 */ 163 static inline void spmi_driver_unregister(struct spmi_driver *sdrv) 164 { 165 if (sdrv) 166 driver_unregister(&sdrv->driver); 167 } 168 169 #define module_spmi_driver(__spmi_driver) \ 170 module_driver(__spmi_driver, spmi_driver_register, \ 171 spmi_driver_unregister) 172 173 struct device_node; 174 175 struct spmi_device *spmi_find_device_by_of_node(struct device_node *np); 176 int spmi_register_read(struct spmi_device *sdev, u8 addr, u8 *buf); 177 int spmi_ext_register_read(struct spmi_device *sdev, u8 addr, u8 *buf, 178 size_t len); 179 int spmi_ext_register_readl(struct spmi_device *sdev, u16 addr, u8 *buf, 180 size_t len); 181 int spmi_register_write(struct spmi_device *sdev, u8 addr, u8 data); 182 int spmi_register_zero_write(struct spmi_device *sdev, u8 data); 183 int spmi_ext_register_write(struct spmi_device *sdev, u8 addr, 184 const u8 *buf, size_t len); 185 int spmi_ext_register_writel(struct spmi_device *sdev, u16 addr, 186 const u8 *buf, size_t len); 187 int spmi_command_reset(struct spmi_device *sdev); 188 int spmi_command_sleep(struct spmi_device *sdev); 189 int spmi_command_wakeup(struct spmi_device *sdev); 190 int spmi_command_shutdown(struct spmi_device *sdev); 191 192 #endif 193