gpio-rcar.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Renesas R-Car GPIO Support
  3. *
  4. * Copyright (C) 2013 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/ioport.h>
  21. #include <linux/irq.h>
  22. #include <linux/irqdomain.h>
  23. #include <linux/module.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/platform_data/gpio-rcar.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/slab.h>
  29. struct gpio_rcar_priv {
  30. void __iomem *base;
  31. spinlock_t lock;
  32. struct gpio_rcar_config config;
  33. struct platform_device *pdev;
  34. struct gpio_chip gpio_chip;
  35. struct irq_chip irq_chip;
  36. struct irq_domain *irq_domain;
  37. };
  38. #define IOINTSEL 0x00
  39. #define INOUTSEL 0x04
  40. #define OUTDT 0x08
  41. #define INDT 0x0c
  42. #define INTDT 0x10
  43. #define INTCLR 0x14
  44. #define INTMSK 0x18
  45. #define MSKCLR 0x1c
  46. #define POSNEG 0x20
  47. #define EDGLEVEL 0x24
  48. #define FILONOFF 0x28
  49. #define BOTHEDGE 0x4c
  50. static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
  51. {
  52. return ioread32(p->base + offs);
  53. }
  54. static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
  55. u32 value)
  56. {
  57. iowrite32(value, p->base + offs);
  58. }
  59. static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
  60. int bit, bool value)
  61. {
  62. u32 tmp = gpio_rcar_read(p, offs);
  63. if (value)
  64. tmp |= BIT(bit);
  65. else
  66. tmp &= ~BIT(bit);
  67. gpio_rcar_write(p, offs, tmp);
  68. }
  69. static void gpio_rcar_irq_disable(struct irq_data *d)
  70. {
  71. struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
  72. gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
  73. }
  74. static void gpio_rcar_irq_enable(struct irq_data *d)
  75. {
  76. struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
  77. gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
  78. }
  79. static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
  80. unsigned int hwirq,
  81. bool active_high_rising_edge,
  82. bool level_trigger,
  83. bool both)
  84. {
  85. unsigned long flags;
  86. /* follow steps in the GPIO documentation for
  87. * "Setting Edge-Sensitive Interrupt Input Mode" and
  88. * "Setting Level-Sensitive Interrupt Input Mode"
  89. */
  90. spin_lock_irqsave(&p->lock, flags);
  91. /* Configure postive or negative logic in POSNEG */
  92. gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
  93. /* Configure edge or level trigger in EDGLEVEL */
  94. gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
  95. /* Select one edge or both edges in BOTHEDGE */
  96. if (p->config.has_both_edge_trigger)
  97. gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
  98. /* Select "Interrupt Input Mode" in IOINTSEL */
  99. gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
  100. /* Write INTCLR in case of edge trigger */
  101. if (!level_trigger)
  102. gpio_rcar_write(p, INTCLR, BIT(hwirq));
  103. spin_unlock_irqrestore(&p->lock, flags);
  104. }
  105. static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
  106. {
  107. struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
  108. unsigned int hwirq = irqd_to_hwirq(d);
  109. dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
  110. switch (type & IRQ_TYPE_SENSE_MASK) {
  111. case IRQ_TYPE_LEVEL_HIGH:
  112. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
  113. false);
  114. break;
  115. case IRQ_TYPE_LEVEL_LOW:
  116. gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
  117. false);
  118. break;
  119. case IRQ_TYPE_EDGE_RISING:
  120. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
  121. false);
  122. break;
  123. case IRQ_TYPE_EDGE_FALLING:
  124. gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
  125. false);
  126. break;
  127. case IRQ_TYPE_EDGE_BOTH:
  128. if (!p->config.has_both_edge_trigger)
  129. return -EINVAL;
  130. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
  131. true);
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
  139. {
  140. struct gpio_rcar_priv *p = dev_id;
  141. u32 pending;
  142. unsigned int offset, irqs_handled = 0;
  143. while ((pending = gpio_rcar_read(p, INTDT))) {
  144. offset = __ffs(pending);
  145. gpio_rcar_write(p, INTCLR, BIT(offset));
  146. generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
  147. irqs_handled++;
  148. }
  149. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  150. }
  151. static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
  152. {
  153. return container_of(chip, struct gpio_rcar_priv, gpio_chip);
  154. }
  155. static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
  156. unsigned int gpio,
  157. bool output)
  158. {
  159. struct gpio_rcar_priv *p = gpio_to_priv(chip);
  160. unsigned long flags;
  161. /* follow steps in the GPIO documentation for
  162. * "Setting General Output Mode" and
  163. * "Setting General Input Mode"
  164. */
  165. spin_lock_irqsave(&p->lock, flags);
  166. /* Configure postive logic in POSNEG */
  167. gpio_rcar_modify_bit(p, POSNEG, gpio, false);
  168. /* Select "General Input/Output Mode" in IOINTSEL */
  169. gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
  170. /* Select Input Mode or Output Mode in INOUTSEL */
  171. gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
  172. spin_unlock_irqrestore(&p->lock, flags);
  173. }
  174. static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
  175. {
  176. return pinctrl_request_gpio(chip->base + offset);
  177. }
  178. static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
  179. {
  180. pinctrl_free_gpio(chip->base + offset);
  181. /* Set the GPIO as an input to ensure that the next GPIO request won't
  182. * drive the GPIO pin as an output.
  183. */
  184. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  185. }
  186. static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
  187. {
  188. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  189. return 0;
  190. }
  191. static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
  192. {
  193. return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & BIT(offset));
  194. }
  195. static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
  196. {
  197. struct gpio_rcar_priv *p = gpio_to_priv(chip);
  198. unsigned long flags;
  199. spin_lock_irqsave(&p->lock, flags);
  200. gpio_rcar_modify_bit(p, OUTDT, offset, value);
  201. spin_unlock_irqrestore(&p->lock, flags);
  202. }
  203. static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
  204. int value)
  205. {
  206. /* write GPIO value to output before selecting output mode of pin */
  207. gpio_rcar_set(chip, offset, value);
  208. gpio_rcar_config_general_input_output_mode(chip, offset, true);
  209. return 0;
  210. }
  211. static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
  212. {
  213. return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
  214. }
  215. static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int virq,
  216. irq_hw_number_t hw)
  217. {
  218. struct gpio_rcar_priv *p = h->host_data;
  219. dev_dbg(&p->pdev->dev, "map hw irq = %d, virq = %d\n", (int)hw, virq);
  220. irq_set_chip_data(virq, h->host_data);
  221. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  222. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  223. return 0;
  224. }
  225. static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
  226. .map = gpio_rcar_irq_domain_map,
  227. };
  228. static int gpio_rcar_probe(struct platform_device *pdev)
  229. {
  230. struct gpio_rcar_config *pdata = pdev->dev.platform_data;
  231. struct gpio_rcar_priv *p;
  232. struct resource *io, *irq;
  233. struct gpio_chip *gpio_chip;
  234. struct irq_chip *irq_chip;
  235. const char *name = dev_name(&pdev->dev);
  236. int ret;
  237. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  238. if (!p) {
  239. dev_err(&pdev->dev, "failed to allocate driver data\n");
  240. ret = -ENOMEM;
  241. goto err0;
  242. }
  243. /* deal with driver instance configuration */
  244. if (pdata)
  245. p->config = *pdata;
  246. p->pdev = pdev;
  247. platform_set_drvdata(pdev, p);
  248. spin_lock_init(&p->lock);
  249. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  250. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  251. if (!io || !irq) {
  252. dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
  253. ret = -EINVAL;
  254. goto err0;
  255. }
  256. p->base = devm_ioremap_nocache(&pdev->dev, io->start,
  257. resource_size(io));
  258. if (!p->base) {
  259. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  260. ret = -ENXIO;
  261. goto err0;
  262. }
  263. gpio_chip = &p->gpio_chip;
  264. gpio_chip->request = gpio_rcar_request;
  265. gpio_chip->free = gpio_rcar_free;
  266. gpio_chip->direction_input = gpio_rcar_direction_input;
  267. gpio_chip->get = gpio_rcar_get;
  268. gpio_chip->direction_output = gpio_rcar_direction_output;
  269. gpio_chip->set = gpio_rcar_set;
  270. gpio_chip->to_irq = gpio_rcar_to_irq;
  271. gpio_chip->label = name;
  272. gpio_chip->owner = THIS_MODULE;
  273. gpio_chip->base = p->config.gpio_base;
  274. gpio_chip->ngpio = p->config.number_of_pins;
  275. irq_chip = &p->irq_chip;
  276. irq_chip->name = name;
  277. irq_chip->irq_mask = gpio_rcar_irq_disable;
  278. irq_chip->irq_unmask = gpio_rcar_irq_enable;
  279. irq_chip->irq_enable = gpio_rcar_irq_enable;
  280. irq_chip->irq_disable = gpio_rcar_irq_disable;
  281. irq_chip->irq_set_type = gpio_rcar_irq_set_type;
  282. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED;
  283. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  284. p->config.number_of_pins,
  285. p->config.irq_base,
  286. &gpio_rcar_irq_domain_ops, p);
  287. if (!p->irq_domain) {
  288. ret = -ENXIO;
  289. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  290. goto err1;
  291. }
  292. if (devm_request_irq(&pdev->dev, irq->start,
  293. gpio_rcar_irq_handler, IRQF_SHARED, name, p)) {
  294. dev_err(&pdev->dev, "failed to request IRQ\n");
  295. ret = -ENOENT;
  296. goto err1;
  297. }
  298. ret = gpiochip_add(gpio_chip);
  299. if (ret) {
  300. dev_err(&pdev->dev, "failed to add GPIO controller\n");
  301. goto err1;
  302. }
  303. dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins);
  304. /* warn in case of mismatch if irq base is specified */
  305. if (p->config.irq_base) {
  306. ret = irq_find_mapping(p->irq_domain, 0);
  307. if (p->config.irq_base != ret)
  308. dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n",
  309. p->config.irq_base, ret);
  310. }
  311. ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
  312. gpio_chip->base, gpio_chip->ngpio);
  313. if (ret < 0)
  314. dev_warn(&pdev->dev, "failed to add pin range\n");
  315. return 0;
  316. err1:
  317. irq_domain_remove(p->irq_domain);
  318. err0:
  319. return ret;
  320. }
  321. static int gpio_rcar_remove(struct platform_device *pdev)
  322. {
  323. struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
  324. int ret;
  325. ret = gpiochip_remove(&p->gpio_chip);
  326. if (ret)
  327. return ret;
  328. irq_domain_remove(p->irq_domain);
  329. return 0;
  330. }
  331. static struct platform_driver gpio_rcar_device_driver = {
  332. .probe = gpio_rcar_probe,
  333. .remove = gpio_rcar_remove,
  334. .driver = {
  335. .name = "gpio_rcar",
  336. }
  337. };
  338. module_platform_driver(gpio_rcar_device_driver);
  339. MODULE_AUTHOR("Magnus Damm");
  340. MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
  341. MODULE_LICENSE("GPL v2");