summaryrefslogtreecommitdiff
path: root/zephyr/include/emul/emul_syv682x.h
blob: 3e4328323ee4af15e5b37204ac6aa7be84a92ae0 (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
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/** @file
 *
 * @brief Backend API for SYV682X emulator
 */

#ifndef __EMUL_SYV682X_H
#define __EMUL_SYV682X_H

#include <stdint.h>

#include <zephyr/drivers/i2c_emul.h>

/* Register info copied from syv682.h */

/* SYV682x register addresses */
#define SYV682X_STATUS_REG 0x00
#define SYV682X_CONTROL_1_REG 0x01
#define SYV682X_CONTROL_2_REG 0x02
#define SYV682X_CONTROL_3_REG 0x03
#define SYV682X_CONTROL_4_REG 0x04

/* Status Register */
#define SYV682X_STATUS_OC_HV BIT(7)
#define SYV682X_STATUS_RVS BIT(6)
#define SYV682X_STATUS_OC_5V BIT(5)
#define SYV682X_STATUS_OVP BIT(4)
#define SYV682X_STATUS_FRS BIT(3)
#define SYV682X_STATUS_TSD BIT(2)
#define SYV682X_STATUS_VSAFE_5V BIT(1)
#define SYV682X_STATUS_VSAFE_0V BIT(0)
#define SYV682X_STATUS_INT_MASK 0xfc
#define SYV682X_STATUS_NONE 0

/* Control Register 1 */
#define SYV682X_CONTROL_1_CH_SEL BIT(1)
#define SYV682X_CONTROL_1_HV_DR BIT(2)
#define SYV682X_CONTROL_1_PWR_ENB BIT(7)

#define SYV682X_5V_ILIM_MASK 0x18
#define SYV682X_5V_ILIM_BIT_SHIFT 3
#define SYV682X_5V_ILIM_1_25 0
#define SYV682X_5V_ILIM_1_75 1
#define SYV682X_5V_ILIM_2_25 2
#define SYV682X_5V_ILIM_3_30 3

#define SYV682X_HV_ILIM_MASK 0x60
#define SYV682X_HV_ILIM_BIT_SHIFT 5
#define SYV682X_HV_ILIM_1_25 0
#define SYV682X_HV_ILIM_1_75 1
#define SYV682X_HV_ILIM_3_30 2
#define SYV682X_HV_ILIM_5_50 3

/* Control Register 2 */
#define SYV682X_OC_DELAY_MASK GENMASK(7, 6)
#define SYV682X_OC_DELAY_SHIFT 6
#define SYV682X_OC_DELAY_1MS 0
#define SYV682X_OC_DELAY_10MS 1
#define SYV682X_OC_DELAY_50MS 2
#define SYV682X_OC_DELAY_100MS 3
#define SYV682X_DSG_TIME_MASK GENMASK(5, 4)
#define SYV682X_DSG_TIME_SHIFT 4
#define SYV682X_DSG_TIME_50MS 0
#define SYV682X_DSG_TIME_100MS 1
#define SYV682X_DSG_TIME_200MS 2
#define SYV682X_DSG_TIME_400MS 3
#define SYV682X_DSG_RON_MASK GENMASK(3, 2)
#define SYV682X_DSG_RON_SHIFT 2
#define SYV682X_DSG_RON_200_OHM 0
#define SYV682X_DSG_RON_400_OHM 1
#define SYV682X_DSG_RON_800_OHM 2
#define SYV682X_DSG_RON_1600_OHM 3
#define SYV682X_CONTROL_2_SDSG BIT(1)
#define SYV682X_CONTROL_2_FDSG BIT(0)

/* Control Register 3 */
#define SYV682X_BUSY BIT(7)
#define SYV682X_RVS_MASK BIT(3)
#define SYV682X_RST_REG BIT(0)
#define SYV682X_OVP_MASK 0x70
#define SYV682X_OVP_BIT_SHIFT 4
#define SYV682X_OVP_06_0 0
#define SYV682X_OVP_08_0 1
#define SYV682X_OVP_11_1 2
#define SYV682X_OVP_12_1 3
#define SYV682X_OVP_14_2 4
#define SYV682X_OVP_17_9 5
#define SYV682X_OVP_21_6 6
#define SYV682X_OVP_23_7 7
#define SYV682X_CONTROL_3_NONE 0

/* Control Register 4 */
#define SYV682X_CONTROL_4_CC1_BPS BIT(7)
#define SYV682X_CONTROL_4_CC2_BPS BIT(6)
#define SYV682X_CONTROL_4_VCONN1 BIT(5)
#define SYV682X_CONTROL_4_VCONN2 BIT(4)
#define SYV682X_CONTROL_4_VBAT_OVP BIT(3)
#define SYV682X_CONTROL_4_VCONN_OCP BIT(2)
#define SYV682X_CONTROL_4_CC_FRS BIT(1)
#define SYV682X_CONTROL_4_INT_MASK 0x0c
#define SYV682X_CONTROL_4_NONE 0

/**
 * @brief Set the underlying interrupt conditions affecting the SYV682x
 *
 * @param emul      SYV682x emulator
 * @param status    A status register value corresponding to the underlying
 *                  conditions
 * @param control_4 A control 4 register value corresponding to the underlying
 *                  conditions; only the bits in SYV682X_CONTROL_4_INT_MASK have
 *                  an effect.
 */
void syv682x_emul_set_condition(const struct emul *emul, uint8_t status,
				uint8_t control_4);

/**
 * @brief Cause CONTROL_3[BUSY] to be set for a number of reads. This bit
 *        signals that I2C writes will be ignored. Each call overrides any
 *        previous setting.
 *
 * @param emul  SYV682x emulator
 * @param reads The number of reads of CONTROL_3 to keep BUSY set for
 */
void syv682x_emul_set_busy_reads(const struct emul *emul, int reads);

/**
 * @brief Set value of a register of SYV682x
 *
 * @param emul SYV682x emulator
 * @param reg  Register address
 * @param val  Value to write to the register
 *
 * @return 0 on success, error code on error
 */
int syv682x_emul_set_reg(const struct emul *emul, int reg, uint8_t val);

/**
 * @brief Get value of a register of SYV682x
 *
 * @param emul SYV682x emulator
 * @param reg  Register address
 * @param val  Pointer at which to store current value of register
 *
 * @return 0 on success, error code on error
 */
int syv682x_emul_get_reg(const struct emul *emul, int reg, uint8_t *val);

/**
 * @brief Returns pointer to i2c_common_emul_data for given emul
 *
 * @param emul Pointer to SYV682X emulator
 * @return common_data Pointer to i2c_common_emul_data
 */
struct i2c_common_emul_data *
emul_syv682x_get_i2c_common_data(const struct emul *emul);

#endif /* __EMUL_SYV682X_H */