gpio-rcar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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/of.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_data/gpio-rcar.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/slab.h>
  30. struct gpio_rcar_priv {
  31. void __iomem *base;
  32. spinlock_t lock;
  33. struct gpio_rcar_config config;
  34. struct platform_device *pdev;
  35. struct gpio_chip gpio_chip;
  36. struct irq_chip irq_chip;
  37. struct irq_domain *irq_domain;
  38. };
  39. #define IOINTSEL 0x00
  40. #define INOUTSEL 0x04
  41. #define OUTDT 0x08
  42. #define INDT 0x0c
  43. #define INTDT 0x10
  44. #define INTCLR 0x14
  45. #define INTMSK 0x18
  46. #define MSKCLR 0x1c
  47. #define POSNEG 0x20
  48. #define EDGLEVEL 0x24
  49. #define FILONOFF 0x28
  50. #define BOTHEDGE 0x4c
  51. #define RCAR_MAX_GPIO_PER_BANK 32
  52. static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
  53. {
  54. return ioread32(p->base + offs);
  55. }
  56. static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
  57. u32 value)
  58. {
  59. iowrite32(value, p->base + offs);
  60. }
  61. static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
  62. int bit, bool value)
  63. {
  64. u32 tmp = gpio_rcar_read(p, offs);
  65. if (value)
  66. tmp |= BIT(bit);
  67. else
  68. tmp &= ~BIT(bit);
  69. gpio_rcar_write(p, offs, tmp);
  70. }
  71. static void gpio_rcar_irq_disable(struct irq_data *d)
  72. {
  73. struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
  74. gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
  75. }
  76. static void gpio_rcar_irq_enable(struct irq_data *d)
  77. {
  78. struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
  79. gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
  80. }
  81. static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
  82. unsigned int hwirq,
  83. bool active_high_rising_edge,
  84. bool level_trigger,
  85. bool both)
  86. {
  87. unsigned long flags;
  88. /* follow steps in the GPIO documentation for
  89. * "Setting Edge-Sensitive Interrupt Input Mode" and
  90. * "Setting Level-Sensitive Interrupt Input Mode"
  91. */
  92. spin_lock_irqsave(&p->lock, flags);
  93. /* Configure postive or negative logic in POSNEG */
  94. gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
  95. /* Configure edge or level trigger in EDGLEVEL */
  96. gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
  97. /* Select one edge or both edges in BOTHEDGE */
  98. if (p->config.has_both_edge_trigger)
  99. gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
  100. /* Select "Interrupt Input Mode" in IOINTSEL */
  101. gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
  102. /* Write INTCLR in case of edge trigger */
  103. if (!level_trigger)
  104. gpio_rcar_write(p, INTCLR, BIT(hwirq));
  105. spin_unlock_irqrestore(&p->lock, flags);
  106. }
  107. static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
  108. {
  109. struct gpio_rcar_priv *p = irq_data_get_irq_chip_data(d);
  110. unsigned int hwirq = irqd_to_hwirq(d);
  111. dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
  112. switch (type & IRQ_TYPE_SENSE_MASK) {
  113. case IRQ_TYPE_LEVEL_HIGH:
  114. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
  115. false);
  116. break;
  117. case IRQ_TYPE_LEVEL_LOW:
  118. gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
  119. false);
  120. break;
  121. case IRQ_TYPE_EDGE_RISING:
  122. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
  123. false);
  124. break;
  125. case IRQ_TYPE_EDGE_FALLING:
  126. gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
  127. false);
  128. break;
  129. case IRQ_TYPE_EDGE_BOTH:
  130. if (!p->config.has_both_edge_trigger)
  131. return -EINVAL;
  132. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
  133. true);
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. return 0;
  139. }
  140. static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
  141. {
  142. struct gpio_rcar_priv *p = dev_id;
  143. u32 pending;
  144. unsigned int offset, irqs_handled = 0;
  145. while ((pending = gpio_rcar_read(p, INTDT))) {
  146. offset = __ffs(pending);
  147. gpio_rcar_write(p, INTCLR, BIT(offset));
  148. generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
  149. irqs_handled++;
  150. }
  151. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  152. }
  153. static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
  154. {
  155. return container_of(chip, struct gpio_rcar_priv, gpio_chip);
  156. }
  157. static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
  158. unsigned int gpio,
  159. bool output)
  160. {
  161. struct gpio_rcar_priv *p = gpio_to_priv(chip);
  162. unsigned long flags;
  163. /* follow steps in the GPIO documentation for
  164. * "Setting General Output Mode" and
  165. * "Setting General Input Mode"
  166. */
  167. spin_lock_irqsave(&p->lock, flags);
  168. /* Configure postive logic in POSNEG */
  169. gpio_rcar_modify_bit(p, POSNEG, gpio, false);
  170. /* Select "General Input/Output Mode" in IOINTSEL */
  171. gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
  172. /* Select Input Mode or Output Mode in INOUTSEL */
  173. gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
  174. spin_unlock_irqrestore(&p->lock, flags);
  175. }
  176. static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
  177. {
  178. return pinctrl_request_gpio(chip->base + offset);
  179. }
  180. static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
  181. {
  182. pinctrl_free_gpio(chip->base + offset);
  183. /* Set the GPIO as an input to ensure that the next GPIO request won't
  184. * drive the GPIO pin as an output.
  185. */
  186. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  187. }
  188. static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
  189. {
  190. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  191. return 0;
  192. }
  193. static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
  194. {
  195. u32 bit = BIT(offset);
  196. /* testing on r8a7790 shows that INDT does not show correct pin state
  197. * when configured as output, so use OUTDT in case of output pins */
  198. if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
  199. return (int)(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
  200. else
  201. return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
  202. }
  203. static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
  204. {
  205. struct gpio_rcar_priv *p = gpio_to_priv(chip);
  206. unsigned long flags;
  207. spin_lock_irqsave(&p->lock, flags);
  208. gpio_rcar_modify_bit(p, OUTDT, offset, value);
  209. spin_unlock_irqrestore(&p->lock, flags);
  210. }
  211. static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
  212. int value)
  213. {
  214. /* write GPIO value to output before selecting output mode of pin */
  215. gpio_rcar_set(chip, offset, value);
  216. gpio_rcar_config_general_input_output_mode(chip, offset, true);
  217. return 0;
  218. }
  219. static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
  220. {
  221. return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
  222. }
  223. static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int irq,
  224. irq_hw_number_t hwirq)
  225. {
  226. struct gpio_rcar_priv *p = h->host_data;
  227. dev_dbg(&p->pdev->dev, "map hw irq = %d, irq = %d\n", (int)hwirq, irq);
  228. irq_set_chip_data(irq, h->host_data);
  229. irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
  230. set_irq_flags(irq, IRQF_VALID); /* kill me now */
  231. return 0;
  232. }
  233. static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
  234. .map = gpio_rcar_irq_domain_map,
  235. };
  236. static void gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
  237. {
  238. struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
  239. struct device_node *np = p->pdev->dev.of_node;
  240. struct of_phandle_args args;
  241. int ret;
  242. if (pdata) {
  243. p->config = *pdata;
  244. } else if (IS_ENABLED(CONFIG_OF) && np) {
  245. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
  246. &args);
  247. p->config.number_of_pins = ret == 0 ? args.args[2]
  248. : RCAR_MAX_GPIO_PER_BANK;
  249. p->config.gpio_base = -1;
  250. }
  251. if (p->config.number_of_pins == 0 ||
  252. p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
  253. dev_warn(&p->pdev->dev,
  254. "Invalid number of gpio lines %u, using %u\n",
  255. p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
  256. p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
  257. }
  258. }
  259. static int gpio_rcar_probe(struct platform_device *pdev)
  260. {
  261. struct gpio_rcar_priv *p;
  262. struct resource *io, *irq;
  263. struct gpio_chip *gpio_chip;
  264. struct irq_chip *irq_chip;
  265. const char *name = dev_name(&pdev->dev);
  266. int ret;
  267. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  268. if (!p) {
  269. dev_err(&pdev->dev, "failed to allocate driver data\n");
  270. ret = -ENOMEM;
  271. goto err0;
  272. }
  273. p->pdev = pdev;
  274. spin_lock_init(&p->lock);
  275. /* Get device configuration from DT node or platform data. */
  276. gpio_rcar_parse_pdata(p);
  277. platform_set_drvdata(pdev, p);
  278. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  279. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  280. if (!io || !irq) {
  281. dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
  282. ret = -EINVAL;
  283. goto err0;
  284. }
  285. p->base = devm_ioremap_nocache(&pdev->dev, io->start,
  286. resource_size(io));
  287. if (!p->base) {
  288. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  289. ret = -ENXIO;
  290. goto err0;
  291. }
  292. gpio_chip = &p->gpio_chip;
  293. gpio_chip->request = gpio_rcar_request;
  294. gpio_chip->free = gpio_rcar_free;
  295. gpio_chip->direction_input = gpio_rcar_direction_input;
  296. gpio_chip->get = gpio_rcar_get;
  297. gpio_chip->direction_output = gpio_rcar_direction_output;
  298. gpio_chip->set = gpio_rcar_set;
  299. gpio_chip->to_irq = gpio_rcar_to_irq;
  300. gpio_chip->label = name;
  301. gpio_chip->dev = &pdev->dev;
  302. gpio_chip->owner = THIS_MODULE;
  303. gpio_chip->base = p->config.gpio_base;
  304. gpio_chip->ngpio = p->config.number_of_pins;
  305. irq_chip = &p->irq_chip;
  306. irq_chip->name = name;
  307. irq_chip->irq_mask = gpio_rcar_irq_disable;
  308. irq_chip->irq_unmask = gpio_rcar_irq_enable;
  309. irq_chip->irq_enable = gpio_rcar_irq_enable;
  310. irq_chip->irq_disable = gpio_rcar_irq_disable;
  311. irq_chip->irq_set_type = gpio_rcar_irq_set_type;
  312. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED;
  313. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  314. p->config.number_of_pins,
  315. p->config.irq_base,
  316. &gpio_rcar_irq_domain_ops, p);
  317. if (!p->irq_domain) {
  318. ret = -ENXIO;
  319. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  320. goto err0;
  321. }
  322. if (devm_request_irq(&pdev->dev, irq->start,
  323. gpio_rcar_irq_handler, IRQF_SHARED, name, p)) {
  324. dev_err(&pdev->dev, "failed to request IRQ\n");
  325. ret = -ENOENT;
  326. goto err1;
  327. }
  328. ret = gpiochip_add(gpio_chip);
  329. if (ret) {
  330. dev_err(&pdev->dev, "failed to add GPIO controller\n");
  331. goto err1;
  332. }
  333. dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins);
  334. /* warn in case of mismatch if irq base is specified */
  335. if (p->config.irq_base) {
  336. ret = irq_find_mapping(p->irq_domain, 0);
  337. if (p->config.irq_base != ret)
  338. dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n",
  339. p->config.irq_base, ret);
  340. }
  341. if (p->config.pctl_name) {
  342. ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
  343. gpio_chip->base, gpio_chip->ngpio);
  344. if (ret < 0)
  345. dev_warn(&pdev->dev, "failed to add pin range\n");
  346. }
  347. return 0;
  348. err1:
  349. irq_domain_remove(p->irq_domain);
  350. err0:
  351. return ret;
  352. }
  353. static int gpio_rcar_remove(struct platform_device *pdev)
  354. {
  355. struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
  356. int ret;
  357. ret = gpiochip_remove(&p->gpio_chip);
  358. if (ret)
  359. return ret;
  360. irq_domain_remove(p->irq_domain);
  361. return 0;
  362. }
  363. #ifdef CONFIG_OF
  364. static const struct of_device_id gpio_rcar_of_table[] = {
  365. {
  366. .compatible = "renesas,gpio-rcar",
  367. },
  368. { },
  369. };
  370. MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
  371. #endif
  372. static struct platform_driver gpio_rcar_device_driver = {
  373. .probe = gpio_rcar_probe,
  374. .remove = gpio_rcar_remove,
  375. .driver = {
  376. .name = "gpio_rcar",
  377. .of_match_table = of_match_ptr(gpio_rcar_of_table),
  378. }
  379. };
  380. module_platform_driver(gpio_rcar_device_driver);
  381. MODULE_AUTHOR("Magnus Damm");
  382. MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
  383. MODULE_LICENSE("GPL v2");