1 /* drivers/power/stc3100_battery
2 * STC31000 battery driver
4 * Copyright (C) 2009 Rockchip Corporation.
6 * This package is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
15 #include <linux/module.h>
16 #include <linux/param.h>
17 #include <linux/jiffies.h>
18 #include <linux/workqueue.h>
19 #include <linux/delay.h>
20 #include <linux/platform_device.h>
21 #include <linux/power_supply.h>
22 #include <linux/idr.h>
23 #include <linux/i2c.h>
24 #include <asm/unaligned.h>
25 #include <mach/gpio.h>
26 #define DRIVER_VERSION "1.0.0"
29 #define DC_CHECKPIN RK29_PIN4_PA4 ///da check charge pin
30 #define BATTAERY_CAPACITY_MAH 2000 /// define battery capacity mah
31 #define RSENSE_RESISTANCE 10 /// Rsense resistance m¦¸
33 #define STC3100_REG_MODE 0x00
34 #define STC3100_REG_CTRL 0x01
35 #define STC3100_REG_RSOCL 0x02 /* Relative State-of-Charge */
36 #define STC3100_REG_RSOCH 0x03
37 #define STC3100_REG_AIL 0x06
38 #define STC3100_REG_AIH 0x07
39 #define STC3100_REG_VOLTL 0x08
40 #define STC3100_REG_VOLTH 0x09
41 #define STC3100_REG_TEMPL 0x0A
42 #define STC3100_REG_TEMPH 0x0B
44 #define STC3100_SPEED 300 * 1000
46 struct stc3100_device_info {
48 struct power_supply bat;
49 struct delayed_work work;
50 unsigned int interval;
51 struct i2c_client *client;
54 static enum power_supply_property stc3100_battery_props[] = {
55 POWER_SUPPLY_PROP_PRESENT,
56 POWER_SUPPLY_PROP_VOLTAGE_NOW,
57 POWER_SUPPLY_PROP_CURRENT_NOW,
58 POWER_SUPPLY_PROP_CAPACITY,
59 POWER_SUPPLY_PROP_TEMP,
63 static int stc3100_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], __u16 len)
66 ret = i2c_master_reg8_send(client, reg, buf, (int)len, STC3100_SPEED);
70 * Common code for STC3100 devices read
72 static int stc3100_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
75 ret = i2c_master_reg8_recv(client, reg, buf, len, STC3100_SPEED);
80 * Return the battery temperature in Celsius degrees
81 * Or < 0 if something fails.
83 static int stc3100_battery_temperature(struct stc3100_device_info *di)
88 ret = stc3100_read_regs(di->client,STC3100_REG_TEMPL,regs,2);
90 dev_err(di->dev, "error reading temperature\n");
94 return (((((int)regs[1]&0xf)<<8) & (int)regs[0])>>3); ///temperature (¡ãC) = Temperature_code * 0.125.
98 * Return the battery Voltage in milivolts
99 * Or < 0 if something fails.
101 static int stc3100_battery_voltage(struct stc3100_device_info *di)
106 ret = stc3100_read_regs(di->client,STC3100_REG_VOLTL,regs,2);
108 dev_err(di->dev, "error reading voltage\n");
112 return (((((int)regs[1]&0xf)<<8) & (int)regs[0])*61/25); //voltage (mV) = Voltage_code * 2.44.
116 * Return the battery average current
117 * Note that current can be negative signed as well
118 * Or 0 if something fails.
120 static int stc3100_battery_current(struct stc3100_device_info *di)
125 ret = stc3100_read_regs(di->client,STC3100_REG_AIL,regs,2);
127 dev_err(di->dev, "error reading current\n");
131 return (((((int)regs[1]&0xf)<<8) & (int)regs[0])*1177/(100*RSENSE_RESISTANCE)); ///current (mA) = current_code* 11.77 / Rsense (m¦¸)
135 * Return the battery Relative State-of-Charge
136 * Or < 0 if something fails.
138 static int stc3100_battery_rsoc(struct stc3100_device_info *di)
143 ret = stc3100_read_regs(di->client,STC3100_REG_RSOCL,regs,2);
145 dev_err(di->dev, "error reading relative State-of-Charge\n");
149 return (((((int)regs[1])<<8) & (int)regs[0])*67/(10*RSENSE_RESISTANCE))*100/BATTAERY_CAPACITY_MAH; ////charge data (mA.h) = 6.70 * charge_code / Rsense (m¦¸).
152 static int dc_charge_status(void)
154 if(gpio_get_value(DC_CHECKPIN))
155 return POWER_SUPPLY_STATUS_CHARGING;
157 return POWER_SUPPLY_STATUS_NOT_CHARGING;
160 static int stc3100_battery_get_property(struct power_supply *psy,
161 enum power_supply_property psp,
162 union power_supply_propval *val)
164 struct stc3100_device_info *di = container_of(psy, struct stc3100_device_info, bat);
167 case POWER_SUPPLY_PROP_STATUS:
168 val->intval = dc_charge_status();
170 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
171 case POWER_SUPPLY_PROP_PRESENT:
172 val->intval = stc3100_battery_voltage(di);
173 if (psp == POWER_SUPPLY_PROP_PRESENT)
174 val->intval = val->intval <= 0 ? 0 : 1;
176 case POWER_SUPPLY_PROP_CURRENT_NOW:
177 val->intval = stc3100_battery_current(di);
179 case POWER_SUPPLY_PROP_CAPACITY:
180 val->intval = stc3100_battery_rsoc(di);
182 case POWER_SUPPLY_PROP_TEMP:
183 val->intval = stc3100_battery_temperature(di);
192 static void stc3100_powersupply_init(struct stc3100_device_info *di)
194 di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
195 di->bat.properties = stc3100_battery_props;
196 di->bat.num_properties = ARRAY_SIZE(stc3100_battery_props);
197 di->bat.get_property = stc3100_battery_get_property;
198 di->bat.external_power_changed = NULL;
201 static void stc3100_battery_update_status(struct stc3100_device_info *di)
203 power_supply_changed(&di->bat);
206 static void stc3100_battery_work(struct work_struct *work)
208 struct stc3100_device_info *di = container_of(work, struct stc3100_device_info, work.work);
210 stc3100_battery_update_status(di);
211 /* reschedule for the next time */
212 schedule_delayed_work(&di->work, di->interval);
215 static int stc3100_battery_probe(struct i2c_client *client,
216 const struct i2c_device_id *id)
218 struct stc3100_device_info *di;
220 u8 regs[2] = {0x10,0x1d}; ///init regs mode ctrl
222 di = kzalloc(sizeof(*di), GFP_KERNEL);
224 dev_err(&client->dev, "failed to allocate device info data\n");
229 i2c_set_clientdata(client, di);
230 di->dev = &client->dev;
231 di->bat.name = "stc3100-battery";
233 stc3100_write_regs(client, STC3100_REG_MODE, regs, 2);
234 /* 4 seconds between monotor runs interval */
235 di->interval = msecs_to_jiffies(4 * 1000);
236 stc3100_powersupply_init(di);
238 retval = power_supply_register(&client->dev, &di->bat);
240 dev_err(&client->dev, "failed to register battery\n");
244 INIT_DELAYED_WORK(&di->work, stc3100_battery_work);
245 schedule_delayed_work(&di->work, di->interval);
247 dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
257 static int stc3100_battery_remove(struct i2c_client *client)
259 struct stc3100_device_info *di = i2c_get_clientdata(client);
261 cancel_delayed_work_sync(&di->work);
262 power_supply_unregister(&di->bat);
275 static const struct i2c_device_id stc3100_id[] = {
280 static struct i2c_driver stc3100_battery_driver = {
282 .name = "stc3100-battery",
284 .probe = stc3100_battery_probe,
285 .remove = stc3100_battery_remove,
286 .id_table = stc3100_id,
289 static int __init stc3100_battery_init(void)
293 ret = i2c_add_driver(&stc3100_battery_driver);
295 printk(KERN_ERR "Unable to register BQ27200 driver\n");
299 module_init(stc3100_battery_init);
301 static void __exit stc3100_battery_exit(void)
303 i2c_del_driver(&stc3100_battery_driver);
305 module_exit(stc3100_battery_exit);
307 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
308 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
309 MODULE_LICENSE("GPL");