gpio-rcar.c 13 KB

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