gpio-rcar.c 14 KB

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