gpio-mb86s7x.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * linux/drivers/gpio/gpio-mb86s7x.c
  3. *
  4. * Copyright (C) 2015 Fujitsu Semiconductor Limited
  5. * Copyright (C) 2015 Linaro Ltd.
  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, 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/io.h>
  17. #include <linux/init.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/errno.h>
  21. #include <linux/ioport.h>
  22. #include <linux/of_device.h>
  23. #include <linux/gpio/driver.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/slab.h>
  27. /*
  28. * Only first 8bits of a register correspond to each pin,
  29. * so there are 4 registers for 32 pins.
  30. */
  31. #define PDR(x) (0x0 + x / 8 * 4)
  32. #define DDR(x) (0x10 + x / 8 * 4)
  33. #define PFR(x) (0x20 + x / 8 * 4)
  34. #define OFFSET(x) BIT((x) % 8)
  35. struct mb86s70_gpio_chip {
  36. struct gpio_chip gc;
  37. void __iomem *base;
  38. struct clk *clk;
  39. spinlock_t lock;
  40. };
  41. static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
  42. {
  43. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  44. unsigned long flags;
  45. u32 val;
  46. spin_lock_irqsave(&gchip->lock, flags);
  47. val = readl(gchip->base + PFR(gpio));
  48. if (!(val & OFFSET(gpio))) {
  49. spin_unlock_irqrestore(&gchip->lock, flags);
  50. return -EINVAL;
  51. }
  52. val &= ~OFFSET(gpio);
  53. writel(val, gchip->base + PFR(gpio));
  54. spin_unlock_irqrestore(&gchip->lock, flags);
  55. return 0;
  56. }
  57. static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
  58. {
  59. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  60. unsigned long flags;
  61. u32 val;
  62. spin_lock_irqsave(&gchip->lock, flags);
  63. val = readl(gchip->base + PFR(gpio));
  64. val |= OFFSET(gpio);
  65. writel(val, gchip->base + PFR(gpio));
  66. spin_unlock_irqrestore(&gchip->lock, flags);
  67. }
  68. static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  69. {
  70. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  71. unsigned long flags;
  72. unsigned char val;
  73. spin_lock_irqsave(&gchip->lock, flags);
  74. val = readl(gchip->base + DDR(gpio));
  75. val &= ~OFFSET(gpio);
  76. writel(val, gchip->base + DDR(gpio));
  77. spin_unlock_irqrestore(&gchip->lock, flags);
  78. return 0;
  79. }
  80. static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
  81. unsigned gpio, int value)
  82. {
  83. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  84. unsigned long flags;
  85. unsigned char val;
  86. spin_lock_irqsave(&gchip->lock, flags);
  87. val = readl(gchip->base + PDR(gpio));
  88. if (value)
  89. val |= OFFSET(gpio);
  90. else
  91. val &= ~OFFSET(gpio);
  92. writel(val, gchip->base + PDR(gpio));
  93. val = readl(gchip->base + DDR(gpio));
  94. val |= OFFSET(gpio);
  95. writel(val, gchip->base + DDR(gpio));
  96. spin_unlock_irqrestore(&gchip->lock, flags);
  97. return 0;
  98. }
  99. static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
  100. {
  101. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  102. return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
  103. }
  104. static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
  105. {
  106. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  107. unsigned long flags;
  108. unsigned char val;
  109. spin_lock_irqsave(&gchip->lock, flags);
  110. val = readl(gchip->base + PDR(gpio));
  111. if (value)
  112. val |= OFFSET(gpio);
  113. else
  114. val &= ~OFFSET(gpio);
  115. writel(val, gchip->base + PDR(gpio));
  116. spin_unlock_irqrestore(&gchip->lock, flags);
  117. }
  118. static int mb86s70_gpio_probe(struct platform_device *pdev)
  119. {
  120. struct mb86s70_gpio_chip *gchip;
  121. struct resource *res;
  122. int ret;
  123. gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
  124. if (gchip == NULL)
  125. return -ENOMEM;
  126. platform_set_drvdata(pdev, gchip);
  127. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  128. gchip->base = devm_ioremap_resource(&pdev->dev, res);
  129. if (IS_ERR(gchip->base))
  130. return PTR_ERR(gchip->base);
  131. gchip->clk = devm_clk_get(&pdev->dev, NULL);
  132. if (IS_ERR(gchip->clk))
  133. return PTR_ERR(gchip->clk);
  134. clk_prepare_enable(gchip->clk);
  135. spin_lock_init(&gchip->lock);
  136. gchip->gc.direction_output = mb86s70_gpio_direction_output;
  137. gchip->gc.direction_input = mb86s70_gpio_direction_input;
  138. gchip->gc.request = mb86s70_gpio_request;
  139. gchip->gc.free = mb86s70_gpio_free;
  140. gchip->gc.get = mb86s70_gpio_get;
  141. gchip->gc.set = mb86s70_gpio_set;
  142. gchip->gc.label = dev_name(&pdev->dev);
  143. gchip->gc.ngpio = 32;
  144. gchip->gc.owner = THIS_MODULE;
  145. gchip->gc.parent = &pdev->dev;
  146. gchip->gc.base = -1;
  147. ret = gpiochip_add_data(&gchip->gc, gchip);
  148. if (ret) {
  149. dev_err(&pdev->dev, "couldn't register gpio driver\n");
  150. clk_disable_unprepare(gchip->clk);
  151. }
  152. return ret;
  153. }
  154. static int mb86s70_gpio_remove(struct platform_device *pdev)
  155. {
  156. struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
  157. gpiochip_remove(&gchip->gc);
  158. clk_disable_unprepare(gchip->clk);
  159. return 0;
  160. }
  161. static const struct of_device_id mb86s70_gpio_dt_ids[] = {
  162. { .compatible = "fujitsu,mb86s70-gpio" },
  163. { /* sentinel */ }
  164. };
  165. static struct platform_driver mb86s70_gpio_driver = {
  166. .driver = {
  167. .name = "mb86s70-gpio",
  168. .of_match_table = mb86s70_gpio_dt_ids,
  169. },
  170. .probe = mb86s70_gpio_probe,
  171. .remove = mb86s70_gpio_remove,
  172. };
  173. static int __init mb86s70_gpio_init(void)
  174. {
  175. return platform_driver_register(&mb86s70_gpio_driver);
  176. }
  177. device_initcall(mb86s70_gpio_init);