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