aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/regulator/gpio-switch-regulator.c
blob: 55dd63675a06e374b8a307e59b9ce2a74451db26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * driver/regulator/gpio-switch-regulator.c
 * GPIO based switch regulator to enable/disable power rails.
 *
 * Copyright (c) 2011, NVIDIA Corporation.
 *
 * 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 program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/*#define DEBUG			1*/
/*#define VERBOSE_DEBUG		1*/

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/gpio.h>
#include <linux/regulator/gpio-switch-regulator.h>

struct gpio_switch_regulator {
	struct regulator_desc		reg_desc;
	struct regulator_init_data	reg_init_data;
	struct regulator		*input_regulator;
	struct regulator_dev		*rdev;
	struct device			*dev;
	int				gpio_nr;
	int				pin_group;
	bool				is_gpio_init;
	bool				is_enable;
	bool				active_low;
	bool				is_init_success;
	int				*voltages;
	unsigned			curr_vol_sel;
	struct gpio_switch_regulator_subdev_data *psubdev_data;
	int (*enable_rail)(struct gpio_switch_regulator_subdev_data *sdata);
	int (*disable_rail)(struct gpio_switch_regulator_subdev_data *sdata);
};

static int _gpio_regulator_enable(struct device *dev,
		struct gpio_switch_regulator *ri)
{
	int init_val;
	int ret;

	if (ri->enable_rail) {
		ret = ri->enable_rail(ri->psubdev_data);
		if (ret < 0)
			dev_err(dev, "Unable to enable rail through board api"
			" error %d\n", ret);
	} else {
		init_val = (ri->active_low) ? 0 : 1;
		ret = gpio_direction_output(ri->gpio_nr, init_val);
		if (ret < 0)
			dev_err(dev, "Unable to set direction %d\n",
				ri->gpio_nr);
	}
	return ret;
}

static int _gpio_regulator_disable(struct device *dev,
		struct gpio_switch_regulator *ri)
{
	int init_val;
	int ret;

	if (ri->disable_rail) {
		ret = ri->disable_rail(ri->psubdev_data);
		if (ret < 0)
			dev_err(dev, "Unable to disable rail through "
				"board api %d\n", ret);
	} else {
		init_val = (ri->active_low) ? 1 : 0;
		ret = gpio_direction_output(ri->gpio_nr, init_val);
		if (ret < 0)
			dev_err(dev, "Unable to set direction %d\n",
				ri->gpio_nr);
	}
	return ret;
}

static int gpio_switch_list_voltage(struct regulator_dev *rdev,
				unsigned selector)
{
	struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev);

	if (selector < ri->reg_desc.n_voltages)
		return ri->voltages[selector] * 1000;
	else
		return 0;
}

static int gpio_switch_set_voltage(struct regulator_dev *rdev,
				   int min_uV, int max_uV, unsigned *selector)
{
	struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev);
	int uV;
	bool found = false;
	unsigned val;

	for (val = 0; val < ri->reg_desc.n_voltages; val++) {
		uV = ri->voltages[val] * 1000;
		if (min_uV <= uV && uV <= max_uV) {
			found = true;
			*selector = ri->curr_vol_sel = val;
			break;
		}
	}
	if (found && ri->input_regulator)
		return regulator_set_voltage(ri->input_regulator, min_uV,
			max_uV);
	ri->curr_vol_sel = 0;
	return -EINVAL;
}

static int gpio_switch_get_voltage(struct regulator_dev *rdev)
{
	struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev);
	if (ri->input_regulator)
		return regulator_get_voltage(ri->input_regulator);

	if (ri->curr_vol_sel < ri->reg_desc.n_voltages)
		return ri->voltages[ri->curr_vol_sel] * 1000;
	return 0;
}

static int gpio_switch_regulator_enable(struct regulator_dev *rdev)
{
	struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev);
	int ret = 0;
	if (ri->is_enable)
		return 0;

	if (ri->input_regulator) {
		ret = regulator_enable(ri->input_regulator);
		if (ret < 0) {
			dev_err(&rdev->dev, "%s:Failed to enable regulator"
				" Error %d\n", __func__, ret);
			return ret;
		}
	}

	ret = _gpio_regulator_enable(&rdev->dev, ri);
	if (ret < 0)
		return ret;
	ri->is_enable = true;
	return 0;
}

static int gpio_switch_regulator_disable(struct regulator_dev *rdev)
{
	struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev);
	int ret = 0;

	if (!ri->is_enable)
		return 0;

	ret = _gpio_regulator_disable(&rdev->dev, ri);
	if (ret < 0)
		return ret;

	if (ri->input_regulator) {
		ret = regulator_disable(ri->input_regulator);
		if (ret < 0) {
			dev_err(&rdev->dev, "%s:Failed to disable regulator"
				" Error %d\n", __func__, ret);
			return ret;
		}
	}

	ri->is_enable = false;
	return 0;
}

static int gpio_switch_regulator_is_enabled(struct regulator_dev *rdev)
{
	struct gpio_switch_regulator *ri = rdev_get_drvdata(rdev);
	int ret = 0;
	if (ri->input_regulator) {
		ret = regulator_is_enabled(ri->input_regulator);
		if (!ret)
			return ret;
	}
	return (ri->is_enable) ? 1 : 0;
}

static struct regulator_ops gpio_switch_regulator_ops = {
	.list_voltage	= gpio_switch_list_voltage,
	.get_voltage	= gpio_switch_get_voltage,
	.set_voltage	= gpio_switch_set_voltage,
	.is_enabled	= gpio_switch_regulator_is_enabled,
	.enable		= gpio_switch_regulator_enable,
	.disable	= gpio_switch_regulator_disable,
};

static int __devinit gpio_switch_regulator_probe(struct platform_device *pdev)
{
	struct gpio_switch_regulator *ri = NULL;
	struct gpio_switch_regulator *gswitch_reg = NULL;
	struct gpio_switch_regulator_platform_data *pdata;
	struct gpio_switch_regulator_subdev_data *sdata = NULL;
	int id = pdev->id;
	int ret = 0;
	int rcount;

	dev_dbg(&pdev->dev, "Probing regulator %d\n", id);

	pdata = pdev->dev.platform_data;
	if (!pdata) {
		dev_err(&pdev->dev, "%s:No platform data Exiting\n", __func__);
		return -ENODEV;
	}

	BUG_ON(!pdata->num_subdevs);

	gswitch_reg = kzalloc(sizeof(struct gpio_switch_regulator) *
				pdata->num_subdevs, GFP_KERNEL);
	if (!gswitch_reg) {
		dev_err(&pdev->dev, "%s:Failed to allocate memory\n", __func__);
		return -ENOMEM;
	}

	for (rcount = 0; rcount < pdata->num_subdevs; ++rcount) {
		ri = &gswitch_reg[rcount];
		sdata = pdata->subdevs[rcount];

		/* Initialize the regulator parameter */
		ri->reg_desc.name = sdata->regulator_name;
		ri->reg_desc.ops = &gpio_switch_regulator_ops;
		ri->reg_desc.type = REGULATOR_VOLTAGE;
		ri->reg_desc.id = sdata->id;
		ri->reg_desc.n_voltages = sdata->n_voltages;
		ri->reg_desc.owner = THIS_MODULE;
		ri->is_init_success = false;

		memcpy(&ri->reg_init_data.constraints, &sdata->constraints,
				sizeof(struct regulation_constraints));

		/* Initialize min and maximum contraint voltage if it is not
		 * define in platform device */
		if (!sdata->constraints.min_uV)
			ri->reg_init_data.constraints.min_uV = 1000 *
						sdata->voltages[0];

		if (!sdata->constraints.max_uV)
			ri->reg_init_data.constraints.max_uV = 1000 *
					sdata->voltages[sdata->n_voltages - 1];

		ri->reg_init_data.num_consumer_supplies =
						sdata->num_consumer_supplies;
		ri->reg_init_data.consumer_supplies = sdata->consumer_supplies;

		ri->input_regulator = NULL;
		ri->is_gpio_init = false;
		ri->is_enable = (sdata->init_state) ? true : false;
		ri->voltages = sdata->voltages;
		ri->psubdev_data = sdata;
		ri->gpio_nr = sdata->gpio_nr;
		ri->active_low = sdata->active_low;
		ri->dev = &pdev->dev;
		ri->enable_rail = sdata->enable_rail;
		ri->disable_rail = sdata->disable_rail;
		ri->pin_group = sdata->pin_group;

		/* Checking for board APIs enable/disable rail */
		if (ri->enable_rail || ri->disable_rail)
			BUG_ON(!(ri->enable_rail && ri->disable_rail));

		/* Get the regulator structure if input supply is available */
		if (sdata->input_supply) {
			ri->input_regulator = regulator_get(NULL,
							sdata->input_supply);
			if (IS_ERR_OR_NULL(ri->input_regulator)) {
				dev_err(&pdev->dev, "Unable to get regu"
					"lator %s\n", sdata->input_supply);
				ret = -ENODEV;
				goto reg_get_fail;
			}
			if (ri->is_enable) {
				ret = regulator_enable(ri->input_regulator);
				if (ret < 0) {
					dev_err(&pdev->dev, "Unable to enable "
						"regulator %s\n",
						sdata->input_supply);
					goto reg_en_fail;
				}
			}
		}

		/* Initialize gpios */
		ret = gpio_request(ri->gpio_nr, sdata->regulator_name);
		if (ret < 0) {
			dev_err(&pdev->dev, "Unable to request gpio %d\n",
					ri->gpio_nr);
			goto gpio_req_fail;
		}

		if (ri->is_enable)
			ret = _gpio_regulator_enable(&pdev->dev, ri);
		else
			ret = _gpio_regulator_disable(&pdev->dev, ri);
		if (ret < 0)
			goto reg_cont_fail;

		ri->is_gpio_init = true;

		ri->rdev = regulator_register(&ri->reg_desc, &pdev->dev,
					&ri->reg_init_data, ri);
		if (IS_ERR_OR_NULL(ri->rdev)) {
			dev_err(&pdev->dev, "Failed to register regulator %s\n",
							ri->reg_desc.name);
			ret = PTR_ERR(ri->rdev);
			goto reg_reg_fail;
		}

		/* If everything success then continue for next registration */
		ri->is_init_success = true;
		continue;

		/* Cleanup the current registration and continue for next
		 * registration*/
reg_reg_fail:
		if (ri->is_enable)
			_gpio_regulator_disable(&pdev->dev, ri);
reg_cont_fail:
		gpio_free(ri->gpio_nr);
gpio_req_fail:
		if (ri->is_enable && ri->input_regulator)
			regulator_disable(ri->input_regulator);
reg_en_fail:
		if (ri->input_regulator) {
			regulator_put(ri->input_regulator);
			ri->input_regulator = NULL;
		}
reg_get_fail:
		dev_err(&pdev->dev, "Unable to register regulator %s\n",
							sdata->regulator_name);
	}

	platform_set_drvdata(pdev, gswitch_reg);
	return 0;
}

static int __devexit gpio_switch_regulator_remove(struct platform_device *pdev)
{
	struct gpio_switch_regulator *ri = NULL;
	struct gpio_switch_regulator *gswitch_reg = platform_get_drvdata(pdev);
	int i;
	struct gpio_switch_regulator_platform_data *pdata;

	pdata = pdev->dev.platform_data;

	/* Unregister devices in reverse order */
	for (i = pdata->num_subdevs; i; --i) {
		ri = &gswitch_reg[i - 1];
		/* If registration was not success, then do not release */
		if (!ri->is_init_success)
			continue;

		if (ri->is_enable)
			_gpio_regulator_disable(&pdev->dev, ri);

		if (ri->input_regulator) {
			if (ri->is_enable)
				regulator_disable(ri->input_regulator);
			regulator_put(ri->input_regulator);
		}

		regulator_unregister(ri->rdev);
		gpio_free(ri->gpio_nr);
	}

	kfree(gswitch_reg);
	platform_set_drvdata(pdev, NULL);
	return 0;
}

static struct platform_driver gpio_switch_regulator_driver = {
	.driver	= {
		.name	= "gpio-switch-regulator",
		.owner	= THIS_MODULE,
	},
	.probe		= gpio_switch_regulator_probe,
	.remove		= __devexit_p(gpio_switch_regulator_remove),
};

static int __init gpio_switch_regulator_init(void)
{
	return platform_driver_register(&gpio_switch_regulator_driver);
}

static void __exit gpio_switch_regulator_exit(void)
{
	platform_driver_unregister(&gpio_switch_regulator_driver);
}

subsys_initcall_sync(gpio_switch_regulator_init);
module_exit(gpio_switch_regulator_exit);