basic_mmio_gpio.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Driver for basic memory-mapped GPIO controllers.
  3. *
  4. * Copyright 2008 MontaVista Software, Inc.
  5. * Copyright 2008,2010 Anton Vorontsov <cbouatmailru@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * ....``.```~~~~````.`.`.`.`.```````'',,,.........`````......`.......
  13. * ...`` ```````..
  14. * ..The simplest form of a GPIO controller that the driver supports is``
  15. * `.just a single "data" register, where GPIO state can be read and/or `
  16. * `,..written. ,,..``~~~~ .....``.`.`.~~.```.`.........``````.```````
  17. * `````````
  18. ___
  19. _/~~|___/~| . ```~~~~~~ ___/___\___ ,~.`.`.`.`````.~~...,,,,...
  20. __________|~$@~~~ %~ /o*o*o*o*o*o\ .. Implementing such a GPIO .
  21. o ` ~~~~\___/~~~~ ` controller in FPGA is ,.`
  22. `....trivial..'~`.```.```
  23. * ```````
  24. * .```````~~~~`..`.``.``.
  25. * . The driver supports `... ,..```.`~~~```````````````....````.``,,
  26. * . big-endian notation, just`. .. A bit more sophisticated controllers ,
  27. * . register the device with -be`. .with a pair of set/clear-bit registers ,
  28. * `.. suffix. ```~~`````....`.` . affecting the data register and the .`
  29. * ``.`.``...``` ```.. output pins are also supported.`
  30. * ^^ `````.`````````.,``~``~``~~``````
  31. * . ^^
  32. * ,..`.`.`...````````````......`.`.`.`.`.`..`.`.`..
  33. * .. The expectation is that in at least some cases . ,-~~~-,
  34. * .this will be used with roll-your-own ASIC/FPGA .` \ /
  35. * .logic in Verilog or VHDL. ~~~`````````..`````~~` \ /
  36. * ..````````......``````````` \o_
  37. * |
  38. * ^^ / \
  39. *
  40. * ...`````~~`.....``.`..........``````.`.``.```........``.
  41. * ` 8, 16, 32 and 64 bits registers are supported, and``.
  42. * . the number of GPIOs is determined by the width of ~
  43. * .. the registers. ,............```.`.`..`.`.~~~.`.`.`~
  44. * `.......````.```
  45. */
  46. #include <linux/init.h>
  47. #include <linux/bug.h>
  48. #include <linux/kernel.h>
  49. #include <linux/module.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/compiler.h>
  52. #include <linux/types.h>
  53. #include <linux/errno.h>
  54. #include <linux/log2.h>
  55. #include <linux/ioport.h>
  56. #include <linux/io.h>
  57. #include <linux/gpio.h>
  58. #include <linux/slab.h>
  59. #include <linux/platform_device.h>
  60. #include <linux/mod_devicetable.h>
  61. #include <linux/basic_mmio_gpio.h>
  62. struct bgpio_chip {
  63. struct gpio_chip gc;
  64. unsigned long (*read_reg)(void __iomem *reg);
  65. void (*write_reg)(void __iomem *reg, unsigned long data);
  66. void __iomem *reg_dat;
  67. void __iomem *reg_set;
  68. void __iomem *reg_clr;
  69. /* Number of bits (GPIOs): <register width> * 8. */
  70. int bits;
  71. /*
  72. * Some GPIO controllers work with the big-endian bits notation,
  73. * e.g. in a 8-bits register, GPIO7 is the least significant bit.
  74. */
  75. unsigned long (*pin2mask)(struct bgpio_chip *bgc, unsigned int pin);
  76. /*
  77. * Used to lock bgpio_chip->data. Also, this is needed to keep
  78. * shadowed and real data registers writes together.
  79. */
  80. spinlock_t lock;
  81. /* Shadowed data register to clear/set bits safely. */
  82. unsigned long data;
  83. };
  84. static struct bgpio_chip *to_bgpio_chip(struct gpio_chip *gc)
  85. {
  86. return container_of(gc, struct bgpio_chip, gc);
  87. }
  88. static void bgpio_write8(void __iomem *reg, unsigned long data)
  89. {
  90. __raw_writeb(data, reg);
  91. }
  92. static unsigned long bgpio_read8(void __iomem *reg)
  93. {
  94. return __raw_readb(reg);
  95. }
  96. static void bgpio_write16(void __iomem *reg, unsigned long data)
  97. {
  98. __raw_writew(data, reg);
  99. }
  100. static unsigned long bgpio_read16(void __iomem *reg)
  101. {
  102. return __raw_readw(reg);
  103. }
  104. static void bgpio_write32(void __iomem *reg, unsigned long data)
  105. {
  106. __raw_writel(data, reg);
  107. }
  108. static unsigned long bgpio_read32(void __iomem *reg)
  109. {
  110. return __raw_readl(reg);
  111. }
  112. #if BITS_PER_LONG >= 64
  113. static void bgpio_write64(void __iomem *reg, unsigned long data)
  114. {
  115. __raw_writeq(data, reg);
  116. }
  117. static unsigned long bgpio_read64(void __iomem *reg)
  118. {
  119. return __raw_readq(reg);
  120. }
  121. #endif /* BITS_PER_LONG >= 64 */
  122. static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
  123. {
  124. return 1 << pin;
  125. }
  126. static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
  127. unsigned int pin)
  128. {
  129. return 1 << (bgc->bits - 1 - pin);
  130. }
  131. static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
  132. {
  133. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  134. return bgc->read_reg(bgc->reg_dat) & bgc->pin2mask(bgc, gpio);
  135. }
  136. static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  137. {
  138. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  139. unsigned long mask = bgc->pin2mask(bgc, gpio);
  140. unsigned long flags;
  141. if (bgc->reg_set) {
  142. if (val)
  143. bgc->write_reg(bgc->reg_set, mask);
  144. else
  145. bgc->write_reg(bgc->reg_clr, mask);
  146. return;
  147. }
  148. spin_lock_irqsave(&bgc->lock, flags);
  149. if (val)
  150. bgc->data |= mask;
  151. else
  152. bgc->data &= ~mask;
  153. bgc->write_reg(bgc->reg_dat, bgc->data);
  154. spin_unlock_irqrestore(&bgc->lock, flags);
  155. }
  156. static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  157. {
  158. return 0;
  159. }
  160. static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  161. {
  162. bgpio_set(gc, gpio, val);
  163. return 0;
  164. }
  165. static int bgpio_setup_accessors(struct platform_device *pdev,
  166. struct bgpio_chip *bgc)
  167. {
  168. const struct platform_device_id *platid = platform_get_device_id(pdev);
  169. switch (bgc->bits) {
  170. case 8:
  171. bgc->read_reg = bgpio_read8;
  172. bgc->write_reg = bgpio_write8;
  173. break;
  174. case 16:
  175. bgc->read_reg = bgpio_read16;
  176. bgc->write_reg = bgpio_write16;
  177. break;
  178. case 32:
  179. bgc->read_reg = bgpio_read32;
  180. bgc->write_reg = bgpio_write32;
  181. break;
  182. #if BITS_PER_LONG >= 64
  183. case 64:
  184. bgc->read_reg = bgpio_read64;
  185. bgc->write_reg = bgpio_write64;
  186. break;
  187. #endif /* BITS_PER_LONG >= 64 */
  188. default:
  189. dev_err(&pdev->dev, "unsupported data width %u bits\n",
  190. bgc->bits);
  191. return -EINVAL;
  192. }
  193. bgc->pin2mask = strcmp(platid->name, "basic-mmio-gpio-be") ?
  194. bgpio_pin2mask : bgpio_pin2mask_be;
  195. return 0;
  196. }
  197. static int __devinit bgpio_probe(struct platform_device *pdev)
  198. {
  199. struct device *dev = &pdev->dev;
  200. struct bgpio_pdata *pdata = dev_get_platdata(dev);
  201. struct bgpio_chip *bgc;
  202. struct resource *res_dat;
  203. struct resource *res_set;
  204. struct resource *res_clr;
  205. resource_size_t dat_sz;
  206. int bits;
  207. int ret;
  208. res_dat = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
  209. if (!res_dat)
  210. return -EINVAL;
  211. dat_sz = resource_size(res_dat);
  212. if (!is_power_of_2(dat_sz))
  213. return -EINVAL;
  214. bits = dat_sz * 8;
  215. if (bits > BITS_PER_LONG)
  216. return -EINVAL;
  217. bgc = devm_kzalloc(dev, sizeof(*bgc), GFP_KERNEL);
  218. if (!bgc)
  219. return -ENOMEM;
  220. bgc->reg_dat = devm_ioremap(dev, res_dat->start, dat_sz);
  221. if (!bgc->reg_dat)
  222. return -ENOMEM;
  223. res_set = platform_get_resource_byname(pdev, IORESOURCE_MEM, "set");
  224. res_clr = platform_get_resource_byname(pdev, IORESOURCE_MEM, "clr");
  225. if (res_set && res_clr) {
  226. if (resource_size(res_set) != resource_size(res_clr) ||
  227. resource_size(res_set) != dat_sz)
  228. return -EINVAL;
  229. bgc->reg_set = devm_ioremap(dev, res_set->start, dat_sz);
  230. bgc->reg_clr = devm_ioremap(dev, res_clr->start, dat_sz);
  231. if (!bgc->reg_set || !bgc->reg_clr)
  232. return -ENOMEM;
  233. } else if (res_set || res_clr) {
  234. return -EINVAL;
  235. }
  236. spin_lock_init(&bgc->lock);
  237. bgc->bits = bits;
  238. ret = bgpio_setup_accessors(pdev, bgc);
  239. if (ret)
  240. return ret;
  241. bgc->data = bgc->read_reg(bgc->reg_dat);
  242. bgc->gc.ngpio = bits;
  243. bgc->gc.direction_input = bgpio_dir_in;
  244. bgc->gc.direction_output = bgpio_dir_out;
  245. bgc->gc.get = bgpio_get;
  246. bgc->gc.set = bgpio_set;
  247. bgc->gc.dev = dev;
  248. bgc->gc.label = dev_name(dev);
  249. if (pdata)
  250. bgc->gc.base = pdata->base;
  251. else
  252. bgc->gc.base = -1;
  253. platform_set_drvdata(pdev, bgc);
  254. ret = gpiochip_add(&bgc->gc);
  255. if (ret)
  256. dev_err(dev, "gpiochip_add() failed: %d\n", ret);
  257. return ret;
  258. }
  259. static int __devexit bgpio_remove(struct platform_device *pdev)
  260. {
  261. struct bgpio_chip *bgc = platform_get_drvdata(pdev);
  262. return gpiochip_remove(&bgc->gc);
  263. }
  264. static const struct platform_device_id bgpio_id_table[] = {
  265. { "basic-mmio-gpio", },
  266. { "basic-mmio-gpio-be", },
  267. {},
  268. };
  269. MODULE_DEVICE_TABLE(platform, bgpio_id_table);
  270. static struct platform_driver bgpio_driver = {
  271. .driver = {
  272. .name = "basic-mmio-gpio",
  273. },
  274. .id_table = bgpio_id_table,
  275. .probe = bgpio_probe,
  276. .remove = __devexit_p(bgpio_remove),
  277. };
  278. static int __init bgpio_init(void)
  279. {
  280. return platform_driver_register(&bgpio_driver);
  281. }
  282. module_init(bgpio_init);
  283. static void __exit bgpio_exit(void)
  284. {
  285. platform_driver_unregister(&bgpio_driver);
  286. }
  287. module_exit(bgpio_exit);
  288. MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
  289. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  290. MODULE_LICENSE("GPL");