pmic.c 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134
  1. /*****************************************************************************
  2. *
  3. * Filename:
  4. * ---------
  5. * pmic.c
  6. *
  7. * Project:
  8. * --------
  9. * Android_Software
  10. *
  11. * Description:
  12. * ------------
  13. * This Module defines PMIC functions
  14. *
  15. * Author:
  16. * -------
  17. * WY Chuang
  18. *
  19. ****************************************************************************/
  20. #include <generated/autoconf.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/sched.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/list.h>
  30. #include <linux/mutex.h>
  31. #include <linux/kthread.h>
  32. #include <linux/reboot.h>
  33. /*
  34. #if !defined CONFIG_HAS_WAKELOCKS
  35. #include <linux/pm_wakeup.h> included in linux/device.h
  36. #else
  37. */
  38. #include <linux/wakelock.h>
  39. /*#endif*/
  40. #include <linux/device.h>
  41. #include <linux/kdev_t.h>
  42. #include <linux/fs.h>
  43. #include <linux/cdev.h>
  44. #include <linux/delay.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/proc_fs.h>
  47. #include <linux/syscalls.h>
  48. #include <linux/sched.h>
  49. #include <linux/writeback.h>
  50. #include <linux/seq_file.h>
  51. #ifdef CONFIG_OF
  52. #include <linux/of.h>
  53. #include <linux/of_irq.h>
  54. #include <linux/regulator/of_regulator.h>
  55. #include <linux/regulator/machine.h>
  56. #include <linux/of_device.h>
  57. #include <linux/of_fdt.h>
  58. #endif
  59. #include <asm/uaccess.h>
  60. #include <linux/regulator/driver.h>
  61. #include <linux/regulator/machine.h>
  62. #if !defined CONFIG_MTK_LEGACY
  63. #include <linux/gpio.h>
  64. #include <linux/gpio/consumer.h>
  65. #endif
  66. #include <mt-plat/upmu_common.h>
  67. #include <pmic.h>
  68. /*#include <mach/eint.h> TBD*/
  69. #include <mach/mt_pmic_wrap.h>
  70. #if defined CONFIG_MTK_LEGACY
  71. #include <mt-plat/mt_gpio.h>
  72. #endif
  73. #include <mt-plat/mtk_rtc.h>
  74. #include <mach/mt_spm_mtcmos.h>
  75. #include <linux/time.h>
  76. #include "pmic_dvt.h"
  77. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  78. #include <mt-plat/mt_boot.h>
  79. #include <mt-plat/mt_gpt.h>
  80. #endif
  81. #if defined(CONFIG_MTK_SMART_BATTERY)
  82. #include <mt-plat/battery_meter.h>
  83. #include <mt-plat/battery_common.h>
  84. #include <mach/mt_battery_meter.h>
  85. #endif
  86. #include <mt6311.h>
  87. #include <mach/mt_pmic.h>
  88. #include <mt-plat/aee.h>
  89. #include "mt_devinfo.h"
  90. /*****************************************************************************
  91. * PMIC extern variable
  92. ******************************************************************************/
  93. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  94. static bool long_pwrkey_press;
  95. static unsigned long timer_pre;
  96. static unsigned long timer_pos;
  97. #define LONG_PWRKEY_PRESS_TIME (500*1000000) /*500ms */
  98. #endif
  99. /*****************************************************************************
  100. * Global variable
  101. ******************************************************************************/
  102. int g_pmic_irq;
  103. unsigned int g_eint_pmic_num = 206;
  104. unsigned int g_cust_eint_mt_pmic_debounce_cn = 1;
  105. unsigned int g_cust_eint_mt_pmic_type = 4;
  106. unsigned int g_cust_eint_mt_pmic_debounce_en = 1;
  107. /*****************************************************************************
  108. * PMIC related define
  109. ******************************************************************************/
  110. static DEFINE_MUTEX(pmic_lock_mutex);
  111. #define PMIC_EINT_SERVICE
  112. /*****************************************************************************
  113. * PMIC read/write APIs
  114. ******************************************************************************/
  115. #if 0 /*defined(CONFIG_MTK_FPGA) */
  116. /* no CONFIG_PMIC_HW_ACCESS_EN */
  117. #else
  118. #define CONFIG_PMIC_HW_ACCESS_EN
  119. #endif
  120. #define PMICTAG "[PMIC] "
  121. #if defined PMIC_DEBUG_PR_DBG
  122. #define PMICLOG(fmt, arg...) pr_err(PMICTAG fmt, ##arg)
  123. #else
  124. #define PMICLOG(fmt, arg...)
  125. #endif
  126. #ifdef CONFIG_OF
  127. #if !defined CONFIG_MTK_LEGACY
  128. /*
  129. static int pmic_mt_cust_probe(struct platform_device *pdev);
  130. static int pmic_mt_cust_remove(struct platform_device *pdev);
  131. static int pmic_regulator_ldo_init(struct platform_device *pdev);
  132. */
  133. #endif
  134. #endif /* End of #ifdef CONFIG_OF */
  135. static DEFINE_MUTEX(pmic_access_mutex);
  136. /*--- Global suspend state ---*/
  137. static bool pmic_suspend_state;
  138. void pmic_md_power_on(void)
  139. {
  140. /*ALPS02057700 workaround:
  141. * Power on VLTE for system power off backlight work normal
  142. */
  143. PMICLOG("md_power_on:set VLTE on,bit0,1\n");
  144. pmic_config_interface(0x04D6, 0x1, 0x1, 0); /* bit[0] =>1'b1 */
  145. udelay(200);
  146. }
  147. unsigned int pmic_read_interface(unsigned int RegNum, unsigned int *val, unsigned int MASK,
  148. unsigned int SHIFT)
  149. {
  150. unsigned int return_value = 0;
  151. #if defined(CONFIG_PMIC_HW_ACCESS_EN)
  152. unsigned int pmic_reg = 0;
  153. unsigned int rdata;
  154. #endif
  155. if ((pmic_suspend_state == true) && irqs_disabled())
  156. return pmic_read_interface_nolock(RegNum, val, MASK, SHIFT);
  157. #if defined(CONFIG_PMIC_HW_ACCESS_EN)
  158. mutex_lock(&pmic_access_mutex);
  159. /*mt_read_byte(RegNum, &pmic_reg); */
  160. return_value = pwrap_wacs2(0, (RegNum), 0, &rdata);
  161. pmic_reg = rdata;
  162. if (return_value != 0) {
  163. PMICLOG("[pmic_read_interface] Reg[%x]= pmic_wrap read data fail\n", RegNum);
  164. mutex_unlock(&pmic_access_mutex);
  165. return return_value;
  166. }
  167. /*PMICLOG"[pmic_read_interface] Reg[%x]=0x%x\n", RegNum, pmic_reg); */
  168. pmic_reg &= (MASK << SHIFT);
  169. *val = (pmic_reg >> SHIFT);
  170. /*PMICLOG"[pmic_read_interface] val=0x%x\n", *val); */
  171. mutex_unlock(&pmic_access_mutex);
  172. #else
  173. /*PMICLOG("[pmic_read_interface] Can not access HW PMIC\n"); */
  174. #endif
  175. return return_value;
  176. }
  177. unsigned int pmic_config_interface(unsigned int RegNum, unsigned int val, unsigned int MASK,
  178. unsigned int SHIFT)
  179. {
  180. unsigned int return_value = 0;
  181. #if defined(CONFIG_PMIC_HW_ACCESS_EN)
  182. unsigned int pmic_reg = 0;
  183. unsigned int rdata;
  184. #endif
  185. if ((pmic_suspend_state == true) && irqs_disabled())
  186. return pmic_config_interface_nolock(RegNum, val, MASK, SHIFT);
  187. #if defined(CONFIG_PMIC_HW_ACCESS_EN)
  188. mutex_lock(&pmic_access_mutex);
  189. /*1. mt_read_byte(RegNum, &pmic_reg); */
  190. return_value = pwrap_wacs2(0, (RegNum), 0, &rdata);
  191. pmic_reg = rdata;
  192. if (return_value != 0) {
  193. PMICLOG("[pmic_config_interface] Reg[%x]= pmic_wrap read data fail\n", RegNum);
  194. mutex_unlock(&pmic_access_mutex);
  195. return return_value;
  196. }
  197. /*PMICLOG"[pmic_config_interface] Reg[%x]=0x%x\n", RegNum, pmic_reg); */
  198. pmic_reg &= ~(MASK << SHIFT);
  199. pmic_reg |= (val << SHIFT);
  200. /*2. mt_write_byte(RegNum, pmic_reg); */
  201. return_value = pwrap_wacs2(1, (RegNum), pmic_reg, &rdata);
  202. if (return_value != 0) {
  203. PMICLOG("[pmic_config_interface] Reg[%x]= pmic_wrap read data fail\n", RegNum);
  204. mutex_unlock(&pmic_access_mutex);
  205. return return_value;
  206. }
  207. /*PMICLOG"[pmic_config_interface] write Reg[%x]=0x%x\n", RegNum, pmic_reg); */
  208. #if 0
  209. /*3. Double Check */
  210. /*mt_read_byte(RegNum, &pmic_reg); */
  211. return_value = pwrap_wacs2(0, (RegNum), 0, &rdata);
  212. pmic_reg = rdata;
  213. if (return_value != 0) {
  214. PMICLOG("[pmic_config_interface] Reg[%x]= pmic_wrap write data fail\n", RegNum);
  215. mutex_unlock(&pmic_access_mutex);
  216. return return_value;
  217. }
  218. PMICLOG("[pmic_config_interface] Reg[%x]=0x%x\n", RegNum, pmic_reg);
  219. #endif
  220. mutex_unlock(&pmic_access_mutex);
  221. #else
  222. /*PMICLOG("[pmic_config_interface] Can not access HW PMIC\n"); */
  223. #endif
  224. return return_value;
  225. }
  226. unsigned int pmic_read_interface_nolock(unsigned int RegNum, unsigned int *val, unsigned int MASK,
  227. unsigned int SHIFT)
  228. {
  229. unsigned int return_value = 0;
  230. #if defined(CONFIG_PMIC_HW_ACCESS_EN)
  231. unsigned int pmic_reg = 0;
  232. unsigned int rdata;
  233. /*mt_read_byte(RegNum, &pmic_reg); */
  234. return_value = pwrap_wacs2(0, (RegNum), 0, &rdata);
  235. pmic_reg = rdata;
  236. if (return_value != 0) {
  237. PMICLOG("[pmic_read_interface] Reg[%x]= pmic_wrap read data fail\n", RegNum);
  238. mutex_unlock(&pmic_access_mutex);
  239. return return_value;
  240. }
  241. /*PMICLOG"[pmic_read_interface] Reg[%x]=0x%x\n", RegNum, pmic_reg); */
  242. pmic_reg &= (MASK << SHIFT);
  243. *val = (pmic_reg >> SHIFT);
  244. /*PMICLOG"[pmic_read_interface] val=0x%x\n", *val); */
  245. #else
  246. /*PMICLOG("[pmic_read_interface] Can not access HW PMIC\n"); */
  247. #endif
  248. return return_value;
  249. }
  250. unsigned int pmic_config_interface_nolock(unsigned int RegNum, unsigned int val, unsigned int MASK,
  251. unsigned int SHIFT)
  252. {
  253. unsigned int return_value = 0;
  254. #if defined(CONFIG_PMIC_HW_ACCESS_EN)
  255. unsigned int pmic_reg = 0;
  256. unsigned int rdata;
  257. /* pmic wrapper has spinlock protection. pmic do not to do it again */
  258. /*1. mt_read_byte(RegNum, &pmic_reg); */
  259. return_value = pwrap_wacs2(0, (RegNum), 0, &rdata);
  260. pmic_reg = rdata;
  261. if (return_value != 0) {
  262. PMICLOG("[pmic_config_interface] Reg[%x]= pmic_wrap read data fail\n", RegNum);
  263. mutex_unlock(&pmic_access_mutex);
  264. return return_value;
  265. }
  266. /*PMICLOG"[pmic_config_interface] Reg[%x]=0x%x\n", RegNum, pmic_reg); */
  267. pmic_reg &= ~(MASK << SHIFT);
  268. pmic_reg |= (val << SHIFT);
  269. /*2. mt_write_byte(RegNum, pmic_reg); */
  270. return_value = pwrap_wacs2(1, (RegNum), pmic_reg, &rdata);
  271. if (return_value != 0) {
  272. PMICLOG("[pmic_config_interface] Reg[%x]= pmic_wrap read data fail\n", RegNum);
  273. mutex_unlock(&pmic_access_mutex);
  274. return return_value;
  275. }
  276. /*PMICLOG"[pmic_config_interface] write Reg[%x]=0x%x\n", RegNum, pmic_reg); */
  277. #if 0
  278. /*3. Double Check */
  279. /*mt_read_byte(RegNum, &pmic_reg); */
  280. return_value = pwrap_wacs2(0, (RegNum), 0, &rdata);
  281. pmic_reg = rdata;
  282. if (return_value != 0) {
  283. PMICLOG("[pmic_config_interface] Reg[%x]= pmic_wrap write data fail\n", RegNum);
  284. mutex_unlock(&pmic_access_mutex);
  285. return return_value;
  286. }
  287. PMICLOG("[pmic_config_interface] Reg[%x]=0x%x\n", RegNum, pmic_reg);
  288. #endif
  289. #else
  290. /*PMICLOG("[pmic_config_interface] Can not access HW PMIC\n"); */
  291. #endif
  292. return return_value;
  293. }
  294. /*****************************************************************************
  295. * PMIC lock/unlock APIs
  296. ******************************************************************************/
  297. void pmic_lock(void)
  298. {
  299. mutex_lock(&pmic_lock_mutex);
  300. }
  301. void pmic_unlock(void)
  302. {
  303. mutex_unlock(&pmic_lock_mutex);
  304. }
  305. unsigned int upmu_get_reg_value(unsigned int reg)
  306. {
  307. unsigned int ret = 0;
  308. unsigned int reg_val = 0;
  309. ret = pmic_read_interface(reg, &reg_val, 0xFFFF, 0x0);
  310. return reg_val;
  311. }
  312. EXPORT_SYMBOL(upmu_get_reg_value);
  313. void upmu_set_reg_value(unsigned int reg, unsigned int reg_val)
  314. {
  315. unsigned int ret = 0;
  316. ret = pmic_config_interface(reg, reg_val, 0xFFFF, 0x0);
  317. }
  318. unsigned int get_pmic_mt6325_cid(void)
  319. {
  320. return 0;
  321. }
  322. unsigned int get_mt6325_pmic_chip_version(void)
  323. {
  324. return 0;
  325. }
  326. /**********************************************************
  327. *
  328. * [Internal Function]
  329. *
  330. *********************************************************/
  331. void mt6328_dump_register(void)
  332. {
  333. unsigned int i = 0;
  334. PMICLOG("dump PMIC 6328 register\n");
  335. for (i = 0; i <= 0x0fae; i = i + 10) {
  336. pr_debug
  337. ("Reg[0x%x]=0x%x Reg[0x%x]=0x%x Reg[0x%x]=0x%x Reg[0x%x]=0x%x Reg[0x%x]=0x%x\n",
  338. i, upmu_get_reg_value(i), i + 1, upmu_get_reg_value(i + 1), i + 2,
  339. upmu_get_reg_value(i + 2), i + 3, upmu_get_reg_value(i + 3), i + 4,
  340. upmu_get_reg_value(i + 4));
  341. pr_debug
  342. ("Reg[0x%x]=0x%x Reg[0x%x]=0x%x Reg[0x%x]=0x%x Reg[0x%x]=0x%x Reg[0x%x]=0x%x\n",
  343. i + 5, upmu_get_reg_value(i + 5), i + 6, upmu_get_reg_value(i + 6), i + 7,
  344. upmu_get_reg_value(i + 7), i + 8, upmu_get_reg_value(i + 8), i + 9,
  345. upmu_get_reg_value(i + 9));
  346. }
  347. }
  348. /*****************************************************************************
  349. * workaround for vio18 drop issue in E1
  350. ******************************************************************************/
  351. static const unsigned char mt6328_VIO[] = {
  352. 12, 13, 14, 15, 0, 1, 2, 3, 8, 8, 8, 8, 8, 9, 10, 11
  353. };
  354. static unsigned char vio18_cal;
  355. void upmu_set_rg_vio18_cal(unsigned int en)
  356. {
  357. unsigned int chip_version = 0;
  358. chip_version = pmic_get_register_value(PMIC_SWCID);
  359. if (chip_version == PMIC6328_E1_CID_CODE) {
  360. if (en == 1)
  361. pmic_set_register_value(PMIC_RG_VIO18_CAL, mt6328_VIO[vio18_cal]);
  362. else
  363. pmic_set_register_value(PMIC_RG_VIO18_CAL, vio18_cal);
  364. }
  365. }
  366. EXPORT_SYMBOL(upmu_set_rg_vio18_cal);
  367. static const unsigned char mt6328_VIO_1_84[] = {
  368. 14, 15, 0, 1, 2, 3, 4, 5, 8, 8, 8, 9, 10, 11, 12, 13
  369. };
  370. void upmu_set_rg_vio18_184(void)
  371. {
  372. PMICLOG("[upmu_set_rg_vio18_184] old cal=%d new cal=%d.\r\n", vio18_cal,
  373. mt6328_VIO_1_84[vio18_cal]);
  374. pmic_set_register_value(PMIC_RG_VIO18_CAL, mt6328_VIO_1_84[vio18_cal]);
  375. }
  376. static const unsigned char mt6328_VMC_1_86[] = {
  377. 14, 15, 0, 1, 2, 3, 4, 5, 8, 8, 8, 9, 10, 11, 12, 13
  378. };
  379. static unsigned char vmc_cal;
  380. void upmu_set_rg_vmc_184(unsigned char x)
  381. {
  382. PMICLOG("[upmu_set_rg_vio18_184] old cal=%d new cal=%d.\r\n", vmc_cal,
  383. mt6328_VMC_1_86[vmc_cal]);
  384. if (x == 0) {
  385. pmic_set_register_value(PMIC_RG_VMC_CAL, vmc_cal);
  386. PMICLOG("[upmu_set_rg_vio18_184]:0 old cal=%d new cal=%d.\r\n", vmc_cal, vmc_cal);
  387. } else {
  388. pmic_set_register_value(PMIC_RG_VMC_CAL, mt6328_VMC_1_86[vmc_cal]);
  389. PMICLOG("[upmu_set_rg_vio18_184]:1 old cal=%d new cal=%d.\r\n", vmc_cal,
  390. mt6328_VMC_1_86[vmc_cal]);
  391. }
  392. }
  393. static unsigned char vcamd_cal;
  394. static const unsigned char mt6328_vcamd[] = {
  395. 1, 2, 3, 4, 5, 6, 7, 7, 9, 10, 11, 12, 13, 14, 15, 0
  396. };
  397. void upmu_set_rg_vcamd(unsigned char x)
  398. {
  399. PMICLOG("[upmu_set_rg_vcamd] old cal=%d new cal=%d.\r\n", vcamd_cal,
  400. mt6328_vcamd[vcamd_cal]);
  401. if (x == 0) {
  402. pmic_set_register_value(PMIC_RG_VCAMD_CAL, vcamd_cal);
  403. PMICLOG("[upmu_set_rg_vcamd]:0 old cal=%d new cal=%d.\r\n", vcamd_cal, vcamd_cal);
  404. } else {
  405. pmic_set_register_value(PMIC_RG_VCAMD_CAL, mt6328_vcamd[vcamd_cal]);
  406. PMICLOG("[upmu_set_rg_vcamd]:1 old cal=%d new cal=%d.\r\n", vcamd_cal,
  407. mt6328_vcamd[vcamd_cal]);
  408. }
  409. }
  410. /*****************************************************************************
  411. * workaround for VMC voltage list
  412. ******************************************************************************/
  413. int pmic_read_VMC_efuse(void)
  414. {
  415. unsigned int val;
  416. pmic_set_register_value(PMIC_RG_EFUSE_CK_PDN_HWEN, 0);
  417. pmic_set_register_value(PMIC_RG_EFUSE_CK_PDN, 0);
  418. pmic_set_register_value(PMIC_RG_OTP_RD_SW, 1);
  419. pmic_set_register_value(PMIC_RG_OTP_PA, 0xd * 2);
  420. udelay(100);
  421. if (pmic_get_register_value(PMIC_RG_OTP_RD_TRIG) == 0)
  422. pmic_set_register_value(PMIC_RG_OTP_RD_TRIG, 1);
  423. else
  424. pmic_set_register_value(PMIC_RG_OTP_RD_TRIG, 0);
  425. udelay(300);
  426. while (pmic_get_register_value(PMIC_RG_OTP_RD_BUSY) == 1)
  427. ;
  428. udelay(1000);
  429. val = pmic_get_register_value(PMIC_RG_OTP_DOUT_SW);
  430. val = val * 0x80;
  431. pmic_set_register_value(PMIC_RG_EFUSE_CK_PDN_HWEN, 1);
  432. pmic_set_register_value(PMIC_RG_EFUSE_CK_PDN, 1);
  433. return val;
  434. }
  435. /*****************************************************************************
  436. * upmu_interrupt_chrdet_int_en
  437. ******************************************************************************/
  438. void upmu_interrupt_chrdet_int_en(unsigned int val)
  439. {
  440. PMICLOG("[upmu_interrupt_chrdet_int_en] val=%d.\r\n", val);
  441. /*mt6325_upmu_set_rg_int_en_chrdet(val); */
  442. pmic_set_register_value(PMIC_RG_INT_EN_CHRDET, val);
  443. }
  444. EXPORT_SYMBOL(upmu_interrupt_chrdet_int_en);
  445. /*****************************************************************************
  446. * PMIC charger detection
  447. ******************************************************************************/
  448. unsigned int upmu_get_rgs_chrdet(void)
  449. {
  450. unsigned int val = 0;
  451. /*val = mt6325_upmu_get_rgs_chrdet(); */
  452. val = pmic_get_register_value(PMIC_RGS_CHRDET);
  453. PMICLOG("[upmu_get_rgs_chrdet] CHRDET status = %d\n", val);
  454. return val;
  455. }
  456. /*****************************************************************************
  457. * mt-pmic dev_attr APIs
  458. ******************************************************************************/
  459. unsigned int g_reg_value = 0;
  460. static ssize_t show_pmic_access(struct device *dev, struct device_attribute *attr, char *buf)
  461. {
  462. pr_err("[show_pmic_access] 0x%x\n", g_reg_value);
  463. return sprintf(buf, "%u\n", g_reg_value);
  464. }
  465. static ssize_t store_pmic_access(struct device *dev, struct device_attribute *attr, const char *buf,
  466. size_t size)
  467. {
  468. int ret = 0;
  469. char *pvalue = NULL, *addr, *val;
  470. unsigned int reg_value = 0;
  471. unsigned int reg_address = 0;
  472. pr_err("[store_pmic_access]\n");
  473. if (buf != NULL && size != 0) {
  474. pr_err("[store_pmic_access] buf is %s\n", buf);
  475. /*reg_address = simple_strtoul(buf, &pvalue, 16); */
  476. pvalue = (char *)buf;
  477. if (size > 5) {
  478. addr = strsep(&pvalue, " ");
  479. ret = kstrtou32(addr, 16, (unsigned int *)&reg_address);
  480. } else
  481. ret = kstrtou32(pvalue, 16, (unsigned int *)&reg_address);
  482. if (size > 5) {
  483. /*reg_value = simple_strtoul((pvalue + 1), NULL, 16); */
  484. /*pvalue = (char *)buf + 1; */
  485. val = strsep(&pvalue, " ");
  486. ret = kstrtou32(val, 16, (unsigned int *)&reg_value);
  487. pr_err("[store_pmic_access] write PMU reg 0x%x with value 0x%x !\n",
  488. reg_address, reg_value);
  489. ret = pmic_config_interface(reg_address, reg_value, 0xFFFF, 0x0);
  490. } else {
  491. ret = pmic_read_interface(reg_address, &g_reg_value, 0xFFFF, 0x0);
  492. pr_err("[store_pmic_access] read PMU reg 0x%x with value 0x%x !\n",
  493. reg_address, g_reg_value);
  494. pr_err
  495. ("[store_pmic_access] use \"cat pmic_access\" to get value(decimal)\r\n");
  496. }
  497. }
  498. return size;
  499. }
  500. static DEVICE_ATTR(pmic_access, 0664, show_pmic_access, store_pmic_access); /*664 */
  501. /*
  502. * DVT entry
  503. */
  504. unsigned char g_reg_value_pmic = 0;
  505. static ssize_t show_pmic_dvt(struct device *dev, struct device_attribute *attr, char *buf)
  506. {
  507. pr_err("[show_pmic_dvt] 0x%x\n", g_reg_value_pmic);
  508. return sprintf(buf, "%u\n", g_reg_value_pmic);
  509. }
  510. static ssize_t store_pmic_dvt(struct device *dev, struct device_attribute *attr, const char *buf,
  511. size_t size)
  512. {
  513. int ret = 0;
  514. char *pvalue = NULL;
  515. unsigned int test_item = 0;
  516. pr_err("[store_pmic_dvt]\n");
  517. if (buf != NULL && size != 0) {
  518. pr_err("[store_pmic_dvt] buf is %s and size is %zu\n", buf, size);
  519. /*test_item = simple_strtoul(buf, &pvalue, 10); */
  520. pvalue = (char *)buf;
  521. ret = kstrtou32(pvalue, 16, (unsigned int *)&test_item);
  522. pr_err("[store_pmic_dvt] test_item=%d\n", test_item);
  523. #ifdef MTK_PMIC_DVT_SUPPORT
  524. pmic_dvt_entry(test_item);
  525. #else
  526. pr_err("[store_pmic_dvt] no define MTK_PMIC_DVT_SUPPORT\n");
  527. #endif
  528. }
  529. return size;
  530. }
  531. static DEVICE_ATTR(pmic_dvt, 0664, show_pmic_dvt, store_pmic_dvt);
  532. /*
  533. * PMIC6328 linux reguplator driver
  534. */
  535. /*extern PMU_FLAG_TABLE_ENTRY pmu_flags_table[];*/
  536. static int mtk_regulator_enable(struct regulator_dev *rdev)
  537. {
  538. const struct regulator_desc *rdesc = rdev->desc;
  539. struct mtk_regulator *mreg;
  540. unsigned int add = 0, val = 0;
  541. mreg = container_of(rdesc, struct mtk_regulator, desc);
  542. if (mreg->en_reg != 0) {
  543. pmic_set_register_value(mreg->en_reg, 1);
  544. add = pmu_flags_table[mreg->en_reg].offset;
  545. val = upmu_get_reg_value(pmu_flags_table[mreg->en_reg].offset);
  546. }
  547. PMICLOG("regulator_enable(name=%s id=%d en_reg=%x vol_reg=%x) [%x]=0x%x\n", rdesc->name,
  548. rdesc->id, mreg->en_reg, mreg->vol_reg, add, val);
  549. return 0;
  550. }
  551. static int mtk_regulator_disable(struct regulator_dev *rdev)
  552. {
  553. const struct regulator_desc *rdesc = rdev->desc;
  554. struct mtk_regulator *mreg;
  555. unsigned int add = 0, val = 0;
  556. mreg = container_of(rdesc, struct mtk_regulator, desc);
  557. if (rdev->use_count == 0) {
  558. PMICLOG("regulator name=%s should not disable( use_count=%d)\n", rdesc->name,
  559. rdev->use_count);
  560. return -1;
  561. }
  562. if (mreg->en_reg != 0) {
  563. pmic_set_register_value(mreg->en_reg, 0);
  564. add = pmu_flags_table[mreg->en_reg].offset;
  565. val = upmu_get_reg_value(pmu_flags_table[mreg->en_reg].offset);
  566. }
  567. PMICLOG("regulator_disable(name=%s id=%d en_reg=%x vol_reg=%x use_count=%d) [%x]=0x%x\n",
  568. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg, rdev->use_count, add, val);
  569. return 0;
  570. }
  571. static int mtk_regulator_is_enabled(struct regulator_dev *rdev)
  572. {
  573. const struct regulator_desc *rdesc = rdev->desc;
  574. struct mtk_regulator *mreg;
  575. int en;
  576. mreg = container_of(rdesc, struct mtk_regulator, desc);
  577. en = pmic_get_register_value(mreg->en_reg);
  578. PMICLOG("[PMIC]regulator_is_enabled(name=%s id=%d en_reg=%x vol_reg=%x en=%d)\n",
  579. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg, en);
  580. return en;
  581. }
  582. static int mtk_regulator_get_voltage_sel(struct regulator_dev *rdev)
  583. {
  584. const struct regulator_desc *rdesc = rdev->desc;
  585. struct mtk_regulator *mreg;
  586. unsigned char regVal = 0;
  587. const int *pVoltage;
  588. int voltage = 0;
  589. unsigned int add = 0, val = 9;
  590. mreg = container_of(rdesc, struct mtk_regulator, desc);
  591. if (mreg->desc.n_voltages != 1) {
  592. if (mreg->vol_reg != 0) {
  593. regVal = pmic_get_register_value(mreg->vol_reg);
  594. if (mreg->pvoltages != NULL) {
  595. pVoltage = (const int *)mreg->pvoltages;
  596. voltage = pVoltage[regVal];
  597. add = pmu_flags_table[mreg->en_reg].offset;
  598. val = upmu_get_reg_value(pmu_flags_table[mreg->en_reg].offset);
  599. } else {
  600. voltage = mreg->desc.min_uV + mreg->desc.uV_step * regVal;
  601. }
  602. } else {
  603. PMICLOG
  604. ("regulator_get_voltage_sel bugl(name=%s id=%d en_reg=%x vol_reg=%x)\n",
  605. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg);
  606. }
  607. } else {
  608. if (mreg->vol_reg != 0) {
  609. regVal = 0;
  610. pVoltage = (const int *)mreg->pvoltages;
  611. voltage = pVoltage[regVal];
  612. } else {
  613. PMICLOG
  614. ("regulator_get_voltage_sel bugl(name=%s id=%d en_reg=%x vol_reg=%x)\n",
  615. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg);
  616. }
  617. }
  618. PMICLOG
  619. ("regulator_get_voltage_sel(name=%s id=%d en_reg=%x vol_reg=%x reg/sel:%d voltage:%d [0x%x]=0x%x)\n",
  620. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg, regVal, voltage, add, val);
  621. return regVal;
  622. }
  623. static int mtk_regulator_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
  624. {
  625. const struct regulator_desc *rdesc = rdev->desc;
  626. struct mtk_regulator *mreg;
  627. mreg = container_of(rdesc, struct mtk_regulator, desc);
  628. PMICLOG("regulator_set_voltage_sel(name=%s id=%d en_reg=%x vol_reg=%x selector=%d)\n",
  629. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg, selector);
  630. if (strcmp(rdesc->name, "VCAMD") == 0) {
  631. if (selector == 3)
  632. upmu_set_rg_vcamd(1);
  633. else
  634. upmu_set_rg_vcamd(0);
  635. }
  636. if (strcmp(rdesc->name, "VRF18_1") == 0) {
  637. if (selector == 4)
  638. pmic_config_interface(0xA86, 0x0, 0x1, 2);
  639. else
  640. pmic_config_interface(0xA86, 0x1, 0x1, 2);
  641. }
  642. if (strcmp(rdesc->name, "VEFUSE") == 0) {
  643. if (mreg->vol_reg != 0)
  644. pmic_set_register_value(mreg->vol_reg, selector+3);
  645. } else {
  646. if (mreg->vol_reg != 0)
  647. pmic_set_register_value(mreg->vol_reg, selector);
  648. }
  649. return 0;
  650. }
  651. static int mtk_regulator_list_voltage(struct regulator_dev *rdev, unsigned selector)
  652. {
  653. const struct regulator_desc *rdesc = rdev->desc;
  654. struct mtk_regulator *mreg;
  655. const int *pVoltage;
  656. int voltage = 0;
  657. mreg = container_of(rdesc, struct mtk_regulator, desc);
  658. if (mreg->desc.n_voltages != 1) {
  659. if (mreg->vol_reg != 0) {
  660. if (mreg->pvoltages != NULL) {
  661. pVoltage = (const int *)mreg->pvoltages;
  662. voltage = pVoltage[selector];
  663. } else {
  664. voltage = mreg->desc.min_uV + mreg->desc.uV_step * selector;
  665. }
  666. } else {
  667. PMICLOG
  668. ("mtk_regulator_list_voltage bugl(name=%s id=%d en_reg=%x vol_reg=%x)\n",
  669. rdesc->name, rdesc->id, mreg->en_reg, mreg->vol_reg);
  670. }
  671. } else {
  672. pVoltage = (const int *)mreg->pvoltages;
  673. voltage = pVoltage[0];
  674. }
  675. return voltage;
  676. }
  677. static struct regulator_ops mtk_regulator_ops = {
  678. .enable = mtk_regulator_enable,
  679. .disable = mtk_regulator_disable,
  680. .is_enabled = mtk_regulator_is_enabled,
  681. .get_voltage_sel = mtk_regulator_get_voltage_sel,
  682. .set_voltage_sel = mtk_regulator_set_voltage_sel,
  683. .list_voltage = mtk_regulator_list_voltage,
  684. };
  685. static ssize_t show_LDO_STATUS(struct device *dev, struct device_attribute *attr, char *buf)
  686. {
  687. struct mtk_regulator *mreg;
  688. unsigned int ret_value = 0;
  689. mreg = container_of(attr, struct mtk_regulator, en_att);
  690. ret_value = pmic_get_register_value(mreg->en_reg);
  691. pr_debug("[EM] LDO_%s_STATUS : %d\n", mreg->desc.name, ret_value);
  692. return sprintf(buf, "%u\n", ret_value);
  693. }
  694. static ssize_t store_LDO_STATUS(struct device *dev, struct device_attribute *attr, const char *buf,
  695. size_t size)
  696. {
  697. PMICLOG("[EM] Not Support Write Function\n");
  698. return size;
  699. }
  700. static ssize_t show_LDO_VOLTAGE(struct device *dev, struct device_attribute *attr, char *buf)
  701. {
  702. struct mtk_regulator *mreg;
  703. const int *pVoltage;
  704. unsigned short regVal;
  705. unsigned int ret_value = 0;
  706. mreg = container_of(attr, struct mtk_regulator, voltage_att);
  707. if (mreg->desc.n_voltages != 1) {
  708. if (mreg->vol_reg != 0) {
  709. regVal = pmic_get_register_value(mreg->vol_reg);
  710. if (mreg->pvoltages != NULL) {
  711. pVoltage = (const int *)mreg->pvoltages;
  712. ret_value = pVoltage[regVal];
  713. } else {
  714. ret_value = mreg->desc.min_uV + mreg->desc.uV_step * regVal;
  715. }
  716. } else {
  717. PMICLOG("[EM][ERROR] LDO_%s_VOLTAGE : voltage=0 vol_reg=0\n",
  718. mreg->desc.name);
  719. }
  720. } else {
  721. pVoltage = (const int *)mreg->pvoltages;
  722. ret_value = pVoltage[0];
  723. }
  724. ret_value = ret_value / 1000;
  725. pr_err("[EM] LDO_%s_VOLTAGE : %d\n", mreg->desc.name, ret_value);
  726. return sprintf(buf, "%u\n", ret_value);
  727. }
  728. static ssize_t store_LDO_VOLTAGE(struct device *dev, struct device_attribute *attr, const char *buf,
  729. size_t size)
  730. {
  731. PMICLOG("[EM] Not Support Write Function\n");
  732. return size;
  733. }
  734. static ssize_t show_BUCK_STATUS(struct device *dev, struct device_attribute *attr, char *buf)
  735. {
  736. struct mtk_regulator *mreg;
  737. unsigned int ret_value = 0;
  738. mreg = container_of(attr, struct mtk_regulator, en_att);
  739. ret_value = pmic_get_register_value(mreg->qi_en_reg);
  740. pr_debug("[EM] BUCK_%s_STATUS : %d\n", mreg->desc.name, ret_value);
  741. return sprintf(buf, "%u\n", ret_value);
  742. }
  743. static ssize_t store_BUCK_STATUS(struct device *dev, struct device_attribute *attr, const char *buf,
  744. size_t size)
  745. {
  746. PMICLOG("[EM] Not Support Write Function\n");
  747. return size;
  748. }
  749. static ssize_t show_BUCK_VOLTAGE(struct device *dev, struct device_attribute *attr, char *buf)
  750. {
  751. struct mtk_regulator *mreg;
  752. const int *pVoltage;
  753. unsigned short regVal;
  754. unsigned int ret_value = 0;
  755. mreg = container_of(attr, struct mtk_regulator, voltage_att);
  756. if (mreg->desc.n_voltages != 1) {
  757. if (mreg->qi_vol_reg != 0) {
  758. regVal = pmic_get_register_value(mreg->qi_vol_reg);
  759. if (mreg->pvoltages != NULL) {
  760. pVoltage = (const int *)mreg->pvoltages;
  761. ret_value = pVoltage[regVal];
  762. } else {
  763. ret_value = mreg->desc.min_uV + mreg->desc.uV_step * regVal;
  764. }
  765. } else {
  766. pr_debug("[EM][ERROR] buck_%s_VOLTAGE : voltage=0 vol_reg=0\n",
  767. mreg->desc.name);
  768. }
  769. } else {
  770. pVoltage = (const int *)mreg->pvoltages;
  771. ret_value = pVoltage[0];
  772. }
  773. ret_value = ret_value / 1000;
  774. pr_debug("[EM] BUCK_%s_VOLTAGE : %d\n", mreg->desc.name, ret_value);
  775. return sprintf(buf, "%u\n", ret_value);
  776. }
  777. static ssize_t store_BUCK_VOLTAGE(struct device *dev, struct device_attribute *attr,
  778. const char *buf, size_t size)
  779. {
  780. PMICLOG("[EM] Not Support Write Function\n");
  781. return size;
  782. }
  783. /*LDO setting*/
  784. static const int mt6328_VCAMA_voltages[] = {
  785. 1500000,
  786. 1800000,
  787. 2500000,
  788. 2800000,
  789. };
  790. static const int mt6328_VCN33_voltages[] = {
  791. 3300000,
  792. 3400000,
  793. 3500000,
  794. 3600000,
  795. };
  796. static const int mt6328_VEFUSE_voltages[] = {
  797. 1800000,
  798. 1900000,
  799. 2000000,
  800. 2100000,
  801. 2200000,
  802. };
  803. /*VSIM1 & VSIM2*/
  804. static const int mt6328_VSIM1_voltages[] = {
  805. 1700000,
  806. 1800000,
  807. 1860000,
  808. 2760000,
  809. 3000000,
  810. 3100000,
  811. };
  812. static const int mt6328_VEMC33_voltages[] = {
  813. 1800000,
  814. 2900000,
  815. 3000000,
  816. 3300000,
  817. };
  818. static const int mt6328_VMCH_voltages[] = {
  819. 2900000,
  820. 3000000,
  821. 3300000,
  822. };
  823. static const int mt6328_VMC_voltages[] = {
  824. 1800000,
  825. 2900000,
  826. 3000000,
  827. 3300000,
  828. };
  829. static const int mt6328_VMC_E1_1_voltages[] = {
  830. 2500000,
  831. 2900000,
  832. 3000000,
  833. 3300000,
  834. };
  835. static const int mt6328_VMC_E1_2_voltages[] = {
  836. 1300000,
  837. 1800000,
  838. 2900000,
  839. 3300000,
  840. };
  841. /*VCAM_AF & VIBR*/
  842. static const int mt6328_VCAM_AF_voltages[] = {
  843. 1200000,
  844. 1300000,
  845. 1500000,
  846. 1800000,
  847. 2000000,
  848. 2800000,
  849. 3000000,
  850. 3300000,
  851. };
  852. static const int mt6328_VGP1_voltages[] = {
  853. 1200000,
  854. 1300000,
  855. 1500000,
  856. 1800000,
  857. 2500000,
  858. 2800000,
  859. 3000000,
  860. 3300000,
  861. };
  862. static const int mt6328_VCAMD_voltages[] = {
  863. 900000,
  864. 1000000,
  865. 1100000,
  866. 1200000,
  867. 1300000,
  868. 1500000,
  869. };
  870. static const int mt6328_VRF18_1_voltages[] = {
  871. 1200000,
  872. 1300000,
  873. 1500000,
  874. 1800000,
  875. 1825000,
  876. };
  877. static const int mt6328_VCAM_IO_voltages[] = {
  878. 1200000,
  879. 1300000,
  880. 1500000,
  881. 1800000,
  882. };
  883. static const int mt6328_VM_voltages[] = {
  884. 1240000,
  885. 1390000,
  886. 1540000,
  887. };
  888. static const int mt6328_1v8_voltages[] = {
  889. 1800000,
  890. };
  891. static const int mt6328_2v8_voltages[] = {
  892. 2800000,
  893. };
  894. static const int mt6328_3v3_voltages[] = {
  895. 3300000,
  896. };
  897. static const int mt6328_1v825_voltages[] = {
  898. 1825000,
  899. };
  900. struct mtk_regulator mtk_ldos[] = {
  901. PMIC_LDO_GEN1(VAUX18, PMIC_RG_VAUX18_EN, NULL, mt6328_1v8_voltages, 0, PMIC_EN),
  902. PMIC_LDO_GEN1(VTCXO_0, PMIC_RG_VTCXO_0_EN, NULL, mt6328_2v8_voltages, 0, PMIC_EN),
  903. PMIC_LDO_GEN1(VTCXO_1, PMIC_RG_VTCXO_1_EN, NULL, mt6328_2v8_voltages, 0, PMIC_EN),
  904. PMIC_LDO_GEN1(VAUD28, PMIC_RG_VAUD28_EN, NULL, mt6328_2v8_voltages, 1, PMIC_EN),
  905. PMIC_LDO_GEN1(VCN28, PMIC_RG_VCN28_EN, NULL, mt6328_2v8_voltages, 1, PMIC_EN),
  906. PMIC_LDO_GEN1(VCAMA, PMIC_RG_VCAMA_EN, PMIC_RG_VCAMA_VOSEL, mt6328_VCAMA_voltages, 1,
  907. PMIC_EN_VOL),
  908. PMIC_LDO_GEN1(VCN33_BT, PMIC_RG_VCN33_EN_BT, PMIC_RG_VCN33_VOSEL, mt6328_VCN33_voltages, 1,
  909. PMIC_EN_VOL),
  910. PMIC_LDO_GEN1(VCN33_WIFI, PMIC_RG_VCN33_EN_WIFI, PMIC_RG_VCN33_VOSEL, mt6328_VCN33_voltages,
  911. 1, PMIC_EN_VOL),
  912. PMIC_LDO_GEN1(VUSB33, PMIC_RG_VUSB33_EN, NULL, mt6328_3v3_voltages, 1, PMIC_EN),
  913. PMIC_LDO_GEN1(VEFUSE, PMIC_RG_VEFUSE_EN, PMIC_RG_VEFUSE_VOSEL, mt6328_VEFUSE_voltages, 0,
  914. PMIC_EN_VOL),
  915. PMIC_LDO_GEN1(VSIM1, PMIC_RG_VSIM1_EN, PMIC_RG_VSIM1_VOSEL, mt6328_VSIM1_voltages, 0,
  916. PMIC_EN_VOL),
  917. PMIC_LDO_GEN1(VSIM2, PMIC_RG_VSIM2_EN, PMIC_RG_VSIM2_VOSEL, mt6328_VSIM1_voltages, 0,
  918. PMIC_EN_VOL),
  919. PMIC_LDO_GEN1(VEMC_3V3, PMIC_RG_VEMC_3V3_EN, PMIC_RG_VEMC_3V3_VOSEL, mt6328_VEMC33_voltages,
  920. 1, PMIC_EN_VOL),
  921. PMIC_LDO_GEN1(VMCH, PMIC_RG_VMCH_EN, PMIC_RG_VMCH_VOSEL, mt6328_VMCH_voltages, 1,
  922. PMIC_EN_VOL),
  923. PMIC_LDO_GEN1(VTREF, PMIC_RG_TREF_EN, NULL, mt6328_1v8_voltages, 0, PMIC_EN),
  924. PMIC_LDO_GEN1(VMC, PMIC_RG_VMC_EN, PMIC_RG_VMC_VOSEL, mt6328_VMC_voltages, 1, PMIC_EN_VOL),
  925. PMIC_LDO_GEN1(VCAMAF, PMIC_RG_VCAMAF_EN, PMIC_RG_VCAMAF_VOSEL, mt6328_VCAM_AF_voltages, 1,
  926. PMIC_EN_VOL),
  927. PMIC_LDO_GEN1(VIO28, PMIC_RG_VIO28_EN, NULL, mt6328_2v8_voltages, 0, PMIC_EN),
  928. PMIC_LDO_GEN1(VGP1, PMIC_RG_VGP1_EN, PMIC_RG_VGP1_VOSEL, mt6328_VGP1_voltages, 1,
  929. PMIC_EN_VOL),
  930. PMIC_LDO_GEN1(VIBR, PMIC_RG_VIBR_EN, PMIC_RG_VIBR_VOSEL, mt6328_VCAM_AF_voltages, 1,
  931. PMIC_EN_VOL),
  932. PMIC_LDO_GEN1(VCAMD, PMIC_RG_VCAMD_EN, PMIC_RG_VCAMD_VOSEL, mt6328_VCAMD_voltages, 1,
  933. PMIC_EN_VOL),
  934. PMIC_LDO_GEN1(VRF18_0, PMIC_RG_VRF18_0_EN, NULL, mt6328_1v825_voltages, 0, PMIC_EN),
  935. PMIC_LDO_GEN1(VRF18_1, PMIC_RG_VRF18_1_EN, PMIC_RG_VRF18_1_VOSEL, mt6328_VRF18_1_voltages,
  936. 1, PMIC_EN_VOL),
  937. PMIC_LDO_GEN1(VIO18, PMIC_RG_VIO18_EN, NULL, mt6328_1v8_voltages, 0, PMIC_EN),
  938. PMIC_LDO_GEN1(VCN18, PMIC_RG_VCN18_EN, NULL, mt6328_1v8_voltages, 1, PMIC_EN),
  939. PMIC_LDO_GEN1(VCAMIO, PMIC_RG_VCAMIO_EN, PMIC_RG_VCAMIO_VOSEL, mt6328_VCAM_IO_voltages, 1,
  940. PMIC_EN_VOL),
  941. PMIC_LDO_GEN2(VSRAM, PMIC_RG_VSRAM_EN, PMIC_RG_VSRAM_VOSEL, 700000, 1493750, 6250, 1,
  942. PMIC_EN_VOL),
  943. PMIC_LDO_GEN1(VM, PMIC_RG_VM_EN, PMIC_RG_VM_VOSEL, mt6328_VM_voltages, 1, PMIC_EN_VOL),
  944. };
  945. static struct mtk_regulator mtk_bucks[] = {
  946. PMIC_BUCK_GEN(VPA, PMIC_QI_VPA_EN, PMIC_NI_VPA_VOSEL, 500000, 3650000, 50000),
  947. PMIC_BUCK_GEN(VPROC, PMIC_QI_VPROC_EN, PMIC_NI_VPROC_VOSEL, 600000, 1393750, 6250),
  948. PMIC_BUCK_GEN(VCORE1, PMIC_QI_VCORE1_EN, PMIC_NI_VCORE1_VOSEL, 600000, 1393750, 6250),
  949. PMIC_BUCK_GEN(VSYS22, PMIC_QI_VSYS22_EN, PMIC_NI_VSYS22_VOSEL, 1200000, 1993750, 6250),
  950. PMIC_BUCK_GEN(VLTE, PMIC_QI_VLTE_EN, PMIC_NI_VLTE_VOSEL, 600000, 1393750, 6250),
  951. };
  952. #if !defined CONFIG_MTK_LEGACY
  953. #ifdef CONFIG_OF
  954. /*
  955. #define PMIC_REGULATOR_OF_MATCH(_name, _id) \
  956. [MT6328_POWER_LDO_##_id] = { \
  957. .name = #_name, \
  958. .driver_data = &mtk_ldos[MT6328_POWER_LDO_##_id], \
  959. }
  960. */
  961. #define PMIC_REGULATOR_OF_MATCH(_name, _id) \
  962. { \
  963. .name = #_name, \
  964. .driver_data = &mtk_ldos[MT6328_POWER_LDO_##_id], \
  965. }
  966. static struct of_regulator_match pmic_regulator_matches[] = {
  967. PMIC_REGULATOR_OF_MATCH(ldo_vaux18, VAUX18),
  968. PMIC_REGULATOR_OF_MATCH(ldo_vtcxo_0, VTCXO_0),
  969. PMIC_REGULATOR_OF_MATCH(ldo_vtcxo_1, VTCXO_1),
  970. PMIC_REGULATOR_OF_MATCH(ldo_vaud28, VAUD28),
  971. PMIC_REGULATOR_OF_MATCH(ldo_vcn28, VCN28),
  972. PMIC_REGULATOR_OF_MATCH(ldo_vcama, VCAMA),
  973. PMIC_REGULATOR_OF_MATCH(ldo_vcn33_bt, VCN33_BT),
  974. PMIC_REGULATOR_OF_MATCH(ldo_vcn33_wifi, VCN33_WIFI),
  975. PMIC_REGULATOR_OF_MATCH(ldo_vusb33, VUSB33),
  976. PMIC_REGULATOR_OF_MATCH(ldo_vefuse, VEFUSE),
  977. PMIC_REGULATOR_OF_MATCH(ldo_vsim1, VSIM1),
  978. PMIC_REGULATOR_OF_MATCH(ldo_vsim2, VSIM2),
  979. PMIC_REGULATOR_OF_MATCH(ldo_vemc_3v3, VEMC_3V3),
  980. PMIC_REGULATOR_OF_MATCH(ldo_vmch, VMCH),
  981. PMIC_REGULATOR_OF_MATCH(ldo_vtref, VTREF),
  982. PMIC_REGULATOR_OF_MATCH(ldo_vmc, VMC),
  983. PMIC_REGULATOR_OF_MATCH(ldo_vcamaf, VCAMAF),
  984. PMIC_REGULATOR_OF_MATCH(ldo_vio28, VIO28),
  985. PMIC_REGULATOR_OF_MATCH(ldo_vgp1, VGP1),
  986. PMIC_REGULATOR_OF_MATCH(ldo_vibr, VIBR),
  987. PMIC_REGULATOR_OF_MATCH(ldo_vcamd, VCAMD),
  988. PMIC_REGULATOR_OF_MATCH(ldo_vrf18_0, VRF18_0),
  989. PMIC_REGULATOR_OF_MATCH(ldo_vrf18_1, VRF18_1),
  990. PMIC_REGULATOR_OF_MATCH(ldo_vio18, VIO18),
  991. PMIC_REGULATOR_OF_MATCH(ldo_vcn18, VCN18),
  992. PMIC_REGULATOR_OF_MATCH(ldo_vcamio, VCAMIO),
  993. PMIC_REGULATOR_OF_MATCH(ldo_vsram, VSRAM),
  994. PMIC_REGULATOR_OF_MATCH(ldo_vm, VM),
  995. };
  996. #endif /* End of #ifdef CONFIG_OF */
  997. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  998. #if !defined CONFIG_MTK_LEGACY
  999. #ifdef CONFIG_OF
  1000. struct platform_device mt_pmic_device = {
  1001. .name = "pmic_regulator",
  1002. .id = -1,
  1003. };
  1004. static const struct platform_device_id pmic_regulator_id[] = {
  1005. {"pmic_regulator", 0},
  1006. {},
  1007. };
  1008. static const struct of_device_id pmic_cust_of_ids[] = {
  1009. {.compatible = "mediatek,mt6328",},
  1010. {},
  1011. };
  1012. MODULE_DEVICE_TABLE(of, pmic_cust_of_ids);
  1013. static int pmic_regulator_ldo_init(struct platform_device *pdev)
  1014. {
  1015. struct device_node *np, *regulators;
  1016. int matched, i = 0, ret;
  1017. pdev->dev.of_node = of_find_compatible_node(NULL, NULL, "mediatek,mt_pmic");
  1018. np = of_node_get(pdev->dev.of_node);
  1019. if (!np)
  1020. return -EINVAL;
  1021. regulators = of_get_child_by_name(np, "ldo_regulators");
  1022. if (!regulators) {
  1023. PMICLOG("[PMIC]regulators node not found\n");
  1024. ret = -EINVAL;
  1025. goto out;
  1026. }
  1027. matched = of_regulator_match(&pdev->dev, regulators,
  1028. pmic_regulator_matches, ARRAY_SIZE(pmic_regulator_matches));
  1029. if ((matched < 0) || (matched != MT65XX_POWER_COUNT_END)) {
  1030. pr_warn("[PMIC]Error parsing regulator init data: %d %d\n", matched,
  1031. MT65XX_POWER_COUNT_END);
  1032. return matched;
  1033. }
  1034. for (i = 0; i < ARRAY_SIZE(pmic_regulator_matches); i++) {
  1035. if (mtk_ldos[i].isUsedable == 1) {
  1036. mtk_ldos[i].config.dev = &(pdev->dev);
  1037. mtk_ldos[i].config.init_data = pmic_regulator_matches[i].init_data;
  1038. mtk_ldos[i].config.of_node = pmic_regulator_matches[i].of_node;
  1039. mtk_ldos[i].config.driver_data = pmic_regulator_matches[i].driver_data;
  1040. mtk_ldos[i].desc.owner = THIS_MODULE;
  1041. mtk_ldos[i].rdev =
  1042. regulator_register(&mtk_ldos[i].desc, &mtk_ldos[i].config);
  1043. if (IS_ERR(mtk_ldos[i].rdev)) {
  1044. ret = PTR_ERR(mtk_ldos[i].rdev);
  1045. pr_warn("[regulator_register] failed to register %s (%d)\n",
  1046. mtk_ldos[i].desc.name, ret);
  1047. } else {
  1048. PMICLOG("[regulator_register] pass to register %s\n",
  1049. mtk_ldos[i].desc.name);
  1050. }
  1051. PMICLOG("[PMIC]mtk_ldos[%d].config.init_data min_uv:%d max_uv:%d\n", i,
  1052. mtk_ldos[i].config.init_data->constraints.min_uV,
  1053. mtk_ldos[i].config.init_data->constraints.max_uV);
  1054. }
  1055. }
  1056. of_node_put(regulators);
  1057. return 0;
  1058. out:
  1059. of_node_put(np);
  1060. return ret;
  1061. }
  1062. static int pmic_mt_cust_probe(struct platform_device *pdev)
  1063. {
  1064. struct device_node *np, *nproot, *regulators, *child;
  1065. #ifdef non_ks
  1066. const struct of_device_id *match;
  1067. #endif
  1068. int ret;
  1069. unsigned int i = 0, default_on;
  1070. PMICLOG("[PMIC]pmic_mt_cust_probe %s %s\n", pdev->name, pdev->id_entry->name);
  1071. #ifdef non_ks
  1072. /* check if device_id is matched */
  1073. match = of_match_device(pmic_cust_of_ids, &pdev->dev);
  1074. if (!match) {
  1075. pr_warn("[PMIC]pmic_cust_of_ids do not matched\n");
  1076. return -EINVAL;
  1077. }
  1078. /* check customer setting */
  1079. nproot = of_find_compatible_node(NULL, NULL, "mediatek,mt6328");
  1080. if (nproot == NULL) {
  1081. pr_info("[PMIC]pmic_mt_cust_probe get node failed\n");
  1082. return -ENOMEM;
  1083. }
  1084. np = of_node_get(nproot);
  1085. if (!np) {
  1086. pr_info("[PMIC]pmic_mt_cust_probe of_node_get fail\n");
  1087. return -ENODEV;
  1088. }
  1089. regulators = of_find_node_by_name(np, "regulators");
  1090. if (!regulators) {
  1091. pr_info("[PMIC]failed to find regulators node\n");
  1092. ret = -ENODEV;
  1093. goto out;
  1094. }
  1095. for_each_child_of_node(regulators, child) {
  1096. /* check ldo regualtors and set it */
  1097. for (i = 0; i < ARRAY_SIZE(pmic_regulator_matches); i++) {
  1098. /* compare dt name & ldos name */
  1099. if (!of_node_cmp(child->name, pmic_regulator_matches[i].name)) {
  1100. PMICLOG("[PMIC]%s regulator_matches %s\n", child->name,
  1101. (char *)of_get_property(child, "regulator-name", NULL));
  1102. break;
  1103. }
  1104. }
  1105. if (i == ARRAY_SIZE(pmic_regulator_matches))
  1106. continue;
  1107. if (!of_property_read_u32(child, "regulator-default-on", &default_on)) {
  1108. switch (default_on) {
  1109. case 0:
  1110. /* skip */
  1111. PMICLOG("[PMIC]%s regulator_skip %s\n", child->name,
  1112. pmic_regulator_matches[i].name);
  1113. break;
  1114. case 1:
  1115. /* turn ldo off */
  1116. pmic_set_register_value(mtk_ldos[i].en_reg, false);
  1117. PMICLOG("[PMIC]%s default is off\n",
  1118. (char *)of_get_property(child, "regulator-name", NULL));
  1119. break;
  1120. case 2:
  1121. /* turn ldo on */
  1122. pmic_set_register_value(mtk_ldos[i].en_reg, true);
  1123. PMICLOG("[PMIC]%s default is on\n",
  1124. (char *)of_get_property(child, "regulator-name", NULL));
  1125. break;
  1126. default:
  1127. break;
  1128. }
  1129. }
  1130. }
  1131. of_node_put(regulators);
  1132. PMICLOG("[PMIC]pmic_mt_cust_probe done\n");
  1133. return 0;
  1134. #else
  1135. nproot = of_find_compatible_node(NULL, NULL, "mediatek,mt_pmic");
  1136. if (nproot == NULL) {
  1137. pr_info("[PMIC]pmic_mt_cust_probe get node failed\n");
  1138. return -ENOMEM;
  1139. }
  1140. np = of_node_get(nproot);
  1141. if (!np) {
  1142. pr_info("[PMIC]pmic_mt_cust_probe of_node_get fail\n");
  1143. return -ENODEV;
  1144. }
  1145. regulators = of_get_child_by_name(np, "ldo_regulators");
  1146. if (!regulators) {
  1147. PMICLOG("[PMIC]pmic_mt_cust_probe ldo regulators node not found\n");
  1148. ret = -ENODEV;
  1149. goto out;
  1150. }
  1151. for_each_child_of_node(regulators, child) {
  1152. /* check ldo regualtors and set it */
  1153. if (!of_property_read_u32(child, "regulator-default-on", &default_on)) {
  1154. switch (default_on) {
  1155. case 0:
  1156. /* skip */
  1157. PMICLOG("[PMIC]%s regulator_skip %s\n", child->name,
  1158. pmic_regulator_matches[i].name);
  1159. break;
  1160. case 1:
  1161. /* turn ldo off */
  1162. pmic_set_register_value(mtk_ldos[i].en_reg, false);
  1163. PMICLOG("[PMIC]%s default is off\n",
  1164. (char *)of_get_property(child, "regulator-name", NULL));
  1165. break;
  1166. case 2:
  1167. /* turn ldo on */
  1168. pmic_set_register_value(mtk_ldos[i].en_reg, true);
  1169. PMICLOG("[PMIC]%s default is on\n",
  1170. (char *)of_get_property(child, "regulator-name", NULL));
  1171. break;
  1172. default:
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. of_node_put(regulators);
  1178. pr_err("[PMIC]pmic_mt_cust_probe done\n");
  1179. return 0;
  1180. #endif
  1181. out:
  1182. of_node_put(np);
  1183. return ret;
  1184. }
  1185. static int pmic_mt_cust_remove(struct platform_device *pdev)
  1186. {
  1187. /*platform_driver_unregister(&mt_pmic_driver); */
  1188. return 0;
  1189. }
  1190. static struct platform_driver mt_pmic_driver = {
  1191. .driver = {
  1192. .name = "pmic_regulator",
  1193. .owner = THIS_MODULE,
  1194. .of_match_table = pmic_cust_of_ids,
  1195. },
  1196. .probe = pmic_mt_cust_probe,
  1197. .remove = pmic_mt_cust_remove,
  1198. /* .id_table = pmic_regulator_id,*/
  1199. };
  1200. #endif /* End of #ifdef CONFIG_OF */
  1201. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  1202. void mtk_regulator_init(struct platform_device *dev)
  1203. {
  1204. #if defined CONFIG_MTK_LEGACY
  1205. int i = 0;
  1206. int ret = 0;
  1207. int isEn = 0;
  1208. #endif
  1209. /*workaround for VMC voltage */
  1210. if (pmic_get_register_value(PMIC_SWCID) == PMIC6328_E1_CID_CODE) {
  1211. if (pmic_read_VMC_efuse() != 0) {
  1212. PMICLOG("VMC voltage use E1_2 voltage table\n");
  1213. mtk_ldos[MT6328_POWER_LDO_VMC].pvoltages = (void *)mt6328_VMC_E1_2_voltages;
  1214. } else {
  1215. PMICLOG("VMC voltage use E1_1 voltage table\n");
  1216. mtk_ldos[MT6328_POWER_LDO_VMC].pvoltages = (void *)mt6328_VMC_E1_1_voltages;
  1217. }
  1218. }
  1219. /*workaround for VIO18*/
  1220. vio18_cal = pmic_get_register_value(PMIC_RG_VIO18_CAL);
  1221. vmc_cal = pmic_get_register_value(PMIC_RG_VMC_CAL);
  1222. vcamd_cal = pmic_get_register_value(PMIC_RG_VCAMD_CAL);
  1223. #if !defined CONFIG_MTK_LEGACY
  1224. #ifdef CONFIG_OF
  1225. pmic_regulator_ldo_init(dev);
  1226. #endif
  1227. #else
  1228. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  1229. if (mtk_ldos[i].isUsedable == 1) {
  1230. mtk_ldos[i].config.dev = &(dev->dev);
  1231. mtk_ldos[i].config.init_data = &mtk_ldos[i].init_data;
  1232. if (mtk_ldos[i].desc.n_voltages != 1) {
  1233. const int *pVoltage;
  1234. if (mtk_ldos[i].vol_reg != 0) {
  1235. if (mtk_ldos[i].pvoltages != NULL) {
  1236. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  1237. mtk_ldos[i].init_data.constraints.max_uV =
  1238. pVoltage[mtk_ldos[i].desc.n_voltages - 1];
  1239. mtk_ldos[i].init_data.constraints.min_uV =
  1240. pVoltage[0];
  1241. } else {
  1242. mtk_ldos[i].init_data.constraints.max_uV =
  1243. (mtk_ldos[i].desc.n_voltages -
  1244. 1) * mtk_ldos[i].desc.uV_step +
  1245. mtk_ldos[i].desc.min_uV;
  1246. mtk_ldos[i].init_data.constraints.min_uV =
  1247. mtk_ldos[i].desc.min_uV;
  1248. PMICLOG("test man_uv:%d min_uv:%d\n",
  1249. (mtk_ldos[i].desc.n_voltages -
  1250. 1) * mtk_ldos[i].desc.uV_step +
  1251. mtk_ldos[i].desc.min_uV,
  1252. mtk_ldos[i].desc.min_uV);
  1253. }
  1254. }
  1255. PMICLOG("min_uv:%d max_uv:%d\n",
  1256. mtk_ldos[i].init_data.constraints.min_uV,
  1257. mtk_ldos[i].init_data.constraints.max_uV);
  1258. }
  1259. mtk_ldos[i].desc.owner = THIS_MODULE;
  1260. mtk_ldos[i].rdev =
  1261. regulator_register(&mtk_ldos[i].desc, &mtk_ldos[i].config);
  1262. if (IS_ERR(mtk_ldos[i].rdev)) {
  1263. ret = PTR_ERR(mtk_ldos[i].rdev);
  1264. PMICLOG("[regulator_register] failed to register %s (%d)\n",
  1265. mtk_ldos[i].desc.name, ret);
  1266. } else {
  1267. PMICLOG("[regulator_register] pass to register %s\n",
  1268. mtk_ldos[i].desc.name);
  1269. }
  1270. mtk_ldos[i].reg = regulator_get(&(dev->dev), mtk_ldos[i].desc.name);
  1271. isEn = regulator_is_enabled(mtk_ldos[i].reg);
  1272. if (isEn != 0) {
  1273. PMICLOG("[regulator] %s is default on\n", mtk_ldos[i].desc.name);
  1274. /*ret=regulator_enable(mtk_ldos[i].reg); */
  1275. }
  1276. }
  1277. }
  1278. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  1279. }
  1280. void pmic6328_regulator_test(void)
  1281. {
  1282. int i = 0, j;
  1283. int ret1, ret2;
  1284. struct regulator *reg;
  1285. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  1286. if (mtk_ldos[i].isUsedable == 1) {
  1287. reg = mtk_ldos[i].reg;
  1288. PMICLOG("[regulator enable test] %s\n", mtk_ldos[i].desc.name);
  1289. ret1 = regulator_enable(reg);
  1290. ret2 = regulator_is_enabled(reg);
  1291. if (ret2 == pmic_get_register_value(mtk_ldos[i].en_reg)) {
  1292. PMICLOG("[enable test pass]\n");
  1293. } else {
  1294. PMICLOG
  1295. ("[enable test fail] ret = %d enable = %d addr:0x%x reg:0x%x\n",
  1296. ret1, ret2, pmu_flags_table[mtk_ldos[i].en_reg].offset,
  1297. pmic_get_register_value(mtk_ldos[i].en_reg));
  1298. }
  1299. ret1 = regulator_disable(reg);
  1300. ret2 = regulator_is_enabled(reg);
  1301. if (ret2 == pmic_get_register_value(mtk_ldos[i].en_reg)) {
  1302. PMICLOG("[disable test pass]\n");
  1303. } else {
  1304. PMICLOG
  1305. ("[disable test fail] ret = %d enable = %d addr:0x%x reg:0x%x\n",
  1306. ret1, ret2, pmu_flags_table[mtk_ldos[i].en_reg].offset,
  1307. pmic_get_register_value(mtk_ldos[i].en_reg));
  1308. }
  1309. }
  1310. }
  1311. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  1312. const int *pVoltage;
  1313. reg = mtk_ldos[i].reg;
  1314. if (mtk_ldos[i].isUsedable == 1) {
  1315. PMICLOG("[regulator voltage test] %s voltage:%d\n",
  1316. mtk_ldos[i].desc.name, mtk_ldos[i].desc.n_voltages);
  1317. if (mtk_ldos[i].pvoltages != NULL) {
  1318. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  1319. for (j = 0; j < mtk_ldos[i].desc.n_voltages; j++) {
  1320. int rvoltage;
  1321. regulator_set_voltage(reg, pVoltage[j], pVoltage[j]);
  1322. rvoltage = regulator_get_voltage(reg);
  1323. if ((j == pmic_get_register_value(mtk_ldos[i].vol_reg))
  1324. && (pVoltage[j] == rvoltage)) {
  1325. PMICLOG
  1326. ("[%d:%d]:pass set_voltage:%d rvoltage:%d\n",
  1327. j,
  1328. pmic_get_register_value(mtk_ldos
  1329. [i].vol_reg),
  1330. pVoltage[j], rvoltage);
  1331. } else {
  1332. PMICLOG
  1333. ("[%d:%d]:fail set_voltage:%d rvoltage:%d\n",
  1334. j,
  1335. pmic_get_register_value(mtk_ldos
  1336. [i].vol_reg),
  1337. pVoltage[j], rvoltage);
  1338. }
  1339. }
  1340. }
  1341. }
  1342. }
  1343. }
  1344. /*#if defined CONFIG_MTK_LEGACY*/
  1345. int getHwVoltage(MT65XX_POWER powerId)
  1346. {
  1347. #if defined CONFIG_MTK_LEGACY
  1348. struct regulator *reg;
  1349. if (powerId >= ARRAY_SIZE(mtk_ldos))
  1350. return -100;
  1351. if (mtk_ldos[powerId].isUsedable != true)
  1352. return -101;
  1353. reg = mtk_ldos[powerId].reg;
  1354. return regulator_get_voltage(reg);
  1355. #else
  1356. return -1;
  1357. #endif
  1358. }
  1359. EXPORT_SYMBOL(getHwVoltage);
  1360. int isHwPowerOn(MT65XX_POWER powerId)
  1361. {
  1362. #if defined CONFIG_MTK_LEGACY
  1363. struct regulator *reg;
  1364. if (powerId >= ARRAY_SIZE(mtk_ldos))
  1365. return -100;
  1366. if (mtk_ldos[powerId].isUsedable != true)
  1367. return -101;
  1368. reg = mtk_ldos[powerId].reg;
  1369. return regulator_is_enabled(reg);
  1370. #else
  1371. return -1;
  1372. #endif
  1373. }
  1374. EXPORT_SYMBOL(isHwPowerOn);
  1375. bool hwPowerOn(MT65XX_POWER powerId, int powerVolt, char *mode_name)
  1376. {
  1377. #if defined CONFIG_MTK_LEGACY
  1378. struct regulator *reg;
  1379. int ret1, ret2;
  1380. if (powerId >= ARRAY_SIZE(mtk_ldos))
  1381. return false;
  1382. if (mtk_ldos[powerId].isUsedable != true)
  1383. return false;
  1384. reg = mtk_ldos[powerId].reg;
  1385. powerVolt = powerVolt * 1000;
  1386. ret2 = regulator_set_voltage(reg, powerVolt, powerVolt);
  1387. if (ret2 != 0) {
  1388. PMICLOG("hwPowerOn:%s can't set the same volt %d again.",
  1389. mtk_ldos[powerId].desc.name, powerVolt);
  1390. PMICLOG("Or please check:%s volt %d is correct.", mtk_ldos[powerId].desc.name,
  1391. powerVolt);
  1392. }
  1393. ret1 = regulator_enable(reg);
  1394. if (ret1 != 0)
  1395. PMICLOG("hwPowerOn:%s enable fail", mtk_ldos[powerId].desc.name);
  1396. PMICLOG("hwPowerOn:%d:%s volt:%d name:%s cnt:%d", powerId, mtk_ldos[powerId].desc.name,
  1397. powerVolt, mode_name, mtk_ldos[powerId].rdev->use_count);
  1398. return true;
  1399. #else
  1400. return false;
  1401. #endif
  1402. }
  1403. EXPORT_SYMBOL(hwPowerOn);
  1404. bool hwPowerDown(MT65XX_POWER powerId, char *mode_name)
  1405. {
  1406. #if defined CONFIG_MTK_LEGACY
  1407. struct regulator *reg;
  1408. int ret1;
  1409. if (powerId >= ARRAY_SIZE(mtk_ldos))
  1410. return false;
  1411. if (mtk_ldos[powerId].isUsedable != true)
  1412. return false;
  1413. reg = mtk_ldos[powerId].reg;
  1414. ret1 = regulator_disable(reg);
  1415. if (ret1 != 0)
  1416. PMICLOG("hwPowerOn err:ret1:%d ", ret1);
  1417. PMICLOG("hwPowerDown:%d:%s name:%s cnt:%d", powerId, mtk_ldos[powerId].desc.name, mode_name,
  1418. mtk_ldos[powerId].rdev->use_count);
  1419. return true;
  1420. #else
  1421. return false;
  1422. #endif
  1423. }
  1424. EXPORT_SYMBOL(hwPowerDown);
  1425. bool hwPowerSetVoltage(MT65XX_POWER powerId, int powerVolt, char *mode_name)
  1426. {
  1427. #if defined CONFIG_MTK_LEGACY
  1428. struct regulator *reg;
  1429. int ret1;
  1430. if (powerId >= ARRAY_SIZE(mtk_ldos))
  1431. return false;
  1432. reg = mtk_ldos[powerId].reg;
  1433. powerVolt = powerVolt * 1000;
  1434. ret1 = regulator_set_voltage(reg, powerVolt, powerVolt);
  1435. if (ret1 != 0) {
  1436. PMICLOG("hwPowerSetVoltage:%s can't set the same voltage %d",
  1437. mtk_ldos[powerId].desc.name, powerVolt);
  1438. }
  1439. PMICLOG("hwPowerSetVoltage:%d:%s name:%s cnt:%d", powerId, mtk_ldos[powerId].desc.name,
  1440. mode_name, mtk_ldos[powerId].rdev->use_count);
  1441. return true;
  1442. #else
  1443. return false;
  1444. #endif
  1445. }
  1446. EXPORT_SYMBOL(hwPowerSetVoltage);
  1447. /*#endif*//* End of #if defined CONFIG_MTK_LEGACY */
  1448. /*****************************************************************************
  1449. * Low battery call back function
  1450. ******************************************************************************/
  1451. #define LBCB_NUM 16
  1452. #ifndef DISABLE_LOW_BATTERY_PROTECT
  1453. #define LOW_BATTERY_PROTECT
  1454. #endif
  1455. int g_lowbat_int_bottom = 0;
  1456. #ifdef LOW_BATTERY_PROTECT
  1457. /* ex. 3400/5400*4096*/
  1458. #define BAT_HV_THD (POWER_INT0_VOLT*4096/5400) /*ex: 3400mV */
  1459. #define BAT_LV_1_THD (POWER_INT1_VOLT*4096/5400) /*ex: 3250mV */
  1460. #define BAT_LV_2_THD (POWER_INT2_VOLT*4096/5400) /*ex: 3000mV */
  1461. #define BAT_LV_3_THD (2900*4096/5400) /*ex: 3000mV */
  1462. int g_low_battery_level = 0;
  1463. int g_low_battery_stop = 0;
  1464. struct low_battery_callback_table {
  1465. void *lbcb;
  1466. };
  1467. struct low_battery_callback_table lbcb_tb[] = {
  1468. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL},
  1469. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}
  1470. };
  1471. #endif
  1472. void (*low_battery_callback)(LOW_BATTERY_LEVEL);
  1473. void register_low_battery_notify(void (*low_battery_callback) (LOW_BATTERY_LEVEL),
  1474. LOW_BATTERY_PRIO prio_val)
  1475. {
  1476. #ifdef LOW_BATTERY_PROTECT
  1477. PMICLOG("[register_low_battery_notify] start\n");
  1478. lbcb_tb[prio_val].lbcb = low_battery_callback;
  1479. pr_err("[register_low_battery_notify] prio_val=%d\n", prio_val);
  1480. #endif /*end of #ifdef LOW_BATTERY_PROTECT */
  1481. }
  1482. #ifdef LOW_BATTERY_PROTECT
  1483. void exec_low_battery_callback(LOW_BATTERY_LEVEL low_battery_level)
  1484. { /*0:no limit */
  1485. int i = 0;
  1486. if (g_low_battery_stop == 1) {
  1487. pr_err("[exec_low_battery_callback] g_low_battery_stop=%d\n", g_low_battery_stop);
  1488. } else {
  1489. for (i = 0; i < LBCB_NUM; i++) {
  1490. if (lbcb_tb[i].lbcb != NULL) {
  1491. low_battery_callback = lbcb_tb[i].lbcb;
  1492. low_battery_callback(low_battery_level);
  1493. pr_debug
  1494. ("[exec_low_battery_callback] prio_val=%d,low_battery=%d\n",
  1495. i, low_battery_level);
  1496. }
  1497. }
  1498. }
  1499. }
  1500. void lbat_min_en_setting(int en_val)
  1501. {
  1502. pmic_set_register_value(PMIC_AUXADC_LBAT_EN_MIN, en_val);
  1503. pmic_set_register_value(PMIC_AUXADC_LBAT_IRQ_EN_MIN, en_val);
  1504. pmic_set_register_value(PMIC_RG_INT_EN_BAT_L, en_val);
  1505. }
  1506. void lbat_max_en_setting(int en_val)
  1507. {
  1508. pmic_set_register_value(PMIC_AUXADC_LBAT_EN_MAX, en_val);
  1509. pmic_set_register_value(PMIC_AUXADC_LBAT_IRQ_EN_MAX, en_val);
  1510. pmic_set_register_value(PMIC_RG_INT_EN_BAT_H, en_val);
  1511. }
  1512. void low_battery_protect_init(void)
  1513. {
  1514. /*default setting */
  1515. pmic_set_register_value(PMIC_AUXADC_LBAT_DEBT_MIN, 0);
  1516. pmic_set_register_value(PMIC_AUXADC_LBAT_DEBT_MAX, 0);
  1517. pmic_set_register_value(PMIC_AUXADC_LBAT_DET_PRD_15_0, 1);
  1518. pmic_set_register_value(PMIC_AUXADC_LBAT_DET_PRD_19_16, 0);
  1519. pmic_set_register_value(PMIC_AUXADC_LBAT_VOLT_MAX, BAT_HV_THD);
  1520. pmic_set_register_value(PMIC_AUXADC_LBAT_VOLT_MIN, BAT_LV_1_THD);
  1521. lbat_min_en_setting(1);
  1522. lbat_max_en_setting(0);
  1523. pr_err("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1524. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  1525. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  1526. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  1527. );
  1528. pr_err("[low_battery_protect_init] %d mV, %d mV, %d mV\n",
  1529. POWER_INT0_VOLT, POWER_INT1_VOLT, POWER_INT2_VOLT);
  1530. PMICLOG("[low_battery_protect_init] Done\n");
  1531. }
  1532. #endif /*#ifdef LOW_BATTERY_PROTECT */
  1533. void bat_h_int_handler(void)
  1534. {
  1535. g_lowbat_int_bottom = 0;
  1536. PMICLOG("[bat_h_int_handler]....\n");
  1537. /*sub-task */
  1538. #ifdef LOW_BATTERY_PROTECT
  1539. g_low_battery_level = 0;
  1540. exec_low_battery_callback(LOW_BATTERY_LEVEL_0);
  1541. #if 0
  1542. lbat_max_en_setting(0);
  1543. mdelay(1);
  1544. lbat_min_en_setting(1);
  1545. #else
  1546. pmic_set_register_value(PMIC_AUXADC_LBAT_VOLT_MIN, BAT_LV_1_THD);
  1547. lbat_min_en_setting(0);
  1548. lbat_max_en_setting(0);
  1549. mdelay(1);
  1550. lbat_min_en_setting(1);
  1551. #endif
  1552. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1553. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  1554. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  1555. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  1556. );
  1557. #endif
  1558. }
  1559. void bat_l_int_handler(void)
  1560. {
  1561. PMICLOG("[bat_l_int_handler]....\n");
  1562. /*sub-task */
  1563. #ifdef LOW_BATTERY_PROTECT
  1564. g_low_battery_level++;
  1565. if (g_low_battery_level > 2)
  1566. g_low_battery_level = 2;
  1567. if (g_low_battery_level == 1)
  1568. exec_low_battery_callback(LOW_BATTERY_LEVEL_1);
  1569. else if (g_low_battery_level == 2) {
  1570. exec_low_battery_callback(LOW_BATTERY_LEVEL_2);
  1571. g_lowbat_int_bottom = 1;
  1572. } else
  1573. PMICLOG("[bat_l_int_handler]err,g_low_battery_level=%d\n", g_low_battery_level);
  1574. #if 0
  1575. lbat_min_en_setting(0);
  1576. mdelay(1);
  1577. lbat_max_en_setting(1);
  1578. #else
  1579. pmic_set_register_value(PMIC_AUXADC_LBAT_VOLT_MIN, BAT_LV_2_THD);
  1580. lbat_min_en_setting(0);
  1581. lbat_max_en_setting(0);
  1582. mdelay(1);
  1583. if (g_low_battery_level < 2) {
  1584. lbat_min_en_setting(1);
  1585. } else if (g_low_battery_level == 2) {
  1586. pmic_set_register_value(PMIC_AUXADC_LBAT_DEBT_MIN, 500);
  1587. pmic_set_register_value(PMIC_AUXADC_LBAT_VOLT_MIN, BAT_LV_3_THD);
  1588. }
  1589. lbat_max_en_setting(1);
  1590. #endif
  1591. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1592. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  1593. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  1594. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  1595. );
  1596. #endif
  1597. }
  1598. /*****************************************************************************
  1599. * Battery OC call back function
  1600. ******************************************************************************/
  1601. #define OCCB_NUM 16
  1602. #ifndef DISABLE_BATTERY_OC_PROTECT
  1603. #define BATTERY_OC_PROTECT
  1604. #endif
  1605. #ifdef BATTERY_OC_PROTECT
  1606. /* ex. Ireg = 65535 - (I * 950000uA / 2 / 158.122 / CAR_TUNE_VALUE * 100)*/
  1607. /* (950000/2/158.122)*100~=300400*/
  1608. #if defined(CONFIG_MTK_SMART_BATTERY)
  1609. #define BAT_OC_H_THD \
  1610. (65535-((300400*POWER_BAT_OC_CURRENT_H/1000)/batt_meter_cust_data.car_tune_value)) /*ex: 4670mA */
  1611. #define BAT_OC_L_THD \
  1612. (65535-((300400*POWER_BAT_OC_CURRENT_L/1000)/batt_meter_cust_data.car_tune_value)) /*ex: 5500mA */
  1613. #define BAT_OC_H_THD_RE \
  1614. (65535-((300400*POWER_BAT_OC_CURRENT_H_RE/1000)/batt_meter_cust_data.car_tune_value)) /*ex: 3400mA */
  1615. #define BAT_OC_L_THD_RE \
  1616. (65535-((300400*POWER_BAT_OC_CURRENT_L_RE/1000)/batt_meter_cust_data.car_tune_value)) /*ex: 4000mA */
  1617. #else
  1618. #define BAT_OC_H_THD 0xc047
  1619. #define BAT_OC_L_THD 0xb4f4
  1620. #define BAT_OC_H_THD_RE 0xc047
  1621. #define BAT_OC_L_THD_RE 0xb4f4
  1622. #endif /* end of #if defined(CONFIG_MTK_SMART_BATTERY) */
  1623. int g_battery_oc_level = 0;
  1624. int g_battery_oc_stop = 0;
  1625. struct battery_oc_callback_table {
  1626. void *occb;
  1627. };
  1628. struct battery_oc_callback_table occb_tb[] = {
  1629. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL},
  1630. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}
  1631. };
  1632. #endif /*end of #ifdef BATTERY_OC_PROTECT */
  1633. void (*battery_oc_callback)(BATTERY_OC_LEVEL);
  1634. void register_battery_oc_notify(void (*battery_oc_callback) (BATTERY_OC_LEVEL),
  1635. BATTERY_OC_PRIO prio_val)
  1636. {
  1637. #ifdef BATTERY_OC_PROTECT
  1638. PMICLOG("[register_battery_oc_notify] start\n");
  1639. occb_tb[prio_val].occb = battery_oc_callback;
  1640. pr_err("[register_battery_oc_notify] prio_val=%d\n", prio_val);
  1641. #endif
  1642. }
  1643. void exec_battery_oc_callback(BATTERY_OC_LEVEL battery_oc_level)
  1644. { /*0:no limit */
  1645. #ifdef BATTERY_OC_PROTECT
  1646. int i = 0;
  1647. if (g_battery_oc_stop == 1) {
  1648. pr_err("[exec_battery_oc_callback] g_battery_oc_stop=%d\n", g_battery_oc_stop);
  1649. } else {
  1650. for (i = 0; i < OCCB_NUM; i++) {
  1651. if (occb_tb[i].occb != NULL) {
  1652. battery_oc_callback = occb_tb[i].occb;
  1653. battery_oc_callback(battery_oc_level);
  1654. pr_debug
  1655. ("[exec_battery_oc_callback] prio_val=%d,battery_oc_level=%d\n",
  1656. i, battery_oc_level);
  1657. }
  1658. }
  1659. }
  1660. #endif
  1661. }
  1662. #ifdef BATTERY_OC_PROTECT
  1663. void bat_oc_h_en_setting(int en_val)
  1664. {
  1665. pmic_set_register_value(PMIC_RG_INT_EN_FG_CUR_H, en_val);
  1666. /* mt6325_upmu_set_rg_int_en_fg_cur_h(en_val); */
  1667. }
  1668. void bat_oc_l_en_setting(int en_val)
  1669. {
  1670. pmic_set_register_value(PMIC_RG_INT_EN_FG_CUR_L, en_val);
  1671. /*mt6325_upmu_set_rg_int_en_fg_cur_l(en_val); */
  1672. }
  1673. void battery_oc_protect_init(void)
  1674. {
  1675. pmic_set_register_value(PMIC_FG_CUR_HTH, BAT_OC_H_THD);
  1676. /*mt6325_upmu_set_fg_cur_hth(BAT_OC_H_THD); */
  1677. pmic_set_register_value(PMIC_FG_CUR_LTH, BAT_OC_L_THD);
  1678. /*mt6325_upmu_set_fg_cur_lth(BAT_OC_L_THD); */
  1679. bat_oc_h_en_setting(0);
  1680. bat_oc_l_en_setting(1);
  1681. pr_err("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1682. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  1683. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  1684. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  1685. );
  1686. pr_err("[battery_oc_protect_init] %d mA, %d mA\n",
  1687. POWER_BAT_OC_CURRENT_H, POWER_BAT_OC_CURRENT_L);
  1688. PMICLOG("[battery_oc_protect_init] Done\n");
  1689. }
  1690. void battery_oc_protect_reinit(void)
  1691. {
  1692. #ifdef BATTERY_OC_PROTECT
  1693. pmic_set_register_value(PMIC_FG_CUR_HTH, BAT_OC_H_THD_RE);
  1694. /*mt6325_upmu_set_fg_cur_hth(BAT_OC_H_THD_RE); */
  1695. pmic_set_register_value(PMIC_FG_CUR_LTH, BAT_OC_L_THD_RE);
  1696. /*mt6325_upmu_set_fg_cur_lth(BAT_OC_L_THD_RE); */
  1697. pr_err("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1698. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  1699. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  1700. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  1701. );
  1702. pr_err("[battery_oc_protect_reinit] %d mA, %d mA\n",
  1703. POWER_BAT_OC_CURRENT_H_RE, POWER_BAT_OC_CURRENT_L_RE);
  1704. pr_err("[battery_oc_protect_reinit] Done\n");
  1705. #else
  1706. pr_warn("[battery_oc_protect_reinit] no define BATTERY_OC_PROTECT\n");
  1707. #endif
  1708. }
  1709. #endif /* #ifdef BATTERY_OC_PROTECT */
  1710. void fg_cur_h_int_handler(void)
  1711. {
  1712. PMICLOG("[fg_cur_h_int_handler]....\n");
  1713. /*sub-task */
  1714. #ifdef BATTERY_OC_PROTECT
  1715. g_battery_oc_level = 0;
  1716. exec_battery_oc_callback(BATTERY_OC_LEVEL_0);
  1717. bat_oc_h_en_setting(0);
  1718. bat_oc_l_en_setting(0);
  1719. mdelay(1);
  1720. bat_oc_l_en_setting(1);
  1721. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1722. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  1723. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  1724. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  1725. );
  1726. #endif
  1727. }
  1728. void fg_cur_l_int_handler(void)
  1729. {
  1730. PMICLOG("[fg_cur_l_int_handler]....\n");
  1731. /*sub-task */
  1732. #ifdef BATTERY_OC_PROTECT
  1733. g_battery_oc_level = 1;
  1734. exec_battery_oc_callback(BATTERY_OC_LEVEL_1);
  1735. bat_oc_h_en_setting(0);
  1736. bat_oc_l_en_setting(0);
  1737. mdelay(1);
  1738. bat_oc_h_en_setting(1);
  1739. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  1740. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  1741. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  1742. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  1743. );
  1744. #endif
  1745. }
  1746. /*****************************************************************************
  1747. * 15% notify service
  1748. ******************************************************************************/
  1749. #ifndef DISABLE_BATTERY_PERCENT_PROTECT
  1750. #define BATTERY_PERCENT_PROTECT
  1751. #endif
  1752. #ifdef BATTERY_PERCENT_PROTECT
  1753. static struct hrtimer bat_percent_notify_timer;
  1754. static struct task_struct *bat_percent_notify_thread;
  1755. static bool bat_percent_notify_flag;
  1756. static DECLARE_WAIT_QUEUE_HEAD(bat_percent_notify_waiter);
  1757. #endif
  1758. #if !defined CONFIG_HAS_WAKELOCKS
  1759. struct wakeup_source bat_percent_notify_lock;
  1760. #else
  1761. struct wake_lock bat_percent_notify_lock;
  1762. #endif
  1763. static DEFINE_MUTEX(bat_percent_notify_mutex);
  1764. #ifdef BATTERY_PERCENT_PROTECT
  1765. /*extern unsigned int bat_get_ui_percentage(void);*/
  1766. #define BPCB_NUM 16
  1767. int g_battery_percent_level = 0;
  1768. int g_battery_percent_stop = 0;
  1769. #define BAT_PERCENT_LINIT 15
  1770. struct battery_percent_callback_table {
  1771. void *bpcb;
  1772. };
  1773. struct battery_percent_callback_table bpcb_tb[] = {
  1774. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL},
  1775. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}
  1776. };
  1777. #endif /* end of #ifdef BATTERY_PERCENT_PROTECT */
  1778. void (*battery_percent_callback)(BATTERY_PERCENT_LEVEL);
  1779. void register_battery_percent_notify(void (*battery_percent_callback) (BATTERY_PERCENT_LEVEL),
  1780. BATTERY_PERCENT_PRIO prio_val)
  1781. {
  1782. #ifdef BATTERY_PERCENT_PROTECT
  1783. PMICLOG("[register_battery_percent_notify] start\n");
  1784. bpcb_tb[prio_val].bpcb = battery_percent_callback;
  1785. pr_err("[register_battery_percent_notify] prio_val=%d\n", prio_val);
  1786. if ((g_battery_percent_stop == 0) && (g_battery_percent_level == 1)) {
  1787. #ifdef DISABLE_DLPT_FEATURE
  1788. pr_err("[register_battery_percent_notify] level l happen\n");
  1789. battery_percent_callback(BATTERY_PERCENT_LEVEL_1);
  1790. #else
  1791. if (prio_val == BATTERY_PERCENT_PRIO_FLASHLIGHT) {
  1792. pr_err("[register_battery_percent_notify at DLPT] level l happen\n");
  1793. battery_percent_callback(BATTERY_PERCENT_LEVEL_1);
  1794. }
  1795. #endif
  1796. }
  1797. #endif /* end of #ifdef BATTERY_PERCENT_PROTECT */
  1798. }
  1799. #ifdef BATTERY_PERCENT_PROTECT
  1800. void exec_battery_percent_callback(BATTERY_PERCENT_LEVEL battery_percent_level)
  1801. { /*0:no limit */
  1802. #ifdef DISABLE_DLPT_FEATURE
  1803. int i = 0;
  1804. #endif
  1805. if (g_battery_percent_stop == 1) {
  1806. pr_err("[%s] g_battery_percent_stop=%d\n", __func__, g_battery_percent_stop);
  1807. } else {
  1808. #ifdef DISABLE_DLPT_FEATURE
  1809. for (i = 0; i < BPCB_NUM; i++) {
  1810. if (bpcb_tb[i].bpcb != NULL) {
  1811. battery_percent_callback = bpcb_tb[i].bpcb;
  1812. battery_percent_callback(battery_percent_level);
  1813. pr_debug("[%s] prio_val=%d,battery_percent_level=%d\n",
  1814. __func__, i, battery_percent_level);
  1815. }
  1816. }
  1817. #else
  1818. if (bpcb_tb[BATTERY_PERCENT_PRIO_FLASHLIGHT].bpcb != NULL) {
  1819. battery_percent_callback = bpcb_tb[BATTERY_PERCENT_PRIO_FLASHLIGHT].bpcb;
  1820. battery_percent_callback(battery_percent_level);
  1821. pr_debug("[%s at DLPT] prio_val=%d,battery_percent_level=%d\n",
  1822. __func__, BATTERY_PERCENT_PRIO_FLASHLIGHT, battery_percent_level);
  1823. } else
  1824. pr_debug
  1825. ("[%s at DLPT] NULL pointer, prio_val=%d,battery_percent_level=%d\n",
  1826. __func__, BATTERY_PERCENT_PRIO_FLASHLIGHT, battery_percent_level);
  1827. #endif
  1828. }
  1829. }
  1830. int bat_percent_notify_handler(void *unused)
  1831. {
  1832. ktime_t ktime;
  1833. int bat_per_val = 0;
  1834. do {
  1835. ktime = ktime_set(10, 0);
  1836. wait_event_interruptible(bat_percent_notify_waiter,
  1837. (bat_percent_notify_flag == true));
  1838. #if !defined CONFIG_HAS_WAKELOCKS
  1839. __pm_stay_awake(&bat_percent_notify_lock);
  1840. #else
  1841. wake_lock(&bat_percent_notify_lock);
  1842. #endif
  1843. mutex_lock(&bat_percent_notify_mutex);
  1844. #if defined(CONFIG_MTK_SMART_BATTERY)
  1845. bat_per_val = bat_get_ui_percentage();
  1846. #endif
  1847. if ((upmu_get_rgs_chrdet() == 0) && (g_battery_percent_level == 0)
  1848. && (bat_per_val <= BAT_PERCENT_LINIT)) {
  1849. g_battery_percent_level = 1;
  1850. exec_battery_percent_callback(BATTERY_PERCENT_LEVEL_1);
  1851. } else if ((g_battery_percent_level == 1) && (bat_per_val > BAT_PERCENT_LINIT)) {
  1852. g_battery_percent_level = 0;
  1853. exec_battery_percent_callback(BATTERY_PERCENT_LEVEL_0);
  1854. } else {
  1855. }
  1856. bat_percent_notify_flag = false;
  1857. PMICLOG("bat_per_level=%d,bat_per_val=%d\n", g_battery_percent_level, bat_per_val);
  1858. mutex_unlock(&bat_percent_notify_mutex);
  1859. #if !defined CONFIG_HAS_WAKELOCKS
  1860. __pm_relax(&bat_percent_notify_lock);
  1861. #else
  1862. wake_unlock(&bat_percent_notify_lock);
  1863. #endif
  1864. hrtimer_start(&bat_percent_notify_timer, ktime, HRTIMER_MODE_REL);
  1865. } while (!kthread_should_stop());
  1866. return 0;
  1867. }
  1868. enum hrtimer_restart bat_percent_notify_task(struct hrtimer *timer)
  1869. {
  1870. bat_percent_notify_flag = true;
  1871. wake_up_interruptible(&bat_percent_notify_waiter);
  1872. PMICLOG("bat_percent_notify_task is called\n");
  1873. return HRTIMER_NORESTART;
  1874. }
  1875. void bat_percent_notify_init(void)
  1876. {
  1877. ktime_t ktime;
  1878. ktime = ktime_set(20, 0);
  1879. hrtimer_init(&bat_percent_notify_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1880. bat_percent_notify_timer.function = bat_percent_notify_task;
  1881. hrtimer_start(&bat_percent_notify_timer, ktime, HRTIMER_MODE_REL);
  1882. bat_percent_notify_thread =
  1883. kthread_run(bat_percent_notify_handler, 0, "bat_percent_notify_thread");
  1884. if (IS_ERR(bat_percent_notify_thread))
  1885. pr_err("Failed to create bat_percent_notify_thread\n");
  1886. else
  1887. pr_err("Create bat_percent_notify_thread : done\n");
  1888. }
  1889. #endif /* #ifdef BATTERY_PERCENT_PROTECT */
  1890. /*****************************************************************************
  1891. * DLPT service
  1892. ******************************************************************************/
  1893. #ifndef DISABLE_DLPT_FEATURE
  1894. #define DLPT_FEATURE_SUPPORT
  1895. #endif
  1896. #ifdef DLPT_FEATURE_SUPPORT
  1897. unsigned int ptim_bat_vol = 0;
  1898. signed int ptim_R_curr = 0;
  1899. int ptim_imix = 0;
  1900. int ptim_rac_val_avg = 0;
  1901. signed int pmic_ptimretest = 0;
  1902. unsigned int ptim_cnt = 0;
  1903. signed int count_time_out_adc_imp = 36;
  1904. unsigned int count_adc_imp = 0;
  1905. int do_ptim(bool isSuspend)
  1906. {
  1907. unsigned int vbat_reg;
  1908. int ret = 0;
  1909. count_adc_imp = 0;
  1910. /*PMICLOG("[do_ptim] start\n"); */
  1911. if (isSuspend == false)
  1912. pmic_auxadc_lock();
  1913. /*pmic_set_register_value(PMIC_RG_AUXADC_RST,1); */
  1914. /*pmic_set_register_value(PMIC_RG_AUXADC_RST,0); */
  1915. upmu_set_reg_value(0x0eac, 0x0006);
  1916. pmic_set_register_value(PMIC_AUXADC_IMP_AUTORPT_PRD, 6);
  1917. pmic_set_register_value(PMIC_RG_AUXADC_SMPS_CK_PDN, 0);
  1918. pmic_set_register_value(PMIC_RG_AUXADC_SMPS_CK_PDN_HWEN, 0);
  1919. pmic_set_register_value(PMIC_RG_AUXADC_CK_PDN_HWEN, 0);
  1920. pmic_set_register_value(PMIC_RG_AUXADC_CK_PDN, 0);
  1921. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 1);
  1922. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 1);
  1923. /*restore to initial state */
  1924. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 0);
  1925. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 0);
  1926. /*set issue interrupt */
  1927. /*pmic_set_register_value(PMIC_RG_INT_EN_AUXADC_IMP,1); */
  1928. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_CHSEL, 0);
  1929. pmic_set_register_value(PMIC_AUXADC_IMP_AUTORPT_EN, 1);
  1930. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_CNT, 3);
  1931. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_MODE, 1);
  1932. /* PMICLOG("[do_ptim] end %d %d\n",pmic_get_register_value
  1933. (PMIC_RG_AUXADC_SMPS_CK_PDN),pmic_get_register_value(
  1934. PMIC_RG_AUXADC_SMPS_CK_PDN_HWEN));*/
  1935. while (pmic_get_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_STATUS) == 0) {
  1936. /*PMICLOG("[do_ptim] PMIC_AUXADC_IMPEDANCE_IRQ_STATUS= %d\n",
  1937. pmic_get_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_STATUS)); */
  1938. if ((count_adc_imp++) > count_time_out_adc_imp) {
  1939. pr_err("do_ptim over %d times/ms\n", count_adc_imp);
  1940. ret = 1;
  1941. break;
  1942. }
  1943. mdelay(1);
  1944. }
  1945. /*disable */
  1946. pmic_set_register_value(PMIC_AUXADC_IMP_AUTORPT_EN, 0); /*typo */
  1947. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_MODE, 0);
  1948. /*clear irq */
  1949. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 1);
  1950. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 1);
  1951. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 0);
  1952. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 0);
  1953. if (isSuspend == false)
  1954. pmic_auxadc_unlock();
  1955. /*PMICLOG("[do_ptim2] 0xee8=0x%x 0x2c6=0x%x\n", upmu_get_reg_value
  1956. (0xee8),upmu_get_reg_value(0x2c6)); */
  1957. /*pmic_set_register_value(PMIC_RG_INT_STATUS_AUXADC_IMP,1);write 1 to clear ! */
  1958. /*pmic_set_register_value(PMIC_RG_INT_EN_AUXADC_IMP,0); */
  1959. vbat_reg = pmic_get_register_value(PMIC_AUXADC_ADC_OUT_IMP_AVG);
  1960. ptim_bat_vol = (vbat_reg * 3 * 18000) / 32768;
  1961. fgauge_read_IM_current((void *)&ptim_R_curr);
  1962. return ret;
  1963. }
  1964. void enable_dummy_load(unsigned int en)
  1965. {
  1966. if (en == 1) {
  1967. /*Enable dummy load-------------------------------------------------- */
  1968. /*mt6325_upmu_set_rg_g_drv_2m_ck_pdn(0); */
  1969. /*mt6325_upmu_set_rg_drv_32k_ck_pdn(0); */
  1970. /*upmu_set_reg_value(0x23c,0xfeb0); */
  1971. pmic_set_register_value(PMIC_RG_DRV_ISINK2_CK_PDN, 0);
  1972. pmic_set_register_value(PMIC_RG_DRV_ISINK3_CK_PDN, 0);
  1973. /*upmu_set_reg_value(0x25a,0x8a00); */
  1974. pmic_set_register_value(PMIC_RG_DRV_ISINK2_CK_CKSEL, 0);
  1975. pmic_set_register_value(PMIC_RG_DRV_ISINK3_CK_CKSEL, 0);
  1976. /*upmu_set_reg_value(0x82a,0x0c00); */
  1977. /*upmu_set_reg_value(0x81c,0x7000); */
  1978. pmic_set_register_value(PMIC_ISINK_CH2_STEP, 0xc);
  1979. /*upmu_set_reg_value(0x81e,0x7000); */
  1980. pmic_set_register_value(PMIC_ISINK_CH3_STEP, 0xc);
  1981. /*upmu_set_reg_value(0x820,0x0300); */
  1982. pmic_set_register_value(PMIC_RG_ISINK2_DOUBLE_EN, 1);
  1983. pmic_set_register_value(PMIC_RG_ISINK3_DOUBLE_EN, 1);
  1984. /*upmu_set_reg_value(0x828,0x0ccc); */
  1985. pmic_set_register_value(PMIC_ISINK_CH2_EN, 1);
  1986. pmic_set_register_value(PMIC_ISINK_CH3_EN, 1);
  1987. pmic_set_register_value(PMIC_ISINK_CHOP2_EN, 1);
  1988. pmic_set_register_value(PMIC_ISINK_CHOP3_EN, 1);
  1989. pmic_set_register_value(PMIC_ISINK_CH2_BIAS_EN, 1);
  1990. pmic_set_register_value(PMIC_ISINK_CH3_BIAS_EN, 1);
  1991. /*pmic_set_register_value(PMIC_RG_VIBR_EN,1); */
  1992. /*PMICLOG("[enable dummy load]\n"); */
  1993. } else {
  1994. /*upmu_set_reg_value(0x828,0x0cc0); */
  1995. pmic_set_register_value(PMIC_ISINK_CH2_EN, 0);
  1996. pmic_set_register_value(PMIC_ISINK_CH3_EN, 0);
  1997. /*pmic_set_register_value(PMIC_RG_VIBR_EN,0); */
  1998. /*PMICLOG("[disable dummy load]\n"); */
  1999. }
  2000. }
  2001. #endif /* #ifdef DLPT_FEATURE_SUPPORT */
  2002. #ifdef DLPT_FEATURE_SUPPORT
  2003. static struct hrtimer dlpt_notify_timer;
  2004. static struct task_struct *dlpt_notify_thread;
  2005. static bool dlpt_notify_flag;
  2006. static DECLARE_WAIT_QUEUE_HEAD(dlpt_notify_waiter);
  2007. #endif
  2008. #if !defined CONFIG_HAS_WAKELOCKS
  2009. struct wakeup_source dlpt_notify_lock;
  2010. #else
  2011. struct wake_lock dlpt_notify_lock;
  2012. #endif
  2013. static DEFINE_MUTEX(dlpt_notify_mutex);
  2014. #ifdef DLPT_FEATURE_SUPPORT
  2015. #define DLPT_NUM 16
  2016. int g_dlpt_stop = 0;
  2017. unsigned int g_dlpt_val = 0;
  2018. int g_dlpt_start = 0;
  2019. int g_imix_val = 0;
  2020. int g_imix_val_pre = 0;
  2021. int g_low_per_timer = 0;
  2022. int g_low_per_timeout_val = 60;
  2023. int g_lbatInt1 = POWER_INT2_VOLT * 10;
  2024. struct dlpt_callback_table {
  2025. void *dlpt_cb;
  2026. };
  2027. struct dlpt_callback_table dlpt_cb_tb[] = {
  2028. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL},
  2029. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}
  2030. };
  2031. void (*dlpt_callback)(unsigned int);
  2032. void register_dlpt_notify(void (*dlpt_callback) (unsigned int), DLPT_PRIO prio_val)
  2033. {
  2034. PMICLOG("[register_dlpt_notify] start\n");
  2035. dlpt_cb_tb[prio_val].dlpt_cb = dlpt_callback;
  2036. pr_err("[register_dlpt_notify] prio_val=%d\n", prio_val);
  2037. if ((g_dlpt_stop == 0) && (g_dlpt_val != 0)) {
  2038. pr_err("[register_dlpt_notify] dlpt happen\n");
  2039. dlpt_callback(g_dlpt_val);
  2040. }
  2041. }
  2042. void exec_dlpt_callback(unsigned int dlpt_val)
  2043. {
  2044. int i = 0;
  2045. g_dlpt_val = dlpt_val;
  2046. if (g_dlpt_stop == 1) {
  2047. pr_err("[exec_dlpt_callback] g_dlpt_stop=%d\n", g_dlpt_stop);
  2048. } else {
  2049. for (i = 0; i < DLPT_NUM; i++) {
  2050. if (dlpt_cb_tb[i].dlpt_cb != NULL) {
  2051. dlpt_callback = dlpt_cb_tb[i].dlpt_cb;
  2052. dlpt_callback(g_dlpt_val);
  2053. pr_debug("[exec_dlpt_callback] g_dlpt_val=%d\n", g_dlpt_val);
  2054. }
  2055. }
  2056. }
  2057. }
  2058. /*
  2059. int get_dlpt_iavg(int is_use_zcv)
  2060. {
  2061. int bat_cap_val = 0;
  2062. int zcv_val = 0;
  2063. int vsys_min_2_val = POWER_INT2_VOLT;
  2064. int rbat_val = 0;
  2065. int rdc_val = 0;
  2066. int iavg_val = 0;
  2067. bat_cap_val = bat_get_ui_percentage();
  2068. if(is_use_zcv == 1)
  2069. zcv_val = fgauge_read_v_by_d(100-bat_cap_val);
  2070. else
  2071. {
  2072. #if defined(SWCHR_POWER_PATH)
  2073. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_ISENSE_AP,5,1);
  2074. #else
  2075. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_BATSNS_AP,5,1);
  2076. #endif
  2077. }
  2078. rbat_val = fgauge_read_r_bat_by_v(zcv_val);
  2079. rdc_val = CUST_R_FG_OFFSET+R_FG_VALUE+rbat_val;
  2080. if(rdc_val==0)
  2081. rdc_val=1;
  2082. iavg_val = ((zcv_val-vsys_min_2_val)*1000)/rdc_val;
  2083. return iavg_val;
  2084. }
  2085. int get_real_volt(int val)*//*0.1mV */
  2086. /*
  2087. {
  2088. int ret = 0;
  2089. ret = val&0x7FFF;
  2090. ret = (ret*4*1800*10)/32768;
  2091. return ret;
  2092. }
  2093. int get_real_curr(int val)*//*0.1mA */
  2094. /*
  2095. {
  2096. int ret=0;
  2097. if ( val > 32767 ) {
  2098. ret = val-65535;
  2099. ret = ret-(ret*2);
  2100. } else
  2101. ret = val;
  2102. ret = ret*158122;
  2103. do_div(ret, 100000);
  2104. ret = (ret*20)/R_FG_VALUE;
  2105. ret = ((ret*CAR_TUNE_VALUE)/100);
  2106. return ret;
  2107. }
  2108. */
  2109. int get_rac_val(void)
  2110. {
  2111. int volt_1 = 0;
  2112. int volt_2 = 0;
  2113. int curr_1 = 0;
  2114. int curr_2 = 0;
  2115. int rac_cal = 0;
  2116. int ret = 0;
  2117. bool retry_state = false;
  2118. int retry_count = 0;
  2119. do {
  2120. /*adc and fg-------------------------------------------------------- */
  2121. do_ptim(KAL_TRUE);
  2122. pmic_spm_crit2("[1,Trigger ADC PTIM mode] volt1=%d, curr_1=%d\n", ptim_bat_vol,
  2123. ptim_R_curr);
  2124. volt_1 = ptim_bat_vol;
  2125. curr_1 = ptim_R_curr;
  2126. pmic_spm_crit2("[2,enable dummy load]");
  2127. enable_dummy_load(1);
  2128. mdelay(50);
  2129. /*Wait -------------------------------------------------------------- */
  2130. /*adc and fg-------------------------------------------------------- */
  2131. do_ptim(KAL_TRUE);
  2132. pmic_spm_crit2("[3,Trigger ADC PTIM mode again] volt2=%d, curr_2=%d\n",
  2133. ptim_bat_vol, ptim_R_curr);
  2134. volt_2 = ptim_bat_vol;
  2135. curr_2 = ptim_R_curr;
  2136. /*Disable dummy load------------------------------------------------- */
  2137. enable_dummy_load(0);
  2138. /*Calculate Rac------------------------------------------------------ */
  2139. if ((curr_2 - curr_1) >= 700 && (curr_2 - curr_1) <= 1200
  2140. && (volt_1 - volt_2) >= 80) {
  2141. /*40.0mA */
  2142. rac_cal = ((volt_1 - volt_2) * 1000) / (curr_2 - curr_1); /*m-ohm */
  2143. if (rac_cal < 0)
  2144. ret = (rac_cal - (rac_cal * 2)) * 1;
  2145. else
  2146. ret = rac_cal * 1;
  2147. } else if ((curr_1 - curr_2) >= 700 &&
  2148. ((curr_1 - curr_2) <= 1200) && ((volt_2 - volt_1) >= 80)) {
  2149. /*40.0mA */
  2150. rac_cal = ((volt_2 - volt_1) * 1000) / (curr_1 - curr_2); /*m-ohm */
  2151. if (rac_cal < 0)
  2152. ret = (rac_cal - (rac_cal * 2)) * 1;
  2153. else
  2154. ret = rac_cal * 1;
  2155. } else {
  2156. ret = -1;
  2157. pmic_spm_crit2("[4,Calculate Rac] bypass due to (curr_x-curr_y) < 40mA\n");
  2158. }
  2159. pmic_spm_crit2
  2160. ("volt_1 = %d,volt_2 = %d,curr_1 = %d,curr_2 = %d,rac_cal = %d,ret = %d,retry_count = %d\n",
  2161. volt_1, volt_2, curr_1, curr_2, rac_cal, ret, retry_count);
  2162. pmic_spm_crit2(" %d,%d,%d,%d,%d,%d,%d\n",
  2163. volt_1, volt_2, curr_1, curr_2, rac_cal, ret, retry_count);
  2164. /*------------------------*/
  2165. retry_count++;
  2166. if ((retry_count < 3) && (ret == -1))
  2167. retry_state = true;
  2168. else
  2169. retry_state = false;
  2170. } while (retry_state == true);
  2171. return ret;
  2172. }
  2173. int get_dlpt_imix_spm(void)
  2174. {
  2175. int rac_val[5], rac_val_avg;
  2176. /* int volt[5], curr[5], volt_avg = 0, curr_avg = 0; */
  2177. /* int imix; */
  2178. int i;
  2179. static unsigned int pre_ui_soc = 101;
  2180. unsigned int ui_soc;
  2181. ui_soc = bat_get_ui_percentage();
  2182. if (ui_soc != pre_ui_soc) {
  2183. pre_ui_soc = ui_soc;
  2184. } else {
  2185. pmic_spm_crit2("[dlpt_R] pre_SOC=%d SOC=%d skip\n", pre_ui_soc, ui_soc);
  2186. return 0;
  2187. }
  2188. for (i = 0; i < 2; i++) {
  2189. rac_val[i] = get_rac_val();
  2190. if (rac_val[i] == -1)
  2191. return -1;
  2192. }
  2193. /*rac_val_avg=rac_val[0]+rac_val[1]+rac_val[2]+rac_val[3]+rac_val[4]; */
  2194. /*rac_val_avg=rac_val_avg/5; */
  2195. /*PMICLOG("[dlpt_R] %d,%d,%d,%d,%d %d\n",rac_val[0],rac_val[1],rac_val[2],rac_val[3],rac_val[4],rac_val_avg); */
  2196. rac_val_avg = rac_val[0] + rac_val[1];
  2197. rac_val_avg = rac_val_avg / 2;
  2198. pmic_spm_crit2("[dlpt_R] %d,%d,%d\n", rac_val[0], rac_val[1], rac_val_avg);
  2199. if (rac_val_avg > 100)
  2200. ptim_rac_val_avg = rac_val_avg;
  2201. /*
  2202. for(i=0;i<5;i++)
  2203. {
  2204. do_ptim();
  2205. volt[i]=ptim_bat_vol;
  2206. curr[i]=ptim_R_curr;
  2207. volt_avg+=ptim_bat_vol;
  2208. curr_avg+=ptim_R_curr;
  2209. }
  2210. volt_avg=volt_avg/5;
  2211. curr_avg=curr_avg/5;
  2212. imix=curr_avg+(volt_avg-g_lbatInt1)*1000/ptim_rac_val_avg;
  2213. pmic_spm_crit2("[dlpt_Imix] %d,%d,%d,%d,%d,%d,%d\n",volt_avg,curr_avg,g_lbatInt1,
  2214. ptim_rac_val_avg,imix,BMT_status.SOC,bat_get_ui_percentage());
  2215. ptim_imix=imix;
  2216. */
  2217. return 0;
  2218. }
  2219. int get_dlpt_imix(void)
  2220. {
  2221. /* int rac_val[5], rac_val_avg; */
  2222. int volt[5], curr[5], volt_avg = 0, curr_avg = 0;
  2223. int imix;
  2224. int i, count_do_ptim = 0;
  2225. for (i = 0; i < 5; i++) {
  2226. /*adc and fg-------------------------------------------------------- */
  2227. /*do_ptim(KAL_FALSE);*/
  2228. while (do_ptim(KAL_FALSE)) {
  2229. if ((count_do_ptim >= 2) && (count_do_ptim < 4))
  2230. pr_err("do_ptim more than twice times\n");
  2231. else if (count_do_ptim > 3) {
  2232. pr_err("do_ptim more than five times\n");
  2233. BUG_ON(1);
  2234. } else
  2235. ;
  2236. count_do_ptim++;
  2237. }
  2238. volt[i] = ptim_bat_vol;
  2239. curr[i] = ptim_R_curr;
  2240. volt_avg += ptim_bat_vol;
  2241. curr_avg += ptim_R_curr;
  2242. }
  2243. volt_avg = volt_avg / 5;
  2244. curr_avg = curr_avg / 5;
  2245. imix = (curr_avg + (volt_avg - g_lbatInt1) * 1000 / ptim_rac_val_avg) / 10;
  2246. pr_debug("[get_dlpt_imix] %d,%d,%d,%d,%d,%d,%d\n", volt_avg, curr_avg, g_lbatInt1,
  2247. ptim_rac_val_avg, imix, BMT_status.SOC, bat_get_ui_percentage());
  2248. ptim_imix = imix;
  2249. return ptim_imix;
  2250. }
  2251. int get_dlpt_imix_charging(void)
  2252. {
  2253. int zcv_val = 0;
  2254. int vsys_min_1_val = POWER_INT2_VOLT;
  2255. int imix_val = 0;
  2256. #if defined(SWCHR_POWER_PATH)
  2257. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_ISENSE_AP, 5, 1);
  2258. #else
  2259. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_BATSNS_AP, 5, 1);
  2260. #endif
  2261. imix_val = (zcv_val - vsys_min_1_val) * 1000 / ptim_rac_val_avg * 9 / 10;
  2262. pr_debug("[dlpt] get_dlpt_imix_charging %d %d %d %d\n",
  2263. imix_val, zcv_val, vsys_min_1_val, ptim_rac_val_avg);
  2264. return imix_val;
  2265. }
  2266. int dlpt_check_power_off(void)
  2267. {
  2268. int ret = 0;
  2269. ret = 0;
  2270. if (g_dlpt_start == 0) {
  2271. PMICLOG("[dlpt_check_power_off] not start\n");
  2272. } else {
  2273. if (g_low_battery_level == 2 && g_lowbat_int_bottom == 1)
  2274. ret = 1;
  2275. else
  2276. ret = 0;
  2277. PMICLOG("[dlpt_check_power_off]");
  2278. PMICLOG("ptim_imix=%d, POWEROFF_BAT_CURRENT=%d", ptim_imix, POWEROFF_BAT_CURRENT);
  2279. PMICLOG(" g_low_battery_level=%d,ret=%d,g_lowbat_int_bottom=%d\n",
  2280. g_low_battery_level, ret, g_lowbat_int_bottom);
  2281. }
  2282. return ret;
  2283. }
  2284. int dlpt_notify_handler(void *unused)
  2285. {
  2286. ktime_t ktime;
  2287. int pre_ui_soc = 0;
  2288. int cur_ui_soc = 0;
  2289. /* int diff_ui_soc = 1; */
  2290. pre_ui_soc = bat_get_ui_percentage();
  2291. cur_ui_soc = pre_ui_soc;
  2292. do {
  2293. ktime = ktime_set(10, 0);
  2294. wait_event_interruptible(dlpt_notify_waiter, (dlpt_notify_flag == true));
  2295. #if !defined CONFIG_HAS_WAKELOCKS
  2296. __pm_stay_awake(&dlpt_notify_lock);
  2297. #else
  2298. wake_lock(&dlpt_notify_lock);
  2299. #endif
  2300. mutex_lock(&dlpt_notify_mutex);
  2301. /*---------------------------------*/
  2302. cur_ui_soc = bat_get_ui_percentage();
  2303. if (cur_ui_soc <= 1) {
  2304. g_low_per_timer += 10;
  2305. if (g_low_per_timer > g_low_per_timeout_val)
  2306. g_low_per_timer = 0;
  2307. PMICLOG("[DLPT] g_low_per_timer=%d,g_low_per_timeout_val=%d\n",
  2308. g_low_per_timer, g_low_per_timeout_val);
  2309. } else {
  2310. g_low_per_timer = 0;
  2311. }
  2312. PMICLOG("[dlpt_notify_handler] %d %d %d %d %d\n", pre_ui_soc, cur_ui_soc,
  2313. g_imix_val, g_low_per_timer, g_low_per_timeout_val);
  2314. /*
  2315. if( ((pre_ui_soc-cur_ui_soc)>=diff_ui_soc) ||
  2316. ((cur_ui_soc-pre_ui_soc)>=diff_ui_soc) ||
  2317. (g_imix_val == 0) ||
  2318. (g_imix_val == -1) ||
  2319. ( (cur_ui_soc <= 1) && (g_low_per_timer >= g_low_per_timeout_val) )
  2320. )
  2321. */
  2322. {
  2323. PMICLOG("[DLPT] is running\n");
  2324. if (ptim_rac_val_avg == 0)
  2325. PMICLOG("[DLPT] ptim_rac_val_avg=0 , skip\n");
  2326. else {
  2327. if (upmu_get_rgs_chrdet()) {
  2328. g_imix_val = get_dlpt_imix_charging();
  2329. } else {
  2330. g_imix_val = get_dlpt_imix();
  2331. /*
  2332. if(g_imix_val != -1) {
  2333. if(g_imix_val_pre <= 0)
  2334. g_imix_val_pre=g_imix_val;
  2335. if(g_imix_val > g_imix_val_pre) {
  2336. PMICLOG("[DLPT] g_imix_val=%d,g_imix_val_pre=%d\n", g_imix_val, g_imix_val_pre);
  2337. g_imix_val=g_imix_val_pre;
  2338. }
  2339. else
  2340. g_imix_val_pre=g_imix_val;
  2341. }
  2342. */
  2343. }
  2344. /*Notify */
  2345. if (g_imix_val >= 1) {
  2346. if (g_imix_val > IMAX_MAX_VALUE)
  2347. g_imix_val = IMAX_MAX_VALUE;
  2348. exec_dlpt_callback(g_imix_val);
  2349. } else {
  2350. exec_dlpt_callback(1);
  2351. PMICLOG("[DLPT] return 1\n");
  2352. }
  2353. pre_ui_soc = cur_ui_soc;
  2354. #if 0
  2355. pr_debug("[DLPT_final] %d,%d,%d,%d,%d,%d\n",
  2356. g_imix_val, g_imix_val_pre, pre_ui_soc, cur_ui_soc,
  2357. diff_ui_soc, IMAX_MAX_VALUE);
  2358. #else
  2359. pr_debug("[DLPT_final] %d,%d,%d,%d,%d\n",
  2360. g_imix_val, g_imix_val_pre, pre_ui_soc, cur_ui_soc,
  2361. IMAX_MAX_VALUE);
  2362. #endif
  2363. }
  2364. }
  2365. g_dlpt_start = 1;
  2366. dlpt_notify_flag = false;
  2367. /*---------------------------------*/
  2368. mutex_unlock(&dlpt_notify_mutex);
  2369. #if !defined CONFIG_HAS_WAKELOCKS
  2370. __pm_relax(&dlpt_notify_lock);
  2371. #else
  2372. wake_unlock(&dlpt_notify_lock);
  2373. #endif
  2374. hrtimer_start(&dlpt_notify_timer, ktime, HRTIMER_MODE_REL);
  2375. } while (!kthread_should_stop());
  2376. return 0;
  2377. }
  2378. enum hrtimer_restart dlpt_notify_task(struct hrtimer *timer)
  2379. {
  2380. dlpt_notify_flag = true;
  2381. wake_up_interruptible(&dlpt_notify_waiter);
  2382. PMICLOG("dlpt_notify_task is called\n");
  2383. return HRTIMER_NORESTART;
  2384. }
  2385. int get_system_loading_ma(void)
  2386. {
  2387. int fg_val = 0;
  2388. if (g_dlpt_start == 0)
  2389. PMICLOG("get_system_loading_ma not ready\n");
  2390. else {
  2391. fg_val = battery_meter_get_battery_current();
  2392. fg_val = fg_val / 10;
  2393. if (battery_meter_get_battery_current_sign() == 1)
  2394. fg_val = 0 - fg_val; /* charging */
  2395. PMICLOG("[get_system_loading_ma] fg_val=%d\n", fg_val);
  2396. }
  2397. return fg_val;
  2398. }
  2399. void dlpt_notify_init(void)
  2400. {
  2401. ktime_t ktime;
  2402. ktime = ktime_set(30, 0);
  2403. hrtimer_init(&dlpt_notify_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2404. dlpt_notify_timer.function = dlpt_notify_task;
  2405. hrtimer_start(&dlpt_notify_timer, ktime, HRTIMER_MODE_REL);
  2406. dlpt_notify_thread = kthread_run(dlpt_notify_handler, 0, "dlpt_notify_thread");
  2407. if (IS_ERR(dlpt_notify_thread))
  2408. pr_err("Failed to create dlpt_notify_thread\n");
  2409. else
  2410. pr_err("Create dlpt_notify_thread : done\n");
  2411. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 0);
  2412. /*re-init UVLO volt */
  2413. switch (POWER_UVLO_VOLT_LEVEL) {
  2414. case 2500:
  2415. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 0);
  2416. break;
  2417. case 2550:
  2418. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 1);
  2419. break;
  2420. case 2600:
  2421. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 2);
  2422. break;
  2423. case 2650:
  2424. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 3);
  2425. break;
  2426. case 2700:
  2427. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 4);
  2428. break;
  2429. case 2750:
  2430. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 5);
  2431. break;
  2432. case 2800:
  2433. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 6);
  2434. break;
  2435. case 2850:
  2436. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 7);
  2437. break;
  2438. case 2900:
  2439. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 8);
  2440. break;
  2441. default:
  2442. PMICLOG("Invalid value(%d)\n", POWER_UVLO_VOLT_LEVEL);
  2443. break;
  2444. }
  2445. pr_err("POWER_UVLO_VOLT_LEVEL=%d, [0x%x]=0x%x\n",
  2446. POWER_UVLO_VOLT_LEVEL, MT6328_CHR_CON17, upmu_get_reg_value(MT6328_CHR_CON17));
  2447. }
  2448. #else
  2449. int get_dlpt_imix_spm(void)
  2450. {
  2451. return 1;
  2452. }
  2453. #endif /*#ifdef DLPT_FEATURE_SUPPORT */
  2454. /*****************************************************************************
  2455. * interrupt Setting
  2456. ******************************************************************************/
  2457. static struct pmic_interrupt_bit interrupt_status0[] = {
  2458. PMIC_S_INT_GEN(RG_INT_STATUS_PWRKEY),
  2459. PMIC_S_INT_GEN(RG_INT_STATUS_HOMEKEY),
  2460. PMIC_S_INT_GEN(RG_INT_STATUS_PWRKEY_R),
  2461. PMIC_S_INT_GEN(RG_INT_STATUS_HOMEKEY_R),
  2462. PMIC_S_INT_GEN(RG_INT_STATUS_THR_H),
  2463. PMIC_S_INT_GEN(RG_INT_STATUS_THR_L),
  2464. PMIC_S_INT_GEN(RG_INT_STATUS_BAT_H),
  2465. PMIC_S_INT_GEN(RG_INT_STATUS_BAT_L),
  2466. PMIC_S_INT_GEN(NO_USE),
  2467. PMIC_S_INT_GEN(RG_INT_STATUS_RTC),
  2468. PMIC_S_INT_GEN(RG_INT_STATUS_AUDIO),
  2469. PMIC_S_INT_GEN(NO_USE),
  2470. PMIC_S_INT_GEN(RG_INT_STATUS_ACCDET),
  2471. PMIC_S_INT_GEN(RG_INT_STATUS_ACCDET_EINT),
  2472. PMIC_S_INT_GEN(RG_INT_STATUS_ACCDET_NEGV),
  2473. PMIC_S_INT_GEN(RG_INT_STATUS_NI_LBAT_INT),
  2474. };
  2475. static struct pmic_interrupt_bit interrupt_status1[] = {
  2476. PMIC_S_INT_GEN(RG_INT_STATUS_VPROC_OC),
  2477. PMIC_S_INT_GEN(RG_INT_STATUS_VSYS_OC),
  2478. PMIC_S_INT_GEN(RG_INT_STATUS_VLTE_OC),
  2479. PMIC_S_INT_GEN(NO_USE),
  2480. PMIC_S_INT_GEN(NO_USE),
  2481. PMIC_S_INT_GEN(RG_INT_STATUS_VCORE_OC),
  2482. PMIC_S_INT_GEN(NO_USE),
  2483. PMIC_S_INT_GEN(NO_USE),
  2484. PMIC_S_INT_GEN(RG_INT_STATUS_VPA_OC),
  2485. PMIC_S_INT_GEN(RG_INT_STATUS_LDO_OC),
  2486. PMIC_S_INT_GEN(RG_INT_STATUS_BAT2_H),
  2487. PMIC_S_INT_GEN(RG_INT_STATUS_BAT2_L),
  2488. PMIC_S_INT_GEN(RG_INT_STATUS_VISMPS0_H),
  2489. PMIC_S_INT_GEN(RG_INT_STATUS_VISMPS0_L),
  2490. PMIC_S_INT_GEN(RG_INT_STATUS_AUXADC_IMP),
  2491. PMIC_S_INT_GEN(NO_USE),
  2492. };
  2493. static struct pmic_interrupt_bit interrupt_status2[] = {
  2494. PMIC_S_INT_GEN(RG_INT_STATUS_OV),
  2495. PMIC_S_INT_GEN(RG_INT_STATUS_BVALID_DET),
  2496. PMIC_S_INT_GEN(RG_INT_STATUS_VBATON_HV),
  2497. PMIC_S_INT_GEN(RG_INT_STATUS_VBATON_UNDET),
  2498. PMIC_S_INT_GEN(RG_INT_STATUS_WATCHDOG),
  2499. PMIC_S_INT_GEN(RG_INT_STATUS_PCHR_CM_VDEC),
  2500. PMIC_S_INT_GEN(RG_INT_STATUS_CHRDET),
  2501. PMIC_S_INT_GEN(RG_INT_STATUS_PCHR_CM_VINC),
  2502. PMIC_S_INT_GEN(RG_INT_STATUS_FG_BAT_H),
  2503. PMIC_S_INT_GEN(RG_INT_STATUS_FG_BAT_L),
  2504. PMIC_S_INT_GEN(RG_INT_STATUS_FG_CUR_H),
  2505. PMIC_S_INT_GEN(RG_INT_STATUS_FG_CUR_L),
  2506. PMIC_S_INT_GEN(RG_INT_STATUS_FG_ZCV),
  2507. PMIC_S_INT_GEN(RG_INT_STATUS_SPKL_D),
  2508. PMIC_S_INT_GEN(RG_INT_STATUS_SPKL_AB),
  2509. PMIC_S_INT_GEN(NO_USE),
  2510. };
  2511. static struct pmic_interrupts interrupts[] = {
  2512. PMIC_M_INTS_GEN(MT6328_INT_STATUS0, MT6328_INT_CON0, MT6328_INT_CON0_SET,
  2513. MT6328_INT_CON0_CLR, interrupt_status0),
  2514. PMIC_M_INTS_GEN(MT6328_INT_STATUS1, MT6328_INT_CON1, MT6328_INT_CON1_SET,
  2515. MT6328_INT_CON1_CLR, interrupt_status1),
  2516. PMIC_M_INTS_GEN(MT6328_INT_STATUS2, MT6328_INT_CON2, MT6328_INT_CON2_SET,
  2517. MT6328_INT_CON2_CLR, interrupt_status2),
  2518. };
  2519. void pwrkey_int_handler(void)
  2520. {
  2521. PMICLOG("[pwrkey_int_handler] Press pwrkey %d\n", pmic_get_register_value(PMIC_PWRKEY_DEB));
  2522. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2523. if (get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT)
  2524. timer_pre = sched_clock();
  2525. #endif
  2526. #if defined(CONFIG_MTK_FPGA)
  2527. #else
  2528. kpd_pwrkey_pmic_handler(0x1);
  2529. #endif
  2530. }
  2531. void pwrkey_int_handler_r(void)
  2532. {
  2533. PMICLOG("[pwrkey_int_handler_r] Release pwrkey %d\n",
  2534. pmic_get_register_value(PMIC_PWRKEY_DEB));
  2535. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2536. if (get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT && timer_pre != 0) {
  2537. timer_pos = sched_clock();
  2538. if (timer_pos - timer_pre >= LONG_PWRKEY_PRESS_TIME)
  2539. long_pwrkey_press = true;
  2540. PMICLOG
  2541. ("timer_pos = %ld, timer_pre = %ld, timer_pos-timer_pre = %ld, long_pwrkey_press = %d\r\n",
  2542. timer_pos, timer_pre, timer_pos - timer_pre, long_pwrkey_press);
  2543. if (long_pwrkey_press) { /*500ms */
  2544. PMICLOG
  2545. ("Power Key Pressed during kernel power off charging, reboot OS\r\n");
  2546. kernel_restart("kernel power off charging reboot system");
  2547. /*arch_reset(0, NULL); */
  2548. }
  2549. }
  2550. #endif
  2551. #if defined(CONFIG_MTK_FPGA)
  2552. #else
  2553. kpd_pwrkey_pmic_handler(0x0);
  2554. #endif
  2555. }
  2556. /*****************************************************************************
  2557. * PMIC Interrupt callback
  2558. ******************************************************************************/
  2559. void homekey_int_handler(void)
  2560. {
  2561. PMICLOG("[homekey_int_handler] Press homekey %d\n",
  2562. pmic_get_register_value(PMIC_HOMEKEY_DEB));
  2563. #if defined(CONFIG_MTK_FPGA)
  2564. #else
  2565. kpd_pmic_rstkey_handler(0x1);
  2566. #endif
  2567. }
  2568. void homekey_int_handler_r(void)
  2569. {
  2570. PMICLOG("[homekey_int_handler_r] Release homekey %d\n",
  2571. pmic_get_register_value(PMIC_HOMEKEY_DEB));
  2572. #if defined(CONFIG_MTK_FPGA)
  2573. #else
  2574. kpd_pmic_rstkey_handler(0x0);
  2575. #endif
  2576. }
  2577. void chrdet_int_handler(void)
  2578. {
  2579. PMICLOG("[chrdet_int_handler]CHRDET status = %d....\n",
  2580. pmic_get_register_value(PMIC_RGS_CHRDET));
  2581. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  2582. if (!upmu_get_rgs_chrdet()) {
  2583. int boot_mode = 0;
  2584. boot_mode = get_boot_mode();
  2585. if (boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  2586. || boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  2587. PMICLOG("[chrdet_int_handler] Unplug Charger/USB\n");
  2588. mt_power_off();
  2589. }
  2590. }
  2591. #endif
  2592. pmic_set_register_value(PMIC_RG_USBDL_RST, 1);
  2593. #if defined(CONFIG_MTK_SMART_BATTERY)
  2594. do_chrdet_int_task();
  2595. #endif
  2596. }
  2597. void auxadc_imp_int_handler_r(void)
  2598. {
  2599. PMICLOG("auxadc_imp_int_handler_r() =%d\n",
  2600. pmic_get_register_value(PMIC_AUXADC_ADC_OUT_IMP));
  2601. /*clear IRQ */
  2602. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 1);
  2603. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 1);
  2604. /*restore to initial state */
  2605. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 0);
  2606. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 0);
  2607. /*turn off interrupt */
  2608. pmic_set_register_value(PMIC_RG_INT_EN_AUXADC_IMP, 0);
  2609. }
  2610. /*****************************************************************************
  2611. * PMIC Interrupt service
  2612. ******************************************************************************/
  2613. static DEFINE_MUTEX(pmic_mutex);
  2614. static struct task_struct *pmic_thread_handle;
  2615. #if !defined CONFIG_HAS_WAKELOCKS
  2616. struct wakeup_source pmicThread_lock;
  2617. #else
  2618. struct wake_lock pmicThread_lock;
  2619. #endif
  2620. void wake_up_pmic(void)
  2621. {
  2622. PMICLOG("[wake_up_pmic]\r\n");
  2623. if (pmic_thread_handle != NULL)
  2624. wake_up_process(pmic_thread_handle);
  2625. #if !defined CONFIG_HAS_WAKELOCKS
  2626. __pm_stay_awake(&pmicThread_lock);
  2627. #else
  2628. wake_lock(&pmicThread_lock);
  2629. #endif
  2630. }
  2631. EXPORT_SYMBOL(wake_up_pmic);
  2632. #if 0 /* def CONFIG_MTK_LEGACY */
  2633. void mt_pmic_eint_irq(void)
  2634. {
  2635. /*PMICLOG("[mt_pmic_eint_irq] receive interrupt\n"); */
  2636. wake_up_pmic();
  2637. /*return; */
  2638. }
  2639. #else
  2640. irqreturn_t mt_pmic_eint_irq(int irq, void *desc)
  2641. {
  2642. /*PMICLOG("[mt_pmic_eint_irq] receive interrupt\n"); */
  2643. disable_irq_nosync(irq);
  2644. wake_up_pmic();
  2645. return IRQ_HANDLED;
  2646. }
  2647. #endif
  2648. void pmic_enable_interrupt(unsigned int intNo, unsigned int en, char *str)
  2649. {
  2650. unsigned int shift, no;
  2651. shift = intNo / PMIC_INT_WIDTH;
  2652. no = intNo % PMIC_INT_WIDTH;
  2653. if (shift >= ARRAY_SIZE(interrupts)) {
  2654. PMICLOG("[pmic_enable_interrupt] fail intno=%d \r\n", intNo);
  2655. return;
  2656. }
  2657. PMICLOG("[pmic_enable_interrupt] intno=%d en=%d str=%s shf=%d no=%d [0x%x]=0x%x\r\n",
  2658. intNo, en, str, shift, no, interrupts[shift].en,
  2659. upmu_get_reg_value(interrupts[shift].en));
  2660. if (en == 1)
  2661. pmic_config_interface(interrupts[shift].set, 0x1, 0x1, no);
  2662. else if (en == 0)
  2663. pmic_config_interface(interrupts[shift].clear, 0x1, 0x1, no);
  2664. PMICLOG("[pmic_enable_interrupt] after [0x%x]=0x%x\r\n",
  2665. interrupts[shift].en, upmu_get_reg_value(interrupts[shift].en));
  2666. }
  2667. void pmic_register_interrupt_callback(unsigned int intNo, void (EINT_FUNC_PTR) (void))
  2668. {
  2669. unsigned int shift, no;
  2670. shift = intNo / PMIC_INT_WIDTH;
  2671. no = intNo % PMIC_INT_WIDTH;
  2672. if (shift >= ARRAY_SIZE(interrupts)) {
  2673. PMICLOG("[pmic_register_interrupt_callback] fail intno=%d\r\n", intNo);
  2674. return;
  2675. }
  2676. PMICLOG("[pmic_register_interrupt_callback] intno=%d\r\n", intNo);
  2677. interrupts[shift].interrupts[no].callback = EINT_FUNC_PTR;
  2678. }
  2679. void PMIC_EINT_SETTING(void)
  2680. {
  2681. struct device_node *node = NULL;
  2682. int ret = 0;
  2683. u32 ints[2] = { 0, 0 };
  2684. upmu_set_reg_value(MT6328_INT_CON0, 0);
  2685. upmu_set_reg_value(MT6328_INT_CON1, 0);
  2686. upmu_set_reg_value(MT6328_INT_CON2, 0);
  2687. /*enable pwrkey/homekey interrupt */
  2688. upmu_set_reg_value(MT6328_INT_CON0_SET, 0xf);
  2689. /*for all interrupt events, turn on interrupt module clock */
  2690. pmic_set_register_value(PMIC_RG_INTRP_CK_PDN, 0);
  2691. /*For BUCK OC related interrupt, please turn on pwmoc_6m_ck (6MHz) */
  2692. pmic_set_register_value(PMIC_RG_PWMOC_6M_CK_PDN, 0);
  2693. pmic_register_interrupt_callback(0, pwrkey_int_handler);
  2694. pmic_register_interrupt_callback(1, homekey_int_handler);
  2695. pmic_register_interrupt_callback(2, pwrkey_int_handler_r);
  2696. pmic_register_interrupt_callback(3, homekey_int_handler_r);
  2697. pmic_register_interrupt_callback(6, bat_h_int_handler);
  2698. pmic_register_interrupt_callback(7, bat_l_int_handler);
  2699. pmic_register_interrupt_callback(38, chrdet_int_handler);
  2700. pmic_register_interrupt_callback(42, fg_cur_h_int_handler);
  2701. pmic_register_interrupt_callback(43, fg_cur_l_int_handler);
  2702. pmic_enable_interrupt(0, 1, "PMIC");
  2703. pmic_enable_interrupt(1, 1, "PMIC");
  2704. pmic_enable_interrupt(2, 1, "PMIC");
  2705. pmic_enable_interrupt(3, 1, "PMIC");
  2706. #ifdef LOW_BATTERY_PROTECT
  2707. /* move to lbat_xxx_en_setting */
  2708. #else
  2709. pmic_enable_interrupt(6, 1, "PMIC");
  2710. pmic_enable_interrupt(7, 1, "PMIC");
  2711. #endif
  2712. /*pmic_enable_interrupt(30,1,"PMIC"); */
  2713. pmic_enable_interrupt(38, 1, "PMIC");
  2714. #ifdef BATTERY_OC_PROTECT
  2715. /* move to bat_oc_x_en_setting */
  2716. #else
  2717. pmic_enable_interrupt(42, 1, "PMIC");
  2718. pmic_enable_interrupt(43, 1, "PMIC");
  2719. #endif
  2720. #if 0
  2721. /*mt_eint_set_hw_debounce(g_eint_pmic_num, g_cust_eint_mt_pmic_debounce_cn); */
  2722. mt_eint_registration(g_eint_pmic_num, g_cust_eint_mt_pmic_type, mt_pmic_eint_irq, 0);
  2723. mt_eint_unmask(g_eint_pmic_num);
  2724. #else
  2725. node = of_find_compatible_node(NULL, NULL, "mediatek, pmic-eint");
  2726. if (node) {
  2727. of_property_read_u32_array(node, "debounce", ints, ARRAY_SIZE(ints));
  2728. mt_gpio_set_debounce(ints[0], ints[1]);
  2729. g_pmic_irq = irq_of_parse_and_map(node, 0);
  2730. ret =
  2731. request_irq(g_pmic_irq, (irq_handler_t) mt_pmic_eint_irq, IRQF_TRIGGER_NONE,
  2732. "pmic-eint", NULL);
  2733. if (ret > 0)
  2734. PMICLOG("EINT IRQ LINENNOT AVAILABLE\n");
  2735. /*enable_irq(g_pmic_irq); */
  2736. disable_irq(g_pmic_irq);
  2737. } else
  2738. PMICLOG("can't find compatible node\n");
  2739. #endif
  2740. PMICLOG("[CUST_EINT] CUST_EINT_MT_PMIC_MT6325_NUM=%d\n", g_eint_pmic_num);
  2741. PMICLOG("[CUST_EINT] CUST_EINT_PMIC_DEBOUNCE_CN=%d\n", g_cust_eint_mt_pmic_debounce_cn);
  2742. PMICLOG("[CUST_EINT] CUST_EINT_PMIC_TYPE=%d\n", g_cust_eint_mt_pmic_type);
  2743. PMICLOG("[CUST_EINT] CUST_EINT_PMIC_DEBOUNCE_EN=%d\n", g_cust_eint_mt_pmic_debounce_en);
  2744. }
  2745. static void pmic_int_handler(void)
  2746. {
  2747. unsigned char i, j;
  2748. unsigned int ret;
  2749. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  2750. unsigned int int_status_val = 0;
  2751. int_status_val = upmu_get_reg_value(interrupts[i].address);
  2752. PMICLOG("[PMIC_INT] addr[0x%x]=0x%x\n", interrupts[i].address, int_status_val);
  2753. for (j = 0; j < PMIC_INT_WIDTH; j++) {
  2754. if ((int_status_val) & (1 << j)) {
  2755. PMICLOG("[PMIC_INT][%s]\n", interrupts[i].interrupts[j].name);
  2756. if (interrupts[i].interrupts[j].callback != NULL) {
  2757. interrupts[i].interrupts[j].callback();
  2758. interrupts[i].interrupts[j].times++;
  2759. }
  2760. ret = pmic_config_interface(interrupts[i].address, 0x1, 0x1, j);
  2761. }
  2762. }
  2763. }
  2764. }
  2765. int pmic_rdy = 0, usb_rdy = 0;
  2766. void pmic_enable_charger_detection_int(int x)
  2767. {
  2768. if (x == 0) {
  2769. pmic_rdy = 1;
  2770. PMICLOG("[pmic_enable_charger_detection_int] PMIC\n");
  2771. } else if (x == 1) {
  2772. usb_rdy = 1;
  2773. PMICLOG("[pmic_enable_charger_detection_int] USB\n");
  2774. }
  2775. PMICLOG("[pmic_enable_charger_detection_int] pmic_rdy=%d usb_rdy=%d\n", pmic_rdy, usb_rdy);
  2776. if (pmic_rdy == 1 && usb_rdy == 1) {
  2777. #if defined(CONFIG_MTK_SMART_BATTERY)
  2778. wake_up_bat();
  2779. #endif
  2780. PMICLOG("[pmic_enable_charger_detection_int] enable charger detection interrupt\n");
  2781. }
  2782. }
  2783. bool is_charger_detection_rdy(void)
  2784. {
  2785. if (pmic_rdy == 1 && usb_rdy == 1)
  2786. return true;
  2787. else
  2788. return false;
  2789. }
  2790. static int pmic_thread_kthread(void *x)
  2791. {
  2792. unsigned int i;
  2793. unsigned int int_status_val = 0;
  2794. unsigned int pwrap_eint_status = 0;
  2795. struct sched_param param = {.sched_priority = 98 };
  2796. sched_setscheduler(current, SCHED_FIFO, &param);
  2797. set_current_state(TASK_INTERRUPTIBLE);
  2798. PMICLOG("[PMIC_INT] enter\n");
  2799. pmic_enable_charger_detection_int(0);
  2800. /* Run on a process content */
  2801. while (1) {
  2802. mutex_lock(&pmic_mutex);
  2803. pwrap_eint_status = pmic_wrap_eint_status();
  2804. PMICLOG("[PMIC_INT] pwrap_eint_status=0x%x\n", pwrap_eint_status);
  2805. pmic_int_handler();
  2806. pmic_wrap_eint_clr(0x0);
  2807. /*PMICLOG("[PMIC_INT] pmic_wrap_eint_clr(0x0);\n"); */
  2808. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  2809. int_status_val = upmu_get_reg_value(interrupts[i].address);
  2810. PMICLOG("[PMIC_INT] after ,int_status_val[0x%x]=0x%x\n",
  2811. interrupts[i].address, int_status_val);
  2812. }
  2813. mdelay(1);
  2814. mutex_unlock(&pmic_mutex);
  2815. #if !defined CONFIG_HAS_WAKELOCKS
  2816. __pm_relax(&pmicThread_lock);
  2817. #else
  2818. wake_unlock(&pmicThread_lock);
  2819. #endif
  2820. set_current_state(TASK_INTERRUPTIBLE);
  2821. #if 0 /*def CONFIG_MTK_LEGACY */
  2822. mt_eint_unmask(g_eint_pmic_num);
  2823. #else
  2824. if (g_pmic_irq != 0)
  2825. enable_irq(g_pmic_irq);
  2826. #endif
  2827. schedule();
  2828. }
  2829. return 0;
  2830. }
  2831. int is_ext_buck2_exist(void)
  2832. {
  2833. #if defined(CONFIG_MTK_FPGA)
  2834. return 0;
  2835. #else
  2836. #if !defined CONFIG_MTK_LEGACY
  2837. return gpiod_get_value(gpio_to_desc(130));
  2838. /*return __gpio_get_value(130); */
  2839. /*return mt_get_gpio_in(130); */
  2840. #else
  2841. return 0;
  2842. #endif
  2843. #endif
  2844. }
  2845. int is_ext_vbat_boost_exist(void)
  2846. {
  2847. return 0;
  2848. }
  2849. int is_ext_swchr_exist(void)
  2850. {
  2851. return 0;
  2852. }
  2853. /*****************************************************************************
  2854. * Enternal SWCHR
  2855. ******************************************************************************/
  2856. /*
  2857. #ifdef MTK_BQ24261_SUPPORT
  2858. extern int is_bq24261_exist(void);
  2859. #endif
  2860. int is_ext_swchr_exist(void)
  2861. {
  2862. #ifdef MTK_BQ24261_SUPPORT
  2863. if (is_bq24261_exist() == 1)
  2864. return 1;
  2865. else
  2866. return 0;
  2867. #else
  2868. PMICLOG("[is_ext_swchr_exist] no define any HW\n");
  2869. return 0;
  2870. #endif
  2871. }
  2872. */
  2873. /*****************************************************************************
  2874. * Enternal VBAT Boost status
  2875. ******************************************************************************/
  2876. /*
  2877. extern int is_tps6128x_sw_ready(void);
  2878. extern int is_tps6128x_exist(void);
  2879. int is_ext_vbat_boost_sw_ready(void)
  2880. {
  2881. if( (is_tps6128x_sw_ready()==1) )
  2882. return 1;
  2883. else
  2884. return 0;
  2885. }
  2886. int is_ext_vbat_boost_exist(void)
  2887. {
  2888. if( (is_tps6128x_exist()==1) )
  2889. return 1;
  2890. else
  2891. return 0;
  2892. }
  2893. */
  2894. /*****************************************************************************
  2895. * Enternal BUCK status
  2896. ******************************************************************************/
  2897. int get_ext_buck_i2c_ch_num(void)
  2898. {
  2899. if (is_mt6311_exist() == 1)
  2900. return get_mt6311_i2c_ch_num();
  2901. else
  2902. return -1;
  2903. }
  2904. int is_ext_buck_sw_ready(void)
  2905. {
  2906. if ((is_mt6311_sw_ready() == 1))
  2907. return 1;
  2908. else
  2909. return 0;
  2910. }
  2911. int is_ext_buck_exist(void)
  2912. {
  2913. if ((is_mt6311_exist() == 1))
  2914. return 1;
  2915. else
  2916. return 0;
  2917. }
  2918. /*****************************************************************************
  2919. * FTM
  2920. ******************************************************************************/
  2921. #define PMIC_DEVNAME "pmic_ftm"
  2922. #define Get_IS_EXT_BUCK_EXIST _IOW('k', 20, int)
  2923. #define Get_IS_EXT_VBAT_BOOST_EXIST _IOW('k', 21, int)
  2924. #define Get_IS_EXT_SWCHR_EXIST _IOW('k', 22, int)
  2925. #define Get_IS_EXT_BUCK2_EXIST _IOW('k', 23, int)
  2926. static struct class *pmic_class;
  2927. static struct cdev *pmic_cdev;
  2928. static int pmic_major;
  2929. static dev_t pmic_devno;
  2930. static long pmic_ftm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2931. {
  2932. int *user_data_addr;
  2933. int ret = 0;
  2934. int adc_in_data[2] = { 1, 1 };
  2935. int adc_out_data[2] = { 1, 1 };
  2936. switch (cmd) {
  2937. /*#if defined(FTM_EXT_BUCK_CHECK) */
  2938. case Get_IS_EXT_BUCK_EXIST:
  2939. user_data_addr = (int *)arg;
  2940. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2941. adc_out_data[0] = is_ext_buck_exist();
  2942. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2943. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_BUCK_EXIST:%d\n", adc_out_data[0]);
  2944. break;
  2945. /*#endif */
  2946. /*#if defined(FTM_EXT_VBAT_BOOST_CHECK) */
  2947. case Get_IS_EXT_VBAT_BOOST_EXIST:
  2948. user_data_addr = (int *)arg;
  2949. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2950. adc_out_data[0] = is_ext_vbat_boost_exist();
  2951. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2952. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_VBAT_BOOST_EXIST:%d\n", adc_out_data[0]);
  2953. break;
  2954. /*#endif */
  2955. /*#if defined(FEATURE_FTM_SWCHR_HW_DETECT) */
  2956. case Get_IS_EXT_SWCHR_EXIST:
  2957. user_data_addr = (int *)arg;
  2958. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2959. adc_out_data[0] = is_ext_swchr_exist();
  2960. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2961. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_SWCHR_EXIST:%d\n", adc_out_data[0]);
  2962. break;
  2963. /*#endif */
  2964. case Get_IS_EXT_BUCK2_EXIST:
  2965. user_data_addr = (int *)arg;
  2966. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2967. adc_out_data[0] = is_ext_buck2_exist();
  2968. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2969. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_BUCK2_EXIST:%d\n", adc_out_data[0]);
  2970. break;
  2971. default:
  2972. PMICLOG("[pmic_ftm_ioctl] Error ID\n");
  2973. break;
  2974. }
  2975. return 0;
  2976. }
  2977. #ifdef CONFIG_COMPAT
  2978. static long pmic_ftm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2979. {
  2980. int ret = 0;
  2981. switch (cmd) {
  2982. /*#if defined(FTM_EXT_BUCK_CHECK) */
  2983. case Get_IS_EXT_BUCK_EXIST:
  2984. case Get_IS_EXT_VBAT_BOOST_EXIST:
  2985. case Get_IS_EXT_SWCHR_EXIST:
  2986. case Get_IS_EXT_BUCK2_EXIST:
  2987. ret = file->f_op->unlocked_ioctl(file, cmd, arg);
  2988. break;
  2989. default:
  2990. PMICLOG("[pmic_ftm_compat_ioctl] Error ID\n");
  2991. break;
  2992. }
  2993. return 0;
  2994. }
  2995. #endif
  2996. static int pmic_ftm_open(struct inode *inode, struct file *file)
  2997. {
  2998. return 0;
  2999. }
  3000. static int pmic_ftm_release(struct inode *inode, struct file *file)
  3001. {
  3002. return 0;
  3003. }
  3004. static const struct file_operations pmic_ftm_fops = {
  3005. .owner = THIS_MODULE,
  3006. .unlocked_ioctl = pmic_ftm_ioctl,
  3007. #ifdef CONFIG_COMPAT
  3008. .compat_ioctl = pmic_ftm_compat_ioctl,
  3009. #endif
  3010. .open = pmic_ftm_open,
  3011. .release = pmic_ftm_release,
  3012. };
  3013. void pmic_ftm_init(void)
  3014. {
  3015. struct class_device *class_dev = NULL;
  3016. int ret = 0;
  3017. ret = alloc_chrdev_region(&pmic_devno, 0, 1, PMIC_DEVNAME);
  3018. if (ret)
  3019. PMICLOG("[pmic_ftm_init] Error: Can't Get Major number for pmic_ftm\n");
  3020. pmic_cdev = cdev_alloc();
  3021. pmic_cdev->owner = THIS_MODULE;
  3022. pmic_cdev->ops = &pmic_ftm_fops;
  3023. ret = cdev_add(pmic_cdev, pmic_devno, 1);
  3024. if (ret)
  3025. PMICLOG("[pmic_ftm_init] Error: cdev_add\n");
  3026. pmic_major = MAJOR(pmic_devno);
  3027. pmic_class = class_create(THIS_MODULE, PMIC_DEVNAME);
  3028. class_dev = (struct class_device *)device_create(pmic_class,
  3029. NULL, pmic_devno, NULL, PMIC_DEVNAME);
  3030. PMICLOG("[pmic_ftm_init] Done\n");
  3031. }
  3032. /*****************************************************************************
  3033. * HW Setting
  3034. ******************************************************************************/
  3035. unsigned short is_battery_remove = 0;
  3036. unsigned short is_wdt_reboot_pmic = 0;
  3037. unsigned short is_battery_remove_pmic(void)
  3038. {
  3039. return is_battery_remove;
  3040. }
  3041. /*extern bool crystal_exist_status(void);*/
  3042. void pmic_setting_for_co_tsx(void)
  3043. {
  3044. unsigned int ret = 0;
  3045. unsigned int devinfo = get_devinfo_with_index(47) >> 25;
  3046. switch (devinfo) {
  3047. case 0x41:
  3048. case 0x42:
  3049. case 0x43:
  3050. /* Denali-1+ MT6737T */
  3051. case 0x49:
  3052. case 0x4A:
  3053. case 0x4B:
  3054. /* Denali-2+ MT6737M */
  3055. case 0x51:
  3056. case 0x52:
  3057. case 0x53:
  3058. /* Denali-2+ MT6737 */
  3059. ret = pmic_config_interface(0x14, 0x1, 0x1, 5);
  3060. ret = pmic_config_interface(0x14, 0x1, 0x1, 7);
  3061. ret = pmic_config_interface(0x25A, 0x0, 0x1, 10);
  3062. ret = pmic_config_interface(0x278, 0x0, 0x1, 11);
  3063. ret = pmic_config_interface(0xF08, 0xC, 0x3FF, 0);
  3064. ret = pmic_config_interface(0xF08, 0x0, 0x1, 15);
  3065. ret = pmic_config_interface(0xF0E, 0xC, 0x3FF, 0);
  3066. ret = pmic_config_interface(0xF0E, 0x1, 0x1, 15);
  3067. ret = pmic_config_interface(0xF12, 0x0, 0x1, 0);
  3068. ret = pmic_config_interface(0xF12, 0x0, 0x1, 1);
  3069. ret = pmic_config_interface(0xF12, 0x1, 0x1, 2);
  3070. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] setting for co-TSX\n");
  3071. break;
  3072. default:
  3073. break;
  3074. }
  3075. }
  3076. void PMIC_INIT_SETTING_V1(void)
  3077. {
  3078. unsigned int chip_version = 0;
  3079. unsigned int ret = 0;
  3080. chip_version = pmic_get_register_value(PMIC_SWCID);
  3081. is_battery_remove = !pmic_get_register_value(PMIC_STRUP_PWROFF_SEQ_EN);
  3082. is_wdt_reboot_pmic = pmic_get_register_value(PMIC_WDTRSTB_STATUS);
  3083. pmic_set_register_value(PMIC_WDTRSTB_STATUS_CLR, 1);
  3084. /*--------------------------------------------------------*/
  3085. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 6328 PMIC Chip = 0x%x\n", chip_version);
  3086. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 2015-03-12...\n");
  3087. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] is_battery_remove =%d is_wdt_reboot=%d\n",
  3088. is_battery_remove, is_wdt_reboot_pmic);
  3089. if (is_ext_buck_exist() == 1) {
  3090. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 2015-04-27 for turbo...\n");
  3091. ret = pmic_config_interface(0x4, 0x1, 0x1, 4);
  3092. ret = pmic_config_interface(0xA, 0x1, 0x1, 0);
  3093. ret = pmic_config_interface(0xA, 0x1, 0x1, 11);
  3094. ret = pmic_config_interface(0xC, 0x1, 0x1, 0);
  3095. ret = pmic_config_interface(0xC, 0x1, 0x1, 1);
  3096. ret = pmic_config_interface(0xC, 0x1, 0x1, 4);
  3097. ret = pmic_config_interface(0xC, 0x1, 0x1, 5);
  3098. ret = pmic_config_interface(0xC, 0x1, 0x1, 6);
  3099. ret = pmic_config_interface(0xC, 0x1, 0x1, 7);
  3100. ret = pmic_config_interface(0xC, 0x1, 0x1, 8);
  3101. ret = pmic_config_interface(0xC, 0x1, 0x1, 9);
  3102. ret = pmic_config_interface(0xC, 0x1, 0x1, 10);
  3103. ret = pmic_config_interface(0xC, 0x1, 0x1, 11);
  3104. ret = pmic_config_interface(0xC, 0x1, 0x1, 12);
  3105. ret = pmic_config_interface(0xC, 0x1, 0x1, 13);
  3106. ret = pmic_config_interface(0xE, 0x1, 0x1, 0);
  3107. ret = pmic_config_interface(0x10, 0x1, 0x1, 5);
  3108. ret = pmic_config_interface(0x16, 0x1, 0x1, 0);
  3109. ret = pmic_config_interface(0x16, 0x1, 0x1, 1);
  3110. ret = pmic_config_interface(0x1E, 0x0, 0x1, 11);
  3111. ret = pmic_config_interface(0x32, 0x1, 0x1, 15);
  3112. ret = pmic_config_interface(0x40, 0x1, 0x1, 12);
  3113. ret = pmic_config_interface(0x204, 0x1, 0x1, 4);
  3114. ret = pmic_config_interface(0x204, 0x1, 0x1, 5);
  3115. ret = pmic_config_interface(0x204, 0x0, 0x1, 6);
  3116. ret = pmic_config_interface(0x226, 0x1, 0x1, 0);
  3117. ret = pmic_config_interface(0x226, 0x1, 0x1, 2);
  3118. ret = pmic_config_interface(0x226, 0x1, 0x1, 3);
  3119. ret = pmic_config_interface(0x242, 0x1, 0x1, 2);
  3120. ret = pmic_config_interface(0x242, 0x1, 0x1, 3);
  3121. ret = pmic_config_interface(0x248, 0x1, 0x1, 13);
  3122. ret = pmic_config_interface(0x248, 0x1, 0x1, 14);
  3123. ret = pmic_config_interface(0x25A, 0x1, 0x1, 9);
  3124. ret = pmic_config_interface(0x40E, 0x0, 0x3, 2);
  3125. ret = pmic_config_interface(0x412, 0x0, 0x3, 2);
  3126. ret = pmic_config_interface(0x420, 0x1, 0x1, 4);
  3127. ret = pmic_config_interface(0x422, 0x0, 0x1, 0);
  3128. ret = pmic_config_interface(0x422, 0x0, 0x1, 1);
  3129. ret = pmic_config_interface(0x422, 0x0, 0x1, 2);
  3130. ret = pmic_config_interface(0x424, 0x0, 0x7F, 0);
  3131. ret = pmic_config_interface(0x424, 0x10, 0x7F, 8);
  3132. ret = pmic_config_interface(0x426, 0x48, 0x7F, 0);
  3133. ret = pmic_config_interface(0x426, 0x78, 0x7F, 8);
  3134. ret = pmic_config_interface(0x428, 0x28, 0x7F, 0);
  3135. ret = pmic_config_interface(0x42E, 0x1, 0x1FF, 0);
  3136. ret = pmic_config_interface(0x446, 0x2, 0x7, 8);
  3137. ret = pmic_config_interface(0x44C, 0x0, 0x3, 7);
  3138. ret = pmic_config_interface(0x450, 0x80, 0xFF, 0);
  3139. ret = pmic_config_interface(0x452, 0x8, 0x3F, 3);
  3140. ret = pmic_config_interface(0x462, 0x3, 0x3, 10);
  3141. ret = pmic_config_interface(0x470, 0x2, 0x7, 8);
  3142. ret = pmic_config_interface(0x486, 0x1, 0x1, 0);
  3143. ret = pmic_config_interface(0x488, 0x11, 0x7F, 0);
  3144. ret = pmic_config_interface(0x488, 0x0, 0x1, 7);
  3145. ret = pmic_config_interface(0x488, 0x4, 0x7F, 8);
  3146. ret = pmic_config_interface(0x488, 0x0, 0x1, 15);
  3147. ret = pmic_config_interface(0x498, 0x1, 0x1, 8);
  3148. ret = pmic_config_interface(0x49A, 0x0, 0x3, 4);
  3149. ret = pmic_config_interface(0x49A, 0x0, 0x3, 8);
  3150. ret = pmic_config_interface(0x49A, 0x0, 0x3, 14);
  3151. ret = pmic_config_interface(0x4AA, 0x0, 0x1, 1);
  3152. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 0);
  3153. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 7);
  3154. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 8);
  3155. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 15);
  3156. ret = pmic_config_interface(0x4B4, 0x38, 0x7F, 0);
  3157. ret = pmic_config_interface(0x4B6, 0x28, 0x7F, 0);
  3158. ret = pmic_config_interface(0x4D2, 0x1, 0x1, 1);
  3159. ret = pmic_config_interface(0x4D8, 0x11, 0x7F, 0);
  3160. ret = pmic_config_interface(0x4D8, 0x4, 0x7F, 8);
  3161. ret = pmic_config_interface(0x4DE, 0x28, 0x7F, 0);
  3162. ret = pmic_config_interface(0x4E8, 0x3, 0x3, 0);
  3163. ret = pmic_config_interface(0x4E8, 0x1, 0x1, 8);
  3164. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 4);
  3165. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 8);
  3166. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 14);
  3167. ret = pmic_config_interface(0x60E, 0x1, 0x1, 1);
  3168. ret = pmic_config_interface(0x614, 0x11, 0x7F, 0);
  3169. ret = pmic_config_interface(0x614, 0x4, 0x7F, 8);
  3170. ret = pmic_config_interface(0x61A, 0x28, 0x7F, 0);
  3171. ret = pmic_config_interface(0x624, 0x3, 0x3, 0);
  3172. ret = pmic_config_interface(0x624, 0x1, 0x1, 8);
  3173. ret = pmic_config_interface(0x626, 0x0, 0x3, 4);
  3174. ret = pmic_config_interface(0x626, 0x0, 0x3, 8);
  3175. ret = pmic_config_interface(0x626, 0x0, 0x3, 14);
  3176. ret = pmic_config_interface(0x646, 0x5, 0x7, 0);
  3177. ret = pmic_config_interface(0x64C, 0x0, 0x1, 8);
  3178. ret = pmic_config_interface(0x664, 0x2, 0x7F, 0);
  3179. ret = pmic_config_interface(0x664, 0x0, 0x1, 7);
  3180. ret = pmic_config_interface(0x664, 0x2, 0x7F, 8);
  3181. ret = pmic_config_interface(0x664, 0x0, 0x1, 15);
  3182. ret = pmic_config_interface(0x674, 0x3, 0x3, 4);
  3183. ret = pmic_config_interface(0x67E, 0x2, 0x3, 4);
  3184. ret = pmic_config_interface(0xA00, 0x1, 0x1, 3);
  3185. ret = pmic_config_interface(0xA02, 0x1, 0x1, 3);
  3186. ret = pmic_config_interface(0xA06, 0x1, 0x1, 6);
  3187. ret = pmic_config_interface(0xA30, 0x0, 0x1, 0);
  3188. ret = pmic_config_interface(0xA44, 0x1, 0x1, 1);
  3189. ret = pmic_config_interface(0xA46, 0x0, 0x1, 14);
  3190. ret = pmic_config_interface(0xA64, 0x2, 0x3, 4);
  3191. ret = pmic_config_interface(0xA88, 0x68, 0x7F, 0);
  3192. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 8);
  3193. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 9);
  3194. ret = pmic_config_interface(0xCC0, 0x24, 0xFFFF, 0);
  3195. ret = pmic_config_interface(0xCC2, 0x14, 0xFF, 0);
  3196. ret = pmic_config_interface(0xCC4, 0xFF, 0xFF, 8);
  3197. ret = pmic_config_interface(0xE94, 0x0, 0x1, 13);
  3198. ret = pmic_config_interface(0xE94, 0x0, 0x1, 14);
  3199. ret = pmic_config_interface(0xE94, 0x0, 0x1, 15);
  3200. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 4);
  3201. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 6);
  3202. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 8);
  3203. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 10);
  3204. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 12);
  3205. ret = pmic_config_interface(0xEA4, 0x2, 0x3, 14);
  3206. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 0);
  3207. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 2);
  3208. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 4);
  3209. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 6);
  3210. ret = pmic_config_interface(0xEB8, 0x1, 0x1, 14);
  3211. ret = pmic_config_interface(0xF4A, 0xB, 0xF, 4);
  3212. ret = pmic_config_interface(0xF54, 0x0, 0x7, 1);
  3213. ret = pmic_config_interface(0xF62, 0x3, 0xF, 0);
  3214. ret = pmic_config_interface(0xF6C, 0x2, 0x1F, 0);
  3215. ret = pmic_config_interface(0xF70, 0x1, 0x1, 1);
  3216. ret = pmic_config_interface(0xF74, 0x0, 0x7, 4);
  3217. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 2);
  3218. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 6);
  3219. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 7);
  3220. } else {
  3221. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 2015-11-4...\n");
  3222. ret = pmic_config_interface(0x4, 0x1, 0x1, 4);
  3223. ret = pmic_config_interface(0xA, 0x1, 0x1, 0);
  3224. ret = pmic_config_interface(0xA, 0x1, 0x1, 11);
  3225. ret = pmic_config_interface(0xC, 0x1, 0x1, 0);
  3226. ret = pmic_config_interface(0xC, 0x1, 0x1, 1);
  3227. ret = pmic_config_interface(0xC, 0x1, 0x1, 4);
  3228. ret = pmic_config_interface(0xC, 0x1, 0x1, 5);
  3229. ret = pmic_config_interface(0xC, 0x1, 0x1, 6);
  3230. ret = pmic_config_interface(0xC, 0x1, 0x1, 7);
  3231. ret = pmic_config_interface(0xC, 0x1, 0x1, 8);
  3232. ret = pmic_config_interface(0xC, 0x1, 0x1, 9);
  3233. ret = pmic_config_interface(0xC, 0x1, 0x1, 10);
  3234. ret = pmic_config_interface(0xC, 0x1, 0x1, 11);
  3235. ret = pmic_config_interface(0xC, 0x1, 0x1, 12);
  3236. ret = pmic_config_interface(0xC, 0x1, 0x1, 13);
  3237. ret = pmic_config_interface(0x10, 0x1, 0x1, 5);
  3238. ret = pmic_config_interface(0x16, 0x1, 0x1, 0);
  3239. ret = pmic_config_interface(0x16, 0x1, 0x1, 1);
  3240. ret = pmic_config_interface(0x1E, 0x0, 0x1, 11);
  3241. ret = pmic_config_interface(0x40, 0x1, 0x1, 12);
  3242. ret = pmic_config_interface(0x204, 0x1, 0x1, 4);
  3243. ret = pmic_config_interface(0x204, 0x1, 0x1, 5);
  3244. ret = pmic_config_interface(0x204, 0x0, 0x1, 6);
  3245. ret = pmic_config_interface(0x226, 0x1, 0x1, 0);
  3246. ret = pmic_config_interface(0x226, 0x1, 0x1, 2);
  3247. ret = pmic_config_interface(0x226, 0x1, 0x1, 3);
  3248. ret = pmic_config_interface(0x242, 0x1, 0x1, 2);
  3249. ret = pmic_config_interface(0x242, 0x1, 0x1, 3);
  3250. ret = pmic_config_interface(0x248, 0x1, 0x1, 13);
  3251. ret = pmic_config_interface(0x248, 0x1, 0x1, 14);
  3252. ret = pmic_config_interface(0x25A, 0x1, 0x1, 9);
  3253. ret = pmic_config_interface(0x40E, 0x0, 0x3, 2);
  3254. ret = pmic_config_interface(0x412, 0x0, 0x3, 2);
  3255. ret = pmic_config_interface(0x420, 0x1, 0x1, 4);
  3256. ret = pmic_config_interface(0x422, 0x1, 0x1, 0);
  3257. ret = pmic_config_interface(0x422, 0x1, 0x1, 1);
  3258. ret = pmic_config_interface(0x422, 0x1, 0x1, 2);
  3259. ret = pmic_config_interface(0x424, 0x0, 0x7F, 0);
  3260. ret = pmic_config_interface(0x424, 0x10, 0x7F, 8);
  3261. ret = pmic_config_interface(0x426, 0x48, 0x7F, 0);
  3262. ret = pmic_config_interface(0x426, 0x78, 0x7F, 8);
  3263. ret = pmic_config_interface(0x428, 0x28, 0x7F, 0);
  3264. ret = pmic_config_interface(0x42E, 0x1, 0x1FF, 0);
  3265. ret = pmic_config_interface(0x446, 0x2, 0x7, 8);
  3266. ret = pmic_config_interface(0x44C, 0x0, 0x3, 7);
  3267. ret = pmic_config_interface(0x450, 0x80, 0xFF, 0);
  3268. ret = pmic_config_interface(0x452, 0x8, 0x3F, 3);
  3269. ret = pmic_config_interface(0x45A, 0x2, 0x7, 8);
  3270. ret = pmic_config_interface(0x462, 0x3, 0x3, 10);
  3271. ret = pmic_config_interface(0x470, 0x2, 0x7, 8);
  3272. ret = pmic_config_interface(0x482, 0x1, 0x1, 1);
  3273. ret = pmic_config_interface(0x488, 0x11, 0x7F, 0);
  3274. ret = pmic_config_interface(0x488, 0x1, 0x1, 7);
  3275. ret = pmic_config_interface(0x488, 0x4, 0x7F, 8);
  3276. ret = pmic_config_interface(0x488, 0x1, 0x1, 15);
  3277. ret = pmic_config_interface(0x48E, 0x28, 0x7F, 0);
  3278. ret = pmic_config_interface(0x498, 0x3, 0x3, 0);
  3279. ret = pmic_config_interface(0x498, 0x1, 0x3, 4);
  3280. ret = pmic_config_interface(0x498, 0x1, 0x1, 8);
  3281. ret = pmic_config_interface(0x49A, 0x0, 0x3, 4);
  3282. ret = pmic_config_interface(0x49A, 0x0, 0x3, 8);
  3283. ret = pmic_config_interface(0x49A, 0x0, 0x3, 14);
  3284. ret = pmic_config_interface(0x4AA, 0x1, 0x1, 1);
  3285. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 0);
  3286. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 7);
  3287. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 8);
  3288. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 15);
  3289. ret = pmic_config_interface(0x4B4, 0x40, 0x7F, 0);
  3290. ret = pmic_config_interface(0x4D2, 0x1, 0x1, 1);
  3291. ret = pmic_config_interface(0x4D8, 0x11, 0x7F, 0);
  3292. ret = pmic_config_interface(0x4D8, 0x4, 0x7F, 8);
  3293. ret = pmic_config_interface(0x4DE, 0x28, 0x7F, 0);
  3294. ret = pmic_config_interface(0x4E8, 0x3, 0x3, 0);
  3295. ret = pmic_config_interface(0x4E8, 0x1, 0x1, 8);
  3296. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 4);
  3297. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 8);
  3298. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 14);
  3299. ret = pmic_config_interface(0x60E, 0x1, 0x1, 1);
  3300. ret = pmic_config_interface(0x614, 0x11, 0x7F, 0);
  3301. ret = pmic_config_interface(0x614, 0x4, 0x7F, 8);
  3302. ret = pmic_config_interface(0x61A, 0x28, 0x7F, 0);
  3303. ret = pmic_config_interface(0x624, 0x3, 0x3, 0);
  3304. ret = pmic_config_interface(0x624, 0x1, 0x1, 8);
  3305. ret = pmic_config_interface(0x626, 0x0, 0x3, 4);
  3306. ret = pmic_config_interface(0x626, 0x0, 0x3, 8);
  3307. ret = pmic_config_interface(0x626, 0x0, 0x3, 14);
  3308. ret = pmic_config_interface(0x646, 0x5, 0x7, 0);
  3309. ret = pmic_config_interface(0x64C, 0x0, 0x1, 8);
  3310. ret = pmic_config_interface(0x664, 0x2, 0x7F, 0);
  3311. ret = pmic_config_interface(0x664, 0x0, 0x1, 7);
  3312. ret = pmic_config_interface(0x664, 0x2, 0x7F, 8);
  3313. ret = pmic_config_interface(0x664, 0x0, 0x1, 15);
  3314. ret = pmic_config_interface(0x674, 0x3, 0x3, 4);
  3315. ret = pmic_config_interface(0x67E, 0x2, 0x3, 4);
  3316. ret = pmic_config_interface(0xA00, 0x1, 0x1, 3);
  3317. ret = pmic_config_interface(0xA02, 0x1, 0x1, 3);
  3318. ret = pmic_config_interface(0xA06, 0x1, 0x1, 6);
  3319. ret = pmic_config_interface(0xA30, 0x0, 0x1, 0);
  3320. ret = pmic_config_interface(0xA44, 0x1, 0x1, 1);
  3321. ret = pmic_config_interface(0xA46, 0x0, 0x1, 14);
  3322. ret = pmic_config_interface(0xA64, 0x2, 0x3, 4);
  3323. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 8);
  3324. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 9);
  3325. ret = pmic_config_interface(0xCC0, 0x24, 0xFFFF, 0);
  3326. ret = pmic_config_interface(0xCC2, 0x14, 0xFF, 0);
  3327. ret = pmic_config_interface(0xCC4, 0xFF, 0xFF, 8);
  3328. ret = pmic_config_interface(0xE94, 0x0, 0x1, 13);
  3329. ret = pmic_config_interface(0xE94, 0x0, 0x1, 14);
  3330. ret = pmic_config_interface(0xE94, 0x0, 0x1, 15);
  3331. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 4);
  3332. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 6);
  3333. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 8);
  3334. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 10);
  3335. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 12);
  3336. ret = pmic_config_interface(0xEA4, 0x2, 0x3, 14);
  3337. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 0);
  3338. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 2);
  3339. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 4);
  3340. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 6);
  3341. ret = pmic_config_interface(0xEB8, 0x1, 0x1, 14);
  3342. ret = pmic_config_interface(0xF4A, 0xB, 0xF, 4);
  3343. ret = pmic_config_interface(0xF54, 0x0, 0x7, 1);
  3344. ret = pmic_config_interface(0xF62, 0x3, 0xF, 0);
  3345. ret = pmic_config_interface(0xF6C, 0x2, 0x1F, 0);
  3346. ret = pmic_config_interface(0xF70, 0x1, 0x1, 1);
  3347. ret = pmic_config_interface(0xF74, 0x0, 0x7, 4);
  3348. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 2);
  3349. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 6);
  3350. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 7);
  3351. pmic_setting_for_co_tsx();
  3352. }
  3353. /*--------------------------------------------------------*/
  3354. if (crystal_exist_status() == 0) {
  3355. PMICLOG("32k-less VTCXO always on...\n");
  3356. pmic_set_register_value(PMIC_RG_VTCXO_0_ON_CTRL, 0);
  3357. pmic_set_register_value(PMIC_RG_VTCXO_0_EN, 1);
  3358. }
  3359. #if defined(PMIC_HW_USE_4L_SS_LAYOUT)
  3360. ret = pmic_config_interface(0x494, 0x0, 0x3, 0); /* [1:0]: VPROC slow slew rate */
  3361. ret = pmic_config_interface(0x496, 0x0, 0x3, 0); /* [1:0]: VPROC slow slew rate */
  3362. ret = pmic_config_interface(0x620, 0x0, 0x3, 0); /* [1:0]: VCORE slow slew rate */
  3363. ret = pmic_config_interface(0x622, 0x0, 0x3, 0); /* [1:0]: VCORE slow slew rate */
  3364. ret = pmic_config_interface(0x4E4, 0x0, 0x3, 0); /* [1:0]: VLTE slow slew rate */
  3365. ret = pmic_config_interface(0x4E6, 0x0, 0x3, 0); /* [1:0]: VLTE slow slew rate */
  3366. ret = pmic_config_interface(0x648, 0x0, 0x3, 0); /* [1:0]: VSYS slow slew rate */
  3367. ret = pmic_config_interface(0x64A, 0x0, 0x3, 0); /* [1:0]: VSYS slow slew rate */
  3368. #endif
  3369. }
  3370. #if defined CONFIG_MTK_LEGACY
  3371. /*extern void pmu_drv_tool_customization_init(void);*/
  3372. #endif
  3373. void PMIC_CUSTOM_SETTING_V1(void)
  3374. {
  3375. #if 0
  3376. #if defined CONFIG_MTK_LEGACY
  3377. #if defined(CONFIG_MTK_FPGA)
  3378. #else
  3379. pmu_drv_tool_customization_init(); /* legacy DCT only */
  3380. #endif
  3381. #endif
  3382. #endif
  3383. }
  3384. /*****************************************************************************
  3385. * Dump all LDO status
  3386. ******************************************************************************/
  3387. void dump_ldo_status_read_debug(void)
  3388. {
  3389. int i, j;
  3390. int en = 0;
  3391. int voltage_reg = 0;
  3392. int voltage = 0;
  3393. const int *pVoltage;
  3394. pr_debug("********** BUCK/LDO status dump [1:ON,0:OFF]**********\n");
  3395. for (i = 0; i < ARRAY_SIZE(mtk_bucks); i++) {
  3396. if (mtk_bucks[i].qi_en_reg != 0)
  3397. en = pmic_get_register_value(mtk_bucks[i].qi_en_reg);
  3398. else
  3399. en = -1;
  3400. if (mtk_bucks[i].qi_vol_reg != 0) {
  3401. voltage_reg = pmic_get_register_value(mtk_bucks[i].qi_vol_reg);
  3402. voltage =
  3403. mtk_bucks[i].desc.min_uV + mtk_bucks[i].desc.uV_step * voltage_reg;
  3404. } else {
  3405. voltage_reg = -1;
  3406. voltage = -1;
  3407. }
  3408. pr_err("%s status:%d voltage:%duv voltage_reg:%d\n",
  3409. mtk_bucks[i].desc.name, en, voltage, voltage_reg);
  3410. }
  3411. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  3412. if (mtk_ldos[i].en_reg != 0)
  3413. en = pmic_get_register_value(mtk_ldos[i].en_reg);
  3414. else
  3415. en = -1;
  3416. if (mtk_ldos[i].desc.n_voltages != 1) {
  3417. if (mtk_ldos[i].vol_reg != 0) {
  3418. voltage_reg = pmic_get_register_value(mtk_ldos[i].vol_reg);
  3419. if (mtk_ldos[i].pvoltages != NULL) {
  3420. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3421. voltage = pVoltage[voltage_reg];
  3422. } else {
  3423. voltage =
  3424. mtk_ldos[i].desc.min_uV +
  3425. mtk_ldos[i].desc.uV_step * voltage_reg;
  3426. }
  3427. } else {
  3428. voltage_reg = -1;
  3429. voltage = -1;
  3430. }
  3431. } else {
  3432. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3433. voltage = pVoltage[0];
  3434. }
  3435. pr_err("%s status:%d voltage:%duv voltage_reg:%d\n",
  3436. mtk_ldos[i].desc.name, en, voltage, voltage_reg);
  3437. }
  3438. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  3439. for (j = 0; j < PMIC_INT_WIDTH; j++) {
  3440. PMICLOG("[PMIC_INT][%s] interrupt issue times: %d\n",
  3441. interrupts[i].interrupts[j].name,
  3442. interrupts[i].interrupts[j].times);
  3443. }
  3444. }
  3445. PMICLOG("Power Good Status=0x%x.\n", upmu_get_reg_value(0x21c));
  3446. PMICLOG("OC Status=0x%x.\n", upmu_get_reg_value(0x214));
  3447. PMICLOG("Thermal Status=0x%x.\n", upmu_get_reg_value(0x21e));
  3448. }
  3449. static int proc_utilization_show(struct seq_file *m, void *v)
  3450. {
  3451. int i, j;
  3452. int en = 0;
  3453. int voltage_reg = 0;
  3454. int voltage = 0;
  3455. const int *pVoltage;
  3456. seq_puts(m, "********** BUCK/LDO status dump [1:ON,0:OFF]**********\n");
  3457. for (i = 0; i < ARRAY_SIZE(mtk_bucks); i++) {
  3458. if (mtk_bucks[i].qi_en_reg != 0)
  3459. en = pmic_get_register_value(mtk_bucks[i].qi_en_reg);
  3460. else
  3461. en = -1;
  3462. if (mtk_bucks[i].qi_vol_reg != 0) {
  3463. voltage_reg = pmic_get_register_value(mtk_bucks[i].qi_vol_reg);
  3464. voltage =
  3465. mtk_bucks[i].desc.min_uV + mtk_bucks[i].desc.uV_step * voltage_reg;
  3466. } else {
  3467. voltage_reg = -1;
  3468. voltage = -1;
  3469. }
  3470. seq_printf(m, "%s status:%d voltage:%duv voltage_reg:%d\n",
  3471. mtk_bucks[i].desc.name, en, voltage, voltage_reg);
  3472. }
  3473. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  3474. if (mtk_ldos[i].en_reg != 0)
  3475. en = pmic_get_register_value(mtk_ldos[i].en_reg);
  3476. else
  3477. en = -1;
  3478. if (mtk_ldos[i].desc.n_voltages != 1) {
  3479. if (mtk_ldos[i].vol_reg != 0) {
  3480. voltage_reg = pmic_get_register_value(mtk_ldos[i].vol_reg);
  3481. if (mtk_ldos[i].pvoltages != NULL) {
  3482. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3483. voltage = pVoltage[voltage_reg];
  3484. } else {
  3485. voltage =
  3486. mtk_ldos[i].desc.min_uV +
  3487. mtk_ldos[i].desc.uV_step * voltage_reg;
  3488. }
  3489. } else {
  3490. voltage_reg = -1;
  3491. voltage = -1;
  3492. }
  3493. } else {
  3494. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3495. voltage = pVoltage[0];
  3496. }
  3497. seq_printf(m, "%s status:%d voltage:%duv voltage_reg:%d\n",
  3498. mtk_ldos[i].desc.name, en, voltage, voltage_reg);
  3499. }
  3500. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  3501. for (j = 0; j < PMIC_INT_WIDTH; j++) {
  3502. seq_printf(m, "[PMIC_INT][%s] interrupt issue times: %d\n",
  3503. interrupts[i].interrupts[j].name,
  3504. interrupts[i].interrupts[j].times);
  3505. }
  3506. }
  3507. seq_printf(m, "Power Good Status=0x%x.\n", upmu_get_reg_value(0x21c));
  3508. seq_printf(m, "OC Status=0x%x.\n", upmu_get_reg_value(0x214));
  3509. seq_printf(m, "Thermal Status=0x%x.\n", upmu_get_reg_value(0x21e));
  3510. return 0;
  3511. }
  3512. static int proc_utilization_open(struct inode *inode, struct file *file)
  3513. {
  3514. return single_open(file, proc_utilization_show, NULL);
  3515. }
  3516. static const struct file_operations pmic_debug_proc_fops = {
  3517. .open = proc_utilization_open,
  3518. .read = seq_read,
  3519. };
  3520. static int proc_dump_register_show(struct seq_file *m, void *v)
  3521. {
  3522. int i;
  3523. seq_puts(m, "********** dump PMIC registers**********\n");
  3524. for (i = 0; i <= 0x0fae; i = i + 10) {
  3525. seq_printf(m, "Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x\n",
  3526. i, upmu_get_reg_value(i), i + 1, upmu_get_reg_value(i + 1), i + 2,
  3527. upmu_get_reg_value(i + 2), i + 3, upmu_get_reg_value(i + 3), i + 4,
  3528. upmu_get_reg_value(i + 4));
  3529. seq_printf(m, "Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x\n",
  3530. i + 5, upmu_get_reg_value(i + 5), i + 6, upmu_get_reg_value(i + 6),
  3531. i + 7, upmu_get_reg_value(i + 7), i + 8, upmu_get_reg_value(i + 8),
  3532. i + 9, upmu_get_reg_value(i + 9));
  3533. }
  3534. return 0;
  3535. }
  3536. static int proc_dump_register_open(struct inode *inode, struct file *file)
  3537. {
  3538. return single_open(file, proc_dump_register_show, NULL);
  3539. }
  3540. static const struct file_operations pmic_dump_register_proc_fops = {
  3541. .open = proc_dump_register_open,
  3542. .read = seq_read,
  3543. };
  3544. void pmic_debug_init(void)
  3545. {
  3546. struct proc_dir_entry *mt_pmic_dir;
  3547. mt_pmic_dir = proc_mkdir("mt_pmic", NULL);
  3548. if (!mt_pmic_dir) {
  3549. PMICLOG("fail to mkdir /proc/mt_pmic\n");
  3550. return;
  3551. }
  3552. proc_create("dump_ldo_status", S_IRUGO | S_IWUSR, mt_pmic_dir, &pmic_debug_proc_fops);
  3553. PMICLOG("proc_create pmic_debug_proc_fops\n");
  3554. proc_create("dump_pmic_reg", S_IRUGO | S_IWUSR, mt_pmic_dir, &pmic_dump_register_proc_fops);
  3555. PMICLOG("proc_create pmic_dump_register_proc_fops\n");
  3556. }
  3557. static bool pwrkey_detect_flag;
  3558. static struct hrtimer pwrkey_detect_timer;
  3559. static struct task_struct *pwrkey_detect_thread;
  3560. static DECLARE_WAIT_QUEUE_HEAD(pwrkey_detect_waiter);
  3561. #define BAT_MS_TO_NS(x) (x * 1000 * 1000)
  3562. enum hrtimer_restart pwrkey_detect_sw_workaround(struct hrtimer *timer)
  3563. {
  3564. pwrkey_detect_flag = true;
  3565. wake_up_interruptible(&pwrkey_detect_waiter);
  3566. return HRTIMER_NORESTART;
  3567. }
  3568. int pwrkey_detect_sw_thread_handler(void *unused)
  3569. {
  3570. ktime_t ktime;
  3571. do {
  3572. ktime = ktime_set(3, BAT_MS_TO_NS(1000));
  3573. wait_event_interruptible(pwrkey_detect_waiter, (pwrkey_detect_flag == true));
  3574. /*PMICLOG("=>charger_hv_detect_sw_workaround\n"); */
  3575. if (pmic_get_register_value(PMIC_RG_STRUP_75K_CK_PDN) == 1) {
  3576. PMICLOG("charger_hv_detect_sw_workaround =0x%x\n",
  3577. upmu_get_reg_value(0x24e));
  3578. pmic_set_register_value(PMIC_RG_STRUP_75K_CK_PDN, 0);
  3579. }
  3580. hrtimer_start(&pwrkey_detect_timer, ktime, HRTIMER_MODE_REL);
  3581. } while (!kthread_should_stop());
  3582. return 0;
  3583. }
  3584. void pwrkey_sw_workaround_init(void)
  3585. {
  3586. ktime_t ktime;
  3587. ktime = ktime_set(0, BAT_MS_TO_NS(2000));
  3588. hrtimer_init(&pwrkey_detect_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3589. pwrkey_detect_timer.function = pwrkey_detect_sw_workaround;
  3590. hrtimer_start(&pwrkey_detect_timer, ktime, HRTIMER_MODE_REL);
  3591. pwrkey_detect_thread =
  3592. kthread_run(pwrkey_detect_sw_thread_handler, 0, "mtk pwrkey_sw_workaround_init");
  3593. if (IS_ERR(pwrkey_detect_thread))
  3594. PMICLOG("[%s]: failed to create pwrkey_detect_thread thread\n", __func__);
  3595. }
  3596. #ifdef LOW_BATTERY_PROTECT
  3597. /*****************************************************************************
  3598. * low battery protect UT
  3599. ******************************************************************************/
  3600. static ssize_t show_low_battery_protect_ut(struct device *dev, struct device_attribute *attr,
  3601. char *buf)
  3602. {
  3603. PMICLOG("[show_low_battery_protect_ut] g_low_battery_level=%d\n", g_low_battery_level);
  3604. return sprintf(buf, "%u\n", g_low_battery_level);
  3605. }
  3606. static ssize_t store_low_battery_protect_ut(struct device *dev, struct device_attribute *attr,
  3607. const char *buf, size_t size)
  3608. {
  3609. int ret = 0;
  3610. char *pvalue = NULL;
  3611. unsigned int val = 0;
  3612. pr_err("[store_low_battery_protect_ut]\n");
  3613. if (buf != NULL && size != 0) {
  3614. pr_err("[store_low_battery_protect_ut] buf is %s and size is %zu\n", buf, size);
  3615. /*val = simple_strtoul(buf, &pvalue, 16); */
  3616. pvalue = (char *)buf;
  3617. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3618. if (val <= 2) {
  3619. pr_err("[store_low_battery_protect_ut] your input is %d\n", val);
  3620. exec_low_battery_callback(val);
  3621. } else {
  3622. pr_err("[store_low_battery_protect_ut] wrong number (%d)\n", val);
  3623. }
  3624. }
  3625. return size;
  3626. }
  3627. static DEVICE_ATTR(low_battery_protect_ut, 0664, show_low_battery_protect_ut, store_low_battery_protect_ut); /*664 */
  3628. /*****************************************************************************
  3629. * low battery protect stop
  3630. ******************************************************************************/
  3631. static ssize_t show_low_battery_protect_stop(struct device *dev, struct device_attribute *attr,
  3632. char *buf)
  3633. {
  3634. PMICLOG("[show_low_battery_protect_stop] g_low_battery_stop=%d\n", g_low_battery_stop);
  3635. return sprintf(buf, "%u\n", g_low_battery_stop);
  3636. }
  3637. static ssize_t store_low_battery_protect_stop(struct device *dev, struct device_attribute *attr,
  3638. const char *buf, size_t size)
  3639. {
  3640. int ret = 0;
  3641. char *pvalue = NULL;
  3642. unsigned int val = 0;
  3643. pr_err("[store_low_battery_protect_stop]\n");
  3644. if (buf != NULL && size != 0) {
  3645. pr_err("[store_low_battery_protect_stop] buf is %s and size is %zu\n", buf, size);
  3646. /*val = simple_strtoul(buf, &pvalue, 16); */
  3647. pvalue = (char *)buf;
  3648. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3649. if ((val != 0) && (val != 1))
  3650. val = 0;
  3651. g_low_battery_stop = val;
  3652. pr_err("[store_low_battery_protect_stop] g_low_battery_stop=%d\n",
  3653. g_low_battery_stop);
  3654. }
  3655. return size;
  3656. }
  3657. static DEVICE_ATTR(low_battery_protect_stop, 0664,
  3658. show_low_battery_protect_stop, store_low_battery_protect_stop); /*664*/
  3659. /*
  3660. * low battery protect level
  3661. */
  3662. static ssize_t show_low_battery_protect_level(struct device *dev, struct device_attribute *attr,
  3663. char *buf)
  3664. {
  3665. PMICLOG("[show_low_battery_protect_level] g_low_battery_level=%d\n", g_low_battery_level);
  3666. return sprintf(buf, "%u\n", g_low_battery_level);
  3667. }
  3668. static ssize_t store_low_battery_protect_level(struct device *dev, struct device_attribute *attr,
  3669. const char *buf, size_t size)
  3670. {
  3671. PMICLOG("[store_low_battery_protect_level] g_low_battery_level=%d\n", g_low_battery_level);
  3672. return size;
  3673. }
  3674. static DEVICE_ATTR(low_battery_protect_level, 0664,
  3675. show_low_battery_protect_level, store_low_battery_protect_level); /*664*/
  3676. #endif
  3677. #ifdef BATTERY_OC_PROTECT
  3678. /*****************************************************************************
  3679. * battery OC protect UT
  3680. ******************************************************************************/
  3681. static ssize_t show_battery_oc_protect_ut(struct device *dev, struct device_attribute *attr,
  3682. char *buf)
  3683. {
  3684. PMICLOG("[show_battery_oc_protect_ut] g_battery_oc_level=%d\n", g_battery_oc_level);
  3685. return sprintf(buf, "%u\n", g_battery_oc_level);
  3686. }
  3687. static ssize_t store_battery_oc_protect_ut(struct device *dev, struct device_attribute *attr,
  3688. const char *buf, size_t size)
  3689. {
  3690. int ret = 0;
  3691. char *pvalue = NULL;
  3692. unsigned int val = 0;
  3693. pr_err("[store_battery_oc_protect_ut]\n");
  3694. if (buf != NULL && size != 0) {
  3695. pr_err("[store_battery_oc_protect_ut] buf is %s and size is %zu\n", buf, size);
  3696. /*val = simple_strtoul(buf, &pvalue, 16); */
  3697. pvalue = (char *)buf;
  3698. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3699. if (val <= 1) {
  3700. pr_err("[store_battery_oc_protect_ut] your input is %d\n", val);
  3701. exec_battery_oc_callback(val);
  3702. } else {
  3703. pr_err("[store_battery_oc_protect_ut] wrong number (%d)\n", val);
  3704. }
  3705. }
  3706. return size;
  3707. }
  3708. static DEVICE_ATTR(battery_oc_protect_ut, 0664,
  3709. show_battery_oc_protect_ut, store_battery_oc_protect_ut); /*664*/
  3710. /*****************************************************************************
  3711. * battery OC protect stop
  3712. ******************************************************************************/
  3713. static ssize_t show_battery_oc_protect_stop(struct device *dev, struct device_attribute *attr,
  3714. char *buf)
  3715. {
  3716. PMICLOG("[show_battery_oc_protect_stop] g_battery_oc_stop=%d\n", g_battery_oc_stop);
  3717. return sprintf(buf, "%u\n", g_battery_oc_stop);
  3718. }
  3719. static ssize_t store_battery_oc_protect_stop(struct device *dev, struct device_attribute *attr,
  3720. const char *buf, size_t size)
  3721. {
  3722. int ret = 0;
  3723. char *pvalue = NULL;
  3724. unsigned int val = 0;
  3725. pr_err("[store_battery_oc_protect_stop]\n");
  3726. if (buf != NULL && size != 0) {
  3727. pr_err("[store_battery_oc_protect_stop] buf is %s and size is %zu\n", buf, size);
  3728. /*val = simple_strtoul(buf, &pvalue, 16); */
  3729. pvalue = (char *)buf;
  3730. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3731. if ((val != 0) && (val != 1))
  3732. val = 0;
  3733. g_battery_oc_stop = val;
  3734. pr_err("[store_battery_oc_protect_stop] g_battery_oc_stop=%d\n", g_battery_oc_stop);
  3735. }
  3736. return size;
  3737. }
  3738. static DEVICE_ATTR(battery_oc_protect_stop, 0664,
  3739. show_battery_oc_protect_stop, store_battery_oc_protect_stop); /*664 */
  3740. /*****************************************************************************
  3741. * battery OC protect level
  3742. ******************************************************************************/
  3743. static ssize_t show_battery_oc_protect_level(struct device *dev, struct device_attribute *attr,
  3744. char *buf)
  3745. {
  3746. PMICLOG("[show_battery_oc_protect_level] g_battery_oc_level=%d\n", g_battery_oc_level);
  3747. return sprintf(buf, "%u\n", g_battery_oc_level);
  3748. }
  3749. static ssize_t store_battery_oc_protect_level(struct device *dev, struct device_attribute *attr,
  3750. const char *buf, size_t size)
  3751. {
  3752. PMICLOG("[store_battery_oc_protect_level] g_battery_oc_level=%d\n", g_battery_oc_level);
  3753. return size;
  3754. }
  3755. static DEVICE_ATTR(battery_oc_protect_level, 0664,
  3756. show_battery_oc_protect_level, store_battery_oc_protect_level); /*664 */
  3757. #endif
  3758. #ifdef BATTERY_PERCENT_PROTECT
  3759. /*****************************************************************************
  3760. * battery percent protect UT
  3761. ******************************************************************************/
  3762. static ssize_t show_battery_percent_ut(struct device *dev, struct device_attribute *attr, char *buf)
  3763. {
  3764. /*show_battery_percent_protect_ut */
  3765. PMICLOG("[show_battery_percent_protect_ut] g_battery_percent_level=%d\n",
  3766. g_battery_percent_level);
  3767. return sprintf(buf, "%u\n", g_battery_percent_level);
  3768. }
  3769. static ssize_t store_battery_percent_ut(struct device *dev, struct device_attribute *attr,
  3770. const char *buf, size_t size)
  3771. {
  3772. int ret = 0;
  3773. char *pvalue = NULL;
  3774. unsigned int val = 0;
  3775. /*store_battery_percent_protect_ut */
  3776. pr_err("[store_battery_percent_protect_ut]\n");
  3777. if (buf != NULL && size != 0) {
  3778. pr_err("[store_battery_percent_protect_ut] buf is %s and size is %zu\n", buf, size);
  3779. /*val = simple_strtoul(buf, &pvalue, 16); */
  3780. pvalue = (char *)buf;
  3781. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3782. if (val <= 1) {
  3783. pr_err("[store_battery_percent_protect_ut] your input is %d\n", val);
  3784. exec_battery_percent_callback(val);
  3785. } else {
  3786. pr_err("[store_battery_percent_protect_ut] wrong number (%d)\n", val);
  3787. }
  3788. }
  3789. return size;
  3790. }
  3791. static DEVICE_ATTR(battery_percent_protect_ut, 0664, show_battery_percent_ut, store_battery_percent_ut); /*664 */
  3792. /*
  3793. * battery percent protect stop
  3794. ******************************************************************************/
  3795. static ssize_t show_battery_percent_stop(struct device *dev, struct device_attribute *attr,
  3796. char *buf)
  3797. {
  3798. /*show_battery_percent_protect_stop */
  3799. PMICLOG("[show_battery_percent_protect_stop] g_battery_percent_stop=%d\n",
  3800. g_battery_percent_stop);
  3801. return sprintf(buf, "%u\n", g_battery_percent_stop);
  3802. }
  3803. static ssize_t store_battery_percent_stop(struct device *dev, struct device_attribute *attr,
  3804. const char *buf, size_t size)
  3805. {
  3806. int ret = 0;
  3807. char *pvalue = NULL;
  3808. unsigned int val = 0;
  3809. /*store_battery_percent_protect_stop */
  3810. pr_err("[store_battery_percent_protect_stop]\n");
  3811. if (buf != NULL && size != 0) {
  3812. pr_err("[store_battery_percent_protect_stop] buf is %s and size is %zu\n", buf,
  3813. size);
  3814. /*val = simple_strtoul(buf, &pvalue, 16); */
  3815. pvalue = (char *)buf;
  3816. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3817. if ((val != 0) && (val != 1))
  3818. val = 0;
  3819. g_battery_percent_stop = val;
  3820. pr_err("[store_battery_percent_protect_stop] g_battery_percent_stop=%d\n",
  3821. g_battery_percent_stop);
  3822. }
  3823. return size;
  3824. }
  3825. static DEVICE_ATTR(battery_percent_protect_stop, 0664, show_battery_percent_stop, store_battery_percent_stop); /*664 */
  3826. /*
  3827. * battery percent protect level
  3828. ******************************************************************************/
  3829. static ssize_t show_battery_percent_level(struct device *dev, struct device_attribute *attr,
  3830. char *buf)
  3831. {
  3832. /*show_battery_percent_protect_level */
  3833. PMICLOG("[show_battery_percent_protect_level] g_battery_percent_level=%d\n",
  3834. g_battery_percent_level);
  3835. return sprintf(buf, "%u\n", g_battery_percent_level);
  3836. }
  3837. static ssize_t store_battery_percent_level(struct device *dev,
  3838. struct device_attribute *attr, const char *buf,
  3839. size_t size)
  3840. {
  3841. /*store_battery_percent_protect_level */
  3842. PMICLOG("[store_battery_percent_protect_level] g_battery_percent_level=%d\n",
  3843. g_battery_percent_level);
  3844. return size;
  3845. }
  3846. static DEVICE_ATTR(battery_percent_protect_level, 0664,
  3847. show_battery_percent_level, store_battery_percent_level); /*664 */
  3848. #endif
  3849. #ifdef DLPT_FEATURE_SUPPORT
  3850. /*****************************************************************************
  3851. * DLPT UT
  3852. ******************************************************************************/
  3853. static ssize_t show_dlpt_ut(struct device *dev, struct device_attribute *attr, char *buf)
  3854. {
  3855. PMICLOG("[show_dlpt_ut] g_dlpt_val=%d\n", g_dlpt_val);
  3856. return sprintf(buf, "%u\n", g_dlpt_val);
  3857. }
  3858. static ssize_t store_dlpt_ut(struct device *dev, struct device_attribute *attr, const char *buf,
  3859. size_t size)
  3860. {
  3861. char *pvalue = NULL;
  3862. unsigned int val = 0;
  3863. int ret = 0;
  3864. pr_err("[store_dlpt_ut]\n");
  3865. if (buf != NULL && size != 0) {
  3866. pr_err("[store_dlpt_ut] buf is %s and size is %zu\n", buf, size);
  3867. /*val = simple_strtoul(buf, &pvalue, 10); */
  3868. pvalue = (char *)buf;
  3869. ret = kstrtou32(pvalue, 10, (unsigned int *)&val);
  3870. pr_err("[store_dlpt_ut] your input is %d\n", val);
  3871. exec_dlpt_callback(val);
  3872. }
  3873. return size;
  3874. }
  3875. static DEVICE_ATTR(dlpt_ut, 0664, show_dlpt_ut, store_dlpt_ut); /*664 */
  3876. /*****************************************************************************
  3877. * DLPT stop
  3878. ******************************************************************************/
  3879. static ssize_t show_dlpt_stop(struct device *dev, struct device_attribute *attr, char *buf)
  3880. {
  3881. PMICLOG("[show_dlpt_stop] g_dlpt_stop=%d\n", g_dlpt_stop);
  3882. return sprintf(buf, "%u\n", g_dlpt_stop);
  3883. }
  3884. static ssize_t store_dlpt_stop(struct device *dev, struct device_attribute *attr, const char *buf,
  3885. size_t size)
  3886. {
  3887. char *pvalue = NULL;
  3888. unsigned int val = 0;
  3889. int ret = 0;
  3890. pr_err("[store_dlpt_stop]\n");
  3891. if (buf != NULL && size != 0) {
  3892. pr_err("[store_dlpt_stop] buf is %s and size is %zu\n", buf, size);
  3893. /*val = simple_strtoul(buf, &pvalue, 16); */
  3894. pvalue = (char *)buf;
  3895. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3896. if ((val != 0) && (val != 1))
  3897. val = 0;
  3898. g_dlpt_stop = val;
  3899. pr_err("[store_dlpt_stop] g_dlpt_stop=%d\n", g_dlpt_stop);
  3900. }
  3901. return size;
  3902. }
  3903. static DEVICE_ATTR(dlpt_stop, 0664, show_dlpt_stop, store_dlpt_stop); /*664 */
  3904. /*****************************************************************************
  3905. * DLPT level
  3906. ******************************************************************************/
  3907. static ssize_t show_dlpt_level(struct device *dev, struct device_attribute *attr, char *buf)
  3908. {
  3909. PMICLOG("[show_dlpt_level] g_dlpt_val=%d\n", g_dlpt_val);
  3910. return sprintf(buf, "%u\n", g_dlpt_val);
  3911. }
  3912. static ssize_t store_dlpt_level(struct device *dev, struct device_attribute *attr, const char *buf,
  3913. size_t size)
  3914. {
  3915. PMICLOG("[store_dlpt_level] g_dlpt_val=%d\n", g_dlpt_val);
  3916. return size;
  3917. }
  3918. static DEVICE_ATTR(dlpt_level, 0664, show_dlpt_level, store_dlpt_level); /*664 */
  3919. #endif
  3920. /*****************************************************************************
  3921. * system function
  3922. ******************************************************************************/
  3923. #ifdef DLPT_FEATURE_SUPPORT
  3924. static unsigned long pmic_node;
  3925. static int fb_early_init_dt_get_chosen(unsigned long node, const char *uname, int depth, void *data)
  3926. {
  3927. if (depth != 1 || (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  3928. return 0;
  3929. pmic_node = node;
  3930. return 1;
  3931. }
  3932. #endif /*end of #ifdef DLPT_FEATURE_SUPPORT */
  3933. static int pmic_mt_probe(struct platform_device *dev)
  3934. {
  3935. int ret_device_file = 0, i;
  3936. #ifdef DLPT_FEATURE_SUPPORT
  3937. const int *pimix;
  3938. int len = 0;
  3939. #endif
  3940. #if defined(CONFIG_MTK_SMART_BATTERY)
  3941. struct device_node *np;
  3942. u32 val;
  3943. /* check customer setting */
  3944. np = of_find_compatible_node(NULL, NULL, "mediatek,bat_meter");
  3945. if (np == NULL)
  3946. PMICLOG("[PMIC]pmic_mt_probe get bat_meter node failed\n");
  3947. if (of_property_read_u32(np, "car_tune_value", &val) == 0) {
  3948. batt_meter_cust_data.car_tune_value = (int)val;
  3949. PMICLOG("Get car_tune_value from DT: %d\n", batt_meter_cust_data.car_tune_value);
  3950. } else {
  3951. batt_meter_cust_data.car_tune_value = CAR_TUNE_VALUE;
  3952. PMICLOG("Get car_tune_value from cust header\n");
  3953. }
  3954. #endif
  3955. /*--- initailize pmic_suspend_state ---*/
  3956. pmic_suspend_state = false;
  3957. #ifdef DLPT_FEATURE_SUPPORT
  3958. pimix = NULL;
  3959. if (of_scan_flat_dt(fb_early_init_dt_get_chosen, NULL) > 0)
  3960. pimix = of_get_flat_dt_prop(pmic_node, "atag,imix_r", &len);
  3961. if (pimix == NULL) {
  3962. pr_err(" pimix==NULL len=%d\n", len);
  3963. } else {
  3964. pr_err(" pimix=%d\n", *pimix);
  3965. ptim_rac_val_avg = *pimix;
  3966. }
  3967. PMICLOG("******** MT pmic driver probe!! ********%d\n", ptim_rac_val_avg);
  3968. pr_debug("[PMIC]pmic_mt_probe %s %s\n", dev->name, dev->id_entry->name);
  3969. #endif /* #ifdef DLPT_FEATURE_SUPPORT */
  3970. /*get PMIC CID */
  3971. pr_debug
  3972. ("PMIC CID=0x%x PowerGoodStatus = 0x%x OCStatus = 0x%x ThermalStatus = 0x%x rsvStatus = 0x%x\n",
  3973. pmic_get_register_value(PMIC_SWCID), upmu_get_reg_value(0x21c),
  3974. upmu_get_reg_value(0x214), upmu_get_reg_value(0x21e), upmu_get_reg_value(0x2a6));
  3975. upmu_set_reg_value(0x2a6, 0xff);
  3976. #if defined(CONFIG_ARCH_MT6753)
  3977. PMICLOG("[PMIC_INIT_SETTING_V1] delay to MT6311 init\n");
  3978. #else
  3979. PMIC_INIT_SETTING_V1();
  3980. PMICLOG("[PMIC_INIT_SETTING_V1] Done\n");
  3981. #endif
  3982. #if !defined CONFIG_MTK_LEGACY
  3983. /* replace by DTS*/
  3984. #else
  3985. PMIC_CUSTOM_SETTING_V1();
  3986. PMICLOG("[PMIC_CUSTOM_SETTING_V1] Done\n");
  3987. #endif /*End of #if !defined CONFIG_MTK_LEGACY */
  3988. /*#if defined(CONFIG_MTK_FPGA)*/
  3989. #if 0
  3990. PMICLOG("[PMIC_EINT_SETTING] disable when CONFIG_MTK_FPGA\n");
  3991. #else
  3992. /*PMIC Interrupt Service */
  3993. PMIC_EINT_SETTING();
  3994. PMICLOG("[PMIC_EINT_SETTING] Done\n");
  3995. pmic_thread_handle = kthread_create(pmic_thread_kthread, (void *)NULL, "pmic_thread");
  3996. if (IS_ERR(pmic_thread_handle)) {
  3997. pmic_thread_handle = NULL;
  3998. PMICLOG("[pmic_thread_kthread_mt6325] creation fails\n");
  3999. } else {
  4000. wake_up_process(pmic_thread_handle);
  4001. PMICLOG("[pmic_thread_kthread_mt6325] kthread_create Done\n");
  4002. }
  4003. #endif
  4004. mtk_regulator_init(dev);
  4005. #ifdef LOW_BATTERY_PROTECT
  4006. low_battery_protect_init();
  4007. #else
  4008. pr_err("[PMIC] no define LOW_BATTERY_PROTECT\n");
  4009. #endif
  4010. #ifdef BATTERY_OC_PROTECT
  4011. battery_oc_protect_init();
  4012. #else
  4013. pr_err("[PMIC] no define BATTERY_OC_PROTECT\n");
  4014. #endif
  4015. #ifdef BATTERY_PERCENT_PROTECT
  4016. bat_percent_notify_init();
  4017. #else
  4018. pr_err("[PMIC] no define BATTERY_PERCENT_PROTECT\n");
  4019. #endif
  4020. #ifdef DLPT_FEATURE_SUPPORT
  4021. dlpt_notify_init();
  4022. #else
  4023. pr_err("[PMIC] no define DLPT_FEATURE_SUPPORT\n");
  4024. #endif
  4025. #if 1
  4026. pmic_set_register_value(PMIC_AUXADC_CK_AON, 1);
  4027. pmic_set_register_value(PMIC_RG_CLKSQ_EN_AUX_AP_MODE, 1);
  4028. PMICLOG("[PMIC] auxadc 26M test : Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4029. MT6328_AUXADC_CON0, upmu_get_reg_value(MT6328_AUXADC_CON0),
  4030. MT6328_TOP_CLKSQ, upmu_get_reg_value(MT6328_TOP_CLKSQ)
  4031. );
  4032. #endif
  4033. pmic_debug_init();
  4034. PMICLOG("[PMIC] pmic_debug_init : done.\n");
  4035. pmic_ftm_init();
  4036. /*EM BUCK voltage & Status */
  4037. for (i = 0; i < ARRAY_SIZE(mtk_bucks); i++) {
  4038. /*PMICLOG("[PMIC] register buck id=%d\n",i); */
  4039. ret_device_file = device_create_file(&(dev->dev), &mtk_bucks[i].en_att);
  4040. ret_device_file = device_create_file(&(dev->dev), &mtk_bucks[i].voltage_att);
  4041. }
  4042. /*EM ldo voltage & Status */
  4043. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  4044. /*PMICLOG("[PMIC] register ldo id=%d\n",i); */
  4045. ret_device_file = device_create_file(&(dev->dev), &mtk_ldos[i].en_att);
  4046. ret_device_file = device_create_file(&(dev->dev), &mtk_ldos[i].voltage_att);
  4047. }
  4048. ret_device_file = device_create_file(&(dev->dev), &dev_attr_pmic_access);
  4049. ret_device_file = device_create_file(&(dev->dev), &dev_attr_pmic_dvt);
  4050. #ifdef LOW_BATTERY_PROTECT
  4051. ret_device_file = device_create_file(&(dev->dev), &dev_attr_low_battery_protect_ut);
  4052. ret_device_file = device_create_file(&(dev->dev), &dev_attr_low_battery_protect_stop);
  4053. ret_device_file = device_create_file(&(dev->dev), &dev_attr_low_battery_protect_level);
  4054. #endif
  4055. #ifdef BATTERY_OC_PROTECT
  4056. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_oc_protect_ut);
  4057. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_oc_protect_stop);
  4058. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_oc_protect_level);
  4059. #endif
  4060. #ifdef BATTERY_PERCENT_PROTECT
  4061. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_percent_protect_ut);
  4062. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_percent_protect_stop);
  4063. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_percent_protect_level);
  4064. #endif
  4065. #ifdef DLPT_FEATURE_SUPPORT
  4066. ret_device_file = device_create_file(&(dev->dev), &dev_attr_dlpt_ut);
  4067. ret_device_file = device_create_file(&(dev->dev), &dev_attr_dlpt_stop);
  4068. ret_device_file = device_create_file(&(dev->dev), &dev_attr_dlpt_level);
  4069. #endif
  4070. PMICLOG("[PMIC] device_create_file for EM : done.\n");
  4071. /*pwrkey_sw_workaround_init(); */
  4072. return 0;
  4073. }
  4074. static int pmic_mt_remove(struct platform_device *dev)
  4075. {
  4076. PMICLOG("******** MT pmic driver remove!! ********\n");
  4077. return 0;
  4078. }
  4079. static void pmic_mt_shutdown(struct platform_device *dev)
  4080. {
  4081. PMICLOG("******** MT pmic driver shutdown!! ********\n");
  4082. pmic_md_power_on();
  4083. }
  4084. static int pmic_mt_suspend(struct platform_device *dev, pm_message_t state)
  4085. {
  4086. pmic_suspend_state = true;
  4087. PMICLOG("******** MT pmic driver suspend!! ********\n");
  4088. #ifdef LOW_BATTERY_PROTECT
  4089. lbat_min_en_setting(0);
  4090. lbat_max_en_setting(0);
  4091. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4092. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  4093. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  4094. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  4095. );
  4096. #endif
  4097. #ifdef BATTERY_OC_PROTECT
  4098. bat_oc_h_en_setting(0);
  4099. bat_oc_l_en_setting(0);
  4100. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4101. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  4102. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  4103. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  4104. );
  4105. #endif
  4106. return 0;
  4107. }
  4108. static int pmic_mt_resume(struct platform_device *dev)
  4109. {
  4110. pmic_suspend_state = false;
  4111. PMICLOG("******** MT pmic driver resume!! ********\n");
  4112. #ifdef LOW_BATTERY_PROTECT
  4113. lbat_min_en_setting(0);
  4114. lbat_max_en_setting(0);
  4115. mdelay(1);
  4116. if (g_low_battery_level == 1) {
  4117. lbat_min_en_setting(1);
  4118. lbat_max_en_setting(1);
  4119. } else if (g_low_battery_level == 2) {
  4120. /*lbat_min_en_setting(0); */
  4121. lbat_max_en_setting(1);
  4122. } else { /*0 */
  4123. lbat_min_en_setting(1);
  4124. /*lbat_max_en_setting(0); */
  4125. }
  4126. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4127. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  4128. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  4129. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  4130. );
  4131. #endif
  4132. #ifdef BATTERY_OC_PROTECT
  4133. bat_oc_h_en_setting(0);
  4134. bat_oc_l_en_setting(0);
  4135. mdelay(1);
  4136. if (g_battery_oc_level == 1)
  4137. bat_oc_h_en_setting(1);
  4138. else
  4139. bat_oc_l_en_setting(1);
  4140. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4141. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  4142. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  4143. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  4144. );
  4145. #endif
  4146. return 0;
  4147. }
  4148. struct platform_device pmic_mt_device = {
  4149. .name = "mt-pmic",
  4150. .id = -1,
  4151. };
  4152. static struct platform_driver pmic_mt_driver = {
  4153. .probe = pmic_mt_probe,
  4154. .remove = pmic_mt_remove,
  4155. .shutdown = pmic_mt_shutdown,
  4156. /*#ifdef CONFIG_PM */
  4157. .suspend = pmic_mt_suspend,
  4158. .resume = pmic_mt_resume,
  4159. /*#endif */
  4160. .driver = {
  4161. .name = "mt-pmic",
  4162. },
  4163. };
  4164. static DEFINE_MUTEX(pmic_efuse_lock_mutex);
  4165. unsigned int pmic_Read_Efuse_HPOffset(int i)
  4166. {
  4167. unsigned int ret = 0;
  4168. unsigned int reg_val = 0;
  4169. unsigned int efusevalue;
  4170. pr_debug("pmic_Read_Efuse_HPOffset(+)\n");
  4171. mutex_lock(&pmic_efuse_lock_mutex);
  4172. /*1. enable efuse ctrl engine clock */
  4173. ret = pmic_config_interface(0x027C, 0x0040, 0xFFFF, 0);
  4174. ret = pmic_config_interface(0x0252, 0x0004, 0xFFFF, 0);
  4175. /*2. */
  4176. ret = pmic_config_interface(0x0C16, 0x1, 0x1, 0);
  4177. /*3. set row to read */
  4178. ret = pmic_config_interface(0x0C00, i, 0x1F, 1);
  4179. /*4. Toggle */
  4180. udelay(100);
  4181. ret = pmic_read_interface(0x0C10, &reg_val, 0x1, 0);
  4182. if (reg_val == 0)
  4183. ret = pmic_config_interface(0x0C10, 1, 0x1, 0);
  4184. else
  4185. ret = pmic_config_interface(0x0C10, 0, 0x1, 0);
  4186. /*5. polling Reg[0x61A] */
  4187. udelay(300);
  4188. reg_val = 1;
  4189. while (reg_val == 1) {
  4190. ret = pmic_read_interface(0x0C1A, &reg_val, 0x1, 0);
  4191. pr_debug("pmic_Read_Efuse_HPOffset polling 0x61A=0x%x\n", reg_val);
  4192. }
  4193. udelay(1000); /*Need to delay at least 1ms for 0x61A and than can read 0xC18 */
  4194. /*6. read data */
  4195. efusevalue = upmu_get_reg_value(0x0C18);
  4196. pr_debug("HPoffset : efuse=0x%x\n", efusevalue);
  4197. /*7. Disable efuse ctrl engine clock */
  4198. ret = pmic_config_interface(0x0250, 0x0004, 0xFFFF, 0);
  4199. ret = pmic_config_interface(0x027A, 0x0040, 0xFFFF, 0);
  4200. mutex_unlock(&pmic_efuse_lock_mutex);
  4201. return efusevalue;
  4202. }
  4203. /*****************************************************************************
  4204. * PMIC mudule init/exit
  4205. ******************************************************************************/
  4206. static int __init pmic_mt_init(void)
  4207. {
  4208. int ret;
  4209. #ifdef BATTERY_PERCENT_PROTECT
  4210. #if !defined CONFIG_HAS_WAKELOCKS
  4211. wakeup_source_init(&pmicThread_lock, "pmicThread_lock_mt6328 wakelock");
  4212. wakeup_source_init(&bat_percent_notify_lock, "bat_percent_notify_lock wakelock");
  4213. #else
  4214. wake_lock_init(&pmicThread_lock, WAKE_LOCK_SUSPEND, "pmicThread_lock_mt6328 wakelock");
  4215. wake_lock_init(&bat_percent_notify_lock, WAKE_LOCK_SUSPEND,
  4216. "bat_percent_notify_lock wakelock");
  4217. #endif
  4218. #endif /* #ifdef BATTERY_PERCENT_PROTECT */
  4219. #ifdef DLPT_FEATURE_SUPPORT
  4220. #if !defined CONFIG_HAS_WAKELOCKS
  4221. wakeup_source_init(&dlpt_notify_lock, "dlpt_notify_lock wakelock");
  4222. #else
  4223. wake_lock_init(&dlpt_notify_lock, WAKE_LOCK_SUSPEND, "dlpt_notify_lock wakelock");
  4224. #endif
  4225. #endif /*#ifdef DLPT_FEATURE_SUPPORT */
  4226. #if !defined CONFIG_MTK_LEGACY
  4227. #ifdef CONFIG_OF
  4228. PMICLOG("pmic_regulator_init_OF\n");
  4229. /* PMIC device driver register */
  4230. ret = platform_device_register(&pmic_mt_device);
  4231. if (ret) {
  4232. PMICLOG("****[pmic_mt_init] Unable to device register(%d)\n", ret);
  4233. return ret;
  4234. }
  4235. ret = platform_driver_register(&pmic_mt_driver);
  4236. if (ret) {
  4237. PMICLOG("****[pmic_mt_init] Unable to register driver (%d)\n", ret);
  4238. return ret;
  4239. }
  4240. ret = platform_driver_register(&mt_pmic_driver);
  4241. if (ret) {
  4242. PMICLOG("****[pmic_mt_init] Unable to register driver by DT(%d)\n", ret);
  4243. return ret;
  4244. }
  4245. #endif /* End of #ifdef CONFIG_OF */
  4246. #else
  4247. PMICLOG("pmic_regulator_init\n");
  4248. /* PMIC device driver register */
  4249. ret = platform_device_register(&pmic_mt_device);
  4250. if (ret) {
  4251. PMICLOG("****[pmic_mt_init] Unable to device register(%d)\n", ret);
  4252. return ret;
  4253. }
  4254. ret = platform_driver_register(&pmic_mt_driver);
  4255. if (ret) {
  4256. PMICLOG("****[pmic_mt_init] Unable to register driver (%d)\n", ret);
  4257. return ret;
  4258. }
  4259. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  4260. pmic_auxadc_init();
  4261. pr_debug("****[pmic_mt_init] Initialization : DONE !!\n");
  4262. return 0;
  4263. }
  4264. static void __exit pmic_mt_exit(void)
  4265. {
  4266. #if !defined CONFIG_MTK_LEGACY
  4267. #ifdef CONFIG_OF
  4268. platform_driver_unregister(&mt_pmic_driver);
  4269. #endif
  4270. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  4271. }
  4272. fs_initcall(pmic_mt_init);
  4273. /*module_init(pmic_mt_init);*/
  4274. module_exit(pmic_mt_exit);
  4275. MODULE_AUTHOR("Argus Lin");
  4276. MODULE_DESCRIPTION("MT PMIC Device Driver");
  4277. MODULE_LICENSE("GPL");