regulator: tps65090: Allow setting the overcurrent wait time
[cascardo/linux.git] / drivers / regulator / tps65090-regulator.c
1 /*
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>
20 #include <linux/init.h>
21 #include <linux/gpio.h>
22 #include <linux/of_gpio.h>
23 #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>
28 #include <linux/regulator/of_regulator.h>
29 #include <linux/mfd/tps65090.h>
30
31 #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
45 struct tps65090_regulator {
46         struct device           *dev;
47         struct regulator_desc   *desc;
48         struct regulator_dev    *rdev;
49         bool                    overcurrent_wait_valid;
50         int                     overcurrent_wait;
51 };
52
53 static struct regulator_ops tps65090_ext_control_ops = {
54 };
55
56 /**
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  */
67 static 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
83 static struct regulator_ops tps65090_reg_contol_ops = {
84         .enable         = regulator_enable_regmap,
85         .disable        = regulator_disable_regmap,
86         .is_enabled     = regulator_is_enabled_regmap,
87 };
88
89 static struct regulator_ops tps65090_ldo_ops = {
90 };
91
92 #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops)   \
93 {                                                       \
94         .name = "TPS65090_RAILS"#_id,                   \
95         .supply_name = _sname,                          \
96         .id = TPS65090_REGULATOR_##_id,                 \
97         .ops = &_ops,                                   \
98         .enable_reg = _en_reg,                          \
99         .enable_mask = BIT(0),                          \
100         .type = REGULATOR_VOLTAGE,                      \
101         .owner = THIS_MODULE,                           \
102 }
103
104 static struct regulator_desc tps65090_regulator_desc[] = {
105         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),
115         tps65090_REG_DESC(LDO1,  "vsys-l1", 0,    tps65090_ldo_ops),
116         tps65090_REG_DESC(LDO2,  "vsys-l2", 0,    tps65090_ldo_ops),
117 };
118
119 static inline bool is_dcdc(int id)
120 {
121         switch (id) {
122         case TPS65090_REGULATOR_DCDC1:
123         case TPS65090_REGULATOR_DCDC2:
124         case TPS65090_REGULATOR_DCDC3:
125                 return true;
126         default:
127                 return false;
128         }
129 }
130
131 static int tps65090_config_ext_control(
132         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
147 static int tps65090_regulator_disable_ext_control(
148                 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);
168 }
169
170 static void tps65090_configure_regulator_config(
171                 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
186 #ifdef CONFIG_OF
187 static 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
202 static 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);
214         if (!tps65090_pdata)
215                 return ERR_PTR(-ENOMEM);
216
217         reg_pdata = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX *
218                                 sizeof(*reg_pdata), GFP_KERNEL);
219         if (!reg_pdata)
220                 return ERR_PTR(-ENOMEM);
221
222         regulators = of_get_child_by_name(np, "regulators");
223         if (!regulators) {
224                 dev_err(&pdev->dev, "regulator node not found\n");
225                 return ERR_PTR(-ENODEV);
226         }
227
228         ret = of_regulator_match(&pdev->dev, regulators, tps65090_matches,
229                         ARRAY_SIZE(tps65090_matches));
230         of_node_put(regulators);
231         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
255                 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
260                 tps65090_pdata->reg_pdata[idx] = rpdata;
261         }
262         return tps65090_pdata;
263 }
264 #else
265 static 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
274 static int tps65090_regulator_probe(struct platform_device *pdev)
275 {
276         struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
277         struct tps65090_regulator *ri = NULL;
278         struct regulator_config config = { };
279         struct regulator_dev *rdev;
280         struct tps65090_regulator_plat_data *tps_pdata;
281         struct tps65090_regulator *pmic;
282         struct tps65090_platform_data *tps65090_pdata;
283         struct of_regulator_match *tps65090_reg_matches = NULL;
284         int num;
285         int ret;
286
287         dev_dbg(&pdev->dev, "Probing regulator\n");
288
289         tps65090_pdata = dev_get_platdata(pdev->dev.parent);
290         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)) {
294                 dev_err(&pdev->dev, "Platform data missing\n");
295                 return tps65090_pdata ? PTR_ERR(tps65090_pdata) : -EINVAL;
296         }
297
298         pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
299                         GFP_KERNEL);
300         if (!pmic)
301                 return -ENOMEM;
302
303         for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
304                 tps_pdata = tps65090_pdata->reg_pdata[num];
305
306                 ri = &pmic[num];
307                 ri->dev = &pdev->dev;
308                 ri->desc = &tps65090_regulator_desc[num];
309                 ri->overcurrent_wait_valid = tps_pdata->overcurrent_wait_valid;
310                 ri->overcurrent_wait = tps_pdata->overcurrent_wait;
311
312                 /*
313                  * TPS5090 DCDC support the control from external digital input.
314                  * Configure it as per platform data.
315                  */
316                 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
317                         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(
323                                                 ri, tps_pdata);
324                                 if (ret < 0) {
325                                         dev_err(&pdev->dev,
326                                                 "failed disable ext control\n");
327                                         return ret;
328                                 }
329                         }
330                 }
331
332                 config.dev = pdev->dev.parent;
333                 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;
339                 if (tps65090_reg_matches)
340                         config.of_node = tps65090_reg_matches[num].of_node;
341                 else
342                         config.of_node = NULL;
343
344                 rdev = devm_regulator_register(&pdev->dev, ri->desc, &config);
345                 if (IS_ERR(rdev)) {
346                         dev_err(&pdev->dev, "failed to register regulator %s\n",
347                                 ri->desc->name);
348                         return PTR_ERR(rdev);
349                 }
350                 ri->rdev = rdev;
351
352                 if (ri->overcurrent_wait_valid) {
353                         ret = tps65090_reg_set_overcurrent_wait(ri, rdev);
354                         if (ret < 0)
355                                 return ret;
356                 }
357
358                 /* 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);
362                         if (ret < 0)
363                                 return ret;
364                 }
365         }
366
367         platform_set_drvdata(pdev, pmic);
368         return 0;
369 }
370
371 static struct platform_driver tps65090_regulator_driver = {
372         .driver = {
373                 .name   = "tps65090-pmic",
374                 .owner  = THIS_MODULE,
375         },
376         .probe          = tps65090_regulator_probe,
377 };
378
379 static int __init tps65090_regulator_init(void)
380 {
381         return platform_driver_register(&tps65090_regulator_driver);
382 }
383 subsys_initcall(tps65090_regulator_init);
384
385 static void __exit tps65090_regulator_exit(void)
386 {
387         platform_driver_unregister(&tps65090_regulator_driver);
388 }
389 module_exit(tps65090_regulator_exit);
390
391 MODULE_DESCRIPTION("tps65090 regulator driver");
392 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
393 MODULE_LICENSE("GPL v2");
394 MODULE_ALIAS("platform:tps65090-pmic");