gpio-generic.c 17 KB

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