gpio-mb86s7x.c 5.7 KB

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