]>
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> | |
3ff4aa95 TF |
31 | #include <linux/of.h> |
32 | #include <linux/of_gpio.h> | |
33 | #include <linux/regulator/of_regulator.h> | |
202f4f53 MH |
34 | #include <linux/slab.h> |
35 | ||
36 | /* Registers */ | |
37 | enum { | |
38 | MAX8952_REG_MODE0, | |
39 | MAX8952_REG_MODE1, | |
40 | MAX8952_REG_MODE2, | |
41 | MAX8952_REG_MODE3, | |
42 | MAX8952_REG_CONTROL, | |
43 | MAX8952_REG_SYNC, | |
44 | MAX8952_REG_RAMP, | |
45 | MAX8952_REG_CHIP_ID1, | |
46 | MAX8952_REG_CHIP_ID2, | |
47 | }; | |
48 | ||
49 | struct max8952_data { | |
50 | struct i2c_client *client; | |
51 | struct device *dev; | |
202f4f53 MH |
52 | struct max8952_platform_data *pdata; |
53 | struct regulator_dev *rdev; | |
54 | ||
55 | bool vid0; | |
56 | bool vid1; | |
202f4f53 MH |
57 | }; |
58 | ||
59 | static int max8952_read_reg(struct max8952_data *max8952, u8 reg) | |
60 | { | |
61 | int ret = i2c_smbus_read_byte_data(max8952->client, reg); | |
62 | if (ret > 0) | |
63 | ret &= 0xff; | |
64 | ||
65 | return ret; | |
66 | } | |
67 | ||
68 | static int max8952_write_reg(struct max8952_data *max8952, | |
69 | u8 reg, u8 value) | |
70 | { | |
71 | return i2c_smbus_write_byte_data(max8952->client, reg, value); | |
72 | } | |
73 | ||
202f4f53 MH |
74 | static int max8952_list_voltage(struct regulator_dev *rdev, |
75 | unsigned int selector) | |
76 | { | |
77 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
78 | ||
79 | if (rdev_get_id(rdev) != 0) | |
80 | return -EINVAL; | |
81 | ||
b9b49af5 | 82 | return (max8952->pdata->dvs_mode[selector] * 10 + 770) * 1000; |
202f4f53 MH |
83 | } |
84 | ||
b9b49af5 | 85 | static int max8952_get_voltage_sel(struct regulator_dev *rdev) |
202f4f53 MH |
86 | { |
87 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
88 | u8 vid = 0; | |
89 | ||
90 | if (max8952->vid0) | |
91 | vid += 1; | |
92 | if (max8952->vid1) | |
93 | vid += 2; | |
94 | ||
b9b49af5 | 95 | return vid; |
202f4f53 MH |
96 | } |
97 | ||
6ea67d04 AL |
98 | static int max8952_set_voltage_sel(struct regulator_dev *rdev, |
99 | unsigned selector) | |
202f4f53 MH |
100 | { |
101 | struct max8952_data *max8952 = rdev_get_drvdata(rdev); | |
202f4f53 MH |
102 | |
103 | if (!gpio_is_valid(max8952->pdata->gpio_vid0) || | |
e2cf3137 | 104 | !gpio_is_valid(max8952->pdata->gpio_vid1)) { |
202f4f53 MH |
105 | /* DVS not supported */ |
106 | return -EPERM; | |
107 | } | |
108 | ||
6ea67d04 AL |
109 | max8952->vid0 = selector & 0x1; |
110 | max8952->vid1 = (selector >> 1) & 0x1; | |
111 | gpio_set_value(max8952->pdata->gpio_vid0, max8952->vid0); | |
112 | gpio_set_value(max8952->pdata->gpio_vid1, max8952->vid1); | |
202f4f53 MH |
113 | |
114 | return 0; | |
115 | } | |
116 | ||
117 | static struct regulator_ops max8952_ops = { | |
118 | .list_voltage = max8952_list_voltage, | |
b9b49af5 | 119 | .get_voltage_sel = max8952_get_voltage_sel, |
6ea67d04 | 120 | .set_voltage_sel = max8952_set_voltage_sel, |
202f4f53 MH |
121 | }; |
122 | ||
bd6ff0d6 | 123 | static const struct regulator_desc regulator = { |
202f4f53 MH |
124 | .name = "MAX8952_VOUT", |
125 | .id = 0, | |
126 | .n_voltages = MAX8952_NUM_DVS_MODE, | |
127 | .ops = &max8952_ops, | |
128 | .type = REGULATOR_VOLTAGE, | |
129 | .owner = THIS_MODULE, | |
130 | }; | |
131 | ||
3ff4aa95 TF |
132 | #ifdef CONFIG_OF |
133 | static struct of_device_id max8952_dt_match[] = { | |
134 | { .compatible = "maxim,max8952" }, | |
135 | {}, | |
136 | }; | |
137 | MODULE_DEVICE_TABLE(of, max8952_dt_match); | |
138 | ||
139 | static struct max8952_platform_data *max8952_parse_dt(struct device *dev) | |
140 | { | |
141 | struct max8952_platform_data *pd; | |
142 | struct device_node *np = dev->of_node; | |
143 | int ret; | |
144 | int i; | |
145 | ||
146 | pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); | |
7752d964 | 147 | if (!pd) |
3ff4aa95 | 148 | return NULL; |
3ff4aa95 TF |
149 | |
150 | pd->gpio_vid0 = of_get_named_gpio(np, "max8952,vid-gpios", 0); | |
151 | pd->gpio_vid1 = of_get_named_gpio(np, "max8952,vid-gpios", 1); | |
152 | pd->gpio_en = of_get_named_gpio(np, "max8952,en-gpio", 0); | |
153 | ||
154 | if (of_property_read_u32(np, "max8952,default-mode", &pd->default_mode)) | |
155 | dev_warn(dev, "Default mode not specified, assuming 0\n"); | |
156 | ||
157 | ret = of_property_read_u32_array(np, "max8952,dvs-mode-microvolt", | |
158 | pd->dvs_mode, ARRAY_SIZE(pd->dvs_mode)); | |
159 | if (ret) { | |
160 | dev_err(dev, "max8952,dvs-mode-microvolt property not specified"); | |
161 | return NULL; | |
162 | } | |
163 | ||
164 | for (i = 0; i < ARRAY_SIZE(pd->dvs_mode); ++i) { | |
165 | if (pd->dvs_mode[i] < 770000 || pd->dvs_mode[i] > 1400000) { | |
166 | dev_err(dev, "DVS voltage %d out of range\n", i); | |
167 | return NULL; | |
168 | } | |
169 | pd->dvs_mode[i] = (pd->dvs_mode[i] - 770000) / 10000; | |
170 | } | |
171 | ||
172 | if (of_property_read_u32(np, "max8952,sync-freq", &pd->sync_freq)) | |
173 | dev_warn(dev, "max8952,sync-freq property not specified, defaulting to 26MHz\n"); | |
174 | ||
175 | if (of_property_read_u32(np, "max8952,ramp-speed", &pd->ramp_speed)) | |
176 | dev_warn(dev, "max8952,ramp-speed property not specified, defaulting to 32mV/us\n"); | |
177 | ||
178 | pd->reg_data = of_get_regulator_init_data(dev, np); | |
179 | if (!pd->reg_data) { | |
180 | dev_err(dev, "Failed to parse regulator init data\n"); | |
181 | return NULL; | |
182 | } | |
183 | ||
184 | return pd; | |
185 | } | |
186 | #else | |
187 | static struct max8952_platform_data *max8952_parse_dt(struct device *dev) | |
188 | { | |
189 | return NULL; | |
190 | } | |
191 | #endif | |
192 | ||
a5023574 | 193 | static int max8952_pmic_probe(struct i2c_client *client, |
202f4f53 MH |
194 | const struct i2c_device_id *i2c_id) |
195 | { | |
196 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
dff91d0b | 197 | struct max8952_platform_data *pdata = dev_get_platdata(&client->dev); |
c172708d | 198 | struct regulator_config config = { }; |
202f4f53 MH |
199 | struct max8952_data *max8952; |
200 | ||
201 | int ret = 0, err = 0; | |
202 | ||
3ff4aa95 TF |
203 | if (client->dev.of_node) |
204 | pdata = max8952_parse_dt(&client->dev); | |
205 | ||
202f4f53 MH |
206 | if (!pdata) { |
207 | dev_err(&client->dev, "Require the platform data\n"); | |
208 | return -EINVAL; | |
209 | } | |
210 | ||
211 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | |
212 | return -EIO; | |
213 | ||
372de4aa AL |
214 | max8952 = devm_kzalloc(&client->dev, sizeof(struct max8952_data), |
215 | GFP_KERNEL); | |
202f4f53 MH |
216 | if (!max8952) |
217 | return -ENOMEM; | |
218 | ||
219 | max8952->client = client; | |
220 | max8952->dev = &client->dev; | |
221 | max8952->pdata = pdata; | |
202f4f53 | 222 | |
c172708d | 223 | config.dev = max8952->dev; |
3ec6eb9c | 224 | config.init_data = pdata->reg_data; |
c172708d | 225 | config.driver_data = max8952; |
71622e15 | 226 | config.of_node = client->dev.of_node; |
c172708d | 227 | |
b669e0ad | 228 | config.ena_gpio = pdata->gpio_en; |
3ec6eb9c | 229 | if (pdata->reg_data->constraints.boot_on) |
b669e0ad AL |
230 | config.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH; |
231 | ||
c172708d | 232 | max8952->rdev = regulator_register(®ulator, &config); |
202f4f53 | 233 | |
da05738e AL |
234 | if (IS_ERR(max8952->rdev)) { |
235 | ret = PTR_ERR(max8952->rdev); | |
202f4f53 | 236 | dev_err(max8952->dev, "regulator init failed (%d)\n", ret); |
372de4aa | 237 | return ret; |
da05738e | 238 | } |
202f4f53 | 239 | |
c8237f01 AL |
240 | max8952->vid0 = pdata->default_mode & 0x1; |
241 | max8952->vid1 = (pdata->default_mode >> 1) & 0x1; | |
202f4f53 | 242 | |
202f4f53 MH |
243 | if (gpio_is_valid(pdata->gpio_vid0) && |
244 | gpio_is_valid(pdata->gpio_vid1)) { | |
245 | if (!gpio_request(pdata->gpio_vid0, "MAX8952 VID0")) | |
246 | gpio_direction_output(pdata->gpio_vid0, | |
c8237f01 | 247 | (pdata->default_mode) & 0x1); |
202f4f53 MH |
248 | else |
249 | err = 1; | |
250 | ||
251 | if (!gpio_request(pdata->gpio_vid1, "MAX8952 VID1")) | |
252 | gpio_direction_output(pdata->gpio_vid1, | |
c8237f01 | 253 | (pdata->default_mode >> 1) & 0x1); |
202f4f53 MH |
254 | else { |
255 | if (!err) | |
256 | gpio_free(pdata->gpio_vid0); | |
257 | err = 2; | |
258 | } | |
259 | ||
260 | } else | |
261 | err = 3; | |
262 | ||
263 | if (err) { | |
264 | dev_warn(max8952->dev, "VID0/1 gpio invalid: " | |
25985edc | 265 | "DVS not available.\n"); |
202f4f53 MH |
266 | max8952->vid0 = 0; |
267 | max8952->vid1 = 0; | |
268 | /* Mark invalid */ | |
269 | pdata->gpio_vid0 = -1; | |
270 | pdata->gpio_vid1 = -1; | |
271 | ||
272 | /* Disable Pulldown of EN only */ | |
273 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x60); | |
274 | ||
275 | dev_err(max8952->dev, "DVS modes disabled because VID0 and VID1" | |
276 | " do not have proper controls.\n"); | |
277 | } else { | |
278 | /* | |
279 | * Disable Pulldown on EN, VID0, VID1 to reduce | |
280 | * leakage current of MAX8952 assuming that MAX8952 | |
281 | * is turned on (EN==1). Note that without having VID0/1 | |
282 | * properly connected, turning pulldown off can be | |
283 | * problematic. Thus, turn this off only when they are | |
284 | * controllable by GPIO. | |
285 | */ | |
286 | max8952_write_reg(max8952, MAX8952_REG_CONTROL, 0x0); | |
287 | } | |
288 | ||
289 | max8952_write_reg(max8952, MAX8952_REG_MODE0, | |
290 | (max8952_read_reg(max8952, | |
291 | MAX8952_REG_MODE0) & 0xC0) | | |
292 | (pdata->dvs_mode[0] & 0x3F)); | |
293 | max8952_write_reg(max8952, MAX8952_REG_MODE1, | |
294 | (max8952_read_reg(max8952, | |
295 | MAX8952_REG_MODE1) & 0xC0) | | |
296 | (pdata->dvs_mode[1] & 0x3F)); | |
297 | max8952_write_reg(max8952, MAX8952_REG_MODE2, | |
298 | (max8952_read_reg(max8952, | |
299 | MAX8952_REG_MODE2) & 0xC0) | | |
300 | (pdata->dvs_mode[2] & 0x3F)); | |
301 | max8952_write_reg(max8952, MAX8952_REG_MODE3, | |
302 | (max8952_read_reg(max8952, | |
303 | MAX8952_REG_MODE3) & 0xC0) | | |
304 | (pdata->dvs_mode[3] & 0x3F)); | |
305 | ||
306 | max8952_write_reg(max8952, MAX8952_REG_SYNC, | |
307 | (max8952_read_reg(max8952, MAX8952_REG_SYNC) & 0x3F) | | |
308 | ((pdata->sync_freq & 0x3) << 6)); | |
309 | max8952_write_reg(max8952, MAX8952_REG_RAMP, | |
310 | (max8952_read_reg(max8952, MAX8952_REG_RAMP) & 0x1F) | | |
311 | ((pdata->ramp_speed & 0x7) << 5)); | |
312 | ||
313 | i2c_set_clientdata(client, max8952); | |
314 | ||
da05738e | 315 | return 0; |
202f4f53 MH |
316 | } |
317 | ||
8dc995f5 | 318 | static int max8952_pmic_remove(struct i2c_client *client) |
202f4f53 MH |
319 | { |
320 | struct max8952_data *max8952 = i2c_get_clientdata(client); | |
321 | struct max8952_platform_data *pdata = max8952->pdata; | |
322 | struct regulator_dev *rdev = max8952->rdev; | |
323 | ||
324 | regulator_unregister(rdev); | |
325 | ||
326 | gpio_free(pdata->gpio_vid0); | |
327 | gpio_free(pdata->gpio_vid1); | |
202f4f53 MH |
328 | return 0; |
329 | } | |
330 | ||
331 | static const struct i2c_device_id max8952_ids[] = { | |
332 | { "max8952", 0 }, | |
333 | { }, | |
334 | }; | |
335 | MODULE_DEVICE_TABLE(i2c, max8952_ids); | |
336 | ||
337 | static struct i2c_driver max8952_pmic_driver = { | |
338 | .probe = max8952_pmic_probe, | |
5eb9f2b9 | 339 | .remove = max8952_pmic_remove, |
202f4f53 MH |
340 | .driver = { |
341 | .name = "max8952", | |
3ff4aa95 | 342 | .of_match_table = of_match_ptr(max8952_dt_match), |
202f4f53 MH |
343 | }, |
344 | .id_table = max8952_ids, | |
345 | }; | |
346 | ||
347 | static int __init max8952_pmic_init(void) | |
348 | { | |
349 | return i2c_add_driver(&max8952_pmic_driver); | |
350 | } | |
351 | subsys_initcall(max8952_pmic_init); | |
352 | ||
353 | static void __exit max8952_pmic_exit(void) | |
354 | { | |
355 | i2c_del_driver(&max8952_pmic_driver); | |
356 | } | |
357 | module_exit(max8952_pmic_exit); | |
358 | ||
359 | MODULE_DESCRIPTION("MAXIM 8952 voltage regulator driver"); | |
360 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
361 | MODULE_LICENSE("GPL"); |