1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2017-2021 Joel Stanley <joel@jms.id.au>, IBM Corporation
5 * Infineon DPS310 temperature and humidity sensor
7 * https://www.infineon.com/cms/en/product/sensor/pressure-sensors/pressure-sensors-for-iot/dps310/
10 #include "qemu/osdep.h"
13 #include "hw/i2c/i2c.h"
14 #include "qapi/error.h"
15 #include "qapi/visitor.h"
16 #include "migration/vmstate.h"
18 #define NUM_REGISTERS 0x33
20 typedef struct DPS310State
{
25 uint8_t regs
[NUM_REGISTERS
];
32 #define TYPE_DPS310 "dps310"
33 #define DPS310(obj) OBJECT_CHECK(DPS310State, (obj), TYPE_DPS310)
35 #define DPS310_PRS_B2 0x00
36 #define DPS310_PRS_B1 0x01
37 #define DPS310_PRS_B0 0x02
38 #define DPS310_TMP_B2 0x03
39 #define DPS310_TMP_B1 0x04
40 #define DPS310_TMP_B0 0x05
41 #define DPS310_PRS_CFG 0x06
42 #define DPS310_TMP_CFG 0x07
43 #define DPS310_TMP_RATE_BITS (0x70)
44 #define DPS310_MEAS_CFG 0x08
45 #define DPS310_MEAS_CTRL_BITS (0x07)
46 #define DPS310_PRESSURE_EN BIT(0)
47 #define DPS310_TEMP_EN BIT(1)
48 #define DPS310_BACKGROUND BIT(2)
49 #define DPS310_PRS_RDY BIT(4)
50 #define DPS310_TMP_RDY BIT(5)
51 #define DPS310_SENSOR_RDY BIT(6)
52 #define DPS310_COEF_RDY BIT(7)
53 #define DPS310_CFG_REG 0x09
54 #define DPS310_RESET 0x0c
55 #define DPS310_RESET_MAGIC (BIT(0) | BIT(3))
56 #define DPS310_COEF_BASE 0x10
57 #define DPS310_COEF_LAST 0x21
58 #define DPS310_COEF_SRC 0x28
60 static void dps310_reset(DeviceState
*dev
)
62 DPS310State
*s
= DPS310(dev
);
64 static const uint8_t regs_reset_state
[sizeof(s
->regs
)] = {
65 0xfe, 0x2f, 0xee, 0x02, 0x69, 0xa6, 0x00, 0x80, 0xc7, 0x00, 0x00, 0x00,
66 0x00, 0x10, 0x00, 0x00, 0x0e, 0x1e, 0xdd, 0x13, 0xca, 0x5f, 0x21, 0x52,
67 0xf9, 0xc6, 0x04, 0xd1, 0xdb, 0x47, 0x00, 0x5b, 0xfb, 0x3a, 0x00, 0x00,
68 0x20, 0x49, 0x4e, 0xa5, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 memcpy(s
->regs
, regs_reset_state
, sizeof(s
->regs
));
75 /* TODO: assert these after some timeout ? */
76 s
->regs
[DPS310_MEAS_CFG
] = DPS310_COEF_RDY
| DPS310_SENSOR_RDY
77 | DPS310_TMP_RDY
| DPS310_PRS_RDY
;
80 static uint8_t dps310_read(DPS310State
*s
, uint8_t reg
)
82 if (reg
>= sizeof(s
->regs
)) {
83 qemu_log_mask(LOG_GUEST_ERROR
, "%s: register 0x%02x out of bounds\n",
84 __func__
, s
->pointer
);
99 case DPS310_COEF_BASE
...DPS310_COEF_LAST
:
100 case DPS310_COEF_SRC
:
101 case 0x32: /* Undocumented register to indicate workaround not required */
104 qemu_log_mask(LOG_UNIMP
, "%s: register 0x%02x unimplemented\n",
110 static void dps310_write(DPS310State
*s
, uint8_t reg
, uint8_t data
)
112 if (reg
>= sizeof(s
->regs
)) {
113 qemu_log_mask(LOG_GUEST_ERROR
, "%s: register %d out of bounds\n",
114 __func__
, s
->pointer
);
120 if (data
== DPS310_RESET_MAGIC
) {
121 device_cold_reset(DEVICE(s
));
126 case DPS310_MEAS_CFG
:
131 qemu_log_mask(LOG_UNIMP
, "%s: register 0x%02x unimplemented\n",
137 static uint8_t dps310_rx(I2CSlave
*i2c
)
139 DPS310State
*s
= DPS310(i2c
);
142 return dps310_read(s
, s
->pointer
++);
148 static int dps310_tx(I2CSlave
*i2c
, uint8_t data
)
150 DPS310State
*s
= DPS310(i2c
);
154 * first byte is the register pointer for a read or write
159 } else if (s
->len
== 1) {
160 dps310_write(s
, s
->pointer
++, data
);
166 static int dps310_event(I2CSlave
*i2c
, enum i2c_event event
)
168 DPS310State
*s
= DPS310(i2c
);
177 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid recv sequence\n",
188 static const VMStateDescription vmstate_dps310
= {
191 .minimum_version_id
= 0,
192 .fields
= (VMStateField
[]) {
193 VMSTATE_UINT8(len
, DPS310State
),
194 VMSTATE_UINT8_ARRAY(regs
, DPS310State
, NUM_REGISTERS
),
195 VMSTATE_UINT8(pointer
, DPS310State
),
196 VMSTATE_I2C_SLAVE(i2c
, DPS310State
),
197 VMSTATE_END_OF_LIST()
201 static void dps310_class_init(ObjectClass
*klass
, void *data
)
203 DeviceClass
*dc
= DEVICE_CLASS(klass
);
204 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
206 k
->event
= dps310_event
;
209 dc
->reset
= dps310_reset
;
210 dc
->vmsd
= &vmstate_dps310
;
213 static const TypeInfo dps310_info
= {
215 .parent
= TYPE_I2C_SLAVE
,
216 .instance_size
= sizeof(DPS310State
),
217 .class_init
= dps310_class_init
,
220 static void dps310_register_types(void)
222 type_register_static(&dps310_info
);
225 type_init(dps310_register_types
)