-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
drivers: sensor: Add driver for SB-TSI #60818
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments on #60833. Please convert this emulator to use the common backend support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. tests/drivers/build_all/sensor/sensors.generic_test
indeed runs my emulator (after manually setting build_only
to false)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after manually setting build_only to false)
Yeah, the generic test is currently disabled upstream due to an unrelated driver acting up in CI. I'm working on addressing this.
drivers/sensor/amd_sb_tsi/sb_tsi.h
Outdated
#define SB_TSI_TEMP_INT 0x01 | ||
#define SB_TSI_TEMP_DEC 0x10 | ||
#define SB_TSI_TEMP_DEC_SHIFT 5 | ||
#define SB_TSI_TEMP_DEC_SCALE 8 | ||
|
||
struct sb_tsi_data { | ||
uint8_t sample_int; | ||
uint8_t sample_dec; | ||
}; | ||
|
||
struct sb_tsi_config { | ||
struct i2c_dt_spec i2c; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's not enough here to justify a separate private header file; please move these definitions to the .c file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The defines are also used by the emulator, this header is included by sb_tsi.c
and sb_tsi_emul.c
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct sb_tsi_data
and struct sb_tsi_config
are only used by the C driver. Please move into the C source.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
scaled_value = (int64_t)value << shift; | ||
temp_eighths = scaled_value >> 28; | ||
temp_eighths = CLAMP(temp_eighths, 0, 0x7ff); | ||
|
||
data->reg[SB_TSI_TEMP_INT] = temp_eighths >> 3; | ||
data->reg[SB_TSI_TEMP_DEC] = (temp_eighths & 0x7) << 5; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Continuing on from the other comment, what you want to do here is convert the fixed-point Celsius value passed in by the test to its corresponding register representation. In this case I'd unshift the supplied value and apply the appropriate scaling factor to convert to integer millicelsius. Then divide by 1/0.125 = 8
and clamp to get the register value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this better?
return -ENOTSUP; | ||
} | ||
|
||
*lower = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like you're using these fields to pass around a raw register representation of the temperature rather than degrees Celsius in fixed-point format, which is the encoding the new sensors API as well as the backend emul API uses in place of the traditional struct sensor_val
.
Based on what I could find this sensor runs a range of 0.125 C to 255.875 C. This will fit within the range of shift value 8 (Q9.23 in DSP notation), which is -256
to 255.9999998807907
. The sensor's lower bound (0.125 C), for example, would be encoded as 0.125 * 2^31 >> 8 = 0x100000
(but I would just write the full expression instead of pasting the hex value for readability's sake: *lower = (int64_t)(0.125 * ((int64_t)INT32_MAX + 1)) >> *shift;
). In this case, epsilon would be the same value because we go up in increments of 0.125 C. Epsilon is used as a tolerance when the generic test compares actual to expected to allow for rounding / precision errors. The value of *upper
would be 0x7ff00000
(same math as above)
Let me know if you have any other questions, this can be hard to wrap one's head around at first. I found https://chummersone.github.io/qformat.html very useful. (For this case you'd plug in 32 as word size and 23 as the number of fractional bits)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for the explanation. I applied the changes, except for the lower bound being 0C, not 0.125C
drivers/sensor/amd_sb_tsi/sb_tsi.c
Outdated
res = pm_device_runtime_get(dev); | ||
if (res) { | ||
return res; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see other sensors using power management this way. There are few examples of sensors using the power management action to put the sensor into a low power mode. But what you've implemented here would prevent a power domain from going into suspend.
bmi323 example
@teburd - you mentioned defining a PM strategy for sensors here. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to be like how it's done in bmi160 (as linked above).
}; | ||
|
||
struct sb_tsi_emul_cfg { | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drivers and emulators don't have to provide const config
data. You can just pass in NULL for the corresponding parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
}; | ||
|
||
#define SB_TSI_EMUL(n) \ | ||
const struct sb_tsi_emul_cfg sb_tsi_emul_cfg_##n; \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above. const config
info is not required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
const struct sb_tsi_emul_cfg sb_tsi_emul_cfg_##n; \ | ||
struct sb_tsi_emul_data sb_tsi_emul_data_##n; \ | ||
EMUL_DT_INST_DEFINE(n, sb_tsi_emul_init, &sb_tsi_emul_data_##n, \ | ||
&sb_tsi_emul_cfg_##n, &sb_tsi_emul_api_i2c, \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&sb_tsi_emul_cfg_##n, &sb_tsi_emul_api_i2c, \ | |
NULL, &sb_tsi_emul_api_i2c, \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
drivers/sensor/amd_sb_tsi/sb_tsi.h
Outdated
#define SB_TSI_TEMP_INT 0x01 | ||
#define SB_TSI_TEMP_DEC 0x10 | ||
#define SB_TSI_TEMP_DEC_SHIFT 5 | ||
#define SB_TSI_TEMP_DEC_SCALE 8 | ||
|
||
struct sb_tsi_data { | ||
uint8_t sample_int; | ||
uint8_t sample_dec; | ||
}; | ||
|
||
struct sb_tsi_config { | ||
struct i2c_dt_spec i2c; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct sb_tsi_data
and struct sb_tsi_config
are only used by the C driver. Please move into the C source.
1750fd4
drivers/sensor/amd_sb_tsi/sb_tsi.c
Outdated
return -EIO; | ||
} | ||
|
||
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_DIE_TEMP) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the wrong channel, die temperature is usually the internal temperature of the sensor used for calibration. I think you want SENSOR_CHAN_AMBIENT_TEMP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
drivers/sensor/amd_sb_tsi/sb_tsi.c
Outdated
{ | ||
struct sb_tsi_data *data = dev->data; | ||
|
||
if (chan != SENSOR_CHAN_DIE_TEMP) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, use SENSOR_CHAN_AMBIENT_TEMP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
a0b71dc
to
c929207
Compare
@yperess, could you please take another look? |
Add a driver for the SB Temperature Sensor Interface. This is an I2C temperature sensor on AMD SoCs. Signed-off-by: Paweł Anikiel <[email protected]>
bcac61f
Add a driver for the SB Temperature Sensor Interface. This is an I2C temperature sensor on AMD SoCs.