nsa320-hwmon.c 5.3 KB

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