All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Benoit Cousson <bcousson@baylibre.com>,
	Patrick Titiano <ptitiano@baylibre.com>
Subject: Re: [PATCH 1/5] hwmon: ina2xx: bail-out from ina2xx_probe() in case of configuration errors
Date: Tue, 25 Nov 2014 07:58:37 -0800	[thread overview]
Message-ID: <5474A72D.2000309@roeck-us.net> (raw)
In-Reply-To: <1416930423-12179-2-git-send-email-bgolaszewski@baylibre.com>

On 11/25/2014 07:46 AM, Bartosz Golaszewski wrote:
> The return value of i2c_smbus_write_word_swapped() isn't checked in
> ina2xx_probe(). This leads to devices being registered even if they can not
> be physically detected (e.g. device is not powered-up at boot-time).
>
> Even after restoring power to such device, it is left unconfigured as the
> configuration has never been actually written to the register.
>
> Error out in case of write errors in probe and notify the user.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> ---
>   drivers/hwmon/ina2xx.c | 49 +++++++++++++++++++++++++++++++++++++++++++------
>   1 file changed, 43 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/hwmon/ina2xx.c b/drivers/hwmon/ina2xx.c
> index bfd3f3e..660f8ca 100644
> --- a/drivers/hwmon/ina2xx.c
> +++ b/drivers/hwmon/ina2xx.c
> @@ -110,6 +110,40 @@ static const struct ina2xx_config ina2xx_config[] = {
>   	},
>   };
>
> +static int ina2xx_write_register(const struct i2c_client *client,
> +						u8 reg, u16 value)
> +{
> +	return i2c_smbus_write_word_swapped(client, reg, value);
> +}
> +
> +static int ina2xx_configure(const struct i2c_client *client, u16 value)
> +{
> +	int status;
> +
> +	status = ina2xx_write_register(client, INA2XX_CONFIG, value);
> +	if (status < 0) {
> +		dev_err(&client->dev,
> +			"error writing to configuration register: %d\n",
> +			status);
> +	}
> +
> +	return status;
> +}
> +
> +static int ina2xx_calibrate(const struct i2c_client *client, u16 value)
> +{
> +	int status;
> +
> +	status = ina2xx_write_register(client, INA2XX_CALIBRATION, value);
> +	if (status < 0) {
> +		dev_err(&client->dev,
> +			"error writing to calibration register: %d\n",
> +			status);
> +	}
> +
> +	return status;
> +}
> +

You are introducing those two functions with the same code just to display
a different error message. That does not provide enough value to have extra
functions and just increases code size.

Just check the return code from ina2xx_write_register directly in the probe
function and bail out there.

Thanks,
Guenter


>   static struct ina2xx_data *ina2xx_update_device(struct device *dev)
>   {
>   	struct ina2xx_data *data = dev_get_drvdata(dev);
> @@ -247,12 +281,15 @@ static int ina2xx_probe(struct i2c_client *client,
>   	data->config = &ina2xx_config[data->kind];
>
>   	/* device configuration */
> -	i2c_smbus_write_word_swapped(client, INA2XX_CONFIG,
> -				     data->config->config_default);
> -	/* set current LSB to 1mA, shunt is in uOhms */
> -	/* (equation 13 in datasheet) */
> -	i2c_smbus_write_word_swapped(client, INA2XX_CALIBRATION,
> -				     data->config->calibration_factor / shunt);
> +	if (ina2xx_configure(client, data->config->config_default) < 0)
> +		return -ENODEV;
> +
> +	/* Set current LSB to 1mA, shunt is in uOhms
> +	 * (equation 13 in datasheet).
> +	 */
> +	if (ina2xx_calibrate(client,
> +			data->config->calibration_factor / shunt) < 0)
> +		return -ENODEV;
>
>   	data->client = client;
>   	mutex_init(&data->update_lock);
>


  reply	other threads:[~2014-11-25 15:58 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-25 15:46 [PATCH 0/5] hwmon: ina2xx: fixes & extensions Bartosz Golaszewski
2014-11-25 15:46 ` [PATCH 1/5] hwmon: ina2xx: bail-out from ina2xx_probe() in case of configuration errors Bartosz Golaszewski
2014-11-25 15:58   ` Guenter Roeck [this message]
2014-11-25 16:25     ` Bartosz Golaszewski
2014-11-25 16:59       ` Guenter Roeck
2014-11-25 17:50         ` Bartosz Golaszewski
2014-11-25 17:59           ` Guenter Roeck
2014-11-25 18:22             ` Bartosz Golaszewski
2014-11-25 18:30               ` Guenter Roeck
2014-11-26  3:05                 ` Guenter Roeck
2014-11-26  9:13                   ` Bartosz Golaszewski
2014-11-26  9:38                   ` Benoit Cousson
2014-11-26 19:04                     ` [lm-sensors] [PATCH 1/5] hwmon: ina2xx: bail-out from ina2xx_probe() in case of configuration er Guenter Roeck
2014-11-26 19:04                       ` [PATCH 1/5] hwmon: ina2xx: bail-out from ina2xx_probe() in case of configuration errors Guenter Roeck
2014-11-27 10:18                       ` [lm-sensors] [PATCH 1/5] hwmon: ina2xx: bail-out from ina2xx_probe() in case of configuration er Jean Delvare
2014-11-27 10:18                         ` [PATCH 1/5] hwmon: ina2xx: bail-out from ina2xx_probe() in case of configuration errors Jean Delvare
2014-11-25 15:47 ` [PATCH 2/5] hwmon: ina2xx: make shunt resistance configurable at run-time Bartosz Golaszewski
2014-11-25 15:59   ` Guenter Roeck
2014-11-25 16:09     ` Bartosz Gołaszewski
2014-11-25 15:47 ` [PATCH 3/5] hwmon: ina2xx: allow to change the averaging rate " Bartosz Golaszewski
2014-11-25 16:01   ` Guenter Roeck
2014-11-25 15:47 ` [PATCH 4/5] hwmon: ina2xx: change hex constants to lower-case Bartosz Golaszewski
2014-11-25 15:47 ` [PATCH 5/5] hwmon: ina2xx: documentation update for new sysfs attributes Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5474A72D.2000309@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=bcousson@baylibre.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ptitiano@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.