gpio-xra1403.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * GPIO driver for EXAR XRA1403 16-bit GPIO expander
  3. *
  4. * Copyright (c) 2017, General Electric Company
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/gpio/driver.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_gpio.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/spi/spi.h>
  27. #include <linux/regmap.h>
  28. /* XRA1403 registers */
  29. #define XRA_GSR 0x00 /* GPIO State */
  30. #define XRA_OCR 0x02 /* Output Control */
  31. #define XRA_PIR 0x04 /* Input Polarity Inversion */
  32. #define XRA_GCR 0x06 /* GPIO Configuration */
  33. #define XRA_PUR 0x08 /* Input Internal Pull-up Resistor Enable/Disable */
  34. #define XRA_IER 0x0A /* Input Interrupt Enable */
  35. #define XRA_TSCR 0x0C /* Output Three-State Control */
  36. #define XRA_ISR 0x0E /* Input Interrupt Status */
  37. #define XRA_REIR 0x10 /* Input Rising Edge Interrupt Enable */
  38. #define XRA_FEIR 0x12 /* Input Falling Edge Interrupt Enable */
  39. #define XRA_IFR 0x14 /* Input Filter Enable/Disable */
  40. #define XRA_LAST 0x15 /* Bounds */
  41. struct xra1403 {
  42. struct gpio_chip chip;
  43. struct regmap *regmap;
  44. };
  45. static const struct regmap_config xra1403_regmap_cfg = {
  46. .reg_bits = 7,
  47. .pad_bits = 1,
  48. .val_bits = 8,
  49. .max_register = XRA_LAST,
  50. };
  51. static unsigned int to_reg(unsigned int reg, unsigned int offset)
  52. {
  53. return reg + (offset > 7);
  54. }
  55. static int xra1403_direction_input(struct gpio_chip *chip, unsigned int offset)
  56. {
  57. struct xra1403 *xra = gpiochip_get_data(chip);
  58. return regmap_update_bits(xra->regmap, to_reg(XRA_GCR, offset),
  59. BIT(offset % 8), BIT(offset % 8));
  60. }
  61. static int xra1403_direction_output(struct gpio_chip *chip, unsigned int offset,
  62. int value)
  63. {
  64. int ret;
  65. struct xra1403 *xra = gpiochip_get_data(chip);
  66. ret = regmap_update_bits(xra->regmap, to_reg(XRA_GCR, offset),
  67. BIT(offset % 8), 0);
  68. if (ret)
  69. return ret;
  70. ret = regmap_update_bits(xra->regmap, to_reg(XRA_OCR, offset),
  71. BIT(offset % 8), value ? BIT(offset % 8) : 0);
  72. return ret;
  73. }
  74. static int xra1403_get_direction(struct gpio_chip *chip, unsigned int offset)
  75. {
  76. int ret;
  77. unsigned int val;
  78. struct xra1403 *xra = gpiochip_get_data(chip);
  79. ret = regmap_read(xra->regmap, to_reg(XRA_GCR, offset), &val);
  80. if (ret)
  81. return ret;
  82. return !!(val & BIT(offset % 8));
  83. }
  84. static int xra1403_get(struct gpio_chip *chip, unsigned int offset)
  85. {
  86. int ret;
  87. unsigned int val;
  88. struct xra1403 *xra = gpiochip_get_data(chip);
  89. ret = regmap_read(xra->regmap, to_reg(XRA_GSR, offset), &val);
  90. if (ret)
  91. return ret;
  92. return !!(val & BIT(offset % 8));
  93. }
  94. static void xra1403_set(struct gpio_chip *chip, unsigned int offset, int value)
  95. {
  96. int ret;
  97. struct xra1403 *xra = gpiochip_get_data(chip);
  98. ret = regmap_update_bits(xra->regmap, to_reg(XRA_OCR, offset),
  99. BIT(offset % 8), value ? BIT(offset % 8) : 0);
  100. if (ret)
  101. dev_err(chip->parent, "Failed to set pin: %d, ret: %d\n",
  102. offset, ret);
  103. }
  104. #ifdef CONFIG_DEBUG_FS
  105. static void xra1403_dbg_show(struct seq_file *s, struct gpio_chip *chip)
  106. {
  107. int reg;
  108. struct xra1403 *xra = gpiochip_get_data(chip);
  109. int value[XRA_LAST];
  110. int i;
  111. unsigned int gcr;
  112. unsigned int gsr;
  113. seq_puts(s, "xra reg:");
  114. for (reg = 0; reg <= XRA_LAST; reg++)
  115. seq_printf(s, " %2.2x", reg);
  116. seq_puts(s, "\n value:");
  117. for (reg = 0; reg < XRA_LAST; reg++) {
  118. regmap_read(xra->regmap, reg, &value[reg]);
  119. seq_printf(s, " %2.2x", value[reg]);
  120. }
  121. seq_puts(s, "\n");
  122. gcr = value[XRA_GCR + 1] << 8 | value[XRA_GCR];
  123. gsr = value[XRA_GSR + 1] << 8 | value[XRA_GSR];
  124. for (i = 0; i < chip->ngpio; i++) {
  125. const char *label = gpiochip_is_requested(chip, i);
  126. if (!label)
  127. continue;
  128. seq_printf(s, " gpio-%-3d (%-12s) %s %s\n",
  129. chip->base + i, label,
  130. (gcr & BIT(i)) ? "in" : "out",
  131. (gsr & BIT(i)) ? "hi" : "lo");
  132. }
  133. }
  134. #else
  135. #define xra1403_dbg_show NULL
  136. #endif
  137. static int xra1403_probe(struct spi_device *spi)
  138. {
  139. struct xra1403 *xra;
  140. struct gpio_desc *reset_gpio;
  141. int ret;
  142. xra = devm_kzalloc(&spi->dev, sizeof(*xra), GFP_KERNEL);
  143. if (!xra)
  144. return -ENOMEM;
  145. /* bring the chip out of reset if reset pin is provided*/
  146. reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", GPIOD_OUT_LOW);
  147. if (IS_ERR(reset_gpio))
  148. dev_warn(&spi->dev, "Could not get reset-gpios\n");
  149. xra->chip.direction_input = xra1403_direction_input;
  150. xra->chip.direction_output = xra1403_direction_output;
  151. xra->chip.get_direction = xra1403_get_direction;
  152. xra->chip.get = xra1403_get;
  153. xra->chip.set = xra1403_set;
  154. xra->chip.dbg_show = xra1403_dbg_show;
  155. xra->chip.ngpio = 16;
  156. xra->chip.label = "xra1403";
  157. xra->chip.base = -1;
  158. xra->chip.can_sleep = true;
  159. xra->chip.parent = &spi->dev;
  160. xra->chip.owner = THIS_MODULE;
  161. xra->regmap = devm_regmap_init_spi(spi, &xra1403_regmap_cfg);
  162. if (IS_ERR(xra->regmap)) {
  163. ret = PTR_ERR(xra->regmap);
  164. dev_err(&spi->dev, "Failed to allocate regmap: %d\n", ret);
  165. return ret;
  166. }
  167. ret = devm_gpiochip_add_data(&spi->dev, &xra->chip, xra);
  168. if (ret < 0) {
  169. dev_err(&spi->dev, "Unable to register gpiochip\n");
  170. return ret;
  171. }
  172. spi_set_drvdata(spi, xra);
  173. return 0;
  174. }
  175. static const struct spi_device_id xra1403_ids[] = {
  176. { "xra1403" },
  177. {},
  178. };
  179. MODULE_DEVICE_TABLE(spi, xra1403_ids);
  180. static const struct of_device_id xra1403_spi_of_match[] = {
  181. { .compatible = "exar,xra1403" },
  182. {},
  183. };
  184. MODULE_DEVICE_TABLE(of, xra1403_spi_of_match);
  185. static struct spi_driver xra1403_driver = {
  186. .probe = xra1403_probe,
  187. .id_table = xra1403_ids,
  188. .driver = {
  189. .name = "xra1403",
  190. .of_match_table = of_match_ptr(xra1403_spi_of_match),
  191. },
  192. };
  193. module_spi_driver(xra1403_driver);
  194. MODULE_AUTHOR("Nandor Han <nandor.han@ge.com>");
  195. MODULE_AUTHOR("Semi Malinen <semi.malinen@ge.com>");
  196. MODULE_DESCRIPTION("GPIO expander driver for EXAR XRA1403");
  197. MODULE_LICENSE("GPL v2");