2 * Copyright (C) 2013 Spreadtrum Communications Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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.
15 * 0.2 unprotect dcdc/ldo before turn on and off
16 * remove dcdc/ldo calibration
21 #include <linux/init.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/version.h>
26 #include <linux/spinlock.h>
27 #include <linux/debugfs.h>
28 #include <linux/slab.h>
29 #include <linux/sort.h>
30 #include <linux/delay.h>
31 #include <linux/err.h>
33 #include <linux/platform_device.h>
36 #include <linux/of_device.h>
37 #include <linux/of_address.h>
38 #include <linux/regulator/of_regulator.h>
40 #include <linux/regulator/consumer.h>
41 #include <linux/regulator/driver.h>
42 #include <linux/regulator/machine.h>
43 #include <linux/i2c.h>
44 #include <soc/sprd/sci.h>
45 #include <soc/sprd/sci_glb_regs.h>
46 #include <soc/sprd/adi.h>
47 #include <soc/sprd/adc.h>
50 #define debug(format, arg...) pr_info("regu: " "@@@%s: " format, __func__, ## arg)
51 #define debug0(format, arg...) //pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
52 #define debug2(format, arg...) pr_debug("regu: " "@@@%s: " format, __func__, ## arg)
53 #define FAIRCHILD_DCDC_NAME "fairchild_fan53555"
55 FAN53555_REG_VSEL_0 = 0x0,
56 FAN53555_REG_VSEL_1 = 0x1,
57 FAN53555_REG_CONTROL = 0x2,
58 FAN53555_REG_ID1 = 0x3,
59 FAN53555_REG_ID2 = 0x4,
60 FAN53555_REG_MONITOR = 0x5,
61 FAN53555_REG_MAX_NUM = FAN53555_REG_MONITOR
72 struct fan5xx_regulator_info {
73 struct regulator_desc desc;
74 struct regulator_init_data *init_data;
75 struct fan5xx_regs regs;
76 struct i2c_client *client;
78 static struct dentry *debugfs_root = NULL;
79 static atomic_t idx = ATOMIC_INIT(1); /* 0: dummy */
80 static int fan5xx_write_reg(struct fan5xx_regulator_info *fan5xx_dcdc, u8 addr,
83 struct i2c_msg msgs[1] = { 0 };
85 int ret = -1, msg_len = 0;
90 msgs[0].addr = fan5xx_dcdc->client->addr;
93 msgs[0].len = ARRAY_SIZE(buf);
95 msg_len = ARRAY_SIZE(msgs);
97 ret = i2c_transfer(fan5xx_dcdc->client->adapter, msgs, msg_len);
99 pr_err("%s i2c write error, ret %d\n", __func__, ret);
106 static int fan5xx_read_reg(struct fan5xx_regulator_info *fan5xx_dcdc,
107 u8 reg_addr, u8 * pdata)
109 int ret = 0, msg_len = 0;
110 u8 *write_buf = ®_addr;
111 u8 *read_buf = pdata;
113 struct i2c_msg msgs[] = {
115 .addr = fan5xx_dcdc->client->addr,
116 .flags = 0, //i2c write
121 .addr = fan5xx_dcdc->client->addr,
122 .flags = I2C_M_RD, //i2c read
127 msg_len = ARRAY_SIZE(msgs);
129 ret = i2c_transfer(fan5xx_dcdc->client->adapter, msgs, msg_len);
130 if (ret != msg_len) {
131 pr_err("%s i2c read error, ret %d, msg_len %d\n", __func__, ret,
140 static int fan5xx_dcdc_turn_on(struct regulator_dev *rdev)
142 struct fan5xx_regulator_info *fan5xx_dcdc = rdev_get_drvdata(rdev);
143 struct fan5xx_regs *regs = &fan5xx_dcdc->regs;
148 static int fan5xx_dcdc_turn_off(struct regulator_dev *rdev)
150 struct fan5xx_regulator_info *fan5xx_dcdc = rdev_get_drvdata(rdev);
151 struct fan5xx_regs *regs = &fan5xx_dcdc->regs;
156 static int fan5xx_dcdc_is_on(struct regulator_dev *rdev)
158 struct fan5xx_regulator_info *fan5xx_dcdc = rdev_get_drvdata(rdev);
159 struct fan5xx_regs *regs = &fan5xx_dcdc->regs;
164 static int fan5xx_dcdc_get_voltage(struct regulator_dev *rdev)
166 struct fan5xx_regulator_info *fan5xx_dcdc = rdev_get_drvdata(rdev);
167 struct fan5xx_regs *regs = &fan5xx_dcdc->regs;
168 int vol_uV = 0, ret = -EINVAL;
169 u8 reg_addr = (u8) (regs->vsel);
170 u8 reg_val = 0, shft = __ffs(regs->vsel_msk);
172 ret = fan5xx_read_reg(fan5xx_dcdc, reg_addr, ®_val);
174 debug("read reg(%#x) data error! ret = %d\n", reg_addr, ret);
178 reg_val = (reg_val & regs->vsel_msk) >> shft;
179 vol_uV = regs->min_uV + reg_val * regs->step_uV;
181 debug("vddbigarm get voltage %d\n", vol_uV);
183 return vol_uV; /* uV */
186 static int fan5xx_dcdc_set_voltage(struct regulator_dev *rdev, int min_uV,
187 int max_uV, unsigned *selector)
189 struct fan5xx_regulator_info *fan5xx_dcdc = rdev_get_drvdata(rdev);
190 struct fan5xx_regs *regs = &fan5xx_dcdc->regs;
191 int uV = min_uV, ret = -EINVAL;
192 u8 reg_addr = (u8) (regs->vsel);
193 u8 reg_val = 0, shft = __ffs(regs->vsel_msk);
194 int old_vol = rdev->desc->ops->get_voltage(rdev);
196 debug("vddbigarm set voltage, %d(uV) - %d(uV)\n", min_uV, max_uV);
198 if (uV < regs->min_uV)
201 ret = fan5xx_read_reg(fan5xx_dcdc, reg_addr, ®_val);
203 debug("read reg(%#x) data error! ret = %d\n", reg_addr, ret);
206 debug0("read reg(%#x) data(%#x)\n", reg_addr, reg_val);
208 uV = (int)(uV - (int)regs->min_uV) / regs->step_uV;
209 if (uV > (regs->vsel_msk >> shft))
210 uV = regs->vsel_msk >> shft;
212 reg_val &= ~regs->vsel_msk;
213 reg_val |= (uV << shft);
214 ret = fan5xx_write_reg(fan5xx_dcdc, reg_addr, reg_val);
216 debug("write reg(%#x) data(%#x) error! ret = %d\n", reg_addr,
220 debug("write reg(%#x) value(%#x)\n", reg_addr, reg_val);
222 /* dcdc boost delay */
223 if (min_uV > old_vol) {
225 /* FIXME: for dcdc fan53555, each step slew_rate[i] uV takes 1us */
226 //int slew_rate_list[8] = {64000, 32000, 16000, 8000, 4000, 2000, 1000, 500} /* unit: uV/us */;
230 fan5xx_read_reg(fan5xx_dcdc, FAN53555_REG_CONTROL,
233 debug("read reg(0x2) data error! ret = %d\n", ret);
236 debug0("read reg(0x2) data(%#x)\n", reg_val);
237 reg_val = (reg_val & 0x70) >> 4;
238 slew_rate = ((64 * 1000) << reg_val);
239 dly = (min_uV - old_vol) / slew_rate;
240 //dly = (min_uV - old_vol) / slew_rate_list[reg_val];
241 debug("slew_rate %d(uV/us), delay_time %d(us)\n", slew_rate,
250 static struct regulator_ops fan5xx_dcdc_ops = {
251 .enable = fan5xx_dcdc_turn_on,
252 .disable = fan5xx_dcdc_turn_off,
253 .is_enabled = fan5xx_dcdc_is_on,
254 .set_voltage = fan5xx_dcdc_set_voltage,
255 .get_voltage = fan5xx_dcdc_get_voltage,
258 static int debugfs_voltage_get(void *data, u64 * val)
260 struct regulator_dev *rdev = data;
262 if (0 == strcmp(rdev->desc->name, "vddbigarm"))
263 if (rdev->desc->ops->get_voltage)
264 *val = rdev->desc->ops->get_voltage(rdev);
270 static int debugfs_enable_get(void *data, u64 * val)
272 struct regulator_dev *rdev = data;
273 if (rdev && rdev->desc->ops->is_enabled)
274 *val = rdev->desc->ops->is_enabled(rdev);
280 static int debugfs_enable_set(void *data, u64 val)
282 struct regulator_dev *rdev = data;
283 if (rdev && rdev->desc->ops->enable)
284 (val) ? rdev->desc->ops->enable(rdev)
285 : rdev->desc->ops->disable(rdev);
289 static int debugfs_voltage_set(void *data, u64 val)
291 struct regulator_dev *rdev = data;
293 if (rdev && rdev->desc->ops->set_voltage) {
294 if (0 != strcmp(rdev->desc->name, "vddbigarm"))
295 min_uV = (u32) val *1000;
298 min_uV += rdev->constraints->uV_offset;
299 rdev->desc->ops->set_voltage(rdev, min_uV, min_uV, 0);
304 DEFINE_SIMPLE_ATTRIBUTE(fops_enable,
305 debugfs_enable_get, debugfs_enable_set, "%llu\n");
306 DEFINE_SIMPLE_ATTRIBUTE(fops_ldo,
307 debugfs_voltage_get, debugfs_voltage_set, "%llu\n");
308 static void ext_dcdc_init_debugfs(struct regulator_dev *rdev)
310 struct fan5xx_regulator_info *fan5xx_dcdc = rdev_get_drvdata(rdev);
311 struct dentry *regu_debugfs = NULL;
313 regu_debugfs = debugfs_create_dir(rdev->desc->name, debugfs_root);
314 if (IS_ERR_OR_NULL(regu_debugfs)) {
315 pr_warn("Failed to create (%s) debugfs directory\n",
317 rdev->debugfs = NULL;
321 debugfs_create_file("enable", S_IRUGO | S_IWUSR,
322 regu_debugfs, rdev, &fops_enable);
324 debugfs_create_file("voltage", S_IRUGO | S_IWUSR,
325 regu_debugfs, rdev, &fops_ldo);
328 static const unsigned short fan5xx_addr_list[] = {
329 0x60, /* 0xc0 >> 1 */
333 static const struct i2c_device_id fan53555_i2c_id[] = {
334 {FAIRCHILD_DCDC_NAME, 0},
339 static const struct of_device_id fan53555_of_match[] = {
340 {.compatible = "fairchild,fairchild_fan53555",},
344 static inline int __strcmp(const char *cs, const char *ct)
349 return strcmp(cs, ct);
352 static struct regulator_consumer_supply *set_supply_map(struct device *dev,
353 const char *supply_name,
356 char **map = (char **)dev_get_platdata(dev);
358 struct regulator_consumer_supply *consumer_supplies = NULL;
360 if (!supply_name || !(map && map[0]))
363 for (i = 0; map[i] || map[i + 1]; i++) {
364 if (map[i] && 0 == strcmp(map[i], supply_name))
368 /* i++; *//* Do not skip supply name */
370 for (n = 0; map[i + n]; n++) ;
373 debug0("supply %s consumers %d - %d\n", supply_name, i, n);
375 kzalloc(n * sizeof(*consumer_supplies), GFP_KERNEL);
376 BUG_ON(!consumer_supplies);
377 for (n = 0; map[i]; i++, n++) {
378 consumer_supplies[n].supply = map[i];
383 return consumer_supplies;
386 static int fan53555_regulator_parse_dt(struct device *dev,
387 struct device_node *np,
388 struct fan5xx_regulator_info *desc,
389 struct regulator_consumer_supply *supply,
392 /*struct sci_regulator_regs *regs = &desc->regs; */
393 struct fan5xx_regs *regs = &desc->regs;
397 int type = 0, cnt = 0, ret = 0;
399 if (!dev || !np || !desc || !supply) {
403 desc->desc.name = np->name;
404 desc->desc.id = (atomic_inc_return(&idx) - 1);
405 desc->desc.type = REGULATOR_VOLTAGE;
406 desc->desc.owner = THIS_MODULE;
408 supply[0].dev_name = NULL;
409 supply[0].supply = np->name;
410 desc->init_data = of_get_regulator_init_data(dev, np);
412 || 0 != __strcmp(desc->init_data->constraints.name, np->name)) {
414 "failed to parse regulator(%s) init data! \n",
419 desc->init_data->supply_regulator = 0;
420 desc->init_data->constraints.valid_modes_mask =
421 REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
422 desc->init_data->constraints.valid_ops_mask =
423 REGULATOR_CHANGE_MODE | REGULATOR_CHANGE_STATUS |
424 REGULATOR_CHANGE_VOLTAGE;
425 desc->init_data->num_consumer_supplies = sz;
427 desc->init_data->consumer_supplies =
428 set_supply_map(dev, desc->desc.name,
429 &desc->init_data->num_consumer_supplies);
431 if (!desc->init_data->consumer_supplies)
432 desc->init_data->consumer_supplies = supply;
434 /* Fill struct sci_regulator_regs variable desc->regs */
436 regs->min_uV = desc->init_data->constraints.min_uV;
439 of_property_read_u32(np, "regulator-step-microvolt", &tmp_val_u32);
441 regs->step_uV = tmp_val_u32;
444 of_property_read_u32(np, "regulator-default-microvolt",
447 regs->vol_def = tmp_val_u32;
449 debug("[%d] %s type %d, range %d(uV) - %d(uV), step %d(uV) \n",
450 (idx.counter - 1), np->name, type,
451 desc->init_data->constraints.min_uV,
452 desc->init_data->constraints.max_uV, regs->step_uV);
457 static int fan53555_parse_dt(struct device *dev)
459 struct device_node *dev_np = dev->of_node;
460 struct device_node *child_np;
461 for_each_child_of_node(dev_np, child_np) {
466 static int fan53335_probe(struct i2c_client *client,
467 const struct i2c_device_id *id)
472 struct fan5xx_regulator_info *fan5xx_dcdc = NULL;
473 struct regulator_dev *rdev = NULL;
474 struct regulator_consumer_supply consumer_supplies_default[1] = { };
475 struct regulator_config config = { };
477 struct device *dev = &client->dev;
478 struct device_node *dev_np = dev->of_node;
479 struct device_node *child_np;
480 struct regulator_init_data init_data = {
481 .supply_regulator = 0,
486 pr_info("%s -- enter probe -->\n", __func__);
488 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
492 fan5xx_dcdc = kzalloc(sizeof(*fan5xx_dcdc), GFP_KERNEL);
494 pr_err("failed allocate memory for fan5xx_regulator_info!\n");
497 for_each_child_of_node(dev_np, child_np) {
499 fan53555_regulator_parse_dt(dev, child_np, fan5xx_dcdc,
500 consumer_supplies_default,
502 (consumer_supplies_default));
505 fan5xx_dcdc->client = client;
506 i2c_set_clientdata(client, fan5xx_dcdc);
507 fan5xx_dcdc->regs.opt = 18; /* FAN53555 BUC 18x */
508 fan5xx_dcdc->regs.vsel = FAN53555_REG_VSEL_0;
509 fan5xx_dcdc->regs.vsel_msk = 0x3F;
511 /* read fan53555 chipid */
512 ret = fan5xx_read_reg(fan5xx_dcdc, FAN53555_REG_ID1, &chipid); /* chipid = 0x88 */
514 pr_info("%s fan53555 chipid %#x\n", __func__, chipid);
516 pr_err("%s fan53555 read chipid error!\n", __func__);
518 //fan5xx_write_reg(fan5xx_dcdc, FAN53555_REG_CONTROL, 0x0); /* set slew rate to 64mV/us */
520 fan5xx_dcdc->desc.type = REGULATOR_VOLTAGE;
521 fan5xx_dcdc->desc.ops = &fan5xx_dcdc_ops;
523 config.dev = &client->dev;
524 config.init_data = fan5xx_dcdc->init_data; //&init_data;
525 config.driver_data = fan5xx_dcdc;
526 config.of_node = NULL;
527 rdev = regulator_register(&fan5xx_dcdc->desc, &config);
528 if (!IS_ERR_OR_NULL(rdev)) {
529 pr_info("%s regulator vddbigarm ok!\n", __func__);
530 rdev->reg_data = fan5xx_dcdc;
531 ext_dcdc_init_debugfs(rdev);
532 fan5xx_dcdc->desc.ops->set_voltage(rdev,
533 fan5xx_dcdc->regs.vol_def,
534 fan5xx_dcdc->regs.vol_def,
538 pr_info("%s -- exit probe -->\n", __func__);
543 static int fan53335_remove(struct i2c_client *client)
545 struct fan5xx_regulator_info *fan5xx_dcdc = i2c_get_clientdata(client);
555 static struct i2c_driver fan5xx_i2c_driver = {
556 .probe = fan53335_probe,
557 .remove = fan53335_remove,
558 .id_table = fan53555_i2c_id,
560 .name = FAIRCHILD_DCDC_NAME,
561 .owner = THIS_MODULE,
562 .of_match_table = fan53555_of_match,
566 static int __init fan53555_regulator_init(void)
568 return i2c_add_driver(&fan5xx_i2c_driver);
571 static void __exit fan53555_regulator_exit(void)
573 i2c_del_driver(&fan5xx_i2c_driver);
576 subsys_initcall(fan53555_regulator_init);
577 module_exit(fan53555_regulator_exit);
578 MODULE_DESCRIPTION("fairchild fan53555 regulator driver");
579 MODULE_LICENSE("GPL");