1 --- a/drivers/regulator/Kconfig
2 +++ b/drivers/regulator/Kconfig
3 @@ -462,6 +462,14 @@ config REGULATOR_MT6397
4 This driver supports the control of different power rails of device
5 through regulator interface.
8 + tristate "Freescale MXS on-chip regulators"
9 + depends on (MXS_POWER || COMPILE_TEST)
11 + Say y here to support Freescale MXS on-chip regulators.
12 + It is recommended that this option be enabled on i.MX23,
15 config REGULATOR_PALMAS
16 tristate "TI Palmas PMIC Regulators"
18 --- a/drivers/regulator/Makefile
19 +++ b/drivers/regulator/Makefile
20 @@ -61,6 +61,7 @@ obj-$(CONFIG_REGULATOR_MC13892) += mc138
21 obj-$(CONFIG_REGULATOR_MC13XXX_CORE) += mc13xxx-regulator-core.o
22 obj-$(CONFIG_REGULATOR_MT6311) += mt6311-regulator.o
23 obj-$(CONFIG_REGULATOR_MT6397) += mt6397-regulator.o
24 +obj-$(CONFIG_REGULATOR_MXS) += mxs-regulator.o
25 obj-$(CONFIG_REGULATOR_QCOM_RPM) += qcom_rpm-regulator.o
26 obj-$(CONFIG_REGULATOR_QCOM_SMD_RPM) += qcom_smd-regulator.o
27 obj-$(CONFIG_REGULATOR_QCOM_SPMI) += qcom_spmi-regulator.o
29 +++ b/drivers/regulator/mxs-regulator.c
32 + * Freescale MXS on-chip regulators
34 + * Embedded Alley Solutions, Inc <source@embeddedalley.com>
36 + * Copyright (C) 2014 Stefan Wahren
37 + * Copyright (C) 2010 Freescale Semiconductor, Inc.
38 + * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved.
40 + * Inspired by imx-bootlets
44 + * The code contained herein is licensed under the GNU General Public
45 + * License. You may obtain a copy of the GNU General Public License
46 + * Version 2 or later at the following locations:
48 + * http://www.opensource.org/licenses/gpl-license.html
49 + * http://www.gnu.org/copyleft/gpl.html
52 +#include <linux/delay.h>
53 +#include <linux/device.h>
54 +#include <linux/err.h>
55 +#include <linux/kernel.h>
56 +#include <linux/mfd/syscon.h>
57 +#include <linux/module.h>
58 +#include <linux/of.h>
59 +#include <linux/of_device.h>
60 +#include <linux/platform_device.h>
61 +#include <linux/regmap.h>
62 +#include <linux/regulator/driver.h>
63 +#include <linux/regulator/machine.h>
64 +#include <linux/regulator/of_regulator.h>
65 +#include <linux/slab.h>
67 +/* Powered by linear regulator. DCDC output is gated off and
68 + the linreg output is equal to the target. */
69 +#define HW_POWER_LINREG_DCDC_OFF 1
71 +/* Powered by linear regulator. DCDC output is not gated off
72 + and is ready for the automatic hardware transistion after a 5V
73 + event. The converters are not enabled when 5V is present. LinReg output
74 + is 25mV below target. */
75 +#define HW_POWER_LINREG_DCDC_READY 2
77 +/* Powered by DCDC converter and the LinReg is on. LinReg output
78 + is 25mV below target. */
79 +#define HW_POWER_DCDC_LINREG_ON 3
81 +/* Powered by DCDC converter and the LinReg is off. LinReg output
82 + is 25mV below target. */
83 +#define HW_POWER_DCDC_LINREG_OFF 4
85 +/* Powered by DCDC converter and the LinReg is ready for the
86 + automatic hardware transfer. The LinReg output is not enabled and
87 + depends on the 5V presence to enable the LinRegs. LinReg offset is 25mV
89 +#define HW_POWER_DCDC_LINREG_READY 5
91 +/* Powered by an external source when 5V is present. This does not
92 + necessarily mean the external source is powered by 5V,but the chip needs
93 + to be aware that 5V is present. */
94 +#define HW_POWER_EXTERNAL_SOURCE_5V 6
96 +/* Powered by an external source when 5V is not present.This doesn't
97 + necessarily mean the external source is powered by the battery, but the
98 + chip needs to be aware that the battery is present */
99 +#define HW_POWER_EXTERNAL_SOURCE_BATTERY 7
101 +/* Unknown configuration. This is an error. */
102 +#define HW_POWER_UNKNOWN_SOURCE 8
104 +/* TODO: Move power register offsets into header file */
105 +#define HW_POWER_5VCTRL 0x00000010
106 +#define HW_POWER_VDDDCTRL 0x00000040
107 +#define HW_POWER_VDDACTRL 0x00000050
108 +#define HW_POWER_VDDIOCTRL 0x00000060
109 +#define HW_POWER_MISC 0x00000090
110 +#define HW_POWER_STS 0x000000c0
112 +#define BM_POWER_STS_VBUSVALID0_STATUS BIT(15)
113 +#define BM_POWER_STS_DC_OK BIT(9)
115 +#define BM_POWER_5VCTRL_ILIMIT_EQ_ZERO BIT(2)
116 +#define BM_POWER_5VCTRL_ENABLE_DCDC BIT(0)
118 +#define BM_POWER_LINREG_OFFSET_DCDC_MODE BIT(1)
120 +#define SHIFT_FREQSEL 4
122 +#define BM_POWER_MISC_FREQSEL (7 << SHIFT_FREQSEL)
124 +/* Recommended DC-DC clock source values */
125 +#define HW_POWER_MISC_FREQSEL_20000_KHZ 1
126 +#define HW_POWER_MISC_FREQSEL_24000_KHZ 2
127 +#define HW_POWER_MISC_FREQSEL_19200_KHZ 3
129 +#define HW_POWER_MISC_SEL_PLLCLK BIT(0)
137 +struct mxs_reg_info {
138 + /* regulator descriptor */
139 + struct regulator_desc desc;
141 + /* regulator control register */
144 + /* disable DC-DC output */
145 + unsigned int disable_fet_mask;
147 + /* steps between linreg output and DC-DC target */
148 + unsigned int linreg_offset_mask;
149 + u8 linreg_offset_shift;
151 + /* function which determine power source */
152 + u8 (*get_power_source)(struct regulator_dev *);
155 +static inline u8 get_linreg_offset(struct mxs_reg_info *ldo, u32 regs)
157 + return (regs & ldo->linreg_offset_mask) >> ldo->linreg_offset_shift;
160 +static u8 get_vddio_power_source(struct regulator_dev *reg)
162 + struct mxs_reg_info *ldo = rdev_get_drvdata(reg);
163 + u32 v5ctrl, status, base;
166 + if (regmap_read(reg->regmap, HW_POWER_5VCTRL, &v5ctrl))
167 + return HW_POWER_UNKNOWN_SOURCE;
169 + if (regmap_read(reg->regmap, HW_POWER_STS, &status))
170 + return HW_POWER_UNKNOWN_SOURCE;
172 + if (regmap_read(reg->regmap, ldo->ctrl_reg, &base))
173 + return HW_POWER_UNKNOWN_SOURCE;
175 + offset = get_linreg_offset(ldo, base);
177 + /* If VBUS valid then 5 V power supply present */
178 + if (status & BM_POWER_STS_VBUSVALID0_STATUS) {
179 + /* Powered by Linreg, DC-DC is off */
180 + if ((base & ldo->disable_fet_mask) &&
181 + !(offset & BM_POWER_LINREG_OFFSET_DCDC_MODE)) {
182 + return HW_POWER_LINREG_DCDC_OFF;
185 + if (v5ctrl & BM_POWER_5VCTRL_ENABLE_DCDC) {
186 + /* Powered by DC-DC, Linreg is on */
187 + if (offset & BM_POWER_LINREG_OFFSET_DCDC_MODE)
188 + return HW_POWER_DCDC_LINREG_ON;
190 + /* Powered by Linreg, DC-DC is off */
191 + if (!(offset & BM_POWER_LINREG_OFFSET_DCDC_MODE))
192 + return HW_POWER_LINREG_DCDC_OFF;
195 + /* Powered by DC-DC, Linreg is on */
196 + if (offset & BM_POWER_LINREG_OFFSET_DCDC_MODE)
197 + return HW_POWER_DCDC_LINREG_ON;
200 + return HW_POWER_UNKNOWN_SOURCE;
203 +static u8 get_vdda_vddd_power_source(struct regulator_dev *reg)
205 + struct mxs_reg_info *ldo = rdev_get_drvdata(reg);
206 + struct regulator_desc *desc = &ldo->desc;
207 + u32 v5ctrl, status, base;
210 + if (regmap_read(reg->regmap, HW_POWER_5VCTRL, &v5ctrl))
211 + return HW_POWER_UNKNOWN_SOURCE;
213 + if (regmap_read(reg->regmap, HW_POWER_STS, &status))
214 + return HW_POWER_UNKNOWN_SOURCE;
216 + if (regmap_read(reg->regmap, ldo->ctrl_reg, &base))
217 + return HW_POWER_UNKNOWN_SOURCE;
219 + offset = get_linreg_offset(ldo, base);
221 + /* DC-DC output is disabled */
222 + if (base & ldo->disable_fet_mask) {
223 + /* Powered by 5 V supply */
224 + if (status & BM_POWER_STS_VBUSVALID0_STATUS)
225 + return HW_POWER_EXTERNAL_SOURCE_5V;
227 + /* Powered by Linreg, DC-DC is off */
228 + if (!(offset & BM_POWER_LINREG_OFFSET_DCDC_MODE))
229 + return HW_POWER_LINREG_DCDC_OFF;
232 + /* If VBUS valid then 5 V power supply present */
233 + if (status & BM_POWER_STS_VBUSVALID0_STATUS) {
234 + /* Powered by DC-DC, Linreg is on */
235 + if (v5ctrl & BM_POWER_5VCTRL_ENABLE_DCDC)
236 + return HW_POWER_DCDC_LINREG_ON;
238 + /* Powered by Linreg, DC-DC is off */
239 + return HW_POWER_LINREG_DCDC_OFF;
243 + if (offset & BM_POWER_LINREG_OFFSET_DCDC_MODE) {
244 + /* Powered by DC-DC, Linreg is on */
245 + if (base & desc->enable_mask)
246 + return HW_POWER_DCDC_LINREG_ON;
248 + /* Powered by DC-DC, Linreg is off */
249 + return HW_POWER_DCDC_LINREG_OFF;
252 + return HW_POWER_UNKNOWN_SOURCE;
255 +static int mxs_set_dcdc_freq(struct regulator_dev *reg, u32 hz)
257 + struct mxs_reg_info *dcdc = rdev_get_drvdata(reg);
261 + if (dcdc->desc.id != MXS_DCDC) {
262 + dev_warn(®->dev, "Setting switching freq is not supported\n");
266 + ret = regmap_read(reg->regmap, HW_POWER_MISC, &val);
270 + val &= ~BM_POWER_MISC_FREQSEL;
271 + val &= ~HW_POWER_MISC_SEL_PLLCLK;
274 + * Select the PLL/PFD based frequency that the DC-DC converter uses.
275 + * The actual switching frequency driving the power inductor is
276 + * DCDC_CLK/16. Accept only values recommend by Freescale.
280 + val |= HW_POWER_MISC_FREQSEL_19200_KHZ << SHIFT_FREQSEL;
283 + val |= HW_POWER_MISC_FREQSEL_20000_KHZ << SHIFT_FREQSEL;
286 + val |= HW_POWER_MISC_FREQSEL_24000_KHZ << SHIFT_FREQSEL;
289 + dev_warn(®->dev, "Switching freq: %u Hz not supported\n",
294 + /* First program FREQSEL */
295 + ret = regmap_write(reg->regmap, HW_POWER_MISC, val);
299 + /* then set PLL as clock for DC-DC converter */
300 + val |= HW_POWER_MISC_SEL_PLLCLK;
302 + return regmap_write(reg->regmap, HW_POWER_MISC, val);
305 +static int mxs_ldo_set_voltage_sel(struct regulator_dev *reg, unsigned sel)
307 + struct mxs_reg_info *ldo = rdev_get_drvdata(reg);
308 + struct regulator_desc *desc = &ldo->desc;
313 + ret = regmap_update_bits(reg->regmap, desc->vsel_reg, desc->vsel_mask,
318 + if (ldo->get_power_source) {
319 + switch (ldo->get_power_source(reg)) {
320 + case HW_POWER_LINREG_DCDC_OFF:
321 + case HW_POWER_LINREG_DCDC_READY:
322 + case HW_POWER_EXTERNAL_SOURCE_5V:
324 + * Since the DC-DC converter is off we can't
325 + * trigger on DC_OK. So wait at least 1 ms
326 + * for stabilization.
328 + usleep_range(1000, 2000);
333 + /* Make sure DC_OK has changed */
334 + usleep_range(15, 20);
336 + for (timeout = 0; timeout < 20; timeout++) {
337 + ret = regmap_read(reg->regmap, HW_POWER_STS, &status);
342 + /* DC-DC converter control loop has stabilized */
343 + if (status & BM_POWER_STS_DC_OK)
350 + dev_warn_ratelimited(®->dev, "%s: timeout status=0x%08x\n",
358 +static int mxs_ldo_is_enabled(struct regulator_dev *reg)
360 + struct mxs_reg_info *ldo = rdev_get_drvdata(reg);
362 + if (ldo->get_power_source) {
363 + switch (ldo->get_power_source(reg)) {
364 + case HW_POWER_LINREG_DCDC_OFF:
365 + case HW_POWER_LINREG_DCDC_READY:
366 + case HW_POWER_DCDC_LINREG_ON:
374 +static struct regulator_ops mxs_dcdc_ops = {
375 + .is_enabled = regulator_is_enabled_regmap,
378 +static struct regulator_ops mxs_ldo_ops = {
379 + .list_voltage = regulator_list_voltage_linear,
380 + .map_voltage = regulator_map_voltage_linear,
381 + .set_voltage_sel = mxs_ldo_set_voltage_sel,
382 + .get_voltage_sel = regulator_get_voltage_sel_regmap,
383 + .is_enabled = mxs_ldo_is_enabled,
386 +static const struct mxs_reg_info mxs_info_dcdc = {
390 + .type = REGULATOR_VOLTAGE,
391 + .owner = THIS_MODULE,
392 + .ops = &mxs_dcdc_ops,
393 + .enable_reg = HW_POWER_STS,
394 + .enable_mask = (1 << 0),
398 +static const struct mxs_reg_info imx23_info_vddio = {
402 + .type = REGULATOR_VOLTAGE,
403 + .owner = THIS_MODULE,
404 + .n_voltages = 0x20,
406 + .linear_min_sel = 0,
408 + .vsel_reg = HW_POWER_VDDIOCTRL,
410 + .ops = &mxs_ldo_ops,
412 + .ctrl_reg = HW_POWER_VDDIOCTRL,
413 + .disable_fet_mask = 1 << 16,
414 + .linreg_offset_mask = 3 << 12,
415 + .linreg_offset_shift = 12,
416 + .get_power_source = get_vddio_power_source,
419 +static const struct mxs_reg_info imx28_info_vddio = {
423 + .type = REGULATOR_VOLTAGE,
424 + .owner = THIS_MODULE,
425 + .n_voltages = 0x11,
427 + .linear_min_sel = 0,
429 + .vsel_reg = HW_POWER_VDDIOCTRL,
431 + .ops = &mxs_ldo_ops,
433 + .ctrl_reg = HW_POWER_VDDIOCTRL,
434 + .disable_fet_mask = 1 << 16,
435 + .linreg_offset_mask = 3 << 12,
436 + .linreg_offset_shift = 12,
437 + .get_power_source = get_vddio_power_source,
440 +static const struct mxs_reg_info mxs_info_vdda = {
444 + .type = REGULATOR_VOLTAGE,
445 + .owner = THIS_MODULE,
446 + .n_voltages = 0x20,
448 + .linear_min_sel = 0,
450 + .vsel_reg = HW_POWER_VDDACTRL,
452 + .ops = &mxs_ldo_ops,
453 + .enable_mask = (1 << 17),
455 + .ctrl_reg = HW_POWER_VDDACTRL,
456 + .disable_fet_mask = 1 << 16,
457 + .linreg_offset_mask = 3 << 12,
458 + .linreg_offset_shift = 12,
459 + .get_power_source = get_vdda_vddd_power_source,
462 +static const struct mxs_reg_info mxs_info_vddd = {
466 + .type = REGULATOR_VOLTAGE,
467 + .owner = THIS_MODULE,
468 + .n_voltages = 0x20,
470 + .linear_min_sel = 0,
472 + .vsel_reg = HW_POWER_VDDDCTRL,
474 + .ops = &mxs_ldo_ops,
475 + .enable_mask = (1 << 21),
477 + .ctrl_reg = HW_POWER_VDDDCTRL,
478 + .disable_fet_mask = 1 << 20,
479 + .linreg_offset_mask = 3 << 16,
480 + .linreg_offset_shift = 16,
481 + .get_power_source = get_vdda_vddd_power_source,
484 +static const struct of_device_id of_mxs_regulator_match[] = {
485 + { .compatible = "fsl,imx23-dcdc", .data = &mxs_info_dcdc },
486 + { .compatible = "fsl,imx28-dcdc", .data = &mxs_info_dcdc },
487 + { .compatible = "fsl,imx23-vddio", .data = &imx23_info_vddio },
488 + { .compatible = "fsl,imx23-vdda", .data = &mxs_info_vdda },
489 + { .compatible = "fsl,imx23-vddd", .data = &mxs_info_vddd },
490 + { .compatible = "fsl,imx28-vddio", .data = &imx28_info_vddio },
491 + { .compatible = "fsl,imx28-vdda", .data = &mxs_info_vdda },
492 + { .compatible = "fsl,imx28-vddd", .data = &mxs_info_vddd },
495 +MODULE_DEVICE_TABLE(of, of_mxs_regulator_match);
497 +static int mxs_regulator_probe(struct platform_device *pdev)
499 + struct device *dev = &pdev->dev;
500 + const struct of_device_id *match;
501 + struct device_node *parent_np;
502 + struct regulator_dev *rdev = NULL;
503 + struct mxs_reg_info *info;
504 + struct regulator_init_data *initdata;
505 + struct regulator_config config = { };
508 + match = of_match_device(of_mxs_regulator_match, dev);
510 + /* We do not expect this to happen */
511 + dev_err(dev, "%s: Unable to match device\n", __func__);
515 + info = devm_kmemdup(dev, match->data, sizeof(struct mxs_reg_info),
520 + initdata = of_get_regulator_init_data(dev, dev->of_node, &info->desc);
522 + dev_err(dev, "missing regulator init data\n");
526 + parent_np = of_get_parent(dev->of_node);
529 + config.regmap = syscon_node_to_regmap(parent_np);
530 + of_node_put(parent_np);
531 + if (IS_ERR(config.regmap))
532 + return PTR_ERR(config.regmap);
535 + config.init_data = initdata;
536 + config.driver_data = info;
537 + config.of_node = dev->of_node;
539 + rdev = devm_regulator_register(dev, &info->desc, &config);
540 + if (IS_ERR(rdev)) {
541 + int ret = PTR_ERR(rdev);
543 + dev_err(dev, "%s: failed to register regulator(%d)\n",
548 + if (!of_property_read_u32(dev->of_node, "switching-frequency",
550 + mxs_set_dcdc_freq(rdev, switch_freq);
552 + platform_set_drvdata(pdev, rdev);
557 +static struct platform_driver mxs_regulator_driver = {
559 + .name = "mxs_regulator",
560 + .of_match_table = of_mxs_regulator_match,
562 + .probe = mxs_regulator_probe,
565 +module_platform_driver(mxs_regulator_driver);
567 +MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
568 +MODULE_DESCRIPTION("Freescale MXS regulators");
569 +MODULE_LICENSE("GPL v2");
570 +MODULE_ALIAS("platform:mxs_regulator");