gpio-zynq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * Xilinx Zynq GPIO device driver
  3. *
  4. * Copyright (C) 2009 - 2014 Xilinx, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License as published by the Free Software
  8. * Foundation; either version 2 of the License, or (at your option) any later
  9. * version.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/clk.h>
  13. #include <linux/gpio/driver.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/pm_runtime.h>
  20. #define DRIVER_NAME "zynq-gpio"
  21. /* Maximum banks */
  22. #define ZYNQ_GPIO_MAX_BANK 4
  23. #define ZYNQ_GPIO_BANK0_NGPIO 32
  24. #define ZYNQ_GPIO_BANK1_NGPIO 22
  25. #define ZYNQ_GPIO_BANK2_NGPIO 32
  26. #define ZYNQ_GPIO_BANK3_NGPIO 32
  27. #define ZYNQ_GPIO_NR_GPIOS (ZYNQ_GPIO_BANK0_NGPIO + \
  28. ZYNQ_GPIO_BANK1_NGPIO + \
  29. ZYNQ_GPIO_BANK2_NGPIO + \
  30. ZYNQ_GPIO_BANK3_NGPIO)
  31. #define ZYNQ_GPIO_BANK0_PIN_MIN 0
  32. #define ZYNQ_GPIO_BANK0_PIN_MAX (ZYNQ_GPIO_BANK0_PIN_MIN + \
  33. ZYNQ_GPIO_BANK0_NGPIO - 1)
  34. #define ZYNQ_GPIO_BANK1_PIN_MIN (ZYNQ_GPIO_BANK0_PIN_MAX + 1)
  35. #define ZYNQ_GPIO_BANK1_PIN_MAX (ZYNQ_GPIO_BANK1_PIN_MIN + \
  36. ZYNQ_GPIO_BANK1_NGPIO - 1)
  37. #define ZYNQ_GPIO_BANK2_PIN_MIN (ZYNQ_GPIO_BANK1_PIN_MAX + 1)
  38. #define ZYNQ_GPIO_BANK2_PIN_MAX (ZYNQ_GPIO_BANK2_PIN_MIN + \
  39. ZYNQ_GPIO_BANK2_NGPIO - 1)
  40. #define ZYNQ_GPIO_BANK3_PIN_MIN (ZYNQ_GPIO_BANK2_PIN_MAX + 1)
  41. #define ZYNQ_GPIO_BANK3_PIN_MAX (ZYNQ_GPIO_BANK3_PIN_MIN + \
  42. ZYNQ_GPIO_BANK3_NGPIO - 1)
  43. /* Register offsets for the GPIO device */
  44. /* LSW Mask & Data -WO */
  45. #define ZYNQ_GPIO_DATA_LSW_OFFSET(BANK) (0x000 + (8 * BANK))
  46. /* MSW Mask & Data -WO */
  47. #define ZYNQ_GPIO_DATA_MSW_OFFSET(BANK) (0x004 + (8 * BANK))
  48. /* Data Register-RW */
  49. #define ZYNQ_GPIO_DATA_RO_OFFSET(BANK) (0x060 + (4 * BANK))
  50. /* Direction mode reg-RW */
  51. #define ZYNQ_GPIO_DIRM_OFFSET(BANK) (0x204 + (0x40 * BANK))
  52. /* Output enable reg-RW */
  53. #define ZYNQ_GPIO_OUTEN_OFFSET(BANK) (0x208 + (0x40 * BANK))
  54. /* Interrupt mask reg-RO */
  55. #define ZYNQ_GPIO_INTMASK_OFFSET(BANK) (0x20C + (0x40 * BANK))
  56. /* Interrupt enable reg-WO */
  57. #define ZYNQ_GPIO_INTEN_OFFSET(BANK) (0x210 + (0x40 * BANK))
  58. /* Interrupt disable reg-WO */
  59. #define ZYNQ_GPIO_INTDIS_OFFSET(BANK) (0x214 + (0x40 * BANK))
  60. /* Interrupt status reg-RO */
  61. #define ZYNQ_GPIO_INTSTS_OFFSET(BANK) (0x218 + (0x40 * BANK))
  62. /* Interrupt type reg-RW */
  63. #define ZYNQ_GPIO_INTTYPE_OFFSET(BANK) (0x21C + (0x40 * BANK))
  64. /* Interrupt polarity reg-RW */
  65. #define ZYNQ_GPIO_INTPOL_OFFSET(BANK) (0x220 + (0x40 * BANK))
  66. /* Interrupt on any, reg-RW */
  67. #define ZYNQ_GPIO_INTANY_OFFSET(BANK) (0x224 + (0x40 * BANK))
  68. /* Disable all interrupts mask */
  69. #define ZYNQ_GPIO_IXR_DISABLE_ALL 0xFFFFFFFF
  70. /* Mid pin number of a bank */
  71. #define ZYNQ_GPIO_MID_PIN_NUM 16
  72. /* GPIO upper 16 bit mask */
  73. #define ZYNQ_GPIO_UPPER_MASK 0xFFFF0000
  74. /**
  75. * struct zynq_gpio - gpio device private data structure
  76. * @chip: instance of the gpio_chip
  77. * @base_addr: base address of the GPIO device
  78. * @clk: clock resource for this controller
  79. * @irq: interrupt for the GPIO device
  80. */
  81. struct zynq_gpio {
  82. struct gpio_chip chip;
  83. void __iomem *base_addr;
  84. struct clk *clk;
  85. int irq;
  86. };
  87. static struct irq_chip zynq_gpio_level_irqchip;
  88. static struct irq_chip zynq_gpio_edge_irqchip;
  89. /**
  90. * zynq_gpio_get_bank_pin - Get the bank number and pin number within that bank
  91. * for a given pin in the GPIO device
  92. * @pin_num: gpio pin number within the device
  93. * @bank_num: an output parameter used to return the bank number of the gpio
  94. * pin
  95. * @bank_pin_num: an output parameter used to return pin number within a bank
  96. * for the given gpio pin
  97. *
  98. * Returns the bank number and pin offset within the bank.
  99. */
  100. static inline void zynq_gpio_get_bank_pin(unsigned int pin_num,
  101. unsigned int *bank_num,
  102. unsigned int *bank_pin_num)
  103. {
  104. switch (pin_num) {
  105. case ZYNQ_GPIO_BANK0_PIN_MIN ... ZYNQ_GPIO_BANK0_PIN_MAX:
  106. *bank_num = 0;
  107. *bank_pin_num = pin_num;
  108. break;
  109. case ZYNQ_GPIO_BANK1_PIN_MIN ... ZYNQ_GPIO_BANK1_PIN_MAX:
  110. *bank_num = 1;
  111. *bank_pin_num = pin_num - ZYNQ_GPIO_BANK1_PIN_MIN;
  112. break;
  113. case ZYNQ_GPIO_BANK2_PIN_MIN ... ZYNQ_GPIO_BANK2_PIN_MAX:
  114. *bank_num = 2;
  115. *bank_pin_num = pin_num - ZYNQ_GPIO_BANK2_PIN_MIN;
  116. break;
  117. case ZYNQ_GPIO_BANK3_PIN_MIN ... ZYNQ_GPIO_BANK3_PIN_MAX:
  118. *bank_num = 3;
  119. *bank_pin_num = pin_num - ZYNQ_GPIO_BANK3_PIN_MIN;
  120. break;
  121. default:
  122. WARN(true, "invalid GPIO pin number: %u", pin_num);
  123. *bank_num = 0;
  124. *bank_pin_num = 0;
  125. break;
  126. }
  127. }
  128. static const unsigned int zynq_gpio_bank_offset[] = {
  129. ZYNQ_GPIO_BANK0_PIN_MIN,
  130. ZYNQ_GPIO_BANK1_PIN_MIN,
  131. ZYNQ_GPIO_BANK2_PIN_MIN,
  132. ZYNQ_GPIO_BANK3_PIN_MIN,
  133. };
  134. /**
  135. * zynq_gpio_get_value - Get the state of the specified pin of GPIO device
  136. * @chip: gpio_chip instance to be worked on
  137. * @pin: gpio pin number within the device
  138. *
  139. * This function reads the state of the specified pin of the GPIO device.
  140. *
  141. * Return: 0 if the pin is low, 1 if pin is high.
  142. */
  143. static int zynq_gpio_get_value(struct gpio_chip *chip, unsigned int pin)
  144. {
  145. u32 data;
  146. unsigned int bank_num, bank_pin_num;
  147. struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
  148. zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
  149. data = readl_relaxed(gpio->base_addr +
  150. ZYNQ_GPIO_DATA_RO_OFFSET(bank_num));
  151. return (data >> bank_pin_num) & 1;
  152. }
  153. /**
  154. * zynq_gpio_set_value - Modify the state of the pin with specified value
  155. * @chip: gpio_chip instance to be worked on
  156. * @pin: gpio pin number within the device
  157. * @state: value used to modify the state of the specified pin
  158. *
  159. * This function calculates the register offset (i.e to lower 16 bits or
  160. * upper 16 bits) based on the given pin number and sets the state of a
  161. * gpio pin to the specified value. The state is either 0 or non-zero.
  162. */
  163. static void zynq_gpio_set_value(struct gpio_chip *chip, unsigned int pin,
  164. int state)
  165. {
  166. unsigned int reg_offset, bank_num, bank_pin_num;
  167. struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
  168. zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
  169. if (bank_pin_num >= ZYNQ_GPIO_MID_PIN_NUM) {
  170. /* only 16 data bits in bit maskable reg */
  171. bank_pin_num -= ZYNQ_GPIO_MID_PIN_NUM;
  172. reg_offset = ZYNQ_GPIO_DATA_MSW_OFFSET(bank_num);
  173. } else {
  174. reg_offset = ZYNQ_GPIO_DATA_LSW_OFFSET(bank_num);
  175. }
  176. /*
  177. * get the 32 bit value to be written to the mask/data register where
  178. * the upper 16 bits is the mask and lower 16 bits is the data
  179. */
  180. state = !!state;
  181. state = ~(1 << (bank_pin_num + ZYNQ_GPIO_MID_PIN_NUM)) &
  182. ((state << bank_pin_num) | ZYNQ_GPIO_UPPER_MASK);
  183. writel_relaxed(state, gpio->base_addr + reg_offset);
  184. }
  185. /**
  186. * zynq_gpio_dir_in - Set the direction of the specified GPIO pin as input
  187. * @chip: gpio_chip instance to be worked on
  188. * @pin: gpio pin number within the device
  189. *
  190. * This function uses the read-modify-write sequence to set the direction of
  191. * the gpio pin as input.
  192. *
  193. * Return: 0 always
  194. */
  195. static int zynq_gpio_dir_in(struct gpio_chip *chip, unsigned int pin)
  196. {
  197. u32 reg;
  198. unsigned int bank_num, bank_pin_num;
  199. struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
  200. zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
  201. /* bank 0 pins 7 and 8 are special and cannot be used as inputs */
  202. if (bank_num == 0 && (bank_pin_num == 7 || bank_pin_num == 8))
  203. return -EINVAL;
  204. /* clear the bit in direction mode reg to set the pin as input */
  205. reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  206. reg &= ~BIT(bank_pin_num);
  207. writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  208. return 0;
  209. }
  210. /**
  211. * zynq_gpio_dir_out - Set the direction of the specified GPIO pin as output
  212. * @chip: gpio_chip instance to be worked on
  213. * @pin: gpio pin number within the device
  214. * @state: value to be written to specified pin
  215. *
  216. * This function sets the direction of specified GPIO pin as output, configures
  217. * the Output Enable register for the pin and uses zynq_gpio_set to set
  218. * the state of the pin to the value specified.
  219. *
  220. * Return: 0 always
  221. */
  222. static int zynq_gpio_dir_out(struct gpio_chip *chip, unsigned int pin,
  223. int state)
  224. {
  225. u32 reg;
  226. unsigned int bank_num, bank_pin_num;
  227. struct zynq_gpio *gpio = container_of(chip, struct zynq_gpio, chip);
  228. zynq_gpio_get_bank_pin(pin, &bank_num, &bank_pin_num);
  229. /* set the GPIO pin as output */
  230. reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  231. reg |= BIT(bank_pin_num);
  232. writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_DIRM_OFFSET(bank_num));
  233. /* configure the output enable reg for the pin */
  234. reg = readl_relaxed(gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
  235. reg |= BIT(bank_pin_num);
  236. writel_relaxed(reg, gpio->base_addr + ZYNQ_GPIO_OUTEN_OFFSET(bank_num));
  237. /* set the state of the pin */
  238. zynq_gpio_set_value(chip, pin, state);
  239. return 0;
  240. }
  241. /**
  242. * zynq_gpio_irq_mask - Disable the interrupts for a gpio pin
  243. * @irq_data: per irq and chip data passed down to chip functions
  244. *
  245. * This function calculates gpio pin number from irq number and sets the
  246. * bit in the Interrupt Disable register of the corresponding bank to disable
  247. * interrupts for that pin.
  248. */
  249. static void zynq_gpio_irq_mask(struct irq_data *irq_data)
  250. {
  251. unsigned int device_pin_num, bank_num, bank_pin_num;
  252. struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
  253. device_pin_num = irq_data->hwirq;
  254. zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
  255. writel_relaxed(BIT(bank_pin_num),
  256. gpio->base_addr + ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
  257. }
  258. /**
  259. * zynq_gpio_irq_unmask - Enable the interrupts for a gpio pin
  260. * @irq_data: irq data containing irq number of gpio pin for the interrupt
  261. * to enable
  262. *
  263. * This function calculates the gpio pin number from irq number and sets the
  264. * bit in the Interrupt Enable register of the corresponding bank to enable
  265. * interrupts for that pin.
  266. */
  267. static void zynq_gpio_irq_unmask(struct irq_data *irq_data)
  268. {
  269. unsigned int device_pin_num, bank_num, bank_pin_num;
  270. struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
  271. device_pin_num = irq_data->hwirq;
  272. zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
  273. writel_relaxed(BIT(bank_pin_num),
  274. gpio->base_addr + ZYNQ_GPIO_INTEN_OFFSET(bank_num));
  275. }
  276. /**
  277. * zynq_gpio_irq_ack - Acknowledge the interrupt of a gpio pin
  278. * @irq_data: irq data containing irq number of gpio pin for the interrupt
  279. * to ack
  280. *
  281. * This function calculates gpio pin number from irq number and sets the bit
  282. * in the Interrupt Status Register of the corresponding bank, to ACK the irq.
  283. */
  284. static void zynq_gpio_irq_ack(struct irq_data *irq_data)
  285. {
  286. unsigned int device_pin_num, bank_num, bank_pin_num;
  287. struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
  288. device_pin_num = irq_data->hwirq;
  289. zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
  290. writel_relaxed(BIT(bank_pin_num),
  291. gpio->base_addr + ZYNQ_GPIO_INTSTS_OFFSET(bank_num));
  292. }
  293. /**
  294. * zynq_gpio_irq_enable - Enable the interrupts for a gpio pin
  295. * @irq_data: irq data containing irq number of gpio pin for the interrupt
  296. * to enable
  297. *
  298. * Clears the INTSTS bit and unmasks the given interrrupt.
  299. */
  300. static void zynq_gpio_irq_enable(struct irq_data *irq_data)
  301. {
  302. /*
  303. * The Zynq GPIO controller does not disable interrupt detection when
  304. * the interrupt is masked and only disables the propagation of the
  305. * interrupt. This means when the controller detects an interrupt
  306. * condition while the interrupt is logically disabled it will propagate
  307. * that interrupt event once the interrupt is enabled. This will cause
  308. * the interrupt consumer to see spurious interrupts to prevent this
  309. * first make sure that the interrupt is not asserted and then enable
  310. * it.
  311. */
  312. zynq_gpio_irq_ack(irq_data);
  313. zynq_gpio_irq_unmask(irq_data);
  314. }
  315. /**
  316. * zynq_gpio_set_irq_type - Set the irq type for a gpio pin
  317. * @irq_data: irq data containing irq number of gpio pin
  318. * @type: interrupt type that is to be set for the gpio pin
  319. *
  320. * This function gets the gpio pin number and its bank from the gpio pin number
  321. * and configures the INT_TYPE, INT_POLARITY and INT_ANY registers.
  322. *
  323. * Return: 0, negative error otherwise.
  324. * TYPE-EDGE_RISING, INT_TYPE - 1, INT_POLARITY - 1, INT_ANY - 0;
  325. * TYPE-EDGE_FALLING, INT_TYPE - 1, INT_POLARITY - 0, INT_ANY - 0;
  326. * TYPE-EDGE_BOTH, INT_TYPE - 1, INT_POLARITY - NA, INT_ANY - 1;
  327. * TYPE-LEVEL_HIGH, INT_TYPE - 0, INT_POLARITY - 1, INT_ANY - NA;
  328. * TYPE-LEVEL_LOW, INT_TYPE - 0, INT_POLARITY - 0, INT_ANY - NA
  329. */
  330. static int zynq_gpio_set_irq_type(struct irq_data *irq_data, unsigned int type)
  331. {
  332. u32 int_type, int_pol, int_any;
  333. unsigned int device_pin_num, bank_num, bank_pin_num;
  334. struct zynq_gpio *gpio = irq_data_get_irq_chip_data(irq_data);
  335. device_pin_num = irq_data->hwirq;
  336. zynq_gpio_get_bank_pin(device_pin_num, &bank_num, &bank_pin_num);
  337. int_type = readl_relaxed(gpio->base_addr +
  338. ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
  339. int_pol = readl_relaxed(gpio->base_addr +
  340. ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
  341. int_any = readl_relaxed(gpio->base_addr +
  342. ZYNQ_GPIO_INTANY_OFFSET(bank_num));
  343. /*
  344. * based on the type requested, configure the INT_TYPE, INT_POLARITY
  345. * and INT_ANY registers
  346. */
  347. switch (type) {
  348. case IRQ_TYPE_EDGE_RISING:
  349. int_type |= BIT(bank_pin_num);
  350. int_pol |= BIT(bank_pin_num);
  351. int_any &= ~BIT(bank_pin_num);
  352. break;
  353. case IRQ_TYPE_EDGE_FALLING:
  354. int_type |= BIT(bank_pin_num);
  355. int_pol &= ~BIT(bank_pin_num);
  356. int_any &= ~BIT(bank_pin_num);
  357. break;
  358. case IRQ_TYPE_EDGE_BOTH:
  359. int_type |= BIT(bank_pin_num);
  360. int_any |= BIT(bank_pin_num);
  361. break;
  362. case IRQ_TYPE_LEVEL_HIGH:
  363. int_type &= ~BIT(bank_pin_num);
  364. int_pol |= BIT(bank_pin_num);
  365. break;
  366. case IRQ_TYPE_LEVEL_LOW:
  367. int_type &= ~BIT(bank_pin_num);
  368. int_pol &= ~BIT(bank_pin_num);
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. writel_relaxed(int_type,
  374. gpio->base_addr + ZYNQ_GPIO_INTTYPE_OFFSET(bank_num));
  375. writel_relaxed(int_pol,
  376. gpio->base_addr + ZYNQ_GPIO_INTPOL_OFFSET(bank_num));
  377. writel_relaxed(int_any,
  378. gpio->base_addr + ZYNQ_GPIO_INTANY_OFFSET(bank_num));
  379. if (type & IRQ_TYPE_LEVEL_MASK) {
  380. __irq_set_chip_handler_name_locked(irq_data->irq,
  381. &zynq_gpio_level_irqchip, handle_fasteoi_irq, NULL);
  382. } else {
  383. __irq_set_chip_handler_name_locked(irq_data->irq,
  384. &zynq_gpio_edge_irqchip, handle_level_irq, NULL);
  385. }
  386. return 0;
  387. }
  388. static int zynq_gpio_set_wake(struct irq_data *data, unsigned int on)
  389. {
  390. struct zynq_gpio *gpio = irq_data_get_irq_chip_data(data);
  391. irq_set_irq_wake(gpio->irq, on);
  392. return 0;
  393. }
  394. /* irq chip descriptor */
  395. static struct irq_chip zynq_gpio_level_irqchip = {
  396. .name = DRIVER_NAME,
  397. .irq_enable = zynq_gpio_irq_enable,
  398. .irq_eoi = zynq_gpio_irq_ack,
  399. .irq_mask = zynq_gpio_irq_mask,
  400. .irq_unmask = zynq_gpio_irq_unmask,
  401. .irq_set_type = zynq_gpio_set_irq_type,
  402. .irq_set_wake = zynq_gpio_set_wake,
  403. .flags = IRQCHIP_EOI_THREADED | IRQCHIP_EOI_IF_HANDLED |
  404. IRQCHIP_MASK_ON_SUSPEND,
  405. };
  406. static struct irq_chip zynq_gpio_edge_irqchip = {
  407. .name = DRIVER_NAME,
  408. .irq_enable = zynq_gpio_irq_enable,
  409. .irq_ack = zynq_gpio_irq_ack,
  410. .irq_mask = zynq_gpio_irq_mask,
  411. .irq_unmask = zynq_gpio_irq_unmask,
  412. .irq_set_type = zynq_gpio_set_irq_type,
  413. .irq_set_wake = zynq_gpio_set_wake,
  414. .flags = IRQCHIP_MASK_ON_SUSPEND,
  415. };
  416. static void zynq_gpio_handle_bank_irq(struct zynq_gpio *gpio,
  417. unsigned int bank_num,
  418. unsigned long pending)
  419. {
  420. unsigned int bank_offset = zynq_gpio_bank_offset[bank_num];
  421. struct irq_domain *irqdomain = gpio->chip.irqdomain;
  422. int offset;
  423. if (!pending)
  424. return;
  425. for_each_set_bit(offset, &pending, 32) {
  426. unsigned int gpio_irq;
  427. gpio_irq = irq_find_mapping(irqdomain, offset + bank_offset);
  428. generic_handle_irq(gpio_irq);
  429. }
  430. }
  431. /**
  432. * zynq_gpio_irqhandler - IRQ handler for the gpio banks of a gpio device
  433. * @irq: irq number of the gpio bank where interrupt has occurred
  434. * @desc: irq descriptor instance of the 'irq'
  435. *
  436. * This function reads the Interrupt Status Register of each bank to get the
  437. * gpio pin number which has triggered an interrupt. It then acks the triggered
  438. * interrupt and calls the pin specific handler set by the higher layer
  439. * application for that pin.
  440. * Note: A bug is reported if no handler is set for the gpio pin.
  441. */
  442. static void zynq_gpio_irqhandler(unsigned int irq, struct irq_desc *desc)
  443. {
  444. u32 int_sts, int_enb;
  445. unsigned int bank_num;
  446. struct zynq_gpio *gpio = irq_get_handler_data(irq);
  447. struct irq_chip *irqchip = irq_desc_get_chip(desc);
  448. chained_irq_enter(irqchip, desc);
  449. for (bank_num = 0; bank_num < ZYNQ_GPIO_MAX_BANK; bank_num++) {
  450. int_sts = readl_relaxed(gpio->base_addr +
  451. ZYNQ_GPIO_INTSTS_OFFSET(bank_num));
  452. int_enb = readl_relaxed(gpio->base_addr +
  453. ZYNQ_GPIO_INTMASK_OFFSET(bank_num));
  454. zynq_gpio_handle_bank_irq(gpio, bank_num, int_sts & ~int_enb);
  455. }
  456. chained_irq_exit(irqchip, desc);
  457. }
  458. static int __maybe_unused zynq_gpio_suspend(struct device *dev)
  459. {
  460. struct platform_device *pdev = to_platform_device(dev);
  461. int irq = platform_get_irq(pdev, 0);
  462. struct irq_data *data = irq_get_irq_data(irq);
  463. if (!irqd_is_wakeup_set(data))
  464. return pm_runtime_force_suspend(dev);
  465. return 0;
  466. }
  467. static int __maybe_unused zynq_gpio_resume(struct device *dev)
  468. {
  469. struct platform_device *pdev = to_platform_device(dev);
  470. int irq = platform_get_irq(pdev, 0);
  471. struct irq_data *data = irq_get_irq_data(irq);
  472. if (!irqd_is_wakeup_set(data))
  473. return pm_runtime_force_resume(dev);
  474. return 0;
  475. }
  476. static int __maybe_unused zynq_gpio_runtime_suspend(struct device *dev)
  477. {
  478. struct platform_device *pdev = to_platform_device(dev);
  479. struct zynq_gpio *gpio = platform_get_drvdata(pdev);
  480. clk_disable_unprepare(gpio->clk);
  481. return 0;
  482. }
  483. static int __maybe_unused zynq_gpio_runtime_resume(struct device *dev)
  484. {
  485. struct platform_device *pdev = to_platform_device(dev);
  486. struct zynq_gpio *gpio = platform_get_drvdata(pdev);
  487. return clk_prepare_enable(gpio->clk);
  488. }
  489. static int zynq_gpio_request(struct gpio_chip *chip, unsigned offset)
  490. {
  491. int ret;
  492. ret = pm_runtime_get_sync(chip->dev);
  493. /*
  494. * If the device is already active pm_runtime_get() will return 1 on
  495. * success, but gpio_request still needs to return 0.
  496. */
  497. return ret < 0 ? ret : 0;
  498. }
  499. static void zynq_gpio_free(struct gpio_chip *chip, unsigned offset)
  500. {
  501. pm_runtime_put(chip->dev);
  502. }
  503. static const struct dev_pm_ops zynq_gpio_dev_pm_ops = {
  504. SET_SYSTEM_SLEEP_PM_OPS(zynq_gpio_suspend, zynq_gpio_resume)
  505. SET_PM_RUNTIME_PM_OPS(zynq_gpio_runtime_suspend,
  506. zynq_gpio_runtime_resume, NULL)
  507. };
  508. /**
  509. * zynq_gpio_probe - Initialization method for a zynq_gpio device
  510. * @pdev: platform device instance
  511. *
  512. * This function allocates memory resources for the gpio device and registers
  513. * all the banks of the device. It will also set up interrupts for the gpio
  514. * pins.
  515. * Note: Interrupts are disabled for all the banks during initialization.
  516. *
  517. * Return: 0 on success, negative error otherwise.
  518. */
  519. static int zynq_gpio_probe(struct platform_device *pdev)
  520. {
  521. int ret, bank_num;
  522. struct zynq_gpio *gpio;
  523. struct gpio_chip *chip;
  524. struct resource *res;
  525. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  526. if (!gpio)
  527. return -ENOMEM;
  528. platform_set_drvdata(pdev, gpio);
  529. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  530. gpio->base_addr = devm_ioremap_resource(&pdev->dev, res);
  531. if (IS_ERR(gpio->base_addr))
  532. return PTR_ERR(gpio->base_addr);
  533. gpio->irq = platform_get_irq(pdev, 0);
  534. if (gpio->irq < 0) {
  535. dev_err(&pdev->dev, "invalid IRQ\n");
  536. return gpio->irq;
  537. }
  538. /* configure the gpio chip */
  539. chip = &gpio->chip;
  540. chip->label = "zynq_gpio";
  541. chip->owner = THIS_MODULE;
  542. chip->dev = &pdev->dev;
  543. chip->get = zynq_gpio_get_value;
  544. chip->set = zynq_gpio_set_value;
  545. chip->request = zynq_gpio_request;
  546. chip->free = zynq_gpio_free;
  547. chip->direction_input = zynq_gpio_dir_in;
  548. chip->direction_output = zynq_gpio_dir_out;
  549. chip->base = -1;
  550. chip->ngpio = ZYNQ_GPIO_NR_GPIOS;
  551. /* Enable GPIO clock */
  552. gpio->clk = devm_clk_get(&pdev->dev, NULL);
  553. if (IS_ERR(gpio->clk)) {
  554. dev_err(&pdev->dev, "input clock not found.\n");
  555. return PTR_ERR(gpio->clk);
  556. }
  557. ret = clk_prepare_enable(gpio->clk);
  558. if (ret) {
  559. dev_err(&pdev->dev, "Unable to enable clock.\n");
  560. return ret;
  561. }
  562. /* report a bug if gpio chip registration fails */
  563. ret = gpiochip_add(chip);
  564. if (ret) {
  565. dev_err(&pdev->dev, "Failed to add gpio chip\n");
  566. goto err_disable_clk;
  567. }
  568. /* disable interrupts for all banks */
  569. for (bank_num = 0; bank_num < ZYNQ_GPIO_MAX_BANK; bank_num++)
  570. writel_relaxed(ZYNQ_GPIO_IXR_DISABLE_ALL, gpio->base_addr +
  571. ZYNQ_GPIO_INTDIS_OFFSET(bank_num));
  572. ret = gpiochip_irqchip_add(chip, &zynq_gpio_edge_irqchip, 0,
  573. handle_level_irq, IRQ_TYPE_NONE);
  574. if (ret) {
  575. dev_err(&pdev->dev, "Failed to add irq chip\n");
  576. goto err_rm_gpiochip;
  577. }
  578. gpiochip_set_chained_irqchip(chip, &zynq_gpio_edge_irqchip, gpio->irq,
  579. zynq_gpio_irqhandler);
  580. pm_runtime_set_active(&pdev->dev);
  581. pm_runtime_enable(&pdev->dev);
  582. return 0;
  583. err_rm_gpiochip:
  584. gpiochip_remove(chip);
  585. err_disable_clk:
  586. clk_disable_unprepare(gpio->clk);
  587. return ret;
  588. }
  589. /**
  590. * zynq_gpio_remove - Driver removal function
  591. * @pdev: platform device instance
  592. *
  593. * Return: 0 always
  594. */
  595. static int zynq_gpio_remove(struct platform_device *pdev)
  596. {
  597. struct zynq_gpio *gpio = platform_get_drvdata(pdev);
  598. pm_runtime_get_sync(&pdev->dev);
  599. gpiochip_remove(&gpio->chip);
  600. clk_disable_unprepare(gpio->clk);
  601. device_set_wakeup_capable(&pdev->dev, 0);
  602. return 0;
  603. }
  604. static struct of_device_id zynq_gpio_of_match[] = {
  605. { .compatible = "xlnx,zynq-gpio-1.0", },
  606. { /* end of table */ }
  607. };
  608. MODULE_DEVICE_TABLE(of, zynq_gpio_of_match);
  609. static struct platform_driver zynq_gpio_driver = {
  610. .driver = {
  611. .name = DRIVER_NAME,
  612. .pm = &zynq_gpio_dev_pm_ops,
  613. .of_match_table = zynq_gpio_of_match,
  614. },
  615. .probe = zynq_gpio_probe,
  616. .remove = zynq_gpio_remove,
  617. };
  618. /**
  619. * zynq_gpio_init - Initial driver registration call
  620. *
  621. * Return: value from platform_driver_register
  622. */
  623. static int __init zynq_gpio_init(void)
  624. {
  625. return platform_driver_register(&zynq_gpio_driver);
  626. }
  627. postcore_initcall(zynq_gpio_init);
  628. MODULE_AUTHOR("Xilinx Inc.");
  629. MODULE_DESCRIPTION("Zynq GPIO driver");
  630. MODULE_LICENSE("GPL");