blob: 7488e36809c87605e52beec263357e05eca1c4a5 [file] [log] [blame]
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +00001/*
2 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
3 * JZ4740 SoC HWMON driver
4 *
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.
9 *
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.
13 *
14 */
15
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>
Guenter Roeckb25df2b2012-06-02 12:22:08 -070023#include <linux/io.h>
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000024
25#include <linux/completion.h>
26#include <linux/mfd/core.h>
27
28#include <linux/hwmon.h>
29
30struct jz4740_hwmon {
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000031 void __iomem *base;
32
33 int irq;
34
Andres Salomone9300062011-03-01 13:35:48 -080035 const struct mfd_cell *cell;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000036 struct device *hwmon;
37
38 struct completion read_completion;
39
40 struct mutex lock;
41};
42
43static ssize_t jz4740_hwmon_show_name(struct device *dev,
44 struct device_attribute *dev_attr, char *buf)
45{
46 return sprintf(buf, "jz4740\n");
47}
48
49static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
50{
51 struct jz4740_hwmon *hwmon = data;
52
53 complete(&hwmon->read_completion);
54 return IRQ_HANDLED;
55}
56
57static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
58 struct device_attribute *dev_attr, char *buf)
59{
60 struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
61 struct completion *completion = &hwmon->read_completion;
Axel Lin0b57d762011-12-08 08:04:12 -050062 long t;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000063 unsigned long val;
64 int ret;
65
66 mutex_lock(&hwmon->lock);
67
Wolfram Sang16735d02013-11-14 14:32:02 -080068 reinit_completion(completion);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000069
70 enable_irq(hwmon->irq);
71 hwmon->cell->enable(to_platform_device(dev));
72
73 t = wait_for_completion_interruptible_timeout(completion, HZ);
74
75 if (t > 0) {
76 val = readw(hwmon->base) & 0xfff;
77 val = (val * 3300) >> 12;
78 ret = sprintf(buf, "%lu\n", val);
79 } else {
80 ret = t ? t : -ETIMEDOUT;
81 }
82
83 hwmon->cell->disable(to_platform_device(dev));
84 disable_irq(hwmon->irq);
85
86 mutex_unlock(&hwmon->lock);
87
88 return ret;
89}
90
91static DEVICE_ATTR(name, S_IRUGO, jz4740_hwmon_show_name, NULL);
92static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
93
94static struct attribute *jz4740_hwmon_attributes[] = {
95 &dev_attr_name.attr,
96 &dev_attr_in0_input.attr,
97 NULL
98};
99
100static const struct attribute_group jz4740_hwmon_attr_group = {
101 .attrs = jz4740_hwmon_attributes,
102};
103
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500104static int jz4740_hwmon_probe(struct platform_device *pdev)
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000105{
106 int ret;
107 struct jz4740_hwmon *hwmon;
Jingoo Han939a0a32014-02-11 21:10:50 +0900108 struct resource *mem;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000109
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700110 hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
111 if (!hwmon)
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000112 return -ENOMEM;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000113
Andres Salomon6a54ac22011-02-17 19:07:10 -0800114 hwmon->cell = mfd_get_cell(pdev);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000115
116 hwmon->irq = platform_get_irq(pdev, 0);
117 if (hwmon->irq < 0) {
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700118 dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
119 hwmon->irq);
120 return hwmon->irq;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000121 }
122
Jingoo Han939a0a32014-02-11 21:10:50 +0900123 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
124 hwmon->base = devm_ioremap_resource(&pdev->dev, mem);
125 if (IS_ERR(hwmon->base))
126 return PTR_ERR(hwmon->base);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000127
128 init_completion(&hwmon->read_completion);
129 mutex_init(&hwmon->lock);
130
131 platform_set_drvdata(pdev, hwmon);
132
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700133 ret = devm_request_irq(&pdev->dev, hwmon->irq, jz4740_hwmon_irq, 0,
134 pdev->name, hwmon);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000135 if (ret) {
136 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700137 return ret;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000138 }
139 disable_irq(hwmon->irq);
140
141 ret = sysfs_create_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
142 if (ret) {
143 dev_err(&pdev->dev, "Failed to create sysfs group: %d\n", ret);
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700144 return ret;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000145 }
146
147 hwmon->hwmon = hwmon_device_register(&pdev->dev);
148 if (IS_ERR(hwmon->hwmon)) {
149 ret = PTR_ERR(hwmon->hwmon);
150 goto err_remove_file;
151 }
152
153 return 0;
154
155err_remove_file:
156 sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000157 return ret;
158}
159
Bill Pemberton281dfd02012-11-19 13:25:51 -0500160static int jz4740_hwmon_remove(struct platform_device *pdev)
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000161{
162 struct jz4740_hwmon *hwmon = platform_get_drvdata(pdev);
163
164 hwmon_device_unregister(hwmon->hwmon);
165 sysfs_remove_group(&pdev->dev.kobj, &jz4740_hwmon_attr_group);
166
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000167 return 0;
168}
169
Axel Lind6c4f2a2011-12-08 08:33:37 -0500170static struct platform_driver jz4740_hwmon_driver = {
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000171 .probe = jz4740_hwmon_probe,
Bill Pemberton9e5e9b72012-11-19 13:21:20 -0500172 .remove = jz4740_hwmon_remove,
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000173 .driver = {
174 .name = "jz4740-hwmon",
175 .owner = THIS_MODULE,
176 },
177};
178
Axel Lin25a236a2011-11-25 02:31:00 -0500179module_platform_driver(jz4740_hwmon_driver);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000180
181MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
182MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
183MODULE_LICENSE("GPL");
184MODULE_ALIAS("platform:jz4740-hwmon");