bq24257_charger.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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");