1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * AXP305 driver
4 *
5 * (C) Copyright 2020 Jernej Skrabec <jernej.skrabec@siol.net>
6 *
7 * Based on axp221.c
8 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
9 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
10 */
11
12#include <command.h>
13#include <errno.h>
14#include <asm/arch/pmic_bus.h>
15#include <axp_pmic.h>
16
17#define AXP305_DCDC4_1600MV_OFFSET 46
18
19static u8 axp305_mvolt_to_cfg(int mvolt, int min, int max, int div)
20{
21	if (mvolt < min)
22		mvolt = min;
23	else if (mvolt > max)
24		mvolt = max;
25
26	return  (mvolt - min) / div;
27}
28
29int axp_set_dcdc4(unsigned int mvolt)
30{
31	int ret;
32	u8 cfg;
33
34	if (mvolt >= 1600)
35		cfg = AXP305_DCDC4_1600MV_OFFSET +
36			axp305_mvolt_to_cfg(mvolt, 1600, 3300, 100);
37	else
38		cfg = axp305_mvolt_to_cfg(mvolt, 600, 1500, 20);
39
40	if (mvolt == 0)
41		return pmic_bus_clrbits(AXP305_OUTPUT_CTRL1,
42					AXP305_OUTPUT_CTRL1_DCDCD_EN);
43
44	ret = pmic_bus_write(AXP305_DCDCD_VOLTAGE, cfg);
45	if (ret)
46		return ret;
47
48	return pmic_bus_setbits(AXP305_OUTPUT_CTRL1,
49				AXP305_OUTPUT_CTRL1_DCDCD_EN);
50}
51
52int axp_init(void)
53{
54	u8 axp_chip_id;
55	int ret;
56
57	ret = pmic_bus_init();
58	if (ret)
59		return ret;
60
61	ret = pmic_bus_read(AXP305_CHIP_VERSION, &axp_chip_id);
62	if (ret)
63		return ret;
64
65	if ((axp_chip_id & AXP305_CHIP_VERSION_MASK) != 0x40)
66		return -ENODEV;
67
68	return ret;
69}
70
71#if !CONFIG_IS_ENABLED(ARM_PSCI_FW) && !IS_ENABLED(CONFIG_SYSRESET_CMD_POWEROFF)
72int do_poweroff(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
73{
74	pmic_bus_write(AXP305_SHUTDOWN, AXP305_POWEROFF);
75
76	/* infinite loop during shutdown */
77	while (1) {}
78
79	/* not reached */
80	return 0;
81}
82#endif
83