blob: 78cdc90e0a7ea88ecccca4d6069fa55d7dc3f253 [file] [log] [blame]
Marek Vasut51bd6942010-06-13 17:25:51 +02001/*
2 * isl6271a-regulator.c
3 *
4 * Support for Intersil ISL6271A voltage regulator
5 *
6 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2.
11 *
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
13 * whether express or implied; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/err.h>
22#include <linux/platform_device.h>
23#include <linux/regulator/driver.h>
24#include <linux/i2c.h>
Marek Vasut51bd6942010-06-13 17:25:51 +020025#include <linux/slab.h>
26
27#define ISL6271A_VOLTAGE_MIN 850000
28#define ISL6271A_VOLTAGE_MAX 1600000
29#define ISL6271A_VOLTAGE_STEP 50000
30
31/* PMIC details */
32struct isl_pmic {
33 struct i2c_client *client;
34 struct regulator_dev *rdev[3];
35 struct mutex mtx;
36};
37
38static int isl6271a_get_voltage(struct regulator_dev *dev)
39{
40 struct isl_pmic *pmic = rdev_get_drvdata(dev);
41 int idx, data;
42
43 mutex_lock(&pmic->mtx);
44
45 idx = i2c_smbus_read_byte(pmic->client);
46 if (idx < 0) {
47 dev_err(&pmic->client->dev, "Error getting voltage\n");
48 data = idx;
49 goto out;
50 }
51
52 /* Convert the data from chip to microvolts */
53 data = ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * (idx & 0xf));
54
55out:
56 mutex_unlock(&pmic->mtx);
57 return data;
58}
59
Mark Brown3a93f2a2010-11-10 14:38:29 +000060static int isl6271a_set_voltage(struct regulator_dev *dev,
61 int minuV, int maxuV,
62 unsigned *selector)
Marek Vasut51bd6942010-06-13 17:25:51 +020063{
64 struct isl_pmic *pmic = rdev_get_drvdata(dev);
Axel Lin48ee1162012-03-02 09:19:02 +080065 int err, data;
Marek Vasut51bd6942010-06-13 17:25:51 +020066
67 if (minuV < ISL6271A_VOLTAGE_MIN || minuV > ISL6271A_VOLTAGE_MAX)
68 return -EINVAL;
69 if (maxuV < ISL6271A_VOLTAGE_MIN || maxuV > ISL6271A_VOLTAGE_MAX)
70 return -EINVAL;
71
Axel Lin48ee1162012-03-02 09:19:02 +080072 data = DIV_ROUND_UP(minuV - ISL6271A_VOLTAGE_MIN,
73 ISL6271A_VOLTAGE_STEP);
Mark Brown3a93f2a2010-11-10 14:38:29 +000074 *selector = data;
75
Marek Vasut51bd6942010-06-13 17:25:51 +020076 mutex_lock(&pmic->mtx);
77
78 err = i2c_smbus_write_byte(pmic->client, data);
79 if (err < 0)
80 dev_err(&pmic->client->dev, "Error setting voltage\n");
81
82 mutex_unlock(&pmic->mtx);
83 return err;
84}
85
Marek Vasut51bd6942010-06-13 17:25:51 +020086static struct regulator_ops isl_core_ops = {
87 .get_voltage = isl6271a_get_voltage,
88 .set_voltage = isl6271a_set_voltage,
Axel Lin2b7a7a42012-05-16 10:09:27 +080089 .list_voltage = regulator_list_voltage_linear,
Marek Vasut51bd6942010-06-13 17:25:51 +020090};
91
92static int isl6271a_get_fixed_voltage(struct regulator_dev *dev)
93{
94 int id = rdev_get_id(dev);
95 return (id == 1) ? 1100000 : 1300000;
96}
97
98static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector)
99{
100 int id = rdev_get_id(dev);
101 return (id == 1) ? 1100000 : 1300000;
102}
103
104static struct regulator_ops isl_fixed_ops = {
105 .get_voltage = isl6271a_get_fixed_voltage,
106 .list_voltage = isl6271a_list_fixed_voltage,
107};
108
Axel Lin4b65e152012-04-05 11:58:06 +0800109static const struct regulator_desc isl_rd[] = {
Marek Vasut51bd6942010-06-13 17:25:51 +0200110 {
111 .name = "Core Buck",
112 .id = 0,
113 .n_voltages = 16,
114 .ops = &isl_core_ops,
115 .type = REGULATOR_VOLTAGE,
116 .owner = THIS_MODULE,
Axel Lin2b7a7a42012-05-16 10:09:27 +0800117 .min_uV = ISL6271A_VOLTAGE_MIN,
118 .uV_step = ISL6271A_VOLTAGE_STEP,
Marek Vasut51bd6942010-06-13 17:25:51 +0200119 }, {
120 .name = "LDO1",
121 .id = 1,
122 .n_voltages = 1,
123 .ops = &isl_fixed_ops,
124 .type = REGULATOR_VOLTAGE,
125 .owner = THIS_MODULE,
126 }, {
127 .name = "LDO2",
128 .id = 2,
129 .n_voltages = 1,
130 .ops = &isl_fixed_ops,
131 .type = REGULATOR_VOLTAGE,
132 .owner = THIS_MODULE,
133 },
134};
135
136static int __devinit isl6271a_probe(struct i2c_client *i2c,
137 const struct i2c_device_id *id)
138{
Mark Brownc1727082012-04-04 00:50:22 +0100139 struct regulator_config config = { };
Marek Vasut51bd6942010-06-13 17:25:51 +0200140 struct regulator_init_data *init_data = i2c->dev.platform_data;
141 struct isl_pmic *pmic;
142 int err, i;
143
144 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
145 return -EIO;
146
Axel Linef6bd5a2012-04-11 23:05:49 +0800147 pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
Marek Vasut51bd6942010-06-13 17:25:51 +0200148 if (!pmic)
149 return -ENOMEM;
150
151 pmic->client = i2c;
152
153 mutex_init(&pmic->mtx);
154
155 for (i = 0; i < 3; i++) {
Mark Brownc1727082012-04-04 00:50:22 +0100156 config.dev = &i2c->dev;
157 if (i == 0)
158 config.init_data = init_data;
159 else
160 config.init_data = 0;
161 config.driver_data = pmic;
162
163 pmic->rdev[i] = regulator_register(&isl_rd[i], &config);
Marek Vasut51bd6942010-06-13 17:25:51 +0200164 if (IS_ERR(pmic->rdev[i])) {
165 dev_err(&i2c->dev, "failed to register %s\n", id->name);
roel kluinfa63bd42010-12-31 16:26:47 +0100166 err = PTR_ERR(pmic->rdev[i]);
Marek Vasut51bd6942010-06-13 17:25:51 +0200167 goto error;
168 }
169 }
170
171 i2c_set_clientdata(i2c, pmic);
172
173 return 0;
174
175error:
176 while (--i >= 0)
177 regulator_unregister(pmic->rdev[i]);
Marek Vasut51bd6942010-06-13 17:25:51 +0200178 return err;
179}
180
181static int __devexit isl6271a_remove(struct i2c_client *i2c)
182{
183 struct isl_pmic *pmic = i2c_get_clientdata(i2c);
184 int i;
185
Marek Vasut51bd6942010-06-13 17:25:51 +0200186 for (i = 0; i < 3; i++)
187 regulator_unregister(pmic->rdev[i]);
Marek Vasut51bd6942010-06-13 17:25:51 +0200188 return 0;
189}
190
191static const struct i2c_device_id isl6271a_id[] = {
192 {.name = "isl6271a", 0 },
193 { },
194};
195
196MODULE_DEVICE_TABLE(i2c, isl6271a_id);
197
198static struct i2c_driver isl6271a_i2c_driver = {
199 .driver = {
200 .name = "isl6271a",
201 .owner = THIS_MODULE,
202 },
203 .probe = isl6271a_probe,
204 .remove = __devexit_p(isl6271a_remove),
205 .id_table = isl6271a_id,
206};
207
208static int __init isl6271a_init(void)
209{
210 return i2c_add_driver(&isl6271a_i2c_driver);
211}
212
213static void __exit isl6271a_cleanup(void)
214{
215 i2c_del_driver(&isl6271a_i2c_driver);
216}
217
218subsys_initcall(isl6271a_init);
219module_exit(isl6271a_cleanup);
220
221MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
222MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
223MODULE_LICENSE("GPL v2");