gpio-mmio.c 21 KB

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