linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Peter Rosin <peda@lysator.liu.se>, linux-iio@vger.kernel.org
Cc: Peter Rosin <peda@axentia.se>, Crt Mori <cmo@melexis.com>,
	Daniel Baluta <daniel.baluta@gmail.com>,
	Andreas Dannenberg <dannenberg@ti.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jonathan Cameron <jic23@kernel.org>,
	Hartmut Knaack <knaack.h@gmx.de>,
	Peter Meerwald <pmeerw@pmeerw.net>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5 2/2] iio: mcp4531: Driver for Microchip digital potentiometers
Date: Wed, 23 Sep 2015 15:28:22 +0200	[thread overview]
Message-ID: <5602A8F6.5060403@metafoo.de> (raw)
In-Reply-To: <1443009454-14310-3-git-send-email-peda@lysator.liu.se>

On 09/23/2015 01:57 PM, Peter Rosin wrote:
[...]

Looks pretty good.

One thing I'm concerned about is that this array

> +static const struct mcp4531_cfg mcp4531_cfg[] = {
> +	{ /* MCP4531-502 */ .wipers = 1, .max_pos = 128, .kohms =   5, },
> +	{ /* MCP4531-103 */ .wipers = 1, .max_pos = 128, .kohms =  10, },
> +	{ /* MCP4531-503 */ .wipers = 1, .max_pos = 128, .kohms =  50, },
> +	{ /* MCP4531-104 */ .wipers = 1, .max_pos = 128, .kohms = 100, },
[...]
> +};
> +

And the indices into the array defined here need to stay in sync. It is
really easy to mess this up by accident when a new entry is added. And it
will probably take a while to notice when this happens.

> +static const struct i2c_device_id mcp4531_id[] = {
> +	{ "mcp4531-502",  0 },
> +	{ "mcp4531-103",  1 },
> +	{ "mcp4531-503",  2 },
> +	{ "mcp4531-104",  3 },
[...]
> +	{}
> +};

I see two options to solve this. Either define a enum with all the different
devices and use a symbolic name for the indices.

E.g.

enum mcp4531_type {
	MCP4531_502,
	MCP4531_103,
	...
};

static const struct mcp5431_cfg mcp5431_cfg[] = {
	[MCP4531_502] = {.wipers = 1, .max_pos = 128, .kohms =   5, },
	[MCP4531_103] = {.wipers = 1, .max_pos = 128, .kohms =  10, },
	...
};

static const struct i2c_device_id mcp4531_id[] = {
	{ "mcp4531-502",  MCP4531_502 },
	{ "mcp4531-103",  MCP4531_103 },
	...
};

Or the second option is to directly assign the cfg struct as the driver data
in the device ID table.

E.g.

#define MCP4531_CFG(_wipers, _max_pos, _kohms) \
	(kernel_ulong_t)&(struct mcp4531_cfg) { \
		.wipers = _wipers, .max_pos = _max_pos, .kohms = _kohms }

static const struct i2c_device_id mcp4531_id[] = {
	{ "mcp4531-502",  MCP4531_CFG(1, 128, 5) },
	{ "mcp4531-103",  MCP4531_CFG(1, 128, 10) },
	...
};

      reply	other threads:[~2015-09-23 13:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-23 11:57 [PATCH v5 0/2] Driver for Microchip digital potentiometers Peter Rosin
2015-09-23 11:57 ` [PATCH v5 1/2] iio: resistance: Document that resistance can be output Peter Rosin
2015-09-23 11:57 ` [PATCH v5 2/2] iio: mcp4531: Driver for Microchip digital potentiometers Peter Rosin
2015-09-23 13:28   ` Lars-Peter Clausen [this message]

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=5602A8F6.5060403@metafoo.de \
    --to=lars@metafoo.de \
    --cc=cmo@melexis.com \
    --cc=daniel.baluta@gmail.com \
    --cc=dannenberg@ti.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jic23@kernel.org \
    --cc=knaack.h@gmx.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peda@axentia.se \
    --cc=peda@lysator.liu.se \
    --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).