nsa320-hwmon.c 5.9 KB

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