Commit 4cd8d874 authored by Lars-Peter Clausen's avatar Lars-Peter Clausen Committed by Greg Kroah-Hartman

staging:iio:dac:max517: Convert to channel spec

Convert the max517 driver to channel spec. As part of the conversion the
"out_voltage_1&2_raw" property, which updates both channel 1 and 2
simultaneously with the same value, is lost, since this is not really covered by
the IIO spec and has only a limited use case in practice.

Also the channel index for the sysfs files is now zero based instead of one
based, which means all channel numbers will be lower by one. E.g.
"out_voltage_1_scale" instead of "out_voltage_2_scale"
Signed-off-by: default avatarLars-Peter Clausen <lars@metafoo.de>
Acked-by: default avatarJonathan Cameron <jic23@kernel.org>
Acked-by: default avatarRoland Stigge <stigge@antcom.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent cadb6951
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include <linux/iio/iio.h> #include <linux/iio/iio.h>
#include <linux/iio/sysfs.h> #include <linux/iio/sysfs.h>
#include "dac.h"
#include "max517.h" #include "max517.h"
...@@ -55,129 +54,67 @@ struct max517_data { ...@@ -55,129 +54,67 @@ struct max517_data {
* bit 1: channel 2 * bit 1: channel 2
* (this way, it's possible to set both channels at once) * (this way, it's possible to set both channels at once)
*/ */
static ssize_t max517_set_value(struct device *dev, static int max517_set_value(struct iio_dev *indio_dev,
struct device_attribute *attr, long val, int channel)
const char *buf, size_t count, int channel)
{ {
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct max517_data *data = iio_priv(indio_dev); struct max517_data *data = iio_priv(indio_dev);
struct i2c_client *client = data->client; struct i2c_client *client = data->client;
u8 outbuf[4]; /* 1x or 2x command + value */ u8 outbuf[2];
int outbuf_size = 0;
int res; int res;
long val;
res = strict_strtol(buf, 10, &val);
if (res)
return res;
if (val < 0 || val > 255) if (val < 0 || val > 255)
return -EINVAL; return -EINVAL;
if (channel & 1) { outbuf[0] = channel;
outbuf[outbuf_size++] = COMMAND_CHANNEL0; outbuf[1] = val;
outbuf[outbuf_size++] = val;
}
if (channel & 2) {
outbuf[outbuf_size++] = COMMAND_CHANNEL1;
outbuf[outbuf_size++] = val;
}
/* res = i2c_master_send(client, outbuf, 2);
* At this point, there are always 1 or 2 two-byte commands in
* outbuf. With 2 commands, the device can set two outputs
* simultaneously, latching the values upon the end of the I2C
* transfer.
*/
res = i2c_master_send(client, outbuf, outbuf_size);
if (res < 0) if (res < 0)
return res; return res;
else if (res != 2)
return count; return -EIO;
} else
return 0;
static ssize_t max517_set_value_1(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
return max517_set_value(dev, attr, buf, count, 1);
}
static IIO_DEV_ATTR_OUT_RAW(1, max517_set_value_1, 0);
static ssize_t max517_set_value_2(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
return max517_set_value(dev, attr, buf, count, 2);
}
static IIO_DEV_ATTR_OUT_RAW(2, max517_set_value_2, 1);
static ssize_t max517_set_value_both(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
return max517_set_value(dev, attr, buf, count, 3);
} }
static IIO_DEVICE_ATTR_NAMED(out_voltage1and2_raw,
out_voltage1&2_raw, S_IWUSR, NULL,
max517_set_value_both, -1);
static ssize_t max517_show_scale(struct device *dev, static int max517_read_raw(struct iio_dev *indio_dev,
struct device_attribute *attr, struct iio_chan_spec const *chan,
char *buf, int channel) int *val,
int *val2,
long m)
{ {
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
struct max517_data *data = iio_priv(indio_dev); struct max517_data *data = iio_priv(indio_dev);
/* Corresponds to Vref / 2^(bits) */ unsigned int scale_uv;
unsigned int scale_uv = (data->vref_mv[channel - 1] * 1000) >> 8;
switch (m) {
return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000); case IIO_CHAN_INFO_SCALE:
/* Corresponds to Vref / 2^(bits) */
scale_uv = (data->vref_mv[chan->channel] * 1000) >> 8;
*val = scale_uv / 1000000;
*val2 = scale_uv % 1000000;
return IIO_VAL_INT_PLUS_MICRO;
default:
break;
}
return -EINVAL;
} }
static ssize_t max517_show_scale1(struct device *dev, static int max517_write_raw(struct iio_dev *indio_dev,
struct device_attribute *attr, struct iio_chan_spec const *chan, int val, int val2, long mask)
char *buf)
{ {
return max517_show_scale(dev, attr, buf, 1); int ret;
}
static IIO_DEVICE_ATTR(out_voltage1_scale, S_IRUGO, switch (mask) {
max517_show_scale1, NULL, 0); case IIO_CHAN_INFO_RAW:
ret = max517_set_value(indio_dev, val, chan->channel);
break;
default:
ret = -EINVAL;
break;
}
static ssize_t max517_show_scale2(struct device *dev, return ret;
struct device_attribute *attr,
char *buf)
{
return max517_show_scale(dev, attr, buf, 2);
} }
static IIO_DEVICE_ATTR(out_voltage2_scale, S_IRUGO,
max517_show_scale2, NULL, 0);
/* On MAX517 variant, we have one output */
static struct attribute *max517_attributes[] = {
&iio_dev_attr_out_voltage1_raw.dev_attr.attr,
&iio_dev_attr_out_voltage1_scale.dev_attr.attr,
NULL
};
static struct attribute_group max517_attribute_group = {
.attrs = max517_attributes,
};
/* On MAX518 and MAX519 variant, we have two outputs */
static struct attribute *max518_attributes[] = {
&iio_dev_attr_out_voltage1_raw.dev_attr.attr,
&iio_dev_attr_out_voltage1_scale.dev_attr.attr,
&iio_dev_attr_out_voltage2_raw.dev_attr.attr,
&iio_dev_attr_out_voltage2_scale.dev_attr.attr,
&iio_dev_attr_out_voltage1and2_raw.dev_attr.attr,
NULL
};
static struct attribute_group max518_attribute_group = {
.attrs = max518_attributes,
};
#ifdef CONFIG_PM_SLEEP #ifdef CONFIG_PM_SLEEP
static int max517_suspend(struct device *dev) static int max517_suspend(struct device *dev)
...@@ -201,13 +138,24 @@ static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume); ...@@ -201,13 +138,24 @@ static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume);
#endif #endif
static const struct iio_info max517_info = { static const struct iio_info max517_info = {
.attrs = &max517_attribute_group, .read_raw = max517_read_raw,
.write_raw = max517_write_raw,
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
static const struct iio_info max518_info = { #define MAX517_CHANNEL(chan) { \
.attrs = &max518_attribute_group, .type = IIO_VOLTAGE, \
.driver_module = THIS_MODULE, .indexed = 1, \
.output = 1, \
.channel = (chan), \
.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
IIO_CHAN_INFO_SCALE_SEPARATE_BIT, \
.scan_type = IIO_ST('u', 8, 8, 0), \
}
static const struct iio_chan_spec max517_channels[] = {
MAX517_CHANNEL(0),
MAX517_CHANNEL(1)
}; };
static int max517_probe(struct i2c_client *client, static int max517_probe(struct i2c_client *client,
...@@ -230,12 +178,14 @@ static int max517_probe(struct i2c_client *client, ...@@ -230,12 +178,14 @@ static int max517_probe(struct i2c_client *client,
/* establish that the iio_dev is a child of the i2c device */ /* establish that the iio_dev is a child of the i2c device */
indio_dev->dev.parent = &client->dev; indio_dev->dev.parent = &client->dev;
/* reduced attribute set for MAX517 */ /* reduced channel set for MAX517 */
if (id->driver_data == ID_MAX517) if (id->driver_data == ID_MAX517)
indio_dev->info = &max517_info; indio_dev->num_channels = 1;
else else
indio_dev->info = &max518_info; indio_dev->num_channels = 2;
indio_dev->channels = max517_channels;
indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->info = &max517_info;
/* /*
* Reference voltage on MAX518 and default is 5V, else take vref_mv * Reference voltage on MAX518 and default is 5V, else take vref_mv
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment