xref: /rk3399_rockchip-uboot/drivers/power/pmic/pmic_fp9931.c (revision 470c0165f9ad8326bdf885c6dde59a54b8886ce2)
1*470c0165SChaoyi Chen // SPDX-License-Identifier: GPL-2.0
2*470c0165SChaoyi Chen /*
3*470c0165SChaoyi Chen  * Copyright (c) 2024 Rockchip Electronics Co., Ltd.
4*470c0165SChaoyi Chen  */
5*470c0165SChaoyi Chen 
6*470c0165SChaoyi Chen #include <common.h>
7*470c0165SChaoyi Chen #include <asm/gpio.h>
8*470c0165SChaoyi Chen #include <dm.h>
9*470c0165SChaoyi Chen #include <dm/lists.h>
10*470c0165SChaoyi Chen #include <dm/device-internal.h>
11*470c0165SChaoyi Chen #include <dm/of_access.h>
12*470c0165SChaoyi Chen #include <dm/pinctrl.h>
13*470c0165SChaoyi Chen #include <i2c.h>
14*470c0165SChaoyi Chen #include <power/pmic.h>
15*470c0165SChaoyi Chen #include <power/regulator.h>
16*470c0165SChaoyi Chen #include <power/fp9931.h>
17*470c0165SChaoyi Chen 
18*470c0165SChaoyi Chen DECLARE_GLOBAL_DATA_PTR;
19*470c0165SChaoyi Chen 
20*470c0165SChaoyi Chen static const struct pmic_child_info pmic_children_info[] = {
21*470c0165SChaoyi Chen 	{ .prefix = "vcom", .driver = FP9931_VCOM_DRIVER_NAME },
22*470c0165SChaoyi Chen 	{ .prefix = "vpos_vneg", .driver = FP9931_VPOS_VNEG_DRIVER_NAME },
23*470c0165SChaoyi Chen 	{ },
24*470c0165SChaoyi Chen };
25*470c0165SChaoyi Chen 
26*470c0165SChaoyi Chen static const struct pmic_child_info thermal_child_info[] = {
27*470c0165SChaoyi Chen 	{ .prefix = "fp9931_thermal", .driver = FP9931_THERMAL_COMTATIBLE_NAME },
28*470c0165SChaoyi Chen 	{ },
29*470c0165SChaoyi Chen };
30*470c0165SChaoyi Chen 
fp9931_reg_count(struct udevice * dev)31*470c0165SChaoyi Chen static int fp9931_reg_count(struct udevice *dev)
32*470c0165SChaoyi Chen {
33*470c0165SChaoyi Chen         return fp9931_REG_MAX;
34*470c0165SChaoyi Chen }
35*470c0165SChaoyi Chen 
fp9931_write(struct udevice * dev,uint reg,const uint8_t * buff,int len)36*470c0165SChaoyi Chen static int fp9931_write(struct udevice *dev, uint reg, const uint8_t *buff, int len)
37*470c0165SChaoyi Chen {
38*470c0165SChaoyi Chen 	int ret;
39*470c0165SChaoyi Chen 
40*470c0165SChaoyi Chen 	ret = dm_i2c_write(dev, reg, buff, len);
41*470c0165SChaoyi Chen 	if (ret) {
42*470c0165SChaoyi Chen 		pr_err("fp9931 failed to write register: %#x, ret:%d\n", reg, ret);
43*470c0165SChaoyi Chen 		return ret;
44*470c0165SChaoyi Chen 	}
45*470c0165SChaoyi Chen 
46*470c0165SChaoyi Chen 	return 0;
47*470c0165SChaoyi Chen }
48*470c0165SChaoyi Chen 
fp9931_read(struct udevice * dev,uint reg,uint8_t * buff,int len)49*470c0165SChaoyi Chen static int fp9931_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
50*470c0165SChaoyi Chen {
51*470c0165SChaoyi Chen 	int ret;
52*470c0165SChaoyi Chen 
53*470c0165SChaoyi Chen 	ret = dm_i2c_read(dev, reg, buff, len);
54*470c0165SChaoyi Chen 	if (ret) {
55*470c0165SChaoyi Chen 		pr_err("fp9931 failed to write register: %#x, ret:%d\n", reg, ret);
56*470c0165SChaoyi Chen 		return ret;
57*470c0165SChaoyi Chen 	}
58*470c0165SChaoyi Chen 
59*470c0165SChaoyi Chen 	return 0;
60*470c0165SChaoyi Chen }
61*470c0165SChaoyi Chen 
pmic_fp9931_probe(struct udevice * dev)62*470c0165SChaoyi Chen static int pmic_fp9931_probe(struct udevice *dev)
63*470c0165SChaoyi Chen {
64*470c0165SChaoyi Chen 	struct fp9931_plat_data *data = dev_get_platdata(dev);
65*470c0165SChaoyi Chen 	uint8_t val;
66*470c0165SChaoyi Chen 	int ret;
67*470c0165SChaoyi Chen 
68*470c0165SChaoyi Chen 	ret = gpio_request_list_by_name(dev, "power-gpios", data->power_gpio, 4,
69*470c0165SChaoyi Chen 					GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
70*470c0165SChaoyi Chen 	if (ret < 0)
71*470c0165SChaoyi Chen 		dev_warn(dev, "fp9931 failed to get power gpios:%d\n", ret);
72*470c0165SChaoyi Chen 
73*470c0165SChaoyi Chen 	data->num_power_gpio = ret;
74*470c0165SChaoyi Chen 
75*470c0165SChaoyi Chen 	ret = gpio_request_by_name(dev, "enable-gpios", 0, &data->enable_gpio, GPIOD_IS_OUT);
76*470c0165SChaoyi Chen 	if (ret) {
77*470c0165SChaoyi Chen 		dev_err(dev, "fp9931 failed to get enable gpios:%d\n", ret);
78*470c0165SChaoyi Chen 		return ret;
79*470c0165SChaoyi Chen 	}
80*470c0165SChaoyi Chen 
81*470c0165SChaoyi Chen 	/* After power on, fp9931 requires 1ms delay time to enter active mode */
82*470c0165SChaoyi Chen 	udelay(1100);
83*470c0165SChaoyi Chen 
84*470c0165SChaoyi Chen 	/* check is device i2c present */
85*470c0165SChaoyi Chen 	ret = dm_i2c_read(dev, FP9931_VCOM_SETTING, &val, 1);
86*470c0165SChaoyi Chen 	if (ret) {
87*470c0165SChaoyi Chen 		dev_warn(dev, "fp9931 i2c not present: %d\n", ret);
88*470c0165SChaoyi Chen 		return ret;
89*470c0165SChaoyi Chen 	}
90*470c0165SChaoyi Chen 
91*470c0165SChaoyi Chen 	return 0;
92*470c0165SChaoyi Chen }
93*470c0165SChaoyi Chen 
pmic_fp9931_bind(struct udevice * dev)94*470c0165SChaoyi Chen static int pmic_fp9931_bind(struct udevice *dev)
95*470c0165SChaoyi Chen {
96*470c0165SChaoyi Chen 	ofnode regulators_node;
97*470c0165SChaoyi Chen 	int children;
98*470c0165SChaoyi Chen 
99*470c0165SChaoyi Chen 	regulators_node = dev_read_subnode(dev, "regulators");
100*470c0165SChaoyi Chen 	if (!ofnode_valid(regulators_node)) {
101*470c0165SChaoyi Chen 		dev_err(dev, "Regulators subnode not found!");
102*470c0165SChaoyi Chen 		return -ENXIO;
103*470c0165SChaoyi Chen 	}
104*470c0165SChaoyi Chen 
105*470c0165SChaoyi Chen 	children = pmic_bind_children(dev, regulators_node, pmic_children_info);
106*470c0165SChaoyi Chen 	if (!children)
107*470c0165SChaoyi Chen 		dev_err(dev, "Failed to bind fp9931 regulator\n");
108*470c0165SChaoyi Chen 
109*470c0165SChaoyi Chen 	children = pmic_bind_children(dev, dev->node, thermal_child_info);
110*470c0165SChaoyi Chen 	if (!children)
111*470c0165SChaoyi Chen 		dev_err(dev, "Failed to bind fp9931 thermal\n");
112*470c0165SChaoyi Chen 
113*470c0165SChaoyi Chen         return 0;
114*470c0165SChaoyi Chen }
115*470c0165SChaoyi Chen 
116*470c0165SChaoyi Chen static struct dm_pmic_ops fp9931_ops = {
117*470c0165SChaoyi Chen 	.reg_count = fp9931_reg_count,
118*470c0165SChaoyi Chen 	.read = fp9931_read,
119*470c0165SChaoyi Chen 	.write = fp9931_write,
120*470c0165SChaoyi Chen };
121*470c0165SChaoyi Chen 
122*470c0165SChaoyi Chen static const struct udevice_id pmic_fp9931_of_match[] = {
123*470c0165SChaoyi Chen 	{ .compatible = "fitipower,fp9931-pmic" },
124*470c0165SChaoyi Chen 	{ }
125*470c0165SChaoyi Chen };
126*470c0165SChaoyi Chen 
127*470c0165SChaoyi Chen U_BOOT_DRIVER(pmic_fp9931) = {
128*470c0165SChaoyi Chen 	.name = "pmic_fp9931",
129*470c0165SChaoyi Chen 	.id = UCLASS_PMIC,
130*470c0165SChaoyi Chen 	.of_match = pmic_fp9931_of_match,
131*470c0165SChaoyi Chen 	.probe = pmic_fp9931_probe,
132*470c0165SChaoyi Chen 	.ops = &fp9931_ops,
133*470c0165SChaoyi Chen 	.bind = pmic_fp9931_bind,
134*470c0165SChaoyi Chen 	.platdata_auto_alloc_size = sizeof(struct fp9931_plat_data),
135*470c0165SChaoyi Chen };
136