max77693_charger.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * max77693_charger.c - Battery charger driver for the Maxim 77693
  3. *
  4. * Copyright (C) 2014 Samsung Electronics
  5. * Krzysztof Kozlowski <krzk@kernel.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/power_supply.h>
  20. #include <linux/regmap.h>
  21. #include <linux/mfd/max77693.h>
  22. #include <linux/mfd/max77693-common.h>
  23. #include <linux/mfd/max77693-private.h>
  24. #define MAX77693_CHARGER_NAME "max77693-charger"
  25. static const char *max77693_charger_model = "MAX77693";
  26. static const char *max77693_charger_manufacturer = "Maxim Integrated";
  27. struct max77693_charger {
  28. struct device *dev;
  29. struct max77693_dev *max77693;
  30. struct power_supply *charger;
  31. u32 constant_volt;
  32. u32 min_system_volt;
  33. u32 thermal_regulation_temp;
  34. u32 batttery_overcurrent;
  35. u32 charge_input_threshold_volt;
  36. };
  37. static int max77693_get_charger_state(struct regmap *regmap, int *val)
  38. {
  39. int ret;
  40. unsigned int data;
  41. ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
  42. if (ret < 0)
  43. return ret;
  44. data &= CHG_DETAILS_01_CHG_MASK;
  45. data >>= CHG_DETAILS_01_CHG_SHIFT;
  46. switch (data) {
  47. case MAX77693_CHARGING_PREQUALIFICATION:
  48. case MAX77693_CHARGING_FAST_CONST_CURRENT:
  49. case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
  50. case MAX77693_CHARGING_TOP_OFF:
  51. /* In high temp the charging current is reduced, but still charging */
  52. case MAX77693_CHARGING_HIGH_TEMP:
  53. *val = POWER_SUPPLY_STATUS_CHARGING;
  54. break;
  55. case MAX77693_CHARGING_DONE:
  56. *val = POWER_SUPPLY_STATUS_FULL;
  57. break;
  58. case MAX77693_CHARGING_TIMER_EXPIRED:
  59. case MAX77693_CHARGING_THERMISTOR_SUSPEND:
  60. *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
  61. break;
  62. case MAX77693_CHARGING_OFF:
  63. case MAX77693_CHARGING_OVER_TEMP:
  64. case MAX77693_CHARGING_WATCHDOG_EXPIRED:
  65. *val = POWER_SUPPLY_STATUS_DISCHARGING;
  66. break;
  67. case MAX77693_CHARGING_RESERVED:
  68. default:
  69. *val = POWER_SUPPLY_STATUS_UNKNOWN;
  70. }
  71. return 0;
  72. }
  73. static int max77693_get_charge_type(struct regmap *regmap, int *val)
  74. {
  75. int ret;
  76. unsigned int data;
  77. ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
  78. if (ret < 0)
  79. return ret;
  80. data &= CHG_DETAILS_01_CHG_MASK;
  81. data >>= CHG_DETAILS_01_CHG_SHIFT;
  82. switch (data) {
  83. case MAX77693_CHARGING_PREQUALIFICATION:
  84. /*
  85. * Top-off: trickle or fast? In top-off the current varies between
  86. * 100 and 250 mA. It is higher than prequalification current.
  87. */
  88. case MAX77693_CHARGING_TOP_OFF:
  89. *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  90. break;
  91. case MAX77693_CHARGING_FAST_CONST_CURRENT:
  92. case MAX77693_CHARGING_FAST_CONST_VOLTAGE:
  93. /* In high temp the charging current is reduced, but still charging */
  94. case MAX77693_CHARGING_HIGH_TEMP:
  95. *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
  96. break;
  97. case MAX77693_CHARGING_DONE:
  98. case MAX77693_CHARGING_TIMER_EXPIRED:
  99. case MAX77693_CHARGING_THERMISTOR_SUSPEND:
  100. case MAX77693_CHARGING_OFF:
  101. case MAX77693_CHARGING_OVER_TEMP:
  102. case MAX77693_CHARGING_WATCHDOG_EXPIRED:
  103. *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
  104. break;
  105. case MAX77693_CHARGING_RESERVED:
  106. default:
  107. *val = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  108. }
  109. return 0;
  110. }
  111. /*
  112. * Supported health statuses:
  113. * - POWER_SUPPLY_HEALTH_DEAD
  114. * - POWER_SUPPLY_HEALTH_GOOD
  115. * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
  116. * - POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
  117. * - POWER_SUPPLY_HEALTH_UNKNOWN
  118. * - POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
  119. */
  120. static int max77693_get_battery_health(struct regmap *regmap, int *val)
  121. {
  122. int ret;
  123. unsigned int data;
  124. ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_DETAILS_01, &data);
  125. if (ret < 0)
  126. return ret;
  127. data &= CHG_DETAILS_01_BAT_MASK;
  128. data >>= CHG_DETAILS_01_BAT_SHIFT;
  129. switch (data) {
  130. case MAX77693_BATTERY_NOBAT:
  131. *val = POWER_SUPPLY_HEALTH_DEAD;
  132. break;
  133. case MAX77693_BATTERY_PREQUALIFICATION:
  134. case MAX77693_BATTERY_GOOD:
  135. case MAX77693_BATTERY_LOWVOLTAGE:
  136. *val = POWER_SUPPLY_HEALTH_GOOD;
  137. break;
  138. case MAX77693_BATTERY_TIMER_EXPIRED:
  139. /*
  140. * Took longer to charge than expected, charging suspended.
  141. * Damaged battery?
  142. */
  143. *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
  144. break;
  145. case MAX77693_BATTERY_OVERVOLTAGE:
  146. *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  147. break;
  148. case MAX77693_BATTERY_OVERCURRENT:
  149. *val = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  150. break;
  151. case MAX77693_BATTERY_RESERVED:
  152. default:
  153. *val = POWER_SUPPLY_HEALTH_UNKNOWN;
  154. break;
  155. }
  156. return 0;
  157. }
  158. static int max77693_get_present(struct regmap *regmap, int *val)
  159. {
  160. unsigned int data;
  161. int ret;
  162. /*
  163. * Read CHG_INT_OK register. High DETBAT bit here should be
  164. * equal to value 0x0 in CHG_DETAILS_01/BAT field.
  165. */
  166. ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
  167. if (ret < 0)
  168. return ret;
  169. *val = (data & CHG_INT_OK_DETBAT_MASK) ? 0 : 1;
  170. return 0;
  171. }
  172. static int max77693_get_online(struct regmap *regmap, int *val)
  173. {
  174. unsigned int data;
  175. int ret;
  176. ret = regmap_read(regmap, MAX77693_CHG_REG_CHG_INT_OK, &data);
  177. if (ret < 0)
  178. return ret;
  179. *val = (data & CHG_INT_OK_CHGIN_MASK) ? 1 : 0;
  180. return 0;
  181. }
  182. static enum power_supply_property max77693_charger_props[] = {
  183. POWER_SUPPLY_PROP_STATUS,
  184. POWER_SUPPLY_PROP_CHARGE_TYPE,
  185. POWER_SUPPLY_PROP_HEALTH,
  186. POWER_SUPPLY_PROP_PRESENT,
  187. POWER_SUPPLY_PROP_ONLINE,
  188. POWER_SUPPLY_PROP_MODEL_NAME,
  189. POWER_SUPPLY_PROP_MANUFACTURER,
  190. };
  191. static int max77693_charger_get_property(struct power_supply *psy,
  192. enum power_supply_property psp,
  193. union power_supply_propval *val)
  194. {
  195. struct max77693_charger *chg = power_supply_get_drvdata(psy);
  196. struct regmap *regmap = chg->max77693->regmap;
  197. int ret = 0;
  198. switch (psp) {
  199. case POWER_SUPPLY_PROP_STATUS:
  200. ret = max77693_get_charger_state(regmap, &val->intval);
  201. break;
  202. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  203. ret = max77693_get_charge_type(regmap, &val->intval);
  204. break;
  205. case POWER_SUPPLY_PROP_HEALTH:
  206. ret = max77693_get_battery_health(regmap, &val->intval);
  207. break;
  208. case POWER_SUPPLY_PROP_PRESENT:
  209. ret = max77693_get_present(regmap, &val->intval);
  210. break;
  211. case POWER_SUPPLY_PROP_ONLINE:
  212. ret = max77693_get_online(regmap, &val->intval);
  213. break;
  214. case POWER_SUPPLY_PROP_MODEL_NAME:
  215. val->strval = max77693_charger_model;
  216. break;
  217. case POWER_SUPPLY_PROP_MANUFACTURER:
  218. val->strval = max77693_charger_manufacturer;
  219. break;
  220. default:
  221. return -EINVAL;
  222. }
  223. return ret;
  224. }
  225. static const struct power_supply_desc max77693_charger_desc = {
  226. .name = MAX77693_CHARGER_NAME,
  227. .type = POWER_SUPPLY_TYPE_BATTERY,
  228. .properties = max77693_charger_props,
  229. .num_properties = ARRAY_SIZE(max77693_charger_props),
  230. .get_property = max77693_charger_get_property,
  231. };
  232. static ssize_t device_attr_store(struct device *dev,
  233. struct device_attribute *attr, const char *buf, size_t count,
  234. int (*fn)(struct max77693_charger *, unsigned long))
  235. {
  236. struct max77693_charger *chg = dev_get_drvdata(dev);
  237. unsigned long val;
  238. int ret;
  239. ret = kstrtoul(buf, 10, &val);
  240. if (ret)
  241. return ret;
  242. ret = fn(chg, val);
  243. if (ret)
  244. return ret;
  245. return count;
  246. }
  247. static ssize_t fast_charge_timer_show(struct device *dev,
  248. struct device_attribute *attr, char *buf)
  249. {
  250. struct max77693_charger *chg = dev_get_drvdata(dev);
  251. unsigned int data, val;
  252. int ret;
  253. ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_01,
  254. &data);
  255. if (ret < 0)
  256. return ret;
  257. data &= CHG_CNFG_01_FCHGTIME_MASK;
  258. data >>= CHG_CNFG_01_FCHGTIME_SHIFT;
  259. switch (data) {
  260. case 0x1 ... 0x7:
  261. /* Starting from 4 hours, step by 2 hours */
  262. val = 4 + (data - 1) * 2;
  263. break;
  264. case 0x0:
  265. default:
  266. val = 0;
  267. break;
  268. }
  269. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  270. }
  271. static int max77693_set_fast_charge_timer(struct max77693_charger *chg,
  272. unsigned long hours)
  273. {
  274. unsigned int data;
  275. /*
  276. * 0x00 - disable
  277. * 0x01 - 4h
  278. * 0x02 - 6h
  279. * ...
  280. * 0x07 - 16h
  281. * Round down odd values.
  282. */
  283. switch (hours) {
  284. case 4 ... 16:
  285. data = (hours - 4) / 2 + 1;
  286. break;
  287. case 0:
  288. /* Disable */
  289. data = 0;
  290. break;
  291. default:
  292. return -EINVAL;
  293. }
  294. data <<= CHG_CNFG_01_FCHGTIME_SHIFT;
  295. return regmap_update_bits(chg->max77693->regmap,
  296. MAX77693_CHG_REG_CHG_CNFG_01,
  297. CHG_CNFG_01_FCHGTIME_MASK, data);
  298. }
  299. static ssize_t fast_charge_timer_store(struct device *dev,
  300. struct device_attribute *attr, const char *buf, size_t count)
  301. {
  302. return device_attr_store(dev, attr, buf, count,
  303. max77693_set_fast_charge_timer);
  304. }
  305. static ssize_t top_off_threshold_current_show(struct device *dev,
  306. struct device_attribute *attr, char *buf)
  307. {
  308. struct max77693_charger *chg = dev_get_drvdata(dev);
  309. unsigned int data, val;
  310. int ret;
  311. ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
  312. &data);
  313. if (ret < 0)
  314. return ret;
  315. data &= CHG_CNFG_03_TOITH_MASK;
  316. data >>= CHG_CNFG_03_TOITH_SHIFT;
  317. if (data <= 0x04)
  318. val = 100000 + data * 25000;
  319. else
  320. val = data * 50000;
  321. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  322. }
  323. static int max77693_set_top_off_threshold_current(struct max77693_charger *chg,
  324. unsigned long uamp)
  325. {
  326. unsigned int data;
  327. if (uamp < 100000 || uamp > 350000)
  328. return -EINVAL;
  329. if (uamp <= 200000)
  330. data = (uamp - 100000) / 25000;
  331. else
  332. /* (200000, 350000> */
  333. data = uamp / 50000;
  334. data <<= CHG_CNFG_03_TOITH_SHIFT;
  335. return regmap_update_bits(chg->max77693->regmap,
  336. MAX77693_CHG_REG_CHG_CNFG_03,
  337. CHG_CNFG_03_TOITH_MASK, data);
  338. }
  339. static ssize_t top_off_threshold_current_store(struct device *dev,
  340. struct device_attribute *attr, const char *buf, size_t count)
  341. {
  342. return device_attr_store(dev, attr, buf, count,
  343. max77693_set_top_off_threshold_current);
  344. }
  345. static ssize_t top_off_timer_show(struct device *dev,
  346. struct device_attribute *attr, char *buf)
  347. {
  348. struct max77693_charger *chg = dev_get_drvdata(dev);
  349. unsigned int data, val;
  350. int ret;
  351. ret = regmap_read(chg->max77693->regmap, MAX77693_CHG_REG_CHG_CNFG_03,
  352. &data);
  353. if (ret < 0)
  354. return ret;
  355. data &= CHG_CNFG_03_TOTIME_MASK;
  356. data >>= CHG_CNFG_03_TOTIME_SHIFT;
  357. val = data * 10;
  358. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  359. }
  360. static int max77693_set_top_off_timer(struct max77693_charger *chg,
  361. unsigned long minutes)
  362. {
  363. unsigned int data;
  364. if (minutes > 70)
  365. return -EINVAL;
  366. data = minutes / 10;
  367. data <<= CHG_CNFG_03_TOTIME_SHIFT;
  368. return regmap_update_bits(chg->max77693->regmap,
  369. MAX77693_CHG_REG_CHG_CNFG_03,
  370. CHG_CNFG_03_TOTIME_MASK, data);
  371. }
  372. static ssize_t top_off_timer_store(struct device *dev,
  373. struct device_attribute *attr, const char *buf, size_t count)
  374. {
  375. return device_attr_store(dev, attr, buf, count,
  376. max77693_set_top_off_timer);
  377. }
  378. static DEVICE_ATTR_RW(fast_charge_timer);
  379. static DEVICE_ATTR_RW(top_off_threshold_current);
  380. static DEVICE_ATTR_RW(top_off_timer);
  381. static int max77693_set_constant_volt(struct max77693_charger *chg,
  382. unsigned int uvolt)
  383. {
  384. unsigned int data;
  385. /*
  386. * 0x00 - 3.650 V
  387. * 0x01 - 3.675 V
  388. * ...
  389. * 0x1b - 4.325 V
  390. * 0x1c - 4.340 V
  391. * 0x1d - 4.350 V
  392. * 0x1e - 4.375 V
  393. * 0x1f - 4.400 V
  394. */
  395. if (uvolt >= 3650000 && uvolt < 4340000)
  396. data = (uvolt - 3650000) / 25000;
  397. else if (uvolt >= 4340000 && uvolt < 4350000)
  398. data = 0x1c;
  399. else if (uvolt >= 4350000 && uvolt <= 4400000)
  400. data = 0x1d + (uvolt - 4350000) / 25000;
  401. else {
  402. dev_err(chg->dev, "Wrong value for charging constant voltage\n");
  403. return -EINVAL;
  404. }
  405. data <<= CHG_CNFG_04_CHGCVPRM_SHIFT;
  406. dev_dbg(chg->dev, "Charging constant voltage: %u (0x%x)\n", uvolt,
  407. data);
  408. return regmap_update_bits(chg->max77693->regmap,
  409. MAX77693_CHG_REG_CHG_CNFG_04,
  410. CHG_CNFG_04_CHGCVPRM_MASK, data);
  411. }
  412. static int max77693_set_min_system_volt(struct max77693_charger *chg,
  413. unsigned int uvolt)
  414. {
  415. unsigned int data;
  416. if (uvolt < 3000000 || uvolt > 3700000) {
  417. dev_err(chg->dev, "Wrong value for minimum system regulation voltage\n");
  418. return -EINVAL;
  419. }
  420. data = (uvolt - 3000000) / 100000;
  421. data <<= CHG_CNFG_04_MINVSYS_SHIFT;
  422. dev_dbg(chg->dev, "Minimum system regulation voltage: %u (0x%x)\n",
  423. uvolt, data);
  424. return regmap_update_bits(chg->max77693->regmap,
  425. MAX77693_CHG_REG_CHG_CNFG_04,
  426. CHG_CNFG_04_MINVSYS_MASK, data);
  427. }
  428. static int max77693_set_thermal_regulation_temp(struct max77693_charger *chg,
  429. unsigned int cels)
  430. {
  431. unsigned int data;
  432. switch (cels) {
  433. case 70:
  434. case 85:
  435. case 100:
  436. case 115:
  437. data = (cels - 70) / 15;
  438. break;
  439. default:
  440. dev_err(chg->dev, "Wrong value for thermal regulation loop temperature\n");
  441. return -EINVAL;
  442. }
  443. data <<= CHG_CNFG_07_REGTEMP_SHIFT;
  444. dev_dbg(chg->dev, "Thermal regulation loop temperature: %u (0x%x)\n",
  445. cels, data);
  446. return regmap_update_bits(chg->max77693->regmap,
  447. MAX77693_CHG_REG_CHG_CNFG_07,
  448. CHG_CNFG_07_REGTEMP_MASK, data);
  449. }
  450. static int max77693_set_batttery_overcurrent(struct max77693_charger *chg,
  451. unsigned int uamp)
  452. {
  453. unsigned int data;
  454. if (uamp && (uamp < 2000000 || uamp > 3500000)) {
  455. dev_err(chg->dev, "Wrong value for battery overcurrent\n");
  456. return -EINVAL;
  457. }
  458. if (uamp)
  459. data = ((uamp - 2000000) / 250000) + 1;
  460. else
  461. data = 0; /* disable */
  462. data <<= CHG_CNFG_12_B2SOVRC_SHIFT;
  463. dev_dbg(chg->dev, "Battery overcurrent: %u (0x%x)\n", uamp, data);
  464. return regmap_update_bits(chg->max77693->regmap,
  465. MAX77693_CHG_REG_CHG_CNFG_12,
  466. CHG_CNFG_12_B2SOVRC_MASK, data);
  467. }
  468. static int max77693_set_charge_input_threshold_volt(struct max77693_charger *chg,
  469. unsigned int uvolt)
  470. {
  471. unsigned int data;
  472. switch (uvolt) {
  473. case 4300000:
  474. data = 0x0;
  475. break;
  476. case 4700000:
  477. case 4800000:
  478. case 4900000:
  479. data = (uvolt - 4700000) / 100000;
  480. default:
  481. dev_err(chg->dev, "Wrong value for charge input voltage regulation threshold\n");
  482. return -EINVAL;
  483. }
  484. data <<= CHG_CNFG_12_VCHGINREG_SHIFT;
  485. dev_dbg(chg->dev, "Charge input voltage regulation threshold: %u (0x%x)\n",
  486. uvolt, data);
  487. return regmap_update_bits(chg->max77693->regmap,
  488. MAX77693_CHG_REG_CHG_CNFG_12,
  489. CHG_CNFG_12_VCHGINREG_MASK, data);
  490. }
  491. /*
  492. * Sets charger registers to proper and safe default values.
  493. */
  494. static int max77693_reg_init(struct max77693_charger *chg)
  495. {
  496. int ret;
  497. unsigned int data;
  498. /* Unlock charger register protection */
  499. data = (0x3 << CHG_CNFG_06_CHGPROT_SHIFT);
  500. ret = regmap_update_bits(chg->max77693->regmap,
  501. MAX77693_CHG_REG_CHG_CNFG_06,
  502. CHG_CNFG_06_CHGPROT_MASK, data);
  503. if (ret) {
  504. dev_err(chg->dev, "Error unlocking registers: %d\n", ret);
  505. return ret;
  506. }
  507. ret = max77693_set_fast_charge_timer(chg, DEFAULT_FAST_CHARGE_TIMER);
  508. if (ret)
  509. return ret;
  510. ret = max77693_set_top_off_threshold_current(chg,
  511. DEFAULT_TOP_OFF_THRESHOLD_CURRENT);
  512. if (ret)
  513. return ret;
  514. ret = max77693_set_top_off_timer(chg, DEFAULT_TOP_OFF_TIMER);
  515. if (ret)
  516. return ret;
  517. ret = max77693_set_constant_volt(chg, chg->constant_volt);
  518. if (ret)
  519. return ret;
  520. ret = max77693_set_min_system_volt(chg, chg->min_system_volt);
  521. if (ret)
  522. return ret;
  523. ret = max77693_set_thermal_regulation_temp(chg,
  524. chg->thermal_regulation_temp);
  525. if (ret)
  526. return ret;
  527. ret = max77693_set_batttery_overcurrent(chg, chg->batttery_overcurrent);
  528. if (ret)
  529. return ret;
  530. return max77693_set_charge_input_threshold_volt(chg,
  531. chg->charge_input_threshold_volt);
  532. }
  533. #ifdef CONFIG_OF
  534. static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
  535. {
  536. struct device_node *np = dev->of_node;
  537. if (!np) {
  538. dev_err(dev, "no charger OF node\n");
  539. return -EINVAL;
  540. }
  541. if (of_property_read_u32(np, "maxim,constant-microvolt",
  542. &chg->constant_volt))
  543. chg->constant_volt = DEFAULT_CONSTANT_VOLT;
  544. if (of_property_read_u32(np, "maxim,min-system-microvolt",
  545. &chg->min_system_volt))
  546. chg->min_system_volt = DEFAULT_MIN_SYSTEM_VOLT;
  547. if (of_property_read_u32(np, "maxim,thermal-regulation-celsius",
  548. &chg->thermal_regulation_temp))
  549. chg->thermal_regulation_temp = DEFAULT_THERMAL_REGULATION_TEMP;
  550. if (of_property_read_u32(np, "maxim,battery-overcurrent-microamp",
  551. &chg->batttery_overcurrent))
  552. chg->batttery_overcurrent = DEFAULT_BATTERY_OVERCURRENT;
  553. if (of_property_read_u32(np, "maxim,charge-input-threshold-microvolt",
  554. &chg->charge_input_threshold_volt))
  555. chg->charge_input_threshold_volt =
  556. DEFAULT_CHARGER_INPUT_THRESHOLD_VOLT;
  557. return 0;
  558. }
  559. #else /* CONFIG_OF */
  560. static int max77693_dt_init(struct device *dev, struct max77693_charger *chg)
  561. {
  562. return 0;
  563. }
  564. #endif /* CONFIG_OF */
  565. static int max77693_charger_probe(struct platform_device *pdev)
  566. {
  567. struct max77693_charger *chg;
  568. struct power_supply_config psy_cfg = {};
  569. struct max77693_dev *max77693 = dev_get_drvdata(pdev->dev.parent);
  570. int ret;
  571. chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
  572. if (!chg)
  573. return -ENOMEM;
  574. platform_set_drvdata(pdev, chg);
  575. chg->dev = &pdev->dev;
  576. chg->max77693 = max77693;
  577. ret = max77693_dt_init(&pdev->dev, chg);
  578. if (ret)
  579. return ret;
  580. ret = max77693_reg_init(chg);
  581. if (ret)
  582. return ret;
  583. psy_cfg.drv_data = chg;
  584. ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
  585. if (ret) {
  586. dev_err(&pdev->dev, "failed: create fast charge timer sysfs entry\n");
  587. goto err;
  588. }
  589. ret = device_create_file(&pdev->dev,
  590. &dev_attr_top_off_threshold_current);
  591. if (ret) {
  592. dev_err(&pdev->dev, "failed: create top off current sysfs entry\n");
  593. goto err;
  594. }
  595. ret = device_create_file(&pdev->dev, &dev_attr_top_off_timer);
  596. if (ret) {
  597. dev_err(&pdev->dev, "failed: create top off timer sysfs entry\n");
  598. goto err;
  599. }
  600. chg->charger = power_supply_register(&pdev->dev,
  601. &max77693_charger_desc,
  602. &psy_cfg);
  603. if (IS_ERR(chg->charger)) {
  604. dev_err(&pdev->dev, "failed: power supply register\n");
  605. ret = PTR_ERR(chg->charger);
  606. goto err;
  607. }
  608. return 0;
  609. err:
  610. device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
  611. device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
  612. device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
  613. return ret;
  614. }
  615. static int max77693_charger_remove(struct platform_device *pdev)
  616. {
  617. struct max77693_charger *chg = platform_get_drvdata(pdev);
  618. device_remove_file(&pdev->dev, &dev_attr_top_off_timer);
  619. device_remove_file(&pdev->dev, &dev_attr_top_off_threshold_current);
  620. device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
  621. power_supply_unregister(chg->charger);
  622. return 0;
  623. }
  624. static const struct platform_device_id max77693_charger_id[] = {
  625. { "max77693-charger", 0, },
  626. { }
  627. };
  628. MODULE_DEVICE_TABLE(platform, max77693_charger_id);
  629. static struct platform_driver max77693_charger_driver = {
  630. .driver = {
  631. .name = "max77693-charger",
  632. },
  633. .probe = max77693_charger_probe,
  634. .remove = max77693_charger_remove,
  635. .id_table = max77693_charger_id,
  636. };
  637. module_platform_driver(max77693_charger_driver);
  638. MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
  639. MODULE_DESCRIPTION("Maxim 77693 charger driver");
  640. MODULE_LICENSE("GPL");