gpio-zynq.c 24 KB

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