Commit | Line | Data |
---|---|---|
202f4f53 MH |
1 | /* |
2 | * max8952.c - Voltage and current regulation for the Maxim 8952 | |
3 | * | |
4 | * Copyright (C) 2010 Samsung Electronics | |
5 | * MyungJoo Ham <myungjoo.ham@samsung.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include <linux/module.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/i2c.h> | |
25 | #include <linux/err.h> | |
26 | #include <linux/platform_device.h> | |
27 | #include <linux/regulator/driver.h> | |
28 | #include <linux/regulator/max8952.h> | |
202f4f53 MH |
29 | #include <linux/gpio.h> |
30 | #include <linux/io.h> | |
31 | #include <linux/slab.h> | |
32 | ||
33 | /* Registers */ | |
34 | enum { | |
35 | MAX8952_REG_MODE0, | |
36 | MAX8952_REG_MODE1, | |
37 | MAX8952_REG_MODE2, | |
38 | MAX8952_REG_MODE3, | |
39 | MAX8952_REG_CONTROL, | |
40 | MAX8952_REG_SYNC, | |
41 | MAX8952_REG_RAMP, | |
42 | MAX8952_REG_CHIP_ID1, | |
43 | MAX8952_REG_CHIP_ID2, | |
44 | }; | |
45 | ||
46 | struct max8952_data { | |
47 | struct i2c_client *client; | |
48 | struct device *dev; | |
202f4f53 MH |
49 | struct max8952_platform_data *pdata; |
50 | struct regulator_dev *rdev; | |
51 | ||
52 | bool vid0; | |
53 | bool vid1; | |
54 | bool en; | |
55 | }; | |
56 | ||
57 | static int max8952_read_reg(struct max8952_data *max8952, u8 reg) | |
58 | { | |
59 | int ret = i2c_smbus_read_byte_data(max8952->client, reg); | |
60 | if (ret > 0) | |
61 | ret &= 0xff; | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
66 | static int max8952_write_reg(struct max8952_data *max8952, | |
67 | u8 reg, u8 value) | |
68 | { | |
69 | return i2c_smbus_write_byte_data(max8952->client, reg, value); | |
70 | } | |
71 | ||
202f4f53 MH |
72 | static int max8952_list_voltage(struct regulator_dev *rdev, |
73 | unsigned int selector) | |
74 | { | |
75 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
76 | ||
77 | if (rdev_get_id(rdev) != 0) | |
78 | return -EINVAL; | |
79 | ||
b9b49af5 | 80 | return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000; |
202f4f53 MH |
81 | } |
82 | ||
83 | static int max8952_is_enabled(struct regulator_dev *rdev) | |
84 | { | |
85 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
86 | return max8952->en; | |
87 | } | |
88 | ||
89 | static int max8952_enable(struct regulator_dev *rdev) | |
90 | { | |
91 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
92 | ||
93 | /* If not valid, assume "ALWAYS_HIGH" */ | |
94 | if (gpio_is_valid(max8952->pdata->gpio_en)) | |
95 | gpio_set_value(max8952->pdata->gpio_en, 1); | |
96 | ||
97 | max8952->en = true; | |
98 | return 0; | |
99 | } | |
100 | ||
101 | static int max8952_disable(struct regulator_dev *rdev) | |
102 | { | |
103 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
104 | ||
105 | /* If not valid, assume "ALWAYS_HIGH" -> not permitted */ | |
106 | if (gpio_is_valid(max8952->pdata->gpio_en)) | |
107 | gpio_set_value(max8952->pdata->gpio_en, 0); | |
108 | else | |
109 | return -EPERM; | |
110 | ||
111 | max8952->en = false; | |
112 | return 0; | |
113 | } | |
114 | ||
b9b49af5 | 115 | static int max8952_get_voltage_sel(struct regulator_dev *rdev) |
202f4f53 MH |
116 | { |
117 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
118 | u8 vid = 0; | |
119 | ||
120 | if (max8952->vid0) | |
121 | vid += 1; | |
122 | if (max8952->vid1) | |
123 | vid += 2; | |
124 | ||
b9b49af5 | 125 | return vid; |
202f4f53 MH |
126 | } |
127 | ||
6ea67d04 AL |
128 | static int max8952_set_voltage_sel(struct regulator_dev *rdev, |
129 | unsigned selector) | |
202f4f53 MH |
130 | { |
131 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
202f4f53 MH |
132 | |
133 | if (!gpio_is_valid(max8952->pdata->gpio_vid0) || | |
e2cf3137 | 134 | !gpio_is_valid(max8952->pdata->gpio_vid1)) { |
202f4f53 MH |
135 | /* DVS not supported */ |
136 | return -EPERM; | |
137 | } | |
138 | ||
6ea67d04 AL |
139 | max8952->vid0 = selector & 0x1; |
140 | max8952->vid1 = (selector >> 1) & 0x1; | |
141 | gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0); | |
142 | gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1); | |
202f4f53 MH |
143 | |
144 | return 0; | |
145 | } | |
146 | ||
147 | static struct regulator_ops max8952_ops = { | |
148 | .list_voltage = max8952_list_voltage, | |
149 | .is_enabled = max8952_is_enabled, | |
150 | .enable = max8952_enable, | |
151 | .disable = max8952_disable, | |
b9b49af5 | 152 | .get_voltage_sel = max8952_get_voltage_sel, |
6ea67d04 | 153 | .set_voltage_sel = max8952_set_voltage_sel, |
202f4f53 MH |
154 | .set_suspend_disable = max8952_disable, |
155 | }; | |
156 | ||
bd6ff0d6 | 157 | static const struct regulator_desc regulator = { |
202f4f53 MH |
158 | .name = "MAX8952_VOUT", |
159 | .id = 0, | |
160 | .n_voltages = MAX8952_NUM_DVS_MODE, | |
161 | .ops = &max8952_ops, | |
162 | .type = REGULATOR_VOLTAGE, | |
163 | .owner = THIS_MODULE, | |
164 | }; | |
165 | ||
166 | static int __devinit max8952_pmic_probe(struct i2c_client *client, | |
167 | const struct i2c_device_id *i2c_id) | |
168 | { | |
169 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
170 | struct max8952_platform_data *pdata = client->dev.platform_data; | |
c172708d | 171 | struct regulator_config config = { }; |
202f4f53 MH |
172 | struct max8952_data *max8952; |
173 | ||
174 | int ret = 0, err = 0; | |
175 | ||
176 | if (!pdata) { | |
177 | dev_err(&client->dev, "Require the platform data\n"); | |
178 | return -EINVAL; | |
179 | } | |
180 | ||
181 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | |
182 | return -EIO; | |
183 | ||
372de4aa AL |
184 | max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data), |
185 | GFP_KERNEL); | |
202f4f53 MH |
186 | if (!max8952) |
187 | return -ENOMEM; | |
188 | ||
189 | max8952->client = client; | |
190 | max8952->dev = &client->dev; | |
191 | max8952->pdata = pdata; | |
202f4f53 | 192 | |
c172708d MB |
193 | config.dev = max8952->dev; |
194 | config.init_data = &pdata->reg_data; | |
195 | config.driver_data = max8952; | |
196 | ||
197 | max8952->rdev = regulator_register(®ulator, &config); | |
202f4f53 | 198 | |
da05738e AL |
199 | if (IS_ERR(max8952->rdev)) { |
200 | ret = PTR_ERR(max8952->rdev); | |
202f4f53 | 201 | dev_err(max8952->dev, "regulator init failed (%d)\n", ret); |
372de4aa | 202 | return ret; |
da05738e | 203 | } |
202f4f53 MH |
204 | |
205 | max8952->en = !!(pdata->reg_data.constraints.boot_on); | |
c8237f01 AL |
206 | max8952->vid0 = pdata->default_mode & 0x1; |
207 | max8952->vid1 = (pdata->default_mode >> 1) & 0x1; | |
202f4f53 MH |
208 | |
209 | if (gpio_is_valid(pdata->gpio_en)) { | |
210 | if (!gpio_request(pdata->gpio_en, "MAX8952 EN")) | |
211 | gpio_direction_output(pdata->gpio_en, max8952->en); | |
212 | else | |
213 | err = 1; | |
214 | } else | |
215 | err = 2; | |
216 | ||
217 | if (err) { | |
218 | dev_info(max8952->dev, "EN gpio invalid: assume that EN" | |
219 | "is always High\n"); | |
220 | max8952->en = 1; | |
221 | pdata->gpio_en = -1; /* Mark invalid */ | |
222 | } | |
223 | ||
224 | err = 0; | |
225 | ||
226 | if (gpio_is_valid(pdata->gpio_vid0) && | |
227 | gpio_is_valid(pdata->gpio_vid1)) { | |
228 | if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0")) | |
229 | gpio_direction_output(pdata->gpio_vid0, | |
c8237f01 | 230 | (pdata->default_mode) & 0x1); |
202f4f53 MH |
231 | else |
232 | err = 1; | |
233 | ||
234 | if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1")) | |
235 | gpio_direction_output(pdata->gpio_vid1, | |
c8237f01 | 236 | (pdata->default_mode >> 1) & 0x1); |
202f4f53 MH |
237 | else { |
238 | if (!err) | |
239 | gpio_free(pdata->gpio_vid0); | |
240 | err = 2; | |
241 | } | |
242 | ||
243 | } else | |
244 | err = 3; | |
245 | ||
246 | if (err) { | |
247 | dev_warn(max8952->dev, "VID0/1 gpio invalid: " | |
25985edc | 248 | "DVS not available.\n"); |
202f4f53 MH |
249 | max8952->vid0 = 0; |
250 | max8952->vid1 = 0; | |
251 | /* Mark invalid */ | |
252 | pdata->gpio_vid0 = -1; | |
253 | pdata->gpio_vid1 = -1; | |
254 | ||
255 | /* Disable Pulldown of EN only */ | |
256 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60); | |
257 | ||
258 | dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1" | |
259 | " do not have proper controls.\n"); | |
260 | } else { | |
261 | /* | |
262 | * Disable Pulldown on EN, VID0, VID1 to reduce | |
263 | * leakage current of MAX8952 assuming that MAX8952 | |
264 | * is turned on (EN==1). Note that without having VID0/1 | |
265 | * properly connected, turning pulldown off can be | |
266 | * problematic. Thus, turn this off only when they are | |
267 | * controllable by GPIO. | |
268 | */ | |
269 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0); | |
270 | } | |
271 | ||
272 | max8952_write_reg(max8952, MAX8952_REG_MODE0, | |
273 | (max8952_read_reg(max8952, | |
274 | MAX8952_REG_MODE0) & 0xC0) | | |
275 | (pdata->dvs_mode[0] & 0x3F)); | |
276 | max8952_write_reg(max8952, MAX8952_REG_MODE1, | |
277 | (max8952_read_reg(max8952, | |
278 | MAX8952_REG_MODE1) & 0xC0) | | |
279 | (pdata->dvs_mode[1] & 0x3F)); | |
280 | max8952_write_reg(max8952, MAX8952_REG_MODE2, | |
281 | (max8952_read_reg(max8952, | |
282 | MAX8952_REG_MODE2) & 0xC0) | | |
283 | (pdata->dvs_mode[2] & 0x3F)); | |
284 | max8952_write_reg(max8952, MAX8952_REG_MODE3, | |
285 | (max8952_read_reg(max8952, | |
286 | MAX8952_REG_MODE3) & 0xC0) | | |
287 | (pdata->dvs_mode[3] & 0x3F)); | |
288 | ||
289 | max8952_write_reg(max8952, MAX8952_REG_SYNC, | |
290 | (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) | | |
291 | ((pdata->sync_freq & 0x3) << 6)); | |
292 | max8952_write_reg(max8952, MAX8952_REG_RAMP, | |
293 | (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) | | |
294 | ((pdata->ramp_speed & 0x7) << 5)); | |
295 | ||
296 | i2c_set_clientdata(client, max8952); | |
297 | ||
da05738e | 298 | return 0; |
202f4f53 MH |
299 | } |
300 | ||
301 | static int __devexit max8952_pmic_remove(struct i2c_client *client) | |
302 | { | |
303 | struct max8952_data *max8952 = i2c_get_clientdata(client); | |
304 | struct max8952_platform_data *pdata = max8952->pdata; | |
305 | struct regulator_dev *rdev = max8952->rdev; | |
306 | ||
307 | regulator_unregister(rdev); | |
308 | ||
309 | gpio_free(pdata->gpio_vid0); | |
310 | gpio_free(pdata->gpio_vid1); | |
311 | gpio_free(pdata->gpio_en); | |
202f4f53 MH |
312 | return 0; |
313 | } | |
314 | ||
315 | static const struct i2c_device_id max8952_ids[] = { | |
316 | { "max8952", 0 }, | |
317 | { }, | |
318 | }; | |
319 | MODULE_DEVICE_TABLE(i2c, max8952_ids); | |
320 | ||
321 | static struct i2c_driver max8952_pmic_driver = { | |
322 | .probe = max8952_pmic_probe, | |
323 | .remove = __devexit_p(max8952_pmic_remove), | |
324 | .driver = { | |
325 | .name = "max8952", | |
326 | }, | |
327 | .id_table = max8952_ids, | |
328 | }; | |
329 | ||
330 | static int __init max8952_pmic_init(void) | |
331 | { | |
332 | return i2c_add_driver(&max8952_pmic_driver); | |
333 | } | |
334 | subsys_initcall(max8952_pmic_init); | |
335 | ||
336 | static void __exit max8952_pmic_exit(void) | |
337 | { | |
338 | i2c_del_driver(&max8952_pmic_driver); | |
339 | } | |
340 | module_exit(max8952_pmic_exit); | |
341 | ||
342 | MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver"); | |
343 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
344 | MODULE_LICENSE("GPL"); |