gpio-ts5500.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Digital I/O driver for Technologic Systems TS-5500
  3. *
  4. * Copyright (c) 2012 Savoir-faire Linux Inc.
  5. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  6. *
  7. * Technologic Systems platforms have pin blocks, exposing several Digital
  8. * Input/Output lines (DIO). This driver aims to support single pin blocks.
  9. * In that sense, the support is not limited to the TS-5500 blocks.
  10. * Actually, the following platforms have DIO support:
  11. *
  12. * TS-5500:
  13. * Documentation: http://wiki.embeddedarm.com/wiki/TS-5500
  14. * Blocks: DIO1, DIO2 and LCD port.
  15. *
  16. * TS-5600:
  17. * Documentation: http://wiki.embeddedarm.com/wiki/TS-5600
  18. * Blocks: LCD port (identical to TS-5500 LCD).
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 2 as
  22. * published by the Free Software Foundation.
  23. */
  24. #include <linux/bitops.h>
  25. #include <linux/gpio.h>
  26. #include <linux/io.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_data/gpio-ts5500.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/slab.h>
  31. /* List of supported Technologic Systems platforms DIO blocks */
  32. enum ts5500_blocks { TS5500_DIO1, TS5500_DIO2, TS5500_LCD, TS5600_LCD };
  33. struct ts5500_priv {
  34. const struct ts5500_dio *pinout;
  35. struct gpio_chip gpio_chip;
  36. spinlock_t lock;
  37. bool strap;
  38. u8 hwirq;
  39. };
  40. /*
  41. * Hex 7D is used to control several blocks (e.g. DIO2 and LCD port).
  42. * This flag ensures that the region has been requested by this driver.
  43. */
  44. static bool hex7d_reserved;
  45. /*
  46. * This structure is used to describe capabilities of DIO lines,
  47. * such as available directions and connected interrupt (if any).
  48. */
  49. struct ts5500_dio {
  50. const u8 value_addr;
  51. const u8 value_mask;
  52. const u8 control_addr;
  53. const u8 control_mask;
  54. const bool no_input;
  55. const bool no_output;
  56. const u8 irq;
  57. };
  58. #define TS5500_DIO_IN_OUT(vaddr, vbit, caddr, cbit) \
  59. { \
  60. .value_addr = vaddr, \
  61. .value_mask = BIT(vbit), \
  62. .control_addr = caddr, \
  63. .control_mask = BIT(cbit), \
  64. }
  65. #define TS5500_DIO_IN(addr, bit) \
  66. { \
  67. .value_addr = addr, \
  68. .value_mask = BIT(bit), \
  69. .no_output = true, \
  70. }
  71. #define TS5500_DIO_IN_IRQ(addr, bit, _irq) \
  72. { \
  73. .value_addr = addr, \
  74. .value_mask = BIT(bit), \
  75. .no_output = true, \
  76. .irq = _irq, \
  77. }
  78. #define TS5500_DIO_OUT(addr, bit) \
  79. { \
  80. .value_addr = addr, \
  81. .value_mask = BIT(bit), \
  82. .no_input = true, \
  83. }
  84. /*
  85. * Input/Output DIO lines are programmed in groups of 4. Their values are
  86. * available through 4 consecutive bits in a value port, whereas the direction
  87. * of these 4 lines is driven by only 1 bit in a control port.
  88. */
  89. #define TS5500_DIO_GROUP(vaddr, vbitfrom, caddr, cbit) \
  90. TS5500_DIO_IN_OUT(vaddr, vbitfrom + 0, caddr, cbit), \
  91. TS5500_DIO_IN_OUT(vaddr, vbitfrom + 1, caddr, cbit), \
  92. TS5500_DIO_IN_OUT(vaddr, vbitfrom + 2, caddr, cbit), \
  93. TS5500_DIO_IN_OUT(vaddr, vbitfrom + 3, caddr, cbit)
  94. /*
  95. * TS-5500 DIO1 block
  96. *
  97. * value control dir hw
  98. * addr bit addr bit in out irq name pin offset
  99. *
  100. * 0x7b 0 0x7a 0 x x DIO1_0 1 0
  101. * 0x7b 1 0x7a 0 x x DIO1_1 3 1
  102. * 0x7b 2 0x7a 0 x x DIO1_2 5 2
  103. * 0x7b 3 0x7a 0 x x DIO1_3 7 3
  104. * 0x7b 4 0x7a 1 x x DIO1_4 9 4
  105. * 0x7b 5 0x7a 1 x x DIO1_5 11 5
  106. * 0x7b 6 0x7a 1 x x DIO1_6 13 6
  107. * 0x7b 7 0x7a 1 x x DIO1_7 15 7
  108. * 0x7c 0 0x7a 5 x x DIO1_8 4 8
  109. * 0x7c 1 0x7a 5 x x DIO1_9 6 9
  110. * 0x7c 2 0x7a 5 x x DIO1_10 8 10
  111. * 0x7c 3 0x7a 5 x x DIO1_11 10 11
  112. * 0x7c 4 x DIO1_12 12 12
  113. * 0x7c 5 x 7 DIO1_13 14 13
  114. */
  115. static const struct ts5500_dio ts5500_dio1[] = {
  116. TS5500_DIO_GROUP(0x7b, 0, 0x7a, 0),
  117. TS5500_DIO_GROUP(0x7b, 4, 0x7a, 1),
  118. TS5500_DIO_GROUP(0x7c, 0, 0x7a, 5),
  119. TS5500_DIO_IN(0x7c, 4),
  120. TS5500_DIO_IN_IRQ(0x7c, 5, 7),
  121. };
  122. /*
  123. * TS-5500 DIO2 block
  124. *
  125. * value control dir hw
  126. * addr bit addr bit in out irq name pin offset
  127. *
  128. * 0x7e 0 0x7d 0 x x DIO2_0 1 0
  129. * 0x7e 1 0x7d 0 x x DIO2_1 3 1
  130. * 0x7e 2 0x7d 0 x x DIO2_2 5 2
  131. * 0x7e 3 0x7d 0 x x DIO2_3 7 3
  132. * 0x7e 4 0x7d 1 x x DIO2_4 9 4
  133. * 0x7e 5 0x7d 1 x x DIO2_5 11 5
  134. * 0x7e 6 0x7d 1 x x DIO2_6 13 6
  135. * 0x7e 7 0x7d 1 x x DIO2_7 15 7
  136. * 0x7f 0 0x7d 5 x x DIO2_8 4 8
  137. * 0x7f 1 0x7d 5 x x DIO2_9 6 9
  138. * 0x7f 2 0x7d 5 x x DIO2_10 8 10
  139. * 0x7f 3 0x7d 5 x x DIO2_11 10 11
  140. * 0x7f 4 x 6 DIO2_13 14 12
  141. */
  142. static const struct ts5500_dio ts5500_dio2[] = {
  143. TS5500_DIO_GROUP(0x7e, 0, 0x7d, 0),
  144. TS5500_DIO_GROUP(0x7e, 4, 0x7d, 1),
  145. TS5500_DIO_GROUP(0x7f, 0, 0x7d, 5),
  146. TS5500_DIO_IN_IRQ(0x7f, 4, 6),
  147. };
  148. /*
  149. * TS-5500 LCD port used as DIO block
  150. * TS-5600 LCD port is identical
  151. *
  152. * value control dir hw
  153. * addr bit addr bit in out irq name pin offset
  154. *
  155. * 0x72 0 0x7d 2 x x LCD_0 8 0
  156. * 0x72 1 0x7d 2 x x LCD_1 7 1
  157. * 0x72 2 0x7d 2 x x LCD_2 10 2
  158. * 0x72 3 0x7d 2 x x LCD_3 9 3
  159. * 0x72 4 0x7d 3 x x LCD_4 12 4
  160. * 0x72 5 0x7d 3 x x LCD_5 11 5
  161. * 0x72 6 0x7d 3 x x LCD_6 14 6
  162. * 0x72 7 0x7d 3 x x LCD_7 13 7
  163. * 0x73 0 x LCD_EN 5 8
  164. * 0x73 6 x LCD_WR 6 9
  165. * 0x73 7 x 1 LCD_RS 3 10
  166. */
  167. static const struct ts5500_dio ts5500_lcd[] = {
  168. TS5500_DIO_GROUP(0x72, 0, 0x7d, 2),
  169. TS5500_DIO_GROUP(0x72, 4, 0x7d, 3),
  170. TS5500_DIO_OUT(0x73, 0),
  171. TS5500_DIO_IN(0x73, 6),
  172. TS5500_DIO_IN_IRQ(0x73, 7, 1),
  173. };
  174. static inline struct ts5500_priv *ts5500_gc_to_priv(struct gpio_chip *chip)
  175. {
  176. return container_of(chip, struct ts5500_priv, gpio_chip);
  177. }
  178. static inline void ts5500_set_mask(u8 mask, u8 addr)
  179. {
  180. u8 val = inb(addr);
  181. val |= mask;
  182. outb(val, addr);
  183. }
  184. static inline void ts5500_clear_mask(u8 mask, u8 addr)
  185. {
  186. u8 val = inb(addr);
  187. val &= ~mask;
  188. outb(val, addr);
  189. }
  190. static int ts5500_gpio_input(struct gpio_chip *chip, unsigned offset)
  191. {
  192. struct ts5500_priv *priv = ts5500_gc_to_priv(chip);
  193. const struct ts5500_dio line = priv->pinout[offset];
  194. unsigned long flags;
  195. if (line.no_input)
  196. return -ENXIO;
  197. if (line.no_output)
  198. return 0;
  199. spin_lock_irqsave(&priv->lock, flags);
  200. ts5500_clear_mask(line.control_mask, line.control_addr);
  201. spin_unlock_irqrestore(&priv->lock, flags);
  202. return 0;
  203. }
  204. static int ts5500_gpio_get(struct gpio_chip *chip, unsigned offset)
  205. {
  206. struct ts5500_priv *priv = ts5500_gc_to_priv(chip);
  207. const struct ts5500_dio line = priv->pinout[offset];
  208. return !!(inb(line.value_addr) & line.value_mask);
  209. }
  210. static int ts5500_gpio_output(struct gpio_chip *chip, unsigned offset, int val)
  211. {
  212. struct ts5500_priv *priv = ts5500_gc_to_priv(chip);
  213. const struct ts5500_dio line = priv->pinout[offset];
  214. unsigned long flags;
  215. if (line.no_output)
  216. return -ENXIO;
  217. spin_lock_irqsave(&priv->lock, flags);
  218. if (!line.no_input)
  219. ts5500_set_mask(line.control_mask, line.control_addr);
  220. if (val)
  221. ts5500_set_mask(line.value_mask, line.value_addr);
  222. else
  223. ts5500_clear_mask(line.value_mask, line.value_addr);
  224. spin_unlock_irqrestore(&priv->lock, flags);
  225. return 0;
  226. }
  227. static void ts5500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  228. {
  229. struct ts5500_priv *priv = ts5500_gc_to_priv(chip);
  230. const struct ts5500_dio line = priv->pinout[offset];
  231. unsigned long flags;
  232. spin_lock_irqsave(&priv->lock, flags);
  233. if (val)
  234. ts5500_set_mask(line.value_mask, line.value_addr);
  235. else
  236. ts5500_clear_mask(line.value_mask, line.value_addr);
  237. spin_unlock_irqrestore(&priv->lock, flags);
  238. }
  239. static int ts5500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  240. {
  241. struct ts5500_priv *priv = ts5500_gc_to_priv(chip);
  242. const struct ts5500_dio *block = priv->pinout;
  243. const struct ts5500_dio line = block[offset];
  244. /* Only one pin is connected to an interrupt */
  245. if (line.irq)
  246. return line.irq;
  247. /* As this pin is input-only, we may strap it to another in/out pin */
  248. if (priv->strap)
  249. return priv->hwirq;
  250. return -ENXIO;
  251. }
  252. static int ts5500_enable_irq(struct ts5500_priv *priv)
  253. {
  254. int ret = 0;
  255. unsigned long flags;
  256. spin_lock_irqsave(&priv->lock, flags);
  257. if (priv->hwirq == 7)
  258. ts5500_set_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
  259. else if (priv->hwirq == 6)
  260. ts5500_set_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
  261. else if (priv->hwirq == 1)
  262. ts5500_set_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
  263. else
  264. ret = -EINVAL;
  265. spin_unlock_irqrestore(&priv->lock, flags);
  266. return ret;
  267. }
  268. static void ts5500_disable_irq(struct ts5500_priv *priv)
  269. {
  270. unsigned long flags;
  271. spin_lock_irqsave(&priv->lock, flags);
  272. if (priv->hwirq == 7)
  273. ts5500_clear_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
  274. else if (priv->hwirq == 6)
  275. ts5500_clear_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
  276. else if (priv->hwirq == 1)
  277. ts5500_clear_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
  278. else
  279. dev_err(priv->gpio_chip.dev, "invalid hwirq %d\n", priv->hwirq);
  280. spin_unlock_irqrestore(&priv->lock, flags);
  281. }
  282. static int ts5500_dio_probe(struct platform_device *pdev)
  283. {
  284. enum ts5500_blocks block = platform_get_device_id(pdev)->driver_data;
  285. struct ts5500_dio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  286. struct device *dev = &pdev->dev;
  287. const char *name = dev_name(dev);
  288. struct ts5500_priv *priv;
  289. struct resource *res;
  290. unsigned long flags;
  291. int ret;
  292. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  293. if (!res) {
  294. dev_err(dev, "missing IRQ resource\n");
  295. return -EINVAL;
  296. }
  297. priv = devm_kzalloc(dev, sizeof(struct ts5500_priv), GFP_KERNEL);
  298. if (!priv)
  299. return -ENOMEM;
  300. platform_set_drvdata(pdev, priv);
  301. priv->hwirq = res->start;
  302. spin_lock_init(&priv->lock);
  303. priv->gpio_chip.owner = THIS_MODULE;
  304. priv->gpio_chip.label = name;
  305. priv->gpio_chip.dev = dev;
  306. priv->gpio_chip.direction_input = ts5500_gpio_input;
  307. priv->gpio_chip.direction_output = ts5500_gpio_output;
  308. priv->gpio_chip.get = ts5500_gpio_get;
  309. priv->gpio_chip.set = ts5500_gpio_set;
  310. priv->gpio_chip.to_irq = ts5500_gpio_to_irq;
  311. priv->gpio_chip.base = -1;
  312. if (pdata) {
  313. priv->gpio_chip.base = pdata->base;
  314. priv->strap = pdata->strap;
  315. }
  316. switch (block) {
  317. case TS5500_DIO1:
  318. priv->pinout = ts5500_dio1;
  319. priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio1);
  320. if (!devm_request_region(dev, 0x7a, 3, name)) {
  321. dev_err(dev, "failed to request %s ports\n", name);
  322. return -EBUSY;
  323. }
  324. break;
  325. case TS5500_DIO2:
  326. priv->pinout = ts5500_dio2;
  327. priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio2);
  328. if (!devm_request_region(dev, 0x7e, 2, name)) {
  329. dev_err(dev, "failed to request %s ports\n", name);
  330. return -EBUSY;
  331. }
  332. if (hex7d_reserved)
  333. break;
  334. if (!devm_request_region(dev, 0x7d, 1, name)) {
  335. dev_err(dev, "failed to request %s 7D\n", name);
  336. return -EBUSY;
  337. }
  338. hex7d_reserved = true;
  339. break;
  340. case TS5500_LCD:
  341. case TS5600_LCD:
  342. priv->pinout = ts5500_lcd;
  343. priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_lcd);
  344. if (!devm_request_region(dev, 0x72, 2, name)) {
  345. dev_err(dev, "failed to request %s ports\n", name);
  346. return -EBUSY;
  347. }
  348. if (!hex7d_reserved) {
  349. if (!devm_request_region(dev, 0x7d, 1, name)) {
  350. dev_err(dev, "failed to request %s 7D\n", name);
  351. return -EBUSY;
  352. }
  353. hex7d_reserved = true;
  354. }
  355. /* Ensure usage of LCD port as DIO */
  356. spin_lock_irqsave(&priv->lock, flags);
  357. ts5500_clear_mask(BIT(4), 0x7d);
  358. spin_unlock_irqrestore(&priv->lock, flags);
  359. break;
  360. }
  361. ret = gpiochip_add(&priv->gpio_chip);
  362. if (ret) {
  363. dev_err(dev, "failed to register the gpio chip\n");
  364. return ret;
  365. }
  366. ret = ts5500_enable_irq(priv);
  367. if (ret) {
  368. dev_err(dev, "invalid interrupt %d\n", priv->hwirq);
  369. goto cleanup;
  370. }
  371. return 0;
  372. cleanup:
  373. gpiochip_remove(&priv->gpio_chip);
  374. return ret;
  375. }
  376. static int ts5500_dio_remove(struct platform_device *pdev)
  377. {
  378. struct ts5500_priv *priv = platform_get_drvdata(pdev);
  379. ts5500_disable_irq(priv);
  380. gpiochip_remove(&priv->gpio_chip);
  381. return 0;
  382. }
  383. static const struct platform_device_id ts5500_dio_ids[] = {
  384. { "ts5500-dio1", TS5500_DIO1 },
  385. { "ts5500-dio2", TS5500_DIO2 },
  386. { "ts5500-dio-lcd", TS5500_LCD },
  387. { "ts5600-dio-lcd", TS5600_LCD },
  388. { }
  389. };
  390. MODULE_DEVICE_TABLE(platform, ts5500_dio_ids);
  391. static struct platform_driver ts5500_dio_driver = {
  392. .driver = {
  393. .name = "ts5500-dio",
  394. },
  395. .probe = ts5500_dio_probe,
  396. .remove = ts5500_dio_remove,
  397. .id_table = ts5500_dio_ids,
  398. };
  399. module_platform_driver(ts5500_dio_driver);
  400. MODULE_LICENSE("GPL");
  401. MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
  402. MODULE_DESCRIPTION("Technologic Systems TS-5500 Digital I/O driver");