rtc: pcf8523: don't return invalid date when battery is low
[linux-2.6-block.git] / drivers / regulator / tps65218-regulator.c
CommitLineData
90e7d526
K
1/*
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>
0aced355 25#include <linux/regmap.h>
90e7d526
K
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>
30
2dc49403
K
31#define TPS65218_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
32 _em, _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm) \
90e7d526
K
33 { \
34 .name = _name, \
2dc49403 35 .of_match = _of, \
90e7d526
K
36 .id = _id, \
37 .ops = &_ops, \
38 .n_voltages = _n, \
c0ea88b8 39 .type = _type, \
90e7d526
K
40 .owner = THIS_MODULE, \
41 .vsel_reg = _vr, \
42 .vsel_mask = _vm, \
c0ea88b8
NK
43 .csel_reg = _cr, \
44 .csel_mask = _cm, \
90e7d526
K
45 .enable_reg = _er, \
46 .enable_mask = _em, \
dd648ef2 47 .volt_table = NULL, \
90e7d526
K
48 .linear_ranges = _lr, \
49 .n_linear_ranges = _nlr, \
5ab9be42 50 .ramp_delay = _delay, \
b9a0d359
TK
51 .fixed_uV = _fuv, \
52 .bypass_reg = _sr, \
53 .bypass_mask = _sm, \
90e7d526
K
54 } \
55
90e7d526
K
56static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
57 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
58 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
59};
60
61static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
62 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
63 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
64};
65
66static const struct regulator_linear_range dcdc4_ranges[] = {
67 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
42ab0f39 68 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
90e7d526
K
69};
70
90e7d526
K
71static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
72 unsigned selector)
73{
74 int ret;
75 struct tps65218 *tps = rdev_get_drvdata(dev);
76 unsigned int rid = rdev_get_id(dev);
77
78 /* Set the voltage based on vsel value and write protect level is 2 */
79 ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
80 selector, TPS65218_PROTECT_L1);
81
82 /* Set GO bit for DCDC1/2 to initiate voltage transistion */
83 switch (rid) {
84 case TPS65218_DCDC_1:
85 case TPS65218_DCDC_2:
86 ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
87 TPS65218_SLEW_RATE_GO,
88 TPS65218_SLEW_RATE_GO,
89 TPS65218_PROTECT_L1);
90 break;
91 }
92
93 return ret;
94}
95
96static int tps65218_pmic_enable(struct regulator_dev *dev)
97{
98 struct tps65218 *tps = rdev_get_drvdata(dev);
5f986f7c 99 int rid = rdev_get_id(dev);
90e7d526
K
100
101 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
102 return -EINVAL;
103
104 /* Enable the regulator and password protection is level 1 */
105 return tps65218_set_bits(tps, dev->desc->enable_reg,
106 dev->desc->enable_mask, dev->desc->enable_mask,
107 TPS65218_PROTECT_L1);
108}
109
110static int tps65218_pmic_disable(struct regulator_dev *dev)
111{
112 struct tps65218 *tps = rdev_get_drvdata(dev);
5f986f7c 113 int rid = rdev_get_id(dev);
90e7d526
K
114
115 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
116 return -EINVAL;
117
118 /* Disable the regulator and password protection is level 1 */
119 return tps65218_clear_bits(tps, dev->desc->enable_reg,
120 dev->desc->enable_mask, TPS65218_PROTECT_L1);
121}
122
b9a0d359
TK
123static int tps65218_pmic_set_suspend_enable(struct regulator_dev *dev)
124{
125 struct tps65218 *tps = rdev_get_drvdata(dev);
126 unsigned int rid = rdev_get_id(dev);
127
128 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
129 return -EINVAL;
130
131 return tps65218_clear_bits(tps, dev->desc->bypass_reg,
132 dev->desc->bypass_mask,
133 TPS65218_PROTECT_L1);
134}
135
136static int tps65218_pmic_set_suspend_disable(struct regulator_dev *dev)
137{
138 struct tps65218 *tps = rdev_get_drvdata(dev);
139 unsigned int rid = rdev_get_id(dev);
140
141 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
142 return -EINVAL;
143
23a34f9d
TK
144 /*
145 * Certain revisions of TPS65218 will need to have DCDC3 regulator
146 * enabled always, otherwise an immediate system reboot will occur
147 * during poweroff.
148 */
149 if (rid == TPS65218_DCDC_3 && tps->rev == TPS65218_REV_2_1)
150 return 0;
151
2dc49403 152 if (!tps->strobes[rid]) {
3fb2ef11 153 if (rid == TPS65218_DCDC_3)
efeb88c6 154 tps->strobes[rid] = 3;
3fb2ef11
TK
155 else
156 return -EINVAL;
157 }
b9a0d359
TK
158
159 return tps65218_set_bits(tps, dev->desc->bypass_reg,
160 dev->desc->bypass_mask,
2dc49403 161 tps->strobes[rid], TPS65218_PROTECT_L1);
b9a0d359
TK
162}
163
90e7d526
K
164/* Operations permitted on DCDC1, DCDC2 */
165static struct regulator_ops tps65218_dcdc12_ops = {
166 .is_enabled = regulator_is_enabled_regmap,
167 .enable = tps65218_pmic_enable,
168 .disable = tps65218_pmic_disable,
169 .get_voltage_sel = regulator_get_voltage_sel_regmap,
170 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
171 .list_voltage = regulator_list_voltage_linear_range,
172 .map_voltage = regulator_map_voltage_linear_range,
5ab9be42 173 .set_voltage_time_sel = regulator_set_voltage_time_sel,
b9a0d359
TK
174 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
175 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
90e7d526
K
176};
177
178/* Operations permitted on DCDC3, DCDC4 and LDO1 */
179static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
180 .is_enabled = regulator_is_enabled_regmap,
181 .enable = tps65218_pmic_enable,
182 .disable = tps65218_pmic_disable,
183 .get_voltage_sel = regulator_get_voltage_sel_regmap,
184 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
185 .list_voltage = regulator_list_voltage_linear_range,
186 .map_voltage = regulator_map_voltage_linear_range,
b9a0d359
TK
187 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
188 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
90e7d526
K
189};
190
c0ea88b8
NK
191static const int ls3_currents[] = { 100, 200, 500, 1000 };
192
193static int tps65218_pmic_set_input_current_lim(struct regulator_dev *dev,
194 int lim_uA)
195{
196 unsigned int index = 0;
197 unsigned int num_currents = ARRAY_SIZE(ls3_currents);
198 struct tps65218 *tps = rdev_get_drvdata(dev);
199
200 while (index < num_currents && ls3_currents[index] != lim_uA)
201 index++;
202
203 if (index == num_currents)
204 return -EINVAL;
205
206 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
207 index << 2, TPS65218_PROTECT_L1);
208}
209
210static int tps65218_pmic_set_current_limit(struct regulator_dev *dev,
211 int min_uA, int max_uA)
212{
213 int index = 0;
214 unsigned int num_currents = ARRAY_SIZE(ls3_currents);
215 struct tps65218 *tps = rdev_get_drvdata(dev);
216
217 while (index < num_currents && ls3_currents[index] < max_uA)
218 index++;
219
220 index--;
221
222 if (index < 0 || ls3_currents[index] < min_uA)
223 return -EINVAL;
224
225 return tps65218_set_bits(tps, dev->desc->csel_reg, dev->desc->csel_mask,
226 index << 2, TPS65218_PROTECT_L1);
227}
228
229static int tps65218_pmic_get_current_limit(struct regulator_dev *dev)
230{
231 int retval;
232 unsigned int index;
233 struct tps65218 *tps = rdev_get_drvdata(dev);
234
0aced355 235 retval = regmap_read(tps->regmap, dev->desc->csel_reg, &index);
c0ea88b8
NK
236 if (retval < 0)
237 return retval;
238
239 index = (index & dev->desc->csel_mask) >> 2;
240
241 return ls3_currents[index];
242}
243
244static struct regulator_ops tps65218_ls3_ops = {
245 .is_enabled = regulator_is_enabled_regmap,
246 .enable = tps65218_pmic_enable,
247 .disable = tps65218_pmic_disable,
248 .set_input_current_limit = tps65218_pmic_set_input_current_lim,
249 .set_current_limit = tps65218_pmic_set_current_limit,
250 .get_current_limit = tps65218_pmic_get_current_limit,
251};
252
90e7d526
K
253/* Operations permitted on DCDC5, DCDC6 */
254static struct regulator_ops tps65218_dcdc56_pmic_ops = {
255 .is_enabled = regulator_is_enabled_regmap,
256 .enable = tps65218_pmic_enable,
257 .disable = tps65218_pmic_disable,
b9a0d359
TK
258 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
259 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
90e7d526
K
260};
261
262static const struct regulator_desc regulators[] = {
2dc49403
K
263 TPS65218_REGULATOR("DCDC1", "regulator-dcdc1", TPS65218_DCDC_1,
264 REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
265 TPS65218_REG_CONTROL_DCDC1,
c0ea88b8
NK
266 TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
267 TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
b9a0d359
TK
268 2, 4000, 0, TPS65218_REG_SEQ3,
269 TPS65218_SEQ3_DC1_SEQ_MASK),
2dc49403
K
270 TPS65218_REGULATOR("DCDC2", "regulator-dcdc2", TPS65218_DCDC_2,
271 REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
272 TPS65218_REG_CONTROL_DCDC2,
c0ea88b8
NK
273 TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
274 TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
b9a0d359
TK
275 2, 4000, 0, TPS65218_REG_SEQ3,
276 TPS65218_SEQ3_DC2_SEQ_MASK),
2dc49403
K
277 TPS65218_REGULATOR("DCDC3", "regulator-dcdc3", TPS65218_DCDC_3,
278 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
c0ea88b8 279 TPS65218_REG_CONTROL_DCDC3,
90e7d526 280 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
c0ea88b8 281 TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
b9a0d359 282 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK),
2dc49403
K
283 TPS65218_REGULATOR("DCDC4", "regulator-dcdc4", TPS65218_DCDC_4,
284 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 53,
c0ea88b8
NK
285 TPS65218_REG_CONTROL_DCDC4,
286 TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
287 TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
b9a0d359 288 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK),
2dc49403
K
289 TPS65218_REGULATOR("DCDC5", "regulator-dcdc5", TPS65218_DCDC_5,
290 REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1,
291 -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC5_EN, 0,
292 0, NULL, 0, 0, 1000000, TPS65218_REG_SEQ5,
b9a0d359 293 TPS65218_SEQ5_DC5_SEQ_MASK),
2dc49403
K
294 TPS65218_REGULATOR("DCDC6", "regulator-dcdc6", TPS65218_DCDC_6,
295 REGULATOR_VOLTAGE, tps65218_dcdc56_pmic_ops, 1, -1,
296 -1, TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC6_EN, 0,
297 0, NULL, 0, 0, 1800000, TPS65218_REG_SEQ5,
b9a0d359 298 TPS65218_SEQ5_DC6_SEQ_MASK),
2dc49403
K
299 TPS65218_REGULATOR("LDO1", "regulator-ldo1", TPS65218_LDO_1,
300 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
0eada6a1 301 TPS65218_REG_CONTROL_LDO1,
90e7d526 302 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
c0ea88b8 303 TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
b9a0d359
TK
304 2, 0, 0, TPS65218_REG_SEQ6,
305 TPS65218_SEQ6_LDO1_SEQ_MASK),
2dc49403
K
306 TPS65218_REGULATOR("LS3", "regulator-ls3", TPS65218_LS_3,
307 REGULATOR_CURRENT, tps65218_ls3_ops, 0, 0, 0,
308 TPS65218_REG_ENABLE2, TPS65218_ENABLE2_LS3_EN,
309 TPS65218_REG_CONFIG2, TPS65218_CONFIG2_LS3ILIM_MASK,
310 NULL, 0, 0, 0, 0, 0),
90e7d526
K
311};
312
313static int tps65218_regulator_probe(struct platform_device *pdev)
314{
315 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
90e7d526 316 struct regulator_dev *rdev;
90e7d526 317 struct regulator_config config = { };
2dc49403 318 int i, ret;
b9a0d359 319 unsigned int val;
90e7d526 320
90e7d526 321 config.dev = &pdev->dev;
2dc49403 322 config.dev->of_node = tps->dev->of_node;
90e7d526
K
323 config.driver_data = tps;
324 config.regmap = tps->regmap;
325
2dc49403 326 /* Allocate memory for strobes */
a86854d0
KC
327 tps->strobes = devm_kcalloc(&pdev->dev,
328 TPS65218_NUM_REGULATOR, sizeof(u8),
329 GFP_KERNEL);
5597bfb4
AL
330 if (!tps->strobes)
331 return -ENOMEM;
90e7d526 332
2dc49403
K
333 for (i = 0; i < ARRAY_SIZE(regulators); i++) {
334 rdev = devm_regulator_register(&pdev->dev, &regulators[i],
335 &config);
336 if (IS_ERR(rdev)) {
337 dev_err(tps->dev, "failed to register %s regulator\n",
338 pdev->name);
339 return PTR_ERR(rdev);
340 }
b9a0d359 341
2dc49403
K
342 ret = regmap_read(tps->regmap, regulators[i].bypass_reg, &val);
343 if (ret)
344 return ret;
345
346 tps->strobes[i] = val & regulators[i].bypass_mask;
347 }
b9a0d359 348
90e7d526
K
349 return 0;
350}
351
2dc49403
K
352static const struct platform_device_id tps65218_regulator_id_table[] = {
353 { "tps65218-regulator", },
354 { /* sentinel */ }
355};
356MODULE_DEVICE_TABLE(platform, tps65218_regulator_id_table);
357
90e7d526
K
358static struct platform_driver tps65218_regulator_driver = {
359 .driver = {
360 .name = "tps65218-pmic",
90e7d526
K
361 },
362 .probe = tps65218_regulator_probe,
2dc49403 363 .id_table = tps65218_regulator_id_table,
90e7d526
K
364};
365
366module_platform_driver(tps65218_regulator_driver);
367
368MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
369MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
370MODULE_ALIAS("platform:tps65218-pmic");
371MODULE_LICENSE("GPL v2");