gpio-mb86s7x.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
  43. {
  44. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  45. unsigned long flags;
  46. u32 val;
  47. spin_lock_irqsave(&gchip->lock, flags);
  48. val = readl(gchip->base + PFR(gpio));
  49. if (!(val & OFFSET(gpio))) {
  50. spin_unlock_irqrestore(&gchip->lock, flags);
  51. return -EINVAL;
  52. }
  53. val &= ~OFFSET(gpio);
  54. writel(val, gchip->base + PFR(gpio));
  55. spin_unlock_irqrestore(&gchip->lock, flags);
  56. return 0;
  57. }
  58. static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
  59. {
  60. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  61. unsigned long flags;
  62. u32 val;
  63. spin_lock_irqsave(&gchip->lock, flags);
  64. val = readl(gchip->base + PFR(gpio));
  65. val |= OFFSET(gpio);
  66. writel(val, gchip->base + PFR(gpio));
  67. spin_unlock_irqrestore(&gchip->lock, flags);
  68. }
  69. static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  70. {
  71. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  72. unsigned long flags;
  73. unsigned char val;
  74. spin_lock_irqsave(&gchip->lock, flags);
  75. val = readl(gchip->base + DDR(gpio));
  76. val &= ~OFFSET(gpio);
  77. writel(val, gchip->base + DDR(gpio));
  78. spin_unlock_irqrestore(&gchip->lock, flags);
  79. return 0;
  80. }
  81. static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
  82. unsigned gpio, int value)
  83. {
  84. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  85. unsigned long flags;
  86. unsigned char val;
  87. spin_lock_irqsave(&gchip->lock, flags);
  88. val = readl(gchip->base + PDR(gpio));
  89. if (value)
  90. val |= OFFSET(gpio);
  91. else
  92. val &= ~OFFSET(gpio);
  93. writel(val, gchip->base + PDR(gpio));
  94. val = readl(gchip->base + DDR(gpio));
  95. val |= OFFSET(gpio);
  96. writel(val, gchip->base + DDR(gpio));
  97. spin_unlock_irqrestore(&gchip->lock, flags);
  98. return 0;
  99. }
  100. static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
  101. {
  102. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  103. return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
  104. }
  105. static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
  106. {
  107. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  108. unsigned long flags;
  109. unsigned char val;
  110. spin_lock_irqsave(&gchip->lock, flags);
  111. val = readl(gchip->base + PDR(gpio));
  112. if (value)
  113. val |= OFFSET(gpio);
  114. else
  115. val &= ~OFFSET(gpio);
  116. writel(val, gchip->base + PDR(gpio));
  117. spin_unlock_irqrestore(&gchip->lock, flags);
  118. }
  119. static int mb86s70_gpio_probe(struct platform_device *pdev)
  120. {
  121. struct mb86s70_gpio_chip *gchip;
  122. struct resource *res;
  123. int ret;
  124. gchip = devm_kzalloc(&pdev->dev, sizeof(*gchip), GFP_KERNEL);
  125. if (gchip == NULL)
  126. return -ENOMEM;
  127. platform_set_drvdata(pdev, gchip);
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. gchip->base = devm_ioremap_resource(&pdev->dev, res);
  130. if (IS_ERR(gchip->base))
  131. return PTR_ERR(gchip->base);
  132. gchip->clk = devm_clk_get(&pdev->dev, NULL);
  133. if (IS_ERR(gchip->clk))
  134. return PTR_ERR(gchip->clk);
  135. clk_prepare_enable(gchip->clk);
  136. spin_lock_init(&gchip->lock);
  137. gchip->gc.direction_output = mb86s70_gpio_direction_output;
  138. gchip->gc.direction_input = mb86s70_gpio_direction_input;
  139. gchip->gc.request = mb86s70_gpio_request;
  140. gchip->gc.free = mb86s70_gpio_free;
  141. gchip->gc.get = mb86s70_gpio_get;
  142. gchip->gc.set = mb86s70_gpio_set;
  143. gchip->gc.label = dev_name(&pdev->dev);
  144. gchip->gc.ngpio = 32;
  145. gchip->gc.owner = THIS_MODULE;
  146. gchip->gc.parent = &pdev->dev;
  147. gchip->gc.base = -1;
  148. platform_set_drvdata(pdev, gchip);
  149. ret = gpiochip_add_data(&gchip->gc, gchip);
  150. if (ret) {
  151. dev_err(&pdev->dev, "couldn't register gpio driver\n");
  152. clk_disable_unprepare(gchip->clk);
  153. }
  154. return ret;
  155. }
  156. static int mb86s70_gpio_remove(struct platform_device *pdev)
  157. {
  158. struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
  159. gpiochip_remove(&gchip->gc);
  160. clk_disable_unprepare(gchip->clk);
  161. return 0;
  162. }
  163. static const struct of_device_id mb86s70_gpio_dt_ids[] = {
  164. { .compatible = "fujitsu,mb86s70-gpio" },
  165. { /* sentinel */ }
  166. };
  167. MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
  168. static struct platform_driver mb86s70_gpio_driver = {
  169. .driver = {
  170. .name = "mb86s70-gpio",
  171. .of_match_table = mb86s70_gpio_dt_ids,
  172. },
  173. .probe = mb86s70_gpio_probe,
  174. .remove = mb86s70_gpio_remove,
  175. };
  176. static int __init mb86s70_gpio_init(void)
  177. {
  178. return platform_driver_register(&mb86s70_gpio_driver);
  179. }
  180. module_init(mb86s70_gpio_init);
  181. MODULE_DESCRIPTION("MB86S7x GPIO Driver");
  182. MODULE_ALIAS("platform:mb86s70-gpio");
  183. MODULE_LICENSE("GPL");