gpio-rcar.c 13 KB

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