gpio-rcar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Renesas R-Car GPIO Support
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  5. * Copyright (C) 2013 Magnus Damm
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/gpio.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/ioport.h>
  22. #include <linux/irq.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/pinctrl/consumer.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 platform_device *pdev;
  35. struct gpio_chip gpio_chip;
  36. struct irq_chip irq_chip;
  37. unsigned int irq_parent;
  38. atomic_t wakeup_path;
  39. bool has_both_edge_trigger;
  40. };
  41. #define IOINTSEL 0x00 /* General IO/Interrupt Switching Register */
  42. #define INOUTSEL 0x04 /* General Input/Output Switching Register */
  43. #define OUTDT 0x08 /* General Output Register */
  44. #define INDT 0x0c /* General Input Register */
  45. #define INTDT 0x10 /* Interrupt Display Register */
  46. #define INTCLR 0x14 /* Interrupt Clear Register */
  47. #define INTMSK 0x18 /* Interrupt Mask Register */
  48. #define MSKCLR 0x1c /* Interrupt Mask Clear Register */
  49. #define POSNEG 0x20 /* Positive/Negative Logic Select Register */
  50. #define EDGLEVEL 0x24 /* Edge/level Select Register */
  51. #define FILONOFF 0x28 /* Chattering Prevention On/Off Register */
  52. #define BOTHEDGE 0x4c /* One Edge/Both Edge Select Register */
  53. #define RCAR_MAX_GPIO_PER_BANK 32
  54. static inline u32 gpio_rcar_read(struct gpio_rcar_priv *p, int offs)
  55. {
  56. return ioread32(p->base + offs);
  57. }
  58. static inline void gpio_rcar_write(struct gpio_rcar_priv *p, int offs,
  59. u32 value)
  60. {
  61. iowrite32(value, p->base + offs);
  62. }
  63. static void gpio_rcar_modify_bit(struct gpio_rcar_priv *p, int offs,
  64. int bit, bool value)
  65. {
  66. u32 tmp = gpio_rcar_read(p, offs);
  67. if (value)
  68. tmp |= BIT(bit);
  69. else
  70. tmp &= ~BIT(bit);
  71. gpio_rcar_write(p, offs, tmp);
  72. }
  73. static void gpio_rcar_irq_disable(struct irq_data *d)
  74. {
  75. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  76. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  77. gpio_rcar_write(p, INTMSK, ~BIT(irqd_to_hwirq(d)));
  78. }
  79. static void gpio_rcar_irq_enable(struct irq_data *d)
  80. {
  81. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  82. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  83. gpio_rcar_write(p, MSKCLR, BIT(irqd_to_hwirq(d)));
  84. }
  85. static void gpio_rcar_config_interrupt_input_mode(struct gpio_rcar_priv *p,
  86. unsigned int hwirq,
  87. bool active_high_rising_edge,
  88. bool level_trigger,
  89. bool both)
  90. {
  91. unsigned long flags;
  92. /* follow steps in the GPIO documentation for
  93. * "Setting Edge-Sensitive Interrupt Input Mode" and
  94. * "Setting Level-Sensitive Interrupt Input Mode"
  95. */
  96. spin_lock_irqsave(&p->lock, flags);
  97. /* Configure postive or negative logic in POSNEG */
  98. gpio_rcar_modify_bit(p, POSNEG, hwirq, !active_high_rising_edge);
  99. /* Configure edge or level trigger in EDGLEVEL */
  100. gpio_rcar_modify_bit(p, EDGLEVEL, hwirq, !level_trigger);
  101. /* Select one edge or both edges in BOTHEDGE */
  102. if (p->has_both_edge_trigger)
  103. gpio_rcar_modify_bit(p, BOTHEDGE, hwirq, both);
  104. /* Select "Interrupt Input Mode" in IOINTSEL */
  105. gpio_rcar_modify_bit(p, IOINTSEL, hwirq, true);
  106. /* Write INTCLR in case of edge trigger */
  107. if (!level_trigger)
  108. gpio_rcar_write(p, INTCLR, BIT(hwirq));
  109. spin_unlock_irqrestore(&p->lock, flags);
  110. }
  111. static int gpio_rcar_irq_set_type(struct irq_data *d, unsigned int type)
  112. {
  113. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  114. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  115. unsigned int hwirq = irqd_to_hwirq(d);
  116. dev_dbg(&p->pdev->dev, "sense irq = %d, type = %d\n", hwirq, type);
  117. switch (type & IRQ_TYPE_SENSE_MASK) {
  118. case IRQ_TYPE_LEVEL_HIGH:
  119. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, true,
  120. false);
  121. break;
  122. case IRQ_TYPE_LEVEL_LOW:
  123. gpio_rcar_config_interrupt_input_mode(p, hwirq, false, true,
  124. false);
  125. break;
  126. case IRQ_TYPE_EDGE_RISING:
  127. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
  128. false);
  129. break;
  130. case IRQ_TYPE_EDGE_FALLING:
  131. gpio_rcar_config_interrupt_input_mode(p, hwirq, false, false,
  132. false);
  133. break;
  134. case IRQ_TYPE_EDGE_BOTH:
  135. if (!p->has_both_edge_trigger)
  136. return -EINVAL;
  137. gpio_rcar_config_interrupt_input_mode(p, hwirq, true, false,
  138. true);
  139. break;
  140. default:
  141. return -EINVAL;
  142. }
  143. return 0;
  144. }
  145. static int gpio_rcar_irq_set_wake(struct irq_data *d, unsigned int on)
  146. {
  147. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  148. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  149. int error;
  150. if (p->irq_parent) {
  151. error = irq_set_irq_wake(p->irq_parent, on);
  152. if (error) {
  153. dev_dbg(&p->pdev->dev,
  154. "irq %u doesn't support irq_set_wake\n",
  155. p->irq_parent);
  156. p->irq_parent = 0;
  157. }
  158. }
  159. if (on)
  160. atomic_inc(&p->wakeup_path);
  161. else
  162. atomic_dec(&p->wakeup_path);
  163. return 0;
  164. }
  165. static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
  166. {
  167. struct gpio_rcar_priv *p = dev_id;
  168. u32 pending;
  169. unsigned int offset, irqs_handled = 0;
  170. while ((pending = gpio_rcar_read(p, INTDT) &
  171. gpio_rcar_read(p, INTMSK))) {
  172. offset = __ffs(pending);
  173. gpio_rcar_write(p, INTCLR, BIT(offset));
  174. generic_handle_irq(irq_find_mapping(p->gpio_chip.irq.domain,
  175. offset));
  176. irqs_handled++;
  177. }
  178. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  179. }
  180. static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
  181. unsigned int gpio,
  182. bool output)
  183. {
  184. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  185. unsigned long flags;
  186. /* follow steps in the GPIO documentation for
  187. * "Setting General Output Mode" and
  188. * "Setting General Input Mode"
  189. */
  190. spin_lock_irqsave(&p->lock, flags);
  191. /* Configure postive logic in POSNEG */
  192. gpio_rcar_modify_bit(p, POSNEG, gpio, false);
  193. /* Select "General Input/Output Mode" in IOINTSEL */
  194. gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
  195. /* Select Input Mode or Output Mode in INOUTSEL */
  196. gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
  197. spin_unlock_irqrestore(&p->lock, flags);
  198. }
  199. static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
  200. {
  201. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  202. int error;
  203. error = pm_runtime_get_sync(&p->pdev->dev);
  204. if (error < 0)
  205. return error;
  206. error = pinctrl_gpio_request(chip->base + offset);
  207. if (error)
  208. pm_runtime_put(&p->pdev->dev);
  209. return error;
  210. }
  211. static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
  212. {
  213. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  214. pinctrl_gpio_free(chip->base + offset);
  215. /*
  216. * Set the GPIO as an input to ensure that the next GPIO request won't
  217. * drive the GPIO pin as an output.
  218. */
  219. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  220. pm_runtime_put(&p->pdev->dev);
  221. }
  222. static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
  223. {
  224. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  225. return 0;
  226. }
  227. static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
  228. {
  229. u32 bit = BIT(offset);
  230. /* testing on r8a7790 shows that INDT does not show correct pin state
  231. * when configured as output, so use OUTDT in case of output pins */
  232. if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
  233. return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
  234. else
  235. return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
  236. }
  237. static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
  238. {
  239. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  240. unsigned long flags;
  241. spin_lock_irqsave(&p->lock, flags);
  242. gpio_rcar_modify_bit(p, OUTDT, offset, value);
  243. spin_unlock_irqrestore(&p->lock, flags);
  244. }
  245. static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
  246. unsigned long *bits)
  247. {
  248. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  249. unsigned long flags;
  250. u32 val, bankmask;
  251. bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
  252. if (!bankmask)
  253. return;
  254. spin_lock_irqsave(&p->lock, flags);
  255. val = gpio_rcar_read(p, OUTDT);
  256. val &= ~bankmask;
  257. val |= (bankmask & bits[0]);
  258. gpio_rcar_write(p, OUTDT, val);
  259. spin_unlock_irqrestore(&p->lock, flags);
  260. }
  261. static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
  262. int value)
  263. {
  264. /* write GPIO value to output before selecting output mode of pin */
  265. gpio_rcar_set(chip, offset, value);
  266. gpio_rcar_config_general_input_output_mode(chip, offset, true);
  267. return 0;
  268. }
  269. struct gpio_rcar_info {
  270. bool has_both_edge_trigger;
  271. };
  272. static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
  273. .has_both_edge_trigger = false,
  274. };
  275. static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
  276. .has_both_edge_trigger = true,
  277. };
  278. static const struct of_device_id gpio_rcar_of_table[] = {
  279. {
  280. .compatible = "renesas,gpio-r8a7743",
  281. /* RZ/G1 GPIO is identical to R-Car Gen2. */
  282. .data = &gpio_rcar_info_gen2,
  283. }, {
  284. .compatible = "renesas,gpio-r8a7790",
  285. .data = &gpio_rcar_info_gen2,
  286. }, {
  287. .compatible = "renesas,gpio-r8a7791",
  288. .data = &gpio_rcar_info_gen2,
  289. }, {
  290. .compatible = "renesas,gpio-r8a7792",
  291. .data = &gpio_rcar_info_gen2,
  292. }, {
  293. .compatible = "renesas,gpio-r8a7793",
  294. .data = &gpio_rcar_info_gen2,
  295. }, {
  296. .compatible = "renesas,gpio-r8a7794",
  297. .data = &gpio_rcar_info_gen2,
  298. }, {
  299. .compatible = "renesas,gpio-r8a7795",
  300. /* Gen3 GPIO is identical to Gen2. */
  301. .data = &gpio_rcar_info_gen2,
  302. }, {
  303. .compatible = "renesas,gpio-r8a7796",
  304. /* Gen3 GPIO is identical to Gen2. */
  305. .data = &gpio_rcar_info_gen2,
  306. }, {
  307. .compatible = "renesas,rcar-gen1-gpio",
  308. .data = &gpio_rcar_info_gen1,
  309. }, {
  310. .compatible = "renesas,rcar-gen2-gpio",
  311. .data = &gpio_rcar_info_gen2,
  312. }, {
  313. .compatible = "renesas,rcar-gen3-gpio",
  314. /* Gen3 GPIO is identical to Gen2. */
  315. .data = &gpio_rcar_info_gen2,
  316. }, {
  317. .compatible = "renesas,gpio-rcar",
  318. .data = &gpio_rcar_info_gen1,
  319. }, {
  320. /* Terminator */
  321. },
  322. };
  323. MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
  324. static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
  325. {
  326. struct device_node *np = p->pdev->dev.of_node;
  327. const struct gpio_rcar_info *info;
  328. struct of_phandle_args args;
  329. int ret;
  330. info = of_device_get_match_data(&p->pdev->dev);
  331. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, 0, &args);
  332. *npins = ret == 0 ? args.args[2] : RCAR_MAX_GPIO_PER_BANK;
  333. p->has_both_edge_trigger = info->has_both_edge_trigger;
  334. if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
  335. dev_warn(&p->pdev->dev,
  336. "Invalid number of gpio lines %u, using %u\n", *npins,
  337. RCAR_MAX_GPIO_PER_BANK);
  338. *npins = RCAR_MAX_GPIO_PER_BANK;
  339. }
  340. return 0;
  341. }
  342. static int gpio_rcar_probe(struct platform_device *pdev)
  343. {
  344. struct gpio_rcar_priv *p;
  345. struct resource *io, *irq;
  346. struct gpio_chip *gpio_chip;
  347. struct irq_chip *irq_chip;
  348. struct device *dev = &pdev->dev;
  349. const char *name = dev_name(dev);
  350. unsigned int npins;
  351. int ret;
  352. p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
  353. if (!p)
  354. return -ENOMEM;
  355. p->pdev = pdev;
  356. spin_lock_init(&p->lock);
  357. /* Get device configuration from DT node */
  358. ret = gpio_rcar_parse_dt(p, &npins);
  359. if (ret < 0)
  360. return ret;
  361. platform_set_drvdata(pdev, p);
  362. pm_runtime_enable(dev);
  363. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  364. if (!irq) {
  365. dev_err(dev, "missing IRQ\n");
  366. ret = -EINVAL;
  367. goto err0;
  368. }
  369. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  370. p->base = devm_ioremap_resource(dev, io);
  371. if (IS_ERR(p->base)) {
  372. ret = PTR_ERR(p->base);
  373. goto err0;
  374. }
  375. gpio_chip = &p->gpio_chip;
  376. gpio_chip->request = gpio_rcar_request;
  377. gpio_chip->free = gpio_rcar_free;
  378. gpio_chip->direction_input = gpio_rcar_direction_input;
  379. gpio_chip->get = gpio_rcar_get;
  380. gpio_chip->direction_output = gpio_rcar_direction_output;
  381. gpio_chip->set = gpio_rcar_set;
  382. gpio_chip->set_multiple = gpio_rcar_set_multiple;
  383. gpio_chip->label = name;
  384. gpio_chip->parent = dev;
  385. gpio_chip->owner = THIS_MODULE;
  386. gpio_chip->base = -1;
  387. gpio_chip->ngpio = npins;
  388. irq_chip = &p->irq_chip;
  389. irq_chip->name = name;
  390. irq_chip->parent_device = dev;
  391. irq_chip->irq_mask = gpio_rcar_irq_disable;
  392. irq_chip->irq_unmask = gpio_rcar_irq_enable;
  393. irq_chip->irq_set_type = gpio_rcar_irq_set_type;
  394. irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
  395. irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
  396. ret = gpiochip_add_data(gpio_chip, p);
  397. if (ret) {
  398. dev_err(dev, "failed to add GPIO controller\n");
  399. goto err0;
  400. }
  401. ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
  402. IRQ_TYPE_NONE);
  403. if (ret) {
  404. dev_err(dev, "cannot add irqchip\n");
  405. goto err1;
  406. }
  407. p->irq_parent = irq->start;
  408. if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
  409. IRQF_SHARED, name, p)) {
  410. dev_err(dev, "failed to request IRQ\n");
  411. ret = -ENOENT;
  412. goto err1;
  413. }
  414. dev_info(dev, "driving %d GPIOs\n", npins);
  415. return 0;
  416. err1:
  417. gpiochip_remove(gpio_chip);
  418. err0:
  419. pm_runtime_disable(dev);
  420. return ret;
  421. }
  422. static int gpio_rcar_remove(struct platform_device *pdev)
  423. {
  424. struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
  425. gpiochip_remove(&p->gpio_chip);
  426. pm_runtime_disable(&pdev->dev);
  427. return 0;
  428. }
  429. static int __maybe_unused gpio_rcar_suspend(struct device *dev)
  430. {
  431. struct gpio_rcar_priv *p = dev_get_drvdata(dev);
  432. if (atomic_read(&p->wakeup_path))
  433. device_set_wakeup_path(dev);
  434. return 0;
  435. }
  436. static SIMPLE_DEV_PM_OPS(gpio_rcar_pm_ops, gpio_rcar_suspend, NULL);
  437. static struct platform_driver gpio_rcar_device_driver = {
  438. .probe = gpio_rcar_probe,
  439. .remove = gpio_rcar_remove,
  440. .driver = {
  441. .name = "gpio_rcar",
  442. .pm = &gpio_rcar_pm_ops,
  443. .of_match_table = of_match_ptr(gpio_rcar_of_table),
  444. }
  445. };
  446. module_platform_driver(gpio_rcar_device_driver);
  447. MODULE_AUTHOR("Magnus Damm");
  448. MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
  449. MODULE_LICENSE("GPL v2");