ab8500_charger.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2012
  3. *
  4. * Charger driver for AB8500
  5. *
  6. * License Terms: GNU General Public License v2
  7. * Author:
  8. * Johan Palsson <johan.palsson@stericsson.com>
  9. * Karl Komierowski <karl.komierowski@stericsson.com>
  10. * Arun R Murthy <arun.murthy@stericsson.com>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/delay.h>
  17. #include <linux/notifier.h>
  18. #include <linux/slab.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/completion.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/err.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/kobject.h>
  26. #include <linux/of.h>
  27. #include <linux/mfd/core.h>
  28. #include <linux/mfd/abx500/ab8500.h>
  29. #include <linux/mfd/abx500.h>
  30. #include <linux/mfd/abx500/ab8500-bm.h>
  31. #include <linux/mfd/abx500/ab8500-gpadc.h>
  32. #include <linux/mfd/abx500/ux500_chargalg.h>
  33. #include <linux/usb/otg.h>
  34. #include <linux/mutex.h>
  35. /* Charger constants */
  36. #define NO_PW_CONN 0
  37. #define AC_PW_CONN 1
  38. #define USB_PW_CONN 2
  39. #define MAIN_WDOG_ENA 0x01
  40. #define MAIN_WDOG_KICK 0x02
  41. #define MAIN_WDOG_DIS 0x00
  42. #define CHARG_WD_KICK 0x01
  43. #define MAIN_CH_ENA 0x01
  44. #define MAIN_CH_NO_OVERSHOOT_ENA_N 0x02
  45. #define USB_CH_ENA 0x01
  46. #define USB_CHG_NO_OVERSHOOT_ENA_N 0x02
  47. #define MAIN_CH_DET 0x01
  48. #define MAIN_CH_CV_ON 0x04
  49. #define USB_CH_CV_ON 0x08
  50. #define VBUS_DET_DBNC100 0x02
  51. #define VBUS_DET_DBNC1 0x01
  52. #define OTP_ENABLE_WD 0x01
  53. #define DROP_COUNT_RESET 0x01
  54. #define USB_CH_DET 0x01
  55. #define MAIN_CH_INPUT_CURR_SHIFT 4
  56. #define VBUS_IN_CURR_LIM_SHIFT 4
  57. #define AB8540_VBUS_IN_CURR_LIM_SHIFT 2
  58. #define AUTO_VBUS_IN_CURR_LIM_SHIFT 4
  59. #define AB8540_AUTO_VBUS_IN_CURR_MASK 0x3F
  60. #define VBUS_IN_CURR_LIM_RETRY_SET_TIME 30 /* seconds */
  61. #define LED_INDICATOR_PWM_ENA 0x01
  62. #define LED_INDICATOR_PWM_DIS 0x00
  63. #define LED_IND_CUR_5MA 0x04
  64. #define LED_INDICATOR_PWM_DUTY_252_256 0xBF
  65. /* HW failure constants */
  66. #define MAIN_CH_TH_PROT 0x02
  67. #define VBUS_CH_NOK 0x08
  68. #define USB_CH_TH_PROT 0x02
  69. #define VBUS_OVV_TH 0x01
  70. #define MAIN_CH_NOK 0x01
  71. #define VBUS_DET 0x80
  72. #define MAIN_CH_STATUS2_MAINCHGDROP 0x80
  73. #define MAIN_CH_STATUS2_MAINCHARGERDETDBNC 0x40
  74. #define USB_CH_VBUSDROP 0x40
  75. #define USB_CH_VBUSDETDBNC 0x01
  76. /* UsbLineStatus register bit masks */
  77. #define AB8500_USB_LINK_STATUS 0x78
  78. #define AB8505_USB_LINK_STATUS 0xF8
  79. #define AB8500_STD_HOST_SUSP 0x18
  80. #define USB_LINK_STATUS_SHIFT 3
  81. /* Watchdog timeout constant */
  82. #define WD_TIMER 0x30 /* 4min */
  83. #define WD_KICK_INTERVAL (60 * HZ)
  84. /* Lowest charger voltage is 3.39V -> 0x4E */
  85. #define LOW_VOLT_REG 0x4E
  86. /* Step up/down delay in us */
  87. #define STEP_UDELAY 1000
  88. #define CHARGER_STATUS_POLL 10 /* in ms */
  89. #define CHG_WD_INTERVAL (60 * HZ)
  90. #define AB8500_SW_CONTROL_FALLBACK 0x03
  91. /* Wait for enumeration before charing in us */
  92. #define WAIT_ACA_RID_ENUMERATION (5 * 1000)
  93. /*External charger control*/
  94. #define AB8500_SYS_CHARGER_CONTROL_REG 0x52
  95. #define EXTERNAL_CHARGER_DISABLE_REG_VAL 0x03
  96. #define EXTERNAL_CHARGER_ENABLE_REG_VAL 0x07
  97. /* UsbLineStatus register - usb types */
  98. enum ab8500_charger_link_status {
  99. USB_STAT_NOT_CONFIGURED,
  100. USB_STAT_STD_HOST_NC,
  101. USB_STAT_STD_HOST_C_NS,
  102. USB_STAT_STD_HOST_C_S,
  103. USB_STAT_HOST_CHG_NM,
  104. USB_STAT_HOST_CHG_HS,
  105. USB_STAT_HOST_CHG_HS_CHIRP,
  106. USB_STAT_DEDICATED_CHG,
  107. USB_STAT_ACA_RID_A,
  108. USB_STAT_ACA_RID_B,
  109. USB_STAT_ACA_RID_C_NM,
  110. USB_STAT_ACA_RID_C_HS,
  111. USB_STAT_ACA_RID_C_HS_CHIRP,
  112. USB_STAT_HM_IDGND,
  113. USB_STAT_RESERVED,
  114. USB_STAT_NOT_VALID_LINK,
  115. USB_STAT_PHY_EN,
  116. USB_STAT_SUP_NO_IDGND_VBUS,
  117. USB_STAT_SUP_IDGND_VBUS,
  118. USB_STAT_CHARGER_LINE_1,
  119. USB_STAT_CARKIT_1,
  120. USB_STAT_CARKIT_2,
  121. USB_STAT_ACA_DOCK_CHARGER,
  122. };
  123. enum ab8500_usb_state {
  124. AB8500_BM_USB_STATE_RESET_HS, /* HighSpeed Reset */
  125. AB8500_BM_USB_STATE_RESET_FS, /* FullSpeed/LowSpeed Reset */
  126. AB8500_BM_USB_STATE_CONFIGURED,
  127. AB8500_BM_USB_STATE_SUSPEND,
  128. AB8500_BM_USB_STATE_RESUME,
  129. AB8500_BM_USB_STATE_MAX,
  130. };
  131. /* VBUS input current limits supported in AB8500 in mA */
  132. #define USB_CH_IP_CUR_LVL_0P05 50
  133. #define USB_CH_IP_CUR_LVL_0P09 98
  134. #define USB_CH_IP_CUR_LVL_0P19 193
  135. #define USB_CH_IP_CUR_LVL_0P29 290
  136. #define USB_CH_IP_CUR_LVL_0P38 380
  137. #define USB_CH_IP_CUR_LVL_0P45 450
  138. #define USB_CH_IP_CUR_LVL_0P5 500
  139. #define USB_CH_IP_CUR_LVL_0P6 600
  140. #define USB_CH_IP_CUR_LVL_0P7 700
  141. #define USB_CH_IP_CUR_LVL_0P8 800
  142. #define USB_CH_IP_CUR_LVL_0P9 900
  143. #define USB_CH_IP_CUR_LVL_1P0 1000
  144. #define USB_CH_IP_CUR_LVL_1P1 1100
  145. #define USB_CH_IP_CUR_LVL_1P3 1300
  146. #define USB_CH_IP_CUR_LVL_1P4 1400
  147. #define USB_CH_IP_CUR_LVL_1P5 1500
  148. #define VBAT_TRESH_IP_CUR_RED 3800
  149. #define to_ab8500_charger_usb_device_info(x) container_of((x), \
  150. struct ab8500_charger, usb_chg)
  151. #define to_ab8500_charger_ac_device_info(x) container_of((x), \
  152. struct ab8500_charger, ac_chg)
  153. /**
  154. * struct ab8500_charger_interrupts - ab8500 interupts
  155. * @name: name of the interrupt
  156. * @isr function pointer to the isr
  157. */
  158. struct ab8500_charger_interrupts {
  159. char *name;
  160. irqreturn_t (*isr)(int irq, void *data);
  161. };
  162. struct ab8500_charger_info {
  163. int charger_connected;
  164. int charger_online;
  165. int charger_voltage;
  166. int cv_active;
  167. bool wd_expired;
  168. int charger_current;
  169. };
  170. struct ab8500_charger_event_flags {
  171. bool mainextchnotok;
  172. bool main_thermal_prot;
  173. bool usb_thermal_prot;
  174. bool vbus_ovv;
  175. bool usbchargernotok;
  176. bool chgwdexp;
  177. bool vbus_collapse;
  178. bool vbus_drop_end;
  179. };
  180. struct ab8500_charger_usb_state {
  181. int usb_current;
  182. int usb_current_tmp;
  183. enum ab8500_usb_state state;
  184. enum ab8500_usb_state state_tmp;
  185. spinlock_t usb_lock;
  186. };
  187. struct ab8500_charger_max_usb_in_curr {
  188. int usb_type_max;
  189. int set_max;
  190. int calculated_max;
  191. };
  192. /**
  193. * struct ab8500_charger - ab8500 Charger device information
  194. * @dev: Pointer to the structure device
  195. * @vbus_detected: VBUS detected
  196. * @vbus_detected_start:
  197. * VBUS detected during startup
  198. * @ac_conn: This will be true when the AC charger has been plugged
  199. * @vddadc_en_ac: Indicate if VDD ADC supply is enabled because AC
  200. * charger is enabled
  201. * @vddadc_en_usb: Indicate if VDD ADC supply is enabled because USB
  202. * charger is enabled
  203. * @vbat Battery voltage
  204. * @old_vbat Previously measured battery voltage
  205. * @usb_device_is_unrecognised USB device is unrecognised by the hardware
  206. * @autopower Indicate if we should have automatic pwron after pwrloss
  207. * @autopower_cfg platform specific power config support for "pwron after pwrloss"
  208. * @invalid_charger_detect_state State when forcing AB to use invalid charger
  209. * @is_aca_rid: Incicate if accessory is ACA type
  210. * @current_stepping_sessions:
  211. * Counter for current stepping sessions
  212. * @parent: Pointer to the struct ab8500
  213. * @gpadc: Pointer to the struct gpadc
  214. * @bm: Platform specific battery management information
  215. * @flags: Structure for information about events triggered
  216. * @usb_state: Structure for usb stack information
  217. * @max_usb_in_curr: Max USB charger input current
  218. * @ac_chg: AC charger power supply
  219. * @usb_chg: USB charger power supply
  220. * @ac: Structure that holds the AC charger properties
  221. * @usb: Structure that holds the USB charger properties
  222. * @regu: Pointer to the struct regulator
  223. * @charger_wq: Work queue for the IRQs and checking HW state
  224. * @usb_ipt_crnt_lock: Lock to protect VBUS input current setting from mutuals
  225. * @pm_lock: Lock to prevent system to suspend
  226. * @check_vbat_work Work for checking vbat threshold to adjust vbus current
  227. * @check_hw_failure_work: Work for checking HW state
  228. * @check_usbchgnotok_work: Work for checking USB charger not ok status
  229. * @kick_wd_work: Work for kicking the charger watchdog in case
  230. * of ABB rev 1.* due to the watchog logic bug
  231. * @ac_charger_attached_work: Work for checking if AC charger is still
  232. * connected
  233. * @usb_charger_attached_work: Work for checking if USB charger is still
  234. * connected
  235. * @ac_work: Work for checking AC charger connection
  236. * @detect_usb_type_work: Work for detecting the USB type connected
  237. * @usb_link_status_work: Work for checking the new USB link status
  238. * @usb_state_changed_work: Work for checking USB state
  239. * @attach_work: Work for detecting USB type
  240. * @vbus_drop_end_work: Work for detecting VBUS drop end
  241. * @check_main_thermal_prot_work:
  242. * Work for checking Main thermal status
  243. * @check_usb_thermal_prot_work:
  244. * Work for checking USB thermal status
  245. * @charger_attached_mutex: For controlling the wakelock
  246. */
  247. struct ab8500_charger {
  248. struct device *dev;
  249. bool vbus_detected;
  250. bool vbus_detected_start;
  251. bool ac_conn;
  252. bool vddadc_en_ac;
  253. bool vddadc_en_usb;
  254. int vbat;
  255. int old_vbat;
  256. bool usb_device_is_unrecognised;
  257. bool autopower;
  258. bool autopower_cfg;
  259. int invalid_charger_detect_state;
  260. int is_aca_rid;
  261. atomic_t current_stepping_sessions;
  262. struct ab8500 *parent;
  263. struct ab8500_gpadc *gpadc;
  264. struct abx500_bm_data *bm;
  265. struct ab8500_charger_event_flags flags;
  266. struct ab8500_charger_usb_state usb_state;
  267. struct ab8500_charger_max_usb_in_curr max_usb_in_curr;
  268. struct ux500_charger ac_chg;
  269. struct ux500_charger usb_chg;
  270. struct ab8500_charger_info ac;
  271. struct ab8500_charger_info usb;
  272. struct regulator *regu;
  273. struct workqueue_struct *charger_wq;
  274. struct mutex usb_ipt_crnt_lock;
  275. struct delayed_work check_vbat_work;
  276. struct delayed_work check_hw_failure_work;
  277. struct delayed_work check_usbchgnotok_work;
  278. struct delayed_work kick_wd_work;
  279. struct delayed_work usb_state_changed_work;
  280. struct delayed_work attach_work;
  281. struct delayed_work ac_charger_attached_work;
  282. struct delayed_work usb_charger_attached_work;
  283. struct delayed_work vbus_drop_end_work;
  284. struct work_struct ac_work;
  285. struct work_struct detect_usb_type_work;
  286. struct work_struct usb_link_status_work;
  287. struct work_struct check_main_thermal_prot_work;
  288. struct work_struct check_usb_thermal_prot_work;
  289. struct usb_phy *usb_phy;
  290. struct notifier_block nb;
  291. struct mutex charger_attached_mutex;
  292. };
  293. /* AC properties */
  294. static enum power_supply_property ab8500_charger_ac_props[] = {
  295. POWER_SUPPLY_PROP_HEALTH,
  296. POWER_SUPPLY_PROP_PRESENT,
  297. POWER_SUPPLY_PROP_ONLINE,
  298. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  299. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  300. POWER_SUPPLY_PROP_CURRENT_NOW,
  301. };
  302. /* USB properties */
  303. static enum power_supply_property ab8500_charger_usb_props[] = {
  304. POWER_SUPPLY_PROP_HEALTH,
  305. POWER_SUPPLY_PROP_CURRENT_AVG,
  306. POWER_SUPPLY_PROP_PRESENT,
  307. POWER_SUPPLY_PROP_ONLINE,
  308. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  309. POWER_SUPPLY_PROP_VOLTAGE_AVG,
  310. POWER_SUPPLY_PROP_CURRENT_NOW,
  311. };
  312. /*
  313. * Function for enabling and disabling sw fallback mode
  314. * should always be disabled when no charger is connected.
  315. */
  316. static void ab8500_enable_disable_sw_fallback(struct ab8500_charger *di,
  317. bool fallback)
  318. {
  319. u8 val;
  320. u8 reg;
  321. u8 bank;
  322. u8 bit;
  323. int ret;
  324. dev_dbg(di->dev, "SW Fallback: %d\n", fallback);
  325. if (is_ab8500(di->parent)) {
  326. bank = 0x15;
  327. reg = 0x0;
  328. bit = 3;
  329. } else {
  330. bank = AB8500_SYS_CTRL1_BLOCK;
  331. reg = AB8500_SW_CONTROL_FALLBACK;
  332. bit = 0;
  333. }
  334. /* read the register containing fallback bit */
  335. ret = abx500_get_register_interruptible(di->dev, bank, reg, &val);
  336. if (ret < 0) {
  337. dev_err(di->dev, "%d read failed\n", __LINE__);
  338. return;
  339. }
  340. if (is_ab8500(di->parent)) {
  341. /* enable the OPT emulation registers */
  342. ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x2);
  343. if (ret) {
  344. dev_err(di->dev, "%d write failed\n", __LINE__);
  345. goto disable_otp;
  346. }
  347. }
  348. if (fallback)
  349. val |= (1 << bit);
  350. else
  351. val &= ~(1 << bit);
  352. /* write back the changed fallback bit value to register */
  353. ret = abx500_set_register_interruptible(di->dev, bank, reg, val);
  354. if (ret) {
  355. dev_err(di->dev, "%d write failed\n", __LINE__);
  356. }
  357. disable_otp:
  358. if (is_ab8500(di->parent)) {
  359. /* disable the set OTP registers again */
  360. ret = abx500_set_register_interruptible(di->dev, 0x11, 0x00, 0x0);
  361. if (ret) {
  362. dev_err(di->dev, "%d write failed\n", __LINE__);
  363. }
  364. }
  365. }
  366. /**
  367. * ab8500_power_supply_changed - a wrapper with local extentions for
  368. * power_supply_changed
  369. * @di: pointer to the ab8500_charger structure
  370. * @psy: pointer to power_supply_that have changed.
  371. *
  372. */
  373. static void ab8500_power_supply_changed(struct ab8500_charger *di,
  374. struct power_supply *psy)
  375. {
  376. /*
  377. * This happens if we get notifications or interrupts and
  378. * the platform has been configured not to support one or
  379. * other type of charging.
  380. */
  381. if (!psy)
  382. return;
  383. if (di->autopower_cfg) {
  384. if (!di->usb.charger_connected &&
  385. !di->ac.charger_connected &&
  386. di->autopower) {
  387. di->autopower = false;
  388. ab8500_enable_disable_sw_fallback(di, false);
  389. } else if (!di->autopower &&
  390. (di->ac.charger_connected ||
  391. di->usb.charger_connected)) {
  392. di->autopower = true;
  393. ab8500_enable_disable_sw_fallback(di, true);
  394. }
  395. }
  396. power_supply_changed(psy);
  397. }
  398. static void ab8500_charger_set_usb_connected(struct ab8500_charger *di,
  399. bool connected)
  400. {
  401. if (connected != di->usb.charger_connected) {
  402. dev_dbg(di->dev, "USB connected:%i\n", connected);
  403. di->usb.charger_connected = connected;
  404. if (!connected)
  405. di->flags.vbus_drop_end = false;
  406. /*
  407. * Sometimes the platform is configured not to support
  408. * USB charging and no psy has been created, but we still
  409. * will get these notifications.
  410. */
  411. if (di->usb_chg.psy) {
  412. sysfs_notify(&di->usb_chg.psy->dev.kobj, NULL,
  413. "present");
  414. }
  415. if (connected) {
  416. mutex_lock(&di->charger_attached_mutex);
  417. mutex_unlock(&di->charger_attached_mutex);
  418. if (is_ab8500(di->parent))
  419. queue_delayed_work(di->charger_wq,
  420. &di->usb_charger_attached_work,
  421. HZ);
  422. } else {
  423. cancel_delayed_work_sync(&di->usb_charger_attached_work);
  424. mutex_lock(&di->charger_attached_mutex);
  425. mutex_unlock(&di->charger_attached_mutex);
  426. }
  427. }
  428. }
  429. /**
  430. * ab8500_charger_get_ac_voltage() - get ac charger voltage
  431. * @di: pointer to the ab8500_charger structure
  432. *
  433. * Returns ac charger voltage (on success)
  434. */
  435. static int ab8500_charger_get_ac_voltage(struct ab8500_charger *di)
  436. {
  437. int vch;
  438. /* Only measure voltage if the charger is connected */
  439. if (di->ac.charger_connected) {
  440. vch = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_V);
  441. if (vch < 0)
  442. dev_err(di->dev, "%s gpadc conv failed,\n", __func__);
  443. } else {
  444. vch = 0;
  445. }
  446. return vch;
  447. }
  448. /**
  449. * ab8500_charger_ac_cv() - check if the main charger is in CV mode
  450. * @di: pointer to the ab8500_charger structure
  451. *
  452. * Returns ac charger CV mode (on success) else error code
  453. */
  454. static int ab8500_charger_ac_cv(struct ab8500_charger *di)
  455. {
  456. u8 val;
  457. int ret = 0;
  458. /* Only check CV mode if the charger is online */
  459. if (di->ac.charger_online) {
  460. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  461. AB8500_CH_STATUS1_REG, &val);
  462. if (ret < 0) {
  463. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  464. return 0;
  465. }
  466. if (val & MAIN_CH_CV_ON)
  467. ret = 1;
  468. else
  469. ret = 0;
  470. }
  471. return ret;
  472. }
  473. /**
  474. * ab8500_charger_get_vbus_voltage() - get vbus voltage
  475. * @di: pointer to the ab8500_charger structure
  476. *
  477. * This function returns the vbus voltage.
  478. * Returns vbus voltage (on success)
  479. */
  480. static int ab8500_charger_get_vbus_voltage(struct ab8500_charger *di)
  481. {
  482. int vch;
  483. /* Only measure voltage if the charger is connected */
  484. if (di->usb.charger_connected) {
  485. vch = ab8500_gpadc_convert(di->gpadc, VBUS_V);
  486. if (vch < 0)
  487. dev_err(di->dev, "%s gpadc conv failed\n", __func__);
  488. } else {
  489. vch = 0;
  490. }
  491. return vch;
  492. }
  493. /**
  494. * ab8500_charger_get_usb_current() - get usb charger current
  495. * @di: pointer to the ab8500_charger structure
  496. *
  497. * This function returns the usb charger current.
  498. * Returns usb current (on success) and error code on failure
  499. */
  500. static int ab8500_charger_get_usb_current(struct ab8500_charger *di)
  501. {
  502. int ich;
  503. /* Only measure current if the charger is online */
  504. if (di->usb.charger_online) {
  505. ich = ab8500_gpadc_convert(di->gpadc, USB_CHARGER_C);
  506. if (ich < 0)
  507. dev_err(di->dev, "%s gpadc conv failed\n", __func__);
  508. } else {
  509. ich = 0;
  510. }
  511. return ich;
  512. }
  513. /**
  514. * ab8500_charger_get_ac_current() - get ac charger current
  515. * @di: pointer to the ab8500_charger structure
  516. *
  517. * This function returns the ac charger current.
  518. * Returns ac current (on success) and error code on failure.
  519. */
  520. static int ab8500_charger_get_ac_current(struct ab8500_charger *di)
  521. {
  522. int ich;
  523. /* Only measure current if the charger is online */
  524. if (di->ac.charger_online) {
  525. ich = ab8500_gpadc_convert(di->gpadc, MAIN_CHARGER_C);
  526. if (ich < 0)
  527. dev_err(di->dev, "%s gpadc conv failed\n", __func__);
  528. } else {
  529. ich = 0;
  530. }
  531. return ich;
  532. }
  533. /**
  534. * ab8500_charger_usb_cv() - check if the usb charger is in CV mode
  535. * @di: pointer to the ab8500_charger structure
  536. *
  537. * Returns ac charger CV mode (on success) else error code
  538. */
  539. static int ab8500_charger_usb_cv(struct ab8500_charger *di)
  540. {
  541. int ret;
  542. u8 val;
  543. /* Only check CV mode if the charger is online */
  544. if (di->usb.charger_online) {
  545. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  546. AB8500_CH_USBCH_STAT1_REG, &val);
  547. if (ret < 0) {
  548. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  549. return 0;
  550. }
  551. if (val & USB_CH_CV_ON)
  552. ret = 1;
  553. else
  554. ret = 0;
  555. } else {
  556. ret = 0;
  557. }
  558. return ret;
  559. }
  560. /**
  561. * ab8500_charger_detect_chargers() - Detect the connected chargers
  562. * @di: pointer to the ab8500_charger structure
  563. * @probe: if probe, don't delay and wait for HW
  564. *
  565. * Returns the type of charger connected.
  566. * For USB it will not mean we can actually charge from it
  567. * but that there is a USB cable connected that we have to
  568. * identify. This is used during startup when we don't get
  569. * interrupts of the charger detection
  570. *
  571. * Returns an integer value, that means,
  572. * NO_PW_CONN no power supply is connected
  573. * AC_PW_CONN if the AC power supply is connected
  574. * USB_PW_CONN if the USB power supply is connected
  575. * AC_PW_CONN + USB_PW_CONN if USB and AC power supplies are both connected
  576. */
  577. static int ab8500_charger_detect_chargers(struct ab8500_charger *di, bool probe)
  578. {
  579. int result = NO_PW_CONN;
  580. int ret;
  581. u8 val;
  582. /* Check for AC charger */
  583. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  584. AB8500_CH_STATUS1_REG, &val);
  585. if (ret < 0) {
  586. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  587. return ret;
  588. }
  589. if (val & MAIN_CH_DET)
  590. result = AC_PW_CONN;
  591. /* Check for USB charger */
  592. if (!probe) {
  593. /*
  594. * AB8500 says VBUS_DET_DBNC1 & VBUS_DET_DBNC100
  595. * when disconnecting ACA even though no
  596. * charger was connected. Try waiting a little
  597. * longer than the 100 ms of VBUS_DET_DBNC100...
  598. */
  599. msleep(110);
  600. }
  601. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  602. AB8500_CH_USBCH_STAT1_REG, &val);
  603. if (ret < 0) {
  604. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  605. return ret;
  606. }
  607. dev_dbg(di->dev,
  608. "%s AB8500_CH_USBCH_STAT1_REG %x\n", __func__,
  609. val);
  610. if ((val & VBUS_DET_DBNC1) && (val & VBUS_DET_DBNC100))
  611. result |= USB_PW_CONN;
  612. return result;
  613. }
  614. /**
  615. * ab8500_charger_max_usb_curr() - get the max curr for the USB type
  616. * @di: pointer to the ab8500_charger structure
  617. * @link_status: the identified USB type
  618. *
  619. * Get the maximum current that is allowed to be drawn from the host
  620. * based on the USB type.
  621. * Returns error code in case of failure else 0 on success
  622. */
  623. static int ab8500_charger_max_usb_curr(struct ab8500_charger *di,
  624. enum ab8500_charger_link_status link_status)
  625. {
  626. int ret = 0;
  627. di->usb_device_is_unrecognised = false;
  628. /*
  629. * Platform only supports USB 2.0.
  630. * This means that charging current from USB source
  631. * is maximum 500 mA. Every occurence of USB_STAT_*_HOST_*
  632. * should set USB_CH_IP_CUR_LVL_0P5.
  633. */
  634. switch (link_status) {
  635. case USB_STAT_STD_HOST_NC:
  636. case USB_STAT_STD_HOST_C_NS:
  637. case USB_STAT_STD_HOST_C_S:
  638. dev_dbg(di->dev, "USB Type - Standard host is "
  639. "detected through USB driver\n");
  640. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  641. di->is_aca_rid = 0;
  642. break;
  643. case USB_STAT_HOST_CHG_HS_CHIRP:
  644. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  645. di->is_aca_rid = 0;
  646. break;
  647. case USB_STAT_HOST_CHG_HS:
  648. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  649. di->is_aca_rid = 0;
  650. break;
  651. case USB_STAT_ACA_RID_C_HS:
  652. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P9;
  653. di->is_aca_rid = 0;
  654. break;
  655. case USB_STAT_ACA_RID_A:
  656. /*
  657. * Dedicated charger level minus maximum current accessory
  658. * can consume (900mA). Closest level is 500mA
  659. */
  660. dev_dbg(di->dev, "USB_STAT_ACA_RID_A detected\n");
  661. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  662. di->is_aca_rid = 1;
  663. break;
  664. case USB_STAT_ACA_RID_B:
  665. /*
  666. * Dedicated charger level minus 120mA (20mA for ACA and
  667. * 100mA for potential accessory). Closest level is 1300mA
  668. */
  669. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P3;
  670. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
  671. di->max_usb_in_curr.usb_type_max);
  672. di->is_aca_rid = 1;
  673. break;
  674. case USB_STAT_HOST_CHG_NM:
  675. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  676. di->is_aca_rid = 0;
  677. break;
  678. case USB_STAT_DEDICATED_CHG:
  679. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
  680. di->is_aca_rid = 0;
  681. break;
  682. case USB_STAT_ACA_RID_C_HS_CHIRP:
  683. case USB_STAT_ACA_RID_C_NM:
  684. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_1P5;
  685. di->is_aca_rid = 1;
  686. break;
  687. case USB_STAT_NOT_CONFIGURED:
  688. if (di->vbus_detected) {
  689. di->usb_device_is_unrecognised = true;
  690. dev_dbg(di->dev, "USB Type - Legacy charger.\n");
  691. di->max_usb_in_curr.usb_type_max =
  692. USB_CH_IP_CUR_LVL_1P5;
  693. break;
  694. }
  695. case USB_STAT_HM_IDGND:
  696. dev_err(di->dev, "USB Type - Charging not allowed\n");
  697. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
  698. ret = -ENXIO;
  699. break;
  700. case USB_STAT_RESERVED:
  701. if (is_ab8500(di->parent)) {
  702. di->flags.vbus_collapse = true;
  703. dev_err(di->dev, "USB Type - USB_STAT_RESERVED "
  704. "VBUS has collapsed\n");
  705. ret = -ENXIO;
  706. break;
  707. } else {
  708. dev_dbg(di->dev, "USB Type - Charging not allowed\n");
  709. di->max_usb_in_curr.usb_type_max =
  710. USB_CH_IP_CUR_LVL_0P05;
  711. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
  712. link_status,
  713. di->max_usb_in_curr.usb_type_max);
  714. ret = -ENXIO;
  715. break;
  716. }
  717. case USB_STAT_CARKIT_1:
  718. case USB_STAT_CARKIT_2:
  719. case USB_STAT_ACA_DOCK_CHARGER:
  720. case USB_STAT_CHARGER_LINE_1:
  721. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  722. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d", link_status,
  723. di->max_usb_in_curr.usb_type_max);
  724. break;
  725. case USB_STAT_NOT_VALID_LINK:
  726. dev_err(di->dev, "USB Type invalid - try charging anyway\n");
  727. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  728. break;
  729. default:
  730. dev_err(di->dev, "USB Type - Unknown\n");
  731. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
  732. ret = -ENXIO;
  733. break;
  734. };
  735. di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
  736. dev_dbg(di->dev, "USB Type - 0x%02x MaxCurr: %d",
  737. link_status, di->max_usb_in_curr.set_max);
  738. return ret;
  739. }
  740. /**
  741. * ab8500_charger_read_usb_type() - read the type of usb connected
  742. * @di: pointer to the ab8500_charger structure
  743. *
  744. * Detect the type of the plugged USB
  745. * Returns error code in case of failure else 0 on success
  746. */
  747. static int ab8500_charger_read_usb_type(struct ab8500_charger *di)
  748. {
  749. int ret;
  750. u8 val;
  751. ret = abx500_get_register_interruptible(di->dev,
  752. AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG, &val);
  753. if (ret < 0) {
  754. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  755. return ret;
  756. }
  757. if (is_ab8500(di->parent))
  758. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  759. AB8500_USB_LINE_STAT_REG, &val);
  760. else
  761. ret = abx500_get_register_interruptible(di->dev,
  762. AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
  763. if (ret < 0) {
  764. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  765. return ret;
  766. }
  767. /* get the USB type */
  768. if (is_ab8500(di->parent))
  769. val = (val & AB8500_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
  770. else
  771. val = (val & AB8505_USB_LINK_STATUS) >> USB_LINK_STATUS_SHIFT;
  772. ret = ab8500_charger_max_usb_curr(di,
  773. (enum ab8500_charger_link_status) val);
  774. return ret;
  775. }
  776. /**
  777. * ab8500_charger_detect_usb_type() - get the type of usb connected
  778. * @di: pointer to the ab8500_charger structure
  779. *
  780. * Detect the type of the plugged USB
  781. * Returns error code in case of failure else 0 on success
  782. */
  783. static int ab8500_charger_detect_usb_type(struct ab8500_charger *di)
  784. {
  785. int i, ret;
  786. u8 val;
  787. /*
  788. * On getting the VBUS rising edge detect interrupt there
  789. * is a 250ms delay after which the register UsbLineStatus
  790. * is filled with valid data.
  791. */
  792. for (i = 0; i < 10; i++) {
  793. msleep(250);
  794. ret = abx500_get_register_interruptible(di->dev,
  795. AB8500_INTERRUPT, AB8500_IT_SOURCE21_REG,
  796. &val);
  797. dev_dbg(di->dev, "%s AB8500_IT_SOURCE21_REG %x\n",
  798. __func__, val);
  799. if (ret < 0) {
  800. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  801. return ret;
  802. }
  803. if (is_ab8500(di->parent))
  804. ret = abx500_get_register_interruptible(di->dev,
  805. AB8500_USB, AB8500_USB_LINE_STAT_REG, &val);
  806. else
  807. ret = abx500_get_register_interruptible(di->dev,
  808. AB8500_USB, AB8500_USB_LINK1_STAT_REG, &val);
  809. if (ret < 0) {
  810. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  811. return ret;
  812. }
  813. dev_dbg(di->dev, "%s AB8500_USB_LINE_STAT_REG %x\n", __func__,
  814. val);
  815. /*
  816. * Until the IT source register is read the UsbLineStatus
  817. * register is not updated, hence doing the same
  818. * Revisit this:
  819. */
  820. /* get the USB type */
  821. if (is_ab8500(di->parent))
  822. val = (val & AB8500_USB_LINK_STATUS) >>
  823. USB_LINK_STATUS_SHIFT;
  824. else
  825. val = (val & AB8505_USB_LINK_STATUS) >>
  826. USB_LINK_STATUS_SHIFT;
  827. if (val)
  828. break;
  829. }
  830. ret = ab8500_charger_max_usb_curr(di,
  831. (enum ab8500_charger_link_status) val);
  832. return ret;
  833. }
  834. /*
  835. * This array maps the raw hex value to charger voltage used by the AB8500
  836. * Values taken from the UM0836
  837. */
  838. static int ab8500_charger_voltage_map[] = {
  839. 3500 ,
  840. 3525 ,
  841. 3550 ,
  842. 3575 ,
  843. 3600 ,
  844. 3625 ,
  845. 3650 ,
  846. 3675 ,
  847. 3700 ,
  848. 3725 ,
  849. 3750 ,
  850. 3775 ,
  851. 3800 ,
  852. 3825 ,
  853. 3850 ,
  854. 3875 ,
  855. 3900 ,
  856. 3925 ,
  857. 3950 ,
  858. 3975 ,
  859. 4000 ,
  860. 4025 ,
  861. 4050 ,
  862. 4060 ,
  863. 4070 ,
  864. 4080 ,
  865. 4090 ,
  866. 4100 ,
  867. 4110 ,
  868. 4120 ,
  869. 4130 ,
  870. 4140 ,
  871. 4150 ,
  872. 4160 ,
  873. 4170 ,
  874. 4180 ,
  875. 4190 ,
  876. 4200 ,
  877. 4210 ,
  878. 4220 ,
  879. 4230 ,
  880. 4240 ,
  881. 4250 ,
  882. 4260 ,
  883. 4270 ,
  884. 4280 ,
  885. 4290 ,
  886. 4300 ,
  887. 4310 ,
  888. 4320 ,
  889. 4330 ,
  890. 4340 ,
  891. 4350 ,
  892. 4360 ,
  893. 4370 ,
  894. 4380 ,
  895. 4390 ,
  896. 4400 ,
  897. 4410 ,
  898. 4420 ,
  899. 4430 ,
  900. 4440 ,
  901. 4450 ,
  902. 4460 ,
  903. 4470 ,
  904. 4480 ,
  905. 4490 ,
  906. 4500 ,
  907. 4510 ,
  908. 4520 ,
  909. 4530 ,
  910. 4540 ,
  911. 4550 ,
  912. 4560 ,
  913. 4570 ,
  914. 4580 ,
  915. 4590 ,
  916. 4600 ,
  917. };
  918. static int ab8500_voltage_to_regval(int voltage)
  919. {
  920. int i;
  921. /* Special case for voltage below 3.5V */
  922. if (voltage < ab8500_charger_voltage_map[0])
  923. return LOW_VOLT_REG;
  924. for (i = 1; i < ARRAY_SIZE(ab8500_charger_voltage_map); i++) {
  925. if (voltage < ab8500_charger_voltage_map[i])
  926. return i - 1;
  927. }
  928. /* If not last element, return error */
  929. i = ARRAY_SIZE(ab8500_charger_voltage_map) - 1;
  930. if (voltage == ab8500_charger_voltage_map[i])
  931. return i;
  932. else
  933. return -1;
  934. }
  935. static int ab8500_current_to_regval(struct ab8500_charger *di, int curr)
  936. {
  937. int i;
  938. if (curr < di->bm->chg_output_curr[0])
  939. return 0;
  940. for (i = 0; i < di->bm->n_chg_out_curr; i++) {
  941. if (curr < di->bm->chg_output_curr[i])
  942. return i - 1;
  943. }
  944. /* If not last element, return error */
  945. i = di->bm->n_chg_out_curr - 1;
  946. if (curr == di->bm->chg_output_curr[i])
  947. return i;
  948. else
  949. return -1;
  950. }
  951. static int ab8500_vbus_in_curr_to_regval(struct ab8500_charger *di, int curr)
  952. {
  953. int i;
  954. if (curr < di->bm->chg_input_curr[0])
  955. return 0;
  956. for (i = 0; i < di->bm->n_chg_in_curr; i++) {
  957. if (curr < di->bm->chg_input_curr[i])
  958. return i - 1;
  959. }
  960. /* If not last element, return error */
  961. i = di->bm->n_chg_in_curr - 1;
  962. if (curr == di->bm->chg_input_curr[i])
  963. return i;
  964. else
  965. return -1;
  966. }
  967. /**
  968. * ab8500_charger_get_usb_cur() - get usb current
  969. * @di: pointer to the ab8500_charger structre
  970. *
  971. * The usb stack provides the maximum current that can be drawn from
  972. * the standard usb host. This will be in mA.
  973. * This function converts current in mA to a value that can be written
  974. * to the register. Returns -1 if charging is not allowed
  975. */
  976. static int ab8500_charger_get_usb_cur(struct ab8500_charger *di)
  977. {
  978. int ret = 0;
  979. switch (di->usb_state.usb_current) {
  980. case 100:
  981. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P09;
  982. break;
  983. case 200:
  984. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P19;
  985. break;
  986. case 300:
  987. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P29;
  988. break;
  989. case 400:
  990. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P38;
  991. break;
  992. case 500:
  993. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P5;
  994. break;
  995. default:
  996. di->max_usb_in_curr.usb_type_max = USB_CH_IP_CUR_LVL_0P05;
  997. ret = -EPERM;
  998. break;
  999. };
  1000. di->max_usb_in_curr.set_max = di->max_usb_in_curr.usb_type_max;
  1001. return ret;
  1002. }
  1003. /**
  1004. * ab8500_charger_check_continue_stepping() - Check to allow stepping
  1005. * @di: pointer to the ab8500_charger structure
  1006. * @reg: select what charger register to check
  1007. *
  1008. * Check if current stepping should be allowed to continue.
  1009. * Checks if charger source has not collapsed. If it has, further stepping
  1010. * is not allowed.
  1011. */
  1012. static bool ab8500_charger_check_continue_stepping(struct ab8500_charger *di,
  1013. int reg)
  1014. {
  1015. if (reg == AB8500_USBCH_IPT_CRNTLVL_REG)
  1016. return !di->flags.vbus_drop_end;
  1017. else
  1018. return true;
  1019. }
  1020. /**
  1021. * ab8500_charger_set_current() - set charger current
  1022. * @di: pointer to the ab8500_charger structure
  1023. * @ich: charger current, in mA
  1024. * @reg: select what charger register to set
  1025. *
  1026. * Set charger current.
  1027. * There is no state machine in the AB to step up/down the charger
  1028. * current to avoid dips and spikes on MAIN, VBUS and VBAT when
  1029. * charging is started. Instead we need to implement
  1030. * this charger current step-up/down here.
  1031. * Returns error code in case of failure else 0(on success)
  1032. */
  1033. static int ab8500_charger_set_current(struct ab8500_charger *di,
  1034. int ich, int reg)
  1035. {
  1036. int ret = 0;
  1037. int curr_index, prev_curr_index, shift_value, i;
  1038. u8 reg_value;
  1039. u32 step_udelay;
  1040. bool no_stepping = false;
  1041. atomic_inc(&di->current_stepping_sessions);
  1042. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  1043. reg, &reg_value);
  1044. if (ret < 0) {
  1045. dev_err(di->dev, "%s read failed\n", __func__);
  1046. goto exit_set_current;
  1047. }
  1048. switch (reg) {
  1049. case AB8500_MCH_IPT_CURLVL_REG:
  1050. shift_value = MAIN_CH_INPUT_CURR_SHIFT;
  1051. prev_curr_index = (reg_value >> shift_value);
  1052. curr_index = ab8500_current_to_regval(di, ich);
  1053. step_udelay = STEP_UDELAY;
  1054. if (!di->ac.charger_connected)
  1055. no_stepping = true;
  1056. break;
  1057. case AB8500_USBCH_IPT_CRNTLVL_REG:
  1058. if (is_ab8540(di->parent))
  1059. shift_value = AB8540_VBUS_IN_CURR_LIM_SHIFT;
  1060. else
  1061. shift_value = VBUS_IN_CURR_LIM_SHIFT;
  1062. prev_curr_index = (reg_value >> shift_value);
  1063. curr_index = ab8500_vbus_in_curr_to_regval(di, ich);
  1064. step_udelay = STEP_UDELAY * 100;
  1065. if (!di->usb.charger_connected)
  1066. no_stepping = true;
  1067. break;
  1068. case AB8500_CH_OPT_CRNTLVL_REG:
  1069. shift_value = 0;
  1070. prev_curr_index = (reg_value >> shift_value);
  1071. curr_index = ab8500_current_to_regval(di, ich);
  1072. step_udelay = STEP_UDELAY;
  1073. if (curr_index && (curr_index - prev_curr_index) > 1)
  1074. step_udelay *= 100;
  1075. if (!di->usb.charger_connected && !di->ac.charger_connected)
  1076. no_stepping = true;
  1077. break;
  1078. default:
  1079. dev_err(di->dev, "%s current register not valid\n", __func__);
  1080. ret = -ENXIO;
  1081. goto exit_set_current;
  1082. }
  1083. if (curr_index < 0) {
  1084. dev_err(di->dev, "requested current limit out-of-range\n");
  1085. ret = -ENXIO;
  1086. goto exit_set_current;
  1087. }
  1088. /* only update current if it's been changed */
  1089. if (prev_curr_index == curr_index) {
  1090. dev_dbg(di->dev, "%s current not changed for reg: 0x%02x\n",
  1091. __func__, reg);
  1092. ret = 0;
  1093. goto exit_set_current;
  1094. }
  1095. dev_dbg(di->dev, "%s set charger current: %d mA for reg: 0x%02x\n",
  1096. __func__, ich, reg);
  1097. if (no_stepping) {
  1098. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1099. reg, (u8)curr_index << shift_value);
  1100. if (ret)
  1101. dev_err(di->dev, "%s write failed\n", __func__);
  1102. } else if (prev_curr_index > curr_index) {
  1103. for (i = prev_curr_index - 1; i >= curr_index; i--) {
  1104. dev_dbg(di->dev, "curr change_1 to: %x for 0x%02x\n",
  1105. (u8) i << shift_value, reg);
  1106. ret = abx500_set_register_interruptible(di->dev,
  1107. AB8500_CHARGER, reg, (u8)i << shift_value);
  1108. if (ret) {
  1109. dev_err(di->dev, "%s write failed\n", __func__);
  1110. goto exit_set_current;
  1111. }
  1112. if (i != curr_index)
  1113. usleep_range(step_udelay, step_udelay * 2);
  1114. }
  1115. } else {
  1116. bool allow = true;
  1117. for (i = prev_curr_index + 1; i <= curr_index && allow; i++) {
  1118. dev_dbg(di->dev, "curr change_2 to: %x for 0x%02x\n",
  1119. (u8)i << shift_value, reg);
  1120. ret = abx500_set_register_interruptible(di->dev,
  1121. AB8500_CHARGER, reg, (u8)i << shift_value);
  1122. if (ret) {
  1123. dev_err(di->dev, "%s write failed\n", __func__);
  1124. goto exit_set_current;
  1125. }
  1126. if (i != curr_index)
  1127. usleep_range(step_udelay, step_udelay * 2);
  1128. allow = ab8500_charger_check_continue_stepping(di, reg);
  1129. }
  1130. }
  1131. exit_set_current:
  1132. atomic_dec(&di->current_stepping_sessions);
  1133. return ret;
  1134. }
  1135. /**
  1136. * ab8500_charger_set_vbus_in_curr() - set VBUS input current limit
  1137. * @di: pointer to the ab8500_charger structure
  1138. * @ich_in: charger input current limit
  1139. *
  1140. * Sets the current that can be drawn from the USB host
  1141. * Returns error code in case of failure else 0(on success)
  1142. */
  1143. static int ab8500_charger_set_vbus_in_curr(struct ab8500_charger *di,
  1144. int ich_in)
  1145. {
  1146. int min_value;
  1147. int ret;
  1148. /* We should always use to lowest current limit */
  1149. min_value = min(di->bm->chg_params->usb_curr_max, ich_in);
  1150. if (di->max_usb_in_curr.set_max > 0)
  1151. min_value = min(di->max_usb_in_curr.set_max, min_value);
  1152. if (di->usb_state.usb_current >= 0)
  1153. min_value = min(di->usb_state.usb_current, min_value);
  1154. switch (min_value) {
  1155. case 100:
  1156. if (di->vbat < VBAT_TRESH_IP_CUR_RED)
  1157. min_value = USB_CH_IP_CUR_LVL_0P05;
  1158. break;
  1159. case 500:
  1160. if (di->vbat < VBAT_TRESH_IP_CUR_RED)
  1161. min_value = USB_CH_IP_CUR_LVL_0P45;
  1162. break;
  1163. default:
  1164. break;
  1165. }
  1166. dev_info(di->dev, "VBUS input current limit set to %d mA\n", min_value);
  1167. mutex_lock(&di->usb_ipt_crnt_lock);
  1168. ret = ab8500_charger_set_current(di, min_value,
  1169. AB8500_USBCH_IPT_CRNTLVL_REG);
  1170. mutex_unlock(&di->usb_ipt_crnt_lock);
  1171. return ret;
  1172. }
  1173. /**
  1174. * ab8500_charger_set_main_in_curr() - set main charger input current
  1175. * @di: pointer to the ab8500_charger structure
  1176. * @ich_in: input charger current, in mA
  1177. *
  1178. * Set main charger input current.
  1179. * Returns error code in case of failure else 0(on success)
  1180. */
  1181. static int ab8500_charger_set_main_in_curr(struct ab8500_charger *di,
  1182. int ich_in)
  1183. {
  1184. return ab8500_charger_set_current(di, ich_in,
  1185. AB8500_MCH_IPT_CURLVL_REG);
  1186. }
  1187. /**
  1188. * ab8500_charger_set_output_curr() - set charger output current
  1189. * @di: pointer to the ab8500_charger structure
  1190. * @ich_out: output charger current, in mA
  1191. *
  1192. * Set charger output current.
  1193. * Returns error code in case of failure else 0(on success)
  1194. */
  1195. static int ab8500_charger_set_output_curr(struct ab8500_charger *di,
  1196. int ich_out)
  1197. {
  1198. return ab8500_charger_set_current(di, ich_out,
  1199. AB8500_CH_OPT_CRNTLVL_REG);
  1200. }
  1201. /**
  1202. * ab8500_charger_led_en() - turn on/off chargign led
  1203. * @di: pointer to the ab8500_charger structure
  1204. * @on: flag to turn on/off the chargign led
  1205. *
  1206. * Power ON/OFF charging LED indication
  1207. * Returns error code in case of failure else 0(on success)
  1208. */
  1209. static int ab8500_charger_led_en(struct ab8500_charger *di, int on)
  1210. {
  1211. int ret;
  1212. if (on) {
  1213. /* Power ON charging LED indicator, set LED current to 5mA */
  1214. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1215. AB8500_LED_INDICATOR_PWM_CTRL,
  1216. (LED_IND_CUR_5MA | LED_INDICATOR_PWM_ENA));
  1217. if (ret) {
  1218. dev_err(di->dev, "Power ON LED failed\n");
  1219. return ret;
  1220. }
  1221. /* LED indicator PWM duty cycle 252/256 */
  1222. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1223. AB8500_LED_INDICATOR_PWM_DUTY,
  1224. LED_INDICATOR_PWM_DUTY_252_256);
  1225. if (ret) {
  1226. dev_err(di->dev, "Set LED PWM duty cycle failed\n");
  1227. return ret;
  1228. }
  1229. } else {
  1230. /* Power off charging LED indicator */
  1231. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1232. AB8500_LED_INDICATOR_PWM_CTRL,
  1233. LED_INDICATOR_PWM_DIS);
  1234. if (ret) {
  1235. dev_err(di->dev, "Power-off LED failed\n");
  1236. return ret;
  1237. }
  1238. }
  1239. return ret;
  1240. }
  1241. /**
  1242. * ab8500_charger_ac_en() - enable or disable ac charging
  1243. * @di: pointer to the ab8500_charger structure
  1244. * @enable: enable/disable flag
  1245. * @vset: charging voltage
  1246. * @iset: charging current
  1247. *
  1248. * Enable/Disable AC/Mains charging and turns on/off the charging led
  1249. * respectively.
  1250. **/
  1251. static int ab8500_charger_ac_en(struct ux500_charger *charger,
  1252. int enable, int vset, int iset)
  1253. {
  1254. int ret;
  1255. int volt_index;
  1256. int curr_index;
  1257. int input_curr_index;
  1258. u8 overshoot = 0;
  1259. struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
  1260. if (enable) {
  1261. /* Check if AC is connected */
  1262. if (!di->ac.charger_connected) {
  1263. dev_err(di->dev, "AC charger not connected\n");
  1264. return -ENXIO;
  1265. }
  1266. /* Enable AC charging */
  1267. dev_dbg(di->dev, "Enable AC: %dmV %dmA\n", vset, iset);
  1268. /*
  1269. * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
  1270. * will be triggered everytime we enable the VDD ADC supply.
  1271. * This will turn off charging for a short while.
  1272. * It can be avoided by having the supply on when
  1273. * there is a charger enabled. Normally the VDD ADC supply
  1274. * is enabled everytime a GPADC conversion is triggered. We will
  1275. * force it to be enabled from this driver to have
  1276. * the GPADC module independant of the AB8500 chargers
  1277. */
  1278. if (!di->vddadc_en_ac) {
  1279. ret = regulator_enable(di->regu);
  1280. if (ret)
  1281. dev_warn(di->dev,
  1282. "Failed to enable regulator\n");
  1283. else
  1284. di->vddadc_en_ac = true;
  1285. }
  1286. /* Check if the requested voltage or current is valid */
  1287. volt_index = ab8500_voltage_to_regval(vset);
  1288. curr_index = ab8500_current_to_regval(di, iset);
  1289. input_curr_index = ab8500_current_to_regval(di,
  1290. di->bm->chg_params->ac_curr_max);
  1291. if (volt_index < 0 || curr_index < 0 || input_curr_index < 0) {
  1292. dev_err(di->dev,
  1293. "Charger voltage or current too high, "
  1294. "charging not started\n");
  1295. return -ENXIO;
  1296. }
  1297. /* ChVoltLevel: maximum battery charging voltage */
  1298. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1299. AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
  1300. if (ret) {
  1301. dev_err(di->dev, "%s write failed\n", __func__);
  1302. return ret;
  1303. }
  1304. /* MainChInputCurr: current that can be drawn from the charger*/
  1305. ret = ab8500_charger_set_main_in_curr(di,
  1306. di->bm->chg_params->ac_curr_max);
  1307. if (ret) {
  1308. dev_err(di->dev, "%s Failed to set MainChInputCurr\n",
  1309. __func__);
  1310. return ret;
  1311. }
  1312. /* ChOutputCurentLevel: protected output current */
  1313. ret = ab8500_charger_set_output_curr(di, iset);
  1314. if (ret) {
  1315. dev_err(di->dev, "%s "
  1316. "Failed to set ChOutputCurentLevel\n",
  1317. __func__);
  1318. return ret;
  1319. }
  1320. /* Check if VBAT overshoot control should be enabled */
  1321. if (!di->bm->enable_overshoot)
  1322. overshoot = MAIN_CH_NO_OVERSHOOT_ENA_N;
  1323. /* Enable Main Charger */
  1324. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1325. AB8500_MCH_CTRL1, MAIN_CH_ENA | overshoot);
  1326. if (ret) {
  1327. dev_err(di->dev, "%s write failed\n", __func__);
  1328. return ret;
  1329. }
  1330. /* Power on charging LED indication */
  1331. ret = ab8500_charger_led_en(di, true);
  1332. if (ret < 0)
  1333. dev_err(di->dev, "failed to enable LED\n");
  1334. di->ac.charger_online = 1;
  1335. } else {
  1336. /* Disable AC charging */
  1337. if (is_ab8500_1p1_or_earlier(di->parent)) {
  1338. /*
  1339. * For ABB revision 1.0 and 1.1 there is a bug in the
  1340. * watchdog logic. That means we have to continously
  1341. * kick the charger watchdog even when no charger is
  1342. * connected. This is only valid once the AC charger
  1343. * has been enabled. This is a bug that is not handled
  1344. * by the algorithm and the watchdog have to be kicked
  1345. * by the charger driver when the AC charger
  1346. * is disabled
  1347. */
  1348. if (di->ac_conn) {
  1349. queue_delayed_work(di->charger_wq,
  1350. &di->kick_wd_work,
  1351. round_jiffies(WD_KICK_INTERVAL));
  1352. }
  1353. /*
  1354. * We can't turn off charging completely
  1355. * due to a bug in AB8500 cut1.
  1356. * If we do, charging will not start again.
  1357. * That is why we set the lowest voltage
  1358. * and current possible
  1359. */
  1360. ret = abx500_set_register_interruptible(di->dev,
  1361. AB8500_CHARGER,
  1362. AB8500_CH_VOLT_LVL_REG, CH_VOL_LVL_3P5);
  1363. if (ret) {
  1364. dev_err(di->dev,
  1365. "%s write failed\n", __func__);
  1366. return ret;
  1367. }
  1368. ret = ab8500_charger_set_output_curr(di, 0);
  1369. if (ret) {
  1370. dev_err(di->dev, "%s "
  1371. "Failed to set ChOutputCurentLevel\n",
  1372. __func__);
  1373. return ret;
  1374. }
  1375. } else {
  1376. ret = abx500_set_register_interruptible(di->dev,
  1377. AB8500_CHARGER,
  1378. AB8500_MCH_CTRL1, 0);
  1379. if (ret) {
  1380. dev_err(di->dev,
  1381. "%s write failed\n", __func__);
  1382. return ret;
  1383. }
  1384. }
  1385. ret = ab8500_charger_led_en(di, false);
  1386. if (ret < 0)
  1387. dev_err(di->dev, "failed to disable LED\n");
  1388. di->ac.charger_online = 0;
  1389. di->ac.wd_expired = false;
  1390. /* Disable regulator if enabled */
  1391. if (di->vddadc_en_ac) {
  1392. regulator_disable(di->regu);
  1393. di->vddadc_en_ac = false;
  1394. }
  1395. dev_dbg(di->dev, "%s Disabled AC charging\n", __func__);
  1396. }
  1397. ab8500_power_supply_changed(di, di->ac_chg.psy);
  1398. return ret;
  1399. }
  1400. /**
  1401. * ab8500_charger_usb_en() - enable usb charging
  1402. * @di: pointer to the ab8500_charger structure
  1403. * @enable: enable/disable flag
  1404. * @vset: charging voltage
  1405. * @ich_out: charger output current
  1406. *
  1407. * Enable/Disable USB charging and turns on/off the charging led respectively.
  1408. * Returns error code in case of failure else 0(on success)
  1409. */
  1410. static int ab8500_charger_usb_en(struct ux500_charger *charger,
  1411. int enable, int vset, int ich_out)
  1412. {
  1413. int ret;
  1414. int volt_index;
  1415. int curr_index;
  1416. u8 overshoot = 0;
  1417. struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
  1418. if (enable) {
  1419. /* Check if USB is connected */
  1420. if (!di->usb.charger_connected) {
  1421. dev_err(di->dev, "USB charger not connected\n");
  1422. return -ENXIO;
  1423. }
  1424. /*
  1425. * Due to a bug in AB8500, BTEMP_HIGH/LOW interrupts
  1426. * will be triggered everytime we enable the VDD ADC supply.
  1427. * This will turn off charging for a short while.
  1428. * It can be avoided by having the supply on when
  1429. * there is a charger enabled. Normally the VDD ADC supply
  1430. * is enabled everytime a GPADC conversion is triggered. We will
  1431. * force it to be enabled from this driver to have
  1432. * the GPADC module independant of the AB8500 chargers
  1433. */
  1434. if (!di->vddadc_en_usb) {
  1435. ret = regulator_enable(di->regu);
  1436. if (ret)
  1437. dev_warn(di->dev,
  1438. "Failed to enable regulator\n");
  1439. else
  1440. di->vddadc_en_usb = true;
  1441. }
  1442. /* Enable USB charging */
  1443. dev_dbg(di->dev, "Enable USB: %dmV %dmA\n", vset, ich_out);
  1444. /* Check if the requested voltage or current is valid */
  1445. volt_index = ab8500_voltage_to_regval(vset);
  1446. curr_index = ab8500_current_to_regval(di, ich_out);
  1447. if (volt_index < 0 || curr_index < 0) {
  1448. dev_err(di->dev,
  1449. "Charger voltage or current too high, "
  1450. "charging not started\n");
  1451. return -ENXIO;
  1452. }
  1453. /* ChVoltLevel: max voltage upto which battery can be charged */
  1454. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1455. AB8500_CH_VOLT_LVL_REG, (u8) volt_index);
  1456. if (ret) {
  1457. dev_err(di->dev, "%s write failed\n", __func__);
  1458. return ret;
  1459. }
  1460. /* Check if VBAT overshoot control should be enabled */
  1461. if (!di->bm->enable_overshoot)
  1462. overshoot = USB_CHG_NO_OVERSHOOT_ENA_N;
  1463. /* Enable USB Charger */
  1464. dev_dbg(di->dev,
  1465. "Enabling USB with write to AB8500_USBCH_CTRL1_REG\n");
  1466. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1467. AB8500_USBCH_CTRL1_REG, USB_CH_ENA | overshoot);
  1468. if (ret) {
  1469. dev_err(di->dev, "%s write failed\n", __func__);
  1470. return ret;
  1471. }
  1472. /* If success power on charging LED indication */
  1473. ret = ab8500_charger_led_en(di, true);
  1474. if (ret < 0)
  1475. dev_err(di->dev, "failed to enable LED\n");
  1476. di->usb.charger_online = 1;
  1477. /* USBChInputCurr: current that can be drawn from the usb */
  1478. ret = ab8500_charger_set_vbus_in_curr(di,
  1479. di->max_usb_in_curr.usb_type_max);
  1480. if (ret) {
  1481. dev_err(di->dev, "setting USBChInputCurr failed\n");
  1482. return ret;
  1483. }
  1484. /* ChOutputCurentLevel: protected output current */
  1485. ret = ab8500_charger_set_output_curr(di, ich_out);
  1486. if (ret) {
  1487. dev_err(di->dev, "%s "
  1488. "Failed to set ChOutputCurentLevel\n",
  1489. __func__);
  1490. return ret;
  1491. }
  1492. queue_delayed_work(di->charger_wq, &di->check_vbat_work, HZ);
  1493. } else {
  1494. /* Disable USB charging */
  1495. dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
  1496. ret = abx500_set_register_interruptible(di->dev,
  1497. AB8500_CHARGER,
  1498. AB8500_USBCH_CTRL1_REG, 0);
  1499. if (ret) {
  1500. dev_err(di->dev,
  1501. "%s write failed\n", __func__);
  1502. return ret;
  1503. }
  1504. ret = ab8500_charger_led_en(di, false);
  1505. if (ret < 0)
  1506. dev_err(di->dev, "failed to disable LED\n");
  1507. /* USBChInputCurr: current that can be drawn from the usb */
  1508. ret = ab8500_charger_set_vbus_in_curr(di, 0);
  1509. if (ret) {
  1510. dev_err(di->dev, "setting USBChInputCurr failed\n");
  1511. return ret;
  1512. }
  1513. /* ChOutputCurentLevel: protected output current */
  1514. ret = ab8500_charger_set_output_curr(di, 0);
  1515. if (ret) {
  1516. dev_err(di->dev, "%s "
  1517. "Failed to reset ChOutputCurentLevel\n",
  1518. __func__);
  1519. return ret;
  1520. }
  1521. di->usb.charger_online = 0;
  1522. di->usb.wd_expired = false;
  1523. /* Disable regulator if enabled */
  1524. if (di->vddadc_en_usb) {
  1525. regulator_disable(di->regu);
  1526. di->vddadc_en_usb = false;
  1527. }
  1528. dev_dbg(di->dev, "%s Disabled USB charging\n", __func__);
  1529. /* Cancel any pending Vbat check work */
  1530. cancel_delayed_work(&di->check_vbat_work);
  1531. }
  1532. ab8500_power_supply_changed(di, di->usb_chg.psy);
  1533. return ret;
  1534. }
  1535. static int ab8500_external_charger_prepare(struct notifier_block *charger_nb,
  1536. unsigned long event, void *data)
  1537. {
  1538. int ret;
  1539. struct device *dev = data;
  1540. /*Toggle External charger control pin*/
  1541. ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
  1542. AB8500_SYS_CHARGER_CONTROL_REG,
  1543. EXTERNAL_CHARGER_DISABLE_REG_VAL);
  1544. if (ret < 0) {
  1545. dev_err(dev, "write reg failed %d\n", ret);
  1546. goto out;
  1547. }
  1548. ret = abx500_set_register_interruptible(dev, AB8500_SYS_CTRL1_BLOCK,
  1549. AB8500_SYS_CHARGER_CONTROL_REG,
  1550. EXTERNAL_CHARGER_ENABLE_REG_VAL);
  1551. if (ret < 0)
  1552. dev_err(dev, "Write reg failed %d\n", ret);
  1553. out:
  1554. return ret;
  1555. }
  1556. /**
  1557. * ab8500_charger_usb_check_enable() - enable usb charging
  1558. * @charger: pointer to the ux500_charger structure
  1559. * @vset: charging voltage
  1560. * @iset: charger output current
  1561. *
  1562. * Check if the VBUS charger has been disconnected and reconnected without
  1563. * AB8500 rising an interrupt. Returns 0 on success.
  1564. */
  1565. static int ab8500_charger_usb_check_enable(struct ux500_charger *charger,
  1566. int vset, int iset)
  1567. {
  1568. u8 usbch_ctrl1 = 0;
  1569. int ret = 0;
  1570. struct ab8500_charger *di = to_ab8500_charger_usb_device_info(charger);
  1571. if (!di->usb.charger_connected)
  1572. return ret;
  1573. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  1574. AB8500_USBCH_CTRL1_REG, &usbch_ctrl1);
  1575. if (ret < 0) {
  1576. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1577. return ret;
  1578. }
  1579. dev_dbg(di->dev, "USB charger ctrl: 0x%02x\n", usbch_ctrl1);
  1580. if (!(usbch_ctrl1 & USB_CH_ENA)) {
  1581. dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
  1582. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1583. AB8500_CHARGER, AB8500_CHARGER_CTRL,
  1584. DROP_COUNT_RESET, DROP_COUNT_RESET);
  1585. if (ret < 0) {
  1586. dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
  1587. return ret;
  1588. }
  1589. ret = ab8500_charger_usb_en(&di->usb_chg, true, vset, iset);
  1590. if (ret < 0) {
  1591. dev_err(di->dev, "Failed to enable VBUS charger %d\n",
  1592. __LINE__);
  1593. return ret;
  1594. }
  1595. }
  1596. return ret;
  1597. }
  1598. /**
  1599. * ab8500_charger_ac_check_enable() - enable usb charging
  1600. * @charger: pointer to the ux500_charger structure
  1601. * @vset: charging voltage
  1602. * @iset: charger output current
  1603. *
  1604. * Check if the AC charger has been disconnected and reconnected without
  1605. * AB8500 rising an interrupt. Returns 0 on success.
  1606. */
  1607. static int ab8500_charger_ac_check_enable(struct ux500_charger *charger,
  1608. int vset, int iset)
  1609. {
  1610. u8 mainch_ctrl1 = 0;
  1611. int ret = 0;
  1612. struct ab8500_charger *di = to_ab8500_charger_ac_device_info(charger);
  1613. if (!di->ac.charger_connected)
  1614. return ret;
  1615. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  1616. AB8500_MCH_CTRL1, &mainch_ctrl1);
  1617. if (ret < 0) {
  1618. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1619. return ret;
  1620. }
  1621. dev_dbg(di->dev, "AC charger ctrl: 0x%02x\n", mainch_ctrl1);
  1622. if (!(mainch_ctrl1 & MAIN_CH_ENA)) {
  1623. dev_info(di->dev, "Charging has been disabled abnormally and will be re-enabled\n");
  1624. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1625. AB8500_CHARGER, AB8500_CHARGER_CTRL,
  1626. DROP_COUNT_RESET, DROP_COUNT_RESET);
  1627. if (ret < 0) {
  1628. dev_err(di->dev, "ab8500 write failed %d\n", __LINE__);
  1629. return ret;
  1630. }
  1631. ret = ab8500_charger_ac_en(&di->usb_chg, true, vset, iset);
  1632. if (ret < 0) {
  1633. dev_err(di->dev, "failed to enable AC charger %d\n",
  1634. __LINE__);
  1635. return ret;
  1636. }
  1637. }
  1638. return ret;
  1639. }
  1640. /**
  1641. * ab8500_charger_watchdog_kick() - kick charger watchdog
  1642. * @di: pointer to the ab8500_charger structure
  1643. *
  1644. * Kick charger watchdog
  1645. * Returns error code in case of failure else 0(on success)
  1646. */
  1647. static int ab8500_charger_watchdog_kick(struct ux500_charger *charger)
  1648. {
  1649. int ret;
  1650. struct ab8500_charger *di;
  1651. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
  1652. di = to_ab8500_charger_ac_device_info(charger);
  1653. else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
  1654. di = to_ab8500_charger_usb_device_info(charger);
  1655. else
  1656. return -ENXIO;
  1657. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1658. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  1659. if (ret)
  1660. dev_err(di->dev, "Failed to kick WD!\n");
  1661. return ret;
  1662. }
  1663. /**
  1664. * ab8500_charger_update_charger_current() - update charger current
  1665. * @di: pointer to the ab8500_charger structure
  1666. *
  1667. * Update the charger output current for the specified charger
  1668. * Returns error code in case of failure else 0(on success)
  1669. */
  1670. static int ab8500_charger_update_charger_current(struct ux500_charger *charger,
  1671. int ich_out)
  1672. {
  1673. int ret;
  1674. struct ab8500_charger *di;
  1675. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
  1676. di = to_ab8500_charger_ac_device_info(charger);
  1677. else if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
  1678. di = to_ab8500_charger_usb_device_info(charger);
  1679. else
  1680. return -ENXIO;
  1681. ret = ab8500_charger_set_output_curr(di, ich_out);
  1682. if (ret) {
  1683. dev_err(di->dev, "%s "
  1684. "Failed to set ChOutputCurentLevel\n",
  1685. __func__);
  1686. return ret;
  1687. }
  1688. /* Reset the main and usb drop input current measurement counter */
  1689. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1690. AB8500_CHARGER_CTRL, DROP_COUNT_RESET);
  1691. if (ret) {
  1692. dev_err(di->dev, "%s write failed\n", __func__);
  1693. return ret;
  1694. }
  1695. return ret;
  1696. }
  1697. /**
  1698. * ab8540_charger_power_path_enable() - enable usb power path mode
  1699. * @charger: pointer to the ux500_charger structure
  1700. * @enable: enable/disable flag
  1701. *
  1702. * Enable or disable the power path for usb mode
  1703. * Returns error code in case of failure else 0(on success)
  1704. */
  1705. static int ab8540_charger_power_path_enable(struct ux500_charger *charger,
  1706. bool enable)
  1707. {
  1708. int ret;
  1709. struct ab8500_charger *di;
  1710. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
  1711. di = to_ab8500_charger_usb_device_info(charger);
  1712. else
  1713. return -ENXIO;
  1714. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1715. AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
  1716. BUS_POWER_PATH_MODE_ENA, enable);
  1717. if (ret) {
  1718. dev_err(di->dev, "%s write failed\n", __func__);
  1719. return ret;
  1720. }
  1721. return ret;
  1722. }
  1723. /**
  1724. * ab8540_charger_usb_pre_chg_enable() - enable usb pre change
  1725. * @charger: pointer to the ux500_charger structure
  1726. * @enable: enable/disable flag
  1727. *
  1728. * Enable or disable the pre-chage for usb mode
  1729. * Returns error code in case of failure else 0(on success)
  1730. */
  1731. static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
  1732. bool enable)
  1733. {
  1734. int ret;
  1735. struct ab8500_charger *di;
  1736. if (charger->psy->desc->type == POWER_SUPPLY_TYPE_USB)
  1737. di = to_ab8500_charger_usb_device_info(charger);
  1738. else
  1739. return -ENXIO;
  1740. ret = abx500_mask_and_set_register_interruptible(di->dev,
  1741. AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
  1742. BUS_POWER_PATH_PRECHG_ENA, enable);
  1743. if (ret) {
  1744. dev_err(di->dev, "%s write failed\n", __func__);
  1745. return ret;
  1746. }
  1747. return ret;
  1748. }
  1749. static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
  1750. {
  1751. struct power_supply *psy;
  1752. struct power_supply *ext = dev_get_drvdata(dev);
  1753. const char **supplicants = (const char **)ext->supplied_to;
  1754. struct ab8500_charger *di;
  1755. union power_supply_propval ret;
  1756. int j;
  1757. struct ux500_charger *usb_chg;
  1758. usb_chg = (struct ux500_charger *)data;
  1759. psy = usb_chg->psy;
  1760. di = to_ab8500_charger_usb_device_info(usb_chg);
  1761. /* For all psy where the driver name appears in any supplied_to */
  1762. j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
  1763. if (j < 0)
  1764. return 0;
  1765. /* Go through all properties for the psy */
  1766. for (j = 0; j < ext->desc->num_properties; j++) {
  1767. enum power_supply_property prop;
  1768. prop = ext->desc->properties[j];
  1769. if (power_supply_get_property(ext, prop, &ret))
  1770. continue;
  1771. switch (prop) {
  1772. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  1773. switch (ext->desc->type) {
  1774. case POWER_SUPPLY_TYPE_BATTERY:
  1775. di->vbat = ret.intval / 1000;
  1776. break;
  1777. default:
  1778. break;
  1779. }
  1780. break;
  1781. default:
  1782. break;
  1783. }
  1784. }
  1785. return 0;
  1786. }
  1787. /**
  1788. * ab8500_charger_check_vbat_work() - keep vbus current within spec
  1789. * @work pointer to the work_struct structure
  1790. *
  1791. * Due to a asic bug it is necessary to lower the input current to the vbus
  1792. * charger when charging with at some specific levels. This issue is only valid
  1793. * for below a certain battery voltage. This function makes sure that the
  1794. * the allowed current limit isn't exceeded.
  1795. */
  1796. static void ab8500_charger_check_vbat_work(struct work_struct *work)
  1797. {
  1798. int t = 10;
  1799. struct ab8500_charger *di = container_of(work,
  1800. struct ab8500_charger, check_vbat_work.work);
  1801. class_for_each_device(power_supply_class, NULL,
  1802. di->usb_chg.psy, ab8500_charger_get_ext_psy_data);
  1803. /* First run old_vbat is 0. */
  1804. if (di->old_vbat == 0)
  1805. di->old_vbat = di->vbat;
  1806. if (!((di->old_vbat <= VBAT_TRESH_IP_CUR_RED &&
  1807. di->vbat <= VBAT_TRESH_IP_CUR_RED) ||
  1808. (di->old_vbat > VBAT_TRESH_IP_CUR_RED &&
  1809. di->vbat > VBAT_TRESH_IP_CUR_RED))) {
  1810. dev_dbg(di->dev, "Vbat did cross threshold, curr: %d, new: %d,"
  1811. " old: %d\n", di->max_usb_in_curr.usb_type_max,
  1812. di->vbat, di->old_vbat);
  1813. ab8500_charger_set_vbus_in_curr(di,
  1814. di->max_usb_in_curr.usb_type_max);
  1815. power_supply_changed(di->usb_chg.psy);
  1816. }
  1817. di->old_vbat = di->vbat;
  1818. /*
  1819. * No need to check the battery voltage every second when not close to
  1820. * the threshold.
  1821. */
  1822. if (di->vbat < (VBAT_TRESH_IP_CUR_RED + 100) &&
  1823. (di->vbat > (VBAT_TRESH_IP_CUR_RED - 100)))
  1824. t = 1;
  1825. queue_delayed_work(di->charger_wq, &di->check_vbat_work, t * HZ);
  1826. }
  1827. /**
  1828. * ab8500_charger_check_hw_failure_work() - check main charger failure
  1829. * @work: pointer to the work_struct structure
  1830. *
  1831. * Work queue function for checking the main charger status
  1832. */
  1833. static void ab8500_charger_check_hw_failure_work(struct work_struct *work)
  1834. {
  1835. int ret;
  1836. u8 reg_value;
  1837. struct ab8500_charger *di = container_of(work,
  1838. struct ab8500_charger, check_hw_failure_work.work);
  1839. /* Check if the status bits for HW failure is still active */
  1840. if (di->flags.mainextchnotok) {
  1841. ret = abx500_get_register_interruptible(di->dev,
  1842. AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
  1843. if (ret < 0) {
  1844. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1845. return;
  1846. }
  1847. if (!(reg_value & MAIN_CH_NOK)) {
  1848. di->flags.mainextchnotok = false;
  1849. ab8500_power_supply_changed(di, di->ac_chg.psy);
  1850. }
  1851. }
  1852. if (di->flags.vbus_ovv) {
  1853. ret = abx500_get_register_interruptible(di->dev,
  1854. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG,
  1855. &reg_value);
  1856. if (ret < 0) {
  1857. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  1858. return;
  1859. }
  1860. if (!(reg_value & VBUS_OVV_TH)) {
  1861. di->flags.vbus_ovv = false;
  1862. ab8500_power_supply_changed(di, di->usb_chg.psy);
  1863. }
  1864. }
  1865. /* If we still have a failure, schedule a new check */
  1866. if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
  1867. queue_delayed_work(di->charger_wq,
  1868. &di->check_hw_failure_work, round_jiffies(HZ));
  1869. }
  1870. }
  1871. /**
  1872. * ab8500_charger_kick_watchdog_work() - kick the watchdog
  1873. * @work: pointer to the work_struct structure
  1874. *
  1875. * Work queue function for kicking the charger watchdog.
  1876. *
  1877. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  1878. * logic. That means we have to continously kick the charger
  1879. * watchdog even when no charger is connected. This is only
  1880. * valid once the AC charger has been enabled. This is
  1881. * a bug that is not handled by the algorithm and the
  1882. * watchdog have to be kicked by the charger driver
  1883. * when the AC charger is disabled
  1884. */
  1885. static void ab8500_charger_kick_watchdog_work(struct work_struct *work)
  1886. {
  1887. int ret;
  1888. struct ab8500_charger *di = container_of(work,
  1889. struct ab8500_charger, kick_wd_work.work);
  1890. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  1891. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  1892. if (ret)
  1893. dev_err(di->dev, "Failed to kick WD!\n");
  1894. /* Schedule a new watchdog kick */
  1895. queue_delayed_work(di->charger_wq,
  1896. &di->kick_wd_work, round_jiffies(WD_KICK_INTERVAL));
  1897. }
  1898. /**
  1899. * ab8500_charger_ac_work() - work to get and set main charger status
  1900. * @work: pointer to the work_struct structure
  1901. *
  1902. * Work queue function for checking the main charger status
  1903. */
  1904. static void ab8500_charger_ac_work(struct work_struct *work)
  1905. {
  1906. int ret;
  1907. struct ab8500_charger *di = container_of(work,
  1908. struct ab8500_charger, ac_work);
  1909. /*
  1910. * Since we can't be sure that the events are received
  1911. * synchronously, we have the check if the main charger is
  1912. * connected by reading the status register
  1913. */
  1914. ret = ab8500_charger_detect_chargers(di, false);
  1915. if (ret < 0)
  1916. return;
  1917. if (ret & AC_PW_CONN) {
  1918. di->ac.charger_connected = 1;
  1919. di->ac_conn = true;
  1920. } else {
  1921. di->ac.charger_connected = 0;
  1922. }
  1923. ab8500_power_supply_changed(di, di->ac_chg.psy);
  1924. sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
  1925. }
  1926. static void ab8500_charger_usb_attached_work(struct work_struct *work)
  1927. {
  1928. struct ab8500_charger *di = container_of(work,
  1929. struct ab8500_charger,
  1930. usb_charger_attached_work.work);
  1931. int usbch = (USB_CH_VBUSDROP | USB_CH_VBUSDETDBNC);
  1932. int ret, i;
  1933. u8 statval;
  1934. for (i = 0; i < 10; i++) {
  1935. ret = abx500_get_register_interruptible(di->dev,
  1936. AB8500_CHARGER,
  1937. AB8500_CH_USBCH_STAT1_REG,
  1938. &statval);
  1939. if (ret < 0) {
  1940. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1941. goto reschedule;
  1942. }
  1943. if ((statval & usbch) != usbch)
  1944. goto reschedule;
  1945. msleep(CHARGER_STATUS_POLL);
  1946. }
  1947. ab8500_charger_usb_en(&di->usb_chg, 0, 0, 0);
  1948. mutex_lock(&di->charger_attached_mutex);
  1949. mutex_unlock(&di->charger_attached_mutex);
  1950. return;
  1951. reschedule:
  1952. queue_delayed_work(di->charger_wq,
  1953. &di->usb_charger_attached_work,
  1954. HZ);
  1955. }
  1956. static void ab8500_charger_ac_attached_work(struct work_struct *work)
  1957. {
  1958. struct ab8500_charger *di = container_of(work,
  1959. struct ab8500_charger,
  1960. ac_charger_attached_work.work);
  1961. int mainch = (MAIN_CH_STATUS2_MAINCHGDROP |
  1962. MAIN_CH_STATUS2_MAINCHARGERDETDBNC);
  1963. int ret, i;
  1964. u8 statval;
  1965. for (i = 0; i < 10; i++) {
  1966. ret = abx500_get_register_interruptible(di->dev,
  1967. AB8500_CHARGER,
  1968. AB8500_CH_STATUS2_REG,
  1969. &statval);
  1970. if (ret < 0) {
  1971. dev_err(di->dev, "ab8500 read failed %d\n", __LINE__);
  1972. goto reschedule;
  1973. }
  1974. if ((statval & mainch) != mainch)
  1975. goto reschedule;
  1976. msleep(CHARGER_STATUS_POLL);
  1977. }
  1978. ab8500_charger_ac_en(&di->ac_chg, 0, 0, 0);
  1979. queue_work(di->charger_wq, &di->ac_work);
  1980. mutex_lock(&di->charger_attached_mutex);
  1981. mutex_unlock(&di->charger_attached_mutex);
  1982. return;
  1983. reschedule:
  1984. queue_delayed_work(di->charger_wq,
  1985. &di->ac_charger_attached_work,
  1986. HZ);
  1987. }
  1988. /**
  1989. * ab8500_charger_detect_usb_type_work() - work to detect USB type
  1990. * @work: Pointer to the work_struct structure
  1991. *
  1992. * Detect the type of USB plugged
  1993. */
  1994. static void ab8500_charger_detect_usb_type_work(struct work_struct *work)
  1995. {
  1996. int ret;
  1997. struct ab8500_charger *di = container_of(work,
  1998. struct ab8500_charger, detect_usb_type_work);
  1999. /*
  2000. * Since we can't be sure that the events are received
  2001. * synchronously, we have the check if is
  2002. * connected by reading the status register
  2003. */
  2004. ret = ab8500_charger_detect_chargers(di, false);
  2005. if (ret < 0)
  2006. return;
  2007. if (!(ret & USB_PW_CONN)) {
  2008. dev_dbg(di->dev, "%s di->vbus_detected = false\n", __func__);
  2009. di->vbus_detected = false;
  2010. ab8500_charger_set_usb_connected(di, false);
  2011. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2012. } else {
  2013. dev_dbg(di->dev, "%s di->vbus_detected = true\n", __func__);
  2014. di->vbus_detected = true;
  2015. if (is_ab8500_1p1_or_earlier(di->parent)) {
  2016. ret = ab8500_charger_detect_usb_type(di);
  2017. if (!ret) {
  2018. ab8500_charger_set_usb_connected(di, true);
  2019. ab8500_power_supply_changed(di,
  2020. di->usb_chg.psy);
  2021. }
  2022. } else {
  2023. /*
  2024. * For ABB cut2.0 and onwards we have an IRQ,
  2025. * USB_LINK_STATUS that will be triggered when the USB
  2026. * link status changes. The exception is USB connected
  2027. * during startup. Then we don't get a
  2028. * USB_LINK_STATUS IRQ
  2029. */
  2030. if (di->vbus_detected_start) {
  2031. di->vbus_detected_start = false;
  2032. ret = ab8500_charger_detect_usb_type(di);
  2033. if (!ret) {
  2034. ab8500_charger_set_usb_connected(di,
  2035. true);
  2036. ab8500_power_supply_changed(di,
  2037. di->usb_chg.psy);
  2038. }
  2039. }
  2040. }
  2041. }
  2042. }
  2043. /**
  2044. * ab8500_charger_usb_link_attach_work() - work to detect USB type
  2045. * @work: pointer to the work_struct structure
  2046. *
  2047. * Detect the type of USB plugged
  2048. */
  2049. static void ab8500_charger_usb_link_attach_work(struct work_struct *work)
  2050. {
  2051. struct ab8500_charger *di =
  2052. container_of(work, struct ab8500_charger, attach_work.work);
  2053. int ret;
  2054. /* Update maximum input current if USB enumeration is not detected */
  2055. if (!di->usb.charger_online) {
  2056. ret = ab8500_charger_set_vbus_in_curr(di,
  2057. di->max_usb_in_curr.usb_type_max);
  2058. if (ret)
  2059. return;
  2060. }
  2061. ab8500_charger_set_usb_connected(di, true);
  2062. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2063. }
  2064. /**
  2065. * ab8500_charger_usb_link_status_work() - work to detect USB type
  2066. * @work: pointer to the work_struct structure
  2067. *
  2068. * Detect the type of USB plugged
  2069. */
  2070. static void ab8500_charger_usb_link_status_work(struct work_struct *work)
  2071. {
  2072. int detected_chargers;
  2073. int ret;
  2074. u8 val;
  2075. u8 link_status;
  2076. struct ab8500_charger *di = container_of(work,
  2077. struct ab8500_charger, usb_link_status_work);
  2078. /*
  2079. * Since we can't be sure that the events are received
  2080. * synchronously, we have the check if is
  2081. * connected by reading the status register
  2082. */
  2083. detected_chargers = ab8500_charger_detect_chargers(di, false);
  2084. if (detected_chargers < 0)
  2085. return;
  2086. /*
  2087. * Some chargers that breaks the USB spec is
  2088. * identified as invalid by AB8500 and it refuse
  2089. * to start the charging process. but by jumping
  2090. * thru a few hoops it can be forced to start.
  2091. */
  2092. if (is_ab8500(di->parent))
  2093. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  2094. AB8500_USB_LINE_STAT_REG, &val);
  2095. else
  2096. ret = abx500_get_register_interruptible(di->dev, AB8500_USB,
  2097. AB8500_USB_LINK1_STAT_REG, &val);
  2098. if (ret >= 0)
  2099. dev_dbg(di->dev, "UsbLineStatus register = 0x%02x\n", val);
  2100. else
  2101. dev_dbg(di->dev, "Error reading USB link status\n");
  2102. if (is_ab8500(di->parent))
  2103. link_status = AB8500_USB_LINK_STATUS;
  2104. else
  2105. link_status = AB8505_USB_LINK_STATUS;
  2106. if (detected_chargers & USB_PW_CONN) {
  2107. if (((val & link_status) >> USB_LINK_STATUS_SHIFT) ==
  2108. USB_STAT_NOT_VALID_LINK &&
  2109. di->invalid_charger_detect_state == 0) {
  2110. dev_dbg(di->dev,
  2111. "Invalid charger detected, state= 0\n");
  2112. /*Enable charger*/
  2113. abx500_mask_and_set_register_interruptible(di->dev,
  2114. AB8500_CHARGER, AB8500_USBCH_CTRL1_REG,
  2115. USB_CH_ENA, USB_CH_ENA);
  2116. /*Enable charger detection*/
  2117. abx500_mask_and_set_register_interruptible(di->dev,
  2118. AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
  2119. USB_CH_DET, USB_CH_DET);
  2120. di->invalid_charger_detect_state = 1;
  2121. /*exit and wait for new link status interrupt.*/
  2122. return;
  2123. }
  2124. if (di->invalid_charger_detect_state == 1) {
  2125. dev_dbg(di->dev,
  2126. "Invalid charger detected, state= 1\n");
  2127. /*Stop charger detection*/
  2128. abx500_mask_and_set_register_interruptible(di->dev,
  2129. AB8500_USB, AB8500_USB_LINE_CTRL2_REG,
  2130. USB_CH_DET, 0x00);
  2131. /*Check link status*/
  2132. if (is_ab8500(di->parent))
  2133. ret = abx500_get_register_interruptible(di->dev,
  2134. AB8500_USB, AB8500_USB_LINE_STAT_REG,
  2135. &val);
  2136. else
  2137. ret = abx500_get_register_interruptible(di->dev,
  2138. AB8500_USB, AB8500_USB_LINK1_STAT_REG,
  2139. &val);
  2140. dev_dbg(di->dev, "USB link status= 0x%02x\n",
  2141. (val & link_status) >> USB_LINK_STATUS_SHIFT);
  2142. di->invalid_charger_detect_state = 2;
  2143. }
  2144. } else {
  2145. di->invalid_charger_detect_state = 0;
  2146. }
  2147. if (!(detected_chargers & USB_PW_CONN)) {
  2148. di->vbus_detected = false;
  2149. ab8500_charger_set_usb_connected(di, false);
  2150. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2151. return;
  2152. }
  2153. dev_dbg(di->dev,"%s di->vbus_detected = true\n",__func__);
  2154. di->vbus_detected = true;
  2155. ret = ab8500_charger_read_usb_type(di);
  2156. if (ret) {
  2157. if (ret == -ENXIO) {
  2158. /* No valid charger type detected */
  2159. ab8500_charger_set_usb_connected(di, false);
  2160. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2161. }
  2162. return;
  2163. }
  2164. if (di->usb_device_is_unrecognised) {
  2165. dev_dbg(di->dev,
  2166. "Potential Legacy Charger device. "
  2167. "Delay work for %d msec for USB enum "
  2168. "to finish",
  2169. WAIT_ACA_RID_ENUMERATION);
  2170. queue_delayed_work(di->charger_wq,
  2171. &di->attach_work,
  2172. msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
  2173. } else if (di->is_aca_rid == 1) {
  2174. /* Only wait once */
  2175. di->is_aca_rid++;
  2176. dev_dbg(di->dev,
  2177. "%s Wait %d msec for USB enum to finish",
  2178. __func__, WAIT_ACA_RID_ENUMERATION);
  2179. queue_delayed_work(di->charger_wq,
  2180. &di->attach_work,
  2181. msecs_to_jiffies(WAIT_ACA_RID_ENUMERATION));
  2182. } else {
  2183. queue_delayed_work(di->charger_wq,
  2184. &di->attach_work,
  2185. 0);
  2186. }
  2187. }
  2188. static void ab8500_charger_usb_state_changed_work(struct work_struct *work)
  2189. {
  2190. int ret;
  2191. unsigned long flags;
  2192. struct ab8500_charger *di = container_of(work,
  2193. struct ab8500_charger, usb_state_changed_work.work);
  2194. if (!di->vbus_detected) {
  2195. dev_dbg(di->dev,
  2196. "%s !di->vbus_detected\n",
  2197. __func__);
  2198. return;
  2199. }
  2200. spin_lock_irqsave(&di->usb_state.usb_lock, flags);
  2201. di->usb_state.state = di->usb_state.state_tmp;
  2202. di->usb_state.usb_current = di->usb_state.usb_current_tmp;
  2203. spin_unlock_irqrestore(&di->usb_state.usb_lock, flags);
  2204. dev_dbg(di->dev, "%s USB state: 0x%02x mA: %d\n",
  2205. __func__, di->usb_state.state, di->usb_state.usb_current);
  2206. switch (di->usb_state.state) {
  2207. case AB8500_BM_USB_STATE_RESET_HS:
  2208. case AB8500_BM_USB_STATE_RESET_FS:
  2209. case AB8500_BM_USB_STATE_SUSPEND:
  2210. case AB8500_BM_USB_STATE_MAX:
  2211. ab8500_charger_set_usb_connected(di, false);
  2212. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2213. break;
  2214. case AB8500_BM_USB_STATE_RESUME:
  2215. /*
  2216. * when suspend->resume there should be delay
  2217. * of 1sec for enabling charging
  2218. */
  2219. msleep(1000);
  2220. /* Intentional fall through */
  2221. case AB8500_BM_USB_STATE_CONFIGURED:
  2222. /*
  2223. * USB is configured, enable charging with the charging
  2224. * input current obtained from USB driver
  2225. */
  2226. if (!ab8500_charger_get_usb_cur(di)) {
  2227. /* Update maximum input current */
  2228. ret = ab8500_charger_set_vbus_in_curr(di,
  2229. di->max_usb_in_curr.usb_type_max);
  2230. if (ret)
  2231. return;
  2232. ab8500_charger_set_usb_connected(di, true);
  2233. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2234. }
  2235. break;
  2236. default:
  2237. break;
  2238. };
  2239. }
  2240. /**
  2241. * ab8500_charger_check_usbchargernotok_work() - check USB chg not ok status
  2242. * @work: pointer to the work_struct structure
  2243. *
  2244. * Work queue function for checking the USB charger Not OK status
  2245. */
  2246. static void ab8500_charger_check_usbchargernotok_work(struct work_struct *work)
  2247. {
  2248. int ret;
  2249. u8 reg_value;
  2250. bool prev_status;
  2251. struct ab8500_charger *di = container_of(work,
  2252. struct ab8500_charger, check_usbchgnotok_work.work);
  2253. /* Check if the status bit for usbchargernotok is still active */
  2254. ret = abx500_get_register_interruptible(di->dev,
  2255. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
  2256. if (ret < 0) {
  2257. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  2258. return;
  2259. }
  2260. prev_status = di->flags.usbchargernotok;
  2261. if (reg_value & VBUS_CH_NOK) {
  2262. di->flags.usbchargernotok = true;
  2263. /* Check again in 1sec */
  2264. queue_delayed_work(di->charger_wq,
  2265. &di->check_usbchgnotok_work, HZ);
  2266. } else {
  2267. di->flags.usbchargernotok = false;
  2268. di->flags.vbus_collapse = false;
  2269. }
  2270. if (prev_status != di->flags.usbchargernotok)
  2271. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2272. }
  2273. /**
  2274. * ab8500_charger_check_main_thermal_prot_work() - check main thermal status
  2275. * @work: pointer to the work_struct structure
  2276. *
  2277. * Work queue function for checking the Main thermal prot status
  2278. */
  2279. static void ab8500_charger_check_main_thermal_prot_work(
  2280. struct work_struct *work)
  2281. {
  2282. int ret;
  2283. u8 reg_value;
  2284. struct ab8500_charger *di = container_of(work,
  2285. struct ab8500_charger, check_main_thermal_prot_work);
  2286. /* Check if the status bit for main_thermal_prot is still active */
  2287. ret = abx500_get_register_interruptible(di->dev,
  2288. AB8500_CHARGER, AB8500_CH_STATUS2_REG, &reg_value);
  2289. if (ret < 0) {
  2290. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  2291. return;
  2292. }
  2293. if (reg_value & MAIN_CH_TH_PROT)
  2294. di->flags.main_thermal_prot = true;
  2295. else
  2296. di->flags.main_thermal_prot = false;
  2297. ab8500_power_supply_changed(di, di->ac_chg.psy);
  2298. }
  2299. /**
  2300. * ab8500_charger_check_usb_thermal_prot_work() - check usb thermal status
  2301. * @work: pointer to the work_struct structure
  2302. *
  2303. * Work queue function for checking the USB thermal prot status
  2304. */
  2305. static void ab8500_charger_check_usb_thermal_prot_work(
  2306. struct work_struct *work)
  2307. {
  2308. int ret;
  2309. u8 reg_value;
  2310. struct ab8500_charger *di = container_of(work,
  2311. struct ab8500_charger, check_usb_thermal_prot_work);
  2312. /* Check if the status bit for usb_thermal_prot is still active */
  2313. ret = abx500_get_register_interruptible(di->dev,
  2314. AB8500_CHARGER, AB8500_CH_USBCH_STAT2_REG, &reg_value);
  2315. if (ret < 0) {
  2316. dev_err(di->dev, "%s ab8500 read failed\n", __func__);
  2317. return;
  2318. }
  2319. if (reg_value & USB_CH_TH_PROT)
  2320. di->flags.usb_thermal_prot = true;
  2321. else
  2322. di->flags.usb_thermal_prot = false;
  2323. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2324. }
  2325. /**
  2326. * ab8500_charger_mainchunplugdet_handler() - main charger unplugged
  2327. * @irq: interrupt number
  2328. * @_di: pointer to the ab8500_charger structure
  2329. *
  2330. * Returns IRQ status(IRQ_HANDLED)
  2331. */
  2332. static irqreturn_t ab8500_charger_mainchunplugdet_handler(int irq, void *_di)
  2333. {
  2334. struct ab8500_charger *di = _di;
  2335. dev_dbg(di->dev, "Main charger unplugged\n");
  2336. queue_work(di->charger_wq, &di->ac_work);
  2337. cancel_delayed_work_sync(&di->ac_charger_attached_work);
  2338. mutex_lock(&di->charger_attached_mutex);
  2339. mutex_unlock(&di->charger_attached_mutex);
  2340. return IRQ_HANDLED;
  2341. }
  2342. /**
  2343. * ab8500_charger_mainchplugdet_handler() - main charger plugged
  2344. * @irq: interrupt number
  2345. * @_di: pointer to the ab8500_charger structure
  2346. *
  2347. * Returns IRQ status(IRQ_HANDLED)
  2348. */
  2349. static irqreturn_t ab8500_charger_mainchplugdet_handler(int irq, void *_di)
  2350. {
  2351. struct ab8500_charger *di = _di;
  2352. dev_dbg(di->dev, "Main charger plugged\n");
  2353. queue_work(di->charger_wq, &di->ac_work);
  2354. mutex_lock(&di->charger_attached_mutex);
  2355. mutex_unlock(&di->charger_attached_mutex);
  2356. if (is_ab8500(di->parent))
  2357. queue_delayed_work(di->charger_wq,
  2358. &di->ac_charger_attached_work,
  2359. HZ);
  2360. return IRQ_HANDLED;
  2361. }
  2362. /**
  2363. * ab8500_charger_mainextchnotok_handler() - main charger not ok
  2364. * @irq: interrupt number
  2365. * @_di: pointer to the ab8500_charger structure
  2366. *
  2367. * Returns IRQ status(IRQ_HANDLED)
  2368. */
  2369. static irqreturn_t ab8500_charger_mainextchnotok_handler(int irq, void *_di)
  2370. {
  2371. struct ab8500_charger *di = _di;
  2372. dev_dbg(di->dev, "Main charger not ok\n");
  2373. di->flags.mainextchnotok = true;
  2374. ab8500_power_supply_changed(di, di->ac_chg.psy);
  2375. /* Schedule a new HW failure check */
  2376. queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
  2377. return IRQ_HANDLED;
  2378. }
  2379. /**
  2380. * ab8500_charger_mainchthprotr_handler() - Die temp is above main charger
  2381. * thermal protection threshold
  2382. * @irq: interrupt number
  2383. * @_di: pointer to the ab8500_charger structure
  2384. *
  2385. * Returns IRQ status(IRQ_HANDLED)
  2386. */
  2387. static irqreturn_t ab8500_charger_mainchthprotr_handler(int irq, void *_di)
  2388. {
  2389. struct ab8500_charger *di = _di;
  2390. dev_dbg(di->dev,
  2391. "Die temp above Main charger thermal protection threshold\n");
  2392. queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
  2393. return IRQ_HANDLED;
  2394. }
  2395. /**
  2396. * ab8500_charger_mainchthprotf_handler() - Die temp is below main charger
  2397. * thermal protection threshold
  2398. * @irq: interrupt number
  2399. * @_di: pointer to the ab8500_charger structure
  2400. *
  2401. * Returns IRQ status(IRQ_HANDLED)
  2402. */
  2403. static irqreturn_t ab8500_charger_mainchthprotf_handler(int irq, void *_di)
  2404. {
  2405. struct ab8500_charger *di = _di;
  2406. dev_dbg(di->dev,
  2407. "Die temp ok for Main charger thermal protection threshold\n");
  2408. queue_work(di->charger_wq, &di->check_main_thermal_prot_work);
  2409. return IRQ_HANDLED;
  2410. }
  2411. static void ab8500_charger_vbus_drop_end_work(struct work_struct *work)
  2412. {
  2413. struct ab8500_charger *di = container_of(work,
  2414. struct ab8500_charger, vbus_drop_end_work.work);
  2415. int ret, curr;
  2416. u8 reg_value;
  2417. di->flags.vbus_drop_end = false;
  2418. /* Reset the drop counter */
  2419. abx500_set_register_interruptible(di->dev,
  2420. AB8500_CHARGER, AB8500_CHARGER_CTRL, 0x01);
  2421. if (is_ab8540(di->parent))
  2422. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  2423. AB8540_CH_USBCH_STAT3_REG, &reg_value);
  2424. else
  2425. ret = abx500_get_register_interruptible(di->dev, AB8500_CHARGER,
  2426. AB8500_CH_USBCH_STAT2_REG, &reg_value);
  2427. if (ret < 0) {
  2428. dev_err(di->dev, "%s read failed\n", __func__);
  2429. return;
  2430. }
  2431. if (is_ab8540(di->parent))
  2432. curr = di->bm->chg_input_curr[
  2433. reg_value & AB8540_AUTO_VBUS_IN_CURR_MASK];
  2434. else
  2435. curr = di->bm->chg_input_curr[
  2436. reg_value >> AUTO_VBUS_IN_CURR_LIM_SHIFT];
  2437. if (di->max_usb_in_curr.calculated_max != curr) {
  2438. /* USB source is collapsing */
  2439. di->max_usb_in_curr.calculated_max = curr;
  2440. dev_dbg(di->dev,
  2441. "VBUS input current limiting to %d mA\n",
  2442. di->max_usb_in_curr.calculated_max);
  2443. } else {
  2444. /*
  2445. * USB source can not give more than this amount.
  2446. * Taking more will collapse the source.
  2447. */
  2448. di->max_usb_in_curr.set_max =
  2449. di->max_usb_in_curr.calculated_max;
  2450. dev_dbg(di->dev,
  2451. "VBUS input current limited to %d mA\n",
  2452. di->max_usb_in_curr.set_max);
  2453. }
  2454. if (di->usb.charger_connected)
  2455. ab8500_charger_set_vbus_in_curr(di,
  2456. di->max_usb_in_curr.usb_type_max);
  2457. }
  2458. /**
  2459. * ab8500_charger_vbusdetf_handler() - VBUS falling detected
  2460. * @irq: interrupt number
  2461. * @_di: pointer to the ab8500_charger structure
  2462. *
  2463. * Returns IRQ status(IRQ_HANDLED)
  2464. */
  2465. static irqreturn_t ab8500_charger_vbusdetf_handler(int irq, void *_di)
  2466. {
  2467. struct ab8500_charger *di = _di;
  2468. di->vbus_detected = false;
  2469. dev_dbg(di->dev, "VBUS falling detected\n");
  2470. queue_work(di->charger_wq, &di->detect_usb_type_work);
  2471. return IRQ_HANDLED;
  2472. }
  2473. /**
  2474. * ab8500_charger_vbusdetr_handler() - VBUS rising detected
  2475. * @irq: interrupt number
  2476. * @_di: pointer to the ab8500_charger structure
  2477. *
  2478. * Returns IRQ status(IRQ_HANDLED)
  2479. */
  2480. static irqreturn_t ab8500_charger_vbusdetr_handler(int irq, void *_di)
  2481. {
  2482. struct ab8500_charger *di = _di;
  2483. di->vbus_detected = true;
  2484. dev_dbg(di->dev, "VBUS rising detected\n");
  2485. queue_work(di->charger_wq, &di->detect_usb_type_work);
  2486. return IRQ_HANDLED;
  2487. }
  2488. /**
  2489. * ab8500_charger_usblinkstatus_handler() - USB link status has changed
  2490. * @irq: interrupt number
  2491. * @_di: pointer to the ab8500_charger structure
  2492. *
  2493. * Returns IRQ status(IRQ_HANDLED)
  2494. */
  2495. static irqreturn_t ab8500_charger_usblinkstatus_handler(int irq, void *_di)
  2496. {
  2497. struct ab8500_charger *di = _di;
  2498. dev_dbg(di->dev, "USB link status changed\n");
  2499. queue_work(di->charger_wq, &di->usb_link_status_work);
  2500. return IRQ_HANDLED;
  2501. }
  2502. /**
  2503. * ab8500_charger_usbchthprotr_handler() - Die temp is above usb charger
  2504. * thermal protection threshold
  2505. * @irq: interrupt number
  2506. * @_di: pointer to the ab8500_charger structure
  2507. *
  2508. * Returns IRQ status(IRQ_HANDLED)
  2509. */
  2510. static irqreturn_t ab8500_charger_usbchthprotr_handler(int irq, void *_di)
  2511. {
  2512. struct ab8500_charger *di = _di;
  2513. dev_dbg(di->dev,
  2514. "Die temp above USB charger thermal protection threshold\n");
  2515. queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
  2516. return IRQ_HANDLED;
  2517. }
  2518. /**
  2519. * ab8500_charger_usbchthprotf_handler() - Die temp is below usb charger
  2520. * thermal protection threshold
  2521. * @irq: interrupt number
  2522. * @_di: pointer to the ab8500_charger structure
  2523. *
  2524. * Returns IRQ status(IRQ_HANDLED)
  2525. */
  2526. static irqreturn_t ab8500_charger_usbchthprotf_handler(int irq, void *_di)
  2527. {
  2528. struct ab8500_charger *di = _di;
  2529. dev_dbg(di->dev,
  2530. "Die temp ok for USB charger thermal protection threshold\n");
  2531. queue_work(di->charger_wq, &di->check_usb_thermal_prot_work);
  2532. return IRQ_HANDLED;
  2533. }
  2534. /**
  2535. * ab8500_charger_usbchargernotokr_handler() - USB charger not ok detected
  2536. * @irq: interrupt number
  2537. * @_di: pointer to the ab8500_charger structure
  2538. *
  2539. * Returns IRQ status(IRQ_HANDLED)
  2540. */
  2541. static irqreturn_t ab8500_charger_usbchargernotokr_handler(int irq, void *_di)
  2542. {
  2543. struct ab8500_charger *di = _di;
  2544. dev_dbg(di->dev, "Not allowed USB charger detected\n");
  2545. queue_delayed_work(di->charger_wq, &di->check_usbchgnotok_work, 0);
  2546. return IRQ_HANDLED;
  2547. }
  2548. /**
  2549. * ab8500_charger_chwdexp_handler() - Charger watchdog expired
  2550. * @irq: interrupt number
  2551. * @_di: pointer to the ab8500_charger structure
  2552. *
  2553. * Returns IRQ status(IRQ_HANDLED)
  2554. */
  2555. static irqreturn_t ab8500_charger_chwdexp_handler(int irq, void *_di)
  2556. {
  2557. struct ab8500_charger *di = _di;
  2558. dev_dbg(di->dev, "Charger watchdog expired\n");
  2559. /*
  2560. * The charger that was online when the watchdog expired
  2561. * needs to be restarted for charging to start again
  2562. */
  2563. if (di->ac.charger_online) {
  2564. di->ac.wd_expired = true;
  2565. ab8500_power_supply_changed(di, di->ac_chg.psy);
  2566. }
  2567. if (di->usb.charger_online) {
  2568. di->usb.wd_expired = true;
  2569. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2570. }
  2571. return IRQ_HANDLED;
  2572. }
  2573. /**
  2574. * ab8500_charger_vbuschdropend_handler() - VBUS drop removed
  2575. * @irq: interrupt number
  2576. * @_di: pointer to the ab8500_charger structure
  2577. *
  2578. * Returns IRQ status(IRQ_HANDLED)
  2579. */
  2580. static irqreturn_t ab8500_charger_vbuschdropend_handler(int irq, void *_di)
  2581. {
  2582. struct ab8500_charger *di = _di;
  2583. dev_dbg(di->dev, "VBUS charger drop ended\n");
  2584. di->flags.vbus_drop_end = true;
  2585. /*
  2586. * VBUS might have dropped due to bad connection.
  2587. * Schedule a new input limit set to the value SW requests.
  2588. */
  2589. queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work,
  2590. round_jiffies(VBUS_IN_CURR_LIM_RETRY_SET_TIME * HZ));
  2591. return IRQ_HANDLED;
  2592. }
  2593. /**
  2594. * ab8500_charger_vbusovv_handler() - VBUS overvoltage detected
  2595. * @irq: interrupt number
  2596. * @_di: pointer to the ab8500_charger structure
  2597. *
  2598. * Returns IRQ status(IRQ_HANDLED)
  2599. */
  2600. static irqreturn_t ab8500_charger_vbusovv_handler(int irq, void *_di)
  2601. {
  2602. struct ab8500_charger *di = _di;
  2603. dev_dbg(di->dev, "VBUS overvoltage detected\n");
  2604. di->flags.vbus_ovv = true;
  2605. ab8500_power_supply_changed(di, di->usb_chg.psy);
  2606. /* Schedule a new HW failure check */
  2607. queue_delayed_work(di->charger_wq, &di->check_hw_failure_work, 0);
  2608. return IRQ_HANDLED;
  2609. }
  2610. /**
  2611. * ab8500_charger_ac_get_property() - get the ac/mains properties
  2612. * @psy: pointer to the power_supply structure
  2613. * @psp: pointer to the power_supply_property structure
  2614. * @val: pointer to the power_supply_propval union
  2615. *
  2616. * This function gets called when an application tries to get the ac/mains
  2617. * properties by reading the sysfs files.
  2618. * AC/Mains properties are online, present and voltage.
  2619. * online: ac/mains charging is in progress or not
  2620. * present: presence of the ac/mains
  2621. * voltage: AC/Mains voltage
  2622. * Returns error code in case of failure else 0(on success)
  2623. */
  2624. static int ab8500_charger_ac_get_property(struct power_supply *psy,
  2625. enum power_supply_property psp,
  2626. union power_supply_propval *val)
  2627. {
  2628. struct ab8500_charger *di;
  2629. int ret;
  2630. di = to_ab8500_charger_ac_device_info(psy_to_ux500_charger(psy));
  2631. switch (psp) {
  2632. case POWER_SUPPLY_PROP_HEALTH:
  2633. if (di->flags.mainextchnotok)
  2634. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  2635. else if (di->ac.wd_expired || di->usb.wd_expired)
  2636. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  2637. else if (di->flags.main_thermal_prot)
  2638. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  2639. else
  2640. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  2641. break;
  2642. case POWER_SUPPLY_PROP_ONLINE:
  2643. val->intval = di->ac.charger_online;
  2644. break;
  2645. case POWER_SUPPLY_PROP_PRESENT:
  2646. val->intval = di->ac.charger_connected;
  2647. break;
  2648. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2649. ret = ab8500_charger_get_ac_voltage(di);
  2650. if (ret >= 0)
  2651. di->ac.charger_voltage = ret;
  2652. /* On error, use previous value */
  2653. val->intval = di->ac.charger_voltage * 1000;
  2654. break;
  2655. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  2656. /*
  2657. * This property is used to indicate when CV mode is entered
  2658. * for the AC charger
  2659. */
  2660. di->ac.cv_active = ab8500_charger_ac_cv(di);
  2661. val->intval = di->ac.cv_active;
  2662. break;
  2663. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2664. ret = ab8500_charger_get_ac_current(di);
  2665. if (ret >= 0)
  2666. di->ac.charger_current = ret;
  2667. val->intval = di->ac.charger_current * 1000;
  2668. break;
  2669. default:
  2670. return -EINVAL;
  2671. }
  2672. return 0;
  2673. }
  2674. /**
  2675. * ab8500_charger_usb_get_property() - get the usb properties
  2676. * @psy: pointer to the power_supply structure
  2677. * @psp: pointer to the power_supply_property structure
  2678. * @val: pointer to the power_supply_propval union
  2679. *
  2680. * This function gets called when an application tries to get the usb
  2681. * properties by reading the sysfs files.
  2682. * USB properties are online, present and voltage.
  2683. * online: usb charging is in progress or not
  2684. * present: presence of the usb
  2685. * voltage: vbus voltage
  2686. * Returns error code in case of failure else 0(on success)
  2687. */
  2688. static int ab8500_charger_usb_get_property(struct power_supply *psy,
  2689. enum power_supply_property psp,
  2690. union power_supply_propval *val)
  2691. {
  2692. struct ab8500_charger *di;
  2693. int ret;
  2694. di = to_ab8500_charger_usb_device_info(psy_to_ux500_charger(psy));
  2695. switch (psp) {
  2696. case POWER_SUPPLY_PROP_HEALTH:
  2697. if (di->flags.usbchargernotok)
  2698. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  2699. else if (di->ac.wd_expired || di->usb.wd_expired)
  2700. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  2701. else if (di->flags.usb_thermal_prot)
  2702. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  2703. else if (di->flags.vbus_ovv)
  2704. val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
  2705. else
  2706. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  2707. break;
  2708. case POWER_SUPPLY_PROP_ONLINE:
  2709. val->intval = di->usb.charger_online;
  2710. break;
  2711. case POWER_SUPPLY_PROP_PRESENT:
  2712. val->intval = di->usb.charger_connected;
  2713. break;
  2714. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  2715. ret = ab8500_charger_get_vbus_voltage(di);
  2716. if (ret >= 0)
  2717. di->usb.charger_voltage = ret;
  2718. val->intval = di->usb.charger_voltage * 1000;
  2719. break;
  2720. case POWER_SUPPLY_PROP_VOLTAGE_AVG:
  2721. /*
  2722. * This property is used to indicate when CV mode is entered
  2723. * for the USB charger
  2724. */
  2725. di->usb.cv_active = ab8500_charger_usb_cv(di);
  2726. val->intval = di->usb.cv_active;
  2727. break;
  2728. case POWER_SUPPLY_PROP_CURRENT_NOW:
  2729. ret = ab8500_charger_get_usb_current(di);
  2730. if (ret >= 0)
  2731. di->usb.charger_current = ret;
  2732. val->intval = di->usb.charger_current * 1000;
  2733. break;
  2734. case POWER_SUPPLY_PROP_CURRENT_AVG:
  2735. /*
  2736. * This property is used to indicate when VBUS has collapsed
  2737. * due to too high output current from the USB charger
  2738. */
  2739. if (di->flags.vbus_collapse)
  2740. val->intval = 1;
  2741. else
  2742. val->intval = 0;
  2743. break;
  2744. default:
  2745. return -EINVAL;
  2746. }
  2747. return 0;
  2748. }
  2749. /**
  2750. * ab8500_charger_init_hw_registers() - Set up charger related registers
  2751. * @di: pointer to the ab8500_charger structure
  2752. *
  2753. * Set up charger OVV, watchdog and maximum voltage registers as well as
  2754. * charging of the backup battery
  2755. */
  2756. static int ab8500_charger_init_hw_registers(struct ab8500_charger *di)
  2757. {
  2758. int ret = 0;
  2759. u8 bup_vch_range = 0, vbup33_vrtcn = 0;
  2760. /* Setup maximum charger current and voltage for ABB cut2.0 */
  2761. if (!is_ab8500_1p1_or_earlier(di->parent)) {
  2762. ret = abx500_set_register_interruptible(di->dev,
  2763. AB8500_CHARGER,
  2764. AB8500_CH_VOLT_LVL_MAX_REG, CH_VOL_LVL_4P6);
  2765. if (ret) {
  2766. dev_err(di->dev,
  2767. "failed to set CH_VOLT_LVL_MAX_REG\n");
  2768. goto out;
  2769. }
  2770. if (is_ab8540(di->parent))
  2771. ret = abx500_set_register_interruptible(di->dev,
  2772. AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
  2773. CH_OP_CUR_LVL_2P);
  2774. else
  2775. ret = abx500_set_register_interruptible(di->dev,
  2776. AB8500_CHARGER, AB8500_CH_OPT_CRNTLVL_MAX_REG,
  2777. CH_OP_CUR_LVL_1P6);
  2778. if (ret) {
  2779. dev_err(di->dev,
  2780. "failed to set CH_OPT_CRNTLVL_MAX_REG\n");
  2781. goto out;
  2782. }
  2783. }
  2784. if (is_ab9540_2p0(di->parent) || is_ab9540_3p0(di->parent)
  2785. || is_ab8505_2p0(di->parent) || is_ab8540(di->parent))
  2786. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2787. AB8500_CHARGER,
  2788. AB8500_USBCH_CTRL2_REG,
  2789. VBUS_AUTO_IN_CURR_LIM_ENA,
  2790. VBUS_AUTO_IN_CURR_LIM_ENA);
  2791. else
  2792. /*
  2793. * VBUS OVV set to 6.3V and enable automatic current limitation
  2794. */
  2795. ret = abx500_set_register_interruptible(di->dev,
  2796. AB8500_CHARGER,
  2797. AB8500_USBCH_CTRL2_REG,
  2798. VBUS_OVV_SELECT_6P3V | VBUS_AUTO_IN_CURR_LIM_ENA);
  2799. if (ret) {
  2800. dev_err(di->dev,
  2801. "failed to set automatic current limitation\n");
  2802. goto out;
  2803. }
  2804. /* Enable main watchdog in OTP */
  2805. ret = abx500_set_register_interruptible(di->dev,
  2806. AB8500_OTP_EMUL, AB8500_OTP_CONF_15, OTP_ENABLE_WD);
  2807. if (ret) {
  2808. dev_err(di->dev, "failed to enable main WD in OTP\n");
  2809. goto out;
  2810. }
  2811. /* Enable main watchdog */
  2812. ret = abx500_set_register_interruptible(di->dev,
  2813. AB8500_SYS_CTRL2_BLOCK,
  2814. AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_ENA);
  2815. if (ret) {
  2816. dev_err(di->dev, "faile to enable main watchdog\n");
  2817. goto out;
  2818. }
  2819. /*
  2820. * Due to internal synchronisation, Enable and Kick watchdog bits
  2821. * cannot be enabled in a single write.
  2822. * A minimum delay of 2*32 kHz period (62.5µs) must be inserted
  2823. * between writing Enable then Kick bits.
  2824. */
  2825. udelay(63);
  2826. /* Kick main watchdog */
  2827. ret = abx500_set_register_interruptible(di->dev,
  2828. AB8500_SYS_CTRL2_BLOCK,
  2829. AB8500_MAIN_WDOG_CTRL_REG,
  2830. (MAIN_WDOG_ENA | MAIN_WDOG_KICK));
  2831. if (ret) {
  2832. dev_err(di->dev, "failed to kick main watchdog\n");
  2833. goto out;
  2834. }
  2835. /* Disable main watchdog */
  2836. ret = abx500_set_register_interruptible(di->dev,
  2837. AB8500_SYS_CTRL2_BLOCK,
  2838. AB8500_MAIN_WDOG_CTRL_REG, MAIN_WDOG_DIS);
  2839. if (ret) {
  2840. dev_err(di->dev, "failed to disable main watchdog\n");
  2841. goto out;
  2842. }
  2843. /* Set watchdog timeout */
  2844. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  2845. AB8500_CH_WD_TIMER_REG, WD_TIMER);
  2846. if (ret) {
  2847. dev_err(di->dev, "failed to set charger watchdog timeout\n");
  2848. goto out;
  2849. }
  2850. ret = ab8500_charger_led_en(di, false);
  2851. if (ret < 0) {
  2852. dev_err(di->dev, "failed to disable LED\n");
  2853. goto out;
  2854. }
  2855. /* Backup battery voltage and current */
  2856. if (di->bm->bkup_bat_v > BUP_VCH_SEL_3P1V)
  2857. bup_vch_range = BUP_VCH_RANGE;
  2858. if (di->bm->bkup_bat_v == BUP_VCH_SEL_3P3V)
  2859. vbup33_vrtcn = VBUP33_VRTCN;
  2860. ret = abx500_set_register_interruptible(di->dev,
  2861. AB8500_RTC,
  2862. AB8500_RTC_BACKUP_CHG_REG,
  2863. (di->bm->bkup_bat_v & 0x3) | di->bm->bkup_bat_i);
  2864. if (ret) {
  2865. dev_err(di->dev, "failed to setup backup battery charging\n");
  2866. goto out;
  2867. }
  2868. if (is_ab8540(di->parent)) {
  2869. ret = abx500_set_register_interruptible(di->dev,
  2870. AB8500_RTC,
  2871. AB8500_RTC_CTRL1_REG,
  2872. bup_vch_range | vbup33_vrtcn);
  2873. if (ret) {
  2874. dev_err(di->dev,
  2875. "failed to setup backup battery charging\n");
  2876. goto out;
  2877. }
  2878. }
  2879. /* Enable backup battery charging */
  2880. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2881. AB8500_RTC, AB8500_RTC_CTRL_REG,
  2882. RTC_BUP_CH_ENA, RTC_BUP_CH_ENA);
  2883. if (ret < 0) {
  2884. dev_err(di->dev, "%s mask and set failed\n", __func__);
  2885. goto out;
  2886. }
  2887. if (is_ab8540(di->parent)) {
  2888. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2889. AB8500_CHARGER, AB8540_USB_PP_MODE_REG,
  2890. BUS_VSYS_VOL_SELECT_MASK, BUS_VSYS_VOL_SELECT_3P6V);
  2891. if (ret) {
  2892. dev_err(di->dev,
  2893. "failed to setup usb power path vsys voltage\n");
  2894. goto out;
  2895. }
  2896. ret = abx500_mask_and_set_register_interruptible(di->dev,
  2897. AB8500_CHARGER, AB8540_USB_PP_CHR_REG,
  2898. BUS_PP_PRECHG_CURRENT_MASK, 0);
  2899. if (ret) {
  2900. dev_err(di->dev,
  2901. "failed to setup usb power path precharge current\n");
  2902. goto out;
  2903. }
  2904. }
  2905. out:
  2906. return ret;
  2907. }
  2908. /*
  2909. * ab8500 charger driver interrupts and their respective isr
  2910. */
  2911. static struct ab8500_charger_interrupts ab8500_charger_irq[] = {
  2912. {"MAIN_CH_UNPLUG_DET", ab8500_charger_mainchunplugdet_handler},
  2913. {"MAIN_CHARGE_PLUG_DET", ab8500_charger_mainchplugdet_handler},
  2914. {"MAIN_EXT_CH_NOT_OK", ab8500_charger_mainextchnotok_handler},
  2915. {"MAIN_CH_TH_PROT_R", ab8500_charger_mainchthprotr_handler},
  2916. {"MAIN_CH_TH_PROT_F", ab8500_charger_mainchthprotf_handler},
  2917. {"VBUS_DET_F", ab8500_charger_vbusdetf_handler},
  2918. {"VBUS_DET_R", ab8500_charger_vbusdetr_handler},
  2919. {"USB_LINK_STATUS", ab8500_charger_usblinkstatus_handler},
  2920. {"USB_CH_TH_PROT_R", ab8500_charger_usbchthprotr_handler},
  2921. {"USB_CH_TH_PROT_F", ab8500_charger_usbchthprotf_handler},
  2922. {"USB_CHARGER_NOT_OKR", ab8500_charger_usbchargernotokr_handler},
  2923. {"VBUS_OVV", ab8500_charger_vbusovv_handler},
  2924. {"CH_WD_EXP", ab8500_charger_chwdexp_handler},
  2925. {"VBUS_CH_DROP_END", ab8500_charger_vbuschdropend_handler},
  2926. };
  2927. static int ab8500_charger_usb_notifier_call(struct notifier_block *nb,
  2928. unsigned long event, void *power)
  2929. {
  2930. struct ab8500_charger *di =
  2931. container_of(nb, struct ab8500_charger, nb);
  2932. enum ab8500_usb_state bm_usb_state;
  2933. unsigned mA = *((unsigned *)power);
  2934. if (!di)
  2935. return NOTIFY_DONE;
  2936. if (event != USB_EVENT_VBUS) {
  2937. dev_dbg(di->dev, "not a standard host, returning\n");
  2938. return NOTIFY_DONE;
  2939. }
  2940. /* TODO: State is fabricate here. See if charger really needs USB
  2941. * state or if mA is enough
  2942. */
  2943. if ((di->usb_state.usb_current == 2) && (mA > 2))
  2944. bm_usb_state = AB8500_BM_USB_STATE_RESUME;
  2945. else if (mA == 0)
  2946. bm_usb_state = AB8500_BM_USB_STATE_RESET_HS;
  2947. else if (mA == 2)
  2948. bm_usb_state = AB8500_BM_USB_STATE_SUSPEND;
  2949. else if (mA >= 8) /* 8, 100, 500 */
  2950. bm_usb_state = AB8500_BM_USB_STATE_CONFIGURED;
  2951. else /* Should never occur */
  2952. bm_usb_state = AB8500_BM_USB_STATE_RESET_FS;
  2953. dev_dbg(di->dev, "%s usb_state: 0x%02x mA: %d\n",
  2954. __func__, bm_usb_state, mA);
  2955. spin_lock(&di->usb_state.usb_lock);
  2956. di->usb_state.state_tmp = bm_usb_state;
  2957. di->usb_state.usb_current_tmp = mA;
  2958. spin_unlock(&di->usb_state.usb_lock);
  2959. /*
  2960. * wait for some time until you get updates from the usb stack
  2961. * and negotiations are completed
  2962. */
  2963. queue_delayed_work(di->charger_wq, &di->usb_state_changed_work, HZ/2);
  2964. return NOTIFY_OK;
  2965. }
  2966. #if defined(CONFIG_PM)
  2967. static int ab8500_charger_resume(struct platform_device *pdev)
  2968. {
  2969. int ret;
  2970. struct ab8500_charger *di = platform_get_drvdata(pdev);
  2971. /*
  2972. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  2973. * logic. That means we have to continously kick the charger
  2974. * watchdog even when no charger is connected. This is only
  2975. * valid once the AC charger has been enabled. This is
  2976. * a bug that is not handled by the algorithm and the
  2977. * watchdog have to be kicked by the charger driver
  2978. * when the AC charger is disabled
  2979. */
  2980. if (di->ac_conn && is_ab8500_1p1_or_earlier(di->parent)) {
  2981. ret = abx500_set_register_interruptible(di->dev, AB8500_CHARGER,
  2982. AB8500_CHARG_WD_CTRL, CHARG_WD_KICK);
  2983. if (ret)
  2984. dev_err(di->dev, "Failed to kick WD!\n");
  2985. /* If not already pending start a new timer */
  2986. queue_delayed_work(di->charger_wq, &di->kick_wd_work,
  2987. round_jiffies(WD_KICK_INTERVAL));
  2988. }
  2989. /* If we still have a HW failure, schedule a new check */
  2990. if (di->flags.mainextchnotok || di->flags.vbus_ovv) {
  2991. queue_delayed_work(di->charger_wq,
  2992. &di->check_hw_failure_work, 0);
  2993. }
  2994. if (di->flags.vbus_drop_end)
  2995. queue_delayed_work(di->charger_wq, &di->vbus_drop_end_work, 0);
  2996. return 0;
  2997. }
  2998. static int ab8500_charger_suspend(struct platform_device *pdev,
  2999. pm_message_t state)
  3000. {
  3001. struct ab8500_charger *di = platform_get_drvdata(pdev);
  3002. /* Cancel any pending jobs */
  3003. cancel_delayed_work(&di->check_hw_failure_work);
  3004. cancel_delayed_work(&di->vbus_drop_end_work);
  3005. flush_delayed_work(&di->attach_work);
  3006. flush_delayed_work(&di->usb_charger_attached_work);
  3007. flush_delayed_work(&di->ac_charger_attached_work);
  3008. flush_delayed_work(&di->check_usbchgnotok_work);
  3009. flush_delayed_work(&di->check_vbat_work);
  3010. flush_delayed_work(&di->kick_wd_work);
  3011. flush_work(&di->usb_link_status_work);
  3012. flush_work(&di->ac_work);
  3013. flush_work(&di->detect_usb_type_work);
  3014. if (atomic_read(&di->current_stepping_sessions))
  3015. return -EAGAIN;
  3016. return 0;
  3017. }
  3018. #else
  3019. #define ab8500_charger_suspend NULL
  3020. #define ab8500_charger_resume NULL
  3021. #endif
  3022. static struct notifier_block charger_nb = {
  3023. .notifier_call = ab8500_external_charger_prepare,
  3024. };
  3025. static int ab8500_charger_remove(struct platform_device *pdev)
  3026. {
  3027. struct ab8500_charger *di = platform_get_drvdata(pdev);
  3028. int i, irq, ret;
  3029. /* Disable AC charging */
  3030. ab8500_charger_ac_en(&di->ac_chg, false, 0, 0);
  3031. /* Disable USB charging */
  3032. ab8500_charger_usb_en(&di->usb_chg, false, 0, 0);
  3033. /* Disable interrupts */
  3034. for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
  3035. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  3036. free_irq(irq, di);
  3037. }
  3038. /* Backup battery voltage and current disable */
  3039. ret = abx500_mask_and_set_register_interruptible(di->dev,
  3040. AB8500_RTC, AB8500_RTC_CTRL_REG, RTC_BUP_CH_ENA, 0);
  3041. if (ret < 0)
  3042. dev_err(di->dev, "%s mask and set failed\n", __func__);
  3043. usb_unregister_notifier(di->usb_phy, &di->nb);
  3044. usb_put_phy(di->usb_phy);
  3045. /* Delete the work queue */
  3046. destroy_workqueue(di->charger_wq);
  3047. /* Unregister external charger enable notifier */
  3048. if (!di->ac_chg.enabled)
  3049. blocking_notifier_chain_unregister(
  3050. &charger_notifier_list, &charger_nb);
  3051. flush_scheduled_work();
  3052. if (di->usb_chg.enabled)
  3053. power_supply_unregister(di->usb_chg.psy);
  3054. if (di->ac_chg.enabled && !di->ac_chg.external)
  3055. power_supply_unregister(di->ac_chg.psy);
  3056. return 0;
  3057. }
  3058. static char *supply_interface[] = {
  3059. "ab8500_chargalg",
  3060. "ab8500_fg",
  3061. "ab8500_btemp",
  3062. };
  3063. static const struct power_supply_desc ab8500_ac_chg_desc = {
  3064. .name = "ab8500_ac",
  3065. .type = POWER_SUPPLY_TYPE_MAINS,
  3066. .properties = ab8500_charger_ac_props,
  3067. .num_properties = ARRAY_SIZE(ab8500_charger_ac_props),
  3068. .get_property = ab8500_charger_ac_get_property,
  3069. };
  3070. static const struct power_supply_desc ab8500_usb_chg_desc = {
  3071. .name = "ab8500_usb",
  3072. .type = POWER_SUPPLY_TYPE_USB,
  3073. .properties = ab8500_charger_usb_props,
  3074. .num_properties = ARRAY_SIZE(ab8500_charger_usb_props),
  3075. .get_property = ab8500_charger_usb_get_property,
  3076. };
  3077. static int ab8500_charger_probe(struct platform_device *pdev)
  3078. {
  3079. struct device_node *np = pdev->dev.of_node;
  3080. struct abx500_bm_data *plat = pdev->dev.platform_data;
  3081. struct power_supply_config ac_psy_cfg = {}, usb_psy_cfg = {};
  3082. struct ab8500_charger *di;
  3083. int irq, i, charger_status, ret = 0, ch_stat;
  3084. di = devm_kzalloc(&pdev->dev, sizeof(*di), GFP_KERNEL);
  3085. if (!di) {
  3086. dev_err(&pdev->dev, "%s no mem for ab8500_charger\n", __func__);
  3087. return -ENOMEM;
  3088. }
  3089. if (!plat) {
  3090. dev_err(&pdev->dev, "no battery management data supplied\n");
  3091. return -EINVAL;
  3092. }
  3093. di->bm = plat;
  3094. if (np) {
  3095. ret = ab8500_bm_of_probe(&pdev->dev, np, di->bm);
  3096. if (ret) {
  3097. dev_err(&pdev->dev, "failed to get battery information\n");
  3098. return ret;
  3099. }
  3100. di->autopower_cfg = of_property_read_bool(np, "autopower_cfg");
  3101. } else
  3102. di->autopower_cfg = false;
  3103. /* get parent data */
  3104. di->dev = &pdev->dev;
  3105. di->parent = dev_get_drvdata(pdev->dev.parent);
  3106. di->gpadc = ab8500_gpadc_get("ab8500-gpadc.0");
  3107. /* initialize lock */
  3108. spin_lock_init(&di->usb_state.usb_lock);
  3109. mutex_init(&di->usb_ipt_crnt_lock);
  3110. di->autopower = false;
  3111. di->invalid_charger_detect_state = 0;
  3112. /* AC and USB supply config */
  3113. ac_psy_cfg.supplied_to = supply_interface;
  3114. ac_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  3115. ac_psy_cfg.drv_data = &di->ac_chg;
  3116. usb_psy_cfg.supplied_to = supply_interface;
  3117. usb_psy_cfg.num_supplicants = ARRAY_SIZE(supply_interface);
  3118. usb_psy_cfg.drv_data = &di->usb_chg;
  3119. /* AC supply */
  3120. /* ux500_charger sub-class */
  3121. di->ac_chg.ops.enable = &ab8500_charger_ac_en;
  3122. di->ac_chg.ops.check_enable = &ab8500_charger_ac_check_enable;
  3123. di->ac_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
  3124. di->ac_chg.ops.update_curr = &ab8500_charger_update_charger_current;
  3125. di->ac_chg.max_out_volt = ab8500_charger_voltage_map[
  3126. ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
  3127. di->ac_chg.max_out_curr =
  3128. di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
  3129. di->ac_chg.wdt_refresh = CHG_WD_INTERVAL;
  3130. di->ac_chg.enabled = di->bm->ac_enabled;
  3131. di->ac_chg.external = false;
  3132. /*notifier for external charger enabling*/
  3133. if (!di->ac_chg.enabled)
  3134. blocking_notifier_chain_register(
  3135. &charger_notifier_list, &charger_nb);
  3136. /* USB supply */
  3137. /* ux500_charger sub-class */
  3138. di->usb_chg.ops.enable = &ab8500_charger_usb_en;
  3139. di->usb_chg.ops.check_enable = &ab8500_charger_usb_check_enable;
  3140. di->usb_chg.ops.kick_wd = &ab8500_charger_watchdog_kick;
  3141. di->usb_chg.ops.update_curr = &ab8500_charger_update_charger_current;
  3142. di->usb_chg.ops.pp_enable = &ab8540_charger_power_path_enable;
  3143. di->usb_chg.ops.pre_chg_enable = &ab8540_charger_usb_pre_chg_enable;
  3144. di->usb_chg.max_out_volt = ab8500_charger_voltage_map[
  3145. ARRAY_SIZE(ab8500_charger_voltage_map) - 1];
  3146. di->usb_chg.max_out_curr =
  3147. di->bm->chg_output_curr[di->bm->n_chg_out_curr - 1];
  3148. di->usb_chg.wdt_refresh = CHG_WD_INTERVAL;
  3149. di->usb_chg.enabled = di->bm->usb_enabled;
  3150. di->usb_chg.external = false;
  3151. di->usb_chg.power_path = di->bm->usb_power_path;
  3152. di->usb_state.usb_current = -1;
  3153. /* Create a work queue for the charger */
  3154. di->charger_wq = alloc_ordered_workqueue("ab8500_charger_wq",
  3155. WQ_MEM_RECLAIM);
  3156. if (di->charger_wq == NULL) {
  3157. dev_err(di->dev, "failed to create work queue\n");
  3158. return -ENOMEM;
  3159. }
  3160. mutex_init(&di->charger_attached_mutex);
  3161. /* Init work for HW failure check */
  3162. INIT_DEFERRABLE_WORK(&di->check_hw_failure_work,
  3163. ab8500_charger_check_hw_failure_work);
  3164. INIT_DEFERRABLE_WORK(&di->check_usbchgnotok_work,
  3165. ab8500_charger_check_usbchargernotok_work);
  3166. INIT_DELAYED_WORK(&di->ac_charger_attached_work,
  3167. ab8500_charger_ac_attached_work);
  3168. INIT_DELAYED_WORK(&di->usb_charger_attached_work,
  3169. ab8500_charger_usb_attached_work);
  3170. /*
  3171. * For ABB revision 1.0 and 1.1 there is a bug in the watchdog
  3172. * logic. That means we have to continously kick the charger
  3173. * watchdog even when no charger is connected. This is only
  3174. * valid once the AC charger has been enabled. This is
  3175. * a bug that is not handled by the algorithm and the
  3176. * watchdog have to be kicked by the charger driver
  3177. * when the AC charger is disabled
  3178. */
  3179. INIT_DEFERRABLE_WORK(&di->kick_wd_work,
  3180. ab8500_charger_kick_watchdog_work);
  3181. INIT_DEFERRABLE_WORK(&di->check_vbat_work,
  3182. ab8500_charger_check_vbat_work);
  3183. INIT_DELAYED_WORK(&di->attach_work,
  3184. ab8500_charger_usb_link_attach_work);
  3185. INIT_DELAYED_WORK(&di->usb_state_changed_work,
  3186. ab8500_charger_usb_state_changed_work);
  3187. INIT_DELAYED_WORK(&di->vbus_drop_end_work,
  3188. ab8500_charger_vbus_drop_end_work);
  3189. /* Init work for charger detection */
  3190. INIT_WORK(&di->usb_link_status_work,
  3191. ab8500_charger_usb_link_status_work);
  3192. INIT_WORK(&di->ac_work, ab8500_charger_ac_work);
  3193. INIT_WORK(&di->detect_usb_type_work,
  3194. ab8500_charger_detect_usb_type_work);
  3195. /* Init work for checking HW status */
  3196. INIT_WORK(&di->check_main_thermal_prot_work,
  3197. ab8500_charger_check_main_thermal_prot_work);
  3198. INIT_WORK(&di->check_usb_thermal_prot_work,
  3199. ab8500_charger_check_usb_thermal_prot_work);
  3200. /*
  3201. * VDD ADC supply needs to be enabled from this driver when there
  3202. * is a charger connected to avoid erroneous BTEMP_HIGH/LOW
  3203. * interrupts during charging
  3204. */
  3205. di->regu = devm_regulator_get(di->dev, "vddadc");
  3206. if (IS_ERR(di->regu)) {
  3207. ret = PTR_ERR(di->regu);
  3208. dev_err(di->dev, "failed to get vddadc regulator\n");
  3209. goto free_charger_wq;
  3210. }
  3211. /* Initialize OVV, and other registers */
  3212. ret = ab8500_charger_init_hw_registers(di);
  3213. if (ret) {
  3214. dev_err(di->dev, "failed to initialize ABB registers\n");
  3215. goto free_charger_wq;
  3216. }
  3217. /* Register AC charger class */
  3218. if (di->ac_chg.enabled) {
  3219. di->ac_chg.psy = power_supply_register(di->dev,
  3220. &ab8500_ac_chg_desc,
  3221. &ac_psy_cfg);
  3222. if (IS_ERR(di->ac_chg.psy)) {
  3223. dev_err(di->dev, "failed to register AC charger\n");
  3224. ret = PTR_ERR(di->ac_chg.psy);
  3225. goto free_charger_wq;
  3226. }
  3227. }
  3228. /* Register USB charger class */
  3229. if (di->usb_chg.enabled) {
  3230. di->usb_chg.psy = power_supply_register(di->dev,
  3231. &ab8500_usb_chg_desc,
  3232. &usb_psy_cfg);
  3233. if (IS_ERR(di->usb_chg.psy)) {
  3234. dev_err(di->dev, "failed to register USB charger\n");
  3235. ret = PTR_ERR(di->usb_chg.psy);
  3236. goto free_ac;
  3237. }
  3238. }
  3239. di->usb_phy = usb_get_phy(USB_PHY_TYPE_USB2);
  3240. if (IS_ERR_OR_NULL(di->usb_phy)) {
  3241. dev_err(di->dev, "failed to get usb transceiver\n");
  3242. ret = -EINVAL;
  3243. goto free_usb;
  3244. }
  3245. di->nb.notifier_call = ab8500_charger_usb_notifier_call;
  3246. ret = usb_register_notifier(di->usb_phy, &di->nb);
  3247. if (ret) {
  3248. dev_err(di->dev, "failed to register usb notifier\n");
  3249. goto put_usb_phy;
  3250. }
  3251. /* Identify the connected charger types during startup */
  3252. charger_status = ab8500_charger_detect_chargers(di, true);
  3253. if (charger_status & AC_PW_CONN) {
  3254. di->ac.charger_connected = 1;
  3255. di->ac_conn = true;
  3256. ab8500_power_supply_changed(di, di->ac_chg.psy);
  3257. sysfs_notify(&di->ac_chg.psy->dev.kobj, NULL, "present");
  3258. }
  3259. if (charger_status & USB_PW_CONN) {
  3260. di->vbus_detected = true;
  3261. di->vbus_detected_start = true;
  3262. queue_work(di->charger_wq,
  3263. &di->detect_usb_type_work);
  3264. }
  3265. /* Register interrupts */
  3266. for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
  3267. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  3268. ret = request_threaded_irq(irq, NULL, ab8500_charger_irq[i].isr,
  3269. IRQF_SHARED | IRQF_NO_SUSPEND,
  3270. ab8500_charger_irq[i].name, di);
  3271. if (ret != 0) {
  3272. dev_err(di->dev, "failed to request %s IRQ %d: %d\n"
  3273. , ab8500_charger_irq[i].name, irq, ret);
  3274. goto free_irq;
  3275. }
  3276. dev_dbg(di->dev, "Requested %s IRQ %d: %d\n",
  3277. ab8500_charger_irq[i].name, irq, ret);
  3278. }
  3279. platform_set_drvdata(pdev, di);
  3280. mutex_lock(&di->charger_attached_mutex);
  3281. ch_stat = ab8500_charger_detect_chargers(di, false);
  3282. if ((ch_stat & AC_PW_CONN) == AC_PW_CONN) {
  3283. if (is_ab8500(di->parent))
  3284. queue_delayed_work(di->charger_wq,
  3285. &di->ac_charger_attached_work,
  3286. HZ);
  3287. }
  3288. if ((ch_stat & USB_PW_CONN) == USB_PW_CONN) {
  3289. if (is_ab8500(di->parent))
  3290. queue_delayed_work(di->charger_wq,
  3291. &di->usb_charger_attached_work,
  3292. HZ);
  3293. }
  3294. mutex_unlock(&di->charger_attached_mutex);
  3295. return ret;
  3296. free_irq:
  3297. usb_unregister_notifier(di->usb_phy, &di->nb);
  3298. /* We also have to free all successfully registered irqs */
  3299. for (i = i - 1; i >= 0; i--) {
  3300. irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
  3301. free_irq(irq, di);
  3302. }
  3303. put_usb_phy:
  3304. usb_put_phy(di->usb_phy);
  3305. free_usb:
  3306. if (di->usb_chg.enabled)
  3307. power_supply_unregister(di->usb_chg.psy);
  3308. free_ac:
  3309. if (di->ac_chg.enabled)
  3310. power_supply_unregister(di->ac_chg.psy);
  3311. free_charger_wq:
  3312. destroy_workqueue(di->charger_wq);
  3313. return ret;
  3314. }
  3315. static const struct of_device_id ab8500_charger_match[] = {
  3316. { .compatible = "stericsson,ab8500-charger", },
  3317. { },
  3318. };
  3319. MODULE_DEVICE_TABLE(of, ab8500_charger_match);
  3320. static struct platform_driver ab8500_charger_driver = {
  3321. .probe = ab8500_charger_probe,
  3322. .remove = ab8500_charger_remove,
  3323. .suspend = ab8500_charger_suspend,
  3324. .resume = ab8500_charger_resume,
  3325. .driver = {
  3326. .name = "ab8500-charger",
  3327. .of_match_table = ab8500_charger_match,
  3328. },
  3329. };
  3330. static int __init ab8500_charger_init(void)
  3331. {
  3332. return platform_driver_register(&ab8500_charger_driver);
  3333. }
  3334. static void __exit ab8500_charger_exit(void)
  3335. {
  3336. platform_driver_unregister(&ab8500_charger_driver);
  3337. }
  3338. subsys_initcall_sync(ab8500_charger_init);
  3339. module_exit(ab8500_charger_exit);
  3340. MODULE_LICENSE("GPL v2");
  3341. MODULE_AUTHOR("Johan Palsson, Karl Komierowski, Arun R Murthy");
  3342. MODULE_ALIAS("platform:ab8500-charger");
  3343. MODULE_DESCRIPTION("AB8500 charger management driver");