1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) 2018 Theobroma Systems Design und Consulting GmbH
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <log.h>
9 #include <dm/device-internal.h>
10 #include <dm/device_compat.h>
11 #include <dm/lists.h>
12 #include <i2c.h>
13 #include <power/fan53555.h>
14 #include <power/pmic.h>
15 #include <power/regulator.h>
16 
pmic_fan53555_reg_count(struct udevice * dev)17 static int pmic_fan53555_reg_count(struct udevice *dev)
18 {
19 	return 1;
20 };
21 
pmic_fan53555_read(struct udevice * dev,uint reg,u8 * buff,int len)22 static int pmic_fan53555_read(struct udevice *dev, uint reg,
23 			      u8 *buff, int len)
24 {
25 	if (dm_i2c_read(dev, reg, buff, len)) {
26 		pr_err("%s: read error for register: %#x!", dev->name, reg);
27 		return -EIO;
28 	}
29 
30 	return 0;
31 }
32 
pmic_fan53555_write(struct udevice * dev,uint reg,const u8 * buff,int len)33 static int pmic_fan53555_write(struct udevice *dev, uint reg,
34 			       const u8 *buff, int len)
35 {
36 	if (dm_i2c_write(dev, reg, buff, len)) {
37 		pr_err("%s: write error for register: %#x!", dev->name, reg);
38 		return -EIO;
39 	}
40 
41 	return 0;
42 }
43 
pmic_fan53555_bind(struct udevice * dev)44 static int pmic_fan53555_bind(struct udevice *dev)
45 {
46 	/*
47 	 * The FAN53555 has only a single regulator and therefore doesn't
48 	 * have a subnode.  So we have to rebind a child device (the one
49 	 * regulator) here.
50 	 */
51 
52 	const char *regulator_driver_name = "fan53555_regulator";
53 	struct udevice *child;
54 	struct driver *drv;
55 
56 	debug("%s\n", __func__);
57 
58 	drv = lists_driver_lookup_name(regulator_driver_name);
59 	if (!drv) {
60 		dev_err(dev, "no driver '%s'\n", regulator_driver_name);
61 		return -ENOENT;
62 	}
63 
64 	return device_bind_with_driver_data(dev, drv, "SW", dev->driver_data,
65 					    dev_ofnode(dev), &child);
66 };
67 
68 static struct dm_pmic_ops pmic_fan53555_ops = {
69 	.reg_count = pmic_fan53555_reg_count,
70 	.read = pmic_fan53555_read,
71 	.write = pmic_fan53555_write,
72 };
73 
74 static const struct udevice_id pmic_fan53555_match[] = {
75 	{ .compatible = "fcs,fan53555", .data = FAN53555_VENDOR_FAIRCHILD, },
76 	{ .compatible = "silergy,syr827", .data = FAN53555_VENDOR_SILERGY, },
77 	{ .compatible = "silergy,syr828", .data = FAN53555_VENDOR_SILERGY, },
78 	{ },
79 };
80 
81 U_BOOT_DRIVER(pmic_fan53555) = {
82 	.name = "pmic_fan53555",
83 	.id = UCLASS_PMIC,
84 	.of_match = pmic_fan53555_match,
85 	.bind = pmic_fan53555_bind,
86 	.ops = &pmic_fan53555_ops,
87 };
88