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/regulator/of_regulator.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/mfd/tps65218.h>
30 static unsigned int tps65218_ramp_delay = 4000;
32 enum tps65218_regulators { DCDC1, DCDC2, DCDC3, DCDC4, DCDC5, DCDC6, LDO1 };
34 #define TPS65218_REGULATOR(_name, _id, _ops, _n, _vr, _vm, _er, _em, _t, \
41 .type = REGULATOR_VOLTAGE, \
42 .owner = THIS_MODULE, \
48 .linear_ranges = _lr, \
49 .n_linear_ranges = _nlr, \
52 #define TPS65218_INFO(_id, _nm, _min, _max) \
60 static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
61 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
62 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
65 static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
66 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
67 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
70 static const struct regulator_linear_range dcdc4_ranges[] = {
71 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
72 REGULATOR_LINEAR_RANGE(1550000, 0x10, 0x34, 50000),
75 static struct tps_info tps65218_pmic_regs[] = {
76 TPS65218_INFO(0, "DCDC1", 850000, 167500),
77 TPS65218_INFO(1, "DCDC2", 850000, 1675000),
78 TPS65218_INFO(2, "DCDC3", 900000, 3400000),
79 TPS65218_INFO(3, "DCDC4", 1175000, 3400000),
80 TPS65218_INFO(4, "DCDC5", 1000000, 1000000),
81 TPS65218_INFO(5, "DCDC6", 1800000, 1800000),
82 TPS65218_INFO(6, "LDO1", 900000, 3400000),
85 #define TPS65218_OF_MATCH(comp, label) \
91 static const struct of_device_id tps65218_of_match[] = {
92 TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
93 TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
94 TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
95 TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
96 TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
97 TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
98 TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
101 MODULE_DEVICE_TABLE(of, tps65218_of_match);
103 static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
107 struct tps65218 *tps = rdev_get_drvdata(dev);
108 unsigned int rid = rdev_get_id(dev);
110 /* Set the voltage based on vsel value and write protect level is 2 */
111 ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
112 selector, TPS65218_PROTECT_L1);
114 /* Set GO bit for DCDC1/2 to initiate voltage transistion */
116 case TPS65218_DCDC_1:
117 case TPS65218_DCDC_2:
118 ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
119 TPS65218_SLEW_RATE_GO,
120 TPS65218_SLEW_RATE_GO,
121 TPS65218_PROTECT_L1);
128 static int tps65218_pmic_enable(struct regulator_dev *dev)
130 struct tps65218 *tps = rdev_get_drvdata(dev);
131 unsigned int rid = rdev_get_id(dev);
133 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
136 /* Enable the regulator and password protection is level 1 */
137 return tps65218_set_bits(tps, dev->desc->enable_reg,
138 dev->desc->enable_mask, dev->desc->enable_mask,
139 TPS65218_PROTECT_L1);
142 static int tps65218_pmic_disable(struct regulator_dev *dev)
144 struct tps65218 *tps = rdev_get_drvdata(dev);
145 unsigned int rid = rdev_get_id(dev);
147 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
150 /* Disable the regulator and password protection is level 1 */
151 return tps65218_clear_bits(tps, dev->desc->enable_reg,
152 dev->desc->enable_mask, TPS65218_PROTECT_L1);
155 static int tps65218_set_voltage_time_sel(struct regulator_dev *rdev,
156 unsigned int old_selector, unsigned int new_selector)
160 old_uv = regulator_list_voltage_linear_range(rdev, old_selector);
164 new_uv = regulator_list_voltage_linear_range(rdev, new_selector);
168 return DIV_ROUND_UP(abs(old_uv - new_uv), tps65218_ramp_delay);
171 /* Operations permitted on DCDC1, DCDC2 */
172 static struct regulator_ops tps65218_dcdc12_ops = {
173 .is_enabled = regulator_is_enabled_regmap,
174 .enable = tps65218_pmic_enable,
175 .disable = tps65218_pmic_disable,
176 .get_voltage_sel = regulator_get_voltage_sel_regmap,
177 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
178 .list_voltage = regulator_list_voltage_linear_range,
179 .map_voltage = regulator_map_voltage_linear_range,
180 .set_voltage_time_sel = tps65218_set_voltage_time_sel,
183 /* Operations permitted on DCDC3, DCDC4 and LDO1 */
184 static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
185 .is_enabled = regulator_is_enabled_regmap,
186 .enable = tps65218_pmic_enable,
187 .disable = tps65218_pmic_disable,
188 .get_voltage_sel = regulator_get_voltage_sel_regmap,
189 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
190 .list_voltage = regulator_list_voltage_linear_range,
191 .map_voltage = regulator_map_voltage_linear_range,
194 /* Operations permitted on DCDC5, DCDC6 */
195 static struct regulator_ops tps65218_dcdc56_pmic_ops = {
196 .is_enabled = regulator_is_enabled_regmap,
197 .enable = tps65218_pmic_enable,
198 .disable = tps65218_pmic_disable,
201 static const struct regulator_desc regulators[] = {
202 TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, tps65218_dcdc12_ops, 64,
203 TPS65218_REG_CONTROL_DCDC1,
204 TPS65218_CONTROL_DCDC1_MASK,
205 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC1_EN, NULL,
206 dcdc1_dcdc2_ranges, 2),
207 TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, tps65218_dcdc12_ops, 64,
208 TPS65218_REG_CONTROL_DCDC2,
209 TPS65218_CONTROL_DCDC2_MASK,
210 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC2_EN, NULL,
211 dcdc1_dcdc2_ranges, 2),
212 TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, tps65218_ldo1_dcdc34_ops,
213 64, TPS65218_REG_CONTROL_DCDC3,
214 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
215 TPS65218_ENABLE1_DC3_EN, NULL,
216 ldo1_dcdc3_ranges, 2),
217 TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, tps65218_ldo1_dcdc34_ops,
218 53, TPS65218_REG_CONTROL_DCDC4,
219 TPS65218_CONTROL_DCDC4_MASK,
220 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC4_EN, NULL,
222 TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, tps65218_dcdc56_pmic_ops,
223 1, -1, -1, TPS65218_REG_ENABLE1,
224 TPS65218_ENABLE1_DC5_EN, NULL, NULL, 0),
225 TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, tps65218_dcdc56_pmic_ops,
226 1, -1, -1, TPS65218_REG_ENABLE1,
227 TPS65218_ENABLE1_DC6_EN, NULL, NULL, 0),
228 TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, tps65218_ldo1_dcdc34_ops, 64,
229 TPS65218_REG_CONTROL_DCDC4,
230 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
231 TPS65218_ENABLE2_LDO1_EN, NULL, ldo1_dcdc3_ranges,
235 static int tps65218_regulator_probe(struct platform_device *pdev)
237 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
238 struct regulator_init_data *init_data;
239 const struct tps_info *template;
240 struct regulator_dev *rdev;
241 const struct of_device_id *match;
242 struct regulator_config config = { };
245 match = of_match_device(tps65218_of_match, &pdev->dev);
249 template = match->data;
251 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node);
253 platform_set_drvdata(pdev, tps);
255 tps->info[id] = &tps65218_pmic_regs[id];
256 config.dev = &pdev->dev;
257 config.init_data = init_data;
258 config.driver_data = tps;
259 config.regmap = tps->regmap;
261 rdev = devm_regulator_register(&pdev->dev, ®ulators[id], &config);
263 dev_err(tps->dev, "failed to register %s regulator\n",
265 return PTR_ERR(rdev);
271 static struct platform_driver tps65218_regulator_driver = {
273 .name = "tps65218-pmic",
274 .owner = THIS_MODULE,
275 .of_match_table = tps65218_of_match,
277 .probe = tps65218_regulator_probe,
280 module_platform_driver(tps65218_regulator_driver);
282 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
283 MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
284 MODULE_ALIAS("platform:tps65218-pmic");
285 MODULE_LICENSE("GPL v2");