bq25890_charger.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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/power/bq25890_charger.h>
  11. #include <linux/regmap.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/types.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/usb/phy.h>
  18. #include <linux/acpi.h>
  19. #include <linux/of.h>
  20. #define BQ25890_MANUFACTURER "Texas Instruments"
  21. #define BQ25890_IRQ_PIN "bq25890_irq"
  22. #define BQ25890_ID 3
  23. #define BQ25895_ID 7
  24. #define BQ25896_ID 0
  25. #define PUMP_EXPRESS_START_DELAY (5 * HZ)
  26. #define PUMP_EXPRESS_MAX_TRIES 6
  27. #define PUMP_EXPRESS_VBUS_MARGIN_uV 1000000
  28. enum bq25890_chip_version {
  29. BQ25890,
  30. BQ25892,
  31. BQ25895,
  32. BQ25896,
  33. };
  34. static const char *const bq25890_chip_name[] = {
  35. "BQ25890",
  36. "BQ25892",
  37. "BQ25895",
  38. "BQ25896",
  39. };
  40. enum bq25890_fields {
  41. F_EN_HIZ, F_EN_ILIM, F_IINLIM, /* Reg00 */
  42. F_BHOT, F_BCOLD, F_VINDPM_OFS, /* Reg01 */
  43. F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
  44. F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN, /* Reg02 */
  45. F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
  46. F_MIN_VBAT_SEL, /* Reg03 */
  47. F_PUMPX_EN, F_ICHG, /* Reg04 */
  48. F_IPRECHG, F_ITERM, /* Reg05 */
  49. F_VREG, F_BATLOWV, F_VRECHG, /* Reg06 */
  50. F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
  51. F_JEITA_ISET, /* Reg07 */
  52. F_BATCMP, F_VCLAMP, F_TREG, /* Reg08 */
  53. F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
  54. F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN, /* Reg09 */
  55. F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI, /* Reg0A */
  56. F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
  57. F_VSYS_STAT, /* Reg0B */
  58. F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
  59. F_NTC_FAULT, /* Reg0C */
  60. F_FORCE_VINDPM, F_VINDPM, /* Reg0D */
  61. F_THERM_STAT, F_BATV, /* Reg0E */
  62. F_SYSV, /* Reg0F */
  63. F_TSPCT, /* Reg10 */
  64. F_VBUS_GD, F_VBUSV, /* Reg11 */
  65. F_ICHGR, /* Reg12 */
  66. F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM, /* Reg13 */
  67. F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV, /* Reg14 */
  68. F_MAX_FIELDS
  69. };
  70. /* initial field values, converted to register values */
  71. struct bq25890_init_data {
  72. u8 ichg; /* charge current */
  73. u8 vreg; /* regulation voltage */
  74. u8 iterm; /* termination current */
  75. u8 iprechg; /* precharge current */
  76. u8 sysvmin; /* minimum system voltage limit */
  77. u8 boostv; /* boost regulation voltage */
  78. u8 boosti; /* boost current limit */
  79. u8 boostf; /* boost frequency */
  80. u8 ilim_en; /* enable ILIM pin */
  81. u8 treg; /* thermal regulation threshold */
  82. u8 rbatcomp; /* IBAT sense resistor value */
  83. u8 vclamp; /* IBAT compensation voltage limit */
  84. };
  85. struct bq25890_state {
  86. u8 online;
  87. u8 chrg_status;
  88. u8 chrg_fault;
  89. u8 vsys_status;
  90. u8 boost_fault;
  91. u8 bat_fault;
  92. u8 ntc_fault;
  93. };
  94. struct bq25890_device {
  95. struct i2c_client *client;
  96. struct device *dev;
  97. struct power_supply *charger;
  98. struct usb_phy *usb_phy;
  99. struct notifier_block usb_nb;
  100. struct work_struct usb_work;
  101. struct delayed_work pump_express_work;
  102. unsigned long usb_event;
  103. struct regmap *rmap;
  104. struct regmap_field *rmap_fields[F_MAX_FIELDS];
  105. bool skip_reset;
  106. bool read_back_init_data;
  107. u32 pump_express_vbus_max;
  108. enum bq25890_chip_version chip_version;
  109. struct bq25890_init_data init_data;
  110. struct bq25890_state state;
  111. struct mutex lock; /* protect state data */
  112. };
  113. static const struct regmap_range bq25890_readonly_reg_ranges[] = {
  114. regmap_reg_range(0x0b, 0x0c),
  115. regmap_reg_range(0x0e, 0x13),
  116. };
  117. static const struct regmap_access_table bq25890_writeable_regs = {
  118. .no_ranges = bq25890_readonly_reg_ranges,
  119. .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
  120. };
  121. static const struct regmap_range bq25890_volatile_reg_ranges[] = {
  122. regmap_reg_range(0x00, 0x00),
  123. regmap_reg_range(0x02, 0x02),
  124. regmap_reg_range(0x09, 0x09),
  125. regmap_reg_range(0x0b, 0x14),
  126. };
  127. static const struct regmap_access_table bq25890_volatile_regs = {
  128. .yes_ranges = bq25890_volatile_reg_ranges,
  129. .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
  130. };
  131. static const struct regmap_config bq25890_regmap_config = {
  132. .reg_bits = 8,
  133. .val_bits = 8,
  134. .max_register = 0x14,
  135. .cache_type = REGCACHE_RBTREE,
  136. .wr_table = &bq25890_writeable_regs,
  137. .volatile_table = &bq25890_volatile_regs,
  138. };
  139. static const struct reg_field bq25890_reg_fields[] = {
  140. /* REG00 */
  141. [F_EN_HIZ] = REG_FIELD(0x00, 7, 7),
  142. [F_EN_ILIM] = REG_FIELD(0x00, 6, 6),
  143. [F_IINLIM] = REG_FIELD(0x00, 0, 5),
  144. /* REG01 */
  145. [F_BHOT] = REG_FIELD(0x01, 6, 7),
  146. [F_BCOLD] = REG_FIELD(0x01, 5, 5),
  147. [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4),
  148. /* REG02 */
  149. [F_CONV_START] = REG_FIELD(0x02, 7, 7),
  150. [F_CONV_RATE] = REG_FIELD(0x02, 6, 6),
  151. [F_BOOSTF] = REG_FIELD(0x02, 5, 5),
  152. [F_ICO_EN] = REG_FIELD(0x02, 4, 4),
  153. [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
  154. [F_MAXC_EN] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
  155. [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1),
  156. [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0),
  157. /* REG03 */
  158. [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7),
  159. [F_WD_RST] = REG_FIELD(0x03, 6, 6),
  160. [F_OTG_CFG] = REG_FIELD(0x03, 5, 5),
  161. [F_CHG_CFG] = REG_FIELD(0x03, 4, 4),
  162. [F_SYSVMIN] = REG_FIELD(0x03, 1, 3),
  163. [F_MIN_VBAT_SEL] = REG_FIELD(0x03, 0, 0), // BQ25896 only
  164. /* REG04 */
  165. [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7),
  166. [F_ICHG] = REG_FIELD(0x04, 0, 6),
  167. /* REG05 */
  168. [F_IPRECHG] = REG_FIELD(0x05, 4, 7),
  169. [F_ITERM] = REG_FIELD(0x05, 0, 3),
  170. /* REG06 */
  171. [F_VREG] = REG_FIELD(0x06, 2, 7),
  172. [F_BATLOWV] = REG_FIELD(0x06, 1, 1),
  173. [F_VRECHG] = REG_FIELD(0x06, 0, 0),
  174. /* REG07 */
  175. [F_TERM_EN] = REG_FIELD(0x07, 7, 7),
  176. [F_STAT_DIS] = REG_FIELD(0x07, 6, 6),
  177. [F_WD] = REG_FIELD(0x07, 4, 5),
  178. [F_TMR_EN] = REG_FIELD(0x07, 3, 3),
  179. [F_CHG_TMR] = REG_FIELD(0x07, 1, 2),
  180. [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
  181. /* REG08 */
  182. [F_BATCMP] = REG_FIELD(0x08, 5, 7),
  183. [F_VCLAMP] = REG_FIELD(0x08, 2, 4),
  184. [F_TREG] = REG_FIELD(0x08, 0, 1),
  185. /* REG09 */
  186. [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7),
  187. [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6),
  188. [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5),
  189. [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
  190. [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3),
  191. [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2),
  192. [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1),
  193. [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0),
  194. /* REG0A */
  195. [F_BOOSTV] = REG_FIELD(0x0A, 4, 7),
  196. [F_BOOSTI] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
  197. [F_PFM_OTG_DIS] = REG_FIELD(0x0A, 3, 3), // BQ25896 only
  198. /* REG0B */
  199. [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7),
  200. [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4),
  201. [F_PG_STAT] = REG_FIELD(0x0B, 2, 2),
  202. [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
  203. [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0),
  204. /* REG0C */
  205. [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7),
  206. [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6),
  207. [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5),
  208. [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3),
  209. [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2),
  210. /* REG0D */
  211. [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7),
  212. [F_VINDPM] = REG_FIELD(0x0D, 0, 6),
  213. /* REG0E */
  214. [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7),
  215. [F_BATV] = REG_FIELD(0x0E, 0, 6),
  216. /* REG0F */
  217. [F_SYSV] = REG_FIELD(0x0F, 0, 6),
  218. /* REG10 */
  219. [F_TSPCT] = REG_FIELD(0x10, 0, 6),
  220. /* REG11 */
  221. [F_VBUS_GD] = REG_FIELD(0x11, 7, 7),
  222. [F_VBUSV] = REG_FIELD(0x11, 0, 6),
  223. /* REG12 */
  224. [F_ICHGR] = REG_FIELD(0x12, 0, 6),
  225. /* REG13 */
  226. [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7),
  227. [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6),
  228. [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5),
  229. /* REG14 */
  230. [F_REG_RST] = REG_FIELD(0x14, 7, 7),
  231. [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6),
  232. [F_PN] = REG_FIELD(0x14, 3, 5),
  233. [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2),
  234. [F_DEV_REV] = REG_FIELD(0x14, 0, 1)
  235. };
  236. /*
  237. * Most of the val -> idx conversions can be computed, given the minimum,
  238. * maximum and the step between values. For the rest of conversions, we use
  239. * lookup tables.
  240. */
  241. enum bq25890_table_ids {
  242. /* range tables */
  243. TBL_ICHG,
  244. TBL_ITERM,
  245. TBL_IINLIM,
  246. TBL_VREG,
  247. TBL_BOOSTV,
  248. TBL_SYSVMIN,
  249. TBL_VBUSV,
  250. TBL_VBATCOMP,
  251. TBL_RBATCOMP,
  252. /* lookup tables */
  253. TBL_TREG,
  254. TBL_BOOSTI,
  255. TBL_TSPCT,
  256. };
  257. /* Thermal Regulation Threshold lookup table, in degrees Celsius */
  258. static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
  259. #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
  260. /* Boost mode current limit lookup table, in uA */
  261. static const u32 bq25890_boosti_tbl[] = {
  262. 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
  263. };
  264. #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
  265. /* NTC 10K temperature lookup table in tenths of a degree */
  266. static const u32 bq25890_tspct_tbl[] = {
  267. 850, 840, 830, 820, 810, 800, 790, 780,
  268. 770, 760, 750, 740, 730, 720, 710, 700,
  269. 690, 685, 680, 675, 670, 660, 650, 645,
  270. 640, 630, 620, 615, 610, 600, 590, 585,
  271. 580, 570, 565, 560, 550, 540, 535, 530,
  272. 520, 515, 510, 500, 495, 490, 480, 475,
  273. 470, 460, 455, 450, 440, 435, 430, 425,
  274. 420, 410, 405, 400, 390, 385, 380, 370,
  275. 365, 360, 355, 350, 340, 335, 330, 320,
  276. 310, 305, 300, 290, 285, 280, 275, 270,
  277. 260, 250, 245, 240, 230, 225, 220, 210,
  278. 205, 200, 190, 180, 175, 170, 160, 150,
  279. 145, 140, 130, 120, 115, 110, 100, 90,
  280. 80, 70, 60, 50, 40, 30, 20, 10,
  281. 0, -10, -20, -30, -40, -60, -70, -80,
  282. -90, -10, -120, -140, -150, -170, -190, -210,
  283. };
  284. #define BQ25890_TSPCT_TBL_SIZE ARRAY_SIZE(bq25890_tspct_tbl)
  285. struct bq25890_range {
  286. u32 min;
  287. u32 max;
  288. u32 step;
  289. };
  290. struct bq25890_lookup {
  291. const u32 *tbl;
  292. u32 size;
  293. };
  294. static const union {
  295. struct bq25890_range rt;
  296. struct bq25890_lookup lt;
  297. } bq25890_tables[] = {
  298. /* range tables */
  299. /* TODO: BQ25896 has max ICHG 3008 mA */
  300. [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
  301. [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
  302. [TBL_IINLIM] = { .rt = {100000, 3250000, 50000} }, /* uA */
  303. [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
  304. [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
  305. [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
  306. [TBL_VBUSV] = { .rt = {2600000, 15300000, 100000} }, /* uV */
  307. [TBL_VBATCOMP] = { .rt = {0, 224000, 32000} }, /* uV */
  308. [TBL_RBATCOMP] = { .rt = {0, 140000, 20000} }, /* uOhm */
  309. /* lookup tables */
  310. [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
  311. [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} },
  312. [TBL_TSPCT] = { .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} }
  313. };
  314. static int bq25890_field_read(struct bq25890_device *bq,
  315. enum bq25890_fields field_id)
  316. {
  317. int ret;
  318. int val;
  319. ret = regmap_field_read(bq->rmap_fields[field_id], &val);
  320. if (ret < 0)
  321. return ret;
  322. return val;
  323. }
  324. static int bq25890_field_write(struct bq25890_device *bq,
  325. enum bq25890_fields field_id, u8 val)
  326. {
  327. return regmap_field_write(bq->rmap_fields[field_id], val);
  328. }
  329. static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
  330. {
  331. u8 idx;
  332. if (id >= TBL_TREG) {
  333. const u32 *tbl = bq25890_tables[id].lt.tbl;
  334. u32 tbl_size = bq25890_tables[id].lt.size;
  335. for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
  336. ;
  337. } else {
  338. const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
  339. u8 rtbl_size;
  340. rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
  341. for (idx = 1;
  342. idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
  343. idx++)
  344. ;
  345. }
  346. return idx - 1;
  347. }
  348. static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
  349. {
  350. const struct bq25890_range *rtbl;
  351. /* lookup table? */
  352. if (id >= TBL_TREG)
  353. return bq25890_tables[id].lt.tbl[idx];
  354. /* range table */
  355. rtbl = &bq25890_tables[id].rt;
  356. return (rtbl->min + idx * rtbl->step);
  357. }
  358. enum bq25890_status {
  359. STATUS_NOT_CHARGING,
  360. STATUS_PRE_CHARGING,
  361. STATUS_FAST_CHARGING,
  362. STATUS_TERMINATION_DONE,
  363. };
  364. enum bq25890_chrg_fault {
  365. CHRG_FAULT_NORMAL,
  366. CHRG_FAULT_INPUT,
  367. CHRG_FAULT_THERMAL_SHUTDOWN,
  368. CHRG_FAULT_TIMER_EXPIRED,
  369. };
  370. enum bq25890_ntc_fault {
  371. NTC_FAULT_NORMAL = 0,
  372. NTC_FAULT_WARM = 2,
  373. NTC_FAULT_COOL = 3,
  374. NTC_FAULT_COLD = 5,
  375. NTC_FAULT_HOT = 6,
  376. };
  377. static bool bq25890_is_adc_property(enum power_supply_property psp)
  378. {
  379. switch (psp) {
  380. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  381. case POWER_SUPPLY_PROP_CURRENT_NOW:
  382. case POWER_SUPPLY_PROP_TEMP:
  383. return true;
  384. default:
  385. return false;
  386. }
  387. }
  388. static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
  389. static int bq25890_get_vbus_voltage(struct bq25890_device *bq)
  390. {
  391. int ret;
  392. ret = bq25890_field_read(bq, F_VBUSV);
  393. if (ret < 0)
  394. return ret;
  395. return bq25890_find_val(ret, TBL_VBUSV);
  396. }
  397. static int bq25890_power_supply_get_property(struct power_supply *psy,
  398. enum power_supply_property psp,
  399. union power_supply_propval *val)
  400. {
  401. struct bq25890_device *bq = power_supply_get_drvdata(psy);
  402. struct bq25890_state state;
  403. bool do_adc_conv;
  404. int ret;
  405. mutex_lock(&bq->lock);
  406. /* update state in case we lost an interrupt */
  407. __bq25890_handle_irq(bq);
  408. state = bq->state;
  409. do_adc_conv = !state.online && bq25890_is_adc_property(psp);
  410. if (do_adc_conv)
  411. bq25890_field_write(bq, F_CONV_START, 1);
  412. mutex_unlock(&bq->lock);
  413. if (do_adc_conv)
  414. regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
  415. ret, !ret, 25000, 1000000);
  416. switch (psp) {
  417. case POWER_SUPPLY_PROP_STATUS:
  418. if (!state.online)
  419. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  420. else if (state.chrg_status == STATUS_NOT_CHARGING)
  421. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  422. else if (state.chrg_status == STATUS_PRE_CHARGING ||
  423. state.chrg_status == STATUS_FAST_CHARGING)
  424. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  425. else if (state.chrg_status == STATUS_TERMINATION_DONE)
  426. val->intval = POWER_SUPPLY_STATUS_FULL;
  427. else
  428. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  429. break;
  430. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  431. if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
  432. state.chrg_status == STATUS_TERMINATION_DONE)
  433. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  434. else if (state.chrg_status == STATUS_PRE_CHARGING)
  435. val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
  436. else if (state.chrg_status == STATUS_FAST_CHARGING)
  437. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  438. else /* unreachable */
  439. val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  440. break;
  441. case POWER_SUPPLY_PROP_MANUFACTURER:
  442. val->strval = BQ25890_MANUFACTURER;
  443. break;
  444. case POWER_SUPPLY_PROP_MODEL_NAME:
  445. val->strval = bq25890_chip_name[bq->chip_version];
  446. break;
  447. case POWER_SUPPLY_PROP_ONLINE:
  448. val->intval = state.online;
  449. break;
  450. case POWER_SUPPLY_PROP_HEALTH:
  451. if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
  452. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  453. else if (state.bat_fault)
  454. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  455. else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
  456. val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  457. else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
  458. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  459. else
  460. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  461. break;
  462. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  463. val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
  464. /* When temperature is too low, charge current is decreased */
  465. if (bq->state.ntc_fault == NTC_FAULT_COOL) {
  466. ret = bq25890_field_read(bq, F_JEITA_ISET);
  467. if (ret < 0)
  468. return ret;
  469. if (ret)
  470. val->intval /= 5;
  471. else
  472. val->intval /= 2;
  473. }
  474. break;
  475. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  476. if (!state.online) {
  477. val->intval = 0;
  478. break;
  479. }
  480. ret = bq25890_field_read(bq, F_BATV); /* read measured value */
  481. if (ret < 0)
  482. return ret;
  483. /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
  484. val->intval = 2304000 + ret * 20000;
  485. break;
  486. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  487. val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
  488. break;
  489. case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
  490. val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
  491. break;
  492. case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  493. val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
  494. break;
  495. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  496. ret = bq25890_field_read(bq, F_IINLIM);
  497. if (ret < 0)
  498. return ret;
  499. val->intval = bq25890_find_val(ret, TBL_IINLIM);
  500. break;
  501. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  502. ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
  503. if (ret < 0)
  504. return ret;
  505. /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
  506. val->intval = 2304000 + ret * 20000;
  507. break;
  508. case POWER_SUPPLY_PROP_CURRENT_NOW:
  509. ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
  510. if (ret < 0)
  511. return ret;
  512. /* converted_val = ADC_val * 50mA (table 10.3.19) */
  513. val->intval = ret * -50000;
  514. break;
  515. case POWER_SUPPLY_PROP_TEMP:
  516. ret = bq25890_field_read(bq, F_TSPCT);
  517. if (ret < 0)
  518. return ret;
  519. /* convert TS percentage into rough temperature */
  520. val->intval = bq25890_find_val(ret, TBL_TSPCT);
  521. break;
  522. default:
  523. return -EINVAL;
  524. }
  525. return 0;
  526. }
  527. static int bq25890_power_supply_set_property(struct power_supply *psy,
  528. enum power_supply_property psp,
  529. const union power_supply_propval *val)
  530. {
  531. struct bq25890_device *bq = power_supply_get_drvdata(psy);
  532. u8 lval;
  533. switch (psp) {
  534. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  535. lval = bq25890_find_idx(val->intval, TBL_IINLIM);
  536. return bq25890_field_write(bq, F_IINLIM, lval);
  537. default:
  538. return -EINVAL;
  539. }
  540. }
  541. static int bq25890_power_supply_property_is_writeable(struct power_supply *psy,
  542. enum power_supply_property psp)
  543. {
  544. switch (psp) {
  545. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  546. return true;
  547. default:
  548. return false;
  549. }
  550. }
  551. /* On the BQ25892 try to get charger-type info from our supplier */
  552. static void bq25890_charger_external_power_changed(struct power_supply *psy)
  553. {
  554. struct bq25890_device *bq = power_supply_get_drvdata(psy);
  555. union power_supply_propval val;
  556. int input_current_limit, ret;
  557. if (bq->chip_version != BQ25892)
  558. return;
  559. ret = power_supply_get_property_from_supplier(psy,
  560. POWER_SUPPLY_PROP_USB_TYPE,
  561. &val);
  562. if (ret)
  563. return;
  564. switch (val.intval) {
  565. case POWER_SUPPLY_USB_TYPE_DCP:
  566. input_current_limit = bq25890_find_idx(2000000, TBL_IINLIM);
  567. if (bq->pump_express_vbus_max) {
  568. queue_delayed_work(system_power_efficient_wq,
  569. &bq->pump_express_work,
  570. PUMP_EXPRESS_START_DELAY);
  571. }
  572. break;
  573. case POWER_SUPPLY_USB_TYPE_CDP:
  574. case POWER_SUPPLY_USB_TYPE_ACA:
  575. input_current_limit = bq25890_find_idx(1500000, TBL_IINLIM);
  576. break;
  577. case POWER_SUPPLY_USB_TYPE_SDP:
  578. default:
  579. input_current_limit = bq25890_find_idx(500000, TBL_IINLIM);
  580. }
  581. bq25890_field_write(bq, F_IINLIM, input_current_limit);
  582. power_supply_changed(psy);
  583. }
  584. static int bq25890_get_chip_state(struct bq25890_device *bq,
  585. struct bq25890_state *state)
  586. {
  587. int i, ret;
  588. struct {
  589. enum bq25890_fields id;
  590. u8 *data;
  591. } state_fields[] = {
  592. {F_CHG_STAT, &state->chrg_status},
  593. {F_PG_STAT, &state->online},
  594. {F_VSYS_STAT, &state->vsys_status},
  595. {F_BOOST_FAULT, &state->boost_fault},
  596. {F_BAT_FAULT, &state->bat_fault},
  597. {F_CHG_FAULT, &state->chrg_fault},
  598. {F_NTC_FAULT, &state->ntc_fault}
  599. };
  600. for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
  601. ret = bq25890_field_read(bq, state_fields[i].id);
  602. if (ret < 0)
  603. return ret;
  604. *state_fields[i].data = ret;
  605. }
  606. dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n",
  607. state->chrg_status, state->online, state->vsys_status,
  608. state->chrg_fault, state->boost_fault, state->bat_fault,
  609. state->ntc_fault);
  610. return 0;
  611. }
  612. static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
  613. {
  614. struct bq25890_state new_state;
  615. int ret;
  616. ret = bq25890_get_chip_state(bq, &new_state);
  617. if (ret < 0)
  618. return IRQ_NONE;
  619. if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
  620. return IRQ_NONE;
  621. if (!new_state.online && bq->state.online) { /* power removed */
  622. /* disable ADC */
  623. ret = bq25890_field_write(bq, F_CONV_RATE, 0);
  624. if (ret < 0)
  625. goto error;
  626. } else if (new_state.online && !bq->state.online) { /* power inserted */
  627. /* enable ADC, to have control of charge current/voltage */
  628. ret = bq25890_field_write(bq, F_CONV_RATE, 1);
  629. if (ret < 0)
  630. goto error;
  631. }
  632. bq->state = new_state;
  633. power_supply_changed(bq->charger);
  634. return IRQ_HANDLED;
  635. error:
  636. dev_err(bq->dev, "Error communicating with the chip: %pe\n",
  637. ERR_PTR(ret));
  638. return IRQ_HANDLED;
  639. }
  640. static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
  641. {
  642. struct bq25890_device *bq = private;
  643. irqreturn_t ret;
  644. mutex_lock(&bq->lock);
  645. ret = __bq25890_handle_irq(bq);
  646. mutex_unlock(&bq->lock);
  647. return ret;
  648. }
  649. static int bq25890_chip_reset(struct bq25890_device *bq)
  650. {
  651. int ret;
  652. int rst_check_counter = 10;
  653. ret = bq25890_field_write(bq, F_REG_RST, 1);
  654. if (ret < 0)
  655. return ret;
  656. do {
  657. ret = bq25890_field_read(bq, F_REG_RST);
  658. if (ret < 0)
  659. return ret;
  660. usleep_range(5, 10);
  661. } while (ret == 1 && --rst_check_counter);
  662. if (!rst_check_counter)
  663. return -ETIMEDOUT;
  664. return 0;
  665. }
  666. static int bq25890_rw_init_data(struct bq25890_device *bq)
  667. {
  668. bool write = !bq->read_back_init_data;
  669. int ret;
  670. int i;
  671. const struct {
  672. enum bq25890_fields id;
  673. u8 *value;
  674. } init_data[] = {
  675. {F_ICHG, &bq->init_data.ichg},
  676. {F_VREG, &bq->init_data.vreg},
  677. {F_ITERM, &bq->init_data.iterm},
  678. {F_IPRECHG, &bq->init_data.iprechg},
  679. {F_SYSVMIN, &bq->init_data.sysvmin},
  680. {F_BOOSTV, &bq->init_data.boostv},
  681. {F_BOOSTI, &bq->init_data.boosti},
  682. {F_BOOSTF, &bq->init_data.boostf},
  683. {F_EN_ILIM, &bq->init_data.ilim_en},
  684. {F_TREG, &bq->init_data.treg},
  685. {F_BATCMP, &bq->init_data.rbatcomp},
  686. {F_VCLAMP, &bq->init_data.vclamp},
  687. };
  688. for (i = 0; i < ARRAY_SIZE(init_data); i++) {
  689. if (write) {
  690. ret = bq25890_field_write(bq, init_data[i].id,
  691. *init_data[i].value);
  692. } else {
  693. ret = bq25890_field_read(bq, init_data[i].id);
  694. if (ret >= 0)
  695. *init_data[i].value = ret;
  696. }
  697. if (ret < 0) {
  698. dev_dbg(bq->dev, "Accessing init data failed %d\n", ret);
  699. return ret;
  700. }
  701. }
  702. return 0;
  703. }
  704. static int bq25890_hw_init(struct bq25890_device *bq)
  705. {
  706. int ret;
  707. if (!bq->skip_reset) {
  708. ret = bq25890_chip_reset(bq);
  709. if (ret < 0) {
  710. dev_dbg(bq->dev, "Reset failed %d\n", ret);
  711. return ret;
  712. }
  713. } else {
  714. /*
  715. * Ensure charging is enabled, on some boards where the fw
  716. * takes care of initalizition F_CHG_CFG is set to 0 before
  717. * handing control over to the OS.
  718. */
  719. ret = bq25890_field_write(bq, F_CHG_CFG, 1);
  720. if (ret < 0) {
  721. dev_dbg(bq->dev, "Enabling charging failed %d\n", ret);
  722. return ret;
  723. }
  724. }
  725. /* disable watchdog */
  726. ret = bq25890_field_write(bq, F_WD, 0);
  727. if (ret < 0) {
  728. dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
  729. return ret;
  730. }
  731. /* initialize currents/voltages and other parameters */
  732. ret = bq25890_rw_init_data(bq);
  733. if (ret)
  734. return ret;
  735. ret = bq25890_get_chip_state(bq, &bq->state);
  736. if (ret < 0) {
  737. dev_dbg(bq->dev, "Get state failed %d\n", ret);
  738. return ret;
  739. }
  740. /* Configure ADC for continuous conversions when charging */
  741. ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
  742. if (ret < 0) {
  743. dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
  744. return ret;
  745. }
  746. return 0;
  747. }
  748. static const enum power_supply_property bq25890_power_supply_props[] = {
  749. POWER_SUPPLY_PROP_MANUFACTURER,
  750. POWER_SUPPLY_PROP_MODEL_NAME,
  751. POWER_SUPPLY_PROP_STATUS,
  752. POWER_SUPPLY_PROP_CHARGE_TYPE,
  753. POWER_SUPPLY_PROP_ONLINE,
  754. POWER_SUPPLY_PROP_HEALTH,
  755. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  756. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  757. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  758. POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
  759. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  760. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  761. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  762. POWER_SUPPLY_PROP_CURRENT_NOW,
  763. POWER_SUPPLY_PROP_TEMP,
  764. };
  765. static char *bq25890_charger_supplied_to[] = {
  766. "main-battery",
  767. };
  768. static const struct power_supply_desc bq25890_power_supply_desc = {
  769. .name = "bq25890-charger",
  770. .type = POWER_SUPPLY_TYPE_USB,
  771. .properties = bq25890_power_supply_props,
  772. .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
  773. .get_property = bq25890_power_supply_get_property,
  774. .set_property = bq25890_power_supply_set_property,
  775. .property_is_writeable = bq25890_power_supply_property_is_writeable,
  776. .external_power_changed = bq25890_charger_external_power_changed,
  777. };
  778. static int bq25890_power_supply_init(struct bq25890_device *bq)
  779. {
  780. struct power_supply_config psy_cfg = { .drv_data = bq, };
  781. psy_cfg.supplied_to = bq25890_charger_supplied_to;
  782. psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
  783. bq->charger = devm_power_supply_register(bq->dev,
  784. &bq25890_power_supply_desc,
  785. &psy_cfg);
  786. return PTR_ERR_OR_ZERO(bq->charger);
  787. }
  788. static int bq25890_set_otg_cfg(struct bq25890_device *bq, u8 val)
  789. {
  790. int ret;
  791. ret = bq25890_field_write(bq, F_OTG_CFG, val);
  792. if (ret < 0)
  793. dev_err(bq->dev, "Error switching to boost/charger mode: %d\n", ret);
  794. return ret;
  795. }
  796. static void bq25890_pump_express_work(struct work_struct *data)
  797. {
  798. struct bq25890_device *bq =
  799. container_of(data, struct bq25890_device, pump_express_work.work);
  800. int voltage, i, ret;
  801. dev_dbg(bq->dev, "Start to request input voltage increasing\n");
  802. /* Enable current pulse voltage control protocol */
  803. ret = bq25890_field_write(bq, F_PUMPX_EN, 1);
  804. if (ret < 0)
  805. goto error_print;
  806. for (i = 0; i < PUMP_EXPRESS_MAX_TRIES; i++) {
  807. voltage = bq25890_get_vbus_voltage(bq);
  808. if (voltage < 0)
  809. goto error_print;
  810. dev_dbg(bq->dev, "input voltage = %d uV\n", voltage);
  811. if ((voltage + PUMP_EXPRESS_VBUS_MARGIN_uV) >
  812. bq->pump_express_vbus_max)
  813. break;
  814. ret = bq25890_field_write(bq, F_PUMPX_UP, 1);
  815. if (ret < 0)
  816. goto error_print;
  817. /* Note a single PUMPX up pulse-sequence takes 2.1s */
  818. ret = regmap_field_read_poll_timeout(bq->rmap_fields[F_PUMPX_UP],
  819. ret, !ret, 100000, 3000000);
  820. if (ret < 0)
  821. goto error_print;
  822. /* Make sure ADC has sampled Vbus before checking again */
  823. msleep(1000);
  824. }
  825. bq25890_field_write(bq, F_PUMPX_EN, 0);
  826. dev_info(bq->dev, "Hi-voltage charging requested, input voltage is %d mV\n",
  827. voltage);
  828. power_supply_changed(bq->charger);
  829. return;
  830. error_print:
  831. bq25890_field_write(bq, F_PUMPX_EN, 0);
  832. dev_err(bq->dev, "Failed to request hi-voltage charging\n");
  833. }
  834. static void bq25890_usb_work(struct work_struct *data)
  835. {
  836. int ret;
  837. struct bq25890_device *bq =
  838. container_of(data, struct bq25890_device, usb_work);
  839. switch (bq->usb_event) {
  840. case USB_EVENT_ID:
  841. /* Enable boost mode */
  842. bq25890_set_otg_cfg(bq, 1);
  843. break;
  844. case USB_EVENT_NONE:
  845. /* Disable boost mode */
  846. ret = bq25890_set_otg_cfg(bq, 0);
  847. if (ret == 0)
  848. power_supply_changed(bq->charger);
  849. break;
  850. }
  851. }
  852. static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
  853. void *priv)
  854. {
  855. struct bq25890_device *bq =
  856. container_of(nb, struct bq25890_device, usb_nb);
  857. bq->usb_event = val;
  858. queue_work(system_power_efficient_wq, &bq->usb_work);
  859. return NOTIFY_OK;
  860. }
  861. #ifdef CONFIG_REGULATOR
  862. static int bq25890_vbus_enable(struct regulator_dev *rdev)
  863. {
  864. struct bq25890_device *bq = rdev_get_drvdata(rdev);
  865. return bq25890_set_otg_cfg(bq, 1);
  866. }
  867. static int bq25890_vbus_disable(struct regulator_dev *rdev)
  868. {
  869. struct bq25890_device *bq = rdev_get_drvdata(rdev);
  870. return bq25890_set_otg_cfg(bq, 0);
  871. }
  872. static int bq25890_vbus_is_enabled(struct regulator_dev *rdev)
  873. {
  874. struct bq25890_device *bq = rdev_get_drvdata(rdev);
  875. return bq25890_field_read(bq, F_OTG_CFG);
  876. }
  877. static const struct regulator_ops bq25890_vbus_ops = {
  878. .enable = bq25890_vbus_enable,
  879. .disable = bq25890_vbus_disable,
  880. .is_enabled = bq25890_vbus_is_enabled,
  881. };
  882. static const struct regulator_desc bq25890_vbus_desc = {
  883. .name = "usb_otg_vbus",
  884. .of_match = "usb-otg-vbus",
  885. .type = REGULATOR_VOLTAGE,
  886. .owner = THIS_MODULE,
  887. .ops = &bq25890_vbus_ops,
  888. .fixed_uV = 5000000,
  889. .n_voltages = 1,
  890. };
  891. static int bq25890_register_regulator(struct bq25890_device *bq)
  892. {
  893. struct bq25890_platform_data *pdata = dev_get_platdata(bq->dev);
  894. struct regulator_config cfg = {
  895. .dev = bq->dev,
  896. .driver_data = bq,
  897. };
  898. struct regulator_dev *reg;
  899. if (!IS_ERR_OR_NULL(bq->usb_phy))
  900. return 0;
  901. if (pdata)
  902. cfg.init_data = pdata->regulator_init_data;
  903. reg = devm_regulator_register(bq->dev, &bq25890_vbus_desc, &cfg);
  904. if (IS_ERR(reg)) {
  905. return dev_err_probe(bq->dev, PTR_ERR(reg),
  906. "registering vbus regulator");
  907. }
  908. return 0;
  909. }
  910. #else
  911. static inline int
  912. bq25890_register_regulator(struct bq25890_device *bq)
  913. {
  914. return 0;
  915. }
  916. #endif
  917. static int bq25890_get_chip_version(struct bq25890_device *bq)
  918. {
  919. int id, rev;
  920. id = bq25890_field_read(bq, F_PN);
  921. if (id < 0) {
  922. dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
  923. return id;
  924. }
  925. rev = bq25890_field_read(bq, F_DEV_REV);
  926. if (rev < 0) {
  927. dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
  928. return rev;
  929. }
  930. switch (id) {
  931. case BQ25890_ID:
  932. bq->chip_version = BQ25890;
  933. break;
  934. /* BQ25892 and BQ25896 share same ID 0 */
  935. case BQ25896_ID:
  936. switch (rev) {
  937. case 2:
  938. bq->chip_version = BQ25896;
  939. break;
  940. case 1:
  941. bq->chip_version = BQ25892;
  942. break;
  943. default:
  944. dev_err(bq->dev,
  945. "Unknown device revision %d, assume BQ25892\n",
  946. rev);
  947. bq->chip_version = BQ25892;
  948. }
  949. break;
  950. case BQ25895_ID:
  951. bq->chip_version = BQ25895;
  952. break;
  953. default:
  954. dev_err(bq->dev, "Unknown chip ID %d\n", id);
  955. return -ENODEV;
  956. }
  957. return 0;
  958. }
  959. static int bq25890_irq_probe(struct bq25890_device *bq)
  960. {
  961. struct gpio_desc *irq;
  962. irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
  963. if (IS_ERR(irq))
  964. return dev_err_probe(bq->dev, PTR_ERR(irq),
  965. "Could not probe irq pin.\n");
  966. return gpiod_to_irq(irq);
  967. }
  968. static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
  969. {
  970. int ret;
  971. u32 property;
  972. int i;
  973. struct bq25890_init_data *init = &bq->init_data;
  974. struct {
  975. char *name;
  976. bool optional;
  977. enum bq25890_table_ids tbl_id;
  978. u8 *conv_data; /* holds converted value from given property */
  979. } props[] = {
  980. /* required properties */
  981. {"ti,charge-current", false, TBL_ICHG, &init->ichg},
  982. {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
  983. {"ti,termination-current", false, TBL_ITERM, &init->iterm},
  984. {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
  985. {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
  986. {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
  987. {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
  988. /* optional properties */
  989. {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
  990. {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
  991. {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
  992. };
  993. /* initialize data for optional properties */
  994. init->treg = 3; /* 120 degrees Celsius */
  995. init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
  996. for (i = 0; i < ARRAY_SIZE(props); i++) {
  997. ret = device_property_read_u32(bq->dev, props[i].name,
  998. &property);
  999. if (ret < 0) {
  1000. if (props[i].optional)
  1001. continue;
  1002. dev_err(bq->dev, "Unable to read property %d %s\n", ret,
  1003. props[i].name);
  1004. return ret;
  1005. }
  1006. *props[i].conv_data = bq25890_find_idx(property,
  1007. props[i].tbl_id);
  1008. }
  1009. return 0;
  1010. }
  1011. static int bq25890_fw_probe(struct bq25890_device *bq)
  1012. {
  1013. int ret;
  1014. struct bq25890_init_data *init = &bq->init_data;
  1015. /* Optional, left at 0 if property is not present */
  1016. device_property_read_u32(bq->dev, "linux,pump-express-vbus-max",
  1017. &bq->pump_express_vbus_max);
  1018. bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset");
  1019. bq->read_back_init_data = device_property_read_bool(bq->dev,
  1020. "linux,read-back-settings");
  1021. if (bq->read_back_init_data)
  1022. return 0;
  1023. ret = bq25890_fw_read_u32_props(bq);
  1024. if (ret < 0)
  1025. return ret;
  1026. init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
  1027. init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
  1028. return 0;
  1029. }
  1030. static void bq25890_non_devm_cleanup(void *data)
  1031. {
  1032. struct bq25890_device *bq = data;
  1033. cancel_delayed_work_sync(&bq->pump_express_work);
  1034. }
  1035. static int bq25890_probe(struct i2c_client *client)
  1036. {
  1037. struct device *dev = &client->dev;
  1038. struct bq25890_device *bq;
  1039. int ret;
  1040. bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
  1041. if (!bq)
  1042. return -ENOMEM;
  1043. bq->client = client;
  1044. bq->dev = dev;
  1045. mutex_init(&bq->lock);
  1046. INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work);
  1047. bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
  1048. if (IS_ERR(bq->rmap))
  1049. return dev_err_probe(dev, PTR_ERR(bq->rmap),
  1050. "failed to allocate register map\n");
  1051. ret = devm_regmap_field_bulk_alloc(dev, bq->rmap, bq->rmap_fields,
  1052. bq25890_reg_fields, F_MAX_FIELDS);
  1053. if (ret)
  1054. return ret;
  1055. i2c_set_clientdata(client, bq);
  1056. ret = bq25890_get_chip_version(bq);
  1057. if (ret) {
  1058. dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
  1059. return ret;
  1060. }
  1061. ret = bq25890_fw_probe(bq);
  1062. if (ret < 0)
  1063. return dev_err_probe(dev, ret, "reading device properties\n");
  1064. ret = bq25890_hw_init(bq);
  1065. if (ret < 0) {
  1066. dev_err(dev, "Cannot initialize the chip: %d\n", ret);
  1067. return ret;
  1068. }
  1069. if (client->irq <= 0)
  1070. client->irq = bq25890_irq_probe(bq);
  1071. if (client->irq < 0) {
  1072. dev_err(dev, "No irq resource found.\n");
  1073. return client->irq;
  1074. }
  1075. /* OTG reporting */
  1076. bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  1077. /*
  1078. * This must be before bq25890_power_supply_init(), so that it runs
  1079. * after devm unregisters the power_supply.
  1080. */
  1081. ret = devm_add_action_or_reset(dev, bq25890_non_devm_cleanup, bq);
  1082. if (ret)
  1083. return ret;
  1084. ret = bq25890_register_regulator(bq);
  1085. if (ret)
  1086. return ret;
  1087. if (!IS_ERR_OR_NULL(bq->usb_phy)) {
  1088. INIT_WORK(&bq->usb_work, bq25890_usb_work);
  1089. bq->usb_nb.notifier_call = bq25890_usb_notifier;
  1090. usb_register_notifier(bq->usb_phy, &bq->usb_nb);
  1091. }
  1092. ret = bq25890_power_supply_init(bq);
  1093. if (ret < 0) {
  1094. dev_err(dev, "Failed to register power supply\n");
  1095. goto err_unregister_usb_notifier;
  1096. }
  1097. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  1098. bq25890_irq_handler_thread,
  1099. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1100. BQ25890_IRQ_PIN, bq);
  1101. if (ret)
  1102. goto err_unregister_usb_notifier;
  1103. return 0;
  1104. err_unregister_usb_notifier:
  1105. if (!IS_ERR_OR_NULL(bq->usb_phy))
  1106. usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
  1107. return ret;
  1108. }
  1109. static void bq25890_remove(struct i2c_client *client)
  1110. {
  1111. struct bq25890_device *bq = i2c_get_clientdata(client);
  1112. if (!IS_ERR_OR_NULL(bq->usb_phy))
  1113. usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
  1114. if (!bq->skip_reset) {
  1115. /* reset all registers to default values */
  1116. bq25890_chip_reset(bq);
  1117. }
  1118. }
  1119. static void bq25890_shutdown(struct i2c_client *client)
  1120. {
  1121. struct bq25890_device *bq = i2c_get_clientdata(client);
  1122. /*
  1123. * TODO this if + return should probably be removed, but that would
  1124. * introduce a function change for boards using the usb-phy framework.
  1125. * This needs to be tested on such a board before making this change.
  1126. */
  1127. if (!IS_ERR_OR_NULL(bq->usb_phy))
  1128. return;
  1129. /*
  1130. * Turn off the 5v Boost regulator which outputs Vbus to the device's
  1131. * Micro-USB or Type-C USB port. Leaving this on drains power and
  1132. * this avoids the PMIC on some device-models seeing this as Vbus
  1133. * getting inserted after shutdown, causing the device to immediately
  1134. * power-up again.
  1135. */
  1136. bq25890_set_otg_cfg(bq, 0);
  1137. }
  1138. #ifdef CONFIG_PM_SLEEP
  1139. static int bq25890_suspend(struct device *dev)
  1140. {
  1141. struct bq25890_device *bq = dev_get_drvdata(dev);
  1142. /*
  1143. * If charger is removed, while in suspend, make sure ADC is diabled
  1144. * since it consumes slightly more power.
  1145. */
  1146. return bq25890_field_write(bq, F_CONV_RATE, 0);
  1147. }
  1148. static int bq25890_resume(struct device *dev)
  1149. {
  1150. int ret;
  1151. struct bq25890_device *bq = dev_get_drvdata(dev);
  1152. mutex_lock(&bq->lock);
  1153. ret = bq25890_get_chip_state(bq, &bq->state);
  1154. if (ret < 0)
  1155. goto unlock;
  1156. /* Re-enable ADC only if charger is plugged in. */
  1157. if (bq->state.online) {
  1158. ret = bq25890_field_write(bq, F_CONV_RATE, 1);
  1159. if (ret < 0)
  1160. goto unlock;
  1161. }
  1162. /* signal userspace, maybe state changed while suspended */
  1163. power_supply_changed(bq->charger);
  1164. unlock:
  1165. mutex_unlock(&bq->lock);
  1166. return ret;
  1167. }
  1168. #endif
  1169. static const struct dev_pm_ops bq25890_pm = {
  1170. SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
  1171. };
  1172. static const struct i2c_device_id bq25890_i2c_ids[] = {
  1173. { "bq25890", 0 },
  1174. { "bq25892", 0 },
  1175. { "bq25895", 0 },
  1176. { "bq25896", 0 },
  1177. {},
  1178. };
  1179. MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
  1180. static const struct of_device_id bq25890_of_match[] = {
  1181. { .compatible = "ti,bq25890", },
  1182. { .compatible = "ti,bq25892", },
  1183. { .compatible = "ti,bq25895", },
  1184. { .compatible = "ti,bq25896", },
  1185. { },
  1186. };
  1187. MODULE_DEVICE_TABLE(of, bq25890_of_match);
  1188. #ifdef CONFIG_ACPI
  1189. static const struct acpi_device_id bq25890_acpi_match[] = {
  1190. {"BQ258900", 0},
  1191. {},
  1192. };
  1193. MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
  1194. #endif
  1195. static struct i2c_driver bq25890_driver = {
  1196. .driver = {
  1197. .name = "bq25890-charger",
  1198. .of_match_table = of_match_ptr(bq25890_of_match),
  1199. .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
  1200. .pm = &bq25890_pm,
  1201. },
  1202. .probe_new = bq25890_probe,
  1203. .remove = bq25890_remove,
  1204. .shutdown = bq25890_shutdown,
  1205. .id_table = bq25890_i2c_ids,
  1206. };
  1207. module_i2c_driver(bq25890_driver);
  1208. MODULE_AUTHOR("Laurentiu Palcu <[email protected]>");
  1209. MODULE_DESCRIPTION("bq25890 charger driver");
  1210. MODULE_LICENSE("GPL");