1// SPDX-License-Identifier:      GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 */
5
6#include <common.h>
7#include <errno.h>
8#include <dm.h>
9#include <i2c.h>
10#include <log.h>
11#include <asm/global_data.h>
12#include <linux/printk.h>
13#include <power/pmic.h>
14#include <power/regulator.h>
15#include <power/bd71837.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19static const struct pmic_child_info pmic_children_info[] = {
20	/* buck */
21	{ .prefix = "b", .driver = BD718XX_REGULATOR_DRIVER},
22	/* ldo */
23	{ .prefix = "l", .driver = BD718XX_REGULATOR_DRIVER},
24	{ },
25};
26
27static int bd71837_reg_count(struct udevice *dev)
28{
29	return BD718XX_MAX_REGISTER - 1;
30}
31
32static int bd71837_write(struct udevice *dev, uint reg, const uint8_t *buff,
33			 int len)
34{
35	if (dm_i2c_write(dev, reg, buff, len)) {
36		pr_err("write error to device: %p register: %#x!", dev, reg);
37		return -EIO;
38	}
39
40	return 0;
41}
42
43static int bd71837_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
44{
45	if (dm_i2c_read(dev, reg, buff, len)) {
46		pr_err("read error from device: %p register: %#x!", dev, reg);
47		return -EIO;
48	}
49
50	return 0;
51}
52
53static int bd71837_bind(struct udevice *dev)
54{
55	int children;
56	ofnode regulators_node;
57
58	regulators_node = dev_read_subnode(dev, "regulators");
59	if (!ofnode_valid(regulators_node)) {
60		debug("%s: %s regulators subnode not found!\n", __func__,
61		      dev->name);
62		return -ENXIO;
63	}
64
65	debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
66
67	if (CONFIG_IS_ENABLED(PMIC_CHILDREN)) {
68		children = pmic_bind_children(dev, regulators_node, pmic_children_info);
69		if (!children)
70			debug("%s: %s - no child found\n", __func__, dev->name);
71	}
72	/* Always return success for this device */
73	return 0;
74}
75
76static int bd718x7_probe(struct udevice *dev)
77{
78	int ret;
79	uint8_t mask = BD718XX_REGLOCK_PWRSEQ | BD718XX_REGLOCK_VREG;
80
81	/* Unlock the PMIC regulator control before probing the children */
82	ret = pmic_clrsetbits(dev, BD718XX_REGLOCK, mask, 0);
83	if (ret) {
84		debug("%s: %s Failed to unlock regulator control\n", __func__,
85		      dev->name);
86		return ret;
87	}
88	debug("%s: '%s' - BD718x7 PMIC registers unlocked\n", __func__,
89	      dev->name);
90
91	return 0;
92}
93
94static struct dm_pmic_ops bd71837_ops = {
95	.reg_count = bd71837_reg_count,
96	.read = bd71837_read,
97	.write = bd71837_write,
98};
99
100static const struct udevice_id bd71837_ids[] = {
101	{ .compatible = "rohm,bd71837", .data = ROHM_CHIP_TYPE_BD71837, },
102	{ .compatible = "rohm,bd71847", .data = ROHM_CHIP_TYPE_BD71847, },
103	{ }
104};
105
106U_BOOT_DRIVER(pmic_bd71837) = {
107	.name = "bd71837 pmic",
108	.id = UCLASS_PMIC,
109	.of_match = bd71837_ids,
110	.bind = bd71837_bind,
111	.probe = bd718x7_probe,
112	.ops = &bd71837_ops,
113};
114