Merge tag 'xtensa-20190816' of git://github.com/jcmvbkbc/linux-xtensa
[linux-2.6-block.git] / drivers / regulator / tps6507x-regulator.c
CommitLineData
3fa5b8e0
AA
1/*
2 * tps6507x-regulator.c
3 *
4 * Regulator driver for TPS65073 PMIC
5 *
6 * Copyright (C) 2009 Texas Instrument 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 as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
7d14831e 25#include <linux/regulator/tps6507x.h>
6116ad94 26#include <linux/of.h>
5a0e3ad6 27#include <linux/slab.h>
d183fcc9 28#include <linux/mfd/tps6507x.h>
6116ad94 29#include <linux/regulator/of_regulator.h>
3fa5b8e0
AA
30
31/* DCDC's */
32#define TPS6507X_DCDC_1 0
33#define TPS6507X_DCDC_2 1
34#define TPS6507X_DCDC_3 2
35/* LDOs */
36#define TPS6507X_LDO_1 3
37#define TPS6507X_LDO_2 4
38
39#define TPS6507X_MAX_REG_ID TPS6507X_LDO_2
40
41/* Number of step-down converters available */
42#define TPS6507X_NUM_DCDC 3
43/* Number of LDO voltage regulators available */
44#define TPS6507X_NUM_LDO 2
45/* Number of total regulators available */
46#define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO)
47
055917ac
AL
48/* Supported voltage values for regulators (in microVolts) */
49static const unsigned int VDCDCx_VSEL_table[] = {
50 725000, 750000, 775000, 800000,
51 825000, 850000, 875000, 900000,
52 925000, 950000, 975000, 1000000,
53 1025000, 1050000, 1075000, 1100000,
54 1125000, 1150000, 1175000, 1200000,
55 1225000, 1250000, 1275000, 1300000,
56 1325000, 1350000, 1375000, 1400000,
57 1425000, 1450000, 1475000, 1500000,
58 1550000, 1600000, 1650000, 1700000,
59 1750000, 1800000, 1850000, 1900000,
60 1950000, 2000000, 2050000, 2100000,
61 2150000, 2200000, 2250000, 2300000,
62 2350000, 2400000, 2450000, 2500000,
63 2550000, 2600000, 2650000, 2700000,
64 2750000, 2800000, 2850000, 2900000,
65 3000000, 3100000, 3200000, 3300000,
3fa5b8e0
AA
66};
67
055917ac
AL
68static const unsigned int LDO1_VSEL_table[] = {
69 1000000, 1100000, 1200000, 1250000,
70 1300000, 1350000, 1400000, 1500000,
71 1600000, 1800000, 2500000, 2750000,
72 2800000, 3000000, 3100000, 3300000,
3fa5b8e0
AA
73};
74
93b07e7b
AL
75/* The voltage mapping table for LDO2 is the same as VDCDCx */
76#define LDO2_VSEL_table VDCDCx_VSEL_table
3fa5b8e0 77
3fa5b8e0
AA
78struct tps_info {
79 const char *name;
3fa5b8e0 80 u8 table_len;
055917ac 81 const unsigned int *table;
7d14831e
AA
82
83 /* Does DCDC high or the low register defines output voltage? */
84 bool defdcdc_default;
3fa5b8e0
AA
85};
86
7d14831e 87static struct tps_info tps6507x_pmic_regs[] = {
31dd6a26
TF
88 {
89 .name = "VDCDC1",
31dd6a26
TF
90 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
91 .table = VDCDCx_VSEL_table,
92 },
93 {
94 .name = "VDCDC2",
31dd6a26
TF
95 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
96 .table = VDCDCx_VSEL_table,
97 },
98 {
99 .name = "VDCDC3",
31dd6a26
TF
100 .table_len = ARRAY_SIZE(VDCDCx_VSEL_table),
101 .table = VDCDCx_VSEL_table,
102 },
103 {
104 .name = "LDO1",
31dd6a26
TF
105 .table_len = ARRAY_SIZE(LDO1_VSEL_table),
106 .table = LDO1_VSEL_table,
107 },
108 {
109 .name = "LDO2",
31dd6a26
TF
110 .table_len = ARRAY_SIZE(LDO2_VSEL_table),
111 .table = LDO2_VSEL_table,
112 },
113};
114
4ce5ba5b 115struct tps6507x_pmic {
3fa5b8e0 116 struct regulator_desc desc[TPS6507X_NUM_REGULATOR];
31dd6a26 117 struct tps6507x_dev *mfd;
7d14831e 118 struct tps_info *info[TPS6507X_NUM_REGULATOR];
3fa5b8e0
AA
119 struct mutex io_lock;
120};
4ce5ba5b 121static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg)
3fa5b8e0 122{
31dd6a26
TF
123 u8 val;
124 int err;
125
126 err = tps->mfd->read_dev(tps->mfd, reg, 1, &val);
127
128 if (err)
129 return err;
130
131 return val;
3fa5b8e0
AA
132}
133
4ce5ba5b 134static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
3fa5b8e0 135{
31dd6a26 136 return tps->mfd->write_dev(tps->mfd, reg, 1, &val);
3fa5b8e0
AA
137}
138
4ce5ba5b 139static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
3fa5b8e0
AA
140{
141 int err, data;
142
143 mutex_lock(&tps->io_lock);
144
4ce5ba5b 145 data = tps6507x_pmic_read(tps, reg);
3fa5b8e0 146 if (data < 0) {
31dd6a26 147 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
3fa5b8e0
AA
148 err = data;
149 goto out;
150 }
151
152 data |= mask;
4ce5ba5b 153 err = tps6507x_pmic_write(tps, reg, data);
3fa5b8e0 154 if (err)
31dd6a26 155 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
3fa5b8e0
AA
156
157out:
158 mutex_unlock(&tps->io_lock);
159 return err;
160}
161
4ce5ba5b 162static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask)
3fa5b8e0
AA
163{
164 int err, data;
165
166 mutex_lock(&tps->io_lock);
167
4ce5ba5b 168 data = tps6507x_pmic_read(tps, reg);
3fa5b8e0 169 if (data < 0) {
31dd6a26 170 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
3fa5b8e0
AA
171 err = data;
172 goto out;
173 }
174
175 data &= ~mask;
4ce5ba5b 176 err = tps6507x_pmic_write(tps, reg, data);
3fa5b8e0 177 if (err)
31dd6a26 178 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
3fa5b8e0
AA
179
180out:
181 mutex_unlock(&tps->io_lock);
182 return err;
183}
184
4ce5ba5b 185static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg)
3fa5b8e0
AA
186{
187 int data;
188
189 mutex_lock(&tps->io_lock);
190
4ce5ba5b 191 data = tps6507x_pmic_read(tps, reg);
3fa5b8e0 192 if (data < 0)
31dd6a26 193 dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg);
3fa5b8e0
AA
194
195 mutex_unlock(&tps->io_lock);
196 return data;
197}
198
4ce5ba5b 199static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val)
3fa5b8e0
AA
200{
201 int err;
202
203 mutex_lock(&tps->io_lock);
204
4ce5ba5b 205 err = tps6507x_pmic_write(tps, reg, val);
3fa5b8e0 206 if (err < 0)
31dd6a26 207 dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg);
3fa5b8e0
AA
208
209 mutex_unlock(&tps->io_lock);
210 return err;
211}
212
f2933d33 213static int tps6507x_pmic_is_enabled(struct regulator_dev *dev)
3fa5b8e0 214{
4ce5ba5b 215 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33 216 int data, rid = rdev_get_id(dev);
3fa5b8e0
AA
217 u8 shift;
218
f2933d33 219 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
3fa5b8e0
AA
220 return -EINVAL;
221
f2933d33 222 shift = TPS6507X_MAX_REG_ID - rid;
4ce5ba5b 223 data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1);
3fa5b8e0
AA
224
225 if (data < 0)
226 return data;
227 else
228 return (data & 1<<shift) ? 1 : 0;
229}
230
f2933d33 231static int tps6507x_pmic_enable(struct regulator_dev *dev)
3fa5b8e0 232{
4ce5ba5b 233 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33 234 int rid = rdev_get_id(dev);
3fa5b8e0
AA
235 u8 shift;
236
f2933d33 237 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
3fa5b8e0
AA
238 return -EINVAL;
239
f2933d33 240 shift = TPS6507X_MAX_REG_ID - rid;
4ce5ba5b 241 return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift);
3fa5b8e0
AA
242}
243
f2933d33 244static int tps6507x_pmic_disable(struct regulator_dev *dev)
3fa5b8e0 245{
4ce5ba5b 246 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33 247 int rid = rdev_get_id(dev);
3fa5b8e0
AA
248 u8 shift;
249
f2933d33 250 if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2)
3fa5b8e0
AA
251 return -EINVAL;
252
f2933d33 253 shift = TPS6507X_MAX_REG_ID - rid;
4ce5ba5b
TF
254 return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1,
255 1 << shift);
3fa5b8e0
AA
256}
257
7c842a1d 258static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev)
3fa5b8e0 259{
4ce5ba5b 260 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
f2933d33
AL
261 int data, rid = rdev_get_id(dev);
262 u8 reg, mask;
3fa5b8e0 263
f2933d33 264 switch (rid) {
3fa5b8e0
AA
265 case TPS6507X_DCDC_1:
266 reg = TPS6507X_REG_DEFDCDC1;
f2933d33 267 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
268 break;
269 case TPS6507X_DCDC_2:
f2933d33 270 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
271 reg = TPS6507X_REG_DEFDCDC2_HIGH;
272 else
273 reg = TPS6507X_REG_DEFDCDC2_LOW;
f2933d33 274 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
275 break;
276 case TPS6507X_DCDC_3:
f2933d33 277 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
278 reg = TPS6507X_REG_DEFDCDC3_HIGH;
279 else
280 reg = TPS6507X_REG_DEFDCDC3_LOW;
f2933d33
AL
281 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
282 break;
283 case TPS6507X_LDO_1:
284 reg = TPS6507X_REG_LDO_CTRL1;
285 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
286 break;
287 case TPS6507X_LDO_2:
288 reg = TPS6507X_REG_DEFLDO2;
289 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
3fa5b8e0
AA
290 break;
291 default:
292 return -EINVAL;
293 }
294
4ce5ba5b 295 data = tps6507x_pmic_reg_read(tps, reg);
3fa5b8e0
AA
296 if (data < 0)
297 return data;
298
f2933d33 299 data &= mask;
7c842a1d 300 return data;
3fa5b8e0
AA
301}
302
ca61a7bf
AL
303static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev,
304 unsigned selector)
3fa5b8e0 305{
4ce5ba5b 306 struct tps6507x_pmic *tps = rdev_get_drvdata(dev);
ca61a7bf 307 int data, rid = rdev_get_id(dev);
f2933d33 308 u8 reg, mask;
3fa5b8e0 309
f2933d33 310 switch (rid) {
3fa5b8e0
AA
311 case TPS6507X_DCDC_1:
312 reg = TPS6507X_REG_DEFDCDC1;
f2933d33 313 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
314 break;
315 case TPS6507X_DCDC_2:
f2933d33 316 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
317 reg = TPS6507X_REG_DEFDCDC2_HIGH;
318 else
319 reg = TPS6507X_REG_DEFDCDC2_LOW;
f2933d33 320 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
3fa5b8e0
AA
321 break;
322 case TPS6507X_DCDC_3:
f2933d33 323 if (tps->info[rid]->defdcdc_default)
7d14831e
AA
324 reg = TPS6507X_REG_DEFDCDC3_HIGH;
325 else
326 reg = TPS6507X_REG_DEFDCDC3_LOW;
f2933d33
AL
327 mask = TPS6507X_DEFDCDCX_DCDC_MASK;
328 break;
329 case TPS6507X_LDO_1:
330 reg = TPS6507X_REG_LDO_CTRL1;
331 mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK;
332 break;
333 case TPS6507X_LDO_2:
334 reg = TPS6507X_REG_DEFLDO2;
335 mask = TPS6507X_REG_DEFLDO2_LDO2_MASK;
3fa5b8e0
AA
336 break;
337 default:
338 return -EINVAL;
339 }
340
4ce5ba5b 341 data = tps6507x_pmic_reg_read(tps, reg);
3fa5b8e0
AA
342 if (data < 0)
343 return data;
344
345 data &= ~mask;
ca61a7bf 346 data |= selector;
3fa5b8e0 347
4ce5ba5b 348 return tps6507x_pmic_reg_write(tps, reg, data);
3fa5b8e0
AA
349}
350
646e268e 351static const struct regulator_ops tps6507x_pmic_ops = {
f2933d33
AL
352 .is_enabled = tps6507x_pmic_is_enabled,
353 .enable = tps6507x_pmic_enable,
354 .disable = tps6507x_pmic_disable,
7c842a1d 355 .get_voltage_sel = tps6507x_pmic_get_voltage_sel,
ca61a7bf 356 .set_voltage_sel = tps6507x_pmic_set_voltage_sel,
055917ac 357 .list_voltage = regulator_list_voltage_table,
a1bb63a8 358 .map_voltage = regulator_map_voltage_ascend,
3fa5b8e0
AA
359};
360
f979c08f
AL
361static int tps6507x_pmic_of_parse_cb(struct device_node *np,
362 const struct regulator_desc *desc,
363 struct regulator_config *config)
6116ad94 364{
f979c08f
AL
365 struct tps6507x_pmic *tps = config->driver_data;
366 struct tps_info *info = tps->info[desc->id];
367 u32 prop;
368 int ret;
6116ad94 369
f979c08f
AL
370 ret = of_property_read_u32(np, "ti,defdcdc_default", &prop);
371 if (!ret)
372 info->defdcdc_default = prop;
6116ad94 373
f979c08f 374 return 0;
6116ad94 375}
4246e55f 376
a5023574 377static int tps6507x_pmic_probe(struct platform_device *pdev)
3fa5b8e0 378{
31dd6a26 379 struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent);
7d14831e 380 struct tps_info *info = &tps6507x_pmic_regs[0];
c172708d 381 struct regulator_config config = { };
f979c08f 382 struct regulator_init_data *init_data = NULL;
3fa5b8e0 383 struct regulator_dev *rdev;
4ce5ba5b 384 struct tps6507x_pmic *tps;
0bc20bba 385 struct tps6507x_board *tps_board;
3fa5b8e0
AA
386 int i;
387
0bc20bba
TF
388 /**
389 * tps_board points to pmic related constants
390 * coming from the board-evm file.
391 */
392
31dd6a26 393 tps_board = dev_get_platdata(tps6507x_dev->dev);
f979c08f
AL
394 if (tps_board)
395 init_data = tps_board->tps6507x_pmic_init_data;
3fa5b8e0 396
9eb0c421 397 tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL);
3fa5b8e0
AA
398 if (!tps)
399 return -ENOMEM;
400
401 mutex_init(&tps->io_lock);
402
403 /* common for all regulators */
31dd6a26 404 tps->mfd = tps6507x_dev;
3fa5b8e0 405
7d293f56 406 for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++) {
3fa5b8e0
AA
407 /* Register the regulators */
408 tps->info[i] = info;
7d293f56 409 if (init_data && init_data[i].driver_data) {
7d14831e 410 struct tps6507x_reg_platform_data *data =
7d293f56 411 init_data[i].driver_data;
f979c08f 412 info->defdcdc_default = data->defdcdc_default;
7d14831e
AA
413 }
414
3fa5b8e0 415 tps->desc[i].name = info->name;
f979c08f
AL
416 tps->desc[i].of_match = of_match_ptr(info->name);
417 tps->desc[i].regulators_node = of_match_ptr("regulators");
418 tps->desc[i].of_parse_cb = tps6507x_pmic_of_parse_cb;
77fa44d0 419 tps->desc[i].id = i;
0fcdb109 420 tps->desc[i].n_voltages = info->table_len;
055917ac 421 tps->desc[i].volt_table = info->table;
f2933d33 422 tps->desc[i].ops = &tps6507x_pmic_ops;
3fa5b8e0
AA
423 tps->desc[i].type = REGULATOR_VOLTAGE;
424 tps->desc[i].owner = THIS_MODULE;
425
c172708d
MB
426 config.dev = tps6507x_dev->dev;
427 config.init_data = init_data;
428 config.driver_data = tps;
429
71b710e7
SK
430 rdev = devm_regulator_register(&pdev->dev, &tps->desc[i],
431 &config);
3fa5b8e0 432 if (IS_ERR(rdev)) {
31dd6a26
TF
433 dev_err(tps6507x_dev->dev,
434 "failed to register %s regulator\n",
435 pdev->name);
71b710e7 436 return PTR_ERR(rdev);
3fa5b8e0 437 }
3fa5b8e0
AA
438 }
439
31dd6a26 440 tps6507x_dev->pmic = tps;
d7399fa8 441 platform_set_drvdata(pdev, tps6507x_dev);
3fa5b8e0
AA
442
443 return 0;
3fa5b8e0
AA
444}
445
31dd6a26 446static struct platform_driver tps6507x_pmic_driver = {
3fa5b8e0 447 .driver = {
31dd6a26 448 .name = "tps6507x-pmic",
3fa5b8e0 449 },
4ce5ba5b 450 .probe = tps6507x_pmic_probe,
3fa5b8e0
AA
451};
452
4ce5ba5b 453static int __init tps6507x_pmic_init(void)
3fa5b8e0 454{
31dd6a26 455 return platform_driver_register(&tps6507x_pmic_driver);
3fa5b8e0 456}
4ce5ba5b 457subsys_initcall(tps6507x_pmic_init);
3fa5b8e0 458
4ce5ba5b 459static void __exit tps6507x_pmic_cleanup(void)
3fa5b8e0 460{
31dd6a26 461 platform_driver_unregister(&tps6507x_pmic_driver);
3fa5b8e0 462}
4ce5ba5b 463module_exit(tps6507x_pmic_cleanup);
3fa5b8e0
AA
464
465MODULE_AUTHOR("Texas Instruments");
466MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
9e108d33 467MODULE_LICENSE("GPL v2");
31dd6a26 468MODULE_ALIAS("platform:tps6507x-pmic");