gpio-generic.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 void bgpio_multiple_get_masks(struct bgpio_chip *bgc,
  164. unsigned long *mask, unsigned long *bits,
  165. unsigned long *set_mask,
  166. unsigned long *clear_mask)
  167. {
  168. int i;
  169. *set_mask = 0;
  170. *clear_mask = 0;
  171. for (i = 0; i < bgc->bits; i++) {
  172. if (*mask == 0)
  173. break;
  174. if (__test_and_clear_bit(i, mask)) {
  175. if (test_bit(i, bits))
  176. *set_mask |= bgc->pin2mask(bgc, i);
  177. else
  178. *clear_mask |= bgc->pin2mask(bgc, i);
  179. }
  180. }
  181. }
  182. static void bgpio_set_multiple_single_reg(struct bgpio_chip *bgc,
  183. unsigned long *mask,
  184. unsigned long *bits,
  185. void __iomem *reg)
  186. {
  187. unsigned long flags;
  188. unsigned long set_mask, clear_mask;
  189. spin_lock_irqsave(&bgc->lock, flags);
  190. bgpio_multiple_get_masks(bgc, mask, bits, &set_mask, &clear_mask);
  191. bgc->data |= set_mask;
  192. bgc->data &= ~clear_mask;
  193. bgc->write_reg(reg, bgc->data);
  194. spin_unlock_irqrestore(&bgc->lock, flags);
  195. }
  196. static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  197. unsigned long *bits)
  198. {
  199. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  200. bgpio_set_multiple_single_reg(bgc, mask, bits, bgc->reg_dat);
  201. }
  202. static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
  203. unsigned long *bits)
  204. {
  205. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  206. bgpio_set_multiple_single_reg(bgc, mask, bits, bgc->reg_set);
  207. }
  208. static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
  209. unsigned long *mask,
  210. unsigned long *bits)
  211. {
  212. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  213. unsigned long set_mask, clear_mask;
  214. bgpio_multiple_get_masks(bgc, mask, bits, &set_mask, &clear_mask);
  215. if (set_mask)
  216. bgc->write_reg(bgc->reg_set, set_mask);
  217. if (clear_mask)
  218. bgc->write_reg(bgc->reg_clr, clear_mask);
  219. }
  220. static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
  221. {
  222. return 0;
  223. }
  224. static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
  225. int val)
  226. {
  227. gc->set(gc, gpio, val);
  228. return 0;
  229. }
  230. static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  231. {
  232. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  233. unsigned long flags;
  234. spin_lock_irqsave(&bgc->lock, flags);
  235. bgc->dir &= ~bgc->pin2mask(bgc, gpio);
  236. bgc->write_reg(bgc->reg_dir, bgc->dir);
  237. spin_unlock_irqrestore(&bgc->lock, flags);
  238. return 0;
  239. }
  240. static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  241. {
  242. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  243. unsigned long flags;
  244. gc->set(gc, gpio, val);
  245. spin_lock_irqsave(&bgc->lock, flags);
  246. bgc->dir |= bgc->pin2mask(bgc, gpio);
  247. bgc->write_reg(bgc->reg_dir, bgc->dir);
  248. spin_unlock_irqrestore(&bgc->lock, flags);
  249. return 0;
  250. }
  251. static int bgpio_dir_in_inv(struct gpio_chip *gc, unsigned int gpio)
  252. {
  253. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  254. unsigned long flags;
  255. spin_lock_irqsave(&bgc->lock, flags);
  256. bgc->dir |= bgc->pin2mask(bgc, gpio);
  257. bgc->write_reg(bgc->reg_dir, bgc->dir);
  258. spin_unlock_irqrestore(&bgc->lock, flags);
  259. return 0;
  260. }
  261. static int bgpio_dir_out_inv(struct gpio_chip *gc, unsigned int gpio, int val)
  262. {
  263. struct bgpio_chip *bgc = to_bgpio_chip(gc);
  264. unsigned long flags;
  265. gc->set(gc, gpio, val);
  266. spin_lock_irqsave(&bgc->lock, flags);
  267. bgc->dir &= ~bgc->pin2mask(bgc, gpio);
  268. bgc->write_reg(bgc->reg_dir, bgc->dir);
  269. spin_unlock_irqrestore(&bgc->lock, flags);
  270. return 0;
  271. }
  272. static int bgpio_setup_accessors(struct device *dev,
  273. struct bgpio_chip *bgc,
  274. bool bit_be,
  275. bool byte_be)
  276. {
  277. switch (bgc->bits) {
  278. case 8:
  279. bgc->read_reg = bgpio_read8;
  280. bgc->write_reg = bgpio_write8;
  281. break;
  282. case 16:
  283. if (byte_be) {
  284. bgc->read_reg = bgpio_read16be;
  285. bgc->write_reg = bgpio_write16be;
  286. } else {
  287. bgc->read_reg = bgpio_read16;
  288. bgc->write_reg = bgpio_write16;
  289. }
  290. break;
  291. case 32:
  292. if (byte_be) {
  293. bgc->read_reg = bgpio_read32be;
  294. bgc->write_reg = bgpio_write32be;
  295. } else {
  296. bgc->read_reg = bgpio_read32;
  297. bgc->write_reg = bgpio_write32;
  298. }
  299. break;
  300. #if BITS_PER_LONG >= 64
  301. case 64:
  302. if (byte_be) {
  303. dev_err(dev,
  304. "64 bit big endian byte order unsupported\n");
  305. return -EINVAL;
  306. } else {
  307. bgc->read_reg = bgpio_read64;
  308. bgc->write_reg = bgpio_write64;
  309. }
  310. break;
  311. #endif /* BITS_PER_LONG >= 64 */
  312. default:
  313. dev_err(dev, "unsupported data width %u bits\n", bgc->bits);
  314. return -EINVAL;
  315. }
  316. bgc->pin2mask = bit_be ? bgpio_pin2mask_be : bgpio_pin2mask;
  317. return 0;
  318. }
  319. /*
  320. * Create the device and allocate the resources. For setting GPIO's there are
  321. * three supported configurations:
  322. *
  323. * - single input/output register resource (named "dat").
  324. * - set/clear pair (named "set" and "clr").
  325. * - single output register resource and single input resource ("set" and
  326. * dat").
  327. *
  328. * For the single output register, this drives a 1 by setting a bit and a zero
  329. * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
  330. * in the set register and clears it by setting a bit in the clear register.
  331. * The configuration is detected by which resources are present.
  332. *
  333. * For setting the GPIO direction, there are three supported configurations:
  334. *
  335. * - simple bidirection GPIO that requires no configuration.
  336. * - an output direction register (named "dirout") where a 1 bit
  337. * indicates the GPIO is an output.
  338. * - an input direction register (named "dirin") where a 1 bit indicates
  339. * the GPIO is an input.
  340. */
  341. static int bgpio_setup_io(struct bgpio_chip *bgc,
  342. void __iomem *dat,
  343. void __iomem *set,
  344. void __iomem *clr)
  345. {
  346. bgc->reg_dat = dat;
  347. if (!bgc->reg_dat)
  348. return -EINVAL;
  349. if (set && clr) {
  350. bgc->reg_set = set;
  351. bgc->reg_clr = clr;
  352. bgc->gc.set = bgpio_set_with_clear;
  353. bgc->gc.set_multiple = bgpio_set_multiple_with_clear;
  354. } else if (set && !clr) {
  355. bgc->reg_set = set;
  356. bgc->gc.set = bgpio_set_set;
  357. bgc->gc.set_multiple = bgpio_set_multiple_set;
  358. } else {
  359. bgc->gc.set = bgpio_set;
  360. bgc->gc.set_multiple = bgpio_set_multiple;
  361. }
  362. bgc->gc.get = bgpio_get;
  363. return 0;
  364. }
  365. static int bgpio_setup_direction(struct bgpio_chip *bgc,
  366. void __iomem *dirout,
  367. void __iomem *dirin)
  368. {
  369. if (dirout && dirin) {
  370. return -EINVAL;
  371. } else if (dirout) {
  372. bgc->reg_dir = dirout;
  373. bgc->gc.direction_output = bgpio_dir_out;
  374. bgc->gc.direction_input = bgpio_dir_in;
  375. } else if (dirin) {
  376. bgc->reg_dir = dirin;
  377. bgc->gc.direction_output = bgpio_dir_out_inv;
  378. bgc->gc.direction_input = bgpio_dir_in_inv;
  379. } else {
  380. bgc->gc.direction_output = bgpio_simple_dir_out;
  381. bgc->gc.direction_input = bgpio_simple_dir_in;
  382. }
  383. return 0;
  384. }
  385. static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
  386. {
  387. if (gpio_pin < chip->ngpio)
  388. return 0;
  389. return -EINVAL;
  390. }
  391. int bgpio_remove(struct bgpio_chip *bgc)
  392. {
  393. gpiochip_remove(&bgc->gc);
  394. return 0;
  395. }
  396. EXPORT_SYMBOL_GPL(bgpio_remove);
  397. int bgpio_init(struct bgpio_chip *bgc, struct device *dev,
  398. unsigned long sz, void __iomem *dat, void __iomem *set,
  399. void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
  400. unsigned long flags)
  401. {
  402. int ret;
  403. if (!is_power_of_2(sz))
  404. return -EINVAL;
  405. bgc->bits = sz * 8;
  406. if (bgc->bits > BITS_PER_LONG)
  407. return -EINVAL;
  408. spin_lock_init(&bgc->lock);
  409. bgc->gc.dev = dev;
  410. bgc->gc.label = dev_name(dev);
  411. bgc->gc.base = -1;
  412. bgc->gc.ngpio = bgc->bits;
  413. bgc->gc.request = bgpio_request;
  414. ret = bgpio_setup_io(bgc, dat, set, clr);
  415. if (ret)
  416. return ret;
  417. ret = bgpio_setup_accessors(dev, bgc, flags & BGPIOF_BIG_ENDIAN,
  418. flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  419. if (ret)
  420. return ret;
  421. ret = bgpio_setup_direction(bgc, dirout, dirin);
  422. if (ret)
  423. return ret;
  424. bgc->data = bgc->read_reg(bgc->reg_dat);
  425. if (bgc->gc.set == bgpio_set_set &&
  426. !(flags & BGPIOF_UNREADABLE_REG_SET))
  427. bgc->data = bgc->read_reg(bgc->reg_set);
  428. if (bgc->reg_dir && !(flags & BGPIOF_UNREADABLE_REG_DIR))
  429. bgc->dir = bgc->read_reg(bgc->reg_dir);
  430. return ret;
  431. }
  432. EXPORT_SYMBOL_GPL(bgpio_init);
  433. #ifdef CONFIG_GPIO_GENERIC_PLATFORM
  434. static void __iomem *bgpio_map(struct platform_device *pdev,
  435. const char *name,
  436. resource_size_t sane_sz,
  437. int *err)
  438. {
  439. struct device *dev = &pdev->dev;
  440. struct resource *r;
  441. resource_size_t start;
  442. resource_size_t sz;
  443. void __iomem *ret;
  444. *err = 0;
  445. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  446. if (!r)
  447. return NULL;
  448. sz = resource_size(r);
  449. if (sz != sane_sz) {
  450. *err = -EINVAL;
  451. return NULL;
  452. }
  453. start = r->start;
  454. if (!devm_request_mem_region(dev, start, sz, r->name)) {
  455. *err = -EBUSY;
  456. return NULL;
  457. }
  458. ret = devm_ioremap(dev, start, sz);
  459. if (!ret) {
  460. *err = -ENOMEM;
  461. return NULL;
  462. }
  463. return ret;
  464. }
  465. static int bgpio_pdev_probe(struct platform_device *pdev)
  466. {
  467. struct device *dev = &pdev->dev;
  468. struct resource *r;
  469. void __iomem *dat;
  470. void __iomem *set;
  471. void __iomem *clr;
  472. void __iomem *dirout;
  473. void __iomem *dirin;
  474. unsigned long sz;
  475. unsigned long flags = pdev->id_entry->driver_data;
  476. int err;
  477. struct bgpio_chip *bgc;
  478. struct bgpio_pdata *pdata = dev_get_platdata(dev);
  479. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
  480. if (!r)
  481. return -EINVAL;
  482. sz = resource_size(r);
  483. dat = bgpio_map(pdev, "dat", sz, &err);
  484. if (!dat)
  485. return err ? err : -EINVAL;
  486. set = bgpio_map(pdev, "set", sz, &err);
  487. if (err)
  488. return err;
  489. clr = bgpio_map(pdev, "clr", sz, &err);
  490. if (err)
  491. return err;
  492. dirout = bgpio_map(pdev, "dirout", sz, &err);
  493. if (err)
  494. return err;
  495. dirin = bgpio_map(pdev, "dirin", sz, &err);
  496. if (err)
  497. return err;
  498. bgc = devm_kzalloc(&pdev->dev, sizeof(*bgc), GFP_KERNEL);
  499. if (!bgc)
  500. return -ENOMEM;
  501. err = bgpio_init(bgc, dev, sz, dat, set, clr, dirout, dirin, flags);
  502. if (err)
  503. return err;
  504. if (pdata) {
  505. if (pdata->label)
  506. bgc->gc.label = pdata->label;
  507. bgc->gc.base = pdata->base;
  508. if (pdata->ngpio > 0)
  509. bgc->gc.ngpio = pdata->ngpio;
  510. }
  511. platform_set_drvdata(pdev, bgc);
  512. return gpiochip_add(&bgc->gc);
  513. }
  514. static int bgpio_pdev_remove(struct platform_device *pdev)
  515. {
  516. struct bgpio_chip *bgc = platform_get_drvdata(pdev);
  517. return bgpio_remove(bgc);
  518. }
  519. static const struct platform_device_id bgpio_id_table[] = {
  520. {
  521. .name = "basic-mmio-gpio",
  522. .driver_data = 0,
  523. }, {
  524. .name = "basic-mmio-gpio-be",
  525. .driver_data = BGPIOF_BIG_ENDIAN,
  526. },
  527. { }
  528. };
  529. MODULE_DEVICE_TABLE(platform, bgpio_id_table);
  530. static struct platform_driver bgpio_driver = {
  531. .driver = {
  532. .name = "basic-mmio-gpio",
  533. },
  534. .id_table = bgpio_id_table,
  535. .probe = bgpio_pdev_probe,
  536. .remove = bgpio_pdev_remove,
  537. };
  538. module_platform_driver(bgpio_driver);
  539. #endif /* CONFIG_GPIO_GENERIC_PLATFORM */
  540. MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
  541. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  542. MODULE_LICENSE("GPL");