blob: 0732d9ba3f405d19df28142982f6c18b52994700 [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>
21#include <linux/slab.h>
22#include <linux/err.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/machine.h>
26#include <linux/mfd/tps65090.h>
Venu Byravarasu452534e2012-03-22 18:34:09 +053027
28struct tps65090_regulator {
Venu Byravarasu452534e2012-03-22 18:34:09 +053029 struct device *dev;
Laxman Dewangan24282a12012-10-09 15:18:59 +053030 struct regulator_desc *desc;
31 struct regulator_dev *rdev;
Venu Byravarasu452534e2012-03-22 18:34:09 +053032};
33
Venu Byravarasu452534e2012-03-22 18:34:09 +053034static struct regulator_ops tps65090_ops = {
Axel Lin06c49982012-04-17 17:08:56 +080035 .enable = regulator_enable_regmap,
36 .disable = regulator_disable_regmap,
37 .is_enabled = regulator_is_enabled_regmap,
Venu Byravarasu452534e2012-03-22 18:34:09 +053038};
39
Laxman Dewangan3a81ef82012-10-09 15:19:01 +053040static struct regulator_ops tps65090_ldo_ops = {
41};
42
Laxman Dewangan24282a12012-10-09 15:18:59 +053043#define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \
Venu Byravarasu452534e2012-03-22 18:34:09 +053044{ \
Laxman Dewangan24282a12012-10-09 15:18:59 +053045 .name = "TPS65090_RAILS"#_id, \
46 .supply_name = _sname, \
Laxman Dewangan8620ca92012-10-09 15:19:00 +053047 .id = TPS65090_REGULATOR_##_id, \
Laxman Dewangan24282a12012-10-09 15:18:59 +053048 .ops = &_ops, \
49 .enable_reg = _en_reg, \
50 .enable_mask = BIT(0), \
51 .type = REGULATOR_VOLTAGE, \
52 .owner = THIS_MODULE, \
Venu Byravarasu452534e2012-03-22 18:34:09 +053053}
54
Laxman Dewangan24282a12012-10-09 15:18:59 +053055static struct regulator_desc tps65090_regulator_desc[] = {
56 tps65090_REG_DESC(DCDC1, "vsys1", 0x0C, tps65090_ops),
57 tps65090_REG_DESC(DCDC2, "vsys2", 0x0D, tps65090_ops),
58 tps65090_REG_DESC(DCDC3, "vsys3", 0x0E, tps65090_ops),
59 tps65090_REG_DESC(FET1, "infet1", 0x0F, tps65090_ops),
60 tps65090_REG_DESC(FET2, "infet2", 0x10, tps65090_ops),
61 tps65090_REG_DESC(FET3, "infet3", 0x11, tps65090_ops),
62 tps65090_REG_DESC(FET4, "infet4", 0x12, tps65090_ops),
63 tps65090_REG_DESC(FET5, "infet5", 0x13, tps65090_ops),
64 tps65090_REG_DESC(FET6, "infet6", 0x14, tps65090_ops),
65 tps65090_REG_DESC(FET7, "infet7", 0x15, tps65090_ops),
Laxman Dewangan3a81ef82012-10-09 15:19:01 +053066 tps65090_REG_DESC(LDO1, "vsys_l1", 0, tps65090_ldo_ops),
67 tps65090_REG_DESC(LDO2, "vsys_l2", 0, tps65090_ldo_ops),
Venu Byravarasu452534e2012-03-22 18:34:09 +053068};
69
Laxman Dewangan24282a12012-10-09 15:18:59 +053070static inline bool is_dcdc(int id)
Venu Byravarasu452534e2012-03-22 18:34:09 +053071{
Laxman Dewangan24282a12012-10-09 15:18:59 +053072 switch (id) {
Laxman Dewangan8620ca92012-10-09 15:19:00 +053073 case TPS65090_REGULATOR_DCDC1:
74 case TPS65090_REGULATOR_DCDC2:
75 case TPS65090_REGULATOR_DCDC3:
Laxman Dewangan24282a12012-10-09 15:18:59 +053076 return true;
77 default:
78 return false;
Venu Byravarasu452534e2012-03-22 18:34:09 +053079 }
Laxman Dewangan24282a12012-10-09 15:18:59 +053080}
81
82static int __devinit tps65090_config_ext_control(
83 struct tps65090_regulator *ri, bool enable)
84{
85 int ret;
86 struct device *parent = ri->dev->parent;
87 unsigned int reg_en_reg = ri->desc->enable_reg;
88
89 if (enable)
90 ret = tps65090_set_bits(parent, reg_en_reg, 1);
91 else
92 ret = tps65090_clr_bits(parent, reg_en_reg, 1);
93 if (ret < 0)
94 dev_err(ri->dev, "Error in updating reg 0x%x\n", reg_en_reg);
95 return ret;
96}
97
98static int __devinit tps65090_regulator_disable_ext_control(
99 struct tps65090_regulator *ri,
100 struct tps65090_regulator_plat_data *tps_pdata)
101{
102 int ret = 0;
103 struct device *parent = ri->dev->parent;
104 unsigned int reg_en_reg = ri->desc->enable_reg;
105
106 /*
107 * First enable output for internal control if require.
108 * And then disable external control.
109 */
110 if (tps_pdata->reg_init_data->constraints.always_on ||
111 tps_pdata->reg_init_data->constraints.boot_on) {
112 ret = tps65090_set_bits(parent, reg_en_reg, 0);
113 if (ret < 0) {
114 dev_err(ri->dev, "Error in set reg 0x%x\n", reg_en_reg);
115 return ret;
116 }
117 }
118 return tps65090_config_ext_control(ri, false);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530119}
120
121static int __devinit tps65090_regulator_probe(struct platform_device *pdev)
122{
Axel Lin06c49982012-04-17 17:08:56 +0800123 struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530124 struct tps65090_regulator *ri = NULL;
Mark Brownc172708d2012-04-04 00:50:22 +0100125 struct regulator_config config = { };
Venu Byravarasu452534e2012-03-22 18:34:09 +0530126 struct regulator_dev *rdev;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530127 struct tps65090_regulator_plat_data *tps_pdata;
128 struct tps65090_regulator *pmic;
129 struct tps65090_platform_data *tps65090_pdata;
130 int num;
131 int ret;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530132
Laxman Dewangan24282a12012-10-09 15:18:59 +0530133 dev_dbg(&pdev->dev, "Probing regulator\n");
Venu Byravarasu452534e2012-03-22 18:34:09 +0530134
Laxman Dewangan24282a12012-10-09 15:18:59 +0530135 tps65090_pdata = dev_get_platdata(pdev->dev.parent);
136 if (!tps65090_pdata) {
137 dev_err(&pdev->dev, "Platform data missing\n");
Venu Byravarasu452534e2012-03-22 18:34:09 +0530138 return -EINVAL;
139 }
Venu Byravarasu452534e2012-03-22 18:34:09 +0530140
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530141 pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic),
Laxman Dewangan24282a12012-10-09 15:18:59 +0530142 GFP_KERNEL);
143 if (!pmic) {
144 dev_err(&pdev->dev, "mem alloc for pmic failed\n");
145 return -ENOMEM;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530146 }
147
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530148 for (num = 0; num < TPS65090_REGULATOR_MAX; num++) {
Laxman Dewangan24282a12012-10-09 15:18:59 +0530149 tps_pdata = tps65090_pdata->reg_pdata[num];
150
151 ri = &pmic[num];
152 ri->dev = &pdev->dev;
153 ri->desc = &tps65090_regulator_desc[num];
154
155 /*
156 * TPS5090 DCDC support the control from external digital input.
157 * It may be possible that during boot, the external control is
158 * enabled. Disabling external control for DCDC.
159 */
160 if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) {
161 ret = tps65090_regulator_disable_ext_control(
162 ri, tps_pdata);
163 if (ret < 0) {
164 dev_err(&pdev->dev,
165 "failed disable ext control\n");
166 goto scrub;
167 }
168 }
169 config.dev = &pdev->dev;
170 config.driver_data = ri;
171 config.regmap = tps65090_mfd->rmap;
172 if (tps_pdata)
173 config.init_data = tps_pdata->reg_init_data;
174 else
175 config.init_data = NULL;
176
177 rdev = regulator_register(ri->desc, &config);
178 if (IS_ERR(rdev)) {
179 dev_err(&pdev->dev, "failed to register regulator %s\n",
180 ri->desc->name);
181 ret = PTR_ERR(rdev);
182 goto scrub;
183 }
184 ri->rdev = rdev;
185 }
186
187 platform_set_drvdata(pdev, pmic);
Venu Byravarasu452534e2012-03-22 18:34:09 +0530188 return 0;
Laxman Dewangan24282a12012-10-09 15:18:59 +0530189
190scrub:
191 while (--num >= 0) {
192 ri = &pmic[num];
193 regulator_unregister(ri->rdev);
194 }
195 return ret;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530196}
197
198static int __devexit tps65090_regulator_remove(struct platform_device *pdev)
199{
Laxman Dewangan24282a12012-10-09 15:18:59 +0530200 struct tps65090_regulator *pmic = platform_get_drvdata(pdev);
201 struct tps65090_regulator *ri;
202 int num;
Venu Byravarasu452534e2012-03-22 18:34:09 +0530203
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530204 for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) {
Laxman Dewangan24282a12012-10-09 15:18:59 +0530205 ri = &pmic[num];
206 regulator_unregister(ri->rdev);
207 }
Venu Byravarasu452534e2012-03-22 18:34:09 +0530208 return 0;
209}
210
211static struct platform_driver tps65090_regulator_driver = {
212 .driver = {
Laxman Dewangan8620ca92012-10-09 15:19:00 +0530213 .name = "tps65090-pmic",
Venu Byravarasu452534e2012-03-22 18:34:09 +0530214 .owner = THIS_MODULE,
215 },
216 .probe = tps65090_regulator_probe,
217 .remove = __devexit_p(tps65090_regulator_remove),
218};
219
220static int __init tps65090_regulator_init(void)
221{
222 return platform_driver_register(&tps65090_regulator_driver);
223}
224subsys_initcall(tps65090_regulator_init);
225
226static void __exit tps65090_regulator_exit(void)
227{
228 platform_driver_unregister(&tps65090_regulator_driver);
229}
230module_exit(tps65090_regulator_exit);
231
232MODULE_DESCRIPTION("tps65090 regulator driver");
233MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
234MODULE_LICENSE("GPL v2");