18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci//
38c2ecf20Sopenharmony_ci// Lochnagar regulator driver
48c2ecf20Sopenharmony_ci//
58c2ecf20Sopenharmony_ci// Copyright (c) 2017-2018 Cirrus Logic, Inc. and
68c2ecf20Sopenharmony_ci//                         Cirrus Logic International Semiconductor Ltd.
78c2ecf20Sopenharmony_ci//
88c2ecf20Sopenharmony_ci// Author: Charles Keepax <ckeepax@opensource.cirrus.com>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/bitops.h>
118c2ecf20Sopenharmony_ci#include <linux/device.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/mutex.h>
158c2ecf20Sopenharmony_ci#include <linux/of.h>
168c2ecf20Sopenharmony_ci#include <linux/of_device.h>
178c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
188c2ecf20Sopenharmony_ci#include <linux/regmap.h>
198c2ecf20Sopenharmony_ci#include <linux/regulator/driver.h>
208c2ecf20Sopenharmony_ci#include <linux/regulator/machine.h>
218c2ecf20Sopenharmony_ci#include <linux/regulator/of_regulator.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/mfd/lochnagar.h>
248c2ecf20Sopenharmony_ci#include <linux/mfd/lochnagar1_regs.h>
258c2ecf20Sopenharmony_ci#include <linux/mfd/lochnagar2_regs.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistatic const struct regulator_ops lochnagar_micvdd_ops = {
288c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
298c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
308c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
338c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
368c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic const struct linear_range lochnagar_micvdd_ranges[] = {
408c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(1000000, 0,    0xC, 50000),
418c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(1700000, 0xD, 0x1F, 100000),
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic int lochnagar_micbias_enable(struct regulator_dev *rdev)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct lochnagar *lochnagar = rdev_get_drvdata(rdev);
478c2ecf20Sopenharmony_ci	int ret;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	mutex_lock(&lochnagar->analogue_config_lock);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	ret = regulator_enable_regmap(rdev);
528c2ecf20Sopenharmony_ci	if (ret < 0)
538c2ecf20Sopenharmony_ci		goto err;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	ret = lochnagar_update_config(lochnagar);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cierr:
588c2ecf20Sopenharmony_ci	mutex_unlock(&lochnagar->analogue_config_lock);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return ret;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int lochnagar_micbias_disable(struct regulator_dev *rdev)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct lochnagar *lochnagar = rdev_get_drvdata(rdev);
668c2ecf20Sopenharmony_ci	int ret;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	mutex_lock(&lochnagar->analogue_config_lock);
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	ret = regulator_disable_regmap(rdev);
718c2ecf20Sopenharmony_ci	if (ret < 0)
728c2ecf20Sopenharmony_ci		goto err;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	ret = lochnagar_update_config(lochnagar);
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cierr:
778c2ecf20Sopenharmony_ci	mutex_unlock(&lochnagar->analogue_config_lock);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return ret;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic const struct regulator_ops lochnagar_micbias_ops = {
838c2ecf20Sopenharmony_ci	.enable = lochnagar_micbias_enable,
848c2ecf20Sopenharmony_ci	.disable = lochnagar_micbias_disable,
858c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic const struct regulator_ops lochnagar_vddcore_ops = {
898c2ecf20Sopenharmony_ci	.enable = regulator_enable_regmap,
908c2ecf20Sopenharmony_ci	.disable = regulator_disable_regmap,
918c2ecf20Sopenharmony_ci	.is_enabled = regulator_is_enabled_regmap,
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	.list_voltage = regulator_list_voltage_linear_range,
948c2ecf20Sopenharmony_ci	.map_voltage = regulator_map_voltage_linear_range,
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	.get_voltage_sel = regulator_get_voltage_sel_regmap,
978c2ecf20Sopenharmony_ci	.set_voltage_sel = regulator_set_voltage_sel_regmap,
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const struct linear_range lochnagar_vddcore_ranges[] = {
1018c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(600000, 0,    0x7, 0),
1028c2ecf20Sopenharmony_ci	REGULATOR_LINEAR_RANGE(600000, 0x8, 0x41, 12500),
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cienum lochnagar_regulators {
1068c2ecf20Sopenharmony_ci	LOCHNAGAR_MICVDD,
1078c2ecf20Sopenharmony_ci	LOCHNAGAR_MIC1VDD,
1088c2ecf20Sopenharmony_ci	LOCHNAGAR_MIC2VDD,
1098c2ecf20Sopenharmony_ci	LOCHNAGAR_VDDCORE,
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic int lochnagar_micbias_of_parse(struct device_node *np,
1138c2ecf20Sopenharmony_ci				      const struct regulator_desc *desc,
1148c2ecf20Sopenharmony_ci				      struct regulator_config *config)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct lochnagar *lochnagar = config->driver_data;
1178c2ecf20Sopenharmony_ci	int shift = (desc->id - LOCHNAGAR_MIC1VDD) *
1188c2ecf20Sopenharmony_ci		    LOCHNAGAR2_P2_MICBIAS_SRC_SHIFT;
1198c2ecf20Sopenharmony_ci	int mask = LOCHNAGAR2_P1_MICBIAS_SRC_MASK << shift;
1208c2ecf20Sopenharmony_ci	unsigned int val;
1218c2ecf20Sopenharmony_ci	int ret;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "cirrus,micbias-input", &val);
1248c2ecf20Sopenharmony_ci	if (ret >= 0) {
1258c2ecf20Sopenharmony_ci		mutex_lock(&lochnagar->analogue_config_lock);
1268c2ecf20Sopenharmony_ci		ret = regmap_update_bits(lochnagar->regmap,
1278c2ecf20Sopenharmony_ci					 LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
1288c2ecf20Sopenharmony_ci					 mask, val << shift);
1298c2ecf20Sopenharmony_ci		mutex_unlock(&lochnagar->analogue_config_lock);
1308c2ecf20Sopenharmony_ci		if (ret < 0) {
1318c2ecf20Sopenharmony_ci			dev_err(lochnagar->dev,
1328c2ecf20Sopenharmony_ci				"Failed to update micbias source: %d\n", ret);
1338c2ecf20Sopenharmony_ci			return ret;
1348c2ecf20Sopenharmony_ci		}
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	return 0;
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic const struct regulator_desc lochnagar_regulators[] = {
1418c2ecf20Sopenharmony_ci	[LOCHNAGAR_MICVDD] = {
1428c2ecf20Sopenharmony_ci		.name = "MICVDD",
1438c2ecf20Sopenharmony_ci		.supply_name = "SYSVDD",
1448c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,
1458c2ecf20Sopenharmony_ci		.n_voltages = 32,
1468c2ecf20Sopenharmony_ci		.ops = &lochnagar_micvdd_ops,
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		.id = LOCHNAGAR_MICVDD,
1498c2ecf20Sopenharmony_ci		.of_match = of_match_ptr("MICVDD"),
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		.enable_reg = LOCHNAGAR2_MICVDD_CTRL1,
1528c2ecf20Sopenharmony_ci		.enable_mask = LOCHNAGAR2_MICVDD_REG_ENA_MASK,
1538c2ecf20Sopenharmony_ci		.vsel_reg = LOCHNAGAR2_MICVDD_CTRL2,
1548c2ecf20Sopenharmony_ci		.vsel_mask = LOCHNAGAR2_MICVDD_VSEL_MASK,
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		.linear_ranges = lochnagar_micvdd_ranges,
1578c2ecf20Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(lochnagar_micvdd_ranges),
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci		.enable_time = 3000,
1608c2ecf20Sopenharmony_ci		.ramp_delay = 1000,
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
1638c2ecf20Sopenharmony_ci	},
1648c2ecf20Sopenharmony_ci	[LOCHNAGAR_MIC1VDD] = {
1658c2ecf20Sopenharmony_ci		.name = "MIC1VDD",
1668c2ecf20Sopenharmony_ci		.supply_name = "MICBIAS1",
1678c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,
1688c2ecf20Sopenharmony_ci		.ops = &lochnagar_micbias_ops,
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		.id = LOCHNAGAR_MIC1VDD,
1718c2ecf20Sopenharmony_ci		.of_match = of_match_ptr("MIC1VDD"),
1728c2ecf20Sopenharmony_ci		.of_parse_cb = lochnagar_micbias_of_parse,
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci		.enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
1758c2ecf20Sopenharmony_ci		.enable_mask = LOCHNAGAR2_P1_INPUT_BIAS_ENA_MASK,
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
1788c2ecf20Sopenharmony_ci	},
1798c2ecf20Sopenharmony_ci	[LOCHNAGAR_MIC2VDD] = {
1808c2ecf20Sopenharmony_ci		.name = "MIC2VDD",
1818c2ecf20Sopenharmony_ci		.supply_name = "MICBIAS2",
1828c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,
1838c2ecf20Sopenharmony_ci		.ops = &lochnagar_micbias_ops,
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci		.id = LOCHNAGAR_MIC2VDD,
1868c2ecf20Sopenharmony_ci		.of_match = of_match_ptr("MIC2VDD"),
1878c2ecf20Sopenharmony_ci		.of_parse_cb = lochnagar_micbias_of_parse,
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		.enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
1908c2ecf20Sopenharmony_ci		.enable_mask = LOCHNAGAR2_P2_INPUT_BIAS_ENA_MASK,
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
1938c2ecf20Sopenharmony_ci	},
1948c2ecf20Sopenharmony_ci	[LOCHNAGAR_VDDCORE] = {
1958c2ecf20Sopenharmony_ci		.name = "VDDCORE",
1968c2ecf20Sopenharmony_ci		.supply_name = "SYSVDD",
1978c2ecf20Sopenharmony_ci		.type = REGULATOR_VOLTAGE,
1988c2ecf20Sopenharmony_ci		.n_voltages = 66,
1998c2ecf20Sopenharmony_ci		.ops = &lochnagar_vddcore_ops,
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci		.id = LOCHNAGAR_VDDCORE,
2028c2ecf20Sopenharmony_ci		.of_match = of_match_ptr("VDDCORE"),
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		.enable_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL1,
2058c2ecf20Sopenharmony_ci		.enable_mask = LOCHNAGAR2_VDDCORE_CDC_REG_ENA_MASK,
2068c2ecf20Sopenharmony_ci		.vsel_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL2,
2078c2ecf20Sopenharmony_ci		.vsel_mask = LOCHNAGAR2_VDDCORE_CDC_VSEL_MASK,
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		.linear_ranges = lochnagar_vddcore_ranges,
2108c2ecf20Sopenharmony_ci		.n_linear_ranges = ARRAY_SIZE(lochnagar_vddcore_ranges),
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		.enable_time = 3000,
2138c2ecf20Sopenharmony_ci		.ramp_delay = 1000,
2148c2ecf20Sopenharmony_ci		.off_on_delay = 15000,
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci		.owner = THIS_MODULE,
2178c2ecf20Sopenharmony_ci	},
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic const struct of_device_id lochnagar_of_match[] = {
2218c2ecf20Sopenharmony_ci	{
2228c2ecf20Sopenharmony_ci		.compatible = "cirrus,lochnagar2-micvdd",
2238c2ecf20Sopenharmony_ci		.data = &lochnagar_regulators[LOCHNAGAR_MICVDD],
2248c2ecf20Sopenharmony_ci	},
2258c2ecf20Sopenharmony_ci	{
2268c2ecf20Sopenharmony_ci		.compatible = "cirrus,lochnagar2-mic1vdd",
2278c2ecf20Sopenharmony_ci		.data = &lochnagar_regulators[LOCHNAGAR_MIC1VDD],
2288c2ecf20Sopenharmony_ci	},
2298c2ecf20Sopenharmony_ci	{
2308c2ecf20Sopenharmony_ci		.compatible = "cirrus,lochnagar2-mic2vdd",
2318c2ecf20Sopenharmony_ci		.data = &lochnagar_regulators[LOCHNAGAR_MIC2VDD],
2328c2ecf20Sopenharmony_ci	},
2338c2ecf20Sopenharmony_ci	{
2348c2ecf20Sopenharmony_ci		.compatible = "cirrus,lochnagar2-vddcore",
2358c2ecf20Sopenharmony_ci		.data = &lochnagar_regulators[LOCHNAGAR_VDDCORE],
2368c2ecf20Sopenharmony_ci	},
2378c2ecf20Sopenharmony_ci	{}
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lochnagar_of_match);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic int lochnagar_regulator_probe(struct platform_device *pdev)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2448c2ecf20Sopenharmony_ci	struct lochnagar *lochnagar = dev_get_drvdata(dev->parent);
2458c2ecf20Sopenharmony_ci	struct regulator_config config = { };
2468c2ecf20Sopenharmony_ci	const struct of_device_id *of_id;
2478c2ecf20Sopenharmony_ci	const struct regulator_desc *desc;
2488c2ecf20Sopenharmony_ci	struct regulator_dev *rdev;
2498c2ecf20Sopenharmony_ci	int ret;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	config.dev = dev;
2528c2ecf20Sopenharmony_ci	config.regmap = lochnagar->regmap;
2538c2ecf20Sopenharmony_ci	config.driver_data = lochnagar;
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	of_id = of_match_device(lochnagar_of_match, dev);
2568c2ecf20Sopenharmony_ci	if (!of_id)
2578c2ecf20Sopenharmony_ci		return -EINVAL;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	desc = of_id->data;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	rdev = devm_regulator_register(dev, desc, &config);
2628c2ecf20Sopenharmony_ci	if (IS_ERR(rdev)) {
2638c2ecf20Sopenharmony_ci		ret = PTR_ERR(rdev);
2648c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to register %s regulator: %d\n",
2658c2ecf20Sopenharmony_ci			desc->name, ret);
2668c2ecf20Sopenharmony_ci		return ret;
2678c2ecf20Sopenharmony_ci	}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	return 0;
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic struct platform_driver lochnagar_regulator_driver = {
2738c2ecf20Sopenharmony_ci	.driver = {
2748c2ecf20Sopenharmony_ci		.name = "lochnagar-regulator",
2758c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(lochnagar_of_match),
2768c2ecf20Sopenharmony_ci	},
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	.probe = lochnagar_regulator_probe,
2798c2ecf20Sopenharmony_ci};
2808c2ecf20Sopenharmony_cimodule_platform_driver(lochnagar_regulator_driver);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ciMODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
2838c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Regulator driver for Cirrus Logic Lochnagar Board");
2848c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2858c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:lochnagar-regulator");
286