device.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_xfers() - do I3C transfers directed to a specific device
  16. *
  17. * @dev: device with which the transfers should be done
  18. * @xfers: array of transfers
  19. * @nxfers: number of transfers
  20. * @mode: transfer mode
  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:
  27. * * 0 in case of success, a negative error core otherwise.
  28. * * -EAGAIN: controller lost address arbitration. Target (IBI, HJ or
  29. * controller role request) win the bus. Client driver needs to resend the
  30. * 'xfers' some time later. See I3C spec ver 1.1.1 09-Jun-2021. Section:
  31. * 5.1.2.2.3.
  32. */
  33. int i3c_device_do_xfers(struct i3c_device *dev, struct i3c_xfer *xfers,
  34. int nxfers, enum i3c_xfer_mode mode)
  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. ret = i3c_bus_rpm_get(dev->bus);
  44. if (ret)
  45. return ret;
  46. i3c_bus_normaluse_lock(dev->bus);
  47. ret = i3c_dev_do_xfers_locked(dev->desc, xfers, nxfers, mode);
  48. i3c_bus_normaluse_unlock(dev->bus);
  49. i3c_bus_rpm_put(dev->bus);
  50. return ret;
  51. }
  52. EXPORT_SYMBOL_GPL(i3c_device_do_xfers);
  53. /**
  54. * i3c_device_do_setdasa() - do I3C dynamic address assignement with
  55. * static address
  56. *
  57. * @dev: device with which the DAA should be done
  58. *
  59. * Return: 0 in case of success, a negative error core otherwise.
  60. */
  61. int i3c_device_do_setdasa(struct i3c_device *dev)
  62. {
  63. int ret;
  64. ret = i3c_bus_rpm_get(dev->bus);
  65. if (ret)
  66. return ret;
  67. i3c_bus_normaluse_lock(dev->bus);
  68. ret = i3c_dev_setdasa_locked(dev->desc);
  69. i3c_bus_normaluse_unlock(dev->bus);
  70. i3c_bus_rpm_put(dev->bus);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL_GPL(i3c_device_do_setdasa);
  74. /**
  75. * i3c_device_get_info() - get I3C device information
  76. *
  77. * @dev: device we want information on
  78. * @info: the information object to fill in
  79. *
  80. * Retrieve I3C dev info.
  81. */
  82. void i3c_device_get_info(const struct i3c_device *dev,
  83. struct i3c_device_info *info)
  84. {
  85. if (!info)
  86. return;
  87. i3c_bus_normaluse_lock(dev->bus);
  88. if (dev->desc)
  89. *info = dev->desc->info;
  90. i3c_bus_normaluse_unlock(dev->bus);
  91. }
  92. EXPORT_SYMBOL_GPL(i3c_device_get_info);
  93. /**
  94. * i3c_device_disable_ibi() - Disable IBIs coming from a specific device
  95. * @dev: device on which IBIs should be disabled
  96. *
  97. * This function disable IBIs coming from a specific device and wait for
  98. * all pending IBIs to be processed.
  99. *
  100. * Return: 0 in case of success, a negative error core otherwise.
  101. */
  102. int i3c_device_disable_ibi(struct i3c_device *dev)
  103. {
  104. int ret;
  105. if (i3c_bus_rpm_ibi_allowed(dev->bus)) {
  106. ret = i3c_bus_rpm_get(dev->bus);
  107. if (ret)
  108. return ret;
  109. }
  110. i3c_bus_normaluse_lock(dev->bus);
  111. if (dev->desc) {
  112. mutex_lock(&dev->desc->ibi_lock);
  113. ret = i3c_dev_disable_ibi_locked(dev->desc);
  114. mutex_unlock(&dev->desc->ibi_lock);
  115. } else {
  116. ret = -ENOENT;
  117. }
  118. i3c_bus_normaluse_unlock(dev->bus);
  119. if (!ret || i3c_bus_rpm_ibi_allowed(dev->bus))
  120. i3c_bus_rpm_put(dev->bus);
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(i3c_device_disable_ibi);
  124. /**
  125. * i3c_device_enable_ibi() - Enable IBIs coming from a specific device
  126. * @dev: device on which IBIs should be enabled
  127. *
  128. * This function enable IBIs coming from a specific device and wait for
  129. * all pending IBIs to be processed. This should be called on a device
  130. * where i3c_device_request_ibi() has succeeded.
  131. *
  132. * Note that IBIs from this device might be received before this function
  133. * returns to its caller.
  134. *
  135. * Return: 0 in case of success, a negative error core otherwise.
  136. */
  137. int i3c_device_enable_ibi(struct i3c_device *dev)
  138. {
  139. int ret;
  140. ret = i3c_bus_rpm_get(dev->bus);
  141. if (ret)
  142. return ret;
  143. i3c_bus_normaluse_lock(dev->bus);
  144. if (dev->desc) {
  145. mutex_lock(&dev->desc->ibi_lock);
  146. ret = i3c_dev_enable_ibi_locked(dev->desc);
  147. mutex_unlock(&dev->desc->ibi_lock);
  148. } else {
  149. ret = -ENOENT;
  150. }
  151. i3c_bus_normaluse_unlock(dev->bus);
  152. if (ret || i3c_bus_rpm_ibi_allowed(dev->bus))
  153. i3c_bus_rpm_put(dev->bus);
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(i3c_device_enable_ibi);
  157. /**
  158. * i3c_device_request_ibi() - Request an IBI
  159. * @dev: device for which we should enable IBIs
  160. * @req: setup requested for this IBI
  161. *
  162. * This function is responsible for pre-allocating all resources needed to
  163. * process IBIs coming from @dev. When this function returns, the IBI is not
  164. * enabled until i3c_device_enable_ibi() is called.
  165. *
  166. * Return: 0 in case of success, a negative error core otherwise.
  167. */
  168. int i3c_device_request_ibi(struct i3c_device *dev,
  169. const struct i3c_ibi_setup *req)
  170. {
  171. int ret;
  172. if (!req->handler || !req->num_slots)
  173. return -EINVAL;
  174. ret = i3c_bus_rpm_get(dev->bus);
  175. if (ret)
  176. return ret;
  177. i3c_bus_normaluse_lock(dev->bus);
  178. if (dev->desc) {
  179. mutex_lock(&dev->desc->ibi_lock);
  180. ret = i3c_dev_request_ibi_locked(dev->desc, req);
  181. mutex_unlock(&dev->desc->ibi_lock);
  182. } else {
  183. ret = -ENOENT;
  184. }
  185. i3c_bus_normaluse_unlock(dev->bus);
  186. i3c_bus_rpm_put(dev->bus);
  187. return ret;
  188. }
  189. EXPORT_SYMBOL_GPL(i3c_device_request_ibi);
  190. /**
  191. * i3c_device_free_ibi() - Free all resources needed for IBI handling
  192. * @dev: device on which you want to release IBI resources
  193. *
  194. * This function is responsible for de-allocating resources previously
  195. * allocated by i3c_device_request_ibi(). It should be called after disabling
  196. * IBIs with i3c_device_disable_ibi().
  197. */
  198. void i3c_device_free_ibi(struct i3c_device *dev)
  199. {
  200. i3c_bus_normaluse_lock(dev->bus);
  201. if (dev->desc) {
  202. mutex_lock(&dev->desc->ibi_lock);
  203. i3c_dev_free_ibi_locked(dev->desc);
  204. mutex_unlock(&dev->desc->ibi_lock);
  205. }
  206. i3c_bus_normaluse_unlock(dev->bus);
  207. }
  208. EXPORT_SYMBOL_GPL(i3c_device_free_ibi);
  209. /**
  210. * i3cdev_to_dev() - Returns the device embedded in @i3cdev
  211. * @i3cdev: I3C device
  212. *
  213. * Return: a pointer to a device object.
  214. */
  215. struct device *i3cdev_to_dev(struct i3c_device *i3cdev)
  216. {
  217. return &i3cdev->dev;
  218. }
  219. EXPORT_SYMBOL_GPL(i3cdev_to_dev);
  220. /**
  221. * i3c_device_match_id() - Returns the i3c_device_id entry matching @i3cdev
  222. * @i3cdev: I3C device
  223. * @id_table: I3C device match table
  224. *
  225. * Return: a pointer to an i3c_device_id object or NULL if there's no match.
  226. */
  227. const struct i3c_device_id *
  228. i3c_device_match_id(struct i3c_device *i3cdev,
  229. const struct i3c_device_id *id_table)
  230. {
  231. struct i3c_device_info devinfo;
  232. const struct i3c_device_id *id;
  233. u16 manuf, part, ext_info;
  234. bool rndpid;
  235. i3c_device_get_info(i3cdev, &devinfo);
  236. manuf = I3C_PID_MANUF_ID(devinfo.pid);
  237. part = I3C_PID_PART_ID(devinfo.pid);
  238. ext_info = I3C_PID_EXTRA_INFO(devinfo.pid);
  239. rndpid = I3C_PID_RND_LOWER_32BITS(devinfo.pid);
  240. for (id = id_table; id->match_flags != 0; id++) {
  241. if ((id->match_flags & I3C_MATCH_DCR) &&
  242. id->dcr != devinfo.dcr)
  243. continue;
  244. if ((id->match_flags & I3C_MATCH_MANUF) &&
  245. id->manuf_id != manuf)
  246. continue;
  247. if ((id->match_flags & I3C_MATCH_PART) &&
  248. (rndpid || id->part_id != part))
  249. continue;
  250. if ((id->match_flags & I3C_MATCH_EXTRA_INFO) &&
  251. (rndpid || id->extra_info != ext_info))
  252. continue;
  253. return id;
  254. }
  255. return NULL;
  256. }
  257. EXPORT_SYMBOL_GPL(i3c_device_match_id);
  258. /**
  259. * i3c_device_get_supported_xfer_mode - Returns the supported transfer mode by
  260. * connected master controller.
  261. * @dev: I3C device
  262. *
  263. * Return: a bit mask, which supported transfer mode, bit position is defined at
  264. * enum i3c_hdr_mode
  265. */
  266. u32 i3c_device_get_supported_xfer_mode(struct i3c_device *dev)
  267. {
  268. return i3c_dev_get_master(dev->desc)->this->info.hdr_cap | BIT(I3C_SDR);
  269. }
  270. EXPORT_SYMBOL_GPL(i3c_device_get_supported_xfer_mode);
  271. /**
  272. * i3c_driver_register_with_owner() - register an I3C device driver
  273. *
  274. * @drv: driver to register
  275. * @owner: module that owns this driver
  276. *
  277. * Register @drv to the core.
  278. *
  279. * Return: 0 in case of success, a negative error core otherwise.
  280. */
  281. int i3c_driver_register_with_owner(struct i3c_driver *drv, struct module *owner)
  282. {
  283. drv->driver.owner = owner;
  284. drv->driver.bus = &i3c_bus_type;
  285. if (!drv->probe) {
  286. pr_err("Trying to register an i3c driver without probe callback\n");
  287. return -EINVAL;
  288. }
  289. return driver_register(&drv->driver);
  290. }
  291. EXPORT_SYMBOL_GPL(i3c_driver_register_with_owner);
  292. /**
  293. * i3c_driver_unregister() - unregister an I3C device driver
  294. *
  295. * @drv: driver to unregister
  296. *
  297. * Unregister @drv.
  298. */
  299. void i3c_driver_unregister(struct i3c_driver *drv)
  300. {
  301. driver_unregister(&drv->driver);
  302. }
  303. EXPORT_SYMBOL_GPL(i3c_driver_unregister);