1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
5 #include <linux/slab.h>
6 #include <linux/device.h>
7 #include <linux/module.h>
8 #include <linux/mfd/syscon.h>
11 #include <linux/platform_device.h>
13 #include <linux/of_address.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/of_regulator.h>
17 #include <linux/regulator/machine.h>
19 #define LDO_RAMP_UP_UNIT_IN_CYCLES 64 /* 64 cycles per step */
20 #define LDO_RAMP_UP_FREQ_IN_MHZ 24 /* cycle based on 24M OSC */
22 #define LDO_POWER_GATE 0x00
23 #define LDO_FET_FULL_ON 0x1f
25 struct anatop_regulator
{
27 struct regmap
*anatop
;
36 struct regulator_desc rdesc
;
37 struct regulator_init_data
*initdata
;
42 static int anatop_regmap_set_voltage_time_sel(struct regulator_dev
*reg
,
46 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
50 /* check whether need to care about LDO ramp up speed */
51 if (anatop_reg
->delay_bit_width
&& new_sel
> old_sel
) {
53 * the delay for LDO ramp up time is
54 * based on the register setting, we need
55 * to calculate how many steps LDO need to
56 * ramp up, and how much delay needed. (us)
58 regmap_read(anatop_reg
->anatop
, anatop_reg
->delay_reg
, &val
);
59 val
= (val
>> anatop_reg
->delay_bit_shift
) &
60 ((1 << anatop_reg
->delay_bit_width
) - 1);
61 ret
= (new_sel
- old_sel
) * (LDO_RAMP_UP_UNIT_IN_CYCLES
<<
62 val
) / LDO_RAMP_UP_FREQ_IN_MHZ
+ 1;
68 static int anatop_regmap_enable(struct regulator_dev
*reg
)
70 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
73 sel
= anatop_reg
->bypass
? LDO_FET_FULL_ON
: anatop_reg
->sel
;
74 return regulator_set_voltage_sel_regmap(reg
, sel
);
77 static int anatop_regmap_disable(struct regulator_dev
*reg
)
79 return regulator_set_voltage_sel_regmap(reg
, LDO_POWER_GATE
);
82 static int anatop_regmap_is_enabled(struct regulator_dev
*reg
)
84 return regulator_get_voltage_sel_regmap(reg
) != LDO_POWER_GATE
;
87 static int anatop_regmap_core_set_voltage_sel(struct regulator_dev
*reg
,
90 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
93 if (anatop_reg
->bypass
|| !anatop_regmap_is_enabled(reg
)) {
94 anatop_reg
->sel
= selector
;
98 ret
= regulator_set_voltage_sel_regmap(reg
, selector
);
100 anatop_reg
->sel
= selector
;
104 static int anatop_regmap_core_get_voltage_sel(struct regulator_dev
*reg
)
106 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
108 if (anatop_reg
->bypass
|| !anatop_regmap_is_enabled(reg
))
109 return anatop_reg
->sel
;
111 return regulator_get_voltage_sel_regmap(reg
);
114 static int anatop_regmap_get_bypass(struct regulator_dev
*reg
, bool *enable
)
116 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
119 sel
= regulator_get_voltage_sel_regmap(reg
);
120 if (sel
== LDO_FET_FULL_ON
)
121 WARN_ON(!anatop_reg
->bypass
);
122 else if (sel
!= LDO_POWER_GATE
)
123 WARN_ON(anatop_reg
->bypass
);
125 *enable
= anatop_reg
->bypass
;
129 static int anatop_regmap_set_bypass(struct regulator_dev
*reg
, bool enable
)
131 struct anatop_regulator
*anatop_reg
= rdev_get_drvdata(reg
);
134 if (enable
== anatop_reg
->bypass
)
137 sel
= enable
? LDO_FET_FULL_ON
: anatop_reg
->sel
;
138 anatop_reg
->bypass
= enable
;
140 return regulator_set_voltage_sel_regmap(reg
, sel
);
143 static struct regulator_ops anatop_rops
= {
144 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
145 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
146 .list_voltage
= regulator_list_voltage_linear
,
147 .map_voltage
= regulator_map_voltage_linear
,
150 static struct regulator_ops anatop_core_rops
= {
151 .enable
= anatop_regmap_enable
,
152 .disable
= anatop_regmap_disable
,
153 .is_enabled
= anatop_regmap_is_enabled
,
154 .set_voltage_sel
= anatop_regmap_core_set_voltage_sel
,
155 .set_voltage_time_sel
= anatop_regmap_set_voltage_time_sel
,
156 .get_voltage_sel
= anatop_regmap_core_get_voltage_sel
,
157 .list_voltage
= regulator_list_voltage_linear
,
158 .map_voltage
= regulator_map_voltage_linear
,
159 .get_bypass
= anatop_regmap_get_bypass
,
160 .set_bypass
= anatop_regmap_set_bypass
,
163 static int anatop_regulator_probe(struct platform_device
*pdev
)
165 struct device
*dev
= &pdev
->dev
;
166 struct device_node
*np
= dev
->of_node
;
167 struct device_node
*anatop_np
;
168 struct regulator_desc
*rdesc
;
169 struct regulator_dev
*rdev
;
170 struct anatop_regulator
*sreg
;
171 struct regulator_init_data
*initdata
;
172 struct regulator_config config
= { };
176 sreg
= devm_kzalloc(dev
, sizeof(*sreg
), GFP_KERNEL
);
180 rdesc
= &sreg
->rdesc
;
181 rdesc
->type
= REGULATOR_VOLTAGE
;
182 rdesc
->owner
= THIS_MODULE
;
184 of_property_read_string(np
, "regulator-name", &rdesc
->name
);
186 dev_err(dev
, "failed to get a regulator-name\n");
190 initdata
= of_get_regulator_init_data(dev
, np
, rdesc
);
194 initdata
->supply_regulator
= "vin";
195 sreg
->initdata
= initdata
;
197 anatop_np
= of_get_parent(np
);
200 sreg
->anatop
= syscon_node_to_regmap(anatop_np
);
201 of_node_put(anatop_np
);
202 if (IS_ERR(sreg
->anatop
))
203 return PTR_ERR(sreg
->anatop
);
205 ret
= of_property_read_u32(np
, "anatop-reg-offset",
208 dev_err(dev
, "no anatop-reg-offset property set\n");
211 ret
= of_property_read_u32(np
, "anatop-vol-bit-width",
212 &sreg
->vol_bit_width
);
214 dev_err(dev
, "no anatop-vol-bit-width property set\n");
217 ret
= of_property_read_u32(np
, "anatop-vol-bit-shift",
218 &sreg
->vol_bit_shift
);
220 dev_err(dev
, "no anatop-vol-bit-shift property set\n");
223 ret
= of_property_read_u32(np
, "anatop-min-bit-val",
226 dev_err(dev
, "no anatop-min-bit-val property set\n");
229 ret
= of_property_read_u32(np
, "anatop-min-voltage",
232 dev_err(dev
, "no anatop-min-voltage property set\n");
235 ret
= of_property_read_u32(np
, "anatop-max-voltage",
238 dev_err(dev
, "no anatop-max-voltage property set\n");
242 /* read LDO ramp up setting, only for core reg */
243 of_property_read_u32(np
, "anatop-delay-reg-offset",
245 of_property_read_u32(np
, "anatop-delay-bit-width",
246 &sreg
->delay_bit_width
);
247 of_property_read_u32(np
, "anatop-delay-bit-shift",
248 &sreg
->delay_bit_shift
);
250 rdesc
->n_voltages
= (sreg
->max_voltage
- sreg
->min_voltage
) / 25000 + 1
252 rdesc
->min_uV
= sreg
->min_voltage
;
253 rdesc
->uV_step
= 25000;
254 rdesc
->linear_min_sel
= sreg
->min_bit_val
;
255 rdesc
->vsel_reg
= sreg
->control_reg
;
256 rdesc
->vsel_mask
= ((1 << sreg
->vol_bit_width
) - 1) <<
258 rdesc
->min_dropout_uV
= 125000;
260 config
.dev
= &pdev
->dev
;
261 config
.init_data
= initdata
;
262 config
.driver_data
= sreg
;
263 config
.of_node
= pdev
->dev
.of_node
;
264 config
.regmap
= sreg
->anatop
;
266 /* Only core regulators have the ramp up delay configuration. */
267 if (sreg
->control_reg
&& sreg
->delay_bit_width
) {
268 rdesc
->ops
= &anatop_core_rops
;
270 ret
= regmap_read(config
.regmap
, rdesc
->vsel_reg
, &val
);
272 dev_err(dev
, "failed to read initial state\n");
276 sreg
->sel
= (val
& rdesc
->vsel_mask
) >> sreg
->vol_bit_shift
;
277 if (sreg
->sel
== LDO_FET_FULL_ON
) {
283 * In case vddpu was disabled by the bootloader, we need to set
284 * a sane default until imx6-cpufreq was probed and changes the
285 * voltage to the correct value. In this case we set 1.25V.
287 if (!sreg
->sel
&& !strcmp(rdesc
->name
, "vddpu"))
290 /* set the default voltage of the pcie phy to be 1.100v */
291 if (!sreg
->sel
&& !strcmp(rdesc
->name
, "vddpcie"))
294 if (!sreg
->bypass
&& !sreg
->sel
) {
295 dev_err(&pdev
->dev
, "Failed to read a valid default voltage selector.\n");
301 rdesc
->ops
= &anatop_rops
;
303 if (!of_property_read_u32(np
, "anatop-enable-bit",
305 anatop_rops
.enable
= regulator_enable_regmap
;
306 anatop_rops
.disable
= regulator_disable_regmap
;
307 anatop_rops
.is_enabled
= regulator_is_enabled_regmap
;
309 rdesc
->enable_reg
= sreg
->control_reg
;
310 rdesc
->enable_mask
= BIT(enable_bit
);
314 /* register regulator */
315 rdev
= devm_regulator_register(dev
, rdesc
, &config
);
317 dev_err(dev
, "failed to register %s\n",
319 return PTR_ERR(rdev
);
322 platform_set_drvdata(pdev
, rdev
);
327 static const struct of_device_id of_anatop_regulator_match_tbl
[] = {
328 { .compatible
= "fsl,anatop-regulator", },
331 MODULE_DEVICE_TABLE(of
, of_anatop_regulator_match_tbl
);
333 static struct platform_driver anatop_regulator_driver
= {
335 .name
= "anatop_regulator",
336 .of_match_table
= of_anatop_regulator_match_tbl
,
338 .probe
= anatop_regulator_probe
,
341 static int __init
anatop_regulator_init(void)
343 return platform_driver_register(&anatop_regulator_driver
);
345 postcore_initcall(anatop_regulator_init
);
347 static void __exit
anatop_regulator_exit(void)
349 platform_driver_unregister(&anatop_regulator_driver
);
351 module_exit(anatop_regulator_exit
);
353 MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>");
354 MODULE_AUTHOR("Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
355 MODULE_DESCRIPTION("ANATOP Regulator driver");
356 MODULE_LICENSE("GPL v2");
357 MODULE_ALIAS("platform:anatop_regulator");