2 * Regulator driver for syr82x DCDC chip for rk32xx
4 * Copyright (C) 2010, 2011 ROCKCHIP, Inc.
6 * Based on syr82x.c that is work by zhangqing<zhangqing@rock-chips.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/bug.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/kernel.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/mutex.h>
22 #include <linux/mfd/core.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_gpio.h>
29 #include <linux/of_device.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/regmap.h>
36 #define DBG(x...) printk(KERN_INFO x)
41 #define DBG_INFO(x...) printk(KERN_INFO x)
43 #define DBG_INFO(x...)
47 #define SYR82X_SPEED 200*1000
48 #define syr82x_NUM_REGULATORS 1
53 struct i2c_client *i2c;
55 struct regulator_dev **rdev;
59 unsigned int dcdc_slp_voltage[3]; /* buckx_voltage in uV */
61 struct regmap *regmap;
64 struct syr82x_regulator {
66 struct regulator_desc *desc;
67 struct regulator_dev *rdev;
72 struct regulator_init_data *syr82x_init_data[syr82x_NUM_REGULATORS];
73 struct device_node *of_node[syr82x_NUM_REGULATORS];
75 unsigned int dcdc_slp_voltage[3]; /* buckx_voltage in uV */
79 struct syr82x_regulator_subdev {
81 struct regulator_init_data *initdata;
82 struct device_node *reg_node;
85 struct syr82x_platform_data {
88 struct syr82x_regulator_subdev *regulators;
91 unsigned int dcdc_slp_voltage[3]; /* buckx_voltage in uV */
94 struct syr82x *g_syr82x;
96 static int syr82x_reg_read(struct syr82x *syr82x, u8 reg);
97 static int syr82x_set_bits(struct syr82x *syr82x, u8 reg, u16 mask, u16 val);
100 #define SYR82X_BUCK1_SET_VOL_BASE 0x00
101 #define SYR82X_BUCK1_SLP_VOL_BASE 0x01
102 #define SYR82X_CONTR_REG1 0x02
103 #define SYR82X_ID1_REG 0x03
104 #define SYR82X_ID2_REG 0x04
105 #define SYR82X_CONTR_REG2 0x05
107 #define BUCK_VOL_MASK 0x3f
108 #define VOL_MIN_IDX 0x00
109 #define VOL_MAX_IDX 0x3f
111 const static int buck_voltage_map[] = {
112 712500, 725000, 737500,750000, 762500,775000,787500,800000,
113 812500, 825000, 837500,850000, 862500,875000,887500,900000,
114 912500, 925000, 937500,950000, 962500,975000,987500,1000000,
115 1012500, 1025000, 1037500,1050000, 1062500,1075000,1087500,1100000,
116 1112500, 1125000, 1137500,1150000, 1162500,1175000,1187500,1200000,
117 1212500, 1225000, 1237500,1250000, 1262500,1275000,1287500,1300000,
118 1312500, 1325000, 1337500,1350000, 1362500,1375000,1387500,1400000,
119 1412500, 1425000, 1437500,1450000, 1462500,1475000,1487500,1500000,
122 static int syr82x_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
124 if (index >= ARRAY_SIZE(buck_voltage_map))
126 return buck_voltage_map[index];
128 static int syr82x_dcdc_is_enabled(struct regulator_dev *dev)
130 struct syr82x *syr82x = rdev_get_drvdata(dev);
133 val = syr82x_reg_read(syr82x, SYR82X_BUCK1_SET_VOL_BASE);
142 static int syr82x_dcdc_enable(struct regulator_dev *dev)
144 struct syr82x *syr82x = rdev_get_drvdata(dev);
147 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0x80);
150 static int syr82x_dcdc_disable(struct regulator_dev *dev)
152 struct syr82x *syr82x = rdev_get_drvdata(dev);
154 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0);
156 static int syr82x_dcdc_get_voltage(struct regulator_dev *dev)
158 struct syr82x *syr82x = rdev_get_drvdata(dev);
161 reg = syr82x_reg_read(syr82x,SYR82X_BUCK1_SET_VOL_BASE);
162 reg &= BUCK_VOL_MASK;
163 val = buck_voltage_map[reg];
166 static int syr82x_dcdc_set_voltage(struct regulator_dev *dev,
167 int min_uV, int max_uV,unsigned *selector)
169 struct syr82x *syr82x = rdev_get_drvdata(dev);
170 const int *vol_map = buck_voltage_map;
174 if (min_uV < vol_map[VOL_MIN_IDX] ||
175 min_uV > vol_map[VOL_MAX_IDX])
178 for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
179 if (vol_map[val] >= min_uV)
183 if (vol_map[val] > max_uV)
184 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
186 ret = syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE ,BUCK_VOL_MASK, val);
188 printk("###################WARNING:set voltage is error!voltage set is %d mv %d\n",vol_map[val],ret);
193 static unsigned int syr82x_dcdc_get_mode(struct regulator_dev *dev)
195 struct syr82x *syr82x = rdev_get_drvdata(dev);
198 val = syr82x_reg_read(syr82x, SYR82X_BUCK1_SET_VOL_BASE);
204 return REGULATOR_MODE_FAST;
206 return REGULATOR_MODE_NORMAL;
209 static int syr82x_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
211 struct syr82x *syr82x = rdev_get_drvdata(dev);
216 case REGULATOR_MODE_FAST:
217 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SET_VOL_BASE, mask, mask);
218 case REGULATOR_MODE_NORMAL:
219 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0);
221 printk("error:dcdc_syr82x only auto and pwm mode\n");
225 static int syr82x_dcdc_set_voltage_time_sel(struct regulator_dev *dev, unsigned int old_selector,
226 unsigned int new_selector)
228 int old_volt, new_volt;
230 old_volt = syr82x_dcdc_list_voltage(dev, old_selector);
234 new_volt = syr82x_dcdc_list_voltage(dev, new_selector);
238 return DIV_ROUND_UP(abs(old_volt - new_volt)*4, 10000);
240 static int syr82x_dcdc_suspend_enable(struct regulator_dev *dev)
242 struct syr82x *syr82x = rdev_get_drvdata(dev);
245 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0x80);
248 static int syr82x_dcdc_suspend_disable(struct regulator_dev *dev)
250 struct syr82x *syr82x = rdev_get_drvdata(dev);
252 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
254 static int syr82x_dcdc_set_sleep_voltage(struct regulator_dev *dev,
257 struct syr82x *syr82x = rdev_get_drvdata(dev);
258 const int *vol_map = buck_voltage_map;
262 if (uV < vol_map[VOL_MIN_IDX] ||
263 uV > vol_map[VOL_MAX_IDX])
266 for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
267 if (vol_map[val] >= uV)
271 if (vol_map[val] > uV)
272 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
273 ret = syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE ,BUCK_VOL_MASK, val);
278 static int syr82x_dcdc_set_suspend_mode(struct regulator_dev *dev, unsigned int mode)
280 struct syr82x *syr82x = rdev_get_drvdata(dev);
285 case REGULATOR_MODE_FAST:
286 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE, mask, mask);
287 case REGULATOR_MODE_NORMAL:
288 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
290 printk("error:dcdc_syr82x only auto and pwm mode\n");
295 static struct regulator_ops syr82x_dcdc_ops = {
296 .set_voltage = syr82x_dcdc_set_voltage,
297 .get_voltage = syr82x_dcdc_get_voltage,
298 .list_voltage= syr82x_dcdc_list_voltage,
299 .is_enabled = syr82x_dcdc_is_enabled,
300 .enable = syr82x_dcdc_enable,
301 .disable = syr82x_dcdc_disable,
302 .get_mode = syr82x_dcdc_get_mode,
303 .set_mode = syr82x_dcdc_set_mode,
304 .set_suspend_voltage = syr82x_dcdc_set_sleep_voltage,
305 .set_suspend_enable = syr82x_dcdc_suspend_enable,
306 .set_suspend_disable = syr82x_dcdc_suspend_disable,
307 .set_suspend_mode = syr82x_dcdc_set_suspend_mode,
308 .set_voltage_time_sel = syr82x_dcdc_set_voltage_time_sel,
310 static struct regulator_desc regulators[] = {
315 .ops = &syr82x_dcdc_ops,
316 .n_voltages = ARRAY_SIZE(buck_voltage_map),
317 .type = REGULATOR_VOLTAGE,
318 .owner = THIS_MODULE,
322 static int syr82x_i2c_read(struct i2c_client *i2c, char reg, int count, u16 *dest)
325 struct i2c_adapter *adap;
326 struct i2c_msg msgs[2];
336 msgs[0].addr = i2c->addr;
338 msgs[0].flags = i2c->flags;
340 msgs[0].scl_rate = SYR82X_SPEED;
342 msgs[1].buf = (u8 *)dest;
343 msgs[1].addr = i2c->addr;
344 msgs[1].flags = i2c->flags | I2C_M_RD;
346 msgs[1].scl_rate = SYR82X_SPEED;
347 ret = i2c_transfer(adap, msgs, 2);
349 DBG("***run in %s %d msgs[1].buf = %d\n",__FUNCTION__,__LINE__,*(msgs[1].buf));
354 static int syr82x_i2c_write(struct i2c_client *i2c, char reg, int count, const u16 src)
358 struct i2c_adapter *adap;
371 msg.addr = i2c->addr;
372 msg.buf = &tx_buf[0];
374 msg.flags = i2c->flags;
375 msg.scl_rate = SYR82X_SPEED;
377 ret = i2c_transfer(adap, &msg, 1);
381 static int syr82x_reg_read(struct syr82x *syr82x, u8 reg)
386 mutex_lock(&syr82x->io_lock);
388 ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &val);
390 DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
392 mutex_unlock(&syr82x->io_lock);
395 mutex_unlock(&syr82x->io_lock);
400 static int syr82x_set_bits(struct syr82x *syr82x, u8 reg, u16 mask, u16 val)
405 mutex_lock(&syr82x->io_lock);
407 ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
408 DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
410 mutex_unlock(&syr82x->io_lock);
413 tmp = (tmp & ~mask) | val;
414 ret = syr82x_i2c_write(syr82x->i2c, reg, 1, tmp);
415 DBG("reg write 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
417 mutex_unlock(&syr82x->io_lock);
420 ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
421 DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
423 mutex_unlock(&syr82x->io_lock);
426 mutex_unlock(&syr82x->io_lock);
432 static struct of_device_id syr82x_of_match[] = {
433 { .compatible = "silergy,syr82x"},
436 MODULE_DEVICE_TABLE(of, syr82x_of_match);
439 static struct of_regulator_match syr82x_reg_matches[] = {
440 { .name = "syr82x_dcdc1" ,.driver_data = (void *)0},
443 static struct syr82x_board *syr82x_parse_dt(struct syr82x *syr82x)
445 struct syr82x_board *pdata;
446 struct device_node *regs;
447 struct device_node *syr82x_np;
449 DBG("%s,line=%d\n", __func__,__LINE__);
451 syr82x_np = of_node_get(syr82x->dev->of_node);
453 printk("could not find pmic sub-node\n");
457 regs = of_find_node_by_name(syr82x_np, "regulators");
460 count = of_regulator_match(syr82x->dev, regs, syr82x_reg_matches,
461 syr82x_NUM_REGULATORS);
463 pdata = devm_kzalloc(syr82x->dev, sizeof(*pdata), GFP_KERNEL);
466 pdata->syr82x_init_data[0] = syr82x_reg_matches[0].init_data;
467 pdata->of_node[0] = syr82x_reg_matches[0].of_node;
473 static struct syr82x_board *syr82x_parse_dt(struct i2c_client *i2c)
479 static int syr82x_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
481 struct syr82x *syr82x;
482 struct syr82x_board *pdev ;
483 const struct of_device_id *match;
484 struct regulator_config config = { };
485 struct regulator_dev *sy_rdev;
486 struct regulator_init_data *reg_data;
487 const char *rail_name = NULL;
490 DBG("%s,line=%d\n", __func__,__LINE__);
492 if (i2c->dev.of_node) {
493 match = of_match_device(syr82x_of_match, &i2c->dev);
495 printk("Failed to find matching dt id\n");
500 syr82x = devm_kzalloc(&i2c->dev,sizeof(struct syr82x), GFP_KERNEL);
501 if (syr82x == NULL) {
506 syr82x->dev = &i2c->dev;
507 i2c_set_clientdata(i2c, syr82x);
510 mutex_init(&syr82x->io_lock);
512 ret = syr82x_reg_read(syr82x,SYR82X_ID1_REG);
513 if ((ret <0) ||(ret ==0xff) ||(ret ==0)){
514 printk("The device is not syr82x %x \n",ret);
518 ret = syr82x_set_bits(syr82x,SYR82X_CONTR_REG1,(1 << 6),(1<<6)); //10mv/2.4us
520 if (syr82x->dev->of_node)
521 pdev = syr82x_parse_dt(syr82x);
524 syr82x->num_regulators = syr82x_NUM_REGULATORS;
525 syr82x->rdev = kcalloc(syr82x_NUM_REGULATORS,sizeof(struct regulator_dev *), GFP_KERNEL);
529 /* Instantiate the regulators */
530 reg_data = pdev->syr82x_init_data[0];
531 config.dev = syr82x->dev;
532 config.driver_data = syr82x;
533 if (syr82x->dev->of_node)
534 config.of_node = pdev->of_node[0];
535 if (reg_data && reg_data->constraints.name)
536 rail_name = reg_data->constraints.name;
538 rail_name = regulators[0].name;
539 reg_data->supply_regulator = rail_name;
541 config.init_data =reg_data;
542 sy_rdev = regulator_register(®ulators[0],&config);
543 if (IS_ERR(sy_rdev)) {
544 printk("failed to register regulator\n");
547 syr82x->rdev[0] = sy_rdev;
555 static int syr82x_i2c_remove(struct i2c_client *i2c)
557 struct syr82x *syr82x = i2c_get_clientdata(i2c);
560 regulator_unregister(syr82x->rdev[0]);
561 i2c_set_clientdata(i2c, NULL);
565 static const struct i2c_device_id syr82x_i2c_id[] = {
570 MODULE_DEVICE_TABLE(i2c, syr82x_i2c_id);
572 static struct i2c_driver syr82x_i2c_driver = {
575 .owner = THIS_MODULE,
576 .of_match_table =of_match_ptr(syr82x_of_match),
578 .probe = syr82x_i2c_probe,
579 .remove = syr82x_i2c_remove,
580 .id_table = syr82x_i2c_id,
583 static int __init syr82x_module_init(void)
586 ret = i2c_add_driver(&syr82x_i2c_driver);
588 pr_err("Failed to register I2C driver: %d\n", ret);
591 subsys_initcall_sync(syr82x_module_init);
593 static void __exit syr82x_module_exit(void)
595 i2c_del_driver(&syr82x_i2c_driver);
597 module_exit(syr82x_module_exit);
599 MODULE_LICENSE("GPL");
600 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
601 MODULE_DESCRIPTION("syr82x PMIC driver");