pm8xxx-regulator.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346
  1. /*
  2. * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/string.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/bitops.h>
  21. #include <linux/mfd/pm8xxx/core.h>
  22. #include <linux/mfd/pm8xxx/regulator.h>
  23. /* Debug Flag Definitions */
  24. enum {
  25. PM8XXX_VREG_DEBUG_REQUEST = BIT(0),
  26. PM8XXX_VREG_DEBUG_DUPLICATE = BIT(1),
  27. PM8XXX_VREG_DEBUG_INIT = BIT(2),
  28. PM8XXX_VREG_DEBUG_WRITES = BIT(3), /* SSBI writes */
  29. };
  30. static int pm8xxx_vreg_debug_mask;
  31. module_param_named(
  32. debug_mask, pm8xxx_vreg_debug_mask, int, S_IRUSR | S_IWUSR
  33. );
  34. /* Common Masks */
  35. #define REGULATOR_ENABLE_MASK 0x80
  36. #define REGULATOR_ENABLE 0x80
  37. #define REGULATOR_DISABLE 0x00
  38. #define REGULATOR_BANK_MASK 0xF0
  39. #define REGULATOR_BANK_SEL(n) ((n) << 4)
  40. #define REGULATOR_BANK_WRITE 0x80
  41. #define LDO_TEST_BANKS 7
  42. #define NLDO1200_TEST_BANKS 5
  43. #define SMPS_TEST_BANKS 8
  44. /*
  45. * This voltage in uV is returned by get_voltage functions when there is no way
  46. * to determine the current voltage level. It is needed because the regulator
  47. * framework treats a 0 uV voltage as an error.
  48. */
  49. #define VOLTAGE_UNKNOWN 1
  50. /* LDO masks and values */
  51. /* CTRL register */
  52. #define LDO_ENABLE_MASK 0x80
  53. #define LDO_DISABLE 0x00
  54. #define LDO_ENABLE 0x80
  55. #define LDO_PULL_DOWN_ENABLE_MASK 0x40
  56. #define LDO_PULL_DOWN_ENABLE 0x40
  57. #define LDO_CTRL_PM_MASK 0x20
  58. #define LDO_CTRL_PM_HPM 0x00
  59. #define LDO_CTRL_PM_LPM 0x20
  60. #define LDO_CTRL_VPROG_MASK 0x1F
  61. /* TEST register bank 0 */
  62. #define LDO_TEST_LPM_MASK 0x04
  63. #define LDO_TEST_LPM_SEL_CTRL 0x00
  64. #define LDO_TEST_LPM_SEL_TCXO 0x04
  65. /* TEST register bank 2 */
  66. #define LDO_TEST_VPROG_UPDATE_MASK 0x08
  67. #define LDO_TEST_RANGE_SEL_MASK 0x04
  68. #define LDO_TEST_FINE_STEP_MASK 0x02
  69. #define LDO_TEST_FINE_STEP_SHIFT 1
  70. /* TEST register bank 4 */
  71. #define LDO_TEST_RANGE_EXT_MASK 0x01
  72. /* TEST register bank 5 */
  73. #define LDO_TEST_PIN_CTRL_MASK 0x0F
  74. #define LDO_TEST_PIN_CTRL_EN3 0x08
  75. #define LDO_TEST_PIN_CTRL_EN2 0x04
  76. #define LDO_TEST_PIN_CTRL_EN1 0x02
  77. #define LDO_TEST_PIN_CTRL_EN0 0x01
  78. /* TEST register bank 6 */
  79. #define LDO_TEST_PIN_CTRL_LPM_MASK 0x0F
  80. /*
  81. * If a given voltage could be output by two ranges, then the preferred one must
  82. * be determined by the range limits. Specified voltage ranges should must
  83. * not overlap.
  84. *
  85. * Allowable voltage ranges:
  86. */
  87. #define PLDO_LOW_UV_MIN 750000
  88. #define PLDO_LOW_UV_MAX 1487500
  89. #define PLDO_LOW_UV_FINE_STEP 12500
  90. #define PLDO_NORM_UV_MIN 1500000
  91. #define PLDO_NORM_UV_MAX 3075000
  92. #define PLDO_NORM_UV_FINE_STEP 25000
  93. #define PLDO_HIGH_UV_MIN 1750000
  94. #define PLDO_HIGH_UV_SET_POINT_MIN 3100000
  95. #define PLDO_HIGH_UV_MAX 4900000
  96. #define PLDO_HIGH_UV_FINE_STEP 50000
  97. #define PLDO_LOW_SET_POINTS ((PLDO_LOW_UV_MAX - PLDO_LOW_UV_MIN) \
  98. / PLDO_LOW_UV_FINE_STEP + 1)
  99. #define PLDO_NORM_SET_POINTS ((PLDO_NORM_UV_MAX - PLDO_NORM_UV_MIN) \
  100. / PLDO_NORM_UV_FINE_STEP + 1)
  101. #define PLDO_HIGH_SET_POINTS ((PLDO_HIGH_UV_MAX \
  102. - PLDO_HIGH_UV_SET_POINT_MIN) \
  103. / PLDO_HIGH_UV_FINE_STEP + 1)
  104. #define PLDO_SET_POINTS (PLDO_LOW_SET_POINTS \
  105. + PLDO_NORM_SET_POINTS \
  106. + PLDO_HIGH_SET_POINTS)
  107. #define NLDO_UV_MIN 750000
  108. #define NLDO_UV_MAX 1537500
  109. #define NLDO_UV_FINE_STEP 12500
  110. #define NLDO_SET_POINTS ((NLDO_UV_MAX - NLDO_UV_MIN) \
  111. / NLDO_UV_FINE_STEP + 1)
  112. /* NLDO1200 masks and values */
  113. /* CTRL register */
  114. #define NLDO1200_ENABLE_MASK 0x80
  115. #define NLDO1200_DISABLE 0x00
  116. #define NLDO1200_ENABLE 0x80
  117. /* Legacy mode */
  118. #define NLDO1200_LEGACY_PM_MASK 0x20
  119. #define NLDO1200_LEGACY_PM_HPM 0x00
  120. #define NLDO1200_LEGACY_PM_LPM 0x20
  121. /* Advanced mode */
  122. #define NLDO1200_CTRL_RANGE_MASK 0x40
  123. #define NLDO1200_CTRL_RANGE_HIGH 0x00
  124. #define NLDO1200_CTRL_RANGE_LOW 0x40
  125. #define NLDO1200_CTRL_VPROG_MASK 0x3F
  126. #define NLDO1200_LOW_UV_MIN 375000
  127. #define NLDO1200_LOW_UV_MAX 743750
  128. #define NLDO1200_LOW_UV_STEP 6250
  129. #define NLDO1200_HIGH_UV_MIN 750000
  130. #define NLDO1200_HIGH_UV_MAX 1537500
  131. #define NLDO1200_HIGH_UV_STEP 12500
  132. #define NLDO1200_LOW_SET_POINTS ((NLDO1200_LOW_UV_MAX \
  133. - NLDO1200_LOW_UV_MIN) \
  134. / NLDO1200_LOW_UV_STEP + 1)
  135. #define NLDO1200_HIGH_SET_POINTS ((NLDO1200_HIGH_UV_MAX \
  136. - NLDO1200_HIGH_UV_MIN) \
  137. / NLDO1200_HIGH_UV_STEP + 1)
  138. #define NLDO1200_SET_POINTS (NLDO1200_LOW_SET_POINTS \
  139. + NLDO1200_HIGH_SET_POINTS)
  140. /* TEST register bank 0 */
  141. #define NLDO1200_TEST_LPM_MASK 0x04
  142. #define NLDO1200_TEST_LPM_SEL_CTRL 0x00
  143. #define NLDO1200_TEST_LPM_SEL_TCXO 0x04
  144. /* TEST register bank 1 */
  145. #define NLDO1200_PULL_DOWN_ENABLE_MASK 0x02
  146. #define NLDO1200_PULL_DOWN_ENABLE 0x02
  147. /* TEST register bank 2 */
  148. #define NLDO1200_ADVANCED_MODE_MASK 0x08
  149. #define NLDO1200_ADVANCED_MODE 0x00
  150. #define NLDO1200_LEGACY_MODE 0x08
  151. /* Advanced mode power mode control */
  152. #define NLDO1200_ADVANCED_PM_MASK 0x02
  153. #define NLDO1200_ADVANCED_PM_HPM 0x00
  154. #define NLDO1200_ADVANCED_PM_LPM 0x02
  155. #define NLDO1200_IN_ADVANCED_MODE(vreg) \
  156. ((vreg->test_reg[2] & NLDO1200_ADVANCED_MODE_MASK) \
  157. == NLDO1200_ADVANCED_MODE)
  158. /* SMPS masks and values */
  159. /* CTRL register */
  160. /* Legacy mode */
  161. #define SMPS_LEGACY_ENABLE_MASK 0x80
  162. #define SMPS_LEGACY_DISABLE 0x00
  163. #define SMPS_LEGACY_ENABLE 0x80
  164. #define SMPS_LEGACY_PULL_DOWN_ENABLE 0x40
  165. #define SMPS_LEGACY_VREF_SEL_MASK 0x20
  166. #define SMPS_LEGACY_VPROG_MASK 0x1F
  167. /* Advanced mode */
  168. #define SMPS_ADVANCED_BAND_MASK 0xC0
  169. #define SMPS_ADVANCED_BAND_OFF 0x00
  170. #define SMPS_ADVANCED_BAND_1 0x40
  171. #define SMPS_ADVANCED_BAND_2 0x80
  172. #define SMPS_ADVANCED_BAND_3 0xC0
  173. #define SMPS_ADVANCED_VPROG_MASK 0x3F
  174. /* Legacy mode voltage ranges */
  175. #define SMPS_MODE3_UV_MIN 375000
  176. #define SMPS_MODE3_UV_MAX 725000
  177. #define SMPS_MODE3_UV_STEP 25000
  178. #define SMPS_MODE2_UV_MIN 750000
  179. #define SMPS_MODE2_UV_MAX 1475000
  180. #define SMPS_MODE2_UV_STEP 25000
  181. #define SMPS_MODE1_UV_MIN 1500000
  182. #define SMPS_MODE1_UV_MAX 3050000
  183. #define SMPS_MODE1_UV_STEP 50000
  184. #define SMPS_MODE3_SET_POINTS ((SMPS_MODE3_UV_MAX \
  185. - SMPS_MODE3_UV_MIN) \
  186. / SMPS_MODE3_UV_STEP + 1)
  187. #define SMPS_MODE2_SET_POINTS ((SMPS_MODE2_UV_MAX \
  188. - SMPS_MODE2_UV_MIN) \
  189. / SMPS_MODE2_UV_STEP + 1)
  190. #define SMPS_MODE1_SET_POINTS ((SMPS_MODE1_UV_MAX \
  191. - SMPS_MODE1_UV_MIN) \
  192. / SMPS_MODE1_UV_STEP + 1)
  193. #define SMPS_LEGACY_SET_POINTS (SMPS_MODE3_SET_POINTS \
  194. + SMPS_MODE2_SET_POINTS \
  195. + SMPS_MODE1_SET_POINTS)
  196. /* Advanced mode voltage ranges */
  197. #define SMPS_BAND1_UV_MIN 375000
  198. #define SMPS_BAND1_UV_MAX 737500
  199. #define SMPS_BAND1_UV_STEP 12500
  200. #define SMPS_BAND2_UV_MIN 750000
  201. #define SMPS_BAND2_UV_MAX 1487500
  202. #define SMPS_BAND2_UV_STEP 12500
  203. #define SMPS_BAND3_UV_MIN 1500000
  204. #define SMPS_BAND3_UV_MAX 3075000
  205. #define SMPS_BAND3_UV_STEP 25000
  206. #define SMPS_BAND1_SET_POINTS ((SMPS_BAND1_UV_MAX \
  207. - SMPS_BAND1_UV_MIN) \
  208. / SMPS_BAND1_UV_STEP + 1)
  209. #define SMPS_BAND2_SET_POINTS ((SMPS_BAND2_UV_MAX \
  210. - SMPS_BAND2_UV_MIN) \
  211. / SMPS_BAND2_UV_STEP + 1)
  212. #define SMPS_BAND3_SET_POINTS ((SMPS_BAND3_UV_MAX \
  213. - SMPS_BAND3_UV_MIN) \
  214. / SMPS_BAND3_UV_STEP + 1)
  215. #define SMPS_ADVANCED_SET_POINTS (SMPS_BAND1_SET_POINTS \
  216. + SMPS_BAND2_SET_POINTS \
  217. + SMPS_BAND3_SET_POINTS)
  218. /* Test2 register bank 1 */
  219. #define SMPS_LEGACY_VLOW_SEL_MASK 0x01
  220. /* Test2 register bank 6 */
  221. #define SMPS_ADVANCED_PULL_DOWN_ENABLE 0x08
  222. /* Test2 register bank 7 */
  223. #define SMPS_ADVANCED_MODE_MASK 0x02
  224. #define SMPS_ADVANCED_MODE 0x02
  225. #define SMPS_LEGACY_MODE 0x00
  226. #define SMPS_IN_ADVANCED_MODE(vreg) \
  227. ((vreg->test_reg[7] & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE)
  228. /* BUCK_SLEEP_CNTRL register */
  229. #define SMPS_PIN_CTRL_MASK 0xF0
  230. #define SMPS_PIN_CTRL_EN3 0x80
  231. #define SMPS_PIN_CTRL_EN2 0x40
  232. #define SMPS_PIN_CTRL_EN1 0x20
  233. #define SMPS_PIN_CTRL_EN0 0x10
  234. #define SMPS_PIN_CTRL_LPM_MASK 0x0F
  235. #define SMPS_PIN_CTRL_LPM_EN3 0x08
  236. #define SMPS_PIN_CTRL_LPM_EN2 0x04
  237. #define SMPS_PIN_CTRL_LPM_EN1 0x02
  238. #define SMPS_PIN_CTRL_LPM_EN0 0x01
  239. /* BUCK_CLOCK_CNTRL register */
  240. #define SMPS_CLK_DIVIDE2 0x40
  241. #define SMPS_CLK_CTRL_MASK 0x30
  242. #define SMPS_CLK_CTRL_FOLLOW_TCXO 0x00
  243. #define SMPS_CLK_CTRL_PWM 0x10
  244. #define SMPS_CLK_CTRL_PFM 0x20
  245. /* FTSMPS masks and values */
  246. /* CTRL register */
  247. #define FTSMPS_VCTRL_BAND_MASK 0xC0
  248. #define FTSMPS_VCTRL_BAND_OFF 0x00
  249. #define FTSMPS_VCTRL_BAND_1 0x40
  250. #define FTSMPS_VCTRL_BAND_2 0x80
  251. #define FTSMPS_VCTRL_BAND_3 0xC0
  252. #define FTSMPS_VCTRL_VPROG_MASK 0x3F
  253. #define FTSMPS_BAND1_UV_MIN 350000
  254. #define FTSMPS_BAND1_UV_MAX 650000
  255. /* 3 LSB's of program voltage must be 0 in band 1. */
  256. /* Logical step size */
  257. #define FTSMPS_BAND1_UV_LOG_STEP 50000
  258. /* Physical step size */
  259. #define FTSMPS_BAND1_UV_PHYS_STEP 6250
  260. #define FTSMPS_BAND2_UV_MIN 700000
  261. #define FTSMPS_BAND2_UV_MAX 1400000
  262. #define FTSMPS_BAND2_UV_STEP 12500
  263. #define FTSMPS_BAND3_UV_MIN 1400000
  264. #define FTSMPS_BAND3_UV_SET_POINT_MIN 1500000
  265. #define FTSMPS_BAND3_UV_MAX 3300000
  266. #define FTSMPS_BAND3_UV_STEP 50000
  267. #define FTSMPS_BAND1_SET_POINTS ((FTSMPS_BAND1_UV_MAX \
  268. - FTSMPS_BAND1_UV_MIN) \
  269. / FTSMPS_BAND1_UV_LOG_STEP + 1)
  270. #define FTSMPS_BAND2_SET_POINTS ((FTSMPS_BAND2_UV_MAX \
  271. - FTSMPS_BAND2_UV_MIN) \
  272. / FTSMPS_BAND2_UV_STEP + 1)
  273. #define FTSMPS_BAND3_SET_POINTS ((FTSMPS_BAND3_UV_MAX \
  274. - FTSMPS_BAND3_UV_SET_POINT_MIN) \
  275. / FTSMPS_BAND3_UV_STEP + 1)
  276. #define FTSMPS_SET_POINTS (FTSMPS_BAND1_SET_POINTS \
  277. + FTSMPS_BAND2_SET_POINTS \
  278. + FTSMPS_BAND3_SET_POINTS)
  279. /* FTS_CNFG1 register bank 0 */
  280. #define FTSMPS_CNFG1_PM_MASK 0x0C
  281. #define FTSMPS_CNFG1_PM_PWM 0x00
  282. #define FTSMPS_CNFG1_PM_PFM 0x08
  283. /* PWR_CNFG register */
  284. #define FTSMPS_PULL_DOWN_ENABLE_MASK 0x40
  285. #define FTSMPS_PULL_DOWN_ENABLE 0x40
  286. /* VS masks and values */
  287. /* CTRL register */
  288. #define VS_ENABLE_MASK 0x80
  289. #define VS_DISABLE 0x00
  290. #define VS_ENABLE 0x80
  291. #define VS_PULL_DOWN_ENABLE_MASK 0x40
  292. #define VS_PULL_DOWN_DISABLE 0x40
  293. #define VS_PULL_DOWN_ENABLE 0x00
  294. #define VS_MODE_MASK 0x30
  295. #define VS_MODE_NORMAL 0x10
  296. #define VS_MODE_LPM 0x20
  297. #define VS_PIN_CTRL_MASK 0x0F
  298. #define VS_PIN_CTRL_EN0 0x08
  299. #define VS_PIN_CTRL_EN1 0x04
  300. #define VS_PIN_CTRL_EN2 0x02
  301. #define VS_PIN_CTRL_EN3 0x01
  302. /* TEST register */
  303. #define VS_OCP_MASK 0x10
  304. #define VS_OCP_ENABLE 0x00
  305. #define VS_OCP_DISABLE 0x10
  306. /* VS300 masks and values */
  307. /* CTRL register */
  308. #define VS300_CTRL_ENABLE_MASK 0xC0
  309. #define VS300_CTRL_DISABLE 0x00
  310. #define VS300_CTRL_ENABLE 0x40
  311. #define VS300_PULL_DOWN_ENABLE_MASK 0x20
  312. #define VS300_PULL_DOWN_ENABLE 0x20
  313. #define VS300_MODE_MASK 0x18
  314. #define VS300_MODE_NORMAL 0x00
  315. #define VS300_MODE_LPM 0x08
  316. /* NCP masks and values */
  317. /* CTRL register */
  318. #define NCP_ENABLE_MASK 0x80
  319. #define NCP_DISABLE 0x00
  320. #define NCP_ENABLE 0x80
  321. #define NCP_VPROG_MASK 0x1F
  322. #define NCP_UV_MIN 1500000
  323. #define NCP_UV_MAX 3050000
  324. #define NCP_UV_STEP 50000
  325. #define NCP_SET_POINTS ((NCP_UV_MAX - NCP_UV_MIN) \
  326. / NCP_UV_STEP + 1)
  327. /* Boost masks and values */
  328. #define BOOST_ENABLE_MASK 0x80
  329. #define BOOST_DISABLE 0x00
  330. #define BOOST_ENABLE 0x80
  331. #define BOOST_VPROG_MASK 0x1F
  332. #define BOOST_UV_MIN 4000000
  333. #define BOOST_UV_MAX 5550000
  334. #define BOOST_UV_STEP 50000
  335. #define BOOST_SET_POINTS ((BOOST_UV_MAX - BOOST_UV_MIN) \
  336. / BOOST_UV_STEP + 1)
  337. #define vreg_err(vreg, fmt, ...) \
  338. pr_err("%s: " fmt, vreg->rdesc.name, ##__VA_ARGS__)
  339. /* Determines which label to add to the print. */
  340. enum pm8xxx_regulator_action {
  341. PM8XXX_REGULATOR_ACTION_INIT,
  342. PM8XXX_REGULATOR_ACTION_ENABLE,
  343. PM8XXX_REGULATOR_ACTION_DISABLE,
  344. PM8XXX_REGULATOR_ACTION_VOLTAGE,
  345. PM8XXX_REGULATOR_ACTION_MODE,
  346. PM8XXX_REGULATOR_ACTION_PIN_CTRL,
  347. };
  348. /* Debug state printing */
  349. static void pm8xxx_vreg_show_state(struct regulator_dev *rdev,
  350. enum pm8xxx_regulator_action action);
  351. /*
  352. * Perform a masked write to a PMIC register only if the new value differs
  353. * from the last value written to the register. This removes redundant
  354. * register writing.
  355. *
  356. * No locking is required because registers are not shared between regulators.
  357. */
  358. static int pm8xxx_vreg_masked_write(struct pm8xxx_vreg *vreg, u16 addr, u8 val,
  359. u8 mask, u8 *reg_save)
  360. {
  361. int rc = 0;
  362. u8 reg;
  363. reg = (*reg_save & ~mask) | (val & mask);
  364. if (reg != *reg_save) {
  365. rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
  366. if (rc) {
  367. pr_err("%s: pm8xxx_writeb failed; addr=0x%03X, rc=%d\n",
  368. vreg->rdesc.name, addr, rc);
  369. } else {
  370. *reg_save = reg;
  371. vreg->write_count++;
  372. if (pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_WRITES)
  373. pr_info("%s: write(0x%03X)=0x%02X\n",
  374. vreg->rdesc.name, addr, reg);
  375. }
  376. }
  377. return rc;
  378. }
  379. /*
  380. * Perform a masked write to a PMIC register without checking the previously
  381. * written value. This is needed for registers that must be rewritten even if
  382. * the value hasn't changed in order for changes in other registers to take
  383. * effect.
  384. */
  385. static int pm8xxx_vreg_masked_write_forced(struct pm8xxx_vreg *vreg, u16 addr,
  386. u8 val, u8 mask, u8 *reg_save)
  387. {
  388. int rc = 0;
  389. u8 reg;
  390. reg = (*reg_save & ~mask) | (val & mask);
  391. rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
  392. if (rc) {
  393. pr_err("%s: pm8xxx_writeb failed; addr=0x%03X, rc=%d\n",
  394. vreg->rdesc.name, addr, rc);
  395. } else {
  396. *reg_save = reg;
  397. vreg->write_count++;
  398. if (pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_WRITES)
  399. pr_info("%s: write(0x%03X)=0x%02X\n", vreg->rdesc.name,
  400. addr, reg);
  401. }
  402. return rc;
  403. }
  404. static int pm8xxx_vreg_is_pin_controlled(struct pm8xxx_vreg *vreg)
  405. {
  406. int ret = 0;
  407. switch (vreg->type) {
  408. case PM8XXX_REGULATOR_TYPE_PLDO:
  409. case PM8XXX_REGULATOR_TYPE_NLDO:
  410. ret = ((vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK) << 4)
  411. | (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK);
  412. break;
  413. case PM8XXX_REGULATOR_TYPE_SMPS:
  414. ret = vreg->sleep_ctrl_reg
  415. & (SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK);
  416. break;
  417. case PM8XXX_REGULATOR_TYPE_VS:
  418. ret = vreg->ctrl_reg & VS_PIN_CTRL_MASK;
  419. break;
  420. default:
  421. break;
  422. }
  423. return ret;
  424. }
  425. /*
  426. * Returns the logical pin control enable state because the pin control options
  427. * present in the hardware out of restart could be different from those desired
  428. * by the consumer.
  429. */
  430. static int pm8xxx_vreg_pin_control_is_enabled(struct regulator_dev *rdev)
  431. {
  432. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  433. int enabled;
  434. mutex_lock(&vreg->pc_lock);
  435. enabled = vreg->is_enabled_pc;
  436. mutex_unlock(&vreg->pc_lock);
  437. return enabled;
  438. }
  439. /* Returns the physical enable state of the regulator. */
  440. static int _pm8xxx_vreg_is_enabled(struct pm8xxx_vreg *vreg)
  441. {
  442. int rc = 0;
  443. /*
  444. * All regulator types except advanced mode SMPS, FTSMPS, and VS300 have
  445. * enable bit in bit 7 of the control register.
  446. */
  447. switch (vreg->type) {
  448. case PM8XXX_REGULATOR_TYPE_FTSMPS:
  449. if ((vreg->ctrl_reg & FTSMPS_VCTRL_BAND_MASK)
  450. != FTSMPS_VCTRL_BAND_OFF)
  451. rc = 1;
  452. break;
  453. case PM8XXX_REGULATOR_TYPE_VS300:
  454. if ((vreg->ctrl_reg & VS300_CTRL_ENABLE_MASK)
  455. != VS300_CTRL_DISABLE)
  456. rc = 1;
  457. break;
  458. case PM8XXX_REGULATOR_TYPE_SMPS:
  459. if (SMPS_IN_ADVANCED_MODE(vreg)) {
  460. if ((vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK)
  461. != SMPS_ADVANCED_BAND_OFF)
  462. rc = 1;
  463. break;
  464. }
  465. /* Fall through for legacy mode SMPS. */
  466. default:
  467. if ((vreg->ctrl_reg & REGULATOR_ENABLE_MASK)
  468. == REGULATOR_ENABLE)
  469. rc = 1;
  470. }
  471. return rc;
  472. }
  473. /*
  474. * Returns the logical enable state of the regulator which may be different from
  475. * the physical enable state thanks to HPM/LPM pin control.
  476. */
  477. static int pm8xxx_vreg_is_enabled(struct regulator_dev *rdev)
  478. {
  479. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  480. int enabled;
  481. if (vreg->type == PM8XXX_REGULATOR_TYPE_PLDO
  482. || vreg->type == PM8XXX_REGULATOR_TYPE_NLDO
  483. || vreg->type == PM8XXX_REGULATOR_TYPE_SMPS
  484. || vreg->type == PM8XXX_REGULATOR_TYPE_VS) {
  485. /* Pin controllable */
  486. mutex_lock(&vreg->pc_lock);
  487. enabled = vreg->is_enabled;
  488. mutex_unlock(&vreg->pc_lock);
  489. } else {
  490. /* Not pin controlable */
  491. enabled = _pm8xxx_vreg_is_enabled(vreg);
  492. }
  493. return enabled;
  494. }
  495. /*
  496. * Adds delay when increasing in voltage to account for the slew rate of
  497. * the regulator.
  498. */
  499. static void pm8xxx_vreg_delay_for_slew(struct pm8xxx_vreg *vreg, int prev_uV,
  500. int new_uV)
  501. {
  502. int delay;
  503. if (vreg->pdata.slew_rate == 0 || new_uV <= prev_uV ||
  504. !_pm8xxx_vreg_is_enabled(vreg))
  505. return;
  506. delay = DIV_ROUND_UP(new_uV - prev_uV, vreg->pdata.slew_rate);
  507. if (delay >= 1000) {
  508. mdelay(delay / 1000);
  509. udelay(delay % 1000);
  510. } else {
  511. udelay(delay);
  512. }
  513. }
  514. static int pm8xxx_pldo_get_voltage(struct regulator_dev *rdev)
  515. {
  516. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  517. int vmin, fine_step;
  518. u8 range_ext, range_sel, vprog, fine_step_reg;
  519. mutex_lock(&vreg->pc_lock);
  520. fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
  521. range_sel = vreg->test_reg[2] & LDO_TEST_RANGE_SEL_MASK;
  522. range_ext = vreg->test_reg[4] & LDO_TEST_RANGE_EXT_MASK;
  523. vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
  524. mutex_unlock(&vreg->pc_lock);
  525. vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
  526. if (range_sel) {
  527. /* low range mode */
  528. fine_step = PLDO_LOW_UV_FINE_STEP;
  529. vmin = PLDO_LOW_UV_MIN;
  530. } else if (!range_ext) {
  531. /* normal mode */
  532. fine_step = PLDO_NORM_UV_FINE_STEP;
  533. vmin = PLDO_NORM_UV_MIN;
  534. } else {
  535. /* high range mode */
  536. fine_step = PLDO_HIGH_UV_FINE_STEP;
  537. vmin = PLDO_HIGH_UV_MIN;
  538. }
  539. return fine_step * vprog + vmin;
  540. }
  541. static int pm8xxx_pldo_list_voltage(struct regulator_dev *rdev,
  542. unsigned selector)
  543. {
  544. int uV;
  545. if (selector >= PLDO_SET_POINTS)
  546. return 0;
  547. if (selector < PLDO_LOW_SET_POINTS)
  548. uV = selector * PLDO_LOW_UV_FINE_STEP + PLDO_LOW_UV_MIN;
  549. else if (selector < (PLDO_LOW_SET_POINTS + PLDO_NORM_SET_POINTS))
  550. uV = (selector - PLDO_LOW_SET_POINTS) * PLDO_NORM_UV_FINE_STEP
  551. + PLDO_NORM_UV_MIN;
  552. else
  553. uV = (selector - PLDO_LOW_SET_POINTS - PLDO_NORM_SET_POINTS)
  554. * PLDO_HIGH_UV_FINE_STEP
  555. + PLDO_HIGH_UV_SET_POINT_MIN;
  556. return uV;
  557. }
  558. static int pm8xxx_pldo_set_voltage(struct regulator_dev *rdev, int min_uV,
  559. int max_uV, unsigned *selector)
  560. {
  561. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  562. int rc = 0, uV = min_uV;
  563. int vmin, prev_uV;
  564. unsigned vprog, fine_step;
  565. u8 range_ext, range_sel, fine_step_reg, prev_reg;
  566. bool reg_changed = false;
  567. if (uV < PLDO_LOW_UV_MIN && max_uV >= PLDO_LOW_UV_MIN)
  568. uV = PLDO_LOW_UV_MIN;
  569. if (uV < PLDO_LOW_UV_MIN || uV > PLDO_HIGH_UV_MAX) {
  570. vreg_err(vreg,
  571. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  572. min_uV, max_uV, PLDO_LOW_UV_MIN, PLDO_HIGH_UV_MAX);
  573. return -EINVAL;
  574. }
  575. if (uV > PLDO_NORM_UV_MAX) {
  576. vmin = PLDO_HIGH_UV_MIN;
  577. fine_step = PLDO_HIGH_UV_FINE_STEP;
  578. range_ext = LDO_TEST_RANGE_EXT_MASK;
  579. range_sel = 0;
  580. } else if (uV > PLDO_LOW_UV_MAX) {
  581. vmin = PLDO_NORM_UV_MIN;
  582. fine_step = PLDO_NORM_UV_FINE_STEP;
  583. range_ext = 0;
  584. range_sel = 0;
  585. } else {
  586. vmin = PLDO_LOW_UV_MIN;
  587. fine_step = PLDO_LOW_UV_FINE_STEP;
  588. range_ext = 0;
  589. range_sel = LDO_TEST_RANGE_SEL_MASK;
  590. }
  591. vprog = (uV - vmin + fine_step - 1) / fine_step;
  592. uV = vprog * fine_step + vmin;
  593. fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
  594. vprog >>= 1;
  595. if (uV > max_uV) {
  596. vreg_err(vreg,
  597. "request v=[%d, %d] cannot be met by any set point\n",
  598. min_uV, max_uV);
  599. return -EINVAL;
  600. }
  601. prev_uV = pm8xxx_pldo_get_voltage(rdev);
  602. mutex_lock(&vreg->pc_lock);
  603. /* Write fine step, range select and program voltage update. */
  604. prev_reg = vreg->test_reg[2];
  605. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  606. fine_step_reg | range_sel | REGULATOR_BANK_SEL(2)
  607. | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
  608. LDO_TEST_FINE_STEP_MASK | LDO_TEST_RANGE_SEL_MASK
  609. | REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
  610. &vreg->test_reg[2]);
  611. if (rc)
  612. goto bail;
  613. if (prev_reg != vreg->test_reg[2])
  614. reg_changed = true;
  615. /* Write range extension. */
  616. prev_reg = vreg->test_reg[4];
  617. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  618. range_ext | REGULATOR_BANK_SEL(4)
  619. | REGULATOR_BANK_WRITE,
  620. LDO_TEST_RANGE_EXT_MASK | REGULATOR_BANK_MASK,
  621. &vreg->test_reg[4]);
  622. if (rc)
  623. goto bail;
  624. if (prev_reg != vreg->test_reg[4])
  625. reg_changed = true;
  626. /* Write new voltage. */
  627. if (reg_changed) {
  628. /*
  629. * Force a CTRL register write even if the value hasn't changed.
  630. * This is neccessary because range select, range extension, and
  631. * fine step will not update until a value is written into the
  632. * control register.
  633. */
  634. rc = pm8xxx_vreg_masked_write_forced(vreg, vreg->ctrl_addr,
  635. vprog, LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
  636. } else {
  637. /* Only write to control register if new value is different. */
  638. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, vprog,
  639. LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
  640. }
  641. bail:
  642. mutex_unlock(&vreg->pc_lock);
  643. if (rc)
  644. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  645. else {
  646. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, uV);
  647. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  648. }
  649. return rc;
  650. }
  651. static int pm8xxx_nldo_get_voltage(struct regulator_dev *rdev)
  652. {
  653. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  654. u8 vprog, fine_step_reg;
  655. mutex_lock(&vreg->pc_lock);
  656. fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
  657. vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
  658. mutex_unlock(&vreg->pc_lock);
  659. vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
  660. return NLDO_UV_FINE_STEP * vprog + NLDO_UV_MIN;
  661. }
  662. static int pm8xxx_nldo_list_voltage(struct regulator_dev *rdev,
  663. unsigned selector)
  664. {
  665. if (selector >= NLDO_SET_POINTS)
  666. return 0;
  667. return selector * NLDO_UV_FINE_STEP + NLDO_UV_MIN;
  668. }
  669. static int pm8xxx_nldo_set_voltage(struct regulator_dev *rdev, int min_uV,
  670. int max_uV, unsigned *selector)
  671. {
  672. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  673. unsigned vprog, fine_step_reg, prev_reg;
  674. int rc, prev_uV;
  675. int uV = min_uV;
  676. if (uV < NLDO_UV_MIN && max_uV >= NLDO_UV_MIN)
  677. uV = NLDO_UV_MIN;
  678. if (uV < NLDO_UV_MIN || uV > NLDO_UV_MAX) {
  679. vreg_err(vreg,
  680. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  681. min_uV, max_uV, NLDO_UV_MIN, NLDO_UV_MAX);
  682. return -EINVAL;
  683. }
  684. vprog = (uV - NLDO_UV_MIN + NLDO_UV_FINE_STEP - 1) / NLDO_UV_FINE_STEP;
  685. uV = vprog * NLDO_UV_FINE_STEP + NLDO_UV_MIN;
  686. fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
  687. vprog >>= 1;
  688. if (uV > max_uV) {
  689. vreg_err(vreg,
  690. "request v=[%d, %d] cannot be met by any set point\n",
  691. min_uV, max_uV);
  692. return -EINVAL;
  693. }
  694. prev_uV = pm8xxx_nldo_get_voltage(rdev);
  695. mutex_lock(&vreg->pc_lock);
  696. /* Write fine step. */
  697. prev_reg = vreg->test_reg[2];
  698. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  699. fine_step_reg | REGULATOR_BANK_SEL(2)
  700. | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
  701. LDO_TEST_FINE_STEP_MASK | REGULATOR_BANK_MASK
  702. | LDO_TEST_VPROG_UPDATE_MASK,
  703. &vreg->test_reg[2]);
  704. if (rc)
  705. goto bail;
  706. /* Write new voltage. */
  707. if (prev_reg != vreg->test_reg[2]) {
  708. /*
  709. * Force a CTRL register write even if the value hasn't changed.
  710. * This is neccessary because fine step will not update until a
  711. * value is written into the control register.
  712. */
  713. rc = pm8xxx_vreg_masked_write_forced(vreg, vreg->ctrl_addr,
  714. vprog, LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
  715. } else {
  716. /* Only write to control register if new value is different. */
  717. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, vprog,
  718. LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
  719. }
  720. bail:
  721. mutex_unlock(&vreg->pc_lock);
  722. if (rc)
  723. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  724. else {
  725. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, uV);
  726. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  727. }
  728. return rc;
  729. }
  730. static int _pm8xxx_nldo1200_get_voltage(struct pm8xxx_vreg *vreg)
  731. {
  732. int uV = 0;
  733. int vprog;
  734. if (!NLDO1200_IN_ADVANCED_MODE(vreg)) {
  735. pr_warn("%s: currently in legacy mode; voltage unknown.\n",
  736. vreg->rdesc.name);
  737. return vreg->save_uV;
  738. }
  739. vprog = vreg->ctrl_reg & NLDO1200_CTRL_VPROG_MASK;
  740. if ((vreg->ctrl_reg & NLDO1200_CTRL_RANGE_MASK)
  741. == NLDO1200_CTRL_RANGE_LOW)
  742. uV = vprog * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
  743. else
  744. uV = vprog * NLDO1200_HIGH_UV_STEP + NLDO1200_HIGH_UV_MIN;
  745. return uV;
  746. }
  747. static int pm8xxx_nldo1200_get_voltage(struct regulator_dev *rdev)
  748. {
  749. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  750. return _pm8xxx_nldo1200_get_voltage(vreg);
  751. }
  752. static int pm8xxx_nldo1200_list_voltage(struct regulator_dev *rdev,
  753. unsigned selector)
  754. {
  755. int uV;
  756. if (selector >= NLDO1200_SET_POINTS)
  757. return 0;
  758. if (selector < NLDO1200_LOW_SET_POINTS)
  759. uV = selector * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
  760. else
  761. uV = (selector - NLDO1200_LOW_SET_POINTS)
  762. * NLDO1200_HIGH_UV_STEP
  763. + NLDO1200_HIGH_UV_MIN;
  764. return uV;
  765. }
  766. static int _pm8xxx_nldo1200_set_voltage(struct pm8xxx_vreg *vreg, int min_uV,
  767. int max_uV)
  768. {
  769. u8 vprog, range;
  770. int rc, prev_uV;
  771. int uV = min_uV;
  772. if (uV < NLDO1200_LOW_UV_MIN && max_uV >= NLDO1200_LOW_UV_MIN)
  773. uV = NLDO1200_LOW_UV_MIN;
  774. if (uV < NLDO1200_LOW_UV_MIN || uV > NLDO1200_HIGH_UV_MAX) {
  775. vreg_err(vreg,
  776. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  777. min_uV, max_uV, NLDO_UV_MIN, NLDO_UV_MAX);
  778. return -EINVAL;
  779. }
  780. if (uV > NLDO1200_LOW_UV_MAX) {
  781. vprog = (uV - NLDO1200_HIGH_UV_MIN + NLDO1200_HIGH_UV_STEP - 1)
  782. / NLDO1200_HIGH_UV_STEP;
  783. uV = vprog * NLDO1200_HIGH_UV_STEP + NLDO1200_HIGH_UV_MIN;
  784. vprog &= NLDO1200_CTRL_VPROG_MASK;
  785. range = NLDO1200_CTRL_RANGE_HIGH;
  786. } else {
  787. vprog = (uV - NLDO1200_LOW_UV_MIN + NLDO1200_LOW_UV_STEP - 1)
  788. / NLDO1200_LOW_UV_STEP;
  789. uV = vprog * NLDO1200_LOW_UV_STEP + NLDO1200_LOW_UV_MIN;
  790. vprog &= NLDO1200_CTRL_VPROG_MASK;
  791. range = NLDO1200_CTRL_RANGE_LOW;
  792. }
  793. if (uV > max_uV) {
  794. vreg_err(vreg,
  795. "request v=[%d, %d] cannot be met by any set point\n",
  796. min_uV, max_uV);
  797. return -EINVAL;
  798. }
  799. prev_uV = _pm8xxx_nldo1200_get_voltage(vreg);
  800. /* Set to advanced mode */
  801. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  802. NLDO1200_ADVANCED_MODE | REGULATOR_BANK_SEL(2)
  803. | REGULATOR_BANK_WRITE, NLDO1200_ADVANCED_MODE_MASK
  804. | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
  805. if (rc)
  806. goto bail;
  807. /* Set voltage and range selection. */
  808. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, vprog | range,
  809. NLDO1200_CTRL_VPROG_MASK | NLDO1200_CTRL_RANGE_MASK,
  810. &vreg->ctrl_reg);
  811. if (rc)
  812. goto bail;
  813. vreg->save_uV = uV;
  814. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, uV);
  815. bail:
  816. if (rc)
  817. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  818. return rc;
  819. }
  820. static int pm8xxx_nldo1200_set_voltage(struct regulator_dev *rdev, int min_uV,
  821. int max_uV, unsigned *selector)
  822. {
  823. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  824. int rc;
  825. rc = _pm8xxx_nldo1200_set_voltage(vreg, min_uV, max_uV);
  826. if (!rc)
  827. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  828. return rc;
  829. }
  830. static int pm8xxx_smps_get_voltage_advanced(struct pm8xxx_vreg *vreg)
  831. {
  832. u8 vprog, band;
  833. int uV = 0;
  834. vprog = vreg->ctrl_reg & SMPS_ADVANCED_VPROG_MASK;
  835. band = vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK;
  836. if (band == SMPS_ADVANCED_BAND_1)
  837. uV = vprog * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
  838. else if (band == SMPS_ADVANCED_BAND_2)
  839. uV = vprog * SMPS_BAND2_UV_STEP + SMPS_BAND2_UV_MIN;
  840. else if (band == SMPS_ADVANCED_BAND_3)
  841. uV = vprog * SMPS_BAND3_UV_STEP + SMPS_BAND3_UV_MIN;
  842. else if (vreg->save_uV > 0)
  843. uV = vreg->save_uV;
  844. else
  845. uV = VOLTAGE_UNKNOWN;
  846. return uV;
  847. }
  848. static int pm8xxx_smps_get_voltage_legacy(struct pm8xxx_vreg *vreg)
  849. {
  850. u8 vlow, vref, vprog;
  851. int uV;
  852. vlow = vreg->test_reg[1] & SMPS_LEGACY_VLOW_SEL_MASK;
  853. vref = vreg->ctrl_reg & SMPS_LEGACY_VREF_SEL_MASK;
  854. vprog = vreg->ctrl_reg & SMPS_LEGACY_VPROG_MASK;
  855. if (vlow && vref) {
  856. /* mode 3 */
  857. uV = vprog * SMPS_MODE3_UV_STEP + SMPS_MODE3_UV_MIN;
  858. } else if (vref) {
  859. /* mode 2 */
  860. uV = vprog * SMPS_MODE2_UV_STEP + SMPS_MODE2_UV_MIN;
  861. } else {
  862. /* mode 1 */
  863. uV = vprog * SMPS_MODE1_UV_STEP + SMPS_MODE1_UV_MIN;
  864. }
  865. return uV;
  866. }
  867. static int _pm8xxx_smps_get_voltage(struct pm8xxx_vreg *vreg)
  868. {
  869. if (SMPS_IN_ADVANCED_MODE(vreg))
  870. return pm8xxx_smps_get_voltage_advanced(vreg);
  871. return pm8xxx_smps_get_voltage_legacy(vreg);
  872. }
  873. static int pm8xxx_smps_list_voltage(struct regulator_dev *rdev,
  874. unsigned selector)
  875. {
  876. int uV;
  877. if (selector >= SMPS_ADVANCED_SET_POINTS)
  878. return 0;
  879. if (selector < SMPS_BAND1_SET_POINTS)
  880. uV = selector * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
  881. else if (selector < (SMPS_BAND1_SET_POINTS + SMPS_BAND2_SET_POINTS))
  882. uV = (selector - SMPS_BAND1_SET_POINTS) * SMPS_BAND2_UV_STEP
  883. + SMPS_BAND2_UV_MIN;
  884. else
  885. uV = (selector - SMPS_BAND1_SET_POINTS - SMPS_BAND2_SET_POINTS)
  886. * SMPS_BAND3_UV_STEP
  887. + SMPS_BAND3_UV_MIN;
  888. return uV;
  889. }
  890. static int pm8xxx_smps_get_voltage(struct regulator_dev *rdev)
  891. {
  892. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  893. int uV;
  894. mutex_lock(&vreg->pc_lock);
  895. uV = _pm8xxx_smps_get_voltage(vreg);
  896. mutex_unlock(&vreg->pc_lock);
  897. return uV;
  898. }
  899. static int pm8xxx_smps_set_voltage_advanced(struct pm8xxx_vreg *vreg,
  900. int min_uV, int max_uV, int force_on)
  901. {
  902. u8 vprog, band;
  903. int rc;
  904. int uV = min_uV;
  905. if (uV < SMPS_BAND1_UV_MIN && max_uV >= SMPS_BAND1_UV_MIN)
  906. uV = SMPS_BAND1_UV_MIN;
  907. if (uV < SMPS_BAND1_UV_MIN || uV > SMPS_BAND3_UV_MAX) {
  908. vreg_err(vreg,
  909. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  910. min_uV, max_uV, SMPS_BAND1_UV_MIN, SMPS_BAND3_UV_MAX);
  911. return -EINVAL;
  912. }
  913. if (uV > SMPS_BAND2_UV_MAX) {
  914. vprog = (uV - SMPS_BAND3_UV_MIN + SMPS_BAND3_UV_STEP - 1)
  915. / SMPS_BAND3_UV_STEP;
  916. band = SMPS_ADVANCED_BAND_3;
  917. uV = SMPS_BAND3_UV_MIN + vprog * SMPS_BAND3_UV_STEP;
  918. } else if (uV > SMPS_BAND1_UV_MAX) {
  919. vprog = (uV - SMPS_BAND2_UV_MIN + SMPS_BAND2_UV_STEP - 1)
  920. / SMPS_BAND2_UV_STEP;
  921. band = SMPS_ADVANCED_BAND_2;
  922. uV = SMPS_BAND2_UV_MIN + vprog * SMPS_BAND2_UV_STEP;
  923. } else {
  924. vprog = (uV - SMPS_BAND1_UV_MIN + SMPS_BAND1_UV_STEP - 1)
  925. / SMPS_BAND1_UV_STEP;
  926. band = SMPS_ADVANCED_BAND_1;
  927. uV = SMPS_BAND1_UV_MIN + vprog * SMPS_BAND1_UV_STEP;
  928. }
  929. if (uV > max_uV) {
  930. vreg_err(vreg,
  931. "request v=[%d, %d] cannot be met by any set point\n",
  932. min_uV, max_uV);
  933. return -EINVAL;
  934. }
  935. /* Do not set band if regulator currently disabled. */
  936. if (!_pm8xxx_vreg_is_enabled(vreg) && !force_on)
  937. band = SMPS_ADVANCED_BAND_OFF;
  938. /* Set advanced mode bit to 1. */
  939. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr, SMPS_ADVANCED_MODE
  940. | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
  941. SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
  942. &vreg->test_reg[7]);
  943. if (rc)
  944. goto bail;
  945. /* Set voltage and voltage band. */
  946. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, band | vprog,
  947. SMPS_ADVANCED_BAND_MASK | SMPS_ADVANCED_VPROG_MASK,
  948. &vreg->ctrl_reg);
  949. if (rc)
  950. goto bail;
  951. vreg->save_uV = uV;
  952. bail:
  953. if (rc)
  954. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  955. return rc;
  956. }
  957. static int pm8xxx_smps_set_voltage_legacy(struct pm8xxx_vreg *vreg, int min_uV,
  958. int max_uV)
  959. {
  960. u8 vlow, vref, vprog, pd, en;
  961. int rc;
  962. int uV = min_uV;
  963. if (uV < SMPS_MODE3_UV_MIN && max_uV >= SMPS_MODE3_UV_MIN)
  964. uV = SMPS_MODE3_UV_MIN;
  965. if (uV < SMPS_MODE3_UV_MIN || uV > SMPS_MODE1_UV_MAX) {
  966. vreg_err(vreg,
  967. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  968. min_uV, max_uV, SMPS_MODE3_UV_MIN, SMPS_MODE1_UV_MAX);
  969. return -EINVAL;
  970. }
  971. if (uV > SMPS_MODE2_UV_MAX) {
  972. vprog = (uV - SMPS_MODE1_UV_MIN + SMPS_MODE1_UV_STEP - 1)
  973. / SMPS_MODE1_UV_STEP;
  974. vref = 0;
  975. vlow = 0;
  976. uV = SMPS_MODE1_UV_MIN + vprog * SMPS_MODE1_UV_STEP;
  977. } else if (uV > SMPS_MODE3_UV_MAX) {
  978. vprog = (uV - SMPS_MODE2_UV_MIN + SMPS_MODE2_UV_STEP - 1)
  979. / SMPS_MODE2_UV_STEP;
  980. vref = SMPS_LEGACY_VREF_SEL_MASK;
  981. vlow = 0;
  982. uV = SMPS_MODE2_UV_MIN + vprog * SMPS_MODE2_UV_STEP;
  983. } else {
  984. vprog = (uV - SMPS_MODE3_UV_MIN + SMPS_MODE3_UV_STEP - 1)
  985. / SMPS_MODE3_UV_STEP;
  986. vref = SMPS_LEGACY_VREF_SEL_MASK;
  987. vlow = SMPS_LEGACY_VLOW_SEL_MASK;
  988. uV = SMPS_MODE3_UV_MIN + vprog * SMPS_MODE3_UV_STEP;
  989. }
  990. if (uV > max_uV) {
  991. vreg_err(vreg,
  992. "request v=[%d, %d] cannot be met by any set point\n",
  993. min_uV, max_uV);
  994. return -EINVAL;
  995. }
  996. /* set vlow bit for ultra low voltage mode */
  997. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  998. vlow | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1),
  999. REGULATOR_BANK_MASK | SMPS_LEGACY_VLOW_SEL_MASK,
  1000. &vreg->test_reg[1]);
  1001. if (rc)
  1002. goto bail;
  1003. /* Set advanced mode bit to 0. */
  1004. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr, SMPS_LEGACY_MODE
  1005. | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
  1006. SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
  1007. &vreg->test_reg[7]);
  1008. if (rc)
  1009. goto bail;
  1010. en = (_pm8xxx_vreg_is_enabled(vreg) ? SMPS_LEGACY_ENABLE : 0);
  1011. pd = (vreg->pdata.pull_down_enable ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0);
  1012. /* Set voltage (and the rest of the control register). */
  1013. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1014. en | pd | vref | vprog,
  1015. SMPS_LEGACY_ENABLE_MASK | SMPS_LEGACY_PULL_DOWN_ENABLE
  1016. | SMPS_LEGACY_VREF_SEL_MASK | SMPS_LEGACY_VPROG_MASK,
  1017. &vreg->ctrl_reg);
  1018. vreg->save_uV = uV;
  1019. bail:
  1020. if (rc)
  1021. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1022. return rc;
  1023. }
  1024. static int pm8xxx_smps_set_voltage(struct regulator_dev *rdev, int min_uV,
  1025. int max_uV, unsigned *selector)
  1026. {
  1027. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1028. int rc = 0;
  1029. int prev_uV, new_uV;
  1030. prev_uV = pm8xxx_smps_get_voltage(rdev);
  1031. mutex_lock(&vreg->pc_lock);
  1032. if (SMPS_IN_ADVANCED_MODE(vreg) || !pm8xxx_vreg_is_pin_controlled(vreg))
  1033. rc = pm8xxx_smps_set_voltage_advanced(vreg, min_uV, max_uV, 0);
  1034. else
  1035. rc = pm8xxx_smps_set_voltage_legacy(vreg, min_uV, max_uV);
  1036. mutex_unlock(&vreg->pc_lock);
  1037. new_uV = pm8xxx_smps_get_voltage(rdev);
  1038. if (!rc) {
  1039. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, new_uV);
  1040. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  1041. }
  1042. return rc;
  1043. }
  1044. static int _pm8xxx_ftsmps_get_voltage(struct pm8xxx_vreg *vreg)
  1045. {
  1046. u8 vprog, band;
  1047. int uV = 0;
  1048. if ((vreg->test_reg[0] & FTSMPS_CNFG1_PM_MASK) == FTSMPS_CNFG1_PM_PFM) {
  1049. vprog = vreg->pfm_ctrl_reg & FTSMPS_VCTRL_VPROG_MASK;
  1050. band = vreg->pfm_ctrl_reg & FTSMPS_VCTRL_BAND_MASK;
  1051. if (band == FTSMPS_VCTRL_BAND_OFF && vprog == 0) {
  1052. /* PWM_VCTRL overrides PFM_VCTRL */
  1053. vprog = vreg->ctrl_reg & FTSMPS_VCTRL_VPROG_MASK;
  1054. band = vreg->ctrl_reg & FTSMPS_VCTRL_BAND_MASK;
  1055. }
  1056. } else {
  1057. vprog = vreg->ctrl_reg & FTSMPS_VCTRL_VPROG_MASK;
  1058. band = vreg->ctrl_reg & FTSMPS_VCTRL_BAND_MASK;
  1059. }
  1060. if (band == FTSMPS_VCTRL_BAND_1)
  1061. uV = vprog * FTSMPS_BAND1_UV_PHYS_STEP + FTSMPS_BAND1_UV_MIN;
  1062. else if (band == FTSMPS_VCTRL_BAND_2)
  1063. uV = vprog * FTSMPS_BAND2_UV_STEP + FTSMPS_BAND2_UV_MIN;
  1064. else if (band == FTSMPS_VCTRL_BAND_3)
  1065. uV = vprog * FTSMPS_BAND3_UV_STEP + FTSMPS_BAND3_UV_MIN;
  1066. else if (vreg->save_uV > 0)
  1067. uV = vreg->save_uV;
  1068. else
  1069. uV = VOLTAGE_UNKNOWN;
  1070. return uV;
  1071. }
  1072. static int pm8xxx_ftsmps_get_voltage(struct regulator_dev *rdev)
  1073. {
  1074. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1075. return _pm8xxx_ftsmps_get_voltage(vreg);
  1076. }
  1077. static int pm8xxx_ftsmps_list_voltage(struct regulator_dev *rdev,
  1078. unsigned selector)
  1079. {
  1080. int uV;
  1081. if (selector >= FTSMPS_SET_POINTS)
  1082. return 0;
  1083. if (selector < FTSMPS_BAND1_SET_POINTS)
  1084. uV = selector * FTSMPS_BAND1_UV_LOG_STEP + FTSMPS_BAND1_UV_MIN;
  1085. else if (selector < (FTSMPS_BAND1_SET_POINTS + FTSMPS_BAND2_SET_POINTS))
  1086. uV = (selector - FTSMPS_BAND1_SET_POINTS) * FTSMPS_BAND2_UV_STEP
  1087. + FTSMPS_BAND2_UV_MIN;
  1088. else
  1089. uV = (selector - FTSMPS_BAND1_SET_POINTS
  1090. - FTSMPS_BAND2_SET_POINTS)
  1091. * FTSMPS_BAND3_UV_STEP
  1092. + FTSMPS_BAND3_UV_SET_POINT_MIN;
  1093. return uV;
  1094. }
  1095. static int _pm8xxx_ftsmps_set_voltage(struct pm8xxx_vreg *vreg, int min_uV,
  1096. int max_uV, int force_on)
  1097. {
  1098. int rc = 0;
  1099. u8 vprog, band;
  1100. int uV = min_uV;
  1101. int prev_uV;
  1102. if (uV < FTSMPS_BAND1_UV_MIN && max_uV >= FTSMPS_BAND1_UV_MIN)
  1103. uV = FTSMPS_BAND1_UV_MIN;
  1104. if (uV < FTSMPS_BAND1_UV_MIN || uV > FTSMPS_BAND3_UV_MAX) {
  1105. vreg_err(vreg,
  1106. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  1107. min_uV, max_uV, FTSMPS_BAND1_UV_MIN,
  1108. FTSMPS_BAND3_UV_MAX);
  1109. return -EINVAL;
  1110. }
  1111. /* Round up for set points in the gaps between bands. */
  1112. if (uV > FTSMPS_BAND1_UV_MAX && uV < FTSMPS_BAND2_UV_MIN)
  1113. uV = FTSMPS_BAND2_UV_MIN;
  1114. else if (uV > FTSMPS_BAND2_UV_MAX
  1115. && uV < FTSMPS_BAND3_UV_SET_POINT_MIN)
  1116. uV = FTSMPS_BAND3_UV_SET_POINT_MIN;
  1117. if (uV > FTSMPS_BAND2_UV_MAX) {
  1118. vprog = (uV - FTSMPS_BAND3_UV_MIN + FTSMPS_BAND3_UV_STEP - 1)
  1119. / FTSMPS_BAND3_UV_STEP;
  1120. band = FTSMPS_VCTRL_BAND_3;
  1121. uV = FTSMPS_BAND3_UV_MIN + vprog * FTSMPS_BAND3_UV_STEP;
  1122. } else if (uV > FTSMPS_BAND1_UV_MAX) {
  1123. vprog = (uV - FTSMPS_BAND2_UV_MIN + FTSMPS_BAND2_UV_STEP - 1)
  1124. / FTSMPS_BAND2_UV_STEP;
  1125. band = FTSMPS_VCTRL_BAND_2;
  1126. uV = FTSMPS_BAND2_UV_MIN + vprog * FTSMPS_BAND2_UV_STEP;
  1127. } else {
  1128. vprog = (uV - FTSMPS_BAND1_UV_MIN
  1129. + FTSMPS_BAND1_UV_LOG_STEP - 1)
  1130. / FTSMPS_BAND1_UV_LOG_STEP;
  1131. uV = FTSMPS_BAND1_UV_MIN + vprog * FTSMPS_BAND1_UV_LOG_STEP;
  1132. vprog *= FTSMPS_BAND1_UV_LOG_STEP / FTSMPS_BAND1_UV_PHYS_STEP;
  1133. band = FTSMPS_VCTRL_BAND_1;
  1134. }
  1135. if (uV > max_uV) {
  1136. vreg_err(vreg,
  1137. "request v=[%d, %d] cannot be met by any set point\n",
  1138. min_uV, max_uV);
  1139. return -EINVAL;
  1140. }
  1141. prev_uV = _pm8xxx_ftsmps_get_voltage(vreg);
  1142. /*
  1143. * Do not set voltage if regulator is currently disabled because doing
  1144. * so will enable it.
  1145. */
  1146. if (_pm8xxx_vreg_is_enabled(vreg) || force_on) {
  1147. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1148. band | vprog,
  1149. FTSMPS_VCTRL_BAND_MASK | FTSMPS_VCTRL_VPROG_MASK,
  1150. &vreg->ctrl_reg);
  1151. if (rc)
  1152. goto bail;
  1153. /* Program PFM_VCTRL as 0x00 so that PWM_VCTRL overrides it. */
  1154. rc = pm8xxx_vreg_masked_write(vreg, vreg->pfm_ctrl_addr, 0x00,
  1155. FTSMPS_VCTRL_BAND_MASK | FTSMPS_VCTRL_VPROG_MASK,
  1156. &vreg->pfm_ctrl_reg);
  1157. if (rc)
  1158. goto bail;
  1159. }
  1160. vreg->save_uV = uV;
  1161. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, uV);
  1162. bail:
  1163. if (rc)
  1164. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1165. return rc;
  1166. }
  1167. static int pm8xxx_ftsmps_set_voltage(struct regulator_dev *rdev, int min_uV,
  1168. int max_uV, unsigned *selector)
  1169. {
  1170. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1171. int rc;
  1172. rc = _pm8xxx_ftsmps_set_voltage(vreg, min_uV, max_uV, 0);
  1173. if (!rc)
  1174. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  1175. return rc;
  1176. }
  1177. static int pm8xxx_ncp_get_voltage(struct regulator_dev *rdev)
  1178. {
  1179. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1180. u8 vprog;
  1181. vprog = vreg->ctrl_reg & NCP_VPROG_MASK;
  1182. return NCP_UV_MIN + vprog * NCP_UV_STEP;
  1183. }
  1184. static int pm8xxx_ncp_list_voltage(struct regulator_dev *rdev,
  1185. unsigned selector)
  1186. {
  1187. if (selector >= NCP_SET_POINTS)
  1188. return 0;
  1189. return selector * NCP_UV_STEP + NCP_UV_MIN;
  1190. }
  1191. static int pm8xxx_ncp_set_voltage(struct regulator_dev *rdev, int min_uV,
  1192. int max_uV, unsigned *selector)
  1193. {
  1194. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1195. int rc, prev_uV;
  1196. int uV = min_uV;
  1197. u8 val;
  1198. if (uV < NCP_UV_MIN && max_uV >= NCP_UV_MIN)
  1199. uV = NCP_UV_MIN;
  1200. if (uV < NCP_UV_MIN || uV > NCP_UV_MAX) {
  1201. vreg_err(vreg,
  1202. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  1203. min_uV, max_uV, NCP_UV_MIN, NCP_UV_MAX);
  1204. return -EINVAL;
  1205. }
  1206. val = (uV - NCP_UV_MIN + NCP_UV_STEP - 1) / NCP_UV_STEP;
  1207. uV = val * NCP_UV_STEP + NCP_UV_MIN;
  1208. if (uV > max_uV) {
  1209. vreg_err(vreg,
  1210. "request v=[%d, %d] cannot be met by any set point\n",
  1211. min_uV, max_uV);
  1212. return -EINVAL;
  1213. }
  1214. prev_uV = pm8xxx_ncp_get_voltage(rdev);
  1215. /* voltage setting */
  1216. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
  1217. NCP_VPROG_MASK, &vreg->ctrl_reg);
  1218. if (rc)
  1219. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1220. else {
  1221. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, uV);
  1222. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  1223. }
  1224. return rc;
  1225. }
  1226. static int pm8xxx_boost_get_voltage(struct regulator_dev *rdev)
  1227. {
  1228. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1229. u8 vprog;
  1230. vprog = vreg->ctrl_reg & BOOST_VPROG_MASK;
  1231. return BOOST_UV_STEP * vprog + BOOST_UV_MIN;
  1232. }
  1233. static int pm8xxx_boost_list_voltage(struct regulator_dev *rdev,
  1234. unsigned selector)
  1235. {
  1236. if (selector >= BOOST_SET_POINTS)
  1237. return 0;
  1238. return selector * BOOST_UV_STEP + BOOST_UV_MIN;
  1239. }
  1240. static int pm8xxx_boost_set_voltage(struct regulator_dev *rdev, int min_uV,
  1241. int max_uV, unsigned *selector)
  1242. {
  1243. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1244. int rc, prev_uV;
  1245. int uV = min_uV;
  1246. u8 val;
  1247. if (uV < BOOST_UV_MIN && max_uV >= BOOST_UV_MIN)
  1248. uV = BOOST_UV_MIN;
  1249. if (uV < BOOST_UV_MIN || uV > BOOST_UV_MAX) {
  1250. vreg_err(vreg,
  1251. "request v=[%d, %d] is outside possible v=[%d, %d]\n",
  1252. min_uV, max_uV, BOOST_UV_MIN, BOOST_UV_MAX);
  1253. return -EINVAL;
  1254. }
  1255. val = (uV - BOOST_UV_MIN + BOOST_UV_STEP - 1) / BOOST_UV_STEP;
  1256. uV = val * BOOST_UV_STEP + BOOST_UV_MIN;
  1257. if (uV > max_uV) {
  1258. vreg_err(vreg,
  1259. "request v=[%d, %d] cannot be met by any set point\n",
  1260. min_uV, max_uV);
  1261. return -EINVAL;
  1262. }
  1263. prev_uV = pm8xxx_boost_get_voltage(rdev);
  1264. /* voltage setting */
  1265. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
  1266. BOOST_VPROG_MASK, &vreg->ctrl_reg);
  1267. if (rc)
  1268. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1269. else {
  1270. pm8xxx_vreg_delay_for_slew(vreg, prev_uV, uV);
  1271. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_VOLTAGE);
  1272. }
  1273. return rc;
  1274. }
  1275. static unsigned int pm8xxx_ldo_get_mode(struct regulator_dev *rdev)
  1276. {
  1277. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1278. unsigned int mode = 0;
  1279. mutex_lock(&vreg->pc_lock);
  1280. mode = vreg->mode;
  1281. mutex_unlock(&vreg->pc_lock);
  1282. return mode;
  1283. }
  1284. static int pm8xxx_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
  1285. {
  1286. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1287. int rc = 0;
  1288. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  1289. vreg_err(vreg, "invalid mode: %u\n", mode);
  1290. return -EINVAL;
  1291. }
  1292. mutex_lock(&vreg->pc_lock);
  1293. if (mode == REGULATOR_MODE_NORMAL
  1294. || (vreg->is_enabled_pc
  1295. && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE)) {
  1296. /* HPM */
  1297. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1298. LDO_CTRL_PM_HPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1299. } else {
  1300. /* LPM */
  1301. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1302. LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1303. if (rc)
  1304. goto bail;
  1305. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1306. LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
  1307. | REGULATOR_BANK_SEL(0),
  1308. LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
  1309. &vreg->test_reg[0]);
  1310. }
  1311. bail:
  1312. if (!rc)
  1313. vreg->mode = mode;
  1314. mutex_unlock(&vreg->pc_lock);
  1315. if (rc)
  1316. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1317. else
  1318. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
  1319. return rc;
  1320. }
  1321. static unsigned int pm8xxx_nldo1200_get_mode(struct regulator_dev *rdev)
  1322. {
  1323. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1324. unsigned int mode = 0;
  1325. if (NLDO1200_IN_ADVANCED_MODE(vreg)) {
  1326. /* Advanced mode */
  1327. if ((vreg->test_reg[2] & NLDO1200_ADVANCED_PM_MASK)
  1328. == NLDO1200_ADVANCED_PM_LPM)
  1329. mode = REGULATOR_MODE_IDLE;
  1330. else
  1331. mode = REGULATOR_MODE_NORMAL;
  1332. } else {
  1333. /* Legacy mode */
  1334. if ((vreg->ctrl_reg & NLDO1200_LEGACY_PM_MASK)
  1335. == NLDO1200_LEGACY_PM_LPM)
  1336. mode = REGULATOR_MODE_IDLE;
  1337. else
  1338. mode = REGULATOR_MODE_NORMAL;
  1339. }
  1340. return mode;
  1341. }
  1342. static int pm8xxx_nldo1200_set_mode(struct regulator_dev *rdev,
  1343. unsigned int mode)
  1344. {
  1345. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1346. int rc = 0;
  1347. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  1348. vreg_err(vreg, "invalid mode: %u\n", mode);
  1349. return -EINVAL;
  1350. }
  1351. /*
  1352. * Make sure that advanced mode is in use. If it isn't, then set it
  1353. * and update the voltage accordingly.
  1354. */
  1355. if (!NLDO1200_IN_ADVANCED_MODE(vreg)) {
  1356. rc = _pm8xxx_nldo1200_set_voltage(vreg, vreg->save_uV,
  1357. vreg->save_uV);
  1358. if (rc)
  1359. goto bail;
  1360. }
  1361. if (mode == REGULATOR_MODE_NORMAL) {
  1362. /* HPM */
  1363. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1364. NLDO1200_ADVANCED_PM_HPM | REGULATOR_BANK_WRITE
  1365. | REGULATOR_BANK_SEL(2), NLDO1200_ADVANCED_PM_MASK
  1366. | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
  1367. } else {
  1368. /* LPM */
  1369. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1370. NLDO1200_ADVANCED_PM_LPM | REGULATOR_BANK_WRITE
  1371. | REGULATOR_BANK_SEL(2), NLDO1200_ADVANCED_PM_MASK
  1372. | REGULATOR_BANK_MASK, &vreg->test_reg[2]);
  1373. }
  1374. bail:
  1375. if (rc)
  1376. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1377. else
  1378. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
  1379. return rc;
  1380. }
  1381. static unsigned int pm8xxx_smps_get_mode(struct regulator_dev *rdev)
  1382. {
  1383. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1384. unsigned int mode = 0;
  1385. mutex_lock(&vreg->pc_lock);
  1386. mode = vreg->mode;
  1387. mutex_unlock(&vreg->pc_lock);
  1388. return mode;
  1389. }
  1390. static int pm8xxx_smps_set_mode(struct regulator_dev *rdev, unsigned int mode)
  1391. {
  1392. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1393. int rc = 0;
  1394. if (mode != REGULATOR_MODE_NORMAL && mode != REGULATOR_MODE_IDLE) {
  1395. vreg_err(vreg, "invalid mode: %u\n", mode);
  1396. return -EINVAL;
  1397. }
  1398. mutex_lock(&vreg->pc_lock);
  1399. if (mode == REGULATOR_MODE_NORMAL
  1400. || (vreg->is_enabled_pc
  1401. && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE)) {
  1402. /* HPM */
  1403. rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
  1404. SMPS_CLK_CTRL_PWM, SMPS_CLK_CTRL_MASK,
  1405. &vreg->clk_ctrl_reg);
  1406. } else {
  1407. /* LPM */
  1408. rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
  1409. SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
  1410. &vreg->clk_ctrl_reg);
  1411. }
  1412. if (!rc)
  1413. vreg->mode = mode;
  1414. mutex_unlock(&vreg->pc_lock);
  1415. if (rc)
  1416. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1417. else
  1418. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
  1419. return rc;
  1420. }
  1421. static unsigned int pm8xxx_ftsmps_get_mode(struct regulator_dev *rdev)
  1422. {
  1423. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1424. unsigned int mode = 0;
  1425. if ((vreg->test_reg[0] & FTSMPS_CNFG1_PM_MASK) == FTSMPS_CNFG1_PM_PFM)
  1426. mode = REGULATOR_MODE_IDLE;
  1427. else
  1428. mode = REGULATOR_MODE_NORMAL;
  1429. return mode;
  1430. }
  1431. static int pm8xxx_ftsmps_set_mode(struct regulator_dev *rdev, unsigned int mode)
  1432. {
  1433. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1434. int rc = 0;
  1435. if (mode == REGULATOR_MODE_NORMAL) {
  1436. /* HPM */
  1437. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1438. FTSMPS_CNFG1_PM_PWM | REGULATOR_BANK_WRITE
  1439. | REGULATOR_BANK_SEL(0), FTSMPS_CNFG1_PM_MASK
  1440. | REGULATOR_BANK_MASK, &vreg->test_reg[0]);
  1441. } else if (mode == REGULATOR_MODE_IDLE) {
  1442. /* LPM */
  1443. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1444. FTSMPS_CNFG1_PM_PFM | REGULATOR_BANK_WRITE
  1445. | REGULATOR_BANK_SEL(0), FTSMPS_CNFG1_PM_MASK
  1446. | REGULATOR_BANK_MASK, &vreg->test_reg[0]);
  1447. } else {
  1448. vreg_err(vreg, "invalid mode: %u\n", mode);
  1449. return -EINVAL;
  1450. }
  1451. if (rc)
  1452. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1453. else
  1454. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_MODE);
  1455. return rc;
  1456. }
  1457. static unsigned int pm8xxx_vreg_get_optimum_mode(struct regulator_dev *rdev,
  1458. int input_uV, int output_uV, int load_uA)
  1459. {
  1460. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1461. unsigned int mode;
  1462. if (load_uA + vreg->pdata.system_uA >= vreg->hpm_min_load)
  1463. mode = REGULATOR_MODE_NORMAL;
  1464. else
  1465. mode = REGULATOR_MODE_IDLE;
  1466. return mode;
  1467. }
  1468. static int pm8xxx_ldo_enable(struct regulator_dev *rdev)
  1469. {
  1470. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1471. int rc, val;
  1472. mutex_lock(&vreg->pc_lock);
  1473. /*
  1474. * Choose HPM if previously set to HPM or if pin control is enabled in
  1475. * on/off mode.
  1476. */
  1477. val = LDO_CTRL_PM_LPM;
  1478. if (vreg->mode == REGULATOR_MODE_NORMAL
  1479. || (vreg->is_enabled_pc
  1480. && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE))
  1481. val = LDO_CTRL_PM_HPM;
  1482. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val | LDO_ENABLE,
  1483. LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1484. if (!rc)
  1485. vreg->is_enabled = true;
  1486. mutex_unlock(&vreg->pc_lock);
  1487. if (rc)
  1488. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1489. else
  1490. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1491. return rc;
  1492. }
  1493. static int pm8xxx_ldo_disable(struct regulator_dev *rdev)
  1494. {
  1495. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1496. int rc;
  1497. mutex_lock(&vreg->pc_lock);
  1498. /*
  1499. * Only disable the regulator if it isn't still required for HPM/LPM
  1500. * pin control.
  1501. */
  1502. if (!vreg->is_enabled_pc
  1503. || vreg->pdata.pin_fn != PM8XXX_VREG_PIN_FN_MODE) {
  1504. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1505. LDO_DISABLE, LDO_ENABLE_MASK, &vreg->ctrl_reg);
  1506. if (rc)
  1507. goto bail;
  1508. }
  1509. /* Change to LPM if HPM/LPM pin control is enabled. */
  1510. if (vreg->is_enabled_pc
  1511. && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_MODE) {
  1512. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1513. LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1514. if (rc)
  1515. goto bail;
  1516. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1517. LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
  1518. | REGULATOR_BANK_SEL(0),
  1519. LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
  1520. &vreg->test_reg[0]);
  1521. }
  1522. if (!rc)
  1523. vreg->is_enabled = false;
  1524. bail:
  1525. mutex_unlock(&vreg->pc_lock);
  1526. if (rc)
  1527. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1528. else
  1529. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1530. return rc;
  1531. }
  1532. static int pm8xxx_nldo1200_enable(struct regulator_dev *rdev)
  1533. {
  1534. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1535. int rc;
  1536. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NLDO1200_ENABLE,
  1537. NLDO1200_ENABLE_MASK, &vreg->ctrl_reg);
  1538. if (rc)
  1539. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1540. else
  1541. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1542. return rc;
  1543. }
  1544. static int pm8xxx_nldo1200_disable(struct regulator_dev *rdev)
  1545. {
  1546. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1547. int rc;
  1548. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NLDO1200_DISABLE,
  1549. NLDO1200_ENABLE_MASK, &vreg->ctrl_reg);
  1550. if (rc)
  1551. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1552. else
  1553. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1554. return rc;
  1555. }
  1556. static int pm8xxx_smps_enable(struct regulator_dev *rdev)
  1557. {
  1558. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1559. int rc = 0;
  1560. int val;
  1561. mutex_lock(&vreg->pc_lock);
  1562. if (SMPS_IN_ADVANCED_MODE(vreg)
  1563. || !pm8xxx_vreg_is_pin_controlled(vreg)) {
  1564. /* Enable in advanced mode if not using pin control. */
  1565. rc = pm8xxx_smps_set_voltage_advanced(vreg, vreg->save_uV,
  1566. vreg->save_uV, 1);
  1567. } else {
  1568. rc = pm8xxx_smps_set_voltage_legacy(vreg, vreg->save_uV,
  1569. vreg->save_uV);
  1570. if (rc)
  1571. goto bail;
  1572. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1573. SMPS_LEGACY_ENABLE, SMPS_LEGACY_ENABLE_MASK,
  1574. &vreg->ctrl_reg);
  1575. }
  1576. /*
  1577. * Choose HPM if previously set to HPM or if pin control is enabled in
  1578. * on/off mode.
  1579. */
  1580. val = SMPS_CLK_CTRL_PFM;
  1581. if (vreg->mode == REGULATOR_MODE_NORMAL
  1582. || (vreg->is_enabled_pc
  1583. && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE))
  1584. val = SMPS_CLK_CTRL_PWM;
  1585. rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr, val,
  1586. SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
  1587. if (!rc)
  1588. vreg->is_enabled = true;
  1589. bail:
  1590. mutex_unlock(&vreg->pc_lock);
  1591. if (rc)
  1592. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1593. else
  1594. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1595. return rc;
  1596. }
  1597. static int pm8xxx_smps_disable(struct regulator_dev *rdev)
  1598. {
  1599. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1600. int rc;
  1601. mutex_lock(&vreg->pc_lock);
  1602. if (SMPS_IN_ADVANCED_MODE(vreg)) {
  1603. /* Change SMPS to legacy mode before disabling. */
  1604. rc = pm8xxx_smps_set_voltage_legacy(vreg, vreg->save_uV,
  1605. vreg->save_uV);
  1606. if (rc)
  1607. goto bail;
  1608. }
  1609. /*
  1610. * Only disable the regulator if it isn't still required for HPM/LPM
  1611. * pin control.
  1612. */
  1613. if (!vreg->is_enabled_pc
  1614. || vreg->pdata.pin_fn != PM8XXX_VREG_PIN_FN_MODE) {
  1615. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1616. SMPS_LEGACY_DISABLE, SMPS_LEGACY_ENABLE_MASK,
  1617. &vreg->ctrl_reg);
  1618. if (rc)
  1619. goto bail;
  1620. }
  1621. /* Change to LPM if HPM/LPM pin control is enabled. */
  1622. if (vreg->is_enabled_pc
  1623. && vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_MODE)
  1624. rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
  1625. SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
  1626. &vreg->clk_ctrl_reg);
  1627. if (!rc)
  1628. vreg->is_enabled = false;
  1629. bail:
  1630. mutex_unlock(&vreg->pc_lock);
  1631. if (rc)
  1632. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1633. else
  1634. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1635. return rc;
  1636. }
  1637. static int pm8xxx_ftsmps_enable(struct regulator_dev *rdev)
  1638. {
  1639. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1640. int rc;
  1641. rc = _pm8xxx_ftsmps_set_voltage(vreg, vreg->save_uV, vreg->save_uV, 1);
  1642. if (rc)
  1643. vreg_err(vreg, "set voltage failed, rc=%d\n", rc);
  1644. else
  1645. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1646. return rc;
  1647. }
  1648. static int pm8xxx_ftsmps_disable(struct regulator_dev *rdev)
  1649. {
  1650. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1651. int rc;
  1652. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1653. FTSMPS_VCTRL_BAND_OFF, FTSMPS_VCTRL_BAND_MASK, &vreg->ctrl_reg);
  1654. if (rc)
  1655. goto bail;
  1656. rc = pm8xxx_vreg_masked_write(vreg, vreg->pfm_ctrl_addr,
  1657. FTSMPS_VCTRL_BAND_OFF, FTSMPS_VCTRL_BAND_MASK,
  1658. &vreg->pfm_ctrl_reg);
  1659. bail:
  1660. if (rc)
  1661. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1662. else
  1663. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1664. return rc;
  1665. }
  1666. static int pm8xxx_vs_enable(struct regulator_dev *rdev)
  1667. {
  1668. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1669. int rc;
  1670. mutex_lock(&vreg->pc_lock);
  1671. if (vreg->pdata.ocp_enable) {
  1672. /* Disable OCP. */
  1673. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1674. VS_OCP_DISABLE, VS_OCP_MASK, &vreg->test_reg[0]);
  1675. if (rc)
  1676. goto done;
  1677. /* Enable the switch while OCP is disabled. */
  1678. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1679. VS_ENABLE | VS_MODE_NORMAL,
  1680. VS_ENABLE_MASK | VS_MODE_MASK,
  1681. &vreg->ctrl_reg);
  1682. if (rc)
  1683. goto done;
  1684. /* Wait for inrush current to subside, then enable OCP. */
  1685. udelay(vreg->pdata.ocp_enable_time);
  1686. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1687. VS_OCP_ENABLE, VS_OCP_MASK, &vreg->test_reg[0]);
  1688. } else {
  1689. /* Enable the switch without touching OCP. */
  1690. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, VS_ENABLE,
  1691. VS_ENABLE_MASK, &vreg->ctrl_reg);
  1692. }
  1693. done:
  1694. if (!rc)
  1695. vreg->is_enabled = true;
  1696. mutex_unlock(&vreg->pc_lock);
  1697. if (rc)
  1698. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1699. else
  1700. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1701. return rc;
  1702. }
  1703. static int pm8xxx_vs_disable(struct regulator_dev *rdev)
  1704. {
  1705. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1706. int rc;
  1707. mutex_lock(&vreg->pc_lock);
  1708. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, VS_DISABLE,
  1709. VS_ENABLE_MASK, &vreg->ctrl_reg);
  1710. if (!rc)
  1711. vreg->is_enabled = false;
  1712. mutex_unlock(&vreg->pc_lock);
  1713. if (rc)
  1714. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1715. else
  1716. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1717. return rc;
  1718. }
  1719. static int pm8xxx_vs300_enable(struct regulator_dev *rdev)
  1720. {
  1721. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1722. int rc;
  1723. if (vreg->pdata.ocp_enable) {
  1724. /* Disable OCP. */
  1725. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1726. VS_OCP_DISABLE, VS_OCP_MASK, &vreg->test_reg[0]);
  1727. if (rc)
  1728. goto done;
  1729. /* Enable the switch while OCP is disabled. */
  1730. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1731. VS300_CTRL_ENABLE | VS300_MODE_NORMAL,
  1732. VS300_CTRL_ENABLE_MASK | VS300_MODE_MASK,
  1733. &vreg->ctrl_reg);
  1734. if (rc)
  1735. goto done;
  1736. /* Wait for inrush current to subside, then enable OCP. */
  1737. udelay(vreg->pdata.ocp_enable_time);
  1738. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1739. VS_OCP_ENABLE, VS_OCP_MASK, &vreg->test_reg[0]);
  1740. } else {
  1741. /* Enable the regulator without touching OCP. */
  1742. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1743. VS300_CTRL_ENABLE, VS300_CTRL_ENABLE_MASK,
  1744. &vreg->ctrl_reg);
  1745. }
  1746. done:
  1747. if (rc) {
  1748. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1749. } else {
  1750. vreg->is_enabled = true;
  1751. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1752. }
  1753. return rc;
  1754. }
  1755. static int pm8xxx_vs300_disable(struct regulator_dev *rdev)
  1756. {
  1757. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1758. int rc;
  1759. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, VS300_CTRL_DISABLE,
  1760. VS300_CTRL_ENABLE_MASK, &vreg->ctrl_reg);
  1761. if (rc)
  1762. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1763. else
  1764. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1765. return rc;
  1766. }
  1767. static int pm8xxx_ncp_enable(struct regulator_dev *rdev)
  1768. {
  1769. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1770. int rc;
  1771. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NCP_ENABLE,
  1772. NCP_ENABLE_MASK, &vreg->ctrl_reg);
  1773. if (rc)
  1774. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1775. else
  1776. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1777. return rc;
  1778. }
  1779. static int pm8xxx_ncp_disable(struct regulator_dev *rdev)
  1780. {
  1781. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1782. int rc;
  1783. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, NCP_DISABLE,
  1784. NCP_ENABLE_MASK, &vreg->ctrl_reg);
  1785. if (rc)
  1786. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1787. else
  1788. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1789. return rc;
  1790. }
  1791. static int pm8xxx_boost_enable(struct regulator_dev *rdev)
  1792. {
  1793. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1794. int rc;
  1795. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, BOOST_ENABLE,
  1796. BOOST_ENABLE_MASK, &vreg->ctrl_reg);
  1797. if (rc)
  1798. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1799. else
  1800. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_ENABLE);
  1801. return rc;
  1802. }
  1803. static int pm8xxx_boost_disable(struct regulator_dev *rdev)
  1804. {
  1805. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1806. int rc;
  1807. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, BOOST_DISABLE,
  1808. BOOST_ENABLE_MASK, &vreg->ctrl_reg);
  1809. if (rc)
  1810. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1811. else
  1812. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_DISABLE);
  1813. return rc;
  1814. }
  1815. static int pm8xxx_ldo_pin_control_enable(struct regulator_dev *rdev)
  1816. {
  1817. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1818. int rc = 0;
  1819. int bank;
  1820. u8 val = 0;
  1821. u8 mask;
  1822. mutex_lock(&vreg->pc_lock);
  1823. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
  1824. val |= LDO_TEST_PIN_CTRL_EN0;
  1825. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
  1826. val |= LDO_TEST_PIN_CTRL_EN1;
  1827. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
  1828. val |= LDO_TEST_PIN_CTRL_EN2;
  1829. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
  1830. val |= LDO_TEST_PIN_CTRL_EN3;
  1831. bank = (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE ? 5 : 6);
  1832. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1833. val | REGULATOR_BANK_SEL(bank) | REGULATOR_BANK_WRITE,
  1834. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  1835. &vreg->test_reg[bank]);
  1836. if (rc)
  1837. goto bail;
  1838. /* Unset pin control bits in unused bank. */
  1839. bank = (bank == 5 ? 6 : 5);
  1840. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1841. REGULATOR_BANK_SEL(bank) | REGULATOR_BANK_WRITE,
  1842. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  1843. &vreg->test_reg[bank]);
  1844. if (rc)
  1845. goto bail;
  1846. val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
  1847. | REGULATOR_BANK_SEL(0);
  1848. mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
  1849. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr, val, mask,
  1850. &vreg->test_reg[0]);
  1851. if (rc)
  1852. goto bail;
  1853. if (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE) {
  1854. /* Pin control ON/OFF */
  1855. val = LDO_CTRL_PM_HPM;
  1856. /* Leave physically enabled if already enabled. */
  1857. val |= (vreg->is_enabled ? LDO_ENABLE : LDO_DISABLE);
  1858. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
  1859. LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1860. if (rc)
  1861. goto bail;
  1862. } else {
  1863. /* Pin control LPM/HPM */
  1864. val = LDO_ENABLE;
  1865. /* Leave in HPM if already enabled in HPM. */
  1866. val |= (vreg->is_enabled && vreg->mode == REGULATOR_MODE_NORMAL
  1867. ? LDO_CTRL_PM_HPM : LDO_CTRL_PM_LPM);
  1868. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
  1869. LDO_ENABLE_MASK | LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1870. if (rc)
  1871. goto bail;
  1872. }
  1873. bail:
  1874. if (!rc)
  1875. vreg->is_enabled_pc = true;
  1876. mutex_unlock(&vreg->pc_lock);
  1877. if (rc)
  1878. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1879. else
  1880. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
  1881. return rc;
  1882. }
  1883. static int pm8xxx_ldo_pin_control_disable(struct regulator_dev *rdev)
  1884. {
  1885. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1886. int rc;
  1887. mutex_lock(&vreg->pc_lock);
  1888. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1889. REGULATOR_BANK_SEL(5) | REGULATOR_BANK_WRITE,
  1890. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  1891. &vreg->test_reg[5]);
  1892. if (rc)
  1893. goto bail;
  1894. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1895. REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
  1896. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  1897. &vreg->test_reg[6]);
  1898. /*
  1899. * Physically disable the regulator if it was enabled in HPM/LPM pin
  1900. * control mode previously and it logically should not be enabled.
  1901. */
  1902. if ((vreg->ctrl_reg & LDO_ENABLE_MASK) == LDO_ENABLE
  1903. && !vreg->is_enabled) {
  1904. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1905. LDO_DISABLE, LDO_ENABLE_MASK, &vreg->ctrl_reg);
  1906. if (rc)
  1907. goto bail;
  1908. }
  1909. /* Change to LPM if LPM was enabled. */
  1910. if (vreg->is_enabled && vreg->mode == REGULATOR_MODE_IDLE) {
  1911. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1912. LDO_CTRL_PM_LPM, LDO_CTRL_PM_MASK, &vreg->ctrl_reg);
  1913. if (rc)
  1914. goto bail;
  1915. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  1916. LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
  1917. | REGULATOR_BANK_SEL(0),
  1918. LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK,
  1919. &vreg->test_reg[0]);
  1920. if (rc)
  1921. goto bail;
  1922. }
  1923. bail:
  1924. if (!rc)
  1925. vreg->is_enabled_pc = false;
  1926. mutex_unlock(&vreg->pc_lock);
  1927. if (rc)
  1928. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1929. else
  1930. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
  1931. return rc;
  1932. }
  1933. static int pm8xxx_smps_pin_control_enable(struct regulator_dev *rdev)
  1934. {
  1935. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  1936. int rc = 0;
  1937. u8 val = 0;
  1938. mutex_lock(&vreg->pc_lock);
  1939. if (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE) {
  1940. /* Pin control ON/OFF */
  1941. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
  1942. val |= SMPS_PIN_CTRL_EN0;
  1943. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
  1944. val |= SMPS_PIN_CTRL_EN1;
  1945. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
  1946. val |= SMPS_PIN_CTRL_EN2;
  1947. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
  1948. val |= SMPS_PIN_CTRL_EN3;
  1949. } else {
  1950. /* Pin control LPM/HPM */
  1951. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
  1952. val |= SMPS_PIN_CTRL_LPM_EN0;
  1953. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
  1954. val |= SMPS_PIN_CTRL_LPM_EN1;
  1955. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
  1956. val |= SMPS_PIN_CTRL_LPM_EN2;
  1957. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
  1958. val |= SMPS_PIN_CTRL_LPM_EN3;
  1959. }
  1960. rc = pm8xxx_smps_set_voltage_legacy(vreg, vreg->save_uV, vreg->save_uV);
  1961. if (rc)
  1962. goto bail;
  1963. rc = pm8xxx_vreg_masked_write(vreg, vreg->sleep_ctrl_addr, val,
  1964. SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
  1965. &vreg->sleep_ctrl_reg);
  1966. if (rc)
  1967. goto bail;
  1968. /*
  1969. * Physically enable the regulator if using HPM/LPM pin control mode or
  1970. * if the regulator should be logically left on.
  1971. */
  1972. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  1973. ((vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_MODE
  1974. || vreg->is_enabled) ?
  1975. SMPS_LEGACY_ENABLE : SMPS_LEGACY_DISABLE),
  1976. SMPS_LEGACY_ENABLE_MASK, &vreg->ctrl_reg);
  1977. if (rc)
  1978. goto bail;
  1979. /*
  1980. * Set regulator to HPM if using on/off pin control or if the regulator
  1981. * is already enabled in HPM. Otherwise, set it to LPM.
  1982. */
  1983. rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
  1984. (vreg->pdata.pin_fn == PM8XXX_VREG_PIN_FN_ENABLE
  1985. || (vreg->is_enabled
  1986. && vreg->mode == REGULATOR_MODE_NORMAL)
  1987. ? SMPS_CLK_CTRL_PWM : SMPS_CLK_CTRL_PFM),
  1988. SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
  1989. bail:
  1990. if (!rc)
  1991. vreg->is_enabled_pc = true;
  1992. mutex_unlock(&vreg->pc_lock);
  1993. if (rc)
  1994. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  1995. else
  1996. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
  1997. return rc;
  1998. }
  1999. static int pm8xxx_smps_pin_control_disable(struct regulator_dev *rdev)
  2000. {
  2001. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  2002. int rc;
  2003. mutex_lock(&vreg->pc_lock);
  2004. rc = pm8xxx_vreg_masked_write(vreg, vreg->sleep_ctrl_addr, 0,
  2005. SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
  2006. &vreg->sleep_ctrl_reg);
  2007. if (rc)
  2008. goto bail;
  2009. /*
  2010. * Physically disable the regulator if it was enabled in HPM/LPM pin
  2011. * control mode previously and it logically should not be enabled.
  2012. */
  2013. if ((vreg->ctrl_reg & SMPS_LEGACY_ENABLE_MASK) == SMPS_LEGACY_ENABLE
  2014. && vreg->is_enabled == false) {
  2015. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  2016. SMPS_LEGACY_DISABLE, SMPS_LEGACY_ENABLE_MASK,
  2017. &vreg->ctrl_reg);
  2018. if (rc)
  2019. goto bail;
  2020. }
  2021. /* Change to LPM if LPM was enabled. */
  2022. if (vreg->is_enabled && vreg->mode == REGULATOR_MODE_IDLE) {
  2023. rc = pm8xxx_vreg_masked_write(vreg, vreg->clk_ctrl_addr,
  2024. SMPS_CLK_CTRL_PFM, SMPS_CLK_CTRL_MASK,
  2025. &vreg->clk_ctrl_reg);
  2026. if (rc)
  2027. goto bail;
  2028. }
  2029. rc = pm8xxx_smps_set_voltage_advanced(vreg, vreg->save_uV,
  2030. vreg->save_uV, 0);
  2031. bail:
  2032. if (!rc)
  2033. vreg->is_enabled_pc = false;
  2034. mutex_unlock(&vreg->pc_lock);
  2035. if (rc)
  2036. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  2037. else
  2038. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
  2039. return rc;
  2040. }
  2041. static int pm8xxx_vs_pin_control_enable(struct regulator_dev *rdev)
  2042. {
  2043. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  2044. int rc;
  2045. u8 val = 0;
  2046. mutex_lock(&vreg->pc_lock);
  2047. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN0)
  2048. val |= VS_PIN_CTRL_EN0;
  2049. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN1)
  2050. val |= VS_PIN_CTRL_EN1;
  2051. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN2)
  2052. val |= VS_PIN_CTRL_EN2;
  2053. if (vreg->pdata.pin_ctrl & PM8XXX_VREG_PIN_CTRL_EN3)
  2054. val |= VS_PIN_CTRL_EN3;
  2055. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, val,
  2056. VS_PIN_CTRL_MASK | VS_ENABLE_MASK, &vreg->ctrl_reg);
  2057. if (!rc)
  2058. vreg->is_enabled_pc = true;
  2059. mutex_unlock(&vreg->pc_lock);
  2060. if (rc)
  2061. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  2062. else
  2063. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
  2064. return rc;
  2065. }
  2066. static int pm8xxx_vs_pin_control_disable(struct regulator_dev *rdev)
  2067. {
  2068. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  2069. int rc;
  2070. mutex_lock(&vreg->pc_lock);
  2071. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr, 0,
  2072. VS_PIN_CTRL_MASK, &vreg->ctrl_reg);
  2073. if (!rc)
  2074. vreg->is_enabled_pc = false;
  2075. mutex_unlock(&vreg->pc_lock);
  2076. if (rc)
  2077. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  2078. else
  2079. pm8xxx_vreg_show_state(rdev, PM8XXX_REGULATOR_ACTION_PIN_CTRL);
  2080. return rc;
  2081. }
  2082. static int pm8xxx_enable_time(struct regulator_dev *rdev)
  2083. {
  2084. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  2085. return vreg->pdata.enable_time;
  2086. }
  2087. static const char const *pm8xxx_print_actions[] = {
  2088. [PM8XXX_REGULATOR_ACTION_INIT] = "initial ",
  2089. [PM8XXX_REGULATOR_ACTION_ENABLE] = "enable ",
  2090. [PM8XXX_REGULATOR_ACTION_DISABLE] = "disable ",
  2091. [PM8XXX_REGULATOR_ACTION_VOLTAGE] = "set voltage",
  2092. [PM8XXX_REGULATOR_ACTION_MODE] = "set mode ",
  2093. [PM8XXX_REGULATOR_ACTION_PIN_CTRL] = "pin control",
  2094. };
  2095. static void pm8xxx_vreg_show_state(struct regulator_dev *rdev,
  2096. enum pm8xxx_regulator_action action)
  2097. {
  2098. struct pm8xxx_vreg *vreg = rdev_get_drvdata(rdev);
  2099. int uV, pc;
  2100. unsigned int mode;
  2101. const char *pc_en0 = "", *pc_en1 = "", *pc_en2 = "", *pc_en3 = "";
  2102. const char *pc_total = "";
  2103. const char *action_label = pm8xxx_print_actions[action];
  2104. const char *enable_label;
  2105. mutex_lock(&vreg->pc_lock);
  2106. /*
  2107. * Do not print unless REQUEST is specified and SSBI writes have taken
  2108. * place, or DUPLICATE is specified.
  2109. */
  2110. if (!((pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_DUPLICATE)
  2111. || ((pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_REQUEST)
  2112. && (vreg->write_count != vreg->prev_write_count)))) {
  2113. mutex_unlock(&vreg->pc_lock);
  2114. return;
  2115. }
  2116. vreg->prev_write_count = vreg->write_count;
  2117. pc = vreg->pdata.pin_ctrl;
  2118. if (vreg->is_enabled_pc) {
  2119. if (pc & PM8XXX_VREG_PIN_CTRL_EN0)
  2120. pc_en0 = " EN0";
  2121. if (pc & PM8XXX_VREG_PIN_CTRL_EN1)
  2122. pc_en1 = " EN1";
  2123. if (pc & PM8XXX_VREG_PIN_CTRL_EN2)
  2124. pc_en2 = " EN2";
  2125. if (pc & PM8XXX_VREG_PIN_CTRL_EN3)
  2126. pc_en3 = " EN3";
  2127. if (pc == PM8XXX_VREG_PIN_CTRL_NONE)
  2128. pc_total = " none";
  2129. } else {
  2130. pc_total = " none";
  2131. }
  2132. mutex_unlock(&vreg->pc_lock);
  2133. enable_label = pm8xxx_vreg_is_enabled(rdev) ? "on " : "off";
  2134. switch (vreg->type) {
  2135. case PM8XXX_REGULATOR_TYPE_PLDO:
  2136. uV = pm8xxx_pldo_get_voltage(rdev);
  2137. mode = pm8xxx_ldo_get_mode(rdev);
  2138. pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
  2139. action_label, vreg->rdesc.name, enable_label, uV,
  2140. (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
  2141. pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
  2142. break;
  2143. case PM8XXX_REGULATOR_TYPE_NLDO:
  2144. uV = pm8xxx_nldo_get_voltage(rdev);
  2145. mode = pm8xxx_ldo_get_mode(rdev);
  2146. pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
  2147. action_label, vreg->rdesc.name, enable_label, uV,
  2148. (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
  2149. pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
  2150. break;
  2151. case PM8XXX_REGULATOR_TYPE_NLDO1200:
  2152. uV = pm8xxx_nldo1200_get_voltage(rdev);
  2153. mode = pm8xxx_nldo1200_get_mode(rdev);
  2154. pr_info("%s %-9s: %s, v=%7d uV, mode=%s\n",
  2155. action_label, vreg->rdesc.name, enable_label, uV,
  2156. (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"));
  2157. break;
  2158. case PM8XXX_REGULATOR_TYPE_SMPS:
  2159. uV = pm8xxx_smps_get_voltage(rdev);
  2160. mode = pm8xxx_smps_get_mode(rdev);
  2161. pr_info("%s %-9s: %s, v=%7d uV, mode=%s, pc=%s%s%s%s%s\n",
  2162. action_label, vreg->rdesc.name, enable_label, uV,
  2163. (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"),
  2164. pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
  2165. break;
  2166. case PM8XXX_REGULATOR_TYPE_FTSMPS:
  2167. uV = pm8xxx_ftsmps_get_voltage(rdev);
  2168. mode = pm8xxx_ftsmps_get_mode(rdev);
  2169. pr_info("%s %-9s: %s, v=%7d uV, mode=%s\n",
  2170. action_label, vreg->rdesc.name, enable_label, uV,
  2171. (mode == REGULATOR_MODE_NORMAL ? "HPM" : "LPM"));
  2172. break;
  2173. case PM8XXX_REGULATOR_TYPE_VS:
  2174. pr_info("%s %-9s: %s, pc=%s%s%s%s%s\n",
  2175. action_label, vreg->rdesc.name, enable_label,
  2176. pc_en0, pc_en1, pc_en2, pc_en3, pc_total);
  2177. break;
  2178. case PM8XXX_REGULATOR_TYPE_VS300:
  2179. pr_info("%s %-9s: %s\n",
  2180. action_label, vreg->rdesc.name, enable_label);
  2181. break;
  2182. case PM8XXX_REGULATOR_TYPE_NCP:
  2183. uV = pm8xxx_ncp_get_voltage(rdev);
  2184. pr_info("%s %-9s: %s, v=%7d uV\n",
  2185. action_label, vreg->rdesc.name, enable_label, uV);
  2186. break;
  2187. case PM8XXX_REGULATOR_TYPE_BOOST:
  2188. uV = pm8xxx_boost_get_voltage(rdev);
  2189. pr_info("%s %-9s: %s, v=%7d uV\n",
  2190. action_label, vreg->rdesc.name, enable_label, uV);
  2191. break;
  2192. default:
  2193. break;
  2194. }
  2195. }
  2196. /* Real regulator operations. */
  2197. static struct regulator_ops pm8xxx_pldo_ops = {
  2198. .enable = pm8xxx_ldo_enable,
  2199. .disable = pm8xxx_ldo_disable,
  2200. .is_enabled = pm8xxx_vreg_is_enabled,
  2201. .set_voltage = pm8xxx_pldo_set_voltage,
  2202. .get_voltage = pm8xxx_pldo_get_voltage,
  2203. .list_voltage = pm8xxx_pldo_list_voltage,
  2204. .set_mode = pm8xxx_ldo_set_mode,
  2205. .get_mode = pm8xxx_ldo_get_mode,
  2206. .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
  2207. .enable_time = pm8xxx_enable_time,
  2208. };
  2209. static struct regulator_ops pm8xxx_nldo_ops = {
  2210. .enable = pm8xxx_ldo_enable,
  2211. .disable = pm8xxx_ldo_disable,
  2212. .is_enabled = pm8xxx_vreg_is_enabled,
  2213. .set_voltage = pm8xxx_nldo_set_voltage,
  2214. .get_voltage = pm8xxx_nldo_get_voltage,
  2215. .list_voltage = pm8xxx_nldo_list_voltage,
  2216. .set_mode = pm8xxx_ldo_set_mode,
  2217. .get_mode = pm8xxx_ldo_get_mode,
  2218. .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
  2219. .enable_time = pm8xxx_enable_time,
  2220. };
  2221. static struct regulator_ops pm8xxx_nldo1200_ops = {
  2222. .enable = pm8xxx_nldo1200_enable,
  2223. .disable = pm8xxx_nldo1200_disable,
  2224. .is_enabled = pm8xxx_vreg_is_enabled,
  2225. .set_voltage = pm8xxx_nldo1200_set_voltage,
  2226. .get_voltage = pm8xxx_nldo1200_get_voltage,
  2227. .list_voltage = pm8xxx_nldo1200_list_voltage,
  2228. .set_mode = pm8xxx_nldo1200_set_mode,
  2229. .get_mode = pm8xxx_nldo1200_get_mode,
  2230. .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
  2231. .enable_time = pm8xxx_enable_time,
  2232. };
  2233. static struct regulator_ops pm8xxx_smps_ops = {
  2234. .enable = pm8xxx_smps_enable,
  2235. .disable = pm8xxx_smps_disable,
  2236. .is_enabled = pm8xxx_vreg_is_enabled,
  2237. .set_voltage = pm8xxx_smps_set_voltage,
  2238. .get_voltage = pm8xxx_smps_get_voltage,
  2239. .list_voltage = pm8xxx_smps_list_voltage,
  2240. .set_mode = pm8xxx_smps_set_mode,
  2241. .get_mode = pm8xxx_smps_get_mode,
  2242. .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
  2243. .enable_time = pm8xxx_enable_time,
  2244. };
  2245. static struct regulator_ops pm8xxx_ftsmps_ops = {
  2246. .enable = pm8xxx_ftsmps_enable,
  2247. .disable = pm8xxx_ftsmps_disable,
  2248. .is_enabled = pm8xxx_vreg_is_enabled,
  2249. .set_voltage = pm8xxx_ftsmps_set_voltage,
  2250. .get_voltage = pm8xxx_ftsmps_get_voltage,
  2251. .list_voltage = pm8xxx_ftsmps_list_voltage,
  2252. .set_mode = pm8xxx_ftsmps_set_mode,
  2253. .get_mode = pm8xxx_ftsmps_get_mode,
  2254. .get_optimum_mode = pm8xxx_vreg_get_optimum_mode,
  2255. .enable_time = pm8xxx_enable_time,
  2256. };
  2257. static struct regulator_ops pm8xxx_vs_ops = {
  2258. .enable = pm8xxx_vs_enable,
  2259. .disable = pm8xxx_vs_disable,
  2260. .is_enabled = pm8xxx_vreg_is_enabled,
  2261. .enable_time = pm8xxx_enable_time,
  2262. };
  2263. static struct regulator_ops pm8xxx_vs300_ops = {
  2264. .enable = pm8xxx_vs300_enable,
  2265. .disable = pm8xxx_vs300_disable,
  2266. .is_enabled = pm8xxx_vreg_is_enabled,
  2267. .enable_time = pm8xxx_enable_time,
  2268. };
  2269. static struct regulator_ops pm8xxx_ncp_ops = {
  2270. .enable = pm8xxx_ncp_enable,
  2271. .disable = pm8xxx_ncp_disable,
  2272. .is_enabled = pm8xxx_vreg_is_enabled,
  2273. .set_voltage = pm8xxx_ncp_set_voltage,
  2274. .get_voltage = pm8xxx_ncp_get_voltage,
  2275. .list_voltage = pm8xxx_ncp_list_voltage,
  2276. .enable_time = pm8xxx_enable_time,
  2277. };
  2278. static struct regulator_ops pm8xxx_boost_ops = {
  2279. .enable = pm8xxx_boost_enable,
  2280. .disable = pm8xxx_boost_disable,
  2281. .is_enabled = pm8xxx_vreg_is_enabled,
  2282. .set_voltage = pm8xxx_boost_set_voltage,
  2283. .get_voltage = pm8xxx_boost_get_voltage,
  2284. .list_voltage = pm8xxx_boost_list_voltage,
  2285. .enable_time = pm8xxx_enable_time,
  2286. };
  2287. /* Pin control regulator operations. */
  2288. static struct regulator_ops pm8xxx_ldo_pc_ops = {
  2289. .enable = pm8xxx_ldo_pin_control_enable,
  2290. .disable = pm8xxx_ldo_pin_control_disable,
  2291. .is_enabled = pm8xxx_vreg_pin_control_is_enabled,
  2292. };
  2293. static struct regulator_ops pm8xxx_smps_pc_ops = {
  2294. .enable = pm8xxx_smps_pin_control_enable,
  2295. .disable = pm8xxx_smps_pin_control_disable,
  2296. .is_enabled = pm8xxx_vreg_pin_control_is_enabled,
  2297. };
  2298. static struct regulator_ops pm8xxx_vs_pc_ops = {
  2299. .enable = pm8xxx_vs_pin_control_enable,
  2300. .disable = pm8xxx_vs_pin_control_disable,
  2301. .is_enabled = pm8xxx_vreg_pin_control_is_enabled,
  2302. };
  2303. static struct regulator_ops *pm8xxx_reg_ops[PM8XXX_REGULATOR_TYPE_MAX] = {
  2304. [PM8XXX_REGULATOR_TYPE_PLDO] = &pm8xxx_pldo_ops,
  2305. [PM8XXX_REGULATOR_TYPE_NLDO] = &pm8xxx_nldo_ops,
  2306. [PM8XXX_REGULATOR_TYPE_NLDO1200] = &pm8xxx_nldo1200_ops,
  2307. [PM8XXX_REGULATOR_TYPE_SMPS] = &pm8xxx_smps_ops,
  2308. [PM8XXX_REGULATOR_TYPE_FTSMPS] = &pm8xxx_ftsmps_ops,
  2309. [PM8XXX_REGULATOR_TYPE_VS] = &pm8xxx_vs_ops,
  2310. [PM8XXX_REGULATOR_TYPE_VS300] = &pm8xxx_vs300_ops,
  2311. [PM8XXX_REGULATOR_TYPE_NCP] = &pm8xxx_ncp_ops,
  2312. [PM8XXX_REGULATOR_TYPE_BOOST] = &pm8xxx_boost_ops,
  2313. };
  2314. static struct regulator_ops *pm8xxx_reg_pc_ops[PM8XXX_REGULATOR_TYPE_MAX] = {
  2315. [PM8XXX_REGULATOR_TYPE_PLDO] = &pm8xxx_ldo_pc_ops,
  2316. [PM8XXX_REGULATOR_TYPE_NLDO] = &pm8xxx_ldo_pc_ops,
  2317. [PM8XXX_REGULATOR_TYPE_SMPS] = &pm8xxx_smps_pc_ops,
  2318. [PM8XXX_REGULATOR_TYPE_VS] = &pm8xxx_vs_pc_ops,
  2319. };
  2320. static unsigned pm8xxx_n_voltages[PM8XXX_REGULATOR_TYPE_MAX] = {
  2321. [PM8XXX_REGULATOR_TYPE_PLDO] = PLDO_SET_POINTS,
  2322. [PM8XXX_REGULATOR_TYPE_NLDO] = NLDO_SET_POINTS,
  2323. [PM8XXX_REGULATOR_TYPE_NLDO1200] = NLDO1200_SET_POINTS,
  2324. [PM8XXX_REGULATOR_TYPE_SMPS] = SMPS_ADVANCED_SET_POINTS,
  2325. [PM8XXX_REGULATOR_TYPE_FTSMPS] = FTSMPS_SET_POINTS,
  2326. [PM8XXX_REGULATOR_TYPE_VS] = 0,
  2327. [PM8XXX_REGULATOR_TYPE_VS300] = 0,
  2328. [PM8XXX_REGULATOR_TYPE_NCP] = NCP_SET_POINTS,
  2329. [PM8XXX_REGULATOR_TYPE_BOOST] = BOOST_SET_POINTS,
  2330. };
  2331. static int pm8xxx_init_ldo(struct pm8xxx_vreg *vreg, bool is_real)
  2332. {
  2333. int rc = 0;
  2334. int i;
  2335. u8 bank;
  2336. /* Save the current control register state. */
  2337. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2338. if (rc)
  2339. goto bail;
  2340. /* Save the current test register state. */
  2341. for (i = 0; i < LDO_TEST_BANKS; i++) {
  2342. bank = REGULATOR_BANK_SEL(i);
  2343. rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
  2344. if (rc)
  2345. goto bail;
  2346. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  2347. &vreg->test_reg[i]);
  2348. if (rc)
  2349. goto bail;
  2350. vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
  2351. }
  2352. if (is_real) {
  2353. /* Set pull down enable based on platform data. */
  2354. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  2355. (vreg->pdata.pull_down_enable ? LDO_PULL_DOWN_ENABLE : 0),
  2356. LDO_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
  2357. vreg->is_enabled = !!_pm8xxx_vreg_is_enabled(vreg);
  2358. vreg->mode = ((vreg->ctrl_reg & LDO_CTRL_PM_MASK)
  2359. == LDO_CTRL_PM_LPM ?
  2360. REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
  2361. }
  2362. bail:
  2363. if (rc)
  2364. vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
  2365. return rc;
  2366. }
  2367. static int pm8xxx_init_nldo1200(struct pm8xxx_vreg *vreg)
  2368. {
  2369. int rc = 0;
  2370. int i;
  2371. u8 bank;
  2372. /* Save the current control register state. */
  2373. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2374. if (rc)
  2375. goto bail;
  2376. /* Save the current test register state. */
  2377. for (i = 0; i < LDO_TEST_BANKS; i++) {
  2378. bank = REGULATOR_BANK_SEL(i);
  2379. rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
  2380. if (rc)
  2381. goto bail;
  2382. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  2383. &vreg->test_reg[i]);
  2384. if (rc)
  2385. goto bail;
  2386. vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
  2387. }
  2388. vreg->save_uV = _pm8xxx_nldo1200_get_voltage(vreg);
  2389. /* Set pull down enable based on platform data. */
  2390. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  2391. (vreg->pdata.pull_down_enable ? NLDO1200_PULL_DOWN_ENABLE : 0)
  2392. | REGULATOR_BANK_SEL(1) | REGULATOR_BANK_WRITE,
  2393. NLDO1200_PULL_DOWN_ENABLE_MASK | REGULATOR_BANK_MASK,
  2394. &vreg->test_reg[1]);
  2395. bail:
  2396. if (rc)
  2397. vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
  2398. return rc;
  2399. }
  2400. static int pm8xxx_init_smps(struct pm8xxx_vreg *vreg, bool is_real)
  2401. {
  2402. int rc = 0;
  2403. int i;
  2404. u8 bank;
  2405. /* Save the current control register state. */
  2406. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2407. if (rc)
  2408. goto bail;
  2409. /* Save the current test2 register state. */
  2410. for (i = 0; i < SMPS_TEST_BANKS; i++) {
  2411. bank = REGULATOR_BANK_SEL(i);
  2412. rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
  2413. if (rc)
  2414. goto bail;
  2415. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  2416. &vreg->test_reg[i]);
  2417. if (rc)
  2418. goto bail;
  2419. vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
  2420. }
  2421. /* Save the current clock control register state. */
  2422. rc = pm8xxx_readb(vreg->dev->parent, vreg->clk_ctrl_addr,
  2423. &vreg->clk_ctrl_reg);
  2424. if (rc)
  2425. goto bail;
  2426. /* Save the current sleep control register state. */
  2427. rc = pm8xxx_readb(vreg->dev->parent, vreg->sleep_ctrl_addr,
  2428. &vreg->sleep_ctrl_reg);
  2429. if (rc)
  2430. goto bail;
  2431. vreg->save_uV = _pm8xxx_smps_get_voltage(vreg);
  2432. if (is_real) {
  2433. /* Set advanced mode pull down enable based on platform data. */
  2434. rc = pm8xxx_vreg_masked_write(vreg, vreg->test_addr,
  2435. (vreg->pdata.pull_down_enable
  2436. ? SMPS_ADVANCED_PULL_DOWN_ENABLE : 0)
  2437. | REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
  2438. REGULATOR_BANK_MASK | SMPS_ADVANCED_PULL_DOWN_ENABLE,
  2439. &vreg->test_reg[6]);
  2440. if (rc)
  2441. goto bail;
  2442. vreg->is_enabled = !!_pm8xxx_vreg_is_enabled(vreg);
  2443. vreg->mode = ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK)
  2444. == SMPS_CLK_CTRL_PFM ?
  2445. REGULATOR_MODE_IDLE : REGULATOR_MODE_NORMAL);
  2446. }
  2447. if (!SMPS_IN_ADVANCED_MODE(vreg) && is_real) {
  2448. /* Set legacy mode pull down enable based on platform data. */
  2449. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  2450. (vreg->pdata.pull_down_enable
  2451. ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0),
  2452. SMPS_LEGACY_PULL_DOWN_ENABLE, &vreg->ctrl_reg);
  2453. if (rc)
  2454. goto bail;
  2455. }
  2456. bail:
  2457. if (rc)
  2458. vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
  2459. return rc;
  2460. }
  2461. static int pm8xxx_init_ftsmps(struct pm8xxx_vreg *vreg)
  2462. {
  2463. int rc, i;
  2464. u8 bank;
  2465. /* Save the current control register state. */
  2466. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2467. if (rc)
  2468. goto bail;
  2469. /* Store current regulator register values. */
  2470. rc = pm8xxx_readb(vreg->dev->parent, vreg->pfm_ctrl_addr,
  2471. &vreg->pfm_ctrl_reg);
  2472. if (rc)
  2473. goto bail;
  2474. rc = pm8xxx_readb(vreg->dev->parent, vreg->pwr_cnfg_addr,
  2475. &vreg->pwr_cnfg_reg);
  2476. if (rc)
  2477. goto bail;
  2478. /* Save the current fts_cnfg1 register state (uses 'test' member). */
  2479. for (i = 0; i < SMPS_TEST_BANKS; i++) {
  2480. bank = REGULATOR_BANK_SEL(i);
  2481. rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
  2482. if (rc)
  2483. goto bail;
  2484. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  2485. &vreg->test_reg[i]);
  2486. if (rc)
  2487. goto bail;
  2488. vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
  2489. }
  2490. vreg->save_uV = _pm8xxx_ftsmps_get_voltage(vreg);
  2491. /* Set pull down enable based on platform data. */
  2492. rc = pm8xxx_vreg_masked_write(vreg, vreg->pwr_cnfg_addr,
  2493. (vreg->pdata.pull_down_enable ? FTSMPS_PULL_DOWN_ENABLE : 0),
  2494. FTSMPS_PULL_DOWN_ENABLE_MASK, &vreg->pwr_cnfg_reg);
  2495. bail:
  2496. if (rc)
  2497. vreg_err(vreg, "pm8xxx_readb/writeb failed, rc=%d\n", rc);
  2498. return rc;
  2499. }
  2500. static int pm8xxx_init_vs(struct pm8xxx_vreg *vreg, bool is_real)
  2501. {
  2502. int rc = 0;
  2503. /* Save the current control register state. */
  2504. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2505. if (rc) {
  2506. vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
  2507. return rc;
  2508. }
  2509. /* Save the current test register state. */
  2510. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  2511. &vreg->test_reg[0]);
  2512. if (rc) {
  2513. vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
  2514. return rc;
  2515. }
  2516. if (is_real) {
  2517. /* Set pull down enable based on platform data. */
  2518. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  2519. (vreg->pdata.pull_down_enable ? VS_PULL_DOWN_ENABLE
  2520. : VS_PULL_DOWN_DISABLE),
  2521. VS_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
  2522. if (rc)
  2523. vreg_err(vreg,
  2524. "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  2525. vreg->is_enabled = !!_pm8xxx_vreg_is_enabled(vreg);
  2526. }
  2527. return rc;
  2528. }
  2529. static int pm8xxx_init_vs300(struct pm8xxx_vreg *vreg)
  2530. {
  2531. int rc;
  2532. /* Save the current control register state. */
  2533. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2534. if (rc) {
  2535. vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
  2536. return rc;
  2537. }
  2538. /* Save the current test register state. */
  2539. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  2540. &vreg->test_reg[0]);
  2541. if (rc) {
  2542. vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
  2543. return rc;
  2544. }
  2545. /* Set pull down enable based on platform data. */
  2546. rc = pm8xxx_vreg_masked_write(vreg, vreg->ctrl_addr,
  2547. (vreg->pdata.pull_down_enable ? VS300_PULL_DOWN_ENABLE : 0),
  2548. VS300_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
  2549. if (rc)
  2550. vreg_err(vreg, "pm8xxx_vreg_masked_write failed, rc=%d\n", rc);
  2551. return rc;
  2552. }
  2553. static int pm8xxx_init_ncp(struct pm8xxx_vreg *vreg)
  2554. {
  2555. int rc;
  2556. /* Save the current control register state. */
  2557. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2558. if (rc) {
  2559. vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
  2560. return rc;
  2561. }
  2562. return rc;
  2563. }
  2564. static int pm8xxx_init_boost(struct pm8xxx_vreg *vreg)
  2565. {
  2566. int rc;
  2567. /* Save the current control register state. */
  2568. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  2569. if (rc) {
  2570. vreg_err(vreg, "pm8xxx_readb failed, rc=%d\n", rc);
  2571. return rc;
  2572. }
  2573. return rc;
  2574. }
  2575. static int __devinit pm8xxx_vreg_probe(struct platform_device *pdev)
  2576. {
  2577. struct pm8xxx_regulator_core_platform_data *core_data;
  2578. const struct pm8xxx_regulator_platform_data *pdata;
  2579. enum pm8xxx_vreg_pin_function pin_fn;
  2580. struct regulator_desc *rdesc;
  2581. struct pm8xxx_vreg *vreg;
  2582. unsigned pin_ctrl;
  2583. int rc = 0;
  2584. if (pdev == NULL) {
  2585. pr_err("no platform device specified\n");
  2586. return -EINVAL;
  2587. }
  2588. core_data = pdev->dev.platform_data;
  2589. if (core_data == NULL) {
  2590. pr_err("no core data specified\n");
  2591. return -EINVAL;
  2592. }
  2593. pdata = core_data->pdata;
  2594. vreg = core_data->vreg;
  2595. if (pdata == NULL) {
  2596. pr_err("no pdata specified\n");
  2597. return -EINVAL;
  2598. } else if (vreg == NULL) {
  2599. pr_err("no vreg specified\n");
  2600. return -EINVAL;
  2601. }
  2602. if (vreg->rdesc.name == NULL) {
  2603. pr_err("regulator name missing\n");
  2604. return -EINVAL;
  2605. } else if (vreg->type < 0 || vreg->type >= PM8XXX_REGULATOR_TYPE_MAX) {
  2606. pr_err("%s: regulator type=%d is invalid\n", vreg->rdesc.name,
  2607. vreg->type);
  2608. return -EINVAL;
  2609. } else if (core_data->is_pin_controlled
  2610. && pm8xxx_reg_pc_ops[vreg->type] == NULL) {
  2611. pr_err("%s: regulator type=%d does not support pin control\n",
  2612. vreg->rdesc.name, vreg->type);
  2613. return -EINVAL;
  2614. } else if (core_data->is_pin_controlled
  2615. && vreg->rdesc_pc.name == NULL) {
  2616. pr_err("%s: regulator pin control name missing\n",
  2617. vreg->rdesc.name);
  2618. return -EINVAL;
  2619. }
  2620. if (core_data->is_pin_controlled)
  2621. rdesc = &vreg->rdesc_pc;
  2622. else
  2623. rdesc = &vreg->rdesc;
  2624. if (!pdata) {
  2625. pr_err("%s requires platform data\n", vreg->rdesc.name);
  2626. return -EINVAL;
  2627. }
  2628. rdesc->id = pdev->id;
  2629. rdesc->owner = THIS_MODULE;
  2630. rdesc->type = REGULATOR_VOLTAGE;
  2631. if (core_data->is_pin_controlled) {
  2632. rdesc->ops = pm8xxx_reg_pc_ops[vreg->type];
  2633. rdesc->n_voltages = 0;
  2634. } else {
  2635. rdesc->ops = pm8xxx_reg_ops[vreg->type];
  2636. rdesc->n_voltages = pm8xxx_n_voltages[vreg->type];
  2637. }
  2638. mutex_lock(&vreg->pc_lock);
  2639. if (!core_data->is_pin_controlled) {
  2640. /* Do not modify pin control and pin function values. */
  2641. pin_ctrl = vreg->pdata.pin_ctrl;
  2642. pin_fn = vreg->pdata.pin_fn;
  2643. memcpy(&(vreg->pdata), pdata,
  2644. sizeof(struct pm8xxx_regulator_platform_data));
  2645. vreg->pdata.pin_ctrl = pin_ctrl;
  2646. vreg->pdata.pin_fn = pin_fn;
  2647. /*
  2648. * If slew_rate isn't specified but enable_time is, then set
  2649. * slew_rate = max_uV / enable_time.
  2650. */
  2651. if (vreg->pdata.enable_time > 0
  2652. && vreg->pdata.init_data.constraints.max_uV > 0
  2653. && vreg->pdata.slew_rate <= 0)
  2654. vreg->pdata.slew_rate =
  2655. DIV_ROUND_UP(vreg->pdata.init_data.constraints.max_uV,
  2656. vreg->pdata.enable_time);
  2657. vreg->dev = &pdev->dev;
  2658. } else {
  2659. /* Pin control regulator */
  2660. if ((pdata->pin_ctrl & PM8XXX_VREG_PIN_CTRL_ALL)
  2661. == PM8XXX_VREG_PIN_CTRL_NONE) {
  2662. pr_err("%s: no pin control input specified\n",
  2663. vreg->rdesc.name);
  2664. mutex_unlock(&vreg->pc_lock);
  2665. return -EINVAL;
  2666. }
  2667. vreg->pdata.pin_ctrl = pdata->pin_ctrl;
  2668. vreg->pdata.pin_fn = pdata->pin_fn;
  2669. vreg->dev_pc = &pdev->dev;
  2670. if (!vreg->dev)
  2671. vreg->dev = &pdev->dev;
  2672. }
  2673. /* Initialize register values. */
  2674. switch (vreg->type) {
  2675. case PM8XXX_REGULATOR_TYPE_PLDO:
  2676. case PM8XXX_REGULATOR_TYPE_NLDO:
  2677. rc = pm8xxx_init_ldo(vreg, !core_data->is_pin_controlled);
  2678. break;
  2679. case PM8XXX_REGULATOR_TYPE_NLDO1200:
  2680. rc = pm8xxx_init_nldo1200(vreg);
  2681. break;
  2682. case PM8XXX_REGULATOR_TYPE_SMPS:
  2683. rc = pm8xxx_init_smps(vreg, !core_data->is_pin_controlled);
  2684. break;
  2685. case PM8XXX_REGULATOR_TYPE_FTSMPS:
  2686. rc = pm8xxx_init_ftsmps(vreg);
  2687. break;
  2688. case PM8XXX_REGULATOR_TYPE_VS:
  2689. rc = pm8xxx_init_vs(vreg, !core_data->is_pin_controlled);
  2690. break;
  2691. case PM8XXX_REGULATOR_TYPE_VS300:
  2692. rc = pm8xxx_init_vs300(vreg);
  2693. break;
  2694. case PM8XXX_REGULATOR_TYPE_NCP:
  2695. rc = pm8xxx_init_ncp(vreg);
  2696. break;
  2697. case PM8XXX_REGULATOR_TYPE_BOOST:
  2698. rc = pm8xxx_init_boost(vreg);
  2699. break;
  2700. default:
  2701. break;
  2702. }
  2703. mutex_unlock(&vreg->pc_lock);
  2704. if (rc)
  2705. goto bail;
  2706. if (!core_data->is_pin_controlled) {
  2707. vreg->rdev = regulator_register(rdesc, &pdev->dev,
  2708. &(pdata->init_data), vreg, NULL);
  2709. if (IS_ERR(vreg->rdev)) {
  2710. rc = PTR_ERR(vreg->rdev);
  2711. vreg->rdev = NULL;
  2712. pr_err("regulator_register failed: %s, rc=%d\n",
  2713. vreg->rdesc.name, rc);
  2714. }
  2715. } else {
  2716. vreg->rdev_pc = regulator_register(rdesc, &pdev->dev,
  2717. &(pdata->init_data), vreg, NULL);
  2718. if (IS_ERR(vreg->rdev_pc)) {
  2719. rc = PTR_ERR(vreg->rdev_pc);
  2720. vreg->rdev_pc = NULL;
  2721. pr_err("regulator_register failed: %s, rc=%d\n",
  2722. vreg->rdesc.name, rc);
  2723. }
  2724. }
  2725. if ((pm8xxx_vreg_debug_mask & PM8XXX_VREG_DEBUG_INIT) && !rc
  2726. && vreg->rdev)
  2727. pm8xxx_vreg_show_state(vreg->rdev,
  2728. PM8XXX_REGULATOR_ACTION_INIT);
  2729. platform_set_drvdata(pdev, core_data);
  2730. bail:
  2731. if (rc)
  2732. pr_err("error for %s, rc=%d\n", vreg->rdesc.name, rc);
  2733. return rc;
  2734. }
  2735. static int __devexit pm8xxx_vreg_remove(struct platform_device *pdev)
  2736. {
  2737. struct pm8xxx_regulator_core_platform_data *core_data;
  2738. core_data = platform_get_drvdata(pdev);
  2739. platform_set_drvdata(pdev, NULL);
  2740. if (core_data) {
  2741. if (core_data->is_pin_controlled)
  2742. regulator_unregister(core_data->vreg->rdev_pc);
  2743. else
  2744. regulator_unregister(core_data->vreg->rdev);
  2745. }
  2746. return 0;
  2747. }
  2748. static struct platform_driver pm8xxx_vreg_driver = {
  2749. .probe = pm8xxx_vreg_probe,
  2750. .remove = __devexit_p(pm8xxx_vreg_remove),
  2751. .driver = {
  2752. .name = PM8XXX_REGULATOR_DEV_NAME,
  2753. .owner = THIS_MODULE,
  2754. },
  2755. };
  2756. static int __init pm8xxx_vreg_init(void)
  2757. {
  2758. return platform_driver_register(&pm8xxx_vreg_driver);
  2759. }
  2760. postcore_initcall(pm8xxx_vreg_init);
  2761. static void __exit pm8xxx_vreg_exit(void)
  2762. {
  2763. platform_driver_unregister(&pm8xxx_vreg_driver);
  2764. }
  2765. module_exit(pm8xxx_vreg_exit);
  2766. MODULE_LICENSE("GPL v2");
  2767. MODULE_DESCRIPTION("PMIC PM8XXX regulator driver");
  2768. MODULE_VERSION("1.0");
  2769. MODULE_ALIAS("platform:" PM8XXX_REGULATOR_DEV_NAME);