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
|
/* $OpenBSD: rkpmic.c,v 1.6 2018/07/31 10:09:25 kettenis Exp $ */
/*
* Copyright (c) 2017 Mark Kettenis <kettenis@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_regulator.h>
#include <dev/ofw/fdt.h>
#include <dev/i2c/i2cvar.h>
#include <dev/clock_subr.h>
extern todr_chip_handle_t todr_handle;
#define RK808_SECONDS 0x00
#define RK808_MINUTES 0x01
#define RK808_HOURS 0x02
#define RK808_DAYS 0x03
#define RK808_MONTHS 0x04
#define RK808_YEARS 0x05
#define RK808_WEEKS 0x06
#define RK808_RTC_CTRL 0x10
#define RK808_RTC_CTRL_STOP_RTC 0x01
#define RK808_RTC_STATUS 0x11
#define RK808_RTC_STATUS_POWER_UP 0x80
#define RK808_NRTC_REGS 7
struct rkpmic_regdata {
const char *name;
uint8_t reg, mask;
uint32_t base, delta;
};
struct rkpmic_regdata rk805_regdata[] = {
{ "DCDC_REG1", 0x2f, 0x3f, 712500, 12500 },
{ "DCDC_REG2", 0x33, 0x3f, 712500, 12500 },
{ "DCDC_REG4", 0x38, 0x1f, 800000, 100000 },
{ "LDO_REG1", 0x3b, 0x1f, 800000, 100000 },
{ "LDO_REG2", 0x3d, 0x1f, 800000, 100000 },
{ "LDO_REG3", 0x3f, 0x1f, 800000, 100000 },
{ }
};
struct rkpmic_regdata rk808_regdata[] = {
{ "DCDC_REG1", 0x2f, 0x3f, 712500, 12500 },
{ "DCDC_REG2", 0x33, 0x3f, 712500, 12500 },
{ "DCDC_REG4", 0x38, 0x0f, 1800000, 100000 },
{ "LDO_REG1", 0x3b, 0x1f, 1800000, 100000 },
{ "LDO_REG2", 0x3d, 0x1f, 1800000, 100000 },
{ "LDO_REG3", 0x3f, 0x0f, 800000, 100000 },
{ "LDO_REG4", 0x41, 0x1f, 1800000, 100000 },
{ "LDO_REG5", 0x43, 0x1f, 1800000, 100000 },
{ "LDO_REG6", 0x45, 0x1f, 800000, 100000 },
{ "LDO_REG7", 0x47, 0x1f, 800000, 100000 },
{ "LDO_REG8", 0x49, 0x1f, 1800000, 100000 },
{ }
};
struct rkpmic_softc {
struct device sc_dev;
i2c_tag_t sc_tag;
i2c_addr_t sc_addr;
struct todr_chip_handle sc_todr;
struct rkpmic_regdata *sc_regdata;
};
int rkpmic_match(struct device *, void *, void *);
void rkpmic_attach(struct device *, struct device *, void *);
struct cfattach rkpmic_ca = {
sizeof(struct rkpmic_softc), rkpmic_match, rkpmic_attach
};
struct cfdriver rkpmic_cd = {
NULL, "rkpmic", DV_DULL
};
void rkpmic_attach_regulator(struct rkpmic_softc *, int);
uint8_t rkpmic_reg_read(struct rkpmic_softc *, int);
void rkpmic_reg_write(struct rkpmic_softc *, int, uint8_t);
int rkpmic_clock_read(struct rkpmic_softc *, struct clock_ymdhms *);
int rkpmic_clock_write(struct rkpmic_softc *, struct clock_ymdhms *);
int rkpmic_gettime(struct todr_chip_handle *, struct timeval *);
int rkpmic_settime(struct todr_chip_handle *, struct timeval *);
int
rkpmic_match(struct device *parent, void *match, void *aux)
{
struct i2c_attach_args *ia = aux;
int node = *(int *)ia->ia_cookie;
return (OF_is_compatible(node, "rockchip,rk805") ||
OF_is_compatible(node, "rockchip,rk808"));
}
void
rkpmic_attach(struct device *parent, struct device *self, void *aux)
{
struct rkpmic_softc *sc = (struct rkpmic_softc *)self;
struct i2c_attach_args *ia = aux;
int node = *(int *)ia->ia_cookie;
const char *chip;
sc->sc_tag = ia->ia_tag;
sc->sc_addr = ia->ia_addr;
sc->sc_todr.cookie = sc;
sc->sc_todr.todr_gettime = rkpmic_gettime;
sc->sc_todr.todr_settime = rkpmic_settime;
if (todr_handle == NULL)
todr_handle = &sc->sc_todr;
if (OF_is_compatible(node, "rockchip,rk805")) {
chip = "RK805";
sc->sc_regdata = rk805_regdata;
} else {
chip = "RK808";
sc->sc_regdata = rk808_regdata;
}
printf(": %s\n", chip);
node = OF_getnodebyname(node, "regulators");
if (node == 0)
return;
for (node = OF_child(node); node; node = OF_peer(node))
rkpmic_attach_regulator(sc, node);
}
struct rkpmic_regulator {
struct rkpmic_softc *rr_sc;
uint8_t rr_reg, rr_mask;
uint32_t rr_base, rr_delta;
struct regulator_device rr_rd;
};
uint32_t rkpmic_get_voltage(void *);
int rkpmic_set_voltage(void *, uint32_t);
void
rkpmic_attach_regulator(struct rkpmic_softc *sc, int node)
{
struct rkpmic_regulator *rr;
char name[32];
int i;
name[0] = 0;
OF_getprop(node, "name", name, sizeof(name));
name[sizeof(name) - 1] = 0;
for (i = 0; sc->sc_regdata[i].name; i++) {
if (strcmp(sc->sc_regdata[i].name, name) == 0)
break;
}
if (sc->sc_regdata[i].name == NULL)
return;
rr = malloc(sizeof(*rr), M_DEVBUF, M_WAITOK | M_ZERO);
rr->rr_sc = sc;
rr->rr_reg = sc->sc_regdata[i].reg;
rr->rr_mask = sc->sc_regdata[i].mask;
rr->rr_base = sc->sc_regdata[i].base;
rr->rr_delta = sc->sc_regdata[i].delta;
rr->rr_rd.rd_node = node;
rr->rr_rd.rd_cookie = rr;
rr->rr_rd.rd_get_voltage = rkpmic_get_voltage;
rr->rr_rd.rd_set_voltage = rkpmic_set_voltage;
regulator_register(&rr->rr_rd);
}
uint32_t
rkpmic_get_voltage(void *cookie)
{
struct rkpmic_regulator *rr = cookie;
uint8_t vsel;
vsel = rkpmic_reg_read(rr->rr_sc, rr->rr_reg);
return rr->rr_base + (vsel & rr->rr_mask) * rr->rr_delta;
}
int
rkpmic_set_voltage(void *cookie, uint32_t voltage)
{
struct rkpmic_regulator *rr = cookie;
uint32_t vmin = rr->rr_base;
uint32_t vmax = vmin + rr->rr_mask * rr->rr_delta;
uint8_t vsel;
if (voltage < vmin || voltage > vmax)
return EINVAL;
vsel = rkpmic_reg_read(rr->rr_sc, rr->rr_reg);
vsel &= ~rr->rr_mask;
vsel |= (voltage - rr->rr_base) / rr->rr_delta;
rkpmic_reg_write(rr->rr_sc, rr->rr_reg, vsel);
return 0;
}
int
rkpmic_gettime(struct todr_chip_handle *handle, struct timeval *tv)
{
struct rkpmic_softc *sc = handle->cookie;
struct clock_ymdhms dt;
time_t secs;
int error;
error = rkpmic_clock_read(sc, &dt);
if (error)
return error;
if (dt.dt_sec > 59 || dt.dt_min > 59 || dt.dt_hour > 23 ||
dt.dt_day > 31 || dt.dt_day == 0 ||
dt.dt_mon > 12 || dt.dt_mon == 0 ||
dt.dt_year < POSIX_BASE_YEAR)
return EINVAL;
/*
* The RTC thinks November has 31 days. Match what Linux does
* and undo the damage by considering the calenders to be in
* sync on January 1st 2016.
*/
secs = clock_ymdhms_to_secs(&dt);
secs += (dt.dt_year - 2016 + (dt.dt_mon == 12 ? 1 : 0)) * 86400;
tv->tv_sec = secs;
tv->tv_usec = 0;
return 0;
}
int
rkpmic_settime(struct todr_chip_handle *handle, struct timeval *tv)
{
struct rkpmic_softc *sc = handle->cookie;
struct clock_ymdhms dt;
time_t secs;
/*
* Take care of the November 31st braindamage here as well.
* Don't try to be clever, just do the conversion in two
* steps, first taking care of November 31 in previous years,
* and then taking care of days in December of the current
* year. Decmber 1st turns into November 31st!
*/
secs = tv->tv_sec;
clock_secs_to_ymdhms(secs, &dt);
secs -= (dt.dt_year - 2016) * 86400;
clock_secs_to_ymdhms(secs, &dt);
if (dt.dt_mon == 12) {
dt.dt_day--;
if (dt.dt_day == 0) {
dt.dt_mon = 11;
dt.dt_day = 31;
}
}
return rkpmic_clock_write(sc, &dt);
}
uint8_t
rkpmic_reg_read(struct rkpmic_softc *sc, int reg)
{
uint8_t cmd = reg;
uint8_t val;
int error;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
&cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
iic_release_bus(sc->sc_tag, I2C_F_POLL);
if (error) {
printf("%s: can't read register 0x%02x\n",
sc->sc_dev.dv_xname, reg);
val = 0xff;
}
return val;
}
void
rkpmic_reg_write(struct rkpmic_softc *sc, int reg, uint8_t val)
{
uint8_t cmd = reg;
int error;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
&cmd, sizeof cmd, &val, sizeof val, I2C_F_POLL);
iic_release_bus(sc->sc_tag, I2C_F_POLL);
if (error) {
printf("%s: can't write register 0x%02x\n",
sc->sc_dev.dv_xname, reg);
}
}
int
rkpmic_clock_read(struct rkpmic_softc *sc, struct clock_ymdhms *dt)
{
uint8_t regs[RK808_NRTC_REGS];
uint8_t cmd = RK808_SECONDS;
uint8_t status;
int error;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
error = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
&cmd, sizeof(cmd), regs, RK808_NRTC_REGS, I2C_F_POLL);
iic_release_bus(sc->sc_tag, I2C_F_POLL);
if (error) {
printf("%s: can't read RTC\n", sc->sc_dev.dv_xname);
return error;
}
/*
* Convert the RK808's register values into something useable.
*/
dt->dt_sec = FROMBCD(regs[0]);
dt->dt_min = FROMBCD(regs[1]);
dt->dt_hour = FROMBCD(regs[2]);
dt->dt_day = FROMBCD(regs[3]);
dt->dt_mon = FROMBCD(regs[4]);
dt->dt_year = FROMBCD(regs[5]) + 2000;
/* Consider the time to be invalid if the POWER_UP bit is set. */
status = rkpmic_reg_read(sc, RK808_RTC_STATUS);
if (status & RK808_RTC_STATUS_POWER_UP)
return EINVAL;
return 0;
}
int
rkpmic_clock_write(struct rkpmic_softc *sc, struct clock_ymdhms *dt)
{
uint8_t regs[RK808_NRTC_REGS];
uint8_t cmd = RK808_SECONDS;
int error;
/*
* Convert our time representation into something the RK808
* can understand.
*/
regs[0] = TOBCD(dt->dt_sec);
regs[1] = TOBCD(dt->dt_min);
regs[2] = TOBCD(dt->dt_hour);
regs[3] = TOBCD(dt->dt_day);
regs[4] = TOBCD(dt->dt_mon);
regs[5] = TOBCD(dt->dt_year - 2000);
regs[6] = TOBCD(dt->dt_wday);
/* Stop RTC such that we can write to it. */
rkpmic_reg_write(sc, RK808_RTC_CTRL, RK808_RTC_CTRL_STOP_RTC);
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
error = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
&cmd, sizeof(cmd), regs, RK808_NRTC_REGS, I2C_F_POLL);
iic_release_bus(sc->sc_tag, I2C_F_POLL);
/* Restart RTC. */
rkpmic_reg_write(sc, RK808_RTC_CTRL, 0);
if (error) {
printf("%s: can't write RTC\n", sc->sc_dev.dv_xname);
return error;
}
/* Clear POWER_UP bit to indicate the time is now valid. */
rkpmic_reg_write(sc, RK808_RTC_STATUS, RK808_RTC_STATUS_POWER_UP);
return 0;
}
|