gpio-mmio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. #include <linux/of.h>
  64. #include <linux/of_device.h>
  65. static void bgpio_write8(void __iomem *reg, unsigned long data)
  66. {
  67. writeb(data, reg);
  68. }
  69. static unsigned long bgpio_read8(void __iomem *reg)
  70. {
  71. return readb(reg);
  72. }
  73. static void bgpio_write16(void __iomem *reg, unsigned long data)
  74. {
  75. writew(data, reg);
  76. }
  77. static unsigned long bgpio_read16(void __iomem *reg)
  78. {
  79. return readw(reg);
  80. }
  81. static void bgpio_write32(void __iomem *reg, unsigned long data)
  82. {
  83. writel(data, reg);
  84. }
  85. static unsigned long bgpio_read32(void __iomem *reg)
  86. {
  87. return readl(reg);
  88. }
  89. #if BITS_PER_LONG >= 64
  90. static void bgpio_write64(void __iomem *reg, unsigned long data)
  91. {
  92. writeq(data, reg);
  93. }
  94. static unsigned long bgpio_read64(void __iomem *reg)
  95. {
  96. return readq(reg);
  97. }
  98. #endif /* BITS_PER_LONG >= 64 */
  99. static void bgpio_write16be(void __iomem *reg, unsigned long data)
  100. {
  101. iowrite16be(data, reg);
  102. }
  103. static unsigned long bgpio_read16be(void __iomem *reg)
  104. {
  105. return ioread16be(reg);
  106. }
  107. static void bgpio_write32be(void __iomem *reg, unsigned long data)
  108. {
  109. iowrite32be(data, reg);
  110. }
  111. static unsigned long bgpio_read32be(void __iomem *reg)
  112. {
  113. return ioread32be(reg);
  114. }
  115. static unsigned long bgpio_line2mask(struct gpio_chip *gc, unsigned int line)
  116. {
  117. if (gc->be_bits)
  118. return BIT(gc->bgpio_bits - 1 - line);
  119. return BIT(line);
  120. }
  121. static int bgpio_get_set(struct gpio_chip *gc, unsigned int gpio)
  122. {
  123. unsigned long pinmask = bgpio_line2mask(gc, gpio);
  124. bool dir = !!(gc->bgpio_dir & pinmask);
  125. /*
  126. * If the direction is OUT we read the value from the SET
  127. * register, and if the direction is IN we read the value
  128. * from the DAT register.
  129. *
  130. * If the direction bits are inverted, naturally this gets
  131. * inverted too.
  132. */
  133. if (gc->bgpio_dir_inverted)
  134. dir = !dir;
  135. if (dir)
  136. return !!(gc->read_reg(gc->reg_set) & pinmask);
  137. else
  138. return !!(gc->read_reg(gc->reg_dat) & pinmask);
  139. }
  140. /*
  141. * This assumes that the bits in the GPIO register are in native endianness.
  142. * We only assign the function pointer if we have that.
  143. */
  144. static int bgpio_get_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  145. unsigned long *bits)
  146. {
  147. unsigned long get_mask = 0;
  148. unsigned long set_mask = 0;
  149. /* Make sure we first clear any bits that are zero when we read the register */
  150. *bits &= ~*mask;
  151. /* Exploit the fact that we know which directions are set */
  152. if (gc->bgpio_dir_inverted) {
  153. set_mask = *mask & ~gc->bgpio_dir;
  154. get_mask = *mask & gc->bgpio_dir;
  155. } else {
  156. set_mask = *mask & gc->bgpio_dir;
  157. get_mask = *mask & ~gc->bgpio_dir;
  158. }
  159. if (set_mask)
  160. *bits |= gc->read_reg(gc->reg_set) & set_mask;
  161. if (get_mask)
  162. *bits |= gc->read_reg(gc->reg_dat) & get_mask;
  163. return 0;
  164. }
  165. static int bgpio_get(struct gpio_chip *gc, unsigned int gpio)
  166. {
  167. return !!(gc->read_reg(gc->reg_dat) & bgpio_line2mask(gc, gpio));
  168. }
  169. /*
  170. * This only works if the bits in the GPIO register are in native endianness.
  171. */
  172. static int bgpio_get_multiple(struct gpio_chip *gc, unsigned long *mask,
  173. unsigned long *bits)
  174. {
  175. /* Make sure we first clear any bits that are zero when we read the register */
  176. *bits &= ~*mask;
  177. *bits |= gc->read_reg(gc->reg_dat) & *mask;
  178. return 0;
  179. }
  180. /*
  181. * With big endian mirrored bit order it becomes more tedious.
  182. */
  183. static int bgpio_get_multiple_be(struct gpio_chip *gc, unsigned long *mask,
  184. unsigned long *bits)
  185. {
  186. unsigned long readmask = 0;
  187. unsigned long val;
  188. int bit;
  189. /* Make sure we first clear any bits that are zero when we read the register */
  190. *bits &= ~*mask;
  191. /* Create a mirrored mask */
  192. bit = -1;
  193. while ((bit = find_next_bit(mask, gc->ngpio, bit + 1)) < gc->ngpio)
  194. readmask |= bgpio_line2mask(gc, bit);
  195. /* Read the register */
  196. val = gc->read_reg(gc->reg_dat) & readmask;
  197. /*
  198. * Mirror the result into the "bits" result, this will give line 0
  199. * in bit 0 ... line 31 in bit 31 for a 32bit register.
  200. */
  201. bit = -1;
  202. while ((bit = find_next_bit(&val, gc->ngpio, bit + 1)) < gc->ngpio)
  203. *bits |= bgpio_line2mask(gc, bit);
  204. return 0;
  205. }
  206. static void bgpio_set_none(struct gpio_chip *gc, unsigned int gpio, int val)
  207. {
  208. }
  209. static void bgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  210. {
  211. unsigned long mask = bgpio_line2mask(gc, gpio);
  212. unsigned long flags;
  213. spin_lock_irqsave(&gc->bgpio_lock, flags);
  214. if (val)
  215. gc->bgpio_data |= mask;
  216. else
  217. gc->bgpio_data &= ~mask;
  218. gc->write_reg(gc->reg_dat, gc->bgpio_data);
  219. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  220. }
  221. static void bgpio_set_with_clear(struct gpio_chip *gc, unsigned int gpio,
  222. int val)
  223. {
  224. unsigned long mask = bgpio_line2mask(gc, gpio);
  225. if (val)
  226. gc->write_reg(gc->reg_set, mask);
  227. else
  228. gc->write_reg(gc->reg_clr, mask);
  229. }
  230. static void bgpio_set_set(struct gpio_chip *gc, unsigned int gpio, int val)
  231. {
  232. unsigned long mask = bgpio_line2mask(gc, gpio);
  233. unsigned long flags;
  234. spin_lock_irqsave(&gc->bgpio_lock, flags);
  235. if (val)
  236. gc->bgpio_data |= mask;
  237. else
  238. gc->bgpio_data &= ~mask;
  239. gc->write_reg(gc->reg_set, gc->bgpio_data);
  240. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  241. }
  242. static void bgpio_multiple_get_masks(struct gpio_chip *gc,
  243. unsigned long *mask, unsigned long *bits,
  244. unsigned long *set_mask,
  245. unsigned long *clear_mask)
  246. {
  247. int i;
  248. *set_mask = 0;
  249. *clear_mask = 0;
  250. for (i = 0; i < gc->bgpio_bits; i++) {
  251. if (*mask == 0)
  252. break;
  253. if (__test_and_clear_bit(i, mask)) {
  254. if (test_bit(i, bits))
  255. *set_mask |= bgpio_line2mask(gc, i);
  256. else
  257. *clear_mask |= bgpio_line2mask(gc, i);
  258. }
  259. }
  260. }
  261. static void bgpio_set_multiple_single_reg(struct gpio_chip *gc,
  262. unsigned long *mask,
  263. unsigned long *bits,
  264. void __iomem *reg)
  265. {
  266. unsigned long flags;
  267. unsigned long set_mask, clear_mask;
  268. spin_lock_irqsave(&gc->bgpio_lock, flags);
  269. bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
  270. gc->bgpio_data |= set_mask;
  271. gc->bgpio_data &= ~clear_mask;
  272. gc->write_reg(reg, gc->bgpio_data);
  273. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  274. }
  275. static void bgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  276. unsigned long *bits)
  277. {
  278. bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_dat);
  279. }
  280. static void bgpio_set_multiple_set(struct gpio_chip *gc, unsigned long *mask,
  281. unsigned long *bits)
  282. {
  283. bgpio_set_multiple_single_reg(gc, mask, bits, gc->reg_set);
  284. }
  285. static void bgpio_set_multiple_with_clear(struct gpio_chip *gc,
  286. unsigned long *mask,
  287. unsigned long *bits)
  288. {
  289. unsigned long set_mask, clear_mask;
  290. bgpio_multiple_get_masks(gc, mask, bits, &set_mask, &clear_mask);
  291. if (set_mask)
  292. gc->write_reg(gc->reg_set, set_mask);
  293. if (clear_mask)
  294. gc->write_reg(gc->reg_clr, clear_mask);
  295. }
  296. static int bgpio_simple_dir_in(struct gpio_chip *gc, unsigned int gpio)
  297. {
  298. return 0;
  299. }
  300. static int bgpio_dir_out_err(struct gpio_chip *gc, unsigned int gpio,
  301. int val)
  302. {
  303. return -EINVAL;
  304. }
  305. static int bgpio_simple_dir_out(struct gpio_chip *gc, unsigned int gpio,
  306. int val)
  307. {
  308. gc->set(gc, gpio, val);
  309. return 0;
  310. }
  311. static int bgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  312. {
  313. unsigned long flags;
  314. spin_lock_irqsave(&gc->bgpio_lock, flags);
  315. if (gc->bgpio_dir_inverted)
  316. gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
  317. else
  318. gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
  319. gc->write_reg(gc->reg_dir, gc->bgpio_dir);
  320. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  321. return 0;
  322. }
  323. static int bgpio_get_dir(struct gpio_chip *gc, unsigned int gpio)
  324. {
  325. /* Return 0 if output, 1 of input */
  326. if (gc->bgpio_dir_inverted)
  327. return !!(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
  328. else
  329. return !(gc->read_reg(gc->reg_dir) & bgpio_line2mask(gc, gpio));
  330. }
  331. static int bgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  332. {
  333. unsigned long flags;
  334. gc->set(gc, gpio, val);
  335. spin_lock_irqsave(&gc->bgpio_lock, flags);
  336. if (gc->bgpio_dir_inverted)
  337. gc->bgpio_dir &= ~bgpio_line2mask(gc, gpio);
  338. else
  339. gc->bgpio_dir |= bgpio_line2mask(gc, gpio);
  340. gc->write_reg(gc->reg_dir, gc->bgpio_dir);
  341. spin_unlock_irqrestore(&gc->bgpio_lock, flags);
  342. return 0;
  343. }
  344. static int bgpio_setup_accessors(struct device *dev,
  345. struct gpio_chip *gc,
  346. bool byte_be)
  347. {
  348. switch (gc->bgpio_bits) {
  349. case 8:
  350. gc->read_reg = bgpio_read8;
  351. gc->write_reg = bgpio_write8;
  352. break;
  353. case 16:
  354. if (byte_be) {
  355. gc->read_reg = bgpio_read16be;
  356. gc->write_reg = bgpio_write16be;
  357. } else {
  358. gc->read_reg = bgpio_read16;
  359. gc->write_reg = bgpio_write16;
  360. }
  361. break;
  362. case 32:
  363. if (byte_be) {
  364. gc->read_reg = bgpio_read32be;
  365. gc->write_reg = bgpio_write32be;
  366. } else {
  367. gc->read_reg = bgpio_read32;
  368. gc->write_reg = bgpio_write32;
  369. }
  370. break;
  371. #if BITS_PER_LONG >= 64
  372. case 64:
  373. if (byte_be) {
  374. dev_err(dev,
  375. "64 bit big endian byte order unsupported\n");
  376. return -EINVAL;
  377. } else {
  378. gc->read_reg = bgpio_read64;
  379. gc->write_reg = bgpio_write64;
  380. }
  381. break;
  382. #endif /* BITS_PER_LONG >= 64 */
  383. default:
  384. dev_err(dev, "unsupported data width %u bits\n", gc->bgpio_bits);
  385. return -EINVAL;
  386. }
  387. return 0;
  388. }
  389. /*
  390. * Create the device and allocate the resources. For setting GPIO's there are
  391. * three supported configurations:
  392. *
  393. * - single input/output register resource (named "dat").
  394. * - set/clear pair (named "set" and "clr").
  395. * - single output register resource and single input resource ("set" and
  396. * dat").
  397. *
  398. * For the single output register, this drives a 1 by setting a bit and a zero
  399. * by clearing a bit. For the set clr pair, this drives a 1 by setting a bit
  400. * in the set register and clears it by setting a bit in the clear register.
  401. * The configuration is detected by which resources are present.
  402. *
  403. * For setting the GPIO direction, there are three supported configurations:
  404. *
  405. * - simple bidirection GPIO that requires no configuration.
  406. * - an output direction register (named "dirout") where a 1 bit
  407. * indicates the GPIO is an output.
  408. * - an input direction register (named "dirin") where a 1 bit indicates
  409. * the GPIO is an input.
  410. */
  411. static int bgpio_setup_io(struct gpio_chip *gc,
  412. void __iomem *dat,
  413. void __iomem *set,
  414. void __iomem *clr,
  415. unsigned long flags)
  416. {
  417. gc->reg_dat = dat;
  418. if (!gc->reg_dat)
  419. return -EINVAL;
  420. if (set && clr) {
  421. gc->reg_set = set;
  422. gc->reg_clr = clr;
  423. gc->set = bgpio_set_with_clear;
  424. gc->set_multiple = bgpio_set_multiple_with_clear;
  425. } else if (set && !clr) {
  426. gc->reg_set = set;
  427. gc->set = bgpio_set_set;
  428. gc->set_multiple = bgpio_set_multiple_set;
  429. } else if (flags & BGPIOF_NO_OUTPUT) {
  430. gc->set = bgpio_set_none;
  431. gc->set_multiple = NULL;
  432. } else {
  433. gc->set = bgpio_set;
  434. gc->set_multiple = bgpio_set_multiple;
  435. }
  436. if (!(flags & BGPIOF_UNREADABLE_REG_SET) &&
  437. (flags & BGPIOF_READ_OUTPUT_REG_SET)) {
  438. gc->get = bgpio_get_set;
  439. if (!gc->be_bits)
  440. gc->get_multiple = bgpio_get_set_multiple;
  441. /*
  442. * We deliberately avoid assigning the ->get_multiple() call
  443. * for big endian mirrored registers which are ALSO reflecting
  444. * their value in the set register when used as output. It is
  445. * simply too much complexity, let the GPIO core fall back to
  446. * reading each line individually in that fringe case.
  447. */
  448. } else {
  449. gc->get = bgpio_get;
  450. if (gc->be_bits)
  451. gc->get_multiple = bgpio_get_multiple_be;
  452. else
  453. gc->get_multiple = bgpio_get_multiple;
  454. }
  455. return 0;
  456. }
  457. static int bgpio_setup_direction(struct gpio_chip *gc,
  458. void __iomem *dirout,
  459. void __iomem *dirin,
  460. unsigned long flags)
  461. {
  462. if (dirout && dirin) {
  463. return -EINVAL;
  464. } else if (dirout) {
  465. gc->reg_dir = dirout;
  466. gc->direction_output = bgpio_dir_out;
  467. gc->direction_input = bgpio_dir_in;
  468. gc->get_direction = bgpio_get_dir;
  469. } else if (dirin) {
  470. gc->reg_dir = dirin;
  471. gc->direction_output = bgpio_dir_out;
  472. gc->direction_input = bgpio_dir_in;
  473. gc->get_direction = bgpio_get_dir;
  474. gc->bgpio_dir_inverted = true;
  475. } else {
  476. if (flags & BGPIOF_NO_OUTPUT)
  477. gc->direction_output = bgpio_dir_out_err;
  478. else
  479. gc->direction_output = bgpio_simple_dir_out;
  480. gc->direction_input = bgpio_simple_dir_in;
  481. }
  482. return 0;
  483. }
  484. static int bgpio_request(struct gpio_chip *chip, unsigned gpio_pin)
  485. {
  486. if (gpio_pin < chip->ngpio)
  487. return 0;
  488. return -EINVAL;
  489. }
  490. /**
  491. * bgpio_init() - Initialize generic GPIO accessor functions
  492. * @gc: the GPIO chip to set up
  493. * @dev: the parent device of the new GPIO chip (compulsory)
  494. * @sz: the size (width) of the MMIO registers in bytes, typically 1, 2 or 4
  495. * @dat: MMIO address for the register to READ the value of the GPIO lines, it
  496. * is expected that a 1 in the corresponding bit in this register means the
  497. * line is asserted
  498. * @set: MMIO address for the register to SET the value of the GPIO lines, it is
  499. * expected that we write the line with 1 in this register to drive the GPIO line
  500. * high.
  501. * @clr: MMIO address for the register to CLEAR the value of the GPIO lines, it is
  502. * expected that we write the line with 1 in this register to drive the GPIO line
  503. * low. It is allowed to leave this address as NULL, in that case the SET register
  504. * will be assumed to also clear the GPIO lines, by actively writing the line
  505. * with 0.
  506. * @dirout: MMIO address for the register to set the line as OUTPUT. It is assumed
  507. * that setting a line to 1 in this register will turn that line into an
  508. * output line. Conversely, setting the line to 0 will turn that line into
  509. * an input. Either this or @dirin can be defined, but never both.
  510. * @dirin: MMIO address for the register to set this line as INPUT. It is assumed
  511. * that setting a line to 1 in this register will turn that line into an
  512. * input line. Conversely, setting the line to 0 will turn that line into
  513. * an output. Either this or @dirout can be defined, but never both.
  514. * @flags: Different flags that will affect the behaviour of the device, such as
  515. * endianness etc.
  516. */
  517. int bgpio_init(struct gpio_chip *gc, struct device *dev,
  518. unsigned long sz, void __iomem *dat, void __iomem *set,
  519. void __iomem *clr, void __iomem *dirout, void __iomem *dirin,
  520. unsigned long flags)
  521. {
  522. int ret;
  523. if (!is_power_of_2(sz))
  524. return -EINVAL;
  525. gc->bgpio_bits = sz * 8;
  526. if (gc->bgpio_bits > BITS_PER_LONG)
  527. return -EINVAL;
  528. spin_lock_init(&gc->bgpio_lock);
  529. gc->parent = dev;
  530. gc->label = dev_name(dev);
  531. gc->base = -1;
  532. gc->ngpio = gc->bgpio_bits;
  533. gc->request = bgpio_request;
  534. gc->be_bits = !!(flags & BGPIOF_BIG_ENDIAN);
  535. ret = bgpio_setup_io(gc, dat, set, clr, flags);
  536. if (ret)
  537. return ret;
  538. ret = bgpio_setup_accessors(dev, gc, flags & BGPIOF_BIG_ENDIAN_BYTE_ORDER);
  539. if (ret)
  540. return ret;
  541. ret = bgpio_setup_direction(gc, dirout, dirin, flags);
  542. if (ret)
  543. return ret;
  544. gc->bgpio_data = gc->read_reg(gc->reg_dat);
  545. if (gc->set == bgpio_set_set &&
  546. !(flags & BGPIOF_UNREADABLE_REG_SET))
  547. gc->bgpio_data = gc->read_reg(gc->reg_set);
  548. if (gc->reg_dir && !(flags & BGPIOF_UNREADABLE_REG_DIR))
  549. gc->bgpio_dir = gc->read_reg(gc->reg_dir);
  550. return ret;
  551. }
  552. EXPORT_SYMBOL_GPL(bgpio_init);
  553. #if IS_ENABLED(CONFIG_GPIO_GENERIC_PLATFORM)
  554. static void __iomem *bgpio_map(struct platform_device *pdev,
  555. const char *name,
  556. resource_size_t sane_sz)
  557. {
  558. struct resource *r;
  559. resource_size_t sz;
  560. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  561. if (!r)
  562. return NULL;
  563. sz = resource_size(r);
  564. if (sz != sane_sz)
  565. return IOMEM_ERR_PTR(-EINVAL);
  566. return devm_ioremap_resource(&pdev->dev, r);
  567. }
  568. #ifdef CONFIG_OF
  569. static const struct of_device_id bgpio_of_match[] = {
  570. { .compatible = "brcm,bcm6345-gpio" },
  571. { .compatible = "wd,mbl-gpio" },
  572. { .compatible = "ni,169445-nand-gpio" },
  573. { }
  574. };
  575. MODULE_DEVICE_TABLE(of, bgpio_of_match);
  576. static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev,
  577. unsigned long *flags)
  578. {
  579. struct bgpio_pdata *pdata;
  580. if (!of_match_device(bgpio_of_match, &pdev->dev))
  581. return NULL;
  582. pdata = devm_kzalloc(&pdev->dev, sizeof(struct bgpio_pdata),
  583. GFP_KERNEL);
  584. if (!pdata)
  585. return ERR_PTR(-ENOMEM);
  586. pdata->base = -1;
  587. if (of_device_is_big_endian(pdev->dev.of_node))
  588. *flags |= BGPIOF_BIG_ENDIAN_BYTE_ORDER;
  589. if (of_property_read_bool(pdev->dev.of_node, "no-output"))
  590. *flags |= BGPIOF_NO_OUTPUT;
  591. return pdata;
  592. }
  593. #else
  594. static struct bgpio_pdata *bgpio_parse_dt(struct platform_device *pdev,
  595. unsigned long *flags)
  596. {
  597. return NULL;
  598. }
  599. #endif /* CONFIG_OF */
  600. static int bgpio_pdev_probe(struct platform_device *pdev)
  601. {
  602. struct device *dev = &pdev->dev;
  603. struct resource *r;
  604. void __iomem *dat;
  605. void __iomem *set;
  606. void __iomem *clr;
  607. void __iomem *dirout;
  608. void __iomem *dirin;
  609. unsigned long sz;
  610. unsigned long flags = 0;
  611. int err;
  612. struct gpio_chip *gc;
  613. struct bgpio_pdata *pdata;
  614. pdata = bgpio_parse_dt(pdev, &flags);
  615. if (IS_ERR(pdata))
  616. return PTR_ERR(pdata);
  617. if (!pdata) {
  618. pdata = dev_get_platdata(dev);
  619. flags = pdev->id_entry->driver_data;
  620. }
  621. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dat");
  622. if (!r)
  623. return -EINVAL;
  624. sz = resource_size(r);
  625. dat = bgpio_map(pdev, "dat", sz);
  626. if (IS_ERR(dat))
  627. return PTR_ERR(dat);
  628. set = bgpio_map(pdev, "set", sz);
  629. if (IS_ERR(set))
  630. return PTR_ERR(set);
  631. clr = bgpio_map(pdev, "clr", sz);
  632. if (IS_ERR(clr))
  633. return PTR_ERR(clr);
  634. dirout = bgpio_map(pdev, "dirout", sz);
  635. if (IS_ERR(dirout))
  636. return PTR_ERR(dirout);
  637. dirin = bgpio_map(pdev, "dirin", sz);
  638. if (IS_ERR(dirin))
  639. return PTR_ERR(dirin);
  640. gc = devm_kzalloc(&pdev->dev, sizeof(*gc), GFP_KERNEL);
  641. if (!gc)
  642. return -ENOMEM;
  643. err = bgpio_init(gc, dev, sz, dat, set, clr, dirout, dirin, flags);
  644. if (err)
  645. return err;
  646. if (pdata) {
  647. if (pdata->label)
  648. gc->label = pdata->label;
  649. gc->base = pdata->base;
  650. if (pdata->ngpio > 0)
  651. gc->ngpio = pdata->ngpio;
  652. }
  653. platform_set_drvdata(pdev, gc);
  654. return devm_gpiochip_add_data(&pdev->dev, gc, NULL);
  655. }
  656. static const struct platform_device_id bgpio_id_table[] = {
  657. {
  658. .name = "basic-mmio-gpio",
  659. .driver_data = 0,
  660. }, {
  661. .name = "basic-mmio-gpio-be",
  662. .driver_data = BGPIOF_BIG_ENDIAN,
  663. },
  664. { }
  665. };
  666. MODULE_DEVICE_TABLE(platform, bgpio_id_table);
  667. static struct platform_driver bgpio_driver = {
  668. .driver = {
  669. .name = "basic-mmio-gpio",
  670. .of_match_table = of_match_ptr(bgpio_of_match),
  671. },
  672. .id_table = bgpio_id_table,
  673. .probe = bgpio_pdev_probe,
  674. };
  675. module_platform_driver(bgpio_driver);
  676. #endif /* CONFIG_GPIO_GENERIC_PLATFORM */
  677. MODULE_DESCRIPTION("Driver for basic memory-mapped GPIO controllers");
  678. MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
  679. MODULE_LICENSE("GPL");