gpio-rcar.c 9.6 KB

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