gpio-mmio.c 19 KB

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