ci-bridge-spi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /* This driver implements a simple SPI read/write interface to access
  13. * an external device over SPI.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/errno.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/device.h>
  21. #include <linux/cdev.h>
  22. #include <linux/fs.h>
  23. #include <linux/mutex.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/gpio.h>
  26. #include <linux/delay.h>
  27. #include <linux/ci-bridge-spi.h>
  28. #define CI_MAX_BUFFER_SIZE (64 * 1024)
  29. struct ci_bridge {
  30. dev_t ci_bridge_dev;
  31. struct cdev cdev;
  32. struct class *bridge_class;
  33. struct device *bridge_dev;
  34. char *write_buffer;
  35. char *read_buffer;
  36. struct mutex lock;
  37. struct spi_device *spi;
  38. unsigned int gpio_reset_pin;
  39. unsigned int gpio_interrupt_pin;
  40. int num_opened;
  41. };
  42. static struct ci_bridge ci;
  43. static int __devinit ci_bridge_spi_probe(struct spi_device *spi)
  44. {
  45. int ret;
  46. struct ci_bridge_platform_data *pdata;
  47. if (spi->dev.platform_data == NULL) {
  48. pr_err("%s: platform data is missing\n", __func__);
  49. return -EINVAL;
  50. }
  51. ci.spi = spi;
  52. ci.num_opened = 0;
  53. mutex_init(&ci.lock);
  54. spi_set_drvdata(spi, &ci);
  55. pdata = spi->dev.platform_data;
  56. ci.gpio_reset_pin = pdata->reset_pin;
  57. ci.gpio_interrupt_pin = pdata->interrupt_pin;
  58. ret = gpio_request(ci.gpio_reset_pin, "ci_bridge_spi");
  59. if (ret) {
  60. pr_err("%s: GPIO request for pin number %u failed\n",
  61. __func__, ci.gpio_reset_pin);
  62. return ret;
  63. }
  64. ret = gpio_direction_output(ci.gpio_reset_pin, 1);
  65. if (ret) {
  66. pr_err("%s: unable to set GPIO direction, err=%d\n",
  67. __func__, ret);
  68. goto err_free_reset_pin;
  69. }
  70. ret = gpio_request(ci.gpio_interrupt_pin, "ci_bridge_spi");
  71. if (ret) {
  72. pr_err("%s: GPIO request for pin number %u failed\n",
  73. __func__, ci.gpio_interrupt_pin);
  74. goto err_free_reset_pin;
  75. }
  76. ret = gpio_direction_input(ci.gpio_interrupt_pin);
  77. if (ret) {
  78. pr_err("%s: unable to set GPIO direction, err=%d\n",
  79. __func__, ret);
  80. goto err_free_int_pin;
  81. }
  82. return 0;
  83. err_free_int_pin:
  84. gpio_free(ci.gpio_interrupt_pin);
  85. err_free_reset_pin:
  86. gpio_free(ci.gpio_reset_pin);
  87. return ret;
  88. }
  89. static int __devexit ci_bridge_spi_remove(struct spi_device *spi)
  90. {
  91. struct ci_bridge *bridge = spi_get_drvdata(spi);
  92. spi_set_drvdata(bridge->spi, NULL);
  93. bridge->spi = NULL;
  94. mutex_destroy(&ci.lock);
  95. gpio_free(ci.gpio_reset_pin);
  96. gpio_free(ci.gpio_interrupt_pin);
  97. return 0;
  98. }
  99. static struct spi_driver ci_bridge_driver = {
  100. .driver = {
  101. .name = "ci_bridge_spi",
  102. .owner = THIS_MODULE,
  103. },
  104. .probe = ci_bridge_spi_probe,
  105. .remove = __devexit_p(ci_bridge_spi_remove),
  106. };
  107. static void ci_bridge_spi_completion_cb(void *arg)
  108. {
  109. complete(arg);
  110. }
  111. static ssize_t ci_bridge_spi_read(struct file *filp,
  112. char __user *buf,
  113. size_t count,
  114. loff_t *f_pos)
  115. {
  116. int ret = 0;
  117. unsigned long not_copied = 0;
  118. struct spi_transfer spi_transfer;
  119. struct spi_message spi_message;
  120. DECLARE_COMPLETION_ONSTACK(context);
  121. struct ci_bridge *bridge = filp->private_data;
  122. if ((bridge == NULL) || (bridge->spi == NULL))
  123. return -ENODEV;
  124. if (count > CI_MAX_BUFFER_SIZE)
  125. return -EMSGSIZE;
  126. memset(&spi_transfer, 0, sizeof(struct spi_transfer));
  127. memset(&spi_message, 0, sizeof(struct spi_message));
  128. mutex_lock(&bridge->lock);
  129. spi_transfer.rx_buf = bridge->read_buffer;
  130. spi_transfer.len = count;
  131. spi_message_init(&spi_message);
  132. spi_message_add_tail(&spi_transfer, &spi_message);
  133. spi_message.complete = ci_bridge_spi_completion_cb;
  134. spi_message.context = &context;
  135. /* must use spi_async in a context that may sleep */
  136. ret = spi_async(bridge->spi, &spi_message);
  137. if (ret == 0) {
  138. wait_for_completion(&context);
  139. if (spi_message.status == 0) {
  140. /* spi_message.actual_length should contain the number
  141. * of bytes actually read and should update ret to be
  142. * the actual length, but since our driver doesn't
  143. * support this, assume all count bytes were read.
  144. */
  145. ret = count;
  146. }
  147. if (ret > 0) {
  148. not_copied =
  149. copy_to_user(buf, bridge->read_buffer, ret);
  150. if (not_copied == ret)
  151. ret = -EFAULT;
  152. else
  153. ret -= not_copied;
  154. }
  155. } else {
  156. pr_err("%s: Error calling spi_async, ret = %d\n",
  157. __func__, ret);
  158. }
  159. mutex_unlock(&bridge->lock);
  160. return ret;
  161. }
  162. static ssize_t ci_bridge_spi_write(struct file *filp,
  163. const char __user *buf,
  164. size_t count,
  165. loff_t *f_pos)
  166. {
  167. int ret = 0;
  168. unsigned long not_copied = 0;
  169. struct spi_transfer spi_transfer;
  170. struct spi_message spi_message;
  171. DECLARE_COMPLETION_ONSTACK(context);
  172. struct ci_bridge *bridge = filp->private_data;
  173. if ((bridge == NULL) || (bridge->spi == NULL))
  174. return -ENODEV;
  175. if (count > CI_MAX_BUFFER_SIZE)
  176. return -EMSGSIZE;
  177. memset(&spi_transfer, 0, sizeof(struct spi_transfer));
  178. memset(&spi_message, 0, sizeof(struct spi_message));
  179. mutex_lock(&bridge->lock);
  180. /* copy user data to our SPI Tx buffer */
  181. not_copied = copy_from_user(bridge->write_buffer, buf, count);
  182. if (not_copied != 0) {
  183. ret = -EFAULT;
  184. } else {
  185. spi_transfer.tx_buf = bridge->write_buffer;
  186. spi_transfer.len = count;
  187. spi_message_init(&spi_message);
  188. spi_message_add_tail(&spi_transfer, &spi_message);
  189. spi_message.complete = ci_bridge_spi_completion_cb;
  190. spi_message.context = &context;
  191. /* must use spi_async in a context that may sleep */
  192. ret = spi_async(bridge->spi, &spi_message);
  193. if (ret == 0) {
  194. wait_for_completion(&context);
  195. /* update ret to contain
  196. * the number of bytes actually written
  197. */
  198. if (spi_message.status == 0)
  199. ret = spi_transfer.len;
  200. else
  201. pr_err("%s: SPI transfer error, spi_message.status = %d\n",
  202. __func__, spi_message.status);
  203. } else {
  204. pr_err("%s: Error calling spi_async, ret = %d\n",
  205. __func__, ret);
  206. }
  207. }
  208. mutex_unlock(&bridge->lock);
  209. return ret;
  210. }
  211. static int ci_bridge_spi_open(struct inode *inode, struct file *filp)
  212. {
  213. /* forbid opening more then one instance at a time,
  214. parallel execution can still be problematic */
  215. if (ci.num_opened != 0)
  216. return -EBUSY;
  217. /* allocate write buffer */
  218. ci.write_buffer =
  219. kzalloc((CI_MAX_BUFFER_SIZE * sizeof(char)), GFP_KERNEL);
  220. if (ci.write_buffer == NULL) {
  221. pr_err("%s: Error allocating memory for write buffer\n",
  222. __func__);
  223. return -ENOMEM;
  224. }
  225. /* allocate read buffer */
  226. ci.read_buffer =
  227. kzalloc((CI_MAX_BUFFER_SIZE * sizeof(char)), GFP_KERNEL);
  228. if (ci.read_buffer == NULL) {
  229. pr_err("%s: Error allocating memory for read buffer\n",
  230. __func__);
  231. kfree(ci.write_buffer);
  232. return -ENOMEM;
  233. }
  234. /* device is non-seekable */
  235. nonseekable_open(inode, filp);
  236. filp->private_data = &ci;
  237. ci.num_opened = 1;
  238. return 0;
  239. }
  240. static int ci_bridge_ioctl_get_int(void *arg)
  241. {
  242. int state;
  243. if (arg == NULL)
  244. return -EINVAL;
  245. state = gpio_get_value_cansleep(ci.gpio_interrupt_pin);
  246. if (copy_to_user(arg, &state, sizeof(state)))
  247. return -EFAULT;
  248. return 0;
  249. }
  250. static int ci_bridge_ioctl_reset(unsigned long arg)
  251. {
  252. if ((arg != 0) && (arg != 1))
  253. return -EINVAL;
  254. gpio_set_value_cansleep(ci.gpio_reset_pin, arg);
  255. return 0;
  256. }
  257. static long ci_bridge_spi_ioctl(struct file *file, unsigned int cmd,
  258. unsigned long arg)
  259. {
  260. int ret;
  261. switch (cmd) {
  262. case CI_BRIDGE_IOCTL_RESET:
  263. ret = ci_bridge_ioctl_reset(arg);
  264. break;
  265. case CI_BRIDGE_IOCTL_GET_INT_STATE:
  266. ret = ci_bridge_ioctl_get_int((void *) arg);
  267. break;
  268. default:
  269. ret = -EINVAL;
  270. break;
  271. }
  272. return ret;
  273. }
  274. static int ci_bridge_spi_release(struct inode *inode, struct file *filp)
  275. {
  276. struct ci_bridge *bridge = filp->private_data;
  277. if ((bridge == NULL) || (bridge->spi == NULL))
  278. return -ENODEV;
  279. kfree(bridge->write_buffer);
  280. kfree(bridge->read_buffer);
  281. filp->private_data = NULL;
  282. ci.num_opened = 0;
  283. return 0;
  284. }
  285. static const struct file_operations ci_bridge_spi_fops = {
  286. .owner = THIS_MODULE,
  287. .read = ci_bridge_spi_read,
  288. .write = ci_bridge_spi_write,
  289. .open = ci_bridge_spi_open,
  290. .unlocked_ioctl = ci_bridge_spi_ioctl,
  291. .release = ci_bridge_spi_release,
  292. .llseek = no_llseek,
  293. };
  294. static int __init ci_bridge_init(void)
  295. {
  296. int ret = 0;
  297. ret = alloc_chrdev_region(&ci.ci_bridge_dev, 0, 1, "ci_bridge_spi");
  298. if (ret != 0)
  299. return ret;
  300. ci.bridge_class = class_create(THIS_MODULE, "ci_bridge_spi");
  301. if (IS_ERR(ci.bridge_class)) {
  302. ret = PTR_ERR(ci.bridge_class);
  303. pr_err("Error creating ci.bridge_class: %d\n", ret);
  304. goto free_region;
  305. }
  306. cdev_init(&ci.cdev, &ci_bridge_spi_fops);
  307. ci.cdev.owner = THIS_MODULE;
  308. ret = cdev_add(&ci.cdev, ci.ci_bridge_dev, 1);
  309. if (ret != 0) {
  310. pr_err("Error calling cdev_add: %d\n", ret);
  311. goto class_destroy;
  312. }
  313. ci.bridge_dev = device_create(ci.bridge_class, NULL, ci.cdev.dev,
  314. &ci, "ci_bridge_spi0");
  315. if (IS_ERR(ci.bridge_dev)) {
  316. ret = PTR_ERR(ci.bridge_dev);
  317. pr_err("device_create failed: %d\n", ret);
  318. goto del_cdev;
  319. }
  320. ret = spi_register_driver(&ci_bridge_driver);
  321. if (ret != 0) {
  322. pr_err("Error registering spi driver: %d\n", ret);
  323. goto device_destroy;
  324. }
  325. /* successful return */
  326. return 0;
  327. device_destroy:
  328. device_destroy(ci.bridge_class, ci.ci_bridge_dev);
  329. del_cdev:
  330. cdev_del(&ci.cdev);
  331. class_destroy:
  332. class_destroy(ci.bridge_class);
  333. free_region:
  334. unregister_chrdev_region(ci.ci_bridge_dev, 1);
  335. return ret;
  336. }
  337. static void __exit ci_bridge_exit(void)
  338. {
  339. spi_unregister_driver(&ci_bridge_driver);
  340. device_destroy(ci.bridge_class, ci.ci_bridge_dev);
  341. cdev_del(&ci.cdev);
  342. class_destroy(ci.bridge_class);
  343. unregister_chrdev_region(ci.ci_bridge_dev, 1);
  344. }
  345. module_init(ci_bridge_init);
  346. module_exit(ci_bridge_exit);
  347. MODULE_DESCRIPTION("CI Bridge SPI Driver");
  348. MODULE_LICENSE("GPL v2");