2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 SoC HWMON driver
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * You should have received a copy of the GNU General Public License along
11 * with this program; if not, write to the Free Software Foundation, Inc.,
12 * 675 Mass Ave, Cambridge, MA 02139, USA.
16 #include <linux/err.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include <linux/completion.h>
25 #include <linux/mfd/core.h>
27 #include <linux/hwmon.h>
35 const struct mfd_cell *cell;
38 struct completion read_completion;
43 static ssize_t jz4740_hwmon_show_name(struct device *dev,
44 struct device_attribute *dev_attr, char *buf)
46 return sprintf(buf, "jz4740\n");
49 static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
51 struct jz4740_hwmon *hwmon = data;
53 complete(&hwmon->read_completion);
57 static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
58 struct device_attribute *dev_attr, char *buf)
60 struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
61 struct completion *completion = &hwmon->read_completion;
66 mutex_lock(&hwmon->lock);
68 INIT_COMPLETION(*completion);
70 enable_irq(hwmon->irq);
71 hwmon->cell->enable(to_platform_device(dev));
73 t = wait_for_completion_interruptible_timeout(completion, HZ);
76 val = readw(hwmon->base) & 0xfff;
77 val = (val * 3300) >> 12;
78 ret = sprintf(buf, "%lu\n", val);
80 ret = t ? t : -ETIMEDOUT;
83 hwmon->cell->disable(to_platform_device(dev));
84 disable_irq(hwmon->irq);
86 mutex_unlock(&hwmon->lock);
91 static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL);
92 static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
94 static struct attribute *jz4740_hwmon_attributes[] = {
96 &dev_attr_in0_input.attr,
100 static const struct attribute_group jz4740_hwmon_attr_group = {
101 .attrs = jz4740_hwmon_attributes,
104 static int __devinit jz4740_hwmon_probe(struct platform_device *pdev)
107 struct jz4740_hwmon *hwmon;
109 hwmon = kmalloc(sizeof(*hwmon), GFP_KERNEL);
111 dev_err(&pdev->dev, "Failed to allocate driver structure\n");
115 hwmon->cell = mfd_get_cell(pdev);
117 hwmon->irq = platform_get_irq(pdev, 0);
118 if (hwmon->irq < 0) {
120 dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
124 hwmon->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
131 hwmon->mem = request_mem_region(hwmon->mem->start,
132 resource_size(hwmon->mem), pdev->name);
135 dev_err(&pdev->dev, "Failed to request mmio memory region\n");
139 hwmon->base = ioremap_nocache(hwmon->mem->start,
140 resource_size(hwmon->mem));
143 dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
144 goto err_release_mem_region;
147 init_completion(&hwmon->read_completion);
148 mutex_init(&hwmon->lock);
150 platform_set_drvdata(pdev, hwmon);
152 ret = request_irq(hwmon->irq, jz4740_hwmon_irq, 0, pdev->name, hwmon);
154 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
157 disable_irq(hwmon->irq);
159 ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
161 dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
165 hwmon->hwmon = hwmon_device_register(&pdev->dev);
166 if (IS_ERR(hwmon->hwmon)) {
167 ret = PTR_ERR(hwmon->hwmon);
168 goto err_remove_file;
174 sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
176 free_irq(hwmon->irq, hwmon);
178 platform_set_drvdata(pdev, NULL);
179 iounmap(hwmon->base);
180 err_release_mem_region:
181 release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
188 static int __devexit jz4740_hwmon_remove(struct platform_device *pdev)
190 struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
192 hwmon_device_unregister(hwmon->hwmon);
193 sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
195 free_irq(hwmon->irq, hwmon);
197 iounmap(hwmon->base);
198 release_mem_region(hwmon->mem->start, resource_size(hwmon->mem));
200 platform_set_drvdata(pdev, NULL);
206 static struct platform_driver jz4740_hwmon_driver = {
207 .probe = jz4740_hwmon_probe,
208 .remove = __devexit_p(jz4740_hwmon_remove),
210 .name = "jz4740-hwmon",
211 .owner = THIS_MODULE,
215 module_platform_driver(jz4740_hwmon_driver);
217 MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
218 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
219 MODULE_LICENSE("GPL");
220 MODULE_ALIAS("platform:jz4740-hwmon");