4 * Regulator driver for TPS65218 PMIC
6 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether expressed or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License version 2 for more details.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/platform_device.h>
24 #include <linux/of_device.h>
25 #include <linux/regmap.h>
26 #include <linux/regulator/of_regulator.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
29 #include <linux/mfd/tps65218.h>
31 enum tps65218_regulators
{ DCDC1
, DCDC2
, DCDC3
, DCDC4
,
32 DCDC5
, DCDC6
, LDO1
, LS3
};
34 #define TPS65218_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
35 _em, _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm) \
43 .owner = THIS_MODULE, \
51 .linear_ranges = _lr, \
52 .n_linear_ranges = _nlr, \
53 .ramp_delay = _delay, \
59 static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
60 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
61 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
64 static const struct regulator_linear_range ldo1_dcdc3_ranges
[] = {
65 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
66 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
69 static const struct regulator_linear_range dcdc4_ranges
[] = {
70 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
71 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
74 static int tps65218_pmic_set_voltage_sel(struct regulator_dev
*dev
,
78 struct tps65218
*tps
= rdev_get_drvdata(dev
);
79 unsigned int rid
= rdev_get_id(dev
);
81 /* Set the voltage based on vsel value and write protect level is 2 */
82 ret
= tps65218_set_bits(tps
, dev
->desc
->vsel_reg
, dev
->desc
->vsel_mask
,
83 selector
, TPS65218_PROTECT_L1
);
85 /* Set GO bit for DCDC1/2 to initiate voltage transistion */
89 ret
= tps65218_set_bits(tps
, TPS65218_REG_CONTRL_SLEW_RATE
,
90 TPS65218_SLEW_RATE_GO
,
91 TPS65218_SLEW_RATE_GO
,
99 static int tps65218_pmic_enable(struct regulator_dev
*dev
)
101 struct tps65218
*tps
= rdev_get_drvdata(dev
);
102 int rid
= rdev_get_id(dev
);
104 if (rid
< TPS65218_DCDC_1
|| rid
> TPS65218_LDO_1
)
107 /* Enable the regulator and password protection is level 1 */
108 return tps65218_set_bits(tps
, dev
->desc
->enable_reg
,
109 dev
->desc
->enable_mask
, dev
->desc
->enable_mask
,
110 TPS65218_PROTECT_L1
);
113 static int tps65218_pmic_disable(struct regulator_dev
*dev
)
115 struct tps65218
*tps
= rdev_get_drvdata(dev
);
116 int rid
= rdev_get_id(dev
);
118 if (rid
< TPS65218_DCDC_1
|| rid
> TPS65218_LDO_1
)
121 /* Disable the regulator and password protection is level 1 */
122 return tps65218_clear_bits(tps
, dev
->desc
->enable_reg
,
123 dev
->desc
->enable_mask
, TPS65218_PROTECT_L1
);
126 static int tps65218_pmic_set_suspend_enable(struct regulator_dev
*dev
)
128 struct tps65218
*tps
= rdev_get_drvdata(dev
);
129 unsigned int rid
= rdev_get_id(dev
);
131 if (rid
< TPS65218_DCDC_1
|| rid
> TPS65218_LDO_1
)
134 return tps65218_clear_bits(tps
, dev
->desc
->bypass_reg
,
135 dev
->desc
->bypass_mask
,
136 TPS65218_PROTECT_L1
);
139 static int tps65218_pmic_set_suspend_disable(struct regulator_dev
*dev
)
141 struct tps65218
*tps
= rdev_get_drvdata(dev
);
142 unsigned int rid
= rdev_get_id(dev
);
144 if (rid
< TPS65218_DCDC_1
|| rid
> TPS65218_LDO_1
)
148 * Certain revisions of TPS65218 will need to have DCDC3 regulator
149 * enabled always, otherwise an immediate system reboot will occur
152 if (rid
== TPS65218_DCDC_3
&& tps
->rev
== TPS65218_REV_2_1
)
155 if (!tps
->strobes
[rid
]) {
156 if (rid
== TPS65218_DCDC_3
)
157 tps
->info
[rid
]->strobe
= 3;
162 return tps65218_set_bits(tps
, dev
->desc
->bypass_reg
,
163 dev
->desc
->bypass_mask
,
164 tps
->strobes
[rid
], TPS65218_PROTECT_L1
);
167 /* Operations permitted on DCDC1, DCDC2 */
168 static struct regulator_ops tps65218_dcdc12_ops
= {
169 .is_enabled
= regulator_is_enabled_regmap
,
170 .enable
= tps65218_pmic_enable
,
171 .disable
= tps65218_pmic_disable
,
172 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
173 .set_voltage_sel
= tps65218_pmic_set_voltage_sel
,
174 .list_voltage
= regulator_list_voltage_linear_range
,
175 .map_voltage
= regulator_map_voltage_linear_range
,
176 .set_voltage_time_sel
= regulator_set_voltage_time_sel
,
177 .set_suspend_enable
= tps65218_pmic_set_suspend_enable
,
178 .set_suspend_disable
= tps65218_pmic_set_suspend_disable
,
181 /* Operations permitted on DCDC3, DCDC4 and LDO1 */
182 static struct regulator_ops tps65218_ldo1_dcdc34_ops
= {
183 .is_enabled
= regulator_is_enabled_regmap
,
184 .enable
= tps65218_pmic_enable
,
185 .disable
= tps65218_pmic_disable
,
186 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
187 .set_voltage_sel
= tps65218_pmic_set_voltage_sel
,
188 .list_voltage
= regulator_list_voltage_linear_range
,
189 .map_voltage
= regulator_map_voltage_linear_range
,
190 .set_suspend_enable
= tps65218_pmic_set_suspend_enable
,
191 .set_suspend_disable
= tps65218_pmic_set_suspend_disable
,
194 static const int ls3_currents
[] = { 100, 200, 500, 1000 };
196 static int tps65218_pmic_set_input_current_lim(struct regulator_dev
*dev
,
199 unsigned int index
= 0;
200 unsigned int num_currents
= ARRAY_SIZE(ls3_currents
);
201 struct tps65218
*tps
= rdev_get_drvdata(dev
);
203 while (index
< num_currents
&& ls3_currents
[index
] != lim_uA
)
206 if (index
== num_currents
)
209 return tps65218_set_bits(tps
, dev
->desc
->csel_reg
, dev
->desc
->csel_mask
,
210 index
<< 2, TPS65218_PROTECT_L1
);
213 static int tps65218_pmic_set_current_limit(struct regulator_dev
*dev
,
214 int min_uA
, int max_uA
)
217 unsigned int num_currents
= ARRAY_SIZE(ls3_currents
);
218 struct tps65218
*tps
= rdev_get_drvdata(dev
);
220 while (index
< num_currents
&& ls3_currents
[index
] < max_uA
)
225 if (index
< 0 || ls3_currents
[index
] < min_uA
)
228 return tps65218_set_bits(tps
, dev
->desc
->csel_reg
, dev
->desc
->csel_mask
,
229 index
<< 2, TPS65218_PROTECT_L1
);
232 static int tps65218_pmic_get_current_limit(struct regulator_dev
*dev
)
236 struct tps65218
*tps
= rdev_get_drvdata(dev
);
238 retval
= regmap_read(tps
->regmap
, dev
->desc
->csel_reg
, &index
);
242 index
= (index
& dev
->desc
->csel_mask
) >> 2;
244 return ls3_currents
[index
];
247 static struct regulator_ops tps65218_ls3_ops
= {
248 .is_enabled
= regulator_is_enabled_regmap
,
249 .enable
= tps65218_pmic_enable
,
250 .disable
= tps65218_pmic_disable
,
251 .set_input_current_limit
= tps65218_pmic_set_input_current_lim
,
252 .set_current_limit
= tps65218_pmic_set_current_limit
,
253 .get_current_limit
= tps65218_pmic_get_current_limit
,
256 /* Operations permitted on DCDC5, DCDC6 */
257 static struct regulator_ops tps65218_dcdc56_pmic_ops
= {
258 .is_enabled
= regulator_is_enabled_regmap
,
259 .enable
= tps65218_pmic_enable
,
260 .disable
= tps65218_pmic_disable
,
261 .set_suspend_enable
= tps65218_pmic_set_suspend_enable
,
262 .set_suspend_disable
= tps65218_pmic_set_suspend_disable
,
265 static const struct regulator_desc regulators
[] = {
266 TPS65218_REGULATOR("DCDC1", "regulator-dcdc1", TPS65218_DCDC_1
,
267 REGULATOR_VOLTAGE
, tps65218_dcdc12_ops
, 64,
268 TPS65218_REG_CONTROL_DCDC1
,
269 TPS65218_CONTROL_DCDC1_MASK
, TPS65218_REG_ENABLE1
,
270 TPS65218_ENABLE1_DC1_EN
, 0, 0, dcdc1_dcdc2_ranges
,
271 2, 4000, 0, TPS65218_REG_SEQ3
,
272 TPS65218_SEQ3_DC1_SEQ_MASK
),
273 TPS65218_REGULATOR("DCDC2", "regulator-dcdc2", TPS65218_DCDC_2
,
274 REGULATOR_VOLTAGE
, tps65218_dcdc12_ops
, 64,
275 TPS65218_REG_CONTROL_DCDC2
,
276 TPS65218_CONTROL_DCDC2_MASK
, TPS65218_REG_ENABLE1
,
277 TPS65218_ENABLE1_DC2_EN
, 0, 0, dcdc1_dcdc2_ranges
,
278 2, 4000, 0, TPS65218_REG_SEQ3
,
279 TPS65218_SEQ3_DC2_SEQ_MASK
),
280 TPS65218_REGULATOR("DCDC3", "regulator-dcdc3", TPS65218_DCDC_3
,
281 REGULATOR_VOLTAGE
, tps65218_ldo1_dcdc34_ops
, 64,
282 TPS65218_REG_CONTROL_DCDC3
,
283 TPS65218_CONTROL_DCDC3_MASK
, TPS65218_REG_ENABLE1
,
284 TPS65218_ENABLE1_DC3_EN
, 0, 0, ldo1_dcdc3_ranges
, 2,
285 0, 0, TPS65218_REG_SEQ4
, TPS65218_SEQ4_DC3_SEQ_MASK
),
286 TPS65218_REGULATOR("DCDC4", "regulator-dcdc4", TPS65218_DCDC_4
,
287 REGULATOR_VOLTAGE
, tps65218_ldo1_dcdc34_ops
, 53,
288 TPS65218_REG_CONTROL_DCDC4
,
289 TPS65218_CONTROL_DCDC4_MASK
, TPS65218_REG_ENABLE1
,
290 TPS65218_ENABLE1_DC4_EN
, 0, 0, dcdc4_ranges
, 2,
291 0, 0, TPS65218_REG_SEQ4
, TPS65218_SEQ4_DC4_SEQ_MASK
),
292 TPS65218_REGULATOR("DCDC5", "regulator-dcdc5", TPS65218_DCDC_5
,
293 REGULATOR_VOLTAGE
, tps65218_dcdc56_pmic_ops
, 1, -1,
294 -1, TPS65218_REG_ENABLE1
, TPS65218_ENABLE1_DC5_EN
, 0,
295 0, NULL
, 0, 0, 1000000, TPS65218_REG_SEQ5
,
296 TPS65218_SEQ5_DC5_SEQ_MASK
),
297 TPS65218_REGULATOR("DCDC6", "regulator-dcdc6", TPS65218_DCDC_6
,
298 REGULATOR_VOLTAGE
, tps65218_dcdc56_pmic_ops
, 1, -1,
299 -1, TPS65218_REG_ENABLE1
, TPS65218_ENABLE1_DC6_EN
, 0,
300 0, NULL
, 0, 0, 1800000, TPS65218_REG_SEQ5
,
301 TPS65218_SEQ5_DC6_SEQ_MASK
),
302 TPS65218_REGULATOR("LDO1", "regulator-ldo1", TPS65218_LDO_1
,
303 REGULATOR_VOLTAGE
, tps65218_ldo1_dcdc34_ops
, 64,
304 TPS65218_REG_CONTROL_LDO1
,
305 TPS65218_CONTROL_LDO1_MASK
, TPS65218_REG_ENABLE2
,
306 TPS65218_ENABLE2_LDO1_EN
, 0, 0, ldo1_dcdc3_ranges
,
307 2, 0, 0, TPS65218_REG_SEQ6
,
308 TPS65218_SEQ6_LDO1_SEQ_MASK
),
309 TPS65218_REGULATOR("LS3", "regulator-ls3", TPS65218_LS_3
,
310 REGULATOR_CURRENT
, tps65218_ls3_ops
, 0, 0, 0,
311 TPS65218_REG_ENABLE2
, TPS65218_ENABLE2_LS3_EN
,
312 TPS65218_REG_CONFIG2
, TPS65218_CONFIG2_LS3ILIM_MASK
,
313 NULL
, 0, 0, 0, 0, 0),
316 static int tps65218_regulator_probe(struct platform_device
*pdev
)
318 struct tps65218
*tps
= dev_get_drvdata(pdev
->dev
.parent
);
319 struct regulator_dev
*rdev
;
320 struct regulator_config config
= { };
324 config
.dev
= &pdev
->dev
;
325 config
.dev
->of_node
= tps
->dev
->of_node
;
326 config
.driver_data
= tps
;
327 config
.regmap
= tps
->regmap
;
329 /* Allocate memory for strobes */
330 tps
->strobes
= devm_kzalloc(&pdev
->dev
, sizeof(u8
) *
331 TPS65218_NUM_REGULATOR
, GFP_KERNEL
);
333 for (i
= 0; i
< ARRAY_SIZE(regulators
); i
++) {
334 rdev
= devm_regulator_register(&pdev
->dev
, ®ulators
[i
],
337 dev_err(tps
->dev
, "failed to register %s regulator\n",
339 return PTR_ERR(rdev
);
342 ret
= regmap_read(tps
->regmap
, regulators
[i
].bypass_reg
, &val
);
346 tps
->strobes
[i
] = val
& regulators
[i
].bypass_mask
;
352 static const struct platform_device_id tps65218_regulator_id_table
[] = {
353 { "tps65218-regulator", },
356 MODULE_DEVICE_TABLE(platform
, tps65218_regulator_id_table
);
358 static struct platform_driver tps65218_regulator_driver
= {
360 .name
= "tps65218-pmic",
362 .probe
= tps65218_regulator_probe
,
363 .id_table
= tps65218_regulator_id_table
,
366 module_platform_driver(tps65218_regulator_driver
);
368 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
369 MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
370 MODULE_ALIAS("platform:tps65218-pmic");
371 MODULE_LICENSE("GPL v2");