nct7904.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /*
  2. * nct7904.c - driver for Nuvoton NCT7904D.
  3. *
  4. * Copyright (c) 2015 Kontron
  5. * Author: Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>
  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/module.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/mutex.h>
  22. #include <linux/hwmon.h>
  23. #include <linux/hwmon-sysfs.h>
  24. #define VENDOR_ID_REG 0x7A /* Any bank */
  25. #define NUVOTON_ID 0x50
  26. #define CHIP_ID_REG 0x7B /* Any bank */
  27. #define NCT7904_ID 0xC5
  28. #define DEVICE_ID_REG 0x7C /* Any bank */
  29. #define BANK_SEL_REG 0xFF
  30. #define BANK_0 0x00
  31. #define BANK_1 0x01
  32. #define BANK_2 0x02
  33. #define BANK_3 0x03
  34. #define BANK_4 0x04
  35. #define BANK_MAX 0x04
  36. #define FANIN_MAX 12 /* Counted from 1 */
  37. #define VSEN_MAX 21 /* VSEN1..14, 3VDD, VBAT, V3VSB,
  38. LTD (not a voltage), VSEN17..19 */
  39. #define FANCTL_MAX 4 /* Counted from 1 */
  40. #define TCPU_MAX 8 /* Counted from 1 */
  41. #define TEMP_MAX 4 /* Counted from 1 */
  42. #define VT_ADC_CTRL0_REG 0x20 /* Bank 0 */
  43. #define VT_ADC_CTRL1_REG 0x21 /* Bank 0 */
  44. #define VT_ADC_CTRL2_REG 0x22 /* Bank 0 */
  45. #define FANIN_CTRL0_REG 0x24
  46. #define FANIN_CTRL1_REG 0x25
  47. #define DTS_T_CTRL0_REG 0x26
  48. #define DTS_T_CTRL1_REG 0x27
  49. #define VT_ADC_MD_REG 0x2E
  50. #define VSEN1_HV_REG 0x40 /* Bank 0; 2 regs (HV/LV) per sensor */
  51. #define TEMP_CH1_HV_REG 0x42 /* Bank 0; same as VSEN2_HV */
  52. #define LTD_HV_REG 0x62 /* Bank 0; 2 regs in VSEN range */
  53. #define FANIN1_HV_REG 0x80 /* Bank 0; 2 regs (HV/LV) per sensor */
  54. #define T_CPU1_HV_REG 0xA0 /* Bank 0; 2 regs (HV/LV) per sensor */
  55. #define PRTS_REG 0x03 /* Bank 2 */
  56. #define FANCTL1_FMR_REG 0x00 /* Bank 3; 1 reg per channel */
  57. #define FANCTL1_OUT_REG 0x10 /* Bank 3; 1 reg per channel */
  58. static const unsigned short normal_i2c[] = {
  59. 0x2d, 0x2e, I2C_CLIENT_END
  60. };
  61. struct nct7904_data {
  62. struct i2c_client *client;
  63. struct mutex bank_lock;
  64. int bank_sel;
  65. u32 fanin_mask;
  66. u32 vsen_mask;
  67. u32 tcpu_mask;
  68. u8 fan_mode[FANCTL_MAX];
  69. };
  70. /* Access functions */
  71. static int nct7904_bank_lock(struct nct7904_data *data, unsigned bank)
  72. {
  73. int ret;
  74. mutex_lock(&data->bank_lock);
  75. if (data->bank_sel == bank)
  76. return 0;
  77. ret = i2c_smbus_write_byte_data(data->client, BANK_SEL_REG, bank);
  78. if (ret == 0)
  79. data->bank_sel = bank;
  80. else
  81. data->bank_sel = -1;
  82. return ret;
  83. }
  84. static inline void nct7904_bank_release(struct nct7904_data *data)
  85. {
  86. mutex_unlock(&data->bank_lock);
  87. }
  88. /* Read 1-byte register. Returns unsigned reg or -ERRNO on error. */
  89. static int nct7904_read_reg(struct nct7904_data *data,
  90. unsigned bank, unsigned reg)
  91. {
  92. struct i2c_client *client = data->client;
  93. int ret;
  94. ret = nct7904_bank_lock(data, bank);
  95. if (ret == 0)
  96. ret = i2c_smbus_read_byte_data(client, reg);
  97. nct7904_bank_release(data);
  98. return ret;
  99. }
  100. /*
  101. * Read 2-byte register. Returns register in big-endian format or
  102. * -ERRNO on error.
  103. */
  104. static int nct7904_read_reg16(struct nct7904_data *data,
  105. unsigned bank, unsigned reg)
  106. {
  107. struct i2c_client *client = data->client;
  108. int ret, hi;
  109. ret = nct7904_bank_lock(data, bank);
  110. if (ret == 0) {
  111. ret = i2c_smbus_read_byte_data(client, reg);
  112. if (ret >= 0) {
  113. hi = ret;
  114. ret = i2c_smbus_read_byte_data(client, reg + 1);
  115. if (ret >= 0)
  116. ret |= hi << 8;
  117. }
  118. }
  119. nct7904_bank_release(data);
  120. return ret;
  121. }
  122. /* Write 1-byte register. Returns 0 or -ERRNO on error. */
  123. static int nct7904_write_reg(struct nct7904_data *data,
  124. unsigned bank, unsigned reg, u8 val)
  125. {
  126. struct i2c_client *client = data->client;
  127. int ret;
  128. ret = nct7904_bank_lock(data, bank);
  129. if (ret == 0)
  130. ret = i2c_smbus_write_byte_data(client, reg, val);
  131. nct7904_bank_release(data);
  132. return ret;
  133. }
  134. /* FANIN ATTR */
  135. static ssize_t show_fan(struct device *dev,
  136. struct device_attribute *devattr, char *buf)
  137. {
  138. int index = to_sensor_dev_attr(devattr)->index;
  139. struct nct7904_data *data = dev_get_drvdata(dev);
  140. int ret;
  141. unsigned cnt, rpm;
  142. ret = nct7904_read_reg16(data, BANK_0, FANIN1_HV_REG + index * 2);
  143. if (ret < 0)
  144. return ret;
  145. cnt = ((ret & 0xff00) >> 3) | (ret & 0x1f);
  146. if (cnt == 0x1fff)
  147. rpm = 0;
  148. else
  149. rpm = 1350000 / cnt;
  150. return sprintf(buf, "%u\n", rpm);
  151. }
  152. static umode_t nct7904_fanin_is_visible(struct kobject *kobj,
  153. struct attribute *a, int n)
  154. {
  155. struct device *dev = container_of(kobj, struct device, kobj);
  156. struct nct7904_data *data = dev_get_drvdata(dev);
  157. if (data->fanin_mask & (1 << n))
  158. return a->mode;
  159. return 0;
  160. }
  161. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  162. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  163. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  164. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
  165. static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4);
  166. static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan, NULL, 5);
  167. static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan, NULL, 6);
  168. static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan, NULL, 7);
  169. static SENSOR_DEVICE_ATTR(fan9_input, S_IRUGO, show_fan, NULL, 8);
  170. static SENSOR_DEVICE_ATTR(fan10_input, S_IRUGO, show_fan, NULL, 9);
  171. static SENSOR_DEVICE_ATTR(fan11_input, S_IRUGO, show_fan, NULL, 10);
  172. static SENSOR_DEVICE_ATTR(fan12_input, S_IRUGO, show_fan, NULL, 11);
  173. static struct attribute *nct7904_fanin_attrs[] = {
  174. &sensor_dev_attr_fan1_input.dev_attr.attr,
  175. &sensor_dev_attr_fan2_input.dev_attr.attr,
  176. &sensor_dev_attr_fan3_input.dev_attr.attr,
  177. &sensor_dev_attr_fan4_input.dev_attr.attr,
  178. &sensor_dev_attr_fan5_input.dev_attr.attr,
  179. &sensor_dev_attr_fan6_input.dev_attr.attr,
  180. &sensor_dev_attr_fan7_input.dev_attr.attr,
  181. &sensor_dev_attr_fan8_input.dev_attr.attr,
  182. &sensor_dev_attr_fan9_input.dev_attr.attr,
  183. &sensor_dev_attr_fan10_input.dev_attr.attr,
  184. &sensor_dev_attr_fan11_input.dev_attr.attr,
  185. &sensor_dev_attr_fan12_input.dev_attr.attr,
  186. NULL
  187. };
  188. static const struct attribute_group nct7904_fanin_group = {
  189. .attrs = nct7904_fanin_attrs,
  190. .is_visible = nct7904_fanin_is_visible,
  191. };
  192. /* VSEN ATTR */
  193. static ssize_t show_voltage(struct device *dev,
  194. struct device_attribute *devattr, char *buf)
  195. {
  196. int index = to_sensor_dev_attr(devattr)->index;
  197. struct nct7904_data *data = dev_get_drvdata(dev);
  198. int ret;
  199. int volt;
  200. ret = nct7904_read_reg16(data, BANK_0, VSEN1_HV_REG + index * 2);
  201. if (ret < 0)
  202. return ret;
  203. volt = ((ret & 0xff00) >> 5) | (ret & 0x7);
  204. if (index < 14)
  205. volt *= 2; /* 0.002V scale */
  206. else
  207. volt *= 6; /* 0.006V scale */
  208. return sprintf(buf, "%d\n", volt);
  209. }
  210. static ssize_t show_ltemp(struct device *dev,
  211. struct device_attribute *devattr, char *buf)
  212. {
  213. struct nct7904_data *data = dev_get_drvdata(dev);
  214. int ret;
  215. int temp;
  216. ret = nct7904_read_reg16(data, BANK_0, LTD_HV_REG);
  217. if (ret < 0)
  218. return ret;
  219. temp = ((ret & 0xff00) >> 5) | (ret & 0x7);
  220. temp = sign_extend32(temp, 10) * 125;
  221. return sprintf(buf, "%d\n", temp);
  222. }
  223. static umode_t nct7904_vsen_is_visible(struct kobject *kobj,
  224. struct attribute *a, int n)
  225. {
  226. struct device *dev = container_of(kobj, struct device, kobj);
  227. struct nct7904_data *data = dev_get_drvdata(dev);
  228. if (data->vsen_mask & (1 << n))
  229. return a->mode;
  230. return 0;
  231. }
  232. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 0);
  233. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 1);
  234. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 2);
  235. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 3);
  236. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 4);
  237. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 5);
  238. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 6);
  239. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_voltage, NULL, 7);
  240. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_voltage, NULL, 8);
  241. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_voltage, NULL, 9);
  242. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_voltage, NULL, 10);
  243. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_voltage, NULL, 11);
  244. static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_voltage, NULL, 12);
  245. static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_voltage, NULL, 13);
  246. /*
  247. * Next 3 voltage sensors have specific names in the Nuvoton doc
  248. * (3VDD, VBAT, 3VSB) but we use vacant numbers for them.
  249. */
  250. static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, show_voltage, NULL, 14);
  251. static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_voltage, NULL, 15);
  252. static SENSOR_DEVICE_ATTR(in20_input, S_IRUGO, show_voltage, NULL, 16);
  253. /* This is not a voltage, but a local temperature sensor. */
  254. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_ltemp, NULL, 0);
  255. static SENSOR_DEVICE_ATTR(in17_input, S_IRUGO, show_voltage, NULL, 18);
  256. static SENSOR_DEVICE_ATTR(in18_input, S_IRUGO, show_voltage, NULL, 19);
  257. static SENSOR_DEVICE_ATTR(in19_input, S_IRUGO, show_voltage, NULL, 20);
  258. static struct attribute *nct7904_vsen_attrs[] = {
  259. &sensor_dev_attr_in1_input.dev_attr.attr,
  260. &sensor_dev_attr_in2_input.dev_attr.attr,
  261. &sensor_dev_attr_in3_input.dev_attr.attr,
  262. &sensor_dev_attr_in4_input.dev_attr.attr,
  263. &sensor_dev_attr_in5_input.dev_attr.attr,
  264. &sensor_dev_attr_in6_input.dev_attr.attr,
  265. &sensor_dev_attr_in7_input.dev_attr.attr,
  266. &sensor_dev_attr_in8_input.dev_attr.attr,
  267. &sensor_dev_attr_in9_input.dev_attr.attr,
  268. &sensor_dev_attr_in10_input.dev_attr.attr,
  269. &sensor_dev_attr_in11_input.dev_attr.attr,
  270. &sensor_dev_attr_in12_input.dev_attr.attr,
  271. &sensor_dev_attr_in13_input.dev_attr.attr,
  272. &sensor_dev_attr_in14_input.dev_attr.attr,
  273. &sensor_dev_attr_in15_input.dev_attr.attr,
  274. &sensor_dev_attr_in16_input.dev_attr.attr,
  275. &sensor_dev_attr_in20_input.dev_attr.attr,
  276. &sensor_dev_attr_temp1_input.dev_attr.attr,
  277. &sensor_dev_attr_in17_input.dev_attr.attr,
  278. &sensor_dev_attr_in18_input.dev_attr.attr,
  279. &sensor_dev_attr_in19_input.dev_attr.attr,
  280. NULL
  281. };
  282. static const struct attribute_group nct7904_vsen_group = {
  283. .attrs = nct7904_vsen_attrs,
  284. .is_visible = nct7904_vsen_is_visible,
  285. };
  286. /* CPU_TEMP ATTR */
  287. static ssize_t show_tcpu(struct device *dev,
  288. struct device_attribute *devattr, char *buf)
  289. {
  290. int index = to_sensor_dev_attr(devattr)->index;
  291. struct nct7904_data *data = dev_get_drvdata(dev);
  292. int ret;
  293. int temp;
  294. ret = nct7904_read_reg16(data, BANK_0, T_CPU1_HV_REG + index * 2);
  295. if (ret < 0)
  296. return ret;
  297. temp = ((ret & 0xff00) >> 5) | (ret & 0x7);
  298. temp = sign_extend32(temp, 10) * 125;
  299. return sprintf(buf, "%d\n", temp);
  300. }
  301. static umode_t nct7904_tcpu_is_visible(struct kobject *kobj,
  302. struct attribute *a, int n)
  303. {
  304. struct device *dev = container_of(kobj, struct device, kobj);
  305. struct nct7904_data *data = dev_get_drvdata(dev);
  306. if (data->tcpu_mask & (1 << n))
  307. return a->mode;
  308. return 0;
  309. }
  310. /* "temp1_input" reserved for local temp */
  311. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_tcpu, NULL, 0);
  312. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_tcpu, NULL, 1);
  313. static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_tcpu, NULL, 2);
  314. static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, show_tcpu, NULL, 3);
  315. static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, show_tcpu, NULL, 4);
  316. static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, show_tcpu, NULL, 5);
  317. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, show_tcpu, NULL, 6);
  318. static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, show_tcpu, NULL, 7);
  319. static struct attribute *nct7904_tcpu_attrs[] = {
  320. &sensor_dev_attr_temp2_input.dev_attr.attr,
  321. &sensor_dev_attr_temp3_input.dev_attr.attr,
  322. &sensor_dev_attr_temp4_input.dev_attr.attr,
  323. &sensor_dev_attr_temp5_input.dev_attr.attr,
  324. &sensor_dev_attr_temp6_input.dev_attr.attr,
  325. &sensor_dev_attr_temp7_input.dev_attr.attr,
  326. &sensor_dev_attr_temp8_input.dev_attr.attr,
  327. &sensor_dev_attr_temp9_input.dev_attr.attr,
  328. NULL
  329. };
  330. static const struct attribute_group nct7904_tcpu_group = {
  331. .attrs = nct7904_tcpu_attrs,
  332. .is_visible = nct7904_tcpu_is_visible,
  333. };
  334. /* PWM ATTR */
  335. static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
  336. const char *buf, size_t count)
  337. {
  338. int index = to_sensor_dev_attr(devattr)->index;
  339. struct nct7904_data *data = dev_get_drvdata(dev);
  340. unsigned long val;
  341. int ret;
  342. if (kstrtoul(buf, 10, &val) < 0)
  343. return -EINVAL;
  344. if (val > 255)
  345. return -EINVAL;
  346. ret = nct7904_write_reg(data, BANK_3, FANCTL1_OUT_REG + index, val);
  347. return ret ? ret : count;
  348. }
  349. static ssize_t show_pwm(struct device *dev,
  350. struct device_attribute *devattr, char *buf)
  351. {
  352. int index = to_sensor_dev_attr(devattr)->index;
  353. struct nct7904_data *data = dev_get_drvdata(dev);
  354. int val;
  355. val = nct7904_read_reg(data, BANK_3, FANCTL1_OUT_REG + index);
  356. if (val < 0)
  357. return val;
  358. return sprintf(buf, "%d\n", val);
  359. }
  360. static ssize_t store_enable(struct device *dev,
  361. struct device_attribute *devattr,
  362. const char *buf, size_t count)
  363. {
  364. int index = to_sensor_dev_attr(devattr)->index;
  365. struct nct7904_data *data = dev_get_drvdata(dev);
  366. unsigned long val;
  367. int ret;
  368. if (kstrtoul(buf, 10, &val) < 0)
  369. return -EINVAL;
  370. if (val < 1 || val > 2 || (val == 2 && !data->fan_mode[index]))
  371. return -EINVAL;
  372. ret = nct7904_write_reg(data, BANK_3, FANCTL1_FMR_REG + index,
  373. val == 2 ? data->fan_mode[index] : 0);
  374. return ret ? ret : count;
  375. }
  376. /* Return 1 for manual mode or 2 for SmartFan mode */
  377. static ssize_t show_enable(struct device *dev,
  378. struct device_attribute *devattr, char *buf)
  379. {
  380. int index = to_sensor_dev_attr(devattr)->index;
  381. struct nct7904_data *data = dev_get_drvdata(dev);
  382. int val;
  383. val = nct7904_read_reg(data, BANK_3, FANCTL1_FMR_REG + index);
  384. if (val < 0)
  385. return val;
  386. return sprintf(buf, "%d\n", val ? 2 : 1);
  387. }
  388. /* 2 attributes per channel: pwm and mode */
  389. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
  390. show_pwm, store_pwm, 0);
  391. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  392. show_enable, store_enable, 0);
  393. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR,
  394. show_pwm, store_pwm, 1);
  395. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
  396. show_enable, store_enable, 1);
  397. static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR,
  398. show_pwm, store_pwm, 2);
  399. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
  400. show_enable, store_enable, 2);
  401. static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR,
  402. show_pwm, store_pwm, 3);
  403. static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
  404. show_enable, store_enable, 3);
  405. static struct attribute *nct7904_fanctl_attrs[] = {
  406. &sensor_dev_attr_pwm1.dev_attr.attr,
  407. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  408. &sensor_dev_attr_pwm2.dev_attr.attr,
  409. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  410. &sensor_dev_attr_pwm3.dev_attr.attr,
  411. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  412. &sensor_dev_attr_pwm4.dev_attr.attr,
  413. &sensor_dev_attr_pwm4_enable.dev_attr.attr,
  414. NULL
  415. };
  416. static const struct attribute_group nct7904_fanctl_group = {
  417. .attrs = nct7904_fanctl_attrs,
  418. };
  419. static const struct attribute_group *nct7904_groups[] = {
  420. &nct7904_fanin_group,
  421. &nct7904_vsen_group,
  422. &nct7904_tcpu_group,
  423. &nct7904_fanctl_group,
  424. NULL
  425. };
  426. /* Return 0 if detection is successful, -ENODEV otherwise */
  427. static int nct7904_detect(struct i2c_client *client,
  428. struct i2c_board_info *info)
  429. {
  430. struct i2c_adapter *adapter = client->adapter;
  431. if (!i2c_check_functionality(adapter,
  432. I2C_FUNC_SMBUS_READ_BYTE |
  433. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  434. return -ENODEV;
  435. /* Determine the chip type. */
  436. if (i2c_smbus_read_byte_data(client, VENDOR_ID_REG) != NUVOTON_ID ||
  437. i2c_smbus_read_byte_data(client, CHIP_ID_REG) != NCT7904_ID ||
  438. (i2c_smbus_read_byte_data(client, DEVICE_ID_REG) & 0xf0) != 0x50 ||
  439. (i2c_smbus_read_byte_data(client, BANK_SEL_REG) & 0xf8) != 0x00)
  440. return -ENODEV;
  441. strlcpy(info->type, "nct7904", I2C_NAME_SIZE);
  442. return 0;
  443. }
  444. static int nct7904_probe(struct i2c_client *client,
  445. const struct i2c_device_id *id)
  446. {
  447. struct nct7904_data *data;
  448. struct device *hwmon_dev;
  449. struct device *dev = &client->dev;
  450. int ret, i;
  451. u32 mask;
  452. data = devm_kzalloc(dev, sizeof(struct nct7904_data), GFP_KERNEL);
  453. if (!data)
  454. return -ENOMEM;
  455. data->client = client;
  456. mutex_init(&data->bank_lock);
  457. data->bank_sel = -1;
  458. /* Setup sensor groups. */
  459. /* FANIN attributes */
  460. ret = nct7904_read_reg16(data, BANK_0, FANIN_CTRL0_REG);
  461. if (ret < 0)
  462. return ret;
  463. data->fanin_mask = (ret >> 8) | ((ret & 0xff) << 8);
  464. /*
  465. * VSEN attributes
  466. *
  467. * Note: voltage sensors overlap with external temperature
  468. * sensors. So, if we ever decide to support the latter
  469. * we will have to adjust 'vsen_mask' accordingly.
  470. */
  471. mask = 0;
  472. ret = nct7904_read_reg16(data, BANK_0, VT_ADC_CTRL0_REG);
  473. if (ret >= 0)
  474. mask = (ret >> 8) | ((ret & 0xff) << 8);
  475. ret = nct7904_read_reg(data, BANK_0, VT_ADC_CTRL2_REG);
  476. if (ret >= 0)
  477. mask |= (ret << 16);
  478. data->vsen_mask = mask;
  479. /* CPU_TEMP attributes */
  480. ret = nct7904_read_reg16(data, BANK_0, DTS_T_CTRL0_REG);
  481. if (ret < 0)
  482. return ret;
  483. data->tcpu_mask = ((ret >> 8) & 0xf) | ((ret & 0xf) << 4);
  484. for (i = 0; i < FANCTL_MAX; i++) {
  485. ret = nct7904_read_reg(data, BANK_3, FANCTL1_FMR_REG + i);
  486. if (ret < 0)
  487. return ret;
  488. data->fan_mode[i] = ret;
  489. }
  490. hwmon_dev =
  491. devm_hwmon_device_register_with_groups(dev, client->name, data,
  492. nct7904_groups);
  493. return PTR_ERR_OR_ZERO(hwmon_dev);
  494. }
  495. static const struct i2c_device_id nct7904_id[] = {
  496. {"nct7904", 0},
  497. {}
  498. };
  499. MODULE_DEVICE_TABLE(i2c, nct7904_id);
  500. static struct i2c_driver nct7904_driver = {
  501. .class = I2C_CLASS_HWMON,
  502. .driver = {
  503. .name = "nct7904",
  504. },
  505. .probe = nct7904_probe,
  506. .id_table = nct7904_id,
  507. .detect = nct7904_detect,
  508. .address_list = normal_i2c,
  509. };
  510. module_i2c_driver(nct7904_driver);
  511. MODULE_AUTHOR("Vadim V. Vlasov <vvlasov@dev.rtsoft.ru>");
  512. MODULE_DESCRIPTION("Hwmon driver for NUVOTON NCT7904");
  513. MODULE_LICENSE("GPL");