leds-qpnp.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647
  1. /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/leds.h>
  17. #include <linux/err.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_device.h>
  21. #include <linux/spmi.h>
  22. #include <linux/qpnp/pwm.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/delay.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/delay.h>
  27. #define WLED_MOD_EN_REG(base, n) (base + 0x60 + n*0x10)
  28. #define WLED_IDAC_DLY_REG(base, n) (WLED_MOD_EN_REG(base, n) + 0x01)
  29. #define WLED_FULL_SCALE_REG(base, n) (WLED_IDAC_DLY_REG(base, n) + 0x01)
  30. #define WLED_MOD_SRC_SEL_REG(base, n) (WLED_FULL_SCALE_REG(base, n) + 0x01)
  31. /* wled control registers */
  32. #define WLED_BRIGHTNESS_CNTL_LSB(base, n) (base + 0x40 + 2*n)
  33. #define WLED_BRIGHTNESS_CNTL_MSB(base, n) (base + 0x41 + 2*n)
  34. #define WLED_MOD_CTRL_REG(base) (base + 0x46)
  35. #define WLED_SYNC_REG(base) (base + 0x47)
  36. #define WLED_FDBCK_CTRL_REG(base) (base + 0x48)
  37. #define WLED_SWITCHING_FREQ_REG(base) (base + 0x4C)
  38. #define WLED_OVP_CFG_REG(base) (base + 0x4D)
  39. #define WLED_BOOST_LIMIT_REG(base) (base + 0x4E)
  40. #define WLED_CURR_SINK_REG(base) (base + 0x4F)
  41. #define WLED_HIGH_POLE_CAP_REG(base) (base + 0x58)
  42. #define WLED_CURR_SINK_MASK 0xE0
  43. #define WLED_CURR_SINK_SHFT 0x05
  44. #define WLED_DISABLE_ALL_SINKS 0x00
  45. #define WLED_SWITCH_FREQ_MASK 0x0F
  46. #define WLED_OVP_VAL_MASK 0x03
  47. #define WLED_OVP_VAL_BIT_SHFT 0x00
  48. #define WLED_BOOST_LIMIT_MASK 0x07
  49. #define WLED_BOOST_LIMIT_BIT_SHFT 0x00
  50. #define WLED_BOOST_ON 0x80
  51. #define WLED_BOOST_OFF 0x00
  52. #define WLED_EN_MASK 0x80
  53. #define WLED_NO_MASK 0x00
  54. #define WLED_CP_SELECT_MAX 0x03
  55. #define WLED_CP_SELECT_MASK 0x02
  56. #define WLED_USE_EXT_GEN_MOD_SRC 0x01
  57. #define WLED_CTL_DLY_STEP 200
  58. #define WLED_CTL_DLY_MAX 1400
  59. #define WLED_MAX_CURR 25
  60. #define WLED_NO_CURRENT 0x00
  61. #define WLED_OVP_DELAY 1000
  62. #define WLED_MSB_MASK 0x0F
  63. #define WLED_MAX_CURR_MASK 0x1F
  64. #define WLED_OP_FDBCK_MASK 0x07
  65. #define WLED_OP_FDBCK_BIT_SHFT 0x00
  66. #define WLED_OP_FDBCK_DEFAULT 0x00
  67. #define WLED_MAX_LEVEL LED_FULL
  68. #define WLED_8_BIT_MASK 0xFF
  69. #define WLED_4_BIT_MASK 0x0F
  70. #define WLED_8_BIT_SHFT 0x08
  71. #define WLED_MAX_DUTY_CYCLE 0xFFF
  72. #define WLED_SYNC_VAL 0x07
  73. #define WLED_SYNC_RESET_VAL 0x00
  74. #define PMIC_VER_8026 0x04
  75. #define PMIC_VERSION_REG 0x0105
  76. #define WLED_DEFAULT_STRINGS 0x01
  77. #define WLED_DEFAULT_OVP_VAL 0x02
  78. #define WLED_BOOST_LIM_DEFAULT 0x03
  79. #define WLED_CP_SEL_DEFAULT 0x00
  80. #define WLED_CTRL_DLY_DEFAULT 0x00
  81. #define WLED_SWITCH_FREQ_DEFAULT 0x0B
  82. #define FLASH_SAFETY_TIMER(base) (base + 0x40)
  83. #define FLASH_MAX_CURR(base) (base + 0x41)
  84. #define FLASH_LED_0_CURR(base) (base + 0x42)
  85. #define FLASH_LED_1_CURR(base) (base + 0x43)
  86. #define FLASH_CLAMP_CURR(base) (base + 0x44)
  87. #define FLASH_LED_TMR_CTRL(base) (base + 0x48)
  88. #define FLASH_HEADROOM(base) (base + 0x4A)
  89. #define FLASH_STARTUP_DELAY(base) (base + 0x4B)
  90. #define FLASH_MASK_ENABLE(base) (base + 0x4C)
  91. #define FLASH_VREG_OK_FORCE(base) (base + 0x4F)
  92. #define FLASH_ENABLE_CONTROL(base) (base + 0x46)
  93. #define FLASH_LED_STROBE_CTRL(base) (base + 0x47)
  94. #define FLASH_LED_UNLOCK_SECURE(base) (base + 0xD0)
  95. #define FLASH_LED_TORCH(base) (base + 0xE4)
  96. #define FLASH_FAULT_DETECT(base) (base + 0x51)
  97. #define FLASH_PERIPHERAL_SUBTYPE(base) (base + 0x05)
  98. #define FLASH_CURRENT_RAMP(base) (base + 0x54)
  99. #define FLASH_MAX_LEVEL 0x4F
  100. #define TORCH_MAX_LEVEL 0x0F
  101. #define FLASH_NO_MASK 0x00
  102. #define FLASH_MASK_1 0x20
  103. #define FLASH_MASK_REG_MASK 0xE0
  104. #define FLASH_HEADROOM_MASK 0x03
  105. #define FLASH_SAFETY_TIMER_MASK 0x7F
  106. #define FLASH_CURRENT_MASK 0xFF
  107. #define FLASH_MAX_CURRENT_MASK 0x7F
  108. #define FLASH_TMR_MASK 0x03
  109. #define FLASH_TMR_WATCHDOG 0x03
  110. #define FLASH_TMR_SAFETY 0x00
  111. #define FLASH_FAULT_DETECT_MASK 0X80
  112. #define FLASH_HW_VREG_OK 0x40
  113. #define FLASH_VREG_MASK 0xC0
  114. #define FLASH_STARTUP_DLY_MASK 0x02
  115. #define FLASH_CURRENT_RAMP_MASK 0xBF
  116. #define FLASH_ENABLE_ALL 0xE0
  117. #define FLASH_ENABLE_MODULE 0x80
  118. #define FLASH_ENABLE_MODULE_MASK 0x80
  119. #define FLASH_DISABLE_ALL 0x00
  120. #define FLASH_ENABLE_MASK 0xE0
  121. #define FLASH_ENABLE_LED_0 0xC0
  122. #define FLASH_ENABLE_LED_1 0xA0
  123. #define FLASH_INIT_MASK 0xE0
  124. #define FLASH_SELFCHECK_ENABLE 0x80
  125. #define FLASH_WATCHDOG_MASK 0x1F
  126. #define FLASH_RAMP_STEP_27US 0xBF
  127. #define FLASH_HW_SW_STROBE_SEL_MASK 0x04
  128. #define FLASH_STROBE_MASK 0xC7
  129. #define FLASH_LED_0_OUTPUT 0x80
  130. #define FLASH_LED_1_OUTPUT 0x40
  131. #define FLASH_TORCH_OUTPUT 0xC0
  132. #define FLASH_CURRENT_PRGM_MIN 1
  133. #define FLASH_CURRENT_PRGM_SHIFT 1
  134. #define FLASH_CURRENT_MAX 0x4F
  135. #define FLASH_CURRENT_TORCH 0x07
  136. #define FLASH_DURATION_200ms 0x13
  137. #define TORCH_DURATION_12s 0x0A
  138. #define FLASH_CLAMP_200mA 0x0F
  139. #define FLASH_TORCH_MASK 0x03
  140. #define FLASH_LED_TORCH_ENABLE 0x00
  141. #define FLASH_LED_TORCH_DISABLE 0x03
  142. #define FLASH_UNLOCK_SECURE 0xA5
  143. #define FLASH_SECURE_MASK 0xFF
  144. #define FLASH_SUBTYPE_DUAL 0x01
  145. #define FLASH_SUBTYPE_SINGLE 0x02
  146. #define FLASH_RAMP_UP_DELAY_US 1000
  147. #define FLASH_RAMP_DN_DELAY_US 2160
  148. #define LED_TRIGGER_DEFAULT "none"
  149. #define RGB_LED_SRC_SEL(base) (base + 0x45)
  150. #define RGB_LED_EN_CTL(base) (base + 0x46)
  151. #define RGB_LED_ATC_CTL(base) (base + 0x47)
  152. #define RGB_MAX_LEVEL LED_FULL
  153. #define RGB_MAX_LEVEL_N2_MENU 120
  154. #define RGB_MAX_LEVEL_N2_BACK 125
  155. #define RGB_MAX_LEVEL_N1 110
  156. #define RGB_MAX_LEVEL_V1 75
  157. #define RGB_LED_ENABLE_RED 0x80
  158. #define RGB_LED_ENABLE_GREEN 0x40
  159. #define RGB_LED_ENABLE_BLUE 0x20
  160. #define RGB_LED_SOURCE_VPH_PWR 0x01
  161. #define RGB_LED_ENABLE_MASK 0xE0
  162. #define RGB_LED_SRC_MASK 0x03
  163. #define QPNP_LED_PWM_FLAGS (PM_PWM_LUT_LOOP | PM_PWM_LUT_RAMP_UP)
  164. #define QPNP_LUT_RAMP_STEP_DEFAULT 255
  165. #define PWM_LUT_MAX_SIZE 63
  166. #define PWM_GPLED_LUT_MAX_SIZE 31
  167. #define RGB_LED_DISABLE 0x00
  168. #define MPP_MAX_LEVEL LED_FULL
  169. #define LED_MPP_MODE_CTRL(base) (base + 0x40)
  170. #define LED_MPP_VIN_CTRL(base) (base + 0x41)
  171. #define LED_MPP_EN_CTRL(base) (base + 0x46)
  172. #define LED_MPP_SINK_CTRL(base) (base + 0x4C)
  173. #define LED_MPP_CURRENT_MIN 5
  174. #define LED_MPP_CURRENT_MAX 40
  175. #define LED_MPP_VIN_CTRL_DEFAULT 0
  176. #define LED_MPP_CURRENT_PER_SETTING 5
  177. #define LED_MPP_SOURCE_SEL_DEFAULT LED_MPP_MODE_ENABLE
  178. #define LED_MPP_SINK_MASK 0x07
  179. #define LED_MPP_MODE_MASK 0x7F
  180. #define LED_MPP_VIN_MASK 0x03
  181. #define LED_MPP_EN_MASK 0x80
  182. #define LED_MPP_SRC_MASK 0x0F
  183. #define LED_MPP_MODE_CTRL_MASK 0x70
  184. #define LED_MPP_MODE_SINK (0x06 << 4)
  185. #define LED_MPP_MODE_ENABLE 0x01
  186. #define LED_MPP_MODE_OUTPUT 0x10
  187. #define LED_MPP_MODE_DISABLE 0x00
  188. #define LED_MPP_EN_ENABLE 0x80
  189. #define LED_MPP_EN_DISABLE 0x00
  190. #define LOW_POWERMODE_DIVIDER 9
  191. #define MPP_SOURCE_DTEST1 0x08
  192. #define KPDBL_MAX_LEVEL LED_FULL
  193. #define KPDBL_ROW_SRC_SEL(base) (base + 0x40)
  194. #define KPDBL_ENABLE(base) (base + 0x46)
  195. #define KPDBL_ROW_SRC(base) (base + 0xE5)
  196. #define KPDBL_ROW_SRC_SEL_VAL_MASK 0x0F
  197. #define KPDBL_ROW_SCAN_EN_MASK 0x80
  198. #define KPDBL_ROW_SCAN_VAL_MASK 0x0F
  199. #define KPDBL_ROW_SCAN_EN_SHIFT 7
  200. #define KPDBL_MODULE_EN 0x80
  201. #define KPDBL_MODULE_DIS 0x00
  202. #define KPDBL_MODULE_EN_MASK 0x80
  203. #define NUM_KPDBL_LEDS 4
  204. #define KPDBL_MASTER_BIT_INDEX 0
  205. #if !defined(CONFIG_SEC_VIENNA_PROJECT) && !defined(CONFIG_SEC_V2_PROJECT) && !defined(CONFIG_SEC_LT03_PROJECT) && !defined(CONFIG_SEC_MONDRIAN_PROJECT) && !defined(CONFIG_SEC_K_PROJECT) && !defined(CONFIG_SEC_N2_PROJECT) && !defined(CONFIG_SEC_PICASSO_PROJECT) && !defined(CONFIG_SEC_KACTIVE_PROJECT) && !defined(CONFIG_MACH_KLIMT_LTE_DCM)
  206. #define SAMSUNG_LED_PATTERN 1
  207. #endif
  208. #define SAMSUNG_TKEY_LED_BRIGHTNESS 90
  209. #if defined(CONFIG_SEC_AFYON_PROJECT) || defined(CONFIG_SEC_ATLANTIC_PROJECT) || defined( CONFIG_SEC_VASTA_PROJECT) || defined(CONFIG_MACH_MEGA2LTE_KTT)
  210. #define SAMSUNG_USE_EXTERNAL_CHARGER
  211. #endif
  212. /**
  213. * enum qpnp_leds - QPNP supported led ids
  214. * @QPNP_ID_WLED - White led backlight
  215. */
  216. enum qpnp_leds {
  217. QPNP_ID_WLED = 0,
  218. QPNP_ID_FLASH1_LED0,
  219. QPNP_ID_FLASH1_LED1,
  220. QPNP_ID_RGB_RED,
  221. QPNP_ID_RGB_GREEN,
  222. QPNP_ID_RGB_BLUE,
  223. QPNP_ID_LED_MPP,
  224. QPNP_ID_KPDBL,
  225. QPNP_ID_MAX,
  226. };
  227. /* current boost limit */
  228. enum wled_current_boost_limit {
  229. WLED_CURR_LIMIT_105mA,
  230. WLED_CURR_LIMIT_385mA,
  231. WLED_CURR_LIMIT_525mA,
  232. WLED_CURR_LIMIT_805mA,
  233. WLED_CURR_LIMIT_980mA,
  234. WLED_CURR_LIMIT_1260mA,
  235. WLED_CURR_LIMIT_1400mA,
  236. WLED_CURR_LIMIT_1680mA,
  237. };
  238. /* over voltage protection threshold */
  239. enum wled_ovp_threshold {
  240. WLED_OVP_35V,
  241. WLED_OVP_32V,
  242. WLED_OVP_29V,
  243. WLED_OVP_27V,
  244. };
  245. enum flash_headroom {
  246. HEADROOM_250mV = 0,
  247. HEADROOM_300mV,
  248. HEADROOM_400mV,
  249. HEADROOM_500mV,
  250. };
  251. enum flash_startup_dly {
  252. DELAY_10us = 0,
  253. DELAY_32us,
  254. DELAY_64us,
  255. DELAY_128us,
  256. };
  257. enum led_mode {
  258. PWM_MODE = 0,
  259. LPG_MODE,
  260. MANUAL_MODE,
  261. };
  262. #ifdef SAMSUNG_LED_PATTERN
  263. enum rgb_led_patternRGB {
  264. LED_CHARGING_PAT = 1,
  265. LED_CHARGING_ERROR_PAT,
  266. LED_MISSED_CALL_PAT,
  267. LED_LOW_BATTERY_PAT,
  268. LED_FULL_BATTERY_PAT,
  269. LED_POWERING_ON_PAT,
  270. LED_PATTERN_MAX,
  271. };
  272. #endif
  273. enum RGB_LEDS{
  274. RGB_RED,
  275. RGB_GREEN,
  276. RGB_BLUE,
  277. RGB_MAX
  278. };
  279. static u8 wled_debug_regs[] = {
  280. /* common registers */
  281. 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4d, 0x4e, 0x4f,
  282. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  283. /* LED1 */
  284. 0x60, 0x61, 0x62, 0x63, 0x66,
  285. /* LED2 */
  286. 0x70, 0x71, 0x72, 0x73, 0x76,
  287. /* LED3 */
  288. 0x80, 0x81, 0x82, 0x83, 0x86,
  289. };
  290. static u8 flash_debug_regs[] = {
  291. 0x40, 0x41, 0x42, 0x43, 0x44, 0x48, 0x49, 0x4b, 0x4c,
  292. 0x4f, 0x46, 0x47,
  293. };
  294. static u8 rgb_pwm_debug_regs[] = {
  295. 0x45, 0x46, 0x47,
  296. };
  297. static u8 mpp_debug_regs[] = {
  298. 0x40, 0x41, 0x42, 0x45, 0x46, 0x4c,
  299. };
  300. static u8 kpdbl_debug_regs[] = {
  301. 0x40, 0x46, 0xb1, 0xb3, 0xb4, 0xe5,
  302. };
  303. /**
  304. * pwm_config_data - pwm configuration data
  305. * @lut_params - lut parameters to be used by pwm driver
  306. * @pwm_device - pwm device
  307. * @pwm_channel - pwm channel to be configured for led
  308. * @pwm_period_us - period for pwm, in us
  309. * @mode - mode the led operates in
  310. * @old_duty_pcts - storage for duty pcts that may need to be reused
  311. * @default_mode - default mode of LED as set in device tree
  312. * @use_blink - use blink sysfs entry
  313. * @blinking - device is currently blinking w/LPG mode
  314. */
  315. struct pwm_config_data {
  316. struct lut_params lut_params;
  317. struct pwm_device *pwm_dev;
  318. int pwm_channel;
  319. u32 pwm_period_us;
  320. struct pwm_duty_cycles *duty_cycles;
  321. int *old_duty_pcts;
  322. u8 mode;
  323. u8 default_mode;
  324. bool use_blink;
  325. bool blinking;
  326. };
  327. /**
  328. * wled_config_data - wled configuration data
  329. * @num_strings - number of wled strings supported
  330. * @ovp_val - over voltage protection threshold
  331. * @boost_curr_lim - boot current limit
  332. * @cp_select - high pole capacitance
  333. * @ctrl_delay_us - delay in activation of led
  334. * @dig_mod_gen_en - digital module generator
  335. * @cs_out_en - current sink output enable
  336. * @op_fdbck - selection of output as feedback for the boost
  337. */
  338. struct wled_config_data {
  339. u8 num_strings;
  340. u8 ovp_val;
  341. u8 boost_curr_lim;
  342. u8 cp_select;
  343. u8 ctrl_delay_us;
  344. u8 switch_freq;
  345. u8 op_fdbck;
  346. u8 pmic_version;
  347. bool dig_mod_gen_en;
  348. bool cs_out_en;
  349. };
  350. /**
  351. * mpp_config_data - mpp configuration data
  352. * @pwm_cfg - device pwm configuration
  353. * @current_setting - current setting, 5ma-40ma in 5ma increments
  354. * @source_sel - source selection
  355. * @mode_ctrl - mode control
  356. * @vin_ctrl - input control
  357. * @min_brightness - minimum brightness supported
  358. * @pwm_mode - pwm mode in use
  359. */
  360. struct mpp_config_data {
  361. struct pwm_config_data *pwm_cfg;
  362. u8 current_setting;
  363. u8 source_sel;
  364. u8 mode_ctrl;
  365. u8 vin_ctrl;
  366. u8 min_brightness;
  367. u8 pwm_mode;
  368. };
  369. /**
  370. * flash_config_data - flash configuration data
  371. * @current_prgm - current to be programmed, scaled by max level
  372. * @clamp_curr - clamp current to use
  373. * @headroom - headroom value to use
  374. * @duration - duration of the flash
  375. * @enable_module - enable address for particular flash
  376. * @trigger_flash - trigger flash
  377. * @startup_dly - startup delay for flash
  378. * @strobe_type - select between sw and hw strobe
  379. * @peripheral_subtype - module peripheral subtype
  380. * @current_addr - address to write for current
  381. * @second_addr - address of secondary flash to be written
  382. * @safety_timer - enable safety timer or watchdog timer
  383. * @torch_enable - enable flash LED torch mode
  384. * @flash_reg_get - flash regulator attached or not
  385. * @flash_wa_reg_get - workaround regulator attached or not
  386. * @flash_on - flash status, on or off
  387. * @torch_on - torch status, on or off
  388. * @flash_boost_reg - boost regulator for flash
  389. * @torch_boost_reg - boost regulator for torch
  390. * @flash_wa_reg - flash regulator for wa
  391. */
  392. struct flash_config_data {
  393. u8 current_prgm;
  394. u8 clamp_curr;
  395. u8 headroom;
  396. u8 duration;
  397. u8 enable_module;
  398. u8 trigger_flash;
  399. u8 startup_dly;
  400. u8 strobe_type;
  401. u8 peripheral_subtype;
  402. u16 current_addr;
  403. u16 second_addr;
  404. bool safety_timer;
  405. bool torch_enable;
  406. bool flash_reg_get;
  407. bool flash_wa_reg_get;
  408. bool flash_on;
  409. bool torch_on;
  410. struct regulator *flash_wa_reg;
  411. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  412. struct regulator *flash_boost_reg;
  413. struct regulator *torch_boost_reg;
  414. #endif
  415. };
  416. /**
  417. * kpdbl_config_data - kpdbl configuration data
  418. * @pwm_cfg - device pwm configuration
  419. * @mode - running mode: pwm or lut
  420. * @row_id - row id of the led
  421. * @row_src_vbst - 0 for vph_pwr and 1 for vbst
  422. * @row_src_en - enable row source
  423. * @always_on - always on row
  424. * @lut_params - lut parameters to be used by pwm driver
  425. * @duty_cycles - duty cycles for lut
  426. * @pwm_mode - pwm mode in use
  427. */
  428. struct kpdbl_config_data {
  429. struct pwm_config_data *pwm_cfg;
  430. u32 row_id;
  431. bool row_src_vbst;
  432. bool row_src_en;
  433. bool always_on;
  434. struct pwm_duty_cycles *duty_cycles;
  435. struct lut_params lut_params;
  436. u8 pwm_mode;
  437. };
  438. /**
  439. * rgb_config_data - rgb configuration data
  440. * @pwm_cfg - device pwm configuration
  441. * @enable - bits to enable led
  442. */
  443. struct rgb_config_data {
  444. struct pwm_config_data *pwm_cfg;
  445. u8 enable;
  446. };
  447. #ifdef SAMSUNG_LED_PATTERN
  448. struct patt_config {
  449. u8 id;
  450. int *duty_pcts;
  451. int *low_pow_duty_pcts;
  452. int num_duty_pcts;
  453. int ramp_step_ms;
  454. u32 pwm_period_us;
  455. int lo_pause;
  456. int hi_pause;
  457. int start_idx;
  458. };
  459. struct patt_registry {
  460. struct patt_config *patt;
  461. int len;
  462. };
  463. #endif
  464. /**
  465. * struct qpnp_led_data - internal led data structure
  466. * @led_classdev - led class device
  467. * @delayed_work - delayed work for turning off the LED
  468. * @workqueue - dedicated workqueue to handle concurrency
  469. * @work - workqueue for led
  470. * @id - led index
  471. * @base_reg - base register given in device tree
  472. * @lock - to protect the transactions
  473. * @reg - cached value of led register
  474. * @num_leds - number of leds in the module
  475. * @max_current - maximum current supported by LED
  476. * @default_on - true: default state max, false, default state 0
  477. * @turn_off_delay_ms - number of msec before turning off the LED
  478. */
  479. struct qpnp_led_data {
  480. struct led_classdev cdev;
  481. struct spmi_device *spmi_dev;
  482. struct delayed_work dwork;
  483. struct workqueue_struct *workqueue;
  484. struct work_struct work;
  485. int id;
  486. u16 base;
  487. u8 reg;
  488. u8 num_leds;
  489. struct mutex lock;
  490. struct wled_config_data *wled_cfg;
  491. struct flash_config_data *flash_cfg;
  492. struct kpdbl_config_data *kpdbl_cfg;
  493. struct rgb_config_data *rgb_cfg;
  494. struct mpp_config_data *mpp_cfg;
  495. int max_current;
  496. bool default_on;
  497. bool in_order_command_processing;
  498. int turn_off_delay_ms;
  499. };
  500. static DEFINE_MUTEX(flash_lock);
  501. static struct pwm_device *kpdbl_master;
  502. static u32 kpdbl_master_period_us;
  503. DECLARE_BITMAP(kpdbl_leds_in_use, NUM_KPDBL_LEDS);
  504. static bool is_kpdbl_master_turn_on;
  505. static struct device *led_dev;
  506. #ifdef SAMSUNG_LED_PATTERN
  507. #define RGB_LED_MAX_BRIGHTNESS 255
  508. #define CURRENT_DIVIDER 12
  509. int low_powermode;
  510. int on_patt;
  511. struct mutex leds_mutex_lock;
  512. struct patt_config blue[] = {
  513. {
  514. .id = QPNP_ID_RGB_BLUE,
  515. .duty_pcts = (int []){100, 100},
  516. .num_duty_pcts = 2,
  517. .ramp_step_ms = 0,
  518. .pwm_period_us = 1000,
  519. .lo_pause = 0,
  520. .hi_pause = 0,
  521. },
  522. };
  523. struct patt_config green[] = {
  524. {
  525. .id = QPNP_ID_RGB_GREEN,
  526. .duty_pcts = (int []){100, 100},
  527. .num_duty_pcts = 2,
  528. .ramp_step_ms = 0,
  529. .pwm_period_us = 1000,
  530. .lo_pause = 0,
  531. .hi_pause = 0,
  532. },
  533. };
  534. struct patt_config red[] = {
  535. {
  536. .id = QPNP_ID_RGB_RED,
  537. .duty_pcts = (int []){100, 100},
  538. .num_duty_pcts = 2,
  539. .ramp_step_ms = 0,
  540. .pwm_period_us = 1000,
  541. .lo_pause = 0,
  542. .hi_pause = 0,
  543. },
  544. };
  545. struct patt_config powering_on[] = {
  546. {
  547. .id = QPNP_ID_RGB_GREEN,
  548. .duty_pcts = (int [] ){
  549. 8, 10 , 11, 13, 15, 17, 18, 19, 20, 22, 24, 26, 28, 31, 33, 34, 37, 39,
  550. 41, 43, 44, 46, 48, 49, 51,
  551. },
  552. .num_duty_pcts = 25,
  553. .ramp_step_ms = 31,
  554. .pwm_period_us = 1000,
  555. .lo_pause = 200,
  556. .hi_pause = 200,
  557. },
  558. {
  559. .id = QPNP_ID_RGB_BLUE,
  560. .duty_pcts = (int [] ){
  561. 79, 80, 80, 81, 82, 83, 84, 85, 85, 86, 87, 88, 89, 90, 91, 92, 92, 93,
  562. 94, 95, 96, 97, 98, 99, 100,
  563. },
  564. .num_duty_pcts = 25,
  565. .ramp_step_ms = 31,
  566. .pwm_period_us = 1000,
  567. .lo_pause = 200,
  568. .hi_pause = 200,
  569. },
  570. };
  571. struct patt_config charging_error[] = {
  572. {
  573. .id = QPNP_ID_RGB_RED,
  574. .duty_pcts = (int []){0, 100},
  575. .low_pow_duty_pcts = (int[]){0, 100/LOW_POWERMODE_DIVIDER},
  576. .num_duty_pcts = 2,
  577. .ramp_step_ms = 0,
  578. .pwm_period_us = 1000,
  579. .lo_pause = 500,
  580. .hi_pause = 500,
  581. },
  582. };
  583. struct patt_config battery_full[] = {
  584. {
  585. .id = QPNP_ID_RGB_GREEN,
  586. .duty_pcts = (int []){100, 100},
  587. .low_pow_duty_pcts = (int []){100/LOW_POWERMODE_DIVIDER,
  588. 100/LOW_POWERMODE_DIVIDER},
  589. .num_duty_pcts = 2,
  590. .ramp_step_ms = 0,
  591. .pwm_period_us = 1000,
  592. .lo_pause = 0,
  593. .hi_pause = 0,
  594. },
  595. };
  596. struct patt_config low_battery[] = {
  597. {
  598. .id = QPNP_ID_RGB_RED,
  599. .duty_pcts = (int []){0, 100},
  600. .low_pow_duty_pcts = (int []){0, 100/LOW_POWERMODE_DIVIDER},
  601. .num_duty_pcts = 2,
  602. .ramp_step_ms = 0,
  603. .pwm_period_us = 1000,
  604. .lo_pause = 5000,
  605. .hi_pause = 500,
  606. },
  607. };
  608. struct patt_config missed_call_noti[] = {
  609. {
  610. .id = QPNP_ID_RGB_BLUE,
  611. .duty_pcts = (int[]){0, 100},
  612. .low_pow_duty_pcts = (int []){0, 100/LOW_POWERMODE_DIVIDER},
  613. .num_duty_pcts = 2,
  614. .ramp_step_ms = 0,
  615. .pwm_period_us = 1000,
  616. .lo_pause = 5000,
  617. .hi_pause = 500,
  618. },
  619. };
  620. struct patt_config charging[] = {
  621. {
  622. .id = QPNP_ID_RGB_RED,
  623. .duty_pcts = (int []){100, 100},
  624. .low_pow_duty_pcts = (int []){100/LOW_POWERMODE_DIVIDER,
  625. 100/LOW_POWERMODE_DIVIDER},
  626. .num_duty_pcts = 2,
  627. .ramp_step_ms = 0,
  628. .pwm_period_us = 1000,
  629. .lo_pause = 0,
  630. .hi_pause = 0,
  631. },
  632. };
  633. struct patt_config blink[] = {
  634. {
  635. .id = QPNP_ID_RGB_RED,
  636. .duty_pcts = (int []){0, 100},
  637. .num_duty_pcts = 2,
  638. .ramp_step_ms = 0,
  639. .pwm_period_us = 1000,
  640. .lo_pause = 0,
  641. .hi_pause = 0,
  642. },
  643. {
  644. .id = QPNP_ID_RGB_GREEN,
  645. .duty_pcts = (int []){0,100},
  646. .num_duty_pcts = 2,
  647. .ramp_step_ms = 0,
  648. .pwm_period_us = 1000,
  649. .lo_pause = 0,
  650. .hi_pause = 0,
  651. },
  652. {
  653. .id = QPNP_ID_RGB_BLUE,
  654. .duty_pcts = (int []){0, 100},
  655. .num_duty_pcts = 2,
  656. .ramp_step_ms = 0,
  657. .pwm_period_us = 1000,
  658. .lo_pause = 0,
  659. .hi_pause = 0,
  660. }
  661. };
  662. static struct patt_registry led_patterns[] = {
  663. {
  664. charging,
  665. ARRAY_SIZE(charging),
  666. },
  667. {
  668. charging_error,
  669. ARRAY_SIZE(charging_error),
  670. },
  671. {
  672. missed_call_noti,
  673. ARRAY_SIZE(missed_call_noti),
  674. },
  675. {
  676. low_battery,
  677. ARRAY_SIZE(low_battery),
  678. },
  679. {
  680. battery_full,
  681. ARRAY_SIZE(battery_full),
  682. },
  683. {
  684. powering_on,
  685. ARRAY_SIZE(powering_on),
  686. },
  687. };
  688. static struct patt_registry led_rgb[] = {
  689. {
  690. red,
  691. ARRAY_SIZE(red),
  692. },
  693. {
  694. green,
  695. ARRAY_SIZE(green),
  696. },
  697. {
  698. blue,
  699. ARRAY_SIZE(blue),
  700. },
  701. };
  702. static struct patt_registry led_blink_patt[] = {
  703. {
  704. blink,
  705. ARRAY_SIZE(blink),
  706. },
  707. };
  708. #endif
  709. #ifdef SAMSUNG_USE_EXTERNAL_CHARGER
  710. static int qpnp_led_masked_write_new(struct qpnp_led_data *led, u16 addr, u8 mask, u8 val)
  711. {
  712. int rc;
  713. u8 reg;
  714. rc = spmi_ext_register_readl(led->spmi_dev->ctrl, 0,
  715. addr, &reg, 1);
  716. if (rc) {
  717. dev_err(&led->spmi_dev->dev,
  718. "Unable to read from addr=%x, rc(%d)\n", addr, rc);
  719. }
  720. reg &= ~mask;
  721. reg |= val;
  722. rc = spmi_ext_register_writel(led->spmi_dev->ctrl, 0,
  723. addr, &reg, 1);
  724. if (rc)
  725. dev_err(&led->spmi_dev->dev,
  726. "Unable to write to addr=%x, rc(%d)\n", addr, rc);
  727. return rc;
  728. }
  729. #endif
  730. static int qpnp_led_masked_write(struct qpnp_led_data *led, u16 addr, u8 mask, u8 val)
  731. {
  732. int rc;
  733. u8 reg;
  734. rc = spmi_ext_register_readl(led->spmi_dev->ctrl, led->spmi_dev->sid,
  735. addr, &reg, 1);
  736. if (rc) {
  737. dev_err(&led->spmi_dev->dev,
  738. "Unable to read from addr=%x, rc(%d)\n", addr, rc);
  739. }
  740. reg &= ~mask;
  741. reg |= val;
  742. rc = spmi_ext_register_writel(led->spmi_dev->ctrl, led->spmi_dev->sid,
  743. addr, &reg, 1);
  744. if (rc)
  745. dev_err(&led->spmi_dev->dev,
  746. "Unable to write to addr=%x, rc(%d)\n", addr, rc);
  747. return rc;
  748. }
  749. static void qpnp_dump_regs(struct qpnp_led_data *led, u8 regs[], u8 array_size)
  750. {
  751. int i;
  752. u8 val;
  753. pr_debug("===== %s LED register dump start =====\n", led->cdev.name);
  754. for (i = 0; i < array_size; i++) {
  755. spmi_ext_register_readl(led->spmi_dev->ctrl,
  756. led->spmi_dev->sid,
  757. led->base + regs[i],
  758. &val, sizeof(val));
  759. pr_debug("%s: 0x%x = 0x%x\n", led->cdev.name,
  760. led->base + regs[i], val);
  761. }
  762. pr_debug("===== %s LED register dump end =====\n", led->cdev.name);
  763. }
  764. #ifdef SAMSUNG_USE_EXTERNAL_CHARGER
  765. #define USB_SUSPEND_BIT BIT(0)
  766. static void qpnp_flash_reg_en(struct qpnp_led_data *led, bool enable)
  767. {
  768. if(enable)
  769. {
  770. qpnp_led_masked_write_new(led, 0x1347, USB_SUSPEND_BIT, USB_SUSPEND_BIT);
  771. qpnp_led_masked_write_new(led, 0x13D0, 0xFF, 0xA5);
  772. qpnp_led_masked_write_new(led, 0x13EA, 0xFF, 0x2F);
  773. qpnp_led_masked_write_new(led, 0x1546, 0x80, 0x80);
  774. }
  775. else
  776. {
  777. qpnp_led_masked_write_new(led, 0x1546, 0x80, 0x00);
  778. qpnp_led_masked_write_new(led, 0x10D0, 0xFF, 0xA5);
  779. qpnp_led_masked_write_new(led, 0x10EA, 0xFF, 0x20);
  780. usleep(2000);
  781. qpnp_led_masked_write_new(led, 0x10D0, 0xFF, 0xA5);
  782. qpnp_led_masked_write_new(led, 0x10EA, 0xFF, 0x00);
  783. qpnp_led_masked_write_new(led, 0x13D0, 0xFF, 0xA5);
  784. qpnp_led_masked_write_new(led, 0x13EA, 0xFF, 0x00);
  785. usleep(1000);
  786. qpnp_led_masked_write_new(led, 0x1347, USB_SUSPEND_BIT, 0x0);
  787. }
  788. }
  789. #endif
  790. static int qpnp_wled_sync(struct qpnp_led_data *led)
  791. {
  792. int rc;
  793. u8 val;
  794. /* sync */
  795. val = WLED_SYNC_VAL;
  796. rc = spmi_ext_register_writel(led->spmi_dev->ctrl, led->spmi_dev->sid,
  797. WLED_SYNC_REG(led->base), &val, 1);
  798. if (rc) {
  799. dev_err(&led->spmi_dev->dev,
  800. "WLED set sync reg failed(%d)\n", rc);
  801. return rc;
  802. }
  803. val = WLED_SYNC_RESET_VAL;
  804. rc = spmi_ext_register_writel(led->spmi_dev->ctrl, led->spmi_dev->sid,
  805. WLED_SYNC_REG(led->base), &val, 1);
  806. if (rc) {
  807. dev_err(&led->spmi_dev->dev,
  808. "WLED reset sync reg failed(%d)\n", rc);
  809. return rc;
  810. }
  811. return 0;
  812. }
  813. static int qpnp_wled_set(struct qpnp_led_data *led)
  814. {
  815. int rc, duty, level;
  816. u8 val, i, num_wled_strings, sink_val;
  817. num_wled_strings = led->wled_cfg->num_strings;
  818. level = led->cdev.brightness;
  819. if (level > WLED_MAX_LEVEL)
  820. level = WLED_MAX_LEVEL;
  821. if (level == 0) {
  822. for (i = 0; i < num_wled_strings; i++) {
  823. rc = qpnp_led_masked_write(led,
  824. WLED_FULL_SCALE_REG(led->base, i),
  825. WLED_MAX_CURR_MASK, WLED_NO_CURRENT);
  826. if (rc) {
  827. dev_err(&led->spmi_dev->dev,
  828. "Write max current failure (%d)\n",
  829. rc);
  830. return rc;
  831. }
  832. }
  833. rc = qpnp_wled_sync(led);
  834. if (rc) {
  835. dev_err(&led->spmi_dev->dev,
  836. "WLED sync failed(%d)\n", rc);
  837. return rc;
  838. }
  839. rc = spmi_ext_register_readl(led->spmi_dev->ctrl,
  840. led->spmi_dev->sid, WLED_CURR_SINK_REG(led->base),
  841. &sink_val, 1);
  842. if (rc) {
  843. dev_err(&led->spmi_dev->dev,
  844. "WLED read sink reg failed(%d)\n", rc);
  845. return rc;
  846. }
  847. if (led->wled_cfg->pmic_version == PMIC_VER_8026) {
  848. val = WLED_DISABLE_ALL_SINKS;
  849. rc = spmi_ext_register_writel(led->spmi_dev->ctrl,
  850. led->spmi_dev->sid,
  851. WLED_CURR_SINK_REG(led->base), &val, 1);
  852. if (rc) {
  853. dev_err(&led->spmi_dev->dev,
  854. "WLED write sink reg failed(%d)\n", rc);
  855. return rc;
  856. }
  857. usleep(WLED_OVP_DELAY);
  858. }
  859. val = WLED_BOOST_OFF;
  860. rc = spmi_ext_register_writel(led->spmi_dev->ctrl,
  861. led->spmi_dev->sid, WLED_MOD_CTRL_REG(led->base),
  862. &val, 1);
  863. if (rc) {
  864. dev_err(&led->spmi_dev->dev,
  865. "WLED write ctrl reg failed(%d)\n", rc);
  866. return rc;
  867. }
  868. for (i = 0; i < num_wled_strings; i++) {
  869. rc = qpnp_led_masked_write(led,
  870. WLED_FULL_SCALE_REG(led->base, i),
  871. WLED_MAX_CURR_MASK, (u8)led->max_current);
  872. if (rc) {
  873. dev_err(&led->spmi_dev->dev,
  874. "Write max current failure (%d)\n",
  875. rc);
  876. return rc;
  877. }
  878. }
  879. rc = qpnp_wled_sync(led);
  880. if (rc) {
  881. dev_err(&led->spmi_dev->dev,
  882. "WLED sync failed(%d)\n", rc);
  883. return rc;
  884. }
  885. rc = spmi_ext_register_writel(led->spmi_dev->ctrl,
  886. led->spmi_dev->sid, WLED_CURR_SINK_REG(led->base),
  887. &sink_val, 1);
  888. if (rc) {
  889. dev_err(&led->spmi_dev->dev,
  890. "WLED write sink reg failed(%d)\n", rc);
  891. return rc;
  892. }
  893. } else {
  894. val = WLED_BOOST_ON;
  895. rc = spmi_ext_register_writel(led->spmi_dev->ctrl,
  896. led->spmi_dev->sid, WLED_MOD_CTRL_REG(led->base),
  897. &val, 1);
  898. if (rc) {
  899. dev_err(&led->spmi_dev->dev,
  900. "WLED write ctrl reg failed(%d)\n", rc);
  901. return rc;
  902. }
  903. }
  904. duty = (WLED_MAX_DUTY_CYCLE * level) / WLED_MAX_LEVEL;
  905. /* program brightness control registers */
  906. for (i = 0; i < num_wled_strings; i++) {
  907. rc = qpnp_led_masked_write(led,
  908. WLED_BRIGHTNESS_CNTL_MSB(led->base, i), WLED_MSB_MASK,
  909. (duty >> WLED_8_BIT_SHFT) & WLED_4_BIT_MASK);
  910. if (rc) {
  911. dev_err(&led->spmi_dev->dev,
  912. "WLED set brightness MSB failed(%d)\n", rc);
  913. return rc;
  914. }
  915. val = duty & WLED_8_BIT_MASK;
  916. rc = spmi_ext_register_writel(led->spmi_dev->ctrl,
  917. led->spmi_dev->sid,
  918. WLED_BRIGHTNESS_CNTL_LSB(led->base, i), &val, 1);
  919. if (rc) {
  920. dev_err(&led->spmi_dev->dev,
  921. "WLED set brightness LSB failed(%d)\n", rc);
  922. return rc;
  923. }
  924. }
  925. rc = qpnp_wled_sync(led);
  926. if (rc) {
  927. dev_err(&led->spmi_dev->dev, "WLED sync failed(%d)\n", rc);
  928. return rc;
  929. }
  930. return 0;
  931. }
  932. static int qpnp_mpp_set(struct qpnp_led_data *led)
  933. {
  934. int rc;
  935. u8 val;
  936. int duty_us, duty_ns, period_us;
  937. if (led->cdev.brightness) {
  938. if (led->cdev.brightness < led->mpp_cfg->min_brightness) {
  939. dev_warn(&led->spmi_dev->dev,
  940. "brightness is less than supported..." \
  941. "set to minimum supported\n");
  942. led->cdev.brightness = led->mpp_cfg->min_brightness;
  943. }
  944. if (led->mpp_cfg->pwm_mode != MANUAL_MODE) {
  945. if (!led->mpp_cfg->pwm_cfg->blinking) {
  946. led->mpp_cfg->pwm_cfg->mode =
  947. led->mpp_cfg->pwm_cfg->default_mode;
  948. led->mpp_cfg->pwm_mode =
  949. led->mpp_cfg->pwm_cfg->default_mode;
  950. }
  951. }
  952. if (led->mpp_cfg->pwm_mode == PWM_MODE) {
  953. /*config pwm for brightness scaling*/
  954. period_us = led->mpp_cfg->pwm_cfg->pwm_period_us;
  955. if (period_us > INT_MAX / NSEC_PER_USEC) {
  956. duty_us = (period_us * led->cdev.brightness) /
  957. LED_FULL;
  958. rc = pwm_config_us(
  959. led->mpp_cfg->pwm_cfg->pwm_dev,
  960. duty_us,
  961. period_us);
  962. } else {
  963. duty_ns = ((period_us * NSEC_PER_USEC) /
  964. LED_FULL) * led->cdev.brightness;
  965. rc = pwm_config(
  966. led->mpp_cfg->pwm_cfg->pwm_dev,
  967. duty_ns,
  968. period_us * NSEC_PER_USEC);
  969. }
  970. if (rc < 0) {
  971. dev_err(&led->spmi_dev->dev, "Failed to " \
  972. "configure pwm for new values\n");
  973. return rc;
  974. }
  975. }
  976. if (led->mpp_cfg->pwm_mode != MANUAL_MODE)
  977. pwm_enable(led->mpp_cfg->pwm_cfg->pwm_dev);
  978. else {
  979. if (led->cdev.brightness < LED_MPP_CURRENT_MIN)
  980. led->cdev.brightness = LED_MPP_CURRENT_MIN;
  981. val = (led->cdev.brightness / LED_MPP_CURRENT_MIN) - 1;
  982. rc = qpnp_led_masked_write(led,
  983. LED_MPP_SINK_CTRL(led->base),
  984. LED_MPP_SINK_MASK, val);
  985. if (rc) {
  986. dev_err(&led->spmi_dev->dev,
  987. "Failed to write sink control reg\n");
  988. return rc;
  989. }
  990. }
  991. val = (led->mpp_cfg->source_sel & LED_MPP_SRC_MASK) |
  992. (led->mpp_cfg->mode_ctrl & LED_MPP_MODE_CTRL_MASK);
  993. rc = qpnp_led_masked_write(led,
  994. LED_MPP_MODE_CTRL(led->base), LED_MPP_MODE_MASK,
  995. val);
  996. if (rc) {
  997. dev_err(&led->spmi_dev->dev,
  998. "Failed to write led mode reg\n");
  999. return rc;
  1000. }
  1001. rc = qpnp_led_masked_write(led,
  1002. LED_MPP_EN_CTRL(led->base), LED_MPP_EN_MASK,
  1003. LED_MPP_EN_ENABLE);
  1004. if (rc) {
  1005. dev_err(&led->spmi_dev->dev,
  1006. "Failed to write led enable " \
  1007. "reg\n");
  1008. return rc;
  1009. }
  1010. } else {
  1011. if (led->mpp_cfg->pwm_mode != MANUAL_MODE) {
  1012. led->mpp_cfg->pwm_cfg->mode =
  1013. led->mpp_cfg->pwm_cfg->default_mode;
  1014. led->mpp_cfg->pwm_mode =
  1015. led->mpp_cfg->pwm_cfg->default_mode;
  1016. pwm_disable(led->mpp_cfg->pwm_cfg->pwm_dev);
  1017. }
  1018. rc = qpnp_led_masked_write(led,
  1019. LED_MPP_MODE_CTRL(led->base),
  1020. LED_MPP_MODE_MASK,
  1021. LED_MPP_MODE_DISABLE);
  1022. if (rc) {
  1023. dev_err(&led->spmi_dev->dev,
  1024. "Failed to write led mode reg\n");
  1025. return rc;
  1026. }
  1027. rc = qpnp_led_masked_write(led,
  1028. LED_MPP_EN_CTRL(led->base),
  1029. LED_MPP_EN_MASK,
  1030. LED_MPP_EN_DISABLE);
  1031. if (rc) {
  1032. dev_err(&led->spmi_dev->dev,
  1033. "Failed to write led enable reg\n");
  1034. return rc;
  1035. }
  1036. }
  1037. if (led->mpp_cfg->pwm_mode != MANUAL_MODE)
  1038. led->mpp_cfg->pwm_cfg->blinking = false;
  1039. qpnp_dump_regs(led, mpp_debug_regs, ARRAY_SIZE(mpp_debug_regs));
  1040. return 0;
  1041. }
  1042. static int qpnp_flash_regulator_operate(struct qpnp_led_data *led, bool on)
  1043. {
  1044. int rc, i;
  1045. struct qpnp_led_data *led_array;
  1046. bool regulator_on = false;
  1047. led_array = dev_get_drvdata(&led->spmi_dev->dev);
  1048. if (!led_array) {
  1049. dev_err(&led->spmi_dev->dev,
  1050. "Unable to get LED array\n");
  1051. return -EINVAL;
  1052. }
  1053. for (i = 0; i < led->num_leds; i++)
  1054. regulator_on |= led_array[i].flash_cfg->flash_on;
  1055. if (!on)
  1056. goto regulator_turn_off;
  1057. if (!regulator_on && !led->flash_cfg->flash_on) {
  1058. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1059. for (i = 0; i < led->num_leds; i++) {
  1060. if (led_array[i].flash_cfg->flash_reg_get) {
  1061. if (led_array[i].flash_cfg->flash_wa_reg_get) {
  1062. rc = regulator_enable(
  1063. led_array[i].flash_cfg->
  1064. flash_wa_reg);
  1065. if (rc) {
  1066. dev_err(&led->spmi_dev->dev,
  1067. "Flash wa regulator"
  1068. "enable failed(%d)\n",
  1069. rc);
  1070. return rc;
  1071. }
  1072. }
  1073. rc = regulator_enable(
  1074. led_array[i].flash_cfg->\
  1075. flash_boost_reg);
  1076. if (rc) {
  1077. if (led_array[i].flash_cfg->
  1078. flash_wa_reg_get)
  1079. /* Disable flash wa regulator
  1080. * when flash boost regulator
  1081. * enable fails
  1082. */
  1083. regulator_disable(
  1084. led_array[i].flash_cfg->
  1085. flash_wa_reg);
  1086. dev_err(&led->spmi_dev->dev,
  1087. "Flash boost regulator enable"
  1088. "failed(%d)\n", rc);
  1089. return rc;
  1090. }
  1091. led->flash_cfg->flash_on = true;
  1092. }
  1093. break;
  1094. }
  1095. for (i = 0; i < led->num_leds; i++) {
  1096. if (led_array[i].flash_cfg->flash_reg_get) {
  1097. rc = regulator_enable(
  1098. led_array[i].flash_cfg->flash_wa_reg);
  1099. if (rc) {
  1100. dev_err(&led->spmi_dev->dev,
  1101. "Flash_wa regulator enable failed(%d)\n",
  1102. rc);
  1103. return rc;
  1104. }
  1105. rc = regulator_enable(
  1106. led_array[i].flash_cfg->\
  1107. flash_boost_reg);
  1108. if (rc) {
  1109. dev_err(&led->spmi_dev->dev,
  1110. "Regulator enable failed(%d)\n",
  1111. rc);
  1112. return rc;
  1113. }
  1114. led->flash_cfg->flash_on = true;
  1115. }
  1116. break;
  1117. }
  1118. #endif
  1119. for (i = 0; i < led->num_leds; i++) {
  1120. #ifdef SAMSUNG_USE_EXTERNAL_CHARGER
  1121. qpnp_flash_reg_en(led, true);
  1122. #else
  1123. if (led_array[i].flash_cfg->flash_reg_get) {
  1124. rc = regulator_enable(
  1125. led_array[i].flash_cfg->\
  1126. flash_boost_reg);
  1127. if (rc) {
  1128. dev_err(&led->spmi_dev->dev,
  1129. "Regulator enable failed(%d)\n",
  1130. rc);
  1131. return rc;
  1132. }
  1133. #endif
  1134. led->flash_cfg->flash_on = true;
  1135. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1136. }
  1137. #endif
  1138. break;
  1139. }
  1140. }
  1141. return 0;
  1142. regulator_turn_off:
  1143. if (regulator_on && led->flash_cfg->flash_on) {
  1144. for (i = 0; i < led->num_leds; i++) {
  1145. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1146. if (led_array[i].flash_cfg->flash_reg_get) {
  1147. #endif
  1148. rc = qpnp_led_masked_write(led,
  1149. FLASH_ENABLE_CONTROL(led->base),
  1150. FLASH_ENABLE_MASK,
  1151. FLASH_DISABLE_ALL);
  1152. if (rc) {
  1153. dev_err(&led->spmi_dev->dev,
  1154. "Enable reg write failed(%d)\n",
  1155. rc);
  1156. }
  1157. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1158. rc = regulator_disable(led_array[i].flash_cfg->\
  1159. flash_boost_reg);
  1160. if (rc) {
  1161. dev_err(&led->spmi_dev->dev,
  1162. "Flash boost regulator disable"
  1163. "failed(%d)\n", rc);
  1164. return rc;
  1165. }
  1166. if (led_array[i].flash_cfg->flash_wa_reg_get) {
  1167. rc = regulator_disable(
  1168. led_array[i].flash_cfg->
  1169. flash_wa_reg);
  1170. if (rc) {
  1171. dev_err(&led->spmi_dev->dev,
  1172. "Flash_wa regulator"
  1173. "disable failed(%d)\n",
  1174. rc);
  1175. return rc;
  1176. }
  1177. }
  1178. #endif
  1179. #ifdef SAMSUNG_USE_EXTERNAL_CHARGER
  1180. qpnp_flash_reg_en(led,false);
  1181. #endif
  1182. led->flash_cfg->flash_on = false;
  1183. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1184. }
  1185. #endif
  1186. break;
  1187. }
  1188. }
  1189. return 0;
  1190. }
  1191. static int qpnp_torch_regulator_operate(struct qpnp_led_data *led, bool on)
  1192. {
  1193. int rc;
  1194. if (!on)
  1195. goto regulator_turn_off;
  1196. if (!led->flash_cfg->torch_on) {
  1197. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1198. rc = regulator_enable(led->flash_cfg->torch_boost_reg);
  1199. if (rc) {
  1200. dev_err(&led->spmi_dev->dev,
  1201. "Regulator enable failed(%d)\n", rc);
  1202. return rc;
  1203. }
  1204. #endif
  1205. #ifdef SAMSUNG_USE_EXTERNAL_CHARGER
  1206. qpnp_flash_reg_en(led,true);
  1207. #endif
  1208. led->flash_cfg->torch_on = true;
  1209. }
  1210. return 0;
  1211. regulator_turn_off:
  1212. if (led->flash_cfg->torch_on) {
  1213. rc = qpnp_led_masked_write(led, FLASH_ENABLE_CONTROL(led->base),
  1214. FLASH_ENABLE_MODULE_MASK, FLASH_DISABLE_ALL);
  1215. if (rc) {
  1216. dev_err(&led->spmi_dev->dev,
  1217. "Enable reg write failed(%d)\n", rc);
  1218. }
  1219. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  1220. rc = regulator_disable(led->flash_cfg->torch_boost_reg);
  1221. if (rc) {
  1222. dev_err(&led->spmi_dev->dev,
  1223. "Regulator disable failed(%d)\n", rc);
  1224. return rc;
  1225. }
  1226. #endif
  1227. #ifdef SAMSUNG_USE_EXTERNAL_CHARGER
  1228. qpnp_flash_reg_en(led,false);
  1229. #endif
  1230. led->flash_cfg->torch_on = false;
  1231. }
  1232. return 0;
  1233. }
  1234. static int qpnp_flash_set(struct qpnp_led_data *led)
  1235. {
  1236. int rc, error;
  1237. int val = led->cdev.brightness;
  1238. if (led->flash_cfg->torch_enable)
  1239. led->flash_cfg->current_prgm =
  1240. (val * TORCH_MAX_LEVEL / led->max_current);
  1241. else
  1242. led->flash_cfg->current_prgm =
  1243. (val * FLASH_MAX_LEVEL / led->max_current);
  1244. /* Set led current */
  1245. if (val > 0) {
  1246. if (led->flash_cfg->torch_enable) {
  1247. if (led->flash_cfg->peripheral_subtype ==
  1248. FLASH_SUBTYPE_DUAL) {
  1249. rc = qpnp_torch_regulator_operate(led, true);
  1250. if (rc) {
  1251. dev_err(&led->spmi_dev->dev,
  1252. "Torch regulator operate failed(%d)\n",
  1253. rc);
  1254. return rc;
  1255. }
  1256. } else if (led->flash_cfg->peripheral_subtype ==
  1257. FLASH_SUBTYPE_SINGLE) {
  1258. rc = qpnp_flash_regulator_operate(led, true);
  1259. if (rc) {
  1260. dev_err(&led->spmi_dev->dev,
  1261. "Flash regulator operate failed(%d)\n",
  1262. rc);
  1263. goto error_flash_set;
  1264. }
  1265. }
  1266. rc = qpnp_led_masked_write(led,
  1267. FLASH_LED_UNLOCK_SECURE(led->base),
  1268. FLASH_SECURE_MASK, FLASH_UNLOCK_SECURE);
  1269. if (rc) {
  1270. dev_err(&led->spmi_dev->dev,
  1271. "Secure reg write failed(%d)\n", rc);
  1272. goto error_reg_write;
  1273. }
  1274. rc = qpnp_led_masked_write(led,
  1275. FLASH_LED_TORCH(led->base),
  1276. FLASH_TORCH_MASK, FLASH_LED_TORCH_ENABLE);
  1277. if (rc) {
  1278. dev_err(&led->spmi_dev->dev,
  1279. "Torch reg write failed(%d)\n", rc);
  1280. goto error_reg_write;
  1281. }
  1282. rc = qpnp_led_masked_write(led,
  1283. led->flash_cfg->current_addr,
  1284. FLASH_CURRENT_MASK,
  1285. led->flash_cfg->current_prgm);
  1286. if (rc) {
  1287. dev_err(&led->spmi_dev->dev,
  1288. "Current reg write failed(%d)\n", rc);
  1289. goto error_reg_write;
  1290. }
  1291. rc = qpnp_led_masked_write(led,
  1292. led->flash_cfg->second_addr,
  1293. FLASH_CURRENT_MASK,
  1294. led->flash_cfg->current_prgm);
  1295. if (rc) {
  1296. dev_err(&led->spmi_dev->dev,
  1297. "2nd Current reg write failed(%d)\n",
  1298. rc);
  1299. goto error_reg_write;
  1300. }
  1301. qpnp_led_masked_write(led, FLASH_MAX_CURR(led->base),
  1302. FLASH_CURRENT_MASK,
  1303. TORCH_MAX_LEVEL);
  1304. if (rc) {
  1305. dev_err(&led->spmi_dev->dev,
  1306. "Max current reg write failed(%d)\n",
  1307. rc);
  1308. goto error_reg_write;
  1309. }
  1310. rc = qpnp_led_masked_write(led,
  1311. FLASH_ENABLE_CONTROL(led->base),
  1312. FLASH_ENABLE_MASK,
  1313. led->flash_cfg->enable_module);
  1314. if (rc) {
  1315. dev_err(&led->spmi_dev->dev,
  1316. "Enable reg write failed(%d)\n",
  1317. rc);
  1318. goto error_reg_write;
  1319. }
  1320. if (!led->flash_cfg->strobe_type)
  1321. led->flash_cfg->trigger_flash &=
  1322. ~FLASH_HW_SW_STROBE_SEL_MASK;
  1323. else
  1324. led->flash_cfg->trigger_flash |=
  1325. FLASH_HW_SW_STROBE_SEL_MASK;
  1326. rc = qpnp_led_masked_write(led,
  1327. FLASH_LED_STROBE_CTRL(led->base),
  1328. led->flash_cfg->trigger_flash,
  1329. led->flash_cfg->trigger_flash);
  1330. if (rc) {
  1331. dev_err(&led->spmi_dev->dev,
  1332. "LED %d strobe reg write failed(%d)\n",
  1333. led->id, rc);
  1334. goto error_reg_write;
  1335. }
  1336. } else {
  1337. rc = qpnp_flash_regulator_operate(led, true);
  1338. if (rc) {
  1339. dev_err(&led->spmi_dev->dev,
  1340. "Flash regulator operate failed(%d)\n",
  1341. rc);
  1342. goto error_flash_set;
  1343. }
  1344. /* Set flash safety timer */
  1345. rc = qpnp_led_masked_write(led,
  1346. FLASH_SAFETY_TIMER(led->base),
  1347. FLASH_SAFETY_TIMER_MASK,
  1348. led->flash_cfg->duration);
  1349. if (rc) {
  1350. dev_err(&led->spmi_dev->dev,
  1351. "Safety timer reg write failed(%d)\n",
  1352. rc);
  1353. goto error_flash_set;
  1354. }
  1355. /* Set max current */
  1356. rc = qpnp_led_masked_write(led,
  1357. FLASH_MAX_CURR(led->base), FLASH_CURRENT_MASK,
  1358. FLASH_MAX_LEVEL);
  1359. if (rc) {
  1360. dev_err(&led->spmi_dev->dev,
  1361. "Max current reg write failed(%d)\n",
  1362. rc);
  1363. goto error_flash_set;
  1364. }
  1365. /* Set clamp current */
  1366. rc = qpnp_led_masked_write(led,
  1367. FLASH_CLAMP_CURR(led->base),
  1368. FLASH_CURRENT_MASK,
  1369. led->flash_cfg->clamp_curr);
  1370. if (rc) {
  1371. dev_err(&led->spmi_dev->dev,
  1372. "Clamp current reg write failed(%d)\n",
  1373. rc);
  1374. goto error_flash_set;
  1375. }
  1376. rc = qpnp_led_masked_write(led,
  1377. led->flash_cfg->current_addr,
  1378. FLASH_CURRENT_MASK,
  1379. led->flash_cfg->current_prgm);
  1380. if (rc) {
  1381. dev_err(&led->spmi_dev->dev,
  1382. "Current reg write failed(%d)\n", rc);
  1383. goto error_flash_set;
  1384. }
  1385. rc = qpnp_led_masked_write(led,
  1386. FLASH_ENABLE_CONTROL(led->base),
  1387. led->flash_cfg->enable_module,
  1388. led->flash_cfg->enable_module);
  1389. if (rc) {
  1390. dev_err(&led->spmi_dev->dev,
  1391. "Enable reg write failed(%d)\n", rc);
  1392. goto error_flash_set;
  1393. }
  1394. /*
  1395. * Add 1ms delay for bharger enter stable state
  1396. */
  1397. usleep(FLASH_RAMP_UP_DELAY_US);
  1398. if (!led->flash_cfg->strobe_type)
  1399. led->flash_cfg->trigger_flash &=
  1400. ~FLASH_HW_SW_STROBE_SEL_MASK;
  1401. else
  1402. led->flash_cfg->trigger_flash |=
  1403. FLASH_HW_SW_STROBE_SEL_MASK;
  1404. rc = qpnp_led_masked_write(led,
  1405. FLASH_LED_STROBE_CTRL(led->base),
  1406. led->flash_cfg->trigger_flash,
  1407. led->flash_cfg->trigger_flash);
  1408. if (rc) {
  1409. dev_err(&led->spmi_dev->dev,
  1410. "LED %d strobe reg write failed(%d)\n",
  1411. led->id, rc);
  1412. goto error_flash_set;
  1413. }
  1414. }
  1415. } else {
  1416. rc = qpnp_led_masked_write(led,
  1417. FLASH_LED_STROBE_CTRL(led->base),
  1418. led->flash_cfg->trigger_flash,
  1419. FLASH_DISABLE_ALL);
  1420. if (rc) {
  1421. dev_err(&led->spmi_dev->dev,
  1422. "LED %d flash write failed(%d)\n", led->id, rc);
  1423. if (led->flash_cfg->torch_enable)
  1424. goto error_torch_set;
  1425. else
  1426. goto error_flash_set;
  1427. }
  1428. if (led->flash_cfg->torch_enable) {
  1429. rc = qpnp_led_masked_write(led,
  1430. FLASH_LED_UNLOCK_SECURE(led->base),
  1431. FLASH_SECURE_MASK, FLASH_UNLOCK_SECURE);
  1432. if (rc) {
  1433. dev_err(&led->spmi_dev->dev,
  1434. "Secure reg write failed(%d)\n", rc);
  1435. goto error_torch_set;
  1436. }
  1437. rc = qpnp_led_masked_write(led,
  1438. FLASH_LED_TORCH(led->base),
  1439. FLASH_TORCH_MASK,
  1440. FLASH_LED_TORCH_DISABLE);
  1441. if (rc) {
  1442. dev_err(&led->spmi_dev->dev,
  1443. "Torch reg write failed(%d)\n", rc);
  1444. goto error_torch_set;
  1445. }
  1446. if (led->flash_cfg->peripheral_subtype ==
  1447. FLASH_SUBTYPE_DUAL) {
  1448. rc = qpnp_torch_regulator_operate(led, false);
  1449. if (rc) {
  1450. dev_err(&led->spmi_dev->dev,
  1451. "Torch regulator operate failed(%d)\n",
  1452. rc);
  1453. return rc;
  1454. }
  1455. } else if (led->flash_cfg->peripheral_subtype ==
  1456. FLASH_SUBTYPE_SINGLE) {
  1457. rc = qpnp_flash_regulator_operate(led, false);
  1458. if (rc) {
  1459. dev_err(&led->spmi_dev->dev,
  1460. "Flash regulator operate failed(%d)\n",
  1461. rc);
  1462. return rc;
  1463. }
  1464. }
  1465. } else {
  1466. /*
  1467. * Disable module after ramp down complete for stable
  1468. * behavior
  1469. */
  1470. usleep(FLASH_RAMP_DN_DELAY_US);
  1471. rc = qpnp_led_masked_write(led,
  1472. FLASH_ENABLE_CONTROL(led->base),
  1473. #if defined(CONFIG_MACH_VICTORLTE_CTC) || defined(CONFIG_SEC_MEGA2_PROJECT)
  1474. led->flash_cfg->enable_module,
  1475. #else
  1476. led->flash_cfg->enable_module &
  1477. ~FLASH_ENABLE_MODULE_MASK,
  1478. #endif
  1479. FLASH_DISABLE_ALL);
  1480. if (rc) {
  1481. dev_err(&led->spmi_dev->dev,
  1482. "Enable reg write failed(%d)\n", rc);
  1483. if (led->flash_cfg->torch_enable)
  1484. goto error_torch_set;
  1485. else
  1486. goto error_flash_set;
  1487. }
  1488. rc = qpnp_flash_regulator_operate(led, false);
  1489. if (rc) {
  1490. dev_err(&led->spmi_dev->dev,
  1491. "Flash regulator operate failed(%d)\n",
  1492. rc);
  1493. return rc;
  1494. }
  1495. }
  1496. }
  1497. qpnp_dump_regs(led, flash_debug_regs, ARRAY_SIZE(flash_debug_regs));
  1498. return 0;
  1499. error_reg_write:
  1500. if (led->flash_cfg->peripheral_subtype == FLASH_SUBTYPE_SINGLE)
  1501. goto error_flash_set;
  1502. error_torch_set:
  1503. error = qpnp_torch_regulator_operate(led, false);
  1504. if (error) {
  1505. dev_err(&led->spmi_dev->dev,
  1506. "Torch regulator operate failed(%d)\n", rc);
  1507. return error;
  1508. }
  1509. return rc;
  1510. error_flash_set:
  1511. error = qpnp_flash_regulator_operate(led, false);
  1512. if (error) {
  1513. dev_err(&led->spmi_dev->dev,
  1514. "Flash regulator operate failed(%d)\n", rc);
  1515. return error;
  1516. }
  1517. return rc;
  1518. }
  1519. static int qpnp_kpdbl_set(struct qpnp_led_data *led)
  1520. {
  1521. int rc;
  1522. int duty_us, duty_ns, period_us;
  1523. if (led->cdev.brightness) {
  1524. if (!led->kpdbl_cfg->pwm_cfg->blinking)
  1525. led->kpdbl_cfg->pwm_cfg->mode =
  1526. led->kpdbl_cfg->pwm_cfg->default_mode;
  1527. if (bitmap_empty(kpdbl_leds_in_use, NUM_KPDBL_LEDS)) {
  1528. rc = qpnp_led_masked_write(led, KPDBL_ENABLE(led->base),
  1529. KPDBL_MODULE_EN_MASK, KPDBL_MODULE_EN);
  1530. if (rc) {
  1531. dev_err(&led->spmi_dev->dev,
  1532. "Enable reg write failed(%d)\n", rc);
  1533. return rc;
  1534. }
  1535. }
  1536. /* On some platforms, GPLED1 channel should always be enabled
  1537. * for the other GPLEDs 2/3/4 to glow. Before enabling GPLED
  1538. * 2/3/4, first check if GPLED1 is already enabled. If GPLED1
  1539. * channel is not enabled, then enable the GPLED1 channel but
  1540. * with a 0 brightness
  1541. */
  1542. if (!led->kpdbl_cfg->always_on &&
  1543. !test_bit(KPDBL_MASTER_BIT_INDEX, kpdbl_leds_in_use) &&
  1544. kpdbl_master) {
  1545. rc = pwm_config_us(kpdbl_master, 0,
  1546. kpdbl_master_period_us);
  1547. if (rc < 0) {
  1548. dev_err(&led->spmi_dev->dev,
  1549. "pwm config failed\n");
  1550. return rc;
  1551. }
  1552. rc = pwm_enable(kpdbl_master);
  1553. if (rc < 0) {
  1554. dev_err(&led->spmi_dev->dev,
  1555. "pwm enable failed\n");
  1556. return rc;
  1557. }
  1558. set_bit(KPDBL_MASTER_BIT_INDEX,
  1559. kpdbl_leds_in_use);
  1560. }
  1561. if (led->kpdbl_cfg->pwm_cfg->mode == PWM_MODE) {
  1562. period_us = led->kpdbl_cfg->pwm_cfg->pwm_period_us;
  1563. if (period_us > INT_MAX / NSEC_PER_USEC) {
  1564. duty_us = (period_us * led->cdev.brightness) /
  1565. KPDBL_MAX_LEVEL;
  1566. rc = pwm_config_us(
  1567. led->kpdbl_cfg->pwm_cfg->pwm_dev,
  1568. duty_us,
  1569. period_us);
  1570. } else {
  1571. duty_ns = ((period_us * NSEC_PER_USEC) /
  1572. KPDBL_MAX_LEVEL) * led->cdev.brightness;
  1573. rc = pwm_config(
  1574. led->kpdbl_cfg->pwm_cfg->pwm_dev,
  1575. duty_ns,
  1576. period_us * NSEC_PER_USEC);
  1577. }
  1578. if (rc < 0) {
  1579. dev_err(&led->spmi_dev->dev, "pwm config failed\n");
  1580. return rc;
  1581. }
  1582. }
  1583. rc = pwm_enable(led->kpdbl_cfg->pwm_cfg->pwm_dev);
  1584. if (rc < 0) {
  1585. dev_err(&led->spmi_dev->dev, "pwm enable failed\n");
  1586. return rc;
  1587. }
  1588. set_bit(led->kpdbl_cfg->row_id, kpdbl_leds_in_use);
  1589. /* is_kpdbl_master_turn_on will be set to true when GPLED1
  1590. * channel is enabled and has a valid brightness value
  1591. */
  1592. if (led->kpdbl_cfg->always_on)
  1593. is_kpdbl_master_turn_on = true;
  1594. } else {
  1595. led->kpdbl_cfg->pwm_cfg->mode =
  1596. led->kpdbl_cfg->pwm_cfg->default_mode;
  1597. /* Before disabling GPLED1, check if any other GPLED 2/3/4 is
  1598. * on. If any of the other GPLED 2/3/4 is on, then have the
  1599. * GPLED1 channel enabled with 0 brightness.
  1600. */
  1601. if (led->kpdbl_cfg->always_on) {
  1602. if (bitmap_weight(kpdbl_leds_in_use,
  1603. NUM_KPDBL_LEDS) > 1) {
  1604. rc = pwm_config_us(
  1605. led->kpdbl_cfg->pwm_cfg->pwm_dev, 0,
  1606. led->kpdbl_cfg->pwm_cfg->pwm_period_us);
  1607. if (rc < 0) {
  1608. dev_err(&led->spmi_dev->dev,
  1609. "pwm config failed\n");
  1610. return rc;
  1611. }
  1612. rc = pwm_enable(led->kpdbl_cfg->pwm_cfg->
  1613. pwm_dev);
  1614. if (rc < 0) {
  1615. dev_err(&led->spmi_dev->dev,
  1616. "pwm enable failed\n");
  1617. return rc;
  1618. }
  1619. } else {
  1620. if (kpdbl_master) {
  1621. pwm_disable(kpdbl_master);
  1622. clear_bit(KPDBL_MASTER_BIT_INDEX,
  1623. kpdbl_leds_in_use);
  1624. rc = qpnp_led_masked_write(
  1625. led, KPDBL_ENABLE(led->base),
  1626. KPDBL_MODULE_EN_MASK,
  1627. KPDBL_MODULE_DIS);
  1628. if (rc) {
  1629. dev_err(&led->spmi_dev->dev,
  1630. "Failed to write led"
  1631. " enable reg\n");
  1632. return rc;
  1633. }
  1634. }
  1635. }
  1636. is_kpdbl_master_turn_on = false;
  1637. } else {
  1638. pwm_disable(led->kpdbl_cfg->pwm_cfg->pwm_dev);
  1639. clear_bit(led->kpdbl_cfg->row_id, kpdbl_leds_in_use);
  1640. if (bitmap_weight(kpdbl_leds_in_use,
  1641. NUM_KPDBL_LEDS) == 1 && kpdbl_master &&
  1642. !is_kpdbl_master_turn_on) {
  1643. pwm_disable(kpdbl_master);
  1644. clear_bit(KPDBL_MASTER_BIT_INDEX,
  1645. kpdbl_leds_in_use);
  1646. rc = qpnp_led_masked_write(
  1647. led, KPDBL_ENABLE(led->base),
  1648. KPDBL_MODULE_EN_MASK, KPDBL_MODULE_DIS);
  1649. if (rc) {
  1650. dev_err(&led->spmi_dev->dev,
  1651. "Failed to write led enable reg\n");
  1652. return rc;
  1653. }
  1654. is_kpdbl_master_turn_on = false;
  1655. }
  1656. }
  1657. }
  1658. led->kpdbl_cfg->pwm_cfg->blinking = false;
  1659. qpnp_dump_regs(led, kpdbl_debug_regs, ARRAY_SIZE(kpdbl_debug_regs));
  1660. return 0;
  1661. }
  1662. static int qpnp_rgb_set(struct qpnp_led_data *led)
  1663. {
  1664. int rc;
  1665. int duty_us, duty_ns, period_us;
  1666. if (led->cdev.brightness) {
  1667. if (!led->rgb_cfg->pwm_cfg->blinking)
  1668. led->rgb_cfg->pwm_cfg->mode =
  1669. led->rgb_cfg->pwm_cfg->default_mode;
  1670. if (led->rgb_cfg->pwm_cfg->mode == PWM_MODE) {
  1671. period_us = led->rgb_cfg->pwm_cfg->pwm_period_us;
  1672. if (period_us > INT_MAX / NSEC_PER_USEC) {
  1673. duty_us = (period_us * led->cdev.brightness) /
  1674. LED_FULL;
  1675. rc = pwm_config_us(
  1676. led->rgb_cfg->pwm_cfg->pwm_dev,
  1677. duty_us,
  1678. period_us);
  1679. } else {
  1680. duty_ns = ((period_us * NSEC_PER_USEC) /
  1681. LED_FULL) * led->cdev.brightness;
  1682. rc = pwm_config(
  1683. led->rgb_cfg->pwm_cfg->pwm_dev,
  1684. duty_ns,
  1685. period_us * NSEC_PER_USEC);
  1686. }
  1687. if (rc < 0) {
  1688. dev_err(&led->spmi_dev->dev,
  1689. "pwm config failed\n");
  1690. return rc;
  1691. }
  1692. }
  1693. rc = qpnp_led_masked_write(led,
  1694. RGB_LED_EN_CTL(led->base),
  1695. led->rgb_cfg->enable, led->rgb_cfg->enable);
  1696. if (rc) {
  1697. dev_err(&led->spmi_dev->dev,
  1698. "Failed to write led enable reg\n");
  1699. return rc;
  1700. }
  1701. rc = pwm_enable(led->rgb_cfg->pwm_cfg->pwm_dev);
  1702. if (rc < 0) {
  1703. dev_err(&led->spmi_dev->dev, "pwm enable failed\n");
  1704. return rc;
  1705. }
  1706. } else {
  1707. led->rgb_cfg->pwm_cfg->mode =
  1708. led->rgb_cfg->pwm_cfg->default_mode;
  1709. pwm_disable(led->rgb_cfg->pwm_cfg->pwm_dev);
  1710. rc = qpnp_led_masked_write(led,
  1711. RGB_LED_EN_CTL(led->base),
  1712. led->rgb_cfg->enable, RGB_LED_DISABLE);
  1713. if (rc) {
  1714. dev_err(&led->spmi_dev->dev,
  1715. "Failed to write led enable reg\n");
  1716. return rc;
  1717. }
  1718. }
  1719. led->rgb_cfg->pwm_cfg->blinking = false;
  1720. qpnp_dump_regs(led, rgb_pwm_debug_regs, ARRAY_SIZE(rgb_pwm_debug_regs));
  1721. return 0;
  1722. }
  1723. static void qpnp_led_set(struct led_classdev *led_cdev,
  1724. enum led_brightness value)
  1725. {
  1726. struct qpnp_led_data *led;
  1727. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  1728. if (value < LED_OFF) {
  1729. dev_err(&led->spmi_dev->dev, "Invalid brightness value\n");
  1730. return;
  1731. }
  1732. if (value > led->cdev.max_brightness)
  1733. value = led->cdev.max_brightness;
  1734. #if defined(CONFIG_MACH_AFYONLTE_TMO) || defined(CONFIG_MACH_AFYONLTE_CAN) || defined(CONFIG_MACH_MS01_EUR_3G) \
  1735. || defined(CONFIG_MACH_AFYONLTE_MTR)
  1736. pr_info("[LED]%s: %s value = %d\n", __func__, led_cdev->name, value);
  1737. if(strncmp(led_cdev->name, "button-backlight", 16))
  1738. led->cdev.brightness = value;
  1739. else
  1740. led->cdev.brightness = value?SAMSUNG_TKEY_LED_BRIGHTNESS:0;
  1741. #else
  1742. led->cdev.brightness = value;
  1743. #endif
  1744. schedule_work(&led->work);
  1745. }
  1746. static void __qpnp_led_work(struct qpnp_led_data *led,
  1747. enum led_brightness value)
  1748. {
  1749. int rc;
  1750. if (led->id == QPNP_ID_FLASH1_LED0 || led->id == QPNP_ID_FLASH1_LED1)
  1751. mutex_lock(&flash_lock);
  1752. else
  1753. mutex_lock(&led->lock);
  1754. switch (led->id) {
  1755. case QPNP_ID_WLED:
  1756. rc = qpnp_wled_set(led);
  1757. if (rc < 0)
  1758. dev_err(&led->spmi_dev->dev,
  1759. "WLED set brightness failed (%d)\n", rc);
  1760. break;
  1761. case QPNP_ID_FLASH1_LED0:
  1762. case QPNP_ID_FLASH1_LED1:
  1763. rc = qpnp_flash_set(led);
  1764. if (rc < 0)
  1765. dev_err(&led->spmi_dev->dev,
  1766. "FLASH set brightness failed (%d)\n", rc);
  1767. break;
  1768. case QPNP_ID_RGB_RED:
  1769. case QPNP_ID_RGB_GREEN:
  1770. case QPNP_ID_RGB_BLUE:
  1771. rc = qpnp_rgb_set(led);
  1772. if (rc < 0)
  1773. dev_err(&led->spmi_dev->dev,
  1774. "RGB set brightness failed (%d)\n", rc);
  1775. break;
  1776. case QPNP_ID_LED_MPP:
  1777. rc = qpnp_mpp_set(led);
  1778. if (rc < 0)
  1779. dev_err(&led->spmi_dev->dev,
  1780. "MPP set brightness failed (%d)\n", rc);
  1781. break;
  1782. case QPNP_ID_KPDBL:
  1783. rc = qpnp_kpdbl_set(led);
  1784. if (rc < 0)
  1785. dev_err(&led->spmi_dev->dev,
  1786. "KPDBL set brightness failed (%d)\n", rc);
  1787. break;
  1788. default:
  1789. dev_err(&led->spmi_dev->dev, "Invalid LED(%d)\n", led->id);
  1790. break;
  1791. }
  1792. if (led->id == QPNP_ID_FLASH1_LED0 || led->id == QPNP_ID_FLASH1_LED1)
  1793. mutex_unlock(&flash_lock);
  1794. else
  1795. mutex_unlock(&led->lock);
  1796. }
  1797. static void qpnp_led_work(struct work_struct *work)
  1798. {
  1799. struct qpnp_led_data *led = container_of(work,
  1800. struct qpnp_led_data, work);
  1801. __qpnp_led_work(led, led->cdev.brightness);
  1802. return;
  1803. }
  1804. void tkey_led_enables(int level)
  1805. {
  1806. long brightness_menu = 0;
  1807. long brightness_back = 0;
  1808. struct qpnp_led_data *led_array;
  1809. led_array = dev_get_drvdata(led_dev);
  1810. printk(KERN_INFO "%s: touchkey LED level : %d\n", __func__, level);
  1811. if (!led_array) {
  1812. printk(KERN_INFO "%s: led_array is NULL\n", __func__);
  1813. return;
  1814. }
  1815. if (level == 1) {
  1816. #if defined(CONFIG_SEC_LT03_PROJECT)
  1817. brightness_menu = RGB_MAX_LEVEL_N1;
  1818. brightness_back = RGB_MAX_LEVEL_N1;
  1819. #elif defined(CONFIG_SEC_PICASSO_PROJECT)
  1820. brightness_menu = RGB_MAX_LEVEL_N2_MENU;
  1821. brightness_back = RGB_MAX_LEVEL_N2_BACK;
  1822. #elif defined(CONFIG_SEC_VIENNA_PROJECT)
  1823. brightness_menu = RGB_MAX_LEVEL_V1;
  1824. brightness_back = RGB_MAX_LEVEL_V1;
  1825. #else
  1826. brightness_menu = RGB_MAX_LEVEL;
  1827. brightness_back = RGB_MAX_LEVEL;
  1828. #endif
  1829. }
  1830. else if (level == 0) {
  1831. brightness_menu = LED_OFF;
  1832. brightness_back = LED_OFF;
  1833. }
  1834. else {
  1835. brightness_menu = level;
  1836. brightness_back = level;
  1837. }
  1838. qpnp_led_set(&led_array[RGB_BLUE].cdev, brightness_back);
  1839. qpnp_led_set(&led_array[RGB_GREEN].cdev, brightness_menu);
  1840. }
  1841. EXPORT_SYMBOL(tkey_led_enables);
  1842. static int __devinit qpnp_led_set_max_brightness(struct qpnp_led_data *led)
  1843. {
  1844. switch (led->id) {
  1845. case QPNP_ID_WLED:
  1846. led->cdev.max_brightness = WLED_MAX_LEVEL;
  1847. break;
  1848. case QPNP_ID_FLASH1_LED0:
  1849. case QPNP_ID_FLASH1_LED1:
  1850. led->cdev.max_brightness = led->max_current;
  1851. break;
  1852. case QPNP_ID_RGB_RED:
  1853. case QPNP_ID_RGB_GREEN:
  1854. case QPNP_ID_RGB_BLUE:
  1855. led->cdev.max_brightness = RGB_MAX_LEVEL;
  1856. break;
  1857. case QPNP_ID_LED_MPP:
  1858. if (led->mpp_cfg->pwm_mode == MANUAL_MODE)
  1859. led->cdev.max_brightness = led->max_current;
  1860. else
  1861. led->cdev.max_brightness = MPP_MAX_LEVEL;
  1862. break;
  1863. case QPNP_ID_KPDBL:
  1864. led->cdev.max_brightness = KPDBL_MAX_LEVEL;
  1865. break;
  1866. default:
  1867. dev_err(&led->spmi_dev->dev, "Invalid LED(%d)\n", led->id);
  1868. return -EINVAL;
  1869. }
  1870. return 0;
  1871. }
  1872. static enum led_brightness qpnp_led_get(struct led_classdev *led_cdev)
  1873. {
  1874. struct qpnp_led_data *led;
  1875. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  1876. return led->cdev.brightness;
  1877. }
  1878. static void qpnp_led_turn_off_delayed(struct work_struct *work)
  1879. {
  1880. struct delayed_work *dwork = to_delayed_work(work);
  1881. struct qpnp_led_data *led
  1882. = container_of(dwork, struct qpnp_led_data, dwork);
  1883. led->cdev.brightness = LED_OFF;
  1884. qpnp_led_set(&led->cdev, led->cdev.brightness);
  1885. }
  1886. static void qpnp_led_turn_off(struct qpnp_led_data *led)
  1887. {
  1888. INIT_DELAYED_WORK(&led->dwork, qpnp_led_turn_off_delayed);
  1889. schedule_delayed_work(&led->dwork,
  1890. msecs_to_jiffies(led->turn_off_delay_ms));
  1891. }
  1892. static int __devinit qpnp_wled_init(struct qpnp_led_data *led)
  1893. {
  1894. int rc, i;
  1895. u8 num_wled_strings;
  1896. num_wled_strings = led->wled_cfg->num_strings;
  1897. /* verify ranges */
  1898. if (led->wled_cfg->ovp_val > WLED_OVP_27V) {
  1899. dev_err(&led->spmi_dev->dev, "Invalid ovp value\n");
  1900. return -EINVAL;
  1901. }
  1902. if (led->wled_cfg->boost_curr_lim > WLED_CURR_LIMIT_1680mA) {
  1903. dev_err(&led->spmi_dev->dev, "Invalid boost current limit\n");
  1904. return -EINVAL;
  1905. }
  1906. if (led->wled_cfg->cp_select > WLED_CP_SELECT_MAX) {
  1907. dev_err(&led->spmi_dev->dev, "Invalid pole capacitance\n");
  1908. return -EINVAL;
  1909. }
  1910. if ((led->max_current > WLED_MAX_CURR)) {
  1911. dev_err(&led->spmi_dev->dev, "Invalid max current\n");
  1912. return -EINVAL;
  1913. }
  1914. if ((led->wled_cfg->ctrl_delay_us % WLED_CTL_DLY_STEP) ||
  1915. (led->wled_cfg->ctrl_delay_us > WLED_CTL_DLY_MAX)) {
  1916. dev_err(&led->spmi_dev->dev, "Invalid control delay\n");
  1917. return -EINVAL;
  1918. }
  1919. /* program over voltage protection threshold */
  1920. rc = qpnp_led_masked_write(led, WLED_OVP_CFG_REG(led->base),
  1921. WLED_OVP_VAL_MASK,
  1922. (led->wled_cfg->ovp_val << WLED_OVP_VAL_BIT_SHFT));
  1923. if (rc) {
  1924. dev_err(&led->spmi_dev->dev,
  1925. "WLED OVP reg write failed(%d)\n", rc);
  1926. return rc;
  1927. }
  1928. /* program current boost limit */
  1929. rc = qpnp_led_masked_write(led, WLED_BOOST_LIMIT_REG(led->base),
  1930. WLED_BOOST_LIMIT_MASK, led->wled_cfg->boost_curr_lim);
  1931. if (rc) {
  1932. dev_err(&led->spmi_dev->dev,
  1933. "WLED boost limit reg write failed(%d)\n", rc);
  1934. return rc;
  1935. }
  1936. /* program output feedback */
  1937. rc = qpnp_led_masked_write(led, WLED_FDBCK_CTRL_REG(led->base),
  1938. WLED_OP_FDBCK_MASK,
  1939. (led->wled_cfg->op_fdbck << WLED_OP_FDBCK_BIT_SHFT));
  1940. if (rc) {
  1941. dev_err(&led->spmi_dev->dev,
  1942. "WLED fdbck ctrl reg write failed(%d)\n", rc);
  1943. return rc;
  1944. }
  1945. /* program switch frequency */
  1946. rc = qpnp_led_masked_write(led,
  1947. WLED_SWITCHING_FREQ_REG(led->base),
  1948. WLED_SWITCH_FREQ_MASK, led->wled_cfg->switch_freq);
  1949. if (rc) {
  1950. dev_err(&led->spmi_dev->dev,
  1951. "WLED switch freq reg write failed(%d)\n", rc);
  1952. return rc;
  1953. }
  1954. /* program current sink */
  1955. if (led->wled_cfg->cs_out_en) {
  1956. rc = qpnp_led_masked_write(led, WLED_CURR_SINK_REG(led->base),
  1957. WLED_CURR_SINK_MASK,
  1958. (((1 << led->wled_cfg->num_strings) - 1)
  1959. << WLED_CURR_SINK_SHFT));
  1960. if (rc) {
  1961. dev_err(&led->spmi_dev->dev,
  1962. "WLED curr sink reg write failed(%d)\n", rc);
  1963. return rc;
  1964. }
  1965. }
  1966. /* program high pole capacitance */
  1967. rc = qpnp_led_masked_write(led, WLED_HIGH_POLE_CAP_REG(led->base),
  1968. WLED_CP_SELECT_MASK, led->wled_cfg->cp_select);
  1969. if (rc) {
  1970. dev_err(&led->spmi_dev->dev,
  1971. "WLED pole cap reg write failed(%d)\n", rc);
  1972. return rc;
  1973. }
  1974. /* program modulator, current mod src and cabc */
  1975. for (i = 0; i < num_wled_strings; i++) {
  1976. rc = qpnp_led_masked_write(led, WLED_MOD_EN_REG(led->base, i),
  1977. WLED_NO_MASK, WLED_EN_MASK);
  1978. if (rc) {
  1979. dev_err(&led->spmi_dev->dev,
  1980. "WLED mod enable reg write failed(%d)\n", rc);
  1981. return rc;
  1982. }
  1983. if (led->wled_cfg->dig_mod_gen_en) {
  1984. rc = qpnp_led_masked_write(led,
  1985. WLED_MOD_SRC_SEL_REG(led->base, i),
  1986. WLED_NO_MASK, WLED_USE_EXT_GEN_MOD_SRC);
  1987. if (rc) {
  1988. dev_err(&led->spmi_dev->dev,
  1989. "WLED dig mod en reg write failed(%d)\n", rc);
  1990. }
  1991. }
  1992. rc = qpnp_led_masked_write(led,
  1993. WLED_FULL_SCALE_REG(led->base, i), WLED_MAX_CURR_MASK,
  1994. (u8)led->max_current);
  1995. if (rc) {
  1996. dev_err(&led->spmi_dev->dev,
  1997. "WLED max current reg write failed(%d)\n", rc);
  1998. return rc;
  1999. }
  2000. }
  2001. /* Reset WLED enable register */
  2002. rc = qpnp_led_masked_write(led, WLED_MOD_CTRL_REG(led->base),
  2003. WLED_8_BIT_MASK, WLED_BOOST_OFF);
  2004. if (rc) {
  2005. dev_err(&led->spmi_dev->dev,
  2006. "WLED write ctrl reg failed(%d)\n", rc);
  2007. return rc;
  2008. }
  2009. /* dump wled registers */
  2010. qpnp_dump_regs(led, wled_debug_regs, ARRAY_SIZE(wled_debug_regs));
  2011. return 0;
  2012. }
  2013. static ssize_t led_mode_store(struct device *dev,
  2014. struct device_attribute *attr,
  2015. const char *buf, size_t count)
  2016. {
  2017. struct qpnp_led_data *led;
  2018. unsigned long state;
  2019. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2020. ssize_t ret = -EINVAL;
  2021. ret = kstrtoul(buf, 10, &state);
  2022. if (ret)
  2023. return ret;
  2024. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2025. /* '1' to enable torch mode; '0' to switch to flash mode */
  2026. if (state == 1)
  2027. led->flash_cfg->torch_enable = true;
  2028. else
  2029. led->flash_cfg->torch_enable = false;
  2030. return count;
  2031. }
  2032. static ssize_t led_strobe_type_store(struct device *dev,
  2033. struct device_attribute *attr,
  2034. const char *buf, size_t count)
  2035. {
  2036. struct qpnp_led_data *led;
  2037. unsigned long state;
  2038. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2039. ssize_t ret = -EINVAL;
  2040. ret = kstrtoul(buf, 10, &state);
  2041. if (ret)
  2042. return ret;
  2043. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2044. /* '0' for sw strobe; '1' for hw strobe */
  2045. if (state == 1)
  2046. led->flash_cfg->strobe_type = 1;
  2047. else
  2048. led->flash_cfg->strobe_type = 0;
  2049. return count;
  2050. }
  2051. static int qpnp_pwm_init(struct pwm_config_data *pwm_cfg,
  2052. struct spmi_device *spmi_dev,
  2053. const char *name)
  2054. {
  2055. int rc, start_idx, idx_len, lut_max_size;
  2056. if (pwm_cfg->pwm_channel != -1) {
  2057. pwm_cfg->pwm_dev =
  2058. pwm_request(pwm_cfg->pwm_channel, name);
  2059. if (IS_ERR_OR_NULL(pwm_cfg->pwm_dev)) {
  2060. dev_err(&spmi_dev->dev,
  2061. "could not acquire PWM Channel %d, " \
  2062. "error %ld\n",
  2063. pwm_cfg->pwm_channel,
  2064. PTR_ERR(pwm_cfg->pwm_dev));
  2065. pwm_cfg->pwm_dev = NULL;
  2066. return -ENODEV;
  2067. }
  2068. if (pwm_cfg->mode == LPG_MODE) {
  2069. start_idx =
  2070. pwm_cfg->duty_cycles->start_idx;
  2071. idx_len =
  2072. pwm_cfg->duty_cycles->num_duty_pcts;
  2073. if (strnstr(name, "kpdbl", sizeof("kpdbl")))
  2074. lut_max_size = PWM_GPLED_LUT_MAX_SIZE;
  2075. else
  2076. lut_max_size = PWM_LUT_MAX_SIZE;
  2077. if (idx_len >= lut_max_size && start_idx) {
  2078. dev_err(&spmi_dev->dev,
  2079. "Wrong LUT size or index\n");
  2080. return -EINVAL;
  2081. }
  2082. if ((start_idx + idx_len) > lut_max_size) {
  2083. dev_err(&spmi_dev->dev,
  2084. "Exceed LUT limit\n");
  2085. return -EINVAL;
  2086. }
  2087. rc = pwm_lut_config(pwm_cfg->pwm_dev,
  2088. pwm_cfg->pwm_period_us,
  2089. pwm_cfg->duty_cycles->duty_pcts,
  2090. pwm_cfg->lut_params);
  2091. if (rc < 0) {
  2092. dev_err(&spmi_dev->dev, "Failed to " \
  2093. "configure pwm LUT\n");
  2094. return rc;
  2095. }
  2096. }
  2097. } else {
  2098. dev_err(&spmi_dev->dev,
  2099. "Invalid PWM channel\n");
  2100. return -EINVAL;
  2101. }
  2102. return 0;
  2103. }
  2104. static ssize_t pwm_us_store(struct device *dev,
  2105. struct device_attribute *attr,
  2106. const char *buf, size_t count)
  2107. {
  2108. struct qpnp_led_data *led;
  2109. u32 pwm_us;
  2110. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2111. ssize_t ret;
  2112. u32 previous_pwm_us;
  2113. struct pwm_config_data *pwm_cfg;
  2114. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2115. ret = kstrtou32(buf, 10, &pwm_us);
  2116. if (ret)
  2117. return ret;
  2118. switch (led->id) {
  2119. case QPNP_ID_LED_MPP:
  2120. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2121. break;
  2122. case QPNP_ID_RGB_RED:
  2123. case QPNP_ID_RGB_GREEN:
  2124. case QPNP_ID_RGB_BLUE:
  2125. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2126. break;
  2127. case QPNP_ID_KPDBL:
  2128. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2129. break;
  2130. default:
  2131. dev_err(&led->spmi_dev->dev,
  2132. "Invalid LED id type for pwm_us\n");
  2133. return -EINVAL;
  2134. }
  2135. if (pwm_cfg->mode == LPG_MODE)
  2136. pwm_cfg->blinking = true;
  2137. previous_pwm_us = pwm_cfg->pwm_period_us;
  2138. pwm_cfg->pwm_period_us = pwm_us;
  2139. pwm_free(pwm_cfg->pwm_dev);
  2140. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2141. if (ret) {
  2142. pwm_cfg->pwm_period_us = previous_pwm_us;
  2143. pwm_free(pwm_cfg->pwm_dev);
  2144. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2145. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2146. dev_err(&led->spmi_dev->dev,
  2147. "Failed to initialize pwm with new pwm_us value\n");
  2148. return ret;
  2149. }
  2150. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2151. return count;
  2152. }
  2153. static ssize_t pause_lo_store(struct device *dev,
  2154. struct device_attribute *attr,
  2155. const char *buf, size_t count)
  2156. {
  2157. struct qpnp_led_data *led;
  2158. u32 pause_lo;
  2159. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2160. ssize_t ret;
  2161. u32 previous_pause_lo;
  2162. struct pwm_config_data *pwm_cfg;
  2163. ret = kstrtou32(buf, 10, &pause_lo);
  2164. if (ret)
  2165. return ret;
  2166. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2167. switch (led->id) {
  2168. case QPNP_ID_LED_MPP:
  2169. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2170. break;
  2171. case QPNP_ID_RGB_RED:
  2172. case QPNP_ID_RGB_GREEN:
  2173. case QPNP_ID_RGB_BLUE:
  2174. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2175. break;
  2176. case QPNP_ID_KPDBL:
  2177. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2178. break;
  2179. default:
  2180. dev_err(&led->spmi_dev->dev,
  2181. "Invalid LED id type for pause lo\n");
  2182. return -EINVAL;
  2183. }
  2184. if (pwm_cfg->mode == LPG_MODE)
  2185. pwm_cfg->blinking = true;
  2186. previous_pause_lo = pwm_cfg->lut_params.lut_pause_lo;
  2187. pwm_free(pwm_cfg->pwm_dev);
  2188. pwm_cfg->lut_params.lut_pause_lo = pause_lo;
  2189. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2190. if (ret) {
  2191. pwm_cfg->lut_params.lut_pause_lo = previous_pause_lo;
  2192. pwm_free(pwm_cfg->pwm_dev);
  2193. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2194. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2195. dev_err(&led->spmi_dev->dev,
  2196. "Failed to initialize pwm with new pause lo value\n");
  2197. return ret;
  2198. }
  2199. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2200. return count;
  2201. }
  2202. static ssize_t pause_hi_store(struct device *dev,
  2203. struct device_attribute *attr,
  2204. const char *buf, size_t count)
  2205. {
  2206. struct qpnp_led_data *led;
  2207. u32 pause_hi;
  2208. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2209. ssize_t ret;
  2210. u32 previous_pause_hi;
  2211. struct pwm_config_data *pwm_cfg;
  2212. ret = kstrtou32(buf, 10, &pause_hi);
  2213. if (ret)
  2214. return ret;
  2215. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2216. switch (led->id) {
  2217. case QPNP_ID_LED_MPP:
  2218. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2219. break;
  2220. case QPNP_ID_RGB_RED:
  2221. case QPNP_ID_RGB_GREEN:
  2222. case QPNP_ID_RGB_BLUE:
  2223. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2224. break;
  2225. case QPNP_ID_KPDBL:
  2226. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2227. break;
  2228. default:
  2229. dev_err(&led->spmi_dev->dev,
  2230. "Invalid LED id type for pause hi\n");
  2231. return -EINVAL;
  2232. }
  2233. if (pwm_cfg->mode == LPG_MODE)
  2234. pwm_cfg->blinking = true;
  2235. previous_pause_hi = pwm_cfg->lut_params.lut_pause_hi;
  2236. pwm_free(pwm_cfg->pwm_dev);
  2237. pwm_cfg->lut_params.lut_pause_hi = pause_hi;
  2238. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2239. if (ret) {
  2240. pwm_cfg->lut_params.lut_pause_hi = previous_pause_hi;
  2241. pwm_free(pwm_cfg->pwm_dev);
  2242. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2243. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2244. dev_err(&led->spmi_dev->dev,
  2245. "Failed to initialize pwm with new pause hi value\n");
  2246. return ret;
  2247. }
  2248. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2249. return count;
  2250. }
  2251. static ssize_t start_idx_store(struct device *dev,
  2252. struct device_attribute *attr,
  2253. const char *buf, size_t count)
  2254. {
  2255. struct qpnp_led_data *led;
  2256. u32 start_idx;
  2257. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2258. ssize_t ret;
  2259. u32 previous_start_idx;
  2260. struct pwm_config_data *pwm_cfg;
  2261. ret = kstrtou32(buf, 10, &start_idx);
  2262. if (ret)
  2263. return ret;
  2264. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2265. switch (led->id) {
  2266. case QPNP_ID_LED_MPP:
  2267. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2268. break;
  2269. case QPNP_ID_RGB_RED:
  2270. case QPNP_ID_RGB_GREEN:
  2271. case QPNP_ID_RGB_BLUE:
  2272. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2273. break;
  2274. case QPNP_ID_KPDBL:
  2275. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2276. break;
  2277. default:
  2278. dev_err(&led->spmi_dev->dev,
  2279. "Invalid LED id type for start idx\n");
  2280. return -EINVAL;
  2281. }
  2282. if (pwm_cfg->mode == LPG_MODE)
  2283. pwm_cfg->blinking = true;
  2284. previous_start_idx = pwm_cfg->duty_cycles->start_idx;
  2285. pwm_cfg->duty_cycles->start_idx = start_idx;
  2286. pwm_cfg->lut_params.start_idx = pwm_cfg->duty_cycles->start_idx;
  2287. pwm_free(pwm_cfg->pwm_dev);
  2288. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2289. if (ret) {
  2290. pwm_cfg->duty_cycles->start_idx = previous_start_idx;
  2291. pwm_cfg->lut_params.start_idx = pwm_cfg->duty_cycles->start_idx;
  2292. pwm_free(pwm_cfg->pwm_dev);
  2293. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2294. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2295. dev_err(&led->spmi_dev->dev,
  2296. "Failed to initialize pwm with new start idx value\n");
  2297. return ret;
  2298. }
  2299. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2300. return count;
  2301. }
  2302. static ssize_t ramp_step_ms_store(struct device *dev,
  2303. struct device_attribute *attr,
  2304. const char *buf, size_t count)
  2305. {
  2306. struct qpnp_led_data *led;
  2307. u32 ramp_step_ms;
  2308. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2309. ssize_t ret;
  2310. u32 previous_ramp_step_ms;
  2311. struct pwm_config_data *pwm_cfg;
  2312. ret = kstrtou32(buf, 10, &ramp_step_ms);
  2313. if (ret)
  2314. return ret;
  2315. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2316. switch (led->id) {
  2317. case QPNP_ID_LED_MPP:
  2318. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2319. break;
  2320. case QPNP_ID_RGB_RED:
  2321. case QPNP_ID_RGB_GREEN:
  2322. case QPNP_ID_RGB_BLUE:
  2323. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2324. break;
  2325. case QPNP_ID_KPDBL:
  2326. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2327. break;
  2328. default:
  2329. dev_err(&led->spmi_dev->dev,
  2330. "Invalid LED id type for ramp step\n");
  2331. return -EINVAL;
  2332. }
  2333. if (pwm_cfg->mode == LPG_MODE)
  2334. pwm_cfg->blinking = true;
  2335. previous_ramp_step_ms = pwm_cfg->lut_params.ramp_step_ms;
  2336. pwm_free(pwm_cfg->pwm_dev);
  2337. pwm_cfg->lut_params.ramp_step_ms = ramp_step_ms;
  2338. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2339. if (ret) {
  2340. pwm_cfg->lut_params.ramp_step_ms = previous_ramp_step_ms;
  2341. pwm_free(pwm_cfg->pwm_dev);
  2342. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2343. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2344. dev_err(&led->spmi_dev->dev,
  2345. "Failed to initialize pwm with new ramp step value\n");
  2346. return ret;
  2347. }
  2348. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2349. return count;
  2350. }
  2351. static ssize_t lut_flags_store(struct device *dev,
  2352. struct device_attribute *attr,
  2353. const char *buf, size_t count)
  2354. {
  2355. struct qpnp_led_data *led;
  2356. u32 lut_flags;
  2357. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2358. ssize_t ret;
  2359. u32 previous_lut_flags;
  2360. struct pwm_config_data *pwm_cfg;
  2361. ret = kstrtou32(buf, 10, &lut_flags);
  2362. if (ret)
  2363. return ret;
  2364. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2365. switch (led->id) {
  2366. case QPNP_ID_LED_MPP:
  2367. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2368. break;
  2369. case QPNP_ID_RGB_RED:
  2370. case QPNP_ID_RGB_GREEN:
  2371. case QPNP_ID_RGB_BLUE:
  2372. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2373. break;
  2374. case QPNP_ID_KPDBL:
  2375. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2376. break;
  2377. default:
  2378. dev_err(&led->spmi_dev->dev,
  2379. "Invalid LED id type for lut flags\n");
  2380. return -EINVAL;
  2381. }
  2382. if (pwm_cfg->mode == LPG_MODE)
  2383. pwm_cfg->blinking = true;
  2384. previous_lut_flags = pwm_cfg->lut_params.flags;
  2385. pwm_free(pwm_cfg->pwm_dev);
  2386. pwm_cfg->lut_params.flags = lut_flags;
  2387. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2388. if (ret) {
  2389. pwm_cfg->lut_params.flags = previous_lut_flags;
  2390. pwm_free(pwm_cfg->pwm_dev);
  2391. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2392. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2393. dev_err(&led->spmi_dev->dev,
  2394. "Failed to initialize pwm with new lut flags value\n");
  2395. return ret;
  2396. }
  2397. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2398. return count;
  2399. }
  2400. static ssize_t duty_pcts_store(struct device *dev,
  2401. struct device_attribute *attr,
  2402. const char *buf, size_t count)
  2403. {
  2404. struct qpnp_led_data *led;
  2405. int num_duty_pcts = 0;
  2406. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2407. char *buffer;
  2408. ssize_t ret;
  2409. int i = 0;
  2410. int max_duty_pcts;
  2411. struct pwm_config_data *pwm_cfg;
  2412. u32 previous_num_duty_pcts;
  2413. int value;
  2414. int *previous_duty_pcts;
  2415. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2416. switch (led->id) {
  2417. case QPNP_ID_LED_MPP:
  2418. pwm_cfg = led->mpp_cfg->pwm_cfg;
  2419. max_duty_pcts = PWM_LUT_MAX_SIZE;
  2420. break;
  2421. case QPNP_ID_RGB_RED:
  2422. case QPNP_ID_RGB_GREEN:
  2423. case QPNP_ID_RGB_BLUE:
  2424. pwm_cfg = led->rgb_cfg->pwm_cfg;
  2425. max_duty_pcts = PWM_LUT_MAX_SIZE;
  2426. break;
  2427. case QPNP_ID_KPDBL:
  2428. pwm_cfg = led->kpdbl_cfg->pwm_cfg;
  2429. max_duty_pcts = PWM_GPLED_LUT_MAX_SIZE;
  2430. break;
  2431. default:
  2432. dev_err(&led->spmi_dev->dev,
  2433. "Invalid LED id type for duty pcts\n");
  2434. return -EINVAL;
  2435. }
  2436. if (pwm_cfg->mode == LPG_MODE)
  2437. pwm_cfg->blinking = true;
  2438. buffer = (char *)buf;
  2439. for (i = 0; i < max_duty_pcts; i++) {
  2440. if (buffer == NULL)
  2441. break;
  2442. ret = sscanf((const char *)buffer, "%u,%s", &value, buffer);
  2443. pwm_cfg->old_duty_pcts[i] = value;
  2444. num_duty_pcts++;
  2445. if (ret <= 1)
  2446. break;
  2447. }
  2448. if (num_duty_pcts >= max_duty_pcts) {
  2449. dev_err(&led->spmi_dev->dev,
  2450. "Number of duty pcts given exceeds max (%d)\n",
  2451. max_duty_pcts);
  2452. return -EINVAL;
  2453. }
  2454. previous_num_duty_pcts = pwm_cfg->duty_cycles->num_duty_pcts;
  2455. previous_duty_pcts = pwm_cfg->duty_cycles->duty_pcts;
  2456. pwm_cfg->duty_cycles->num_duty_pcts = num_duty_pcts;
  2457. pwm_cfg->duty_cycles->duty_pcts = pwm_cfg->old_duty_pcts;
  2458. pwm_cfg->old_duty_pcts = previous_duty_pcts;
  2459. pwm_cfg->lut_params.idx_len = pwm_cfg->duty_cycles->num_duty_pcts;
  2460. pwm_free(pwm_cfg->pwm_dev);
  2461. ret = qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2462. if (ret)
  2463. goto restore;
  2464. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2465. return count;
  2466. restore:
  2467. dev_err(&led->spmi_dev->dev,
  2468. "Failed to initialize pwm with new duty pcts value\n");
  2469. pwm_cfg->duty_cycles->num_duty_pcts = previous_num_duty_pcts;
  2470. pwm_cfg->old_duty_pcts = pwm_cfg->duty_cycles->duty_pcts;
  2471. pwm_cfg->duty_cycles->duty_pcts = previous_duty_pcts;
  2472. pwm_cfg->lut_params.idx_len = pwm_cfg->duty_cycles->num_duty_pcts;
  2473. pwm_free(pwm_cfg->pwm_dev);
  2474. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2475. qpnp_led_set(&led->cdev, led->cdev.brightness);
  2476. return ret;
  2477. }
  2478. static void led_blink(struct qpnp_led_data *led,
  2479. struct pwm_config_data *pwm_cfg)
  2480. {
  2481. int rc;
  2482. flush_work(&led->work);
  2483. mutex_lock(&led->lock);
  2484. if (pwm_cfg->use_blink) {
  2485. if (led->cdev.brightness) {
  2486. pwm_cfg->blinking = true;
  2487. if (led->id == QPNP_ID_LED_MPP)
  2488. led->mpp_cfg->pwm_mode = LPG_MODE;
  2489. else if (led->id == QPNP_ID_KPDBL)
  2490. led->kpdbl_cfg->pwm_mode = LPG_MODE;
  2491. pwm_cfg->mode = LPG_MODE;
  2492. } else {
  2493. pwm_cfg->blinking = false;
  2494. pwm_cfg->mode = pwm_cfg->default_mode;
  2495. if (led->id == QPNP_ID_LED_MPP)
  2496. led->mpp_cfg->pwm_mode = pwm_cfg->default_mode;
  2497. else if (led->id == QPNP_ID_KPDBL)
  2498. led->kpdbl_cfg->pwm_mode =
  2499. pwm_cfg->default_mode;
  2500. }
  2501. pwm_free(pwm_cfg->pwm_dev);
  2502. qpnp_pwm_init(pwm_cfg, led->spmi_dev, led->cdev.name);
  2503. if (led->id == QPNP_ID_RGB_RED || led->id == QPNP_ID_RGB_GREEN
  2504. || led->id == QPNP_ID_RGB_BLUE) {
  2505. rc = qpnp_rgb_set(led);
  2506. if (rc < 0)
  2507. dev_err(&led->spmi_dev->dev,
  2508. "RGB set brightness failed (%d)\n", rc);
  2509. } else if (led->id == QPNP_ID_LED_MPP) {
  2510. rc = qpnp_mpp_set(led);
  2511. if (rc < 0)
  2512. dev_err(&led->spmi_dev->dev,
  2513. "MPP set brightness failed (%d)\n", rc);
  2514. } else if (led->id == QPNP_ID_KPDBL) {
  2515. rc = qpnp_kpdbl_set(led);
  2516. if (rc < 0)
  2517. dev_err(&led->spmi_dev->dev,
  2518. "KPDBL set brightness failed (%d)\n", rc);
  2519. }
  2520. }
  2521. mutex_unlock(&led->lock);
  2522. }
  2523. static ssize_t blink_store(struct device *dev,
  2524. struct device_attribute *attr,
  2525. const char *buf, size_t count)
  2526. {
  2527. struct qpnp_led_data *led;
  2528. unsigned long blinking;
  2529. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  2530. ssize_t ret = -EINVAL;
  2531. ret = kstrtoul(buf, 10, &blinking);
  2532. if (ret)
  2533. return ret;
  2534. led = container_of(led_cdev, struct qpnp_led_data, cdev);
  2535. led->cdev.brightness = blinking ? led->cdev.max_brightness : 0;
  2536. switch (led->id) {
  2537. case QPNP_ID_LED_MPP:
  2538. led_blink(led, led->mpp_cfg->pwm_cfg);
  2539. break;
  2540. case QPNP_ID_RGB_RED:
  2541. case QPNP_ID_RGB_GREEN:
  2542. case QPNP_ID_RGB_BLUE:
  2543. led_blink(led, led->rgb_cfg->pwm_cfg);
  2544. break;
  2545. case QPNP_ID_KPDBL:
  2546. led_blink(led, led->kpdbl_cfg->pwm_cfg);
  2547. break;
  2548. default:
  2549. dev_err(&led->spmi_dev->dev, "Invalid LED id type for blink\n");
  2550. return -EINVAL;
  2551. }
  2552. return count;
  2553. }
  2554. static DEVICE_ATTR(led_mode, 0664, NULL, led_mode_store);
  2555. static DEVICE_ATTR(strobe, 0664, NULL, led_strobe_type_store);
  2556. static DEVICE_ATTR(pwm_us, 0664, NULL, pwm_us_store);
  2557. static DEVICE_ATTR(pause_lo, 0664, NULL, pause_lo_store);
  2558. static DEVICE_ATTR(pause_hi, 0664, NULL, pause_hi_store);
  2559. static DEVICE_ATTR(start_idx, 0664, NULL, start_idx_store);
  2560. static DEVICE_ATTR(ramp_step_ms, 0664, NULL, ramp_step_ms_store);
  2561. static DEVICE_ATTR(lut_flags, 0664, NULL, lut_flags_store);
  2562. static DEVICE_ATTR(duty_pcts, 0664, NULL, duty_pcts_store);
  2563. static DEVICE_ATTR(blink, 0664, NULL, blink_store);
  2564. static struct attribute *led_attrs[] = {
  2565. &dev_attr_led_mode.attr,
  2566. &dev_attr_strobe.attr,
  2567. NULL
  2568. };
  2569. static const struct attribute_group led_attr_group = {
  2570. .attrs = led_attrs,
  2571. };
  2572. static struct attribute *pwm_attrs[] = {
  2573. &dev_attr_pwm_us.attr,
  2574. NULL
  2575. };
  2576. static struct attribute *lpg_attrs[] = {
  2577. &dev_attr_pause_lo.attr,
  2578. &dev_attr_pause_hi.attr,
  2579. &dev_attr_start_idx.attr,
  2580. &dev_attr_ramp_step_ms.attr,
  2581. &dev_attr_lut_flags.attr,
  2582. &dev_attr_duty_pcts.attr,
  2583. NULL
  2584. };
  2585. static struct attribute *blink_attrs[] = {
  2586. &dev_attr_blink.attr,
  2587. NULL
  2588. };
  2589. static const struct attribute_group pwm_attr_group = {
  2590. .attrs = pwm_attrs,
  2591. };
  2592. static const struct attribute_group lpg_attr_group = {
  2593. .attrs = lpg_attrs,
  2594. };
  2595. static const struct attribute_group blink_attr_group = {
  2596. .attrs = blink_attrs,
  2597. };
  2598. static int __devinit qpnp_flash_init(struct qpnp_led_data *led)
  2599. {
  2600. int rc;
  2601. led->flash_cfg->flash_on = false;
  2602. rc = qpnp_led_masked_write(led,
  2603. FLASH_LED_STROBE_CTRL(led->base),
  2604. FLASH_STROBE_MASK, FLASH_DISABLE_ALL);
  2605. if (rc) {
  2606. dev_err(&led->spmi_dev->dev,
  2607. "LED %d flash write failed(%d)\n", led->id, rc);
  2608. return rc;
  2609. }
  2610. /* Disable flash LED module */
  2611. rc = qpnp_led_masked_write(led, FLASH_ENABLE_CONTROL(led->base),
  2612. FLASH_ENABLE_MASK, FLASH_DISABLE_ALL);
  2613. if (rc) {
  2614. dev_err(&led->spmi_dev->dev,
  2615. "Enable reg write failed(%d)\n", rc);
  2616. return rc;
  2617. }
  2618. if (led->flash_cfg->torch_enable)
  2619. return 0;
  2620. /* Set headroom */
  2621. rc = qpnp_led_masked_write(led, FLASH_HEADROOM(led->base),
  2622. FLASH_HEADROOM_MASK, led->flash_cfg->headroom);
  2623. if (rc) {
  2624. dev_err(&led->spmi_dev->dev,
  2625. "Headroom reg write failed(%d)\n", rc);
  2626. return rc;
  2627. }
  2628. /* Set startup delay */
  2629. rc = qpnp_led_masked_write(led,
  2630. FLASH_STARTUP_DELAY(led->base), FLASH_STARTUP_DLY_MASK,
  2631. led->flash_cfg->startup_dly);
  2632. if (rc) {
  2633. dev_err(&led->spmi_dev->dev,
  2634. "Startup delay reg write failed(%d)\n", rc);
  2635. return rc;
  2636. }
  2637. /* Set timer control - safety or watchdog */
  2638. if (led->flash_cfg->safety_timer) {
  2639. rc = qpnp_led_masked_write(led,
  2640. FLASH_LED_TMR_CTRL(led->base),
  2641. FLASH_TMR_MASK, FLASH_TMR_SAFETY);
  2642. if (rc) {
  2643. dev_err(&led->spmi_dev->dev,
  2644. "LED timer ctrl reg write failed(%d)\n",
  2645. rc);
  2646. return rc;
  2647. }
  2648. }
  2649. /* Set Vreg force */
  2650. rc = qpnp_led_masked_write(led, FLASH_VREG_OK_FORCE(led->base),
  2651. FLASH_VREG_MASK, FLASH_HW_VREG_OK);
  2652. if (rc) {
  2653. dev_err(&led->spmi_dev->dev,
  2654. "Vreg OK reg write failed(%d)\n", rc);
  2655. return rc;
  2656. }
  2657. /* Set self fault check */
  2658. rc = qpnp_led_masked_write(led, FLASH_FAULT_DETECT(led->base),
  2659. FLASH_FAULT_DETECT_MASK, FLASH_SELFCHECK_ENABLE);
  2660. if (rc) {
  2661. dev_err(&led->spmi_dev->dev,
  2662. "Fault detect reg write failed(%d)\n", rc);
  2663. return rc;
  2664. }
  2665. /* Set mask enable */
  2666. rc = qpnp_led_masked_write(led, FLASH_MASK_ENABLE(led->base),
  2667. FLASH_MASK_REG_MASK, FLASH_MASK_1);
  2668. if (rc) {
  2669. dev_err(&led->spmi_dev->dev,
  2670. "Mask enable reg write failed(%d)\n", rc);
  2671. return rc;
  2672. }
  2673. /* Set current ramp */
  2674. rc = qpnp_led_masked_write(led, FLASH_CURRENT_RAMP(led->base),
  2675. FLASH_CURRENT_RAMP_MASK, FLASH_RAMP_STEP_27US);
  2676. if (rc) {
  2677. dev_err(&led->spmi_dev->dev,
  2678. "Current ramp reg write failed(%d)\n", rc);
  2679. return rc;
  2680. }
  2681. led->flash_cfg->strobe_type = 0;
  2682. /* dump flash registers */
  2683. qpnp_dump_regs(led, flash_debug_regs, ARRAY_SIZE(flash_debug_regs));
  2684. return 0;
  2685. }
  2686. static int __devinit qpnp_kpdbl_init(struct qpnp_led_data *led)
  2687. {
  2688. int rc;
  2689. u8 val;
  2690. /* select row source - vbst or vph */
  2691. rc = spmi_ext_register_readl(led->spmi_dev->ctrl, led->spmi_dev->sid,
  2692. KPDBL_ROW_SRC_SEL(led->base), &val, 1);
  2693. if (rc) {
  2694. dev_err(&led->spmi_dev->dev,
  2695. "Unable to read from addr=%x, rc(%d)\n",
  2696. KPDBL_ROW_SRC_SEL(led->base), rc);
  2697. return rc;
  2698. }
  2699. if (led->kpdbl_cfg->row_src_vbst)
  2700. val |= 1 << led->kpdbl_cfg->row_id;
  2701. else
  2702. val &= ~(1 << led->kpdbl_cfg->row_id);
  2703. rc = spmi_ext_register_writel(led->spmi_dev->ctrl, led->spmi_dev->sid,
  2704. KPDBL_ROW_SRC_SEL(led->base), &val, 1);
  2705. if (rc) {
  2706. dev_err(&led->spmi_dev->dev,
  2707. "Unable to read from addr=%x, rc(%d)\n",
  2708. KPDBL_ROW_SRC_SEL(led->base), rc);
  2709. return rc;
  2710. }
  2711. /* row source enable */
  2712. rc = spmi_ext_register_readl(led->spmi_dev->ctrl, led->spmi_dev->sid,
  2713. KPDBL_ROW_SRC(led->base), &val, 1);
  2714. if (rc) {
  2715. dev_err(&led->spmi_dev->dev,
  2716. "Unable to read from addr=%x, rc(%d)\n",
  2717. KPDBL_ROW_SRC(led->base), rc);
  2718. return rc;
  2719. }
  2720. if (led->kpdbl_cfg->row_src_en)
  2721. val |= KPDBL_ROW_SCAN_EN_MASK | (1 << led->kpdbl_cfg->row_id);
  2722. else
  2723. val &= ~(1 << led->kpdbl_cfg->row_id);
  2724. rc = spmi_ext_register_writel(led->spmi_dev->ctrl, led->spmi_dev->sid,
  2725. KPDBL_ROW_SRC(led->base), &val, 1);
  2726. if (rc) {
  2727. dev_err(&led->spmi_dev->dev,
  2728. "Unable to write to addr=%x, rc(%d)\n",
  2729. KPDBL_ROW_SRC(led->base), rc);
  2730. return rc;
  2731. }
  2732. /* enable module */
  2733. rc = qpnp_led_masked_write(led, KPDBL_ENABLE(led->base),
  2734. KPDBL_MODULE_EN_MASK, KPDBL_MODULE_EN);
  2735. if (rc) {
  2736. dev_err(&led->spmi_dev->dev,
  2737. "Enable module write failed(%d)\n", rc);
  2738. return rc;
  2739. }
  2740. rc = qpnp_pwm_init(led->kpdbl_cfg->pwm_cfg, led->spmi_dev,
  2741. led->cdev.name);
  2742. if (rc) {
  2743. dev_err(&led->spmi_dev->dev,
  2744. "Failed to initialize pwm\n");
  2745. return rc;
  2746. }
  2747. if (led->kpdbl_cfg->always_on) {
  2748. kpdbl_master = led->kpdbl_cfg->pwm_cfg->pwm_dev;
  2749. kpdbl_master_period_us = led->kpdbl_cfg->pwm_cfg->pwm_period_us;
  2750. }
  2751. /* dump kpdbl registers */
  2752. qpnp_dump_regs(led, kpdbl_debug_regs, ARRAY_SIZE(kpdbl_debug_regs));
  2753. return 0;
  2754. }
  2755. static int __devinit qpnp_rgb_init(struct qpnp_led_data *led)
  2756. {
  2757. int rc;
  2758. rc = qpnp_led_masked_write(led, RGB_LED_SRC_SEL(led->base),
  2759. RGB_LED_SRC_MASK, RGB_LED_SOURCE_VPH_PWR);
  2760. if (rc) {
  2761. dev_err(&led->spmi_dev->dev,
  2762. "Failed to write led source select register\n");
  2763. return rc;
  2764. }
  2765. rc = qpnp_pwm_init(led->rgb_cfg->pwm_cfg, led->spmi_dev,
  2766. led->cdev.name);
  2767. if (rc) {
  2768. dev_err(&led->spmi_dev->dev,
  2769. "Failed to initialize pwm\n");
  2770. return rc;
  2771. }
  2772. /* Initialize led for use in auto trickle charging mode */
  2773. rc = qpnp_led_masked_write(led, RGB_LED_ATC_CTL(led->base),
  2774. led->rgb_cfg->enable, led->rgb_cfg->enable);
  2775. return 0;
  2776. }
  2777. static int __devinit qpnp_mpp_init(struct qpnp_led_data *led)
  2778. {
  2779. int rc;
  2780. u8 val;
  2781. if (led->max_current < LED_MPP_CURRENT_MIN ||
  2782. led->max_current > LED_MPP_CURRENT_MAX) {
  2783. dev_err(&led->spmi_dev->dev,
  2784. "max current for mpp is not valid\n");
  2785. return -EINVAL;
  2786. }
  2787. val = (led->mpp_cfg->current_setting / LED_MPP_CURRENT_PER_SETTING) - 1;
  2788. if (val < 0)
  2789. val = 0;
  2790. rc = qpnp_led_masked_write(led, LED_MPP_VIN_CTRL(led->base),
  2791. LED_MPP_VIN_MASK, led->mpp_cfg->vin_ctrl);
  2792. if (rc) {
  2793. dev_err(&led->spmi_dev->dev,
  2794. "Failed to write led vin control reg\n");
  2795. return rc;
  2796. }
  2797. rc = qpnp_led_masked_write(led, LED_MPP_SINK_CTRL(led->base),
  2798. LED_MPP_SINK_MASK, val);
  2799. if (rc) {
  2800. dev_err(&led->spmi_dev->dev,
  2801. "Failed to write sink control reg\n");
  2802. return rc;
  2803. }
  2804. if (led->mpp_cfg->pwm_mode != MANUAL_MODE) {
  2805. rc = qpnp_pwm_init(led->mpp_cfg->pwm_cfg, led->spmi_dev,
  2806. led->cdev.name);
  2807. if (rc) {
  2808. dev_err(&led->spmi_dev->dev,
  2809. "Failed to initialize pwm\n");
  2810. return rc;
  2811. }
  2812. }
  2813. return 0;
  2814. }
  2815. static int __devinit qpnp_led_initialize(struct qpnp_led_data *led)
  2816. {
  2817. int rc = 0;
  2818. switch (led->id) {
  2819. case QPNP_ID_WLED:
  2820. rc = qpnp_wled_init(led);
  2821. if (rc)
  2822. dev_err(&led->spmi_dev->dev,
  2823. "WLED initialize failed(%d)\n", rc);
  2824. break;
  2825. case QPNP_ID_FLASH1_LED0:
  2826. case QPNP_ID_FLASH1_LED1:
  2827. rc = qpnp_flash_init(led);
  2828. if (rc)
  2829. dev_err(&led->spmi_dev->dev,
  2830. "FLASH initialize failed(%d)\n", rc);
  2831. break;
  2832. case QPNP_ID_RGB_RED:
  2833. case QPNP_ID_RGB_GREEN:
  2834. case QPNP_ID_RGB_BLUE:
  2835. rc = qpnp_rgb_init(led);
  2836. if (rc)
  2837. dev_err(&led->spmi_dev->dev,
  2838. "RGB initialize failed(%d)\n", rc);
  2839. break;
  2840. case QPNP_ID_LED_MPP:
  2841. rc = qpnp_mpp_init(led);
  2842. if (rc)
  2843. dev_err(&led->spmi_dev->dev,
  2844. "MPP initialize failed(%d)\n", rc);
  2845. break;
  2846. case QPNP_ID_KPDBL:
  2847. rc = qpnp_kpdbl_init(led);
  2848. if (rc)
  2849. dev_err(&led->spmi_dev->dev,
  2850. "KPDBL initialize failed(%d)\n", rc);
  2851. break;
  2852. default:
  2853. dev_err(&led->spmi_dev->dev, "Invalid LED(%d)\n", led->id);
  2854. return -EINVAL;
  2855. }
  2856. return rc;
  2857. }
  2858. static int __devinit qpnp_get_common_configs(struct qpnp_led_data *led,
  2859. struct device_node *node)
  2860. {
  2861. int rc;
  2862. u32 val;
  2863. const char *temp_string;
  2864. led->cdev.default_trigger = LED_TRIGGER_DEFAULT;
  2865. rc = of_property_read_string(node, "linux,default-trigger",
  2866. &temp_string);
  2867. if (!rc)
  2868. led->cdev.default_trigger = temp_string;
  2869. else if (rc != -EINVAL)
  2870. return rc;
  2871. led->default_on = false;
  2872. rc = of_property_read_string(node, "qcom,default-state",
  2873. &temp_string);
  2874. if (!rc) {
  2875. if (strncmp(temp_string, "on", sizeof("on")) == 0)
  2876. led->default_on = true;
  2877. } else if (rc != -EINVAL)
  2878. return rc;
  2879. led->turn_off_delay_ms = 0;
  2880. rc = of_property_read_u32(node, "qcom,turn-off-delay-ms", &val);
  2881. if (!rc)
  2882. led->turn_off_delay_ms = val;
  2883. else if (rc != -EINVAL)
  2884. return rc;
  2885. return 0;
  2886. }
  2887. /*
  2888. * Handlers for alternative sources of platform_data
  2889. */
  2890. static int __devinit qpnp_get_config_wled(struct qpnp_led_data *led,
  2891. struct device_node *node)
  2892. {
  2893. u32 val;
  2894. int rc;
  2895. led->wled_cfg = devm_kzalloc(&led->spmi_dev->dev,
  2896. sizeof(struct wled_config_data), GFP_KERNEL);
  2897. if (!led->wled_cfg) {
  2898. dev_err(&led->spmi_dev->dev, "Unable to allocate memory\n");
  2899. return -ENOMEM;
  2900. }
  2901. rc = spmi_ext_register_readl(led->spmi_dev->ctrl, led->spmi_dev->sid,
  2902. PMIC_VERSION_REG, &led->wled_cfg->pmic_version, 1);
  2903. if (rc) {
  2904. dev_err(&led->spmi_dev->dev,
  2905. "Unable to read pmic ver, rc(%d)\n", rc);
  2906. }
  2907. led->wled_cfg->num_strings = WLED_DEFAULT_STRINGS;
  2908. rc = of_property_read_u32(node, "qcom,num-strings", &val);
  2909. if (!rc)
  2910. led->wled_cfg->num_strings = (u8) val;
  2911. else if (rc != -EINVAL)
  2912. return rc;
  2913. led->wled_cfg->ovp_val = WLED_DEFAULT_OVP_VAL;
  2914. rc = of_property_read_u32(node, "qcom,ovp-val", &val);
  2915. if (!rc)
  2916. led->wled_cfg->ovp_val = (u8) val;
  2917. else if (rc != -EINVAL)
  2918. return rc;
  2919. led->wled_cfg->boost_curr_lim = WLED_BOOST_LIM_DEFAULT;
  2920. rc = of_property_read_u32(node, "qcom,boost-curr-lim", &val);
  2921. if (!rc)
  2922. led->wled_cfg->boost_curr_lim = (u8) val;
  2923. else if (rc != -EINVAL)
  2924. return rc;
  2925. led->wled_cfg->cp_select = WLED_CP_SEL_DEFAULT;
  2926. rc = of_property_read_u32(node, "qcom,cp-sel", &val);
  2927. if (!rc)
  2928. led->wled_cfg->cp_select = (u8) val;
  2929. else if (rc != -EINVAL)
  2930. return rc;
  2931. led->wled_cfg->ctrl_delay_us = WLED_CTRL_DLY_DEFAULT;
  2932. rc = of_property_read_u32(node, "qcom,ctrl-delay-us", &val);
  2933. if (!rc)
  2934. led->wled_cfg->ctrl_delay_us = (u8) val;
  2935. else if (rc != -EINVAL)
  2936. return rc;
  2937. led->wled_cfg->op_fdbck = WLED_OP_FDBCK_DEFAULT;
  2938. rc = of_property_read_u32(node, "qcom,op-fdbck", &val);
  2939. if (!rc)
  2940. led->wled_cfg->op_fdbck = (u8) val;
  2941. else if (rc != -EINVAL)
  2942. return rc;
  2943. led->wled_cfg->switch_freq = WLED_SWITCH_FREQ_DEFAULT;
  2944. rc = of_property_read_u32(node, "qcom,switch-freq", &val);
  2945. if (!rc)
  2946. led->wled_cfg->switch_freq = (u8) val;
  2947. else if (rc != -EINVAL)
  2948. return rc;
  2949. led->wled_cfg->dig_mod_gen_en =
  2950. of_property_read_bool(node, "qcom,dig-mod-gen-en");
  2951. led->wled_cfg->cs_out_en =
  2952. of_property_read_bool(node, "qcom,cs-out-en");
  2953. return 0;
  2954. }
  2955. static int __devinit qpnp_get_config_flash(struct qpnp_led_data *led,
  2956. struct device_node *node, bool *reg_set)
  2957. {
  2958. int rc;
  2959. u32 val;
  2960. led->flash_cfg = devm_kzalloc(&led->spmi_dev->dev,
  2961. sizeof(struct flash_config_data), GFP_KERNEL);
  2962. if (!led->flash_cfg) {
  2963. dev_err(&led->spmi_dev->dev, "Unable to allocate memory\n");
  2964. return -ENOMEM;
  2965. }
  2966. rc = spmi_ext_register_readl(led->spmi_dev->ctrl, led->spmi_dev->sid,
  2967. FLASH_PERIPHERAL_SUBTYPE(led->base),
  2968. &led->flash_cfg->peripheral_subtype, 1);
  2969. if (rc) {
  2970. dev_err(&led->spmi_dev->dev,
  2971. "Unable to read from addr=%x, rc(%d)\n",
  2972. FLASH_PERIPHERAL_SUBTYPE(led->base), rc);
  2973. }
  2974. led->flash_cfg->torch_enable =
  2975. of_property_read_bool(node, "qcom,torch-enable");
  2976. if (of_find_property(of_get_parent(node), "flash-wa-supply",
  2977. NULL) && (!*reg_set)) {
  2978. led->flash_cfg->flash_wa_reg =
  2979. devm_regulator_get(&led->spmi_dev->dev, "flash-wa");
  2980. if (IS_ERR_OR_NULL(led->flash_cfg->flash_wa_reg)) {
  2981. rc = PTR_ERR(led->flash_cfg->flash_wa_reg);
  2982. if (rc != EPROBE_DEFER) {
  2983. dev_err(&led->spmi_dev->dev,
  2984. "Flash wa regulator get failed(%d)\n",
  2985. rc);
  2986. }
  2987. } else
  2988. led->flash_cfg->flash_wa_reg_get = true;
  2989. }
  2990. if (led->id == QPNP_ID_FLASH1_LED0) {
  2991. led->flash_cfg->enable_module = FLASH_ENABLE_LED_0;
  2992. led->flash_cfg->current_addr = FLASH_LED_0_CURR(led->base);
  2993. led->flash_cfg->trigger_flash = FLASH_LED_0_OUTPUT;
  2994. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  2995. if (!*reg_set) {
  2996. led->flash_cfg->flash_boost_reg =
  2997. regulator_get(&led->spmi_dev->dev,
  2998. "flash-boost");
  2999. if (IS_ERR(led->flash_cfg->flash_boost_reg)) {
  3000. rc = PTR_ERR(led->flash_cfg->flash_boost_reg);
  3001. dev_err(&led->spmi_dev->dev,
  3002. "Regulator get failed(%d)\n", rc);
  3003. goto error_get_flash_reg;
  3004. }
  3005. led->flash_cfg->flash_reg_get = true;
  3006. *reg_set = true;
  3007. } else
  3008. led->flash_cfg->flash_reg_get = false;
  3009. #endif
  3010. if (led->flash_cfg->torch_enable) {
  3011. led->flash_cfg->second_addr =
  3012. FLASH_LED_1_CURR(led->base);
  3013. }
  3014. } else if (led->id == QPNP_ID_FLASH1_LED1) {
  3015. led->flash_cfg->enable_module = FLASH_ENABLE_LED_1;
  3016. led->flash_cfg->current_addr = FLASH_LED_1_CURR(led->base);
  3017. led->flash_cfg->trigger_flash = FLASH_LED_1_OUTPUT;
  3018. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  3019. if (!*reg_set) {
  3020. led->flash_cfg->flash_boost_reg =
  3021. regulator_get(&led->spmi_dev->dev,
  3022. "flash-boost");
  3023. if (IS_ERR(led->flash_cfg->flash_boost_reg)) {
  3024. rc = PTR_ERR(led->flash_cfg->flash_boost_reg);
  3025. dev_err(&led->spmi_dev->dev,
  3026. "Regulator get failed(%d)\n", rc);
  3027. goto error_get_flash_reg;
  3028. }
  3029. led->flash_cfg->flash_reg_get = true;
  3030. *reg_set = true;
  3031. } else
  3032. led->flash_cfg->flash_reg_get = false;
  3033. #endif
  3034. if (led->flash_cfg->torch_enable) {
  3035. led->flash_cfg->second_addr =
  3036. FLASH_LED_0_CURR(led->base);
  3037. }
  3038. } else {
  3039. dev_err(&led->spmi_dev->dev, "Unknown flash LED name given\n");
  3040. return -EINVAL;
  3041. }
  3042. if (led->flash_cfg->torch_enable) {
  3043. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  3044. if (of_find_property(of_get_parent(node), "torch-boost-supply",
  3045. NULL)) {
  3046. led->flash_cfg->torch_boost_reg =
  3047. regulator_get(&led->spmi_dev->dev,
  3048. "torch-boost");
  3049. if (IS_ERR(led->flash_cfg->torch_boost_reg)) {
  3050. rc = PTR_ERR(led->flash_cfg->torch_boost_reg);
  3051. dev_err(&led->spmi_dev->dev,
  3052. "Torch regulator get failed(%d)\n", rc);
  3053. goto error_get_torch_reg;
  3054. }
  3055. #endif
  3056. led->flash_cfg->enable_module = FLASH_ENABLE_MODULE;
  3057. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  3058. } else
  3059. #endif
  3060. led->flash_cfg->enable_module = FLASH_ENABLE_ALL;
  3061. led->flash_cfg->trigger_flash = FLASH_TORCH_OUTPUT;
  3062. }
  3063. rc = of_property_read_u32(node, "qcom,current", &val);
  3064. if (!rc) {
  3065. if (led->flash_cfg->torch_enable) {
  3066. led->flash_cfg->current_prgm = (val *
  3067. TORCH_MAX_LEVEL / led->max_current);
  3068. return 0;
  3069. }
  3070. else
  3071. led->flash_cfg->current_prgm = (val *
  3072. FLASH_MAX_LEVEL / led->max_current);
  3073. } else
  3074. if (led->flash_cfg->torch_enable)
  3075. goto error_get_torch_reg;
  3076. else
  3077. goto error_get_flash_reg;
  3078. rc = of_property_read_u32(node, "qcom,headroom", &val);
  3079. if (!rc)
  3080. led->flash_cfg->headroom = (u8) val;
  3081. else if (rc == -EINVAL)
  3082. led->flash_cfg->headroom = HEADROOM_500mV;
  3083. else
  3084. goto error_get_flash_reg;
  3085. rc = of_property_read_u32(node, "qcom,duration", &val);
  3086. if (!rc)
  3087. led->flash_cfg->duration = (u8)((val - 10) / 10);
  3088. else if (rc == -EINVAL)
  3089. led->flash_cfg->duration = FLASH_DURATION_200ms;
  3090. else
  3091. goto error_get_flash_reg;
  3092. rc = of_property_read_u32(node, "qcom,clamp-curr", &val);
  3093. if (!rc)
  3094. led->flash_cfg->clamp_curr = (val *
  3095. FLASH_MAX_LEVEL / led->max_current);
  3096. else if (rc == -EINVAL)
  3097. led->flash_cfg->clamp_curr = FLASH_CLAMP_200mA;
  3098. else
  3099. goto error_get_flash_reg;
  3100. rc = of_property_read_u32(node, "qcom,startup-dly", &val);
  3101. if (!rc)
  3102. led->flash_cfg->startup_dly = (u8) val;
  3103. else if (rc == -EINVAL)
  3104. led->flash_cfg->startup_dly = DELAY_128us;
  3105. else
  3106. goto error_get_flash_reg;
  3107. led->flash_cfg->safety_timer =
  3108. of_property_read_bool(node, "qcom,safety-timer");
  3109. return 0;
  3110. error_get_torch_reg:
  3111. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  3112. regulator_put(led->flash_cfg->torch_boost_reg);
  3113. #endif
  3114. error_get_flash_reg:
  3115. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  3116. regulator_put(led->flash_cfg->flash_boost_reg);
  3117. #endif
  3118. return rc;
  3119. }
  3120. static int __devinit qpnp_get_config_pwm(struct pwm_config_data *pwm_cfg,
  3121. struct spmi_device *spmi_dev,
  3122. struct device_node *node)
  3123. {
  3124. struct property *prop;
  3125. int rc, i, lut_max_size;
  3126. u32 val;
  3127. u8 *temp_cfg;
  3128. const char *led_label;
  3129. rc = of_property_read_u32(node, "qcom,pwm-channel", &val);
  3130. if (!rc)
  3131. pwm_cfg->pwm_channel = val;
  3132. else
  3133. return rc;
  3134. if (pwm_cfg->mode != MANUAL_MODE) {
  3135. rc = of_property_read_u32(node, "qcom,pwm-us", &val);
  3136. if (!rc)
  3137. pwm_cfg->pwm_period_us = val;
  3138. else
  3139. return rc;
  3140. }
  3141. pwm_cfg->use_blink =
  3142. of_property_read_bool(node, "qcom,use-blink");
  3143. if (pwm_cfg->mode == LPG_MODE || pwm_cfg->use_blink) {
  3144. pwm_cfg->duty_cycles =
  3145. devm_kzalloc(&spmi_dev->dev,
  3146. sizeof(struct pwm_duty_cycles), GFP_KERNEL);
  3147. if (!pwm_cfg->duty_cycles) {
  3148. dev_err(&spmi_dev->dev,
  3149. "Unable to allocate memory\n");
  3150. rc = -ENOMEM;
  3151. goto bad_lpg_params;
  3152. }
  3153. prop = of_find_property(node, "qcom,duty-pcts",
  3154. &pwm_cfg->duty_cycles->num_duty_pcts);
  3155. if (!prop) {
  3156. dev_err(&spmi_dev->dev, "Looking up property " \
  3157. "node qcom,duty-pcts failed\n");
  3158. rc = -ENODEV;
  3159. goto bad_lpg_params;
  3160. } else if (!pwm_cfg->duty_cycles->num_duty_pcts) {
  3161. dev_err(&spmi_dev->dev, "Invalid length of " \
  3162. "duty pcts\n");
  3163. rc = -EINVAL;
  3164. goto bad_lpg_params;
  3165. }
  3166. rc = of_property_read_string(node, "label", &led_label);
  3167. if (rc < 0) {
  3168. dev_err(&spmi_dev->dev,
  3169. "Failure reading label, rc = %d\n", rc);
  3170. return rc;
  3171. }
  3172. if (strcmp(led_label, "kpdbl") == 0)
  3173. lut_max_size = PWM_GPLED_LUT_MAX_SIZE;
  3174. else
  3175. lut_max_size = PWM_LUT_MAX_SIZE;
  3176. pwm_cfg->duty_cycles->duty_pcts =
  3177. devm_kzalloc(&spmi_dev->dev,
  3178. sizeof(int) * lut_max_size,
  3179. GFP_KERNEL);
  3180. if (!pwm_cfg->duty_cycles->duty_pcts) {
  3181. dev_err(&spmi_dev->dev,
  3182. "Unable to allocate memory\n");
  3183. rc = -ENOMEM;
  3184. goto bad_lpg_params;
  3185. }
  3186. pwm_cfg->old_duty_pcts =
  3187. devm_kzalloc(&spmi_dev->dev,
  3188. sizeof(int) * lut_max_size,
  3189. GFP_KERNEL);
  3190. if (!pwm_cfg->old_duty_pcts) {
  3191. dev_err(&spmi_dev->dev,
  3192. "Unable to allocate memory\n");
  3193. rc = -ENOMEM;
  3194. goto bad_lpg_params;
  3195. }
  3196. temp_cfg = devm_kzalloc(&spmi_dev->dev,
  3197. pwm_cfg->duty_cycles->num_duty_pcts *
  3198. sizeof(u8), GFP_KERNEL);
  3199. if (!temp_cfg) {
  3200. dev_err(&spmi_dev->dev, "Failed to allocate " \
  3201. "memory for duty pcts\n");
  3202. rc = -ENOMEM;
  3203. goto bad_lpg_params;
  3204. }
  3205. memcpy(temp_cfg, prop->value,
  3206. pwm_cfg->duty_cycles->num_duty_pcts);
  3207. for (i = 0; i < pwm_cfg->duty_cycles->num_duty_pcts; i++)
  3208. pwm_cfg->duty_cycles->duty_pcts[i] =
  3209. (int) temp_cfg[i];
  3210. rc = of_property_read_u32(node, "qcom,start-idx", &val);
  3211. if (!rc) {
  3212. pwm_cfg->lut_params.start_idx = val;
  3213. pwm_cfg->duty_cycles->start_idx = val;
  3214. } else
  3215. goto bad_lpg_params;
  3216. pwm_cfg->lut_params.lut_pause_hi = 0;
  3217. rc = of_property_read_u32(node, "qcom,pause-hi", &val);
  3218. if (!rc)
  3219. pwm_cfg->lut_params.lut_pause_hi = val;
  3220. else if (rc != -EINVAL)
  3221. goto bad_lpg_params;
  3222. pwm_cfg->lut_params.lut_pause_lo = 0;
  3223. rc = of_property_read_u32(node, "qcom,pause-lo", &val);
  3224. if (!rc)
  3225. pwm_cfg->lut_params.lut_pause_lo = val;
  3226. else if (rc != -EINVAL)
  3227. goto bad_lpg_params;
  3228. pwm_cfg->lut_params.ramp_step_ms =
  3229. QPNP_LUT_RAMP_STEP_DEFAULT;
  3230. rc = of_property_read_u32(node, "qcom,ramp-step-ms", &val);
  3231. if (!rc)
  3232. pwm_cfg->lut_params.ramp_step_ms = val;
  3233. else if (rc != -EINVAL)
  3234. goto bad_lpg_params;
  3235. pwm_cfg->lut_params.flags = QPNP_LED_PWM_FLAGS;
  3236. rc = of_property_read_u32(node, "qcom,lut-flags", &val);
  3237. if (!rc)
  3238. pwm_cfg->lut_params.flags = (u8) val;
  3239. else if (rc != -EINVAL)
  3240. goto bad_lpg_params;
  3241. pwm_cfg->lut_params.idx_len =
  3242. pwm_cfg->duty_cycles->num_duty_pcts;
  3243. }
  3244. return 0;
  3245. bad_lpg_params:
  3246. pwm_cfg->use_blink = false;
  3247. if (pwm_cfg->mode == PWM_MODE) {
  3248. dev_err(&spmi_dev->dev, "LPG parameters not set for" \
  3249. " blink mode, defaulting to PWM mode\n");
  3250. return 0;
  3251. }
  3252. return rc;
  3253. };
  3254. static int qpnp_led_get_mode(const char *mode)
  3255. {
  3256. if (strncmp(mode, "manual", strlen(mode)) == 0)
  3257. return MANUAL_MODE;
  3258. else if (strncmp(mode, "pwm", strlen(mode)) == 0)
  3259. return PWM_MODE;
  3260. else if (strncmp(mode, "lpg", strlen(mode)) == 0)
  3261. return LPG_MODE;
  3262. else
  3263. return -EINVAL;
  3264. };
  3265. static int __devinit qpnp_get_config_kpdbl(struct qpnp_led_data *led,
  3266. struct device_node *node)
  3267. {
  3268. int rc;
  3269. u32 val;
  3270. u8 led_mode;
  3271. const char *mode;
  3272. led->kpdbl_cfg = devm_kzalloc(&led->spmi_dev->dev,
  3273. sizeof(struct kpdbl_config_data), GFP_KERNEL);
  3274. if (!led->kpdbl_cfg) {
  3275. dev_err(&led->spmi_dev->dev, "Unable to allocate memory\n");
  3276. return -ENOMEM;
  3277. }
  3278. rc = of_property_read_string(node, "qcom,mode", &mode);
  3279. if (!rc) {
  3280. led_mode = qpnp_led_get_mode(mode);
  3281. if ((led_mode == MANUAL_MODE) || (led_mode == -EINVAL)) {
  3282. dev_err(&led->spmi_dev->dev, "Selected mode not " \
  3283. "supported for kpdbl.\n");
  3284. return -EINVAL;
  3285. }
  3286. led->kpdbl_cfg->pwm_cfg = devm_kzalloc(&led->spmi_dev->dev,
  3287. sizeof(struct pwm_config_data),
  3288. GFP_KERNEL);
  3289. if (!led->kpdbl_cfg->pwm_cfg) {
  3290. dev_err(&led->spmi_dev->dev,
  3291. "Unable to allocate memory\n");
  3292. return -ENOMEM;
  3293. }
  3294. led->kpdbl_cfg->pwm_cfg->mode = led_mode;
  3295. led->kpdbl_cfg->pwm_cfg->default_mode = led_mode;
  3296. } else
  3297. return rc;
  3298. rc = qpnp_get_config_pwm(led->kpdbl_cfg->pwm_cfg, led->spmi_dev, node);
  3299. if (rc < 0)
  3300. return rc;
  3301. rc = of_property_read_u32(node, "qcom,row-id", &val);
  3302. if (!rc)
  3303. led->kpdbl_cfg->row_id = val;
  3304. else
  3305. return rc;
  3306. led->kpdbl_cfg->row_src_vbst =
  3307. of_property_read_bool(node, "qcom,row-src-vbst");
  3308. led->kpdbl_cfg->row_src_en =
  3309. of_property_read_bool(node, "qcom,row-src-en");
  3310. led->kpdbl_cfg->always_on =
  3311. of_property_read_bool(node, "qcom,always-on");
  3312. return 0;
  3313. }
  3314. static int __devinit qpnp_get_config_rgb(struct qpnp_led_data *led,
  3315. struct device_node *node)
  3316. {
  3317. int rc;
  3318. u8 led_mode;
  3319. const char *mode;
  3320. led->rgb_cfg = devm_kzalloc(&led->spmi_dev->dev,
  3321. sizeof(struct rgb_config_data), GFP_KERNEL);
  3322. if (!led->rgb_cfg) {
  3323. dev_err(&led->spmi_dev->dev, "Unable to allocate memory\n");
  3324. return -ENOMEM;
  3325. }
  3326. if (led->id == QPNP_ID_RGB_RED)
  3327. led->rgb_cfg->enable = RGB_LED_ENABLE_RED;
  3328. else if (led->id == QPNP_ID_RGB_GREEN)
  3329. led->rgb_cfg->enable = RGB_LED_ENABLE_GREEN;
  3330. else if (led->id == QPNP_ID_RGB_BLUE)
  3331. led->rgb_cfg->enable = RGB_LED_ENABLE_BLUE;
  3332. else
  3333. return -EINVAL;
  3334. rc = of_property_read_string(node, "qcom,mode", &mode);
  3335. if (!rc) {
  3336. led_mode = qpnp_led_get_mode(mode);
  3337. if ((led_mode == MANUAL_MODE) || (led_mode == -EINVAL)) {
  3338. dev_err(&led->spmi_dev->dev, "Selected mode not " \
  3339. "supported for rgb.\n");
  3340. return -EINVAL;
  3341. }
  3342. led->rgb_cfg->pwm_cfg = devm_kzalloc(&led->spmi_dev->dev,
  3343. sizeof(struct pwm_config_data),
  3344. GFP_KERNEL);
  3345. if (!led->rgb_cfg->pwm_cfg) {
  3346. dev_err(&led->spmi_dev->dev,
  3347. "Unable to allocate memory\n");
  3348. return -ENOMEM;
  3349. }
  3350. led->rgb_cfg->pwm_cfg->mode = led_mode;
  3351. led->rgb_cfg->pwm_cfg->default_mode = led_mode;
  3352. } else
  3353. return rc;
  3354. rc = qpnp_get_config_pwm(led->rgb_cfg->pwm_cfg, led->spmi_dev, node);
  3355. if (rc < 0)
  3356. return rc;
  3357. return 0;
  3358. }
  3359. static int __devinit qpnp_get_config_mpp(struct qpnp_led_data *led,
  3360. struct device_node *node)
  3361. {
  3362. int rc;
  3363. u32 val;
  3364. u8 led_mode;
  3365. const char *mode;
  3366. led->mpp_cfg = devm_kzalloc(&led->spmi_dev->dev,
  3367. sizeof(struct mpp_config_data), GFP_KERNEL);
  3368. if (!led->mpp_cfg) {
  3369. dev_err(&led->spmi_dev->dev, "Unable to allocate memory\n");
  3370. return -ENOMEM;
  3371. }
  3372. led->mpp_cfg->current_setting = LED_MPP_CURRENT_MIN;
  3373. rc = of_property_read_u32(node, "qcom,current-setting", &val);
  3374. if (!rc) {
  3375. if (led->mpp_cfg->current_setting < LED_MPP_CURRENT_MIN)
  3376. led->mpp_cfg->current_setting = LED_MPP_CURRENT_MIN;
  3377. else if (led->mpp_cfg->current_setting > LED_MPP_CURRENT_MAX)
  3378. led->mpp_cfg->current_setting = LED_MPP_CURRENT_MAX;
  3379. else
  3380. led->mpp_cfg->current_setting = (u8) val;
  3381. } else if (rc != -EINVAL)
  3382. return rc;
  3383. led->mpp_cfg->source_sel = LED_MPP_SOURCE_SEL_DEFAULT;
  3384. rc = of_property_read_u32(node, "qcom,source-sel", &val);
  3385. if (!rc)
  3386. led->mpp_cfg->source_sel = (u8) val;
  3387. else if (rc != -EINVAL)
  3388. return rc;
  3389. led->mpp_cfg->mode_ctrl = LED_MPP_MODE_SINK;
  3390. rc = of_property_read_u32(node, "qcom,mode-ctrl", &val);
  3391. if (!rc)
  3392. led->mpp_cfg->mode_ctrl = (u8) val;
  3393. else if (rc != -EINVAL)
  3394. return rc;
  3395. led->mpp_cfg->vin_ctrl = LED_MPP_VIN_CTRL_DEFAULT;
  3396. rc = of_property_read_u32(node, "qcom,vin-ctrl", &val);
  3397. if (!rc)
  3398. led->mpp_cfg->vin_ctrl = (u8) val;
  3399. else if (rc != -EINVAL)
  3400. return rc;
  3401. led->mpp_cfg->min_brightness = 0;
  3402. rc = of_property_read_u32(node, "qcom,min-brightness", &val);
  3403. if (!rc)
  3404. led->mpp_cfg->min_brightness = (u8) val;
  3405. else if (rc != -EINVAL)
  3406. return rc;
  3407. rc = of_property_read_string(node, "qcom,mode", &mode);
  3408. if (!rc) {
  3409. led_mode = qpnp_led_get_mode(mode);
  3410. led->mpp_cfg->pwm_mode = led_mode;
  3411. if (led_mode == MANUAL_MODE)
  3412. return MANUAL_MODE;
  3413. else if (led_mode == -EINVAL) {
  3414. dev_err(&led->spmi_dev->dev, "Selected mode not " \
  3415. "supported for mpp.\n");
  3416. return -EINVAL;
  3417. }
  3418. led->mpp_cfg->pwm_cfg = devm_kzalloc(&led->spmi_dev->dev,
  3419. sizeof(struct pwm_config_data),
  3420. GFP_KERNEL);
  3421. if (!led->mpp_cfg->pwm_cfg) {
  3422. dev_err(&led->spmi_dev->dev,
  3423. "Unable to allocate memory\n");
  3424. return -ENOMEM;
  3425. }
  3426. led->mpp_cfg->pwm_cfg->mode = led_mode;
  3427. led->mpp_cfg->pwm_cfg->default_mode = led_mode;
  3428. } else
  3429. return rc;
  3430. rc = qpnp_get_config_pwm(led->mpp_cfg->pwm_cfg, led->spmi_dev, node);
  3431. if (rc < 0)
  3432. return rc;
  3433. return 0;
  3434. }
  3435. #ifdef SAMSUNG_LED_PATTERN
  3436. /* Pattern Start*/
  3437. static void samsung_led_set(struct qpnp_led_data *info,
  3438. enum led_brightness value){
  3439. int rc;
  3440. info->cdev.brightness = value;
  3441. switch (info->id) {
  3442. case QPNP_ID_RGB_RED:
  3443. case QPNP_ID_RGB_GREEN:
  3444. case QPNP_ID_RGB_BLUE:
  3445. rc = qpnp_rgb_set(info);
  3446. if (rc < 0)
  3447. dev_err(&info->spmi_dev->dev,"RGB set brightness failed (%d)\n", rc);
  3448. break;
  3449. }
  3450. }
  3451. static void led_pat_on(struct qpnp_led_data *info, struct patt_registry *patt_register,int brightness){
  3452. struct patt_config *patt_led;
  3453. int cnt,i;
  3454. int ret;
  3455. int led_num = 0;
  3456. for(cnt = 0;cnt < patt_register->len; cnt++){
  3457. patt_led = &(patt_register->patt[cnt]);
  3458. led_num = patt_led->id - 3;
  3459. if(info[led_num].id == patt_led->id){
  3460. if(info[led_num].rgb_cfg->pwm_cfg->duty_cycles->duty_pcts){
  3461. devm_kfree(&(info[led_num].spmi_dev->dev),info[led_num].rgb_cfg->pwm_cfg->duty_cycles->duty_pcts);
  3462. info[led_num].rgb_cfg->pwm_cfg->duty_cycles->duty_pcts = devm_kzalloc(&(info[led_num].spmi_dev->dev),
  3463. sizeof(int) * patt_led->num_duty_pcts,GFP_KERNEL);
  3464. for(i = 0;i < patt_led->num_duty_pcts; i++){
  3465. if(low_powermode && patt_led->low_pow_duty_pcts)
  3466. info[led_num].rgb_cfg->pwm_cfg->duty_cycles->duty_pcts[i] = (int) ((patt_led->low_pow_duty_pcts[i]*info[led_num].max_current)/CURRENT_DIVIDER);
  3467. else
  3468. info[led_num].rgb_cfg->pwm_cfg->duty_cycles->duty_pcts[i] = (int) ((patt_led->duty_pcts[i]*info[led_num].max_current)/CURRENT_DIVIDER);
  3469. }
  3470. }
  3471. info[led_num].rgb_cfg->pwm_cfg->pwm_period_us = patt_led->pwm_period_us;
  3472. info[led_num].rgb_cfg->pwm_cfg->lut_params.lut_pause_hi = patt_led->hi_pause;
  3473. info[led_num].rgb_cfg->pwm_cfg->lut_params.lut_pause_lo = patt_led->lo_pause;
  3474. if(!cnt)
  3475. info[led_num].rgb_cfg->pwm_cfg->lut_params.start_idx = 0;
  3476. else
  3477. info[led_num].rgb_cfg->pwm_cfg->lut_params.start_idx = info[led_num-1].rgb_cfg->pwm_cfg->lut_params.start_idx + patt_register->patt[led_num-1].num_duty_pcts;
  3478. info[led_num].rgb_cfg->pwm_cfg->lut_params.idx_len = patt_led->num_duty_pcts;
  3479. info[led_num].rgb_cfg->pwm_cfg->lut_params.ramp_step_ms = patt_led->ramp_step_ms;
  3480. ret = pwm_lut_config(info[led_num].rgb_cfg->pwm_cfg->pwm_dev,
  3481. info[led_num].rgb_cfg->pwm_cfg->pwm_period_us,
  3482. info[led_num].rgb_cfg->pwm_cfg->duty_cycles->duty_pcts,
  3483. info[led_num].rgb_cfg->pwm_cfg->lut_params);
  3484. if (ret < 0) {
  3485. dev_err(&(info[led_num].spmi_dev->dev), "Failed to configure pwm LUT\n");
  3486. return ;
  3487. }
  3488. }
  3489. }
  3490. for(cnt = 0;cnt < patt_register->len; cnt++){
  3491. patt_led = &(patt_register->patt[cnt]);
  3492. led_num = patt_led->id - 3;
  3493. samsung_led_set(&info[led_num], brightness);
  3494. }
  3495. }
  3496. static ssize_t store_patt(struct device *dev, struct device_attribute *devattr,
  3497. const char *buf, size_t count){
  3498. struct qpnp_led_data *info;
  3499. struct patt_registry *pat_reg = NULL;
  3500. int cnt,i;
  3501. info = dev_get_drvdata(dev);
  3502. if(buf[0] == '0'){
  3503. on_patt = 0;
  3504. for(cnt = 0; cnt < RGB_MAX; cnt++)
  3505. samsung_led_set(&info[cnt],0);
  3506. return count;
  3507. }
  3508. cnt= buf[0]-'0';
  3509. for(i = 0; i < RGB_MAX; i++)
  3510. samsung_led_set(&info[i],LED_OFF);
  3511. if(cnt == LED_POWERING_ON_PAT){
  3512. info[RGB_GREEN].rgb_cfg->enable = RGB_LED_ENABLE_GREEN | RGB_LED_ENABLE_BLUE;
  3513. info[RGB_BLUE].rgb_cfg->enable = RGB_LED_ENABLE_GREEN | RGB_LED_ENABLE_BLUE;
  3514. printk(KERN_INFO "Green Enable =%x \n", info[RGB_GREEN].rgb_cfg->enable);
  3515. printk(KERN_INFO "Blue Enable =%x \n", info[RGB_BLUE].rgb_cfg->enable);
  3516. }
  3517. else {
  3518. info[RGB_RED].rgb_cfg->enable = RGB_LED_ENABLE_RED;
  3519. info[RGB_GREEN].rgb_cfg->enable = RGB_LED_ENABLE_GREEN;
  3520. info[RGB_BLUE].rgb_cfg->enable = RGB_LED_ENABLE_BLUE;
  3521. }
  3522. if(cnt < LED_PATTERN_MAX){
  3523. mutex_lock(&leds_mutex_lock);
  3524. on_patt = cnt; //saving pattern number
  3525. pat_reg = &led_patterns[cnt-1];
  3526. printk(KERN_INFO "store_patt: Pattern %d will swtiched on\n",cnt);
  3527. led_pat_on(info, pat_reg,LED_FULL);
  3528. mutex_unlock(&leds_mutex_lock);
  3529. }
  3530. return count;
  3531. }
  3532. static ssize_t show_patt(struct device *dev,
  3533. struct device_attribute *attr, char *buf){
  3534. return snprintf(buf, 4, "%d\n", on_patt);
  3535. }
  3536. static ssize_t led_r_store(struct device *dev, struct device_attribute *devattr,
  3537. const char *buf, size_t count){
  3538. struct qpnp_led_data *info;
  3539. int ret,i;
  3540. long brightness = 0;
  3541. struct patt_registry *pat_reg = NULL;
  3542. printk(KERN_INFO "led_r_store called\n");
  3543. info = dev_get_drvdata(dev);
  3544. ret = kstrtol(buf, 10, &brightness);
  3545. if(ret || ret < 0){
  3546. printk(KERN_INFO "[LED] Led_r cant set brightness \n");
  3547. return count;
  3548. }
  3549. if(brightness < 0 || brightness > LED_FULL){
  3550. printk(KERN_INFO "[LED] Led_r brightness is out of range(0-255)\n");
  3551. return count;
  3552. }
  3553. for(i = 0; i < RGB_MAX; i++)
  3554. samsung_led_set(&info[i],LED_OFF);
  3555. mutex_lock(&leds_mutex_lock);
  3556. info[RGB_RED].rgb_cfg->enable = RGB_LED_ENABLE_RED;
  3557. pat_reg = &led_rgb[RGB_RED];
  3558. for(i = 0; i< pat_reg->patt->num_duty_pcts; i++)
  3559. pat_reg->patt->duty_pcts[i] = (brightness * 100)/RGB_LED_MAX_BRIGHTNESS;
  3560. led_pat_on(info, pat_reg,brightness);
  3561. mutex_unlock(&leds_mutex_lock);
  3562. return count;
  3563. }
  3564. static ssize_t led_r_show(struct device *dev,
  3565. struct device_attribute *attr, char *buf)
  3566. {
  3567. struct qpnp_led_data *led_array;
  3568. led_array = dev_get_drvdata(dev);
  3569. return snprintf(buf, 4, "%d\n", led_array[RGB_RED].cdev.brightness );
  3570. }
  3571. static ssize_t led_g_store(struct device *dev, struct device_attribute *devattr,
  3572. const char *buf, size_t count){
  3573. struct qpnp_led_data *info;
  3574. int ret,i;
  3575. long brightness = 0;
  3576. struct patt_registry *pat_reg = NULL;
  3577. printk(KERN_INFO "led_g_store called\n");
  3578. info = dev_get_drvdata(dev);
  3579. ret = kstrtol(buf, 10, &brightness);
  3580. if(ret || ret < 0){
  3581. printk(KERN_INFO "[LED] Led_g cant set brightness \n");
  3582. return count;
  3583. }
  3584. if(brightness < 0 || brightness > LED_FULL){
  3585. printk(KERN_INFO "[LED] Led_g brightness is out of range(0-255)\n");
  3586. return count;
  3587. }
  3588. for(i = 0; i < RGB_MAX; i++)
  3589. samsung_led_set(&info[i],LED_OFF);
  3590. mutex_lock(&leds_mutex_lock);
  3591. info[RGB_GREEN].rgb_cfg->enable = RGB_LED_ENABLE_GREEN;
  3592. pat_reg = &led_rgb[RGB_GREEN];
  3593. for(i = 0; i< pat_reg->patt->num_duty_pcts; i++)
  3594. pat_reg->patt->duty_pcts[i] = (brightness * 100)/RGB_LED_MAX_BRIGHTNESS;
  3595. led_pat_on(info, pat_reg, brightness);
  3596. mutex_unlock(&leds_mutex_lock);
  3597. return count;
  3598. }
  3599. static ssize_t led_g_show(struct device *dev,
  3600. struct device_attribute *attr, char *buf)
  3601. {
  3602. struct qpnp_led_data *led_array;
  3603. led_array = dev_get_drvdata(dev);
  3604. return snprintf(buf, 4, "%d\n", led_array[RGB_GREEN].cdev.brightness );
  3605. }
  3606. static ssize_t led_b_store(struct device *dev, struct device_attribute *devattr,
  3607. const char *buf, size_t count){
  3608. struct qpnp_led_data *info;
  3609. int ret,i;
  3610. long brightness = 0;
  3611. struct patt_registry *pat_reg = NULL;
  3612. printk(KERN_INFO "led_b_store called\n");
  3613. info = dev_get_drvdata(dev);
  3614. ret = kstrtol(buf, 10, &brightness);
  3615. if(ret || ret < 0){
  3616. printk(KERN_INFO "[LED] Led_b cant set brightness \n");
  3617. return count;
  3618. }
  3619. if(brightness < 0 || brightness > LED_FULL){
  3620. printk(KERN_INFO "[LED] Led_b brightness is out of range(0-255)\n");
  3621. return count;
  3622. }
  3623. for(i = 0; i < RGB_MAX; i++)
  3624. samsung_led_set(&info[i],LED_OFF);
  3625. mutex_lock(&leds_mutex_lock);
  3626. info[RGB_BLUE].rgb_cfg->enable = RGB_LED_ENABLE_BLUE;
  3627. pat_reg = &led_rgb[RGB_BLUE];
  3628. for(i = 0; i< pat_reg->patt->num_duty_pcts; i++)
  3629. pat_reg->patt->duty_pcts[i] = (brightness * 100)/RGB_LED_MAX_BRIGHTNESS;
  3630. led_pat_on(info, pat_reg,brightness);
  3631. mutex_unlock(&leds_mutex_lock);
  3632. return count;
  3633. }
  3634. static ssize_t led_b_show(struct device *dev,
  3635. struct device_attribute *attr, char *buf)
  3636. {
  3637. struct qpnp_led_data *led_array;
  3638. led_array = dev_get_drvdata(dev);
  3639. return snprintf(buf, 4, "%d\n", led_array[RGB_BLUE].cdev.brightness );
  3640. }
  3641. static unsigned int hex_to_dec(char c1, char c2)
  3642. {
  3643. unsigned int ret_val = 0;
  3644. if (c1 >= '0' && c1 <= '9')
  3645. ret_val = (c1 - '0');
  3646. else if (c1 >= 'A' && c1 <= 'F')
  3647. ret_val = (c1 - 'A') + 10;
  3648. else if (c1 >= 'a' && c1 <= 'f')
  3649. ret_val = (c1 - 'a') + 10;
  3650. ret_val = ret_val * 16;
  3651. if (c2 >= '0' && c2 <= '9')
  3652. ret_val += (c2 - '0');
  3653. else if (c2 >= 'A' && c2 <= 'F')
  3654. ret_val += (c2 - 'A') + 10;
  3655. else if (c2 >= 'a' && c2 <= 'f')
  3656. ret_val += (c2 - 'a') + 10;
  3657. return ret_val;
  3658. }
  3659. static ssize_t led_blink_store(struct device *dev, struct device_attribute *devattr,
  3660. const char *buf, size_t size){
  3661. struct qpnp_led_data *info = dev_get_drvdata(dev);
  3662. unsigned int brightness_r = 0;
  3663. unsigned int brightness_g = 0;
  3664. unsigned int brightness_b = 0;
  3665. unsigned int loop_cnt = 0;
  3666. unsigned int delayon = 0;
  3667. unsigned int delayoff = 0;
  3668. unsigned int argb_count = 0;
  3669. struct patt_registry *pat_reg = NULL;
  3670. unsigned int cnt = 0;
  3671. u8 val = 0;
  3672. int i,j;
  3673. printk(KERN_INFO "led_blink_store called\n");
  3674. if (size < 7) {
  3675. printk(KERN_DEBUG "led_blink: Invlid input\n");
  3676. return size;
  3677. }
  3678. if (buf[8] == ' ') { /*case of RGB delay_on delay_off*/
  3679. for (loop_cnt = 9; loop_cnt < size-1; loop_cnt++) {
  3680. delayon = delayon*10 + (buf[loop_cnt] - '0');
  3681. if (buf[loop_cnt+1] == ' ') {
  3682. loop_cnt += 2;
  3683. break;
  3684. }
  3685. }
  3686. for (; loop_cnt < size-1; loop_cnt++)
  3687. delayoff = delayoff*10 + (buf[loop_cnt] - '0');
  3688. }
  3689. else if (buf[10] == ' ') { /*case of ARGB delay_on delay_off*/
  3690. argb_count = 1;
  3691. for (loop_cnt = 11; loop_cnt < size-1; loop_cnt++) {
  3692. delayon = delayon*10 + (buf[loop_cnt] - '0');
  3693. if (buf[loop_cnt+1] == ' ') {
  3694. loop_cnt += 2;
  3695. break;
  3696. }
  3697. }
  3698. for (; loop_cnt < size-1; loop_cnt++)
  3699. delayoff = delayoff*10 + (buf[loop_cnt] - '0');
  3700. }
  3701. else if (size > 9) { /*case of ARGB*/
  3702. argb_count = 1;
  3703. }
  3704. if (!argb_count) {
  3705. brightness_r = hex_to_dec(buf[2], buf[3]);
  3706. brightness_g = hex_to_dec(buf[4], buf[5]);
  3707. brightness_b = hex_to_dec(buf[6], buf[7]);
  3708. }
  3709. /*buf[0], buf[1] contains 0x, so ignore it.
  3710. buf[2], buf[3] contains A (alpha value), ignore it.case of ARGB*/
  3711. else {
  3712. brightness_r = hex_to_dec(buf[4], buf[5]);
  3713. brightness_g = hex_to_dec(buf[6], buf[7]);
  3714. brightness_b = hex_to_dec(buf[8], buf[9]);
  3715. }
  3716. for(cnt = 0; cnt < RGB_MAX; cnt++)
  3717. samsung_led_set(&info[cnt],0);
  3718. mutex_lock(&leds_mutex_lock);
  3719. pat_reg = &led_blink_patt[0];
  3720. for(i=0; i< pat_reg->len; i++){
  3721. for(j=1; j < pat_reg->patt[i].num_duty_pcts; j++){
  3722. if(pat_reg->patt[i].id == QPNP_ID_RGB_RED)
  3723. pat_reg->patt[i].duty_pcts[j] = ((brightness_r*100)/RGB_LED_MAX_BRIGHTNESS);
  3724. else if(pat_reg->patt[i].id == QPNP_ID_RGB_GREEN)
  3725. pat_reg->patt[i].duty_pcts[j] = ((brightness_g*100)/RGB_LED_MAX_BRIGHTNESS);
  3726. else
  3727. pat_reg->patt[i].duty_pcts[j] = ((brightness_b*100)/RGB_LED_MAX_BRIGHTNESS);
  3728. }
  3729. pat_reg->patt[i].hi_pause = delayon;
  3730. pat_reg->patt[i].lo_pause = delayoff;
  3731. }
  3732. if(brightness_r)
  3733. val |= RGB_LED_ENABLE_RED;
  3734. if(brightness_g)
  3735. val |= RGB_LED_ENABLE_GREEN;
  3736. if(brightness_b)
  3737. val |= RGB_LED_ENABLE_BLUE;
  3738. info[RGB_RED].rgb_cfg->enable = val;
  3739. info[RGB_GREEN].rgb_cfg->enable = val;
  3740. info[RGB_BLUE].rgb_cfg->enable = val;
  3741. led_pat_on(info, pat_reg, LED_FULL);
  3742. mutex_unlock(&leds_mutex_lock);
  3743. return size;
  3744. }
  3745. static ssize_t led_lowpower_show(struct device *dev,
  3746. struct device_attribute *attr, char *buf){
  3747. return snprintf(buf, 4, "%d\n", low_powermode);
  3748. }
  3749. static ssize_t led_lowpower_store(struct device *dev,
  3750. struct device_attribute *attr, const char *buf, size_t count){
  3751. low_powermode = (buf[0] == '1')?1:0;
  3752. return count;
  3753. }
  3754. static DEVICE_ATTR(led_pattern, S_IRUGO | S_IWUSR | S_IWGRP, show_patt,
  3755. store_patt);
  3756. static DEVICE_ATTR(led_r, S_IRUGO | S_IWUSR | S_IWGRP, led_r_show,led_r_store );
  3757. static DEVICE_ATTR(led_g, S_IRUGO | S_IWUSR | S_IWGRP, led_g_show,led_g_store );
  3758. static DEVICE_ATTR(led_b, S_IRUGO | S_IWUSR | S_IWGRP, led_b_show,led_b_store );
  3759. static DEVICE_ATTR(led_blink, S_IRUGO | S_IWUSR | S_IWGRP, NULL, led_blink_store );
  3760. static DEVICE_ATTR(led_lowpower, S_IRUGO | S_IWUSR | S_IWGRP, led_lowpower_show, led_lowpower_store );
  3761. static struct attribute *sec_led_attributes[] = {
  3762. &dev_attr_led_pattern.attr,
  3763. &dev_attr_led_r.attr,
  3764. &dev_attr_led_g.attr,
  3765. &dev_attr_led_b.attr,
  3766. &dev_attr_led_blink.attr,
  3767. &dev_attr_led_lowpower.attr,
  3768. NULL,
  3769. };
  3770. static struct attribute_group sec_led_attr_group = {
  3771. .attrs = sec_led_attributes,
  3772. };
  3773. /* Pattern end*/
  3774. #endif
  3775. static int __devinit qpnp_leds_probe(struct spmi_device *spmi)
  3776. {
  3777. struct qpnp_led_data *led, *led_array;
  3778. struct resource *led_resource;
  3779. struct device_node *node, *temp;
  3780. int rc, i, num_leds = 0, parsed_leds = 0;
  3781. const char *led_label;
  3782. bool regulator_probe = false;
  3783. node = spmi->dev.of_node;
  3784. if (node == NULL)
  3785. return -ENODEV;
  3786. temp = NULL;
  3787. while ((temp = of_get_next_child(node, temp)))
  3788. num_leds++;
  3789. if (!num_leds)
  3790. return -ECHILD;
  3791. led_array = devm_kzalloc(&spmi->dev,
  3792. (sizeof(struct qpnp_led_data) * num_leds), GFP_KERNEL);
  3793. if (!led_array) {
  3794. dev_err(&spmi->dev, "Unable to allocate memory\n");
  3795. return -ENOMEM;
  3796. }
  3797. for_each_child_of_node(node, temp) {
  3798. led = &led_array[parsed_leds];
  3799. led->num_leds = num_leds;
  3800. led->spmi_dev = spmi;
  3801. led_resource = spmi_get_resource(spmi, NULL, IORESOURCE_MEM, 0);
  3802. if (!led_resource) {
  3803. dev_err(&spmi->dev, "Unable to get LED base address\n");
  3804. rc = -ENXIO;
  3805. goto fail_id_check;
  3806. }
  3807. led->base = led_resource->start;
  3808. rc = of_property_read_string(temp, "label", &led_label);
  3809. if (rc < 0) {
  3810. dev_err(&led->spmi_dev->dev,
  3811. "Failure reading label, rc = %d\n", rc);
  3812. goto fail_id_check;
  3813. }
  3814. rc = of_property_read_string(temp, "linux,name",
  3815. &led->cdev.name);
  3816. if (rc < 0) {
  3817. dev_err(&led->spmi_dev->dev,
  3818. "Failure reading led name, rc = %d\n", rc);
  3819. goto fail_id_check;
  3820. }
  3821. rc = of_property_read_u32(temp, "qcom,max-current",
  3822. &led->max_current);
  3823. if (rc < 0) {
  3824. dev_err(&led->spmi_dev->dev,
  3825. "Failure reading max_current, rc = %d\n", rc);
  3826. goto fail_id_check;
  3827. }
  3828. rc = of_property_read_u32(temp, "qcom,id", &led->id);
  3829. if (rc < 0) {
  3830. dev_err(&led->spmi_dev->dev,
  3831. "Failure reading led id, rc = %d\n", rc);
  3832. goto fail_id_check;
  3833. }
  3834. rc = qpnp_get_common_configs(led, temp);
  3835. if (rc) {
  3836. dev_err(&led->spmi_dev->dev,
  3837. "Failure reading common led configuration," \
  3838. " rc = %d\n", rc);
  3839. goto fail_id_check;
  3840. }
  3841. led->cdev.brightness_set = qpnp_led_set;
  3842. led->cdev.brightness_get = qpnp_led_get;
  3843. if (strncmp(led_label, "wled", sizeof("wled")) == 0) {
  3844. rc = qpnp_get_config_wled(led, temp);
  3845. if (rc < 0) {
  3846. dev_err(&led->spmi_dev->dev,
  3847. "Unable to read wled config data\n");
  3848. goto fail_id_check;
  3849. }
  3850. } else if (strncmp(led_label, "flash", sizeof("flash"))
  3851. == 0) {
  3852. if (!of_find_property(node, "flash-boost-supply", NULL))
  3853. regulator_probe = true;
  3854. rc = qpnp_get_config_flash(led, temp, &regulator_probe);
  3855. if (rc < 0) {
  3856. dev_err(&led->spmi_dev->dev,
  3857. "Unable to read flash config data\n");
  3858. goto fail_id_check;
  3859. }
  3860. } else if (strncmp(led_label, "rgb", sizeof("rgb")) == 0) {
  3861. rc = qpnp_get_config_rgb(led, temp);
  3862. if (rc < 0) {
  3863. dev_err(&led->spmi_dev->dev,
  3864. "Unable to read rgb config data\n");
  3865. goto fail_id_check;
  3866. }
  3867. } else if (strncmp(led_label, "mpp", sizeof("mpp")) == 0) {
  3868. rc = qpnp_get_config_mpp(led, temp);
  3869. if (rc < 0) {
  3870. dev_err(&led->spmi_dev->dev,
  3871. "Unable to read mpp config data\n");
  3872. goto fail_id_check;
  3873. }
  3874. } else if (strncmp(led_label, "kpdbl", sizeof("kpdbl")) == 0) {
  3875. bitmap_zero(kpdbl_leds_in_use, NUM_KPDBL_LEDS);
  3876. is_kpdbl_master_turn_on = false;
  3877. rc = qpnp_get_config_kpdbl(led, temp);
  3878. if (rc < 0) {
  3879. dev_err(&led->spmi_dev->dev,
  3880. "Unable to read kpdbl config data\n");
  3881. goto fail_id_check;
  3882. }
  3883. } else {
  3884. dev_err(&led->spmi_dev->dev, "No LED matching label\n");
  3885. rc = -EINVAL;
  3886. goto fail_id_check;
  3887. }
  3888. if (led->id != QPNP_ID_FLASH1_LED0 &&
  3889. led->id != QPNP_ID_FLASH1_LED1)
  3890. mutex_init(&led->lock);
  3891. led->in_order_command_processing = of_property_read_bool
  3892. (temp, "qcom,in-order-command-processing");
  3893. if (led->in_order_command_processing) {
  3894. /*
  3895. * the command order from user space needs to be
  3896. * maintained use ordered workqueue to prevent
  3897. * concurrency
  3898. */
  3899. led->workqueue = alloc_ordered_workqueue
  3900. ("led_workqueue", 0);
  3901. if (!led->workqueue) {
  3902. rc = -ENOMEM;
  3903. goto fail_id_check;
  3904. }
  3905. }
  3906. INIT_WORK(&led->work, qpnp_led_work);
  3907. rc = qpnp_led_initialize(led);
  3908. if (rc < 0)
  3909. goto fail_id_check;
  3910. rc = qpnp_led_set_max_brightness(led);
  3911. if (rc < 0)
  3912. goto fail_id_check;
  3913. rc = led_classdev_register(&spmi->dev, &led->cdev);
  3914. if (rc) {
  3915. dev_err(&spmi->dev, "unable to register led %d,rc=%d\n",
  3916. led->id, rc);
  3917. goto fail_id_check;
  3918. }
  3919. if (led->id == QPNP_ID_FLASH1_LED0 ||
  3920. led->id == QPNP_ID_FLASH1_LED1) {
  3921. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3922. &led_attr_group);
  3923. if (rc)
  3924. goto fail_id_check;
  3925. }
  3926. if (led->id == QPNP_ID_LED_MPP) {
  3927. if (!led->mpp_cfg->pwm_cfg)
  3928. break;
  3929. if (led->mpp_cfg->pwm_cfg->mode == PWM_MODE) {
  3930. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3931. &pwm_attr_group);
  3932. if (rc)
  3933. goto fail_id_check;
  3934. }
  3935. if (led->mpp_cfg->pwm_cfg->use_blink) {
  3936. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3937. &blink_attr_group);
  3938. if (rc)
  3939. goto fail_id_check;
  3940. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3941. &lpg_attr_group);
  3942. if (rc)
  3943. goto fail_id_check;
  3944. } else if (led->mpp_cfg->pwm_cfg->mode == LPG_MODE) {
  3945. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3946. &lpg_attr_group);
  3947. if (rc)
  3948. goto fail_id_check;
  3949. }
  3950. } else if ((led->id == QPNP_ID_RGB_RED) ||
  3951. (led->id == QPNP_ID_RGB_GREEN) ||
  3952. (led->id == QPNP_ID_RGB_BLUE)) {
  3953. if (led->rgb_cfg->pwm_cfg->mode == PWM_MODE) {
  3954. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3955. &pwm_attr_group);
  3956. if (rc)
  3957. goto fail_id_check;
  3958. }
  3959. if (led->rgb_cfg->pwm_cfg->use_blink) {
  3960. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3961. &blink_attr_group);
  3962. if (rc)
  3963. goto fail_id_check;
  3964. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3965. &lpg_attr_group);
  3966. if (rc)
  3967. goto fail_id_check;
  3968. } else if (led->rgb_cfg->pwm_cfg->mode == LPG_MODE) {
  3969. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3970. &lpg_attr_group);
  3971. if (rc)
  3972. goto fail_id_check;
  3973. }
  3974. } else if (led->id == QPNP_ID_KPDBL) {
  3975. if (led->kpdbl_cfg->pwm_cfg->mode == PWM_MODE) {
  3976. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3977. &pwm_attr_group);
  3978. if (rc)
  3979. goto fail_id_check;
  3980. }
  3981. if (led->kpdbl_cfg->pwm_cfg->use_blink) {
  3982. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3983. &blink_attr_group);
  3984. if (rc)
  3985. goto fail_id_check;
  3986. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3987. &lpg_attr_group);
  3988. if (rc)
  3989. goto fail_id_check;
  3990. } else if (led->kpdbl_cfg->pwm_cfg->mode == LPG_MODE) {
  3991. rc = sysfs_create_group(&led->cdev.dev->kobj,
  3992. &lpg_attr_group);
  3993. if (rc)
  3994. goto fail_id_check;
  3995. }
  3996. }
  3997. /* configure default state */
  3998. if (led->default_on) {
  3999. led->cdev.brightness = led->cdev.max_brightness;
  4000. __qpnp_led_work(led, led->cdev.brightness);
  4001. if (led->turn_off_delay_ms > 0)
  4002. qpnp_led_turn_off(led);
  4003. } else
  4004. led->cdev.brightness = LED_OFF;
  4005. parsed_leds++;
  4006. }
  4007. if(led_array[RGB_RED].id == QPNP_ID_RGB_RED &&
  4008. led_array[RGB_GREEN].id == QPNP_ID_RGB_GREEN &&
  4009. led_array[RGB_BLUE].id == QPNP_ID_RGB_BLUE){
  4010. led_dev = device_create(sec_class, NULL, 0, NULL, "led");
  4011. if(IS_ERR(led_dev)){
  4012. printk(KERN_ERR "[LED]unable to create device");
  4013. goto fail_id_check;
  4014. }
  4015. dev_set_drvdata(led_dev, led_array);
  4016. #ifdef SAMSUNG_LED_PATTERN
  4017. rc = sysfs_create_group(&led_dev->kobj, &sec_led_attr_group);
  4018. if(rc) {
  4019. printk(KERN_ERR "[LED]unable to create sysfs\n");
  4020. goto fail_id_check;
  4021. }
  4022. mutex_init(&leds_mutex_lock);
  4023. low_powermode = 0;
  4024. on_patt = 0;
  4025. #endif
  4026. }
  4027. dev_set_drvdata(&spmi->dev, led_array);
  4028. return 0;
  4029. fail_id_check:
  4030. for (i = 0; i < parsed_leds; i++) {
  4031. if (led_array[i].id != QPNP_ID_FLASH1_LED0 &&
  4032. led_array[i].id != QPNP_ID_FLASH1_LED1)
  4033. mutex_destroy(&led_array[i].lock);
  4034. if (led_array[i].in_order_command_processing)
  4035. destroy_workqueue(led_array[i].workqueue);
  4036. led_classdev_unregister(&led_array[i].cdev);
  4037. }
  4038. return rc;
  4039. }
  4040. static int __devexit qpnp_leds_remove(struct spmi_device *spmi)
  4041. {
  4042. struct qpnp_led_data *led_array = dev_get_drvdata(&spmi->dev);
  4043. int i, parsed_leds = led_array->num_leds;
  4044. for (i = 0; i < parsed_leds; i++) {
  4045. cancel_work_sync(&led_array[i].work);
  4046. if (led_array[i].id != QPNP_ID_FLASH1_LED0 &&
  4047. led_array[i].id != QPNP_ID_FLASH1_LED1)
  4048. mutex_destroy(&led_array[i].lock);
  4049. if (led_array[i].in_order_command_processing)
  4050. destroy_workqueue(led_array[i].workqueue);
  4051. led_classdev_unregister(&led_array[i].cdev);
  4052. switch (led_array[i].id) {
  4053. case QPNP_ID_WLED:
  4054. break;
  4055. case QPNP_ID_FLASH1_LED0:
  4056. case QPNP_ID_FLASH1_LED1:
  4057. #ifndef SAMSUNG_USE_EXTERNAL_CHARGER
  4058. if (led_array[i].flash_cfg->flash_reg_get)
  4059. regulator_put(led_array[i].flash_cfg-> \
  4060. flash_boost_reg);
  4061. if (led_array[i].flash_cfg->torch_enable)
  4062. regulator_put(led_array[i].flash_cfg->\
  4063. torch_boost_reg);
  4064. #endif
  4065. sysfs_remove_group(&led_array[i].cdev.dev->kobj,
  4066. &led_attr_group);
  4067. break;
  4068. case QPNP_ID_RGB_RED:
  4069. case QPNP_ID_RGB_GREEN:
  4070. case QPNP_ID_RGB_BLUE:
  4071. if (led_array[i].rgb_cfg->pwm_cfg->mode == PWM_MODE)
  4072. sysfs_remove_group(&led_array[i].cdev.dev->\
  4073. kobj, &pwm_attr_group);
  4074. if (led_array[i].rgb_cfg->pwm_cfg->use_blink) {
  4075. sysfs_remove_group(&led_array[i].cdev.dev->\
  4076. kobj, &blink_attr_group);
  4077. sysfs_remove_group(&led_array[i].cdev.dev->\
  4078. kobj, &lpg_attr_group);
  4079. } else if (led_array[i].rgb_cfg->pwm_cfg->mode\
  4080. == LPG_MODE)
  4081. sysfs_remove_group(&led_array[i].cdev.dev->\
  4082. kobj, &lpg_attr_group);
  4083. break;
  4084. case QPNP_ID_LED_MPP:
  4085. if (!led_array[i].mpp_cfg->pwm_cfg)
  4086. break;
  4087. if (led_array[i].mpp_cfg->pwm_cfg->mode == PWM_MODE)
  4088. sysfs_remove_group(&led_array[i].cdev.dev->\
  4089. kobj, &pwm_attr_group);
  4090. if (led_array[i].mpp_cfg->pwm_cfg->use_blink) {
  4091. sysfs_remove_group(&led_array[i].cdev.dev->\
  4092. kobj, &blink_attr_group);
  4093. sysfs_remove_group(&led_array[i].cdev.dev->\
  4094. kobj, &lpg_attr_group);
  4095. } else if (led_array[i].mpp_cfg->pwm_cfg->mode\
  4096. == LPG_MODE)
  4097. sysfs_remove_group(&led_array[i].cdev.dev->\
  4098. kobj, &lpg_attr_group);
  4099. break;
  4100. case QPNP_ID_KPDBL:
  4101. if (led_array[i].kpdbl_cfg->pwm_cfg->mode == PWM_MODE)
  4102. sysfs_remove_group(&led_array[i].cdev.dev->
  4103. kobj, &pwm_attr_group);
  4104. if (led_array[i].kpdbl_cfg->pwm_cfg->use_blink) {
  4105. sysfs_remove_group(&led_array[i].cdev.dev->
  4106. kobj, &blink_attr_group);
  4107. sysfs_remove_group(&led_array[i].cdev.dev->
  4108. kobj, &lpg_attr_group);
  4109. } else if (led_array[i].kpdbl_cfg->pwm_cfg->mode
  4110. == LPG_MODE)
  4111. sysfs_remove_group(&led_array[i].cdev.dev->
  4112. kobj, &lpg_attr_group);
  4113. break;
  4114. default:
  4115. dev_err(&led_array[i].spmi_dev->dev,
  4116. "Invalid LED(%d)\n",
  4117. led_array[i].id);
  4118. return -EINVAL;
  4119. }
  4120. }
  4121. #ifdef SAMSUNG_LED_PATTERN
  4122. mutex_destroy(&leds_mutex_lock);
  4123. #endif
  4124. return 0;
  4125. }
  4126. #ifdef CONFIG_OF
  4127. static struct of_device_id spmi_match_table[] = {
  4128. { .compatible = "qcom,leds-qpnp",},
  4129. { },
  4130. };
  4131. #else
  4132. #define spmi_match_table NULL
  4133. #endif
  4134. static struct spmi_driver qpnp_leds_driver = {
  4135. .driver = {
  4136. .name = "qcom,leds-qpnp",
  4137. .of_match_table = spmi_match_table,
  4138. },
  4139. .probe = qpnp_leds_probe,
  4140. .remove = __devexit_p(qpnp_leds_remove),
  4141. };
  4142. static int __init qpnp_led_init(void)
  4143. {
  4144. return spmi_driver_register(&qpnp_leds_driver);
  4145. }
  4146. module_init(qpnp_led_init);
  4147. static void __exit qpnp_led_exit(void)
  4148. {
  4149. spmi_driver_unregister(&qpnp_leds_driver);
  4150. }
  4151. module_exit(qpnp_led_exit);
  4152. MODULE_DESCRIPTION("QPNP LEDs driver");
  4153. MODULE_LICENSE("GPL v2");
  4154. MODULE_ALIAS("leds:leds-qpnp");