Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Samsung Electronics |
| 4 | * Przemyslaw Marczak <p.marczak@samsung.com> |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 8 | #include <errno.h> |
| 9 | #include <dm.h> |
| 10 | #include <i2c.h> |
| 11 | #include <power/pmic.h> |
| 12 | #include <power/sandbox_pmic.h> |
| 13 | |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 14 | /** |
| 15 | * struct sandbox_i2c_pmic_plat_data - platform data for the PMIC |
| 16 | * |
| 17 | * @rw_reg: PMICs register of the chip I/O transaction |
| 18 | * @reg: PMICs registers array |
| 19 | */ |
| 20 | struct sandbox_i2c_pmic_plat_data { |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 21 | u8 rw_reg, rw_idx; |
| 22 | u8 reg_count; |
| 23 | u8 trans_len; |
| 24 | u8 buf_size; |
| 25 | u8 *reg; |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static int sandbox_i2c_pmic_read_data(struct udevice *emul, uchar chip, |
| 29 | uchar *buffer, int len) |
| 30 | { |
| 31 | struct sandbox_i2c_pmic_plat_data *plat = dev_get_platdata(emul); |
| 32 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 33 | if (plat->rw_idx + len > plat->buf_size) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 34 | pr_err("Request exceeds PMIC register range! Max register: %#x", |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 35 | plat->reg_count); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 36 | return -EFAULT; |
| 37 | } |
| 38 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 39 | debug("Read PMIC: %#x at register: %#x idx: %#x count: %d\n", |
| 40 | (unsigned int)chip & 0xff, plat->rw_reg, plat->rw_idx, len); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 41 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 42 | memcpy(buffer, plat->reg + plat->rw_idx, len); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int sandbox_i2c_pmic_write_data(struct udevice *emul, uchar chip, |
| 48 | uchar *buffer, int len, |
| 49 | bool next_is_read) |
| 50 | { |
| 51 | struct sandbox_i2c_pmic_plat_data *plat = dev_get_platdata(emul); |
| 52 | |
| 53 | /* Probe only */ |
| 54 | if (!len) |
| 55 | return 0; |
| 56 | |
| 57 | /* Set PMIC register for I/O */ |
| 58 | plat->rw_reg = *buffer; |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 59 | plat->rw_idx = plat->rw_reg * plat->trans_len; |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 60 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 61 | debug("Write PMIC: %#x at register: %#x idx: %#x count: %d\n", |
| 62 | (unsigned int)chip & 0xff, plat->rw_reg, plat->rw_idx, len); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 63 | |
| 64 | /* For read operation, set (write) only chip reg */ |
| 65 | if (next_is_read) |
| 66 | return 0; |
| 67 | |
| 68 | buffer++; |
| 69 | len--; |
| 70 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 71 | if (plat->rw_idx + len > plat->buf_size) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 72 | pr_err("Request exceeds PMIC register range! Max register: %#x", |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 73 | plat->reg_count); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 76 | memcpy(plat->reg + plat->rw_idx, buffer, len); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int sandbox_i2c_pmic_xfer(struct udevice *emul, struct i2c_msg *msg, |
| 82 | int nmsgs) |
| 83 | { |
| 84 | int ret = 0; |
| 85 | |
| 86 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 87 | bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD); |
| 88 | if (msg->flags & I2C_M_RD) { |
| 89 | ret = sandbox_i2c_pmic_read_data(emul, msg->addr, |
| 90 | msg->buf, msg->len); |
| 91 | } else { |
| 92 | ret = sandbox_i2c_pmic_write_data(emul, msg->addr, |
| 93 | msg->buf, msg->len, |
| 94 | next_is_read); |
| 95 | } |
| 96 | |
| 97 | if (ret) |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int sandbox_i2c_pmic_ofdata_to_platdata(struct udevice *emul) |
| 105 | { |
| 106 | struct sandbox_i2c_pmic_plat_data *plat = dev_get_platdata(emul); |
Simon Glass | 17b56f6 | 2018-11-18 08:14:34 -0700 | [diff] [blame] | 107 | struct udevice *pmic_dev = i2c_emul_get_device(emul); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 108 | |
| 109 | debug("%s:%d Setting PMIC default registers\n", __func__, __LINE__); |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 110 | plat->reg_count = pmic_reg_count(pmic_dev); |
Simon Glass | 9779eb9 | 2020-01-27 08:49:53 -0700 | [diff] [blame] | 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int sandbox_i2c_pmic_probe(struct udevice *emul) |
| 116 | { |
| 117 | struct sandbox_i2c_pmic_plat_data *plat = dev_get_platdata(emul); |
| 118 | struct udevice *pmic_dev = i2c_emul_get_device(emul); |
| 119 | struct uc_pmic_priv *upriv = dev_get_uclass_priv(pmic_dev); |
| 120 | const u8 *reg_defaults; |
| 121 | |
| 122 | plat->trans_len = upriv->trans_len; |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 123 | plat->buf_size = plat->reg_count * plat->trans_len; |
| 124 | |
| 125 | plat->reg = calloc(1, plat->buf_size); |
| 126 | if (!plat->reg) { |
| 127 | debug("Canot allocate memory (%d B) for PMIC I2C emulation!\n", |
| 128 | plat->buf_size); |
| 129 | return -ENOMEM; |
| 130 | } |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 131 | |
Simon Glass | 5cdb516 | 2017-05-18 20:09:33 -0600 | [diff] [blame] | 132 | reg_defaults = dev_read_u8_array_ptr(emul, "reg-defaults", |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 133 | plat->buf_size); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 134 | |
| 135 | if (!reg_defaults) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 136 | pr_err("Property \"reg-defaults\" not found for device: %s!", |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 137 | emul->name); |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 138 | free(plat->reg); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 139 | return -EINVAL; |
| 140 | } |
| 141 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 142 | memcpy(plat->reg, reg_defaults, plat->buf_size); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | struct dm_i2c_ops sandbox_i2c_pmic_emul_ops = { |
| 148 | .xfer = sandbox_i2c_pmic_xfer, |
| 149 | }; |
| 150 | |
| 151 | static const struct udevice_id sandbox_i2c_pmic_ids[] = { |
| 152 | { .compatible = "sandbox,i2c-pmic" }, |
| 153 | { } |
| 154 | }; |
| 155 | |
| 156 | U_BOOT_DRIVER(sandbox_i2c_pmic_emul) = { |
| 157 | .name = "sandbox_i2c_pmic_emul", |
| 158 | .id = UCLASS_I2C_EMUL, |
| 159 | .of_match = sandbox_i2c_pmic_ids, |
| 160 | .ofdata_to_platdata = sandbox_i2c_pmic_ofdata_to_platdata, |
Simon Glass | 9779eb9 | 2020-01-27 08:49:53 -0700 | [diff] [blame] | 161 | .probe = sandbox_i2c_pmic_probe, |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 162 | .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_pmic_plat_data), |
| 163 | .ops = &sandbox_i2c_pmic_emul_ops, |
| 164 | }; |