gpio-rcar.c 15 KB

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