gpio-rcar.c 13 KB

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