qcom_smbb.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. /* Copyright (c) 2014, Sony Mobile Communications Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * This driver is for the multi-block Switch-Mode Battery Charger and Boost
  13. * (SMBB) hardware, found in Qualcomm PM8941 PMICs. The charger is an
  14. * integrated, single-cell lithium-ion battery charger.
  15. *
  16. * Sub-components:
  17. * - Charger core
  18. * - Buck
  19. * - DC charge-path
  20. * - USB charge-path
  21. * - Battery interface
  22. * - Boost (not implemented)
  23. * - Misc
  24. * - HF-Buck
  25. */
  26. #include <linux/errno.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/power_supply.h>
  34. #include <linux/regmap.h>
  35. #include <linux/slab.h>
  36. #include <linux/extcon.h>
  37. #define SMBB_CHG_VMAX 0x040
  38. #define SMBB_CHG_VSAFE 0x041
  39. #define SMBB_CHG_CFG 0x043
  40. #define SMBB_CHG_IMAX 0x044
  41. #define SMBB_CHG_ISAFE 0x045
  42. #define SMBB_CHG_VIN_MIN 0x047
  43. #define SMBB_CHG_CTRL 0x049
  44. #define CTRL_EN BIT(7)
  45. #define SMBB_CHG_VBAT_WEAK 0x052
  46. #define SMBB_CHG_IBAT_TERM_CHG 0x05b
  47. #define IBAT_TERM_CHG_IEOC BIT(7)
  48. #define IBAT_TERM_CHG_IEOC_BMS BIT(7)
  49. #define IBAT_TERM_CHG_IEOC_CHG 0
  50. #define SMBB_CHG_VBAT_DET 0x05d
  51. #define SMBB_CHG_TCHG_MAX_EN 0x060
  52. #define TCHG_MAX_EN BIT(7)
  53. #define SMBB_CHG_WDOG_TIME 0x062
  54. #define SMBB_CHG_WDOG_EN 0x065
  55. #define WDOG_EN BIT(7)
  56. #define SMBB_BUCK_REG_MODE 0x174
  57. #define BUCK_REG_MODE BIT(0)
  58. #define BUCK_REG_MODE_VBAT BIT(0)
  59. #define BUCK_REG_MODE_VSYS 0
  60. #define SMBB_BAT_PRES_STATUS 0x208
  61. #define PRES_STATUS_BAT_PRES BIT(7)
  62. #define SMBB_BAT_TEMP_STATUS 0x209
  63. #define TEMP_STATUS_OK BIT(7)
  64. #define TEMP_STATUS_HOT BIT(6)
  65. #define SMBB_BAT_BTC_CTRL 0x249
  66. #define BTC_CTRL_COMP_EN BIT(7)
  67. #define BTC_CTRL_COLD_EXT BIT(1)
  68. #define BTC_CTRL_HOT_EXT_N BIT(0)
  69. #define SMBB_USB_IMAX 0x344
  70. #define SMBB_USB_ENUM_TIMER_STOP 0x34e
  71. #define ENUM_TIMER_STOP BIT(0)
  72. #define SMBB_USB_SEC_ACCESS 0x3d0
  73. #define SEC_ACCESS_MAGIC 0xa5
  74. #define SMBB_USB_REV_BST 0x3ed
  75. #define REV_BST_CHG_GONE BIT(7)
  76. #define SMBB_DC_IMAX 0x444
  77. #define SMBB_MISC_REV2 0x601
  78. #define SMBB_MISC_BOOT_DONE 0x642
  79. #define BOOT_DONE BIT(7)
  80. #define STATUS_USBIN_VALID BIT(0) /* USB connection is valid */
  81. #define STATUS_DCIN_VALID BIT(1) /* DC connection is valid */
  82. #define STATUS_BAT_HOT BIT(2) /* Battery temp 1=Hot, 0=Cold */
  83. #define STATUS_BAT_OK BIT(3) /* Battery temp OK */
  84. #define STATUS_BAT_PRESENT BIT(4) /* Battery is present */
  85. #define STATUS_CHG_DONE BIT(5) /* Charge cycle is complete */
  86. #define STATUS_CHG_TRKL BIT(6) /* Trickle charging */
  87. #define STATUS_CHG_FAST BIT(7) /* Fast charging */
  88. #define STATUS_CHG_GONE BIT(8) /* No charger is connected */
  89. enum smbb_attr {
  90. ATTR_BAT_ISAFE,
  91. ATTR_BAT_IMAX,
  92. ATTR_USBIN_IMAX,
  93. ATTR_DCIN_IMAX,
  94. ATTR_BAT_VSAFE,
  95. ATTR_BAT_VMAX,
  96. ATTR_BAT_VMIN,
  97. ATTR_CHG_VDET,
  98. ATTR_VIN_MIN,
  99. _ATTR_CNT,
  100. };
  101. struct smbb_charger {
  102. unsigned int revision;
  103. unsigned int addr;
  104. struct device *dev;
  105. struct extcon_dev *edev;
  106. bool dc_disabled;
  107. bool jeita_ext_temp;
  108. unsigned long status;
  109. struct mutex statlock;
  110. unsigned int attr[_ATTR_CNT];
  111. struct power_supply *usb_psy;
  112. struct power_supply *dc_psy;
  113. struct power_supply *bat_psy;
  114. struct regmap *regmap;
  115. };
  116. static const unsigned int smbb_usb_extcon_cable[] = {
  117. EXTCON_USB,
  118. EXTCON_NONE,
  119. };
  120. static int smbb_vbat_weak_fn(unsigned int index)
  121. {
  122. return 2100000 + index * 100000;
  123. }
  124. static int smbb_vin_fn(unsigned int index)
  125. {
  126. if (index > 42)
  127. return 5600000 + (index - 43) * 200000;
  128. return 3400000 + index * 50000;
  129. }
  130. static int smbb_vmax_fn(unsigned int index)
  131. {
  132. return 3240000 + index * 10000;
  133. }
  134. static int smbb_vbat_det_fn(unsigned int index)
  135. {
  136. return 3240000 + index * 20000;
  137. }
  138. static int smbb_imax_fn(unsigned int index)
  139. {
  140. if (index < 2)
  141. return 100000 + index * 50000;
  142. return index * 100000;
  143. }
  144. static int smbb_bat_imax_fn(unsigned int index)
  145. {
  146. return index * 50000;
  147. }
  148. static unsigned int smbb_hw_lookup(unsigned int val, int (*fn)(unsigned int))
  149. {
  150. unsigned int widx;
  151. unsigned int sel;
  152. for (widx = sel = 0; (*fn)(widx) <= val; ++widx)
  153. sel = widx;
  154. return sel;
  155. }
  156. static const struct smbb_charger_attr {
  157. const char *name;
  158. unsigned int reg;
  159. unsigned int safe_reg;
  160. unsigned int max;
  161. unsigned int min;
  162. unsigned int fail_ok;
  163. int (*hw_fn)(unsigned int);
  164. } smbb_charger_attrs[] = {
  165. [ATTR_BAT_ISAFE] = {
  166. .name = "qcom,fast-charge-safe-current",
  167. .reg = SMBB_CHG_ISAFE,
  168. .max = 3000000,
  169. .min = 200000,
  170. .hw_fn = smbb_bat_imax_fn,
  171. .fail_ok = 1,
  172. },
  173. [ATTR_BAT_IMAX] = {
  174. .name = "qcom,fast-charge-current-limit",
  175. .reg = SMBB_CHG_IMAX,
  176. .safe_reg = SMBB_CHG_ISAFE,
  177. .max = 3000000,
  178. .min = 200000,
  179. .hw_fn = smbb_bat_imax_fn,
  180. },
  181. [ATTR_DCIN_IMAX] = {
  182. .name = "qcom,dc-current-limit",
  183. .reg = SMBB_DC_IMAX,
  184. .max = 2500000,
  185. .min = 100000,
  186. .hw_fn = smbb_imax_fn,
  187. },
  188. [ATTR_BAT_VSAFE] = {
  189. .name = "qcom,fast-charge-safe-voltage",
  190. .reg = SMBB_CHG_VSAFE,
  191. .max = 5000000,
  192. .min = 3240000,
  193. .hw_fn = smbb_vmax_fn,
  194. .fail_ok = 1,
  195. },
  196. [ATTR_BAT_VMAX] = {
  197. .name = "qcom,fast-charge-high-threshold-voltage",
  198. .reg = SMBB_CHG_VMAX,
  199. .safe_reg = SMBB_CHG_VSAFE,
  200. .max = 5000000,
  201. .min = 3240000,
  202. .hw_fn = smbb_vmax_fn,
  203. },
  204. [ATTR_BAT_VMIN] = {
  205. .name = "qcom,fast-charge-low-threshold-voltage",
  206. .reg = SMBB_CHG_VBAT_WEAK,
  207. .max = 3600000,
  208. .min = 2100000,
  209. .hw_fn = smbb_vbat_weak_fn,
  210. },
  211. [ATTR_CHG_VDET] = {
  212. .name = "qcom,auto-recharge-threshold-voltage",
  213. .reg = SMBB_CHG_VBAT_DET,
  214. .max = 5000000,
  215. .min = 3240000,
  216. .hw_fn = smbb_vbat_det_fn,
  217. },
  218. [ATTR_VIN_MIN] = {
  219. .name = "qcom,minimum-input-voltage",
  220. .reg = SMBB_CHG_VIN_MIN,
  221. .max = 9600000,
  222. .min = 4200000,
  223. .hw_fn = smbb_vin_fn,
  224. },
  225. [ATTR_USBIN_IMAX] = {
  226. .name = "usb-charge-current-limit",
  227. .reg = SMBB_USB_IMAX,
  228. .max = 2500000,
  229. .min = 100000,
  230. .hw_fn = smbb_imax_fn,
  231. },
  232. };
  233. static int smbb_charger_attr_write(struct smbb_charger *chg,
  234. enum smbb_attr which, unsigned int val)
  235. {
  236. const struct smbb_charger_attr *prop;
  237. unsigned int wval;
  238. unsigned int out;
  239. int rc;
  240. prop = &smbb_charger_attrs[which];
  241. if (val > prop->max || val < prop->min) {
  242. dev_err(chg->dev, "value out of range for %s [%u:%u]\n",
  243. prop->name, prop->min, prop->max);
  244. return -EINVAL;
  245. }
  246. if (prop->safe_reg) {
  247. rc = regmap_read(chg->regmap,
  248. chg->addr + prop->safe_reg, &wval);
  249. if (rc) {
  250. dev_err(chg->dev,
  251. "unable to read safe value for '%s'\n",
  252. prop->name);
  253. return rc;
  254. }
  255. wval = prop->hw_fn(wval);
  256. if (val > wval) {
  257. dev_warn(chg->dev,
  258. "%s above safe value, clamping at %u\n",
  259. prop->name, wval);
  260. val = wval;
  261. }
  262. }
  263. wval = smbb_hw_lookup(val, prop->hw_fn);
  264. rc = regmap_write(chg->regmap, chg->addr + prop->reg, wval);
  265. if (rc) {
  266. dev_err(chg->dev, "unable to update %s", prop->name);
  267. return rc;
  268. }
  269. out = prop->hw_fn(wval);
  270. if (out != val) {
  271. dev_warn(chg->dev,
  272. "%s inaccurate, rounded to %u\n",
  273. prop->name, out);
  274. }
  275. dev_dbg(chg->dev, "%s <= %d\n", prop->name, out);
  276. chg->attr[which] = out;
  277. return 0;
  278. }
  279. static int smbb_charger_attr_read(struct smbb_charger *chg,
  280. enum smbb_attr which)
  281. {
  282. const struct smbb_charger_attr *prop;
  283. unsigned int val;
  284. int rc;
  285. prop = &smbb_charger_attrs[which];
  286. rc = regmap_read(chg->regmap, chg->addr + prop->reg, &val);
  287. if (rc) {
  288. dev_err(chg->dev, "failed to read %s\n", prop->name);
  289. return rc;
  290. }
  291. val = prop->hw_fn(val);
  292. dev_dbg(chg->dev, "%s => %d\n", prop->name, val);
  293. chg->attr[which] = val;
  294. return 0;
  295. }
  296. static int smbb_charger_attr_parse(struct smbb_charger *chg,
  297. enum smbb_attr which)
  298. {
  299. const struct smbb_charger_attr *prop;
  300. unsigned int val;
  301. int rc;
  302. prop = &smbb_charger_attrs[which];
  303. rc = of_property_read_u32(chg->dev->of_node, prop->name, &val);
  304. if (rc == 0) {
  305. rc = smbb_charger_attr_write(chg, which, val);
  306. if (!rc || !prop->fail_ok)
  307. return rc;
  308. }
  309. return smbb_charger_attr_read(chg, which);
  310. }
  311. static void smbb_set_line_flag(struct smbb_charger *chg, int irq, int flag)
  312. {
  313. bool state;
  314. int ret;
  315. ret = irq_get_irqchip_state(irq, IRQCHIP_STATE_LINE_LEVEL, &state);
  316. if (ret < 0) {
  317. dev_err(chg->dev, "failed to read irq line\n");
  318. return;
  319. }
  320. mutex_lock(&chg->statlock);
  321. if (state)
  322. chg->status |= flag;
  323. else
  324. chg->status &= ~flag;
  325. mutex_unlock(&chg->statlock);
  326. dev_dbg(chg->dev, "status = %03lx\n", chg->status);
  327. }
  328. static irqreturn_t smbb_usb_valid_handler(int irq, void *_data)
  329. {
  330. struct smbb_charger *chg = _data;
  331. smbb_set_line_flag(chg, irq, STATUS_USBIN_VALID);
  332. extcon_set_cable_state_(chg->edev, EXTCON_USB,
  333. chg->status & STATUS_USBIN_VALID);
  334. power_supply_changed(chg->usb_psy);
  335. return IRQ_HANDLED;
  336. }
  337. static irqreturn_t smbb_dc_valid_handler(int irq, void *_data)
  338. {
  339. struct smbb_charger *chg = _data;
  340. smbb_set_line_flag(chg, irq, STATUS_DCIN_VALID);
  341. if (!chg->dc_disabled)
  342. power_supply_changed(chg->dc_psy);
  343. return IRQ_HANDLED;
  344. }
  345. static irqreturn_t smbb_bat_temp_handler(int irq, void *_data)
  346. {
  347. struct smbb_charger *chg = _data;
  348. unsigned int val;
  349. int rc;
  350. rc = regmap_read(chg->regmap, chg->addr + SMBB_BAT_TEMP_STATUS, &val);
  351. if (rc)
  352. return IRQ_HANDLED;
  353. mutex_lock(&chg->statlock);
  354. if (val & TEMP_STATUS_OK) {
  355. chg->status |= STATUS_BAT_OK;
  356. } else {
  357. chg->status &= ~STATUS_BAT_OK;
  358. if (val & TEMP_STATUS_HOT)
  359. chg->status |= STATUS_BAT_HOT;
  360. }
  361. mutex_unlock(&chg->statlock);
  362. power_supply_changed(chg->bat_psy);
  363. return IRQ_HANDLED;
  364. }
  365. static irqreturn_t smbb_bat_present_handler(int irq, void *_data)
  366. {
  367. struct smbb_charger *chg = _data;
  368. smbb_set_line_flag(chg, irq, STATUS_BAT_PRESENT);
  369. power_supply_changed(chg->bat_psy);
  370. return IRQ_HANDLED;
  371. }
  372. static irqreturn_t smbb_chg_done_handler(int irq, void *_data)
  373. {
  374. struct smbb_charger *chg = _data;
  375. smbb_set_line_flag(chg, irq, STATUS_CHG_DONE);
  376. power_supply_changed(chg->bat_psy);
  377. return IRQ_HANDLED;
  378. }
  379. static irqreturn_t smbb_chg_gone_handler(int irq, void *_data)
  380. {
  381. struct smbb_charger *chg = _data;
  382. smbb_set_line_flag(chg, irq, STATUS_CHG_GONE);
  383. power_supply_changed(chg->bat_psy);
  384. power_supply_changed(chg->usb_psy);
  385. if (!chg->dc_disabled)
  386. power_supply_changed(chg->dc_psy);
  387. return IRQ_HANDLED;
  388. }
  389. static irqreturn_t smbb_chg_fast_handler(int irq, void *_data)
  390. {
  391. struct smbb_charger *chg = _data;
  392. smbb_set_line_flag(chg, irq, STATUS_CHG_FAST);
  393. power_supply_changed(chg->bat_psy);
  394. return IRQ_HANDLED;
  395. }
  396. static irqreturn_t smbb_chg_trkl_handler(int irq, void *_data)
  397. {
  398. struct smbb_charger *chg = _data;
  399. smbb_set_line_flag(chg, irq, STATUS_CHG_TRKL);
  400. power_supply_changed(chg->bat_psy);
  401. return IRQ_HANDLED;
  402. }
  403. static const struct smbb_irq {
  404. const char *name;
  405. irqreturn_t (*handler)(int, void *);
  406. } smbb_charger_irqs[] = {
  407. { "chg-done", smbb_chg_done_handler },
  408. { "chg-fast", smbb_chg_fast_handler },
  409. { "chg-trkl", smbb_chg_trkl_handler },
  410. { "bat-temp-ok", smbb_bat_temp_handler },
  411. { "bat-present", smbb_bat_present_handler },
  412. { "chg-gone", smbb_chg_gone_handler },
  413. { "usb-valid", smbb_usb_valid_handler },
  414. { "dc-valid", smbb_dc_valid_handler },
  415. };
  416. static int smbb_usbin_get_property(struct power_supply *psy,
  417. enum power_supply_property psp,
  418. union power_supply_propval *val)
  419. {
  420. struct smbb_charger *chg = power_supply_get_drvdata(psy);
  421. int rc = 0;
  422. switch (psp) {
  423. case POWER_SUPPLY_PROP_ONLINE:
  424. mutex_lock(&chg->statlock);
  425. val->intval = !(chg->status & STATUS_CHG_GONE) &&
  426. (chg->status & STATUS_USBIN_VALID);
  427. mutex_unlock(&chg->statlock);
  428. break;
  429. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  430. val->intval = chg->attr[ATTR_USBIN_IMAX];
  431. break;
  432. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
  433. val->intval = 2500000;
  434. break;
  435. default:
  436. rc = -EINVAL;
  437. break;
  438. }
  439. return rc;
  440. }
  441. static int smbb_usbin_set_property(struct power_supply *psy,
  442. enum power_supply_property psp,
  443. const union power_supply_propval *val)
  444. {
  445. struct smbb_charger *chg = power_supply_get_drvdata(psy);
  446. int rc;
  447. switch (psp) {
  448. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  449. rc = smbb_charger_attr_write(chg, ATTR_USBIN_IMAX,
  450. val->intval);
  451. break;
  452. default:
  453. rc = -EINVAL;
  454. break;
  455. }
  456. return rc;
  457. }
  458. static int smbb_dcin_get_property(struct power_supply *psy,
  459. enum power_supply_property psp,
  460. union power_supply_propval *val)
  461. {
  462. struct smbb_charger *chg = power_supply_get_drvdata(psy);
  463. int rc = 0;
  464. switch (psp) {
  465. case POWER_SUPPLY_PROP_ONLINE:
  466. mutex_lock(&chg->statlock);
  467. val->intval = !(chg->status & STATUS_CHG_GONE) &&
  468. (chg->status & STATUS_DCIN_VALID);
  469. mutex_unlock(&chg->statlock);
  470. break;
  471. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  472. val->intval = chg->attr[ATTR_DCIN_IMAX];
  473. break;
  474. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX:
  475. val->intval = 2500000;
  476. break;
  477. default:
  478. rc = -EINVAL;
  479. break;
  480. }
  481. return rc;
  482. }
  483. static int smbb_dcin_set_property(struct power_supply *psy,
  484. enum power_supply_property psp,
  485. const union power_supply_propval *val)
  486. {
  487. struct smbb_charger *chg = power_supply_get_drvdata(psy);
  488. int rc;
  489. switch (psp) {
  490. case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
  491. rc = smbb_charger_attr_write(chg, ATTR_DCIN_IMAX,
  492. val->intval);
  493. break;
  494. default:
  495. rc = -EINVAL;
  496. break;
  497. }
  498. return rc;
  499. }
  500. static int smbb_charger_writable_property(struct power_supply *psy,
  501. enum power_supply_property psp)
  502. {
  503. return psp == POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT;
  504. }
  505. static int smbb_battery_get_property(struct power_supply *psy,
  506. enum power_supply_property psp,
  507. union power_supply_propval *val)
  508. {
  509. struct smbb_charger *chg = power_supply_get_drvdata(psy);
  510. unsigned long status;
  511. int rc = 0;
  512. mutex_lock(&chg->statlock);
  513. status = chg->status;
  514. mutex_unlock(&chg->statlock);
  515. switch (psp) {
  516. case POWER_SUPPLY_PROP_STATUS:
  517. if (status & STATUS_CHG_GONE)
  518. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  519. else if (!(status & (STATUS_DCIN_VALID | STATUS_USBIN_VALID)))
  520. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  521. else if (status & STATUS_CHG_DONE)
  522. val->intval = POWER_SUPPLY_STATUS_FULL;
  523. else if (!(status & STATUS_BAT_OK))
  524. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  525. else if (status & (STATUS_CHG_FAST | STATUS_CHG_TRKL))
  526. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  527. else /* everything is ok for charging, but we are not... */
  528. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  529. break;
  530. case POWER_SUPPLY_PROP_HEALTH:
  531. if (status & STATUS_BAT_OK)
  532. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  533. else if (status & STATUS_BAT_HOT)
  534. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  535. else
  536. val->intval = POWER_SUPPLY_HEALTH_COLD;
  537. break;
  538. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  539. if (status & STATUS_CHG_FAST)
  540. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  541. else if (status & STATUS_CHG_TRKL)
  542. val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  543. else
  544. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  545. break;
  546. case POWER_SUPPLY_PROP_PRESENT:
  547. val->intval = !!(status & STATUS_BAT_PRESENT);
  548. break;
  549. case POWER_SUPPLY_PROP_CURRENT_MAX:
  550. val->intval = chg->attr[ATTR_BAT_IMAX];
  551. break;
  552. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  553. val->intval = chg->attr[ATTR_BAT_VMAX];
  554. break;
  555. case POWER_SUPPLY_PROP_TECHNOLOGY:
  556. /* this charger is a single-cell lithium-ion battery charger
  557. * only. If you hook up some other technology, there will be
  558. * fireworks.
  559. */
  560. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  561. break;
  562. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  563. val->intval = 3000000; /* single-cell li-ion low end */
  564. break;
  565. default:
  566. rc = -EINVAL;
  567. break;
  568. }
  569. return rc;
  570. }
  571. static int smbb_battery_set_property(struct power_supply *psy,
  572. enum power_supply_property psp,
  573. const union power_supply_propval *val)
  574. {
  575. struct smbb_charger *chg = power_supply_get_drvdata(psy);
  576. int rc;
  577. switch (psp) {
  578. case POWER_SUPPLY_PROP_CURRENT_MAX:
  579. rc = smbb_charger_attr_write(chg, ATTR_BAT_IMAX, val->intval);
  580. break;
  581. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  582. rc = smbb_charger_attr_write(chg, ATTR_BAT_VMAX, val->intval);
  583. break;
  584. default:
  585. rc = -EINVAL;
  586. break;
  587. }
  588. return rc;
  589. }
  590. static int smbb_battery_writable_property(struct power_supply *psy,
  591. enum power_supply_property psp)
  592. {
  593. switch (psp) {
  594. case POWER_SUPPLY_PROP_CURRENT_MAX:
  595. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  596. return 1;
  597. default:
  598. return 0;
  599. }
  600. }
  601. static enum power_supply_property smbb_charger_properties[] = {
  602. POWER_SUPPLY_PROP_ONLINE,
  603. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
  604. POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT_MAX,
  605. };
  606. static enum power_supply_property smbb_battery_properties[] = {
  607. POWER_SUPPLY_PROP_STATUS,
  608. POWER_SUPPLY_PROP_HEALTH,
  609. POWER_SUPPLY_PROP_PRESENT,
  610. POWER_SUPPLY_PROP_CHARGE_TYPE,
  611. POWER_SUPPLY_PROP_CURRENT_MAX,
  612. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  613. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  614. POWER_SUPPLY_PROP_TECHNOLOGY,
  615. };
  616. static const struct reg_off_mask_default {
  617. unsigned int offset;
  618. unsigned int mask;
  619. unsigned int value;
  620. unsigned int rev_mask;
  621. } smbb_charger_setup[] = {
  622. /* The bootloader is supposed to set this... make sure anyway. */
  623. { SMBB_MISC_BOOT_DONE, BOOT_DONE, BOOT_DONE },
  624. /* Disable software timer */
  625. { SMBB_CHG_TCHG_MAX_EN, TCHG_MAX_EN, 0 },
  626. /* Clear and disable watchdog */
  627. { SMBB_CHG_WDOG_TIME, 0xff, 160 },
  628. { SMBB_CHG_WDOG_EN, WDOG_EN, 0 },
  629. /* Use charger based EoC detection */
  630. { SMBB_CHG_IBAT_TERM_CHG, IBAT_TERM_CHG_IEOC, IBAT_TERM_CHG_IEOC_CHG },
  631. /* Disable GSM PA load adjustment.
  632. * The PA signal is incorrectly connected on v2.
  633. */
  634. { SMBB_CHG_CFG, 0xff, 0x00, BIT(3) },
  635. /* Use VBAT (not VSYS) to compensate for IR drop during fast charging */
  636. { SMBB_BUCK_REG_MODE, BUCK_REG_MODE, BUCK_REG_MODE_VBAT },
  637. /* Enable battery temperature comparators */
  638. { SMBB_BAT_BTC_CTRL, BTC_CTRL_COMP_EN, BTC_CTRL_COMP_EN },
  639. /* Stop USB enumeration timer */
  640. { SMBB_USB_ENUM_TIMER_STOP, ENUM_TIMER_STOP, ENUM_TIMER_STOP },
  641. #if 0 /* FIXME supposedly only to disable hardware ARB termination */
  642. { SMBB_USB_SEC_ACCESS, SEC_ACCESS_MAGIC },
  643. { SMBB_USB_REV_BST, 0xff, REV_BST_CHG_GONE },
  644. #endif
  645. /* Stop USB enumeration timer, again */
  646. { SMBB_USB_ENUM_TIMER_STOP, ENUM_TIMER_STOP, ENUM_TIMER_STOP },
  647. /* Enable charging */
  648. { SMBB_CHG_CTRL, CTRL_EN, CTRL_EN },
  649. };
  650. static char *smbb_bif[] = { "smbb-bif" };
  651. static const struct power_supply_desc bat_psy_desc = {
  652. .name = "smbb-bif",
  653. .type = POWER_SUPPLY_TYPE_BATTERY,
  654. .properties = smbb_battery_properties,
  655. .num_properties = ARRAY_SIZE(smbb_battery_properties),
  656. .get_property = smbb_battery_get_property,
  657. .set_property = smbb_battery_set_property,
  658. .property_is_writeable = smbb_battery_writable_property,
  659. };
  660. static const struct power_supply_desc usb_psy_desc = {
  661. .name = "smbb-usbin",
  662. .type = POWER_SUPPLY_TYPE_USB,
  663. .properties = smbb_charger_properties,
  664. .num_properties = ARRAY_SIZE(smbb_charger_properties),
  665. .get_property = smbb_usbin_get_property,
  666. .set_property = smbb_usbin_set_property,
  667. .property_is_writeable = smbb_charger_writable_property,
  668. };
  669. static const struct power_supply_desc dc_psy_desc = {
  670. .name = "smbb-dcin",
  671. .type = POWER_SUPPLY_TYPE_MAINS,
  672. .properties = smbb_charger_properties,
  673. .num_properties = ARRAY_SIZE(smbb_charger_properties),
  674. .get_property = smbb_dcin_get_property,
  675. .set_property = smbb_dcin_set_property,
  676. .property_is_writeable = smbb_charger_writable_property,
  677. };
  678. static int smbb_charger_probe(struct platform_device *pdev)
  679. {
  680. struct power_supply_config bat_cfg = {};
  681. struct power_supply_config usb_cfg = {};
  682. struct power_supply_config dc_cfg = {};
  683. struct smbb_charger *chg;
  684. int rc, i;
  685. chg = devm_kzalloc(&pdev->dev, sizeof(*chg), GFP_KERNEL);
  686. if (!chg)
  687. return -ENOMEM;
  688. chg->dev = &pdev->dev;
  689. mutex_init(&chg->statlock);
  690. chg->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  691. if (!chg->regmap) {
  692. dev_err(&pdev->dev, "failed to locate regmap\n");
  693. return -ENODEV;
  694. }
  695. rc = of_property_read_u32(pdev->dev.of_node, "reg", &chg->addr);
  696. if (rc) {
  697. dev_err(&pdev->dev, "missing or invalid 'reg' property\n");
  698. return rc;
  699. }
  700. rc = regmap_read(chg->regmap, chg->addr + SMBB_MISC_REV2, &chg->revision);
  701. if (rc) {
  702. dev_err(&pdev->dev, "unable to read revision\n");
  703. return rc;
  704. }
  705. chg->revision += 1;
  706. if (chg->revision != 2 && chg->revision != 3) {
  707. dev_err(&pdev->dev, "v1 hardware not supported\n");
  708. return -ENODEV;
  709. }
  710. dev_info(&pdev->dev, "Initializing SMBB rev %u", chg->revision);
  711. chg->dc_disabled = of_property_read_bool(pdev->dev.of_node, "qcom,disable-dc");
  712. for (i = 0; i < _ATTR_CNT; ++i) {
  713. rc = smbb_charger_attr_parse(chg, i);
  714. if (rc) {
  715. dev_err(&pdev->dev, "failed to parse/apply settings\n");
  716. return rc;
  717. }
  718. }
  719. bat_cfg.drv_data = chg;
  720. bat_cfg.of_node = pdev->dev.of_node;
  721. chg->bat_psy = devm_power_supply_register(&pdev->dev,
  722. &bat_psy_desc,
  723. &bat_cfg);
  724. if (IS_ERR(chg->bat_psy)) {
  725. dev_err(&pdev->dev, "failed to register battery\n");
  726. return PTR_ERR(chg->bat_psy);
  727. }
  728. usb_cfg.drv_data = chg;
  729. usb_cfg.supplied_to = smbb_bif;
  730. usb_cfg.num_supplicants = ARRAY_SIZE(smbb_bif);
  731. chg->usb_psy = devm_power_supply_register(&pdev->dev,
  732. &usb_psy_desc,
  733. &usb_cfg);
  734. if (IS_ERR(chg->usb_psy)) {
  735. dev_err(&pdev->dev, "failed to register USB power supply\n");
  736. return PTR_ERR(chg->usb_psy);
  737. }
  738. chg->edev = devm_extcon_dev_allocate(&pdev->dev, smbb_usb_extcon_cable);
  739. if (IS_ERR(chg->edev)) {
  740. dev_err(&pdev->dev, "failed to allocate extcon device\n");
  741. return -ENOMEM;
  742. }
  743. rc = devm_extcon_dev_register(&pdev->dev, chg->edev);
  744. if (rc < 0) {
  745. dev_err(&pdev->dev, "failed to register extcon device\n");
  746. return rc;
  747. }
  748. if (!chg->dc_disabled) {
  749. dc_cfg.drv_data = chg;
  750. dc_cfg.supplied_to = smbb_bif;
  751. dc_cfg.num_supplicants = ARRAY_SIZE(smbb_bif);
  752. chg->dc_psy = devm_power_supply_register(&pdev->dev,
  753. &dc_psy_desc,
  754. &dc_cfg);
  755. if (IS_ERR(chg->dc_psy)) {
  756. dev_err(&pdev->dev, "failed to register DC power supply\n");
  757. return PTR_ERR(chg->dc_psy);
  758. }
  759. }
  760. for (i = 0; i < ARRAY_SIZE(smbb_charger_irqs); ++i) {
  761. int irq;
  762. irq = platform_get_irq_byname(pdev, smbb_charger_irqs[i].name);
  763. if (irq < 0) {
  764. dev_err(&pdev->dev, "failed to get irq '%s'\n",
  765. smbb_charger_irqs[i].name);
  766. return irq;
  767. }
  768. smbb_charger_irqs[i].handler(irq, chg);
  769. rc = devm_request_threaded_irq(&pdev->dev, irq, NULL,
  770. smbb_charger_irqs[i].handler, IRQF_ONESHOT,
  771. smbb_charger_irqs[i].name, chg);
  772. if (rc) {
  773. dev_err(&pdev->dev, "failed to request irq '%s'\n",
  774. smbb_charger_irqs[i].name);
  775. return rc;
  776. }
  777. }
  778. chg->jeita_ext_temp = of_property_read_bool(pdev->dev.of_node,
  779. "qcom,jeita-extended-temp-range");
  780. /* Set temperature range to [35%:70%] or [25%:80%] accordingly */
  781. rc = regmap_update_bits(chg->regmap, chg->addr + SMBB_BAT_BTC_CTRL,
  782. BTC_CTRL_COLD_EXT | BTC_CTRL_HOT_EXT_N,
  783. chg->jeita_ext_temp ?
  784. BTC_CTRL_COLD_EXT :
  785. BTC_CTRL_HOT_EXT_N);
  786. if (rc) {
  787. dev_err(&pdev->dev,
  788. "unable to set %s temperature range\n",
  789. chg->jeita_ext_temp ? "JEITA extended" : "normal");
  790. return rc;
  791. }
  792. for (i = 0; i < ARRAY_SIZE(smbb_charger_setup); ++i) {
  793. const struct reg_off_mask_default *r = &smbb_charger_setup[i];
  794. if (r->rev_mask & BIT(chg->revision))
  795. continue;
  796. rc = regmap_update_bits(chg->regmap, chg->addr + r->offset,
  797. r->mask, r->value);
  798. if (rc) {
  799. dev_err(&pdev->dev,
  800. "unable to initializing charging, bailing\n");
  801. return rc;
  802. }
  803. }
  804. platform_set_drvdata(pdev, chg);
  805. return 0;
  806. }
  807. static int smbb_charger_remove(struct platform_device *pdev)
  808. {
  809. struct smbb_charger *chg;
  810. chg = platform_get_drvdata(pdev);
  811. regmap_update_bits(chg->regmap, chg->addr + SMBB_CHG_CTRL, CTRL_EN, 0);
  812. return 0;
  813. }
  814. static const struct of_device_id smbb_charger_id_table[] = {
  815. { .compatible = "qcom,pm8941-charger" },
  816. { }
  817. };
  818. MODULE_DEVICE_TABLE(of, smbb_charger_id_table);
  819. static struct platform_driver smbb_charger_driver = {
  820. .probe = smbb_charger_probe,
  821. .remove = smbb_charger_remove,
  822. .driver = {
  823. .name = "qcom-smbb",
  824. .of_match_table = smbb_charger_id_table,
  825. },
  826. };
  827. module_platform_driver(smbb_charger_driver);
  828. MODULE_DESCRIPTION("Qualcomm Switch-Mode Battery Charger and Boost driver");
  829. MODULE_LICENSE("GPL v2");