gpio-syscon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SYSCON GPIO driver
  4. *
  5. * Copyright (C) 2014 Alexander Shiyan <[email protected]>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/gpio/driver.h>
  9. #include <linux/module.h>
  10. #include <linux/of.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <linux/mfd/syscon.h>
  15. #define GPIO_SYSCON_FEAT_IN BIT(0)
  16. #define GPIO_SYSCON_FEAT_OUT BIT(1)
  17. #define GPIO_SYSCON_FEAT_DIR BIT(2)
  18. /* SYSCON driver is designed to use 32-bit wide registers */
  19. #define SYSCON_REG_SIZE (4)
  20. #define SYSCON_REG_BITS (SYSCON_REG_SIZE * 8)
  21. /**
  22. * struct syscon_gpio_data - Configuration for the device.
  23. * @compatible: SYSCON driver compatible string.
  24. * @flags: Set of GPIO_SYSCON_FEAT_ flags:
  25. * GPIO_SYSCON_FEAT_IN: GPIOs supports input,
  26. * GPIO_SYSCON_FEAT_OUT: GPIOs supports output,
  27. * GPIO_SYSCON_FEAT_DIR: GPIOs supports switch direction.
  28. * @bit_count: Number of bits used as GPIOs.
  29. * @dat_bit_offset: Offset (in bits) to the first GPIO bit.
  30. * @dir_bit_offset: Optional offset (in bits) to the first bit to switch
  31. * GPIO direction (Used with GPIO_SYSCON_FEAT_DIR flag).
  32. * @set: HW specific callback to assigns output value
  33. * for signal "offset"
  34. */
  35. struct syscon_gpio_data {
  36. unsigned int flags;
  37. unsigned int bit_count;
  38. unsigned int dat_bit_offset;
  39. unsigned int dir_bit_offset;
  40. void (*set)(struct gpio_chip *chip,
  41. unsigned offset, int value);
  42. };
  43. struct syscon_gpio_priv {
  44. struct gpio_chip chip;
  45. struct regmap *syscon;
  46. const struct syscon_gpio_data *data;
  47. u32 dreg_offset;
  48. u32 dir_reg_offset;
  49. };
  50. static int syscon_gpio_get(struct gpio_chip *chip, unsigned offset)
  51. {
  52. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  53. unsigned int val, offs;
  54. int ret;
  55. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  56. ret = regmap_read(priv->syscon,
  57. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE, &val);
  58. if (ret)
  59. return ret;
  60. return !!(val & BIT(offs % SYSCON_REG_BITS));
  61. }
  62. static void syscon_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  63. {
  64. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  65. unsigned int offs;
  66. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  67. regmap_update_bits(priv->syscon,
  68. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  69. BIT(offs % SYSCON_REG_BITS),
  70. val ? BIT(offs % SYSCON_REG_BITS) : 0);
  71. }
  72. static int syscon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  73. {
  74. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  75. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  76. unsigned int offs;
  77. offs = priv->dir_reg_offset +
  78. priv->data->dir_bit_offset + offset;
  79. regmap_update_bits(priv->syscon,
  80. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  81. BIT(offs % SYSCON_REG_BITS), 0);
  82. }
  83. return 0;
  84. }
  85. static int syscon_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int val)
  86. {
  87. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  88. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  89. unsigned int offs;
  90. offs = priv->dir_reg_offset +
  91. priv->data->dir_bit_offset + offset;
  92. regmap_update_bits(priv->syscon,
  93. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  94. BIT(offs % SYSCON_REG_BITS),
  95. BIT(offs % SYSCON_REG_BITS));
  96. }
  97. chip->set(chip, offset, val);
  98. return 0;
  99. }
  100. static const struct syscon_gpio_data clps711x_mctrl_gpio = {
  101. /* ARM CLPS711X SYSFLG1 Bits 8-10 */
  102. .flags = GPIO_SYSCON_FEAT_IN,
  103. .bit_count = 3,
  104. .dat_bit_offset = 0x40 * 8 + 8,
  105. };
  106. static void rockchip_gpio_set(struct gpio_chip *chip, unsigned int offset,
  107. int val)
  108. {
  109. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  110. unsigned int offs;
  111. u8 bit;
  112. u32 data;
  113. int ret;
  114. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  115. bit = offs % SYSCON_REG_BITS;
  116. data = (val ? BIT(bit) : 0) | BIT(bit + 16);
  117. ret = regmap_write(priv->syscon,
  118. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  119. data);
  120. if (ret < 0)
  121. dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
  122. }
  123. static const struct syscon_gpio_data rockchip_rk3328_gpio_mute = {
  124. /* RK3328 GPIO_MUTE is an output only pin at GRF_SOC_CON10[1] */
  125. .flags = GPIO_SYSCON_FEAT_OUT,
  126. .bit_count = 1,
  127. .dat_bit_offset = 0x0428 * 8 + 1,
  128. .set = rockchip_gpio_set,
  129. };
  130. #define KEYSTONE_LOCK_BIT BIT(0)
  131. static void keystone_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  132. {
  133. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  134. unsigned int offs;
  135. int ret;
  136. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  137. if (!val)
  138. return;
  139. ret = regmap_update_bits(
  140. priv->syscon,
  141. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  142. BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT,
  143. BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT);
  144. if (ret < 0)
  145. dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
  146. }
  147. static const struct syscon_gpio_data keystone_dsp_gpio = {
  148. /* ARM Keystone 2 */
  149. .flags = GPIO_SYSCON_FEAT_OUT,
  150. .bit_count = 28,
  151. .dat_bit_offset = 4,
  152. .set = keystone_gpio_set,
  153. };
  154. static const struct of_device_id syscon_gpio_ids[] = {
  155. {
  156. .compatible = "cirrus,ep7209-mctrl-gpio",
  157. .data = &clps711x_mctrl_gpio,
  158. },
  159. {
  160. .compatible = "ti,keystone-dsp-gpio",
  161. .data = &keystone_dsp_gpio,
  162. },
  163. {
  164. .compatible = "rockchip,rk3328-grf-gpio",
  165. .data = &rockchip_rk3328_gpio_mute,
  166. },
  167. { }
  168. };
  169. MODULE_DEVICE_TABLE(of, syscon_gpio_ids);
  170. static int syscon_gpio_probe(struct platform_device *pdev)
  171. {
  172. struct device *dev = &pdev->dev;
  173. struct syscon_gpio_priv *priv;
  174. struct device_node *np = dev->of_node;
  175. int ret;
  176. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  177. if (!priv)
  178. return -ENOMEM;
  179. priv->data = of_device_get_match_data(dev);
  180. priv->syscon = syscon_regmap_lookup_by_phandle(np, "gpio,syscon-dev");
  181. if (IS_ERR(priv->syscon) && np->parent)
  182. priv->syscon = syscon_node_to_regmap(np->parent);
  183. if (IS_ERR(priv->syscon))
  184. return PTR_ERR(priv->syscon);
  185. ret = of_property_read_u32_index(np, "gpio,syscon-dev", 1,
  186. &priv->dreg_offset);
  187. if (ret)
  188. dev_err(dev, "can't read the data register offset!\n");
  189. priv->dreg_offset <<= 3;
  190. ret = of_property_read_u32_index(np, "gpio,syscon-dev", 2,
  191. &priv->dir_reg_offset);
  192. if (ret)
  193. dev_dbg(dev, "can't read the dir register offset!\n");
  194. priv->dir_reg_offset <<= 3;
  195. priv->chip.parent = dev;
  196. priv->chip.owner = THIS_MODULE;
  197. priv->chip.label = dev_name(dev);
  198. priv->chip.base = -1;
  199. priv->chip.ngpio = priv->data->bit_count;
  200. priv->chip.get = syscon_gpio_get;
  201. if (priv->data->flags & GPIO_SYSCON_FEAT_IN)
  202. priv->chip.direction_input = syscon_gpio_dir_in;
  203. if (priv->data->flags & GPIO_SYSCON_FEAT_OUT) {
  204. priv->chip.set = priv->data->set ? : syscon_gpio_set;
  205. priv->chip.direction_output = syscon_gpio_dir_out;
  206. }
  207. platform_set_drvdata(pdev, priv);
  208. return devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
  209. }
  210. static struct platform_driver syscon_gpio_driver = {
  211. .driver = {
  212. .name = "gpio-syscon",
  213. .of_match_table = syscon_gpio_ids,
  214. },
  215. .probe = syscon_gpio_probe,
  216. };
  217. module_platform_driver(syscon_gpio_driver);
  218. MODULE_AUTHOR("Alexander Shiyan <[email protected]>");
  219. MODULE_DESCRIPTION("SYSCON GPIO driver");
  220. MODULE_LICENSE("GPL");