summaryrefslogtreecommitdiff
path: root/bin/nv_aux.c
blob: e3956f1bf8fe9ca0a64fec630d9418ca355e2c21 (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
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>

#include <nvif/client.h>
#include <nvif/device.h>
#include <nvif/class.h>

#include "util.h"

static void
print_aux(struct nvkm_i2c_aux *aux)
{
	printf("aux %04x\n", aux->id);
}

int
main(int argc, char **argv)
{
	struct nvif_client client;
	struct nvif_device device;
	struct nvkm_i2c_aux *aux;
	struct nvkm_i2c *i2c;
	int action = -1, index = -1;
	int addr = -1, val = -1;
	int ret, c;
	u8 data;

	while ((c = getopt(argc, argv, "-"U_GETOPT)) != -1) {
		switch (c) {
		case 1:
			if (action < 0) {
				if (!strcasecmp(optarg, "rd"))
					action = 0;
				else
				if (!strcasecmp(optarg, "wr"))
					action = 1;
				else
					return -EINVAL;
			} else
			if (action >= 0 && index < 0) {
				index = strtoul(optarg, NULL, 0);
			} else
			if (action >= 0 && addr < 0) {
				addr = strtoul(optarg, NULL, 0);
				if (addr > 0x000fffff)
					return -EINVAL;
			} else
			if (action >= 1 && val < 0) {
				val = strtoul(optarg, NULL, 0);
				if (val > 0xff)
					return -EINVAL;
				data = val;
			} else
				return -EINVAL;
			break;
		default:
			if (!u_option(c))
				return 1;
			break;
		}
	}

	ret = u_device("lib", argv[0], "error", true, true,
		       (1ULL << NVDEV_SUBDEV_VBIOS) |
		       (1ULL << NVDEV_SUBDEV_I2C),
		       0x00000000, &client, &device);
	if (ret)
		return ret;

	i2c = nvxx_i2c(&device);

	if (action < 0) {
		list_for_each_entry(aux, &i2c->aux, head) {
			print_aux(aux);
		}
	} else {
		aux = nvkm_i2c_aux_find(i2c, index);
		if (!aux) {
			ret = -ENOENT;
			goto done;
		}

		print_aux(aux);
	}

	switch (action) {
	case 0:
		ret = nvkm_rdaux(aux, addr, &data, 1);
		printf("%05x: ", addr);
		if (ret < 0)
			printf("%s\n", strerror(ret));
		else
			printf("%02x\n", data);
		break;
	case 1:
		printf("%05x: %02x", addr, data);
		ret = nvkm_wraux(aux, addr, &data, 1);
		if (ret < 0)
			printf(" - %s", strerror(ret));
		printf("\n");
		break;
	}

done:
	nvif_device_fini(&device);
	nvif_client_fini(&client);
	return ret;
}