blob: 81a74d1d15a93cabdf7dfa6e3427b87ddb578e55 [file] [log] [blame]
Laxman Dewangan6ffc3272012-04-04 12:44:00 +05301/*
2 * Regulator driver for RICOH RC5T583 power management chip.
3 *
4 * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
5 * Author: Laxman dewangan <ldewangan@nvidia.com>
6 *
7 * based on code
8 * Copyright (C) 2011 RICOH COMPANY,LTD
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <linux/module.h>
26#include <linux/delay.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/err.h>
30#include <linux/platform_device.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
33#include <linux/gpio.h>
34#include <linux/mfd/rc5t583.h>
35
36struct rc5t583_regulator_info {
37 int deepsleep_id;
38
39 /* Regulator register address.*/
Laxman Dewangan6ffc3272012-04-04 12:44:00 +053040 uint8_t reg_disc_reg;
41 uint8_t disc_bit;
42 uint8_t vout_reg;
43 uint8_t vout_mask;
44 uint8_t deepsleep_reg;
45
46 /* Chip constraints on regulator behavior */
47 int min_uV;
48 int max_uV;
49 int step_uV;
Laxman Dewangan6ffc3272012-04-04 12:44:00 +053050
51 /* Regulator specific turn-on delay and voltage settling time*/
52 int enable_uv_per_us;
53 int change_uv_per_us;
54
55 /* Used by regulator core */
56 struct regulator_desc desc;
57};
58
59struct rc5t583_regulator {
60 struct rc5t583_regulator_info *reg_info;
61
62 /* Devices */
63 struct device *dev;
64 struct rc5t583 *mfd;
65 struct regulator_dev *rdev;
66};
67
Laxman Dewangan6ffc3272012-04-04 12:44:00 +053068static int rc5t583_list_voltage(struct regulator_dev *rdev, unsigned selector)
69{
70 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
71 struct rc5t583_regulator_info *ri = reg->reg_info;
72 return ri->min_uV + (ri->step_uV * selector);
73}
74
Axel Linf604c102012-04-05 14:07:36 +080075static int rc5t583_set_voltage(struct regulator_dev *rdev,
76 int min_uV, int max_uV, unsigned *selector)
Laxman Dewangan6ffc3272012-04-04 12:44:00 +053077{
78 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
79 struct rc5t583_regulator_info *ri = reg->reg_info;
Axel Linf604c102012-04-05 14:07:36 +080080 int sel, ret;
81
82 if (min_uV < ri->min_uV)
83 min_uV = ri->min_uV;
84
85 sel = DIV_ROUND_UP(min_uV - ri->min_uV, ri->step_uV);
86
87 if (sel >= rdev->desc->n_voltages) {
88 dev_err(&rdev->dev, "Invalid selector 0x%02x\n", sel);
Laxman Dewangan6ffc3272012-04-04 12:44:00 +053089 return -EINVAL;
90 }
91
Axel Linf604c102012-04-05 14:07:36 +080092 *selector = sel;
93
94 ret = rc5t583_update(reg->mfd->dev, ri->vout_reg, sel, ri->vout_mask);
Laxman Dewangan6ffc3272012-04-04 12:44:00 +053095 if (ret < 0)
96 dev_err(&rdev->dev,
97 "Error in update voltage register 0x%02x\n", ri->vout_reg);
98 return ret;
99}
100
101static int rc5t583_get_voltage_sel(struct regulator_dev *rdev)
102{
103 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
104 struct rc5t583_regulator_info *ri = reg->reg_info;
105 uint8_t vsel;
106 int ret;
107 ret = rc5t583_read(reg->mfd->dev, ri->vout_reg, &vsel);
108 if (ret < 0) {
109 dev_err(&rdev->dev,
110 "Error in reading voltage register 0x%02x\n", ri->vout_reg);
111 return ret;
112 }
113 return vsel & ri->vout_mask;
114}
115
116static int rc5t583_regulator_enable_time(struct regulator_dev *rdev)
117{
118 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
119 int vsel = rc5t583_get_voltage_sel(rdev);
120 int curr_uV = rc5t583_list_voltage(rdev, vsel);
121 return DIV_ROUND_UP(curr_uV, reg->reg_info->enable_uv_per_us);
122}
123
124static int rc5t583_set_voltage_time_sel(struct regulator_dev *rdev,
125 unsigned int old_selector, unsigned int new_selector)
126{
127 struct rc5t583_regulator *reg = rdev_get_drvdata(rdev);
128 int old_uV, new_uV;
129 old_uV = rc5t583_list_voltage(rdev, old_selector);
130
131 if (old_uV < 0)
132 return old_uV;
133
134 new_uV = rc5t583_list_voltage(rdev, new_selector);
135 if (new_uV < 0)
136 return new_uV;
137
138 return DIV_ROUND_UP(abs(old_uV - new_uV),
139 reg->reg_info->change_uv_per_us);
140}
141
142
143static struct regulator_ops rc5t583_ops = {
Axel Lin5bb69362012-04-17 14:11:31 +0800144 .is_enabled = regulator_is_enabled_regmap,
145 .enable = regulator_enable_regmap,
146 .disable = regulator_disable_regmap,
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530147 .enable_time = rc5t583_regulator_enable_time,
148 .get_voltage_sel = rc5t583_get_voltage_sel,
Axel Linf604c102012-04-05 14:07:36 +0800149 .set_voltage = rc5t583_set_voltage,
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530150 .list_voltage = rc5t583_list_voltage,
151 .set_voltage_time_sel = rc5t583_set_voltage_time_sel,
152};
153
Axel Lin95e301b2012-04-06 08:01:36 +0800154#define RC5T583_REG(_id, _en_reg, _en_bit, _disc_reg, _disc_bit, \
155 _vout_mask, _min_mv, _max_mv, _step_uV, _enable_mv) \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530156{ \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530157 .reg_disc_reg = RC5T583_REG_##_disc_reg, \
158 .disc_bit = _disc_bit, \
Axel Lin95e301b2012-04-06 08:01:36 +0800159 .vout_reg = RC5T583_REG_##_id##DAC, \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530160 .vout_mask = _vout_mask, \
Axel Lin95e301b2012-04-06 08:01:36 +0800161 .deepsleep_reg = RC5T583_REG_##_id##DAC_DS, \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530162 .min_uV = _min_mv * 1000, \
163 .max_uV = _max_mv * 1000, \
164 .step_uV = _step_uV, \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530165 .enable_uv_per_us = _enable_mv * 1000, \
166 .change_uv_per_us = 40 * 1000, \
167 .deepsleep_id = RC5T583_DS_##_id, \
168 .desc = { \
169 .name = "rc5t583-regulator-"#_id, \
170 .id = RC5T583_REGULATOR_##_id, \
Axel Line3a73842012-04-05 14:04:48 +0800171 .n_voltages = (_max_mv - _min_mv) * 1000 / _step_uV + 1, \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530172 .ops = &rc5t583_ops, \
173 .type = REGULATOR_VOLTAGE, \
174 .owner = THIS_MODULE, \
Axel Lin5bb69362012-04-17 14:11:31 +0800175 .enable_reg = RC5T583_REG_##_en_reg, \
176 .enable_mask = BIT(_en_bit), \
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530177 }, \
178}
179
180static struct rc5t583_regulator_info rc5t583_reg_info[RC5T583_REGULATOR_MAX] = {
Axel Lin95e301b2012-04-06 08:01:36 +0800181 RC5T583_REG(DC0, DC0CTL, 0, DC0CTL, 1, 0x7F, 700, 1500, 12500, 4),
182 RC5T583_REG(DC1, DC1CTL, 0, DC1CTL, 1, 0x7F, 700, 1500, 12500, 14),
183 RC5T583_REG(DC2, DC2CTL, 0, DC2CTL, 1, 0x7F, 900, 2400, 12500, 14),
184 RC5T583_REG(DC3, DC3CTL, 0, DC3CTL, 1, 0x7F, 900, 2400, 12500, 14),
185 RC5T583_REG(LDO0, LDOEN2, 0, LDODIS2, 0, 0x7F, 900, 3400, 25000, 160),
186 RC5T583_REG(LDO1, LDOEN2, 1, LDODIS2, 1, 0x7F, 900, 3400, 25000, 160),
187 RC5T583_REG(LDO2, LDOEN2, 2, LDODIS2, 2, 0x7F, 900, 3400, 25000, 160),
188 RC5T583_REG(LDO3, LDOEN2, 3, LDODIS2, 3, 0x7F, 900, 3400, 25000, 160),
189 RC5T583_REG(LDO4, LDOEN2, 4, LDODIS2, 4, 0x3F, 750, 1500, 12500, 133),
190 RC5T583_REG(LDO5, LDOEN2, 5, LDODIS2, 5, 0x7F, 900, 3400, 25000, 267),
191 RC5T583_REG(LDO6, LDOEN2, 6, LDODIS2, 6, 0x7F, 900, 3400, 25000, 133),
192 RC5T583_REG(LDO7, LDOEN2, 7, LDODIS2, 7, 0x7F, 900, 3400, 25000, 233),
193 RC5T583_REG(LDO8, LDOEN1, 0, LDODIS1, 0, 0x7F, 900, 3400, 25000, 233),
194 RC5T583_REG(LDO9, LDOEN1, 1, LDODIS1, 1, 0x7F, 900, 3400, 25000, 133),
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530195};
196
197static int __devinit rc5t583_regulator_probe(struct platform_device *pdev)
198{
199 struct rc5t583 *rc5t583 = dev_get_drvdata(pdev->dev.parent);
200 struct rc5t583_platform_data *pdata = dev_get_platdata(rc5t583->dev);
201 struct regulator_init_data *reg_data;
Mark Brownc1727082012-04-04 00:50:22 +0100202 struct regulator_config config = { };
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530203 struct rc5t583_regulator *reg = NULL;
204 struct rc5t583_regulator *regs;
205 struct regulator_dev *rdev;
206 struct rc5t583_regulator_info *ri;
207 int ret;
208 int id;
209
210 if (!pdata) {
211 dev_err(&pdev->dev, "No platform data, exiting...\n");
212 return -ENODEV;
213 }
214
215 regs = devm_kzalloc(&pdev->dev, RC5T583_REGULATOR_MAX *
216 sizeof(struct rc5t583_regulator), GFP_KERNEL);
217 if (!regs) {
218 dev_err(&pdev->dev, "Memory allocation failed exiting..\n");
219 return -ENOMEM;
220 }
221
222
223 for (id = 0; id < RC5T583_REGULATOR_MAX; ++id) {
224 reg_data = pdata->reg_init_data[id];
225
226 /* No need to register if there is no regulator data */
227 if (!reg_data)
228 continue;
229
230 reg = &regs[id];
231 ri = &rc5t583_reg_info[id];
232 reg->reg_info = ri;
233 reg->mfd = rc5t583;
234 reg->dev = &pdev->dev;
235
236 if (ri->deepsleep_id == RC5T583_DS_NONE)
237 goto skip_ext_pwr_config;
238
239 ret = rc5t583_ext_power_req_config(rc5t583->dev,
240 ri->deepsleep_id,
241 pdata->regulator_ext_pwr_control[id],
242 pdata->regulator_deepsleep_slot[id]);
243 /*
244 * Configuring external control is not a major issue,
245 * just give warning.
246 */
247 if (ret < 0)
248 dev_warn(&pdev->dev,
249 "Failed to configure ext control %d\n", id);
250
251skip_ext_pwr_config:
Mark Brownc1727082012-04-04 00:50:22 +0100252 config.dev = &pdev->dev;
253 config.init_data = reg_data;
254 config.driver_data = reg;
Axel Lin5bb69362012-04-17 14:11:31 +0800255 config.regmap = rc5t583->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100256
257 rdev = regulator_register(&ri->desc, &config);
Axel Lina69df8a2012-04-04 19:52:35 +0800258 if (IS_ERR(rdev)) {
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530259 dev_err(&pdev->dev, "Failed to register regulator %s\n",
260 ri->desc.name);
261 ret = PTR_ERR(rdev);
262 goto clean_exit;
263 }
264 reg->rdev = rdev;
265 }
266 platform_set_drvdata(pdev, regs);
267 return 0;
268
269clean_exit:
Axel Lina69df8a2012-04-04 19:52:35 +0800270 while (--id >= 0)
Laxman Dewangan6ffc3272012-04-04 12:44:00 +0530271 regulator_unregister(regs[id].rdev);
272
273 return ret;
274}
275
276static int __devexit rc5t583_regulator_remove(struct platform_device *pdev)
277{
278 struct rc5t583_regulator *regs = platform_get_drvdata(pdev);
279 int id;
280
281 for (id = 0; id < RC5T583_REGULATOR_MAX; ++id)
282 regulator_unregister(regs[id].rdev);
283 return 0;
284}
285
286static struct platform_driver rc5t583_regulator_driver = {
287 .driver = {
288 .name = "rc5t583-regulator",
289 .owner = THIS_MODULE,
290 },
291 .probe = rc5t583_regulator_probe,
292 .remove = __devexit_p(rc5t583_regulator_remove),
293};
294
295static int __init rc5t583_regulator_init(void)
296{
297 return platform_driver_register(&rc5t583_regulator_driver);
298}
299subsys_initcall(rc5t583_regulator_init);
300
301static void __exit rc5t583_regulator_exit(void)
302{
303 platform_driver_unregister(&rc5t583_regulator_driver);
304}
305module_exit(rc5t583_regulator_exit);
306
307MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
308MODULE_DESCRIPTION("RC5T583 regulator driver");
309MODULE_ALIAS("platform:rc5t583-regulator");
Laxman Dewangan4eb06452012-04-06 10:58:33 +0530310MODULE_LICENSE("GPL v2");