0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/bitops.h>
0014 #include <linux/delay.h>
0015 #include <linux/err.h>
0016 #include <linux/gpio/consumer.h>
0017 #include <linux/hwmon.h>
0018 #include <linux/hwmon-sysfs.h>
0019 #include <linux/jiffies.h>
0020 #include <linux/module.h>
0021 #include <linux/mutex.h>
0022 #include <linux/of.h>
0023 #include <linux/of_device.h>
0024 #include <linux/of_platform.h>
0025 #include <linux/platform_device.h>
0026
0027
0028 #define MAGIC_NUMBER 0x55
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040 struct nsa320_hwmon {
0041 struct mutex update_lock;
0042 unsigned long last_updated;
0043 unsigned long mcu_data;
0044 struct gpio_desc *act;
0045 struct gpio_desc *clk;
0046 struct gpio_desc *data;
0047 };
0048
0049 enum nsa320_inputs {
0050 NSA320_TEMP = 0,
0051 NSA320_FAN = 1,
0052 };
0053
0054 static const char * const nsa320_input_names[] = {
0055 [NSA320_TEMP] = "System Temperature",
0056 [NSA320_FAN] = "Chassis Fan",
0057 };
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 static s32 nsa320_hwmon_update(struct device *dev)
0069 {
0070 u32 mcu_data;
0071 u32 mask;
0072 struct nsa320_hwmon *hwmon = dev_get_drvdata(dev);
0073
0074 mutex_lock(&hwmon->update_lock);
0075
0076 mcu_data = hwmon->mcu_data;
0077
0078 if (time_after(jiffies, hwmon->last_updated + HZ) || mcu_data == 0) {
0079 gpiod_set_value(hwmon->act, 1);
0080 msleep(100);
0081
0082 mcu_data = 0;
0083 for (mask = BIT(31); mask; mask >>= 1) {
0084 gpiod_set_value(hwmon->clk, 0);
0085 usleep_range(100, 200);
0086 gpiod_set_value(hwmon->clk, 1);
0087 usleep_range(100, 200);
0088 if (gpiod_get_value(hwmon->data))
0089 mcu_data |= mask;
0090 }
0091
0092 gpiod_set_value(hwmon->act, 0);
0093 dev_dbg(dev, "Read raw MCU data %08x\n", mcu_data);
0094
0095 if ((mcu_data >> 24) != MAGIC_NUMBER) {
0096 dev_dbg(dev, "Read invalid MCU data %08x\n", mcu_data);
0097 mcu_data = -EIO;
0098 } else {
0099 hwmon->mcu_data = mcu_data;
0100 hwmon->last_updated = jiffies;
0101 }
0102 }
0103
0104 mutex_unlock(&hwmon->update_lock);
0105
0106 return mcu_data;
0107 }
0108
0109 static ssize_t label_show(struct device *dev, struct device_attribute *attr,
0110 char *buf)
0111 {
0112 int channel = to_sensor_dev_attr(attr)->index;
0113
0114 return sprintf(buf, "%s\n", nsa320_input_names[channel]);
0115 }
0116
0117 static ssize_t temp1_input_show(struct device *dev,
0118 struct device_attribute *attr, char *buf)
0119 {
0120 s32 mcu_data = nsa320_hwmon_update(dev);
0121
0122 if (mcu_data < 0)
0123 return mcu_data;
0124
0125 return sprintf(buf, "%d\n", (mcu_data & 0xffff) * 100);
0126 }
0127
0128 static ssize_t fan1_input_show(struct device *dev,
0129 struct device_attribute *attr, char *buf)
0130 {
0131 s32 mcu_data = nsa320_hwmon_update(dev);
0132
0133 if (mcu_data < 0)
0134 return mcu_data;
0135
0136 return sprintf(buf, "%d\n", ((mcu_data & 0xff0000) >> 16) * 100);
0137 }
0138
0139 static SENSOR_DEVICE_ATTR_RO(temp1_label, label, NSA320_TEMP);
0140 static DEVICE_ATTR_RO(temp1_input);
0141 static SENSOR_DEVICE_ATTR_RO(fan1_label, label, NSA320_FAN);
0142 static DEVICE_ATTR_RO(fan1_input);
0143
0144 static struct attribute *nsa320_attrs[] = {
0145 &sensor_dev_attr_temp1_label.dev_attr.attr,
0146 &dev_attr_temp1_input.attr,
0147 &sensor_dev_attr_fan1_label.dev_attr.attr,
0148 &dev_attr_fan1_input.attr,
0149 NULL
0150 };
0151
0152 ATTRIBUTE_GROUPS(nsa320);
0153
0154 static const struct of_device_id of_nsa320_hwmon_match[] = {
0155 { .compatible = "zyxel,nsa320-mcu", },
0156 { },
0157 };
0158
0159 static int nsa320_hwmon_probe(struct platform_device *pdev)
0160 {
0161 struct nsa320_hwmon *hwmon;
0162 struct device *classdev;
0163
0164 hwmon = devm_kzalloc(&pdev->dev, sizeof(*hwmon), GFP_KERNEL);
0165 if (!hwmon)
0166 return -ENOMEM;
0167
0168
0169 hwmon->act = devm_gpiod_get(&pdev->dev, "act", GPIOD_OUT_LOW);
0170 if (IS_ERR(hwmon->act))
0171 return PTR_ERR(hwmon->act);
0172
0173 hwmon->clk = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_HIGH);
0174 if (IS_ERR(hwmon->clk))
0175 return PTR_ERR(hwmon->clk);
0176
0177 hwmon->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
0178 if (IS_ERR(hwmon->data))
0179 return PTR_ERR(hwmon->data);
0180
0181 mutex_init(&hwmon->update_lock);
0182
0183 classdev = devm_hwmon_device_register_with_groups(&pdev->dev,
0184 "nsa320", hwmon, nsa320_groups);
0185
0186 return PTR_ERR_OR_ZERO(classdev);
0187
0188 }
0189
0190
0191
0192 static struct platform_driver nsa320_hwmon_driver = {
0193 .probe = nsa320_hwmon_probe,
0194 .driver = {
0195 .name = "nsa320-hwmon",
0196 .of_match_table = of_match_ptr(of_nsa320_hwmon_match),
0197 },
0198 };
0199
0200 module_platform_driver(nsa320_hwmon_driver);
0201
0202 MODULE_DEVICE_TABLE(of, of_nsa320_hwmon_match);
0203 MODULE_AUTHOR("Peter Schildmann <linux@schildmann.info>");
0204 MODULE_AUTHOR("Adam Baker <linux@baker-net.org.uk>");
0205 MODULE_DESCRIPTION("NSA320 Hardware Monitoring");
0206 MODULE_LICENSE("GPL v2");
0207 MODULE_ALIAS("platform:nsa320-hwmon");