Message ID | 20240114172009.179893-14-jic23@kernel.org |
---|---|
State | New |
Headers | show |
Series | device property / IIO: Use cleanup.h magic for fwnode_handle_put() handling. | expand |
On Sun, 2024-01-14 at 17:20 +0000, Jonathan Cameron wrote: > From: Jonathan Cameron <Jonathan.Cameron@huawei.com> > > This use of the new cleanup.h scope based freeing infrastructure allows > us to exit directly from error conditions and in the good path with > the reference obtained from fwnode_find_reference() (which may be an error > pointer) automatically released. > > Cc: Cosmin Tanislav <cosmin.tanislav@analog.com> > Cc: Nuno Sá <nuno.sa@analog.com> > Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> > --- I guess this one could also benefit from using dev_err_probe(). Anyways: Reviewed-by: Nuno Sa <nuno.sa@analog.com> I might also be able to test this one... > drivers/iio/temperature/ltc2983.c | 70 ++++++++++--------------------- > 1 file changed, 21 insertions(+), 49 deletions(-) > > diff --git a/drivers/iio/temperature/ltc2983.c > b/drivers/iio/temperature/ltc2983.c > index fcb96c44d954..4357364e611e 100644 > --- a/drivers/iio/temperature/ltc2983.c > +++ b/drivers/iio/temperature/ltc2983.c > @@ -656,7 +656,7 @@ ltc2983_thermocouple_new(const struct fwnode_handle > *child, struct ltc2983_data > const struct ltc2983_sensor *sensor) > { > struct ltc2983_thermocouple *thermo; > - struct fwnode_handle *ref; > + struct fwnode_handle *ref __free(fwnode_handle) = NULL; > u32 oc_current; > int ret; > > @@ -714,7 +714,7 @@ ltc2983_thermocouple_new(const struct fwnode_handle > *child, struct ltc2983_data > * the error right away. > */ > dev_err(&st->spi->dev, "Property reg must be > given\n"); > - goto fail; > + return ERR_PTR(ret); > } > } > > @@ -725,22 +725,15 @@ ltc2983_thermocouple_new(const struct fwnode_handle > *child, struct ltc2983_data > thermo->custom = __ltc2983_custom_sensor_new(st, child, > propname, false, > 16384, true); > - if (IS_ERR(thermo->custom)) { > - ret = PTR_ERR(thermo->custom); > - goto fail; > - } > + if (IS_ERR(thermo->custom)) > + return ERR_CAST(thermo->custom); > } > > /* set common parameters */ > thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; > thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; > > - fwnode_handle_put(ref); > return &thermo->sensor; > - > -fail: > - fwnode_handle_put(ref); > - return ERR_PTR(ret); > } > > static struct ltc2983_sensor * > @@ -750,7 +743,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct > ltc2983_data *st, > struct ltc2983_rtd *rtd; > int ret = 0; > struct device *dev = &st->spi->dev; > - struct fwnode_handle *ref; > + struct fwnode_handle *ref __free(fwnode_handle) = NULL; > u32 excitation_current = 0, n_wires = 0; > > rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL); > @@ -766,7 +759,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct > ltc2983_data *st, > ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan); > if (ret) { > dev_err(dev, "Property reg must be given\n"); > - goto fail; > + return ERR_PTR(ret); > } > > ret = fwnode_property_read_u32(child, "adi,number-of-wires", > &n_wires); > @@ -787,8 +780,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct > ltc2983_data *st, > break; > default: > dev_err(dev, "Invalid number of wires:%u\n", > n_wires); > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > } > > @@ -798,8 +790,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct > ltc2983_data *st, > if (n_wires == 2 || n_wires == 3) { > dev_err(dev, > "Rotation not allowed for 2/3 Wire > RTDs"); > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); > } else { > @@ -829,16 +820,14 @@ ltc2983_rtd_new(const struct fwnode_handle *child, > struct ltc2983_data *st, > "Invalid rsense chann:%d to use in kelvin > rsense", > rtd->r_sense_chan); > > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > > if (sensor->chan < min || sensor->chan > max) { > dev_err(dev, "Invalid chann:%d for the rtd config", > sensor->chan); > > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > } else { > /* same as differential case */ > @@ -846,8 +835,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct > ltc2983_data *st, > dev_err(&st->spi->dev, > "Invalid chann:%d for RTD", sensor->chan); > > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > } > > @@ -856,10 +844,8 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct > ltc2983_data *st, > rtd->custom = __ltc2983_custom_sensor_new(st, child, > "adi,custom-rtd", > false, 2048, > false); > - if (IS_ERR(rtd->custom)) { > - ret = PTR_ERR(rtd->custom); > - goto fail; > - } > + if (IS_ERR(rtd->custom)) > + return ERR_CAST(rtd->custom); > } > > /* set common parameters */ > @@ -901,18 +887,13 @@ ltc2983_rtd_new(const struct fwnode_handle *child, > struct ltc2983_data *st, > dev_err(&st->spi->dev, > "Invalid value for excitation current(%u)", > excitation_current); > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > } > > fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); > > - fwnode_handle_put(ref); > return &rtd->sensor; > -fail: > - fwnode_handle_put(ref); > - return ERR_PTR(ret); > } > > static struct ltc2983_sensor * > @@ -921,7 +902,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, > struct ltc2983_data *s > { > struct ltc2983_thermistor *thermistor; > struct device *dev = &st->spi->dev; > - struct fwnode_handle *ref; > + struct fwnode_handle *ref __free(fwnode_handle) = NULL; > u32 excitation_current = 0; > int ret = 0; > > @@ -938,7 +919,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, > struct ltc2983_data *s > ret = fwnode_property_read_u32(ref, "reg", &thermistor- > >r_sense_chan); > if (ret) { > dev_err(dev, "rsense channel must be configured...\n"); > - goto fail; > + return ERR_PTR(ret); > } > > if (fwnode_property_read_bool(child, "adi,single-ended")) { > @@ -958,8 +939,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, > struct ltc2983_data *s > dev_err(&st->spi->dev, > "Invalid chann:%d for differential thermistor", > sensor->chan); > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > > /* check custom sensor */ > @@ -978,10 +958,8 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, > struct ltc2983_data *s > propname, > steinhart, > 64, false); > - if (IS_ERR(thermistor->custom)) { > - ret = PTR_ERR(thermistor->custom); > - goto fail; > - } > + if (IS_ERR(thermistor->custom)) > + return ERR_CAST(thermistor->custom); > } > /* set common parameters */ > thermistor->sensor.fault_handler = ltc2983_common_fault_handler; > @@ -1005,8 +983,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, > struct ltc2983_data *s > LTC2983_SENSOR_THERMISTOR_STEINHART) { > dev_err(&st->spi->dev, > "Auto Range not allowed for custom > sensors\n"); > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > thermistor->excitation_current = 0x0c; > break; > @@ -1047,16 +1024,11 @@ ltc2983_thermistor_new(const struct fwnode_handle > *child, struct ltc2983_data *s > dev_err(&st->spi->dev, > "Invalid value for excitation current(%u)", > excitation_current); > - ret = -EINVAL; > - goto fail; > + return ERR_PTR(-EINVAL); > } > } > > - fwnode_handle_put(ref); > return &thermistor->sensor; > -fail: > - fwnode_handle_put(ref); > - return ERR_PTR(ret); > } > > static struct ltc2983_sensor *
diff --git a/drivers/iio/temperature/ltc2983.c b/drivers/iio/temperature/ltc2983.c index fcb96c44d954..4357364e611e 100644 --- a/drivers/iio/temperature/ltc2983.c +++ b/drivers/iio/temperature/ltc2983.c @@ -656,7 +656,7 @@ ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data const struct ltc2983_sensor *sensor) { struct ltc2983_thermocouple *thermo; - struct fwnode_handle *ref; + struct fwnode_handle *ref __free(fwnode_handle) = NULL; u32 oc_current; int ret; @@ -714,7 +714,7 @@ ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data * the error right away. */ dev_err(&st->spi->dev, "Property reg must be given\n"); - goto fail; + return ERR_PTR(ret); } } @@ -725,22 +725,15 @@ ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data thermo->custom = __ltc2983_custom_sensor_new(st, child, propname, false, 16384, true); - if (IS_ERR(thermo->custom)) { - ret = PTR_ERR(thermo->custom); - goto fail; - } + if (IS_ERR(thermo->custom)) + return ERR_CAST(thermo->custom); } /* set common parameters */ thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; - fwnode_handle_put(ref); return &thermo->sensor; - -fail: - fwnode_handle_put(ref); - return ERR_PTR(ret); } static struct ltc2983_sensor * @@ -750,7 +743,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, struct ltc2983_rtd *rtd; int ret = 0; struct device *dev = &st->spi->dev; - struct fwnode_handle *ref; + struct fwnode_handle *ref __free(fwnode_handle) = NULL; u32 excitation_current = 0, n_wires = 0; rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL); @@ -766,7 +759,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan); if (ret) { dev_err(dev, "Property reg must be given\n"); - goto fail; + return ERR_PTR(ret); } ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires); @@ -787,8 +780,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, break; default: dev_err(dev, "Invalid number of wires:%u\n", n_wires); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } } @@ -798,8 +790,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, if (n_wires == 2 || n_wires == 3) { dev_err(dev, "Rotation not allowed for 2/3 Wire RTDs"); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); } else { @@ -829,16 +820,14 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, "Invalid rsense chann:%d to use in kelvin rsense", rtd->r_sense_chan); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } if (sensor->chan < min || sensor->chan > max) { dev_err(dev, "Invalid chann:%d for the rtd config", sensor->chan); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } } else { /* same as differential case */ @@ -846,8 +835,7 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, dev_err(&st->spi->dev, "Invalid chann:%d for RTD", sensor->chan); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } } @@ -856,10 +844,8 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, rtd->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-rtd", false, 2048, false); - if (IS_ERR(rtd->custom)) { - ret = PTR_ERR(rtd->custom); - goto fail; - } + if (IS_ERR(rtd->custom)) + return ERR_CAST(rtd->custom); } /* set common parameters */ @@ -901,18 +887,13 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st, dev_err(&st->spi->dev, "Invalid value for excitation current(%u)", excitation_current); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } } fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); - fwnode_handle_put(ref); return &rtd->sensor; -fail: - fwnode_handle_put(ref); - return ERR_PTR(ret); } static struct ltc2983_sensor * @@ -921,7 +902,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s { struct ltc2983_thermistor *thermistor; struct device *dev = &st->spi->dev; - struct fwnode_handle *ref; + struct fwnode_handle *ref __free(fwnode_handle) = NULL; u32 excitation_current = 0; int ret = 0; @@ -938,7 +919,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan); if (ret) { dev_err(dev, "rsense channel must be configured...\n"); - goto fail; + return ERR_PTR(ret); } if (fwnode_property_read_bool(child, "adi,single-ended")) { @@ -958,8 +939,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s dev_err(&st->spi->dev, "Invalid chann:%d for differential thermistor", sensor->chan); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } /* check custom sensor */ @@ -978,10 +958,8 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s propname, steinhart, 64, false); - if (IS_ERR(thermistor->custom)) { - ret = PTR_ERR(thermistor->custom); - goto fail; - } + if (IS_ERR(thermistor->custom)) + return ERR_CAST(thermistor->custom); } /* set common parameters */ thermistor->sensor.fault_handler = ltc2983_common_fault_handler; @@ -1005,8 +983,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s LTC2983_SENSOR_THERMISTOR_STEINHART) { dev_err(&st->spi->dev, "Auto Range not allowed for custom sensors\n"); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } thermistor->excitation_current = 0x0c; break; @@ -1047,16 +1024,11 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s dev_err(&st->spi->dev, "Invalid value for excitation current(%u)", excitation_current); - ret = -EINVAL; - goto fail; + return ERR_PTR(-EINVAL); } } - fwnode_handle_put(ref); return &thermistor->sensor; -fail: - fwnode_handle_put(ref); - return ERR_PTR(ret); } static struct ltc2983_sensor *