gpio-syscon.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. */
  37. struct syscon_gpio_data {
  38. const char *compatible;
  39. unsigned int flags;
  40. unsigned int bit_count;
  41. unsigned int dat_bit_offset;
  42. unsigned int dir_bit_offset;
  43. };
  44. struct syscon_gpio_priv {
  45. struct gpio_chip chip;
  46. struct regmap *syscon;
  47. const struct syscon_gpio_data *data;
  48. };
  49. static inline struct syscon_gpio_priv *to_syscon_gpio(struct gpio_chip *chip)
  50. {
  51. return container_of(chip, struct syscon_gpio_priv, chip);
  52. }
  53. static int syscon_gpio_get(struct gpio_chip *chip, unsigned offset)
  54. {
  55. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  56. unsigned int val, offs = priv->data->dat_bit_offset + offset;
  57. int ret;
  58. ret = regmap_read(priv->syscon,
  59. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE, &val);
  60. if (ret)
  61. return ret;
  62. return !!(val & BIT(offs % SYSCON_REG_BITS));
  63. }
  64. static void syscon_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
  65. {
  66. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  67. unsigned int offs = priv->data->dat_bit_offset + offset;
  68. regmap_update_bits(priv->syscon,
  69. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  70. BIT(offs % SYSCON_REG_BITS),
  71. val ? BIT(offs % SYSCON_REG_BITS) : 0);
  72. }
  73. static int syscon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  74. {
  75. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  76. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  77. unsigned int offs = priv->data->dir_bit_offset + offset;
  78. regmap_update_bits(priv->syscon,
  79. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  80. BIT(offs % SYSCON_REG_BITS), 0);
  81. }
  82. return 0;
  83. }
  84. static int syscon_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int val)
  85. {
  86. struct syscon_gpio_priv *priv = to_syscon_gpio(chip);
  87. if (priv->data->flags & GPIO_SYSCON_FEAT_DIR) {
  88. unsigned int offs = priv->data->dir_bit_offset + offset;
  89. regmap_update_bits(priv->syscon,
  90. (offs / SYSCON_REG_BITS) * SYSCON_REG_SIZE,
  91. BIT(offs % SYSCON_REG_BITS),
  92. BIT(offs % SYSCON_REG_BITS));
  93. }
  94. syscon_gpio_set(chip, offset, val);
  95. return 0;
  96. }
  97. static const struct syscon_gpio_data clps711x_mctrl_gpio = {
  98. /* ARM CLPS711X SYSFLG1 Bits 8-10 */
  99. .compatible = "cirrus,clps711x-syscon1",
  100. .flags = GPIO_SYSCON_FEAT_IN,
  101. .bit_count = 3,
  102. .dat_bit_offset = 0x40 * 8 + 8,
  103. };
  104. static const struct of_device_id syscon_gpio_ids[] = {
  105. {
  106. .compatible = "cirrus,clps711x-mctrl-gpio",
  107. .data = &clps711x_mctrl_gpio,
  108. },
  109. { }
  110. };
  111. MODULE_DEVICE_TABLE(of, syscon_gpio_ids);
  112. static int syscon_gpio_probe(struct platform_device *pdev)
  113. {
  114. struct device *dev = &pdev->dev;
  115. const struct of_device_id *of_id = of_match_device(syscon_gpio_ids, dev);
  116. struct syscon_gpio_priv *priv;
  117. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  118. if (!priv)
  119. return -ENOMEM;
  120. priv->data = of_id->data;
  121. priv->syscon =
  122. syscon_regmap_lookup_by_compatible(priv->data->compatible);
  123. if (IS_ERR(priv->syscon))
  124. return PTR_ERR(priv->syscon);
  125. priv->chip.dev = dev;
  126. priv->chip.owner = THIS_MODULE;
  127. priv->chip.label = dev_name(dev);
  128. priv->chip.base = -1;
  129. priv->chip.ngpio = priv->data->bit_count;
  130. priv->chip.get = syscon_gpio_get;
  131. if (priv->data->flags & GPIO_SYSCON_FEAT_IN)
  132. priv->chip.direction_input = syscon_gpio_dir_in;
  133. if (priv->data->flags & GPIO_SYSCON_FEAT_OUT) {
  134. priv->chip.set = syscon_gpio_set;
  135. priv->chip.direction_output = syscon_gpio_dir_out;
  136. }
  137. platform_set_drvdata(pdev, priv);
  138. return gpiochip_add(&priv->chip);
  139. }
  140. static int syscon_gpio_remove(struct platform_device *pdev)
  141. {
  142. struct syscon_gpio_priv *priv = platform_get_drvdata(pdev);
  143. return gpiochip_remove(&priv->chip);
  144. }
  145. static struct platform_driver syscon_gpio_driver = {
  146. .driver = {
  147. .name = "gpio-syscon",
  148. .owner = THIS_MODULE,
  149. .of_match_table = syscon_gpio_ids,
  150. },
  151. .probe = syscon_gpio_probe,
  152. .remove = syscon_gpio_remove,
  153. };
  154. module_platform_driver(syscon_gpio_driver);
  155. MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
  156. MODULE_DESCRIPTION("SYSCON GPIO driver");
  157. MODULE_LICENSE("GPL");