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 u8 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);
191 static unsigned int syr82x_dcdc_get_mode(struct regulator_dev *dev)
193 struct syr82x *syr82x = rdev_get_drvdata(dev);
196 val = syr82x_reg_read(syr82x, SYR82X_BUCK1_SET_VOL_BASE);
202 return REGULATOR_MODE_FAST;
204 return REGULATOR_MODE_NORMAL;
207 static int syr82x_dcdc_set_mode(struct regulator_dev *dev, unsigned int mode)
209 struct syr82x *syr82x = rdev_get_drvdata(dev);
214 case REGULATOR_MODE_FAST:
215 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SET_VOL_BASE, mask, mask);
216 case REGULATOR_MODE_NORMAL:
217 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SET_VOL_BASE, mask, 0);
219 printk("error:dcdc_syr82x only auto and pwm mode\n");
223 static int syr82x_dcdc_set_voltage_time_sel(struct regulator_dev *dev, unsigned int old_selector,
224 unsigned int new_selector)
226 int old_volt, new_volt;
228 old_volt = syr82x_dcdc_list_voltage(dev, old_selector);
232 new_volt = syr82x_dcdc_list_voltage(dev, new_selector);
236 return DIV_ROUND_UP(abs(old_volt - new_volt)*4, 10000);
238 static int syr82x_dcdc_suspend_enable(struct regulator_dev *dev)
240 struct syr82x *syr82x = rdev_get_drvdata(dev);
243 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0x80);
246 static int syr82x_dcdc_suspend_disable(struct regulator_dev *dev)
248 struct syr82x *syr82x = rdev_get_drvdata(dev);
250 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
252 static int syr82x_dcdc_set_sleep_voltage(struct regulator_dev *dev,
255 struct syr82x *syr82x = rdev_get_drvdata(dev);
256 const int *vol_map = buck_voltage_map;
260 if (uV < vol_map[VOL_MIN_IDX] ||
261 uV > vol_map[VOL_MAX_IDX])
264 for (val = VOL_MIN_IDX; val <= VOL_MAX_IDX; val++){
265 if (vol_map[val] >= uV)
269 if (vol_map[val] > uV)
270 printk("WARNING:this voltage is not support!voltage set is %d mv\n",vol_map[val]);
271 ret = syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE ,BUCK_VOL_MASK, val);
276 static int syr82x_dcdc_set_suspend_mode(struct regulator_dev *dev, unsigned int mode)
278 struct syr82x *syr82x = rdev_get_drvdata(dev);
283 case REGULATOR_MODE_FAST:
284 return syr82x_set_bits(syr82x,SYR82X_BUCK1_SLP_VOL_BASE, mask, mask);
285 case REGULATOR_MODE_NORMAL:
286 return syr82x_set_bits(syr82x, SYR82X_BUCK1_SLP_VOL_BASE, mask, 0);
288 printk("error:dcdc_syr82x only auto and pwm mode\n");
293 static struct regulator_ops syr82x_dcdc_ops = {
294 .set_voltage = syr82x_dcdc_set_voltage,
295 .get_voltage = syr82x_dcdc_get_voltage,
296 .list_voltage= syr82x_dcdc_list_voltage,
297 .is_enabled = syr82x_dcdc_is_enabled,
298 .enable = syr82x_dcdc_enable,
299 .disable = syr82x_dcdc_disable,
300 .get_mode = syr82x_dcdc_get_mode,
301 .set_mode = syr82x_dcdc_set_mode,
302 .set_suspend_voltage = syr82x_dcdc_set_sleep_voltage,
303 .set_suspend_enable = syr82x_dcdc_suspend_enable,
304 .set_suspend_disable = syr82x_dcdc_suspend_disable,
305 .set_suspend_mode = syr82x_dcdc_set_suspend_mode,
306 .set_voltage_time_sel = syr82x_dcdc_set_voltage_time_sel,
308 static struct regulator_desc regulators[] = {
313 .ops = &syr82x_dcdc_ops,
314 .n_voltages = ARRAY_SIZE(buck_voltage_map),
315 .type = REGULATOR_VOLTAGE,
316 .owner = THIS_MODULE,
320 static int syr82x_i2c_read(struct i2c_client *i2c, char reg, int count, u16 *dest)
323 struct i2c_adapter *adap;
324 struct i2c_msg msgs[2];
334 msgs[0].addr = i2c->addr;
336 msgs[0].flags = i2c->flags;
338 msgs[0].scl_rate = SYR82X_SPEED;
340 msgs[1].buf = (u8 *)dest;
341 msgs[1].addr = i2c->addr;
342 msgs[1].flags = i2c->flags | I2C_M_RD;
344 msgs[1].scl_rate = SYR82X_SPEED;
345 ret = i2c_transfer(adap, msgs, 2);
347 DBG("***run in %s %d msgs[1].buf = %d\n",__FUNCTION__,__LINE__,*(msgs[1].buf));
352 static int syr82x_i2c_write(struct i2c_client *i2c, char reg, int count, const u16 src)
356 struct i2c_adapter *adap;
369 msg.addr = i2c->addr;
370 msg.buf = &tx_buf[0];
372 msg.flags = i2c->flags;
373 msg.scl_rate = SYR82X_SPEED;
375 ret = i2c_transfer(adap, &msg, 1);
379 static u8 syr82x_reg_read(struct syr82x *syr82x, u8 reg)
383 mutex_lock(&syr82x->io_lock);
385 syr82x_i2c_read(syr82x->i2c, reg, 1, &val);
387 DBG("reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
389 mutex_unlock(&syr82x->io_lock);
394 static int syr82x_set_bits(struct syr82x *syr82x, u8 reg, u16 mask, u16 val)
399 mutex_lock(&syr82x->io_lock);
401 ret = syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
402 DBG("1 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
403 tmp = (tmp & ~mask) | val;
405 ret = syr82x_i2c_write(syr82x->i2c, reg, 1, tmp);
406 DBG("reg write 0x%02x -> 0x%02x\n", (int)reg, (unsigned)val&0xff);
408 syr82x_i2c_read(syr82x->i2c, reg, 1, &tmp);
409 DBG("2 reg read 0x%02x -> 0x%02x\n", (int)reg, (unsigned)tmp&0xff);
410 mutex_unlock(&syr82x->io_lock);
416 static struct of_device_id syr82x_of_match[] = {
417 { .compatible = "silergy,syr82x"},
420 MODULE_DEVICE_TABLE(of, syr82x_of_match);
423 static struct of_regulator_match syr82x_reg_matches[] = {
424 { .name = "syr82x_dcdc1" ,.driver_data = (void *)0},
427 static struct syr82x_board *syr82x_parse_dt(struct syr82x *syr82x)
429 struct syr82x_board *pdata;
430 struct device_node *regs;
431 struct device_node *syr82x_np;
433 DBG("%s,line=%d\n", __func__,__LINE__);
435 syr82x_np = of_node_get(syr82x->dev->of_node);
437 printk("could not find pmic sub-node\n");
441 regs = of_find_node_by_name(syr82x_np, "regulators");
444 count = of_regulator_match(syr82x->dev, regs, syr82x_reg_matches,
445 syr82x_NUM_REGULATORS);
447 pdata = devm_kzalloc(syr82x->dev, sizeof(*pdata), GFP_KERNEL);
450 pdata->syr82x_init_data[0] = syr82x_reg_matches[0].init_data;
451 pdata->of_node[0] = syr82x_reg_matches[0].of_node;
457 static struct syr82x_board *syr82x_parse_dt(struct i2c_client *i2c)
463 static int syr82x_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
465 struct syr82x *syr82x;
466 struct syr82x_board *pdev ;
467 const struct of_device_id *match;
468 struct regulator_config config = { };
469 struct regulator_dev *sy_rdev;
470 struct regulator_init_data *reg_data;
471 const char *rail_name = NULL;
474 DBG("%s,line=%d\n", __func__,__LINE__);
476 if (i2c->dev.of_node) {
477 match = of_match_device(syr82x_of_match, &i2c->dev);
479 printk("Failed to find matching dt id\n");
484 syr82x = devm_kzalloc(&i2c->dev,sizeof(struct syr82x), GFP_KERNEL);
485 if (syr82x == NULL) {
490 syr82x->dev = &i2c->dev;
491 i2c_set_clientdata(i2c, syr82x);
494 mutex_init(&syr82x->io_lock);
496 ret = syr82x_reg_read(syr82x,SYR82X_ID1_REG);
497 if ((ret <0) ||(ret ==0xff) ||(ret ==0)){
498 printk("The device is not syr82x %x \n",ret);
502 ret = syr82x_set_bits(syr82x,SYR82X_CONTR_REG1,(1 << 6),(1<<6)); //10mv/2.4us
504 if (syr82x->dev->of_node)
505 pdev = syr82x_parse_dt(syr82x);
508 syr82x->num_regulators = syr82x_NUM_REGULATORS;
509 syr82x->rdev = kcalloc(syr82x_NUM_REGULATORS,sizeof(struct regulator_dev *), GFP_KERNEL);
513 /* Instantiate the regulators */
514 reg_data = pdev->syr82x_init_data[0];
515 config.dev = syr82x->dev;
516 config.driver_data = syr82x;
517 if (syr82x->dev->of_node)
518 config.of_node = pdev->of_node[0];
519 if (reg_data && reg_data->constraints.name)
520 rail_name = reg_data->constraints.name;
522 rail_name = regulators[0].name;
523 reg_data->supply_regulator = rail_name;
525 config.init_data =reg_data;
526 sy_rdev = regulator_register(®ulators[0],&config);
527 if (IS_ERR(sy_rdev)) {
528 printk("failed to register regulator\n");
531 syr82x->rdev[0] = sy_rdev;
539 static int syr82x_i2c_remove(struct i2c_client *i2c)
541 struct syr82x *syr82x = i2c_get_clientdata(i2c);
544 regulator_unregister(syr82x->rdev[0]);
545 i2c_set_clientdata(i2c, NULL);
549 static const struct i2c_device_id syr82x_i2c_id[] = {
554 MODULE_DEVICE_TABLE(i2c, syr82x_i2c_id);
556 static struct i2c_driver syr82x_i2c_driver = {
559 .owner = THIS_MODULE,
560 .of_match_table =of_match_ptr(syr82x_of_match),
562 .probe = syr82x_i2c_probe,
563 .remove = syr82x_i2c_remove,
564 .id_table = syr82x_i2c_id,
567 static int __init syr82x_module_init(void)
570 ret = i2c_add_driver(&syr82x_i2c_driver);
572 pr_err("Failed to register I2C driver: %d\n", ret);
575 subsys_initcall_sync(syr82x_module_init);
577 static void __exit syr82x_module_exit(void)
579 i2c_del_driver(&syr82x_i2c_driver);
581 module_exit(syr82x_module_exit);
583 MODULE_LICENSE("GPL");
584 MODULE_AUTHOR("zhangqing <zhangqing@rock-chips.com>");
585 MODULE_DESCRIPTION("syr82x PMIC driver");