blob: 9bb0be37495f417d65728720f2d13217d51962d5 [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>
20
21#define SD1_DVM_VMIN 850000
22#define SD1_DVM_VMAX 1000000
23#define SD1_DVM_STEP 50000
24#define SD1_DVM_SHIFT 5 /* SDCTL1 bit5 */
25#define SD1_DVM_EN 6 /* SDV1 bit 6 */
26
Kevin Liu65364e92012-01-04 15:12:04 +080027/* bit definitions in LDO control registers */
28#define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */
29#define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */
30#define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */
31#define LDO_I2C_EN 0x1 /* Enable by i2c */
32#define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */
33#define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */
Haojian Zhuangdb13c042011-04-18 22:04:09 +080034
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010035struct max8925_regulator_info {
36 struct regulator_desc desc;
37 struct regulator_dev *regulator;
38 struct i2c_client *i2c;
39 struct max8925_chip *chip;
40
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010041 int vol_reg;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010042 int enable_reg;
43};
44
Axel Lincd8318c2012-05-14 10:36:40 +080045static int max8925_set_voltage_sel(struct regulator_dev *rdev,
46 unsigned int selector)
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010047{
48 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
Axel Lincd8318c2012-05-14 10:36:40 +080049 unsigned char mask = rdev->desc->n_voltages - 1;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010050
Axel Lincd8318c2012-05-14 10:36:40 +080051 return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010052}
53
Axel Lin3c8983b2012-05-14 10:34:50 +080054static int max8925_get_voltage_sel(struct regulator_dev *rdev)
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010055{
56 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
57 unsigned char data, mask;
58 int ret;
59
60 ret = max8925_reg_read(info->i2c, info->vol_reg);
61 if (ret < 0)
62 return ret;
Axel Lin7f52ba72012-04-02 09:16:08 +080063 mask = rdev->desc->n_voltages - 1;
64 data = ret & mask;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010065
Axel Lin3c8983b2012-05-14 10:34:50 +080066 return data;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010067}
68
69static int max8925_enable(struct regulator_dev *rdev)
70{
71 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
72
73 return max8925_set_bits(info->i2c, info->enable_reg,
Kevin Liu65364e92012-01-04 15:12:04 +080074 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
75 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
76 LDO_SEQ_I2C << LDO_SEQ_SHIFT |
77 LDO_I2C_EN << LDO_I2C_EN_SHIFT);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010078}
79
80static int max8925_disable(struct regulator_dev *rdev)
81{
82 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
83
84 return max8925_set_bits(info->i2c, info->enable_reg,
Kevin Liu65364e92012-01-04 15:12:04 +080085 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
86 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
87 LDO_SEQ_I2C << LDO_SEQ_SHIFT);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010088}
89
90static int max8925_is_enabled(struct regulator_dev *rdev)
91{
92 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
Kevin Liu65364e92012-01-04 15:12:04 +080093 int ldo_seq, ret;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010094
Haojian Zhuang4b99fef2010-04-06 06:19:15 -040095 ret = max8925_reg_read(info->i2c, info->enable_reg);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +010096 if (ret < 0)
97 return ret;
Kevin Liu65364e92012-01-04 15:12:04 +080098 ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
99 if (ldo_seq != LDO_SEQ_I2C)
100 return 1;
101 else
102 return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100103}
104
105static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
106{
107 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
108 unsigned char data, mask;
109
110 if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
111 return -EINVAL;
112
Axel Lin5db6d092012-03-13 16:09:28 +0800113 data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100114 data <<= SD1_DVM_SHIFT;
115 mask = 3 << SD1_DVM_SHIFT;
116
117 return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
118}
119
120static int max8925_set_dvm_enable(struct regulator_dev *rdev)
121{
122 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
123
124 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
125 1 << SD1_DVM_EN);
126}
127
128static int max8925_set_dvm_disable(struct regulator_dev *rdev)
129{
130 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
131
132 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
133}
134
135static struct regulator_ops max8925_regulator_sdv_ops = {
Axel Lincd8318c2012-05-14 10:36:40 +0800136 .map_voltage = regulator_map_voltage_linear,
Axel Lin4093e732012-05-14 10:35:40 +0800137 .list_voltage = regulator_list_voltage_linear,
Axel Lincd8318c2012-05-14 10:36:40 +0800138 .set_voltage_sel = max8925_set_voltage_sel,
Axel Lin3c8983b2012-05-14 10:34:50 +0800139 .get_voltage_sel = max8925_get_voltage_sel,
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100140 .enable = max8925_enable,
141 .disable = max8925_disable,
142 .is_enabled = max8925_is_enabled,
143 .set_suspend_voltage = max8925_set_dvm_voltage,
144 .set_suspend_enable = max8925_set_dvm_enable,
145 .set_suspend_disable = max8925_set_dvm_disable,
146};
147
148static struct regulator_ops max8925_regulator_ldo_ops = {
Axel Lincd8318c2012-05-14 10:36:40 +0800149 .map_voltage = regulator_map_voltage_linear,
Axel Lin4093e732012-05-14 10:35:40 +0800150 .list_voltage = regulator_list_voltage_linear,
Axel Lincd8318c2012-05-14 10:36:40 +0800151 .set_voltage_sel = max8925_set_voltage_sel,
Axel Lin3c8983b2012-05-14 10:34:50 +0800152 .get_voltage_sel = max8925_get_voltage_sel,
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100153 .enable = max8925_enable,
154 .disable = max8925_disable,
155 .is_enabled = max8925_is_enabled,
156};
157
158#define MAX8925_SDV(_id, min, max, step) \
159{ \
160 .desc = { \
161 .name = "SDV" #_id, \
162 .ops = &max8925_regulator_sdv_ops, \
163 .type = REGULATOR_VOLTAGE, \
164 .id = MAX8925_ID_SD##_id, \
165 .owner = THIS_MODULE, \
Axel Lin7f52ba72012-04-02 09:16:08 +0800166 .n_voltages = 64, \
Axel Lin4093e732012-05-14 10:35:40 +0800167 .min_uV = min * 1000, \
168 .uV_step = step * 1000, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100169 }, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100170 .vol_reg = MAX8925_SDV##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100171 .enable_reg = MAX8925_SDCTL##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100172}
173
174#define MAX8925_LDO(_id, min, max, step) \
175{ \
176 .desc = { \
177 .name = "LDO" #_id, \
178 .ops = &max8925_regulator_ldo_ops, \
179 .type = REGULATOR_VOLTAGE, \
180 .id = MAX8925_ID_LDO##_id, \
181 .owner = THIS_MODULE, \
Axel Lin7f52ba72012-04-02 09:16:08 +0800182 .n_voltages = 64, \
Axel Lin4093e732012-05-14 10:35:40 +0800183 .min_uV = min * 1000, \
184 .uV_step = step * 1000, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100185 }, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100186 .vol_reg = MAX8925_LDOVOUT##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100187 .enable_reg = MAX8925_LDOCTL##_id, \
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100188}
189
190static struct max8925_regulator_info max8925_regulator_info[] = {
191 MAX8925_SDV(1, 637.5, 1425, 12.5),
192 MAX8925_SDV(2, 650, 2225, 25),
193 MAX8925_SDV(3, 750, 3900, 50),
194
195 MAX8925_LDO(1, 750, 3900, 50),
196 MAX8925_LDO(2, 650, 2250, 25),
197 MAX8925_LDO(3, 650, 2250, 25),
198 MAX8925_LDO(4, 750, 3900, 50),
199 MAX8925_LDO(5, 750, 3900, 50),
200 MAX8925_LDO(6, 750, 3900, 50),
201 MAX8925_LDO(7, 750, 3900, 50),
202 MAX8925_LDO(8, 750, 3900, 50),
203 MAX8925_LDO(9, 750, 3900, 50),
204 MAX8925_LDO(10, 750, 3900, 50),
205 MAX8925_LDO(11, 750, 3900, 50),
206 MAX8925_LDO(12, 750, 3900, 50),
207 MAX8925_LDO(13, 750, 3900, 50),
208 MAX8925_LDO(14, 750, 3900, 50),
209 MAX8925_LDO(15, 750, 3900, 50),
210 MAX8925_LDO(16, 750, 3900, 50),
211 MAX8925_LDO(17, 650, 2250, 25),
212 MAX8925_LDO(18, 650, 2250, 25),
213 MAX8925_LDO(19, 750, 3900, 50),
214 MAX8925_LDO(20, 750, 3900, 50),
215};
216
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100217static int __devinit max8925_regulator_probe(struct platform_device *pdev)
218{
219 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800220 struct regulator_init_data *pdata = pdev->dev.platform_data;
Mark Brownc1727082012-04-04 00:50:22 +0100221 struct regulator_config config = { };
Dmitry Torokhov3fff6c02010-02-23 23:38:55 -0800222 struct max8925_regulator_info *ri;
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800223 struct resource *res;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100224 struct regulator_dev *rdev;
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800225 int i;
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100226
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800227 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
228 if (!res) {
229 dev_err(&pdev->dev, "No REG resource!\n");
230 return -EINVAL;
231 }
232 for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
233 ri = &max8925_regulator_info[i];
234 if (ri->vol_reg == res->start)
235 break;
236 }
237 if (i == ARRAY_SIZE(max8925_regulator_info)) {
238 dev_err(&pdev->dev, "Failed to find regulator %llu\n",
239 (unsigned long long)res->start);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100240 return -EINVAL;
241 }
242 ri->i2c = chip->i2c;
243 ri->chip = chip;
244
Mark Brownc1727082012-04-04 00:50:22 +0100245 config.dev = &pdev->dev;
Haojian Zhuang51acdb62012-09-17 12:19:05 +0800246 config.init_data = pdata;
Mark Brownc1727082012-04-04 00:50:22 +0100247 config.driver_data = ri;
248
249 rdev = regulator_register(&ri->desc, &config);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100250 if (IS_ERR(rdev)) {
251 dev_err(&pdev->dev, "failed to register regulator %s\n",
252 ri->desc.name);
253 return PTR_ERR(rdev);
254 }
255
256 platform_set_drvdata(pdev, rdev);
257 return 0;
258}
259
260static int __devexit max8925_regulator_remove(struct platform_device *pdev)
261{
262 struct regulator_dev *rdev = platform_get_drvdata(pdev);
263
Dmitry Torokhov3fff6c02010-02-23 23:38:55 -0800264 platform_set_drvdata(pdev, NULL);
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100265 regulator_unregister(rdev);
Dmitry Torokhov3fff6c02010-02-23 23:38:55 -0800266
Haojian Zhuang08ff6f22010-01-11 13:10:08 +0100267 return 0;
268}
269
270static struct platform_driver max8925_regulator_driver = {
271 .driver = {
272 .name = "max8925-regulator",
273 .owner = THIS_MODULE,
274 },
275 .probe = max8925_regulator_probe,
276 .remove = __devexit_p(max8925_regulator_remove),
277};
278
279static int __init max8925_regulator_init(void)
280{
281 return platform_driver_register(&max8925_regulator_driver);
282}
283subsys_initcall(max8925_regulator_init);
284
285static void __exit max8925_regulator_exit(void)
286{
287 platform_driver_unregister(&max8925_regulator_driver);
288}
289module_exit(max8925_regulator_exit);
290
291MODULE_LICENSE("GPL");
292MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
293MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
294MODULE_ALIAS("platform:max8925-regulator");