smb358-charger.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  1. /* Copyright (c) 2014 The Linux Foundation. All rights reserved.
  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. #define pr_fmt(fmt) "SMB358 %s: " fmt, __func__
  13. #include <linux/i2c.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/gpio.h>
  16. #include <linux/errno.h>
  17. #include <linux/delay.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <linux/power_supply.h>
  22. #include <linux/regulator/of_regulator.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/regulator/driver.h>
  26. #include <linux/of.h>
  27. #include <linux/of_gpio.h>
  28. #include <linux/mutex.h>
  29. #include <linux/qpnp/qpnp-adc.h>
  30. /* Config/Control registers */
  31. #define CHG_CURRENT_CTRL_REG 0x0
  32. #define CHG_OTH_CURRENT_CTRL_REG 0x1
  33. #define VARIOUS_FUNC_REG 0x2
  34. #define VFLOAT_REG 0x3
  35. #define CHG_CTRL_REG 0x4
  36. #define STAT_AND_TIMER_CTRL_REG 0x5
  37. #define CHG_PIN_EN_CTRL_REG 0x6
  38. #define THERM_A_CTRL_REG 0x7
  39. #define SYSOK_AND_USB3_REG 0x8
  40. #define FAULT_INT_REG 0xC
  41. #define STATUS_INT_REG 0xD
  42. /* Command registers */
  43. #define CMD_A_REG 0x30
  44. #define CMD_B_REG 0x31
  45. /* IRQ status registers */
  46. #define IRQ_A_REG 0x35
  47. #define IRQ_B_REG 0x36
  48. #define IRQ_C_REG 0x37
  49. #define IRQ_D_REG 0x38
  50. #define IRQ_E_REG 0x39
  51. #define IRQ_F_REG 0x3A
  52. /* Status registers */
  53. #define STATUS_C_REG 0x3D
  54. #define STATUS_D_REG 0x3E
  55. #define STATUS_E_REG 0x3F
  56. /* Config bits */
  57. #define CHG_INHI_EN_MASK BIT(1)
  58. #define CHG_INHI_EN_BIT BIT(1)
  59. #define CMD_A_CHG_ENABLE_BIT BIT(1)
  60. #define CMD_A_VOLATILE_W_PERM_BIT BIT(7)
  61. #define CMD_A_CHG_SUSP_EN_BIT BIT(2)
  62. #define CMD_A_CHG_SUSP_EN_MASK BIT(2)
  63. #define CMD_A_OTG_ENABLE_BIT BIT(4)
  64. #define CMD_A_OTG_ENABLE_MASK BIT(4)
  65. #define CMD_B_CHG_HC_ENABLE_BIT BIT(0)
  66. #define USB3_ENABLE_BIT BIT(5)
  67. #define USB3_ENABLE_MASK BIT(5)
  68. #define CMD_B_CHG_USB_500_900_ENABLE_BIT BIT(1)
  69. #define CHG_CTRL_AUTO_RECHARGE_ENABLE_BIT 0x0
  70. #define CHG_CTRL_CURR_TERM_END_CHG_BIT 0x0
  71. #define CHG_CTRL_BATT_MISSING_DET_THERM_IO (BIT(5) | BIT(4))
  72. #define CHG_CTRL_AUTO_RECHARGE_MASK BIT(7)
  73. #define CHG_CTRL_CURR_TERM_END_MASK BIT(6)
  74. #define CHG_CTRL_BATT_MISSING_DET_MASK (BIT(5) | BIT(4))
  75. #define CHG_CTRL_APSD_EN_BIT BIT(2)
  76. #define CHG_CTRL_APSD_EN_MASK BIT(2)
  77. #define CHG_ITERM_MASK 0x07
  78. #define CHG_PIN_CTRL_USBCS_REG_BIT 0x0
  79. /* This is to select if use external pin EN to control CHG */
  80. #define CHG_PIN_CTRL_CHG_EN_LOW_PIN_BIT (BIT(5) | BIT(6))
  81. #define CHG_PIN_CTRL_CHG_EN_LOW_REG_BIT 0x0
  82. #define CHG_PIN_CTRL_CHG_EN_MASK (BIT(5) | BIT(6))
  83. #define CHG_PIN_CTRL_USBCS_REG_MASK BIT(4)
  84. #define CHG_PIN_CTRL_APSD_IRQ_BIT BIT(1)
  85. #define CHG_PIN_CTRL_APSD_IRQ_MASK BIT(1)
  86. #define CHG_PIN_CTRL_CHG_ERR_IRQ_BIT BIT(2)
  87. #define CHG_PIN_CTRL_CHG_ERR_IRQ_MASK BIT(2)
  88. #define VARIOUS_FUNC_USB_SUSP_EN_REG_BIT BIT(6)
  89. #define VARIOUS_FUNC_USB_SUSP_MASK BIT(6)
  90. #define FAULT_INT_HOT_COLD_HARD_BIT BIT(7)
  91. #define FAULT_INT_HOT_COLD_SOFT_BIT BIT(6)
  92. #define FAULT_INT_INPUT_OV_BIT BIT(3)
  93. #define FAULT_INT_INPUT_UV_BIT BIT(2)
  94. #define FAULT_INT_AICL_COMPLETE_BIT BIT(1)
  95. #define STATUS_INT_CHG_TIMEOUT_BIT BIT(7)
  96. #define STATUS_INT_OTG_DETECT_BIT BIT(6)
  97. #define STATUS_INT_BATT_OV_BIT BIT(5)
  98. #define STATUS_INT_CHGING_BIT BIT(4)
  99. #define STATUS_INT_CHG_INHI_BIT BIT(3)
  100. #define STATUS_INT_INOK_BIT BIT(2)
  101. #define STATUS_INT_MISSING_BATT_BIT BIT(1)
  102. #define STATUS_INT_LOW_BATT_BIT BIT(0)
  103. #define THERM_A_THERM_MONITOR_EN_BIT 0x0
  104. #define THERM_A_THERM_MONITOR_EN_MASK BIT(4)
  105. #define VFLOAT_MASK 0x3F
  106. /* IRQ status bits */
  107. #define IRQ_A_HOT_HARD_BIT BIT(6)
  108. #define IRQ_A_COLD_HARD_BIT BIT(4)
  109. #define IRQ_A_HOT_SOFT_BIT BIT(2)
  110. #define IRQ_A_COLD_SOFT_BIT BIT(0)
  111. #define IRQ_B_BATT_MISSING_BIT BIT(4)
  112. #define IRQ_B_BATT_LOW_BIT BIT(2)
  113. #define IRQ_B_BATT_OV_BIT BIT(6)
  114. #define IRQ_B_PRE_FAST_CHG_BIT BIT(0)
  115. #define IRQ_C_TAPER_CHG_BIT BIT(2)
  116. #define IRQ_C_TERM_BIT BIT(0)
  117. #define IRQ_C_INT_OVER_TEMP_BIT BIT(6)
  118. #define IRQ_D_CHG_TIMEOUT_BIT (BIT(0) | BIT(2))
  119. #define IRQ_D_AICL_DONE_BIT BIT(4)
  120. #define IRQ_D_APSD_COMPLETE BIT(6)
  121. #define IRQ_E_INPUT_UV_BIT BIT(0)
  122. #define IRQ_E_INPUT_OV_BIT BIT(2)
  123. #define IRQ_E_AFVC_ACTIVE BIT(4)
  124. #define IRQ_F_OTG_VALID_BIT BIT(2)
  125. #define IRQ_F_OTG_BATT_FAIL_BIT BIT(4)
  126. #define IRQ_F_OTG_OC_BIT BIT(6)
  127. #define IRQ_F_POWER_OK BIT(0)
  128. /* Status bits */
  129. #define STATUS_C_CHARGING_MASK (BIT(1) | BIT(2))
  130. #define STATUS_C_FAST_CHARGING BIT(2)
  131. #define STATUS_C_PRE_CHARGING BIT(1)
  132. #define STATUS_C_TAPER_CHARGING (BIT(2) | BIT(1))
  133. #define STATUS_C_CHG_ERR_STATUS_BIT BIT(6)
  134. #define STATUS_C_CHG_ENABLE_STATUS_BIT BIT(0)
  135. #define STATUS_C_CHG_HOLD_OFF_BIT BIT(3)
  136. #define STATUS_D_PORT_OTHER BIT(0)
  137. #define STATUS_D_PORT_SDP BIT(1)
  138. #define STATUS_D_PORT_DCP BIT(2)
  139. #define STATUS_D_PORT_CDP BIT(3)
  140. #define STATUS_D_PORT_ACA_A BIT(4)
  141. #define STATUS_D_PORT_ACA_B BIT(5)
  142. #define STATUS_D_PORT_ACA_C BIT(6)
  143. #define STATUS_D_PORT_ACA_DOCK BIT(7)
  144. /* constants */
  145. #define USB2_MIN_CURRENT_MA 100
  146. #define USB2_MAX_CURRENT_MA 500
  147. #define USB3_MIN_CURRENT_MA 150
  148. #define USB3_MAX_CURRENT_MA 900
  149. #define AC_CHG_CURRENT_MASK 0x70
  150. #define AC_CHG_CURRENT_SHIFT 4
  151. #define SMB358_IRQ_REG_COUNT 6
  152. #define SMB358_FAST_CHG_MIN_MA 200
  153. #define SMB358_FAST_CHG_MAX_MA 2000
  154. #define SMB358_FAST_CHG_SHIFT 5
  155. #define SMB_FAST_CHG_CURRENT_MASK 0xE0
  156. #define SMB358_DEFAULT_BATT_CAPACITY 50
  157. enum {
  158. USER = BIT(0),
  159. THERMAL = BIT(1),
  160. CURRENT = BIT(2),
  161. };
  162. struct smb358_regulator {
  163. struct regulator_desc rdesc;
  164. struct regulator_dev *rdev;
  165. };
  166. struct smb358_charger {
  167. struct i2c_client *client;
  168. struct device *dev;
  169. bool recharge_disabled;
  170. int recharge_mv;
  171. bool iterm_disabled;
  172. int iterm_ma;
  173. int vfloat_mv;
  174. int chg_valid_gpio;
  175. int chg_valid_act_low;
  176. int chg_present;
  177. int fake_battery_soc;
  178. bool chg_autonomous_mode;
  179. bool disable_apsd;
  180. bool using_pmic_therm;
  181. bool battery_missing;
  182. const char *bms_psy_name;
  183. bool resume_completed;
  184. bool irq_waiting;
  185. struct mutex read_write_lock;
  186. struct mutex path_suspend_lock;
  187. struct mutex irq_complete;
  188. u8 irq_cfg_mask[2];
  189. int irq_gpio;
  190. int charging_disabled;
  191. int fastchg_current_max_ma;
  192. /* debugfs related */
  193. #if defined(CONFIG_DEBUG_FS)
  194. struct dentry *debug_root;
  195. u32 peek_poke_address;
  196. #endif
  197. /* status tracking */
  198. bool batt_full;
  199. bool batt_hot;
  200. bool batt_cold;
  201. bool batt_warm;
  202. bool batt_cool;
  203. int charging_disabled_status;
  204. int usb_suspended;
  205. /* power supply */
  206. struct power_supply *usb_psy;
  207. struct power_supply *bms_psy;
  208. struct power_supply batt_psy;
  209. /* otg 5V regulator */
  210. struct smb358_regulator otg_vreg;
  211. /* adc_tm paramters */
  212. struct qpnp_vadc_chip *vadc_dev;
  213. struct qpnp_adc_tm_chip *adc_tm_dev;
  214. struct qpnp_adc_tm_btm_param adc_param;
  215. int cold_bat_decidegc;
  216. int hot_bat_decidegc;
  217. int bat_present_decidegc;
  218. /* i2c pull up regulator */
  219. struct regulator *vcc_i2c;
  220. };
  221. struct smb_irq_info {
  222. const char *name;
  223. int (*smb_irq)(struct smb358_charger *chip,
  224. u8 rt_stat);
  225. int high;
  226. int low;
  227. };
  228. struct irq_handler_info {
  229. u8 stat_reg;
  230. u8 val;
  231. u8 prev_val;
  232. struct smb_irq_info irq_info[4];
  233. };
  234. static int chg_current[] = {
  235. 300, 500, 700, 1000, 1200, 1500, 1800, 2000,
  236. };
  237. static int fast_chg_current[] = {
  238. 200, 450, 600, 900, 1300, 1500, 1800, 2000,
  239. };
  240. /* add supplied to "bms" function */
  241. static char *pm_batt_supplied_to[] = {
  242. "bms",
  243. };
  244. static int __smb358_read_reg(struct smb358_charger *chip, u8 reg, u8 *val)
  245. {
  246. s32 ret;
  247. ret = i2c_smbus_read_byte_data(chip->client, reg);
  248. if (ret < 0) {
  249. dev_err(chip->dev,
  250. "i2c read fail: can't read from %02x: %d\n", reg, ret);
  251. return ret;
  252. } else {
  253. *val = ret;
  254. }
  255. return 0;
  256. }
  257. static int __smb358_write_reg(struct smb358_charger *chip, int reg, u8 val)
  258. {
  259. s32 ret;
  260. ret = i2c_smbus_write_byte_data(chip->client, reg, val);
  261. if (ret < 0) {
  262. dev_err(chip->dev,
  263. "i2c write fail: can't write %02x to %02x: %d\n",
  264. val, reg, ret);
  265. return ret;
  266. }
  267. return 0;
  268. }
  269. static int smb358_read_reg(struct smb358_charger *chip, int reg,
  270. u8 *val)
  271. {
  272. int rc;
  273. mutex_lock(&chip->read_write_lock);
  274. rc = __smb358_read_reg(chip, reg, val);
  275. mutex_unlock(&chip->read_write_lock);
  276. return rc;
  277. }
  278. static int smb358_write_reg(struct smb358_charger *chip, int reg,
  279. u8 val)
  280. {
  281. int rc;
  282. mutex_lock(&chip->read_write_lock);
  283. rc = __smb358_write_reg(chip, reg, val);
  284. mutex_unlock(&chip->read_write_lock);
  285. return rc;
  286. }
  287. static int smb358_masked_write(struct smb358_charger *chip, int reg,
  288. u8 mask, u8 val)
  289. {
  290. s32 rc;
  291. u8 temp;
  292. mutex_lock(&chip->read_write_lock);
  293. rc = __smb358_read_reg(chip, reg, &temp);
  294. if (rc) {
  295. dev_err(chip->dev,
  296. "smb358_read_reg Failed: reg=%03X, rc=%d\n", reg, rc);
  297. goto out;
  298. }
  299. temp &= ~mask;
  300. temp |= val & mask;
  301. rc = __smb358_write_reg(chip, reg, temp);
  302. if (rc) {
  303. dev_err(chip->dev,
  304. "smb358_write Failed: reg=%03X, rc=%d\n", reg, rc);
  305. }
  306. out:
  307. mutex_unlock(&chip->read_write_lock);
  308. return rc;
  309. }
  310. static int smb358_enable_volatile_writes(struct smb358_charger *chip)
  311. {
  312. int rc;
  313. rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_VOLATILE_W_PERM_BIT,
  314. CMD_A_VOLATILE_W_PERM_BIT);
  315. if (rc)
  316. dev_err(chip->dev, "Couldn't write VOLATILE_W_PERM_BIT rc=%d\n",
  317. rc);
  318. return rc;
  319. }
  320. static int smb358_fastchg_current_set(struct smb358_charger *chip)
  321. {
  322. int i;
  323. if ((chip->fastchg_current_max_ma < SMB358_FAST_CHG_MIN_MA) ||
  324. (chip->fastchg_current_max_ma > SMB358_FAST_CHG_MAX_MA)) {
  325. dev_dbg(chip->dev, "bad fastchg current mA=%d asked to set\n",
  326. chip->fastchg_current_max_ma);
  327. return -EINVAL;
  328. }
  329. for (i = ARRAY_SIZE(fast_chg_current) - 1; i >= 0; i--) {
  330. if (fast_chg_current[i] <= chip->fastchg_current_max_ma)
  331. break;
  332. }
  333. if (i < 0) {
  334. dev_err(chip->dev, "Invalid current setting %dmA\n",
  335. chip->fastchg_current_max_ma);
  336. i = 0;
  337. }
  338. i = i << SMB358_FAST_CHG_SHIFT;
  339. dev_dbg(chip->dev, "fastchg limit=%d setting %02x\n",
  340. chip->fastchg_current_max_ma, i);
  341. return smb358_masked_write(chip, CHG_CURRENT_CTRL_REG,
  342. SMB_FAST_CHG_CURRENT_MASK, i);
  343. }
  344. #define MIN_FLOAT_MV 3500
  345. #define MAX_FLOAT_MV 4500
  346. #define VFLOAT_STEP_MV 20
  347. #define VFLOAT_4350MV 4350
  348. static int smb358_float_voltage_set(struct smb358_charger *chip, int vfloat_mv)
  349. {
  350. u8 temp;
  351. if ((vfloat_mv < MIN_FLOAT_MV) || (vfloat_mv > MAX_FLOAT_MV)) {
  352. dev_err(chip->dev, "bad float voltage mv =%d asked to set\n",
  353. vfloat_mv);
  354. return -EINVAL;
  355. }
  356. if (VFLOAT_4350MV == vfloat_mv)
  357. temp = 0x2B;
  358. else if (vfloat_mv > VFLOAT_4350MV)
  359. temp = (vfloat_mv - MIN_FLOAT_MV) / VFLOAT_STEP_MV - 1;
  360. else
  361. temp = (vfloat_mv - MIN_FLOAT_MV) / VFLOAT_STEP_MV;
  362. return smb358_masked_write(chip, VFLOAT_REG, VFLOAT_MASK, temp);
  363. }
  364. #define CHG_ITERM_30MA 0x00
  365. #define CHG_ITERM_40MA 0x01
  366. #define CHG_ITERM_60MA 0x02
  367. #define CHG_ITERM_80MA 0x03
  368. #define CHG_ITERM_100MA 0x04
  369. #define CHG_ITERM_125MA 0x05
  370. #define CHG_ITERM_150MA 0x06
  371. #define CHG_ITERM_200MA 0x07
  372. static int smb358_term_current_set(struct smb358_charger *chip)
  373. {
  374. u8 reg = 0;
  375. int rc;
  376. if (chip->iterm_ma != -EINVAL) {
  377. if (chip->iterm_disabled)
  378. dev_err(chip->dev, "Error: Both iterm_disabled and iterm_ma set\n");
  379. if (chip->iterm_ma <= 30)
  380. reg = CHG_ITERM_30MA;
  381. else if (chip->iterm_ma <= 40)
  382. reg = CHG_ITERM_40MA;
  383. else if (chip->iterm_ma <= 60)
  384. reg = CHG_ITERM_60MA;
  385. else if (chip->iterm_ma <= 80)
  386. reg = CHG_ITERM_80MA;
  387. else if (chip->iterm_ma <= 100)
  388. reg = CHG_ITERM_100MA;
  389. else if (chip->iterm_ma <= 125)
  390. reg = CHG_ITERM_125MA;
  391. else if (chip->iterm_ma <= 150)
  392. reg = CHG_ITERM_150MA;
  393. else
  394. reg = CHG_ITERM_200MA;
  395. rc = smb358_masked_write(chip, CHG_CURRENT_CTRL_REG,
  396. CHG_ITERM_MASK, reg);
  397. if (rc) {
  398. dev_err(chip->dev,
  399. "Couldn't set iterm rc = %d\n", rc);
  400. return rc;
  401. }
  402. }
  403. if (chip->iterm_disabled) {
  404. rc = smb358_masked_write(chip, CHG_CTRL_REG,
  405. CHG_CTRL_CURR_TERM_END_MASK,
  406. CHG_CTRL_CURR_TERM_END_MASK);
  407. if (rc) {
  408. dev_err(chip->dev, "Couldn't set iterm rc = %d\n",
  409. rc);
  410. return rc;
  411. }
  412. } else {
  413. rc = smb358_masked_write(chip, CHG_CTRL_REG,
  414. CHG_CTRL_CURR_TERM_END_MASK, 0);
  415. if (rc) {
  416. dev_err(chip->dev,
  417. "Couldn't enable iterm rc = %d\n", rc);
  418. return rc;
  419. }
  420. }
  421. return 0;
  422. }
  423. #define VFLT_300MV 0x0C
  424. #define VFLT_200MV 0x08
  425. #define VFLT_100MV 0x04
  426. #define VFLT_50MV 0x00
  427. #define VFLT_MASK 0x0C
  428. static int smb358_recharge_set(struct smb358_charger *chip)
  429. {
  430. u8 reg = 0;
  431. int rc;
  432. if (chip->recharge_disabled)
  433. rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG,
  434. CHG_INHI_EN_MASK, 0x0);
  435. else
  436. rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG,
  437. CHG_INHI_EN_MASK, CHG_INHI_EN_BIT);
  438. if (rc) {
  439. dev_err(chip->dev,
  440. "Couldn't set inhibit en reg rc = %d\n", rc);
  441. return rc;
  442. }
  443. if (chip->recharge_mv != -EINVAL) {
  444. if (chip->recharge_mv <= 50)
  445. reg = VFLT_50MV;
  446. else if (chip->recharge_mv <= 100)
  447. reg = VFLT_100MV;
  448. else if (chip->recharge_mv <= 200)
  449. reg = VFLT_200MV;
  450. else
  451. reg = VFLT_300MV;
  452. rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG,
  453. VFLT_MASK, reg);
  454. if (rc) {
  455. dev_err(chip->dev,
  456. "Couldn't set inhibit threshold rc = %d\n", rc);
  457. return rc;
  458. }
  459. }
  460. return 0;
  461. }
  462. static int smb358_chg_otg_regulator_enable(struct regulator_dev *rdev)
  463. {
  464. int rc = 0;
  465. struct smb358_charger *chip = rdev_get_drvdata(rdev);
  466. rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_OTG_ENABLE_BIT,
  467. CMD_A_OTG_ENABLE_BIT);
  468. if (rc)
  469. dev_err(chip->dev, "Couldn't enable OTG mode rc=%d, reg=%2x\n",
  470. rc, CMD_A_REG);
  471. return rc;
  472. }
  473. static int smb358_chg_otg_regulator_disable(struct regulator_dev *rdev)
  474. {
  475. int rc = 0;
  476. struct smb358_charger *chip = rdev_get_drvdata(rdev);
  477. rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_OTG_ENABLE_BIT, 0);
  478. if (rc)
  479. dev_err(chip->dev, "Couldn't disable OTG mode rc=%d, reg=%2x\n",
  480. rc, CMD_A_REG);
  481. return rc;
  482. }
  483. static int smb358_chg_otg_regulator_is_enable(struct regulator_dev *rdev)
  484. {
  485. int rc = 0;
  486. u8 reg = 0;
  487. struct smb358_charger *chip = rdev_get_drvdata(rdev);
  488. rc = smb358_read_reg(chip, CMD_A_REG, &reg);
  489. if (rc) {
  490. dev_err(chip->dev,
  491. "Couldn't read OTG enable bit rc=%d, reg=%2x\n",
  492. rc, CMD_A_REG);
  493. return rc;
  494. }
  495. return (reg & CMD_A_OTG_ENABLE_BIT) ? 1 : 0;
  496. }
  497. struct regulator_ops smb358_chg_otg_reg_ops = {
  498. .enable = smb358_chg_otg_regulator_enable,
  499. .disable = smb358_chg_otg_regulator_disable,
  500. .is_enabled = smb358_chg_otg_regulator_is_enable,
  501. };
  502. static int smb358_regulator_init(struct smb358_charger *chip)
  503. {
  504. int rc = 0;
  505. struct regulator_init_data *init_data;
  506. struct regulator_config cfg;
  507. init_data = of_get_regulator_init_data(chip->dev, chip->dev->of_node);
  508. if (!init_data) {
  509. dev_err(chip->dev, "Get regulator init data failed\n");
  510. return -EINVAL;
  511. }
  512. /* Give the name, then will register */
  513. if (init_data->constraints.name) {
  514. chip->otg_vreg.rdesc.owner = THIS_MODULE;
  515. chip->otg_vreg.rdesc.type = REGULATOR_VOLTAGE;
  516. chip->otg_vreg.rdesc.ops = &smb358_chg_otg_reg_ops;
  517. chip->otg_vreg.rdesc.name = init_data->constraints.name;
  518. cfg.dev = chip->dev;
  519. cfg.init_data = init_data;
  520. cfg.driver_data = chip;
  521. cfg.of_node = chip->dev->of_node;
  522. init_data->constraints.valid_ops_mask
  523. |= REGULATOR_CHANGE_STATUS;
  524. chip->otg_vreg.rdev = regulator_register(
  525. &chip->otg_vreg.rdesc, &cfg);
  526. if (IS_ERR(chip->otg_vreg.rdev)) {
  527. rc = PTR_ERR(chip->otg_vreg.rdev);
  528. chip->otg_vreg.rdev = NULL;
  529. if (rc != -EPROBE_DEFER)
  530. dev_err(chip->dev,
  531. "OTG reg failed, rc=%d\n", rc);
  532. }
  533. }
  534. return rc;
  535. }
  536. static int __smb358_charging_disable(struct smb358_charger *chip, bool disable)
  537. {
  538. int rc;
  539. rc = smb358_masked_write(chip, CMD_A_REG, CMD_A_CHG_ENABLE_BIT,
  540. disable ? 0 : CMD_A_CHG_ENABLE_BIT);
  541. if (rc < 0)
  542. pr_err("Couldn't set CHG_ENABLE_BIT diable = %d, rc = %d\n",
  543. disable, rc);
  544. return rc;
  545. }
  546. static int smb358_charging_disable(struct smb358_charger *chip,
  547. int reason, int disable)
  548. {
  549. int rc = 0;
  550. int disabled;
  551. disabled = chip->charging_disabled_status;
  552. pr_debug("reason = %d requested_disable = %d disabled_status = %d\n",
  553. reason, disable, disabled);
  554. if (disable == true)
  555. disabled |= reason;
  556. else
  557. disabled &= ~reason;
  558. if (!!disabled == !!chip->charging_disabled_status)
  559. goto skip;
  560. rc = __smb358_charging_disable(chip, !!disabled);
  561. if (rc) {
  562. pr_err("Failed to disable charging rc = %d\n", rc);
  563. return rc;
  564. } else {
  565. /* will not modify online status in this condition */
  566. power_supply_changed(&chip->batt_psy);
  567. }
  568. skip:
  569. chip->charging_disabled_status = disabled;
  570. return rc;
  571. }
  572. static int smb358_hw_init(struct smb358_charger *chip)
  573. {
  574. int rc;
  575. u8 reg = 0, mask = 0;
  576. /*
  577. * If the charger is pre-configured for autonomous operation,
  578. * do not apply additonal settings
  579. */
  580. if (chip->chg_autonomous_mode) {
  581. dev_dbg(chip->dev, "Charger configured for autonomous mode\n");
  582. return 0;
  583. }
  584. rc = smb358_enable_volatile_writes(chip);
  585. if (rc) {
  586. dev_err(chip->dev, "Couldn't configure volatile writes rc=%d\n",
  587. rc);
  588. return rc;
  589. }
  590. /* setup defaults for CHG_CNTRL_REG */
  591. reg = CHG_CTRL_BATT_MISSING_DET_THERM_IO;
  592. mask = CHG_CTRL_BATT_MISSING_DET_MASK;
  593. rc = smb358_masked_write(chip, CHG_CTRL_REG, mask, reg);
  594. if (rc) {
  595. dev_err(chip->dev, "Couldn't set CHG_CTRL_REG rc=%d\n", rc);
  596. return rc;
  597. }
  598. /* setup defaults for PIN_CTRL_REG */
  599. reg = CHG_PIN_CTRL_USBCS_REG_BIT | CHG_PIN_CTRL_CHG_EN_LOW_REG_BIT |
  600. CHG_PIN_CTRL_APSD_IRQ_BIT | CHG_PIN_CTRL_CHG_ERR_IRQ_BIT;
  601. mask = CHG_PIN_CTRL_CHG_EN_MASK | CHG_PIN_CTRL_USBCS_REG_MASK |
  602. CHG_PIN_CTRL_APSD_IRQ_MASK | CHG_PIN_CTRL_CHG_ERR_IRQ_MASK;
  603. rc = smb358_masked_write(chip, CHG_PIN_EN_CTRL_REG, mask, reg);
  604. if (rc) {
  605. dev_err(chip->dev, "Couldn't set CHG_PIN_EN_CTRL_REG rc=%d\n",
  606. rc);
  607. return rc;
  608. }
  609. /* setup USB suspend and APSD */
  610. rc = smb358_masked_write(chip, VARIOUS_FUNC_REG,
  611. VARIOUS_FUNC_USB_SUSP_MASK, VARIOUS_FUNC_USB_SUSP_EN_REG_BIT);
  612. if (rc) {
  613. dev_err(chip->dev, "Couldn't set VARIOUS_FUNC_REG rc=%d\n",
  614. rc);
  615. return rc;
  616. }
  617. if (!chip->disable_apsd)
  618. reg = CHG_CTRL_APSD_EN_BIT;
  619. rc = smb358_masked_write(chip, CHG_CTRL_REG,
  620. CHG_CTRL_APSD_EN_MASK, reg);
  621. if (rc) {
  622. dev_err(chip->dev, "Couldn't set CHG_CTRL_REG rc=%d\n",
  623. rc);
  624. return rc;
  625. }
  626. /* Fault and Status IRQ configuration */
  627. reg = FAULT_INT_HOT_COLD_HARD_BIT | FAULT_INT_HOT_COLD_SOFT_BIT
  628. | FAULT_INT_INPUT_UV_BIT | FAULT_INT_AICL_COMPLETE_BIT
  629. | FAULT_INT_INPUT_OV_BIT;
  630. rc = smb358_write_reg(chip, FAULT_INT_REG, reg);
  631. if (rc) {
  632. dev_err(chip->dev, "Couldn't set FAULT_INT_REG rc=%d\n", rc);
  633. return rc;
  634. }
  635. reg = STATUS_INT_CHG_TIMEOUT_BIT | STATUS_INT_OTG_DETECT_BIT |
  636. STATUS_INT_BATT_OV_BIT | STATUS_INT_CHGING_BIT |
  637. STATUS_INT_CHG_INHI_BIT | STATUS_INT_INOK_BIT |
  638. STATUS_INT_LOW_BATT_BIT | STATUS_INT_MISSING_BATT_BIT;
  639. rc = smb358_write_reg(chip, STATUS_INT_REG, reg);
  640. if (rc) {
  641. dev_err(chip->dev, "Couldn't set STATUS_INT_REG rc=%d\n", rc);
  642. return rc;
  643. }
  644. /* setup THERM Monitor */
  645. rc = smb358_masked_write(chip, THERM_A_CTRL_REG,
  646. THERM_A_THERM_MONITOR_EN_MASK, THERM_A_THERM_MONITOR_EN_BIT);
  647. if (rc) {
  648. dev_err(chip->dev, "Couldn't set THERM_A_CTRL_REG rc=%d\n",
  649. rc);
  650. return rc;
  651. }
  652. /* set the fast charge current limit */
  653. rc = smb358_fastchg_current_set(chip);
  654. if (rc) {
  655. dev_err(chip->dev, "Couldn't set fastchg current rc=%d\n", rc);
  656. return rc;
  657. }
  658. /* set the float voltage */
  659. rc = smb358_float_voltage_set(chip, chip->vfloat_mv);
  660. if (rc < 0) {
  661. dev_err(chip->dev,
  662. "Couldn't set float voltage rc = %d\n", rc);
  663. return rc;
  664. }
  665. /* set iterm */
  666. rc = smb358_term_current_set(chip);
  667. if (rc)
  668. dev_err(chip->dev, "Couldn't set term current rc=%d\n", rc);
  669. /* set recharge */
  670. rc = smb358_recharge_set(chip);
  671. if (rc)
  672. dev_err(chip->dev, "Couldn't set recharge para rc=%d\n", rc);
  673. /* enable/disable charging */
  674. rc = smb358_charging_disable(chip, USER, !!chip->charging_disabled);
  675. if (rc)
  676. dev_err(chip->dev, "Couldn't '%s' charging rc = %d\n",
  677. chip->charging_disabled ? "disable" : "enable", rc);
  678. return rc;
  679. }
  680. static enum power_supply_property smb358_battery_properties[] = {
  681. POWER_SUPPLY_PROP_STATUS,
  682. POWER_SUPPLY_PROP_PRESENT,
  683. POWER_SUPPLY_PROP_CHARGING_ENABLED,
  684. POWER_SUPPLY_PROP_CHARGE_TYPE,
  685. POWER_SUPPLY_PROP_CAPACITY,
  686. POWER_SUPPLY_PROP_HEALTH,
  687. POWER_SUPPLY_PROP_TECHNOLOGY,
  688. POWER_SUPPLY_PROP_MODEL_NAME,
  689. POWER_SUPPLY_PROP_TEMP,
  690. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  691. };
  692. static int smb358_get_prop_batt_status(struct smb358_charger *chip)
  693. {
  694. int rc;
  695. u8 reg = 0;
  696. if (chip->batt_full)
  697. return POWER_SUPPLY_STATUS_FULL;
  698. rc = smb358_read_reg(chip, STATUS_C_REG, &reg);
  699. if (rc) {
  700. dev_err(chip->dev, "Couldn't read STAT_C rc = %d\n", rc);
  701. return POWER_SUPPLY_STATUS_UNKNOWN;
  702. }
  703. dev_dbg(chip->dev, "%s: STATUS_C_REG=%x\n", __func__, reg);
  704. if (reg & STATUS_C_CHG_HOLD_OFF_BIT)
  705. return POWER_SUPPLY_STATUS_NOT_CHARGING;
  706. if ((reg & STATUS_C_CHARGING_MASK) &&
  707. !(reg & STATUS_C_CHG_ERR_STATUS_BIT))
  708. return POWER_SUPPLY_STATUS_CHARGING;
  709. return POWER_SUPPLY_STATUS_DISCHARGING;
  710. }
  711. static int smb358_get_prop_batt_present(struct smb358_charger *chip)
  712. {
  713. return !chip->battery_missing;
  714. }
  715. static int smb358_get_prop_batt_capacity(struct smb358_charger *chip)
  716. {
  717. union power_supply_propval ret = {0, };
  718. if (chip->fake_battery_soc >= 0)
  719. return chip->fake_battery_soc;
  720. if (chip->bms_psy) {
  721. chip->bms_psy->get_property(chip->bms_psy,
  722. POWER_SUPPLY_PROP_CAPACITY, &ret);
  723. return ret.intval;
  724. }
  725. dev_dbg(chip->dev,
  726. "Couldn't get bms_psy, return default capacity\n");
  727. return SMB358_DEFAULT_BATT_CAPACITY;
  728. }
  729. static int smb358_get_prop_charge_type(struct smb358_charger *chip)
  730. {
  731. int rc;
  732. u8 reg = 0;
  733. rc = smb358_read_reg(chip, STATUS_C_REG, &reg);
  734. if (rc) {
  735. dev_err(chip->dev, "Couldn't read STAT_C rc = %d\n", rc);
  736. return POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
  737. }
  738. dev_dbg(chip->dev, "%s: STATUS_C_REG=%x\n", __func__, reg);
  739. reg &= STATUS_C_CHARGING_MASK;
  740. if (reg == STATUS_C_FAST_CHARGING)
  741. return POWER_SUPPLY_CHARGE_TYPE_FAST;
  742. else if (reg == STATUS_C_TAPER_CHARGING)
  743. return POWER_SUPPLY_CHARGE_TYPE_TAPER;
  744. else if (reg == STATUS_C_PRE_CHARGING)
  745. return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  746. else
  747. return POWER_SUPPLY_CHARGE_TYPE_NONE;
  748. }
  749. static int smb358_get_prop_batt_health(struct smb358_charger *chip)
  750. {
  751. union power_supply_propval ret = {0, };
  752. if (chip->batt_hot)
  753. ret.intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  754. else if (chip->batt_cold)
  755. ret.intval = POWER_SUPPLY_HEALTH_COLD;
  756. else if (chip->batt_warm)
  757. ret.intval = POWER_SUPPLY_HEALTH_WARM;
  758. else if (chip->batt_cool)
  759. ret.intval = POWER_SUPPLY_HEALTH_COOL;
  760. else
  761. ret.intval = POWER_SUPPLY_HEALTH_GOOD;
  762. return ret.intval;
  763. }
  764. #define DEFAULT_TEMP 250
  765. static int smb358_get_prop_batt_temp(struct smb358_charger *chip)
  766. {
  767. int rc = 0;
  768. struct qpnp_vadc_result results;
  769. if (!smb358_get_prop_batt_present(chip))
  770. return DEFAULT_TEMP;
  771. rc = qpnp_vadc_read(chip->vadc_dev, LR_MUX1_BATT_THERM, &results);
  772. if (rc) {
  773. pr_debug("Unable to read batt temperature rc=%d\n", rc);
  774. return DEFAULT_TEMP;
  775. }
  776. pr_debug("get_bat_temp %d, %lld\n",
  777. results.adc_code, results.physical);
  778. return (int)results.physical;
  779. }
  780. static int
  781. smb358_get_prop_battery_voltage_now(struct smb358_charger *chip)
  782. {
  783. int rc = 0;
  784. struct qpnp_vadc_result results;
  785. rc = qpnp_vadc_read(chip->vadc_dev, VBAT_SNS, &results);
  786. if (rc) {
  787. pr_err("Unable to read vbat rc=%d\n", rc);
  788. return 0;
  789. }
  790. return results.physical;
  791. }
  792. static int __smb358_path_suspend(struct smb358_charger *chip, bool suspend)
  793. {
  794. int rc;
  795. rc = smb358_masked_write(chip, CMD_A_REG,
  796. CMD_A_CHG_SUSP_EN_MASK,
  797. suspend ? CMD_A_CHG_SUSP_EN_BIT : 0);
  798. if (rc < 0)
  799. dev_err(chip->dev, "Couldn't set CMD_A reg, rc = %d\n", rc);
  800. return rc;
  801. }
  802. static int smb358_path_suspend(struct smb358_charger *chip, int reason,
  803. bool suspend)
  804. {
  805. int rc = 0;
  806. int suspended;
  807. mutex_lock(&chip->path_suspend_lock);
  808. suspended = chip->usb_suspended;
  809. if (suspend == false)
  810. suspended &= ~reason;
  811. else
  812. suspended |= reason;
  813. if (!chip->usb_suspended && suspended) {
  814. rc = __smb358_path_suspend(chip, true);
  815. chip->usb_suspended = suspended;
  816. power_supply_set_online(chip->usb_psy, !chip->usb_suspended);
  817. power_supply_changed(chip->usb_psy);
  818. } else if (chip->usb_suspended && !suspended) {
  819. rc = __smb358_path_suspend(chip, false);
  820. chip->usb_suspended = suspended;
  821. power_supply_set_online(chip->usb_psy, !chip->usb_suspended);
  822. power_supply_changed(chip->usb_psy);
  823. }
  824. if (rc)
  825. dev_err(chip->dev, "Couldn't set/unset suspend rc = %d\n", rc);
  826. mutex_unlock(&chip->path_suspend_lock);
  827. return rc;
  828. }
  829. static int smb358_set_usb_chg_current(struct smb358_charger *chip,
  830. int current_ma)
  831. {
  832. int i, rc = 0;
  833. u8 reg1 = 0, reg2 = 0, mask = 0;
  834. dev_dbg(chip->dev, "%s: USB current_ma = %d\n", __func__, current_ma);
  835. if (chip->chg_autonomous_mode) {
  836. dev_dbg(chip->dev, "%s: Charger in autonmous mode\n", __func__);
  837. return 0;
  838. }
  839. if (current_ma < USB3_MIN_CURRENT_MA && current_ma != 2)
  840. current_ma = USB2_MIN_CURRENT_MA;
  841. if (current_ma == USB2_MIN_CURRENT_MA) {
  842. /* USB 2.0 - 100mA */
  843. reg1 &= ~USB3_ENABLE_BIT;
  844. reg2 &= ~CMD_B_CHG_USB_500_900_ENABLE_BIT;
  845. } else if (current_ma == USB2_MAX_CURRENT_MA) {
  846. /* USB 2.0 - 500mA */
  847. reg1 &= ~USB3_ENABLE_BIT;
  848. reg2 |= CMD_B_CHG_USB_500_900_ENABLE_BIT;
  849. } else if (current_ma == USB3_MAX_CURRENT_MA) {
  850. /* USB 3.0 - 900mA */
  851. reg1 |= USB3_ENABLE_BIT;
  852. reg2 |= CMD_B_CHG_USB_500_900_ENABLE_BIT;
  853. } else if (current_ma > USB2_MAX_CURRENT_MA) {
  854. /* HC mode - if none of the above */
  855. reg2 |= CMD_B_CHG_HC_ENABLE_BIT;
  856. for (i = ARRAY_SIZE(chg_current) - 1; i >= 0; i--) {
  857. if (chg_current[i] <= current_ma)
  858. break;
  859. }
  860. if (i < 0) {
  861. dev_err(chip->dev, "Cannot find %dmA\n", current_ma);
  862. i = 0;
  863. }
  864. i = i << AC_CHG_CURRENT_SHIFT;
  865. rc = smb358_masked_write(chip, CHG_OTH_CURRENT_CTRL_REG,
  866. AC_CHG_CURRENT_MASK, i);
  867. if (rc)
  868. dev_err(chip->dev, "Couldn't set input mA rc=%d\n", rc);
  869. }
  870. mask = CMD_B_CHG_HC_ENABLE_BIT | CMD_B_CHG_USB_500_900_ENABLE_BIT;
  871. rc = smb358_masked_write(chip, CMD_B_REG, mask, reg2);
  872. if (rc < 0)
  873. dev_err(chip->dev, "Couldn't set charging mode rc = %d\n", rc);
  874. mask = USB3_ENABLE_MASK;
  875. rc = smb358_masked_write(chip, SYSOK_AND_USB3_REG, mask, reg1);
  876. if (rc < 0)
  877. dev_err(chip->dev, "Couldn't set USB3 mode rc = %d\n", rc);
  878. /* Only set suspend bit when chg present and current_ma = 2 */
  879. if (current_ma == 2 && chip->chg_present) {
  880. rc = smb358_path_suspend(chip, CURRENT, true);
  881. if (rc < 0)
  882. dev_err(chip->dev, "Couldn't suspend rc = %d\n", rc);
  883. } else {
  884. rc = smb358_path_suspend(chip, CURRENT, false);
  885. if (rc < 0)
  886. dev_err(chip->dev, "Couldn't set susp rc = %d\n", rc);
  887. }
  888. return rc;
  889. }
  890. static int
  891. smb358_batt_property_is_writeable(struct power_supply *psy,
  892. enum power_supply_property psp)
  893. {
  894. switch (psp) {
  895. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  896. case POWER_SUPPLY_PROP_CAPACITY:
  897. return 1;
  898. default:
  899. break;
  900. }
  901. return 0;
  902. }
  903. static int bound_soc(int soc)
  904. {
  905. soc = max(0, soc);
  906. soc = min(soc, 100);
  907. return soc;
  908. }
  909. static int smb358_battery_set_property(struct power_supply *psy,
  910. enum power_supply_property prop,
  911. const union power_supply_propval *val)
  912. {
  913. struct smb358_charger *chip = container_of(psy,
  914. struct smb358_charger, batt_psy);
  915. switch (prop) {
  916. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  917. smb358_charging_disable(chip, USER, !val->intval);
  918. smb358_path_suspend(chip, USER, !val->intval);
  919. break;
  920. case POWER_SUPPLY_PROP_CAPACITY:
  921. chip->fake_battery_soc = bound_soc(val->intval);
  922. power_supply_changed(&chip->batt_psy);
  923. break;
  924. default:
  925. return -EINVAL;
  926. }
  927. return 0;
  928. }
  929. static int smb358_battery_get_property(struct power_supply *psy,
  930. enum power_supply_property prop,
  931. union power_supply_propval *val)
  932. {
  933. struct smb358_charger *chip = container_of(psy,
  934. struct smb358_charger, batt_psy);
  935. switch (prop) {
  936. case POWER_SUPPLY_PROP_STATUS:
  937. val->intval = smb358_get_prop_batt_status(chip);
  938. break;
  939. case POWER_SUPPLY_PROP_PRESENT:
  940. val->intval = smb358_get_prop_batt_present(chip);
  941. break;
  942. case POWER_SUPPLY_PROP_CAPACITY:
  943. val->intval = smb358_get_prop_batt_capacity(chip);
  944. break;
  945. case POWER_SUPPLY_PROP_CHARGING_ENABLED:
  946. val->intval = !(chip->charging_disabled_status & USER);
  947. break;
  948. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  949. val->intval = smb358_get_prop_charge_type(chip);
  950. break;
  951. case POWER_SUPPLY_PROP_HEALTH:
  952. val->intval = smb358_get_prop_batt_health(chip);
  953. break;
  954. case POWER_SUPPLY_PROP_TECHNOLOGY:
  955. val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  956. break;
  957. case POWER_SUPPLY_PROP_MODEL_NAME:
  958. val->strval = "SMB358";
  959. break;
  960. case POWER_SUPPLY_PROP_TEMP:
  961. val->intval = smb358_get_prop_batt_temp(chip);
  962. break;
  963. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  964. val->intval = smb358_get_prop_battery_voltage_now(chip);
  965. break;
  966. default:
  967. return -EINVAL;
  968. }
  969. return 0;
  970. }
  971. static int apsd_complete(struct smb358_charger *chip, u8 status)
  972. {
  973. int rc;
  974. u8 reg = 0;
  975. enum power_supply_type type = POWER_SUPPLY_TYPE_UNKNOWN;
  976. /*
  977. * If apsd is disabled, charger detection is done by
  978. * DCIN UV irq.
  979. * status = ZERO - indicates charger removed, handled
  980. * by DCIN UV irq
  981. */
  982. if (chip->disable_apsd || status == 0) {
  983. dev_dbg(chip->dev, "APSD %s, status = %d\n",
  984. chip->disable_apsd ? "disabled" : "enabled", !!status);
  985. return 0;
  986. }
  987. rc = smb358_read_reg(chip, STATUS_D_REG, &reg);
  988. if (rc) {
  989. dev_err(chip->dev, "Couldn't read STATUS D rc = %d\n", rc);
  990. return rc;
  991. }
  992. dev_dbg(chip->dev, "%s: STATUS_D_REG=%x\n", __func__, reg);
  993. switch (reg) {
  994. case STATUS_D_PORT_ACA_DOCK:
  995. case STATUS_D_PORT_ACA_C:
  996. case STATUS_D_PORT_ACA_B:
  997. case STATUS_D_PORT_ACA_A:
  998. type = POWER_SUPPLY_TYPE_USB_ACA;
  999. break;
  1000. case STATUS_D_PORT_CDP:
  1001. type = POWER_SUPPLY_TYPE_USB_CDP;
  1002. break;
  1003. case STATUS_D_PORT_DCP:
  1004. type = POWER_SUPPLY_TYPE_USB_DCP;
  1005. break;
  1006. case STATUS_D_PORT_SDP:
  1007. type = POWER_SUPPLY_TYPE_USB;
  1008. break;
  1009. case STATUS_D_PORT_OTHER:
  1010. type = POWER_SUPPLY_TYPE_USB_DCP;
  1011. break;
  1012. default:
  1013. type = POWER_SUPPLY_TYPE_USB;
  1014. break;
  1015. }
  1016. chip->chg_present = !!status;
  1017. dev_dbg(chip->dev, "APSD complete. USB type detected=%d chg_present=%d",
  1018. type, chip->chg_present);
  1019. power_supply_set_charge_type(chip->usb_psy, type);
  1020. /* SMB is now done sampling the D+/D- lines, indicate USB driver */
  1021. dev_dbg(chip->dev, "%s updating usb_psy present=%d", __func__,
  1022. chip->chg_present);
  1023. power_supply_set_present(chip->usb_psy, chip->chg_present);
  1024. return 0;
  1025. }
  1026. static int chg_uv(struct smb358_charger *chip, u8 status)
  1027. {
  1028. /* use this to detect USB insertion only if !apsd */
  1029. if (chip->disable_apsd && status == 0) {
  1030. chip->chg_present = true;
  1031. dev_dbg(chip->dev, "%s updating usb_psy present=%d",
  1032. __func__, chip->chg_present);
  1033. power_supply_set_supply_type(chip->usb_psy,
  1034. POWER_SUPPLY_TYPE_USB);
  1035. power_supply_set_present(chip->usb_psy, chip->chg_present);
  1036. }
  1037. if (status != 0) {
  1038. chip->chg_present = false;
  1039. dev_dbg(chip->dev, "%s updating usb_psy present=%d",
  1040. __func__, chip->chg_present);
  1041. /* we can't set usb_psy as UNKNOWN here, will lead USERSPACE issue */
  1042. power_supply_set_present(chip->usb_psy, chip->chg_present);
  1043. }
  1044. power_supply_changed(chip->usb_psy);
  1045. dev_dbg(chip->dev, "chip->chg_present = %d\n", chip->chg_present);
  1046. return 0;
  1047. }
  1048. static int chg_ov(struct smb358_charger *chip, u8 status)
  1049. {
  1050. u8 psy_health_sts;
  1051. if (status)
  1052. psy_health_sts = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  1053. else
  1054. psy_health_sts = POWER_SUPPLY_HEALTH_GOOD;
  1055. power_supply_set_health_state(
  1056. chip->usb_psy, psy_health_sts);
  1057. power_supply_changed(chip->usb_psy);
  1058. return 0;
  1059. }
  1060. static int fast_chg(struct smb358_charger *chip, u8 status)
  1061. {
  1062. dev_dbg(chip->dev, "%s\n", __func__);
  1063. return 0;
  1064. }
  1065. static int chg_term(struct smb358_charger *chip, u8 status)
  1066. {
  1067. dev_dbg(chip->dev, "%s\n", __func__);
  1068. chip->batt_full = !!status;
  1069. return 0;
  1070. }
  1071. static int taper_chg(struct smb358_charger *chip, u8 status)
  1072. {
  1073. dev_dbg(chip->dev, "%s\n", __func__);
  1074. return 0;
  1075. }
  1076. static int chg_recharge(struct smb358_charger *chip, u8 status)
  1077. {
  1078. dev_dbg(chip->dev, "%s, status = %d\n", __func__, !!status);
  1079. /* to check the status mean */
  1080. chip->batt_full = !status;
  1081. return 0;
  1082. }
  1083. #define HYSTERISIS_DECIDEGC 20
  1084. static void smb_chg_adc_notification(enum qpnp_tm_state state, void *ctx)
  1085. {
  1086. struct smb358_charger *chip = ctx;
  1087. bool bat_hot = 0, bat_cold = 0, bat_present = 0;
  1088. int temp;
  1089. if (state >= ADC_TM_STATE_NUM) {
  1090. pr_err("invallid state parameter %d\n", state);
  1091. return;
  1092. }
  1093. temp = smb358_get_prop_batt_temp(chip);
  1094. pr_debug("temp = %d state = %s\n", temp,
  1095. state == ADC_TM_WARM_STATE ? "hot" : "cold");
  1096. if (state == ADC_TM_WARM_STATE) {
  1097. if (temp > chip->hot_bat_decidegc) {
  1098. /* Normal to hot */
  1099. bat_hot = true;
  1100. bat_cold = false;
  1101. bat_present = true;
  1102. chip->adc_param.low_temp =
  1103. chip->hot_bat_decidegc - HYSTERISIS_DECIDEGC;
  1104. /* shall we need add high_temp here? */
  1105. chip->adc_param.state_request =
  1106. ADC_TM_COOL_THR_ENABLE;
  1107. } else if (temp >
  1108. chip->cold_bat_decidegc + HYSTERISIS_DECIDEGC) {
  1109. /* Cool to normal */
  1110. bat_hot = false;
  1111. bat_cold = false;
  1112. bat_present = true;
  1113. chip->adc_param.low_temp = chip->cold_bat_decidegc;
  1114. chip->adc_param.high_temp = chip->hot_bat_decidegc;
  1115. chip->adc_param.state_request =
  1116. ADC_TM_HIGH_LOW_THR_ENABLE;
  1117. } else if (temp > chip->bat_present_decidegc) {
  1118. /* Present to cold */
  1119. bat_hot = false;
  1120. bat_cold = true;
  1121. bat_present = true;
  1122. chip->adc_param.high_temp = chip->cold_bat_decidegc;
  1123. chip->adc_param.low_temp = chip->bat_present_decidegc;
  1124. chip->adc_param.state_request =
  1125. ADC_TM_HIGH_LOW_THR_ENABLE;
  1126. }
  1127. } else {
  1128. if (temp <= chip->bat_present_decidegc) {
  1129. /* Cold to present */
  1130. bat_cold = true;
  1131. bat_hot = false;
  1132. bat_present = false;
  1133. chip->adc_param.high_temp =
  1134. chip->bat_present_decidegc;
  1135. chip->adc_param.state_request =
  1136. ADC_TM_WARM_THR_ENABLE;
  1137. } else if (chip->bat_present_decidegc < temp &&
  1138. temp < chip->cold_bat_decidegc) {
  1139. /* Normal to cold */
  1140. bat_hot = false;
  1141. bat_cold = true;
  1142. bat_present = true;
  1143. chip->adc_param.high_temp =
  1144. chip->cold_bat_decidegc + HYSTERISIS_DECIDEGC;
  1145. /* add low_temp to enable batt present check */
  1146. chip->adc_param.low_temp =
  1147. chip->bat_present_decidegc;
  1148. chip->adc_param.state_request =
  1149. ADC_TM_HIGH_LOW_THR_ENABLE;
  1150. } else if (temp <
  1151. chip->hot_bat_decidegc - HYSTERISIS_DECIDEGC) {
  1152. /* Warm to normal */
  1153. bat_hot = false;
  1154. bat_cold = false;
  1155. bat_present = true;
  1156. chip->adc_param.low_temp = chip->cold_bat_decidegc;
  1157. chip->adc_param.high_temp = chip->hot_bat_decidegc;
  1158. chip->adc_param.state_request =
  1159. ADC_TM_HIGH_LOW_THR_ENABLE;
  1160. }
  1161. }
  1162. if (bat_present)
  1163. chip->battery_missing = false;
  1164. else
  1165. chip->battery_missing = true;
  1166. if (bat_hot ^ chip->batt_hot || bat_cold ^ chip->batt_cold) {
  1167. chip->batt_hot = bat_hot;
  1168. chip->batt_cold = bat_cold;
  1169. /* stop charging explicitly since we use PMIC thermal pin*/
  1170. if (bat_hot || bat_cold || chip->battery_missing)
  1171. smb358_charging_disable(chip, THERMAL, 1);
  1172. else
  1173. smb358_charging_disable(chip, THERMAL, 0);
  1174. }
  1175. pr_debug("hot %d, cold %d, missing %d, low = %d deciDegC, high = %d deciDegC\n",
  1176. chip->batt_hot, chip->batt_cold, chip->battery_missing,
  1177. chip->adc_param.low_temp, chip->adc_param.high_temp);
  1178. if (qpnp_adc_tm_channel_measure(chip->adc_tm_dev, &chip->adc_param))
  1179. pr_err("request ADC error\n");
  1180. }
  1181. /* only for SMB thermal */
  1182. static int hot_hard_handler(struct smb358_charger *chip, u8 status)
  1183. {
  1184. pr_debug("status = 0x%02x\n", status);
  1185. chip->batt_hot = !!status;
  1186. return 0;
  1187. }
  1188. static int cold_hard_handler(struct smb358_charger *chip, u8 status)
  1189. {
  1190. pr_debug("status = 0x%02x\n", status);
  1191. chip->batt_cold = !!status;
  1192. return 0;
  1193. }
  1194. static int hot_soft_handler(struct smb358_charger *chip, u8 status)
  1195. {
  1196. pr_debug("status = 0x%02x\n", status);
  1197. chip->batt_warm = !!status;
  1198. return 0;
  1199. }
  1200. static int cold_soft_handler(struct smb358_charger *chip, u8 status)
  1201. {
  1202. pr_debug("status = 0x%02x\n", status);
  1203. chip->batt_cool = !!status;
  1204. return 0;
  1205. }
  1206. static int battery_missing(struct smb358_charger *chip, u8 status)
  1207. {
  1208. chip->battery_missing = !!status;
  1209. return 0;
  1210. }
  1211. static struct irq_handler_info handlers[] = {
  1212. [0] = {
  1213. .stat_reg = IRQ_A_REG,
  1214. .val = 0,
  1215. .prev_val = 0,
  1216. .irq_info = {
  1217. {
  1218. .name = "cold_soft",
  1219. .smb_irq = cold_soft_handler,
  1220. },
  1221. {
  1222. .name = "hot_soft",
  1223. .smb_irq = hot_soft_handler,
  1224. },
  1225. {
  1226. .name = "cold_hard",
  1227. .smb_irq = cold_hard_handler,
  1228. },
  1229. {
  1230. .name = "hot_hard",
  1231. .smb_irq = hot_hard_handler,
  1232. },
  1233. },
  1234. },
  1235. [1] = {
  1236. .stat_reg = IRQ_B_REG,
  1237. .val = 0,
  1238. .prev_val = 0,
  1239. .irq_info = {
  1240. {
  1241. .name = "chg_hot",
  1242. },
  1243. {
  1244. .name = "vbat_low",
  1245. },
  1246. {
  1247. .name = "battery_missing",
  1248. .smb_irq = battery_missing
  1249. },
  1250. {
  1251. .name = "battery_ov",
  1252. },
  1253. },
  1254. },
  1255. [2] = {
  1256. .stat_reg = IRQ_C_REG,
  1257. .val = 0,
  1258. .prev_val = 0,
  1259. .irq_info = {
  1260. {
  1261. .name = "chg_term",
  1262. .smb_irq = chg_term,
  1263. },
  1264. {
  1265. .name = "taper",
  1266. .smb_irq = taper_chg,
  1267. },
  1268. {
  1269. .name = "recharge",
  1270. .smb_irq = chg_recharge,
  1271. },
  1272. {
  1273. .name = "fast_chg",
  1274. .smb_irq = fast_chg,
  1275. },
  1276. },
  1277. },
  1278. [3] = {
  1279. .stat_reg = IRQ_D_REG,
  1280. .val = 0,
  1281. .prev_val = 0,
  1282. .irq_info = {
  1283. {
  1284. .name = "prechg_timeout",
  1285. },
  1286. {
  1287. .name = "safety_timeout",
  1288. },
  1289. {
  1290. .name = "aicl_complete",
  1291. },
  1292. {
  1293. .name = "src_detect",
  1294. .smb_irq = apsd_complete,
  1295. },
  1296. },
  1297. },
  1298. [4] = {
  1299. .stat_reg = IRQ_E_REG,
  1300. .val = 0,
  1301. .prev_val = 0,
  1302. .irq_info = {
  1303. {
  1304. .name = "usbin_uv",
  1305. .smb_irq = chg_uv,
  1306. },
  1307. {
  1308. .name = "usbin_ov",
  1309. .smb_irq = chg_ov,
  1310. },
  1311. {
  1312. .name = "unknown",
  1313. },
  1314. {
  1315. .name = "unknown",
  1316. },
  1317. },
  1318. },
  1319. [5] = {
  1320. .stat_reg = IRQ_F_REG,
  1321. .val = 0,
  1322. .prev_val = 0,
  1323. .irq_info = {
  1324. {
  1325. .name = "power_ok",
  1326. },
  1327. {
  1328. .name = "otg_det",
  1329. },
  1330. {
  1331. .name = "otg_batt_uv",
  1332. },
  1333. {
  1334. .name = "otg_oc",
  1335. },
  1336. },
  1337. },
  1338. };
  1339. #define IRQ_LATCHED_MASK 0x02
  1340. #define IRQ_STATUS_MASK 0x01
  1341. #define BITS_PER_IRQ 2
  1342. static irqreturn_t smb358_chg_stat_handler(int irq, void *dev_id)
  1343. {
  1344. struct smb358_charger *chip = dev_id;
  1345. int i, j;
  1346. u8 triggered;
  1347. u8 changed;
  1348. u8 rt_stat, prev_rt_stat;
  1349. int rc;
  1350. int handler_count = 0;
  1351. mutex_lock(&chip->irq_complete);
  1352. chip->irq_waiting = true;
  1353. if (!chip->resume_completed) {
  1354. dev_dbg(chip->dev, "IRQ triggered before device-resume\n");
  1355. disable_irq_nosync(irq);
  1356. mutex_unlock(&chip->irq_complete);
  1357. return IRQ_HANDLED;
  1358. }
  1359. chip->irq_waiting = false;
  1360. for (i = 0; i < ARRAY_SIZE(handlers); i++) {
  1361. rc = smb358_read_reg(chip, handlers[i].stat_reg,
  1362. &handlers[i].val);
  1363. if (rc < 0) {
  1364. dev_err(chip->dev, "Couldn't read %d rc = %d\n",
  1365. handlers[i].stat_reg, rc);
  1366. continue;
  1367. }
  1368. for (j = 0; j < ARRAY_SIZE(handlers[i].irq_info); j++) {
  1369. triggered = handlers[i].val
  1370. & (IRQ_LATCHED_MASK << (j * BITS_PER_IRQ));
  1371. rt_stat = handlers[i].val
  1372. & (IRQ_STATUS_MASK << (j * BITS_PER_IRQ));
  1373. prev_rt_stat = handlers[i].prev_val
  1374. & (IRQ_STATUS_MASK << (j * BITS_PER_IRQ));
  1375. changed = prev_rt_stat ^ rt_stat;
  1376. if (triggered || changed)
  1377. rt_stat ? handlers[i].irq_info[j].high++ :
  1378. handlers[i].irq_info[j].low++;
  1379. if ((triggered || changed)
  1380. && handlers[i].irq_info[j].smb_irq != NULL) {
  1381. handler_count++;
  1382. rc = handlers[i].irq_info[j].smb_irq(chip,
  1383. rt_stat);
  1384. if (rc < 0)
  1385. dev_err(chip->dev,
  1386. "Couldn't handle %d irq for reg 0x%02x rc = %d\n",
  1387. j, handlers[i].stat_reg, rc);
  1388. }
  1389. }
  1390. handlers[i].prev_val = handlers[i].val;
  1391. }
  1392. pr_debug("handler count = %d\n", handler_count);
  1393. if (handler_count) {
  1394. pr_debug("batt psy changed\n");
  1395. power_supply_changed(&chip->batt_psy);
  1396. }
  1397. mutex_unlock(&chip->irq_complete);
  1398. return IRQ_HANDLED;
  1399. }
  1400. static irqreturn_t smb358_chg_valid_handler(int irq, void *dev_id)
  1401. {
  1402. struct smb358_charger *chip = dev_id;
  1403. int present;
  1404. present = gpio_get_value_cansleep(chip->chg_valid_gpio);
  1405. if (present < 0) {
  1406. dev_err(chip->dev, "Couldn't read chg_valid gpio=%d\n",
  1407. chip->chg_valid_gpio);
  1408. return IRQ_HANDLED;
  1409. }
  1410. present ^= chip->chg_valid_act_low;
  1411. dev_dbg(chip->dev, "%s: chg_present = %d\n", __func__, present);
  1412. if (present != chip->chg_present) {
  1413. chip->chg_present = present;
  1414. dev_dbg(chip->dev, "%s updating usb_psy present=%d",
  1415. __func__, chip->chg_present);
  1416. power_supply_set_present(chip->usb_psy, chip->chg_present);
  1417. }
  1418. return IRQ_HANDLED;
  1419. }
  1420. static void smb358_external_power_changed(struct power_supply *psy)
  1421. {
  1422. struct smb358_charger *chip = container_of(psy,
  1423. struct smb358_charger, batt_psy);
  1424. union power_supply_propval prop = {0,};
  1425. int rc, current_limit = 0;
  1426. if (chip->bms_psy_name)
  1427. chip->bms_psy =
  1428. power_supply_get_by_name((char *)chip->bms_psy_name);
  1429. rc = chip->usb_psy->get_property(chip->usb_psy,
  1430. POWER_SUPPLY_PROP_CURRENT_MAX, &prop);
  1431. if (rc)
  1432. dev_err(chip->dev,
  1433. "Couldn't read USB current_max property, rc=%d\n", rc);
  1434. else
  1435. current_limit = prop.intval / 1000;
  1436. smb358_enable_volatile_writes(chip);
  1437. smb358_set_usb_chg_current(chip, current_limit);
  1438. dev_dbg(chip->dev, "current_limit = %d\n", current_limit);
  1439. }
  1440. #if defined(CONFIG_DEBUG_FS)
  1441. #define LAST_CNFG_REG 0x13
  1442. static int show_cnfg_regs(struct seq_file *m, void *data)
  1443. {
  1444. struct smb358_charger *chip = m->private;
  1445. int rc;
  1446. u8 reg;
  1447. u8 addr;
  1448. for (addr = 0; addr <= LAST_CNFG_REG; addr++) {
  1449. rc = smb358_read_reg(chip, addr, &reg);
  1450. if (!rc)
  1451. seq_printf(m, "0x%02x = 0x%02x\n", addr, reg);
  1452. }
  1453. return 0;
  1454. }
  1455. static int cnfg_debugfs_open(struct inode *inode, struct file *file)
  1456. {
  1457. struct smb358_charger *chip = inode->i_private;
  1458. return single_open(file, show_cnfg_regs, chip);
  1459. }
  1460. static const struct file_operations cnfg_debugfs_ops = {
  1461. .owner = THIS_MODULE,
  1462. .open = cnfg_debugfs_open,
  1463. .read = seq_read,
  1464. .llseek = seq_lseek,
  1465. .release = single_release,
  1466. };
  1467. #define FIRST_CMD_REG 0x30
  1468. #define LAST_CMD_REG 0x33
  1469. static int show_cmd_regs(struct seq_file *m, void *data)
  1470. {
  1471. struct smb358_charger *chip = m->private;
  1472. int rc;
  1473. u8 reg;
  1474. u8 addr;
  1475. for (addr = FIRST_CMD_REG; addr <= LAST_CMD_REG; addr++) {
  1476. rc = smb358_read_reg(chip, addr, &reg);
  1477. if (!rc)
  1478. seq_printf(m, "0x%02x = 0x%02x\n", addr, reg);
  1479. }
  1480. return 0;
  1481. }
  1482. static int cmd_debugfs_open(struct inode *inode, struct file *file)
  1483. {
  1484. struct smb358_charger *chip = inode->i_private;
  1485. return single_open(file, show_cmd_regs, chip);
  1486. }
  1487. static const struct file_operations cmd_debugfs_ops = {
  1488. .owner = THIS_MODULE,
  1489. .open = cmd_debugfs_open,
  1490. .read = seq_read,
  1491. .llseek = seq_lseek,
  1492. .release = single_release,
  1493. };
  1494. #define FIRST_STATUS_REG 0x35
  1495. #define LAST_STATUS_REG 0x3F
  1496. static int show_status_regs(struct seq_file *m, void *data)
  1497. {
  1498. struct smb358_charger *chip = m->private;
  1499. int rc;
  1500. u8 reg;
  1501. u8 addr;
  1502. for (addr = FIRST_STATUS_REG; addr <= LAST_STATUS_REG; addr++) {
  1503. rc = smb358_read_reg(chip, addr, &reg);
  1504. if (!rc)
  1505. seq_printf(m, "0x%02x = 0x%02x\n", addr, reg);
  1506. }
  1507. return 0;
  1508. }
  1509. static int status_debugfs_open(struct inode *inode, struct file *file)
  1510. {
  1511. struct smb358_charger *chip = inode->i_private;
  1512. return single_open(file, show_status_regs, chip);
  1513. }
  1514. static const struct file_operations status_debugfs_ops = {
  1515. .owner = THIS_MODULE,
  1516. .open = status_debugfs_open,
  1517. .read = seq_read,
  1518. .llseek = seq_lseek,
  1519. .release = single_release,
  1520. };
  1521. static int show_irq_count(struct seq_file *m, void *data)
  1522. {
  1523. int i, j, total = 0;
  1524. for (i = 0; i < ARRAY_SIZE(handlers); i++)
  1525. for (j = 0; j < 4; j++) {
  1526. seq_printf(m, "%s=%d\t(high=%d low=%d)\n",
  1527. handlers[i].irq_info[j].name,
  1528. handlers[i].irq_info[j].high
  1529. + handlers[i].irq_info[j].low,
  1530. handlers[i].irq_info[j].high,
  1531. handlers[i].irq_info[j].low);
  1532. total += (handlers[i].irq_info[j].high
  1533. + handlers[i].irq_info[j].low);
  1534. }
  1535. seq_printf(m, "\n\tTotal = %d\n", total);
  1536. return 0;
  1537. }
  1538. static int irq_count_debugfs_open(struct inode *inode, struct file *file)
  1539. {
  1540. struct smb358_charger *chip = inode->i_private;
  1541. return single_open(file, show_irq_count, chip);
  1542. }
  1543. static const struct file_operations irq_count_debugfs_ops = {
  1544. .owner = THIS_MODULE,
  1545. .open = irq_count_debugfs_open,
  1546. .read = seq_read,
  1547. .llseek = seq_lseek,
  1548. .release = single_release,
  1549. };
  1550. static int get_reg(void *data, u64 *val)
  1551. {
  1552. struct smb358_charger *chip = data;
  1553. int rc;
  1554. u8 temp;
  1555. rc = smb358_read_reg(chip, chip->peek_poke_address, &temp);
  1556. if (rc < 0) {
  1557. dev_err(chip->dev,
  1558. "Couldn't read reg %x rc = %d\n",
  1559. chip->peek_poke_address, rc);
  1560. return -EAGAIN;
  1561. }
  1562. *val = temp;
  1563. return 0;
  1564. }
  1565. static int set_reg(void *data, u64 val)
  1566. {
  1567. struct smb358_charger *chip = data;
  1568. int rc;
  1569. u8 temp;
  1570. temp = (u8) val;
  1571. rc = smb358_write_reg(chip, chip->peek_poke_address, temp);
  1572. if (rc < 0) {
  1573. dev_err(chip->dev,
  1574. "Couldn't write 0x%02x to 0x%02x rc= %d\n",
  1575. chip->peek_poke_address, temp, rc);
  1576. return -EAGAIN;
  1577. }
  1578. return 0;
  1579. }
  1580. DEFINE_SIMPLE_ATTRIBUTE(poke_poke_debug_ops, get_reg, set_reg, "0x%02llx\n");
  1581. static int force_irq_set(void *data, u64 val)
  1582. {
  1583. struct smb358_charger *chip = data;
  1584. smb358_chg_stat_handler(chip->client->irq, data);
  1585. return 0;
  1586. }
  1587. DEFINE_SIMPLE_ATTRIBUTE(force_irq_ops, NULL, force_irq_set, "0x%02llx\n");
  1588. #endif
  1589. #ifdef DEBUG
  1590. static void dump_regs(struct smb358_charger *chip)
  1591. {
  1592. int rc;
  1593. u8 reg;
  1594. u8 addr;
  1595. for (addr = 0; addr <= LAST_CNFG_REG; addr++) {
  1596. rc = smb358_read_reg(chip, addr, &reg);
  1597. if (rc)
  1598. dev_err(chip->dev, "Couldn't read 0x%02x rc = %d\n",
  1599. addr, rc);
  1600. else
  1601. pr_debug("0x%02x = 0x%02x\n", addr, reg);
  1602. }
  1603. for (addr = FIRST_STATUS_REG; addr <= LAST_STATUS_REG; addr++) {
  1604. rc = smb358_read_reg(chip, addr, &reg);
  1605. if (rc)
  1606. dev_err(chip->dev, "Couldn't read 0x%02x rc = %d\n",
  1607. addr, rc);
  1608. else
  1609. pr_debug("0x%02x = 0x%02x\n", addr, reg);
  1610. }
  1611. for (addr = FIRST_CMD_REG; addr <= LAST_CMD_REG; addr++) {
  1612. rc = smb358_read_reg(chip, addr, &reg);
  1613. if (rc)
  1614. dev_err(chip->dev, "Couldn't read 0x%02x rc = %d\n",
  1615. addr, rc);
  1616. else
  1617. pr_debug("0x%02x = 0x%02x\n", addr, reg);
  1618. }
  1619. }
  1620. #else
  1621. static void dump_regs(struct smb358_charger *chip)
  1622. {
  1623. }
  1624. #endif
  1625. static int smb_parse_dt(struct smb358_charger *chip)
  1626. {
  1627. int rc;
  1628. enum of_gpio_flags gpio_flags;
  1629. struct device_node *node = chip->dev->of_node;
  1630. int batt_present_degree_negative;
  1631. if (!node) {
  1632. dev_err(chip->dev, "device tree info. missing\n");
  1633. return -EINVAL;
  1634. }
  1635. chip->charging_disabled = of_property_read_bool(node,
  1636. "qcom,charger-disabled");
  1637. chip->chg_autonomous_mode = of_property_read_bool(node,
  1638. "qcom,chg-autonomous-mode");
  1639. chip->disable_apsd = of_property_read_bool(node, "qcom,disable-apsd");
  1640. chip->using_pmic_therm = of_property_read_bool(node,
  1641. "qcom,using-pmic-therm");
  1642. rc = of_property_read_string(node, "qcom,bms-psy-name",
  1643. &chip->bms_psy_name);
  1644. if (rc)
  1645. chip->bms_psy_name = NULL;
  1646. chip->chg_valid_gpio = of_get_named_gpio_flags(node,
  1647. "qcom,chg-valid-gpio", 0, &gpio_flags);
  1648. if (!gpio_is_valid(chip->chg_valid_gpio))
  1649. dev_dbg(chip->dev, "Invalid chg-valid-gpio");
  1650. else
  1651. chip->chg_valid_act_low = gpio_flags & OF_GPIO_ACTIVE_LOW;
  1652. rc = of_property_read_u32(node, "qcom,fastchg-current-max-ma",
  1653. &chip->fastchg_current_max_ma);
  1654. if (rc)
  1655. chip->fastchg_current_max_ma = SMB358_FAST_CHG_MAX_MA;
  1656. chip->ieerm_disabled = of_property_read_bool(node,
  1657. "qcom,iterm-disabled");
  1658. rc = of_property_read_u32(node, "qcom,iterm-ma", &chip->iterm_ma);
  1659. if (rc < 0)
  1660. chip->iterm_ma = -EINVAL;
  1661. rc = of_property_read_u32(node, "qcom,float-voltage-mv",
  1662. &chip->vfloat_mv);
  1663. if (rc < 0) {
  1664. chip->vfloat_mv = -EINVAL;
  1665. pr_err("float-voltage-mv property missing, exit\n");
  1666. return -EINVAL;
  1667. }
  1668. rc = of_property_read_u32(node, "qcom,recharge-mv",
  1669. &chip->recharge_mv);
  1670. if (rc < 0)
  1671. chip->recharge_mv = -EINVAL;
  1672. chip->recharge_disabled = of_property_read_bool(node,
  1673. "qcom,recharge-disabled");
  1674. rc = of_property_read_u32(node, "qcom,cold-bat-decidegc",
  1675. &chip->cold_bat_decidegc);
  1676. if (rc < 0)
  1677. chip->cold_bat_decidegc = -EINVAL;
  1678. rc = of_property_read_u32(node, "qcom,hot-bat-decidegc",
  1679. &chip->hot_bat_decidegc);
  1680. if (rc < 0)
  1681. chip->hot_bat_decidegc = -EINVAL;
  1682. rc = of_property_read_u32(node, "qcom,bat-present-decidegc",
  1683. &batt_present_degree_negative);
  1684. if (rc < 0)
  1685. chip->bat_present_decidegc = -EINVAL;
  1686. else
  1687. chip->bat_present_decidegc = -batt_present_degree_negative;
  1688. pr_debug("recharge-disabled = %d, recharge-mv = %d,",
  1689. chip->recharge_disabled, chip->recharge_mv);
  1690. pr_debug("vfloat-mv = %d, iterm-disabled = %d,",
  1691. chip->vfloat_mv, chip->iterm_ma);
  1692. pr_debug("fastchg-current = %d, charging-disabled = %d,",
  1693. chip->fastchg_current_max_ma,
  1694. chip->charging_disabled);
  1695. pr_debug("disable-apsd = %d bms = %s cold-bat-degree = %d,",
  1696. chip->disable_apsd, chip->bms_psy_name,
  1697. chip->cold_bat_decidegc);
  1698. pr_debug("hot-bat-degree = %d, bat-present-decidegc = %d\n",
  1699. chip->hot_bat_decidegc, chip->bat_present_decidegc);
  1700. return 0;
  1701. }
  1702. static int determine_initial_state(struct smb358_charger *chip)
  1703. {
  1704. int rc;
  1705. u8 reg = 0;
  1706. rc = smb358_read_reg(chip, IRQ_B_REG, &reg);
  1707. if (rc) {
  1708. dev_err(chip->dev, "Couldn't read IRQ_B rc = %d\n", rc);
  1709. goto fail_init_status;
  1710. }
  1711. rc = smb358_read_reg(chip, IRQ_C_REG, &reg);
  1712. if (rc) {
  1713. dev_err(chip->dev, "Couldn't read IRQ_C rc = %d\n", rc);
  1714. goto fail_init_status;
  1715. }
  1716. chip->batt_full = (reg & IRQ_C_TERM_BIT) ? true : false;
  1717. rc = smb358_read_reg(chip, IRQ_A_REG, &reg);
  1718. if (rc < 0) {
  1719. dev_err(chip->dev, "Couldn't read irq A rc = %d\n", rc);
  1720. return rc;
  1721. }
  1722. /* For current design, can ignore this */
  1723. if (reg & IRQ_A_HOT_HARD_BIT)
  1724. chip->batt_hot = true;
  1725. if (reg & IRQ_A_COLD_HARD_BIT)
  1726. chip->batt_cold = true;
  1727. if (reg & IRQ_A_HOT_SOFT_BIT)
  1728. chip->batt_warm = true;
  1729. if (reg & IRQ_A_COLD_SOFT_BIT)
  1730. chip->batt_cool = true;
  1731. rc = smb358_read_reg(chip, IRQ_E_REG, &reg);
  1732. if (rc) {
  1733. dev_err(chip->dev, "Couldn't read IRQ_E rc = %d\n", rc);
  1734. goto fail_init_status;
  1735. }
  1736. if (reg & IRQ_E_INPUT_UV_BIT) {
  1737. chg_uv(chip, 1);
  1738. } else {
  1739. chg_uv(chip, 0);
  1740. apsd_complete(chip, 1);
  1741. }
  1742. return 0;
  1743. fail_init_status:
  1744. dev_err(chip->dev, "Couldn't determine initial status\n");
  1745. return rc;
  1746. }
  1747. #if defined(CONFIG_DEBUG_FS)
  1748. static void smb358_debugfs_init(struct smb358_charger *chip)
  1749. {
  1750. int rc;
  1751. chip->debug_root = debugfs_create_dir("smb358", NULL);
  1752. if (!chip->debug_root)
  1753. dev_err(chip->dev, "Couldn't create debug dir\n");
  1754. if (chip->debug_root) {
  1755. struct dentry *ent;
  1756. ent = debugfs_create_file("config_registers", S_IFREG | S_IRUGO,
  1757. chip->debug_root, chip,
  1758. &cnfg_debugfs_ops);
  1759. if (!ent || IS_ERR(ent)) {
  1760. rc = PTR_ERR(ent);
  1761. dev_err(chip->dev,
  1762. "Couldn't create cnfg debug file rc = %d\n",
  1763. rc);
  1764. }
  1765. ent = debugfs_create_file("status_registers", S_IFREG | S_IRUGO,
  1766. chip->debug_root, chip,
  1767. &status_debugfs_ops);
  1768. if (!ent || IS_ERR(ent)) {
  1769. rc = PTR_ERR(ent);
  1770. dev_err(chip->dev,
  1771. "Couldn't create status debug file rc = %d\n",
  1772. rc);
  1773. }
  1774. ent = debugfs_create_file("cmd_registers", S_IFREG | S_IRUGO,
  1775. chip->debug_root, chip,
  1776. &cmd_debugfs_ops);
  1777. if (!ent || IS_ERR(ent)) {
  1778. rc = PTR_ERR(ent);
  1779. dev_err(chip->dev,
  1780. "Couldn't create cmd debug file rc = %d\n",
  1781. rc);
  1782. }
  1783. ent = debugfs_create_x32("address", S_IFREG | S_IWUSR | S_IRUGO,
  1784. chip->debug_root,
  1785. &(chip->peek_poke_address));
  1786. if (!ent || IS_ERR(ent)) {
  1787. rc = PTR_ERR(ent);
  1788. dev_err(chip->dev,
  1789. "Couldn't create address debug file rc = %d\n",
  1790. rc);
  1791. }
  1792. ent = debugfs_create_file("data", S_IFREG | S_IWUSR | S_IRUGO,
  1793. chip->debug_root, chip,
  1794. &poke_poke_debug_ops);
  1795. if (!ent || IS_ERR(ent)) {
  1796. rc = PTR_ERR(ent);
  1797. dev_err(chip->dev,
  1798. "Couldn't create data debug file rc = %d\n",
  1799. rc);
  1800. }
  1801. ent = debugfs_create_file("force_irq",
  1802. S_IFREG | S_IWUSR | S_IRUGO,
  1803. chip->debug_root, chip,
  1804. &force_irq_ops);
  1805. if (!ent || IS_ERR(ent)) {
  1806. rc = PTR_ERR(ent);
  1807. dev_err(chip->dev,
  1808. "Couldn't create force_irq debug file rc =%d\n",
  1809. rc);
  1810. }
  1811. ent = debugfs_create_file("irq_count", S_IFREG | S_IRUGO,
  1812. chip->debug_root, chip,
  1813. &irq_count_debugfs_ops);
  1814. if (!ent || IS_ERR(ent)) {
  1815. rc = PTR_ERR(ent);
  1816. dev_err(chip->dev,
  1817. "Couldn't create cnfg irq_count file rc = %d\n",
  1818. rc);
  1819. }
  1820. }
  1821. }
  1822. #else
  1823. static void smb358_debugfs_init(struct smb358_charger *chip)
  1824. {
  1825. }
  1826. #endif
  1827. #define SMB_I2C_VTG_MIN_UV 1800000
  1828. #define SMB_I2C_VTG_MAX_UV 1800000
  1829. static int smb358_charger_probe(struct i2c_client *client,
  1830. const struct i2c_device_id *id)
  1831. {
  1832. int rc, irq;
  1833. struct smb358_charger *chip;
  1834. struct power_supply *usb_psy;
  1835. u8 reg = 0;
  1836. usb_psy = power_supply_get_by_name("usb");
  1837. if (!usb_psy) {
  1838. dev_dbg(&client->dev, "USB psy not found; deferring probe\n");
  1839. return -EPROBE_DEFER;
  1840. }
  1841. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  1842. if (!chip) {
  1843. dev_err(&client->dev, "Couldn't allocate memory\n");
  1844. return -ENOMEM;
  1845. }
  1846. chip->client = client;
  1847. chip->dev = &client->dev;
  1848. chip->usb_psy = usb_psy;
  1849. chip->fake_battery_soc = -EINVAL;
  1850. /* early for VADC get, defer probe if needed */
  1851. chip->vadc_dev = qpnp_get_vadc(chip->dev, "chg");
  1852. if (IS_ERR(chip->vadc_dev)) {
  1853. rc = PTR_ERR(chip->vadc_dev);
  1854. if (rc != -EPROBE_DEFER)
  1855. pr_err("vadc property missing\n");
  1856. return rc;
  1857. }
  1858. /* i2c pull up Regulator configuration */
  1859. chip->vcc_i2c = regulator_get(&client->dev, "vcc-i2c");
  1860. if (IS_ERR(chip->vcc_i2c)) {
  1861. dev_err(&client->dev,
  1862. "%s: Failed to get vcc_i2c regulator\n",
  1863. __func__);
  1864. rc = PTR_ERR(chip->vcc_i2c);
  1865. goto err_get_vtg_i2c;
  1866. }
  1867. if (regulator_count_voltages(chip->vcc_i2c) > 0) {
  1868. rc = regulator_set_voltage(chip->vcc_i2c,
  1869. SMB_I2C_VTG_MIN_UV, SMB_I2C_VTG_MAX_UV);
  1870. if (rc) {
  1871. dev_err(&client->dev,
  1872. "regulator vcc_i2c set failed, rc = %d\n",
  1873. rc);
  1874. goto err_set_vtg_i2c;
  1875. }
  1876. }
  1877. rc = regulator_enable(chip->vcc_i2c);
  1878. if (rc) {
  1879. dev_err(&client->dev,
  1880. "Regulator vcc_i2c enable failed "
  1881. "rc=%d\n", rc);
  1882. goto err_set_vtg_i2c;
  1883. }
  1884. mutex_init(&chip->irq_complete);
  1885. mutex_init(&chip->read_write_lock);
  1886. mutex_init(&chip->path_suspend_lock);
  1887. /* probe the device to check if its actually connected */
  1888. rc = smb358_read_reg(chip, CHG_OTH_CURRENT_CTRL_REG, &reg);
  1889. if (rc) {
  1890. pr_err("Failed to detect SMB358, device absent, rc = %d\n", rc);
  1891. goto err_set_vtg_i2c;
  1892. }
  1893. rc = smb_parse_dt(chip);
  1894. if (rc) {
  1895. dev_err(&client->dev, "Couldn't parse DT nodes rc=%d\n", rc);
  1896. goto err_set_vtg_i2c;
  1897. }
  1898. /* using adc_tm for implementing pmic therm */
  1899. if (chip->using_pmic_therm) {
  1900. chip->adc_tm_dev = qpnp_get_adc_tm(chip->dev, "chg");
  1901. if (IS_ERR(chip->adc_tm_dev)) {
  1902. rc = PTR_ERR(chip->adc_tm_dev);
  1903. if (rc != -EPROBE_DEFER)
  1904. pr_err("adc_tm property missing\n");
  1905. return rc;
  1906. }
  1907. }
  1908. i2c_set_clientdata(client, chip);
  1909. chip->batt_psy.name = "battery";
  1910. chip->batt_psy.type = POWER_SUPPLY_TYPE_BATTERY;
  1911. chip->batt_psy.get_property = smb358_battery_get_property;
  1912. chip->batt_psy.set_property = smb358_battery_set_property;
  1913. chip->batt_psy.property_is_writeable =
  1914. smb358_batt_property_is_writeable;
  1915. chip->batt_psy.properties = smb358_battery_properties;
  1916. chip->batt_psy.num_properties = ARRAY_SIZE(smb358_battery_properties);
  1917. chip->batt_psy.external_power_changed = smb358_external_power_changed;
  1918. chip->batt_psy.supplied_to = pm_batt_supplied_to;
  1919. chip->batt_psy.num_supplicants = ARRAY_SIZE(pm_batt_supplied_to);
  1920. chip->resume_completed = true;
  1921. rc = power_supply_register(chip->dev, &chip->batt_psy);
  1922. if (rc < 0) {
  1923. dev_err(&client->dev, "Couldn't register batt psy rc = %d\n",
  1924. rc);
  1925. goto err_set_vtg_i2c;
  1926. }
  1927. dump_regs(chip);
  1928. rc = smb358_regulator_init(chip);
  1929. if (rc) {
  1930. dev_err(&client->dev,
  1931. "Couldn't initialize smb358 ragulator rc=%d\n", rc);
  1932. goto err_set_vtg_i2c;
  1933. }
  1934. rc = smb358_hw_init(chip);
  1935. if (rc) {
  1936. dev_err(&client->dev,
  1937. "Couldn't intialize hardware rc=%d\n", rc);
  1938. goto fail_smb358_hw_init;
  1939. }
  1940. rc = determine_initial_state(chip);
  1941. if (rc) {
  1942. dev_err(&client->dev,
  1943. "Couldn't determine initial state rc=%d\n", rc);
  1944. goto fail_smb358_hw_init;
  1945. }
  1946. /* We will not use it by default */
  1947. if (gpio_is_valid(chip->chg_valid_gpio)) {
  1948. rc = gpio_request(chip->chg_valid_gpio, "smb358_chg_valid");
  1949. if (rc) {
  1950. dev_err(&client->dev,
  1951. "gpio_request for %d failed rc=%d\n",
  1952. chip->chg_valid_gpio, rc);
  1953. goto fail_chg_valid_irq;
  1954. }
  1955. irq = gpio_to_irq(chip->chg_valid_gpio);
  1956. if (irq < 0) {
  1957. dev_err(&client->dev,
  1958. "Invalid chg_valid irq = %d\n", irq);
  1959. goto fail_chg_valid_irq;
  1960. }
  1961. rc = devm_request_threaded_irq(&client->dev, irq,
  1962. NULL, smb358_chg_valid_handler,
  1963. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  1964. "smb358_chg_valid_irq", chip);
  1965. if (rc) {
  1966. dev_err(&client->dev,
  1967. "Failed request_irq irq=%d, gpio=%d rc=%d\n",
  1968. irq, chip->chg_valid_gpio, rc);
  1969. goto fail_chg_valid_irq;
  1970. }
  1971. smb358_chg_valid_handler(irq, chip);
  1972. enable_irq_wake(irq);
  1973. }
  1974. chip->irq_gpio = of_get_named_gpio_flags(chip->dev->of_node,
  1975. "qcom,irq-gpio", 0, NULL);
  1976. /* STAT irq configuration */
  1977. if (gpio_is_valid(chip->irq_gpio)) {
  1978. rc = gpio_request(chip->irq_gpio, "smb358_irq");
  1979. if (rc) {
  1980. dev_err(&client->dev,
  1981. "irq gpio request failed, rc=%d", rc);
  1982. goto fail_smb358_hw_init;
  1983. }
  1984. rc = gpio_direction_input(chip->irq_gpio);
  1985. if (rc) {
  1986. dev_err(&client->dev,
  1987. "set_direction for irq gpio failed\n");
  1988. goto fail_irq_gpio;
  1989. }
  1990. irq = gpio_to_irq(chip->irq_gpio);
  1991. if (irq < 0) {
  1992. dev_err(&client->dev,
  1993. "Invalid irq_gpio irq = %d\n", irq);
  1994. goto fail_irq_gpio;
  1995. }
  1996. rc = devm_request_threaded_irq(&client->dev, irq, NULL,
  1997. smb358_chg_stat_handler,
  1998. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1999. "smb358_chg_stat_irq", chip);
  2000. if (rc) {
  2001. dev_err(&client->dev,
  2002. "Failed STAT irq=%d request rc = %d\n",
  2003. irq, rc);
  2004. goto fail_irq_gpio;
  2005. }
  2006. enable_irq_wake(irq);
  2007. } else {
  2008. goto fail_irq_gpio;
  2009. }
  2010. if (chip->using_pmic_therm) {
  2011. /* add hot/cold temperature monitor */
  2012. chip->adc_param.low_temp = chip->cold_bat_decidegc;
  2013. chip->adc_param.high_temp = chip->hot_bat_decidegc;
  2014. chip->adc_param.timer_interval = ADC_MEAS2_INTERVAL_1S;
  2015. chip->adc_param.state_request = ADC_TM_HIGH_LOW_THR_ENABLE;
  2016. chip->adc_param.btm_ctx = chip;
  2017. chip->adc_param.threshold_notification =
  2018. smb_chg_adc_notification;
  2019. chip->adc_param.channel = LR_MUX1_BATT_THERM;
  2020. /* update battery missing info in tm_channel_measure*/
  2021. rc = qpnp_adc_tm_channel_measure(chip->adc_tm_dev,
  2022. &chip->adc_param);
  2023. if (rc)
  2024. pr_err("requesting ADC error %d\n", rc);
  2025. }
  2026. smb358_debugfs_init(chip);
  2027. dump_regs(chip);
  2028. dev_info(chip->dev, "SMB358 successfully probed. charger=%d, batt=%d\n",
  2029. chip->chg_present, smb358_get_prop_batt_present(chip));
  2030. return 0;
  2031. fail_chg_valid_irq:
  2032. if (gpio_is_valid(chip->chg_valid_gpio))
  2033. gpio_free(chip->chg_valid_gpio);
  2034. fail_irq_gpio:
  2035. if (gpio_is_valid(chip->irq_gpio))
  2036. gpio_free(chip->irq_gpio);
  2037. fail_smb358_hw_init:
  2038. power_supply_unregister(&chip->batt_psy);
  2039. regulator_unregister(chip->otg_vreg.rdev);
  2040. err_set_vtg_i2c:
  2041. if (regulator_count_voltages(chip->vcc_i2c) > 0)
  2042. regulator_set_voltage(chip->vcc_i2c, 0, SMB_I2C_VTG_MAX_UV);
  2043. err_get_vtg_i2c:
  2044. regulator_put(chip->vcc_i2c);
  2045. return rc;
  2046. }
  2047. static int smb358_charger_remove(struct i2c_client *client)
  2048. {
  2049. struct smb358_charger *chip = i2c_get_clientdata(client);
  2050. power_supply_unregister(&chip->batt_psy);
  2051. if (gpio_is_valid(chip->chg_valid_gpio))
  2052. gpio_free(chip->chg_valid_gpio);
  2053. regulator_disable(chip->vcc_i2c);
  2054. regulator_put(chip->vcc_i2c);
  2055. mutex_destroy(&chip->irq_complete);
  2056. debugfs_remove_recursive(chip->debug_root);
  2057. return 0;
  2058. }
  2059. static int smb358_suspend(struct device *dev)
  2060. {
  2061. struct i2c_client *client = to_i2c_client(dev);
  2062. struct smb358_charger *chip = i2c_get_clientdata(client);
  2063. int rc;
  2064. int i;
  2065. for (i = 0; i < 2; i++) {
  2066. rc = smb358_read_reg(chip, FAULT_INT_REG + i,
  2067. &chip->irq_cfg_mask[i]);
  2068. if (rc)
  2069. dev_err(chip->dev,
  2070. "Couldn't save irq cfg regs rc = %d\n", rc);
  2071. }
  2072. /* enable wake up IRQs */
  2073. rc = smb358_write_reg(chip, FAULT_INT_REG,
  2074. FAULT_INT_HOT_COLD_HARD_BIT | FAULT_INT_INPUT_UV_BIT);
  2075. if (rc < 0)
  2076. dev_err(chip->dev, "Couldn't set fault_irq_cfg rc = %d\n", rc);
  2077. rc = smb358_write_reg(chip, STATUS_INT_REG,
  2078. STATUS_INT_LOW_BATT_BIT | STATUS_INT_MISSING_BATT_BIT |
  2079. STATUS_INT_CHGING_BIT | STATUS_INT_INOK_BIT |
  2080. STATUS_INT_OTG_DETECT_BIT | STATUS_INT_CHG_INHI_BIT);
  2081. if (rc < 0)
  2082. dev_err(chip->dev,
  2083. "Couldn't set status_irq_cfg rc = %d\n", rc);
  2084. mutex_lock(&chip->irq_complete);
  2085. rc = regulator_disable(chip->vcc_i2c);
  2086. if (rc) {
  2087. dev_err(chip->dev,
  2088. "Regulator vcc_i2c disable failed rc=%d\n", rc);
  2089. mutex_unlock(&chip->irq_complete);
  2090. return rc;
  2091. }
  2092. chip->resume_completed = false;
  2093. mutex_unlock(&chip->irq_complete);
  2094. return 0;
  2095. }
  2096. static int smb358_suspend_noirq(struct device *dev)
  2097. {
  2098. struct i2c_client *client = to_i2c_client(dev);
  2099. struct smb358_charger *chip = i2c_get_clientdata(client);
  2100. if (chip->irq_waiting) {
  2101. pr_err_ratelimited("Aborting suspend, an interrupt was detected while suspending\n");
  2102. return -EBUSY;
  2103. }
  2104. return 0;
  2105. }
  2106. static int smb358_resume(struct device *dev)
  2107. {
  2108. struct i2c_client *client = to_i2c_client(dev);
  2109. struct smb358_charger *chip = i2c_get_clientdata(client);
  2110. int rc;
  2111. int i;
  2112. /* Restore IRQ config */
  2113. for (i = 0; i < 2; i++) {
  2114. rc = smb358_write_reg(chip, FAULT_INT_REG + i,
  2115. chip->irq_cfg_mask[i]);
  2116. if (rc)
  2117. dev_err(chip->dev,
  2118. "Couldn't restore irq cfg regs rc=%d\n", rc);
  2119. }
  2120. mutex_lock(&chip->irq_complete);
  2121. rc = regulator_enable(chip->vcc_i2c);
  2122. if (rc) {
  2123. dev_err(chip->dev,
  2124. "Regulator vcc_i2c enable failed rc=%d\n", rc);
  2125. mutex_unlock(&chip->irq_complete);
  2126. return rc;
  2127. }
  2128. chip->resume_completed = true;
  2129. mutex_unlock(&chip->irq_complete);
  2130. if (chip->irq_waiting) {
  2131. smb358_chg_stat_handler(client->irq, chip);
  2132. enable_irq(client->irq);
  2133. }
  2134. return 0;
  2135. }
  2136. static const struct dev_pm_ops smb358_pm_ops = {
  2137. .suspend = smb358_suspend,
  2138. .suspend_noirq = smb358_suspend_noirq,
  2139. .resume = smb358_resume,
  2140. };
  2141. static struct of_device_id smb358_match_table[] = {
  2142. { .compatible = "qcom,smb358-charger",},
  2143. { },
  2144. };
  2145. static const struct i2c_device_id smb358_charger_id[] = {
  2146. {"smb358-charger", 0},
  2147. {},
  2148. };
  2149. MODULE_DEVICE_TABLE(i2c, smb358_charger_id);
  2150. static struct i2c_driver smb358_charger_driver = {
  2151. .driver = {
  2152. .name = "smb358-charger",
  2153. .owner = THIS_MODULE,
  2154. .of_match_table = smb358_match_table,
  2155. .pm = &smb358_pm_ops,
  2156. },
  2157. .probe = smb358_charger_probe,
  2158. .remove = smb358_charger_remove,
  2159. .id_table = smb358_charger_id,
  2160. };
  2161. module_i2c_driver(smb358_charger_driver);
  2162. MODULE_DESCRIPTION("SMB358 Charger");
  2163. MODULE_LICENSE("GPL v2");
  2164. MODULE_ALIAS("i2c:smb358-charger");