blob: 3c9d14c0017bba05c5c770dea4478c51b60f533e [file] [log] [blame]
Balaji Rao5ec271e2009-01-09 01:51:01 +01001/* NXP PCF50633 PMIC Driver
2 *
3 * (C) 2006-2008 by Openmoko, Inc.
4 * Author: Balaji Rao <balajirrao@openmoko.org>
5 * All rights reserved.
6 *
7 * Broken down from monstrous PCF50633 driver mainly by
8 * Harald Welte and Andy Green and Werner Almesberger
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/device.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23
24#include <linux/mfd/pcf50633/core.h>
25#include <linux/mfd/pcf50633/pmic.h>
26
Axel Lin11bb88e2012-04-18 10:50:37 +080027#define PCF50633_REGULATOR(_name, _id, _n) \
28 { \
29 .name = _name, \
30 .id = PCF50633_REGULATOR_##_id, \
31 .ops = &pcf50633_regulator_ops, \
32 .n_voltages = _n, \
33 .type = REGULATOR_VOLTAGE, \
34 .owner = THIS_MODULE, \
Axel Lin98667b42012-04-18 10:51:59 +080035 .vsel_reg = PCF50633_REG_##_id##OUT, \
36 .vsel_mask = 0xff, \
Axel Lin11bb88e2012-04-18 10:50:37 +080037 .enable_reg = PCF50633_REG_##_id##OUT + 1, \
38 .enable_mask = PCF50633_REGULATOR_ON, \
Balaji Rao5ec271e2009-01-09 01:51:01 +010039 }
40
Balaji Rao5ec271e2009-01-09 01:51:01 +010041/* Bits from voltage value */
42static u8 auto_voltage_bits(unsigned int millivolts)
43{
44 if (millivolts < 1800)
Axel Lin022dcdf2012-03-19 10:55:24 +080045 return 0x2f;
Balaji Rao5ec271e2009-01-09 01:51:01 +010046 if (millivolts > 3800)
47 return 0xff;
48
49 millivolts -= 625;
50
51 return millivolts / 25;
52}
53
54static u8 down_voltage_bits(unsigned int millivolts)
55{
56 if (millivolts < 625)
57 return 0;
58 else if (millivolts > 3000)
59 return 0xff;
60
61 millivolts -= 625;
62
63 return millivolts / 25;
64}
65
66static u8 ldo_voltage_bits(unsigned int millivolts)
67{
68 if (millivolts < 900)
69 return 0;
70 else if (millivolts > 3600)
71 return 0x1f;
72
73 millivolts -= 900;
74 return millivolts / 100;
75}
76
77/* Obtain voltage value from bits */
78static unsigned int auto_voltage_value(u8 bits)
79{
Axel Lin022dcdf2012-03-19 10:55:24 +080080 /* AUTOOUT: 00000000 to 00101110 are reserved.
81 * Return 0 for bits in reserved range, which means this selector code
82 * can't be used on this system */
Balaji Rao5ec271e2009-01-09 01:51:01 +010083 if (bits < 0x2f)
84 return 0;
85
86 return 625 + (bits * 25);
87}
88
89
90static unsigned int down_voltage_value(u8 bits)
91{
92 return 625 + (bits * 25);
93}
94
95
96static unsigned int ldo_voltage_value(u8 bits)
97{
98 bits &= 0x1f;
99
100 return 900 + (bits * 100);
101}
102
103static int pcf50633_regulator_set_voltage(struct regulator_dev *rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000104 int min_uV, int max_uV,
105 unsigned *selector)
Balaji Rao5ec271e2009-01-09 01:51:01 +0100106{
107 struct pcf50633 *pcf;
108 int regulator_id, millivolts;
109 u8 volt_bits, regnr;
110
111 pcf = rdev_get_drvdata(rdev);
112
113 regulator_id = rdev_get_id(rdev);
114 if (regulator_id >= PCF50633_NUM_REGULATORS)
115 return -EINVAL;
116
117 millivolts = min_uV / 1000;
118
Axel Lin98667b42012-04-18 10:51:59 +0800119 regnr = rdev->desc->vsel_reg;
Balaji Rao5ec271e2009-01-09 01:51:01 +0100120
121 switch (regulator_id) {
122 case PCF50633_REGULATOR_AUTO:
123 volt_bits = auto_voltage_bits(millivolts);
124 break;
125 case PCF50633_REGULATOR_DOWN1:
126 volt_bits = down_voltage_bits(millivolts);
127 break;
128 case PCF50633_REGULATOR_DOWN2:
129 volt_bits = down_voltage_bits(millivolts);
130 break;
131 case PCF50633_REGULATOR_LDO1:
132 case PCF50633_REGULATOR_LDO2:
133 case PCF50633_REGULATOR_LDO3:
134 case PCF50633_REGULATOR_LDO4:
135 case PCF50633_REGULATOR_LDO5:
136 case PCF50633_REGULATOR_LDO6:
137 case PCF50633_REGULATOR_HCLDO:
Axel Linba51c6c2012-02-29 12:45:35 +0800138 case PCF50633_REGULATOR_MEMLDO:
Balaji Rao5ec271e2009-01-09 01:51:01 +0100139 volt_bits = ldo_voltage_bits(millivolts);
140 break;
141 default:
142 return -EINVAL;
143 }
144
Mark Brown3a93f2a2010-11-10 14:38:29 +0000145 *selector = volt_bits;
146
Balaji Rao5ec271e2009-01-09 01:51:01 +0100147 return pcf50633_reg_write(pcf, regnr, volt_bits);
148}
149
Lars-Peter Clausena6576cf2009-08-04 02:03:52 +0200150static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev,
151 unsigned int index)
152{
Axel Lin022dcdf2012-03-19 10:55:24 +0800153 int regulator_id = rdev_get_id(rdev);
Balaji Rao5ec271e2009-01-09 01:51:01 +0100154
Axel Lin8300d2f2012-03-19 10:57:06 +0800155 int millivolts;
156
157 switch (regulator_id) {
158 case PCF50633_REGULATOR_AUTO:
159 millivolts = auto_voltage_value(index);
160 break;
161 case PCF50633_REGULATOR_DOWN1:
162 millivolts = down_voltage_value(index);
163 break;
164 case PCF50633_REGULATOR_DOWN2:
165 millivolts = down_voltage_value(index);
166 break;
167 case PCF50633_REGULATOR_LDO1:
168 case PCF50633_REGULATOR_LDO2:
169 case PCF50633_REGULATOR_LDO3:
170 case PCF50633_REGULATOR_LDO4:
171 case PCF50633_REGULATOR_LDO5:
172 case PCF50633_REGULATOR_LDO6:
173 case PCF50633_REGULATOR_HCLDO:
174 case PCF50633_REGULATOR_MEMLDO:
175 millivolts = ldo_voltage_value(index);
176 break;
177 default:
178 return -EINVAL;
179 }
180
181 return millivolts * 1000;
Balaji Rao5ec271e2009-01-09 01:51:01 +0100182}
183
Balaji Rao5ec271e2009-01-09 01:51:01 +0100184static struct regulator_ops pcf50633_regulator_ops = {
185 .set_voltage = pcf50633_regulator_set_voltage,
Axel Lin98667b42012-04-18 10:51:59 +0800186 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Lars-Peter Clausena6576cf2009-08-04 02:03:52 +0200187 .list_voltage = pcf50633_regulator_list_voltage,
Axel Lin11bb88e2012-04-18 10:50:37 +0800188 .enable = regulator_enable_regmap,
189 .disable = regulator_disable_regmap,
190 .is_enabled = regulator_is_enabled_regmap,
Balaji Rao5ec271e2009-01-09 01:51:01 +0100191};
192
Axel Lin2ac2d7d2012-04-05 12:02:36 +0800193static const struct regulator_desc regulators[] = {
Axel Lin11bb88e2012-04-18 10:50:37 +0800194 [PCF50633_REGULATOR_AUTO] = PCF50633_REGULATOR("auto", AUTO, 128),
195 [PCF50633_REGULATOR_DOWN1] = PCF50633_REGULATOR("down1", DOWN1, 96),
196 [PCF50633_REGULATOR_DOWN2] = PCF50633_REGULATOR("down2", DOWN2, 96),
197 [PCF50633_REGULATOR_LDO1] = PCF50633_REGULATOR("ldo1", LDO1, 28),
198 [PCF50633_REGULATOR_LDO2] = PCF50633_REGULATOR("ldo2", LDO2, 28),
199 [PCF50633_REGULATOR_LDO3] = PCF50633_REGULATOR("ldo3", LDO3, 28),
200 [PCF50633_REGULATOR_LDO4] = PCF50633_REGULATOR("ldo4", LDO4, 28),
201 [PCF50633_REGULATOR_LDO5] = PCF50633_REGULATOR("ldo5", LDO5, 28),
202 [PCF50633_REGULATOR_LDO6] = PCF50633_REGULATOR("ldo6", LDO6, 28),
203 [PCF50633_REGULATOR_HCLDO] = PCF50633_REGULATOR("hcldo", HCLDO, 28),
204 [PCF50633_REGULATOR_MEMLDO] = PCF50633_REGULATOR("memldo", MEMLDO, 28),
Balaji Rao5ec271e2009-01-09 01:51:01 +0100205};
206
207static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
208{
209 struct regulator_dev *rdev;
210 struct pcf50633 *pcf;
Mark Brownc1727082012-04-04 00:50:22 +0100211 struct regulator_config config = { };
Balaji Rao5ec271e2009-01-09 01:51:01 +0100212
213 /* Already set by core driver */
Lars-Peter Clausen98c2e492009-10-14 02:12:36 +0400214 pcf = dev_to_pcf50633(pdev->dev.parent);
Balaji Rao5ec271e2009-01-09 01:51:01 +0100215
Mark Brownc1727082012-04-04 00:50:22 +0100216 config.dev = &pdev->dev;
217 config.init_data = pdev->dev.platform_data;
218 config.driver_data = pcf;
Axel Lin11bb88e2012-04-18 10:50:37 +0800219 config.regmap = pcf->regmap;
Mark Brownc1727082012-04-04 00:50:22 +0100220
221 rdev = regulator_register(&regulators[pdev->id], &config);
Balaji Rao5ec271e2009-01-09 01:51:01 +0100222 if (IS_ERR(rdev))
223 return PTR_ERR(rdev);
224
Lars-Peter Clausen98c2e492009-10-14 02:12:36 +0400225 platform_set_drvdata(pdev, rdev);
226
Balaji Rao5ec271e2009-01-09 01:51:01 +0100227 if (pcf->pdata->regulator_registered)
228 pcf->pdata->regulator_registered(pcf, pdev->id);
229
230 return 0;
231}
232
233static int __devexit pcf50633_regulator_remove(struct platform_device *pdev)
234{
235 struct regulator_dev *rdev = platform_get_drvdata(pdev);
236
Lars-Peter Clausen98c2e492009-10-14 02:12:36 +0400237 platform_set_drvdata(pdev, NULL);
Balaji Rao5ec271e2009-01-09 01:51:01 +0100238 regulator_unregister(rdev);
239
240 return 0;
241}
242
243static struct platform_driver pcf50633_regulator_driver = {
244 .driver = {
245 .name = "pcf50633-regltr",
246 },
247 .probe = pcf50633_regulator_probe,
248 .remove = __devexit_p(pcf50633_regulator_remove),
249};
250
251static int __init pcf50633_regulator_init(void)
252{
253 return platform_driver_register(&pcf50633_regulator_driver);
254}
Mark Brown5a1b22b2009-04-27 18:21:18 +0100255subsys_initcall(pcf50633_regulator_init);
Balaji Rao5ec271e2009-01-09 01:51:01 +0100256
257static void __exit pcf50633_regulator_exit(void)
258{
259 platform_driver_unregister(&pcf50633_regulator_driver);
260}
261module_exit(pcf50633_regulator_exit);
262
263MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
264MODULE_DESCRIPTION("PCF50633 regulator driver");
265MODULE_LICENSE("GPL");
266MODULE_ALIAS("platform:pcf50633-regulator");