max8649.c 6.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Regulators driver for Maxim max8649
 *
 * Copyright (C) 2009-2010 Marvell International Ltd.
 *      Haojian Zhuang <haojian.zhuang@marvell.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
17
#include <linux/slab.h>
18
#include <linux/regulator/max8649.h>
19
#include <linux/regmap.h>
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#define MAX8649_DCDC_VMIN	750000		/* uV */
#define MAX8649_DCDC_VMAX	1380000		/* uV */
#define MAX8649_DCDC_STEP	10000		/* uV */
#define MAX8649_VOL_MASK	0x3f

/* Registers */
#define MAX8649_MODE0		0x00
#define MAX8649_MODE1		0x01
#define MAX8649_MODE2		0x02
#define MAX8649_MODE3		0x03
#define MAX8649_CONTROL		0x04
#define MAX8649_SYNC		0x05
#define MAX8649_RAMP		0x06
#define MAX8649_CHIP_ID1	0x08
#define MAX8649_CHIP_ID2	0x09

/* Bits */
#define MAX8649_EN_PD		(1 << 7)
#define MAX8649_VID0_PD		(1 << 6)
#define MAX8649_VID1_PD		(1 << 5)
#define MAX8649_VID_MASK	(3 << 5)

#define MAX8649_FORCE_PWM	(1 << 7)
#define MAX8649_SYNC_EXTCLK	(1 << 6)

#define MAX8649_EXT_MASK	(3 << 6)

#define MAX8649_RAMP_MASK	(7 << 5)
#define MAX8649_RAMP_DOWN	(1 << 1)

struct max8649_regulator_info {
	struct device		*dev;
53
	struct regmap		*regmap;
54 55 56 57 58 59 60 61 62 63 64 65

	unsigned	mode:2;	/* bit[1:0] = VID1, VID0 */
	unsigned	extclk_freq:2;
	unsigned	extclk:1;
	unsigned	ramp_timing:3;
	unsigned	ramp_down:1;
};

static int max8649_enable_time(struct regulator_dev *rdev)
{
	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
	int voltage, rate, ret;
66
	unsigned int val;
67 68

	/* get voltage */
69
	ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
70
	if (ret != 0)
71
		return ret;
72
	val &= MAX8649_VOL_MASK;
73
	voltage = regulator_list_voltage_linear(rdev, (unsigned char)val);
74 75

	/* get rate */
76 77
	ret = regmap_read(info->regmap, MAX8649_RAMP, &val);
	if (ret != 0)
78
		return ret;
79
	ret = (val & MAX8649_RAMP_MASK) >> 5;
80 81
	rate = (32 * 1000) >> ret;	/* uV/uS */

82
	return DIV_ROUND_UP(voltage, rate);
83 84 85 86 87 88 89 90
}

static int max8649_set_mode(struct regulator_dev *rdev, unsigned int mode)
{
	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);

	switch (mode) {
	case REGULATOR_MODE_FAST:
91 92
		regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
				   MAX8649_FORCE_PWM, MAX8649_FORCE_PWM);
93 94
		break;
	case REGULATOR_MODE_NORMAL:
95
		regmap_update_bits(info->regmap, rdev->desc->vsel_reg,
96
				   MAX8649_FORCE_PWM, 0);
97 98 99 100 101 102 103 104 105 106
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

static unsigned int max8649_get_mode(struct regulator_dev *rdev)
{
	struct max8649_regulator_info *info = rdev_get_drvdata(rdev);
107
	unsigned int val;
108 109
	int ret;

110
	ret = regmap_read(info->regmap, rdev->desc->vsel_reg, &val);
111 112 113
	if (ret != 0)
		return ret;
	if (val & MAX8649_FORCE_PWM)
114 115 116 117
		return REGULATOR_MODE_FAST;
	return REGULATOR_MODE_NORMAL;
}

118
static const 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 124 125
	.enable		= regulator_enable_regmap,
	.disable	= regulator_disable_regmap,
	.is_enabled	= regulator_is_enabled_regmap,
126 127 128 129 130 131
	.enable_time	= max8649_enable_time,
	.set_mode	= max8649_set_mode,
	.get_mode	= max8649_get_mode,

};

132
static struct regulator_desc dcdc_desc = {
133 134 135 136 137
	.name		= "max8649",
	.ops		= &max8649_dcdc_ops,
	.type		= REGULATOR_VOLTAGE,
	.n_voltages	= 1 << 6,
	.owner		= THIS_MODULE,
138
	.vsel_mask	= MAX8649_VOL_MASK,
139 140
	.min_uV		= MAX8649_DCDC_VMIN,
	.uV_step	= MAX8649_DCDC_STEP,
141 142 143
	.enable_reg	= MAX8649_CONTROL,
	.enable_mask	= MAX8649_EN_PD,
	.enable_is_inverted = true,
144 145
};

146
static const struct regmap_config max8649_regmap_config = {
147 148 149 150
	.reg_bits = 8,
	.val_bits = 8,
};

151
static int max8649_regulator_probe(struct i2c_client *client,
152 153
					     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 = { };
158
	unsigned int val;
159 160 161
	unsigned char data;
	int ret;

162 163
	info = devm_kzalloc(&client->dev, sizeof(struct max8649_regulator_info),
			    GFP_KERNEL);
164
	if (!info)
165 166
		return -ENOMEM;

167
	info->regmap = devm_regmap_init_i2c(client, &max8649_regmap_config);
168 169 170
	if (IS_ERR(info->regmap)) {
		ret = PTR_ERR(info->regmap);
		dev_err(&client->dev, "Failed to allocate register map: %d\n", ret);
171
		return ret;
172 173
	}

174 175 176 177 178 179
	info->dev = &client->dev;
	i2c_set_clientdata(client, info);

	info->mode = pdata->mode;
	switch (info->mode) {
	case 0:
180
		dcdc_desc.vsel_reg = MAX8649_MODE0;
181 182
		break;
	case 1:
183
		dcdc_desc.vsel_reg = MAX8649_MODE1;
184 185
		break;
	case 2:
186
		dcdc_desc.vsel_reg = MAX8649_MODE2;
187 188
		break;
	case 3:
189
		dcdc_desc.vsel_reg = MAX8649_MODE3;
190 191 192 193 194
		break;
	default:
		break;
	}

195 196
	ret = regmap_read(info->regmap, MAX8649_CHIP_ID1, &val);
	if (ret != 0) {
197 198
		dev_err(info->dev, "Failed to detect ID of MAX8649:%d\n",
			ret);
199
		return ret;
200
	}
201
	dev_info(info->dev, "Detected MAX8649 (ID:%x)\n", val);
202 203

	/* enable VID0 & VID1 */
204
	regmap_update_bits(info->regmap, MAX8649_CONTROL, MAX8649_VID_MASK, 0);
205 206 207 208

	/* enable/disable external clock synchronization */
	info->extclk = pdata->extclk;
	data = (info->extclk) ? MAX8649_SYNC_EXTCLK : 0;
209 210
	regmap_update_bits(info->regmap, dcdc_desc.vsel_reg,
			   MAX8649_SYNC_EXTCLK, data);
211 212 213
	if (info->extclk) {
		/* set external clock frequency */
		info->extclk_freq = pdata->extclk_freq;
214 215
		regmap_update_bits(info->regmap, MAX8649_SYNC, MAX8649_EXT_MASK,
				   info->extclk_freq << 6);
216 217 218 219
	}

	if (pdata->ramp_timing) {
		info->ramp_timing = pdata->ramp_timing;
220 221
		regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_MASK,
				   info->ramp_timing << 5);
222 223 224 225
	}

	info->ramp_down = pdata->ramp_down;
	if (info->ramp_down) {
226 227
		regmap_update_bits(info->regmap, MAX8649_RAMP, MAX8649_RAMP_DOWN,
				   MAX8649_RAMP_DOWN);
228 229
	}

230 231 232
	config.dev = &client->dev;
	config.init_data = pdata->regulator;
	config.driver_data = info;
233
	config.regmap = info->regmap;
234

235
	regulator = devm_regulator_register(&client->dev, &dcdc_desc,
236
						  &config);
237
	if (IS_ERR(regulator)) {
238 239
		dev_err(info->dev, "failed to register regulator %s\n",
			dcdc_desc.name);
240
		return PTR_ERR(regulator);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	}

	return 0;
}

static const struct i2c_device_id max8649_id[] = {
	{ "max8649", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, max8649_id);

static struct i2c_driver max8649_driver = {
	.probe		= max8649_regulator_probe,
	.driver		= {
		.name	= "max8649",
	},
	.id_table	= max8649_id,
};

static int __init max8649_init(void)
{
	return i2c_add_driver(&max8649_driver);
}
subsys_initcall(max8649_init);

static void __exit max8649_exit(void)
{
	i2c_del_driver(&max8649_driver);
}
module_exit(max8649_exit);

/* Module information */
MODULE_DESCRIPTION("MAXIM 8649 voltage regulator driver");
MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
MODULE_LICENSE("GPL");