UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / stc3100_battery.c
1 /* drivers/power/stc3100_battery
2  * STC31000 battery driver
3  *
4  * Copyright (C) 2009 Rockchip Corporation.
5  *
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.
9  *
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.
13  *
14  */
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"
27
28
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¦¸
32
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
43
44 #define STC3100_SPEED   300 * 1000
45
46 struct stc3100_device_info {
47         struct device           *dev;
48         struct power_supply     bat;
49         struct delayed_work work;
50         unsigned int interval;
51         struct i2c_client       *client;
52 };
53
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,
60 };
61
62
63 static int stc3100_write_regs(struct i2c_client *client, u8 reg, u8 const buf[], __u16 len)
64 {
65         int ret; 
66         ret = i2c_master_reg8_send(client, reg, buf, (int)len, STC3100_SPEED);
67         return ret;
68 }
69 /*
70  * Common code for STC3100 devices read
71  */
72 static int stc3100_read_regs(struct i2c_client *client, u8 reg, u8 buf[], unsigned len)
73 {
74         int ret; 
75         ret = i2c_master_reg8_recv(client, reg, buf, len, STC3100_SPEED);
76         return ret; 
77 }
78
79 /*
80  * Return the battery temperature in Celsius degrees
81  * Or < 0 if something fails.
82  */
83 static int stc3100_battery_temperature(struct stc3100_device_info *di)
84 {
85         int ret;
86         u8 regs[2];
87
88         ret = stc3100_read_regs(di->client,STC3100_REG_TEMPL,regs,2);
89         if (ret<0) {
90                 dev_err(di->dev, "error reading temperature\n");
91                 return ret;
92         }
93
94         return (((((int)regs[1]&0xf)<<8) & (int)regs[0])>>3);  ///temperature (¡ãC) = Temperature_code * 0.125.
95 }
96
97 /*
98  * Return the battery Voltage in milivolts
99  * Or < 0 if something fails.
100  */
101 static int stc3100_battery_voltage(struct stc3100_device_info *di)
102 {
103         int ret;
104         u8 regs[2];
105
106         ret = stc3100_read_regs(di->client,STC3100_REG_VOLTL,regs,2);
107         if (ret<0) {
108                 dev_err(di->dev, "error reading voltage\n");
109                 return ret;
110         }
111
112         return (((((int)regs[1]&0xf)<<8) & (int)regs[0])*61/25);   //voltage (mV) = Voltage_code * 2.44.
113 }
114
115 /*
116  * Return the battery average current
117  * Note that current can be negative signed as well
118  * Or 0 if something fails.
119  */
120 static int stc3100_battery_current(struct stc3100_device_info *di)
121 {
122         int ret;
123         u8 regs[2];
124
125         ret = stc3100_read_regs(di->client,STC3100_REG_AIL,regs,2);
126         if (ret<0) {
127                 dev_err(di->dev, "error reading current\n");
128                 return 0;
129         }
130         
131         return (((((int)regs[1]&0xf)<<8) & (int)regs[0])*1177/(100*RSENSE_RESISTANCE));   ///current (mA) = current_code* 11.77 / Rsense (m¦¸)
132 }
133
134 /*
135  * Return the battery Relative State-of-Charge
136  * Or < 0 if something fails.
137  */
138 static int stc3100_battery_rsoc(struct stc3100_device_info *di)
139 {
140         int ret;
141         u8 regs[2];
142
143         ret = stc3100_read_regs(di->client,STC3100_REG_RSOCL,regs,2);
144         if (ret<0) {
145                 dev_err(di->dev, "error reading relative State-of-Charge\n");
146                 return ret;
147         }
148
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¦¸).
150 }
151
152 static int dc_charge_status(void)
153 {
154         if(gpio_get_value(DC_CHECKPIN))
155                 return POWER_SUPPLY_STATUS_CHARGING;
156         else
157                 return POWER_SUPPLY_STATUS_NOT_CHARGING;
158 }
159
160 static int stc3100_battery_get_property(struct power_supply *psy,
161                                         enum power_supply_property psp,
162                                         union power_supply_propval *val)
163 {
164         struct stc3100_device_info *di = container_of(psy, struct stc3100_device_info, bat);
165
166         switch (psp) {
167         case POWER_SUPPLY_PROP_STATUS:
168                 val->intval = dc_charge_status();
169                 break;
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;
175                 break;
176         case POWER_SUPPLY_PROP_CURRENT_NOW:
177                 val->intval = stc3100_battery_current(di);
178                 break;
179         case POWER_SUPPLY_PROP_CAPACITY:
180                 val->intval = stc3100_battery_rsoc(di);
181                 break;
182         case POWER_SUPPLY_PROP_TEMP:
183                 val->intval = stc3100_battery_temperature(di);
184                 break;
185         default:
186                 return -EINVAL;
187         }
188
189         return 0;
190 }
191
192 static void stc3100_powersupply_init(struct stc3100_device_info *di)
193 {
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;
199 }
200
201 static void stc3100_battery_update_status(struct stc3100_device_info *di)
202 {
203         power_supply_changed(&di->bat);
204 }
205
206 static void stc3100_battery_work(struct work_struct *work)
207 {
208         struct stc3100_device_info *di = container_of(work, struct stc3100_device_info, work.work); 
209
210         stc3100_battery_update_status(di);
211         /* reschedule for the next time */
212         schedule_delayed_work(&di->work, di->interval);
213 }
214
215 static int stc3100_battery_probe(struct i2c_client *client,
216                                  const struct i2c_device_id *id)
217 {
218         struct stc3100_device_info *di;
219         int retval = 0;
220         u8 regs[2] = {0x10,0x1d};  ///init regs mode ctrl
221
222         di = kzalloc(sizeof(*di), GFP_KERNEL);
223         if (!di) {
224                 dev_err(&client->dev, "failed to allocate device info data\n");
225                 retval = -ENOMEM;
226                 goto batt_failed_2;
227         }
228
229         i2c_set_clientdata(client, di);
230         di->dev = &client->dev;
231         di->bat.name = "stc3100-battery";
232         di->client = client;
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);
237
238         retval = power_supply_register(&client->dev, &di->bat);
239         if (retval) {
240                 dev_err(&client->dev, "failed to register battery\n");
241                 goto batt_failed_3;
242         }
243         
244         INIT_DELAYED_WORK(&di->work, stc3100_battery_work);
245         schedule_delayed_work(&di->work, di->interval);
246         
247         dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
248
249         return 0;
250
251 batt_failed_3:
252         kfree(di);
253 batt_failed_2:
254         return retval;
255 }
256
257 static int stc3100_battery_remove(struct i2c_client *client)
258 {
259         struct stc3100_device_info *di = i2c_get_clientdata(client);
260
261         cancel_delayed_work_sync(&di->work);
262         power_supply_unregister(&di->bat);
263
264         kfree(di->bat.name);
265
266         kfree(di);
267
268         return 0;
269 }
270
271 /*
272  * Module stuff
273  */
274
275 static const struct i2c_device_id stc3100_id[] = {
276         { "stc3100", 0 },
277         {},
278 };
279
280 static struct i2c_driver stc3100_battery_driver = {
281         .driver = {
282                 .name = "stc3100-battery",
283         },
284         .probe = stc3100_battery_probe,
285         .remove = stc3100_battery_remove,
286         .id_table = stc3100_id,
287 };
288
289 static int __init stc3100_battery_init(void)
290 {
291         int ret;
292
293         ret = i2c_add_driver(&stc3100_battery_driver);
294         if (ret)
295                 printk(KERN_ERR "Unable to register BQ27200 driver\n");
296
297         return ret;
298 }
299 module_init(stc3100_battery_init);
300
301 static void __exit stc3100_battery_exit(void)
302 {
303         i2c_del_driver(&stc3100_battery_driver);
304 }
305 module_exit(stc3100_battery_exit);
306
307 MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
308 MODULE_DESCRIPTION("BQ27x00 battery monitor driver");
309 MODULE_LICENSE("GPL");