]>
Commit | Line | Data |
---|---|---|
3784b6d6 RG |
1 | /* |
2 | * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/err.h> | |
22 | #include <linux/of.h> | |
23 | #include <linux/of_device.h> | |
24 | #include <linux/regulator/of_regulator.h> | |
25 | #include <linux/platform_device.h> | |
26 | #include <linux/regulator/driver.h> | |
27 | #include <linux/regulator/machine.h> | |
28 | #include <linux/regulator/pfuze100.h> | |
29 | #include <linux/i2c.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/regmap.h> | |
32 | ||
33 | #define PFUZE_NUMREGS 128 | |
34 | #define PFUZE100_VOL_OFFSET 0 | |
35 | #define PFUZE100_STANDBY_OFFSET 1 | |
36 | #define PFUZE100_MODE_OFFSET 3 | |
37 | #define PFUZE100_CONF_OFFSET 4 | |
38 | ||
39 | #define PFUZE100_DEVICEID 0x0 | |
40 | #define PFUZE100_REVID 0x3 | |
41 | #define PFUZE100_FABID 0x3 | |
42 | ||
43 | #define PFUZE100_SW1ABVOL 0x20 | |
44 | #define PFUZE100_SW1CVOL 0x2e | |
45 | #define PFUZE100_SW2VOL 0x35 | |
46 | #define PFUZE100_SW3AVOL 0x3c | |
47 | #define PFUZE100_SW3BVOL 0x43 | |
48 | #define PFUZE100_SW4VOL 0x4a | |
49 | #define PFUZE100_SWBSTCON1 0x66 | |
50 | #define PFUZE100_VREFDDRCON 0x6a | |
51 | #define PFUZE100_VSNVSVOL 0x6b | |
52 | #define PFUZE100_VGEN1VOL 0x6c | |
53 | #define PFUZE100_VGEN2VOL 0x6d | |
54 | #define PFUZE100_VGEN3VOL 0x6e | |
55 | #define PFUZE100_VGEN4VOL 0x6f | |
56 | #define PFUZE100_VGEN5VOL 0x70 | |
57 | #define PFUZE100_VGEN6VOL 0x71 | |
58 | ||
59 | struct pfuze_regulator { | |
60 | struct regulator_desc desc; | |
61 | unsigned char stby_reg; | |
62 | unsigned char stby_mask; | |
63 | }; | |
64 | ||
65 | struct pfuze_chip { | |
66 | struct regmap *regmap; | |
67 | struct device *dev; | |
68 | struct pfuze_regulator regulator_descs[PFUZE100_MAX_REGULATOR]; | |
69 | struct regulator_dev *regulators[PFUZE100_MAX_REGULATOR]; | |
70 | }; | |
71 | ||
72 | static const int pfuze100_swbst[] = { | |
73 | 5000000, 5050000, 5100000, 5150000, | |
74 | }; | |
75 | ||
76 | static const int pfuze100_vsnvs[] = { | |
77 | 1000000, 1100000, 1200000, 1300000, 1500000, 1800000, 3000000, | |
78 | }; | |
79 | ||
80 | static const struct i2c_device_id pfuze_device_id[] = { | |
81 | {.name = "pfuze100"}, | |
82 | {}, | |
83 | }; | |
84 | MODULE_DEVICE_TABLE(i2c, pfuze_device_id); | |
85 | ||
86 | static const struct of_device_id pfuze_dt_ids[] = { | |
87 | { .compatible = "fsl,pfuze100" }, | |
88 | {}, | |
89 | }; | |
90 | MODULE_DEVICE_TABLE(of, pfuze_dt_ids); | |
91 | ||
92 | static int pfuze100_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay) | |
93 | { | |
94 | struct pfuze_chip *pfuze100 = rdev_get_drvdata(rdev); | |
95 | int id = rdev->desc->id; | |
96 | unsigned int val, ramp_bits, reg; | |
97 | int ret; | |
98 | ||
99 | if (id < PFUZE100_SWBST) { | |
100 | if (id == PFUZE100_SW1AB) | |
101 | reg = PFUZE100_SW1ABVOL; | |
102 | else | |
103 | reg = PFUZE100_SW1CVOL + (id - PFUZE100_SW1C) * 7; | |
104 | regmap_read(pfuze100->regmap, reg, &val); | |
105 | ||
106 | if (id <= PFUZE100_SW1C) | |
107 | ramp_delay = 25000 / (2 * ramp_delay); | |
108 | else if (val & 0x40) | |
109 | ramp_delay = 50000 / (4 * ramp_delay); | |
110 | else | |
111 | ramp_delay = 25000 / (2 * ramp_delay); | |
112 | ||
113 | ramp_bits = (ramp_delay >> 1) - (ramp_delay >> 3); | |
114 | ret = regmap_update_bits(pfuze100->regmap, reg + 4 , 0xc0, | |
115 | ramp_bits << 6); | |
116 | if (ret < 0) | |
117 | dev_err(pfuze100->dev, "ramp failed, err %d\n", ret); | |
118 | } else | |
119 | ret = -EACCES; | |
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
124 | static struct regulator_ops pfuze100_ldo_regulator_ops = { | |
125 | .enable = regulator_enable_regmap, | |
126 | .disable = regulator_disable_regmap, | |
127 | .is_enabled = regulator_is_enabled_regmap, | |
128 | .list_voltage = regulator_list_voltage_linear, | |
129 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
130 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
131 | }; | |
132 | ||
133 | static struct regulator_ops pfuze100_fixed_regulator_ops = { | |
134 | .list_voltage = regulator_list_voltage_linear, | |
135 | }; | |
136 | ||
137 | static struct regulator_ops pfuze100_sw_regulator_ops = { | |
138 | .list_voltage = regulator_list_voltage_linear, | |
139 | .set_voltage_sel = regulator_set_voltage_sel_regmap, | |
140 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
141 | .set_voltage_time_sel = regulator_set_voltage_time_sel, | |
142 | .set_ramp_delay = pfuze100_set_ramp_delay, | |
143 | }; | |
144 | ||
145 | static struct regulator_ops pfuze100_swb_regulator_ops = { | |
146 | .list_voltage = regulator_list_voltage_table, | |
2e04cc41 | 147 | .map_voltage = regulator_map_voltage_ascend, |
3784b6d6 RG |
148 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
149 | .get_voltage_sel = regulator_get_voltage_sel_regmap, | |
150 | ||
151 | }; | |
152 | ||
153 | #define PFUZE100_FIXED_REG(_name, base, voltage) \ | |
154 | [PFUZE100_ ## _name] = { \ | |
155 | .desc = { \ | |
156 | .name = #_name, \ | |
157 | .n_voltages = 1, \ | |
158 | .ops = &pfuze100_fixed_regulator_ops, \ | |
159 | .type = REGULATOR_VOLTAGE, \ | |
160 | .id = PFUZE100_ ## _name, \ | |
161 | .owner = THIS_MODULE, \ | |
162 | .min_uV = (voltage), \ | |
163 | .enable_reg = (base), \ | |
164 | .enable_mask = 0x10, \ | |
165 | }, \ | |
166 | } | |
167 | ||
168 | #define PFUZE100_SW_REG(_name, base, min, max, step) \ | |
169 | [PFUZE100_ ## _name] = { \ | |
170 | .desc = { \ | |
171 | .name = #_name,\ | |
172 | .n_voltages = ((max) - (min)) / (step) + 1, \ | |
173 | .ops = &pfuze100_sw_regulator_ops, \ | |
174 | .type = REGULATOR_VOLTAGE, \ | |
175 | .id = PFUZE100_ ## _name, \ | |
176 | .owner = THIS_MODULE, \ | |
177 | .min_uV = (min), \ | |
178 | .uV_step = (step), \ | |
179 | .vsel_reg = (base) + PFUZE100_VOL_OFFSET, \ | |
180 | .vsel_mask = 0x3f, \ | |
181 | }, \ | |
182 | .stby_reg = (base) + PFUZE100_STANDBY_OFFSET, \ | |
183 | .stby_mask = 0x3f, \ | |
184 | } | |
185 | ||
186 | #define PFUZE100_SWB_REG(_name, base, mask, voltages) \ | |
187 | [PFUZE100_ ## _name] = { \ | |
188 | .desc = { \ | |
189 | .name = #_name, \ | |
190 | .n_voltages = ARRAY_SIZE(voltages), \ | |
191 | .ops = &pfuze100_swb_regulator_ops, \ | |
192 | .type = REGULATOR_VOLTAGE, \ | |
193 | .id = PFUZE100_ ## _name, \ | |
194 | .owner = THIS_MODULE, \ | |
195 | .volt_table = voltages, \ | |
196 | .vsel_reg = (base), \ | |
197 | .vsel_mask = (mask), \ | |
198 | }, \ | |
199 | } | |
200 | ||
201 | #define PFUZE100_VGEN_REG(_name, base, min, max, step) \ | |
202 | [PFUZE100_ ## _name] = { \ | |
203 | .desc = { \ | |
204 | .name = #_name, \ | |
205 | .n_voltages = ((max) - (min)) / (step) + 1, \ | |
206 | .ops = &pfuze100_ldo_regulator_ops, \ | |
207 | .type = REGULATOR_VOLTAGE, \ | |
208 | .id = PFUZE100_ ## _name, \ | |
209 | .owner = THIS_MODULE, \ | |
210 | .min_uV = (min), \ | |
211 | .uV_step = (step), \ | |
212 | .vsel_reg = (base), \ | |
213 | .vsel_mask = 0xf, \ | |
214 | .enable_reg = (base), \ | |
215 | .enable_mask = 0x10, \ | |
216 | }, \ | |
217 | .stby_reg = (base), \ | |
218 | .stby_mask = 0x20, \ | |
219 | } | |
220 | ||
221 | static struct pfuze_regulator pfuze100_regulators[] = { | |
222 | PFUZE100_SW_REG(SW1AB, PFUZE100_SW1ABVOL, 300000, 1875000, 25000), | |
223 | PFUZE100_SW_REG(SW1C, PFUZE100_SW1CVOL, 300000, 1875000, 25000), | |
224 | PFUZE100_SW_REG(SW2, PFUZE100_SW2VOL, 400000, 1975000, 25000), | |
225 | PFUZE100_SW_REG(SW3A, PFUZE100_SW3AVOL, 400000, 1975000, 25000), | |
226 | PFUZE100_SW_REG(SW3B, PFUZE100_SW3BVOL, 400000, 1975000, 25000), | |
227 | PFUZE100_SW_REG(SW4, PFUZE100_SW4VOL, 400000, 1975000, 25000), | |
228 | PFUZE100_SWB_REG(SWBST, PFUZE100_SWBSTCON1, 0x3 , pfuze100_swbst), | |
229 | PFUZE100_SWB_REG(VSNVS, PFUZE100_VSNVSVOL, 0x7, pfuze100_vsnvs), | |
230 | PFUZE100_FIXED_REG(VREFDDR, PFUZE100_VREFDDRCON, 750000), | |
231 | PFUZE100_VGEN_REG(VGEN1, PFUZE100_VGEN1VOL, 800000, 1550000, 50000), | |
232 | PFUZE100_VGEN_REG(VGEN2, PFUZE100_VGEN2VOL, 800000, 1550000, 50000), | |
233 | PFUZE100_VGEN_REG(VGEN3, PFUZE100_VGEN3VOL, 1800000, 3300000, 100000), | |
234 | PFUZE100_VGEN_REG(VGEN4, PFUZE100_VGEN4VOL, 1800000, 3300000, 100000), | |
235 | PFUZE100_VGEN_REG(VGEN5, PFUZE100_VGEN5VOL, 1800000, 3300000, 100000), | |
236 | PFUZE100_VGEN_REG(VGEN6, PFUZE100_VGEN6VOL, 1800000, 3300000, 100000), | |
237 | }; | |
238 | ||
239 | #ifdef CONFIG_OF | |
240 | static struct of_regulator_match pfuze100_matches[] = { | |
241 | { .name = "sw1ab", }, | |
242 | { .name = "sw1c", }, | |
243 | { .name = "sw2", }, | |
244 | { .name = "sw3a", }, | |
245 | { .name = "sw3b", }, | |
246 | { .name = "sw4", }, | |
247 | { .name = "swbst", }, | |
248 | { .name = "vsnvs", }, | |
249 | { .name = "vrefddr", }, | |
250 | { .name = "vgen1", }, | |
251 | { .name = "vgen2", }, | |
252 | { .name = "vgen3", }, | |
253 | { .name = "vgen4", }, | |
254 | { .name = "vgen5", }, | |
255 | { .name = "vgen6", }, | |
256 | }; | |
257 | ||
258 | static int pfuze_parse_regulators_dt(struct pfuze_chip *chip) | |
259 | { | |
260 | struct device *dev = chip->dev; | |
261 | struct device_node *np, *parent; | |
262 | int ret; | |
263 | ||
264 | np = of_node_get(dev->parent->of_node); | |
265 | if (!np) | |
266 | return 0; | |
267 | ||
268 | parent = of_find_node_by_name(np, "regulators"); | |
269 | if (!parent) { | |
270 | dev_err(dev, "regulators node not found\n"); | |
271 | return -EINVAL; | |
272 | } | |
273 | ||
274 | ret = of_regulator_match(dev, parent, pfuze100_matches, | |
275 | ARRAY_SIZE(pfuze100_matches)); | |
276 | ||
277 | of_node_put(parent); | |
278 | if (ret < 0) { | |
279 | dev_err(dev, "Error parsing regulator init data: %d\n", | |
280 | ret); | |
281 | return ret; | |
282 | } | |
283 | ||
284 | return 0; | |
285 | } | |
286 | ||
287 | static inline struct regulator_init_data *match_init_data(int index) | |
288 | { | |
289 | return pfuze100_matches[index].init_data; | |
290 | } | |
291 | ||
292 | static inline struct device_node *match_of_node(int index) | |
293 | { | |
294 | return pfuze100_matches[index].of_node; | |
295 | } | |
296 | #else | |
297 | static int pfuze_parse_regulators_dt(struct pfuze_chip *chip) | |
298 | { | |
205c97bc | 299 | return 0; |
3784b6d6 RG |
300 | } |
301 | ||
302 | static inline struct regulator_init_data *match_init_data(int index) | |
303 | { | |
304 | return NULL; | |
305 | } | |
306 | ||
307 | static inline struct device_node *match_of_node(int index) | |
308 | { | |
309 | return NULL; | |
310 | } | |
311 | #endif | |
312 | ||
313 | static int pfuze_identify(struct pfuze_chip *pfuze_chip) | |
314 | { | |
315 | unsigned int value; | |
316 | int ret; | |
317 | ||
318 | ret = regmap_read(pfuze_chip->regmap, PFUZE100_DEVICEID, &value); | |
319 | if (ret) | |
320 | return ret; | |
321 | ||
322 | if (value & 0x0f) { | |
323 | dev_warn(pfuze_chip->dev, "Illegal ID: %x\n", value); | |
324 | return -ENODEV; | |
325 | } | |
326 | ||
327 | ret = regmap_read(pfuze_chip->regmap, PFUZE100_REVID, &value); | |
328 | if (ret) | |
329 | return ret; | |
330 | dev_info(pfuze_chip->dev, | |
331 | "Full lay: %x, Metal lay: %x\n", | |
332 | (value & 0xf0) >> 4, value & 0x0f); | |
333 | ||
334 | ret = regmap_read(pfuze_chip->regmap, PFUZE100_FABID, &value); | |
335 | if (ret) | |
336 | return ret; | |
337 | dev_info(pfuze_chip->dev, "FAB: %x, FIN: %x\n", | |
338 | (value & 0xc) >> 2, value & 0x3); | |
339 | ||
340 | return 0; | |
341 | } | |
342 | ||
343 | static const struct regmap_config pfuze_regmap_config = { | |
344 | .reg_bits = 8, | |
345 | .val_bits = 8, | |
346 | .max_register = PFUZE_NUMREGS, | |
347 | .cache_type = REGCACHE_RBTREE, | |
348 | }; | |
349 | ||
350 | static int pfuze100_regulator_probe(struct i2c_client *client, | |
351 | const struct i2c_device_id *id) | |
352 | { | |
353 | struct pfuze_chip *pfuze_chip; | |
354 | struct pfuze_regulator_platform_data *pdata = | |
355 | dev_get_platdata(&client->dev); | |
356 | struct regulator_config config = { }; | |
357 | int i, ret; | |
358 | ||
359 | pfuze_chip = devm_kzalloc(&client->dev, sizeof(*pfuze_chip), | |
360 | GFP_KERNEL); | |
361 | if (!pfuze_chip) | |
362 | return -ENOMEM; | |
363 | ||
8c86ab25 | 364 | i2c_set_clientdata(client, pfuze_chip); |
3784b6d6 RG |
365 | |
366 | memcpy(pfuze_chip->regulator_descs, pfuze100_regulators, | |
367 | sizeof(pfuze_chip->regulator_descs)); | |
368 | ||
369 | pfuze_chip->dev = &client->dev; | |
370 | ||
371 | pfuze_chip->regmap = devm_regmap_init_i2c(client, &pfuze_regmap_config); | |
372 | if (IS_ERR(pfuze_chip->regmap)) { | |
373 | ret = PTR_ERR(pfuze_chip->regmap); | |
374 | dev_err(&client->dev, | |
375 | "regmap allocation failed with err %d\n", ret); | |
376 | return ret; | |
377 | } | |
378 | ||
379 | ret = pfuze_identify(pfuze_chip); | |
380 | if (ret) { | |
381 | dev_err(&client->dev, "unrecognized pfuze chip ID!\n"); | |
382 | return ret; | |
383 | } | |
384 | ||
385 | ret = pfuze_parse_regulators_dt(pfuze_chip); | |
386 | if (ret) | |
387 | return ret; | |
388 | ||
389 | for (i = 0; i < PFUZE100_MAX_REGULATOR; i++) { | |
390 | struct regulator_init_data *init_data; | |
d9493234 | 391 | struct regulator_desc *desc; |
3784b6d6 RG |
392 | int val; |
393 | ||
d9493234 AL |
394 | desc = &pfuze_chip->regulator_descs[i].desc; |
395 | ||
3784b6d6 RG |
396 | if (pdata) |
397 | init_data = pdata->init_data[i]; | |
398 | else | |
399 | init_data = match_init_data(i); | |
400 | ||
401 | /* SW2~SW4 high bit check and modify the voltage value table */ | |
402 | if (i > PFUZE100_SW1C && i < PFUZE100_SWBST) { | |
d9493234 | 403 | regmap_read(pfuze_chip->regmap, desc->vsel_reg, &val); |
3784b6d6 | 404 | if (val & 0x40) { |
d9493234 AL |
405 | desc->min_uV = 800000; |
406 | desc->uV_step = 50000; | |
407 | desc->n_voltages = 51; | |
3784b6d6 RG |
408 | } |
409 | } | |
410 | ||
411 | config.dev = &client->dev; | |
412 | config.init_data = init_data; | |
413 | config.driver_data = pfuze_chip; | |
414 | config.of_node = match_of_node(i); | |
415 | ||
d9493234 | 416 | pfuze_chip->regulators[i] = regulator_register(desc, &config); |
3784b6d6 RG |
417 | if (IS_ERR(pfuze_chip->regulators[i])) { |
418 | dev_err(&client->dev, "register regulator%s failed\n", | |
419 | pfuze100_regulators[i].desc.name); | |
420 | ret = PTR_ERR(pfuze_chip->regulators[i]); | |
421 | while (--i >= 0) | |
422 | regulator_unregister(pfuze_chip->regulators[i]); | |
423 | return ret; | |
424 | } | |
425 | } | |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
430 | static int pfuze100_regulator_remove(struct i2c_client *client) | |
431 | { | |
432 | int i; | |
8c86ab25 | 433 | struct pfuze_chip *pfuze_chip = i2c_get_clientdata(client); |
3784b6d6 RG |
434 | |
435 | for (i = 0; i < PFUZE100_MAX_REGULATOR; i++) | |
436 | regulator_unregister(pfuze_chip->regulators[i]); | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
441 | static struct i2c_driver pfuze_driver = { | |
442 | .id_table = pfuze_device_id, | |
443 | .driver = { | |
444 | .name = "pfuze100-regulator", | |
445 | .owner = THIS_MODULE, | |
446 | .of_match_table = pfuze_dt_ids, | |
447 | }, | |
448 | .probe = pfuze100_regulator_probe, | |
449 | .remove = pfuze100_regulator_remove, | |
450 | }; | |
451 | module_i2c_driver(pfuze_driver); | |
452 | ||
453 | MODULE_AUTHOR("Robin Gong <[email protected]>"); | |
454 | MODULE_DESCRIPTION("Regulator Driver for Freescale PFUZE100 PMIC"); | |
12d20fc2 | 455 | MODULE_LICENSE("GPL v2"); |
40839bff | 456 | MODULE_ALIAS("i2c:pfuze100-regulator"); |