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
|
/* $NetBSD: act8846.c,v 1.6 2019/07/27 16:02:27 thorpej Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
//#define ACT_DEBUG
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: act8846.c,v 1.6 2019/07/27 16:02:27 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/act8846.h>
#define ACT_BATTVOL_STATUS_REG 0x00
#define ACT_THERMAL_CTRL_REG 0x01
#define ACT_DCDC1_BASE_REG 0x10
#define ACT_DCDC2_BASE_REG 0x20
#define ACT_DCDC3_BASE_REG 0x30
#define ACT_DCDC4_BASE_REG 0x40
#define ACT_LDO1_BASE_REG 0x50
#define ACT_LDO2_BASE_REG 0x58
#define ACT_LDO3_BASE_REG 0x60
#define ACT_LDO4_BASE_REG 0x68
#define ACT_LDO5_BASE_REG 0x70
#define ACT_LDO6_BASE_REG 0x80
#define ACT_LDO7_BASE_REG 0x90
#define ACT_LDO8_BASE_REG 0xa0
#define ACT_LDO9_BASE_REG 0xb0
#define ACT_VSET0_OFFSET 0
#define ACT_VSET1_OFFSET 1
#define ACT_DCDC_CTRL_OFFSET 2
#define ACT_LDO_CTRL_OFFSET 1
#define ACT_VSET_VSET __BITS(5,0)
#define ACT_DCDC_CTRL_ON __BIT(7)
#define ACT_LDO_CTRL_ON __BIT(7)
enum act8846_ctrl_type {
ACT_CTRL_DCDC,
ACT_CTRL_LDO,
};
#define ACT_VOLTAGE_MIN 600
#define ACT_VOLTAGE_MAX 3900
struct act8846_ctrl {
device_t c_dev;
const char * c_name;
u_int c_min;
u_int c_max;
uint8_t c_base;
enum act8846_ctrl_type c_type;
};
#define ACT_CTRL(name, base, type) \
{ .c_name = (name), \
.c_min = ACT_VOLTAGE_MIN, .c_max = ACT_VOLTAGE_MAX, \
.c_base = ACT_ ## base ## _BASE_REG, .c_type = (type) }
#define ACT_DCDC(name, base) ACT_CTRL(name, base, ACT_CTRL_DCDC)
#define ACT_LDO(name, base) ACT_CTRL(name, base, ACT_CTRL_LDO)
static const struct act8846_ctrl act8846_ctrls[] = {
ACT_DCDC("DCDC1", DCDC1), /* VCC_DDR */
ACT_DCDC("DCDC2", DCDC2), /* VDD_LOG */
ACT_DCDC("DCDC3", DCDC3), /* VDD_ARM */
ACT_DCDC("DCDC4", DCDC4), /* VCC_IO */
ACT_LDO("LDO1", LDO1), /* VDD_10 */
ACT_LDO("LDO2", LDO2), /* VCC_25 */
ACT_LDO("LDO3", LDO3), /* VCC18_CIF */
ACT_LDO("LDO4", LDO4), /* VCCA_33 */
ACT_LDO("LDO5", LDO5), /* VCC_TOUCH */
ACT_LDO("LDO6", LDO6), /* VCC33 */
ACT_LDO("LDO7", LDO7), /* VCC18_IO */
ACT_LDO("LDO8", LDO8), /* VCC28_CIF */
#if 0
ACT_LDO("LDO9", LDO9), /* VDD_RTC (Always-ON) */
#endif
};
/* From datasheet, Table 5: REGx/VSET[] Output Voltage Setting */
static const u_int act8846_vset[] = {
600, 625, 650, 675, 700, 725, 750, 775,
800, 825, 850, 875, 900, 925, 950, 975,
1000, 1025, 1050, 1075, 1100, 1125, 1150, 1175,
1200, 1250, 1300, 1350, 1400, 1450, 1500, 1550,
1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950,
2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350,
2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100,
3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900
};
struct act8846_softc {
device_t sc_dev;
i2c_tag_t sc_i2c;
i2c_addr_t sc_addr;
u_int sc_nctrl;
struct act8846_ctrl *sc_ctrl;
};
static int act8846_match(device_t, cfdata_t, void *);
static void act8846_attach(device_t, device_t, void *);
static int act8846_read(struct act8846_softc *, uint8_t, uint8_t *);
static int act8846_write(struct act8846_softc *, uint8_t, uint8_t);
static void act8846_print(struct act8846_ctrl *c);
CFATTACH_DECL_NEW(act8846pm, sizeof(struct act8846_softc),
act8846_match, act8846_attach, NULL, NULL);
static int
act8846_match(device_t parent, cfdata_t match, void *aux)
{
struct i2c_attach_args *ia = aux;
if (ia->ia_addr == 0x5a)
return I2C_MATCH_ADDRESS_ONLY;
return 0;
}
static void
act8846_attach(device_t parent, device_t self, void *aux)
{
struct act8846_softc *sc = device_private(self);
struct i2c_attach_args *ia = aux;
u_int n;
sc->sc_dev = self;
sc->sc_i2c = ia->ia_tag;
sc->sc_addr = ia->ia_addr;
aprint_naive("\n");
aprint_normal("\n");
sc->sc_nctrl = __arraycount(act8846_ctrls);
sc->sc_ctrl = kmem_alloc(sizeof(act8846_ctrls), KM_SLEEP);
memcpy(sc->sc_ctrl, act8846_ctrls, sizeof(act8846_ctrls));
for (n = 0; n < sc->sc_nctrl; n++) {
sc->sc_ctrl[n].c_dev = self;
}
for (n = 0; n < sc->sc_nctrl; n++) {
act8846_print(&sc->sc_ctrl[n]);
}
}
static int
act8846_read(struct act8846_softc *sc, uint8_t reg, uint8_t *val)
{
return iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
}
static int
act8846_write(struct act8846_softc *sc, uint8_t reg, uint8_t val)
{
return iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
}
static void
act8846_print(struct act8846_ctrl *c)
{
struct act8846_softc *sc = device_private(c->c_dev);
u_int voltage;
bool enabled;
device_printf(sc->sc_dev, "%s:", c->c_name);
if (act8846_get_voltage(c, &voltage)) {
printf(" [??? V]");
} else {
printf(" [%d.%03dV]", voltage / 1000,
voltage % 1000);
}
if (act8846_is_enabled(c, &enabled)) {
printf(" [unknown state]");
} else {
printf(" [%s]", enabled ? "ON" : "OFF");
}
printf("\n");
}
struct act8846_ctrl *
act8846_lookup(device_t dev, const char *name)
{
struct act8846_softc *sc = device_private(dev);
struct act8846_ctrl *c;
u_int n;
for (n = 0; n < sc->sc_nctrl; n++) {
c = &sc->sc_ctrl[n];
if (strcmp(c->c_name, name) == 0) {
return c;
}
}
return NULL;
}
int
act8846_set_voltage(struct act8846_ctrl *c, u_int min, u_int max)
{
struct act8846_softc *sc = device_private(c->c_dev);
uint8_t val;
int error, n;
if (min < c->c_min || min > c->c_max || (min % 25) != 0)
return EINVAL;
for (n = 0; n < __arraycount(act8846_vset); n++) {
if (min >= act8846_vset[n] && max <= act8846_vset[n]) {
break;
}
}
if (n == __arraycount(act8846_vset))
return EINVAL;
val = __SHIFTIN(n, ACT_VSET_VSET);
iic_acquire_bus(sc->sc_i2c, 0);
error = act8846_write(sc, c->c_base + ACT_VSET0_OFFSET, val);
iic_release_bus(sc->sc_i2c, 0);
#ifdef ACT_DEBUG
if (error == 0)
act8846_print(c);
#endif
return error;
}
int
act8846_get_voltage(struct act8846_ctrl *c, u_int *pvol)
{
struct act8846_softc *sc = device_private(c->c_dev);
uint8_t val;
int error;
iic_acquire_bus(sc->sc_i2c, 0);
error = act8846_read(sc, c->c_base + ACT_VSET0_OFFSET, &val);
iic_release_bus(sc->sc_i2c, 0);
if (error)
return error;
*pvol = act8846_vset[__SHIFTOUT(val, ACT_VSET_VSET)];
return 0;
}
int
act8846_is_enabled(struct act8846_ctrl *c, bool *penabled)
{
struct act8846_softc *sc = device_private(c->c_dev);
uint8_t val, regoff, regmask;
int error;
if (c->c_type == ACT_CTRL_DCDC) {
regoff = ACT_DCDC_CTRL_OFFSET;
regmask = ACT_DCDC_CTRL_ON;
} else {
regoff = ACT_LDO_CTRL_OFFSET;
regmask = ACT_LDO_CTRL_ON;
}
iic_acquire_bus(sc->sc_i2c, 0);
error = act8846_read(sc, c->c_base + regoff, &val);
iic_release_bus(sc->sc_i2c, 0);
if (error)
return error;
*penabled = !!(val & regmask);
return 0;
}
int
act8846_enable(struct act8846_ctrl *c)
{
struct act8846_softc *sc = device_private(c->c_dev);
uint8_t val, regoff, regmask;
int error;
if (c->c_type == ACT_CTRL_DCDC) {
regoff = ACT_DCDC_CTRL_OFFSET;
regmask = ACT_DCDC_CTRL_ON;
} else {
regoff = ACT_LDO_CTRL_OFFSET;
regmask = ACT_LDO_CTRL_ON;
}
iic_acquire_bus(sc->sc_i2c, 0);
if ((error = act8846_read(sc, c->c_base + regoff, &val)) != 0)
goto done;
val |= regmask;
error = act8846_write(sc, c->c_base + regoff, val);
done:
iic_release_bus(sc->sc_i2c, 0);
#ifdef ACT_DEBUG
if (error == 0)
act8846_print(c);
#endif
return error;
}
int
act8846_disable(struct act8846_ctrl *c)
{
struct act8846_softc *sc = device_private(c->c_dev);
uint8_t val, regoff, regmask;
int error;
if (c->c_type == ACT_CTRL_DCDC) {
regoff = ACT_DCDC_CTRL_OFFSET;
regmask = ACT_DCDC_CTRL_ON;
} else {
regoff = ACT_LDO_CTRL_OFFSET;
regmask = ACT_LDO_CTRL_ON;
}
iic_acquire_bus(sc->sc_i2c, 0);
if ((error = act8846_read(sc, c->c_base + regoff, &val)) != 0)
goto done;
val &= ~regmask;
error = act8846_write(sc, c->c_base + regoff, val);
done:
iic_release_bus(sc->sc_i2c, 0);
#ifdef ACT_DEBUG
if (error == 0)
act8846_print(c);
#endif
return error;
}
|