isl6271a-regulator.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * isl6271a-regulator.c
 *
 * Support for Intersil ISL6271A voltage regulator
 *
 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 * whether express or implied; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/i2c.h>
#include <linux/slab.h>

#define	ISL6271A_VOLTAGE_MIN	850000
#define	ISL6271A_VOLTAGE_MAX	1600000
#define	ISL6271A_VOLTAGE_STEP	50000

/* PMIC details */
struct isl_pmic {
	struct i2c_client	*client;
	struct regulator_dev	*rdev[3];
	struct mutex		mtx;
};

38
static int isl6271a_get_voltage_sel(struct regulator_dev *dev)
39 40
{
	struct isl_pmic *pmic = rdev_get_drvdata(dev);
41
	int idx;
42 43 44 45

	mutex_lock(&pmic->mtx);

	idx = i2c_smbus_read_byte(pmic->client);
46
	if (idx < 0)
47 48 49
		dev_err(&pmic->client->dev, "Error getting voltage\n");

	mutex_unlock(&pmic->mtx);
50
	return idx;
51 52
}

53 54
static int isl6271a_set_voltage_sel(struct regulator_dev *dev,
				    unsigned selector)
55 56
{
	struct isl_pmic *pmic = rdev_get_drvdata(dev);
57
	int err;
58

59 60
	mutex_lock(&pmic->mtx);

61
	err = i2c_smbus_write_byte(pmic->client, selector);
62 63 64 65 66 67 68 69
	if (err < 0)
		dev_err(&pmic->client->dev, "Error setting voltage\n");

	mutex_unlock(&pmic->mtx);
	return err;
}

static struct regulator_ops isl_core_ops = {
70
	.get_voltage_sel = isl6271a_get_voltage_sel,
71
	.set_voltage_sel = isl6271a_set_voltage_sel,
72
	.list_voltage	= regulator_list_voltage_linear,
73
	.map_voltage	= regulator_map_voltage_linear,
74 75 76
};

static struct regulator_ops isl_fixed_ops = {
77
	.list_voltage	= regulator_list_voltage_linear,
78 79
};

80
static const struct regulator_desc isl_rd[] = {
81 82 83 84 85 86 87
	{
		.name		= "Core Buck",
		.id		= 0,
		.n_voltages	= 16,
		.ops		= &isl_core_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
88 89
		.min_uV		= ISL6271A_VOLTAGE_MIN,
		.uV_step	= ISL6271A_VOLTAGE_STEP,
90 91 92 93 94 95 96
	}, {
		.name		= "LDO1",
		.id		= 1,
		.n_voltages	= 1,
		.ops		= &isl_fixed_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
97
		.min_uV		= 1100000,
98 99 100 101 102 103 104
	}, {
		.name		= "LDO2",
		.id		= 2,
		.n_voltages	= 1,
		.ops		= &isl_fixed_ops,
		.type		= REGULATOR_VOLTAGE,
		.owner		= THIS_MODULE,
105
		.min_uV		= 1300000,
106 107 108
	},
};

109
static int isl6271a_probe(struct i2c_client *i2c,
110 111
				     const struct i2c_device_id *id)
{
112
	struct regulator_config config = { };
J
Jingoo Han 已提交
113
	struct regulator_init_data *init_data	= dev_get_platdata(&i2c->dev);
114 115 116 117 118 119
	struct isl_pmic *pmic;
	int err, i;

	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

120
	pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
121 122 123 124 125 126 127 128
	if (!pmic)
		return -ENOMEM;

	pmic->client = i2c;

	mutex_init(&pmic->mtx);

	for (i = 0; i < 3; i++) {
129 130 131 132
		config.dev = &i2c->dev;
		if (i == 0)
			config.init_data = init_data;
		else
133
			config.init_data = NULL;
134 135 136
		config.driver_data = pmic;

		pmic->rdev[i] = regulator_register(&isl_rd[i], &config);
137 138
		if (IS_ERR(pmic->rdev[i])) {
			dev_err(&i2c->dev, "failed to register %s\n", id->name);
139
			err = PTR_ERR(pmic->rdev[i]);
140 141 142 143 144 145 146 147 148 149 150 151 152 153
			goto error;
		}
	}

	i2c_set_clientdata(i2c, pmic);

	return 0;

error:
	while (--i >= 0)
		regulator_unregister(pmic->rdev[i]);
	return err;
}

154
static int isl6271a_remove(struct i2c_client *i2c)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
{
	struct isl_pmic *pmic = i2c_get_clientdata(i2c);
	int i;

	for (i = 0; i < 3; i++)
		regulator_unregister(pmic->rdev[i]);
	return 0;
}

static const struct i2c_device_id isl6271a_id[] = {
	{.name = "isl6271a", 0 },
	{ },
};

MODULE_DEVICE_TABLE(i2c, isl6271a_id);

static struct i2c_driver isl6271a_i2c_driver = {
	.driver = {
		.name = "isl6271a",
		.owner = THIS_MODULE,
	},
	.probe = isl6271a_probe,
177
	.remove = isl6271a_remove,
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	.id_table = isl6271a_id,
};

static int __init isl6271a_init(void)
{
	return i2c_add_driver(&isl6271a_i2c_driver);
}

static void __exit isl6271a_cleanup(void)
{
	i2c_del_driver(&isl6271a_i2c_driver);
}

subsys_initcall(isl6271a_init);
module_exit(isl6271a_cleanup);

MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
MODULE_LICENSE("GPL v2");