max17042_battery.c 21 KB

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