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); |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 108 | struct uc_pmic_priv *priv = dev_get_uclass_priv(pmic_dev); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 109 | const u8 *reg_defaults; |
| 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); |
| 113 | plat->trans_len = priv->trans_len; |
| 114 | plat->buf_size = plat->reg_count * plat->trans_len; |
| 115 | |
| 116 | plat->reg = calloc(1, plat->buf_size); |
| 117 | if (!plat->reg) { |
| 118 | debug("Canot allocate memory (%d B) for PMIC I2C emulation!\n", |
| 119 | plat->buf_size); |
| 120 | return -ENOMEM; |
| 121 | } |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 122 | |
Simon Glass | 5cdb516 | 2017-05-18 20:09:33 -0600 | [diff] [blame] | 123 | reg_defaults = dev_read_u8_array_ptr(emul, "reg-defaults", |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 124 | plat->buf_size); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 125 | |
| 126 | if (!reg_defaults) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 127 | pr_err("Property \"reg-defaults\" not found for device: %s!", |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 128 | emul->name); |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 129 | free(plat->reg); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 130 | return -EINVAL; |
| 131 | } |
| 132 | |
Lukasz Majewski | ace7504 | 2018-05-15 16:26:39 +0200 | [diff] [blame] | 133 | memcpy(plat->reg, reg_defaults, plat->buf_size); |
Przemyslaw Marczak | d5175dc | 2015-05-13 13:38:32 +0200 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | struct dm_i2c_ops sandbox_i2c_pmic_emul_ops = { |
| 139 | .xfer = sandbox_i2c_pmic_xfer, |
| 140 | }; |
| 141 | |
| 142 | static const struct udevice_id sandbox_i2c_pmic_ids[] = { |
| 143 | { .compatible = "sandbox,i2c-pmic" }, |
| 144 | { } |
| 145 | }; |
| 146 | |
| 147 | U_BOOT_DRIVER(sandbox_i2c_pmic_emul) = { |
| 148 | .name = "sandbox_i2c_pmic_emul", |
| 149 | .id = UCLASS_I2C_EMUL, |
| 150 | .of_match = sandbox_i2c_pmic_ids, |
| 151 | .ofdata_to_platdata = sandbox_i2c_pmic_ofdata_to_platdata, |
| 152 | .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_pmic_plat_data), |
| 153 | .ops = &sandbox_i2c_pmic_emul_ops, |
| 154 | }; |