2 * drivers/hwmon/nsa320-hwmon.c
4 * ZyXEL NSA320 Media Servers
7 * Copyright (C) 2016 Adam Baker <linux@baker-net.org.uk>
8 * based on a board file driver
9 * Copyright (C) 2012 Peter Schildmann <linux@schildmann.info>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License v2 as published by the
13 * Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 #include <linux/bitops.h>
22 #include <linux/delay.h>
23 #include <linux/err.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/hwmon.h>
26 #include <linux/hwmon-sysfs.h>
27 #include <linux/jiffies.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
31 #include <linux/of_device.h>
32 #include <linux/of_platform.h>
33 #include <linux/platform_device.h>
35 /* Tests for error return values rely upon this value being < 0x80 */
36 #define MAGIC_NUMBER 0x55
39 * The Zyxel hwmon MCU is a Holtek HT46R065 that is factory programmed
40 * to perform temperature and fan speed monitoring. It is read by taking
41 * the active pin low. The 32 bit output word is then clocked onto the
42 * data line. The MSB of the data word is a magic nuber to indicate it
43 * has been read correctly, the next byte is the fan speed (in hundreds
44 * of RPM) and the last two bytes are the temperature (in tenths of a
49 struct mutex update_lock
; /* lock GPIO operations */
50 unsigned long last_updated
; /* jiffies */
51 unsigned long mcu_data
;
52 struct gpio_desc
*act
;
53 struct gpio_desc
*clk
;
54 struct gpio_desc
*data
;
62 static const char * const nsa320_input_names
[] = {
63 [NSA320_TEMP
] = "System Temperature",
64 [NSA320_FAN
] = "Chassis Fan",
68 * Although this protocol looks similar to SPI the long delay
69 * between the active (aka chip select) signal and the shorter
70 * delay between clock pulses are needed for reliable operation.
71 * The delays provided are taken from the manufacturer kernel,
72 * testing suggest they probably incorporate a reasonable safety
73 * margin. (The single device tested became unreliable if the
74 * delay was reduced to 1/10th of this value.)
76 static s32
nsa320_hwmon_update(struct device
*dev
)
80 struct nsa320_hwmon
*hwmon
= dev_get_drvdata(dev
);
82 mutex_lock(&hwmon
->update_lock
);
84 mcu_data
= hwmon
->mcu_data
;
86 if (time_after(jiffies
, hwmon
->last_updated
+ HZ
) || mcu_data
== 0) {
87 gpiod_set_value(hwmon
->act
, 1);
91 for (mask
= BIT(31); mask
; mask
>>= 1) {
92 gpiod_set_value(hwmon
->clk
, 0);
93 usleep_range(100, 200);
94 gpiod_set_value(hwmon
->clk
, 1);
95 usleep_range(100, 200);
96 if (gpiod_get_value(hwmon
->data
))
100 gpiod_set_value(hwmon
->act
, 0);
101 dev_dbg(dev
, "Read raw MCU data %08x\n", mcu_data
);
103 if ((mcu_data
>> 24) != MAGIC_NUMBER
) {
104 dev_dbg(dev
, "Read invalid MCU data %08x\n", mcu_data
);
107 hwmon
->mcu_data
= mcu_data
;
108 hwmon
->last_updated
= jiffies
;
112 mutex_unlock(&hwmon
->update_lock
);
117 static ssize_t
show_label(struct device
*dev
,
118 struct device_attribute
*attr
, char *buf
)
120 int channel
= to_sensor_dev_attr(attr
)->index
;
122 return sprintf(buf
, "%s\n", nsa320_input_names
[channel
]);
125 static ssize_t
show_temp(struct device
*dev
, struct device_attribute
*attr
,
128 s32 mcu_data
= nsa320_hwmon_update(dev
);
133 return sprintf(buf
, "%d\n", (mcu_data
& 0xffff) * 100);
136 static ssize_t
show_fan(struct device
*dev
, struct device_attribute
*attr
,
139 s32 mcu_data
= nsa320_hwmon_update(dev
);
144 return sprintf(buf
, "%d\n", ((mcu_data
& 0xff0000) >> 16) * 100);
147 static SENSOR_DEVICE_ATTR(temp1_label
, S_IRUGO
, show_label
, NULL
, NSA320_TEMP
);
148 static DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
);
149 static SENSOR_DEVICE_ATTR(fan1_label
, S_IRUGO
, show_label
, NULL
, NSA320_FAN
);
150 static DEVICE_ATTR(fan1_input
, S_IRUGO
, show_fan
, NULL
);
152 static struct attribute
*nsa320_attrs
[] = {
153 &sensor_dev_attr_temp1_label
.dev_attr
.attr
,
154 &dev_attr_temp1_input
.attr
,
155 &sensor_dev_attr_fan1_label
.dev_attr
.attr
,
156 &dev_attr_fan1_input
.attr
,
160 ATTRIBUTE_GROUPS(nsa320
);
162 static const struct of_device_id of_nsa320_hwmon_match
[] = {
163 { .compatible
= "zyxel,nsa320-mcu", },
167 static int nsa320_hwmon_probe(struct platform_device
*pdev
)
169 struct nsa320_hwmon
*hwmon
;
170 struct device
*classdev
;
172 hwmon
= devm_kzalloc(&pdev
->dev
, sizeof(*hwmon
), GFP_KERNEL
);
176 /* Look up the GPIO pins to use */
177 hwmon
->act
= devm_gpiod_get(&pdev
->dev
, "act", GPIOD_OUT_LOW
);
178 if (IS_ERR(hwmon
->act
))
179 return PTR_ERR(hwmon
->act
);
181 hwmon
->clk
= devm_gpiod_get(&pdev
->dev
, "clk", GPIOD_OUT_HIGH
);
182 if (IS_ERR(hwmon
->clk
))
183 return PTR_ERR(hwmon
->clk
);
185 hwmon
->data
= devm_gpiod_get(&pdev
->dev
, "data", GPIOD_IN
);
186 if (IS_ERR(hwmon
->data
))
187 return PTR_ERR(hwmon
->data
);
189 mutex_init(&hwmon
->update_lock
);
191 classdev
= devm_hwmon_device_register_with_groups(&pdev
->dev
,
192 "nsa320", hwmon
, nsa320_groups
);
194 return PTR_ERR_OR_ZERO(classdev
);
198 /* All allocations use devres so remove() is not needed. */
200 static struct platform_driver nsa320_hwmon_driver
= {
201 .probe
= nsa320_hwmon_probe
,
203 .name
= "nsa320-hwmon",
204 .of_match_table
= of_match_ptr(of_nsa320_hwmon_match
),
208 module_platform_driver(nsa320_hwmon_driver
);
210 MODULE_DEVICE_TABLE(of
, of_nsa320_hwmon_match
);
211 MODULE_AUTHOR("Peter Schildmann <linux@schildmann.info>");
212 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>");
213 MODULE_DESCRIPTION("NSA320 Hardware Monitoring");
214 MODULE_LICENSE("GPL v2");
215 MODULE_ALIAS("platform:nsa320-hwmon");