blob: f9856d4e295f8f7ae45ed81131695f50e8f746e8 [file] [log] [blame]
Fabio Estevamc07bbfe2018-05-23 16:17:35 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +08004
5#include <linux/slab.h>
6#include <linux/device.h>
7#include <linux/module.h>
Dong Aishengbaa64152012-09-05 10:57:15 +08008#include <linux/mfd/syscon.h>
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +08009#include <linux/err.h>
10#include <linux/io.h>
11#include <linux/platform_device.h>
12#include <linux/of.h>
13#include <linux/of_address.h>
Dong Aishengbaa64152012-09-05 10:57:15 +080014#include <linux/regmap.h>
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080015#include <linux/regulator/driver.h>
16#include <linux/regulator/of_regulator.h>
Sascha Hauer0d192082015-10-13 12:45:30 +020017#include <linux/regulator/machine.h>
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080018
Anson Huang9ee417c2013-01-31 11:23:53 -050019#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 */
21
Philipp Zabel605ebd32014-02-11 14:43:44 +010022#define LDO_POWER_GATE 0x00
Philipp Zabeld38018f2014-02-11 14:43:45 +010023#define LDO_FET_FULL_ON 0x1f
Philipp Zabel605ebd32014-02-11 14:43:44 +010024
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080025struct anatop_regulator {
Anson Huang9ee417c2013-01-31 11:23:53 -050026 u32 delay_reg;
27 int delay_bit_shift;
28 int delay_bit_width;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080029 struct regulator_desc rdesc;
Philipp Zabeld38018f2014-02-11 14:43:45 +010030 bool bypass;
Philipp Zabel605ebd32014-02-11 14:43:44 +010031 int sel;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +080032};
33
Anson Huang9ee417c2013-01-31 11:23:53 -050034static int anatop_regmap_set_voltage_time_sel(struct regulator_dev *reg,
35 unsigned int old_sel,
36 unsigned int new_sel)
37{
38 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
39 u32 val;
40 int ret = 0;
41
42 /* check whether need to care about LDO ramp up speed */
43 if (anatop_reg->delay_bit_width && new_sel > old_sel) {
44 /*
45 * the delay for LDO ramp up time is
46 * based on the register setting, we need
47 * to calculate how many steps LDO need to
48 * ramp up, and how much delay needed. (us)
49 */
Axel Linf34a2692019-04-10 00:10:39 +080050 regmap_read(reg->regmap, anatop_reg->delay_reg, &val);
Anson Huang9ee417c2013-01-31 11:23:53 -050051 val = (val >> anatop_reg->delay_bit_shift) &
52 ((1 << anatop_reg->delay_bit_width) - 1);
Shawn Guoff1ce052013-02-04 10:21:32 +080053 ret = (new_sel - old_sel) * (LDO_RAMP_UP_UNIT_IN_CYCLES <<
54 val) / LDO_RAMP_UP_FREQ_IN_MHZ + 1;
Anson Huang9ee417c2013-01-31 11:23:53 -050055 }
56
57 return ret;
58}
59
Philipp Zabel605ebd32014-02-11 14:43:44 +010060static int anatop_regmap_enable(struct regulator_dev *reg)
61{
62 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
Philipp Zabeld38018f2014-02-11 14:43:45 +010063 int sel;
Philipp Zabel605ebd32014-02-11 14:43:44 +010064
Philipp Zabeld38018f2014-02-11 14:43:45 +010065 sel = anatop_reg->bypass ? LDO_FET_FULL_ON : anatop_reg->sel;
66 return regulator_set_voltage_sel_regmap(reg, sel);
Philipp Zabel605ebd32014-02-11 14:43:44 +010067}
68
69static int anatop_regmap_disable(struct regulator_dev *reg)
70{
71 return regulator_set_voltage_sel_regmap(reg, LDO_POWER_GATE);
72}
73
74static int anatop_regmap_is_enabled(struct regulator_dev *reg)
75{
76 return regulator_get_voltage_sel_regmap(reg) != LDO_POWER_GATE;
77}
78
79static int anatop_regmap_core_set_voltage_sel(struct regulator_dev *reg,
80 unsigned selector)
81{
82 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
83 int ret;
84
Philipp Zabeld38018f2014-02-11 14:43:45 +010085 if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg)) {
Philipp Zabel605ebd32014-02-11 14:43:44 +010086 anatop_reg->sel = selector;
87 return 0;
88 }
89
90 ret = regulator_set_voltage_sel_regmap(reg, selector);
91 if (!ret)
92 anatop_reg->sel = selector;
93 return ret;
94}
95
96static int anatop_regmap_core_get_voltage_sel(struct regulator_dev *reg)
97{
98 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
99
Philipp Zabeld38018f2014-02-11 14:43:45 +0100100 if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg))
Philipp Zabel605ebd32014-02-11 14:43:44 +0100101 return anatop_reg->sel;
102
103 return regulator_get_voltage_sel_regmap(reg);
104}
105
Philipp Zabeld38018f2014-02-11 14:43:45 +0100106static int anatop_regmap_get_bypass(struct regulator_dev *reg, bool *enable)
107{
108 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
109 int sel;
110
111 sel = regulator_get_voltage_sel_regmap(reg);
112 if (sel == LDO_FET_FULL_ON)
113 WARN_ON(!anatop_reg->bypass);
114 else if (sel != LDO_POWER_GATE)
115 WARN_ON(anatop_reg->bypass);
116
117 *enable = anatop_reg->bypass;
118 return 0;
119}
120
121static int anatop_regmap_set_bypass(struct regulator_dev *reg, bool enable)
122{
123 struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
124 int sel;
125
126 if (enable == anatop_reg->bypass)
127 return 0;
128
129 sel = enable ? LDO_FET_FULL_ON : anatop_reg->sel;
130 anatop_reg->bypass = enable;
131
132 return regulator_set_voltage_sel_regmap(reg, sel);
133}
134
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800135static struct regulator_ops anatop_rops = {
Axel Lin114c5742014-02-22 12:53:18 +0800136 .set_voltage_sel = regulator_set_voltage_sel_regmap,
137 .get_voltage_sel = regulator_get_voltage_sel_regmap,
Axel Lind01c3a1e2012-06-03 23:02:34 +0800138 .list_voltage = regulator_list_voltage_linear,
139 .map_voltage = regulator_map_voltage_linear,
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800140};
141
Rikard Falkeborncae62a92020-06-18 00:32:43 +0200142static const struct regulator_ops anatop_core_rops = {
Philipp Zabel605ebd32014-02-11 14:43:44 +0100143 .enable = anatop_regmap_enable,
144 .disable = anatop_regmap_disable,
145 .is_enabled = anatop_regmap_is_enabled,
146 .set_voltage_sel = anatop_regmap_core_set_voltage_sel,
147 .set_voltage_time_sel = anatop_regmap_set_voltage_time_sel,
148 .get_voltage_sel = anatop_regmap_core_get_voltage_sel,
149 .list_voltage = regulator_list_voltage_linear,
150 .map_voltage = regulator_map_voltage_linear,
Philipp Zabeld38018f2014-02-11 14:43:45 +0100151 .get_bypass = anatop_regmap_get_bypass,
152 .set_bypass = anatop_regmap_set_bypass,
Philipp Zabel605ebd32014-02-11 14:43:44 +0100153};
154
Bill Pembertona5023572012-11-19 13:22:22 -0500155static int anatop_regulator_probe(struct platform_device *pdev)
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800156{
157 struct device *dev = &pdev->dev;
158 struct device_node *np = dev->of_node;
Dong Aishengbaa64152012-09-05 10:57:15 +0800159 struct device_node *anatop_np;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800160 struct regulator_desc *rdesc;
161 struct regulator_dev *rdev;
162 struct anatop_regulator *sreg;
163 struct regulator_init_data *initdata;
Axel Lind914d812012-04-10 22:45:01 +0800164 struct regulator_config config = { };
Axel Linf34a2692019-04-10 00:10:39 +0800165 struct regmap *regmap;
166 u32 control_reg;
167 u32 vol_bit_shift;
168 u32 vol_bit_width;
169 u32 min_bit_val;
170 u32 min_voltage;
171 u32 max_voltage;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800172 int ret = 0;
Philipp Zabel605ebd32014-02-11 14:43:44 +0100173 u32 val;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800174
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800175 sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
176 if (!sreg)
177 return -ENOMEM;
Dong Aisheng5062e042017-04-12 09:58:44 +0800178
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800179 rdesc = &sreg->rdesc;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800180 rdesc->type = REGULATOR_VOLTAGE;
181 rdesc->owner = THIS_MODULE;
Dong Aishengbaa64152012-09-05 10:57:15 +0800182
Dong Aishengaeb14042017-04-12 09:58:45 +0800183 of_property_read_string(np, "regulator-name", &rdesc->name);
Dong Aisheng4af59242017-04-14 22:32:43 +0800184 if (!rdesc->name) {
185 dev_err(dev, "failed to get a regulator-name\n");
186 return -EINVAL;
187 }
Dong Aishengaeb14042017-04-12 09:58:45 +0800188
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100189 initdata = of_get_regulator_init_data(dev, np, rdesc);
Dong Aisheng7f51cf22017-04-12 09:58:42 +0800190 if (!initdata)
191 return -ENOMEM;
192
Sascha Hauer0d192082015-10-13 12:45:30 +0200193 initdata->supply_regulator = "vin";
Javier Martinez Canillas072e78b2014-11-10 14:43:53 +0100194
Dong Aishengbaa64152012-09-05 10:57:15 +0800195 anatop_np = of_get_parent(np);
196 if (!anatop_np)
197 return -ENODEV;
Axel Linf34a2692019-04-10 00:10:39 +0800198 regmap = syscon_node_to_regmap(anatop_np);
Dong Aishengbaa64152012-09-05 10:57:15 +0800199 of_node_put(anatop_np);
Axel Linf34a2692019-04-10 00:10:39 +0800200 if (IS_ERR(regmap))
201 return PTR_ERR(regmap);
Dong Aishengbaa64152012-09-05 10:57:15 +0800202
Axel Linf34a2692019-04-10 00:10:39 +0800203 ret = of_property_read_u32(np, "anatop-reg-offset", &control_reg);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800204 if (ret) {
Ying-Chun Liu (PaulLiu)2f2cc272012-03-27 15:54:01 +0800205 dev_err(dev, "no anatop-reg-offset property set\n");
Fabio Estevamf2b269b2014-01-06 10:13:15 -0200206 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800207 }
Axel Linf34a2692019-04-10 00:10:39 +0800208 ret = of_property_read_u32(np, "anatop-vol-bit-width", &vol_bit_width);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800209 if (ret) {
210 dev_err(dev, "no anatop-vol-bit-width property set\n");
Fabio Estevamf2b269b2014-01-06 10:13:15 -0200211 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800212 }
Axel Linf34a2692019-04-10 00:10:39 +0800213 ret = of_property_read_u32(np, "anatop-vol-bit-shift", &vol_bit_shift);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800214 if (ret) {
215 dev_err(dev, "no anatop-vol-bit-shift property set\n");
Fabio Estevamf2b269b2014-01-06 10:13:15 -0200216 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800217 }
Axel Linf34a2692019-04-10 00:10:39 +0800218 ret = of_property_read_u32(np, "anatop-min-bit-val", &min_bit_val);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800219 if (ret) {
220 dev_err(dev, "no anatop-min-bit-val property set\n");
Fabio Estevamf2b269b2014-01-06 10:13:15 -0200221 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800222 }
Axel Linf34a2692019-04-10 00:10:39 +0800223 ret = of_property_read_u32(np, "anatop-min-voltage", &min_voltage);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800224 if (ret) {
225 dev_err(dev, "no anatop-min-voltage property set\n");
Fabio Estevamf2b269b2014-01-06 10:13:15 -0200226 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800227 }
Axel Linf34a2692019-04-10 00:10:39 +0800228 ret = of_property_read_u32(np, "anatop-max-voltage", &max_voltage);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800229 if (ret) {
230 dev_err(dev, "no anatop-max-voltage property set\n");
Fabio Estevamf2b269b2014-01-06 10:13:15 -0200231 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800232 }
233
Anson Huang9ee417c2013-01-31 11:23:53 -0500234 /* read LDO ramp up setting, only for core reg */
235 of_property_read_u32(np, "anatop-delay-reg-offset",
236 &sreg->delay_reg);
237 of_property_read_u32(np, "anatop-delay-bit-width",
238 &sreg->delay_bit_width);
239 of_property_read_u32(np, "anatop-delay-bit-shift",
240 &sreg->delay_bit_shift);
241
Axel Linf34a2692019-04-10 00:10:39 +0800242 rdesc->n_voltages = (max_voltage - min_voltage) / 25000 + 1
243 + min_bit_val;
244 rdesc->min_uV = min_voltage;
Axel Lin0713e6a2012-05-14 11:06:44 +0800245 rdesc->uV_step = 25000;
Axel Linf34a2692019-04-10 00:10:39 +0800246 rdesc->linear_min_sel = min_bit_val;
247 rdesc->vsel_reg = control_reg;
248 rdesc->vsel_mask = ((1 << vol_bit_width) - 1) << vol_bit_shift;
Sascha Hauer0d192082015-10-13 12:45:30 +0200249 rdesc->min_dropout_uV = 125000;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800250
Axel Lind914d812012-04-10 22:45:01 +0800251 config.dev = &pdev->dev;
252 config.init_data = initdata;
253 config.driver_data = sreg;
254 config.of_node = pdev->dev.of_node;
Axel Linf34a2692019-04-10 00:10:39 +0800255 config.regmap = regmap;
Axel Lind914d812012-04-10 22:45:01 +0800256
Philipp Zabel605ebd32014-02-11 14:43:44 +0100257 /* Only core regulators have the ramp up delay configuration. */
Axel Linf34a2692019-04-10 00:10:39 +0800258 if (control_reg && sreg->delay_bit_width) {
Philipp Zabel605ebd32014-02-11 14:43:44 +0100259 rdesc->ops = &anatop_core_rops;
260
261 ret = regmap_read(config.regmap, rdesc->vsel_reg, &val);
262 if (ret) {
263 dev_err(dev, "failed to read initial state\n");
264 return ret;
265 }
266
Axel Linf34a2692019-04-10 00:10:39 +0800267 sreg->sel = (val & rdesc->vsel_mask) >> vol_bit_shift;
Philipp Zabeld38018f2014-02-11 14:43:45 +0100268 if (sreg->sel == LDO_FET_FULL_ON) {
269 sreg->sel = 0;
270 sreg->bypass = true;
271 }
Markus Pargmannfe08be32014-10-06 21:33:36 +0200272
273 /*
274 * In case vddpu was disabled by the bootloader, we need to set
275 * a sane default until imx6-cpufreq was probed and changes the
276 * voltage to the correct value. In this case we set 1.25V.
277 */
Dong Aishengaeb14042017-04-12 09:58:45 +0800278 if (!sreg->sel && !strcmp(rdesc->name, "vddpu"))
Markus Pargmannfe08be32014-10-06 21:33:36 +0200279 sreg->sel = 22;
Markus Pargmannda0607c2014-10-06 21:33:37 +0200280
Dong Aisheng9bf94452017-04-12 09:58:47 +0800281 /* set the default voltage of the pcie phy to be 1.100v */
Dong Aisheng4af59242017-04-14 22:32:43 +0800282 if (!sreg->sel && !strcmp(rdesc->name, "vddpcie"))
Dong Aisheng9bf94452017-04-12 09:58:47 +0800283 sreg->sel = 0x10;
284
Mika Båtsman8a092e62016-06-17 13:31:37 +0300285 if (!sreg->bypass && !sreg->sel) {
Markus Pargmannda0607c2014-10-06 21:33:37 +0200286 dev_err(&pdev->dev, "Failed to read a valid default voltage selector.\n");
287 return -EINVAL;
288 }
Philipp Zabel605ebd32014-02-11 14:43:44 +0100289 } else {
Andrey Smirnovca7734a2017-01-10 08:30:14 -0800290 u32 enable_bit;
291
Philipp Zabel605ebd32014-02-11 14:43:44 +0100292 rdesc->ops = &anatop_rops;
Andrey Smirnovca7734a2017-01-10 08:30:14 -0800293
294 if (!of_property_read_u32(np, "anatop-enable-bit",
295 &enable_bit)) {
296 anatop_rops.enable = regulator_enable_regmap;
297 anatop_rops.disable = regulator_disable_regmap;
298 anatop_rops.is_enabled = regulator_is_enabled_regmap;
299
Axel Linf34a2692019-04-10 00:10:39 +0800300 rdesc->enable_reg = control_reg;
Andrey Smirnovca7734a2017-01-10 08:30:14 -0800301 rdesc->enable_mask = BIT(enable_bit);
302 }
Philipp Zabel605ebd32014-02-11 14:43:44 +0100303 }
304
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800305 /* register regulator */
Sachin Kamatbe1221e2013-09-04 12:00:57 +0530306 rdev = devm_regulator_register(dev, rdesc, &config);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800307 if (IS_ERR(rdev)) {
Anson Huang788bfc62020-03-03 21:44:12 +0800308 ret = PTR_ERR(rdev);
309 if (ret == -EPROBE_DEFER)
310 dev_dbg(dev, "failed to register %s, deferring...\n",
311 rdesc->name);
312 else
313 dev_err(dev, "failed to register %s\n", rdesc->name);
314 return ret;
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800315 }
316
317 platform_set_drvdata(pdev, rdev);
318
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800319 return 0;
320}
321
Jingoo Hana799baa2014-05-07 16:55:10 +0900322static const struct of_device_id of_anatop_regulator_match_tbl[] = {
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800323 { .compatible = "fsl,anatop-regulator", },
324 { /* end */ }
325};
Luis de Bethencourtd702ffd2015-09-18 19:09:07 +0200326MODULE_DEVICE_TABLE(of, of_anatop_regulator_match_tbl);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800327
Shawn Guoc0d78c22012-04-02 14:57:01 +0800328static struct platform_driver anatop_regulator_driver = {
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800329 .driver = {
330 .name = "anatop_regulator",
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800331 .of_match_table = of_anatop_regulator_match_tbl,
332 },
333 .probe = anatop_regulator_probe,
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800334};
335
336static int __init anatop_regulator_init(void)
337{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800338 return platform_driver_register(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800339}
340postcore_initcall(anatop_regulator_init);
341
342static void __exit anatop_regulator_exit(void)
343{
Shawn Guoc0d78c22012-04-02 14:57:01 +0800344 platform_driver_unregister(&anatop_regulator_driver);
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800345}
346module_exit(anatop_regulator_exit);
347
Jingoo Han34f75682013-10-14 17:45:51 +0900348MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>");
349MODULE_AUTHOR("Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
Ying-Chun Liu (PaulLiu)e3e5aff2012-03-14 10:29:12 +0800350MODULE_DESCRIPTION("ANATOP Regulator driver");
351MODULE_LICENSE("GPL v2");
Fabio Estevam89705b92013-12-31 10:56:00 -0200352MODULE_ALIAS("platform:anatop_regulator");