gpio-mb86s7x.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/gpio/gpio-mb86s7x.c
  4. *
  5. * Copyright (C) 2015 Fujitsu Semiconductor Limited
  6. * Copyright (C) 2015 Linaro Ltd.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/io.h>
  10. #include <linux/init.h>
  11. #include <linux/clk.h>
  12. #include <linux/module.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/ioport.h>
  16. #include <linux/of_device.h>
  17. #include <linux/gpio/driver.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/slab.h>
  21. #include "gpiolib.h"
  22. #include "gpiolib-acpi.h"
  23. /*
  24. * Only first 8bits of a register correspond to each pin,
  25. * so there are 4 registers for 32 pins.
  26. */
  27. #define PDR(x) (0x0 + x / 8 * 4)
  28. #define DDR(x) (0x10 + x / 8 * 4)
  29. #define PFR(x) (0x20 + x / 8 * 4)
  30. #define OFFSET(x) BIT((x) % 8)
  31. struct mb86s70_gpio_chip {
  32. struct gpio_chip gc;
  33. void __iomem *base;
  34. struct clk *clk;
  35. spinlock_t lock;
  36. };
  37. static int mb86s70_gpio_request(struct gpio_chip *gc, unsigned gpio)
  38. {
  39. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  40. unsigned long flags;
  41. u32 val;
  42. spin_lock_irqsave(&gchip->lock, flags);
  43. val = readl(gchip->base + PFR(gpio));
  44. val &= ~OFFSET(gpio);
  45. writel(val, gchip->base + PFR(gpio));
  46. spin_unlock_irqrestore(&gchip->lock, flags);
  47. return 0;
  48. }
  49. static void mb86s70_gpio_free(struct gpio_chip *gc, unsigned gpio)
  50. {
  51. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  52. unsigned long flags;
  53. u32 val;
  54. spin_lock_irqsave(&gchip->lock, flags);
  55. val = readl(gchip->base + PFR(gpio));
  56. val |= OFFSET(gpio);
  57. writel(val, gchip->base + PFR(gpio));
  58. spin_unlock_irqrestore(&gchip->lock, flags);
  59. }
  60. static int mb86s70_gpio_direction_input(struct gpio_chip *gc, unsigned gpio)
  61. {
  62. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  63. unsigned long flags;
  64. unsigned char val;
  65. spin_lock_irqsave(&gchip->lock, flags);
  66. val = readl(gchip->base + DDR(gpio));
  67. val &= ~OFFSET(gpio);
  68. writel(val, gchip->base + DDR(gpio));
  69. spin_unlock_irqrestore(&gchip->lock, flags);
  70. return 0;
  71. }
  72. static int mb86s70_gpio_direction_output(struct gpio_chip *gc,
  73. unsigned gpio, int value)
  74. {
  75. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  76. unsigned long flags;
  77. unsigned char val;
  78. spin_lock_irqsave(&gchip->lock, flags);
  79. val = readl(gchip->base + PDR(gpio));
  80. if (value)
  81. val |= OFFSET(gpio);
  82. else
  83. val &= ~OFFSET(gpio);
  84. writel(val, gchip->base + PDR(gpio));
  85. val = readl(gchip->base + DDR(gpio));
  86. val |= OFFSET(gpio);
  87. writel(val, gchip->base + DDR(gpio));
  88. spin_unlock_irqrestore(&gchip->lock, flags);
  89. return 0;
  90. }
  91. static int mb86s70_gpio_get(struct gpio_chip *gc, unsigned gpio)
  92. {
  93. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  94. return !!(readl(gchip->base + PDR(gpio)) & OFFSET(gpio));
  95. }
  96. static void mb86s70_gpio_set(struct gpio_chip *gc, unsigned gpio, int value)
  97. {
  98. struct mb86s70_gpio_chip *gchip = gpiochip_get_data(gc);
  99. unsigned long flags;
  100. unsigned char val;
  101. spin_lock_irqsave(&gchip->lock, flags);
  102. val = readl(gchip->base + PDR(gpio));
  103. if (value)
  104. val |= OFFSET(gpio);
  105. else
  106. val &= ~OFFSET(gpio);
  107. writel(val, gchip->base + PDR(gpio));
  108. spin_unlock_irqrestore(&gchip->lock, flags);
  109. }
  110. static int mb86s70_gpio_to_irq(struct gpio_chip *gc, unsigned int offset)
  111. {
  112. int irq, index;
  113. for (index = 0;; index++) {
  114. irq = platform_get_irq(to_platform_device(gc->parent), index);
  115. if (irq < 0)
  116. return irq;
  117. if (irq == 0)
  118. break;
  119. if (irq_get_irq_data(irq)->hwirq == offset)
  120. return irq;
  121. }
  122. return -EINVAL;
  123. }
  124. static int mb86s70_gpio_probe(struct platform_device *pdev)
  125. {
  126. struct mb86s70_gpio_chip *gchip;
  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. gchip->base = devm_platform_ioremap_resource(pdev, 0);
  133. if (IS_ERR(gchip->base))
  134. return PTR_ERR(gchip->base);
  135. gchip->clk = devm_clk_get_optional(&pdev->dev, NULL);
  136. if (IS_ERR(gchip->clk))
  137. return PTR_ERR(gchip->clk);
  138. ret = clk_prepare_enable(gchip->clk);
  139. if (ret)
  140. return ret;
  141. spin_lock_init(&gchip->lock);
  142. gchip->gc.direction_output = mb86s70_gpio_direction_output;
  143. gchip->gc.direction_input = mb86s70_gpio_direction_input;
  144. gchip->gc.request = mb86s70_gpio_request;
  145. gchip->gc.free = mb86s70_gpio_free;
  146. gchip->gc.get = mb86s70_gpio_get;
  147. gchip->gc.set = mb86s70_gpio_set;
  148. gchip->gc.to_irq = mb86s70_gpio_to_irq;
  149. gchip->gc.label = dev_name(&pdev->dev);
  150. gchip->gc.ngpio = 32;
  151. gchip->gc.owner = THIS_MODULE;
  152. gchip->gc.parent = &pdev->dev;
  153. gchip->gc.base = -1;
  154. ret = gpiochip_add_data(&gchip->gc, gchip);
  155. if (ret) {
  156. dev_err(&pdev->dev, "couldn't register gpio driver\n");
  157. clk_disable_unprepare(gchip->clk);
  158. return ret;
  159. }
  160. acpi_gpiochip_request_interrupts(&gchip->gc);
  161. return 0;
  162. }
  163. static int mb86s70_gpio_remove(struct platform_device *pdev)
  164. {
  165. struct mb86s70_gpio_chip *gchip = platform_get_drvdata(pdev);
  166. acpi_gpiochip_free_interrupts(&gchip->gc);
  167. gpiochip_remove(&gchip->gc);
  168. clk_disable_unprepare(gchip->clk);
  169. return 0;
  170. }
  171. static const struct of_device_id mb86s70_gpio_dt_ids[] = {
  172. { .compatible = "fujitsu,mb86s70-gpio" },
  173. { /* sentinel */ }
  174. };
  175. MODULE_DEVICE_TABLE(of, mb86s70_gpio_dt_ids);
  176. #ifdef CONFIG_ACPI
  177. static const struct acpi_device_id mb86s70_gpio_acpi_ids[] = {
  178. { "SCX0007" },
  179. { /* sentinel */ }
  180. };
  181. MODULE_DEVICE_TABLE(acpi, mb86s70_gpio_acpi_ids);
  182. #endif
  183. static struct platform_driver mb86s70_gpio_driver = {
  184. .driver = {
  185. .name = "mb86s70-gpio",
  186. .of_match_table = mb86s70_gpio_dt_ids,
  187. .acpi_match_table = ACPI_PTR(mb86s70_gpio_acpi_ids),
  188. },
  189. .probe = mb86s70_gpio_probe,
  190. .remove = mb86s70_gpio_remove,
  191. };
  192. module_platform_driver(mb86s70_gpio_driver);
  193. MODULE_DESCRIPTION("MB86S7x GPIO Driver");
  194. MODULE_ALIAS("platform:mb86s70-gpio");
  195. MODULE_LICENSE("GPL");