Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 1 | /* |
| 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 Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 13 | #include <power/rk8xx_pmic.h> |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 14 | #include <power/pmic.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | static const struct pmic_child_info pmic_children_info[] = { |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 19 | { .prefix = "DCDC_REG", .driver = "rk8xx_buck"}, |
| 20 | { .prefix = "LDO_REG", .driver = "rk8xx_ldo"}, |
| 21 | { .prefix = "SWITCH_REG", .driver = "rk8xx_switch"}, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 22 | { }, |
| 23 | }; |
| 24 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 25 | static int rk8xx_reg_count(struct udevice *dev) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 26 | { |
| 27 | return RK808_NUM_OF_REGS; |
| 28 | } |
| 29 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 30 | static int rk8xx_write(struct udevice *dev, uint reg, const uint8_t *buff, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 31 | int len) |
| 32 | { |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 33 | int ret; |
| 34 | |
| 35 | ret = dm_i2c_write(dev, reg, buff, len); |
| 36 | if (ret) { |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 37 | debug("write error to device: %p register: %#x!", dev, reg); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 38 | return ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 44 | static int rk8xx_read(struct udevice *dev, uint reg, uint8_t *buff, int len) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 45 | { |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 46 | int ret; |
| 47 | |
| 48 | ret = dm_i2c_read(dev, reg, buff, len); |
| 49 | if (ret) { |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 50 | debug("read error from device: %p register: %#x!", dev, reg); |
Simon Glass | 7d57799 | 2016-01-21 19:43:58 -0700 | [diff] [blame] | 51 | return ret; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 58 | static int rk8xx_bind(struct udevice *dev) |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 59 | { |
Simon Glass | 7a869e6 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 60 | ofnode regulators_node; |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 61 | int children; |
| 62 | |
Simon Glass | 7a869e6 | 2017-05-18 20:09:32 -0600 | [diff] [blame] | 63 | regulators_node = dev_read_subnode(dev, "regulators"); |
| 64 | if (!ofnode_valid(regulators_node)) { |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 65 | 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 Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 81 | static int rk8xx_probe(struct udevice *dev) |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 82 | { |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 83 | struct rk8xx_priv *priv = dev_get_priv(dev); |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 84 | uint8_t msb, lsb; |
| 85 | |
| 86 | /* read Chip variant */ |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 87 | rk8xx_read(dev, ID_MSB, &msb, 1); |
| 88 | rk8xx_read(dev, ID_LSB, &lsb, 1); |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 89 | |
| 90 | priv->variant = ((msb << 8) | lsb) & RK8XX_ID_MSK; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 95 | static struct dm_pmic_ops rk8xx_ops = { |
| 96 | .reg_count = rk8xx_reg_count, |
| 97 | .read = rk8xx_read, |
| 98 | .write = rk8xx_write, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 101 | static const struct udevice_id rk8xx_ids[] = { |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 102 | { .compatible = "rockchip,rk808" }, |
Jacob Chen | d77af8a | 2017-05-02 14:54:49 +0800 | [diff] [blame] | 103 | { .compatible = "rockchip,rk818" }, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 104 | { } |
| 105 | }; |
| 106 | |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 107 | U_BOOT_DRIVER(pmic_rk8xx) = { |
| 108 | .name = "rk8xx pmic", |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 109 | .id = UCLASS_PMIC, |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 110 | .of_match = rk8xx_ids, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 111 | #if CONFIG_IS_ENABLED(PMIC_CHILDREN) |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 112 | .bind = rk8xx_bind, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 113 | #endif |
Jacob Chen | 453c5a9 | 2017-05-02 14:54:52 +0800 | [diff] [blame] | 114 | .probe = rk8xx_probe, |
| 115 | .ops = &rk8xx_ops, |
Simon Glass | 2a4febf | 2016-01-21 19:43:29 -0700 | [diff] [blame] | 116 | }; |