gpio-rcar.c 15 KB

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