gpio-lp3943.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI/National Semiconductor LP3943 GPIO driver
  4. *
  5. * Copyright 2013 Texas Instruments
  6. *
  7. * Author: Milo Kim <milo.kim@ti.com>
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/i2c.h>
  13. #include <linux/mfd/lp3943.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. enum lp3943_gpios {
  18. LP3943_GPIO1,
  19. LP3943_GPIO2,
  20. LP3943_GPIO3,
  21. LP3943_GPIO4,
  22. LP3943_GPIO5,
  23. LP3943_GPIO6,
  24. LP3943_GPIO7,
  25. LP3943_GPIO8,
  26. LP3943_GPIO9,
  27. LP3943_GPIO10,
  28. LP3943_GPIO11,
  29. LP3943_GPIO12,
  30. LP3943_GPIO13,
  31. LP3943_GPIO14,
  32. LP3943_GPIO15,
  33. LP3943_GPIO16,
  34. LP3943_MAX_GPIO,
  35. };
  36. struct lp3943_gpio {
  37. struct gpio_chip chip;
  38. struct lp3943 *lp3943;
  39. u16 input_mask; /* 1 = GPIO is input direction, 0 = output */
  40. };
  41. static int lp3943_gpio_request(struct gpio_chip *chip, unsigned offset)
  42. {
  43. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  44. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  45. /* Return an error if the pin is already assigned */
  46. if (test_and_set_bit(offset, &lp3943->pin_used))
  47. return -EBUSY;
  48. return 0;
  49. }
  50. static void lp3943_gpio_free(struct gpio_chip *chip, unsigned offset)
  51. {
  52. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  53. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  54. clear_bit(offset, &lp3943->pin_used);
  55. }
  56. static int lp3943_gpio_set_mode(struct lp3943_gpio *lp3943_gpio, u8 offset,
  57. u8 val)
  58. {
  59. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  60. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  61. return lp3943_update_bits(lp3943, mux[offset].reg, mux[offset].mask,
  62. val << mux[offset].shift);
  63. }
  64. static int lp3943_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  65. {
  66. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  67. lp3943_gpio->input_mask |= BIT(offset);
  68. return lp3943_gpio_set_mode(lp3943_gpio, offset, LP3943_GPIO_IN);
  69. }
  70. static int lp3943_get_gpio_in_status(struct lp3943_gpio *lp3943_gpio,
  71. struct gpio_chip *chip, unsigned offset)
  72. {
  73. u8 addr, read;
  74. int err;
  75. switch (offset) {
  76. case LP3943_GPIO1 ... LP3943_GPIO8:
  77. addr = LP3943_REG_GPIO_A;
  78. break;
  79. case LP3943_GPIO9 ... LP3943_GPIO16:
  80. addr = LP3943_REG_GPIO_B;
  81. offset = offset - 8;
  82. break;
  83. default:
  84. return -EINVAL;
  85. }
  86. err = lp3943_read_byte(lp3943_gpio->lp3943, addr, &read);
  87. if (err)
  88. return err;
  89. return !!(read & BIT(offset));
  90. }
  91. static int lp3943_get_gpio_out_status(struct lp3943_gpio *lp3943_gpio,
  92. struct gpio_chip *chip, unsigned offset)
  93. {
  94. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  95. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  96. u8 read;
  97. int err;
  98. err = lp3943_read_byte(lp3943, mux[offset].reg, &read);
  99. if (err)
  100. return err;
  101. read = (read & mux[offset].mask) >> mux[offset].shift;
  102. if (read == LP3943_GPIO_OUT_HIGH)
  103. return 1;
  104. else if (read == LP3943_GPIO_OUT_LOW)
  105. return 0;
  106. else
  107. return -EINVAL;
  108. }
  109. static int lp3943_gpio_get(struct gpio_chip *chip, unsigned offset)
  110. {
  111. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  112. /*
  113. * Limitation:
  114. * LP3943 doesn't have the GPIO direction register. It provides
  115. * only input and output status registers.
  116. * So, direction info is required to handle the 'get' operation.
  117. * This variable is updated whenever the direction is changed and
  118. * it is used here.
  119. */
  120. if (lp3943_gpio->input_mask & BIT(offset))
  121. return lp3943_get_gpio_in_status(lp3943_gpio, chip, offset);
  122. else
  123. return lp3943_get_gpio_out_status(lp3943_gpio, chip, offset);
  124. }
  125. static void lp3943_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  126. {
  127. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  128. u8 data;
  129. if (value)
  130. data = LP3943_GPIO_OUT_HIGH;
  131. else
  132. data = LP3943_GPIO_OUT_LOW;
  133. lp3943_gpio_set_mode(lp3943_gpio, offset, data);
  134. }
  135. static int lp3943_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  136. int value)
  137. {
  138. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  139. lp3943_gpio_set(chip, offset, value);
  140. lp3943_gpio->input_mask &= ~BIT(offset);
  141. return 0;
  142. }
  143. static const struct gpio_chip lp3943_gpio_chip = {
  144. .label = "lp3943",
  145. .owner = THIS_MODULE,
  146. .request = lp3943_gpio_request,
  147. .free = lp3943_gpio_free,
  148. .direction_input = lp3943_gpio_direction_input,
  149. .get = lp3943_gpio_get,
  150. .direction_output = lp3943_gpio_direction_output,
  151. .set = lp3943_gpio_set,
  152. .base = -1,
  153. .ngpio = LP3943_MAX_GPIO,
  154. .can_sleep = 1,
  155. };
  156. static int lp3943_gpio_probe(struct platform_device *pdev)
  157. {
  158. struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
  159. struct lp3943_gpio *lp3943_gpio;
  160. lp3943_gpio = devm_kzalloc(&pdev->dev, sizeof(*lp3943_gpio),
  161. GFP_KERNEL);
  162. if (!lp3943_gpio)
  163. return -ENOMEM;
  164. lp3943_gpio->lp3943 = lp3943;
  165. lp3943_gpio->chip = lp3943_gpio_chip;
  166. lp3943_gpio->chip.parent = &pdev->dev;
  167. platform_set_drvdata(pdev, lp3943_gpio);
  168. return devm_gpiochip_add_data(&pdev->dev, &lp3943_gpio->chip,
  169. lp3943_gpio);
  170. }
  171. static const struct of_device_id lp3943_gpio_of_match[] = {
  172. { .compatible = "ti,lp3943-gpio", },
  173. { }
  174. };
  175. MODULE_DEVICE_TABLE(of, lp3943_gpio_of_match);
  176. static struct platform_driver lp3943_gpio_driver = {
  177. .probe = lp3943_gpio_probe,
  178. .driver = {
  179. .name = "lp3943-gpio",
  180. .of_match_table = lp3943_gpio_of_match,
  181. },
  182. };
  183. module_platform_driver(lp3943_gpio_driver);
  184. MODULE_DESCRIPTION("LP3943 GPIO driver");
  185. MODULE_ALIAS("platform:lp3943-gpio");
  186. MODULE_AUTHOR("Milo Kim");
  187. MODULE_LICENSE("GPL");