summaryrefslogtreecommitdiff
path: root/zephyr/emul/emul_isl923x.c
blob: e7a286e6fb99e5fd209d5b6f5b9b9c4e60a85830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#define DT_DRV_COMPAT cros_isl923x_emul

#include <device.h>
#include <drivers/i2c.h>
#include <drivers/i2c_emul.h>
#include <emul.h>
#include <errno.h>
#include <sys/__assert.h>

#include "driver/charger/isl923x.h"
#include "driver/charger/isl923x_public.h"
#include "emul/emul_common_i2c.h"
#include "emul/emul_isl923x.h"
#include "i2c.h"

#include <logging/log.h>
LOG_MODULE_REGISTER(isl923x_emul, CONFIG_ISL923X_EMUL_LOG_LEVEL);

#define ISL923X_DATA_FROM_I2C_EMUL(_emul)                                    \
	CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \
		     struct isl923x_emul_data, common)

/** Mask used for the charge current register */
#define REG_CHG_CURRENT_MASK GENMASK(12, 2)

/** Mask used for the system voltage max register */
#define REG_SYS_VOLTAGE_MAX_MASK GENMASK(14, 3)

/** Mask used for the adapter current limit 1 register */
#define REG_ADAPTER_CURRENT_LIMIT1_MASK GENMASK(12, 2)

/** Mask used for the adapter current limit 2 register */
#define REG_ADAPTER_CURRENT_LIMIT2_MASK GENMASK(12, 2)

/** Mask used for the control 0 register */
#define REG_CONTROL0_MASK GENMASK(15, 1)

/** Mask used for the control 1 register */
#define REG_CONTROL1_MASK (GENMASK(15, 8) | GENMASK(6, 0))

/** Mask used for the control 2 register */
#define REG_CONTROL2_MASK GENMASK(15, 0)

/** Mask used for the control 3 register */
#define REG_CONTROL3_MASK GENMASK(15, 0)

/** Mask used for the AC PROCHOT register */
#define REG_PROCHOT_AC_MASK GENMASK(12, 7)

/** Mask used for the DC PROCHOT register */
#define REG_PROCHOT_DC_MASK GENMASK(13, 8)

struct isl923x_emul_data {
	/** Common I2C data */
	struct i2c_common_emul_data common;
	/** Emulated charge current limit register */
	uint16_t current_limit_reg;
	/** Emulated adapter current limit 1 register */
	uint16_t adapter_current_limit1_reg;
	/** Emulated adapter current limit 2 register */
	uint16_t adapter_current_limit2_reg;
	/** Emulated max voltage register */
	uint16_t max_volt_reg;
	/** Emulated manufacturer ID register */
	uint16_t manufacturer_id_reg;
	/** Emulated device ID register */
	uint16_t device_id_reg;
	/** Emulated control 0 register */
	uint16_t control_0_reg;
	/** Emulated control 1 register */
	uint16_t control_1_reg;
	/** Emulated control 2 register */
	uint16_t control_2_reg;
	/** Emulated control 3 register */
	uint16_t control_3_reg;
	/** Emulated AC PROCHOT register */
	uint16_t ac_prochot_reg;
	/** Emulated DC PROCHOT register */
	uint16_t dc_prochot_reg;
	/** Emulated ADC vbus register */
	uint16_t adc_vbus_reg;
};

struct isl923x_emul_cfg {
	/** Common I2C config */
	const struct i2c_common_emul_cfg common;
};

const struct device *isl923x_emul_get_parent(const struct emul *emulator)
{
	struct isl923x_emul_data *data = emulator->data;

	return data->common.i2c;
}

struct i2c_emul *isl923x_emul_get_i2c_emul(const struct emul *emulator)
{
	struct isl923x_emul_data *data = emulator->data;

	return &(data->common.emul);
}

void isl923x_emul_reset(const struct emul *emulator)
{
	struct isl923x_emul_data *data = emulator->data;
	struct i2c_common_emul_data common_backup = data->common;

	memset(data, 0, sizeof(struct isl923x_emul_data));
	data->common = common_backup;
}

void isl923x_emul_set_manufacturer_id(const struct emul *emulator,
				      uint16_t manufacturer_id)
{
	struct isl923x_emul_data *data = emulator->data;

	data->manufacturer_id_reg = manufacturer_id;
}

void isl923x_emul_set_device_id(const struct emul *emulator,
				uint16_t device_id)
{
	struct isl923x_emul_data *data = emulator->data;

	data->device_id_reg = device_id;
}

bool isl923x_emul_is_learn_mode_enabled(const struct emul *emulator)
{
	struct isl923x_emul_data *data = emulator->data;

	return (data->control_1_reg & ISL923X_C1_LEARN_MODE_ENABLE) != 0;
}

void isl923x_emul_set_learn_mode_enabled(const struct emul *emulator,
					 bool enabled)
{
	struct isl923x_emul_data *data = emulator->data;

	if (enabled)
		data->control_1_reg |= ISL923X_C1_LEARN_MODE_ENABLE;
	else
		data->control_1_reg &= ~ISL923X_C1_LEARN_MODE_ENABLE;
}

void isl923x_emul_set_adc_vbus(const struct emul *emulator,
			       uint16_t value)
{
	struct isl923x_emul_data *data = emulator->data;

	/* The VBUS voltage is returned in bits 13:6. The LSB is 96mV. */
	data->adc_vbus_reg = value & GENMASK(13, 6);
}

static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
				  int bytes)
{
	struct isl923x_emul_data *data = ISL923X_DATA_FROM_I2C_EMUL(emul);

	switch (reg) {
	case ISL923X_REG_CHG_CURRENT:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->current_limit_reg & 0xff);
		else
			*val = (uint8_t)((data->current_limit_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_SYS_VOLTAGE_MAX:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->max_volt_reg & 0xff);
		else
			*val = (uint8_t)((data->max_volt_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_ADAPTER_CURRENT_LIMIT1:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->adapter_current_limit1_reg &
					 0xff);
		else
			*val = (uint8_t)((data->adapter_current_limit1_reg >>
					  8) &
					 0xff);
		break;
	case ISL923X_REG_ADAPTER_CURRENT_LIMIT2:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->adapter_current_limit2_reg &
					 0xff);
		else
			*val = (uint8_t)((data->adapter_current_limit2_reg >>
					  8) &
					 0xff);
		break;
	case ISL923X_REG_MANUFACTURER_ID:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->manufacturer_id_reg & 0xff);
		else
			*val = (uint8_t)((data->manufacturer_id_reg >> 8) &
					 0xff);
		break;
	case ISL923X_REG_DEVICE_ID:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->device_id_reg & 0xff);
		else
			*val = (uint8_t)((data->device_id_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_CONTROL0:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->control_0_reg & 0xff);
		else
			*val = (uint8_t)((data->control_0_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_CONTROL1:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->control_1_reg & 0xff);
		else
			*val = (uint8_t)((data->control_1_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_CONTROL2:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->control_2_reg & 0xff);
		else
			*val = (uint8_t)((data->control_2_reg >> 8) & 0xff);
		break;
	case ISL9238_REG_CONTROL3:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->control_3_reg & 0xff);
		else
			*val = (uint8_t)((data->control_3_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_PROCHOT_AC:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->ac_prochot_reg & 0xff);
		else
			*val = (uint8_t)((data->ac_prochot_reg >> 8) & 0xff);
		break;
	case ISL923X_REG_PROCHOT_DC:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->dc_prochot_reg & 0xff);
		else
			*val = (uint8_t)((data->dc_prochot_reg >> 8) & 0xff);
		break;
	case RAA489000_REG_ADC_VBUS:
		__ASSERT_NO_MSG(bytes == 0 || bytes == 1);
		if (bytes == 0)
			*val = (uint8_t)(data->adc_vbus_reg & 0xff);
		else
			*val = (uint8_t)((data->adc_vbus_reg >> 8) & 0xff);
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
				   int bytes)
{
	struct isl923x_emul_data *data = ISL923X_DATA_FROM_I2C_EMUL(emul);

	switch (reg) {
	case ISL923X_REG_CHG_CURRENT:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->current_limit_reg = val & REG_CHG_CURRENT_MASK;
		else
			data->current_limit_reg |= (val << 8) &
						   REG_CHG_CURRENT_MASK;
		break;
	case ISL923X_REG_SYS_VOLTAGE_MAX:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->max_volt_reg = val & REG_SYS_VOLTAGE_MAX_MASK;
		else
			data->max_volt_reg |= (val << 8) &
					      REG_SYS_VOLTAGE_MAX_MASK;
		break;
	case ISL923X_REG_ADAPTER_CURRENT_LIMIT1:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->adapter_current_limit1_reg =
				val & REG_ADAPTER_CURRENT_LIMIT1_MASK;
		else
			data->adapter_current_limit1_reg |=
				(val << 8) & REG_ADAPTER_CURRENT_LIMIT1_MASK;
		break;
	case ISL923X_REG_ADAPTER_CURRENT_LIMIT2:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->adapter_current_limit2_reg =
				val & REG_ADAPTER_CURRENT_LIMIT2_MASK;
		else
			data->adapter_current_limit2_reg |=
				(val << 8) & REG_ADAPTER_CURRENT_LIMIT2_MASK;
		break;
	case ISL923X_REG_CONTROL0:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->control_0_reg = val & REG_CONTROL0_MASK;
		else
			data->control_0_reg |= (val << 8) & REG_CONTROL0_MASK;
		break;
	case ISL923X_REG_CONTROL1:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->control_1_reg = val & REG_CONTROL1_MASK;
		else
			data->control_1_reg |= (val << 8) & REG_CONTROL1_MASK;
		break;
	case ISL923X_REG_CONTROL2:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->control_2_reg = val & REG_CONTROL2_MASK;
		else
			data->control_2_reg |= (val << 8) & REG_CONTROL2_MASK;
		break;
	case ISL9238_REG_CONTROL3:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->control_3_reg = val & REG_CONTROL3_MASK;
		else
			data->control_3_reg |= (val << 8) & REG_CONTROL3_MASK;
		break;
	case ISL923X_REG_PROCHOT_AC:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->ac_prochot_reg = val & REG_PROCHOT_AC_MASK;
		else
			data->ac_prochot_reg |= (val << 8) &
						REG_PROCHOT_AC_MASK;
		break;
	case ISL923X_REG_PROCHOT_DC:
		__ASSERT_NO_MSG(bytes == 1 || bytes == 2);
		if (bytes == 1)
			data->dc_prochot_reg = val & REG_PROCHOT_DC_MASK;
		else
			data->dc_prochot_reg |= (val << 8) &
						REG_PROCHOT_DC_MASK;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

static int emul_isl923x_init(const struct emul *emul,
			     const struct device *parent)
{
	const struct isl923x_emul_cfg *cfg = emul->cfg;
	struct isl923x_emul_data *data = emul->data;

	data->common.emul.api = &i2c_common_emul_api;
	data->common.emul.addr = cfg->common.addr;
	data->common.emul.parent = emul;
	data->common.i2c = parent;
	data->common.cfg = &cfg->common;
	i2c_common_emul_init(&data->common);

	return i2c_emul_register(parent, emul->dev_label, &data->common.emul);
}

#define INIT_ISL923X(n)                                                        \
	static struct isl923x_emul_data isl923x_emul_data_##n = {              \
		.common = {                                                    \
			.write_byte = isl923x_emul_write_byte,                 \
			.read_byte = isl923x_emul_read_byte,                   \
		},                                                             \
	};                                                                     \
	static struct isl923x_emul_cfg isl923x_emul_cfg_##n = {                \
	.common = {                                                            \
		.i2c_label = DT_INST_BUS_LABEL(n),                             \
		.dev_label = DT_INST_LABEL(n),                                 \
		.addr = DT_INST_REG_ADDR(n),                                   \
		},                                                             \
	};                                                                     \
	EMUL_DEFINE(emul_isl923x_init, DT_DRV_INST(n), &isl923x_emul_cfg_##n,  \
		    &isl923x_emul_data_##n)

DT_INST_FOREACH_STATUS_OKAY(INIT_ISL923X)