Skip to content
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

Merged
merged 1 commit into from
Oct 20, 2023

Conversation

panikiel
Copy link
Contributor

Add a driver for the SB Temperature Sensor Interface. This is an I2C temperature sensor on AMD SoCs.

Copy link
Contributor

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.

Copy link
Contributor Author

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)

Copy link
Collaborator

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.c Outdated Show resolved Hide resolved
Comment on lines 13 to 25
#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;
};
Copy link
Member

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

Copy link
Contributor Author

@panikiel panikiel Aug 1, 2023

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 116 to 121
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;
Copy link
Collaborator

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.

Copy link
Contributor Author

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;
Copy link
Collaborator

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)

Copy link
Contributor Author

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

@str4t0m str4t0m changed the title Add driver for SB-TSI drivers: sensor: Add driver for SB-TSI Aug 1, 2023
Comment on lines 30 to 43
res = pm_device_runtime_get(dev);
if (res) {
return res;
}
Copy link
Contributor

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?

Copy link
Contributor Author

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 {
};
Copy link
Contributor

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.

Copy link
Contributor Author

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; \
Copy link
Contributor

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.

Copy link
Contributor Author

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, \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&sb_tsi_emul_cfg_##n, &sb_tsi_emul_api_i2c, \
NULL, &sb_tsi_emul_api_i2c, \

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 13 to 25
#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;
};
Copy link
Contributor

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.

MaureenHelm
MaureenHelm previously approved these changes Aug 16, 2023
keith-zephyr
keith-zephyr previously approved these changes Sep 6, 2023
return -EIO;
}

if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_DIE_TEMP) {
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
struct sb_tsi_data *data = dev->data;

if (chan != SENSOR_CHAN_DIE_TEMP) {
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@panikiel panikiel force-pushed the sb-tsi branch 2 times, most recently from a0b71dc to c929207 Compare September 13, 2023 08:40
@panikiel
Copy link
Contributor Author

@yperess, could you please take another look?

keith-zephyr
keith-zephyr previously approved these changes Sep 21, 2023
MaureenHelm
MaureenHelm previously approved these changes Sep 21, 2023
@tristan-google tristan-google self-requested a review September 21, 2023 18:08
tristan-google
tristan-google previously approved these changes Sep 21, 2023
teburd
teburd previously approved these changes Sep 22, 2023
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]>
@fabiobaltieri fabiobaltieri added this to the v3.6.0 milestone Oct 3, 2023
@carlescufi carlescufi merged commit 2f7cb40 into zephyrproject-rtos:main Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Binding PR modifies or adds a Device Tree binding area: Sensors Sensors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants