gpio-zynq.c 29 KB

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