max14577_charger.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * max14577_charger.c - Battery charger driver for the Maxim 14577/77836
  3. *
  4. * Copyright (C) 2013,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/mfd/max14577-private.h>
  21. #include <linux/mfd/max14577.h>
  22. struct max14577_charger {
  23. struct device *dev;
  24. struct max14577 *max14577;
  25. struct power_supply *charger;
  26. struct max14577_charger_platform_data *pdata;
  27. };
  28. /*
  29. * Helper function for mapping values of STATUS2/CHGTYP register on max14577
  30. * and max77836 chipsets to enum maxim_muic_charger_type.
  31. */
  32. static enum max14577_muic_charger_type maxim_get_charger_type(
  33. enum maxim_device_type dev_type, u8 val) {
  34. switch (val) {
  35. case MAX14577_CHARGER_TYPE_NONE:
  36. case MAX14577_CHARGER_TYPE_USB:
  37. case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
  38. case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
  39. case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
  40. case MAX14577_CHARGER_TYPE_SPECIAL_1A:
  41. return val;
  42. case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
  43. case MAX14577_CHARGER_TYPE_RESERVED:
  44. if (dev_type == MAXIM_DEVICE_TYPE_MAX77836)
  45. val |= 0x8;
  46. return val;
  47. default:
  48. WARN_ONCE(1, "max14577: Unsupported chgtyp register value 0x%02x", val);
  49. return val;
  50. }
  51. }
  52. static int max14577_get_charger_state(struct max14577_charger *chg, int *val)
  53. {
  54. struct regmap *rmap = chg->max14577->regmap;
  55. int ret;
  56. u8 reg_data;
  57. /*
  58. * Charging occurs only if:
  59. * - CHGCTRL2/MBCHOSTEN == 1
  60. * - STATUS2/CGMBC == 1
  61. *
  62. * TODO:
  63. * - handle FULL after Top-off timer (EOC register may be off
  64. * and the charger won't be charging although MBCHOSTEN is on)
  65. * - handle properly dead-battery charging (respect timer)
  66. * - handle timers (fast-charge and prequal) /MBCCHGERR/
  67. */
  68. ret = max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, &reg_data);
  69. if (ret < 0)
  70. goto out;
  71. if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0) {
  72. *val = POWER_SUPPLY_STATUS_DISCHARGING;
  73. goto out;
  74. }
  75. ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
  76. if (ret < 0)
  77. goto out;
  78. if (reg_data & STATUS3_CGMBC_MASK) {
  79. /* Charger or USB-cable is connected */
  80. if (reg_data & STATUS3_EOC_MASK)
  81. *val = POWER_SUPPLY_STATUS_FULL;
  82. else
  83. *val = POWER_SUPPLY_STATUS_CHARGING;
  84. goto out;
  85. }
  86. *val = POWER_SUPPLY_STATUS_DISCHARGING;
  87. out:
  88. return ret;
  89. }
  90. /*
  91. * Supported charge types:
  92. * - POWER_SUPPLY_CHARGE_TYPE_NONE
  93. * - POWER_SUPPLY_CHARGE_TYPE_FAST
  94. */
  95. static int max14577_get_charge_type(struct max14577_charger *chg, int *val)
  96. {
  97. int ret, charging;
  98. /*
  99. * TODO: CHARGE_TYPE_TRICKLE (VCHGR_RC or EOC)?
  100. * As spec says:
  101. * [after reaching EOC interrupt]
  102. * "When the battery is fully charged, the 30-minute (typ)
  103. * top-off timer starts. The device continues to trickle
  104. * charge the battery until the top-off timer runs out."
  105. */
  106. ret = max14577_get_charger_state(chg, &charging);
  107. if (ret < 0)
  108. return ret;
  109. if (charging == POWER_SUPPLY_STATUS_CHARGING)
  110. *val = POWER_SUPPLY_CHARGE_TYPE_FAST;
  111. else
  112. *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
  113. return 0;
  114. }
  115. static int max14577_get_online(struct max14577_charger *chg, int *val)
  116. {
  117. struct regmap *rmap = chg->max14577->regmap;
  118. u8 reg_data;
  119. int ret;
  120. enum max14577_muic_charger_type chg_type;
  121. ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
  122. if (ret < 0)
  123. return ret;
  124. reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
  125. chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
  126. switch (chg_type) {
  127. case MAX14577_CHARGER_TYPE_USB:
  128. case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
  129. case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
  130. case MAX14577_CHARGER_TYPE_SPECIAL_1A:
  131. case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
  132. case MAX77836_CHARGER_TYPE_SPECIAL_BIAS:
  133. *val = 1;
  134. break;
  135. case MAX14577_CHARGER_TYPE_NONE:
  136. case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
  137. case MAX14577_CHARGER_TYPE_RESERVED:
  138. case MAX77836_CHARGER_TYPE_RESERVED:
  139. default:
  140. *val = 0;
  141. }
  142. return 0;
  143. }
  144. /*
  145. * Supported health statuses:
  146. * - POWER_SUPPLY_HEALTH_DEAD
  147. * - POWER_SUPPLY_HEALTH_OVERVOLTAGE
  148. * - POWER_SUPPLY_HEALTH_GOOD
  149. */
  150. static int max14577_get_battery_health(struct max14577_charger *chg, int *val)
  151. {
  152. struct regmap *rmap = chg->max14577->regmap;
  153. int ret;
  154. u8 reg_data;
  155. enum max14577_muic_charger_type chg_type;
  156. ret = max14577_read_reg(rmap, MAX14577_MUIC_REG_STATUS2, &reg_data);
  157. if (ret < 0)
  158. goto out;
  159. reg_data = ((reg_data & STATUS2_CHGTYP_MASK) >> STATUS2_CHGTYP_SHIFT);
  160. chg_type = maxim_get_charger_type(chg->max14577->dev_type, reg_data);
  161. if (chg_type == MAX14577_CHARGER_TYPE_DEAD_BATTERY) {
  162. *val = POWER_SUPPLY_HEALTH_DEAD;
  163. goto out;
  164. }
  165. ret = max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, &reg_data);
  166. if (ret < 0)
  167. goto out;
  168. if (reg_data & STATUS3_OVP_MASK) {
  169. *val = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  170. goto out;
  171. }
  172. /* Not dead, not overvoltage */
  173. *val = POWER_SUPPLY_HEALTH_GOOD;
  174. out:
  175. return ret;
  176. }
  177. /*
  178. * Always returns 1.
  179. * The max14577 chip doesn't report any status of battery presence.
  180. * Lets assume that it will always be used with some battery.
  181. */
  182. static int max14577_get_present(struct max14577_charger *chg, int *val)
  183. {
  184. *val = 1;
  185. return 0;
  186. }
  187. static int max14577_set_fast_charge_timer(struct max14577_charger *chg,
  188. unsigned long hours)
  189. {
  190. u8 reg_data;
  191. switch (hours) {
  192. case 5 ... 7:
  193. reg_data = hours - 3;
  194. break;
  195. case 0:
  196. /* Disable */
  197. reg_data = 0x7;
  198. break;
  199. default:
  200. dev_err(chg->dev, "Wrong value for Fast-Charge Timer: %lu\n",
  201. hours);
  202. return -EINVAL;
  203. }
  204. reg_data <<= CHGCTRL1_TCHW_SHIFT;
  205. return max14577_update_reg(chg->max14577->regmap,
  206. MAX14577_REG_CHGCTRL1, CHGCTRL1_TCHW_MASK, reg_data);
  207. }
  208. static int max14577_init_constant_voltage(struct max14577_charger *chg,
  209. unsigned int uvolt)
  210. {
  211. u8 reg_data;
  212. if (uvolt < MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN ||
  213. uvolt > MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX)
  214. return -EINVAL;
  215. if (uvolt == 4200000)
  216. reg_data = 0x0;
  217. else if (uvolt == MAXIM_CHARGER_CONSTANT_VOLTAGE_MAX)
  218. reg_data = 0x1f;
  219. else if (uvolt <= 4280000) {
  220. unsigned int val = uvolt;
  221. val -= MAXIM_CHARGER_CONSTANT_VOLTAGE_MIN;
  222. val /= MAXIM_CHARGER_CONSTANT_VOLTAGE_STEP;
  223. if (uvolt <= 4180000)
  224. reg_data = 0x1 + val;
  225. else
  226. reg_data = val; /* Fix for gap between 4.18V and 4.22V */
  227. } else
  228. return -EINVAL;
  229. reg_data <<= CHGCTRL3_MBCCVWRC_SHIFT;
  230. return max14577_write_reg(chg->max14577->regmap,
  231. MAX14577_CHG_REG_CHG_CTRL3, reg_data);
  232. }
  233. static int max14577_init_eoc(struct max14577_charger *chg,
  234. unsigned int uamp)
  235. {
  236. unsigned int current_bits = 0xf;
  237. u8 reg_data;
  238. switch (chg->max14577->dev_type) {
  239. case MAXIM_DEVICE_TYPE_MAX77836:
  240. if (uamp < 5000)
  241. return -EINVAL; /* Requested current is too low */
  242. if (uamp >= 7500 && uamp < 10000)
  243. current_bits = 0x0;
  244. else if (uamp <= 50000) {
  245. /* <5000, 7499> and <10000, 50000> */
  246. current_bits = uamp / 5000;
  247. } else {
  248. uamp = min(uamp, 100000U) - 50000U;
  249. current_bits = 0xa + uamp / 10000;
  250. }
  251. break;
  252. case MAXIM_DEVICE_TYPE_MAX14577:
  253. default:
  254. if (uamp < MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN)
  255. return -EINVAL; /* Requested current is too low */
  256. uamp = min(uamp, MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX);
  257. uamp -= MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN;
  258. current_bits = uamp / MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP;
  259. break;
  260. }
  261. reg_data = current_bits << CHGCTRL5_EOCS_SHIFT;
  262. return max14577_update_reg(chg->max14577->regmap,
  263. MAX14577_CHG_REG_CHG_CTRL5, CHGCTRL5_EOCS_MASK,
  264. reg_data);
  265. }
  266. static int max14577_init_fast_charge(struct max14577_charger *chg,
  267. unsigned int uamp)
  268. {
  269. u8 reg_data;
  270. int ret;
  271. const struct maxim_charger_current *limits =
  272. &maxim_charger_currents[chg->max14577->dev_type];
  273. ret = maxim_charger_calc_reg_current(limits, uamp, uamp, &reg_data);
  274. if (ret) {
  275. dev_err(chg->dev, "Wrong value for fast charge: %u\n", uamp);
  276. return ret;
  277. }
  278. return max14577_update_reg(chg->max14577->regmap,
  279. MAX14577_CHG_REG_CHG_CTRL4,
  280. CHGCTRL4_MBCICHWRCL_MASK | CHGCTRL4_MBCICHWRCH_MASK,
  281. reg_data);
  282. }
  283. /*
  284. * Sets charger registers to proper and safe default values.
  285. * Some of these values are equal to defaults in MAX14577E
  286. * data sheet but there are minor differences.
  287. */
  288. static int max14577_charger_reg_init(struct max14577_charger *chg)
  289. {
  290. struct regmap *rmap = chg->max14577->regmap;
  291. u8 reg_data;
  292. int ret;
  293. /*
  294. * Charger-Type Manual Detection, default off (set CHGTYPMAN to 0)
  295. * Charger-Detection Enable, default on (set CHGDETEN to 1)
  296. * Combined mask of CHGDETEN and CHGTYPMAN will zero the CHGTYPMAN bit
  297. */
  298. reg_data = 0x1 << CDETCTRL1_CHGDETEN_SHIFT;
  299. max14577_update_reg(rmap, MAX14577_REG_CDETCTRL1,
  300. CDETCTRL1_CHGDETEN_MASK | CDETCTRL1_CHGTYPMAN_MASK,
  301. reg_data);
  302. /*
  303. * Wall-Adapter Rapid Charge, default on
  304. * Battery-Charger, default on
  305. */
  306. reg_data = 0x1 << CHGCTRL2_VCHGR_RC_SHIFT;
  307. reg_data |= 0x1 << CHGCTRL2_MBCHOSTEN_SHIFT;
  308. max14577_write_reg(rmap, MAX14577_REG_CHGCTRL2, reg_data);
  309. /* Auto Charging Stop, default off */
  310. reg_data = 0x0 << CHGCTRL6_AUTOSTOP_SHIFT;
  311. max14577_write_reg(rmap, MAX14577_REG_CHGCTRL6, reg_data);
  312. ret = max14577_init_constant_voltage(chg, chg->pdata->constant_uvolt);
  313. if (ret)
  314. return ret;
  315. ret = max14577_init_eoc(chg, chg->pdata->eoc_uamp);
  316. if (ret)
  317. return ret;
  318. ret = max14577_init_fast_charge(chg, chg->pdata->fast_charge_uamp);
  319. if (ret)
  320. return ret;
  321. ret = max14577_set_fast_charge_timer(chg,
  322. MAXIM_CHARGER_FAST_CHARGE_TIMER_DEFAULT);
  323. if (ret)
  324. return ret;
  325. /* Initialize Overvoltage-Protection Threshold */
  326. switch (chg->pdata->ovp_uvolt) {
  327. case 7500000:
  328. reg_data = 0x0;
  329. break;
  330. case 6000000:
  331. case 6500000:
  332. case 7000000:
  333. reg_data = 0x1 + (chg->pdata->ovp_uvolt - 6000000) / 500000;
  334. break;
  335. default:
  336. dev_err(chg->dev, "Wrong value for OVP: %u\n",
  337. chg->pdata->ovp_uvolt);
  338. return -EINVAL;
  339. }
  340. reg_data <<= CHGCTRL7_OTPCGHCVS_SHIFT;
  341. max14577_write_reg(rmap, MAX14577_REG_CHGCTRL7, reg_data);
  342. return 0;
  343. }
  344. /* Support property from charger */
  345. static enum power_supply_property max14577_charger_props[] = {
  346. POWER_SUPPLY_PROP_STATUS,
  347. POWER_SUPPLY_PROP_CHARGE_TYPE,
  348. POWER_SUPPLY_PROP_HEALTH,
  349. POWER_SUPPLY_PROP_PRESENT,
  350. POWER_SUPPLY_PROP_ONLINE,
  351. POWER_SUPPLY_PROP_MODEL_NAME,
  352. POWER_SUPPLY_PROP_MANUFACTURER,
  353. };
  354. static const char * const model_names[] = {
  355. [MAXIM_DEVICE_TYPE_UNKNOWN] = "MAX14577-like",
  356. [MAXIM_DEVICE_TYPE_MAX14577] = "MAX14577",
  357. [MAXIM_DEVICE_TYPE_MAX77836] = "MAX77836",
  358. };
  359. static const char *manufacturer = "Maxim Integrated";
  360. static int max14577_charger_get_property(struct power_supply *psy,
  361. enum power_supply_property psp,
  362. union power_supply_propval *val)
  363. {
  364. struct max14577_charger *chg = power_supply_get_drvdata(psy);
  365. int ret = 0;
  366. switch (psp) {
  367. case POWER_SUPPLY_PROP_STATUS:
  368. ret = max14577_get_charger_state(chg, &val->intval);
  369. break;
  370. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  371. ret = max14577_get_charge_type(chg, &val->intval);
  372. break;
  373. case POWER_SUPPLY_PROP_HEALTH:
  374. ret = max14577_get_battery_health(chg, &val->intval);
  375. break;
  376. case POWER_SUPPLY_PROP_PRESENT:
  377. ret = max14577_get_present(chg, &val->intval);
  378. break;
  379. case POWER_SUPPLY_PROP_ONLINE:
  380. ret = max14577_get_online(chg, &val->intval);
  381. break;
  382. case POWER_SUPPLY_PROP_MODEL_NAME:
  383. BUILD_BUG_ON(ARRAY_SIZE(model_names) != MAXIM_DEVICE_TYPE_NUM);
  384. val->strval = model_names[chg->max14577->dev_type];
  385. break;
  386. case POWER_SUPPLY_PROP_MANUFACTURER:
  387. val->strval = manufacturer;
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. return ret;
  393. }
  394. static const struct power_supply_desc max14577_charger_desc = {
  395. .name = "max14577-charger",
  396. .type = POWER_SUPPLY_TYPE_BATTERY,
  397. .properties = max14577_charger_props,
  398. .num_properties = ARRAY_SIZE(max14577_charger_props),
  399. .get_property = max14577_charger_get_property,
  400. };
  401. #ifdef CONFIG_OF
  402. static struct max14577_charger_platform_data *max14577_charger_dt_init(
  403. struct platform_device *pdev)
  404. {
  405. struct max14577_charger_platform_data *pdata;
  406. struct device_node *np = pdev->dev.of_node;
  407. int ret;
  408. if (!np) {
  409. dev_err(&pdev->dev, "No charger OF node\n");
  410. return ERR_PTR(-EINVAL);
  411. }
  412. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  413. if (!pdata)
  414. return ERR_PTR(-ENOMEM);
  415. ret = of_property_read_u32(np, "maxim,constant-uvolt",
  416. &pdata->constant_uvolt);
  417. if (ret) {
  418. dev_err(&pdev->dev, "Cannot parse maxim,constant-uvolt field from DT\n");
  419. return ERR_PTR(ret);
  420. }
  421. ret = of_property_read_u32(np, "maxim,fast-charge-uamp",
  422. &pdata->fast_charge_uamp);
  423. if (ret) {
  424. dev_err(&pdev->dev, "Cannot parse maxim,fast-charge-uamp field from DT\n");
  425. return ERR_PTR(ret);
  426. }
  427. ret = of_property_read_u32(np, "maxim,eoc-uamp", &pdata->eoc_uamp);
  428. if (ret) {
  429. dev_err(&pdev->dev, "Cannot parse maxim,eoc-uamp field from DT\n");
  430. return ERR_PTR(ret);
  431. }
  432. ret = of_property_read_u32(np, "maxim,ovp-uvolt", &pdata->ovp_uvolt);
  433. if (ret) {
  434. dev_err(&pdev->dev, "Cannot parse maxim,ovp-uvolt field from DT\n");
  435. return ERR_PTR(ret);
  436. }
  437. return pdata;
  438. }
  439. #else /* CONFIG_OF */
  440. static struct max14577_charger_platform_data *max14577_charger_dt_init(
  441. struct platform_device *pdev)
  442. {
  443. return NULL;
  444. }
  445. #endif /* CONFIG_OF */
  446. static ssize_t show_fast_charge_timer(struct device *dev,
  447. struct device_attribute *attr, char *buf)
  448. {
  449. struct max14577_charger *chg = dev_get_drvdata(dev);
  450. u8 reg_data;
  451. int ret;
  452. unsigned int val;
  453. ret = max14577_read_reg(chg->max14577->regmap, MAX14577_REG_CHGCTRL1,
  454. &reg_data);
  455. if (ret)
  456. return ret;
  457. reg_data &= CHGCTRL1_TCHW_MASK;
  458. reg_data >>= CHGCTRL1_TCHW_SHIFT;
  459. switch (reg_data) {
  460. case 0x2 ... 0x4:
  461. val = reg_data + 3;
  462. break;
  463. case 0x7:
  464. val = 0;
  465. break;
  466. default:
  467. val = 5;
  468. break;
  469. }
  470. return scnprintf(buf, PAGE_SIZE, "%u\n", val);
  471. }
  472. static ssize_t store_fast_charge_timer(struct device *dev,
  473. struct device_attribute *attr, const char *buf, size_t count)
  474. {
  475. struct max14577_charger *chg = dev_get_drvdata(dev);
  476. unsigned long val;
  477. int ret;
  478. ret = kstrtoul(buf, 10, &val);
  479. if (ret)
  480. return ret;
  481. ret = max14577_set_fast_charge_timer(chg, val);
  482. if (ret)
  483. return ret;
  484. return count;
  485. }
  486. static DEVICE_ATTR(fast_charge_timer, S_IRUGO | S_IWUSR,
  487. show_fast_charge_timer, store_fast_charge_timer);
  488. static int max14577_charger_probe(struct platform_device *pdev)
  489. {
  490. struct max14577_charger *chg;
  491. struct power_supply_config psy_cfg = {};
  492. struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
  493. int ret;
  494. chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
  495. if (!chg)
  496. return -ENOMEM;
  497. platform_set_drvdata(pdev, chg);
  498. chg->dev = &pdev->dev;
  499. chg->max14577 = max14577;
  500. chg->pdata = max14577_charger_dt_init(pdev);
  501. if (IS_ERR_OR_NULL(chg->pdata))
  502. return PTR_ERR(chg->pdata);
  503. ret = max14577_charger_reg_init(chg);
  504. if (ret)
  505. return ret;
  506. ret = device_create_file(&pdev->dev, &dev_attr_fast_charge_timer);
  507. if (ret) {
  508. dev_err(&pdev->dev, "failed: create sysfs entry\n");
  509. return ret;
  510. }
  511. psy_cfg.drv_data = chg;
  512. chg->charger = power_supply_register(&pdev->dev, &max14577_charger_desc,
  513. &psy_cfg);
  514. if (IS_ERR(chg->charger)) {
  515. dev_err(&pdev->dev, "failed: power supply register\n");
  516. ret = PTR_ERR(chg->charger);
  517. goto err;
  518. }
  519. /* Check for valid values for charger */
  520. BUILD_BUG_ON(MAX14577_CHARGER_EOC_CURRENT_LIMIT_MIN +
  521. MAX14577_CHARGER_EOC_CURRENT_LIMIT_STEP * 0xf !=
  522. MAX14577_CHARGER_EOC_CURRENT_LIMIT_MAX);
  523. return 0;
  524. err:
  525. device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
  526. return ret;
  527. }
  528. static int max14577_charger_remove(struct platform_device *pdev)
  529. {
  530. struct max14577_charger *chg = platform_get_drvdata(pdev);
  531. device_remove_file(&pdev->dev, &dev_attr_fast_charge_timer);
  532. power_supply_unregister(chg->charger);
  533. return 0;
  534. }
  535. static const struct platform_device_id max14577_charger_id[] = {
  536. { "max14577-charger", MAXIM_DEVICE_TYPE_MAX14577, },
  537. { "max77836-charger", MAXIM_DEVICE_TYPE_MAX77836, },
  538. { }
  539. };
  540. MODULE_DEVICE_TABLE(platform, max14577_charger_id);
  541. static struct platform_driver max14577_charger_driver = {
  542. .driver = {
  543. .name = "max14577-charger",
  544. },
  545. .probe = max14577_charger_probe,
  546. .remove = max14577_charger_remove,
  547. .id_table = max14577_charger_id,
  548. };
  549. module_platform_driver(max14577_charger_driver);
  550. MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
  551. MODULE_DESCRIPTION("Maxim 14577/77836 charger driver");
  552. MODULE_LICENSE("GPL");