Linux 5.1.15
[linux/fpc-iii.git] / drivers / mfd / ti-lmu.c
blob37d0bdb291c32fa5c1fe138b082a58af41d694dc
1 /*
2 * TI LMU (Lighting Management Unit) Core Driver
4 * Copyright 2017 Texas Instruments
6 * Author: Milo Kim <milo.kim@ti.com>
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>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/slab.h>
26 struct ti_lmu_data {
27 const struct mfd_cell *cells;
28 int num_cells;
29 unsigned int max_register;
32 static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
34 if (lmu->en_gpio)
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. */
41 if (id == LM3631) {
42 return regmap_update_bits(lmu->regmap, LM3631_REG_DEVCTRL,
43 LM3631_LCD_EN_MASK,
44 LM3631_LCD_EN_MASK);
47 return 0;
50 static void ti_lmu_disable_hw(void *data)
52 struct ti_lmu *lmu = data;
53 if (lmu->en_gpio)
54 gpiod_set_value(lmu->en_gpio, 0);
57 static const struct mfd_cell lm3532_devices[] = {
59 .name = "ti-lmu-backlight",
60 .id = LM3532,
61 .of_compatible = "ti,lm3532-backlight",
65 #define LM363X_REGULATOR(_id) \
66 { \
67 .name = "lm363x-regulator", \
68 .id = _id, \
69 .of_compatible = "ti,lm363x-regulator", \
70 } \
72 static const struct mfd_cell lm3631_devices[] = {
73 LM363X_REGULATOR(LM3631_BOOST),
74 LM363X_REGULATOR(LM3631_LDO_CONT),
75 LM363X_REGULATOR(LM3631_LDO_OREF),
76 LM363X_REGULATOR(LM3631_LDO_POS),
77 LM363X_REGULATOR(LM3631_LDO_NEG),
79 .name = "ti-lmu-backlight",
80 .id = LM3631,
81 .of_compatible = "ti,lm3631-backlight",
85 static const struct mfd_cell lm3632_devices[] = {
86 LM363X_REGULATOR(LM3632_BOOST),
87 LM363X_REGULATOR(LM3632_LDO_POS),
88 LM363X_REGULATOR(LM3632_LDO_NEG),
90 .name = "ti-lmu-backlight",
91 .id = LM3632,
92 .of_compatible = "ti,lm3632-backlight",
96 static const struct mfd_cell lm3633_devices[] = {
98 .name = "ti-lmu-backlight",
99 .id = LM3633,
100 .of_compatible = "ti,lm3633-backlight",
103 .name = "lm3633-leds",
104 .of_compatible = "ti,lm3633-leds",
106 /* Monitoring driver for open/short circuit detection */
108 .name = "ti-lmu-fault-monitor",
109 .id = LM3633,
110 .of_compatible = "ti,lm3633-fault-monitor",
114 static const struct mfd_cell lm3695_devices[] = {
116 .name = "ti-lmu-backlight",
117 .id = LM3695,
118 .of_compatible = "ti,lm3695-backlight",
122 static const struct mfd_cell lm3697_devices[] = {
124 .name = "ti-lmu-backlight",
125 .id = LM3697,
126 .of_compatible = "ti,lm3697-backlight",
128 /* Monitoring driver for open/short circuit detection */
130 .name = "ti-lmu-fault-monitor",
131 .id = LM3697,
132 .of_compatible = "ti,lm3697-fault-monitor",
136 #define TI_LMU_DATA(chip, max_reg) \
137 static const struct ti_lmu_data chip##_data = \
139 .cells = chip##_devices, \
140 .num_cells = ARRAY_SIZE(chip##_devices),\
141 .max_register = max_reg, \
144 TI_LMU_DATA(lm3532, LM3532_MAX_REG);
145 TI_LMU_DATA(lm3631, LM3631_MAX_REG);
146 TI_LMU_DATA(lm3632, LM3632_MAX_REG);
147 TI_LMU_DATA(lm3633, LM3633_MAX_REG);
148 TI_LMU_DATA(lm3695, LM3695_MAX_REG);
149 TI_LMU_DATA(lm3697, LM3697_MAX_REG);
151 static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
153 struct device *dev = &cl->dev;
154 const struct ti_lmu_data *data;
155 struct regmap_config regmap_cfg;
156 struct ti_lmu *lmu;
157 int ret;
160 * Get device specific data from of_match table.
161 * This data is defined by using TI_LMU_DATA() macro.
163 data = of_device_get_match_data(dev);
164 if (!data)
165 return -ENODEV;
167 lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
168 if (!lmu)
169 return -ENOMEM;
171 lmu->dev = &cl->dev;
173 /* Setup regmap */
174 memset(&regmap_cfg, 0, sizeof(struct regmap_config));
175 regmap_cfg.reg_bits = 8;
176 regmap_cfg.val_bits = 8;
177 regmap_cfg.name = id->name;
178 regmap_cfg.max_register = data->max_register;
180 lmu->regmap = devm_regmap_init_i2c(cl, &regmap_cfg);
181 if (IS_ERR(lmu->regmap))
182 return PTR_ERR(lmu->regmap);
184 /* HW enable pin control and additional power up sequence if required */
185 lmu->en_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH);
186 if (IS_ERR(lmu->en_gpio)) {
187 ret = PTR_ERR(lmu->en_gpio);
188 dev_err(dev, "Can not request enable GPIO: %d\n", ret);
189 return ret;
192 ret = ti_lmu_enable_hw(lmu, id->driver_data);
193 if (ret)
194 return ret;
196 ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
197 if (ret)
198 return ret;
201 * Fault circuit(open/short) can be detected by ti-lmu-fault-monitor.
202 * After fault detection is done, some devices should re-initialize
203 * configuration. The notifier enables such kind of handling.
205 BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
207 i2c_set_clientdata(cl, lmu);
209 return devm_mfd_add_devices(lmu->dev, 0, data->cells,
210 data->num_cells, NULL, 0, NULL);
213 static const struct of_device_id ti_lmu_of_match[] = {
214 { .compatible = "ti,lm3532", .data = &lm3532_data },
215 { .compatible = "ti,lm3631", .data = &lm3631_data },
216 { .compatible = "ti,lm3632", .data = &lm3632_data },
217 { .compatible = "ti,lm3633", .data = &lm3633_data },
218 { .compatible = "ti,lm3695", .data = &lm3695_data },
219 { .compatible = "ti,lm3697", .data = &lm3697_data },
222 MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
224 static const struct i2c_device_id ti_lmu_ids[] = {
225 { "lm3532", LM3532 },
226 { "lm3631", LM3631 },
227 { "lm3632", LM3632 },
228 { "lm3633", LM3633 },
229 { "lm3695", LM3695 },
230 { "lm3697", LM3697 },
233 MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
235 static struct i2c_driver ti_lmu_driver = {
236 .probe = ti_lmu_probe,
237 .driver = {
238 .name = "ti-lmu",
239 .of_match_table = ti_lmu_of_match,
241 .id_table = ti_lmu_ids,
244 module_i2c_driver(ti_lmu_driver);
246 MODULE_DESCRIPTION("TI LMU MFD Core Driver");
247 MODULE_AUTHOR("Milo Kim");
248 MODULE_LICENSE("GPL v2");