blob: eb0f5b13841a505ea4157e949cf179d71d26175e [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, \
Tero Kristob9a0d352016-06-24 13:58:08 +053034 _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm) \
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, \
Tero Kristob9a0d352016-06-24 13:58:08 +053052 .fixed_uV = _fuv, \
53 .bypass_reg = _sr, \
54 .bypass_mask = _sm, \
Keerthy90e7d522014-02-06 11:20:13 +053055 } \
56
57#define TPS65218_INFO(_id, _nm, _min, _max) \
Felipe Balbi6a5e06d2014-07-08 14:09:14 -050058 [_id] = { \
Keerthy90e7d522014-02-06 11:20:13 +053059 .id = _id, \
60 .name = _nm, \
61 .min_uV = _min, \
62 .max_uV = _max, \
63 }
64
65static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
66 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
67 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
68};
69
70static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
71 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
72 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
73};
74
75static const struct regulator_linear_range dcdc4_ranges[] = {
76 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
Felipe Balbi42ab0f32014-07-08 14:09:12 -050077 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
Keerthy90e7d522014-02-06 11:20:13 +053078};
79
80static struct tps_info tps65218_pmic_regs[] = {
Andrew F. Davis0f1d08d2015-09-15 15:34:22 -050081 TPS65218_INFO(DCDC1, "DCDC1", 850000, 1675000),
Felipe Balbi6a5e06d2014-07-08 14:09:14 -050082 TPS65218_INFO(DCDC2, "DCDC2", 850000, 1675000),
83 TPS65218_INFO(DCDC3, "DCDC3", 900000, 3400000),
84 TPS65218_INFO(DCDC4, "DCDC4", 1175000, 3400000),
85 TPS65218_INFO(DCDC5, "DCDC5", 1000000, 1000000),
86 TPS65218_INFO(DCDC6, "DCDC6", 1800000, 1800000),
87 TPS65218_INFO(LDO1, "LDO1", 900000, 3400000),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020088 TPS65218_INFO(LS3, "LS3", -1, -1),
Keerthy90e7d522014-02-06 11:20:13 +053089};
90
91#define TPS65218_OF_MATCH(comp, label) \
92 { \
93 .compatible = comp, \
94 .data = &label, \
95 }
96
97static const struct of_device_id tps65218_of_match[] = {
98 TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
99 TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
100 TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
101 TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
102 TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
103 TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
104 TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200105 TPS65218_OF_MATCH("ti,tps65218-ls3", tps65218_pmic_regs[LS3]),
Axel Linc46b5292014-02-19 16:33:21 +0800106 { }
Keerthy90e7d522014-02-06 11:20:13 +0530107};
108MODULE_DEVICE_TABLE(of, tps65218_of_match);
109
110static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
111 unsigned selector)
112{
113 int ret;
114 struct tps65218 *tps = rdev_get_drvdata(dev);
115 unsigned int rid = rdev_get_id(dev);
116
117 /* Set the voltage based on vsel value and write protect level is 2 */
118 ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
119 selector, TPS65218_PROTECT_L1);
120
121 /* Set GO bit for DCDC1/2 to initiate voltage transistion */
122 switch (rid) {
123 case TPS65218_DCDC_1:
124 case TPS65218_DCDC_2:
125 ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
126 TPS65218_SLEW_RATE_GO,
127 TPS65218_SLEW_RATE_GO,
128 TPS65218_PROTECT_L1);
129 break;
130 }
131
132 return ret;
133}
134
135static int tps65218_pmic_enable(struct regulator_dev *dev)
136{
137 struct tps65218 *tps = rdev_get_drvdata(dev);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530138 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530139
140 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
141 return -EINVAL;
142
143 /* Enable the regulator and password protection is level 1 */
144 return tps65218_set_bits(tps, dev->desc->enable_reg,
145 dev->desc->enable_mask, dev->desc->enable_mask,
146 TPS65218_PROTECT_L1);
147}
148
149static int tps65218_pmic_disable(struct regulator_dev *dev)
150{
151 struct tps65218 *tps = rdev_get_drvdata(dev);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530152 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530153
154 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
155 return -EINVAL;
156
157 /* Disable the regulator and password protection is level 1 */
158 return tps65218_clear_bits(tps, dev->desc->enable_reg,
159 dev->desc->enable_mask, TPS65218_PROTECT_L1);
160}
161
Tero Kristob9a0d352016-06-24 13:58:08 +0530162static int tps65218_pmic_set_suspend_enable(struct regulator_dev *dev)
163{
164 struct tps65218 *tps = rdev_get_drvdata(dev);
165 unsigned int rid = rdev_get_id(dev);
166
167 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
168 return -EINVAL;
169
170 return tps65218_clear_bits(tps, dev->desc->bypass_reg,
171 dev->desc->bypass_mask,
172 TPS65218_PROTECT_L1);
173}
174
175static int tps65218_pmic_set_suspend_disable(struct regulator_dev *dev)
176{
177 struct tps65218 *tps = rdev_get_drvdata(dev);
178 unsigned int rid = rdev_get_id(dev);
179
180 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
181 return -EINVAL;
182
Tero Kristo23a34f92016-08-10 17:53:55 +0530183 /*
184 * Certain revisions of TPS65218 will need to have DCDC3 regulator
185 * enabled always, otherwise an immediate system reboot will occur
186 * during poweroff.
187 */
188 if (rid == TPS65218_DCDC_3 && tps->rev == TPS65218_REV_2_1)
189 return 0;
190
Tero Kristo3fb2ef12016-06-24 13:58:09 +0530191 if (!tps->info[rid]->strobe) {
192 if (rid == TPS65218_DCDC_3)
193 tps->info[rid]->strobe = 3;
194 else
195 return -EINVAL;
196 }
Tero Kristob9a0d352016-06-24 13:58:08 +0530197
198 return tps65218_set_bits(tps, dev->desc->bypass_reg,
199 dev->desc->bypass_mask,
200 tps->info[rid]->strobe,
201 TPS65218_PROTECT_L1);
202}
203
Keerthy90e7d522014-02-06 11:20:13 +0530204/* Operations permitted on DCDC1, DCDC2 */
205static struct regulator_ops tps65218_dcdc12_ops = {
206 .is_enabled = regulator_is_enabled_regmap,
207 .enable = tps65218_pmic_enable,
208 .disable = tps65218_pmic_disable,
209 .get_voltage_sel = regulator_get_voltage_sel_regmap,
210 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
211 .list_voltage = regulator_list_voltage_linear_range,
212 .map_voltage = regulator_map_voltage_linear_range,
Axel Lin5ab9be42014-05-22 09:26:47 +0800213 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Tero Kristob9a0d352016-06-24 13:58:08 +0530214 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
215 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
Keerthy90e7d522014-02-06 11:20:13 +0530216};
217
218/* Operations permitted on DCDC3, DCDC4 and LDO1 */
219static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
220 .is_enabled = regulator_is_enabled_regmap,
221 .enable = tps65218_pmic_enable,
222 .disable = tps65218_pmic_disable,
223 .get_voltage_sel = regulator_get_voltage_sel_regmap,
224 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
225 .list_voltage = regulator_list_voltage_linear_range,
226 .map_voltage = regulator_map_voltage_linear_range,
Tero Kristob9a0d352016-06-24 13:58:08 +0530227 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
228 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
Keerthy90e7d522014-02-06 11:20:13 +0530229};
230
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200231static const int ls3_currents[] = { 100, 200, 500, 1000 };
232
233static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev,
234 int lim_uA)
235{
236 unsigned int index = 0;
237 unsigned int num_currents = ARRAY_SIZE(ls3_currents);
238 struct tps65218 *tps = rdev_get_drvdata(dev);
239
240 while (index < num_currents && ls3_currents[index] != lim_uA)
241 index++;
242
243 if (index == num_currents)
244 return -EINVAL;
245
246 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
247 index << 2, TPS65218_PROTECT_L1);
248}
249
250static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
251 int min_uA, int max_uA)
252{
253 int index = 0;
254 unsigned int num_currents = ARRAY_SIZE(ls3_currents);
255 struct tps65218 *tps = rdev_get_drvdata(dev);
256
257 while (index < num_currents && ls3_currents[index] < max_uA)
258 index++;
259
260 index--;
261
262 if (index < 0 || ls3_currents[index] < min_uA)
263 return -EINVAL;
264
265 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
266 index << 2, TPS65218_PROTECT_L1);
267}
268
269static int tps65218_pmic_get_current_limit(struct regulator_dev *dev)
270{
271 int retval;
272 unsigned int index;
273 struct tps65218 *tps = rdev_get_drvdata(dev);
274
275 retval = tps65218_reg_read(tps, dev->desc->csel_reg, &index);
276 if (retval < 0)
277 return retval;
278
279 index = (index & dev->desc->csel_mask) >> 2;
280
281 return ls3_currents[index];
282}
283
284static struct regulator_ops tps65218_ls3_ops = {
285 .is_enabled = regulator_is_enabled_regmap,
286 .enable = tps65218_pmic_enable,
287 .disable = tps65218_pmic_disable,
288 .set_input_current_limit = tps65218_pmic_set_input_current_lim,
289 .set_current_limit = tps65218_pmic_set_current_limit,
290 .get_current_limit = tps65218_pmic_get_current_limit,
291};
292
Keerthy90e7d522014-02-06 11:20:13 +0530293/* Operations permitted on DCDC5, DCDC6 */
294static struct regulator_ops tps65218_dcdc56_pmic_ops = {
295 .is_enabled = regulator_is_enabled_regmap,
296 .enable = tps65218_pmic_enable,
297 .disable = tps65218_pmic_disable,
Tero Kristob9a0d352016-06-24 13:58:08 +0530298 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
299 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
Keerthy90e7d522014-02-06 11:20:13 +0530300};
301
302static const struct regulator_desc regulators[] = {
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200303 TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, REGULATOR_VOLTAGE,
304 tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC1,
305 TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
306 TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
Tero Kristob9a0d352016-06-24 13:58:08 +0530307 2, 4000, 0, TPS65218_REG_SEQ3,
308 TPS65218_SEQ3_DC1_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200309 TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, REGULATOR_VOLTAGE,
310 tps65218_dcdc12_ops, 64, TPS65218_REG_CONTROL_DCDC2,
311 TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
312 TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
Tero Kristob9a0d352016-06-24 13:58:08 +0530313 2, 4000, 0, TPS65218_REG_SEQ3,
314 TPS65218_SEQ3_DC2_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200315 TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, REGULATOR_VOLTAGE,
316 tps65218_ldo1_dcdc34_ops, 64,
317 TPS65218_REG_CONTROL_DCDC3,
Keerthy90e7d522014-02-06 11:20:13 +0530318 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200319 TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
Tero Kristob9a0d352016-06-24 13:58:08 +0530320 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200321 TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, REGULATOR_VOLTAGE,
322 tps65218_ldo1_dcdc34_ops, 53,
323 TPS65218_REG_CONTROL_DCDC4,
324 TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
325 TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
Tero Kristob9a0d352016-06-24 13:58:08 +0530326 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200327 TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, REGULATOR_VOLTAGE,
328 tps65218_dcdc56_pmic_ops, 1, -1, -1,
329 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0, 0,
Tero Kristob9a0d352016-06-24 13:58:08 +0530330 NULL, 0, 0, 1000000, TPS65218_REG_SEQ5,
331 TPS65218_SEQ5_DC5_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200332 TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, REGULATOR_VOLTAGE,
333 tps65218_dcdc56_pmic_ops, 1, -1, -1,
334 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0, 0,
Tero Kristob9a0d352016-06-24 13:58:08 +0530335 NULL, 0, 0, 1800000, TPS65218_REG_SEQ5,
336 TPS65218_SEQ5_DC6_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200337 TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, REGULATOR_VOLTAGE,
338 tps65218_ldo1_dcdc34_ops, 64,
Keerthy0eada6a2014-06-18 10:17:48 -0500339 TPS65218_REG_CONTROL_LDO1,
Keerthy90e7d522014-02-06 11:20:13 +0530340 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200341 TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
Tero Kristob9a0d352016-06-24 13:58:08 +0530342 2, 0, 0, TPS65218_REG_SEQ6,
343 TPS65218_SEQ6_LDO1_SEQ_MASK),
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200344 TPS65218_REGULATOR("LS3", TPS65218_LS_3, REGULATOR_CURRENT,
345 tps65218_ls3_ops, 0, 0, 0, TPS65218_REG_ENABLE2,
346 TPS65218_ENABLE2_LS3_EN, TPS65218_REG_CONFIG2,
Tero Kristob9a0d352016-06-24 13:58:08 +0530347 TPS65218_CONFIG2_LS3ILIM_MASK, NULL, 0, 0, 0, 0, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530348};
349
350static int tps65218_regulator_probe(struct platform_device *pdev)
351{
352 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
353 struct regulator_init_data *init_data;
354 const struct tps_info *template;
355 struct regulator_dev *rdev;
356 const struct of_device_id *match;
357 struct regulator_config config = { };
Tero Kristob9a0d352016-06-24 13:58:08 +0530358 int id, ret;
359 unsigned int val;
Keerthy90e7d522014-02-06 11:20:13 +0530360
361 match = of_match_device(tps65218_of_match, &pdev->dev);
Axel Lin948838a2014-02-19 16:35:03 +0800362 if (!match)
Keerthy90e7d522014-02-06 11:20:13 +0530363 return -ENODEV;
Axel Lin948838a2014-02-19 16:35:03 +0800364
365 template = match->data;
366 id = template->id;
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100367 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
368 &regulators[id]);
Keerthy90e7d522014-02-06 11:20:13 +0530369
370 platform_set_drvdata(pdev, tps);
371
372 tps->info[id] = &tps65218_pmic_regs[id];
373 config.dev = &pdev->dev;
374 config.init_data = init_data;
375 config.driver_data = tps;
376 config.regmap = tps->regmap;
Keerthyd2fa87c2014-06-18 10:17:47 -0500377 config.of_node = pdev->dev.of_node;
Keerthy90e7d522014-02-06 11:20:13 +0530378
379 rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
380 if (IS_ERR(rdev)) {
381 dev_err(tps->dev, "failed to register %s regulator\n",
382 pdev->name);
383 return PTR_ERR(rdev);
384 }
385
Tero Kristob9a0d352016-06-24 13:58:08 +0530386 ret = tps65218_reg_read(tps, regulators[id].bypass_reg, &val);
387 if (ret)
388 return ret;
389
390 tps->info[id]->strobe = val & regulators[id].bypass_mask;
391
Keerthy90e7d522014-02-06 11:20:13 +0530392 return 0;
393}
394
395static struct platform_driver tps65218_regulator_driver = {
396 .driver = {
397 .name = "tps65218-pmic",
Axel Lin948838a2014-02-19 16:35:03 +0800398 .of_match_table = tps65218_of_match,
Keerthy90e7d522014-02-06 11:20:13 +0530399 },
400 .probe = tps65218_regulator_probe,
Keerthy90e7d522014-02-06 11:20:13 +0530401};
402
403module_platform_driver(tps65218_regulator_driver);
404
405MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
406MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
407MODULE_ALIAS("platform:tps65218-pmic");
408MODULE_LICENSE("GPL v2");