1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016 Texas Instruments Incorporated, <www.ti.com>
4  * Keerthy <j-keerthy@ti.com>
5  */
6 
7 #include <common.h>
8 #include <fdtdec.h>
9 #include <errno.h>
10 #include <dm.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <power/pmic.h>
14 #include <power/regulator.h>
15 #include <power/palmas.h>
16 #include <dm/device.h>
17 
18 static const struct pmic_child_info pmic_children_info[] = {
19 	{ .prefix = "ldo", .driver = PALMAS_LDO_DRIVER },
20 	{ .prefix = "smps", .driver = PALMAS_SMPS_DRIVER },
21 	{ },
22 };
23 
palmas_write(struct udevice * dev,uint reg,const uint8_t * buff,int len)24 static int palmas_write(struct udevice *dev, uint reg, const uint8_t *buff,
25 			  int len)
26 {
27 	if (dm_i2c_write(dev, reg, buff, len)) {
28 		pr_err("write error to device: %p register: %#x!\n", dev, reg);
29 		return -EIO;
30 	}
31 
32 	return 0;
33 }
34 
palmas_read(struct udevice * dev,uint reg,uint8_t * buff,int len)35 static int palmas_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
36 {
37 	if (dm_i2c_read(dev, reg, buff, len)) {
38 		pr_err("read error from device: %p register: %#x!\n", dev, reg);
39 		return -EIO;
40 	}
41 
42 	return 0;
43 }
44 
palmas_bind(struct udevice * dev)45 static int palmas_bind(struct udevice *dev)
46 {
47 	ofnode pmic_node = ofnode_null(), regulators_node;
48 	ofnode subnode;
49 	int children;
50 
51 	dev_for_each_subnode(subnode, dev) {
52 		const char *name;
53 		char *temp;
54 
55 		name = ofnode_get_name(subnode);
56 		temp = strstr(name, "pmic");
57 		if (temp) {
58 			pmic_node = subnode;
59 			break;
60 		}
61 	}
62 
63 	if (!ofnode_valid(pmic_node)) {
64 		debug("%s: %s pmic subnode not found!\n", __func__, dev->name);
65 		return -ENXIO;
66 	}
67 
68 	regulators_node = ofnode_find_subnode(pmic_node, "regulators");
69 
70 	if (!ofnode_valid(regulators_node)) {
71 		debug("%s: %s reg subnode not found!\n", __func__, dev->name);
72 		return -ENXIO;
73 	}
74 
75 	children = pmic_bind_children(dev, regulators_node, pmic_children_info);
76 	if (!children)
77 		debug("%s: %s - no child found\n", __func__, dev->name);
78 
79 	/* Always return success for this device */
80 	return 0;
81 }
82 
83 static struct dm_pmic_ops palmas_ops = {
84 	.read = palmas_read,
85 	.write = palmas_write,
86 };
87 
88 static const struct udevice_id palmas_ids[] = {
89 	{ .compatible = "ti,tps659038", .data = TPS659038 },
90 	{ .compatible = "ti,tps65917" , .data = TPS65917 },
91 	{ }
92 };
93 
94 U_BOOT_DRIVER(pmic_palmas) = {
95 	.name = "palmas_pmic",
96 	.id = UCLASS_PMIC,
97 	.of_match = palmas_ids,
98 	.bind = palmas_bind,
99 	.ops = &palmas_ops,
100 };
101