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