cpcap-battery.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Battery driver for CPCAP PMIC
  4. *
  5. * Copyright (C) 2017 Tony Lindgren <[email protected]>
  6. *
  7. * Some parts of the code based on earlier Motorola mapphone Linux kernel
  8. * drivers:
  9. *
  10. * Copyright (C) 2009-2010 Motorola, Inc.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of_device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/reboot.h>
  21. #include <linux/regmap.h>
  22. #include <linux/nvmem-consumer.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/iio/consumer.h>
  25. #include <linux/iio/types.h>
  26. #include <linux/mfd/motorola-cpcap.h>
  27. /*
  28. * Register bit defines for CPCAP_REG_BPEOL. Some of these seem to
  29. * map to MC13783UG.pdf "Table 5-19. Register 13, Power Control 0"
  30. * to enable BATTDETEN, LOBAT and EOL features. We currently use
  31. * LOBAT interrupts instead of EOL.
  32. */
  33. #define CPCAP_REG_BPEOL_BIT_EOL9 BIT(9) /* Set for EOL irq */
  34. #define CPCAP_REG_BPEOL_BIT_EOL8 BIT(8) /* Set for EOL irq */
  35. #define CPCAP_REG_BPEOL_BIT_UNKNOWN7 BIT(7)
  36. #define CPCAP_REG_BPEOL_BIT_UNKNOWN6 BIT(6)
  37. #define CPCAP_REG_BPEOL_BIT_UNKNOWN5 BIT(5)
  38. #define CPCAP_REG_BPEOL_BIT_EOL_MULTI BIT(4) /* Set for multiple EOL irqs */
  39. #define CPCAP_REG_BPEOL_BIT_UNKNOWN3 BIT(3)
  40. #define CPCAP_REG_BPEOL_BIT_UNKNOWN2 BIT(2)
  41. #define CPCAP_REG_BPEOL_BIT_BATTDETEN BIT(1) /* Enable battery detect */
  42. #define CPCAP_REG_BPEOL_BIT_EOLSEL BIT(0) /* BPDET = 0, EOL = 1 */
  43. /*
  44. * Register bit defines for CPCAP_REG_CCC1. These seem similar to the twl6030
  45. * coulomb counter registers rather than the mc13892 registers. Both twl6030
  46. * and mc13892 set bits 2 and 1 to reset and clear registers. But mc13892
  47. * sets bit 0 to start the coulomb counter while twl6030 sets bit 0 to stop
  48. * the coulomb counter like cpcap does. So for now, we use the twl6030 style
  49. * naming for the registers.
  50. */
  51. #define CPCAP_REG_CCC1_ACTIVE_MODE1 BIT(4) /* Update rate */
  52. #define CPCAP_REG_CCC1_ACTIVE_MODE0 BIT(3) /* Update rate */
  53. #define CPCAP_REG_CCC1_AUTOCLEAR BIT(2) /* Resets sample registers */
  54. #define CPCAP_REG_CCC1_CAL_EN BIT(1) /* Clears after write in 1s */
  55. #define CPCAP_REG_CCC1_PAUSE BIT(0) /* Stop counters, allow write */
  56. #define CPCAP_REG_CCC1_RESET_MASK (CPCAP_REG_CCC1_AUTOCLEAR | \
  57. CPCAP_REG_CCC1_CAL_EN)
  58. #define CPCAP_REG_CCCC2_RATE1 BIT(5)
  59. #define CPCAP_REG_CCCC2_RATE0 BIT(4)
  60. #define CPCAP_REG_CCCC2_ENABLE BIT(3)
  61. #define CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS 250
  62. #define CPCAP_BATTERY_EB41_HW4X_ID 0x9E
  63. #define CPCAP_BATTERY_BW8X_ID 0x98
  64. enum {
  65. CPCAP_BATTERY_IIO_BATTDET,
  66. CPCAP_BATTERY_IIO_VOLTAGE,
  67. CPCAP_BATTERY_IIO_CHRG_CURRENT,
  68. CPCAP_BATTERY_IIO_BATT_CURRENT,
  69. CPCAP_BATTERY_IIO_NR,
  70. };
  71. enum cpcap_battery_irq_action {
  72. CPCAP_BATTERY_IRQ_ACTION_NONE,
  73. CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE,
  74. CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW,
  75. CPCAP_BATTERY_IRQ_ACTION_POWEROFF,
  76. };
  77. struct cpcap_interrupt_desc {
  78. const char *name;
  79. struct list_head node;
  80. int irq;
  81. enum cpcap_battery_irq_action action;
  82. };
  83. struct cpcap_battery_config {
  84. int cd_factor;
  85. struct power_supply_info info;
  86. struct power_supply_battery_info bat;
  87. };
  88. struct cpcap_coulomb_counter_data {
  89. s32 sample; /* 24 or 32 bits */
  90. s32 accumulator;
  91. s16 offset; /* 9 bits */
  92. s16 integrator; /* 13 or 16 bits */
  93. };
  94. enum cpcap_battery_state {
  95. CPCAP_BATTERY_STATE_PREVIOUS,
  96. CPCAP_BATTERY_STATE_LATEST,
  97. CPCAP_BATTERY_STATE_EMPTY,
  98. CPCAP_BATTERY_STATE_FULL,
  99. CPCAP_BATTERY_STATE_NR,
  100. };
  101. struct cpcap_battery_state_data {
  102. int voltage;
  103. int current_ua;
  104. int counter_uah;
  105. int temperature;
  106. ktime_t time;
  107. struct cpcap_coulomb_counter_data cc;
  108. };
  109. struct cpcap_battery_ddata {
  110. struct device *dev;
  111. struct regmap *reg;
  112. struct list_head irq_list;
  113. struct iio_channel *channels[CPCAP_BATTERY_IIO_NR];
  114. struct power_supply *psy;
  115. struct cpcap_battery_config config;
  116. struct cpcap_battery_state_data state[CPCAP_BATTERY_STATE_NR];
  117. u32 cc_lsb; /* μAms per LSB */
  118. atomic_t active;
  119. int charge_full;
  120. int status;
  121. u16 vendor;
  122. bool check_nvmem;
  123. unsigned int is_full:1;
  124. };
  125. #define CPCAP_NO_BATTERY -400
  126. static bool ignore_temperature_probe;
  127. module_param(ignore_temperature_probe, bool, 0660);
  128. static struct cpcap_battery_state_data *
  129. cpcap_battery_get_state(struct cpcap_battery_ddata *ddata,
  130. enum cpcap_battery_state state)
  131. {
  132. if (state >= CPCAP_BATTERY_STATE_NR)
  133. return NULL;
  134. return &ddata->state[state];
  135. }
  136. static struct cpcap_battery_state_data *
  137. cpcap_battery_latest(struct cpcap_battery_ddata *ddata)
  138. {
  139. return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_LATEST);
  140. }
  141. static struct cpcap_battery_state_data *
  142. cpcap_battery_previous(struct cpcap_battery_ddata *ddata)
  143. {
  144. return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_PREVIOUS);
  145. }
  146. static struct cpcap_battery_state_data *
  147. cpcap_battery_get_empty(struct cpcap_battery_ddata *ddata)
  148. {
  149. return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_EMPTY);
  150. }
  151. static struct cpcap_battery_state_data *
  152. cpcap_battery_get_full(struct cpcap_battery_ddata *ddata)
  153. {
  154. return cpcap_battery_get_state(ddata, CPCAP_BATTERY_STATE_FULL);
  155. }
  156. static int cpcap_charger_battery_temperature(struct cpcap_battery_ddata *ddata,
  157. int *value)
  158. {
  159. struct iio_channel *channel;
  160. int error;
  161. channel = ddata->channels[CPCAP_BATTERY_IIO_BATTDET];
  162. error = iio_read_channel_processed(channel, value);
  163. if (error < 0) {
  164. if (!ignore_temperature_probe)
  165. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  166. *value = CPCAP_NO_BATTERY;
  167. return error;
  168. }
  169. *value /= 100;
  170. return 0;
  171. }
  172. static int cpcap_battery_get_voltage(struct cpcap_battery_ddata *ddata)
  173. {
  174. struct iio_channel *channel;
  175. int error, value = 0;
  176. channel = ddata->channels[CPCAP_BATTERY_IIO_VOLTAGE];
  177. error = iio_read_channel_processed(channel, &value);
  178. if (error < 0) {
  179. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  180. return 0;
  181. }
  182. return value * 1000;
  183. }
  184. static int cpcap_battery_get_current(struct cpcap_battery_ddata *ddata)
  185. {
  186. struct iio_channel *channel;
  187. int error, value = 0;
  188. channel = ddata->channels[CPCAP_BATTERY_IIO_BATT_CURRENT];
  189. error = iio_read_channel_processed(channel, &value);
  190. if (error < 0) {
  191. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  192. return 0;
  193. }
  194. return value * 1000;
  195. }
  196. /**
  197. * cpcap_battery_cc_raw_div - calculate and divide coulomb counter μAms values
  198. * @ddata: device driver data
  199. * @sample: coulomb counter sample value
  200. * @accumulator: coulomb counter integrator value
  201. * @offset: coulomb counter offset value
  202. * @divider: conversion divider
  203. *
  204. * Note that cc_lsb and cc_dur values are from Motorola Linux kernel
  205. * function data_get_avg_curr_ua() and seem to be based on measured test
  206. * results. It also has the following comment:
  207. *
  208. * Adjustment factors are applied here as a temp solution per the test
  209. * results. Need to work out a formal solution for this adjustment.
  210. *
  211. * A coulomb counter for similar hardware seems to be documented in
  212. * "TWL6030 Gas Gauging Basics (Rev. A)" swca095a.pdf in chapter
  213. * "10 Calculating Accumulated Current". We however follow what the
  214. * Motorola mapphone Linux kernel is doing as there may be either a
  215. * TI or ST coulomb counter in the PMIC.
  216. */
  217. static int cpcap_battery_cc_raw_div(struct cpcap_battery_ddata *ddata,
  218. s32 sample, s32 accumulator,
  219. s16 offset, u32 divider)
  220. {
  221. s64 acc;
  222. if (!divider)
  223. return 0;
  224. acc = accumulator;
  225. acc -= (s64)sample * offset;
  226. acc *= ddata->cc_lsb;
  227. acc *= -1;
  228. acc = div_s64(acc, divider);
  229. return acc;
  230. }
  231. /* 3600000μAms = 1μAh */
  232. static int cpcap_battery_cc_to_uah(struct cpcap_battery_ddata *ddata,
  233. s32 sample, s32 accumulator,
  234. s16 offset)
  235. {
  236. return cpcap_battery_cc_raw_div(ddata, sample,
  237. accumulator, offset,
  238. 3600000);
  239. }
  240. static int cpcap_battery_cc_to_ua(struct cpcap_battery_ddata *ddata,
  241. s32 sample, s32 accumulator,
  242. s16 offset)
  243. {
  244. return cpcap_battery_cc_raw_div(ddata, sample,
  245. accumulator, offset,
  246. sample *
  247. CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS);
  248. }
  249. /**
  250. * cpcap_battery_read_accumulated - reads cpcap coulomb counter
  251. * @ddata: device driver data
  252. * @ccd: coulomb counter values
  253. *
  254. * Based on Motorola mapphone kernel function data_read_regs().
  255. * Looking at the registers, the coulomb counter seems similar to
  256. * the coulomb counter in TWL6030. See "TWL6030 Gas Gauging Basics
  257. * (Rev. A) swca095a.pdf for "10 Calculating Accumulated Current".
  258. *
  259. * Note that swca095a.pdf instructs to stop the coulomb counter
  260. * before reading to avoid values changing. Motorola mapphone
  261. * Linux kernel does not do it, so let's assume they've verified
  262. * the data produced is correct.
  263. */
  264. static int
  265. cpcap_battery_read_accumulated(struct cpcap_battery_ddata *ddata,
  266. struct cpcap_coulomb_counter_data *ccd)
  267. {
  268. u16 buf[7]; /* CPCAP_REG_CCS1 to CCI */
  269. int error;
  270. ccd->sample = 0;
  271. ccd->accumulator = 0;
  272. ccd->offset = 0;
  273. ccd->integrator = 0;
  274. /* Read coulomb counter register range */
  275. error = regmap_bulk_read(ddata->reg, CPCAP_REG_CCS1,
  276. buf, ARRAY_SIZE(buf));
  277. if (error)
  278. return 0;
  279. /* Sample value CPCAP_REG_CCS1 & 2 */
  280. ccd->sample = (buf[1] & 0x0fff) << 16;
  281. ccd->sample |= buf[0];
  282. if (ddata->vendor == CPCAP_VENDOR_TI)
  283. ccd->sample = sign_extend32(24, ccd->sample);
  284. /* Accumulator value CPCAP_REG_CCA1 & 2 */
  285. ccd->accumulator = ((s16)buf[3]) << 16;
  286. ccd->accumulator |= buf[2];
  287. /*
  288. * Coulomb counter calibration offset is CPCAP_REG_CCM,
  289. * REG_CCO seems unused
  290. */
  291. ccd->offset = buf[4];
  292. ccd->offset = sign_extend32(ccd->offset, 9);
  293. /* Integrator register CPCAP_REG_CCI */
  294. if (ddata->vendor == CPCAP_VENDOR_TI)
  295. ccd->integrator = sign_extend32(buf[6], 13);
  296. else
  297. ccd->integrator = (s16)buf[6];
  298. return cpcap_battery_cc_to_uah(ddata,
  299. ccd->sample,
  300. ccd->accumulator,
  301. ccd->offset);
  302. }
  303. /*
  304. * Based on the values from Motorola mapphone Linux kernel for the
  305. * stock Droid 4 battery eb41. In the Motorola mapphone Linux
  306. * kernel tree the value for pm_cd_factor is passed to the kernel
  307. * via device tree. If it turns out to be something device specific
  308. * we can consider that too later. These values are also fine for
  309. * Bionic's hw4x.
  310. *
  311. * And looking at the battery full and shutdown values for the stock
  312. * kernel on droid 4, full is 4351000 and software initiates shutdown
  313. * at 3078000. The device will die around 2743000.
  314. */
  315. static const struct cpcap_battery_config cpcap_battery_eb41_data = {
  316. .cd_factor = 0x3cc,
  317. .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
  318. .info.voltage_max_design = 4351000,
  319. .info.voltage_min_design = 3100000,
  320. .info.charge_full_design = 1740000,
  321. .bat.constant_charge_voltage_max_uv = 4200000,
  322. };
  323. /* Values for the extended Droid Bionic battery bw8x. */
  324. static const struct cpcap_battery_config cpcap_battery_bw8x_data = {
  325. .cd_factor = 0x3cc,
  326. .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
  327. .info.voltage_max_design = 4200000,
  328. .info.voltage_min_design = 3200000,
  329. .info.charge_full_design = 2760000,
  330. .bat.constant_charge_voltage_max_uv = 4200000,
  331. };
  332. /*
  333. * Safe values for any lipo battery likely to fit into a mapphone
  334. * battery bay.
  335. */
  336. static const struct cpcap_battery_config cpcap_battery_unkown_data = {
  337. .cd_factor = 0x3cc,
  338. .info.technology = POWER_SUPPLY_TECHNOLOGY_LION,
  339. .info.voltage_max_design = 4200000,
  340. .info.voltage_min_design = 3200000,
  341. .info.charge_full_design = 3000000,
  342. .bat.constant_charge_voltage_max_uv = 4200000,
  343. };
  344. static int cpcap_battery_match_nvmem(struct device *dev, const void *data)
  345. {
  346. if (strcmp(dev_name(dev), "89-500029ba0f73") == 0)
  347. return 1;
  348. else
  349. return 0;
  350. }
  351. static void cpcap_battery_detect_battery_type(struct cpcap_battery_ddata *ddata)
  352. {
  353. struct nvmem_device *nvmem;
  354. u8 battery_id = 0;
  355. ddata->check_nvmem = false;
  356. nvmem = nvmem_device_find(NULL, &cpcap_battery_match_nvmem);
  357. if (IS_ERR_OR_NULL(nvmem)) {
  358. ddata->check_nvmem = true;
  359. dev_info_once(ddata->dev, "Can not find battery nvmem device. Assuming generic lipo battery\n");
  360. } else if (nvmem_device_read(nvmem, 2, 1, &battery_id) < 0) {
  361. battery_id = 0;
  362. ddata->check_nvmem = true;
  363. dev_warn(ddata->dev, "Can not read battery nvmem device. Assuming generic lipo battery\n");
  364. }
  365. switch (battery_id) {
  366. case CPCAP_BATTERY_EB41_HW4X_ID:
  367. ddata->config = cpcap_battery_eb41_data;
  368. break;
  369. case CPCAP_BATTERY_BW8X_ID:
  370. ddata->config = cpcap_battery_bw8x_data;
  371. break;
  372. default:
  373. ddata->config = cpcap_battery_unkown_data;
  374. }
  375. }
  376. /**
  377. * cpcap_battery_cc_get_avg_current - read cpcap coulumb counter
  378. * @ddata: cpcap battery driver device data
  379. */
  380. static int cpcap_battery_cc_get_avg_current(struct cpcap_battery_ddata *ddata)
  381. {
  382. int value, acc, error;
  383. s32 sample;
  384. s16 offset;
  385. /* Coulomb counter integrator */
  386. error = regmap_read(ddata->reg, CPCAP_REG_CCI, &value);
  387. if (error)
  388. return error;
  389. if (ddata->vendor == CPCAP_VENDOR_TI) {
  390. acc = sign_extend32(value, 13);
  391. sample = 1;
  392. } else {
  393. acc = (s16)value;
  394. sample = 4;
  395. }
  396. /* Coulomb counter calibration offset */
  397. error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
  398. if (error)
  399. return error;
  400. offset = sign_extend32(value, 9);
  401. return cpcap_battery_cc_to_ua(ddata, sample, acc, offset);
  402. }
  403. static int cpcap_battery_get_charger_status(struct cpcap_battery_ddata *ddata,
  404. int *val)
  405. {
  406. union power_supply_propval prop;
  407. struct power_supply *charger;
  408. int error;
  409. charger = power_supply_get_by_name("usb");
  410. if (!charger)
  411. return -ENODEV;
  412. error = power_supply_get_property(charger, POWER_SUPPLY_PROP_STATUS,
  413. &prop);
  414. if (error)
  415. *val = POWER_SUPPLY_STATUS_UNKNOWN;
  416. else
  417. *val = prop.intval;
  418. power_supply_put(charger);
  419. return error;
  420. }
  421. static bool cpcap_battery_full(struct cpcap_battery_ddata *ddata)
  422. {
  423. struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
  424. unsigned int vfull;
  425. int error, val;
  426. error = cpcap_battery_get_charger_status(ddata, &val);
  427. if (!error) {
  428. switch (val) {
  429. case POWER_SUPPLY_STATUS_DISCHARGING:
  430. dev_dbg(ddata->dev, "charger disconnected\n");
  431. ddata->is_full = 0;
  432. break;
  433. case POWER_SUPPLY_STATUS_FULL:
  434. dev_dbg(ddata->dev, "charger full status\n");
  435. ddata->is_full = 1;
  436. break;
  437. default:
  438. break;
  439. }
  440. }
  441. /*
  442. * The full battery voltage here can be inaccurate, it's used just to
  443. * filter out any trickle charging events. We clear the is_full status
  444. * on charger disconnect above anyways.
  445. */
  446. vfull = ddata->config.bat.constant_charge_voltage_max_uv - 120000;
  447. if (ddata->is_full && state->voltage < vfull)
  448. ddata->is_full = 0;
  449. return ddata->is_full;
  450. }
  451. static bool cpcap_battery_low(struct cpcap_battery_ddata *ddata)
  452. {
  453. struct cpcap_battery_state_data *state = cpcap_battery_latest(ddata);
  454. static bool is_low;
  455. if (state->current_ua > 0 && (state->voltage <= 3350000 || is_low))
  456. is_low = true;
  457. else
  458. is_low = false;
  459. return is_low;
  460. }
  461. static int cpcap_battery_update_status(struct cpcap_battery_ddata *ddata)
  462. {
  463. struct cpcap_battery_state_data state, *latest, *previous,
  464. *empty, *full;
  465. ktime_t now;
  466. int error;
  467. memset(&state, 0, sizeof(state));
  468. now = ktime_get();
  469. latest = cpcap_battery_latest(ddata);
  470. if (latest) {
  471. s64 delta_ms = ktime_to_ms(ktime_sub(now, latest->time));
  472. if (delta_ms < CPCAP_BATTERY_CC_SAMPLE_PERIOD_MS)
  473. return delta_ms;
  474. }
  475. state.time = now;
  476. state.voltage = cpcap_battery_get_voltage(ddata);
  477. state.current_ua = cpcap_battery_get_current(ddata);
  478. state.counter_uah = cpcap_battery_read_accumulated(ddata, &state.cc);
  479. error = cpcap_charger_battery_temperature(ddata,
  480. &state.temperature);
  481. if (error)
  482. return error;
  483. previous = cpcap_battery_previous(ddata);
  484. memcpy(previous, latest, sizeof(*previous));
  485. memcpy(latest, &state, sizeof(*latest));
  486. if (cpcap_battery_full(ddata)) {
  487. full = cpcap_battery_get_full(ddata);
  488. memcpy(full, latest, sizeof(*full));
  489. empty = cpcap_battery_get_empty(ddata);
  490. if (empty->voltage && empty->voltage != -1) {
  491. empty->voltage = -1;
  492. ddata->charge_full =
  493. empty->counter_uah - full->counter_uah;
  494. } else if (ddata->charge_full) {
  495. empty->voltage = -1;
  496. empty->counter_uah =
  497. full->counter_uah + ddata->charge_full;
  498. }
  499. } else if (cpcap_battery_low(ddata)) {
  500. empty = cpcap_battery_get_empty(ddata);
  501. memcpy(empty, latest, sizeof(*empty));
  502. full = cpcap_battery_get_full(ddata);
  503. if (full->voltage) {
  504. full->voltage = 0;
  505. ddata->charge_full =
  506. empty->counter_uah - full->counter_uah;
  507. }
  508. }
  509. return 0;
  510. }
  511. /*
  512. * Update battery status when cpcap-charger calls power_supply_changed().
  513. * This allows us to detect battery full condition before the charger
  514. * disconnects.
  515. */
  516. static void cpcap_battery_external_power_changed(struct power_supply *psy)
  517. {
  518. union power_supply_propval prop;
  519. power_supply_get_property(psy, POWER_SUPPLY_PROP_STATUS, &prop);
  520. }
  521. static enum power_supply_property cpcap_battery_props[] = {
  522. POWER_SUPPLY_PROP_STATUS,
  523. POWER_SUPPLY_PROP_PRESENT,
  524. POWER_SUPPLY_PROP_TECHNOLOGY,
  525. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  526. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  527. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  528. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  529. POWER_SUPPLY_PROP_CURRENT_AVG,
  530. POWER_SUPPLY_PROP_CURRENT_NOW,
  531. POWER_SUPPLY_PROP_CHARGE_FULL,
  532. POWER_SUPPLY_PROP_CHARGE_NOW,
  533. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  534. POWER_SUPPLY_PROP_CHARGE_COUNTER,
  535. POWER_SUPPLY_PROP_POWER_NOW,
  536. POWER_SUPPLY_PROP_POWER_AVG,
  537. POWER_SUPPLY_PROP_CAPACITY,
  538. POWER_SUPPLY_PROP_CAPACITY_LEVEL,
  539. POWER_SUPPLY_PROP_SCOPE,
  540. POWER_SUPPLY_PROP_TEMP,
  541. };
  542. static int cpcap_battery_get_property(struct power_supply *psy,
  543. enum power_supply_property psp,
  544. union power_supply_propval *val)
  545. {
  546. struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
  547. struct cpcap_battery_state_data *latest, *previous, *empty;
  548. u32 sample;
  549. s32 accumulator;
  550. int cached;
  551. s64 tmp;
  552. cached = cpcap_battery_update_status(ddata);
  553. if (cached < 0)
  554. return cached;
  555. latest = cpcap_battery_latest(ddata);
  556. previous = cpcap_battery_previous(ddata);
  557. if (ddata->check_nvmem)
  558. cpcap_battery_detect_battery_type(ddata);
  559. switch (psp) {
  560. case POWER_SUPPLY_PROP_PRESENT:
  561. if (latest->temperature > CPCAP_NO_BATTERY || ignore_temperature_probe)
  562. val->intval = 1;
  563. else
  564. val->intval = 0;
  565. break;
  566. case POWER_SUPPLY_PROP_STATUS:
  567. if (cpcap_battery_full(ddata)) {
  568. val->intval = POWER_SUPPLY_STATUS_FULL;
  569. break;
  570. }
  571. if (cpcap_battery_cc_get_avg_current(ddata) < 0)
  572. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  573. else
  574. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  575. break;
  576. case POWER_SUPPLY_PROP_TECHNOLOGY:
  577. val->intval = ddata->config.info.technology;
  578. break;
  579. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  580. val->intval = cpcap_battery_get_voltage(ddata);
  581. break;
  582. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  583. val->intval = ddata->config.info.voltage_max_design;
  584. break;
  585. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  586. val->intval = ddata->config.info.voltage_min_design;
  587. break;
  588. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  589. val->intval = ddata->config.bat.constant_charge_voltage_max_uv;
  590. break;
  591. case POWER_SUPPLY_PROP_CURRENT_AVG:
  592. sample = latest->cc.sample - previous->cc.sample;
  593. if (!sample) {
  594. val->intval = cpcap_battery_cc_get_avg_current(ddata);
  595. break;
  596. }
  597. accumulator = latest->cc.accumulator - previous->cc.accumulator;
  598. val->intval = cpcap_battery_cc_to_ua(ddata, sample,
  599. accumulator,
  600. latest->cc.offset);
  601. break;
  602. case POWER_SUPPLY_PROP_CURRENT_NOW:
  603. val->intval = latest->current_ua;
  604. break;
  605. case POWER_SUPPLY_PROP_CHARGE_COUNTER:
  606. val->intval = latest->counter_uah;
  607. break;
  608. case POWER_SUPPLY_PROP_POWER_NOW:
  609. tmp = (latest->voltage / 10000) * latest->current_ua;
  610. val->intval = div64_s64(tmp, 100);
  611. break;
  612. case POWER_SUPPLY_PROP_POWER_AVG:
  613. sample = latest->cc.sample - previous->cc.sample;
  614. if (!sample) {
  615. tmp = cpcap_battery_cc_get_avg_current(ddata);
  616. tmp *= (latest->voltage / 10000);
  617. val->intval = div64_s64(tmp, 100);
  618. break;
  619. }
  620. accumulator = latest->cc.accumulator - previous->cc.accumulator;
  621. tmp = cpcap_battery_cc_to_ua(ddata, sample, accumulator,
  622. latest->cc.offset);
  623. tmp *= ((latest->voltage + previous->voltage) / 20000);
  624. val->intval = div64_s64(tmp, 100);
  625. break;
  626. case POWER_SUPPLY_PROP_CAPACITY:
  627. empty = cpcap_battery_get_empty(ddata);
  628. if (!empty->voltage || !ddata->charge_full)
  629. return -ENODATA;
  630. /* (ddata->charge_full / 200) is needed for rounding */
  631. val->intval = empty->counter_uah - latest->counter_uah +
  632. ddata->charge_full / 200;
  633. val->intval = clamp(val->intval, 0, ddata->charge_full);
  634. val->intval = val->intval * 100 / ddata->charge_full;
  635. break;
  636. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  637. if (cpcap_battery_full(ddata))
  638. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
  639. else if (latest->voltage >= 3750000)
  640. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_HIGH;
  641. else if (latest->voltage >= 3300000)
  642. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
  643. else if (latest->voltage > 3100000)
  644. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
  645. else if (latest->voltage <= 3100000)
  646. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
  647. else
  648. val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN;
  649. break;
  650. case POWER_SUPPLY_PROP_CHARGE_NOW:
  651. empty = cpcap_battery_get_empty(ddata);
  652. if (!empty->voltage)
  653. return -ENODATA;
  654. val->intval = empty->counter_uah - latest->counter_uah;
  655. if (val->intval < 0) {
  656. /* Assume invalid config if CHARGE_NOW is -20% */
  657. if (ddata->charge_full && abs(val->intval) > ddata->charge_full/5) {
  658. empty->voltage = 0;
  659. ddata->charge_full = 0;
  660. return -ENODATA;
  661. }
  662. val->intval = 0;
  663. } else if (ddata->charge_full && ddata->charge_full < val->intval) {
  664. /* Assume invalid config if CHARGE_NOW exceeds CHARGE_FULL by 20% */
  665. if (val->intval > (6*ddata->charge_full)/5) {
  666. empty->voltage = 0;
  667. ddata->charge_full = 0;
  668. return -ENODATA;
  669. }
  670. val->intval = ddata->charge_full;
  671. }
  672. break;
  673. case POWER_SUPPLY_PROP_CHARGE_FULL:
  674. if (!ddata->charge_full)
  675. return -ENODATA;
  676. val->intval = ddata->charge_full;
  677. break;
  678. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  679. val->intval = ddata->config.info.charge_full_design;
  680. break;
  681. case POWER_SUPPLY_PROP_SCOPE:
  682. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  683. break;
  684. case POWER_SUPPLY_PROP_TEMP:
  685. if (ignore_temperature_probe)
  686. return -ENODATA;
  687. val->intval = latest->temperature;
  688. break;
  689. default:
  690. return -EINVAL;
  691. }
  692. return 0;
  693. }
  694. static int cpcap_battery_update_charger(struct cpcap_battery_ddata *ddata,
  695. int const_charge_voltage)
  696. {
  697. union power_supply_propval prop;
  698. union power_supply_propval val;
  699. struct power_supply *charger;
  700. int error;
  701. charger = power_supply_get_by_name("usb");
  702. if (!charger)
  703. return -ENODEV;
  704. error = power_supply_get_property(charger,
  705. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  706. &prop);
  707. if (error)
  708. goto out_put;
  709. /* Allow charger const voltage lower than battery const voltage */
  710. if (const_charge_voltage > prop.intval)
  711. goto out_put;
  712. val.intval = const_charge_voltage;
  713. error = power_supply_set_property(charger,
  714. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  715. &val);
  716. out_put:
  717. power_supply_put(charger);
  718. return error;
  719. }
  720. static int cpcap_battery_set_property(struct power_supply *psy,
  721. enum power_supply_property psp,
  722. const union power_supply_propval *val)
  723. {
  724. struct cpcap_battery_ddata *ddata = power_supply_get_drvdata(psy);
  725. switch (psp) {
  726. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  727. if (val->intval < ddata->config.info.voltage_min_design)
  728. return -EINVAL;
  729. if (val->intval > ddata->config.info.voltage_max_design)
  730. return -EINVAL;
  731. ddata->config.bat.constant_charge_voltage_max_uv = val->intval;
  732. return cpcap_battery_update_charger(ddata, val->intval);
  733. case POWER_SUPPLY_PROP_CHARGE_FULL:
  734. if (val->intval < 0)
  735. return -EINVAL;
  736. if (val->intval > (6*ddata->config.info.charge_full_design)/5)
  737. return -EINVAL;
  738. ddata->charge_full = val->intval;
  739. return 0;
  740. default:
  741. return -EINVAL;
  742. }
  743. return 0;
  744. }
  745. static int cpcap_battery_property_is_writeable(struct power_supply *psy,
  746. enum power_supply_property psp)
  747. {
  748. switch (psp) {
  749. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  750. case POWER_SUPPLY_PROP_CHARGE_FULL:
  751. return 1;
  752. default:
  753. return 0;
  754. }
  755. }
  756. static irqreturn_t cpcap_battery_irq_thread(int irq, void *data)
  757. {
  758. struct cpcap_battery_ddata *ddata = data;
  759. struct cpcap_battery_state_data *latest;
  760. struct cpcap_interrupt_desc *d;
  761. if (!atomic_read(&ddata->active))
  762. return IRQ_NONE;
  763. list_for_each_entry(d, &ddata->irq_list, node) {
  764. if (irq == d->irq)
  765. break;
  766. }
  767. if (list_entry_is_head(d, &ddata->irq_list, node))
  768. return IRQ_NONE;
  769. latest = cpcap_battery_latest(ddata);
  770. switch (d->action) {
  771. case CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE:
  772. dev_info(ddata->dev, "Coulomb counter calibration done\n");
  773. break;
  774. case CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW:
  775. if (latest->current_ua >= 0)
  776. dev_warn(ddata->dev, "Battery low at %imV!\n",
  777. latest->voltage / 1000);
  778. break;
  779. case CPCAP_BATTERY_IRQ_ACTION_POWEROFF:
  780. if (latest->current_ua >= 0 && latest->voltage <= 3200000) {
  781. dev_emerg(ddata->dev,
  782. "Battery empty at %imV, powering off\n",
  783. latest->voltage / 1000);
  784. orderly_poweroff(true);
  785. }
  786. break;
  787. default:
  788. break;
  789. }
  790. power_supply_changed(ddata->psy);
  791. return IRQ_HANDLED;
  792. }
  793. static int cpcap_battery_init_irq(struct platform_device *pdev,
  794. struct cpcap_battery_ddata *ddata,
  795. const char *name)
  796. {
  797. struct cpcap_interrupt_desc *d;
  798. int irq, error;
  799. irq = platform_get_irq_byname(pdev, name);
  800. if (irq < 0)
  801. return irq;
  802. error = devm_request_threaded_irq(ddata->dev, irq, NULL,
  803. cpcap_battery_irq_thread,
  804. IRQF_SHARED | IRQF_ONESHOT,
  805. name, ddata);
  806. if (error) {
  807. dev_err(ddata->dev, "could not get irq %s: %i\n",
  808. name, error);
  809. return error;
  810. }
  811. d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
  812. if (!d)
  813. return -ENOMEM;
  814. d->name = name;
  815. d->irq = irq;
  816. if (!strncmp(name, "cccal", 5))
  817. d->action = CPCAP_BATTERY_IRQ_ACTION_CC_CAL_DONE;
  818. else if (!strncmp(name, "lowbph", 6))
  819. d->action = CPCAP_BATTERY_IRQ_ACTION_BATTERY_LOW;
  820. else if (!strncmp(name, "lowbpl", 6))
  821. d->action = CPCAP_BATTERY_IRQ_ACTION_POWEROFF;
  822. list_add(&d->node, &ddata->irq_list);
  823. return 0;
  824. }
  825. static int cpcap_battery_init_interrupts(struct platform_device *pdev,
  826. struct cpcap_battery_ddata *ddata)
  827. {
  828. static const char * const cpcap_battery_irqs[] = {
  829. "eol", "lowbph", "lowbpl",
  830. "chrgcurr1", "battdetb"
  831. };
  832. int i, error;
  833. for (i = 0; i < ARRAY_SIZE(cpcap_battery_irqs); i++) {
  834. error = cpcap_battery_init_irq(pdev, ddata,
  835. cpcap_battery_irqs[i]);
  836. if (error)
  837. return error;
  838. }
  839. /* Enable calibration interrupt if already available in dts */
  840. cpcap_battery_init_irq(pdev, ddata, "cccal");
  841. /* Enable low battery interrupts for 3.3V high and 3.1V low */
  842. error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
  843. 0xffff,
  844. CPCAP_REG_BPEOL_BIT_BATTDETEN);
  845. if (error)
  846. return error;
  847. return 0;
  848. }
  849. static int cpcap_battery_init_iio(struct cpcap_battery_ddata *ddata)
  850. {
  851. const char * const names[CPCAP_BATTERY_IIO_NR] = {
  852. "battdetb", "battp", "chg_isense", "batti",
  853. };
  854. int error, i;
  855. for (i = 0; i < CPCAP_BATTERY_IIO_NR; i++) {
  856. ddata->channels[i] = devm_iio_channel_get(ddata->dev,
  857. names[i]);
  858. if (IS_ERR(ddata->channels[i])) {
  859. error = PTR_ERR(ddata->channels[i]);
  860. goto out_err;
  861. }
  862. if (!ddata->channels[i]->indio_dev) {
  863. error = -ENXIO;
  864. goto out_err;
  865. }
  866. }
  867. return 0;
  868. out_err:
  869. return dev_err_probe(ddata->dev, error,
  870. "could not initialize VBUS or ID IIO\n");
  871. }
  872. /* Calibrate coulomb counter */
  873. static int cpcap_battery_calibrate(struct cpcap_battery_ddata *ddata)
  874. {
  875. int error, ccc1, value;
  876. unsigned long timeout;
  877. error = regmap_read(ddata->reg, CPCAP_REG_CCC1, &ccc1);
  878. if (error)
  879. return error;
  880. timeout = jiffies + msecs_to_jiffies(6000);
  881. /* Start calibration */
  882. error = regmap_update_bits(ddata->reg, CPCAP_REG_CCC1,
  883. 0xffff,
  884. CPCAP_REG_CCC1_CAL_EN);
  885. if (error)
  886. goto restore;
  887. while (time_before(jiffies, timeout)) {
  888. error = regmap_read(ddata->reg, CPCAP_REG_CCC1, &value);
  889. if (error)
  890. goto restore;
  891. if (!(value & CPCAP_REG_CCC1_CAL_EN))
  892. break;
  893. error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
  894. if (error)
  895. goto restore;
  896. msleep(300);
  897. }
  898. /* Read calibration offset from CCM */
  899. error = regmap_read(ddata->reg, CPCAP_REG_CCM, &value);
  900. if (error)
  901. goto restore;
  902. dev_info(ddata->dev, "calibration done: 0x%04x\n", value);
  903. restore:
  904. if (error)
  905. dev_err(ddata->dev, "%s: error %i\n", __func__, error);
  906. error = regmap_update_bits(ddata->reg, CPCAP_REG_CCC1,
  907. 0xffff, ccc1);
  908. if (error)
  909. dev_err(ddata->dev, "%s: restore error %i\n",
  910. __func__, error);
  911. return error;
  912. }
  913. #ifdef CONFIG_OF
  914. static const struct of_device_id cpcap_battery_id_table[] = {
  915. {
  916. .compatible = "motorola,cpcap-battery",
  917. },
  918. {},
  919. };
  920. MODULE_DEVICE_TABLE(of, cpcap_battery_id_table);
  921. #endif
  922. static const struct power_supply_desc cpcap_charger_battery_desc = {
  923. .name = "battery",
  924. .type = POWER_SUPPLY_TYPE_BATTERY,
  925. .properties = cpcap_battery_props,
  926. .num_properties = ARRAY_SIZE(cpcap_battery_props),
  927. .get_property = cpcap_battery_get_property,
  928. .set_property = cpcap_battery_set_property,
  929. .property_is_writeable = cpcap_battery_property_is_writeable,
  930. .external_power_changed = cpcap_battery_external_power_changed,
  931. };
  932. static int cpcap_battery_probe(struct platform_device *pdev)
  933. {
  934. struct cpcap_battery_ddata *ddata;
  935. struct power_supply_config psy_cfg = {};
  936. int error;
  937. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  938. if (!ddata)
  939. return -ENOMEM;
  940. cpcap_battery_detect_battery_type(ddata);
  941. INIT_LIST_HEAD(&ddata->irq_list);
  942. ddata->dev = &pdev->dev;
  943. ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
  944. if (!ddata->reg)
  945. return -ENODEV;
  946. error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor);
  947. if (error)
  948. return error;
  949. switch (ddata->vendor) {
  950. case CPCAP_VENDOR_ST:
  951. ddata->cc_lsb = 95374; /* μAms per LSB */
  952. break;
  953. case CPCAP_VENDOR_TI:
  954. ddata->cc_lsb = 91501; /* μAms per LSB */
  955. break;
  956. default:
  957. return -EINVAL;
  958. }
  959. ddata->cc_lsb = (ddata->cc_lsb * ddata->config.cd_factor) / 1000;
  960. platform_set_drvdata(pdev, ddata);
  961. error = cpcap_battery_init_interrupts(pdev, ddata);
  962. if (error)
  963. return error;
  964. error = cpcap_battery_init_iio(ddata);
  965. if (error)
  966. return error;
  967. psy_cfg.of_node = pdev->dev.of_node;
  968. psy_cfg.drv_data = ddata;
  969. ddata->psy = devm_power_supply_register(ddata->dev,
  970. &cpcap_charger_battery_desc,
  971. &psy_cfg);
  972. error = PTR_ERR_OR_ZERO(ddata->psy);
  973. if (error) {
  974. dev_err(ddata->dev, "failed to register power supply\n");
  975. return error;
  976. }
  977. atomic_set(&ddata->active, 1);
  978. error = cpcap_battery_calibrate(ddata);
  979. if (error)
  980. return error;
  981. return 0;
  982. }
  983. static int cpcap_battery_remove(struct platform_device *pdev)
  984. {
  985. struct cpcap_battery_ddata *ddata = platform_get_drvdata(pdev);
  986. int error;
  987. atomic_set(&ddata->active, 0);
  988. error = regmap_update_bits(ddata->reg, CPCAP_REG_BPEOL,
  989. 0xffff, 0);
  990. if (error)
  991. dev_err(&pdev->dev, "could not disable: %i\n", error);
  992. return 0;
  993. }
  994. static struct platform_driver cpcap_battery_driver = {
  995. .driver = {
  996. .name = "cpcap_battery",
  997. .of_match_table = of_match_ptr(cpcap_battery_id_table),
  998. },
  999. .probe = cpcap_battery_probe,
  1000. .remove = cpcap_battery_remove,
  1001. };
  1002. module_platform_driver(cpcap_battery_driver);
  1003. MODULE_LICENSE("GPL v2");
  1004. MODULE_AUTHOR("Tony Lindgren <[email protected]>");
  1005. MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");