fixed.c 7.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8
/*
 * fixed.c
 *
 * Copyright 2008 Wolfson Microelectronics PLC.
 *
 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 *
9 10 11
 * Copyright (c) 2009 Nokia Corporation
 * Roger Quadros <ext-roger.quadros@nokia.com>
 *
12 13 14 15 16 17 18
 * This is useful for systems with mixed controllable and
 * non-controllable regulators, as well as for allowing testing on
 * systems with no controllable regulators.
 */

#include <linux/err.h>
#include <linux/mutex.h>
19
#include <linux/module.h>
20 21 22
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/fixed.h>
23
#include <linux/gpio/consumer.h>
24
#include <linux/slab.h>
25
#include <linux/of.h>
26
#include <linux/of_device.h>
27 28
#include <linux/regulator/of_regulator.h>
#include <linux/regulator/machine.h>
29 30
#include <linux/clk.h>

31 32 33 34

struct fixed_voltage_data {
	struct regulator_desc desc;
	struct regulator_dev *dev;
35 36 37

	struct clk *enable_clock;
	unsigned int clk_enable_counter;
38 39
};

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
struct fixed_dev_type {
	bool has_enable_clock;
};

static int reg_clock_enable(struct regulator_dev *rdev)
{
	struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
	int ret = 0;

	ret = clk_prepare_enable(priv->enable_clock);
	if (ret)
		return ret;

	priv->clk_enable_counter++;

	return ret;
}

static int reg_clock_disable(struct regulator_dev *rdev)
{
	struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);

	clk_disable_unprepare(priv->enable_clock);
	priv->clk_enable_counter--;

	return 0;
}

static int reg_clock_is_enabled(struct regulator_dev *rdev)
{
	struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);

	return priv->clk_enable_counter > 0;
}

75 76 77 78

/**
 * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
 * @dev: device requesting for fixed_voltage_config
79
 * @desc: regulator description
80 81 82 83 84
 *
 * Populates fixed_voltage_config structure by extracting data from device
 * tree node, returns a pointer to the populated structure of NULL if memory
 * alloc fails.
 */
85
static struct fixed_voltage_config *
86 87
of_get_fixed_voltage_config(struct device *dev,
			    const struct regulator_desc *desc)
88 89 90 91 92 93 94 95
{
	struct fixed_voltage_config *config;
	struct device_node *np = dev->of_node;
	struct regulator_init_data *init_data;

	config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
								 GFP_KERNEL);
	if (!config)
96
		return ERR_PTR(-ENOMEM);
97

98
	config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
99
	if (!config->init_data)
100
		return ERR_PTR(-EINVAL);
101

102
	init_data = config->init_data;
103
	init_data->constraints.apply_uV = 0;
104 105 106 107 108 109 110

	config->supply_name = init_data->constraints.name;
	if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
		config->microvolts = init_data->constraints.min_uV;
	} else {
		dev_err(dev,
			 "Fixed regulator specified with variable voltages\n");
111
		return ERR_PTR(-EINVAL);
112 113 114 115 116
	}

	if (init_data->constraints.boot_on)
		config->enabled_at_boot = true;

117
	of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
P
Peng Fan 已提交
118
	of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
119

120 121 122
	if (of_find_property(np, "vin-supply", NULL))
		config->input_supply = "vin";

123 124 125
	return config;
}

126 127 128
static struct regulator_ops fixed_voltage_ops = {
};

129 130 131 132 133 134
static struct regulator_ops fixed_voltage_clkenabled_ops = {
	.enable = reg_clock_enable,
	.disable = reg_clock_disable,
	.is_enabled = reg_clock_is_enabled,
};

135
static int reg_fixed_voltage_probe(struct platform_device *pdev)
136
{
137
	struct device *dev = &pdev->dev;
138
	struct fixed_voltage_config *config;
139
	struct fixed_voltage_data *drvdata;
140
	const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
141
	struct regulator_config cfg = { };
142
	enum gpiod_flags gflags;
143 144
	int ret;

145 146 147 148 149
	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
			       GFP_KERNEL);
	if (!drvdata)
		return -ENOMEM;

150
	if (pdev->dev.of_node) {
151 152
		config = of_get_fixed_voltage_config(&pdev->dev,
						     &drvdata->desc);
153 154 155
		if (IS_ERR(config))
			return PTR_ERR(config);
	} else {
J
Jingoo Han 已提交
156
		config = dev_get_platdata(&pdev->dev);
157
	}
158 159 160

	if (!config)
		return -ENOMEM;
161

162 163 164
	drvdata->desc.name = devm_kstrdup(&pdev->dev,
					  config->supply_name,
					  GFP_KERNEL);
165
	if (drvdata->desc.name == NULL) {
166
		dev_err(&pdev->dev, "Failed to allocate supply name\n");
167
		return -ENOMEM;
168 169 170
	}
	drvdata->desc.type = REGULATOR_VOLTAGE;
	drvdata->desc.owner = THIS_MODULE;
171

172
	if (drvtype && drvtype->has_enable_clock) {
173 174 175 176
		drvdata->desc.ops = &fixed_voltage_clkenabled_ops;

		drvdata->enable_clock = devm_clk_get(dev, NULL);
		if (IS_ERR(drvdata->enable_clock)) {
177
			dev_err(dev, "Can't get enable-clock from devicetree\n");
178 179 180 181 182
			return -ENOENT;
		}
	} else {
		drvdata->desc.ops = &fixed_voltage_ops;
	}
183

184
	drvdata->desc.enable_time = config->startup_delay;
P
Peng Fan 已提交
185
	drvdata->desc.off_on_delay = config->off_on_delay;
186

187
	if (config->input_supply) {
188 189 190
		drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
					    config->input_supply,
					    GFP_KERNEL);
191 192 193
		if (!drvdata->desc.supply_name) {
			dev_err(&pdev->dev,
				"Failed to allocate input supply\n");
194
			return -ENOMEM;
195 196 197
		}
	}

198 199
	if (config->microvolts)
		drvdata->desc.n_voltages = 1;
200

201
	drvdata->desc.fixed_uV = config->microvolts;
202

203 204
	/*
	 * The signal will be inverted by the GPIO core if flagged so in the
K
Kieran Bingham 已提交
205
	 * descriptor.
206 207 208 209 210
	 */
	if (config->enabled_at_boot)
		gflags = GPIOD_OUT_HIGH;
	else
		gflags = GPIOD_OUT_LOW;
211

212 213 214 215 216 217 218 219 220 221 222 223 224
	/*
	 * Some fixed regulators share the enable line between two
	 * regulators which makes it necessary to get a handle on the
	 * same descriptor for two different consumers. This will get
	 * the GPIO descriptor, but only the first call will initialize
	 * it so any flags such as inversion or open drain will only
	 * be set up by the first caller and assumed identical on the
	 * next caller.
	 *
	 * FIXME: find a better way to deal with this.
	 */
	gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;

225 226 227 228 229
	/*
	 * Do not use devm* here: the regulator core takes over the
	 * lifecycle management of the GPIO descriptor.
	 */
	cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
230 231
	if (IS_ERR(cfg.ena_gpiod))
		return PTR_ERR(cfg.ena_gpiod);
232

233 234 235 236 237
	cfg.dev = &pdev->dev;
	cfg.init_data = config->init_data;
	cfg.driver_data = drvdata;
	cfg.of_node = pdev->dev.of_node;

238 239
	drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
					       &cfg);
240 241
	if (IS_ERR(drvdata->dev)) {
		ret = PTR_ERR(drvdata->dev);
242
		dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
243
		return ret;
244 245 246 247 248
	}

	platform_set_drvdata(pdev, drvdata);

	dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
249
		drvdata->desc.fixed_uV);
250 251 252 253

	return 0;
}

254
#if defined(CONFIG_OF)
255 256 257 258 259 260 261 262
static const struct fixed_dev_type fixed_voltage_data = {
	.has_enable_clock = false,
};

static const struct fixed_dev_type fixed_clkenable_data = {
	.has_enable_clock = true,
};

263
static const struct of_device_id fixed_of_match[] = {
264 265 266 267 268 269 270 271 272 273
	{
		.compatible = "regulator-fixed",
		.data = &fixed_voltage_data,
	},
	{
		.compatible = "regulator-fixed-clock",
		.data = &fixed_clkenable_data,
	},
	{
	},
274 275 276 277
};
MODULE_DEVICE_TABLE(of, fixed_of_match);
#endif

278
static struct platform_driver regulator_fixed_voltage_driver = {
279
	.probe		= reg_fixed_voltage_probe,
280 281
	.driver		= {
		.name		= "reg-fixed-voltage",
282
		.of_match_table = of_match_ptr(fixed_of_match),
283 284 285 286 287 288 289
	},
};

static int __init regulator_fixed_voltage_init(void)
{
	return platform_driver_register(&regulator_fixed_voltage_driver);
}
290
subsys_initcall(regulator_fixed_voltage_init);
291 292 293 294 295 296 297 298 299 300

static void __exit regulator_fixed_voltage_exit(void)
{
	platform_driver_unregister(&regulator_fixed_voltage_driver);
}
module_exit(regulator_fixed_voltage_exit);

MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
MODULE_DESCRIPTION("Fixed voltage regulator");
MODULE_LICENSE("GPL");
301
MODULE_ALIAS("platform:reg-fixed-voltage");