max8925-regulator.c 9.6 KB
Newer Older
H
Haojian Zhuang 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Regulators driver for Maxim max8925
 *
 * Copyright (C) 2009 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>
12
#include <linux/module.h>
H
Haojian Zhuang 已提交
13 14 15 16 17 18 19
#include <linux/init.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/mfd/max8925.h>
20 21
#include <linux/of.h>
#include <linux/regulator/of_regulator.h>
H
Haojian Zhuang 已提交
22 23 24 25 26 27 28

#define SD1_DVM_VMIN		850000
#define SD1_DVM_VMAX		1000000
#define SD1_DVM_STEP		50000
#define SD1_DVM_SHIFT		5		/* SDCTL1 bit5 */
#define SD1_DVM_EN		6		/* SDV1 bit 6 */

29 30 31 32 33 34 35
/* bit definitions in LDO control registers */
#define LDO_SEQ_I2C		0x7		/* Power U/D by i2c */
#define LDO_SEQ_MASK		0x7		/* Power U/D sequence mask */
#define LDO_SEQ_SHIFT		2		/* Power U/D sequence offset */
#define LDO_I2C_EN		0x1		/* Enable by i2c */
#define LDO_I2C_EN_MASK		0x1		/* Enable mask by i2c */
#define LDO_I2C_EN_SHIFT	0		/* Enable offset by i2c */
36

H
Haojian Zhuang 已提交
37 38 39 40 41 42 43 44 45 46
struct max8925_regulator_info {
	struct regulator_desc	desc;
	struct regulator_dev	*regulator;
	struct i2c_client	*i2c;
	struct max8925_chip	*chip;

	int	vol_reg;
	int	enable_reg;
};

47 48
static int max8925_set_voltage_sel(struct regulator_dev *rdev,
				   unsigned int selector)
H
Haojian Zhuang 已提交
49 50
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
51
	unsigned char mask = rdev->desc->n_voltages - 1;
H
Haojian Zhuang 已提交
52

53
	return max8925_set_bits(info->i2c, info->vol_reg, mask, selector);
H
Haojian Zhuang 已提交
54 55
}

56
static int max8925_get_voltage_sel(struct regulator_dev *rdev)
H
Haojian Zhuang 已提交
57 58 59 60 61 62 63 64
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
	unsigned char data, mask;
	int ret;

	ret = max8925_reg_read(info->i2c, info->vol_reg);
	if (ret < 0)
		return ret;
65 66
	mask = rdev->desc->n_voltages - 1;
	data = ret & mask;
H
Haojian Zhuang 已提交
67

68
	return data;
H
Haojian Zhuang 已提交
69 70 71 72 73 74 75
}

static int max8925_enable(struct regulator_dev *rdev)
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);

	return max8925_set_bits(info->i2c, info->enable_reg,
76 77 78 79
				LDO_SEQ_MASK << LDO_SEQ_SHIFT |
				LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
				LDO_SEQ_I2C << LDO_SEQ_SHIFT |
				LDO_I2C_EN << LDO_I2C_EN_SHIFT);
H
Haojian Zhuang 已提交
80 81 82 83 84 85 86
}

static int max8925_disable(struct regulator_dev *rdev)
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);

	return max8925_set_bits(info->i2c, info->enable_reg,
87 88 89
				LDO_SEQ_MASK << LDO_SEQ_SHIFT |
				LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
				LDO_SEQ_I2C << LDO_SEQ_SHIFT);
H
Haojian Zhuang 已提交
90 91 92 93 94
}

static int max8925_is_enabled(struct regulator_dev *rdev)
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
95
	int ldo_seq, ret;
H
Haojian Zhuang 已提交
96

97
	ret = max8925_reg_read(info->i2c, info->enable_reg);
H
Haojian Zhuang 已提交
98 99
	if (ret < 0)
		return ret;
100 101 102 103 104
	ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
	if (ldo_seq != LDO_SEQ_I2C)
		return 1;
	else
		return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
H
Haojian Zhuang 已提交
105 106 107 108 109 110 111 112 113 114
}

static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
	unsigned char data, mask;

	if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
		return -EINVAL;

115
	data = DIV_ROUND_UP(uV - SD1_DVM_VMIN, SD1_DVM_STEP);
H
Haojian Zhuang 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	data <<= SD1_DVM_SHIFT;
	mask = 3 << SD1_DVM_SHIFT;

	return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
}

static int max8925_set_dvm_enable(struct regulator_dev *rdev)
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);

	return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
				1 << SD1_DVM_EN);
}

static int max8925_set_dvm_disable(struct regulator_dev *rdev)
{
	struct max8925_regulator_info *info = rdev_get_drvdata(rdev);

	return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
}

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,
H
Haojian Zhuang 已提交
142 143 144 145 146 147 148 149 150
	.enable			= max8925_enable,
	.disable		= max8925_disable,
	.is_enabled		= max8925_is_enabled,
	.set_suspend_voltage	= max8925_set_dvm_voltage,
	.set_suspend_enable	= max8925_set_dvm_enable,
	.set_suspend_disable	= max8925_set_dvm_disable,
};

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,
H
Haojian Zhuang 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
	.enable			= max8925_enable,
	.disable		= max8925_disable,
	.is_enabled		= max8925_is_enabled,
};

#define MAX8925_SDV(_id, min, max, step)			\
{								\
	.desc	= {						\
		.name	= "SDV" #_id,				\
		.ops	= &max8925_regulator_sdv_ops,		\
		.type	= REGULATOR_VOLTAGE,			\
		.id	= MAX8925_ID_SD##_id,			\
		.owner	= THIS_MODULE,				\
168
		.n_voltages = 64,				\
169 170
		.min_uV = min * 1000,				\
		.uV_step = step * 1000,				\
H
Haojian Zhuang 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
	},							\
	.vol_reg	= MAX8925_SDV##_id,			\
	.enable_reg	= MAX8925_SDCTL##_id,			\
}

#define MAX8925_LDO(_id, min, max, step)			\
{								\
	.desc	= {						\
		.name	= "LDO" #_id,				\
		.ops	= &max8925_regulator_ldo_ops,		\
		.type	= REGULATOR_VOLTAGE,			\
		.id	= MAX8925_ID_LDO##_id,			\
		.owner	= THIS_MODULE,				\
184
		.n_voltages = 64,				\
185 186
		.min_uV = min * 1000,				\
		.uV_step = step * 1000,				\
H
Haojian Zhuang 已提交
187 188 189 190 191
	},							\
	.vol_reg	= MAX8925_LDOVOUT##_id,			\
	.enable_reg	= MAX8925_LDOCTL##_id,			\
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
#ifdef CONFIG_OF
static struct of_regulator_match max8925_regulator_matches[] = {
	{ .name	= "SDV1",},
	{ .name = "SDV2",},
	{ .name = "SDV3",},
	{ .name = "LDO1",},
	{ .name = "LDO2",},
	{ .name = "LDO3",},
	{ .name = "LDO4",},
	{ .name = "LDO5",},
	{ .name = "LDO6",},
	{ .name = "LDO7",},
	{ .name = "LDO8",},
	{ .name = "LDO9",},
	{ .name = "LDO10",},
	{ .name = "LDO11",},
	{ .name = "LDO12",},
	{ .name = "LDO13",},
	{ .name = "LDO14",},
	{ .name = "LDO15",},
	{ .name = "LDO16",},
	{ .name = "LDO17",},
	{ .name = "LDO18",},
	{ .name = "LDO19",},
	{ .name = "LDO20",},
};
#endif

H
Haojian Zhuang 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
static struct max8925_regulator_info max8925_regulator_info[] = {
	MAX8925_SDV(1, 637.5, 1425, 12.5),
	MAX8925_SDV(2,   650, 2225,   25),
	MAX8925_SDV(3,   750, 3900,   50),

	MAX8925_LDO(1,  750, 3900, 50),
	MAX8925_LDO(2,  650, 2250, 25),
	MAX8925_LDO(3,  650, 2250, 25),
	MAX8925_LDO(4,  750, 3900, 50),
	MAX8925_LDO(5,  750, 3900, 50),
	MAX8925_LDO(6,  750, 3900, 50),
	MAX8925_LDO(7,  750, 3900, 50),
	MAX8925_LDO(8,  750, 3900, 50),
	MAX8925_LDO(9,  750, 3900, 50),
	MAX8925_LDO(10, 750, 3900, 50),
	MAX8925_LDO(11, 750, 3900, 50),
	MAX8925_LDO(12, 750, 3900, 50),
	MAX8925_LDO(13, 750, 3900, 50),
	MAX8925_LDO(14, 750, 3900, 50),
	MAX8925_LDO(15, 750, 3900, 50),
	MAX8925_LDO(16, 750, 3900, 50),
	MAX8925_LDO(17, 650, 2250, 25),
	MAX8925_LDO(18, 650, 2250, 25),
	MAX8925_LDO(19, 750, 3900, 50),
	MAX8925_LDO(20, 750, 3900, 50),
};

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 276
#ifdef CONFIG_OF
static int max8925_regulator_dt_init(struct platform_device *pdev,
				    struct max8925_regulator_info *info,
				    struct regulator_config *config,
				    int ridx)
{
	struct device_node *nproot, *np;
	int rcount;
	nproot = pdev->dev.parent->of_node;
	if (!nproot)
		return -ENODEV;
	np = of_find_node_by_name(nproot, "regulators");
	if (!np) {
		dev_err(&pdev->dev, "failed to find regulators node\n");
		return -ENODEV;
	}

	rcount = of_regulator_match(&pdev->dev, np,
				&max8925_regulator_matches[ridx], 1);
	if (rcount < 0)
		return -ENODEV;
	config->init_data =	max8925_regulator_matches[ridx].init_data;
	config->of_node = max8925_regulator_matches[ridx].of_node;

	return 0;
}
#else
#define max8925_regulator_dt_init(w, x, y, z)	(-1)
#endif

H
Haojian Zhuang 已提交
277 278 279
static int __devinit max8925_regulator_probe(struct platform_device *pdev)
{
	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;
H
Haojian Zhuang 已提交
284
	struct regulator_dev *rdev;
285
	int i, regulator_idx;
H
Haojian Zhuang 已提交
286

287 288 289 290 291 292 293
	res = platform_get_resource(pdev, IORESOURCE_REG, 0);
	if (!res) {
		dev_err(&pdev->dev, "No REG resource!\n");
		return -EINVAL;
	}
	for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
		ri = &max8925_regulator_info[i];
294 295
		if (ri->vol_reg == res->start) {
			regulator_idx = i;
296
			break;
297
		}
298
	}
299

300 301 302
	if (i == ARRAY_SIZE(max8925_regulator_info)) {
		dev_err(&pdev->dev, "Failed to find regulator %llu\n",
			(unsigned long long)res->start);
H
Haojian Zhuang 已提交
303 304 305 306 307
		return -EINVAL;
	}
	ri->i2c = chip->i2c;
	ri->chip = chip;

308 309 310
	config.dev = &pdev->dev;
	config.driver_data = ri;

311 312 313 314
	if (max8925_regulator_dt_init(pdev, ri, &config, regulator_idx))
		if (pdata)
			config.init_data = pdata;

315
	rdev = regulator_register(&ri->desc, &config);
H
Haojian Zhuang 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328 329
	if (IS_ERR(rdev)) {
		dev_err(&pdev->dev, "failed to register regulator %s\n",
				ri->desc.name);
		return PTR_ERR(rdev);
	}

	platform_set_drvdata(pdev, rdev);
	return 0;
}

static int __devexit max8925_regulator_remove(struct platform_device *pdev)
{
	struct regulator_dev *rdev = platform_get_drvdata(pdev);

330
	platform_set_drvdata(pdev, NULL);
H
Haojian Zhuang 已提交
331
	regulator_unregister(rdev);
332

H
Haojian Zhuang 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	return 0;
}

static struct platform_driver max8925_regulator_driver = {
	.driver		= {
		.name	= "max8925-regulator",
		.owner	= THIS_MODULE,
	},
	.probe		= max8925_regulator_probe,
	.remove		= __devexit_p(max8925_regulator_remove),
};

static int __init max8925_regulator_init(void)
{
	return platform_driver_register(&max8925_regulator_driver);
}
subsys_initcall(max8925_regulator_init);

static void __exit max8925_regulator_exit(void)
{
	platform_driver_unregister(&max8925_regulator_driver);
}
module_exit(max8925_regulator_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
MODULE_ALIAS("platform:max8925-regulator");