axp288_fuel_gauge.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 FG_CNTL_OCV_ADJ_STAT (1 << 2)
  50. #define FG_CNTL_OCV_ADJ_EN (1 << 3)
  51. #define FG_CNTL_CAP_ADJ_STAT (1 << 4)
  52. #define FG_CNTL_CAP_ADJ_EN (1 << 5)
  53. #define FG_CNTL_CC_EN (1 << 6)
  54. #define FG_CNTL_GAUGE_EN (1 << 7)
  55. #define FG_15BIT_WORD_VALID (1 << 15)
  56. #define FG_15BIT_VAL_MASK 0x7fff
  57. #define FG_REP_CAP_VALID (1 << 7)
  58. #define FG_REP_CAP_VAL_MASK 0x7F
  59. #define FG_DES_CAP1_VALID (1 << 7)
  60. #define FG_DES_CAP_RES_LSB 1456 /* 1.456mAhr */
  61. #define FG_DES_CC_RES_LSB 1456 /* 1.456mAhr */
  62. #define FG_OCV_CAP_VALID (1 << 7)
  63. #define FG_OCV_CAP_VAL_MASK 0x7F
  64. #define FG_CC_CAP_VALID (1 << 7)
  65. #define FG_CC_CAP_VAL_MASK 0x7F
  66. #define FG_LOW_CAP_THR1_MASK 0xf0 /* 5% tp 20% */
  67. #define FG_LOW_CAP_THR1_VAL 0xa0 /* 15 perc */
  68. #define FG_LOW_CAP_THR2_MASK 0x0f /* 0% to 15% */
  69. #define FG_LOW_CAP_WARN_THR 14 /* 14 perc */
  70. #define FG_LOW_CAP_CRIT_THR 4 /* 4 perc */
  71. #define FG_LOW_CAP_SHDN_THR 0 /* 0 perc */
  72. #define STATUS_MON_DELAY_JIFFIES (HZ * 60) /*60 sec */
  73. #define NR_RETRY_CNT 3
  74. #define DEV_NAME "axp288_fuel_gauge"
  75. /* 1.1mV per LSB expressed in uV */
  76. #define VOLTAGE_FROM_ADC(a) ((a * 11) / 10)
  77. /* properties converted to uV, uA */
  78. #define PROP_VOLT(a) ((a) * 1000)
  79. #define PROP_CURR(a) ((a) * 1000)
  80. #define AXP288_FG_INTR_NUM 6
  81. enum {
  82. QWBTU_IRQ = 0,
  83. WBTU_IRQ,
  84. QWBTO_IRQ,
  85. WBTO_IRQ,
  86. WL2_IRQ,
  87. WL1_IRQ,
  88. };
  89. struct axp288_fg_info {
  90. struct platform_device *pdev;
  91. struct regmap *regmap;
  92. struct regmap_irq_chip_data *regmap_irqc;
  93. int irq[AXP288_FG_INTR_NUM];
  94. struct power_supply *bat;
  95. struct mutex lock;
  96. int status;
  97. int max_volt;
  98. struct delayed_work status_monitor;
  99. struct dentry *debug_file;
  100. };
  101. static enum power_supply_property fuel_gauge_props[] = {
  102. POWER_SUPPLY_PROP_STATUS,
  103. POWER_SUPPLY_PROP_PRESENT,
  104. POWER_SUPPLY_PROP_HEALTH,
  105. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  106. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  107. POWER_SUPPLY_PROP_VOLTAGE_OCV,
  108. POWER_SUPPLY_PROP_CURRENT_NOW,
  109. POWER_SUPPLY_PROP_CAPACITY,
  110. POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN,
  111. POWER_SUPPLY_PROP_TECHNOLOGY,
  112. POWER_SUPPLY_PROP_CHARGE_FULL,
  113. POWER_SUPPLY_PROP_CHARGE_NOW,
  114. };
  115. static int fuel_gauge_reg_readb(struct axp288_fg_info *info, int reg)
  116. {
  117. int ret, i;
  118. unsigned int val;
  119. for (i = 0; i < NR_RETRY_CNT; i++) {
  120. ret = regmap_read(info->regmap, reg, &val);
  121. if (ret == -EBUSY)
  122. continue;
  123. else
  124. break;
  125. }
  126. if (ret < 0) {
  127. dev_err(&info->pdev->dev, "axp288 reg read err:%d\n", ret);
  128. return ret;
  129. }
  130. return val;
  131. }
  132. static int fuel_gauge_reg_writeb(struct axp288_fg_info *info, int reg, u8 val)
  133. {
  134. int ret;
  135. ret = regmap_write(info->regmap, reg, (unsigned int)val);
  136. if (ret < 0)
  137. dev_err(&info->pdev->dev, "axp288 reg write err:%d\n", ret);
  138. return ret;
  139. }
  140. static int fuel_gauge_read_15bit_word(struct axp288_fg_info *info, int reg)
  141. {
  142. unsigned char buf[2];
  143. int ret;
  144. ret = regmap_bulk_read(info->regmap, reg, buf, 2);
  145. if (ret < 0) {
  146. dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n",
  147. reg, ret);
  148. return ret;
  149. }
  150. ret = get_unaligned_be16(buf);
  151. if (!(ret & FG_15BIT_WORD_VALID)) {
  152. dev_err(&info->pdev->dev, "Error reg 0x%02x contents not valid\n",
  153. reg);
  154. return -ENXIO;
  155. }
  156. return ret & FG_15BIT_VAL_MASK;
  157. }
  158. static int fuel_gauge_read_12bit_word(struct axp288_fg_info *info, int reg)
  159. {
  160. unsigned char buf[2];
  161. int ret;
  162. ret = regmap_bulk_read(info->regmap, reg, buf, 2);
  163. if (ret < 0) {
  164. dev_err(&info->pdev->dev, "Error reading reg 0x%02x err: %d\n",
  165. reg, ret);
  166. return ret;
  167. }
  168. /* 12-bit data values have upper 8 bits in buf[0], lower 4 in buf[1] */
  169. return (buf[0] << 4) | ((buf[1] >> 4) & 0x0f);
  170. }
  171. static int pmic_read_adc_val(const char *name, int *raw_val,
  172. struct axp288_fg_info *info)
  173. {
  174. int ret, val = 0;
  175. struct iio_channel *indio_chan;
  176. indio_chan = iio_channel_get(NULL, name);
  177. if (IS_ERR_OR_NULL(indio_chan)) {
  178. ret = PTR_ERR(indio_chan);
  179. goto exit;
  180. }
  181. ret = iio_read_channel_raw(indio_chan, &val);
  182. if (ret < 0) {
  183. dev_err(&info->pdev->dev,
  184. "IIO channel read error: %x, %x\n", ret, val);
  185. goto err_exit;
  186. }
  187. dev_dbg(&info->pdev->dev, "adc raw val=%x\n", val);
  188. *raw_val = val;
  189. err_exit:
  190. iio_channel_release(indio_chan);
  191. exit:
  192. return ret;
  193. }
  194. #ifdef CONFIG_DEBUG_FS
  195. static int fuel_gauge_debug_show(struct seq_file *s, void *data)
  196. {
  197. struct axp288_fg_info *info = s->private;
  198. int raw_val, ret;
  199. seq_printf(s, " PWR_STATUS[%02x] : %02x\n",
  200. AXP20X_PWR_INPUT_STATUS,
  201. fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS));
  202. seq_printf(s, "PWR_OP_MODE[%02x] : %02x\n",
  203. AXP20X_PWR_OP_MODE,
  204. fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE));
  205. seq_printf(s, " CHRG_CTRL1[%02x] : %02x\n",
  206. AXP20X_CHRG_CTRL1,
  207. fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1));
  208. seq_printf(s, " VLTF[%02x] : %02x\n",
  209. AXP20X_V_LTF_DISCHRG,
  210. fuel_gauge_reg_readb(info, AXP20X_V_LTF_DISCHRG));
  211. seq_printf(s, " VHTF[%02x] : %02x\n",
  212. AXP20X_V_HTF_DISCHRG,
  213. fuel_gauge_reg_readb(info, AXP20X_V_HTF_DISCHRG));
  214. seq_printf(s, " CC_CTRL[%02x] : %02x\n",
  215. AXP20X_CC_CTRL,
  216. fuel_gauge_reg_readb(info, AXP20X_CC_CTRL));
  217. seq_printf(s, "BATTERY CAP[%02x] : %02x\n",
  218. AXP20X_FG_RES,
  219. fuel_gauge_reg_readb(info, AXP20X_FG_RES));
  220. seq_printf(s, " FG_RDC1[%02x] : %02x\n",
  221. AXP288_FG_RDC1_REG,
  222. fuel_gauge_reg_readb(info, AXP288_FG_RDC1_REG));
  223. seq_printf(s, " FG_RDC0[%02x] : %02x\n",
  224. AXP288_FG_RDC0_REG,
  225. fuel_gauge_reg_readb(info, AXP288_FG_RDC0_REG));
  226. seq_printf(s, " FG_OCV[%02x] : %04x\n",
  227. AXP288_FG_OCVH_REG,
  228. fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG));
  229. seq_printf(s, " FG_DES_CAP[%02x] : %04x\n",
  230. AXP288_FG_DES_CAP1_REG,
  231. fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG));
  232. seq_printf(s, " FG_CC_MTR[%02x] : %04x\n",
  233. AXP288_FG_CC_MTR1_REG,
  234. fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG));
  235. seq_printf(s, " FG_OCV_CAP[%02x] : %02x\n",
  236. AXP288_FG_OCV_CAP_REG,
  237. fuel_gauge_reg_readb(info, AXP288_FG_OCV_CAP_REG));
  238. seq_printf(s, " FG_CC_CAP[%02x] : %02x\n",
  239. AXP288_FG_CC_CAP_REG,
  240. fuel_gauge_reg_readb(info, AXP288_FG_CC_CAP_REG));
  241. seq_printf(s, " FG_LOW_CAP[%02x] : %02x\n",
  242. AXP288_FG_LOW_CAP_REG,
  243. fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG));
  244. seq_printf(s, "TUNING_CTL0[%02x] : %02x\n",
  245. AXP288_FG_TUNE0,
  246. fuel_gauge_reg_readb(info, AXP288_FG_TUNE0));
  247. seq_printf(s, "TUNING_CTL1[%02x] : %02x\n",
  248. AXP288_FG_TUNE1,
  249. fuel_gauge_reg_readb(info, AXP288_FG_TUNE1));
  250. seq_printf(s, "TUNING_CTL2[%02x] : %02x\n",
  251. AXP288_FG_TUNE2,
  252. fuel_gauge_reg_readb(info, AXP288_FG_TUNE2));
  253. seq_printf(s, "TUNING_CTL3[%02x] : %02x\n",
  254. AXP288_FG_TUNE3,
  255. fuel_gauge_reg_readb(info, AXP288_FG_TUNE3));
  256. seq_printf(s, "TUNING_CTL4[%02x] : %02x\n",
  257. AXP288_FG_TUNE4,
  258. fuel_gauge_reg_readb(info, AXP288_FG_TUNE4));
  259. seq_printf(s, "TUNING_CTL5[%02x] : %02x\n",
  260. AXP288_FG_TUNE5,
  261. fuel_gauge_reg_readb(info, AXP288_FG_TUNE5));
  262. ret = pmic_read_adc_val("axp288-batt-temp", &raw_val, info);
  263. if (ret >= 0)
  264. seq_printf(s, "axp288-batttemp : %d\n", raw_val);
  265. ret = pmic_read_adc_val("axp288-pmic-temp", &raw_val, info);
  266. if (ret >= 0)
  267. seq_printf(s, "axp288-pmictemp : %d\n", raw_val);
  268. ret = pmic_read_adc_val("axp288-system-temp", &raw_val, info);
  269. if (ret >= 0)
  270. seq_printf(s, "axp288-systtemp : %d\n", raw_val);
  271. ret = pmic_read_adc_val("axp288-chrg-curr", &raw_val, info);
  272. if (ret >= 0)
  273. seq_printf(s, "axp288-chrgcurr : %d\n", raw_val);
  274. ret = pmic_read_adc_val("axp288-chrg-d-curr", &raw_val, info);
  275. if (ret >= 0)
  276. seq_printf(s, "axp288-dchrgcur : %d\n", raw_val);
  277. ret = pmic_read_adc_val("axp288-batt-volt", &raw_val, info);
  278. if (ret >= 0)
  279. seq_printf(s, "axp288-battvolt : %d\n", raw_val);
  280. return 0;
  281. }
  282. static int debug_open(struct inode *inode, struct file *file)
  283. {
  284. return single_open(file, fuel_gauge_debug_show, inode->i_private);
  285. }
  286. static const struct file_operations fg_debug_fops = {
  287. .open = debug_open,
  288. .read = seq_read,
  289. .llseek = seq_lseek,
  290. .release = single_release,
  291. };
  292. static void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
  293. {
  294. info->debug_file = debugfs_create_file("fuelgauge", 0666, NULL,
  295. info, &fg_debug_fops);
  296. }
  297. static void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
  298. {
  299. debugfs_remove(info->debug_file);
  300. }
  301. #else
  302. static inline void fuel_gauge_create_debugfs(struct axp288_fg_info *info)
  303. {
  304. }
  305. static inline void fuel_gauge_remove_debugfs(struct axp288_fg_info *info)
  306. {
  307. }
  308. #endif
  309. static void fuel_gauge_get_status(struct axp288_fg_info *info)
  310. {
  311. int pwr_stat, ret;
  312. int charge, discharge;
  313. pwr_stat = fuel_gauge_reg_readb(info, AXP20X_PWR_INPUT_STATUS);
  314. if (pwr_stat < 0) {
  315. dev_err(&info->pdev->dev,
  316. "PWR STAT read failed:%d\n", pwr_stat);
  317. return;
  318. }
  319. ret = pmic_read_adc_val("axp288-chrg-curr", &charge, info);
  320. if (ret < 0) {
  321. dev_err(&info->pdev->dev,
  322. "ADC charge current read failed:%d\n", ret);
  323. return;
  324. }
  325. ret = pmic_read_adc_val("axp288-chrg-d-curr", &discharge, info);
  326. if (ret < 0) {
  327. dev_err(&info->pdev->dev,
  328. "ADC discharge current read failed:%d\n", ret);
  329. return;
  330. }
  331. if (charge > 0)
  332. info->status = POWER_SUPPLY_STATUS_CHARGING;
  333. else if (discharge > 0)
  334. info->status = POWER_SUPPLY_STATUS_DISCHARGING;
  335. else {
  336. if (pwr_stat & CHRG_STAT_BAT_PRESENT)
  337. info->status = POWER_SUPPLY_STATUS_FULL;
  338. else
  339. info->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  340. }
  341. }
  342. static int fuel_gauge_get_vbatt(struct axp288_fg_info *info, int *vbatt)
  343. {
  344. int ret = 0, raw_val;
  345. ret = pmic_read_adc_val("axp288-batt-volt", &raw_val, info);
  346. if (ret < 0)
  347. goto vbatt_read_fail;
  348. *vbatt = VOLTAGE_FROM_ADC(raw_val);
  349. vbatt_read_fail:
  350. return ret;
  351. }
  352. static int fuel_gauge_get_current(struct axp288_fg_info *info, int *cur)
  353. {
  354. int ret, value = 0;
  355. int charge, discharge;
  356. ret = pmic_read_adc_val("axp288-chrg-curr", &charge, info);
  357. if (ret < 0)
  358. goto current_read_fail;
  359. ret = pmic_read_adc_val("axp288-chrg-d-curr", &discharge, info);
  360. if (ret < 0)
  361. goto current_read_fail;
  362. if (charge > 0)
  363. value = charge;
  364. else if (discharge > 0)
  365. value = -1 * discharge;
  366. *cur = value;
  367. current_read_fail:
  368. return ret;
  369. }
  370. static int fuel_gauge_get_vocv(struct axp288_fg_info *info, int *vocv)
  371. {
  372. int ret;
  373. ret = fuel_gauge_read_12bit_word(info, AXP288_FG_OCVH_REG);
  374. if (ret >= 0)
  375. *vocv = VOLTAGE_FROM_ADC(ret);
  376. return ret;
  377. }
  378. static int fuel_gauge_battery_health(struct axp288_fg_info *info)
  379. {
  380. int ret, vocv, health = POWER_SUPPLY_HEALTH_UNKNOWN;
  381. ret = fuel_gauge_get_vocv(info, &vocv);
  382. if (ret < 0)
  383. goto health_read_fail;
  384. if (vocv > info->max_volt)
  385. health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  386. else
  387. health = POWER_SUPPLY_HEALTH_GOOD;
  388. health_read_fail:
  389. return health;
  390. }
  391. static int fuel_gauge_get_property(struct power_supply *ps,
  392. enum power_supply_property prop,
  393. union power_supply_propval *val)
  394. {
  395. struct axp288_fg_info *info = power_supply_get_drvdata(ps);
  396. int ret = 0, value;
  397. mutex_lock(&info->lock);
  398. switch (prop) {
  399. case POWER_SUPPLY_PROP_STATUS:
  400. fuel_gauge_get_status(info);
  401. val->intval = info->status;
  402. break;
  403. case POWER_SUPPLY_PROP_HEALTH:
  404. val->intval = fuel_gauge_battery_health(info);
  405. break;
  406. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  407. ret = fuel_gauge_get_vbatt(info, &value);
  408. if (ret < 0)
  409. goto fuel_gauge_read_err;
  410. val->intval = PROP_VOLT(value);
  411. break;
  412. case POWER_SUPPLY_PROP_VOLTAGE_OCV:
  413. ret = fuel_gauge_get_vocv(info, &value);
  414. if (ret < 0)
  415. goto fuel_gauge_read_err;
  416. val->intval = PROP_VOLT(value);
  417. break;
  418. case POWER_SUPPLY_PROP_CURRENT_NOW:
  419. ret = fuel_gauge_get_current(info, &value);
  420. if (ret < 0)
  421. goto fuel_gauge_read_err;
  422. val->intval = PROP_CURR(value);
  423. break;
  424. case POWER_SUPPLY_PROP_PRESENT:
  425. ret = fuel_gauge_reg_readb(info, AXP20X_PWR_OP_MODE);
  426. if (ret < 0)
  427. goto fuel_gauge_read_err;
  428. if (ret & CHRG_STAT_BAT_PRESENT)
  429. val->intval = 1;
  430. else
  431. val->intval = 0;
  432. break;
  433. case POWER_SUPPLY_PROP_CAPACITY:
  434. ret = fuel_gauge_reg_readb(info, AXP20X_FG_RES);
  435. if (ret < 0)
  436. goto fuel_gauge_read_err;
  437. if (!(ret & FG_REP_CAP_VALID))
  438. dev_err(&info->pdev->dev,
  439. "capacity measurement not valid\n");
  440. val->intval = (ret & FG_REP_CAP_VAL_MASK);
  441. break;
  442. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  443. ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
  444. if (ret < 0)
  445. goto fuel_gauge_read_err;
  446. val->intval = (ret & 0x0f);
  447. break;
  448. case POWER_SUPPLY_PROP_TECHNOLOGY:
  449. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  450. break;
  451. case POWER_SUPPLY_PROP_CHARGE_NOW:
  452. ret = fuel_gauge_read_15bit_word(info, AXP288_FG_CC_MTR1_REG);
  453. if (ret < 0)
  454. goto fuel_gauge_read_err;
  455. val->intval = ret * FG_DES_CAP_RES_LSB;
  456. break;
  457. case POWER_SUPPLY_PROP_CHARGE_FULL:
  458. ret = fuel_gauge_read_15bit_word(info, AXP288_FG_DES_CAP1_REG);
  459. if (ret < 0)
  460. goto fuel_gauge_read_err;
  461. val->intval = ret * FG_DES_CAP_RES_LSB;
  462. break;
  463. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  464. val->intval = PROP_VOLT(info->max_volt);
  465. break;
  466. default:
  467. mutex_unlock(&info->lock);
  468. return -EINVAL;
  469. }
  470. mutex_unlock(&info->lock);
  471. return 0;
  472. fuel_gauge_read_err:
  473. mutex_unlock(&info->lock);
  474. return ret;
  475. }
  476. static int fuel_gauge_set_property(struct power_supply *ps,
  477. enum power_supply_property prop,
  478. const union power_supply_propval *val)
  479. {
  480. struct axp288_fg_info *info = power_supply_get_drvdata(ps);
  481. int ret = 0;
  482. mutex_lock(&info->lock);
  483. switch (prop) {
  484. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  485. if ((val->intval < 0) || (val->intval > 15)) {
  486. ret = -EINVAL;
  487. break;
  488. }
  489. ret = fuel_gauge_reg_readb(info, AXP288_FG_LOW_CAP_REG);
  490. if (ret < 0)
  491. break;
  492. ret &= 0xf0;
  493. ret |= (val->intval & 0xf);
  494. ret = fuel_gauge_reg_writeb(info, AXP288_FG_LOW_CAP_REG, ret);
  495. break;
  496. default:
  497. ret = -EINVAL;
  498. break;
  499. }
  500. mutex_unlock(&info->lock);
  501. return ret;
  502. }
  503. static int fuel_gauge_property_is_writeable(struct power_supply *psy,
  504. enum power_supply_property psp)
  505. {
  506. int ret;
  507. switch (psp) {
  508. case POWER_SUPPLY_PROP_CAPACITY_ALERT_MIN:
  509. ret = 1;
  510. break;
  511. default:
  512. ret = 0;
  513. }
  514. return ret;
  515. }
  516. static void fuel_gauge_status_monitor(struct work_struct *work)
  517. {
  518. struct axp288_fg_info *info = container_of(work,
  519. struct axp288_fg_info, status_monitor.work);
  520. fuel_gauge_get_status(info);
  521. power_supply_changed(info->bat);
  522. schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES);
  523. }
  524. static irqreturn_t fuel_gauge_thread_handler(int irq, void *dev)
  525. {
  526. struct axp288_fg_info *info = dev;
  527. int i;
  528. for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
  529. if (info->irq[i] == irq)
  530. break;
  531. }
  532. if (i >= AXP288_FG_INTR_NUM) {
  533. dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
  534. return IRQ_NONE;
  535. }
  536. switch (i) {
  537. case QWBTU_IRQ:
  538. dev_info(&info->pdev->dev,
  539. "Quit Battery under temperature in work mode IRQ (QWBTU)\n");
  540. break;
  541. case WBTU_IRQ:
  542. dev_info(&info->pdev->dev,
  543. "Battery under temperature in work mode IRQ (WBTU)\n");
  544. break;
  545. case QWBTO_IRQ:
  546. dev_info(&info->pdev->dev,
  547. "Quit Battery over temperature in work mode IRQ (QWBTO)\n");
  548. break;
  549. case WBTO_IRQ:
  550. dev_info(&info->pdev->dev,
  551. "Battery over temperature in work mode IRQ (WBTO)\n");
  552. break;
  553. case WL2_IRQ:
  554. dev_info(&info->pdev->dev, "Low Batt Warning(2) INTR\n");
  555. break;
  556. case WL1_IRQ:
  557. dev_info(&info->pdev->dev, "Low Batt Warning(1) INTR\n");
  558. break;
  559. default:
  560. dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
  561. }
  562. power_supply_changed(info->bat);
  563. return IRQ_HANDLED;
  564. }
  565. static void fuel_gauge_external_power_changed(struct power_supply *psy)
  566. {
  567. struct axp288_fg_info *info = power_supply_get_drvdata(psy);
  568. power_supply_changed(info->bat);
  569. }
  570. static const struct power_supply_desc fuel_gauge_desc = {
  571. .name = DEV_NAME,
  572. .type = POWER_SUPPLY_TYPE_BATTERY,
  573. .properties = fuel_gauge_props,
  574. .num_properties = ARRAY_SIZE(fuel_gauge_props),
  575. .get_property = fuel_gauge_get_property,
  576. .set_property = fuel_gauge_set_property,
  577. .property_is_writeable = fuel_gauge_property_is_writeable,
  578. .external_power_changed = fuel_gauge_external_power_changed,
  579. };
  580. static void fuel_gauge_init_irq(struct axp288_fg_info *info)
  581. {
  582. int ret, i, pirq;
  583. for (i = 0; i < AXP288_FG_INTR_NUM; i++) {
  584. pirq = platform_get_irq(info->pdev, i);
  585. info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
  586. if (info->irq[i] < 0) {
  587. dev_warn(&info->pdev->dev,
  588. "regmap_irq get virq failed for IRQ %d: %d\n",
  589. pirq, info->irq[i]);
  590. info->irq[i] = -1;
  591. goto intr_failed;
  592. }
  593. ret = request_threaded_irq(info->irq[i],
  594. NULL, fuel_gauge_thread_handler,
  595. IRQF_ONESHOT, DEV_NAME, info);
  596. if (ret) {
  597. dev_warn(&info->pdev->dev,
  598. "request irq failed for IRQ %d: %d\n",
  599. pirq, info->irq[i]);
  600. info->irq[i] = -1;
  601. goto intr_failed;
  602. } else {
  603. dev_info(&info->pdev->dev, "HW IRQ %d -> VIRQ %d\n",
  604. pirq, info->irq[i]);
  605. }
  606. }
  607. return;
  608. intr_failed:
  609. for (; i > 0; i--) {
  610. free_irq(info->irq[i - 1], info);
  611. info->irq[i - 1] = -1;
  612. }
  613. }
  614. static int axp288_fuel_gauge_probe(struct platform_device *pdev)
  615. {
  616. int ret = 0;
  617. struct axp288_fg_info *info;
  618. struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
  619. struct power_supply_config psy_cfg = {};
  620. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  621. if (!info)
  622. return -ENOMEM;
  623. info->pdev = pdev;
  624. info->regmap = axp20x->regmap;
  625. info->regmap_irqc = axp20x->regmap_irqc;
  626. info->status = POWER_SUPPLY_STATUS_UNKNOWN;
  627. platform_set_drvdata(pdev, info);
  628. mutex_init(&info->lock);
  629. INIT_DELAYED_WORK(&info->status_monitor, fuel_gauge_status_monitor);
  630. ret = fuel_gauge_reg_readb(info, AXP288_FG_DES_CAP1_REG);
  631. if (ret < 0)
  632. return ret;
  633. if (!(ret & FG_DES_CAP1_VALID)) {
  634. dev_err(&pdev->dev, "axp288 not configured by firmware\n");
  635. return -ENODEV;
  636. }
  637. ret = fuel_gauge_reg_readb(info, AXP20X_CHRG_CTRL1);
  638. if (ret < 0)
  639. return ret;
  640. switch ((ret & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS) {
  641. case CHRG_CCCV_CV_4100MV:
  642. info->max_volt = 4100;
  643. break;
  644. case CHRG_CCCV_CV_4150MV:
  645. info->max_volt = 4150;
  646. break;
  647. case CHRG_CCCV_CV_4200MV:
  648. info->max_volt = 4200;
  649. break;
  650. case CHRG_CCCV_CV_4350MV:
  651. info->max_volt = 4350;
  652. break;
  653. }
  654. psy_cfg.drv_data = info;
  655. info->bat = power_supply_register(&pdev->dev, &fuel_gauge_desc, &psy_cfg);
  656. if (IS_ERR(info->bat)) {
  657. ret = PTR_ERR(info->bat);
  658. dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
  659. return ret;
  660. }
  661. fuel_gauge_create_debugfs(info);
  662. fuel_gauge_init_irq(info);
  663. schedule_delayed_work(&info->status_monitor, STATUS_MON_DELAY_JIFFIES);
  664. return 0;
  665. }
  666. static const struct platform_device_id axp288_fg_id_table[] = {
  667. { .name = DEV_NAME },
  668. {},
  669. };
  670. MODULE_DEVICE_TABLE(platform, axp288_fg_id_table);
  671. static int axp288_fuel_gauge_remove(struct platform_device *pdev)
  672. {
  673. struct axp288_fg_info *info = platform_get_drvdata(pdev);
  674. int i;
  675. cancel_delayed_work_sync(&info->status_monitor);
  676. power_supply_unregister(info->bat);
  677. fuel_gauge_remove_debugfs(info);
  678. for (i = 0; i < AXP288_FG_INTR_NUM; i++)
  679. if (info->irq[i] >= 0)
  680. free_irq(info->irq[i], info);
  681. return 0;
  682. }
  683. static struct platform_driver axp288_fuel_gauge_driver = {
  684. .probe = axp288_fuel_gauge_probe,
  685. .remove = axp288_fuel_gauge_remove,
  686. .id_table = axp288_fg_id_table,
  687. .driver = {
  688. .name = DEV_NAME,
  689. },
  690. };
  691. module_platform_driver(axp288_fuel_gauge_driver);
  692. MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
  693. MODULE_AUTHOR("Todd Brandt <todd.e.brandt@linux.intel.com>");
  694. MODULE_DESCRIPTION("Xpower AXP288 Fuel Gauge Driver");
  695. MODULE_LICENSE("GPL");