]>
Commit | Line | Data |
---|---|---|
452534e5 VB |
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> | |
452534e5 | 20 | #include <linux/init.h> |
f329b175 | 21 | #include <linux/gpio.h> |
452534e5 VB |
22 | #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> | |
452534e5 VB |
28 | |
29 | struct tps65090_regulator { | |
452534e5 | 30 | struct device *dev; |
24282a1c LD |
31 | struct regulator_desc *desc; |
32 | struct regulator_dev *rdev; | |
452534e5 VB |
33 | }; |
34 | ||
f329b175 LD |
35 | static struct regulator_ops tps65090_ext_control_ops = { |
36 | }; | |
37 | ||
38 | static struct regulator_ops tps65090_reg_contol_ops = { | |
39 | .enable = regulator_enable_regmap, | |
40 | .disable = regulator_disable_regmap, | |
41 | .is_enabled = regulator_is_enabled_regmap, | |
452534e5 VB |
42 | }; |
43 | ||
3a81ef8c LD |
44 | static struct regulator_ops tps65090_ldo_ops = { |
45 | }; | |
46 | ||
24282a1c | 47 | #define tps65090_REG_DESC(_id, _sname, _en_reg, _ops) \ |
452534e5 | 48 | { \ |
24282a1c LD |
49 | .name = "TPS65090_RAILS"#_id, \ |
50 | .supply_name = _sname, \ | |
8620ca9f | 51 | .id = TPS65090_REGULATOR_##_id, \ |
24282a1c LD |
52 | .ops = &_ops, \ |
53 | .enable_reg = _en_reg, \ | |
54 | .enable_mask = BIT(0), \ | |
55 | .type = REGULATOR_VOLTAGE, \ | |
56 | .owner = THIS_MODULE, \ | |
452534e5 VB |
57 | } |
58 | ||
24282a1c | 59 | static struct regulator_desc tps65090_regulator_desc[] = { |
f329b175 LD |
60 | 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), | |
3a81ef8c LD |
70 | tps65090_REG_DESC(LDO1, "vsys_l1", 0, tps65090_ldo_ops), |
71 | tps65090_REG_DESC(LDO2, "vsys_l2", 0, tps65090_ldo_ops), | |
452534e5 VB |
72 | }; |
73 | ||
24282a1c | 74 | static inline bool is_dcdc(int id) |
452534e5 | 75 | { |
24282a1c | 76 | switch (id) { |
8620ca9f LD |
77 | case TPS65090_REGULATOR_DCDC1: |
78 | case TPS65090_REGULATOR_DCDC2: | |
79 | case TPS65090_REGULATOR_DCDC3: | |
24282a1c LD |
80 | return true; |
81 | default: | |
82 | return false; | |
83 | } | |
84 | } | |
452534e5 | 85 | |
a5023574 | 86 | static int tps65090_config_ext_control( |
24282a1c LD |
87 | 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 | ||
a5023574 | 102 | static int tps65090_regulator_disable_ext_control( |
24282a1c LD |
103 | 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 | } | |
452534e5 | 121 | } |
24282a1c | 122 | return tps65090_config_ext_control(ri, false); |
452534e5 VB |
123 | } |
124 | ||
a5023574 | 125 | static void tps65090_configure_regulator_config( |
f329b175 LD |
126 | 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 | ||
a5023574 | 141 | static int tps65090_regulator_probe(struct platform_device *pdev) |
452534e5 | 142 | { |
06c4998b | 143 | struct tps65090 *tps65090_mfd = dev_get_drvdata(pdev->dev.parent); |
452534e5 | 144 | struct tps65090_regulator *ri = NULL; |
c172708d | 145 | struct regulator_config config = { }; |
452534e5 | 146 | struct regulator_dev *rdev; |
24282a1c LD |
147 | 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; | |
452534e5 | 152 | |
24282a1c | 153 | dev_dbg(&pdev->dev, "Probing regulator\n"); |
452534e5 | 154 | |
24282a1c LD |
155 | tps65090_pdata = dev_get_platdata(pdev->dev.parent); |
156 | if (!tps65090_pdata) { | |
157 | dev_err(&pdev->dev, "Platform data missing\n"); | |
452534e5 VB |
158 | return -EINVAL; |
159 | } | |
24282a1c | 160 | |
8620ca9f | 161 | pmic = devm_kzalloc(&pdev->dev, TPS65090_REGULATOR_MAX * sizeof(*pmic), |
24282a1c LD |
162 | GFP_KERNEL); |
163 | if (!pmic) { | |
164 | dev_err(&pdev->dev, "mem alloc for pmic failed\n"); | |
165 | return -ENOMEM; | |
166 | } | |
167 | ||
8620ca9f | 168 | for (num = 0; num < TPS65090_REGULATOR_MAX; num++) { |
24282a1c LD |
169 | 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. | |
f329b175 | 177 | * Configure it as per platform data. |
24282a1c LD |
178 | */ |
179 | if (tps_pdata && is_dcdc(num) && tps_pdata->reg_init_data) { | |
f329b175 LD |
180 | 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( | |
24282a1c | 186 | ri, tps_pdata); |
f329b175 LD |
187 | if (ret < 0) { |
188 | dev_err(&pdev->dev, | |
24282a1c | 189 | "failed disable ext control\n"); |
f329b175 LD |
190 | goto scrub; |
191 | } | |
24282a1c LD |
192 | } |
193 | } | |
f329b175 | 194 | |
24282a1c LD |
195 | 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; | |
f329b175 LD |
211 | |
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 | } | |
452534e5 VB |
222 | } |
223 | ||
24282a1c | 224 | platform_set_drvdata(pdev, pmic); |
452534e5 | 225 | return 0; |
24282a1c LD |
226 | |
227 | scrub: | |
228 | while (--num >= 0) { | |
229 | ri = &pmic[num]; | |
230 | regulator_unregister(ri->rdev); | |
231 | } | |
232 | return ret; | |
452534e5 VB |
233 | } |
234 | ||
235 | static int __devexit tps65090_regulator_remove(struct platform_device *pdev) | |
236 | { | |
24282a1c LD |
237 | struct tps65090_regulator *pmic = platform_get_drvdata(pdev); |
238 | struct tps65090_regulator *ri; | |
239 | int num; | |
452534e5 | 240 | |
8620ca9f | 241 | for (num = 0; num < TPS65090_REGULATOR_MAX; ++num) { |
24282a1c LD |
242 | ri = &pmic[num]; |
243 | regulator_unregister(ri->rdev); | |
244 | } | |
452534e5 VB |
245 | return 0; |
246 | } | |
247 | ||
248 | static struct platform_driver tps65090_regulator_driver = { | |
249 | .driver = { | |
8620ca9f | 250 | .name = "tps65090-pmic", |
452534e5 VB |
251 | .owner = THIS_MODULE, |
252 | }, | |
253 | .probe = tps65090_regulator_probe, | |
5eb9f2b9 | 254 | .remove = tps65090_regulator_remove, |
452534e5 VB |
255 | }; |
256 | ||
257 | static int __init tps65090_regulator_init(void) | |
258 | { | |
259 | return platform_driver_register(&tps65090_regulator_driver); | |
260 | } | |
261 | subsys_initcall(tps65090_regulator_init); | |
262 | ||
263 | static void __exit tps65090_regulator_exit(void) | |
264 | { | |
265 | platform_driver_unregister(&tps65090_regulator_driver); | |
266 | } | |
267 | module_exit(tps65090_regulator_exit); | |
268 | ||
269 | MODULE_DESCRIPTION("tps65090 regulator driver"); | |
270 | MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>"); | |
271 | MODULE_LICENSE("GPL v2"); |