1 From 548ab33dc0bd3c824e6002936ae0931158658a19 Mon Sep 17 00:00:00 2001
2 From: Lars-Peter Clausen <lars@metafoo.de>
3 Date: Sat, 19 Jun 2010 18:32:58 +0000
4 Subject: [PATCH] HWMON: Add JZ4740 ADC driver
6 Add support for reading the ADCIN pin of the ADC unit on JZ4740 SoCs.
8 Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
9 Cc: lm-sensors@lm-sensors.org
10 Acked-by: Jean Delvare <khali@linux-fr.org>
11 Cc: linux-mips@linux-mips.org
12 Cc: linux-kernel@vger.kernel.org
13 Patchwork: https://patchwork.linux-mips.org/patch/1425/
14 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
16 drivers/hwmon/Kconfig | 10 ++
17 drivers/hwmon/Makefile | 1 +
18 drivers/hwmon/jz4740-hwmon.c | 230 ++++++++++++++++++++++++++++++++++++++++++
19 3 files changed, 241 insertions(+), 0 deletions(-)
20 create mode 100644 drivers/hwmon/jz4740-hwmon.c
22 --- a/drivers/hwmon/Kconfig
23 +++ b/drivers/hwmon/Kconfig
24 @@ -446,6 +446,16 @@ config SENSORS_IT87
25 This driver can also be built as a module. If so, the module
28 +config SENSORS_JZ4740
29 + tristate "Ingenic JZ4740 SoC ADC driver"
30 + depends on MACH_JZ4740 && MFD_JZ4740_ADC
32 + If you say yes here you get support for reading adc values from the ADCIN
33 + pin on Ingenic JZ4740 SoC based boards.
35 + This driver can also be build as a module. If so, the module will be
36 + called jz4740-hwmon.
39 tristate "National Semiconductor LM63 and LM64"
41 --- a/drivers/hwmon/Makefile
42 +++ b/drivers/hwmon/Makefile
43 @@ -55,6 +55,7 @@ obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb
44 obj-$(CONFIG_SENSORS_IBMAEM) += ibmaem.o
45 obj-$(CONFIG_SENSORS_IBMPEX) += ibmpex.o
46 obj-$(CONFIG_SENSORS_IT87) += it87.o
47 +obj-$(CONFIG_SENSORS_JZ4740) += jz4740-hwmon.o
48 obj-$(CONFIG_SENSORS_K8TEMP) += k8temp.o
49 obj-$(CONFIG_SENSORS_K10TEMP) += k10temp.o
50 obj-$(CONFIG_SENSORS_LIS3LV02D) += lis3lv02d.o hp_accel.o
52 +++ b/drivers/hwmon/jz4740-hwmon.c
55 + * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
56 + * JZ4740 SoC HWMON driver
58 + * This program is free software; you can redistribute it and/or modify it
59 + * under the terms of the GNU General Public License as published by the
60 + * Free Software Foundation; either version 2 of the License, or (at your
61 + * option) any later version.
63 + * You should have received a copy of the GNU General Public License along
64 + * with this program; if not, write to the Free Software Foundation, Inc.,
65 + * 675 Mass Ave, Cambridge, MA 02139, USA.
69 +#include <linux/err.h>
70 +#include <linux/interrupt.h>
71 +#include <linux/kernel.h>
72 +#include <linux/module.h>
73 +#include <linux/mutex.h>
74 +#include <linux/platform_device.h>
75 +#include <linux/slab.h>
77 +#include <linux/completion.h>
78 +#include <linux/mfd/core.h>
80 +#include <linux/hwmon.h>
82 +struct jz4740_hwmon {
83 + struct resource *mem;
88 + struct mfd_cell *cell;
89 + struct device *hwmon;
91 + struct completion read_completion;
96 +static ssize_t jz4740_hwmon_show_name(struct device *dev,
97 + struct device_attribute *dev_attr, char *buf)
99 + return sprintf(buf, "jz4740\n");
102 +static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
104 + struct jz4740_hwmon *hwmon = data;
106 + complete(&hwmon->read_completion);
107 + return IRQ_HANDLED;
110 +static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
111 + struct device_attribute *dev_attr, char *buf)
113 + struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
114 + struct completion *completion = &hwmon->read_completion;
119 + mutex_lock(&hwmon->lock);
121 + INIT_COMPLETION(*completion);
123 + enable_irq(hwmon->irq);
124 + hwmon->cell->enable(to_platform_device(dev));
126 + t = wait_for_completion_interruptible_timeout(completion, HZ);
129 + val = readw(hwmon->base) & 0xfff;
130 + val = (val * 3300) >> 12;
131 + ret = sprintf(buf, "%lu\n", val);
133 + ret = t ? t : -ETIMEDOUT;
136 + hwmon->cell->disable(to_platform_device(dev));
137 + disable_irq(hwmon->irq);
139 + mutex_unlock(&hwmon->lock);
144 +static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL);
145 +static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
147 +static struct attribute *jz4740_hwmon_attributes[] = {
148 + &dev_attr_name.attr,
149 + &dev_attr_in0_input.attr,
153 +static const struct attribute_group jz4740_hwmon_attr_group = {
154 + .attrs = jz4740_hwmon_attributes,
157 +static int __devinit jz4740_hwmon_probe(struct platform_device *pdev)
160 + struct jz4740_hwmon *hwmon;
162 + hwmon = kmalloc(sizeof(*hwmon), GFP_KERNEL);
164 + dev_err(&pdev->dev, "Failed to allocate driver structure\n");
168 + hwmon->cell = pdev->dev.platform_data;
170 + hwmon->irq = platform_get_irq(pdev, 0);
171 + if (hwmon->irq < 0) {
173 + dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
177 + hwmon->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
180 + dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
184 + hwmon->mem = request_mem_region(hwmon->mem->start,
185 + resource_size(hwmon->mem), pdev->name);
188 + dev_err(&pdev->dev, "Failed to request mmio memory region\n");
192 + hwmon->base = ioremap_nocache(hwmon->mem->start,
193 + resource_size(hwmon->mem));
194 + if (!hwmon->base) {
196 + dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
197 + goto err_release_mem_region;
200 + init_completion(&hwmon->read_completion);
201 + mutex_init(&hwmon->lock);
203 + platform_set_drvdata(pdev, hwmon);
205 + ret = request_irq(hwmon->irq, jz4740_hwmon_irq, 0, pdev->name, hwmon);
207 + dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
210 + disable_irq(hwmon->irq);
212 + ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
214 + dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
218 + hwmon->hwmon = hwmon_device_register(&pdev->dev);
219 + if (IS_ERR(hwmon->hwmon)) {
220 + ret = PTR_ERR(hwmon->hwmon);
221 + goto err_remove_file;
227 + sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
229 + free_irq(hwmon->irq, hwmon);
231 + platform_set_drvdata(pdev, NULL);
232 + iounmap(hwmon->base);
233 +err_release_mem_region:
234 + release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
241 +static int __devexit jz4740_hwmon_remove(struct platform_device *pdev)
243 + struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
245 + hwmon_device_unregister(hwmon->hwmon);
246 + sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
248 + free_irq(hwmon->irq, hwmon);
250 + iounmap(hwmon->base);
251 + release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
253 + platform_set_drvdata(pdev, NULL);
259 +struct platform_driver jz4740_hwmon_driver = {
260 + .probe = jz4740_hwmon_probe,
261 + .remove = __devexit_p(jz4740_hwmon_remove),
263 + .name = "jz4740-hwmon",
264 + .owner = THIS_MODULE,
268 +static int __init jz4740_hwmon_init(void)
270 + return platform_driver_register(&jz4740_hwmon_driver);
272 +module_init(jz4740_hwmon_init);
274 +static void __exit jz4740_hwmon_exit(void)
276 + platform_driver_unregister(&jz4740_hwmon_driver);
278 +module_exit(jz4740_hwmon_exit);
280 +MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
281 +MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
282 +MODULE_LICENSE("GPL");
283 +MODULE_ALIAS("platform:jz4740-hwmon");