qcom_smbb.c 26 KB

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