bq24257_charger.c 29 KB

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