ti-lmu.c 6.3 KB

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