blob: 4940e8287df68090104a78cc6239ab90e22423c6 [file] [log] [blame]
Yunfan Zhang49d8c592012-09-08 03:52:18 -07001/*
2 * FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver.
3 *
4 * Supported Part Numbers:
5 * FAN53555UC00X/01X/03X/04X/05X
6 *
7 * Copyright (c) 2012 Marvell Technology Ltd.
8 * Yunfan Zhang <yfzhang@marvell.com>
9 *
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15#include <linux/module.h>
16#include <linux/param.h>
17#include <linux/err.h>
18#include <linux/platform_device.h>
19#include <linux/regulator/driver.h>
20#include <linux/regulator/machine.h>
Heiko Stuebner91f23d82014-09-16 21:22:53 +020021#include <linux/regulator/of_regulator.h>
22#include <linux/of_device.h>
Yunfan Zhang49d8c592012-09-08 03:52:18 -070023#include <linux/i2c.h>
24#include <linux/slab.h>
25#include <linux/regmap.h>
26#include <linux/regulator/fan53555.h>
27
28/* Voltage setting */
29#define FAN53555_VSEL0 0x00
30#define FAN53555_VSEL1 0x01
31/* Control register */
32#define FAN53555_CONTROL 0x02
33/* IC Type */
34#define FAN53555_ID1 0x03
35/* IC mask version */
36#define FAN53555_ID2 0x04
37/* Monitor register */
38#define FAN53555_MONITOR 0x05
39
40/* VSEL bit definitions */
41#define VSEL_BUCK_EN (1 << 7)
42#define VSEL_MODE (1 << 6)
43#define VSEL_NSEL_MASK 0x3F
44/* Chip ID and Verison */
45#define DIE_ID 0x0F /* ID1 */
46#define DIE_REV 0x0F /* ID2 */
47/* Control bit definitions */
48#define CTL_OUTPUT_DISCHG (1 << 7)
49#define CTL_SLEW_MASK (0x7 << 4)
50#define CTL_SLEW_SHIFT 4
51#define CTL_RESET (1 << 2)
52
53#define FAN53555_NVOLTAGES 64 /* Numbers of voltages */
54
Heiko Stuebneree309282014-09-16 21:22:54 +020055enum fan53555_vendor {
56 FAN53555_VENDOR_FAIRCHILD = 0,
57 FAN53555_VENDOR_SILERGY,
58};
59
Yunfan Zhang49d8c592012-09-08 03:52:18 -070060/* IC Type */
61enum {
62 FAN53555_CHIP_ID_00 = 0,
63 FAN53555_CHIP_ID_01,
64 FAN53555_CHIP_ID_02,
65 FAN53555_CHIP_ID_03,
66 FAN53555_CHIP_ID_04,
67 FAN53555_CHIP_ID_05,
68};
69
Heiko Stuebneree309282014-09-16 21:22:54 +020070enum {
71 SILERGY_SYR82X = 8,
72};
73
Yunfan Zhang49d8c592012-09-08 03:52:18 -070074struct fan53555_device_info {
Heiko Stuebneree309282014-09-16 21:22:54 +020075 enum fan53555_vendor vendor;
Yunfan Zhang49d8c592012-09-08 03:52:18 -070076 struct regmap *regmap;
77 struct device *dev;
78 struct regulator_desc desc;
79 struct regulator_dev *rdev;
80 struct regulator_init_data *regulator;
81 /* IC Type and Rev */
82 int chip_id;
83 int chip_rev;
84 /* Voltage setting register */
85 unsigned int vol_reg;
86 unsigned int sleep_reg;
87 /* Voltage range and step(linear) */
88 unsigned int vsel_min;
Yunfan Zhang49d8c592012-09-08 03:52:18 -070089 unsigned int vsel_step;
90 /* Voltage slew rate limiting */
91 unsigned int slew_rate;
92 /* Sleep voltage cache */
93 unsigned int sleep_vol_cache;
94};
95
96static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV)
97{
98 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
99 int ret;
100
101 if (di->sleep_vol_cache == uV)
102 return 0;
103 ret = regulator_map_voltage_linear(rdev, uV, uV);
104 if (ret < 0)
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530105 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700106 ret = regmap_update_bits(di->regmap, di->sleep_reg,
107 VSEL_NSEL_MASK, ret);
108 if (ret < 0)
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530109 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700110 /* Cache the sleep voltage setting.
111 * Might not be the real voltage which is rounded */
112 di->sleep_vol_cache = uV;
113
114 return 0;
115}
116
117static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode)
118{
119 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
120
121 switch (mode) {
122 case REGULATOR_MODE_FAST:
123 regmap_update_bits(di->regmap, di->vol_reg,
124 VSEL_MODE, VSEL_MODE);
125 break;
126 case REGULATOR_MODE_NORMAL:
127 regmap_update_bits(di->regmap, di->vol_reg, VSEL_MODE, 0);
128 break;
129 default:
130 return -EINVAL;
131 }
132 return 0;
133}
134
135static unsigned int fan53555_get_mode(struct regulator_dev *rdev)
136{
137 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
138 unsigned int val;
139 int ret = 0;
140
141 ret = regmap_read(di->regmap, di->vol_reg, &val);
142 if (ret < 0)
143 return ret;
144 if (val & VSEL_MODE)
145 return REGULATOR_MODE_FAST;
146 else
147 return REGULATOR_MODE_NORMAL;
148}
149
Krzysztof Kozlowski121b5672015-01-05 10:04:19 +0100150static const int slew_rates[] = {
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200151 64000,
152 32000,
153 16000,
154 8000,
155 4000,
156 2000,
157 1000,
158 500,
159};
160
161static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp)
162{
163 struct fan53555_device_info *di = rdev_get_drvdata(rdev);
164 int regval = -1, i;
165
166 for (i = 0; i < ARRAY_SIZE(slew_rates); i++) {
167 if (ramp <= slew_rates[i])
168 regval = i;
169 else
170 break;
171 }
172
173 if (regval < 0) {
174 dev_err(di->dev, "unsupported ramp value %d\n", ramp);
175 return -EINVAL;
176 }
177
178 return regmap_update_bits(di->regmap, FAN53555_CONTROL,
179 CTL_SLEW_MASK, regval << CTL_SLEW_SHIFT);
180}
181
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700182static struct regulator_ops fan53555_regulator_ops = {
183 .set_voltage_sel = regulator_set_voltage_sel_regmap,
184 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Heiko Stuebnerfda87a42015-05-26 18:19:45 +0200185 .set_voltage_time_sel = regulator_set_voltage_time_sel,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700186 .map_voltage = regulator_map_voltage_linear,
187 .list_voltage = regulator_list_voltage_linear,
188 .set_suspend_voltage = fan53555_set_suspend_voltage,
189 .enable = regulator_enable_regmap,
190 .disable = regulator_disable_regmap,
191 .is_enabled = regulator_is_enabled_regmap,
192 .set_mode = fan53555_set_mode,
193 .get_mode = fan53555_get_mode,
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200194 .set_ramp_delay = fan53555_set_ramp,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700195};
196
Heiko Stuebneree309282014-09-16 21:22:54 +0200197static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di)
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700198{
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700199 /* Init voltage range and step */
200 switch (di->chip_id) {
201 case FAN53555_CHIP_ID_00:
202 case FAN53555_CHIP_ID_01:
203 case FAN53555_CHIP_ID_03:
204 case FAN53555_CHIP_ID_05:
205 di->vsel_min = 600000;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700206 di->vsel_step = 10000;
207 break;
208 case FAN53555_CHIP_ID_04:
209 di->vsel_min = 603000;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700210 di->vsel_step = 12826;
211 break;
212 default:
213 dev_err(di->dev,
Heiko Stuebneree309282014-09-16 21:22:54 +0200214 "Chip ID %d not supported!\n", di->chip_id);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700215 return -EINVAL;
216 }
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200217
218 return 0;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700219}
220
Heiko Stuebneree309282014-09-16 21:22:54 +0200221static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di)
222{
223 /* Init voltage range and step */
224 switch (di->chip_id) {
225 case SILERGY_SYR82X:
226 di->vsel_min = 712500;
227 di->vsel_step = 12500;
228 break;
229 default:
230 dev_err(di->dev,
231 "Chip ID %d not supported!\n", di->chip_id);
232 return -EINVAL;
233 }
234
235 return 0;
236}
237
238/* For 00,01,03,05 options:
239 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V.
240 * For 04 option:
241 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V.
242 * */
243static int fan53555_device_setup(struct fan53555_device_info *di,
244 struct fan53555_platform_data *pdata)
245{
246 int ret = 0;
247
248 /* Setup voltage control register */
249 switch (pdata->sleep_vsel_id) {
250 case FAN53555_VSEL_ID_0:
251 di->sleep_reg = FAN53555_VSEL0;
252 di->vol_reg = FAN53555_VSEL1;
253 break;
254 case FAN53555_VSEL_ID_1:
255 di->sleep_reg = FAN53555_VSEL1;
256 di->vol_reg = FAN53555_VSEL0;
257 break;
258 default:
259 dev_err(di->dev, "Invalid VSEL ID!\n");
260 return -EINVAL;
261 }
262
263 switch (di->vendor) {
264 case FAN53555_VENDOR_FAIRCHILD:
265 ret = fan53555_voltages_setup_fairchild(di);
266 break;
267 case FAN53555_VENDOR_SILERGY:
268 ret = fan53555_voltages_setup_silergy(di);
269 break;
270 default:
Axel Linfe230532014-09-18 21:48:48 +0800271 dev_err(di->dev, "vendor %d not supported!\n", di->vendor);
Heiko Stuebneree309282014-09-16 21:22:54 +0200272 return -EINVAL;
273 }
274
275 return ret;
276}
277
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700278static int fan53555_regulator_register(struct fan53555_device_info *di,
279 struct regulator_config *config)
280{
281 struct regulator_desc *rdesc = &di->desc;
282
283 rdesc->name = "fan53555-reg";
Heiko Stuebner3415d602014-09-14 21:23:01 +0200284 rdesc->supply_name = "vin";
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700285 rdesc->ops = &fan53555_regulator_ops;
286 rdesc->type = REGULATOR_VOLTAGE;
287 rdesc->n_voltages = FAN53555_NVOLTAGES;
288 rdesc->enable_reg = di->vol_reg;
289 rdesc->enable_mask = VSEL_BUCK_EN;
290 rdesc->min_uV = di->vsel_min;
291 rdesc->uV_step = di->vsel_step;
292 rdesc->vsel_reg = di->vol_reg;
293 rdesc->vsel_mask = VSEL_NSEL_MASK;
294 rdesc->owner = THIS_MODULE;
295
Axel Linb15f5f72013-09-03 14:27:56 +0800296 di->rdev = devm_regulator_register(di->dev, &di->desc, config);
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930297 return PTR_ERR_OR_ZERO(di->rdev);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700298}
299
Krzysztof Kozlowski121b5672015-01-05 10:04:19 +0100300static const struct regmap_config fan53555_regmap_config = {
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700301 .reg_bits = 8,
302 .val_bits = 8,
303};
304
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200305static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev,
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100306 struct device_node *np,
307 const struct regulator_desc *desc)
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200308{
309 struct fan53555_platform_data *pdata;
310 int ret;
311 u32 tmp;
312
313 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
314 if (!pdata)
315 return NULL;
316
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100317 pdata->regulator = of_get_regulator_init_data(dev, np, desc);
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200318
319 ret = of_property_read_u32(np, "fcs,suspend-voltage-selector",
320 &tmp);
321 if (!ret)
322 pdata->sleep_vsel_id = tmp;
323
324 return pdata;
325}
326
327static const struct of_device_id fan53555_dt_ids[] = {
328 {
329 .compatible = "fcs,fan53555",
Heiko Stuebneree309282014-09-16 21:22:54 +0200330 .data = (void *)FAN53555_VENDOR_FAIRCHILD
331 }, {
332 .compatible = "silergy,syr827",
333 .data = (void *)FAN53555_VENDOR_SILERGY,
334 }, {
335 .compatible = "silergy,syr828",
336 .data = (void *)FAN53555_VENDOR_SILERGY,
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200337 },
338 { }
339};
340MODULE_DEVICE_TABLE(of, fan53555_dt_ids);
341
Bill Pembertona5023572012-11-19 13:22:22 -0500342static int fan53555_regulator_probe(struct i2c_client *client,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700343 const struct i2c_device_id *id)
344{
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200345 struct device_node *np = client->dev.of_node;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700346 struct fan53555_device_info *di;
347 struct fan53555_platform_data *pdata;
348 struct regulator_config config = { };
349 unsigned int val;
350 int ret;
351
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100352 di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info),
353 GFP_KERNEL);
354 if (!di)
355 return -ENOMEM;
356
Jingoo Handff91d02013-07-30 17:20:47 +0900357 pdata = dev_get_platdata(&client->dev);
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200358 if (!pdata)
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100359 pdata = fan53555_parse_dt(&client->dev, np, &di->desc);
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200360
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700361 if (!pdata || !pdata->regulator) {
362 dev_err(&client->dev, "Platform data not found!\n");
363 return -ENODEV;
364 }
365
Axel Line13426b2014-09-18 21:49:38 +0800366 di->regulator = pdata->regulator;
Heiko Stuebneree309282014-09-16 21:22:54 +0200367 if (client->dev.of_node) {
368 const struct of_device_id *match;
369
370 match = of_match_device(of_match_ptr(fan53555_dt_ids),
371 &client->dev);
372 if (!match)
373 return -ENODEV;
374
Heiko Stübner6f341632014-09-17 13:21:48 +0200375 di->vendor = (unsigned long) match->data;
Heiko Stuebneree309282014-09-16 21:22:54 +0200376 } else {
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200377 /* if no ramp constraint set, get the pdata ramp_delay */
378 if (!di->regulator->constraints.ramp_delay) {
379 int slew_idx = (pdata->slew_rate & 0x7)
380 ? pdata->slew_rate : 0;
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200381
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200382 di->regulator->constraints.ramp_delay
383 = slew_rates[slew_idx];
384 }
Heiko Stuebneree309282014-09-16 21:22:54 +0200385
386 di->vendor = id->driver_data;
Heiko Stuebnerdd7e71f2014-09-16 17:54:01 +0200387 }
388
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700389 di->regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config);
390 if (IS_ERR(di->regmap)) {
391 dev_err(&client->dev, "Failed to allocate regmap!\n");
392 return PTR_ERR(di->regmap);
393 }
394 di->dev = &client->dev;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700395 i2c_set_clientdata(client, di);
396 /* Get chip ID */
397 ret = regmap_read(di->regmap, FAN53555_ID1, &val);
398 if (ret < 0) {
399 dev_err(&client->dev, "Failed to get chip ID!\n");
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530400 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700401 }
402 di->chip_id = val & DIE_ID;
403 /* Get chip revision */
404 ret = regmap_read(di->regmap, FAN53555_ID2, &val);
405 if (ret < 0) {
406 dev_err(&client->dev, "Failed to get chip Rev!\n");
Sachin Kamat145fe1e2014-02-18 16:11:04 +0530407 return ret;
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700408 }
409 di->chip_rev = val & DIE_REV;
410 dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n",
411 di->chip_id, di->chip_rev);
412 /* Device init */
413 ret = fan53555_device_setup(di, pdata);
414 if (ret < 0) {
415 dev_err(&client->dev, "Failed to setup device!\n");
416 return ret;
417 }
418 /* Register regulator */
419 config.dev = di->dev;
420 config.init_data = di->regulator;
421 config.regmap = di->regmap;
422 config.driver_data = di;
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200423 config.of_node = np;
424
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700425 ret = fan53555_regulator_register(di, &config);
426 if (ret < 0)
427 dev_err(&client->dev, "Failed to register regulator!\n");
428 return ret;
429
430}
431
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700432static const struct i2c_device_id fan53555_id[] = {
Heiko Stuebneree309282014-09-16 21:22:54 +0200433 {
434 .name = "fan53555",
435 .driver_data = FAN53555_VENDOR_FAIRCHILD
436 }, {
437 .name = "syr82x",
438 .driver_data = FAN53555_VENDOR_SILERGY
439 },
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700440 { },
441};
Javier Martinez Canillase80c47b2015-07-30 18:18:40 +0200442MODULE_DEVICE_TABLE(i2c, fan53555_id);
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700443
444static struct i2c_driver fan53555_regulator_driver = {
445 .driver = {
446 .name = "fan53555-regulator",
Heiko Stuebner91f23d82014-09-16 21:22:53 +0200447 .of_match_table = of_match_ptr(fan53555_dt_ids),
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700448 },
449 .probe = fan53555_regulator_probe,
Yunfan Zhang49d8c592012-09-08 03:52:18 -0700450 .id_table = fan53555_id,
451};
452
453module_i2c_driver(fan53555_regulator_driver);
454
455MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>");
456MODULE_DESCRIPTION("FAN53555 regulator driver");
457MODULE_LICENSE("GPL v2");