power_supply: Add voltage_ocv property and use it for max17042 driver
[linux-2.6-block.git] / drivers / power / max17042_battery.c
1 /*
2  * 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 free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * This driver is based on max17040_battery.c
23  */
24
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/pm.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/power_supply.h>
34 #include <linux/power/max17042_battery.h>
35 #include <linux/of.h>
36
37 /* Status register bits */
38 #define STATUS_POR_BIT         (1 << 1)
39 #define STATUS_BST_BIT         (1 << 3)
40 #define STATUS_VMN_BIT         (1 << 8)
41 #define STATUS_TMN_BIT         (1 << 9)
42 #define STATUS_SMN_BIT         (1 << 10)
43 #define STATUS_BI_BIT          (1 << 11)
44 #define STATUS_VMX_BIT         (1 << 12)
45 #define STATUS_TMX_BIT         (1 << 13)
46 #define STATUS_SMX_BIT         (1 << 14)
47 #define STATUS_BR_BIT          (1 << 15)
48
49 /* Interrupt mask bits */
50 #define CONFIG_ALRT_BIT_ENBL    (1 << 2)
51 #define STATUS_INTR_SOCMIN_BIT  (1 << 10)
52 #define STATUS_INTR_SOCMAX_BIT  (1 << 14)
53
54 #define VFSOC0_LOCK             0x0000
55 #define VFSOC0_UNLOCK           0x0080
56 #define MODEL_UNLOCK1   0X0059
57 #define MODEL_UNLOCK2   0X00C4
58 #define MODEL_LOCK1             0X0000
59 #define MODEL_LOCK2             0X0000
60
61 #define dQ_ACC_DIV      0x4
62 #define dP_ACC_100      0x1900
63 #define dP_ACC_200      0x3200
64
65 struct max17042_chip {
66         struct i2c_client *client;
67         struct power_supply battery;
68         struct max17042_platform_data *pdata;
69         struct work_struct work;
70         int    init_complete;
71 };
72
73 static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
74 {
75         int ret = i2c_smbus_write_word_data(client, reg, value);
76
77         if (ret < 0)
78                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
79
80         return ret;
81 }
82
83 static int max17042_read_reg(struct i2c_client *client, u8 reg)
84 {
85         int ret = i2c_smbus_read_word_data(client, reg);
86
87         if (ret < 0)
88                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
89
90         return ret;
91 }
92
93 static void max17042_set_reg(struct i2c_client *client,
94                              struct max17042_reg_data *data, int size)
95 {
96         int i;
97
98         for (i = 0; i < size; i++)
99                 max17042_write_reg(client, data[i].addr, data[i].data);
100 }
101
102 static enum power_supply_property max17042_battery_props[] = {
103         POWER_SUPPLY_PROP_PRESENT,
104         POWER_SUPPLY_PROP_CYCLE_COUNT,
105         POWER_SUPPLY_PROP_VOLTAGE_MAX,
106         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
107         POWER_SUPPLY_PROP_VOLTAGE_NOW,
108         POWER_SUPPLY_PROP_VOLTAGE_AVG,
109         POWER_SUPPLY_PROP_VOLTAGE_OCV,
110         POWER_SUPPLY_PROP_CAPACITY,
111         POWER_SUPPLY_PROP_CHARGE_FULL,
112         POWER_SUPPLY_PROP_TEMP,
113         POWER_SUPPLY_PROP_CURRENT_NOW,
114         POWER_SUPPLY_PROP_CURRENT_AVG,
115 };
116
117 static int max17042_get_property(struct power_supply *psy,
118                             enum power_supply_property psp,
119                             union power_supply_propval *val)
120 {
121         struct max17042_chip *chip = container_of(psy,
122                                 struct max17042_chip, battery);
123         int ret;
124
125         if (!chip->init_complete)
126                 return -EAGAIN;
127
128         switch (psp) {
129         case POWER_SUPPLY_PROP_PRESENT:
130                 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
131                 if (ret < 0)
132                         return ret;
133
134                 if (ret & MAX17042_STATUS_BattAbsent)
135                         val->intval = 0;
136                 else
137                         val->intval = 1;
138                 break;
139         case POWER_SUPPLY_PROP_CYCLE_COUNT:
140                 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
141                 if (ret < 0)
142                         return ret;
143
144                 val->intval = ret;
145                 break;
146         case POWER_SUPPLY_PROP_VOLTAGE_MAX:
147                 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
148                 if (ret < 0)
149                         return ret;
150
151                 val->intval = ret >> 8;
152                 val->intval *= 20000; /* Units of LSB = 20mV */
153                 break;
154         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
155                 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
156                 if (ret < 0)
157                         return ret;
158
159                 val->intval = ret >> 7;
160                 val->intval *= 10000; /* Units of LSB = 10mV */
161                 break;
162         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
163                 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
164                 if (ret < 0)
165                         return ret;
166
167                 val->intval = ret * 625 / 8;
168                 break;
169         case POWER_SUPPLY_PROP_VOLTAGE_AVG:
170                 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
171                 if (ret < 0)
172                         return ret;
173
174                 val->intval = ret * 625 / 8;
175                 break;
176         case POWER_SUPPLY_PROP_VOLTAGE_OCV:
177                 ret = max17042_read_reg(chip->client, MAX17042_OCVInternal);
178                 if (ret < 0)
179                         return ret;
180
181                 val->intval = ret * 625 / 8;
182                 break;
183         case POWER_SUPPLY_PROP_CAPACITY:
184                 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
185                 if (ret < 0)
186                         return ret;
187
188                 val->intval = ret >> 8;
189                 break;
190         case POWER_SUPPLY_PROP_CHARGE_FULL:
191                 ret = max17042_read_reg(chip->client, MAX17042_FullCAP);
192                 if (ret < 0)
193                         return ret;
194
195                 val->intval = ret * 1000 / 2;
196                 break;
197         case POWER_SUPPLY_PROP_TEMP:
198                 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
199                 if (ret < 0)
200                         return ret;
201
202                 val->intval = ret;
203                 /* The value is signed. */
204                 if (val->intval & 0x8000) {
205                         val->intval = (0x7fff & ~val->intval) + 1;
206                         val->intval *= -1;
207                 }
208                 /* The value is converted into deci-centigrade scale */
209                 /* Units of LSB = 1 / 256 degree Celsius */
210                 val->intval = val->intval * 10 / 256;
211                 break;
212         case POWER_SUPPLY_PROP_CURRENT_NOW:
213                 if (chip->pdata->enable_current_sense) {
214                         ret = max17042_read_reg(chip->client, MAX17042_Current);
215                         if (ret < 0)
216                                 return ret;
217
218                         val->intval = ret;
219                         if (val->intval & 0x8000) {
220                                 /* Negative */
221                                 val->intval = ~val->intval & 0x7fff;
222                                 val->intval++;
223                                 val->intval *= -1;
224                         }
225                         val->intval *= 1562500 / chip->pdata->r_sns;
226                 } else {
227                         return -EINVAL;
228                 }
229                 break;
230         case POWER_SUPPLY_PROP_CURRENT_AVG:
231                 if (chip->pdata->enable_current_sense) {
232                         ret = max17042_read_reg(chip->client,
233                                                 MAX17042_AvgCurrent);
234                         if (ret < 0)
235                                 return ret;
236
237                         val->intval = ret;
238                         if (val->intval & 0x8000) {
239                                 /* Negative */
240                                 val->intval = ~val->intval & 0x7fff;
241                                 val->intval++;
242                                 val->intval *= -1;
243                         }
244                         val->intval *= 1562500 / chip->pdata->r_sns;
245                 } else {
246                         return -EINVAL;
247                 }
248                 break;
249         default:
250                 return -EINVAL;
251         }
252         return 0;
253 }
254
255 static int max17042_write_verify_reg(struct i2c_client *client,
256                                 u8 reg, u16 value)
257 {
258         int retries = 8;
259         int ret;
260         u16 read_value;
261
262         do {
263                 ret = i2c_smbus_write_word_data(client, reg, value);
264                 read_value =  max17042_read_reg(client, reg);
265                 if (read_value != value) {
266                         ret = -EIO;
267                         retries--;
268                 }
269         } while (retries && read_value != value);
270
271         if (ret < 0)
272                 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
273
274         return ret;
275 }
276
277 static inline void max17042_override_por(
278         struct i2c_client *client, u8 reg, u16 value)
279 {
280         if (value)
281                 max17042_write_reg(client, reg, value);
282 }
283
284 static inline void max10742_unlock_model(struct max17042_chip *chip)
285 {
286         struct i2c_client *client = chip->client;
287         max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
288         max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
289 }
290
291 static inline void max10742_lock_model(struct max17042_chip *chip)
292 {
293         struct i2c_client *client = chip->client;
294         max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1);
295         max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2);
296 }
297
298 static inline void max17042_write_model_data(struct max17042_chip *chip,
299                                         u8 addr, int size)
300 {
301         struct i2c_client *client = chip->client;
302         int i;
303         for (i = 0; i < size; i++)
304                 max17042_write_reg(client, addr + i,
305                                 chip->pdata->config_data->cell_char_tbl[i]);
306 }
307
308 static inline void max17042_read_model_data(struct max17042_chip *chip,
309                                         u8 addr, u16 *data, int size)
310 {
311         struct i2c_client *client = chip->client;
312         int i;
313
314         for (i = 0; i < size; i++)
315                 data[i] = max17042_read_reg(client, addr + i);
316 }
317
318 static inline int max17042_model_data_compare(struct max17042_chip *chip,
319                                         u16 *data1, u16 *data2, int size)
320 {
321         int i;
322
323         if (memcmp(data1, data2, size)) {
324                 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
325                 for (i = 0; i < size; i++)
326                         dev_info(&chip->client->dev, "0x%x, 0x%x",
327                                 data1[i], data2[i]);
328                 dev_info(&chip->client->dev, "\n");
329                 return -EINVAL;
330         }
331         return 0;
332 }
333
334 static int max17042_init_model(struct max17042_chip *chip)
335 {
336         int ret;
337         int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
338         u16 *temp_data;
339
340         temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
341         if (!temp_data)
342                 return -ENOMEM;
343
344         max10742_unlock_model(chip);
345         max17042_write_model_data(chip, MAX17042_MODELChrTbl,
346                                 table_size);
347         max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
348                                 table_size);
349
350         ret = max17042_model_data_compare(
351                 chip,
352                 chip->pdata->config_data->cell_char_tbl,
353                 temp_data,
354                 table_size);
355
356         max10742_lock_model(chip);
357         kfree(temp_data);
358
359         return ret;
360 }
361
362 static int max17042_verify_model_lock(struct max17042_chip *chip)
363 {
364         int i;
365         int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
366         u16 *temp_data;
367         int ret = 0;
368
369         temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
370         if (!temp_data)
371                 return -ENOMEM;
372
373         max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
374                                 table_size);
375         for (i = 0; i < table_size; i++)
376                 if (temp_data[i])
377                         ret = -EINVAL;
378
379         kfree(temp_data);
380         return ret;
381 }
382
383 static void max17042_write_config_regs(struct max17042_chip *chip)
384 {
385         struct max17042_config_data *config = chip->pdata->config_data;
386
387         max17042_write_reg(chip->client, MAX17042_CONFIG, config->config);
388         max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg);
389         max17042_write_reg(chip->client, MAX17042_FilterCFG,
390                         config->filter_cfg);
391         max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg);
392 }
393
394 static void  max17042_write_custom_regs(struct max17042_chip *chip)
395 {
396         struct max17042_config_data *config = chip->pdata->config_data;
397
398         max17042_write_verify_reg(chip->client, MAX17042_RCOMP0,
399                                 config->rcomp0);
400         max17042_write_verify_reg(chip->client, MAX17042_TempCo,
401                                 config->tcompc0);
402         max17042_write_reg(chip->client, MAX17042_EmptyTempCo,
403                         config->empty_tempco);
404         max17042_write_verify_reg(chip->client, MAX17042_K_empty0,
405                                 config->kempty0);
406         max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm,
407                                 config->ichgt_term);
408 }
409
410 static void max17042_update_capacity_regs(struct max17042_chip *chip)
411 {
412         struct max17042_config_data *config = chip->pdata->config_data;
413
414         max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
415                                 config->fullcap);
416         max17042_write_reg(chip->client, MAX17042_DesignCap,
417                         config->design_cap);
418         max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
419                                 config->fullcapnom);
420 }
421
422 static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
423 {
424         u16 vfSoc;
425
426         vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
427         max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
428         max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc);
429         max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
430 }
431
432 static void max17042_load_new_capacity_params(struct max17042_chip *chip)
433 {
434         u16 full_cap0, rep_cap, dq_acc, vfSoc;
435         u32 rem_cap;
436
437         struct max17042_config_data *config = chip->pdata->config_data;
438
439         full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0);
440         vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
441
442         /* fg_vfSoc needs to shifted by 8 bits to get the
443          * perc in 1% accuracy, to get the right rem_cap multiply
444          * full_cap0, fg_vfSoc and devide by 100
445          */
446         rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
447         max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap);
448
449         rep_cap = (u16)rem_cap;
450         max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap);
451
452         /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
453         dq_acc = config->fullcap / dQ_ACC_DIV;
454         max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc);
455         max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200);
456
457         max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
458                         config->fullcap);
459         max17042_write_reg(chip->client, MAX17042_DesignCap,
460                         config->design_cap);
461         max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
462                         config->fullcapnom);
463 }
464
465 /*
466  * Block write all the override values coming from platform data.
467  * This function MUST be called before the POR initialization proceedure
468  * specified by maxim.
469  */
470 static inline void max17042_override_por_values(struct max17042_chip *chip)
471 {
472         struct i2c_client *client = chip->client;
473         struct max17042_config_data *config = chip->pdata->config_data;
474
475         max17042_override_por(client, MAX17042_TGAIN, config->tgain);
476         max17042_override_por(client, MAx17042_TOFF, config->toff);
477         max17042_override_por(client, MAX17042_CGAIN, config->cgain);
478         max17042_override_por(client, MAX17042_COFF, config->coff);
479
480         max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh);
481         max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh);
482         max17042_override_por(client, MAX17042_SALRT_Th,
483                         config->soc_alrt_thresh);
484         max17042_override_por(client, MAX17042_CONFIG, config->config);
485         max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer);
486
487         max17042_override_por(client, MAX17042_DesignCap, config->design_cap);
488         max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term);
489
490         max17042_override_por(client, MAX17042_AtRate, config->at_rate);
491         max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg);
492         max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg);
493         max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg);
494         max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg);
495         max17042_override_por(client, MAX17042_MaskSOC, config->masksoc);
496
497         max17042_override_por(client, MAX17042_FullCAP, config->fullcap);
498         max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom);
499         max17042_override_por(client, MAX17042_SOC_empty, config->socempty);
500         max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty);
501         max17042_override_por(client, MAX17042_dQacc, config->dqacc);
502         max17042_override_por(client, MAX17042_dPacc, config->dpacc);
503
504         max17042_override_por(client, MAX17042_V_empty, config->vempty);
505         max17042_override_por(client, MAX17042_TempNom, config->temp_nom);
506         max17042_override_por(client, MAX17042_TempLim, config->temp_lim);
507         max17042_override_por(client, MAX17042_FCTC, config->fctc);
508         max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0);
509         max17042_override_por(client, MAX17042_TempCo, config->tcompc0);
510         max17042_override_por(client, MAX17042_EmptyTempCo,
511                         config->empty_tempco);
512         max17042_override_por(client, MAX17042_K_empty0, config->kempty0);
513 }
514
515 static int max17042_init_chip(struct max17042_chip *chip)
516 {
517         int ret;
518         int val;
519
520         max17042_override_por_values(chip);
521         /* After Power up, the MAX17042 requires 500mS in order
522          * to perform signal debouncing and initial SOC reporting
523          */
524         msleep(500);
525
526         /* Initialize configaration */
527         max17042_write_config_regs(chip);
528
529         /* write cell characterization data */
530         ret = max17042_init_model(chip);
531         if (ret) {
532                 dev_err(&chip->client->dev, "%s init failed\n",
533                         __func__);
534                 return -EIO;
535         }
536         max17042_verify_model_lock(chip);
537         if (ret) {
538                 dev_err(&chip->client->dev, "%s lock verify failed\n",
539                         __func__);
540                 return -EIO;
541         }
542         /* write custom parameters */
543         max17042_write_custom_regs(chip);
544
545         /* update capacity params */
546         max17042_update_capacity_regs(chip);
547
548         /* delay must be atleast 350mS to allow VFSOC
549          * to be calculated from the new configuration
550          */
551         msleep(350);
552
553         /* reset vfsoc0 reg */
554         max17042_reset_vfsoc0_reg(chip);
555
556         /* load new capacity params */
557         max17042_load_new_capacity_params(chip);
558
559         /* Init complete, Clear the POR bit */
560         val = max17042_read_reg(chip->client, MAX17042_STATUS);
561         max17042_write_reg(chip->client, MAX17042_STATUS,
562                         val & (~STATUS_POR_BIT));
563         return 0;
564 }
565
566 static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
567 {
568         u16 soc, soc_tr;
569
570         /* program interrupt thesholds such that we should
571          * get interrupt for every 'off' perc change in the soc
572          */
573         soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8;
574         soc_tr = (soc + off) << 8;
575         soc_tr |= (soc - off);
576         max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr);
577 }
578
579 static irqreturn_t max17042_thread_handler(int id, void *dev)
580 {
581         struct max17042_chip *chip = dev;
582         u16 val;
583
584         val = max17042_read_reg(chip->client, MAX17042_STATUS);
585         if ((val & STATUS_INTR_SOCMIN_BIT) ||
586                 (val & STATUS_INTR_SOCMAX_BIT)) {
587                 dev_info(&chip->client->dev, "SOC threshold INTR\n");
588                 max17042_set_soc_threshold(chip, 1);
589         }
590
591         power_supply_changed(&chip->battery);
592         return IRQ_HANDLED;
593 }
594
595 static void max17042_init_worker(struct work_struct *work)
596 {
597         struct max17042_chip *chip = container_of(work,
598                                 struct max17042_chip, work);
599         int ret;
600
601         /* Initialize registers according to values from the platform data */
602         if (chip->pdata->enable_por_init && chip->pdata->config_data) {
603                 ret = max17042_init_chip(chip);
604                 if (ret)
605                         return;
606         }
607
608         chip->init_complete = 1;
609 }
610
611 #ifdef CONFIG_OF
612 static struct max17042_platform_data *
613 max17042_get_pdata(struct device *dev)
614 {
615         struct device_node *np = dev->of_node;
616         u32 prop;
617         struct max17042_platform_data *pdata;
618
619         if (!np)
620                 return dev->platform_data;
621
622         pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
623         if (!pdata)
624                 return NULL;
625
626         /*
627          * Require current sense resistor value to be specified for
628          * current-sense functionality to be enabled at all.
629          */
630         if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
631                 pdata->r_sns = prop;
632                 pdata->enable_current_sense = true;
633         }
634
635         return pdata;
636 }
637 #else
638 static struct max17042_platform_data *
639 max17042_get_pdata(struct device *dev)
640 {
641         return dev->platform_data;
642 }
643 #endif
644
645 static int __devinit max17042_probe(struct i2c_client *client,
646                         const struct i2c_device_id *id)
647 {
648         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
649         struct max17042_chip *chip;
650         int ret;
651         int reg;
652
653         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
654                 return -EIO;
655
656         chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
657         if (!chip)
658                 return -ENOMEM;
659
660         chip->client = client;
661         chip->pdata = max17042_get_pdata(&client->dev);
662         if (!chip->pdata) {
663                 dev_err(&client->dev, "no platform data provided\n");
664                 return -EINVAL;
665         }
666
667         i2c_set_clientdata(client, chip);
668
669         chip->battery.name              = "max17042_battery";
670         chip->battery.type              = POWER_SUPPLY_TYPE_BATTERY;
671         chip->battery.get_property      = max17042_get_property;
672         chip->battery.properties        = max17042_battery_props;
673         chip->battery.num_properties    = ARRAY_SIZE(max17042_battery_props);
674
675         /* When current is not measured,
676          * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
677         if (!chip->pdata->enable_current_sense)
678                 chip->battery.num_properties -= 2;
679
680         if (chip->pdata->r_sns == 0)
681                 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
682
683         if (chip->pdata->init_data)
684                 max17042_set_reg(client, chip->pdata->init_data,
685                                 chip->pdata->num_init_data);
686
687         if (!chip->pdata->enable_current_sense) {
688                 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
689                 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
690                 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
691         }
692
693         if (client->irq) {
694                 ret = request_threaded_irq(client->irq, NULL,
695                                                 max17042_thread_handler,
696                                                 IRQF_TRIGGER_FALLING,
697                                                 chip->battery.name, chip);
698                 if (!ret) {
699                         reg =  max17042_read_reg(client, MAX17042_CONFIG);
700                         reg |= CONFIG_ALRT_BIT_ENBL;
701                         max17042_write_reg(client, MAX17042_CONFIG, reg);
702                         max17042_set_soc_threshold(chip, 1);
703                 } else
704                         dev_err(&client->dev, "%s(): cannot get IRQ\n",
705                                 __func__);
706         }
707
708         reg = max17042_read_reg(chip->client, MAX17042_STATUS);
709
710         if (reg & STATUS_POR_BIT) {
711                 INIT_WORK(&chip->work, max17042_init_worker);
712                 schedule_work(&chip->work);
713         } else {
714                 chip->init_complete = 1;
715         }
716
717         ret = power_supply_register(&client->dev, &chip->battery);
718         if (ret)
719                 dev_err(&client->dev, "failed: power supply register\n");
720         return ret;
721 }
722
723 static int __devexit max17042_remove(struct i2c_client *client)
724 {
725         struct max17042_chip *chip = i2c_get_clientdata(client);
726
727         if (client->irq)
728                 free_irq(client->irq, chip);
729         power_supply_unregister(&chip->battery);
730         return 0;
731 }
732
733 #ifdef CONFIG_PM
734 static int max17042_suspend(struct device *dev)
735 {
736         struct max17042_chip *chip = dev_get_drvdata(dev);
737
738         /*
739          * disable the irq and enable irq_wake
740          * capability to the interrupt line.
741          */
742         if (chip->client->irq) {
743                 disable_irq(chip->client->irq);
744                 enable_irq_wake(chip->client->irq);
745         }
746
747         return 0;
748 }
749
750 static int max17042_resume(struct device *dev)
751 {
752         struct max17042_chip *chip = dev_get_drvdata(dev);
753
754         if (chip->client->irq) {
755                 disable_irq_wake(chip->client->irq);
756                 enable_irq(chip->client->irq);
757                 /* re-program the SOC thresholds to 1% change */
758                 max17042_set_soc_threshold(chip, 1);
759         }
760
761         return 0;
762 }
763
764 static const struct dev_pm_ops max17042_pm_ops = {
765         .suspend        = max17042_suspend,
766         .resume         = max17042_resume,
767 };
768
769 #define MAX17042_PM_OPS (&max17042_pm_ops)
770 #else
771 #define MAX17042_PM_OPS NULL
772 #endif
773
774 #ifdef CONFIG_OF
775 static const struct of_device_id max17042_dt_match[] = {
776         { .compatible = "maxim,max17042" },
777         { },
778 };
779 MODULE_DEVICE_TABLE(of, max17042_dt_match);
780 #endif
781
782 static const struct i2c_device_id max17042_id[] = {
783         { "max17042", 0 },
784         { }
785 };
786 MODULE_DEVICE_TABLE(i2c, max17042_id);
787
788 static struct i2c_driver max17042_i2c_driver = {
789         .driver = {
790                 .name   = "max17042",
791                 .of_match_table = of_match_ptr(max17042_dt_match),
792                 .pm     = MAX17042_PM_OPS,
793         },
794         .probe          = max17042_probe,
795         .remove         = __devexit_p(max17042_remove),
796         .id_table       = max17042_id,
797 };
798 module_i2c_driver(max17042_i2c_driver);
799
800 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
801 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
802 MODULE_LICENSE("GPL");