gpio-viperboard.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 = gpiochip_get_data(chip);
  79. struct vprbrd *vb = gpio->vb;
  80. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  81. /* if io is set to output, just return the saved value */
  82. if (gpio->gpioa_out & (1 << offset))
  83. return !!(gpio->gpioa_val & (1 << offset));
  84. mutex_lock(&vb->lock);
  85. gamsg->cmd = VPRBRD_GPIOA_CMD_GETIN;
  86. gamsg->clk = 0x00;
  87. gamsg->offset = offset;
  88. gamsg->t1 = 0x00;
  89. gamsg->t2 = 0x00;
  90. gamsg->invert = 0x00;
  91. gamsg->pwmlevel = 0x00;
  92. gamsg->outval = 0x00;
  93. gamsg->risefall = 0x00;
  94. gamsg->answer = 0x00;
  95. gamsg->__fill = 0x00;
  96. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  97. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
  98. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  99. VPRBRD_USB_TIMEOUT_MS);
  100. if (ret != sizeof(struct vprbrd_gpioa_msg))
  101. error = -EREMOTEIO;
  102. ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
  103. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_IN, 0x0000,
  104. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  105. VPRBRD_USB_TIMEOUT_MS);
  106. answer = gamsg->answer & 0x01;
  107. mutex_unlock(&vb->lock);
  108. if (ret != sizeof(struct vprbrd_gpioa_msg))
  109. error = -EREMOTEIO;
  110. if (error)
  111. return error;
  112. return answer;
  113. }
  114. static void vprbrd_gpioa_set(struct gpio_chip *chip,
  115. unsigned offset, int value)
  116. {
  117. int ret;
  118. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  119. struct vprbrd *vb = gpio->vb;
  120. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  121. if (gpio->gpioa_out & (1 << offset)) {
  122. if (value)
  123. gpio->gpioa_val |= (1 << offset);
  124. else
  125. gpio->gpioa_val &= ~(1 << offset);
  126. mutex_lock(&vb->lock);
  127. gamsg->cmd = VPRBRD_GPIOA_CMD_SETOUT;
  128. gamsg->clk = 0x00;
  129. gamsg->offset = offset;
  130. gamsg->t1 = 0x00;
  131. gamsg->t2 = 0x00;
  132. gamsg->invert = 0x00;
  133. gamsg->pwmlevel = 0x00;
  134. gamsg->outval = value;
  135. gamsg->risefall = 0x00;
  136. gamsg->answer = 0x00;
  137. gamsg->__fill = 0x00;
  138. ret = usb_control_msg(vb->usb_dev,
  139. usb_sndctrlpipe(vb->usb_dev, 0),
  140. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT,
  141. 0x0000, 0x0000, gamsg,
  142. sizeof(struct vprbrd_gpioa_msg), VPRBRD_USB_TIMEOUT_MS);
  143. mutex_unlock(&vb->lock);
  144. if (ret != sizeof(struct vprbrd_gpioa_msg))
  145. dev_err(chip->parent, "usb error setting pin value\n");
  146. }
  147. }
  148. static int vprbrd_gpioa_direction_input(struct gpio_chip *chip,
  149. unsigned offset)
  150. {
  151. int ret;
  152. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  153. struct vprbrd *vb = gpio->vb;
  154. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  155. gpio->gpioa_out &= ~(1 << offset);
  156. mutex_lock(&vb->lock);
  157. gamsg->cmd = VPRBRD_GPIOA_CMD_SETIN;
  158. gamsg->clk = gpioa_clk;
  159. gamsg->offset = offset;
  160. gamsg->t1 = 0x00;
  161. gamsg->t2 = 0x00;
  162. gamsg->invert = 0x00;
  163. gamsg->pwmlevel = 0x00;
  164. gamsg->outval = 0x00;
  165. gamsg->risefall = 0x00;
  166. gamsg->answer = 0x00;
  167. gamsg->__fill = 0x00;
  168. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  169. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
  170. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  171. VPRBRD_USB_TIMEOUT_MS);
  172. mutex_unlock(&vb->lock);
  173. if (ret != sizeof(struct vprbrd_gpioa_msg))
  174. return -EREMOTEIO;
  175. return 0;
  176. }
  177. static int vprbrd_gpioa_direction_output(struct gpio_chip *chip,
  178. unsigned offset, int value)
  179. {
  180. int ret;
  181. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  182. struct vprbrd *vb = gpio->vb;
  183. struct vprbrd_gpioa_msg *gamsg = (struct vprbrd_gpioa_msg *)vb->buf;
  184. gpio->gpioa_out |= (1 << offset);
  185. if (value)
  186. gpio->gpioa_val |= (1 << offset);
  187. else
  188. gpio->gpioa_val &= ~(1 << offset);
  189. mutex_lock(&vb->lock);
  190. gamsg->cmd = VPRBRD_GPIOA_CMD_SETOUT;
  191. gamsg->clk = 0x00;
  192. gamsg->offset = offset;
  193. gamsg->t1 = 0x00;
  194. gamsg->t2 = 0x00;
  195. gamsg->invert = 0x00;
  196. gamsg->pwmlevel = 0x00;
  197. gamsg->outval = value;
  198. gamsg->risefall = 0x00;
  199. gamsg->answer = 0x00;
  200. gamsg->__fill = 0x00;
  201. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  202. VPRBRD_USB_REQUEST_GPIOA, VPRBRD_USB_TYPE_OUT, 0x0000,
  203. 0x0000, gamsg, sizeof(struct vprbrd_gpioa_msg),
  204. VPRBRD_USB_TIMEOUT_MS);
  205. mutex_unlock(&vb->lock);
  206. if (ret != sizeof(struct vprbrd_gpioa_msg))
  207. return -EREMOTEIO;
  208. return 0;
  209. }
  210. /* ----- end of gpio a chip ---------------------------------------------- */
  211. /* ----- begin of gipo b chip -------------------------------------------- */
  212. static int vprbrd_gpiob_setdir(struct vprbrd *vb, unsigned offset,
  213. unsigned dir)
  214. {
  215. struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
  216. int ret;
  217. gbmsg->cmd = VPRBRD_GPIOB_CMD_SETDIR;
  218. gbmsg->val = cpu_to_be16(dir << offset);
  219. gbmsg->mask = cpu_to_be16(0x0001 << offset);
  220. ret = usb_control_msg(vb->usb_dev, usb_sndctrlpipe(vb->usb_dev, 0),
  221. VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_OUT, 0x0000,
  222. 0x0000, gbmsg, sizeof(struct vprbrd_gpiob_msg),
  223. VPRBRD_USB_TIMEOUT_MS);
  224. if (ret != sizeof(struct vprbrd_gpiob_msg))
  225. return -EREMOTEIO;
  226. return 0;
  227. }
  228. static int vprbrd_gpiob_get(struct gpio_chip *chip,
  229. unsigned offset)
  230. {
  231. int ret;
  232. u16 val;
  233. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  234. struct vprbrd *vb = gpio->vb;
  235. struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
  236. /* if io is set to output, just return the saved value */
  237. if (gpio->gpiob_out & (1 << offset))
  238. return gpio->gpiob_val & (1 << offset);
  239. mutex_lock(&vb->lock);
  240. ret = usb_control_msg(vb->usb_dev, usb_rcvctrlpipe(vb->usb_dev, 0),
  241. VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_IN, 0x0000,
  242. 0x0000, gbmsg, sizeof(struct vprbrd_gpiob_msg),
  243. VPRBRD_USB_TIMEOUT_MS);
  244. val = gbmsg->val;
  245. mutex_unlock(&vb->lock);
  246. if (ret != sizeof(struct vprbrd_gpiob_msg))
  247. return ret;
  248. /* cache the read values */
  249. gpio->gpiob_val = be16_to_cpu(val);
  250. return (gpio->gpiob_val >> offset) & 0x1;
  251. }
  252. static void vprbrd_gpiob_set(struct gpio_chip *chip,
  253. unsigned offset, int value)
  254. {
  255. int ret;
  256. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  257. struct vprbrd *vb = gpio->vb;
  258. struct vprbrd_gpiob_msg *gbmsg = (struct vprbrd_gpiob_msg *)vb->buf;
  259. if (gpio->gpiob_out & (1 << offset)) {
  260. if (value)
  261. gpio->gpiob_val |= (1 << offset);
  262. else
  263. gpio->gpiob_val &= ~(1 << offset);
  264. mutex_lock(&vb->lock);
  265. gbmsg->cmd = VPRBRD_GPIOB_CMD_SETVAL;
  266. gbmsg->val = cpu_to_be16(value << offset);
  267. gbmsg->mask = cpu_to_be16(0x0001 << offset);
  268. ret = usb_control_msg(vb->usb_dev,
  269. usb_sndctrlpipe(vb->usb_dev, 0),
  270. VPRBRD_USB_REQUEST_GPIOB, VPRBRD_USB_TYPE_OUT,
  271. 0x0000, 0x0000, gbmsg,
  272. sizeof(struct vprbrd_gpiob_msg), VPRBRD_USB_TIMEOUT_MS);
  273. mutex_unlock(&vb->lock);
  274. if (ret != sizeof(struct vprbrd_gpiob_msg))
  275. dev_err(chip->parent, "usb error setting pin value\n");
  276. }
  277. }
  278. static int vprbrd_gpiob_direction_input(struct gpio_chip *chip,
  279. unsigned offset)
  280. {
  281. int ret;
  282. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  283. struct vprbrd *vb = gpio->vb;
  284. gpio->gpiob_out &= ~(1 << offset);
  285. mutex_lock(&vb->lock);
  286. ret = vprbrd_gpiob_setdir(vb, offset, 0);
  287. mutex_unlock(&vb->lock);
  288. if (ret)
  289. dev_err(chip->parent, "usb error setting pin to input\n");
  290. return ret;
  291. }
  292. static int vprbrd_gpiob_direction_output(struct gpio_chip *chip,
  293. unsigned offset, int value)
  294. {
  295. int ret;
  296. struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
  297. struct vprbrd *vb = gpio->vb;
  298. gpio->gpiob_out |= (1 << offset);
  299. mutex_lock(&vb->lock);
  300. ret = vprbrd_gpiob_setdir(vb, offset, 1);
  301. if (ret)
  302. dev_err(chip->parent, "usb error setting pin to output\n");
  303. mutex_unlock(&vb->lock);
  304. vprbrd_gpiob_set(chip, offset, value);
  305. return ret;
  306. }
  307. /* ----- end of gpio b chip ---------------------------------------------- */
  308. static int vprbrd_gpio_probe(struct platform_device *pdev)
  309. {
  310. struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
  311. struct vprbrd_gpio *vb_gpio;
  312. int ret;
  313. vb_gpio = devm_kzalloc(&pdev->dev, sizeof(*vb_gpio), GFP_KERNEL);
  314. if (vb_gpio == NULL)
  315. return -ENOMEM;
  316. vb_gpio->vb = vb;
  317. /* registering gpio a */
  318. vb_gpio->gpioa.label = "viperboard gpio a";
  319. vb_gpio->gpioa.parent = &pdev->dev;
  320. vb_gpio->gpioa.owner = THIS_MODULE;
  321. vb_gpio->gpioa.base = -1;
  322. vb_gpio->gpioa.ngpio = 16;
  323. vb_gpio->gpioa.can_sleep = true;
  324. vb_gpio->gpioa.set = vprbrd_gpioa_set;
  325. vb_gpio->gpioa.get = vprbrd_gpioa_get;
  326. vb_gpio->gpioa.direction_input = vprbrd_gpioa_direction_input;
  327. vb_gpio->gpioa.direction_output = vprbrd_gpioa_direction_output;
  328. ret = devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpioa, vb_gpio);
  329. if (ret < 0) {
  330. dev_err(vb_gpio->gpioa.parent, "could not add gpio a");
  331. return ret;
  332. }
  333. /* registering gpio b */
  334. vb_gpio->gpiob.label = "viperboard gpio b";
  335. vb_gpio->gpiob.parent = &pdev->dev;
  336. vb_gpio->gpiob.owner = THIS_MODULE;
  337. vb_gpio->gpiob.base = -1;
  338. vb_gpio->gpiob.ngpio = 16;
  339. vb_gpio->gpiob.can_sleep = true;
  340. vb_gpio->gpiob.set = vprbrd_gpiob_set;
  341. vb_gpio->gpiob.get = vprbrd_gpiob_get;
  342. vb_gpio->gpiob.direction_input = vprbrd_gpiob_direction_input;
  343. vb_gpio->gpiob.direction_output = vprbrd_gpiob_direction_output;
  344. ret = devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpiob, vb_gpio);
  345. if (ret < 0) {
  346. dev_err(vb_gpio->gpiob.parent, "could not add gpio b");
  347. return ret;
  348. }
  349. platform_set_drvdata(pdev, vb_gpio);
  350. return ret;
  351. }
  352. static struct platform_driver vprbrd_gpio_driver = {
  353. .driver.name = "viperboard-gpio",
  354. .probe = vprbrd_gpio_probe,
  355. };
  356. static int __init vprbrd_gpio_init(void)
  357. {
  358. switch (gpioa_freq) {
  359. case 1000000:
  360. gpioa_clk = VPRBRD_GPIOA_CLK_1MHZ;
  361. break;
  362. case 100000:
  363. gpioa_clk = VPRBRD_GPIOA_CLK_100KHZ;
  364. break;
  365. case 10000:
  366. gpioa_clk = VPRBRD_GPIOA_CLK_10KHZ;
  367. break;
  368. case 1000:
  369. gpioa_clk = VPRBRD_GPIOA_CLK_1KHZ;
  370. break;
  371. case 100:
  372. gpioa_clk = VPRBRD_GPIOA_CLK_100HZ;
  373. break;
  374. case 10:
  375. gpioa_clk = VPRBRD_GPIOA_CLK_10HZ;
  376. break;
  377. default:
  378. pr_warn("invalid gpioa_freq (%d)\n", gpioa_freq);
  379. gpioa_clk = VPRBRD_GPIOA_CLK_1KHZ;
  380. }
  381. return platform_driver_register(&vprbrd_gpio_driver);
  382. }
  383. subsys_initcall(vprbrd_gpio_init);
  384. static void __exit vprbrd_gpio_exit(void)
  385. {
  386. platform_driver_unregister(&vprbrd_gpio_driver);
  387. }
  388. module_exit(vprbrd_gpio_exit);
  389. MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
  390. MODULE_DESCRIPTION("GPIO driver for Nano River Techs Viperboard");
  391. MODULE_LICENSE("GPL");
  392. MODULE_ALIAS("platform:viperboard-gpio");