10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
29 if (gpio_is_valid(st->
pdata->gpio_reset)) {
39 static int ad7606_scan_direct(
struct iio_dev *indio_dev,
unsigned ch)
51 if (gpio_is_valid(st->
pdata->gpio_frstdata)) {
52 ret = st->
bops->read_block(st->
dev, 1, st->data);
61 ret = st->
bops->read_block(st->
dev,
62 st->
chip_info->num_channels - 1, &st->data[1]);
66 ret = st->
bops->read_block(st->
dev,
80 static int ad7606_read_raw(
struct iio_dev *indio_dev,
88 unsigned int scale_uv;
93 if (iio_buffer_enabled(indio_dev))
96 ret = ad7606_scan_direct(indio_dev, chan->
address);
104 scale_uv = (st->
range * 1000 * 2)
105 >> st->
chip_info->channels[0].scan_type.realbits;
106 *val = scale_uv / 1000;
107 *val2 = (scale_uv % 1000) * 1000;
116 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
125 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
131 if (!(lval == 5000 || lval == 10000)) {
132 dev_err(dev,
"range is not supported\n");
144 ad7606_show_range, ad7606_store_range, 0);
147 static ssize_t ad7606_show_oversampling_ratio(
struct device *dev,
150 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
156 static int ad7606_oversampling_get_index(
unsigned val)
158 unsigned char supported[] = {0, 2, 4, 8, 16, 32, 64};
162 if (val == supported[i])
168 static ssize_t ad7606_store_oversampling_ratio(
struct device *dev,
171 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
179 ret = ad7606_oversampling_get_index(lval);
181 dev_err(dev,
"oversampling %lu is not supported\n", lval);
196 ad7606_show_oversampling_ratio,
197 ad7606_store_oversampling_ratio, 0);
198 static IIO_CONST_ATTR(oversampling_ratio_available,
"0 2 4 8 16 32 64");
200 static struct attribute *ad7606_attributes_os_and_range[] = {
201 &iio_dev_attr_in_voltage_range.dev_attr.attr,
202 &iio_const_attr_in_voltage_range_available.dev_attr.attr,
203 &iio_dev_attr_oversampling_ratio.dev_attr.attr,
204 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
208 static const struct attribute_group ad7606_attribute_group_os_and_range = {
209 .attrs = ad7606_attributes_os_and_range,
212 static struct attribute *ad7606_attributes_os[] = {
213 &iio_dev_attr_oversampling_ratio.dev_attr.attr,
214 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
219 .attrs = ad7606_attributes_os,
222 static struct attribute *ad7606_attributes_range[] = {
223 &iio_dev_attr_in_voltage_range.dev_attr.attr,
224 &iio_const_attr_in_voltage_range_available.dev_attr.attr,
229 .attrs = ad7606_attributes_range,
232 #define AD7606_CHANNEL(num) \
234 .type = IIO_VOLTAGE, \
238 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
239 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
241 .scan_type = IIO_ST('s', 16, 16, 0), \
281 .channels = ad7606_8_channels,
287 .channels = ad7606_6_channels,
293 .channels = ad7606_4_channels,
298 static int ad7606_request_gpios(
struct ad7606_state *st)
300 struct gpio gpio_array[3] = {
305 .label =
"AD7606_OS0",
308 .gpio = st->
pdata->gpio_os1,
311 .label =
"AD7606_OS1",
314 .gpio = st->
pdata->gpio_os2,
317 .label =
"AD7606_OS2",
322 if (gpio_is_valid(st->
pdata->gpio_convst)) {
327 dev_err(st->
dev,
"failed to request GPIO CONVST\n");
335 if (gpio_is_valid(st->
pdata->gpio_os0) &&
336 gpio_is_valid(st->
pdata->gpio_os1) &&
337 gpio_is_valid(st->
pdata->gpio_os2)) {
340 goto error_free_convst;
343 if (gpio_is_valid(st->
pdata->gpio_reset)) {
351 if (gpio_is_valid(st->
pdata->gpio_range)) {
356 goto error_free_reset;
358 if (gpio_is_valid(st->
pdata->gpio_stby)) {
363 goto error_free_range;
366 if (gpio_is_valid(st->
pdata->gpio_frstdata)) {
370 goto error_free_stby;
376 if (gpio_is_valid(st->
pdata->gpio_stby))
379 if (gpio_is_valid(st->
pdata->gpio_range))
382 if (gpio_is_valid(st->
pdata->gpio_reset))
385 if (gpio_is_valid(st->
pdata->gpio_os0) &&
386 gpio_is_valid(st->
pdata->gpio_os1) &&
387 gpio_is_valid(st->
pdata->gpio_os2))
397 if (gpio_is_valid(st->
pdata->gpio_frstdata))
399 if (gpio_is_valid(st->
pdata->gpio_stby))
401 if (gpio_is_valid(st->
pdata->gpio_range))
403 if (gpio_is_valid(st->
pdata->gpio_reset))
405 if (gpio_is_valid(st->
pdata->gpio_os0) &&
406 gpio_is_valid(st->
pdata->gpio_os1) &&
407 gpio_is_valid(st->
pdata->gpio_os2)) {
423 if (iio_buffer_enabled(indio_dev)) {
434 static const struct iio_info ad7606_info_no_os_or_range = {
436 .read_raw = &ad7606_read_raw,
439 static const struct iio_info ad7606_info_os_and_range = {
441 .read_raw = &ad7606_read_raw,
442 .attrs = &ad7606_attribute_group_os_and_range,
445 static const struct iio_info ad7606_info_os = {
447 .read_raw = &ad7606_read_raw,
448 .attrs = &ad7606_attribute_group_os,
451 static const struct iio_info ad7606_info_range = {
453 .read_raw = &ad7606_read_raw,
454 .attrs = &ad7606_attribute_group_range,
467 if (indio_dev ==
NULL) {
472 st = iio_priv(indio_dev);
479 ret = ad7606_oversampling_get_index(pdata->
default_os);
481 dev_warn(dev,
"oversampling %d is not supported\n",
489 if (!IS_ERR(st->
reg)) {
498 indio_dev->
dev.parent =
dev;
499 if (gpio_is_valid(st->
pdata->gpio_os0) &&
500 gpio_is_valid(st->
pdata->gpio_os1) &&
501 gpio_is_valid(st->
pdata->gpio_os2)) {
502 if (gpio_is_valid(st->
pdata->gpio_range))
503 indio_dev->
info = &ad7606_info_os_and_range;
505 indio_dev->
info = &ad7606_info_os;
507 if (gpio_is_valid(st->
pdata->gpio_range))
508 indio_dev->
info = &ad7606_info_range;
510 indio_dev->
info = &ad7606_info_no_os_or_range;
519 ret = ad7606_request_gpios(st);
521 goto error_disable_reg;
525 dev_warn(st->
dev,
"failed to RESET: no RESET GPIO specified\n");
530 goto error_free_gpios;
538 goto error_unregister_ring;
541 error_unregister_ring:
548 ad7606_free_gpios(st);
551 if (!IS_ERR(st->
reg))
554 if (!IS_ERR(st->
reg))
569 if (!IS_ERR(st->
reg)) {
574 ad7606_free_gpios(st);
584 if (gpio_is_valid(st->
pdata->gpio_stby)) {
585 if (gpio_is_valid(st->
pdata->gpio_range))
595 if (gpio_is_valid(st->
pdata->gpio_stby)) {
596 if (gpio_is_valid(st->
pdata->gpio_range))