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