2 * TI LMU (Lighting Management Unit) Core Driver
4 * Copyright 2017 Texas Instruments
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.
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/ti-lmu.h>
20 #include <linux/mfd/ti-lmu-register.h>
21 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/slab.h>
27 const struct mfd_cell *cells;
29 unsigned int max_register;
32 static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
35 gpiod_set_value(lmu->en_gpio, 1);
37 /* Delay about 1ms after HW enable pin control */
38 usleep_range(1000, 1500);
40 /* LM3631 has additional power up sequence - enable LCD_EN bit. */
42 return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL,
50 static void ti_lmu_disable_hw(void *data)
52 struct ti_lmu *lmu = data;
54 gpiod_set_value(lmu->en_gpio, 0);
57 #define LM363X_REGULATOR(_id) \
59 .name = "lm363x-regulator", \
61 .of_compatible = "ti,lm363x-regulator", \
64 static const struct mfd_cell lm3631_devices[] = {
65 LM363X_REGULATOR(LM3631_BOOST),
66 LM363X_REGULATOR(LM3631_LDO_CONT),
67 LM363X_REGULATOR(LM3631_LDO_OREF),
68 LM363X_REGULATOR(LM3631_LDO_POS),
69 LM363X_REGULATOR(LM3631_LDO_NEG),
71 .name = "ti-lmu-backlight",
73 .of_compatible = "ti,lm3631-backlight",
77 static const struct mfd_cell lm3632_devices[] = {
78 LM363X_REGULATOR(LM3632_BOOST),
79 LM363X_REGULATOR(LM3632_LDO_POS),
80 LM363X_REGULATOR(LM3632_LDO_NEG),
82 .name = "ti-lmu-backlight",
84 .of_compatible = "ti,lm3632-backlight",
88 static const struct mfd_cell lm3633_devices[] = {
90 .name = "ti-lmu-backlight",
92 .of_compatible = "ti,lm3633-backlight",
95 .name = "lm3633-leds",
96 .of_compatible = "ti,lm3633-leds",
98 /* Monitoring driver for open/short circuit detection */
100 .name = "ti-lmu-fault-monitor",
102 .of_compatible = "ti,lm3633-fault-monitor",
106 static const struct mfd_cell lm3695_devices[] = {
108 .name = "ti-lmu-backlight",
110 .of_compatible = "ti,lm3695-backlight",
114 static const struct mfd_cell lm3697_devices[] = {
116 .name = "ti-lmu-backlight",
118 .of_compatible = "ti,lm3697-backlight",
120 /* Monitoring driver for open/short circuit detection */
122 .name = "ti-lmu-fault-monitor",
124 .of_compatible = "ti,lm3697-fault-monitor",
128 #define TI_LMU_DATA(chip, max_reg) \
129 static const struct ti_lmu_data chip##_data = \
131 .cells = chip##_devices, \
132 .num_cells = ARRAY_SIZE(chip##_devices),\
133 .max_register = max_reg, \
136 TI_LMU_DATA(lm3631, LM3631_MAX_REG);
137 TI_LMU_DATA(lm3632, LM3632_MAX_REG);
138 TI_LMU_DATA(lm3633, LM3633_MAX_REG);
139 TI_LMU_DATA(lm3695, LM3695_MAX_REG);
140 TI_LMU_DATA(lm3697, LM3697_MAX_REG);
142 static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
144 struct device *dev = &cl->dev;
145 const struct ti_lmu_data *data;
146 struct regmap_config regmap_cfg;
151 * Get device specific data from of_match table.
152 * This data is defined by using TI_LMU_DATA() macro.
154 data = of_device_get_match_data(dev);
158 lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
165 memset(®map_cfg, 0, sizeof(struct regmap_config));
166 regmap_cfg.reg_bits = 8;
167 regmap_cfg.val_bits = 8;
168 regmap_cfg.name = id->name;
169 regmap_cfg.max_register = data->max_register;
171 lmu->regmap = devm_regmap_init_i2c(cl, ®map_cfg);
172 if (IS_ERR(lmu->regmap))
173 return PTR_ERR(lmu->regmap);
175 /* HW enable pin control and additional power up sequence if required */
176 lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
177 if (IS_ERR(lmu->en_gpio)) {
178 ret = PTR_ERR(lmu->en_gpio);
179 dev_err(dev, "Can not request enable GPIO: %d\n", ret);
183 ret = ti_lmu_enable_hw(lmu, id->driver_data);
187 ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
192 * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor.
193 * After fault detection is done, some devices should re-initialize
194 * configuration. The notifier enables such kind of handling.
196 BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
198 i2c_set_clientdata(cl, lmu);
200 return devm_mfd_add_devices(lmu->dev, 0, data->cells,
201 data->num_cells, NULL, 0, NULL);
204 static const struct of_device_id ti_lmu_of_match[] = {
205 { .compatible = "ti,lm3631", .data = &lm3631_data },
206 { .compatible = "ti,lm3632", .data = &lm3632_data },
207 { .compatible = "ti,lm3633", .data = &lm3633_data },
208 { .compatible = "ti,lm3695", .data = &lm3695_data },
209 { .compatible = "ti,lm3697", .data = &lm3697_data },
212 MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
214 static const struct i2c_device_id ti_lmu_ids[] = {
215 { "lm3631", LM3631 },
216 { "lm3632", LM3632 },
217 { "lm3633", LM3633 },
218 { "lm3695", LM3695 },
219 { "lm3697", LM3697 },
222 MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
224 static struct i2c_driver ti_lmu_driver = {
225 .probe = ti_lmu_probe,
228 .of_match_table = ti_lmu_of_match,
230 .id_table = ti_lmu_ids,
233 module_i2c_driver(ti_lmu_driver);
235 MODULE_DESCRIPTION("TI LMU MFD Core Driver");
236 MODULE_AUTHOR("Milo Kim");
237 MODULE_LICENSE("GPL v2");