2 * Regulators driver for Maxim max8925
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
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.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #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>
21 #include <linux/regulator/of_regulator.h>
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 */
29 /* 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 */
37 struct max8925_regulator_info {
38 struct regulator_desc desc;
39 struct regulator_dev *regulator;
40 struct i2c_client *i2c;
41 struct max8925_chip *chip;
47 static int max8925_set_voltage_sel(struct regulator_dev *rdev,
48 unsigned int selector)
50 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
51 unsigned char mask = rdev->desc->n_voltages - 1;
53 return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
56 static int max8925_get_voltage_sel(struct regulator_dev *rdev)
58 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
59 unsigned char data, mask;
62 ret = max8925_reg_read(info->i2c, info->vol_reg);
65 mask = rdev->desc->n_voltages - 1;
71 static int max8925_enable(struct regulator_dev *rdev)
73 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
75 return max8925_set_bits(info->i2c, info->enable_reg,
76 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);
82 static int max8925_disable(struct regulator_dev *rdev)
84 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
86 return max8925_set_bits(info->i2c, info->enable_reg,
87 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
88 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
89 LDO_SEQ_I2C << LDO_SEQ_SHIFT);
92 static int max8925_is_enabled(struct regulator_dev *rdev)
94 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
97 ret = max8925_reg_read(info->i2c, info->enable_reg);
100 ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
101 if (ldo_seq != LDO_SEQ_I2C)
104 return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
107 static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
109 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
110 unsigned char data, mask;
112 if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
115 data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
116 data <<= SD1_DVM_SHIFT;
117 mask = 3 << SD1_DVM_SHIFT;
119 return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
122 static int max8925_set_dvm_enable(struct regulator_dev *rdev)
124 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
126 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
130 static int max8925_set_dvm_disable(struct regulator_dev *rdev)
132 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
134 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
137 static struct regulator_ops max8925_regulator_sdv_ops = {
138 .map_voltage = regulator_map_voltage_linear,
139 .list_voltage = regulator_list_voltage_linear,
140 .set_voltage_sel = max8925_set_voltage_sel,
141 .get_voltage_sel = max8925_get_voltage_sel,
142 .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,
150 static struct regulator_ops max8925_regulator_ldo_ops = {
151 .map_voltage = regulator_map_voltage_linear,
152 .list_voltage = regulator_list_voltage_linear,
153 .set_voltage_sel = max8925_set_voltage_sel,
154 .get_voltage_sel = max8925_get_voltage_sel,
155 .enable = max8925_enable,
156 .disable = max8925_disable,
157 .is_enabled = max8925_is_enabled,
160 #define MAX8925_SDV(_id, min, max, step) \
163 .name = "SDV" #_id, \
164 .ops = &max8925_regulator_sdv_ops, \
165 .type = REGULATOR_VOLTAGE, \
166 .id = MAX8925_ID_SD##_id, \
167 .owner = THIS_MODULE, \
169 .min_uV = min * 1000, \
170 .uV_step = step * 1000, \
172 .vol_reg = MAX8925_SDV##_id, \
173 .enable_reg = MAX8925_SDCTL##_id, \
176 #define MAX8925_LDO(_id, min, max, step) \
179 .name = "LDO" #_id, \
180 .ops = &max8925_regulator_ldo_ops, \
181 .type = REGULATOR_VOLTAGE, \
182 .id = MAX8925_ID_LDO##_id, \
183 .owner = THIS_MODULE, \
185 .min_uV = min * 1000, \
186 .uV_step = step * 1000, \
188 .vol_reg = MAX8925_LDOVOUT##_id, \
189 .enable_reg = MAX8925_LDOCTL##_id, \
193 static struct of_regulator_match max8925_regulator_matches[] = {
220 static 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),
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),
248 static int max8925_regulator_dt_init(struct platform_device *pdev,
249 struct max8925_regulator_info *info,
250 struct regulator_config *config,
253 struct device_node *nproot, *np;
255 nproot = pdev->dev.parent->of_node;
258 np = of_find_node_by_name(nproot, "regulators");
260 dev_err(&pdev->dev, "failed to find regulators node\n");
264 rcount = of_regulator_match(&pdev->dev, np,
265 &max8925_regulator_matches[ridx], 1);
268 config->init_data = max8925_regulator_matches[ridx].init_data;
269 config->of_node = max8925_regulator_matches[ridx].of_node;
274 #define max8925_regulator_dt_init(w, x, y, z) (-1)
277 static int max8925_regulator_probe(struct platform_device *pdev)
279 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
280 struct regulator_init_data *pdata = pdev->dev.platform_data;
281 struct regulator_config config = { };
282 struct max8925_regulator_info *ri;
283 struct resource *res;
284 struct regulator_dev *rdev;
285 int i, regulator_idx;
287 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
289 dev_err(&pdev->dev, "No REG resource!\n");
292 for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
293 ri = &max8925_regulator_info[i];
294 if (ri->vol_reg == res->start) {
300 if (i == ARRAY_SIZE(max8925_regulator_info)) {
301 dev_err(&pdev->dev, "Failed to find regulator %llu\n",
302 (unsigned long long)res->start);
308 config.dev = &pdev->dev;
309 config.driver_data = ri;
311 if (max8925_regulator_dt_init(pdev, ri, &config, regulator_idx))
313 config.init_data = pdata;
315 rdev = regulator_register(&ri->desc, &config);
317 dev_err(&pdev->dev, "failed to register regulator %s\n",
319 return PTR_ERR(rdev);
322 platform_set_drvdata(pdev, rdev);
326 static int max8925_regulator_remove(struct platform_device *pdev)
328 struct regulator_dev *rdev = platform_get_drvdata(pdev);
330 platform_set_drvdata(pdev, NULL);
331 regulator_unregister(rdev);
336 static struct platform_driver max8925_regulator_driver = {
338 .name = "max8925-regulator",
339 .owner = THIS_MODULE,
341 .probe = max8925_regulator_probe,
342 .remove = max8925_regulator_remove,
345 static int __init max8925_regulator_init(void)
347 return platform_driver_register(&max8925_regulator_driver);
349 subsys_initcall(max8925_regulator_init);
351 static void __exit max8925_regulator_exit(void)
353 platform_driver_unregister(&max8925_regulator_driver);
355 module_exit(max8925_regulator_exit);
357 MODULE_LICENSE("GPL");
358 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
359 MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
360 MODULE_ALIAS("platform:max8925-regulator");