xref: /freebsd/sys/dev/iicbus/pmic/rockchip/rk808.c (revision fdafd315)
198c60dc3SEmmanuel Vadot /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
398c60dc3SEmmanuel Vadot  *
498c60dc3SEmmanuel Vadot  * Copyright (c) 2018-2021 Emmanuel Vadot <manu@FreeBSD.org>
598c60dc3SEmmanuel Vadot  *
698c60dc3SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
798c60dc3SEmmanuel Vadot  * modification, are permitted provided that the following conditions
898c60dc3SEmmanuel Vadot  * are met:
998c60dc3SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
1098c60dc3SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
1198c60dc3SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
1298c60dc3SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
1398c60dc3SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
1498c60dc3SEmmanuel Vadot  *
1598c60dc3SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1698c60dc3SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1798c60dc3SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1898c60dc3SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1998c60dc3SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2098c60dc3SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2198c60dc3SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2298c60dc3SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2398c60dc3SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2498c60dc3SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2598c60dc3SEmmanuel Vadot  * SUCH DAMAGE.
2698c60dc3SEmmanuel Vadot  */
2798c60dc3SEmmanuel Vadot 
2898c60dc3SEmmanuel Vadot #include <sys/param.h>
2998c60dc3SEmmanuel Vadot #include <sys/bus.h>
3098c60dc3SEmmanuel Vadot #include <sys/clock.h>
3198c60dc3SEmmanuel Vadot #include <sys/kernel.h>
3298c60dc3SEmmanuel Vadot #include <sys/module.h>
3398c60dc3SEmmanuel Vadot #include <sys/mutex.h>
3498c60dc3SEmmanuel Vadot #include <sys/rman.h>
3598c60dc3SEmmanuel Vadot #include <machine/bus.h>
3698c60dc3SEmmanuel Vadot 
3798c60dc3SEmmanuel Vadot #include <dev/iicbus/iiconf.h>
3898c60dc3SEmmanuel Vadot #include <dev/iicbus/iicbus.h>
3998c60dc3SEmmanuel Vadot 
4098c60dc3SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
4198c60dc3SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
4298c60dc3SEmmanuel Vadot 
4398c60dc3SEmmanuel Vadot #include <dev/iicbus/pmic/rockchip/rk808reg.h>
4498c60dc3SEmmanuel Vadot #include <dev/iicbus/pmic/rockchip/rk8xx.h>
4598c60dc3SEmmanuel Vadot 
4698c60dc3SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
4798c60dc3SEmmanuel Vadot 	{"rockchip,rk808", RK808},
4898c60dc3SEmmanuel Vadot 	{NULL,             0}
4998c60dc3SEmmanuel Vadot };
5098c60dc3SEmmanuel Vadot 
5198c60dc3SEmmanuel Vadot static struct rk8xx_regdef rk808_regdefs[] = {
5298c60dc3SEmmanuel Vadot 	{
5398c60dc3SEmmanuel Vadot 		.id = RK808_BUCK1,
5498c60dc3SEmmanuel Vadot 		.name = "DCDC_REG1",
5598c60dc3SEmmanuel Vadot 		.enable_reg = RK808_DCDC_EN,
5698c60dc3SEmmanuel Vadot 		.enable_mask = 0x1,
5798c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_BUCK1_ON_VSEL,
5898c60dc3SEmmanuel Vadot 		.voltage_mask = 0x3F,
5998c60dc3SEmmanuel Vadot 		.voltage_min = 712500,
6098c60dc3SEmmanuel Vadot 		.voltage_max = 1500000,
6198c60dc3SEmmanuel Vadot 		.voltage_step = 12500,
6298c60dc3SEmmanuel Vadot 		.voltage_nstep = 64,
6398c60dc3SEmmanuel Vadot 	},
6498c60dc3SEmmanuel Vadot 	{
6598c60dc3SEmmanuel Vadot 		.id = RK808_BUCK2,
6698c60dc3SEmmanuel Vadot 		.name = "DCDC_REG2",
6798c60dc3SEmmanuel Vadot 		.enable_reg = RK808_DCDC_EN,
6898c60dc3SEmmanuel Vadot 		.enable_mask = 0x2,
6998c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_BUCK2_ON_VSEL,
7098c60dc3SEmmanuel Vadot 		.voltage_mask = 0x3F,
7198c60dc3SEmmanuel Vadot 		.voltage_min = 712500,
7298c60dc3SEmmanuel Vadot 		.voltage_max = 1500000,
7398c60dc3SEmmanuel Vadot 		.voltage_step = 12500,
7498c60dc3SEmmanuel Vadot 		.voltage_nstep = 64,
7598c60dc3SEmmanuel Vadot 	},
7698c60dc3SEmmanuel Vadot 	{
7798c60dc3SEmmanuel Vadot 		/* BUCK3 voltage is calculated based on external resistor */
7898c60dc3SEmmanuel Vadot 		.id = RK808_BUCK3,
7998c60dc3SEmmanuel Vadot 		.name = "DCDC_REG3",
8098c60dc3SEmmanuel Vadot 		.enable_reg = RK808_DCDC_EN,
8198c60dc3SEmmanuel Vadot 		.enable_mask = 0x4,
8298c60dc3SEmmanuel Vadot 	},
8398c60dc3SEmmanuel Vadot 	{
8498c60dc3SEmmanuel Vadot 		.id = RK808_BUCK4,
8598c60dc3SEmmanuel Vadot 		.name = "DCDC_REG4",
8698c60dc3SEmmanuel Vadot 		.enable_reg = RK808_DCDC_EN,
8798c60dc3SEmmanuel Vadot 		.enable_mask = 0x8,
8898c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_BUCK4_ON_VSEL,
8998c60dc3SEmmanuel Vadot 		.voltage_mask = 0xF,
9098c60dc3SEmmanuel Vadot 		.voltage_min = 1800000,
9198c60dc3SEmmanuel Vadot 		.voltage_max = 3300000,
9298c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
9398c60dc3SEmmanuel Vadot 		.voltage_nstep = 16,
9498c60dc3SEmmanuel Vadot 	},
9598c60dc3SEmmanuel Vadot 	{
9698c60dc3SEmmanuel Vadot 		.id = RK808_LDO1,
9798c60dc3SEmmanuel Vadot 		.name = "LDO_REG1",
9898c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
9998c60dc3SEmmanuel Vadot 		.enable_mask = 0x1,
10098c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO1_ON_VSEL,
10198c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
10298c60dc3SEmmanuel Vadot 		.voltage_min = 1800000,
10398c60dc3SEmmanuel Vadot 		.voltage_max = 3400000,
10498c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
10598c60dc3SEmmanuel Vadot 		.voltage_nstep = 17,
10698c60dc3SEmmanuel Vadot 	},
10798c60dc3SEmmanuel Vadot 	{
10898c60dc3SEmmanuel Vadot 		.id = RK808_LDO2,
10998c60dc3SEmmanuel Vadot 		.name = "LDO_REG2",
11098c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
11198c60dc3SEmmanuel Vadot 		.enable_mask = 0x2,
11298c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO2_ON_VSEL,
11398c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
11498c60dc3SEmmanuel Vadot 		.voltage_min = 1800000,
11598c60dc3SEmmanuel Vadot 		.voltage_max = 3400000,
11698c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
11798c60dc3SEmmanuel Vadot 		.voltage_nstep = 17,
11898c60dc3SEmmanuel Vadot 	},
11998c60dc3SEmmanuel Vadot 	{
12098c60dc3SEmmanuel Vadot 		.id = RK808_LDO3,
12198c60dc3SEmmanuel Vadot 		.name = "LDO_REG3",
12298c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
12398c60dc3SEmmanuel Vadot 		.enable_mask = 0x4,
12498c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO3_ON_VSEL,
12598c60dc3SEmmanuel Vadot 		.voltage_mask = 0xF,
12698c60dc3SEmmanuel Vadot 		.voltage_min = 800000,
12798c60dc3SEmmanuel Vadot 		.voltage_max = 2500000,
12898c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
12998c60dc3SEmmanuel Vadot 		.voltage_nstep = 18,
13098c60dc3SEmmanuel Vadot 	},
13198c60dc3SEmmanuel Vadot 	{
13298c60dc3SEmmanuel Vadot 		.id = RK808_LDO4,
13398c60dc3SEmmanuel Vadot 		.name = "LDO_REG4",
13498c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
13598c60dc3SEmmanuel Vadot 		.enable_mask = 0x8,
13698c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO4_ON_VSEL,
13798c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
13898c60dc3SEmmanuel Vadot 		.voltage_min = 1800000,
13998c60dc3SEmmanuel Vadot 		.voltage_max = 3400000,
14098c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
14198c60dc3SEmmanuel Vadot 		.voltage_nstep = 17,
14298c60dc3SEmmanuel Vadot 	},
14398c60dc3SEmmanuel Vadot 	{
14498c60dc3SEmmanuel Vadot 		.id = RK808_LDO5,
14598c60dc3SEmmanuel Vadot 		.name = "LDO_REG5",
14698c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
14798c60dc3SEmmanuel Vadot 		.enable_mask = 0x10,
14898c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO5_ON_VSEL,
14998c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
15098c60dc3SEmmanuel Vadot 		.voltage_min = 1800000,
15198c60dc3SEmmanuel Vadot 		.voltage_max = 3400000,
15298c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
15398c60dc3SEmmanuel Vadot 		.voltage_nstep = 17,
15498c60dc3SEmmanuel Vadot 	},
15598c60dc3SEmmanuel Vadot 	{
15698c60dc3SEmmanuel Vadot 		.id = RK808_LDO6,
15798c60dc3SEmmanuel Vadot 		.name = "LDO_REG6",
15898c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
15998c60dc3SEmmanuel Vadot 		.enable_mask = 0x20,
16098c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO6_ON_VSEL,
16198c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
16298c60dc3SEmmanuel Vadot 		.voltage_min = 800000,
16398c60dc3SEmmanuel Vadot 		.voltage_max = 2500000,
16498c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
16598c60dc3SEmmanuel Vadot 		.voltage_nstep = 18,
16698c60dc3SEmmanuel Vadot 	},
16798c60dc3SEmmanuel Vadot 	{
16898c60dc3SEmmanuel Vadot 		.id = RK808_LDO7,
16998c60dc3SEmmanuel Vadot 		.name = "LDO_REG7",
17098c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
17198c60dc3SEmmanuel Vadot 		.enable_mask = 0x40,
17298c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO7_ON_VSEL,
17398c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
17498c60dc3SEmmanuel Vadot 		.voltage_min = 800000,
17598c60dc3SEmmanuel Vadot 		.voltage_max = 2500000,
17698c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
17798c60dc3SEmmanuel Vadot 		.voltage_nstep = 18,
17898c60dc3SEmmanuel Vadot 	},
17998c60dc3SEmmanuel Vadot 	{
18098c60dc3SEmmanuel Vadot 		.id = RK808_LDO8,
18198c60dc3SEmmanuel Vadot 		.name = "LDO_REG8",
18298c60dc3SEmmanuel Vadot 		.enable_reg = RK808_LDO_EN,
18398c60dc3SEmmanuel Vadot 		.enable_mask = 0x80,
18498c60dc3SEmmanuel Vadot 		.voltage_reg = RK808_LDO8_ON_VSEL,
18598c60dc3SEmmanuel Vadot 		.voltage_mask = 0x1F,
18698c60dc3SEmmanuel Vadot 		.voltage_min = 1800000,
18798c60dc3SEmmanuel Vadot 		.voltage_max = 3400000,
18898c60dc3SEmmanuel Vadot 		.voltage_step = 100000,
18998c60dc3SEmmanuel Vadot 		.voltage_nstep = 17,
19098c60dc3SEmmanuel Vadot 	},
19198c60dc3SEmmanuel Vadot 	{
19298c60dc3SEmmanuel Vadot 		.id = RK808_SWITCH1,
19398c60dc3SEmmanuel Vadot 		.name = "SWITCH_REG1",
19498c60dc3SEmmanuel Vadot 		.enable_reg = RK808_DCDC_EN,
19598c60dc3SEmmanuel Vadot 		.enable_mask = 0x20,
19698c60dc3SEmmanuel Vadot 		.voltage_min = 3000000,
19798c60dc3SEmmanuel Vadot 		.voltage_max = 3000000,
19898c60dc3SEmmanuel Vadot 	},
19998c60dc3SEmmanuel Vadot 	{
20098c60dc3SEmmanuel Vadot 		.id = RK808_SWITCH2,
20198c60dc3SEmmanuel Vadot 		.name = "SWITCH_REG2",
20298c60dc3SEmmanuel Vadot 		.enable_reg = RK808_DCDC_EN,
20398c60dc3SEmmanuel Vadot 		.enable_mask = 0x40,
20498c60dc3SEmmanuel Vadot 		.voltage_min = 3000000,
20598c60dc3SEmmanuel Vadot 		.voltage_max = 3000000,
20698c60dc3SEmmanuel Vadot 	},
20798c60dc3SEmmanuel Vadot };
20898c60dc3SEmmanuel Vadot 
20998c60dc3SEmmanuel Vadot static int
rk808_probe(device_t dev)21098c60dc3SEmmanuel Vadot rk808_probe(device_t dev)
21198c60dc3SEmmanuel Vadot {
21298c60dc3SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
21398c60dc3SEmmanuel Vadot 		return (ENXIO);
21498c60dc3SEmmanuel Vadot 
21598c60dc3SEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
21698c60dc3SEmmanuel Vadot 		return (ENXIO);
21798c60dc3SEmmanuel Vadot 
21898c60dc3SEmmanuel Vadot 	device_set_desc(dev, "RockChip RK808 PMIC");
21998c60dc3SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
22098c60dc3SEmmanuel Vadot }
22198c60dc3SEmmanuel Vadot 
22298c60dc3SEmmanuel Vadot static int
rk808_attach(device_t dev)22398c60dc3SEmmanuel Vadot rk808_attach(device_t dev)
22498c60dc3SEmmanuel Vadot {
22598c60dc3SEmmanuel Vadot 	struct rk8xx_softc *sc;
22698c60dc3SEmmanuel Vadot 
22798c60dc3SEmmanuel Vadot 	sc = device_get_softc(dev);
22898c60dc3SEmmanuel Vadot 	sc->dev = dev;
22998c60dc3SEmmanuel Vadot 
23098c60dc3SEmmanuel Vadot 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
23198c60dc3SEmmanuel Vadot 
23298c60dc3SEmmanuel Vadot 	sc->regdefs = rk808_regdefs;
23398c60dc3SEmmanuel Vadot 	sc->nregs = nitems(rk808_regdefs);
23498c60dc3SEmmanuel Vadot 	sc->rtc_regs.secs = RK808_RTC_SECS;
23598c60dc3SEmmanuel Vadot 	sc->rtc_regs.secs_mask = RK808_RTC_SECS_MASK;
23698c60dc3SEmmanuel Vadot 	sc->rtc_regs.minutes = RK808_RTC_MINUTES;
23798c60dc3SEmmanuel Vadot 	sc->rtc_regs.minutes_mask = RK808_RTC_MINUTES_MASK;
23898c60dc3SEmmanuel Vadot 	sc->rtc_regs.hours = RK808_RTC_HOURS;
23998c60dc3SEmmanuel Vadot 	sc->rtc_regs.hours_mask = RK808_RTC_HOURS_MASK;
24098c60dc3SEmmanuel Vadot 	sc->rtc_regs.days = RK808_RTC_DAYS;
24198c60dc3SEmmanuel Vadot 	sc->rtc_regs.days_mask = RK808_RTC_DAYS_MASK;
24298c60dc3SEmmanuel Vadot 	sc->rtc_regs.months = RK808_RTC_MONTHS;
24398c60dc3SEmmanuel Vadot 	sc->rtc_regs.months_mask = RK808_RTC_MONTHS_MASK;
24498c60dc3SEmmanuel Vadot 	sc->rtc_regs.years = RK808_RTC_YEARS;
24598c60dc3SEmmanuel Vadot 	sc->rtc_regs.weeks = RK808_RTC_WEEKS_MASK;
24698c60dc3SEmmanuel Vadot 	sc->rtc_regs.ctrl = RK808_RTC_CTRL;
24798c60dc3SEmmanuel Vadot 	sc->rtc_regs.ctrl_stop_mask = RK808_RTC_CTRL_STOP;
24898c60dc3SEmmanuel Vadot 	sc->rtc_regs.ctrl_ampm_mask = RK808_RTC_AMPM_MODE;
24998c60dc3SEmmanuel Vadot 	sc->rtc_regs.ctrl_gettime_mask = RK808_RTC_GET_TIME;
25098c60dc3SEmmanuel Vadot 	sc->rtc_regs.ctrl_readsel_mask = RK808_RTC_READSEL;
25194f4afd7SAndriy Gapon 	sc->dev_ctrl.dev_ctrl_reg = RK808_DEV_CTRL;
25294f4afd7SAndriy Gapon 	sc->dev_ctrl.pwr_off_mask = RK808_DEV_CTRL_OFF;
25398c60dc3SEmmanuel Vadot 
25498c60dc3SEmmanuel Vadot 	return (rk8xx_attach(sc));
25598c60dc3SEmmanuel Vadot }
25698c60dc3SEmmanuel Vadot 
25798c60dc3SEmmanuel Vadot static device_method_t rk808_methods[] = {
25898c60dc3SEmmanuel Vadot 	DEVMETHOD(device_probe,		rk808_probe),
25998c60dc3SEmmanuel Vadot 	DEVMETHOD(device_attach,	rk808_attach),
26098c60dc3SEmmanuel Vadot 
26198c60dc3SEmmanuel Vadot 	DEVMETHOD_END
26298c60dc3SEmmanuel Vadot };
26398c60dc3SEmmanuel Vadot 
26491a908e0SEmmanuel Vadot DEFINE_CLASS_1(rk808_pmu, rk808_driver, rk808_methods,
26598c60dc3SEmmanuel Vadot     sizeof(struct rk8xx_softc), rk8xx_driver);
26698c60dc3SEmmanuel Vadot 
2673a866152SJohn Baldwin EARLY_DRIVER_MODULE(rk808_pmu, iicbus, rk808_driver, 0, 0,
268ddefad7cSEmmanuel Vadot     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
269676ea8e1SJohn Baldwin EARLY_DRIVER_MODULE(iicbus, rk808_pmu, iicbus_driver, 0, 0,
270ddefad7cSEmmanuel Vadot     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
27191a908e0SEmmanuel Vadot MODULE_DEPEND(rk808_pmu, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
27291a908e0SEmmanuel Vadot MODULE_VERSION(rk808_pmu, 1);
273