lp3943.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI/National Semiconductor LP3943 MFD Core Driver
  4. *
  5. * Copyright 2013 Texas Instruments
  6. *
  7. * Author: Milo Kim <milo.kim@ti.com>
  8. *
  9. * Driver structure:
  10. * LP3943 is an integrated device capable of driving 16 output channels.
  11. * It can be used for a GPIO expander and PWM generators.
  12. *
  13. * LED control General usage for a device
  14. * ___________ ____________________________
  15. *
  16. * LP3943 MFD ---- GPIO expander leds-gpio eg) HW enable pin
  17. * |
  18. * --- PWM generator leds-pwm eg) PWM input
  19. *
  20. * Internal two PWM channels are used for LED dimming effect.
  21. * And each output pin can be used as a GPIO as well.
  22. * The LED functionality can work with GPIOs or PWMs.
  23. * LEDs can be controlled with legacy leds-gpio(static brightness) or
  24. * leds-pwm drivers(dynamic brightness control).
  25. * Alternatively, it can be used for generic GPIO and PWM controller.
  26. * For example, a GPIO is HW enable pin of a device.
  27. * A PWM is input pin of a backlight device.
  28. */
  29. #include <linux/err.h>
  30. #include <linux/gpio.h>
  31. #include <linux/i2c.h>
  32. #include <linux/mfd/core.h>
  33. #include <linux/mfd/lp3943.h>
  34. #include <linux/module.h>
  35. #include <linux/of.h>
  36. #include <linux/slab.h>
  37. #define LP3943_MAX_REGISTERS 0x09
  38. /* Register configuration for pin MUX */
  39. static const struct lp3943_reg_cfg lp3943_mux_cfg[] = {
  40. /* address, mask, shift */
  41. { LP3943_REG_MUX0, 0x03, 0 },
  42. { LP3943_REG_MUX0, 0x0C, 2 },
  43. { LP3943_REG_MUX0, 0x30, 4 },
  44. { LP3943_REG_MUX0, 0xC0, 6 },
  45. { LP3943_REG_MUX1, 0x03, 0 },
  46. { LP3943_REG_MUX1, 0x0C, 2 },
  47. { LP3943_REG_MUX1, 0x30, 4 },
  48. { LP3943_REG_MUX1, 0xC0, 6 },
  49. { LP3943_REG_MUX2, 0x03, 0 },
  50. { LP3943_REG_MUX2, 0x0C, 2 },
  51. { LP3943_REG_MUX2, 0x30, 4 },
  52. { LP3943_REG_MUX2, 0xC0, 6 },
  53. { LP3943_REG_MUX3, 0x03, 0 },
  54. { LP3943_REG_MUX3, 0x0C, 2 },
  55. { LP3943_REG_MUX3, 0x30, 4 },
  56. { LP3943_REG_MUX3, 0xC0, 6 },
  57. };
  58. static const struct mfd_cell lp3943_devs[] = {
  59. {
  60. .name = "lp3943-pwm",
  61. .of_compatible = "ti,lp3943-pwm",
  62. },
  63. {
  64. .name = "lp3943-gpio",
  65. .of_compatible = "ti,lp3943-gpio",
  66. },
  67. };
  68. int lp3943_read_byte(struct lp3943 *lp3943, u8 reg, u8 *read)
  69. {
  70. int ret;
  71. unsigned int val;
  72. ret = regmap_read(lp3943->regmap, reg, &val);
  73. if (ret < 0)
  74. return ret;
  75. *read = (u8)val;
  76. return 0;
  77. }
  78. EXPORT_SYMBOL_GPL(lp3943_read_byte);
  79. int lp3943_write_byte(struct lp3943 *lp3943, u8 reg, u8 data)
  80. {
  81. return regmap_write(lp3943->regmap, reg, data);
  82. }
  83. EXPORT_SYMBOL_GPL(lp3943_write_byte);
  84. int lp3943_update_bits(struct lp3943 *lp3943, u8 reg, u8 mask, u8 data)
  85. {
  86. return regmap_update_bits(lp3943->regmap, reg, mask, data);
  87. }
  88. EXPORT_SYMBOL_GPL(lp3943_update_bits);
  89. static const struct regmap_config lp3943_regmap_config = {
  90. .reg_bits = 8,
  91. .val_bits = 8,
  92. .max_register = LP3943_MAX_REGISTERS,
  93. };
  94. static int lp3943_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  95. {
  96. struct lp3943 *lp3943;
  97. struct device *dev = &cl->dev;
  98. lp3943 = devm_kzalloc(dev, sizeof(*lp3943), GFP_KERNEL);
  99. if (!lp3943)
  100. return -ENOMEM;
  101. lp3943->regmap = devm_regmap_init_i2c(cl, &lp3943_regmap_config);
  102. if (IS_ERR(lp3943->regmap))
  103. return PTR_ERR(lp3943->regmap);
  104. lp3943->pdata = dev_get_platdata(dev);
  105. lp3943->dev = dev;
  106. lp3943->mux_cfg = lp3943_mux_cfg;
  107. i2c_set_clientdata(cl, lp3943);
  108. return devm_mfd_add_devices(dev, -1, lp3943_devs,
  109. ARRAY_SIZE(lp3943_devs),
  110. NULL, 0, NULL);
  111. }
  112. static const struct i2c_device_id lp3943_ids[] = {
  113. { "lp3943", 0 },
  114. { }
  115. };
  116. MODULE_DEVICE_TABLE(i2c, lp3943_ids);
  117. #ifdef CONFIG_OF
  118. static const struct of_device_id lp3943_of_match[] = {
  119. { .compatible = "ti,lp3943", },
  120. { }
  121. };
  122. MODULE_DEVICE_TABLE(of, lp3943_of_match);
  123. #endif
  124. static struct i2c_driver lp3943_driver = {
  125. .probe = lp3943_probe,
  126. .driver = {
  127. .name = "lp3943",
  128. .of_match_table = of_match_ptr(lp3943_of_match),
  129. },
  130. .id_table = lp3943_ids,
  131. };
  132. module_i2c_driver(lp3943_driver);
  133. MODULE_DESCRIPTION("LP3943 MFD Core Driver");
  134. MODULE_AUTHOR("Milo Kim");
  135. MODULE_LICENSE("GPL");