2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/clock.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/reboot.h>
38 #include <sys/mutex.h>
40 #include <machine/bus.h>
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/extres/regulator/regulator.h>
50 #include <arm64/rockchip/rk805reg.h>
53 #include "regdev_if.h"
55 MALLOC_DEFINE(M_RK805_REG, "RK805 regulator", "RK805 power regulator");
57 /* #define dprintf(sc, format, arg...) device_printf(sc->base_dev, "%s: " format, __func__, arg) */
58 #define dprintf(sc, format, arg...)
65 static struct ofw_compat_data compat_data[] = {
66 {"rockchip,rk805", RK805},
67 {"rockchip,rk808", RK808},
85 struct regnode *regnode;
87 struct rk805_regdef *def;
89 struct regnode_std_param *param;
93 TAILQ_ENTRY(reg_list) next;
94 struct rk805_reg_sc *reg;
100 struct resource * res[1];
102 struct intr_config_hook intr_hook;
103 enum rk_pmic_type type;
105 TAILQ_HEAD(, reg_list) regs;
109 static int rk805_regnode_status(struct regnode *regnode, int *status);
110 static int rk805_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
111 int max_uvolt, int *udelay);
112 static int rk805_regnode_get_voltage(struct regnode *regnode, int *uvolt);
114 static struct rk805_regdef rk805_regdefs[] = {
118 .enable_reg = RK805_DCDC_EN,
120 .voltage_reg = RK805_DCDC1_ON_VSEL,
121 .voltage_mask = 0x3F,
122 .voltage_min = 712500,
123 .voltage_max = 1450000,
124 .voltage_step = 12500,
130 .enable_reg = RK805_DCDC_EN,
132 .voltage_reg = RK805_DCDC2_ON_VSEL,
133 .voltage_mask = 0x3F,
134 .voltage_min = 712500,
135 .voltage_max = 1450000,
136 .voltage_step = 12500,
142 .enable_reg = RK805_DCDC_EN,
148 .enable_reg = RK805_DCDC_EN,
150 .voltage_reg = RK805_DCDC4_ON_VSEL,
151 .voltage_mask = 0x3F,
152 .voltage_min = 800000,
153 .voltage_max = 3500000,
154 .voltage_step = 100000,
160 .enable_reg = RK805_LDO_EN,
162 .voltage_reg = RK805_LDO1_ON_VSEL,
163 .voltage_mask = 0x1F,
164 .voltage_min = 800000,
165 .voltage_max = 3400000,
166 .voltage_step = 100000,
172 .enable_reg = RK805_LDO_EN,
174 .voltage_reg = RK805_LDO2_ON_VSEL,
175 .voltage_mask = 0x1F,
176 .voltage_min = 800000,
177 .voltage_max = 3400000,
178 .voltage_step = 100000,
184 .enable_reg = RK805_LDO_EN,
186 .voltage_reg = RK805_LDO3_ON_VSEL,
187 .voltage_mask = 0x1F,
188 .voltage_min = 800000,
189 .voltage_max = 3400000,
190 .voltage_step = 100000,
195 static struct rk805_regdef rk808_regdefs[] = {
199 .enable_reg = RK805_DCDC_EN,
201 .voltage_reg = RK805_DCDC1_ON_VSEL,
202 .voltage_mask = 0x3F,
203 .voltage_min = 712500,
204 .voltage_max = 1500000,
205 .voltage_step = 12500,
211 .enable_reg = RK805_DCDC_EN,
213 .voltage_reg = RK805_DCDC2_ON_VSEL,
214 .voltage_mask = 0x3F,
215 .voltage_min = 712500,
216 .voltage_max = 1500000,
217 .voltage_step = 12500,
221 /* BUCK3 voltage is calculated based on external resistor */
224 .enable_reg = RK805_DCDC_EN,
230 .enable_reg = RK805_DCDC_EN,
232 .voltage_reg = RK805_DCDC4_ON_VSEL,
234 .voltage_min = 1800000,
235 .voltage_max = 3300000,
236 .voltage_step = 100000,
242 .enable_reg = RK808_LDO_EN,
244 .voltage_reg = RK805_LDO1_ON_VSEL,
245 .voltage_mask = 0x1F,
246 .voltage_min = 1800000,
247 .voltage_max = 3400000,
248 .voltage_step = 100000,
254 .enable_reg = RK808_LDO_EN,
256 .voltage_reg = RK805_LDO2_ON_VSEL,
257 .voltage_mask = 0x1F,
258 .voltage_min = 1800000,
259 .voltage_max = 3400000,
260 .voltage_step = 100000,
266 .enable_reg = RK808_LDO_EN,
268 .voltage_reg = RK805_LDO3_ON_VSEL,
270 .voltage_min = 800000,
271 .voltage_max = 2500000,
272 .voltage_step = 100000,
278 .enable_reg = RK808_LDO_EN,
280 .voltage_reg = RK808_LDO4_ON_VSEL,
281 .voltage_mask = 0x1F,
282 .voltage_min = 1800000,
283 .voltage_max = 3400000,
284 .voltage_step = 100000,
290 .enable_reg = RK808_LDO_EN,
292 .voltage_reg = RK808_LDO5_ON_VSEL,
293 .voltage_mask = 0x1F,
294 .voltage_min = 1800000,
295 .voltage_max = 3400000,
296 .voltage_step = 100000,
302 .enable_reg = RK808_LDO_EN,
304 .voltage_reg = RK808_LDO6_ON_VSEL,
305 .voltage_mask = 0x1F,
306 .voltage_min = 800000,
307 .voltage_max = 2500000,
308 .voltage_step = 100000,
314 .enable_reg = RK808_LDO_EN,
316 .voltage_reg = RK808_LDO7_ON_VSEL,
317 .voltage_mask = 0x1F,
318 .voltage_min = 800000,
319 .voltage_max = 2500000,
320 .voltage_step = 100000,
326 .enable_reg = RK808_LDO_EN,
328 .voltage_reg = RK808_LDO8_ON_VSEL,
329 .voltage_mask = 0x1F,
330 .voltage_min = 1800000,
331 .voltage_max = 3400000,
332 .voltage_step = 100000,
337 .name = "SWITCH_REG1",
338 .enable_reg = RK805_DCDC_EN,
340 .voltage_min = 3000000,
341 .voltage_max = 3000000,
345 .name = "SWITCH_REG2",
346 .enable_reg = RK805_DCDC_EN,
348 .voltage_min = 3000000,
349 .voltage_max = 3000000,
354 rk805_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
358 err = iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT);
363 rk805_write(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
366 return (iicdev_writeto(dev, reg, data, size, IIC_INTRWAIT));
370 rk805_regnode_init(struct regnode *regnode)
372 struct rk805_reg_sc *sc;
373 struct regnode_std_param *param;
374 int rv, udelay, uvolt, status;
376 sc = regnode_get_softc(regnode);
377 dprintf(sc, "Regulator %s init called\n", sc->def->name);
378 param = regnode_get_stdparam(regnode);
379 if (param->min_uvolt == 0)
382 /* Check that the regulator is preset to the correct voltage */
383 rv = rk805_regnode_get_voltage(regnode, &uvolt);
387 if (uvolt >= param->min_uvolt && uvolt <= param->max_uvolt)
390 * Set the regulator at the correct voltage if it is not enabled.
391 * Do not enable it, this is will be done either by a
392 * consumer or by regnode_set_constraint if boot_on is true
394 rv = rk805_regnode_status(regnode, &status);
395 if (rv != 0 || status == REGULATOR_STATUS_ENABLED)
398 rv = rk805_regnode_set_voltage(regnode, param->min_uvolt,
399 param->max_uvolt, &udelay);
407 rk805_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
409 struct rk805_reg_sc *sc;
412 sc = regnode_get_softc(regnode);
414 dprintf(sc, "%sabling regulator %s\n",
415 enable ? "En" : "Dis",
417 rk805_read(sc->base_dev, sc->def->enable_reg, &val, 1);
419 val |= sc->def->enable_mask;
421 val &= ~sc->def->enable_mask;
422 rk805_write(sc->base_dev, sc->def->enable_reg, &val, 1);
430 rk805_regnode_reg_to_voltage(struct rk805_reg_sc *sc, uint8_t val, int *uv)
432 if (val < sc->def->voltage_nstep)
433 *uv = sc->def->voltage_min + val * sc->def->voltage_step;
435 *uv = sc->def->voltage_min +
436 (sc->def->voltage_nstep * sc->def->voltage_step);
440 rk805_regnode_voltage_to_reg(struct rk805_reg_sc *sc, int min_uvolt,
441 int max_uvolt, uint8_t *val)
447 uvolt = sc->def->voltage_min;
449 for (nstep = 0; nstep < sc->def->voltage_nstep && uvolt < min_uvolt;
452 uvolt += sc->def->voltage_step;
454 if (uvolt > max_uvolt)
462 rk805_regnode_status(struct regnode *regnode, int *status)
464 struct rk805_reg_sc *sc;
467 sc = regnode_get_softc(regnode);
470 rk805_read(sc->base_dev, sc->def->enable_reg, &val, 1);
471 if (val & sc->def->enable_mask)
472 *status = REGULATOR_STATUS_ENABLED;
478 rk805_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
479 int max_uvolt, int *udelay)
481 struct rk805_reg_sc *sc;
485 sc = regnode_get_softc(regnode);
487 if (!sc->def->voltage_step)
490 dprintf(sc, "Setting %s to %d<->%d uvolts\n",
494 rk805_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
495 if (rk805_regnode_voltage_to_reg(sc, min_uvolt, max_uvolt, &val) != 0)
498 rk805_write(sc->base_dev, sc->def->voltage_reg, &val, 1);
500 rk805_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
504 rk805_regnode_reg_to_voltage(sc, val, &uvolt);
505 dprintf(sc, "Regulator %s set to %d uvolt\n",
513 rk805_regnode_get_voltage(struct regnode *regnode, int *uvolt)
515 struct rk805_reg_sc *sc;
518 sc = regnode_get_softc(regnode);
520 if (sc->def->voltage_min == sc->def->voltage_max) {
521 *uvolt = sc->def->voltage_min;
525 if (!sc->def->voltage_step)
528 rk805_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
529 rk805_regnode_reg_to_voltage(sc, val & sc->def->voltage_mask, uvolt);
531 dprintf(sc, "Regulator %s is at %d uvolt\n",
538 static regnode_method_t rk805_regnode_methods[] = {
539 /* Regulator interface */
540 REGNODEMETHOD(regnode_init, rk805_regnode_init),
541 REGNODEMETHOD(regnode_enable, rk805_regnode_enable),
542 REGNODEMETHOD(regnode_status, rk805_regnode_status),
543 REGNODEMETHOD(regnode_set_voltage, rk805_regnode_set_voltage),
544 REGNODEMETHOD(regnode_get_voltage, rk805_regnode_get_voltage),
545 REGNODEMETHOD(regnode_check_voltage, regnode_method_check_voltage),
548 DEFINE_CLASS_1(rk805_regnode, rk805_regnode_class, rk805_regnode_methods,
549 sizeof(struct rk805_reg_sc), regnode_class);
551 static struct rk805_reg_sc *
552 rk805_reg_attach(device_t dev, phandle_t node,
553 struct rk805_regdef *def)
555 struct rk805_reg_sc *reg_sc;
556 struct regnode_init_def initdef;
557 struct regnode *regnode;
559 memset(&initdef, 0, sizeof(initdef));
560 if (regulator_parse_ofw_stdparam(dev, node, &initdef) != 0) {
561 device_printf(dev, "cannot create regulator\n");
564 if (initdef.std_param.min_uvolt == 0)
565 initdef.std_param.min_uvolt = def->voltage_min;
566 if (initdef.std_param.max_uvolt == 0)
567 initdef.std_param.max_uvolt = def->voltage_max;
568 initdef.id = def->id;
569 initdef.ofw_node = node;
571 regnode = regnode_create(dev, &rk805_regnode_class, &initdef);
572 if (regnode == NULL) {
573 device_printf(dev, "cannot create regulator\n");
577 reg_sc = regnode_get_softc(regnode);
578 reg_sc->regnode = regnode;
579 reg_sc->base_dev = dev;
581 reg_sc->xref = OF_xref_from_node(node);
582 reg_sc->param = regnode_get_stdparam(regnode);
584 regnode_register(regnode);
590 rk805_probe(device_t dev)
592 if (!ofw_bus_status_okay(dev))
595 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
598 device_set_desc(dev, "RockChip RK805 PMIC");
599 return (BUS_PROBE_DEFAULT);
603 rk805_start(void *pdev)
605 struct rk805_softc *sc;
611 sc = device_get_softc(dev);
614 /* No version register in RK808 */
615 if (bootverbose && sc->type == RK805) {
616 err = rk805_read(dev, RK805_CHIP_NAME, data, 1);
618 device_printf(dev, "Cannot read chip name reg\n");
621 err = rk805_read(dev, RK805_CHIP_VER, data + 1, 1);
623 device_printf(dev, "Cannot read chip version reg\n");
626 device_printf(dev, "Chip Name: %x\n",
627 data[0] << 4 | ((data[1] >> 4) & 0xf));
628 device_printf(dev, "Chip Version: %x\n", data[1] & 0xf);
631 /* Register this as a 1Hz clock */
632 clock_register(dev, 1000000);
634 config_intrhook_disestablish(&sc->intr_hook);
638 rk805_gettime(device_t dev, struct timespec *ts)
640 struct bcd_clocktime bct;
645 /* Latch the RTC value into the shadow registers and set 24hr mode */
646 error = rk805_read(dev, RK805_RTC_CTRL, &ctrl, 1);
650 ctrl |= RK805_RTC_READSEL;
651 ctrl &= ~(RK805_RTC_AMPM_MODE | RK805_RTC_GET_TIME);
652 error = rk805_write(dev, RK805_RTC_CTRL, &ctrl, 1);
655 ctrl |= RK805_RTC_GET_TIME;
656 error = rk805_write(dev, RK805_RTC_CTRL, &ctrl, 1);
659 ctrl &= ~RK805_RTC_GET_TIME;
660 error = rk805_write(dev, RK805_RTC_CTRL, &ctrl, 1);
664 /* This works as long as RK805_RTC_SECS = 0 */
665 error = rk805_read(dev, RK805_RTC_SECS, data, 7);
670 * If the reported year is earlier than 2019, assume the clock is unset.
671 * This is both later than the reset value for the RK805 and RK808 as
672 * well as being prior to the current time.
674 if (data[RK805_RTC_YEARS] < 0x19)
677 memset(&bct, 0, sizeof(bct));
678 bct.year = data[RK805_RTC_YEARS];
679 bct.mon = data[RK805_RTC_MONTHS] & RK805_RTC_MONTHS_MASK;
680 bct.day = data[RK805_RTC_DAYS] & RK805_RTC_DAYS_MASK;
681 bct.hour = data[RK805_RTC_HOURS] & RK805_RTC_HOURS_MASK;
682 bct.min = data[RK805_RTC_MINUTES] & RK805_RTC_MINUTES_MASK;
683 bct.sec = data[RK805_RTC_SECS] & RK805_RTC_SECS_MASK;
684 bct.dow = data[RK805_RTC_WEEKS] & RK805_RTC_WEEKS_MASK;
685 /* The day of week is reported as 1-7 with 1 = Monday */
691 device_printf(dev, "Read RTC: %02x-%02x-%02x %02x:%02x:%02x\n",
692 bct.year, bct.mon, bct.day, bct.hour, bct.min, bct.sec);
694 return (clock_bcd_to_ts(&bct, ts, false));
698 rk805_settime(device_t dev, struct timespec *ts)
700 struct bcd_clocktime bct;
705 clock_ts_to_bcd(ts, &bct, false);
707 /* This works as long as RK805_RTC_SECS = 0 */
708 data[RK805_RTC_YEARS] = bct.year;
709 data[RK805_RTC_MONTHS] = bct.mon;
710 data[RK805_RTC_DAYS] = bct.day;
711 data[RK805_RTC_HOURS] = bct.hour;
712 data[RK805_RTC_MINUTES] = bct.min;
713 data[RK805_RTC_SECS] = bct.sec;
714 data[RK805_RTC_WEEKS] = bct.dow;
715 /* The day of week is reported as 1-7 with 1 = Monday */
716 if (data[RK805_RTC_WEEKS] == 0)
717 data[RK805_RTC_WEEKS] = 7;
719 error = rk805_read(dev, RK805_RTC_CTRL, &ctrl, 1);
723 ctrl |= RK805_RTC_CTRL_STOP;
724 ctrl &= ~RK805_RTC_AMPM_MODE;
725 error = rk805_write(dev, RK805_RTC_CTRL, &ctrl, 1);
729 error = rk805_write(dev, RK805_RTC_SECS, data, 7);
730 ctrl &= ~RK805_RTC_CTRL_STOP;
731 rk805_write(dev, RK805_RTC_CTRL, &ctrl, 1);
735 "Set RTC at %04x-%02x-%02x %02x:%02x:%02x[.%09ld]\n",
736 bct.year, bct.mon, bct.day, bct.hour, bct.min, bct.sec,
743 rk805_poweroff(void *arg, int howto)
749 if ((howto & RB_POWEROFF) == 0)
752 device_printf(dev, "Powering off...\n");
753 error = rk805_read(dev, RK805_DEV_CTRL, &val, 1);
755 val |= RK805_DEV_CTRL_OFF;
756 error = rk805_write(dev, RK805_DEV_CTRL, &val, 1);
758 /* Wait a bit for the command to take effect. */
762 device_printf(dev, "Power off failed\n");
766 rk805_attach(device_t dev)
768 struct rk805_softc *sc;
769 struct rk805_reg_sc *reg;
770 struct rk805_regdef *regdefs;
771 struct reg_list *regp;
772 phandle_t rnode, child;
775 sc = device_get_softc(dev);
777 sc->intr_hook.ich_func = rk805_start;
778 sc->intr_hook.ich_arg = dev;
780 if (config_intrhook_establish(&sc->intr_hook) != 0)
783 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
786 regdefs = rk805_regdefs;
787 sc->nregs = nitems(rk805_regdefs);
790 regdefs = rk808_regdefs;
791 sc->nregs = nitems(rk808_regdefs);
794 device_printf(dev, "Unknown type %d\n", sc->type);
798 TAILQ_INIT(&sc->regs);
800 rnode = ofw_bus_find_child(ofw_bus_get_node(dev), "regulators");
802 for (i = 0; i < sc->nregs; i++) {
803 child = ofw_bus_find_child(rnode,
807 if (OF_hasprop(child, "regulator-name") != 1)
809 reg = rk805_reg_attach(dev, child, ®defs[i]);
812 "cannot attach regulator %s\n",
816 regp = malloc(sizeof(*regp), M_DEVBUF, M_WAITOK | M_ZERO);
818 TAILQ_INSERT_TAIL(&sc->regs, regp, next);
820 device_printf(dev, "Regulator %s attached\n",
825 if (OF_hasprop(ofw_bus_get_node(dev),
826 "rockchip,system-power-controller")) {
828 * The priority is chosen to override PSCI and EFI shutdown
829 * methods as those two just hang without powering off on Rock64
832 EVENTHANDLER_REGISTER(shutdown_final, rk805_poweroff, dev,
833 SHUTDOWN_PRI_LAST - 2);
840 rk805_detach(device_t dev)
843 /* We cannot detach regulators */
848 rk805_map(device_t dev, phandle_t xref, int ncells,
849 pcell_t *cells, intptr_t *id)
851 struct rk805_softc *sc;
852 struct reg_list *regp;
854 sc = device_get_softc(dev);
856 TAILQ_FOREACH(regp, &sc->regs, next) {
857 if (regp->reg->xref == xref) {
858 *id = regp->reg->def->id;
866 static device_method_t rk805_methods[] = {
867 DEVMETHOD(device_probe, rk805_probe),
868 DEVMETHOD(device_attach, rk805_attach),
869 DEVMETHOD(device_detach, rk805_detach),
871 /* regdev interface */
872 DEVMETHOD(regdev_map, rk805_map),
874 /* Clock interface */
875 DEVMETHOD(clock_gettime, rk805_gettime),
876 DEVMETHOD(clock_settime, rk805_settime),
881 static driver_t rk805_driver = {
884 sizeof(struct rk805_softc),
887 static devclass_t rk805_devclass;
889 EARLY_DRIVER_MODULE(rk805, iicbus, rk805_driver, rk805_devclass, 0, 0,
890 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
891 MODULE_DEPEND(rk805, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
892 MODULE_VERSION(rk805, 1);