emc2305.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Hardware monitoring driver for EMC2305 fan controller
  4. *
  5. * Copyright (C) 2022 Nvidia Technologies Ltd.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/hwmon.h>
  9. #include <linux/i2c.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_data/emc2305.h>
  12. #include <linux/thermal.h>
  13. static const unsigned short
  14. emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
  15. #define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
  16. #define EMC2305_REG_VENDOR 0xfe
  17. #define EMC2305_FAN_MAX 0xff
  18. #define EMC2305_FAN_MIN 0x00
  19. #define EMC2305_FAN_MAX_STATE 10
  20. #define EMC2305_DEVICE 0x34
  21. #define EMC2305_VENDOR 0x5d
  22. #define EMC2305_REG_PRODUCT_ID 0xfd
  23. #define EMC2305_TACH_REGS_UNUSE_BITS 3
  24. #define EMC2305_TACH_CNT_MULTIPLIER 0x02
  25. #define EMC2305_TACH_RANGE_MIN 480
  26. #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
  27. DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max))
  28. #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
  29. DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state))
  30. /*
  31. * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
  32. * equal (poles * 2 + 1).
  33. */
  34. #define EMC2305_RPM_FACTOR 3932160
  35. #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
  36. #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
  37. #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
  38. enum emc230x_product_id {
  39. EMC2305 = 0x34,
  40. EMC2303 = 0x35,
  41. EMC2302 = 0x36,
  42. EMC2301 = 0x37,
  43. };
  44. static const struct i2c_device_id emc2305_ids[] = {
  45. { "emc2305", 0 },
  46. { "emc2303", 0 },
  47. { "emc2302", 0 },
  48. { "emc2301", 0 },
  49. { }
  50. };
  51. MODULE_DEVICE_TABLE(i2c, emc2305_ids);
  52. /**
  53. * @cdev: cooling device;
  54. * @curr_state: cooling current state;
  55. * @last_hwmon_state: last cooling state updated by hwmon subsystem;
  56. * @last_thermal_state: last cooling state updated by thermal subsystem;
  57. *
  58. * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
  59. * speed feature. The purpose of this feature is to provides ability to limit fan speed
  60. * according to some system wise considerations, like absence of some replaceable units (PSU or
  61. * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
  62. * some other factors which indirectly impacts system's airflow
  63. * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
  64. * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
  65. * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
  66. * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
  67. * attribute.
  68. * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
  69. * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
  70. * speed will be just stored with no PWM update.
  71. */
  72. struct emc2305_cdev_data {
  73. struct thermal_cooling_device *cdev;
  74. unsigned int cur_state;
  75. unsigned long last_hwmon_state;
  76. unsigned long last_thermal_state;
  77. };
  78. /**
  79. * @client: i2c client;
  80. * @hwmon_dev: hwmon device;
  81. * @max_state: maximum cooling state of the cooling device;
  82. * @pwm_num: number of PWM channels;
  83. * @pwm_separate: separate PWM settings for every channel;
  84. * @pwm_min: array of minimum PWM per channel;
  85. * @cdev_data: array of cooling devices data;
  86. */
  87. struct emc2305_data {
  88. struct i2c_client *client;
  89. struct device *hwmon_dev;
  90. u8 max_state;
  91. u8 pwm_num;
  92. bool pwm_separate;
  93. u8 pwm_min[EMC2305_PWM_MAX];
  94. struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
  95. };
  96. static char *emc2305_fan_name[] = {
  97. "emc2305_fan",
  98. "emc2305_fan1",
  99. "emc2305_fan2",
  100. "emc2305_fan3",
  101. "emc2305_fan4",
  102. "emc2305_fan5",
  103. };
  104. static void emc2305_unset_tz(struct device *dev);
  105. static int emc2305_get_max_channel(const struct emc2305_data *data)
  106. {
  107. return data->pwm_num;
  108. }
  109. static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
  110. {
  111. struct emc2305_data *data = cdev->devdata;
  112. size_t len = strlen(cdev->type);
  113. int ret;
  114. if (len <= 0)
  115. return -EINVAL;
  116. /*
  117. * Returns index of cooling device 0..4 in case of separate PWM setting.
  118. * Zero index is used in case of one common PWM setting.
  119. * If the mode is not set as pwm_separate, all PWMs are to be bound
  120. * to the common thermal zone and should work at the same speed
  121. * to perform cooling for the same thermal junction.
  122. * Otherwise, return specific channel that will be used in bound
  123. * related PWM to the thermal zone.
  124. */
  125. if (!data->pwm_separate)
  126. return 0;
  127. ret = cdev->type[len - 1];
  128. switch (ret) {
  129. case '1' ... '5':
  130. return ret - '1';
  131. default:
  132. break;
  133. }
  134. return -EINVAL;
  135. }
  136. static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  137. {
  138. int cdev_idx;
  139. struct emc2305_data *data = cdev->devdata;
  140. cdev_idx = emc2305_get_cdev_idx(cdev);
  141. if (cdev_idx < 0)
  142. return cdev_idx;
  143. *state = data->cdev_data[cdev_idx].cur_state;
  144. return 0;
  145. }
  146. static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  147. {
  148. struct emc2305_data *data = cdev->devdata;
  149. *state = data->max_state;
  150. return 0;
  151. }
  152. static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state)
  153. {
  154. int ret;
  155. struct i2c_client *client = data->client;
  156. u8 val, i;
  157. state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
  158. val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
  159. data->cdev_data[cdev_idx].cur_state = state;
  160. if (data->pwm_separate) {
  161. ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
  162. if (ret < 0)
  163. return ret;
  164. } else {
  165. /*
  166. * Set the same PWM value in all channels
  167. * if common PWM channel is used.
  168. */
  169. for (i = 0; i < data->pwm_num; i++) {
  170. ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
  171. if (ret < 0)
  172. return ret;
  173. }
  174. }
  175. return 0;
  176. }
  177. static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  178. {
  179. int cdev_idx, ret;
  180. struct emc2305_data *data = cdev->devdata;
  181. if (state > data->max_state)
  182. return -EINVAL;
  183. cdev_idx = emc2305_get_cdev_idx(cdev);
  184. if (cdev_idx < 0)
  185. return cdev_idx;
  186. /* Save thermal state. */
  187. data->cdev_data[cdev_idx].last_thermal_state = state;
  188. ret = __emc2305_set_cur_state(data, cdev_idx, state);
  189. if (ret < 0)
  190. return ret;
  191. return 0;
  192. }
  193. static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
  194. .get_max_state = emc2305_get_max_state,
  195. .get_cur_state = emc2305_get_cur_state,
  196. .set_cur_state = emc2305_set_cur_state,
  197. };
  198. static int emc2305_show_fault(struct device *dev, int channel)
  199. {
  200. struct emc2305_data *data = dev_get_drvdata(dev);
  201. struct i2c_client *client = data->client;
  202. int status_reg;
  203. status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
  204. if (status_reg < 0)
  205. return status_reg;
  206. return status_reg & (1 << channel) ? 1 : 0;
  207. }
  208. static int emc2305_show_fan(struct device *dev, int channel)
  209. {
  210. struct emc2305_data *data = dev_get_drvdata(dev);
  211. struct i2c_client *client = data->client;
  212. int ret;
  213. ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
  214. if (ret <= 0)
  215. return ret;
  216. ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
  217. ret = EMC2305_RPM_FACTOR / ret;
  218. if (ret <= EMC2305_TACH_RANGE_MIN)
  219. return 0;
  220. return ret * EMC2305_TACH_CNT_MULTIPLIER;
  221. }
  222. static int emc2305_show_pwm(struct device *dev, int channel)
  223. {
  224. struct emc2305_data *data = dev_get_drvdata(dev);
  225. struct i2c_client *client = data->client;
  226. return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
  227. }
  228. static int emc2305_set_pwm(struct device *dev, long val, int channel)
  229. {
  230. struct emc2305_data *data = dev_get_drvdata(dev);
  231. struct i2c_client *client = data->client;
  232. int ret;
  233. if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
  234. return -EINVAL;
  235. ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
  236. if (ret < 0)
  237. return ret;
  238. data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
  239. EMC2305_FAN_MAX);
  240. return 0;
  241. }
  242. static int emc2305_set_single_tz(struct device *dev, int idx)
  243. {
  244. struct emc2305_data *data = dev_get_drvdata(dev);
  245. long pwm;
  246. int i, cdev_idx, ret;
  247. cdev_idx = (idx) ? idx - 1 : 0;
  248. pwm = data->pwm_min[cdev_idx];
  249. data->cdev_data[cdev_idx].cdev =
  250. thermal_cooling_device_register(emc2305_fan_name[idx], data,
  251. &emc2305_cooling_ops);
  252. if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
  253. dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
  254. return PTR_ERR(data->cdev_data[cdev_idx].cdev);
  255. }
  256. /* Set minimal PWM speed. */
  257. if (data->pwm_separate) {
  258. ret = emc2305_set_pwm(dev, pwm, cdev_idx);
  259. if (ret < 0)
  260. return ret;
  261. } else {
  262. for (i = 0; i < data->pwm_num; i++) {
  263. ret = emc2305_set_pwm(dev, pwm, i);
  264. if (ret < 0)
  265. return ret;
  266. }
  267. }
  268. data->cdev_data[cdev_idx].cur_state =
  269. EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
  270. EMC2305_FAN_MAX);
  271. data->cdev_data[cdev_idx].last_hwmon_state =
  272. EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
  273. EMC2305_FAN_MAX);
  274. return 0;
  275. }
  276. static int emc2305_set_tz(struct device *dev)
  277. {
  278. struct emc2305_data *data = dev_get_drvdata(dev);
  279. int i, ret;
  280. if (!data->pwm_separate)
  281. return emc2305_set_single_tz(dev, 0);
  282. for (i = 0; i < data->pwm_num; i++) {
  283. ret = emc2305_set_single_tz(dev, i + 1);
  284. if (ret)
  285. goto thermal_cooling_device_register_fail;
  286. }
  287. return 0;
  288. thermal_cooling_device_register_fail:
  289. emc2305_unset_tz(dev);
  290. return ret;
  291. }
  292. static void emc2305_unset_tz(struct device *dev)
  293. {
  294. struct emc2305_data *data = dev_get_drvdata(dev);
  295. int i;
  296. /* Unregister cooling device. */
  297. for (i = 0; i < EMC2305_PWM_MAX; i++)
  298. if (data->cdev_data[i].cdev)
  299. thermal_cooling_device_unregister(data->cdev_data[i].cdev);
  300. }
  301. static umode_t
  302. emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
  303. {
  304. int max_channel = emc2305_get_max_channel(data);
  305. /* Don't show channels which are not physically connected. */
  306. if (channel >= max_channel)
  307. return 0;
  308. switch (type) {
  309. case hwmon_fan:
  310. switch (attr) {
  311. case hwmon_fan_input:
  312. return 0444;
  313. case hwmon_fan_fault:
  314. return 0444;
  315. default:
  316. break;
  317. }
  318. break;
  319. case hwmon_pwm:
  320. switch (attr) {
  321. case hwmon_pwm_input:
  322. return 0644;
  323. default:
  324. break;
  325. }
  326. break;
  327. default:
  328. break;
  329. }
  330. return 0;
  331. };
  332. static int
  333. emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
  334. {
  335. struct emc2305_data *data = dev_get_drvdata(dev);
  336. int cdev_idx;
  337. switch (type) {
  338. case hwmon_pwm:
  339. switch (attr) {
  340. case hwmon_pwm_input:
  341. /* If thermal is configured - handle PWM limit setting. */
  342. if (IS_REACHABLE(CONFIG_THERMAL)) {
  343. if (data->pwm_separate)
  344. cdev_idx = channel;
  345. else
  346. cdev_idx = 0;
  347. data->cdev_data[cdev_idx].last_hwmon_state =
  348. EMC2305_PWM_DUTY2STATE(val, data->max_state,
  349. EMC2305_FAN_MAX);
  350. /*
  351. * Update PWM only in case requested state is not less than the
  352. * last thermal state.
  353. */
  354. if (data->cdev_data[cdev_idx].last_hwmon_state >=
  355. data->cdev_data[cdev_idx].last_thermal_state)
  356. return __emc2305_set_cur_state(data, cdev_idx,
  357. data->cdev_data[cdev_idx].last_hwmon_state);
  358. return 0;
  359. }
  360. return emc2305_set_pwm(dev, val, channel);
  361. default:
  362. break;
  363. }
  364. break;
  365. default:
  366. break;
  367. }
  368. return -EOPNOTSUPP;
  369. };
  370. static int
  371. emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
  372. {
  373. int ret;
  374. switch (type) {
  375. case hwmon_fan:
  376. switch (attr) {
  377. case hwmon_fan_input:
  378. ret = emc2305_show_fan(dev, channel);
  379. if (ret < 0)
  380. return ret;
  381. *val = ret;
  382. return 0;
  383. case hwmon_fan_fault:
  384. ret = emc2305_show_fault(dev, channel);
  385. if (ret < 0)
  386. return ret;
  387. *val = ret;
  388. return 0;
  389. default:
  390. break;
  391. }
  392. break;
  393. case hwmon_pwm:
  394. switch (attr) {
  395. case hwmon_pwm_input:
  396. ret = emc2305_show_pwm(dev, channel);
  397. if (ret < 0)
  398. return ret;
  399. *val = ret;
  400. return 0;
  401. default:
  402. break;
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. return -EOPNOTSUPP;
  409. };
  410. static const struct hwmon_ops emc2305_ops = {
  411. .is_visible = emc2305_is_visible,
  412. .read = emc2305_read,
  413. .write = emc2305_write,
  414. };
  415. static const struct hwmon_channel_info *emc2305_info[] = {
  416. HWMON_CHANNEL_INFO(fan,
  417. HWMON_F_INPUT | HWMON_F_FAULT,
  418. HWMON_F_INPUT | HWMON_F_FAULT,
  419. HWMON_F_INPUT | HWMON_F_FAULT,
  420. HWMON_F_INPUT | HWMON_F_FAULT,
  421. HWMON_F_INPUT | HWMON_F_FAULT),
  422. HWMON_CHANNEL_INFO(pwm,
  423. HWMON_PWM_INPUT,
  424. HWMON_PWM_INPUT,
  425. HWMON_PWM_INPUT,
  426. HWMON_PWM_INPUT,
  427. HWMON_PWM_INPUT),
  428. NULL
  429. };
  430. static const struct hwmon_chip_info emc2305_chip_info = {
  431. .ops = &emc2305_ops,
  432. .info = emc2305_info,
  433. };
  434. static int emc2305_identify(struct device *dev)
  435. {
  436. struct i2c_client *client = to_i2c_client(dev);
  437. struct emc2305_data *data = i2c_get_clientdata(client);
  438. int ret;
  439. ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
  440. if (ret < 0)
  441. return ret;
  442. switch (ret) {
  443. case EMC2305:
  444. data->pwm_num = 5;
  445. break;
  446. case EMC2303:
  447. data->pwm_num = 3;
  448. break;
  449. case EMC2302:
  450. data->pwm_num = 2;
  451. break;
  452. case EMC2301:
  453. data->pwm_num = 1;
  454. break;
  455. default:
  456. return -ENODEV;
  457. }
  458. return 0;
  459. }
  460. static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
  461. {
  462. struct i2c_adapter *adapter = client->adapter;
  463. struct device *dev = &client->dev;
  464. struct emc2305_data *data;
  465. struct emc2305_platform_data *pdata;
  466. int vendor;
  467. int ret;
  468. int i;
  469. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  470. return -ENODEV;
  471. vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
  472. if (vendor != EMC2305_VENDOR)
  473. return -ENODEV;
  474. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  475. if (!data)
  476. return -ENOMEM;
  477. i2c_set_clientdata(client, data);
  478. data->client = client;
  479. ret = emc2305_identify(dev);
  480. if (ret)
  481. return ret;
  482. pdata = dev_get_platdata(&client->dev);
  483. if (pdata) {
  484. if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
  485. return -EINVAL;
  486. data->max_state = pdata->max_state;
  487. /*
  488. * Validate a number of active PWM channels. Note that
  489. * configured number can be less than the actual maximum
  490. * supported by the device.
  491. */
  492. if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
  493. return -EINVAL;
  494. data->pwm_num = pdata->pwm_num;
  495. data->pwm_separate = pdata->pwm_separate;
  496. for (i = 0; i < EMC2305_PWM_MAX; i++)
  497. data->pwm_min[i] = pdata->pwm_min[i];
  498. } else {
  499. data->max_state = EMC2305_FAN_MAX_STATE;
  500. data->pwm_separate = false;
  501. for (i = 0; i < EMC2305_PWM_MAX; i++)
  502. data->pwm_min[i] = EMC2305_FAN_MIN;
  503. }
  504. data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
  505. &emc2305_chip_info, NULL);
  506. if (IS_ERR(data->hwmon_dev))
  507. return PTR_ERR(data->hwmon_dev);
  508. if (IS_REACHABLE(CONFIG_THERMAL)) {
  509. ret = emc2305_set_tz(dev);
  510. if (ret != 0)
  511. return ret;
  512. }
  513. for (i = 0; i < data->pwm_num; i++) {
  514. ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
  515. data->pwm_min[i]);
  516. if (ret < 0)
  517. return ret;
  518. }
  519. return 0;
  520. }
  521. static void emc2305_remove(struct i2c_client *client)
  522. {
  523. struct device *dev = &client->dev;
  524. if (IS_REACHABLE(CONFIG_THERMAL))
  525. emc2305_unset_tz(dev);
  526. }
  527. static struct i2c_driver emc2305_driver = {
  528. .class = I2C_CLASS_HWMON,
  529. .driver = {
  530. .name = "emc2305",
  531. },
  532. .probe = emc2305_probe,
  533. .remove = emc2305_remove,
  534. .id_table = emc2305_ids,
  535. .address_list = emc2305_normal_i2c,
  536. };
  537. module_i2c_driver(emc2305_driver);
  538. MODULE_AUTHOR("Nvidia");
  539. MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
  540. MODULE_LICENSE("GPL");