Blame view

drivers/regulator/fixed.c 6.58 KB
4b74ff651   Mark Brown   regulator: add su...
1
2
3
4
5
6
7
  /*
   * fixed.c
   *
   * Copyright 2008 Wolfson Microelectronics PLC.
   *
   * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
   *
86d9884b6   Roger Quadros   regulator: Add GP...
8
9
10
   * Copyright (c) 2009 Nokia Corporation
   * Roger Quadros <ext-roger.quadros@nokia.com>
   *
4b74ff651   Mark Brown   regulator: add su...
11
12
13
14
15
16
17
18
19
20
21
22
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2 of the
   * License, or (at your option) any later version.
   *
   * 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>
65602c32e   Paul Gortmaker   regulator: Add mo...
23
  #include <linux/module.h>
4b74ff651   Mark Brown   regulator: add su...
24
25
26
  #include <linux/platform_device.h>
  #include <linux/regulator/driver.h>
  #include <linux/regulator/fixed.h>
86d9884b6   Roger Quadros   regulator: Add GP...
27
  #include <linux/gpio.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
28
  #include <linux/slab.h>
cef49102c   Rajendra Nayak   regulator: adapt ...
29
30
31
32
  #include <linux/of.h>
  #include <linux/of_gpio.h>
  #include <linux/regulator/of_regulator.h>
  #include <linux/regulator/machine.h>
0b164cf7c   Peter Chen   MLK-13638-4 regul...
33
  #include <linux/pinctrl/consumer.h>
4b74ff651   Mark Brown   regulator: add su...
34
35
36
37
  
  struct fixed_voltage_data {
  	struct regulator_desc desc;
  	struct regulator_dev *dev;
4b74ff651   Mark Brown   regulator: add su...
38
  };
cef49102c   Rajendra Nayak   regulator: adapt ...
39
40
41
42
  
  /**
   * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
   * @dev: device requesting for fixed_voltage_config
072e78b12   Javier Martinez Canillas   regulator: of: Ad...
43
   * @desc: regulator description
cef49102c   Rajendra Nayak   regulator: adapt ...
44
45
46
47
48
   *
   * 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.
   */
bc91396b0   Axel Lin   regulator: Static...
49
  static struct fixed_voltage_config *
072e78b12   Javier Martinez Canillas   regulator: of: Ad...
50
51
  of_get_fixed_voltage_config(struct device *dev,
  			    const struct regulator_desc *desc)
cef49102c   Rajendra Nayak   regulator: adapt ...
52
53
54
  {
  	struct fixed_voltage_config *config;
  	struct device_node *np = dev->of_node;
cef49102c   Rajendra Nayak   regulator: adapt ...
55
56
57
58
59
  	struct regulator_init_data *init_data;
  
  	config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  								 GFP_KERNEL);
  	if (!config)
f141822b1   Stephen Warren   regulator: fixed:...
60
  		return ERR_PTR(-ENOMEM);
cef49102c   Rajendra Nayak   regulator: adapt ...
61

072e78b12   Javier Martinez Canillas   regulator: of: Ad...
62
  	config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
4b864af1a   Axel Lin   regulator: Checki...
63
  	if (!config->init_data)
f141822b1   Stephen Warren   regulator: fixed:...
64
  		return ERR_PTR(-EINVAL);
4b864af1a   Axel Lin   regulator: Checki...
65

cef49102c   Rajendra Nayak   regulator: adapt ...
66
  	init_data = config->init_data;
0c437c4ae   Richard Zhao   regulator: set co...
67
  	init_data->constraints.apply_uV = 0;
cef49102c   Rajendra Nayak   regulator: adapt ...
68
69
70
71
72
73
74
75
  
  	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
  ");
f141822b1   Stephen Warren   regulator: fixed:...
76
  		return ERR_PTR(-EINVAL);
cef49102c   Rajendra Nayak   regulator: adapt ...
77
78
79
80
81
82
  	}
  
  	if (init_data->constraints.boot_on)
  		config->enabled_at_boot = true;
  
  	config->gpio = of_get_named_gpio(np, "gpio", 0);
f5a6d3516   Laxman Dewangan   regulator: fixed:...
83
84
  	if ((config->gpio < 0) && (config->gpio != -ENOENT))
  		return ERR_PTR(config->gpio);
f141822b1   Stephen Warren   regulator: fixed:...
85

4127f696f   Sergei Shtylyov   regulator: fixed:...
86
  	of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
969a46385   Haibo Chen   MLK-14638-1 regul...
87
  	of_property_read_u32(np, "off-on-delay", &config->off_on_delay);
cef49102c   Rajendra Nayak   regulator: adapt ...
88

4127f696f   Sergei Shtylyov   regulator: fixed:...
89
90
91
  	config->enable_high = of_property_read_bool(np, "enable-active-high");
  	config->gpio_is_open_drain = of_property_read_bool(np,
  							   "gpio-open-drain");
9a50dba50   Laxman Dewangan   regulator: fixed:...
92

6be5bfc3b   Laxman Dewangan   regulator: fixed:...
93
94
  	if (of_find_property(np, "vin-supply", NULL))
  		config->input_supply = "vin";
cef49102c   Rajendra Nayak   regulator: adapt ...
95
96
  	return config;
  }
9d442061d   Mark Brown   regulator: fixed:...
97
  static struct regulator_ops fixed_voltage_ops = {
9d442061d   Mark Brown   regulator: fixed:...
98
  };
a5023574d   Bill Pemberton   regulator: remove...
99
  static int reg_fixed_voltage_probe(struct platform_device *pdev)
4b74ff651   Mark Brown   regulator: add su...
100
  {
22d881c06   Axel Lin   regulator: Avoid ...
101
  	struct fixed_voltage_config *config;
4b74ff651   Mark Brown   regulator: add su...
102
  	struct fixed_voltage_data *drvdata;
c172708d3   Mark Brown   regulator: core: ...
103
  	struct regulator_config cfg = { };
4b74ff651   Mark Brown   regulator: add su...
104
  	int ret;
072e78b12   Javier Martinez Canillas   regulator: of: Ad...
105
106
107
108
  	drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  			       GFP_KERNEL);
  	if (!drvdata)
  		return -ENOMEM;
f141822b1   Stephen Warren   regulator: fixed:...
109
  	if (pdev->dev.of_node) {
072e78b12   Javier Martinez Canillas   regulator: of: Ad...
110
111
  		config = of_get_fixed_voltage_config(&pdev->dev,
  						     &drvdata->desc);
f141822b1   Stephen Warren   regulator: fixed:...
112
113
114
  		if (IS_ERR(config))
  			return PTR_ERR(config);
  	} else {
dff91d0b7   Jingoo Han   regulator: use de...
115
  		config = dev_get_platdata(&pdev->dev);
f141822b1   Stephen Warren   regulator: fixed:...
116
  	}
22d881c06   Axel Lin   regulator: Avoid ...
117
118
119
  
  	if (!config)
  		return -ENOMEM;
cef49102c   Rajendra Nayak   regulator: adapt ...
120

84d0ffbe2   Manish Badarkhe   regulator: fixed:...
121
122
123
  	drvdata->desc.name = devm_kstrdup(&pdev->dev,
  					  config->supply_name,
  					  GFP_KERNEL);
4b74ff651   Mark Brown   regulator: add su...
124
  	if (drvdata->desc.name == NULL) {
c53ad7fe5   Mark Brown   regulator: More e...
125
126
  		dev_err(&pdev->dev, "Failed to allocate supply name
  ");
84d0ffbe2   Manish Badarkhe   regulator: fixed:...
127
  		return -ENOMEM;
4b74ff651   Mark Brown   regulator: add su...
128
129
130
  	}
  	drvdata->desc.type = REGULATOR_VOLTAGE;
  	drvdata->desc.owner = THIS_MODULE;
25a53dfbf   Mark Brown   regulator: fixed:...
131
  	drvdata->desc.ops = &fixed_voltage_ops;
1c37f8a83   Sascha Hauer   regulator fixed: ...
132

3d0f267fc   Mark Brown   regulator: fixed:...
133
  	drvdata->desc.enable_time = config->startup_delay;
969a46385   Haibo Chen   MLK-14638-1 regul...
134
  	drvdata->desc.off_on_delay = config->off_on_delay;
3d0f267fc   Mark Brown   regulator: fixed:...
135

6be5bfc3b   Laxman Dewangan   regulator: fixed:...
136
  	if (config->input_supply) {
84d0ffbe2   Manish Badarkhe   regulator: fixed:...
137
138
139
  		drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
  					    config->input_supply,
  					    GFP_KERNEL);
6be5bfc3b   Laxman Dewangan   regulator: fixed:...
140
141
142
143
  		if (!drvdata->desc.supply_name) {
  			dev_err(&pdev->dev,
  				"Failed to allocate input supply
  ");
84d0ffbe2   Manish Badarkhe   regulator: fixed:...
144
  			return -ENOMEM;
6be5bfc3b   Laxman Dewangan   regulator: fixed:...
145
146
  		}
  	}
1c37f8a83   Sascha Hauer   regulator fixed: ...
147
148
  	if (config->microvolts)
  		drvdata->desc.n_voltages = 1;
4b74ff651   Mark Brown   regulator: add su...
149

c368e5fc2   Laxman Dewangan   regulator: fixed:...
150
  	drvdata->desc.fixed_uV = config->microvolts;
9d442061d   Mark Brown   regulator: fixed:...
151

5315fe2f8   Markus Pargmann   regulator: fixed:...
152
  	if (gpio_is_valid(config->gpio)) {
25a53dfbf   Mark Brown   regulator: fixed:...
153
  		cfg.ena_gpio = config->gpio;
1de3821ac   Markus Pargmann   regulator: Set en...
154
155
156
  		if (pdev->dev.of_node)
  			cfg.ena_gpio_initialized = true;
  	}
25a53dfbf   Mark Brown   regulator: fixed:...
157
158
  	cfg.ena_gpio_invert = !config->enable_high;
  	if (config->enabled_at_boot) {
609d5f6dd   Jingoo Han   regulator: fixed:...
159
  		if (config->enable_high)
25a53dfbf   Mark Brown   regulator: fixed:...
160
  			cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
609d5f6dd   Jingoo Han   regulator: fixed:...
161
  		else
25a53dfbf   Mark Brown   regulator: fixed:...
162
  			cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
86d9884b6   Roger Quadros   regulator: Add GP...
163
  	} else {
609d5f6dd   Jingoo Han   regulator: fixed:...
164
  		if (config->enable_high)
25a53dfbf   Mark Brown   regulator: fixed:...
165
  			cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
609d5f6dd   Jingoo Han   regulator: fixed:...
166
  		else
25a53dfbf   Mark Brown   regulator: fixed:...
167
  			cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
86d9884b6   Roger Quadros   regulator: Add GP...
168
  	}
25a53dfbf   Mark Brown   regulator: fixed:...
169
170
  	if (config->gpio_is_open_drain)
  		cfg.ena_gpio_flags |= GPIOF_OPEN_DRAIN;
4b74ff651   Mark Brown   regulator: add su...
171

c172708d3   Mark Brown   regulator: core: ...
172
173
174
175
  	cfg.dev = &pdev->dev;
  	cfg.init_data = config->init_data;
  	cfg.driver_data = drvdata;
  	cfg.of_node = pdev->dev.of_node;
84d0ffbe2   Manish Badarkhe   regulator: fixed:...
176
177
  	drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
  					       &cfg);
4b74ff651   Mark Brown   regulator: add su...
178
179
  	if (IS_ERR(drvdata->dev)) {
  		ret = PTR_ERR(drvdata->dev);
c53ad7fe5   Mark Brown   regulator: More e...
180
181
  		dev_err(&pdev->dev, "Failed to register regulator: %d
  ", ret);
84d0ffbe2   Manish Badarkhe   regulator: fixed:...
182
  		return ret;
4b74ff651   Mark Brown   regulator: add su...
183
184
185
186
187
188
  	}
  
  	platform_set_drvdata(pdev, drvdata);
  
  	dev_dbg(&pdev->dev, "%s supplying %duV
  ", drvdata->desc.name,
c368e5fc2   Laxman Dewangan   regulator: fixed:...
189
  		drvdata->desc.fixed_uV);
4b74ff651   Mark Brown   regulator: add su...
190
191
  
  	return 0;
4b74ff651   Mark Brown   regulator: add su...
192
  }
cef49102c   Rajendra Nayak   regulator: adapt ...
193
  #if defined(CONFIG_OF)
3d68dfe32   Greg Kroah-Hartman   Drivers: regulato...
194
  static const struct of_device_id fixed_of_match[] = {
cef49102c   Rajendra Nayak   regulator: adapt ...
195
196
197
198
  	{ .compatible = "regulator-fixed", },
  	{},
  };
  MODULE_DEVICE_TABLE(of, fixed_of_match);
cef49102c   Rajendra Nayak   regulator: adapt ...
199
  #endif
0b164cf7c   Peter Chen   MLK-13638-4 regul...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  #ifdef CONFIG_PM_SLEEP
  static int reg_fixed_voltage_suspend(struct device *dev)
  {
  	pinctrl_pm_select_sleep_state(dev);
  
  	return 0;
  }
  static int reg_fixed_voltage_resume(struct device *dev)
  {
  	pinctrl_pm_select_default_state(dev);
  
  	return 0;
  }
  #endif
  
  static const struct dev_pm_ops reg_fixed_voltage_pm_ops = {
  	SET_LATE_SYSTEM_SLEEP_PM_OPS(reg_fixed_voltage_suspend,
  		reg_fixed_voltage_resume)
  };
4b74ff651   Mark Brown   regulator: add su...
219
  static struct platform_driver regulator_fixed_voltage_driver = {
8ab3343dc   Dmitry Torokhov   Regulators: fixed...
220
  	.probe		= reg_fixed_voltage_probe,
4b74ff651   Mark Brown   regulator: add su...
221
222
  	.driver		= {
  		.name		= "reg-fixed-voltage",
abcfaf23c   Axel Lin   regulator: fixed:...
223
  		.of_match_table = of_match_ptr(fixed_of_match),
0b164cf7c   Peter Chen   MLK-13638-4 regul...
224
  		.pm = &reg_fixed_voltage_pm_ops,
4b74ff651   Mark Brown   regulator: add su...
225
226
227
228
229
230
231
  	},
  };
  
  static int __init regulator_fixed_voltage_init(void)
  {
  	return platform_driver_register(&regulator_fixed_voltage_driver);
  }
5a1b22bee   Mark Brown   regulator: Move r...
232
  subsys_initcall(regulator_fixed_voltage_init);
4b74ff651   Mark Brown   regulator: add su...
233
234
235
236
237
238
239
240
241
242
  
  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");
38c53c891   Mark Brown   regulator: Set MO...
243
  MODULE_ALIAS("platform:reg-fixed-voltage");