linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Peter Meerwald <pmeerw@pmeerw.net>
Cc: linux-iio@vger.kernel.org
Subject: Re: [PATCH v2 07/15] staging:iio:hmc5843: Use CALIBSCALE instead of magn_range
Date: Fri, 20 Sep 2013 20:57:37 +0100	[thread overview]
Message-ID: <523CA8B1.7030003@kernel.org> (raw)
In-Reply-To: <1379619360-8150-8-git-send-email-pmeerw@pmeerw.net>

On 09/19/13 20:35, Peter Meerwald wrote:
> Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
Hi Peter,

I'm afraid I don't follow why you don't just make scale writeable
rather than introducing a second element to control the same thing.
Calibscale is intended for the case where the hardware can change
the scale without it being apparent from the raw readings. Sometimes
it also gets used to handling amplifiers in cases where the conversion
function is 'interesting' and we have no choice but to do it in kernel
in order to have data provided to userspace in a comprehensible form
but that doesn't apply here.

Jonathan

> ---
>  drivers/staging/iio/magnetometer/hmc5843.c | 108 +++++++++++++----------------
>  1 file changed, 47 insertions(+), 61 deletions(-)
> 
> diff --git a/drivers/staging/iio/magnetometer/hmc5843.c b/drivers/staging/iio/magnetometer/hmc5843.c
> index 71f26de..0c3f41e 100644
> --- a/drivers/staging/iio/magnetometer/hmc5843.c
> +++ b/drivers/staging/iio/magnetometer/hmc5843.c
> @@ -125,16 +125,19 @@ static const int hmc5883l_regval_to_nanoscale[] = {
>   * 6		| (+-)5.6				| 330
>   * 7		| (+-)8.1				| 230
>   */
> -static const int hmc5843_regval_to_input_field_mga[] = {
> -	700, 1000, 1500, 2000, 3200, 3800, 4500, 6500
> +static const int hmc5843_regval_to_range_ga[8][2] = {
> +	{0, 700000}, {1, 0}, {1, 500000}, {2, 0}, {3, 200000},
> +	{3, 800000}, {4, 500000}, {6, 500000}
>  };
>  
> -static const int hmc5883_regval_to_input_field_mga[] = {
> -	900, 1200, 1900, 2500, 4000, 4600, 5500, 7900
> +static const int hmc5883_regval_to_range_ga[8][2] = {
> +	{0, 900000}, {1, 200000}, {1, 900000}, {2, 500000}, {4, 0},
> +	{4, 600000}, {5, 500000}, {7, 900000}
>  };
>  
> -static const int hmc5883l_regval_to_input_field_mga[] = {
> -	880, 1300, 1900, 2500, 4000, 4700, 5600, 8100
> +static const int hmc5883l_regval_to_range_ga[8][2] = {
> +	{0, 880000}, {1, 300000}, {1, 900000}, {2, 500000}, {4, 0},
> +	{4, 700000}, {5, 600000}, {8, 100000}
>  };
>  
>  /*
> @@ -163,7 +166,7 @@ static const int hmc5883_regval_to_samp_freq[7][2] = {
>  struct hmc5843_chip_info {
>  	const struct iio_chan_spec *channels;
>  	const int (*regval_to_samp_freq)[2];
> -	const int *regval_to_input_field_mga;
> +	const int (*regval_to_range_ga)[2];
>  	const int *regval_to_nanoscale;
>  };
>  
> @@ -412,58 +415,25 @@ static int hmc5843_check_samp_freq(struct hmc5843_data *data,
>  		val, val2);
>  }
>  
> -static ssize_t hmc5843_show_range_gain(struct device *dev,
> -				struct device_attribute *attr,
> -				char *buf)
> +static ssize_t hmc5843_show_calibscale_avail(struct device *dev,
> +				struct device_attribute *attr, char *buf)
>  {
> -	u8 range;
> -	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> -	struct hmc5843_data *data = iio_priv(indio_dev);
> +	struct hmc5843_data *data = iio_priv(dev_to_iio_dev(dev));
>  
> -	range = data->range;
> -	return sprintf(buf, "%d\n", data->variant->regval_to_input_field_mga[range]);
> +	return hmc5843_show_int_plus_micros(buf,
> +		data->variant->regval_to_range_ga, HMC5843_RANGE_GAIN_MAX+1);
>  }
>  
> -static ssize_t hmc5843_set_range_gain(struct device *dev,
> -			struct device_attribute *attr,
> -			const char *buf,
> -			size_t count)
> -{
> -	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> -	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
> -	struct hmc5843_data *data = iio_priv(indio_dev);
> -	unsigned long range = 0;
> -	int error;
> -
> -	mutex_lock(&data->lock);
> -	error = kstrtoul(buf, 10, &range);
> -	if (error) {
> -		count = error;
> -		goto exit;
> -	}
> -	dev_dbg(dev, "set range to %lu\n", range);
> +static IIO_DEVICE_ATTR(calibscale_available, S_IRUGO,
> +	hmc5843_show_calibscale_avail, NULL, 0);
>  
> -	if (range > HMC5843_RANGE_GAIN_MAX) {
> -		count = -EINVAL;
> -		goto exit;
> -	}
> -
> -	data->range = range;
> -	range = range << HMC5843_RANGE_GAIN_OFFSET;
> -	if (i2c_smbus_write_byte_data(data->client, this_attr->address, range))
> -		count = -EINVAL;
> -
> -exit:
> -	mutex_unlock(&data->lock);
> -	return count;
> +static int hmc5843_check_range(struct hmc5843_data *data,
> +	int val, int val2)
> +{
> +	return hmc5843_check_int_plus_micros(data->variant->regval_to_range_ga,
> +		HMC5843_RANGE_GAIN_MAX+1, val, val2);
>  }
>  
> -static IIO_DEVICE_ATTR(in_magn_range,
> -			S_IWUSR | S_IRUGO,
> -			hmc5843_show_range_gain,
> -			hmc5843_set_range_gain,
> -			HMC5843_CONFIG_REG_B);
> -
>  static int hmc5843_read_raw(struct iio_dev *indio_dev,
>  			    struct iio_chan_spec const *chan,
>  			    int *val, int *val2, long mask)
> @@ -481,6 +451,10 @@ static int hmc5843_read_raw(struct iio_dev *indio_dev,
>  		*val = data->variant->regval_to_samp_freq[data->rate][0];
>  		*val2 = data->variant->regval_to_samp_freq[data->rate][1];
>  		return IIO_VAL_INT_PLUS_MICRO;
> +	case IIO_CHAN_INFO_CALIBSCALE:
> +		*val = data->variant->regval_to_range_ga[data->range][0];
> +		*val2 = data->variant->regval_to_range_ga[data->range][1];
> +		return IIO_VAL_INT_PLUS_MICRO;
>  	}
>  	return -EINVAL;
>  }
> @@ -490,7 +464,7 @@ static int hmc5843_write_raw(struct iio_dev *indio_dev,
>  			     int val, int val2, long mask)
>  {
>  	struct hmc5843_data *data = iio_priv(indio_dev);
> -	int ret, rate;
> +	int ret, rate, range;
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_SAMP_FREQ:
> @@ -505,6 +479,20 @@ static int hmc5843_write_raw(struct iio_dev *indio_dev,
>  		mutex_unlock(&data->lock);
>  
>  		return ret;
> +	case IIO_CHAN_INFO_CALIBSCALE:
> +		range = hmc5843_check_range(data, val, val2);
> +		if (range < 0)
> +			return -EINVAL;
> +
> +		range <<= HMC5843_RANGE_GAIN_OFFSET;
> +		mutex_lock(&data->lock);
> +		ret = i2c_smbus_write_byte_data(data->client,
> +			HMC5843_CONFIG_REG_B, range);
> +		if (ret >= 0)
> +			data->range = range;
> +		mutex_unlock(&data->lock);
> +
> +		return ret;
>  	default:
>  		return -EINVAL;
>  	}
> @@ -517,7 +505,8 @@ static int hmc5843_write_raw(struct iio_dev *indio_dev,
>  		.channel2 = IIO_MOD_##axis,				\
>  		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
>  		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |	\
> -			BIT(IIO_CHAN_INFO_SAMP_FREQ),			\
> +			BIT(IIO_CHAN_INFO_SAMP_FREQ) |			\
> +			BIT(IIO_CHAN_INFO_CALIBSCALE),			\
>  		.address = addr						\
>  	}
>  
> @@ -536,7 +525,7 @@ static const struct iio_chan_spec hmc5883_channels[] = {
>  static struct attribute *hmc5843_attributes[] = {
>  	&iio_dev_attr_meas_conf.dev_attr.attr,
>  	&iio_dev_attr_operating_mode.dev_attr.attr,
> -	&iio_dev_attr_in_magn_range.dev_attr.attr,
> +	&iio_dev_attr_calibscale_available.dev_attr.attr,
>  	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
>  	NULL
>  };
> @@ -549,22 +538,19 @@ static const struct hmc5843_chip_info hmc5843_chip_info_tbl[] = {
>  	[HMC5843_ID] = {
>  		.channels = hmc5843_channels,
>  		.regval_to_samp_freq = hmc5843_regval_to_samp_freq,
> -		.regval_to_input_field_mga =
> -			hmc5843_regval_to_input_field_mga,
> +		.regval_to_range_ga = hmc5843_regval_to_range_ga,
>  		.regval_to_nanoscale = hmc5843_regval_to_nanoscale,
>  	},
>  	[HMC5883_ID] = {
>  		.channels = hmc5883_channels,
>  		.regval_to_samp_freq = hmc5883_regval_to_samp_freq,
> -		.regval_to_input_field_mga =
> -			hmc5883_regval_to_input_field_mga,
> +		.regval_to_range_ga = hmc5883_regval_to_range_ga,
>  		.regval_to_nanoscale = hmc5883_regval_to_nanoscale,
>  	},
>  	[HMC5883L_ID] = {
>  		.channels = hmc5883_channels,
>  		.regval_to_samp_freq = hmc5883_regval_to_samp_freq,
> -		.regval_to_input_field_mga =
> -			hmc5883l_regval_to_input_field_mga,
> +		.regval_to_range_ga = hmc5883l_regval_to_range_ga,
>  		.regval_to_nanoscale = hmc5883l_regval_to_nanoscale,
>  	},
>  };
> 

  reply	other threads:[~2013-09-20 18:57 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-19 19:35 [PATCH v2 00/15] staging:iio:hmc5843 cleanup Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 01/15] staging:iio:hmc5843: Add pointer to i2c client to data struct Peter Meerwald
2013-09-21 10:46   ` Jonathan Cameron
2013-10-01  9:40     ` Jonathan Cameron
2013-10-01  8:44       ` Peter Meerwald
2013-10-01 10:10         ` Jonathan Cameron
2013-09-19 19:35 ` [PATCH v2 02/15] staging:iio:hmc5843: Rewrite init function Peter Meerwald
2013-10-01  9:41   ` Jonathan Cameron
2013-09-19 19:35 ` [PATCH v2 03/15] staging:iio:hmc5843: Use INFO_SAMP_FREQ Peter Meerwald
2013-10-01  9:44   ` Jonathan Cameron
2013-09-19 19:35 ` [PATCH v2 04/15] staging:iio:hmc5843: Remove unused LSB register #defines Peter Meerwald
2013-10-01  9:55   ` Jonathan Cameron
2013-09-19 19:35 ` [PATCH v2 05/15] staging:iio:hmc5843: Tighten comments Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 06/15] staging:iio:hmc5843: Introduce helper functions to show/check list of int pairs Peter Meerwald
2013-10-01  9:55   ` Jonathan Cameron
2013-10-01  9:58     ` Jonathan Cameron
2013-09-19 19:35 ` [PATCH v2 07/15] staging:iio:hmc5843: Use CALIBSCALE instead of magn_range Peter Meerwald
2013-09-20 19:57   ` Jonathan Cameron [this message]
2013-09-20 20:12     ` Peter Meerwald
2013-09-20 21:54       ` Jonathan Cameron
2013-09-19 19:35 ` [PATCH v2 08/15] staging:iio:hmc5843: Always read all channels values otherwise no updates Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 09/15] staging:iio:hmc5843: Add trigger handling Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 10/15] staging:iio:hmc5843: Remove ability to change operating mode Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 11/15] staging:iio:hmc5843: Rename _configure() to _set_mode() Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 12/15] staging:iio:hmc5843: Reorganize _set_meas_conf() Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 13/15] staging:iio:hmc5843: Rename _set_rate() to _set_samp_freq() Peter Meerwald
2013-09-19 19:35 ` [PATCH v2 14/15] staging:iio:hmc5843: Introduce _set_range_gain() Peter Meerwald
2013-09-19 19:36 ` [PATCH v2 15/15] staging:iio:hmc5843: Check initialization and chip identifier Peter Meerwald
2013-09-20 20:05 ` [PATCH v2 00/15] staging:iio:hmc5843 cleanup Jonathan Cameron

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=523CA8B1.7030003@kernel.org \
    --to=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=pmeerw@pmeerw.net \
    /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).