gpio-rcar.c 13 KB

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