Merge tag 'omap-for-v5.2/fixes-rc4' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-microblaze.git] / drivers / hwmon / jz4740-hwmon.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
4  * JZ4740 SoC HWMON driver
5  */
6
7 #include <linux/err.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/io.h>
15
16 #include <linux/completion.h>
17 #include <linux/mfd/core.h>
18
19 #include <linux/hwmon.h>
20
21 struct jz4740_hwmon {
22         void __iomem *base;
23         int irq;
24         const struct mfd_cell *cell;
25         struct platform_device *pdev;
26         struct completion read_completion;
27         struct mutex lock;
28 };
29
30 static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
31 {
32         struct jz4740_hwmon *hwmon = data;
33
34         complete(&hwmon->read_completion);
35         return IRQ_HANDLED;
36 }
37
38 static ssize_t in0_input_show(struct device *dev,
39                               struct device_attribute *dev_attr, char *buf)
40 {
41         struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
42         struct platform_device *pdev = hwmon->pdev;
43         struct completion *completion = &hwmon->read_completion;
44         long t;
45         unsigned long val;
46         int ret;
47
48         mutex_lock(&hwmon->lock);
49
50         reinit_completion(completion);
51
52         enable_irq(hwmon->irq);
53         hwmon->cell->enable(pdev);
54
55         t = wait_for_completion_interruptible_timeout(completion, HZ);
56
57         if (t > 0) {
58                 val = readw(hwmon->base) & 0xfff;
59                 val = (val * 3300) >> 12;
60                 ret = sprintf(buf, "%lu\n", val);
61         } else {
62                 ret = t ? t : -ETIMEDOUT;
63         }
64
65         hwmon->cell->disable(pdev);
66         disable_irq(hwmon->irq);
67
68         mutex_unlock(&hwmon->lock);
69
70         return ret;
71 }
72
73 static DEVICE_ATTR_RO(in0_input);
74
75 static struct attribute *jz4740_attrs[] = {
76         &dev_attr_in0_input.attr,
77         NULL
78 };
79
80 ATTRIBUTE_GROUPS(jz4740);
81
82 static int jz4740_hwmon_probe(struct platform_device *pdev)
83 {
84         int ret;
85         struct device *dev = &pdev->dev;
86         struct jz4740_hwmon *hwmon;
87         struct device *hwmon_dev;
88
89         hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
90         if (!hwmon)
91                 return -ENOMEM;
92
93         hwmon->cell = mfd_get_cell(pdev);
94
95         hwmon->irq = platform_get_irq(pdev, 0);
96         if (hwmon->irq < 0) {
97                 dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
98                         hwmon->irq);
99                 return hwmon->irq;
100         }
101
102         hwmon->base = devm_platform_ioremap_resource(pdev, 0);
103         if (IS_ERR(hwmon->base))
104                 return PTR_ERR(hwmon->base);
105
106         hwmon->pdev = pdev;
107         init_completion(&hwmon->read_completion);
108         mutex_init(&hwmon->lock);
109
110         ret = devm_request_irq(dev, hwmon->irq, jz4740_hwmon_irq, 0,
111                                pdev->name, hwmon);
112         if (ret) {
113                 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
114                 return ret;
115         }
116         disable_irq(hwmon->irq);
117
118         hwmon_dev = devm_hwmon_device_register_with_groups(dev, "jz4740", hwmon,
119                                                            jz4740_groups);
120         return PTR_ERR_OR_ZERO(hwmon_dev);
121 }
122
123 static struct platform_driver jz4740_hwmon_driver = {
124         .probe  = jz4740_hwmon_probe,
125         .driver = {
126                 .name = "jz4740-hwmon",
127         },
128 };
129
130 module_platform_driver(jz4740_hwmon_driver);
131
132 MODULE_DESCRIPTION("JZ4740 SoC HWMON driver");
133 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
134 MODULE_LICENSE("GPL");
135 MODULE_ALIAS("platform:jz4740-hwmon");