basic_mmio_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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/err.h>
  48. #include <linux/bug.h>
  49. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/compiler.h>
  53. #include <linux/types.h>
  54. #include <linux/errno.h>
  55. #include <linux/log2.h>
  56. #include <linux/ioport.h>
  57. #include <linux/io.h>
  58. #include <linux/gpio.h>
  59. #include <linux/slab.h>
  60. #include <linux/platform_device.h>
  61. #include <linux/mod_devicetable.h>
  62. #include <linux/basic_mmio_gpio.h>
  63. static void bgpio_write8(void __iomem *reg, unsigned long data)
  64. {
  65. writeb(data, reg);
  66. }
  67. static unsigned long bgpio_read8(void __iomem *reg)
  68. {
  69. return readb(reg);
  70. }
  71. static void bgpio_write16(void __iomem *reg, unsigned long data)
  72. {
  73. writew(data, reg);
  74. }
  75. static unsigned long bgpio_read16(void __iomem *reg)
  76. {
  77. return readw(reg);
  78. }
  79. static void bgpio_write32(void __iomem *reg, unsigned long data)
  80. {
  81. writel(data, reg);
  82. }
  83. static unsigned long bgpio_read32(void __iomem *reg)
  84. {
  85. return readl(reg);
  86. }
  87. #if BITS_PER_LONG >= 64
  88. static void bgpio_write64(void __iomem *reg, unsigned long data)
  89. {
  90. writeq(data, reg);
  91. }
  92. static unsigned long bgpio_read64(void __iomem *reg)
  93. {
  94. return readq(reg);
  95. }
  96. #endif /* BITS_PER_LONG >= 64 */
  97. static unsigned long bgpio_pin2mask(struct bgpio_chip *bgc, unsigned int pin)
  98. {
  99. return 1 << pin;
  100. }
  101. static unsigned long bgpio_pin2mask_be(struct bgpio_chip *bgc,
  102. unsigned int pin)
  103. {
  104. return 1 << (bgc->bits - 1 - pin);
  105. }
  106. static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
  107. {
  108. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  109. return bgc->read_reg(bgc->reg_dat) & bgc->pin2mask(bgc, gpio);
  110. }
  111. static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  112. {
  113. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  114. unsigned long mask = bgc->pin2mask(bgc, gpio);
  115. unsigned long flags;
  116. spin_lock_irqsave(&bgc->lock, flags);
  117. if (val)
  118. bgc->data |= mask;
  119. else
  120. bgc->data &= ~mask;
  121. bgc->write_reg(bgc->reg_dat, bgc->data);
  122. spin_unlock_irqrestore(&bgc->lock, flags);
  123. }
  124. static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
  125. int val)
  126. {
  127. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  128. unsigned long mask = bgc->pin2mask(bgc, gpio);
  129. if (val)
  130. bgc->write_reg(bgc->reg_set, mask);
  131. else
  132. bgc->write_reg(bgc->reg_clr, mask);
  133. }
  134. static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
  135. {
  136. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  137. unsigned long mask = bgc->pin2mask(bgc, gpio);
  138. unsigned long flags;
  139. spin_lock_irqsave(&bgc->lock, flags);
  140. if (val)
  141. bgc->data |= mask;
  142. else
  143. bgc->data &= ~mask;
  144. bgc->write_reg(bgc->reg_set, bgc->data);
  145. spin_unlock_irqrestore(&bgc->lock, flags);
  146. }
  147. static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
  148. {
  149. return 0;
  150. }
  151. static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
  152. int val)
  153. {
  154. gc->set(gc, gpio, val);
  155. return 0;
  156. }
  157. static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  158. {
  159. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  160. unsigned long flags;
  161. spin_lock_irqsave(&bgc->lock, flags);
  162. bgc->dir &= ~bgc->pin2mask(bgc, gpio);
  163. bgc->write_reg(bgc->reg_dir, bgc->dir);
  164. spin_unlock_irqrestore(&bgc->lock, flags);
  165. return 0;
  166. }
  167. static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  168. {
  169. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  170. unsigned long flags;
  171. gc->set(gc, gpio, val);
  172. spin_lock_irqsave(&bgc->lock, flags);
  173. bgc->dir |= bgc->pin2mask(bgc, gpio);
  174. bgc->write_reg(bgc->reg_dir, bgc->dir);
  175. spin_unlock_irqrestore(&bgc->lock, flags);
  176. return 0;
  177. }
  178. static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio)
  179. {
  180. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  181. unsigned long flags;
  182. spin_lock_irqsave(&bgc->lock, flags);
  183. bgc->dir |= bgc->pin2mask(bgc, gpio);
  184. bgc->write_reg(bgc->reg_dir, bgc->dir);
  185. spin_unlock_irqrestore(&bgc->lock, flags);
  186. return 0;
  187. }
  188. static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
  189. {
  190. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  191. unsigned long flags;
  192. gc->set(gc, gpio, val);
  193. spin_lock_irqsave(&bgc->lock, flags);
  194. bgc->dir &= ~bgc->pin2mask(bgc, gpio);
  195. bgc->write_reg(bgc->reg_dir, bgc->dir);
  196. spin_unlock_irqrestore(&bgc->lock, flags);
  197. return 0;
  198. }
  199. static int bgpio_setup_accessors(struct device *dev,
  200. struct bgpio_chip *bgc,
  201. bool be)
  202. {
  203. switch (bgc->bits) {
  204. case 8:
  205. bgc->read_reg = bgpio_read8;
  206. bgc->write_reg = bgpio_write8;
  207. break;
  208. case 16:
  209. bgc->read_reg = bgpio_read16;
  210. bgc->write_reg = bgpio_write16;
  211. break;
  212. case 32:
  213. bgc->read_reg = bgpio_read32;
  214. bgc->write_reg = bgpio_write32;
  215. break;
  216. #if BITS_PER_LONG >= 64
  217. case 64:
  218. bgc->read_reg = bgpio_read64;
  219. bgc->write_reg = bgpio_write64;
  220. break;
  221. #endif /* BITS_PER_LONG >= 64 */
  222. default:
  223. dev_err(dev, "unsupported data width %u bits\n", bgc->bits);
  224. return -EINVAL;
  225. }
  226. bgc->pin2mask = be ? bgpio_pin2mask_be : bgpio_pin2mask;
  227. return 0;
  228. }
  229. /*
  230. * Create the device and allocate the resources. For setting GPIO's there are
  231. * three supported configurations:
  232. *
  233. * - single input/output register resource (named "dat").
  234. * - set/clear pair (named "set" and "clr").
  235. * - single output register resource and single input resource ("set" and
  236. * dat").
  237. *
  238. * For the single output register, this drives a 1 by setting a bit and a zero
  239. * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
  240. * in the set register and clears it by setting a bit in the clear register.
  241. * The configuration is detected by which resources are present.
  242. *
  243. * For setting the GPIO direction, there are three supported configurations:
  244. *
  245. * - simple bidirection GPIO that requires no configuration.
  246. * - an output direction register (named "dirout") where a 1 bit
  247. * indicates the GPIO is an output.
  248. * - an input direction register (named "dirin") where a 1 bit indicates
  249. * the GPIO is an input.
  250. */
  251. static int bgpio_setup_io(struct bgpio_chip *bgc,
  252. void __iomem *dat,
  253. void __iomem *set,
  254. void __iomem *clr)
  255. {
  256. bgc->reg_dat = dat;
  257. if (!bgc->reg_dat)
  258. return -EINVAL;
  259. if (set && clr) {
  260. bgc->reg_set = set;
  261. bgc->reg_clr = clr;
  262. bgc->gc.set = bgpio_set_with_clear;
  263. } else if (set && !clr) {
  264. bgc->reg_set = set;
  265. bgc->gc.set = bgpio_set_set;
  266. } else {
  267. bgc->gc.set = bgpio_set;
  268. }
  269. bgc->gc.get = bgpio_get;
  270. return 0;
  271. }
  272. static int bgpio_setup_direction(struct bgpio_chip *bgc,
  273. void __iomem *dirout,
  274. void __iomem *dirin)
  275. {
  276. if (dirout && dirin) {
  277. return -EINVAL;
  278. } else if (dirout) {
  279. bgc->reg_dir = dirout;
  280. bgc->gc.direction_output = bgpio_dir_out;
  281. bgc->gc.direction_input = bgpio_dir_in;
  282. } else if (dirin) {
  283. bgc->reg_dir = dirin;
  284. bgc->gc.direction_output = bgpio_dir_out_inv;
  285. bgc->gc.direction_input = bgpio_dir_in_inv;
  286. } else {
  287. bgc->gc.direction_output = bgpio_simple_dir_out;
  288. bgc->gc.direction_input = bgpio_simple_dir_in;
  289. }
  290. return 0;
  291. }
  292. int __devexit bgpio_remove(struct bgpio_chip *bgc)
  293. {
  294. int err = gpiochip_remove(&bgc->gc);
  295. kfree(bgc);
  296. return err;
  297. }
  298. EXPORT_SYMBOL_GPL(bgpio_remove);
  299. int __devinit bgpio_init(struct bgpio_chip *bgc,
  300. struct device *dev,
  301. unsigned long sz,
  302. void __iomem *dat,
  303. void __iomem *set,
  304. void __iomem *clr,
  305. void __iomem *dirout,
  306. void __iomem *dirin,
  307. bool big_endian)
  308. {
  309. int ret;
  310. if (!is_power_of_2(sz))
  311. return -EINVAL;
  312. bgc->bits = sz * 8;
  313. if (bgc->bits > BITS_PER_LONG)
  314. return -EINVAL;
  315. spin_lock_init(&bgc->lock);
  316. bgc->gc.dev = dev;
  317. bgc->gc.label = dev_name(dev);
  318. bgc->gc.base = -1;
  319. bgc->gc.ngpio = bgc->bits;
  320. ret = bgpio_setup_io(bgc, dat, set, clr);
  321. if (ret)
  322. return ret;
  323. ret = bgpio_setup_accessors(dev, bgc, big_endian);
  324. if (ret)
  325. return ret;
  326. ret = bgpio_setup_direction(bgc, dirout, dirin);
  327. if (ret)
  328. return ret;
  329. bgc->data = bgc->read_reg(bgc->reg_dat);
  330. return ret;
  331. }
  332. EXPORT_SYMBOL_GPL(bgpio_init);
  333. #ifdef CONFIG_GPIO_BASIC_MMIO
  334. static void __iomem *bgpio_map(struct platform_device *pdev,
  335. const char *name,
  336. resource_size_t sane_sz,
  337. int *err)
  338. {
  339. struct device *dev = &pdev->dev;
  340. struct resource *r;
  341. resource_size_t start;
  342. resource_size_t sz;
  343. void __iomem *ret;
  344. *err = 0;
  345. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  346. if (!r)
  347. return NULL;
  348. sz = resource_size(r);
  349. if (sz != sane_sz) {
  350. *err = -EINVAL;
  351. return NULL;
  352. }
  353. start = r->start;
  354. if (!devm_request_mem_region(dev, start, sz, r->name)) {
  355. *err = -EBUSY;
  356. return NULL;
  357. }
  358. ret = devm_ioremap(dev, start, sz);
  359. if (!ret) {
  360. *err = -ENOMEM;
  361. return NULL;
  362. }
  363. return ret;
  364. }
  365. static int __devinit bgpio_pdev_probe(struct platform_device *pdev)
  366. {
  367. struct device *dev = &pdev->dev;
  368. struct resource *r;
  369. void __iomem *dat;
  370. void __iomem *set;
  371. void __iomem *clr;
  372. void __iomem *dirout;
  373. void __iomem *dirin;
  374. unsigned long sz;
  375. bool be;
  376. int err;
  377. struct bgpio_chip *bgc;
  378. struct bgpio_pdata *pdata = dev_get_platdata(dev);
  379. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
  380. if (!r)
  381. return -EINVAL;
  382. sz = resource_size(r);
  383. dat = bgpio_map(pdev, "dat", sz, &err);
  384. if (!dat)
  385. return err ? err : -EINVAL;
  386. set = bgpio_map(pdev, "set", sz, &err);
  387. if (err)
  388. return err;
  389. clr = bgpio_map(pdev, "clr", sz, &err);
  390. if (err)
  391. return err;
  392. dirout = bgpio_map(pdev, "dirout", sz, &err);
  393. if (err)
  394. return err;
  395. dirin = bgpio_map(pdev, "dirin", sz, &err);
  396. if (err)
  397. return err;
  398. be = !strcmp(platform_get_device_id(pdev)->name, "basic-mmio-gpio-be");
  399. bgc = devm_kzalloc(&pdev->dev, sizeof(*bgc), GFP_KERNEL);
  400. if (!bgc)
  401. return -ENOMEM;
  402. err = bgpio_init(bgc, dev, sz, dat, set, clr, dirout, dirin, be);
  403. if (err)
  404. return err;
  405. if (pdata) {
  406. bgc->gc.base = pdata->base;
  407. if (pdata->ngpio > 0)
  408. bgc->gc.ngpio = pdata->ngpio;
  409. }
  410. platform_set_drvdata(pdev, bgc);
  411. return gpiochip_add(&bgc->gc);
  412. }
  413. static int __devexit bgpio_pdev_remove(struct platform_device *pdev)
  414. {
  415. struct bgpio_chip *bgc = platform_get_drvdata(pdev);
  416. return bgpio_remove(bgc);
  417. }
  418. static const struct platform_device_id bgpio_id_table[] = {
  419. { "basic-mmio-gpio", },
  420. { "basic-mmio-gpio-be", },
  421. {},
  422. };
  423. MODULE_DEVICE_TABLE(platform, bgpio_id_table);
  424. static struct platform_driver bgpio_driver = {
  425. .driver = {
  426. .name = "basic-mmio-gpio",
  427. },
  428. .id_table = bgpio_id_table,
  429. .probe = bgpio_pdev_probe,
  430. .remove = __devexit_p(bgpio_pdev_remove),
  431. };
  432. static int __init bgpio_platform_init(void)
  433. {
  434. return platform_driver_register(&bgpio_driver);
  435. }
  436. module_init(bgpio_platform_init);
  437. static void __exit bgpio_platform_exit(void)
  438. {
  439. platform_driver_unregister(&bgpio_driver);
  440. }
  441. module_exit(bgpio_platform_exit);
  442. #endif /* CONFIG_GPIO_BASIC_MMIO */
  443. MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
  444. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  445. MODULE_LICENSE("GPL");