Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / jz4740-hwmon.c
blob2d40a2e771d75db582e4eed9d497b47c5a4e9ab2
1 /*
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>
23 #include <linux/io.h>
25 #include <linux/completion.h>
26 #include <linux/mfd/core.h>
28 #include <linux/hwmon.h>
30 struct jz4740_hwmon {
31 void __iomem *base;
32 int irq;
33 const struct mfd_cell *cell;
34 struct platform_device *pdev;
35 struct completion read_completion;
36 struct mutex lock;
39 static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
41 struct jz4740_hwmon *hwmon = data;
43 complete(&hwmon->read_completion);
44 return IRQ_HANDLED;
47 static ssize_t in0_input_show(struct device *dev,
48 struct device_attribute *dev_attr, char *buf)
50 struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
51 struct platform_device *pdev = hwmon->pdev;
52 struct completion *completion = &hwmon->read_completion;
53 long t;
54 unsigned long val;
55 int ret;
57 mutex_lock(&hwmon->lock);
59 reinit_completion(completion);
61 enable_irq(hwmon->irq);
62 hwmon->cell->enable(pdev);
64 t = wait_for_completion_interruptible_timeout(completion, HZ);
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;
74 hwmon->cell->disable(pdev);
75 disable_irq(hwmon->irq);
77 mutex_unlock(&hwmon->lock);
79 return ret;
82 static DEVICE_ATTR_RO(in0_input);
84 static struct attribute *jz4740_attrs[] = {
85 &dev_attr_in0_input.attr,
86 NULL
89 ATTRIBUTE_GROUPS(jz4740);
91 static int jz4740_hwmon_probe(struct platform_device *pdev)
93 int ret;
94 struct device *dev = &pdev->dev;
95 struct jz4740_hwmon *hwmon;
96 struct device *hwmon_dev;
97 struct resource *mem;
99 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
100 if (!hwmon)
101 return -ENOMEM;
103 hwmon->cell = mfd_get_cell(pdev);
105 hwmon->irq = platform_get_irq(pdev, 0);
106 if (hwmon->irq < 0) {
107 dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
108 hwmon->irq);
109 return hwmon->irq;
112 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);
117 hwmon->pdev = pdev;
118 init_completion(&hwmon->read_completion);
119 mutex_init(&hwmon->lock);
121 ret = devm_request_irq(dev, hwmon->irq, jz4740_hwmon_irq, 0,
122 pdev->name, hwmon);
123 if (ret) {
124 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
125 return ret;
127 disable_irq(hwmon->irq);
129 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "jz4740", hwmon,
130 jz4740_groups);
131 return PTR_ERR_OR_ZERO(hwmon_dev);
134 static struct platform_driver jz4740_hwmon_driver = {
135 .probe = jz4740_hwmon_probe,
136 .driver = {
137 .name = "jz4740-hwmon",
141 module_platform_driver(jz4740_hwmon_driver);
143 MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
144 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
145 MODULE_LICENSE("GPL");
146 MODULE_ALIAS("platform:jz4740-hwmon");