Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Nano River Technologies viperboard IIO ADC driver
0004  *
0005  *  (C) 2012 by Lemonage GmbH
0006  *  Author: Lars Poeschel <poeschel@lemonage.de>
0007  *  All rights reserved.
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/errno.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/types.h>
0015 #include <linux/mutex.h>
0016 #include <linux/platform_device.h>
0017 
0018 #include <linux/usb.h>
0019 #include <linux/iio/iio.h>
0020 
0021 #include <linux/mfd/viperboard.h>
0022 
0023 #define VPRBRD_ADC_CMD_GET      0x00
0024 
0025 struct vprbrd_adc_msg {
0026     u8 cmd;
0027     u8 chan;
0028     u8 val;
0029 } __packed;
0030 
0031 struct vprbrd_adc {
0032     struct vprbrd *vb;
0033 };
0034 
0035 #define VPRBRD_ADC_CHANNEL(_index) {            \
0036     .type = IIO_VOLTAGE,                \
0037     .indexed = 1,                   \
0038     .channel = _index,              \
0039     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),   \
0040 }
0041 
0042 static struct iio_chan_spec const vprbrd_adc_iio_channels[] = {
0043     VPRBRD_ADC_CHANNEL(0),
0044     VPRBRD_ADC_CHANNEL(1),
0045     VPRBRD_ADC_CHANNEL(2),
0046     VPRBRD_ADC_CHANNEL(3),
0047 };
0048 
0049 static int vprbrd_iio_read_raw(struct iio_dev *iio_dev,
0050                 struct iio_chan_spec const *chan,
0051                 int *val,
0052                 int *val2,
0053                 long info)
0054 {
0055     int ret, error = 0;
0056     struct vprbrd_adc *adc = iio_priv(iio_dev);
0057     struct vprbrd *vb = adc->vb;
0058     struct vprbrd_adc_msg *admsg = (struct vprbrd_adc_msg *)vb->buf;
0059 
0060     switch (info) {
0061     case IIO_CHAN_INFO_RAW:
0062         mutex_lock(&vb->lock);
0063 
0064         admsg->cmd = VPRBRD_ADC_CMD_GET;
0065         admsg->chan = chan->channel;
0066         admsg->val = 0x00;
0067 
0068         ret = usb_control_msg(vb->usb_dev,
0069             usb_sndctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
0070             VPRBRD_USB_TYPE_OUT, 0x0000, 0x0000, admsg,
0071             sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
0072         if (ret != sizeof(struct vprbrd_adc_msg)) {
0073             dev_err(&iio_dev->dev, "usb send error on adc read\n");
0074             error = -EREMOTEIO;
0075         }
0076 
0077         ret = usb_control_msg(vb->usb_dev,
0078             usb_rcvctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
0079             VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, admsg,
0080             sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
0081 
0082         *val = admsg->val;
0083 
0084         mutex_unlock(&vb->lock);
0085 
0086         if (ret != sizeof(struct vprbrd_adc_msg)) {
0087             dev_err(&iio_dev->dev, "usb recv error on adc read\n");
0088             error = -EREMOTEIO;
0089         }
0090 
0091         if (error)
0092             goto error;
0093 
0094         return IIO_VAL_INT;
0095     default:
0096         error = -EINVAL;
0097         break;
0098     }
0099 error:
0100     return error;
0101 }
0102 
0103 static const struct iio_info vprbrd_adc_iio_info = {
0104     .read_raw = &vprbrd_iio_read_raw,
0105 };
0106 
0107 static int vprbrd_adc_probe(struct platform_device *pdev)
0108 {
0109     struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
0110     struct vprbrd_adc *adc;
0111     struct iio_dev *indio_dev;
0112     int ret;
0113 
0114     /* registering iio */
0115     indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
0116     if (!indio_dev) {
0117         dev_err(&pdev->dev, "failed allocating iio device\n");
0118         return -ENOMEM;
0119     }
0120 
0121     adc = iio_priv(indio_dev);
0122     adc->vb = vb;
0123     indio_dev->name = "viperboard adc";
0124     indio_dev->info = &vprbrd_adc_iio_info;
0125     indio_dev->modes = INDIO_DIRECT_MODE;
0126     indio_dev->channels = vprbrd_adc_iio_channels;
0127     indio_dev->num_channels = ARRAY_SIZE(vprbrd_adc_iio_channels);
0128 
0129     ret = devm_iio_device_register(&pdev->dev, indio_dev);
0130     if (ret) {
0131         dev_err(&pdev->dev, "could not register iio (adc)");
0132         return ret;
0133     }
0134 
0135     return 0;
0136 }
0137 
0138 static struct platform_driver vprbrd_adc_driver = {
0139     .driver = {
0140         .name   = "viperboard-adc",
0141     },
0142     .probe      = vprbrd_adc_probe,
0143 };
0144 
0145 module_platform_driver(vprbrd_adc_driver);
0146 
0147 MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
0148 MODULE_DESCRIPTION("IIO ADC driver for Nano River Techs Viperboard");
0149 MODULE_LICENSE("GPL");
0150 MODULE_ALIAS("platform:viperboard-adc");