kfan.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2025 KEBA Industrial Automation GmbH
  4. *
  5. * Driver for KEBA fan controller FPGA IP core
  6. *
  7. */
  8. #include <linux/hwmon.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/auxiliary_bus.h>
  13. #include <linux/misc/keba.h>
  14. #define KFAN "kfan"
  15. #define KFAN_CONTROL_REG 0x04
  16. #define KFAN_STATUS_REG 0x08
  17. #define KFAN_STATUS_PRESENT 0x01
  18. #define KFAN_STATUS_REGULABLE 0x02
  19. #define KFAN_STATUS_TACHO 0x04
  20. #define KFAN_STATUS_BLOCKED 0x08
  21. #define KFAN_TACHO_REG 0x0c
  22. #define KFAN_DEFAULT_DIV 2
  23. struct kfan {
  24. void __iomem *base;
  25. bool tacho;
  26. bool regulable;
  27. /* hwmon API configuration */
  28. u32 fan_channel_config[2];
  29. struct hwmon_channel_info fan_info;
  30. u32 pwm_channel_config[2];
  31. struct hwmon_channel_info pwm_info;
  32. const struct hwmon_channel_info *info[3];
  33. struct hwmon_chip_info chip;
  34. };
  35. static bool kfan_get_fault(struct kfan *kfan)
  36. {
  37. u8 status = ioread8(kfan->base + KFAN_STATUS_REG);
  38. if (!(status & KFAN_STATUS_PRESENT))
  39. return true;
  40. if (!kfan->tacho && (status & KFAN_STATUS_BLOCKED))
  41. return true;
  42. return false;
  43. }
  44. static unsigned int kfan_count_to_rpm(u16 count)
  45. {
  46. if (count == 0 || count == 0xffff)
  47. return 0;
  48. return 5000000UL / (KFAN_DEFAULT_DIV * count);
  49. }
  50. static unsigned int kfan_get_rpm(struct kfan *kfan)
  51. {
  52. unsigned int rpm;
  53. u16 count;
  54. count = ioread16(kfan->base + KFAN_TACHO_REG);
  55. rpm = kfan_count_to_rpm(count);
  56. return rpm;
  57. }
  58. static unsigned int kfan_get_pwm(struct kfan *kfan)
  59. {
  60. return ioread8(kfan->base + KFAN_CONTROL_REG);
  61. }
  62. static int kfan_set_pwm(struct kfan *kfan, long val)
  63. {
  64. if (val < 0 || val > 0xff)
  65. return -EINVAL;
  66. /* if none-regulable, then only 0 or 0xff can be written */
  67. if (!kfan->regulable && val > 0)
  68. val = 0xff;
  69. iowrite8(val, kfan->base + KFAN_CONTROL_REG);
  70. return 0;
  71. }
  72. static int kfan_write(struct device *dev, enum hwmon_sensor_types type,
  73. u32 attr, int channel, long val)
  74. {
  75. struct kfan *kfan = dev_get_drvdata(dev);
  76. switch (type) {
  77. case hwmon_pwm:
  78. switch (attr) {
  79. case hwmon_pwm_input:
  80. return kfan_set_pwm(kfan, val);
  81. default:
  82. break;
  83. }
  84. break;
  85. default:
  86. break;
  87. }
  88. return -EOPNOTSUPP;
  89. }
  90. static int kfan_read(struct device *dev, enum hwmon_sensor_types type,
  91. u32 attr, int channel, long *val)
  92. {
  93. struct kfan *kfan = dev_get_drvdata(dev);
  94. switch (type) {
  95. case hwmon_fan:
  96. switch (attr) {
  97. case hwmon_fan_fault:
  98. *val = kfan_get_fault(kfan);
  99. return 0;
  100. case hwmon_fan_input:
  101. *val = kfan_get_rpm(kfan);
  102. return 0;
  103. default:
  104. break;
  105. }
  106. break;
  107. case hwmon_pwm:
  108. switch (attr) {
  109. case hwmon_pwm_input:
  110. *val = kfan_get_pwm(kfan);
  111. return 0;
  112. default:
  113. break;
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. return -EOPNOTSUPP;
  120. }
  121. static umode_t kfan_is_visible(const void *data, enum hwmon_sensor_types type,
  122. u32 attr, int channel)
  123. {
  124. switch (type) {
  125. case hwmon_fan:
  126. switch (attr) {
  127. case hwmon_fan_input:
  128. return 0444;
  129. case hwmon_fan_fault:
  130. return 0444;
  131. default:
  132. break;
  133. }
  134. break;
  135. case hwmon_pwm:
  136. switch (attr) {
  137. case hwmon_pwm_input:
  138. return 0644;
  139. default:
  140. break;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. return 0;
  147. }
  148. static const struct hwmon_ops kfan_hwmon_ops = {
  149. .is_visible = kfan_is_visible,
  150. .read = kfan_read,
  151. .write = kfan_write,
  152. };
  153. static int kfan_probe(struct auxiliary_device *auxdev,
  154. const struct auxiliary_device_id *id)
  155. {
  156. struct keba_fan_auxdev *kfan_auxdev =
  157. container_of(auxdev, struct keba_fan_auxdev, auxdev);
  158. struct device *dev = &auxdev->dev;
  159. struct device *hwmon_dev;
  160. struct kfan *kfan;
  161. u8 status;
  162. kfan = devm_kzalloc(dev, sizeof(*kfan), GFP_KERNEL);
  163. if (!kfan)
  164. return -ENOMEM;
  165. kfan->base = devm_ioremap_resource(dev, &kfan_auxdev->io);
  166. if (IS_ERR(kfan->base))
  167. return PTR_ERR(kfan->base);
  168. status = ioread8(kfan->base + KFAN_STATUS_REG);
  169. if (status & KFAN_STATUS_REGULABLE)
  170. kfan->regulable = true;
  171. if (status & KFAN_STATUS_TACHO)
  172. kfan->tacho = true;
  173. /* fan */
  174. kfan->fan_channel_config[0] = HWMON_F_FAULT;
  175. if (kfan->tacho)
  176. kfan->fan_channel_config[0] |= HWMON_F_INPUT;
  177. kfan->fan_info.type = hwmon_fan;
  178. kfan->fan_info.config = kfan->fan_channel_config;
  179. kfan->info[0] = &kfan->fan_info;
  180. /* PWM */
  181. kfan->pwm_channel_config[0] = HWMON_PWM_INPUT;
  182. kfan->pwm_info.type = hwmon_pwm;
  183. kfan->pwm_info.config = kfan->pwm_channel_config;
  184. kfan->info[1] = &kfan->pwm_info;
  185. kfan->chip.ops = &kfan_hwmon_ops;
  186. kfan->chip.info = kfan->info;
  187. hwmon_dev = devm_hwmon_device_register_with_info(dev, KFAN, kfan,
  188. &kfan->chip, NULL);
  189. return PTR_ERR_OR_ZERO(hwmon_dev);
  190. }
  191. static const struct auxiliary_device_id kfan_devtype_aux[] = {
  192. { .name = "keba.fan" },
  193. {}
  194. };
  195. MODULE_DEVICE_TABLE(auxiliary, kfan_devtype_aux);
  196. static struct auxiliary_driver kfan_driver_aux = {
  197. .name = KFAN,
  198. .id_table = kfan_devtype_aux,
  199. .probe = kfan_probe,
  200. };
  201. module_auxiliary_driver(kfan_driver_aux);
  202. MODULE_AUTHOR("Petar Bojanic <boja@keba.com>");
  203. MODULE_AUTHOR("Gerhard Engleder <eg@keba.com>");
  204. MODULE_DESCRIPTION("KEBA fan controller driver");
  205. MODULE_LICENSE("GPL");