device.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #include <linux/atomic.h>
  8. #include <linux/bug.h>
  9. #include <linux/completion.h>
  10. #include <linux/device.h>
  11. #include <linux/mutex.h>
  12. #include <linux/slab.h>
  13. #include "internals.h"
  14. /**
  15. * i3c_device_do_priv_xfers() - do I3C SDR private transfers directed to a
  16. * specific device
  17. *
  18. * @dev: device with which the transfers should be done
  19. * @xfers: array of transfers
  20. * @nxfers: number of transfers
  21. *
  22. * Initiate one or several private SDR transfers with @dev.
  23. *
  24. * This function can sleep and thus cannot be called in atomic context.
  25. *
  26. * Return: 0 in case of success, a negative error core otherwise.
  27. * -EAGAIN: controller lost address arbitration. Target
  28. * (IBI, HJ or controller role request) win the bus. Client
  29. * driver needs to resend the 'xfers' some time later.
  30. * See I3C spec ver 1.1.1 09-Jun-2021. Section: 5.1.2.2.3.
  31. */
  32. int i3c_device_do_priv_xfers(struct i3c_device *dev,
  33. struct i3c_priv_xfer *xfers,
  34. int nxfers)
  35. {
  36. int ret, i;
  37. if (nxfers < 1)
  38. return 0;
  39. for (i = 0; i < nxfers; i++) {
  40. if (!xfers[i].len || !xfers[i].data.in)
  41. return -EINVAL;
  42. }
  43. i3c_bus_normaluse_lock(dev->bus);
  44. ret = i3c_dev_do_priv_xfers_locked(dev->desc, xfers, nxfers);
  45. i3c_bus_normaluse_unlock(dev->bus);
  46. return ret;
  47. }
  48. EXPORT_SYMBOL_GPL(i3c_device_do_priv_xfers);
  49. /**
  50. * i3c_device_do_setdasa() - do I3C dynamic address assignement with
  51. * static address
  52. *
  53. * @dev: device with which the DAA should be done
  54. *
  55. * Return: 0 in case of success, a negative error core otherwise.
  56. */
  57. int i3c_device_do_setdasa(struct i3c_device *dev)
  58. {
  59. int ret;
  60. i3c_bus_normaluse_lock(dev->bus);
  61. ret = i3c_dev_setdasa_locked(dev->desc);
  62. i3c_bus_normaluse_unlock(dev->bus);
  63. return ret;
  64. }
  65. EXPORT_SYMBOL_GPL(i3c_device_do_setdasa);
  66. /**
  67. * i3c_device_get_info() - get I3C device information
  68. *
  69. * @dev: device we want information on
  70. * @info: the information object to fill in
  71. *
  72. * Retrieve I3C dev info.
  73. */
  74. void i3c_device_get_info(const struct i3c_device *dev,
  75. struct i3c_device_info *info)
  76. {
  77. if (!info)
  78. return;
  79. i3c_bus_normaluse_lock(dev->bus);
  80. if (dev->desc)
  81. *info = dev->desc->info;
  82. i3c_bus_normaluse_unlock(dev->bus);
  83. }
  84. EXPORT_SYMBOL_GPL(i3c_device_get_info);
  85. /**
  86. * i3c_device_disable_ibi() - Disable IBIs coming from a specific device
  87. * @dev: device on which IBIs should be disabled
  88. *
  89. * This function disable IBIs coming from a specific device and wait for
  90. * all pending IBIs to be processed.
  91. *
  92. * Return: 0 in case of success, a negative error core otherwise.
  93. */
  94. int i3c_device_disable_ibi(struct i3c_device *dev)
  95. {
  96. int ret = -ENOENT;
  97. i3c_bus_normaluse_lock(dev->bus);
  98. if (dev->desc) {
  99. mutex_lock(&dev->desc->ibi_lock);
  100. ret = i3c_dev_disable_ibi_locked(dev->desc);
  101. mutex_unlock(&dev->desc->ibi_lock);
  102. }
  103. i3c_bus_normaluse_unlock(dev->bus);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(i3c_device_disable_ibi);
  107. /**
  108. * i3c_device_enable_ibi() - Enable IBIs coming from a specific device
  109. * @dev: device on which IBIs should be enabled
  110. *
  111. * This function enable IBIs coming from a specific device and wait for
  112. * all pending IBIs to be processed. This should be called on a device
  113. * where i3c_device_request_ibi() has succeeded.
  114. *
  115. * Note that IBIs from this device might be received before this function
  116. * returns to its caller.
  117. *
  118. * Return: 0 in case of success, a negative error core otherwise.
  119. */
  120. int i3c_device_enable_ibi(struct i3c_device *dev)
  121. {
  122. int ret = -ENOENT;
  123. i3c_bus_normaluse_lock(dev->bus);
  124. if (dev->desc) {
  125. mutex_lock(&dev->desc->ibi_lock);
  126. ret = i3c_dev_enable_ibi_locked(dev->desc);
  127. mutex_unlock(&dev->desc->ibi_lock);
  128. }
  129. i3c_bus_normaluse_unlock(dev->bus);
  130. return ret;
  131. }
  132. EXPORT_SYMBOL_GPL(i3c_device_enable_ibi);
  133. /**
  134. * i3c_device_request_ibi() - Request an IBI
  135. * @dev: device for which we should enable IBIs
  136. * @req: setup requested for this IBI
  137. *
  138. * This function is responsible for pre-allocating all resources needed to
  139. * process IBIs coming from @dev. When this function returns, the IBI is not
  140. * enabled until i3c_device_enable_ibi() is called.
  141. *
  142. * Return: 0 in case of success, a negative error core otherwise.
  143. */
  144. int i3c_device_request_ibi(struct i3c_device *dev,
  145. const struct i3c_ibi_setup *req)
  146. {
  147. int ret = -ENOENT;
  148. if (!req->handler || !req->num_slots)
  149. return -EINVAL;
  150. i3c_bus_normaluse_lock(dev->bus);
  151. if (dev->desc) {
  152. mutex_lock(&dev->desc->ibi_lock);
  153. ret = i3c_dev_request_ibi_locked(dev->desc, req);
  154. mutex_unlock(&dev->desc->ibi_lock);
  155. }
  156. i3c_bus_normaluse_unlock(dev->bus);
  157. return ret;
  158. }
  159. EXPORT_SYMBOL_GPL(i3c_device_request_ibi);
  160. /**
  161. * i3c_device_free_ibi() - Free all resources needed for IBI handling
  162. * @dev: device on which you want to release IBI resources
  163. *
  164. * This function is responsible for de-allocating resources previously
  165. * allocated by i3c_device_request_ibi(). It should be called after disabling
  166. * IBIs with i3c_device_disable_ibi().
  167. */
  168. void i3c_device_free_ibi(struct i3c_device *dev)
  169. {
  170. i3c_bus_normaluse_lock(dev->bus);
  171. if (dev->desc) {
  172. mutex_lock(&dev->desc->ibi_lock);
  173. i3c_dev_free_ibi_locked(dev->desc);
  174. mutex_unlock(&dev->desc->ibi_lock);
  175. }
  176. i3c_bus_normaluse_unlock(dev->bus);
  177. }
  178. EXPORT_SYMBOL_GPL(i3c_device_free_ibi);
  179. /**
  180. * i3cdev_to_dev() - Returns the device embedded in @i3cdev
  181. * @i3cdev: I3C device
  182. *
  183. * Return: a pointer to a device object.
  184. */
  185. struct device *i3cdev_to_dev(struct i3c_device *i3cdev)
  186. {
  187. return &i3cdev->dev;
  188. }
  189. EXPORT_SYMBOL_GPL(i3cdev_to_dev);
  190. /**
  191. * i3c_device_match_id() - Returns the i3c_device_id entry matching @i3cdev
  192. * @i3cdev: I3C device
  193. * @id_table: I3C device match table
  194. *
  195. * Return: a pointer to an i3c_device_id object or NULL if there's no match.
  196. */
  197. const struct i3c_device_id *
  198. i3c_device_match_id(struct i3c_device *i3cdev,
  199. const struct i3c_device_id *id_table)
  200. {
  201. struct i3c_device_info devinfo;
  202. const struct i3c_device_id *id;
  203. u16 manuf, part, ext_info;
  204. bool rndpid;
  205. i3c_device_get_info(i3cdev, &devinfo);
  206. manuf = I3C_PID_MANUF_ID(devinfo.pid);
  207. part = I3C_PID_PART_ID(devinfo.pid);
  208. ext_info = I3C_PID_EXTRA_INFO(devinfo.pid);
  209. rndpid = I3C_PID_RND_LOWER_32BITS(devinfo.pid);
  210. for (id = id_table; id->match_flags != 0; id++) {
  211. if ((id->match_flags & I3C_MATCH_DCR) &&
  212. id->dcr != devinfo.dcr)
  213. continue;
  214. if ((id->match_flags & I3C_MATCH_MANUF) &&
  215. id->manuf_id != manuf)
  216. continue;
  217. if ((id->match_flags & I3C_MATCH_PART) &&
  218. (rndpid || id->part_id != part))
  219. continue;
  220. if ((id->match_flags & I3C_MATCH_EXTRA_INFO) &&
  221. (rndpid || id->extra_info != ext_info))
  222. continue;
  223. return id;
  224. }
  225. return NULL;
  226. }
  227. EXPORT_SYMBOL_GPL(i3c_device_match_id);
  228. /**
  229. * i3c_driver_register_with_owner() - register an I3C device driver
  230. *
  231. * @drv: driver to register
  232. * @owner: module that owns this driver
  233. *
  234. * Register @drv to the core.
  235. *
  236. * Return: 0 in case of success, a negative error core otherwise.
  237. */
  238. int i3c_driver_register_with_owner(struct i3c_driver *drv, struct module *owner)
  239. {
  240. drv->driver.owner = owner;
  241. drv->driver.bus = &i3c_bus_type;
  242. if (!drv->probe) {
  243. pr_err("Trying to register an i3c driver without probe callback\n");
  244. return -EINVAL;
  245. }
  246. return driver_register(&drv->driver);
  247. }
  248. EXPORT_SYMBOL_GPL(i3c_driver_register_with_owner);
  249. /**
  250. * i3c_driver_unregister() - unregister an I3C device driver
  251. *
  252. * @drv: driver to unregister
  253. *
  254. * Unregister @drv.
  255. */
  256. void i3c_driver_unregister(struct i3c_driver *drv)
  257. {
  258. driver_unregister(&drv->driver);
  259. }
  260. EXPORT_SYMBOL_GPL(i3c_driver_unregister);