device.h 9.8 KB

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