diff mbox series

[5/6] regulator: tahvo-vcore: Add basic Tahvo/Betty ASIC Vcore output support

Message ID 20211224215635.1585808-5-peter.vasil@gmail.com (mailing list archive)
State New, archived
Headers show
Series OMAP2: Nokia N810: prepare tahvo cells for display | expand

Commit Message

Peter Vasil Dec. 24, 2021, 9:56 p.m. UTC
From: Peter Vasil <peter.vasil@gmail.com>

Nokia Tahvo/Betty ASIC is a companion chip for mobile devices. One of
its outputs is a Vcore adjustable voltage regulator.
Register control code has been written based on original Nokia kernel
sources for N810 display driver.
Driver expects a regmap device as parent, usually retu-mfd driver bound
to the Tahvo ASIC.
---
 drivers/regulator/Kconfig                 |   8 ++
 drivers/regulator/Makefile                |   1 +
 drivers/regulator/tahvo-vcore-regulator.c | 104 ++++++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 drivers/regulator/tahvo-vcore-regulator.c
diff mbox series

Patch

diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index 6be9b1c8a615..16c684c742cf 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -1404,6 +1404,14 @@  config REGULATOR_WM8994
 	  This driver provides support for the voltage regulators on the
 	  WM8994 CODEC.
 
+config REGULATOR_TAHVO_VCORE
+	tristate "Tahvo/Betty Vcore regulator support"
+	depends on MFD_RETU
+	help
+	  This driver supports Vcore voltage control on Nokia's Tahvo/Betty
+	  ASIC chip. The regulator controls framebuffer supply voltage in
+	  Nokia internet tablets (for sure at least N810).
+
 config REGULATOR_QCOM_LABIBB
 	tristate "QCOM LAB/IBB regulator support"
 	depends on SPMI || COMPILE_TEST
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index b07d2a22df0b..ed64d2cf8a42 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -170,5 +170,6 @@  obj-$(CONFIG_REGULATOR_WM831X) += wm831x-ldo.o
 obj-$(CONFIG_REGULATOR_WM8350) += wm8350-regulator.o
 obj-$(CONFIG_REGULATOR_WM8400) += wm8400-regulator.o
 obj-$(CONFIG_REGULATOR_WM8994) += wm8994-regulator.o
+obj-$(CONFIG_REGULATOR_TAHVO_VCORE) += tahvo-vcore-regulator.o
 
 ccflags-$(CONFIG_REGULATOR_DEBUG) += -DDEBUG
diff --git a/drivers/regulator/tahvo-vcore-regulator.c b/drivers/regulator/tahvo-vcore-regulator.c
new file mode 100644
index 000000000000..a7c92aa7a148
--- /dev/null
+++ b/drivers/regulator/tahvo-vcore-regulator.c
@@ -0,0 +1,104 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+//
+// Copyright (C) 2004, 2005 Nokia Corporation
+//
+// Based on original 2.6 kernel driver for Nokia N8x0 LCD panel.
+// Rewritten in 2021 by Peter Vasil <petervasil@gmail.com>.
+//
+// Driver for Nokia Betty/Tahvo Vcore regulator
+// The only known voltages are currently 1.005V==0x0f and 1.475V==0x00 with mask 0x0f
+// Whether the sequence is actually linear is only a guess.
+
+#include <linux/io.h>
+#include <linux/module.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>
+
+#define TAHVO_REG_VCORE		0x07
+
+// Values in this table are simply interpolated from the only known min/max.
+static const unsigned int tahvo_vcore_voltages[] = {
+	1475000, 1443667, 1412333, 1381000, 1349667, 1318333, 1287000, 1255667,
+	1224333, 1193000, 1161667, 1130333, 1099000, 1067667, 1036333, 1005000,
+};
+
+static const struct regulator_ops tahvo_vcore_regulator_voltage_ops = {
+	.list_voltage = regulator_list_voltage_table,
+	.map_voltage = regulator_map_voltage_iterate,
+	.get_voltage_sel = regulator_get_voltage_sel_regmap,
+	.set_voltage_sel = regulator_set_voltage_sel_regmap,
+};
+
+static const struct regulator_desc vcore_regulator = {
+	.name		= "vcore",
+	.ops		= &tahvo_vcore_regulator_voltage_ops,
+	.type		= REGULATOR_VOLTAGE,
+	.owner		= THIS_MODULE,
+	.volt_table	= tahvo_vcore_voltages,
+	.n_voltages	= ARRAY_SIZE(tahvo_vcore_voltages),
+	.vsel_reg	= TAHVO_REG_VCORE,
+	.vsel_mask	= 0x0f,
+};
+
+static const struct regmap_config tahvo_vcore_regmap_config = {
+	.reg_bits	= 8,
+	.reg_stride	= 1,
+	.val_bits	= 16,
+};
+
+static int tahvo_vcore_regulator_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct regulator_init_data *init_data;
+	struct regulator_config cfg = {};
+	struct regulator_dev *rdev;
+
+	init_data = of_get_regulator_init_data(dev, dev->of_node,
+					       &vcore_regulator);
+	if (!init_data) {
+		dev_err(dev, "Failed to init regulator data!\n");
+		return -EINVAL;
+	}
+
+	cfg.dev = dev;
+	cfg.init_data = init_data;
+	cfg.of_node = dev->of_node;
+
+	cfg.regmap = dev_get_regmap(dev->parent, NULL);
+	if (!cfg.regmap) {
+		dev_err(dev, "failed to locate regmap\n");
+		return -ENODEV;
+	}
+
+	rdev = devm_regulator_register(dev, &vcore_regulator, &cfg);
+	if (IS_ERR(rdev)) {
+		dev_err(dev, "Failed to register regulator: %ld\n",
+			PTR_ERR(rdev));
+		return PTR_ERR(rdev);
+	}
+	platform_set_drvdata(pdev, rdev);
+
+	return 0;
+}
+
+static const struct of_device_id regulator_tahvo_vcore_of_match[] = {
+	{ .compatible = "nokia,tahvo-vcore-regulator", },
+	{},
+};
+
+static struct platform_driver tahvo_vcore_regulator_driver = {
+	.probe = tahvo_vcore_regulator_probe,
+	.driver = {
+		.name = "tahvo-vcore-regulator",
+		.of_match_table = of_match_ptr(regulator_tahvo_vcore_of_match),
+	},
+};
+module_platform_driver(tahvo_vcore_regulator_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Peter Vasil <petervasil@gmail.com>");
+MODULE_DESCRIPTION("Tahvo/Betty Vcore voltage regulator");