pmic.c 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182
  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_internal(bool isSuspend, unsigned int *bat, signed int *cur)
  1906. {
  1907. unsigned int vbat_reg;
  1908. int ret = 0;
  1909. count_adc_imp = 0;
  1910. /*PMICLOG("[do_ptim] start\n"); */
  1911. /*pmic_set_register_value(PMIC_RG_AUXADC_RST,1); */
  1912. /*pmic_set_register_value(PMIC_RG_AUXADC_RST,0); */
  1913. upmu_set_reg_value(0x0eac, 0x0006);
  1914. pmic_set_register_value(PMIC_AUXADC_IMP_AUTORPT_PRD, 6);
  1915. pmic_set_register_value(PMIC_RG_AUXADC_SMPS_CK_PDN, 0);
  1916. pmic_set_register_value(PMIC_RG_AUXADC_SMPS_CK_PDN_HWEN, 0);
  1917. pmic_set_register_value(PMIC_RG_AUXADC_CK_PDN_HWEN, 0);
  1918. pmic_set_register_value(PMIC_RG_AUXADC_CK_PDN, 0);
  1919. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 1);
  1920. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 1);
  1921. /*restore to initial state */
  1922. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 0);
  1923. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 0);
  1924. /*set issue interrupt */
  1925. /*pmic_set_register_value(PMIC_RG_INT_EN_AUXADC_IMP,1); */
  1926. #if defined(SWCHR_POWER_PATH)
  1927. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_CHSEL, 1);
  1928. #else
  1929. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_CHSEL, 0);
  1930. #endif
  1931. pmic_set_register_value(PMIC_AUXADC_IMP_AUTORPT_EN, 1);
  1932. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_CNT, 3);
  1933. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_MODE, 1);
  1934. /* PMICLOG("[do_ptim] end %d %d\n",pmic_get_register_value
  1935. (PMIC_RG_AUXADC_SMPS_CK_PDN),pmic_get_register_value(
  1936. PMIC_RG_AUXADC_SMPS_CK_PDN_HWEN));*/
  1937. while (pmic_get_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_STATUS) == 0) {
  1938. /*PMICLOG("[do_ptim] PMIC_AUXADC_IMPEDANCE_IRQ_STATUS= %d\n",
  1939. pmic_get_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_STATUS)); */
  1940. if ((count_adc_imp++) > count_time_out_adc_imp) {
  1941. pr_err("do_ptim over %d times/ms\n", count_adc_imp);
  1942. ret = 1;
  1943. break;
  1944. }
  1945. mdelay(1);
  1946. }
  1947. /*disable */
  1948. pmic_set_register_value(PMIC_AUXADC_IMP_AUTORPT_EN, 0); /*typo */
  1949. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_MODE, 0);
  1950. /*clear irq */
  1951. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 1);
  1952. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 1);
  1953. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 0);
  1954. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 0);
  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. *bat = (vbat_reg * 3 * 18000) / 32768;
  1962. #if defined(CONFIG_MTK_SMART_BATTERY)
  1963. fgauge_read_IM_current((void *)cur);
  1964. #else
  1965. *cur = 0;
  1966. #endif
  1967. pr_err("do_ptim_internal : bat %d cur %d\n", *bat, *cur);
  1968. #if defined(SWCHR_POWER_PATH)
  1969. pr_err("do_ptim_internal test: bat %d cur %d\n", *bat, *cur);
  1970. #endif
  1971. return ret;
  1972. }
  1973. int do_ptim(bool isSuspend)
  1974. {
  1975. int ret;
  1976. if (isSuspend == false)
  1977. pmic_auxadc_lock();
  1978. ret = do_ptim_internal(isSuspend, &ptim_bat_vol, &ptim_R_curr);
  1979. if (isSuspend == false)
  1980. pmic_auxadc_unlock();
  1981. return ret;
  1982. }
  1983. int do_ptim_ex(bool isSuspend, unsigned int *bat, signed int *cur)
  1984. {
  1985. int ret;
  1986. if (isSuspend == false)
  1987. pmic_auxadc_lock();
  1988. ret = do_ptim_internal(isSuspend, bat, cur);
  1989. if (isSuspend == false)
  1990. pmic_auxadc_unlock();
  1991. return ret;
  1992. }
  1993. void get_ptim_value(unsigned int *bat, signed int *cur)
  1994. {
  1995. pmic_auxadc_lock();
  1996. *bat = ptim_bat_vol;
  1997. *cur = ptim_R_curr;
  1998. pmic_auxadc_unlock();
  1999. }
  2000. void enable_dummy_load(unsigned int en)
  2001. {
  2002. if (en == 1) {
  2003. /*Enable dummy load-------------------------------------------------- */
  2004. /*mt6325_upmu_set_rg_g_drv_2m_ck_pdn(0); */
  2005. /*mt6325_upmu_set_rg_drv_32k_ck_pdn(0); */
  2006. /*upmu_set_reg_value(0x23c,0xfeb0); */
  2007. pmic_set_register_value(PMIC_RG_DRV_ISINK2_CK_PDN, 0);
  2008. pmic_set_register_value(PMIC_RG_DRV_ISINK3_CK_PDN, 0);
  2009. /*upmu_set_reg_value(0x25a,0x8a00); */
  2010. pmic_set_register_value(PMIC_RG_DRV_ISINK2_CK_CKSEL, 0);
  2011. pmic_set_register_value(PMIC_RG_DRV_ISINK3_CK_CKSEL, 0);
  2012. /*upmu_set_reg_value(0x82a,0x0c00); */
  2013. /*upmu_set_reg_value(0x81c,0x7000); */
  2014. pmic_set_register_value(PMIC_ISINK_CH2_STEP, 0xc);
  2015. /*upmu_set_reg_value(0x81e,0x7000); */
  2016. pmic_set_register_value(PMIC_ISINK_CH3_STEP, 0xc);
  2017. /*upmu_set_reg_value(0x820,0x0300); */
  2018. pmic_set_register_value(PMIC_RG_ISINK2_DOUBLE_EN, 1);
  2019. pmic_set_register_value(PMIC_RG_ISINK3_DOUBLE_EN, 1);
  2020. /*upmu_set_reg_value(0x828,0x0ccc); */
  2021. pmic_set_register_value(PMIC_ISINK_CH2_EN, 1);
  2022. pmic_set_register_value(PMIC_ISINK_CH3_EN, 1);
  2023. pmic_set_register_value(PMIC_ISINK_CHOP2_EN, 1);
  2024. pmic_set_register_value(PMIC_ISINK_CHOP3_EN, 1);
  2025. pmic_set_register_value(PMIC_ISINK_CH2_BIAS_EN, 1);
  2026. pmic_set_register_value(PMIC_ISINK_CH3_BIAS_EN, 1);
  2027. /*pmic_set_register_value(PMIC_RG_VIBR_EN,1); */
  2028. /*PMICLOG("[enable dummy load]\n"); */
  2029. } else {
  2030. /*upmu_set_reg_value(0x828,0x0cc0); */
  2031. pmic_set_register_value(PMIC_ISINK_CH2_EN, 0);
  2032. pmic_set_register_value(PMIC_ISINK_CH3_EN, 0);
  2033. /*pmic_set_register_value(PMIC_RG_VIBR_EN,0); */
  2034. /*PMICLOG("[disable dummy load]\n"); */
  2035. }
  2036. }
  2037. #endif /* #ifdef DLPT_FEATURE_SUPPORT */
  2038. #ifdef DLPT_FEATURE_SUPPORT
  2039. static struct hrtimer dlpt_notify_timer;
  2040. static struct task_struct *dlpt_notify_thread;
  2041. static bool dlpt_notify_flag;
  2042. static DECLARE_WAIT_QUEUE_HEAD(dlpt_notify_waiter);
  2043. #endif
  2044. #if !defined CONFIG_HAS_WAKELOCKS
  2045. struct wakeup_source dlpt_notify_lock;
  2046. #else
  2047. struct wake_lock dlpt_notify_lock;
  2048. #endif
  2049. static DEFINE_MUTEX(dlpt_notify_mutex);
  2050. #ifdef DLPT_FEATURE_SUPPORT
  2051. #define DLPT_NUM 16
  2052. int g_dlpt_stop = 0;
  2053. unsigned int g_dlpt_val = 0;
  2054. int g_dlpt_start = 0;
  2055. int g_imix_val = 0;
  2056. int g_imix_val_pre = 0;
  2057. int g_low_per_timer = 0;
  2058. int g_low_per_timeout_val = 60;
  2059. int g_lbatInt1 = POWER_INT2_VOLT * 10;
  2060. struct dlpt_callback_table {
  2061. void *dlpt_cb;
  2062. };
  2063. struct dlpt_callback_table dlpt_cb_tb[] = {
  2064. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL},
  2065. {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}
  2066. };
  2067. void (*dlpt_callback)(unsigned int);
  2068. void register_dlpt_notify(void (*dlpt_callback) (unsigned int), DLPT_PRIO prio_val)
  2069. {
  2070. PMICLOG("[register_dlpt_notify] start\n");
  2071. dlpt_cb_tb[prio_val].dlpt_cb = dlpt_callback;
  2072. pr_err("[register_dlpt_notify] prio_val=%d\n", prio_val);
  2073. if ((g_dlpt_stop == 0) && (g_dlpt_val != 0)) {
  2074. pr_err("[register_dlpt_notify] dlpt happen\n");
  2075. dlpt_callback(g_dlpt_val);
  2076. }
  2077. }
  2078. void exec_dlpt_callback(unsigned int dlpt_val)
  2079. {
  2080. int i = 0;
  2081. g_dlpt_val = dlpt_val;
  2082. if (g_dlpt_stop == 1) {
  2083. pr_err("[exec_dlpt_callback] g_dlpt_stop=%d\n", g_dlpt_stop);
  2084. } else {
  2085. for (i = 0; i < DLPT_NUM; i++) {
  2086. if (dlpt_cb_tb[i].dlpt_cb != NULL) {
  2087. dlpt_callback = dlpt_cb_tb[i].dlpt_cb;
  2088. dlpt_callback(g_dlpt_val);
  2089. pr_debug("[exec_dlpt_callback] g_dlpt_val=%d\n", g_dlpt_val);
  2090. }
  2091. }
  2092. }
  2093. }
  2094. /*
  2095. int get_dlpt_iavg(int is_use_zcv)
  2096. {
  2097. int bat_cap_val = 0;
  2098. int zcv_val = 0;
  2099. int vsys_min_2_val = POWER_INT2_VOLT;
  2100. int rbat_val = 0;
  2101. int rdc_val = 0;
  2102. int iavg_val = 0;
  2103. bat_cap_val = bat_get_ui_percentage();
  2104. if(is_use_zcv == 1)
  2105. zcv_val = fgauge_read_v_by_d(100-bat_cap_val);
  2106. else
  2107. {
  2108. #if defined(SWCHR_POWER_PATH)
  2109. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_ISENSE_AP,5,1);
  2110. #else
  2111. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_BATSNS_AP,5,1);
  2112. #endif
  2113. }
  2114. rbat_val = fgauge_read_r_bat_by_v(zcv_val);
  2115. rdc_val = CUST_R_FG_OFFSET+R_FG_VALUE+rbat_val;
  2116. if(rdc_val==0)
  2117. rdc_val=1;
  2118. iavg_val = ((zcv_val-vsys_min_2_val)*1000)/rdc_val;
  2119. return iavg_val;
  2120. }
  2121. int get_real_volt(int val)*//*0.1mV */
  2122. /*
  2123. {
  2124. int ret = 0;
  2125. ret = val&0x7FFF;
  2126. ret = (ret*4*1800*10)/32768;
  2127. return ret;
  2128. }
  2129. int get_real_curr(int val)*//*0.1mA */
  2130. /*
  2131. {
  2132. int ret=0;
  2133. if ( val > 32767 ) {
  2134. ret = val-65535;
  2135. ret = ret-(ret*2);
  2136. } else
  2137. ret = val;
  2138. ret = ret*158122;
  2139. do_div(ret, 100000);
  2140. ret = (ret*20)/R_FG_VALUE;
  2141. ret = ((ret*CAR_TUNE_VALUE)/100);
  2142. return ret;
  2143. }
  2144. */
  2145. int get_rac_val(void)
  2146. {
  2147. int volt_1 = 0;
  2148. int volt_2 = 0;
  2149. int curr_1 = 0;
  2150. int curr_2 = 0;
  2151. int rac_cal = 0;
  2152. int ret = 0;
  2153. bool retry_state = false;
  2154. int retry_count = 0;
  2155. do {
  2156. /*adc and fg-------------------------------------------------------- */
  2157. do_ptim(KAL_TRUE);
  2158. pmic_spm_crit2("[1,Trigger ADC PTIM mode] volt1=%d, curr_1=%d\n", ptim_bat_vol,
  2159. ptim_R_curr);
  2160. volt_1 = ptim_bat_vol;
  2161. curr_1 = ptim_R_curr;
  2162. pmic_spm_crit2("[2,enable dummy load]");
  2163. enable_dummy_load(1);
  2164. mdelay(50);
  2165. /*Wait -------------------------------------------------------------- */
  2166. /*adc and fg-------------------------------------------------------- */
  2167. do_ptim(KAL_TRUE);
  2168. pmic_spm_crit2("[3,Trigger ADC PTIM mode again] volt2=%d, curr_2=%d\n",
  2169. ptim_bat_vol, ptim_R_curr);
  2170. volt_2 = ptim_bat_vol;
  2171. curr_2 = ptim_R_curr;
  2172. /*Disable dummy load------------------------------------------------- */
  2173. enable_dummy_load(0);
  2174. /*Calculate Rac------------------------------------------------------ */
  2175. if ((curr_2 - curr_1) >= 700 && (curr_2 - curr_1) <= 1200
  2176. && (volt_1 - volt_2) >= 80) {
  2177. /*40.0mA */
  2178. rac_cal = ((volt_1 - volt_2) * 1000) / (curr_2 - curr_1); /*m-ohm */
  2179. if (rac_cal < 0)
  2180. ret = (rac_cal - (rac_cal * 2)) * 1;
  2181. else
  2182. ret = rac_cal * 1;
  2183. } else if ((curr_1 - curr_2) >= 700 &&
  2184. ((curr_1 - curr_2) <= 1200) && ((volt_2 - volt_1) >= 80)) {
  2185. /*40.0mA */
  2186. rac_cal = ((volt_2 - volt_1) * 1000) / (curr_1 - curr_2); /*m-ohm */
  2187. if (rac_cal < 0)
  2188. ret = (rac_cal - (rac_cal * 2)) * 1;
  2189. else
  2190. ret = rac_cal * 1;
  2191. } else {
  2192. ret = -1;
  2193. pmic_spm_crit2("[4,Calculate Rac] bypass due to (curr_x-curr_y) < 40mA\n");
  2194. }
  2195. pmic_spm_crit2
  2196. ("volt_1 = %d,volt_2 = %d,curr_1 = %d,curr_2 = %d,rac_cal = %d,ret = %d,retry_count = %d\n",
  2197. volt_1, volt_2, curr_1, curr_2, rac_cal, ret, retry_count);
  2198. pmic_spm_crit2(" %d,%d,%d,%d,%d,%d,%d\n",
  2199. volt_1, volt_2, curr_1, curr_2, rac_cal, ret, retry_count);
  2200. /*------------------------*/
  2201. retry_count++;
  2202. if ((retry_count < 3) && (ret == -1))
  2203. retry_state = true;
  2204. else
  2205. retry_state = false;
  2206. } while (retry_state == true);
  2207. return ret;
  2208. }
  2209. int get_dlpt_imix_spm(void)
  2210. {
  2211. int rac_val[5], rac_val_avg;
  2212. /* int volt[5], curr[5], volt_avg = 0, curr_avg = 0; */
  2213. /* int imix; */
  2214. int i;
  2215. static unsigned int pre_ui_soc = 101;
  2216. unsigned int ui_soc;
  2217. ui_soc = bat_get_ui_percentage();
  2218. if (ui_soc != pre_ui_soc) {
  2219. pre_ui_soc = ui_soc;
  2220. } else {
  2221. pmic_spm_crit2("[dlpt_R] pre_SOC=%d SOC=%d skip\n", pre_ui_soc, ui_soc);
  2222. return 0;
  2223. }
  2224. for (i = 0; i < 2; i++) {
  2225. rac_val[i] = get_rac_val();
  2226. if (rac_val[i] == -1)
  2227. return -1;
  2228. }
  2229. /*rac_val_avg=rac_val[0]+rac_val[1]+rac_val[2]+rac_val[3]+rac_val[4]; */
  2230. /*rac_val_avg=rac_val_avg/5; */
  2231. /*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); */
  2232. rac_val_avg = rac_val[0] + rac_val[1];
  2233. rac_val_avg = rac_val_avg / 2;
  2234. pmic_spm_crit2("[dlpt_R] %d,%d,%d\n", rac_val[0], rac_val[1], rac_val_avg);
  2235. if (rac_val_avg > 100)
  2236. ptim_rac_val_avg = rac_val_avg;
  2237. /*
  2238. for(i=0;i<5;i++)
  2239. {
  2240. do_ptim();
  2241. volt[i]=ptim_bat_vol;
  2242. curr[i]=ptim_R_curr;
  2243. volt_avg+=ptim_bat_vol;
  2244. curr_avg+=ptim_R_curr;
  2245. }
  2246. volt_avg=volt_avg/5;
  2247. curr_avg=curr_avg/5;
  2248. imix=curr_avg+(volt_avg-g_lbatInt1)*1000/ptim_rac_val_avg;
  2249. pmic_spm_crit2("[dlpt_Imix] %d,%d,%d,%d,%d,%d,%d\n",volt_avg,curr_avg,g_lbatInt1,
  2250. ptim_rac_val_avg,imix,BMT_status.SOC,bat_get_ui_percentage());
  2251. ptim_imix=imix;
  2252. */
  2253. return 0;
  2254. }
  2255. int get_dlpt_imix(void)
  2256. {
  2257. /* int rac_val[5], rac_val_avg; */
  2258. int volt[5], curr[5], volt_avg = 0, curr_avg = 0;
  2259. int imix;
  2260. int i, count_do_ptim = 0;
  2261. for (i = 0; i < 5; i++) {
  2262. /*adc and fg-------------------------------------------------------- */
  2263. /*do_ptim(KAL_FALSE);*/
  2264. while (do_ptim(KAL_FALSE)) {
  2265. if ((count_do_ptim >= 2) && (count_do_ptim < 4))
  2266. pr_err("do_ptim more than twice times\n");
  2267. else if (count_do_ptim > 3) {
  2268. pr_err("do_ptim more than five times\n");
  2269. BUG_ON(1);
  2270. } else
  2271. ;
  2272. count_do_ptim++;
  2273. }
  2274. volt[i] = ptim_bat_vol;
  2275. curr[i] = ptim_R_curr;
  2276. volt_avg += ptim_bat_vol;
  2277. curr_avg += ptim_R_curr;
  2278. }
  2279. volt_avg = volt_avg / 5;
  2280. curr_avg = curr_avg / 5;
  2281. imix = (curr_avg + (volt_avg - g_lbatInt1) * 1000 / ptim_rac_val_avg) / 10;
  2282. pr_debug("[get_dlpt_imix] %d,%d,%d,%d,%d,%d,%d\n", volt_avg, curr_avg, g_lbatInt1,
  2283. ptim_rac_val_avg, imix, BMT_status.SOC, bat_get_ui_percentage());
  2284. ptim_imix = imix;
  2285. return ptim_imix;
  2286. }
  2287. int get_dlpt_imix_charging(void)
  2288. {
  2289. int zcv_val = 0;
  2290. int vsys_min_1_val = POWER_INT2_VOLT;
  2291. int imix_val = 0;
  2292. #if defined(SWCHR_POWER_PATH)
  2293. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_ISENSE_AP, 5, 1);
  2294. #else
  2295. zcv_val = PMIC_IMM_GetOneChannelValue(MT6328_AUX_BATSNS_AP, 5, 1);
  2296. #endif
  2297. imix_val = (zcv_val - vsys_min_1_val) * 1000 / ptim_rac_val_avg * 9 / 10;
  2298. pr_debug("[dlpt] get_dlpt_imix_charging %d %d %d %d\n",
  2299. imix_val, zcv_val, vsys_min_1_val, ptim_rac_val_avg);
  2300. return imix_val;
  2301. }
  2302. int dlpt_check_power_off(void)
  2303. {
  2304. int ret = 0;
  2305. ret = 0;
  2306. if (g_dlpt_start == 0) {
  2307. PMICLOG("[dlpt_check_power_off] not start\n");
  2308. } else {
  2309. if (g_low_battery_level == 2 && g_lowbat_int_bottom == 1)
  2310. ret = 1;
  2311. else
  2312. ret = 0;
  2313. PMICLOG("[dlpt_check_power_off]");
  2314. PMICLOG("ptim_imix=%d, POWEROFF_BAT_CURRENT=%d", ptim_imix, POWEROFF_BAT_CURRENT);
  2315. PMICLOG(" g_low_battery_level=%d,ret=%d,g_lowbat_int_bottom=%d\n",
  2316. g_low_battery_level, ret, g_lowbat_int_bottom);
  2317. }
  2318. return ret;
  2319. }
  2320. int dlpt_notify_handler(void *unused)
  2321. {
  2322. ktime_t ktime;
  2323. int pre_ui_soc = 0;
  2324. int cur_ui_soc = 0;
  2325. /* int diff_ui_soc = 1; */
  2326. pre_ui_soc = bat_get_ui_percentage();
  2327. cur_ui_soc = pre_ui_soc;
  2328. do {
  2329. ktime = ktime_set(10, 0);
  2330. wait_event_interruptible(dlpt_notify_waiter, (dlpt_notify_flag == true));
  2331. #if !defined CONFIG_HAS_WAKELOCKS
  2332. __pm_stay_awake(&dlpt_notify_lock);
  2333. #else
  2334. wake_lock(&dlpt_notify_lock);
  2335. #endif
  2336. mutex_lock(&dlpt_notify_mutex);
  2337. /*---------------------------------*/
  2338. cur_ui_soc = bat_get_ui_percentage();
  2339. if (cur_ui_soc <= 1) {
  2340. g_low_per_timer += 10;
  2341. if (g_low_per_timer > g_low_per_timeout_val)
  2342. g_low_per_timer = 0;
  2343. PMICLOG("[DLPT] g_low_per_timer=%d,g_low_per_timeout_val=%d\n",
  2344. g_low_per_timer, g_low_per_timeout_val);
  2345. } else {
  2346. g_low_per_timer = 0;
  2347. }
  2348. PMICLOG("[dlpt_notify_handler] %d %d %d %d %d\n", pre_ui_soc, cur_ui_soc,
  2349. g_imix_val, g_low_per_timer, g_low_per_timeout_val);
  2350. /*
  2351. if( ((pre_ui_soc-cur_ui_soc)>=diff_ui_soc) ||
  2352. ((cur_ui_soc-pre_ui_soc)>=diff_ui_soc) ||
  2353. (g_imix_val == 0) ||
  2354. (g_imix_val == -1) ||
  2355. ( (cur_ui_soc <= 1) && (g_low_per_timer >= g_low_per_timeout_val) )
  2356. )
  2357. */
  2358. {
  2359. PMICLOG("[DLPT] is running\n");
  2360. if (ptim_rac_val_avg == 0)
  2361. PMICLOG("[DLPT] ptim_rac_val_avg=0 , skip\n");
  2362. else {
  2363. if (upmu_get_rgs_chrdet()) {
  2364. g_imix_val = get_dlpt_imix_charging();
  2365. } else {
  2366. g_imix_val = get_dlpt_imix();
  2367. /*
  2368. if(g_imix_val != -1) {
  2369. if(g_imix_val_pre <= 0)
  2370. g_imix_val_pre=g_imix_val;
  2371. if(g_imix_val > g_imix_val_pre) {
  2372. PMICLOG("[DLPT] g_imix_val=%d,g_imix_val_pre=%d\n", g_imix_val, g_imix_val_pre);
  2373. g_imix_val=g_imix_val_pre;
  2374. }
  2375. else
  2376. g_imix_val_pre=g_imix_val;
  2377. }
  2378. */
  2379. }
  2380. /*Notify */
  2381. if (g_imix_val >= 1) {
  2382. if (g_imix_val > IMAX_MAX_VALUE)
  2383. g_imix_val = IMAX_MAX_VALUE;
  2384. exec_dlpt_callback(g_imix_val);
  2385. } else {
  2386. exec_dlpt_callback(1);
  2387. PMICLOG("[DLPT] return 1\n");
  2388. }
  2389. pre_ui_soc = cur_ui_soc;
  2390. #if 0
  2391. pr_debug("[DLPT_final] %d,%d,%d,%d,%d,%d\n",
  2392. g_imix_val, g_imix_val_pre, pre_ui_soc, cur_ui_soc,
  2393. diff_ui_soc, IMAX_MAX_VALUE);
  2394. #else
  2395. pr_debug("[DLPT_final] %d,%d,%d,%d,%d\n",
  2396. g_imix_val, g_imix_val_pre, pre_ui_soc, cur_ui_soc,
  2397. IMAX_MAX_VALUE);
  2398. #endif
  2399. }
  2400. }
  2401. g_dlpt_start = 1;
  2402. dlpt_notify_flag = false;
  2403. /*---------------------------------*/
  2404. mutex_unlock(&dlpt_notify_mutex);
  2405. #if !defined CONFIG_HAS_WAKELOCKS
  2406. __pm_relax(&dlpt_notify_lock);
  2407. #else
  2408. wake_unlock(&dlpt_notify_lock);
  2409. #endif
  2410. hrtimer_start(&dlpt_notify_timer, ktime, HRTIMER_MODE_REL);
  2411. } while (!kthread_should_stop());
  2412. return 0;
  2413. }
  2414. enum hrtimer_restart dlpt_notify_task(struct hrtimer *timer)
  2415. {
  2416. dlpt_notify_flag = true;
  2417. wake_up_interruptible(&dlpt_notify_waiter);
  2418. PMICLOG("dlpt_notify_task is called\n");
  2419. return HRTIMER_NORESTART;
  2420. }
  2421. int get_system_loading_ma(void)
  2422. {
  2423. int fg_val = 0;
  2424. if (g_dlpt_start == 0)
  2425. PMICLOG("get_system_loading_ma not ready\n");
  2426. else {
  2427. fg_val = battery_meter_get_battery_current();
  2428. fg_val = fg_val / 10;
  2429. if (battery_meter_get_battery_current_sign() == 1)
  2430. fg_val = 0 - fg_val; /* charging */
  2431. PMICLOG("[get_system_loading_ma] fg_val=%d\n", fg_val);
  2432. }
  2433. return fg_val;
  2434. }
  2435. void dlpt_notify_init(void)
  2436. {
  2437. ktime_t ktime;
  2438. ktime = ktime_set(30, 0);
  2439. hrtimer_init(&dlpt_notify_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2440. dlpt_notify_timer.function = dlpt_notify_task;
  2441. hrtimer_start(&dlpt_notify_timer, ktime, HRTIMER_MODE_REL);
  2442. dlpt_notify_thread = kthread_run(dlpt_notify_handler, 0, "dlpt_notify_thread");
  2443. if (IS_ERR(dlpt_notify_thread))
  2444. pr_err("Failed to create dlpt_notify_thread\n");
  2445. else
  2446. pr_err("Create dlpt_notify_thread : done\n");
  2447. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 0);
  2448. /*re-init UVLO volt */
  2449. switch (POWER_UVLO_VOLT_LEVEL) {
  2450. case 2500:
  2451. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 0);
  2452. break;
  2453. case 2550:
  2454. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 1);
  2455. break;
  2456. case 2600:
  2457. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 2);
  2458. break;
  2459. case 2650:
  2460. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 3);
  2461. break;
  2462. case 2700:
  2463. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 4);
  2464. break;
  2465. case 2750:
  2466. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 5);
  2467. break;
  2468. case 2800:
  2469. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 6);
  2470. break;
  2471. case 2850:
  2472. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 7);
  2473. break;
  2474. case 2900:
  2475. pmic_set_register_value(PMIC_RG_UVLO_VTHL, 8);
  2476. break;
  2477. default:
  2478. PMICLOG("Invalid value(%d)\n", POWER_UVLO_VOLT_LEVEL);
  2479. break;
  2480. }
  2481. pr_err("POWER_UVLO_VOLT_LEVEL=%d, [0x%x]=0x%x\n",
  2482. POWER_UVLO_VOLT_LEVEL, MT6328_CHR_CON17, upmu_get_reg_value(MT6328_CHR_CON17));
  2483. }
  2484. #else
  2485. int get_dlpt_imix_spm(void)
  2486. {
  2487. return 1;
  2488. }
  2489. #endif /*#ifdef DLPT_FEATURE_SUPPORT */
  2490. /*****************************************************************************
  2491. * interrupt Setting
  2492. ******************************************************************************/
  2493. static struct pmic_interrupt_bit interrupt_status0[] = {
  2494. PMIC_S_INT_GEN(RG_INT_STATUS_PWRKEY),
  2495. PMIC_S_INT_GEN(RG_INT_STATUS_HOMEKEY),
  2496. PMIC_S_INT_GEN(RG_INT_STATUS_PWRKEY_R),
  2497. PMIC_S_INT_GEN(RG_INT_STATUS_HOMEKEY_R),
  2498. PMIC_S_INT_GEN(RG_INT_STATUS_THR_H),
  2499. PMIC_S_INT_GEN(RG_INT_STATUS_THR_L),
  2500. PMIC_S_INT_GEN(RG_INT_STATUS_BAT_H),
  2501. PMIC_S_INT_GEN(RG_INT_STATUS_BAT_L),
  2502. PMIC_S_INT_GEN(NO_USE),
  2503. PMIC_S_INT_GEN(RG_INT_STATUS_RTC),
  2504. PMIC_S_INT_GEN(RG_INT_STATUS_AUDIO),
  2505. PMIC_S_INT_GEN(NO_USE),
  2506. PMIC_S_INT_GEN(RG_INT_STATUS_ACCDET),
  2507. PMIC_S_INT_GEN(RG_INT_STATUS_ACCDET_EINT),
  2508. PMIC_S_INT_GEN(RG_INT_STATUS_ACCDET_NEGV),
  2509. PMIC_S_INT_GEN(RG_INT_STATUS_NI_LBAT_INT),
  2510. };
  2511. static struct pmic_interrupt_bit interrupt_status1[] = {
  2512. PMIC_S_INT_GEN(RG_INT_STATUS_VPROC_OC),
  2513. PMIC_S_INT_GEN(RG_INT_STATUS_VSYS_OC),
  2514. PMIC_S_INT_GEN(RG_INT_STATUS_VLTE_OC),
  2515. PMIC_S_INT_GEN(NO_USE),
  2516. PMIC_S_INT_GEN(NO_USE),
  2517. PMIC_S_INT_GEN(RG_INT_STATUS_VCORE_OC),
  2518. PMIC_S_INT_GEN(NO_USE),
  2519. PMIC_S_INT_GEN(NO_USE),
  2520. PMIC_S_INT_GEN(RG_INT_STATUS_VPA_OC),
  2521. PMIC_S_INT_GEN(RG_INT_STATUS_LDO_OC),
  2522. PMIC_S_INT_GEN(RG_INT_STATUS_BAT2_H),
  2523. PMIC_S_INT_GEN(RG_INT_STATUS_BAT2_L),
  2524. PMIC_S_INT_GEN(RG_INT_STATUS_VISMPS0_H),
  2525. PMIC_S_INT_GEN(RG_INT_STATUS_VISMPS0_L),
  2526. PMIC_S_INT_GEN(RG_INT_STATUS_AUXADC_IMP),
  2527. PMIC_S_INT_GEN(NO_USE),
  2528. };
  2529. static struct pmic_interrupt_bit interrupt_status2[] = {
  2530. PMIC_S_INT_GEN(RG_INT_STATUS_OV),
  2531. PMIC_S_INT_GEN(RG_INT_STATUS_BVALID_DET),
  2532. PMIC_S_INT_GEN(RG_INT_STATUS_VBATON_HV),
  2533. PMIC_S_INT_GEN(RG_INT_STATUS_VBATON_UNDET),
  2534. PMIC_S_INT_GEN(RG_INT_STATUS_WATCHDOG),
  2535. PMIC_S_INT_GEN(RG_INT_STATUS_PCHR_CM_VDEC),
  2536. PMIC_S_INT_GEN(RG_INT_STATUS_CHRDET),
  2537. PMIC_S_INT_GEN(RG_INT_STATUS_PCHR_CM_VINC),
  2538. PMIC_S_INT_GEN(RG_INT_STATUS_FG_BAT_H),
  2539. PMIC_S_INT_GEN(RG_INT_STATUS_FG_BAT_L),
  2540. PMIC_S_INT_GEN(RG_INT_STATUS_FG_CUR_H),
  2541. PMIC_S_INT_GEN(RG_INT_STATUS_FG_CUR_L),
  2542. PMIC_S_INT_GEN(RG_INT_STATUS_FG_ZCV),
  2543. PMIC_S_INT_GEN(RG_INT_STATUS_SPKL_D),
  2544. PMIC_S_INT_GEN(RG_INT_STATUS_SPKL_AB),
  2545. PMIC_S_INT_GEN(NO_USE),
  2546. };
  2547. static struct pmic_interrupts interrupts[] = {
  2548. PMIC_M_INTS_GEN(MT6328_INT_STATUS0, MT6328_INT_CON0, MT6328_INT_CON0_SET,
  2549. MT6328_INT_CON0_CLR, interrupt_status0),
  2550. PMIC_M_INTS_GEN(MT6328_INT_STATUS1, MT6328_INT_CON1, MT6328_INT_CON1_SET,
  2551. MT6328_INT_CON1_CLR, interrupt_status1),
  2552. PMIC_M_INTS_GEN(MT6328_INT_STATUS2, MT6328_INT_CON2, MT6328_INT_CON2_SET,
  2553. MT6328_INT_CON2_CLR, interrupt_status2),
  2554. };
  2555. void pwrkey_int_handler(void)
  2556. {
  2557. PMICLOG("[pwrkey_int_handler] Press pwrkey %d\n", pmic_get_register_value(PMIC_PWRKEY_DEB));
  2558. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2559. if (get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT)
  2560. timer_pre = sched_clock();
  2561. #endif
  2562. #if defined(CONFIG_MTK_FPGA)
  2563. #else
  2564. kpd_pwrkey_pmic_handler(0x1);
  2565. #endif
  2566. }
  2567. void pwrkey_int_handler_r(void)
  2568. {
  2569. PMICLOG("[pwrkey_int_handler_r] Release pwrkey %d\n",
  2570. pmic_get_register_value(PMIC_PWRKEY_DEB));
  2571. #if defined(CONFIG_MTK_KERNEL_POWER_OFF_CHARGING)
  2572. if (get_boot_mode() == KERNEL_POWER_OFF_CHARGING_BOOT && timer_pre != 0) {
  2573. timer_pos = sched_clock();
  2574. if (timer_pos - timer_pre >= LONG_PWRKEY_PRESS_TIME)
  2575. long_pwrkey_press = true;
  2576. PMICLOG
  2577. ("timer_pos = %ld, timer_pre = %ld, timer_pos-timer_pre = %ld, long_pwrkey_press = %d\r\n",
  2578. timer_pos, timer_pre, timer_pos - timer_pre, long_pwrkey_press);
  2579. if (long_pwrkey_press) { /*500ms */
  2580. PMICLOG
  2581. ("Power Key Pressed during kernel power off charging, reboot OS\r\n");
  2582. kernel_restart("kernel power off charging reboot system");
  2583. /*arch_reset(0, NULL); */
  2584. }
  2585. }
  2586. #endif
  2587. #if defined(CONFIG_MTK_FPGA)
  2588. #else
  2589. kpd_pwrkey_pmic_handler(0x0);
  2590. #endif
  2591. }
  2592. /*****************************************************************************
  2593. * PMIC Interrupt callback
  2594. ******************************************************************************/
  2595. void homekey_int_handler(void)
  2596. {
  2597. PMICLOG("[homekey_int_handler] Press homekey %d\n",
  2598. pmic_get_register_value(PMIC_HOMEKEY_DEB));
  2599. #if defined(CONFIG_MTK_FPGA)
  2600. #else
  2601. kpd_pmic_rstkey_handler(0x1);
  2602. #endif
  2603. }
  2604. void homekey_int_handler_r(void)
  2605. {
  2606. PMICLOG("[homekey_int_handler_r] Release homekey %d\n",
  2607. pmic_get_register_value(PMIC_HOMEKEY_DEB));
  2608. #if defined(CONFIG_MTK_FPGA)
  2609. #else
  2610. kpd_pmic_rstkey_handler(0x0);
  2611. #endif
  2612. }
  2613. void chrdet_int_handler(void)
  2614. {
  2615. PMICLOG("[chrdet_int_handler]CHRDET status = %d....\n",
  2616. pmic_get_register_value(PMIC_RGS_CHRDET));
  2617. #ifdef CONFIG_MTK_KERNEL_POWER_OFF_CHARGING
  2618. if (!upmu_get_rgs_chrdet()) {
  2619. int boot_mode = 0;
  2620. boot_mode = get_boot_mode();
  2621. if (boot_mode == KERNEL_POWER_OFF_CHARGING_BOOT
  2622. || boot_mode == LOW_POWER_OFF_CHARGING_BOOT) {
  2623. PMICLOG("[chrdet_int_handler] Unplug Charger/USB\n");
  2624. mt_power_off();
  2625. }
  2626. }
  2627. #endif
  2628. pmic_set_register_value(PMIC_RG_USBDL_RST, 1);
  2629. #if defined(CONFIG_MTK_SMART_BATTERY)
  2630. do_chrdet_int_task();
  2631. #endif
  2632. }
  2633. void auxadc_imp_int_handler_r(void)
  2634. {
  2635. PMICLOG("auxadc_imp_int_handler_r() =%d\n",
  2636. pmic_get_register_value(PMIC_AUXADC_ADC_OUT_IMP));
  2637. /*clear IRQ */
  2638. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 1);
  2639. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 1);
  2640. /*restore to initial state */
  2641. pmic_set_register_value(PMIC_AUXADC_CLR_IMP_CNT_STOP, 0);
  2642. pmic_set_register_value(PMIC_AUXADC_IMPEDANCE_IRQ_CLR, 0);
  2643. /*turn off interrupt */
  2644. pmic_set_register_value(PMIC_RG_INT_EN_AUXADC_IMP, 0);
  2645. }
  2646. /*****************************************************************************
  2647. * PMIC Interrupt service
  2648. ******************************************************************************/
  2649. static DEFINE_MUTEX(pmic_mutex);
  2650. static struct task_struct *pmic_thread_handle;
  2651. #if !defined CONFIG_HAS_WAKELOCKS
  2652. struct wakeup_source pmicThread_lock;
  2653. #else
  2654. struct wake_lock pmicThread_lock;
  2655. #endif
  2656. void wake_up_pmic(void)
  2657. {
  2658. PMICLOG("[wake_up_pmic]\r\n");
  2659. if (pmic_thread_handle != NULL)
  2660. wake_up_process(pmic_thread_handle);
  2661. #if !defined CONFIG_HAS_WAKELOCKS
  2662. __pm_stay_awake(&pmicThread_lock);
  2663. #else
  2664. wake_lock(&pmicThread_lock);
  2665. #endif
  2666. }
  2667. EXPORT_SYMBOL(wake_up_pmic);
  2668. #if 0 /* def CONFIG_MTK_LEGACY */
  2669. void mt_pmic_eint_irq(void)
  2670. {
  2671. /*PMICLOG("[mt_pmic_eint_irq] receive interrupt\n"); */
  2672. wake_up_pmic();
  2673. /*return; */
  2674. }
  2675. #else
  2676. irqreturn_t mt_pmic_eint_irq(int irq, void *desc)
  2677. {
  2678. /*PMICLOG("[mt_pmic_eint_irq] receive interrupt\n"); */
  2679. disable_irq_nosync(irq);
  2680. wake_up_pmic();
  2681. return IRQ_HANDLED;
  2682. }
  2683. #endif
  2684. void pmic_enable_interrupt(unsigned int intNo, unsigned int en, char *str)
  2685. {
  2686. unsigned int shift, no;
  2687. shift = intNo / PMIC_INT_WIDTH;
  2688. no = intNo % PMIC_INT_WIDTH;
  2689. if (shift >= ARRAY_SIZE(interrupts)) {
  2690. PMICLOG("[pmic_enable_interrupt] fail intno=%d \r\n", intNo);
  2691. return;
  2692. }
  2693. PMICLOG("[pmic_enable_interrupt] intno=%d en=%d str=%s shf=%d no=%d [0x%x]=0x%x\r\n",
  2694. intNo, en, str, shift, no, interrupts[shift].en,
  2695. upmu_get_reg_value(interrupts[shift].en));
  2696. if (en == 1)
  2697. pmic_config_interface(interrupts[shift].set, 0x1, 0x1, no);
  2698. else if (en == 0)
  2699. pmic_config_interface(interrupts[shift].clear, 0x1, 0x1, no);
  2700. PMICLOG("[pmic_enable_interrupt] after [0x%x]=0x%x\r\n",
  2701. interrupts[shift].en, upmu_get_reg_value(interrupts[shift].en));
  2702. }
  2703. void pmic_register_interrupt_callback(unsigned int intNo, void (EINT_FUNC_PTR) (void))
  2704. {
  2705. unsigned int shift, no;
  2706. shift = intNo / PMIC_INT_WIDTH;
  2707. no = intNo % PMIC_INT_WIDTH;
  2708. if (shift >= ARRAY_SIZE(interrupts)) {
  2709. PMICLOG("[pmic_register_interrupt_callback] fail intno=%d\r\n", intNo);
  2710. return;
  2711. }
  2712. PMICLOG("[pmic_register_interrupt_callback] intno=%d\r\n", intNo);
  2713. interrupts[shift].interrupts[no].callback = EINT_FUNC_PTR;
  2714. }
  2715. void PMIC_EINT_SETTING(void)
  2716. {
  2717. struct device_node *node = NULL;
  2718. int ret = 0;
  2719. u32 ints[2] = { 0, 0 };
  2720. upmu_set_reg_value(MT6328_INT_CON0, 0);
  2721. upmu_set_reg_value(MT6328_INT_CON1, 0);
  2722. upmu_set_reg_value(MT6328_INT_CON2, 0);
  2723. /*enable pwrkey/homekey interrupt */
  2724. upmu_set_reg_value(MT6328_INT_CON0_SET, 0xf);
  2725. /*for all interrupt events, turn on interrupt module clock */
  2726. pmic_set_register_value(PMIC_RG_INTRP_CK_PDN, 0);
  2727. /*For BUCK OC related interrupt, please turn on pwmoc_6m_ck (6MHz) */
  2728. pmic_set_register_value(PMIC_RG_PWMOC_6M_CK_PDN, 0);
  2729. pmic_register_interrupt_callback(0, pwrkey_int_handler);
  2730. pmic_register_interrupt_callback(1, homekey_int_handler);
  2731. pmic_register_interrupt_callback(2, pwrkey_int_handler_r);
  2732. pmic_register_interrupt_callback(3, homekey_int_handler_r);
  2733. pmic_register_interrupt_callback(6, bat_h_int_handler);
  2734. pmic_register_interrupt_callback(7, bat_l_int_handler);
  2735. pmic_register_interrupt_callback(38, chrdet_int_handler);
  2736. pmic_register_interrupt_callback(42, fg_cur_h_int_handler);
  2737. pmic_register_interrupt_callback(43, fg_cur_l_int_handler);
  2738. pmic_enable_interrupt(0, 1, "PMIC");
  2739. pmic_enable_interrupt(1, 1, "PMIC");
  2740. pmic_enable_interrupt(2, 1, "PMIC");
  2741. pmic_enable_interrupt(3, 1, "PMIC");
  2742. #ifdef LOW_BATTERY_PROTECT
  2743. /* move to lbat_xxx_en_setting */
  2744. #else
  2745. pmic_enable_interrupt(6, 1, "PMIC");
  2746. pmic_enable_interrupt(7, 1, "PMIC");
  2747. #endif
  2748. /*pmic_enable_interrupt(30,1,"PMIC"); */
  2749. pmic_enable_interrupt(38, 1, "PMIC");
  2750. #ifdef BATTERY_OC_PROTECT
  2751. /* move to bat_oc_x_en_setting */
  2752. #else
  2753. pmic_enable_interrupt(42, 1, "PMIC");
  2754. pmic_enable_interrupt(43, 1, "PMIC");
  2755. #endif
  2756. #if 0
  2757. /*mt_eint_set_hw_debounce(g_eint_pmic_num, g_cust_eint_mt_pmic_debounce_cn); */
  2758. mt_eint_registration(g_eint_pmic_num, g_cust_eint_mt_pmic_type, mt_pmic_eint_irq, 0);
  2759. mt_eint_unmask(g_eint_pmic_num);
  2760. #else
  2761. node = of_find_compatible_node(NULL, NULL, "mediatek, pmic-eint");
  2762. if (node) {
  2763. of_property_read_u32_array(node, "debounce", ints, ARRAY_SIZE(ints));
  2764. mt_gpio_set_debounce(ints[0], ints[1]);
  2765. g_pmic_irq = irq_of_parse_and_map(node, 0);
  2766. ret =
  2767. request_irq(g_pmic_irq, (irq_handler_t) mt_pmic_eint_irq, IRQF_TRIGGER_NONE,
  2768. "pmic-eint", NULL);
  2769. if (ret > 0)
  2770. PMICLOG("EINT IRQ LINENNOT AVAILABLE\n");
  2771. /*enable_irq(g_pmic_irq); */
  2772. disable_irq(g_pmic_irq);
  2773. } else
  2774. PMICLOG("can't find compatible node\n");
  2775. #endif
  2776. PMICLOG("[CUST_EINT] CUST_EINT_MT_PMIC_MT6325_NUM=%d\n", g_eint_pmic_num);
  2777. PMICLOG("[CUST_EINT] CUST_EINT_PMIC_DEBOUNCE_CN=%d\n", g_cust_eint_mt_pmic_debounce_cn);
  2778. PMICLOG("[CUST_EINT] CUST_EINT_PMIC_TYPE=%d\n", g_cust_eint_mt_pmic_type);
  2779. PMICLOG("[CUST_EINT] CUST_EINT_PMIC_DEBOUNCE_EN=%d\n", g_cust_eint_mt_pmic_debounce_en);
  2780. }
  2781. static void pmic_int_handler(void)
  2782. {
  2783. unsigned char i, j;
  2784. unsigned int ret;
  2785. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  2786. unsigned int int_status_val = 0;
  2787. int_status_val = upmu_get_reg_value(interrupts[i].address);
  2788. PMICLOG("[PMIC_INT] addr[0x%x]=0x%x\n", interrupts[i].address, int_status_val);
  2789. for (j = 0; j < PMIC_INT_WIDTH; j++) {
  2790. if ((int_status_val) & (1 << j)) {
  2791. PMICLOG("[PMIC_INT][%s]\n", interrupts[i].interrupts[j].name);
  2792. if (interrupts[i].interrupts[j].callback != NULL) {
  2793. interrupts[i].interrupts[j].callback();
  2794. interrupts[i].interrupts[j].times++;
  2795. }
  2796. ret = pmic_config_interface(interrupts[i].address, 0x1, 0x1, j);
  2797. }
  2798. }
  2799. }
  2800. }
  2801. int pmic_rdy = 0, usb_rdy = 0;
  2802. void pmic_enable_charger_detection_int(int x)
  2803. {
  2804. if (x == 0) {
  2805. pmic_rdy = 1;
  2806. PMICLOG("[pmic_enable_charger_detection_int] PMIC\n");
  2807. } else if (x == 1) {
  2808. usb_rdy = 1;
  2809. PMICLOG("[pmic_enable_charger_detection_int] USB\n");
  2810. }
  2811. PMICLOG("[pmic_enable_charger_detection_int] pmic_rdy=%d usb_rdy=%d\n", pmic_rdy, usb_rdy);
  2812. if (pmic_rdy == 1 && usb_rdy == 1) {
  2813. #if defined(CONFIG_MTK_SMART_BATTERY)
  2814. wake_up_bat();
  2815. #endif
  2816. PMICLOG("[pmic_enable_charger_detection_int] enable charger detection interrupt\n");
  2817. }
  2818. }
  2819. bool is_charger_detection_rdy(void)
  2820. {
  2821. if (pmic_rdy == 1 && usb_rdy == 1)
  2822. return true;
  2823. else
  2824. return false;
  2825. }
  2826. static int pmic_thread_kthread(void *x)
  2827. {
  2828. unsigned int i;
  2829. unsigned int int_status_val = 0;
  2830. unsigned int pwrap_eint_status = 0;
  2831. struct sched_param param = {.sched_priority = 98 };
  2832. sched_setscheduler(current, SCHED_FIFO, &param);
  2833. set_current_state(TASK_INTERRUPTIBLE);
  2834. PMICLOG("[PMIC_INT] enter\n");
  2835. pmic_enable_charger_detection_int(0);
  2836. /* Run on a process content */
  2837. while (1) {
  2838. mutex_lock(&pmic_mutex);
  2839. pwrap_eint_status = pmic_wrap_eint_status();
  2840. PMICLOG("[PMIC_INT] pwrap_eint_status=0x%x\n", pwrap_eint_status);
  2841. pmic_int_handler();
  2842. pmic_wrap_eint_clr(0x0);
  2843. /*PMICLOG("[PMIC_INT] pmic_wrap_eint_clr(0x0);\n"); */
  2844. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  2845. int_status_val = upmu_get_reg_value(interrupts[i].address);
  2846. PMICLOG("[PMIC_INT] after ,int_status_val[0x%x]=0x%x\n",
  2847. interrupts[i].address, int_status_val);
  2848. }
  2849. mdelay(1);
  2850. mutex_unlock(&pmic_mutex);
  2851. #if !defined CONFIG_HAS_WAKELOCKS
  2852. __pm_relax(&pmicThread_lock);
  2853. #else
  2854. wake_unlock(&pmicThread_lock);
  2855. #endif
  2856. set_current_state(TASK_INTERRUPTIBLE);
  2857. #if 0 /*def CONFIG_MTK_LEGACY */
  2858. mt_eint_unmask(g_eint_pmic_num);
  2859. #else
  2860. if (g_pmic_irq != 0)
  2861. enable_irq(g_pmic_irq);
  2862. #endif
  2863. schedule();
  2864. }
  2865. return 0;
  2866. }
  2867. int is_ext_buck2_exist(void)
  2868. {
  2869. #if defined(CONFIG_MTK_FPGA)
  2870. return 0;
  2871. #else
  2872. #if !defined CONFIG_MTK_LEGACY
  2873. return gpiod_get_value(gpio_to_desc(130));
  2874. /*return __gpio_get_value(130); */
  2875. /*return mt_get_gpio_in(130); */
  2876. #else
  2877. return 0;
  2878. #endif
  2879. #endif
  2880. }
  2881. int is_ext_vbat_boost_exist(void)
  2882. {
  2883. return 0;
  2884. }
  2885. int is_ext_swchr_exist(void)
  2886. {
  2887. return 0;
  2888. }
  2889. /*****************************************************************************
  2890. * Enternal SWCHR
  2891. ******************************************************************************/
  2892. /*
  2893. #ifdef MTK_BQ24261_SUPPORT
  2894. extern int is_bq24261_exist(void);
  2895. #endif
  2896. int is_ext_swchr_exist(void)
  2897. {
  2898. #ifdef MTK_BQ24261_SUPPORT
  2899. if (is_bq24261_exist() == 1)
  2900. return 1;
  2901. else
  2902. return 0;
  2903. #else
  2904. PMICLOG("[is_ext_swchr_exist] no define any HW\n");
  2905. return 0;
  2906. #endif
  2907. }
  2908. */
  2909. /*****************************************************************************
  2910. * Enternal VBAT Boost status
  2911. ******************************************************************************/
  2912. /*
  2913. extern int is_tps6128x_sw_ready(void);
  2914. extern int is_tps6128x_exist(void);
  2915. int is_ext_vbat_boost_sw_ready(void)
  2916. {
  2917. if( (is_tps6128x_sw_ready()==1) )
  2918. return 1;
  2919. else
  2920. return 0;
  2921. }
  2922. int is_ext_vbat_boost_exist(void)
  2923. {
  2924. if( (is_tps6128x_exist()==1) )
  2925. return 1;
  2926. else
  2927. return 0;
  2928. }
  2929. */
  2930. /*****************************************************************************
  2931. * Enternal BUCK status
  2932. ******************************************************************************/
  2933. int get_ext_buck_i2c_ch_num(void)
  2934. {
  2935. if (is_mt6311_exist() == 1)
  2936. return get_mt6311_i2c_ch_num();
  2937. else
  2938. return -1;
  2939. }
  2940. int is_ext_buck_sw_ready(void)
  2941. {
  2942. if ((is_mt6311_sw_ready() == 1))
  2943. return 1;
  2944. else
  2945. return 0;
  2946. }
  2947. int is_ext_buck_exist(void)
  2948. {
  2949. if ((is_mt6311_exist() == 1))
  2950. return 1;
  2951. else
  2952. return 0;
  2953. }
  2954. /*****************************************************************************
  2955. * FTM
  2956. ******************************************************************************/
  2957. #define PMIC_DEVNAME "pmic_ftm"
  2958. #define Get_IS_EXT_BUCK_EXIST _IOW('k', 20, int)
  2959. #define Get_IS_EXT_VBAT_BOOST_EXIST _IOW('k', 21, int)
  2960. #define Get_IS_EXT_SWCHR_EXIST _IOW('k', 22, int)
  2961. #define Get_IS_EXT_BUCK2_EXIST _IOW('k', 23, int)
  2962. static struct class *pmic_class;
  2963. static struct cdev *pmic_cdev;
  2964. static int pmic_major;
  2965. static dev_t pmic_devno;
  2966. static long pmic_ftm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2967. {
  2968. int *user_data_addr;
  2969. int ret = 0;
  2970. int adc_in_data[2] = { 1, 1 };
  2971. int adc_out_data[2] = { 1, 1 };
  2972. switch (cmd) {
  2973. /*#if defined(FTM_EXT_BUCK_CHECK) */
  2974. case Get_IS_EXT_BUCK_EXIST:
  2975. user_data_addr = (int *)arg;
  2976. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2977. adc_out_data[0] = is_ext_buck_exist();
  2978. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2979. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_BUCK_EXIST:%d\n", adc_out_data[0]);
  2980. break;
  2981. /*#endif */
  2982. /*#if defined(FTM_EXT_VBAT_BOOST_CHECK) */
  2983. case Get_IS_EXT_VBAT_BOOST_EXIST:
  2984. user_data_addr = (int *)arg;
  2985. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2986. adc_out_data[0] = is_ext_vbat_boost_exist();
  2987. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2988. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_VBAT_BOOST_EXIST:%d\n", adc_out_data[0]);
  2989. break;
  2990. /*#endif */
  2991. /*#if defined(FEATURE_FTM_SWCHR_HW_DETECT) */
  2992. case Get_IS_EXT_SWCHR_EXIST:
  2993. user_data_addr = (int *)arg;
  2994. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  2995. adc_out_data[0] = is_ext_swchr_exist();
  2996. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  2997. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_SWCHR_EXIST:%d\n", adc_out_data[0]);
  2998. break;
  2999. /*#endif */
  3000. case Get_IS_EXT_BUCK2_EXIST:
  3001. user_data_addr = (int *)arg;
  3002. ret = copy_from_user(adc_in_data, user_data_addr, 8);
  3003. adc_out_data[0] = is_ext_buck2_exist();
  3004. ret = copy_to_user(user_data_addr, adc_out_data, 8);
  3005. PMICLOG("[pmic_ftm_ioctl] Get_IS_EXT_BUCK2_EXIST:%d\n", adc_out_data[0]);
  3006. break;
  3007. default:
  3008. PMICLOG("[pmic_ftm_ioctl] Error ID\n");
  3009. break;
  3010. }
  3011. return 0;
  3012. }
  3013. #ifdef CONFIG_COMPAT
  3014. static long pmic_ftm_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3015. {
  3016. int ret = 0;
  3017. switch (cmd) {
  3018. /*#if defined(FTM_EXT_BUCK_CHECK) */
  3019. case Get_IS_EXT_BUCK_EXIST:
  3020. case Get_IS_EXT_VBAT_BOOST_EXIST:
  3021. case Get_IS_EXT_SWCHR_EXIST:
  3022. case Get_IS_EXT_BUCK2_EXIST:
  3023. ret = file->f_op->unlocked_ioctl(file, cmd, arg);
  3024. break;
  3025. default:
  3026. PMICLOG("[pmic_ftm_compat_ioctl] Error ID\n");
  3027. break;
  3028. }
  3029. return 0;
  3030. }
  3031. #endif
  3032. static int pmic_ftm_open(struct inode *inode, struct file *file)
  3033. {
  3034. return 0;
  3035. }
  3036. static int pmic_ftm_release(struct inode *inode, struct file *file)
  3037. {
  3038. return 0;
  3039. }
  3040. static const struct file_operations pmic_ftm_fops = {
  3041. .owner = THIS_MODULE,
  3042. .unlocked_ioctl = pmic_ftm_ioctl,
  3043. #ifdef CONFIG_COMPAT
  3044. .compat_ioctl = pmic_ftm_compat_ioctl,
  3045. #endif
  3046. .open = pmic_ftm_open,
  3047. .release = pmic_ftm_release,
  3048. };
  3049. void pmic_ftm_init(void)
  3050. {
  3051. struct class_device *class_dev = NULL;
  3052. int ret = 0;
  3053. ret = alloc_chrdev_region(&pmic_devno, 0, 1, PMIC_DEVNAME);
  3054. if (ret)
  3055. PMICLOG("[pmic_ftm_init] Error: Can't Get Major number for pmic_ftm\n");
  3056. pmic_cdev = cdev_alloc();
  3057. pmic_cdev->owner = THIS_MODULE;
  3058. pmic_cdev->ops = &pmic_ftm_fops;
  3059. ret = cdev_add(pmic_cdev, pmic_devno, 1);
  3060. if (ret)
  3061. PMICLOG("[pmic_ftm_init] Error: cdev_add\n");
  3062. pmic_major = MAJOR(pmic_devno);
  3063. pmic_class = class_create(THIS_MODULE, PMIC_DEVNAME);
  3064. class_dev = (struct class_device *)device_create(pmic_class,
  3065. NULL, pmic_devno, NULL, PMIC_DEVNAME);
  3066. PMICLOG("[pmic_ftm_init] Done\n");
  3067. }
  3068. /*****************************************************************************
  3069. * HW Setting
  3070. ******************************************************************************/
  3071. unsigned short is_battery_remove = 0;
  3072. unsigned short is_wdt_reboot_pmic = 0;
  3073. unsigned short is_battery_remove_pmic(void)
  3074. {
  3075. return is_battery_remove;
  3076. }
  3077. /*extern bool crystal_exist_status(void);*/
  3078. void pmic_setting_for_co_tsx(void)
  3079. {
  3080. unsigned int ret = 0;
  3081. unsigned int devinfo = get_devinfo_with_index(47) >> 25;
  3082. switch (devinfo) {
  3083. case 0x41:
  3084. case 0x42:
  3085. case 0x43:
  3086. /* Denali-1+ MT6737T */
  3087. case 0x49:
  3088. case 0x4A:
  3089. case 0x4B:
  3090. /* Denali-2+ MT6737M */
  3091. case 0x51:
  3092. case 0x52:
  3093. case 0x53:
  3094. /* Denali-2+ MT6737 */
  3095. ret = pmic_config_interface(0x14, 0x1, 0x1, 5);
  3096. ret = pmic_config_interface(0x14, 0x1, 0x1, 7);
  3097. ret = pmic_config_interface(0x25A, 0x0, 0x1, 10);
  3098. ret = pmic_config_interface(0x278, 0x0, 0x1, 11);
  3099. ret = pmic_config_interface(0xF08, 0xC, 0x3FF, 0);
  3100. ret = pmic_config_interface(0xF08, 0x0, 0x1, 15);
  3101. ret = pmic_config_interface(0xF0E, 0xC, 0x3FF, 0);
  3102. ret = pmic_config_interface(0xF0E, 0x1, 0x1, 15);
  3103. ret = pmic_config_interface(0xF12, 0x0, 0x1, 0);
  3104. ret = pmic_config_interface(0xF12, 0x0, 0x1, 1);
  3105. ret = pmic_config_interface(0xF12, 0x1, 0x1, 2);
  3106. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] setting for co-TSX\n");
  3107. break;
  3108. default:
  3109. break;
  3110. }
  3111. }
  3112. void PMIC_INIT_SETTING_V1(void)
  3113. {
  3114. unsigned int chip_version = 0;
  3115. unsigned int ret = 0;
  3116. chip_version = pmic_get_register_value(PMIC_SWCID);
  3117. is_battery_remove = !pmic_get_register_value(PMIC_STRUP_PWROFF_SEQ_EN);
  3118. is_wdt_reboot_pmic = pmic_get_register_value(PMIC_WDTRSTB_STATUS);
  3119. pmic_set_register_value(PMIC_WDTRSTB_STATUS_CLR, 1);
  3120. /*--------------------------------------------------------*/
  3121. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 6328 PMIC Chip = 0x%x\n", chip_version);
  3122. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 2015-03-12...\n");
  3123. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] is_battery_remove =%d is_wdt_reboot=%d\n",
  3124. is_battery_remove, is_wdt_reboot_pmic);
  3125. if (is_ext_buck_exist() == 1) {
  3126. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 2015-04-27 for turbo...\n");
  3127. ret = pmic_config_interface(0x4, 0x1, 0x1, 4);
  3128. ret = pmic_config_interface(0xA, 0x1, 0x1, 0);
  3129. ret = pmic_config_interface(0xA, 0x1, 0x1, 11);
  3130. ret = pmic_config_interface(0xC, 0x1, 0x1, 0);
  3131. ret = pmic_config_interface(0xC, 0x1, 0x1, 1);
  3132. ret = pmic_config_interface(0xC, 0x1, 0x1, 4);
  3133. ret = pmic_config_interface(0xC, 0x1, 0x1, 5);
  3134. ret = pmic_config_interface(0xC, 0x1, 0x1, 6);
  3135. ret = pmic_config_interface(0xC, 0x1, 0x1, 7);
  3136. ret = pmic_config_interface(0xC, 0x1, 0x1, 8);
  3137. ret = pmic_config_interface(0xC, 0x1, 0x1, 9);
  3138. ret = pmic_config_interface(0xC, 0x1, 0x1, 10);
  3139. ret = pmic_config_interface(0xC, 0x1, 0x1, 11);
  3140. ret = pmic_config_interface(0xC, 0x1, 0x1, 12);
  3141. ret = pmic_config_interface(0xC, 0x1, 0x1, 13);
  3142. ret = pmic_config_interface(0xE, 0x1, 0x1, 0);
  3143. ret = pmic_config_interface(0x10, 0x1, 0x1, 5);
  3144. ret = pmic_config_interface(0x16, 0x1, 0x1, 0);
  3145. ret = pmic_config_interface(0x16, 0x1, 0x1, 1);
  3146. ret = pmic_config_interface(0x1E, 0x0, 0x1, 11);
  3147. ret = pmic_config_interface(0x32, 0x1, 0x1, 15);
  3148. ret = pmic_config_interface(0x40, 0x1, 0x1, 12);
  3149. ret = pmic_config_interface(0x204, 0x1, 0x1, 4);
  3150. ret = pmic_config_interface(0x204, 0x1, 0x1, 5);
  3151. ret = pmic_config_interface(0x204, 0x0, 0x1, 6);
  3152. ret = pmic_config_interface(0x226, 0x1, 0x1, 0);
  3153. ret = pmic_config_interface(0x226, 0x1, 0x1, 2);
  3154. ret = pmic_config_interface(0x226, 0x1, 0x1, 3);
  3155. ret = pmic_config_interface(0x242, 0x1, 0x1, 2);
  3156. ret = pmic_config_interface(0x242, 0x1, 0x1, 3);
  3157. ret = pmic_config_interface(0x248, 0x1, 0x1, 13);
  3158. ret = pmic_config_interface(0x248, 0x1, 0x1, 14);
  3159. ret = pmic_config_interface(0x25A, 0x1, 0x1, 9);
  3160. ret = pmic_config_interface(0x40E, 0x0, 0x3, 2);
  3161. ret = pmic_config_interface(0x412, 0x0, 0x3, 2);
  3162. ret = pmic_config_interface(0x420, 0x1, 0x1, 4);
  3163. ret = pmic_config_interface(0x422, 0x0, 0x1, 0);
  3164. ret = pmic_config_interface(0x422, 0x0, 0x1, 1);
  3165. ret = pmic_config_interface(0x422, 0x0, 0x1, 2);
  3166. ret = pmic_config_interface(0x424, 0x0, 0x7F, 0);
  3167. ret = pmic_config_interface(0x424, 0x10, 0x7F, 8);
  3168. ret = pmic_config_interface(0x426, 0x48, 0x7F, 0);
  3169. ret = pmic_config_interface(0x426, 0x78, 0x7F, 8);
  3170. ret = pmic_config_interface(0x428, 0x28, 0x7F, 0);
  3171. ret = pmic_config_interface(0x42E, 0x1, 0x1FF, 0);
  3172. ret = pmic_config_interface(0x446, 0x2, 0x7, 8);
  3173. ret = pmic_config_interface(0x44C, 0x0, 0x3, 7);
  3174. ret = pmic_config_interface(0x450, 0x80, 0xFF, 0);
  3175. ret = pmic_config_interface(0x452, 0x8, 0x3F, 3);
  3176. ret = pmic_config_interface(0x462, 0x3, 0x3, 10);
  3177. ret = pmic_config_interface(0x470, 0x2, 0x7, 8);
  3178. ret = pmic_config_interface(0x486, 0x1, 0x1, 0);
  3179. ret = pmic_config_interface(0x488, 0x11, 0x7F, 0);
  3180. ret = pmic_config_interface(0x488, 0x0, 0x1, 7);
  3181. ret = pmic_config_interface(0x488, 0x4, 0x7F, 8);
  3182. ret = pmic_config_interface(0x488, 0x0, 0x1, 15);
  3183. ret = pmic_config_interface(0x498, 0x1, 0x1, 8);
  3184. ret = pmic_config_interface(0x49A, 0x0, 0x3, 4);
  3185. ret = pmic_config_interface(0x49A, 0x0, 0x3, 8);
  3186. ret = pmic_config_interface(0x49A, 0x0, 0x3, 14);
  3187. ret = pmic_config_interface(0x4AA, 0x0, 0x1, 1);
  3188. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 0);
  3189. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 7);
  3190. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 8);
  3191. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 15);
  3192. ret = pmic_config_interface(0x4B4, 0x38, 0x7F, 0);
  3193. ret = pmic_config_interface(0x4B6, 0x28, 0x7F, 0);
  3194. ret = pmic_config_interface(0x4D2, 0x1, 0x1, 1);
  3195. ret = pmic_config_interface(0x4D8, 0x11, 0x7F, 0);
  3196. ret = pmic_config_interface(0x4D8, 0x4, 0x7F, 8);
  3197. ret = pmic_config_interface(0x4DE, 0x28, 0x7F, 0);
  3198. ret = pmic_config_interface(0x4E8, 0x3, 0x3, 0);
  3199. ret = pmic_config_interface(0x4E8, 0x1, 0x1, 8);
  3200. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 4);
  3201. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 8);
  3202. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 14);
  3203. ret = pmic_config_interface(0x60E, 0x1, 0x1, 1);
  3204. ret = pmic_config_interface(0x614, 0x11, 0x7F, 0);
  3205. ret = pmic_config_interface(0x614, 0x4, 0x7F, 8);
  3206. ret = pmic_config_interface(0x61A, 0x28, 0x7F, 0);
  3207. ret = pmic_config_interface(0x624, 0x3, 0x3, 0);
  3208. ret = pmic_config_interface(0x624, 0x1, 0x1, 8);
  3209. ret = pmic_config_interface(0x626, 0x0, 0x3, 4);
  3210. ret = pmic_config_interface(0x626, 0x0, 0x3, 8);
  3211. ret = pmic_config_interface(0x626, 0x0, 0x3, 14);
  3212. ret = pmic_config_interface(0x646, 0x5, 0x7, 0);
  3213. ret = pmic_config_interface(0x64C, 0x0, 0x1, 8);
  3214. ret = pmic_config_interface(0x664, 0x2, 0x7F, 0);
  3215. ret = pmic_config_interface(0x664, 0x0, 0x1, 7);
  3216. ret = pmic_config_interface(0x664, 0x2, 0x7F, 8);
  3217. ret = pmic_config_interface(0x664, 0x0, 0x1, 15);
  3218. ret = pmic_config_interface(0x674, 0x3, 0x3, 4);
  3219. ret = pmic_config_interface(0x67E, 0x2, 0x3, 4);
  3220. ret = pmic_config_interface(0xA00, 0x1, 0x1, 3);
  3221. ret = pmic_config_interface(0xA02, 0x1, 0x1, 3);
  3222. ret = pmic_config_interface(0xA06, 0x1, 0x1, 6);
  3223. ret = pmic_config_interface(0xA30, 0x0, 0x1, 0);
  3224. ret = pmic_config_interface(0xA44, 0x1, 0x1, 1);
  3225. ret = pmic_config_interface(0xA46, 0x0, 0x1, 14);
  3226. ret = pmic_config_interface(0xA64, 0x2, 0x3, 4);
  3227. ret = pmic_config_interface(0xA88, 0x68, 0x7F, 0);
  3228. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 8);
  3229. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 9);
  3230. ret = pmic_config_interface(0xCC0, 0x24, 0xFFFF, 0);
  3231. ret = pmic_config_interface(0xCC2, 0x14, 0xFF, 0);
  3232. ret = pmic_config_interface(0xCC4, 0xFF, 0xFF, 8);
  3233. ret = pmic_config_interface(0xE94, 0x0, 0x1, 13);
  3234. ret = pmic_config_interface(0xE94, 0x0, 0x1, 14);
  3235. ret = pmic_config_interface(0xE94, 0x0, 0x1, 15);
  3236. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 4);
  3237. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 6);
  3238. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 8);
  3239. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 10);
  3240. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 12);
  3241. ret = pmic_config_interface(0xEA4, 0x2, 0x3, 14);
  3242. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 0);
  3243. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 2);
  3244. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 4);
  3245. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 6);
  3246. ret = pmic_config_interface(0xEB8, 0x1, 0x1, 14);
  3247. ret = pmic_config_interface(0xF4A, 0xB, 0xF, 4);
  3248. ret = pmic_config_interface(0xF54, 0x0, 0x7, 1);
  3249. ret = pmic_config_interface(0xF62, 0x3, 0xF, 0);
  3250. ret = pmic_config_interface(0xF6C, 0x2, 0x1F, 0);
  3251. ret = pmic_config_interface(0xF70, 0x1, 0x1, 1);
  3252. ret = pmic_config_interface(0xF74, 0x0, 0x7, 4);
  3253. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 2);
  3254. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 6);
  3255. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 7);
  3256. } else {
  3257. PMICLOG("[Kernel_PMIC_INIT_SETTING_V1] 2015-11-4...\n");
  3258. ret = pmic_config_interface(0x4, 0x1, 0x1, 4);
  3259. ret = pmic_config_interface(0xA, 0x1, 0x1, 0);
  3260. ret = pmic_config_interface(0xA, 0x1, 0x1, 11);
  3261. ret = pmic_config_interface(0xC, 0x1, 0x1, 0);
  3262. ret = pmic_config_interface(0xC, 0x1, 0x1, 1);
  3263. ret = pmic_config_interface(0xC, 0x1, 0x1, 4);
  3264. ret = pmic_config_interface(0xC, 0x1, 0x1, 5);
  3265. ret = pmic_config_interface(0xC, 0x1, 0x1, 6);
  3266. ret = pmic_config_interface(0xC, 0x1, 0x1, 7);
  3267. ret = pmic_config_interface(0xC, 0x1, 0x1, 8);
  3268. ret = pmic_config_interface(0xC, 0x1, 0x1, 9);
  3269. ret = pmic_config_interface(0xC, 0x1, 0x1, 10);
  3270. ret = pmic_config_interface(0xC, 0x1, 0x1, 11);
  3271. ret = pmic_config_interface(0xC, 0x1, 0x1, 12);
  3272. ret = pmic_config_interface(0xC, 0x1, 0x1, 13);
  3273. ret = pmic_config_interface(0x10, 0x1, 0x1, 5);
  3274. ret = pmic_config_interface(0x16, 0x1, 0x1, 0);
  3275. ret = pmic_config_interface(0x16, 0x1, 0x1, 1);
  3276. ret = pmic_config_interface(0x1E, 0x0, 0x1, 11);
  3277. ret = pmic_config_interface(0x40, 0x1, 0x1, 12);
  3278. ret = pmic_config_interface(0x204, 0x1, 0x1, 4);
  3279. ret = pmic_config_interface(0x204, 0x1, 0x1, 5);
  3280. ret = pmic_config_interface(0x204, 0x0, 0x1, 6);
  3281. ret = pmic_config_interface(0x226, 0x1, 0x1, 0);
  3282. ret = pmic_config_interface(0x226, 0x1, 0x1, 2);
  3283. ret = pmic_config_interface(0x226, 0x1, 0x1, 3);
  3284. ret = pmic_config_interface(0x242, 0x1, 0x1, 2);
  3285. ret = pmic_config_interface(0x242, 0x1, 0x1, 3);
  3286. ret = pmic_config_interface(0x248, 0x1, 0x1, 13);
  3287. ret = pmic_config_interface(0x248, 0x1, 0x1, 14);
  3288. ret = pmic_config_interface(0x25A, 0x1, 0x1, 9);
  3289. ret = pmic_config_interface(0x40E, 0x0, 0x3, 2);
  3290. ret = pmic_config_interface(0x412, 0x0, 0x3, 2);
  3291. ret = pmic_config_interface(0x420, 0x1, 0x1, 4);
  3292. ret = pmic_config_interface(0x422, 0x1, 0x1, 0);
  3293. ret = pmic_config_interface(0x422, 0x1, 0x1, 1);
  3294. ret = pmic_config_interface(0x422, 0x1, 0x1, 2);
  3295. ret = pmic_config_interface(0x424, 0x0, 0x7F, 0);
  3296. ret = pmic_config_interface(0x424, 0x10, 0x7F, 8);
  3297. ret = pmic_config_interface(0x426, 0x48, 0x7F, 0);
  3298. ret = pmic_config_interface(0x426, 0x78, 0x7F, 8);
  3299. ret = pmic_config_interface(0x428, 0x28, 0x7F, 0);
  3300. ret = pmic_config_interface(0x42E, 0x1, 0x1FF, 0);
  3301. ret = pmic_config_interface(0x446, 0x2, 0x7, 8);
  3302. ret = pmic_config_interface(0x44C, 0x0, 0x3, 7);
  3303. ret = pmic_config_interface(0x450, 0x80, 0xFF, 0);
  3304. ret = pmic_config_interface(0x452, 0x8, 0x3F, 3);
  3305. ret = pmic_config_interface(0x45A, 0x2, 0x7, 8);
  3306. ret = pmic_config_interface(0x462, 0x3, 0x3, 10);
  3307. ret = pmic_config_interface(0x470, 0x2, 0x7, 8);
  3308. ret = pmic_config_interface(0x482, 0x1, 0x1, 1);
  3309. ret = pmic_config_interface(0x488, 0x11, 0x7F, 0);
  3310. ret = pmic_config_interface(0x488, 0x1, 0x1, 7);
  3311. ret = pmic_config_interface(0x488, 0x4, 0x7F, 8);
  3312. ret = pmic_config_interface(0x488, 0x1, 0x1, 15);
  3313. ret = pmic_config_interface(0x48E, 0x28, 0x7F, 0);
  3314. ret = pmic_config_interface(0x498, 0x3, 0x3, 0);
  3315. ret = pmic_config_interface(0x498, 0x1, 0x3, 4);
  3316. ret = pmic_config_interface(0x498, 0x1, 0x1, 8);
  3317. ret = pmic_config_interface(0x49A, 0x0, 0x3, 4);
  3318. ret = pmic_config_interface(0x49A, 0x0, 0x3, 8);
  3319. ret = pmic_config_interface(0x49A, 0x0, 0x3, 14);
  3320. ret = pmic_config_interface(0x4AA, 0x1, 0x1, 1);
  3321. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 0);
  3322. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 7);
  3323. ret = pmic_config_interface(0x4B0, 0x8, 0x7F, 8);
  3324. ret = pmic_config_interface(0x4B0, 0x1, 0x1, 15);
  3325. ret = pmic_config_interface(0x4B4, 0x40, 0x7F, 0);
  3326. ret = pmic_config_interface(0x4D2, 0x1, 0x1, 1);
  3327. ret = pmic_config_interface(0x4D8, 0x11, 0x7F, 0);
  3328. ret = pmic_config_interface(0x4D8, 0x4, 0x7F, 8);
  3329. ret = pmic_config_interface(0x4DE, 0x28, 0x7F, 0);
  3330. ret = pmic_config_interface(0x4E8, 0x3, 0x3, 0);
  3331. ret = pmic_config_interface(0x4E8, 0x1, 0x1, 8);
  3332. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 4);
  3333. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 8);
  3334. ret = pmic_config_interface(0x4EA, 0x0, 0x3, 14);
  3335. ret = pmic_config_interface(0x60E, 0x1, 0x1, 1);
  3336. ret = pmic_config_interface(0x614, 0x11, 0x7F, 0);
  3337. ret = pmic_config_interface(0x614, 0x4, 0x7F, 8);
  3338. ret = pmic_config_interface(0x61A, 0x28, 0x7F, 0);
  3339. ret = pmic_config_interface(0x624, 0x3, 0x3, 0);
  3340. ret = pmic_config_interface(0x624, 0x1, 0x1, 8);
  3341. ret = pmic_config_interface(0x626, 0x0, 0x3, 4);
  3342. ret = pmic_config_interface(0x626, 0x0, 0x3, 8);
  3343. ret = pmic_config_interface(0x626, 0x0, 0x3, 14);
  3344. ret = pmic_config_interface(0x646, 0x5, 0x7, 0);
  3345. ret = pmic_config_interface(0x64C, 0x0, 0x1, 8);
  3346. ret = pmic_config_interface(0x664, 0x2, 0x7F, 0);
  3347. ret = pmic_config_interface(0x664, 0x0, 0x1, 7);
  3348. ret = pmic_config_interface(0x664, 0x2, 0x7F, 8);
  3349. ret = pmic_config_interface(0x664, 0x0, 0x1, 15);
  3350. ret = pmic_config_interface(0x674, 0x3, 0x3, 4);
  3351. ret = pmic_config_interface(0x67E, 0x2, 0x3, 4);
  3352. ret = pmic_config_interface(0xA00, 0x1, 0x1, 3);
  3353. ret = pmic_config_interface(0xA02, 0x1, 0x1, 3);
  3354. ret = pmic_config_interface(0xA06, 0x1, 0x1, 6);
  3355. ret = pmic_config_interface(0xA30, 0x0, 0x1, 0);
  3356. ret = pmic_config_interface(0xA44, 0x1, 0x1, 1);
  3357. ret = pmic_config_interface(0xA46, 0x0, 0x1, 14);
  3358. ret = pmic_config_interface(0xA64, 0x2, 0x3, 4);
  3359. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 8);
  3360. ret = pmic_config_interface(0xCBC, 0x1, 0x1, 9);
  3361. ret = pmic_config_interface(0xCC0, 0x24, 0xFFFF, 0);
  3362. ret = pmic_config_interface(0xCC2, 0x14, 0xFF, 0);
  3363. ret = pmic_config_interface(0xCC4, 0xFF, 0xFF, 8);
  3364. ret = pmic_config_interface(0xE94, 0x0, 0x1, 13);
  3365. ret = pmic_config_interface(0xE94, 0x0, 0x1, 14);
  3366. ret = pmic_config_interface(0xE94, 0x0, 0x1, 15);
  3367. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 4);
  3368. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 6);
  3369. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 8);
  3370. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 10);
  3371. ret = pmic_config_interface(0xEA4, 0x1, 0x3, 12);
  3372. ret = pmic_config_interface(0xEA4, 0x2, 0x3, 14);
  3373. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 0);
  3374. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 2);
  3375. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 4);
  3376. ret = pmic_config_interface(0xEA6, 0x1, 0x3, 6);
  3377. ret = pmic_config_interface(0xEB8, 0x1, 0x1, 14);
  3378. ret = pmic_config_interface(0xF4A, 0xB, 0xF, 4);
  3379. ret = pmic_config_interface(0xF54, 0x0, 0x7, 1);
  3380. ret = pmic_config_interface(0xF62, 0x3, 0xF, 0);
  3381. ret = pmic_config_interface(0xF6C, 0x2, 0x1F, 0);
  3382. ret = pmic_config_interface(0xF70, 0x1, 0x1, 1);
  3383. ret = pmic_config_interface(0xF74, 0x0, 0x7, 4);
  3384. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 2);
  3385. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 6);
  3386. ret = pmic_config_interface(0xF7A, 0x1, 0x1, 7);
  3387. pmic_setting_for_co_tsx();
  3388. }
  3389. /*--------------------------------------------------------*/
  3390. if (crystal_exist_status() == 0) {
  3391. PMICLOG("32k-less VTCXO always on...\n");
  3392. pmic_set_register_value(PMIC_RG_VTCXO_0_ON_CTRL, 0);
  3393. pmic_set_register_value(PMIC_RG_VTCXO_0_EN, 1);
  3394. }
  3395. #if defined(PMIC_HW_USE_4L_SS_LAYOUT)
  3396. ret = pmic_config_interface(0x494, 0x0, 0x3, 0); /* [1:0]: VPROC slow slew rate */
  3397. ret = pmic_config_interface(0x496, 0x0, 0x3, 0); /* [1:0]: VPROC slow slew rate */
  3398. ret = pmic_config_interface(0x620, 0x0, 0x3, 0); /* [1:0]: VCORE slow slew rate */
  3399. ret = pmic_config_interface(0x622, 0x0, 0x3, 0); /* [1:0]: VCORE slow slew rate */
  3400. ret = pmic_config_interface(0x4E4, 0x0, 0x3, 0); /* [1:0]: VLTE slow slew rate */
  3401. ret = pmic_config_interface(0x4E6, 0x0, 0x3, 0); /* [1:0]: VLTE slow slew rate */
  3402. ret = pmic_config_interface(0x648, 0x0, 0x3, 0); /* [1:0]: VSYS slow slew rate */
  3403. ret = pmic_config_interface(0x64A, 0x0, 0x3, 0); /* [1:0]: VSYS slow slew rate */
  3404. #endif
  3405. }
  3406. #if defined CONFIG_MTK_LEGACY
  3407. /*extern void pmu_drv_tool_customization_init(void);*/
  3408. #endif
  3409. void PMIC_CUSTOM_SETTING_V1(void)
  3410. {
  3411. #if 0
  3412. #if defined CONFIG_MTK_LEGACY
  3413. #if defined(CONFIG_MTK_FPGA)
  3414. #else
  3415. pmu_drv_tool_customization_init(); /* legacy DCT only */
  3416. #endif
  3417. #endif
  3418. #endif
  3419. }
  3420. /*****************************************************************************
  3421. * Dump all LDO status
  3422. ******************************************************************************/
  3423. void dump_ldo_status_read_debug(void)
  3424. {
  3425. int i, j;
  3426. int en = 0;
  3427. int voltage_reg = 0;
  3428. int voltage = 0;
  3429. const int *pVoltage;
  3430. pr_debug("********** BUCK/LDO status dump [1:ON,0:OFF]**********\n");
  3431. for (i = 0; i < ARRAY_SIZE(mtk_bucks); i++) {
  3432. if (mtk_bucks[i].qi_en_reg != 0)
  3433. en = pmic_get_register_value(mtk_bucks[i].qi_en_reg);
  3434. else
  3435. en = -1;
  3436. if (mtk_bucks[i].qi_vol_reg != 0) {
  3437. voltage_reg = pmic_get_register_value(mtk_bucks[i].qi_vol_reg);
  3438. voltage =
  3439. mtk_bucks[i].desc.min_uV + mtk_bucks[i].desc.uV_step * voltage_reg;
  3440. } else {
  3441. voltage_reg = -1;
  3442. voltage = -1;
  3443. }
  3444. pr_err("%s status:%d voltage:%duv voltage_reg:%d\n",
  3445. mtk_bucks[i].desc.name, en, voltage, voltage_reg);
  3446. }
  3447. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  3448. if (mtk_ldos[i].en_reg != 0)
  3449. en = pmic_get_register_value(mtk_ldos[i].en_reg);
  3450. else
  3451. en = -1;
  3452. if (mtk_ldos[i].desc.n_voltages != 1) {
  3453. if (mtk_ldos[i].vol_reg != 0) {
  3454. voltage_reg = pmic_get_register_value(mtk_ldos[i].vol_reg);
  3455. if (mtk_ldos[i].pvoltages != NULL) {
  3456. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3457. voltage = pVoltage[voltage_reg];
  3458. } else {
  3459. voltage =
  3460. mtk_ldos[i].desc.min_uV +
  3461. mtk_ldos[i].desc.uV_step * voltage_reg;
  3462. }
  3463. } else {
  3464. voltage_reg = -1;
  3465. voltage = -1;
  3466. }
  3467. } else {
  3468. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3469. voltage = pVoltage[0];
  3470. }
  3471. pr_err("%s status:%d voltage:%duv voltage_reg:%d\n",
  3472. mtk_ldos[i].desc.name, en, voltage, voltage_reg);
  3473. }
  3474. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  3475. for (j = 0; j < PMIC_INT_WIDTH; j++) {
  3476. PMICLOG("[PMIC_INT][%s] interrupt issue times: %d\n",
  3477. interrupts[i].interrupts[j].name,
  3478. interrupts[i].interrupts[j].times);
  3479. }
  3480. }
  3481. PMICLOG("Power Good Status=0x%x.\n", upmu_get_reg_value(0x21c));
  3482. PMICLOG("OC Status=0x%x.\n", upmu_get_reg_value(0x214));
  3483. PMICLOG("Thermal Status=0x%x.\n", upmu_get_reg_value(0x21e));
  3484. }
  3485. static int proc_utilization_show(struct seq_file *m, void *v)
  3486. {
  3487. int i, j;
  3488. int en = 0;
  3489. int voltage_reg = 0;
  3490. int voltage = 0;
  3491. const int *pVoltage;
  3492. seq_puts(m, "********** BUCK/LDO status dump [1:ON,0:OFF]**********\n");
  3493. for (i = 0; i < ARRAY_SIZE(mtk_bucks); i++) {
  3494. if (mtk_bucks[i].qi_en_reg != 0)
  3495. en = pmic_get_register_value(mtk_bucks[i].qi_en_reg);
  3496. else
  3497. en = -1;
  3498. if (mtk_bucks[i].qi_vol_reg != 0) {
  3499. voltage_reg = pmic_get_register_value(mtk_bucks[i].qi_vol_reg);
  3500. voltage =
  3501. mtk_bucks[i].desc.min_uV + mtk_bucks[i].desc.uV_step * voltage_reg;
  3502. } else {
  3503. voltage_reg = -1;
  3504. voltage = -1;
  3505. }
  3506. seq_printf(m, "%s status:%d voltage:%duv voltage_reg:%d\n",
  3507. mtk_bucks[i].desc.name, en, voltage, voltage_reg);
  3508. }
  3509. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  3510. if (mtk_ldos[i].en_reg != 0)
  3511. en = pmic_get_register_value(mtk_ldos[i].en_reg);
  3512. else
  3513. en = -1;
  3514. if (mtk_ldos[i].desc.n_voltages != 1) {
  3515. if (mtk_ldos[i].vol_reg != 0) {
  3516. voltage_reg = pmic_get_register_value(mtk_ldos[i].vol_reg);
  3517. if (mtk_ldos[i].pvoltages != NULL) {
  3518. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3519. voltage = pVoltage[voltage_reg];
  3520. } else {
  3521. voltage =
  3522. mtk_ldos[i].desc.min_uV +
  3523. mtk_ldos[i].desc.uV_step * voltage_reg;
  3524. }
  3525. } else {
  3526. voltage_reg = -1;
  3527. voltage = -1;
  3528. }
  3529. } else {
  3530. pVoltage = (const int *)mtk_ldos[i].pvoltages;
  3531. voltage = pVoltage[0];
  3532. }
  3533. seq_printf(m, "%s status:%d voltage:%duv voltage_reg:%d\n",
  3534. mtk_ldos[i].desc.name, en, voltage, voltage_reg);
  3535. }
  3536. for (i = 0; i < ARRAY_SIZE(interrupts); i++) {
  3537. for (j = 0; j < PMIC_INT_WIDTH; j++) {
  3538. seq_printf(m, "[PMIC_INT][%s] interrupt issue times: %d\n",
  3539. interrupts[i].interrupts[j].name,
  3540. interrupts[i].interrupts[j].times);
  3541. }
  3542. }
  3543. seq_printf(m, "Power Good Status=0x%x.\n", upmu_get_reg_value(0x21c));
  3544. seq_printf(m, "OC Status=0x%x.\n", upmu_get_reg_value(0x214));
  3545. seq_printf(m, "Thermal Status=0x%x.\n", upmu_get_reg_value(0x21e));
  3546. return 0;
  3547. }
  3548. static int proc_utilization_open(struct inode *inode, struct file *file)
  3549. {
  3550. return single_open(file, proc_utilization_show, NULL);
  3551. }
  3552. static const struct file_operations pmic_debug_proc_fops = {
  3553. .open = proc_utilization_open,
  3554. .read = seq_read,
  3555. };
  3556. static int proc_dump_register_show(struct seq_file *m, void *v)
  3557. {
  3558. int i;
  3559. seq_puts(m, "********** dump PMIC registers**********\n");
  3560. for (i = 0; i <= 0x0fae; i = i + 10) {
  3561. seq_printf(m, "Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x\n",
  3562. i, upmu_get_reg_value(i), i + 1, upmu_get_reg_value(i + 1), i + 2,
  3563. upmu_get_reg_value(i + 2), i + 3, upmu_get_reg_value(i + 3), i + 4,
  3564. upmu_get_reg_value(i + 4));
  3565. seq_printf(m, "Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x Reg[%x]=0x%x\n",
  3566. i + 5, upmu_get_reg_value(i + 5), i + 6, upmu_get_reg_value(i + 6),
  3567. i + 7, upmu_get_reg_value(i + 7), i + 8, upmu_get_reg_value(i + 8),
  3568. i + 9, upmu_get_reg_value(i + 9));
  3569. }
  3570. return 0;
  3571. }
  3572. static int proc_dump_register_open(struct inode *inode, struct file *file)
  3573. {
  3574. return single_open(file, proc_dump_register_show, NULL);
  3575. }
  3576. static const struct file_operations pmic_dump_register_proc_fops = {
  3577. .open = proc_dump_register_open,
  3578. .read = seq_read,
  3579. };
  3580. void pmic_debug_init(void)
  3581. {
  3582. struct proc_dir_entry *mt_pmic_dir;
  3583. mt_pmic_dir = proc_mkdir("mt_pmic", NULL);
  3584. if (!mt_pmic_dir) {
  3585. PMICLOG("fail to mkdir /proc/mt_pmic\n");
  3586. return;
  3587. }
  3588. proc_create("dump_ldo_status", S_IRUGO | S_IWUSR, mt_pmic_dir, &pmic_debug_proc_fops);
  3589. PMICLOG("proc_create pmic_debug_proc_fops\n");
  3590. proc_create("dump_pmic_reg", S_IRUGO | S_IWUSR, mt_pmic_dir, &pmic_dump_register_proc_fops);
  3591. PMICLOG("proc_create pmic_dump_register_proc_fops\n");
  3592. }
  3593. static bool pwrkey_detect_flag;
  3594. static struct hrtimer pwrkey_detect_timer;
  3595. static struct task_struct *pwrkey_detect_thread;
  3596. static DECLARE_WAIT_QUEUE_HEAD(pwrkey_detect_waiter);
  3597. #define BAT_MS_TO_NS(x) (x * 1000 * 1000)
  3598. enum hrtimer_restart pwrkey_detect_sw_workaround(struct hrtimer *timer)
  3599. {
  3600. pwrkey_detect_flag = true;
  3601. wake_up_interruptible(&pwrkey_detect_waiter);
  3602. return HRTIMER_NORESTART;
  3603. }
  3604. int pwrkey_detect_sw_thread_handler(void *unused)
  3605. {
  3606. ktime_t ktime;
  3607. do {
  3608. ktime = ktime_set(3, BAT_MS_TO_NS(1000));
  3609. wait_event_interruptible(pwrkey_detect_waiter, (pwrkey_detect_flag == true));
  3610. /*PMICLOG("=>charger_hv_detect_sw_workaround\n"); */
  3611. if (pmic_get_register_value(PMIC_RG_STRUP_75K_CK_PDN) == 1) {
  3612. PMICLOG("charger_hv_detect_sw_workaround =0x%x\n",
  3613. upmu_get_reg_value(0x24e));
  3614. pmic_set_register_value(PMIC_RG_STRUP_75K_CK_PDN, 0);
  3615. }
  3616. hrtimer_start(&pwrkey_detect_timer, ktime, HRTIMER_MODE_REL);
  3617. } while (!kthread_should_stop());
  3618. return 0;
  3619. }
  3620. void pwrkey_sw_workaround_init(void)
  3621. {
  3622. ktime_t ktime;
  3623. ktime = ktime_set(0, BAT_MS_TO_NS(2000));
  3624. hrtimer_init(&pwrkey_detect_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3625. pwrkey_detect_timer.function = pwrkey_detect_sw_workaround;
  3626. hrtimer_start(&pwrkey_detect_timer, ktime, HRTIMER_MODE_REL);
  3627. pwrkey_detect_thread =
  3628. kthread_run(pwrkey_detect_sw_thread_handler, 0, "mtk pwrkey_sw_workaround_init");
  3629. if (IS_ERR(pwrkey_detect_thread))
  3630. PMICLOG("[%s]: failed to create pwrkey_detect_thread thread\n", __func__);
  3631. }
  3632. #ifdef LOW_BATTERY_PROTECT
  3633. /*****************************************************************************
  3634. * low battery protect UT
  3635. ******************************************************************************/
  3636. static ssize_t show_low_battery_protect_ut(struct device *dev, struct device_attribute *attr,
  3637. char *buf)
  3638. {
  3639. PMICLOG("[show_low_battery_protect_ut] g_low_battery_level=%d\n", g_low_battery_level);
  3640. return sprintf(buf, "%u\n", g_low_battery_level);
  3641. }
  3642. static ssize_t store_low_battery_protect_ut(struct device *dev, struct device_attribute *attr,
  3643. const char *buf, size_t size)
  3644. {
  3645. int ret = 0;
  3646. char *pvalue = NULL;
  3647. unsigned int val = 0;
  3648. pr_err("[store_low_battery_protect_ut]\n");
  3649. if (buf != NULL && size != 0) {
  3650. pr_err("[store_low_battery_protect_ut] buf is %s and size is %zu\n", buf, size);
  3651. /*val = simple_strtoul(buf, &pvalue, 16); */
  3652. pvalue = (char *)buf;
  3653. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3654. if (val <= 2) {
  3655. pr_err("[store_low_battery_protect_ut] your input is %d\n", val);
  3656. exec_low_battery_callback(val);
  3657. } else {
  3658. pr_err("[store_low_battery_protect_ut] wrong number (%d)\n", val);
  3659. }
  3660. }
  3661. return size;
  3662. }
  3663. static DEVICE_ATTR(low_battery_protect_ut, 0664, show_low_battery_protect_ut, store_low_battery_protect_ut); /*664 */
  3664. /*****************************************************************************
  3665. * low battery protect stop
  3666. ******************************************************************************/
  3667. static ssize_t show_low_battery_protect_stop(struct device *dev, struct device_attribute *attr,
  3668. char *buf)
  3669. {
  3670. PMICLOG("[show_low_battery_protect_stop] g_low_battery_stop=%d\n", g_low_battery_stop);
  3671. return sprintf(buf, "%u\n", g_low_battery_stop);
  3672. }
  3673. static ssize_t store_low_battery_protect_stop(struct device *dev, struct device_attribute *attr,
  3674. const char *buf, size_t size)
  3675. {
  3676. int ret = 0;
  3677. char *pvalue = NULL;
  3678. unsigned int val = 0;
  3679. pr_err("[store_low_battery_protect_stop]\n");
  3680. if (buf != NULL && size != 0) {
  3681. pr_err("[store_low_battery_protect_stop] buf is %s and size is %zu\n", buf, size);
  3682. /*val = simple_strtoul(buf, &pvalue, 16); */
  3683. pvalue = (char *)buf;
  3684. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3685. if ((val != 0) && (val != 1))
  3686. val = 0;
  3687. g_low_battery_stop = val;
  3688. pr_err("[store_low_battery_protect_stop] g_low_battery_stop=%d\n",
  3689. g_low_battery_stop);
  3690. }
  3691. return size;
  3692. }
  3693. static DEVICE_ATTR(low_battery_protect_stop, 0664,
  3694. show_low_battery_protect_stop, store_low_battery_protect_stop); /*664*/
  3695. /*
  3696. * low battery protect level
  3697. */
  3698. static ssize_t show_low_battery_protect_level(struct device *dev, struct device_attribute *attr,
  3699. char *buf)
  3700. {
  3701. PMICLOG("[show_low_battery_protect_level] g_low_battery_level=%d\n", g_low_battery_level);
  3702. return sprintf(buf, "%u\n", g_low_battery_level);
  3703. }
  3704. static ssize_t store_low_battery_protect_level(struct device *dev, struct device_attribute *attr,
  3705. const char *buf, size_t size)
  3706. {
  3707. PMICLOG("[store_low_battery_protect_level] g_low_battery_level=%d\n", g_low_battery_level);
  3708. return size;
  3709. }
  3710. static DEVICE_ATTR(low_battery_protect_level, 0664,
  3711. show_low_battery_protect_level, store_low_battery_protect_level); /*664*/
  3712. #endif
  3713. #ifdef BATTERY_OC_PROTECT
  3714. /*****************************************************************************
  3715. * battery OC protect UT
  3716. ******************************************************************************/
  3717. static ssize_t show_battery_oc_protect_ut(struct device *dev, struct device_attribute *attr,
  3718. char *buf)
  3719. {
  3720. PMICLOG("[show_battery_oc_protect_ut] g_battery_oc_level=%d\n", g_battery_oc_level);
  3721. return sprintf(buf, "%u\n", g_battery_oc_level);
  3722. }
  3723. static ssize_t store_battery_oc_protect_ut(struct device *dev, struct device_attribute *attr,
  3724. const char *buf, size_t size)
  3725. {
  3726. int ret = 0;
  3727. char *pvalue = NULL;
  3728. unsigned int val = 0;
  3729. pr_err("[store_battery_oc_protect_ut]\n");
  3730. if (buf != NULL && size != 0) {
  3731. pr_err("[store_battery_oc_protect_ut] buf is %s and size is %zu\n", buf, size);
  3732. /*val = simple_strtoul(buf, &pvalue, 16); */
  3733. pvalue = (char *)buf;
  3734. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3735. if (val <= 1) {
  3736. pr_err("[store_battery_oc_protect_ut] your input is %d\n", val);
  3737. exec_battery_oc_callback(val);
  3738. } else {
  3739. pr_err("[store_battery_oc_protect_ut] wrong number (%d)\n", val);
  3740. }
  3741. }
  3742. return size;
  3743. }
  3744. static DEVICE_ATTR(battery_oc_protect_ut, 0664,
  3745. show_battery_oc_protect_ut, store_battery_oc_protect_ut); /*664*/
  3746. /*****************************************************************************
  3747. * battery OC protect stop
  3748. ******************************************************************************/
  3749. static ssize_t show_battery_oc_protect_stop(struct device *dev, struct device_attribute *attr,
  3750. char *buf)
  3751. {
  3752. PMICLOG("[show_battery_oc_protect_stop] g_battery_oc_stop=%d\n", g_battery_oc_stop);
  3753. return sprintf(buf, "%u\n", g_battery_oc_stop);
  3754. }
  3755. static ssize_t store_battery_oc_protect_stop(struct device *dev, struct device_attribute *attr,
  3756. const char *buf, size_t size)
  3757. {
  3758. int ret = 0;
  3759. char *pvalue = NULL;
  3760. unsigned int val = 0;
  3761. pr_err("[store_battery_oc_protect_stop]\n");
  3762. if (buf != NULL && size != 0) {
  3763. pr_err("[store_battery_oc_protect_stop] buf is %s and size is %zu\n", buf, size);
  3764. /*val = simple_strtoul(buf, &pvalue, 16); */
  3765. pvalue = (char *)buf;
  3766. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3767. if ((val != 0) && (val != 1))
  3768. val = 0;
  3769. g_battery_oc_stop = val;
  3770. pr_err("[store_battery_oc_protect_stop] g_battery_oc_stop=%d\n", g_battery_oc_stop);
  3771. }
  3772. return size;
  3773. }
  3774. static DEVICE_ATTR(battery_oc_protect_stop, 0664,
  3775. show_battery_oc_protect_stop, store_battery_oc_protect_stop); /*664 */
  3776. /*****************************************************************************
  3777. * battery OC protect level
  3778. ******************************************************************************/
  3779. static ssize_t show_battery_oc_protect_level(struct device *dev, struct device_attribute *attr,
  3780. char *buf)
  3781. {
  3782. PMICLOG("[show_battery_oc_protect_level] g_battery_oc_level=%d\n", g_battery_oc_level);
  3783. return sprintf(buf, "%u\n", g_battery_oc_level);
  3784. }
  3785. static ssize_t store_battery_oc_protect_level(struct device *dev, struct device_attribute *attr,
  3786. const char *buf, size_t size)
  3787. {
  3788. PMICLOG("[store_battery_oc_protect_level] g_battery_oc_level=%d\n", g_battery_oc_level);
  3789. return size;
  3790. }
  3791. static DEVICE_ATTR(battery_oc_protect_level, 0664,
  3792. show_battery_oc_protect_level, store_battery_oc_protect_level); /*664 */
  3793. #endif
  3794. #ifdef BATTERY_PERCENT_PROTECT
  3795. /*****************************************************************************
  3796. * battery percent protect UT
  3797. ******************************************************************************/
  3798. static ssize_t show_battery_percent_ut(struct device *dev, struct device_attribute *attr, char *buf)
  3799. {
  3800. /*show_battery_percent_protect_ut */
  3801. PMICLOG("[show_battery_percent_protect_ut] g_battery_percent_level=%d\n",
  3802. g_battery_percent_level);
  3803. return sprintf(buf, "%u\n", g_battery_percent_level);
  3804. }
  3805. static ssize_t store_battery_percent_ut(struct device *dev, struct device_attribute *attr,
  3806. const char *buf, size_t size)
  3807. {
  3808. int ret = 0;
  3809. char *pvalue = NULL;
  3810. unsigned int val = 0;
  3811. /*store_battery_percent_protect_ut */
  3812. pr_err("[store_battery_percent_protect_ut]\n");
  3813. if (buf != NULL && size != 0) {
  3814. pr_err("[store_battery_percent_protect_ut] buf is %s and size is %zu\n", buf, size);
  3815. /*val = simple_strtoul(buf, &pvalue, 16); */
  3816. pvalue = (char *)buf;
  3817. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3818. if (val <= 1) {
  3819. pr_err("[store_battery_percent_protect_ut] your input is %d\n", val);
  3820. exec_battery_percent_callback(val);
  3821. } else {
  3822. pr_err("[store_battery_percent_protect_ut] wrong number (%d)\n", val);
  3823. }
  3824. }
  3825. return size;
  3826. }
  3827. static DEVICE_ATTR(battery_percent_protect_ut, 0664, show_battery_percent_ut, store_battery_percent_ut); /*664 */
  3828. /*
  3829. * battery percent protect stop
  3830. ******************************************************************************/
  3831. static ssize_t show_battery_percent_stop(struct device *dev, struct device_attribute *attr,
  3832. char *buf)
  3833. {
  3834. /*show_battery_percent_protect_stop */
  3835. PMICLOG("[show_battery_percent_protect_stop] g_battery_percent_stop=%d\n",
  3836. g_battery_percent_stop);
  3837. return sprintf(buf, "%u\n", g_battery_percent_stop);
  3838. }
  3839. static ssize_t store_battery_percent_stop(struct device *dev, struct device_attribute *attr,
  3840. const char *buf, size_t size)
  3841. {
  3842. int ret = 0;
  3843. char *pvalue = NULL;
  3844. unsigned int val = 0;
  3845. /*store_battery_percent_protect_stop */
  3846. pr_err("[store_battery_percent_protect_stop]\n");
  3847. if (buf != NULL && size != 0) {
  3848. pr_err("[store_battery_percent_protect_stop] buf is %s and size is %zu\n", buf,
  3849. size);
  3850. /*val = simple_strtoul(buf, &pvalue, 16); */
  3851. pvalue = (char *)buf;
  3852. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3853. if ((val != 0) && (val != 1))
  3854. val = 0;
  3855. g_battery_percent_stop = val;
  3856. pr_err("[store_battery_percent_protect_stop] g_battery_percent_stop=%d\n",
  3857. g_battery_percent_stop);
  3858. }
  3859. return size;
  3860. }
  3861. static DEVICE_ATTR(battery_percent_protect_stop, 0664, show_battery_percent_stop, store_battery_percent_stop); /*664 */
  3862. /*
  3863. * battery percent protect level
  3864. ******************************************************************************/
  3865. static ssize_t show_battery_percent_level(struct device *dev, struct device_attribute *attr,
  3866. char *buf)
  3867. {
  3868. /*show_battery_percent_protect_level */
  3869. PMICLOG("[show_battery_percent_protect_level] g_battery_percent_level=%d\n",
  3870. g_battery_percent_level);
  3871. return sprintf(buf, "%u\n", g_battery_percent_level);
  3872. }
  3873. static ssize_t store_battery_percent_level(struct device *dev,
  3874. struct device_attribute *attr, const char *buf,
  3875. size_t size)
  3876. {
  3877. /*store_battery_percent_protect_level */
  3878. PMICLOG("[store_battery_percent_protect_level] g_battery_percent_level=%d\n",
  3879. g_battery_percent_level);
  3880. return size;
  3881. }
  3882. static DEVICE_ATTR(battery_percent_protect_level, 0664,
  3883. show_battery_percent_level, store_battery_percent_level); /*664 */
  3884. #endif
  3885. #ifdef DLPT_FEATURE_SUPPORT
  3886. /*****************************************************************************
  3887. * DLPT UT
  3888. ******************************************************************************/
  3889. static ssize_t show_dlpt_ut(struct device *dev, struct device_attribute *attr, char *buf)
  3890. {
  3891. PMICLOG("[show_dlpt_ut] g_dlpt_val=%d\n", g_dlpt_val);
  3892. return sprintf(buf, "%u\n", g_dlpt_val);
  3893. }
  3894. static ssize_t store_dlpt_ut(struct device *dev, struct device_attribute *attr, const char *buf,
  3895. size_t size)
  3896. {
  3897. char *pvalue = NULL;
  3898. unsigned int val = 0;
  3899. int ret = 0;
  3900. pr_err("[store_dlpt_ut]\n");
  3901. if (buf != NULL && size != 0) {
  3902. pr_err("[store_dlpt_ut] buf is %s and size is %zu\n", buf, size);
  3903. /*val = simple_strtoul(buf, &pvalue, 10); */
  3904. pvalue = (char *)buf;
  3905. ret = kstrtou32(pvalue, 10, (unsigned int *)&val);
  3906. pr_err("[store_dlpt_ut] your input is %d\n", val);
  3907. exec_dlpt_callback(val);
  3908. }
  3909. return size;
  3910. }
  3911. static DEVICE_ATTR(dlpt_ut, 0664, show_dlpt_ut, store_dlpt_ut); /*664 */
  3912. /*****************************************************************************
  3913. * DLPT stop
  3914. ******************************************************************************/
  3915. static ssize_t show_dlpt_stop(struct device *dev, struct device_attribute *attr, char *buf)
  3916. {
  3917. PMICLOG("[show_dlpt_stop] g_dlpt_stop=%d\n", g_dlpt_stop);
  3918. return sprintf(buf, "%u\n", g_dlpt_stop);
  3919. }
  3920. static ssize_t store_dlpt_stop(struct device *dev, struct device_attribute *attr, const char *buf,
  3921. size_t size)
  3922. {
  3923. char *pvalue = NULL;
  3924. unsigned int val = 0;
  3925. int ret = 0;
  3926. pr_err("[store_dlpt_stop]\n");
  3927. if (buf != NULL && size != 0) {
  3928. pr_err("[store_dlpt_stop] buf is %s and size is %zu\n", buf, size);
  3929. /*val = simple_strtoul(buf, &pvalue, 16); */
  3930. pvalue = (char *)buf;
  3931. ret = kstrtou32(pvalue, 16, (unsigned int *)&val);
  3932. if ((val != 0) && (val != 1))
  3933. val = 0;
  3934. g_dlpt_stop = val;
  3935. pr_err("[store_dlpt_stop] g_dlpt_stop=%d\n", g_dlpt_stop);
  3936. }
  3937. return size;
  3938. }
  3939. static DEVICE_ATTR(dlpt_stop, 0664, show_dlpt_stop, store_dlpt_stop); /*664 */
  3940. /*****************************************************************************
  3941. * DLPT level
  3942. ******************************************************************************/
  3943. static ssize_t show_dlpt_level(struct device *dev, struct device_attribute *attr, char *buf)
  3944. {
  3945. PMICLOG("[show_dlpt_level] g_dlpt_val=%d\n", g_dlpt_val);
  3946. return sprintf(buf, "%u\n", g_dlpt_val);
  3947. }
  3948. static ssize_t store_dlpt_level(struct device *dev, struct device_attribute *attr, const char *buf,
  3949. size_t size)
  3950. {
  3951. PMICLOG("[store_dlpt_level] g_dlpt_val=%d\n", g_dlpt_val);
  3952. return size;
  3953. }
  3954. static DEVICE_ATTR(dlpt_level, 0664, show_dlpt_level, store_dlpt_level); /*664 */
  3955. #endif
  3956. /*****************************************************************************
  3957. * system function
  3958. ******************************************************************************/
  3959. #ifdef DLPT_FEATURE_SUPPORT
  3960. static unsigned long pmic_node;
  3961. static int fb_early_init_dt_get_chosen(unsigned long node, const char *uname, int depth, void *data)
  3962. {
  3963. if (depth != 1 || (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
  3964. return 0;
  3965. pmic_node = node;
  3966. return 1;
  3967. }
  3968. #endif /*end of #ifdef DLPT_FEATURE_SUPPORT */
  3969. static int pmic_mt_probe(struct platform_device *dev)
  3970. {
  3971. int ret_device_file = 0, i;
  3972. #ifdef DLPT_FEATURE_SUPPORT
  3973. const int *pimix;
  3974. int len = 0;
  3975. #endif
  3976. #if defined(CONFIG_MTK_SMART_BATTERY)
  3977. struct device_node *np;
  3978. u32 val;
  3979. /* check customer setting */
  3980. np = of_find_compatible_node(NULL, NULL, "mediatek,bat_meter");
  3981. if (np == NULL)
  3982. PMICLOG("[PMIC]pmic_mt_probe get bat_meter node failed\n");
  3983. if (of_property_read_u32(np, "car_tune_value", &val) == 0) {
  3984. batt_meter_cust_data.car_tune_value = (int)val;
  3985. PMICLOG("Get car_tune_value from DT: %d\n", batt_meter_cust_data.car_tune_value);
  3986. } else {
  3987. batt_meter_cust_data.car_tune_value = CAR_TUNE_VALUE;
  3988. PMICLOG("Get car_tune_value from cust header\n");
  3989. }
  3990. #endif
  3991. /*--- initailize pmic_suspend_state ---*/
  3992. pmic_suspend_state = false;
  3993. #ifdef DLPT_FEATURE_SUPPORT
  3994. pimix = NULL;
  3995. if (of_scan_flat_dt(fb_early_init_dt_get_chosen, NULL) > 0)
  3996. pimix = of_get_flat_dt_prop(pmic_node, "atag,imix_r", &len);
  3997. if (pimix == NULL) {
  3998. pr_err(" pimix==NULL len=%d\n", len);
  3999. } else {
  4000. pr_err(" pimix=%d\n", *pimix);
  4001. ptim_rac_val_avg = *pimix;
  4002. }
  4003. PMICLOG("******** MT pmic driver probe!! ********%d\n", ptim_rac_val_avg);
  4004. pr_debug("[PMIC]pmic_mt_probe %s %s\n", dev->name, dev->id_entry->name);
  4005. #endif /* #ifdef DLPT_FEATURE_SUPPORT */
  4006. /*get PMIC CID */
  4007. pr_debug
  4008. ("PMIC CID=0x%x PowerGoodStatus = 0x%x OCStatus = 0x%x ThermalStatus = 0x%x rsvStatus = 0x%x\n",
  4009. pmic_get_register_value(PMIC_SWCID), upmu_get_reg_value(0x21c),
  4010. upmu_get_reg_value(0x214), upmu_get_reg_value(0x21e), upmu_get_reg_value(0x2a6));
  4011. upmu_set_reg_value(0x2a6, 0xff);
  4012. #if defined(CONFIG_ARCH_MT6753)
  4013. PMICLOG("[PMIC_INIT_SETTING_V1] delay to MT6311 init\n");
  4014. #else
  4015. PMIC_INIT_SETTING_V1();
  4016. PMICLOG("[PMIC_INIT_SETTING_V1] Done\n");
  4017. #endif
  4018. #if !defined CONFIG_MTK_LEGACY
  4019. /* replace by DTS*/
  4020. #else
  4021. PMIC_CUSTOM_SETTING_V1();
  4022. PMICLOG("[PMIC_CUSTOM_SETTING_V1] Done\n");
  4023. #endif /*End of #if !defined CONFIG_MTK_LEGACY */
  4024. /*#if defined(CONFIG_MTK_FPGA)*/
  4025. #if 0
  4026. PMICLOG("[PMIC_EINT_SETTING] disable when CONFIG_MTK_FPGA\n");
  4027. #else
  4028. /*PMIC Interrupt Service */
  4029. PMIC_EINT_SETTING();
  4030. PMICLOG("[PMIC_EINT_SETTING] Done\n");
  4031. pmic_thread_handle = kthread_create(pmic_thread_kthread, (void *)NULL, "pmic_thread");
  4032. if (IS_ERR(pmic_thread_handle)) {
  4033. pmic_thread_handle = NULL;
  4034. PMICLOG("[pmic_thread_kthread_mt6325] creation fails\n");
  4035. } else {
  4036. wake_up_process(pmic_thread_handle);
  4037. PMICLOG("[pmic_thread_kthread_mt6325] kthread_create Done\n");
  4038. }
  4039. #endif
  4040. mtk_regulator_init(dev);
  4041. #ifdef LOW_BATTERY_PROTECT
  4042. low_battery_protect_init();
  4043. #else
  4044. pr_err("[PMIC] no define LOW_BATTERY_PROTECT\n");
  4045. #endif
  4046. #ifdef BATTERY_OC_PROTECT
  4047. battery_oc_protect_init();
  4048. #else
  4049. pr_err("[PMIC] no define BATTERY_OC_PROTECT\n");
  4050. #endif
  4051. #ifdef BATTERY_PERCENT_PROTECT
  4052. bat_percent_notify_init();
  4053. #else
  4054. pr_err("[PMIC] no define BATTERY_PERCENT_PROTECT\n");
  4055. #endif
  4056. #ifdef DLPT_FEATURE_SUPPORT
  4057. dlpt_notify_init();
  4058. #else
  4059. pr_err("[PMIC] no define DLPT_FEATURE_SUPPORT\n");
  4060. #endif
  4061. #if 1
  4062. pmic_set_register_value(PMIC_AUXADC_CK_AON, 1);
  4063. pmic_set_register_value(PMIC_RG_CLKSQ_EN_AUX_AP_MODE, 1);
  4064. PMICLOG("[PMIC] auxadc 26M test : Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4065. MT6328_AUXADC_CON0, upmu_get_reg_value(MT6328_AUXADC_CON0),
  4066. MT6328_TOP_CLKSQ, upmu_get_reg_value(MT6328_TOP_CLKSQ)
  4067. );
  4068. #endif
  4069. pmic_debug_init();
  4070. PMICLOG("[PMIC] pmic_debug_init : done.\n");
  4071. pmic_ftm_init();
  4072. /*EM BUCK voltage & Status */
  4073. for (i = 0; i < ARRAY_SIZE(mtk_bucks); i++) {
  4074. /*PMICLOG("[PMIC] register buck id=%d\n",i); */
  4075. ret_device_file = device_create_file(&(dev->dev), &mtk_bucks[i].en_att);
  4076. ret_device_file = device_create_file(&(dev->dev), &mtk_bucks[i].voltage_att);
  4077. }
  4078. /*EM ldo voltage & Status */
  4079. for (i = 0; i < ARRAY_SIZE(mtk_ldos); i++) {
  4080. /*PMICLOG("[PMIC] register ldo id=%d\n",i); */
  4081. ret_device_file = device_create_file(&(dev->dev), &mtk_ldos[i].en_att);
  4082. ret_device_file = device_create_file(&(dev->dev), &mtk_ldos[i].voltage_att);
  4083. }
  4084. ret_device_file = device_create_file(&(dev->dev), &dev_attr_pmic_access);
  4085. ret_device_file = device_create_file(&(dev->dev), &dev_attr_pmic_dvt);
  4086. #ifdef LOW_BATTERY_PROTECT
  4087. ret_device_file = device_create_file(&(dev->dev), &dev_attr_low_battery_protect_ut);
  4088. ret_device_file = device_create_file(&(dev->dev), &dev_attr_low_battery_protect_stop);
  4089. ret_device_file = device_create_file(&(dev->dev), &dev_attr_low_battery_protect_level);
  4090. #endif
  4091. #ifdef BATTERY_OC_PROTECT
  4092. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_oc_protect_ut);
  4093. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_oc_protect_stop);
  4094. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_oc_protect_level);
  4095. #endif
  4096. #ifdef BATTERY_PERCENT_PROTECT
  4097. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_percent_protect_ut);
  4098. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_percent_protect_stop);
  4099. ret_device_file = device_create_file(&(dev->dev), &dev_attr_battery_percent_protect_level);
  4100. #endif
  4101. #ifdef DLPT_FEATURE_SUPPORT
  4102. ret_device_file = device_create_file(&(dev->dev), &dev_attr_dlpt_ut);
  4103. ret_device_file = device_create_file(&(dev->dev), &dev_attr_dlpt_stop);
  4104. ret_device_file = device_create_file(&(dev->dev), &dev_attr_dlpt_level);
  4105. #endif
  4106. PMICLOG("[PMIC] device_create_file for EM : done.\n");
  4107. /*pwrkey_sw_workaround_init(); */
  4108. return 0;
  4109. }
  4110. static int pmic_mt_remove(struct platform_device *dev)
  4111. {
  4112. PMICLOG("******** MT pmic driver remove!! ********\n");
  4113. return 0;
  4114. }
  4115. static void pmic_mt_shutdown(struct platform_device *dev)
  4116. {
  4117. PMICLOG("******** MT pmic driver shutdown!! ********\n");
  4118. pmic_md_power_on();
  4119. }
  4120. static int pmic_mt_suspend(struct platform_device *dev, pm_message_t state)
  4121. {
  4122. pmic_suspend_state = true;
  4123. PMICLOG("******** MT pmic driver suspend!! ********\n");
  4124. #ifdef LOW_BATTERY_PROTECT
  4125. lbat_min_en_setting(0);
  4126. lbat_max_en_setting(0);
  4127. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4128. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  4129. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  4130. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  4131. );
  4132. #endif
  4133. #ifdef BATTERY_OC_PROTECT
  4134. bat_oc_h_en_setting(0);
  4135. bat_oc_l_en_setting(0);
  4136. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4137. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  4138. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  4139. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  4140. );
  4141. #endif
  4142. return 0;
  4143. }
  4144. static int pmic_mt_resume(struct platform_device *dev)
  4145. {
  4146. pmic_suspend_state = false;
  4147. PMICLOG("******** MT pmic driver resume!! ********\n");
  4148. #ifdef LOW_BATTERY_PROTECT
  4149. lbat_min_en_setting(0);
  4150. lbat_max_en_setting(0);
  4151. mdelay(1);
  4152. if (g_low_battery_level == 1) {
  4153. lbat_min_en_setting(1);
  4154. lbat_max_en_setting(1);
  4155. } else if (g_low_battery_level == 2) {
  4156. /*lbat_min_en_setting(0); */
  4157. lbat_max_en_setting(1);
  4158. } else { /*0 */
  4159. lbat_min_en_setting(1);
  4160. /*lbat_max_en_setting(0); */
  4161. }
  4162. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4163. MT6328_AUXADC_LBAT3, upmu_get_reg_value(MT6328_AUXADC_LBAT3),
  4164. MT6328_AUXADC_LBAT4, upmu_get_reg_value(MT6328_AUXADC_LBAT4),
  4165. MT6328_INT_CON0, upmu_get_reg_value(MT6328_INT_CON0)
  4166. );
  4167. #endif
  4168. #ifdef BATTERY_OC_PROTECT
  4169. bat_oc_h_en_setting(0);
  4170. bat_oc_l_en_setting(0);
  4171. mdelay(1);
  4172. if (g_battery_oc_level == 1)
  4173. bat_oc_h_en_setting(1);
  4174. else
  4175. bat_oc_l_en_setting(1);
  4176. PMICLOG("Reg[0x%x]=0x%x, Reg[0x%x]=0x%x, Reg[0x%x]=0x%x\n",
  4177. MT6328_FGADC_CON23, upmu_get_reg_value(MT6328_FGADC_CON23),
  4178. MT6328_FGADC_CON24, upmu_get_reg_value(MT6328_FGADC_CON24),
  4179. MT6328_INT_CON2, upmu_get_reg_value(MT6328_INT_CON2)
  4180. );
  4181. #endif
  4182. return 0;
  4183. }
  4184. struct platform_device pmic_mt_device = {
  4185. .name = "mt-pmic",
  4186. .id = -1,
  4187. };
  4188. static struct platform_driver pmic_mt_driver = {
  4189. .probe = pmic_mt_probe,
  4190. .remove = pmic_mt_remove,
  4191. .shutdown = pmic_mt_shutdown,
  4192. /*#ifdef CONFIG_PM */
  4193. .suspend = pmic_mt_suspend,
  4194. .resume = pmic_mt_resume,
  4195. /*#endif */
  4196. .driver = {
  4197. .name = "mt-pmic",
  4198. },
  4199. };
  4200. static DEFINE_MUTEX(pmic_efuse_lock_mutex);
  4201. unsigned int pmic_Read_Efuse_HPOffset(int i)
  4202. {
  4203. unsigned int ret = 0;
  4204. unsigned int reg_val = 0;
  4205. unsigned int efusevalue;
  4206. pr_debug("pmic_Read_Efuse_HPOffset(+)\n");
  4207. mutex_lock(&pmic_efuse_lock_mutex);
  4208. /*1. enable efuse ctrl engine clock */
  4209. ret = pmic_config_interface(0x027C, 0x0040, 0xFFFF, 0);
  4210. ret = pmic_config_interface(0x0252, 0x0004, 0xFFFF, 0);
  4211. /*2. */
  4212. ret = pmic_config_interface(0x0C16, 0x1, 0x1, 0);
  4213. /*3. set row to read */
  4214. ret = pmic_config_interface(0x0C00, i, 0x1F, 1);
  4215. /*4. Toggle */
  4216. udelay(100);
  4217. ret = pmic_read_interface(0x0C10, &reg_val, 0x1, 0);
  4218. if (reg_val == 0)
  4219. ret = pmic_config_interface(0x0C10, 1, 0x1, 0);
  4220. else
  4221. ret = pmic_config_interface(0x0C10, 0, 0x1, 0);
  4222. /*5. polling Reg[0x61A] */
  4223. udelay(300);
  4224. reg_val = 1;
  4225. while (reg_val == 1) {
  4226. ret = pmic_read_interface(0x0C1A, &reg_val, 0x1, 0);
  4227. pr_debug("pmic_Read_Efuse_HPOffset polling 0x61A=0x%x\n", reg_val);
  4228. }
  4229. udelay(1000); /*Need to delay at least 1ms for 0x61A and than can read 0xC18 */
  4230. /*6. read data */
  4231. efusevalue = upmu_get_reg_value(0x0C18);
  4232. pr_debug("HPoffset : efuse=0x%x\n", efusevalue);
  4233. /*7. Disable efuse ctrl engine clock */
  4234. ret = pmic_config_interface(0x0250, 0x0004, 0xFFFF, 0);
  4235. ret = pmic_config_interface(0x027A, 0x0040, 0xFFFF, 0);
  4236. mutex_unlock(&pmic_efuse_lock_mutex);
  4237. return efusevalue;
  4238. }
  4239. /*****************************************************************************
  4240. * PMIC mudule init/exit
  4241. ******************************************************************************/
  4242. static int __init pmic_mt_init(void)
  4243. {
  4244. int ret;
  4245. #ifdef BATTERY_PERCENT_PROTECT
  4246. #if !defined CONFIG_HAS_WAKELOCKS
  4247. wakeup_source_init(&pmicThread_lock, "pmicThread_lock_mt6328 wakelock");
  4248. wakeup_source_init(&bat_percent_notify_lock, "bat_percent_notify_lock wakelock");
  4249. #else
  4250. wake_lock_init(&pmicThread_lock, WAKE_LOCK_SUSPEND, "pmicThread_lock_mt6328 wakelock");
  4251. wake_lock_init(&bat_percent_notify_lock, WAKE_LOCK_SUSPEND,
  4252. "bat_percent_notify_lock wakelock");
  4253. #endif
  4254. #endif /* #ifdef BATTERY_PERCENT_PROTECT */
  4255. #ifdef DLPT_FEATURE_SUPPORT
  4256. #if !defined CONFIG_HAS_WAKELOCKS
  4257. wakeup_source_init(&dlpt_notify_lock, "dlpt_notify_lock wakelock");
  4258. #else
  4259. wake_lock_init(&dlpt_notify_lock, WAKE_LOCK_SUSPEND, "dlpt_notify_lock wakelock");
  4260. #endif
  4261. #endif /*#ifdef DLPT_FEATURE_SUPPORT */
  4262. #if !defined CONFIG_MTK_LEGACY
  4263. #ifdef CONFIG_OF
  4264. PMICLOG("pmic_regulator_init_OF\n");
  4265. /* PMIC device driver register */
  4266. ret = platform_device_register(&pmic_mt_device);
  4267. if (ret) {
  4268. PMICLOG("****[pmic_mt_init] Unable to device register(%d)\n", ret);
  4269. return ret;
  4270. }
  4271. ret = platform_driver_register(&pmic_mt_driver);
  4272. if (ret) {
  4273. PMICLOG("****[pmic_mt_init] Unable to register driver (%d)\n", ret);
  4274. return ret;
  4275. }
  4276. ret = platform_driver_register(&mt_pmic_driver);
  4277. if (ret) {
  4278. PMICLOG("****[pmic_mt_init] Unable to register driver by DT(%d)\n", ret);
  4279. return ret;
  4280. }
  4281. #endif /* End of #ifdef CONFIG_OF */
  4282. #else
  4283. PMICLOG("pmic_regulator_init\n");
  4284. /* PMIC device driver register */
  4285. ret = platform_device_register(&pmic_mt_device);
  4286. if (ret) {
  4287. PMICLOG("****[pmic_mt_init] Unable to device register(%d)\n", ret);
  4288. return ret;
  4289. }
  4290. ret = platform_driver_register(&pmic_mt_driver);
  4291. if (ret) {
  4292. PMICLOG("****[pmic_mt_init] Unable to register driver (%d)\n", ret);
  4293. return ret;
  4294. }
  4295. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  4296. pmic_auxadc_init();
  4297. pr_debug("****[pmic_mt_init] Initialization : DONE !!\n");
  4298. return 0;
  4299. }
  4300. static void __exit pmic_mt_exit(void)
  4301. {
  4302. #if !defined CONFIG_MTK_LEGACY
  4303. #ifdef CONFIG_OF
  4304. platform_driver_unregister(&mt_pmic_driver);
  4305. #endif
  4306. #endif /* End of #if !defined CONFIG_MTK_LEGACY */
  4307. }
  4308. fs_initcall(pmic_mt_init);
  4309. /*module_init(pmic_mt_init);*/
  4310. module_exit(pmic_mt_exit);
  4311. MODULE_AUTHOR("Argus Lin");
  4312. MODULE_DESCRIPTION("MT PMIC Device Driver");
  4313. MODULE_LICENSE("GPL");