core_hwmon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/core_hwmon.c
  3. * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/types.h>
  36. #include <linux/device.h>
  37. #include <linux/sysfs.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/err.h>
  40. #include "core.h"
  41. #define MLXSW_HWMON_TEMP_SENSOR_MAX_COUNT 127
  42. #define MLXSW_HWMON_ATTR_COUNT (MLXSW_HWMON_TEMP_SENSOR_MAX_COUNT * 4 + \
  43. MLXSW_MFCR_TACHOS_MAX + MLXSW_MFCR_PWMS_MAX)
  44. struct mlxsw_hwmon_attr {
  45. struct device_attribute dev_attr;
  46. struct mlxsw_hwmon *hwmon;
  47. unsigned int type_index;
  48. char name[32];
  49. };
  50. struct mlxsw_hwmon {
  51. struct mlxsw_core *core;
  52. const struct mlxsw_bus_info *bus_info;
  53. struct device *hwmon_dev;
  54. struct attribute_group group;
  55. const struct attribute_group *groups[2];
  56. struct attribute *attrs[MLXSW_HWMON_ATTR_COUNT + 1];
  57. struct mlxsw_hwmon_attr hwmon_attrs[MLXSW_HWMON_ATTR_COUNT];
  58. unsigned int attrs_count;
  59. };
  60. static ssize_t mlxsw_hwmon_temp_show(struct device *dev,
  61. struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
  65. container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
  66. struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
  67. char mtmp_pl[MLXSW_REG_MTMP_LEN];
  68. unsigned int temp;
  69. int err;
  70. mlxsw_reg_mtmp_pack(mtmp_pl, mlwsw_hwmon_attr->type_index,
  71. false, false);
  72. err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mtmp), mtmp_pl);
  73. if (err) {
  74. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query temp sensor\n");
  75. return err;
  76. }
  77. mlxsw_reg_mtmp_unpack(mtmp_pl, &temp, NULL, NULL);
  78. return sprintf(buf, "%u\n", temp);
  79. }
  80. static ssize_t mlxsw_hwmon_temp_max_show(struct device *dev,
  81. struct device_attribute *attr,
  82. char *buf)
  83. {
  84. struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
  85. container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
  86. struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
  87. char mtmp_pl[MLXSW_REG_MTMP_LEN];
  88. unsigned int temp_max;
  89. int err;
  90. mlxsw_reg_mtmp_pack(mtmp_pl, mlwsw_hwmon_attr->type_index,
  91. false, false);
  92. err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mtmp), mtmp_pl);
  93. if (err) {
  94. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query temp sensor\n");
  95. return err;
  96. }
  97. mlxsw_reg_mtmp_unpack(mtmp_pl, NULL, &temp_max, NULL);
  98. return sprintf(buf, "%u\n", temp_max);
  99. }
  100. static ssize_t mlxsw_hwmon_temp_rst_store(struct device *dev,
  101. struct device_attribute *attr,
  102. const char *buf, size_t len)
  103. {
  104. struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
  105. container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
  106. struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
  107. char mtmp_pl[MLXSW_REG_MTMP_LEN];
  108. unsigned long val;
  109. int err;
  110. err = kstrtoul(buf, 10, &val);
  111. if (err)
  112. return err;
  113. if (val != 1)
  114. return -EINVAL;
  115. mlxsw_reg_mtmp_pack(mtmp_pl, mlwsw_hwmon_attr->type_index, true, true);
  116. err = mlxsw_reg_write(mlxsw_hwmon->core, MLXSW_REG(mtmp), mtmp_pl);
  117. if (err) {
  118. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to reset temp sensor history\n");
  119. return err;
  120. }
  121. return len;
  122. }
  123. static ssize_t mlxsw_hwmon_fan_rpm_show(struct device *dev,
  124. struct device_attribute *attr,
  125. char *buf)
  126. {
  127. struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
  128. container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
  129. struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
  130. char mfsm_pl[MLXSW_REG_MFSM_LEN];
  131. int err;
  132. mlxsw_reg_mfsm_pack(mfsm_pl, mlwsw_hwmon_attr->type_index);
  133. err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mfsm), mfsm_pl);
  134. if (err) {
  135. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query fan\n");
  136. return err;
  137. }
  138. return sprintf(buf, "%u\n", mlxsw_reg_mfsm_rpm_get(mfsm_pl));
  139. }
  140. static ssize_t mlxsw_hwmon_pwm_show(struct device *dev,
  141. struct device_attribute *attr,
  142. char *buf)
  143. {
  144. struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
  145. container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
  146. struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
  147. char mfsc_pl[MLXSW_REG_MFSC_LEN];
  148. int err;
  149. mlxsw_reg_mfsc_pack(mfsc_pl, mlwsw_hwmon_attr->type_index, 0);
  150. err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mfsc), mfsc_pl);
  151. if (err) {
  152. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to query PWM\n");
  153. return err;
  154. }
  155. return sprintf(buf, "%u\n",
  156. mlxsw_reg_mfsc_pwm_duty_cycle_get(mfsc_pl));
  157. }
  158. static ssize_t mlxsw_hwmon_pwm_store(struct device *dev,
  159. struct device_attribute *attr,
  160. const char *buf, size_t len)
  161. {
  162. struct mlxsw_hwmon_attr *mlwsw_hwmon_attr =
  163. container_of(attr, struct mlxsw_hwmon_attr, dev_attr);
  164. struct mlxsw_hwmon *mlxsw_hwmon = mlwsw_hwmon_attr->hwmon;
  165. char mfsc_pl[MLXSW_REG_MFSC_LEN];
  166. unsigned long val;
  167. int err;
  168. err = kstrtoul(buf, 10, &val);
  169. if (err)
  170. return err;
  171. if (val > 255)
  172. return -EINVAL;
  173. mlxsw_reg_mfsc_pack(mfsc_pl, mlwsw_hwmon_attr->type_index, val);
  174. err = mlxsw_reg_write(mlxsw_hwmon->core, MLXSW_REG(mfsc), mfsc_pl);
  175. if (err) {
  176. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to write PWM\n");
  177. return err;
  178. }
  179. return len;
  180. }
  181. enum mlxsw_hwmon_attr_type {
  182. MLXSW_HWMON_ATTR_TYPE_TEMP,
  183. MLXSW_HWMON_ATTR_TYPE_TEMP_MAX,
  184. MLXSW_HWMON_ATTR_TYPE_TEMP_RST,
  185. MLXSW_HWMON_ATTR_TYPE_FAN_RPM,
  186. MLXSW_HWMON_ATTR_TYPE_PWM,
  187. };
  188. static void mlxsw_hwmon_attr_add(struct mlxsw_hwmon *mlxsw_hwmon,
  189. enum mlxsw_hwmon_attr_type attr_type,
  190. unsigned int type_index, unsigned int num) {
  191. struct mlxsw_hwmon_attr *mlxsw_hwmon_attr;
  192. unsigned int attr_index;
  193. attr_index = mlxsw_hwmon->attrs_count;
  194. mlxsw_hwmon_attr = &mlxsw_hwmon->hwmon_attrs[attr_index];
  195. switch (attr_type) {
  196. case MLXSW_HWMON_ATTR_TYPE_TEMP:
  197. mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_temp_show;
  198. mlxsw_hwmon_attr->dev_attr.attr.mode = S_IRUGO;
  199. snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
  200. "temp%u_input", num + 1);
  201. break;
  202. case MLXSW_HWMON_ATTR_TYPE_TEMP_MAX:
  203. mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_temp_max_show;
  204. mlxsw_hwmon_attr->dev_attr.attr.mode = S_IRUGO;
  205. snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
  206. "temp%u_highest", num + 1);
  207. break;
  208. case MLXSW_HWMON_ATTR_TYPE_TEMP_RST:
  209. mlxsw_hwmon_attr->dev_attr.store = mlxsw_hwmon_temp_rst_store;
  210. mlxsw_hwmon_attr->dev_attr.attr.mode = S_IWUSR;
  211. snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
  212. "temp%u_reset_history", num + 1);
  213. break;
  214. case MLXSW_HWMON_ATTR_TYPE_FAN_RPM:
  215. mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_fan_rpm_show;
  216. mlxsw_hwmon_attr->dev_attr.attr.mode = S_IRUGO;
  217. snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
  218. "fan%u_input", num + 1);
  219. break;
  220. case MLXSW_HWMON_ATTR_TYPE_PWM:
  221. mlxsw_hwmon_attr->dev_attr.show = mlxsw_hwmon_pwm_show;
  222. mlxsw_hwmon_attr->dev_attr.store = mlxsw_hwmon_pwm_store;
  223. mlxsw_hwmon_attr->dev_attr.attr.mode = S_IWUSR | S_IRUGO;
  224. snprintf(mlxsw_hwmon_attr->name, sizeof(mlxsw_hwmon_attr->name),
  225. "pwm%u", num + 1);
  226. break;
  227. default:
  228. WARN_ON(1);
  229. }
  230. mlxsw_hwmon_attr->type_index = type_index;
  231. mlxsw_hwmon_attr->hwmon = mlxsw_hwmon;
  232. mlxsw_hwmon_attr->dev_attr.attr.name = mlxsw_hwmon_attr->name;
  233. sysfs_attr_init(&mlxsw_hwmon_attr->dev_attr.attr);
  234. mlxsw_hwmon->attrs[attr_index] = &mlxsw_hwmon_attr->dev_attr.attr;
  235. mlxsw_hwmon->attrs_count++;
  236. }
  237. static int mlxsw_hwmon_temp_init(struct mlxsw_hwmon *mlxsw_hwmon)
  238. {
  239. char mtcap_pl[MLXSW_REG_MTCAP_LEN];
  240. char mtmp_pl[MLXSW_REG_MTMP_LEN];
  241. u8 sensor_count;
  242. int i;
  243. int err;
  244. err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mtcap), mtcap_pl);
  245. if (err) {
  246. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to get number of temp sensors\n");
  247. return err;
  248. }
  249. sensor_count = mlxsw_reg_mtcap_sensor_count_get(mtcap_pl);
  250. for (i = 0; i < sensor_count; i++) {
  251. mlxsw_reg_mtmp_pack(mtmp_pl, i, true, true);
  252. err = mlxsw_reg_write(mlxsw_hwmon->core,
  253. MLXSW_REG(mtmp), mtmp_pl);
  254. if (err) {
  255. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to setup temp sensor number %d\n",
  256. i);
  257. return err;
  258. }
  259. mlxsw_hwmon_attr_add(mlxsw_hwmon,
  260. MLXSW_HWMON_ATTR_TYPE_TEMP, i, i);
  261. mlxsw_hwmon_attr_add(mlxsw_hwmon,
  262. MLXSW_HWMON_ATTR_TYPE_TEMP_MAX, i, i);
  263. mlxsw_hwmon_attr_add(mlxsw_hwmon,
  264. MLXSW_HWMON_ATTR_TYPE_TEMP_RST, i, i);
  265. }
  266. return 0;
  267. }
  268. static int mlxsw_hwmon_fans_init(struct mlxsw_hwmon *mlxsw_hwmon)
  269. {
  270. char mfcr_pl[MLXSW_REG_MFCR_LEN];
  271. enum mlxsw_reg_mfcr_pwm_frequency freq;
  272. unsigned int type_index;
  273. unsigned int num;
  274. u16 tacho_active;
  275. u8 pwm_active;
  276. int err;
  277. err = mlxsw_reg_query(mlxsw_hwmon->core, MLXSW_REG(mfcr), mfcr_pl);
  278. if (err) {
  279. dev_err(mlxsw_hwmon->bus_info->dev, "Failed to get to probe PWMs and Tachometers\n");
  280. return err;
  281. }
  282. mlxsw_reg_mfcr_unpack(mfcr_pl, &freq, &tacho_active, &pwm_active);
  283. num = 0;
  284. for (type_index = 0; type_index < MLXSW_MFCR_TACHOS_MAX; type_index++) {
  285. if (tacho_active & BIT(type_index))
  286. mlxsw_hwmon_attr_add(mlxsw_hwmon,
  287. MLXSW_HWMON_ATTR_TYPE_FAN_RPM,
  288. type_index, num++);
  289. }
  290. num = 0;
  291. for (type_index = 0; type_index < MLXSW_MFCR_PWMS_MAX; type_index++) {
  292. if (pwm_active & BIT(type_index))
  293. mlxsw_hwmon_attr_add(mlxsw_hwmon,
  294. MLXSW_HWMON_ATTR_TYPE_PWM,
  295. type_index, num++);
  296. }
  297. return 0;
  298. }
  299. int mlxsw_hwmon_init(struct mlxsw_core *mlxsw_core,
  300. const struct mlxsw_bus_info *mlxsw_bus_info,
  301. struct mlxsw_hwmon **p_hwmon)
  302. {
  303. struct mlxsw_hwmon *mlxsw_hwmon;
  304. struct device *hwmon_dev;
  305. int err;
  306. mlxsw_hwmon = devm_kzalloc(mlxsw_bus_info->dev, sizeof(*mlxsw_hwmon),
  307. GFP_KERNEL);
  308. if (!mlxsw_hwmon)
  309. return -ENOMEM;
  310. mlxsw_hwmon->core = mlxsw_core;
  311. mlxsw_hwmon->bus_info = mlxsw_bus_info;
  312. err = mlxsw_hwmon_temp_init(mlxsw_hwmon);
  313. if (err)
  314. goto err_temp_init;
  315. err = mlxsw_hwmon_fans_init(mlxsw_hwmon);
  316. if (err)
  317. goto err_fans_init;
  318. mlxsw_hwmon->groups[0] = &mlxsw_hwmon->group;
  319. mlxsw_hwmon->group.attrs = mlxsw_hwmon->attrs;
  320. hwmon_dev = devm_hwmon_device_register_with_groups(mlxsw_bus_info->dev,
  321. "mlxsw",
  322. mlxsw_hwmon,
  323. mlxsw_hwmon->groups);
  324. if (IS_ERR(hwmon_dev)) {
  325. err = PTR_ERR(hwmon_dev);
  326. goto err_hwmon_register;
  327. }
  328. mlxsw_hwmon->hwmon_dev = hwmon_dev;
  329. *p_hwmon = mlxsw_hwmon;
  330. return 0;
  331. err_hwmon_register:
  332. err_fans_init:
  333. err_temp_init:
  334. return err;
  335. }