bq24257_charger.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * TI BQ24257 charger driver
  3. *
  4. * Copyright (C) 2015 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Datasheets:
  17. * http://www.ti.com/product/bq24250
  18. * http://www.ti.com/product/bq24251
  19. * http://www.ti.com/product/bq24257
  20. */
  21. #include <linux/module.h>
  22. #include <linux/i2c.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/regmap.h>
  25. #include <linux/types.h>
  26. #include <linux/gpio/consumer.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/delay.h>
  29. #include <linux/acpi.h>
  30. #include <linux/of.h>
  31. #define BQ24257_REG_1 0x00
  32. #define BQ24257_REG_2 0x01
  33. #define BQ24257_REG_3 0x02
  34. #define BQ24257_REG_4 0x03
  35. #define BQ24257_REG_5 0x04
  36. #define BQ24257_REG_6 0x05
  37. #define BQ24257_REG_7 0x06
  38. #define BQ24257_MANUFACTURER "Texas Instruments"
  39. #define BQ24257_PG_GPIO "pg"
  40. #define BQ24257_ILIM_SET_DELAY 1000 /* msec */
  41. /*
  42. * When adding support for new devices make sure that enum bq2425x_chip and
  43. * bq2425x_chip_name[] always stay in sync!
  44. */
  45. enum bq2425x_chip {
  46. BQ24250,
  47. BQ24251,
  48. BQ24257,
  49. };
  50. static const char *const bq2425x_chip_name[] = {
  51. "bq24250",
  52. "bq24251",
  53. "bq24257",
  54. };
  55. enum bq24257_fields {
  56. F_WD_FAULT, F_WD_EN, F_STAT, F_FAULT, /* REG 1 */
  57. F_RESET, F_IILIMIT, F_EN_STAT, F_EN_TERM, F_CE, F_HZ_MODE, /* REG 2 */
  58. F_VBAT, F_USB_DET, /* REG 3 */
  59. F_ICHG, F_ITERM, /* REG 4 */
  60. F_LOOP_STATUS, F_LOW_CHG, F_DPDM_EN, F_CE_STATUS, F_VINDPM, /* REG 5 */
  61. F_X2_TMR_EN, F_TMR, F_SYSOFF, F_TS_EN, F_TS_STAT, /* REG 6 */
  62. F_VOVP, F_CLR_VDP, F_FORCE_BATDET, F_FORCE_PTM, /* REG 7 */
  63. F_MAX_FIELDS
  64. };
  65. /* initial field values, converted from uV/uA */
  66. struct bq24257_init_data {
  67. u8 ichg; /* charge current */
  68. u8 vbat; /* regulation voltage */
  69. u8 iterm; /* termination current */
  70. u8 iilimit; /* input current limit */
  71. u8 vovp; /* over voltage protection voltage */
  72. u8 vindpm; /* VDMP input threshold voltage */
  73. };
  74. struct bq24257_state {
  75. u8 status;
  76. u8 fault;
  77. bool power_good;
  78. };
  79. struct bq24257_device {
  80. struct i2c_client *client;
  81. struct device *dev;
  82. struct power_supply *charger;
  83. enum bq2425x_chip chip;
  84. struct regmap *rmap;
  85. struct regmap_field *rmap_fields[F_MAX_FIELDS];
  86. struct gpio_desc *pg;
  87. struct delayed_work iilimit_setup_work;
  88. struct bq24257_init_data init_data;
  89. struct bq24257_state state;
  90. struct mutex lock; /* protect state data */
  91. bool iilimit_autoset_enable;
  92. };
  93. static bool bq24257_is_volatile_reg(struct device *dev, unsigned int reg)
  94. {
  95. switch (reg) {
  96. case BQ24257_REG_2:
  97. case BQ24257_REG_4:
  98. return false;
  99. default:
  100. return true;
  101. }
  102. }
  103. static const struct regmap_config bq24257_regmap_config = {
  104. .reg_bits = 8,
  105. .val_bits = 8,
  106. .max_register = BQ24257_REG_7,
  107. .cache_type = REGCACHE_RBTREE,
  108. .volatile_reg = bq24257_is_volatile_reg,
  109. };
  110. static const struct reg_field bq24257_reg_fields[] = {
  111. /* REG 1 */
  112. [F_WD_FAULT] = REG_FIELD(BQ24257_REG_1, 7, 7),
  113. [F_WD_EN] = REG_FIELD(BQ24257_REG_1, 6, 6),
  114. [F_STAT] = REG_FIELD(BQ24257_REG_1, 4, 5),
  115. [F_FAULT] = REG_FIELD(BQ24257_REG_1, 0, 3),
  116. /* REG 2 */
  117. [F_RESET] = REG_FIELD(BQ24257_REG_2, 7, 7),
  118. [F_IILIMIT] = REG_FIELD(BQ24257_REG_2, 4, 6),
  119. [F_EN_STAT] = REG_FIELD(BQ24257_REG_2, 3, 3),
  120. [F_EN_TERM] = REG_FIELD(BQ24257_REG_2, 2, 2),
  121. [F_CE] = REG_FIELD(BQ24257_REG_2, 1, 1),
  122. [F_HZ_MODE] = REG_FIELD(BQ24257_REG_2, 0, 0),
  123. /* REG 3 */
  124. [F_VBAT] = REG_FIELD(BQ24257_REG_3, 2, 7),
  125. [F_USB_DET] = REG_FIELD(BQ24257_REG_3, 0, 1),
  126. /* REG 4 */
  127. [F_ICHG] = REG_FIELD(BQ24257_REG_4, 3, 7),
  128. [F_ITERM] = REG_FIELD(BQ24257_REG_4, 0, 2),
  129. /* REG 5 */
  130. [F_LOOP_STATUS] = REG_FIELD(BQ24257_REG_5, 6, 7),
  131. [F_LOW_CHG] = REG_FIELD(BQ24257_REG_5, 5, 5),
  132. [F_DPDM_EN] = REG_FIELD(BQ24257_REG_5, 4, 4),
  133. [F_CE_STATUS] = REG_FIELD(BQ24257_REG_5, 3, 3),
  134. [F_VINDPM] = REG_FIELD(BQ24257_REG_5, 0, 2),
  135. /* REG 6 */
  136. [F_X2_TMR_EN] = REG_FIELD(BQ24257_REG_6, 7, 7),
  137. [F_TMR] = REG_FIELD(BQ24257_REG_6, 5, 6),
  138. [F_SYSOFF] = REG_FIELD(BQ24257_REG_6, 4, 4),
  139. [F_TS_EN] = REG_FIELD(BQ24257_REG_6, 3, 3),
  140. [F_TS_STAT] = REG_FIELD(BQ24257_REG_6, 0, 2),
  141. /* REG 7 */
  142. [F_VOVP] = REG_FIELD(BQ24257_REG_7, 5, 7),
  143. [F_CLR_VDP] = REG_FIELD(BQ24257_REG_7, 4, 4),
  144. [F_FORCE_BATDET] = REG_FIELD(BQ24257_REG_7, 3, 3),
  145. [F_FORCE_PTM] = REG_FIELD(BQ24257_REG_7, 2, 2)
  146. };
  147. static const u32 bq24257_vbat_map[] = {
  148. 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
  149. 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
  150. 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
  151. 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
  152. 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
  153. 4300000, 4320000, 4340000, 4360000, 4380000, 4400000, 4420000, 4440000
  154. };
  155. #define BQ24257_VBAT_MAP_SIZE ARRAY_SIZE(bq24257_vbat_map)
  156. static const u32 bq24257_ichg_map[] = {
  157. 500000, 550000, 600000, 650000, 700000, 750000, 800000, 850000, 900000,
  158. 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000,
  159. 1350000, 1400000, 1450000, 1500000, 1550000, 1600000, 1650000, 1700000,
  160. 1750000, 1800000, 1850000, 1900000, 1950000, 2000000
  161. };
  162. #define BQ24257_ICHG_MAP_SIZE ARRAY_SIZE(bq24257_ichg_map)
  163. static const u32 bq24257_iterm_map[] = {
  164. 50000, 75000, 100000, 125000, 150000, 175000, 200000, 225000
  165. };
  166. #define BQ24257_ITERM_MAP_SIZE ARRAY_SIZE(bq24257_iterm_map)
  167. static const u32 bq24257_iilimit_map[] = {
  168. 100000, 150000, 500000, 900000, 1500000, 2000000
  169. };
  170. #define BQ24257_IILIMIT_MAP_SIZE ARRAY_SIZE(bq24257_iilimit_map)
  171. static const u32 bq24257_vovp_map[] = {
  172. 6000000, 6500000, 7000000, 8000000, 9000000, 9500000, 10000000,
  173. 10500000
  174. };
  175. #define BQ24257_VOVP_MAP_SIZE ARRAY_SIZE(bq24257_vovp_map)
  176. static const u32 bq24257_vindpm_map[] = {
  177. 4200000, 4280000, 4360000, 4440000, 4520000, 4600000, 4680000,
  178. 4760000
  179. };
  180. #define BQ24257_VINDPM_MAP_SIZE ARRAY_SIZE(bq24257_vindpm_map)
  181. static int bq24257_field_read(struct bq24257_device *bq,
  182. enum bq24257_fields field_id)
  183. {
  184. int ret;
  185. int val;
  186. ret = regmap_field_read(bq->rmap_fields[field_id], &val);
  187. if (ret < 0)
  188. return ret;
  189. return val;
  190. }
  191. static int bq24257_field_write(struct bq24257_device *bq,
  192. enum bq24257_fields field_id, u8 val)
  193. {
  194. return regmap_field_write(bq->rmap_fields[field_id], val);
  195. }
  196. static u8 bq24257_find_idx(u32 value, const u32 *map, u8 map_size)
  197. {
  198. u8 idx;
  199. for (idx = 1; idx < map_size; idx++)
  200. if (value < map[idx])
  201. break;
  202. return idx - 1;
  203. }
  204. enum bq24257_status {
  205. STATUS_READY,
  206. STATUS_CHARGE_IN_PROGRESS,
  207. STATUS_CHARGE_DONE,
  208. STATUS_FAULT,
  209. };
  210. enum bq24257_fault {
  211. FAULT_NORMAL,
  212. FAULT_INPUT_OVP,
  213. FAULT_INPUT_UVLO,
  214. FAULT_SLEEP,
  215. FAULT_BAT_TS,
  216. FAULT_BAT_OVP,
  217. FAULT_TS,
  218. FAULT_TIMER,
  219. FAULT_NO_BAT,
  220. FAULT_ISET,
  221. FAULT_INPUT_LDO_LOW,
  222. };
  223. static int bq24257_get_input_current_limit(struct bq24257_device *bq,
  224. union power_supply_propval *val)
  225. {
  226. int ret;
  227. ret = bq24257_field_read(bq, F_IILIMIT);
  228. if (ret < 0)
  229. return ret;
  230. /*
  231. * The "External ILIM" and "Production & Test" modes are not exposed
  232. * through this driver and not being covered by the lookup table.
  233. * Should such a mode have become active let's return an error rather
  234. * than exceeding the bounds of the lookup table and returning
  235. * garbage.
  236. */
  237. if (ret >= BQ24257_IILIMIT_MAP_SIZE)
  238. return -ENODATA;
  239. val->intval = bq24257_iilimit_map[ret];
  240. return 0;
  241. }
  242. static int bq24257_set_input_current_limit(struct bq24257_device *bq,
  243. const union power_supply_propval *val)
  244. {
  245. /*
  246. * Address the case where the user manually sets an input current limit
  247. * while the charger auto-detection mechanism is is active. In this
  248. * case we want to abort and go straight to the user-specified value.
  249. */
  250. if (bq->iilimit_autoset_enable)
  251. cancel_delayed_work_sync(&bq->iilimit_setup_work);
  252. return bq24257_field_write(bq, F_IILIMIT,
  253. bq24257_find_idx(val->intval,
  254. bq24257_iilimit_map,
  255. BQ24257_IILIMIT_MAP_SIZE));
  256. }
  257. static int bq24257_power_supply_get_property(struct power_supply *psy,
  258. enum power_supply_property psp,
  259. union power_supply_propval *val)
  260. {
  261. struct bq24257_device *bq = power_supply_get_drvdata(psy);
  262. struct bq24257_state state;
  263. mutex_lock(&bq->lock);
  264. state = bq->state;
  265. mutex_unlock(&bq->lock);
  266. switch (psp) {
  267. case POWER_SUPPLY_PROP_STATUS:
  268. if (!state.power_good)
  269. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  270. else if (state.status == STATUS_READY)
  271. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  272. else if (state.status == STATUS_CHARGE_IN_PROGRESS)
  273. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  274. else if (state.status == STATUS_CHARGE_DONE)
  275. val->intval = POWER_SUPPLY_STATUS_FULL;
  276. else
  277. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  278. break;
  279. case POWER_SUPPLY_PROP_MANUFACTURER:
  280. val->strval = BQ24257_MANUFACTURER;
  281. break;
  282. case POWER_SUPPLY_PROP_MODEL_NAME:
  283. val->strval = bq2425x_chip_name[bq->chip];
  284. break;
  285. case POWER_SUPPLY_PROP_ONLINE:
  286. val->intval = state.power_good;
  287. break;
  288. case POWER_SUPPLY_PROP_HEALTH:
  289. switch (state.fault) {
  290. case FAULT_NORMAL:
  291. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  292. break;
  293. case FAULT_INPUT_OVP:
  294. case FAULT_BAT_OVP:
  295. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  296. break;
  297. case FAULT_TS:
  298. case FAULT_BAT_TS:
  299. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  300. break;
  301. case FAULT_TIMER:
  302. val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  303. break;
  304. default:
  305. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  306. break;
  307. }
  308. break;
  309. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  310. val->intval = bq24257_ichg_map[bq->init_data.ichg];
  311. break;
  312. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  313. val->intval = bq24257_ichg_map[BQ24257_ICHG_MAP_SIZE - 1];
  314. break;
  315. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
  316. val->intval = bq24257_vbat_map[bq->init_data.vbat];
  317. break;
  318. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  319. val->intval = bq24257_vbat_map[BQ24257_VBAT_MAP_SIZE - 1];
  320. break;
  321. case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
  322. val->intval = bq24257_iterm_map[bq->init_data.iterm];
  323. break;
  324. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  325. return bq24257_get_input_current_limit(bq, val);
  326. default:
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. static int bq24257_power_supply_set_property(struct power_supply *psy,
  332. enum power_supply_property prop,
  333. const union power_supply_propval *val)
  334. {
  335. struct bq24257_device *bq = power_supply_get_drvdata(psy);
  336. switch (prop) {
  337. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  338. return bq24257_set_input_current_limit(bq, val);
  339. default:
  340. return -EINVAL;
  341. }
  342. }
  343. static int bq24257_power_supply_property_is_writeable(struct power_supply *psy,
  344. enum power_supply_property psp)
  345. {
  346. switch (psp) {
  347. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  348. return true;
  349. default:
  350. return false;
  351. }
  352. }
  353. static int bq24257_get_chip_state(struct bq24257_device *bq,
  354. struct bq24257_state *state)
  355. {
  356. int ret;
  357. ret = bq24257_field_read(bq, F_STAT);
  358. if (ret < 0)
  359. return ret;
  360. state->status = ret;
  361. ret = bq24257_field_read(bq, F_FAULT);
  362. if (ret < 0)
  363. return ret;
  364. state->fault = ret;
  365. if (bq->pg)
  366. state->power_good = !gpiod_get_value_cansleep(bq->pg);
  367. else
  368. /*
  369. * If we have a chip without a dedicated power-good GPIO or
  370. * some other explicit bit that would provide this information
  371. * assume the power is good if there is no supply related
  372. * fault - and not good otherwise. There is a possibility for
  373. * other errors to mask that power in fact is not good but this
  374. * is probably the best we can do here.
  375. */
  376. switch (state->fault) {
  377. case FAULT_INPUT_OVP:
  378. case FAULT_INPUT_UVLO:
  379. case FAULT_INPUT_LDO_LOW:
  380. state->power_good = false;
  381. break;
  382. default:
  383. state->power_good = true;
  384. }
  385. return 0;
  386. }
  387. static bool bq24257_state_changed(struct bq24257_device *bq,
  388. struct bq24257_state *new_state)
  389. {
  390. int ret;
  391. mutex_lock(&bq->lock);
  392. ret = (bq->state.status != new_state->status ||
  393. bq->state.fault != new_state->fault ||
  394. bq->state.power_good != new_state->power_good);
  395. mutex_unlock(&bq->lock);
  396. return ret;
  397. }
  398. enum bq24257_loop_status {
  399. LOOP_STATUS_NONE,
  400. LOOP_STATUS_IN_DPM,
  401. LOOP_STATUS_IN_CURRENT_LIMIT,
  402. LOOP_STATUS_THERMAL,
  403. };
  404. enum bq24257_in_ilimit {
  405. IILIMIT_100,
  406. IILIMIT_150,
  407. IILIMIT_500,
  408. IILIMIT_900,
  409. IILIMIT_1500,
  410. IILIMIT_2000,
  411. IILIMIT_EXT,
  412. IILIMIT_NONE,
  413. };
  414. enum bq24257_vovp {
  415. VOVP_6000,
  416. VOVP_6500,
  417. VOVP_7000,
  418. VOVP_8000,
  419. VOVP_9000,
  420. VOVP_9500,
  421. VOVP_10000,
  422. VOVP_10500
  423. };
  424. enum bq24257_vindpm {
  425. VINDPM_4200,
  426. VINDPM_4280,
  427. VINDPM_4360,
  428. VINDPM_4440,
  429. VINDPM_4520,
  430. VINDPM_4600,
  431. VINDPM_4680,
  432. VINDPM_4760
  433. };
  434. enum bq24257_port_type {
  435. PORT_TYPE_DCP, /* Dedicated Charging Port */
  436. PORT_TYPE_CDP, /* Charging Downstream Port */
  437. PORT_TYPE_SDP, /* Standard Downstream Port */
  438. PORT_TYPE_NON_STANDARD,
  439. };
  440. enum bq24257_safety_timer {
  441. SAFETY_TIMER_45,
  442. SAFETY_TIMER_360,
  443. SAFETY_TIMER_540,
  444. SAFETY_TIMER_NONE,
  445. };
  446. static int bq24257_iilimit_autoset(struct bq24257_device *bq)
  447. {
  448. int loop_status;
  449. int iilimit;
  450. int port_type;
  451. int ret;
  452. const u8 new_iilimit[] = {
  453. [PORT_TYPE_DCP] = IILIMIT_2000,
  454. [PORT_TYPE_CDP] = IILIMIT_2000,
  455. [PORT_TYPE_SDP] = IILIMIT_500,
  456. [PORT_TYPE_NON_STANDARD] = IILIMIT_500
  457. };
  458. ret = bq24257_field_read(bq, F_LOOP_STATUS);
  459. if (ret < 0)
  460. goto error;
  461. loop_status = ret;
  462. ret = bq24257_field_read(bq, F_IILIMIT);
  463. if (ret < 0)
  464. goto error;
  465. iilimit = ret;
  466. /*
  467. * All USB ports should be able to handle 500mA. If not, DPM will lower
  468. * the charging current to accommodate the power source. No need to set
  469. * a lower IILIMIT value.
  470. */
  471. if (loop_status == LOOP_STATUS_IN_DPM && iilimit == IILIMIT_500)
  472. return 0;
  473. ret = bq24257_field_read(bq, F_USB_DET);
  474. if (ret < 0)
  475. goto error;
  476. port_type = ret;
  477. ret = bq24257_field_write(bq, F_IILIMIT, new_iilimit[port_type]);
  478. if (ret < 0)
  479. goto error;
  480. ret = bq24257_field_write(bq, F_TMR, SAFETY_TIMER_360);
  481. if (ret < 0)
  482. goto error;
  483. ret = bq24257_field_write(bq, F_CLR_VDP, 1);
  484. if (ret < 0)
  485. goto error;
  486. dev_dbg(bq->dev, "port/loop = %d/%d -> iilimit = %d\n",
  487. port_type, loop_status, new_iilimit[port_type]);
  488. return 0;
  489. error:
  490. dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
  491. return ret;
  492. }
  493. static void bq24257_iilimit_setup_work(struct work_struct *work)
  494. {
  495. struct bq24257_device *bq = container_of(work, struct bq24257_device,
  496. iilimit_setup_work.work);
  497. bq24257_iilimit_autoset(bq);
  498. }
  499. static void bq24257_handle_state_change(struct bq24257_device *bq,
  500. struct bq24257_state *new_state)
  501. {
  502. int ret;
  503. struct bq24257_state old_state;
  504. mutex_lock(&bq->lock);
  505. old_state = bq->state;
  506. mutex_unlock(&bq->lock);
  507. /*
  508. * Handle BQ2425x state changes observing whether the D+/D- based input
  509. * current limit autoset functionality is enabled.
  510. */
  511. if (!new_state->power_good) {
  512. dev_dbg(bq->dev, "Power removed\n");
  513. if (bq->iilimit_autoset_enable) {
  514. cancel_delayed_work_sync(&bq->iilimit_setup_work);
  515. /* activate D+/D- port detection algorithm */
  516. ret = bq24257_field_write(bq, F_DPDM_EN, 1);
  517. if (ret < 0)
  518. goto error;
  519. }
  520. /*
  521. * When power is removed always return to the default input
  522. * current limit as configured during probe.
  523. */
  524. ret = bq24257_field_write(bq, F_IILIMIT, bq->init_data.iilimit);
  525. if (ret < 0)
  526. goto error;
  527. } else if (!old_state.power_good) {
  528. dev_dbg(bq->dev, "Power inserted\n");
  529. if (bq->iilimit_autoset_enable)
  530. /* configure input current limit */
  531. schedule_delayed_work(&bq->iilimit_setup_work,
  532. msecs_to_jiffies(BQ24257_ILIM_SET_DELAY));
  533. } else if (new_state->fault == FAULT_NO_BAT) {
  534. dev_warn(bq->dev, "Battery removed\n");
  535. } else if (new_state->fault == FAULT_TIMER) {
  536. dev_err(bq->dev, "Safety timer expired! Battery dead?\n");
  537. }
  538. return;
  539. error:
  540. dev_err(bq->dev, "%s: Error communicating with the chip.\n", __func__);
  541. }
  542. static irqreturn_t bq24257_irq_handler_thread(int irq, void *private)
  543. {
  544. int ret;
  545. struct bq24257_device *bq = private;
  546. struct bq24257_state state;
  547. ret = bq24257_get_chip_state(bq, &state);
  548. if (ret < 0)
  549. return IRQ_HANDLED;
  550. if (!bq24257_state_changed(bq, &state))
  551. return IRQ_HANDLED;
  552. dev_dbg(bq->dev, "irq(state changed): status/fault/pg = %d/%d/%d\n",
  553. state.status, state.fault, state.power_good);
  554. bq24257_handle_state_change(bq, &state);
  555. mutex_lock(&bq->lock);
  556. bq->state = state;
  557. mutex_unlock(&bq->lock);
  558. power_supply_changed(bq->charger);
  559. return IRQ_HANDLED;
  560. }
  561. static int bq24257_hw_init(struct bq24257_device *bq)
  562. {
  563. int ret;
  564. int i;
  565. struct bq24257_state state;
  566. const struct {
  567. int field;
  568. u32 value;
  569. } init_data[] = {
  570. {F_ICHG, bq->init_data.ichg},
  571. {F_VBAT, bq->init_data.vbat},
  572. {F_ITERM, bq->init_data.iterm},
  573. {F_VOVP, bq->init_data.vovp},
  574. {F_VINDPM, bq->init_data.vindpm},
  575. };
  576. /*
  577. * Disable the watchdog timer to prevent the IC from going back to
  578. * default settings after 50 seconds of I2C inactivity.
  579. */
  580. ret = bq24257_field_write(bq, F_WD_EN, 0);
  581. if (ret < 0)
  582. return ret;
  583. /* configure the charge currents and voltages */
  584. for (i = 0; i < ARRAY_SIZE(init_data); i++) {
  585. ret = bq24257_field_write(bq, init_data[i].field,
  586. init_data[i].value);
  587. if (ret < 0)
  588. return ret;
  589. }
  590. ret = bq24257_get_chip_state(bq, &state);
  591. if (ret < 0)
  592. return ret;
  593. mutex_lock(&bq->lock);
  594. bq->state = state;
  595. mutex_unlock(&bq->lock);
  596. if (!bq->iilimit_autoset_enable) {
  597. dev_dbg(bq->dev, "manually setting iilimit = %u\n",
  598. bq->init_data.iilimit);
  599. /* program fixed input current limit */
  600. ret = bq24257_field_write(bq, F_IILIMIT,
  601. bq->init_data.iilimit);
  602. if (ret < 0)
  603. return ret;
  604. } else if (!state.power_good)
  605. /* activate D+/D- detection algorithm */
  606. ret = bq24257_field_write(bq, F_DPDM_EN, 1);
  607. else if (state.fault != FAULT_NO_BAT)
  608. ret = bq24257_iilimit_autoset(bq);
  609. return ret;
  610. }
  611. static enum power_supply_property bq24257_power_supply_props[] = {
  612. POWER_SUPPLY_PROP_MANUFACTURER,
  613. POWER_SUPPLY_PROP_MODEL_NAME,
  614. POWER_SUPPLY_PROP_STATUS,
  615. POWER_SUPPLY_PROP_ONLINE,
  616. POWER_SUPPLY_PROP_HEALTH,
  617. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  618. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  619. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
  620. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  621. POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
  622. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  623. };
  624. static char *bq24257_charger_supplied_to[] = {
  625. "main-battery",
  626. };
  627. static const struct power_supply_desc bq24257_power_supply_desc = {
  628. .name = "bq24257-charger",
  629. .type = POWER_SUPPLY_TYPE_USB,
  630. .properties = bq24257_power_supply_props,
  631. .num_properties = ARRAY_SIZE(bq24257_power_supply_props),
  632. .get_property = bq24257_power_supply_get_property,
  633. .set_property = bq24257_power_supply_set_property,
  634. .property_is_writeable = bq24257_power_supply_property_is_writeable,
  635. };
  636. static ssize_t bq24257_show_ovp_voltage(struct device *dev,
  637. struct device_attribute *attr,
  638. char *buf)
  639. {
  640. struct power_supply *psy = dev_get_drvdata(dev);
  641. struct bq24257_device *bq = power_supply_get_drvdata(psy);
  642. return scnprintf(buf, PAGE_SIZE, "%u\n",
  643. bq24257_vovp_map[bq->init_data.vovp]);
  644. }
  645. static ssize_t bq24257_show_in_dpm_voltage(struct device *dev,
  646. struct device_attribute *attr,
  647. char *buf)
  648. {
  649. struct power_supply *psy = dev_get_drvdata(dev);
  650. struct bq24257_device *bq = power_supply_get_drvdata(psy);
  651. return scnprintf(buf, PAGE_SIZE, "%u\n",
  652. bq24257_vindpm_map[bq->init_data.vindpm]);
  653. }
  654. static ssize_t bq24257_sysfs_show_enable(struct device *dev,
  655. struct device_attribute *attr,
  656. char *buf)
  657. {
  658. struct power_supply *psy = dev_get_drvdata(dev);
  659. struct bq24257_device *bq = power_supply_get_drvdata(psy);
  660. int ret;
  661. if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
  662. ret = bq24257_field_read(bq, F_HZ_MODE);
  663. else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
  664. ret = bq24257_field_read(bq, F_SYSOFF);
  665. else
  666. return -EINVAL;
  667. if (ret < 0)
  668. return ret;
  669. return scnprintf(buf, PAGE_SIZE, "%d\n", ret);
  670. }
  671. static ssize_t bq24257_sysfs_set_enable(struct device *dev,
  672. struct device_attribute *attr,
  673. const char *buf,
  674. size_t count)
  675. {
  676. struct power_supply *psy = dev_get_drvdata(dev);
  677. struct bq24257_device *bq = power_supply_get_drvdata(psy);
  678. long val;
  679. int ret;
  680. if (kstrtol(buf, 10, &val) < 0)
  681. return -EINVAL;
  682. if (strcmp(attr->attr.name, "high_impedance_enable") == 0)
  683. ret = bq24257_field_write(bq, F_HZ_MODE, (bool)val);
  684. else if (strcmp(attr->attr.name, "sysoff_enable") == 0)
  685. ret = bq24257_field_write(bq, F_SYSOFF, (bool)val);
  686. else
  687. return -EINVAL;
  688. if (ret < 0)
  689. return ret;
  690. return count;
  691. }
  692. static DEVICE_ATTR(ovp_voltage, S_IRUGO, bq24257_show_ovp_voltage, NULL);
  693. static DEVICE_ATTR(in_dpm_voltage, S_IRUGO, bq24257_show_in_dpm_voltage, NULL);
  694. static DEVICE_ATTR(high_impedance_enable, S_IWUSR | S_IRUGO,
  695. bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
  696. static DEVICE_ATTR(sysoff_enable, S_IWUSR | S_IRUGO,
  697. bq24257_sysfs_show_enable, bq24257_sysfs_set_enable);
  698. static struct attribute *bq24257_charger_attr[] = {
  699. &dev_attr_ovp_voltage.attr,
  700. &dev_attr_in_dpm_voltage.attr,
  701. &dev_attr_high_impedance_enable.attr,
  702. &dev_attr_sysoff_enable.attr,
  703. NULL,
  704. };
  705. static const struct attribute_group bq24257_attr_group = {
  706. .attrs = bq24257_charger_attr,
  707. };
  708. static int bq24257_power_supply_init(struct bq24257_device *bq)
  709. {
  710. struct power_supply_config psy_cfg = { .drv_data = bq, };
  711. psy_cfg.supplied_to = bq24257_charger_supplied_to;
  712. psy_cfg.num_supplicants = ARRAY_SIZE(bq24257_charger_supplied_to);
  713. bq->charger = devm_power_supply_register(bq->dev,
  714. &bq24257_power_supply_desc,
  715. &psy_cfg);
  716. return PTR_ERR_OR_ZERO(bq->charger);
  717. }
  718. static void bq24257_pg_gpio_probe(struct bq24257_device *bq)
  719. {
  720. bq->pg = devm_gpiod_get_optional(bq->dev, BQ24257_PG_GPIO, GPIOD_IN);
  721. if (PTR_ERR(bq->pg) == -EPROBE_DEFER) {
  722. dev_info(bq->dev, "probe retry requested for PG pin\n");
  723. return;
  724. } else if (IS_ERR(bq->pg)) {
  725. dev_err(bq->dev, "error probing PG pin\n");
  726. bq->pg = NULL;
  727. return;
  728. }
  729. if (bq->pg)
  730. dev_dbg(bq->dev, "probed PG pin = %d\n", desc_to_gpio(bq->pg));
  731. }
  732. static int bq24257_fw_probe(struct bq24257_device *bq)
  733. {
  734. int ret;
  735. u32 property;
  736. /* Required properties */
  737. ret = device_property_read_u32(bq->dev, "ti,charge-current", &property);
  738. if (ret < 0)
  739. return ret;
  740. bq->init_data.ichg = bq24257_find_idx(property, bq24257_ichg_map,
  741. BQ24257_ICHG_MAP_SIZE);
  742. ret = device_property_read_u32(bq->dev, "ti,battery-regulation-voltage",
  743. &property);
  744. if (ret < 0)
  745. return ret;
  746. bq->init_data.vbat = bq24257_find_idx(property, bq24257_vbat_map,
  747. BQ24257_VBAT_MAP_SIZE);
  748. ret = device_property_read_u32(bq->dev, "ti,termination-current",
  749. &property);
  750. if (ret < 0)
  751. return ret;
  752. bq->init_data.iterm = bq24257_find_idx(property, bq24257_iterm_map,
  753. BQ24257_ITERM_MAP_SIZE);
  754. /* Optional properties. If not provided use reasonable default. */
  755. ret = device_property_read_u32(bq->dev, "ti,current-limit",
  756. &property);
  757. if (ret < 0) {
  758. bq->iilimit_autoset_enable = true;
  759. /*
  760. * Explicitly set a default value which will be needed for
  761. * devices that don't support the automatic setting of the input
  762. * current limit through the charger type detection mechanism.
  763. */
  764. bq->init_data.iilimit = IILIMIT_500;
  765. } else
  766. bq->init_data.iilimit =
  767. bq24257_find_idx(property,
  768. bq24257_iilimit_map,
  769. BQ24257_IILIMIT_MAP_SIZE);
  770. ret = device_property_read_u32(bq->dev, "ti,ovp-voltage",
  771. &property);
  772. if (ret < 0)
  773. bq->init_data.vovp = VOVP_6500;
  774. else
  775. bq->init_data.vovp = bq24257_find_idx(property,
  776. bq24257_vovp_map,
  777. BQ24257_VOVP_MAP_SIZE);
  778. ret = device_property_read_u32(bq->dev, "ti,in-dpm-voltage",
  779. &property);
  780. if (ret < 0)
  781. bq->init_data.vindpm = VINDPM_4360;
  782. else
  783. bq->init_data.vindpm =
  784. bq24257_find_idx(property,
  785. bq24257_vindpm_map,
  786. BQ24257_VINDPM_MAP_SIZE);
  787. return 0;
  788. }
  789. static int bq24257_probe(struct i2c_client *client,
  790. const struct i2c_device_id *id)
  791. {
  792. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  793. struct device *dev = &client->dev;
  794. const struct acpi_device_id *acpi_id;
  795. struct bq24257_device *bq;
  796. int ret;
  797. int i;
  798. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  799. dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
  800. return -ENODEV;
  801. }
  802. bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
  803. if (!bq)
  804. return -ENOMEM;
  805. bq->client = client;
  806. bq->dev = dev;
  807. if (ACPI_HANDLE(dev)) {
  808. acpi_id = acpi_match_device(dev->driver->acpi_match_table,
  809. &client->dev);
  810. if (!acpi_id) {
  811. dev_err(dev, "Failed to match ACPI device\n");
  812. return -ENODEV;
  813. }
  814. bq->chip = (enum bq2425x_chip)acpi_id->driver_data;
  815. } else {
  816. bq->chip = (enum bq2425x_chip)id->driver_data;
  817. }
  818. mutex_init(&bq->lock);
  819. bq->rmap = devm_regmap_init_i2c(client, &bq24257_regmap_config);
  820. if (IS_ERR(bq->rmap)) {
  821. dev_err(dev, "failed to allocate register map\n");
  822. return PTR_ERR(bq->rmap);
  823. }
  824. for (i = 0; i < ARRAY_SIZE(bq24257_reg_fields); i++) {
  825. const struct reg_field *reg_fields = bq24257_reg_fields;
  826. bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
  827. reg_fields[i]);
  828. if (IS_ERR(bq->rmap_fields[i])) {
  829. dev_err(dev, "cannot allocate regmap field\n");
  830. return PTR_ERR(bq->rmap_fields[i]);
  831. }
  832. }
  833. i2c_set_clientdata(client, bq);
  834. if (!dev->platform_data) {
  835. ret = bq24257_fw_probe(bq);
  836. if (ret < 0) {
  837. dev_err(dev, "Cannot read device properties.\n");
  838. return ret;
  839. }
  840. } else {
  841. return -ENODEV;
  842. }
  843. /*
  844. * The BQ24250 doesn't support the D+/D- based charger type detection
  845. * used for the automatic setting of the input current limit setting so
  846. * explicitly disable that feature.
  847. */
  848. if (bq->chip == BQ24250)
  849. bq->iilimit_autoset_enable = false;
  850. if (bq->iilimit_autoset_enable)
  851. INIT_DELAYED_WORK(&bq->iilimit_setup_work,
  852. bq24257_iilimit_setup_work);
  853. /*
  854. * The BQ24250 doesn't have a dedicated Power Good (PG) pin so let's
  855. * not probe for it and instead use a SW-based approach to determine
  856. * the PG state. We also use a SW-based approach for all other devices
  857. * if the PG pin is either not defined or can't be probed.
  858. */
  859. if (bq->chip != BQ24250)
  860. bq24257_pg_gpio_probe(bq);
  861. if (PTR_ERR(bq->pg) == -EPROBE_DEFER)
  862. return PTR_ERR(bq->pg);
  863. else if (!bq->pg)
  864. dev_info(bq->dev, "using SW-based power-good detection\n");
  865. /* reset all registers to defaults */
  866. ret = bq24257_field_write(bq, F_RESET, 1);
  867. if (ret < 0)
  868. return ret;
  869. /*
  870. * Put the RESET bit back to 0, in cache. For some reason the HW always
  871. * returns 1 on this bit, so this is the only way to avoid resetting the
  872. * chip every time we update another field in this register.
  873. */
  874. ret = bq24257_field_write(bq, F_RESET, 0);
  875. if (ret < 0)
  876. return ret;
  877. ret = bq24257_hw_init(bq);
  878. if (ret < 0) {
  879. dev_err(dev, "Cannot initialize the chip.\n");
  880. return ret;
  881. }
  882. ret = bq24257_power_supply_init(bq);
  883. if (ret < 0) {
  884. dev_err(dev, "Failed to register power supply\n");
  885. return ret;
  886. }
  887. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  888. bq24257_irq_handler_thread,
  889. IRQF_TRIGGER_FALLING |
  890. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  891. bq2425x_chip_name[bq->chip], bq);
  892. if (ret) {
  893. dev_err(dev, "Failed to request IRQ #%d\n", client->irq);
  894. return ret;
  895. }
  896. ret = sysfs_create_group(&bq->charger->dev.kobj, &bq24257_attr_group);
  897. if (ret < 0) {
  898. dev_err(dev, "Can't create sysfs entries\n");
  899. return ret;
  900. }
  901. return 0;
  902. }
  903. static int bq24257_remove(struct i2c_client *client)
  904. {
  905. struct bq24257_device *bq = i2c_get_clientdata(client);
  906. if (bq->iilimit_autoset_enable)
  907. cancel_delayed_work_sync(&bq->iilimit_setup_work);
  908. sysfs_remove_group(&bq->charger->dev.kobj, &bq24257_attr_group);
  909. bq24257_field_write(bq, F_RESET, 1); /* reset to defaults */
  910. return 0;
  911. }
  912. #ifdef CONFIG_PM_SLEEP
  913. static int bq24257_suspend(struct device *dev)
  914. {
  915. struct bq24257_device *bq = dev_get_drvdata(dev);
  916. int ret = 0;
  917. if (bq->iilimit_autoset_enable)
  918. cancel_delayed_work_sync(&bq->iilimit_setup_work);
  919. /* reset all registers to default (and activate standalone mode) */
  920. ret = bq24257_field_write(bq, F_RESET, 1);
  921. if (ret < 0)
  922. dev_err(bq->dev, "Cannot reset chip to standalone mode.\n");
  923. return ret;
  924. }
  925. static int bq24257_resume(struct device *dev)
  926. {
  927. int ret;
  928. struct bq24257_device *bq = dev_get_drvdata(dev);
  929. ret = regcache_drop_region(bq->rmap, BQ24257_REG_1, BQ24257_REG_7);
  930. if (ret < 0)
  931. return ret;
  932. ret = bq24257_field_write(bq, F_RESET, 0);
  933. if (ret < 0)
  934. return ret;
  935. ret = bq24257_hw_init(bq);
  936. if (ret < 0) {
  937. dev_err(bq->dev, "Cannot init chip after resume.\n");
  938. return ret;
  939. }
  940. /* signal userspace, maybe state changed while suspended */
  941. power_supply_changed(bq->charger);
  942. return 0;
  943. }
  944. #endif
  945. static const struct dev_pm_ops bq24257_pm = {
  946. SET_SYSTEM_SLEEP_PM_OPS(bq24257_suspend, bq24257_resume)
  947. };
  948. static const struct i2c_device_id bq24257_i2c_ids[] = {
  949. { "bq24250", BQ24250 },
  950. { "bq24251", BQ24251 },
  951. { "bq24257", BQ24257 },
  952. {},
  953. };
  954. MODULE_DEVICE_TABLE(i2c, bq24257_i2c_ids);
  955. static const struct of_device_id bq24257_of_match[] = {
  956. { .compatible = "ti,bq24250", },
  957. { .compatible = "ti,bq24251", },
  958. { .compatible = "ti,bq24257", },
  959. { },
  960. };
  961. MODULE_DEVICE_TABLE(of, bq24257_of_match);
  962. static const struct acpi_device_id bq24257_acpi_match[] = {
  963. { "BQ242500", BQ24250 },
  964. { "BQ242510", BQ24251 },
  965. { "BQ242570", BQ24257 },
  966. {},
  967. };
  968. MODULE_DEVICE_TABLE(acpi, bq24257_acpi_match);
  969. static struct i2c_driver bq24257_driver = {
  970. .driver = {
  971. .name = "bq24257-charger",
  972. .of_match_table = of_match_ptr(bq24257_of_match),
  973. .acpi_match_table = ACPI_PTR(bq24257_acpi_match),
  974. .pm = &bq24257_pm,
  975. },
  976. .probe = bq24257_probe,
  977. .remove = bq24257_remove,
  978. .id_table = bq24257_i2c_ids,
  979. };
  980. module_i2c_driver(bq24257_driver);
  981. MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
  982. MODULE_DESCRIPTION("bq24257 charger driver");
  983. MODULE_LICENSE("GPL");