gpio-lp3943.c 5.7 KB

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