blob: 6209beee10188d6c17e0d9f395367909a4e3aacd [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>
Keerthy0aced352016-09-19 13:09:02 +053025#include <linux/regmap.h>
Keerthy90e7d522014-02-06 11:20:13 +053026#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
Keerthy2dc49402016-09-19 13:09:06 +053031#define TPS65218_REGULATOR(_name, _of, _id, _type, _ops, _n, _vr, _vm, _er, \
32 _em, _cr, _cm, _lr, _nlr, _delay, _fuv, _sr, _sm) \
Keerthy90e7d522014-02-06 11:20:13 +053033 { \
34 .name = _name, \
Keerthy2dc49402016-09-19 13:09:06 +053035 .of_match = _of, \
Keerthy90e7d522014-02-06 11:20:13 +053036 .id = _id, \
37 .ops = &_ops, \
38 .n_voltages = _n, \
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020039 .type = _type, \
Keerthy90e7d522014-02-06 11:20:13 +053040 .owner = THIS_MODULE, \
41 .vsel_reg = _vr, \
42 .vsel_mask = _vm, \
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +020043 .csel_reg = _cr, \
44 .csel_mask = _cm, \
Keerthy90e7d522014-02-06 11:20:13 +053045 .enable_reg = _er, \
46 .enable_mask = _em, \
Felipe Balbidd648ef2014-07-08 14:09:13 -050047 .volt_table = NULL, \
Keerthy90e7d522014-02-06 11:20:13 +053048 .linear_ranges = _lr, \
49 .n_linear_ranges = _nlr, \
Axel Lin5ab9be42014-05-22 09:26:47 +080050 .ramp_delay = _delay, \
Tero Kristob9a0d352016-06-24 13:58:08 +053051 .fixed_uV = _fuv, \
52 .bypass_reg = _sr, \
53 .bypass_mask = _sm, \
Keerthy90e7d522014-02-06 11:20:13 +053054 } \
55
Keerthy90e7d522014-02-06 11:20:13 +053056static 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),
Felipe Balbi42ab0f32014-07-08 14:09:12 -050068 REGULATOR_LINEAR_RANGE(1600000, 0x10, 0x34, 50000),
Keerthy90e7d522014-02-06 11:20:13 +053069};
70
Keerthy90e7d522014-02-06 11:20:13 +053071static 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);
Sachin Kamat5f986f72014-06-24 13:59:16 +053099 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530100
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);
Sachin Kamat5f986f72014-06-24 13:59:16 +0530113 int rid = rdev_get_id(dev);
Keerthy90e7d522014-02-06 11:20:13 +0530114
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
Tero Kristob9a0d352016-06-24 13:58:08 +0530123static 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
Tero Kristo23a34f92016-08-10 17:53:55 +0530144 /*
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
Keerthy2dc49402016-09-19 13:09:06 +0530152 if (!tps->strobes[rid]) {
Tero Kristo3fb2ef12016-06-24 13:58:09 +0530153 if (rid == TPS65218_DCDC_3)
Keerthyefeb88c2017-11-10 17:22:43 +0530154 tps->strobes[rid] = 3;
Tero Kristo3fb2ef12016-06-24 13:58:09 +0530155 else
156 return -EINVAL;
157 }
Tero Kristob9a0d352016-06-24 13:58:08 +0530158
159 return tps65218_set_bits(tps, dev->desc->bypass_reg,
160 dev->desc->bypass_mask,
Keerthy2dc49402016-09-19 13:09:06 +0530161 tps->strobes[rid], TPS65218_PROTECT_L1);
Tero Kristob9a0d352016-06-24 13:58:08 +0530162}
163
Keerthy90e7d522014-02-06 11:20:13 +0530164/* 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,
Axel Lin5ab9be42014-05-22 09:26:47 +0800173 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Tero Kristob9a0d352016-06-24 13:58:08 +0530174 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
175 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
Keerthy90e7d522014-02-06 11:20:13 +0530176};
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,
Tero Kristob9a0d352016-06-24 13:58:08 +0530187 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
188 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
Keerthy90e7d522014-02-06 11:20:13 +0530189};
190
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200191static 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
Keerthy0aced352016-09-19 13:09:02 +0530235 retval = regmap_read(tps->regmap, dev->desc->csel_reg, &index);
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200236 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
Keerthy90e7d522014-02-06 11:20:13 +0530253/* 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,
Tero Kristob9a0d352016-06-24 13:58:08 +0530258 .set_suspend_enable = tps65218_pmic_set_suspend_enable,
259 .set_suspend_disable = tps65218_pmic_set_suspend_disable,
Keerthy90e7d522014-02-06 11:20:13 +0530260};
261
262static const struct regulator_desc regulators[] = {
Keerthy2dc49402016-09-19 13:09:06 +0530263 TPS65218_REGULATOR("DCDC1", "regulator-dcdc1", TPS65218_DCDC_1,
264 REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
265 TPS65218_REG_CONTROL_DCDC1,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200266 TPS65218_CONTROL_DCDC1_MASK, TPS65218_REG_ENABLE1,
267 TPS65218_ENABLE1_DC1_EN, 0, 0, dcdc1_dcdc2_ranges,
Tero Kristob9a0d352016-06-24 13:58:08 +0530268 2, 4000, 0, TPS65218_REG_SEQ3,
269 TPS65218_SEQ3_DC1_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530270 TPS65218_REGULATOR("DCDC2", "regulator-dcdc2", TPS65218_DCDC_2,
271 REGULATOR_VOLTAGE, tps65218_dcdc12_ops, 64,
272 TPS65218_REG_CONTROL_DCDC2,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200273 TPS65218_CONTROL_DCDC2_MASK, TPS65218_REG_ENABLE1,
274 TPS65218_ENABLE1_DC2_EN, 0, 0, dcdc1_dcdc2_ranges,
Tero Kristob9a0d352016-06-24 13:58:08 +0530275 2, 4000, 0, TPS65218_REG_SEQ3,
276 TPS65218_SEQ3_DC2_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530277 TPS65218_REGULATOR("DCDC3", "regulator-dcdc3", TPS65218_DCDC_3,
278 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200279 TPS65218_REG_CONTROL_DCDC3,
Keerthy90e7d522014-02-06 11:20:13 +0530280 TPS65218_CONTROL_DCDC3_MASK, TPS65218_REG_ENABLE1,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200281 TPS65218_ENABLE1_DC3_EN, 0, 0, ldo1_dcdc3_ranges, 2,
Tero Kristob9a0d352016-06-24 13:58:08 +0530282 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC3_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530283 TPS65218_REGULATOR("DCDC4", "regulator-dcdc4", TPS65218_DCDC_4,
284 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 53,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200285 TPS65218_REG_CONTROL_DCDC4,
286 TPS65218_CONTROL_DCDC4_MASK, TPS65218_REG_ENABLE1,
287 TPS65218_ENABLE1_DC4_EN, 0, 0, dcdc4_ranges, 2,
Tero Kristob9a0d352016-06-24 13:58:08 +0530288 0, 0, TPS65218_REG_SEQ4, TPS65218_SEQ4_DC4_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530289 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,
Tero Kristob9a0d352016-06-24 13:58:08 +0530293 TPS65218_SEQ5_DC5_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530294 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,
Tero Kristob9a0d352016-06-24 13:58:08 +0530298 TPS65218_SEQ5_DC6_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530299 TPS65218_REGULATOR("LDO1", "regulator-ldo1", TPS65218_LDO_1,
300 REGULATOR_VOLTAGE, tps65218_ldo1_dcdc34_ops, 64,
Keerthy0eada6a2014-06-18 10:17:48 -0500301 TPS65218_REG_CONTROL_LDO1,
Keerthy90e7d522014-02-06 11:20:13 +0530302 TPS65218_CONTROL_LDO1_MASK, TPS65218_REG_ENABLE2,
Nikita Kiryanovc0ea88b2015-11-25 13:59:04 +0200303 TPS65218_ENABLE2_LDO1_EN, 0, 0, ldo1_dcdc3_ranges,
Tero Kristob9a0d352016-06-24 13:58:08 +0530304 2, 0, 0, TPS65218_REG_SEQ6,
305 TPS65218_SEQ6_LDO1_SEQ_MASK),
Keerthy2dc49402016-09-19 13:09:06 +0530306 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),
Keerthy90e7d522014-02-06 11:20:13 +0530311};
312
313static int tps65218_regulator_probe(struct platform_device *pdev)
314{
315 struct tps65218 *tps = dev_get_drvdata(pdev->dev.parent);
Keerthy90e7d522014-02-06 11:20:13 +0530316 struct regulator_dev *rdev;
Keerthy90e7d522014-02-06 11:20:13 +0530317 struct regulator_config config = { };
Keerthy2dc49402016-09-19 13:09:06 +0530318 int i, ret;
Tero Kristob9a0d352016-06-24 13:58:08 +0530319 unsigned int val;
Keerthy90e7d522014-02-06 11:20:13 +0530320
Keerthy90e7d522014-02-06 11:20:13 +0530321 config.dev = &pdev->dev;
Keerthy2dc49402016-09-19 13:09:06 +0530322 config.dev->of_node = tps->dev->of_node;
Keerthy90e7d522014-02-06 11:20:13 +0530323 config.driver_data = tps;
324 config.regmap = tps->regmap;
325
Keerthy2dc49402016-09-19 13:09:06 +0530326 /* Allocate memory for strobes */
Kees Cooka86854d2018-06-12 14:07:58 -0700327 tps->strobes = devm_kcalloc(&pdev->dev,
328 TPS65218_NUM_REGULATOR, sizeof(u8),
329 GFP_KERNEL);
Axel Lin5597bfb2017-11-13 20:52:32 +0800330 if (!tps->strobes)
331 return -ENOMEM;
Keerthy2dc49402016-09-19 13:09:06 +0530332
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 }
341
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;
Keerthy90e7d522014-02-06 11:20:13 +0530347 }
348
Keerthy90e7d522014-02-06 11:20:13 +0530349 return 0;
350}
351
Keerthy2dc49402016-09-19 13:09:06 +0530352static const struct platform_device_id tps65218_regulator_id_table[] = {
353 { "tps65218-regulator", },
354 { /* sentinel */ }
355};
356MODULE_DEVICE_TABLE(platform, tps65218_regulator_id_table);
357
Keerthy90e7d522014-02-06 11:20:13 +0530358static struct platform_driver tps65218_regulator_driver = {
359 .driver = {
360 .name = "tps65218-pmic",
Keerthy90e7d522014-02-06 11:20:13 +0530361 },
362 .probe = tps65218_regulator_probe,
Keerthy2dc49402016-09-19 13:09:06 +0530363 .id_table = tps65218_regulator_id_table,
Keerthy90e7d522014-02-06 11:20:13 +0530364};
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");