bq25890_charger.c 28 KB

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