max17042_battery.c 23 KB

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