linux-hwmon.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Iker Perez <iker.perez@codethink.co.uk>,
	Jean Delvare <jdelvare@suse.com>,
	linux-hwmon@vger.kernel.org
Subject: Re: [PATCH v1 4/5] hwmon: (lm75) Create function from code to write into registers
Date: Tue, 9 Jul 2019 06:39:56 -0700	[thread overview]
Message-ID: <ccc9310a-e13d-c327-0467-d6f82674a4ac@roeck-us.net> (raw)
In-Reply-To: <20190709095052.7964-5-iker.perez@codethink.co.uk>

On 7/9/19 2:50 AM, Iker Perez wrote:
> From: Iker Perez del Palomar Sustatxa <iker.perez@codethink.co.uk>
> 
> This function will be needed later to configure update_interval
> 
> Signed-off-by: Iker Perez del Palomar Sustatxa <iker.perez@codethink.co.uk>
> ---
>   drivers/hwmon/lm75.c | 63 ++++++++++++++++++++++++++++++----------------------
>   1 file changed, 36 insertions(+), 27 deletions(-)
> 
> diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c
> index 1d4d060bd695..5ba7277dac69 100644
> --- a/drivers/hwmon/lm75.c
> +++ b/drivers/hwmon/lm75.c
> @@ -78,6 +78,40 @@ static inline long lm75_reg_to_mc(s16 temp, u8 resolution)
>   	return ((temp >> (16 - resolution)) * 1000) >> (resolution - 8);
>   }
>   
> +static void lm75_remove(void *data)
> +{
> +	struct lm75_data *lm75 = data;
> +	struct i2c_client *client = lm75->client;
> +
> +	i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
> +}
> +static int configure_reg(u8 set_mask, u8 clr_mask, struct lm75_data *data,
> +		struct i2c_client *client)
> +{
> +	int status, err, new;
> +	struct device *dev = &client->dev;
> +
> +	/* configure as specified */
> +	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
> +	if (status < 0) {
> +		dev_dbg(dev, "Can't read config? %d\n", status);
> +		return status;
> +	}
> +	data->orig_conf = status;

Overwriting the "original" configuration each time the update time is changed
is really a bad idea.

You'll want to cache the current configuration register value, and not
re-read it each time the configuration is updated.


> +	new = status & ~clr_mask;
> +	new |= set_mask;
> +	if (status != new)
> +		i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
> +
> +	err = devm_add_action_or_reset(dev, lm75_remove, data);
> +	if (err)
> +		return err;
> +

The function is a good idea, but you can't use devm_add_action_or_reset() here.
That will have to remain in the probe function. Otherwise an action will be added
each time the resolution/update time is changed.

> +	dev_dbg(dev, "Config %02x\n", new);
> +
> +	return 0;
> +}
> +
>   static int lm75_read(struct device *dev, enum hwmon_sensor_types type,
>   		     u32 attr, int channel, long *val)
>   {
> @@ -238,14 +272,6 @@ static const struct regmap_config lm75_regmap_config = {
>   	.use_single_write = true,
>   };
>   
> -static void lm75_remove(void *data)
> -{
> -	struct lm75_data *lm75 = data;
> -	struct i2c_client *client = lm75->client;
> -
> -	i2c_smbus_write_byte_data(client, LM75_REG_CONF, lm75->orig_conf);
> -}
> -
>   static int
>   lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
>   {
> @@ -253,9 +279,8 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
>   	struct device *hwmon_dev;
>   	struct lm75_data *data;
>   	struct lm75_data device_data;
> -	int status, err;
> +	int status;
>   	u8 set_mask, clr_mask;
> -	int new;
>   
>   	data = &device_data;
>   	if (client->dev.of_node)
> @@ -370,23 +395,7 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id)
>   		break;
>   	}
>   
> -	/* configure as specified */
> -	status = i2c_smbus_read_byte_data(client, LM75_REG_CONF);
> -	if (status < 0) {
> -		dev_dbg(dev, "Can't read config? %d\n", status);
> -		return status;
> -	}
> -	data->orig_conf = status;
> -	new = status & ~clr_mask;
> -	new |= set_mask;
> -	if (status != new)
> -		i2c_smbus_write_byte_data(client, LM75_REG_CONF, new);
> -
> -	err = devm_add_action_or_reset(dev, lm75_remove, data);
> -	if (err)
> -		return err;
> -
> -	dev_dbg(dev, "Config %02x\n", new);
> +	status = configure_reg(set_mask, clr_mask, data, client);
>   
>   	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
>   							 data, &lm75_chip_info,
> 


  reply	other threads:[~2019-07-09 13:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-09  9:50 [PATCH v1 0/5] Help with lm75.c changes Iker Perez
2019-07-09  9:50 ` [PATCH v1 1/5] hwmon: (lm75) Add kind field to struct lm75_data Iker Perez
2019-07-09 13:20   ` Guenter Roeck
2019-07-09  9:50 ` [PATCH v1 2/5] hwmon: (lm75) Include hwmon_chip in the permitted types to be writen Iker Perez
2019-07-09 13:26   ` Guenter Roeck
2019-07-09  9:50 ` [PATCH v1 3/5] hwmon: (lm75) Give write permission to hwmon_chip_update_interval Iker Perez
2019-07-09  9:50 ` [PATCH v1 4/5] hwmon: (lm75) Create function from code to write into registers Iker Perez
2019-07-09 13:39   ` Guenter Roeck [this message]
2019-07-09  9:50 ` [PATCH v1 5/5] First approach to sample time writing method Iker Perez
2019-07-09 13:33   ` Guenter Roeck
2019-07-09 13:43 ` [PATCH v1 0/5] Help with lm75.c changes Guenter Roeck
2019-07-09 15:11   ` Iker Perez del Palomar

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=ccc9310a-e13d-c327-0467-d6f82674a4ac@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=iker.perez@codethink.co.uk \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).