2012-03-13 20:29:12 -06:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/module.h>
|
2012-09-04 20:57:15 -06:00
|
|
|
#include <linux/mfd/syscon.h>
|
2012-03-13 20:29:12 -06: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-04 20:57:15 -06:00
|
|
|
#include <linux/regmap.h>
|
2012-03-13 20:29:12 -06:00
|
|
|
#include <linux/regulator/driver.h>
|
|
|
|
#include <linux/regulator/of_regulator.h>
|
|
|
|
|
|
|
|
struct anatop_regulator {
|
|
|
|
const char *name;
|
|
|
|
u32 control_reg;
|
2012-09-04 20:57:15 -06:00
|
|
|
struct regmap *anatop;
|
2012-03-13 20:29:12 -06:00
|
|
|
int vol_bit_shift;
|
|
|
|
int vol_bit_width;
|
|
|
|
int min_bit_val;
|
|
|
|
int min_voltage;
|
|
|
|
int max_voltage;
|
|
|
|
struct regulator_desc rdesc;
|
|
|
|
struct regulator_init_data *initdata;
|
|
|
|
};
|
|
|
|
|
2012-09-04 20:57:15 -06:00
|
|
|
static int anatop_regmap_set_voltage_sel(struct regulator_dev *reg,
|
|
|
|
unsigned selector)
|
2012-03-13 20:29:12 -06:00
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
|
|
|
|
if (!anatop_reg->control_reg)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2012-12-08 17:07:43 -07:00
|
|
|
return regulator_set_voltage_sel_regmap(reg, selector);
|
2012-03-13 20:29:12 -06:00
|
|
|
}
|
|
|
|
|
2012-09-04 20:57:15 -06:00
|
|
|
static int anatop_regmap_get_voltage_sel(struct regulator_dev *reg)
|
2012-03-13 20:29:12 -06:00
|
|
|
{
|
|
|
|
struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
|
|
|
|
|
|
|
|
if (!anatop_reg->control_reg)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2012-12-08 17:07:43 -07:00
|
|
|
return regulator_get_voltage_sel_regmap(reg);
|
2012-03-13 20:29:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct regulator_ops anatop_rops = {
|
2012-09-04 20:57:15 -06:00
|
|
|
.set_voltage_sel = anatop_regmap_set_voltage_sel,
|
|
|
|
.get_voltage_sel = anatop_regmap_get_voltage_sel,
|
2012-06-03 09:02:34 -06:00
|
|
|
.list_voltage = regulator_list_voltage_linear,
|
|
|
|
.map_voltage = regulator_map_voltage_linear,
|
2012-03-13 20:29:12 -06:00
|
|
|
};
|
|
|
|
|
2012-11-19 11:22:22 -07:00
|
|
|
static int anatop_regulator_probe(struct platform_device *pdev)
|
2012-03-13 20:29:12 -06:00
|
|
|
{
|
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
struct device_node *np = dev->of_node;
|
2012-09-04 20:57:15 -06:00
|
|
|
struct device_node *anatop_np;
|
2012-03-13 20:29:12 -06:00
|
|
|
struct regulator_desc *rdesc;
|
|
|
|
struct regulator_dev *rdev;
|
|
|
|
struct anatop_regulator *sreg;
|
|
|
|
struct regulator_init_data *initdata;
|
2012-04-10 08:45:01 -06:00
|
|
|
struct regulator_config config = { };
|
2012-03-13 20:29:12 -06:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
initdata = of_get_regulator_init_data(dev, np);
|
|
|
|
sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
|
|
|
|
if (!sreg)
|
|
|
|
return -ENOMEM;
|
|
|
|
sreg->initdata = initdata;
|
|
|
|
sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
|
|
|
|
GFP_KERNEL);
|
|
|
|
rdesc = &sreg->rdesc;
|
|
|
|
memset(rdesc, 0, sizeof(*rdesc));
|
|
|
|
rdesc->name = sreg->name;
|
|
|
|
rdesc->ops = &anatop_rops;
|
|
|
|
rdesc->type = REGULATOR_VOLTAGE;
|
|
|
|
rdesc->owner = THIS_MODULE;
|
2012-09-04 20:57:15 -06:00
|
|
|
|
|
|
|
anatop_np = of_get_parent(np);
|
|
|
|
if (!anatop_np)
|
|
|
|
return -ENODEV;
|
|
|
|
sreg->anatop = syscon_node_to_regmap(anatop_np);
|
|
|
|
of_node_put(anatop_np);
|
|
|
|
if (IS_ERR(sreg->anatop))
|
|
|
|
return PTR_ERR(sreg->anatop);
|
|
|
|
|
2012-03-27 01:54:01 -06:00
|
|
|
ret = of_property_read_u32(np, "anatop-reg-offset",
|
|
|
|
&sreg->control_reg);
|
2012-03-13 20:29:12 -06:00
|
|
|
if (ret) {
|
2012-03-27 01:54:01 -06:00
|
|
|
dev_err(dev, "no anatop-reg-offset property set\n");
|
2012-03-13 20:29:12 -06:00
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
ret = of_property_read_u32(np, "anatop-vol-bit-width",
|
|
|
|
&sreg->vol_bit_width);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-vol-bit-width property set\n");
|
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
ret = of_property_read_u32(np, "anatop-vol-bit-shift",
|
|
|
|
&sreg->vol_bit_shift);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-vol-bit-shift property set\n");
|
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
ret = of_property_read_u32(np, "anatop-min-bit-val",
|
|
|
|
&sreg->min_bit_val);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-min-bit-val property set\n");
|
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
ret = of_property_read_u32(np, "anatop-min-voltage",
|
|
|
|
&sreg->min_voltage);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-min-voltage property set\n");
|
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
ret = of_property_read_u32(np, "anatop-max-voltage",
|
|
|
|
&sreg->max_voltage);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "no anatop-max-voltage property set\n");
|
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
|
2012-12-08 17:05:45 -07:00
|
|
|
rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage) / 25000 + 1
|
|
|
|
+ sreg->min_bit_val;
|
2012-05-13 21:06:44 -06:00
|
|
|
rdesc->min_uV = sreg->min_voltage;
|
|
|
|
rdesc->uV_step = 25000;
|
2012-12-08 17:05:45 -07:00
|
|
|
rdesc->linear_min_sel = sreg->min_bit_val;
|
2012-12-08 17:07:43 -07:00
|
|
|
rdesc->vsel_reg = sreg->control_reg;
|
|
|
|
rdesc->vsel_mask = ((1 << sreg->vol_bit_width) - 1) <<
|
|
|
|
sreg->vol_bit_shift;
|
2012-03-13 20:29:12 -06:00
|
|
|
|
2012-04-10 08:45:01 -06:00
|
|
|
config.dev = &pdev->dev;
|
|
|
|
config.init_data = initdata;
|
|
|
|
config.driver_data = sreg;
|
|
|
|
config.of_node = pdev->dev.of_node;
|
2012-12-08 17:07:43 -07:00
|
|
|
config.regmap = sreg->anatop;
|
2012-04-10 08:45:01 -06:00
|
|
|
|
2012-03-13 20:29:12 -06:00
|
|
|
/* register regulator */
|
2012-04-10 08:45:01 -06:00
|
|
|
rdev = regulator_register(rdesc, &config);
|
2012-03-13 20:29:12 -06:00
|
|
|
if (IS_ERR(rdev)) {
|
|
|
|
dev_err(dev, "failed to register %s\n",
|
|
|
|
rdesc->name);
|
|
|
|
ret = PTR_ERR(rdev);
|
|
|
|
goto anatop_probe_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, rdev);
|
|
|
|
|
|
|
|
anatop_probe_end:
|
|
|
|
if (ret)
|
|
|
|
kfree(sreg->name);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-19 11:26:10 -07:00
|
|
|
static int anatop_regulator_remove(struct platform_device *pdev)
|
2012-03-13 20:29:12 -06:00
|
|
|
{
|
|
|
|
struct regulator_dev *rdev = platform_get_drvdata(pdev);
|
|
|
|
struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
|
|
|
|
const char *name = sreg->name;
|
|
|
|
|
|
|
|
regulator_unregister(rdev);
|
|
|
|
kfree(name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
|
|
|
|
{ .compatible = "fsl,anatop-regulator", },
|
|
|
|
{ /* end */ }
|
|
|
|
};
|
|
|
|
|
2012-04-02 00:57:01 -06:00
|
|
|
static struct platform_driver anatop_regulator_driver = {
|
2012-03-13 20:29:12 -06:00
|
|
|
.driver = {
|
|
|
|
.name = "anatop_regulator",
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.of_match_table = of_anatop_regulator_match_tbl,
|
|
|
|
},
|
|
|
|
.probe = anatop_regulator_probe,
|
2012-11-19 11:20:42 -07:00
|
|
|
.remove = anatop_regulator_remove,
|
2012-03-13 20:29:12 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init anatop_regulator_init(void)
|
|
|
|
{
|
2012-04-02 00:57:01 -06:00
|
|
|
return platform_driver_register(&anatop_regulator_driver);
|
2012-03-13 20:29:12 -06:00
|
|
|
}
|
|
|
|
postcore_initcall(anatop_regulator_init);
|
|
|
|
|
|
|
|
static void __exit anatop_regulator_exit(void)
|
|
|
|
{
|
2012-04-02 00:57:01 -06:00
|
|
|
platform_driver_unregister(&anatop_regulator_driver);
|
2012-03-13 20:29:12 -06:00
|
|
|
}
|
|
|
|
module_exit(anatop_regulator_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
|
|
|
|
"Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
|
|
|
|
MODULE_DESCRIPTION("ANATOP Regulator driver");
|
|
|
|
MODULE_LICENSE("GPL v2");
|