gpio-rcar.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. gpio_rcar_read(p, INTMSK))) {
  147. offset = __ffs(pending);
  148. gpio_rcar_write(p, INTCLR, BIT(offset));
  149. generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
  150. irqs_handled++;
  151. }
  152. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  153. }
  154. static inline struct gpio_rcar_priv *gpio_to_priv(struct gpio_chip *chip)
  155. {
  156. return container_of(chip, struct gpio_rcar_priv, gpio_chip);
  157. }
  158. static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
  159. unsigned int gpio,
  160. bool output)
  161. {
  162. struct gpio_rcar_priv *p = gpio_to_priv(chip);
  163. unsigned long flags;
  164. /* follow steps in the GPIO documentation for
  165. * "Setting General Output Mode" and
  166. * "Setting General Input Mode"
  167. */
  168. spin_lock_irqsave(&p->lock, flags);
  169. /* Configure postive logic in POSNEG */
  170. gpio_rcar_modify_bit(p, POSNEG, gpio, false);
  171. /* Select "General Input/Output Mode" in IOINTSEL */
  172. gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
  173. /* Select Input Mode or Output Mode in INOUTSEL */
  174. gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
  175. spin_unlock_irqrestore(&p->lock, flags);
  176. }
  177. static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
  178. {
  179. return pinctrl_request_gpio(chip->base + offset);
  180. }
  181. static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
  182. {
  183. pinctrl_free_gpio(chip->base + offset);
  184. /* Set the GPIO as an input to ensure that the next GPIO request won't
  185. * drive the GPIO pin as an output.
  186. */
  187. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  188. }
  189. static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
  190. {
  191. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  192. return 0;
  193. }
  194. static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
  195. {
  196. u32 bit = BIT(offset);
  197. /* testing on r8a7790 shows that INDT does not show correct pin state
  198. * when configured as output, so use OUTDT in case of output pins */
  199. if (gpio_rcar_read(gpio_to_priv(chip), INOUTSEL) & bit)
  200. return (int)(gpio_rcar_read(gpio_to_priv(chip), OUTDT) & bit);
  201. else
  202. return (int)(gpio_rcar_read(gpio_to_priv(chip), INDT) & bit);
  203. }
  204. static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
  205. {
  206. struct gpio_rcar_priv *p = gpio_to_priv(chip);
  207. unsigned long flags;
  208. spin_lock_irqsave(&p->lock, flags);
  209. gpio_rcar_modify_bit(p, OUTDT, offset, value);
  210. spin_unlock_irqrestore(&p->lock, flags);
  211. }
  212. static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
  213. int value)
  214. {
  215. /* write GPIO value to output before selecting output mode of pin */
  216. gpio_rcar_set(chip, offset, value);
  217. gpio_rcar_config_general_input_output_mode(chip, offset, true);
  218. return 0;
  219. }
  220. static int gpio_rcar_to_irq(struct gpio_chip *chip, unsigned offset)
  221. {
  222. return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
  223. }
  224. static int gpio_rcar_irq_domain_map(struct irq_domain *h, unsigned int irq,
  225. irq_hw_number_t hwirq)
  226. {
  227. struct gpio_rcar_priv *p = h->host_data;
  228. dev_dbg(&p->pdev->dev, "map hw irq = %d, irq = %d\n", (int)hwirq, irq);
  229. irq_set_chip_data(irq, h->host_data);
  230. irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
  231. set_irq_flags(irq, IRQF_VALID); /* kill me now */
  232. return 0;
  233. }
  234. static struct irq_domain_ops gpio_rcar_irq_domain_ops = {
  235. .map = gpio_rcar_irq_domain_map,
  236. };
  237. struct gpio_rcar_info {
  238. bool has_both_edge_trigger;
  239. };
  240. static const struct of_device_id gpio_rcar_of_table[] = {
  241. {
  242. .compatible = "renesas,gpio-r8a7790",
  243. .data = (void *)&(const struct gpio_rcar_info) {
  244. .has_both_edge_trigger = true,
  245. },
  246. }, {
  247. .compatible = "renesas,gpio-r8a7791",
  248. .data = (void *)&(const struct gpio_rcar_info) {
  249. .has_both_edge_trigger = true,
  250. },
  251. }, {
  252. .compatible = "renesas,gpio-rcar",
  253. .data = (void *)&(const struct gpio_rcar_info) {
  254. .has_both_edge_trigger = false,
  255. },
  256. }, {
  257. /* Terminator */
  258. },
  259. };
  260. MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
  261. static int gpio_rcar_parse_pdata(struct gpio_rcar_priv *p)
  262. {
  263. struct gpio_rcar_config *pdata = dev_get_platdata(&p->pdev->dev);
  264. struct device_node *np = p->pdev->dev.of_node;
  265. struct of_phandle_args args;
  266. int ret;
  267. if (pdata) {
  268. p->config = *pdata;
  269. } else if (IS_ENABLED(CONFIG_OF) && np) {
  270. const struct of_device_id *match;
  271. const struct gpio_rcar_info *info;
  272. match = of_match_node(gpio_rcar_of_table, np);
  273. if (!match)
  274. return -EINVAL;
  275. info = match->data;
  276. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0,
  277. &args);
  278. p->config.number_of_pins = ret == 0 ? args.args[2]
  279. : RCAR_MAX_GPIO_PER_BANK;
  280. p->config.gpio_base = -1;
  281. p->config.has_both_edge_trigger = info->has_both_edge_trigger;
  282. }
  283. if (p->config.number_of_pins == 0 ||
  284. p->config.number_of_pins > RCAR_MAX_GPIO_PER_BANK) {
  285. dev_warn(&p->pdev->dev,
  286. "Invalid number of gpio lines %u, using %u\n",
  287. p->config.number_of_pins, RCAR_MAX_GPIO_PER_BANK);
  288. p->config.number_of_pins = RCAR_MAX_GPIO_PER_BANK;
  289. }
  290. return 0;
  291. }
  292. static int gpio_rcar_probe(struct platform_device *pdev)
  293. {
  294. struct gpio_rcar_priv *p;
  295. struct resource *io, *irq;
  296. struct gpio_chip *gpio_chip;
  297. struct irq_chip *irq_chip;
  298. const char *name = dev_name(&pdev->dev);
  299. int ret;
  300. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  301. if (!p) {
  302. dev_err(&pdev->dev, "failed to allocate driver data\n");
  303. ret = -ENOMEM;
  304. goto err0;
  305. }
  306. p->pdev = pdev;
  307. spin_lock_init(&p->lock);
  308. /* Get device configuration from DT node or platform data. */
  309. ret = gpio_rcar_parse_pdata(p);
  310. if (ret < 0)
  311. return ret;
  312. platform_set_drvdata(pdev, p);
  313. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  314. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  315. if (!io || !irq) {
  316. dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
  317. ret = -EINVAL;
  318. goto err0;
  319. }
  320. p->base = devm_ioremap_nocache(&pdev->dev, io->start,
  321. resource_size(io));
  322. if (!p->base) {
  323. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  324. ret = -ENXIO;
  325. goto err0;
  326. }
  327. gpio_chip = &p->gpio_chip;
  328. gpio_chip->request = gpio_rcar_request;
  329. gpio_chip->free = gpio_rcar_free;
  330. gpio_chip->direction_input = gpio_rcar_direction_input;
  331. gpio_chip->get = gpio_rcar_get;
  332. gpio_chip->direction_output = gpio_rcar_direction_output;
  333. gpio_chip->set = gpio_rcar_set;
  334. gpio_chip->to_irq = gpio_rcar_to_irq;
  335. gpio_chip->label = name;
  336. gpio_chip->dev = &pdev->dev;
  337. gpio_chip->owner = THIS_MODULE;
  338. gpio_chip->base = p->config.gpio_base;
  339. gpio_chip->ngpio = p->config.number_of_pins;
  340. irq_chip = &p->irq_chip;
  341. irq_chip->name = name;
  342. irq_chip->irq_mask = gpio_rcar_irq_disable;
  343. irq_chip->irq_unmask = gpio_rcar_irq_enable;
  344. irq_chip->irq_set_type = gpio_rcar_irq_set_type;
  345. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_SET_TYPE_MASKED
  346. | IRQCHIP_MASK_ON_SUSPEND;
  347. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  348. p->config.number_of_pins,
  349. p->config.irq_base,
  350. &gpio_rcar_irq_domain_ops, p);
  351. if (!p->irq_domain) {
  352. ret = -ENXIO;
  353. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  354. goto err0;
  355. }
  356. if (devm_request_irq(&pdev->dev, irq->start,
  357. gpio_rcar_irq_handler, IRQF_SHARED, name, p)) {
  358. dev_err(&pdev->dev, "failed to request IRQ\n");
  359. ret = -ENOENT;
  360. goto err1;
  361. }
  362. ret = gpiochip_add(gpio_chip);
  363. if (ret) {
  364. dev_err(&pdev->dev, "failed to add GPIO controller\n");
  365. goto err1;
  366. }
  367. dev_info(&pdev->dev, "driving %d GPIOs\n", p->config.number_of_pins);
  368. /* warn in case of mismatch if irq base is specified */
  369. if (p->config.irq_base) {
  370. ret = irq_find_mapping(p->irq_domain, 0);
  371. if (p->config.irq_base != ret)
  372. dev_warn(&pdev->dev, "irq base mismatch (%u/%u)\n",
  373. p->config.irq_base, ret);
  374. }
  375. if (p->config.pctl_name) {
  376. ret = gpiochip_add_pin_range(gpio_chip, p->config.pctl_name, 0,
  377. gpio_chip->base, gpio_chip->ngpio);
  378. if (ret < 0)
  379. dev_warn(&pdev->dev, "failed to add pin range\n");
  380. }
  381. return 0;
  382. err1:
  383. irq_domain_remove(p->irq_domain);
  384. err0:
  385. return ret;
  386. }
  387. static int gpio_rcar_remove(struct platform_device *pdev)
  388. {
  389. struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
  390. int ret;
  391. ret = gpiochip_remove(&p->gpio_chip);
  392. if (ret)
  393. return ret;
  394. irq_domain_remove(p->irq_domain);
  395. return 0;
  396. }
  397. static struct platform_driver gpio_rcar_device_driver = {
  398. .probe = gpio_rcar_probe,
  399. .remove = gpio_rcar_remove,
  400. .driver = {
  401. .name = "gpio_rcar",
  402. .of_match_table = of_match_ptr(gpio_rcar_of_table),
  403. }
  404. };
  405. module_platform_driver(gpio_rcar_device_driver);
  406. MODULE_AUTHOR("Magnus Damm");
  407. MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
  408. MODULE_LICENSE("GPL v2");