gpio-generic.c 16 KB

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