max31790.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * max31790.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring.
  4. *
  5. * (C) 2015 by Il Han <corone.il.han@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  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/slab.h>
  25. /* MAX31790 registers */
  26. #define MAX31790_REG_GLOBAL_CONFIG 0x00
  27. #define MAX31790_REG_FAN_CONFIG(ch) (0x02 + (ch))
  28. #define MAX31790_REG_FAN_DYNAMICS(ch) (0x08 + (ch))
  29. #define MAX31790_REG_FAN_FAULT_STATUS2 0x10
  30. #define MAX31790_REG_FAN_FAULT_STATUS1 0x11
  31. #define MAX31790_REG_TACH_COUNT(ch) (0x18 + (ch) * 2)
  32. #define MAX31790_REG_PWM_DUTY_CYCLE(ch) (0x30 + (ch) * 2)
  33. #define MAX31790_REG_PWMOUT(ch) (0x40 + (ch) * 2)
  34. #define MAX31790_REG_TARGET_COUNT(ch) (0x50 + (ch) * 2)
  35. /* Fan Config register bits */
  36. #define MAX31790_FAN_CFG_RPM_MODE 0x80
  37. #define MAX31790_FAN_CFG_TACH_INPUT_EN 0x08
  38. #define MAX31790_FAN_CFG_TACH_INPUT 0x01
  39. /* Fan Dynamics register bits */
  40. #define MAX31790_FAN_DYN_SR_SHIFT 5
  41. #define MAX31790_FAN_DYN_SR_MASK 0xE0
  42. #define SR_FROM_REG(reg) (((reg) & MAX31790_FAN_DYN_SR_MASK) \
  43. >> MAX31790_FAN_DYN_SR_SHIFT)
  44. #define FAN_RPM_MIN 120
  45. #define FAN_RPM_MAX 7864320
  46. #define RPM_FROM_REG(reg, sr) (((reg) >> 4) ? \
  47. ((60 * (sr) * 8192) / ((reg) >> 4)) : \
  48. FAN_RPM_MAX)
  49. #define RPM_TO_REG(rpm, sr) ((60 * (sr) * 8192) / ((rpm) * 2))
  50. #define NR_CHANNEL 6
  51. /*
  52. * Client data (each client gets its own)
  53. */
  54. struct max31790_data {
  55. struct i2c_client *client;
  56. struct mutex update_lock;
  57. bool valid; /* zero until following fields are valid */
  58. unsigned long last_updated; /* in jiffies */
  59. /* register values */
  60. u8 fan_config[NR_CHANNEL];
  61. u8 fan_dynamics[NR_CHANNEL];
  62. u16 fault_status;
  63. u16 tach[NR_CHANNEL * 2];
  64. u16 pwm[NR_CHANNEL];
  65. u16 target_count[NR_CHANNEL];
  66. };
  67. static struct max31790_data *max31790_update_device(struct device *dev)
  68. {
  69. struct max31790_data *data = dev_get_drvdata(dev);
  70. struct i2c_client *client = data->client;
  71. struct max31790_data *ret = data;
  72. int i;
  73. int rv;
  74. mutex_lock(&data->update_lock);
  75. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  76. rv = i2c_smbus_read_byte_data(client,
  77. MAX31790_REG_FAN_FAULT_STATUS1);
  78. if (rv < 0)
  79. goto abort;
  80. data->fault_status = rv & 0x3F;
  81. rv = i2c_smbus_read_byte_data(client,
  82. MAX31790_REG_FAN_FAULT_STATUS2);
  83. if (rv < 0)
  84. goto abort;
  85. data->fault_status |= (rv & 0x3F) << 6;
  86. for (i = 0; i < NR_CHANNEL; i++) {
  87. rv = i2c_smbus_read_word_swapped(client,
  88. MAX31790_REG_TACH_COUNT(i));
  89. if (rv < 0)
  90. goto abort;
  91. data->tach[i] = rv;
  92. if (data->fan_config[i]
  93. & MAX31790_FAN_CFG_TACH_INPUT) {
  94. rv = i2c_smbus_read_word_swapped(client,
  95. MAX31790_REG_TACH_COUNT(NR_CHANNEL
  96. + i));
  97. if (rv < 0)
  98. goto abort;
  99. data->tach[NR_CHANNEL + i] = rv;
  100. } else {
  101. rv = i2c_smbus_read_word_swapped(client,
  102. MAX31790_REG_PWMOUT(i));
  103. if (rv < 0)
  104. goto abort;
  105. data->pwm[i] = rv;
  106. rv = i2c_smbus_read_word_swapped(client,
  107. MAX31790_REG_TARGET_COUNT(i));
  108. if (rv < 0)
  109. goto abort;
  110. data->target_count[i] = rv;
  111. }
  112. }
  113. data->last_updated = jiffies;
  114. data->valid = true;
  115. }
  116. goto done;
  117. abort:
  118. data->valid = false;
  119. ret = ERR_PTR(rv);
  120. done:
  121. mutex_unlock(&data->update_lock);
  122. return ret;
  123. }
  124. static const u8 tach_period[8] = { 1, 2, 4, 8, 16, 32, 32, 32 };
  125. static u8 get_tach_period(u8 fan_dynamics)
  126. {
  127. return tach_period[SR_FROM_REG(fan_dynamics)];
  128. }
  129. static u8 bits_for_tach_period(int rpm)
  130. {
  131. u8 bits;
  132. if (rpm < 500)
  133. bits = 0x0;
  134. else if (rpm < 1000)
  135. bits = 0x1;
  136. else if (rpm < 2000)
  137. bits = 0x2;
  138. else if (rpm < 4000)
  139. bits = 0x3;
  140. else if (rpm < 8000)
  141. bits = 0x4;
  142. else
  143. bits = 0x5;
  144. return bits;
  145. }
  146. static ssize_t get_fan(struct device *dev,
  147. struct device_attribute *devattr, char *buf)
  148. {
  149. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  150. struct max31790_data *data = max31790_update_device(dev);
  151. int sr, rpm;
  152. if (IS_ERR(data))
  153. return PTR_ERR(data);
  154. sr = get_tach_period(data->fan_dynamics[attr->index]);
  155. rpm = RPM_FROM_REG(data->tach[attr->index], sr);
  156. return sprintf(buf, "%d\n", rpm);
  157. }
  158. static ssize_t get_fan_target(struct device *dev,
  159. struct device_attribute *devattr, char *buf)
  160. {
  161. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  162. struct max31790_data *data = max31790_update_device(dev);
  163. int sr, rpm;
  164. if (IS_ERR(data))
  165. return PTR_ERR(data);
  166. sr = get_tach_period(data->fan_dynamics[attr->index]);
  167. rpm = RPM_FROM_REG(data->target_count[attr->index], sr);
  168. return sprintf(buf, "%d\n", rpm);
  169. }
  170. static ssize_t set_fan_target(struct device *dev,
  171. struct device_attribute *devattr,
  172. const char *buf, size_t count)
  173. {
  174. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  175. struct max31790_data *data = dev_get_drvdata(dev);
  176. struct i2c_client *client = data->client;
  177. u8 bits;
  178. int sr;
  179. int target_count;
  180. unsigned long rpm;
  181. int err;
  182. err = kstrtoul(buf, 10, &rpm);
  183. if (err)
  184. return err;
  185. mutex_lock(&data->update_lock);
  186. rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
  187. bits = bits_for_tach_period(rpm);
  188. data->fan_dynamics[attr->index] =
  189. ((data->fan_dynamics[attr->index]
  190. & ~MAX31790_FAN_DYN_SR_MASK)
  191. | (bits << MAX31790_FAN_DYN_SR_SHIFT));
  192. err = i2c_smbus_write_byte_data(client,
  193. MAX31790_REG_FAN_DYNAMICS(attr->index),
  194. data->fan_dynamics[attr->index]);
  195. if (err < 0) {
  196. mutex_unlock(&data->update_lock);
  197. return err;
  198. }
  199. sr = get_tach_period(data->fan_dynamics[attr->index]);
  200. target_count = RPM_TO_REG(rpm, sr);
  201. target_count = clamp_val(target_count, 0x1, 0x7FF);
  202. data->target_count[attr->index] = target_count << 5;
  203. err = i2c_smbus_write_word_swapped(client,
  204. MAX31790_REG_TARGET_COUNT(attr->index),
  205. data->target_count[attr->index]);
  206. mutex_unlock(&data->update_lock);
  207. if (err < 0)
  208. return err;
  209. return count;
  210. }
  211. static ssize_t get_pwm(struct device *dev,
  212. struct device_attribute *devattr, char *buf)
  213. {
  214. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  215. struct max31790_data *data = max31790_update_device(dev);
  216. int pwm;
  217. if (IS_ERR(data))
  218. return PTR_ERR(data);
  219. pwm = data->pwm[attr->index] >> 8;
  220. return sprintf(buf, "%d\n", pwm);
  221. }
  222. static ssize_t set_pwm(struct device *dev,
  223. struct device_attribute *devattr,
  224. const char *buf, size_t count)
  225. {
  226. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  227. struct max31790_data *data = dev_get_drvdata(dev);
  228. struct i2c_client *client = data->client;
  229. unsigned long pwm;
  230. int err;
  231. err = kstrtoul(buf, 10, &pwm);
  232. if (err)
  233. return err;
  234. if (pwm > 255)
  235. return -EINVAL;
  236. mutex_lock(&data->update_lock);
  237. data->pwm[attr->index] = pwm << 8;
  238. err = i2c_smbus_write_word_swapped(client,
  239. MAX31790_REG_PWMOUT(attr->index),
  240. data->pwm[attr->index]);
  241. mutex_unlock(&data->update_lock);
  242. if (err < 0)
  243. return err;
  244. return count;
  245. }
  246. static ssize_t get_pwm_enable(struct device *dev,
  247. struct device_attribute *devattr, char *buf)
  248. {
  249. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  250. struct max31790_data *data = max31790_update_device(dev);
  251. int mode;
  252. if (IS_ERR(data))
  253. return PTR_ERR(data);
  254. if (data->fan_config[attr->index] & MAX31790_FAN_CFG_RPM_MODE)
  255. mode = 2;
  256. else if (data->fan_config[attr->index] & MAX31790_FAN_CFG_TACH_INPUT_EN)
  257. mode = 1;
  258. else
  259. mode = 0;
  260. return sprintf(buf, "%d\n", mode);
  261. }
  262. static ssize_t set_pwm_enable(struct device *dev,
  263. struct device_attribute *devattr,
  264. const char *buf, size_t count)
  265. {
  266. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  267. struct max31790_data *data = dev_get_drvdata(dev);
  268. struct i2c_client *client = data->client;
  269. unsigned long mode;
  270. int err;
  271. err = kstrtoul(buf, 10, &mode);
  272. if (err)
  273. return err;
  274. switch (mode) {
  275. case 0:
  276. data->fan_config[attr->index] =
  277. data->fan_config[attr->index]
  278. & ~(MAX31790_FAN_CFG_TACH_INPUT_EN
  279. | MAX31790_FAN_CFG_RPM_MODE);
  280. break;
  281. case 1:
  282. data->fan_config[attr->index] =
  283. (data->fan_config[attr->index]
  284. | MAX31790_FAN_CFG_TACH_INPUT_EN)
  285. & ~MAX31790_FAN_CFG_RPM_MODE;
  286. break;
  287. case 2:
  288. data->fan_config[attr->index] =
  289. data->fan_config[attr->index]
  290. | MAX31790_FAN_CFG_TACH_INPUT_EN
  291. | MAX31790_FAN_CFG_RPM_MODE;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. mutex_lock(&data->update_lock);
  297. err = i2c_smbus_write_byte_data(client,
  298. MAX31790_REG_FAN_CONFIG(attr->index),
  299. data->fan_config[attr->index]);
  300. mutex_unlock(&data->update_lock);
  301. if (err < 0)
  302. return err;
  303. return count;
  304. }
  305. static ssize_t get_fan_fault(struct device *dev,
  306. struct device_attribute *devattr, char *buf)
  307. {
  308. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  309. struct max31790_data *data = max31790_update_device(dev);
  310. int fault;
  311. if (IS_ERR(data))
  312. return PTR_ERR(data);
  313. fault = !!(data->fault_status & (1 << attr->index));
  314. return sprintf(buf, "%d\n", fault);
  315. }
  316. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, get_fan, NULL, 0);
  317. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, get_fan, NULL, 1);
  318. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, get_fan, NULL, 2);
  319. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, get_fan, NULL, 3);
  320. static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, get_fan, NULL, 4);
  321. static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, get_fan, NULL, 5);
  322. static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, get_fan_fault, NULL, 0);
  323. static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, get_fan_fault, NULL, 1);
  324. static SENSOR_DEVICE_ATTR(fan3_fault, S_IRUGO, get_fan_fault, NULL, 2);
  325. static SENSOR_DEVICE_ATTR(fan4_fault, S_IRUGO, get_fan_fault, NULL, 3);
  326. static SENSOR_DEVICE_ATTR(fan5_fault, S_IRUGO, get_fan_fault, NULL, 4);
  327. static SENSOR_DEVICE_ATTR(fan6_fault, S_IRUGO, get_fan_fault, NULL, 5);
  328. static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, get_fan, NULL, 6);
  329. static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, get_fan, NULL, 7);
  330. static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, get_fan, NULL, 8);
  331. static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, get_fan, NULL, 9);
  332. static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, get_fan, NULL, 10);
  333. static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, get_fan, NULL, 11);
  334. static SENSOR_DEVICE_ATTR(fan7_fault, S_IRUGO, get_fan_fault, NULL, 6);
  335. static SENSOR_DEVICE_ATTR(fan8_fault, S_IRUGO, get_fan_fault, NULL, 7);
  336. static SENSOR_DEVICE_ATTR(fan9_fault, S_IRUGO, get_fan_fault, NULL, 8);
  337. static SENSOR_DEVICE_ATTR(fan10_fault, S_IRUGO, get_fan_fault, NULL, 9);
  338. static SENSOR_DEVICE_ATTR(fan11_fault, S_IRUGO, get_fan_fault, NULL, 10);
  339. static SENSOR_DEVICE_ATTR(fan12_fault, S_IRUGO, get_fan_fault, NULL, 11);
  340. static SENSOR_DEVICE_ATTR(fan1_target, S_IWUSR | S_IRUGO,
  341. get_fan_target, set_fan_target, 0);
  342. static SENSOR_DEVICE_ATTR(fan2_target, S_IWUSR | S_IRUGO,
  343. get_fan_target, set_fan_target, 1);
  344. static SENSOR_DEVICE_ATTR(fan3_target, S_IWUSR | S_IRUGO,
  345. get_fan_target, set_fan_target, 2);
  346. static SENSOR_DEVICE_ATTR(fan4_target, S_IWUSR | S_IRUGO,
  347. get_fan_target, set_fan_target, 3);
  348. static SENSOR_DEVICE_ATTR(fan5_target, S_IWUSR | S_IRUGO,
  349. get_fan_target, set_fan_target, 4);
  350. static SENSOR_DEVICE_ATTR(fan6_target, S_IWUSR | S_IRUGO,
  351. get_fan_target, set_fan_target, 5);
  352. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 0);
  353. static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 1);
  354. static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 2);
  355. static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 3);
  356. static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 4);
  357. static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, get_pwm, set_pwm, 5);
  358. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  359. get_pwm_enable, set_pwm_enable, 0);
  360. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
  361. get_pwm_enable, set_pwm_enable, 1);
  362. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
  363. get_pwm_enable, set_pwm_enable, 2);
  364. static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO,
  365. get_pwm_enable, set_pwm_enable, 3);
  366. static SENSOR_DEVICE_ATTR(pwm5_enable, S_IWUSR | S_IRUGO,
  367. get_pwm_enable, set_pwm_enable, 4);
  368. static SENSOR_DEVICE_ATTR(pwm6_enable, S_IWUSR | S_IRUGO,
  369. get_pwm_enable, set_pwm_enable, 5);
  370. static struct attribute *max31790_attrs[] = {
  371. &sensor_dev_attr_fan1_input.dev_attr.attr,
  372. &sensor_dev_attr_fan2_input.dev_attr.attr,
  373. &sensor_dev_attr_fan3_input.dev_attr.attr,
  374. &sensor_dev_attr_fan4_input.dev_attr.attr,
  375. &sensor_dev_attr_fan5_input.dev_attr.attr,
  376. &sensor_dev_attr_fan6_input.dev_attr.attr,
  377. &sensor_dev_attr_fan1_fault.dev_attr.attr,
  378. &sensor_dev_attr_fan2_fault.dev_attr.attr,
  379. &sensor_dev_attr_fan3_fault.dev_attr.attr,
  380. &sensor_dev_attr_fan4_fault.dev_attr.attr,
  381. &sensor_dev_attr_fan5_fault.dev_attr.attr,
  382. &sensor_dev_attr_fan6_fault.dev_attr.attr,
  383. &sensor_dev_attr_fan7_input.dev_attr.attr,
  384. &sensor_dev_attr_fan8_input.dev_attr.attr,
  385. &sensor_dev_attr_fan9_input.dev_attr.attr,
  386. &sensor_dev_attr_fan10_input.dev_attr.attr,
  387. &sensor_dev_attr_fan11_input.dev_attr.attr,
  388. &sensor_dev_attr_fan12_input.dev_attr.attr,
  389. &sensor_dev_attr_fan7_fault.dev_attr.attr,
  390. &sensor_dev_attr_fan8_fault.dev_attr.attr,
  391. &sensor_dev_attr_fan9_fault.dev_attr.attr,
  392. &sensor_dev_attr_fan10_fault.dev_attr.attr,
  393. &sensor_dev_attr_fan11_fault.dev_attr.attr,
  394. &sensor_dev_attr_fan12_fault.dev_attr.attr,
  395. &sensor_dev_attr_fan1_target.dev_attr.attr,
  396. &sensor_dev_attr_fan2_target.dev_attr.attr,
  397. &sensor_dev_attr_fan3_target.dev_attr.attr,
  398. &sensor_dev_attr_fan4_target.dev_attr.attr,
  399. &sensor_dev_attr_fan5_target.dev_attr.attr,
  400. &sensor_dev_attr_fan6_target.dev_attr.attr,
  401. &sensor_dev_attr_pwm1.dev_attr.attr,
  402. &sensor_dev_attr_pwm2.dev_attr.attr,
  403. &sensor_dev_attr_pwm3.dev_attr.attr,
  404. &sensor_dev_attr_pwm4.dev_attr.attr,
  405. &sensor_dev_attr_pwm5.dev_attr.attr,
  406. &sensor_dev_attr_pwm6.dev_attr.attr,
  407. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  408. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  409. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  410. &sensor_dev_attr_pwm4_enable.dev_attr.attr,
  411. &sensor_dev_attr_pwm5_enable.dev_attr.attr,
  412. &sensor_dev_attr_pwm6_enable.dev_attr.attr,
  413. NULL
  414. };
  415. static umode_t max31790_attrs_visible(struct kobject *kobj,
  416. struct attribute *a, int n)
  417. {
  418. struct device *dev = container_of(kobj, struct device, kobj);
  419. struct max31790_data *data = dev_get_drvdata(dev);
  420. struct device_attribute *devattr =
  421. container_of(a, struct device_attribute, attr);
  422. int index = to_sensor_dev_attr(devattr)->index % NR_CHANNEL;
  423. u8 fan_config;
  424. fan_config = data->fan_config[index];
  425. if (n >= NR_CHANNEL * 2 && n < NR_CHANNEL * 4 &&
  426. !(fan_config & MAX31790_FAN_CFG_TACH_INPUT))
  427. return 0;
  428. if (n >= NR_CHANNEL * 4 && (fan_config & MAX31790_FAN_CFG_TACH_INPUT))
  429. return 0;
  430. return a->mode;
  431. }
  432. static const struct attribute_group max31790_group = {
  433. .attrs = max31790_attrs,
  434. .is_visible = max31790_attrs_visible,
  435. };
  436. __ATTRIBUTE_GROUPS(max31790);
  437. static int max31790_init_client(struct i2c_client *client,
  438. struct max31790_data *data)
  439. {
  440. int i, rv;
  441. for (i = 0; i < NR_CHANNEL; i++) {
  442. rv = i2c_smbus_read_byte_data(client,
  443. MAX31790_REG_FAN_CONFIG(i));
  444. if (rv < 0)
  445. return rv;
  446. data->fan_config[i] = rv;
  447. rv = i2c_smbus_read_byte_data(client,
  448. MAX31790_REG_FAN_DYNAMICS(i));
  449. if (rv < 0)
  450. return rv;
  451. data->fan_dynamics[i] = rv;
  452. }
  453. return 0;
  454. }
  455. static int max31790_probe(struct i2c_client *client,
  456. const struct i2c_device_id *id)
  457. {
  458. struct i2c_adapter *adapter = client->adapter;
  459. struct device *dev = &client->dev;
  460. struct max31790_data *data;
  461. struct device *hwmon_dev;
  462. int err;
  463. if (!i2c_check_functionality(adapter,
  464. I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
  465. return -ENODEV;
  466. data = devm_kzalloc(dev, sizeof(struct max31790_data), GFP_KERNEL);
  467. if (!data)
  468. return -ENOMEM;
  469. data->client = client;
  470. mutex_init(&data->update_lock);
  471. /*
  472. * Initialize the max31790 chip
  473. */
  474. err = max31790_init_client(client, data);
  475. if (err)
  476. return err;
  477. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  478. client->name, data, max31790_groups);
  479. return PTR_ERR_OR_ZERO(hwmon_dev);
  480. }
  481. static const struct i2c_device_id max31790_id[] = {
  482. { "max31790", 0 },
  483. { }
  484. };
  485. MODULE_DEVICE_TABLE(i2c, max31790_id);
  486. static struct i2c_driver max31790_driver = {
  487. .class = I2C_CLASS_HWMON,
  488. .probe = max31790_probe,
  489. .driver = {
  490. .name = "max31790",
  491. },
  492. .id_table = max31790_id,
  493. };
  494. module_i2c_driver(max31790_driver);
  495. MODULE_AUTHOR("Il Han <corone.il.han@gmail.com>");
  496. MODULE_DESCRIPTION("MAX31790 sensor driver");
  497. MODULE_LICENSE("GPL");