summaryrefslogtreecommitdiffstats
path: root/drivers/misc/nvs/nvs_gte.c
blob: f4753dc4065c3cda15cd6f7a4b422d0b022515ee (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
/* Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * 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.
 */

#ifndef NVS_GTE
#define NVS_GTE				(0)
#endif

#define NVS_GTE_VERSION			(1)

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/nvs.h>
#include <linux/nvs_gte.h>


static void nvs_gte_exit_irq(struct nvs_gte_irq *ngi, unsigned int n)
{
	unsigned int i;
	unsigned int irq;
	unsigned int j;

	for (i = 0; i < n; i++) {
		if (ngi[i].irq > 0 && ngi[i].gpio >= 0) {
			for (j = 0; j < i; j++) {
				if (ngi[j].irq == ngi[i].irq)
					break;
			}
			if (j < i)
				/* IRQ already done */
				continue;

			irq = ngi[i].irq;
			free_irq(irq, &ngi[i]);
		}
	}

	for (i = 0; i < n; i++) {
		if (ngi[i].gpio >= 0)
			ngi[i].irq = -1;
	}

	return;
}

static void nvs_gte_exit_gpio(struct nvs_gte_irq *ngi, unsigned int n)
{
	unsigned int gpio;
	unsigned int i;
	unsigned int j;

	for (i = 0; i < n; i++) {
		if (ngi[i].gpio >= 0) {
			for (j = 0; j < i; j++) {
				if (ngi[j].gpio == ngi[i].gpio)
					break;
			}
			if (j < i)
				/* GPIO already done */
				continue;

			gpio = ngi[i].gpio;
			gpio_free(gpio);
		}
	}

	for (i = 0; i < n; i++)
		ngi[i].gpio = -1;

	return;
}

static int nvs_gte_init_gpio2irq(struct device *dev, struct nvs_gte_irq *ngi,
				 unsigned int n)
{
	unsigned int i;
	unsigned int j;
	int ret = -EINVAL;

	for (i = 0; i < n; i++) {
		if (ngi[i].irq > 0) {
			ret = 0;
			continue;
		}

		if (ngi[i].gpio < 0)
			continue;

		for (j = 0; j < i; j++) {
			if (ngi[j].gpio == ngi[i].gpio)
				break;
		}
		if (j < i) {
			/* GPIO already done */
			ngi[i].irq = ngi[j].irq;
			continue;
		}

		if (!gpio_is_valid(ngi[i].gpio)) {
			ret = -EPROBE_DEFER;
			goto nvs_gte_init_gpio_fie;
		}

		ret = gpio_request(ngi[i].gpio, ngi[i].dev_name);
		if (ret) {
			ret = -EPROBE_DEFER;
			goto nvs_gte_init_gpio_fie;
		}

		ret = gpio_direction_input(ngi[i].gpio);
		if (ret < 0) {
			dev_err(dev, "%s %s gpio_dir_input(%d) ERR:%d\n",
				__func__, ngi[i].dev_name, ngi[i].gpio, ret);
			nvs_gte_exit_irq(ngi, n);
			nvs_gte_exit_gpio(ngi, n);
			ret = -ENODEV;
			goto nvs_gte_init_gpio_fie;
		}

		ret = gpio_to_irq(ngi[i].gpio);
		if (ret <= 0) {
			dev_err(dev, "%s %s gpio_to_irq(%d) ERR:%d\n",
				__func__, ngi[i].dev_name, ngi[i].gpio, ret);
			nvs_gte_exit_irq(ngi, n);
			nvs_gte_exit_gpio(ngi, n);
			ret = -ENODEV;
			goto nvs_gte_init_gpio_fie;
		}

		ngi[i].irq = ret;
		ret = 0;
	}

nvs_gte_init_gpio_fie:
	return ret;
}


#if NVS_GTE


#include <linux/tegra-gte.h>

static char *nvs_gte_hw_str = "nvidia,tegra194-gte-aon";

int nvs_gte_ts(struct nvs_gte_irq *ngi)
{
	struct tegra_gte_ev_desc *desc = (struct tegra_gte_ev_desc *)ngi->gte;
	struct tegra_gte_ev_detail dtl;
	int ret;

	ret = tegra_gte_retrieve_event(desc, &dtl);
	if (ret)
		ngi->err_n++;
	else
		ngi->irq_ts = dtl.ts_ns;
	return ret;
}
EXPORT_SYMBOL_GPL(nvs_gte_ts);

int nvs_gte_exit(struct device *dev, struct nvs_gte_irq *ngi, unsigned int n)
{
	struct tegra_gte_ev_desc *desc;
	unsigned int i;
	int ret;
	int ret_t = 0;

	for (i = 0; i < n; i++) {
		if (ngi[i].gte) {
			desc = (struct tegra_gte_ev_desc *)ngi[i].gte;
			ret = tegra_gte_unregister_event(desc);
			if (ret) {
				ngi[i].gte = NULL;
				dev_err(dev,
					"%s gte_unregister_event ERR: %d\n",
					__func__, ret);
			} else {
				ret_t |= ret;
			}
		}
	}

	nvs_gte_exit_irq(ngi, n);
	nvs_gte_exit_gpio(ngi, n);
	return ret_t;
}
EXPORT_SYMBOL_GPL(nvs_gte_exit);

int nvs_gte_init(struct device *dev, struct nvs_gte_irq *ngi, unsigned int n)
{
	struct tegra_gte_ev_desc *desc;
	unsigned int gpio;
	unsigned int i;
	int ret;
	/* Until the GTE API is improved we have to do this */
	struct device_node *np;
	np = of_find_compatible_node(NULL, NULL, nvs_gte_hw_str);

	ret = nvs_gte_init_gpio2irq(dev, ngi, n);
	if (ret == -EPROBE_DEFER)
		goto nvs_gte_init_fie;

	if (ret < 0) {
		nvs_gte_exit(dev, ngi, n);
		goto nvs_gte_init_fie;
	}

	ret = -ENODEV;
	for (i = 0; i < n; i++) {
		if (ngi[i].gpio >= 0 && !ngi[i].gte) {
			gpio = ngi[i].gpio;
			desc = tegra_gte_register_event(np, gpio);
			if (desc) {
				ngi[i].gte = (void *)desc;
				ret = 0;
			}
		}
	}

nvs_gte_init_fie:
	of_node_put(np);
	return ret;
}
EXPORT_SYMBOL_GPL(nvs_gte_init);

int nvs_gte_sts(struct nvs_gte_irq *ngi, struct nvs_gte_sts *ngs)
{
	struct tegra_gte_ev_desc *desc = (struct tegra_gte_ev_desc *)ngi->gte;
	struct tegra_gte_ev_detail dtl;
	int ret;

	ngs->ver = NVS_GTE_VERSION;
	ngs->gte = nvs_gte_hw_str;
	ngs->ts_ns = 0;
	ngs->ts_raw = 0;
	if (desc) {
		ret = tegra_gte_retrieve_event(desc, &dtl);
		if (ret) {
			ngs->err = "HW read err";
		} else {
			ngs->err = NULL;
			ngs->ts_ns = dtl.ts_ns;
			ngs->ts_raw = dtl.ts_raw;
		}
	} else {
		ngs->err = "not initialized";
	}
	return 0;
}
EXPORT_SYMBOL_GPL(nvs_gte_sts);


#else /* !NVS_GTE ========================================================== */


int nvs_gte_ts(struct nvs_gte_irq *ngi)
{
	ngi->irq_ts = nvs_timestamp();
	return 0;
}
EXPORT_SYMBOL_GPL(nvs_gte_ts);

int nvs_gte_exit(struct device *dev, struct nvs_gte_irq *ngi, unsigned int n)
{
	nvs_gte_exit_irq(ngi, n);
	nvs_gte_exit_gpio(ngi, n);
	return 0;
}
EXPORT_SYMBOL_GPL(nvs_gte_exit);

int nvs_gte_init(struct device *dev, struct nvs_gte_irq *ngi, unsigned int n)
{
	int ret;

	ret = nvs_gte_init_gpio2irq(dev, ngi, n);
	return ret;
}
EXPORT_SYMBOL_GPL(nvs_gte_init);

int nvs_gte_sts(struct nvs_gte_irq *ngi, struct nvs_gte_sts *ngs)
{
	ngs->ver = NVS_GTE_VERSION;
	ngs->gte = "disabled";
	ngs->err = NULL;
	ngs->ts_ns = 0;
	ngs->ts_raw = 0;
	return 0;
}
EXPORT_SYMBOL_GPL(nvs_gte_sts);


#endif /* ! NVS_GTE */


MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("NVidia Sensor Generic Timestamp Engine module");
MODULE_AUTHOR("NVIDIA Corporation");