i2c_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /****************************************************************************
  2. * drivers/i2c/i2c_driver.c
  3. *
  4. * Copyright (C) 2016 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. 0, /* open */
  95. 0, /* close */
  96. #endif
  97. i2cdrvr_read, /* read */
  98. i2cdrvr_write, /* write */
  99. 0, /* seek */
  100. i2cdrvr_ioctl /* ioctl */
  101. #ifndef CONFIG_DISABLE_POLL
  102. , 0 /* poll */
  103. #endif
  104. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  105. , i2cdrvr_unlink /* unlink */
  106. #endif
  107. };
  108. /****************************************************************************
  109. * Private Functions
  110. ****************************************************************************/
  111. /****************************************************************************
  112. * Name: i2cdrvr_open
  113. ****************************************************************************/
  114. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  115. static int i2cdrvr_open(FAR struct file *filep)
  116. {
  117. FAR struct inode *inode = filep->f_inode;
  118. FAR struct i2c_driver_s *priv;
  119. int ret;
  120. /* Get our private data structure */
  121. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  122. inode = filep->f_inode;
  123. priv = (FAR struct i2c_driver_s *)inode->i_private;
  124. DEBUGASSERT(priv);
  125. /* Get exclusive access to the I2C driver state structure */
  126. ret = sem_wait(&priv->exclsem);
  127. if (ret < 0)
  128. {
  129. int errcode = errno;
  130. DEBUGASSERT(errcode < 0);
  131. return -errcode;
  132. }
  133. /* Increment the count of open references on the RTC driver */
  134. priv->crefs++;
  135. DEBUGASSERT(priv->crefs > 0);
  136. sem_post(&priv->exclsem);
  137. return OK;
  138. }
  139. #endif
  140. /****************************************************************************
  141. * Name: i2cdrvr_close
  142. ****************************************************************************/
  143. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  144. static int i2cdrvr_close(FAR struct file *filep)
  145. {
  146. FAR struct inode *inode = filep->f_inode;
  147. FAR struct i2c_driver_s *priv;
  148. int ret;
  149. /* Get our private data structure */
  150. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  151. inode = filep->f_inode;
  152. priv = (FAR struct i2c_driver_s *)inode->i_private;
  153. DEBUGASSERT(priv);
  154. /* Get exclusive access to the I2C driver state structure */
  155. ret = sem_wait(&priv->exclsem);
  156. if (ret < 0)
  157. {
  158. int errcode = errno;
  159. DEBUGASSERT(errcode < 0);
  160. return -errcode;
  161. }
  162. /* Decrement the count of open references on the RTC driver */
  163. DEBUGASSERT(priv->crefs > 0);
  164. priv->crefs--;
  165. /* If the count has decremented to zero and the driver has been unlinked,
  166. * then commit Hara-Kiri now.
  167. */
  168. if (priv->crefs <= 0 && priv->unlinked)
  169. {
  170. sem_destroy(&priv->exclsem);
  171. kmm_free(priv);
  172. return OK;
  173. }
  174. sem_post(&priv->exclsem);
  175. return OK;
  176. }
  177. #endif
  178. /****************************************************************************
  179. * Name: i2cdrvr_read
  180. ****************************************************************************/
  181. static ssize_t i2cdrvr_read(FAR struct file *filep, FAR char *buffer,
  182. size_t len)
  183. {
  184. return 0; /* Return EOF */
  185. }
  186. /****************************************************************************
  187. * Name: i2cdrvr_write
  188. ****************************************************************************/
  189. static ssize_t i2cdrvr_write(FAR struct file *filep, FAR const char *buffer,
  190. size_t len)
  191. {
  192. return len; /* Say that everything was written */
  193. }
  194. /****************************************************************************
  195. * Name: i2cdrvr_ioctl
  196. ****************************************************************************/
  197. static int i2cdrvr_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  198. {
  199. FAR struct inode *inode = filep->f_inode;
  200. FAR struct i2c_driver_s *priv;
  201. FAR struct i2c_transfer_s *transfer;
  202. int ret;
  203. i2cinfo("cmd=%d arg=%lu\n", cmd, arg);
  204. /* Get our private data structure */
  205. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  206. inode = filep->f_inode;
  207. priv = (FAR struct i2c_driver_s *)inode->i_private;
  208. DEBUGASSERT(priv);
  209. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  210. /* Get exclusive access to the I2C driver state structure */
  211. ret = sem_wait(&priv->exclsem);
  212. if (ret < 0)
  213. {
  214. int errcode = errno;
  215. DEBUGASSERT(errcode < 0);
  216. return -errcode;
  217. }
  218. #endif
  219. /* Process the IOCTL command */
  220. switch (cmd)
  221. {
  222. /* Command: I2CIOC_TRANSFER
  223. * Description: Perform an I2C transfer
  224. * Argument: A reference to an instance of struct i2c_transfer_s.
  225. * Dependencies: CONFIG_I2C_DRIVER
  226. */
  227. case I2CIOC_TRANSFER:
  228. {
  229. /* Get the reference to the i2c_transfer_s structure */
  230. transfer = (FAR struct i2c_transfer_s *)((uintptr_t)arg);
  231. DEBUGASSERT(transfer != NULL);
  232. /* Perform the transfer */
  233. ret = I2C_TRANSFER(priv->i2c, transfer->msgv, transfer->msgc);
  234. }
  235. break;
  236. #ifdef CONFIG_I2C_RESET
  237. /* Command: I2CIOC_RESET
  238. * Description: Perform an I2C bus reset in an attempt to break loose
  239. * stuck I2C devices.
  240. * Argument: None
  241. * Dependencies: CONFIG_I2C_DRIVER && CONFIG_I2C_RESET
  242. */
  243. case I2CIOC_RESET:
  244. {
  245. ret = I2C_RESET(priv->i2c);
  246. }
  247. break;
  248. #endif
  249. default:
  250. ret = -ENOTTY;
  251. break;
  252. }
  253. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  254. sem_post(&priv->exclsem);
  255. #endif
  256. return ret;
  257. }
  258. /****************************************************************************
  259. * Name: i2cdrvr_unlink
  260. ****************************************************************************/
  261. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  262. static int i2cdrvr_unlink(FAR struct inode *inode)
  263. {
  264. FAR struct i2c_driver_s *priv;
  265. int ret;
  266. /* Get our private data structure */
  267. DEBUGASSERT(inode != NULL && inode->i_private != NULL);
  268. priv = (FAR struct i2c_driver_s *)inode->i_private;
  269. /* Get exclusive access to the I2C driver state structure */
  270. ret = sem_wait(&priv->exclsem);
  271. if (ret < 0)
  272. {
  273. int errcode = errno;
  274. DEBUGASSERT(errcode < 0);
  275. return -errcode;
  276. }
  277. /* Are there open references to the driver data structure? */
  278. if (priv->crefs <= 0)
  279. {
  280. sem_destroy(&priv->exclsem);
  281. kmm_free(priv);
  282. return OK;
  283. }
  284. /* No... just mark the driver as unlinked and free the resouces when the
  285. * last client closes their reference to the driver.
  286. */
  287. priv->unlinked = true;
  288. sem_post(&priv->exclsem);
  289. return ret;
  290. }
  291. #endif
  292. /****************************************************************************
  293. * Public Functions
  294. ****************************************************************************/
  295. /****************************************************************************
  296. * Name: i2c_register
  297. *
  298. * Description:
  299. * Create and register the I2C character driver.
  300. *
  301. * The I2C character driver is a simple character driver that supports I2C
  302. * transfers. The intent of this driver is to support I2C testing. It is
  303. * not suitable for use in any real driver application.
  304. *
  305. * Input Parameters:
  306. * i2c - An instance of the lower half I2C driver
  307. * bus - The I2C bus number. This will be used as the I2C device minor
  308. * number. The I2C character device will be registered as /dev/i2cN
  309. * where N is the minor number
  310. *
  311. * Returned Value:
  312. * OK if the driver was successfully register; A negated errno value is
  313. * returned on any failure.
  314. *
  315. ****************************************************************************/
  316. int i2c_register(FAR struct i2c_master_s *i2c, int bus)
  317. {
  318. FAR struct i2c_driver_s *priv;
  319. char devname[DEVNAME_FMTLEN];
  320. int ret;
  321. /* Sanity check */
  322. DEBUGASSERT(i2c != NULL && (unsigned)bus < 1000);
  323. /* Allocate a I2C character device structure */
  324. priv = (FAR struct i2c_driver_s *)kmm_zalloc(sizeof(struct i2c_driver_s));
  325. if (priv)
  326. {
  327. /* Initialize the I2C character device structure */
  328. priv->i2c = i2c;
  329. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  330. sem_init(&priv->exclsem, 0, 1);
  331. #endif
  332. /* Create the character device name */
  333. snprintf(devname, DEVNAME_FMTLEN, DEVNAME_FMT, bus);
  334. ret = register_driver(devname, &i2cdrvr_fops, 0666, priv);
  335. if (ret < 0)
  336. {
  337. /* Free the device structure if we failed to create the character
  338. * device.
  339. */
  340. kmm_free(priv);
  341. }
  342. /* Return the result of the registration */
  343. return OK;
  344. }
  345. return -ENOMEM;
  346. }
  347. #endif /* CONFIG_I2C_DRIVER */