1// SPDX-License-Identifier: GPL-2.0+
2/*
3 *  Copyright (C) 2018 Flowbird
4 *  Martin Fuzzey  <martin.fuzzey@flowbird.group>
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 <linux/printk.h>
14#include <power/pmic.h>
15#include <power/regulator.h>
16#include <power/da9063_pmic.h>
17
18static const struct pmic_child_info pmic_children_info[] = {
19	{ .prefix = "ldo", .driver = DA9063_LDO_DRIVER },
20	{ .prefix = "b", .driver = DA9063_BUCK_DRIVER },
21	{ },
22};
23
24/*
25 * The register map is non contiguous and attempts to read in the holes fail.
26 * But "pmic dump" tries to dump the full register map.
27 * So define the holes here so we can fix that.
28 */
29struct da9063_reg_hole {
30	u16	first;
31	u16	last;
32};
33
34static const struct da9063_reg_hole da9063_reg_holes[] = {
35	DA9063_REG_HOLE_1,
36	DA9063_REG_HOLE_2,
37	DA9063_REG_HOLE_3,
38	/* These aren't readable. I can't see why from the datasheet but
39	 * attempts to read fail and the kernel marks them unreadable too,
40	 */
41	{DA9063_REG_OTP_COUNT, DA9063_REG_OTP_DATA},
42};
43
44static int da9063_reg_count(struct udevice *dev)
45{
46	return DA9063_NUM_OF_REGS;
47}
48
49static bool da9063_reg_valid(uint reg)
50{
51	int i;
52
53	for (i = 0; i < ARRAY_SIZE(da9063_reg_holes); i++) {
54		const struct da9063_reg_hole *hole = &da9063_reg_holes[i];
55
56		if (reg >= hole->first && reg <= hole->last)
57			return false;
58	}
59
60	return true;
61}
62
63static int da9063_write(struct udevice *dev, uint reg, const uint8_t *buff,
64			int len)
65{
66	if (dm_i2c_write(dev, reg, buff, len)) {
67		pr_err("write error to device: %p register: %#x!", dev, reg);
68		return -EIO;
69	}
70
71	return 0;
72}
73
74static int da9063_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
75{
76	if (!da9063_reg_valid(reg))
77		return -ENODATA;
78
79	if (dm_i2c_read(dev, reg, buff, len)) {
80		pr_err("read error from device: %p register: %#x!", dev, reg);
81		return -EIO;
82	}
83
84	return 0;
85}
86
87static int da9063_bind(struct udevice *dev)
88{
89	ofnode regulators_node;
90	int children;
91
92	regulators_node = dev_read_subnode(dev, "regulators");
93	if (!ofnode_valid(regulators_node)) {
94		debug("%s: %s regulators subnode not found!", __func__,
95		      dev->name);
96		return -ENXIO;
97	}
98
99	debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
100
101	children = pmic_bind_children(dev, regulators_node, pmic_children_info);
102	if (!children)
103		debug("%s: %s - no child found\n", __func__, dev->name);
104
105	/* Always return success for this device */
106	return 0;
107}
108
109static int da9063_probe(struct udevice *dev)
110{
111	return i2c_set_chip_addr_offset_mask(dev, 0x1);
112}
113
114static struct dm_pmic_ops da9063_ops = {
115	.reg_count = da9063_reg_count,
116	.read = da9063_read,
117	.write = da9063_write,
118};
119
120static const struct udevice_id da9063_ids[] = {
121	{ .compatible = "dlg,da9063" },
122	{ }
123};
124
125U_BOOT_DRIVER(pmic_da9063) = {
126	.name = "da9063_pmic",
127	.id = UCLASS_PMIC,
128	.of_match = da9063_ids,
129	.bind = da9063_bind,
130	.probe = da9063_probe,
131	.ops = &da9063_ops,
132};
133