2018-05-24 03:17:35 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
|
|
|
//
|
|
|
|
// Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
|
2012-03-14 10:29:12 +08:00
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/module.h>
|
2012-09-05 10:57:15 +08:00
|
|
|
#include <linux/mfd/syscon.h>
|
2012-03-14 10:29:12 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_address.h>
|
2012-09-05 10:57:15 +08:00
|
|
|
#include <linux/regmap.h>
|
2012-03-14 10:29:12 +08:00
|
|
|
#include <linux/regulator/driver.h>
|
|
|
|
#include <linux/regulator/of_regulator.h>
|
2015-10-13 18:45:30 +08:00
|
|
|
#include <linux/regulator/machine.h>
|
2012-03-14 10:29:12 +08:00
|
|
|
|
2013-02-01 00:23:53 +08:00
|
|
|
#define LDO_RAMP_UP_UNIT_IN_CYCLES 64 /* 64 cycles per step */
|
|
|
|
#define LDO_RAMP_UP_FREQ_IN_MHZ 24 /* cycle based on 24M OSC */
|
|
|
|
|
2014-02-11 21:43:44 +08:00
|
|
|
#define LDO_POWER_GATE 0x00
|
2014-02-11 21:43:45 +08:00
|
|
|
#define LDO_FET_FULL_ON 0x1f
|
2014-02-11 21:43:44 +08:00
|
|
|
|
2012-03-14 10:29:12 +08:00
|
|
|
struct anatop_regulator {
|
2013-02-01 00:23:53 +08:00
|
|
|
u32 delay_reg;
|
|
|
|
int delay_bit_shift;
|
|
|
|
int delay_bit_width;
|
2012-03-14 10:29:12 +08:00
|
|
|
struct regulator_desc rdesc;
|
2014-02-11 21:43:45 +08:00
|
|
|
bool bypass;
|
2014-02-11 21:43:44 +08:00
|
|
|
int sel;
|
2012-03-14 10:29:12 +08:00
|
|
|
};
|
|
|
|
|
2013-02-01 00:23:53 +08:00
|
|
|
static int anatop_regmap_set_voltage_time_sel(struct regulator_dev *reg,
|
|
|
|
unsigned int old_sel,
|
|
|
|
unsigned int new_sel)
|
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
u32 val;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* check whether need to care about LDO ramp up speed */
|
|
|
|
if (anatop_reg->delay_bit_width && new_sel > old_sel) {
|
|
|
|
/*
|
|
|
|
* the delay for LDO ramp up time is
|
|
|
|
* based on the register setting, we need
|
|
|
|
* to calculate how many steps LDO need to
|
|
|
|
* ramp up, and how much delay needed. (us)
|
|
|
|
*/
|
2019-04-10 00:10:39 +08:00
|
|
|
regmap_read(reg->regmap, anatop_reg->delay_reg, &val);
|
2013-02-01 00:23:53 +08:00
|
|
|
val = (val >> anatop_reg->delay_bit_shift) &
|
|
|
|
((1 << anatop_reg->delay_bit_width) - 1);
|
2013-02-04 10:21:32 +08:00
|
|
|
ret = (new_sel - old_sel) * (LDO_RAMP_UP_UNIT_IN_CYCLES <<
|
|
|
|
val) / LDO_RAMP_UP_FREQ_IN_MHZ + 1;
|
2013-02-01 00:23:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:43:44 +08:00
|
|
|
static int anatop_regmap_enable(struct regulator_dev *reg)
|
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
2014-02-11 21:43:45 +08:00
|
|
|
int sel;
|
2014-02-11 21:43:44 +08:00
|
|
|
|
2014-02-11 21:43:45 +08:00
|
|
|
sel = anatop_reg->bypass ? LDO_FET_FULL_ON : anatop_reg->sel;
|
|
|
|
return regulator_set_voltage_sel_regmap(reg, sel);
|
2014-02-11 21:43:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int anatop_regmap_disable(struct regulator_dev *reg)
|
|
|
|
{
|
|
|
|
return regulator_set_voltage_sel_regmap(reg, LDO_POWER_GATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anatop_regmap_is_enabled(struct regulator_dev *reg)
|
|
|
|
{
|
|
|
|
return regulator_get_voltage_sel_regmap(reg) != LDO_POWER_GATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anatop_regmap_core_set_voltage_sel(struct regulator_dev *reg,
|
|
|
|
unsigned selector)
|
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
int ret;
|
|
|
|
|
2014-02-11 21:43:45 +08:00
|
|
|
if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg)) {
|
2014-02-11 21:43:44 +08:00
|
|
|
anatop_reg->sel = selector;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = regulator_set_voltage_sel_regmap(reg, selector);
|
|
|
|
if (!ret)
|
|
|
|
anatop_reg->sel = selector;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anatop_regmap_core_get_voltage_sel(struct regulator_dev *reg)
|
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
|
2014-02-11 21:43:45 +08:00
|
|
|
if (anatop_reg->bypass || !anatop_regmap_is_enabled(reg))
|
2014-02-11 21:43:44 +08:00
|
|
|
return anatop_reg->sel;
|
|
|
|
|
|
|
|
return regulator_get_voltage_sel_regmap(reg);
|
|
|
|
}
|
|
|
|
|
2014-02-11 21:43:45 +08:00
|
|
|
static int anatop_regmap_get_bypass(struct regulator_dev *reg, bool *enable)
|
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
int sel;
|
|
|
|
|
|
|
|
sel = regulator_get_voltage_sel_regmap(reg);
|
|
|
|
if (sel == LDO_FET_FULL_ON)
|
|
|
|
WARN_ON(!anatop_reg->bypass);
|
|
|
|
else if (sel != LDO_POWER_GATE)
|
|
|
|
WARN_ON(anatop_reg->bypass);
|
|
|
|
|
|
|
|
*enable = anatop_reg->bypass;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int anatop_regmap_set_bypass(struct regulator_dev *reg, bool enable)
|
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
int sel;
|
|
|
|
|
|
|
|
if (enable == anatop_reg->bypass)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
sel = enable ? LDO_FET_FULL_ON : anatop_reg->sel;
|
|
|
|
anatop_reg->bypass = enable;
|
|
|
|
|
|
|
|
return regulator_set_voltage_sel_regmap(reg, sel);
|
|
|
|
}
|
|
|
|
|
2012-03-14 10:29:12 +08:00
|
|
|
static struct regulator_ops anatop_rops = {
|
2014-02-22 12:53:18 +08:00
|
|
|
.set_voltage_sel = regulator_set_voltage_sel_regmap,
|
|
|
|
.get_voltage_sel = regulator_get_voltage_sel_regmap,
|
2012-06-03 23:02:34 +08:00
|
|
|
.list_voltage = regulator_list_voltage_linear,
|
|
|
|
.map_voltage = regulator_map_voltage_linear,
|
2012-03-14 10:29:12 +08:00
|
|
|
};
|
|
|
|
|
2014-02-11 21:43:44 +08:00
|
|
|
static struct regulator_ops anatop_core_rops = {
|
|
|
|
.enable = anatop_regmap_enable,
|
|
|
|
.disable = anatop_regmap_disable,
|
|
|
|
.is_enabled = anatop_regmap_is_enabled,
|
|
|
|
.set_voltage_sel = anatop_regmap_core_set_voltage_sel,
|
|
|
|
.set_voltage_time_sel = anatop_regmap_set_voltage_time_sel,
|
|
|
|
.get_voltage_sel = anatop_regmap_core_get_voltage_sel,
|
|
|
|
.list_voltage = regulator_list_voltage_linear,
|
|
|
|
.map_voltage = regulator_map_voltage_linear,
|
2014-02-11 21:43:45 +08:00
|
|
|
.get_bypass = anatop_regmap_get_bypass,
|
|
|
|
.set_bypass = anatop_regmap_set_bypass,
|
2014-02-11 21:43:44 +08:00
|
|
|
};
|
|
|
|
|
2012-11-20 02:22:22 +08:00
|
|
|
static int anatop_regulator_probe(struct platform_device *pdev)
|
2012-03-14 10:29:12 +08:00
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct device_node *np = dev->of_node;
|
2012-09-05 10:57:15 +08:00
|
|
|
struct device_node *anatop_np;
|
2012-03-14 10:29:12 +08:00
|
|
|
struct regulator_desc *rdesc;
|
|
|
|
struct regulator_dev *rdev;
|
|
|
|
struct anatop_regulator *sreg;
|
|
|
|
struct regulator_init_data *initdata;
|
2012-04-10 22:45:01 +08:00
|
|
|
struct regulator_config config = { };
|
2019-04-10 00:10:39 +08:00
|
|
|
struct regmap *regmap;
|
|
|
|
u32 control_reg;
|
|
|
|
u32 vol_bit_shift;
|
|
|
|
u32 vol_bit_width;
|
|
|
|
u32 min_bit_val;
|
|
|
|
u32 min_voltage;
|
|
|
|
u32 max_voltage;
|
2012-03-14 10:29:12 +08:00
|
|
|
int ret = 0;
|
2014-02-11 21:43:44 +08:00
|
|
|
u32 val;
|
2012-03-14 10:29:12 +08:00
|
|
|
|
|
|
|
sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
|
|
|
|
if (!sreg)
|
|
|
|
return -ENOMEM;
|
2017-04-12 09:58:44 +08:00
|
|
|
|
2012-03-14 10:29:12 +08:00
|
|
|
rdesc = &sreg->rdesc;
|
|
|
|
rdesc->type = REGULATOR_VOLTAGE;
|
|
|
|
rdesc->owner = THIS_MODULE;
|
2012-09-05 10:57:15 +08:00
|
|
|
|
2017-04-12 09:58:45 +08:00
|
|
|
of_property_read_string(np, "regulator-name", &rdesc->name);
|
2017-04-14 22:32:43 +08:00
|
|
|
if (!rdesc->name) {
|
|
|
|
dev_err(dev, "failed to get a regulator-name\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2017-04-12 09:58:45 +08:00
|
|
|
|
2014-11-10 21:43:53 +08:00
|
|
|
initdata = of_get_regulator_init_data(dev, np, rdesc);
|
2017-04-12 09:58:42 +08:00
|
|
|
if (!initdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-10-13 18:45:30 +08:00
|
|
|
initdata->supply_regulator = "vin";
|
2014-11-10 21:43:53 +08:00
|
|
|
|
2012-09-05 10:57:15 +08:00
|
|
|
anatop_np = of_get_parent(np);
|
|
|
|
if (!anatop_np)
|
|
|
|
return -ENODEV;
|
2019-04-10 00:10:39 +08:00
|
|
|
regmap = syscon_node_to_regmap(anatop_np);
|
2012-09-05 10:57:15 +08:00
|
|
|
of_node_put(anatop_np);
|
2019-04-10 00:10:39 +08:00
|
|
|
if (IS_ERR(regmap))
|
|
|
|
return PTR_ERR(regmap);
|
2012-09-05 10:57:15 +08:00
|
|
|
|
2019-04-10 00:10:39 +08:00
|
|
|
ret = of_property_read_u32(np, "anatop-reg-offset", &control_reg);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (ret) {
|
2012-03-27 15:54:01 +08:00
|
|
|
dev_err(dev, "no anatop-reg-offset property set\n");
|
2014-01-06 20:13:15 +08:00
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
2019-04-10 00:10:39 +08:00
|
|
|
ret = of_property_read_u32(np, "anatop-vol-bit-width", &vol_bit_width);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-vol-bit-width property set\n");
|
2014-01-06 20:13:15 +08:00
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
2019-04-10 00:10:39 +08:00
|
|
|
ret = of_property_read_u32(np, "anatop-vol-bit-shift", &vol_bit_shift);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-vol-bit-shift property set\n");
|
2014-01-06 20:13:15 +08:00
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
2019-04-10 00:10:39 +08:00
|
|
|
ret = of_property_read_u32(np, "anatop-min-bit-val", &min_bit_val);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-min-bit-val property set\n");
|
2014-01-06 20:13:15 +08:00
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
2019-04-10 00:10:39 +08:00
|
|
|
ret = of_property_read_u32(np, "anatop-min-voltage", &min_voltage);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-min-voltage property set\n");
|
2014-01-06 20:13:15 +08:00
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
2019-04-10 00:10:39 +08:00
|
|
|
ret = of_property_read_u32(np, "anatop-max-voltage", &max_voltage);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-max-voltage property set\n");
|
2014-01-06 20:13:15 +08:00
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 00:23:53 +08:00
|
|
|
/* read LDO ramp up setting, only for core reg */
|
|
|
|
of_property_read_u32(np, "anatop-delay-reg-offset",
|
|
|
|
&sreg->delay_reg);
|
|
|
|
of_property_read_u32(np, "anatop-delay-bit-width",
|
|
|
|
&sreg->delay_bit_width);
|
|
|
|
of_property_read_u32(np, "anatop-delay-bit-shift",
|
|
|
|
&sreg->delay_bit_shift);
|
|
|
|
|
2019-04-10 00:10:39 +08:00
|
|
|
rdesc->n_voltages = (max_voltage - min_voltage) / 25000 + 1
|
|
|
|
+ min_bit_val;
|
|
|
|
rdesc->min_uV = min_voltage;
|
2012-05-14 11:06:44 +08:00
|
|
|
rdesc->uV_step = 25000;
|
2019-04-10 00:10:39 +08:00
|
|
|
rdesc->linear_min_sel = min_bit_val;
|
|
|
|
rdesc->vsel_reg = control_reg;
|
|
|
|
rdesc->vsel_mask = ((1 << vol_bit_width) - 1) << vol_bit_shift;
|
2015-10-13 18:45:30 +08:00
|
|
|
rdesc->min_dropout_uV = 125000;
|
2012-03-14 10:29:12 +08:00
|
|
|
|
2012-04-10 22:45:01 +08:00
|
|
|
config.dev = &pdev->dev;
|
|
|
|
config.init_data = initdata;
|
|
|
|
config.driver_data = sreg;
|
|
|
|
config.of_node = pdev->dev.of_node;
|
2019-04-10 00:10:39 +08:00
|
|
|
config.regmap = regmap;
|
2012-04-10 22:45:01 +08:00
|
|
|
|
2014-02-11 21:43:44 +08:00
|
|
|
/* Only core regulators have the ramp up delay configuration. */
|
2019-04-10 00:10:39 +08:00
|
|
|
if (control_reg && sreg->delay_bit_width) {
|
2014-02-11 21:43:44 +08:00
|
|
|
rdesc->ops = &anatop_core_rops;
|
|
|
|
|
|
|
|
ret = regmap_read(config.regmap, rdesc->vsel_reg, &val);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "failed to read initial state\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:10:39 +08:00
|
|
|
sreg->sel = (val & rdesc->vsel_mask) >> vol_bit_shift;
|
2014-02-11 21:43:45 +08:00
|
|
|
if (sreg->sel == LDO_FET_FULL_ON) {
|
|
|
|
sreg->sel = 0;
|
|
|
|
sreg->bypass = true;
|
|
|
|
}
|
2014-10-07 03:33:36 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* In case vddpu was disabled by the bootloader, we need to set
|
|
|
|
* a sane default until imx6-cpufreq was probed and changes the
|
|
|
|
* voltage to the correct value. In this case we set 1.25V.
|
|
|
|
*/
|
2017-04-12 09:58:45 +08:00
|
|
|
if (!sreg->sel && !strcmp(rdesc->name, "vddpu"))
|
2014-10-07 03:33:36 +08:00
|
|
|
sreg->sel = 22;
|
2014-10-07 03:33:37 +08:00
|
|
|
|
2017-04-12 09:58:47 +08:00
|
|
|
/* set the default voltage of the pcie phy to be 1.100v */
|
2017-04-14 22:32:43 +08:00
|
|
|
if (!sreg->sel && !strcmp(rdesc->name, "vddpcie"))
|
2017-04-12 09:58:47 +08:00
|
|
|
sreg->sel = 0x10;
|
|
|
|
|
2016-06-17 18:31:37 +08:00
|
|
|
if (!sreg->bypass && !sreg->sel) {
|
2014-10-07 03:33:37 +08:00
|
|
|
dev_err(&pdev->dev, "Failed to read a valid default voltage selector.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-02-11 21:43:44 +08:00
|
|
|
} else {
|
2017-01-11 00:30:14 +08:00
|
|
|
u32 enable_bit;
|
|
|
|
|
2014-02-11 21:43:44 +08:00
|
|
|
rdesc->ops = &anatop_rops;
|
2017-01-11 00:30:14 +08:00
|
|
|
|
|
|
|
if (!of_property_read_u32(np, "anatop-enable-bit",
|
|
|
|
&enable_bit)) {
|
|
|
|
anatop_rops.enable = regulator_enable_regmap;
|
|
|
|
anatop_rops.disable = regulator_disable_regmap;
|
|
|
|
anatop_rops.is_enabled = regulator_is_enabled_regmap;
|
|
|
|
|
2019-04-10 00:10:39 +08:00
|
|
|
rdesc->enable_reg = control_reg;
|
2017-01-11 00:30:14 +08:00
|
|
|
rdesc->enable_mask = BIT(enable_bit);
|
|
|
|
}
|
2014-02-11 21:43:44 +08:00
|
|
|
}
|
|
|
|
|
2012-03-14 10:29:12 +08:00
|
|
|
/* register regulator */
|
2013-09-04 14:30:57 +08:00
|
|
|
rdev = devm_regulator_register(dev, rdesc, &config);
|
2012-03-14 10:29:12 +08:00
|
|
|
if (IS_ERR(rdev)) {
|
2020-03-03 21:44:12 +08:00
|
|
|
ret = PTR_ERR(rdev);
|
|
|
|
if (ret == -EPROBE_DEFER)
|
|
|
|
dev_dbg(dev, "failed to register %s, deferring...\n",
|
|
|
|
rdesc->name);
|
|
|
|
else
|
|
|
|
dev_err(dev, "failed to register %s\n", rdesc->name);
|
|
|
|
return ret;
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, rdev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-07 15:55:10 +08:00
|
|
|
static const struct of_device_id of_anatop_regulator_match_tbl[] = {
|
2012-03-14 10:29:12 +08:00
|
|
|
{ .compatible = "fsl,anatop-regulator", },
|
|
|
|
{ /* end */ }
|
|
|
|
};
|
2015-09-19 01:09:07 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, of_anatop_regulator_match_tbl);
|
2012-03-14 10:29:12 +08:00
|
|
|
|
2012-04-02 14:57:01 +08:00
|
|
|
static struct platform_driver anatop_regulator_driver = {
|
2012-03-14 10:29:12 +08:00
|
|
|
.driver = {
|
|
|
|
.name = "anatop_regulator",
|
|
|
|
.of_match_table = of_anatop_regulator_match_tbl,
|
|
|
|
},
|
|
|
|
.probe = anatop_regulator_probe,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init anatop_regulator_init(void)
|
|
|
|
{
|
2012-04-02 14:57:01 +08:00
|
|
|
return platform_driver_register(&anatop_regulator_driver);
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
|
|
|
postcore_initcall(anatop_regulator_init);
|
|
|
|
|
|
|
|
static void __exit anatop_regulator_exit(void)
|
|
|
|
{
|
2012-04-02 14:57:01 +08:00
|
|
|
platform_driver_unregister(&anatop_regulator_driver);
|
2012-03-14 10:29:12 +08:00
|
|
|
}
|
|
|
|
module_exit(anatop_regulator_exit);
|
|
|
|
|
2013-10-14 16:45:51 +08:00
|
|
|
MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>");
|
|
|
|
MODULE_AUTHOR("Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
|
2012-03-14 10:29:12 +08:00
|
|
|
MODULE_DESCRIPTION("ANATOP Regulator driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
2013-12-31 20:56:00 +08:00
|
|
|
MODULE_ALIAS("platform:anatop_regulator");
|