device.c 6.9 KB

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