151919325SEmmanuel Vadot /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
351919325SEmmanuel Vadot *
498c60dc3SEmmanuel Vadot * Copyright (c) 2018-2021 Emmanuel Vadot <manu@FreeBSD.org>
551919325SEmmanuel Vadot *
651919325SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
751919325SEmmanuel Vadot * modification, are permitted provided that the following conditions
851919325SEmmanuel Vadot * are met:
951919325SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright
1051919325SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
1151919325SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright
1251919325SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
1351919325SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
1451919325SEmmanuel Vadot *
1551919325SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1651919325SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1751919325SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1851919325SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1951919325SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2051919325SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2151919325SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2251919325SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2351919325SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2451919325SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2551919325SEmmanuel Vadot * SUCH DAMAGE.
2651919325SEmmanuel Vadot */
2751919325SEmmanuel Vadot
2851919325SEmmanuel Vadot #include <sys/param.h>
2951919325SEmmanuel Vadot #include <sys/bus.h>
3051919325SEmmanuel Vadot #include <sys/clock.h>
3151919325SEmmanuel Vadot #include <sys/eventhandler.h>
3251919325SEmmanuel Vadot #include <sys/kernel.h>
3351919325SEmmanuel Vadot #include <sys/module.h>
3451919325SEmmanuel Vadot #include <sys/reboot.h>
3551919325SEmmanuel Vadot #include <sys/mutex.h>
3651919325SEmmanuel Vadot #include <sys/rman.h>
3751919325SEmmanuel Vadot #include <machine/bus.h>
3851919325SEmmanuel Vadot
3951919325SEmmanuel Vadot #include <dev/iicbus/iiconf.h>
4051919325SEmmanuel Vadot #include <dev/iicbus/iicbus.h>
4151919325SEmmanuel Vadot
4251919325SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
4351919325SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
4451919325SEmmanuel Vadot
4551919325SEmmanuel Vadot #include <dev/iicbus/pmic/rockchip/rk805reg.h>
46328077bbSEmmanuel Vadot #include <dev/iicbus/pmic/rockchip/rk8xx.h>
4751919325SEmmanuel Vadot
4851919325SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
4951919325SEmmanuel Vadot {"rockchip,rk805", RK805},
5051919325SEmmanuel Vadot {NULL, 0}
5151919325SEmmanuel Vadot };
5251919325SEmmanuel Vadot
53c9b101d2SEmmanuel Vadot static struct rk8xx_regdef rk805_regdefs[] = {
5451919325SEmmanuel Vadot {
5543301ad2SEmmanuel Vadot .id = RK805_BUCK1,
5651919325SEmmanuel Vadot .name = "DCDC_REG1",
5751919325SEmmanuel Vadot .enable_reg = RK805_DCDC_EN,
5851919325SEmmanuel Vadot .enable_mask = 0x11,
5943301ad2SEmmanuel Vadot .voltage_reg = RK805_BUCK1_ON_VSEL,
6051919325SEmmanuel Vadot .voltage_mask = 0x3F,
6151919325SEmmanuel Vadot .voltage_min = 712500,
6251919325SEmmanuel Vadot .voltage_max = 1450000,
6351919325SEmmanuel Vadot .voltage_step = 12500,
6451919325SEmmanuel Vadot .voltage_nstep = 64,
6551919325SEmmanuel Vadot },
6651919325SEmmanuel Vadot {
6743301ad2SEmmanuel Vadot .id = RK805_BUCK2,
6851919325SEmmanuel Vadot .name = "DCDC_REG2",
6951919325SEmmanuel Vadot .enable_reg = RK805_DCDC_EN,
7051919325SEmmanuel Vadot .enable_mask = 0x22,
7143301ad2SEmmanuel Vadot .voltage_reg = RK805_BUCK2_ON_VSEL,
7251919325SEmmanuel Vadot .voltage_mask = 0x3F,
7351919325SEmmanuel Vadot .voltage_min = 712500,
7451919325SEmmanuel Vadot .voltage_max = 1450000,
7551919325SEmmanuel Vadot .voltage_step = 12500,
7651919325SEmmanuel Vadot .voltage_nstep = 64,
7751919325SEmmanuel Vadot },
7851919325SEmmanuel Vadot {
7943301ad2SEmmanuel Vadot .id = RK805_BUCK3,
8051919325SEmmanuel Vadot .name = "DCDC_REG3",
8151919325SEmmanuel Vadot .enable_reg = RK805_DCDC_EN,
8251919325SEmmanuel Vadot .enable_mask = 0x44,
8351919325SEmmanuel Vadot },
8451919325SEmmanuel Vadot {
8543301ad2SEmmanuel Vadot .id = RK805_BUCK4,
8651919325SEmmanuel Vadot .name = "DCDC_REG4",
8751919325SEmmanuel Vadot .enable_reg = RK805_DCDC_EN,
8851919325SEmmanuel Vadot .enable_mask = 0x88,
8943301ad2SEmmanuel Vadot .voltage_reg = RK805_BUCK4_ON_VSEL,
9051919325SEmmanuel Vadot .voltage_mask = 0x3F,
9151919325SEmmanuel Vadot .voltage_min = 800000,
9251919325SEmmanuel Vadot .voltage_max = 3500000,
9351919325SEmmanuel Vadot .voltage_step = 100000,
9451919325SEmmanuel Vadot .voltage_nstep = 28,
9551919325SEmmanuel Vadot },
9651919325SEmmanuel Vadot {
9751919325SEmmanuel Vadot .id = RK805_LDO1,
9851919325SEmmanuel Vadot .name = "LDO_REG1",
9951919325SEmmanuel Vadot .enable_reg = RK805_LDO_EN,
10051919325SEmmanuel Vadot .enable_mask = 0x11,
10151919325SEmmanuel Vadot .voltage_reg = RK805_LDO1_ON_VSEL,
10251919325SEmmanuel Vadot .voltage_mask = 0x1F,
10351919325SEmmanuel Vadot .voltage_min = 800000,
10451919325SEmmanuel Vadot .voltage_max = 3400000,
10551919325SEmmanuel Vadot .voltage_step = 100000,
10651919325SEmmanuel Vadot .voltage_nstep = 27,
10751919325SEmmanuel Vadot },
10851919325SEmmanuel Vadot {
10951919325SEmmanuel Vadot .id = RK805_LDO2,
11051919325SEmmanuel Vadot .name = "LDO_REG2",
11151919325SEmmanuel Vadot .enable_reg = RK805_LDO_EN,
11251919325SEmmanuel Vadot .enable_mask = 0x22,
11351919325SEmmanuel Vadot .voltage_reg = RK805_LDO2_ON_VSEL,
11451919325SEmmanuel Vadot .voltage_mask = 0x1F,
11551919325SEmmanuel Vadot .voltage_min = 800000,
11651919325SEmmanuel Vadot .voltage_max = 3400000,
11751919325SEmmanuel Vadot .voltage_step = 100000,
11851919325SEmmanuel Vadot .voltage_nstep = 27,
11951919325SEmmanuel Vadot },
12051919325SEmmanuel Vadot {
12151919325SEmmanuel Vadot .id = RK805_LDO3,
12251919325SEmmanuel Vadot .name = "LDO_REG3",
12351919325SEmmanuel Vadot .enable_reg = RK805_LDO_EN,
12451919325SEmmanuel Vadot .enable_mask = 0x44,
12551919325SEmmanuel Vadot .voltage_reg = RK805_LDO3_ON_VSEL,
12651919325SEmmanuel Vadot .voltage_mask = 0x1F,
12751919325SEmmanuel Vadot .voltage_min = 800000,
12851919325SEmmanuel Vadot .voltage_max = 3400000,
12951919325SEmmanuel Vadot .voltage_step = 100000,
13051919325SEmmanuel Vadot .voltage_nstep = 27,
13151919325SEmmanuel Vadot },
13251919325SEmmanuel Vadot };
13351919325SEmmanuel Vadot
13451919325SEmmanuel Vadot static int
rk805_probe(device_t dev)13598c60dc3SEmmanuel Vadot rk805_probe(device_t dev)
13651919325SEmmanuel Vadot {
13751919325SEmmanuel Vadot if (!ofw_bus_status_okay(dev))
13851919325SEmmanuel Vadot return (ENXIO);
13951919325SEmmanuel Vadot
14051919325SEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
14151919325SEmmanuel Vadot return (ENXIO);
14251919325SEmmanuel Vadot
14398c60dc3SEmmanuel Vadot device_set_desc(dev, "RockChip RK805 PMIC");
14451919325SEmmanuel Vadot return (BUS_PROBE_DEFAULT);
14551919325SEmmanuel Vadot }
14651919325SEmmanuel Vadot
14798c60dc3SEmmanuel Vadot static int
rk805_attach(device_t dev)14898c60dc3SEmmanuel Vadot rk805_attach(device_t dev)
14951919325SEmmanuel Vadot {
150c9b101d2SEmmanuel Vadot struct rk8xx_softc *sc;
15151919325SEmmanuel Vadot
15251919325SEmmanuel Vadot sc = device_get_softc(dev);
15351919325SEmmanuel Vadot sc->dev = dev;
15451919325SEmmanuel Vadot
15551919325SEmmanuel Vadot sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
15651919325SEmmanuel Vadot
15798c60dc3SEmmanuel Vadot sc->regdefs = rk805_regdefs;
15851919325SEmmanuel Vadot sc->nregs = nitems(rk805_regdefs);
159328077bbSEmmanuel Vadot sc->rtc_regs.secs = RK805_RTC_SECS;
160328077bbSEmmanuel Vadot sc->rtc_regs.secs_mask = RK805_RTC_SECS_MASK;
161328077bbSEmmanuel Vadot sc->rtc_regs.minutes = RK805_RTC_MINUTES;
162328077bbSEmmanuel Vadot sc->rtc_regs.minutes_mask = RK805_RTC_MINUTES_MASK;
163328077bbSEmmanuel Vadot sc->rtc_regs.hours = RK805_RTC_HOURS;
164328077bbSEmmanuel Vadot sc->rtc_regs.hours_mask = RK805_RTC_HOURS_MASK;
165328077bbSEmmanuel Vadot sc->rtc_regs.days = RK805_RTC_DAYS;
166328077bbSEmmanuel Vadot sc->rtc_regs.days_mask = RK805_RTC_DAYS_MASK;
167328077bbSEmmanuel Vadot sc->rtc_regs.months = RK805_RTC_MONTHS;
168328077bbSEmmanuel Vadot sc->rtc_regs.months_mask = RK805_RTC_MONTHS_MASK;
169328077bbSEmmanuel Vadot sc->rtc_regs.years = RK805_RTC_YEARS;
170328077bbSEmmanuel Vadot sc->rtc_regs.weeks = RK805_RTC_WEEKS_MASK;
171328077bbSEmmanuel Vadot sc->rtc_regs.ctrl = RK805_RTC_CTRL;
172328077bbSEmmanuel Vadot sc->rtc_regs.ctrl_stop_mask = RK805_RTC_CTRL_STOP;
173328077bbSEmmanuel Vadot sc->rtc_regs.ctrl_ampm_mask = RK805_RTC_AMPM_MODE;
174328077bbSEmmanuel Vadot sc->rtc_regs.ctrl_gettime_mask = RK805_RTC_GET_TIME;
175328077bbSEmmanuel Vadot sc->rtc_regs.ctrl_readsel_mask = RK805_RTC_READSEL;
17694f4afd7SAndriy Gapon sc->dev_ctrl.dev_ctrl_reg = RK805_DEV_CTRL;
17794f4afd7SAndriy Gapon sc->dev_ctrl.pwr_off_mask = RK805_DEV_CTRL_OFF;
17898c60dc3SEmmanuel Vadot
17998c60dc3SEmmanuel Vadot return (rk8xx_attach(sc));
18051919325SEmmanuel Vadot }
18151919325SEmmanuel Vadot
18298c60dc3SEmmanuel Vadot static device_method_t rk805_methods[] = {
18398c60dc3SEmmanuel Vadot DEVMETHOD(device_probe, rk805_probe),
18498c60dc3SEmmanuel Vadot DEVMETHOD(device_attach, rk805_attach),
18551919325SEmmanuel Vadot
18651919325SEmmanuel Vadot DEVMETHOD_END
18751919325SEmmanuel Vadot };
18851919325SEmmanuel Vadot
18991a908e0SEmmanuel Vadot DEFINE_CLASS_1(rk805_pmu, rk805_driver, rk805_methods,
19098c60dc3SEmmanuel Vadot sizeof(struct rk8xx_softc), rk8xx_driver);
19151919325SEmmanuel Vadot
1923a866152SJohn Baldwin EARLY_DRIVER_MODULE(rk805_pmu, iicbus, rk805_driver, 0, 0,
193*ddefad7cSEmmanuel Vadot BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
194676ea8e1SJohn Baldwin EARLY_DRIVER_MODULE(iicbus, rk805_pmu, iicbus_driver, 0, 0,
195*ddefad7cSEmmanuel Vadot BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
19691a908e0SEmmanuel Vadot MODULE_DEPEND(rk805_pmu, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
19791a908e0SEmmanuel Vadot MODULE_VERSION(rk805_pmu, 1);
198