cpcap-battery.c 25 KB

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