gpio-rcar.c 14 KB

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