Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Andrea Collamati <andrea.collamati@gmail.com>
Cc: Lars-Peter Clausen <lars@metafoo.de>,
	Rob Herring <robh+dt@kernel.org>,
	Krzysztof Kozlowski <krzysztof.kozlowski+dt@linaro.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 2/2] iio: add MCP4728 I2C DAC driver
Date: Sun, 23 Jul 2023 12:34:08 +0100	[thread overview]
Message-ID: <20230723123408.5d0078a6@jic23-huawei> (raw)
In-Reply-To: <a95bdc44-68e6-8d31-0c8a-24b83fb4c613@gmail.com>

On Fri, 21 Jul 2023 21:47:37 +0200
Andrea Collamati <andrea.collamati@gmail.com> wrote:

> Hi Jonathan,
> 
> thank you for your first review.
> 
> See below.
> 
> On 7/20/23 21:13, Jonathan Cameron wrote:
> 
> >> +
> >> +	outbuf[1] = ch->ref_mode << MCP4728_CMD_VREF_POS;
> >> +
> >> +	if (data->powerdown) {
> >> +		u8 mcp4728_pd_mode = ch->pd_mode + 1;
> >> +
> >> +		outbuf[1] |= mcp4728_pd_mode << MCP4728_CMD_PDMODE_POS;
> >> +	}
> >> +
> >> +	outbuf[1] |= ch->g_mode << MCP4728_CMD_GAIN_POS;  
> > FIELD_PREP()
> >  
> >> +	outbuf[1] |= ch->dac_value >> 8;
> >> +	outbuf[2] = ch->dac_value & 0xff;  
> > put_unaligned_be16()
> >  
> outbuf[1] contains other data (gain mode, powerdown  mode, etc) in addition to dac value. Using put_unaligned_be16 will probably reset them.

Ah. I'd missed the |= because you then didn't mask the dac_value.

> 
> Something like this could be the solution?
> 
> #defineMCP4728_DAC_H_FIELD GENMASK(3, 0)
> 
> #defineMCP4728_DAC_L_FIELD GENMASK(7, 0)

Call them _MASK or _MSK rather than field.
I think that is much more common naming.

> 
> ...
> 
> outbuf[1] |= FIELD_PREP(MCP4728_DAC_H_FIELD, ch->dac_value>> 8);
> 
> outbuf[2] = FIELD_PREP(MCP4728_DAC_L_FIELD, ch->dac_value);

That's better.

> 
> 
> >> +		return 0;
> >> +}
> >> +
> >> +// powerdown mode
> >> +static const char *const mcp4728_powerdown_modes[] = { "1kohm_to_gnd",
> >> +						       "100kohm_to_gnd",
> >> +						       "500kohm_to_gnd" };
> >> +
> >> +static int mcp4728_get_powerdown_mode(struct iio_dev *indio_dev,
> >> +				      const struct iio_chan_spec *chan)
> >> +{
> >> +	struct mcp4728_data *data = iio_priv(indio_dev);
> >> +
> >> +	return data->channel_data[chan->channel].pd_mode;
> >> +}
> >> +
> >> +static int mcp4728_set_powerdown_mode(struct iio_dev *indio_dev,
> >> +				      const struct iio_chan_spec *chan,
> >> +				      unsigned int mode)
> >> +{
> >> +	struct mcp4728_data *data = iio_priv(indio_dev);
> >> +
> >> +	data->channel_data[chan->channel].pd_mode = mode;
> >> +
> >> +	return 0;
> >> +}
> >> +
> >> +static ssize_t mcp4728_read_powerdown(struct iio_dev *indio_dev,
> >> +				      uintptr_t private,
> >> +				      const struct iio_chan_spec *chan,
> >> +				      char *buf)
> >> +{
> >> +	struct mcp4728_data *data = iio_priv(indio_dev);
> >> +
> >> +	return sysfs_emit(buf, "%d\n", data->powerdown);
> >> +}
> >> +
> >> +static ssize_t mcp4728_write_powerdown(struct iio_dev *indio_dev,
> >> +				       uintptr_t private,
> >> +				       const struct iio_chan_spec *chan,
> >> +				       const char *buf, size_t len)
> >> +{
> >> +	struct mcp4728_data *data = iio_priv(indio_dev);
> >> +	bool state;
> >> +	int ret;
> >> +
> >> +	ret = kstrtobool(buf, &state);
> >> +	if (ret)
> >> +		return ret;
> >> +
> >> +	if (state)
> >> +		ret = mcp4728_suspend(&data->client->dev);
> >> +	else
> >> +		ret = mcp4728_resume(&data->client->dev);
> >> +
> >> +	if (ret < 0)
> >> +		return ret;
> >> +
> >> +	return len;  
> > Don't support custom powering down. If this is needed it should be
> > using the existing power management flows.  
> 
> Could you explain better? I have extended customized powering down because I took as reference  the driver mcp4725.c and  I extended to multichannel.
> 
> How should I change it?

Ok. I'd missed that we had an existing driver doing this and indeed
we have documented it.  Fair enough - I must have been persuaded in the past
and then forgotten about it :)


> 
> > Might have been more interesting if it were per channel, but it's not.
> > (and I have no idea off the top of my head on how we would deal with it
> > if it were per channel).  
> 
> MCP4728 can handle power down per channel...
> 
> There are two bits per each channel the could be
> 
> 00 => No Power Down
> 
> 01 => 1kohm_to_gnd
> 
> 10 => 100kohm_to_gnd
> 
> 11 => 500kohm_to_gnd
> 

Ok.  Good to support that rather than a full power down.

> >  
> >> +				mcp4728_resume);
> >> +
> >> +static int mcp4728_init_channels_data(struct mcp4728_data *data)
> >> +{
> >> +	u8 inbuf[MCP4728_READ_RESPONSE_LEN];
> >> +	int ret;
> >> +	unsigned int i;
> >> +
> >> +	ret = i2c_master_recv(data->client, inbuf, MCP4728_READ_RESPONSE_LEN);
> >> +	if (ret < 0) {
> >> +		dev_err(&data->client->dev,
> >> +			"failed to read mcp4728 conf. Err=%d\n", ret);
> >> +		return ret;
> >> +	} else if (ret != MCP4728_READ_RESPONSE_LEN) {
> >> +		dev_err(&data->client->dev,
> >> +			"failed to read mcp4728 conf. Wrong Response Len ret=%d\n",
> >> +			ret);
> >> +		return -EIO;
> >> +	}
> >> +
> >> +	for (i = 0; i < MCP4728_N_CHANNELS; i++) {  
> > Unusual to read back existing values from the device rather than resetting it into a clean
> > state. What is your reasoning?  
> 
> MCP4728 has an EEPROM memory.
> 
> Under the reset conditions, the device uploads the
> EEPROM data into both of the DAC input and output
> registers simultaneously.
> 
> My reasoning was that the driver syncs with device at probe time and let the user change each channel parameters via iio_chan_spec_ext_info.

Ok - this is fine if it's reading back from EEPROM.
Please add a comment on that though because as demonstrated above
I'm forgetful and may wonder why it was done like this in the future.

Jonathan


> 
> 
> Thank you again.
> 
>         Andrea
> 
> 


  reply	other threads:[~2023-07-23 11:34 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 15:40 [PATCH v3 0/2] add MCP4728 I2C DAC driver​ Andrea Collamati
2023-07-20 15:40 ` [PATCH v3 1/2] dt-bindings: iio: dac: add mcp4728.yaml Andrea Collamati
2023-07-20 17:01   ` Conor Dooley
2023-07-23  4:59     ` Andrea Collamati
2023-07-21  8:21   ` Krzysztof Kozlowski
2023-07-21  8:22     ` Krzysztof Kozlowski
2023-07-21 12:02       ` Andrea Collamati
2023-07-21 11:58     ` Andrea Collamati
2023-07-21 12:07       ` Krzysztof Kozlowski
2023-07-20 15:40 ` [PATCH v3 2/2] iio: add MCP4728 I2C DAC driver Andrea Collamati
2023-07-20 19:13   ` Jonathan Cameron
2023-07-21 19:47     ` Andrea Collamati
2023-07-23 11:34       ` Jonathan Cameron [this message]
2023-07-23  5:09     ` Andrea Collamati
2023-07-23 11:41       ` Jonathan Cameron
2023-07-23 12:15         ` Andrea Collamati
2023-07-23 12:47           ` 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=20230723123408.5d0078a6@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andrea.collamati@gmail.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robh+dt@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