1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
4 * JZ4740 SoC HWMON driver
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>
16 #include <linux/completion.h>
17 #include <linux/mfd/core.h>
19 #include <linux/hwmon.h>
24 const struct mfd_cell *cell;
25 struct platform_device *pdev;
26 struct completion read_completion;
30 static irqreturn_t jz4740_hwmon_irq(int irq, void *data)
32 struct jz4740_hwmon *hwmon = data;
34 complete(&hwmon->read_completion);
38 static ssize_t in0_input_show(struct device *dev,
39 struct device_attribute *dev_attr, char *buf)
41 struct jz4740_hwmon *hwmon = dev_get_drvdata(dev);
42 struct platform_device *pdev = hwmon->pdev;
43 struct completion *completion = &hwmon->read_completion;
48 mutex_lock(&hwmon->lock);
50 reinit_completion(completion);
52 enable_irq(hwmon->irq);
53 hwmon->cell->enable(pdev);
55 t = wait_for_completion_interruptible_timeout(completion, HZ);
58 val = readw(hwmon->base) & 0xfff;
59 val = (val * 3300) >> 12;
60 ret = sprintf(buf, "%lu\n", val);
62 ret = t ? t : -ETIMEDOUT;
65 hwmon->cell->disable(pdev);
66 disable_irq(hwmon->irq);
68 mutex_unlock(&hwmon->lock);
73 static DEVICE_ATTR_RO(in0_input);
75 static struct attribute *jz4740_attrs[] = {
76 &dev_attr_in0_input.attr,
80 ATTRIBUTE_GROUPS(jz4740);
82 static int jz4740_hwmon_probe(struct platform_device *pdev)
85 struct device *dev = &pdev->dev;
86 struct jz4740_hwmon *hwmon;
87 struct device *hwmon_dev;
89 hwmon = devm_kzalloc(dev, sizeof(*hwmon), GFP_KERNEL);
93 hwmon->cell = mfd_get_cell(pdev);
95 hwmon->irq = platform_get_irq(pdev, 0);
97 dev_err(&pdev->dev, "Failed to get platform irq: %d\n",
102 hwmon->base = devm_platform_ioremap_resource(pdev, 0);
103 if (IS_ERR(hwmon->base))
104 return PTR_ERR(hwmon->base);
107 init_completion(&hwmon->read_completion);
108 mutex_init(&hwmon->lock);
110 ret = devm_request_irq(dev, hwmon->irq, jz4740_hwmon_irq, 0,
113 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
116 disable_irq(hwmon->irq);
118 hwmon_dev = devm_hwmon_device_register_with_groups(dev, "jz4740", hwmon,
120 return PTR_ERR_OR_ZERO(hwmon_dev);
123 static struct platform_driver jz4740_hwmon_driver = {
124 .probe = jz4740_hwmon_probe,
126 .name = "jz4740-hwmon",
130 module_platform_driver(jz4740_hwmon_driver);
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");