gpio-xilinx.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Xilinx gpio driver for xps/axi_gpio IP.
  4. *
  5. * Copyright 2008 - 2013 Xilinx, Inc.
  6. */
  7. #include <linux/bitmap.h>
  8. #include <linux/bitops.h>
  9. #include <linux/clk.h>
  10. #include <linux/errno.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/slab.h>
  21. /* Register Offset Definitions */
  22. #define XGPIO_DATA_OFFSET (0x0) /* Data register */
  23. #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
  24. #define XGPIO_CHANNEL0_OFFSET 0x0
  25. #define XGPIO_CHANNEL1_OFFSET 0x8
  26. #define XGPIO_GIER_OFFSET 0x11c /* Global Interrupt Enable */
  27. #define XGPIO_GIER_IE BIT(31)
  28. #define XGPIO_IPISR_OFFSET 0x120 /* IP Interrupt Status */
  29. #define XGPIO_IPIER_OFFSET 0x128 /* IP Interrupt Enable */
  30. /* Read/Write access to the GPIO registers */
  31. #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
  32. # define xgpio_readreg(offset) readl(offset)
  33. # define xgpio_writereg(offset, val) writel(val, offset)
  34. #else
  35. # define xgpio_readreg(offset) __raw_readl(offset)
  36. # define xgpio_writereg(offset, val) __raw_writel(val, offset)
  37. #endif
  38. /**
  39. * struct xgpio_instance - Stores information about GPIO device
  40. * @gc: GPIO chip
  41. * @regs: register block
  42. * @hw_map: GPIO pin mapping on hardware side
  43. * @sw_map: GPIO pin mapping on software side
  44. * @state: GPIO write state shadow register
  45. * @last_irq_read: GPIO read state register from last interrupt
  46. * @dir: GPIO direction shadow register
  47. * @gpio_lock: Lock used for synchronization
  48. * @irq: IRQ used by GPIO device
  49. * @irqchip: IRQ chip
  50. * @enable: GPIO IRQ enable/disable bitfield
  51. * @rising_edge: GPIO IRQ rising edge enable/disable bitfield
  52. * @falling_edge: GPIO IRQ falling edge enable/disable bitfield
  53. * @clk: clock resource for this driver
  54. */
  55. struct xgpio_instance {
  56. struct gpio_chip gc;
  57. void __iomem *regs;
  58. DECLARE_BITMAP(hw_map, 64);
  59. DECLARE_BITMAP(sw_map, 64);
  60. DECLARE_BITMAP(state, 64);
  61. DECLARE_BITMAP(last_irq_read, 64);
  62. DECLARE_BITMAP(dir, 64);
  63. spinlock_t gpio_lock; /* For serializing operations */
  64. int irq;
  65. struct irq_chip irqchip;
  66. DECLARE_BITMAP(enable, 64);
  67. DECLARE_BITMAP(rising_edge, 64);
  68. DECLARE_BITMAP(falling_edge, 64);
  69. struct clk *clk;
  70. };
  71. static inline int xgpio_from_bit(struct xgpio_instance *chip, int bit)
  72. {
  73. return bitmap_bitremap(bit, chip->hw_map, chip->sw_map, 64);
  74. }
  75. static inline int xgpio_to_bit(struct xgpio_instance *chip, int gpio)
  76. {
  77. return bitmap_bitremap(gpio, chip->sw_map, chip->hw_map, 64);
  78. }
  79. static inline u32 xgpio_get_value32(const unsigned long *map, int bit)
  80. {
  81. const size_t index = BIT_WORD(bit);
  82. const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5);
  83. return (map[index] >> offset) & 0xFFFFFFFFul;
  84. }
  85. static inline void xgpio_set_value32(unsigned long *map, int bit, u32 v)
  86. {
  87. const size_t index = BIT_WORD(bit);
  88. const unsigned long offset = (bit % BITS_PER_LONG) & BIT(5);
  89. map[index] &= ~(0xFFFFFFFFul << offset);
  90. map[index] |= (unsigned long)v << offset;
  91. }
  92. static inline int xgpio_regoffset(struct xgpio_instance *chip, int ch)
  93. {
  94. switch (ch) {
  95. case 0:
  96. return XGPIO_CHANNEL0_OFFSET;
  97. case 1:
  98. return XGPIO_CHANNEL1_OFFSET;
  99. default:
  100. return -EINVAL;
  101. }
  102. }
  103. static void xgpio_read_ch(struct xgpio_instance *chip, int reg, int bit, unsigned long *a)
  104. {
  105. void __iomem *addr = chip->regs + reg + xgpio_regoffset(chip, bit / 32);
  106. xgpio_set_value32(a, bit, xgpio_readreg(addr));
  107. }
  108. static void xgpio_write_ch(struct xgpio_instance *chip, int reg, int bit, unsigned long *a)
  109. {
  110. void __iomem *addr = chip->regs + reg + xgpio_regoffset(chip, bit / 32);
  111. xgpio_writereg(addr, xgpio_get_value32(a, bit));
  112. }
  113. static void xgpio_read_ch_all(struct xgpio_instance *chip, int reg, unsigned long *a)
  114. {
  115. int bit, lastbit = xgpio_to_bit(chip, chip->gc.ngpio - 1);
  116. for (bit = 0; bit <= lastbit ; bit += 32)
  117. xgpio_read_ch(chip, reg, bit, a);
  118. }
  119. static void xgpio_write_ch_all(struct xgpio_instance *chip, int reg, unsigned long *a)
  120. {
  121. int bit, lastbit = xgpio_to_bit(chip, chip->gc.ngpio - 1);
  122. for (bit = 0; bit <= lastbit ; bit += 32)
  123. xgpio_write_ch(chip, reg, bit, a);
  124. }
  125. /**
  126. * xgpio_get - Read the specified signal of the GPIO device.
  127. * @gc: Pointer to gpio_chip device structure.
  128. * @gpio: GPIO signal number.
  129. *
  130. * This function reads the specified signal of the GPIO device.
  131. *
  132. * Return:
  133. * 0 if direction of GPIO signals is set as input otherwise it
  134. * returns negative error value.
  135. */
  136. static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
  137. {
  138. struct xgpio_instance *chip = gpiochip_get_data(gc);
  139. int bit = xgpio_to_bit(chip, gpio);
  140. DECLARE_BITMAP(state, 64);
  141. xgpio_read_ch(chip, XGPIO_DATA_OFFSET, bit, state);
  142. return test_bit(bit, state);
  143. }
  144. /**
  145. * xgpio_set - Write the specified signal of the GPIO device.
  146. * @gc: Pointer to gpio_chip device structure.
  147. * @gpio: GPIO signal number.
  148. * @val: Value to be written to specified signal.
  149. *
  150. * This function writes the specified value in to the specified signal of the
  151. * GPIO device.
  152. */
  153. static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  154. {
  155. unsigned long flags;
  156. struct xgpio_instance *chip = gpiochip_get_data(gc);
  157. int bit = xgpio_to_bit(chip, gpio);
  158. spin_lock_irqsave(&chip->gpio_lock, flags);
  159. /* Write to GPIO signal and set its direction to output */
  160. __assign_bit(bit, chip->state, val);
  161. xgpio_write_ch(chip, XGPIO_DATA_OFFSET, bit, chip->state);
  162. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  163. }
  164. /**
  165. * xgpio_set_multiple - Write the specified signals of the GPIO device.
  166. * @gc: Pointer to gpio_chip device structure.
  167. * @mask: Mask of the GPIOS to modify.
  168. * @bits: Value to be wrote on each GPIO
  169. *
  170. * This function writes the specified values into the specified signals of the
  171. * GPIO devices.
  172. */
  173. static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  174. unsigned long *bits)
  175. {
  176. DECLARE_BITMAP(hw_mask, 64);
  177. DECLARE_BITMAP(hw_bits, 64);
  178. DECLARE_BITMAP(state, 64);
  179. unsigned long flags;
  180. struct xgpio_instance *chip = gpiochip_get_data(gc);
  181. bitmap_remap(hw_mask, mask, chip->sw_map, chip->hw_map, 64);
  182. bitmap_remap(hw_bits, bits, chip->sw_map, chip->hw_map, 64);
  183. spin_lock_irqsave(&chip->gpio_lock, flags);
  184. bitmap_replace(state, chip->state, hw_bits, hw_mask, 64);
  185. xgpio_write_ch_all(chip, XGPIO_DATA_OFFSET, state);
  186. bitmap_copy(chip->state, state, 64);
  187. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  188. }
  189. /**
  190. * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
  191. * @gc: Pointer to gpio_chip device structure.
  192. * @gpio: GPIO signal number.
  193. *
  194. * Return:
  195. * 0 - if direction of GPIO signals is set as input
  196. * otherwise it returns negative error value.
  197. */
  198. static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  199. {
  200. unsigned long flags;
  201. struct xgpio_instance *chip = gpiochip_get_data(gc);
  202. int bit = xgpio_to_bit(chip, gpio);
  203. spin_lock_irqsave(&chip->gpio_lock, flags);
  204. /* Set the GPIO bit in shadow register and set direction as input */
  205. __set_bit(bit, chip->dir);
  206. xgpio_write_ch(chip, XGPIO_TRI_OFFSET, bit, chip->dir);
  207. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  208. return 0;
  209. }
  210. /**
  211. * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
  212. * @gc: Pointer to gpio_chip device structure.
  213. * @gpio: GPIO signal number.
  214. * @val: Value to be written to specified signal.
  215. *
  216. * This function sets the direction of specified GPIO signal as output.
  217. *
  218. * Return:
  219. * If all GPIO signals of GPIO chip is configured as input then it returns
  220. * error otherwise it returns 0.
  221. */
  222. static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  223. {
  224. unsigned long flags;
  225. struct xgpio_instance *chip = gpiochip_get_data(gc);
  226. int bit = xgpio_to_bit(chip, gpio);
  227. spin_lock_irqsave(&chip->gpio_lock, flags);
  228. /* Write state of GPIO signal */
  229. __assign_bit(bit, chip->state, val);
  230. xgpio_write_ch(chip, XGPIO_DATA_OFFSET, bit, chip->state);
  231. /* Clear the GPIO bit in shadow register and set direction as output */
  232. __clear_bit(bit, chip->dir);
  233. xgpio_write_ch(chip, XGPIO_TRI_OFFSET, bit, chip->dir);
  234. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  235. return 0;
  236. }
  237. /**
  238. * xgpio_save_regs - Set initial values of GPIO pins
  239. * @chip: Pointer to GPIO instance
  240. */
  241. static void xgpio_save_regs(struct xgpio_instance *chip)
  242. {
  243. xgpio_write_ch_all(chip, XGPIO_DATA_OFFSET, chip->state);
  244. xgpio_write_ch_all(chip, XGPIO_TRI_OFFSET, chip->dir);
  245. }
  246. static int xgpio_request(struct gpio_chip *chip, unsigned int offset)
  247. {
  248. int ret;
  249. ret = pm_runtime_get_sync(chip->parent);
  250. /*
  251. * If the device is already active pm_runtime_get() will return 1 on
  252. * success, but gpio_request still needs to return 0.
  253. */
  254. return ret < 0 ? ret : 0;
  255. }
  256. static void xgpio_free(struct gpio_chip *chip, unsigned int offset)
  257. {
  258. pm_runtime_put(chip->parent);
  259. }
  260. static int __maybe_unused xgpio_suspend(struct device *dev)
  261. {
  262. struct xgpio_instance *gpio = dev_get_drvdata(dev);
  263. struct irq_data *data = irq_get_irq_data(gpio->irq);
  264. if (!data) {
  265. dev_dbg(dev, "IRQ not connected\n");
  266. return pm_runtime_force_suspend(dev);
  267. }
  268. if (!irqd_is_wakeup_set(data))
  269. return pm_runtime_force_suspend(dev);
  270. return 0;
  271. }
  272. /**
  273. * xgpio_remove - Remove method for the GPIO device.
  274. * @pdev: pointer to the platform device
  275. *
  276. * This function remove gpiochips and frees all the allocated resources.
  277. *
  278. * Return: 0 always
  279. */
  280. static int xgpio_remove(struct platform_device *pdev)
  281. {
  282. struct xgpio_instance *gpio = platform_get_drvdata(pdev);
  283. pm_runtime_get_sync(&pdev->dev);
  284. pm_runtime_put_noidle(&pdev->dev);
  285. pm_runtime_disable(&pdev->dev);
  286. clk_disable_unprepare(gpio->clk);
  287. return 0;
  288. }
  289. /**
  290. * xgpio_irq_ack - Acknowledge a child GPIO interrupt.
  291. * @irq_data: per IRQ and chip data passed down to chip functions
  292. * This currently does nothing, but irq_ack is unconditionally called by
  293. * handle_edge_irq and therefore must be defined.
  294. */
  295. static void xgpio_irq_ack(struct irq_data *irq_data)
  296. {
  297. }
  298. static int __maybe_unused xgpio_resume(struct device *dev)
  299. {
  300. struct xgpio_instance *gpio = dev_get_drvdata(dev);
  301. struct irq_data *data = irq_get_irq_data(gpio->irq);
  302. if (!data) {
  303. dev_dbg(dev, "IRQ not connected\n");
  304. return pm_runtime_force_resume(dev);
  305. }
  306. if (!irqd_is_wakeup_set(data))
  307. return pm_runtime_force_resume(dev);
  308. return 0;
  309. }
  310. static int __maybe_unused xgpio_runtime_suspend(struct device *dev)
  311. {
  312. struct xgpio_instance *gpio = dev_get_drvdata(dev);
  313. clk_disable(gpio->clk);
  314. return 0;
  315. }
  316. static int __maybe_unused xgpio_runtime_resume(struct device *dev)
  317. {
  318. struct xgpio_instance *gpio = dev_get_drvdata(dev);
  319. return clk_enable(gpio->clk);
  320. }
  321. static const struct dev_pm_ops xgpio_dev_pm_ops = {
  322. SET_SYSTEM_SLEEP_PM_OPS(xgpio_suspend, xgpio_resume)
  323. SET_RUNTIME_PM_OPS(xgpio_runtime_suspend,
  324. xgpio_runtime_resume, NULL)
  325. };
  326. /**
  327. * xgpio_irq_mask - Write the specified signal of the GPIO device.
  328. * @irq_data: per IRQ and chip data passed down to chip functions
  329. */
  330. static void xgpio_irq_mask(struct irq_data *irq_data)
  331. {
  332. unsigned long flags;
  333. struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
  334. int irq_offset = irqd_to_hwirq(irq_data);
  335. int bit = xgpio_to_bit(chip, irq_offset);
  336. u32 mask = BIT(bit / 32), temp;
  337. spin_lock_irqsave(&chip->gpio_lock, flags);
  338. __clear_bit(bit, chip->enable);
  339. if (xgpio_get_value32(chip->enable, bit) == 0) {
  340. /* Disable per channel interrupt */
  341. temp = xgpio_readreg(chip->regs + XGPIO_IPIER_OFFSET);
  342. temp &= ~mask;
  343. xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, temp);
  344. }
  345. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  346. }
  347. /**
  348. * xgpio_irq_unmask - Write the specified signal of the GPIO device.
  349. * @irq_data: per IRQ and chip data passed down to chip functions
  350. */
  351. static void xgpio_irq_unmask(struct irq_data *irq_data)
  352. {
  353. unsigned long flags;
  354. struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
  355. int irq_offset = irqd_to_hwirq(irq_data);
  356. int bit = xgpio_to_bit(chip, irq_offset);
  357. u32 old_enable = xgpio_get_value32(chip->enable, bit);
  358. u32 mask = BIT(bit / 32), val;
  359. spin_lock_irqsave(&chip->gpio_lock, flags);
  360. __set_bit(bit, chip->enable);
  361. if (old_enable == 0) {
  362. /* Clear any existing per-channel interrupts */
  363. val = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
  364. val &= mask;
  365. xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, val);
  366. /* Update GPIO IRQ read data before enabling interrupt*/
  367. xgpio_read_ch(chip, XGPIO_DATA_OFFSET, bit, chip->last_irq_read);
  368. /* Enable per channel interrupt */
  369. val = xgpio_readreg(chip->regs + XGPIO_IPIER_OFFSET);
  370. val |= mask;
  371. xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, val);
  372. }
  373. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  374. }
  375. /**
  376. * xgpio_set_irq_type - Write the specified signal of the GPIO device.
  377. * @irq_data: Per IRQ and chip data passed down to chip functions
  378. * @type: Interrupt type that is to be set for the gpio pin
  379. *
  380. * Return:
  381. * 0 if interrupt type is supported otherwise -EINVAL
  382. */
  383. static int xgpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
  384. {
  385. struct xgpio_instance *chip = irq_data_get_irq_chip_data(irq_data);
  386. int irq_offset = irqd_to_hwirq(irq_data);
  387. int bit = xgpio_to_bit(chip, irq_offset);
  388. /*
  389. * The Xilinx GPIO hardware provides a single interrupt status
  390. * indication for any state change in a given GPIO channel (bank).
  391. * Therefore, only rising edge or falling edge triggers are
  392. * supported.
  393. */
  394. switch (type & IRQ_TYPE_SENSE_MASK) {
  395. case IRQ_TYPE_EDGE_BOTH:
  396. __set_bit(bit, chip->rising_edge);
  397. __set_bit(bit, chip->falling_edge);
  398. break;
  399. case IRQ_TYPE_EDGE_RISING:
  400. __set_bit(bit, chip->rising_edge);
  401. __clear_bit(bit, chip->falling_edge);
  402. break;
  403. case IRQ_TYPE_EDGE_FALLING:
  404. __clear_bit(bit, chip->rising_edge);
  405. __set_bit(bit, chip->falling_edge);
  406. break;
  407. default:
  408. return -EINVAL;
  409. }
  410. irq_set_handler_locked(irq_data, handle_edge_irq);
  411. return 0;
  412. }
  413. /**
  414. * xgpio_irqhandler - Gpio interrupt service routine
  415. * @desc: Pointer to interrupt description
  416. */
  417. static void xgpio_irqhandler(struct irq_desc *desc)
  418. {
  419. struct xgpio_instance *chip = irq_desc_get_handler_data(desc);
  420. struct gpio_chip *gc = &chip->gc;
  421. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  422. DECLARE_BITMAP(rising, 64);
  423. DECLARE_BITMAP(falling, 64);
  424. DECLARE_BITMAP(all, 64);
  425. int irq_offset;
  426. u32 status;
  427. u32 bit;
  428. status = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
  429. xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, status);
  430. chained_irq_enter(irqchip, desc);
  431. spin_lock(&chip->gpio_lock);
  432. xgpio_read_ch_all(chip, XGPIO_DATA_OFFSET, all);
  433. bitmap_complement(rising, chip->last_irq_read, 64);
  434. bitmap_and(rising, rising, all, 64);
  435. bitmap_and(rising, rising, chip->enable, 64);
  436. bitmap_and(rising, rising, chip->rising_edge, 64);
  437. bitmap_complement(falling, all, 64);
  438. bitmap_and(falling, falling, chip->last_irq_read, 64);
  439. bitmap_and(falling, falling, chip->enable, 64);
  440. bitmap_and(falling, falling, chip->falling_edge, 64);
  441. bitmap_copy(chip->last_irq_read, all, 64);
  442. bitmap_or(all, rising, falling, 64);
  443. spin_unlock(&chip->gpio_lock);
  444. dev_dbg(gc->parent, "IRQ rising %*pb falling %*pb\n", 64, rising, 64, falling);
  445. for_each_set_bit(bit, all, 64) {
  446. irq_offset = xgpio_from_bit(chip, bit);
  447. generic_handle_domain_irq(gc->irq.domain, irq_offset);
  448. }
  449. chained_irq_exit(irqchip, desc);
  450. }
  451. /**
  452. * xgpio_probe - Probe method for the GPIO device.
  453. * @pdev: pointer to the platform device
  454. *
  455. * Return:
  456. * It returns 0, if the driver is bound to the GPIO device, or
  457. * a negative value if there is an error.
  458. */
  459. static int xgpio_probe(struct platform_device *pdev)
  460. {
  461. struct xgpio_instance *chip;
  462. int status = 0;
  463. struct device_node *np = pdev->dev.of_node;
  464. u32 is_dual = 0;
  465. u32 cells = 2;
  466. u32 width[2];
  467. u32 state[2];
  468. u32 dir[2];
  469. struct gpio_irq_chip *girq;
  470. u32 temp;
  471. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  472. if (!chip)
  473. return -ENOMEM;
  474. platform_set_drvdata(pdev, chip);
  475. /* First, check if the device is dual-channel */
  476. of_property_read_u32(np, "xlnx,is-dual", &is_dual);
  477. /* Setup defaults */
  478. memset32(width, 0, ARRAY_SIZE(width));
  479. memset32(state, 0, ARRAY_SIZE(state));
  480. memset32(dir, 0xFFFFFFFF, ARRAY_SIZE(dir));
  481. /* Update GPIO state shadow register with default value */
  482. of_property_read_u32(np, "xlnx,dout-default", &state[0]);
  483. of_property_read_u32(np, "xlnx,dout-default-2", &state[1]);
  484. bitmap_from_arr32(chip->state, state, 64);
  485. /* Update GPIO direction shadow register with default value */
  486. of_property_read_u32(np, "xlnx,tri-default", &dir[0]);
  487. of_property_read_u32(np, "xlnx,tri-default-2", &dir[1]);
  488. bitmap_from_arr32(chip->dir, dir, 64);
  489. /* Update cells with gpio-cells value */
  490. if (of_property_read_u32(np, "#gpio-cells", &cells))
  491. dev_dbg(&pdev->dev, "Missing gpio-cells property\n");
  492. if (cells != 2) {
  493. dev_err(&pdev->dev, "#gpio-cells mismatch\n");
  494. return -EINVAL;
  495. }
  496. /*
  497. * Check device node and parent device node for device width
  498. * and assume default width of 32
  499. */
  500. if (of_property_read_u32(np, "xlnx,gpio-width", &width[0]))
  501. width[0] = 32;
  502. if (width[0] > 32)
  503. return -EINVAL;
  504. if (is_dual && of_property_read_u32(np, "xlnx,gpio2-width", &width[1]))
  505. width[1] = 32;
  506. if (width[1] > 32)
  507. return -EINVAL;
  508. /* Setup software pin mapping */
  509. bitmap_set(chip->sw_map, 0, width[0] + width[1]);
  510. /* Setup hardware pin mapping */
  511. bitmap_set(chip->hw_map, 0, width[0]);
  512. bitmap_set(chip->hw_map, 32, width[1]);
  513. spin_lock_init(&chip->gpio_lock);
  514. chip->gc.base = -1;
  515. chip->gc.ngpio = bitmap_weight(chip->hw_map, 64);
  516. chip->gc.parent = &pdev->dev;
  517. chip->gc.direction_input = xgpio_dir_in;
  518. chip->gc.direction_output = xgpio_dir_out;
  519. chip->gc.of_gpio_n_cells = cells;
  520. chip->gc.get = xgpio_get;
  521. chip->gc.set = xgpio_set;
  522. chip->gc.request = xgpio_request;
  523. chip->gc.free = xgpio_free;
  524. chip->gc.set_multiple = xgpio_set_multiple;
  525. chip->gc.label = dev_name(&pdev->dev);
  526. chip->regs = devm_platform_ioremap_resource(pdev, 0);
  527. if (IS_ERR(chip->regs)) {
  528. dev_err(&pdev->dev, "failed to ioremap memory resource\n");
  529. return PTR_ERR(chip->regs);
  530. }
  531. chip->clk = devm_clk_get_optional(&pdev->dev, NULL);
  532. if (IS_ERR(chip->clk))
  533. return dev_err_probe(&pdev->dev, PTR_ERR(chip->clk), "input clock not found.\n");
  534. status = clk_prepare_enable(chip->clk);
  535. if (status < 0) {
  536. dev_err(&pdev->dev, "Failed to prepare clk\n");
  537. return status;
  538. }
  539. pm_runtime_get_noresume(&pdev->dev);
  540. pm_runtime_set_active(&pdev->dev);
  541. pm_runtime_enable(&pdev->dev);
  542. xgpio_save_regs(chip);
  543. chip->irq = platform_get_irq_optional(pdev, 0);
  544. if (chip->irq <= 0)
  545. goto skip_irq;
  546. chip->irqchip.name = "gpio-xilinx";
  547. chip->irqchip.irq_ack = xgpio_irq_ack;
  548. chip->irqchip.irq_mask = xgpio_irq_mask;
  549. chip->irqchip.irq_unmask = xgpio_irq_unmask;
  550. chip->irqchip.irq_set_type = xgpio_set_irq_type;
  551. /* Disable per-channel interrupts */
  552. xgpio_writereg(chip->regs + XGPIO_IPIER_OFFSET, 0);
  553. /* Clear any existing per-channel interrupts */
  554. temp = xgpio_readreg(chip->regs + XGPIO_IPISR_OFFSET);
  555. xgpio_writereg(chip->regs + XGPIO_IPISR_OFFSET, temp);
  556. /* Enable global interrupts */
  557. xgpio_writereg(chip->regs + XGPIO_GIER_OFFSET, XGPIO_GIER_IE);
  558. girq = &chip->gc.irq;
  559. girq->chip = &chip->irqchip;
  560. girq->parent_handler = xgpio_irqhandler;
  561. girq->num_parents = 1;
  562. girq->parents = devm_kcalloc(&pdev->dev, 1,
  563. sizeof(*girq->parents),
  564. GFP_KERNEL);
  565. if (!girq->parents) {
  566. status = -ENOMEM;
  567. goto err_pm_put;
  568. }
  569. girq->parents[0] = chip->irq;
  570. girq->default_type = IRQ_TYPE_NONE;
  571. girq->handler = handle_bad_irq;
  572. skip_irq:
  573. status = devm_gpiochip_add_data(&pdev->dev, &chip->gc, chip);
  574. if (status) {
  575. dev_err(&pdev->dev, "failed to add GPIO chip\n");
  576. goto err_pm_put;
  577. }
  578. pm_runtime_put(&pdev->dev);
  579. return 0;
  580. err_pm_put:
  581. pm_runtime_disable(&pdev->dev);
  582. pm_runtime_put_noidle(&pdev->dev);
  583. clk_disable_unprepare(chip->clk);
  584. return status;
  585. }
  586. static const struct of_device_id xgpio_of_match[] = {
  587. { .compatible = "xlnx,xps-gpio-1.00.a", },
  588. { /* end of list */ },
  589. };
  590. MODULE_DEVICE_TABLE(of, xgpio_of_match);
  591. static struct platform_driver xgpio_plat_driver = {
  592. .probe = xgpio_probe,
  593. .remove = xgpio_remove,
  594. .driver = {
  595. .name = "gpio-xilinx",
  596. .of_match_table = xgpio_of_match,
  597. .pm = &xgpio_dev_pm_ops,
  598. },
  599. };
  600. static int __init xgpio_init(void)
  601. {
  602. return platform_driver_register(&xgpio_plat_driver);
  603. }
  604. subsys_initcall(xgpio_init);
  605. static void __exit xgpio_exit(void)
  606. {
  607. platform_driver_unregister(&xgpio_plat_driver);
  608. }
  609. module_exit(xgpio_exit);
  610. MODULE_AUTHOR("Xilinx, Inc.");
  611. MODULE_DESCRIPTION("Xilinx GPIO driver");
  612. MODULE_LICENSE("GPL");