gpio-rcar.c 14 KB

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