summaryrefslogtreecommitdiffstats
path: root/configs/platform-energymicro-efm32gg-dk3750/patches/linux-3.12-rc4/0017-hwmon-efm32-adc-new-driver.patch
blob: 0e7b231d9337b8e4162900506b3207aefecc58a0 (plain)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= <u.kleine-koenig@pengutronix.de>
Date: Thu, 9 Feb 2012 22:35:24 +0100
Subject: [PATCH] hwmon/efm32-adc: new driver
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/hwmon/Kconfig     |  10 ++
 drivers/hwmon/Makefile    |   1 +
 drivers/hwmon/efm32-adc.c | 321 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 332 insertions(+)
 create mode 100644 drivers/hwmon/efm32-adc.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index b3ab9d4..3788c4b 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -382,6 +382,16 @@ config SENSORS_DA9055
 	  This driver can also be built as a module.  If so, the module
 	  will be called da9055-hwmon.
 
+config SENSORS_EFM32_ADC
+	tristate "Energy Micro EFM32 ADC"
+	depends on OF && ARM && (ARCH_EFM32 || COMPILE_TEST)
+	help
+	  If you say yes here you get support for Energy Micro's ADC
+	  build into their EFM32 SoCs
+
+	  This driver can also be built as a module. If so, the module
+	  will be called efm32-adc.
+
 config SENSORS_I5K_AMB
 	tristate "FB-DIMM AMB temperature sensor on Intel 5000 series chipsets"
 	depends on PCI
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index ec7cde0..442f586 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -51,6 +51,7 @@ obj-$(CONFIG_SENSORS_DA9055)+= da9055-hwmon.o
 obj-$(CONFIG_SENSORS_DME1737)	+= dme1737.o
 obj-$(CONFIG_SENSORS_DS620)	+= ds620.o
 obj-$(CONFIG_SENSORS_DS1621)	+= ds1621.o
+obj-$(CONFIG_SENSORS_EFM32_ADC)	+= efm32-adc.o
 obj-$(CONFIG_SENSORS_EMC1403)	+= emc1403.o
 obj-$(CONFIG_SENSORS_EMC2103)	+= emc2103.o
 obj-$(CONFIG_SENSORS_EMC6W201)	+= emc6w201.o
diff --git a/drivers/hwmon/efm32-adc.c b/drivers/hwmon/efm32-adc.c
new file mode 100644
index 0000000..fa4bd21
--- /dev/null
+++ b/drivers/hwmon/efm32-adc.c
@@ -0,0 +1,321 @@
+#define DEBUG
+/*
+ * Energy Micro EFM32 adc
+ *
+ * Copyright (C) 2012 Uwe Kleine-Koenig for Pengutronix
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ */
+#include <linux/platform_device.h>
+#include <linux/hwmon-sysfs.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/hwmon.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/clk.h>
+#include <linux/init.h>
+#include <linux/err.h>
+
+#define DRIVER_NAME "efm32-adc"
+
+#define ADC_CTRL		0x000
+
+#define ADC_CMD			0x004
+#define ADC_CMD_SINGLESTART		0x00000001
+#define ADC_CMD_SINGLESTOP		0x00000002
+#define ADC_CMD_SCANSTART		0x00000004
+#define ADC_CMD_SCANSTOP		0x00000008
+
+#define ADC_STATUS		0x008
+#define ADC_STATUS_SINGLEDV		0x00010000
+#define ADC_SINGLECTRL		0x00c
+#define ADC_SINGLEDATA		0x024
+
+#define ADC_IEN			0x014
+#define ADC_IF			0x018
+#define ADC_IFC			0x020
+#define ADC_IF_SINGLE			0x00000001
+
+struct efm32_adc_ddata {
+	struct device *hwmondev;
+	void __iomem *base;
+	struct clk *clk;
+	unsigned int irq;
+	spinlock_t lock;
+	unsigned int busy;
+};
+
+static void efm32_adc_write32(struct efm32_adc_ddata *ddata,
+		u32 value, unsigned offset)
+{
+	writel_relaxed(value, ddata->base + offset);
+}
+
+static u32 efm32_adc_read32(struct efm32_adc_ddata *ddata, unsigned offset)
+{
+	return readl_relaxed(ddata->base + offset);
+}
+
+static ssize_t efm32_adc_show_name(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	return sprintf(buf, "efm32\n");
+}
+
+struct efm32_adc_irqdata {
+	struct efm32_adc_ddata *ddata;
+	struct completion done;
+};
+
+static irqreturn_t efm32_adc_irq(int irq, void *data)
+{
+	struct efm32_adc_irqdata *irqdata = data;
+	u32 iflag = efm32_adc_read32(irqdata->ddata, ADC_IF);
+
+	if (iflag & ADC_IF_SINGLE) {
+		efm32_adc_write32(irqdata->ddata, ADC_IF_SINGLE, ADC_IFC);
+		complete(&irqdata->done);
+		return IRQ_HANDLED;
+	}
+
+	return IRQ_NONE;
+}
+
+static int efm32_adc_read_single(struct device *dev,
+		struct device_attribute *devattr, unsigned int *val)
+{
+	struct platform_device *pdev = to_platform_device(dev);
+	struct efm32_adc_ddata *ddata = platform_get_drvdata(pdev);
+	int ret;
+	struct efm32_adc_irqdata irqdata = { .ddata = ddata, };
+	u32 status;
+	unsigned long freq;
+
+	ret = clk_prepare(ddata->clk);
+	if (ret < 0) {
+		dev_dbg(ddata->hwmondev, "failed to enable clk (%d)\n", ret);
+		return ret;
+	}
+
+	spin_lock_irq(&ddata->lock);
+	if (ddata->busy) {
+		dev_dbg(ddata->hwmondev, "busy\n");
+		ret = -EBUSY;
+		goto out_unlock;
+	}
+
+	init_completion(&irqdata.done);
+
+	ret = clk_enable(ddata->clk);
+	if (ret < 0) {
+		dev_dbg(ddata->hwmondev, "failed to enable clk (%d)\n", ret);
+		goto out_unlock;
+	}
+	freq = clk_get_rate(ddata->clk);
+
+	efm32_adc_write32(ddata,
+			ADC_CMD_SINGLESTOP | ADC_CMD_SCANSTOP, ADC_CMD);
+	efm32_adc_write32(ddata, ((freq - 1) / 1000000) << 16 |
+			((freq / 400000) - 1) << 8, ADC_CTRL);
+	efm32_adc_write32(ddata, 0x800, ADC_SINGLECTRL);
+	efm32_adc_write32(ddata, ADC_IF_SINGLE, ADC_IFC);
+	efm32_adc_write32(ddata, ADC_CMD_SINGLESTART, ADC_CMD);
+
+	ret = request_irq(ddata->irq, efm32_adc_irq, 0, DRIVER_NAME, &irqdata);
+	if (ret) {
+		efm32_adc_write32(ddata, ADC_CMD_SINGLESTOP, ADC_CMD);
+		goto out_clkoff;
+	}
+
+	efm32_adc_write32(ddata, ADC_IF_SINGLE, ADC_IEN);
+
+	ddata->busy = 1;
+
+	spin_unlock_irq(&ddata->lock);
+
+	ret = wait_for_completion_interruptible_timeout(&irqdata.done, 2 * HZ);
+
+	spin_lock_irq(&ddata->lock);
+
+	efm32_adc_write32(ddata, 0, ADC_IEN);
+	free_irq(ddata->irq, &irqdata);
+
+	if (ret < 0)
+		goto done_out_unlock;
+
+	status = efm32_adc_read32(ddata, ADC_STATUS);
+	if (status & ADC_STATUS_SINGLEDV) {
+		*val = efm32_adc_read32(ddata, ADC_SINGLEDATA);
+		ret = 0;
+	} else
+		ret = -ETIMEDOUT;
+
+done_out_unlock:
+	ddata->busy = 0;
+out_clkoff:
+	clk_disable(ddata->clk);
+out_unlock:
+	spin_unlock_irq(&ddata->lock);
+
+	clk_unprepare(ddata->clk);
+
+	return ret;
+}
+
+static ssize_t efm32_adc_read_chan(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	unsigned int val;
+	int ret = efm32_adc_read_single(dev, devattr, &val);
+
+	if (ret)
+		return ret;
+
+	return sprintf(buf, "%u\n", val);
+}
+
+static ssize_t efm32_adc_read_temp(struct device *dev,
+		struct device_attribute *devattr, char *buf)
+{
+	unsigned int val;
+	int ret = efm32_adc_read_single(dev, devattr, &val);
+	/*
+	 * XXX: get these via pdata or read them from the device information
+	 * registers
+	 */
+	unsigned temp0 = 0x19 * 1000;
+	unsigned adc0 = 0x910;
+
+	if (ret)
+		return ret;
+
+	val = temp0 + DIV_ROUND_CLOSEST((adc0 - val) * 10000, 63);
+
+	return sprintf(buf, "%u\n", val);
+}
+
+static DEVICE_ATTR(name, S_IRUGO, efm32_adc_show_name, NULL);
+static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, efm32_adc_read_chan, NULL, 8);
+static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, efm32_adc_read_temp, NULL, 8);
+
+static struct attribute *efm32_adc_attr[] = {
+	&dev_attr_name.attr,
+	&sensor_dev_attr_in8_input.dev_attr.attr,
+	&sensor_dev_attr_temp1_input.dev_attr.attr,
+	NULL
+};
+
+static const struct attribute_group efm32_adc_group = {
+	.attrs = efm32_adc_attr,
+};
+
+static int efm32_adc_probe(struct platform_device *pdev)
+{
+	struct efm32_adc_ddata *ddata;
+	struct resource *res;
+	int ret;
+
+	ddata = kzalloc(sizeof(*ddata), GFP_KERNEL);
+	if (!ddata)
+		return -ENOMEM;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res) {
+		ret = -ENXIO;
+		dev_dbg(&pdev->dev, "failed to determine base address\n");
+		goto err_get_base;
+	}
+
+	ret = platform_get_irq(pdev, 0);
+	if (ret <= 0) {
+		ret = -ENXIO;
+		dev_dbg(&pdev->dev, "failed to determine irq\n");
+		goto err_get_irq;
+	}
+	ddata->irq = ret;
+
+	ddata->base = ioremap(res->start, resource_size(res));
+	if (!ddata->base) {
+		ret = -ENOMEM;
+		dev_dbg(&pdev->dev, "failed to remap\n");
+		goto err_ioremap;
+	}
+
+	ddata->clk = clk_get(&pdev->dev, NULL);
+	if (IS_ERR(ddata->clk)) {
+		ret = PTR_ERR(ddata->clk);
+		dev_dbg(&pdev->dev, "failed to get clock\n");
+		goto err_clk_get;
+	}
+
+	platform_set_drvdata(pdev, ddata);
+	spin_lock_init(&ddata->lock);
+
+	ret = sysfs_create_group(&pdev->dev.kobj, &efm32_adc_group);
+	if (ret)
+		goto err_create_group;
+
+	ddata->hwmondev = hwmon_device_register(&pdev->dev);
+	if (IS_ERR(ddata->hwmondev)) {
+		ret = PTR_ERR(ddata->hwmondev);
+
+		sysfs_remove_group(&pdev->dev.kobj, &efm32_adc_group);
+err_create_group:
+
+		platform_set_drvdata(pdev, NULL);
+
+		clk_put(ddata->clk);
+err_clk_get:
+
+		iounmap(ddata->base);
+err_ioremap:
+err_get_irq:
+err_get_base:
+		kfree(ddata);
+	}
+
+	return ret;
+}
+
+static int efm32_adc_remove(struct platform_device *pdev)
+{
+	struct efm32_adc_ddata *ddata = platform_get_drvdata(pdev);
+
+	hwmon_device_unregister(ddata->hwmondev);
+	sysfs_remove_group(&pdev->dev.kobj, &efm32_adc_group);
+	platform_set_drvdata(pdev, NULL);
+	clk_put(ddata->clk);
+	iounmap(ddata->base);
+	kfree(ddata);
+
+	return 0;
+}
+
+static const struct of_device_id efm32_adc_dt_ids[] = {
+	{
+		.compatible = "efm32,adc",
+	}, {
+		/* sentinel */
+	}
+};
+MODULE_DEVICE_TABLE(of, efm32_adc_dt_ids);
+
+static struct platform_driver efm32_adc_driver = {
+	.probe = efm32_adc_probe,
+	.remove = efm32_adc_remove,
+	.driver = {
+		.name = DRIVER_NAME,
+		.owner = THIS_MODULE,
+		.of_match_table = efm32_adc_dt_ids,
+	},
+};
+module_platform_driver(efm32_adc_driver);
+
+MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
+MODULE_DESCRIPTION("EFM32 ADC driver");
+MODULE_LICENSE("GPL v2");
+MODULE_ALIAS("platform:" DRIVER_NAME);