gpio-xra1403.c 5.7 KB

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