bq25890_charger.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * TI BQ25890 charger driver
  3. *
  4. * Copyright (C) 2015 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/regmap.h>
  21. #include <linux/types.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/usb/phy.h>
  26. #include <linux/acpi.h>
  27. #include <linux/of.h>
  28. #define BQ25890_MANUFACTURER "Texas Instruments"
  29. #define BQ25890_IRQ_PIN "bq25890_irq"
  30. #define BQ25890_ID 3
  31. enum bq25890_fields {
  32. F_EN_HIZ, F_EN_ILIM, F_IILIM, /* Reg00 */
  33. F_BHOT, F_BCOLD, F_VINDPM_OFS, /* Reg01 */
  34. F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
  35. F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN, /* Reg02 */
  36. F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN, /* Reg03 */
  37. F_PUMPX_EN, F_ICHG, /* Reg04 */
  38. F_IPRECHG, F_ITERM, /* Reg05 */
  39. F_VREG, F_BATLOWV, F_VRECHG, /* Reg06 */
  40. F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
  41. F_JEITA_ISET, /* Reg07 */
  42. F_BATCMP, F_VCLAMP, F_TREG, /* Reg08 */
  43. F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
  44. F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN, /* Reg09 */
  45. F_BOOSTV, F_BOOSTI, /* Reg0A */
  46. F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_VSYS_STAT, /* Reg0B */
  47. F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
  48. F_NTC_FAULT, /* Reg0C */
  49. F_FORCE_VINDPM, F_VINDPM, /* Reg0D */
  50. F_THERM_STAT, F_BATV, /* Reg0E */
  51. F_SYSV, /* Reg0F */
  52. F_TSPCT, /* Reg10 */
  53. F_VBUS_GD, F_VBUSV, /* Reg11 */
  54. F_ICHGR, /* Reg12 */
  55. F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM, /* Reg13 */
  56. F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV, /* Reg14 */
  57. F_MAX_FIELDS
  58. };
  59. /* initial field values, converted to register values */
  60. struct bq25890_init_data {
  61. u8 ichg; /* charge current */
  62. u8 vreg; /* regulation voltage */
  63. u8 iterm; /* termination current */
  64. u8 iprechg; /* precharge current */
  65. u8 sysvmin; /* minimum system voltage limit */
  66. u8 boostv; /* boost regulation voltage */
  67. u8 boosti; /* boost current limit */
  68. u8 boostf; /* boost frequency */
  69. u8 ilim_en; /* enable ILIM pin */
  70. u8 treg; /* thermal regulation threshold */
  71. };
  72. struct bq25890_state {
  73. u8 online;
  74. u8 chrg_status;
  75. u8 chrg_fault;
  76. u8 vsys_status;
  77. u8 boost_fault;
  78. u8 bat_fault;
  79. };
  80. struct bq25890_device {
  81. struct i2c_client *client;
  82. struct device *dev;
  83. struct power_supply *charger;
  84. struct usb_phy *usb_phy;
  85. struct notifier_block usb_nb;
  86. struct work_struct usb_work;
  87. unsigned long usb_event;
  88. struct regmap *rmap;
  89. struct regmap_field *rmap_fields[F_MAX_FIELDS];
  90. int chip_id;
  91. struct bq25890_init_data init_data;
  92. struct bq25890_state state;
  93. struct mutex lock; /* protect state data */
  94. };
  95. static const struct regmap_range bq25890_readonly_reg_ranges[] = {
  96. regmap_reg_range(0x0b, 0x0c),
  97. regmap_reg_range(0x0e, 0x13),
  98. };
  99. static const struct regmap_access_table bq25890_writeable_regs = {
  100. .no_ranges = bq25890_readonly_reg_ranges,
  101. .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
  102. };
  103. static const struct regmap_range bq25890_volatile_reg_ranges[] = {
  104. regmap_reg_range(0x00, 0x00),
  105. regmap_reg_range(0x09, 0x09),
  106. regmap_reg_range(0x0b, 0x0c),
  107. regmap_reg_range(0x0e, 0x14),
  108. };
  109. static const struct regmap_access_table bq25890_volatile_regs = {
  110. .yes_ranges = bq25890_volatile_reg_ranges,
  111. .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
  112. };
  113. static const struct regmap_config bq25890_regmap_config = {
  114. .reg_bits = 8,
  115. .val_bits = 8,
  116. .max_register = 0x14,
  117. .cache_type = REGCACHE_RBTREE,
  118. .wr_table = &bq25890_writeable_regs,
  119. .volatile_table = &bq25890_volatile_regs,
  120. };
  121. static const struct reg_field bq25890_reg_fields[] = {
  122. /* REG00 */
  123. [F_EN_HIZ] = REG_FIELD(0x00, 7, 7),
  124. [F_EN_ILIM] = REG_FIELD(0x00, 6, 6),
  125. [F_IILIM] = REG_FIELD(0x00, 0, 5),
  126. /* REG01 */
  127. [F_BHOT] = REG_FIELD(0x01, 6, 7),
  128. [F_BCOLD] = REG_FIELD(0x01, 5, 5),
  129. [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4),
  130. /* REG02 */
  131. [F_CONV_START] = REG_FIELD(0x02, 7, 7),
  132. [F_CONV_RATE] = REG_FIELD(0x02, 6, 6),
  133. [F_BOOSTF] = REG_FIELD(0x02, 5, 5),
  134. [F_ICO_EN] = REG_FIELD(0x02, 4, 4),
  135. [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3),
  136. [F_MAXC_EN] = REG_FIELD(0x02, 2, 2),
  137. [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1),
  138. [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0),
  139. /* REG03 */
  140. [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7),
  141. [F_WD_RST] = REG_FIELD(0x03, 6, 6),
  142. [F_OTG_CFG] = REG_FIELD(0x03, 5, 5),
  143. [F_CHG_CFG] = REG_FIELD(0x03, 4, 4),
  144. [F_SYSVMIN] = REG_FIELD(0x03, 1, 3),
  145. /* REG04 */
  146. [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7),
  147. [F_ICHG] = REG_FIELD(0x04, 0, 6),
  148. /* REG05 */
  149. [F_IPRECHG] = REG_FIELD(0x05, 4, 7),
  150. [F_ITERM] = REG_FIELD(0x05, 0, 3),
  151. /* REG06 */
  152. [F_VREG] = REG_FIELD(0x06, 2, 7),
  153. [F_BATLOWV] = REG_FIELD(0x06, 1, 1),
  154. [F_VRECHG] = REG_FIELD(0x06, 0, 0),
  155. /* REG07 */
  156. [F_TERM_EN] = REG_FIELD(0x07, 7, 7),
  157. [F_STAT_DIS] = REG_FIELD(0x07, 6, 6),
  158. [F_WD] = REG_FIELD(0x07, 4, 5),
  159. [F_TMR_EN] = REG_FIELD(0x07, 3, 3),
  160. [F_CHG_TMR] = REG_FIELD(0x07, 1, 2),
  161. [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0),
  162. /* REG08 */
  163. [F_BATCMP] = REG_FIELD(0x08, 6, 7),
  164. [F_VCLAMP] = REG_FIELD(0x08, 2, 4),
  165. [F_TREG] = REG_FIELD(0x08, 0, 1),
  166. /* REG09 */
  167. [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7),
  168. [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6),
  169. [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5),
  170. [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4),
  171. [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3),
  172. [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2),
  173. [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1),
  174. [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0),
  175. /* REG0A */
  176. [F_BOOSTV] = REG_FIELD(0x0A, 4, 7),
  177. [F_BOOSTI] = REG_FIELD(0x0A, 0, 2),
  178. /* REG0B */
  179. [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7),
  180. [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4),
  181. [F_PG_STAT] = REG_FIELD(0x0B, 2, 2),
  182. [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1),
  183. [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0),
  184. /* REG0C */
  185. [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7),
  186. [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6),
  187. [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5),
  188. [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3),
  189. [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2),
  190. /* REG0D */
  191. [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7),
  192. [F_VINDPM] = REG_FIELD(0x0D, 0, 6),
  193. /* REG0E */
  194. [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7),
  195. [F_BATV] = REG_FIELD(0x0E, 0, 6),
  196. /* REG0F */
  197. [F_SYSV] = REG_FIELD(0x0F, 0, 6),
  198. /* REG10 */
  199. [F_TSPCT] = REG_FIELD(0x10, 0, 6),
  200. /* REG11 */
  201. [F_VBUS_GD] = REG_FIELD(0x11, 7, 7),
  202. [F_VBUSV] = REG_FIELD(0x11, 0, 6),
  203. /* REG12 */
  204. [F_ICHGR] = REG_FIELD(0x12, 0, 6),
  205. /* REG13 */
  206. [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7),
  207. [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6),
  208. [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5),
  209. /* REG14 */
  210. [F_REG_RST] = REG_FIELD(0x14, 7, 7),
  211. [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6),
  212. [F_PN] = REG_FIELD(0x14, 3, 5),
  213. [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2),
  214. [F_DEV_REV] = REG_FIELD(0x14, 0, 1)
  215. };
  216. /*
  217. * Most of the val -> idx conversions can be computed, given the minimum,
  218. * maximum and the step between values. For the rest of conversions, we use
  219. * lookup tables.
  220. */
  221. enum bq25890_table_ids {
  222. /* range tables */
  223. TBL_ICHG,
  224. TBL_ITERM,
  225. TBL_IPRECHG,
  226. TBL_VREG,
  227. TBL_BATCMP,
  228. TBL_VCLAMP,
  229. TBL_BOOSTV,
  230. TBL_SYSVMIN,
  231. /* lookup tables */
  232. TBL_TREG,
  233. TBL_BOOSTI,
  234. };
  235. /* Thermal Regulation Threshold lookup table, in degrees Celsius */
  236. static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
  237. #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
  238. /* Boost mode current limit lookup table, in uA */
  239. static const u32 bq25890_boosti_tbl[] = {
  240. 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
  241. };
  242. #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
  243. struct bq25890_range {
  244. u32 min;
  245. u32 max;
  246. u32 step;
  247. };
  248. struct bq25890_lookup {
  249. const u32 *tbl;
  250. u32 size;
  251. };
  252. static const union {
  253. struct bq25890_range rt;
  254. struct bq25890_lookup lt;
  255. } bq25890_tables[] = {
  256. /* range tables */
  257. [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
  258. [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
  259. [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
  260. [TBL_BATCMP] = { .rt = {0, 140, 20} }, /* mOhm */
  261. [TBL_VCLAMP] = { .rt = {0, 224000, 32000} }, /* uV */
  262. [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
  263. [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
  264. /* lookup tables */
  265. [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
  266. [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
  267. };
  268. static int bq25890_field_read(struct bq25890_device *bq,
  269. enum bq25890_fields field_id)
  270. {
  271. int ret;
  272. int val;
  273. ret = regmap_field_read(bq->rmap_fields[field_id], &val);
  274. if (ret < 0)
  275. return ret;
  276. return val;
  277. }
  278. static int bq25890_field_write(struct bq25890_device *bq,
  279. enum bq25890_fields field_id, u8 val)
  280. {
  281. return regmap_field_write(bq->rmap_fields[field_id], val);
  282. }
  283. static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
  284. {
  285. u8 idx;
  286. if (id >= TBL_TREG) {
  287. const u32 *tbl = bq25890_tables[id].lt.tbl;
  288. u32 tbl_size = bq25890_tables[id].lt.size;
  289. for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
  290. ;
  291. } else {
  292. const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
  293. u8 rtbl_size;
  294. rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
  295. for (idx = 1;
  296. idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
  297. idx++)
  298. ;
  299. }
  300. return idx - 1;
  301. }
  302. static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
  303. {
  304. const struct bq25890_range *rtbl;
  305. /* lookup table? */
  306. if (id >= TBL_TREG)
  307. return bq25890_tables[id].lt.tbl[idx];
  308. /* range table */
  309. rtbl = &bq25890_tables[id].rt;
  310. return (rtbl->min + idx * rtbl->step);
  311. }
  312. enum bq25890_status {
  313. STATUS_NOT_CHARGING,
  314. STATUS_PRE_CHARGING,
  315. STATUS_FAST_CHARGING,
  316. STATUS_TERMINATION_DONE,
  317. };
  318. enum bq25890_chrg_fault {
  319. CHRG_FAULT_NORMAL,
  320. CHRG_FAULT_INPUT,
  321. CHRG_FAULT_THERMAL_SHUTDOWN,
  322. CHRG_FAULT_TIMER_EXPIRED,
  323. };
  324. static int bq25890_power_supply_get_property(struct power_supply *psy,
  325. enum power_supply_property psp,
  326. union power_supply_propval *val)
  327. {
  328. int ret;
  329. struct bq25890_device *bq = power_supply_get_drvdata(psy);
  330. struct bq25890_state state;
  331. mutex_lock(&bq->lock);
  332. state = bq->state;
  333. mutex_unlock(&bq->lock);
  334. switch (psp) {
  335. case POWER_SUPPLY_PROP_STATUS:
  336. if (!state.online)
  337. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  338. else if (state.chrg_status == STATUS_NOT_CHARGING)
  339. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  340. else if (state.chrg_status == STATUS_PRE_CHARGING ||
  341. state.chrg_status == STATUS_FAST_CHARGING)
  342. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  343. else if (state.chrg_status == STATUS_TERMINATION_DONE)
  344. val->intval = POWER_SUPPLY_STATUS_FULL;
  345. else
  346. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  347. break;
  348. case POWER_SUPPLY_PROP_MANUFACTURER:
  349. val->strval = BQ25890_MANUFACTURER;
  350. break;
  351. case POWER_SUPPLY_PROP_ONLINE:
  352. val->intval = state.online;
  353. break;
  354. case POWER_SUPPLY_PROP_HEALTH:
  355. if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
  356. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  357. else if (state.bat_fault)
  358. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  359. else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
  360. val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  361. else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
  362. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  363. else
  364. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  365. break;
  366. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  367. ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
  368. if (ret < 0)
  369. return ret;
  370. /* converted_val = ADC_val * 50mA (table 10.3.19) */
  371. val->intval = ret * 50000;
  372. break;
  373. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  374. val->intval = bq25890_tables[TBL_ICHG].rt.max;
  375. break;
  376. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  377. if (!state.online) {
  378. val->intval = 0;
  379. break;
  380. }
  381. ret = bq25890_field_read(bq, F_BATV); /* read measured value */
  382. if (ret < 0)
  383. return ret;
  384. /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
  385. val->intval = 2304000 + ret * 20000;
  386. break;
  387. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  388. val->intval = bq25890_tables[TBL_VREG].rt.max;
  389. break;
  390. case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  391. val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
  392. break;
  393. default:
  394. return -EINVAL;
  395. }
  396. return 0;
  397. }
  398. static int bq25890_get_chip_state(struct bq25890_device *bq,
  399. struct bq25890_state *state)
  400. {
  401. int i, ret;
  402. struct {
  403. enum bq25890_fields id;
  404. u8 *data;
  405. } state_fields[] = {
  406. {F_CHG_STAT, &state->chrg_status},
  407. {F_PG_STAT, &state->online},
  408. {F_VSYS_STAT, &state->vsys_status},
  409. {F_BOOST_FAULT, &state->boost_fault},
  410. {F_BAT_FAULT, &state->bat_fault},
  411. {F_CHG_FAULT, &state->chrg_fault}
  412. };
  413. for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
  414. ret = bq25890_field_read(bq, state_fields[i].id);
  415. if (ret < 0)
  416. return ret;
  417. *state_fields[i].data = ret;
  418. }
  419. dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
  420. state->chrg_status, state->online, state->vsys_status,
  421. state->chrg_fault, state->boost_fault, state->bat_fault);
  422. return 0;
  423. }
  424. static bool bq25890_state_changed(struct bq25890_device *bq,
  425. struct bq25890_state *new_state)
  426. {
  427. struct bq25890_state old_state;
  428. mutex_lock(&bq->lock);
  429. old_state = bq->state;
  430. mutex_unlock(&bq->lock);
  431. return (old_state.chrg_status != new_state->chrg_status ||
  432. old_state.chrg_fault != new_state->chrg_fault ||
  433. old_state.online != new_state->online ||
  434. old_state.bat_fault != new_state->bat_fault ||
  435. old_state.boost_fault != new_state->boost_fault ||
  436. old_state.vsys_status != new_state->vsys_status);
  437. }
  438. static void bq25890_handle_state_change(struct bq25890_device *bq,
  439. struct bq25890_state *new_state)
  440. {
  441. int ret;
  442. struct bq25890_state old_state;
  443. mutex_lock(&bq->lock);
  444. old_state = bq->state;
  445. mutex_unlock(&bq->lock);
  446. if (!new_state->online) { /* power removed */
  447. /* disable ADC */
  448. ret = bq25890_field_write(bq, F_CONV_START, 0);
  449. if (ret < 0)
  450. goto error;
  451. } else if (!old_state.online) { /* power inserted */
  452. /* enable ADC, to have control of charge current/voltage */
  453. ret = bq25890_field_write(bq, F_CONV_START, 1);
  454. if (ret < 0)
  455. goto error;
  456. }
  457. return;
  458. error:
  459. dev_err(bq->dev, "Error communicating with the chip.\n");
  460. }
  461. static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
  462. {
  463. struct bq25890_device *bq = private;
  464. int ret;
  465. struct bq25890_state state;
  466. ret = bq25890_get_chip_state(bq, &state);
  467. if (ret < 0)
  468. goto handled;
  469. if (!bq25890_state_changed(bq, &state))
  470. goto handled;
  471. bq25890_handle_state_change(bq, &state);
  472. mutex_lock(&bq->lock);
  473. bq->state = state;
  474. mutex_unlock(&bq->lock);
  475. power_supply_changed(bq->charger);
  476. handled:
  477. return IRQ_HANDLED;
  478. }
  479. static int bq25890_chip_reset(struct bq25890_device *bq)
  480. {
  481. int ret;
  482. int rst_check_counter = 10;
  483. ret = bq25890_field_write(bq, F_REG_RST, 1);
  484. if (ret < 0)
  485. return ret;
  486. do {
  487. ret = bq25890_field_read(bq, F_REG_RST);
  488. if (ret < 0)
  489. return ret;
  490. usleep_range(5, 10);
  491. } while (ret == 1 && --rst_check_counter);
  492. if (!rst_check_counter)
  493. return -ETIMEDOUT;
  494. return 0;
  495. }
  496. static int bq25890_hw_init(struct bq25890_device *bq)
  497. {
  498. int ret;
  499. int i;
  500. struct bq25890_state state;
  501. const struct {
  502. enum bq25890_fields id;
  503. u32 value;
  504. } init_data[] = {
  505. {F_ICHG, bq->init_data.ichg},
  506. {F_VREG, bq->init_data.vreg},
  507. {F_ITERM, bq->init_data.iterm},
  508. {F_IPRECHG, bq->init_data.iprechg},
  509. {F_SYSVMIN, bq->init_data.sysvmin},
  510. {F_BOOSTV, bq->init_data.boostv},
  511. {F_BOOSTI, bq->init_data.boosti},
  512. {F_BOOSTF, bq->init_data.boostf},
  513. {F_EN_ILIM, bq->init_data.ilim_en},
  514. {F_TREG, bq->init_data.treg}
  515. };
  516. ret = bq25890_chip_reset(bq);
  517. if (ret < 0)
  518. return ret;
  519. /* disable watchdog */
  520. ret = bq25890_field_write(bq, F_WD, 0);
  521. if (ret < 0)
  522. return ret;
  523. /* initialize currents/voltages and other parameters */
  524. for (i = 0; i < ARRAY_SIZE(init_data); i++) {
  525. ret = bq25890_field_write(bq, init_data[i].id,
  526. init_data[i].value);
  527. if (ret < 0)
  528. return ret;
  529. }
  530. /* Configure ADC for continuous conversions. This does not enable it. */
  531. ret = bq25890_field_write(bq, F_CONV_RATE, 1);
  532. if (ret < 0)
  533. return ret;
  534. ret = bq25890_get_chip_state(bq, &state);
  535. if (ret < 0)
  536. return ret;
  537. mutex_lock(&bq->lock);
  538. bq->state = state;
  539. mutex_unlock(&bq->lock);
  540. return 0;
  541. }
  542. static enum power_supply_property bq25890_power_supply_props[] = {
  543. POWER_SUPPLY_PROP_MANUFACTURER,
  544. POWER_SUPPLY_PROP_STATUS,
  545. POWER_SUPPLY_PROP_ONLINE,
  546. POWER_SUPPLY_PROP_HEALTH,
  547. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  548. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  549. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  550. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  551. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  552. };
  553. static char *bq25890_charger_supplied_to[] = {
  554. "main-battery",
  555. };
  556. static const struct power_supply_desc bq25890_power_supply_desc = {
  557. .name = "bq25890-charger",
  558. .type = POWER_SUPPLY_TYPE_USB,
  559. .properties = bq25890_power_supply_props,
  560. .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
  561. .get_property = bq25890_power_supply_get_property,
  562. };
  563. static int bq25890_power_supply_init(struct bq25890_device *bq)
  564. {
  565. struct power_supply_config psy_cfg = { .drv_data = bq, };
  566. psy_cfg.supplied_to = bq25890_charger_supplied_to;
  567. psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
  568. bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
  569. &psy_cfg);
  570. return PTR_ERR_OR_ZERO(bq->charger);
  571. }
  572. static void bq25890_usb_work(struct work_struct *data)
  573. {
  574. int ret;
  575. struct bq25890_device *bq =
  576. container_of(data, struct bq25890_device, usb_work);
  577. switch (bq->usb_event) {
  578. case USB_EVENT_ID:
  579. /* Enable boost mode */
  580. ret = bq25890_field_write(bq, F_OTG_CFG, 1);
  581. if (ret < 0)
  582. goto error;
  583. break;
  584. case USB_EVENT_NONE:
  585. /* Disable boost mode */
  586. ret = bq25890_field_write(bq, F_OTG_CFG, 0);
  587. if (ret < 0)
  588. goto error;
  589. power_supply_changed(bq->charger);
  590. break;
  591. }
  592. return;
  593. error:
  594. dev_err(bq->dev, "Error switching to boost/charger mode.\n");
  595. }
  596. static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
  597. void *priv)
  598. {
  599. struct bq25890_device *bq =
  600. container_of(nb, struct bq25890_device, usb_nb);
  601. bq->usb_event = val;
  602. queue_work(system_power_efficient_wq, &bq->usb_work);
  603. return NOTIFY_OK;
  604. }
  605. static int bq25890_irq_probe(struct bq25890_device *bq)
  606. {
  607. struct gpio_desc *irq;
  608. irq = devm_gpiod_get_index(bq->dev, BQ25890_IRQ_PIN, 0, GPIOD_IN);
  609. if (IS_ERR(irq)) {
  610. dev_err(bq->dev, "Could not probe irq pin.\n");
  611. return PTR_ERR(irq);
  612. }
  613. return gpiod_to_irq(irq);
  614. }
  615. static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
  616. {
  617. int ret;
  618. u32 property;
  619. int i;
  620. struct bq25890_init_data *init = &bq->init_data;
  621. struct {
  622. char *name;
  623. bool optional;
  624. enum bq25890_table_ids tbl_id;
  625. u8 *conv_data; /* holds converted value from given property */
  626. } props[] = {
  627. /* required properties */
  628. {"ti,charge-current", false, TBL_ICHG, &init->ichg},
  629. {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
  630. {"ti,termination-current", false, TBL_ITERM, &init->iterm},
  631. {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
  632. {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
  633. {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
  634. {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
  635. /* optional properties */
  636. {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg}
  637. };
  638. /* initialize data for optional properties */
  639. init->treg = 3; /* 120 degrees Celsius */
  640. for (i = 0; i < ARRAY_SIZE(props); i++) {
  641. ret = device_property_read_u32(bq->dev, props[i].name,
  642. &property);
  643. if (ret < 0) {
  644. if (props[i].optional)
  645. continue;
  646. return ret;
  647. }
  648. *props[i].conv_data = bq25890_find_idx(property,
  649. props[i].tbl_id);
  650. }
  651. return 0;
  652. }
  653. static int bq25890_fw_probe(struct bq25890_device *bq)
  654. {
  655. int ret;
  656. struct bq25890_init_data *init = &bq->init_data;
  657. ret = bq25890_fw_read_u32_props(bq);
  658. if (ret < 0)
  659. return ret;
  660. init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
  661. init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
  662. return 0;
  663. }
  664. static int bq25890_probe(struct i2c_client *client,
  665. const struct i2c_device_id *id)
  666. {
  667. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  668. struct device *dev = &client->dev;
  669. struct bq25890_device *bq;
  670. int ret;
  671. int i;
  672. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  673. dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
  674. return -ENODEV;
  675. }
  676. bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
  677. if (!bq)
  678. return -ENOMEM;
  679. bq->client = client;
  680. bq->dev = dev;
  681. mutex_init(&bq->lock);
  682. bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
  683. if (IS_ERR(bq->rmap)) {
  684. dev_err(dev, "failed to allocate register map\n");
  685. return PTR_ERR(bq->rmap);
  686. }
  687. for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
  688. const struct reg_field *reg_fields = bq25890_reg_fields;
  689. bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
  690. reg_fields[i]);
  691. if (IS_ERR(bq->rmap_fields[i])) {
  692. dev_err(dev, "cannot allocate regmap field\n");
  693. return PTR_ERR(bq->rmap_fields[i]);
  694. }
  695. }
  696. i2c_set_clientdata(client, bq);
  697. bq->chip_id = bq25890_field_read(bq, F_PN);
  698. if (bq->chip_id < 0) {
  699. dev_err(dev, "Cannot read chip ID.\n");
  700. return bq->chip_id;
  701. }
  702. if (bq->chip_id != BQ25890_ID) {
  703. dev_err(dev, "Chip with ID=%d, not supported!\n", bq->chip_id);
  704. return -ENODEV;
  705. }
  706. if (!dev->platform_data) {
  707. ret = bq25890_fw_probe(bq);
  708. if (ret < 0) {
  709. dev_err(dev, "Cannot read device properties.\n");
  710. return ret;
  711. }
  712. } else {
  713. return -ENODEV;
  714. }
  715. ret = bq25890_hw_init(bq);
  716. if (ret < 0) {
  717. dev_err(dev, "Cannot initialize the chip.\n");
  718. return ret;
  719. }
  720. if (client->irq <= 0)
  721. client->irq = bq25890_irq_probe(bq);
  722. if (client->irq < 0) {
  723. dev_err(dev, "No irq resource found.\n");
  724. return client->irq;
  725. }
  726. /* OTG reporting */
  727. bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  728. if (!IS_ERR_OR_NULL(bq->usb_phy)) {
  729. INIT_WORK(&bq->usb_work, bq25890_usb_work);
  730. bq->usb_nb.notifier_call = bq25890_usb_notifier;
  731. usb_register_notifier(bq->usb_phy, &bq->usb_nb);
  732. }
  733. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  734. bq25890_irq_handler_thread,
  735. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  736. BQ25890_IRQ_PIN, bq);
  737. if (ret)
  738. goto irq_fail;
  739. ret = bq25890_power_supply_init(bq);
  740. if (ret < 0) {
  741. dev_err(dev, "Failed to register power supply\n");
  742. goto irq_fail;
  743. }
  744. return 0;
  745. irq_fail:
  746. if (!IS_ERR_OR_NULL(bq->usb_phy))
  747. usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
  748. return ret;
  749. }
  750. static int bq25890_remove(struct i2c_client *client)
  751. {
  752. struct bq25890_device *bq = i2c_get_clientdata(client);
  753. power_supply_unregister(bq->charger);
  754. if (!IS_ERR_OR_NULL(bq->usb_phy))
  755. usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
  756. /* reset all registers to default values */
  757. bq25890_chip_reset(bq);
  758. return 0;
  759. }
  760. #ifdef CONFIG_PM_SLEEP
  761. static int bq25890_suspend(struct device *dev)
  762. {
  763. struct bq25890_device *bq = dev_get_drvdata(dev);
  764. /*
  765. * If charger is removed, while in suspend, make sure ADC is diabled
  766. * since it consumes slightly more power.
  767. */
  768. return bq25890_field_write(bq, F_CONV_START, 0);
  769. }
  770. static int bq25890_resume(struct device *dev)
  771. {
  772. int ret;
  773. struct bq25890_state state;
  774. struct bq25890_device *bq = dev_get_drvdata(dev);
  775. ret = bq25890_get_chip_state(bq, &state);
  776. if (ret < 0)
  777. return ret;
  778. mutex_lock(&bq->lock);
  779. bq->state = state;
  780. mutex_unlock(&bq->lock);
  781. /* Re-enable ADC only if charger is plugged in. */
  782. if (state.online) {
  783. ret = bq25890_field_write(bq, F_CONV_START, 1);
  784. if (ret < 0)
  785. return ret;
  786. }
  787. /* signal userspace, maybe state changed while suspended */
  788. power_supply_changed(bq->charger);
  789. return 0;
  790. }
  791. #endif
  792. static const struct dev_pm_ops bq25890_pm = {
  793. SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
  794. };
  795. static const struct i2c_device_id bq25890_i2c_ids[] = {
  796. { "bq25890", 0 },
  797. {},
  798. };
  799. MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
  800. static const struct of_device_id bq25890_of_match[] = {
  801. { .compatible = "ti,bq25890", },
  802. { },
  803. };
  804. MODULE_DEVICE_TABLE(of, bq25890_of_match);
  805. static const struct acpi_device_id bq25890_acpi_match[] = {
  806. {"BQ258900", 0},
  807. {},
  808. };
  809. MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
  810. static struct i2c_driver bq25890_driver = {
  811. .driver = {
  812. .name = "bq25890-charger",
  813. .of_match_table = of_match_ptr(bq25890_of_match),
  814. .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
  815. .pm = &bq25890_pm,
  816. },
  817. .probe = bq25890_probe,
  818. .remove = bq25890_remove,
  819. .id_table = bq25890_i2c_ids,
  820. };
  821. module_i2c_driver(bq25890_driver);
  822. MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
  823. MODULE_DESCRIPTION("bq25890 charger driver");
  824. MODULE_LICENSE("GPL");