blob: 394e2ff9db38abd716d752e022a1b918e00b7781 [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{
60 const void *blob = gd->fdt_blob;
61 int regulators_node;
62 int children;
63
Simon Glasse160f7d2017-01-17 16:52:55 -070064 regulators_node = fdt_subnode_offset(blob, dev_of_offset(dev),
Simon Glass2a4febf2016-01-21 19:43:29 -070065 "regulators");
66 if (regulators_node <= 0) {
67 debug("%s: %s regulators subnode not found!", __func__,
68 dev->name);
69 return -ENXIO;
70 }
71
72 debug("%s: '%s' - found regulators subnode\n", __func__, dev->name);
73
74 children = pmic_bind_children(dev, regulators_node, pmic_children_info);
75 if (!children)
76 debug("%s: %s - no child found\n", __func__, dev->name);
77
78 /* Always return success for this device */
79 return 0;
80}
81#endif
82
Jacob Chen453c5a92017-05-02 14:54:52 +080083static int rk8xx_probe(struct udevice *dev)
Jacob Chend77af8a2017-05-02 14:54:49 +080084{
Jacob Chen453c5a92017-05-02 14:54:52 +080085 struct rk8xx_priv *priv = dev_get_priv(dev);
Jacob Chend77af8a2017-05-02 14:54:49 +080086 uint8_t msb, lsb;
87
88 /* read Chip variant */
Jacob Chen453c5a92017-05-02 14:54:52 +080089 rk8xx_read(dev, ID_MSB, &msb, 1);
90 rk8xx_read(dev, ID_LSB, &lsb, 1);
Jacob Chend77af8a2017-05-02 14:54:49 +080091
92 priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK;
93
94 return 0;
95}
96
Jacob Chen453c5a92017-05-02 14:54:52 +080097static struct dm_pmic_ops rk8xx_ops = {
98 .reg_count = rk8xx_reg_count,
99 .read = rk8xx_read,
100 .write = rk8xx_write,
Simon Glass2a4febf2016-01-21 19:43:29 -0700101};
102
Jacob Chen453c5a92017-05-02 14:54:52 +0800103static const struct udevice_id rk8xx_ids[] = {
Simon Glass2a4febf2016-01-21 19:43:29 -0700104 { .compatible = "rockchip,rk808" },
Jacob Chend77af8a2017-05-02 14:54:49 +0800105 { .compatible = "rockchip,rk818" },
Simon Glass2a4febf2016-01-21 19:43:29 -0700106 { }
107};
108
Jacob Chen453c5a92017-05-02 14:54:52 +0800109U_BOOT_DRIVER(pmic_rk8xx) = {
110 .name = "rk8xx pmic",
Simon Glass2a4febf2016-01-21 19:43:29 -0700111 .id = UCLASS_PMIC,
Jacob Chen453c5a92017-05-02 14:54:52 +0800112 .of_match = rk8xx_ids,
Simon Glass2a4febf2016-01-21 19:43:29 -0700113#if CONFIG_IS_ENABLED(PMIC_CHILDREN)
Jacob Chen453c5a92017-05-02 14:54:52 +0800114 .bind = rk8xx_bind,
Simon Glass2a4febf2016-01-21 19:43:29 -0700115#endif
Jacob Chen453c5a92017-05-02 14:54:52 +0800116 .probe = rk8xx_probe,
117 .ops = &rk8xx_ops,
Simon Glass2a4febf2016-01-21 19:43:29 -0700118};