gpio-rcar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 void gpio_rcar_irq_bus_lock(struct irq_data *d)
  169. {
  170. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  171. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  172. pm_runtime_get_sync(&p->pdev->dev);
  173. }
  174. static void gpio_rcar_irq_bus_sync_unlock(struct irq_data *d)
  175. {
  176. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  177. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  178. pm_runtime_put(&p->pdev->dev);
  179. }
  180. static int gpio_rcar_irq_request_resources(struct irq_data *d)
  181. {
  182. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  183. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  184. int error;
  185. error = pm_runtime_get_sync(&p->pdev->dev);
  186. if (error < 0)
  187. return error;
  188. return 0;
  189. }
  190. static void gpio_rcar_irq_release_resources(struct irq_data *d)
  191. {
  192. struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
  193. struct gpio_rcar_priv *p = gpiochip_get_data(gc);
  194. pm_runtime_put(&p->pdev->dev);
  195. }
  196. static irqreturn_t gpio_rcar_irq_handler(int irq, void *dev_id)
  197. {
  198. struct gpio_rcar_priv *p = dev_id;
  199. u32 pending;
  200. unsigned int offset, irqs_handled = 0;
  201. while ((pending = gpio_rcar_read(p, INTDT) &
  202. gpio_rcar_read(p, INTMSK))) {
  203. offset = __ffs(pending);
  204. gpio_rcar_write(p, INTCLR, BIT(offset));
  205. generic_handle_irq(irq_find_mapping(p->gpio_chip.irqdomain,
  206. offset));
  207. irqs_handled++;
  208. }
  209. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  210. }
  211. static void gpio_rcar_config_general_input_output_mode(struct gpio_chip *chip,
  212. unsigned int gpio,
  213. bool output)
  214. {
  215. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  216. unsigned long flags;
  217. /* follow steps in the GPIO documentation for
  218. * "Setting General Output Mode" and
  219. * "Setting General Input Mode"
  220. */
  221. spin_lock_irqsave(&p->lock, flags);
  222. /* Configure postive logic in POSNEG */
  223. gpio_rcar_modify_bit(p, POSNEG, gpio, false);
  224. /* Select "General Input/Output Mode" in IOINTSEL */
  225. gpio_rcar_modify_bit(p, IOINTSEL, gpio, false);
  226. /* Select Input Mode or Output Mode in INOUTSEL */
  227. gpio_rcar_modify_bit(p, INOUTSEL, gpio, output);
  228. spin_unlock_irqrestore(&p->lock, flags);
  229. }
  230. static int gpio_rcar_request(struct gpio_chip *chip, unsigned offset)
  231. {
  232. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  233. int error;
  234. error = pm_runtime_get_sync(&p->pdev->dev);
  235. if (error < 0)
  236. return error;
  237. error = pinctrl_request_gpio(chip->base + offset);
  238. if (error)
  239. pm_runtime_put(&p->pdev->dev);
  240. return error;
  241. }
  242. static void gpio_rcar_free(struct gpio_chip *chip, unsigned offset)
  243. {
  244. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  245. pinctrl_free_gpio(chip->base + offset);
  246. /* Set the GPIO as an input to ensure that the next GPIO request won't
  247. * drive the GPIO pin as an output.
  248. */
  249. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  250. pm_runtime_put(&p->pdev->dev);
  251. }
  252. static int gpio_rcar_direction_input(struct gpio_chip *chip, unsigned offset)
  253. {
  254. gpio_rcar_config_general_input_output_mode(chip, offset, false);
  255. return 0;
  256. }
  257. static int gpio_rcar_get(struct gpio_chip *chip, unsigned offset)
  258. {
  259. u32 bit = BIT(offset);
  260. /* testing on r8a7790 shows that INDT does not show correct pin state
  261. * when configured as output, so use OUTDT in case of output pins */
  262. if (gpio_rcar_read(gpiochip_get_data(chip), INOUTSEL) & bit)
  263. return !!(gpio_rcar_read(gpiochip_get_data(chip), OUTDT) & bit);
  264. else
  265. return !!(gpio_rcar_read(gpiochip_get_data(chip), INDT) & bit);
  266. }
  267. static void gpio_rcar_set(struct gpio_chip *chip, unsigned offset, int value)
  268. {
  269. struct gpio_rcar_priv *p = gpiochip_get_data(chip);
  270. unsigned long flags;
  271. spin_lock_irqsave(&p->lock, flags);
  272. gpio_rcar_modify_bit(p, OUTDT, offset, value);
  273. spin_unlock_irqrestore(&p->lock, flags);
  274. }
  275. static int gpio_rcar_direction_output(struct gpio_chip *chip, unsigned offset,
  276. int value)
  277. {
  278. /* write GPIO value to output before selecting output mode of pin */
  279. gpio_rcar_set(chip, offset, value);
  280. gpio_rcar_config_general_input_output_mode(chip, offset, true);
  281. return 0;
  282. }
  283. struct gpio_rcar_info {
  284. bool has_both_edge_trigger;
  285. bool needs_clk;
  286. };
  287. static const struct gpio_rcar_info gpio_rcar_info_gen1 = {
  288. .has_both_edge_trigger = false,
  289. .needs_clk = false,
  290. };
  291. static const struct gpio_rcar_info gpio_rcar_info_gen2 = {
  292. .has_both_edge_trigger = true,
  293. .needs_clk = true,
  294. };
  295. static const struct of_device_id gpio_rcar_of_table[] = {
  296. {
  297. .compatible = "renesas,gpio-r8a7790",
  298. .data = &gpio_rcar_info_gen2,
  299. }, {
  300. .compatible = "renesas,gpio-r8a7791",
  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-rcar",
  314. .data = &gpio_rcar_info_gen1,
  315. }, {
  316. /* Terminator */
  317. },
  318. };
  319. MODULE_DEVICE_TABLE(of, gpio_rcar_of_table);
  320. static int gpio_rcar_parse_dt(struct gpio_rcar_priv *p, unsigned int *npins)
  321. {
  322. struct device_node *np = p->pdev->dev.of_node;
  323. const struct of_device_id *match;
  324. const struct gpio_rcar_info *info;
  325. struct of_phandle_args args;
  326. int ret;
  327. match = of_match_node(gpio_rcar_of_table, np);
  328. if (!match)
  329. return -EINVAL;
  330. info = match->data;
  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. p->needs_clk = info->needs_clk;
  335. if (*npins == 0 || *npins > RCAR_MAX_GPIO_PER_BANK) {
  336. dev_warn(&p->pdev->dev,
  337. "Invalid number of gpio lines %u, using %u\n", *npins,
  338. RCAR_MAX_GPIO_PER_BANK);
  339. *npins = RCAR_MAX_GPIO_PER_BANK;
  340. }
  341. return 0;
  342. }
  343. static int gpio_rcar_probe(struct platform_device *pdev)
  344. {
  345. struct gpio_rcar_priv *p;
  346. struct resource *io, *irq;
  347. struct gpio_chip *gpio_chip;
  348. struct irq_chip *irq_chip;
  349. struct device *dev = &pdev->dev;
  350. const char *name = dev_name(dev);
  351. unsigned int npins;
  352. int ret;
  353. p = devm_kzalloc(dev, sizeof(*p), GFP_KERNEL);
  354. if (!p)
  355. return -ENOMEM;
  356. p->pdev = pdev;
  357. spin_lock_init(&p->lock);
  358. /* Get device configuration from DT node */
  359. ret = gpio_rcar_parse_dt(p, &npins);
  360. if (ret < 0)
  361. return ret;
  362. platform_set_drvdata(pdev, p);
  363. p->clk = devm_clk_get(dev, NULL);
  364. if (IS_ERR(p->clk)) {
  365. if (p->needs_clk) {
  366. dev_err(dev, "unable to get clock\n");
  367. ret = PTR_ERR(p->clk);
  368. goto err0;
  369. }
  370. p->clk = NULL;
  371. }
  372. pm_runtime_enable(dev);
  373. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  374. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  375. if (!io || !irq) {
  376. dev_err(dev, "missing IRQ or IOMEM\n");
  377. ret = -EINVAL;
  378. goto err0;
  379. }
  380. p->base = devm_ioremap_nocache(dev, io->start, resource_size(io));
  381. if (!p->base) {
  382. dev_err(dev, "failed to remap I/O memory\n");
  383. ret = -ENXIO;
  384. goto err0;
  385. }
  386. gpio_chip = &p->gpio_chip;
  387. gpio_chip->request = gpio_rcar_request;
  388. gpio_chip->free = gpio_rcar_free;
  389. gpio_chip->direction_input = gpio_rcar_direction_input;
  390. gpio_chip->get = gpio_rcar_get;
  391. gpio_chip->direction_output = gpio_rcar_direction_output;
  392. gpio_chip->set = gpio_rcar_set;
  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->irq_mask = gpio_rcar_irq_disable;
  401. irq_chip->irq_unmask = gpio_rcar_irq_enable;
  402. irq_chip->irq_set_type = gpio_rcar_irq_set_type;
  403. irq_chip->irq_set_wake = gpio_rcar_irq_set_wake;
  404. irq_chip->irq_bus_lock = gpio_rcar_irq_bus_lock;
  405. irq_chip->irq_bus_sync_unlock = gpio_rcar_irq_bus_sync_unlock;
  406. irq_chip->irq_request_resources = gpio_rcar_irq_request_resources;
  407. irq_chip->irq_release_resources = gpio_rcar_irq_release_resources;
  408. irq_chip->flags = IRQCHIP_SET_TYPE_MASKED | IRQCHIP_MASK_ON_SUSPEND;
  409. ret = gpiochip_add_data(gpio_chip, p);
  410. if (ret) {
  411. dev_err(dev, "failed to add GPIO controller\n");
  412. goto err0;
  413. }
  414. ret = gpiochip_irqchip_add(gpio_chip, irq_chip, 0, handle_level_irq,
  415. IRQ_TYPE_NONE);
  416. if (ret) {
  417. dev_err(dev, "cannot add irqchip\n");
  418. goto err1;
  419. }
  420. p->irq_parent = irq->start;
  421. if (devm_request_irq(dev, irq->start, gpio_rcar_irq_handler,
  422. IRQF_SHARED, name, p)) {
  423. dev_err(dev, "failed to request IRQ\n");
  424. ret = -ENOENT;
  425. goto err1;
  426. }
  427. dev_info(dev, "driving %d GPIOs\n", npins);
  428. return 0;
  429. err1:
  430. gpiochip_remove(gpio_chip);
  431. err0:
  432. pm_runtime_disable(dev);
  433. return ret;
  434. }
  435. static int gpio_rcar_remove(struct platform_device *pdev)
  436. {
  437. struct gpio_rcar_priv *p = platform_get_drvdata(pdev);
  438. gpiochip_remove(&p->gpio_chip);
  439. pm_runtime_disable(&pdev->dev);
  440. return 0;
  441. }
  442. static struct platform_driver gpio_rcar_device_driver = {
  443. .probe = gpio_rcar_probe,
  444. .remove = gpio_rcar_remove,
  445. .driver = {
  446. .name = "gpio_rcar",
  447. .of_match_table = of_match_ptr(gpio_rcar_of_table),
  448. }
  449. };
  450. module_platform_driver(gpio_rcar_device_driver);
  451. MODULE_AUTHOR("Magnus Damm");
  452. MODULE_DESCRIPTION("Renesas R-Car GPIO Driver");
  453. MODULE_LICENSE("GPL v2");