2 * Nano River Technologies viperboard IIO ADC driver
4 * (C) 2012 by Lemonage GmbH
5 * Author: Lars Poeschel <poeschel@lemonage.de>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
23 #include <linux/usb.h>
24 #include <linux/iio/iio.h>
26 #include <linux/mfd/viperboard.h>
28 #define VPRBRD_ADC_CMD_GET 0x00
30 struct vprbrd_adc_msg {
40 #define VPRBRD_ADC_CHANNEL(_index) { \
41 .type = IIO_VOLTAGE, \
44 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
45 .scan_index = _index, \
53 static struct iio_chan_spec const vprbrd_adc_iio_channels[] = {
54 VPRBRD_ADC_CHANNEL(0),
55 VPRBRD_ADC_CHANNEL(1),
56 VPRBRD_ADC_CHANNEL(2),
57 VPRBRD_ADC_CHANNEL(3),
60 static int vprbrd_iio_read_raw(struct iio_dev *iio_dev,
61 struct iio_chan_spec const *chan,
67 struct vprbrd_adc *adc = iio_priv(iio_dev);
68 struct vprbrd *vb = adc->vb;
69 struct vprbrd_adc_msg *admsg = (struct vprbrd_adc_msg *)vb->buf;
72 case IIO_CHAN_INFO_RAW:
73 mutex_lock(&vb->lock);
75 admsg->cmd = VPRBRD_ADC_CMD_GET;
76 admsg->chan = chan->scan_index;
79 ret = usb_control_msg(vb->usb_dev,
80 usb_sndctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
81 VPRBRD_USB_TYPE_OUT, 0x0000, 0x0000, admsg,
82 sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
83 if (ret != sizeof(struct vprbrd_adc_msg)) {
84 dev_err(&iio_dev->dev, "usb send error on adc read\n");
88 ret = usb_control_msg(vb->usb_dev,
89 usb_rcvctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
90 VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, admsg,
91 sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
95 mutex_unlock(&vb->lock);
97 if (ret != sizeof(struct vprbrd_adc_msg)) {
98 dev_err(&iio_dev->dev, "usb recv error on adc read\n");
114 static const struct iio_info vprbrd_adc_iio_info = {
115 .read_raw = &vprbrd_iio_read_raw,
116 .driver_module = THIS_MODULE,
119 static int vprbrd_adc_probe(struct platform_device *pdev)
121 struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
122 struct vprbrd_adc *adc;
123 struct iio_dev *indio_dev;
126 /* registering iio */
127 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
129 dev_err(&pdev->dev, "failed allocating iio device\n");
133 adc = iio_priv(indio_dev);
135 indio_dev->name = "viperboard adc";
136 indio_dev->dev.parent = &pdev->dev;
137 indio_dev->info = &vprbrd_adc_iio_info;
138 indio_dev->modes = INDIO_DIRECT_MODE;
139 indio_dev->channels = vprbrd_adc_iio_channels;
140 indio_dev->num_channels = ARRAY_SIZE(vprbrd_adc_iio_channels);
142 ret = iio_device_register(indio_dev);
144 dev_err(&pdev->dev, "could not register iio (adc)");
148 platform_set_drvdata(pdev, indio_dev);
153 static int vprbrd_adc_remove(struct platform_device *pdev)
155 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
157 iio_device_unregister(indio_dev);
162 static struct platform_driver vprbrd_adc_driver = {
164 .name = "viperboard-adc",
165 .owner = THIS_MODULE,
167 .probe = vprbrd_adc_probe,
168 .remove = vprbrd_adc_remove,
171 module_platform_driver(vprbrd_adc_driver);
173 MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
174 MODULE_DESCRIPTION("IIO ADC driver for Nano River Techs Viperboard");
175 MODULE_LICENSE("GPL");
176 MODULE_ALIAS("platform:viperboard-adc");