/
0125-cape-Added-support-for-IIO-helper-cape.patch
231 lines (224 loc) · 6.71 KB
/
0125-cape-Added-support-for-IIO-helper-cape.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
From ab0813154582de151e9c7d3a9bbdfc6143e78392 Mon Sep 17 00:00:00 2001
From: Matt Ranostay <mranostay@gmail.com>
Date: Tue, 19 Mar 2013 17:06:03 +0000
Subject: [PATCH 125/184] cape: Added support for IIO helper cape
Since we currently can't export the IIO channels from the
ADCs this patch exposes an sysfs entry to read a respective
channel.
Signed-off-by: Matt Ranostay <mranostay@gmail.com>
---
drivers/misc/cape/beaglebone/Kconfig | 7 +
drivers/misc/cape/beaglebone/Makefile | 1 +
drivers/misc/cape/beaglebone/bone-iio-helper.c | 175 +++++++++++++++++++++++++
3 files changed, 183 insertions(+)
create mode 100644 drivers/misc/cape/beaglebone/bone-iio-helper.c
diff --git a/drivers/misc/cape/beaglebone/Kconfig b/drivers/misc/cape/beaglebone/Kconfig
index 9bf5ac0..4937577 100644
--- a/drivers/misc/cape/beaglebone/Kconfig
+++ b/drivers/misc/cape/beaglebone/Kconfig
@@ -9,6 +9,13 @@ config BEAGLEBONE_PINMUX_HELPER
help
Say Y here to include support for the pinmux helper
+config BEAGLEBONE_IIO_HELPER
+ tristate "Beaglebone AM335x TSCADC Helper"
+ depends on ARCH_OMAP2PLUS && OF && MFD_TI_AM335X_TSCADC
+ default n
+ help
+ Say Y here to include support for the IIO helper
+
config CAPE_BEAGLEBONE
tristate "Beaglebone cape support"
depends on ARCH_OMAP2PLUS && OF && I2C
diff --git a/drivers/misc/cape/beaglebone/Makefile b/drivers/misc/cape/beaglebone/Makefile
index 508b931..e32550d 100644
--- a/drivers/misc/cape/beaglebone/Makefile
+++ b/drivers/misc/cape/beaglebone/Makefile
@@ -3,6 +3,7 @@
#
obj-$(CONFIG_BEAGLEBONE_PINMUX_HELPER) += bone-pinmux-helper.o
+obj-$(CONFIG_BEAGLEBONE_IIO_HELPER) += bone-iio-helper.o
obj-$(CONFIG_CAPE_BEAGLEBONE) += capemgr.o
obj-$(CONFIG_CAPE_BEAGLEBONE_GEIGER) += cape-bone-geiger.o
diff --git a/drivers/misc/cape/beaglebone/bone-iio-helper.c b/drivers/misc/cape/beaglebone/bone-iio-helper.c
new file mode 100644
index 0000000..d2c76e1
--- /dev/null
+++ b/drivers/misc/cape/beaglebone/bone-iio-helper.c
@@ -0,0 +1,175 @@
+/*
+ * Industrial IO helper driver
+ *
+ * Copyright (C) 2013 Matt Ranostay <mranostay@gmail.com>
+ *
+ * Based on original work by
+ * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/kernel.h>
+#include <linux/errno.h>
+#include <linux/init.h>
+#include <linux/err.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/math64.h>
+#include <linux/input/ti_am335x_tsc.h>
+#include <linux/platform_data/ti_am335x_adc.h>
+#include <linux/mfd/ti_am335x_tscadc.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/machine.h>
+#include <linux/iio/consumer.h>
+
+static const struct of_device_id bone_iio_helper_of_match[] = {
+ {
+ .compatible = "bone-iio-helper",
+ },
+ { },
+};
+MODULE_DEVICE_TABLE(of, bone_iio_helper_of_match);
+
+struct iio_helper_info {
+ struct platform_device *pdev;
+ const char *vsense_name;
+ unsigned int vsense_scale;
+ struct iio_channel *vsense_channel;
+};
+
+static ssize_t iio_helper_show_mvolts(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct platform_device *pdev = to_platform_device(dev);
+ struct iio_helper_info *info = platform_get_drvdata(pdev);
+ int ret, val;
+ u32 mvolts;
+
+ ret = iio_read_channel_raw(info->vsense_channel, &val);
+ if (ret < 0)
+ return ret;
+
+ /* volts = ((1800 / 4096) * val * scale) = (1.8 * val * scale /4096) */
+ mvolts = div_u64(1800 * info->vsense_scale * (u64)val, 4096 * 100);
+
+ return sprintf(buf, "%d\n", mvolts);
+}
+
+static DEVICE_ATTR(mvolts, S_IRUGO,
+ iio_helper_show_mvolts, NULL);
+
+static int iio_helper_sysfs_register(struct platform_device *pdev)
+{
+ return device_create_file(&pdev->dev, &dev_attr_mvolts);
+}
+
+static int bone_iio_helper_probe(struct platform_device *pdev)
+{
+ struct iio_helper_info *info;
+ struct device_node *pnode = pdev->dev.of_node;
+ int err;
+
+ /* we only support OF */
+ if (pnode == NULL) {
+ dev_err(&pdev->dev, "No platform of_node!\n");
+ return -ENODEV;
+ }
+
+ info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
+ if (info == NULL) {
+ dev_err(&pdev->dev, "Failed to allocate info\n");
+ err = -ENOMEM;
+ goto err_no_mem;
+ }
+ platform_set_drvdata(pdev, info);
+ info->pdev = pdev;
+
+ if (of_property_read_string(pnode, "vsense-name", &info->vsense_name) != 0) {
+ dev_err(&pdev->dev, "Could not read vsense-name property.");
+ err = -EINVAL;
+ goto err_no_vsense;
+ }
+
+ if (of_property_read_u32(pnode, "vsense-scale", &info->vsense_scale) != 0) {
+ dev_err(&pdev->dev, "Could not read vsense-scale property");
+ err = -EINVAL;
+ goto err_no_vsense;
+ }
+
+ if (info->vsense_scale == 0) {
+ dev_err(&pdev->dev, "Invalid vsense-scale property");
+ err = -EINVAL;
+ goto err_no_vsense;
+ }
+
+ info->vsense_channel = iio_channel_get(NULL, info->vsense_name);
+ if (IS_ERR(info->vsense_channel)) {
+ dev_err(&pdev->dev, "Could not get %s analog input\n",
+ info->vsense_name);
+ err = PTR_ERR(info->vsense_channel);
+ goto err_no_vsense;
+ }
+
+ err = iio_helper_sysfs_register(pdev);
+ if (err != 0) {
+ dev_err(&pdev->dev, "unable to register sysfs\n");
+ goto err_no_sysfs;
+ }
+
+ dev_info(&pdev->dev, "ready\n");
+
+ return 0;
+
+err_no_sysfs:
+ /* fall-through */
+err_no_vsense:
+ kfree(info);
+err_no_mem:
+ return err;
+}
+
+
+static int bone_iio_helper_remove(struct platform_device *pdev)
+{
+ struct iio_helper_info *info = platform_get_drvdata(pdev);
+ struct device *dev = &pdev->dev;
+
+ iio_channel_release(info->vsense_channel);
+ device_remove_file(dev, &dev_attr_mvolts);
+ kfree(info->vsense_name);
+ devm_kfree(dev, info);
+
+ return 0;
+}
+
+struct platform_driver bone_iio_helper_driver = {
+ .probe = bone_iio_helper_probe,
+ .remove = bone_iio_helper_remove,
+ .driver = {
+ .name = "bone-iio-helper",
+ .owner = THIS_MODULE,
+ .of_match_table = bone_iio_helper_of_match,
+ },
+};
+
+module_platform_driver(bone_iio_helper_driver);
+
+MODULE_AUTHOR("Matt Ranostay");
+MODULE_DESCRIPTION("Beaglebone IIO helper driver");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:bone-iio-helper");
--
1.8.2.1