gpio-mb86s7x.c 5.4 KB

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