i2c_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /****************************************************************************
  2. * drivers/i2c/i2c_driver.c
  3. *
  4. * Copyright (C) 2016-2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdbool.h>
  41. #include <stdio.h>
  42. #include <semaphore.h>
  43. #include <string.h>
  44. #include <errno.h>
  45. #include <debug.h>
  46. #include <nuttx/kmalloc.h>
  47. #include <nuttx/fs/fs.h>
  48. #include <nuttx/i2c/i2c_master.h>
  49. #ifdef CONFIG_I2C_DRIVER
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. /* Device naming ************************************************************/
  54. #define DEVNAME_FMT "/dev/i2c%d"
  55. #define DEVNAME_FMTLEN (8 + 3 + 1)
  56. /****************************************************************************
  57. * Private Types
  58. ****************************************************************************/
  59. /* Driver state structure */
  60. struct i2c_driver_s
  61. {
  62. FAR struct i2c_master_s *i2c; /* Contained I2C lower half driver */
  63. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  64. sem_t exclsem; /* Mutual exclusion */
  65. int16_t crefs; /* Number of open references */
  66. bool unlinked; /* True, driver has been unlinked */
  67. #endif
  68. };
  69. /****************************************************************************
  70. * Private Function Prototypes
  71. ****************************************************************************/
  72. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  73. static int i2cdrvr_open(FAR struct file *filep);
  74. static int i2cdrvr_close(FAR struct file *filep);
  75. #endif
  76. static ssize_t i2cdrvr_read(FAR struct file *filep, FAR char *buffer,
  77. size_t buflen);
  78. static ssize_t i2cdrvr_write(FAR struct file *filep, FAR const char *buffer,
  79. size_t buflen);
  80. static int i2cdrvr_ioctl(FAR struct file *filep, int cmd,
  81. unsigned long arg);
  82. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  83. static int i2cdrvr_unlink(FAR struct inode *inode);
  84. #endif
  85. /****************************************************************************
  86. * Private Data
  87. ****************************************************************************/
  88. static const struct file_operations i2cdrvr_fops =
  89. {
  90. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  91. i2cdrvr_open, /* open */
  92. i2cdrvr_close, /* close */
  93. #else
  94. NULL, /* open */
  95. NULL, /* close */
  96. #endif
  97. i2cdrvr_read, /* read */
  98. i2cdrvr_write, /* write */
  99. NULL, /* seek */
  100. i2cdrvr_ioctl, /* ioctl */
  101. NULL /* poll */
  102. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  103. , i2cdrvr_unlink /* unlink */
  104. #endif
  105. };
  106. /****************************************************************************
  107. * Private Functions
  108. ****************************************************************************/
  109. /****************************************************************************
  110. * Name: i2cdrvr_open
  111. ****************************************************************************/
  112. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  113. static int i2cdrvr_open(FAR struct file *filep)
  114. {
  115. FAR struct inode *inode;
  116. FAR struct i2c_driver_s *priv;
  117. int ret;
  118. /* Get our private data structure */
  119. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  120. inode = filep->f_inode;
  121. priv = (FAR struct i2c_driver_s *)inode->i_private;
  122. DEBUGASSERT(priv);
  123. /* Get exclusive access to the I2C driver state structure */
  124. ret = nxsem_wait(&priv->exclsem);
  125. if (ret < 0)
  126. {
  127. return ret;
  128. }
  129. /* Increment the count of open references on the driver */
  130. priv->crefs++;
  131. DEBUGASSERT(priv->crefs > 0);
  132. nxsem_post(&priv->exclsem);
  133. return OK;
  134. }
  135. #endif
  136. /****************************************************************************
  137. * Name: i2cdrvr_close
  138. ****************************************************************************/
  139. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  140. static int i2cdrvr_close(FAR struct file *filep)
  141. {
  142. FAR struct inode *inode;
  143. FAR struct i2c_driver_s *priv;
  144. int ret;
  145. /* Get our private data structure */
  146. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  147. inode = filep->f_inode;
  148. priv = (FAR struct i2c_driver_s *)inode->i_private;
  149. DEBUGASSERT(priv);
  150. /* Get exclusive access to the I2C driver state structure */
  151. ret = nxsem_wait(&priv->exclsem);
  152. if (ret < 0)
  153. {
  154. return ret;
  155. }
  156. /* Decrement the count of open references on the driver */
  157. DEBUGASSERT(priv->crefs > 0);
  158. priv->crefs--;
  159. /* If the count has decremented to zero and the driver has been unlinked,
  160. * then commit Hara-Kiri now.
  161. */
  162. if (priv->crefs <= 0 && priv->unlinked)
  163. {
  164. nxsem_destroy(&priv->exclsem);
  165. kmm_free(priv);
  166. return OK;
  167. }
  168. nxsem_post(&priv->exclsem);
  169. return OK;
  170. }
  171. #endif
  172. /****************************************************************************
  173. * Name: i2cdrvr_read
  174. ****************************************************************************/
  175. static ssize_t i2cdrvr_read(FAR struct file *filep, FAR char *buffer,
  176. size_t len)
  177. {
  178. return 0; /* Return EOF */
  179. }
  180. /****************************************************************************
  181. * Name: i2cdrvr_write
  182. ****************************************************************************/
  183. static ssize_t i2cdrvr_write(FAR struct file *filep, FAR const char *buffer,
  184. size_t len)
  185. {
  186. return len; /* Say that everything was written */
  187. }
  188. /****************************************************************************
  189. * Name: i2cdrvr_ioctl
  190. ****************************************************************************/
  191. static int i2cdrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  192. {
  193. FAR struct inode *inode;
  194. FAR struct i2c_driver_s *priv;
  195. FAR struct i2c_transfer_s *transfer;
  196. int ret;
  197. i2cinfo("cmd=%x arg=%08x\n", cmd, arg);
  198. /* Get our private data structure */
  199. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  200. inode = filep->f_inode;
  201. priv = (FAR struct i2c_driver_s *)inode->i_private;
  202. DEBUGASSERT(priv);
  203. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  204. /* Get exclusive access to the I2C driver state structure */
  205. ret = nxsem_wait(&priv->exclsem);
  206. if (ret < 0)
  207. {
  208. return ret;
  209. }
  210. #endif
  211. /* Process the IOCTL command */
  212. switch (cmd)
  213. {
  214. /* Command: I2CIOC_TRANSFER
  215. * Description: Perform an I2C transfer
  216. * Argument: A reference to an instance of struct i2c_transfer_s.
  217. * Dependencies: CONFIG_I2C_DRIVER
  218. */
  219. case I2CIOC_TRANSFER:
  220. {
  221. /* Get the reference to the i2c_transfer_s structure */
  222. transfer = (FAR struct i2c_transfer_s *)((uintptr_t)arg);
  223. DEBUGASSERT(transfer != NULL);
  224. /* Perform the transfer */
  225. ret = I2C_TRANSFER(priv->i2c, transfer->msgv, transfer->msgc);
  226. }
  227. break;
  228. #ifdef CONFIG_I2C_RESET
  229. /* Command: I2CIOC_RESET
  230. * Description: Perform an I2C bus reset in an attempt to break loose
  231. * stuck I2C devices.
  232. * Argument: None
  233. * Dependencies: CONFIG_I2C_DRIVER && CONFIG_I2C_RESET
  234. */
  235. case I2CIOC_RESET:
  236. {
  237. ret = I2C_RESET(priv->i2c);
  238. }
  239. break;
  240. #endif
  241. default:
  242. ret = -ENOTTY;
  243. break;
  244. }
  245. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  246. nxsem_post(&priv->exclsem);
  247. #endif
  248. return ret;
  249. }
  250. /****************************************************************************
  251. * Name: i2cdrvr_unlink
  252. ****************************************************************************/
  253. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  254. static int i2cdrvr_unlink(FAR struct inode *inode)
  255. {
  256. FAR struct i2c_driver_s *priv;
  257. int ret;
  258. /* Get our private data structure */
  259. DEBUGASSERT(inode != NULL && inode->i_private != NULL);
  260. priv = (FAR struct i2c_driver_s *)inode->i_private;
  261. /* Get exclusive access to the I2C driver state structure */
  262. ret = nxsem_wait(&priv->exclsem);
  263. if (ret < 0)
  264. {
  265. return ret;
  266. }
  267. /* Are there open references to the driver data structure? */
  268. if (priv->crefs <= 0)
  269. {
  270. nxsem_destroy(&priv->exclsem);
  271. kmm_free(priv);
  272. return OK;
  273. }
  274. /* No... just mark the driver as unlinked and free the resouces when the
  275. * last client closes their reference to the driver.
  276. */
  277. priv->unlinked = true;
  278. nxsem_post(&priv->exclsem);
  279. return ret;
  280. }
  281. #endif
  282. /****************************************************************************
  283. * Public Functions
  284. ****************************************************************************/
  285. /****************************************************************************
  286. * Name: i2c_register
  287. *
  288. * Description:
  289. * Create and register the I2C character driver.
  290. *
  291. * The I2C character driver is a simple character driver that supports I2C
  292. * transfers. The intent of this driver is to support I2C testing. It is
  293. * not suitable for use in any real driver application.
  294. *
  295. * Input Parameters:
  296. * i2c - An instance of the lower half I2C driver
  297. * bus - The I2C bus number. This will be used as the I2C device minor
  298. * number. The I2C character device will be registered as /dev/i2cN
  299. * where N is the minor number
  300. *
  301. * Returned Value:
  302. * OK if the driver was successfully register; A negated errno value is
  303. * returned on any failure.
  304. *
  305. ****************************************************************************/
  306. int i2c_register(FAR struct i2c_master_s *i2c, int bus)
  307. {
  308. FAR struct i2c_driver_s *priv;
  309. char devname[DEVNAME_FMTLEN];
  310. int ret;
  311. /* Sanity check */
  312. DEBUGASSERT(i2c != NULL && (unsigned)bus < 1000);
  313. /* Allocate a I2C character device structure */
  314. priv = (FAR struct i2c_driver_s *)kmm_zalloc(sizeof(struct i2c_driver_s));
  315. if (priv)
  316. {
  317. /* Initialize the I2C character device structure */
  318. priv->i2c = i2c;
  319. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  320. nxsem_init(&priv->exclsem, 0, 1);
  321. #endif
  322. /* Create the character device name */
  323. snprintf(devname, DEVNAME_FMTLEN, DEVNAME_FMT, bus);
  324. ret = register_driver(devname, &i2cdrvr_fops, 0666, priv);
  325. if (ret < 0)
  326. {
  327. /* Free the device structure if we failed to create the character
  328. * device.
  329. */
  330. kmm_free(priv);
  331. return ret;
  332. }
  333. /* Return the result of the registration */
  334. return OK;
  335. }
  336. return -ENOMEM;
  337. }
  338. #endif /* CONFIG_I2C_DRIVER */