menf21bmc_hwmon.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * MEN 14F021P00 Board Management Controller (BMC) hwmon driver.
  3. *
  4. * This is the core hwmon driver of the MEN 14F021P00 BMC.
  5. * The BMC monitors the board voltages which can be access with this
  6. * driver through sysfs.
  7. *
  8. * Copyright (C) 2014 MEN Mikro Elektronik Nuernberg GmbH
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/hwmon-sysfs.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/err.h>
  24. #define DRV_NAME "menf21bmc_hwmon"
  25. #define BMC_VOLT_COUNT 5
  26. #define MENF21BMC_V33 0
  27. #define MENF21BMC_V5 1
  28. #define MENF21BMC_V12 2
  29. #define MENF21BMC_V5_SB 3
  30. #define MENF21BMC_VBAT 4
  31. #define IDX_TO_VOLT_MIN_CMD(idx) (0x40 + idx)
  32. #define IDX_TO_VOLT_MAX_CMD(idx) (0x50 + idx)
  33. #define IDX_TO_VOLT_INP_CMD(idx) (0x60 + idx)
  34. struct menf21bmc_hwmon {
  35. bool valid;
  36. struct i2c_client *i2c_client;
  37. unsigned long last_update;
  38. int in_val[BMC_VOLT_COUNT];
  39. int in_min[BMC_VOLT_COUNT];
  40. int in_max[BMC_VOLT_COUNT];
  41. };
  42. static const char *const input_names[] = {
  43. [MENF21BMC_V33] = "MON_3_3V",
  44. [MENF21BMC_V5] = "MON_5V",
  45. [MENF21BMC_V12] = "MON_12V",
  46. [MENF21BMC_V5_SB] = "5V_STANDBY",
  47. [MENF21BMC_VBAT] = "VBAT"
  48. };
  49. static struct menf21bmc_hwmon *menf21bmc_hwmon_update(struct device *dev)
  50. {
  51. int i;
  52. int val;
  53. struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
  54. struct menf21bmc_hwmon *data_ret = drv_data;
  55. if (time_after(jiffies, drv_data->last_update + HZ)
  56. || !drv_data->valid) {
  57. for (i = 0; i < BMC_VOLT_COUNT; i++) {
  58. val = i2c_smbus_read_word_data(drv_data->i2c_client,
  59. IDX_TO_VOLT_INP_CMD(i));
  60. if (val < 0) {
  61. data_ret = ERR_PTR(val);
  62. goto abort;
  63. }
  64. drv_data->in_val[i] = val;
  65. }
  66. drv_data->last_update = jiffies;
  67. drv_data->valid = true;
  68. }
  69. abort:
  70. return data_ret;
  71. }
  72. static int menf21bmc_hwmon_get_volt_limits(struct menf21bmc_hwmon *drv_data)
  73. {
  74. int i, val;
  75. for (i = 0; i < BMC_VOLT_COUNT; i++) {
  76. val = i2c_smbus_read_word_data(drv_data->i2c_client,
  77. IDX_TO_VOLT_MIN_CMD(i));
  78. if (val < 0)
  79. return val;
  80. drv_data->in_min[i] = val;
  81. val = i2c_smbus_read_word_data(drv_data->i2c_client,
  82. IDX_TO_VOLT_MAX_CMD(i));
  83. if (val < 0)
  84. return val;
  85. drv_data->in_max[i] = val;
  86. }
  87. return 0;
  88. }
  89. static ssize_t
  90. show_label(struct device *dev, struct device_attribute *devattr, char *buf)
  91. {
  92. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  93. return sprintf(buf, "%s\n", input_names[attr->index]);
  94. }
  95. static ssize_t
  96. show_in(struct device *dev, struct device_attribute *devattr, char *buf)
  97. {
  98. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  99. struct menf21bmc_hwmon *drv_data = menf21bmc_hwmon_update(dev);
  100. if (IS_ERR(drv_data))
  101. return PTR_ERR(drv_data);
  102. return sprintf(buf, "%d\n", drv_data->in_val[attr->index]);
  103. }
  104. static ssize_t
  105. show_min(struct device *dev, struct device_attribute *devattr, char *buf)
  106. {
  107. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  108. struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
  109. return sprintf(buf, "%d\n", drv_data->in_min[attr->index]);
  110. }
  111. static ssize_t
  112. show_max(struct device *dev, struct device_attribute *devattr, char *buf)
  113. {
  114. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  115. struct menf21bmc_hwmon *drv_data = dev_get_drvdata(dev);
  116. return sprintf(buf, "%d\n", drv_data->in_max[attr->index]);
  117. }
  118. #define create_voltage_sysfs(idx) \
  119. static SENSOR_DEVICE_ATTR(in##idx##_input, S_IRUGO, \
  120. show_in, NULL, idx); \
  121. static SENSOR_DEVICE_ATTR(in##idx##_min, S_IRUGO, \
  122. show_min, NULL, idx); \
  123. static SENSOR_DEVICE_ATTR(in##idx##_max, S_IRUGO, \
  124. show_max, NULL, idx); \
  125. static SENSOR_DEVICE_ATTR(in##idx##_label, S_IRUGO, \
  126. show_label, NULL, idx);
  127. create_voltage_sysfs(0);
  128. create_voltage_sysfs(1);
  129. create_voltage_sysfs(2);
  130. create_voltage_sysfs(3);
  131. create_voltage_sysfs(4);
  132. static struct attribute *menf21bmc_hwmon_attrs[] = {
  133. &sensor_dev_attr_in0_input.dev_attr.attr,
  134. &sensor_dev_attr_in0_min.dev_attr.attr,
  135. &sensor_dev_attr_in0_max.dev_attr.attr,
  136. &sensor_dev_attr_in0_label.dev_attr.attr,
  137. &sensor_dev_attr_in1_input.dev_attr.attr,
  138. &sensor_dev_attr_in1_min.dev_attr.attr,
  139. &sensor_dev_attr_in1_max.dev_attr.attr,
  140. &sensor_dev_attr_in1_label.dev_attr.attr,
  141. &sensor_dev_attr_in2_input.dev_attr.attr,
  142. &sensor_dev_attr_in2_min.dev_attr.attr,
  143. &sensor_dev_attr_in2_max.dev_attr.attr,
  144. &sensor_dev_attr_in2_label.dev_attr.attr,
  145. &sensor_dev_attr_in3_input.dev_attr.attr,
  146. &sensor_dev_attr_in3_min.dev_attr.attr,
  147. &sensor_dev_attr_in3_max.dev_attr.attr,
  148. &sensor_dev_attr_in3_label.dev_attr.attr,
  149. &sensor_dev_attr_in4_input.dev_attr.attr,
  150. &sensor_dev_attr_in4_min.dev_attr.attr,
  151. &sensor_dev_attr_in4_max.dev_attr.attr,
  152. &sensor_dev_attr_in4_label.dev_attr.attr,
  153. NULL
  154. };
  155. ATTRIBUTE_GROUPS(menf21bmc_hwmon);
  156. static int menf21bmc_hwmon_probe(struct platform_device *pdev)
  157. {
  158. int ret;
  159. struct menf21bmc_hwmon *drv_data;
  160. struct i2c_client *i2c_client = to_i2c_client(pdev->dev.parent);
  161. struct device *hwmon_dev;
  162. drv_data = devm_kzalloc(&pdev->dev, sizeof(struct menf21bmc_hwmon),
  163. GFP_KERNEL);
  164. if (!drv_data)
  165. return -ENOMEM;
  166. drv_data->i2c_client = i2c_client;
  167. ret = menf21bmc_hwmon_get_volt_limits(drv_data);
  168. if (ret) {
  169. dev_err(&pdev->dev, "failed to read sensor limits");
  170. return ret;
  171. }
  172. hwmon_dev = devm_hwmon_device_register_with_groups(&pdev->dev,
  173. "menf21bmc", drv_data,
  174. menf21bmc_hwmon_groups);
  175. if (IS_ERR(hwmon_dev))
  176. return PTR_ERR(hwmon_dev);
  177. dev_info(&pdev->dev, "MEN 14F021P00 BMC hwmon device enabled");
  178. return 0;
  179. }
  180. static struct platform_driver menf21bmc_hwmon = {
  181. .probe = menf21bmc_hwmon_probe,
  182. .driver = {
  183. .name = DRV_NAME,
  184. },
  185. };
  186. module_platform_driver(menf21bmc_hwmon);
  187. MODULE_AUTHOR("Andreas Werner <andreas.werner@men.de>");
  188. MODULE_DESCRIPTION("MEN 14F021P00 BMC hwmon");
  189. MODULE_LICENSE("GPL v2");
  190. MODULE_ALIAS("platform:menf21bmc_hwmon");