axp288_fuel_gauge.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * axp288_fuel_gauge.c - Xpower AXP288 PMIC Fuel Gauge Driver
  3. *
  4. * Copyright (C) 2014 Intel Corporation
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  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; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <linux/regmap.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/device.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/mfd/axp20x.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/power_supply.h>
  29. #include <linux/iio/consumer.h>
  30. #include <linux/debugfs.h>
  31. #include <linux/seq_file.h>
  32. #define CHRG_STAT_BAT_SAFE_MODE (1 << 3)
  33. #define CHRG_STAT_BAT_VALID (1 << 4)
  34. #define CHRG_STAT_BAT_PRESENT (1 << 5)
  35. #define CHRG_STAT_CHARGING (1 << 6)
  36. #define CHRG_STAT_PMIC_OTP (1 << 7)
  37. #define CHRG_CCCV_CC_MASK 0xf /* 4 bits */
  38. #define CHRG_CCCV_CC_BIT_POS 0
  39. #define CHRG_CCCV_CC_OFFSET 200 /* 200mA */
  40. #define CHRG_CCCV_CC_LSB_RES 200 /* 200mA */
  41. #define CHRG_CCCV_ITERM_20P (1 << 4) /* 20% of CC */
  42. #define CHRG_CCCV_CV_MASK 0x60 /* 2 bits */
  43. #define CHRG_CCCV_CV_BIT_POS 5
  44. #define CHRG_CCCV_CV_4100MV 0x0 /* 4.10V */
  45. #define CHRG_CCCV_CV_4150MV 0x1 /* 4.15V */
  46. #define CHRG_CCCV_CV_4200MV 0x2 /* 4.20V */
  47. #define CHRG_CCCV_CV_4350MV 0x3 /* 4.35V */
  48. #define CHRG_CCCV_CHG_EN (1 << 7)
  49. #define CV_4100 4100 /* 4100mV */
  50. #define CV_4150 4150 /* 4150mV */
  51. #define CV_4200 4200 /* 4200mV */
  52. #define CV_4350 4350 /* 4350mV */
  53. #define TEMP_IRQ_CFG_QWBTU (1 << 0)
  54. #define TEMP_IRQ_CFG_WBTU (1 << 1)
  55. #define TEMP_IRQ_CFG_QWBTO (1 << 2)
  56. #define TEMP_IRQ_CFG_WBTO (1 << 3)
  57. #define TEMP_IRQ_CFG_MASK 0xf
  58. #define FG_IRQ_CFG_LOWBATT_WL2 (1 << 0)
  59. #define FG_IRQ_CFG_LOWBATT_WL1 (1 << 1)
  60. #define FG_IRQ_CFG_LOWBATT_MASK 0x3
  61. #define LOWBAT_IRQ_STAT_LOWBATT_WL2 (1 << 0)
  62. #define LOWBAT_IRQ_STAT_LOWBATT_WL1 (1 << 1)
  63. #define FG_CNTL_OCV_ADJ_STAT (1 << 2)
  64. #define FG_CNTL_OCV_ADJ_EN (1 << 3)
  65. #define FG_CNTL_CAP_ADJ_STAT (1 << 4)
  66. #define FG_CNTL_CAP_ADJ_EN (1 << 5)
  67. #define FG_CNTL_CC_EN (1 << 6)
  68. #define FG_CNTL_GAUGE_EN (1 << 7)
  69. #define FG_REP_CAP_VALID (1 << 7)
  70. #define FG_REP_CAP_VAL_MASK 0x7F
  71. #define FG_DES_CAP1_VALID (1 << 7)
  72. #define FG_DES_CAP1_VAL_MASK 0x7F
  73. #define FG_DES_CAP0_VAL_MASK 0xFF
  74. #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */
  75. #define FG_CC_MTR1_VALID (1 << 7)
  76. #define FG_CC_MTR1_VAL_MASK 0x7F
  77. #define FG_CC_MTR0_VAL_MASK 0xFF
  78. #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */
  79. #define FG_OCV_CAP_VALID (1 << 7)
  80. #define FG_OCV_CAP_VAL_MASK 0x7F
  81. #define FG_CC_CAP_VALID (1 << 7)
  82. #define FG_CC_CAP_VAL_MASK 0x7F
  83. #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */
  84. #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */
  85. #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */
  86. #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */
  87. #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */
  88. #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */
  89. #define STATUS_MON_DELAY_JIFFIES (HZ * 60) /*60 sec */
  90. #define NR_RETRY_CNT 3
  91. #define DEV_NAME "axp288_fuel_gauge"
  92. /* 1.1mV per LSB expressed in uV */
  93. #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10)
  94. /* properties converted to tenths of degrees, uV, uA, uW */
  95. #define PROP_TEMP(a) ((a) * 10)
  96. #define UNPROP_TEMP(a) ((a) / 10)
  97. #define PROP_VOLT(a) ((a) * 1000)
  98. #define PROP_CURR(a) ((a) * 1000)
  99. #define AXP288_FG_INTR_NUM 6
  100. enum {
  101. QWBTU_IRQ = 0,
  102. WBTU_IRQ,
  103. QWBTO_IRQ,
  104. WBTO_IRQ,
  105. WL2_IRQ,
  106. WL1_IRQ,
  107. };
  108. struct axp288_fg_info {
  109. struct platform_device *pdev;
  110. struct axp20x_fg_pdata *pdata;
  111. struct regmap *regmap;
  112. struct regmap_irq_chip_data *regmap_irqc;
  113. int irq[AXP288_FG_INTR_NUM];
  114. struct power_supply *bat;
  115. struct mutex lock;
  116. int status;
  117. struct delayed_work status_monitor;
  118. struct dentry *debug_file;
  119. };
  120. static enum power_supply_property fuel_gauge_props[] = {
  121. POWER_SUPPLY_PROP_STATUS,
  122. POWER_SUPPLY_PROP_PRESENT,
  123. POWER_SUPPLY_PROP_HEALTH,
  124. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  125. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  126. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  127. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  128. POWER_SUPPLY_PROP_CURRENT_NOW,
  129. POWER_SUPPLY_PROP_CAPACITY,
  130. POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
  131. POWER_SUPPLY_PROP_TEMP,
  132. POWER_SUPPLY_PROP_TEMP_MAX,
  133. POWER_SUPPLY_PROP_TEMP_MIN,
  134. POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
  135. POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
  136. POWER_SUPPLY_PROP_TECHNOLOGY,
  137. POWER_SUPPLY_PROP_CHARGE_FULL,
  138. POWER_SUPPLY_PROP_CHARGE_NOW,
  139. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  140. POWER_SUPPLY_PROP_MODEL_NAME,
  141. };
  142. static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
  143. {
  144. int ret, i;
  145. unsigned int val;
  146. for (i = 0; i < NR_RETRY_CNT; i++) {
  147. ret = regmap_read(info->regmap, reg, &val);
  148. if (ret == -EBUSY)
  149. continue;
  150. else
  151. break;
  152. }
  153. if (ret < 0)
  154. dev_err(&info->pdev->dev, "axp288 reg read err:%d\n", ret);
  155. return val;
  156. }
  157. static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
  158. {
  159. int ret;
  160. ret = regmap_write(info->regmap, reg, (unsigned int)val);
  161. if (ret < 0)
  162. dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret);
  163. return ret;
  164. }
  165. static int pmic_read_adc_val(const char *name, int *raw_val,
  166. struct axp288_fg_info *info)
  167. {
  168. int ret, val = 0;
  169. struct iio_channel *indio_chan;
  170. indio_chan = iio_channel_get(NULL, name);
  171. if (IS_ERR_OR_NULL(indio_chan)) {
  172. ret = PTR_ERR(indio_chan);
  173. goto exit;
  174. }
  175. ret = iio_read_channel_raw(indio_chan, &val);
  176. if (ret < 0) {
  177. dev_err(&info->pdev->dev,
  178. "IIO channel read error: %x, %x\n", ret, val);
  179. goto err_exit;
  180. }
  181. dev_dbg(&info->pdev->dev, "adc raw val=%x\n", val);
  182. *raw_val = val;
  183. err_exit:
  184. iio_channel_release(indio_chan);
  185. exit:
  186. return ret;
  187. }
  188. #ifdef CONFIG_DEBUG_FS
  189. static int fuel_gauge_debug_show(struct seq_file *s, void *data)
  190. {
  191. struct axp288_fg_info *info = s->private;
  192. int raw_val, ret;
  193. seq_printf(s, " PWR_STATUS[%02x] : %02x\n",
  194. AXP20X_PWR_INPUT_STATUS,
  195. fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS));
  196. seq_printf(s, "PWR_OP_MODE[%02x] : %02x\n",
  197. AXP20X_PWR_OP_MODE,
  198. fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE));
  199. seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n",
  200. AXP20X_CHRG_CTRL1,
  201. fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1));
  202. seq_printf(s, " VLTF[%02x] : %02x\n",
  203. AXP20X_V_LTF_DISCHRG,
  204. fuel_gauge_reg_readb(info, AXP20X_V_LTF_DISCHRG));
  205. seq_printf(s, " VHTF[%02x] : %02x\n",
  206. AXP20X_V_HTF_DISCHRG,
  207. fuel_gauge_reg_readb(info, AXP20X_V_HTF_DISCHRG));
  208. seq_printf(s, " CC_CTRL[%02x] : %02x\n",
  209. AXP20X_CC_CTRL,
  210. fuel_gauge_reg_readb(info, AXP20X_CC_CTRL));
  211. seq_printf(s, "BATTERY CAP[%02x] : %02x\n",
  212. AXP20X_FG_RES,
  213. fuel_gauge_reg_readb(info, AXP20X_FG_RES));
  214. seq_printf(s, " FG_RDC1[%02x] : %02x\n",
  215. AXP288_FG_RDC1_REG,
  216. fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG));
  217. seq_printf(s, " FG_RDC0[%02x] : %02x\n",
  218. AXP288_FG_RDC0_REG,
  219. fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG));
  220. seq_printf(s, " FG_OCVH[%02x] : %02x\n",
  221. AXP288_FG_OCVH_REG,
  222. fuel_gauge_reg_readb(info, AXP288_FG_OCVH_REG));
  223. seq_printf(s, " FG_OCVL[%02x] : %02x\n",
  224. AXP288_FG_OCVL_REG,
  225. fuel_gauge_reg_readb(info, AXP288_FG_OCVL_REG));
  226. seq_printf(s, "FG_DES_CAP1[%02x] : %02x\n",
  227. AXP288_FG_DES_CAP1_REG,
  228. fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG));
  229. seq_printf(s, "FG_DES_CAP0[%02x] : %02x\n",
  230. AXP288_FG_DES_CAP0_REG,
  231. fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP0_REG));
  232. seq_printf(s, " FG_CC_MTR1[%02x] : %02x\n",
  233. AXP288_FG_CC_MTR1_REG,
  234. fuel_gauge_reg_readb(info, AXP288_FG_CC_MTR1_REG));
  235. seq_printf(s, " FG_CC_MTR0[%02x] : %02x\n",
  236. AXP288_FG_CC_MTR0_REG,
  237. fuel_gauge_reg_readb(info, AXP288_FG_CC_MTR0_REG));
  238. seq_printf(s, " FG_OCV_CAP[%02x] : %02x\n",
  239. AXP288_FG_OCV_CAP_REG,
  240. fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG));
  241. seq_printf(s, " FG_CC_CAP[%02x] : %02x\n",
  242. AXP288_FG_CC_CAP_REG,
  243. fuel_gauge_reg_readb(info, AXP288_FG_CC_CAP_REG));
  244. seq_printf(s, " FG_LOW_CAP[%02x] : %02x\n",
  245. AXP288_FG_LOW_CAP_REG,
  246. fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG));
  247. seq_printf(s, "TUNING_CTL0[%02x] : %02x\n",
  248. AXP288_FG_TUNE0,
  249. fuel_gauge_reg_readb(info, AXP288_FG_TUNE0));
  250. seq_printf(s, "TUNING_CTL1[%02x] : %02x\n",
  251. AXP288_FG_TUNE1,
  252. fuel_gauge_reg_readb(info, AXP288_FG_TUNE1));
  253. seq_printf(s, "TUNING_CTL2[%02x] : %02x\n",
  254. AXP288_FG_TUNE2,
  255. fuel_gauge_reg_readb(info, AXP288_FG_TUNE2));
  256. seq_printf(s, "TUNING_CTL3[%02x] : %02x\n",
  257. AXP288_FG_TUNE3,
  258. fuel_gauge_reg_readb(info, AXP288_FG_TUNE3));
  259. seq_printf(s, "TUNING_CTL4[%02x] : %02x\n",
  260. AXP288_FG_TUNE4,
  261. fuel_gauge_reg_readb(info, AXP288_FG_TUNE4));
  262. seq_printf(s, "TUNING_CTL5[%02x] : %02x\n",
  263. AXP288_FG_TUNE5,
  264. fuel_gauge_reg_readb(info, AXP288_FG_TUNE5));
  265. ret = pmic_read_adc_val("axp288-batt-temp", &raw_val, info);
  266. if (ret >= 0)
  267. seq_printf(s, "axp288-batttemp : %d\n", raw_val);
  268. ret = pmic_read_adc_val("axp288-pmic-temp", &raw_val, info);
  269. if (ret >= 0)
  270. seq_printf(s, "axp288-pmictemp : %d\n", raw_val);
  271. ret = pmic_read_adc_val("axp288-system-temp", &raw_val, info);
  272. if (ret >= 0)
  273. seq_printf(s, "axp288-systtemp : %d\n", raw_val);
  274. ret = pmic_read_adc_val("axp288-chrg-curr", &raw_val, info);
  275. if (ret >= 0)
  276. seq_printf(s, "axp288-chrgcurr : %d\n", raw_val);
  277. ret = pmic_read_adc_val("axp288-chrg-d-curr", &raw_val, info);
  278. if (ret >= 0)
  279. seq_printf(s, "axp288-dchrgcur : %d\n", raw_val);
  280. ret = pmic_read_adc_val("axp288-batt-volt", &raw_val, info);
  281. if (ret >= 0)
  282. seq_printf(s, "axp288-battvolt : %d\n", raw_val);
  283. return 0;
  284. }
  285. static int debug_open(struct inode *inode, struct file *file)
  286. {
  287. return single_open(file, fuel_gauge_debug_show, inode->i_private);
  288. }
  289. static const struct file_operations fg_debug_fops = {
  290. .open = debug_open,
  291. .read = seq_read,
  292. .llseek = seq_lseek,
  293. .release = single_release,
  294. };
  295. static void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
  296. {
  297. info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL,
  298. info, &fg_debug_fops);
  299. }
  300. static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
  301. {
  302. debugfs_remove(info->debug_file);
  303. }
  304. #else
  305. static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
  306. {
  307. }
  308. static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
  309. {
  310. }
  311. #endif
  312. static void fuel_gauge_get_status(struct axp288_fg_info *info)
  313. {
  314. int pwr_stat, ret;
  315. int charge, discharge;
  316. pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
  317. if (pwr_stat < 0) {
  318. dev_err(&info->pdev->dev,
  319. "PWR STAT read failed:%d\n", pwr_stat);
  320. return;
  321. }
  322. ret = pmic_read_adc_val("axp288-chrg-curr", &charge, info);
  323. if (ret < 0) {
  324. dev_err(&info->pdev->dev,
  325. "ADC charge current read failed:%d\n", ret);
  326. return;
  327. }
  328. ret = pmic_read_adc_val("axp288-chrg-d-curr", &discharge, info);
  329. if (ret < 0) {
  330. dev_err(&info->pdev->dev,
  331. "ADC discharge current read failed:%d\n", ret);
  332. return;
  333. }
  334. if (charge > 0)
  335. info->status = POWER_SUPPLY_STATUS_CHARGING;
  336. else if (discharge > 0)
  337. info->status = POWER_SUPPLY_STATUS_DISCHARGING;
  338. else {
  339. if (pwr_stat & CHRG_STAT_BAT_PRESENT)
  340. info->status = POWER_SUPPLY_STATUS_FULL;
  341. else
  342. info->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  343. }
  344. }
  345. static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt)
  346. {
  347. int ret = 0, raw_val;
  348. ret = pmic_read_adc_val("axp288-batt-volt", &raw_val, info);
  349. if (ret < 0)
  350. goto vbatt_read_fail;
  351. *vbatt = VOLTAGE_FROM_ADC(raw_val);
  352. vbatt_read_fail:
  353. return ret;
  354. }
  355. static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur)
  356. {
  357. int ret, value = 0;
  358. int charge, discharge;
  359. ret = pmic_read_adc_val("axp288-chrg-curr", &charge, info);
  360. if (ret < 0)
  361. goto current_read_fail;
  362. ret = pmic_read_adc_val("axp288-chrg-d-curr", &discharge, info);
  363. if (ret < 0)
  364. goto current_read_fail;
  365. if (charge > 0)
  366. value = charge;
  367. else if (discharge > 0)
  368. value = -1 * discharge;
  369. *cur = value;
  370. current_read_fail:
  371. return ret;
  372. }
  373. static int temp_to_adc(struct axp288_fg_info *info, int tval)
  374. {
  375. int rntc = 0, i, ret, adc_val;
  376. int rmin, rmax, tmin, tmax;
  377. int tcsz = info->pdata->tcsz;
  378. /* get the Rntc resitance value for this temp */
  379. if (tval > info->pdata->thermistor_curve[0][1]) {
  380. rntc = info->pdata->thermistor_curve[0][0];
  381. } else if (tval <= info->pdata->thermistor_curve[tcsz-1][1]) {
  382. rntc = info->pdata->thermistor_curve[tcsz-1][0];
  383. } else {
  384. for (i = 1; i < tcsz; i++) {
  385. if (tval > info->pdata->thermistor_curve[i][1]) {
  386. rmin = info->pdata->thermistor_curve[i-1][0];
  387. rmax = info->pdata->thermistor_curve[i][0];
  388. tmin = info->pdata->thermistor_curve[i-1][1];
  389. tmax = info->pdata->thermistor_curve[i][1];
  390. rntc = rmin + ((rmax - rmin) *
  391. (tval - tmin) / (tmax - tmin));
  392. break;
  393. }
  394. }
  395. }
  396. /* we need the current to calculate the proper adc voltage */
  397. ret = fuel_gauge_reg_readb(info, AXP20X_ADC_RATE);
  398. if (ret < 0) {
  399. dev_err(&info->pdev->dev, "%s:read err:%d\n", __func__, ret);
  400. ret = 0x30;
  401. }
  402. /*
  403. * temperature is proportional to NTS thermistor resistance
  404. * ADC_RATE[5-4] determines current, 00=20uA,01=40uA,10=60uA,11=80uA
  405. * [12-bit ADC VAL] = R_NTC(Ω) * current / 800
  406. */
  407. adc_val = rntc * (20 + (20 * ((ret >> 4) & 0x3))) / 800;
  408. return adc_val;
  409. }
  410. static int adc_to_temp(struct axp288_fg_info *info, int adc_val)
  411. {
  412. int ret, r, i, tval = 0;
  413. int rmin, rmax, tmin, tmax;
  414. int tcsz = info->pdata->tcsz;
  415. ret = fuel_gauge_reg_readb(info, AXP20X_ADC_RATE);
  416. if (ret < 0) {
  417. dev_err(&info->pdev->dev, "%s:read err:%d\n", __func__, ret);
  418. ret = 0x30;
  419. }
  420. /*
  421. * temperature is proportional to NTS thermistor resistance
  422. * ADC_RATE[5-4] determines current, 00=20uA,01=40uA,10=60uA,11=80uA
  423. * R_NTC(Ω) = [12-bit ADC VAL] * 800 / current
  424. */
  425. r = adc_val * 800 / (20 + (20 * ((ret >> 4) & 0x3)));
  426. if (r < info->pdata->thermistor_curve[0][0]) {
  427. tval = info->pdata->thermistor_curve[0][1];
  428. } else if (r >= info->pdata->thermistor_curve[tcsz-1][0]) {
  429. tval = info->pdata->thermistor_curve[tcsz-1][1];
  430. } else {
  431. for (i = 1; i < tcsz; i++) {
  432. if (r < info->pdata->thermistor_curve[i][0]) {
  433. rmin = info->pdata->thermistor_curve[i-1][0];
  434. rmax = info->pdata->thermistor_curve[i][0];
  435. tmin = info->pdata->thermistor_curve[i-1][1];
  436. tmax = info->pdata->thermistor_curve[i][1];
  437. tval = tmin + ((tmax - tmin) *
  438. (r - rmin) / (rmax - rmin));
  439. break;
  440. }
  441. }
  442. }
  443. return tval;
  444. }
  445. static int fuel_gauge_get_btemp(struct axp288_fg_info *info, int *btemp)
  446. {
  447. int ret, raw_val = 0;
  448. ret = pmic_read_adc_val("axp288-batt-temp", &raw_val, info);
  449. if (ret < 0)
  450. goto temp_read_fail;
  451. *btemp = adc_to_temp(info, raw_val);
  452. temp_read_fail:
  453. return ret;
  454. }
  455. static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv)
  456. {
  457. int ret, value;
  458. /* 12-bit data value, upper 8 in OCVH, lower 4 in OCVL */
  459. ret = fuel_gauge_reg_readb(info, AXP288_FG_OCVH_REG);
  460. if (ret < 0)
  461. goto vocv_read_fail;
  462. value = ret << 4;
  463. ret = fuel_gauge_reg_readb(info, AXP288_FG_OCVL_REG);
  464. if (ret < 0)
  465. goto vocv_read_fail;
  466. value |= (ret & 0xf);
  467. *vocv = VOLTAGE_FROM_ADC(value);
  468. vocv_read_fail:
  469. return ret;
  470. }
  471. static int fuel_gauge_battery_health(struct axp288_fg_info *info)
  472. {
  473. int temp, vocv;
  474. int ret, health = POWER_SUPPLY_HEALTH_UNKNOWN;
  475. ret = fuel_gauge_get_btemp(info, &temp);
  476. if (ret < 0)
  477. goto health_read_fail;
  478. ret = fuel_gauge_get_vocv(info, &vocv);
  479. if (ret < 0)
  480. goto health_read_fail;
  481. if (vocv > info->pdata->max_volt)
  482. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  483. else if (temp > info->pdata->max_temp)
  484. health = POWER_SUPPLY_HEALTH_OVERHEAT;
  485. else if (temp < info->pdata->min_temp)
  486. health = POWER_SUPPLY_HEALTH_COLD;
  487. else if (vocv < info->pdata->min_volt)
  488. health = POWER_SUPPLY_HEALTH_DEAD;
  489. else
  490. health = POWER_SUPPLY_HEALTH_GOOD;
  491. health_read_fail:
  492. return health;
  493. }
  494. static int fuel_gauge_set_high_btemp_alert(struct axp288_fg_info *info)
  495. {
  496. int ret, adc_val;
  497. /* program temperature threshold as 1/16 ADC value */
  498. adc_val = temp_to_adc(info, info->pdata->max_temp);
  499. ret = fuel_gauge_reg_writeb(info, AXP20X_V_HTF_DISCHRG, adc_val >> 4);
  500. return ret;
  501. }
  502. static int fuel_gauge_set_low_btemp_alert(struct axp288_fg_info *info)
  503. {
  504. int ret, adc_val;
  505. /* program temperature threshold as 1/16 ADC value */
  506. adc_val = temp_to_adc(info, info->pdata->min_temp);
  507. ret = fuel_gauge_reg_writeb(info, AXP20X_V_LTF_DISCHRG, adc_val >> 4);
  508. return ret;
  509. }
  510. static int fuel_gauge_get_property(struct power_supply *ps,
  511. enum power_supply_property prop,
  512. union power_supply_propval *val)
  513. {
  514. struct axp288_fg_info *info = power_supply_get_drvdata(ps);
  515. int ret = 0, value;
  516. mutex_lock(&info->lock);
  517. switch (prop) {
  518. case POWER_SUPPLY_PROP_STATUS:
  519. fuel_gauge_get_status(info);
  520. val->intval = info->status;
  521. break;
  522. case POWER_SUPPLY_PROP_HEALTH:
  523. val->intval = fuel_gauge_battery_health(info);
  524. break;
  525. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  526. ret = fuel_gauge_get_vbatt(info, &value);
  527. if (ret < 0)
  528. goto fuel_gauge_read_err;
  529. val->intval = PROP_VOLT(value);
  530. break;
  531. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  532. ret = fuel_gauge_get_vocv(info, &value);
  533. if (ret < 0)
  534. goto fuel_gauge_read_err;
  535. val->intval = PROP_VOLT(value);
  536. break;
  537. case POWER_SUPPLY_PROP_CURRENT_NOW:
  538. ret = fuel_gauge_get_current(info, &value);
  539. if (ret < 0)
  540. goto fuel_gauge_read_err;
  541. val->intval = PROP_CURR(value);
  542. break;
  543. case POWER_SUPPLY_PROP_PRESENT:
  544. ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
  545. if (ret < 0)
  546. goto fuel_gauge_read_err;
  547. if (ret & CHRG_STAT_BAT_PRESENT)
  548. val->intval = 1;
  549. else
  550. val->intval = 0;
  551. break;
  552. case POWER_SUPPLY_PROP_CAPACITY:
  553. ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
  554. if (ret < 0)
  555. goto fuel_gauge_read_err;
  556. if (!(ret & FG_REP_CAP_VALID))
  557. dev_err(&info->pdev->dev,
  558. "capacity measurement not valid\n");
  559. val->intval = (ret & FG_REP_CAP_VAL_MASK);
  560. break;
  561. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  562. ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
  563. if (ret < 0)
  564. goto fuel_gauge_read_err;
  565. val->intval = (ret & 0x0f);
  566. break;
  567. case POWER_SUPPLY_PROP_TEMP:
  568. ret = fuel_gauge_get_btemp(info, &value);
  569. if (ret < 0)
  570. goto fuel_gauge_read_err;
  571. val->intval = PROP_TEMP(value);
  572. break;
  573. case POWER_SUPPLY_PROP_TEMP_MAX:
  574. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  575. val->intval = PROP_TEMP(info->pdata->max_temp);
  576. break;
  577. case POWER_SUPPLY_PROP_TEMP_MIN:
  578. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  579. val->intval = PROP_TEMP(info->pdata->min_temp);
  580. break;
  581. case POWER_SUPPLY_PROP_TECHNOLOGY:
  582. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  583. break;
  584. case POWER_SUPPLY_PROP_CHARGE_NOW:
  585. ret = fuel_gauge_reg_readb(info, AXP288_FG_CC_MTR1_REG);
  586. if (ret < 0)
  587. goto fuel_gauge_read_err;
  588. value = (ret & FG_CC_MTR1_VAL_MASK) << 8;
  589. ret = fuel_gauge_reg_readb(info, AXP288_FG_CC_MTR0_REG);
  590. if (ret < 0)
  591. goto fuel_gauge_read_err;
  592. value |= (ret & FG_CC_MTR0_VAL_MASK);
  593. val->intval = value * FG_DES_CAP_RES_LSB;
  594. break;
  595. case POWER_SUPPLY_PROP_CHARGE_FULL:
  596. ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
  597. if (ret < 0)
  598. goto fuel_gauge_read_err;
  599. value = (ret & FG_DES_CAP1_VAL_MASK) << 8;
  600. ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP0_REG);
  601. if (ret < 0)
  602. goto fuel_gauge_read_err;
  603. value |= (ret & FG_DES_CAP0_VAL_MASK);
  604. val->intval = value * FG_DES_CAP_RES_LSB;
  605. break;
  606. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  607. val->intval = PROP_CURR(info->pdata->design_cap);
  608. break;
  609. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  610. val->intval = PROP_VOLT(info->pdata->max_volt);
  611. break;
  612. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  613. val->intval = PROP_VOLT(info->pdata->min_volt);
  614. break;
  615. case POWER_SUPPLY_PROP_MODEL_NAME:
  616. val->strval = info->pdata->battid;
  617. break;
  618. default:
  619. mutex_unlock(&info->lock);
  620. return -EINVAL;
  621. }
  622. mutex_unlock(&info->lock);
  623. return 0;
  624. fuel_gauge_read_err:
  625. mutex_unlock(&info->lock);
  626. return ret;
  627. }
  628. static int fuel_gauge_set_property(struct power_supply *ps,
  629. enum power_supply_property prop,
  630. const union power_supply_propval *val)
  631. {
  632. struct axp288_fg_info *info = power_supply_get_drvdata(ps);
  633. int ret = 0;
  634. mutex_lock(&info->lock);
  635. switch (prop) {
  636. case POWER_SUPPLY_PROP_STATUS:
  637. info->status = val->intval;
  638. break;
  639. case POWER_SUPPLY_PROP_TEMP_MIN:
  640. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  641. if ((val->intval < PD_DEF_MIN_TEMP) ||
  642. (val->intval > PD_DEF_MAX_TEMP)) {
  643. ret = -EINVAL;
  644. break;
  645. }
  646. info->pdata->min_temp = UNPROP_TEMP(val->intval);
  647. ret = fuel_gauge_set_low_btemp_alert(info);
  648. if (ret < 0)
  649. dev_err(&info->pdev->dev,
  650. "temp alert min set fail:%d\n", ret);
  651. break;
  652. case POWER_SUPPLY_PROP_TEMP_MAX:
  653. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  654. if ((val->intval < PD_DEF_MIN_TEMP) ||
  655. (val->intval > PD_DEF_MAX_TEMP)) {
  656. ret = -EINVAL;
  657. break;
  658. }
  659. info->pdata->max_temp = UNPROP_TEMP(val->intval);
  660. ret = fuel_gauge_set_high_btemp_alert(info);
  661. if (ret < 0)
  662. dev_err(&info->pdev->dev,
  663. "temp alert max set fail:%d\n", ret);
  664. break;
  665. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  666. if ((val->intval < 0) || (val->intval > 15)) {
  667. ret = -EINVAL;
  668. break;
  669. }
  670. ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
  671. if (ret < 0)
  672. break;
  673. ret &= 0xf0;
  674. ret |= (val->intval & 0xf);
  675. ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret);
  676. break;
  677. default:
  678. ret = -EINVAL;
  679. break;
  680. }
  681. mutex_unlock(&info->lock);
  682. return ret;
  683. }
  684. static int fuel_gauge_property_is_writeable(struct power_supply *psy,
  685. enum power_supply_property psp)
  686. {
  687. int ret;
  688. switch (psp) {
  689. case POWER_SUPPLY_PROP_STATUS:
  690. case POWER_SUPPLY_PROP_TEMP_MIN:
  691. case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
  692. case POWER_SUPPLY_PROP_TEMP_MAX:
  693. case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
  694. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  695. ret = 1;
  696. break;
  697. default:
  698. ret = 0;
  699. }
  700. return ret;
  701. }
  702. static void fuel_gauge_status_monitor(struct work_struct *work)
  703. {
  704. struct axp288_fg_info *info = container_of(work,
  705. struct axp288_fg_info, status_monitor.work);
  706. fuel_gauge_get_status(info);
  707. power_supply_changed(info->bat);
  708. schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES);
  709. }
  710. static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
  711. {
  712. struct axp288_fg_info *info = dev;
  713. int i;
  714. for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
  715. if (info->irq[i] == irq)
  716. break;
  717. }
  718. if (i >= AXP288_FG_INTR_NUM) {
  719. dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
  720. return IRQ_NONE;
  721. }
  722. switch (i) {
  723. case QWBTU_IRQ:
  724. dev_info(&info->pdev->dev,
  725. "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
  726. break;
  727. case WBTU_IRQ:
  728. dev_info(&info->pdev->dev,
  729. "Battery under temperature in work mode IRQ (WBTU)\n");
  730. break;
  731. case QWBTO_IRQ:
  732. dev_info(&info->pdev->dev,
  733. "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
  734. break;
  735. case WBTO_IRQ:
  736. dev_info(&info->pdev->dev,
  737. "Battery over temperature in work mode IRQ (WBTO)\n");
  738. break;
  739. case WL2_IRQ:
  740. dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n");
  741. break;
  742. case WL1_IRQ:
  743. dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n");
  744. break;
  745. default:
  746. dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
  747. }
  748. power_supply_changed(info->bat);
  749. return IRQ_HANDLED;
  750. }
  751. static void fuel_gauge_external_power_changed(struct power_supply *psy)
  752. {
  753. struct axp288_fg_info *info = power_supply_get_drvdata(psy);
  754. power_supply_changed(info->bat);
  755. }
  756. static const struct power_supply_desc fuel_gauge_desc = {
  757. .name = DEV_NAME,
  758. .type = POWER_SUPPLY_TYPE_BATTERY,
  759. .properties = fuel_gauge_props,
  760. .num_properties = ARRAY_SIZE(fuel_gauge_props),
  761. .get_property = fuel_gauge_get_property,
  762. .set_property = fuel_gauge_set_property,
  763. .property_is_writeable = fuel_gauge_property_is_writeable,
  764. .external_power_changed = fuel_gauge_external_power_changed,
  765. };
  766. static int fuel_gauge_set_lowbatt_thresholds(struct axp288_fg_info *info)
  767. {
  768. int ret;
  769. u8 reg_val;
  770. ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
  771. if (ret < 0) {
  772. dev_err(&info->pdev->dev, "%s:read err:%d\n", __func__, ret);
  773. return ret;
  774. }
  775. ret = (ret & FG_REP_CAP_VAL_MASK);
  776. if (ret > FG_LOW_CAP_WARN_THR)
  777. reg_val = FG_LOW_CAP_WARN_THR;
  778. else if (ret > FG_LOW_CAP_CRIT_THR)
  779. reg_val = FG_LOW_CAP_CRIT_THR;
  780. else
  781. reg_val = FG_LOW_CAP_SHDN_THR;
  782. reg_val |= FG_LOW_CAP_THR1_VAL;
  783. ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, reg_val);
  784. if (ret < 0)
  785. dev_err(&info->pdev->dev, "%s:write err:%d\n", __func__, ret);
  786. return ret;
  787. }
  788. static int fuel_gauge_program_vbatt_full(struct axp288_fg_info *info)
  789. {
  790. int ret;
  791. u8 val;
  792. ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
  793. if (ret < 0)
  794. goto fg_prog_ocv_fail;
  795. else
  796. val = (ret & ~CHRG_CCCV_CV_MASK);
  797. switch (info->pdata->max_volt) {
  798. case CV_4100:
  799. val |= (CHRG_CCCV_CV_4100MV << CHRG_CCCV_CV_BIT_POS);
  800. break;
  801. case CV_4150:
  802. val |= (CHRG_CCCV_CV_4150MV << CHRG_CCCV_CV_BIT_POS);
  803. break;
  804. case CV_4200:
  805. val |= (CHRG_CCCV_CV_4200MV << CHRG_CCCV_CV_BIT_POS);
  806. break;
  807. case CV_4350:
  808. val |= (CHRG_CCCV_CV_4350MV << CHRG_CCCV_CV_BIT_POS);
  809. break;
  810. default:
  811. val |= (CHRG_CCCV_CV_4200MV << CHRG_CCCV_CV_BIT_POS);
  812. break;
  813. }
  814. ret = fuel_gauge_reg_writeb(info, AXP20X_CHRG_CTRL1, val);
  815. fg_prog_ocv_fail:
  816. return ret;
  817. }
  818. static int fuel_gauge_program_design_cap(struct axp288_fg_info *info)
  819. {
  820. int ret;
  821. ret = fuel_gauge_reg_writeb(info,
  822. AXP288_FG_DES_CAP1_REG, info->pdata->cap1);
  823. if (ret < 0)
  824. goto fg_prog_descap_fail;
  825. ret = fuel_gauge_reg_writeb(info,
  826. AXP288_FG_DES_CAP0_REG, info->pdata->cap0);
  827. fg_prog_descap_fail:
  828. return ret;
  829. }
  830. static int fuel_gauge_program_ocv_curve(struct axp288_fg_info *info)
  831. {
  832. int ret = 0, i;
  833. for (i = 0; i < OCV_CURVE_SIZE; i++) {
  834. ret = fuel_gauge_reg_writeb(info,
  835. AXP288_FG_OCV_CURVE_REG + i, info->pdata->ocv_curve[i]);
  836. if (ret < 0)
  837. goto fg_prog_ocv_fail;
  838. }
  839. fg_prog_ocv_fail:
  840. return ret;
  841. }
  842. static int fuel_gauge_program_rdc_vals(struct axp288_fg_info *info)
  843. {
  844. int ret;
  845. ret = fuel_gauge_reg_writeb(info,
  846. AXP288_FG_RDC1_REG, info->pdata->rdc1);
  847. if (ret < 0)
  848. goto fg_prog_ocv_fail;
  849. ret = fuel_gauge_reg_writeb(info,
  850. AXP288_FG_RDC0_REG, info->pdata->rdc0);
  851. fg_prog_ocv_fail:
  852. return ret;
  853. }
  854. static void fuel_gauge_init_config_regs(struct axp288_fg_info *info)
  855. {
  856. int ret;
  857. /*
  858. * check if the config data is already
  859. * programmed and if so just return.
  860. */
  861. ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
  862. if (ret < 0) {
  863. dev_warn(&info->pdev->dev, "CAP1 reg read err!!\n");
  864. } else if (!(ret & FG_DES_CAP1_VALID)) {
  865. dev_info(&info->pdev->dev, "FG data needs to be initialized\n");
  866. } else {
  867. dev_info(&info->pdev->dev, "FG data is already initialized\n");
  868. return;
  869. }
  870. ret = fuel_gauge_program_vbatt_full(info);
  871. if (ret < 0)
  872. dev_err(&info->pdev->dev, "set vbatt full fail:%d\n", ret);
  873. ret = fuel_gauge_program_design_cap(info);
  874. if (ret < 0)
  875. dev_err(&info->pdev->dev, "set design cap fail:%d\n", ret);
  876. ret = fuel_gauge_program_rdc_vals(info);
  877. if (ret < 0)
  878. dev_err(&info->pdev->dev, "set rdc fail:%d\n", ret);
  879. ret = fuel_gauge_program_ocv_curve(info);
  880. if (ret < 0)
  881. dev_err(&info->pdev->dev, "set ocv curve fail:%d\n", ret);
  882. ret = fuel_gauge_set_lowbatt_thresholds(info);
  883. if (ret < 0)
  884. dev_err(&info->pdev->dev, "lowbatt thr set fail:%d\n", ret);
  885. ret = fuel_gauge_reg_writeb(info, AXP20X_CC_CTRL, 0xef);
  886. if (ret < 0)
  887. dev_err(&info->pdev->dev, "gauge cntl set fail:%d\n", ret);
  888. }
  889. static void fuel_gauge_init_irq(struct axp288_fg_info *info)
  890. {
  891. int ret, i, pirq;
  892. for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
  893. pirq = platform_get_irq(info->pdev, i);
  894. info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
  895. if (info->irq[i] < 0) {
  896. dev_warn(&info->pdev->dev,
  897. "regmap_irq get virq failed for IRQ %d: %d\n",
  898. pirq, info->irq[i]);
  899. info->irq[i] = -1;
  900. goto intr_failed;
  901. }
  902. ret = request_threaded_irq(info->irq[i],
  903. NULL, fuel_gauge_thread_handler,
  904. IRQF_ONESHOT, DEV_NAME, info);
  905. if (ret) {
  906. dev_warn(&info->pdev->dev,
  907. "request irq failed for IRQ %d: %d\n",
  908. pirq, info->irq[i]);
  909. info->irq[i] = -1;
  910. goto intr_failed;
  911. } else {
  912. dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n",
  913. pirq, info->irq[i]);
  914. }
  915. }
  916. return;
  917. intr_failed:
  918. for (; i > 0; i--) {
  919. free_irq(info->irq[i - 1], info);
  920. info->irq[i - 1] = -1;
  921. }
  922. }
  923. static void fuel_gauge_init_hw_regs(struct axp288_fg_info *info)
  924. {
  925. int ret;
  926. unsigned int val;
  927. ret = fuel_gauge_set_high_btemp_alert(info);
  928. if (ret < 0)
  929. dev_err(&info->pdev->dev, "high batt temp set fail:%d\n", ret);
  930. ret = fuel_gauge_set_low_btemp_alert(info);
  931. if (ret < 0)
  932. dev_err(&info->pdev->dev, "low batt temp set fail:%d\n", ret);
  933. /* enable interrupts */
  934. val = fuel_gauge_reg_readb(info, AXP20X_IRQ3_EN);
  935. val |= TEMP_IRQ_CFG_MASK;
  936. fuel_gauge_reg_writeb(info, AXP20X_IRQ3_EN, val);
  937. val = fuel_gauge_reg_readb(info, AXP20X_IRQ4_EN);
  938. val |= FG_IRQ_CFG_LOWBATT_MASK;
  939. val = fuel_gauge_reg_writeb(info, AXP20X_IRQ4_EN, val);
  940. }
  941. static int axp288_fuel_gauge_probe(struct platform_device *pdev)
  942. {
  943. int ret = 0;
  944. struct axp288_fg_info *info;
  945. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  946. struct power_supply_config psy_cfg = {};
  947. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  948. if (!info)
  949. return -ENOMEM;
  950. info->pdev = pdev;
  951. info->regmap = axp20x->regmap;
  952. info->regmap_irqc = axp20x->regmap_irqc;
  953. info->status = POWER_SUPPLY_STATUS_UNKNOWN;
  954. info->pdata = pdev->dev.platform_data;
  955. if (!info->pdata)
  956. return -ENODEV;
  957. platform_set_drvdata(pdev, info);
  958. mutex_init(&info->lock);
  959. INIT_DELAYED_WORK(&info->status_monitor, fuel_gauge_status_monitor);
  960. psy_cfg.drv_data = info;
  961. info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg);
  962. if (IS_ERR(info->bat)) {
  963. ret = PTR_ERR(info->bat);
  964. dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
  965. return ret;
  966. }
  967. fuel_gauge_create_debugfs(info);
  968. fuel_gauge_init_config_regs(info);
  969. fuel_gauge_init_irq(info);
  970. fuel_gauge_init_hw_regs(info);
  971. schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES);
  972. return ret;
  973. }
  974. static const struct platform_device_id axp288_fg_id_table[] = {
  975. { .name = DEV_NAME },
  976. {},
  977. };
  978. static int axp288_fuel_gauge_remove(struct platform_device *pdev)
  979. {
  980. struct axp288_fg_info *info = platform_get_drvdata(pdev);
  981. int i;
  982. cancel_delayed_work_sync(&info->status_monitor);
  983. power_supply_unregister(info->bat);
  984. fuel_gauge_remove_debugfs(info);
  985. for (i = 0; i < AXP288_FG_INTR_NUM; i++)
  986. if (info->irq[i] >= 0)
  987. free_irq(info->irq[i], info);
  988. return 0;
  989. }
  990. static struct platform_driver axp288_fuel_gauge_driver = {
  991. .probe = axp288_fuel_gauge_probe,
  992. .remove = axp288_fuel_gauge_remove,
  993. .id_table = axp288_fg_id_table,
  994. .driver = {
  995. .name = DEV_NAME,
  996. },
  997. };
  998. module_platform_driver(axp288_fuel_gauge_driver);
  999. MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
  1000. MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
  1001. MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
  1002. MODULE_LICENSE("GPL");