blob: 6e81b9c34279d9b2a70c3bd287973b97000bbbf5 [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
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +02007#include <errno.h>
8#include <dm.h>
9#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060012#include <linux/printk.h>
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020013#include <power/pmic.h>
14#include <power/sandbox_pmic.h>
15
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020016/**
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 */
22struct sandbox_i2c_pmic_plat_data {
Lukasz Majewskiace75042018-05-15 16:26:39 +020023 u8 rw_reg, rw_idx;
24 u8 reg_count;
25 u8 trans_len;
26 u8 buf_size;
27 u8 *reg;
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020028};
29
30static int sandbox_i2c_pmic_read_data(struct udevice *emul, uchar chip,
31 uchar *buffer, int len)
32{
Simon Glassfa20e932020-12-03 16:55:20 -070033 struct sandbox_i2c_pmic_plat_data *plat = dev_get_plat(emul);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020034
Lukasz Majewskiace75042018-05-15 16:26:39 +020035 if (plat->rw_idx + len > plat->buf_size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090036 pr_err("Request exceeds PMIC register range! Max register: %#x",
Lukasz Majewskiace75042018-05-15 16:26:39 +020037 plat->reg_count);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020038 return -EFAULT;
39 }
40
Lukasz Majewskiace75042018-05-15 16:26:39 +020041 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 Marczakd5175dc2015-05-13 13:38:32 +020043
Lukasz Majewskiace75042018-05-15 16:26:39 +020044 memcpy(buffer, plat->reg + plat->rw_idx, len);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020045
46 return 0;
47}
48
49static int sandbox_i2c_pmic_write_data(struct udevice *emul, uchar chip,
50 uchar *buffer, int len,
51 bool next_is_read)
52{
Simon Glassfa20e932020-12-03 16:55:20 -070053 struct sandbox_i2c_pmic_plat_data *plat = dev_get_plat(emul);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020054
55 /* Probe only */
56 if (!len)
57 return 0;
58
59 /* Set PMIC register for I/O */
60 plat->rw_reg = *buffer;
Lukasz Majewskiace75042018-05-15 16:26:39 +020061 plat->rw_idx = plat->rw_reg * plat->trans_len;
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020062
Lukasz Majewskiace75042018-05-15 16:26:39 +020063 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 Marczakd5175dc2015-05-13 13:38:32 +020065
66 /* For read operation, set (write) only chip reg */
67 if (next_is_read)
68 return 0;
69
70 buffer++;
71 len--;
72
Lukasz Majewskiace75042018-05-15 16:26:39 +020073 if (plat->rw_idx + len > plat->buf_size) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090074 pr_err("Request exceeds PMIC register range! Max register: %#x",
Lukasz Majewskiace75042018-05-15 16:26:39 +020075 plat->reg_count);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020076 }
77
Lukasz Majewskiace75042018-05-15 16:26:39 +020078 memcpy(plat->reg + plat->rw_idx, buffer, len);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +020079
80 return 0;
81}
82
83static 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 Glassaad29ae2020-12-03 16:55:21 -0700106static int sandbox_i2c_pmic_of_to_plat(struct udevice *emul)
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200107{
Simon Glassfa20e932020-12-03 16:55:20 -0700108 struct sandbox_i2c_pmic_plat_data *plat = dev_get_plat(emul);
Simon Glass17b56f62018-11-18 08:14:34 -0700109 struct udevice *pmic_dev = i2c_emul_get_device(emul);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200110
111 debug("%s:%d Setting PMIC default registers\n", __func__, __LINE__);
Lukasz Majewskiace75042018-05-15 16:26:39 +0200112 plat->reg_count = pmic_reg_count(pmic_dev);
Simon Glass9779eb92020-01-27 08:49:53 -0700113
114 return 0;
115}
116
117static int sandbox_i2c_pmic_probe(struct udevice *emul)
118{
Simon Glassfa20e932020-12-03 16:55:20 -0700119 struct sandbox_i2c_pmic_plat_data *plat = dev_get_plat(emul);
Simon Glass9779eb92020-01-27 08:49:53 -0700120 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 Majewskiace75042018-05-15 16:26:39 +0200125 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 Marczakd5175dc2015-05-13 13:38:32 +0200133
Simon Glass5cdb5162017-05-18 20:09:33 -0600134 reg_defaults = dev_read_u8_array_ptr(emul, "reg-defaults",
Lukasz Majewskiace75042018-05-15 16:26:39 +0200135 plat->buf_size);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200136
137 if (!reg_defaults) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900138 pr_err("Property \"reg-defaults\" not found for device: %s!",
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200139 emul->name);
Lukasz Majewskiace75042018-05-15 16:26:39 +0200140 free(plat->reg);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200141 return -EINVAL;
142 }
143
Lukasz Majewskiace75042018-05-15 16:26:39 +0200144 memcpy(plat->reg, reg_defaults, plat->buf_size);
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200145
146 return 0;
147}
148
149struct dm_i2c_ops sandbox_i2c_pmic_emul_ops = {
150 .xfer = sandbox_i2c_pmic_xfer,
151};
152
153static const struct udevice_id sandbox_i2c_pmic_ids[] = {
154 { .compatible = "sandbox,i2c-pmic" },
155 { }
156};
157
158U_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 Glassaad29ae2020-12-03 16:55:21 -0700162 .of_to_plat = sandbox_i2c_pmic_of_to_plat,
Simon Glass9779eb92020-01-27 08:49:53 -0700163 .probe = sandbox_i2c_pmic_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700164 .plat_auto = sizeof(struct sandbox_i2c_pmic_plat_data),
Przemyslaw Marczakd5175dc2015-05-13 13:38:32 +0200165 .ops = &sandbox_i2c_pmic_emul_ops,
166};