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