cpcap-battery.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Battery driver for CPCAP PMIC
  4. *
  5. * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
  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.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 void 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. }
  993. static struct platform_driver cpcap_battery_driver = {
  994. .driver = {
  995. .name = "cpcap_battery",
  996. .of_match_table = of_match_ptr(cpcap_battery_id_table),
  997. },
  998. .probe = cpcap_battery_probe,
  999. .remove_new = cpcap_battery_remove,
  1000. };
  1001. module_platform_driver(cpcap_battery_driver);
  1002. MODULE_LICENSE("GPL v2");
  1003. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  1004. MODULE_DESCRIPTION("CPCAP PMIC Battery Driver");