xref: /openbmc/u-boot/drivers/power/pmic/act8846.c (revision e1a71f8b)
1 /*
2  * Copyright (C) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <fdtdec.h>
12 #include <libfdt.h>
13 #include <power/act8846_pmic.h>
14 #include <power/pmic.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 static const struct pmic_child_info pmic_children_info[] = {
19 	{ .prefix = "REG", .driver = "act8846_reg"},
20 	{ },
21 };
22 
23 static int act8846_reg_count(struct udevice *dev)
24 {
25 	return ACT8846_NUM_OF_REGS;
26 }
27 
28 static int act8846_write(struct udevice *dev, uint reg, const uint8_t *buff,
29 			  int len)
30 {
31 	if (dm_i2c_write(dev, reg, buff, len)) {
32 		debug("write error to device: %p register: %#x!\n", dev, reg);
33 		return -EIO;
34 	}
35 
36 	return 0;
37 }
38 
39 static int act8846_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
40 {
41 	if (dm_i2c_read(dev, reg, buff, len)) {
42 		debug("read error from device: %p register: %#x!\n", dev, reg);
43 		return -EIO;
44 	}
45 
46 	return 0;
47 }
48 
49 static int act8846_bind(struct udevice *dev)
50 {
51 	const void *blob = gd->fdt_blob;
52 	int regulators_node;
53 	int children;
54 
55 	regulators_node = fdt_subnode_offset(blob, dev_of_offset(dev),
56 					     "regulators");
57 	if (regulators_node <= 0) {
58 		debug("%s: %s regulators subnode not found!", __func__,
59 		      dev->name);
60 		return -ENXIO;
61 	}
62 
63 	debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
64 
65 	children = pmic_bind_children(dev, regulators_node, pmic_children_info);
66 	if (!children)
67 		debug("%s: %s - no child found\n", __func__, dev->name);
68 
69 	/* Always return success for this device */
70 	return 0;
71 }
72 
73 static struct dm_pmic_ops act8846_ops = {
74 	.reg_count = act8846_reg_count,
75 	.read = act8846_read,
76 	.write = act8846_write,
77 };
78 
79 static const struct udevice_id act8846_ids[] = {
80 	{ .compatible = "active-semi,act8846" },
81 	{ }
82 };
83 
84 U_BOOT_DRIVER(pmic_act8846) = {
85 	.name = "act8846 pmic",
86 	.id = UCLASS_PMIC,
87 	.of_match = act8846_ids,
88 	.bind = act8846_bind,
89 	.ops = &act8846_ops,
90 };
91