upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / power / max17042_battery.c
1 /*
2  * max17042_battery.c - Fuel gauge driver for Maxim 17042 / 8966 / 8997
3  *  Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
4  *
5  * Copyright (C) 2011 Samsung Electronics
6  * MyungJoo Ham <myungjoo.ham@samsung.com>
7  *
8  * This program is not provided / owned by Maxim Integrated Products.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  * This driver is based on max17040_battery.c
25  */
26
27 #include <linux/irq.h>
28 #include <linux/interrupt.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/power_supply.h>
32 #include <linux/power/max17042_battery.h>
33
34 #define MAX17042_BATTERY_FULL   (100)
35
36 struct max17042_chip {
37         struct i2c_client *client;
38         struct power_supply battery;
39         struct max17042_platform_data *pdata;
40         struct delayed_work dwork;
41         struct mutex irqlock;
42         bool irq_masked[MAX17042_IRQ_NR];
43         int irq_base;
44         u16 *reg_dump;
45 };
46
47 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
48 {
49         int ret = i2c_smbus_write_word_data(client, reg, value);
50
51         if (ret < 0)
52                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
53
54         return ret;
55 }
56
57 static int max17042_read_reg(struct i2c_client *client, u8 reg)
58 {
59         int ret = i2c_smbus_read_word_data(client, reg);
60
61         if (ret < 0)
62                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
63
64         return ret;
65 }
66
67 static void max17042_set_reg(struct i2c_client *client,
68                              struct max17042_reg_data *data, int size)
69 {
70         int i;
71
72         for (i = 0; i < size; i++)
73                 max17042_write_reg(client, data[i].addr, data[i].data);
74 }
75
76 static void __maybe_unused max17042_dump(struct max17042_chip *chip)
77 {
78         u16 buf[0x50];
79         int i;
80
81         for (i = 0; i < 0x50; i++)
82                 buf[i] = max17042_read_reg(chip->client, i);
83
84         print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 2, buf,
85                        sizeof(buf), false);
86 }
87
88 static enum power_supply_property max17042_battery_props[] = {
89         POWER_SUPPLY_PROP_PRESENT,
90         POWER_SUPPLY_PROP_CYCLE_COUNT,
91         POWER_SUPPLY_PROP_VOLTAGE_MAX,
92         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
93         POWER_SUPPLY_PROP_VOLTAGE_NOW,
94         POWER_SUPPLY_PROP_VOLTAGE_AVG,
95         POWER_SUPPLY_PROP_CURRENT_NOW,
96         POWER_SUPPLY_PROP_CURRENT_AVG,
97         POWER_SUPPLY_PROP_CAPACITY,
98         POWER_SUPPLY_PROP_CHARGE_FULL,
99         POWER_SUPPLY_PROP_TEMP,
100 };
101
102 #define MAX17042_HANDLE(reg)            \
103                 val->intval = max17042_read_reg(chip->client, reg); \
104                 if (val->intval < 0) \
105                         return val->intval;
106 static int max17042_get_property(struct power_supply *psy,
107                             enum power_supply_property psp,
108                             union power_supply_propval *val)
109 {
110         struct max17042_chip *chip = container_of(psy,
111                                 struct max17042_chip, battery);
112
113         switch (psp) {
114         case POWER_SUPPLY_PROP_PRESENT:
115                 MAX17042_HANDLE(MAX17042_STATUS);
116                 if (val->intval & MAX17042_STATUS_BattAbsent)
117                         val->intval = 0;
118                 else
119                         val->intval = 1;
120                 break;
121         case POWER_SUPPLY_PROP_CYCLE_COUNT:
122                 MAX17042_HANDLE(MAX17042_Cycles);
123                 break;
124         case POWER_SUPPLY_PROP_VOLTAGE_MAX:
125                 MAX17042_HANDLE(MAX17042_MinMaxVolt);
126                 val->intval >>= 8;
127                 val->intval *= 20000; /* LSB = 20mV */
128                 break;
129         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
130                 MAX17042_HANDLE(MAX17042_V_empty);
131                 val->intval >>= 7;
132                 val->intval *= 10000; /* LSB = 10mV */
133                 break;
134         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
135                 MAX17042_HANDLE(MAX17042_VCELL);
136                 val->intval *= 625;
137                 val->intval >>= 3;
138                 break;
139         case POWER_SUPPLY_PROP_VOLTAGE_AVG:
140                 MAX17042_HANDLE(MAX17042_AvgVCELL);
141                 val->intval *= 625;
142                 val->intval >>= 3;
143                 break;
144         case POWER_SUPPLY_PROP_CURRENT_NOW:
145                 /* FIXME: Check COFF,CGAIN are sain. */
146                 MAX17042_HANDLE(MAX17042_Current);
147                 if (val->intval & 0x8000) {
148                         /* Negative */
149                         val->intval = ~val->intval & 0x7fff;
150                         val->intval++;
151                         val->intval *= -1;
152                 }
153                 val->intval *= 1562500 / chip->pdata->r_sns;
154                 break;
155         case POWER_SUPPLY_PROP_CURRENT_AVG:
156                 /* FIXME: Check COFF,CGAIN are sain. */
157                 MAX17042_HANDLE(MAX17042_AvgCurrent);
158                 if (val->intval & 0x8000) {
159                         /* Negative */
160                         val->intval = ~val->intval & 0x7fff;
161                         val->intval++;
162                         val->intval *= -1;
163                 }
164                 val->intval *= 1562500 / chip->pdata->r_sns;
165                 break;
166         case POWER_SUPPLY_PROP_CAPACITY:
167                 MAX17042_HANDLE(MAX17042_VFSOC);
168                 val->intval = (val->intval >> 8);
169                 break;
170         case POWER_SUPPLY_PROP_CHARGE_FULL:
171                 /* FIXME: CHARGE_FULL does not mean this! */
172                 MAX17042_HANDLE(MAX17042_RepSOC);
173                 if ((val->intval / 256) >= MAX17042_BATTERY_FULL)
174                         val->intval = 1;
175                 else if (val->intval >= 0)
176                         val->intval = 0;
177                 break;
178         case POWER_SUPPLY_PROP_TEMP:
179                 MAX17042_HANDLE(MAX17042_TEMP);
180                 /* The value is signed. max17042_read_reg gives u16. */
181                 if (val->intval & 0x8000) {
182                         val->intval = (0x7fff & ~val->intval) + 1;
183                         val->intval *= -1;
184                 }
185                 /* psy wants deci-centigrade */
186                 val->intval = val->intval * 10 / 256;
187                 break;
188         default:
189                 return -EINVAL;
190         }
191         return 0;
192 }
193
194 static int max17042_set_property(struct power_supply *psy,
195                     enum power_supply_property psp,
196                     const union power_supply_propval *val)
197 {
198         struct max17042_chip *chip = container_of(psy,
199                                 struct max17042_chip, battery);
200         u16 reg;
201
202         switch (psp) {
203         case POWER_SUPPLY_PROP_CAPACITY:
204                 reg = max17042_read_reg(chip->client, MAX17042_MiscCFG);
205                 /* 0x0800(Bidb=1) for OCV compensation. Toggle it. */
206                 if (reg & 0x0800)
207                         reg &= ~0x0800;
208                 else
209                         reg |= 0x0800;
210                 max17042_write_reg(chip->client, MAX17042_MiscCFG, reg);
211                 return 0;
212         default:
213                 break;
214         }
215
216         return -EINVAL;
217 }
218
219 static int max17042_property_is_writeable(struct power_supply *psy,
220                                      enum power_supply_property psp)
221 {
222         switch (psp) {
223         case POWER_SUPPLY_PROP_CAPACITY:
224                 return 1;
225         default:
226                 break;
227         }
228
229         return 0;
230 }
231
232 static void max17042_irq_lock(unsigned int irq)
233 {
234         struct max17042_chip *chip = get_irq_chip_data(irq);
235
236         mutex_lock(&chip->irqlock);
237 }
238
239 static void max17042_irq_sync_unlock(unsigned int irq)
240 {
241         struct max17042_chip *chip = get_irq_chip_data(irq);
242
243         /* The hardware does not have maksing feature. skip caching */
244
245         mutex_unlock(&chip->irqlock);
246 }
247
248 static void max17042_irq_mask(unsigned int irq)
249 {
250         struct max17042_chip *chip = get_irq_chip_data(irq);
251
252         chip->irq_masked[irq - chip->irq_base] = true;
253 }
254
255 static void max17042_irq_unmask(unsigned int irq)
256 {
257         struct max17042_chip *chip = get_irq_chip_data(irq);
258
259         chip->irq_masked[irq - chip->irq_base] = false;
260 }
261
262 static struct irq_chip max17042_irq_chip = {
263         /* Note that in 2.6.39, this is deprecated */
264         .name                   = "max17042",
265         .bus_lock               = max17042_irq_lock,
266         .bus_sync_unlock        = max17042_irq_sync_unlock,
267         .mask                   = max17042_irq_mask,
268         .unmask                 = max17042_irq_unmask,
269 };
270
271 static void max17042_irq_thread_work(struct work_struct *work)
272 {
273         int val, i;
274         bool irq_reg[MAX17042_IRQ_NR] = {};
275         struct delayed_work *dwork = container_of(work, struct delayed_work,
276                                                   work);
277         struct max17042_chip *chip = container_of(dwork, struct max17042_chip,
278                                                   dwork);
279
280         val = max17042_read_reg(chip->client, MAX17042_STATUS);
281         max17042_write_reg(chip->client, MAX17042_STATUS,
282                            val & (MAX17042_STATUS_BattAbsent |
283                                   MAX17042_STATUS_POR));
284
285         /* Report */
286         if (val & MAX17042_STATUS_BattRemove)
287                 irq_reg[MAX17042_IRQ_Battery_Removed] = true;
288         if (val & MAX17042_STATUS_BattInsert)
289                 irq_reg[MAX17042_IRQ_Battery_Inserted] = true;
290         if (val & MAX17042_STATUS_BattInsert2)
291                 irq_reg[MAX17042_IRQ_Battery_Inserted2] = true;
292         if (val & MAX17042_STATUS_SOCMAX)
293                 irq_reg[MAX17042_IRQ_SOC_MAX] = true;
294         if (val & MAX17042_STATUS_TempMAX)
295                 irq_reg[MAX17042_IRQ_TEMP_MAX] = true;
296         if (val & MAX17042_STATUS_VoltMAX)
297                 irq_reg[MAX17042_IRQ_V_MAX] = true;
298         if (val & MAX17042_STATUS_SOCMIN)
299                 irq_reg[MAX17042_IRQ_SOC_MIN] = true;
300         if (val & MAX17042_STATUS_TempMIN)
301                 irq_reg[MAX17042_IRQ_TEMP_MIN] = true;
302         if (val & MAX17042_STATUS_VoltMIN)
303                 irq_reg[MAX17042_IRQ_V_MIN] = true;
304
305         for (i = 0; i < MAX17042_IRQ_NR; i++) {
306                 if (irq_reg[i] && !chip->irq_masked[i])
307                         handle_nested_irq(chip->irq_base + i);
308         }
309 }
310
311 static irqreturn_t max17042_irq_thread(int irq, void *irq_data)
312 {
313         struct max17042_chip *chip = irq_data;
314
315         /*
316          * Process 125ms later: datasheet says there is 125ms delay
317          * between ALRT pin responses and STATUS value changes.
318          * Give some margin and use 150ms.
319          */
320         schedule_delayed_work(&chip->dwork, msecs_to_jiffies(150));
321
322         return IRQ_HANDLED;
323 }
324
325 static int max17042_irq_init(struct max17042_chip *chip)
326 {
327         int ret, i;
328         int value;
329         int cur_irq;
330
331         INIT_DELAYED_WORK(&chip->dwork, max17042_irq_thread_work);
332         mutex_init(&chip->irqlock);
333         chip->irq_base = chip->pdata->irq_base;
334
335         /* Register with genirq */
336         for (i = 0; i < MAX17042_IRQ_NR; i++) {
337                 chip->irq_masked[i] = true;
338                 cur_irq = i + chip->irq_base;
339                 set_irq_chip_data(cur_irq, chip);
340                 set_irq_chip_and_handler(cur_irq, &max17042_irq_chip,
341                                 handle_edge_irq);
342                 set_irq_nested_thread(cur_irq, 1);
343 #ifdef CONFIG_ARM
344                 set_irq_flags(cur_irq, IRQF_VALID);
345 #else
346                 set_irq_noprobe(cur_irq);
347 #endif
348         }
349
350         if (chip->pdata->alrt_data)
351                 max17042_set_reg(chip->client, chip->pdata->alrt_data,
352                                  chip->pdata->num_alrt_data);
353
354         ret = request_threaded_irq(chip->client->irq, NULL,
355                                    max17042_irq_thread,
356                                    IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
357                                    "max17042 fuel alert", chip);
358         if (ret)
359                 return ret;
360
361         value = max17042_read_reg(chip->client, MAX17042_CONFIG);
362         if (value < 0)
363                 return value;
364
365         value &= ~MAX17042_CONFIG_AEN;
366         if (chip->pdata->enable_alert)
367                 value |= MAX17042_CONFIG_AEN;
368
369         max17042_write_reg(chip->client, MAX17042_CONFIG, value);
370
371         return 0;
372 }
373
374 static int __devinit max17042_probe(struct i2c_client *client,
375                         const struct i2c_device_id *id)
376 {
377         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
378         struct max17042_chip *chip;
379         int ret;
380
381         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
382                 return -EIO;
383
384         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
385         if (!chip)
386                 return -ENOMEM;
387
388         chip->client = client;
389         chip->pdata = client->dev.platform_data;
390         chip->reg_dump = NULL;
391         if (chip->pdata->r_sns == 0)
392                 chip->pdata->r_sns = 10000;
393
394         i2c_set_clientdata(client, chip);
395
396         /* Reset Average Current with Current */
397         ret = max17042_read_reg(client, MAX17042_Current);
398         if (ret < 0)
399                 goto err;
400         ret = max17042_write_reg(client, MAX17042_AvgCurrent, ret & 0xffff);
401         if (ret < 0)
402                 goto err;
403
404         chip->battery.name              = "max17042_battery";
405         chip->battery.type              = POWER_SUPPLY_TYPE_BATTERY;
406         chip->battery.get_property      = max17042_get_property;
407         chip->battery.set_property      = max17042_set_property;
408         chip->battery.property_is_writeable = max17042_property_is_writeable;
409         chip->battery.properties        = max17042_battery_props;
410         chip->battery.num_properties    = ARRAY_SIZE(max17042_battery_props);
411
412         ret = power_supply_register(&client->dev, &chip->battery);
413         if (ret) {
414                 dev_err(&client->dev, "failed: power supply register\n");
415                 goto err;
416         }
417
418         if (chip->pdata->init_data)
419                 max17042_set_reg(client, chip->pdata->init_data,
420                                  chip->pdata->num_init_data);
421
422         if (chip->pdata->irq_base && chip->client->irq) {
423                 ret = max17042_irq_init(chip);
424                 if (ret) {
425                         dev_err(&client->dev, "failed: irq init.\n");
426                         goto err_irq;
427                 }
428         }
429
430         return 0;
431 err_irq:
432         power_supply_unregister(&chip->battery);
433 err:
434         kfree(chip);
435         return ret;
436 }
437
438 static int __devexit max17042_remove(struct i2c_client *client)
439 {
440         struct max17042_chip *chip = i2c_get_clientdata(client);
441
442         if (chip->reg_dump)
443                 kfree(chip->reg_dump);
444
445         power_supply_unregister(&chip->battery);
446         i2c_set_clientdata(client, NULL);
447         kfree(chip);
448         return 0;
449 }
450
451 static const struct i2c_device_id max17042_id[] = {
452         { "max17042", 0 },
453         { }
454 };
455 MODULE_DEVICE_TABLE(i2c, max17042_id);
456
457 static int max17042_suspend(struct device *dev)
458 {
459         struct i2c_client *client = container_of(dev, struct i2c_client, dev);
460         struct max17042_chip *chip = i2c_get_clientdata(client);
461
462         if (chip->pdata->wakeup)
463                 set_irq_wake(client->irq, 1);
464         return 0;
465 }
466
467 static int max17042_resume(struct device *dev)
468 {
469         struct i2c_client *client = container_of(dev, struct i2c_client, dev);
470         struct max17042_chip *chip = i2c_get_clientdata(client);
471
472         if (chip->pdata->wakeup)
473                 set_irq_wake(client->irq, 0);
474
475         if (chip->pdata->irq_base && client->irq)
476                 max17042_irq_thread(client->irq, chip);
477
478         return 0;
479 }
480
481 static const u16 save_addr[] = {
482         MAX17042_VALRT_Th,
483         MAX17042_TALRT_Th,
484         MAX17042_SALRT_Th,
485         MAX17042_AtRate,
486         MAX17042_V_empty,
487         MAX17042_DesignCap,
488         MAX17042_CONFIG,
489         MAX17042_ICHGTerm,
490         MAX17042_FullCAPNom,
491         MAX17042_TempNom,
492         MAX17042_TempCold,
493         MAX17042_TempHot,
494         MAX17042_AIN,
495         MAX17042_LearnCFG,
496         MAX17042_SHFTCFG,
497         MAX17042_RelaxCFG,
498         MAX17042_MiscCFG,
499         MAX17042_TGAIN,
500         MAX17042_TOFF,
501         MAX17042_CGAIN,
502         MAX17042_COFF,
503         MAX17042_Q_empty,
504         MAX17042_T_empty,
505         MAX17042_RCOMP0,
506         MAX17042_TempCo,
507         MAX17042_TaskPeriod,
508 };
509
510 static int max17042_freeze(struct device *dev)
511 {
512         struct i2c_client *client = container_of(dev, struct i2c_client, dev);
513         struct max17042_chip *chip = i2c_get_clientdata(client);
514         int i;
515
516         if (chip->reg_dump) {
517                 dev_err(dev, "Register dump is not clean.\n");
518                 return -EINVAL;
519         }
520
521         chip->reg_dump = kzalloc(sizeof(*chip->reg_dump) * ARRAY_SIZE(save_addr),
522                                  GFP_KERNEL);
523         if (!chip->reg_dump) {
524                 dev_err(dev, "Cannot allocate memory for hibernation dump.\n");
525                 return -ENOMEM;
526         }
527
528         for (i = 0; i < ARRAY_SIZE(save_addr); i++)
529                 chip->reg_dump[i] = max17042_read_reg(client, save_addr[i]) & 0xffff;
530
531         return 0;
532 }
533
534 static int max17042_restore(struct device *dev)
535 {
536         struct i2c_client *client = container_of(dev, struct i2c_client, dev);
537         struct max17042_chip *chip = i2c_get_clientdata(client);
538         int i;
539
540         if (!chip->reg_dump) {
541                 dev_err(dev, "Cannot allocate memory for hibernation dump.\n");
542                 return -ENOMEM;
543         }
544
545         for (i = 0; i < ARRAY_SIZE(save_addr); i++)
546                 max17042_write_reg(client, save_addr[i], chip->reg_dump[i]);
547
548         kfree(chip->reg_dump);
549         chip->reg_dump = NULL;
550
551         return 0;
552 }
553
554 const struct dev_pm_ops max17042_pm = {
555         .suspend = max17042_suspend,
556         .resume = max17042_resume,
557         .freeze = max17042_freeze,
558         .restore = max17042_restore,
559 };
560
561 static struct i2c_driver max17042_i2c_driver = {
562         .driver = {
563                 .name   = "max17042",
564                 .pm     = &max17042_pm,
565         },
566         .probe          = max17042_probe,
567         .remove         = __devexit_p(max17042_remove),
568         .id_table       = max17042_id,
569 };
570
571 static int __init max17042_init(void)
572 {
573         return i2c_add_driver(&max17042_i2c_driver);
574 }
575 module_init(max17042_init);
576
577 static void __exit max17042_exit(void)
578 {
579         i2c_del_driver(&max17042_i2c_driver);
580 }
581 module_exit(max17042_exit);
582
583 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
584 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
585 MODULE_LICENSE("GPL");
586 MODULE_ALIAS("max17042");