summaryrefslogtreecommitdiff
path: root/drivers/regulator/bq2419x-regulator.c
blob: e4f660212cc4785ccc6a6da875347eab83d5e5e1 (plain)
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
/*
 * bq2419x-regulator.c --  bq2419x
 *
 * Regulator driver for BQ2419X charger.
 *
 * Copyright (c) 2012-2013, NVIDIA CORPORATION.  All rights reserved.
 *
 * Author: Laxman Dewangan <ldewangan@nvidia.com>
 *
 * 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 version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
 * whether express or implied; 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., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307, USA
 */

#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/mfd/bq2419x.h>
#include <linux/slab.h>

struct bq2419x_regulator_info {
	struct device *dev;
	struct regulator_desc desc;
	struct regulator_dev *rdev;
	struct bq2419x_chip *chip;
	int gpio_otg_iusb;
	bool power_off_on_suspend;
	struct mutex mutex;
	int shutdown_complete;
};

static int bq2419x_regulator_enable_time(struct regulator_dev *rdev)
{
	return 500000;
}

static int bq2419x_dcdc_enable(struct regulator_dev *rdev)
{
	struct bq2419x_regulator_info *bq = rdev_get_drvdata(rdev);
	int ret;

	dev_info(bq->dev, "%s called\n", __func__);
	if (gpio_is_valid(bq->gpio_otg_iusb))
		gpio_set_value(bq->gpio_otg_iusb, 1);

	mutex_lock(&bq->mutex);
	if (bq && bq->shutdown_complete) {
		mutex_unlock(&bq->mutex);
		return -ENODEV;
	}

	/* Clear EN_HIZ */
	ret = regmap_update_bits(bq->chip->regmap,
			BQ2419X_INPUT_SRC_REG, BQ2419X_EN_HIZ, 0);
	if (ret < 0) {
		dev_err(bq->dev, "error reading reg: 0x%x\n",
			BQ2419X_INPUT_SRC_REG);
		return ret;
	}

	ret = regmap_update_bits(bq->chip->regmap, BQ2419X_OTG,
			BQ2419X_OTG_ENABLE_MASK, BQ2419X_OTG_ENABLE);
	if (ret < 0) {
		dev_err(bq->dev, "register %d update failed with err %d",
			 BQ2419X_OTG, ret);
		mutex_unlock(&bq->mutex);
		return ret;
	}
	mutex_unlock(&bq->mutex);
	return ret;
}

static int bq2419x_dcdc_disable(struct regulator_dev *rdev)
{
	struct bq2419x_regulator_info *bq = rdev_get_drvdata(rdev);
	int ret = 0;

	dev_info(bq->dev, "%s called\n", __func__);
	mutex_lock(&bq->mutex);
	if (bq && bq->shutdown_complete) {
		mutex_unlock(&bq->mutex);
		return -ENODEV;
	}

	ret = regmap_update_bits(bq->chip->regmap, BQ2419X_OTG,
					BQ2419X_OTG_ENABLE_MASK, 0x10);
	if (ret < 0) {
		dev_err(bq->dev, "register %d update failed with err %d",
			BQ2419X_OTG, ret);
		mutex_unlock(&bq->mutex);
		return ret;
	}

	if (gpio_is_valid(bq->gpio_otg_iusb))
		gpio_set_value(bq->gpio_otg_iusb, 0);

	mutex_unlock(&bq->mutex);
	return ret;
}

static int bq2419x_dcdc_is_enabled(struct regulator_dev *rdev)
{
	struct bq2419x_regulator_info *bq = rdev_get_drvdata(rdev);
	int ret;
	unsigned int data;

	mutex_lock(&bq->mutex);
	if (bq && bq->shutdown_complete) {
		mutex_unlock(&bq->mutex);
		return -ENODEV;
	}
	ret = regmap_read(bq->chip->regmap, BQ2419X_OTG, &data);
	if (ret < 0) {
		dev_err(bq->dev, "register %d read failed with err %d",
			BQ2419X_OTG, ret);
		mutex_unlock(&bq->mutex);
		return ret;
	}
	mutex_unlock(&bq->mutex);
	return (data & BQ2419X_OTG_ENABLE_MASK) == BQ2419X_OTG_ENABLE;
}

static struct regulator_ops bq2419x_dcdc_ops = {
	.enable			= bq2419x_dcdc_enable,
	.disable		= bq2419x_dcdc_disable,
	.is_enabled		= bq2419x_dcdc_is_enabled,
	.enable_time		= bq2419x_regulator_enable_time,
};

static void bq2419x_regulator_shutdown(struct platform_device *pdev)
{
	struct bq2419x_regulator_info *bq = platform_get_drvdata(pdev);

	mutex_lock(&bq->mutex);
	bq->shutdown_complete = 1;
	mutex_unlock(&bq->mutex);
}

static int __devinit bq2419x_regulator_probe(struct platform_device *pdev)
{
	struct bq2419x_regulator_platform_data *pdata = NULL;
	struct bq2419x_platform_data *chip_pdata;
	struct regulator_dev *rdev;
	struct bq2419x_regulator_info *bq;
	int ret;

	chip_pdata = dev_get_platdata(pdev->dev.parent);
	if (chip_pdata)
		pdata = chip_pdata->reg_pdata;

	if (!pdata) {
		dev_err(&pdev->dev, "No Platform data");
		return -EIO;
	}

	bq = devm_kzalloc(&pdev->dev, sizeof(*bq), GFP_KERNEL);
	if (!bq) {
		dev_err(&pdev->dev, "Memory allocation failed\n");
		return -ENOMEM;
	}

	bq->dev = &pdev->dev;
	bq->chip = dev_get_drvdata(pdev->dev.parent);

	bq->gpio_otg_iusb = pdata->gpio_otg_iusb;
	bq->power_off_on_suspend = pdata->power_off_on_suspend;
	bq->desc.name = "bq2419x-vbus";
	bq->desc.id = 0;
	bq->desc.ops = &bq2419x_dcdc_ops;
	bq->desc.type = REGULATOR_VOLTAGE;
	bq->desc.owner = THIS_MODULE;
	bq->shutdown_complete = 0;
	mutex_init(&bq->mutex);

	platform_set_drvdata(pdev, bq);

	if (gpio_is_valid(bq->gpio_otg_iusb)) {
		ret = gpio_request(bq->gpio_otg_iusb, dev_name(&pdev->dev));
		if (ret < 0) {
			dev_err(&pdev->dev,
				"gpio request failed, err = %d\n", ret);
			return ret;
		}
		gpio_direction_output(bq->gpio_otg_iusb, 0);
	}

	/* Register the regulators */
	rdev = regulator_register(&bq->desc, &pdev->dev,
			pdata->reg_init_data, bq, NULL);
	if (IS_ERR(rdev)) {
		ret = PTR_ERR(rdev);
		dev_err(bq->dev, "regulator register failed, err %d\n", ret);
		goto err_init;
	}

	bq->rdev = rdev;

	ret = regmap_update_bits(bq->chip->regmap, BQ2419X_OTG,
					BQ2419X_OTG_ENABLE_MASK, 0x10);
	if (ret < 0) {
		dev_err(bq->dev, "register %d update failed with err %d",
			BQ2419X_OTG, ret);
		goto err_reg_update;
	}
	return 0;

err_reg_update:
	regulator_unregister(bq->rdev);
err_init:
	if (gpio_is_valid(bq->gpio_otg_iusb))
		gpio_free(bq->gpio_otg_iusb);
	mutex_destroy(&bq->mutex);
	return ret;
}

static int __devexit bq2419x_regulator_remove(struct platform_device *pdev)
{
	struct bq2419x_regulator_info *bq = platform_get_drvdata(pdev);

	mutex_destroy(&bq->mutex);
	regulator_unregister(bq->rdev);
	if (gpio_is_valid(bq->gpio_otg_iusb))
		gpio_free(bq->gpio_otg_iusb);
	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int bq2419x_reg_suspend(struct device *dev)
{
	struct bq2419x_regulator_info *bq = dev_get_drvdata(dev);

	if (bq->power_off_on_suspend && bq2419x_dcdc_is_enabled(bq->rdev) > 0)
		bq2419x_dcdc_disable(bq->rdev);
	return 0;
}

static int bq2419x_reg_resume(struct device *dev)
{
	struct bq2419x_regulator_info *bq = dev_get_drvdata(dev);

	/* Turn on regulator that might be turned off by bq2419x_reg_suspend()
	 * and that should be turned on according to the regulators properties.
	 */
	if (bq->power_off_on_suspend &&
		(bq->rdev->use_count > 0 || bq->rdev->constraints->always_on))
		bq2419x_dcdc_enable(bq->rdev);
	return 0;
}

static const struct dev_pm_ops bq2419x_reg_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(bq2419x_reg_suspend, bq2419x_reg_resume)
};
#endif

static struct platform_driver bq2419x_regulator_driver = {
	.driver = {
		.name = "bq2419x-pmic",
		.owner = THIS_MODULE,
#ifdef CONFIG_PM_SLEEP
		.pm = &bq2419x_reg_pm_ops,
#endif
	},
	.probe = bq2419x_regulator_probe,
	.shutdown = bq2419x_regulator_shutdown,
	.remove = __devexit_p(bq2419x_regulator_remove),
};

static int __init bq2419x_init(void)
{
	return platform_driver_register(&bq2419x_regulator_driver);
}
subsys_initcall(bq2419x_init);

static void __exit bq2419x_cleanup(void)
{
	platform_driver_unregister(&bq2419x_regulator_driver);
}
module_exit(bq2419x_cleanup);

MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
MODULE_DESCRIPTION("BQ2419X voltage regulator driver");
MODULE_LICENSE("GPL v2");