bd99954-charger.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 <mikko.mutanen@fi.rohmeurope.com>
  8. * Markus Laine <markus.laine@fi.rohmeurope.com>
  9. * Bugs added by:
  10. * Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
  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. .info_data = &info.tricklecharge_current_ua,
  779. .range = &charging_current_ranges[0],
  780. .ranges = 2,
  781. .data = &init->itrich_set,
  782. }, {
  783. .name = "pre-charging current",
  784. .info_data = &info.precharge_current_ua,
  785. .range = &charging_current_ranges[0],
  786. .ranges = 2,
  787. .data = &init->iprech_set,
  788. }, {
  789. .name = "pre-to-trickle charge voltage threshold",
  790. .info_data = &info.precharge_voltage_max_uv,
  791. .range = &trickle_to_pre_threshold_ranges[0],
  792. .ranges = 2,
  793. .data = &init->vprechg_th_set,
  794. }, {
  795. .name = "charging termination current",
  796. .info_data = &info.charge_term_current_ua,
  797. .range = &charging_current_ranges[0],
  798. .ranges = 2,
  799. .data = &init->iterm_set,
  800. }, {
  801. .name = "charging re-start voltage",
  802. .info_data = &info.charge_restart_voltage_uv,
  803. .range = &charge_voltage_regulation_ranges[0],
  804. .ranges = 2,
  805. .data = &init->vrechg_set,
  806. }, {
  807. .name = "battery overvoltage limit",
  808. .info_data = &info.overvoltage_limit_uv,
  809. .range = &charge_voltage_regulation_ranges[0],
  810. .ranges = 2,
  811. .data = &init->vbatovp_set,
  812. }, {
  813. .name = "fast-charging max current",
  814. .info_data = &info.constant_charge_current_max_ua,
  815. .range = &fast_charge_current_ranges[0],
  816. .ranges = 1,
  817. .data = &init->ichg_set,
  818. }, {
  819. .name = "fast-charging voltage",
  820. .info_data = &info.constant_charge_voltage_max_uv,
  821. .range = &charge_voltage_regulation_ranges[0],
  822. .ranges = 2,
  823. .data = &init->vfastchg_reg_set1,
  824. },
  825. };
  826. struct dt_init props[] = {
  827. {
  828. .prop = "rohm,vsys-regulation-microvolt",
  829. .range = &vsys_voltage_regulation_ranges[0],
  830. .ranges = 2,
  831. .data = &init->vsysreg_set,
  832. }, {
  833. .prop = "rohm,vbus-input-current-limit-microamp",
  834. .range = &input_current_limit_ranges[0],
  835. .ranges = 1,
  836. .data = &init->ibus_lim_set,
  837. }, {
  838. .prop = "rohm,vcc-input-current-limit-microamp",
  839. .range = &input_current_limit_ranges[0],
  840. .ranges = 1,
  841. .data = &init->icc_lim_set,
  842. },
  843. };
  844. /*
  845. * The power_supply_get_battery_info() does not support getting values
  846. * from ACPI. Let's fix it if ACPI is required here.
  847. */
  848. ret = power_supply_get_battery_info(bd->charger, &info);
  849. if (ret < 0)
  850. return ret;
  851. for (i = 0; i < ARRAY_SIZE(battery_inits); i++) {
  852. int val = *battery_inits[i].info_data;
  853. const struct linear_range *range = battery_inits[i].range;
  854. int ranges = battery_inits[i].ranges;
  855. if (val == -EINVAL)
  856. continue;
  857. ret = linear_range_get_selector_low_array(range, ranges, val,
  858. &regval, &found);
  859. if (ret) {
  860. dev_err(bd->dev, "Unsupported value for %s\n",
  861. battery_inits[i].name);
  862. power_supply_put_battery_info(bd->charger, &info);
  863. return -EINVAL;
  864. }
  865. if (!found) {
  866. dev_warn(bd->dev,
  867. "Unsupported value for %s - using smaller\n",
  868. battery_inits[i].name);
  869. }
  870. *(battery_inits[i].data) = regval;
  871. }
  872. power_supply_put_battery_info(bd->charger, &info);
  873. for (i = 0; i < ARRAY_SIZE(props); i++) {
  874. ret = device_property_read_u32(bd->dev, props[i].prop,
  875. &property);
  876. if (ret < 0) {
  877. dev_err(bd->dev, "failed to read %s", props[i].prop);
  878. return ret;
  879. }
  880. ret = linear_range_get_selector_low_array(props[i].range,
  881. props[i].ranges,
  882. property, &regval,
  883. &found);
  884. if (ret) {
  885. dev_err(bd->dev, "Unsupported value for '%s'\n",
  886. props[i].prop);
  887. return -EINVAL;
  888. }
  889. if (!found) {
  890. dev_warn(bd->dev,
  891. "Unsupported value for '%s' - using smaller\n",
  892. props[i].prop);
  893. }
  894. *(props[i].data) = regval;
  895. }
  896. return 0;
  897. }
  898. static void bd9995x_chip_reset(void *bd)
  899. {
  900. __bd9995x_chip_reset(bd);
  901. }
  902. static int bd9995x_probe(struct i2c_client *client)
  903. {
  904. struct device *dev = &client->dev;
  905. struct bd9995x_device *bd;
  906. struct power_supply_config psy_cfg = {};
  907. int ret;
  908. int i;
  909. bd = devm_kzalloc(dev, sizeof(*bd), GFP_KERNEL);
  910. if (!bd)
  911. return -ENOMEM;
  912. bd->client = client;
  913. bd->dev = dev;
  914. psy_cfg.drv_data = bd;
  915. psy_cfg.of_node = dev->of_node;
  916. mutex_init(&bd->lock);
  917. bd->rmap = devm_regmap_init_i2c(client, &bd9995x_regmap_config);
  918. if (IS_ERR(bd->rmap)) {
  919. dev_err(dev, "Failed to setup register access via i2c\n");
  920. return PTR_ERR(bd->rmap);
  921. }
  922. for (i = 0; i < ARRAY_SIZE(bd9995x_reg_fields); i++) {
  923. const struct reg_field *reg_fields = bd9995x_reg_fields;
  924. bd->rmap_fields[i] = devm_regmap_field_alloc(dev, bd->rmap,
  925. reg_fields[i]);
  926. if (IS_ERR(bd->rmap_fields[i])) {
  927. dev_err(dev, "cannot allocate regmap field\n");
  928. return PTR_ERR(bd->rmap_fields[i]);
  929. }
  930. }
  931. i2c_set_clientdata(client, bd);
  932. ret = regmap_field_read(bd->rmap_fields[F_CHIP_ID], &bd->chip_id);
  933. if (ret) {
  934. dev_err(dev, "Cannot read chip ID.\n");
  935. return ret;
  936. }
  937. if (bd->chip_id != BD99954_ID) {
  938. dev_err(dev, "Chip with ID=0x%x, not supported!\n",
  939. bd->chip_id);
  940. return -ENODEV;
  941. }
  942. ret = regmap_field_read(bd->rmap_fields[F_CHIP_REV], &bd->chip_rev);
  943. if (ret) {
  944. dev_err(dev, "Cannot read revision.\n");
  945. return ret;
  946. }
  947. dev_info(bd->dev, "Found BD99954 chip rev %d\n", bd->chip_rev);
  948. /*
  949. * We need to init the psy before we can call
  950. * power_supply_get_battery_info() for it
  951. */
  952. bd->charger = devm_power_supply_register(bd->dev,
  953. &bd9995x_power_supply_desc,
  954. &psy_cfg);
  955. if (IS_ERR(bd->charger)) {
  956. dev_err(dev, "Failed to register power supply\n");
  957. return PTR_ERR(bd->charger);
  958. }
  959. ret = bd9995x_fw_probe(bd);
  960. if (ret < 0) {
  961. dev_err(dev, "Cannot read device properties.\n");
  962. return ret;
  963. }
  964. ret = bd9995x_hw_init(bd);
  965. if (ret < 0) {
  966. dev_err(dev, "Cannot initialize the chip.\n");
  967. return ret;
  968. }
  969. ret = devm_add_action_or_reset(dev, bd9995x_chip_reset, bd);
  970. if (ret)
  971. return ret;
  972. return devm_request_threaded_irq(dev, client->irq, NULL,
  973. bd9995x_irq_handler_thread,
  974. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  975. BD9995X_IRQ_PIN, bd);
  976. }
  977. static const struct of_device_id bd9995x_of_match[] = {
  978. { .compatible = "rohm,bd99954", },
  979. { }
  980. };
  981. MODULE_DEVICE_TABLE(of, bd9995x_of_match);
  982. static struct i2c_driver bd9995x_driver = {
  983. .driver = {
  984. .name = "bd9995x-charger",
  985. .of_match_table = bd9995x_of_match,
  986. },
  987. .probe_new = bd9995x_probe,
  988. };
  989. module_i2c_driver(bd9995x_driver);
  990. MODULE_AUTHOR("Laine Markus <markus.laine@fi.rohmeurope.com>");
  991. MODULE_DESCRIPTION("ROHM BD99954 charger driver");
  992. MODULE_LICENSE("GPL");