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 | ||
359ab9f5 MH |
66 | struct max17042_chip { |
67 | struct i2c_client *client; | |
39e7213e | 68 | struct regmap *regmap; |
297d716f | 69 | struct power_supply *battery; |
9a8422d2 | 70 | enum max170xx_chip_type chip_type; |
359ab9f5 | 71 | struct max17042_platform_data *pdata; |
f3a71a6e RP |
72 | struct work_struct work; |
73 | int init_complete; | |
359ab9f5 MH |
74 | }; |
75 | ||
359ab9f5 | 76 | static enum power_supply_property max17042_battery_props[] = { |
086ef502 DK |
77 | POWER_SUPPLY_PROP_PRESENT, |
78 | POWER_SUPPLY_PROP_CYCLE_COUNT, | |
79 | POWER_SUPPLY_PROP_VOLTAGE_MAX, | |
80 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
359ab9f5 MH |
81 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
82 | POWER_SUPPLY_PROP_VOLTAGE_AVG, | |
a2ebfe2f | 83 | POWER_SUPPLY_PROP_VOLTAGE_OCV, |
359ab9f5 | 84 | POWER_SUPPLY_PROP_CAPACITY, |
086ef502 | 85 | POWER_SUPPLY_PROP_CHARGE_FULL, |
5fc55bc8 | 86 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
086ef502 DK |
87 | POWER_SUPPLY_PROP_TEMP, |
88 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
89 | POWER_SUPPLY_PROP_CURRENT_AVG, | |
359ab9f5 MH |
90 | }; |
91 | ||
92 | static int max17042_get_property(struct power_supply *psy, | |
93 | enum power_supply_property psp, | |
94 | union power_supply_propval *val) | |
95 | { | |
297d716f | 96 | struct max17042_chip *chip = power_supply_get_drvdata(psy); |
39e7213e | 97 | struct regmap *map = chip->regmap; |
60a1f6e4 | 98 | int ret; |
39e7213e | 99 | u32 data; |
359ab9f5 | 100 | |
f3a71a6e RP |
101 | if (!chip->init_complete) |
102 | return -EAGAIN; | |
103 | ||
359ab9f5 | 104 | switch (psp) { |
086ef502 | 105 | case POWER_SUPPLY_PROP_PRESENT: |
39e7213e | 106 | ret = regmap_read(map, MAX17042_STATUS, &data); |
60a1f6e4 RP |
107 | if (ret < 0) |
108 | return ret; | |
109 | ||
39e7213e | 110 | if (data & MAX17042_STATUS_BattAbsent) |
086ef502 DK |
111 | val->intval = 0; |
112 | else | |
113 | val->intval = 1; | |
114 | break; | |
115 | case POWER_SUPPLY_PROP_CYCLE_COUNT: | |
39e7213e | 116 | ret = regmap_read(map, MAX17042_Cycles, &data); |
60a1f6e4 RP |
117 | if (ret < 0) |
118 | return ret; | |
119 | ||
39e7213e | 120 | val->intval = data; |
086ef502 DK |
121 | break; |
122 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: | |
39e7213e | 123 | ret = regmap_read(map, MAX17042_MinMaxVolt, &data); |
60a1f6e4 RP |
124 | if (ret < 0) |
125 | return ret; | |
126 | ||
39e7213e | 127 | val->intval = data >> 8; |
086ef502 DK |
128 | val->intval *= 20000; /* Units of LSB = 20mV */ |
129 | break; | |
130 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: | |
709c2c70 | 131 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
39e7213e | 132 | ret = regmap_read(map, MAX17042_V_empty, &data); |
9a8422d2 | 133 | else |
39e7213e | 134 | ret = regmap_read(map, MAX17047_V_empty, &data); |
60a1f6e4 RP |
135 | if (ret < 0) |
136 | return ret; | |
137 | ||
39e7213e | 138 | val->intval = data >> 7; |
086ef502 DK |
139 | val->intval *= 10000; /* Units of LSB = 10mV */ |
140 | break; | |
359ab9f5 | 141 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
39e7213e | 142 | ret = regmap_read(map, MAX17042_VCELL, &data); |
60a1f6e4 RP |
143 | if (ret < 0) |
144 | return ret; | |
145 | ||
39e7213e | 146 | val->intval = data * 625 / 8; |
359ab9f5 MH |
147 | break; |
148 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | |
39e7213e | 149 | ret = regmap_read(map, MAX17042_AvgVCELL, &data); |
60a1f6e4 RP |
150 | if (ret < 0) |
151 | return ret; | |
152 | ||
39e7213e | 153 | val->intval = data * 625 / 8; |
a2ebfe2f RP |
154 | break; |
155 | case POWER_SUPPLY_PROP_VOLTAGE_OCV: | |
39e7213e | 156 | ret = regmap_read(map, MAX17042_OCVInternal, &data); |
a2ebfe2f RP |
157 | if (ret < 0) |
158 | return ret; | |
159 | ||
39e7213e | 160 | val->intval = data * 625 / 8; |
359ab9f5 MH |
161 | break; |
162 | case POWER_SUPPLY_PROP_CAPACITY: | |
39e7213e | 163 | ret = regmap_read(map, MAX17042_RepSOC, &data); |
60a1f6e4 RP |
164 | if (ret < 0) |
165 | return ret; | |
166 | ||
39e7213e | 167 | val->intval = data >> 8; |
359ab9f5 | 168 | break; |
086ef502 | 169 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
39e7213e | 170 | ret = regmap_read(map, MAX17042_FullCAP, &data); |
60a1f6e4 RP |
171 | if (ret < 0) |
172 | return ret; | |
173 | ||
39e7213e | 174 | val->intval = data * 1000 / 2; |
5fc55bc8 RP |
175 | break; |
176 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: | |
39e7213e | 177 | ret = regmap_read(map, MAX17042_QH, &data); |
5fc55bc8 RP |
178 | if (ret < 0) |
179 | return ret; | |
180 | ||
39e7213e | 181 | val->intval = data * 1000 / 2; |
086ef502 DK |
182 | break; |
183 | case POWER_SUPPLY_PROP_TEMP: | |
39e7213e | 184 | ret = regmap_read(map, MAX17042_TEMP, &data); |
60a1f6e4 RP |
185 | if (ret < 0) |
186 | return ret; | |
187 | ||
39e7213e | 188 | val->intval = data; |
086ef502 DK |
189 | /* 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) { | |
39e7213e | 200 | ret = regmap_read(map, MAX17042_Current, &data); |
60a1f6e4 RP |
201 | if (ret < 0) |
202 | return ret; | |
203 | ||
39e7213e | 204 | val->intval = data; |
086ef502 DK |
205 | if (val->intval & 0x8000) { |
206 | /* Negative */ | |
207 | val->intval = ~val->intval & 0x7fff; | |
208 | val->intval++; | |
209 | val->intval *= -1; | |
210 | } | |
91d8b0d6 | 211 | val->intval *= 1562500 / chip->pdata->r_sns; |
086ef502 DK |
212 | } else { |
213 | return -EINVAL; | |
214 | } | |
215 | break; | |
216 | case POWER_SUPPLY_PROP_CURRENT_AVG: | |
217 | if (chip->pdata->enable_current_sense) { | |
39e7213e | 218 | ret = regmap_read(map, MAX17042_AvgCurrent, &data); |
60a1f6e4 RP |
219 | if (ret < 0) |
220 | return ret; | |
221 | ||
39e7213e | 222 | val->intval = data; |
086ef502 DK |
223 | 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; | |
359ab9f5 MH |
234 | default: |
235 | return -EINVAL; | |
236 | } | |
237 | return 0; | |
238 | } | |
239 | ||
39e7213e | 240 | static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value) |
f3a71a6e RP |
241 | { |
242 | int retries = 8; | |
243 | int ret; | |
39e7213e | 244 | u32 read_value; |
f3a71a6e RP |
245 | |
246 | do { | |
39e7213e JL |
247 | ret = regmap_write(map, reg, value); |
248 | regmap_read(map, reg, &read_value); | |
f3a71a6e RP |
249 | if (read_value != value) { |
250 | ret = -EIO; | |
251 | retries--; | |
252 | } | |
253 | } while (retries && read_value != value); | |
254 | ||
255 | if (ret < 0) | |
39e7213e | 256 | pr_err("%s: err %d\n", __func__, ret); |
f3a71a6e RP |
257 | |
258 | return ret; | |
259 | } | |
260 | ||
39e7213e JL |
261 | static inline void max17042_override_por(struct regmap *map, |
262 | u8 reg, u16 value) | |
f3a71a6e RP |
263 | { |
264 | if (value) | |
39e7213e | 265 | regmap_write(map, reg, value); |
f3a71a6e RP |
266 | } |
267 | ||
268 | static inline void max10742_unlock_model(struct max17042_chip *chip) | |
269 | { | |
39e7213e JL |
270 | struct regmap *map = chip->regmap; |
271 | regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1); | |
272 | regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2); | |
f3a71a6e RP |
273 | } |
274 | ||
275 | static inline void max10742_lock_model(struct max17042_chip *chip) | |
276 | { | |
39e7213e JL |
277 | struct regmap *map = chip->regmap; |
278 | ||
279 | regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1); | |
280 | regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2); | |
f3a71a6e RP |
281 | } |
282 | ||
283 | static inline void max17042_write_model_data(struct max17042_chip *chip, | |
284 | u8 addr, int size) | |
285 | { | |
39e7213e | 286 | struct regmap *map = chip->regmap; |
f3a71a6e RP |
287 | int i; |
288 | for (i = 0; i < size; i++) | |
39e7213e JL |
289 | regmap_write(map, addr + i, |
290 | chip->pdata->config_data->cell_char_tbl[i]); | |
f3a71a6e RP |
291 | } |
292 | ||
293 | static inline void max17042_read_model_data(struct max17042_chip *chip, | |
39e7213e | 294 | u8 addr, u32 *data, int size) |
f3a71a6e | 295 | { |
39e7213e | 296 | struct regmap *map = chip->regmap; |
f3a71a6e RP |
297 | int i; |
298 | ||
299 | for (i = 0; i < size; i++) | |
39e7213e | 300 | regmap_read(map, addr + i, &data[i]); |
f3a71a6e RP |
301 | } |
302 | ||
303 | static 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 | ||
319 | static int max17042_init_model(struct max17042_chip *chip) | |
320 | { | |
321 | int ret; | |
1ef3d8fb | 322 | int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); |
39e7213e | 323 | u32 *temp_data; |
f3a71a6e | 324 | |
1ef3d8fb | 325 | temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); |
f3a71a6e RP |
326 | 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, | |
39e7213e | 338 | (u16 *)temp_data, |
f3a71a6e RP |
339 | table_size); |
340 | ||
341 | max10742_lock_model(chip); | |
342 | kfree(temp_data); | |
343 | ||
344 | return ret; | |
345 | } | |
346 | ||
347 | static int max17042_verify_model_lock(struct max17042_chip *chip) | |
348 | { | |
349 | int i; | |
1ef3d8fb | 350 | int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl); |
39e7213e | 351 | u32 *temp_data; |
f3a71a6e RP |
352 | int ret = 0; |
353 | ||
1ef3d8fb | 354 | temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL); |
f3a71a6e RP |
355 | 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 | ||
368 | static void max17042_write_config_regs(struct max17042_chip *chip) | |
369 | { | |
370 | struct max17042_config_data *config = chip->pdata->config_data; | |
39e7213e | 371 | struct regmap *map = chip->regmap; |
f3a71a6e | 372 | |
39e7213e JL |
373 | regmap_write(map, MAX17042_CONFIG, config->config); |
374 | regmap_write(map, MAX17042_LearnCFG, config->learn_cfg); | |
375 | regmap_write(map, MAX17042_FilterCFG, | |
f3a71a6e | 376 | config->filter_cfg); |
39e7213e | 377 | regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg); |
709c2c70 BS |
378 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 || |
379 | chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050) | |
39e7213e | 380 | regmap_write(map, MAX17047_FullSOCThr, |
9a8422d2 | 381 | config->full_soc_thresh); |
f3a71a6e RP |
382 | } |
383 | ||
384 | static void max17042_write_custom_regs(struct max17042_chip *chip) | |
385 | { | |
386 | struct max17042_config_data *config = chip->pdata->config_data; | |
39e7213e | 387 | struct regmap *map = chip->regmap; |
f3a71a6e | 388 | |
39e7213e JL |
389 | 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); | |
709c2c70 | 392 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) { |
39e7213e JL |
393 | regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco); |
394 | max17042_write_verify_reg(map, MAX17042_K_empty0, | |
9a8422d2 RP |
395 | config->kempty0); |
396 | } else { | |
39e7213e | 397 | max17042_write_verify_reg(map, MAX17047_QRTbl00, |
9a8422d2 | 398 | config->qrtbl00); |
39e7213e | 399 | max17042_write_verify_reg(map, MAX17047_QRTbl10, |
9a8422d2 | 400 | config->qrtbl10); |
39e7213e | 401 | max17042_write_verify_reg(map, MAX17047_QRTbl20, |
9a8422d2 | 402 | config->qrtbl20); |
39e7213e | 403 | max17042_write_verify_reg(map, MAX17047_QRTbl30, |
9a8422d2 RP |
404 | config->qrtbl30); |
405 | } | |
f3a71a6e RP |
406 | } |
407 | ||
408 | static void max17042_update_capacity_regs(struct max17042_chip *chip) | |
409 | { | |
410 | struct max17042_config_data *config = chip->pdata->config_data; | |
39e7213e | 411 | struct regmap *map = chip->regmap; |
f3a71a6e | 412 | |
39e7213e | 413 | max17042_write_verify_reg(map, MAX17042_FullCAP, |
f3a71a6e | 414 | config->fullcap); |
39e7213e JL |
415 | regmap_write(map, MAX17042_DesignCap, config->design_cap); |
416 | max17042_write_verify_reg(map, MAX17042_FullCAPNom, | |
f3a71a6e RP |
417 | config->fullcapnom); |
418 | } | |
419 | ||
420 | static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) | |
421 | { | |
39e7213e JL |
422 | unsigned int vfSoc; |
423 | struct regmap *map = chip->regmap; | |
f3a71a6e | 424 | |
39e7213e JL |
425 | 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); | |
f3a71a6e RP |
429 | } |
430 | ||
431 | static void max17042_load_new_capacity_params(struct max17042_chip *chip) | |
432 | { | |
39e7213e | 433 | u32 full_cap0, rep_cap, dq_acc, vfSoc; |
f3a71a6e RP |
434 | u32 rem_cap; |
435 | ||
436 | struct max17042_config_data *config = chip->pdata->config_data; | |
39e7213e | 437 | struct regmap *map = chip->regmap; |
f3a71a6e | 438 | |
39e7213e JL |
439 | regmap_read(map, MAX17042_FullCAP0, &full_cap0); |
440 | regmap_read(map, MAX17042_VFSOC, &vfSoc); | |
f3a71a6e RP |
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; | |
39e7213e | 447 | max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap); |
f3a71a6e | 448 | |
39e7213e JL |
449 | rep_cap = rem_cap; |
450 | max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap); | |
f3a71a6e RP |
451 | |
452 | /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */ | |
453 | dq_acc = config->fullcap / dQ_ACC_DIV; | |
39e7213e JL |
454 | max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc); |
455 | max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200); | |
f3a71a6e | 456 | |
39e7213e | 457 | max17042_write_verify_reg(map, MAX17042_FullCAP, |
f3a71a6e | 458 | config->fullcap); |
39e7213e | 459 | regmap_write(map, MAX17042_DesignCap, |
f3a71a6e | 460 | config->design_cap); |
39e7213e | 461 | max17042_write_verify_reg(map, MAX17042_FullCAPNom, |
f3a71a6e | 462 | config->fullcapnom); |
9a8422d2 | 463 | /* Update SOC register with new SOC */ |
39e7213e | 464 | regmap_write(map, MAX17042_RepSOC, vfSoc); |
f3a71a6e RP |
465 | } |
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 | */ | |
472 | static inline void max17042_override_por_values(struct max17042_chip *chip) | |
473 | { | |
39e7213e | 474 | struct regmap *map = chip->regmap; |
f3a71a6e RP |
475 | struct max17042_config_data *config = chip->pdata->config_data; |
476 | ||
39e7213e JL |
477 | 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); | |
481 | ||
482 | 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); | |
488 | ||
489 | max17042_override_por(map, MAX17042_DesignCap, config->design_cap); | |
490 | max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term); | |
491 | ||
492 | 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); | |
498 | ||
499 | max17042_override_por(map, MAX17042_FullCAP, config->fullcap); | |
500 | max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom); | |
709c2c70 | 501 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
39e7213e | 502 | max17042_override_por(map, MAX17042_SOC_empty, |
9a8422d2 | 503 | config->socempty); |
39e7213e JL |
504 | 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); | |
f3a71a6e | 507 | |
709c2c70 | 508 | if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) |
39e7213e | 509 | max17042_override_por(map, MAX17042_V_empty, config->vempty); |
9a8422d2 | 510 | else |
39e7213e JL |
511 | 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); | |
9a8422d2 | 517 | if (chip->chip_type) { |
39e7213e JL |
518 | max17042_override_por(map, MAX17042_EmptyTempCo, |
519 | config->empty_tempco); | |
520 | max17042_override_por(map, MAX17042_K_empty0, | |
521 | config->kempty0); | |
9a8422d2 | 522 | } |
f3a71a6e RP |
523 | } |
524 | ||
525 | static int max17042_init_chip(struct max17042_chip *chip) | |
526 | { | |
39e7213e | 527 | struct regmap *map = chip->regmap; |
f3a71a6e | 528 | int ret; |
f3a71a6e RP |
529 | |
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 | } | |
a879f19f AC |
546 | |
547 | ret = max17042_verify_model_lock(chip); | |
f3a71a6e RP |
548 | 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 */ | |
bc352686 | 571 | regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0); |
f3a71a6e RP |
572 | return 0; |
573 | } | |
574 | ||
e5f3872d RP |
575 | static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off) |
576 | { | |
39e7213e JL |
577 | struct regmap *map = chip->regmap; |
578 | u32 soc, soc_tr; | |
e5f3872d RP |
579 | |
580 | /* program interrupt thesholds such that we should | |
581 | * get interrupt for every 'off' perc change in the soc | |
582 | */ | |
39e7213e JL |
583 | regmap_read(map, MAX17042_RepSOC, &soc); |
584 | soc >>= 8; | |
e5f3872d RP |
585 | soc_tr = (soc + off) << 8; |
586 | soc_tr |= (soc - off); | |
39e7213e | 587 | regmap_write(map, MAX17042_SALRT_Th, soc_tr); |
e5f3872d RP |
588 | } |
589 | ||
e5f3872d RP |
590 | static irqreturn_t max17042_thread_handler(int id, void *dev) |
591 | { | |
592 | struct max17042_chip *chip = dev; | |
39e7213e | 593 | u32 val; |
e5f3872d | 594 | |
39e7213e | 595 | regmap_read(chip->regmap, MAX17042_STATUS, &val); |
5cdd4d7f RP |
596 | if ((val & STATUS_INTR_SOCMIN_BIT) || |
597 | (val & STATUS_INTR_SOCMAX_BIT)) { | |
e5f3872d RP |
598 | dev_info(&chip->client->dev, "SOC threshold INTR\n"); |
599 | max17042_set_soc_threshold(chip, 1); | |
600 | } | |
601 | ||
297d716f | 602 | power_supply_changed(chip->battery); |
e5f3872d RP |
603 | return IRQ_HANDLED; |
604 | } | |
f3a71a6e RP |
605 | |
606 | static 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 | ||
3832246d KL |
622 | #ifdef CONFIG_OF |
623 | static struct max17042_platform_data * | |
624 | max17042_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 | |
649 | static struct max17042_platform_data * | |
650 | max17042_get_pdata(struct device *dev) | |
651 | { | |
652 | return dev->platform_data; | |
653 | } | |
654 | #endif | |
655 | ||
e0291285 | 656 | static const struct regmap_config max17042_regmap_config = { |
39e7213e JL |
657 | .reg_bits = 8, |
658 | .val_bits = 16, | |
659 | .val_format_endian = REGMAP_ENDIAN_NATIVE, | |
660 | }; | |
661 | ||
297d716f KK |
662 | static 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 | ||
670 | static 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 | ||
c8afa640 | 678 | static int max17042_probe(struct i2c_client *client, |
359ab9f5 MH |
679 | const struct i2c_device_id *id) |
680 | { | |
681 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
297d716f KK |
682 | const struct power_supply_desc *max17042_desc = &max17042_psy_desc; |
683 | struct power_supply_config psy_cfg = {}; | |
359ab9f5 MH |
684 | struct max17042_chip *chip; |
685 | int ret; | |
39e7213e JL |
686 | int i; |
687 | u32 val; | |
359ab9f5 MH |
688 | |
689 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) | |
690 | return -EIO; | |
691 | ||
2f3b4342 | 692 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
359ab9f5 MH |
693 | if (!chip) |
694 | return -ENOMEM; | |
695 | ||
696 | chip->client = client; | |
39e7213e JL |
697 | 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 | ||
3832246d KL |
703 | 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 | } | |
359ab9f5 MH |
708 | |
709 | i2c_set_clientdata(client, chip); | |
709c2c70 | 710 | chip->chip_type = id->driver_data; |
297d716f | 711 | psy_cfg.drv_data = chip; |
359ab9f5 | 712 | |
086ef502 DK |
713 | /* When current is not measured, |
714 | * CURRENT_NOW and CURRENT_AVG properties should be invisible. */ | |
715 | if (!chip->pdata->enable_current_sense) | |
297d716f | 716 | max17042_desc = &max17042_no_current_sense_psy_desc; |
086ef502 | 717 | |
4cfa892c PR |
718 | if (chip->pdata->r_sns == 0) |
719 | chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; | |
720 | ||
086ef502 | 721 | if (chip->pdata->init_data) |
39e7213e JL |
722 | 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); | |
086ef502 | 726 | |
359ab9f5 | 727 | if (!chip->pdata->enable_current_sense) { |
39e7213e JL |
728 | regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000); |
729 | regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003); | |
730 | regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007); | |
359ab9f5 MH |
731 | } |
732 | ||
297d716f KK |
733 | chip->battery = power_supply_register(&client->dev, max17042_desc, |
734 | &psy_cfg); | |
735 | if (IS_ERR(chip->battery)) { | |
243e3527 | 736 | dev_err(&client->dev, "failed: power supply register\n"); |
297d716f | 737 | return PTR_ERR(chip->battery); |
243e3527 RP |
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, | |
297d716f | 744 | chip->battery->desc->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); | |
297d716f | 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 | 823 | static const struct i2c_device_id max17042_id[] = { |
709c2c70 BS |
824 | { "max17042", MAXIM_DEVICE_TYPE_MAX17042 }, |
825 | { "max17047", MAXIM_DEVICE_TYPE_MAX17047 }, | |
826 | { "max17050", MAXIM_DEVICE_TYPE_MAX17050 }, | |
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"); |