gpio-ts5500.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 void ts5500_set_mask(u8 mask, u8 addr)
  175. {
  176. u8 val = inb(addr);
  177. val |= mask;
  178. outb(val, addr);
  179. }
  180. static inline void ts5500_clear_mask(u8 mask, u8 addr)
  181. {
  182. u8 val = inb(addr);
  183. val &= ~mask;
  184. outb(val, addr);
  185. }
  186. static int ts5500_gpio_input(struct gpio_chip *chip, unsigned offset)
  187. {
  188. struct ts5500_priv *priv = gpiochip_get_data(chip);
  189. const struct ts5500_dio line = priv->pinout[offset];
  190. unsigned long flags;
  191. if (line.no_input)
  192. return -ENXIO;
  193. if (line.no_output)
  194. return 0;
  195. spin_lock_irqsave(&priv->lock, flags);
  196. ts5500_clear_mask(line.control_mask, line.control_addr);
  197. spin_unlock_irqrestore(&priv->lock, flags);
  198. return 0;
  199. }
  200. static int ts5500_gpio_get(struct gpio_chip *chip, unsigned offset)
  201. {
  202. struct ts5500_priv *priv = gpiochip_get_data(chip);
  203. const struct ts5500_dio line = priv->pinout[offset];
  204. return !!(inb(line.value_addr) & line.value_mask);
  205. }
  206. static int ts5500_gpio_output(struct gpio_chip *chip, unsigned offset, int val)
  207. {
  208. struct ts5500_priv *priv = gpiochip_get_data(chip);
  209. const struct ts5500_dio line = priv->pinout[offset];
  210. unsigned long flags;
  211. if (line.no_output)
  212. return -ENXIO;
  213. spin_lock_irqsave(&priv->lock, flags);
  214. if (!line.no_input)
  215. ts5500_set_mask(line.control_mask, line.control_addr);
  216. if (val)
  217. ts5500_set_mask(line.value_mask, line.value_addr);
  218. else
  219. ts5500_clear_mask(line.value_mask, line.value_addr);
  220. spin_unlock_irqrestore(&priv->lock, flags);
  221. return 0;
  222. }
  223. static void ts5500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  224. {
  225. struct ts5500_priv *priv = gpiochip_get_data(chip);
  226. const struct ts5500_dio line = priv->pinout[offset];
  227. unsigned long flags;
  228. spin_lock_irqsave(&priv->lock, flags);
  229. if (val)
  230. ts5500_set_mask(line.value_mask, line.value_addr);
  231. else
  232. ts5500_clear_mask(line.value_mask, line.value_addr);
  233. spin_unlock_irqrestore(&priv->lock, flags);
  234. }
  235. static int ts5500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
  236. {
  237. struct ts5500_priv *priv = gpiochip_get_data(chip);
  238. const struct ts5500_dio *block = priv->pinout;
  239. const struct ts5500_dio line = block[offset];
  240. /* Only one pin is connected to an interrupt */
  241. if (line.irq)
  242. return line.irq;
  243. /* As this pin is input-only, we may strap it to another in/out pin */
  244. if (priv->strap)
  245. return priv->hwirq;
  246. return -ENXIO;
  247. }
  248. static int ts5500_enable_irq(struct ts5500_priv *priv)
  249. {
  250. int ret = 0;
  251. unsigned long flags;
  252. spin_lock_irqsave(&priv->lock, flags);
  253. if (priv->hwirq == 7)
  254. ts5500_set_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
  255. else if (priv->hwirq == 6)
  256. ts5500_set_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
  257. else if (priv->hwirq == 1)
  258. ts5500_set_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
  259. else
  260. ret = -EINVAL;
  261. spin_unlock_irqrestore(&priv->lock, flags);
  262. return ret;
  263. }
  264. static void ts5500_disable_irq(struct ts5500_priv *priv)
  265. {
  266. unsigned long flags;
  267. spin_lock_irqsave(&priv->lock, flags);
  268. if (priv->hwirq == 7)
  269. ts5500_clear_mask(BIT(7), 0x7a); /* DIO1_13 on IRQ7 */
  270. else if (priv->hwirq == 6)
  271. ts5500_clear_mask(BIT(7), 0x7d); /* DIO2_13 on IRQ6 */
  272. else if (priv->hwirq == 1)
  273. ts5500_clear_mask(BIT(6), 0x7d); /* LCD_RS on IRQ1 */
  274. else
  275. dev_err(priv->gpio_chip.parent, "invalid hwirq %d\n",
  276. priv->hwirq);
  277. spin_unlock_irqrestore(&priv->lock, flags);
  278. }
  279. static int ts5500_dio_probe(struct platform_device *pdev)
  280. {
  281. enum ts5500_blocks block = platform_get_device_id(pdev)->driver_data;
  282. struct ts5500_dio_platform_data *pdata = dev_get_platdata(&pdev->dev);
  283. struct device *dev = &pdev->dev;
  284. const char *name = dev_name(dev);
  285. struct ts5500_priv *priv;
  286. struct resource *res;
  287. unsigned long flags;
  288. int ret;
  289. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  290. if (!res) {
  291. dev_err(dev, "missing IRQ resource\n");
  292. return -EINVAL;
  293. }
  294. priv = devm_kzalloc(dev, sizeof(struct ts5500_priv), GFP_KERNEL);
  295. if (!priv)
  296. return -ENOMEM;
  297. platform_set_drvdata(pdev, priv);
  298. priv->hwirq = res->start;
  299. spin_lock_init(&priv->lock);
  300. priv->gpio_chip.owner = THIS_MODULE;
  301. priv->gpio_chip.label = name;
  302. priv->gpio_chip.parent = dev;
  303. priv->gpio_chip.direction_input = ts5500_gpio_input;
  304. priv->gpio_chip.direction_output = ts5500_gpio_output;
  305. priv->gpio_chip.get = ts5500_gpio_get;
  306. priv->gpio_chip.set = ts5500_gpio_set;
  307. priv->gpio_chip.to_irq = ts5500_gpio_to_irq;
  308. priv->gpio_chip.base = -1;
  309. if (pdata) {
  310. priv->gpio_chip.base = pdata->base;
  311. priv->strap = pdata->strap;
  312. }
  313. switch (block) {
  314. case TS5500_DIO1:
  315. priv->pinout = ts5500_dio1;
  316. priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio1);
  317. if (!devm_request_region(dev, 0x7a, 3, name)) {
  318. dev_err(dev, "failed to request %s ports\n", name);
  319. return -EBUSY;
  320. }
  321. break;
  322. case TS5500_DIO2:
  323. priv->pinout = ts5500_dio2;
  324. priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_dio2);
  325. if (!devm_request_region(dev, 0x7e, 2, name)) {
  326. dev_err(dev, "failed to request %s ports\n", name);
  327. return -EBUSY;
  328. }
  329. if (hex7d_reserved)
  330. break;
  331. if (!devm_request_region(dev, 0x7d, 1, name)) {
  332. dev_err(dev, "failed to request %s 7D\n", name);
  333. return -EBUSY;
  334. }
  335. hex7d_reserved = true;
  336. break;
  337. case TS5500_LCD:
  338. case TS5600_LCD:
  339. priv->pinout = ts5500_lcd;
  340. priv->gpio_chip.ngpio = ARRAY_SIZE(ts5500_lcd);
  341. if (!devm_request_region(dev, 0x72, 2, name)) {
  342. dev_err(dev, "failed to request %s ports\n", name);
  343. return -EBUSY;
  344. }
  345. if (!hex7d_reserved) {
  346. if (!devm_request_region(dev, 0x7d, 1, name)) {
  347. dev_err(dev, "failed to request %s 7D\n", name);
  348. return -EBUSY;
  349. }
  350. hex7d_reserved = true;
  351. }
  352. /* Ensure usage of LCD port as DIO */
  353. spin_lock_irqsave(&priv->lock, flags);
  354. ts5500_clear_mask(BIT(4), 0x7d);
  355. spin_unlock_irqrestore(&priv->lock, flags);
  356. break;
  357. }
  358. ret = devm_gpiochip_add_data(dev, &priv->gpio_chip, priv);
  359. if (ret) {
  360. dev_err(dev, "failed to register the gpio chip\n");
  361. return ret;
  362. }
  363. ret = ts5500_enable_irq(priv);
  364. if (ret) {
  365. dev_err(dev, "invalid interrupt %d\n", priv->hwirq);
  366. return ret;
  367. }
  368. return 0;
  369. }
  370. static int ts5500_dio_remove(struct platform_device *pdev)
  371. {
  372. struct ts5500_priv *priv = platform_get_drvdata(pdev);
  373. ts5500_disable_irq(priv);
  374. return 0;
  375. }
  376. static const struct platform_device_id ts5500_dio_ids[] = {
  377. { "ts5500-dio1", TS5500_DIO1 },
  378. { "ts5500-dio2", TS5500_DIO2 },
  379. { "ts5500-dio-lcd", TS5500_LCD },
  380. { "ts5600-dio-lcd", TS5600_LCD },
  381. { }
  382. };
  383. MODULE_DEVICE_TABLE(platform, ts5500_dio_ids);
  384. static struct platform_driver ts5500_dio_driver = {
  385. .driver = {
  386. .name = "ts5500-dio",
  387. },
  388. .probe = ts5500_dio_probe,
  389. .remove = ts5500_dio_remove,
  390. .id_table = ts5500_dio_ids,
  391. };
  392. module_platform_driver(ts5500_dio_driver);
  393. MODULE_LICENSE("GPL");
  394. MODULE_AUTHOR("Savoir-faire Linux Inc. <kernel@savoirfairelinux.com>");
  395. MODULE_DESCRIPTION("Technologic Systems TS-5500 Digital I/O driver");