bd99954-charger.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ROHM BD99954 charger driver
  4. *
  5. * Copyright (C) 2020 Rohm Semiconductors
  6. * Originally written by:
  7. * Mikko Mutanen <[email protected]>
  8. * Markus Laine <[email protected]>
  9. * Bugs added by:
  10. * Matti Vaittinen <[email protected]>
  11. */
  12. /*
  13. * The battery charging profile of BD99954.
  14. *
  15. * Curve (1) represents charging current.
  16. * Curve (2) represents battery voltage.
  17. *
  18. * The BD99954 data sheet divides charging to three phases.
  19. * a) Trickle-charge with constant current (8).
  20. * b) pre-charge with constant current (6)
  21. * c) fast-charge, first with constant current (5) phase. After
  22. * the battery voltage has reached target level (4) we have constant
  23. * voltage phase until charging current has dropped to termination
  24. * level (7)
  25. *
  26. * V ^ ^ I
  27. * . .
  28. * . .
  29. *(4)` `.` ` ` ` ` ` ` ` ` ` ` ` ` ` ----------------------------.
  30. * . :/ .
  31. * . o----+/:/ ` ` ` ` ` ` ` ` ` ` ` ` `.` ` (5)
  32. * . + :: + .
  33. * . + /- -- .
  34. * . +`/- + .
  35. * . o/- -: .
  36. * . .s. +` .
  37. * . .--+ `/ .
  38. * . ..`` + .: .
  39. * . -` + -- .
  40. * . (2) ...`` + :- .
  41. * . ...`` + -: .
  42. *(3)` `.`."" ` ` ` `+-------- ` ` ` ` ` ` `.:` ` ` ` ` ` ` ` ` .` ` (6)
  43. * . + `:. .
  44. * . + -: .
  45. * . + -:. .
  46. * . + .--. .
  47. * . (1) + `.+` ` ` `.` ` (7)
  48. * -..............` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` + ` ` ` .` ` (8)
  49. * . + -
  50. * -------------------------------------------------+++++++++-->
  51. * | trickle | pre | fast |
  52. *
  53. * Details of DT properties for different limits can be found from BD99954
  54. * device tree binding documentation.
  55. */
  56. #include <linux/delay.h>
  57. #include <linux/gpio/consumer.h>
  58. #include <linux/interrupt.h>
  59. #include <linux/i2c.h>
  60. #include <linux/kernel.h>
  61. #include <linux/linear_range.h>
  62. #include <linux/module.h>
  63. #include <linux/mod_devicetable.h>
  64. #include <linux/power_supply.h>
  65. #include <linux/property.h>
  66. #include <linux/regmap.h>
  67. #include <linux/types.h>
  68. #include "bd99954-charger.h"
  69. struct battery_data {
  70. u16 precharge_current; /* Trickle-charge Current */
  71. u16 fc_reg_voltage; /* Fast Charging Regulation Voltage */
  72. u16 voltage_min;
  73. u16 voltage_max;
  74. };
  75. /* Initial field values, converted to initial register values */
  76. struct bd9995x_init_data {
  77. u16 vsysreg_set; /* VSYS Regulation Setting */
  78. u16 ibus_lim_set; /* VBUS input current limitation */
  79. u16 icc_lim_set; /* VCC/VACP Input Current Limit Setting */
  80. u16 itrich_set; /* Trickle-charge Current Setting */
  81. u16 iprech_set; /* Pre-Charge Current Setting */
  82. u16 ichg_set; /* Fast-Charge constant current */
  83. u16 vfastchg_reg_set1; /* Fast Charging Regulation Voltage */
  84. u16 vprechg_th_set; /* Pre-charge Voltage Threshold Setting */
  85. u16 vrechg_set; /* Re-charge Battery Voltage Setting */
  86. u16 vbatovp_set; /* Battery Over Voltage Threshold Setting */
  87. u16 iterm_set; /* Charging termination current */
  88. };
  89. struct bd9995x_state {
  90. u8 online;
  91. u16 chgstm_status;
  92. u16 vbat_vsys_status;
  93. u16 vbus_vcc_status;
  94. };
  95. struct bd9995x_device {
  96. struct i2c_client *client;
  97. struct device *dev;
  98. struct power_supply *charger;
  99. struct regmap *rmap;
  100. struct regmap_field *rmap_fields[F_MAX_FIELDS];
  101. int chip_id;
  102. int chip_rev;
  103. struct bd9995x_init_data init_data;
  104. struct bd9995x_state state;
  105. struct mutex lock; /* Protect state data */
  106. };
  107. static const struct regmap_range bd9995x_readonly_reg_ranges[] = {
  108. regmap_reg_range(CHGSTM_STATUS, SEL_ILIM_VAL),
  109. regmap_reg_range(IOUT_DACIN_VAL, IOUT_DACIN_VAL),
  110. regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
  111. regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
  112. regmap_reg_range(CHIP_ID, CHIP_REV),
  113. regmap_reg_range(SYSTEM_STATUS, SYSTEM_STATUS),
  114. regmap_reg_range(IBATP_VAL, VBAT_AVE_VAL),
  115. regmap_reg_range(VTH_VAL, EXTIADP_AVE_VAL),
  116. };
  117. static const struct regmap_access_table bd9995x_writeable_regs = {
  118. .no_ranges = bd9995x_readonly_reg_ranges,
  119. .n_no_ranges = ARRAY_SIZE(bd9995x_readonly_reg_ranges),
  120. };
  121. static const struct regmap_range bd9995x_volatile_reg_ranges[] = {
  122. regmap_reg_range(CHGSTM_STATUS, WDT_STATUS),
  123. regmap_reg_range(VCC_UCD_STATUS, VCC_IDD_STATUS),
  124. regmap_reg_range(VBUS_UCD_STATUS, VBUS_IDD_STATUS),
  125. regmap_reg_range(INT0_STATUS, INT7_STATUS),
  126. regmap_reg_range(SYSTEM_STATUS, SYSTEM_CTRL_SET),
  127. regmap_reg_range(IBATP_VAL, EXTIADP_AVE_VAL), /* Measurement regs */
  128. };
  129. static const struct regmap_access_table bd9995x_volatile_regs = {
  130. .yes_ranges = bd9995x_volatile_reg_ranges,
  131. .n_yes_ranges = ARRAY_SIZE(bd9995x_volatile_reg_ranges),
  132. };
  133. static const struct regmap_range_cfg regmap_range_cfg[] = {
  134. {
  135. .selector_reg = MAP_SET,
  136. .selector_mask = 0xFFFF,
  137. .selector_shift = 0,
  138. .window_start = 0,
  139. .window_len = 0x100,
  140. .range_min = 0 * 0x100,
  141. .range_max = 3 * 0x100,
  142. },
  143. };
  144. static const struct regmap_config bd9995x_regmap_config = {
  145. .reg_bits = 8,
  146. .val_bits = 16,
  147. .reg_stride = 1,
  148. .max_register = 3 * 0x100,
  149. .cache_type = REGCACHE_RBTREE,
  150. .ranges = regmap_range_cfg,
  151. .num_ranges = ARRAY_SIZE(regmap_range_cfg),
  152. .val_format_endian = REGMAP_ENDIAN_LITTLE,
  153. .wr_table = &bd9995x_writeable_regs,
  154. .volatile_table = &bd9995x_volatile_regs,
  155. };
  156. enum bd9995x_chrg_fault {
  157. CHRG_FAULT_NORMAL,
  158. CHRG_FAULT_INPUT,
  159. CHRG_FAULT_THERMAL_SHUTDOWN,
  160. CHRG_FAULT_TIMER_EXPIRED,
  161. };
  162. static int bd9995x_get_prop_batt_health(struct bd9995x_device *bd)
  163. {
  164. int ret, tmp;
  165. ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
  166. if (ret)
  167. return POWER_SUPPLY_HEALTH_UNKNOWN;
  168. /* TODO: Check these against datasheet page 34 */
  169. switch (tmp) {
  170. case ROOM:
  171. return POWER_SUPPLY_HEALTH_GOOD;
  172. case HOT1:
  173. case HOT2:
  174. case HOT3:
  175. return POWER_SUPPLY_HEALTH_OVERHEAT;
  176. case COLD1:
  177. case COLD2:
  178. return POWER_SUPPLY_HEALTH_COLD;
  179. case TEMP_DIS:
  180. case BATT_OPEN:
  181. default:
  182. return POWER_SUPPLY_HEALTH_UNKNOWN;
  183. }
  184. }
  185. static int bd9995x_get_prop_charge_type(struct bd9995x_device *bd)
  186. {
  187. int ret, tmp;
  188. ret = regmap_field_read(bd->rmap_fields[F_CHGSTM_STATE], &tmp);
  189. if (ret)
  190. return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  191. switch (tmp) {
  192. case CHGSTM_TRICKLE_CHARGE:
  193. case CHGSTM_PRE_CHARGE:
  194. return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  195. case CHGSTM_FAST_CHARGE:
  196. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  197. case CHGSTM_TOP_OFF:
  198. case CHGSTM_DONE:
  199. case CHGSTM_SUSPEND:
  200. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  201. default: /* Rest of the states are error related, no charging */
  202. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  203. }
  204. }
  205. static bool bd9995x_get_prop_batt_present(struct bd9995x_device *bd)
  206. {
  207. int ret, tmp;
  208. ret = regmap_field_read(bd->rmap_fields[F_BATTEMP], &tmp);
  209. if (ret)
  210. return false;
  211. return tmp != BATT_OPEN;
  212. }
  213. static int bd9995x_get_prop_batt_voltage(struct bd9995x_device *bd)
  214. {
  215. int ret, tmp;
  216. ret = regmap_field_read(bd->rmap_fields[F_VBAT_VAL], &tmp);
  217. if (ret)
  218. return 0;
  219. tmp = min(tmp, 19200);
  220. return tmp * 1000;
  221. }
  222. static int bd9995x_get_prop_batt_current(struct bd9995x_device *bd)
  223. {
  224. int ret, tmp;
  225. ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
  226. if (ret)
  227. return 0;
  228. return tmp * 1000;
  229. }
  230. #define DEFAULT_BATTERY_TEMPERATURE 250
  231. static int bd9995x_get_prop_batt_temp(struct bd9995x_device *bd)
  232. {
  233. int ret, tmp;
  234. ret = regmap_field_read(bd->rmap_fields[F_THERM_VAL], &tmp);
  235. if (ret)
  236. return DEFAULT_BATTERY_TEMPERATURE;
  237. return (200 - tmp) * 10;
  238. }
  239. static int bd9995x_power_supply_get_property(struct power_supply *psy,
  240. enum power_supply_property psp,
  241. union power_supply_propval *val)
  242. {
  243. int ret, tmp;
  244. struct bd9995x_device *bd = power_supply_get_drvdata(psy);
  245. struct bd9995x_state state;
  246. mutex_lock(&bd->lock);
  247. state = bd->state;
  248. mutex_unlock(&bd->lock);
  249. switch (psp) {
  250. case POWER_SUPPLY_PROP_STATUS:
  251. switch (state.chgstm_status) {
  252. case CHGSTM_TRICKLE_CHARGE:
  253. case CHGSTM_PRE_CHARGE:
  254. case CHGSTM_FAST_CHARGE:
  255. case CHGSTM_TOP_OFF:
  256. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  257. break;
  258. case CHGSTM_DONE:
  259. val->intval = POWER_SUPPLY_STATUS_FULL;
  260. break;
  261. case CHGSTM_SUSPEND:
  262. case CHGSTM_TEMPERATURE_ERROR_1:
  263. case CHGSTM_TEMPERATURE_ERROR_2:
  264. case CHGSTM_TEMPERATURE_ERROR_3:
  265. case CHGSTM_TEMPERATURE_ERROR_4:
  266. case CHGSTM_TEMPERATURE_ERROR_5:
  267. case CHGSTM_TEMPERATURE_ERROR_6:
  268. case CHGSTM_TEMPERATURE_ERROR_7:
  269. case CHGSTM_THERMAL_SHUT_DOWN_1:
  270. case CHGSTM_THERMAL_SHUT_DOWN_2:
  271. case CHGSTM_THERMAL_SHUT_DOWN_3:
  272. case CHGSTM_THERMAL_SHUT_DOWN_4:
  273. case CHGSTM_THERMAL_SHUT_DOWN_5:
  274. case CHGSTM_THERMAL_SHUT_DOWN_6:
  275. case CHGSTM_THERMAL_SHUT_DOWN_7:
  276. case CHGSTM_BATTERY_ERROR:
  277. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  278. break;
  279. default:
  280. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  281. break;
  282. }
  283. break;
  284. case POWER_SUPPLY_PROP_MANUFACTURER:
  285. val->strval = BD9995X_MANUFACTURER;
  286. break;
  287. case POWER_SUPPLY_PROP_ONLINE:
  288. val->intval = state.online;
  289. break;
  290. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  291. ret = regmap_field_read(bd->rmap_fields[F_IBATP_VAL], &tmp);
  292. if (ret)
  293. return ret;
  294. val->intval = tmp * 1000;
  295. break;
  296. case POWER_SUPPLY_PROP_CHARGE_AVG:
  297. ret = regmap_field_read(bd->rmap_fields[F_IBATP_AVE_VAL], &tmp);
  298. if (ret)
  299. return ret;
  300. val->intval = tmp * 1000;
  301. break;
  302. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  303. /*
  304. * Currently the DT uses this property to give the
  305. * target current for fast-charging constant current phase.
  306. * I think it is correct in a sense.
  307. *
  308. * Yet, this prop we read and return here is the programmed
  309. * safety limit for combined input currents. This feels
  310. * also correct in a sense.
  311. *
  312. * However, this results a mismatch to DT value and value
  313. * read from sysfs.
  314. */
  315. ret = regmap_field_read(bd->rmap_fields[F_SEL_ILIM_VAL], &tmp);
  316. if (ret)
  317. return ret;
  318. val->intval = tmp * 1000;
  319. break;
  320. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  321. if (!state.online) {
  322. val->intval = 0;
  323. break;
  324. }
  325. ret = regmap_field_read(bd->rmap_fields[F_VFASTCHG_REG_SET1],
  326. &tmp);
  327. if (ret)
  328. return ret;
  329. /*
  330. * The actual range : 2560 to 19200 mV. No matter what the
  331. * register says
  332. */
  333. val->intval = clamp_val(tmp << 4, 2560, 19200);
  334. val->intval *= 1000;
  335. break;
  336. case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  337. ret = regmap_field_read(bd->rmap_fields[F_ITERM_SET], &tmp);
  338. if (ret)
  339. return ret;
  340. /* Start step is 64 mA */
  341. val->intval = tmp << 6;
  342. /* Maximum is 1024 mA - no matter what register says */
  343. val->intval = min(val->intval, 1024);
  344. val->intval *= 1000;
  345. break;
  346. /* Battery properties which we access through charger */
  347. case POWER_SUPPLY_PROP_PRESENT:
  348. val->intval = bd9995x_get_prop_batt_present(bd);
  349. break;
  350. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  351. val->intval = bd9995x_get_prop_batt_voltage(bd);
  352. break;
  353. case POWER_SUPPLY_PROP_CURRENT_NOW:
  354. val->intval = bd9995x_get_prop_batt_current(bd);
  355. break;
  356. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  357. val->intval = bd9995x_get_prop_charge_type(bd);
  358. break;
  359. case POWER_SUPPLY_PROP_HEALTH:
  360. val->intval = bd9995x_get_prop_batt_health(bd);
  361. break;
  362. case POWER_SUPPLY_PROP_TEMP:
  363. val->intval = bd9995x_get_prop_batt_temp(bd);
  364. break;
  365. case POWER_SUPPLY_PROP_TECHNOLOGY:
  366. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  367. break;
  368. case POWER_SUPPLY_PROP_MODEL_NAME:
  369. val->strval = "bd99954";
  370. break;
  371. default:
  372. return -EINVAL;
  373. }
  374. return 0;
  375. }
  376. static int bd9995x_get_chip_state(struct bd9995x_device *bd,
  377. struct bd9995x_state *state)
  378. {
  379. int i, ret, tmp;
  380. struct {
  381. struct regmap_field *id;
  382. u16 *data;
  383. } state_fields[] = {
  384. {
  385. bd->rmap_fields[F_CHGSTM_STATE], &state->chgstm_status,
  386. }, {
  387. bd->rmap_fields[F_VBAT_VSYS_STATUS],
  388. &state->vbat_vsys_status,
  389. }, {
  390. bd->rmap_fields[F_VBUS_VCC_STATUS],
  391. &state->vbus_vcc_status,
  392. },
  393. };
  394. for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
  395. ret = regmap_field_read(state_fields[i].id, &tmp);
  396. if (ret)
  397. return ret;
  398. *state_fields[i].data = tmp;
  399. }
  400. if (state->vbus_vcc_status & STATUS_VCC_DET ||
  401. state->vbus_vcc_status & STATUS_VBUS_DET)
  402. state->online = 1;
  403. else
  404. state->online = 0;
  405. return 0;
  406. }
  407. static irqreturn_t bd9995x_irq_handler_thread(int irq, void *private)
  408. {
  409. struct bd9995x_device *bd = private;
  410. int ret, status, mask, i;
  411. unsigned long tmp;
  412. struct bd9995x_state state;
  413. /*
  414. * The bd9995x does not seem to generate big amount of interrupts.
  415. * The logic regarding which interrupts can cause relevant
  416. * status changes seem to be pretty complex.
  417. *
  418. * So lets implement really simple and hopefully bullet-proof handler:
  419. * It does not really matter which IRQ we handle, we just go and
  420. * re-read all interesting statuses + give the framework a nudge.
  421. *
  422. * Other option would be building a _complex_ and error prone logic
  423. * trying to decide what could have been changed (resulting this IRQ
  424. * we are now handling). During the normal operation the BD99954 does
  425. * not seem to be generating much of interrupts so benefit from such
  426. * logic would probably be minimal.
  427. */
  428. ret = regmap_read(bd->rmap, INT0_STATUS, &status);
  429. if (ret) {
  430. dev_err(bd->dev, "Failed to read IRQ status\n");
  431. return IRQ_NONE;
  432. }
  433. ret = regmap_field_read(bd->rmap_fields[F_INT0_SET], &mask);
  434. if (ret) {
  435. dev_err(bd->dev, "Failed to read IRQ mask\n");
  436. return IRQ_NONE;
  437. }
  438. /* Handle only IRQs that are not masked */
  439. status &= mask;
  440. tmp = status;
  441. /* Lowest bit does not represent any sub-registers */
  442. tmp >>= 1;
  443. /*
  444. * Mask and ack IRQs we will handle (+ the idiot bit)
  445. */
  446. ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], 0);
  447. if (ret) {
  448. dev_err(bd->dev, "Failed to mask F_INT0\n");
  449. return IRQ_NONE;
  450. }
  451. ret = regmap_write(bd->rmap, INT0_STATUS, status);
  452. if (ret) {
  453. dev_err(bd->dev, "Failed to ack F_INT0\n");
  454. goto err_umask;
  455. }
  456. for_each_set_bit(i, &tmp, 7) {
  457. int sub_status, sub_mask;
  458. int sub_status_reg[] = {
  459. INT1_STATUS, INT2_STATUS, INT3_STATUS, INT4_STATUS,
  460. INT5_STATUS, INT6_STATUS, INT7_STATUS,
  461. };
  462. struct regmap_field *sub_mask_f[] = {
  463. bd->rmap_fields[F_INT1_SET],
  464. bd->rmap_fields[F_INT2_SET],
  465. bd->rmap_fields[F_INT3_SET],
  466. bd->rmap_fields[F_INT4_SET],
  467. bd->rmap_fields[F_INT5_SET],
  468. bd->rmap_fields[F_INT6_SET],
  469. bd->rmap_fields[F_INT7_SET],
  470. };
  471. /* Clear sub IRQs */
  472. ret = regmap_read(bd->rmap, sub_status_reg[i], &sub_status);
  473. if (ret) {
  474. dev_err(bd->dev, "Failed to read IRQ sub-status\n");
  475. goto err_umask;
  476. }
  477. ret = regmap_field_read(sub_mask_f[i], &sub_mask);
  478. if (ret) {
  479. dev_err(bd->dev, "Failed to read IRQ sub-mask\n");
  480. goto err_umask;
  481. }
  482. /* Ack active sub-statuses */
  483. sub_status &= sub_mask;
  484. ret = regmap_write(bd->rmap, sub_status_reg[i], sub_status);
  485. if (ret) {
  486. dev_err(bd->dev, "Failed to ack sub-IRQ\n");
  487. goto err_umask;
  488. }
  489. }
  490. ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
  491. if (ret)
  492. /* May as well retry once */
  493. goto err_umask;
  494. /* Read whole chip state */
  495. ret = bd9995x_get_chip_state(bd, &state);
  496. if (ret < 0) {
  497. dev_err(bd->dev, "Failed to read chip state\n");
  498. } else {
  499. mutex_lock(&bd->lock);
  500. bd->state = state;
  501. mutex_unlock(&bd->lock);
  502. power_supply_changed(bd->charger);
  503. }
  504. return IRQ_HANDLED;
  505. err_umask:
  506. ret = regmap_field_write(bd->rmap_fields[F_INT0_SET], mask);
  507. if (ret)
  508. dev_err(bd->dev,
  509. "Failed to un-mask F_INT0 - IRQ permanently disabled\n");
  510. return IRQ_NONE;
  511. }
  512. static int __bd9995x_chip_reset(struct bd9995x_device *bd)
  513. {
  514. int ret, state;
  515. int rst_check_counter = 10;
  516. u16 tmp = ALLRST | OTPLD;
  517. ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
  518. if (ret < 0)
  519. return ret;
  520. do {
  521. ret = regmap_field_read(bd->rmap_fields[F_OTPLD_STATE], &state);
  522. if (ret)
  523. return ret;
  524. msleep(10);
  525. } while (state == 0 && --rst_check_counter);
  526. if (!rst_check_counter) {
  527. dev_err(bd->dev, "chip reset not completed\n");
  528. return -ETIMEDOUT;
  529. }
  530. tmp = 0;
  531. ret = regmap_raw_write(bd->rmap, SYSTEM_CTRL_SET, &tmp, 2);
  532. return ret;
  533. }
  534. static int bd9995x_hw_init(struct bd9995x_device *bd)
  535. {
  536. int ret;
  537. int i;
  538. struct bd9995x_state state;
  539. struct bd9995x_init_data *id = &bd->init_data;
  540. const struct {
  541. enum bd9995x_fields id;
  542. u16 value;
  543. } init_data[] = {
  544. /* Enable the charging trigger after SDP charger attached */
  545. {F_SDP_CHG_TRIG_EN, 1},
  546. /* Enable charging trigger after SDP charger attached */
  547. {F_SDP_CHG_TRIG, 1},
  548. /* Disable charging trigger by BC1.2 detection */
  549. {F_VBUS_BC_DISEN, 1},
  550. /* Disable charging trigger by BC1.2 detection */
  551. {F_VCC_BC_DISEN, 1},
  552. /* Disable automatic limitation of the input current */
  553. {F_ILIM_AUTO_DISEN, 1},
  554. /* Select current limitation when SDP charger attached*/
  555. {F_SDP_500_SEL, 1},
  556. /* Select current limitation when DCP charger attached */
  557. {F_DCP_2500_SEL, 1},
  558. {F_VSYSREG_SET, id->vsysreg_set},
  559. /* Activate USB charging and DC/DC converter */
  560. {F_USB_SUS, 0},
  561. /* DCDC clock: 1200 kHz*/
  562. {F_DCDC_CLK_SEL, 3},
  563. /* Enable charging */
  564. {F_CHG_EN, 1},
  565. /* Disable Input current Limit setting voltage measurement */
  566. {F_EXTIADPEN, 0},
  567. /* Disable input current limiting */
  568. {F_VSYS_PRIORITY, 1},
  569. {F_IBUS_LIM_SET, id->ibus_lim_set},
  570. {F_ICC_LIM_SET, id->icc_lim_set},
  571. /* Charge Termination Current Setting to 0*/
  572. {F_ITERM_SET, id->iterm_set},
  573. /* Trickle-charge Current Setting */
  574. {F_ITRICH_SET, id->itrich_set},
  575. /* Pre-charge Current setting */
  576. {F_IPRECH_SET, id->iprech_set},
  577. /* Fast Charge Current for constant current phase */
  578. {F_ICHG_SET, id->ichg_set},
  579. /* Fast Charge Voltage Regulation Setting */
  580. {F_VFASTCHG_REG_SET1, id->vfastchg_reg_set1},
  581. /* Set Pre-charge Voltage Threshold for trickle charging. */
  582. {F_VPRECHG_TH_SET, id->vprechg_th_set},
  583. {F_VRECHG_SET, id->vrechg_set},
  584. {F_VBATOVP_SET, id->vbatovp_set},
  585. /* Reverse buck boost voltage Setting */
  586. {F_VRBOOST_SET, 0},
  587. /* Disable fast-charging watchdog */
  588. {F_WDT_FST, 0},
  589. /* Disable pre-charging watchdog */
  590. {F_WDT_PRE, 0},
  591. /* Power save off */
  592. {F_POWER_SAVE_MODE, 0},
  593. {F_INT1_SET, INT1_ALL},
  594. {F_INT2_SET, INT2_ALL},
  595. {F_INT3_SET, INT3_ALL},
  596. {F_INT4_SET, INT4_ALL},
  597. {F_INT5_SET, INT5_ALL},
  598. {F_INT6_SET, INT6_ALL},
  599. {F_INT7_SET, INT7_ALL},
  600. };
  601. /*
  602. * Currently we initialize charger to a known state at startup.
  603. * If we want to allow for example the boot code to initialize
  604. * charger we should get rid of this.
  605. */
  606. ret = __bd9995x_chip_reset(bd);
  607. if (ret < 0)
  608. return ret;
  609. /* Initialize currents/voltages and other parameters */
  610. for (i = 0; i < ARRAY_SIZE(init_data); i++) {
  611. ret = regmap_field_write(bd->rmap_fields[init_data[i].id],
  612. init_data[i].value);
  613. if (ret) {
  614. dev_err(bd->dev, "failed to initialize charger (%d)\n",
  615. ret);
  616. return ret;
  617. }
  618. }
  619. ret = bd9995x_get_chip_state(bd, &state);
  620. if (ret < 0)
  621. return ret;
  622. mutex_lock(&bd->lock);
  623. bd->state = state;
  624. mutex_unlock(&bd->lock);
  625. return 0;
  626. }
  627. static enum power_supply_property bd9995x_power_supply_props[] = {
  628. POWER_SUPPLY_PROP_MANUFACTURER,
  629. POWER_SUPPLY_PROP_STATUS,
  630. POWER_SUPPLY_PROP_ONLINE,
  631. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  632. POWER_SUPPLY_PROP_CHARGE_AVG,
  633. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  634. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  635. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  636. /* Battery props we access through charger */
  637. POWER_SUPPLY_PROP_PRESENT,
  638. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  639. POWER_SUPPLY_PROP_CURRENT_NOW,
  640. POWER_SUPPLY_PROP_CHARGE_TYPE,
  641. POWER_SUPPLY_PROP_HEALTH,
  642. POWER_SUPPLY_PROP_TEMP,
  643. POWER_SUPPLY_PROP_TECHNOLOGY,
  644. POWER_SUPPLY_PROP_MODEL_NAME,
  645. };
  646. static const struct power_supply_desc bd9995x_power_supply_desc = {
  647. .name = "bd9995x-charger",
  648. .type = POWER_SUPPLY_TYPE_USB,
  649. .properties = bd9995x_power_supply_props,
  650. .num_properties = ARRAY_SIZE(bd9995x_power_supply_props),
  651. .get_property = bd9995x_power_supply_get_property,
  652. };
  653. /*
  654. * Limit configurations for vbus-input-current and vcc-vacp-input-current
  655. * Minimum limit is 0 uA. Max is 511 * 32000 uA = 16352000 uA. This is
  656. * configured by writing a register so that each increment in register
  657. * value equals to 32000 uA limit increment.
  658. *
  659. * Eg, value 0x0 is limit 0, value 0x1 is limit 32000, ...
  660. * Describe the setting in linear_range table.
  661. */
  662. static const struct linear_range input_current_limit_ranges[] = {
  663. {
  664. .min = 0,
  665. .step = 32000,
  666. .min_sel = 0x0,
  667. .max_sel = 0x1ff,
  668. },
  669. };
  670. /* Possible trickle, pre-charging and termination current values */
  671. static const struct linear_range charging_current_ranges[] = {
  672. {
  673. .min = 0,
  674. .step = 64000,
  675. .min_sel = 0x0,
  676. .max_sel = 0x10,
  677. }, {
  678. .min = 1024000,
  679. .step = 0,
  680. .min_sel = 0x11,
  681. .max_sel = 0x1f,
  682. },
  683. };
  684. /*
  685. * Fast charging voltage regulation, starting re-charging limit
  686. * and battery over voltage protection have same possible values
  687. */
  688. static const struct linear_range charge_voltage_regulation_ranges[] = {
  689. {
  690. .min = 2560000,
  691. .step = 0,
  692. .min_sel = 0,
  693. .max_sel = 0xA0,
  694. }, {
  695. .min = 2560000,
  696. .step = 16000,
  697. .min_sel = 0xA0,
  698. .max_sel = 0x4B0,
  699. }, {
  700. .min = 19200000,
  701. .step = 0,
  702. .min_sel = 0x4B0,
  703. .max_sel = 0x7FF,
  704. },
  705. };
  706. /* Possible VSYS voltage regulation values */
  707. static const struct linear_range vsys_voltage_regulation_ranges[] = {
  708. {
  709. .min = 2560000,
  710. .step = 0,
  711. .min_sel = 0,
  712. .max_sel = 0x28,
  713. }, {
  714. .min = 2560000,
  715. .step = 64000,
  716. .min_sel = 0x28,
  717. .max_sel = 0x12C,
  718. }, {
  719. .min = 19200000,
  720. .step = 0,
  721. .min_sel = 0x12C,
  722. .max_sel = 0x1FF,
  723. },
  724. };
  725. /* Possible settings for switching from trickle to pre-charging limits */
  726. static const struct linear_range trickle_to_pre_threshold_ranges[] = {
  727. {
  728. .min = 2048000,
  729. .step = 0,
  730. .min_sel = 0,
  731. .max_sel = 0x20,
  732. }, {
  733. .min = 2048000,
  734. .step = 64000,
  735. .min_sel = 0x20,
  736. .max_sel = 0x12C,
  737. }, {
  738. .min = 19200000,
  739. .step = 0,
  740. .min_sel = 0x12C,
  741. .max_sel = 0x1FF
  742. }
  743. };
  744. /* Possible current values for fast-charging constant current phase */
  745. static const struct linear_range fast_charge_current_ranges[] = {
  746. {
  747. .min = 0,
  748. .step = 64000,
  749. .min_sel = 0,
  750. .max_sel = 0xFF,
  751. }
  752. };
  753. struct battery_init {
  754. const char *name;
  755. int *info_data;
  756. const struct linear_range *range;
  757. int ranges;
  758. u16 *data;
  759. };
  760. struct dt_init {
  761. char *prop;
  762. const struct linear_range *range;
  763. int ranges;
  764. u16 *data;
  765. };
  766. static int bd9995x_fw_probe(struct bd9995x_device *bd)
  767. {
  768. int ret;
  769. struct power_supply_battery_info *info;
  770. u32 property;
  771. int i;
  772. int regval;
  773. bool found;
  774. struct bd9995x_init_data *init = &bd->init_data;
  775. struct battery_init battery_inits[] = {
  776. {
  777. .name = "trickle-charging current",
  778. .range = &charging_current_ranges[0],
  779. .ranges = 2,
  780. .data = &init->itrich_set,
  781. }, {
  782. .name = "pre-charging current",
  783. .range = &charging_current_ranges[0],
  784. .ranges = 2,
  785. .data = &init->iprech_set,
  786. }, {
  787. .name = "pre-to-trickle charge voltage threshold",
  788. .range = &trickle_to_pre_threshold_ranges[0],
  789. .ranges = 2,
  790. .data = &init->vprechg_th_set,
  791. }, {
  792. .name = "charging termination current",
  793. .range = &charging_current_ranges[0],
  794. .ranges = 2,
  795. .data = &init->iterm_set,
  796. }, {
  797. .name = "charging re-start voltage",
  798. .range = &charge_voltage_regulation_ranges[0],
  799. .ranges = 2,
  800. .data = &init->vrechg_set,
  801. }, {
  802. .name = "battery overvoltage limit",
  803. .range = &charge_voltage_regulation_ranges[0],
  804. .ranges = 2,
  805. .data = &init->vbatovp_set,
  806. }, {
  807. .name = "fast-charging max current",
  808. .range = &fast_charge_current_ranges[0],
  809. .ranges = 1,
  810. .data = &init->ichg_set,
  811. }, {
  812. .name = "fast-charging voltage",
  813. .range = &charge_voltage_regulation_ranges[0],
  814. .ranges = 2,
  815. .data = &init->vfastchg_reg_set1,
  816. },
  817. };
  818. struct dt_init props[] = {
  819. {
  820. .prop = "rohm,vsys-regulation-microvolt",
  821. .range = &vsys_voltage_regulation_ranges[0],
  822. .ranges = 2,
  823. .data = &init->vsysreg_set,
  824. }, {
  825. .prop = "rohm,vbus-input-current-limit-microamp",
  826. .range = &input_current_limit_ranges[0],
  827. .ranges = 1,
  828. .data = &init->ibus_lim_set,
  829. }, {
  830. .prop = "rohm,vcc-input-current-limit-microamp",
  831. .range = &input_current_limit_ranges[0],
  832. .ranges = 1,
  833. .data = &init->icc_lim_set,
  834. },
  835. };
  836. /*
  837. * The power_supply_get_battery_info() does not support getting values
  838. * from ACPI. Let's fix it if ACPI is required here.
  839. */
  840. ret = power_supply_get_battery_info(bd->charger, &info);
  841. if (ret < 0)
  842. return ret;
  843. /* Put pointers to the generic battery info */
  844. battery_inits[0].info_data = &info->tricklecharge_current_ua;
  845. battery_inits[1].info_data = &info->precharge_current_ua;
  846. battery_inits[2].info_data = &info->precharge_voltage_max_uv;
  847. battery_inits[3].info_data = &info->charge_term_current_ua;
  848. battery_inits[4].info_data = &info->charge_restart_voltage_uv;
  849. battery_inits[5].info_data = &info->overvoltage_limit_uv;
  850. battery_inits[6].info_data = &info->constant_charge_current_max_ua;
  851. battery_inits[7].info_data = &info->constant_charge_voltage_max_uv;
  852. for (i = 0; i < ARRAY_SIZE(battery_inits); i++) {
  853. int val = *battery_inits[i].info_data;
  854. const struct linear_range *range = battery_inits[i].range;
  855. int ranges = battery_inits[i].ranges;
  856. if (val == -EINVAL)
  857. continue;
  858. ret = linear_range_get_selector_low_array(range, ranges, val,
  859. &regval, &found);
  860. if (ret) {
  861. dev_err(bd->dev, "Unsupported value for %s\n",
  862. battery_inits[i].name);
  863. power_supply_put_battery_info(bd->charger, info);
  864. return -EINVAL;
  865. }
  866. if (!found) {
  867. dev_warn(bd->dev,
  868. "Unsupported value for %s - using smaller\n",
  869. battery_inits[i].name);
  870. }
  871. *(battery_inits[i].data) = regval;
  872. }
  873. power_supply_put_battery_info(bd->charger, info);
  874. for (i = 0; i < ARRAY_SIZE(props); i++) {
  875. ret = device_property_read_u32(bd->dev, props[i].prop,
  876. &property);
  877. if (ret < 0) {
  878. dev_err(bd->dev, "failed to read %s", props[i].prop);
  879. return ret;
  880. }
  881. ret = linear_range_get_selector_low_array(props[i].range,
  882. props[i].ranges,
  883. property, &regval,
  884. &found);
  885. if (ret) {
  886. dev_err(bd->dev, "Unsupported value for '%s'\n",
  887. props[i].prop);
  888. return -EINVAL;
  889. }
  890. if (!found) {
  891. dev_warn(bd->dev,
  892. "Unsupported value for '%s' - using smaller\n",
  893. props[i].prop);
  894. }
  895. *(props[i].data) = regval;
  896. }
  897. return 0;
  898. }
  899. static void bd9995x_chip_reset(void *bd)
  900. {
  901. __bd9995x_chip_reset(bd);
  902. }
  903. static int bd9995x_probe(struct i2c_client *client)
  904. {
  905. struct device *dev = &client->dev;
  906. struct bd9995x_device *bd;
  907. struct power_supply_config psy_cfg = {};
  908. int ret;
  909. int i;
  910. bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL);
  911. if (!bd)
  912. return -ENOMEM;
  913. bd->client = client;
  914. bd->dev = dev;
  915. psy_cfg.drv_data = bd;
  916. psy_cfg.of_node = dev->of_node;
  917. mutex_init(&bd->lock);
  918. bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config);
  919. if (IS_ERR(bd->rmap)) {
  920. dev_err(dev, "Failed to setup register access via i2c\n");
  921. return PTR_ERR(bd->rmap);
  922. }
  923. for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) {
  924. const struct reg_field *reg_fields = bd9995x_reg_fields;
  925. bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap,
  926. reg_fields[i]);
  927. if (IS_ERR(bd->rmap_fields[i])) {
  928. dev_err(dev, "cannot allocate regmap field\n");
  929. return PTR_ERR(bd->rmap_fields[i]);
  930. }
  931. }
  932. i2c_set_clientdata(client, bd);
  933. ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id);
  934. if (ret) {
  935. dev_err(dev, "Cannot read chip ID.\n");
  936. return ret;
  937. }
  938. if (bd->chip_id != BD99954_ID) {
  939. dev_err(dev, "Chip with ID=0x%x, not supported!\n",
  940. bd->chip_id);
  941. return -ENODEV;
  942. }
  943. ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev);
  944. if (ret) {
  945. dev_err(dev, "Cannot read revision.\n");
  946. return ret;
  947. }
  948. dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev);
  949. /*
  950. * We need to init the psy before we can call
  951. * power_supply_get_battery_info() for it
  952. */
  953. bd->charger = devm_power_supply_register(bd->dev,
  954. &bd9995x_power_supply_desc,
  955. &psy_cfg);
  956. if (IS_ERR(bd->charger)) {
  957. dev_err(dev, "Failed to register power supply\n");
  958. return PTR_ERR(bd->charger);
  959. }
  960. ret = bd9995x_fw_probe(bd);
  961. if (ret < 0) {
  962. dev_err(dev, "Cannot read device properties.\n");
  963. return ret;
  964. }
  965. ret = bd9995x_hw_init(bd);
  966. if (ret < 0) {
  967. dev_err(dev, "Cannot initialize the chip.\n");
  968. return ret;
  969. }
  970. ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd);
  971. if (ret)
  972. return ret;
  973. return devm_request_threaded_irq(dev, client->irq, NULL,
  974. bd9995x_irq_handler_thread,
  975. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  976. BD9995X_IRQ_PIN, bd);
  977. }
  978. static const struct of_device_id bd9995x_of_match[] = {
  979. { .compatible = "rohm,bd99954", },
  980. { }
  981. };
  982. MODULE_DEVICE_TABLE(of, bd9995x_of_match);
  983. static struct i2c_driver bd9995x_driver = {
  984. .driver = {
  985. .name = "bd9995x-charger",
  986. .of_match_table = bd9995x_of_match,
  987. },
  988. .probe_new = bd9995x_probe,
  989. };
  990. module_i2c_driver(bd9995x_driver);
  991. MODULE_AUTHOR("Laine Markus <[email protected]>");
  992. MODULE_DESCRIPTION("ROHM BD99954 charger driver");
  993. MODULE_LICENSE("GPL");