bd70528-charger.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // Copyright (C) 2018 ROHM Semiconductors
  4. //
  5. // power-supply driver for ROHM BD70528 PMIC
  6. /*
  7. * BD70528 charger HW state machine.
  8. *
  9. * The thermal shutdown state is not drawn. From any other state but
  10. * battery error and suspend it is possible to go to TSD/TMP states
  11. * if temperature is out of bounds.
  12. *
  13. * CHG_RST = H
  14. * or CHG_EN=L
  15. * or (DCIN2_UVLO=L && DCIN1_UVLO=L)
  16. * or (DCIN2_OVLO=H & DCIN1_UVKLO=L)
  17. *
  18. * +--------------+ +--------------+
  19. * | | | |
  20. * | Any state +-------> | Suspend |
  21. * | | | |
  22. * +--------------+ +------+-------+
  23. * |
  24. * CHG_EN = H && BAT_DET = H && |
  25. * No errors (temp, bat_ov, UVLO, |
  26. * OVLO...) |
  27. * |
  28. * BAT_OV or +---------v----------+
  29. * (DBAT && TTRI) | |
  30. * +-----------------+ Trickle Charge | <---------------+
  31. * | | | |
  32. * | +-------+------------+ |
  33. * | | |
  34. * | | ^ |
  35. * | V_BAT > VTRI_TH | | VBAT < VTRI_TH - 50mV |
  36. * | | | |
  37. * | v | |
  38. * | | |
  39. * | BAT_OV or +----------+----+ |
  40. * | (DBAT && TFST) | | |
  41. * | +----------------+ Fast Charge | |
  42. * | | | | |
  43. * v v +----+----------+ |
  44. * | |
  45. *+----------------+ ILIM_DET=L | ^ ILIM_DET |
  46. *| | & CV_DET=H | | or CV_DET=L |
  47. *| Battery Error | & VBAT > | | or VBAT < VRECHG_TH |
  48. *| | VRECHG_TH | | or IBAT > IFST/x |
  49. *+----------------+ & IBAT < | | |
  50. * IFST/x v | |
  51. * ^ | |
  52. * | +---------+-+ |
  53. * | | | |
  54. * +-------------------+ Top OFF | |
  55. * BAT_OV = H or | | |
  56. * (DBAT && TFST) +-----+-----+ |
  57. * | |
  58. * Stay top-off for 15s | |
  59. * v |
  60. * |
  61. * +--------+ |
  62. * | | |
  63. * | Done +-------------------------+
  64. * | |
  65. * +--------+ VBAT < VRECHG_TH
  66. */
  67. #include <linux/kernel.h>
  68. #include <linux/interrupt.h>
  69. #include <linux/mfd/rohm-bd70528.h>
  70. #include <linux/module.h>
  71. #include <linux/platform_device.h>
  72. #include <linux/power_supply.h>
  73. #include <linux/linear_range.h>
  74. #define CHG_STAT_SUSPEND 0x0
  75. #define CHG_STAT_TRICKLE 0x1
  76. #define CHG_STAT_FAST 0x3
  77. #define CHG_STAT_TOPOFF 0xe
  78. #define CHG_STAT_DONE 0xf
  79. #define CHG_STAT_OTP_TRICKLE 0x10
  80. #define CHG_STAT_OTP_FAST 0x11
  81. #define CHG_STAT_OTP_DONE 0x12
  82. #define CHG_STAT_TSD_TRICKLE 0x20
  83. #define CHG_STAT_TSD_FAST 0x21
  84. #define CHG_STAT_TSD_TOPOFF 0x22
  85. #define CHG_STAT_BAT_ERR 0x7f
  86. static const char *bd70528_charger_model = "BD70528";
  87. static const char *bd70528_charger_manufacturer = "ROHM Semiconductors";
  88. #define BD_ERR_IRQ_HND(_name_, _wrn_) \
  89. static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg) \
  90. { \
  91. struct power_supply *psy = (struct power_supply *)arg; \
  92. \
  93. power_supply_changed(psy); \
  94. dev_err(&psy->dev, (_wrn_)); \
  95. \
  96. return IRQ_HANDLED; \
  97. }
  98. #define BD_INFO_IRQ_HND(_name_, _wrn_) \
  99. static irqreturn_t bd0528_##_name_##_interrupt(int irq, void *arg) \
  100. { \
  101. struct power_supply *psy = (struct power_supply *)arg; \
  102. \
  103. power_supply_changed(psy); \
  104. dev_dbg(&psy->dev, (_wrn_)); \
  105. \
  106. return IRQ_HANDLED; \
  107. }
  108. #define BD_IRQ_HND(_name_) bd0528_##_name_##_interrupt
  109. struct bd70528_psy {
  110. struct regmap *regmap;
  111. struct device *dev;
  112. struct power_supply *psy;
  113. };
  114. BD_ERR_IRQ_HND(BAT_OV_DET, "Battery overvoltage detected\n");
  115. BD_ERR_IRQ_HND(DBAT_DET, "Dead battery detected\n");
  116. BD_ERR_IRQ_HND(COLD_DET, "Battery cold\n");
  117. BD_ERR_IRQ_HND(HOT_DET, "Battery hot\n");
  118. BD_ERR_IRQ_HND(CHG_TSD, "Charger thermal shutdown\n");
  119. BD_ERR_IRQ_HND(DCIN2_OV_DET, "DCIN2 overvoltage detected\n");
  120. BD_INFO_IRQ_HND(BAT_OV_RES, "Battery voltage back to normal\n");
  121. BD_INFO_IRQ_HND(COLD_RES, "Battery temperature back to normal\n");
  122. BD_INFO_IRQ_HND(HOT_RES, "Battery temperature back to normal\n");
  123. BD_INFO_IRQ_HND(BAT_RMV, "Battery removed\n");
  124. BD_INFO_IRQ_HND(BAT_DET, "Battery detected\n");
  125. BD_INFO_IRQ_HND(DCIN2_OV_RES, "DCIN2 voltage back to normal\n");
  126. BD_INFO_IRQ_HND(DCIN2_RMV, "DCIN2 removed\n");
  127. BD_INFO_IRQ_HND(DCIN2_DET, "DCIN2 detected\n");
  128. BD_INFO_IRQ_HND(DCIN1_RMV, "DCIN1 removed\n");
  129. BD_INFO_IRQ_HND(DCIN1_DET, "DCIN1 detected\n");
  130. struct irq_name_pair {
  131. const char *n;
  132. irqreturn_t (*h)(int irq, void *arg);
  133. };
  134. static int bd70528_get_irqs(struct platform_device *pdev,
  135. struct bd70528_psy *bdpsy)
  136. {
  137. int irq, i, ret;
  138. unsigned int mask;
  139. static const struct irq_name_pair bd70528_chg_irqs[] = {
  140. { .n = "bd70528-bat-ov-res", .h = BD_IRQ_HND(BAT_OV_RES) },
  141. { .n = "bd70528-bat-ov-det", .h = BD_IRQ_HND(BAT_OV_DET) },
  142. { .n = "bd70528-bat-dead", .h = BD_IRQ_HND(DBAT_DET) },
  143. { .n = "bd70528-bat-warmed", .h = BD_IRQ_HND(COLD_RES) },
  144. { .n = "bd70528-bat-cold", .h = BD_IRQ_HND(COLD_DET) },
  145. { .n = "bd70528-bat-cooled", .h = BD_IRQ_HND(HOT_RES) },
  146. { .n = "bd70528-bat-hot", .h = BD_IRQ_HND(HOT_DET) },
  147. { .n = "bd70528-chg-tshd", .h = BD_IRQ_HND(CHG_TSD) },
  148. { .n = "bd70528-bat-removed", .h = BD_IRQ_HND(BAT_RMV) },
  149. { .n = "bd70528-bat-detected", .h = BD_IRQ_HND(BAT_DET) },
  150. { .n = "bd70528-dcin2-ov-res", .h = BD_IRQ_HND(DCIN2_OV_RES) },
  151. { .n = "bd70528-dcin2-ov-det", .h = BD_IRQ_HND(DCIN2_OV_DET) },
  152. { .n = "bd70528-dcin2-removed", .h = BD_IRQ_HND(DCIN2_RMV) },
  153. { .n = "bd70528-dcin2-detected", .h = BD_IRQ_HND(DCIN2_DET) },
  154. { .n = "bd70528-dcin1-removed", .h = BD_IRQ_HND(DCIN1_RMV) },
  155. { .n = "bd70528-dcin1-detected", .h = BD_IRQ_HND(DCIN1_DET) },
  156. };
  157. for (i = 0; i < ARRAY_SIZE(bd70528_chg_irqs); i++) {
  158. irq = platform_get_irq_byname(pdev, bd70528_chg_irqs[i].n);
  159. if (irq < 0) {
  160. dev_err(&pdev->dev, "Bad IRQ information for %s (%d)\n",
  161. bd70528_chg_irqs[i].n, irq);
  162. return irq;
  163. }
  164. ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  165. bd70528_chg_irqs[i].h,
  166. IRQF_ONESHOT,
  167. bd70528_chg_irqs[i].n,
  168. bdpsy->psy);
  169. if (ret)
  170. return ret;
  171. }
  172. /*
  173. * BD70528 irq controller is not touching the main mask register.
  174. * So enable the charger block interrupts at main level. We can just
  175. * leave them enabled as irq-controller should disable irqs
  176. * from sub-registers when IRQ is disabled or freed.
  177. */
  178. mask = BD70528_REG_INT_BAT1_MASK | BD70528_REG_INT_BAT2_MASK;
  179. ret = regmap_update_bits(bdpsy->regmap,
  180. BD70528_REG_INT_MAIN_MASK, mask, 0);
  181. if (ret)
  182. dev_err(&pdev->dev, "Failed to enable charger IRQs\n");
  183. return ret;
  184. }
  185. static int bd70528_get_charger_status(struct bd70528_psy *bdpsy, int *val)
  186. {
  187. int ret;
  188. unsigned int v;
  189. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CURR_STAT, &v);
  190. if (ret) {
  191. dev_err(bdpsy->dev, "Charger state read failure %d\n",
  192. ret);
  193. return ret;
  194. }
  195. switch (v & BD70528_MASK_CHG_STAT) {
  196. case CHG_STAT_SUSPEND:
  197. /* Maybe we should check the CHG_TTRI_EN? */
  198. case CHG_STAT_OTP_TRICKLE:
  199. case CHG_STAT_OTP_FAST:
  200. case CHG_STAT_OTP_DONE:
  201. case CHG_STAT_TSD_TRICKLE:
  202. case CHG_STAT_TSD_FAST:
  203. case CHG_STAT_TSD_TOPOFF:
  204. case CHG_STAT_BAT_ERR:
  205. *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
  206. break;
  207. case CHG_STAT_DONE:
  208. *val = POWER_SUPPLY_STATUS_FULL;
  209. break;
  210. case CHG_STAT_TRICKLE:
  211. case CHG_STAT_FAST:
  212. case CHG_STAT_TOPOFF:
  213. *val = POWER_SUPPLY_STATUS_CHARGING;
  214. break;
  215. default:
  216. *val = POWER_SUPPLY_STATUS_UNKNOWN;
  217. break;
  218. }
  219. return 0;
  220. }
  221. static int bd70528_get_charge_type(struct bd70528_psy *bdpsy, int *val)
  222. {
  223. int ret;
  224. unsigned int v;
  225. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CURR_STAT, &v);
  226. if (ret) {
  227. dev_err(bdpsy->dev, "Charger state read failure %d\n",
  228. ret);
  229. return ret;
  230. }
  231. switch (v & BD70528_MASK_CHG_STAT) {
  232. case CHG_STAT_TRICKLE:
  233. *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  234. break;
  235. case CHG_STAT_FAST:
  236. case CHG_STAT_TOPOFF:
  237. *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
  238. break;
  239. case CHG_STAT_DONE:
  240. case CHG_STAT_SUSPEND:
  241. /* Maybe we should check the CHG_TTRI_EN? */
  242. case CHG_STAT_OTP_TRICKLE:
  243. case CHG_STAT_OTP_FAST:
  244. case CHG_STAT_OTP_DONE:
  245. case CHG_STAT_TSD_TRICKLE:
  246. case CHG_STAT_TSD_FAST:
  247. case CHG_STAT_TSD_TOPOFF:
  248. case CHG_STAT_BAT_ERR:
  249. *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
  250. break;
  251. default:
  252. *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  253. break;
  254. }
  255. return 0;
  256. }
  257. static int bd70528_get_battery_health(struct bd70528_psy *bdpsy, int *val)
  258. {
  259. int ret;
  260. unsigned int v;
  261. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_BAT_STAT, &v);
  262. if (ret) {
  263. dev_err(bdpsy->dev, "Battery state read failure %d\n",
  264. ret);
  265. return ret;
  266. }
  267. /* No battery? */
  268. if (!(v & BD70528_MASK_CHG_BAT_DETECT))
  269. *val = POWER_SUPPLY_HEALTH_DEAD;
  270. else if (v & BD70528_MASK_CHG_BAT_OVERVOLT)
  271. *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  272. else if (v & BD70528_MASK_CHG_BAT_TIMER)
  273. *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  274. else
  275. *val = POWER_SUPPLY_HEALTH_GOOD;
  276. return 0;
  277. }
  278. static int bd70528_get_online(struct bd70528_psy *bdpsy, int *val)
  279. {
  280. int ret;
  281. unsigned int v;
  282. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_IN_STAT, &v);
  283. if (ret) {
  284. dev_err(bdpsy->dev, "DC1 IN state read failure %d\n",
  285. ret);
  286. return ret;
  287. }
  288. *val = (v & BD70528_MASK_CHG_DCIN1_UVLO) ? 1 : 0;
  289. return 0;
  290. }
  291. static int bd70528_get_present(struct bd70528_psy *bdpsy, int *val)
  292. {
  293. int ret;
  294. unsigned int v;
  295. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_BAT_STAT, &v);
  296. if (ret) {
  297. dev_err(bdpsy->dev, "Battery state read failure %d\n",
  298. ret);
  299. return ret;
  300. }
  301. *val = (v & BD70528_MASK_CHG_BAT_DETECT) ? 1 : 0;
  302. return 0;
  303. }
  304. static const struct linear_range current_limit_ranges[] = {
  305. {
  306. .min = 5,
  307. .step = 1,
  308. .min_sel = 0,
  309. .max_sel = 0x22,
  310. },
  311. {
  312. .min = 40,
  313. .step = 5,
  314. .min_sel = 0x23,
  315. .max_sel = 0x26,
  316. },
  317. {
  318. .min = 60,
  319. .step = 20,
  320. .min_sel = 0x27,
  321. .max_sel = 0x2d,
  322. },
  323. {
  324. .min = 200,
  325. .step = 50,
  326. .min_sel = 0x2e,
  327. .max_sel = 0x34,
  328. },
  329. {
  330. .min = 500,
  331. .step = 0,
  332. .min_sel = 0x35,
  333. .max_sel = 0x3f,
  334. },
  335. };
  336. /*
  337. * BD70528 would support setting and getting own charge current/
  338. * voltage for low temperatures. The driver currently only reads
  339. * the charge current at room temperature. We do set both though.
  340. */
  341. static const struct linear_range warm_charge_curr[] = {
  342. {
  343. .min = 10,
  344. .step = 10,
  345. .min_sel = 0,
  346. .max_sel = 0x12,
  347. },
  348. {
  349. .min = 200,
  350. .step = 25,
  351. .min_sel = 0x13,
  352. .max_sel = 0x1f,
  353. },
  354. };
  355. /*
  356. * Cold charge current selectors are identical to warm charge current
  357. * selectors. The difference is that only smaller currents are available
  358. * at cold charge range.
  359. */
  360. #define MAX_COLD_CHG_CURR_SEL 0x15
  361. #define MAX_WARM_CHG_CURR_SEL 0x1f
  362. #define MIN_CHG_CURR_SEL 0x0
  363. static int get_charge_current(struct bd70528_psy *bdpsy, int *ma)
  364. {
  365. unsigned int sel;
  366. int ret;
  367. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_CHG_CURR_WARM,
  368. &sel);
  369. if (ret) {
  370. dev_err(bdpsy->dev,
  371. "Charge current reading failed (%d)\n", ret);
  372. return ret;
  373. }
  374. sel &= BD70528_MASK_CHG_CHG_CURR;
  375. ret = linear_range_get_value_array(&warm_charge_curr[0],
  376. ARRAY_SIZE(warm_charge_curr),
  377. sel, ma);
  378. if (ret) {
  379. dev_err(bdpsy->dev,
  380. "Unknown charge current value 0x%x\n",
  381. sel);
  382. }
  383. return ret;
  384. }
  385. static int get_current_limit(struct bd70528_psy *bdpsy, int *ma)
  386. {
  387. unsigned int sel;
  388. int ret;
  389. ret = regmap_read(bdpsy->regmap, BD70528_REG_CHG_DCIN_ILIM,
  390. &sel);
  391. if (ret) {
  392. dev_err(bdpsy->dev,
  393. "Input current limit reading failed (%d)\n", ret);
  394. return ret;
  395. }
  396. sel &= BD70528_MASK_CHG_DCIN_ILIM;
  397. ret = linear_range_get_value_array(&current_limit_ranges[0],
  398. ARRAY_SIZE(current_limit_ranges),
  399. sel, ma);
  400. if (ret) {
  401. /* Unspecified values mean 500 mA */
  402. *ma = 500;
  403. }
  404. return 0;
  405. }
  406. static enum power_supply_property bd70528_charger_props[] = {
  407. POWER_SUPPLY_PROP_STATUS,
  408. POWER_SUPPLY_PROP_CHARGE_TYPE,
  409. POWER_SUPPLY_PROP_HEALTH,
  410. POWER_SUPPLY_PROP_PRESENT,
  411. POWER_SUPPLY_PROP_ONLINE,
  412. POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
  413. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
  414. POWER_SUPPLY_PROP_MODEL_NAME,
  415. POWER_SUPPLY_PROP_MANUFACTURER,
  416. };
  417. static int bd70528_charger_get_property(struct power_supply *psy,
  418. enum power_supply_property psp,
  419. union power_supply_propval *val)
  420. {
  421. struct bd70528_psy *bdpsy = power_supply_get_drvdata(psy);
  422. int ret = 0;
  423. switch (psp) {
  424. case POWER_SUPPLY_PROP_STATUS:
  425. return bd70528_get_charger_status(bdpsy, &val->intval);
  426. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  427. return bd70528_get_charge_type(bdpsy, &val->intval);
  428. case POWER_SUPPLY_PROP_HEALTH:
  429. return bd70528_get_battery_health(bdpsy, &val->intval);
  430. case POWER_SUPPLY_PROP_PRESENT:
  431. return bd70528_get_present(bdpsy, &val->intval);
  432. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  433. ret = get_current_limit(bdpsy, &val->intval);
  434. val->intval *= 1000;
  435. return ret;
  436. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  437. ret = get_charge_current(bdpsy, &val->intval);
  438. val->intval *= 1000;
  439. return ret;
  440. case POWER_SUPPLY_PROP_ONLINE:
  441. return bd70528_get_online(bdpsy, &val->intval);
  442. case POWER_SUPPLY_PROP_MODEL_NAME:
  443. val->strval = bd70528_charger_model;
  444. return 0;
  445. case POWER_SUPPLY_PROP_MANUFACTURER:
  446. val->strval = bd70528_charger_manufacturer;
  447. return 0;
  448. default:
  449. break;
  450. }
  451. return -EINVAL;
  452. }
  453. static int bd70528_prop_is_writable(struct power_supply *psy,
  454. enum power_supply_property psp)
  455. {
  456. switch (psp) {
  457. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  458. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  459. return 1;
  460. default:
  461. break;
  462. }
  463. return 0;
  464. }
  465. static int set_charge_current(struct bd70528_psy *bdpsy, int ma)
  466. {
  467. unsigned int reg;
  468. int ret = 0, tmpret;
  469. bool found;
  470. if (ma > 500) {
  471. dev_warn(bdpsy->dev,
  472. "Requested charge current %u exceed maximum (500mA)\n",
  473. ma);
  474. reg = MAX_WARM_CHG_CURR_SEL;
  475. goto set;
  476. }
  477. if (ma < 10) {
  478. dev_err(bdpsy->dev,
  479. "Requested charge current %u smaller than min (10mA)\n",
  480. ma);
  481. reg = MIN_CHG_CURR_SEL;
  482. ret = -EINVAL;
  483. goto set;
  484. }
  485. /*
  486. * For BD70528 voltage/current limits we happily accept any value which
  487. * belongs the range. We could check if value matching the selector is
  488. * desired by computing the range min + (sel - sel_low) * range step - but
  489. * I guess it is enough if we use voltage/current which is closest (below)
  490. * the requested?
  491. */
  492. ret = linear_range_get_selector_low_array(warm_charge_curr,
  493. ARRAY_SIZE(warm_charge_curr),
  494. ma, &reg, &found);
  495. if (ret) {
  496. dev_err(bdpsy->dev,
  497. "Unsupported charge current %u mA\n", ma);
  498. reg = MIN_CHG_CURR_SEL;
  499. goto set;
  500. }
  501. if (!found) {
  502. /*
  503. * There was a gap in supported values and we hit it.
  504. * Yet a smaller value was found so we use it.
  505. */
  506. dev_warn(bdpsy->dev,
  507. "Unsupported charge current %u mA\n", ma);
  508. }
  509. set:
  510. tmpret = regmap_update_bits(bdpsy->regmap,
  511. BD70528_REG_CHG_CHG_CURR_WARM,
  512. BD70528_MASK_CHG_CHG_CURR, reg);
  513. if (tmpret)
  514. dev_err(bdpsy->dev,
  515. "Charge current write failure (%d)\n", tmpret);
  516. if (reg > MAX_COLD_CHG_CURR_SEL)
  517. reg = MAX_COLD_CHG_CURR_SEL;
  518. if (!tmpret)
  519. tmpret = regmap_update_bits(bdpsy->regmap,
  520. BD70528_REG_CHG_CHG_CURR_COLD,
  521. BD70528_MASK_CHG_CHG_CURR, reg);
  522. if (!ret)
  523. ret = tmpret;
  524. return ret;
  525. }
  526. #define MAX_CURR_LIMIT_SEL 0x34
  527. #define MIN_CURR_LIMIT_SEL 0x0
  528. static int set_current_limit(struct bd70528_psy *bdpsy, int ma)
  529. {
  530. unsigned int reg;
  531. int ret = 0, tmpret;
  532. bool found;
  533. if (ma > 500) {
  534. dev_warn(bdpsy->dev,
  535. "Requested current limit %u exceed maximum (500mA)\n",
  536. ma);
  537. reg = MAX_CURR_LIMIT_SEL;
  538. goto set;
  539. }
  540. if (ma < 5) {
  541. dev_err(bdpsy->dev,
  542. "Requested current limit %u smaller than min (5mA)\n",
  543. ma);
  544. reg = MIN_CURR_LIMIT_SEL;
  545. ret = -EINVAL;
  546. goto set;
  547. }
  548. ret = linear_range_get_selector_low_array(current_limit_ranges,
  549. ARRAY_SIZE(current_limit_ranges),
  550. ma, &reg, &found);
  551. if (ret) {
  552. dev_err(bdpsy->dev, "Unsupported current limit %umA\n", ma);
  553. reg = MIN_CURR_LIMIT_SEL;
  554. goto set;
  555. }
  556. if (!found) {
  557. /*
  558. * There was a gap in supported values and we hit it.
  559. * We found a smaller value from ranges and use it.
  560. * Warn user though.
  561. */
  562. dev_warn(bdpsy->dev, "Unsupported current limit %umA\n", ma);
  563. }
  564. set:
  565. tmpret = regmap_update_bits(bdpsy->regmap,
  566. BD70528_REG_CHG_DCIN_ILIM,
  567. BD70528_MASK_CHG_DCIN_ILIM, reg);
  568. if (!ret)
  569. ret = tmpret;
  570. return ret;
  571. }
  572. static int bd70528_charger_set_property(struct power_supply *psy,
  573. enum power_supply_property psp,
  574. const union power_supply_propval *val)
  575. {
  576. struct bd70528_psy *bdpsy = power_supply_get_drvdata(psy);
  577. switch (psp) {
  578. case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
  579. return set_current_limit(bdpsy, val->intval / 1000);
  580. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
  581. return set_charge_current(bdpsy, val->intval / 1000);
  582. default:
  583. break;
  584. }
  585. return -EINVAL;
  586. }
  587. static const struct power_supply_desc bd70528_charger_desc = {
  588. .name = "bd70528-charger",
  589. .type = POWER_SUPPLY_TYPE_MAINS,
  590. .properties = bd70528_charger_props,
  591. .num_properties = ARRAY_SIZE(bd70528_charger_props),
  592. .get_property = bd70528_charger_get_property,
  593. .set_property = bd70528_charger_set_property,
  594. .property_is_writeable = bd70528_prop_is_writable,
  595. };
  596. static int bd70528_power_probe(struct platform_device *pdev)
  597. {
  598. struct bd70528_psy *bdpsy;
  599. struct power_supply_config cfg = {};
  600. bdpsy = devm_kzalloc(&pdev->dev, sizeof(*bdpsy), GFP_KERNEL);
  601. if (!bdpsy)
  602. return -ENOMEM;
  603. bdpsy->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  604. if (!bdpsy->regmap) {
  605. dev_err(&pdev->dev, "No regmap found for chip\n");
  606. return -EINVAL;
  607. }
  608. bdpsy->dev = &pdev->dev;
  609. platform_set_drvdata(pdev, bdpsy);
  610. cfg.drv_data = bdpsy;
  611. cfg.of_node = pdev->dev.parent->of_node;
  612. bdpsy->psy = devm_power_supply_register(&pdev->dev,
  613. &bd70528_charger_desc, &cfg);
  614. if (IS_ERR(bdpsy->psy)) {
  615. dev_err(&pdev->dev, "failed: power supply register\n");
  616. return PTR_ERR(bdpsy->psy);
  617. }
  618. return bd70528_get_irqs(pdev, bdpsy);
  619. }
  620. static struct platform_driver bd70528_power = {
  621. .driver = {
  622. .name = "bd70528-power"
  623. },
  624. .probe = bd70528_power_probe,
  625. };
  626. module_platform_driver(bd70528_power);
  627. MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
  628. MODULE_DESCRIPTION("BD70528 power-supply driver");
  629. MODULE_LICENSE("GPL");
  630. MODULE_ALIAS("platform:bd70528-power");