2 * bq3060 battery driver
4 * This package is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 #include <linux/module.h>
14 #include <linux/param.h>
15 #include <linux/jiffies.h>
16 #include <linux/workqueue.h>
17 #include <linux/delay.h>
18 #include <linux/platform_device.h>
19 #include <linux/power_supply.h>
20 #include <linux/idr.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <asm/unaligned.h>
24 #include <mach/gpio.h>
26 #define DRIVER_VERSION "1.1.0"
28 #define BQ3060_REG_TEMP 0x08
29 #define BQ3060_REG_VOLT 0x09
30 #define BQ3060_REG_AI 0x0a ///0x14
31 #define BQ3060_REG_STATUS 0x16
32 #define BQ3060_REG_TTE 0x06 ///0x16
33 #define BQ3060_REG_TTF 0x05 //0x18
34 #define BQ3060_REG_TTECP 0x12 //0x26
35 #define BQ3060_REG_DESIGNCAPACITY 0x18 //0x26
37 #define BQ3060_REG_RSOC 0x0B /* Relative State-of-Charge */
38 #define BQ3060_FLAG_CHGS BIT(7)
40 #define BQ3060_REG_CAPACITY 0x0f ///0x0E
41 #define BQ3060_FLAG_DSC BIT(0)
42 #define BQ3060_FLAG_FC BIT(9)
44 #define bq3060_SPEED 200 * 1000
46 #define DC_CHECK_PIN RK29_PIN4_PA1
48 /* manufacturer access defines */
49 #define MANUFACTURER_ACCESS_STATUS 0x0006
50 #define MANUFACTURER_ACCESS_SLEEP 0x0011
52 /* battery status value bits */
53 #define BATTERY_DISCHARGING 0x40
54 #define BATTERY_FULL_CHARGED 0x20
55 #define BATTERY_FULL_DISCHARGED 0x10
59 #define DBG(x...) printk(KERN_INFO x)
61 #define DBG(x...) do { } while (0)
64 /* If the system has several batteries we need a different name for each
67 static DEFINE_MUTEX(battery_mutex);
69 struct bq3060_device_info {
71 struct power_supply bat;
72 struct power_supply ac;
73 struct power_supply usb;
74 struct delayed_work work;
75 unsigned int interval;
76 struct i2c_client *client;
79 static enum power_supply_property bq3060_battery_props[] = {
80 POWER_SUPPLY_PROP_STATUS,
81 POWER_SUPPLY_PROP_PRESENT,
82 POWER_SUPPLY_PROP_VOLTAGE_NOW,
83 POWER_SUPPLY_PROP_CURRENT_NOW,
84 POWER_SUPPLY_PROP_CAPACITY,
85 POWER_SUPPLY_PROP_TEMP,
86 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
87 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
88 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
91 static enum power_supply_property rk29_ac_props[] = {
92 POWER_SUPPLY_PROP_ONLINE,
97 * Common code for bq3060 devices read
99 static int bq3060_read(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
101 struct i2c_adapter *adap=client->adapter;
102 struct i2c_msg msgs[2];
106 msgs[0].addr = client->addr;
107 msgs[0].flags = client->flags;
109 msgs[0].buf = ®_buf;
110 msgs[0].scl_rate = bq3060_SPEED;
112 msgs[1].addr = client->addr;
113 msgs[1].flags = client->flags | I2C_M_RD;
115 msgs[1].buf = (char *)buf;
116 msgs[1].scl_rate = bq3060_SPEED;
118 ret = i2c_transfer(adap, msgs, 2);
120 return (ret == 2)? len : ret;
122 static int bq3060_write(struct i2c_client *client, u8 reg, u8 const buf[], unsigned len)
126 ret = i2c_master_reg8_send(client, reg, buf, (int)len, bq3060_SPEED);
130 * Return the battery temperature in tenths of degree Celsius
131 * Or < 0 if something fails.
133 static int bq3060_battery_temperature(struct bq3060_device_info *di)
138 ret = bq3060_read(di->client,BQ3060_REG_TEMP,buf,2);
140 dev_err(di->dev, "error reading temperature\n");
143 temp = get_unaligned_le16(buf);
145 //#if CONFIG_NO_BATTERY_IC
148 DBG("Enter:%s %d--temp = %d\n",__FUNCTION__,__LINE__,temp);
153 * Return the battery Voltage in milivolts
154 * Or < 0 if something fails.
156 static int bq3060_battery_voltage(struct bq3060_device_info *di)
162 ret = bq3060_read(di->client,BQ3060_REG_VOLT,buf,2);
164 dev_err(di->dev, "error reading voltage\n");
167 volt = get_unaligned_le16(buf);
169 DBG("Enter:%s %d--volt = %d\n",__FUNCTION__,__LINE__,volt);
174 * Return the battery average current
175 * Note that current can be negative signed as well
176 * Or 0 if something fails.
178 static int bq3060_battery_current(struct bq3060_device_info *di)
184 ret = bq3060_read(di->client,BQ3060_REG_AI,buf,2);
186 dev_err(di->dev, "error reading current\n");
190 curr = get_unaligned_le16(buf);
192 curr = 0xFFFF^(curr-1);
195 DBG("Enter:%s %d--curr = %d\n",__FUNCTION__,__LINE__,curr);
200 * Return the battery Relative State-of-Charge
201 * Or < 0 if something fails.
203 static int bq3060_battery_capacity(struct bq3060_device_info *di)
208 int designcapacity=0;
212 ret = bq3060_read(di->client,BQ3060_REG_CAPACITY,buf,2);
214 dev_err(di->dev, "error reading relative State-of-Charge\n");
217 rsoc = get_unaligned_le16(buf);
218 DBG("Enter:%s %d--capacity = %d\n",__FUNCTION__,__LINE__,rsoc);
219 #if CONFIG_NO_BATTERY_IC
223 ret = bq3060_read(di->client,BQ3060_REG_DESIGNCAPACITY,buf,2);
224 designcapacity = get_unaligned_le16(buf);
225 DBG("Enter:%s %d--designcapacity = %d\n",__FUNCTION__,__LINE__,designcapacity);
228 if((rsoc<150)|(designcapacity<=200))
230 rsoc = ((rsoc - 100)*100) / (designcapacity -200);
233 DBG("Enter:%s %d--capacity = %d\n",__FUNCTION__,__LINE__,rsoc);
237 static int bq3060_battery_status(struct bq3060_device_info *di,
238 union power_supply_propval *val)
245 ret = bq3060_read(di->client,BQ3060_REG_STATUS, buf, 2);
247 dev_err(di->dev, "error reading flags\n");
250 flags = get_unaligned_le16(buf);
251 DBG("Enter:%s %d--flags = %x\n",__FUNCTION__,__LINE__,flags);
253 status = POWER_SUPPLY_STATUS_FULL;
254 else if (flags & 0x40 )
255 status = POWER_SUPPLY_STATUS_DISCHARGING;
257 status = POWER_SUPPLY_STATUS_CHARGING;
259 val->intval = status;
264 * Read a time register.
265 * Return < 0 if something fails.
267 static int bq3060_battery_time(struct bq3060_device_info *di, int reg,
268 union power_supply_propval *val)
274 ret = bq3060_read(di->client,reg,buf,2);
276 dev_err(di->dev, "error reading register %02x\n", reg);
279 tval = get_unaligned_le16(buf);
280 DBG("Enter:%s %d--tval=%d\n",__FUNCTION__,__LINE__,tval);
284 val->intval = tval * 60;
285 DBG("Enter:%s %d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
289 #define to_bq3060_device_info(x) container_of((x), \
290 struct bq3060_device_info, bat);
292 static int bq3060_battery_get_property(struct power_supply *psy,
293 enum power_supply_property psp,
294 union power_supply_propval *val)
297 struct bq3060_device_info *di = to_bq3060_device_info(psy);
300 case POWER_SUPPLY_PROP_ONLINE:
303 case POWER_SUPPLY_PROP_STATUS:
304 val->intval = bq3060_battery_status(di, val);
306 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
308 case POWER_SUPPLY_PROP_HEALTH:
309 val->intval = POWER_SUPPLY_HEALTH_GOOD;
311 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
312 case POWER_SUPPLY_PROP_PRESENT:
313 val->intval =1;// bq3060_battery_voltage(di);
316 case POWER_SUPPLY_PROP_CURRENT_NOW:
317 val->intval = bq3060_battery_current(di);
319 case POWER_SUPPLY_PROP_CAPACITY:
320 val->intval = bq3060_battery_capacity(di);
322 case POWER_SUPPLY_PROP_TEMP:
323 val->intval = bq3060_battery_temperature(di);
325 case POWER_SUPPLY_PROP_TECHNOLOGY:
326 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
328 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
329 ret = bq3060_battery_time(di, BQ3060_REG_TTE, val);
331 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
332 ret = bq3060_battery_time(di, BQ3060_REG_TTECP, val);
334 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
335 ret = bq3060_battery_time(di, BQ3060_REG_TTF, val);
344 static int rk29_ac_get_property(struct power_supply *psy,
345 enum power_supply_property psp,
346 union power_supply_propval *val)
350 case POWER_SUPPLY_PROP_ONLINE:
351 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
352 if(gpio_get_value(DC_CHECK_PIN))
357 DBG("%s:%d val->intval = %d\n",__FUNCTION__,__LINE__,val->intval);
367 static void bq3060_powersupply_init(struct bq3060_device_info *di)
369 di->bat.name = "battery";
370 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
371 di->bat.properties = bq3060_battery_props;
372 di->bat.num_properties = ARRAY_SIZE(bq3060_battery_props);
373 di->bat.get_property = bq3060_battery_get_property;
376 di->ac.type = POWER_SUPPLY_TYPE_MAINS;
377 di->ac.properties = rk29_ac_props;
378 di->ac.num_properties = ARRAY_SIZE(rk29_ac_props);
379 di->ac.get_property = rk29_ac_get_property;
381 di->usb.name = "usb";
382 di->usb.type = POWER_SUPPLY_TYPE_USB;
383 di->usb.properties = rk29_ac_props;
384 di->usb.num_properties = ARRAY_SIZE(rk29_ac_props);
385 di->usb.get_property = bq3060_battery_get_property;
389 static void bq3060_battery_update_status(struct bq3060_device_info *di)
391 power_supply_changed(&di->bat);
394 static void bq3060_battery_work(struct work_struct *work)
396 struct bq3060_device_info *di = container_of(work, struct bq3060_device_info, work.work);
397 bq3060_battery_update_status(di);
398 /* reschedule for the next time */
399 schedule_delayed_work(&di->work, di->interval);
402 static int bq3060_battery_probe(struct i2c_client *client,
403 const struct i2c_device_id *id)
405 struct bq3060_device_info *di;
410 di = kzalloc(sizeof(*di), GFP_KERNEL);
412 dev_err(&client->dev, "failed to allocate device info data\n");
416 i2c_set_clientdata(client, di);
417 di->dev = &client->dev;
418 di->bat.name = "bq3060-battery";
420 /* 4 seconds between monotor runs interval */
421 di->interval = msecs_to_jiffies(1 * 1000);
423 gpio_request(DC_CHECK_PIN,"dc_check");
424 gpio_direction_input(DC_CHECK_PIN);
425 bq3060_powersupply_init(di);
428 bq3060_write(di->client,0x00,buf,2);
431 bq3060_write(di->client,0x00,buf,2);
432 retval = power_supply_register(&client->dev, &di->bat);
434 dev_err(&client->dev, "failed to register battery\n");
437 //retval = power_supply_register(&client->dev, &di->usb);
439 dev_err(&client->dev, "failed to register usb battery\n");
442 retval = power_supply_register(&client->dev, &di->ac);
444 dev_err(&client->dev, "failed to register ac adapter\n");
447 INIT_DELAYED_WORK(&di->work, bq3060_battery_work);
448 schedule_delayed_work(&di->work, di->interval);
449 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
459 static int bq3060_battery_remove(struct i2c_client *client)
461 struct bq3060_device_info *di = i2c_get_clientdata(client);
463 power_supply_unregister(&di->bat);
473 static const struct i2c_device_id bq3060_id[] = {
477 static struct i2c_driver bq3060_battery_driver = {
479 .name = "bq3060-battery",
481 .probe = bq3060_battery_probe,
482 .remove = bq3060_battery_remove,
483 .id_table = bq3060_id,
486 static int __init bq3060_battery_init(void)
490 ret = i2c_add_driver(&bq3060_battery_driver);
492 printk(KERN_ERR "Unable to register bq3060 driver\n");
495 module_init(bq3060_battery_init);
497 static void __exit bq3060_battery_exit(void)
499 i2c_del_driver(&bq3060_battery_driver);
501 module_exit(bq3060_battery_exit);
503 MODULE_AUTHOR("Rockchip");
504 MODULE_DESCRIPTION("bq3060 battery monitor driver");
505 MODULE_LICENSE("GPL");