tc654.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tc654.c - Linux kernel modules for fan speed controller
  4. *
  5. * Copyright (C) 2016 Allied Telesis Labs NZ
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/err.h>
  9. #include <linux/hwmon.h>
  10. #include <linux/hwmon-sysfs.h>
  11. #include <linux/i2c.h>
  12. #include <linux/init.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/thermal.h>
  18. #include <linux/util_macros.h>
  19. enum tc654_regs {
  20. TC654_REG_RPM1 = 0x00, /* RPM Output 1 */
  21. TC654_REG_RPM2 = 0x01, /* RPM Output 2 */
  22. TC654_REG_FAN_FAULT1 = 0x02, /* Fan Fault 1 Threshold */
  23. TC654_REG_FAN_FAULT2 = 0x03, /* Fan Fault 2 Threshold */
  24. TC654_REG_CONFIG = 0x04, /* Configuration */
  25. TC654_REG_STATUS = 0x05, /* Status */
  26. TC654_REG_DUTY_CYCLE = 0x06, /* Fan Speed Duty Cycle */
  27. TC654_REG_MFR_ID = 0x07, /* Manufacturer Identification */
  28. TC654_REG_VER_ID = 0x08, /* Version Identification */
  29. };
  30. /* Macros to easily index the registers */
  31. #define TC654_REG_RPM(idx) (TC654_REG_RPM1 + (idx))
  32. #define TC654_REG_FAN_FAULT(idx) (TC654_REG_FAN_FAULT1 + (idx))
  33. /* Config register bits */
  34. #define TC654_REG_CONFIG_RES BIT(6) /* Resolution Selection */
  35. #define TC654_REG_CONFIG_DUTYC BIT(5) /* Duty Cycle Control */
  36. #define TC654_REG_CONFIG_SDM BIT(0) /* Shutdown Mode */
  37. /* Status register bits */
  38. #define TC654_REG_STATUS_F2F BIT(1) /* Fan 2 Fault */
  39. #define TC654_REG_STATUS_F1F BIT(0) /* Fan 1 Fault */
  40. /* RPM resolution for RPM Output registers */
  41. #define TC654_HIGH_RPM_RESOLUTION 25 /* 25 RPM resolution */
  42. #define TC654_LOW_RPM_RESOLUTION 50 /* 50 RPM resolution */
  43. /* Convert to the fan fault RPM threshold from register value */
  44. #define TC654_FAN_FAULT_FROM_REG(val) ((val) * 50) /* 50 RPM resolution */
  45. /* Convert to register value from the fan fault RPM threshold */
  46. #define TC654_FAN_FAULT_TO_REG(val) (((val) / 50) & 0xff)
  47. /* Register data is read (and cached) at most once per second. */
  48. #define TC654_UPDATE_INTERVAL HZ
  49. struct tc654_data {
  50. struct i2c_client *client;
  51. /* update mutex */
  52. struct mutex update_lock;
  53. /* tc654 register cache */
  54. bool valid;
  55. unsigned long last_updated; /* in jiffies */
  56. u8 rpm_output[2]; /* The fan RPM data for fans 1 and 2 is then
  57. * written to registers RPM1 and RPM2
  58. */
  59. u8 fan_fault[2]; /* The Fan Fault Threshold Registers are used to
  60. * set the fan fault threshold levels for fan 1
  61. * and fan 2
  62. */
  63. u8 config; /* The Configuration Register is an 8-bit read/
  64. * writable multi-function control register
  65. * 7: Fan Fault Clear
  66. * 1 = Clear Fan Fault
  67. * 0 = Normal Operation (default)
  68. * 6: Resolution Selection for RPM Output Registers
  69. * RPM Output Registers (RPM1 and RPM2) will be
  70. * set for
  71. * 1 = 25 RPM (9-bit) resolution
  72. * 0 = 50 RPM (8-bit) resolution (default)
  73. * 5: Duty Cycle Control Method
  74. * The V OUT duty cycle will be controlled via
  75. * 1 = the SMBus interface.
  76. * 0 = via the V IN analog input pin. (default)
  77. * 4,3: Fan 2 Pulses Per Rotation
  78. * 00 = 1
  79. * 01 = 2 (default)
  80. * 10 = 4
  81. * 11 = 8
  82. * 2,1: Fan 1 Pulses Per Rotation
  83. * 00 = 1
  84. * 01 = 2 (default)
  85. * 10 = 4
  86. * 11 = 8
  87. * 0: Shutdown Mode
  88. * 1 = Shutdown mode.
  89. * 0 = Normal operation. (default)
  90. */
  91. u8 status; /* The Status register provides all the information
  92. * about what is going on within the TC654/TC655
  93. * devices.
  94. * 7,6: Unimplemented, Read as '0'
  95. * 5: Over-Temperature Fault Condition
  96. * 1 = Over-Temperature condition has occurred
  97. * 0 = Normal operation. V IN is less than 2.6V
  98. * 4: RPM2 Counter Overflow
  99. * 1 = Fault condition
  100. * 0 = Normal operation
  101. * 3: RPM1 Counter Overflow
  102. * 1 = Fault condition
  103. * 0 = Normal operation
  104. * 2: V IN Input Status
  105. * 1 = V IN is open
  106. * 0 = Normal operation. voltage present at V IN
  107. * 1: Fan 2 Fault
  108. * 1 = Fault condition
  109. * 0 = Normal operation
  110. * 0: Fan 1 Fault
  111. * 1 = Fault condition
  112. * 0 = Normal operation
  113. */
  114. u8 duty_cycle; /* The DUTY_CYCLE register is a 4-bit read/
  115. * writable register used to control the duty
  116. * cycle of the V OUT output.
  117. */
  118. };
  119. /* helper to grab and cache data, at most one time per second */
  120. static struct tc654_data *tc654_update_client(struct device *dev)
  121. {
  122. struct tc654_data *data = dev_get_drvdata(dev);
  123. struct i2c_client *client = data->client;
  124. int ret = 0;
  125. mutex_lock(&data->update_lock);
  126. if (time_before(jiffies, data->last_updated + TC654_UPDATE_INTERVAL) &&
  127. likely(data->valid))
  128. goto out;
  129. ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(0));
  130. if (ret < 0)
  131. goto out;
  132. data->rpm_output[0] = ret;
  133. ret = i2c_smbus_read_byte_data(client, TC654_REG_RPM(1));
  134. if (ret < 0)
  135. goto out;
  136. data->rpm_output[1] = ret;
  137. ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(0));
  138. if (ret < 0)
  139. goto out;
  140. data->fan_fault[0] = ret;
  141. ret = i2c_smbus_read_byte_data(client, TC654_REG_FAN_FAULT(1));
  142. if (ret < 0)
  143. goto out;
  144. data->fan_fault[1] = ret;
  145. ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
  146. if (ret < 0)
  147. goto out;
  148. data->config = ret;
  149. ret = i2c_smbus_read_byte_data(client, TC654_REG_STATUS);
  150. if (ret < 0)
  151. goto out;
  152. data->status = ret;
  153. ret = i2c_smbus_read_byte_data(client, TC654_REG_DUTY_CYCLE);
  154. if (ret < 0)
  155. goto out;
  156. data->duty_cycle = ret & 0x0f;
  157. data->last_updated = jiffies;
  158. data->valid = true;
  159. out:
  160. mutex_unlock(&data->update_lock);
  161. if (ret < 0) /* upon error, encode it in return value */
  162. data = ERR_PTR(ret);
  163. return data;
  164. }
  165. /*
  166. * sysfs attributes
  167. */
  168. static ssize_t fan_show(struct device *dev, struct device_attribute *da,
  169. char *buf)
  170. {
  171. int nr = to_sensor_dev_attr(da)->index;
  172. struct tc654_data *data = tc654_update_client(dev);
  173. int val;
  174. if (IS_ERR(data))
  175. return PTR_ERR(data);
  176. if (data->config & TC654_REG_CONFIG_RES)
  177. val = data->rpm_output[nr] * TC654_HIGH_RPM_RESOLUTION;
  178. else
  179. val = data->rpm_output[nr] * TC654_LOW_RPM_RESOLUTION;
  180. return sprintf(buf, "%d\n", val);
  181. }
  182. static ssize_t fan_min_show(struct device *dev, struct device_attribute *da,
  183. char *buf)
  184. {
  185. int nr = to_sensor_dev_attr(da)->index;
  186. struct tc654_data *data = tc654_update_client(dev);
  187. if (IS_ERR(data))
  188. return PTR_ERR(data);
  189. return sprintf(buf, "%d\n",
  190. TC654_FAN_FAULT_FROM_REG(data->fan_fault[nr]));
  191. }
  192. static ssize_t fan_min_store(struct device *dev, struct device_attribute *da,
  193. const char *buf, size_t count)
  194. {
  195. int nr = to_sensor_dev_attr(da)->index;
  196. struct tc654_data *data = dev_get_drvdata(dev);
  197. struct i2c_client *client = data->client;
  198. unsigned long val;
  199. int ret;
  200. if (kstrtoul(buf, 10, &val))
  201. return -EINVAL;
  202. val = clamp_val(val, 0, 12750);
  203. mutex_lock(&data->update_lock);
  204. data->fan_fault[nr] = TC654_FAN_FAULT_TO_REG(val);
  205. ret = i2c_smbus_write_byte_data(client, TC654_REG_FAN_FAULT(nr),
  206. data->fan_fault[nr]);
  207. mutex_unlock(&data->update_lock);
  208. return ret < 0 ? ret : count;
  209. }
  210. static ssize_t fan_alarm_show(struct device *dev, struct device_attribute *da,
  211. char *buf)
  212. {
  213. int nr = to_sensor_dev_attr(da)->index;
  214. struct tc654_data *data = tc654_update_client(dev);
  215. int val;
  216. if (IS_ERR(data))
  217. return PTR_ERR(data);
  218. if (nr == 0)
  219. val = !!(data->status & TC654_REG_STATUS_F1F);
  220. else
  221. val = !!(data->status & TC654_REG_STATUS_F2F);
  222. return sprintf(buf, "%d\n", val);
  223. }
  224. static const u8 TC654_FAN_PULSE_SHIFT[] = { 1, 3 };
  225. static ssize_t fan_pulses_show(struct device *dev,
  226. struct device_attribute *da, char *buf)
  227. {
  228. int nr = to_sensor_dev_attr(da)->index;
  229. struct tc654_data *data = tc654_update_client(dev);
  230. u8 val;
  231. if (IS_ERR(data))
  232. return PTR_ERR(data);
  233. val = BIT((data->config >> TC654_FAN_PULSE_SHIFT[nr]) & 0x03);
  234. return sprintf(buf, "%d\n", val);
  235. }
  236. static ssize_t fan_pulses_store(struct device *dev,
  237. struct device_attribute *da, const char *buf,
  238. size_t count)
  239. {
  240. int nr = to_sensor_dev_attr(da)->index;
  241. struct tc654_data *data = dev_get_drvdata(dev);
  242. struct i2c_client *client = data->client;
  243. u8 config;
  244. unsigned long val;
  245. int ret;
  246. if (kstrtoul(buf, 10, &val))
  247. return -EINVAL;
  248. switch (val) {
  249. case 1:
  250. config = 0;
  251. break;
  252. case 2:
  253. config = 1;
  254. break;
  255. case 4:
  256. config = 2;
  257. break;
  258. case 8:
  259. config = 3;
  260. break;
  261. default:
  262. return -EINVAL;
  263. }
  264. mutex_lock(&data->update_lock);
  265. data->config &= ~(0x03 << TC654_FAN_PULSE_SHIFT[nr]);
  266. data->config |= (config << TC654_FAN_PULSE_SHIFT[nr]);
  267. ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
  268. mutex_unlock(&data->update_lock);
  269. return ret < 0 ? ret : count;
  270. }
  271. static ssize_t pwm_mode_show(struct device *dev, struct device_attribute *da,
  272. char *buf)
  273. {
  274. struct tc654_data *data = tc654_update_client(dev);
  275. if (IS_ERR(data))
  276. return PTR_ERR(data);
  277. return sprintf(buf, "%d\n", !!(data->config & TC654_REG_CONFIG_DUTYC));
  278. }
  279. static ssize_t pwm_mode_store(struct device *dev, struct device_attribute *da,
  280. const char *buf, size_t count)
  281. {
  282. struct tc654_data *data = dev_get_drvdata(dev);
  283. struct i2c_client *client = data->client;
  284. unsigned long val;
  285. int ret;
  286. if (kstrtoul(buf, 10, &val))
  287. return -EINVAL;
  288. if (val != 0 && val != 1)
  289. return -EINVAL;
  290. mutex_lock(&data->update_lock);
  291. if (val)
  292. data->config |= TC654_REG_CONFIG_DUTYC;
  293. else
  294. data->config &= ~TC654_REG_CONFIG_DUTYC;
  295. ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
  296. mutex_unlock(&data->update_lock);
  297. return ret < 0 ? ret : count;
  298. }
  299. static const int tc654_pwm_map[16] = { 77, 88, 102, 112, 124, 136, 148, 160,
  300. 172, 184, 196, 207, 219, 231, 243, 255};
  301. static ssize_t pwm_show(struct device *dev, struct device_attribute *da,
  302. char *buf)
  303. {
  304. struct tc654_data *data = tc654_update_client(dev);
  305. int pwm;
  306. if (IS_ERR(data))
  307. return PTR_ERR(data);
  308. if (data->config & TC654_REG_CONFIG_SDM)
  309. pwm = 0;
  310. else
  311. pwm = tc654_pwm_map[data->duty_cycle];
  312. return sprintf(buf, "%d\n", pwm);
  313. }
  314. static int _set_pwm(struct tc654_data *data, unsigned long val)
  315. {
  316. struct i2c_client *client = data->client;
  317. int ret;
  318. mutex_lock(&data->update_lock);
  319. if (val == 0) {
  320. data->config |= TC654_REG_CONFIG_SDM;
  321. data->duty_cycle = 0;
  322. } else {
  323. data->config &= ~TC654_REG_CONFIG_SDM;
  324. data->duty_cycle = val - 1;
  325. }
  326. ret = i2c_smbus_write_byte_data(client, TC654_REG_CONFIG, data->config);
  327. if (ret < 0)
  328. goto out;
  329. ret = i2c_smbus_write_byte_data(client, TC654_REG_DUTY_CYCLE,
  330. data->duty_cycle);
  331. out:
  332. mutex_unlock(&data->update_lock);
  333. return ret;
  334. }
  335. static ssize_t pwm_store(struct device *dev, struct device_attribute *da,
  336. const char *buf, size_t count)
  337. {
  338. struct tc654_data *data = dev_get_drvdata(dev);
  339. unsigned long val;
  340. int ret;
  341. if (kstrtoul(buf, 10, &val))
  342. return -EINVAL;
  343. if (val > 255)
  344. return -EINVAL;
  345. if (val > 0)
  346. val = find_closest(val, tc654_pwm_map, ARRAY_SIZE(tc654_pwm_map)) + 1;
  347. ret = _set_pwm(data, val);
  348. return ret < 0 ? ret : count;
  349. }
  350. static SENSOR_DEVICE_ATTR_RO(fan1_input, fan, 0);
  351. static SENSOR_DEVICE_ATTR_RO(fan2_input, fan, 1);
  352. static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
  353. static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
  354. static SENSOR_DEVICE_ATTR_RO(fan1_alarm, fan_alarm, 0);
  355. static SENSOR_DEVICE_ATTR_RO(fan2_alarm, fan_alarm, 1);
  356. static SENSOR_DEVICE_ATTR_RW(fan1_pulses, fan_pulses, 0);
  357. static SENSOR_DEVICE_ATTR_RW(fan2_pulses, fan_pulses, 1);
  358. static SENSOR_DEVICE_ATTR_RW(pwm1_mode, pwm_mode, 0);
  359. static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
  360. /* Driver data */
  361. static struct attribute *tc654_attrs[] = {
  362. &sensor_dev_attr_fan1_input.dev_attr.attr,
  363. &sensor_dev_attr_fan2_input.dev_attr.attr,
  364. &sensor_dev_attr_fan1_min.dev_attr.attr,
  365. &sensor_dev_attr_fan2_min.dev_attr.attr,
  366. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  367. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  368. &sensor_dev_attr_fan1_pulses.dev_attr.attr,
  369. &sensor_dev_attr_fan2_pulses.dev_attr.attr,
  370. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  371. &sensor_dev_attr_pwm1.dev_attr.attr,
  372. NULL
  373. };
  374. ATTRIBUTE_GROUPS(tc654);
  375. /*
  376. * thermal cooling device functions
  377. *
  378. * Account for the "ShutDown Mode (SDM)" state by offsetting
  379. * the 16 PWM duty cycle states by 1.
  380. *
  381. * State 0 = 0% PWM | Shutdown - Fan(s) are off
  382. * State 1 = 30% PWM | duty_cycle = 0
  383. * State 2 = ~35% PWM | duty_cycle = 1
  384. * [...]
  385. * State 15 = ~95% PWM | duty_cycle = 14
  386. * State 16 = 100% PWM | duty_cycle = 15
  387. */
  388. #define TC654_MAX_COOLING_STATE 16
  389. static int tc654_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
  390. {
  391. *state = TC654_MAX_COOLING_STATE;
  392. return 0;
  393. }
  394. static int tc654_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
  395. {
  396. struct tc654_data *data = tc654_update_client(cdev->devdata);
  397. if (IS_ERR(data))
  398. return PTR_ERR(data);
  399. if (data->config & TC654_REG_CONFIG_SDM)
  400. *state = 0; /* FAN is off */
  401. else
  402. *state = data->duty_cycle + 1; /* offset PWM States by 1 */
  403. return 0;
  404. }
  405. static int tc654_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
  406. {
  407. struct tc654_data *data = tc654_update_client(cdev->devdata);
  408. if (IS_ERR(data))
  409. return PTR_ERR(data);
  410. return _set_pwm(data, clamp_val(state, 0, TC654_MAX_COOLING_STATE));
  411. }
  412. static const struct thermal_cooling_device_ops tc654_fan_cool_ops = {
  413. .get_max_state = tc654_get_max_state,
  414. .get_cur_state = tc654_get_cur_state,
  415. .set_cur_state = tc654_set_cur_state,
  416. };
  417. /*
  418. * device probe and removal
  419. */
  420. static int tc654_probe(struct i2c_client *client)
  421. {
  422. struct device *dev = &client->dev;
  423. struct tc654_data *data;
  424. struct device *hwmon_dev;
  425. int ret;
  426. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  427. return -ENODEV;
  428. data = devm_kzalloc(dev, sizeof(struct tc654_data), GFP_KERNEL);
  429. if (!data)
  430. return -ENOMEM;
  431. data->client = client;
  432. mutex_init(&data->update_lock);
  433. ret = i2c_smbus_read_byte_data(client, TC654_REG_CONFIG);
  434. if (ret < 0)
  435. return ret;
  436. data->config = ret;
  437. hwmon_dev =
  438. devm_hwmon_device_register_with_groups(dev, client->name, data,
  439. tc654_groups);
  440. if (IS_ERR(hwmon_dev))
  441. return PTR_ERR(hwmon_dev);
  442. if (IS_ENABLED(CONFIG_THERMAL)) {
  443. struct thermal_cooling_device *cdev;
  444. cdev = devm_thermal_of_cooling_device_register(dev, dev->of_node, client->name,
  445. hwmon_dev, &tc654_fan_cool_ops);
  446. return PTR_ERR_OR_ZERO(cdev);
  447. }
  448. return 0;
  449. }
  450. static const struct i2c_device_id tc654_id[] = {
  451. {"tc654", 0},
  452. {"tc655", 0},
  453. {}
  454. };
  455. MODULE_DEVICE_TABLE(i2c, tc654_id);
  456. static struct i2c_driver tc654_driver = {
  457. .driver = {
  458. .name = "tc654",
  459. },
  460. .probe_new = tc654_probe,
  461. .id_table = tc654_id,
  462. };
  463. module_i2c_driver(tc654_driver);
  464. MODULE_AUTHOR("Allied Telesis Labs");
  465. MODULE_DESCRIPTION("Microchip TC654/TC655 driver");
  466. MODULE_LICENSE("GPL");