blob: 3be2e35721cc58f53c1691101553d952633b8126 [file] [log] [blame]
Lars Poeschelffd8a6e2012-11-05 15:48:26 +01001/*
2 * Nano River Technologies viperboard IIO ADC driver
3 *
4 * (C) 2012 by Lemonage GmbH
5 * Author: Lars Poeschel <poeschel@lemonage.de>
6 * All rights reserved.
7 *
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.
12 *
13 */
14
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>
22
23#include <linux/usb.h>
24#include <linux/iio/iio.h>
25
26#include <linux/mfd/viperboard.h>
27
28#define VPRBRD_ADC_CMD_GET 0x00
29
30struct vprbrd_adc_msg {
31 u8 cmd;
32 u8 chan;
33 u8 val;
34} __packed;
35
36struct vprbrd_adc {
37 struct vprbrd *vb;
38};
39
40#define VPRBRD_ADC_CHANNEL(_index) { \
41 .type = IIO_VOLTAGE, \
42 .indexed = 1, \
43 .channel = _index, \
Jonathan Cameron91239722013-02-27 19:07:39 +000044 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
Lars Poeschelffd8a6e2012-11-05 15:48:26 +010045}
46
47static struct iio_chan_spec const vprbrd_adc_iio_channels[] = {
48 VPRBRD_ADC_CHANNEL(0),
49 VPRBRD_ADC_CHANNEL(1),
50 VPRBRD_ADC_CHANNEL(2),
51 VPRBRD_ADC_CHANNEL(3),
52};
53
54static int vprbrd_iio_read_raw(struct iio_dev *iio_dev,
55 struct iio_chan_spec const *chan,
56 int *val,
57 int *val2,
58 long info)
59{
60 int ret, error = 0;
61 struct vprbrd_adc *adc = iio_priv(iio_dev);
62 struct vprbrd *vb = adc->vb;
63 struct vprbrd_adc_msg *admsg = (struct vprbrd_adc_msg *)vb->buf;
64
65 switch (info) {
66 case IIO_CHAN_INFO_RAW:
67 mutex_lock(&vb->lock);
68
69 admsg->cmd = VPRBRD_ADC_CMD_GET;
Peter Meerwaldf227c132013-11-02 19:44:00 +000070 admsg->chan = chan->channel;
Lars Poeschelffd8a6e2012-11-05 15:48:26 +010071 admsg->val = 0x00;
72
73 ret = usb_control_msg(vb->usb_dev,
74 usb_sndctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
75 VPRBRD_USB_TYPE_OUT, 0x0000, 0x0000, admsg,
76 sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
77 if (ret != sizeof(struct vprbrd_adc_msg)) {
78 dev_err(&iio_dev->dev, "usb send error on adc read\n");
79 error = -EREMOTEIO;
80 }
81
82 ret = usb_control_msg(vb->usb_dev,
83 usb_rcvctrlpipe(vb->usb_dev, 0), VPRBRD_USB_REQUEST_ADC,
84 VPRBRD_USB_TYPE_IN, 0x0000, 0x0000, admsg,
85 sizeof(struct vprbrd_adc_msg), VPRBRD_USB_TIMEOUT_MS);
86
87 *val = admsg->val;
88
89 mutex_unlock(&vb->lock);
90
91 if (ret != sizeof(struct vprbrd_adc_msg)) {
92 dev_err(&iio_dev->dev, "usb recv error on adc read\n");
93 error = -EREMOTEIO;
94 }
95
96 if (error)
97 goto error;
98
99 return IIO_VAL_INT;
100 default:
101 error = -EINVAL;
102 break;
103 }
104error:
105 return error;
106}
107
108static const struct iio_info vprbrd_adc_iio_info = {
109 .read_raw = &vprbrd_iio_read_raw,
110 .driver_module = THIS_MODULE,
111};
112
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800113static int vprbrd_adc_probe(struct platform_device *pdev)
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100114{
115 struct vprbrd *vb = dev_get_drvdata(pdev->dev.parent);
116 struct vprbrd_adc *adc;
117 struct iio_dev *indio_dev;
118 int ret;
119
120 /* registering iio */
Sachin Kamat3ef50f82013-07-23 09:58:00 +0100121 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100122 if (!indio_dev) {
123 dev_err(&pdev->dev, "failed allocating iio device\n");
124 return -ENOMEM;
125 }
126
127 adc = iio_priv(indio_dev);
128 adc->vb = vb;
129 indio_dev->name = "viperboard adc";
130 indio_dev->dev.parent = &pdev->dev;
131 indio_dev->info = &vprbrd_adc_iio_info;
132 indio_dev->modes = INDIO_DIRECT_MODE;
133 indio_dev->channels = vprbrd_adc_iio_channels;
134 indio_dev->num_channels = ARRAY_SIZE(vprbrd_adc_iio_channels);
135
Sachin Kamat2191d0f2013-10-29 11:39:00 +0000136 ret = devm_iio_device_register(&pdev->dev, indio_dev);
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100137 if (ret) {
138 dev_err(&pdev->dev, "could not register iio (adc)");
Sachin Kamat3ef50f82013-07-23 09:58:00 +0100139 return ret;
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100140 }
141
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100142 return 0;
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100143}
144
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100145static struct platform_driver vprbrd_adc_driver = {
146 .driver = {
147 .name = "viperboard-adc",
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100148 },
149 .probe = vprbrd_adc_probe,
Lars Poeschelffd8a6e2012-11-05 15:48:26 +0100150};
151
152module_platform_driver(vprbrd_adc_driver);
153
154MODULE_AUTHOR("Lars Poeschel <poeschel@lemonage.de>");
155MODULE_DESCRIPTION("IIO ADC driver for Nano River Techs Viperboard");
156MODULE_LICENSE("GPL");
157MODULE_ALIAS("platform:viperboard-adc");