bq24257_charger.c 29 KB

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