max17042_battery.c 30 KB

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