regulator: isl6271a: Convert to get_voltage_sel
[deliverable/linux.git] / drivers / regulator / isl6271a-regulator.c
1 /*
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>
25 #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 */
32 struct isl_pmic {
33 struct i2c_client *client;
34 struct regulator_dev *rdev[3];
35 struct mutex mtx;
36 };
37
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 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
49 mutex_unlock(&pmic->mtx);
50 return idx;
51 }
52
53 static int isl6271a_set_voltage(struct regulator_dev *dev,
54 int minuV, int maxuV,
55 unsigned *selector)
56 {
57 struct isl_pmic *pmic = rdev_get_drvdata(dev);
58 int err, data;
59
60 if (minuV < ISL6271A_VOLTAGE_MIN || minuV > ISL6271A_VOLTAGE_MAX)
61 return -EINVAL;
62 if (maxuV < ISL6271A_VOLTAGE_MIN || maxuV > ISL6271A_VOLTAGE_MAX)
63 return -EINVAL;
64
65 data = DIV_ROUND_UP(minuV - ISL6271A_VOLTAGE_MIN,
66 ISL6271A_VOLTAGE_STEP);
67 *selector = data;
68
69 mutex_lock(&pmic->mtx);
70
71 err = i2c_smbus_write_byte(pmic->client, data);
72 if (err < 0)
73 dev_err(&pmic->client->dev, "Error setting voltage\n");
74
75 mutex_unlock(&pmic->mtx);
76 return err;
77 }
78
79 static struct regulator_ops isl_core_ops = {
80 .get_voltage_sel = isl6271a_get_voltage_sel,
81 .set_voltage = isl6271a_set_voltage,
82 .list_voltage = regulator_list_voltage_linear,
83 };
84
85 static int isl6271a_get_fixed_voltage(struct regulator_dev *dev)
86 {
87 int id = rdev_get_id(dev);
88 return (id == 1) ? 1100000 : 1300000;
89 }
90
91 static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector)
92 {
93 int id = rdev_get_id(dev);
94 return (id == 1) ? 1100000 : 1300000;
95 }
96
97 static struct regulator_ops isl_fixed_ops = {
98 .get_voltage = isl6271a_get_fixed_voltage,
99 .list_voltage = isl6271a_list_fixed_voltage,
100 };
101
102 static const struct regulator_desc isl_rd[] = {
103 {
104 .name = "Core Buck",
105 .id = 0,
106 .n_voltages = 16,
107 .ops = &isl_core_ops,
108 .type = REGULATOR_VOLTAGE,
109 .owner = THIS_MODULE,
110 .min_uV = ISL6271A_VOLTAGE_MIN,
111 .uV_step = ISL6271A_VOLTAGE_STEP,
112 }, {
113 .name = "LDO1",
114 .id = 1,
115 .n_voltages = 1,
116 .ops = &isl_fixed_ops,
117 .type = REGULATOR_VOLTAGE,
118 .owner = THIS_MODULE,
119 }, {
120 .name = "LDO2",
121 .id = 2,
122 .n_voltages = 1,
123 .ops = &isl_fixed_ops,
124 .type = REGULATOR_VOLTAGE,
125 .owner = THIS_MODULE,
126 },
127 };
128
129 static int __devinit isl6271a_probe(struct i2c_client *i2c,
130 const struct i2c_device_id *id)
131 {
132 struct regulator_config config = { };
133 struct regulator_init_data *init_data = i2c->dev.platform_data;
134 struct isl_pmic *pmic;
135 int err, i;
136
137 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
138 return -EIO;
139
140 pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
141 if (!pmic)
142 return -ENOMEM;
143
144 pmic->client = i2c;
145
146 mutex_init(&pmic->mtx);
147
148 for (i = 0; i < 3; i++) {
149 config.dev = &i2c->dev;
150 if (i == 0)
151 config.init_data = init_data;
152 else
153 config.init_data = 0;
154 config.driver_data = pmic;
155
156 pmic->rdev[i] = regulator_register(&isl_rd[i], &config);
157 if (IS_ERR(pmic->rdev[i])) {
158 dev_err(&i2c->dev, "failed to register %s\n", id->name);
159 err = PTR_ERR(pmic->rdev[i]);
160 goto error;
161 }
162 }
163
164 i2c_set_clientdata(i2c, pmic);
165
166 return 0;
167
168 error:
169 while (--i >= 0)
170 regulator_unregister(pmic->rdev[i]);
171 return err;
172 }
173
174 static int __devexit isl6271a_remove(struct i2c_client *i2c)
175 {
176 struct isl_pmic *pmic = i2c_get_clientdata(i2c);
177 int i;
178
179 for (i = 0; i < 3; i++)
180 regulator_unregister(pmic->rdev[i]);
181 return 0;
182 }
183
184 static const struct i2c_device_id isl6271a_id[] = {
185 {.name = "isl6271a", 0 },
186 { },
187 };
188
189 MODULE_DEVICE_TABLE(i2c, isl6271a_id);
190
191 static struct i2c_driver isl6271a_i2c_driver = {
192 .driver = {
193 .name = "isl6271a",
194 .owner = THIS_MODULE,
195 },
196 .probe = isl6271a_probe,
197 .remove = __devexit_p(isl6271a_remove),
198 .id_table = isl6271a_id,
199 };
200
201 static int __init isl6271a_init(void)
202 {
203 return i2c_add_driver(&isl6271a_i2c_driver);
204 }
205
206 static void __exit isl6271a_cleanup(void)
207 {
208 i2c_del_driver(&isl6271a_i2c_driver);
209 }
210
211 subsys_initcall(isl6271a_init);
212 module_exit(isl6271a_cleanup);
213
214 MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
215 MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
216 MODULE_LICENSE("GPL v2");
This page took 0.045025 seconds and 5 git commands to generate.