max17042_battery.c 27 KB

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