gpio-lp3943.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/driver.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 int lp3943_gpio_request(struct gpio_chip *chip, unsigned offset)
  45. {
  46. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  47. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  48. /* Return an error if the pin is already assigned */
  49. if (test_and_set_bit(offset, &lp3943->pin_used))
  50. return -EBUSY;
  51. return 0;
  52. }
  53. static void lp3943_gpio_free(struct gpio_chip *chip, unsigned offset)
  54. {
  55. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  56. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  57. clear_bit(offset, &lp3943->pin_used);
  58. }
  59. static int lp3943_gpio_set_mode(struct lp3943_gpio *lp3943_gpio, u8 offset,
  60. u8 val)
  61. {
  62. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  63. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  64. return lp3943_update_bits(lp3943, mux[offset].reg, mux[offset].mask,
  65. val << mux[offset].shift);
  66. }
  67. static int lp3943_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
  68. {
  69. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  70. lp3943_gpio->input_mask |= BIT(offset);
  71. return lp3943_gpio_set_mode(lp3943_gpio, offset, LP3943_GPIO_IN);
  72. }
  73. static int lp3943_get_gpio_in_status(struct lp3943_gpio *lp3943_gpio,
  74. struct gpio_chip *chip, unsigned offset)
  75. {
  76. u8 addr, read;
  77. int err;
  78. switch (offset) {
  79. case LP3943_GPIO1 ... LP3943_GPIO8:
  80. addr = LP3943_REG_GPIO_A;
  81. break;
  82. case LP3943_GPIO9 ... LP3943_GPIO16:
  83. addr = LP3943_REG_GPIO_B;
  84. offset = offset - 8;
  85. break;
  86. default:
  87. return -EINVAL;
  88. }
  89. err = lp3943_read_byte(lp3943_gpio->lp3943, addr, &read);
  90. if (err)
  91. return err;
  92. return !!(read & BIT(offset));
  93. }
  94. static int lp3943_get_gpio_out_status(struct lp3943_gpio *lp3943_gpio,
  95. struct gpio_chip *chip, unsigned offset)
  96. {
  97. struct lp3943 *lp3943 = lp3943_gpio->lp3943;
  98. const struct lp3943_reg_cfg *mux = lp3943->mux_cfg;
  99. u8 read;
  100. int err;
  101. err = lp3943_read_byte(lp3943, mux[offset].reg, &read);
  102. if (err)
  103. return err;
  104. read = (read & mux[offset].mask) >> mux[offset].shift;
  105. if (read == LP3943_GPIO_OUT_HIGH)
  106. return 1;
  107. else if (read == LP3943_GPIO_OUT_LOW)
  108. return 0;
  109. else
  110. return -EINVAL;
  111. }
  112. static int lp3943_gpio_get(struct gpio_chip *chip, unsigned offset)
  113. {
  114. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  115. /*
  116. * Limitation:
  117. * LP3943 doesn't have the GPIO direction register. It provides
  118. * only input and output status registers.
  119. * So, direction info is required to handle the 'get' operation.
  120. * This variable is updated whenever the direction is changed and
  121. * it is used here.
  122. */
  123. if (lp3943_gpio->input_mask & BIT(offset))
  124. return lp3943_get_gpio_in_status(lp3943_gpio, chip, offset);
  125. else
  126. return lp3943_get_gpio_out_status(lp3943_gpio, chip, offset);
  127. }
  128. static void lp3943_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  129. {
  130. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  131. u8 data;
  132. if (value)
  133. data = LP3943_GPIO_OUT_HIGH;
  134. else
  135. data = LP3943_GPIO_OUT_LOW;
  136. lp3943_gpio_set_mode(lp3943_gpio, offset, data);
  137. }
  138. static int lp3943_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
  139. int value)
  140. {
  141. struct lp3943_gpio *lp3943_gpio = gpiochip_get_data(chip);
  142. lp3943_gpio_set(chip, offset, value);
  143. lp3943_gpio->input_mask &= ~BIT(offset);
  144. return 0;
  145. }
  146. static const struct gpio_chip lp3943_gpio_chip = {
  147. .label = "lp3943",
  148. .owner = THIS_MODULE,
  149. .request = lp3943_gpio_request,
  150. .free = lp3943_gpio_free,
  151. .direction_input = lp3943_gpio_direction_input,
  152. .get = lp3943_gpio_get,
  153. .direction_output = lp3943_gpio_direction_output,
  154. .set = lp3943_gpio_set,
  155. .base = -1,
  156. .ngpio = LP3943_MAX_GPIO,
  157. .can_sleep = 1,
  158. };
  159. static int lp3943_gpio_probe(struct platform_device *pdev)
  160. {
  161. struct lp3943 *lp3943 = dev_get_drvdata(pdev->dev.parent);
  162. struct lp3943_gpio *lp3943_gpio;
  163. lp3943_gpio = devm_kzalloc(&pdev->dev, sizeof(*lp3943_gpio),
  164. GFP_KERNEL);
  165. if (!lp3943_gpio)
  166. return -ENOMEM;
  167. lp3943_gpio->lp3943 = lp3943;
  168. lp3943_gpio->chip = lp3943_gpio_chip;
  169. lp3943_gpio->chip.parent = &pdev->dev;
  170. platform_set_drvdata(pdev, lp3943_gpio);
  171. return devm_gpiochip_add_data(&pdev->dev, &lp3943_gpio->chip,
  172. lp3943_gpio);
  173. }
  174. static const struct of_device_id lp3943_gpio_of_match[] = {
  175. { .compatible = "ti,lp3943-gpio", },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(of, lp3943_gpio_of_match);
  179. static struct platform_driver lp3943_gpio_driver = {
  180. .probe = lp3943_gpio_probe,
  181. .driver = {
  182. .name = "lp3943-gpio",
  183. .of_match_table = lp3943_gpio_of_match,
  184. },
  185. };
  186. module_platform_driver(lp3943_gpio_driver);
  187. MODULE_DESCRIPTION("LP3943 GPIO driver");
  188. MODULE_ALIAS("platform:lp3943-gpio");
  189. MODULE_AUTHOR("Milo Kim");
  190. MODULE_LICENSE("GPL");