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