gpio-viperboard.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Nano River Technologies viperboard GPIO lib driver
  3. *
  4. * (C) 2012 by Lemonage GmbH
  5. * Author: Lars Poeschel <poeschel@lemonage.de>
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #include <linux/mutex.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/usb.h>
  22. #include <linux/gpio.h>
  23. #include <linux/mfd/viperboard.h>
  24. #define VPRBRD_GPIOA_CLK_1MHZ 0
  25. #define VPRBRD_GPIOA_CLK_100KHZ 1
  26. #define VPRBRD_GPIOA_CLK_10KHZ 2
  27. #define VPRBRD_GPIOA_CLK_1KHZ 3
  28. #define VPRBRD_GPIOA_CLK_100HZ 4
  29. #define VPRBRD_GPIOA_CLK_10HZ 5
  30. #define VPRBRD_GPIOA_FREQ_DEFAULT 1000
  31. #define VPRBRD_GPIOA_CMD_CONT 0x00
  32. #define VPRBRD_GPIOA_CMD_PULSE 0x01
  33. #define VPRBRD_GPIOA_CMD_PWM 0x02
  34. #define VPRBRD_GPIOA_CMD_SETOUT 0x03
  35. #define VPRBRD_GPIOA_CMD_SETIN 0x04
  36. #define VPRBRD_GPIOA_CMD_SETINT 0x05
  37. #define VPRBRD_GPIOA_CMD_GETIN 0x06
  38. #define VPRBRD_GPIOB_CMD_SETDIR 0x00
  39. #define VPRBRD_GPIOB_CMD_SETVAL 0x01
  40. struct vprbrd_gpioa_msg {
  41. u8 cmd;
  42. u8 clk;
  43. u8 offset;
  44. u8 t1;
  45. u8 t2;
  46. u8 invert;
  47. u8 pwmlevel;
  48. u8 outval;
  49. u8 risefall;
  50. u8 answer;
  51. u8 __fill;
  52. } __packed;
  53. struct vprbrd_gpiob_msg {
  54. u8 cmd;
  55. u16 val;
  56. u16 mask;
  57. } __packed;
  58. struct vprbrd_gpio {
  59. struct gpio_chip gpioa; /* gpio a related things */
  60. u32 gpioa_out;
  61. u32 gpioa_val;
  62. struct gpio_chip gpiob; /* gpio b related things */
  63. u32 gpiob_out;
  64. u32 gpiob_val;
  65. struct vprbrd *vb;
  66. };
  67. /* gpioa sampling clock module parameter */
  68. static unsigned char gpioa_clk;
  69. static unsigned int gpioa_freq = VPRBRD_GPIOA_FREQ_DEFAULT;
  70. module_param(gpioa_freq, uint, 0);
  71. MODULE_PARM_DESC(gpioa_freq,
  72. "gpio-a sampling freq in Hz (default is 1000Hz) valid values: 10, 100, 1000, 10000, 100000, 1000000");
  73. /* ----- begin of gipo a chip -------------------------------------------- */
  74. static int vprbrd_gpioa_get(struct gpio_chip *chip,
  75. unsigned offset)
  76. {
  77. int ret, answer, error = 0;
  78. struct vprbrd_gpio *gpio =
  79. container_of(chip, struct vprbrd_gpio, gpioa);
  80. struct vprbrd *vb = gpio->vb;
  81. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  82. /* if io is set to output, just return the saved value */
  83. if (gpio->gpioa_out & (1 << offset))
  84. return gpio->gpioa_val & (1 << offset);
  85. mutex_lock(&vb->lock);
  86. gamsg->cmd = VPRBRD_GPIOA_CMD_GETIN;
  87. gamsg->clk = 0x00;
  88. gamsg->offset = offset;
  89. gamsg->t1 = 0x00;
  90. gamsg->t2 = 0x00;
  91. gamsg->invert = 0x00;
  92. gamsg->pwmlevel = 0x00;
  93. gamsg->outval = 0x00;
  94. gamsg->risefall = 0x00;
  95. gamsg->answer = 0x00;
  96. gamsg->__fill = 0x00;
  97. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  98. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
  99. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  100. VPRBRD_USB_TIMEOUT_MS);
  101. if (ret != sizeof(struct vprbrd_gpioa_msg))
  102. error = -EREMOTEIO;
  103. ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
  104. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_IN, 0x0000,
  105. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  106. VPRBRD_USB_TIMEOUT_MS);
  107. answer = gamsg->answer & 0x01;
  108. mutex_unlock(&vb->lock);
  109. if (ret != sizeof(struct vprbrd_gpioa_msg))
  110. error = -EREMOTEIO;
  111. if (error)
  112. return error;
  113. return answer;
  114. }
  115. static void vprbrd_gpioa_set(struct gpio_chip *chip,
  116. unsigned offset, int value)
  117. {
  118. int ret;
  119. struct vprbrd_gpio *gpio =
  120. container_of(chip, struct vprbrd_gpio, gpioa);
  121. struct vprbrd *vb = gpio->vb;
  122. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  123. if (gpio->gpioa_out & (1 << offset)) {
  124. if (value)
  125. gpio->gpioa_val |= (1 << offset);
  126. else
  127. gpio->gpioa_val &= ~(1 << offset);
  128. mutex_lock(&vb->lock);
  129. gamsg->cmd = VPRBRD_GPIOA_CMD_SETOUT;
  130. gamsg->clk = 0x00;
  131. gamsg->offset = offset;
  132. gamsg->t1 = 0x00;
  133. gamsg->t2 = 0x00;
  134. gamsg->invert = 0x00;
  135. gamsg->pwmlevel = 0x00;
  136. gamsg->outval = value;
  137. gamsg->risefall = 0x00;
  138. gamsg->answer = 0x00;
  139. gamsg->__fill = 0x00;
  140. ret = usb_control_msg(vb->usb_dev,
  141. usb_sndctrlpipe(vb->usb_dev, 0),
  142. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT,
  143. 0x0000, 0x0000, gamsg,
  144. sizeof(struct vprbrd_gpioa_msg), VPRBRD_USB_TIMEOUT_MS);
  145. mutex_unlock(&vb->lock);
  146. if (ret != sizeof(struct vprbrd_gpioa_msg))
  147. dev_err(chip->dev, "usb error setting pin value\n");
  148. }
  149. }
  150. static int vprbrd_gpioa_direction_input(struct gpio_chip *chip,
  151. unsigned offset)
  152. {
  153. int ret;
  154. struct vprbrd_gpio *gpio =
  155. container_of(chip, struct vprbrd_gpio, gpioa);
  156. struct vprbrd *vb = gpio->vb;
  157. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  158. gpio->gpioa_out &= ~(1 << offset);
  159. mutex_lock(&vb->lock);
  160. gamsg->cmd = VPRBRD_GPIOA_CMD_SETIN;
  161. gamsg->clk = gpioa_clk;
  162. gamsg->offset = offset;
  163. gamsg->t1 = 0x00;
  164. gamsg->t2 = 0x00;
  165. gamsg->invert = 0x00;
  166. gamsg->pwmlevel = 0x00;
  167. gamsg->outval = 0x00;
  168. gamsg->risefall = 0x00;
  169. gamsg->answer = 0x00;
  170. gamsg->__fill = 0x00;
  171. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  172. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
  173. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  174. VPRBRD_USB_TIMEOUT_MS);
  175. mutex_unlock(&vb->lock);
  176. if (ret != sizeof(struct vprbrd_gpioa_msg))
  177. return -EREMOTEIO;
  178. return 0;
  179. }
  180. static int vprbrd_gpioa_direction_output(struct gpio_chip *chip,
  181. unsigned offset, int value)
  182. {
  183. int ret;
  184. struct vprbrd_gpio *gpio =
  185. container_of(chip, struct vprbrd_gpio, gpioa);
  186. struct vprbrd *vb = gpio->vb;
  187. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  188. gpio->gpioa_out |= (1 << offset);
  189. if (value)
  190. gpio->gpioa_val |= (1 << offset);
  191. else
  192. gpio->gpioa_val &= ~(1 << offset);
  193. mutex_lock(&vb->lock);
  194. gamsg->cmd = VPRBRD_GPIOA_CMD_SETOUT;
  195. gamsg->clk = 0x00;
  196. gamsg->offset = offset;
  197. gamsg->t1 = 0x00;
  198. gamsg->t2 = 0x00;
  199. gamsg->invert = 0x00;
  200. gamsg->pwmlevel = 0x00;
  201. gamsg->outval = value;
  202. gamsg->risefall = 0x00;
  203. gamsg->answer = 0x00;
  204. gamsg->__fill = 0x00;
  205. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  206. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
  207. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  208. VPRBRD_USB_TIMEOUT_MS);
  209. mutex_unlock(&vb->lock);
  210. if (ret != sizeof(struct vprbrd_gpioa_msg))
  211. return -EREMOTEIO;
  212. return 0;
  213. }
  214. /* ----- end of gpio a chip ---------------------------------------------- */
  215. /* ----- begin of gipo b chip -------------------------------------------- */
  216. static int vprbrd_gpiob_setdir(struct vprbrd *vb, unsigned offset,
  217. unsigned dir)
  218. {
  219. struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
  220. int ret;
  221. gbmsg->cmd = VPRBRD_GPIOB_CMD_SETDIR;
  222. gbmsg->val = cpu_to_be16(dir << offset);
  223. gbmsg->mask = cpu_to_be16(0x0001 << offset);
  224. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  225. VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_OUT, 0x0000,
  226. 0x0000, gbmsg, sizeof(struct vprbrd_gpiob_msg),
  227. VPRBRD_USB_TIMEOUT_MS);
  228. if (ret != sizeof(struct vprbrd_gpiob_msg))
  229. return -EREMOTEIO;
  230. return 0;
  231. }
  232. static int vprbrd_gpiob_get(struct gpio_chip *chip,
  233. unsigned offset)
  234. {
  235. int ret;
  236. u16 val;
  237. struct vprbrd_gpio *gpio =
  238. container_of(chip, struct vprbrd_gpio, gpiob);
  239. struct vprbrd *vb = gpio->vb;
  240. struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
  241. /* if io is set to output, just return the saved value */
  242. if (gpio->gpiob_out & (1 << offset))
  243. return gpio->gpiob_val & (1 << offset);
  244. mutex_lock(&vb->lock);
  245. ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
  246. VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_IN, 0x0000,
  247. 0x0000, gbmsg, sizeof(struct vprbrd_gpiob_msg),
  248. VPRBRD_USB_TIMEOUT_MS);
  249. val = gbmsg->val;
  250. mutex_unlock(&vb->lock);
  251. if (ret != sizeof(struct vprbrd_gpiob_msg))
  252. return ret;
  253. /* cache the read values */
  254. gpio->gpiob_val = be16_to_cpu(val);
  255. return (gpio->gpiob_val >> offset) & 0x1;
  256. }
  257. static void vprbrd_gpiob_set(struct gpio_chip *chip,
  258. unsigned offset, int value)
  259. {
  260. int ret;
  261. struct vprbrd_gpio *gpio =
  262. container_of(chip, struct vprbrd_gpio, gpiob);
  263. struct vprbrd *vb = gpio->vb;
  264. struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
  265. if (gpio->gpiob_out & (1 << offset)) {
  266. if (value)
  267. gpio->gpiob_val |= (1 << offset);
  268. else
  269. gpio->gpiob_val &= ~(1 << offset);
  270. mutex_lock(&vb->lock);
  271. gbmsg->cmd = VPRBRD_GPIOB_CMD_SETVAL;
  272. gbmsg->val = cpu_to_be16(value << offset);
  273. gbmsg->mask = cpu_to_be16(0x0001 << offset);
  274. ret = usb_control_msg(vb->usb_dev,
  275. usb_sndctrlpipe(vb->usb_dev, 0),
  276. VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_OUT,
  277. 0x0000, 0x0000, gbmsg,
  278. sizeof(struct vprbrd_gpiob_msg), VPRBRD_USB_TIMEOUT_MS);
  279. mutex_unlock(&vb->lock);
  280. if (ret != sizeof(struct vprbrd_gpiob_msg))
  281. dev_err(chip->dev, "usb error setting pin value\n");
  282. }
  283. }
  284. static int vprbrd_gpiob_direction_input(struct gpio_chip *chip,
  285. unsigned offset)
  286. {
  287. int ret;
  288. struct vprbrd_gpio *gpio =
  289. container_of(chip, struct vprbrd_gpio, gpiob);
  290. struct vprbrd *vb = gpio->vb;
  291. gpio->gpiob_out &= ~(1 << offset);
  292. mutex_lock(&vb->lock);
  293. ret = vprbrd_gpiob_setdir(vb, offset, 0);
  294. mutex_unlock(&vb->lock);
  295. if (ret)
  296. dev_err(chip->dev, "usb error setting pin to input\n");
  297. return ret;
  298. }
  299. static int vprbrd_gpiob_direction_output(struct gpio_chip *chip,
  300. unsigned offset, int value)
  301. {
  302. int ret;
  303. struct vprbrd_gpio *gpio =
  304. container_of(chip, struct vprbrd_gpio, gpiob);
  305. struct vprbrd *vb = gpio->vb;
  306. gpio->gpiob_out |= (1 << offset);
  307. mutex_lock(&vb->lock);
  308. ret = vprbrd_gpiob_setdir(vb, offset, 1);
  309. if (ret)
  310. dev_err(chip->dev, "usb error setting pin to output\n");
  311. mutex_unlock(&vb->lock);
  312. vprbrd_gpiob_set(chip, offset, value);
  313. return ret;
  314. }
  315. /* ----- end of gpio b chip ---------------------------------------------- */
  316. static int vprbrd_gpio_probe(struct platform_device *pdev)
  317. {
  318. struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
  319. struct vprbrd_gpio *vb_gpio;
  320. int ret;
  321. vb_gpio = devm_kzalloc(&pdev->dev, sizeof(*vb_gpio), GFP_KERNEL);
  322. if (vb_gpio == NULL)
  323. return -ENOMEM;
  324. vb_gpio->vb = vb;
  325. /* registering gpio a */
  326. vb_gpio->gpioa.label = "viperboard gpio a";
  327. vb_gpio->gpioa.dev = &pdev->dev;
  328. vb_gpio->gpioa.owner = THIS_MODULE;
  329. vb_gpio->gpioa.base = -1;
  330. vb_gpio->gpioa.ngpio = 16;
  331. vb_gpio->gpioa.can_sleep = true;
  332. vb_gpio->gpioa.set = vprbrd_gpioa_set;
  333. vb_gpio->gpioa.get = vprbrd_gpioa_get;
  334. vb_gpio->gpioa.direction_input = vprbrd_gpioa_direction_input;
  335. vb_gpio->gpioa.direction_output = vprbrd_gpioa_direction_output;
  336. ret = gpiochip_add(&vb_gpio->gpioa);
  337. if (ret < 0) {
  338. dev_err(vb_gpio->gpioa.dev, "could not add gpio a");
  339. goto err_gpioa;
  340. }
  341. /* registering gpio b */
  342. vb_gpio->gpiob.label = "viperboard gpio b";
  343. vb_gpio->gpiob.dev = &pdev->dev;
  344. vb_gpio->gpiob.owner = THIS_MODULE;
  345. vb_gpio->gpiob.base = -1;
  346. vb_gpio->gpiob.ngpio = 16;
  347. vb_gpio->gpiob.can_sleep = true;
  348. vb_gpio->gpiob.set = vprbrd_gpiob_set;
  349. vb_gpio->gpiob.get = vprbrd_gpiob_get;
  350. vb_gpio->gpiob.direction_input = vprbrd_gpiob_direction_input;
  351. vb_gpio->gpiob.direction_output = vprbrd_gpiob_direction_output;
  352. ret = gpiochip_add(&vb_gpio->gpiob);
  353. if (ret < 0) {
  354. dev_err(vb_gpio->gpiob.dev, "could not add gpio b");
  355. goto err_gpiob;
  356. }
  357. platform_set_drvdata(pdev, vb_gpio);
  358. return ret;
  359. err_gpiob:
  360. gpiochip_remove(&vb_gpio->gpioa);
  361. err_gpioa:
  362. return ret;
  363. }
  364. static int vprbrd_gpio_remove(struct platform_device *pdev)
  365. {
  366. struct vprbrd_gpio *vb_gpio = platform_get_drvdata(pdev);
  367. gpiochip_remove(&vb_gpio->gpiob);
  368. return 0;
  369. }
  370. static struct platform_driver vprbrd_gpio_driver = {
  371. .driver.name = "viperboard-gpio",
  372. .driver.owner = THIS_MODULE,
  373. .probe = vprbrd_gpio_probe,
  374. .remove = vprbrd_gpio_remove,
  375. };
  376. static int __init vprbrd_gpio_init(void)
  377. {
  378. switch (gpioa_freq) {
  379. case 1000000:
  380. gpioa_clk = VPRBRD_GPIOA_CLK_1MHZ;
  381. break;
  382. case 100000:
  383. gpioa_clk = VPRBRD_GPIOA_CLK_100KHZ;
  384. break;
  385. case 10000:
  386. gpioa_clk = VPRBRD_GPIOA_CLK_10KHZ;
  387. break;
  388. case 1000:
  389. gpioa_clk = VPRBRD_GPIOA_CLK_1KHZ;
  390. break;
  391. case 100:
  392. gpioa_clk = VPRBRD_GPIOA_CLK_100HZ;
  393. break;
  394. case 10:
  395. gpioa_clk = VPRBRD_GPIOA_CLK_10HZ;
  396. break;
  397. default:
  398. pr_warn("invalid gpioa_freq (%d)\n", gpioa_freq);
  399. gpioa_clk = VPRBRD_GPIOA_CLK_1KHZ;
  400. }
  401. return platform_driver_register(&vprbrd_gpio_driver);
  402. }
  403. subsys_initcall(vprbrd_gpio_init);
  404. static void __exit vprbrd_gpio_exit(void)
  405. {
  406. platform_driver_unregister(&vprbrd_gpio_driver);
  407. }
  408. module_exit(vprbrd_gpio_exit);
  409. MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
  410. MODULE_DESCRIPTION("GPIO driver for Nano River Techs Viperboard");
  411. MODULE_LICENSE("GPL");
  412. MODULE_ALIAS("platform:viperboard-gpio");