nct7802.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * nct7802 - Driver for Nuvoton NCT7802Y
  3. *
  4. * Copyright (C) 2014 Guenter Roeck <linux@roeck-us.net>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/err.h>
  18. #include <linux/i2c.h>
  19. #include <linux/init.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/module.h>
  24. #include <linux/mutex.h>
  25. #include <linux/regmap.h>
  26. #include <linux/slab.h>
  27. #define DRVNAME "nct7802"
  28. static const u8 REG_VOLTAGE[5] = { 0x09, 0x0a, 0x0c, 0x0d, 0x0e };
  29. static const u8 REG_VOLTAGE_LIMIT_LSB[2][5] = {
  30. { 0x40, 0x00, 0x42, 0x44, 0x46 },
  31. { 0x3f, 0x00, 0x41, 0x43, 0x45 },
  32. };
  33. static const u8 REG_VOLTAGE_LIMIT_MSB[5] = { 0x48, 0x00, 0x47, 0x47, 0x48 };
  34. static const u8 REG_VOLTAGE_LIMIT_MSB_SHIFT[2][5] = {
  35. { 0, 0, 4, 0, 4 },
  36. { 2, 0, 6, 2, 6 },
  37. };
  38. #define REG_BANK 0x00
  39. #define REG_TEMP_LSB 0x05
  40. #define REG_TEMP_PECI_LSB 0x08
  41. #define REG_VOLTAGE_LOW 0x0f
  42. #define REG_FANCOUNT_LOW 0x13
  43. #define REG_START 0x21
  44. #define REG_MODE 0x22
  45. #define REG_PECI_ENABLE 0x23
  46. #define REG_FAN_ENABLE 0x24
  47. #define REG_VMON_ENABLE 0x25
  48. #define REG_VENDOR_ID 0xfd
  49. #define REG_CHIP_ID 0xfe
  50. #define REG_VERSION_ID 0xff
  51. /*
  52. * Data structures and manipulation thereof
  53. */
  54. struct nct7802_data {
  55. struct regmap *regmap;
  56. struct mutex access_lock; /* for multi-byte read and write operations */
  57. };
  58. static int nct7802_read_temp(struct nct7802_data *data,
  59. u8 reg_temp, u8 reg_temp_low, int *temp)
  60. {
  61. unsigned int t1, t2 = 0;
  62. int err;
  63. *temp = 0;
  64. mutex_lock(&data->access_lock);
  65. err = regmap_read(data->regmap, reg_temp, &t1);
  66. if (err < 0)
  67. goto abort;
  68. t1 <<= 8;
  69. if (reg_temp_low) { /* 11 bit data */
  70. err = regmap_read(data->regmap, reg_temp_low, &t2);
  71. if (err < 0)
  72. goto abort;
  73. }
  74. t1 |= t2 & 0xe0;
  75. *temp = (s16)t1 / 32 * 125;
  76. abort:
  77. mutex_unlock(&data->access_lock);
  78. return err;
  79. }
  80. static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
  81. {
  82. unsigned int f1, f2;
  83. int ret;
  84. mutex_lock(&data->access_lock);
  85. ret = regmap_read(data->regmap, reg_fan, &f1);
  86. if (ret < 0)
  87. goto abort;
  88. ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
  89. if (ret < 0)
  90. goto abort;
  91. ret = (f1 << 5) | (f2 >> 3);
  92. /* convert fan count to rpm */
  93. if (ret == 0x1fff) /* maximum value, assume fan is stopped */
  94. ret = 0;
  95. else if (ret)
  96. ret = DIV_ROUND_CLOSEST(1350000U, ret);
  97. abort:
  98. mutex_unlock(&data->access_lock);
  99. return ret;
  100. }
  101. static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
  102. u8 reg_fan_high)
  103. {
  104. unsigned int f1, f2;
  105. int ret;
  106. mutex_lock(&data->access_lock);
  107. ret = regmap_read(data->regmap, reg_fan_low, &f1);
  108. if (ret < 0)
  109. goto abort;
  110. ret = regmap_read(data->regmap, reg_fan_high, &f2);
  111. if (ret < 0)
  112. goto abort;
  113. ret = f1 | ((f2 & 0xf8) << 5);
  114. /* convert fan count to rpm */
  115. if (ret == 0x1fff) /* maximum value, assume no limit */
  116. ret = 0;
  117. else if (ret)
  118. ret = DIV_ROUND_CLOSEST(1350000U, ret);
  119. abort:
  120. mutex_unlock(&data->access_lock);
  121. return ret;
  122. }
  123. static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
  124. u8 reg_fan_high, unsigned int limit)
  125. {
  126. int err;
  127. if (limit)
  128. limit = DIV_ROUND_CLOSEST(1350000U, limit);
  129. else
  130. limit = 0x1fff;
  131. limit = clamp_val(limit, 0, 0x1fff);
  132. mutex_lock(&data->access_lock);
  133. err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
  134. if (err < 0)
  135. goto abort;
  136. err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
  137. abort:
  138. mutex_unlock(&data->access_lock);
  139. return err;
  140. }
  141. static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
  142. static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
  143. {
  144. unsigned int v1, v2;
  145. int ret;
  146. mutex_lock(&data->access_lock);
  147. if (index == 0) { /* voltage */
  148. ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
  149. if (ret < 0)
  150. goto abort;
  151. ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
  152. if (ret < 0)
  153. goto abort;
  154. ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
  155. } else { /* limit */
  156. int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
  157. ret = regmap_read(data->regmap,
  158. REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
  159. if (ret < 0)
  160. goto abort;
  161. ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
  162. &v2);
  163. if (ret < 0)
  164. goto abort;
  165. ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
  166. }
  167. abort:
  168. mutex_unlock(&data->access_lock);
  169. return ret;
  170. }
  171. static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
  172. unsigned int voltage)
  173. {
  174. int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
  175. int err;
  176. voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
  177. voltage = clamp_val(voltage, 0, 0x3ff);
  178. mutex_lock(&data->access_lock);
  179. err = regmap_write(data->regmap,
  180. REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
  181. voltage & 0xff);
  182. if (err < 0)
  183. goto abort;
  184. err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
  185. 0x0300 >> shift, (voltage & 0x0300) >> shift);
  186. abort:
  187. mutex_unlock(&data->access_lock);
  188. return err;
  189. }
  190. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  194. struct nct7802_data *data = dev_get_drvdata(dev);
  195. int voltage;
  196. voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
  197. if (voltage < 0)
  198. return voltage;
  199. return sprintf(buf, "%d\n", voltage);
  200. }
  201. static ssize_t store_in(struct device *dev, struct device_attribute *attr,
  202. const char *buf, size_t count)
  203. {
  204. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  205. struct nct7802_data *data = dev_get_drvdata(dev);
  206. int index = sattr->index;
  207. int nr = sattr->nr;
  208. unsigned long val;
  209. int err;
  210. err = kstrtoul(buf, 10, &val);
  211. if (err < 0)
  212. return err;
  213. err = nct7802_write_voltage(data, nr, index, val);
  214. return err ? : count;
  215. }
  216. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  217. char *buf)
  218. {
  219. struct nct7802_data *data = dev_get_drvdata(dev);
  220. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  221. int err, temp;
  222. err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
  223. if (err < 0)
  224. return err;
  225. return sprintf(buf, "%d\n", temp);
  226. }
  227. static ssize_t store_temp(struct device *dev, struct device_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  231. struct nct7802_data *data = dev_get_drvdata(dev);
  232. int nr = sattr->nr;
  233. long val;
  234. int err;
  235. err = kstrtol(buf, 10, &val);
  236. if (err < 0)
  237. return err;
  238. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
  239. err = regmap_write(data->regmap, nr, val & 0xff);
  240. return err ? : count;
  241. }
  242. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  243. char *buf)
  244. {
  245. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  246. struct nct7802_data *data = dev_get_drvdata(dev);
  247. int speed;
  248. speed = nct7802_read_fan(data, sattr->index);
  249. if (speed < 0)
  250. return speed;
  251. return sprintf(buf, "%d\n", speed);
  252. }
  253. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  254. char *buf)
  255. {
  256. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  257. struct nct7802_data *data = dev_get_drvdata(dev);
  258. int speed;
  259. speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
  260. if (speed < 0)
  261. return speed;
  262. return sprintf(buf, "%d\n", speed);
  263. }
  264. static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
  265. const char *buf, size_t count)
  266. {
  267. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  268. struct nct7802_data *data = dev_get_drvdata(dev);
  269. unsigned long val;
  270. int err;
  271. err = kstrtoul(buf, 10, &val);
  272. if (err < 0)
  273. return err;
  274. err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
  275. return err ? : count;
  276. }
  277. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  278. char *buf)
  279. {
  280. struct nct7802_data *data = dev_get_drvdata(dev);
  281. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  282. int bit = sattr->index;
  283. unsigned int val;
  284. int ret;
  285. ret = regmap_read(data->regmap, sattr->nr, &val);
  286. if (ret < 0)
  287. return ret;
  288. return sprintf(buf, "%u\n", !!(val & (1 << bit)));
  289. }
  290. static ssize_t
  291. show_beep(struct device *dev, struct device_attribute *attr, char *buf)
  292. {
  293. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  294. struct nct7802_data *data = dev_get_drvdata(dev);
  295. unsigned int regval;
  296. int err;
  297. err = regmap_read(data->regmap, sattr->nr, &regval);
  298. if (err)
  299. return err;
  300. return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
  301. }
  302. static ssize_t
  303. store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
  304. size_t count)
  305. {
  306. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  307. struct nct7802_data *data = dev_get_drvdata(dev);
  308. unsigned long val;
  309. int err;
  310. err = kstrtoul(buf, 10, &val);
  311. if (err < 0)
  312. return err;
  313. if (val > 1)
  314. return -EINVAL;
  315. err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
  316. val ? 1 << sattr->index : 0);
  317. return err ? : count;
  318. }
  319. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0x01,
  320. REG_TEMP_LSB);
  321. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp,
  322. store_temp, 0x31, 0);
  323. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp,
  324. store_temp, 0x30, 0);
  325. static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp,
  326. store_temp, 0x3a, 0);
  327. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0x02,
  328. REG_TEMP_LSB);
  329. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp,
  330. store_temp, 0x33, 0);
  331. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp,
  332. store_temp, 0x32, 0);
  333. static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp,
  334. store_temp, 0x3b, 0);
  335. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0x03,
  336. REG_TEMP_LSB);
  337. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp,
  338. store_temp, 0x35, 0);
  339. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp,
  340. store_temp, 0x34, 0);
  341. static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp,
  342. store_temp, 0x3c, 0);
  343. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 0x04, 0);
  344. static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp,
  345. store_temp, 0x37, 0);
  346. static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp,
  347. store_temp, 0x36, 0);
  348. static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp,
  349. store_temp, 0x3d, 0);
  350. static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 0x06,
  351. REG_TEMP_PECI_LSB);
  352. static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp,
  353. store_temp, 0x39, 0);
  354. static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp,
  355. store_temp, 0x38, 0);
  356. static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp,
  357. store_temp, 0x3e, 0);
  358. static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 0x07,
  359. REG_TEMP_PECI_LSB);
  360. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  361. 0x18, 0);
  362. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_alarm, NULL,
  363. 0x18, 1);
  364. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_alarm, NULL,
  365. 0x18, 2);
  366. static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, show_alarm, NULL,
  367. 0x18, 3);
  368. static SENSOR_DEVICE_ATTR_2(temp5_min_alarm, S_IRUGO, show_alarm, NULL,
  369. 0x18, 4);
  370. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  371. 0x19, 0);
  372. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
  373. 0x19, 1);
  374. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
  375. 0x19, 2);
  376. static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
  377. 0x19, 3);
  378. static SENSOR_DEVICE_ATTR_2(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
  379. 0x19, 4);
  380. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
  381. 0x1b, 0);
  382. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
  383. 0x1b, 1);
  384. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
  385. 0x1b, 2);
  386. static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, show_alarm, NULL,
  387. 0x1b, 3);
  388. static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm, S_IRUGO, show_alarm, NULL,
  389. 0x1b, 4);
  390. static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_alarm, NULL, 0x17, 0);
  391. static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_alarm, NULL, 0x17, 1);
  392. static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_alarm, NULL, 0x17, 2);
  393. static SENSOR_DEVICE_ATTR_2(temp1_beep, S_IRUGO | S_IWUSR, show_beep,
  394. store_beep, 0x5c, 0);
  395. static SENSOR_DEVICE_ATTR_2(temp2_beep, S_IRUGO | S_IWUSR, show_beep,
  396. store_beep, 0x5c, 1);
  397. static SENSOR_DEVICE_ATTR_2(temp3_beep, S_IRUGO | S_IWUSR, show_beep,
  398. store_beep, 0x5c, 2);
  399. static SENSOR_DEVICE_ATTR_2(temp4_beep, S_IRUGO | S_IWUSR, show_beep,
  400. store_beep, 0x5c, 3);
  401. static SENSOR_DEVICE_ATTR_2(temp5_beep, S_IRUGO | S_IWUSR, show_beep,
  402. store_beep, 0x5c, 4);
  403. static SENSOR_DEVICE_ATTR_2(temp6_beep, S_IRUGO | S_IWUSR, show_beep,
  404. store_beep, 0x5c, 5);
  405. static struct attribute *nct7802_temp_attrs[] = {
  406. &sensor_dev_attr_temp1_input.dev_attr.attr,
  407. &sensor_dev_attr_temp1_min.dev_attr.attr,
  408. &sensor_dev_attr_temp1_max.dev_attr.attr,
  409. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  410. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  411. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  412. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  413. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  414. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  415. &sensor_dev_attr_temp2_input.dev_attr.attr, /* 9 */
  416. &sensor_dev_attr_temp2_min.dev_attr.attr,
  417. &sensor_dev_attr_temp2_max.dev_attr.attr,
  418. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  419. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  420. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  421. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  422. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  423. &sensor_dev_attr_temp2_beep.dev_attr.attr,
  424. &sensor_dev_attr_temp3_input.dev_attr.attr, /* 18 */
  425. &sensor_dev_attr_temp3_min.dev_attr.attr,
  426. &sensor_dev_attr_temp3_max.dev_attr.attr,
  427. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  428. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  429. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  430. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  431. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  432. &sensor_dev_attr_temp3_beep.dev_attr.attr,
  433. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 27 */
  434. &sensor_dev_attr_temp4_min.dev_attr.attr,
  435. &sensor_dev_attr_temp4_max.dev_attr.attr,
  436. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  437. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  438. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  439. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  440. &sensor_dev_attr_temp4_beep.dev_attr.attr,
  441. &sensor_dev_attr_temp5_input.dev_attr.attr, /* 35 */
  442. &sensor_dev_attr_temp5_min.dev_attr.attr,
  443. &sensor_dev_attr_temp5_max.dev_attr.attr,
  444. &sensor_dev_attr_temp5_crit.dev_attr.attr,
  445. &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
  446. &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
  447. &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
  448. &sensor_dev_attr_temp5_beep.dev_attr.attr,
  449. &sensor_dev_attr_temp6_input.dev_attr.attr, /* 43 */
  450. &sensor_dev_attr_temp6_beep.dev_attr.attr,
  451. NULL
  452. };
  453. static umode_t nct7802_temp_is_visible(struct kobject *kobj,
  454. struct attribute *attr, int index)
  455. {
  456. struct device *dev = container_of(kobj, struct device, kobj);
  457. struct nct7802_data *data = dev_get_drvdata(dev);
  458. unsigned int reg;
  459. int err;
  460. err = regmap_read(data->regmap, REG_MODE, &reg);
  461. if (err < 0)
  462. return 0;
  463. if (index < 9 &&
  464. (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */
  465. return 0;
  466. if (index >= 9 && index < 18 &&
  467. (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */
  468. return 0;
  469. if (index >= 18 && index < 27 && (reg & 0x30) != 0x20) /* RD3 */
  470. return 0;
  471. if (index >= 27 && index < 35) /* local */
  472. return attr->mode;
  473. err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
  474. if (err < 0)
  475. return 0;
  476. if (index >= 35 && index < 43 && !(reg & 0x01)) /* PECI 0 */
  477. return 0;
  478. if (index >= 0x43 && (!(reg & 0x02))) /* PECI 1 */
  479. return 0;
  480. return attr->mode;
  481. }
  482. static struct attribute_group nct7802_temp_group = {
  483. .attrs = nct7802_temp_attrs,
  484. .is_visible = nct7802_temp_is_visible,
  485. };
  486. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
  487. static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, store_in,
  488. 0, 1);
  489. static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, store_in,
  490. 0, 2);
  491. static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 3);
  492. static SENSOR_DEVICE_ATTR_2(in0_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  493. 0x5a, 3);
  494. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
  495. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
  496. static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, store_in,
  497. 2, 1);
  498. static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, store_in,
  499. 2, 2);
  500. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 0);
  501. static SENSOR_DEVICE_ATTR_2(in2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  502. 0x5a, 0);
  503. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
  504. static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, store_in,
  505. 3, 1);
  506. static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, store_in,
  507. 3, 2);
  508. static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 1);
  509. static SENSOR_DEVICE_ATTR_2(in3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  510. 0x5a, 1);
  511. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
  512. static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, store_in,
  513. 4, 1);
  514. static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, store_in,
  515. 4, 2);
  516. static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 2);
  517. static SENSOR_DEVICE_ATTR_2(in4_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  518. 0x5a, 2);
  519. static struct attribute *nct7802_in_attrs[] = {
  520. &sensor_dev_attr_in0_input.dev_attr.attr,
  521. &sensor_dev_attr_in0_min.dev_attr.attr,
  522. &sensor_dev_attr_in0_max.dev_attr.attr,
  523. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  524. &sensor_dev_attr_in0_beep.dev_attr.attr,
  525. &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */
  526. &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */
  527. &sensor_dev_attr_in2_min.dev_attr.attr,
  528. &sensor_dev_attr_in2_max.dev_attr.attr,
  529. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  530. &sensor_dev_attr_in2_beep.dev_attr.attr,
  531. &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */
  532. &sensor_dev_attr_in3_min.dev_attr.attr,
  533. &sensor_dev_attr_in3_max.dev_attr.attr,
  534. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  535. &sensor_dev_attr_in3_beep.dev_attr.attr,
  536. &sensor_dev_attr_in4_input.dev_attr.attr, /* 17 */
  537. &sensor_dev_attr_in4_min.dev_attr.attr,
  538. &sensor_dev_attr_in4_max.dev_attr.attr,
  539. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  540. &sensor_dev_attr_in4_beep.dev_attr.attr,
  541. NULL,
  542. };
  543. static umode_t nct7802_in_is_visible(struct kobject *kobj,
  544. struct attribute *attr, int index)
  545. {
  546. struct device *dev = container_of(kobj, struct device, kobj);
  547. struct nct7802_data *data = dev_get_drvdata(dev);
  548. unsigned int reg;
  549. int err;
  550. if (index < 6) /* VCC, VCORE */
  551. return attr->mode;
  552. err = regmap_read(data->regmap, REG_MODE, &reg);
  553. if (err < 0)
  554. return 0;
  555. if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */
  556. return 0;
  557. if (index >= 11 && index < 17 && (reg & 0x0c) != 0x0c) /* VSEN2 */
  558. return 0;
  559. if (index >= 17 && (reg & 0x30) != 0x30) /* VSEN3 */
  560. return 0;
  561. return attr->mode;
  562. }
  563. static struct attribute_group nct7802_in_group = {
  564. .attrs = nct7802_in_attrs,
  565. .is_visible = nct7802_in_is_visible,
  566. };
  567. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0x10);
  568. static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan_min,
  569. store_fan_min, 0x49, 0x4c);
  570. static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 0);
  571. static SENSOR_DEVICE_ATTR_2(fan1_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  572. 0x5b, 0);
  573. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 0x11);
  574. static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan_min,
  575. store_fan_min, 0x4a, 0x4d);
  576. static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 1);
  577. static SENSOR_DEVICE_ATTR_2(fan2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  578. 0x5b, 1);
  579. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 0x12);
  580. static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan_min,
  581. store_fan_min, 0x4b, 0x4e);
  582. static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 2);
  583. static SENSOR_DEVICE_ATTR_2(fan3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  584. 0x5b, 2);
  585. static struct attribute *nct7802_fan_attrs[] = {
  586. &sensor_dev_attr_fan1_input.dev_attr.attr,
  587. &sensor_dev_attr_fan1_min.dev_attr.attr,
  588. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  589. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  590. &sensor_dev_attr_fan2_input.dev_attr.attr,
  591. &sensor_dev_attr_fan2_min.dev_attr.attr,
  592. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  593. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  594. &sensor_dev_attr_fan3_input.dev_attr.attr,
  595. &sensor_dev_attr_fan3_min.dev_attr.attr,
  596. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  597. &sensor_dev_attr_fan3_beep.dev_attr.attr,
  598. NULL
  599. };
  600. static umode_t nct7802_fan_is_visible(struct kobject *kobj,
  601. struct attribute *attr, int index)
  602. {
  603. struct device *dev = container_of(kobj, struct device, kobj);
  604. struct nct7802_data *data = dev_get_drvdata(dev);
  605. int fan = index / 4; /* 4 attributes per fan */
  606. unsigned int reg;
  607. int err;
  608. err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
  609. if (err < 0 || !(reg & (1 << fan)))
  610. return 0;
  611. return attr->mode;
  612. }
  613. static struct attribute_group nct7802_fan_group = {
  614. .attrs = nct7802_fan_attrs,
  615. .is_visible = nct7802_fan_is_visible,
  616. };
  617. static const struct attribute_group *nct7802_groups[] = {
  618. &nct7802_temp_group,
  619. &nct7802_in_group,
  620. &nct7802_fan_group,
  621. NULL
  622. };
  623. static int nct7802_detect(struct i2c_client *client,
  624. struct i2c_board_info *info)
  625. {
  626. int reg;
  627. /*
  628. * Chip identification registers are only available in bank 0,
  629. * so only attempt chip detection if bank 0 is selected
  630. */
  631. reg = i2c_smbus_read_byte_data(client, REG_BANK);
  632. if (reg != 0x00)
  633. return -ENODEV;
  634. reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
  635. if (reg != 0x50)
  636. return -ENODEV;
  637. reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
  638. if (reg != 0xc3)
  639. return -ENODEV;
  640. reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
  641. if (reg < 0 || (reg & 0xf0) != 0x20)
  642. return -ENODEV;
  643. /* Also validate lower bits of voltage and temperature registers */
  644. reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
  645. if (reg < 0 || (reg & 0x1f))
  646. return -ENODEV;
  647. reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
  648. if (reg < 0 || (reg & 0x3f))
  649. return -ENODEV;
  650. reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
  651. if (reg < 0 || (reg & 0x3f))
  652. return -ENODEV;
  653. strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
  654. return 0;
  655. }
  656. static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
  657. {
  658. return reg != REG_BANK && reg <= 0x20;
  659. }
  660. static const struct regmap_config nct7802_regmap_config = {
  661. .reg_bits = 8,
  662. .val_bits = 8,
  663. .cache_type = REGCACHE_RBTREE,
  664. .volatile_reg = nct7802_regmap_is_volatile,
  665. };
  666. static int nct7802_init_chip(struct nct7802_data *data)
  667. {
  668. int err;
  669. /* Enable ADC */
  670. err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
  671. if (err)
  672. return err;
  673. /* Enable local temperature sensor */
  674. err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40);
  675. if (err)
  676. return err;
  677. /* Enable Vcore and VCC voltage monitoring */
  678. return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
  679. }
  680. static int nct7802_probe(struct i2c_client *client,
  681. const struct i2c_device_id *id)
  682. {
  683. struct device *dev = &client->dev;
  684. struct nct7802_data *data;
  685. struct device *hwmon_dev;
  686. int ret;
  687. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  688. if (data == NULL)
  689. return -ENOMEM;
  690. data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
  691. if (IS_ERR(data->regmap))
  692. return PTR_ERR(data->regmap);
  693. mutex_init(&data->access_lock);
  694. ret = nct7802_init_chip(data);
  695. if (ret < 0)
  696. return ret;
  697. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  698. data,
  699. nct7802_groups);
  700. return PTR_ERR_OR_ZERO(hwmon_dev);
  701. }
  702. static const unsigned short nct7802_address_list[] = {
  703. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
  704. };
  705. static const struct i2c_device_id nct7802_idtable[] = {
  706. { "nct7802", 0 },
  707. { }
  708. };
  709. MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
  710. static struct i2c_driver nct7802_driver = {
  711. .class = I2C_CLASS_HWMON,
  712. .driver = {
  713. .name = DRVNAME,
  714. },
  715. .detect = nct7802_detect,
  716. .probe = nct7802_probe,
  717. .id_table = nct7802_idtable,
  718. .address_list = nct7802_address_list,
  719. };
  720. module_i2c_driver(nct7802_driver);
  721. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  722. MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
  723. MODULE_LICENSE("GPL v2");