axp288_fuel_gauge.c 30 KB

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