gpio-zynq.c 21 KB

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