xref: /openbmc/u-boot/drivers/power/pmic/s5m8767.c (revision 8ee59472)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <fdtdec.h>
8 #include <errno.h>
9 #include <dm.h>
10 #include <i2c.h>
11 #include <power/pmic.h>
12 #include <power/regulator.h>
13 #include <power/s5m8767.h>
14 
15 static const struct pmic_child_info pmic_children_info[] = {
16 	{ .prefix = "LDO", .driver = S5M8767_LDO_DRIVER },
17 	{ .prefix = "BUCK", .driver = S5M8767_BUCK_DRIVER },
18 	{ },
19 };
20 
21 static int s5m8767_reg_count(struct udevice *dev)
22 {
23 	return S5M8767_NUM_OF_REGS;
24 }
25 
26 static int s5m8767_write(struct udevice *dev, uint reg, const uint8_t *buff,
27 			  int len)
28 {
29 	if (dm_i2c_write(dev, reg, buff, len)) {
30 		pr_err("write error to device: %p register: %#x!", dev, reg);
31 		return -EIO;
32 	}
33 
34 	return 0;
35 }
36 
37 static int s5m8767_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
38 {
39 	if (dm_i2c_read(dev, reg, buff, len)) {
40 		pr_err("read error from device: %p register: %#x!", dev, reg);
41 		return -EIO;
42 	}
43 
44 	return 0;
45 }
46 
47 int s5m8767_enable_32khz_cp(struct udevice *dev)
48 {
49 	return pmic_clrsetbits(dev, S5M8767_EN32KHZ_CP, 0, 1 << 1);
50 }
51 
52 static int s5m8767_bind(struct udevice *dev)
53 {
54 	int children;
55 	ofnode node;
56 
57 	node = dev_read_subnode(dev, "regulators");
58 	if (!ofnode_valid(node)) {
59 		debug("%s: %s regulators subnode not found!", __func__,
60 		      dev->name);
61 		return -ENXIO;
62 	}
63 
64 	debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
65 
66 	children = pmic_bind_children(dev, node, pmic_children_info);
67 	if (!children)
68 		debug("%s: %s - no child found\n", __func__, dev->name);
69 
70 	/* Always return success for this device */
71 	return 0;
72 }
73 
74 static struct dm_pmic_ops s5m8767_ops = {
75 	.reg_count = s5m8767_reg_count,
76 	.read = s5m8767_read,
77 	.write = s5m8767_write,
78 };
79 
80 static const struct udevice_id s5m8767_ids[] = {
81 	{ .compatible = "samsung,s5m8767-pmic" },
82 	{ }
83 };
84 
85 U_BOOT_DRIVER(pmic_s5m8767) = {
86 	.name = "s5m8767_pmic",
87 	.id = UCLASS_PMIC,
88 	.of_match = s5m8767_ids,
89 	.bind = s5m8767_bind,
90 	.ops = &s5m8767_ops,
91 };
92