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
|
#include <common.h>
#include <command.h>
#include <linux/ctype.h>
#include <asm/errno.h>
#include "../../../include/i2c.h"
#include "../../wmt_battery.h"
#include "sp2541_battery.h"
static int i2c_bus = I2C_BUS_ID;
static int sp2541_read(u8 cmd, u8 reg, u8 *rt_value, unsigned int len)
{
struct i2c_msg_s msg[2];
unsigned char data[2] = {cmd, reg};
msg[0].addr = SP2541_I2C_ADDR;
msg[0].flags = 0;
msg[0].len = 2;
msg[0].buf = data;
msg[1].addr = SP2541_I2C_ADDR;
msg[1].flags = I2C_M_RD;
msg[1].len = len;
msg[1].buf = rt_value;
return wmt_i2c_transfer(msg, 2, i2c_bus);
}
static int sp2541_write(u8 cmd, u8 reg, u8 const buf)
{
struct i2c_msg_s msg;
char data[3] = {cmd, reg, buf};
msg.addr = SP2541_I2C_ADDR;
msg.flags = 0;
msg.len = 3;
msg.buf = data;
return wmt_i2c_transfer(&msg, 1, i2c_bus);
}
static int EEPROM_read_byte(int addr, u8 *data)
{
u8 addr_hi, addr_lo, i;
u8 tmp = -1;
addr_hi = (addr >> 8) & 0xFF;
addr_lo = addr & 0xFF;
if (sp2541_write(EEPROM_WRITE_CMD, 0x00, addr_lo) < 0)
return -1;
if (sp2541_write(EEPROM_WRITE_CMD, 0x01, addr_hi) < 0)
return -1;
if (sp2541_write(EEPROM_WRITE_CMD, 0x03, 0x06) < 0)
return -1;
for (i=0; i<10; i++) {
sp2541_read(EEPROM_READ_CMD, 0x03, &tmp, 1);
if (tmp == 0)
break;
}
if (i == 10)
return -1;
if (sp2541_read(EEPROM_READ_CMD, 0x02, data, 1) < 0)
return -1;
return 0;
}
static int EEPROM_write_byte(int addr, u8 data)
{
u8 addr_hi, addr_lo, i;
u8 tmp = -1;
addr_hi = (addr >> 8) & 0xFF;
addr_lo = addr & 0xFF;
if (sp2541_write(EEPROM_WRITE_CMD, 0x00, addr_lo) < 0)
return -1;
if (sp2541_write(EEPROM_WRITE_CMD, 0x01, addr_hi) < 0)
return -1;
if (sp2541_write(EEPROM_WRITE_CMD, 0x02, data) < 0)
return -1;
if (sp2541_write(EEPROM_WRITE_CMD, 0x03, 0x05) < 0)
return -1;
for (i=0; i<10; i++) {
sp2541_read(EEPROM_READ_CMD, 0x03, &tmp, 1);
if (tmp == 0)
break;
}
if (i == 10)
return -1;
return 0;
}
static int sp2541_init(void)
{
char *env, *p;
char *endp;
p = env = getenv("wmt.battery.param");
if (!p)
return -EINVAL;
if (prefixcmp(p, "sp2541"))
return -EINVAL;
p = strchr(env, ':');
p++;
i2c_bus = simple_strtol(p, &endp, 10);
if (*endp != ':')
return -EINVAL;
return 0;
}
static int sp2541_get_capacity(void)
{
unsigned char buf[2];
int ret;
ret = sp2541_read(RAM_READ_CMD, SP2541_REG_SOC, buf, 2);
if (ret<0) {
printf("error reading capacity\n");
return ret;
}
ret = buf[0] | buf[1] << 8;
printf("Current capacity: %d\%\n", ret);
return ret;
}
static int sp2541_get_voltage(void)
{
unsigned char buf[2];
int ret;
ret = sp2541_read(RAM_READ_CMD, SP2541_REG_VOLT, buf, 2);
if (ret<0) {
printf("error reading capacity\n");
return ret;
}
return buf[0] | buf[1] << 8;
}
static int sp2541_get_current(void)
{
unsigned char buf[2];
int ret;
ret = sp2541_read(RAM_READ_CMD, SP2541_REG_AI, buf, 2);
if (ret<0) {
printf("error reading capacity\n");
return ret;
}
return buf[0] | buf[1] << 8;
}
static int sp2541_check_bl(void)
{
int capacity;
capacity = sp2541_get_capacity();
if (capacity < 0)
return -1;
return (capacity < 3);
}
struct battery_dev sp2541_battery_dev = {
.name = "sp2541",
.is_gauge = 1,
.init = sp2541_init,
.get_capacity = sp2541_get_capacity,
.get_voltage = sp2541_get_voltage,
.get_current = sp2541_get_current,
.check_batlow = sp2541_check_bl,
};
static int do_sp2541(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
int i, j;
int capacity,voltage,current;
u8 data_buf;
int eeprom_data;
int ret;
if (sp2541_init())
return 0;
capacity = sp2541_get_capacity();
voltage = sp2541_get_voltage();
current = sp2541_get_current();
printf("capacity = %d\n", capacity);
printf("voltage = %d\n", voltage);
printf("current = %d\n", current);
/*for (i = 0; i < ARRAY_SIZE(default_table); i++) {
printf("{0x%x, %d, %x}\n", default_table[i].addr, default_table[i].len, default_table[i].data);
for (j = 0; j < default_table[i].len; j++) {
data_buf = (default_table[i].data >> (8*(default_table[i].len-j-1))) & 0xFF;
ret = EEPROM_write_byte(default_table[i].addr + j, data_buf);
if (ret<0) {
printf("error write eeprom.\n");
return 0;
}
}
}*/
for (i = 0; i < ARRAY_SIZE(default_table); i++) {
eeprom_data = 0;
for (j = 0; j < default_table[i].len; j++) {
ret = EEPROM_read_byte(default_table[i].addr + j, &data_buf);
if (ret<0) {
printf("error read eeprom.\n");
return 0;
}
eeprom_data = (eeprom_data << 8*j) | data_buf;
}
//printf("{0x%x, %d, %x}\n", default_table[i].addr, default_table[i].len, default_table[i].data);
printf("0x%x: %x\n", default_table[i].addr, eeprom_data);
}
return 0;
}
U_BOOT_CMD(
sp2541, 1, 1, do_sp2541,
"sp2541\n",
NULL
);
|