tc654.c 14 KB

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