xref: /linux/drivers/regulator/lochnagar-regulator.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1bef9391cSCharles Keepax // SPDX-License-Identifier: GPL-2.0
2d8b2a8e9SCharles Keepax //
3d8b2a8e9SCharles Keepax // Lochnagar regulator driver
4d8b2a8e9SCharles Keepax //
5d8b2a8e9SCharles Keepax // Copyright (c) 2017-2018 Cirrus Logic, Inc. and
6d8b2a8e9SCharles Keepax //                         Cirrus Logic International Semiconductor Ltd.
7d8b2a8e9SCharles Keepax //
8d8b2a8e9SCharles Keepax // Author: Charles Keepax <ckeepax@opensource.cirrus.com>
9bef9391cSCharles Keepax 
10bef9391cSCharles Keepax #include <linux/bitops.h>
11bef9391cSCharles Keepax #include <linux/device.h>
12bef9391cSCharles Keepax #include <linux/err.h>
13bef9391cSCharles Keepax #include <linux/module.h>
14bef9391cSCharles Keepax #include <linux/mutex.h>
15bef9391cSCharles Keepax #include <linux/of.h>
16bef9391cSCharles Keepax #include <linux/platform_device.h>
17bef9391cSCharles Keepax #include <linux/regmap.h>
18bef9391cSCharles Keepax #include <linux/regulator/driver.h>
19bef9391cSCharles Keepax #include <linux/regulator/machine.h>
20bef9391cSCharles Keepax #include <linux/regulator/of_regulator.h>
21bef9391cSCharles Keepax 
22bef9391cSCharles Keepax #include <linux/mfd/lochnagar.h>
23fa2bb8b9SCharles Keepax #include <linux/mfd/lochnagar1_regs.h>
24fa2bb8b9SCharles Keepax #include <linux/mfd/lochnagar2_regs.h>
25bef9391cSCharles Keepax 
26bef9391cSCharles Keepax static const struct regulator_ops lochnagar_micvdd_ops = {
27bef9391cSCharles Keepax 	.enable = regulator_enable_regmap,
28bef9391cSCharles Keepax 	.disable = regulator_disable_regmap,
29bef9391cSCharles Keepax 	.is_enabled = regulator_is_enabled_regmap,
30bef9391cSCharles Keepax 
31bef9391cSCharles Keepax 	.list_voltage = regulator_list_voltage_linear_range,
32bef9391cSCharles Keepax 	.map_voltage = regulator_map_voltage_linear_range,
33bef9391cSCharles Keepax 
34bef9391cSCharles Keepax 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
35bef9391cSCharles Keepax 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
36bef9391cSCharles Keepax };
37bef9391cSCharles Keepax 
3860ab7f41SMatti Vaittinen static const struct linear_range lochnagar_micvdd_ranges[] = {
39bef9391cSCharles Keepax 	REGULATOR_LINEAR_RANGE(1000000, 0,    0xC, 50000),
40bef9391cSCharles Keepax 	REGULATOR_LINEAR_RANGE(1700000, 0xD, 0x1F, 100000),
41bef9391cSCharles Keepax };
42bef9391cSCharles Keepax 
lochnagar_micbias_enable(struct regulator_dev * rdev)43bef9391cSCharles Keepax static int lochnagar_micbias_enable(struct regulator_dev *rdev)
44bef9391cSCharles Keepax {
45bef9391cSCharles Keepax 	struct lochnagar *lochnagar = rdev_get_drvdata(rdev);
46bef9391cSCharles Keepax 	int ret;
47bef9391cSCharles Keepax 
48bef9391cSCharles Keepax 	mutex_lock(&lochnagar->analogue_config_lock);
49bef9391cSCharles Keepax 
50bef9391cSCharles Keepax 	ret = regulator_enable_regmap(rdev);
51bef9391cSCharles Keepax 	if (ret < 0)
52bef9391cSCharles Keepax 		goto err;
53bef9391cSCharles Keepax 
54bef9391cSCharles Keepax 	ret = lochnagar_update_config(lochnagar);
55bef9391cSCharles Keepax 
56bef9391cSCharles Keepax err:
57bef9391cSCharles Keepax 	mutex_unlock(&lochnagar->analogue_config_lock);
58bef9391cSCharles Keepax 
59bef9391cSCharles Keepax 	return ret;
60bef9391cSCharles Keepax }
61bef9391cSCharles Keepax 
lochnagar_micbias_disable(struct regulator_dev * rdev)62bef9391cSCharles Keepax static int lochnagar_micbias_disable(struct regulator_dev *rdev)
63bef9391cSCharles Keepax {
64bef9391cSCharles Keepax 	struct lochnagar *lochnagar = rdev_get_drvdata(rdev);
65bef9391cSCharles Keepax 	int ret;
66bef9391cSCharles Keepax 
67bef9391cSCharles Keepax 	mutex_lock(&lochnagar->analogue_config_lock);
68bef9391cSCharles Keepax 
69bef9391cSCharles Keepax 	ret = regulator_disable_regmap(rdev);
70bef9391cSCharles Keepax 	if (ret < 0)
71bef9391cSCharles Keepax 		goto err;
72bef9391cSCharles Keepax 
73bef9391cSCharles Keepax 	ret = lochnagar_update_config(lochnagar);
74bef9391cSCharles Keepax 
75bef9391cSCharles Keepax err:
76bef9391cSCharles Keepax 	mutex_unlock(&lochnagar->analogue_config_lock);
77bef9391cSCharles Keepax 
78bef9391cSCharles Keepax 	return ret;
79bef9391cSCharles Keepax }
80bef9391cSCharles Keepax 
81bef9391cSCharles Keepax static const struct regulator_ops lochnagar_micbias_ops = {
82bef9391cSCharles Keepax 	.enable = lochnagar_micbias_enable,
83bef9391cSCharles Keepax 	.disable = lochnagar_micbias_disable,
84bef9391cSCharles Keepax 	.is_enabled = regulator_is_enabled_regmap,
85bef9391cSCharles Keepax };
86bef9391cSCharles Keepax 
87bef9391cSCharles Keepax static const struct regulator_ops lochnagar_vddcore_ops = {
88bef9391cSCharles Keepax 	.enable = regulator_enable_regmap,
89bef9391cSCharles Keepax 	.disable = regulator_disable_regmap,
90bef9391cSCharles Keepax 	.is_enabled = regulator_is_enabled_regmap,
91bef9391cSCharles Keepax 
92bef9391cSCharles Keepax 	.list_voltage = regulator_list_voltage_linear_range,
93bef9391cSCharles Keepax 	.map_voltage = regulator_map_voltage_linear_range,
94bef9391cSCharles Keepax 
95bef9391cSCharles Keepax 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
96bef9391cSCharles Keepax 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
97bef9391cSCharles Keepax };
98bef9391cSCharles Keepax 
9960ab7f41SMatti Vaittinen static const struct linear_range lochnagar_vddcore_ranges[] = {
1006dc9674dSCharles Keepax 	REGULATOR_LINEAR_RANGE(600000, 0,    0x7, 0),
101bef9391cSCharles Keepax 	REGULATOR_LINEAR_RANGE(600000, 0x8, 0x41, 12500),
102bef9391cSCharles Keepax };
103bef9391cSCharles Keepax 
104bef9391cSCharles Keepax enum lochnagar_regulators {
105bef9391cSCharles Keepax 	LOCHNAGAR_MICVDD,
106bef9391cSCharles Keepax 	LOCHNAGAR_MIC1VDD,
107bef9391cSCharles Keepax 	LOCHNAGAR_MIC2VDD,
108bef9391cSCharles Keepax 	LOCHNAGAR_VDDCORE,
109bef9391cSCharles Keepax };
110bef9391cSCharles Keepax 
lochnagar_micbias_of_parse(struct device_node * np,const struct regulator_desc * desc,struct regulator_config * config)111bef9391cSCharles Keepax static int lochnagar_micbias_of_parse(struct device_node *np,
112bef9391cSCharles Keepax 				      const struct regulator_desc *desc,
113bef9391cSCharles Keepax 				      struct regulator_config *config)
114bef9391cSCharles Keepax {
115bef9391cSCharles Keepax 	struct lochnagar *lochnagar = config->driver_data;
116bef9391cSCharles Keepax 	int shift = (desc->id - LOCHNAGAR_MIC1VDD) *
117bef9391cSCharles Keepax 		    LOCHNAGAR2_P2_MICBIAS_SRC_SHIFT;
118bef9391cSCharles Keepax 	int mask = LOCHNAGAR2_P1_MICBIAS_SRC_MASK << shift;
119bef9391cSCharles Keepax 	unsigned int val;
120bef9391cSCharles Keepax 	int ret;
121bef9391cSCharles Keepax 
122bef9391cSCharles Keepax 	ret = of_property_read_u32(np, "cirrus,micbias-input", &val);
123bef9391cSCharles Keepax 	if (ret >= 0) {
124bef9391cSCharles Keepax 		mutex_lock(&lochnagar->analogue_config_lock);
125bef9391cSCharles Keepax 		ret = regmap_update_bits(lochnagar->regmap,
126bef9391cSCharles Keepax 					 LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
127bef9391cSCharles Keepax 					 mask, val << shift);
128bef9391cSCharles Keepax 		mutex_unlock(&lochnagar->analogue_config_lock);
129bef9391cSCharles Keepax 		if (ret < 0) {
130bef9391cSCharles Keepax 			dev_err(lochnagar->dev,
131bef9391cSCharles Keepax 				"Failed to update micbias source: %d\n", ret);
132bef9391cSCharles Keepax 			return ret;
133bef9391cSCharles Keepax 		}
134bef9391cSCharles Keepax 	}
135bef9391cSCharles Keepax 
136bef9391cSCharles Keepax 	return 0;
137bef9391cSCharles Keepax }
138bef9391cSCharles Keepax 
139bef9391cSCharles Keepax static const struct regulator_desc lochnagar_regulators[] = {
140bef9391cSCharles Keepax 	[LOCHNAGAR_MICVDD] = {
141bef9391cSCharles Keepax 		.name = "MICVDD",
142bef9391cSCharles Keepax 		.supply_name = "SYSVDD",
143bef9391cSCharles Keepax 		.type = REGULATOR_VOLTAGE,
144bef9391cSCharles Keepax 		.n_voltages = 32,
145bef9391cSCharles Keepax 		.ops = &lochnagar_micvdd_ops,
146bef9391cSCharles Keepax 
147bef9391cSCharles Keepax 		.id = LOCHNAGAR_MICVDD,
148bef9391cSCharles Keepax 		.of_match = of_match_ptr("MICVDD"),
149bef9391cSCharles Keepax 
150bef9391cSCharles Keepax 		.enable_reg = LOCHNAGAR2_MICVDD_CTRL1,
151bef9391cSCharles Keepax 		.enable_mask = LOCHNAGAR2_MICVDD_REG_ENA_MASK,
152bef9391cSCharles Keepax 		.vsel_reg = LOCHNAGAR2_MICVDD_CTRL2,
153bef9391cSCharles Keepax 		.vsel_mask = LOCHNAGAR2_MICVDD_VSEL_MASK,
154bef9391cSCharles Keepax 
155bef9391cSCharles Keepax 		.linear_ranges = lochnagar_micvdd_ranges,
156bef9391cSCharles Keepax 		.n_linear_ranges = ARRAY_SIZE(lochnagar_micvdd_ranges),
157bef9391cSCharles Keepax 
158bef9391cSCharles Keepax 		.enable_time = 3000,
159bef9391cSCharles Keepax 		.ramp_delay = 1000,
160bef9391cSCharles Keepax 
161bef9391cSCharles Keepax 		.owner = THIS_MODULE,
162bef9391cSCharles Keepax 	},
163bef9391cSCharles Keepax 	[LOCHNAGAR_MIC1VDD] = {
164bef9391cSCharles Keepax 		.name = "MIC1VDD",
165bef9391cSCharles Keepax 		.supply_name = "MICBIAS1",
166bef9391cSCharles Keepax 		.type = REGULATOR_VOLTAGE,
167bef9391cSCharles Keepax 		.ops = &lochnagar_micbias_ops,
168bef9391cSCharles Keepax 
169bef9391cSCharles Keepax 		.id = LOCHNAGAR_MIC1VDD,
170bef9391cSCharles Keepax 		.of_match = of_match_ptr("MIC1VDD"),
171bef9391cSCharles Keepax 		.of_parse_cb = lochnagar_micbias_of_parse,
172bef9391cSCharles Keepax 
173bef9391cSCharles Keepax 		.enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
174bef9391cSCharles Keepax 		.enable_mask = LOCHNAGAR2_P1_INPUT_BIAS_ENA_MASK,
175bef9391cSCharles Keepax 
176bef9391cSCharles Keepax 		.owner = THIS_MODULE,
177bef9391cSCharles Keepax 	},
178bef9391cSCharles Keepax 	[LOCHNAGAR_MIC2VDD] = {
179bef9391cSCharles Keepax 		.name = "MIC2VDD",
180bef9391cSCharles Keepax 		.supply_name = "MICBIAS2",
181bef9391cSCharles Keepax 		.type = REGULATOR_VOLTAGE,
182bef9391cSCharles Keepax 		.ops = &lochnagar_micbias_ops,
183bef9391cSCharles Keepax 
184bef9391cSCharles Keepax 		.id = LOCHNAGAR_MIC2VDD,
185bef9391cSCharles Keepax 		.of_match = of_match_ptr("MIC2VDD"),
186bef9391cSCharles Keepax 		.of_parse_cb = lochnagar_micbias_of_parse,
187bef9391cSCharles Keepax 
188bef9391cSCharles Keepax 		.enable_reg = LOCHNAGAR2_ANALOGUE_PATH_CTRL2,
189bef9391cSCharles Keepax 		.enable_mask = LOCHNAGAR2_P2_INPUT_BIAS_ENA_MASK,
190bef9391cSCharles Keepax 
191bef9391cSCharles Keepax 		.owner = THIS_MODULE,
192bef9391cSCharles Keepax 	},
193bef9391cSCharles Keepax 	[LOCHNAGAR_VDDCORE] = {
194bef9391cSCharles Keepax 		.name = "VDDCORE",
195bef9391cSCharles Keepax 		.supply_name = "SYSVDD",
196bef9391cSCharles Keepax 		.type = REGULATOR_VOLTAGE,
1979df3bb31SAxel Lin 		.n_voltages = 66,
198bef9391cSCharles Keepax 		.ops = &lochnagar_vddcore_ops,
199bef9391cSCharles Keepax 
200bef9391cSCharles Keepax 		.id = LOCHNAGAR_VDDCORE,
201bef9391cSCharles Keepax 		.of_match = of_match_ptr("VDDCORE"),
202bef9391cSCharles Keepax 
203bef9391cSCharles Keepax 		.enable_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL1,
204bef9391cSCharles Keepax 		.enable_mask = LOCHNAGAR2_VDDCORE_CDC_REG_ENA_MASK,
205bef9391cSCharles Keepax 		.vsel_reg = LOCHNAGAR2_VDDCORE_CDC_CTRL2,
206bef9391cSCharles Keepax 		.vsel_mask = LOCHNAGAR2_VDDCORE_CDC_VSEL_MASK,
207bef9391cSCharles Keepax 
208bef9391cSCharles Keepax 		.linear_ranges = lochnagar_vddcore_ranges,
209bef9391cSCharles Keepax 		.n_linear_ranges = ARRAY_SIZE(lochnagar_vddcore_ranges),
210bef9391cSCharles Keepax 
211bef9391cSCharles Keepax 		.enable_time = 3000,
212bef9391cSCharles Keepax 		.ramp_delay = 1000,
213f75841aaSCharles Keepax 		.off_on_delay = 15000,
214bef9391cSCharles Keepax 
215bef9391cSCharles Keepax 		.owner = THIS_MODULE,
216bef9391cSCharles Keepax 	},
217bef9391cSCharles Keepax };
218bef9391cSCharles Keepax 
219d90acbc4SCharles Keepax static const struct of_device_id lochnagar_of_match[] = {
220d90acbc4SCharles Keepax 	{
221d90acbc4SCharles Keepax 		.compatible = "cirrus,lochnagar2-micvdd",
222d90acbc4SCharles Keepax 		.data = &lochnagar_regulators[LOCHNAGAR_MICVDD],
223d90acbc4SCharles Keepax 	},
224d90acbc4SCharles Keepax 	{
225d90acbc4SCharles Keepax 		.compatible = "cirrus,lochnagar2-mic1vdd",
226d90acbc4SCharles Keepax 		.data = &lochnagar_regulators[LOCHNAGAR_MIC1VDD],
227d90acbc4SCharles Keepax 	},
228d90acbc4SCharles Keepax 	{
229d90acbc4SCharles Keepax 		.compatible = "cirrus,lochnagar2-mic2vdd",
2304cac31e2SAxel Lin 		.data = &lochnagar_regulators[LOCHNAGAR_MIC2VDD],
231d90acbc4SCharles Keepax 	},
232d90acbc4SCharles Keepax 	{
233d90acbc4SCharles Keepax 		.compatible = "cirrus,lochnagar2-vddcore",
234d90acbc4SCharles Keepax 		.data = &lochnagar_regulators[LOCHNAGAR_VDDCORE],
235d90acbc4SCharles Keepax 	},
236692f8b56SCharles Keepax 	{}
237d90acbc4SCharles Keepax };
238692f8b56SCharles Keepax MODULE_DEVICE_TABLE(of, lochnagar_of_match);
239d90acbc4SCharles Keepax 
lochnagar_regulator_probe(struct platform_device * pdev)240bef9391cSCharles Keepax static int lochnagar_regulator_probe(struct platform_device *pdev)
241bef9391cSCharles Keepax {
242bef9391cSCharles Keepax 	struct device *dev = &pdev->dev;
243bef9391cSCharles Keepax 	struct lochnagar *lochnagar = dev_get_drvdata(dev->parent);
244bef9391cSCharles Keepax 	struct regulator_config config = { };
245d90acbc4SCharles Keepax 	const struct regulator_desc *desc;
246bef9391cSCharles Keepax 	struct regulator_dev *rdev;
247d90acbc4SCharles Keepax 	int ret;
248bef9391cSCharles Keepax 
249d90acbc4SCharles Keepax 	config.dev = dev;
250bef9391cSCharles Keepax 	config.regmap = lochnagar->regmap;
251bef9391cSCharles Keepax 	config.driver_data = lochnagar;
252bef9391cSCharles Keepax 
253*8f7e17d8SRob Herring 	desc = device_get_match_data(dev);
254*8f7e17d8SRob Herring 	if (!desc)
255d90acbc4SCharles Keepax 		return -EINVAL;
256d90acbc4SCharles Keepax 
257bef9391cSCharles Keepax 	rdev = devm_regulator_register(dev, desc, &config);
258bef9391cSCharles Keepax 	if (IS_ERR(rdev)) {
259bef9391cSCharles Keepax 		ret = PTR_ERR(rdev);
260bef9391cSCharles Keepax 		dev_err(dev, "Failed to register %s regulator: %d\n",
261bef9391cSCharles Keepax 			desc->name, ret);
262bef9391cSCharles Keepax 		return ret;
263bef9391cSCharles Keepax 	}
264bef9391cSCharles Keepax 
265bef9391cSCharles Keepax 	return 0;
266bef9391cSCharles Keepax }
267bef9391cSCharles Keepax 
268bef9391cSCharles Keepax static struct platform_driver lochnagar_regulator_driver = {
269bef9391cSCharles Keepax 	.driver = {
270bef9391cSCharles Keepax 		.name = "lochnagar-regulator",
271d3b81d97SDouglas Anderson 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
272d90acbc4SCharles Keepax 		.of_match_table = of_match_ptr(lochnagar_of_match),
273bef9391cSCharles Keepax 	},
274bef9391cSCharles Keepax 
275bef9391cSCharles Keepax 	.probe = lochnagar_regulator_probe,
276bef9391cSCharles Keepax };
277bef9391cSCharles Keepax module_platform_driver(lochnagar_regulator_driver);
278bef9391cSCharles Keepax 
279bef9391cSCharles Keepax MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
280bef9391cSCharles Keepax MODULE_DESCRIPTION("Regulator driver for Cirrus Logic Lochnagar Board");
281bef9391cSCharles Keepax MODULE_LICENSE("GPL v2");
282bef9391cSCharles Keepax MODULE_ALIAS("platform:lochnagar-regulator");
283