gpio-syscon.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * SYSCON GPIO driver
  3. *
  4. * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/gpio.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/regmap.h>
  18. #include <linux/mfd/syscon.h>
  19. #define GPIO_SYSCON_FEAT_IN BIT(0)
  20. #define GPIO_SYSCON_FEAT_OUT BIT(1)
  21. #define GPIO_SYSCON_FEAT_DIR BIT(2)
  22. /* SYSCON driver is designed to use 32-bit wide registers */
  23. #define SYSCON_REG_SIZE (4)
  24. #define SYSCON_REG_BITS (SYSCON_REG_SIZE * 8)
  25. /**
  26. * struct syscon_gpio_data - Configuration for the device.
  27. * compatible: SYSCON driver compatible string.
  28. * flags: Set of GPIO_SYSCON_FEAT_ flags:
  29. * GPIO_SYSCON_FEAT_IN: GPIOs supports input,
  30. * GPIO_SYSCON_FEAT_OUT: GPIOs supports output,
  31. * GPIO_SYSCON_FEAT_DIR: GPIOs supports switch direction.
  32. * bit_count: Number of bits used as GPIOs.
  33. * dat_bit_offset: Offset (in bits) to the first GPIO bit.
  34. * dir_bit_offset: Optional offset (in bits) to the first bit to switch
  35. * GPIO direction (Used with GPIO_SYSCON_FEAT_DIR flag).
  36. * set: HW specific callback to assigns output value
  37. * for signal "offset"
  38. */
  39. struct syscon_gpio_data {
  40. const char *compatible;
  41. unsigned int flags;
  42. unsigned int bit_count;
  43. unsigned int dat_bit_offset;
  44. unsigned int dir_bit_offset;
  45. void (*set)(struct gpio_chip *chip,
  46. unsigned offset, int value);
  47. };
  48. struct syscon_gpio_priv {
  49. struct gpio_chip chip;
  50. struct regmap *syscon;
  51. const struct syscon_gpio_data *data;
  52. };
  53. static inline struct syscon_gpio_priv *to_syscon_gpio(struct gpio_chip *chip)
  54. {
  55. return container_of(chip, struct syscon_gpio_priv, chip);
  56. }
  57. static int syscon_gpio_get(struct gpio_chip *chip, unsigned offset)
  58. {
  59. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  60. unsigned int val, offs = priv->data->dat_bit_offset + offset;
  61. int ret;
  62. ret = regmap_read(priv->syscon,
  63. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE, &val);
  64. if (ret)
  65. return ret;
  66. return !!(val & BIT(offs % SYSCON_REG_BITS));
  67. }
  68. static void syscon_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  69. {
  70. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  71. unsigned int offs = priv->data->dat_bit_offset + offset;
  72. regmap_update_bits(priv->syscon,
  73. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  74. BIT(offs % SYSCON_REG_BITS),
  75. val ? BIT(offs % SYSCON_REG_BITS) : 0);
  76. }
  77. static int syscon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  78. {
  79. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  80. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  81. unsigned int offs = priv->data->dir_bit_offset + offset;
  82. regmap_update_bits(priv->syscon,
  83. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  84. BIT(offs % SYSCON_REG_BITS), 0);
  85. }
  86. return 0;
  87. }
  88. static int syscon_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int val)
  89. {
  90. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  91. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  92. unsigned int offs = priv->data->dir_bit_offset + offset;
  93. regmap_update_bits(priv->syscon,
  94. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  95. BIT(offs % SYSCON_REG_BITS),
  96. BIT(offs % SYSCON_REG_BITS));
  97. }
  98. priv->data->set(chip, offset, val);
  99. return 0;
  100. }
  101. static const struct syscon_gpio_data clps711x_mctrl_gpio = {
  102. /* ARM CLPS711X SYSFLG1 Bits 8-10 */
  103. .compatible = "cirrus,clps711x-syscon1",
  104. .flags = GPIO_SYSCON_FEAT_IN,
  105. .bit_count = 3,
  106. .dat_bit_offset = 0x40 * 8 + 8,
  107. };
  108. static const struct of_device_id syscon_gpio_ids[] = {
  109. {
  110. .compatible = "cirrus,clps711x-mctrl-gpio",
  111. .data = &clps711x_mctrl_gpio,
  112. },
  113. { }
  114. };
  115. MODULE_DEVICE_TABLE(of, syscon_gpio_ids);
  116. static int syscon_gpio_probe(struct platform_device *pdev)
  117. {
  118. struct device *dev = &pdev->dev;
  119. const struct of_device_id *of_id = of_match_device(syscon_gpio_ids, dev);
  120. struct syscon_gpio_priv *priv;
  121. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  122. if (!priv)
  123. return -ENOMEM;
  124. priv->data = of_id->data;
  125. priv->syscon =
  126. syscon_regmap_lookup_by_compatible(priv->data->compatible);
  127. if (IS_ERR(priv->syscon))
  128. return PTR_ERR(priv->syscon);
  129. priv->chip.dev = dev;
  130. priv->chip.owner = THIS_MODULE;
  131. priv->chip.label = dev_name(dev);
  132. priv->chip.base = -1;
  133. priv->chip.ngpio = priv->data->bit_count;
  134. priv->chip.get = syscon_gpio_get;
  135. if (priv->data->flags & GPIO_SYSCON_FEAT_IN)
  136. priv->chip.direction_input = syscon_gpio_dir_in;
  137. if (priv->data->flags & GPIO_SYSCON_FEAT_OUT) {
  138. priv->chip.set = priv->data->set ? : syscon_gpio_set;
  139. priv->chip.direction_output = syscon_gpio_dir_out;
  140. }
  141. platform_set_drvdata(pdev, priv);
  142. return gpiochip_add(&priv->chip);
  143. }
  144. static int syscon_gpio_remove(struct platform_device *pdev)
  145. {
  146. struct syscon_gpio_priv *priv = platform_get_drvdata(pdev);
  147. gpiochip_remove(&priv->chip);
  148. return 0;
  149. }
  150. static struct platform_driver syscon_gpio_driver = {
  151. .driver = {
  152. .name = "gpio-syscon",
  153. .owner = THIS_MODULE,
  154. .of_match_table = syscon_gpio_ids,
  155. },
  156. .probe = syscon_gpio_probe,
  157. .remove = syscon_gpio_remove,
  158. };
  159. module_platform_driver(syscon_gpio_driver);
  160. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  161. MODULE_DESCRIPTION("SYSCON GPIO driver");
  162. MODULE_LICENSE("GPL");