gpio-rcar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  205. int error;
  206. error = pm_runtime_get_sync(&p->pdev->dev);
  207. if (error < 0)
  208. return error;
  209. error = pinctrl_request_gpio(chip->base + offset);
  210. if (error)
  211. pm_runtime_put(&p->pdev->dev);
  212. return error;
  213. }
  214. static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
  215. {
  216. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  217. pinctrl_free_gpio(chip->base + offset);
  218. /*
  219. * Set the GPIO as an input to ensure that the next GPIO request won't
  220. * drive the GPIO pin as an output.
  221. */
  222. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  223. pm_runtime_put(&p->pdev->dev);
  224. }
  225. static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
  226. {
  227. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  228. return 0;
  229. }
  230. static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
  231. {
  232. u32 bit = BIT(offset);
  233. /* testing on r8a7790 shows that INDT does not show correct pin state
  234. * when configured as output, so use OUTDT in case of output pins */
  235. if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
  236. return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
  237. else
  238. return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
  239. }
  240. static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
  241. {
  242. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  243. unsigned long flags;
  244. spin_lock_irqsave(&p->lock, flags);
  245. gpio_rcar_modify_bit(p, OUTDT, offset, value);
  246. spin_unlock_irqrestore(&p->lock, flags);
  247. }
  248. static void gpio_rcar_set_multiple(struct gpio_chip *chip, unsigned long *mask,
  249. unsigned long *bits)
  250. {
  251. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  252. unsigned long flags;
  253. u32 val, bankmask;
  254. bankmask = mask[0] & GENMASK(chip->ngpio - 1, 0);
  255. if (!bankmask)
  256. return;
  257. spin_lock_irqsave(&p->lock, flags);
  258. val = gpio_rcar_read(p, OUTDT);
  259. val &= ~bankmask;
  260. val |= (bankmask & bits[0]);
  261. gpio_rcar_write(p, OUTDT, val);
  262. spin_unlock_irqrestore(&p->lock, flags);
  263. }
  264. static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
  265. int value)
  266. {
  267. /* write GPIO value to output before selecting output mode of pin */
  268. gpio_rcar_set(chip, offset, value);
  269. gpio_rcar_config_general_input_output_mode(chip, offset, true);
  270. return 0;
  271. }
  272. struct gpio_rcar_info {
  273. bool has_both_edge_trigger;
  274. bool needs_clk;
  275. };
  276. static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
  277. .has_both_edge_trigger = false,
  278. .needs_clk = false,
  279. };
  280. static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
  281. .has_both_edge_trigger = true,
  282. .needs_clk = true,
  283. };
  284. static const struct of_device_id gpio_rcar_of_table[] = {
  285. {
  286. .compatible = "renesas,gpio-r8a7743",
  287. /* RZ/G1 GPIO is identical to R-Car Gen2. */
  288. .data = &gpio_rcar_info_gen2,
  289. }, {
  290. .compatible = "renesas,gpio-r8a7790",
  291. .data = &gpio_rcar_info_gen2,
  292. }, {
  293. .compatible = "renesas,gpio-r8a7791",
  294. .data = &gpio_rcar_info_gen2,
  295. }, {
  296. .compatible = "renesas,gpio-r8a7792",
  297. .data = &gpio_rcar_info_gen2,
  298. }, {
  299. .compatible = "renesas,gpio-r8a7793",
  300. .data = &gpio_rcar_info_gen2,
  301. }, {
  302. .compatible = "renesas,gpio-r8a7794",
  303. .data = &gpio_rcar_info_gen2,
  304. }, {
  305. .compatible = "renesas,gpio-r8a7795",
  306. /* Gen3 GPIO is identical to Gen2. */
  307. .data = &gpio_rcar_info_gen2,
  308. }, {
  309. .compatible = "renesas,gpio-r8a7796",
  310. /* Gen3 GPIO is identical to Gen2. */
  311. .data = &gpio_rcar_info_gen2,
  312. }, {
  313. .compatible = "renesas,rcar-gen1-gpio",
  314. .data = &gpio_rcar_info_gen1,
  315. }, {
  316. .compatible = "renesas,rcar-gen2-gpio",
  317. .data = &gpio_rcar_info_gen2,
  318. }, {
  319. .compatible = "renesas,rcar-gen3-gpio",
  320. /* Gen3 GPIO is identical to Gen2. */
  321. .data = &gpio_rcar_info_gen2,
  322. }, {
  323. .compatible = "renesas,gpio-rcar",
  324. .data = &gpio_rcar_info_gen1,
  325. }, {
  326. /* Terminator */
  327. },
  328. };
  329. MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
  330. static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
  331. {
  332. struct device_node *np = p->pdev->dev.of_node;
  333. const struct of_device_id *match;
  334. const struct gpio_rcar_info *info;
  335. struct of_phandle_args args;
  336. int ret;
  337. match = of_match_node(gpio_rcar_of_table, np);
  338. if (!match)
  339. return -EINVAL;
  340. info = match->data;
  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. p->needs_clk = info->needs_clk;
  345. if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
  346. dev_warn(&p->pdev->dev,
  347. "Invalid number of gpio lines %u, using %u\n", *npins,
  348. RCAR_MAX_GPIO_PER_BANK);
  349. *npins = RCAR_MAX_GPIO_PER_BANK;
  350. }
  351. return 0;
  352. }
  353. static int gpio_rcar_probe(struct platform_device *pdev)
  354. {
  355. struct gpio_rcar_priv *p;
  356. struct resource *io, *irq;
  357. struct gpio_chip *gpio_chip;
  358. struct irq_chip *irq_chip;
  359. struct device *dev = &pdev->dev;
  360. const char *name = dev_name(dev);
  361. unsigned int npins;
  362. int ret;
  363. p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
  364. if (!p)
  365. return -ENOMEM;
  366. p->pdev = pdev;
  367. spin_lock_init(&p->lock);
  368. /* Get device configuration from DT node */
  369. ret = gpio_rcar_parse_dt(p, &npins);
  370. if (ret < 0)
  371. return ret;
  372. platform_set_drvdata(pdev, p);
  373. p->clk = devm_clk_get(dev, NULL);
  374. if (IS_ERR(p->clk)) {
  375. if (p->needs_clk) {
  376. dev_err(dev, "unable to get clock\n");
  377. ret = PTR_ERR(p->clk);
  378. goto err0;
  379. }
  380. p->clk = NULL;
  381. }
  382. pm_runtime_enable(dev);
  383. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  384. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  385. if (!io || !irq) {
  386. dev_err(dev, "missing IRQ or IOMEM\n");
  387. ret = -EINVAL;
  388. goto err0;
  389. }
  390. p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
  391. if (!p->base) {
  392. dev_err(dev, "failed to remap I/O memory\n");
  393. ret = -ENXIO;
  394. goto err0;
  395. }
  396. gpio_chip = &p->gpio_chip;
  397. gpio_chip->request = gpio_rcar_request;
  398. gpio_chip->free = gpio_rcar_free;
  399. gpio_chip->direction_input = gpio_rcar_direction_input;
  400. gpio_chip->get = gpio_rcar_get;
  401. gpio_chip->direction_output = gpio_rcar_direction_output;
  402. gpio_chip->set = gpio_rcar_set;
  403. gpio_chip->set_multiple = gpio_rcar_set_multiple;
  404. gpio_chip->label = name;
  405. gpio_chip->parent = dev;
  406. gpio_chip->owner = THIS_MODULE;
  407. gpio_chip->base = -1;
  408. gpio_chip->ngpio = npins;
  409. irq_chip = &p->irq_chip;
  410. irq_chip->name = name;
  411. irq_chip->parent_device = dev;
  412. irq_chip->irq_mask = gpio_rcar_irq_disable;
  413. irq_chip->irq_unmask = gpio_rcar_irq_enable;
  414. irq_chip->irq_set_type = gpio_rcar_irq_set_type;
  415. irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
  416. irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
  417. ret = gpiochip_add_data(gpio_chip, p);
  418. if (ret) {
  419. dev_err(dev, "failed to add GPIO controller\n");
  420. goto err0;
  421. }
  422. ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
  423. IRQ_TYPE_NONE);
  424. if (ret) {
  425. dev_err(dev, "cannot add irqchip\n");
  426. goto err1;
  427. }
  428. p->irq_parent = irq->start;
  429. if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
  430. IRQF_SHARED, name, p)) {
  431. dev_err(dev, "failed to request IRQ\n");
  432. ret = -ENOENT;
  433. goto err1;
  434. }
  435. dev_info(dev, "driving %d GPIOs\n", npins);
  436. return 0;
  437. err1:
  438. gpiochip_remove(gpio_chip);
  439. err0:
  440. pm_runtime_disable(dev);
  441. return ret;
  442. }
  443. static int gpio_rcar_remove(struct platform_device *pdev)
  444. {
  445. struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
  446. gpiochip_remove(&p->gpio_chip);
  447. pm_runtime_disable(&pdev->dev);
  448. return 0;
  449. }
  450. static struct platform_driver gpio_rcar_device_driver = {
  451. .probe = gpio_rcar_probe,
  452. .remove = gpio_rcar_remove,
  453. .driver = {
  454. .name = "gpio_rcar",
  455. .of_match_table = of_match_ptr(gpio_rcar_of_table),
  456. }
  457. };
  458. module_platform_driver(gpio_rcar_device_driver);
  459. MODULE_AUTHOR("Magnus Damm");
  460. MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
  461. MODULE_LICENSE("GPL v2");