xref: /linux/drivers/mfd/ti-lmu.c (revision 18f90d372cf35b387663f1567de701e5393f6eb5)
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 
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>
25 
26 struct ti_lmu_data {
27 	const struct mfd_cell *cells;
28 	int num_cells;
29 	unsigned int max_register;
30 };
31 
32 static int ti_lmu_enable_hw(struct ti_lmu *lmu, enum ti_lmu_id id)
33 {
34 	if (lmu->en_gpio)
35 		gpiod_set_value(lmu->en_gpio, 1);
36 
37 	/* Delay about 1ms after HW enable pin control */
38 	usleep_range(1000, 1500);
39 
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);
45 	}
46 
47 	return 0;
48 }
49 
50 static void ti_lmu_disable_hw(void *data)
51 {
52 	struct ti_lmu *lmu = data;
53 	if (lmu->en_gpio)
54 		gpiod_set_value(lmu->en_gpio, 0);
55 }
56 
57 static const struct mfd_cell lm3532_devices[] = {
58 	{
59 		.name          = "ti-lmu-backlight",
60 		.id            = LM3532,
61 		.of_compatible = "ti,lm3532-backlight",
62 	},
63 };
64 
65 #define LM363X_REGULATOR(_id)			\
66 {						\
67 	.name          = "lm363x-regulator",	\
68 	.id            = _id,			\
69 	.of_compatible = "ti,lm363x-regulator",	\
70 }						\
71 
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),
78 	{
79 		.name          = "ti-lmu-backlight",
80 		.id            = LM3631,
81 		.of_compatible = "ti,lm3631-backlight",
82 	},
83 };
84 
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),
89 	{
90 		.name          = "ti-lmu-backlight",
91 		.id            = LM3632,
92 		.of_compatible = "ti,lm3632-backlight",
93 	},
94 };
95 
96 static const struct mfd_cell lm3633_devices[] = {
97 	{
98 		.name          = "ti-lmu-backlight",
99 		.id            = LM3633,
100 		.of_compatible = "ti,lm3633-backlight",
101 	},
102 	{
103 		.name          = "lm3633-leds",
104 		.of_compatible = "ti,lm3633-leds",
105 	},
106 	/* Monitoring driver for open/short circuit detection */
107 	{
108 		.name          = "ti-lmu-fault-monitor",
109 		.id            = LM3633,
110 		.of_compatible = "ti,lm3633-fault-monitor",
111 	},
112 };
113 
114 static const struct mfd_cell lm3695_devices[] = {
115 	{
116 		.name          = "ti-lmu-backlight",
117 		.id            = LM3695,
118 		.of_compatible = "ti,lm3695-backlight",
119 	},
120 };
121 
122 static const struct mfd_cell lm3697_devices[] = {
123 	{
124 		.name          = "ti-lmu-backlight",
125 		.id            = LM3697,
126 		.of_compatible = "ti,lm3697-backlight",
127 	},
128 	/* Monitoring driver for open/short circuit detection */
129 	{
130 		.name          = "ti-lmu-fault-monitor",
131 		.id            = LM3697,
132 		.of_compatible = "ti,lm3697-fault-monitor",
133 	},
134 };
135 
136 #define TI_LMU_DATA(chip, max_reg)		\
137 static const struct ti_lmu_data chip##_data =	\
138 {						\
139 	.cells = chip##_devices,		\
140 	.num_cells = ARRAY_SIZE(chip##_devices),\
141 	.max_register = max_reg,		\
142 }						\
143 
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);
150 
151 static int ti_lmu_probe(struct i2c_client *cl, const struct i2c_device_id *id)
152 {
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;
158 
159 	/*
160 	 * Get device specific data from of_match table.
161 	 * This data is defined by using TI_LMU_DATA() macro.
162 	 */
163 	data = of_device_get_match_data(dev);
164 	if (!data)
165 		return -ENODEV;
166 
167 	lmu = devm_kzalloc(dev, sizeof(*lmu), GFP_KERNEL);
168 	if (!lmu)
169 		return -ENOMEM;
170 
171 	lmu->dev = &cl->dev;
172 
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;
179 
180 	lmu->regmap = devm_regmap_init_i2c(cl, &regmap_cfg);
181 	if (IS_ERR(lmu->regmap))
182 		return PTR_ERR(lmu->regmap);
183 
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;
190 	}
191 
192 	ret = ti_lmu_enable_hw(lmu, id->driver_data);
193 	if (ret)
194 		return ret;
195 
196 	ret = devm_add_action_or_reset(dev, ti_lmu_disable_hw, lmu);
197 	if (ret)
198 		return ret;
199 
200 	/*
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.
204 	 */
205 	BLOCKING_INIT_NOTIFIER_HEAD(&lmu->notifier);
206 
207 	i2c_set_clientdata(cl, lmu);
208 
209 	return devm_mfd_add_devices(lmu->dev, 0, data->cells,
210 				    data->num_cells, NULL, 0, NULL);
211 }
212 
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 },
220 	{ }
221 };
222 MODULE_DEVICE_TABLE(of, ti_lmu_of_match);
223 
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 },
231 	{ }
232 };
233 MODULE_DEVICE_TABLE(i2c, ti_lmu_ids);
234 
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,
240 	},
241 	.id_table = ti_lmu_ids,
242 };
243 
244 module_i2c_driver(ti_lmu_driver);
245 
246 MODULE_DESCRIPTION("TI LMU MFD Core Driver");
247 MODULE_AUTHOR("Milo Kim");
248 MODULE_LICENSE("GPL v2");
249