1// SPDX-License-Identifier:      GPL-2.0+
2/*
3 * Copyright 2020 Gateworks Corporation
4 */
5#include <common.h>
6#include <errno.h>
7#include <dm.h>
8#include <i2c.h>
9#include <log.h>
10#include <power/pmic.h>
11#include <power/regulator.h>
12#include <power/mp5416.h>
13#include <asm/global_data.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17static const struct pmic_child_info pmic_children_info[] = {
18	/* buck */
19	{ .prefix = "b", .driver = MP6416_REGULATOR_DRIVER },
20	/* ldo */
21	{ .prefix = "l", .driver = MP6416_REGULATOR_DRIVER },
22	{ },
23};
24
25static int mp5416_reg_count(struct udevice *dev)
26{
27	return MP5416_NUM_OF_REGS - 1;
28}
29
30static int mp5416_write(struct udevice *dev, uint reg, const uint8_t *buff,
31			int len)
32{
33	if (dm_i2c_write(dev, reg, buff, len)) {
34		pr_err("write error to device: %p register: %#x!", dev, reg);
35		return -EIO;
36	}
37
38	return 0;
39}
40
41static int mp5416_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
42{
43	if (dm_i2c_read(dev, reg, buff, len)) {
44		pr_err("read error from device: %p register: %#x!", dev, reg);
45		return -EIO;
46	}
47
48	return 0;
49}
50
51static int mp5416_bind(struct udevice *dev)
52{
53	int children;
54	ofnode regulators_node;
55
56	debug("%s %s\n", __func__, dev->name);
57	regulators_node = dev_read_subnode(dev, "regulators");
58	if (!ofnode_valid(regulators_node)) {
59		debug("%s: %s regulators subnode not found!\n", __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, regulators_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
74static int mp5416_probe(struct udevice *dev)
75{
76	debug("%s %s\n", __func__, dev->name);
77
78	return 0;
79}
80
81static struct dm_pmic_ops mp5416_ops = {
82	.reg_count = mp5416_reg_count,
83	.read = mp5416_read,
84	.write = mp5416_write,
85};
86
87static const struct udevice_id mp5416_ids[] = {
88	{ .compatible = "mps,mp5416", },
89	{ }
90};
91
92U_BOOT_DRIVER(pmic_mp5416) = {
93	.name = "mp5416 pmic",
94	.id = UCLASS_PMIC,
95	.of_match = mp5416_ids,
96	.bind = mp5416_bind,
97	.probe = mp5416_probe,
98	.ops = &mp5416_ops,
99};
100