nsa320-hwmon.c 5.4 KB

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