From: khali@linux-fr.org (Jean Delvare)
To: lm-sensors@vger.kernel.org
Subject: [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier
Date: Sat, 04 Jun 2005 09:25:51 +0000 [thread overview]
Message-ID: <20050604092624.1810ad73.khali@linux-fr.org> (raw)
In-Reply-To: <F06EB2894F3F0D45AD9BEA7246DE9C9076C7D9@WCSEXCMBS1.ad.wabtec.com>
Hi Ben,
> Anyway, here is attempt 3, as an attachment.
> =>
> This is a driver for the PCA9539, a 16 bit digital I/O chip.
> It uses the new i2c-sysfs interfaces.
And here are my comments about your code (which overall looks very
good).
> +/* following are the sysfs callback functions */
> +static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
> + char *buf)
> +{
> + struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
> + struct pca9539_data *data = i2c_get_clientdata(to_i2c_client(dev));
> + return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(&data->client, (u8)psa->index));
> +}
This can be made a lot more simple. You currently go from dev to client
to data back to client. What about:
static ssize_t pca9539_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
struct i2c_client *client = to_i2c_client(dev);
return sprintf(buf, "%d\n", i2c_smbus_read_byte_data(client, (u8)psa->index));
}
Same holds for pca9539_store(), obviously.
BTW, please split lines which are longer than 80 characters, as
mentioned in Documentation/CodingStyle.
> +static ssize_t pca9539_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t count)
> +{
> + struct sensor_device_attribute *psa = to_sensor_dev_attr(attr);
> + struct pca9539_data *data = i2c_get_clientdata(to_i2c_client(dev));
> + u8 val = simple_strtoul(buf, NULL, 0);
> + i2c_smbus_write_byte_data(&data->client, (u8)psa->index, val);
> + return count;
> +}
You fail to check the validity of the new value. Wrapping it into an u8
is no good. It would be far better to actually check the value, and
reject it if incorrect. Something in the lines of:
unsigned long val = simple_strtoul(buf, NULL, 0);
if (val > 0xff)
return -EINVAL;
This is what the similar pcf8574 driver does. You may even have it warn
in the logs if you want.
> + static SENSOR_DEVICE_ATTR(name,S_IRUGO,pca9539_show,NULL,enum_name)
> (...)
> + static SENSOR_DEVICE_ATTR(name,S_IRUGO|S_IWUSR,pca9539_show,pca9539_store,enum_name)
Add one space after each comma please. Spaces are also welcome around
"|". As a side note, "enum_name" is a bad name IMHO, I would go for
"index" or "command" or whatever you like which expresses what this
parameter is holding.
> +PCA9539_ENTRY_RW(config0, PCA9539_CONFIG_0);
> +PCA9539_ENTRY_RW(config1, PCA9539_CONFIG_1);
"config" isn't a very relevant name. It could mean about anything
depending on the chip. What about "direction" instead?
> + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
> + | I2C_FUNC_SMBUS_WRITE_BYTE_DATA
> + | I2C_FUNC_SMBUS_READ_BYTE_DATA))
> + goto exit;
Not correct. You test for I2C_FUNC_SMBUS_BYTE but never actually use
this SMBus command in your code. Additionally,
I2C_FUNC_SMBUS_WRITE_BYTE_DATA | I2C_FUNC_SMBUS_READ_BYTE_DATA can be
shorten into I2C_FUNC_SMBUS_BYTE_DATA.
> + /* Detection: not sure how to do that...
> + * possibility: toggle the value read from invert0 and see if input0
> + * changes polarity.
> + * However, if the inputs are changing, this won't work.
> + */
No, detection code should not write anything to the chips.
One possible detection method is to check that input and output match
(modulo the polarity inversion) for all pins configured as outputs. I
have made something similar in sensors-detect for the PCA9556, which is
basically the same chip as the PCA9539 with half the GPIO pins. The code
there reads like:
return unless ($input & ~$config) = (($output ^ $invert) & ~$config);
Another possible check would be on the number of registers (or
commands). The PCA9539 has 8 commands, so reading "register" 0x07 will
return a value, while reading register "0x08" will return an error.
If you could add detection for your chip to sensors-detect once you're
done with the driver, this would be appreciated.
> + /* Register sysfs hooks */
> + return sysfs_create_group(&new_client->dev.kobj, &pca9539_defattr_group);
Negative, an error from sysfs_create_group() should not be returned by
the detection function due to the current (broken) design of
i2c_detect().
> --- linux-2.6.12-rc5-mm2-clean/drivers/i2c/chips/Kconfig 2005-06-01 14:29:17.000000000 -0500
> +++ linux-2.6.12-rc5-mm2-pca9539/drivers/i2c/chips/Kconfig 2005-06-03 12:09:22.000000000 -0500
> @@ -498,4 +498,14 @@
> This driver can also be built as a module. If so, the module
> will be called m41t00.
>
> +config SENSORS_PCA9539
> + tristate "PCA9539 16-bit digital IO"
> + depends on I2C && EXPERIMENTAL
> + help
> + If you say yes here you get support for the PCA9539
> + low power digital I/O with 16 bit.
> +
> + This driver can also be built as a module. If so, the module
> + will be called pca9539.
> +
> endmenu
I'd rather see this entry next to the PCF8574 one, as both chips are
similar in their function.
Thanks,
--
Jean Delvare
next prev parent reply other threads:[~2005-06-04 9:25 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-06-03 22:25 [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier BGardner
2005-06-03 22:30 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier (retry) BGardner
2005-06-03 23:17 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier Greg KH
2005-06-03 23:49 ` BGardner
2005-06-04 9:25 ` Jean Delvare [this message]
2005-06-04 9:31 ` Yani Ioannou
2005-06-06 17:18 ` BGardner
2005-06-06 17:30 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier (retry 4 BGardner
2005-06-06 21:37 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier Jean Delvare
2005-06-06 22:28 ` BGardner
2005-06-06 22:33 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier (retry 5 BGardner
2005-06-06 22:39 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier Greg KH
2005-06-07 1:13 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c driver (retry 7) bgardner
2005-06-07 1:15 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c drvier BGardner
2005-06-07 1:22 ` Greg KH
2005-06-07 15:57 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c driver (retry 9 bgardner
2005-06-07 21:31 ` [lm-sensors] [PATCH 2.6.12-rc5-mm2] pca9539: new i2c driver Jean Delvare
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=20050604092624.1810ad73.khali@linux-fr.org \
--to=khali@linux-fr.org \
--cc=lm-sensors@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 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.