fsl/fman: fix error handling
[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>
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
c0ea88b8
NK
30enum tps65218_regulators { DCDC1, DCDC2, DCDC3, DCDC4,
31 DCDC5, DCDC6, LDO1, LS3 };
90e7d526 32
c0ea88b8
NK
33#define TPS65218_REGULATOR(_name, _id, _type, _ops, _n, _vr, _vm, _er, _em, \
34 _cr, _cm, _lr, _nlr, _delay, _fuv) \
90e7d526
K
35 { \
36 .name = _name, \
37 .id = _id, \
38 .ops = &_ops, \
39 .n_voltages = _n, \
c0ea88b8 40 .type = _type, \
90e7d526
K
41 .owner = THIS_MODULE, \
42 .vsel_reg = _vr, \
43 .vsel_mask = _vm, \
c0ea88b8
NK
44 .csel_reg = _cr, \
45 .csel_mask = _cm, \
90e7d526
K
46 .enable_reg = _er, \
47 .enable_mask = _em, \
dd648ef2 48 .volt_table = NULL, \
90e7d526
K
49 .linear_ranges = _lr, \
50 .n_linear_ranges = _nlr, \
5ab9be42 51 .ramp_delay = _delay, \
7753d2d4 52 .fixed_uV = _fuv \
90e7d526
K
53 } \
54
55#define TPS65218_INFO(_id, _nm, _min, _max) \
6a5e06d9 56 [_id] = { \
90e7d526
K
57 .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),
42ab0f39 75 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
90e7d526
K
76};
77
78static struct tps_info tps65218_pmic_regs[] = {
0f1d08dd 79 TPS65218_INFO(DCDC1, "DCDC1", 850000, 1675000),
6a5e06d9
FB
80 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),
c0ea88b8 86 TPS65218_INFO(LS3, "LS3", -1, -1),
90e7d526
K
87};
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]),
c0ea88b8 103 TPS65218_OF_MATCH("ti,tps65218-ls3", tps65218_pmic_regs[LS3]),
c46b5295 104 { }
90e7d526
K
105};
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);
5f986f7c 136 int rid = rdev_get_id(dev);
90e7d526
K
137
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);
5f986f7c 150 int rid = rdev_get_id(dev);
90e7d526
K
151
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
90e7d526
K
160/* 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,
5ab9be42 169 .set_voltage_time_sel = regulator_set_voltage_time_sel,
90e7d526
K
170};
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
c0ea88b8
NK
183static 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
90e7d526
K
245/* 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[] = {
c0ea88b8
NK
253 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,
90e7d526 266 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
c0ea88b8
NK
267 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,
0eada6a1 285 TPS65218_REG_CONTROL_LDO1,
90e7d526 286 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
c0ea88b8 287 TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
7753d2d4 288 2, 0, 0),
c0ea88b8
NK
289 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),
90e7d526
K
293};
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);
948838a1 306 if (!match)
90e7d526 307 return -ENODEV;
948838a1
AL
308
309 template = match->data;
310 id = template->id;
072e78b1
JMC
311 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
312 &regulators[id]);
90e7d526
K
313
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;
d2fa87c3 321 config.of_node = pdev->dev.of_node;
90e7d526
K
322
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
90e7d526
K
330 return 0;
331}
332
333static struct platform_driver tps65218_regulator_driver = {
334 .driver = {
335 .name = "tps65218-pmic",
948838a1 336 .of_match_table = tps65218_of_match,
90e7d526
K
337 },
338 .probe = tps65218_regulator_probe,
90e7d526
K
339};
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");