1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2018 Cadence Design Systems Inc. 4 * 5 * Author: Boris Brezillon <boris.brezillon@bootlin.com> 6 */ 7 8 #ifndef I3C_DEV_H 9 #define I3C_DEV_H 10 11 #include <linux/bitops.h> 12 #include <linux/device.h> 13 #include <linux/i2c.h> 14 #include <linux/kconfig.h> 15 #include <linux/device-id/i3c.h> 16 #include <linux/module.h> 17 18 /** 19 * enum i3c_error_code - I3C error codes 20 * 21 * @I3C_ERROR_UNKNOWN: unknown error, usually means the error is not I3C 22 * related 23 * @I3C_ERROR_M0: M0 error 24 * @I3C_ERROR_M1: M1 error 25 * @I3C_ERROR_M2: M2 error 26 * 27 * These are the standard error codes as defined by the I3C specification. 28 * When -EIO is returned by the i3c_device_do_i3c_xfers() or 29 * i3c_device_send_hdr_cmds() one can check the error code in 30 * &struct_i3c_xfer.err or &struct i3c_hdr_cmd.err to get a better idea of 31 * what went wrong. 32 * 33 */ 34 enum i3c_error_code { 35 I3C_ERROR_UNKNOWN = 0, 36 I3C_ERROR_M0 = 1, 37 I3C_ERROR_M1, 38 I3C_ERROR_M2, 39 }; 40 41 /** 42 * enum i3c_xfer_mode - I3C xfer mode ids 43 * @I3C_HDR_DDR: DDR mode 44 * @I3C_HDR_TSP: TSP mode 45 * @I3C_HDR_TSL: TSL mode 46 * @I3C_SDR: SDR mode (NOT HDR mode) 47 */ 48 enum i3c_xfer_mode { 49 /* The below 3 value (I3C_HDR*) must match GETCAP1 Byte bit position */ 50 I3C_HDR_DDR = 0, 51 I3C_HDR_TSP = 1, 52 I3C_HDR_TSL = 2, 53 /* Use for default SDR transfer mode */ 54 I3C_SDR = 31, 55 }; 56 57 /** 58 * struct i3c_xfer - I3C data transfer 59 * @rnw: encodes the transfer direction. true for a read, false for a write 60 * @cmd: Read/Write command in HDR mode, read: 0x80 - 0xff, write: 0x00 - 0x7f 61 * @len: transfer length in bytes of the transfer 62 * @actual_len: actual length in bytes are transferred by the controller 63 * @data: input/output buffer 64 * @data.in: input buffer. Must point to a DMA-able buffer 65 * @data.out: output buffer. Must point to a DMA-able buffer 66 * @err: I3C error code 67 */ 68 struct i3c_xfer { 69 union { 70 u8 rnw; 71 u8 cmd; 72 }; 73 u16 len; 74 u16 actual_len; 75 union { 76 void *in; 77 const void *out; 78 } data; 79 enum i3c_error_code err; 80 }; 81 82 /* For splitting 'cmd' from struct i3c_xfer */ 83 #define I3C_HDR_CMD_RNW BIT(7) 84 #define I3C_HDR_CMD_CODE GENMASK(6, 0) 85 86 /** 87 * enum i3c_dcr - I3C DCR values 88 * @I3C_DCR_GENERIC_DEVICE: generic I3C device 89 */ 90 enum i3c_dcr { 91 I3C_DCR_GENERIC_DEVICE = 0, 92 }; 93 94 #define I3C_PID_MANUF_ID(pid) (((pid) & GENMASK_ULL(47, 33)) >> 33) 95 #define I3C_PID_RND_LOWER_32BITS(pid) (!!((pid) & BIT_ULL(32))) 96 #define I3C_PID_RND_VAL(pid) ((pid) & GENMASK_ULL(31, 0)) 97 #define I3C_PID_PART_ID(pid) (((pid) & GENMASK_ULL(31, 16)) >> 16) 98 #define I3C_PID_INSTANCE_ID(pid) (((pid) & GENMASK_ULL(15, 12)) >> 12) 99 #define I3C_PID_EXTRA_INFO(pid) ((pid) & GENMASK_ULL(11, 0)) 100 101 #define I3C_BCR_DEVICE_ROLE(bcr) ((bcr) & GENMASK(7, 6)) 102 #define I3C_BCR_I3C_SLAVE (0 << 6) 103 #define I3C_BCR_I3C_MASTER (1 << 6) 104 #define I3C_BCR_HDR_CAP BIT(5) 105 #define I3C_BCR_BRIDGE BIT(4) 106 #define I3C_BCR_OFFLINE_CAP BIT(3) 107 #define I3C_BCR_IBI_PAYLOAD BIT(2) 108 #define I3C_BCR_IBI_REQ_CAP BIT(1) 109 #define I3C_BCR_MAX_DATA_SPEED_LIM BIT(0) 110 111 /** 112 * struct i3c_device_info - I3C device information 113 * @pid: Provisioned ID 114 * @bcr: Bus Characteristic Register 115 * @dcr: Device Characteristic Register 116 * @static_addr: static/I2C address 117 * @dyn_addr: dynamic address 118 * @hdr_cap: supported HDR modes 119 * @max_read_ds: max read speed information 120 * @max_write_ds: max write speed information 121 * @max_ibi_len: max IBI payload length 122 * @max_read_turnaround: max read turn-around time in micro-seconds 123 * @max_read_len: max private SDR read length in bytes 124 * @max_write_len: max private SDR write length in bytes 125 * 126 * These are all basic information that should be advertised by an I3C device. 127 * Some of them are optional depending on the device type and device 128 * capabilities. 129 * For each I3C slave attached to a master with 130 * i3c_master_add_i3c_dev_locked(), the core will send the relevant CCC command 131 * to retrieve these data. 132 */ 133 struct i3c_device_info { 134 u64 pid; 135 u8 bcr; 136 u8 dcr; 137 u8 static_addr; 138 u8 dyn_addr; 139 u8 hdr_cap; 140 u8 max_read_ds; 141 u8 max_write_ds; 142 u8 max_ibi_len; 143 u32 max_read_turnaround; 144 u16 max_read_len; 145 u16 max_write_len; 146 }; 147 148 /* 149 * I3C device internals are kept hidden from I3C device users. It's just 150 * simpler to refactor things when everything goes through getter/setters, and 151 * I3C device drivers should not have to worry about internal representation 152 * anyway. 153 */ 154 struct i3c_device; 155 156 /* These macros should be used to i3c_device_id entries. */ 157 #define I3C_MATCH_MANUF_AND_PART (I3C_MATCH_MANUF | I3C_MATCH_PART) 158 159 #define I3C_DEVICE(_manufid, _partid, _drvdata) \ 160 { \ 161 .match_flags = I3C_MATCH_MANUF_AND_PART, \ 162 .manuf_id = _manufid, \ 163 .part_id = _partid, \ 164 .data = _drvdata, \ 165 } 166 167 #define I3C_DEVICE_EXTRA_INFO(_manufid, _partid, _info, _drvdata) \ 168 { \ 169 .match_flags = I3C_MATCH_MANUF_AND_PART | \ 170 I3C_MATCH_EXTRA_INFO, \ 171 .manuf_id = _manufid, \ 172 .part_id = _partid, \ 173 .extra_info = _info, \ 174 .data = _drvdata, \ 175 } 176 177 #define I3C_CLASS(_dcr, _drvdata) \ 178 { \ 179 .match_flags = I3C_MATCH_DCR, \ 180 .dcr = _dcr, \ 181 } 182 183 /** 184 * struct i3c_driver - I3C device driver 185 * @driver: inherit from device_driver 186 * @probe: I3C device probe method 187 * @remove: I3C device remove method 188 * @id_table: I3C device match table. Will be used by the framework to decide 189 * which device to bind to this driver 190 */ 191 struct i3c_driver { 192 struct device_driver driver; 193 int (*probe)(struct i3c_device *dev); 194 void (*remove)(struct i3c_device *dev); 195 const struct i3c_device_id *id_table; 196 }; 197 198 #define drv_to_i3cdrv(__drv) container_of_const(__drv, struct i3c_driver, driver) 199 200 struct device *i3cdev_to_dev(struct i3c_device *i3cdev); 201 202 /** 203 * dev_to_i3cdev() - Returns the I3C device containing @dev 204 * @__dev: device object 205 * 206 * Return: a pointer to an I3C device object. 207 */ 208 #define dev_to_i3cdev(__dev) container_of_const(__dev, struct i3c_device, dev) 209 210 const struct i3c_device_id * 211 i3c_device_match_id(struct i3c_device *i3cdev, 212 const struct i3c_device_id *id_table); 213 214 static inline void i3cdev_set_drvdata(struct i3c_device *i3cdev, 215 void *data) 216 { 217 struct device *dev = i3cdev_to_dev(i3cdev); 218 219 dev_set_drvdata(dev, data); 220 } 221 222 static inline void *i3cdev_get_drvdata(struct i3c_device *i3cdev) 223 { 224 struct device *dev = i3cdev_to_dev(i3cdev); 225 226 return dev_get_drvdata(dev); 227 } 228 229 int i3c_driver_register_with_owner(struct i3c_driver *drv, 230 struct module *owner); 231 void i3c_driver_unregister(struct i3c_driver *drv); 232 233 #define i3c_driver_register(__drv) \ 234 i3c_driver_register_with_owner(__drv, THIS_MODULE) 235 236 /** 237 * module_i3c_driver() - Register a module providing an I3C driver 238 * @__drv: the I3C driver to register 239 * 240 * Provide generic init/exit functions that simply register/unregister an I3C 241 * driver. 242 * Should be used by any driver that does not require extra init/cleanup steps. 243 */ 244 #define module_i3c_driver(__drv) \ 245 module_driver(__drv, i3c_driver_register, i3c_driver_unregister) 246 247 /** 248 * i3c_i2c_driver_register() - Register an i2c and an i3c driver 249 * @i3cdrv: the I3C driver to register 250 * @i2cdrv: the I2C driver to register 251 * 252 * This function registers both @i2cdev and @i3cdev, and fails if one of these 253 * registrations fails. This is mainly useful for devices that support both I2C 254 * and I3C modes. 255 * Note that when CONFIG_I3C is not enabled, this function only registers the 256 * I2C driver. 257 * 258 * Return: 0 if both registrations succeeds, a negative error code otherwise. 259 */ 260 static __always_inline int i3c_i2c_driver_register(struct i3c_driver *i3cdrv, 261 struct i2c_driver *i2cdrv) 262 { 263 int ret; 264 265 ret = i2c_add_driver(i2cdrv); 266 if (ret || !IS_ENABLED(CONFIG_I3C)) 267 return ret; 268 269 ret = i3c_driver_register(i3cdrv); 270 if (ret) 271 i2c_del_driver(i2cdrv); 272 273 return ret; 274 } 275 276 /** 277 * i3c_i2c_driver_unregister() - Unregister an i2c and an i3c driver 278 * @i3cdrv: the I3C driver to register 279 * @i2cdrv: the I2C driver to register 280 * 281 * This function unregisters both @i3cdrv and @i2cdrv. 282 * Note that when CONFIG_I3C is not enabled, this function only unregisters the 283 * @i2cdrv. 284 */ 285 static __always_inline void i3c_i2c_driver_unregister(struct i3c_driver *i3cdrv, 286 struct i2c_driver *i2cdrv) 287 { 288 if (IS_ENABLED(CONFIG_I3C)) 289 i3c_driver_unregister(i3cdrv); 290 291 i2c_del_driver(i2cdrv); 292 } 293 294 /** 295 * module_i3c_i2c_driver() - Register a module providing an I3C and an I2C 296 * driver 297 * @__i3cdrv: the I3C driver to register 298 * @__i2cdrv: the I2C driver to register 299 * 300 * Provide generic init/exit functions that simply register/unregister an I3C 301 * and an I2C driver. 302 * This macro can be used even if CONFIG_I3C is disabled, in this case, only 303 * the I2C driver will be registered. 304 * Should be used by any driver that does not require extra init/cleanup steps. 305 */ 306 #define module_i3c_i2c_driver(__i3cdrv, __i2cdrv) \ 307 module_driver(__i3cdrv, \ 308 i3c_i2c_driver_register, \ 309 i3c_i2c_driver_unregister, \ 310 __i2cdrv) 311 312 #if IS_ENABLED(CONFIG_I3C) 313 int i3c_device_do_xfers(struct i3c_device *dev, struct i3c_xfer *xfers, 314 int nxfers, enum i3c_xfer_mode mode); 315 u32 i3c_device_get_supported_xfer_mode(struct i3c_device *dev); 316 #else 317 static inline int 318 i3c_device_do_xfers(struct i3c_device *dev, struct i3c_xfer *xfers, 319 int nxfers, enum i3c_xfer_mode mode) 320 { 321 return -EOPNOTSUPP; 322 } 323 324 static inline u32 i3c_device_get_supported_xfer_mode(struct i3c_device *dev) 325 { 326 return 0; 327 } 328 #endif 329 330 int i3c_device_do_setdasa(struct i3c_device *dev); 331 332 void i3c_device_get_info(const struct i3c_device *dev, struct i3c_device_info *info); 333 334 struct i3c_ibi_payload { 335 unsigned int len; 336 const void *data; 337 }; 338 339 /** 340 * struct i3c_ibi_setup - IBI setup object 341 * @max_payload_len: maximum length of the payload associated to an IBI. If one 342 * IBI appears to have a payload that is bigger than this 343 * number, the IBI will be rejected. 344 * @num_slots: number of pre-allocated IBI slots. This should be chosen so that 345 * the system never runs out of IBI slots, otherwise you'll lose 346 * IBIs. 347 * @handler: IBI handler, every time an IBI is received. This handler is called 348 * in a workqueue context. It is allowed to sleep and send new 349 * messages on the bus, though it's recommended to keep the 350 * processing done there as fast as possible to avoid delaying 351 * processing of other queued on the same workqueue. 352 * 353 * Temporary structure used to pass information to i3c_device_request_ibi(). 354 * This object can be allocated on the stack since i3c_device_request_ibi() 355 * copies every bit of information and do not use it after 356 * i3c_device_request_ibi() has returned. 357 */ 358 struct i3c_ibi_setup { 359 unsigned int max_payload_len; 360 unsigned int num_slots; 361 void (*handler)(struct i3c_device *dev, 362 const struct i3c_ibi_payload *payload); 363 }; 364 365 int i3c_device_request_ibi(struct i3c_device *dev, 366 const struct i3c_ibi_setup *setup); 367 void i3c_device_free_ibi(struct i3c_device *dev); 368 int i3c_device_enable_ibi(struct i3c_device *dev); 369 int i3c_device_disable_ibi(struct i3c_device *dev); 370 371 #endif /* I3C_DEV_H */ 372