msm_otg.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261
  1. /* Copyright (c) 2009-2015, Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #ifndef DEBUG
  14. #define DEBUG
  15. #endif
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/clk.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/err.h>
  23. #include <linux/delay.h>
  24. #include <linux/io.h>
  25. #include <linux/ioport.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/suspend.h>
  31. #include <linux/of.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/usb/ulpi.h>
  36. #include <linux/usb/gadget.h>
  37. #include <linux/usb/hcd.h>
  38. #include <linux/usb/quirks.h>
  39. #include <linux/usb/msm_hsusb.h>
  40. #include <linux/usb/msm_hsusb_hw.h>
  41. #include <linux/usb/msm_ext_chg.h>
  42. #include <linux/regulator/consumer.h>
  43. #include <linux/mfd/pm8xxx/pm8921-charger.h>
  44. #include <linux/mfd/pm8xxx/misc.h>
  45. #include <linux/mhl_8334.h>
  46. #include <linux/qpnp/qpnp-adc.h>
  47. #include <mach/scm.h>
  48. #include <mach/clk.h>
  49. #include <mach/mpm.h>
  50. #include <mach/msm_xo.h>
  51. #include <mach/msm_bus.h>
  52. #include <mach/rpm-regulator.h>
  53. #define MSM_USB_BASE (motg->regs)
  54. #define DRIVER_NAME "msm_otg"
  55. #define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
  56. #define CHG_RECHECK_DELAY (jiffies + msecs_to_jiffies(2000))
  57. #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
  58. #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
  59. #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
  60. #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
  61. #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
  62. #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
  63. #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
  64. #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
  65. #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
  66. #define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
  67. #define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
  68. #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
  69. #define USB_SUSPEND_DELAY_TIME (500 * HZ/1000) /* 500 msec */
  70. #define USE_MUIC_CHGTYPE (CONFIG_USB_SWITCH_RT8973)
  71. enum msm_otg_phy_reg_mode {
  72. USB_PHY_REG_OFF,
  73. USB_PHY_REG_ON,
  74. USB_PHY_REG_LPM_ON,
  75. USB_PHY_REG_LPM_OFF,
  76. };
  77. extern int poweroff_charging;
  78. static char *override_phy_init;
  79. module_param(override_phy_init, charp, S_IRUGO|S_IWUSR);
  80. MODULE_PARM_DESC(override_phy_init,
  81. "Override HSUSB PHY Init Settings");
  82. unsigned int lpm_disconnect_thresh = 1000;
  83. module_param(lpm_disconnect_thresh , uint, S_IRUGO | S_IWUSR);
  84. MODULE_PARM_DESC(lpm_disconnect_thresh,
  85. "Delay before entering LPM on USB disconnect");
  86. static bool floated_charger_enable;
  87. module_param(floated_charger_enable , bool, S_IRUGO | S_IWUSR);
  88. MODULE_PARM_DESC(floated_charger_enable,
  89. "Whether to enable floated charger");
  90. #ifdef OTG_WAIT_PMIC
  91. static DECLARE_COMPLETION(pmic_vbus_init);
  92. #endif
  93. static struct msm_otg *the_msm_otg;
  94. static bool debug_aca_enabled;
  95. static bool debug_bus_voting_enabled;
  96. static bool mhl_det_in_progress;
  97. static struct regulator *hsusb_3p3;
  98. static struct regulator *hsusb_1p8;
  99. static struct regulator *hsusb_vdd;
  100. static struct regulator *vbus_otg;
  101. static struct regulator *mhl_usb_hs_switch;
  102. static struct power_supply *psy;
  103. #ifdef CONFIG_USBIRQ_BALANCING_LTE_HIGHTP
  104. #include <linux/netdevice.h>
  105. #include <linux/cpu.h>
  106. int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask);
  107. static struct notifier_block rndis_notifier;
  108. static struct notifier_block cpu_hotplug_notifier;
  109. static int otg_irq = 0;
  110. static bool is_rndis_running = false;
  111. static bool is_irq_masked = false;
  112. #endif
  113. static bool aca_id_turned_on;
  114. static bool legacy_power_supply;
  115. static inline bool aca_enabled(void)
  116. {
  117. #ifdef CONFIG_USB_MSM_ACA
  118. return true;
  119. #else
  120. return debug_aca_enabled;
  121. #endif
  122. }
  123. static int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
  124. { /* VDD_CX CORNER Voting */
  125. [VDD_NONE] = RPM_VREG_CORNER_NONE,
  126. [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
  127. [VDD_MAX] = RPM_VREG_CORNER_HIGH,
  128. },
  129. { /* VDD_CX Voltage Voting */
  130. [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
  131. [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
  132. [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
  133. },
  134. };
  135. #ifdef CONFIG_USB_HOST_NOTIFY
  136. #include "msm_otg_sec.c"
  137. #else
  138. void sec_otg_set_dock_state(int enable)
  139. { pr_info("%s\n", __func__); }
  140. EXPORT_SYMBOL_GPL(sec_otg_set_dock_state);
  141. void sec_otg_set_id_state(bool enable)
  142. { pr_info("%s\n", __func__); }
  143. EXPORT_SYMBOL_GPL(sec_otg_set_id_state);
  144. #endif
  145. /* rapper function for set_vbus_state() */
  146. static void msm_otg_set_vbus_state(int online);
  147. void sec_otg_set_vbus_state(int online)
  148. {
  149. msm_otg_set_vbus_state(online);
  150. }
  151. EXPORT_SYMBOL(sec_otg_set_vbus_state);
  152. static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
  153. {
  154. int rc = 0;
  155. if (init) {
  156. hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
  157. if (IS_ERR(hsusb_3p3)) {
  158. dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
  159. return PTR_ERR(hsusb_3p3);
  160. }
  161. rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
  162. USB_PHY_3P3_VOL_MAX);
  163. if (rc) {
  164. dev_err(motg->phy.dev, "unable to set voltage level for"
  165. "hsusb 3p3\n");
  166. return rc;
  167. }
  168. hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
  169. if (IS_ERR(hsusb_1p8)) {
  170. dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
  171. rc = PTR_ERR(hsusb_1p8);
  172. goto put_3p3_lpm;
  173. }
  174. rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
  175. USB_PHY_1P8_VOL_MAX);
  176. if (rc) {
  177. dev_err(motg->phy.dev, "unable to set voltage level "
  178. "for hsusb 1p8\n");
  179. goto put_1p8;
  180. }
  181. return 0;
  182. }
  183. put_1p8:
  184. regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
  185. put_3p3_lpm:
  186. regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
  187. return rc;
  188. }
  189. static int msm_hsusb_config_vddcx(int high)
  190. {
  191. struct msm_otg *motg = the_msm_otg;
  192. enum usb_vdd_type vdd_type = motg->vdd_type;
  193. int max_vol = vdd_val[vdd_type][VDD_MAX];
  194. int min_vol;
  195. int ret;
  196. min_vol = vdd_val[vdd_type][!!high];
  197. ret = regulator_set_voltage(hsusb_vdd, min_vol, max_vol);
  198. if (ret) {
  199. pr_err("%s: unable to set the voltage for regulator "
  200. "HSUSB_VDDCX\n", __func__);
  201. return ret;
  202. }
  203. pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
  204. return ret;
  205. }
  206. static int msm_hsusb_ldo_enable(struct msm_otg *motg,
  207. enum msm_otg_phy_reg_mode mode)
  208. {
  209. int ret = 0;
  210. if (IS_ERR(hsusb_1p8)) {
  211. pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
  212. return -ENODEV;
  213. }
  214. if (IS_ERR(hsusb_3p3)) {
  215. pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
  216. return -ENODEV;
  217. }
  218. switch (mode) {
  219. case USB_PHY_REG_ON:
  220. ret = regulator_set_optimum_mode(hsusb_1p8,
  221. USB_PHY_1P8_HPM_LOAD);
  222. if (ret < 0) {
  223. pr_err("%s: Unable to set HPM of the regulator "
  224. "HSUSB_1p8\n", __func__);
  225. return ret;
  226. }
  227. ret = regulator_enable(hsusb_1p8);
  228. if (ret) {
  229. dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
  230. __func__);
  231. regulator_set_optimum_mode(hsusb_1p8, 0);
  232. return ret;
  233. }
  234. ret = regulator_set_optimum_mode(hsusb_3p3,
  235. USB_PHY_3P3_HPM_LOAD);
  236. if (ret < 0) {
  237. pr_err("%s: Unable to set HPM of the regulator "
  238. "HSUSB_3p3\n", __func__);
  239. regulator_set_optimum_mode(hsusb_1p8, 0);
  240. regulator_disable(hsusb_1p8);
  241. return ret;
  242. }
  243. ret = regulator_enable(hsusb_3p3);
  244. if (ret) {
  245. dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
  246. __func__);
  247. regulator_set_optimum_mode(hsusb_3p3, 0);
  248. regulator_set_optimum_mode(hsusb_1p8, 0);
  249. regulator_disable(hsusb_1p8);
  250. return ret;
  251. }
  252. break;
  253. case USB_PHY_REG_OFF:
  254. ret = regulator_disable(hsusb_1p8);
  255. if (ret) {
  256. dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
  257. __func__);
  258. return ret;
  259. }
  260. ret = regulator_set_optimum_mode(hsusb_1p8, 0);
  261. if (ret < 0)
  262. pr_err("%s: Unable to set LPM of the regulator "
  263. "HSUSB_1p8\n", __func__);
  264. ret = regulator_disable(hsusb_3p3);
  265. if (ret) {
  266. dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
  267. __func__);
  268. return ret;
  269. }
  270. ret = regulator_set_optimum_mode(hsusb_3p3, 0);
  271. if (ret < 0)
  272. pr_err("%s: Unable to set LPM of the regulator "
  273. "HSUSB_3p3\n", __func__);
  274. break;
  275. case USB_PHY_REG_LPM_ON:
  276. ret = regulator_set_optimum_mode(hsusb_1p8,
  277. USB_PHY_1P8_LPM_LOAD);
  278. if (ret < 0) {
  279. pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
  280. __func__);
  281. return ret;
  282. }
  283. ret = regulator_set_optimum_mode(hsusb_3p3,
  284. USB_PHY_3P3_LPM_LOAD);
  285. if (ret < 0) {
  286. pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
  287. __func__);
  288. regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
  289. return ret;
  290. }
  291. break;
  292. case USB_PHY_REG_LPM_OFF:
  293. ret = regulator_set_optimum_mode(hsusb_1p8,
  294. USB_PHY_1P8_HPM_LOAD);
  295. if (ret < 0) {
  296. pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
  297. __func__);
  298. return ret;
  299. }
  300. ret = regulator_set_optimum_mode(hsusb_3p3,
  301. USB_PHY_3P3_HPM_LOAD);
  302. if (ret < 0) {
  303. pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
  304. __func__);
  305. regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
  306. return ret;
  307. }
  308. break;
  309. default:
  310. pr_err("%s: Unsupported mode (%d).", __func__, mode);
  311. return -ENOTSUPP;
  312. }
  313. pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
  314. return ret < 0 ? ret : 0;
  315. }
  316. static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
  317. {
  318. struct msm_otg_platform_data *pdata = motg->pdata;
  319. if (!pdata->mhl_enable)
  320. return;
  321. if (!mhl_usb_hs_switch) {
  322. pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
  323. return;
  324. }
  325. if (on) {
  326. if (regulator_enable(mhl_usb_hs_switch))
  327. pr_err("unable to enable mhl_usb_hs_switch\n");
  328. } else {
  329. regulator_disable(mhl_usb_hs_switch);
  330. }
  331. }
  332. static int ulpi_read(struct usb_phy *phy, u32 reg)
  333. {
  334. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  335. int cnt = 0;
  336. /* initiate read operation */
  337. writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
  338. USB_ULPI_VIEWPORT);
  339. /* wait for completion */
  340. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  341. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  342. break;
  343. udelay(1);
  344. cnt++;
  345. }
  346. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  347. dev_err(phy->dev, "ulpi_read: timeout %08x\n",
  348. readl(USB_ULPI_VIEWPORT));
  349. dev_err(phy->dev, "PORTSC: %08x USBCMD: %08x\n",
  350. readl_relaxed(USB_PORTSC), readl_relaxed(USB_USBCMD));
  351. return -ETIMEDOUT;
  352. }
  353. return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
  354. }
  355. static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
  356. {
  357. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  358. int cnt = 0;
  359. /* initiate write operation */
  360. writel(ULPI_RUN | ULPI_WRITE |
  361. ULPI_ADDR(reg) | ULPI_DATA(val),
  362. USB_ULPI_VIEWPORT);
  363. /* wait for completion */
  364. while (cnt < ULPI_IO_TIMEOUT_USEC) {
  365. if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
  366. break;
  367. udelay(1);
  368. cnt++;
  369. }
  370. if (cnt >= ULPI_IO_TIMEOUT_USEC) {
  371. dev_err(phy->dev, "ulpi_write: timeout\n");
  372. dev_err(phy->dev, "PORTSC: %08x USBCMD: %08x\n",
  373. readl_relaxed(USB_PORTSC), readl_relaxed(USB_USBCMD));
  374. return -ETIMEDOUT;
  375. }
  376. return 0;
  377. }
  378. static struct usb_phy_io_ops msm_otg_io_ops = {
  379. .read = ulpi_read,
  380. .write = ulpi_write,
  381. };
  382. static void ulpi_init(struct msm_otg *motg)
  383. {
  384. struct msm_otg_platform_data *pdata = motg->pdata;
  385. int aseq[10];
  386. int *seq = NULL;
  387. if (override_phy_init) {
  388. pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
  389. override_phy_init);
  390. get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
  391. seq = &aseq[1];
  392. } else {
  393. seq = pdata->phy_init_seq;
  394. }
  395. if (!seq)
  396. return;
  397. while (seq[0] >= 0) {
  398. if (override_phy_init)
  399. pr_debug("ulpi: write 0x%02x to 0x%02x\n",
  400. seq[0], seq[1]);
  401. dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
  402. seq[0], seq[1]);
  403. ulpi_write(&motg->phy, seq[0], seq[1]);
  404. seq += 2;
  405. }
  406. }
  407. static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
  408. {
  409. int ret;
  410. if (assert) {
  411. if (!IS_ERR(motg->clk)) {
  412. ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
  413. } else {
  414. /* Using asynchronous block reset to the hardware */
  415. dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
  416. clk_disable_unprepare(motg->pclk);
  417. clk_disable_unprepare(motg->core_clk);
  418. ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
  419. }
  420. if (ret)
  421. dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
  422. } else {
  423. if (!IS_ERR(motg->clk)) {
  424. ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
  425. } else {
  426. dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
  427. ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
  428. ndelay(200);
  429. ret = clk_prepare_enable(motg->core_clk);
  430. WARN(ret, "USB core_clk enable failed\n");
  431. ret = clk_prepare_enable(motg->pclk);
  432. WARN(ret, "USB pclk enable failed\n");
  433. }
  434. if (ret)
  435. dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
  436. }
  437. return ret;
  438. }
  439. static int msm_otg_phy_reset(struct msm_otg *motg)
  440. {
  441. u32 val;
  442. int ret;
  443. struct msm_otg_platform_data *pdata = motg->pdata;
  444. /*
  445. * AHB2AHB Bypass mode shouldn't be enable before doing
  446. * async clock reset. If it is enable, disable the same.
  447. */
  448. val = readl_relaxed(USB_AHBMODE);
  449. if (val & AHB2AHB_BYPASS) {
  450. pr_err("%s(): AHB2AHB_BYPASS SET: AHBMODE:%x\n",
  451. __func__, val);
  452. val &= ~AHB2AHB_BYPASS_BIT_MASK;
  453. writel_relaxed(val | AHB2AHB_BYPASS_CLEAR, USB_AHBMODE);
  454. pr_err("%s(): AHBMODE: %x\n", __func__,
  455. readl_relaxed(USB_AHBMODE));
  456. }
  457. ret = msm_otg_link_clk_reset(motg, 1);
  458. if (ret)
  459. return ret;
  460. /* wait for 1ms delay as suggested in HPG. */
  461. usleep_range(1000, 1200);
  462. ret = msm_otg_link_clk_reset(motg, 0);
  463. if (ret)
  464. return ret;
  465. if (pdata && pdata->enable_sec_phy)
  466. writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
  467. USB_PHY_CTRL2);
  468. val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
  469. writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
  470. dev_info(motg->phy.dev, "phy_reset: success\n");
  471. return 0;
  472. }
  473. #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
  474. static int msm_otg_link_reset(struct msm_otg *motg)
  475. {
  476. int cnt = 0;
  477. struct msm_otg_platform_data *pdata = motg->pdata;
  478. writel_relaxed(USBCMD_RESET, USB_USBCMD);
  479. while (cnt < LINK_RESET_TIMEOUT_USEC) {
  480. if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
  481. break;
  482. udelay(1);
  483. cnt++;
  484. }
  485. if (cnt >= LINK_RESET_TIMEOUT_USEC)
  486. return -ETIMEDOUT;
  487. /* select ULPI phy */
  488. writel_relaxed(0x80000000, USB_PORTSC);
  489. writel_relaxed(0x0, USB_AHBBURST);
  490. writel_relaxed(0x08, USB_AHBMODE);
  491. if (pdata && pdata->enable_sec_phy)
  492. writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
  493. USB_PHY_CTRL2);
  494. return 0;
  495. }
  496. static void usb_phy_reset(struct msm_otg *motg)
  497. {
  498. u32 val;
  499. if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY)
  500. return;
  501. /* Assert USB PHY_PON */
  502. val = readl_relaxed(motg->usb_phy_ctrl_reg);
  503. val &= ~PHY_POR_BIT_MASK;
  504. val |= PHY_POR_ASSERT;
  505. writel_relaxed(val, motg->usb_phy_ctrl_reg);
  506. /* wait for minimum 10 microseconds as suggested in HPG. */
  507. usleep_range(10, 15);
  508. /* Deassert USB PHY_PON */
  509. val = readl_relaxed(motg->usb_phy_ctrl_reg);
  510. val &= ~PHY_POR_BIT_MASK;
  511. val |= PHY_POR_DEASSERT;
  512. writel_relaxed(val, motg->usb_phy_ctrl_reg);
  513. /* Ensure that RESET operation is completed. */
  514. mb();
  515. }
  516. static int msm_otg_reset(struct usb_phy *phy)
  517. {
  518. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  519. struct msm_otg_platform_data *pdata = motg->pdata;
  520. int ret;
  521. u32 val = 0;
  522. u32 ulpi_val = 0;
  523. /*
  524. * USB PHY and Link reset also reset the USB BAM.
  525. * Thus perform reset operation only once to avoid
  526. * USB BAM reset on other cases e.g. USB cable disconnections.
  527. */
  528. if (pdata->disable_reset_on_disconnect) {
  529. if (motg->reset_counter)
  530. return 0;
  531. else
  532. motg->reset_counter++;
  533. }
  534. if (!IS_ERR(motg->clk))
  535. clk_prepare_enable(motg->clk);
  536. ret = msm_otg_phy_reset(motg);
  537. if (ret) {
  538. dev_err(phy->dev, "phy_reset failed\n");
  539. return ret;
  540. }
  541. aca_id_turned_on = false;
  542. ret = msm_otg_link_reset(motg);
  543. if (ret) {
  544. dev_err(phy->dev, "link reset failed\n");
  545. return ret;
  546. }
  547. msleep(100);
  548. /* Reset USB PHY after performing USB Link RESET */
  549. usb_phy_reset(motg);
  550. /* Program USB PHY Override registers. */
  551. ulpi_init(motg);
  552. /*
  553. * It is recommended in HPG to reset USB PHY after programming
  554. * USB PHY Override registers.
  555. */
  556. usb_phy_reset(motg);
  557. if (!IS_ERR(motg->clk))
  558. clk_disable_unprepare(motg->clk);
  559. if (pdata->otg_control == OTG_PHY_CONTROL) {
  560. val = readl_relaxed(USB_OTGSC);
  561. if (pdata->mode == USB_OTG) {
  562. ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
  563. val |= OTGSC_IDIE | OTGSC_BSVIE;
  564. } else if (pdata->mode == USB_PERIPHERAL) {
  565. ulpi_val = ULPI_INT_SESS_VALID;
  566. val |= OTGSC_BSVIE;
  567. }
  568. writel_relaxed(val, USB_OTGSC);
  569. ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
  570. ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
  571. } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
  572. ulpi_write(phy, OTG_COMP_DISABLE,
  573. ULPI_SET(ULPI_PWR_CLK_MNG_REG));
  574. /* Enable PMIC pull-up */
  575. pm8xxx_usb_id_pullup(1);
  576. }
  577. if (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)
  578. writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU),
  579. USB_OTGSC);
  580. return 0;
  581. }
  582. static const char *timer_string(int bit)
  583. {
  584. switch (bit) {
  585. case A_WAIT_VRISE: return "a_wait_vrise";
  586. case A_WAIT_VFALL: return "a_wait_vfall";
  587. case B_SRP_FAIL: return "b_srp_fail";
  588. case A_WAIT_BCON: return "a_wait_bcon";
  589. case A_AIDL_BDIS: return "a_aidl_bdis";
  590. case A_BIDL_ADIS: return "a_bidl_adis";
  591. case B_ASE0_BRST: return "b_ase0_brst";
  592. case A_TST_MAINT: return "a_tst_maint";
  593. case B_TST_SRP: return "b_tst_srp";
  594. case B_TST_CONFIG: return "b_tst_config";
  595. default: return "UNDEFINED";
  596. }
  597. }
  598. static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
  599. {
  600. struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
  601. switch (motg->active_tmout) {
  602. case A_WAIT_VRISE:
  603. /* TODO: use vbus_vld interrupt */
  604. set_bit(A_VBUS_VLD, &motg->inputs);
  605. break;
  606. case A_TST_MAINT:
  607. /* OTG PET: End session after TA_TST_MAINT */
  608. set_bit(A_BUS_DROP, &motg->inputs);
  609. break;
  610. case B_TST_SRP:
  611. /*
  612. * OTG PET: Initiate SRP after TB_TST_SRP of
  613. * previous session end.
  614. */
  615. set_bit(B_BUS_REQ, &motg->inputs);
  616. break;
  617. case B_TST_CONFIG:
  618. clear_bit(A_CONN, &motg->inputs);
  619. break;
  620. default:
  621. set_bit(motg->active_tmout, &motg->tmouts);
  622. }
  623. pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
  624. queue_work(system_nrt_wq, &motg->sm_work);
  625. return HRTIMER_NORESTART;
  626. }
  627. static void msm_otg_del_timer(struct msm_otg *motg)
  628. {
  629. int bit = motg->active_tmout;
  630. pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
  631. div_s64(ktime_to_us(hrtimer_get_remaining(
  632. &motg->timer)), 1000));
  633. hrtimer_cancel(&motg->timer);
  634. clear_bit(bit, &motg->tmouts);
  635. }
  636. static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
  637. {
  638. clear_bit(bit, &motg->tmouts);
  639. motg->active_tmout = bit;
  640. pr_debug("starting %s timer\n", timer_string(bit));
  641. hrtimer_start(&motg->timer,
  642. ktime_set(time / 1000, (time % 1000) * 1000000),
  643. HRTIMER_MODE_REL);
  644. }
  645. static void msm_otg_init_timer(struct msm_otg *motg)
  646. {
  647. hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  648. motg->timer.function = msm_otg_timer_func;
  649. }
  650. static int msm_otg_start_hnp(struct usb_otg *otg)
  651. {
  652. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  653. if (otg->phy->state != OTG_STATE_A_HOST) {
  654. pr_err("HNP can not be initiated in %s state\n",
  655. otg_state_string(otg->phy->state));
  656. return -EINVAL;
  657. }
  658. pr_debug("A-Host: HNP initiated\n");
  659. clear_bit(A_BUS_REQ, &motg->inputs);
  660. queue_work(system_nrt_wq, &motg->sm_work);
  661. return 0;
  662. }
  663. static int msm_otg_start_srp(struct usb_otg *otg)
  664. {
  665. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  666. u32 val;
  667. int ret = 0;
  668. if (otg->phy->state != OTG_STATE_B_IDLE) {
  669. pr_err("SRP can not be initiated in %s state\n",
  670. otg_state_string(otg->phy->state));
  671. ret = -EINVAL;
  672. goto out;
  673. }
  674. if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
  675. pr_debug("initial conditions of SRP are not met. Try again"
  676. "after some time\n");
  677. ret = -EAGAIN;
  678. goto out;
  679. }
  680. pr_debug("B-Device SRP started\n");
  681. /*
  682. * PHY won't pull D+ high unless it detects Vbus valid.
  683. * Since by definition, SRP is only done when Vbus is not valid,
  684. * software work-around needs to be used to spoof the PHY into
  685. * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
  686. * VBUSVLDEXT register bits.
  687. */
  688. ulpi_write(otg->phy, 0x03, 0x97);
  689. /*
  690. * Harware auto assist data pulsing: Data pulse is given
  691. * for 7msec; wait for vbus
  692. */
  693. val = readl_relaxed(USB_OTGSC);
  694. writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
  695. /* VBUS plusing is obsoleted in OTG 2.0 supplement */
  696. out:
  697. return ret;
  698. }
  699. static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
  700. {
  701. struct usb_hcd *hcd = bus_to_hcd(otg->host);
  702. struct usb_device *rhub = otg->host->root_hub;
  703. if (enable) {
  704. pm_runtime_disable(&rhub->dev);
  705. rhub->state = USB_STATE_NOTATTACHED;
  706. hcd->driver->bus_suspend(hcd);
  707. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  708. } else {
  709. usb_remove_hcd(hcd);
  710. msm_otg_reset(otg->phy);
  711. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  712. }
  713. }
  714. static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
  715. {
  716. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  717. if (aca_enabled())
  718. return 0;
  719. /*
  720. * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
  721. * during bus suspend/resume. Update the relevant state
  722. * machine inputs and trigger LPM entry/exit. Checking
  723. * in_lpm flag would avoid unnecessary work scheduling.
  724. */
  725. if (suspend) {
  726. switch (phy->state) {
  727. case OTG_STATE_A_WAIT_BCON:
  728. if (TA_WAIT_BCON > 0)
  729. break;
  730. /* fall through */
  731. case OTG_STATE_A_HOST:
  732. pr_debug("host bus suspend\n");
  733. clear_bit(A_BUS_REQ, &motg->inputs);
  734. if (!atomic_read(&motg->in_lpm))
  735. queue_work(system_nrt_wq, &motg->sm_work);
  736. break;
  737. case OTG_STATE_B_PERIPHERAL:
  738. pr_debug("peripheral bus suspend\n");
  739. if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
  740. break;
  741. set_bit(A_BUS_SUSPEND, &motg->inputs);
  742. if (!atomic_read(&motg->in_lpm))
  743. queue_delayed_work(system_nrt_wq,
  744. &motg->suspend_work,
  745. USB_SUSPEND_DELAY_TIME);
  746. break;
  747. default:
  748. break;
  749. }
  750. } else {
  751. switch (phy->state) {
  752. case OTG_STATE_A_WAIT_BCON:
  753. /* Remote wakeup or resume */
  754. set_bit(A_BUS_REQ, &motg->inputs);
  755. /* ensure hardware is not in low power mode */
  756. if (atomic_read(&motg->in_lpm))
  757. pm_runtime_resume(phy->dev);
  758. break;
  759. case OTG_STATE_A_SUSPEND:
  760. /* Remote wakeup or resume */
  761. set_bit(A_BUS_REQ, &motg->inputs);
  762. phy->state = OTG_STATE_A_HOST;
  763. /* ensure hardware is not in low power mode */
  764. if (atomic_read(&motg->in_lpm))
  765. pm_runtime_resume(phy->dev);
  766. break;
  767. case OTG_STATE_B_PERIPHERAL:
  768. pr_debug("peripheral bus resume\n");
  769. if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
  770. break;
  771. clear_bit(A_BUS_SUSPEND, &motg->inputs);
  772. if (atomic_read(&motg->in_lpm))
  773. queue_work(system_nrt_wq, &motg->sm_work);
  774. break;
  775. default:
  776. break;
  777. }
  778. }
  779. return 0;
  780. }
  781. static void msm_otg_bus_vote(struct msm_otg *motg, enum usb_bus_vote vote)
  782. {
  783. int ret;
  784. struct msm_otg_platform_data *pdata = motg->pdata;
  785. /* Check if target allows min_vote to be same as no_vote */
  786. if (pdata->bus_scale_table &&
  787. vote >= pdata->bus_scale_table->num_usecases)
  788. vote = USB_NO_PERF_VOTE;
  789. if (motg->bus_perf_client) {
  790. ret = msm_bus_scale_client_update_request(
  791. motg->bus_perf_client, vote);
  792. if (ret)
  793. dev_err(motg->phy.dev, "%s: Failed to vote (%d)\n"
  794. "for bus bw %d\n", __func__, vote, ret);
  795. }
  796. }
  797. #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
  798. #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
  799. #ifdef CONFIG_PM_SLEEP
  800. static int msm_otg_suspend(struct msm_otg *motg)
  801. {
  802. struct usb_phy *phy = &motg->phy;
  803. struct usb_bus *bus = phy->otg->host;
  804. struct msm_otg_platform_data *pdata = motg->pdata;
  805. int cnt = 0;
  806. bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
  807. bool floated_charger;
  808. u32 phy_ctrl_val = 0, cmd_val;
  809. unsigned ret;
  810. u32 portsc, config2;
  811. u32 func_ctrl;
  812. if (atomic_read(&motg->in_lpm))
  813. return 0;
  814. if (motg->pdata->delay_lpm_hndshk_on_disconnect && !msm_bam_lpm_ok())
  815. return -EBUSY;
  816. motg->ui_enabled = 0;
  817. disable_irq(motg->irq);
  818. host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
  819. !test_bit(ID, &motg->inputs);
  820. device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
  821. test_bit(A_BUS_SUSPEND, &motg->inputs) &&
  822. motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
  823. dcp = motg->chg_type == USB_DCP_CHARGER;
  824. prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
  825. floated_charger = motg->chg_type == USB_FLOATED_CHARGER;
  826. /* Enable line state difference wakeup fix for only device and host
  827. * bus suspend scenarios. Otherwise PHY can not be suspended when
  828. * a charger that pulls DP/DM high is connected.
  829. */
  830. config2 = readl_relaxed(USB_GENCONFIG2);
  831. if (device_bus_suspend)
  832. config2 |= GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
  833. else
  834. config2 &= ~GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
  835. writel_relaxed(config2, USB_GENCONFIG2);
  836. /*
  837. * Abort suspend when,
  838. * 1. charging detection in progress due to cable plug-in
  839. * 2. host mode activation in progress due to Micro-A cable insertion
  840. */
  841. if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
  842. !dcp && !prop_charger && !floated_charger) ||
  843. test_bit(A_BUS_REQ, &motg->inputs)) {
  844. motg->ui_enabled = 1;
  845. enable_irq(motg->irq);
  846. return -EBUSY;
  847. }
  848. /*
  849. * Chipidea 45-nm PHY suspend sequence:
  850. *
  851. * Interrupt Latch Register auto-clear feature is not present
  852. * in all PHY versions. Latch register is clear on read type.
  853. * Clear latch register to avoid spurious wakeup from
  854. * low power mode (LPM).
  855. *
  856. * PHY comparators are disabled when PHY enters into low power
  857. * mode (LPM). Keep PHY comparators ON in LPM only when we expect
  858. * VBUS/Id notifications from USB PHY. Otherwise turn off USB
  859. * PHY comparators. This save significant amount of power.
  860. *
  861. * PLL is not turned off when PHY enters into low power mode (LPM).
  862. * Disable PLL for maximum power savings.
  863. */
  864. if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
  865. ulpi_read(phy, 0x14);
  866. if (pdata->otg_control == OTG_PHY_CONTROL)
  867. ulpi_write(phy, 0x01, 0x30);
  868. ulpi_write(phy, 0x08, 0x09);
  869. }
  870. if (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED) {
  871. /* put the controller in non-driving mode */
  872. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  873. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  874. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  875. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  876. ulpi_write(phy, ULPI_IFC_CTRL_AUTORESUME,
  877. ULPI_CLR(ULPI_IFC_CTRL));
  878. }
  879. /* Set the PHCD bit, only if it is not set by the controller.
  880. * PHY may take some time or even fail to enter into low power
  881. * mode (LPM). Hence poll for 500 msec and reset the PHY and link
  882. * in failure case.
  883. */
  884. portsc = readl_relaxed(USB_PORTSC);
  885. if (!(portsc & PORTSC_PHCD)) {
  886. writel_relaxed(portsc | PORTSC_PHCD,
  887. USB_PORTSC);
  888. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  889. if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
  890. break;
  891. udelay(1);
  892. cnt++;
  893. }
  894. }
  895. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
  896. dev_err(phy->dev, "Unable to suspend PHY\n");
  897. msm_otg_reset(phy);
  898. motg->ui_enabled = 1;
  899. enable_irq(motg->irq);
  900. return -ETIMEDOUT;
  901. }
  902. /*
  903. * PHY has capability to generate interrupt asynchronously in low
  904. * power mode (LPM). This interrupt is level triggered. So USB IRQ
  905. * line must be disabled till async interrupt enable bit is cleared
  906. * in USBCMD register. Assert STP (ULPI interface STOP signal) to
  907. * block data communication from PHY.
  908. *
  909. * PHY retention mode is disallowed while entering to LPM with wall
  910. * charger connected. But PHY is put into suspend mode. Hence
  911. * enable asynchronous interrupt to detect charger disconnection when
  912. * PMIC notifications are unavailable.
  913. */
  914. cmd_val = readl_relaxed(USB_USBCMD);
  915. if (host_bus_suspend || device_bus_suspend ||
  916. (motg->pdata->otg_control == OTG_PHY_CONTROL))
  917. cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
  918. else
  919. cmd_val |= ULPI_STP_CTRL;
  920. writel_relaxed(cmd_val, USB_USBCMD);
  921. /*
  922. * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
  923. * PHY retention and collapse can not happen with VDP_SRC enabled.
  924. */
  925. if (motg->caps & ALLOW_PHY_RETENTION && !device_bus_suspend && !dcp &&
  926. (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION)
  927. && (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS))))) {
  928. phy_ctrl_val = readl_relaxed(motg->usb_phy_ctrl_reg);
  929. if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
  930. /* Enable PHY HV interrupts to wake MPM/Link */
  931. if ((motg->pdata->mode == USB_OTG) ||
  932. (motg->pdata->mode == USB_HOST))
  933. phy_ctrl_val |= (PHY_IDHV_INTEN |
  934. PHY_OTGSESSVLDHV_INTEN);
  935. else
  936. phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
  937. }
  938. if (host_bus_suspend)
  939. phy_ctrl_val |= (PHY_CLAMP_DPDMSE_EN |PHY_DMSE_INTEN |
  940. PHY_DPSE_INTEN);
  941. if (!(motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)) {
  942. writel_relaxed(phy_ctrl_val & ~PHY_RETEN,
  943. motg->usb_phy_ctrl_reg);
  944. motg->lpm_flags |= PHY_RETENTIONED;
  945. } else {
  946. writel_relaxed(phy_ctrl_val, motg->usb_phy_ctrl_reg);
  947. }
  948. }
  949. /* Ensure that above operation is completed before turning off clocks */
  950. mb();
  951. /* Consider clocks on workaround flag only in case of bus suspend */
  952. if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
  953. test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
  954. !motg->pdata->core_clk_always_on_workaround) {
  955. clk_disable_unprepare(motg->pclk);
  956. clk_disable_unprepare(motg->core_clk);
  957. motg->lpm_flags |= CLOCKS_DOWN;
  958. }
  959. /* usb phy no more require TCXO clock, hence vote for TCXO disable */
  960. if (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION) &&
  961. (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS)))) {
  962. if (!IS_ERR(motg->xo_clk)) {
  963. clk_disable_unprepare(motg->xo_clk);
  964. motg->lpm_flags |= XO_SHUTDOWN;
  965. } else {
  966. ret = msm_xo_mode_vote(motg->xo_handle,
  967. MSM_XO_MODE_OFF);
  968. if (ret)
  969. dev_err(phy->dev, "%s fail to devote XO %d\n",
  970. __func__, ret);
  971. else
  972. motg->lpm_flags |= XO_SHUTDOWN;
  973. }
  974. }
  975. if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
  976. !host_bus_suspend && !dcp) {
  977. msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
  978. motg->lpm_flags |= PHY_PWR_COLLAPSED;
  979. } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
  980. !host_bus_suspend && !device_bus_suspend && !dcp) {
  981. msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
  982. motg->lpm_flags |= PHY_REGULATORS_LPM;
  983. }
  984. if (motg->lpm_flags & PHY_RETENTIONED ||
  985. (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)) {
  986. msm_hsusb_config_vddcx(0);
  987. msm_hsusb_mhl_switch_enable(motg, 0);
  988. }
  989. if (device_may_wakeup(phy->dev)) {
  990. if (motg->async_irq)
  991. enable_irq_wake(motg->async_irq);
  992. else
  993. enable_irq_wake(motg->irq);
  994. if (motg->pdata->pmic_id_irq)
  995. enable_irq_wake(motg->pdata->pmic_id_irq);
  996. if (pdata->otg_control == OTG_PHY_CONTROL &&
  997. pdata->mpm_otgsessvld_int)
  998. msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
  999. if (host_bus_suspend && pdata->mpm_dpshv_int)
  1000. msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 1);
  1001. if (host_bus_suspend && pdata->mpm_dmshv_int)
  1002. msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 1);
  1003. }
  1004. if (bus)
  1005. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  1006. msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
  1007. motg->host_bus_suspend = host_bus_suspend;
  1008. atomic_set(&motg->in_lpm, 1);
  1009. /* Enable ASYNC IRQ (if present) during LPM */
  1010. if (motg->async_irq)
  1011. enable_irq(motg->async_irq);
  1012. /* XO shutdown during idle , non wakeable irqs must be disabled */
  1013. if (device_bus_suspend || host_bus_suspend || !motg->async_irq) {
  1014. motg->ui_enabled = 1;
  1015. enable_irq(motg->irq);
  1016. }
  1017. wake_unlock(&motg->wlock);
  1018. dev_info(phy->dev, "USB in low power mode\n");
  1019. return 0;
  1020. }
  1021. static int msm_otg_resume(struct msm_otg *motg)
  1022. {
  1023. struct usb_phy *phy = &motg->phy;
  1024. struct usb_bus *bus = phy->otg->host;
  1025. struct msm_otg_platform_data *pdata = motg->pdata;
  1026. int cnt = 0;
  1027. unsigned temp;
  1028. u32 phy_ctrl_val = 0;
  1029. unsigned ret;
  1030. u32 func_ctrl;
  1031. if (!atomic_read(&motg->in_lpm))
  1032. return 0;
  1033. if (motg->pdata->delay_lpm_hndshk_on_disconnect)
  1034. msm_bam_notify_lpm_resume();
  1035. if (motg->ui_enabled) {
  1036. motg->ui_enabled = 0;
  1037. disable_irq(motg->irq);
  1038. }
  1039. wake_lock(&motg->wlock);
  1040. /* Some platforms require BUS vote to enable/disable clocks */
  1041. msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
  1042. /* Vote for TCXO when waking up the phy */
  1043. if (motg->lpm_flags & XO_SHUTDOWN) {
  1044. if (!IS_ERR(motg->xo_clk)) {
  1045. clk_prepare_enable(motg->xo_clk);
  1046. } else {
  1047. ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
  1048. if (ret)
  1049. dev_err(phy->dev, "%s fail to vote for XO %d\n",
  1050. __func__, ret);
  1051. }
  1052. motg->lpm_flags &= ~XO_SHUTDOWN;
  1053. }
  1054. if (motg->lpm_flags & CLOCKS_DOWN) {
  1055. ret = clk_prepare_enable(motg->core_clk);
  1056. WARN(ret, "USB core_clk enable failed\n");
  1057. ret = clk_prepare_enable(motg->pclk);
  1058. WARN(ret, "USB pclk enable failed\n");
  1059. motg->lpm_flags &= ~CLOCKS_DOWN;
  1060. }
  1061. if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
  1062. msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
  1063. motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
  1064. } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
  1065. msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
  1066. motg->lpm_flags &= ~PHY_REGULATORS_LPM;
  1067. }
  1068. if (motg->lpm_flags & PHY_RETENTIONED ||
  1069. (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)) {
  1070. msm_hsusb_mhl_switch_enable(motg, 1);
  1071. msm_hsusb_config_vddcx(1);
  1072. phy_ctrl_val = readl_relaxed(motg->usb_phy_ctrl_reg);
  1073. phy_ctrl_val |= PHY_RETEN;
  1074. if (motg->pdata->otg_control == OTG_PHY_CONTROL)
  1075. /* Disable PHY HV interrupts */
  1076. phy_ctrl_val &=
  1077. ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
  1078. phy_ctrl_val &= ~(PHY_CLAMP_DPDMSE_EN | PHY_DMSE_INTEN |
  1079. PHY_DPSE_INTEN);
  1080. writel_relaxed(phy_ctrl_val, motg->usb_phy_ctrl_reg);
  1081. motg->lpm_flags &= ~PHY_RETENTIONED;
  1082. }
  1083. temp = readl(USB_USBCMD);
  1084. temp &= ~ASYNC_INTR_CTRL;
  1085. temp &= ~ULPI_STP_CTRL;
  1086. writel(temp, USB_USBCMD);
  1087. /*
  1088. * PHY comes out of low power mode (LPM) in case of wakeup
  1089. * from asynchronous interrupt.
  1090. */
  1091. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  1092. goto skip_phy_resume;
  1093. writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
  1094. while (cnt < PHY_RESUME_TIMEOUT_USEC) {
  1095. if (!(readl(USB_PORTSC) & PORTSC_PHCD))
  1096. break;
  1097. udelay(1);
  1098. cnt++;
  1099. }
  1100. if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
  1101. /*
  1102. * This is a fatal error. Reset the link and
  1103. * PHY. USB state can not be restored. Re-insertion
  1104. * of USB cable is the only way to get USB working.
  1105. */
  1106. dev_err(phy->dev, "Unable to resume USB."
  1107. "Re-plugin the cable\n");
  1108. msm_otg_reset(phy);
  1109. }
  1110. skip_phy_resume:
  1111. if (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED) {
  1112. /* put the controller in normal mode */
  1113. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  1114. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  1115. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  1116. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  1117. }
  1118. if (device_may_wakeup(phy->dev)) {
  1119. if (motg->async_irq)
  1120. disable_irq_wake(motg->async_irq);
  1121. else
  1122. disable_irq_wake(motg->irq);
  1123. if (motg->pdata->pmic_id_irq)
  1124. disable_irq_wake(motg->pdata->pmic_id_irq);
  1125. if (pdata->otg_control == OTG_PHY_CONTROL &&
  1126. pdata->mpm_otgsessvld_int)
  1127. msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
  1128. if (motg->host_bus_suspend && pdata->mpm_dpshv_int)
  1129. msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 0);
  1130. if (motg->host_bus_suspend && pdata->mpm_dmshv_int)
  1131. msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 0);
  1132. }
  1133. if (bus)
  1134. set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
  1135. atomic_set(&motg->in_lpm, 0);
  1136. if (motg->async_int) {
  1137. /* Match the disable_irq call from ISR */
  1138. enable_irq(motg->async_int);
  1139. motg->async_int = 0;
  1140. }
  1141. motg->ui_enabled = 1;
  1142. enable_irq(motg->irq);
  1143. /* If ASYNC IRQ is present then keep it enabled only during LPM */
  1144. if (motg->async_irq)
  1145. disable_irq(motg->async_irq);
  1146. dev_info(phy->dev, "USB exited from low power mode\n");
  1147. return 0;
  1148. }
  1149. #endif
  1150. static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
  1151. {
  1152. if (!psy) {
  1153. pr_err("No USB power supply registered!\n");
  1154. return;
  1155. }
  1156. if (legacy_power_supply) {
  1157. /* legacy support */
  1158. if (host_mode) {
  1159. power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
  1160. } else {
  1161. power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
  1162. /*
  1163. * VBUS comparator is disabled by PMIC charging driver
  1164. * when SYSTEM scope is selected. For ID_GND->ID_A
  1165. * transition, give 50 msec delay so that PMIC charger
  1166. * driver detect the VBUS and ready for accepting
  1167. * charging current value from USB.
  1168. */
  1169. if (test_bit(ID_A, &motg->inputs))
  1170. msleep(50);
  1171. }
  1172. } else {
  1173. motg->host_mode = host_mode;
  1174. power_supply_changed(psy);
  1175. }
  1176. }
  1177. #ifndef USE_MUIC_CHGTYPE
  1178. static int msm_otg_notify_chg_type(struct msm_otg *motg)
  1179. {
  1180. static int charger_type;
  1181. /*
  1182. * TODO
  1183. * Unify OTG driver charger types and power supply charger types
  1184. */
  1185. if (charger_type == motg->chg_type)
  1186. return 0;
  1187. if (motg->chg_type == USB_SDP_CHARGER)
  1188. charger_type = POWER_SUPPLY_TYPE_USB;
  1189. else if (motg->chg_type == USB_CDP_CHARGER)
  1190. charger_type = POWER_SUPPLY_TYPE_USB_CDP;
  1191. else if (motg->chg_type == USB_DCP_CHARGER ||
  1192. motg->chg_type == USB_PROPRIETARY_CHARGER ||
  1193. motg->chg_type == USB_FLOATED_CHARGER)
  1194. charger_type = POWER_SUPPLY_TYPE_USB_DCP;
  1195. else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
  1196. motg->chg_type == USB_ACA_A_CHARGER ||
  1197. motg->chg_type == USB_ACA_B_CHARGER ||
  1198. motg->chg_type == USB_ACA_C_CHARGER))
  1199. charger_type = POWER_SUPPLY_TYPE_USB_ACA;
  1200. else
  1201. charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
  1202. if (!psy) {
  1203. pr_err("No USB power supply registered!\n");
  1204. return -EINVAL;
  1205. }
  1206. pr_debug("setting usb power supply type %d\n", charger_type);
  1207. power_supply_set_supply_type(psy, charger_type);
  1208. return 0;
  1209. }
  1210. static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
  1211. {
  1212. if (!psy) {
  1213. dev_dbg(motg->phy.dev, "no usb power supply registered\n");
  1214. goto psy_error;
  1215. }
  1216. if (motg->cur_power == 0 && mA > 2) {
  1217. /* Enable charging */
  1218. if (power_supply_set_online(psy, true))
  1219. goto psy_error;
  1220. if (power_supply_set_current_limit(psy, 1000*mA))
  1221. goto psy_error;
  1222. } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
  1223. /* Disable charging */
  1224. if (power_supply_set_online(psy, false))
  1225. goto psy_error;
  1226. /* Set max current limit */
  1227. if (power_supply_set_current_limit(psy, 0))
  1228. goto psy_error;
  1229. } else {
  1230. if (power_supply_set_online(psy, true))
  1231. goto psy_error;
  1232. /* Current has changed (100/2 --> 500) */
  1233. if (power_supply_set_current_limit(psy, 1000*mA))
  1234. goto psy_error;
  1235. }
  1236. power_supply_changed(psy);
  1237. return 0;
  1238. psy_error:
  1239. dev_dbg(motg->phy.dev, "power supply error when setting property\n");
  1240. return -ENXIO;
  1241. }
  1242. #endif
  1243. #ifndef USE_MUIC_CHGTYPE
  1244. static void msm_otg_set_online_status(struct msm_otg *motg)
  1245. {
  1246. if (!psy)
  1247. dev_dbg(motg->phy.dev, "no usb power supply registered\n");
  1248. /* Set power supply online status to false */
  1249. if (power_supply_set_online(psy, false))
  1250. dev_dbg(motg->phy.dev, "error setting power supply property\n");
  1251. }
  1252. #endif
  1253. static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
  1254. {
  1255. #ifndef USE_MUIC_CHGTYPE
  1256. struct usb_gadget *g = motg->phy.otg->gadget;
  1257. if (g && g->is_a_peripheral)
  1258. return;
  1259. if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
  1260. motg->chg_type == USB_ACA_A_CHARGER ||
  1261. motg->chg_type == USB_ACA_B_CHARGER ||
  1262. motg->chg_type == USB_ACA_C_CHARGER) &&
  1263. mA > IDEV_ACA_CHG_LIMIT)
  1264. mA = IDEV_ACA_CHG_LIMIT;
  1265. if (msm_otg_notify_chg_type(motg))
  1266. dev_err(motg->phy.dev,
  1267. "Failed notifying %d charger type to PMIC\n",
  1268. motg->chg_type);
  1269. /*
  1270. * This condition will be true when usb cable is disconnected
  1271. * during bootup before charger detection mechanism starts.
  1272. */
  1273. if (motg->online && motg->cur_power == 0 && mA == 0)
  1274. msm_otg_set_online_status(motg);
  1275. if (motg->cur_power == mA)
  1276. return;
  1277. dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
  1278. /*
  1279. * Use Power Supply API if supported, otherwise fallback
  1280. * to legacy pm8921 API.
  1281. */
  1282. if (msm_otg_notify_power_supply(motg, mA))
  1283. pm8921_charger_vbus_draw(mA);
  1284. motg->cur_power = mA;
  1285. #endif
  1286. }
  1287. static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
  1288. {
  1289. struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
  1290. /*
  1291. * Gadget driver uses set_power method to notify about the
  1292. * available current based on suspend/configured states.
  1293. *
  1294. * IDEV_CHG can be drawn irrespective of suspend/un-configured
  1295. * states when CDP/ACA is connected.
  1296. */
  1297. if (motg->chg_type == USB_SDP_CHARGER)
  1298. msm_otg_notify_charger(motg, mA);
  1299. return 0;
  1300. }
  1301. static void msm_otg_start_host(struct usb_otg *otg, int on)
  1302. {
  1303. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  1304. struct msm_otg_platform_data *pdata = motg->pdata;
  1305. struct usb_hcd *hcd;
  1306. if (!otg->host)
  1307. return;
  1308. #ifdef CONFIG_USB_HOST_NOTIFY
  1309. msm_otg_host_notify(motg, on);
  1310. #endif
  1311. hcd = bus_to_hcd(otg->host);
  1312. if (on) {
  1313. dev_dbg(otg->phy->dev, "host on\n");
  1314. if (pdata->otg_control == OTG_PHY_CONTROL)
  1315. ulpi_write(otg->phy, OTG_COMP_DISABLE,
  1316. ULPI_SET(ULPI_PWR_CLK_MNG_REG));
  1317. /*
  1318. * Some boards have a switch cotrolled by gpio
  1319. * to enable/disable internal HUB. Enable internal
  1320. * HUB before kicking the host.
  1321. */
  1322. if (pdata->setup_gpio)
  1323. pdata->setup_gpio(OTG_STATE_A_HOST);
  1324. usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
  1325. } else {
  1326. dev_dbg(otg->phy->dev, "host off\n");
  1327. usb_remove_hcd(hcd);
  1328. /* HCD core reset all bits of PORTSC. select ULPI phy */
  1329. writel_relaxed(0x80000000, USB_PORTSC);
  1330. if (pdata->setup_gpio)
  1331. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  1332. if (pdata->otg_control == OTG_PHY_CONTROL)
  1333. ulpi_write(otg->phy, OTG_COMP_DISABLE,
  1334. ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
  1335. }
  1336. }
  1337. static int msm_otg_usbdev_notify(struct notifier_block *self,
  1338. unsigned long action, void *priv)
  1339. {
  1340. struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
  1341. struct usb_otg *otg = motg->phy.otg;
  1342. struct usb_device *udev = priv;
  1343. if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
  1344. goto out;
  1345. if (udev->bus != otg->host)
  1346. goto out;
  1347. /*
  1348. * Interested in devices connected directly to the root hub.
  1349. * ACA dock can supply IDEV_CHG irrespective devices connected
  1350. * on the accessory port.
  1351. */
  1352. if (!udev->parent || udev->parent->parent ||
  1353. motg->chg_type == USB_ACA_DOCK_CHARGER)
  1354. goto out;
  1355. switch (action) {
  1356. case USB_DEVICE_ADD:
  1357. if (aca_enabled())
  1358. usb_disable_autosuspend(udev);
  1359. if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
  1360. pr_debug("B_CONN set\n");
  1361. set_bit(B_CONN, &motg->inputs);
  1362. msm_otg_del_timer(motg);
  1363. otg->phy->state = OTG_STATE_A_HOST;
  1364. /*
  1365. * OTG PET: A-device must end session within
  1366. * 10 sec after PET enumeration.
  1367. */
  1368. if (udev->quirks & USB_QUIRK_OTG_PET)
  1369. msm_otg_start_timer(motg, TA_TST_MAINT,
  1370. A_TST_MAINT);
  1371. }
  1372. /* fall through */
  1373. case USB_DEVICE_CONFIG:
  1374. if (udev->actconfig)
  1375. motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
  1376. else
  1377. motg->mA_port = IUNIT;
  1378. if (otg->phy->state == OTG_STATE_B_HOST)
  1379. msm_otg_del_timer(motg);
  1380. break;
  1381. case USB_DEVICE_REMOVE:
  1382. if ((otg->phy->state == OTG_STATE_A_HOST) ||
  1383. (otg->phy->state == OTG_STATE_A_SUSPEND)) {
  1384. pr_debug("B_CONN clear\n");
  1385. clear_bit(B_CONN, &motg->inputs);
  1386. /*
  1387. * OTG PET: A-device must end session after
  1388. * PET disconnection if it is enumerated
  1389. * with bcdDevice[0] = 1. USB core sets
  1390. * bus->otg_vbus_off for us. clear it here.
  1391. */
  1392. if (udev->bus->otg_vbus_off) {
  1393. udev->bus->otg_vbus_off = 0;
  1394. set_bit(A_BUS_DROP, &motg->inputs);
  1395. }
  1396. queue_work(system_nrt_wq, &motg->sm_work);
  1397. }
  1398. default:
  1399. break;
  1400. }
  1401. if (test_bit(ID_A, &motg->inputs))
  1402. msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
  1403. motg->mA_port);
  1404. out:
  1405. return NOTIFY_OK;
  1406. }
  1407. static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
  1408. {
  1409. int ret;
  1410. static bool vbus_is_on;
  1411. if (vbus_is_on == on)
  1412. return;
  1413. #ifdef CONFIG_USB_HOST_NOTIFY
  1414. if (motg->smartdock)
  1415. return;
  1416. #endif
  1417. if (motg->pdata->vbus_power) {
  1418. pr_info("msm_otg: pdata->vbus_power : %d\n", on);
  1419. ret = motg->pdata->vbus_power(on);
  1420. if (!ret)
  1421. vbus_is_on = on;
  1422. #ifdef CONFIG_USB_HOST_NOTIFY
  1423. else
  1424. schedule_delayed_work(&motg->late_power_work,
  1425. (1000 * HZ / 1000));
  1426. #endif
  1427. return;
  1428. }
  1429. if (!vbus_otg) {
  1430. pr_err("vbus_otg is NULL.");
  1431. return;
  1432. }
  1433. /*
  1434. * if entering host mode tell the charger to not draw any current
  1435. * from usb before turning on the boost.
  1436. * if exiting host mode disable the boost before enabling to draw
  1437. * current from the source.
  1438. */
  1439. if (on) {
  1440. msm_otg_notify_host_mode(motg, on);
  1441. ret = regulator_enable(vbus_otg);
  1442. if (ret) {
  1443. pr_err("unable to enable vbus_otg\n");
  1444. return;
  1445. }
  1446. vbus_is_on = true;
  1447. } else {
  1448. ret = regulator_disable(vbus_otg);
  1449. if (ret) {
  1450. pr_err("unable to disable vbus_otg\n");
  1451. return;
  1452. }
  1453. msm_otg_notify_host_mode(motg, on);
  1454. vbus_is_on = false;
  1455. }
  1456. #ifdef CONFIG_USB_HOST_NOTIFY
  1457. if (motg->pdata->ovp_ctrl_gpio) {
  1458. gpio_set_value(motg->pdata->ovp_ctrl_gpio, on);
  1459. pr_info("ovp_ctrl : %d\n",
  1460. gpio_get_value(motg->pdata->ovp_ctrl_gpio));
  1461. }
  1462. #endif
  1463. }
  1464. static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
  1465. {
  1466. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  1467. struct usb_hcd *hcd;
  1468. /*
  1469. * Fail host registration if this board can support
  1470. * only peripheral configuration.
  1471. */
  1472. if (motg->pdata->mode == USB_PERIPHERAL) {
  1473. dev_info(otg->phy->dev, "Host mode is not supported\n");
  1474. return -ENODEV;
  1475. }
  1476. if (!motg->pdata->vbus_power && host) {
  1477. vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
  1478. if (IS_ERR(vbus_otg)) {
  1479. pr_err("Unable to get vbus_otg\n");
  1480. return PTR_ERR(vbus_otg);
  1481. }
  1482. }
  1483. if (!host) {
  1484. if (otg->phy->state == OTG_STATE_A_HOST) {
  1485. pm_runtime_get_sync(otg->phy->dev);
  1486. usb_unregister_notify(&motg->usbdev_nb);
  1487. msm_otg_start_host(otg, 0);
  1488. msm_hsusb_vbus_power(motg, 0);
  1489. otg->host = NULL;
  1490. otg->phy->state = OTG_STATE_UNDEFINED;
  1491. queue_work(system_nrt_wq, &motg->sm_work);
  1492. } else {
  1493. otg->host = NULL;
  1494. }
  1495. return 0;
  1496. }
  1497. hcd = bus_to_hcd(host);
  1498. hcd->power_budget = motg->pdata->power_budget;
  1499. #ifdef CONFIG_USB_OTG
  1500. host->otg_port = 1;
  1501. #endif
  1502. motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
  1503. usb_register_notify(&motg->usbdev_nb);
  1504. otg->host = host;
  1505. dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
  1506. /*
  1507. * Kick the state machine work, if peripheral is not supported
  1508. * or peripheral is already registered with us.
  1509. */
  1510. if (motg->pdata->mode == USB_HOST || otg->gadget) {
  1511. pm_runtime_get_sync(otg->phy->dev);
  1512. queue_work(system_nrt_wq, &motg->sm_work);
  1513. }
  1514. return 0;
  1515. }
  1516. static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
  1517. {
  1518. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  1519. struct msm_otg_platform_data *pdata = motg->pdata;
  1520. if (!otg->gadget)
  1521. return;
  1522. if (on) {
  1523. dev_dbg(otg->phy->dev, "gadget on\n");
  1524. /*
  1525. * Some boards have a switch cotrolled by gpio
  1526. * to enable/disable internal HUB. Disable internal
  1527. * HUB before kicking the gadget.
  1528. */
  1529. if (pdata->setup_gpio)
  1530. pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
  1531. /* Configure BUS performance parameters for MAX bandwidth */
  1532. if (debug_bus_voting_enabled)
  1533. msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
  1534. usb_gadget_vbus_connect(otg->gadget);
  1535. } else {
  1536. dev_dbg(otg->phy->dev, "gadget off\n");
  1537. usb_gadget_vbus_disconnect(otg->gadget);
  1538. /* Configure BUS performance parameters to default */
  1539. msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
  1540. if (pdata->setup_gpio)
  1541. pdata->setup_gpio(OTG_STATE_UNDEFINED);
  1542. }
  1543. }
  1544. static int msm_otg_set_peripheral(struct usb_otg *otg,
  1545. struct usb_gadget *gadget)
  1546. {
  1547. struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
  1548. /*
  1549. * Fail peripheral registration if this board can support
  1550. * only host configuration.
  1551. */
  1552. if (motg->pdata->mode == USB_HOST) {
  1553. dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
  1554. return -ENODEV;
  1555. }
  1556. if (!gadget) {
  1557. if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
  1558. pm_runtime_get_sync(otg->phy->dev);
  1559. msm_otg_start_peripheral(otg, 0);
  1560. otg->gadget = NULL;
  1561. otg->phy->state = OTG_STATE_UNDEFINED;
  1562. queue_work(system_nrt_wq, &motg->sm_work);
  1563. } else {
  1564. otg->gadget = NULL;
  1565. }
  1566. return 0;
  1567. }
  1568. otg->gadget = gadget;
  1569. dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
  1570. /*
  1571. * Kick the state machine work, if host is not supported
  1572. * or host is already registered with us.
  1573. */
  1574. if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
  1575. pm_runtime_get_sync(otg->phy->dev);
  1576. queue_work(system_nrt_wq, &motg->sm_work);
  1577. }
  1578. return 0;
  1579. }
  1580. static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
  1581. {
  1582. unsigned long flags;
  1583. int id;
  1584. if (!motg->pdata->pmic_id_irq)
  1585. return -ENODEV;
  1586. local_irq_save(flags);
  1587. id = irq_read_line(motg->pdata->pmic_id_irq);
  1588. local_irq_restore(flags);
  1589. /*
  1590. * If we can not read ID line state for some reason, treat
  1591. * it as float. This would prevent MHL discovery and kicking
  1592. * host mode unnecessarily.
  1593. */
  1594. return !!id;
  1595. }
  1596. static int msm_otg_mhl_register_callback(struct msm_otg *motg,
  1597. void (*callback)(int on))
  1598. {
  1599. struct usb_phy *phy = &motg->phy;
  1600. int ret;
  1601. if (!motg->pdata->mhl_enable) {
  1602. dev_dbg(phy->dev, "MHL feature not enabled\n");
  1603. return -ENODEV;
  1604. }
  1605. if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
  1606. !motg->pdata->pmic_id_irq) {
  1607. dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
  1608. return -ENODEV;
  1609. }
  1610. if (!motg->pdata->mhl_dev_name) {
  1611. dev_dbg(phy->dev, "MHL device name does not exist.\n");
  1612. return -ENODEV;
  1613. }
  1614. if (callback)
  1615. ret = mhl_register_callback(motg->pdata->mhl_dev_name,
  1616. callback);
  1617. else
  1618. ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
  1619. if (ret)
  1620. dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
  1621. motg->pdata->mhl_dev_name, ret);
  1622. else
  1623. motg->mhl_enabled = true;
  1624. return ret;
  1625. }
  1626. static void msm_otg_mhl_notify_online(int on)
  1627. {
  1628. struct msm_otg *motg = the_msm_otg;
  1629. struct usb_phy *phy = &motg->phy;
  1630. bool queue = false;
  1631. dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
  1632. if (on) {
  1633. set_bit(MHL, &motg->inputs);
  1634. } else {
  1635. clear_bit(MHL, &motg->inputs);
  1636. queue = true;
  1637. }
  1638. if (queue && phy->state != OTG_STATE_UNDEFINED)
  1639. schedule_work(&motg->sm_work);
  1640. }
  1641. static bool msm_otg_is_mhl(struct msm_otg *motg)
  1642. {
  1643. struct usb_phy *phy = &motg->phy;
  1644. int is_mhl, ret;
  1645. ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
  1646. if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
  1647. /*
  1648. * MHL driver calls our callback saying that MHL connected
  1649. * if RID_GND is detected. But at later part of discovery
  1650. * it may figure out MHL is not connected and returns
  1651. * false. Hence clear MHL input here.
  1652. */
  1653. clear_bit(MHL, &motg->inputs);
  1654. dev_dbg(phy->dev, "MHL device not found\n");
  1655. return false;
  1656. }
  1657. set_bit(MHL, &motg->inputs);
  1658. dev_dbg(phy->dev, "MHL device found\n");
  1659. return true;
  1660. }
  1661. static bool msm_chg_mhl_detect(struct msm_otg *motg)
  1662. {
  1663. bool ret, id;
  1664. if (!motg->mhl_enabled)
  1665. return false;
  1666. id = msm_otg_read_pmic_id_state(motg);
  1667. if (id)
  1668. return false;
  1669. mhl_det_in_progress = true;
  1670. ret = msm_otg_is_mhl(motg);
  1671. mhl_det_in_progress = false;
  1672. return ret;
  1673. }
  1674. static void msm_otg_chg_check_timer_func(unsigned long data)
  1675. {
  1676. struct msm_otg *motg = (struct msm_otg *) data;
  1677. struct usb_otg *otg = motg->phy.otg;
  1678. if (atomic_read(&motg->in_lpm) ||
  1679. !test_bit(B_SESS_VLD, &motg->inputs) ||
  1680. otg->phy->state != OTG_STATE_B_PERIPHERAL ||
  1681. otg->gadget->speed != USB_SPEED_UNKNOWN) {
  1682. dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
  1683. return;
  1684. }
  1685. if (((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) || poweroff_charging) {
  1686. dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
  1687. set_bit(B_FALSE_SDP, &motg->inputs);
  1688. queue_work(system_nrt_wq, &motg->sm_work);
  1689. }
  1690. }
  1691. static bool msm_chg_aca_detect(struct msm_otg *motg)
  1692. {
  1693. struct usb_phy *phy = &motg->phy;
  1694. u32 int_sts;
  1695. bool ret = false;
  1696. if (!aca_enabled())
  1697. goto out;
  1698. if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
  1699. goto out;
  1700. int_sts = ulpi_read(phy, 0x87);
  1701. switch (int_sts & 0x1C) {
  1702. case 0x08:
  1703. if (!test_and_set_bit(ID_A, &motg->inputs)) {
  1704. dev_dbg(phy->dev, "ID_A\n");
  1705. motg->chg_type = USB_ACA_A_CHARGER;
  1706. motg->chg_state = USB_CHG_STATE_DETECTED;
  1707. clear_bit(ID_B, &motg->inputs);
  1708. clear_bit(ID_C, &motg->inputs);
  1709. set_bit(ID, &motg->inputs);
  1710. ret = true;
  1711. }
  1712. break;
  1713. case 0x0C:
  1714. if (!test_and_set_bit(ID_B, &motg->inputs)) {
  1715. dev_dbg(phy->dev, "ID_B\n");
  1716. motg->chg_type = USB_ACA_B_CHARGER;
  1717. motg->chg_state = USB_CHG_STATE_DETECTED;
  1718. clear_bit(ID_A, &motg->inputs);
  1719. clear_bit(ID_C, &motg->inputs);
  1720. set_bit(ID, &motg->inputs);
  1721. ret = true;
  1722. }
  1723. break;
  1724. case 0x10:
  1725. if (!test_and_set_bit(ID_C, &motg->inputs)) {
  1726. dev_dbg(phy->dev, "ID_C\n");
  1727. motg->chg_type = USB_ACA_C_CHARGER;
  1728. motg->chg_state = USB_CHG_STATE_DETECTED;
  1729. clear_bit(ID_A, &motg->inputs);
  1730. clear_bit(ID_B, &motg->inputs);
  1731. set_bit(ID, &motg->inputs);
  1732. ret = true;
  1733. }
  1734. break;
  1735. case 0x04:
  1736. if (test_and_clear_bit(ID, &motg->inputs)) {
  1737. dev_dbg(phy->dev, "ID_GND\n");
  1738. motg->chg_type = USB_INVALID_CHARGER;
  1739. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1740. clear_bit(ID_A, &motg->inputs);
  1741. clear_bit(ID_B, &motg->inputs);
  1742. clear_bit(ID_C, &motg->inputs);
  1743. ret = true;
  1744. }
  1745. break;
  1746. default:
  1747. ret = test_and_clear_bit(ID_A, &motg->inputs) |
  1748. test_and_clear_bit(ID_B, &motg->inputs) |
  1749. test_and_clear_bit(ID_C, &motg->inputs) |
  1750. !test_and_set_bit(ID, &motg->inputs);
  1751. if (ret) {
  1752. dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
  1753. motg->chg_type = USB_INVALID_CHARGER;
  1754. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  1755. }
  1756. }
  1757. out:
  1758. return ret;
  1759. }
  1760. static void msm_chg_enable_aca_det(struct msm_otg *motg)
  1761. {
  1762. struct usb_phy *phy = &motg->phy;
  1763. if (!aca_enabled())
  1764. return;
  1765. switch (motg->pdata->phy_type) {
  1766. case SNPS_28NM_INTEGRATED_PHY:
  1767. /* Disable ID_GND in link and PHY */
  1768. writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
  1769. OTGSC_IDIE), USB_OTGSC);
  1770. ulpi_write(phy, 0x01, 0x0C);
  1771. ulpi_write(phy, 0x10, 0x0F);
  1772. ulpi_write(phy, 0x10, 0x12);
  1773. /* Disable PMIC ID pull-up */
  1774. pm8xxx_usb_id_pullup(0);
  1775. /* Enable ACA ID detection */
  1776. ulpi_write(phy, 0x20, 0x85);
  1777. aca_id_turned_on = true;
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. }
  1783. static void msm_chg_enable_aca_intr(struct msm_otg *motg)
  1784. {
  1785. struct usb_phy *phy = &motg->phy;
  1786. if (!aca_enabled())
  1787. return;
  1788. switch (motg->pdata->phy_type) {
  1789. case SNPS_28NM_INTEGRATED_PHY:
  1790. /* Enable ACA Detection interrupt (on any RID change) */
  1791. ulpi_write(phy, 0x01, 0x94);
  1792. break;
  1793. default:
  1794. break;
  1795. }
  1796. }
  1797. static void msm_chg_disable_aca_intr(struct msm_otg *motg)
  1798. {
  1799. struct usb_phy *phy = &motg->phy;
  1800. if (!aca_enabled())
  1801. return;
  1802. switch (motg->pdata->phy_type) {
  1803. case SNPS_28NM_INTEGRATED_PHY:
  1804. ulpi_write(phy, 0x01, 0x95);
  1805. break;
  1806. default:
  1807. break;
  1808. }
  1809. }
  1810. static bool msm_chg_check_aca_intr(struct msm_otg *motg)
  1811. {
  1812. struct usb_phy *phy = &motg->phy;
  1813. bool ret = false;
  1814. if (!aca_enabled())
  1815. return ret;
  1816. switch (motg->pdata->phy_type) {
  1817. case SNPS_28NM_INTEGRATED_PHY:
  1818. if (ulpi_read(phy, 0x91) & 1) {
  1819. dev_dbg(phy->dev, "RID change\n");
  1820. ulpi_write(phy, 0x01, 0x92);
  1821. ret = msm_chg_aca_detect(motg);
  1822. }
  1823. default:
  1824. break;
  1825. }
  1826. return ret;
  1827. }
  1828. static void msm_otg_id_timer_func(unsigned long data)
  1829. {
  1830. struct msm_otg *motg = (struct msm_otg *) data;
  1831. if (!aca_enabled())
  1832. return;
  1833. if (atomic_read(&motg->in_lpm)) {
  1834. dev_dbg(motg->phy.dev, "timer: in lpm\n");
  1835. return;
  1836. }
  1837. if (motg->phy.state == OTG_STATE_A_SUSPEND)
  1838. goto out;
  1839. if (msm_chg_check_aca_intr(motg)) {
  1840. dev_dbg(motg->phy.dev, "timer: aca work\n");
  1841. queue_work(system_nrt_wq, &motg->sm_work);
  1842. }
  1843. out:
  1844. if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
  1845. mod_timer(&motg->id_timer, ID_TIMER_FREQ);
  1846. }
  1847. static bool msm_chg_check_secondary_det(struct msm_otg *motg)
  1848. {
  1849. struct usb_phy *phy = &motg->phy;
  1850. u32 chg_det;
  1851. bool ret = false;
  1852. switch (motg->pdata->phy_type) {
  1853. case CI_45NM_INTEGRATED_PHY:
  1854. chg_det = ulpi_read(phy, 0x34);
  1855. ret = chg_det & (1 << 4);
  1856. break;
  1857. case SNPS_28NM_INTEGRATED_PHY:
  1858. chg_det = ulpi_read(phy, 0x87);
  1859. ret = chg_det & 1;
  1860. break;
  1861. default:
  1862. break;
  1863. }
  1864. return ret;
  1865. }
  1866. static void msm_chg_enable_secondary_det(struct msm_otg *motg)
  1867. {
  1868. struct usb_phy *phy = &motg->phy;
  1869. u32 chg_det;
  1870. switch (motg->pdata->phy_type) {
  1871. case CI_45NM_INTEGRATED_PHY:
  1872. chg_det = ulpi_read(phy, 0x34);
  1873. /* Turn off charger block */
  1874. chg_det |= ~(1 << 1);
  1875. ulpi_write(phy, chg_det, 0x34);
  1876. udelay(20);
  1877. /* control chg block via ULPI */
  1878. chg_det &= ~(1 << 3);
  1879. ulpi_write(phy, chg_det, 0x34);
  1880. /* put it in host mode for enabling D- source */
  1881. chg_det &= ~(1 << 2);
  1882. ulpi_write(phy, chg_det, 0x34);
  1883. /* Turn on chg detect block */
  1884. chg_det &= ~(1 << 1);
  1885. ulpi_write(phy, chg_det, 0x34);
  1886. udelay(20);
  1887. /* enable chg detection */
  1888. chg_det &= ~(1 << 0);
  1889. ulpi_write(phy, chg_det, 0x34);
  1890. break;
  1891. case SNPS_28NM_INTEGRATED_PHY:
  1892. /*
  1893. * Configure DM as current source, DP as current sink
  1894. * and enable battery charging comparators.
  1895. */
  1896. ulpi_write(phy, 0x8, 0x85);
  1897. ulpi_write(phy, 0x2, 0x85);
  1898. ulpi_write(phy, 0x1, 0x85);
  1899. break;
  1900. default:
  1901. break;
  1902. }
  1903. }
  1904. static bool msm_chg_check_primary_det(struct msm_otg *motg)
  1905. {
  1906. struct usb_phy *phy = &motg->phy;
  1907. u32 chg_det;
  1908. bool ret = false;
  1909. switch (motg->pdata->phy_type) {
  1910. case CI_45NM_INTEGRATED_PHY:
  1911. chg_det = ulpi_read(phy, 0x34);
  1912. ret = chg_det & (1 << 4);
  1913. break;
  1914. case SNPS_28NM_INTEGRATED_PHY:
  1915. chg_det = ulpi_read(phy, 0x87);
  1916. ret = chg_det & 1;
  1917. /* Turn off VDP_SRC */
  1918. ulpi_write(phy, 0x3, 0x86);
  1919. msleep(20);
  1920. break;
  1921. default:
  1922. break;
  1923. }
  1924. return ret;
  1925. }
  1926. static void msm_chg_enable_primary_det(struct msm_otg *motg)
  1927. {
  1928. struct usb_phy *phy = &motg->phy;
  1929. u32 chg_det;
  1930. switch (motg->pdata->phy_type) {
  1931. case CI_45NM_INTEGRATED_PHY:
  1932. chg_det = ulpi_read(phy, 0x34);
  1933. /* enable chg detection */
  1934. chg_det &= ~(1 << 0);
  1935. ulpi_write(phy, chg_det, 0x34);
  1936. break;
  1937. case SNPS_28NM_INTEGRATED_PHY:
  1938. /*
  1939. * Configure DP as current source, DM as current sink
  1940. * and enable battery charging comparators.
  1941. */
  1942. ulpi_write(phy, 0x2, 0x85);
  1943. ulpi_write(phy, 0x1, 0x85);
  1944. break;
  1945. default:
  1946. break;
  1947. }
  1948. }
  1949. static bool msm_chg_check_dcd(struct msm_otg *motg)
  1950. {
  1951. struct usb_phy *phy = &motg->phy;
  1952. u32 line_state;
  1953. bool ret = false;
  1954. switch (motg->pdata->phy_type) {
  1955. case CI_45NM_INTEGRATED_PHY:
  1956. line_state = ulpi_read(phy, 0x15);
  1957. ret = !(line_state & 1);
  1958. break;
  1959. case SNPS_28NM_INTEGRATED_PHY:
  1960. line_state = ulpi_read(phy, 0x87);
  1961. ret = line_state & 2;
  1962. break;
  1963. default:
  1964. break;
  1965. }
  1966. return ret;
  1967. }
  1968. static void msm_chg_disable_dcd(struct msm_otg *motg)
  1969. {
  1970. struct usb_phy *phy = &motg->phy;
  1971. u32 chg_det;
  1972. switch (motg->pdata->phy_type) {
  1973. case CI_45NM_INTEGRATED_PHY:
  1974. chg_det = ulpi_read(phy, 0x34);
  1975. chg_det &= ~(1 << 5);
  1976. ulpi_write(phy, chg_det, 0x34);
  1977. break;
  1978. case SNPS_28NM_INTEGRATED_PHY:
  1979. ulpi_write(phy, 0x10, 0x86);
  1980. break;
  1981. default:
  1982. break;
  1983. }
  1984. }
  1985. static void msm_chg_enable_dcd(struct msm_otg *motg)
  1986. {
  1987. struct usb_phy *phy = &motg->phy;
  1988. u32 chg_det;
  1989. switch (motg->pdata->phy_type) {
  1990. case CI_45NM_INTEGRATED_PHY:
  1991. chg_det = ulpi_read(phy, 0x34);
  1992. /* Turn on D+ current source */
  1993. chg_det |= (1 << 5);
  1994. ulpi_write(phy, chg_det, 0x34);
  1995. break;
  1996. case SNPS_28NM_INTEGRATED_PHY:
  1997. /* Data contact detection enable */
  1998. ulpi_write(phy, 0x10, 0x85);
  1999. break;
  2000. default:
  2001. break;
  2002. }
  2003. }
  2004. static void msm_chg_block_on(struct msm_otg *motg)
  2005. {
  2006. struct usb_phy *phy = &motg->phy;
  2007. u32 func_ctrl, chg_det;
  2008. /* put the controller in non-driving mode */
  2009. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  2010. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  2011. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
  2012. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  2013. switch (motg->pdata->phy_type) {
  2014. case CI_45NM_INTEGRATED_PHY:
  2015. chg_det = ulpi_read(phy, 0x34);
  2016. /* control chg block via ULPI */
  2017. chg_det &= ~(1 << 3);
  2018. ulpi_write(phy, chg_det, 0x34);
  2019. /* Turn on chg detect block */
  2020. chg_det &= ~(1 << 1);
  2021. ulpi_write(phy, chg_det, 0x34);
  2022. udelay(20);
  2023. break;
  2024. case SNPS_28NM_INTEGRATED_PHY:
  2025. /* disable DP and DM pull down resistors */
  2026. ulpi_write(phy, 0x6, 0xC);
  2027. /* Clear charger detecting control bits */
  2028. ulpi_write(phy, 0x1F, 0x86);
  2029. /* Clear alt interrupt latch and enable bits */
  2030. ulpi_write(phy, 0x1F, 0x92);
  2031. ulpi_write(phy, 0x1F, 0x95);
  2032. udelay(100);
  2033. break;
  2034. default:
  2035. break;
  2036. }
  2037. }
  2038. static void msm_chg_block_off(struct msm_otg *motg)
  2039. {
  2040. struct usb_phy *phy = &motg->phy;
  2041. u32 func_ctrl, chg_det;
  2042. switch (motg->pdata->phy_type) {
  2043. case CI_45NM_INTEGRATED_PHY:
  2044. chg_det = ulpi_read(phy, 0x34);
  2045. /* Turn off charger block */
  2046. chg_det |= ~(1 << 1);
  2047. ulpi_write(phy, chg_det, 0x34);
  2048. break;
  2049. case SNPS_28NM_INTEGRATED_PHY:
  2050. /* Clear charger detecting control bits */
  2051. ulpi_write(phy, 0x3F, 0x86);
  2052. /* Clear alt interrupt latch and enable bits */
  2053. ulpi_write(phy, 0x1F, 0x92);
  2054. ulpi_write(phy, 0x1F, 0x95);
  2055. /* re-enable DP and DM pull down resistors */
  2056. ulpi_write(phy, 0x6, 0xB);
  2057. break;
  2058. default:
  2059. break;
  2060. }
  2061. /* put the controller in normal mode */
  2062. func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
  2063. func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
  2064. func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
  2065. ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
  2066. }
  2067. static const char *chg_to_string(enum usb_chg_type chg_type)
  2068. {
  2069. switch (chg_type) {
  2070. case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
  2071. case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
  2072. case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
  2073. case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
  2074. case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
  2075. case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
  2076. case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
  2077. case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
  2078. case USB_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
  2079. default: return "INVALID_CHARGER";
  2080. }
  2081. }
  2082. #define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
  2083. #define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
  2084. #define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
  2085. #define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
  2086. static void msm_chg_detect_work(struct work_struct *w)
  2087. {
  2088. struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
  2089. struct usb_phy *phy = &motg->phy;
  2090. bool is_dcd = false, tmout, vout, is_aca;
  2091. static bool dcd;
  2092. u32 line_state, dm_vlgc;
  2093. unsigned long delay;
  2094. dev_dbg(phy->dev, "chg detection work\n");
  2095. if (test_bit(MHL, &motg->inputs)) {
  2096. dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
  2097. return;
  2098. }
  2099. switch (motg->chg_state) {
  2100. case USB_CHG_STATE_UNDEFINED:
  2101. msm_chg_block_on(motg);
  2102. msm_chg_enable_dcd(motg);
  2103. msm_chg_enable_aca_det(motg);
  2104. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  2105. motg->dcd_time = 0;
  2106. delay = MSM_CHG_DCD_POLL_TIME;
  2107. break;
  2108. case USB_CHG_STATE_WAIT_FOR_DCD:
  2109. if (msm_chg_mhl_detect(motg)) {
  2110. msm_chg_block_off(motg);
  2111. motg->chg_state = USB_CHG_STATE_DETECTED;
  2112. motg->chg_type = USB_INVALID_CHARGER;
  2113. queue_work(system_nrt_wq, &motg->sm_work);
  2114. return;
  2115. }
  2116. is_aca = msm_chg_aca_detect(motg);
  2117. if (is_aca) {
  2118. /*
  2119. * ID_A can be ACA dock too. continue
  2120. * primary detection after DCD.
  2121. */
  2122. if (test_bit(ID_A, &motg->inputs)) {
  2123. motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
  2124. } else {
  2125. delay = 0;
  2126. break;
  2127. }
  2128. }
  2129. is_dcd = msm_chg_check_dcd(motg);
  2130. motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
  2131. tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
  2132. if (is_dcd || tmout) {
  2133. if (is_dcd)
  2134. dcd = true;
  2135. else
  2136. dcd = false;
  2137. msm_chg_disable_dcd(motg);
  2138. msm_chg_enable_primary_det(motg);
  2139. delay = MSM_CHG_PRIMARY_DET_TIME;
  2140. motg->chg_state = USB_CHG_STATE_DCD_DONE;
  2141. } else {
  2142. delay = MSM_CHG_DCD_POLL_TIME;
  2143. }
  2144. break;
  2145. case USB_CHG_STATE_DCD_DONE:
  2146. vout = msm_chg_check_primary_det(motg);
  2147. line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
  2148. dm_vlgc = line_state & PORTSC_LS_DM;
  2149. if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
  2150. if (test_bit(ID_A, &motg->inputs)) {
  2151. motg->chg_type = USB_ACA_DOCK_CHARGER;
  2152. motg->chg_state = USB_CHG_STATE_DETECTED;
  2153. delay = 0;
  2154. break;
  2155. }
  2156. if (line_state) { /* DP > VLGC */
  2157. motg->chg_type = USB_PROPRIETARY_CHARGER;
  2158. motg->chg_state = USB_CHG_STATE_DETECTED;
  2159. delay = 0;
  2160. } else {
  2161. msm_chg_enable_secondary_det(motg);
  2162. delay = MSM_CHG_SECONDARY_DET_TIME;
  2163. motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
  2164. }
  2165. } else { /* DM < VDAT_REF || DM > VLGC */
  2166. if (test_bit(ID_A, &motg->inputs)) {
  2167. motg->chg_type = USB_ACA_A_CHARGER;
  2168. motg->chg_state = USB_CHG_STATE_DETECTED;
  2169. delay = 0;
  2170. break;
  2171. }
  2172. if (line_state) /* DP > VLGC or/and DM > VLGC */
  2173. motg->chg_type = USB_PROPRIETARY_CHARGER;
  2174. else if (!dcd && floated_charger_enable)
  2175. motg->chg_type = USB_FLOATED_CHARGER;
  2176. else
  2177. motg->chg_type = USB_SDP_CHARGER;
  2178. motg->chg_state = USB_CHG_STATE_DETECTED;
  2179. delay = 0;
  2180. }
  2181. break;
  2182. case USB_CHG_STATE_PRIMARY_DONE:
  2183. vout = msm_chg_check_secondary_det(motg);
  2184. if (vout)
  2185. motg->chg_type = USB_DCP_CHARGER;
  2186. else
  2187. motg->chg_type = USB_CDP_CHARGER;
  2188. motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
  2189. /* fall through */
  2190. case USB_CHG_STATE_SECONDARY_DONE:
  2191. motg->chg_state = USB_CHG_STATE_DETECTED;
  2192. case USB_CHG_STATE_DETECTED:
  2193. /*
  2194. * Notify the charger type to power supply
  2195. * owner as soon as we determine the charger.
  2196. */
  2197. if (motg->chg_type == USB_DCP_CHARGER &&
  2198. motg->ext_chg_opened) {
  2199. init_completion(&motg->ext_chg_wait);
  2200. motg->ext_chg_active = DEFAULT;
  2201. }
  2202. #ifndef USE_MUIC_CHGTYPE
  2203. msm_otg_notify_chg_type(motg);
  2204. #endif
  2205. msm_chg_block_off(motg);
  2206. msm_chg_enable_aca_det(motg);
  2207. /*
  2208. * Spurious interrupt is seen after enabling ACA detection
  2209. * due to which charger detection fails in case of PET.
  2210. * Add delay of 100 microsec to avoid that.
  2211. */
  2212. udelay(100);
  2213. msm_chg_enable_aca_intr(motg);
  2214. dev_dbg(phy->dev, "chg_type = %s\n",
  2215. chg_to_string(motg->chg_type));
  2216. queue_work(system_nrt_wq, &motg->sm_work);
  2217. return;
  2218. default:
  2219. return;
  2220. }
  2221. queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
  2222. }
  2223. /*
  2224. * We support OTG, Peripheral only and Host only configurations. In case
  2225. * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
  2226. * via Id pin status or user request (debugfs). Id/BSV interrupts are not
  2227. * enabled when switch is controlled by user and default mode is supplied
  2228. * by board file, which can be changed by userspace later.
  2229. */
  2230. static void msm_otg_init_sm(struct msm_otg *motg)
  2231. {
  2232. struct msm_otg_platform_data *pdata = motg->pdata;
  2233. u32 otgsc = readl(USB_OTGSC);
  2234. switch (pdata->mode) {
  2235. case USB_OTG:
  2236. if (pdata->otg_control == OTG_USER_CONTROL) {
  2237. if (pdata->default_mode == USB_HOST) {
  2238. clear_bit(ID, &motg->inputs);
  2239. } else if (pdata->default_mode == USB_PERIPHERAL) {
  2240. set_bit(ID, &motg->inputs);
  2241. set_bit(B_SESS_VLD, &motg->inputs);
  2242. } else {
  2243. set_bit(ID, &motg->inputs);
  2244. clear_bit(B_SESS_VLD, &motg->inputs);
  2245. }
  2246. } else if (pdata->otg_control == OTG_PHY_CONTROL) {
  2247. if (otgsc & OTGSC_ID) {
  2248. set_bit(ID, &motg->inputs);
  2249. } else {
  2250. clear_bit(ID, &motg->inputs);
  2251. set_bit(A_BUS_REQ, &motg->inputs);
  2252. }
  2253. if (otgsc & OTGSC_BSV)
  2254. set_bit(B_SESS_VLD, &motg->inputs);
  2255. else
  2256. clear_bit(B_SESS_VLD, &motg->inputs);
  2257. } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
  2258. if (pdata->pmic_id_irq) {
  2259. if (msm_otg_read_pmic_id_state(motg))
  2260. set_bit(ID, &motg->inputs);
  2261. else
  2262. clear_bit(ID, &motg->inputs);
  2263. }
  2264. /*
  2265. * VBUS initial state is reported after PMIC
  2266. * driver initialization. Wait for it.
  2267. */
  2268. #ifdef OTG_WAIT_PMIC
  2269. wait_for_completion(&pmic_vbus_init);
  2270. #endif
  2271. }
  2272. break;
  2273. case USB_HOST:
  2274. clear_bit(ID, &motg->inputs);
  2275. break;
  2276. case USB_PERIPHERAL:
  2277. set_bit(ID, &motg->inputs);
  2278. if (pdata->otg_control == OTG_PHY_CONTROL) {
  2279. if (otgsc & OTGSC_BSV)
  2280. set_bit(B_SESS_VLD, &motg->inputs);
  2281. else
  2282. clear_bit(B_SESS_VLD, &motg->inputs);
  2283. } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
  2284. /*
  2285. * VBUS initial state is reported after PMIC
  2286. * driver initialization. Wait for it.
  2287. */
  2288. #ifdef OTG_WAIT_PMIC
  2289. wait_for_completion(&pmic_vbus_init);
  2290. #endif
  2291. }
  2292. break;
  2293. default:
  2294. break;
  2295. }
  2296. }
  2297. static void msm_otg_wait_for_ext_chg_done(struct msm_otg *motg)
  2298. {
  2299. struct usb_phy *phy = &motg->phy;
  2300. unsigned long t;
  2301. /*
  2302. * Defer next cable connect event till external charger
  2303. * detection is completed.
  2304. */
  2305. if (motg->ext_chg_active == ACTIVE) {
  2306. do_wait:
  2307. pr_debug("before msm_otg ext chg wait\n");
  2308. t = wait_for_completion_timeout(&motg->ext_chg_wait,
  2309. msecs_to_jiffies(3000));
  2310. if (!t)
  2311. pr_err("msm_otg ext chg wait timeout\n");
  2312. else if (motg->ext_chg_active == ACTIVE)
  2313. goto do_wait;
  2314. else
  2315. pr_debug("msm_otg ext chg wait done\n");
  2316. }
  2317. if (motg->ext_chg_opened) {
  2318. if (phy->flags & ENABLE_DP_MANUAL_PULLUP) {
  2319. ulpi_write(phy, ULPI_MISC_A_VBUSVLDEXT |
  2320. ULPI_MISC_A_VBUSVLDEXTSEL,
  2321. ULPI_CLR(ULPI_MISC_A));
  2322. }
  2323. /* clear charging register bits */
  2324. ulpi_write(phy, 0x3F, 0x86);
  2325. /* re-enable DP and DM pull-down resistors*/
  2326. ulpi_write(phy, 0x6, 0xB);
  2327. }
  2328. }
  2329. static void msm_otg_sm_work(struct work_struct *w)
  2330. {
  2331. struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
  2332. struct usb_otg *otg = motg->phy.otg;
  2333. bool work = 0, srp_reqd, dcp;
  2334. pm_runtime_resume(otg->phy->dev);
  2335. if (motg->pm_done) {
  2336. pm_runtime_get_sync(otg->phy->dev);
  2337. motg->pm_done = 0;
  2338. }
  2339. pr_debug("%s work\n", otg_state_string(otg->phy->state));
  2340. switch (otg->phy->state) {
  2341. case OTG_STATE_UNDEFINED:
  2342. msm_otg_reset(otg->phy);
  2343. msm_otg_init_sm(motg);
  2344. if (!psy && legacy_power_supply) {
  2345. psy = power_supply_get_by_name("usb");
  2346. if (!psy)
  2347. pr_err("couldn't get usb power supply\n");
  2348. }
  2349. otg->phy->state = OTG_STATE_B_IDLE;
  2350. if (!test_bit(B_SESS_VLD, &motg->inputs) &&
  2351. test_bit(ID, &motg->inputs)) {
  2352. pm_runtime_put_noidle(otg->phy->dev);
  2353. pm_runtime_suspend(otg->phy->dev);
  2354. break;
  2355. }
  2356. /* FALL THROUGH */
  2357. case OTG_STATE_B_IDLE:
  2358. if (test_bit(MHL, &motg->inputs)) {
  2359. /* allow LPM */
  2360. pm_runtime_put_noidle(otg->phy->dev);
  2361. pm_runtime_suspend(otg->phy->dev);
  2362. } else if ((!test_bit(ID, &motg->inputs) ||
  2363. test_bit(ID_A, &motg->inputs)) && otg->host) {
  2364. pr_debug("!id || id_A\n");
  2365. if (msm_chg_mhl_detect(motg)) {
  2366. work = 1;
  2367. break;
  2368. }
  2369. clear_bit(B_BUS_REQ, &motg->inputs);
  2370. set_bit(A_BUS_REQ, &motg->inputs);
  2371. otg->phy->state = OTG_STATE_A_IDLE;
  2372. work = 1;
  2373. } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
  2374. pr_debug("b_sess_vld\n");
  2375. switch (motg->chg_state) {
  2376. case USB_CHG_STATE_UNDEFINED:
  2377. msm_chg_detect_work(&motg->chg_work.work);
  2378. break;
  2379. case USB_CHG_STATE_DETECTED:
  2380. switch (motg->chg_type) {
  2381. case USB_DCP_CHARGER:
  2382. /* Enable VDP_SRC */
  2383. ulpi_write(otg->phy, 0x2, 0x85);
  2384. /* fall through */
  2385. case USB_PROPRIETARY_CHARGER:
  2386. msm_otg_notify_charger(motg,
  2387. IDEV_CHG_MAX);
  2388. pm_runtime_put_sync(otg->phy->dev);
  2389. break;
  2390. case USB_FLOATED_CHARGER:
  2391. msm_otg_notify_charger(motg,
  2392. IDEV_CHG_MAX);
  2393. pm_runtime_put_noidle(otg->phy->dev);
  2394. pm_runtime_suspend(otg->phy->dev);
  2395. break;
  2396. case USB_ACA_B_CHARGER:
  2397. msm_otg_notify_charger(motg,
  2398. IDEV_ACA_CHG_MAX);
  2399. /*
  2400. * (ID_B --> ID_C) PHY_ALT interrupt can
  2401. * not be detected in LPM.
  2402. */
  2403. break;
  2404. case USB_CDP_CHARGER:
  2405. msm_otg_notify_charger(motg,
  2406. IDEV_CHG_MAX);
  2407. msm_otg_start_peripheral(otg, 1);
  2408. otg->phy->state =
  2409. OTG_STATE_B_PERIPHERAL;
  2410. break;
  2411. case USB_ACA_C_CHARGER:
  2412. msm_otg_notify_charger(motg,
  2413. IDEV_ACA_CHG_MAX);
  2414. msm_otg_start_peripheral(otg, 1);
  2415. otg->phy->state =
  2416. OTG_STATE_B_PERIPHERAL;
  2417. break;
  2418. case USB_SDP_CHARGER:
  2419. msm_otg_start_peripheral(otg, 1);
  2420. otg->phy->state =
  2421. OTG_STATE_B_PERIPHERAL;
  2422. mod_timer(&motg->chg_check_timer,
  2423. CHG_RECHECK_DELAY);
  2424. break;
  2425. default:
  2426. break;
  2427. }
  2428. break;
  2429. default:
  2430. break;
  2431. }
  2432. } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
  2433. pr_debug("b_sess_end && b_bus_req\n");
  2434. if (msm_otg_start_srp(otg) < 0) {
  2435. clear_bit(B_BUS_REQ, &motg->inputs);
  2436. work = 1;
  2437. break;
  2438. }
  2439. otg->phy->state = OTG_STATE_B_SRP_INIT;
  2440. msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
  2441. break;
  2442. } else {
  2443. pr_debug("chg_work cancel");
  2444. del_timer_sync(&motg->chg_check_timer);
  2445. clear_bit(B_FALSE_SDP, &motg->inputs);
  2446. clear_bit(A_BUS_REQ, &motg->inputs);
  2447. cancel_delayed_work_sync(&motg->chg_work);
  2448. dcp = (motg->chg_type == USB_DCP_CHARGER);
  2449. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  2450. motg->chg_type = USB_INVALID_CHARGER;
  2451. msm_otg_notify_charger(motg, 0);
  2452. if (dcp) {
  2453. if (motg->ext_chg_active == DEFAULT)
  2454. motg->ext_chg_active = INACTIVE;
  2455. msm_otg_wait_for_ext_chg_done(motg);
  2456. /* Turn off VDP_SRC */
  2457. ulpi_write(otg->phy, 0x2, 0x86);
  2458. }
  2459. msm_chg_block_off(motg);
  2460. msm_otg_reset(otg->phy);
  2461. /*
  2462. * There is a small window where ID interrupt
  2463. * is not monitored during ID detection circuit
  2464. * switch from ACA to PMIC. Check ID state
  2465. * before entering into low power mode.
  2466. */
  2467. if (!msm_otg_read_pmic_id_state(motg)) {
  2468. pr_debug("process missed ID intr\n");
  2469. clear_bit(ID, &motg->inputs);
  2470. work = 1;
  2471. break;
  2472. }
  2473. pm_runtime_put_noidle(otg->phy->dev);
  2474. /*
  2475. * Only if autosuspend was enabled in probe, it will be
  2476. * used here. Otherwise, no delay will be used.
  2477. */
  2478. pm_runtime_mark_last_busy(otg->phy->dev);
  2479. pm_runtime_autosuspend(otg->phy->dev);
  2480. motg->pm_done = 1;
  2481. }
  2482. break;
  2483. case OTG_STATE_B_SRP_INIT:
  2484. if (!test_bit(ID, &motg->inputs) ||
  2485. test_bit(ID_A, &motg->inputs) ||
  2486. test_bit(ID_C, &motg->inputs) ||
  2487. (test_bit(B_SESS_VLD, &motg->inputs) &&
  2488. !test_bit(ID_B, &motg->inputs))) {
  2489. pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
  2490. msm_otg_del_timer(motg);
  2491. otg->phy->state = OTG_STATE_B_IDLE;
  2492. /*
  2493. * clear VBUSVLDEXTSEL and VBUSVLDEXT register
  2494. * bits after SRP initiation.
  2495. */
  2496. ulpi_write(otg->phy, 0x0, 0x98);
  2497. work = 1;
  2498. } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
  2499. pr_debug("b_srp_fail\n");
  2500. pr_info("A-device did not respond to SRP\n");
  2501. clear_bit(B_BUS_REQ, &motg->inputs);
  2502. clear_bit(B_SRP_FAIL, &motg->tmouts);
  2503. otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
  2504. ulpi_write(otg->phy, 0x0, 0x98);
  2505. otg->phy->state = OTG_STATE_B_IDLE;
  2506. motg->b_last_se0_sess = jiffies;
  2507. work = 1;
  2508. }
  2509. break;
  2510. case OTG_STATE_B_PERIPHERAL:
  2511. if (test_bit(B_SESS_VLD, &motg->inputs) &&
  2512. test_bit(B_FALSE_SDP, &motg->inputs)) {
  2513. pr_debug("B_FALSE_SDP\n");
  2514. msm_otg_start_peripheral(otg, 0);
  2515. motg->chg_type = USB_DCP_CHARGER;
  2516. clear_bit(B_FALSE_SDP, &motg->inputs);
  2517. otg->phy->state = OTG_STATE_B_IDLE;
  2518. work = 1;
  2519. } else if (!test_bit(ID, &motg->inputs) ||
  2520. test_bit(ID_A, &motg->inputs) ||
  2521. test_bit(ID_B, &motg->inputs) ||
  2522. !test_bit(B_SESS_VLD, &motg->inputs)) {
  2523. pr_debug("!id || id_a/b || !b_sess_vld\n");
  2524. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  2525. motg->chg_type = USB_INVALID_CHARGER;
  2526. msm_otg_notify_charger(motg, 0);
  2527. srp_reqd = otg->gadget->otg_srp_reqd;
  2528. msm_otg_start_peripheral(otg, 0);
  2529. if (test_bit(ID_B, &motg->inputs))
  2530. clear_bit(ID_B, &motg->inputs);
  2531. clear_bit(B_BUS_REQ, &motg->inputs);
  2532. otg->phy->state = OTG_STATE_B_IDLE;
  2533. motg->b_last_se0_sess = jiffies;
  2534. if (srp_reqd)
  2535. msm_otg_start_timer(motg,
  2536. TB_TST_SRP, B_TST_SRP);
  2537. else
  2538. work = 1;
  2539. } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
  2540. otg->gadget->b_hnp_enable &&
  2541. test_bit(A_BUS_SUSPEND, &motg->inputs)) {
  2542. pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
  2543. msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
  2544. /* D+ pullup should not be disconnected within 4msec
  2545. * after A device suspends the bus. Otherwise PET will
  2546. * fail the compliance test.
  2547. */
  2548. udelay(1000);
  2549. msm_otg_start_peripheral(otg, 0);
  2550. otg->phy->state = OTG_STATE_B_WAIT_ACON;
  2551. /*
  2552. * start HCD even before A-device enable
  2553. * pull-up to meet HNP timings.
  2554. */
  2555. otg->host->is_b_host = 1;
  2556. msm_otg_start_host(otg, 1);
  2557. } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
  2558. test_bit(B_SESS_VLD, &motg->inputs)) {
  2559. pr_debug("a_bus_suspend && b_sess_vld\n");
  2560. if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
  2561. pm_runtime_put_noidle(otg->phy->dev);
  2562. pm_runtime_suspend(otg->phy->dev);
  2563. }
  2564. } else if (test_bit(ID_C, &motg->inputs)) {
  2565. msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
  2566. }
  2567. break;
  2568. case OTG_STATE_B_WAIT_ACON:
  2569. if (!test_bit(ID, &motg->inputs) ||
  2570. test_bit(ID_A, &motg->inputs) ||
  2571. test_bit(ID_B, &motg->inputs) ||
  2572. !test_bit(B_SESS_VLD, &motg->inputs)) {
  2573. pr_debug("!id || id_a/b || !b_sess_vld\n");
  2574. msm_otg_del_timer(motg);
  2575. /*
  2576. * A-device is physically disconnected during
  2577. * HNP. Remove HCD.
  2578. */
  2579. msm_otg_start_host(otg, 0);
  2580. otg->host->is_b_host = 0;
  2581. clear_bit(B_BUS_REQ, &motg->inputs);
  2582. clear_bit(A_BUS_SUSPEND, &motg->inputs);
  2583. motg->b_last_se0_sess = jiffies;
  2584. otg->phy->state = OTG_STATE_B_IDLE;
  2585. msm_otg_reset(otg->phy);
  2586. work = 1;
  2587. } else if (test_bit(A_CONN, &motg->inputs)) {
  2588. pr_debug("a_conn\n");
  2589. clear_bit(A_BUS_SUSPEND, &motg->inputs);
  2590. otg->phy->state = OTG_STATE_B_HOST;
  2591. /*
  2592. * PET disconnects D+ pullup after reset is generated
  2593. * by B device in B_HOST role which is not detected by
  2594. * B device. As workaorund , start timer of 300msec
  2595. * and stop timer if A device is enumerated else clear
  2596. * A_CONN.
  2597. */
  2598. msm_otg_start_timer(motg, TB_TST_CONFIG,
  2599. B_TST_CONFIG);
  2600. } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
  2601. pr_debug("b_ase0_brst_tmout\n");
  2602. pr_info("B HNP fail:No response from A device\n");
  2603. msm_otg_start_host(otg, 0);
  2604. msm_otg_reset(otg->phy);
  2605. otg->host->is_b_host = 0;
  2606. clear_bit(B_ASE0_BRST, &motg->tmouts);
  2607. clear_bit(A_BUS_SUSPEND, &motg->inputs);
  2608. clear_bit(B_BUS_REQ, &motg->inputs);
  2609. otg_send_event(OTG_EVENT_HNP_FAILED);
  2610. otg->phy->state = OTG_STATE_B_IDLE;
  2611. work = 1;
  2612. } else if (test_bit(ID_C, &motg->inputs)) {
  2613. msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
  2614. }
  2615. break;
  2616. case OTG_STATE_B_HOST:
  2617. if (!test_bit(B_BUS_REQ, &motg->inputs) ||
  2618. !test_bit(A_CONN, &motg->inputs) ||
  2619. !test_bit(B_SESS_VLD, &motg->inputs)) {
  2620. pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
  2621. clear_bit(A_CONN, &motg->inputs);
  2622. clear_bit(B_BUS_REQ, &motg->inputs);
  2623. msm_otg_start_host(otg, 0);
  2624. otg->host->is_b_host = 0;
  2625. otg->phy->state = OTG_STATE_B_IDLE;
  2626. msm_otg_reset(otg->phy);
  2627. work = 1;
  2628. } else if (test_bit(ID_C, &motg->inputs)) {
  2629. msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
  2630. }
  2631. break;
  2632. case OTG_STATE_A_IDLE:
  2633. otg->default_a = 1;
  2634. if (test_bit(ID, &motg->inputs) &&
  2635. !test_bit(ID_A, &motg->inputs)) {
  2636. pr_debug("id && !id_a\n");
  2637. otg->default_a = 0;
  2638. clear_bit(A_BUS_DROP, &motg->inputs);
  2639. otg->phy->state = OTG_STATE_B_IDLE;
  2640. del_timer_sync(&motg->id_timer);
  2641. msm_otg_link_reset(motg);
  2642. msm_chg_enable_aca_intr(motg);
  2643. msm_otg_notify_charger(motg, 0);
  2644. work = 1;
  2645. } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
  2646. (test_bit(A_SRP_DET, &motg->inputs) ||
  2647. test_bit(A_BUS_REQ, &motg->inputs))) {
  2648. pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
  2649. clear_bit(A_SRP_DET, &motg->inputs);
  2650. /* Disable SRP detection */
  2651. writel_relaxed((readl_relaxed(USB_OTGSC) &
  2652. ~OTGSC_INTSTS_MASK) &
  2653. ~OTGSC_DPIE, USB_OTGSC);
  2654. otg->phy->state = OTG_STATE_A_WAIT_VRISE;
  2655. /* VBUS should not be supplied before end of SRP pulse
  2656. * generated by PET, if not complaince test fail.
  2657. */
  2658. usleep_range(10000, 12000);
  2659. /* ACA: ID_A: Stop charging untill enumeration */
  2660. if (test_bit(ID_A, &motg->inputs))
  2661. msm_otg_notify_charger(motg, 0);
  2662. else
  2663. msm_hsusb_vbus_power(motg, 1);
  2664. msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
  2665. } else {
  2666. pr_debug("No session requested\n");
  2667. clear_bit(A_BUS_DROP, &motg->inputs);
  2668. if (test_bit(ID_A, &motg->inputs)) {
  2669. msm_otg_notify_charger(motg,
  2670. IDEV_ACA_CHG_MAX);
  2671. } else if (!test_bit(ID, &motg->inputs)) {
  2672. msm_otg_notify_charger(motg, 0);
  2673. /*
  2674. * A-device is not providing power on VBUS.
  2675. * Enable SRP detection.
  2676. */
  2677. writel_relaxed(0x13, USB_USBMODE);
  2678. writel_relaxed((readl_relaxed(USB_OTGSC) &
  2679. ~OTGSC_INTSTS_MASK) |
  2680. OTGSC_DPIE, USB_OTGSC);
  2681. mb();
  2682. }
  2683. }
  2684. break;
  2685. case OTG_STATE_A_WAIT_VRISE:
  2686. if ((test_bit(ID, &motg->inputs) &&
  2687. !test_bit(ID_A, &motg->inputs)) ||
  2688. test_bit(A_BUS_DROP, &motg->inputs) ||
  2689. test_bit(A_WAIT_VRISE, &motg->tmouts)) {
  2690. pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
  2691. clear_bit(A_BUS_REQ, &motg->inputs);
  2692. msm_otg_del_timer(motg);
  2693. msm_hsusb_vbus_power(motg, 0);
  2694. otg->phy->state = OTG_STATE_A_WAIT_VFALL;
  2695. msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
  2696. } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
  2697. pr_debug("a_vbus_vld\n");
  2698. otg->phy->state = OTG_STATE_A_WAIT_BCON;
  2699. if (TA_WAIT_BCON > 0)
  2700. msm_otg_start_timer(motg, TA_WAIT_BCON,
  2701. A_WAIT_BCON);
  2702. /* Clear BSV in host mode */
  2703. clear_bit(B_SESS_VLD, &motg->inputs);
  2704. msm_otg_start_host(otg, 1);
  2705. msm_chg_enable_aca_det(motg);
  2706. msm_chg_disable_aca_intr(motg);
  2707. mod_timer(&motg->id_timer, ID_TIMER_FREQ);
  2708. if (msm_chg_check_aca_intr(motg))
  2709. work = 1;
  2710. }
  2711. break;
  2712. case OTG_STATE_A_WAIT_BCON:
  2713. if ((test_bit(ID, &motg->inputs) &&
  2714. !test_bit(ID_A, &motg->inputs)) ||
  2715. test_bit(A_BUS_DROP, &motg->inputs) ||
  2716. test_bit(A_WAIT_BCON, &motg->tmouts)) {
  2717. pr_debug("(id && id_a/b/c) || a_bus_drop ||"
  2718. "a_wait_bcon_tmout\n");
  2719. if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
  2720. pr_info("Device No Response\n");
  2721. otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
  2722. }
  2723. msm_otg_del_timer(motg);
  2724. clear_bit(A_BUS_REQ, &motg->inputs);
  2725. clear_bit(B_CONN, &motg->inputs);
  2726. msm_otg_start_host(otg, 0);
  2727. /*
  2728. * ACA: ID_A with NO accessory, just the A plug is
  2729. * attached to ACA: Use IDCHG_MAX for charging
  2730. */
  2731. if (test_bit(ID_A, &motg->inputs))
  2732. msm_otg_notify_charger(motg, IDEV_CHG_MIN);
  2733. else
  2734. msm_hsusb_vbus_power(motg, 0);
  2735. otg->phy->state = OTG_STATE_A_WAIT_VFALL;
  2736. msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
  2737. } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
  2738. pr_debug("!a_vbus_vld\n");
  2739. clear_bit(B_CONN, &motg->inputs);
  2740. msm_otg_del_timer(motg);
  2741. msm_otg_start_host(otg, 0);
  2742. otg->phy->state = OTG_STATE_A_VBUS_ERR;
  2743. msm_otg_reset(otg->phy);
  2744. } else if (test_bit(ID_A, &motg->inputs)) {
  2745. msm_hsusb_vbus_power(motg, 0);
  2746. } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
  2747. /*
  2748. * If TA_WAIT_BCON is infinite, we don;t
  2749. * turn off VBUS. Enter low power mode.
  2750. */
  2751. if (TA_WAIT_BCON < 0)
  2752. pm_runtime_put_sync(otg->phy->dev);
  2753. } else if (!test_bit(ID, &motg->inputs)) {
  2754. msm_hsusb_vbus_power(motg, 1);
  2755. }
  2756. break;
  2757. case OTG_STATE_A_HOST:
  2758. if ((test_bit(ID, &motg->inputs) &&
  2759. !test_bit(ID_A, &motg->inputs)) ||
  2760. test_bit(A_BUS_DROP, &motg->inputs)) {
  2761. pr_debug("id_a/b/c || a_bus_drop\n");
  2762. clear_bit(B_CONN, &motg->inputs);
  2763. clear_bit(A_BUS_REQ, &motg->inputs);
  2764. msm_otg_del_timer(motg);
  2765. otg->phy->state = OTG_STATE_A_WAIT_VFALL;
  2766. msm_otg_start_host(otg, 0);
  2767. if (!test_bit(ID_A, &motg->inputs))
  2768. msm_hsusb_vbus_power(motg, 0);
  2769. msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
  2770. } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
  2771. pr_debug("!a_vbus_vld\n");
  2772. clear_bit(B_CONN, &motg->inputs);
  2773. msm_otg_del_timer(motg);
  2774. otg->phy->state = OTG_STATE_A_VBUS_ERR;
  2775. msm_otg_start_host(otg, 0);
  2776. msm_otg_reset(otg->phy);
  2777. } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
  2778. /*
  2779. * a_bus_req is de-asserted when root hub is
  2780. * suspended or HNP is in progress.
  2781. */
  2782. pr_debug("!a_bus_req\n");
  2783. msm_otg_del_timer(motg);
  2784. otg->phy->state = OTG_STATE_A_SUSPEND;
  2785. if (otg->host->b_hnp_enable)
  2786. msm_otg_start_timer(motg, TA_AIDL_BDIS,
  2787. A_AIDL_BDIS);
  2788. else
  2789. pm_runtime_put_sync(otg->phy->dev);
  2790. } else if (!test_bit(B_CONN, &motg->inputs)) {
  2791. pr_debug("!b_conn\n");
  2792. msm_otg_del_timer(motg);
  2793. otg->phy->state = OTG_STATE_A_WAIT_BCON;
  2794. if (TA_WAIT_BCON > 0)
  2795. msm_otg_start_timer(motg, TA_WAIT_BCON,
  2796. A_WAIT_BCON);
  2797. if (msm_chg_check_aca_intr(motg))
  2798. work = 1;
  2799. } else if (test_bit(ID_A, &motg->inputs)) {
  2800. msm_otg_del_timer(motg);
  2801. msm_hsusb_vbus_power(motg, 0);
  2802. if (motg->chg_type == USB_ACA_DOCK_CHARGER)
  2803. msm_otg_notify_charger(motg,
  2804. IDEV_ACA_CHG_MAX);
  2805. else
  2806. msm_otg_notify_charger(motg,
  2807. IDEV_CHG_MIN - motg->mA_port);
  2808. } else if (!test_bit(ID, &motg->inputs)) {
  2809. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  2810. motg->chg_type = USB_INVALID_CHARGER;
  2811. msm_otg_notify_charger(motg, 0);
  2812. msm_hsusb_vbus_power(motg, 1);
  2813. }
  2814. break;
  2815. case OTG_STATE_A_SUSPEND:
  2816. if ((test_bit(ID, &motg->inputs) &&
  2817. !test_bit(ID_A, &motg->inputs)) ||
  2818. test_bit(A_BUS_DROP, &motg->inputs) ||
  2819. test_bit(A_AIDL_BDIS, &motg->tmouts)) {
  2820. pr_debug("id_a/b/c || a_bus_drop ||"
  2821. "a_aidl_bdis_tmout\n");
  2822. msm_otg_del_timer(motg);
  2823. clear_bit(B_CONN, &motg->inputs);
  2824. otg->phy->state = OTG_STATE_A_WAIT_VFALL;
  2825. msm_otg_start_host(otg, 0);
  2826. msm_otg_reset(otg->phy);
  2827. if (!test_bit(ID_A, &motg->inputs))
  2828. msm_hsusb_vbus_power(motg, 0);
  2829. msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
  2830. } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
  2831. pr_debug("!a_vbus_vld\n");
  2832. msm_otg_del_timer(motg);
  2833. clear_bit(B_CONN, &motg->inputs);
  2834. otg->phy->state = OTG_STATE_A_VBUS_ERR;
  2835. msm_otg_start_host(otg, 0);
  2836. msm_otg_reset(otg->phy);
  2837. } else if (!test_bit(B_CONN, &motg->inputs) &&
  2838. otg->host->b_hnp_enable) {
  2839. pr_debug("!b_conn && b_hnp_enable");
  2840. otg->phy->state = OTG_STATE_A_PERIPHERAL;
  2841. msm_otg_host_hnp_enable(otg, 1);
  2842. otg->gadget->is_a_peripheral = 1;
  2843. msm_otg_start_peripheral(otg, 1);
  2844. } else if (!test_bit(B_CONN, &motg->inputs) &&
  2845. !otg->host->b_hnp_enable) {
  2846. pr_debug("!b_conn && !b_hnp_enable");
  2847. /*
  2848. * bus request is dropped during suspend.
  2849. * acquire again for next device.
  2850. */
  2851. set_bit(A_BUS_REQ, &motg->inputs);
  2852. otg->phy->state = OTG_STATE_A_WAIT_BCON;
  2853. if (TA_WAIT_BCON > 0)
  2854. msm_otg_start_timer(motg, TA_WAIT_BCON,
  2855. A_WAIT_BCON);
  2856. } else if (test_bit(ID_A, &motg->inputs)) {
  2857. msm_hsusb_vbus_power(motg, 0);
  2858. msm_otg_notify_charger(motg,
  2859. IDEV_CHG_MIN - motg->mA_port);
  2860. } else if (!test_bit(ID, &motg->inputs)) {
  2861. msm_otg_notify_charger(motg, 0);
  2862. msm_hsusb_vbus_power(motg, 1);
  2863. }
  2864. break;
  2865. case OTG_STATE_A_PERIPHERAL:
  2866. if ((test_bit(ID, &motg->inputs) &&
  2867. !test_bit(ID_A, &motg->inputs)) ||
  2868. test_bit(A_BUS_DROP, &motg->inputs)) {
  2869. pr_debug("id _f/b/c || a_bus_drop\n");
  2870. /* Clear BIDL_ADIS timer */
  2871. msm_otg_del_timer(motg);
  2872. otg->phy->state = OTG_STATE_A_WAIT_VFALL;
  2873. msm_otg_start_peripheral(otg, 0);
  2874. otg->gadget->is_a_peripheral = 0;
  2875. msm_otg_start_host(otg, 0);
  2876. msm_otg_reset(otg->phy);
  2877. if (!test_bit(ID_A, &motg->inputs))
  2878. msm_hsusb_vbus_power(motg, 0);
  2879. msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
  2880. } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
  2881. pr_debug("!a_vbus_vld\n");
  2882. /* Clear BIDL_ADIS timer */
  2883. msm_otg_del_timer(motg);
  2884. otg->phy->state = OTG_STATE_A_VBUS_ERR;
  2885. msm_otg_start_peripheral(otg, 0);
  2886. otg->gadget->is_a_peripheral = 0;
  2887. msm_otg_start_host(otg, 0);
  2888. } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
  2889. pr_debug("a_bidl_adis_tmout\n");
  2890. msm_otg_start_peripheral(otg, 0);
  2891. otg->gadget->is_a_peripheral = 0;
  2892. otg->phy->state = OTG_STATE_A_WAIT_BCON;
  2893. set_bit(A_BUS_REQ, &motg->inputs);
  2894. msm_otg_host_hnp_enable(otg, 0);
  2895. if (TA_WAIT_BCON > 0)
  2896. msm_otg_start_timer(motg, TA_WAIT_BCON,
  2897. A_WAIT_BCON);
  2898. } else if (test_bit(ID_A, &motg->inputs)) {
  2899. msm_hsusb_vbus_power(motg, 0);
  2900. msm_otg_notify_charger(motg,
  2901. IDEV_CHG_MIN - motg->mA_port);
  2902. } else if (!test_bit(ID, &motg->inputs)) {
  2903. msm_otg_notify_charger(motg, 0);
  2904. msm_hsusb_vbus_power(motg, 1);
  2905. }
  2906. break;
  2907. case OTG_STATE_A_WAIT_VFALL:
  2908. if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
  2909. clear_bit(A_VBUS_VLD, &motg->inputs);
  2910. otg->phy->state = OTG_STATE_A_IDLE;
  2911. work = 1;
  2912. }
  2913. break;
  2914. case OTG_STATE_A_VBUS_ERR:
  2915. if ((test_bit(ID, &motg->inputs) &&
  2916. !test_bit(ID_A, &motg->inputs)) ||
  2917. test_bit(A_BUS_DROP, &motg->inputs) ||
  2918. test_bit(A_CLR_ERR, &motg->inputs)) {
  2919. otg->phy->state = OTG_STATE_A_WAIT_VFALL;
  2920. if (!test_bit(ID_A, &motg->inputs))
  2921. msm_hsusb_vbus_power(motg, 0);
  2922. msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
  2923. motg->chg_state = USB_CHG_STATE_UNDEFINED;
  2924. motg->chg_type = USB_INVALID_CHARGER;
  2925. msm_otg_notify_charger(motg, 0);
  2926. }
  2927. break;
  2928. default:
  2929. break;
  2930. }
  2931. if (work)
  2932. queue_work(system_nrt_wq, &motg->sm_work);
  2933. }
  2934. static void msm_otg_suspend_work(struct work_struct *w)
  2935. {
  2936. struct msm_otg *motg =
  2937. container_of(w, struct msm_otg, suspend_work.work);
  2938. /* This work is only for device bus suspend */
  2939. if (test_bit(A_BUS_SUSPEND, &motg->inputs))
  2940. msm_otg_sm_work(&motg->sm_work);
  2941. }
  2942. static irqreturn_t msm_otg_irq(int irq, void *data)
  2943. {
  2944. struct msm_otg *motg = data;
  2945. struct usb_otg *otg = motg->phy.otg;
  2946. u32 otgsc = 0, usbsts, pc;
  2947. bool work = 0;
  2948. irqreturn_t ret = IRQ_HANDLED;
  2949. if (atomic_read(&motg->in_lpm)) {
  2950. pr_debug("OTG IRQ: %d in LPM\n", irq);
  2951. disable_irq_nosync(irq);
  2952. motg->async_int = irq;
  2953. if (!atomic_read(&motg->pm_suspended))
  2954. pm_request_resume(otg->phy->dev);
  2955. return IRQ_HANDLED;
  2956. }
  2957. usbsts = readl(USB_USBSTS);
  2958. otgsc = readl(USB_OTGSC);
  2959. if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
  2960. return IRQ_NONE;
  2961. if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
  2962. if (otgsc & OTGSC_ID) {
  2963. dev_dbg(otg->phy->dev, "ID set\n");
  2964. set_bit(ID, &motg->inputs);
  2965. } else {
  2966. dev_dbg(otg->phy->dev, "ID clear\n");
  2967. /*
  2968. * Assert a_bus_req to supply power on
  2969. * VBUS when Micro/Mini-A cable is connected
  2970. * with out user intervention.
  2971. */
  2972. set_bit(A_BUS_REQ, &motg->inputs);
  2973. clear_bit(ID, &motg->inputs);
  2974. msm_chg_enable_aca_det(motg);
  2975. }
  2976. writel_relaxed(otgsc, USB_OTGSC);
  2977. work = 1;
  2978. } else if (otgsc & OTGSC_DPIS) {
  2979. pr_debug("DPIS detected\n");
  2980. writel_relaxed(otgsc, USB_OTGSC);
  2981. set_bit(A_SRP_DET, &motg->inputs);
  2982. set_bit(A_BUS_REQ, &motg->inputs);
  2983. work = 1;
  2984. } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
  2985. writel_relaxed(otgsc, USB_OTGSC);
  2986. #ifdef CONFIG_USB_HOST_NOTIFY
  2987. msm_otg_host_notify_set(motg, otgsc & OTGSC_BSV ? 1 : 0);
  2988. #endif
  2989. /*
  2990. * BSV interrupt comes when operating as an A-device
  2991. * (VBUS on/off).
  2992. * But, handle BSV when charger is removed from ACA in ID_A
  2993. */
  2994. if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
  2995. !test_bit(ID_A, &motg->inputs))
  2996. return IRQ_HANDLED;
  2997. if (otgsc & OTGSC_BSV) {
  2998. dev_dbg(otg->phy->dev, "BSV set\n");
  2999. set_bit(B_SESS_VLD, &motg->inputs);
  3000. } else {
  3001. dev_dbg(otg->phy->dev, "BSV clear\n");
  3002. clear_bit(B_SESS_VLD, &motg->inputs);
  3003. clear_bit(A_BUS_SUSPEND, &motg->inputs);
  3004. msm_chg_check_aca_intr(motg);
  3005. }
  3006. work = 1;
  3007. } else if (usbsts & STS_PCI) {
  3008. pc = readl_relaxed(USB_PORTSC);
  3009. pr_debug("portsc = %x\n", pc);
  3010. ret = IRQ_NONE;
  3011. /*
  3012. * HCD Acks PCI interrupt. We use this to switch
  3013. * between different OTG states.
  3014. */
  3015. work = 1;
  3016. switch (otg->phy->state) {
  3017. case OTG_STATE_A_SUSPEND:
  3018. if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
  3019. !(pc & PORTSC_CCS)) {
  3020. pr_debug("B_CONN clear\n");
  3021. clear_bit(B_CONN, &motg->inputs);
  3022. msm_otg_del_timer(motg);
  3023. }
  3024. break;
  3025. case OTG_STATE_A_PERIPHERAL:
  3026. /*
  3027. * A-peripheral observed activity on bus.
  3028. * clear A_BIDL_ADIS timer.
  3029. */
  3030. msm_otg_del_timer(motg);
  3031. work = 0;
  3032. break;
  3033. case OTG_STATE_B_WAIT_ACON:
  3034. if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
  3035. pr_debug("A_CONN set\n");
  3036. set_bit(A_CONN, &motg->inputs);
  3037. /* Clear ASE0_BRST timer */
  3038. msm_otg_del_timer(motg);
  3039. }
  3040. break;
  3041. case OTG_STATE_B_HOST:
  3042. if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
  3043. pr_debug("A_CONN clear\n");
  3044. clear_bit(A_CONN, &motg->inputs);
  3045. msm_otg_del_timer(motg);
  3046. }
  3047. break;
  3048. case OTG_STATE_A_WAIT_BCON:
  3049. if (TA_WAIT_BCON < 0)
  3050. set_bit(A_BUS_REQ, &motg->inputs);
  3051. default:
  3052. work = 0;
  3053. break;
  3054. }
  3055. } else if (usbsts & STS_URI) {
  3056. ret = IRQ_NONE;
  3057. switch (otg->phy->state) {
  3058. case OTG_STATE_A_PERIPHERAL:
  3059. /*
  3060. * A-peripheral observed activity on bus.
  3061. * clear A_BIDL_ADIS timer.
  3062. */
  3063. msm_otg_del_timer(motg);
  3064. work = 0;
  3065. break;
  3066. default:
  3067. work = 0;
  3068. break;
  3069. }
  3070. } else if (usbsts & STS_SLI) {
  3071. ret = IRQ_NONE;
  3072. work = 0;
  3073. switch (otg->phy->state) {
  3074. case OTG_STATE_B_PERIPHERAL:
  3075. if (otg->gadget->b_hnp_enable) {
  3076. set_bit(A_BUS_SUSPEND, &motg->inputs);
  3077. set_bit(B_BUS_REQ, &motg->inputs);
  3078. work = 1;
  3079. }
  3080. break;
  3081. case OTG_STATE_A_PERIPHERAL:
  3082. msm_otg_start_timer(motg, TA_BIDL_ADIS,
  3083. A_BIDL_ADIS);
  3084. break;
  3085. default:
  3086. break;
  3087. }
  3088. } else if ((usbsts & PHY_ALT_INT)) {
  3089. writel_relaxed(PHY_ALT_INT, USB_USBSTS);
  3090. if (msm_chg_check_aca_intr(motg))
  3091. work = 1;
  3092. ret = IRQ_HANDLED;
  3093. }
  3094. if (work)
  3095. queue_work(system_nrt_wq, &motg->sm_work);
  3096. return ret;
  3097. }
  3098. static void msm_otg_set_vbus_state(int online)
  3099. {
  3100. #ifdef OTG_WAIT_PMIC
  3101. static bool init;
  3102. #endif
  3103. struct msm_otg *motg = the_msm_otg;
  3104. if (online) {
  3105. pr_info("PMIC: BSV set\n");
  3106. set_bit(B_SESS_VLD, &motg->inputs);
  3107. #ifdef USE_MUIC_CHGTYPE
  3108. motg->chg_state = USB_CHG_STATE_DETECTED;
  3109. motg->chg_type = USB_SDP_CHARGER;
  3110. #endif
  3111. } else {
  3112. pr_info("PMIC: BSV clear\n");
  3113. clear_bit(B_SESS_VLD, &motg->inputs);
  3114. }
  3115. #ifdef OTG_WAIT_PMIC
  3116. /* do not queue state m/c work if id is grounded */
  3117. if (!test_bit(ID, &motg->inputs)) {
  3118. /*
  3119. * state machine work waits for initial VBUS
  3120. * completion in UNDEFINED state. Process
  3121. * the initial VBUS event in ID_GND state.
  3122. */
  3123. if (init)
  3124. return;
  3125. }
  3126. if (!init) {
  3127. init = true;
  3128. complete(&pmic_vbus_init);
  3129. pr_info("PMIC: BSV init complete\n");
  3130. return;
  3131. }
  3132. #endif
  3133. if (test_bit(MHL, &motg->inputs) ||
  3134. mhl_det_in_progress) {
  3135. pr_info("PMIC: BSV interrupt ignored in MHL\n");
  3136. return;
  3137. }
  3138. if (atomic_read(&motg->pm_suspended)) {
  3139. motg->sm_work_pending = true;
  3140. } else if (!motg->sm_work_pending) {
  3141. /* process event only if previous one is not pending */
  3142. queue_work(system_nrt_wq, &motg->sm_work);
  3143. }
  3144. }
  3145. static void msm_pmic_id_status_w(struct work_struct *w)
  3146. {
  3147. struct msm_otg *motg = container_of(w, struct msm_otg,
  3148. pmic_id_status_work.work);
  3149. int work = 0;
  3150. dev_dbg(motg->phy.dev, "ID status_w\n");
  3151. if (msm_otg_read_pmic_id_state(motg)) {
  3152. if (!test_and_set_bit(ID, &motg->inputs)) {
  3153. pr_debug("PMIC: ID set\n");
  3154. work = 1;
  3155. }
  3156. } else {
  3157. if (test_and_clear_bit(ID, &motg->inputs)) {
  3158. pr_debug("PMIC: ID clear\n");
  3159. set_bit(A_BUS_REQ, &motg->inputs);
  3160. work = 1;
  3161. }
  3162. }
  3163. if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
  3164. if (atomic_read(&motg->pm_suspended)) {
  3165. motg->sm_work_pending = true;
  3166. } else if (!motg->sm_work_pending) {
  3167. /* process event only if previous one is not pending */
  3168. queue_work(system_nrt_wq, &motg->sm_work);
  3169. }
  3170. }
  3171. }
  3172. int msm_otg_pm_notify(struct notifier_block *notify_block,
  3173. unsigned long mode, void *unused)
  3174. {
  3175. struct msm_otg *motg = container_of(
  3176. notify_block, struct msm_otg, pm_notify);
  3177. dev_dbg(motg->phy.dev, "OTG PM notify:%lx, sm_pending:%u\n", mode,
  3178. motg->sm_work_pending);
  3179. switch (mode) {
  3180. case PM_POST_SUSPEND:
  3181. /* OTG sm_work can be armed now */
  3182. atomic_set(&motg->pm_suspended, 0);
  3183. /* Handle any deferred wakeup events from USB during suspend */
  3184. if (motg->sm_work_pending) {
  3185. motg->sm_work_pending = false;
  3186. queue_work(system_nrt_wq, &motg->sm_work);
  3187. }
  3188. break;
  3189. default:
  3190. break;
  3191. }
  3192. return NOTIFY_OK;
  3193. }
  3194. static int msm_otg_mode_show(struct seq_file *s, void *unused)
  3195. {
  3196. struct msm_otg *motg = s->private;
  3197. struct usb_otg *otg = motg->phy.otg;
  3198. switch (otg->phy->state) {
  3199. case OTG_STATE_A_HOST:
  3200. seq_printf(s, "host\n");
  3201. break;
  3202. case OTG_STATE_B_PERIPHERAL:
  3203. seq_printf(s, "peripheral\n");
  3204. break;
  3205. default:
  3206. seq_printf(s, "none\n");
  3207. break;
  3208. }
  3209. return 0;
  3210. }
  3211. static int msm_otg_mode_open(struct inode *inode, struct file *file)
  3212. {
  3213. return single_open(file, msm_otg_mode_show, inode->i_private);
  3214. }
  3215. static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
  3216. size_t count, loff_t *ppos)
  3217. {
  3218. struct seq_file *s = file->private_data;
  3219. struct msm_otg *motg = s->private;
  3220. char buf[16];
  3221. struct usb_phy *phy = &motg->phy;
  3222. int status = count;
  3223. enum usb_mode_type req_mode;
  3224. memset(buf, 0x00, sizeof(buf));
  3225. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
  3226. status = -EFAULT;
  3227. goto out;
  3228. }
  3229. if (!strncmp(buf, "host", 4)) {
  3230. req_mode = USB_HOST;
  3231. } else if (!strncmp(buf, "peripheral", 10)) {
  3232. req_mode = USB_PERIPHERAL;
  3233. } else if (!strncmp(buf, "none", 4)) {
  3234. req_mode = USB_NONE;
  3235. } else {
  3236. status = -EINVAL;
  3237. goto out;
  3238. }
  3239. switch (req_mode) {
  3240. case USB_NONE:
  3241. switch (phy->state) {
  3242. case OTG_STATE_A_HOST:
  3243. case OTG_STATE_B_PERIPHERAL:
  3244. set_bit(ID, &motg->inputs);
  3245. clear_bit(B_SESS_VLD, &motg->inputs);
  3246. break;
  3247. default:
  3248. goto out;
  3249. }
  3250. break;
  3251. case USB_PERIPHERAL:
  3252. switch (phy->state) {
  3253. case OTG_STATE_B_IDLE:
  3254. case OTG_STATE_A_HOST:
  3255. set_bit(ID, &motg->inputs);
  3256. set_bit(B_SESS_VLD, &motg->inputs);
  3257. break;
  3258. default:
  3259. goto out;
  3260. }
  3261. break;
  3262. case USB_HOST:
  3263. switch (phy->state) {
  3264. case OTG_STATE_B_IDLE:
  3265. case OTG_STATE_B_PERIPHERAL:
  3266. clear_bit(ID, &motg->inputs);
  3267. break;
  3268. default:
  3269. goto out;
  3270. }
  3271. break;
  3272. default:
  3273. goto out;
  3274. }
  3275. pm_runtime_resume(phy->dev);
  3276. queue_work(system_nrt_wq, &motg->sm_work);
  3277. out:
  3278. return status;
  3279. }
  3280. const struct file_operations msm_otg_mode_fops = {
  3281. .open = msm_otg_mode_open,
  3282. .read = seq_read,
  3283. .write = msm_otg_mode_write,
  3284. .llseek = seq_lseek,
  3285. .release = single_release,
  3286. };
  3287. static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
  3288. {
  3289. struct msm_otg *motg = s->private;
  3290. struct usb_phy *phy = &motg->phy;
  3291. seq_printf(s, "%s\n", otg_state_string(phy->state));
  3292. return 0;
  3293. }
  3294. static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
  3295. {
  3296. return single_open(file, msm_otg_show_otg_state, inode->i_private);
  3297. }
  3298. const struct file_operations msm_otg_state_fops = {
  3299. .open = msm_otg_otg_state_open,
  3300. .read = seq_read,
  3301. .llseek = seq_lseek,
  3302. .release = single_release,
  3303. };
  3304. static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
  3305. {
  3306. struct msm_otg *motg = s->private;
  3307. seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
  3308. return 0;
  3309. }
  3310. static int msm_otg_chg_open(struct inode *inode, struct file *file)
  3311. {
  3312. return single_open(file, msm_otg_show_chg_type, inode->i_private);
  3313. }
  3314. const struct file_operations msm_otg_chg_fops = {
  3315. .open = msm_otg_chg_open,
  3316. .read = seq_read,
  3317. .llseek = seq_lseek,
  3318. .release = single_release,
  3319. };
  3320. static int msm_otg_aca_show(struct seq_file *s, void *unused)
  3321. {
  3322. if (debug_aca_enabled)
  3323. seq_printf(s, "enabled\n");
  3324. else
  3325. seq_printf(s, "disabled\n");
  3326. return 0;
  3327. }
  3328. static int msm_otg_aca_open(struct inode *inode, struct file *file)
  3329. {
  3330. return single_open(file, msm_otg_aca_show, inode->i_private);
  3331. }
  3332. static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
  3333. size_t count, loff_t *ppos)
  3334. {
  3335. char buf[8];
  3336. memset(buf, 0x00, sizeof(buf));
  3337. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  3338. return -EFAULT;
  3339. if (!strncmp(buf, "enable", 6))
  3340. debug_aca_enabled = true;
  3341. else
  3342. debug_aca_enabled = false;
  3343. return count;
  3344. }
  3345. const struct file_operations msm_otg_aca_fops = {
  3346. .open = msm_otg_aca_open,
  3347. .read = seq_read,
  3348. .write = msm_otg_aca_write,
  3349. .llseek = seq_lseek,
  3350. .release = single_release,
  3351. };
  3352. static int msm_otg_bus_show(struct seq_file *s, void *unused)
  3353. {
  3354. if (debug_bus_voting_enabled)
  3355. seq_printf(s, "enabled\n");
  3356. else
  3357. seq_printf(s, "disabled\n");
  3358. return 0;
  3359. }
  3360. static int msm_otg_bus_open(struct inode *inode, struct file *file)
  3361. {
  3362. return single_open(file, msm_otg_bus_show, inode->i_private);
  3363. }
  3364. static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
  3365. size_t count, loff_t *ppos)
  3366. {
  3367. char buf[8];
  3368. struct seq_file *s = file->private_data;
  3369. struct msm_otg *motg = s->private;
  3370. memset(buf, 0x00, sizeof(buf));
  3371. if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
  3372. return -EFAULT;
  3373. if (!strncmp(buf, "enable", 6)) {
  3374. /* Do not vote here. Let OTG statemachine decide when to vote */
  3375. debug_bus_voting_enabled = true;
  3376. } else {
  3377. debug_bus_voting_enabled = false;
  3378. msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
  3379. }
  3380. return count;
  3381. }
  3382. #ifdef CONFIG_QPNP_SEC_CHARGER
  3383. static int
  3384. otg_get_prop_usbin_voltage_now(struct msm_otg *motg)
  3385. {
  3386. int rc = 0;
  3387. struct qpnp_vadc_result results;
  3388. if (IS_ERR_OR_NULL(motg->vadc_dev)) {
  3389. motg->vadc_dev = qpnp_get_vadc(motg->phy.dev, "usbin");
  3390. if (IS_ERR(motg->vadc_dev))
  3391. return PTR_ERR(motg->vadc_dev);
  3392. }
  3393. rc = qpnp_vadc_read(motg->vadc_dev, USBIN, &results);
  3394. if (rc) {
  3395. pr_err("Unable to read usbin rc=%d\n", rc);
  3396. return 0;
  3397. } else {
  3398. return results.physical;
  3399. }
  3400. }
  3401. static int otg_power_get_property_usb(struct power_supply *psy,
  3402. enum power_supply_property psp,
  3403. union power_supply_propval *val)
  3404. {
  3405. struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
  3406. switch (psp) {
  3407. case POWER_SUPPLY_PROP_SCOPE:
  3408. if (motg->host_mode)
  3409. val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
  3410. else
  3411. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  3412. break;
  3413. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  3414. val->intval = motg->voltage_max;
  3415. break;
  3416. case POWER_SUPPLY_PROP_CURRENT_MAX:
  3417. val->intval = motg->current_max;
  3418. break;
  3419. /* Reflect USB enumeration */
  3420. case POWER_SUPPLY_PROP_PRESENT:
  3421. case POWER_SUPPLY_PROP_ONLINE:
  3422. val->intval = motg->online;
  3423. break;
  3424. case POWER_SUPPLY_PROP_TYPE:
  3425. val->intval = psy->type;
  3426. break;
  3427. case POWER_SUPPLY_PROP_HEALTH:
  3428. val->intval = motg->usbin_health;
  3429. break;
  3430. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  3431. val->intval = otg_get_prop_usbin_voltage_now(motg);
  3432. break;
  3433. default:
  3434. return -EINVAL;
  3435. }
  3436. return 0;
  3437. }
  3438. static int otg_power_set_property_usb(struct power_supply *psy,
  3439. enum power_supply_property psp,
  3440. const union power_supply_propval *val)
  3441. {
  3442. struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
  3443. switch (psp) {
  3444. /* Process PMIC notification in PRESENT prop */
  3445. case POWER_SUPPLY_PROP_PRESENT:
  3446. msm_otg_set_vbus_state(val->intval);
  3447. break;
  3448. /* The ONLINE property reflects if usb has enumerated */
  3449. case POWER_SUPPLY_PROP_ONLINE:
  3450. motg->online = val->intval;
  3451. pr_info("POWER_SUPPLY_PROP_ONLINE motg->online : %d, val->intval : %d psy->type : %d\n",
  3452. motg->online, val->intval, psy->type);
  3453. break;
  3454. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  3455. motg->voltage_max = val->intval;
  3456. break;
  3457. case POWER_SUPPLY_PROP_CURRENT_MAX:
  3458. motg->current_max = val->intval;
  3459. break;
  3460. case POWER_SUPPLY_PROP_TYPE:
  3461. psy->type = val->intval;
  3462. pr_info("POWER_SUPPLY_PROP_TYPE motg->online : %d, val->intval : %d psy->type : %d\n",
  3463. motg->online, val->intval, psy->type);
  3464. break;
  3465. case POWER_SUPPLY_PROP_HEALTH:
  3466. motg->usbin_health = val->intval;
  3467. break;
  3468. default:
  3469. return -EINVAL;
  3470. }
  3471. power_supply_changed(&motg->usb_psy);
  3472. return 0;
  3473. }
  3474. static int otg_power_property_is_writeable_usb(struct power_supply *psy,
  3475. enum power_supply_property psp)
  3476. {
  3477. switch (psp) {
  3478. case POWER_SUPPLY_PROP_HEALTH:
  3479. case POWER_SUPPLY_PROP_PRESENT:
  3480. case POWER_SUPPLY_PROP_ONLINE:
  3481. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  3482. case POWER_SUPPLY_PROP_CURRENT_MAX:
  3483. return 1;
  3484. default:
  3485. break;
  3486. }
  3487. return 0;
  3488. }
  3489. static char *otg_pm_power_supplied_to[] = {
  3490. "battery",
  3491. };
  3492. static enum power_supply_property otg_pm_power_props_usb[] = {
  3493. POWER_SUPPLY_PROP_HEALTH,
  3494. POWER_SUPPLY_PROP_PRESENT,
  3495. POWER_SUPPLY_PROP_ONLINE,
  3496. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  3497. POWER_SUPPLY_PROP_CURRENT_MAX,
  3498. POWER_SUPPLY_PROP_SCOPE,
  3499. POWER_SUPPLY_PROP_TYPE,
  3500. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  3501. };
  3502. #endif
  3503. const struct file_operations msm_otg_bus_fops = {
  3504. .open = msm_otg_bus_open,
  3505. .read = seq_read,
  3506. .write = msm_otg_bus_write,
  3507. .llseek = seq_lseek,
  3508. .release = single_release,
  3509. };
  3510. static struct dentry *msm_otg_dbg_root;
  3511. static int msm_otg_debugfs_init(struct msm_otg *motg)
  3512. {
  3513. struct dentry *msm_otg_dentry;
  3514. msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
  3515. if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
  3516. return -ENODEV;
  3517. if (motg->pdata->mode == USB_OTG &&
  3518. motg->pdata->otg_control == OTG_USER_CONTROL) {
  3519. msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
  3520. S_IWUSR, msm_otg_dbg_root, motg,
  3521. &msm_otg_mode_fops);
  3522. if (!msm_otg_dentry) {
  3523. debugfs_remove(msm_otg_dbg_root);
  3524. msm_otg_dbg_root = NULL;
  3525. return -ENODEV;
  3526. }
  3527. }
  3528. msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
  3529. msm_otg_dbg_root, motg,
  3530. &msm_otg_chg_fops);
  3531. if (!msm_otg_dentry) {
  3532. debugfs_remove_recursive(msm_otg_dbg_root);
  3533. return -ENODEV;
  3534. }
  3535. msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
  3536. msm_otg_dbg_root, motg,
  3537. &msm_otg_aca_fops);
  3538. if (!msm_otg_dentry) {
  3539. debugfs_remove_recursive(msm_otg_dbg_root);
  3540. return -ENODEV;
  3541. }
  3542. msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
  3543. msm_otg_dbg_root, motg,
  3544. &msm_otg_bus_fops);
  3545. if (!msm_otg_dentry) {
  3546. debugfs_remove_recursive(msm_otg_dbg_root);
  3547. return -ENODEV;
  3548. }
  3549. msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
  3550. msm_otg_dbg_root, motg, &msm_otg_state_fops);
  3551. if (!msm_otg_dentry) {
  3552. debugfs_remove_recursive(msm_otg_dbg_root);
  3553. return -ENODEV;
  3554. }
  3555. return 0;
  3556. }
  3557. static void msm_otg_debugfs_cleanup(void)
  3558. {
  3559. debugfs_remove_recursive(msm_otg_dbg_root);
  3560. }
  3561. #define MSM_OTG_CMD_ID 0x09
  3562. #define MSM_OTG_DEVICE_ID 0x04
  3563. #define MSM_OTG_VMID_IDX 0xFF
  3564. #define MSM_OTG_MEM_TYPE 0x02
  3565. struct msm_otg_scm_cmd_buf {
  3566. unsigned int device_id;
  3567. unsigned int vmid_idx;
  3568. unsigned int mem_type;
  3569. } __attribute__ ((__packed__));
  3570. static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
  3571. {
  3572. int ret;
  3573. struct msm_otg_platform_data *pdata = motg->pdata;
  3574. struct msm_otg_scm_cmd_buf cmd_buf;
  3575. if (!pdata->pnoc_errata_fix)
  3576. return;
  3577. dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
  3578. cmd_buf.device_id = MSM_OTG_DEVICE_ID;
  3579. cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
  3580. cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
  3581. ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
  3582. sizeof(cmd_buf), NULL, 0);
  3583. if (ret)
  3584. dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
  3585. }
  3586. static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
  3587. static struct platform_device *msm_otg_add_pdev(
  3588. struct platform_device *ofdev, const char *name)
  3589. {
  3590. struct platform_device *pdev;
  3591. const struct resource *res = ofdev->resource;
  3592. unsigned int num = ofdev->num_resources;
  3593. int retval;
  3594. struct ci13xxx_platform_data ci_pdata;
  3595. struct msm_otg_platform_data *otg_pdata;
  3596. pdev = platform_device_alloc(name, -1);
  3597. if (!pdev) {
  3598. retval = -ENOMEM;
  3599. goto error;
  3600. }
  3601. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  3602. pdev->dev.dma_mask = &msm_otg_dma_mask;
  3603. if (num) {
  3604. retval = platform_device_add_resources(pdev, res, num);
  3605. if (retval)
  3606. goto error;
  3607. }
  3608. if (!strcmp(name, "msm_hsusb")) {
  3609. otg_pdata =
  3610. (struct msm_otg_platform_data *)
  3611. ofdev->dev.platform_data;
  3612. ci_pdata.log2_itc = otg_pdata->log2_itc;
  3613. ci_pdata.usb_core_id = 0;
  3614. ci_pdata.l1_supported = otg_pdata->l1_supported;
  3615. ci_pdata.enable_ahb2ahb_bypass =
  3616. otg_pdata->enable_ahb2ahb_bypass;
  3617. retval = platform_device_add_data(pdev, &ci_pdata,
  3618. sizeof(ci_pdata));
  3619. if (retval)
  3620. goto error;
  3621. }
  3622. retval = platform_device_add(pdev);
  3623. if (retval)
  3624. goto error;
  3625. return pdev;
  3626. error:
  3627. platform_device_put(pdev);
  3628. return ERR_PTR(retval);
  3629. }
  3630. static int msm_otg_setup_devices(struct platform_device *ofdev,
  3631. enum usb_mode_type mode, bool init)
  3632. {
  3633. const char *gadget_name = "msm_hsusb";
  3634. const char *host_name = "msm_hsusb_host";
  3635. static struct platform_device *gadget_pdev;
  3636. static struct platform_device *host_pdev;
  3637. int retval = 0;
  3638. if (!init) {
  3639. if (gadget_pdev)
  3640. platform_device_unregister(gadget_pdev);
  3641. if (host_pdev)
  3642. platform_device_unregister(host_pdev);
  3643. return 0;
  3644. }
  3645. switch (mode) {
  3646. case USB_OTG:
  3647. /* fall through */
  3648. case USB_PERIPHERAL:
  3649. gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
  3650. if (IS_ERR(gadget_pdev)) {
  3651. retval = PTR_ERR(gadget_pdev);
  3652. break;
  3653. }
  3654. if (mode == USB_PERIPHERAL)
  3655. break;
  3656. /* fall through */
  3657. case USB_HOST:
  3658. host_pdev = msm_otg_add_pdev(ofdev, host_name);
  3659. if (IS_ERR(host_pdev)) {
  3660. retval = PTR_ERR(host_pdev);
  3661. if (mode == USB_OTG)
  3662. platform_device_unregister(gadget_pdev);
  3663. }
  3664. break;
  3665. default:
  3666. break;
  3667. }
  3668. return retval;
  3669. }
  3670. #ifdef CONFIG_QPNP_SEC_CHARGER
  3671. static int msm_otg_register_power_supply(struct platform_device *pdev,
  3672. struct msm_otg *motg)
  3673. {
  3674. int ret;
  3675. ret = power_supply_register(&pdev->dev, &motg->usb_psy);
  3676. if (ret < 0) {
  3677. dev_err(motg->phy.dev,
  3678. "%s:power_supply_register usb failed\n",
  3679. __func__);
  3680. return ret;
  3681. }
  3682. legacy_power_supply = false;
  3683. return 0;
  3684. }
  3685. #endif
  3686. static int msm_otg_ext_chg_open(struct inode *inode, struct file *file)
  3687. {
  3688. struct msm_otg *motg = the_msm_otg;
  3689. pr_debug("msm_otg ext chg open\n");
  3690. motg->ext_chg_opened = true;
  3691. file->private_data = (void *)motg;
  3692. return 0;
  3693. }
  3694. static long
  3695. msm_otg_ext_chg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3696. {
  3697. struct msm_otg *motg = file->private_data;
  3698. struct msm_usb_chg_info info = {0};
  3699. int ret = 0, val;
  3700. switch (cmd) {
  3701. case MSM_USB_EXT_CHG_INFO:
  3702. info.chg_block_type = USB_CHG_BLOCK_ULPI;
  3703. info.page_offset = motg->io_res->start & ~PAGE_MASK;
  3704. /* mmap() works on PAGE granularity */
  3705. info.length = PAGE_SIZE;
  3706. if (copy_to_user((void __user *)arg, &info, sizeof(info))) {
  3707. pr_err("%s: copy to user failed\n\n", __func__);
  3708. ret = -EFAULT;
  3709. }
  3710. break;
  3711. case MSM_USB_EXT_CHG_BLOCK_LPM:
  3712. if (get_user(val, (int __user *)arg)) {
  3713. pr_err("%s: get_user failed\n\n", __func__);
  3714. ret = -EFAULT;
  3715. break;
  3716. }
  3717. pr_debug("%s: LPM block request %d\n", __func__, val);
  3718. if (val) { /* block LPM */
  3719. if (motg->chg_type == USB_DCP_CHARGER) {
  3720. motg->ext_chg_active = ACTIVE;
  3721. /*
  3722. * If device is already suspended, resume it.
  3723. * The PM usage counter is incremented in
  3724. * runtime resume method. if device is not
  3725. * suspended, cancel the scheduled suspend
  3726. * and increment the PM usage counter.
  3727. */
  3728. if (pm_runtime_suspended(motg->phy.dev))
  3729. pm_runtime_resume(motg->phy.dev);
  3730. else
  3731. pm_runtime_get_sync(motg->phy.dev);
  3732. } else {
  3733. motg->ext_chg_active = INACTIVE;
  3734. complete(&motg->ext_chg_wait);
  3735. ret = -ENODEV;
  3736. }
  3737. } else {
  3738. motg->ext_chg_active = INACTIVE;
  3739. complete(&motg->ext_chg_wait);
  3740. /*
  3741. * If usb cable is disconnected and then userspace
  3742. * calls ioctl to unblock low power mode, make sure
  3743. * otg_sm work for usb disconnect is processed first
  3744. * followed by decrementing the PM usage counters.
  3745. */
  3746. flush_work(&motg->sm_work);
  3747. pm_runtime_put_noidle(motg->phy.dev);
  3748. motg->pm_done = 1;
  3749. pm_runtime_suspend(motg->phy.dev);
  3750. }
  3751. break;
  3752. case MSM_USB_EXT_CHG_VOLTAGE_INFO:
  3753. if (get_user(val, (int __user *)arg)) {
  3754. pr_err("%s: get_user failed\n\n", __func__);
  3755. ret = -EFAULT;
  3756. break;
  3757. }
  3758. if (val == USB_REQUEST_5V)
  3759. pr_debug("%s:voting 5V voltage request\n", __func__);
  3760. else if (val == USB_REQUEST_9V)
  3761. pr_debug("%s:voting 9V voltage request\n", __func__);
  3762. break;
  3763. case MSM_USB_EXT_CHG_RESULT:
  3764. if (get_user(val, (int __user *)arg)) {
  3765. pr_err("%s: get_user failed\n\n", __func__);
  3766. ret = -EFAULT;
  3767. break;
  3768. }
  3769. if (!val)
  3770. pr_debug("%s:voltage request successful\n", __func__);
  3771. else
  3772. pr_debug("%s:voltage request failed\n", __func__);
  3773. break;
  3774. case MSM_USB_EXT_CHG_TYPE:
  3775. if (get_user(val, (int __user *)arg)) {
  3776. pr_err("%s: get_user failed\n\n", __func__);
  3777. ret = -EFAULT;
  3778. break;
  3779. }
  3780. if (val)
  3781. pr_debug("%s:charger is external charger\n", __func__);
  3782. else
  3783. pr_debug("%s:charger is not ext charger\n", __func__);
  3784. break;
  3785. default:
  3786. ret = -EINVAL;
  3787. }
  3788. return ret;
  3789. }
  3790. static int msm_otg_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
  3791. {
  3792. struct msm_otg *motg = file->private_data;
  3793. unsigned long vsize = vma->vm_end - vma->vm_start;
  3794. int ret;
  3795. if (vma->vm_pgoff || vsize > PAGE_SIZE)
  3796. return -EINVAL;
  3797. vma->vm_pgoff = __phys_to_pfn(motg->io_res->start);
  3798. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  3799. ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  3800. vsize, vma->vm_page_prot);
  3801. if (ret < 0) {
  3802. pr_err("%s: failed with return val %d\n", __func__, ret);
  3803. return ret;
  3804. }
  3805. return 0;
  3806. }
  3807. static int msm_otg_ext_chg_release(struct inode *inode, struct file *file)
  3808. {
  3809. struct msm_otg *motg = file->private_data;
  3810. pr_debug("msm_otg ext chg release\n");
  3811. motg->ext_chg_opened = false;
  3812. return 0;
  3813. }
  3814. static const struct file_operations msm_otg_ext_chg_fops = {
  3815. .owner = THIS_MODULE,
  3816. .open = msm_otg_ext_chg_open,
  3817. .unlocked_ioctl = msm_otg_ext_chg_ioctl,
  3818. .mmap = msm_otg_ext_chg_mmap,
  3819. .release = msm_otg_ext_chg_release,
  3820. };
  3821. static int msm_otg_setup_ext_chg_cdev(struct msm_otg *motg)
  3822. {
  3823. int ret;
  3824. if (motg->pdata->enable_sec_phy || motg->pdata->mode == USB_HOST ||
  3825. motg->pdata->otg_control != OTG_PMIC_CONTROL ||
  3826. psy != &motg->usb_psy) {
  3827. pr_debug("usb ext chg is not supported by msm otg\n");
  3828. return -ENODEV;
  3829. }
  3830. ret = alloc_chrdev_region(&motg->ext_chg_dev, 0, 1, "usb_ext_chg");
  3831. if (ret < 0) {
  3832. pr_err("Fail to allocate usb ext char dev region\n");
  3833. return ret;
  3834. }
  3835. motg->ext_chg_class = class_create(THIS_MODULE, "msm_ext_chg");
  3836. if (ret < 0) {
  3837. pr_err("Fail to create usb ext chg class\n");
  3838. goto unreg_chrdev;
  3839. }
  3840. cdev_init(&motg->ext_chg_cdev, &msm_otg_ext_chg_fops);
  3841. motg->ext_chg_cdev.owner = THIS_MODULE;
  3842. ret = cdev_add(&motg->ext_chg_cdev, motg->ext_chg_dev, 1);
  3843. if (ret < 0) {
  3844. pr_err("Fail to add usb ext chg cdev\n");
  3845. goto destroy_class;
  3846. }
  3847. motg->ext_chg_device = device_create(motg->ext_chg_class,
  3848. NULL, motg->ext_chg_dev, NULL,
  3849. "usb_ext_chg");
  3850. if (IS_ERR(motg->ext_chg_device)) {
  3851. pr_err("Fail to create usb ext chg device\n");
  3852. ret = PTR_ERR(motg->ext_chg_device);
  3853. motg->ext_chg_device = NULL;
  3854. goto del_cdev;
  3855. }
  3856. init_completion(&motg->ext_chg_wait);
  3857. pr_debug("msm otg ext chg cdev setup success\n");
  3858. return 0;
  3859. del_cdev:
  3860. cdev_del(&motg->ext_chg_cdev);
  3861. destroy_class:
  3862. class_destroy(motg->ext_chg_class);
  3863. unreg_chrdev:
  3864. unregister_chrdev_region(motg->ext_chg_dev, 1);
  3865. return ret;
  3866. }
  3867. #ifdef CONFIG_USBIRQ_BALANCING_LTE_HIGHTP
  3868. static int clear_cpu0_from_usb_irq(bool enable)
  3869. {
  3870. int err = 0;
  3871. unsigned int irq = otg_irq;
  3872. cpumask_var_t new_value;
  3873. if (!irq_can_set_affinity(irq))
  3874. return -EIO;
  3875. if (!alloc_cpumask_var(&new_value, GFP_KERNEL))
  3876. return -ENOMEM;
  3877. cpumask_setall(new_value);
  3878. if (enable)
  3879. cpumask_clear_cpu(0, new_value);
  3880. if (!cpumask_intersects(new_value, cpu_online_mask)) {
  3881. err = irq_select_affinity_usr(irq, new_value);
  3882. } else {
  3883. err = irq_set_affinity(irq, new_value);
  3884. }
  3885. free_cpumask_var(new_value);
  3886. return err;
  3887. }
  3888. static int rndis_notify_callback(struct notifier_block *this,
  3889. unsigned long event, void *ptr)
  3890. {
  3891. struct net_device *dev = ptr;
  3892. if (!net_eq(dev_net(dev), &init_net))
  3893. return NOTIFY_DONE;
  3894. if (!strncmp(dev->name, "rndis", 5)) {
  3895. switch (event) {
  3896. case NETDEV_UP:
  3897. is_rndis_running = true;
  3898. if (!clear_cpu0_from_usb_irq(true)) {
  3899. is_irq_masked = true;
  3900. }
  3901. break;
  3902. case NETDEV_DOWN:
  3903. clear_cpu0_from_usb_irq(false);
  3904. is_irq_masked = false;
  3905. is_rndis_running = false;
  3906. break;
  3907. }
  3908. }
  3909. return NOTIFY_DONE;
  3910. }
  3911. static int hotplug_notify_callback(struct notifier_block *n,
  3912. unsigned long action, void *hcpu)
  3913. {
  3914. if (is_rndis_running) {
  3915. switch (action) {
  3916. case CPU_POST_DEAD:
  3917. if (is_irq_masked && num_online_cpus()==1) {
  3918. is_irq_masked = false;
  3919. }
  3920. break;
  3921. case CPU_ONLINE:
  3922. if (!is_irq_masked && num_online_cpus()==2) {
  3923. if (!clear_cpu0_from_usb_irq(true)) {
  3924. is_irq_masked = true;
  3925. }
  3926. }
  3927. break;
  3928. }
  3929. }
  3930. return NOTIFY_OK;
  3931. }
  3932. #endif
  3933. static ssize_t dpdm_pulldown_enable_show(struct device *dev,
  3934. struct device_attribute *attr, char *buf)
  3935. {
  3936. struct msm_otg *motg = the_msm_otg;
  3937. struct msm_otg_platform_data *pdata = motg->pdata;
  3938. return snprintf(buf, PAGE_SIZE, "%s\n", pdata->dpdm_pulldown_added ?
  3939. "enabled" : "disabled");
  3940. }
  3941. static ssize_t dpdm_pulldown_enable_store(struct device *dev,
  3942. struct device_attribute *attr, const char
  3943. *buf, size_t size)
  3944. {
  3945. struct msm_otg *motg = the_msm_otg;
  3946. struct msm_otg_platform_data *pdata = motg->pdata;
  3947. if (!strnicmp(buf, "enable", 6)) {
  3948. pdata->dpdm_pulldown_added = true;
  3949. return size;
  3950. } else if (!strnicmp(buf, "disable", 7)) {
  3951. pdata->dpdm_pulldown_added = false;
  3952. return size;
  3953. }
  3954. return -EINVAL;
  3955. }
  3956. static DEVICE_ATTR(dpdm_pulldown_enable, S_IRUGO | S_IWUSR,
  3957. dpdm_pulldown_enable_show, dpdm_pulldown_enable_store);
  3958. struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
  3959. {
  3960. struct device_node *node = pdev->dev.of_node;
  3961. struct msm_otg_platform_data *pdata;
  3962. int len = 0;
  3963. pr_info("%s\n",__func__);
  3964. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  3965. if (!pdata) {
  3966. pr_err("unable to allocate platform data\n");
  3967. return NULL;
  3968. }
  3969. of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
  3970. if (len) {
  3971. pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
  3972. if (!pdata->phy_init_seq)
  3973. return NULL;
  3974. of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
  3975. pdata->phy_init_seq,
  3976. len/sizeof(*pdata->phy_init_seq));
  3977. }
  3978. of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
  3979. &pdata->power_budget);
  3980. of_property_read_u32(node, "qcom,hsusb-otg-mode",
  3981. &pdata->mode);
  3982. of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
  3983. &pdata->otg_control);
  3984. of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
  3985. &pdata->default_mode);
  3986. of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
  3987. &pdata->phy_type);
  3988. pdata->disable_reset_on_disconnect = of_property_read_bool(node,
  3989. "qcom,hsusb-otg-disable-reset");
  3990. pdata->pnoc_errata_fix = of_property_read_bool(node,
  3991. "qcom,hsusb-otg-pnoc-errata-fix");
  3992. pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
  3993. "qcom,hsusb-otg-lpm-on-dev-suspend");
  3994. pdata->core_clk_always_on_workaround = of_property_read_bool(node,
  3995. "qcom,hsusb-otg-clk-always-on-workaround");
  3996. pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
  3997. "qcom,hsusb-otg-delay-lpm");
  3998. pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
  3999. "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
  4000. pdata->dp_manual_pullup = of_property_read_bool(node,
  4001. "qcom,dp-manual-pullup");
  4002. pdata->enable_sec_phy = of_property_read_bool(node,
  4003. "qcom,usb2-enable-hsphy2");
  4004. of_property_read_u32(node, "qcom,hsusb-log2-itc",
  4005. &pdata->log2_itc);
  4006. of_property_read_u32(node, "qcom,hsusb-otg-mpm-dpsehv-int",
  4007. &pdata->mpm_dpshv_int);
  4008. of_property_read_u32(node, "qcom,hsusb-otg-mpm-dmsehv-int",
  4009. &pdata->mpm_dmshv_int);
  4010. pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
  4011. if (pdata->pmic_id_irq < 0)
  4012. pdata->pmic_id_irq = 0;
  4013. #ifdef CONFIG_USB_HOST_NOTIFY
  4014. if (pdata->mode == USB_OTG)
  4015. /*Booster Work*/
  4016. pdata->vbus_power = msm_otg_sec_power;
  4017. #endif
  4018. pdata->l1_supported = of_property_read_bool(node,
  4019. "qcom,hsusb-l1-supported");
  4020. pdata->enable_ahb2ahb_bypass = of_property_read_bool(node,
  4021. "qcom,ahb-async-bridge-bypass");
  4022. pdata->disable_retention_with_vdd_min = of_property_read_bool(node,
  4023. "qcom,disable-retention-with-vdd-min");
  4024. return pdata;
  4025. }
  4026. static int __init msm_otg_probe(struct platform_device *pdev)
  4027. {
  4028. int ret = 0;
  4029. int len = 0;
  4030. u32 tmp[3];
  4031. struct resource *res;
  4032. struct msm_otg *motg;
  4033. struct usb_phy *phy;
  4034. struct msm_otg_platform_data *pdata;
  4035. dev_info(&pdev->dev, "msm_otg probe\n");
  4036. if (pdev->dev.of_node) {
  4037. dev_dbg(&pdev->dev, "device tree enabled\n");
  4038. pdata = msm_otg_dt_to_pdata(pdev);
  4039. if (!pdata)
  4040. return -ENOMEM;
  4041. pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
  4042. if (!pdata->bus_scale_table)
  4043. dev_dbg(&pdev->dev, "bus scaling is disabled\n");
  4044. pdev->dev.platform_data = pdata;
  4045. ret = msm_otg_setup_devices(pdev, pdata->mode, true);
  4046. if (ret) {
  4047. dev_err(&pdev->dev, "devices setup failed\n");
  4048. return ret;
  4049. }
  4050. } else if (!pdev->dev.platform_data) {
  4051. dev_err(&pdev->dev, "No platform data given. Bailing out\n");
  4052. return -ENODEV;
  4053. } else {
  4054. pdata = pdev->dev.platform_data;
  4055. }
  4056. motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
  4057. if (!motg) {
  4058. dev_err(&pdev->dev, "unable to allocate msm_otg\n");
  4059. return -ENOMEM;
  4060. }
  4061. motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
  4062. GFP_KERNEL);
  4063. if (!motg->phy.otg) {
  4064. dev_err(&pdev->dev, "unable to allocate usb_otg\n");
  4065. return -ENOMEM;
  4066. }
  4067. the_msm_otg = motg;
  4068. motg->pdata = pdata;
  4069. phy = &motg->phy;
  4070. phy->dev = &pdev->dev;
  4071. if (motg->pdata->bus_scale_table) {
  4072. motg->bus_perf_client =
  4073. msm_bus_scale_register_client(motg->pdata->bus_scale_table);
  4074. if (!motg->bus_perf_client) {
  4075. dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
  4076. "scaling client!!\n", __func__);
  4077. } else {
  4078. debug_bus_voting_enabled = true;
  4079. /* Some platforms require BUS vote to control clocks */
  4080. msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
  4081. }
  4082. }
  4083. /*
  4084. * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
  4085. * PHY treat ACA ID_GND as float and no interrupt is generated. But
  4086. * PMIC can detect ACA ID_GND and generate an interrupt.
  4087. */
  4088. if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
  4089. dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
  4090. ret = -EINVAL;
  4091. goto devote_bus_bw;
  4092. }
  4093. /* initialize reset counter */
  4094. motg->reset_counter = 0;
  4095. /*
  4096. * Targets on which link uses asynchronous reset methodology,
  4097. * free running clock is not required during the reset.
  4098. */
  4099. motg->clk = clk_get(&pdev->dev, "alt_core_clk");
  4100. if (IS_ERR(motg->clk))
  4101. dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
  4102. else
  4103. clk_set_rate(motg->clk, 60000000);
  4104. /*
  4105. * USB Core is running its protocol engine based on CORE CLK,
  4106. * CORE CLK must be running at >55Mhz for correct HSUSB
  4107. * operation and USB core cannot tolerate frequency changes on
  4108. * CORE CLK. For such USB cores, vote for maximum clk frequency
  4109. * on pclk source
  4110. */
  4111. motg->core_clk = clk_get(&pdev->dev, "core_clk");
  4112. if (IS_ERR(motg->core_clk)) {
  4113. motg->core_clk = NULL;
  4114. dev_err(&pdev->dev, "failed to get core_clk\n");
  4115. ret = PTR_ERR(motg->core_clk);
  4116. goto put_clk;
  4117. }
  4118. /*
  4119. * Get Max supported clk frequency for USB Core CLK and request
  4120. * to set the same.
  4121. */
  4122. motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
  4123. if (IS_ERR_VALUE(motg->core_clk_rate)) {
  4124. dev_err(&pdev->dev, "fail to get core clk max freq.\n");
  4125. } else {
  4126. ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
  4127. if (ret)
  4128. dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
  4129. ret);
  4130. }
  4131. motg->pclk = clk_get(&pdev->dev, "iface_clk");
  4132. if (IS_ERR(motg->pclk)) {
  4133. dev_err(&pdev->dev, "failed to get iface_clk\n");
  4134. ret = PTR_ERR(motg->pclk);
  4135. goto put_core_clk;
  4136. }
  4137. /*
  4138. * On few platforms USB PHY is fed with sleep clk.
  4139. * Hence don't fail probe.
  4140. */
  4141. motg->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
  4142. if (IS_ERR(motg->sleep_clk)) {
  4143. dev_dbg(&pdev->dev, "failed to get sleep_clk\n");
  4144. } else {
  4145. ret = clk_prepare_enable(motg->sleep_clk);
  4146. if (ret) {
  4147. dev_err(&pdev->dev, "%s failed to vote sleep_clk%d\n",
  4148. __func__, ret);
  4149. goto put_pclk;
  4150. }
  4151. }
  4152. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4153. if (!res) {
  4154. dev_err(&pdev->dev, "failed to get platform resource mem\n");
  4155. ret = -ENODEV;
  4156. goto disable_sleep_clk;
  4157. }
  4158. motg->io_res = res;
  4159. motg->regs = ioremap(res->start, resource_size(res));
  4160. if (!motg->regs) {
  4161. dev_err(&pdev->dev, "ioremap failed\n");
  4162. ret = -ENOMEM;
  4163. goto disable_sleep_clk;
  4164. }
  4165. dev_info(&pdev->dev, "OTG regs = %pK\n", motg->regs);
  4166. if (pdata->enable_sec_phy)
  4167. motg->usb_phy_ctrl_reg = USB_PHY_CTRL2;
  4168. else
  4169. motg->usb_phy_ctrl_reg = USB_PHY_CTRL;
  4170. motg->irq = platform_get_irq(pdev, 0);
  4171. if (!motg->irq) {
  4172. dev_err(&pdev->dev, "platform_get_irq failed\n");
  4173. ret = -ENODEV;
  4174. goto free_regs;
  4175. }
  4176. motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
  4177. if (motg->async_irq < 0) {
  4178. dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
  4179. motg->async_irq = 0;
  4180. }
  4181. motg->xo_clk = clk_get(&pdev->dev, "xo");
  4182. if (IS_ERR(motg->xo_clk)) {
  4183. motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
  4184. if (IS_ERR(motg->xo_handle)) {
  4185. dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
  4186. __func__);
  4187. ret = PTR_ERR(motg->xo_handle);
  4188. goto free_regs;
  4189. } else {
  4190. ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
  4191. if (ret) {
  4192. dev_err(&pdev->dev, "%s XO voting failed %d\n",
  4193. __func__, ret);
  4194. goto free_xo_handle;
  4195. }
  4196. }
  4197. } else {
  4198. ret = clk_prepare_enable(motg->xo_clk);
  4199. if (ret) {
  4200. dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
  4201. __func__, ret);
  4202. goto free_xo_handle;
  4203. }
  4204. }
  4205. clk_prepare_enable(motg->pclk);
  4206. motg->vdd_type = VDDCX_CORNER;
  4207. hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
  4208. if (IS_ERR(hsusb_vdd)) {
  4209. hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
  4210. if (IS_ERR(hsusb_vdd)) {
  4211. dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
  4212. ret = PTR_ERR(hsusb_vdd);
  4213. goto devote_xo_handle;
  4214. }
  4215. motg->vdd_type = VDDCX;
  4216. }
  4217. if (pdev->dev.of_node) {
  4218. of_get_property(pdev->dev.of_node,
  4219. "qcom,vdd-voltage-level",
  4220. &len);
  4221. if (len == sizeof(tmp)) {
  4222. of_property_read_u32_array(pdev->dev.of_node,
  4223. "qcom,vdd-voltage-level",
  4224. tmp, len/sizeof(*tmp));
  4225. vdd_val[motg->vdd_type][0] = tmp[0];
  4226. vdd_val[motg->vdd_type][1] = tmp[1];
  4227. vdd_val[motg->vdd_type][2] = tmp[2];
  4228. } else {
  4229. dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
  4230. }
  4231. }
  4232. ret = msm_hsusb_config_vddcx(1);
  4233. if (ret) {
  4234. dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
  4235. goto devote_xo_handle;
  4236. }
  4237. ret = regulator_enable(hsusb_vdd);
  4238. if (ret) {
  4239. dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
  4240. goto free_config_vddcx;
  4241. }
  4242. ret = msm_hsusb_ldo_init(motg, 1);
  4243. if (ret) {
  4244. dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
  4245. goto free_hsusb_vdd;
  4246. }
  4247. if (pdata->mhl_enable) {
  4248. mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
  4249. "mhl_usb_hs_switch");
  4250. if (IS_ERR(mhl_usb_hs_switch)) {
  4251. dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
  4252. ret = PTR_ERR(mhl_usb_hs_switch);
  4253. goto free_ldo_init;
  4254. }
  4255. }
  4256. ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
  4257. if (ret) {
  4258. dev_err(&pdev->dev, "hsusb vreg enable failed\n");
  4259. goto free_ldo_init;
  4260. }
  4261. clk_prepare_enable(motg->core_clk);
  4262. /* Check if USB mem_type change is needed to workaround PNOC hw issue */
  4263. msm_otg_pnoc_errata_fix(motg);
  4264. writel(0, USB_USBINTR);
  4265. writel(0, USB_OTGSC);
  4266. /* Ensure that above STOREs are completed before enabling interrupts */
  4267. mb();
  4268. ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
  4269. if (ret)
  4270. dev_dbg(&pdev->dev, "MHL can not be supported\n");
  4271. wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
  4272. msm_otg_init_timer(motg);
  4273. INIT_WORK(&motg->sm_work, msm_otg_sm_work);
  4274. INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
  4275. INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
  4276. INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
  4277. setup_timer(&motg->id_timer, msm_otg_id_timer_func,
  4278. (unsigned long) motg);
  4279. setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
  4280. (unsigned long) motg);
  4281. ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
  4282. "msm_otg", motg);
  4283. if (ret) {
  4284. dev_err(&pdev->dev, "request irq failed\n");
  4285. goto destroy_wlock;
  4286. }
  4287. if (motg->async_irq) {
  4288. ret = request_irq(motg->async_irq, msm_otg_irq,
  4289. IRQF_TRIGGER_RISING, "msm_otg", motg);
  4290. if (ret) {
  4291. dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
  4292. goto free_irq;
  4293. }
  4294. disable_irq(motg->async_irq);
  4295. }
  4296. if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
  4297. msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
  4298. if (pdata->mpm_dpshv_int)
  4299. msm_mpm_enable_pin(pdata->mpm_dpshv_int, 1);
  4300. if (pdata->mpm_dmshv_int)
  4301. msm_mpm_enable_pin(pdata->mpm_dmshv_int, 1);
  4302. phy->init = msm_otg_reset;
  4303. phy->set_power = msm_otg_set_power;
  4304. phy->set_suspend = msm_otg_set_suspend;
  4305. #ifdef CONFIG_USB_HOST_NOTIFY
  4306. phy->set_suspend = NULL;
  4307. #endif
  4308. phy->io_ops = &msm_otg_io_ops;
  4309. phy->otg->phy = &motg->phy;
  4310. phy->otg->set_host = msm_otg_set_host;
  4311. phy->otg->set_peripheral = msm_otg_set_peripheral;
  4312. phy->otg->start_hnp = msm_otg_start_hnp;
  4313. phy->otg->start_srp = msm_otg_start_srp;
  4314. if (pdata->dp_manual_pullup)
  4315. phy->flags |= ENABLE_DP_MANUAL_PULLUP;
  4316. if (pdata->enable_sec_phy)
  4317. phy->flags |= ENABLE_SECONDARY_PHY;
  4318. ret = usb_set_transceiver(&motg->phy);
  4319. if (ret) {
  4320. dev_err(&pdev->dev, "usb_set_transceiver failed\n");
  4321. goto free_async_irq;
  4322. }
  4323. #ifdef CONFIG_USB_HOST_NOTIFY
  4324. msm_host_notify_init(&pdev->dev, motg);
  4325. #endif
  4326. msm_hsusb_mhl_switch_enable(motg, 1);
  4327. platform_set_drvdata(pdev, motg);
  4328. device_init_wakeup(&pdev->dev, 1);
  4329. motg->mA_port = IUNIT;
  4330. ret = msm_otg_debugfs_init(motg);
  4331. if (ret)
  4332. dev_dbg(&pdev->dev, "mode debugfs file is"
  4333. "not available\n");
  4334. if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
  4335. if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
  4336. (!(motg->pdata->mode == USB_OTG) ||
  4337. motg->pdata->pmic_id_irq))
  4338. motg->caps = ALLOW_PHY_POWER_COLLAPSE |
  4339. ALLOW_PHY_RETENTION;
  4340. if (motg->pdata->otg_control == OTG_PHY_CONTROL)
  4341. motg->caps = ALLOW_PHY_RETENTION |
  4342. ALLOW_PHY_REGULATORS_LPM;
  4343. if (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int)
  4344. motg->caps |= ALLOW_HOST_PHY_RETENTION;
  4345. device_create_file(&pdev->dev,
  4346. &dev_attr_dpdm_pulldown_enable);
  4347. }
  4348. if (motg->pdata->enable_lpm_on_dev_suspend)
  4349. motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
  4350. if (motg->pdata->disable_retention_with_vdd_min)
  4351. motg->caps |= ALLOW_VDD_MIN_WITH_RETENTION_DISABLED;
  4352. wake_lock(&motg->wlock);
  4353. pm_runtime_set_active(&pdev->dev);
  4354. pm_runtime_enable(&pdev->dev);
  4355. if (motg->pdata->delay_lpm_on_disconnect) {
  4356. pm_runtime_set_autosuspend_delay(&pdev->dev,
  4357. lpm_disconnect_thresh);
  4358. pm_runtime_use_autosuspend(&pdev->dev);
  4359. }
  4360. #ifdef CONFIG_QPNP_SEC_CHARGER
  4361. motg->usb_psy.name = "usb";
  4362. motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
  4363. motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
  4364. motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
  4365. motg->usb_psy.properties = otg_pm_power_props_usb;
  4366. motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
  4367. motg->usb_psy.get_property = otg_power_get_property_usb;
  4368. motg->usb_psy.set_property = otg_power_set_property_usb;
  4369. motg->usb_psy.property_is_writeable
  4370. = otg_power_property_is_writeable_usb;
  4371. if (!pm8921_charger_register_vbus_sn(NULL)) {
  4372. /* if pm8921 use legacy implementation */
  4373. dev_info(motg->phy.dev, "%s: legacy support\n", __func__);
  4374. legacy_power_supply = true;
  4375. } else {
  4376. /* otherwise register our own power supply */
  4377. if (!msm_otg_register_power_supply(pdev, motg))
  4378. psy = &motg->usb_psy;
  4379. }
  4380. #endif
  4381. #ifndef USE_MUIC_CHGTYPE
  4382. if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
  4383. pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
  4384. #endif
  4385. ret = msm_otg_setup_ext_chg_cdev(motg);
  4386. if (ret)
  4387. dev_dbg(&pdev->dev, "fail to setup cdev\n");
  4388. motg->pm_notify.notifier_call = msm_otg_pm_notify;
  4389. register_pm_notifier(&motg->pm_notify);
  4390. #ifdef CONFIG_USBIRQ_BALANCING_LTE_HIGHTP
  4391. is_rndis_running = false;
  4392. is_irq_masked = false;
  4393. otg_irq = motg->irq;
  4394. rndis_notifier.notifier_call = rndis_notify_callback;
  4395. register_netdevice_notifier(&rndis_notifier);
  4396. cpu_hotplug_notifier.notifier_call = hotplug_notify_callback;
  4397. register_cpu_notifier(&cpu_hotplug_notifier);
  4398. #endif
  4399. return 0;
  4400. free_async_irq:
  4401. if (motg->async_irq)
  4402. free_irq(motg->async_irq, motg);
  4403. free_irq:
  4404. free_irq(motg->irq, motg);
  4405. destroy_wlock:
  4406. wake_lock_destroy(&motg->wlock);
  4407. clk_disable_unprepare(motg->core_clk);
  4408. msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
  4409. free_ldo_init:
  4410. msm_hsusb_ldo_init(motg, 0);
  4411. free_hsusb_vdd:
  4412. regulator_disable(hsusb_vdd);
  4413. free_config_vddcx:
  4414. regulator_set_voltage(hsusb_vdd,
  4415. vdd_val[motg->vdd_type][VDD_NONE],
  4416. vdd_val[motg->vdd_type][VDD_MAX]);
  4417. devote_xo_handle:
  4418. clk_disable_unprepare(motg->pclk);
  4419. if (!IS_ERR(motg->xo_clk))
  4420. clk_disable_unprepare(motg->xo_clk);
  4421. else
  4422. msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
  4423. free_xo_handle:
  4424. if (!IS_ERR(motg->xo_clk))
  4425. clk_put(motg->xo_clk);
  4426. else
  4427. msm_xo_put(motg->xo_handle);
  4428. free_regs:
  4429. iounmap(motg->regs);
  4430. disable_sleep_clk:
  4431. if (!IS_ERR(motg->sleep_clk))
  4432. clk_disable_unprepare(motg->sleep_clk);
  4433. put_pclk:
  4434. clk_put(motg->pclk);
  4435. put_core_clk:
  4436. clk_put(motg->core_clk);
  4437. put_clk:
  4438. if (!IS_ERR(motg->clk))
  4439. clk_put(motg->clk);
  4440. devote_bus_bw:
  4441. if (motg->bus_perf_client) {
  4442. msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
  4443. msm_bus_scale_unregister_client(motg->bus_perf_client);
  4444. }
  4445. return ret;
  4446. }
  4447. static int __devexit msm_otg_remove(struct platform_device *pdev)
  4448. {
  4449. struct msm_otg *motg = platform_get_drvdata(pdev);
  4450. struct usb_phy *phy = &motg->phy;
  4451. int cnt = 0;
  4452. if (phy->otg->host || phy->otg->gadget)
  4453. return -EBUSY;
  4454. unregister_pm_notifier(&motg->pm_notify);
  4455. #ifdef CONFIG_USBIRQ_BALANCING_LTE_HIGHTP
  4456. unregister_cpu_notifier(&cpu_hotplug_notifier);
  4457. unregister_netdevice_notifier(&rndis_notifier);
  4458. #endif
  4459. if (!motg->ext_chg_device) {
  4460. device_destroy(motg->ext_chg_class, motg->ext_chg_dev);
  4461. cdev_del(&motg->ext_chg_cdev);
  4462. class_destroy(motg->ext_chg_class);
  4463. unregister_chrdev_region(motg->ext_chg_dev, 1);
  4464. }
  4465. if (pdev->dev.of_node)
  4466. msm_otg_setup_devices(pdev, motg->pdata->mode, false);
  4467. if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
  4468. pm8921_charger_unregister_vbus_sn(0);
  4469. msm_otg_mhl_register_callback(motg, NULL);
  4470. msm_otg_debugfs_cleanup();
  4471. cancel_delayed_work_sync(&motg->chg_work);
  4472. cancel_delayed_work_sync(&motg->pmic_id_status_work);
  4473. cancel_delayed_work_sync(&motg->suspend_work);
  4474. cancel_work_sync(&motg->sm_work);
  4475. #ifdef CONFIG_USB_HOST_NOTIFY
  4476. msm_host_notify_exit(&pdev->dev, motg);
  4477. #endif
  4478. pm_runtime_resume(&pdev->dev);
  4479. device_init_wakeup(&pdev->dev, 0);
  4480. pm_runtime_disable(&pdev->dev);
  4481. wake_lock_destroy(&motg->wlock);
  4482. msm_hsusb_mhl_switch_enable(motg, 0);
  4483. if (motg->pdata->pmic_id_irq)
  4484. free_irq(motg->pdata->pmic_id_irq, motg);
  4485. usb_set_transceiver(NULL);
  4486. free_irq(motg->irq, motg);
  4487. if ((motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) &&
  4488. (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int))
  4489. device_remove_file(&pdev->dev,
  4490. &dev_attr_dpdm_pulldown_enable);
  4491. if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
  4492. motg->pdata->mpm_otgsessvld_int)
  4493. msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
  4494. if (motg->pdata->mpm_dpshv_int)
  4495. msm_mpm_enable_pin(motg->pdata->mpm_dpshv_int, 0);
  4496. if (motg->pdata->mpm_dmshv_int)
  4497. msm_mpm_enable_pin(motg->pdata->mpm_dmshv_int, 0);
  4498. /*
  4499. * Put PHY in low power mode.
  4500. */
  4501. ulpi_read(phy, 0x14);
  4502. ulpi_write(phy, 0x08, 0x09);
  4503. writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
  4504. while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
  4505. if (readl(USB_PORTSC) & PORTSC_PHCD)
  4506. break;
  4507. udelay(1);
  4508. cnt++;
  4509. }
  4510. if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
  4511. dev_err(phy->dev, "Unable to suspend PHY\n");
  4512. clk_disable_unprepare(motg->pclk);
  4513. clk_disable_unprepare(motg->core_clk);
  4514. if (!IS_ERR(motg->xo_clk)) {
  4515. clk_disable_unprepare(motg->xo_clk);
  4516. clk_put(motg->xo_clk);
  4517. } else {
  4518. msm_xo_put(motg->xo_handle);
  4519. }
  4520. if (!IS_ERR(motg->sleep_clk))
  4521. clk_disable_unprepare(motg->sleep_clk);
  4522. msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
  4523. msm_hsusb_ldo_init(motg, 0);
  4524. regulator_disable(hsusb_vdd);
  4525. regulator_set_voltage(hsusb_vdd,
  4526. vdd_val[motg->vdd_type][VDD_NONE],
  4527. vdd_val[motg->vdd_type][VDD_MAX]);
  4528. iounmap(motg->regs);
  4529. pm_runtime_set_suspended(&pdev->dev);
  4530. clk_put(motg->pclk);
  4531. if (!IS_ERR(motg->clk))
  4532. clk_put(motg->clk);
  4533. clk_put(motg->core_clk);
  4534. if (motg->bus_perf_client) {
  4535. msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
  4536. msm_bus_scale_unregister_client(motg->bus_perf_client);
  4537. }
  4538. return 0;
  4539. }
  4540. #ifdef CONFIG_PM_RUNTIME
  4541. static int msm_otg_runtime_idle(struct device *dev)
  4542. {
  4543. struct msm_otg *motg = dev_get_drvdata(dev);
  4544. struct usb_phy *phy = &motg->phy;
  4545. dev_dbg(dev, "OTG runtime idle\n");
  4546. if (phy->state == OTG_STATE_UNDEFINED)
  4547. return -EAGAIN;
  4548. if (motg->ext_chg_active == DEFAULT) {
  4549. dev_dbg(dev, "Deferring LPM\n");
  4550. /*
  4551. * Charger detection may happen in user space.
  4552. * Delay entering LPM by 3 sec. Otherwise we
  4553. * have to exit LPM when user space begins
  4554. * charger detection.
  4555. *
  4556. * This timer will be canceled when user space
  4557. * votes against LPM by incrementing PM usage
  4558. * counter. We enter low power mode when
  4559. * PM usage counter is decremented.
  4560. */
  4561. pm_schedule_suspend(dev, 3000);
  4562. return -EAGAIN;
  4563. }
  4564. return 0;
  4565. }
  4566. static int msm_otg_runtime_suspend(struct device *dev)
  4567. {
  4568. struct msm_otg *motg = dev_get_drvdata(dev);
  4569. dev_dbg(dev, "OTG runtime suspend\n");
  4570. return msm_otg_suspend(motg);
  4571. }
  4572. static int msm_otg_runtime_resume(struct device *dev)
  4573. {
  4574. struct msm_otg *motg = dev_get_drvdata(dev);
  4575. dev_dbg(dev, "OTG runtime resume\n");
  4576. pm_runtime_get_noresume(dev);
  4577. motg->pm_done = 0;
  4578. return msm_otg_resume(motg);
  4579. }
  4580. #endif
  4581. #ifdef CONFIG_PM_SLEEP
  4582. static int msm_otg_pm_suspend(struct device *dev)
  4583. {
  4584. int ret = 0;
  4585. struct msm_otg *motg = dev_get_drvdata(dev);
  4586. dev_dbg(dev, "OTG PM suspend\n");
  4587. atomic_set(&motg->pm_suspended, 1);
  4588. ret = msm_otg_suspend(motg);
  4589. if (ret)
  4590. atomic_set(&motg->pm_suspended, 0);
  4591. return ret;
  4592. }
  4593. static int msm_otg_pm_resume(struct device *dev)
  4594. {
  4595. int ret = 0;
  4596. struct msm_otg *motg = dev_get_drvdata(dev);
  4597. dev_dbg(dev, "OTG PM resume\n");
  4598. motg->pm_done = 0;
  4599. if (!motg->host_bus_suspend)
  4600. atomic_set(&motg->pm_suspended, 0);
  4601. if (motg->async_int || motg->sm_work_pending) {
  4602. pm_runtime_get_noresume(dev);
  4603. ret = msm_otg_resume(motg);
  4604. /* Update runtime PM status */
  4605. pm_runtime_disable(dev);
  4606. pm_runtime_set_active(dev);
  4607. pm_runtime_enable(dev);
  4608. /*
  4609. * Defer any host mode disconnect events until
  4610. * all devices are RESUMED
  4611. *
  4612. */
  4613. if (motg->sm_work_pending && !motg->host_bus_suspend) {
  4614. motg->sm_work_pending = false;
  4615. queue_work(system_nrt_wq, &motg->sm_work);
  4616. }
  4617. }
  4618. return ret;
  4619. }
  4620. #endif
  4621. #ifdef CONFIG_PM
  4622. static const struct dev_pm_ops msm_otg_dev_pm_ops = {
  4623. SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
  4624. SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
  4625. msm_otg_runtime_idle)
  4626. };
  4627. #endif
  4628. static struct of_device_id msm_otg_dt_match[] = {
  4629. { .compatible = "qcom,hsusb-otg",
  4630. },
  4631. {}
  4632. };
  4633. static struct platform_driver msm_otg_driver = {
  4634. .remove = __devexit_p(msm_otg_remove),
  4635. .driver = {
  4636. .name = DRIVER_NAME,
  4637. .owner = THIS_MODULE,
  4638. #ifdef CONFIG_PM
  4639. .pm = &msm_otg_dev_pm_ops,
  4640. #endif
  4641. .of_match_table = msm_otg_dt_match,
  4642. },
  4643. };
  4644. static int __init msm_otg_init(void)
  4645. {
  4646. return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
  4647. }
  4648. static void __exit msm_otg_exit(void)
  4649. {
  4650. platform_driver_unregister(&msm_otg_driver);
  4651. }
  4652. module_init(msm_otg_init);
  4653. module_exit(msm_otg_exit);
  4654. MODULE_LICENSE("GPL v2");
  4655. MODULE_DESCRIPTION("MSM USB transceiver driver");