intel-m10-bmc-hwmon.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel MAX 10 BMC HWMON Driver
  4. *
  5. * Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
  6. *
  7. */
  8. #include <linux/device.h>
  9. #include <linux/hwmon.h>
  10. #include <linux/mfd/intel-m10-bmc.h>
  11. #include <linux/module.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/platform_device.h>
  14. struct m10bmc_sdata {
  15. unsigned int reg_input;
  16. unsigned int reg_max;
  17. unsigned int reg_crit;
  18. unsigned int reg_hyst;
  19. unsigned int reg_min;
  20. unsigned int multiplier;
  21. const char *label;
  22. };
  23. struct m10bmc_hwmon_board_data {
  24. const struct m10bmc_sdata *tables[hwmon_max];
  25. const struct hwmon_channel_info **hinfo;
  26. };
  27. struct m10bmc_hwmon {
  28. struct device *dev;
  29. struct hwmon_chip_info chip;
  30. char *hw_name;
  31. struct intel_m10bmc *m10bmc;
  32. const struct m10bmc_hwmon_board_data *bdata;
  33. };
  34. static const struct m10bmc_sdata n3000bmc_temp_tbl[] = {
  35. { 0x100, 0x104, 0x108, 0x10c, 0x0, 500, "Board Temperature" },
  36. { 0x110, 0x114, 0x118, 0x0, 0x0, 500, "FPGA Die Temperature" },
  37. { 0x11c, 0x124, 0x120, 0x0, 0x0, 500, "QSFP0 Temperature" },
  38. { 0x12c, 0x134, 0x130, 0x0, 0x0, 500, "QSFP1 Temperature" },
  39. { 0x168, 0x0, 0x0, 0x0, 0x0, 500, "Retimer A Temperature" },
  40. { 0x16c, 0x0, 0x0, 0x0, 0x0, 500, "Retimer A SerDes Temperature" },
  41. { 0x170, 0x0, 0x0, 0x0, 0x0, 500, "Retimer B Temperature" },
  42. { 0x174, 0x0, 0x0, 0x0, 0x0, 500, "Retimer B SerDes Temperature" },
  43. };
  44. static const struct m10bmc_sdata n3000bmc_in_tbl[] = {
  45. { 0x128, 0x0, 0x0, 0x0, 0x0, 1, "QSFP0 Supply Voltage" },
  46. { 0x138, 0x0, 0x0, 0x0, 0x0, 1, "QSFP1 Supply Voltage" },
  47. { 0x13c, 0x0, 0x0, 0x0, 0x0, 1, "FPGA Core Voltage" },
  48. { 0x144, 0x0, 0x0, 0x0, 0x0, 1, "12V Backplane Voltage" },
  49. { 0x14c, 0x0, 0x0, 0x0, 0x0, 1, "1.2V Voltage" },
  50. { 0x150, 0x0, 0x0, 0x0, 0x0, 1, "12V AUX Voltage" },
  51. { 0x158, 0x0, 0x0, 0x0, 0x0, 1, "1.8V Voltage" },
  52. { 0x15c, 0x0, 0x0, 0x0, 0x0, 1, "3.3V Voltage" },
  53. };
  54. static const struct m10bmc_sdata n3000bmc_curr_tbl[] = {
  55. { 0x140, 0x0, 0x0, 0x0, 0x0, 1, "FPGA Core Current" },
  56. { 0x148, 0x0, 0x0, 0x0, 0x0, 1, "12V Backplane Current" },
  57. { 0x154, 0x0, 0x0, 0x0, 0x0, 1, "12V AUX Current" },
  58. };
  59. static const struct m10bmc_sdata n3000bmc_power_tbl[] = {
  60. { 0x160, 0x0, 0x0, 0x0, 0x0, 1000, "Board Power" },
  61. };
  62. static const struct hwmon_channel_info *n3000bmc_hinfo[] = {
  63. HWMON_CHANNEL_INFO(temp,
  64. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
  65. HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_LABEL,
  66. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
  67. HWMON_T_LABEL,
  68. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
  69. HWMON_T_LABEL,
  70. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
  71. HWMON_T_LABEL,
  72. HWMON_T_INPUT | HWMON_T_LABEL,
  73. HWMON_T_INPUT | HWMON_T_LABEL,
  74. HWMON_T_INPUT | HWMON_T_LABEL,
  75. HWMON_T_INPUT | HWMON_T_LABEL),
  76. HWMON_CHANNEL_INFO(in,
  77. HWMON_I_INPUT | HWMON_I_LABEL,
  78. HWMON_I_INPUT | HWMON_I_LABEL,
  79. HWMON_I_INPUT | HWMON_I_LABEL,
  80. HWMON_I_INPUT | HWMON_I_LABEL,
  81. HWMON_I_INPUT | HWMON_I_LABEL,
  82. HWMON_I_INPUT | HWMON_I_LABEL,
  83. HWMON_I_INPUT | HWMON_I_LABEL,
  84. HWMON_I_INPUT | HWMON_I_LABEL),
  85. HWMON_CHANNEL_INFO(curr,
  86. HWMON_C_INPUT | HWMON_C_LABEL,
  87. HWMON_C_INPUT | HWMON_C_LABEL,
  88. HWMON_C_INPUT | HWMON_C_LABEL),
  89. HWMON_CHANNEL_INFO(power,
  90. HWMON_P_INPUT | HWMON_P_LABEL),
  91. NULL
  92. };
  93. static const struct m10bmc_hwmon_board_data n3000bmc_hwmon_bdata = {
  94. .tables = {
  95. [hwmon_temp] = n3000bmc_temp_tbl,
  96. [hwmon_in] = n3000bmc_in_tbl,
  97. [hwmon_curr] = n3000bmc_curr_tbl,
  98. [hwmon_power] = n3000bmc_power_tbl,
  99. },
  100. .hinfo = n3000bmc_hinfo,
  101. };
  102. static umode_t
  103. m10bmc_hwmon_is_visible(const void *data, enum hwmon_sensor_types type,
  104. u32 attr, int channel)
  105. {
  106. return 0444;
  107. }
  108. static const struct m10bmc_sdata *
  109. find_sensor_data(struct m10bmc_hwmon *hw, enum hwmon_sensor_types type,
  110. int channel)
  111. {
  112. const struct m10bmc_sdata *tbl;
  113. tbl = hw->bdata->tables[type];
  114. if (!tbl)
  115. return ERR_PTR(-EOPNOTSUPP);
  116. return &tbl[channel];
  117. }
  118. static int do_sensor_read(struct m10bmc_hwmon *hw,
  119. const struct m10bmc_sdata *data,
  120. unsigned int regoff, long *val)
  121. {
  122. unsigned int regval;
  123. int ret;
  124. ret = m10bmc_sys_read(hw->m10bmc, regoff, &regval);
  125. if (ret)
  126. return ret;
  127. /*
  128. * BMC Firmware will return 0xdeadbeef if the sensor value is invalid
  129. * at that time. This usually happens on sensor channels which connect
  130. * to external pluggable modules, e.g. QSFP temperature and voltage.
  131. * When the QSFP is unplugged from cage, driver will get 0xdeadbeef
  132. * from their registers.
  133. */
  134. if (regval == 0xdeadbeef)
  135. return -ENODATA;
  136. *val = regval * data->multiplier;
  137. return 0;
  138. }
  139. static int m10bmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  140. u32 attr, int channel, long *val)
  141. {
  142. struct m10bmc_hwmon *hw = dev_get_drvdata(dev);
  143. unsigned int reg = 0, reg_hyst = 0;
  144. const struct m10bmc_sdata *data;
  145. long hyst, value;
  146. int ret;
  147. data = find_sensor_data(hw, type, channel);
  148. if (IS_ERR(data))
  149. return PTR_ERR(data);
  150. switch (type) {
  151. case hwmon_temp:
  152. switch (attr) {
  153. case hwmon_temp_input:
  154. reg = data->reg_input;
  155. break;
  156. case hwmon_temp_max_hyst:
  157. reg_hyst = data->reg_hyst;
  158. fallthrough;
  159. case hwmon_temp_max:
  160. reg = data->reg_max;
  161. break;
  162. case hwmon_temp_crit_hyst:
  163. reg_hyst = data->reg_hyst;
  164. fallthrough;
  165. case hwmon_temp_crit:
  166. reg = data->reg_crit;
  167. break;
  168. default:
  169. return -EOPNOTSUPP;
  170. }
  171. break;
  172. case hwmon_in:
  173. switch (attr) {
  174. case hwmon_in_input:
  175. reg = data->reg_input;
  176. break;
  177. case hwmon_in_max:
  178. reg = data->reg_max;
  179. break;
  180. case hwmon_in_crit:
  181. reg = data->reg_crit;
  182. break;
  183. case hwmon_in_min:
  184. reg = data->reg_min;
  185. break;
  186. default:
  187. return -EOPNOTSUPP;
  188. }
  189. break;
  190. case hwmon_curr:
  191. switch (attr) {
  192. case hwmon_curr_input:
  193. reg = data->reg_input;
  194. break;
  195. case hwmon_curr_max:
  196. reg = data->reg_max;
  197. break;
  198. case hwmon_curr_crit:
  199. reg = data->reg_crit;
  200. break;
  201. default:
  202. return -EOPNOTSUPP;
  203. }
  204. break;
  205. case hwmon_power:
  206. switch (attr) {
  207. case hwmon_power_input:
  208. reg = data->reg_input;
  209. break;
  210. default:
  211. return -EOPNOTSUPP;
  212. }
  213. break;
  214. default:
  215. return -EOPNOTSUPP;
  216. }
  217. if (!reg)
  218. return -EOPNOTSUPP;
  219. ret = do_sensor_read(hw, data, reg, &value);
  220. if (ret)
  221. return ret;
  222. if (reg_hyst) {
  223. ret = do_sensor_read(hw, data, reg_hyst, &hyst);
  224. if (ret)
  225. return ret;
  226. value -= hyst;
  227. }
  228. *val = value;
  229. return 0;
  230. }
  231. static int m10bmc_hwmon_read_string(struct device *dev,
  232. enum hwmon_sensor_types type,
  233. u32 attr, int channel, const char **str)
  234. {
  235. struct m10bmc_hwmon *hw = dev_get_drvdata(dev);
  236. const struct m10bmc_sdata *data;
  237. data = find_sensor_data(hw, type, channel);
  238. if (IS_ERR(data))
  239. return PTR_ERR(data);
  240. *str = data->label;
  241. return 0;
  242. }
  243. static const struct hwmon_ops m10bmc_hwmon_ops = {
  244. .is_visible = m10bmc_hwmon_is_visible,
  245. .read = m10bmc_hwmon_read,
  246. .read_string = m10bmc_hwmon_read_string,
  247. };
  248. static int m10bmc_hwmon_probe(struct platform_device *pdev)
  249. {
  250. const struct platform_device_id *id = platform_get_device_id(pdev);
  251. struct intel_m10bmc *m10bmc = dev_get_drvdata(pdev->dev.parent);
  252. struct device *hwmon_dev, *dev = &pdev->dev;
  253. struct m10bmc_hwmon *hw;
  254. int i;
  255. hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
  256. if (!hw)
  257. return -ENOMEM;
  258. hw->dev = dev;
  259. hw->m10bmc = m10bmc;
  260. hw->bdata = (const struct m10bmc_hwmon_board_data *)id->driver_data;
  261. hw->chip.info = hw->bdata->hinfo;
  262. hw->chip.ops = &m10bmc_hwmon_ops;
  263. hw->hw_name = devm_kstrdup(dev, id->name, GFP_KERNEL);
  264. if (!hw->hw_name)
  265. return -ENOMEM;
  266. for (i = 0; hw->hw_name[i]; i++)
  267. if (hwmon_is_bad_char(hw->hw_name[i]))
  268. hw->hw_name[i] = '_';
  269. hwmon_dev = devm_hwmon_device_register_with_info(dev, hw->hw_name,
  270. hw, &hw->chip, NULL);
  271. return PTR_ERR_OR_ZERO(hwmon_dev);
  272. }
  273. static const struct platform_device_id intel_m10bmc_hwmon_ids[] = {
  274. {
  275. .name = "n3000bmc-hwmon",
  276. .driver_data = (unsigned long)&n3000bmc_hwmon_bdata,
  277. },
  278. { }
  279. };
  280. static struct platform_driver intel_m10bmc_hwmon_driver = {
  281. .probe = m10bmc_hwmon_probe,
  282. .driver = {
  283. .name = "intel-m10-bmc-hwmon",
  284. },
  285. .id_table = intel_m10bmc_hwmon_ids,
  286. };
  287. module_platform_driver(intel_m10bmc_hwmon_driver);
  288. MODULE_DEVICE_TABLE(platform, intel_m10bmc_hwmon_ids);
  289. MODULE_AUTHOR("Intel Corporation");
  290. MODULE_DESCRIPTION("Intel MAX 10 BMC hardware monitor");
  291. MODULE_LICENSE("GPL");