gpio-ts4900.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Digital I/O driver for Technologic Systems I2C FPGA Core
  3. *
  4. * Copyright (C) 2015 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. return !(reg & TS4900_GPIO_OE);
  42. }
  43. static int ts4900_gpio_direction_input(struct gpio_chip *chip,
  44. unsigned int offset)
  45. {
  46. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  47. /*
  48. * This will clear the output enable bit, the other bits are
  49. * dontcare when this is cleared
  50. */
  51. return regmap_write(priv->regmap, offset, 0);
  52. }
  53. static int ts4900_gpio_direction_output(struct gpio_chip *chip,
  54. unsigned int offset, int value)
  55. {
  56. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  57. int ret;
  58. if (value)
  59. ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE |
  60. TS4900_GPIO_OUT);
  61. else
  62. ret = regmap_write(priv->regmap, offset, TS4900_GPIO_OE);
  63. return ret;
  64. }
  65. static int ts4900_gpio_get(struct gpio_chip *chip, unsigned int offset)
  66. {
  67. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  68. unsigned int reg;
  69. regmap_read(priv->regmap, offset, &reg);
  70. return !!(reg & priv->input_bit);
  71. }
  72. static void ts4900_gpio_set(struct gpio_chip *chip, unsigned int offset,
  73. int value)
  74. {
  75. struct ts4900_gpio_priv *priv = gpiochip_get_data(chip);
  76. if (value)
  77. regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT,
  78. TS4900_GPIO_OUT);
  79. else
  80. regmap_update_bits(priv->regmap, offset, TS4900_GPIO_OUT, 0);
  81. }
  82. static const struct regmap_config ts4900_regmap_config = {
  83. .reg_bits = 16,
  84. .val_bits = 8,
  85. };
  86. static const struct gpio_chip template_chip = {
  87. .label = "ts4900-gpio",
  88. .owner = THIS_MODULE,
  89. .get_direction = ts4900_gpio_get_direction,
  90. .direction_input = ts4900_gpio_direction_input,
  91. .direction_output = ts4900_gpio_direction_output,
  92. .get = ts4900_gpio_get,
  93. .set = ts4900_gpio_set,
  94. .base = -1,
  95. .can_sleep = true,
  96. };
  97. static const struct of_device_id ts4900_gpio_of_match_table[] = {
  98. {
  99. .compatible = "technologic,ts4900-gpio",
  100. .data = (void *)TS4900_GPIO_IN,
  101. }, {
  102. .compatible = "technologic,ts7970-gpio",
  103. .data = (void *)TS7970_GPIO_IN,
  104. },
  105. { /* sentinel */ },
  106. };
  107. MODULE_DEVICE_TABLE(of, ts4900_gpio_of_match_table);
  108. static int ts4900_gpio_probe(struct i2c_client *client,
  109. const struct i2c_device_id *id)
  110. {
  111. const struct of_device_id *match;
  112. struct ts4900_gpio_priv *priv;
  113. u32 ngpio;
  114. int ret;
  115. match = of_match_device(ts4900_gpio_of_match_table, &client->dev);
  116. if (!match)
  117. return -EINVAL;
  118. if (of_property_read_u32(client->dev.of_node, "ngpios", &ngpio))
  119. ngpio = DEFAULT_PIN_NUMBER;
  120. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  121. if (!priv)
  122. return -ENOMEM;
  123. priv->gpio_chip = template_chip;
  124. priv->gpio_chip.label = "ts4900-gpio";
  125. priv->gpio_chip.ngpio = ngpio;
  126. priv->gpio_chip.parent = &client->dev;
  127. priv->input_bit = (uintptr_t)match->data;
  128. priv->regmap = devm_regmap_init_i2c(client, &ts4900_regmap_config);
  129. if (IS_ERR(priv->regmap)) {
  130. ret = PTR_ERR(priv->regmap);
  131. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  132. ret);
  133. return ret;
  134. }
  135. ret = devm_gpiochip_add_data(&client->dev, &priv->gpio_chip, priv);
  136. if (ret < 0) {
  137. dev_err(&client->dev, "Unable to register gpiochip\n");
  138. return ret;
  139. }
  140. i2c_set_clientdata(client, priv);
  141. return 0;
  142. }
  143. static const struct i2c_device_id ts4900_gpio_id_table[] = {
  144. { "ts4900-gpio", },
  145. { /* sentinel */ }
  146. };
  147. MODULE_DEVICE_TABLE(i2c, ts4900_gpio_id_table);
  148. static struct i2c_driver ts4900_gpio_driver = {
  149. .driver = {
  150. .name = "ts4900-gpio",
  151. .of_match_table = ts4900_gpio_of_match_table,
  152. },
  153. .probe = ts4900_gpio_probe,
  154. .id_table = ts4900_gpio_id_table,
  155. };
  156. module_i2c_driver(ts4900_gpio_driver);
  157. MODULE_AUTHOR("Technologic Systems");
  158. MODULE_DESCRIPTION("GPIO interface for Technologic Systems I2C-FPGA core");
  159. MODULE_LICENSE("GPL");