ti-lmu.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * TI LMU (Lighting Management Unit) Core Driver
  4. *
  5. * Copyright 2017 Texas Instruments
  6. *
  7. * Author: Milo Kim <[email protected]>
  8. */
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/i2c.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/mfd/ti-lmu.h>
  16. #include <linux/mfd/ti-lmu-register.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/slab.h>
  21. struct ti_lmu_data {
  22. const struct mfd_cell *cells;
  23. int num_cells;
  24. unsigned int max_register;
  25. };
  26. static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
  27. {
  28. if (lmu->en_gpio)
  29. gpiod_set_value(lmu->en_gpio, 1);
  30. /* Delay about 1ms after HW enable pin control */
  31. usleep_range(1000, 1500);
  32. /* LM3631 has additional power up sequence - enable LCD_EN bit. */
  33. if (id == LM3631) {
  34. return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL,
  35. LM3631_LCD_EN_MASK,
  36. LM3631_LCD_EN_MASK);
  37. }
  38. return 0;
  39. }
  40. static void ti_lmu_disable_hw(void *data)
  41. {
  42. struct ti_lmu *lmu = data;
  43. if (lmu->en_gpio)
  44. gpiod_set_value(lmu->en_gpio, 0);
  45. }
  46. #define LM363X_REGULATOR(_id) \
  47. { \
  48. .name = "lm363x-regulator", \
  49. .id = _id, \
  50. .of_compatible = "ti,lm363x-regulator", \
  51. } \
  52. static const struct mfd_cell lm3631_devices[] = {
  53. LM363X_REGULATOR(LM3631_BOOST),
  54. LM363X_REGULATOR(LM3631_LDO_CONT),
  55. LM363X_REGULATOR(LM3631_LDO_OREF),
  56. LM363X_REGULATOR(LM3631_LDO_POS),
  57. LM363X_REGULATOR(LM3631_LDO_NEG),
  58. {
  59. .name = "ti-lmu-backlight",
  60. .id = LM3631,
  61. .of_compatible = "ti,lm3631-backlight",
  62. },
  63. };
  64. static const struct mfd_cell lm3632_devices[] = {
  65. LM363X_REGULATOR(LM3632_BOOST),
  66. LM363X_REGULATOR(LM3632_LDO_POS),
  67. LM363X_REGULATOR(LM3632_LDO_NEG),
  68. {
  69. .name = "ti-lmu-backlight",
  70. .id = LM3632,
  71. .of_compatible = "ti,lm3632-backlight",
  72. },
  73. };
  74. static const struct mfd_cell lm3633_devices[] = {
  75. {
  76. .name = "ti-lmu-backlight",
  77. .id = LM3633,
  78. .of_compatible = "ti,lm3633-backlight",
  79. },
  80. {
  81. .name = "lm3633-leds",
  82. .of_compatible = "ti,lm3633-leds",
  83. },
  84. /* Monitoring driver for open/short circuit detection */
  85. {
  86. .name = "ti-lmu-fault-monitor",
  87. .id = LM3633,
  88. .of_compatible = "ti,lm3633-fault-monitor",
  89. },
  90. };
  91. static const struct mfd_cell lm3695_devices[] = {
  92. {
  93. .name = "ti-lmu-backlight",
  94. .id = LM3695,
  95. .of_compatible = "ti,lm3695-backlight",
  96. },
  97. };
  98. static const struct mfd_cell lm36274_devices[] = {
  99. LM363X_REGULATOR(LM36274_BOOST),
  100. LM363X_REGULATOR(LM36274_LDO_POS),
  101. LM363X_REGULATOR(LM36274_LDO_NEG),
  102. {
  103. .name = "lm36274-leds",
  104. .id = LM36274,
  105. .of_compatible = "ti,lm36274-backlight",
  106. },
  107. };
  108. #define TI_LMU_DATA(chip, max_reg) \
  109. static const struct ti_lmu_data chip##_data = \
  110. { \
  111. .cells = chip##_devices, \
  112. .num_cells = ARRAY_SIZE(chip##_devices),\
  113. .max_register = max_reg, \
  114. } \
  115. TI_LMU_DATA(lm3631, LM3631_MAX_REG);
  116. TI_LMU_DATA(lm3632, LM3632_MAX_REG);
  117. TI_LMU_DATA(lm3633, LM3633_MAX_REG);
  118. TI_LMU_DATA(lm3695, LM3695_MAX_REG);
  119. TI_LMU_DATA(lm36274, LM36274_MAX_REG);
  120. static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
  121. {
  122. struct device *dev = &cl->dev;
  123. const struct ti_lmu_data *data;
  124. struct regmap_config regmap_cfg;
  125. struct ti_lmu *lmu;
  126. int ret;
  127. /*
  128. * Get device specific data from of_match table.
  129. * This data is defined by using TI_LMU_DATA() macro.
  130. */
  131. data = of_device_get_match_data(dev);
  132. if (!data)
  133. return -ENODEV;
  134. lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
  135. if (!lmu)
  136. return -ENOMEM;
  137. lmu->dev = &cl->dev;
  138. /* Setup regmap */
  139. memset(&regmap_cfg, 0, sizeof(struct regmap_config));
  140. regmap_cfg.reg_bits = 8;
  141. regmap_cfg.val_bits = 8;
  142. regmap_cfg.name = id->name;
  143. regmap_cfg.max_register = data->max_register;
  144. lmu->regmap = devm_regmap_init_i2c(cl, &regmap_cfg);
  145. if (IS_ERR(lmu->regmap))
  146. return PTR_ERR(lmu->regmap);
  147. /* HW enable pin control and additional power up sequence if required */
  148. lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
  149. if (IS_ERR(lmu->en_gpio)) {
  150. ret = PTR_ERR(lmu->en_gpio);
  151. dev_err(dev, "Can not request enable GPIO: %d\n", ret);
  152. return ret;
  153. }
  154. ret = ti_lmu_enable_hw(lmu, id->driver_data);
  155. if (ret)
  156. return ret;
  157. ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
  158. if (ret)
  159. return ret;
  160. /*
  161. * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor.
  162. * After fault detection is done, some devices should re-initialize
  163. * configuration. The notifier enables such kind of handling.
  164. */
  165. BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
  166. i2c_set_clientdata(cl, lmu);
  167. return devm_mfd_add_devices(lmu->dev, 0, data->cells,
  168. data->num_cells, NULL, 0, NULL);
  169. }
  170. static const struct of_device_id ti_lmu_of_match[] = {
  171. { .compatible = "ti,lm3631", .data = &lm3631_data },
  172. { .compatible = "ti,lm3632", .data = &lm3632_data },
  173. { .compatible = "ti,lm3633", .data = &lm3633_data },
  174. { .compatible = "ti,lm3695", .data = &lm3695_data },
  175. { .compatible = "ti,lm36274", .data = &lm36274_data },
  176. { }
  177. };
  178. MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
  179. static const struct i2c_device_id ti_lmu_ids[] = {
  180. { "lm3631", LM3631 },
  181. { "lm3632", LM3632 },
  182. { "lm3633", LM3633 },
  183. { "lm3695", LM3695 },
  184. { "lm36274", LM36274 },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
  188. static struct i2c_driver ti_lmu_driver = {
  189. .probe = ti_lmu_probe,
  190. .driver = {
  191. .name = "ti-lmu",
  192. .of_match_table = ti_lmu_of_match,
  193. },
  194. .id_table = ti_lmu_ids,
  195. };
  196. module_i2c_driver(ti_lmu_driver);
  197. MODULE_DESCRIPTION("TI LMU MFD Core Driver");
  198. MODULE_AUTHOR("Milo Kim");
  199. MODULE_LICENSE("GPL v2");