blob: 09b9b54c62f81bfa77d2436ff88982b29ca8b561 [file] [log] [blame]
Simon Glass2a4febf2016-01-21 19:43:29 -07001/*
2 * Copyright (C) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <fdtdec.h>
12#include <libfdt.h>
Jacob Chen453c5a92017-05-02 14:54:52 +080013#include <power/rk8xx_pmic.h>
Simon Glass2a4febf2016-01-21 19:43:29 -070014#include <power/pmic.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18static const struct pmic_child_info pmic_children_info[] = {
Jacob Chen453c5a92017-05-02 14:54:52 +080019 { .prefix = "DCDC_REG", .driver = "rk8xx_buck"},
20 { .prefix = "LDO_REG", .driver = "rk8xx_ldo"},
21 { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"},
Simon Glass2a4febf2016-01-21 19:43:29 -070022 { },
23};
24
Jacob Chen453c5a92017-05-02 14:54:52 +080025static int rk8xx_reg_count(struct udevice *dev)
Simon Glass2a4febf2016-01-21 19:43:29 -070026{
27 return RK808_NUM_OF_REGS;
28}
29
Jacob Chen453c5a92017-05-02 14:54:52 +080030static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff,
Simon Glass2a4febf2016-01-21 19:43:29 -070031 int len)
32{
Simon Glass7d577992016-01-21 19:43:58 -070033 int ret;
34
35 ret = dm_i2c_write(dev, reg, buff, len);
36 if (ret) {
Simon Glass2a4febf2016-01-21 19:43:29 -070037 debug("write error to device: %p register: %#x!", dev, reg);
Simon Glass7d577992016-01-21 19:43:58 -070038 return ret;
Simon Glass2a4febf2016-01-21 19:43:29 -070039 }
40
41 return 0;
42}
43
Jacob Chen453c5a92017-05-02 14:54:52 +080044static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len)
Simon Glass2a4febf2016-01-21 19:43:29 -070045{
Simon Glass7d577992016-01-21 19:43:58 -070046 int ret;
47
48 ret = dm_i2c_read(dev, reg, buff, len);
49 if (ret) {
Simon Glass2a4febf2016-01-21 19:43:29 -070050 debug("read error from device: %p register: %#x!", dev, reg);
Simon Glass7d577992016-01-21 19:43:58 -070051 return ret;
Simon Glass2a4febf2016-01-21 19:43:29 -070052 }
53
54 return 0;
55}
56
57#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen453c5a92017-05-02 14:54:52 +080058static int rk8xx_bind(struct udevice *dev)
Simon Glass2a4febf2016-01-21 19:43:29 -070059{
Simon Glass7a869e62017-05-18 20:09:32 -060060 ofnode regulators_node;
Simon Glass2a4febf2016-01-21 19:43:29 -070061 int children;
62
Simon Glass7a869e62017-05-18 20:09:32 -060063 regulators_node = dev_read_subnode(dev, "regulators");
64 if (!ofnode_valid(regulators_node)) {
Simon Glass2a4febf2016-01-21 19:43:29 -070065 debug("%s: %s regulators subnode not found!", __func__,
66 dev->name);
67 return -ENXIO;
68 }
69
70 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
71
72 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
73 if (!children)
74 debug("%s: %s - no child found\n", __func__, dev->name);
75
76 /* Always return success for this device */
77 return 0;
78}
79#endif
80
Jacob Chen453c5a92017-05-02 14:54:52 +080081static int rk8xx_probe(struct udevice *dev)
Jacob Chend77af8a2017-05-02 14:54:49 +080082{
Jacob Chen453c5a92017-05-02 14:54:52 +080083 struct rk8xx_priv *priv = dev_get_priv(dev);
Jacob Chend77af8a2017-05-02 14:54:49 +080084 uint8_t msb, lsb;
85
86 /* read Chip variant */
Jacob Chen453c5a92017-05-02 14:54:52 +080087 rk8xx_read(dev, ID_MSB, &msb, 1);
88 rk8xx_read(dev, ID_LSB, &lsb, 1);
Jacob Chend77af8a2017-05-02 14:54:49 +080089
90 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
91
92 return 0;
93}
94
Jacob Chen453c5a92017-05-02 14:54:52 +080095static struct dm_pmic_ops rk8xx_ops = {
96 .reg_count = rk8xx_reg_count,
97 .read = rk8xx_read,
98 .write = rk8xx_write,
Simon Glass2a4febf2016-01-21 19:43:29 -070099};
100
Jacob Chen453c5a92017-05-02 14:54:52 +0800101static const struct udevice_id rk8xx_ids[] = {
Simon Glass2a4febf2016-01-21 19:43:29 -0700102 { .compatible = "rockchip,rk808" },
Jacob Chend77af8a2017-05-02 14:54:49 +0800103 { .compatible = "rockchip,rk818" },
Simon Glass2a4febf2016-01-21 19:43:29 -0700104 { }
105};
106
Jacob Chen453c5a92017-05-02 14:54:52 +0800107U_BOOT_DRIVER(pmic_rk8xx) = {
108 .name = "rk8xx pmic",
Simon Glass2a4febf2016-01-21 19:43:29 -0700109 .id = UCLASS_PMIC,
Jacob Chen453c5a92017-05-02 14:54:52 +0800110 .of_match = rk8xx_ids,
Simon Glass2a4febf2016-01-21 19:43:29 -0700111#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen453c5a92017-05-02 14:54:52 +0800112 .bind = rk8xx_bind,
Simon Glass2a4febf2016-01-21 19:43:29 -0700113#endif
Jacob Chen453c5a92017-05-02 14:54:52 +0800114 .probe = rk8xx_probe,
115 .ops = &rk8xx_ops,
Simon Glass2a4febf2016-01-21 19:43:29 -0700116};