blob: bf5a82c9370efef6209122e6fdf313f38f122bcf [file] [log] [blame]
MyungJoo Ham359ab9f2011-01-14 14:46:11 +09001/*
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>
Paul Gortmaker7e6d62d2011-07-03 15:28:29 -040026#include <linux/module.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090027#include <linux/slab.h>
28#include <linux/i2c.h>
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040029#include <linux/delay.h>
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -080030#include <linux/interrupt.h>
Ramakrishna Pallala48bc1772012-03-27 02:23:40 +053031#include <linux/pm.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090032#include <linux/mod_devicetable.h>
33#include <linux/power_supply.h>
34#include <linux/power/max17042_battery.h>
Karol Lewandowski38322462012-02-22 19:06:22 +010035#include <linux/of.h>
Jonghwa Lee39e72132013-10-25 13:55:02 +090036#include <linux/regmap.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090037
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040038/* Status register bits */
39#define STATUS_POR_BIT (1 << 1)
40#define STATUS_BST_BIT (1 << 3)
41#define STATUS_VMN_BIT (1 << 8)
42#define STATUS_TMN_BIT (1 << 9)
43#define STATUS_SMN_BIT (1 << 10)
44#define STATUS_BI_BIT (1 << 11)
45#define STATUS_VMX_BIT (1 << 12)
46#define STATUS_TMX_BIT (1 << 13)
47#define STATUS_SMX_BIT (1 << 14)
48#define STATUS_BR_BIT (1 << 15)
49
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -080050/* Interrupt mask bits */
51#define CONFIG_ALRT_BIT_ENBL (1 << 2)
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +053052#define STATUS_INTR_SOCMIN_BIT (1 << 10)
53#define STATUS_INTR_SOCMAX_BIT (1 << 14)
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -080054
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040055#define VFSOC0_LOCK 0x0000
56#define VFSOC0_UNLOCK 0x0080
57#define MODEL_UNLOCK1 0X0059
58#define MODEL_UNLOCK2 0X00C4
59#define MODEL_LOCK1 0X0000
60#define MODEL_LOCK2 0X0000
61
62#define dQ_ACC_DIV 0x4
63#define dP_ACC_100 0x1900
64#define dP_ACC_200 0x3200
65
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090066struct max17042_chip {
67 struct i2c_client *client;
Jonghwa Lee39e72132013-10-25 13:55:02 +090068 struct regmap *regmap;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010069 struct power_supply *battery;
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +053070 enum max170xx_chip_type chip_type;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090071 struct max17042_platform_data *pdata;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040072 struct work_struct work;
73 int init_complete;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090074};
75
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090076static enum power_supply_property max17042_battery_props[] = {
Donggeun Kim086ef502011-06-30 18:07:41 +090077 POWER_SUPPLY_PROP_PRESENT,
78 POWER_SUPPLY_PROP_CYCLE_COUNT,
79 POWER_SUPPLY_PROP_VOLTAGE_MAX,
80 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090081 POWER_SUPPLY_PROP_VOLTAGE_NOW,
82 POWER_SUPPLY_PROP_VOLTAGE_AVG,
Ramakrishna Pallalaa2ebfe22012-04-10 16:21:20 +053083 POWER_SUPPLY_PROP_VOLTAGE_OCV,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090084 POWER_SUPPLY_PROP_CAPACITY,
Donggeun Kim086ef502011-06-30 18:07:41 +090085 POWER_SUPPLY_PROP_CHARGE_FULL,
Ramakrishna Pallala5fc55bc2012-05-07 10:25:58 +053086 POWER_SUPPLY_PROP_CHARGE_COUNTER,
Donggeun Kim086ef502011-06-30 18:07:41 +090087 POWER_SUPPLY_PROP_TEMP,
88 POWER_SUPPLY_PROP_CURRENT_NOW,
89 POWER_SUPPLY_PROP_CURRENT_AVG,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090090};
91
92static int max17042_get_property(struct power_supply *psy,
93 enum power_supply_property psp,
94 union power_supply_propval *val)
95{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010096 struct max17042_chip *chip = power_supply_get_drvdata(psy);
Jonghwa Lee39e72132013-10-25 13:55:02 +090097 struct regmap *map = chip->regmap;
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +040098 int ret;
Jonghwa Lee39e72132013-10-25 13:55:02 +090099 u32 data;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900100
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400101 if (!chip->init_complete)
102 return -EAGAIN;
103
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900104 switch (psp) {
Donggeun Kim086ef502011-06-30 18:07:41 +0900105 case POWER_SUPPLY_PROP_PRESENT:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900106 ret = regmap_read(map, MAX17042_STATUS, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400107 if (ret < 0)
108 return ret;
109
Jonghwa Lee39e72132013-10-25 13:55:02 +0900110 if (data & MAX17042_STATUS_BattAbsent)
Donggeun Kim086ef502011-06-30 18:07:41 +0900111 val->intval = 0;
112 else
113 val->intval = 1;
114 break;
115 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900116 ret = regmap_read(map, MAX17042_Cycles, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400117 if (ret < 0)
118 return ret;
119
Jonghwa Lee39e72132013-10-25 13:55:02 +0900120 val->intval = data;
Donggeun Kim086ef502011-06-30 18:07:41 +0900121 break;
122 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900123 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400124 if (ret < 0)
125 return ret;
126
Jonghwa Lee39e72132013-10-25 13:55:02 +0900127 val->intval = data >> 8;
Donggeun Kim086ef502011-06-30 18:07:41 +0900128 val->intval *= 20000; /* Units of LSB = 20mV */
129 break;
130 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Beomho Seo709c2c72015-04-03 17:26:08 +0900131 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900132 ret = regmap_read(map, MAX17042_V_empty, &data);
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530133 else
Jonghwa Lee39e72132013-10-25 13:55:02 +0900134 ret = regmap_read(map, MAX17047_V_empty, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400135 if (ret < 0)
136 return ret;
137
Jonghwa Lee39e72132013-10-25 13:55:02 +0900138 val->intval = data >> 7;
Donggeun Kim086ef502011-06-30 18:07:41 +0900139 val->intval *= 10000; /* Units of LSB = 10mV */
140 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900141 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900142 ret = regmap_read(map, MAX17042_VCELL, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400143 if (ret < 0)
144 return ret;
145
Jonghwa Lee39e72132013-10-25 13:55:02 +0900146 val->intval = data * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900147 break;
148 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900149 ret = regmap_read(map, MAX17042_AvgVCELL, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400150 if (ret < 0)
151 return ret;
152
Jonghwa Lee39e72132013-10-25 13:55:02 +0900153 val->intval = data * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900154 break;
Ramakrishna Pallalaa2ebfe22012-04-10 16:21:20 +0530155 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900156 ret = regmap_read(map, MAX17042_OCVInternal, &data);
Ramakrishna Pallalaa2ebfe22012-04-10 16:21:20 +0530157 if (ret < 0)
158 return ret;
159
Jonghwa Lee39e72132013-10-25 13:55:02 +0900160 val->intval = data * 625 / 8;
Ramakrishna Pallalaa2ebfe22012-04-10 16:21:20 +0530161 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900162 case POWER_SUPPLY_PROP_CAPACITY:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900163 ret = regmap_read(map, MAX17042_RepSOC, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400164 if (ret < 0)
165 return ret;
166
Jonghwa Lee39e72132013-10-25 13:55:02 +0900167 val->intval = data >> 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900168 break;
Donggeun Kim086ef502011-06-30 18:07:41 +0900169 case POWER_SUPPLY_PROP_CHARGE_FULL:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900170 ret = regmap_read(map, MAX17042_FullCAP, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400171 if (ret < 0)
172 return ret;
173
Jonghwa Lee39e72132013-10-25 13:55:02 +0900174 val->intval = data * 1000 / 2;
Donggeun Kim086ef502011-06-30 18:07:41 +0900175 break;
Ramakrishna Pallala5fc55bc2012-05-07 10:25:58 +0530176 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900177 ret = regmap_read(map, MAX17042_QH, &data);
Ramakrishna Pallala5fc55bc2012-05-07 10:25:58 +0530178 if (ret < 0)
179 return ret;
180
Jonghwa Lee39e72132013-10-25 13:55:02 +0900181 val->intval = data * 1000 / 2;
Ramakrishna Pallala5fc55bc2012-05-07 10:25:58 +0530182 break;
Donggeun Kim086ef502011-06-30 18:07:41 +0900183 case POWER_SUPPLY_PROP_TEMP:
Jonghwa Lee39e72132013-10-25 13:55:02 +0900184 ret = regmap_read(map, MAX17042_TEMP, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400185 if (ret < 0)
186 return ret;
187
Jonghwa Lee39e72132013-10-25 13:55:02 +0900188 val->intval = data;
Donggeun Kim086ef502011-06-30 18:07:41 +0900189 /* The value is signed. */
190 if (val->intval & 0x8000) {
191 val->intval = (0x7fff & ~val->intval) + 1;
192 val->intval *= -1;
193 }
194 /* The value is converted into deci-centigrade scale */
195 /* Units of LSB = 1 / 256 degree Celsius */
196 val->intval = val->intval * 10 / 256;
197 break;
198 case POWER_SUPPLY_PROP_CURRENT_NOW:
199 if (chip->pdata->enable_current_sense) {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900200 ret = regmap_read(map, MAX17042_Current, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400201 if (ret < 0)
202 return ret;
203
Jonghwa Lee39e72132013-10-25 13:55:02 +0900204 val->intval = data;
Donggeun Kim086ef502011-06-30 18:07:41 +0900205 if (val->intval & 0x8000) {
206 /* Negative */
207 val->intval = ~val->intval & 0x7fff;
208 val->intval++;
209 val->intval *= -1;
210 }
Philip Rakity91d8b0d2011-08-12 21:19:57 -0700211 val->intval *= 1562500 / chip->pdata->r_sns;
Donggeun Kim086ef502011-06-30 18:07:41 +0900212 } else {
213 return -EINVAL;
214 }
215 break;
216 case POWER_SUPPLY_PROP_CURRENT_AVG:
217 if (chip->pdata->enable_current_sense) {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900218 ret = regmap_read(map, MAX17042_AvgCurrent, &data);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400219 if (ret < 0)
220 return ret;
221
Jonghwa Lee39e72132013-10-25 13:55:02 +0900222 val->intval = data;
Donggeun Kim086ef502011-06-30 18:07:41 +0900223 if (val->intval & 0x8000) {
224 /* Negative */
225 val->intval = ~val->intval & 0x7fff;
226 val->intval++;
227 val->intval *= -1;
228 }
229 val->intval *= 1562500 / chip->pdata->r_sns;
230 } else {
231 return -EINVAL;
232 }
233 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900234 default:
235 return -EINVAL;
236 }
237 return 0;
238}
239
Jonghwa Lee39e72132013-10-25 13:55:02 +0900240static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400241{
242 int retries = 8;
243 int ret;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900244 u32 read_value;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400245
246 do {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900247 ret = regmap_write(map, reg, value);
248 regmap_read(map, reg, &read_value);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400249 if (read_value != value) {
250 ret = -EIO;
251 retries--;
252 }
253 } while (retries && read_value != value);
254
255 if (ret < 0)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900256 pr_err("%s: err %d\n", __func__, ret);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400257
258 return ret;
259}
260
Jonghwa Lee39e72132013-10-25 13:55:02 +0900261static inline void max17042_override_por(struct regmap *map,
262 u8 reg, u16 value)
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400263{
264 if (value)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900265 regmap_write(map, reg, value);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400266}
267
268static inline void max10742_unlock_model(struct max17042_chip *chip)
269{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900270 struct regmap *map = chip->regmap;
271 regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
272 regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400273}
274
275static inline void max10742_lock_model(struct max17042_chip *chip)
276{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900277 struct regmap *map = chip->regmap;
278
279 regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
280 regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400281}
282
283static inline void max17042_write_model_data(struct max17042_chip *chip,
284 u8 addr, int size)
285{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900286 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400287 int i;
288 for (i = 0; i < size; i++)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900289 regmap_write(map, addr + i,
290 chip->pdata->config_data->cell_char_tbl[i]);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400291}
292
293static inline void max17042_read_model_data(struct max17042_chip *chip,
Jonghwa Lee39e72132013-10-25 13:55:02 +0900294 u8 addr, u32 *data, int size)
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400295{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900296 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400297 int i;
298
299 for (i = 0; i < size; i++)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900300 regmap_read(map, addr + i, &data[i]);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400301}
302
303static inline int max17042_model_data_compare(struct max17042_chip *chip,
304 u16 *data1, u16 *data2, int size)
305{
306 int i;
307
308 if (memcmp(data1, data2, size)) {
309 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
310 for (i = 0; i < size; i++)
311 dev_info(&chip->client->dev, "0x%x, 0x%x",
312 data1[i], data2[i]);
313 dev_info(&chip->client->dev, "\n");
314 return -EINVAL;
315 }
316 return 0;
317}
318
319static int max17042_init_model(struct max17042_chip *chip)
320{
321 int ret;
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300322 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900323 u32 *temp_data;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400324
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300325 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400326 if (!temp_data)
327 return -ENOMEM;
328
329 max10742_unlock_model(chip);
330 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
331 table_size);
332 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
333 table_size);
334
335 ret = max17042_model_data_compare(
336 chip,
337 chip->pdata->config_data->cell_char_tbl,
Jonghwa Lee39e72132013-10-25 13:55:02 +0900338 (u16 *)temp_data,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400339 table_size);
340
341 max10742_lock_model(chip);
342 kfree(temp_data);
343
344 return ret;
345}
346
347static int max17042_verify_model_lock(struct max17042_chip *chip)
348{
349 int i;
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300350 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900351 u32 *temp_data;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400352 int ret = 0;
353
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300354 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400355 if (!temp_data)
356 return -ENOMEM;
357
358 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
359 table_size);
360 for (i = 0; i < table_size; i++)
361 if (temp_data[i])
362 ret = -EINVAL;
363
364 kfree(temp_data);
365 return ret;
366}
367
368static void max17042_write_config_regs(struct max17042_chip *chip)
369{
370 struct max17042_config_data *config = chip->pdata->config_data;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900371 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400372
Jonghwa Lee39e72132013-10-25 13:55:02 +0900373 regmap_write(map, MAX17042_CONFIG, config->config);
374 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
375 regmap_write(map, MAX17042_FilterCFG,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400376 config->filter_cfg);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900377 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
Beomho Seo709c2c72015-04-03 17:26:08 +0900378 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 ||
379 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900380 regmap_write(map, MAX17047_FullSOCThr,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530381 config->full_soc_thresh);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400382}
383
384static void max17042_write_custom_regs(struct max17042_chip *chip)
385{
386 struct max17042_config_data *config = chip->pdata->config_data;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900387 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400388
Jonghwa Lee39e72132013-10-25 13:55:02 +0900389 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
390 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
391 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
Beomho Seo709c2c72015-04-03 17:26:08 +0900392 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900393 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
394 max17042_write_verify_reg(map, MAX17042_K_empty0,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530395 config->kempty0);
396 } else {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900397 max17042_write_verify_reg(map, MAX17047_QRTbl00,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530398 config->qrtbl00);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900399 max17042_write_verify_reg(map, MAX17047_QRTbl10,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530400 config->qrtbl10);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900401 max17042_write_verify_reg(map, MAX17047_QRTbl20,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530402 config->qrtbl20);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900403 max17042_write_verify_reg(map, MAX17047_QRTbl30,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530404 config->qrtbl30);
405 }
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400406}
407
408static void max17042_update_capacity_regs(struct max17042_chip *chip)
409{
410 struct max17042_config_data *config = chip->pdata->config_data;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900411 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400412
Jonghwa Lee39e72132013-10-25 13:55:02 +0900413 max17042_write_verify_reg(map, MAX17042_FullCAP,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400414 config->fullcap);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900415 regmap_write(map, MAX17042_DesignCap, config->design_cap);
416 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400417 config->fullcapnom);
418}
419
420static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
421{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900422 unsigned int vfSoc;
423 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400424
Jonghwa Lee39e72132013-10-25 13:55:02 +0900425 regmap_read(map, MAX17042_VFSOC, &vfSoc);
426 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
427 max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
428 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400429}
430
431static void max17042_load_new_capacity_params(struct max17042_chip *chip)
432{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900433 u32 full_cap0, rep_cap, dq_acc, vfSoc;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400434 u32 rem_cap;
435
436 struct max17042_config_data *config = chip->pdata->config_data;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900437 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400438
Jonghwa Lee39e72132013-10-25 13:55:02 +0900439 regmap_read(map, MAX17042_FullCAP0, &full_cap0);
440 regmap_read(map, MAX17042_VFSOC, &vfSoc);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400441
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;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900447 max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400448
Jonghwa Lee39e72132013-10-25 13:55:02 +0900449 rep_cap = rem_cap;
450 max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400451
452 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
453 dq_acc = config->fullcap / dQ_ACC_DIV;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900454 max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
455 max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400456
Jonghwa Lee39e72132013-10-25 13:55:02 +0900457 max17042_write_verify_reg(map, MAX17042_FullCAP,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400458 config->fullcap);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900459 regmap_write(map, MAX17042_DesignCap,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400460 config->design_cap);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900461 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400462 config->fullcapnom);
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530463 /* Update SOC register with new SOC */
Jonghwa Lee39e72132013-10-25 13:55:02 +0900464 regmap_write(map, MAX17042_RepSOC, vfSoc);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400465}
466
467/*
468 * Block write all the override values coming from platform data.
469 * This function MUST be called before the POR initialization proceedure
470 * specified by maxim.
471 */
472static inline void max17042_override_por_values(struct max17042_chip *chip)
473{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900474 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400475 struct max17042_config_data *config = chip->pdata->config_data;
476
Jonghwa Lee39e72132013-10-25 13:55:02 +0900477 max17042_override_por(map, MAX17042_TGAIN, config->tgain);
478 max17042_override_por(map, MAx17042_TOFF, config->toff);
479 max17042_override_por(map, MAX17042_CGAIN, config->cgain);
480 max17042_override_por(map, MAX17042_COFF, config->coff);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400481
Jonghwa Lee39e72132013-10-25 13:55:02 +0900482 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
483 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
484 max17042_override_por(map, MAX17042_SALRT_Th,
485 config->soc_alrt_thresh);
486 max17042_override_por(map, MAX17042_CONFIG, config->config);
487 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400488
Jonghwa Lee39e72132013-10-25 13:55:02 +0900489 max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
490 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400491
Jonghwa Lee39e72132013-10-25 13:55:02 +0900492 max17042_override_por(map, MAX17042_AtRate, config->at_rate);
493 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
494 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
495 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
496 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
497 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400498
Jonghwa Lee39e72132013-10-25 13:55:02 +0900499 max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
500 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
Beomho Seo709c2c72015-04-03 17:26:08 +0900501 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900502 max17042_override_por(map, MAX17042_SOC_empty,
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530503 config->socempty);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900504 max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
505 max17042_override_por(map, MAX17042_dQacc, config->dqacc);
506 max17042_override_por(map, MAX17042_dPacc, config->dpacc);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400507
Beomho Seo709c2c72015-04-03 17:26:08 +0900508 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900509 max17042_override_por(map, MAX17042_V_empty, config->vempty);
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530510 else
Jonghwa Lee39e72132013-10-25 13:55:02 +0900511 max17042_override_por(map, MAX17047_V_empty, config->vempty);
512 max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
513 max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
514 max17042_override_por(map, MAX17042_FCTC, config->fctc);
515 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
516 max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530517 if (chip->chip_type) {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900518 max17042_override_por(map, MAX17042_EmptyTempCo,
519 config->empty_tempco);
520 max17042_override_por(map, MAX17042_K_empty0,
521 config->kempty0);
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530522 }
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400523}
524
525static int max17042_init_chip(struct max17042_chip *chip)
526{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900527 struct regmap *map = chip->regmap;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400528 int ret;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400529
530 max17042_override_por_values(chip);
531 /* After Power up, the MAX17042 requires 500mS in order
532 * to perform signal debouncing and initial SOC reporting
533 */
534 msleep(500);
535
536 /* Initialize configaration */
537 max17042_write_config_regs(chip);
538
539 /* write cell characterization data */
540 ret = max17042_init_model(chip);
541 if (ret) {
542 dev_err(&chip->client->dev, "%s init failed\n",
543 __func__);
544 return -EIO;
545 }
Alan Coxa879f192012-11-18 14:59:47 -0800546
547 ret = max17042_verify_model_lock(chip);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400548 if (ret) {
549 dev_err(&chip->client->dev, "%s lock verify failed\n",
550 __func__);
551 return -EIO;
552 }
553 /* write custom parameters */
554 max17042_write_custom_regs(chip);
555
556 /* update capacity params */
557 max17042_update_capacity_regs(chip);
558
559 /* delay must be atleast 350mS to allow VFSOC
560 * to be calculated from the new configuration
561 */
562 msleep(350);
563
564 /* reset vfsoc0 reg */
565 max17042_reset_vfsoc0_reg(chip);
566
567 /* load new capacity params */
568 max17042_load_new_capacity_params(chip);
569
570 /* Init complete, Clear the POR bit */
Krzysztof Kozlowskibc352682015-02-24 10:54:47 +0100571 regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400572 return 0;
573}
574
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800575static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
576{
Jonghwa Lee39e72132013-10-25 13:55:02 +0900577 struct regmap *map = chip->regmap;
578 u32 soc, soc_tr;
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800579
580 /* program interrupt thesholds such that we should
581 * get interrupt for every 'off' perc change in the soc
582 */
Jonghwa Lee39e72132013-10-25 13:55:02 +0900583 regmap_read(map, MAX17042_RepSOC, &soc);
584 soc >>= 8;
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800585 soc_tr = (soc + off) << 8;
586 soc_tr |= (soc - off);
Jonghwa Lee39e72132013-10-25 13:55:02 +0900587 regmap_write(map, MAX17042_SALRT_Th, soc_tr);
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800588}
589
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800590static irqreturn_t max17042_thread_handler(int id, void *dev)
591{
592 struct max17042_chip *chip = dev;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900593 u32 val;
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800594
Jonghwa Lee39e72132013-10-25 13:55:02 +0900595 regmap_read(chip->regmap, MAX17042_STATUS, &val);
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530596 if ((val & STATUS_INTR_SOCMIN_BIT) ||
597 (val & STATUS_INTR_SOCMAX_BIT)) {
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800598 dev_info(&chip->client->dev, "SOC threshold INTR\n");
599 max17042_set_soc_threshold(chip, 1);
600 }
601
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100602 power_supply_changed(chip->battery);
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800603 return IRQ_HANDLED;
604}
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400605
606static void max17042_init_worker(struct work_struct *work)
607{
608 struct max17042_chip *chip = container_of(work,
609 struct max17042_chip, work);
610 int ret;
611
612 /* Initialize registers according to values from the platform data */
613 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
614 ret = max17042_init_chip(chip);
615 if (ret)
616 return;
617 }
618
619 chip->init_complete = 1;
620}
621
Karol Lewandowski38322462012-02-22 19:06:22 +0100622#ifdef CONFIG_OF
623static struct max17042_platform_data *
624max17042_get_pdata(struct device *dev)
625{
626 struct device_node *np = dev->of_node;
627 u32 prop;
628 struct max17042_platform_data *pdata;
629
630 if (!np)
631 return dev->platform_data;
632
633 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
634 if (!pdata)
635 return NULL;
636
637 /*
638 * Require current sense resistor value to be specified for
639 * current-sense functionality to be enabled at all.
640 */
641 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
642 pdata->r_sns = prop;
643 pdata->enable_current_sense = true;
644 }
645
646 return pdata;
647}
648#else
649static struct max17042_platform_data *
650max17042_get_pdata(struct device *dev)
651{
652 return dev->platform_data;
653}
654#endif
655
Krzysztof Kozlowskie0291282015-01-05 09:50:17 +0100656static const struct regmap_config max17042_regmap_config = {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900657 .reg_bits = 8,
658 .val_bits = 16,
659 .val_format_endian = REGMAP_ENDIAN_NATIVE,
660};
661
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100662static const struct power_supply_desc max17042_psy_desc = {
663 .name = "max170xx_battery",
664 .type = POWER_SUPPLY_TYPE_BATTERY,
665 .get_property = max17042_get_property,
666 .properties = max17042_battery_props,
667 .num_properties = ARRAY_SIZE(max17042_battery_props),
668};
669
670static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
671 .name = "max170xx_battery",
672 .type = POWER_SUPPLY_TYPE_BATTERY,
673 .get_property = max17042_get_property,
674 .properties = max17042_battery_props,
675 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
676};
677
Bill Pembertonc8afa642012-11-19 13:22:23 -0500678static int max17042_probe(struct i2c_client *client,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900679 const struct i2c_device_id *id)
680{
681 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100682 const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
683 struct power_supply_config psy_cfg = {};
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900684 struct max17042_chip *chip;
685 int ret;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900686 int i;
687 u32 val;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900688
689 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
690 return -EIO;
691
Karol Lewandowski2f3b4342012-02-22 19:06:20 +0100692 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900693 if (!chip)
694 return -ENOMEM;
695
696 chip->client = client;
Jonghwa Lee39e72132013-10-25 13:55:02 +0900697 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
698 if (IS_ERR(chip->regmap)) {
699 dev_err(&client->dev, "Failed to initialize regmap\n");
700 return -EINVAL;
701 }
702
Karol Lewandowski38322462012-02-22 19:06:22 +0100703 chip->pdata = max17042_get_pdata(&client->dev);
704 if (!chip->pdata) {
705 dev_err(&client->dev, "no platform data provided\n");
706 return -EINVAL;
707 }
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900708
709 i2c_set_clientdata(client, chip);
Beomho Seo709c2c72015-04-03 17:26:08 +0900710 chip->chip_type = id->driver_data;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100711 psy_cfg.drv_data = chip;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900712
Donggeun Kim086ef502011-06-30 18:07:41 +0900713 /* When current is not measured,
714 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
715 if (!chip->pdata->enable_current_sense)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100716 max17042_desc = &max17042_no_current_sense_psy_desc;
Donggeun Kim086ef502011-06-30 18:07:41 +0900717
Philip Rakity4cfa8922011-08-12 21:18:18 -0700718 if (chip->pdata->r_sns == 0)
719 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
720
Donggeun Kim086ef502011-06-30 18:07:41 +0900721 if (chip->pdata->init_data)
Jonghwa Lee39e72132013-10-25 13:55:02 +0900722 for (i = 0; i < chip->pdata->num_init_data; i++)
723 regmap_write(chip->regmap,
724 chip->pdata->init_data[i].addr,
725 chip->pdata->init_data[i].data);
Donggeun Kim086ef502011-06-30 18:07:41 +0900726
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900727 if (!chip->pdata->enable_current_sense) {
Jonghwa Lee39e72132013-10-25 13:55:02 +0900728 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
729 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
730 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900731 }
732
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100733 chip->battery = power_supply_register(&client->dev, max17042_desc,
734 &psy_cfg);
735 if (IS_ERR(chip->battery)) {
Ramakrishna Pallala243e3522012-05-05 03:08:37 +0530736 dev_err(&client->dev, "failed: power supply register\n");
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100737 return PTR_ERR(chip->battery);
Ramakrishna Pallala243e3522012-05-05 03:08:37 +0530738 }
739
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800740 if (client->irq) {
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530741 ret = request_threaded_irq(client->irq, NULL,
Jonghwa Lee26740db2013-12-18 12:53:53 +0900742 max17042_thread_handler,
743 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100744 chip->battery->desc->name, chip);
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800745 if (!ret) {
Krzysztof Kozlowskibc352682015-02-24 10:54:47 +0100746 regmap_update_bits(chip->regmap, MAX17042_CONFIG,
747 CONFIG_ALRT_BIT_ENBL,
748 CONFIG_ALRT_BIT_ENBL);
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800749 max17042_set_soc_threshold(chip, 1);
Ramakrishna Pallalae5ba50b2012-05-05 04:43:10 +0530750 } else {
751 client->irq = 0;
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800752 dev_err(&client->dev, "%s(): cannot get IRQ\n",
753 __func__);
Ramakrishna Pallalae5ba50b2012-05-05 04:43:10 +0530754 }
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800755 }
756
Jonghwa Lee39e72132013-10-25 13:55:02 +0900757 regmap_read(chip->regmap, MAX17042_STATUS, &val);
758 if (val & STATUS_POR_BIT) {
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400759 INIT_WORK(&chip->work, max17042_init_worker);
760 schedule_work(&chip->work);
761 } else {
762 chip->init_complete = 1;
763 }
764
Ramakrishna Pallala243e3522012-05-05 03:08:37 +0530765 return 0;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900766}
767
Bill Pemberton415ec692012-11-19 13:26:07 -0500768static int max17042_remove(struct i2c_client *client)
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900769{
770 struct max17042_chip *chip = i2c_get_clientdata(client);
771
Ramakrishna Pallalabb28da92012-03-26 15:38:26 +0530772 if (client->irq)
773 free_irq(client->irq, chip);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100774 power_supply_unregister(chip->battery);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900775 return 0;
776}
777
Manish Badarkhe3d23c7f2013-10-14 10:56:51 +0530778#ifdef CONFIG_PM_SLEEP
Ramakrishna Pallala48bc1772012-03-27 02:23:40 +0530779static int max17042_suspend(struct device *dev)
780{
781 struct max17042_chip *chip = dev_get_drvdata(dev);
782
Anton Vorontsov48e41c72012-05-04 20:38:13 -0700783 /*
784 * disable the irq and enable irq_wake
Ramakrishna Pallala48bc1772012-03-27 02:23:40 +0530785 * capability to the interrupt line.
786 */
787 if (chip->client->irq) {
788 disable_irq(chip->client->irq);
789 enable_irq_wake(chip->client->irq);
790 }
791
792 return 0;
793}
794
795static int max17042_resume(struct device *dev)
796{
797 struct max17042_chip *chip = dev_get_drvdata(dev);
798
799 if (chip->client->irq) {
800 disable_irq_wake(chip->client->irq);
801 enable_irq(chip->client->irq);
802 /* re-program the SOC thresholds to 1% change */
803 max17042_set_soc_threshold(chip, 1);
804 }
805
806 return 0;
807}
Ramakrishna Pallala48bc1772012-03-27 02:23:40 +0530808#endif
809
Manish Badarkhe3d23c7f2013-10-14 10:56:51 +0530810static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
811 max17042_resume);
812
Karol Lewandowski38322462012-02-22 19:06:22 +0100813#ifdef CONFIG_OF
814static const struct of_device_id max17042_dt_match[] = {
815 { .compatible = "maxim,max17042" },
Ramakrishna Pallala9a8422d2012-05-05 14:34:26 +0530816 { .compatible = "maxim,max17047" },
817 { .compatible = "maxim,max17050" },
Karol Lewandowski38322462012-02-22 19:06:22 +0100818 { },
819};
820MODULE_DEVICE_TABLE(of, max17042_dt_match);
821#endif
822
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900823static const struct i2c_device_id max17042_id[] = {
Beomho Seo709c2c72015-04-03 17:26:08 +0900824 { "max17042", MAXIM_DEVICE_TYPE_MAX17042 },
825 { "max17047", MAXIM_DEVICE_TYPE_MAX17047 },
826 { "max17050", MAXIM_DEVICE_TYPE_MAX17050 },
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900827 { }
828};
829MODULE_DEVICE_TABLE(i2c, max17042_id);
830
831static struct i2c_driver max17042_i2c_driver = {
832 .driver = {
833 .name = "max17042",
Karol Lewandowski38322462012-02-22 19:06:22 +0100834 .of_match_table = of_match_ptr(max17042_dt_match),
Manish Badarkhe3d23c7f2013-10-14 10:56:51 +0530835 .pm = &max17042_pm_ops,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900836 },
837 .probe = max17042_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500838 .remove = max17042_remove,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900839 .id_table = max17042_id,
840};
Axel Lin5ff92e72012-01-21 14:42:54 +0800841module_i2c_driver(max17042_i2c_driver);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900842
843MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
844MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
845MODULE_LICENSE("GPL");