gpio-syscon.c 6.9 KB

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