bq25890_charger.c 43 KB

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