gpio-generic.c 14 KB

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