Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/hwmon/nsa320-hwmon.c
  4 *
  5 * ZyXEL NSA320 Media Servers
  6 * hardware monitoring
  7 *
  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>
 11 */
 12
 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>
 22#include <linux/of.h>
 23#include <linux/of_device.h>
 24#include <linux/of_platform.h>
 25#include <linux/platform_device.h>
 26
 27/* Tests for error return values rely upon this value being < 0x80 */
 28#define MAGIC_NUMBER 0x55
 29
 30/*
 31 * The Zyxel hwmon MCU is a Holtek HT46R065 that is factory programmed
 32 * to perform temperature and fan speed monitoring. It is read by taking
 33 * the active pin low. The 32 bit output word is then clocked onto the
 34 * data line. The MSB of the data word is a magic nuber to indicate it
 35 * has been read correctly, the next byte is the fan speed (in hundreds
 36 * of RPM) and the last two bytes are the temperature (in tenths of a
 37 * degree)
 38 */
 39
 40struct nsa320_hwmon {
 41	struct mutex		update_lock;	/* lock GPIO operations */
 42	unsigned long		last_updated;	/* jiffies */
 43	unsigned long		mcu_data;
 44	struct gpio_desc	*act;
 45	struct gpio_desc	*clk;
 46	struct gpio_desc	*data;
 47};
 48
 49enum nsa320_inputs {
 50	NSA320_TEMP = 0,
 51	NSA320_FAN = 1,
 52};
 53
 54static const char * const nsa320_input_names[] = {
 55	[NSA320_TEMP] = "System Temperature",
 56	[NSA320_FAN] = "Chassis Fan",
 57};
 58
 59/*
 60 * Although this protocol looks similar to SPI the long delay
 61 * between the active (aka chip select) signal and the shorter
 62 * delay between clock pulses are needed for reliable operation.
 63 * The delays provided are taken from the manufacturer kernel,
 64 * testing suggest they probably incorporate a reasonable safety
 65 * margin. (The single device tested became unreliable if the
 66 * delay was reduced to 1/10th of this value.)
 67 */
 68static s32 nsa320_hwmon_update(struct device *dev)
 69{
 70	u32 mcu_data;
 71	u32 mask;
 72	struct nsa320_hwmon *hwmon = dev_get_drvdata(dev);
 73
 74	mutex_lock(&hwmon->update_lock);
 75
 76	mcu_data = hwmon->mcu_data;
 77
 78	if (time_after(jiffies, hwmon->last_updated + HZ) || mcu_data == 0) {
 79		gpiod_set_value(hwmon->act, 1);
 80		msleep(100);
 81
 82		mcu_data = 0;
 83		for (mask = BIT(31); mask; mask >>= 1) {
 84			gpiod_set_value(hwmon->clk, 0);
 85			usleep_range(100, 200);
 86			gpiod_set_value(hwmon->clk, 1);
 87			usleep_range(100, 200);
 88			if (gpiod_get_value(hwmon->data))
 89				mcu_data |= mask;
 90		}
 91
 92		gpiod_set_value(hwmon->act, 0);
 93		dev_dbg(dev, "Read raw MCU data %08x\n", mcu_data);
 94
 95		if ((mcu_data >> 24) != MAGIC_NUMBER) {
 96			dev_dbg(dev, "Read invalid MCU data %08x\n", mcu_data);
 97			mcu_data = -EIO;
 98		} else {
 99			hwmon->mcu_data = mcu_data;
100			hwmon->last_updated = jiffies;
101		}
102	}
103
104	mutex_unlock(&hwmon->update_lock);
105
106	return mcu_data;
107}
108
109static ssize_t label_show(struct device *dev, struct device_attribute *attr,
110			  char *buf)
111{
112	int channel = to_sensor_dev_attr(attr)->index;
113
114	return sprintf(buf, "%s\n", nsa320_input_names[channel]);
115}
116
117static ssize_t temp1_input_show(struct device *dev,
118				struct device_attribute *attr, char *buf)
119{
120	s32 mcu_data = nsa320_hwmon_update(dev);
121
122	if (mcu_data < 0)
123		return mcu_data;
124
125	return sprintf(buf, "%d\n", (mcu_data & 0xffff) * 100);
126}
127
128static ssize_t fan1_input_show(struct device *dev,
129			       struct device_attribute *attr, char *buf)
130{
131	s32 mcu_data = nsa320_hwmon_update(dev);
132
133	if (mcu_data < 0)
134		return mcu_data;
135
136	return sprintf(buf, "%d\n", ((mcu_data & 0xff0000) >> 16) * 100);
137}
138
139static SENSOR_DEVICE_ATTR_RO(temp1_label, label, NSA320_TEMP);
140static DEVICE_ATTR_RO(temp1_input);
141static SENSOR_DEVICE_ATTR_RO(fan1_label, label, NSA320_FAN);
142static DEVICE_ATTR_RO(fan1_input);
143
144static struct attribute *nsa320_attrs[] = {
145	&sensor_dev_attr_temp1_label.dev_attr.attr,
146	&dev_attr_temp1_input.attr,
147	&sensor_dev_attr_fan1_label.dev_attr.attr,
148	&dev_attr_fan1_input.attr,
149	NULL
150};
151
152ATTRIBUTE_GROUPS(nsa320);
153
154static const struct of_device_id of_nsa320_hwmon_match[] = {
155	{ .compatible = "zyxel,nsa320-mcu", },
156	{ },
157};
158
159static int nsa320_hwmon_probe(struct platform_device *pdev)
160{
161	struct nsa320_hwmon	*hwmon;
162	struct device		*classdev;
163
164	hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
165	if (!hwmon)
166		return -ENOMEM;
167
168	/* Look up the GPIO pins to use */
169	hwmon->act = devm_gpiod_get(&pdev->dev, "act", GPIOD_OUT_LOW);
170	if (IS_ERR(hwmon->act))
171		return PTR_ERR(hwmon->act);
172
173	hwmon->clk = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_HIGH);
174	if (IS_ERR(hwmon->clk))
175		return PTR_ERR(hwmon->clk);
176
177	hwmon->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
178	if (IS_ERR(hwmon->data))
179		return PTR_ERR(hwmon->data);
180
181	mutex_init(&hwmon->update_lock);
182
183	classdev = devm_hwmon_device_register_with_groups(&pdev->dev,
184					"nsa320", hwmon, nsa320_groups);
185
186	return PTR_ERR_OR_ZERO(classdev);
187
188}
189
190/* All allocations use devres so remove() is not needed. */
191
192static struct platform_driver nsa320_hwmon_driver = {
193	.probe = nsa320_hwmon_probe,
194	.driver = {
195		.name = "nsa320-hwmon",
196		.of_match_table = of_match_ptr(of_nsa320_hwmon_match),
197	},
198};
199
200module_platform_driver(nsa320_hwmon_driver);
201
202MODULE_DEVICE_TABLE(of, of_nsa320_hwmon_match);
203MODULE_AUTHOR("Peter Schildmann <linux@schildmann.info>");
204MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>");
205MODULE_DESCRIPTION("NSA320 Hardware Monitoring");
206MODULE_LICENSE("GPL v2");
207MODULE_ALIAS("platform:nsa320-hwmon");
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * drivers/hwmon/nsa320-hwmon.c
  4 *
  5 * ZyXEL NSA320 Media Servers
  6 * hardware monitoring
  7 *
  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>
 11 */
 12
 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>
 22#include <linux/of.h>
 
 
 23#include <linux/platform_device.h>
 24
 25/* Tests for error return values rely upon this value being < 0x80 */
 26#define MAGIC_NUMBER 0x55
 27
 28/*
 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
 35 * degree)
 36 */
 37
 38struct nsa320_hwmon {
 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;
 45};
 46
 47enum nsa320_inputs {
 48	NSA320_TEMP = 0,
 49	NSA320_FAN = 1,
 50};
 51
 52static const char * const nsa320_input_names[] = {
 53	[NSA320_TEMP] = "System Temperature",
 54	[NSA320_FAN] = "Chassis Fan",
 55};
 56
 57/*
 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.)
 65 */
 66static s32 nsa320_hwmon_update(struct device *dev)
 67{
 68	u32 mcu_data;
 69	u32 mask;
 70	struct nsa320_hwmon *hwmon = dev_get_drvdata(dev);
 71
 72	mutex_lock(&hwmon->update_lock);
 73
 74	mcu_data = hwmon->mcu_data;
 75
 76	if (time_after(jiffies, hwmon->last_updated + HZ) || mcu_data == 0) {
 77		gpiod_set_value(hwmon->act, 1);
 78		msleep(100);
 79
 80		mcu_data = 0;
 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))
 87				mcu_data |= mask;
 88		}
 89
 90		gpiod_set_value(hwmon->act, 0);
 91		dev_dbg(dev, "Read raw MCU data %08x\n", mcu_data);
 92
 93		if ((mcu_data >> 24) != MAGIC_NUMBER) {
 94			dev_dbg(dev, "Read invalid MCU data %08x\n", mcu_data);
 95			mcu_data = -EIO;
 96		} else {
 97			hwmon->mcu_data = mcu_data;
 98			hwmon->last_updated = jiffies;
 99		}
100	}
101
102	mutex_unlock(&hwmon->update_lock);
103
104	return mcu_data;
105}
106
107static ssize_t label_show(struct device *dev, struct device_attribute *attr,
108			  char *buf)
109{
110	int channel = to_sensor_dev_attr(attr)->index;
111
112	return sprintf(buf, "%s\n", nsa320_input_names[channel]);
113}
114
115static ssize_t temp1_input_show(struct device *dev,
116				struct device_attribute *attr, char *buf)
117{
118	s32 mcu_data = nsa320_hwmon_update(dev);
119
120	if (mcu_data < 0)
121		return mcu_data;
122
123	return sprintf(buf, "%d\n", (mcu_data & 0xffff) * 100);
124}
125
126static ssize_t fan1_input_show(struct device *dev,
127			       struct device_attribute *attr, char *buf)
128{
129	s32 mcu_data = nsa320_hwmon_update(dev);
130
131	if (mcu_data < 0)
132		return mcu_data;
133
134	return sprintf(buf, "%d\n", ((mcu_data & 0xff0000) >> 16) * 100);
135}
136
137static SENSOR_DEVICE_ATTR_RO(temp1_label, label, NSA320_TEMP);
138static DEVICE_ATTR_RO(temp1_input);
139static SENSOR_DEVICE_ATTR_RO(fan1_label, label, NSA320_FAN);
140static DEVICE_ATTR_RO(fan1_input);
141
142static 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,
147	NULL
148};
149
150ATTRIBUTE_GROUPS(nsa320);
151
152static const struct of_device_id of_nsa320_hwmon_match[] = {
153	{ .compatible = "zyxel,nsa320-mcu", },
154	{ },
155};
156
157static int nsa320_hwmon_probe(struct platform_device *pdev)
158{
159	struct nsa320_hwmon	*hwmon;
160	struct device		*classdev;
161
162	hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
163	if (!hwmon)
164		return -ENOMEM;
165
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);
170
171	hwmon->clk = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_HIGH);
172	if (IS_ERR(hwmon->clk))
173		return PTR_ERR(hwmon->clk);
174
175	hwmon->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
176	if (IS_ERR(hwmon->data))
177		return PTR_ERR(hwmon->data);
178
179	mutex_init(&hwmon->update_lock);
180
181	classdev = devm_hwmon_device_register_with_groups(&pdev->dev,
182					"nsa320", hwmon, nsa320_groups);
183
184	return PTR_ERR_OR_ZERO(classdev);
185
186}
187
188/* All allocations use devres so remove() is not needed. */
189
190static struct platform_driver nsa320_hwmon_driver = {
191	.probe = nsa320_hwmon_probe,
192	.driver = {
193		.name = "nsa320-hwmon",
194		.of_match_table = of_nsa320_hwmon_match,
195	},
196};
197
198module_platform_driver(nsa320_hwmon_driver);
199
200MODULE_DEVICE_TABLE(of, of_nsa320_hwmon_match);
201MODULE_AUTHOR("Peter Schildmann <linux@schildmann.info>");
202MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>");
203MODULE_DESCRIPTION("NSA320 Hardware Monitoring");
204MODULE_LICENSE("GPL v2");
205MODULE_ALIAS("platform:nsa320-hwmon");