gpio-ts4900.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Digital I/O driver for Technologic Systems I2C FPGA Core
  4. *
  5. * Copyright (C) 2015, 2018 Technologic Systems
  6. * Copyright (C) 2016 Savoir-Faire Linux
  7. */
  8. #include <linux/gpio/driver.h>
  9. #include <linux/i2c.h>
  10. #include <linux/of_device.h>
  11. #include <linux/module.h>
  12. #include <linux/regmap.h>
  13. #define DEFAULT_PIN_NUMBER 32
  14. /*
  15. * Register bits used by the GPIO device
  16. * Some boards, such as TS-7970 do not have a separate input bit
  17. */
  18. #define TS4900_GPIO_OE 0x01
  19. #define TS4900_GPIO_OUT 0x02
  20. #define TS4900_GPIO_IN 0x04
  21. #define TS7970_GPIO_IN 0x02
  22. struct ts4900_gpio_priv {
  23. struct regmap *regmap;
  24. struct gpio_chip gpio_chip;
  25. unsigned int input_bit;
  26. };
  27. static int ts4900_gpio_get_direction(struct gpio_chip *chip,
  28. unsigned int offset)
  29. {
  30. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  31. unsigned int reg;
  32. regmap_read(priv->regmap, offset, &reg);
  33. if (reg & TS4900_GPIO_OE)
  34. return GPIO_LINE_DIRECTION_OUT;
  35. return GPIO_LINE_DIRECTION_IN;
  36. }
  37. static int ts4900_gpio_direction_input(struct gpio_chip *chip,
  38. unsigned int offset)
  39. {
  40. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  41. /*
  42. * Only clear the OE bit here, requires a RMW. Prevents a potential issue
  43. * with OE and DAT getting to the physical pin at different times.
  44. */
  45. return regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OE, 0);
  46. }
  47. static int ts4900_gpio_direction_output(struct gpio_chip *chip,
  48. unsigned int offset, int value)
  49. {
  50. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  51. unsigned int reg;
  52. int ret;
  53. /*
  54. * If changing from an input to an output, we need to first set the
  55. * GPIO's DAT bit to what is requested and then set the OE bit. This
  56. * prevents a glitch that can occur on the IO line.
  57. */
  58. regmap_read(priv->regmap, offset, &reg);
  59. if (!(reg & TS4900_GPIO_OE)) {
  60. if (value)
  61. reg = TS4900_GPIO_OUT;
  62. else
  63. reg &= ~TS4900_GPIO_OUT;
  64. regmap_write(priv->regmap, offset, reg);
  65. }
  66. if (value)
  67. ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE |
  68. TS4900_GPIO_OUT);
  69. else
  70. ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE);
  71. return ret;
  72. }
  73. static int ts4900_gpio_get(struct gpio_chip *chip, unsigned int offset)
  74. {
  75. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  76. unsigned int reg;
  77. regmap_read(priv->regmap, offset, &reg);
  78. return !!(reg & priv->input_bit);
  79. }
  80. static void ts4900_gpio_set(struct gpio_chip *chip, unsigned int offset,
  81. int value)
  82. {
  83. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  84. if (value)
  85. regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT,
  86. TS4900_GPIO_OUT);
  87. else
  88. regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT, 0);
  89. }
  90. static const struct regmap_config ts4900_regmap_config = {
  91. .reg_bits = 16,
  92. .val_bits = 8,
  93. };
  94. static const struct gpio_chip template_chip = {
  95. .label = "ts4900-gpio",
  96. .owner = THIS_MODULE,
  97. .get_direction = ts4900_gpio_get_direction,
  98. .direction_input = ts4900_gpio_direction_input,
  99. .direction_output = ts4900_gpio_direction_output,
  100. .get = ts4900_gpio_get,
  101. .set = ts4900_gpio_set,
  102. .base = -1,
  103. .can_sleep = true,
  104. };
  105. static const struct of_device_id ts4900_gpio_of_match_table[] = {
  106. {
  107. .compatible = "technologic,ts4900-gpio",
  108. .data = (void *)TS4900_GPIO_IN,
  109. }, {
  110. .compatible = "technologic,ts7970-gpio",
  111. .data = (void *)TS7970_GPIO_IN,
  112. },
  113. { /* sentinel */ },
  114. };
  115. MODULE_DEVICE_TABLE(of, ts4900_gpio_of_match_table);
  116. static int ts4900_gpio_probe(struct i2c_client *client,
  117. const struct i2c_device_id *id)
  118. {
  119. struct ts4900_gpio_priv *priv;
  120. u32 ngpio;
  121. int ret;
  122. if (of_property_read_u32(client->dev.of_node, "ngpios", &ngpio))
  123. ngpio = DEFAULT_PIN_NUMBER;
  124. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  125. if (!priv)
  126. return -ENOMEM;
  127. priv->gpio_chip = template_chip;
  128. priv->gpio_chip.label = "ts4900-gpio";
  129. priv->gpio_chip.ngpio = ngpio;
  130. priv->gpio_chip.parent = &client->dev;
  131. priv->input_bit = (uintptr_t)of_device_get_match_data(&client->dev);
  132. priv->regmap = devm_regmap_init_i2c(client, &ts4900_regmap_config);
  133. if (IS_ERR(priv->regmap)) {
  134. ret = PTR_ERR(priv->regmap);
  135. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  136. ret);
  137. return ret;
  138. }
  139. ret = devm_gpiochip_add_data(&client->dev, &priv->gpio_chip, priv);
  140. if (ret < 0) {
  141. dev_err(&client->dev, "Unable to register gpiochip\n");
  142. return ret;
  143. }
  144. i2c_set_clientdata(client, priv);
  145. return 0;
  146. }
  147. static const struct i2c_device_id ts4900_gpio_id_table[] = {
  148. { "ts4900-gpio", },
  149. { /* sentinel */ }
  150. };
  151. MODULE_DEVICE_TABLE(i2c, ts4900_gpio_id_table);
  152. static struct i2c_driver ts4900_gpio_driver = {
  153. .driver = {
  154. .name = "ts4900-gpio",
  155. .of_match_table = ts4900_gpio_of_match_table,
  156. },
  157. .probe = ts4900_gpio_probe,
  158. .id_table = ts4900_gpio_id_table,
  159. };
  160. module_i2c_driver(ts4900_gpio_driver);
  161. MODULE_AUTHOR("Technologic Systems");
  162. MODULE_DESCRIPTION("GPIO interface for Technologic Systems I2C-FPGA core");
  163. MODULE_LICENSE("GPL");