summaryrefslogtreecommitdiffstats
path: root/drivers/misc/mods/mods_tegraprod.c
blob: b1e84b49883a9778910caa8cd6d3619b51edfa66 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/*
 * mods_tegraprod.c - This file is part of NVIDIA MODS kernel driver.
 *
 * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA MODS kernel driver is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * NVIDIA MODS kernel driver 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 NVIDIA MODS kernel driver.
 * If not, see <http://www.gnu.org/licenses/>.
 */

#include "mods_internal.h"
#include <linux/err.h>
#include <linux/miscdevice.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/tegra_prod.h>

#define MAX_REG_INFO_ENTRY	400
#define MAX_IO_MAP_ENTRY	200

static struct device *mods_tegra_prod_dev;

/**
 * mods_tegra_prod_init - Initialize tegra prod module.
 * @misc_dev:       pointer to mods device
 *
 * Returns 0 on success, others for error.
 */
int mods_tegra_prod_init(const struct miscdevice *misc_dev)
{
	if (!misc_dev) {
		mods_error_printk("mods device pointer is NULL\n");
		return -EINVAL;
	}

	mods_tegra_prod_dev = misc_dev->this_device;
	if (!mods_tegra_prod_dev) {
		mods_error_printk("this_device in mods device is NULL\n");
		return -EINVAL;
	}

	return OK;
}

/**
 * esc_mods_tegra_prod_iterate_dt - Find device_node by device name.
 * @MODS_TEGRA_PROD_ITERATOR: Input information, only the following
 *				members are used.
 *   +device_handle:	Last node pointer for current iteration
 *   +name:		Current node name
 *   +next_name:	Next node name
 *   +index:		Index of the device nodes shared the same name
 *			(NOTE: index starts from 0)
 *   +is_leaf:		Is the node with current name a leaf node
 *   +next_device_handle: Result node pointer for next iteration
 * Returns 0 on success, others for error or no matching node found.
 */
int esc_mods_tegra_prod_iterate_dt(
	struct file *fp,
	struct MODS_TEGRA_PROD_ITERATOR *iterator
)
{
	struct device_node *dev_node;
	struct device_node *child_node = NULL;
	struct resource res;
	int ret_resource = -1;
	int i;

	dev_node = (struct device_node *)iterator->device_handle;

	if (!iterator->name[0]) {
		mods_error_printk(
			"node name is missing for tegra prod value\n");
		return -EINVAL;
	}
	if (!iterator->next_name[0] && !iterator->is_leaf) {
		mods_error_printk("inner node with empty next_name\n");
		return -EINVAL;
	}

	/* Need to search several times since some nodes may share same name */
	for (i = 0; i <= iterator->index; ++i) {
		/* Search from Left to Right in DT */
		dev_node = of_find_node_by_name(dev_node, iterator->name);
		if (!dev_node) {
			mods_error_printk("node %s not found in device tree\n",
					iterator->name);
			return -EINVAL;
		}

		/*
		 * Search from Top to Bottom in DT
		 * Check: does this node need to be filtered out
		 */
		if (iterator->is_leaf) {
			/* Leaf node : search for "prod-settings" node */
			child_node = of_get_child_by_name(dev_node,
							"prod-settings");
			ret_resource = of_address_to_resource(
							dev_node, 0, &res);
		} else {
			/* Inner node : search for next device node */
			child_node = of_get_child_by_name(dev_node,
							iterator->next_name);
			ret_resource = -1;
		}
		if (!child_node && ret_resource < 0)
			--i;	/* Not a valid device node */
	}

	/* Return next_device_handler to ioctl caller */
	iterator->next_device_handle = (__u64)dev_node;
	return OK;
}

/**
 * mods_read_reg_info - Read register information (address and size)
 * @dev_node:		 Device node pointer
 * @reg_info:		 Register information array
 * @count_reg_cells:	 (OUT) Pointer to count of cells in "reg"
 * @count_addrsize_pair: (OUT) Pointer to count of each pair in "reg"
 * @count_address_cells: (OUT) Pointer to count of "address" cell in "reg"
 * @count_size_cells:	 (OUT) Pointer to count of "size" cell in "reg"
 *
 * Return Value :
 *    0   - OK
 * Others - ERROR
 *
 */
static int mods_read_reg_info(
	const struct device_node *dev_node,
	__u32 *reg_info,
	__u32 *count_reg_cells,
	__u32 *count_addrsize_pair,
	__u32 *count_address_cells,
	__u32 *count_size_cells
)
{
	struct device_node *parent_node;
	int ret;

	/* Get parent node to read the format of "reg" property */
	parent_node = dev_node->parent;
	ret = of_property_read_u32(parent_node, "#address-cells",
				count_address_cells);
	if (ret < 0) {
		mods_error_printk("Read #address-cells failed\n");
		return ret;

	} else if (*count_address_cells == 0) {
		mods_error_printk("#address-cells cannot be 0\n");
		return -EINVAL;
	}

	ret = of_property_read_u32(parent_node, "#size-cells",
				count_size_cells);
	if (ret < 0) {
		mods_error_printk("Read #size-cells failed\n");
		return ret;

	} else if (*count_size_cells == 0) {
		mods_error_printk("#size-cells cannot be 0\n");
		return -EINVAL;
	}
	*count_addrsize_pair = *count_address_cells + *count_size_cells;

	/* Read count of cells in "reg" property */
	ret = of_property_count_u32_elems(dev_node, "reg");
	if (ret < 0) {
		mods_error_printk(
			"Unable to get count of cells in \"reg\" of node %s\n",
			dev_node->name);
		return ret;
	}

	*count_reg_cells = (__u32)ret;
	if (*count_reg_cells == 0) {
		mods_error_printk("No \"reg\" info is available\n");
		return -EINVAL;

	} else if (*count_reg_cells % *count_addrsize_pair != 0) {
		/*
		 * "reg" property may have more than 1 pairs of address
		 * and size. The length of each pair is represented by
		 * "count_addrsize_pair"; the total length of "reg"
		 * property is represented by "count_reg_cells". If
		 * "count_reg_cells" is not an integral multiple of
		 * "count_addrsize_pair", the read "reg" information is
		 * incomplete or incorrect.
		 */
		mods_error_printk(
			"\"reg\" property has invalid length : %d\n",
			*count_reg_cells);
		return -EINVAL;
	}

	/* Read register information from "reg" property */
	ret = of_property_read_u32_array(dev_node, "reg", reg_info,
					(size_t)(*count_reg_cells));
	if (ret < 0) {
		mods_error_printk(
			"Unable to read \"reg\" property of node %s\n",
			dev_node->name);
		return ret;
	}

	return OK;
}

/**
 * mods_batch_iounmap - Unmap all virtual memory mapped by IO address.
 * @io_base:		IO address array
 * @count_io_base:	Count of IO addresses are mapped
 *
 */
static void mods_batch_iounmap(
	void __iomem **io_base,
	__u32 count_io_base
)
{
	int i;

	if (!io_base) {
		mods_error_printk("IO base address array is NULL\n");
		return;
	}

	for (i = 0; i < count_io_base; ++i)
		iounmap(io_base[i]);
}

/**
 * mods_batch_iomap - Get all segments of IO address from a device node,
 *                    and create a virtual memory mapping.
 * @dev_node:		Device node pointer
 * @io_base:		(OUT) IO address array
 * @count_io_base:	(OUT) Pointer to the count of IO addresses are mapped
 *
 * Return Value :
 *    0   - OK
 * Others - ERROR (NOTE: partially mapped IO memory will be unmapped on error)
 *
 */
static int mods_batch_iomap(
	const struct device_node *dev_node,
	void __iomem **io_base,
	__u32 *count_io_base
)
{
	__u32 reg_info[MAX_REG_INFO_ENTRY];
	__u32 count_reg_cells = 0;
	__u32 count_addrsize_pair, count_address_cells, count_size_cells;
	__u64 io_addr_base, io_addr_length;
	void __iomem *io_addr_mapped;
	int ret;
	int i;

	/* Check arguments */
	if (!dev_node) {
		mods_error_printk("Controller device handle is NULL\n");
		return -EINVAL;
	}
	if (!io_base) {
		mods_error_printk("IO base address array is NULL\n");
		return -EINVAL;
	}
	if (!count_io_base) {
		mods_error_printk("Count of IO base address array is NULL\n");
		return -EINVAL;
	}

	/* Get registers information */
	ret = mods_read_reg_info(
		dev_node,
		reg_info,
		&count_reg_cells,
		&count_addrsize_pair,
		&count_address_cells,
		&count_size_cells
	);
	if (ret < 0)
		return ret;

	/* IO mapping - using 64-bit physical address */
	*count_io_base = 0;
	for (i = 0; i < count_reg_cells;
		i += count_addrsize_pair, *count_io_base += 1) {

		/* Compute IO physical address */
		/* Lower 32-bit IO physical address in DTB */
		io_addr_base = reg_info[i + count_address_cells - 1];
		if (count_address_cells > 1) {
			/* Higher 32-bit IO physical address in DTB */
			io_addr_base += ((__u64)reg_info
					[i + count_address_cells - 2]) << 32;
		}

		/* Compute IO address space length */
		/* Lower 32-bit IO address space length in DTB */
		io_addr_length = reg_info[i + count_addrsize_pair - 1];
		if (count_size_cells > 1) {
			/* Higher 32-bit IO address space length in DTB */
			io_addr_length += ((__u64)reg_info
					[i + count_addrsize_pair - 2]) << 32;
		}

		/* Map physical address to virtual address space */
		io_addr_mapped = ioremap((resource_size_t)io_addr_base,
					(resource_size_t)io_addr_length);
		if (io_addr_mapped == NULL) {
			mods_error_printk(
			"Unable to map io address 0x%llx, length 0x%llx\n",
			io_addr_base, io_addr_length);
			/* Clean :
			 * Unmap the IO memory that have already been mapped
			 */
			mods_batch_iounmap(io_base, *count_io_base);
			return -ENXIO;
		}
		io_base[*count_io_base] = io_addr_mapped;
	}

	return OK;
}


/**
 * mods_tegra_get_prod_list - Get tegra_prod list by valid device node.
 * @dev_node:       Device node.
 *
 * Returns non-NULL on success, NULL on error.
 */
static struct tegra_prod *mods_tegra_get_prod_list(struct device_node *dev_node)
{
	struct tegra_prod *prod_list;

	if (!mods_tegra_prod_dev) {
		mods_error_printk("tegra prod is not initialized\n");
		return NULL;
	}

	if (!dev_node) {
		mods_error_printk("device node is NULL\n");
		return NULL;
	}

	prod_list = devm_tegra_prod_get_from_node(mods_tegra_prod_dev,
						dev_node);
	if (IS_ERR(prod_list)) {
		mods_error_printk("failed to get prod_list : %s\n",
				dev_node->name);
		return NULL;
	}

	return prod_list;
}

/**
 * mods_tegra_get_prod_info - Get tegra_prod_list & mapped IO addresses
 * @MODS_TEGRA_PROD_SET_TUPLE:  Input information, only the following
 *				members are used.
 *   +prod_node_handle:	Handle of device node contained prod values.
 *   +ctrl_node_handle:	Handle of controller device node.
 * @tegra_prod_list:	(OUT) Pointer to tegra_prod_list.
 * @ctrl_base:		(OUT) Array of mapped IO address.
 * @count_ctrl_base:	(OUT) Pointer to count of mapped IO address.
 *
 * Return Value :
 *    0   - OK
 * Others - ERROR
 *
 */
static int mods_tegra_get_prod_info(
	const struct MODS_TEGRA_PROD_SET_TUPLE *tuple,
	struct tegra_prod **tegra_prod_list,
	void __iomem **ctrl_base,
	__u32 *count_ctrl_base
)
{
	struct device_node *prod_node, *ctrl_node;

	if (!tegra_prod_list || !ctrl_base || !count_ctrl_base) {
		mods_error_printk("Detected NULL pointer for out value.");
		return -EINVAL;
	}

	prod_node = (struct device_node *)tuple->prod_dev_handle;
	if (!prod_node) {
		mods_error_printk("Prod device handle is NULL\n");
		return -EINVAL;
	}

	*tegra_prod_list = mods_tegra_get_prod_list(prod_node);
	if (!(*tegra_prod_list)) {
		mods_error_printk(
		"Failed to get prod_list with prod handle 0x%llx\n",
		tuple->prod_dev_handle);
		return -EINVAL;
	}

	ctrl_node = (struct device_node *)tuple->ctrl_dev_handle;
	if (!ctrl_node) {
		mods_error_printk("Controller device handle is NULL\n");
		return -EINVAL;
	}

	return mods_batch_iomap(ctrl_node, ctrl_base, count_ctrl_base);
}

/**
 * esc_mods_tegra_prod_is_supported - Test the prod is supported by
 *					given prod name.
 * @MODS_TEGRA_PROD_IS_SUPPORTED:  Input information, only the following
				   members are used.
 *   +prod_node_handle:     Handle of device node contained prod values.
 *   +prod_name:            Prod name.
 *   +is_supported:         Result of supporting given prod configuration.
 *
 * Return Value :
 *    0   - OK
 * Others - ERROR
 *
 */
int esc_mods_tegra_prod_is_supported(
	struct file *fp,
	struct MODS_TEGRA_PROD_IS_SUPPORTED *tuple
)
{
	struct device_node *prod_node;
	struct tegra_prod *tegra_prod;
	bool is_supported;

	prod_node = (struct device_node *)tuple->prod_dev_handle;
	if (!prod_node) {
		mods_error_printk("Prod device handle is NULL\n");
		return -EINVAL;
	}

	tegra_prod = mods_tegra_get_prod_list(prod_node);
	if (!tegra_prod) {
		mods_error_printk(
		"Failed to get prod_list with prod handle 0x