2 * Regulators driver for Maxim max8649
4 * Copyright (C) 2009-2010 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/err.h>
14 #include <linux/i2c.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/slab.h>
18 #include <linux/regulator/max8649.h>
19 #include <linux/regmap.h>
21 #define MAX8649_DCDC_VMIN 750000 /* uV */
22 #define MAX8649_DCDC_VMAX 1380000 /* uV */
23 #define MAX8649_DCDC_STEP 10000 /* uV */
24 #define MAX8649_VOL_MASK 0x3f
27 #define MAX8649_MODE0 0x00
28 #define MAX8649_MODE1 0x01
29 #define MAX8649_MODE2 0x02
30 #define MAX8649_MODE3 0x03
31 #define MAX8649_CONTROL 0x04
32 #define MAX8649_SYNC 0x05
33 #define MAX8649_RAMP 0x06
34 #define MAX8649_CHIP_ID1 0x08
35 #define MAX8649_CHIP_ID2 0x09
38 #define MAX8649_EN_PD (1 << 7)
39 #define MAX8649_VID0_PD (1 << 6)
40 #define MAX8649_VID1_PD (1 << 5)
41 #define MAX8649_VID_MASK (3 << 5)
43 #define MAX8649_FORCE_PWM (1 << 7)
44 #define MAX8649_SYNC_EXTCLK (1 << 6)
46 #define MAX8649_EXT_MASK (3 << 6)
48 #define MAX8649_RAMP_MASK (7 << 5)
49 #define MAX8649_RAMP_DOWN (1 << 1)
51 struct max8649_regulator_info
{
53 struct regmap
*regmap
;
55 unsigned mode
:2; /* bit[1:0] = VID1, VID0 */
56 unsigned extclk_freq
:2;
58 unsigned ramp_timing
:3;
62 static int max8649_enable_time(struct regulator_dev
*rdev
)
64 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
65 int voltage
, rate
, ret
;
69 ret
= regmap_read(info
->regmap
, rdev
->desc
->vsel_reg
, &val
);
72 val
&= MAX8649_VOL_MASK
;
73 voltage
= regulator_list_voltage_linear(rdev
, (unsigned char)val
);
76 ret
= regmap_read(info
->regmap
, MAX8649_RAMP
, &val
);
79 ret
= (val
& MAX8649_RAMP_MASK
) >> 5;
80 rate
= (32 * 1000) >> ret
; /* uV/uS */
82 return DIV_ROUND_UP(voltage
, rate
);
85 static int max8649_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
87 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
90 case REGULATOR_MODE_FAST
:
91 regmap_update_bits(info
->regmap
, rdev
->desc
->vsel_reg
,
92 MAX8649_FORCE_PWM
, MAX8649_FORCE_PWM
);
94 case REGULATOR_MODE_NORMAL
:
95 regmap_update_bits(info
->regmap
, rdev
->desc
->vsel_reg
,
96 MAX8649_FORCE_PWM
, 0);
104 static unsigned int max8649_get_mode(struct regulator_dev
*rdev
)
106 struct max8649_regulator_info
*info
= rdev_get_drvdata(rdev
);
110 ret
= regmap_read(info
->regmap
, rdev
->desc
->vsel_reg
, &val
);
113 if (val
& MAX8649_FORCE_PWM
)
114 return REGULATOR_MODE_FAST
;
115 return REGULATOR_MODE_NORMAL
;
118 static struct regulator_ops max8649_dcdc_ops
= {
119 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
120 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
121 .list_voltage
= regulator_list_voltage_linear
,
122 .map_voltage
= regulator_map_voltage_linear
,
123 .enable
= regulator_enable_regmap
,
124 .disable
= regulator_disable_regmap
,
125 .is_enabled
= regulator_is_enabled_regmap
,
126 .enable_time
= max8649_enable_time
,
127 .set_mode
= max8649_set_mode
,
128 .get_mode
= max8649_get_mode
,
132 static struct regulator_desc dcdc_desc
= {
134 .ops
= &max8649_dcdc_ops
,
135 .type
= REGULATOR_VOLTAGE
,
136 .n_voltages
= 1 << 6,
137 .owner
= THIS_MODULE
,
138 .vsel_mask
= MAX8649_VOL_MASK
,
139 .min_uV
= MAX8649_DCDC_VMIN
,
140 .uV_step
= MAX8649_DCDC_STEP
,
141 .enable_reg
= MAX8649_CONTROL
,
142 .enable_mask
= MAX8649_EN_PD
,
143 .enable_is_inverted
= true,
146 static struct regmap_config max8649_regmap_config
= {
151 static int max8649_regulator_probe(struct i2c_client
*client
,
152 const struct i2c_device_id
*id
)
154 struct max8649_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
155 struct max8649_regulator_info
*info
= NULL
;
156 struct regulator_dev
*regulator
;
157 struct regulator_config config
= { };
162 info
= devm_kzalloc(&client
->dev
, sizeof(struct max8649_regulator_info
),
167 info
->regmap
= devm_regmap_init_i2c(client
, &max8649_regmap_config
);
168 if (IS_ERR(info
->regmap
)) {
169 ret
= PTR_ERR(info
->regmap
);
170 dev_err(&client
->dev
, "Failed to allocate register map: %d\n", ret
);
174 info
->dev
= &client
->dev
;
175 i2c_set_clientdata(client
, info
);
177 info
->mode
= pdata
->mode
;
178 switch (info
->mode
) {
180 dcdc_desc
.vsel_reg
= MAX8649_MODE0
;
183 dcdc_desc
.vsel_reg
= MAX8649_MODE1
;
186 dcdc_desc
.vsel_reg
= MAX8649_MODE2
;
189 dcdc_desc
.vsel_reg
= MAX8649_MODE3
;
195 ret
= regmap_read(info
->regmap
, MAX8649_CHIP_ID1
, &val
);
197 dev_err(info
->dev
, "Failed to detect ID of MAX8649:%d\n",
201 dev_info(info
->dev
, "Detected MAX8649 (ID:%x)\n", val
);
203 /* enable VID0 & VID1 */
204 regmap_update_bits(info
->regmap
, MAX8649_CONTROL
, MAX8649_VID_MASK
, 0);
206 /* enable/disable external clock synchronization */
207 info
->extclk
= pdata
->extclk
;
208 data
= (info
->extclk
) ? MAX8649_SYNC_EXTCLK
: 0;
209 regmap_update_bits(info
->regmap
, dcdc_desc
.vsel_reg
,
210 MAX8649_SYNC_EXTCLK
, data
);
212 /* set external clock frequency */
213 info
->extclk_freq
= pdata
->extclk_freq
;
214 regmap_update_bits(info
->regmap
, MAX8649_SYNC
, MAX8649_EXT_MASK
,
215 info
->extclk_freq
<< 6);
218 if (pdata
->ramp_timing
) {
219 info
->ramp_timing
= pdata
->ramp_timing
;
220 regmap_update_bits(info
->regmap
, MAX8649_RAMP
, MAX8649_RAMP_MASK
,
221 info
->ramp_timing
<< 5);
224 info
->ramp_down
= pdata
->ramp_down
;
225 if (info
->ramp_down
) {
226 regmap_update_bits(info
->regmap
, MAX8649_RAMP
, MAX8649_RAMP_DOWN
,
230 config
.dev
= &client
->dev
;
231 config
.init_data
= pdata
->regulator
;
232 config
.driver_data
= info
;
233 config
.regmap
= info
->regmap
;
235 regulator
= devm_regulator_register(&client
->dev
, &dcdc_desc
,
237 if (IS_ERR(regulator
)) {
238 dev_err(info
->dev
, "failed to register regulator %s\n",
240 return PTR_ERR(regulator
);
246 static const struct i2c_device_id max8649_id
[] = {
250 MODULE_DEVICE_TABLE(i2c
, max8649_id
);
252 static struct i2c_driver max8649_driver
= {
253 .probe
= max8649_regulator_probe
,
257 .id_table
= max8649_id
,
260 static int __init
max8649_init(void)
262 return i2c_add_driver(&max8649_driver
);
264 subsys_initcall(max8649_init
);
266 static void __exit
max8649_exit(void)
268 i2c_del_driver(&max8649_driver
);
270 module_exit(max8649_exit
);
272 /* Module information */
273 MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
274 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
275 MODULE_LICENSE("GPL");