1 // SPDX-License-Identifier: GPL-2.0-only
3 * drivers/hwmon/nsa320-hwmon.c
5 * ZyXEL NSA320 Media Servers
8 * Copyright (C) 2016 Adam Baker <linux@baker-net.org.uk>
9 * based on a board file driver
10 * Copyright (C) 2012 Peter Schildmann <linux@schildmann.info>
13 #include <linux/bitops.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/jiffies.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
25 /* Tests for error return values rely upon this value being < 0x80 */
26 #define MAGIC_NUMBER 0x55
29 * The Zyxel hwmon MCU is a Holtek HT46R065 that is factory programmed
30 * to perform temperature and fan speed monitoring. It is read by taking
31 * the active pin low. The 32 bit output word is then clocked onto the
32 * data line. The MSB of the data word is a magic nuber to indicate it
33 * has been read correctly, the next byte is the fan speed (in hundreds
34 * of RPM) and the last two bytes are the temperature (in tenths of a
39 struct mutex update_lock
; /* lock GPIO operations */
40 unsigned long last_updated
; /* jiffies */
41 unsigned long mcu_data
;
42 struct gpio_desc
*act
;
43 struct gpio_desc
*clk
;
44 struct gpio_desc
*data
;
52 static const char * const nsa320_input_names
[] = {
53 [NSA320_TEMP
] = "System Temperature",
54 [NSA320_FAN
] = "Chassis Fan",
58 * Although this protocol looks similar to SPI the long delay
59 * between the active (aka chip select) signal and the shorter
60 * delay between clock pulses are needed for reliable operation.
61 * The delays provided are taken from the manufacturer kernel,
62 * testing suggest they probably incorporate a reasonable safety
63 * margin. (The single device tested became unreliable if the
64 * delay was reduced to 1/10th of this value.)
66 static s32
nsa320_hwmon_update(struct device
*dev
)
70 struct nsa320_hwmon
*hwmon
= dev_get_drvdata(dev
);
72 mutex_lock(&hwmon
->update_lock
);
74 mcu_data
= hwmon
->mcu_data
;
76 if (time_after(jiffies
, hwmon
->last_updated
+ HZ
) || mcu_data
== 0) {
77 gpiod_set_value(hwmon
->act
, 1);
81 for (mask
= BIT(31); mask
; mask
>>= 1) {
82 gpiod_set_value(hwmon
->clk
, 0);
83 usleep_range(100, 200);
84 gpiod_set_value(hwmon
->clk
, 1);
85 usleep_range(100, 200);
86 if (gpiod_get_value(hwmon
->data
))
90 gpiod_set_value(hwmon
->act
, 0);
91 dev_dbg(dev
, "Read raw MCU data %08x\n", mcu_data
);
93 if ((mcu_data
>> 24) != MAGIC_NUMBER
) {
94 dev_dbg(dev
, "Read invalid MCU data %08x\n", mcu_data
);
97 hwmon
->mcu_data
= mcu_data
;
98 hwmon
->last_updated
= jiffies
;
102 mutex_unlock(&hwmon
->update_lock
);
107 static ssize_t
label_show(struct device
*dev
, struct device_attribute
*attr
,
110 int channel
= to_sensor_dev_attr(attr
)->index
;
112 return sprintf(buf
, "%s\n", nsa320_input_names
[channel
]);
115 static ssize_t
temp1_input_show(struct device
*dev
,
116 struct device_attribute
*attr
, char *buf
)
118 s32 mcu_data
= nsa320_hwmon_update(dev
);
123 return sprintf(buf
, "%d\n", (mcu_data
& 0xffff) * 100);
126 static ssize_t
fan1_input_show(struct device
*dev
,
127 struct device_attribute
*attr
, char *buf
)
129 s32 mcu_data
= nsa320_hwmon_update(dev
);
134 return sprintf(buf
, "%d\n", ((mcu_data
& 0xff0000) >> 16) * 100);
137 static SENSOR_DEVICE_ATTR_RO(temp1_label
, label
, NSA320_TEMP
);
138 static DEVICE_ATTR_RO(temp1_input
);
139 static SENSOR_DEVICE_ATTR_RO(fan1_label
, label
, NSA320_FAN
);
140 static DEVICE_ATTR_RO(fan1_input
);
142 static struct attribute
*nsa320_attrs
[] = {
143 &sensor_dev_attr_temp1_label
.dev_attr
.attr
,
144 &dev_attr_temp1_input
.attr
,
145 &sensor_dev_attr_fan1_label
.dev_attr
.attr
,
146 &dev_attr_fan1_input
.attr
,
150 ATTRIBUTE_GROUPS(nsa320
);
152 static const struct of_device_id of_nsa320_hwmon_match
[] = {
153 { .compatible
= "zyxel,nsa320-mcu", },
157 static int nsa320_hwmon_probe(struct platform_device
*pdev
)
159 struct nsa320_hwmon
*hwmon
;
160 struct device
*classdev
;
162 hwmon
= devm_kzalloc(&pdev
->dev
, sizeof(*hwmon
), GFP_KERNEL
);
166 /* Look up the GPIO pins to use */
167 hwmon
->act
= devm_gpiod_get(&pdev
->dev
, "act", GPIOD_OUT_LOW
);
168 if (IS_ERR(hwmon
->act
))
169 return PTR_ERR(hwmon
->act
);
171 hwmon
->clk
= devm_gpiod_get(&pdev
->dev
, "clk", GPIOD_OUT_HIGH
);
172 if (IS_ERR(hwmon
->clk
))
173 return PTR_ERR(hwmon
->clk
);
175 hwmon
->data
= devm_gpiod_get(&pdev
->dev
, "data", GPIOD_IN
);
176 if (IS_ERR(hwmon
->data
))
177 return PTR_ERR(hwmon
->data
);
179 mutex_init(&hwmon
->update_lock
);
181 classdev
= devm_hwmon_device_register_with_groups(&pdev
->dev
,
182 "nsa320", hwmon
, nsa320_groups
);
184 return PTR_ERR_OR_ZERO(classdev
);
188 /* All allocations use devres so remove() is not needed. */
190 static struct platform_driver nsa320_hwmon_driver
= {
191 .probe
= nsa320_hwmon_probe
,
193 .name
= "nsa320-hwmon",
194 .of_match_table
= of_nsa320_hwmon_match
,
198 module_platform_driver(nsa320_hwmon_driver
);
200 MODULE_DEVICE_TABLE(of
, of_nsa320_hwmon_match
);
201 MODULE_AUTHOR("Peter Schildmann <linux@schildmann.info>");
202 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>");
203 MODULE_DESCRIPTION("NSA320 Hardware Monitoring");
204 MODULE_LICENSE("GPL v2");
205 MODULE_ALIAS("platform:nsa320-hwmon");