blob: 0621ee1b3c98f1ebb9a86fb316fb8d0690a6e9e1 [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;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000032 int irq;
Andres Salomone9300062011-03-01 13:35:48 -080033 const struct mfd_cell *cell;
Axel Lin699f2792014-06-29 21:39:11 +080034 struct platform_device *pdev;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000035 struct completion read_completion;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000036 struct mutex lock;
37};
38
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000039static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
40{
41 struct jz4740_hwmon *hwmon = data;
42
43 complete(&hwmon->read_completion);
44 return IRQ_HANDLED;
45}
46
47static ssize_t jz4740_hwmon_read_adcin(struct device *dev,
48 struct device_attribute *dev_attr, char *buf)
49{
50 struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
Axel Lin699f2792014-06-29 21:39:11 +080051 struct platform_device *pdev = hwmon->pdev;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000052 struct completion *completion = &hwmon->read_completion;
Axel Lin0b57d762011-12-08 08:04:12 -050053 long t;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000054 unsigned long val;
55 int ret;
56
57 mutex_lock(&hwmon->lock);
58
Wolfram Sang16735d02013-11-14 14:32:02 -080059 reinit_completion(completion);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000060
61 enable_irq(hwmon->irq);
Axel Lin699f2792014-06-29 21:39:11 +080062 hwmon->cell->enable(pdev);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000063
64 t = wait_for_completion_interruptible_timeout(completion, HZ);
65
66 if (t > 0) {
67 val = readw(hwmon->base) & 0xfff;
68 val = (val * 3300) >> 12;
69 ret = sprintf(buf, "%lu\n", val);
70 } else {
71 ret = t ? t : -ETIMEDOUT;
72 }
73
Axel Lin699f2792014-06-29 21:39:11 +080074 hwmon->cell->disable(pdev);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000075 disable_irq(hwmon->irq);
76
77 mutex_unlock(&hwmon->lock);
78
79 return ret;
80}
81
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000082static DEVICE_ATTR(in0_input, S_IRUGO, jz4740_hwmon_read_adcin, NULL);
83
Axel Lin699f2792014-06-29 21:39:11 +080084static struct attribute *jz4740_attrs[] = {
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000085 &dev_attr_in0_input.attr,
86 NULL
87};
88
Axel Lin699f2792014-06-29 21:39:11 +080089ATTRIBUTE_GROUPS(jz4740);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000090
Bill Pemberton6c931ae2012-11-19 13:22:35 -050091static int jz4740_hwmon_probe(struct platform_device *pdev)
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000092{
93 int ret;
Axel Lin699f2792014-06-29 21:39:11 +080094 struct device *dev = &pdev->dev;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000095 struct jz4740_hwmon *hwmon;
Axel Lin699f2792014-06-29 21:39:11 +080096 struct device *hwmon_dev;
Jingoo Han939a0a32014-02-11 21:10:50 +090097 struct resource *mem;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +000098
Axel Lin699f2792014-06-29 21:39:11 +080099 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700100 if (!hwmon)
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000101 return -ENOMEM;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000102
Andres Salomon6a54ac22011-02-17 19:07:10 -0800103 hwmon->cell = mfd_get_cell(pdev);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000104
105 hwmon->irq = platform_get_irq(pdev, 0);
106 if (hwmon->irq < 0) {
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700107 dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
108 hwmon->irq);
109 return hwmon->irq;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000110 }
111
Jingoo Han939a0a32014-02-11 21:10:50 +0900112 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113 hwmon->base = devm_ioremap_resource(&pdev->dev, mem);
114 if (IS_ERR(hwmon->base))
115 return PTR_ERR(hwmon->base);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000116
Axel Lin699f2792014-06-29 21:39:11 +0800117 hwmon->pdev = pdev;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000118 init_completion(&hwmon->read_completion);
119 mutex_init(&hwmon->lock);
120
Axel Lin699f2792014-06-29 21:39:11 +0800121 ret = devm_request_irq(dev, hwmon->irq, jz4740_hwmon_irq, 0,
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700122 pdev->name, hwmon);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000123 if (ret) {
124 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
Guenter Roeckb25df2b2012-06-02 12:22:08 -0700125 return ret;
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000126 }
127 disable_irq(hwmon->irq);
128
Axel Lin699f2792014-06-29 21:39:11 +0800129 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "jz4740", hwmon,
130 jz4740_groups);
131 return PTR_ERR_OR_ZERO(hwmon_dev);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000132}
133
Axel Lind6c4f2a2011-12-08 08:33:37 -0500134static struct platform_driver jz4740_hwmon_driver = {
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000135 .probe = jz4740_hwmon_probe,
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000136 .driver = {
137 .name = "jz4740-hwmon",
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000138 },
139};
140
Axel Lin25a236a2011-11-25 02:31:00 -0500141module_platform_driver(jz4740_hwmon_driver);
Lars-Peter Clausen7f983ba2010-06-19 18:32:58 +0000142
143MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
144MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
145MODULE_LICENSE("GPL");
146MODULE_ALIAS("platform:jz4740-hwmon");