gpio-mmio.c 19 KB

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