blob: a5e5634eeb9e493067b73e440ee11caee3c346f9 [file] [log] [blame]
Keerthy90e7d522014-02-06 11:20:13 +05301/*
2 * tps65218-regulator.c
3 *
4 * Regulator driver for TPS65218 PMIC
5 *
6 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
7 *
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.
11 *
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.
16 */
17
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>
29
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020030enum tps65218_regulators { DCDC1, DCDC2, DCDC3, DCDC4,
31 DCDC5, DCDC6, LDO1, LS3 };
Keerthy90e7d522014-02-06 11:20:13 +053032
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020033#define TPS65218_REGULATOR(_name, _id, _type, _ops, _n, _vr, _vm, _er, _em, \
34 _cr, _cm, _lr, _nlr, _delay, _fuv) \
Keerthy90e7d522014-02-06 11:20:13 +053035 { \
36 .name = _name, \
37 .id = _id, \
38 .ops = &_ops, \
39 .n_voltages = _n, \
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020040 .type = _type, \
Keerthy90e7d522014-02-06 11:20:13 +053041 .owner = THIS_MODULE, \
42 .vsel_reg = _vr, \
43 .vsel_mask = _vm, \
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020044 .csel_reg = _cr, \
45 .csel_mask = _cm, \
Keerthy90e7d522014-02-06 11:20:13 +053046 .enable_reg = _er, \
47 .enable_mask = _em, \
Felipe Balbidd648ef2014-07-08 14:09:13 -050048 .volt_table = NULL, \
Keerthy90e7d522014-02-06 11:20:13 +053049 .linear_ranges = _lr, \
50 .n_linear_ranges = _nlr, \
Axel Lin5ab9be42014-05-22 09:26:47 +080051 .ramp_delay = _delay, \
Keerthy7753d2d2014-07-09 15:52:59 +053052 .fixed_uV = _fuv \
Keerthy90e7d522014-02-06 11:20:13 +053053 } \
54
55#define TPS65218_INFO(_id, _nm, _min, _max) \
Felipe Balbi6a5e06d2014-07-08 14:09:14 -050056 [_id] = { \
Keerthy90e7d522014-02-06 11:20:13 +053057 .id = _id, \
58 .name = _nm, \
59 .min_uV = _min, \
60 .max_uV = _max, \
61 }
62
63static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
64 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
65 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
66};
67
68static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
69 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
70 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
71};
72
73static const struct regulator_linear_range dcdc4_ranges[] = {
74 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
Felipe Balbi42ab0f32014-07-08 14:09:12 -050075 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
Keerthy90e7d522014-02-06 11:20:13 +053076};
77
78static struct tps_info tps65218_pmic_regs[] = {
Andrew F. Davis0f1d08d2015-09-15 15:34:22 -050079 TPS65218_INFO(DCDC1, "DCDC1", 850000, 1675000),
Felipe Balbi6a5e06d2014-07-08 14:09:14 -050080 TPS65218_INFO(DCDC2, "DCDC2", 850000, 1675000),
81 TPS65218_INFO(DCDC3, "DCDC3", 900000, 3400000),
82 TPS65218_INFO(DCDC4, "DCDC4", 1175000, 3400000),
83 TPS65218_INFO(DCDC5, "DCDC5", 1000000, 1000000),
84 TPS65218_INFO(DCDC6, "DCDC6", 1800000, 1800000),
85 TPS65218_INFO(LDO1, "LDO1", 900000, 3400000),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020086 TPS65218_INFO(LS3, "LS3", -1, -1),
Keerthy90e7d522014-02-06 11:20:13 +053087};
88
89#define TPS65218_OF_MATCH(comp, label) \
90 { \
91 .compatible = comp, \
92 .data = &label, \
93 }
94
95static const struct of_device_id tps65218_of_match[] = {
96 TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
97 TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
98 TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
99 TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
100 TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
101 TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
102 TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200103 TPS65218_OF_MATCH("ti,tps65218-ls3", tps65218_pmic_regs[LS3]),
Axel Linc46b5292014-02-19 16:33:21 +0800104 { }
Keerthy90e7d522014-02-06 11:20:13 +0530105};
106MODULE_DEVICE_TABLE(of, tps65218_of_match);
107
108static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
109 unsigned selector)
110{
111 int ret;
112 struct tps65218 *tps = rdev_get_drvdata(dev);
113 unsigned int rid = rdev_get_id(dev);
114
115 /* Set the voltage based on vsel value and write protect level is 2 */
116 ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
117 selector, TPS65218_PROTECT_L1);
118
119 /* Set GO bit for DCDC1/2 to initiate voltage transistion */
120 switch (rid) {
121 case TPS65218_DCDC_1:
122 case TPS65218_DCDC_2:
123 ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
124 TPS65218_SLEW_RATE_GO,
125 TPS65218_SLEW_RATE_GO,
126 TPS65218_PROTECT_L1);
127 break;
128 }
129
130 return ret;
131}
132
133static int tps65218_pmic_enable(struct regulator_dev *dev)
134{
135 struct tps65218 *tps = rdev_get_drvdata(dev);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530136 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530137
138 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
139 return -EINVAL;
140
141 /* Enable the regulator and password protection is level 1 */
142 return tps65218_set_bits(tps, dev->desc->enable_reg,
143 dev->desc->enable_mask, dev->desc->enable_mask,
144 TPS65218_PROTECT_L1);
145}
146
147static int tps65218_pmic_disable(struct regulator_dev *dev)
148{
149 struct tps65218 *tps = rdev_get_drvdata(dev);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530150 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530151
152 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
153 return -EINVAL;
154
155 /* Disable the regulator and password protection is level 1 */
156 return tps65218_clear_bits(tps, dev->desc->enable_reg,
157 dev->desc->enable_mask, TPS65218_PROTECT_L1);
158}
159
Keerthy90e7d522014-02-06 11:20:13 +0530160/* Operations permitted on DCDC1, DCDC2 */
161static struct regulator_ops tps65218_dcdc12_ops = {
162 .is_enabled = regulator_is_enabled_regmap,
163 .enable = tps65218_pmic_enable,
164 .disable = tps65218_pmic_disable,
165 .get_voltage_sel = regulator_get_voltage_sel_regmap,
166 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
167 .list_voltage = regulator_list_voltage_linear_range,
168 .map_voltage = regulator_map_voltage_linear_range,
Axel Lin5ab9be42014-05-22 09:26:47 +0800169 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Keerthy90e7d522014-02-06 11:20:13 +0530170};
171
172/* Operations permitted on DCDC3, DCDC4 and LDO1 */
173static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
174 .is_enabled = regulator_is_enabled_regmap,
175 .enable = tps65218_pmic_enable,
176 .disable = tps65218_pmic_disable,
177 .get_voltage_sel = regulator_get_voltage_sel_regmap,
178 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
179 .list_voltage = regulator_list_voltage_linear_range,
180 .map_voltage = regulator_map_voltage_linear_range,
181};
182
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200183static const int ls3_currents[] = { 100, 200, 500, 1000 };
184
185static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev,
186 int lim_uA)
187{
188 unsigned int index = 0;
189 unsigned int num_currents = ARRAY_SIZE(ls3_currents);
190 struct tps65218 *tps = rdev_get_drvdata(dev);
191
192 while (index < num_currents && ls3_currents[index] != lim_uA)
193 index++;
194
195 if (index == num_currents)
196 return -EINVAL;
197
198 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
199 index << 2, TPS65218_PROTECT_L1);
200}
201
202static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
203 int min_uA, int max_uA)
204{
205 int index = 0;
206 unsigned int num_currents = ARRAY_SIZE(ls3_currents);
207 struct tps65218 *tps = rdev_get_drvdata(dev);
208
209 while (index < num_currents && ls3_currents[index] < max_uA)
210 index++;
211
212 index--;
213
214 if (index < 0 || ls3_currents[index] < min_uA)
215 return -EINVAL;
216
217 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
218 index << 2, TPS65218_PROTECT_L1);
219}
220
221static int tps65218_pmic_get_current_limit(struct regulator_dev *dev)
222{
223 int retval;
224 unsigned int index;
225 struct tps65218 *tps = rdev_get_drvdata(dev);
226
227 retval = tps65218_reg_read(tps, dev->desc->csel_reg, &index);
228 if (retval < 0)
229 return retval;
230
231 index = (index & dev->desc->csel_mask) >> 2;
232
233 return ls3_currents[index];
234}
235
236static struct regulator_ops tps65218_ls3_ops = {
237 .is_enabled = regulator_is_enabled_regmap,
238 .enable = tps65218_pmic_enable,
239 .disable = tps65218_pmic_disable,
240 .set_input_current_limit = tps65218_pmic_set_input_current_lim,
241 .set_current_limit = tps65218_pmic_set_current_limit,
242 .get_current_limit = tps65218_pmic_get_current_limit,
243};
244
Keerthy90e7d522014-02-06 11:20:13 +0530245/* Operations permitted on DCDC5, DCDC6 */
246static struct regulator_ops tps65218_dcdc56_pmic_ops = {
247 .is_enabled = regulator_is_enabled_regmap,
248 .enable = tps65218_pmic_enable,
249 .disable = tps65218_pmic_disable,
250};
251
252static const struct regulator_desc regulators[] = {
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200253 TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, REGULATOR_VOLTAGE,
254 tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC1,
255 TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
256 TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
257 2, 4000, 0),
258 TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, REGULATOR_VOLTAGE,
259 tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC2,
260 TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
261 TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
262 2, 4000, 0),
263 TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, REGULATOR_VOLTAGE,
264 tps65218_ldo1_dcdc34_ops, 64,
265 TPS65218_REG_CONTROL_DCDC3,
Keerthy90e7d522014-02-06 11:20:13 +0530266 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200267 TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
268 0, 0),
269 TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, REGULATOR_VOLTAGE,
270 tps65218_ldo1_dcdc34_ops, 53,
271 TPS65218_REG_CONTROL_DCDC4,
272 TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
273 TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
274 0, 0),
275 TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, REGULATOR_VOLTAGE,
276 tps65218_dcdc56_pmic_ops, 1, -1, -1,
277 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0, 0,
278 NULL, 0, 0, 1000000),
279 TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, REGULATOR_VOLTAGE,
280 tps65218_dcdc56_pmic_ops, 1, -1, -1,
281 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0, 0,
282 NULL, 0, 0, 1800000),
283 TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, REGULATOR_VOLTAGE,
284 tps65218_ldo1_dcdc34_ops, 64,
Keerthy0eada6a2014-06-18 10:17:48 -0500285 TPS65218_REG_CONTROL_LDO1,
Keerthy90e7d522014-02-06 11:20:13 +0530286 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200287 TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
Keerthy7753d2d2014-07-09 15:52:59 +0530288 2, 0, 0),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200289 TPS65218_REGULATOR("LS3", TPS65218_LS_3, REGULATOR_CURRENT,
290 tps65218_ls3_ops, 0, 0, 0, TPS65218_REG_ENABLE2,
291 TPS65218_ENABLE2_LS3_EN, TPS65218_REG_CONFIG2,
292 TPS65218_CONFIG2_LS3ILIM_MASK, NULL, 0, 0, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530293};
294
295static int tps65218_regulator_probe(struct platform_device *pdev)
296{
297 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
298 struct regulator_init_data *init_data;
299 const struct tps_info *template;
300 struct regulator_dev *rdev;
301 const struct of_device_id *match;
302 struct regulator_config config = { };
303 int id;
304
305 match = of_match_device(tps65218_of_match, &pdev->dev);
Axel Lin948838a2014-02-19 16:35:03 +0800306 if (!match)
Keerthy90e7d522014-02-06 11:20:13 +0530307 return -ENODEV;
Axel Lin948838a2014-02-19 16:35:03 +0800308
309 template = match->data;
310 id = template->id;
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100311 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
312 &regulators[id]);
Keerthy90e7d522014-02-06 11:20:13 +0530313
314 platform_set_drvdata(pdev, tps);
315
316 tps->info[id] = &tps65218_pmic_regs[id];
317 config.dev = &pdev->dev;
318 config.init_data = init_data;
319 config.driver_data = tps;
320 config.regmap = tps->regmap;
Keerthyd2fa87c2014-06-18 10:17:47 -0500321 config.of_node = pdev->dev.of_node;
Keerthy90e7d522014-02-06 11:20:13 +0530322
323 rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
324 if (IS_ERR(rdev)) {
325 dev_err(tps->dev, "failed to register %s regulator\n",
326 pdev->name);
327 return PTR_ERR(rdev);
328 }
329
Keerthy90e7d522014-02-06 11:20:13 +0530330 return 0;
331}
332
333static struct platform_driver tps65218_regulator_driver = {
334 .driver = {
335 .name = "tps65218-pmic",
Axel Lin948838a2014-02-19 16:35:03 +0800336 .of_match_table = tps65218_of_match,
Keerthy90e7d522014-02-06 11:20:13 +0530337 },
338 .probe = tps65218_regulator_probe,
Keerthy90e7d522014-02-06 11:20:13 +0530339};
340
341module_platform_driver(tps65218_regulator_driver);
342
343MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
344MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
345MODULE_ALIAS("platform:tps65218-pmic");
346MODULE_LICENSE("GPL v2");