blob: e6d54a546d36d40c68aeb0b623c8d83fee79ef0f [file] [log] [blame]
Haojian Zhuang08ff6f22010-01-11 13:10:08 +01001/*
2 * Regulators driver for Maxim max8925
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/kernel.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040012#include <linux/module.h>
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010013#include <linux/init.h>
14#include <linux/err.h>
15#include <linux/i2c.h>
16#include <linux/platform_device.h>
17#include <linux/regulator/driver.h>
18#include <linux/regulator/machine.h>
19#include <linux/mfd/max8925.h>
Qing Xu45b6f8e2012-11-07 18:09:53 +080020#include <linux/of.h>
21#include <linux/regulator/of_regulator.h>
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010022
23#define SD1_DVM_VMIN 850000
24#define SD1_DVM_VMAX 1000000
25#define SD1_DVM_STEP 50000
26#define SD1_DVM_SHIFT 5 /* SDCTL1 bit5 */
27#define SD1_DVM_EN 6 /* SDV1 bit 6 */
28
Kevin Liu65364e92012-01-04 15:12:04 +080029/* bit definitions in LDO control registers */
30#define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */
31#define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */
32#define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */
33#define LDO_I2C_EN 0x1 /* Enable by i2c */
34#define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */
35#define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */
Haojian Zhuangdb13c042011-04-18 22:04:09 +080036
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010037struct max8925_regulator_info {
38 struct regulator_desc desc;
39 struct regulator_dev *regulator;
40 struct i2c_client *i2c;
41 struct max8925_chip *chip;
42
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010043 int vol_reg;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010044 int enable_reg;
45};
46
Axel Lincd8318c2012-05-14 10:36:40 +080047static int max8925_set_voltage_sel(struct regulator_dev *rdev,
48 unsigned int selector)
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010049{
50 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
Axel Lincd8318c2012-05-14 10:36:40 +080051 unsigned char mask = rdev->desc->n_voltages - 1;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010052
Axel Lincd8318c2012-05-14 10:36:40 +080053 return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010054}
55
Axel Lin3c8983b2012-05-14 10:34:50 +080056static int max8925_get_voltage_sel(struct regulator_dev *rdev)
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010057{
58 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
59 unsigned char data, mask;
60 int ret;
61
62 ret = max8925_reg_read(info->i2c, info->vol_reg);
63 if (ret < 0)
64 return ret;
Axel Lin7f52ba72012-04-02 09:16:08 +080065 mask = rdev->desc->n_voltages - 1;
66 data = ret & mask;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010067
Axel Lin3c8983b2012-05-14 10:34:50 +080068 return data;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010069}
70
71static int max8925_enable(struct regulator_dev *rdev)
72{
73 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
74
75 return max8925_set_bits(info->i2c, info->enable_reg,
Kevin Liu65364e92012-01-04 15:12:04 +080076 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
77 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
78 LDO_SEQ_I2C << LDO_SEQ_SHIFT |
79 LDO_I2C_EN << LDO_I2C_EN_SHIFT);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010080}
81
82static int max8925_disable(struct regulator_dev *rdev)
83{
84 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
85
86 return max8925_set_bits(info->i2c, info->enable_reg,
Kevin Liu65364e92012-01-04 15:12:04 +080087 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
88 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
89 LDO_SEQ_I2C << LDO_SEQ_SHIFT);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010090}
91
92static int max8925_is_enabled(struct regulator_dev *rdev)
93{
94 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
Kevin Liu65364e92012-01-04 15:12:04 +080095 int ldo_seq, ret;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010096
Haojian Zhuang4b99fef2010-04-06 06:19:15 -040097 ret = max8925_reg_read(info->i2c, info->enable_reg);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010098 if (ret < 0)
99 return ret;
Kevin Liu65364e92012-01-04 15:12:04 +0800100 ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
101 if (ldo_seq != LDO_SEQ_I2C)
102 return 1;
103 else
104 return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100105}
106
107static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
108{
109 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
110 unsigned char data, mask;
111
112 if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
113 return -EINVAL;
114
Axel Lin5db6d092012-03-13 16:09:28 +0800115 data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100116 data <<= SD1_DVM_SHIFT;
117 mask = 3 << SD1_DVM_SHIFT;
118
119 return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
120}
121
122static int max8925_set_dvm_enable(struct regulator_dev *rdev)
123{
124 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
125
126 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
127 1 << SD1_DVM_EN);
128}
129
130static int max8925_set_dvm_disable(struct regulator_dev *rdev)
131{
132 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
133
134 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
135}
136
137static struct regulator_ops max8925_regulator_sdv_ops = {
Axel Lincd8318c2012-05-14 10:36:40 +0800138 .map_voltage = regulator_map_voltage_linear,
Axel Lin4093e732012-05-14 10:35:40 +0800139 .list_voltage = regulator_list_voltage_linear,
Axel Lincd8318c2012-05-14 10:36:40 +0800140 .set_voltage_sel = max8925_set_voltage_sel,
Axel Lin3c8983b2012-05-14 10:34:50 +0800141 .get_voltage_sel = max8925_get_voltage_sel,
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100142 .enable = max8925_enable,
143 .disable = max8925_disable,
144 .is_enabled = max8925_is_enabled,
145 .set_suspend_voltage = max8925_set_dvm_voltage,
146 .set_suspend_enable = max8925_set_dvm_enable,
147 .set_suspend_disable = max8925_set_dvm_disable,
148};
149
150static struct regulator_ops max8925_regulator_ldo_ops = {
Axel Lincd8318c2012-05-14 10:36:40 +0800151 .map_voltage = regulator_map_voltage_linear,
Axel Lin4093e732012-05-14 10:35:40 +0800152 .list_voltage = regulator_list_voltage_linear,
Axel Lincd8318c2012-05-14 10:36:40 +0800153 .set_voltage_sel = max8925_set_voltage_sel,
Axel Lin3c8983b2012-05-14 10:34:50 +0800154 .get_voltage_sel = max8925_get_voltage_sel,
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100155 .enable = max8925_enable,
156 .disable = max8925_disable,
157 .is_enabled = max8925_is_enabled,
158};
159
160#define MAX8925_SDV(_id, min, max, step) \
161{ \
162 .desc = { \
163 .name = "SDV" #_id, \
164 .ops = &max8925_regulator_sdv_ops, \
165 .type = REGULATOR_VOLTAGE, \
166 .id = MAX8925_ID_SD##_id, \
167 .owner = THIS_MODULE, \
Axel Lin7f52ba72012-04-02 09:16:08 +0800168 .n_voltages = 64, \
Axel Lin4093e732012-05-14 10:35:40 +0800169 .min_uV = min * 1000, \
170 .uV_step = step * 1000, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100171 }, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100172 .vol_reg = MAX8925_SDV##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100173 .enable_reg = MAX8925_SDCTL##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100174}
175
176#define MAX8925_LDO(_id, min, max, step) \
177{ \
178 .desc = { \
179 .name = "LDO" #_id, \
180 .ops = &max8925_regulator_ldo_ops, \
181 .type = REGULATOR_VOLTAGE, \
182 .id = MAX8925_ID_LDO##_id, \
183 .owner = THIS_MODULE, \
Axel Lin7f52ba72012-04-02 09:16:08 +0800184 .n_voltages = 64, \
Axel Lin4093e732012-05-14 10:35:40 +0800185 .min_uV = min * 1000, \
186 .uV_step = step * 1000, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100187 }, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100188 .vol_reg = MAX8925_LDOVOUT##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100189 .enable_reg = MAX8925_LDOCTL##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100190}
191
Qing Xu45b6f8e2012-11-07 18:09:53 +0800192#ifdef CONFIG_OF
193static struct of_regulator_match max8925_regulator_matches[] = {
194 { .name = "SDV1",},
195 { .name = "SDV2",},
196 { .name = "SDV3",},
197 { .name = "LDO1",},
198 { .name = "LDO2",},
199 { .name = "LDO3",},
200 { .name = "LDO4",},
201 { .name = "LDO5",},
202 { .name = "LDO6",},
203 { .name = "LDO7",},
204 { .name = "LDO8",},
205 { .name = "LDO9",},
206 { .name = "LDO10",},
207 { .name = "LDO11",},
208 { .name = "LDO12",},
209 { .name = "LDO13",},
210 { .name = "LDO14",},
211 { .name = "LDO15",},
212 { .name = "LDO16",},
213 { .name = "LDO17",},
214 { .name = "LDO18",},
215 { .name = "LDO19",},
216 { .name = "LDO20",},
217};
218#endif
219
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100220static struct max8925_regulator_info max8925_regulator_info[] = {
221 MAX8925_SDV(1, 637.5, 1425, 12.5),
222 MAX8925_SDV(2, 650, 2225, 25),
223 MAX8925_SDV(3, 750, 3900, 50),
224
225 MAX8925_LDO(1, 750, 3900, 50),
226 MAX8925_LDO(2, 650, 2250, 25),
227 MAX8925_LDO(3, 650, 2250, 25),
228 MAX8925_LDO(4, 750, 3900, 50),
229 MAX8925_LDO(5, 750, 3900, 50),
230 MAX8925_LDO(6, 750, 3900, 50),
231 MAX8925_LDO(7, 750, 3900, 50),
232 MAX8925_LDO(8, 750, 3900, 50),
233 MAX8925_LDO(9, 750, 3900, 50),
234 MAX8925_LDO(10, 750, 3900, 50),
235 MAX8925_LDO(11, 750, 3900, 50),
236 MAX8925_LDO(12, 750, 3900, 50),
237 MAX8925_LDO(13, 750, 3900, 50),
238 MAX8925_LDO(14, 750, 3900, 50),
239 MAX8925_LDO(15, 750, 3900, 50),
240 MAX8925_LDO(16, 750, 3900, 50),
241 MAX8925_LDO(17, 650, 2250, 25),
242 MAX8925_LDO(18, 650, 2250, 25),
243 MAX8925_LDO(19, 750, 3900, 50),
244 MAX8925_LDO(20, 750, 3900, 50),
245};
246
Qing Xu45b6f8e2012-11-07 18:09:53 +0800247#ifdef CONFIG_OF
248static int max8925_regulator_dt_init(struct platform_device *pdev,
Qing Xu45b6f8e2012-11-07 18:09:53 +0800249 struct regulator_config *config,
250 int ridx)
251{
252 struct device_node *nproot, *np;
253 int rcount;
Axel Linc92f5dd2013-01-27 21:16:56 +0800254 nproot = of_node_get(pdev->dev.parent->of_node);
Qing Xu45b6f8e2012-11-07 18:09:53 +0800255 if (!nproot)
256 return -ENODEV;
257 np = of_find_node_by_name(nproot, "regulators");
258 if (!np) {
259 dev_err(&pdev->dev, "failed to find regulators node\n");
260 return -ENODEV;
261 }
262
263 rcount = of_regulator_match(&pdev->dev, np,
264 &max8925_regulator_matches[ridx], 1);
Axel Linc92f5dd2013-01-27 21:16:56 +0800265 of_node_put(np);
Qing Xu45b6f8e2012-11-07 18:09:53 +0800266 if (rcount < 0)
267 return -ENODEV;
268 config->init_data = max8925_regulator_matches[ridx].init_data;
269 config->of_node = max8925_regulator_matches[ridx].of_node;
270
271 return 0;
272}
273#else
Axel Linad4928f2013-02-15 20:36:06 +0800274#define max8925_regulator_dt_init(x, y, z) (-1)
Qing Xu45b6f8e2012-11-07 18:09:53 +0800275#endif
276
Bill Pembertona5023572012-11-19 13:22:22 -0500277static int max8925_regulator_probe(struct platform_device *pdev)
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100278{
279 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800280 struct regulator_init_data *pdata = pdev->dev.platform_data;
Mark Brownc1727082012-04-04 00:50:22 +0100281 struct regulator_config config = { };
Dmitry Torokhov3fff6c02010-02-23 23:38:55 -0800282 struct max8925_regulator_info *ri;
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800283 struct resource *res;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100284 struct regulator_dev *rdev;
Qing Xu45b6f8e2012-11-07 18:09:53 +0800285 int i, regulator_idx;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100286
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800287 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
288 if (!res) {
289 dev_err(&pdev->dev, "No REG resource!\n");
290 return -EINVAL;
291 }
292 for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
293 ri = &max8925_regulator_info[i];
Qing Xu45b6f8e2012-11-07 18:09:53 +0800294 if (ri->vol_reg == res->start) {
295 regulator_idx = i;
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800296 break;
Qing Xu45b6f8e2012-11-07 18:09:53 +0800297 }
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800298 }
Qing Xu45b6f8e2012-11-07 18:09:53 +0800299
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800300 if (i == ARRAY_SIZE(max8925_regulator_info)) {
301 dev_err(&pdev->dev, "Failed to find regulator %llu\n",
302 (unsigned long long)res->start);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100303 return -EINVAL;
304 }
305 ri->i2c = chip->i2c;
306 ri->chip = chip;
307
Mark Brownc1727082012-04-04 00:50:22 +0100308 config.dev = &pdev->dev;
Mark Brownc1727082012-04-04 00:50:22 +0100309 config.driver_data = ri;
310
Axel Linad4928f2013-02-15 20:36:06 +0800311 if (max8925_regulator_dt_init(pdev, &config, regulator_idx))
Qing Xu45b6f8e2012-11-07 18:09:53 +0800312 if (pdata)
313 config.init_data = pdata;
314
Mark Brownc1727082012-04-04 00:50:22 +0100315 rdev = regulator_register(&ri->desc, &config);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100316 if (IS_ERR(rdev)) {
317 dev_err(&pdev->dev, "failed to register regulator %s\n",
318 ri->desc.name);
319 return PTR_ERR(rdev);
320 }
321
322 platform_set_drvdata(pdev, rdev);
323 return 0;
324}
325
Bill Pemberton8dc995f2012-11-19 13:26:10 -0500326static int max8925_regulator_remove(struct platform_device *pdev)
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100327{
328 struct regulator_dev *rdev = platform_get_drvdata(pdev);
329
330 regulator_unregister(rdev);
Dmitry Torokhov3fff6c02010-02-23 23:38:55 -0800331
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100332 return 0;
333}
334
335static struct platform_driver max8925_regulator_driver = {
336 .driver = {
337 .name = "max8925-regulator",
338 .owner = THIS_MODULE,
339 },
340 .probe = max8925_regulator_probe,
Bill Pemberton5eb9f2b2012-11-19 13:20:42 -0500341 .remove = max8925_regulator_remove,
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100342};
343
344static int __init max8925_regulator_init(void)
345{
346 return platform_driver_register(&max8925_regulator_driver);
347}
348subsys_initcall(max8925_regulator_init);
349
350static void __exit max8925_regulator_exit(void)
351{
352 platform_driver_unregister(&max8925_regulator_driver);
353}
354module_exit(max8925_regulator_exit);
355
356MODULE_LICENSE("GPL");
357MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
358MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
359MODULE_ALIAS("platform:max8925-regulator");