pmic8058-regulator.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. /* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/err.h>
  13. #include <linux/string.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/bitops.h>
  18. #include <linux/mfd/pmic8058.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/mfd/pm8xxx/core.h>
  22. #include <linux/regulator/pmic8058-regulator.h>
  23. /* Regulator types */
  24. #define REGULATOR_TYPE_LDO 0
  25. #define REGULATOR_TYPE_SMPS 1
  26. #define REGULATOR_TYPE_LVS 2
  27. #define REGULATOR_TYPE_NCP 3
  28. /* Common masks */
  29. #define REGULATOR_EN_MASK 0x80
  30. #define REGULATOR_BANK_MASK 0xF0
  31. #define REGULATOR_BANK_SEL(n) ((n) << 4)
  32. #define REGULATOR_BANK_WRITE 0x80
  33. #define LDO_TEST_BANKS 7
  34. #define SMPS_TEST_BANKS 8
  35. #define REGULATOR_TEST_BANKS_MAX SMPS_TEST_BANKS
  36. /* LDO programming */
  37. /* CTRL register */
  38. #define LDO_ENABLE_MASK 0x80
  39. #define LDO_ENABLE 0x80
  40. #define LDO_PULL_DOWN_ENABLE_MASK 0x40
  41. #define LDO_PULL_DOWN_ENABLE 0x40
  42. #define LDO_CTRL_PM_MASK 0x20
  43. #define LDO_CTRL_PM_HPM 0x00
  44. #define LDO_CTRL_PM_LPM 0x20
  45. #define LDO_CTRL_VPROG_MASK 0x1F
  46. /* TEST register bank 0 */
  47. #define LDO_TEST_LPM_MASK 0x40
  48. #define LDO_TEST_LPM_SEL_CTRL 0x00
  49. #define LDO_TEST_LPM_SEL_TCXO 0x40
  50. /* TEST register bank 2 */
  51. #define LDO_TEST_VPROG_UPDATE_MASK 0x08
  52. #define LDO_TEST_RANGE_SEL_MASK 0x04
  53. #define LDO_TEST_FINE_STEP_MASK 0x02
  54. #define LDO_TEST_FINE_STEP_SHIFT 1
  55. /* TEST register bank 4 */
  56. #define LDO_TEST_RANGE_EXT_MASK 0x01
  57. /* TEST register bank 5 */
  58. #define LDO_TEST_PIN_CTRL_MASK 0x0F
  59. #define LDO_TEST_PIN_CTRL_EN3 0x08
  60. #define LDO_TEST_PIN_CTRL_EN2 0x04
  61. #define LDO_TEST_PIN_CTRL_EN1 0x02
  62. #define LDO_TEST_PIN_CTRL_EN0 0x01
  63. /* TEST register bank 6 */
  64. #define LDO_TEST_PIN_CTRL_LPM_MASK 0x0F
  65. /* Allowable voltage ranges */
  66. #define PLDO_LOW_UV_MIN 750000
  67. #define PLDO_LOW_UV_MAX 1537500
  68. #define PLDO_LOW_FINE_STEP_UV 12500
  69. #define PLDO_NORM_UV_MIN 1500000
  70. #define PLDO_NORM_UV_MAX 3075000
  71. #define PLDO_NORM_FINE_STEP_UV 25000
  72. #define PLDO_HIGH_UV_MIN 1750000
  73. #define PLDO_HIGH_UV_MAX 4900000
  74. #define PLDO_HIGH_FINE_STEP_UV 50000
  75. #define NLDO_UV_MIN 750000
  76. #define NLDO_UV_MAX 1537500
  77. #define NLDO_FINE_STEP_UV 12500
  78. /* SMPS masks and values */
  79. /* CTRL register */
  80. /* Legacy mode */
  81. #define SMPS_LEGACY_ENABLE 0x80
  82. #define SMPS_LEGACY_PULL_DOWN_ENABLE 0x40
  83. #define SMPS_LEGACY_VREF_SEL_MASK 0x20
  84. #define SMPS_LEGACY_VPROG_MASK 0x1F
  85. /* Advanced mode */
  86. #define SMPS_ADVANCED_BAND_MASK 0xC0
  87. #define SMPS_ADVANCED_BAND_OFF 0x00
  88. #define SMPS_ADVANCED_BAND_1 0x40
  89. #define SMPS_ADVANCED_BAND_2 0x80
  90. #define SMPS_ADVANCED_BAND_3 0xC0
  91. #define SMPS_ADVANCED_VPROG_MASK 0x3F
  92. /* Legacy mode voltage ranges */
  93. #define SMPS_MODE1_UV_MIN 1500000
  94. #define SMPS_MODE1_UV_MAX 3050000
  95. #define SMPS_MODE1_UV_STEP 50000
  96. #define SMPS_MODE2_UV_MIN 750000
  97. #define SMPS_MODE2_UV_MAX 1525000
  98. #define SMPS_MODE2_UV_STEP 25000
  99. #define SMPS_MODE3_UV_MIN 375000
  100. #define SMPS_MODE3_UV_MAX 1150000
  101. #define SMPS_MODE3_UV_STEP 25000
  102. /* Advanced mode voltage ranges */
  103. #define SMPS_BAND3_UV_MIN 1500000
  104. #define SMPS_BAND3_UV_MAX 3075000
  105. #define SMPS_BAND3_UV_STEP 25000
  106. #define SMPS_BAND2_UV_MIN 750000
  107. #define SMPS_BAND2_UV_MAX 1537500
  108. #define SMPS_BAND2_UV_STEP 12500
  109. #define SMPS_BAND1_UV_MIN 375000
  110. #define SMPS_BAND1_UV_MAX 1162500
  111. #define SMPS_BAND1_UV_STEP 12500
  112. #define SMPS_UV_MIN SMPS_MODE3_UV_MIN
  113. #define SMPS_UV_MAX SMPS_MODE1_UV_MAX
  114. /* Test2 register bank 1 */
  115. #define SMPS_LEGACY_VLOW_SEL_MASK 0x01
  116. /* Test2 register bank 6 */
  117. #define SMPS_ADVANCED_PULL_DOWN_ENABLE 0x08
  118. /* Test2 register bank 7 */
  119. #define SMPS_ADVANCED_MODE_MASK 0x02
  120. #define SMPS_ADVANCED_MODE 0x02
  121. #define SMPS_LEGACY_MODE 0x00
  122. #define SMPS_IN_ADVANCED_MODE(vreg) \
  123. ((vreg->test_reg[7] & SMPS_ADVANCED_MODE_MASK) == SMPS_ADVANCED_MODE)
  124. /* BUCK_SLEEP_CNTRL register */
  125. #define SMPS_PIN_CTRL_MASK 0xF0
  126. #define SMPS_PIN_CTRL_A1 0x80
  127. #define SMPS_PIN_CTRL_A0 0x40
  128. #define SMPS_PIN_CTRL_D1 0x20
  129. #define SMPS_PIN_CTRL_D0 0x10
  130. #define SMPS_PIN_CTRL_LPM_MASK 0x0F
  131. #define SMPS_PIN_CTRL_LPM_A1 0x08
  132. #define SMPS_PIN_CTRL_LPM_A0 0x04
  133. #define SMPS_PIN_CTRL_LPM_D1 0x02
  134. #define SMPS_PIN_CTRL_LPM_D0 0x01
  135. /* BUCK_CLOCK_CNTRL register */
  136. #define SMPS_CLK_DIVIDE2 0x40
  137. #define SMPS_CLK_CTRL_MASK 0x30
  138. #define SMPS_CLK_CTRL_FOLLOW_TCXO 0x00
  139. #define SMPS_CLK_CTRL_PWM 0x10
  140. #define SMPS_CLK_CTRL_PFM 0x20
  141. /* LVS masks and values */
  142. /* CTRL register */
  143. #define LVS_ENABLE_MASK 0x80
  144. #define LVS_ENABLE 0x80
  145. #define LVS_PULL_DOWN_ENABLE_MASK 0x40
  146. #define LVS_PULL_DOWN_ENABLE 0x00
  147. #define LVS_PULL_DOWN_DISABLE 0x40
  148. #define LVS_PIN_CTRL_MASK 0x0F
  149. #define LVS_PIN_CTRL_EN0 0x08
  150. #define LVS_PIN_CTRL_EN1 0x04
  151. #define LVS_PIN_CTRL_EN2 0x02
  152. #define LVS_PIN_CTRL_EN3 0x01
  153. /* NCP masks and values */
  154. /* CTRL register */
  155. #define NCP_VPROG_MASK 0x1F
  156. #define NCP_UV_MIN 1500000
  157. #define NCP_UV_MAX 3050000
  158. #define NCP_UV_STEP 50000
  159. #define GLOBAL_ENABLE_MAX (2)
  160. struct pm8058_enable {
  161. u16 addr;
  162. u8 reg;
  163. };
  164. struct pm8058_vreg {
  165. struct device *dev;
  166. struct pm8058_vreg_pdata *pdata;
  167. struct regulator_dev *rdev;
  168. struct pm8058_enable *global_enable[GLOBAL_ENABLE_MAX];
  169. int hpm_min_load;
  170. int save_uV;
  171. unsigned pc_vote;
  172. unsigned optimum;
  173. unsigned mode_initialized;
  174. u16 ctrl_addr;
  175. u16 test_addr;
  176. u16 clk_ctrl_addr;
  177. u16 sleep_ctrl_addr;
  178. u8 type;
  179. u8 ctrl_reg;
  180. u8 test_reg[REGULATOR_TEST_BANKS_MAX];
  181. u8 clk_ctrl_reg;
  182. u8 sleep_ctrl_reg;
  183. u8 is_nmos;
  184. u8 global_enable_mask[GLOBAL_ENABLE_MAX];
  185. };
  186. #define LDO_M2(_id, _ctrl_addr, _test_addr, _is_nmos, _hpm_min_load, \
  187. _en0, _en0_mask, _en1, _en1_mask) \
  188. [PM8058_VREG_ID_##_id] = { \
  189. .ctrl_addr = _ctrl_addr, \
  190. .test_addr = _test_addr, \
  191. .type = REGULATOR_TYPE_LDO, \
  192. .hpm_min_load = PM8058_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
  193. .is_nmos = _is_nmos, \
  194. .global_enable = { \
  195. [0] = _en0, \
  196. [1] = _en1, \
  197. }, \
  198. .global_enable_mask = { \
  199. [0] = _en0_mask, \
  200. [1] = _en1_mask, \
  201. }, \
  202. }
  203. #define LDO(_id, _ctrl_addr, _test_addr, _is_nmos, _hpm_min_load, \
  204. _en0, _en0_mask) \
  205. LDO_M2(_id, _ctrl_addr, _test_addr, _is_nmos, _hpm_min_load, \
  206. _en0, _en0_mask, NULL, 0)
  207. #define SMPS(_id, _ctrl_addr, _test_addr, _clk_ctrl_addr, _sleep_ctrl_addr, \
  208. _hpm_min_load, _en0, _en0_mask) \
  209. [PM8058_VREG_ID_##_id] = { \
  210. .ctrl_addr = _ctrl_addr, \
  211. .test_addr = _test_addr, \
  212. .clk_ctrl_addr = _clk_ctrl_addr, \
  213. .sleep_ctrl_addr = _sleep_ctrl_addr, \
  214. .type = REGULATOR_TYPE_SMPS, \
  215. .hpm_min_load = PM8058_VREG_##_hpm_min_load##_HPM_MIN_LOAD, \
  216. .global_enable = { \
  217. [0] = _en0, \
  218. [1] = NULL, \
  219. }, \
  220. .global_enable_mask = { \
  221. [0] = _en0_mask, \
  222. [1] = 0, \
  223. }, \
  224. }
  225. #define LVS(_id, _ctrl_addr, _en0, _en0_mask) \
  226. [PM8058_VREG_ID_##_id] = { \
  227. .ctrl_addr = _ctrl_addr, \
  228. .type = REGULATOR_TYPE_LVS, \
  229. .global_enable = { \
  230. [0] = _en0, \
  231. [1] = NULL, \
  232. }, \
  233. .global_enable_mask = { \
  234. [0] = _en0_mask, \
  235. [1] = 0, \
  236. }, \
  237. }
  238. #define NCP(_id, _ctrl_addr, _test1) \
  239. [PM8058_VREG_ID_##_id] = { \
  240. .ctrl_addr = _ctrl_addr, \
  241. .type = REGULATOR_TYPE_NCP, \
  242. .test_addr = _test1, \
  243. .global_enable = { \
  244. [0] = NULL, \
  245. [1] = NULL, \
  246. }, \
  247. .global_enable_mask = { \
  248. [0] = 0, \
  249. [1] = 0, \
  250. }, \
  251. }
  252. #define MASTER_ENABLE_COUNT 6
  253. #define EN_MSM 0
  254. #define EN_PH 1
  255. #define EN_RF 2
  256. #define EN_GRP_5_4 3
  257. #define EN_GRP_3_2 4
  258. #define EN_GRP_1_0 5
  259. /* Master regulator control registers */
  260. static struct pm8058_enable m_en[MASTER_ENABLE_COUNT] = {
  261. [EN_MSM] = {
  262. .addr = 0x018, /* VREG_EN_MSM */
  263. },
  264. [EN_PH] = {
  265. .addr = 0x019, /* VREG_EN_PH */
  266. },
  267. [EN_RF] = {
  268. .addr = 0x01A, /* VREG_EN_RF */
  269. },
  270. [EN_GRP_5_4] = {
  271. .addr = 0x1C8, /* VREG_EN_MSM_GRP_5-4 */
  272. },
  273. [EN_GRP_3_2] = {
  274. .addr = 0x1C9, /* VREG_EN_MSM_GRP_3-2 */
  275. },
  276. [EN_GRP_1_0] = {
  277. .addr = 0x1CA, /* VREG_EN_MSM_GRP_1-0 */
  278. },
  279. };
  280. static struct pm8058_vreg pm8058_vreg[] = {
  281. /* id ctrl test n/p hpm_min m_en m_en_mask */
  282. LDO(L0, 0x009, 0x065, 1, LDO_150, &m_en[EN_GRP_5_4], BIT(3)),
  283. LDO(L1, 0x00A, 0x066, 1, LDO_300, &m_en[EN_GRP_5_4], BIT(6) | BIT(2)),
  284. LDO(L2, 0x00B, 0x067, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(2)),
  285. LDO(L3, 0x00C, 0x068, 0, LDO_150, &m_en[EN_GRP_1_0], BIT(1)),
  286. LDO(L4, 0x00D, 0x069, 0, LDO_50, &m_en[EN_MSM], 0),
  287. LDO(L5, 0x00E, 0x06A, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(7)),
  288. LDO(L6, 0x00F, 0x06B, 0, LDO_50, &m_en[EN_GRP_1_0], BIT(2)),
  289. LDO(L7, 0x010, 0x06C, 0, LDO_50, &m_en[EN_GRP_3_2], BIT(3)),
  290. LDO(L8, 0x011, 0x06D, 0, LDO_300, &m_en[EN_PH], BIT(7)),
  291. LDO(L9, 0x012, 0x06E, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(3)),
  292. LDO(L10, 0x013, 0x06F, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(4)),
  293. LDO(L11, 0x014, 0x070, 0, LDO_150, &m_en[EN_PH], BIT(4)),
  294. LDO(L12, 0x015, 0x071, 0, LDO_150, &m_en[EN_PH], BIT(3)),
  295. LDO(L13, 0x016, 0x072, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(1)),
  296. LDO(L14, 0x017, 0x073, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(5)),
  297. LDO(L15, 0x089, 0x0E5, 0, LDO_300, &m_en[EN_GRP_1_0], BIT(4)),
  298. LDO(L16, 0x08A, 0x0E6, 0, LDO_300, &m_en[EN_GRP_3_2], BIT(0)),
  299. LDO(L17, 0x08B, 0x0E7, 0, LDO_150, &m_en[EN_RF], BIT(7)),
  300. LDO(L18, 0x11D, 0x125, 0, LDO_150, &m_en[EN_RF], BIT(6)),
  301. LDO(L19, 0x11E, 0x126, 0, LDO_150, &m_en[EN_RF], BIT(5)),
  302. LDO(L20, 0x11F, 0x127, 0, LDO_150, &m_en[EN_RF], BIT(4)),
  303. LDO_M2(L21, 0x120, 0x128, 1, LDO_150, &m_en[EN_GRP_5_4], BIT(1),
  304. &m_en[EN_GRP_1_0], BIT(6)),
  305. LDO(L22, 0x121, 0x129, 1, LDO_300, &m_en[EN_GRP_3_2], BIT(7)),
  306. LDO(L23, 0x122, 0x12A, 1, LDO_300, &m_en[EN_GRP_5_4], BIT(0)),
  307. LDO(L24, 0x123, 0x12B, 1, LDO_150, &m_en[EN_RF], BIT(3)),
  308. LDO(L25, 0x124, 0x12C, 1, LDO_150, &m_en[EN_RF], BIT(2)),
  309. /* id ctrl test2 clk sleep hpm_min m_en m_en_mask */
  310. SMPS(S0, 0x004, 0x084, 0x1D1, 0x1D8, SMPS, &m_en[EN_MSM], BIT(7)),
  311. SMPS(S1, 0x005, 0x085, 0x1D2, 0x1DB, SMPS, &m_en[EN_MSM], BIT(6)),
  312. SMPS(S2, 0x110, 0x119, 0x1D3, 0x1DE, SMPS, &m_en[EN_GRP_5_4], BIT(5)),
  313. SMPS(S3, 0x111, 0x11A, 0x1D4, 0x1E1, SMPS, &m_en[EN_GRP_5_4],
  314. BIT(7) | BIT(4)),
  315. SMPS(S4, 0x112, 0x11B, 0x1D5, 0x1E4, SMPS, &m_en[EN_GRP_3_2], BIT(5)),
  316. /* id ctrl m_en m_en_mask */
  317. LVS(LVS0, 0x12D, &m_en[EN_RF], BIT(1)),
  318. LVS(LVS1, 0x12F, &m_en[EN_GRP_1_0], BIT(0)),
  319. /* id ctrl test1 */
  320. NCP(NCP, 0x090, 0x0EC),
  321. };
  322. static int pm8058_smps_set_voltage_advanced(struct pm8058_vreg *vreg, int uV,
  323. int force_on);
  324. static int pm8058_smps_set_voltage_legacy(struct pm8058_vreg *vreg, int uV);
  325. static int _pm8058_vreg_is_enabled(struct pm8058_vreg *vreg);
  326. static unsigned int pm8058_vreg_get_mode(struct regulator_dev *dev);
  327. static void print_write_error(struct pm8058_vreg *vreg, int rc,
  328. const char *func);
  329. static int pm8058_vreg_write(struct pm8058_vreg *vreg,
  330. u16 addr, u8 val, u8 mask, u8 *reg_save)
  331. {
  332. int rc = 0;
  333. u8 reg;
  334. reg = (*reg_save & ~mask) | (val & mask);
  335. if (reg != *reg_save)
  336. rc = pm8xxx_writeb(vreg->dev->parent, addr, reg);
  337. if (rc)
  338. pr_err("%s: pm8xxx_write failed, rc=%d\n", __func__, rc);
  339. else
  340. *reg_save = reg;
  341. return rc;
  342. }
  343. static int pm8058_vreg_is_global_enabled(struct pm8058_vreg *vreg)
  344. {
  345. int ret = 0, i;
  346. for (i = 0;
  347. (i < GLOBAL_ENABLE_MAX) && !ret && vreg->global_enable[i]; i++)
  348. ret = vreg->global_enable[i]->reg &
  349. vreg->global_enable_mask[i];
  350. return ret;
  351. }
  352. static int pm8058_vreg_set_global_enable(struct pm8058_vreg *vreg, int on)
  353. {
  354. int rc = 0, i;
  355. for (i = 0;
  356. (i < GLOBAL_ENABLE_MAX) && !rc && vreg->global_enable[i]; i++)
  357. rc = pm8058_vreg_write(vreg, vreg->global_enable[i]->addr,
  358. (on ? vreg->global_enable_mask[i] : 0),
  359. vreg->global_enable_mask[i],
  360. &vreg->global_enable[i]->reg);
  361. return rc;
  362. }
  363. static int pm8058_vreg_using_pin_ctrl(struct pm8058_vreg *vreg)
  364. {
  365. int ret = 0;
  366. switch (vreg->type) {
  367. case REGULATOR_TYPE_LDO:
  368. ret = ((vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK) << 4)
  369. | (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK);
  370. break;
  371. case REGULATOR_TYPE_SMPS:
  372. ret = vreg->sleep_ctrl_reg
  373. & (SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK);
  374. break;
  375. case REGULATOR_TYPE_LVS:
  376. ret = vreg->ctrl_reg & LVS_PIN_CTRL_MASK;
  377. break;
  378. }
  379. return ret;
  380. }
  381. static int pm8058_vreg_set_pin_ctrl(struct pm8058_vreg *vreg, int on)
  382. {
  383. int rc = 0, bank;
  384. u8 val = 0, mask;
  385. unsigned pc = vreg->pdata->pin_ctrl;
  386. unsigned pf = vreg->pdata->pin_fn;
  387. switch (vreg->type) {
  388. case REGULATOR_TYPE_LDO:
  389. if (on) {
  390. if (pc & PM8058_VREG_PIN_CTRL_D0)
  391. val |= LDO_TEST_PIN_CTRL_EN0;
  392. if (pc & PM8058_VREG_PIN_CTRL_D1)
  393. val |= LDO_TEST_PIN_CTRL_EN1;
  394. if (pc & PM8058_VREG_PIN_CTRL_A0)
  395. val |= LDO_TEST_PIN_CTRL_EN2;
  396. if (pc & PM8058_VREG_PIN_CTRL_A1)
  397. val |= LDO_TEST_PIN_CTRL_EN3;
  398. bank = (pf == PM8058_VREG_PIN_FN_ENABLE ? 5 : 6);
  399. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  400. val | REGULATOR_BANK_SEL(bank)
  401. | REGULATOR_BANK_WRITE,
  402. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  403. &vreg->test_reg[bank]);
  404. if (rc)
  405. goto bail;
  406. val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
  407. | REGULATOR_BANK_SEL(0);
  408. mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
  409. rc = pm8058_vreg_write(vreg, vreg->test_addr, val, mask,
  410. &vreg->test_reg[0]);
  411. if (rc)
  412. goto bail;
  413. if (pf == PM8058_VREG_PIN_FN_ENABLE) {
  414. /* Pin control ON/OFF */
  415. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  416. LDO_CTRL_PM_HPM,
  417. LDO_ENABLE_MASK | LDO_CTRL_PM_MASK,
  418. &vreg->ctrl_reg);
  419. if (rc)
  420. goto bail;
  421. rc = pm8058_vreg_set_global_enable(vreg, 0);
  422. if (rc)
  423. goto bail;
  424. } else {
  425. /* Pin control LPM/HPM */
  426. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  427. LDO_ENABLE | LDO_CTRL_PM_LPM,
  428. LDO_ENABLE_MASK | LDO_CTRL_PM_MASK,
  429. &vreg->ctrl_reg);
  430. if (rc)
  431. goto bail;
  432. }
  433. } else {
  434. /* Pin control off */
  435. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  436. REGULATOR_BANK_SEL(5) | REGULATOR_BANK_WRITE,
  437. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  438. &vreg->test_reg[5]);
  439. if (rc)
  440. goto bail;
  441. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  442. REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
  443. LDO_TEST_PIN_CTRL_MASK | REGULATOR_BANK_MASK,
  444. &vreg->test_reg[6]);
  445. if (rc)
  446. goto bail;
  447. }
  448. break;
  449. case REGULATOR_TYPE_SMPS:
  450. if (on) {
  451. if (pf == PM8058_VREG_PIN_FN_ENABLE) {
  452. /* Pin control ON/OFF */
  453. if (pc & PM8058_VREG_PIN_CTRL_D0)
  454. val |= SMPS_PIN_CTRL_D0;
  455. if (pc & PM8058_VREG_PIN_CTRL_D1)
  456. val |= SMPS_PIN_CTRL_D1;
  457. if (pc & PM8058_VREG_PIN_CTRL_A0)
  458. val |= SMPS_PIN_CTRL_A0;
  459. if (pc & PM8058_VREG_PIN_CTRL_A1)
  460. val |= SMPS_PIN_CTRL_A1;
  461. } else {
  462. /* Pin control LPM/HPM */
  463. if (pc & PM8058_VREG_PIN_CTRL_D0)
  464. val |= SMPS_PIN_CTRL_LPM_D0;
  465. if (pc & PM8058_VREG_PIN_CTRL_D1)
  466. val |= SMPS_PIN_CTRL_LPM_D1;
  467. if (pc & PM8058_VREG_PIN_CTRL_A0)
  468. val |= SMPS_PIN_CTRL_LPM_A0;
  469. if (pc & PM8058_VREG_PIN_CTRL_A1)
  470. val |= SMPS_PIN_CTRL_LPM_A1;
  471. }
  472. rc = pm8058_vreg_set_global_enable(vreg, 0);
  473. if (rc)
  474. goto bail;
  475. rc = pm8058_smps_set_voltage_legacy(vreg,
  476. vreg->save_uV);
  477. if (rc)
  478. goto bail;
  479. rc = pm8058_vreg_write(vreg, vreg->sleep_ctrl_addr, val,
  480. SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
  481. &vreg->sleep_ctrl_reg);
  482. if (rc)
  483. goto bail;
  484. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  485. (pf == PM8058_VREG_PIN_FN_ENABLE
  486. ? 0 : SMPS_LEGACY_ENABLE),
  487. SMPS_LEGACY_ENABLE, &vreg->ctrl_reg);
  488. if (rc)
  489. goto bail;
  490. rc = pm8058_vreg_write(vreg, vreg->clk_ctrl_addr,
  491. (pf == PM8058_VREG_PIN_FN_ENABLE
  492. ? SMPS_CLK_CTRL_PWM : SMPS_CLK_CTRL_PFM),
  493. SMPS_CLK_CTRL_MASK, &vreg->clk_ctrl_reg);
  494. if (rc)
  495. goto bail;
  496. } else {
  497. /* Pin control off */
  498. if (!SMPS_IN_ADVANCED_MODE(vreg)) {
  499. if (_pm8058_vreg_is_enabled(vreg))
  500. val = SMPS_LEGACY_ENABLE;
  501. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  502. val, SMPS_LEGACY_ENABLE,
  503. &vreg->ctrl_reg);
  504. if (rc)
  505. goto bail;
  506. }
  507. rc = pm8058_vreg_write(vreg, vreg->sleep_ctrl_addr, 0,
  508. SMPS_PIN_CTRL_MASK | SMPS_PIN_CTRL_LPM_MASK,
  509. &vreg->sleep_ctrl_reg);
  510. if (rc)
  511. goto bail;
  512. rc = pm8058_smps_set_voltage_advanced(vreg,
  513. vreg->save_uV, 0);
  514. if (rc)
  515. goto bail;
  516. }
  517. break;
  518. case REGULATOR_TYPE_LVS:
  519. if (on) {
  520. if (pc & PM8058_VREG_PIN_CTRL_D0)
  521. val |= LVS_PIN_CTRL_EN0;
  522. if (pc & PM8058_VREG_PIN_CTRL_D1)
  523. val |= LVS_PIN_CTRL_EN1;
  524. if (pc & PM8058_VREG_PIN_CTRL_A0)
  525. val |= LVS_PIN_CTRL_EN2;
  526. if (pc & PM8058_VREG_PIN_CTRL_A1)
  527. val |= LVS_PIN_CTRL_EN3;
  528. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, val,
  529. LVS_PIN_CTRL_MASK | LVS_ENABLE_MASK,
  530. &vreg->ctrl_reg);
  531. if (rc)
  532. goto bail;
  533. rc = pm8058_vreg_set_global_enable(vreg, 0);
  534. if (rc)
  535. goto bail;
  536. } else {
  537. /* Pin control off */
  538. if (_pm8058_vreg_is_enabled(vreg))
  539. val = LVS_ENABLE;
  540. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, val,
  541. LVS_ENABLE_MASK | LVS_PIN_CTRL_MASK,
  542. &vreg->ctrl_reg);
  543. if (rc)
  544. goto bail;
  545. }
  546. break;
  547. }
  548. bail:
  549. if (rc)
  550. print_write_error(vreg, rc, __func__);
  551. return rc;
  552. }
  553. static int pm8058_vreg_enable(struct regulator_dev *dev)
  554. {
  555. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  556. int mode;
  557. int rc = 0;
  558. mode = pm8058_vreg_get_mode(dev);
  559. if (mode == REGULATOR_MODE_IDLE) {
  560. /* Turn on pin control. */
  561. rc = pm8058_vreg_set_pin_ctrl(vreg, 1);
  562. if (rc)
  563. goto bail;
  564. return rc;
  565. }
  566. if (vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg))
  567. rc = pm8058_smps_set_voltage_advanced(vreg, vreg->save_uV, 1);
  568. else
  569. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, REGULATOR_EN_MASK,
  570. REGULATOR_EN_MASK, &vreg->ctrl_reg);
  571. bail:
  572. if (rc)
  573. print_write_error(vreg, rc, __func__);
  574. return rc;
  575. }
  576. static int _pm8058_vreg_is_enabled(struct pm8058_vreg *vreg)
  577. {
  578. /*
  579. * All regulator types except advanced mode SMPS have enable bit in
  580. * bit 7 of the control register. Global enable and pin control also
  581. * do not work for advanced mode SMPS.
  582. */
  583. if (!(vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg))
  584. && ((vreg->ctrl_reg & REGULATOR_EN_MASK)
  585. || pm8058_vreg_is_global_enabled(vreg)
  586. || pm8058_vreg_using_pin_ctrl(vreg)))
  587. return 1;
  588. else if (vreg->type == REGULATOR_TYPE_SMPS
  589. && SMPS_IN_ADVANCED_MODE(vreg)
  590. && ((vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK)
  591. != SMPS_ADVANCED_BAND_OFF))
  592. return 1;
  593. return 0;
  594. }
  595. static int pm8058_vreg_is_enabled(struct regulator_dev *dev)
  596. {
  597. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  598. return _pm8058_vreg_is_enabled(vreg);
  599. }
  600. static int pm8058_vreg_disable(struct regulator_dev *dev)
  601. {
  602. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  603. int rc = 0;
  604. /* Disable in global control register. */
  605. rc = pm8058_vreg_set_global_enable(vreg, 0);
  606. if (rc)
  607. goto bail;
  608. /* Turn off pin control. */
  609. rc = pm8058_vreg_set_pin_ctrl(vreg, 0);
  610. if (rc)
  611. goto bail;
  612. /* Disable in local control register. */
  613. if (vreg->type == REGULATOR_TYPE_SMPS && SMPS_IN_ADVANCED_MODE(vreg))
  614. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  615. SMPS_ADVANCED_BAND_OFF, SMPS_ADVANCED_BAND_MASK,
  616. &vreg->ctrl_reg);
  617. else
  618. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, 0,
  619. REGULATOR_EN_MASK, &vreg->ctrl_reg);
  620. bail:
  621. if (rc)
  622. print_write_error(vreg, rc, __func__);
  623. return rc;
  624. }
  625. static int pm8058_pldo_set_voltage(struct pm8058_vreg *vreg, int uV)
  626. {
  627. int vmin, rc = 0;
  628. unsigned vprog, fine_step;
  629. u8 range_ext, range_sel, fine_step_reg;
  630. if (uV < PLDO_LOW_UV_MIN || uV > PLDO_HIGH_UV_MAX)
  631. return -EINVAL;
  632. if (uV < PLDO_LOW_UV_MAX + PLDO_LOW_FINE_STEP_UV) {
  633. vmin = PLDO_LOW_UV_MIN;
  634. fine_step = PLDO_LOW_FINE_STEP_UV;
  635. range_ext = 0;
  636. range_sel = LDO_TEST_RANGE_SEL_MASK;
  637. } else if (uV < PLDO_NORM_UV_MAX + PLDO_NORM_FINE_STEP_UV) {
  638. vmin = PLDO_NORM_UV_MIN;
  639. fine_step = PLDO_NORM_FINE_STEP_UV;
  640. range_ext = 0;
  641. range_sel = 0;
  642. } else {
  643. vmin = PLDO_HIGH_UV_MIN;
  644. fine_step = PLDO_HIGH_FINE_STEP_UV;
  645. range_ext = LDO_TEST_RANGE_EXT_MASK;
  646. range_sel = 0;
  647. }
  648. vprog = (uV - vmin) / fine_step;
  649. fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
  650. vprog >>= 1;
  651. /*
  652. * Disable program voltage update if range extension, range select,
  653. * or fine step have changed and the regulator is enabled.
  654. */
  655. if (_pm8058_vreg_is_enabled(vreg) &&
  656. (((range_ext ^ vreg->test_reg[4]) & LDO_TEST_RANGE_EXT_MASK)
  657. || ((range_sel ^ vreg->test_reg[2]) & LDO_TEST_RANGE_SEL_MASK)
  658. || ((fine_step_reg ^ vreg->test_reg[2])
  659. & LDO_TEST_FINE_STEP_MASK))) {
  660. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  661. REGULATOR_BANK_SEL(2) | REGULATOR_BANK_WRITE,
  662. REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
  663. &vreg->test_reg[2]);
  664. if (rc)
  665. goto bail;
  666. }
  667. /* Write new voltage. */
  668. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, vprog,
  669. LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
  670. if (rc)
  671. goto bail;
  672. /* Write range extension. */
  673. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  674. range_ext | REGULATOR_BANK_SEL(4)
  675. | REGULATOR_BANK_WRITE,
  676. LDO_TEST_RANGE_EXT_MASK | REGULATOR_BANK_MASK,
  677. &vreg->test_reg[4]);
  678. if (rc)
  679. goto bail;
  680. /* Write fine step, range select and program voltage update. */
  681. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  682. fine_step_reg | range_sel | REGULATOR_BANK_SEL(2)
  683. | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
  684. LDO_TEST_FINE_STEP_MASK | LDO_TEST_RANGE_SEL_MASK
  685. | REGULATOR_BANK_MASK | LDO_TEST_VPROG_UPDATE_MASK,
  686. &vreg->test_reg[2]);
  687. bail:
  688. if (rc)
  689. print_write_error(vreg, rc, __func__);
  690. return rc;
  691. }
  692. static int pm8058_nldo_set_voltage(struct pm8058_vreg *vreg, int uV)
  693. {
  694. unsigned vprog, fine_step_reg;
  695. int rc;
  696. if (uV < NLDO_UV_MIN || uV > NLDO_UV_MAX)
  697. return -EINVAL;
  698. vprog = (uV - NLDO_UV_MIN) / NLDO_FINE_STEP_UV;
  699. fine_step_reg = (vprog & 1) << LDO_TEST_FINE_STEP_SHIFT;
  700. vprog >>= 1;
  701. /* Write new voltage. */
  702. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, vprog,
  703. LDO_CTRL_VPROG_MASK, &vreg->ctrl_reg);
  704. if (rc)
  705. goto bail;
  706. /* Write fine step. */
  707. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  708. fine_step_reg | REGULATOR_BANK_SEL(2)
  709. | REGULATOR_BANK_WRITE | LDO_TEST_VPROG_UPDATE_MASK,
  710. LDO_TEST_FINE_STEP_MASK | REGULATOR_BANK_MASK
  711. | LDO_TEST_VPROG_UPDATE_MASK,
  712. &vreg->test_reg[2]);
  713. bail:
  714. if (rc)
  715. print_write_error(vreg, rc, __func__);
  716. return rc;
  717. }
  718. static int pm8058_ldo_set_voltage(struct regulator_dev *dev,
  719. int min_uV, int max_uV, unsigned *selector)
  720. {
  721. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  722. if (vreg->is_nmos)
  723. return pm8058_nldo_set_voltage(vreg, min_uV);
  724. else
  725. return pm8058_pldo_set_voltage(vreg, min_uV);
  726. }
  727. static int pm8058_pldo_get_voltage(struct pm8058_vreg *vreg)
  728. {
  729. int vmin, fine_step;
  730. u8 range_ext, range_sel, vprog, fine_step_reg;
  731. fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
  732. range_sel = vreg->test_reg[2] & LDO_TEST_RANGE_SEL_MASK;
  733. range_ext = vreg->test_reg[4] & LDO_TEST_RANGE_EXT_MASK;
  734. vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
  735. vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
  736. if (range_sel) {
  737. /* low range mode */
  738. fine_step = PLDO_LOW_FINE_STEP_UV;
  739. vmin = PLDO_LOW_UV_MIN;
  740. } else if (!range_ext) {
  741. /* normal mode */
  742. fine_step = PLDO_NORM_FINE_STEP_UV;
  743. vmin = PLDO_NORM_UV_MIN;
  744. } else {
  745. /* high range mode */
  746. fine_step = PLDO_HIGH_FINE_STEP_UV;
  747. vmin = PLDO_HIGH_UV_MIN;
  748. }
  749. return fine_step * vprog + vmin;
  750. }
  751. static int pm8058_nldo_get_voltage(struct pm8058_vreg *vreg)
  752. {
  753. u8 vprog, fine_step_reg;
  754. fine_step_reg = vreg->test_reg[2] & LDO_TEST_FINE_STEP_MASK;
  755. vprog = vreg->ctrl_reg & LDO_CTRL_VPROG_MASK;
  756. vprog = (vprog << 1) | (fine_step_reg >> LDO_TEST_FINE_STEP_SHIFT);
  757. return NLDO_FINE_STEP_UV * vprog + NLDO_UV_MIN;
  758. }
  759. static int pm8058_ldo_get_voltage(struct regulator_dev *dev)
  760. {
  761. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  762. if (vreg->is_nmos)
  763. return pm8058_nldo_get_voltage(vreg);
  764. else
  765. return pm8058_pldo_get_voltage(vreg);
  766. }
  767. static int pm8058_smps_get_voltage_advanced(struct pm8058_vreg *vreg)
  768. {
  769. u8 vprog, band;
  770. int uV = 0;
  771. vprog = vreg->ctrl_reg & SMPS_ADVANCED_VPROG_MASK;
  772. band = vreg->ctrl_reg & SMPS_ADVANCED_BAND_MASK;
  773. if (band == SMPS_ADVANCED_BAND_1)
  774. uV = vprog * SMPS_BAND1_UV_STEP + SMPS_BAND1_UV_MIN;
  775. else if (band == SMPS_ADVANCED_BAND_2)
  776. uV = vprog * SMPS_BAND2_UV_STEP + SMPS_BAND2_UV_MIN;
  777. else if (band == SMPS_ADVANCED_BAND_3)
  778. uV = vprog * SMPS_BAND3_UV_STEP + SMPS_BAND3_UV_MIN;
  779. else
  780. uV = vreg->save_uV;
  781. return uV;
  782. }
  783. static int pm8058_smps_get_voltage_legacy(struct pm8058_vreg *vreg)
  784. {
  785. u8 vlow, vref, vprog;
  786. int uV;
  787. vlow = vreg->test_reg[1] & SMPS_LEGACY_VLOW_SEL_MASK;
  788. vref = vreg->ctrl_reg & SMPS_LEGACY_VREF_SEL_MASK;
  789. vprog = vreg->ctrl_reg & SMPS_LEGACY_VPROG_MASK;
  790. if (vlow && vref) {
  791. /* mode 3 */
  792. uV = vprog * SMPS_MODE3_UV_STEP + SMPS_MODE3_UV_MIN;
  793. } else if (vref) {
  794. /* mode 2 */
  795. uV = vprog * SMPS_MODE2_UV_STEP + SMPS_MODE2_UV_MIN;
  796. } else {
  797. /* mode 1 */
  798. uV = vprog * SMPS_MODE1_UV_STEP + SMPS_MODE1_UV_MIN;
  799. }
  800. return uV;
  801. }
  802. static int _pm8058_smps_get_voltage(struct pm8058_vreg *vreg)
  803. {
  804. if (SMPS_IN_ADVANCED_MODE(vreg))
  805. return pm8058_smps_get_voltage_advanced(vreg);
  806. return pm8058_smps_get_voltage_legacy(vreg);
  807. }
  808. static int pm8058_smps_get_voltage(struct regulator_dev *dev)
  809. {
  810. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  811. return _pm8058_smps_get_voltage(vreg);
  812. }
  813. static int pm8058_smps_set_voltage_advanced(struct pm8058_vreg *vreg,
  814. int uV, int force_on)
  815. {
  816. u8 vprog, band;
  817. int rc, new_uV;
  818. if (uV < SMPS_BAND1_UV_MAX + SMPS_BAND1_UV_STEP) {
  819. vprog = ((uV - SMPS_BAND1_UV_MIN) / SMPS_BAND1_UV_STEP);
  820. band = SMPS_ADVANCED_BAND_1;
  821. new_uV = SMPS_BAND1_UV_MIN + vprog * SMPS_BAND1_UV_STEP;
  822. } else if (uV < SMPS_BAND2_UV_MAX + SMPS_BAND2_UV_STEP) {
  823. vprog = ((uV - SMPS_BAND2_UV_MIN) / SMPS_BAND2_UV_STEP);
  824. band = SMPS_ADVANCED_BAND_2;
  825. new_uV = SMPS_BAND2_UV_MIN + vprog * SMPS_BAND2_UV_STEP;
  826. } else {
  827. vprog = ((uV - SMPS_BAND3_UV_MIN) / SMPS_BAND3_UV_STEP);
  828. band = SMPS_ADVANCED_BAND_3;
  829. new_uV = SMPS_BAND3_UV_MIN + vprog * SMPS_BAND3_UV_STEP;
  830. }
  831. /* Do not set band if regulator currently disabled. */
  832. if (!_pm8058_vreg_is_enabled(vreg) && !force_on)
  833. band = SMPS_ADVANCED_BAND_OFF;
  834. /* Set advanced mode bit to 1. */
  835. rc = pm8058_vreg_write(vreg, vreg->test_addr, SMPS_ADVANCED_MODE
  836. | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
  837. SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
  838. &vreg->test_reg[7]);
  839. if (rc)
  840. goto bail;
  841. /* Set voltage and voltage band. */
  842. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, band | vprog,
  843. SMPS_ADVANCED_BAND_MASK | SMPS_ADVANCED_VPROG_MASK,
  844. &vreg->ctrl_reg);
  845. if (rc)
  846. goto bail;
  847. vreg->save_uV = new_uV;
  848. bail:
  849. return rc;
  850. }
  851. static int pm8058_smps_set_voltage_legacy(struct pm8058_vreg *vreg, int uV)
  852. {
  853. u8 vlow, vref, vprog, pd, en;
  854. int rc;
  855. if (uV < SMPS_MODE3_UV_MAX + SMPS_MODE3_UV_STEP) {
  856. vprog = ((uV - SMPS_MODE3_UV_MIN) / SMPS_MODE3_UV_STEP);
  857. vref = SMPS_LEGACY_VREF_SEL_MASK;
  858. vlow = SMPS_LEGACY_VLOW_SEL_MASK;
  859. } else if (uV < SMPS_MODE2_UV_MAX + SMPS_MODE2_UV_STEP) {
  860. vprog = ((uV - SMPS_MODE2_UV_MIN) / SMPS_MODE2_UV_STEP);
  861. vref = SMPS_LEGACY_VREF_SEL_MASK;
  862. vlow = 0;
  863. } else {
  864. vprog = ((uV - SMPS_MODE1_UV_MIN) / SMPS_MODE1_UV_STEP);
  865. vref = 0;
  866. vlow = 0;
  867. }
  868. /* set vlow bit for ultra low voltage mode */
  869. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  870. vlow | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(1),
  871. REGULATOR_BANK_MASK | SMPS_LEGACY_VLOW_SEL_MASK,
  872. &vreg->test_reg[1]);
  873. if (rc)
  874. goto bail;
  875. /* Set advanced mode bit to 0. */
  876. rc = pm8058_vreg_write(vreg, vreg->test_addr, SMPS_LEGACY_MODE
  877. | REGULATOR_BANK_WRITE | REGULATOR_BANK_SEL(7),
  878. SMPS_ADVANCED_MODE_MASK | REGULATOR_BANK_MASK,
  879. &vreg->test_reg[7]);
  880. if (rc)
  881. goto bail;
  882. en = (_pm8058_vreg_is_enabled(vreg) ? SMPS_LEGACY_ENABLE : 0);
  883. pd = (vreg->pdata->pull_down_enable ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0);
  884. /* Set voltage (and the rest of the control register). */
  885. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, en | pd | vref | vprog,
  886. SMPS_LEGACY_ENABLE | SMPS_LEGACY_PULL_DOWN_ENABLE
  887. | SMPS_LEGACY_VREF_SEL_MASK | SMPS_LEGACY_VPROG_MASK,
  888. &vreg->ctrl_reg);
  889. vreg->save_uV = pm8058_smps_get_voltage_legacy(vreg);
  890. bail:
  891. return rc;
  892. }
  893. static int pm8058_smps_set_voltage(struct regulator_dev *dev,
  894. int min_uV, int max_uV, unsigned *selector)
  895. {
  896. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  897. int rc = 0;
  898. if (min_uV < SMPS_UV_MIN || min_uV > SMPS_UV_MAX)
  899. return -EINVAL;
  900. if (SMPS_IN_ADVANCED_MODE(vreg))
  901. rc = pm8058_smps_set_voltage_advanced(vreg, min_uV, 0);
  902. else
  903. rc = pm8058_smps_set_voltage_legacy(vreg, min_uV);
  904. if (rc)
  905. print_write_error(vreg, rc, __func__);
  906. return rc;
  907. }
  908. static int pm8058_ncp_set_voltage(struct regulator_dev *dev,
  909. int min_uV, int max_uV, unsigned *selector)
  910. {
  911. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  912. int rc;
  913. u8 val;
  914. if (min_uV < NCP_UV_MIN || min_uV > NCP_UV_MAX)
  915. return -EINVAL;
  916. val = (min_uV - NCP_UV_MIN) / NCP_UV_STEP;
  917. /* voltage setting */
  918. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, val, NCP_VPROG_MASK,
  919. &vreg->ctrl_reg);
  920. if (rc)
  921. print_write_error(vreg, rc, __func__);
  922. return rc;
  923. }
  924. static int pm8058_ncp_get_voltage(struct regulator_dev *dev)
  925. {
  926. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  927. u8 vprog = vreg->ctrl_reg & NCP_VPROG_MASK;
  928. return NCP_UV_MIN + vprog * NCP_UV_STEP;
  929. }
  930. static int pm8058_ldo_set_mode(struct pm8058_vreg *vreg, unsigned int mode)
  931. {
  932. int rc = 0;
  933. u8 mask, val;
  934. switch (mode) {
  935. case REGULATOR_MODE_FAST:
  936. /* HPM */
  937. val = (_pm8058_vreg_is_enabled(vreg) ? LDO_ENABLE : 0)
  938. | LDO_CTRL_PM_HPM;
  939. mask = LDO_ENABLE_MASK | LDO_CTRL_PM_MASK;
  940. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, val, mask,
  941. &vreg->ctrl_reg);
  942. if (rc)
  943. goto bail;
  944. if (pm8058_vreg_using_pin_ctrl(vreg))
  945. rc = pm8058_vreg_set_pin_ctrl(vreg, 0);
  946. if (rc)
  947. goto bail;
  948. break;
  949. case REGULATOR_MODE_STANDBY:
  950. /* LPM */
  951. val = (_pm8058_vreg_is_enabled(vreg) ? LDO_ENABLE : 0)
  952. | LDO_CTRL_PM_LPM;
  953. mask = LDO_ENABLE_MASK | LDO_CTRL_PM_MASK;
  954. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr, val, mask,
  955. &vreg->ctrl_reg);
  956. if (rc)
  957. goto bail;
  958. val = LDO_TEST_LPM_SEL_CTRL | REGULATOR_BANK_WRITE
  959. | REGULATOR_BANK_SEL(0);
  960. mask = LDO_TEST_LPM_MASK | REGULATOR_BANK_MASK;
  961. rc = pm8058_vreg_write(vreg, vreg->test_addr, val, mask,
  962. &vreg->test_reg[0]);
  963. if (rc)
  964. goto bail;
  965. if (pm8058_vreg_using_pin_ctrl(vreg))
  966. rc = pm8058_vreg_set_pin_ctrl(vreg, 0);
  967. if (rc)
  968. goto bail;
  969. break;
  970. case REGULATOR_MODE_IDLE:
  971. /* Pin Control */
  972. if (_pm8058_vreg_is_enabled(vreg))
  973. rc = pm8058_vreg_set_pin_ctrl(vreg, 1);
  974. if (rc)
  975. goto bail;
  976. break;
  977. default:
  978. pr_err("%s: invalid mode: %u\n", __func__, mode);
  979. return -EINVAL;
  980. }
  981. bail:
  982. if (rc)
  983. print_write_error(vreg, rc, __func__);
  984. return rc;
  985. }
  986. static int pm8058_smps_set_mode(struct pm8058_vreg *vreg, unsigned int mode)
  987. {
  988. int rc = 0;
  989. u8 mask, val;
  990. switch (mode) {
  991. case REGULATOR_MODE_FAST:
  992. /* HPM */
  993. val = SMPS_CLK_CTRL_PWM;
  994. mask = SMPS_CLK_CTRL_MASK;
  995. rc = pm8058_vreg_write(vreg, vreg->clk_ctrl_addr, val, mask,
  996. &vreg->clk_ctrl_reg);
  997. if (rc)
  998. goto bail;
  999. if (pm8058_vreg_using_pin_ctrl(vreg))
  1000. rc = pm8058_vreg_set_pin_ctrl(vreg, 0);
  1001. if (rc)
  1002. goto bail;
  1003. break;
  1004. case REGULATOR_MODE_STANDBY:
  1005. /* LPM */
  1006. val = SMPS_CLK_CTRL_PFM;
  1007. mask = SMPS_CLK_CTRL_MASK;
  1008. rc = pm8058_vreg_write(vreg, vreg->clk_ctrl_addr, val, mask,
  1009. &vreg->clk_ctrl_reg);
  1010. if (rc)
  1011. goto bail;
  1012. if (pm8058_vreg_using_pin_ctrl(vreg))
  1013. rc = pm8058_vreg_set_pin_ctrl(vreg, 0);
  1014. if (rc)
  1015. goto bail;
  1016. break;
  1017. case REGULATOR_MODE_IDLE:
  1018. /* Pin Control */
  1019. if (_pm8058_vreg_is_enabled(vreg))
  1020. rc = pm8058_vreg_set_pin_ctrl(vreg, 1);
  1021. if (rc)
  1022. goto bail;
  1023. break;
  1024. default:
  1025. pr_err("%s: invalid mode: %u\n", __func__, mode);
  1026. return -EINVAL;
  1027. }
  1028. bail:
  1029. if (rc)
  1030. print_write_error(vreg, rc, __func__);
  1031. return rc;
  1032. }
  1033. static int pm8058_lvs_set_mode(struct pm8058_vreg *vreg, unsigned int mode)
  1034. {
  1035. int rc = 0;
  1036. if (mode == REGULATOR_MODE_IDLE) {
  1037. /* Use pin control. */
  1038. if (_pm8058_vreg_is_enabled(vreg))
  1039. rc = pm8058_vreg_set_pin_ctrl(vreg, 1);
  1040. } else {
  1041. /* Turn off pin control. */
  1042. rc = pm8058_vreg_set_pin_ctrl(vreg, 0);
  1043. }
  1044. return rc;
  1045. }
  1046. /*
  1047. * Optimum mode programming:
  1048. * REGULATOR_MODE_FAST: Go to HPM (highest priority)
  1049. * REGULATOR_MODE_STANDBY: Go to pin ctrl mode if there are any pin ctrl
  1050. * votes, else go to LPM
  1051. *
  1052. * Pin ctrl mode voting via regulator set_mode:
  1053. * REGULATOR_MODE_IDLE: Go to pin ctrl mode if the optimum mode is LPM, else
  1054. * go to HPM
  1055. * REGULATOR_MODE_NORMAL: Go to LPM if it is the optimum mode, else go to HPM
  1056. */
  1057. static int pm8058_vreg_set_mode(struct regulator_dev *dev, unsigned int mode)
  1058. {
  1059. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  1060. unsigned prev_optimum = vreg->optimum;
  1061. unsigned prev_pc_vote = vreg->pc_vote;
  1062. unsigned prev_mode_initialized = vreg->mode_initialized;
  1063. int new_mode = REGULATOR_MODE_FAST;
  1064. int rc = 0;
  1065. /* Determine new mode to go into. */
  1066. switch (mode) {
  1067. case REGULATOR_MODE_FAST:
  1068. new_mode = REGULATOR_MODE_FAST;
  1069. vreg->optimum = mode;
  1070. vreg->mode_initialized = 1;
  1071. break;
  1072. case REGULATOR_MODE_STANDBY:
  1073. if (vreg->pc_vote)
  1074. new_mode = REGULATOR_MODE_IDLE;
  1075. else
  1076. new_mode = REGULATOR_MODE_STANDBY;
  1077. vreg->optimum = mode;
  1078. vreg->mode_initialized = 1;
  1079. break;
  1080. case REGULATOR_MODE_IDLE:
  1081. if (vreg->pc_vote++)
  1082. goto done; /* already taken care of */
  1083. if (vreg->mode_initialized
  1084. && vreg->optimum == REGULATOR_MODE_FAST)
  1085. new_mode = REGULATOR_MODE_FAST;
  1086. else
  1087. new_mode = REGULATOR_MODE_IDLE;
  1088. break;
  1089. case REGULATOR_MODE_NORMAL:
  1090. if (vreg->pc_vote && --(vreg->pc_vote))
  1091. goto done; /* already taken care of */
  1092. if (vreg->optimum == REGULATOR_MODE_STANDBY)
  1093. new_mode = REGULATOR_MODE_STANDBY;
  1094. else
  1095. new_mode = REGULATOR_MODE_FAST;
  1096. break;
  1097. default:
  1098. pr_err("%s: unknown mode, mode=%u\n", __func__, mode);
  1099. return -EINVAL;
  1100. }
  1101. switch (vreg->type) {
  1102. case REGULATOR_TYPE_LDO:
  1103. rc = pm8058_ldo_set_mode(vreg, new_mode);
  1104. break;
  1105. case REGULATOR_TYPE_SMPS:
  1106. rc = pm8058_smps_set_mode(vreg, new_mode);
  1107. break;
  1108. case REGULATOR_TYPE_LVS:
  1109. rc = pm8058_lvs_set_mode(vreg, new_mode);
  1110. break;
  1111. }
  1112. if (rc) {
  1113. print_write_error(vreg, rc, __func__);
  1114. vreg->mode_initialized = prev_mode_initialized;
  1115. vreg->optimum = prev_optimum;
  1116. vreg->pc_vote = prev_pc_vote;
  1117. return rc;
  1118. }
  1119. done:
  1120. return 0;
  1121. }
  1122. static unsigned int pm8058_vreg_get_mode(struct regulator_dev *dev)
  1123. {
  1124. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  1125. if (!vreg->mode_initialized && vreg->pc_vote)
  1126. return REGULATOR_MODE_IDLE;
  1127. /* Check physical pin control state. */
  1128. switch (vreg->type) {
  1129. case REGULATOR_TYPE_LDO:
  1130. if (!(vreg->ctrl_reg & LDO_ENABLE_MASK)
  1131. && !pm8058_vreg_is_global_enabled(vreg)
  1132. && (vreg->test_reg[5] & LDO_TEST_PIN_CTRL_MASK))
  1133. return REGULATOR_MODE_IDLE;
  1134. else if (((vreg->ctrl_reg & LDO_ENABLE_MASK)
  1135. || pm8058_vreg_is_global_enabled(vreg))
  1136. && (vreg->ctrl_reg & LDO_CTRL_PM_MASK)
  1137. && (vreg->test_reg[6] & LDO_TEST_PIN_CTRL_LPM_MASK))
  1138. return REGULATOR_MODE_IDLE;
  1139. break;
  1140. case REGULATOR_TYPE_SMPS:
  1141. if (!SMPS_IN_ADVANCED_MODE(vreg)
  1142. && !(vreg->ctrl_reg & REGULATOR_EN_MASK)
  1143. && !pm8058_vreg_is_global_enabled(vreg)
  1144. && (vreg->sleep_ctrl_reg & SMPS_PIN_CTRL_MASK))
  1145. return REGULATOR_MODE_IDLE;
  1146. else if (!SMPS_IN_ADVANCED_MODE(vreg)
  1147. && ((vreg->ctrl_reg & REGULATOR_EN_MASK)
  1148. || pm8058_vreg_is_global_enabled(vreg))
  1149. && ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK)
  1150. == SMPS_CLK_CTRL_PFM)
  1151. && (vreg->sleep_ctrl_reg & SMPS_PIN_CTRL_LPM_MASK))
  1152. return REGULATOR_MODE_IDLE;
  1153. break;
  1154. case REGULATOR_TYPE_LVS:
  1155. if (!(vreg->ctrl_reg & LVS_ENABLE_MASK)
  1156. && !pm8058_vreg_is_global_enabled(vreg)
  1157. && (vreg->ctrl_reg & LVS_PIN_CTRL_MASK))
  1158. return REGULATOR_MODE_IDLE;
  1159. }
  1160. if (vreg->optimum == REGULATOR_MODE_FAST)
  1161. return REGULATOR_MODE_FAST;
  1162. else if (vreg->pc_vote)
  1163. return REGULATOR_MODE_IDLE;
  1164. else if (vreg->optimum == REGULATOR_MODE_STANDBY)
  1165. return REGULATOR_MODE_STANDBY;
  1166. return REGULATOR_MODE_FAST;
  1167. }
  1168. unsigned int pm8058_vreg_get_optimum_mode(struct regulator_dev *dev,
  1169. int input_uV, int output_uV, int load_uA)
  1170. {
  1171. struct pm8058_vreg *vreg = rdev_get_drvdata(dev);
  1172. if (load_uA <= 0) {
  1173. /*
  1174. * pm8058_vreg_get_optimum_mode is being called before consumers
  1175. * have specified their load currents via
  1176. * regulator_set_optimum_mode. Return whatever the existing mode
  1177. * is.
  1178. */
  1179. return pm8058_vreg_get_mode(dev);
  1180. }
  1181. if (load_uA >= vreg->hpm_min_load)
  1182. return REGULATOR_MODE_FAST;
  1183. return REGULATOR_MODE_STANDBY;
  1184. }
  1185. static struct regulator_ops pm8058_ldo_ops = {
  1186. .enable = pm8058_vreg_enable,
  1187. .disable = pm8058_vreg_disable,
  1188. .is_enabled = pm8058_vreg_is_enabled,
  1189. .set_voltage = pm8058_ldo_set_voltage,
  1190. .get_voltage = pm8058_ldo_get_voltage,
  1191. .set_mode = pm8058_vreg_set_mode,
  1192. .get_mode = pm8058_vreg_get_mode,
  1193. .get_optimum_mode = pm8058_vreg_get_optimum_mode,
  1194. };
  1195. static struct regulator_ops pm8058_smps_ops = {
  1196. .enable = pm8058_vreg_enable,
  1197. .disable = pm8058_vreg_disable,
  1198. .is_enabled = pm8058_vreg_is_enabled,
  1199. .set_voltage = pm8058_smps_set_voltage,
  1200. .get_voltage = pm8058_smps_get_voltage,
  1201. .set_mode = pm8058_vreg_set_mode,
  1202. .get_mode = pm8058_vreg_get_mode,
  1203. .get_optimum_mode = pm8058_vreg_get_optimum_mode,
  1204. };
  1205. static struct regulator_ops pm8058_lvs_ops = {
  1206. .enable = pm8058_vreg_enable,
  1207. .disable = pm8058_vreg_disable,
  1208. .is_enabled = pm8058_vreg_is_enabled,
  1209. .set_mode = pm8058_vreg_set_mode,
  1210. .get_mode = pm8058_vreg_get_mode,
  1211. };
  1212. static struct regulator_ops pm8058_ncp_ops = {
  1213. .enable = pm8058_vreg_enable,
  1214. .disable = pm8058_vreg_disable,
  1215. .is_enabled = pm8058_vreg_is_enabled,
  1216. .set_voltage = pm8058_ncp_set_voltage,
  1217. .get_voltage = pm8058_ncp_get_voltage,
  1218. };
  1219. #define VREG_DESCRIP(_id, _name, _ops) \
  1220. [_id] = { \
  1221. .id = _id, \
  1222. .name = _name, \
  1223. .ops = _ops, \
  1224. .type = REGULATOR_VOLTAGE, \
  1225. .owner = THIS_MODULE, \
  1226. }
  1227. static struct regulator_desc pm8058_vreg_descrip[] = {
  1228. VREG_DESCRIP(PM8058_VREG_ID_L0, "8058_l0", &pm8058_ldo_ops),
  1229. VREG_DESCRIP(PM8058_VREG_ID_L1, "8058_l1", &pm8058_ldo_ops),
  1230. VREG_DESCRIP(PM8058_VREG_ID_L2, "8058_l2", &pm8058_ldo_ops),
  1231. VREG_DESCRIP(PM8058_VREG_ID_L3, "8058_l3", &pm8058_ldo_ops),
  1232. VREG_DESCRIP(PM8058_VREG_ID_L4, "8058_l4", &pm8058_ldo_ops),
  1233. VREG_DESCRIP(PM8058_VREG_ID_L5, "8058_l5", &pm8058_ldo_ops),
  1234. VREG_DESCRIP(PM8058_VREG_ID_L6, "8058_l6", &pm8058_ldo_ops),
  1235. VREG_DESCRIP(PM8058_VREG_ID_L7, "8058_l7", &pm8058_ldo_ops),
  1236. VREG_DESCRIP(PM8058_VREG_ID_L8, "8058_l8", &pm8058_ldo_ops),
  1237. VREG_DESCRIP(PM8058_VREG_ID_L9, "8058_l9", &pm8058_ldo_ops),
  1238. VREG_DESCRIP(PM8058_VREG_ID_L10, "8058_l10", &pm8058_ldo_ops),
  1239. VREG_DESCRIP(PM8058_VREG_ID_L11, "8058_l11", &pm8058_ldo_ops),
  1240. VREG_DESCRIP(PM8058_VREG_ID_L12, "8058_l12", &pm8058_ldo_ops),
  1241. VREG_DESCRIP(PM8058_VREG_ID_L13, "8058_l13", &pm8058_ldo_ops),
  1242. VREG_DESCRIP(PM8058_VREG_ID_L14, "8058_l14", &pm8058_ldo_ops),
  1243. VREG_DESCRIP(PM8058_VREG_ID_L15, "8058_l15", &pm8058_ldo_ops),
  1244. VREG_DESCRIP(PM8058_VREG_ID_L16, "8058_l16", &pm8058_ldo_ops),
  1245. VREG_DESCRIP(PM8058_VREG_ID_L17, "8058_l17", &pm8058_ldo_ops),
  1246. VREG_DESCRIP(PM8058_VREG_ID_L18, "8058_l18", &pm8058_ldo_ops),
  1247. VREG_DESCRIP(PM8058_VREG_ID_L19, "8058_l19", &pm8058_ldo_ops),
  1248. VREG_DESCRIP(PM8058_VREG_ID_L20, "8058_l20", &pm8058_ldo_ops),
  1249. VREG_DESCRIP(PM8058_VREG_ID_L21, "8058_l21", &pm8058_ldo_ops),
  1250. VREG_DESCRIP(PM8058_VREG_ID_L22, "8058_l22", &pm8058_ldo_ops),
  1251. VREG_DESCRIP(PM8058_VREG_ID_L23, "8058_l23", &pm8058_ldo_ops),
  1252. VREG_DESCRIP(PM8058_VREG_ID_L24, "8058_l24", &pm8058_ldo_ops),
  1253. VREG_DESCRIP(PM8058_VREG_ID_L25, "8058_l25", &pm8058_ldo_ops),
  1254. VREG_DESCRIP(PM8058_VREG_ID_S0, "8058_s0", &pm8058_smps_ops),
  1255. VREG_DESCRIP(PM8058_VREG_ID_S1, "8058_s1", &pm8058_smps_ops),
  1256. VREG_DESCRIP(PM8058_VREG_ID_S2, "8058_s2", &pm8058_smps_ops),
  1257. VREG_DESCRIP(PM8058_VREG_ID_S3, "8058_s3", &pm8058_smps_ops),
  1258. VREG_DESCRIP(PM8058_VREG_ID_S4, "8058_s4", &pm8058_smps_ops),
  1259. VREG_DESCRIP(PM8058_VREG_ID_LVS0, "8058_lvs0", &pm8058_lvs_ops),
  1260. VREG_DESCRIP(PM8058_VREG_ID_LVS1, "8058_lvs1", &pm8058_lvs_ops),
  1261. VREG_DESCRIP(PM8058_VREG_ID_NCP, "8058_ncp", &pm8058_ncp_ops),
  1262. };
  1263. static int pm8058_master_enable_init(struct pm8058_vreg *vreg)
  1264. {
  1265. int rc = 0, i;
  1266. for (i = 0; i < MASTER_ENABLE_COUNT; i++) {
  1267. rc = pm8xxx_readb(vreg->dev->parent, m_en[i].addr,
  1268. &(m_en[i].reg));
  1269. if (rc)
  1270. goto bail;
  1271. }
  1272. bail:
  1273. if (rc)
  1274. pr_err("%s: pm8xxx_read failed, rc=%d\n", __func__, rc);
  1275. return rc;
  1276. }
  1277. static int pm8058_init_ldo(struct pm8058_vreg *vreg)
  1278. {
  1279. int rc = 0, i;
  1280. u8 bank;
  1281. /* Save the current test register state. */
  1282. for (i = 0; i < LDO_TEST_BANKS; i++) {
  1283. bank = REGULATOR_BANK_SEL(i);
  1284. rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
  1285. if (rc)
  1286. goto bail;
  1287. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  1288. &vreg->test_reg[i]);
  1289. if (rc)
  1290. goto bail;
  1291. vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
  1292. }
  1293. if ((vreg->ctrl_reg & LDO_CTRL_PM_MASK) == LDO_CTRL_PM_LPM)
  1294. vreg->optimum = REGULATOR_MODE_STANDBY;
  1295. else
  1296. vreg->optimum = REGULATOR_MODE_FAST;
  1297. /* Set pull down enable based on platform data. */
  1298. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  1299. (vreg->pdata->pull_down_enable ? LDO_PULL_DOWN_ENABLE : 0),
  1300. LDO_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
  1301. bail:
  1302. return rc;
  1303. }
  1304. static int pm8058_init_smps(struct pm8058_vreg *vreg)
  1305. {
  1306. int rc = 0, i;
  1307. u8 bank;
  1308. /* Save the current test2 register state. */
  1309. for (i = 0; i < SMPS_TEST_BANKS; i++) {
  1310. bank = REGULATOR_BANK_SEL(i);
  1311. rc = pm8xxx_writeb(vreg->dev->parent, vreg->test_addr, bank);
  1312. if (rc)
  1313. goto bail;
  1314. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  1315. &vreg->test_reg[i]);
  1316. if (rc)
  1317. goto bail;
  1318. vreg->test_reg[i] |= REGULATOR_BANK_WRITE;
  1319. }
  1320. /* Save the current clock control register state. */
  1321. rc = pm8xxx_readb(vreg->dev->parent, vreg->clk_ctrl_addr,
  1322. &vreg->clk_ctrl_reg);
  1323. if (rc)
  1324. goto bail;
  1325. /* Save the current sleep control register state. */
  1326. rc = pm8xxx_readb(vreg->dev->parent, vreg->sleep_ctrl_addr,
  1327. &vreg->sleep_ctrl_reg);
  1328. if (rc)
  1329. goto bail;
  1330. vreg->save_uV = 1; /* This is not a no-op. */
  1331. vreg->save_uV = _pm8058_smps_get_voltage(vreg);
  1332. if ((vreg->clk_ctrl_reg & SMPS_CLK_CTRL_MASK) == SMPS_CLK_CTRL_PFM)
  1333. vreg->optimum = REGULATOR_MODE_STANDBY;
  1334. else
  1335. vreg->optimum = REGULATOR_MODE_FAST;
  1336. /* Set advanced mode pull down enable based on platform data. */
  1337. rc = pm8058_vreg_write(vreg, vreg->test_addr,
  1338. (vreg->pdata->pull_down_enable
  1339. ? SMPS_ADVANCED_PULL_DOWN_ENABLE : 0)
  1340. | REGULATOR_BANK_SEL(6) | REGULATOR_BANK_WRITE,
  1341. REGULATOR_BANK_MASK | SMPS_ADVANCED_PULL_DOWN_ENABLE,
  1342. &vreg->test_reg[6]);
  1343. if (rc)
  1344. goto bail;
  1345. if (!SMPS_IN_ADVANCED_MODE(vreg)) {
  1346. /* Set legacy mode pull down enable based on platform data. */
  1347. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  1348. (vreg->pdata->pull_down_enable
  1349. ? SMPS_LEGACY_PULL_DOWN_ENABLE : 0),
  1350. SMPS_LEGACY_PULL_DOWN_ENABLE, &vreg->ctrl_reg);
  1351. if (rc)
  1352. goto bail;
  1353. }
  1354. bail:
  1355. return rc;
  1356. }
  1357. static int pm8058_init_lvs(struct pm8058_vreg *vreg)
  1358. {
  1359. int rc = 0;
  1360. vreg->optimum = REGULATOR_MODE_FAST;
  1361. /* Set pull down enable based on platform data. */
  1362. rc = pm8058_vreg_write(vreg, vreg->ctrl_addr,
  1363. (vreg->pdata->pull_down_enable
  1364. ? LVS_PULL_DOWN_ENABLE : LVS_PULL_DOWN_DISABLE),
  1365. LVS_PULL_DOWN_ENABLE_MASK, &vreg->ctrl_reg);
  1366. return rc;
  1367. }
  1368. static int pm8058_init_ncp(struct pm8058_vreg *vreg)
  1369. {
  1370. int rc = 0;
  1371. /* Save the current test1 register state. */
  1372. rc = pm8xxx_readb(vreg->dev->parent, vreg->test_addr,
  1373. &vreg->test_reg[0]);
  1374. if (rc)
  1375. goto bail;
  1376. vreg->optimum = REGULATOR_MODE_FAST;
  1377. bail:
  1378. return rc;
  1379. }
  1380. static int pm8058_init_regulator(struct pm8058_vreg *vreg)
  1381. {
  1382. static int master_enable_inited;
  1383. int rc = 0;
  1384. vreg->mode_initialized = 0;
  1385. if (!master_enable_inited) {
  1386. rc = pm8058_master_enable_init(vreg);
  1387. if (!rc)
  1388. master_enable_inited = 1;
  1389. }
  1390. /* save the current control register state */
  1391. rc = pm8xxx_readb(vreg->dev->parent, vreg->ctrl_addr, &vreg->ctrl_reg);
  1392. if (rc)
  1393. goto bail;
  1394. switch (vreg->type) {
  1395. case REGULATOR_TYPE_LDO:
  1396. rc = pm8058_init_ldo(vreg);
  1397. break;
  1398. case REGULATOR_TYPE_SMPS:
  1399. rc = pm8058_init_smps(vreg);
  1400. break;
  1401. case REGULATOR_TYPE_LVS:
  1402. rc = pm8058_init_lvs(vreg);
  1403. break;
  1404. case REGULATOR_TYPE_NCP:
  1405. rc = pm8058_init_ncp(vreg);
  1406. break;
  1407. }
  1408. bail:
  1409. if (rc)
  1410. pr_err("%s: pm8058_read/write failed; initial register states "
  1411. "unknown, rc=%d\n", __func__, rc);
  1412. return rc;
  1413. }
  1414. static int __devinit pm8058_vreg_probe(struct platform_device *pdev)
  1415. {
  1416. struct regulator_desc *rdesc;
  1417. struct pm8058_vreg *vreg;
  1418. const char *reg_name = NULL;
  1419. int rc = 0;
  1420. if (pdev == NULL)
  1421. return -EINVAL;
  1422. if (pdev->id >= 0 && pdev->id < PM8058_VREG_MAX) {
  1423. rdesc = &pm8058_vreg_descrip[pdev->id];
  1424. vreg = &pm8058_vreg[pdev->id];
  1425. vreg->pdata = pdev->dev.platform_data;
  1426. reg_name = pm8058_vreg_descrip[pdev->id].name;
  1427. vreg->dev = &pdev->dev;
  1428. rc = pm8058_init_regulator(vreg);
  1429. if (rc)
  1430. goto bail;
  1431. /* Disallow idle and normal modes if pin control isn't set. */
  1432. if (vreg->pdata->pin_ctrl == 0)
  1433. vreg->pdata->init_data.constraints.valid_modes_mask
  1434. &= ~(REGULATOR_MODE_NORMAL | REGULATOR_MODE_IDLE);
  1435. vreg->rdev = regulator_register(rdesc, &pdev->dev,
  1436. &vreg->pdata->init_data, vreg, NULL);
  1437. if (IS_ERR(vreg->rdev)) {
  1438. rc = PTR_ERR(vreg->rdev);
  1439. pr_err("%s: regulator_register failed for %s, rc=%d\n",
  1440. __func__, reg_name, rc);
  1441. }
  1442. } else {
  1443. rc = -ENODEV;
  1444. }
  1445. bail:
  1446. if (rc)
  1447. pr_err("%s: error for %s, rc=%d\n", __func__, reg_name, rc);
  1448. return rc;
  1449. }
  1450. static int __devexit pm8058_vreg_remove(struct platform_device *pdev)
  1451. {
  1452. regulator_unregister(pm8058_vreg[pdev->id].rdev);
  1453. return 0;
  1454. }
  1455. static struct platform_driver pm8058_vreg_driver = {
  1456. .probe = pm8058_vreg_probe,
  1457. .remove = __devexit_p(pm8058_vreg_remove),
  1458. .driver = {
  1459. .name = "pm8058-regulator",
  1460. .owner = THIS_MODULE,
  1461. },
  1462. };
  1463. static int __init pm8058_vreg_init(void)
  1464. {
  1465. return platform_driver_register(&pm8058_vreg_driver);
  1466. }
  1467. static void __exit pm8058_vreg_exit(void)
  1468. {
  1469. platform_driver_unregister(&pm8058_vreg_driver);
  1470. }
  1471. static void print_write_error(struct pm8058_vreg *vreg, int rc,
  1472. const char *func)
  1473. {
  1474. const char *reg_name = NULL;
  1475. ptrdiff_t id = vreg - pm8058_vreg;
  1476. if (id >= 0 && id < PM8058_VREG_MAX)
  1477. reg_name = pm8058_vreg_descrip[id].name;
  1478. pr_err("%s: pm8058_vreg_write failed for %s, rc=%d\n",
  1479. func, reg_name, rc);
  1480. }
  1481. subsys_initcall(pm8058_vreg_init);
  1482. module_exit(pm8058_vreg_exit);
  1483. MODULE_LICENSE("GPL v2");
  1484. MODULE_DESCRIPTION("PMIC8058 regulator driver");
  1485. MODULE_VERSION("1.0");
  1486. MODULE_ALIAS("platform:pm8058-regulator");