summaryrefslogtreecommitdiff
path: root/drivers/regulator/tegra-regulator.c
blob: 8a1ecb15ebc5026c0bed7b4eaadce3ba741c90bf (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
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
/*
 * drivers/regulator/tegra-regulator.c
 *
 * Regulator driver for NVIDIA Tegra NvRm PMU APIs
 *
 * Copyright (C) 2010 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 NV_DEBUG 0

#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/err.h>
#include <linux/regulator/machine.h>
#include <linux/mutex.h>
#include <linux/delay.h>

#include <mach/nvrm_linux.h>
#include <mach/regulator.h>

#include <nvassert.h>
#include <nvrm_pmu.h>
#include <nvrm_power.h>
#include <nvodm_query_discovery.h>

struct tegra_vdd {
	NvU32 id;
	unsigned long request_uV;
};

struct tegra_charger {
	struct regulator_desc rdesc;
	struct regulator_init_data init;
	struct regulator_dev *rdev;
	struct device *dev;
	struct work_struct work;
	struct list_head node;
	NvRmPmuChargingPath path;
	unsigned long request_uA;
};

struct tegra_regulator {
	struct regulator_desc rdesc;
	struct regulator_init_data init;
	struct regulator_dev *rdev;
	struct device *dev;
	struct list_head node;
	bool enable;
	int nr_vdd;
	struct tegra_vdd vdd_list[1];
};

struct tegra_regulator_drv {
	struct list_head list_reg;
	struct list_head list_chg;
};

static void tegra_charger_work(struct work_struct *w)
{
	struct tegra_charger *c= container_of(w, struct tegra_charger, work);

	NvRmPmuSetChargingCurrentLimit(s_hRmGlobal, c->path,
		       c->request_uA/1000, NvOdmUsbChargerType_SE0);
}

static int tegra_charger_set_current_limit(struct regulator_dev *rdev,
					    int min_uA, int max_uA)

{
	struct tegra_charger *c = rdev_get_drvdata(rdev);

	c->request_uA = max_uA;
	schedule_work(&c->work);

	return 0;
}

static int tegra_regulator_enable(struct regulator_dev *rdev)
{
	struct tegra_regulator *data = rdev_get_drvdata(rdev);
	NvU32 settle;
	int i;

	dev_dbg(data->dev, "%s: %s->%luuV enabled:%s\n", __func__,
		data->rdesc.name, data->vdd_list[0].request_uV,
		data->enable ? "true" : "false");

	if (!data->enable) {
		for (i=0; i<data->nr_vdd; i++) {
			const struct tegra_vdd *vdd = &data->vdd_list[i];
			NvRmPmuSetVoltage(s_hRmGlobal, vdd->id,
					  vdd->request_uV / 1000, &settle);
			udelay(settle);
		}
	}

	data->enable = true;
	return 0;
}

static int tegra_regulator_disable(struct regulator_dev *rdev)
{
	struct tegra_regulator *data = rdev_get_drvdata(rdev);
	int i;

	dev_dbg(data->dev, "%s: %s enabled:%s\n", __func__,
		data->rdesc.name, data->enable ? "true" : "false");

	if (data->enable) {
		for (i=0; i<data->nr_vdd; i++) {
			NvRmPmuSetVoltage(s_hRmGlobal, data->vdd_list[i].id,
					  NVODM_VOLTAGE_OFF, NULL);
		}
	}
	data->enable = false;
	return 0;
}

static int tegra_regulator_set_voltage(struct regulator_dev *rdev,
				       int min_uV, int max_uV)
{
	struct tegra_regulator *data = rdev_get_drvdata(rdev);

	BUG_ON(data->nr_vdd > 1);

	dev_dbg(data->dev, "%s: %s->[%d..%d]uV enabled:%s\n", __func__,
		data->rdesc.name, min_uV, max_uV,
		data->enable ? "true" : "false");

	if (data->enable) {
		NvU32 settle;
		NvRmPmuSetVoltage(s_hRmGlobal, data->vdd_list[0].id,
				  min_uV, &settle);
		udelay(settle);
	}
	data->vdd_list[0].request_uV = min_uV;
	return 0;
}

static struct regulator_ops tegra_soc_dynamic_vdd_ops = {
	.enable = tegra_regulator_enable,
	.disable = tegra_regulator_disable,
	.set_voltage = tegra_regulator_set_voltage,
};

static struct regulator_ops tegra_soc_fixed_vdd_ops = {
	.enable = tegra_regulator_enable,
	.disable = tegra_regulator_disable,
};

static struct regulator_ops tegra_charger_ops = {
	.set_current_limit = tegra_charger_set_current_limit,
};

static int tegra_charger_register(struct platform_device *pdev,
				  struct tegra_regulator_entry *entry)
{
	struct tegra_charger *chg;
	struct regulator_dev *rdev;
	struct tegra_regulator_drv *drv;

	drv = platform_get_drvdata(pdev);

	chg = kzalloc(sizeof(*chg), GFP_KERNEL);
	if (!chg) {
		dev_err(&pdev->dev, "out of memory\n");
		return -ENOMEM;
	}

	chg->path = entry->charging_path;

	chg->rdesc.type = REGULATOR_CURRENT;
	chg->rdesc.name = kstrdup(entry->name, GFP_KERNEL);
	chg->rdesc.owner = THIS_MODULE;
	chg->dev = &pdev->dev;
	chg->init.consumer_supplies = entry->consumers;
	chg->init.num_consumer_supplies = entry->nr_consumers;
	chg->rdesc.id = entry->id;
	chg->init.constraints.valid_ops_mask = REGULATOR_CHANGE_CURRENT;
	chg->rdesc.ops = &tegra_charger_ops;
	chg->init.constraints.min_uA = 0;
	chg->init.constraints.max_uA = 1800000;
	INIT_WORK(&chg->work, tegra_charger_work);

	rdev = regulator_register(&chg->rdesc, &pdev->dev, &chg->init, chg);
	if (IS_ERR(rdev)) {
		dev_err(&pdev->dev, "unable to register %s\n", entry->name);
		kfree(chg);
		return PTR_ERR(chg);
	}
	chg->rdev = rdev;

	list_add_tail(&chg->node, &drv->list_chg);

	return 0;
}

static int tegra_regulator_register(struct platform_device *pdev,
				    struct tegra_regulator_entry *entry)
{
	const NvOdmPeripheralConnectivity *con;
	struct tegra_regulator *reg;
	struct tegra_regulator_drv *drv;
	struct regulator_dev *rdev;
	NvRmPmuVddRailCapabilities cap;
	unsigned int i;
	unsigned int cnt;

	drv = platform_get_drvdata(pdev);

	con = NvOdmPeripheralGetGuid(entry->guid);
	if (!con)
		return -ENODEV;

	for (i=0, cnt=0; i<con->NumAddress; i++) {
		if (con->AddressList[i].Interface == NvOdmIoModule_Vdd)
			cnt++;
	}

	if (!cnt) {
		char guid_str[9];
		memcpy(guid_str, &entry->guid, 8);
		guid_str[8] = 0;
		dev_err(&pdev->dev, "(%s): no VDDs defined for %s\n",
			entry->name, guid_str);
		return -ENOSYS;
	}

	reg = kzalloc(sizeof(*reg) + sizeof(struct tegra_vdd)*(cnt-1), GFP_KERNEL);
	if (!reg) {
		dev_err(&pdev->dev, "out of memory\n");
		return -ENOMEM;
	}
	reg->nr_vdd = cnt;
	for (i=0, cnt=0; i<con->NumAddress; i++) {
		NvU32 vdd;
		if (con->AddressList[i].Interface != NvOdmIoModule_Vdd)
			continue;

		vdd = con->AddressList[i].Address;
		NvRmPmuGetCapabilities(s_hRmGlobal, vdd, &cap);
		reg->vdd_list[cnt].id = vdd;
		reg->vdd_list[cnt].request_uV = cap.requestMilliVolts * 1000;
		cnt++;
	}

	reg->rdesc.type = REGULATOR_VOLTAGE;
	reg->rdesc.name = kstrdup(entry->name, GFP_KERNEL);
	reg->rdesc.owner = THIS_MODULE;
	reg->enable = false;
	reg->dev = &pdev->dev;
	reg->init.consumer_supplies = entry->consumers;
	reg->init.num_consumer_supplies = entry->nr_consumers;
	reg->rdesc.id = entry->id;

	if (entry->guid == NV_VDD_SoC_ODM_ID) {
		reg->init.constraints.boot_on = true;
		reg->enable = true;
	}

	if (cnt>1 || cap.RmProtected) {
		reg->rdesc.ops = &tegra_soc_fixed_vdd_ops;
		reg->init.constraints.min_uV = reg->vdd_list[0].request_uV;
		reg->init.constraints.max_uV = reg->vdd_list[0].request_uV;
		reg->init.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS;
	} else {
		reg->rdesc.ops = &tegra_soc_dynamic_vdd_ops;
		reg->init.constraints.min_uV = cap.MinMilliVolts * 1000;
		reg->init.constraints.max_uV = cap.MaxMilliVolts * 1000;
		reg->init.constraints.valid_ops_mask = REGULATOR_CHANGE_STATUS |
			REGULATOR_CHANGE_VOLTAGE;
	}

	rdev = regulator_register(&reg->rdesc, &pdev->dev, &reg->init, reg);
	if (IS_ERR(rdev)) {
		dev_err(&pdev->dev, "unable to register %s\n", entry->name);
		kfree(reg);
		return PTR_ERR(reg);
	}
	reg->rdev = rdev;

	list_add_tail(&reg->node, &drv->list_reg);

	return 0;
}

static int tegra_regulator_probe(struct platform_device *pdev)
{
	struct tegra_regulator_platform_data *plat = pdev->dev.platform_data;
	struct tegra_regulator_drv *drv;
	int i;

	drv = kzalloc(sizeof(*drv), GFP_KERNEL);
	if (!drv)
		return -ENOMEM;

	INIT_LIST_HEAD(&drv->list_reg);
	INIT_LIST_HEAD(&drv->list_chg);
	platform_set_drvdata(pdev, drv);

	for (i=0; i<plat->nr_regs; i++) {
		if (plat->regs[i].is_charger)
			tegra_charger_register(pdev, &plat->regs[i]);
		else
			tegra_regulator_register(pdev, &plat->regs[i]);
	}

	return 0;
}

static int tegra_regulator_remove(struct platform_device *pdev)
{
	struct tegra_regulator_drv *drv = platform_get_drvdata(pdev);

	while (!list_empty(&drv->list_reg)) {
		struct tegra_regulator *reg;
		reg = list_first_entry(&drv->list_reg, struct tegra_regulator, node);
		list_del_init(&drv->list_reg);
		regulator_unregister(reg->rdev);
		kfree(reg);
	}
	while (!list_empty(&drv->list_chg)) {
		struct tegra_charger *reg;
		reg = list_first_entry(&drv->list_chg, struct tegra_charger, node);
		list_del_init(&drv->list_chg);
		regulator_unregister(reg->rdev);
		cancel_work_sync(&reg->work);
		kfree(reg);
	}

	kfree(drv);

	return 0;
}

static struct platform_driver tegra_regulator_driver = {
	.driver = {
		.name = "tegra_regulator",
	},
	.probe = tegra_regulator_probe,
	.remove = tegra_regulator_remove,
};

static int __init tegra_regulator_init(void)
{
	return platform_driver_register(&tegra_regulator_driver);
}
postcore_initcall_sync(tegra_regulator_init);

static void __exit tegra_regulator_exit(void)
{
	platform_driver_unregister(&tegra_regulator_driver);
}
module_exit(tegra_regulator_exit);

MODULE_DESCRIPTION("Tegra regulator driver");
MODULE_LICENSE("GPL");