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