diff mbox

[v5,4/5] regulator: add regulator driver of hi655x pmic

Message ID 1452514817-118311-5-git-send-email-puck.chen@hisilicon.com
State New
Headers show

Commit Message

Chen Feng Jan. 11, 2016, 12:20 p.m. UTC
Add regulator support for hi655x pmic

Signed-off-by: Chen Feng <puck.chen@hisilicon.com>

Signed-off-by: Fei Wang <w.f@huawei.com>

Signed-off-by: Xinwei Kong <kong.kongxinwei@hisilicon.com>

---
 drivers/regulator/Kconfig            |   8 ++
 drivers/regulator/Makefile           |   1 +
 drivers/regulator/hi655x-regulator.c | 256 +++++++++++++++++++++++++++++++++++
 3 files changed, 265 insertions(+)
 create mode 100644 drivers/regulator/hi655x-regulator.c

-- 
1.9.1

Comments

Mark Brown Jan. 11, 2016, 6:24 p.m. UTC | #1
On Mon, Jan 11, 2016 at 08:20:16PM +0800, Chen Feng wrote:

> +config REGULATOR_HI655X

> +	tristate "Hisilicon HI655X PMIC regulators support"

> +	depends on ARCH_HISI || (COMPILE_TEST && ARM64)


Why does this depend on ARM64?  If it's needed it probably indicates a
problem...

> +	np = of_get_child_by_name(dev->parent->of_node, "regulators");

> +	if (!np)

> +		return -ENODEV;

> +

> +	ret = of_regulator_match(dev, np,

> +				 hi655x_regulator_match,

> +				 ARRAY_SIZE(hi655x_regulator_match));


Like I said on the previous version:

| Don't open code this, use the standard support with of_match and
| regulators_node.

The code is now using of_match but still open coding regulators_node.
Chen Feng Jan. 12, 2016, 2:18 a.m. UTC | #2
On 2016/1/12 2:24, Mark Brown wrote:
> On Mon, Jan 11, 2016 at 08:20:16PM +0800, Chen Feng wrote:

> 

>> +config REGULATOR_HI655X

>> +	tristate "Hisilicon HI655X PMIC regulators support"

>> +	depends on ARCH_HISI || (COMPILE_TEST && ARM64)

> 

> Why does this depend on ARM64?  If it's needed it probably indicates a

> problem...

> 

There will be compile warning with arch parisc.

Add the current support platform is ARM64.
>> +	np = of_get_child_by_name(dev->parent->of_node, "regulators");

>> +	if (!np)

>> +		return -ENODEV;

>> +

>> +	ret = of_regulator_match(dev, np,

>> +				 hi655x_regulator_match,

>> +				 ARRAY_SIZE(hi655x_regulator_match));

> 

> Like I said on the previous version:

> 

> | Don't open code this, use the standard support with of_match and

> | regulators_node.

> 

> The code is now using of_match but still open coding regulators_node.

> 

I am not sure about open coding regulators_node.

I take max8907-regulator.c for reference. The code there is:
224 static int max8907_regulator_parse_dt(struct platform_device *pdev)
225 {
226         struct device_node *np, *regulators;
227         int ret;
228
229         np = pdev->dev.parent->of_node;
230         if (!np)
231                 return 0;
232
233         regulators = of_get_child_by_name(np, "regulators");
	    ...
238
239         ret = of_regulator_match(&pdev->dev, regulators, max8907_matches,
240                                  ARRAY_SIZE(max8907_matches));

249 }

Can you give me some references? Really thanks for your help.

ChenFeng
Mark Brown Jan. 15, 2016, 6:07 p.m. UTC | #3
On Tue, Jan 12, 2016 at 10:18:03AM +0800, chenfeng wrote:
> On 2016/1/12 2:24, Mark Brown wrote:

> > On Mon, Jan 11, 2016 at 08:20:16PM +0800, Chen Feng wrote:


> >> +config REGULATOR_HI655X

> >> +	tristate "Hisilicon HI655X PMIC regulators support"

> >> +	depends on ARCH_HISI || (COMPILE_TEST && ARM64)


> > Why does this depend on ARM64?  If it's needed it probably indicates a

> > problem...


> There will be compile warning with arch parisc.


> Add the current support platform is ARM64.


The whole point of COMPILE_TEST is to allow people who are working
generally rather than with the particular hardware to build things to
improve build test coverage when doing general kernel work.  A warning
on a very obscure architecture is really not a blocker here.

> I am not sure about open coding regulators_node.


> I take max8907-regulator.c for reference. The code there is:

> 224 static int max8907_regulator_parse_dt(struct platform_device *pdev)


The existance of older drivers that have not yet been converted to use
newer core subsystem features is not a good reason to avoid using those
core subsystem features.  You'll commonly find this situation in the
kernel.

> Can you give me some references? Really thanks for your help.


$ grep -l regulators_node drivers/regulator/*.c
drivers/regulator/88pm800.c
drivers/regulator/axp20x-regulator.c
drivers/regulator/da9062-regulator.c
drivers/regulator/isl9305.c
drivers/regulator/max14577.c
drivers/regulator/max77686.c
drivers/regulator/max77693.c
drivers/regulator/max77802.c
drivers/regulator/mt6311-regulator.c
drivers/regulator/of_regulator.c
drivers/regulator/pv88060-regulator.c
drivers/regulator/pv88090-regulator.c
drivers/regulator/rn5t618-regulator.c
drivers/regulator/rt5033-regulator.c
drivers/regulator/sky81452-regulator.c
drivers/regulator/tps65023-regulator.c
drivers/regulator/tps65086-regulator.c
drivers/regulator/tps65217-regulator.c
Chen Feng Jan. 18, 2016, 8:26 a.m. UTC | #4
On 2016/1/16 2:07, Mark Brown wrote:
> On Tue, Jan 12, 2016 at 10:18:03AM +0800, chenfeng wrote:

>> On 2016/1/12 2:24, Mark Brown wrote:

>>> On Mon, Jan 11, 2016 at 08:20:16PM +0800, Chen Feng wrote:

> 

>>>> +config REGULATOR_HI655X

>>>> +	tristate "Hisilicon HI655X PMIC regulators support"

>>>> +	depends on ARCH_HISI || (COMPILE_TEST && ARM64)

> 

>>> Why does this depend on ARM64?  If it's needed it probably indicates a

>>> problem...

> 

>> There will be compile warning with arch parisc.

> 

>> Add the current support platform is ARM64.

> 

> The whole point of COMPILE_TEST is to allow people who are working

> generally rather than with the particular hardware to build things to

> improve build test coverage when doing general kernel work.  A warning

> on a very obscure architecture is really not a blocker here.

> 

ok, I will remove the ARM64 depends.
>> I am not sure about open coding regulators_node.

> 

>> I take max8907-regulator.c for reference. The code there is:

>> 224 static int max8907_regulator_parse_dt(struct platform_device *pdev)

> 

> The existance of older drivers that have not yet been converted to use

> newer core subsystem features is not a good reason to avoid using those

> core subsystem features.  You'll commonly find this situation in the

> kernel.

> 

>> Can you give me some references? Really thanks for your help.

> 

> $ grep -l regulators_node drivers/regulator/*.c

> drivers/regulator/88pm800.c

> drivers/regulator/axp20x-regulator.c

> drivers/regulator/da9062-regulator.c

> drivers/regulator/isl9305.c

> drivers/regulator/max14577.c

> drivers/regulator/max77686.c

> drivers/regulator/max77693.c

> drivers/regulator/max77802.c

> drivers/regulator/mt6311-regulator.c

> drivers/regulator/of_regulator.c

> drivers/regulator/pv88060-regulator.c

> drivers/regulator/pv88090-regulator.c

> drivers/regulator/rn5t618-regulator.c

> drivers/regulator/rt5033-regulator.c

> drivers/regulator/sky81452-regulator.c

> drivers/regulator/tps65023-regulator.c

> drivers/regulator/tps65086-regulator.c

> drivers/regulator/tps65217-regulator.c

> 

Understand, I will remove the match-table and add the regulators_node
to match the regulator-compatible in dts.
diff mbox

Patch

diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index 8df0b0e..c00c915 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -261,6 +261,14 @@  config REGULATOR_HI6421
 	  21 general purpose LDOs, 3 dedicated LDOs, and 5 BUCKs. All
 	  of them come with support to either ECO (idle) or sleep mode.
 
+config REGULATOR_HI655X
+	tristate "Hisilicon HI655X PMIC regulators support"
+	depends on ARCH_HISI || (COMPILE_TEST && ARM64)
+	depends on MFD_HI655X_PMIC && OF
+	help
+	  This driver provides support for the voltage regulators of the
+	  Hisilicon Hi655x PMIC device.
+
 config REGULATOR_ISL9305
 	tristate "Intersil ISL9305 regulator"
 	depends on I2C
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index 0f81749..8e4db96 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -34,6 +34,7 @@  obj-$(CONFIG_REGULATOR_DB8500_PRCMU) += db8500-prcmu.o
 obj-$(CONFIG_REGULATOR_FAN53555) += fan53555.o
 obj-$(CONFIG_REGULATOR_GPIO) += gpio-regulator.o
 obj-$(CONFIG_REGULATOR_HI6421) += hi6421-regulator.o
+obj-$(CONFIG_REGULATOR_HI655X) += hi655x-regulator.o
 obj-$(CONFIG_REGULATOR_ISL6271A) += isl6271a-regulator.o
 obj-$(CONFIG_REGULATOR_ISL9305) += isl9305.o
 obj-$(CONFIG_REGULATOR_LP3971) += lp3971.o
diff --git a/drivers/regulator/hi655x-regulator.c b/drivers/regulator/hi655x-regulator.c
new file mode 100644
index 0000000..e8e9995
--- /dev/null
+++ b/drivers/regulator/hi655x-regulator.c
@@ -0,0 +1,256 @@ 
+/*
+ * Device driver for regulators in hi655x IC
+ *
+ * Copyright (c) 2016 Hisilicon.
+ *
+ * Chen Feng <puck.chen@hisilicon.com>
+ * Fei  Wang <w.f@huawei.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/bitops.h>
+#include <linux/device.h>
+#include <linux/err.h>
+#include <linux/module.h>
+#include <linux/io.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/regulator/driver.h>
+#include <linux/regulator/machine.h>
+#include <linux/regulator/of_regulator.h>
+#include <linux/mfd/hi655x-pmic.h>
+
+struct hi655x_regulator {
+	unsigned int disable_reg;
+	unsigned int status_reg;
+	unsigned int ctrl_regs;
+	unsigned int ctrl_mask;
+	struct regulator_desc rdesc;
+};
+
+/* LDO7 & LDO10 */
+static const unsigned int ldo7_voltages[] = {
+	1800000, 1850000, 2850000, 2900000,
+	3000000, 3100000, 3200000, 3300000,
+};
+
+static const unsigned int ldo19_voltages[] = {
+	1800000, 1850000, 1900000, 1750000,
+	2800000, 2850000, 2900000, 3000000,
+};
+
+static const unsigned int ldo22_voltages[] = {
+	 900000, 1000000, 1050000, 1100000,
+	1150000, 1175000, 1185000, 1200000,
+};
+
+enum hi655x_regulator_id {
+	HI655X_LDO0,
+	HI655X_LDO1,
+	HI655X_LDO2,
+	HI655X_LDO3,
+	HI655X_LDO4,
+	HI655X_LDO5,
+	HI655X_LDO6,
+	HI655X_LDO7,
+	HI655X_LDO8,
+	HI655X_LDO9,
+	HI655X_LDO10,
+	HI655X_LDO11,
+	HI655X_LDO12,
+	HI655X_LDO13,
+	HI655X_LDO14,
+	HI655X_LDO15,
+	HI655X_LDO16,
+	HI655X_LDO17,
+	HI655X_LDO18,
+	HI655X_LDO19,
+	HI655X_LDO20,
+	HI655X_LDO21,
+	HI655X_LDO22,
+};
+
+static int hi655x_is_enabled(struct regulator_dev *rdev)
+{
+	unsigned int value = 0;
+
+	struct hi655x_regulator *regulator = rdev_get_drvdata(rdev);
+
+	regmap_read(rdev->regmap, regulator->status_reg, &value);
+	return (value & BIT(regulator->ctrl_mask));
+}
+
+static int hi655x_disable(struct regulator_dev *rdev)
+{
+	int ret = 0;
+
+	struct hi655x_regulator *regulator = rdev_get_drvdata(rdev);
+
+	ret = regmap_write(rdev->regmap, regulator->disable_reg,
+			   BIT(regulator->ctrl_mask));
+	return ret;
+}
+
+static struct regulator_ops hi655x_regulator_ops = {
+	.enable = regulator_enable_regmap,
+	.disable = hi655x_disable,
+	.is_enabled = hi655x_is_enabled,
+	.list_voltage = regulator_list_voltage_table,
+	.get_voltage_sel = regulator_get_voltage_sel_regmap,
+	.set_voltage_sel = regulator_set_voltage_sel_regmap,
+};
+
+static struct regulator_ops hi655x_ldo_linear_ops = {
+	.enable = regulator_enable_regmap,
+	.disable = hi655x_disable,
+	.is_enabled = hi655x_is_enabled,
+	.list_voltage = regulator_list_voltage_linear,
+	.get_voltage_sel = regulator_get_voltage_sel_regmap,
+	.set_voltage_sel = regulator_set_voltage_sel_regmap,
+};
+
+#define HI655X_LDO(_ID, vreg, vmask, ereg, dreg,                \
+		   sreg, cmask, vtable) {                       \
+	.rdesc = {                                              \
+		.name           = #_ID,                         \
+		.ops            = &hi655x_regulator_ops,        \
+		.type           = REGULATOR_VOLTAGE,            \
+		.id             = HI655X_##_ID,                 \
+		.owner          = THIS_MODULE,                  \
+		.n_voltages     = ARRAY_SIZE(vtable),           \
+		.volt_table     = vtable,                       \
+		.vsel_reg       = HI655X_BUS_ADDR(vreg),        \
+		.vsel_mask      = vmask,                        \
+		.enable_reg     = HI655X_BUS_ADDR(ereg),        \
+		.enable_mask    = cmask,                        \
+	},                                                      \
+	.disable_reg = HI655X_BUS_ADDR(dreg),                   \
+	.status_reg = HI655X_BUS_ADDR(sreg),                    \
+	.ctrl_mask = cmask,                                     \
+}
+
+#define HI655X_LDO_LINEAR(_ID, vreg, vmask, ereg, dreg,         \
+			  sreg, cmask, minv, nvolt, vstep) {    \
+	.rdesc = {                                              \
+		.name           = #_ID,                         \
+		.ops            = &hi655x_ldo_linear_ops,       \
+		.type           = REGULATOR_VOLTAGE,            \
+		.id             = HI655X_##_ID,                 \
+		.owner          = THIS_MODULE,                  \
+		.min_uV         = minv,                         \
+		.n_voltages     = nvolt,                        \
+		.uV_step        = vstep,                        \
+		.uV_step        = vstep,                        \
+		.vsel_reg       = HI655X_BUS_ADDR(vreg),        \
+		.vsel_mask      = vmask,                        \
+		.enable_reg     = HI655X_BUS_ADDR(ereg),        \
+		.enable_mask    = cmask,                        \
+	},                                                      \
+	.disable_reg = HI655X_BUS_ADDR(dreg),                   \
+	.status_reg = HI655X_BUS_ADDR(sreg),                    \
+	.ctrl_mask = cmask,                                     \
+}
+
+static struct hi655x_regulator regulators[] = {
+	HI655X_LDO_LINEAR(LDO2, 0x72, 0x07, 0x29, 0x2a, 0x2b, 0x01,
+			  2500000, 8, 100000),
+	HI655X_LDO(LDO7, 0x78, 0x07, 0x29, 0x2a, 0x2b, 0x06, ldo7_voltages),
+	HI655X_LDO(LDO10, 0x78, 0x07, 0x29, 0x2a, 0x2b, 0x01, ldo7_voltages),
+	HI655X_LDO_LINEAR(LDO13, 0x7e, 0x07, 0x2c, 0x2d, 0x2e, 0x04,
+			  1600000, 8, 50000),
+	HI655X_LDO_LINEAR(LDO14, 0x7f, 0x07, 0x2c, 0x2d, 0x2e, 0x05,
+			  2500000, 8, 100000),
+	HI655X_LDO_LINEAR(LDO15, 0x80, 0x07, 0x2c, 0x2d, 0x2e, 0x06,
+			  1600000, 8, 50000),
+	HI655X_LDO_LINEAR(LDO17, 0x82, 0x07, 0x2f, 0x30, 0x31, 0x00,
+			  2500000, 8, 100000),
+	HI655X_LDO(LDO19, 0x84, 0x07, 0x2f, 0x30, 0x31, 0x02, ldo19_voltages),
+	HI655X_LDO_LINEAR(LDO21, 0x86, 0x07, 0x2f, 0x30, 0x31, 0x04,
+			  1650000, 8, 50000),
+	HI655X_LDO(LDO22, 0x87, 0x07, 0x2f, 0x30, 0x31, 0x05, ldo22_voltages),
+};
+
+static struct of_regulator_match hi655x_regulator_match[] = {
+	{ .name	= "ldo2", .driver_data = (void *)HI655X_LDO0, },
+	{ .name	= "ldo7", .driver_data = (void *)HI655X_LDO7, },
+	{ .name	= "ldo10", .driver_data = (void *)HI655X_LDO10, },
+	{ .name	= "ldo13", .driver_data = (void *)HI655X_LDO13, },
+	{ .name	= "ldo14", .driver_data = (void *)HI655X_LDO14, },
+	{ .name = "ldo15", .driver_data = (void *)HI655X_LDO15, },
+	{ .name	= "ldo17", .driver_data = (void *)HI655X_LDO17, },
+	{ .name	= "ldo19", .driver_data = (void *)HI655X_LDO19, },
+	{ .name	= "ldo21", .driver_data = (void *)HI655X_LDO21, },
+	{ .name	= "ldo22", .driver_data = (void *)HI655X_LDO22, },
+};
+
+static int hi655x_regulator_probe(struct platform_device *pdev)
+{
+	unsigned int i;
+	int ret;
+	struct hi655x_regulator *regulator;
+	struct hi655x_pmic *pmic;
+	struct regulator_config config = { };
+	struct device *dev = &pdev->dev;
+	struct regulator_dev *rdev;
+	struct device_node *np;
+
+	np = of_get_child_by_name(dev->parent->of_node, "regulators");
+	if (!np)
+		return -ENODEV;
+
+	ret = of_regulator_match(dev, np,
+				 hi655x_regulator_match,
+				 ARRAY_SIZE(hi655x_regulator_match));
+	of_node_put(np);
+	if (ret < 0) {
+		dev_err(dev, "Error parsing regulator init data: %d\n", ret);
+		return ret;
+	}
+
+	pmic = dev_get_drvdata(pdev->dev.parent);
+	if (!pmic) {
+		dev_err(dev, "no pmic in the regulator parent node\n");
+		return -ENODEV;
+	}
+
+	regulator = devm_kzalloc(dev, sizeof(*regulator), GFP_KERNEL);
+	if (!regulator)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, regulator);
+
+	for (i = 0; i < ARRAY_SIZE(regulators); i++) {
+		config.dev = &pdev->dev;
+		config.driver_data = regulator;
+		config.regmap = pmic->regmap;
+		config.of_node = hi655x_regulator_match[i].of_node;
+		config.init_data = hi655x_regulator_match[i].init_data;
+
+		/* register regulator with framework */
+		rdev = devm_regulator_register(&pdev->dev,
+					       &regulators[i].rdesc,
+					       &config);
+		if (IS_ERR(rdev)) {
+			dev_err(&pdev->dev, "failed to register regulator %s\n",
+				regulator->rdesc.name);
+			return PTR_ERR(rdev);
+		}
+	}
+	return 0;
+}
+
+static struct platform_driver hi655x_regulator_driver = {
+	.driver = {
+		.name	= "hi655x-regulator",
+	},
+	.probe	= hi655x_regulator_probe,
+};
+module_platform_driver(hi655x_regulator_driver);
+
+MODULE_AUTHOR("Chen Feng <puck.chen@hisilicon.com>");
+MODULE_DESCRIPTION("Hisilicon hi655x regulator driver");
+MODULE_LICENSE("GPL v2");