gpio-ts4900.c 5.3 KB

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