gpio-syscon.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/driver.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. u32 dreg_offset;
  53. u32 dir_reg_offset;
  54. };
  55. static int syscon_gpio_get(struct gpio_chip *chip, unsigned offset)
  56. {
  57. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  58. unsigned int val, offs;
  59. int ret;
  60. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  61. ret = regmap_read(priv->syscon,
  62. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE, &val);
  63. if (ret)
  64. return ret;
  65. return !!(val & BIT(offs % SYSCON_REG_BITS));
  66. }
  67. static void syscon_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  68. {
  69. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  70. unsigned int offs;
  71. offs = priv->dreg_offset + 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 = gpiochip_get_data(chip);
  80. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  81. unsigned int offs;
  82. offs = priv->dir_reg_offset +
  83. priv->data->dir_bit_offset + offset;
  84. regmap_update_bits(priv->syscon,
  85. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  86. BIT(offs % SYSCON_REG_BITS), 0);
  87. }
  88. return 0;
  89. }
  90. static int syscon_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int val)
  91. {
  92. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  93. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  94. unsigned int offs;
  95. offs = priv->dir_reg_offset +
  96. priv->data->dir_bit_offset + offset;
  97. regmap_update_bits(priv->syscon,
  98. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  99. BIT(offs % SYSCON_REG_BITS),
  100. BIT(offs % SYSCON_REG_BITS));
  101. }
  102. chip->set(chip, offset, val);
  103. return 0;
  104. }
  105. static const struct syscon_gpio_data clps711x_mctrl_gpio = {
  106. /* ARM CLPS711X SYSFLG1 Bits 8-10 */
  107. .compatible = "cirrus,ep7209-syscon1",
  108. .flags = GPIO_SYSCON_FEAT_IN,
  109. .bit_count = 3,
  110. .dat_bit_offset = 0x40 * 8 + 8,
  111. };
  112. static void rockchip_gpio_set(struct gpio_chip *chip, unsigned int offset,
  113. int val)
  114. {
  115. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  116. unsigned int offs;
  117. u8 bit;
  118. u32 data;
  119. int ret;
  120. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  121. bit = offs % SYSCON_REG_BITS;
  122. data = (val ? BIT(bit) : 0) | BIT(bit + 16);
  123. ret = regmap_write(priv->syscon,
  124. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  125. data);
  126. if (ret < 0)
  127. dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
  128. }
  129. static const struct syscon_gpio_data rockchip_rk3328_gpio_mute = {
  130. /* RK3328 GPIO_MUTE is an output only pin at GRF_SOC_CON10[1] */
  131. .flags = GPIO_SYSCON_FEAT_OUT,
  132. .bit_count = 1,
  133. .dat_bit_offset = 0x0428 * 8 + 1,
  134. .set = rockchip_gpio_set,
  135. };
  136. #define KEYSTONE_LOCK_BIT BIT(0)
  137. static void keystone_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  138. {
  139. struct syscon_gpio_priv *priv = gpiochip_get_data(chip);
  140. unsigned int offs;
  141. int ret;
  142. offs = priv->dreg_offset + priv->data->dat_bit_offset + offset;
  143. if (!val)
  144. return;
  145. ret = regmap_update_bits(
  146. priv->syscon,
  147. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  148. BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT,
  149. BIT(offs % SYSCON_REG_BITS) | KEYSTONE_LOCK_BIT);
  150. if (ret < 0)
  151. dev_err(chip->parent, "gpio write failed ret(%d)\n", ret);
  152. }
  153. static const struct syscon_gpio_data keystone_dsp_gpio = {
  154. /* ARM Keystone 2 */
  155. .compatible = NULL,
  156. .flags = GPIO_SYSCON_FEAT_OUT,
  157. .bit_count = 28,
  158. .dat_bit_offset = 4,
  159. .set = keystone_gpio_set,
  160. };
  161. static const struct of_device_id syscon_gpio_ids[] = {
  162. {
  163. .compatible = "cirrus,ep7209-mctrl-gpio",
  164. .data = &clps711x_mctrl_gpio,
  165. },
  166. {
  167. .compatible = "ti,keystone-dsp-gpio",
  168. .data = &keystone_dsp_gpio,
  169. },
  170. {
  171. .compatible = "rockchip,rk3328-grf-gpio",
  172. .data = &rockchip_rk3328_gpio_mute,
  173. },
  174. { }
  175. };
  176. MODULE_DEVICE_TABLE(of, syscon_gpio_ids);
  177. static int syscon_gpio_probe(struct platform_device *pdev)
  178. {
  179. struct device *dev = &pdev->dev;
  180. struct syscon_gpio_priv *priv;
  181. struct device_node *np = dev->of_node;
  182. int ret;
  183. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  184. if (!priv)
  185. return -ENOMEM;
  186. priv->data = of_device_get_match_data(dev);
  187. if (priv->data->compatible) {
  188. priv->syscon = syscon_regmap_lookup_by_compatible(
  189. priv->data->compatible);
  190. if (IS_ERR(priv->syscon))
  191. return PTR_ERR(priv->syscon);
  192. } else {
  193. priv->syscon =
  194. syscon_regmap_lookup_by_phandle(np, "gpio,syscon-dev");
  195. if (IS_ERR(priv->syscon) && np->parent)
  196. priv->syscon = syscon_node_to_regmap(np->parent);
  197. if (IS_ERR(priv->syscon))
  198. return PTR_ERR(priv->syscon);
  199. ret = of_property_read_u32_index(np, "gpio,syscon-dev", 1,
  200. &priv->dreg_offset);
  201. if (ret)
  202. dev_err(dev, "can't read the data register offset!\n");
  203. priv->dreg_offset <<= 3;
  204. ret = of_property_read_u32_index(np, "gpio,syscon-dev", 2,
  205. &priv->dir_reg_offset);
  206. if (ret)
  207. dev_dbg(dev, "can't read the dir register offset!\n");
  208. priv->dir_reg_offset <<= 3;
  209. }
  210. priv->chip.parent = dev;
  211. priv->chip.owner = THIS_MODULE;
  212. priv->chip.label = dev_name(dev);
  213. priv->chip.base = -1;
  214. priv->chip.ngpio = priv->data->bit_count;
  215. priv->chip.get = syscon_gpio_get;
  216. if (priv->data->flags & GPIO_SYSCON_FEAT_IN)
  217. priv->chip.direction_input = syscon_gpio_dir_in;
  218. if (priv->data->flags & GPIO_SYSCON_FEAT_OUT) {
  219. priv->chip.set = priv->data->set ? : syscon_gpio_set;
  220. priv->chip.direction_output = syscon_gpio_dir_out;
  221. }
  222. platform_set_drvdata(pdev, priv);
  223. return devm_gpiochip_add_data(&pdev->dev, &priv->chip, priv);
  224. }
  225. static struct platform_driver syscon_gpio_driver = {
  226. .driver = {
  227. .name = "gpio-syscon",
  228. .of_match_table = syscon_gpio_ids,
  229. },
  230. .probe = syscon_gpio_probe,
  231. };
  232. module_platform_driver(syscon_gpio_driver);
  233. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  234. MODULE_DESCRIPTION("SYSCON GPIO driver");
  235. MODULE_LICENSE("GPL");