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