gpio-aspeed.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright 2015 IBM Corp.
  3. *
  4. * Joel Stanley <joel@jms.id.au>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/pinctrl/consumer.h>
  19. struct aspeed_gpio {
  20. struct gpio_chip chip;
  21. spinlock_t lock;
  22. void __iomem *base;
  23. int irq;
  24. };
  25. struct aspeed_gpio_bank {
  26. uint16_t val_regs;
  27. uint16_t irq_regs;
  28. const char names[4];
  29. };
  30. static const struct aspeed_gpio_bank aspeed_gpio_banks[] = {
  31. {
  32. .val_regs = 0x0000,
  33. .irq_regs = 0x0008,
  34. .names = { 'A', 'B', 'C', 'D' },
  35. },
  36. {
  37. .val_regs = 0x0020,
  38. .irq_regs = 0x0028,
  39. .names = { 'E', 'F', 'G', 'H' },
  40. },
  41. {
  42. .val_regs = 0x0070,
  43. .irq_regs = 0x0098,
  44. .names = { 'I', 'J', 'K', 'L' },
  45. },
  46. {
  47. .val_regs = 0x0078,
  48. .irq_regs = 0x00e8,
  49. .names = { 'M', 'N', 'O', 'P' },
  50. },
  51. {
  52. .val_regs = 0x0080,
  53. .irq_regs = 0x0118,
  54. .names = { 'Q', 'R', 'S', 'T' },
  55. },
  56. {
  57. .val_regs = 0x0088,
  58. .irq_regs = 0x0148,
  59. .names = { 'U', 'V', 'W', 'X' },
  60. },
  61. /*
  62. * A bank exists for { 'Y', 'Z', "AA", "AB" }, but is not implemented.
  63. * Only half of GPIOs Y support interrupt configuration, and none of Z,
  64. * AA or AB do as they are output only.
  65. */
  66. };
  67. #define GPIO_BANK(x) ((x) >> 5)
  68. #define GPIO_OFFSET(x) ((x) & 0x1f)
  69. #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
  70. #define GPIO_DATA 0x00
  71. #define GPIO_DIR 0x04
  72. #define GPIO_IRQ_ENABLE 0x00
  73. #define GPIO_IRQ_TYPE0 0x04
  74. #define GPIO_IRQ_TYPE1 0x08
  75. #define GPIO_IRQ_TYPE2 0x0c
  76. #define GPIO_IRQ_STATUS 0x10
  77. static const struct aspeed_gpio_bank *to_bank(unsigned int offset)
  78. {
  79. unsigned int bank = GPIO_BANK(offset);
  80. WARN_ON(bank > ARRAY_SIZE(aspeed_gpio_banks));
  81. return &aspeed_gpio_banks[bank];
  82. }
  83. static void __iomem *bank_val_reg(struct aspeed_gpio *gpio,
  84. const struct aspeed_gpio_bank *bank,
  85. unsigned int reg)
  86. {
  87. return gpio->base + bank->val_regs + reg;
  88. }
  89. static void __iomem *bank_irq_reg(struct aspeed_gpio *gpio,
  90. const struct aspeed_gpio_bank *bank,
  91. unsigned int reg)
  92. {
  93. return gpio->base + bank->irq_regs + reg;
  94. }
  95. static int aspeed_gpio_get(struct gpio_chip *gc, unsigned int offset)
  96. {
  97. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  98. const struct aspeed_gpio_bank *bank = to_bank(offset);
  99. return !!(ioread32(bank_val_reg(gpio, bank, GPIO_DATA))
  100. & GPIO_BIT(offset));
  101. }
  102. static void __aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
  103. int val)
  104. {
  105. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  106. const struct aspeed_gpio_bank *bank = to_bank(offset);
  107. void __iomem *addr;
  108. u32 reg;
  109. addr = bank_val_reg(gpio, bank, GPIO_DATA);
  110. reg = ioread32(addr);
  111. if (val)
  112. reg |= GPIO_BIT(offset);
  113. else
  114. reg &= ~GPIO_BIT(offset);
  115. iowrite32(reg, addr);
  116. }
  117. static void aspeed_gpio_set(struct gpio_chip *gc, unsigned int offset,
  118. int val)
  119. {
  120. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  121. unsigned long flags;
  122. spin_lock_irqsave(&gpio->lock, flags);
  123. __aspeed_gpio_set(gc, offset, val);
  124. spin_unlock_irqrestore(&gpio->lock, flags);
  125. }
  126. static int aspeed_gpio_dir_in(struct gpio_chip *gc, unsigned int offset)
  127. {
  128. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  129. const struct aspeed_gpio_bank *bank = to_bank(offset);
  130. unsigned long flags;
  131. u32 reg;
  132. spin_lock_irqsave(&gpio->lock, flags);
  133. reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
  134. iowrite32(reg & ~GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
  135. spin_unlock_irqrestore(&gpio->lock, flags);
  136. return 0;
  137. }
  138. static int aspeed_gpio_dir_out(struct gpio_chip *gc,
  139. unsigned int offset, int val)
  140. {
  141. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  142. const struct aspeed_gpio_bank *bank = to_bank(offset);
  143. unsigned long flags;
  144. u32 reg;
  145. spin_lock_irqsave(&gpio->lock, flags);
  146. reg = ioread32(bank_val_reg(gpio, bank, GPIO_DIR));
  147. iowrite32(reg | GPIO_BIT(offset), bank_val_reg(gpio, bank, GPIO_DIR));
  148. __aspeed_gpio_set(gc, offset, val);
  149. spin_unlock_irqrestore(&gpio->lock, flags);
  150. return 0;
  151. }
  152. static int aspeed_gpio_get_direction(struct gpio_chip *gc, unsigned int offset)
  153. {
  154. struct aspeed_gpio *gpio = gpiochip_get_data(gc);
  155. const struct aspeed_gpio_bank *bank = to_bank(offset);
  156. unsigned long flags;
  157. u32 val;
  158. spin_lock_irqsave(&gpio->lock, flags);
  159. val = ioread32(bank_val_reg(gpio, bank, GPIO_DIR)) & GPIO_BIT(offset);
  160. spin_unlock_irqrestore(&gpio->lock, flags);
  161. return !val;
  162. }
  163. static inline int irqd_to_aspeed_gpio_data(struct irq_data *d,
  164. struct aspeed_gpio **gpio,
  165. const struct aspeed_gpio_bank **bank,
  166. u32 *bit)
  167. {
  168. int offset;
  169. offset = irqd_to_hwirq(d);
  170. *gpio = irq_data_get_irq_chip_data(d);
  171. *bank = to_bank(offset);
  172. *bit = GPIO_BIT(offset);
  173. return 0;
  174. }
  175. static void aspeed_gpio_irq_ack(struct irq_data *d)
  176. {
  177. const struct aspeed_gpio_bank *bank;
  178. struct aspeed_gpio *gpio;
  179. unsigned long flags;
  180. void __iomem *status_addr;
  181. u32 bit;
  182. int rc;
  183. rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
  184. if (rc)
  185. return;
  186. status_addr = bank_irq_reg(gpio, bank, GPIO_IRQ_STATUS);
  187. spin_lock_irqsave(&gpio->lock, flags);
  188. iowrite32(bit, status_addr);
  189. spin_unlock_irqrestore(&gpio->lock, flags);
  190. }
  191. static void aspeed_gpio_irq_set_mask(struct irq_data *d, bool set)
  192. {
  193. const struct aspeed_gpio_bank *bank;
  194. struct aspeed_gpio *gpio;
  195. unsigned long flags;
  196. u32 reg, bit;
  197. void __iomem *addr;
  198. int rc;
  199. rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
  200. if (rc)
  201. return;
  202. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_ENABLE);
  203. spin_lock_irqsave(&gpio->lock, flags);
  204. reg = ioread32(addr);
  205. if (set)
  206. reg |= bit;
  207. else
  208. reg &= ~bit;
  209. iowrite32(reg, addr);
  210. spin_unlock_irqrestore(&gpio->lock, flags);
  211. }
  212. static void aspeed_gpio_irq_mask(struct irq_data *d)
  213. {
  214. aspeed_gpio_irq_set_mask(d, false);
  215. }
  216. static void aspeed_gpio_irq_unmask(struct irq_data *d)
  217. {
  218. aspeed_gpio_irq_set_mask(d, true);
  219. }
  220. static int aspeed_gpio_set_type(struct irq_data *d, unsigned int type)
  221. {
  222. u32 type0 = 0;
  223. u32 type1 = 0;
  224. u32 type2 = 0;
  225. u32 bit, reg;
  226. const struct aspeed_gpio_bank *bank;
  227. irq_flow_handler_t handler;
  228. struct aspeed_gpio *gpio;
  229. unsigned long flags;
  230. void __iomem *addr;
  231. int rc;
  232. rc = irqd_to_aspeed_gpio_data(d, &gpio, &bank, &bit);
  233. if (rc)
  234. return -EINVAL;
  235. switch (type & IRQ_TYPE_SENSE_MASK) {
  236. case IRQ_TYPE_EDGE_BOTH:
  237. type2 |= bit;
  238. case IRQ_TYPE_EDGE_RISING:
  239. type0 |= bit;
  240. case IRQ_TYPE_EDGE_FALLING:
  241. handler = handle_edge_irq;
  242. break;
  243. case IRQ_TYPE_LEVEL_HIGH:
  244. type0 |= bit;
  245. case IRQ_TYPE_LEVEL_LOW:
  246. type1 |= bit;
  247. handler = handle_level_irq;
  248. break;
  249. default:
  250. return -EINVAL;
  251. }
  252. spin_lock_irqsave(&gpio->lock, flags);
  253. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE0);
  254. reg = ioread32(addr);
  255. reg = (reg & ~bit) | type0;
  256. iowrite32(reg, addr);
  257. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE1);
  258. reg = ioread32(addr);
  259. reg = (reg & ~bit) | type1;
  260. iowrite32(reg, addr);
  261. addr = bank_irq_reg(gpio, bank, GPIO_IRQ_TYPE2);
  262. reg = ioread32(addr);
  263. reg = (reg & ~bit) | type2;
  264. iowrite32(reg, addr);
  265. spin_unlock_irqrestore(&gpio->lock, flags);
  266. irq_set_handler_locked(d, handler);
  267. return 0;
  268. }
  269. static void aspeed_gpio_irq_handler(struct irq_desc *desc)
  270. {
  271. struct gpio_chip *gc = irq_desc_get_handler_data(desc);
  272. struct irq_chip *ic = irq_desc_get_chip(desc);
  273. struct aspeed_gpio *data = gpiochip_get_data(gc);
  274. unsigned int i, p, girq;
  275. unsigned long reg;
  276. chained_irq_enter(ic, desc);
  277. for (i = 0; i < ARRAY_SIZE(aspeed_gpio_banks); i++) {
  278. const struct aspeed_gpio_bank *bank = &aspeed_gpio_banks[i];
  279. reg = ioread32(bank_irq_reg(data, bank, GPIO_IRQ_STATUS));
  280. for_each_set_bit(p, &reg, 32) {
  281. girq = irq_find_mapping(gc->irqdomain, i * 32 + p);
  282. generic_handle_irq(girq);
  283. }
  284. }
  285. chained_irq_exit(ic, desc);
  286. }
  287. static struct irq_chip aspeed_gpio_irqchip = {
  288. .name = "aspeed-gpio",
  289. .irq_ack = aspeed_gpio_irq_ack,
  290. .irq_mask = aspeed_gpio_irq_mask,
  291. .irq_unmask = aspeed_gpio_irq_unmask,
  292. .irq_set_type = aspeed_gpio_set_type,
  293. };
  294. static int aspeed_gpio_setup_irqs(struct aspeed_gpio *gpio,
  295. struct platform_device *pdev)
  296. {
  297. int rc;
  298. rc = platform_get_irq(pdev, 0);
  299. if (rc < 0)
  300. return rc;
  301. gpio->irq = rc;
  302. rc = gpiochip_irqchip_add(&gpio->chip, &aspeed_gpio_irqchip,
  303. 0, handle_bad_irq, IRQ_TYPE_NONE);
  304. if (rc) {
  305. dev_info(&pdev->dev, "Could not add irqchip\n");
  306. return rc;
  307. }
  308. gpiochip_set_chained_irqchip(&gpio->chip, &aspeed_gpio_irqchip,
  309. gpio->irq, aspeed_gpio_irq_handler);
  310. return 0;
  311. }
  312. static int aspeed_gpio_request(struct gpio_chip *chip, unsigned int offset)
  313. {
  314. return pinctrl_request_gpio(chip->base + offset);
  315. }
  316. static void aspeed_gpio_free(struct gpio_chip *chip, unsigned int offset)
  317. {
  318. pinctrl_free_gpio(chip->base + offset);
  319. }
  320. static int __init aspeed_gpio_probe(struct platform_device *pdev)
  321. {
  322. struct aspeed_gpio *gpio;
  323. struct resource *res;
  324. int rc;
  325. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  326. if (!gpio)
  327. return -ENOMEM;
  328. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  329. gpio->base = devm_ioremap_resource(&pdev->dev, res);
  330. if (IS_ERR(gpio->base))
  331. return PTR_ERR(gpio->base);
  332. spin_lock_init(&gpio->lock);
  333. gpio->chip.ngpio = ARRAY_SIZE(aspeed_gpio_banks) * 32;
  334. gpio->chip.parent = &pdev->dev;
  335. gpio->chip.direction_input = aspeed_gpio_dir_in;
  336. gpio->chip.direction_output = aspeed_gpio_dir_out;
  337. gpio->chip.get_direction = aspeed_gpio_get_direction;
  338. gpio->chip.request = aspeed_gpio_request;
  339. gpio->chip.free = aspeed_gpio_free;
  340. gpio->chip.get = aspeed_gpio_get;
  341. gpio->chip.set = aspeed_gpio_set;
  342. gpio->chip.label = dev_name(&pdev->dev);
  343. gpio->chip.base = -1;
  344. rc = devm_gpiochip_add_data(&pdev->dev, &gpio->chip, gpio);
  345. if (rc < 0)
  346. return rc;
  347. return aspeed_gpio_setup_irqs(gpio, pdev);
  348. }
  349. static const struct of_device_id aspeed_gpio_of_table[] = {
  350. { .compatible = "aspeed,ast2400-gpio" },
  351. { .compatible = "aspeed,ast2500-gpio" },
  352. {}
  353. };
  354. MODULE_DEVICE_TABLE(of, aspeed_gpio_of_table);
  355. static struct platform_driver aspeed_gpio_driver = {
  356. .driver = {
  357. .name = KBUILD_MODNAME,
  358. .of_match_table = aspeed_gpio_of_table,
  359. },
  360. };
  361. module_platform_driver_probe(aspeed_gpio_driver, aspeed_gpio_probe);
  362. MODULE_DESCRIPTION("Aspeed GPIO Driver");
  363. MODULE_LICENSE("GPL");