gpio-zynq.c 25 KB

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