diff --git a/drivers/sensor/nxp/p3t1755/p3t1755.c b/drivers/sensor/nxp/p3t1755/p3t1755.c index 361b25b7ca64..68e47116fdb1 100644 --- a/drivers/sensor/nxp/p3t1755/p3t1755.c +++ b/drivers/sensor/nxp/p3t1755/p3t1755.c @@ -11,6 +11,8 @@ #include #include #include +#include +#include LOG_MODULE_REGISTER(P3T1755, CONFIG_SENSOR_LOG_LEVEL); @@ -29,22 +31,52 @@ static int p3t1755_i3c_write_reg(const struct device *dev, uint8_t reg, uint8_t return i3c_burst_write(data->i3c_dev, reg, byte, len); } + +static int p3t1755_i3c_get(const struct device *dev) +{ + const struct p3t1755_config *config = dev->config; + + return pm_device_runtime_get(config->i3c.bus); +} + +static int p3t1755_i3c_put(const struct device *dev) +{ + const struct p3t1755_config *config = dev->config; + + return pm_device_runtime_put(config->i3c.bus); +} #endif #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) -int p3t1755_i2c_read_reg(const struct device *dev, uint8_t reg, uint8_t *value, uint8_t len) +__maybe_unused static int p3t1755_i2c_read_reg(const struct device *dev, uint8_t reg, + uint8_t *value, uint8_t len) { const struct p3t1755_config *config = dev->config; return i2c_burst_read_dt(&config->bus_cfg.i2c, reg, value, len); } -int p3t1755_i2c_write_reg(const struct device *dev, uint8_t reg, uint8_t *byte, uint8_t len) +__maybe_unused static int p3t1755_i2c_write_reg(const struct device *dev, uint8_t reg, + uint8_t *byte, uint8_t len) { const struct p3t1755_config *config = dev->config; return i2c_burst_write_dt(&config->bus_cfg.i2c, reg, byte, len); } + +__maybe_unused static int p3t1755_i2c_get(const struct device *dev) +{ + const struct p3t1755_config *config = dev->config; + + return pm_device_runtime_get(config->bus_cfg.i2c.bus); +} + +__maybe_unused static int p3t1755_i2c_put(const struct device *dev) +{ + const struct p3t1755_config *config = dev->config; + + return pm_device_runtime_put(config->bus_cfg.i2c.bus); +} #endif static int p3t1755_sample_fetch(const struct device *dev, enum sensor_channel chan) @@ -58,6 +90,8 @@ static int p3t1755_sample_fetch(const struct device *dev, enum sensor_channel ch return -ENOTSUP; } + config->ops.get(dev); + if (config->oneshot_mode) { data->config_reg |= P3T1755_CONFIG_REG_OS; config->ops.write(dev, P3T1755_CONFIG_REG, &data->config_reg, 1); @@ -67,6 +101,8 @@ static int p3t1755_sample_fetch(const struct device *dev, enum sensor_channel ch int status = config->ops.read(dev, P3T1755_TEMPERATURE_REG, raw_temp, 2); + config->ops.put(dev); + if (status) { LOG_ERR("read return error %d", status); return -ENOTSUP; @@ -115,17 +151,23 @@ static int p3t1755_channel_get(const struct device *dev, enum sensor_channel cha return 0; } -static int p3t1755_init(const struct device *dev) +static int p3t1755_pm_resume(const struct device *dev) { const struct p3t1755_config *config = dev->config; struct p3t1755_data *data = dev->data; + int ret = -ENODEV; + + if (config->ops.get(dev)) { + LOG_ERR("Bus device get failed"); + goto put_and_ret; + } #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c) if (config->i3c.bus != NULL) { data->i3c_dev = i3c_device_find(config->i3c.bus, &config->i3c.dev_id); if (data->i3c_dev == NULL) { LOG_ERR("Cannot find I3C device descriptor"); - return -ENODEV; + goto put_and_ret; } } #endif @@ -133,7 +175,7 @@ static int p3t1755_init(const struct device *dev) if (config->inst_on_bus == P3T1755_BUS_I2C) { if (!i2c_is_ready_dt(&config->bus_cfg.i2c)) { LOG_ERR("I2C bus device not ready"); - return -ENODEV; + goto put_and_ret; } } #endif @@ -147,9 +189,35 @@ static int p3t1755_init(const struct device *dev) config->ops.write(dev, P3T1755_CONFIG_REG, &data->config_reg, 1); } - LOG_DBG("Init complete"); + ret = 0; - return 0; +put_and_ret: + (void)config->ops.put(dev); + + return ret; +} + +static int p3t1755_pm_hook(const struct device *dev, enum pm_device_action action) +{ + int ret; + + switch (action) { + case PM_DEVICE_ACTION_SUSPEND: + ret = 0; + break; + case PM_DEVICE_ACTION_RESUME: + ret = p3t1755_pm_resume(dev); + break; + default: + ret = -ENOTSUP; + } + + return ret; +} + +static int p3t1755_init(const struct device *dev) +{ + return pm_device_driver_init(dev, p3t1755_pm_hook); } static DEVICE_API(sensor, p3t1755_driver_api) = { @@ -165,6 +233,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = { .ops = { \ .read = p3t1755_i2c_read_reg, \ .write = p3t1755_i2c_write_reg, \ + .get = p3t1755_i2c_get, \ + .put = p3t1755_i2c_put, \ }, \ .inst_on_bus = P3T1755_BUS_I2C, @@ -176,6 +246,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = { .ops = { \ .read = p3t1755_i3c_read_reg, \ .write = p3t1755_i3c_write_reg, \ + .get = p3t1755_i3c_get, \ + .put = p3t1755_i3c_put, \ }, \ .inst_on_bus = P3T1755_BUS_I3C, \ .i3c.bus = DEVICE_DT_GET(DT_INST_BUS(inst)), .i3c.dev_id = I3C_DEVICE_ID_DT_INST(inst), @@ -189,7 +261,8 @@ static DEVICE_API(sensor, p3t1755_driver_api) = { .oneshot_mode = DT_INST_PROP(n, oneshot_mode), \ }; \ \ - SENSOR_DEVICE_DT_INST_DEFINE(n, p3t1755_init, NULL, &p3t1755_data_##n, \ + PM_DEVICE_DT_INST_DEFINE(n, p3t1755_pm_hook); \ + SENSOR_DEVICE_DT_INST_DEFINE(n, p3t1755_init, PM_DEVICE_DT_INST_GET(n), &p3t1755_data_##n, \ &p3t1755_config_##n, POST_KERNEL, \ CONFIG_SENSOR_INIT_PRIORITY, &p3t1755_driver_api); diff --git a/drivers/sensor/nxp/p3t1755/p3t1755.h b/drivers/sensor/nxp/p3t1755/p3t1755.h index b2ba329f2fe2..cd6b07bf9a4f 100644 --- a/drivers/sensor/nxp/p3t1755/p3t1755.h +++ b/drivers/sensor/nxp/p3t1755/p3t1755.h @@ -36,6 +36,8 @@ struct p3t1755_io_ops { int (*read)(const struct device *dev, uint8_t reg, uint8_t *byte, uint8_t len); int (*write)(const struct device *dev, uint8_t reg, uint8_t *byte, uint8_t len); + int (*get)(const struct device *dev); + int (*put)(const struct device *dev); }; union p3t1755_bus_cfg {