-
Notifications
You must be signed in to change notification settings - Fork 25
/
io-pwr.c
133 lines (115 loc) · 2.86 KB
/
io-pwr.c
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
/*
* KnCMiner IO Board power control
*
* Copyright (c) 2014 ORSoC AB
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include "tps65217.h"
#include "i2c.h"
static int opt_verbose = 0;
static int opt_bitbang = 0;
static int opt_attempts = 3;
static int opt_titan_voltage_fix = 0;
static void print_usage(const char *prog)
{
printf("Usage: %s [-v] [command,..]\n", prog);
puts(" -v --verbose Verbose operation\n"
" -b --bitbang Use bitbang GPIO for I2C\n"
" -a --attempts Use so many initializing attempts.\n"
" (< 0) => infinity. Default is 3.\n"
" --titan-voltage-fix\n"
" init Initialize I/O power\n"
);
exit(1);
}
static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = {
{ "bitbang", 0, 0, 'b' },
{ "verbose", 0, 0, 'v' },
{ "attempts", 1, 0, 'a' },
{ "titan-voltage-fix", 0, 0, 1000 },
{ NULL, 0, 0, 0 },
};
int c;
c = getopt_long(argc, argv, "vba:", lopts, NULL);
if (c == -1)
break;
switch (c) {
case 'v':
opt_verbose = 1;
break;
case 'b':
opt_bitbang = 1;
break;
case 'a':
opt_attempts = atoi(optarg);
if (0 != opt_attempts)
break;
printf("Invalid number of attempts: %s\n", optarg);
print_usage(argv[0]);
break;
case 1000:
opt_titan_voltage_fix = 1;
break;
default:
print_usage(argv[0]);
break;
}
}
}
static int io_pwr_init(void)
{
int i2c_bus = -1;
if (!opt_bitbang) {
if (0 > (i2c_bus = i2c_connect(TPS65217_BUS_NUM)))
exit(1);
i2c_set_slave_device_addr(i2c_bus, TPS65217_BUS_ADDRESS);
}
if (opt_titan_voltage_fix) {
if (opt_verbose) fprintf(stderr, "Titan voltage fix enabled!\n");
}
if (opt_verbose) fprintf(stderr, "Testing TPS65217\n");
if(!test_tps65217(i2c_bus)) {
fprintf(stderr, "TPS65217 TEST failure\n");
return 1;
}
/* Try to configure DC/DC several times */
int try;
bool success = false;
for (try = 1; (try <= opt_attempts) || (opt_attempts < 0); ++try) {
if (opt_verbose) fprintf(stderr, "Configuring TPS65217 try %d\n", try);
if (configure_tps65217(i2c_bus, opt_titan_voltage_fix)) {
success = true;
break;
}
}
if (!success) {
fprintf(stderr, "DC/DC converter configuration failed!\n");
return 1;
}
if (0 <= i2c_bus)
i2c_disconnect(i2c_bus);
if (opt_verbose) fprintf(stderr, "TPS65217 successfully initialized\n");
return 0;
}
int main(int argc, char *argv[])
{
parse_opts(argc, argv);
if (optind >= argc) {
print_usage(argv[0]);
return 0;
}
if (strcmp(argv[optind], "init") == 0)
return io_pwr_init();
fprintf(stderr, "Unknown command '%s'\n", argv[optind]);
return 1;
}