blob: f3fba1cc1379212345fa9128a4f3cf12b53d3d9c [file] [log] [blame]
Fabio Estevam0f568392018-05-23 16:17:37 -03001// SPDX-License-Identifier: GPL-2.0
2//
3// Regulator Driver for Freescale MC13892 PMIC
4//
5// Copyright 2010 Yong Shen <yong.shen@linaro.org>
6//
7// Based on draft driver from Arnaud Patard <arnaud.patard@rtp-net.org>
Yong Shen5e428d52010-12-14 14:00:55 +08008
9#include <linux/mfd/mc13892.h>
10#include <linux/regulator/machine.h>
11#include <linux/regulator/driver.h>
12#include <linux/platform_device.h>
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/init.h>
16#include <linux/err.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040017#include <linux/module.h>
Yong Shen5e428d52010-12-14 14:00:55 +080018#include "mc13xxx.h"
19
20#define MC13892_REVISION 7
21
22#define MC13892_POWERCTL0 13
23#define MC13892_POWERCTL0_USEROFFSPI 3
24#define MC13892_POWERCTL0_VCOINCELLVSEL 20
25#define MC13892_POWERCTL0_VCOINCELLVSEL_M (7<<20)
26#define MC13892_POWERCTL0_VCOINCELLEN (1<<23)
27
28#define MC13892_SWITCHERS0_SWxHI (1<<23)
29
30#define MC13892_SWITCHERS0 24
31#define MC13892_SWITCHERS0_SW1VSEL 0
32#define MC13892_SWITCHERS0_SW1VSEL_M (0x1f<<0)
33#define MC13892_SWITCHERS0_SW1HI (1<<23)
34#define MC13892_SWITCHERS0_SW1EN 0
35
36#define MC13892_SWITCHERS1 25
37#define MC13892_SWITCHERS1_SW2VSEL 0
38#define MC13892_SWITCHERS1_SW2VSEL_M (0x1f<<0)
39#define MC13892_SWITCHERS1_SW2HI (1<<23)
40#define MC13892_SWITCHERS1_SW2EN 0
41
42#define MC13892_SWITCHERS2 26
43#define MC13892_SWITCHERS2_SW3VSEL 0
44#define MC13892_SWITCHERS2_SW3VSEL_M (0x1f<<0)
45#define MC13892_SWITCHERS2_SW3HI (1<<23)
46#define MC13892_SWITCHERS2_SW3EN 0
47
48#define MC13892_SWITCHERS3 27
49#define MC13892_SWITCHERS3_SW4VSEL 0
50#define MC13892_SWITCHERS3_SW4VSEL_M (0x1f<<0)
51#define MC13892_SWITCHERS3_SW4HI (1<<23)
52#define MC13892_SWITCHERS3_SW4EN 0
53
54#define MC13892_SWITCHERS4 28
55#define MC13892_SWITCHERS4_SW1MODE 0
56#define MC13892_SWITCHERS4_SW1MODE_AUTO (8<<0)
57#define MC13892_SWITCHERS4_SW1MODE_M (0xf<<0)
58#define MC13892_SWITCHERS4_SW2MODE 10
59#define MC13892_SWITCHERS4_SW2MODE_AUTO (8<<10)
60#define MC13892_SWITCHERS4_SW2MODE_M (0xf<<10)
61
62#define MC13892_SWITCHERS5 29
63#define MC13892_SWITCHERS5_SW3MODE 0
64#define MC13892_SWITCHERS5_SW3MODE_AUTO (8<<0)
65#define MC13892_SWITCHERS5_SW3MODE_M (0xf<<0)
66#define MC13892_SWITCHERS5_SW4MODE 8
67#define MC13892_SWITCHERS5_SW4MODE_AUTO (8<<8)
68#define MC13892_SWITCHERS5_SW4MODE_M (0xf<<8)
69#define MC13892_SWITCHERS5_SWBSTEN (1<<20)
70
71#define MC13892_REGULATORSETTING0 30
72#define MC13892_REGULATORSETTING0_VGEN1VSEL 0
73#define MC13892_REGULATORSETTING0_VDIGVSEL 4
74#define MC13892_REGULATORSETTING0_VGEN2VSEL 6
75#define MC13892_REGULATORSETTING0_VPLLVSEL 9
76#define MC13892_REGULATORSETTING0_VUSB2VSEL 11
77#define MC13892_REGULATORSETTING0_VGEN3VSEL 14
78#define MC13892_REGULATORSETTING0_VCAMVSEL 16
79
80#define MC13892_REGULATORSETTING0_VGEN1VSEL_M (3<<0)
81#define MC13892_REGULATORSETTING0_VDIGVSEL_M (3<<4)
82#define MC13892_REGULATORSETTING0_VGEN2VSEL_M (7<<6)
83#define MC13892_REGULATORSETTING0_VPLLVSEL_M (3<<9)
84#define MC13892_REGULATORSETTING0_VUSB2VSEL_M (3<<11)
85#define MC13892_REGULATORSETTING0_VGEN3VSEL_M (1<<14)
86#define MC13892_REGULATORSETTING0_VCAMVSEL_M (3<<16)
87
88#define MC13892_REGULATORSETTING1 31
89#define MC13892_REGULATORSETTING1_VVIDEOVSEL 2
90#define MC13892_REGULATORSETTING1_VAUDIOVSEL 4
91#define MC13892_REGULATORSETTING1_VSDVSEL 6
92
93#define MC13892_REGULATORSETTING1_VVIDEOVSEL_M (3<<2)
94#define MC13892_REGULATORSETTING1_VAUDIOVSEL_M (3<<4)
95#define MC13892_REGULATORSETTING1_VSDVSEL_M (7<<6)
96
97#define MC13892_REGULATORMODE0 32
98#define MC13892_REGULATORMODE0_VGEN1EN (1<<0)
99#define MC13892_REGULATORMODE0_VGEN1STDBY (1<<1)
100#define MC13892_REGULATORMODE0_VGEN1MODE (1<<2)
101#define MC13892_REGULATORMODE0_VIOHIEN (1<<3)
102#define MC13892_REGULATORMODE0_VIOHISTDBY (1<<4)
103#define MC13892_REGULATORMODE0_VIOHIMODE (1<<5)
104#define MC13892_REGULATORMODE0_VDIGEN (1<<9)
105#define MC13892_REGULATORMODE0_VDIGSTDBY (1<<10)
106#define MC13892_REGULATORMODE0_VDIGMODE (1<<11)
107#define MC13892_REGULATORMODE0_VGEN2EN (1<<12)
108#define MC13892_REGULATORMODE0_VGEN2STDBY (1<<13)
109#define MC13892_REGULATORMODE0_VGEN2MODE (1<<14)
110#define MC13892_REGULATORMODE0_VPLLEN (1<<15)
111#define MC13892_REGULATORMODE0_VPLLSTDBY (1<<16)
112#define MC13892_REGULATORMODE0_VPLLMODE (1<<17)
113#define MC13892_REGULATORMODE0_VUSB2EN (1<<18)
114#define MC13892_REGULATORMODE0_VUSB2STDBY (1<<19)
115#define MC13892_REGULATORMODE0_VUSB2MODE (1<<20)
116
117#define MC13892_REGULATORMODE1 33
118#define MC13892_REGULATORMODE1_VGEN3EN (1<<0)
119#define MC13892_REGULATORMODE1_VGEN3STDBY (1<<1)
120#define MC13892_REGULATORMODE1_VGEN3MODE (1<<2)
121#define MC13892_REGULATORMODE1_VCAMEN (1<<6)
122#define MC13892_REGULATORMODE1_VCAMSTDBY (1<<7)
123#define MC13892_REGULATORMODE1_VCAMMODE (1<<8)
124#define MC13892_REGULATORMODE1_VCAMCONFIGEN (1<<9)
125#define MC13892_REGULATORMODE1_VVIDEOEN (1<<12)
126#define MC13892_REGULATORMODE1_VVIDEOSTDBY (1<<13)
127#define MC13892_REGULATORMODE1_VVIDEOMODE (1<<14)
128#define MC13892_REGULATORMODE1_VAUDIOEN (1<<15)
129#define MC13892_REGULATORMODE1_VAUDIOSTDBY (1<<16)
130#define MC13892_REGULATORMODE1_VAUDIOMODE (1<<17)
131#define MC13892_REGULATORMODE1_VSDEN (1<<18)
132#define MC13892_REGULATORMODE1_VSDSTDBY (1<<19)
133#define MC13892_REGULATORMODE1_VSDMODE (1<<20)
134
135#define MC13892_POWERMISC 34
136#define MC13892_POWERMISC_GPO1EN (1<<6)
137#define MC13892_POWERMISC_GPO2EN (1<<8)
138#define MC13892_POWERMISC_GPO3EN (1<<10)
139#define MC13892_POWERMISC_GPO4EN (1<<12)
140#define MC13892_POWERMISC_PWGT1SPIEN (1<<15)
141#define MC13892_POWERMISC_PWGT2SPIEN (1<<16)
142#define MC13892_POWERMISC_GPO4ADINEN (1<<21)
143
144#define MC13892_POWERMISC_PWGTSPI_M (3 << 15)
145
146#define MC13892_USB1 50
147#define MC13892_USB1_VUSBEN (1<<3)
148
Axel Lin34e74f32012-06-08 15:41:48 +0800149static const unsigned int mc13892_vcoincell[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800150 2500000, 2700000, 2800000, 2900000, 3000000, 3100000,
151 3200000, 3300000,
152};
153
Axel Lin34e74f32012-06-08 15:41:48 +0800154static const unsigned int mc13892_sw1[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800155 600000, 625000, 650000, 675000, 700000, 725000,
156 750000, 775000, 800000, 825000, 850000, 875000,
157 900000, 925000, 950000, 975000, 1000000, 1025000,
158 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
159 1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
160 1350000, 1375000
161};
162
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600163/*
164 * Note: this table is used to derive SWxVSEL by index into
165 * the array. Offset the values by the index of 1100000uV
166 * to get the actual register value for that voltage selector
167 * if the HI bit is to be set as well.
168 */
169#define MC13892_SWxHI_SEL_OFFSET 20
170
Axel Lin34e74f32012-06-08 15:41:48 +0800171static const unsigned int mc13892_sw[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800172 600000, 625000, 650000, 675000, 700000, 725000,
173 750000, 775000, 800000, 825000, 850000, 875000,
174 900000, 925000, 950000, 975000, 1000000, 1025000,
175 1050000, 1075000, 1100000, 1125000, 1150000, 1175000,
176 1200000, 1225000, 1250000, 1275000, 1300000, 1325000,
177 1350000, 1375000, 1400000, 1425000, 1450000, 1475000,
178 1500000, 1525000, 1550000, 1575000, 1600000, 1625000,
179 1650000, 1675000, 1700000, 1725000, 1750000, 1775000,
180 1800000, 1825000, 1850000, 1875000
181};
182
Axel Lin34e74f32012-06-08 15:41:48 +0800183static const unsigned int mc13892_swbst[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800184 5000000,
185};
186
Axel Lin34e74f32012-06-08 15:41:48 +0800187static const unsigned int mc13892_viohi[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800188 2775000,
189};
190
Axel Lin34e74f32012-06-08 15:41:48 +0800191static const unsigned int mc13892_vpll[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800192 1050000, 1250000, 1650000, 1800000,
193};
194
Axel Lin34e74f32012-06-08 15:41:48 +0800195static const unsigned int mc13892_vdig[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800196 1050000, 1250000, 1650000, 1800000,
197};
198
Axel Lin34e74f32012-06-08 15:41:48 +0800199static const unsigned int mc13892_vsd[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800200 1800000, 2000000, 2600000, 2700000,
201 2800000, 2900000, 3000000, 3150000,
202};
203
Axel Lin34e74f32012-06-08 15:41:48 +0800204static const unsigned int mc13892_vusb2[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800205 2400000, 2600000, 2700000, 2775000,
206};
207
Axel Lin34e74f32012-06-08 15:41:48 +0800208static const unsigned int mc13892_vvideo[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800209 2700000, 2775000, 2500000, 2600000,
210};
211
Axel Lin34e74f32012-06-08 15:41:48 +0800212static const unsigned int mc13892_vaudio[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800213 2300000, 2500000, 2775000, 3000000,
214};
215
Axel Lin34e74f32012-06-08 15:41:48 +0800216static const unsigned int mc13892_vcam[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800217 2500000, 2600000, 2750000, 3000000,
218};
219
Axel Lin34e74f32012-06-08 15:41:48 +0800220static const unsigned int mc13892_vgen1[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800221 1200000, 1500000, 2775000, 3150000,
222};
223
Axel Lin34e74f32012-06-08 15:41:48 +0800224static const unsigned int mc13892_vgen2[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800225 1200000, 1500000, 1600000, 1800000,
226 2700000, 2800000, 3000000, 3150000,
227};
228
Axel Lin34e74f32012-06-08 15:41:48 +0800229static const unsigned int mc13892_vgen3[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800230 1800000, 2900000,
231};
232
Axel Lin34e74f32012-06-08 15:41:48 +0800233static const unsigned int mc13892_vusb[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800234 3300000,
235};
236
Axel Lin34e74f32012-06-08 15:41:48 +0800237static const unsigned int mc13892_gpo[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800238 2750000,
239};
240
Axel Lin34e74f32012-06-08 15:41:48 +0800241static const unsigned int mc13892_pwgtdrv[] = {
Yong Shen5e428d52010-12-14 14:00:55 +0800242 5000000,
243};
244
245static struct regulator_ops mc13892_gpo_regulator_ops;
Yong Shen5e428d52010-12-14 14:00:55 +0800246static struct regulator_ops mc13892_sw_regulator_ops;
247
248
249#define MC13892_FIXED_DEFINE(name, reg, voltages) \
250 MC13xxx_FIXED_DEFINE(MC13892_, name, reg, voltages, \
251 mc13xxx_fixed_regulator_ops)
252
253#define MC13892_GPO_DEFINE(name, reg, voltages) \
254 MC13xxx_GPO_DEFINE(MC13892_, name, reg, voltages, \
255 mc13892_gpo_regulator_ops)
256
257#define MC13892_SW_DEFINE(name, reg, vsel_reg, voltages) \
258 MC13xxx_DEFINE(MC13892_, name, reg, vsel_reg, voltages, \
259 mc13892_sw_regulator_ops)
260
261#define MC13892_DEFINE_REGU(name, reg, vsel_reg, voltages) \
262 MC13xxx_DEFINE(MC13892_, name, reg, vsel_reg, voltages, \
263 mc13xxx_regulator_ops)
264
265static struct mc13xxx_regulator mc13892_regulators[] = {
Axel Lin327e15a2011-05-10 19:10:36 +0800266 MC13892_DEFINE_REGU(VCOINCELL, POWERCTL0, POWERCTL0, mc13892_vcoincell),
Yong Shen5e428d52010-12-14 14:00:55 +0800267 MC13892_SW_DEFINE(SW1, SWITCHERS0, SWITCHERS0, mc13892_sw1),
268 MC13892_SW_DEFINE(SW2, SWITCHERS1, SWITCHERS1, mc13892_sw),
269 MC13892_SW_DEFINE(SW3, SWITCHERS2, SWITCHERS2, mc13892_sw),
270 MC13892_SW_DEFINE(SW4, SWITCHERS3, SWITCHERS3, mc13892_sw),
271 MC13892_FIXED_DEFINE(SWBST, SWITCHERS5, mc13892_swbst),
272 MC13892_FIXED_DEFINE(VIOHI, REGULATORMODE0, mc13892_viohi),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900273 MC13892_DEFINE_REGU(VPLL, REGULATORMODE0, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800274 mc13892_vpll),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900275 MC13892_DEFINE_REGU(VDIG, REGULATORMODE0, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800276 mc13892_vdig),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900277 MC13892_DEFINE_REGU(VSD, REGULATORMODE1, REGULATORSETTING1,
Yong Shen5e428d52010-12-14 14:00:55 +0800278 mc13892_vsd),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900279 MC13892_DEFINE_REGU(VUSB2, REGULATORMODE0, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800280 mc13892_vusb2),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900281 MC13892_DEFINE_REGU(VVIDEO, REGULATORMODE1, REGULATORSETTING1,
Yong Shen5e428d52010-12-14 14:00:55 +0800282 mc13892_vvideo),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900283 MC13892_DEFINE_REGU(VAUDIO, REGULATORMODE1, REGULATORSETTING1,
Yong Shen5e428d52010-12-14 14:00:55 +0800284 mc13892_vaudio),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900285 MC13892_DEFINE_REGU(VCAM, REGULATORMODE1, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800286 mc13892_vcam),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900287 MC13892_DEFINE_REGU(VGEN1, REGULATORMODE0, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800288 mc13892_vgen1),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900289 MC13892_DEFINE_REGU(VGEN2, REGULATORMODE0, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800290 mc13892_vgen2),
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900291 MC13892_DEFINE_REGU(VGEN3, REGULATORMODE1, REGULATORSETTING0,
Yong Shen5e428d52010-12-14 14:00:55 +0800292 mc13892_vgen3),
293 MC13892_FIXED_DEFINE(VUSB, USB1, mc13892_vusb),
294 MC13892_GPO_DEFINE(GPO1, POWERMISC, mc13892_gpo),
295 MC13892_GPO_DEFINE(GPO2, POWERMISC, mc13892_gpo),
296 MC13892_GPO_DEFINE(GPO3, POWERMISC, mc13892_gpo),
297 MC13892_GPO_DEFINE(GPO4, POWERMISC, mc13892_gpo),
298 MC13892_GPO_DEFINE(PWGT1SPI, POWERMISC, mc13892_pwgtdrv),
299 MC13892_GPO_DEFINE(PWGT2SPI, POWERMISC, mc13892_pwgtdrv),
300};
301
Mark Brown27315cf2010-12-16 15:29:56 +0000302static int mc13892_powermisc_rmw(struct mc13xxx_regulator_priv *priv, u32 mask,
303 u32 val)
Yong Shen5e428d52010-12-14 14:00:55 +0800304{
305 struct mc13xxx *mc13892 = priv->mc13xxx;
306 int ret;
307 u32 valread;
308
309 BUG_ON(val & ~mask);
310
Axel Lin825d1052012-07-19 11:17:15 +0800311 mc13xxx_lock(priv->mc13xxx);
Yong Shen5e428d52010-12-14 14:00:55 +0800312 ret = mc13xxx_reg_read(mc13892, MC13892_POWERMISC, &valread);
313 if (ret)
Axel Lin825d1052012-07-19 11:17:15 +0800314 goto out;
Yong Shen5e428d52010-12-14 14:00:55 +0800315
316 /* Update the stored state for Power Gates. */
317 priv->powermisc_pwgt_state =
318 (priv->powermisc_pwgt_state & ~mask) | val;
319 priv->powermisc_pwgt_state &= MC13892_POWERMISC_PWGTSPI_M;
320
321 /* Construct the new register value */
322 valread = (valread & ~mask) | val;
323 /* Overwrite the PWGTxEN with the stored version */
324 valread = (valread & ~MC13892_POWERMISC_PWGTSPI_M) |
325 priv->powermisc_pwgt_state;
326
Axel Lin825d1052012-07-19 11:17:15 +0800327 ret = mc13xxx_reg_write(mc13892, MC13892_POWERMISC, valread);
328out:
329 mc13xxx_unlock(priv->mc13xxx);
330 return ret;
Yong Shen5e428d52010-12-14 14:00:55 +0800331}
332
333static int mc13892_gpo_regulator_enable(struct regulator_dev *rdev)
334{
335 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
336 int id = rdev_get_id(rdev);
Yong Shen5e428d52010-12-14 14:00:55 +0800337 u32 en_val = mc13892_regulators[id].enable_bit;
338 u32 mask = mc13892_regulators[id].enable_bit;
339
340 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
341
342 /* Power Gate enable value is 0 */
343 if (id == MC13892_PWGT1SPI || id == MC13892_PWGT2SPI)
344 en_val = 0;
345
346 if (id == MC13892_GPO4)
347 mask |= MC13892_POWERMISC_GPO4ADINEN;
348
Axel Lin825d1052012-07-19 11:17:15 +0800349 return mc13892_powermisc_rmw(priv, mask, en_val);
Yong Shen5e428d52010-12-14 14:00:55 +0800350}
351
352static int mc13892_gpo_regulator_disable(struct regulator_dev *rdev)
353{
354 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
355 int id = rdev_get_id(rdev);
Yong Shen5e428d52010-12-14 14:00:55 +0800356 u32 dis_val = 0;
357
358 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
359
360 /* Power Gate disable value is 1 */
361 if (id == MC13892_PWGT1SPI || id == MC13892_PWGT2SPI)
362 dis_val = mc13892_regulators[id].enable_bit;
363
Axel Lin825d1052012-07-19 11:17:15 +0800364 return mc13892_powermisc_rmw(priv, mc13892_regulators[id].enable_bit,
Yong Shen5e428d52010-12-14 14:00:55 +0800365 dis_val);
Yong Shen5e428d52010-12-14 14:00:55 +0800366}
367
368static int mc13892_gpo_regulator_is_enabled(struct regulator_dev *rdev)
369{
370 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
371 int ret, id = rdev_get_id(rdev);
372 unsigned int val;
373
374 mc13xxx_lock(priv->mc13xxx);
375 ret = mc13xxx_reg_read(priv->mc13xxx, mc13892_regulators[id].reg, &val);
376 mc13xxx_unlock(priv->mc13xxx);
377
378 if (ret)
379 return ret;
380
381 /* Power Gates state is stored in powermisc_pwgt_state
382 * where the meaning of bits is negated */
383 val = (val & ~MC13892_POWERMISC_PWGTSPI_M) |
384 (priv->powermisc_pwgt_state ^ MC13892_POWERMISC_PWGTSPI_M);
385
386 return (val & mc13892_regulators[id].enable_bit) != 0;
387}
388
389
390static struct regulator_ops mc13892_gpo_regulator_ops = {
391 .enable = mc13892_gpo_regulator_enable,
392 .disable = mc13892_gpo_regulator_disable,
393 .is_enabled = mc13892_gpo_regulator_is_enabled,
Axel Lin34e74f32012-06-08 15:41:48 +0800394 .list_voltage = regulator_list_voltage_table,
Yong Shen5e428d52010-12-14 14:00:55 +0800395 .set_voltage = mc13xxx_fixed_regulator_set_voltage,
Yong Shen5e428d52010-12-14 14:00:55 +0800396};
397
Axel Linf1dcf9e2012-07-19 15:11:39 +0800398static int mc13892_sw_regulator_get_voltage_sel(struct regulator_dev *rdev)
Yong Shen5e428d52010-12-14 14:00:55 +0800399{
400 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
401 int ret, id = rdev_get_id(rdev);
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600402 unsigned int val, selector;
Yong Shen5e428d52010-12-14 14:00:55 +0800403
404 dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
405
406 mc13xxx_lock(priv->mc13xxx);
407 ret = mc13xxx_reg_read(priv->mc13xxx,
408 mc13892_regulators[id].vsel_reg, &val);
409 mc13xxx_unlock(priv->mc13xxx);
410 if (ret)
411 return ret;
412
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600413 /*
414 * Figure out if the HI bit is set inside the switcher mode register
415 * since this means the selector value we return is at a different
416 * offset into the selector table.
417 *
418 * According to the MC13892 documentation note 59 (Table 47) the SW1
419 * buck switcher does not support output range programming therefore
420 * the HI bit must always remain 0. So do not do anything strange if
421 * our register is MC13892_SWITCHERS0.
422 */
Yong Shen5e428d52010-12-14 14:00:55 +0800423
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600424 selector = val & mc13892_regulators[id].vsel_mask;
Yong Shen5e428d52010-12-14 14:00:55 +0800425
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600426 if ((mc13892_regulators[id].vsel_reg != MC13892_SWITCHERS0) &&
427 (val & MC13892_SWITCHERS0_SWxHI)) {
428 selector += MC13892_SWxHI_SEL_OFFSET;
429 }
430
431 dev_dbg(rdev_get_dev(rdev), "%s id: %d val: 0x%08x selector: %d\n",
432 __func__, id, val, selector);
433
434 return selector;
Yong Shen5e428d52010-12-14 14:00:55 +0800435}
436
Axel Lin939b62d2012-03-20 10:46:35 +0800437static int mc13892_sw_regulator_set_voltage_sel(struct regulator_dev *rdev,
438 unsigned selector)
Yong Shen5e428d52010-12-14 14:00:55 +0800439{
440 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
Axel Lin8db98442012-07-19 15:10:31 +0800441 int volt, mask, id = rdev_get_id(rdev);
442 u32 reg_value;
Yong Shen5e428d52010-12-14 14:00:55 +0800443 int ret;
444
Axel Lin8db98442012-07-19 15:10:31 +0800445 volt = rdev->desc->volt_table[selector];
446 mask = mc13892_regulators[id].vsel_mask;
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600447 reg_value = selector;
Axel Lin8db98442012-07-19 15:10:31 +0800448
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600449 /*
450 * Don't mess with the HI bit or support HI voltage offsets for SW1.
451 *
452 * Since the get_voltage_sel callback has given a fudged value for
453 * the selector offset, we need to back out that offset if HI is
454 * to be set so we write the correct value to the register.
455 *
456 * The HI bit addition and selector offset handling COULD be more
457 * complicated by shifting and masking off the voltage selector part
458 * of the register then logical OR it back in, but since the selector
459 * is at bits 4:0 there is very little point. This makes the whole
460 * thing more readable and we do far less work.
461 */
462
463 if (mc13892_regulators[id].vsel_reg != MC13892_SWITCHERS0) {
Axel Lin78040b62013-04-24 11:51:12 +0800464 mask |= MC13892_SWITCHERS0_SWxHI;
465
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600466 if (volt > 1375000) {
467 reg_value -= MC13892_SWxHI_SEL_OFFSET;
468 reg_value |= MC13892_SWITCHERS0_SWxHI;
Axel Lin78040b62013-04-24 11:51:12 +0800469 } else {
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600470 reg_value &= ~MC13892_SWITCHERS0_SWxHI;
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600471 }
Axel Lin8db98442012-07-19 15:10:31 +0800472 }
Yong Shen5e428d52010-12-14 14:00:55 +0800473
474 mc13xxx_lock(priv->mc13xxx);
Jingoo Hanc6a21f22013-10-14 17:51:19 +0900475 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].vsel_reg,
476 mask, reg_value);
Yong Shen5e428d52010-12-14 14:00:55 +0800477 mc13xxx_unlock(priv->mc13xxx);
478
479 return ret;
480}
481
482static struct regulator_ops mc13892_sw_regulator_ops = {
Axel Lin34e74f32012-06-08 15:41:48 +0800483 .list_voltage = regulator_list_voltage_table,
Axel Lin3bdf5992013-04-24 20:42:29 +0800484 .map_voltage = regulator_map_voltage_ascend,
Axel Lin939b62d2012-03-20 10:46:35 +0800485 .set_voltage_sel = mc13892_sw_regulator_set_voltage_sel,
Axel Linf1dcf9e2012-07-19 15:11:39 +0800486 .get_voltage_sel = mc13892_sw_regulator_get_voltage_sel,
Yong Shen5e428d52010-12-14 14:00:55 +0800487};
488
489static int mc13892_vcam_set_mode(struct regulator_dev *rdev, unsigned int mode)
490{
491 unsigned int en_val = 0;
492 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
493 int ret, id = rdev_get_id(rdev);
494
495 if (mode == REGULATOR_MODE_FAST)
496 en_val = MC13892_REGULATORMODE1_VCAMCONFIGEN;
497
498 mc13xxx_lock(priv->mc13xxx);
499 ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13892_regulators[id].reg,
500 MC13892_REGULATORMODE1_VCAMCONFIGEN, en_val);
501 mc13xxx_unlock(priv->mc13xxx);
502
503 return ret;
504}
505
Mark Brown27315cf2010-12-16 15:29:56 +0000506static unsigned int mc13892_vcam_get_mode(struct regulator_dev *rdev)
Yong Shen5e428d52010-12-14 14:00:55 +0800507{
508 struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
509 int ret, id = rdev_get_id(rdev);
510 unsigned int val;
511
512 mc13xxx_lock(priv->mc13xxx);
513 ret = mc13xxx_reg_read(priv->mc13xxx, mc13892_regulators[id].reg, &val);
514 mc13xxx_unlock(priv->mc13xxx);
515
516 if (ret)
517 return ret;
518
519 if (val & MC13892_REGULATORMODE1_VCAMCONFIGEN)
520 return REGULATOR_MODE_FAST;
521
522 return REGULATOR_MODE_NORMAL;
523}
524
Guodong Xu39f54602014-08-19 18:07:41 +0800525static struct regulator_ops mc13892_vcam_ops;
Yong Shen5e428d52010-12-14 14:00:55 +0800526
Bill Pembertona5023572012-11-19 13:22:22 -0500527static int mc13892_regulator_probe(struct platform_device *pdev)
Yong Shen5e428d52010-12-14 14:00:55 +0800528{
529 struct mc13xxx_regulator_priv *priv;
530 struct mc13xxx *mc13892 = dev_get_drvdata(pdev->dev.parent);
Samuel Ortizc8a03c92011-04-08 01:55:01 +0200531 struct mc13xxx_regulator_platform_data *pdata =
532 dev_get_platdata(&pdev->dev);
Shawn Guo93bcb232011-12-21 23:00:46 +0800533 struct mc13xxx_regulator_init_data *mc13xxx_data;
Axel Lina9d58012012-04-10 13:51:06 +0800534 struct regulator_config config = { };
Yong Shen5e428d52010-12-14 14:00:55 +0800535 int i, ret;
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400536 int num_regulators = 0;
Yong Shen5e428d52010-12-14 14:00:55 +0800537 u32 val;
538
Shawn Guo93bcb232011-12-21 23:00:46 +0800539 num_regulators = mc13xxx_get_num_regulators_dt(pdev);
Matt Sealey2c8a5dc2013-01-21 12:25:45 -0600540
Shawn Guo93bcb232011-12-21 23:00:46 +0800541 if (num_regulators <= 0 && pdata)
542 num_regulators = pdata->num_regulators;
543 if (num_regulators <= 0)
544 return -EINVAL;
545
Kees Cook0ed2dd02018-05-08 16:08:53 -0700546 priv = devm_kzalloc(&pdev->dev,
547 struct_size(priv, regulators, num_regulators),
548 GFP_KERNEL);
Yong Shen5e428d52010-12-14 14:00:55 +0800549 if (!priv)
550 return -ENOMEM;
551
Shawn Guo93bcb232011-12-21 23:00:46 +0800552 priv->num_regulators = num_regulators;
Yong Shen5e428d52010-12-14 14:00:55 +0800553 priv->mc13xxx_regulators = mc13892_regulators;
554 priv->mc13xxx = mc13892;
Shawn Guo93bcb232011-12-21 23:00:46 +0800555 platform_set_drvdata(pdev, priv);
Yong Shen5e428d52010-12-14 14:00:55 +0800556
557 mc13xxx_lock(mc13892);
558 ret = mc13xxx_reg_read(mc13892, MC13892_REVISION, &val);
559 if (ret)
Axel Lin5777d9b2012-03-15 12:49:09 +0800560 goto err_unlock;
Yong Shen5e428d52010-12-14 14:00:55 +0800561
Matt Sealeyccf3ed72013-01-21 11:38:40 -0600562 /* enable switch auto mode (on 2.0A silicon only) */
Yong Shen5e428d52010-12-14 14:00:55 +0800563 if ((val & 0x0000FFFF) == 0x45d0) {
564 ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS4,
565 MC13892_SWITCHERS4_SW1MODE_M |
566 MC13892_SWITCHERS4_SW2MODE_M,
567 MC13892_SWITCHERS4_SW1MODE_AUTO |
568 MC13892_SWITCHERS4_SW2MODE_AUTO);
569 if (ret)
Axel Lin5777d9b2012-03-15 12:49:09 +0800570 goto err_unlock;
Yong Shen5e428d52010-12-14 14:00:55 +0800571
Axel Lin923430c2011-01-03 23:57:38 +0800572 ret = mc13xxx_reg_rmw(mc13892, MC13892_SWITCHERS5,
Yong Shen5e428d52010-12-14 14:00:55 +0800573 MC13892_SWITCHERS5_SW3MODE_M |
574 MC13892_SWITCHERS5_SW4MODE_M,
575 MC13892_SWITCHERS5_SW3MODE_AUTO |
576 MC13892_SWITCHERS5_SW4MODE_AUTO);
577 if (ret)
Axel Lin5777d9b2012-03-15 12:49:09 +0800578 goto err_unlock;
Yong Shen5e428d52010-12-14 14:00:55 +0800579 }
580 mc13xxx_unlock(mc13892);
581
Guodong Xu39f54602014-08-19 18:07:41 +0800582 /* update mc13892_vcam ops */
583 memcpy(&mc13892_vcam_ops, mc13892_regulators[MC13892_VCAM].desc.ops,
584 sizeof(struct regulator_ops));
585 mc13892_vcam_ops.set_mode = mc13892_vcam_set_mode,
586 mc13892_vcam_ops.get_mode = mc13892_vcam_get_mode,
587 mc13892_regulators[MC13892_VCAM].desc.ops = &mc13892_vcam_ops;
Shawn Guo93bcb232011-12-21 23:00:46 +0800588
589 mc13xxx_data = mc13xxx_parse_regulators_dt(pdev, mc13892_regulators,
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400590 ARRAY_SIZE(mc13892_regulators));
Matt Sealey2c8a5dc2013-01-21 12:25:45 -0600591
Alexander Shiyaneb0d8e72013-04-27 10:29:24 +0400592 for (i = 0; i < priv->num_regulators; i++) {
Shawn Guo93bcb232011-12-21 23:00:46 +0800593 struct regulator_init_data *init_data;
594 struct regulator_desc *desc;
595 struct device_node *node = NULL;
596 int id;
597
598 if (mc13xxx_data) {
599 id = mc13xxx_data[i].id;
600 init_data = mc13xxx_data[i].init_data;
601 node = mc13xxx_data[i].node;
602 } else {
603 id = pdata->regulators[i].id;
604 init_data = pdata->regulators[i].init_data;
605 }
606 desc = &mc13892_regulators[id].desc;
607
Axel Lina9d58012012-04-10 13:51:06 +0800608 config.dev = &pdev->dev;
609 config.init_data = init_data;
610 config.driver_data = priv;
611 config.of_node = node;
Yong Shen5e428d52010-12-14 14:00:55 +0800612
Sachin Kamat8c0b4ab2013-09-04 12:01:00 +0530613 priv->regulators[i] = devm_regulator_register(&pdev->dev, desc,
614 &config);
Yong Shen5e428d52010-12-14 14:00:55 +0800615 if (IS_ERR(priv->regulators[i])) {
616 dev_err(&pdev->dev, "failed to register regulator %s\n",
617 mc13892_regulators[i].desc.name);
Sachin Kamat8c0b4ab2013-09-04 12:01:00 +0530618 return PTR_ERR(priv->regulators[i]);
Yong Shen5e428d52010-12-14 14:00:55 +0800619 }
620 }
621
Yong Shen5e428d52010-12-14 14:00:55 +0800622 return 0;
Yong Shen5e428d52010-12-14 14:00:55 +0800623
Axel Lin5777d9b2012-03-15 12:49:09 +0800624err_unlock:
Yong Shen5e428d52010-12-14 14:00:55 +0800625 mc13xxx_unlock(mc13892);
Yong Shen5e428d52010-12-14 14:00:55 +0800626 return ret;
627}
628
Yong Shen5e428d52010-12-14 14:00:55 +0800629static struct platform_driver mc13892_regulator_driver = {
630 .driver = {
631 .name = "mc13892-regulator",
Yong Shen5e428d52010-12-14 14:00:55 +0800632 },
Yong Shen5e428d52010-12-14 14:00:55 +0800633 .probe = mc13892_regulator_probe,
634};
635
636static int __init mc13892_regulator_init(void)
637{
638 return platform_driver_register(&mc13892_regulator_driver);
639}
640subsys_initcall(mc13892_regulator_init);
641
642static void __exit mc13892_regulator_exit(void)
643{
644 platform_driver_unregister(&mc13892_regulator_driver);
645}
646module_exit(mc13892_regulator_exit);
647
648MODULE_LICENSE("GPL v2");
649MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
650MODULE_DESCRIPTION("Regulator Driver for Freescale MC13892 PMIC");
651MODULE_ALIAS("platform:mc13892-regulator");