blob: b58c8302cf726ed92f349fad1636f6febc0e7cfb [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +02002/*
3 * Copyright (C) 2015 Samsung Electronics
4 * Przemyslaw Marczak <p.marczak@samsung.com>
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +02005 */
6
7#include <common.h>
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +02008#include <errno.h>
9#include <dm.h>
10#include <i2c.h>
11#include <power/pmic.h>
12#include <power/sandbox_pmic.h>
13
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020014/**
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 */
20struct sandbox_i2c_pmic_plat_data {
Lukasz Majewskiace75042018-05-15 16:26:39 +020021 u8 rw_reg, rw_idx;
22 u8 reg_count;
23 u8 trans_len;
24 u8 buf_size;
25 u8 *reg;
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020026};
27
28static 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 Majewskiace75042018-05-15 16:26:39 +020033 if (plat->rw_idx + len > plat->buf_size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090034 pr_err("Request exceeds PMIC register range! Max register: %#x",
Lukasz Majewskiace75042018-05-15 16:26:39 +020035 plat->reg_count);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020036 return -EFAULT;
37 }
38
Lukasz Majewskiace75042018-05-15 16:26:39 +020039 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 Marczakd5175dc2015-05-13 13:38:32 +020041
Lukasz Majewskiace75042018-05-15 16:26:39 +020042 memcpy(buffer, plat->reg + plat->rw_idx, len);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020043
44 return 0;
45}
46
47static 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 Majewskiace75042018-05-15 16:26:39 +020059 plat->rw_idx = plat->rw_reg * plat->trans_len;
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020060
Lukasz Majewskiace75042018-05-15 16:26:39 +020061 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 Marczakd5175dc2015-05-13 13:38:32 +020063
64 /* For read operation, set (write) only chip reg */
65 if (next_is_read)
66 return 0;
67
68 buffer++;
69 len--;
70
Lukasz Majewskiace75042018-05-15 16:26:39 +020071 if (plat->rw_idx + len > plat->buf_size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090072 pr_err("Request exceeds PMIC register range! Max register: %#x",
Lukasz Majewskiace75042018-05-15 16:26:39 +020073 plat->reg_count);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020074 }
75
Lukasz Majewskiace75042018-05-15 16:26:39 +020076 memcpy(plat->reg + plat->rw_idx, buffer, len);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020077
78 return 0;
79}
80
81static 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
104static int sandbox_i2c_pmic_ofdata_to_platdata(struct udevice *emul)
105{
106 struct sandbox_i2c_pmic_plat_data *plat = dev_get_platdata(emul);
Simon Glass17b56f62018-11-18 08:14:34 -0700107 struct udevice *pmic_dev = i2c_emul_get_device(emul);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200108
109 debug("%s:%d Setting PMIC default registers\n", __func__, __LINE__);
Lukasz Majewskiace75042018-05-15 16:26:39 +0200110 plat->reg_count = pmic_reg_count(pmic_dev);
Simon Glass9779eb92020-01-27 08:49:53 -0700111
112 return 0;
113}
114
115static 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 Majewskiace75042018-05-15 16:26:39 +0200123 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 Marczakd5175dc2015-05-13 13:38:32 +0200131
Simon Glass5cdb5162017-05-18 20:09:33 -0600132 reg_defaults = dev_read_u8_array_ptr(emul, "reg-defaults",
Lukasz Majewskiace75042018-05-15 16:26:39 +0200133 plat->buf_size);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200134
135 if (!reg_defaults) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900136 pr_err("Property \"reg-defaults\" not found for device: %s!",
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200137 emul->name);
Lukasz Majewskiace75042018-05-15 16:26:39 +0200138 free(plat->reg);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200139 return -EINVAL;
140 }
141
Lukasz Majewskiace75042018-05-15 16:26:39 +0200142 memcpy(plat->reg, reg_defaults, plat->buf_size);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200143
144 return 0;
145}
146
147struct dm_i2c_ops sandbox_i2c_pmic_emul_ops = {
148 .xfer = sandbox_i2c_pmic_xfer,
149};
150
151static const struct udevice_id sandbox_i2c_pmic_ids[] = {
152 { .compatible = "sandbox,i2c-pmic" },
153 { }
154};
155
156U_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 Glass9779eb92020-01-27 08:49:53 -0700161 .probe = sandbox_i2c_pmic_probe,
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200162 .platdata_auto_alloc_size = sizeof(struct sandbox_i2c_pmic_plat_data),
163 .ops = &sandbox_i2c_pmic_emul_ops,
164};