blob: ca04e9f010e1bddd4704a4aec59f5ec3cc6d6d7a [file] [log] [blame]
Venu Byravarasu452534e2012-03-22 18:34:09 +05301/*
2 * Regulator driver for tps65090 power management chip.
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 */
18
19#include <linux/module.h>
Venu Byravarasu452534e2012-03-22 18:34:09 +053020#include <linux/init.h>
Laxman Dewanganf329b172012-10-09 15:19:02 +053021#include <linux/gpio.h>
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +053022#include <linux/of_gpio.h>
Venu Byravarasu452534e2012-03-22 18:34:09 +053023#include <linux/slab.h>
24#include <linux/err.h>
25#include <linux/platform_device.h>
26#include <linux/regulator/driver.h>
27#include <linux/regulator/machine.h>
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +053028#include <linux/regulator/of_regulator.h>
Venu Byravarasu452534e2012-03-22 18:34:09 +053029#include <linux/mfd/tps65090.h>
Venu Byravarasu452534e2012-03-22 18:34:09 +053030
Doug Anderson29041442014-04-16 16:12:28 -070031#define CTRL_WT_BIT 2 /* Regulator wait time 0 bit */
32
33#define MAX_OVERCURRENT_WAIT 3 /* Overcurrent wait must be <= this */
34
35/**
36 * struct tps65090_regulator - Per-regulator data for a tps65090 regulator
37 *
38 * @dev: Pointer to our device.
39 * @desc: The struct regulator_desc for the regulator.
40 * @rdev: The struct regulator_dev for the regulator.
41 * @overcurrent_wait_valid: True if overcurrent_wait is valid.
42 * @overcurrent_wait: For FETs, the value to put in the WTFET bitfield.
43 */
44
Venu Byravarasu452534e2012-03-22 18:34:09 +053045struct tps65090_regulator {
Venu Byravarasu452534e2012-03-22 18:34:09 +053046 struct device *dev;
Laxman Dewangan24282a12012-10-09 15:18:59 +053047 struct regulator_desc *desc;
48 struct regulator_dev *rdev;
Doug Anderson29041442014-04-16 16:12:28 -070049 bool overcurrent_wait_valid;
50 int overcurrent_wait;
Venu Byravarasu452534e2012-03-22 18:34:09 +053051};
52
Laxman Dewanganf329b172012-10-09 15:19:02 +053053static struct regulator_ops tps65090_ext_control_ops = {
54};
55
Doug Anderson29041442014-04-16 16:12:28 -070056/**
57 * tps65090_reg_set_overcurrent_wait - Setup overcurrent wait
58 *
59 * This will set the overcurrent wait time based on what's in the regulator
60 * info.
61 *
62 * @ri: Overall regulator data
63 * @rdev: Regulator device
64 *
65 * Return: 0 if no error, non-zero if there was an error writing the register.
66 */
67static int tps65090_reg_set_overcurrent_wait(struct tps65090_regulator *ri,
68 struct regulator_dev *rdev)
69{
70 int ret;
71
72 ret = regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
73 MAX_OVERCURRENT_WAIT << CTRL_WT_BIT,
74 ri->overcurrent_wait << CTRL_WT_BIT);
75 if (ret) {
76 dev_err(&rdev->dev, "Error updating overcurrent wait %#x\n",
77 rdev->desc->enable_reg);
78 }
79
80 return ret;
81}
82
Laxman Dewanganf329b172012-10-09 15:19:02 +053083static struct regulator_ops tps65090_reg_contol_ops = {
84 .enable = regulator_enable_regmap,
85 .disable = regulator_disable_regmap,
86 .is_enabled = regulator_is_enabled_regmap,
Venu Byravarasu452534e2012-03-22 18:34:09 +053087};
88
Laxman Dewangan3a81ef82012-10-09 15:19:01 +053089static struct regulator_ops tps65090_ldo_ops = {
90};
91
Laxman Dewangan24282a12012-10-09 15:18:59 +053092#define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
Venu Byravarasu452534e2012-03-22 18:34:09 +053093{ \
Laxman Dewangan24282a12012-10-09 15:18:59 +053094 .name = "TPS65090_RAILS"#_id, \
95 .supply_name = _sname, \
Laxman Dewangan8620ca92012-10-09 15:19:00 +053096 .id = TPS65090_REGULATOR_##_id, \
Laxman Dewangan24282a12012-10-09 15:18:59 +053097 .ops = &_ops, \
98 .enable_reg = _en_reg, \
99 .enable_mask = BIT(0), \
100 .type = REGULATOR_VOLTAGE, \
101 .owner = THIS_MODULE, \
Venu Byravarasu452534e2012-03-22 18:34:09 +0530102}
103
Laxman Dewangan24282a12012-10-09 15:18:59 +0530104static struct regulator_desc tps65090_regulator_desc[] = {
Laxman Dewanganf329b172012-10-09 15:19:02 +0530105 tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops),
106 tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops),
107 tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops),
108 tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops),
109 tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops),
110 tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops),
111 tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops),
112 tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops),
113 tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops),
114 tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops),
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530115 tps65090_REG_DESC(LDO1, "vsys-l1", 0, tps65090_ldo_ops),
116 tps65090_REG_DESC(LDO2, "vsys-l2", 0, tps65090_ldo_ops),
Venu Byravarasu452534e2012-03-22 18:34:09 +0530117};
118
Laxman Dewangan24282a12012-10-09 15:18:59 +0530119static inline bool is_dcdc(int id)
Venu Byravarasu452534e2012-03-22 18:34:09 +0530120{
Laxman Dewangan24282a12012-10-09 15:18:59 +0530121 switch (id) {
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530122 case TPS65090_REGULATOR_DCDC1:
123 case TPS65090_REGULATOR_DCDC2:
124 case TPS65090_REGULATOR_DCDC3:
Laxman Dewangan24282a12012-10-09 15:18:59 +0530125 return true;
126 default:
127 return false;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530128 }
Laxman Dewangan24282a12012-10-09 15:18:59 +0530129}
130
Bill Pembertona5023572012-11-19 13:22:22 -0500131static int tps65090_config_ext_control(
Laxman Dewangan24282a12012-10-09 15:18:59 +0530132 struct tps65090_regulator *ri, bool enable)
133{
134 int ret;
135 struct device *parent = ri->dev->parent;
136 unsigned int reg_en_reg = ri->desc->enable_reg;
137
138 if (enable)
139 ret = tps65090_set_bits(parent, reg_en_reg, 1);
140 else
141 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
142 if (ret < 0)
143 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
144 return ret;
145}
146
Bill Pembertona5023572012-11-19 13:22:22 -0500147static int tps65090_regulator_disable_ext_control(
Laxman Dewangan24282a12012-10-09 15:18:59 +0530148 struct tps65090_regulator *ri,
149 struct tps65090_regulator_plat_data *tps_pdata)
150{
151 int ret = 0;
152 struct device *parent = ri->dev->parent;
153 unsigned int reg_en_reg = ri->desc->enable_reg;
154
155 /*
156 * First enable output for internal control if require.
157 * And then disable external control.
158 */
159 if (tps_pdata->reg_init_data->constraints.always_on ||
160 tps_pdata->reg_init_data->constraints.boot_on) {
161 ret = tps65090_set_bits(parent, reg_en_reg, 0);
162 if (ret < 0) {
163 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
164 return ret;
165 }
166 }
167 return tps65090_config_ext_control(ri, false);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530168}
169
Bill Pembertona5023572012-11-19 13:22:22 -0500170static void tps65090_configure_regulator_config(
Laxman Dewanganf329b172012-10-09 15:19:02 +0530171 struct tps65090_regulator_plat_data *tps_pdata,
172 struct regulator_config *config)
173{
174 if (gpio_is_valid(tps_pdata->gpio)) {
175 int gpio_flag = GPIOF_OUT_INIT_LOW;
176
177 if (tps_pdata->reg_init_data->constraints.always_on ||
178 tps_pdata->reg_init_data->constraints.boot_on)
179 gpio_flag = GPIOF_OUT_INIT_HIGH;
180
181 config->ena_gpio = tps_pdata->gpio;
182 config->ena_gpio_flags = gpio_flag;
183 }
184}
185
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530186#ifdef CONFIG_OF
187static struct of_regulator_match tps65090_matches[] = {
188 { .name = "dcdc1", },
189 { .name = "dcdc2", },
190 { .name = "dcdc3", },
191 { .name = "fet1", },
192 { .name = "fet2", },
193 { .name = "fet3", },
194 { .name = "fet4", },
195 { .name = "fet5", },
196 { .name = "fet6", },
197 { .name = "fet7", },
198 { .name = "ldo1", },
199 { .name = "ldo2", },
200};
201
202static struct tps65090_platform_data *tps65090_parse_dt_reg_data(
203 struct platform_device *pdev,
204 struct of_regulator_match **tps65090_reg_matches)
205{
206 struct tps65090_platform_data *tps65090_pdata;
207 struct device_node *np = pdev->dev.parent->of_node;
208 struct device_node *regulators;
209 int idx = 0, ret;
210 struct tps65090_regulator_plat_data *reg_pdata;
211
212 tps65090_pdata = devm_kzalloc(&pdev->dev, sizeof(*tps65090_pdata),
213 GFP_KERNEL);
Sachin Kamat0ad91c62014-02-20 14:23:15 +0530214 if (!tps65090_pdata)
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530215 return ERR_PTR(-ENOMEM);
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530216
217 reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
218 sizeof(*reg_pdata), GFP_KERNEL);
Sachin Kamat0ad91c62014-02-20 14:23:15 +0530219 if (!reg_pdata)
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530220 return ERR_PTR(-ENOMEM);
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530221
Laxman Dewangan4c850ea2013-10-08 19:31:02 +0530222 regulators = of_get_child_by_name(np, "regulators");
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530223 if (!regulators) {
224 dev_err(&pdev->dev, "regulator node not found\n");
225 return ERR_PTR(-ENODEV);
226 }
227
Axel Lin09a228e2013-01-30 20:28:20 +0800228 ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530229 ARRAY_SIZE(tps65090_matches));
Sachin Kamat026cdfe2014-02-17 14:33:37 +0530230 of_node_put(regulators);
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530231 if (ret < 0) {
232 dev_err(&pdev->dev,
233 "Error parsing regulator init data: %d\n", ret);
234 return ERR_PTR(ret);
235 }
236
237 *tps65090_reg_matches = tps65090_matches;
238 for (idx = 0; idx < ARRAY_SIZE(tps65090_matches); idx++) {
239 struct regulator_init_data *ri_data;
240 struct tps65090_regulator_plat_data *rpdata;
241
242 rpdata = &reg_pdata[idx];
243 ri_data = tps65090_matches[idx].init_data;
244 if (!ri_data || !tps65090_matches[idx].of_node)
245 continue;
246
247 rpdata->reg_init_data = ri_data;
248 rpdata->enable_ext_control = of_property_read_bool(
249 tps65090_matches[idx].of_node,
250 "ti,enable-ext-control");
251 if (rpdata->enable_ext_control)
252 rpdata->gpio = of_get_named_gpio(np,
253 "dcdc-ext-control-gpios", 0);
254
Doug Anderson29041442014-04-16 16:12:28 -0700255 if (of_property_read_u32(tps65090_matches[idx].of_node,
256 "ti,overcurrent-wait",
257 &rpdata->overcurrent_wait) == 0)
258 rpdata->overcurrent_wait_valid = true;
259
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530260 tps65090_pdata->reg_pdata[idx] = rpdata;
261 }
262 return tps65090_pdata;
263}
264#else
265static inline struct tps65090_platform_data *tps65090_parse_dt_reg_data(
266 struct platform_device *pdev,
267 struct of_regulator_match **tps65090_reg_matches)
268{
269 *tps65090_reg_matches = NULL;
270 return NULL;
271}
272#endif
273
Bill Pembertona5023572012-11-19 13:22:22 -0500274static int tps65090_regulator_probe(struct platform_device *pdev)
Venu Byravarasu452534e2012-03-22 18:34:09 +0530275{
Axel Lin06c49982012-04-17 17:08:56 +0800276 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530277 struct tps65090_regulator *ri = NULL;
Mark Brownc172708d2012-04-04 00:50:22 +0100278 struct regulator_config config = { };
Venu Byravarasu452534e2012-03-22 18:34:09 +0530279 struct regulator_dev *rdev;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530280 struct tps65090_regulator_plat_data *tps_pdata;
281 struct tps65090_regulator *pmic;
282 struct tps65090_platform_data *tps65090_pdata;
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530283 struct of_regulator_match *tps65090_reg_matches = NULL;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530284 int num;
285 int ret;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530286
Laxman Dewangan24282a12012-10-09 15:18:59 +0530287 dev_dbg(&pdev->dev, "Probing regulator\n");
Venu Byravarasu452534e2012-03-22 18:34:09 +0530288
Laxman Dewangan24282a12012-10-09 15:18:59 +0530289 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530290 if (!tps65090_pdata && tps65090_mfd->dev->of_node)
291 tps65090_pdata = tps65090_parse_dt_reg_data(pdev,
292 &tps65090_reg_matches);
293 if (IS_ERR_OR_NULL(tps65090_pdata)) {
Laxman Dewangan24282a12012-10-09 15:18:59 +0530294 dev_err(&pdev->dev, "Platform data missing\n");
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530295 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530296 }
Venu Byravarasu452534e2012-03-22 18:34:09 +0530297
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530298 pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
Laxman Dewangan24282a12012-10-09 15:18:59 +0530299 GFP_KERNEL);
Sachin Kamat0ad91c62014-02-20 14:23:15 +0530300 if (!pmic)
Laxman Dewangan24282a12012-10-09 15:18:59 +0530301 return -ENOMEM;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530302
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530303 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
Laxman Dewangan24282a12012-10-09 15:18:59 +0530304 tps_pdata = tps65090_pdata->reg_pdata[num];
305
306 ri = &pmic[num];
307 ri->dev = &pdev->dev;
308 ri->desc = &tps65090_regulator_desc[num];
Doug Anderson29041442014-04-16 16:12:28 -0700309 ri->overcurrent_wait_valid = tps_pdata->overcurrent_wait_valid;
310 ri->overcurrent_wait = tps_pdata->overcurrent_wait;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530311
312 /*
313 * TPS5090 DCDC support the control from external digital input.
Laxman Dewanganf329b172012-10-09 15:19:02 +0530314 * Configure it as per platform data.
Laxman Dewangan24282a12012-10-09 15:18:59 +0530315 */
316 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
Laxman Dewanganf329b172012-10-09 15:19:02 +0530317 if (tps_pdata->enable_ext_control) {
318 tps65090_configure_regulator_config(
319 tps_pdata, &config);
320 ri->desc->ops = &tps65090_ext_control_ops;
321 } else {
322 ret = tps65090_regulator_disable_ext_control(
Laxman Dewangan24282a12012-10-09 15:18:59 +0530323 ri, tps_pdata);
Laxman Dewanganf329b172012-10-09 15:19:02 +0530324 if (ret < 0) {
325 dev_err(&pdev->dev,
Laxman Dewangan24282a12012-10-09 15:18:59 +0530326 "failed disable ext control\n");
Sachin Kamat9738efa2013-09-04 17:17:48 +0530327 return ret;
Laxman Dewanganf329b172012-10-09 15:19:02 +0530328 }
Laxman Dewangan24282a12012-10-09 15:18:59 +0530329 }
330 }
Laxman Dewanganf329b172012-10-09 15:19:02 +0530331
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530332 config.dev = pdev->dev.parent;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530333 config.driver_data = ri;
334 config.regmap = tps65090_mfd->rmap;
335 if (tps_pdata)
336 config.init_data = tps_pdata->reg_init_data;
337 else
338 config.init_data = NULL;
Laxman Dewangan6c7a7a02013-01-29 14:35:16 +0530339 if (tps65090_reg_matches)
340 config.of_node = tps65090_reg_matches[num].of_node;
341 else
342 config.of_node = NULL;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530343
Sachin Kamat9738efa2013-09-04 17:17:48 +0530344 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
Laxman Dewangan24282a12012-10-09 15:18:59 +0530345 if (IS_ERR(rdev)) {
346 dev_err(&pdev->dev, "failed to register regulator %s\n",
347 ri->desc->name);
Sachin Kamat9738efa2013-09-04 17:17:48 +0530348 return PTR_ERR(rdev);
Laxman Dewangan24282a12012-10-09 15:18:59 +0530349 }
350 ri->rdev = rdev;
Laxman Dewanganf329b172012-10-09 15:19:02 +0530351
Doug Anderson29041442014-04-16 16:12:28 -0700352 if (ri->overcurrent_wait_valid) {
353 ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
354 if (ret < 0)
355 return ret;
356 }
357
Laxman Dewanganf329b172012-10-09 15:19:02 +0530358 /* Enable external control if it is require */
359 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
360 tps_pdata->enable_ext_control) {
361 ret = tps65090_config_ext_control(ri, true);
Sachin Kamat9738efa2013-09-04 17:17:48 +0530362 if (ret < 0)
363 return ret;
Laxman Dewanganf329b172012-10-09 15:19:02 +0530364 }
Laxman Dewangan24282a12012-10-09 15:18:59 +0530365 }
366
367 platform_set_drvdata(pdev, pmic);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530368 return 0;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530369}
370
371static struct platform_driver tps65090_regulator_driver = {
372 .driver = {
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530373 .name = "tps65090-pmic",
Venu Byravarasu452534e2012-03-22 18:34:09 +0530374 .owner = THIS_MODULE,
375 },
376 .probe = tps65090_regulator_probe,
Venu Byravarasu452534e2012-03-22 18:34:09 +0530377};
378
379static int __init tps65090_regulator_init(void)
380{
381 return platform_driver_register(&tps65090_regulator_driver);
382}
383subsys_initcall(tps65090_regulator_init);
384
385static void __exit tps65090_regulator_exit(void)
386{
387 platform_driver_unregister(&tps65090_regulator_driver);
388}
389module_exit(tps65090_regulator_exit);
390
391MODULE_DESCRIPTION("tps65090 regulator driver");
392MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
393MODULE_LICENSE("GPL v2");
Axel Lind95420b2012-11-23 23:47:16 +0800394MODULE_ALIAS("platform:tps65090-pmic");