linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lars-Peter Clausen <lars@metafoo.de>
To: Ralf Baechle <ralf@linux-mips.org>
Cc: linux-mips@linux-mips.org, linux-kernel@vger.kernel.org,
	Lars-Peter Clausen <lars@metafoo.de>,
	lm-sensors@lm-sensors.org
Subject: [PATCH v3] hwmon: Add JZ4740 ADC driver
Date: Sat, 19 Jun 2010 16:47:00 +0200	[thread overview]
Message-ID: <1276958820-8862-1-git-send-email-lars@metafoo.de> (raw)
In-Reply-To: <1276924111-11158-24-git-send-email-lars@metafoo.de>

This patch adds support for reading the ADCIN pin of ADC unit on JZ4740 SoCs.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Cc: lm-sensors@lm-sensors.org

--
Changes since v1
- Move ADC core access synchronizing from the HWMON driver to a MFD driver. The
  ADC driver now only reads the adcin value.
Changes since v2
- Add name sysfs attribute
- Report adcin in value in millivolts
---
 drivers/hwmon/Kconfig        |   11 ++
 drivers/hwmon/Makefile       |    1 +
 drivers/hwmon/jz4740-hwmon.c |  224 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 236 insertions(+), 0 deletions(-)
 create mode 100644 drivers/hwmon/jz4740-hwmon.c

diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index 569082c..51fc2f6 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -446,6 +446,17 @@ config SENSORS_IT87
 	  This driver can also be built as a module.  If so, the module
 	  will be called it87.
 
+config SENSORS_JZ4740
+	tristate "Ingenic JZ4740 SoC ADC driver"
+	depends on MACH_JZ4740
+    help
+      If you say yes here you get support for the Ingenic JZ4740 SoC ADC core.
+      It is required for the JZ4740 battery and touchscreen driver and is used
+      to synchronize access to the adc module between those two.
+
+      This driver can also be build as a module. If so, the module will be
+      called jz4740-adc.
+
 config SENSORS_LM63
 	tristate "National Semiconductor LM63 and LM64"
 	depends on I2C
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index bca0d45..dffbdff 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -55,6 +55,7 @@ obj-$(CONFIG_SENSORS_I5K_AMB)	+= i5k_amb.o
 obj-$(CONFIG_SENSORS_IBMAEM)	+= ibmaem.o
 obj-$(CONFIG_SENSORS_IBMPEX)	+= ibmpex.o
 obj-$(CONFIG_SENSORS_IT87)	+= it87.o
+obj-$(CONFIG_SENSORS_JZ4740)	+= jz4740-hwmon.o
 obj-$(CONFIG_SENSORS_K8TEMP)	+= k8temp.o
 obj-$(CONFIG_SENSORS_K10TEMP)	+= k10temp.o
 obj-$(CONFIG_SENSORS_LIS3LV02D) += lis3lv02d.o hp_accel.o
diff --git a/drivers/hwmon/jz4740-hwmon.c b/drivers/hwmon/jz4740-hwmon.c
new file mode 100644
index 0000000..a448d78
--- /dev/null
+++ b/drivers/hwmon/jz4740-hwmon.c
@@ -0,0 +1,224 @@
+/*
+ * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
+ * JZ4740 SoC HWMON driver
+ *
+ * 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.
+ *
+ * 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/err.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+
+#include <linux/mfd/core.h>
+
+#include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
+
+struct jz4740_hwmon {
+	struct resource *mem;
+	void __iomem *base;
+
+	int irq;
+
+	struct mfd_cell *cell;
+	struct device *hwmon;
+
+	struct completion read_completion;
+
+	struct mutex lock;
+};
+
+static ssize_t jz4740_hwmon_show_name(struct device *dev,
+	struct device_attribute *dev_attr, char *buf)
+{
+	return sprintf(buf, "jz4740\n");
+}
+
+static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
+{
+	struct jz4740_hwmon *hwmon = data;
+
+	complete(&hwmon->read_completion);
+	return IRQ_HANDLED;
+}
+
+static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
+	struct device_attribute *dev_attr, char *buf)
+{
+	struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
+	unsigned long t;
+	unsigned long val;
+	int ret;
+
+	mutex_lock(&hwmon->lock);
+
+	INIT_COMPLETION(hwmon->read_completion);
+
+	enable_irq(hwmon->irq);
+	hwmon->cell->enable(to_platform_device(dev));
+
+	t = wait_for_completion_interruptible_timeout(&hwmon->read_completion, HZ);
+
+	if (t > 0) {
+		val = readw(hwmon->base) & 0xfff;
+		val = (val * 3300) >> 12;
+		ret = sprintf(buf, "%lu\n", val);
+	} else {
+		ret = t ? t : -ETIMEDOUT;
+	}
+
+	hwmon->cell->disable(to_platform_device(dev));
+	disable_irq(hwmon->irq);
+
+	mutex_unlock(&hwmon->lock);
+
+	return ret;
+}
+
+static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL);
+static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL, 0);
+
+static struct attribute jz4740_hwmon_attributes[] = {
+	&dev_attr_name.attr,
+	&sensor_dev_attr_in0_input.dev_attr.attr,
+	NULL
+};
+
+static const struct attribute_group jz4740_hwmon_attr_group = {
+	.attrs = jz4740_hwmon_attributes,
+};
+
+static int __devinit jz4740_hwmon_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct jz4740_hwmon *hwmon;
+
+	hwmon = kmalloc(sizeof(*hwmon), GFP_KERNEL);
+
+	hwmon->cell = pdev->dev.platform_data;
+
+	hwmon->irq = platform_get_irq(pdev, 0);
+	if (hwmon->irq < 0) {
+		ret = hwmon->irq;
+		dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
+		goto err_free;
+	}
+
+	hwmon->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!hwmon->mem) {
+		ret = -ENOENT;
+		dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
+		goto err_free;
+	}
+
+	hwmon->mem = request_mem_region(hwmon->mem->start,
+					resource_size(hwmon->mem), pdev->name);
+	if (!hwmon->mem) {
+		ret = -EBUSY;
+		dev_err(&pdev->dev, "Failed to request mmio memory region\n");
+		goto err_free;
+	}
+
+	hwmon->base = ioremap_nocache(hwmon->mem->start, resource_size(hwmon->mem));
+	if (!hwmon->base) {
+		ret = -EBUSY;
+		dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
+		goto err_release_mem_region;
+	}
+
+	init_completion(&hwmon->read_completion);
+	mutex_init(&hwmon->lock);
+
+	platform_set_drvdata(pdev, hwmon);
+
+	ret = request_irq(hwmon->irq, jz4740_hwmon_irq, 0, pdev->name, hwmon);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
+		goto err_iounmap;
+	}
+	disable_irq(hwmon->irq);
+
+	ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
+	if (ret) {
+		dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
+		goto err_free_irq;
+	}
+
+	hwmon->hwmon = hwmon_device_register(&pdev->dev);
+	if (IS_ERR(hwmon->hwmon)) {
+		ret = PTR_ERR(hwmon->hwmon);
+		goto err_remove_file;
+	}
+
+	return 0;
+
+err_remove_file:
+	sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
+err_free_irq:
+	free_irq(hwmon->irq, hwmon);
+err_iounmap:
+	platform_set_drvdata(pdev, NULL);
+	iounmap(hwmon->base);
+err_release_mem_region:
+	release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
+err_free:
+	kfree(hwmon);
+
+	return ret;
+}
+
+static int __devexit jz4740_hwmon_remove(struct platform_device *pdev)
+{
+	struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
+
+	hwmon_device_unregister(hwmon->hwmon);
+	sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
+
+	free_irq(hwmon->irq, hwmon);
+
+	iounmap(hwmon->base);
+	release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
+
+	platform_set_drvdata(pdev, NULL);
+	kfree(hwmon);
+
+	return 0;
+}
+
+struct platform_driver jz4740_hwmon_driver = {
+	.probe	= jz4740_hwmon_probe,
+	.remove = __devexit_p(jz4740_hwmon_remove),
+	.driver = {
+		.name = "jz4740-hwmon",
+		.owner = THIS_MODULE,
+	},
+};
+
+static int __init jz4740_hwmon_init(void)
+{
+	return platform_driver_register(&jz4740_hwmon_driver);
+}
+module_init(jz4740_hwmon_init);
+
+static void __exit jz4740_hwmon_exit(void)
+{
+	platform_driver_unregister(&jz4740_hwmon_driver);
+}
+module_exit(jz4740_hwmon_exit);
+
+MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
+MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:jz4740-hwmon");
-- 
1.5.6.5


  parent reply	other threads:[~2010-06-19 14:47 UTC|newest]

Thread overview: 111+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-19  5:08 [PATCH v2 00/26] Add support for the Ingenic JZ4740 System-on-a-Chip Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 01/26] MIPS: Add base support for " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 02/26] MIPS: jz4740: Add IRQ handler code Lars-Peter Clausen
2010-07-17 12:08   ` [PATCH v3] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 03/26] MIPS: JZ4740: Add clock API support Lars-Peter Clausen
2010-06-28  1:24   ` [PATCH v3 " Lars-Peter Clausen
2010-07-17 12:10     ` [PATCH v4] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 04/26] MIPS: JZ4740: Add timer support Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 05/26] MIPS: JZ4740: Add clocksource/clockevent support Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 06/26] MIPS: JZ4740: Add power-management and system reset support Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 07/26] MIPS: JZ4740: Add setup code Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 08/26] MIPS: JZ4740: Add gpio support Lars-Peter Clausen
2010-07-17 12:11   ` [PATCH v3] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 09/26] MIPS: JZ4740: Add DMA support Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 10/26] MIPS: JZ4740: Add PWM support Lars-Peter Clausen
2010-06-28  1:23   ` [PATCH v3 " Lars-Peter Clausen
2010-07-17 12:12     ` [PATCH v4] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 11/26] MIPS: JZ4740: Add serial support Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 12/26] MIPS: JZ4740: Add prom support Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 13/26] MIPS: JZ4740: Add platform devices Lars-Peter Clausen
2010-07-17 12:13   ` [PATCH v3] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 14/26] MIPS: JZ4740: Add Kbuild files Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 15/26] RTC: Add JZ4740 RTC driver Lars-Peter Clausen
2010-06-19 10:43   ` Marek Vasut
2010-06-19 13:05     ` Lars-Peter Clausen
2010-06-19 13:37       ` Wan ZongShun
2010-06-19 13:53         ` Lars-Peter Clausen
2010-06-19 14:36           ` Wan ZongShun
2010-06-19 14:04       ` Marek Vasut
2010-06-19 17:42         ` Lars-Peter Clausen
2010-06-19 17:53           ` Geert Uytterhoeven
2010-06-19 19:29   ` [PATCH v3] " Lars-Peter Clausen
2010-06-20  1:13     ` [rtc-linux] " Wan ZongShun
2010-06-20  1:23       ` Lars-Peter Clausen
2010-06-20  1:30         ` Wan ZongShun
2010-06-22  5:53     ` Alessandro Zummo
2010-06-19  5:08 ` [PATCH v2 16/26] fbdev: Add JZ4740 framebuffer driver Lars-Peter Clausen
2010-07-04 22:27   ` Lars-Peter Clausen
2010-07-07 23:41     ` Andrew Morton
2010-07-08 13:28       ` Lars-Peter Clausen
2010-07-08 16:46         ` Andrew Morton
2010-07-09  1:26     ` Jaya Kumar
2010-07-09 15:31       ` Lars-Peter Clausen
2010-07-17 12:14   ` [PATCH v3] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 17/26] MTD: Nand: Add JZ4740 NAND driver Lars-Peter Clausen
2010-07-04 22:35   ` [PATCH v2 17/26] MTD: Nand: Add JZ4740 NAND Lars-Peter Clausen
2010-07-08  6:06   ` [PATCH v2 17/26] MTD: Nand: Add JZ4740 NAND driver Artem Bityutskiy
2010-07-08 13:20     ` Lars-Peter Clausen
2010-07-08 13:19       ` Artem Bityutskiy
2010-07-08 14:02         ` Lars-Peter Clausen
2010-07-08 14:14           ` Artem Bityutskiy
2010-07-17 12:15   ` [PATCH v3] " Lars-Peter Clausen
2010-07-18 16:54     ` Artem Bityutskiy
2010-07-18 17:02       ` Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 18/26] MMC: Add JZ4740 mmc driver Lars-Peter Clausen
2010-06-19 14:46   ` Matt Fleming
2010-06-19 15:29     ` Lars-Peter Clausen
2010-06-28  1:20   ` [PATCH v3] " Lars-Peter Clausen
2010-06-29 20:17     ` Matt Fleming
2010-07-01 15:47       ` Lars-Peter Clausen
2010-06-30 20:55     ` Andrew Morton
2010-07-01 15:45       ` Lars-Peter Clausen
2010-07-12 21:33     ` [PATCH v4] " Lars-Peter Clausen
2010-07-12 21:41       ` Randy Dunlap
2010-07-12 22:07         ` Lars-Peter Clausen
2010-07-12 22:20       ` [PATCH v5] " Lars-Peter Clausen
2010-07-12 22:45         ` Joe Perches
2010-07-12 23:45           ` Lars-Peter Clausen
2010-07-15 21:06         ` [PATCH v6] " Lars-Peter Clausen
2010-07-15 21:16           ` Andrew Morton
2010-07-15 21:37             ` Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 19/26] USB: Add JZ4740 ohci support Lars-Peter Clausen
2010-06-19 17:17   ` Greg KH
2010-06-19  5:08 ` [PATCH v2 20/26] alsa: ASoC: Add JZ4740 codec driver Lars-Peter Clausen
2010-06-19 14:49   ` [PATCH v3] " Lars-Peter Clausen
2010-06-20 13:11     ` Mark Brown
2010-06-21 22:46     ` [PATCH v4] " Lars-Peter Clausen
2010-06-22 10:12       ` Liam Girdwood
2010-06-22 23:12       ` Mark Brown
2010-06-19  5:08 ` [PATCH v2 21/26] alsa: ASoC: Add JZ4740 ASoC support Lars-Peter Clausen
2010-06-19 14:50   ` [PATCH v3] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 22/26] MFD: Add JZ4740 ADC driver Lars-Peter Clausen
2010-07-04 22:47   ` Lars-Peter Clausen
2010-07-05 14:53   ` Samuel Ortiz
2010-07-05 15:43     ` Lars-Peter Clausen
2010-07-05 15:53       ` Samuel Ortiz
2010-07-12  1:48   ` [PATCH v3] " Lars-Peter Clausen
2010-07-14  9:19     ` Samuel Ortiz
2010-06-19  5:08 ` [PATCH v2 23/26] hwmon: " Lars-Peter Clausen
2010-06-19  8:36   ` [lm-sensors] " Jean Delvare
2010-06-19 12:58     ` Lars-Peter Clausen
2010-06-19 14:47   ` Lars-Peter Clausen [this message]
2010-06-19 16:24     ` [lm-sensors] [PATCH v3] " Jean Delvare
2010-06-19 17:59       ` Lars-Peter Clausen
2010-06-19 19:32     ` [PATCH v4] " Lars-Peter Clausen
2010-06-20  6:32       ` [lm-sensors] " Jean Delvare
2010-06-19  5:08 ` [PATCH v2 24/26] power: Add JZ4740 battery driver Lars-Peter Clausen
2010-06-27  1:58   ` Lars-Peter Clausen
2010-06-28 11:43     ` Anton Vorontsov
2010-06-19  5:08 ` [PATCH v2 25/26] MIPS: JZ4740: Add qi_lb60 board support Lars-Peter Clausen
2010-07-17 12:16   ` [PATCH v3] " Lars-Peter Clausen
2010-06-19  5:08 ` [PATCH v2 26/26] alsa: ASoC: JZ4740: Add qi_lb60 board driver Lars-Peter Clausen
2010-06-19 14:52   ` [PATCH v3] " Lars-Peter Clausen
2010-06-20  9:26 ` [PATCH v2 00/26] Add support for the Ingenic JZ4740 System-on-a-Chip Thomas Bogendoerfer
2010-06-20 14:31   ` Lars-Peter Clausen
2010-06-20 16:34     ` Thomas Bogendoerfer
2010-06-20 16:49       ` Lars-Peter Clausen
2010-06-20 17:01         ` Thomas Bogendoerfer
2010-06-20 17:57           ` Florian Fainelli
2010-06-20 18:30             ` Lars-Peter Clausen
2010-06-21  2:56   ` Xiangfu Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1276958820-8862-1-git-send-email-lars@metafoo.de \
    --to=lars@metafoo.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@linux-mips.org \
    --cc=lm-sensors@lm-sensors.org \
    --cc=ralf@linux-mips.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).