blob: 41c391789c9790a59678dc1232bf0938e978a47e [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>
Venu Byravarasu452534e2012-03-22 18:34:09 +053022#include <linux/slab.h>
23#include <linux/err.h>
24#include <linux/platform_device.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27#include <linux/mfd/tps65090.h>
Venu Byravarasu452534e2012-03-22 18:34:09 +053028
29struct tps65090_regulator {
Venu Byravarasu452534e2012-03-22 18:34:09 +053030 struct device *dev;
Laxman Dewangan24282a12012-10-09 15:18:59 +053031 struct regulator_desc *desc;
32 struct regulator_dev *rdev;
Venu Byravarasu452534e2012-03-22 18:34:09 +053033};
34
Laxman Dewanganf329b172012-10-09 15:19:02 +053035static struct regulator_ops tps65090_ext_control_ops = {
36};
37
38static struct regulator_ops tps65090_reg_contol_ops = {
39 .enable = regulator_enable_regmap,
40 .disable = regulator_disable_regmap,
41 .is_enabled = regulator_is_enabled_regmap,
Venu Byravarasu452534e2012-03-22 18:34:09 +053042};
43
Laxman Dewangan3a81ef82012-10-09 15:19:01 +053044static struct regulator_ops tps65090_ldo_ops = {
45};
46
Laxman Dewangan24282a12012-10-09 15:18:59 +053047#define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
Venu Byravarasu452534e2012-03-22 18:34:09 +053048{ \
Laxman Dewangan24282a12012-10-09 15:18:59 +053049 .name = "TPS65090_RAILS"#_id, \
50 .supply_name = _sname, \
Laxman Dewangan8620ca92012-10-09 15:19:00 +053051 .id = TPS65090_REGULATOR_##_id, \
Laxman Dewangan24282a12012-10-09 15:18:59 +053052 .ops = &_ops, \
53 .enable_reg = _en_reg, \
54 .enable_mask = BIT(0), \
55 .type = REGULATOR_VOLTAGE, \
56 .owner = THIS_MODULE, \
Venu Byravarasu452534e2012-03-22 18:34:09 +053057}
58
Laxman Dewangan24282a12012-10-09 15:18:59 +053059static struct regulator_desc tps65090_regulator_desc[] = {
Laxman Dewanganf329b172012-10-09 15:19:02 +053060 tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_reg_contol_ops),
61 tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_reg_contol_ops),
62 tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_reg_contol_ops),
63 tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_reg_contol_ops),
64 tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_reg_contol_ops),
65 tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_reg_contol_ops),
66 tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_reg_contol_ops),
67 tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_reg_contol_ops),
68 tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_reg_contol_ops),
69 tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_reg_contol_ops),
Laxman Dewangan3a81ef82012-10-09 15:19:01 +053070 tps65090_REG_DESC(LDO1, "vsys_l1", 0, tps65090_ldo_ops),
71 tps65090_REG_DESC(LDO2, "vsys_l2", 0, tps65090_ldo_ops),
Venu Byravarasu452534e2012-03-22 18:34:09 +053072};
73
Laxman Dewangan24282a12012-10-09 15:18:59 +053074static inline bool is_dcdc(int id)
Venu Byravarasu452534e2012-03-22 18:34:09 +053075{
Laxman Dewangan24282a12012-10-09 15:18:59 +053076 switch (id) {
Laxman Dewangan8620ca92012-10-09 15:19:00 +053077 case TPS65090_REGULATOR_DCDC1:
78 case TPS65090_REGULATOR_DCDC2:
79 case TPS65090_REGULATOR_DCDC3:
Laxman Dewangan24282a12012-10-09 15:18:59 +053080 return true;
81 default:
82 return false;
Venu Byravarasu452534e2012-03-22 18:34:09 +053083 }
Laxman Dewangan24282a12012-10-09 15:18:59 +053084}
85
Bill Pembertona5023572012-11-19 13:22:22 -050086static int tps65090_config_ext_control(
Laxman Dewangan24282a12012-10-09 15:18:59 +053087 struct tps65090_regulator *ri, bool enable)
88{
89 int ret;
90 struct device *parent = ri->dev->parent;
91 unsigned int reg_en_reg = ri->desc->enable_reg;
92
93 if (enable)
94 ret = tps65090_set_bits(parent, reg_en_reg, 1);
95 else
96 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
97 if (ret < 0)
98 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
99 return ret;
100}
101
Bill Pembertona5023572012-11-19 13:22:22 -0500102static int tps65090_regulator_disable_ext_control(
Laxman Dewangan24282a12012-10-09 15:18:59 +0530103 struct tps65090_regulator *ri,
104 struct tps65090_regulator_plat_data *tps_pdata)
105{
106 int ret = 0;
107 struct device *parent = ri->dev->parent;
108 unsigned int reg_en_reg = ri->desc->enable_reg;
109
110 /*
111 * First enable output for internal control if require.
112 * And then disable external control.
113 */
114 if (tps_pdata->reg_init_data->constraints.always_on ||
115 tps_pdata->reg_init_data->constraints.boot_on) {
116 ret = tps65090_set_bits(parent, reg_en_reg, 0);
117 if (ret < 0) {
118 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
119 return ret;
120 }
121 }
122 return tps65090_config_ext_control(ri, false);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530123}
124
Bill Pembertona5023572012-11-19 13:22:22 -0500125static void tps65090_configure_regulator_config(
Laxman Dewanganf329b172012-10-09 15:19:02 +0530126 struct tps65090_regulator_plat_data *tps_pdata,
127 struct regulator_config *config)
128{
129 if (gpio_is_valid(tps_pdata->gpio)) {
130 int gpio_flag = GPIOF_OUT_INIT_LOW;
131
132 if (tps_pdata->reg_init_data->constraints.always_on ||
133 tps_pdata->reg_init_data->constraints.boot_on)
134 gpio_flag = GPIOF_OUT_INIT_HIGH;
135
136 config->ena_gpio = tps_pdata->gpio;
137 config->ena_gpio_flags = gpio_flag;
138 }
139}
140
Bill Pembertona5023572012-11-19 13:22:22 -0500141static int tps65090_regulator_probe(struct platform_device *pdev)
Venu Byravarasu452534e2012-03-22 18:34:09 +0530142{
Axel Lin06c49982012-04-17 17:08:56 +0800143 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530144 struct tps65090_regulator *ri = NULL;
Mark Brownc1727082012-04-04 00:50:22 +0100145 struct regulator_config config = { };
Venu Byravarasu452534e2012-03-22 18:34:09 +0530146 struct regulator_dev *rdev;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530147 struct tps65090_regulator_plat_data *tps_pdata;
148 struct tps65090_regulator *pmic;
149 struct tps65090_platform_data *tps65090_pdata;
150 int num;
151 int ret;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530152
Laxman Dewangan24282a12012-10-09 15:18:59 +0530153 dev_dbg(&pdev->dev, "Probing regulator\n");
Venu Byravarasu452534e2012-03-22 18:34:09 +0530154
Laxman Dewangan24282a12012-10-09 15:18:59 +0530155 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
156 if (!tps65090_pdata) {
157 dev_err(&pdev->dev, "Platform data missing\n");
Venu Byravarasu452534e2012-03-22 18:34:09 +0530158 return -EINVAL;
159 }
Venu Byravarasu452534e2012-03-22 18:34:09 +0530160
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530161 pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
Laxman Dewangan24282a12012-10-09 15:18:59 +0530162 GFP_KERNEL);
163 if (!pmic) {
164 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
165 return -ENOMEM;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530166 }
167
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530168 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
Laxman Dewangan24282a12012-10-09 15:18:59 +0530169 tps_pdata = tps65090_pdata->reg_pdata[num];
170
171 ri = &pmic[num];
172 ri->dev = &pdev->dev;
173 ri->desc = &tps65090_regulator_desc[num];
174
175 /*
176 * TPS5090 DCDC support the control from external digital input.
Laxman Dewanganf329b172012-10-09 15:19:02 +0530177 * Configure it as per platform data.
Laxman Dewangan24282a12012-10-09 15:18:59 +0530178 */
179 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
Laxman Dewanganf329b172012-10-09 15:19:02 +0530180 if (tps_pdata->enable_ext_control) {
181 tps65090_configure_regulator_config(
182 tps_pdata, &config);
183 ri->desc->ops = &tps65090_ext_control_ops;
184 } else {
185 ret = tps65090_regulator_disable_ext_control(
Laxman Dewangan24282a12012-10-09 15:18:59 +0530186 ri, tps_pdata);
Laxman Dewanganf329b172012-10-09 15:19:02 +0530187 if (ret < 0) {
188 dev_err(&pdev->dev,
Laxman Dewangan24282a12012-10-09 15:18:59 +0530189 "failed disable ext control\n");
Laxman Dewanganf329b172012-10-09 15:19:02 +0530190 goto scrub;
191 }
Laxman Dewangan24282a12012-10-09 15:18:59 +0530192 }
193 }
Laxman Dewanganf329b172012-10-09 15:19:02 +0530194
Laxman Dewangan24282a12012-10-09 15:18:59 +0530195 config.dev = &pdev->dev;
196 config.driver_data = ri;
197 config.regmap = tps65090_mfd->rmap;
198 if (tps_pdata)
199 config.init_data = tps_pdata->reg_init_data;
200 else
201 config.init_data = NULL;
202
203 rdev = regulator_register(ri->desc, &config);
204 if (IS_ERR(rdev)) {
205 dev_err(&pdev->dev, "failed to register regulator %s\n",
206 ri->desc->name);
207 ret = PTR_ERR(rdev);
208 goto scrub;
209 }
210 ri->rdev = rdev;
Laxman Dewanganf329b172012-10-09 15:19:02 +0530211
212 /* Enable external control if it is require */
213 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data &&
214 tps_pdata->enable_ext_control) {
215 ret = tps65090_config_ext_control(ri, true);
216 if (ret < 0) {
217 /* Increment num to get unregister rdev */
218 num++;
219 goto scrub;
220 }
221 }
Laxman Dewangan24282a12012-10-09 15:18:59 +0530222 }
223
224 platform_set_drvdata(pdev, pmic);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530225 return 0;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530226
227scrub:
228 while (--num >= 0) {
229 ri = &pmic[num];
230 regulator_unregister(ri->rdev);
231 }
232 return ret;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530233}
234
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500235static int tps65090_regulator_remove(struct platform_device *pdev)
Venu Byravarasu452534e2012-03-22 18:34:09 +0530236{
Laxman Dewangan24282a12012-10-09 15:18:59 +0530237 struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
238 struct tps65090_regulator *ri;
239 int num;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530240
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530241 for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) {
Laxman Dewangan24282a12012-10-09 15:18:59 +0530242 ri = &pmic[num];
243 regulator_unregister(ri->rdev);
244 }
Venu Byravarasu452534e2012-03-22 18:34:09 +0530245 return 0;
246}
247
248static struct platform_driver tps65090_regulator_driver = {
249 .driver = {
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530250 .name = "tps65090-pmic",
Venu Byravarasu452534e2012-03-22 18:34:09 +0530251 .owner = THIS_MODULE,
252 },
253 .probe = tps65090_regulator_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500254 .remove = tps65090_regulator_remove,
Venu Byravarasu452534e2012-03-22 18:34:09 +0530255};
256
257static int __init tps65090_regulator_init(void)
258{
259 return platform_driver_register(&tps65090_regulator_driver);
260}
261subsys_initcall(tps65090_regulator_init);
262
263static void __exit tps65090_regulator_exit(void)
264{
265 platform_driver_unregister(&tps65090_regulator_driver);
266}
267module_exit(tps65090_regulator_exit);
268
269MODULE_DESCRIPTION("tps65090 regulator driver");
270MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
271MODULE_LICENSE("GPL v2");
Axel Lind95420b2012-11-23 23:47:16 +0800272MODULE_ALIAS("platform:tps65090-pmic");