lp3943.c 4.1 KB

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