blob: a02c1b9610396aeb981ff68a8989d57ec6dac6a1 [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
Keerthy90e7d522014-02-06 11:20:13 +053030enum tps65218_regulators { DCDC1, DCDC2, DCDC3, DCDC4, DCDC5, DCDC6, LDO1 };
31
Felipe Balbidd648ef2014-07-08 14:09:13 -050032#define TPS65218_REGULATOR(_name, _id, _ops, _n, _vr, _vm, _er, _em, \
Keerthy7753d2d2014-07-09 15:52:59 +053033 _lr, _nlr, _delay, _fuv) \
Keerthy90e7d522014-02-06 11:20:13 +053034 { \
35 .name = _name, \
36 .id = _id, \
37 .ops = &_ops, \
38 .n_voltages = _n, \
39 .type = REGULATOR_VOLTAGE, \
40 .owner = THIS_MODULE, \
41 .vsel_reg = _vr, \
42 .vsel_mask = _vm, \
43 .enable_reg = _er, \
44 .enable_mask = _em, \
Felipe Balbidd648ef2014-07-08 14:09:13 -050045 .volt_table = NULL, \
Keerthy90e7d522014-02-06 11:20:13 +053046 .linear_ranges = _lr, \
47 .n_linear_ranges = _nlr, \
Axel Lin5ab9be42014-05-22 09:26:47 +080048 .ramp_delay = _delay, \
Keerthy7753d2d2014-07-09 15:52:59 +053049 .fixed_uV = _fuv \
Keerthy90e7d522014-02-06 11:20:13 +053050 } \
51
52#define TPS65218_INFO(_id, _nm, _min, _max) \
Felipe Balbi6a5e06d2014-07-08 14:09:14 -050053 [_id] = { \
Keerthy90e7d522014-02-06 11:20:13 +053054 .id = _id, \
55 .name = _nm, \
56 .min_uV = _min, \
57 .max_uV = _max, \
58 }
59
60static const struct regulator_linear_range dcdc1_dcdc2_ranges[] = {
61 REGULATOR_LINEAR_RANGE(850000, 0x0, 0x32, 10000),
62 REGULATOR_LINEAR_RANGE(1375000, 0x33, 0x3f, 25000),
63};
64
65static const struct regulator_linear_range ldo1_dcdc3_ranges[] = {
66 REGULATOR_LINEAR_RANGE(900000, 0x0, 0x1a, 25000),
67 REGULATOR_LINEAR_RANGE(1600000, 0x1b, 0x3f, 50000),
68};
69
70static const struct regulator_linear_range dcdc4_ranges[] = {
71 REGULATOR_LINEAR_RANGE(1175000, 0x0, 0xf, 25000),
Felipe Balbi42ab0f32014-07-08 14:09:12 -050072 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
Keerthy90e7d522014-02-06 11:20:13 +053073};
74
75static struct tps_info tps65218_pmic_regs[] = {
Andrew F. Davis0f1d08d2015-09-15 15:34:22 -050076 TPS65218_INFO(DCDC1, "DCDC1", 850000, 1675000),
Felipe Balbi6a5e06d2014-07-08 14:09:14 -050077 TPS65218_INFO(DCDC2, "DCDC2", 850000, 1675000),
78 TPS65218_INFO(DCDC3, "DCDC3", 900000, 3400000),
79 TPS65218_INFO(DCDC4, "DCDC4", 1175000, 3400000),
80 TPS65218_INFO(DCDC5, "DCDC5", 1000000, 1000000),
81 TPS65218_INFO(DCDC6, "DCDC6", 1800000, 1800000),
82 TPS65218_INFO(LDO1, "LDO1", 900000, 3400000),
Keerthy90e7d522014-02-06 11:20:13 +053083};
84
85#define TPS65218_OF_MATCH(comp, label) \
86 { \
87 .compatible = comp, \
88 .data = &label, \
89 }
90
91static const struct of_device_id tps65218_of_match[] = {
92 TPS65218_OF_MATCH("ti,tps65218-dcdc1", tps65218_pmic_regs[DCDC1]),
93 TPS65218_OF_MATCH("ti,tps65218-dcdc2", tps65218_pmic_regs[DCDC2]),
94 TPS65218_OF_MATCH("ti,tps65218-dcdc3", tps65218_pmic_regs[DCDC3]),
95 TPS65218_OF_MATCH("ti,tps65218-dcdc4", tps65218_pmic_regs[DCDC4]),
96 TPS65218_OF_MATCH("ti,tps65218-dcdc5", tps65218_pmic_regs[DCDC5]),
97 TPS65218_OF_MATCH("ti,tps65218-dcdc6", tps65218_pmic_regs[DCDC6]),
98 TPS65218_OF_MATCH("ti,tps65218-ldo1", tps65218_pmic_regs[LDO1]),
Axel Linc46b5292014-02-19 16:33:21 +080099 { }
Keerthy90e7d522014-02-06 11:20:13 +0530100};
101MODULE_DEVICE_TABLE(of, tps65218_of_match);
102
103static int tps65218_pmic_set_voltage_sel(struct regulator_dev *dev,
104 unsigned selector)
105{
106 int ret;
107 struct tps65218 *tps = rdev_get_drvdata(dev);
108 unsigned int rid = rdev_get_id(dev);
109
110 /* Set the voltage based on vsel value and write protect level is 2 */
111 ret = tps65218_set_bits(tps, dev->desc->vsel_reg, dev->desc->vsel_mask,
112 selector, TPS65218_PROTECT_L1);
113
114 /* Set GO bit for DCDC1/2 to initiate voltage transistion */
115 switch (rid) {
116 case TPS65218_DCDC_1:
117 case TPS65218_DCDC_2:
118 ret = tps65218_set_bits(tps, TPS65218_REG_CONTRL_SLEW_RATE,
119 TPS65218_SLEW_RATE_GO,
120 TPS65218_SLEW_RATE_GO,
121 TPS65218_PROTECT_L1);
122 break;
123 }
124
125 return ret;
126}
127
128static int tps65218_pmic_enable(struct regulator_dev *dev)
129{
130 struct tps65218 *tps = rdev_get_drvdata(dev);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530131 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530132
133 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
134 return -EINVAL;
135
136 /* Enable the regulator and password protection is level 1 */
137 return tps65218_set_bits(tps, dev->desc->enable_reg,
138 dev->desc->enable_mask, dev->desc->enable_mask,
139 TPS65218_PROTECT_L1);
140}
141
142static int tps65218_pmic_disable(struct regulator_dev *dev)
143{
144 struct tps65218 *tps = rdev_get_drvdata(dev);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530145 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530146
147 if (rid < TPS65218_DCDC_1 || rid > TPS65218_LDO_1)
148 return -EINVAL;
149
150 /* Disable the regulator and password protection is level 1 */
151 return tps65218_clear_bits(tps, dev->desc->enable_reg,
152 dev->desc->enable_mask, TPS65218_PROTECT_L1);
153}
154
Keerthy90e7d522014-02-06 11:20:13 +0530155/* Operations permitted on DCDC1, DCDC2 */
156static struct regulator_ops tps65218_dcdc12_ops = {
157 .is_enabled = regulator_is_enabled_regmap,
158 .enable = tps65218_pmic_enable,
159 .disable = tps65218_pmic_disable,
160 .get_voltage_sel = regulator_get_voltage_sel_regmap,
161 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
162 .list_voltage = regulator_list_voltage_linear_range,
163 .map_voltage = regulator_map_voltage_linear_range,
Axel Lin5ab9be42014-05-22 09:26:47 +0800164 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Keerthy90e7d522014-02-06 11:20:13 +0530165};
166
167/* Operations permitted on DCDC3, DCDC4 and LDO1 */
168static struct regulator_ops tps65218_ldo1_dcdc34_ops = {
169 .is_enabled = regulator_is_enabled_regmap,
170 .enable = tps65218_pmic_enable,
171 .disable = tps65218_pmic_disable,
172 .get_voltage_sel = regulator_get_voltage_sel_regmap,
173 .set_voltage_sel = tps65218_pmic_set_voltage_sel,
174 .list_voltage = regulator_list_voltage_linear_range,
175 .map_voltage = regulator_map_voltage_linear_range,
176};
177
178/* Operations permitted on DCDC5, DCDC6 */
179static struct regulator_ops tps65218_dcdc56_pmic_ops = {
180 .is_enabled = regulator_is_enabled_regmap,
181 .enable = tps65218_pmic_enable,
182 .disable = tps65218_pmic_disable,
183};
184
185static const struct regulator_desc regulators[] = {
186 TPS65218_REGULATOR("DCDC1", TPS65218_DCDC_1, tps65218_dcdc12_ops, 64,
187 TPS65218_REG_CONTROL_DCDC1,
188 TPS65218_CONTROL_DCDC1_MASK,
Felipe Balbidd648ef2014-07-08 14:09:13 -0500189 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC1_EN,
Keerthy7753d2d2014-07-09 15:52:59 +0530190 dcdc1_dcdc2_ranges, 2, 4000, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530191 TPS65218_REGULATOR("DCDC2", TPS65218_DCDC_2, tps65218_dcdc12_ops, 64,
192 TPS65218_REG_CONTROL_DCDC2,
193 TPS65218_CONTROL_DCDC2_MASK,
Felipe Balbidd648ef2014-07-08 14:09:13 -0500194 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC2_EN,
Keerthy7753d2d2014-07-09 15:52:59 +0530195 dcdc1_dcdc2_ranges, 2, 4000, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530196 TPS65218_REGULATOR("DCDC3", TPS65218_DCDC_3, tps65218_ldo1_dcdc34_ops,
197 64, TPS65218_REG_CONTROL_DCDC3,
198 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
Keerthy7753d2d2014-07-09 15:52:59 +0530199 TPS65218_ENABLE1_DC3_EN, ldo1_dcdc3_ranges, 2, 0, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530200 TPS65218_REGULATOR("DCDC4", TPS65218_DCDC_4, tps65218_ldo1_dcdc34_ops,
201 53, TPS65218_REG_CONTROL_DCDC4,
202 TPS65218_CONTROL_DCDC4_MASK,
Felipe Balbidd648ef2014-07-08 14:09:13 -0500203 TPS65218_REG_ENABLE1, TPS65218_ENABLE1_DC4_EN,
Keerthy7753d2d2014-07-09 15:52:59 +0530204 dcdc4_ranges, 2, 0, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530205 TPS65218_REGULATOR("DCDC5", TPS65218_DCDC_5, tps65218_dcdc56_pmic_ops,
206 1, -1, -1, TPS65218_REG_ENABLE1,
Keerthy7753d2d2014-07-09 15:52:59 +0530207 TPS65218_ENABLE1_DC5_EN, NULL, 0, 0, 1000000),
Keerthy90e7d522014-02-06 11:20:13 +0530208 TPS65218_REGULATOR("DCDC6", TPS65218_DCDC_6, tps65218_dcdc56_pmic_ops,
209 1, -1, -1, TPS65218_REG_ENABLE1,
Keerthy7753d2d2014-07-09 15:52:59 +0530210 TPS65218_ENABLE1_DC6_EN, NULL, 0, 0, 1800000),
Keerthy90e7d522014-02-06 11:20:13 +0530211 TPS65218_REGULATOR("LDO1", TPS65218_LDO_1, tps65218_ldo1_dcdc34_ops, 64,
Keerthy0eada6a2014-06-18 10:17:48 -0500212 TPS65218_REG_CONTROL_LDO1,
Keerthy90e7d522014-02-06 11:20:13 +0530213 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
Felipe Balbidd648ef2014-07-08 14:09:13 -0500214 TPS65218_ENABLE2_LDO1_EN, ldo1_dcdc3_ranges,
Keerthy7753d2d2014-07-09 15:52:59 +0530215 2, 0, 0),
Keerthy90e7d522014-02-06 11:20:13 +0530216};
217
218static int tps65218_regulator_probe(struct platform_device *pdev)
219{
220 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
221 struct regulator_init_data *init_data;
222 const struct tps_info *template;
223 struct regulator_dev *rdev;
224 const struct of_device_id *match;
225 struct regulator_config config = { };
226 int id;
227
228 match = of_match_device(tps65218_of_match, &pdev->dev);
Axel Lin948838a2014-02-19 16:35:03 +0800229 if (!match)
Keerthy90e7d522014-02-06 11:20:13 +0530230 return -ENODEV;
Axel Lin948838a2014-02-19 16:35:03 +0800231
232 template = match->data;
233 id = template->id;
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100234 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
235 &regulators[id]);
Keerthy90e7d522014-02-06 11:20:13 +0530236
237 platform_set_drvdata(pdev, tps);
238
239 tps->info[id] = &tps65218_pmic_regs[id];
240 config.dev = &pdev->dev;
241 config.init_data = init_data;
242 config.driver_data = tps;
243 config.regmap = tps->regmap;
Keerthyd2fa87c2014-06-18 10:17:47 -0500244 config.of_node = pdev->dev.of_node;
Keerthy90e7d522014-02-06 11:20:13 +0530245
246 rdev = devm_regulator_register(&pdev->dev, &regulators[id], &config);
247 if (IS_ERR(rdev)) {
248 dev_err(tps->dev, "failed to register %s regulator\n",
249 pdev->name);
250 return PTR_ERR(rdev);
251 }
252
Keerthy90e7d522014-02-06 11:20:13 +0530253 return 0;
254}
255
256static struct platform_driver tps65218_regulator_driver = {
257 .driver = {
258 .name = "tps65218-pmic",
Axel Lin948838a2014-02-19 16:35:03 +0800259 .of_match_table = tps65218_of_match,
Keerthy90e7d522014-02-06 11:20:13 +0530260 },
261 .probe = tps65218_regulator_probe,
Keerthy90e7d522014-02-06 11:20:13 +0530262};
263
264module_platform_driver(tps65218_regulator_driver);
265
266MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
267MODULE_DESCRIPTION("TPS65218 voltage regulator driver");
268MODULE_ALIAS("platform:tps65218-pmic");
269MODULE_LICENSE("GPL v2");