nct7802.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 /* 7.2.32 Mode Selection Register */
  45. #define REG_PECI_ENABLE 0x23
  46. #define REG_FAN_ENABLE 0x24
  47. #define REG_VMON_ENABLE 0x25
  48. #define REG_PWM(x) (0x60 + (x))
  49. #define REG_SMARTFAN_EN(x) (0x64 + (x) / 2)
  50. #define SMARTFAN_EN_SHIFT(x) ((x) % 2 * 4)
  51. #define REG_VENDOR_ID 0xfd
  52. #define REG_CHIP_ID 0xfe
  53. #define REG_VERSION_ID 0xff
  54. /*
  55. * Data structures and manipulation thereof
  56. */
  57. struct nct7802_data {
  58. struct regmap *regmap;
  59. struct mutex access_lock; /* for multi-byte read and write operations */
  60. };
  61. static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
  62. char *buf)
  63. {
  64. struct nct7802_data *data = dev_get_drvdata(dev);
  65. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  66. unsigned int mode;
  67. int ret;
  68. ret = regmap_read(data->regmap, REG_MODE, &mode);
  69. if (ret < 0)
  70. return ret;
  71. return sprintf(buf, "%u\n", (mode >> (2 * sattr->index) & 3) + 2);
  72. }
  73. static ssize_t store_temp_type(struct device *dev,
  74. struct device_attribute *attr,
  75. const char *buf, size_t count)
  76. {
  77. struct nct7802_data *data = dev_get_drvdata(dev);
  78. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  79. unsigned int type;
  80. int err;
  81. err = kstrtouint(buf, 0, &type);
  82. if (err < 0)
  83. return err;
  84. if (sattr->index == 2 && type != 4) /* RD3 */
  85. return -EINVAL;
  86. if (type < 3 || type > 4)
  87. return -EINVAL;
  88. err = regmap_update_bits(data->regmap, REG_MODE,
  89. 3 << 2 * sattr->index, (type - 2) << 2 * sattr->index);
  90. return err ? : count;
  91. }
  92. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute *attr,
  93. char *buf)
  94. {
  95. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  96. struct nct7802_data *data = dev_get_drvdata(dev);
  97. unsigned int regval;
  98. int ret;
  99. if (sattr->index > 1)
  100. return sprintf(buf, "1\n");
  101. ret = regmap_read(data->regmap, 0x5E, &regval);
  102. if (ret < 0)
  103. return ret;
  104. return sprintf(buf, "%u\n", !(regval & (1 << sattr->index)));
  105. }
  106. static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
  107. char *buf)
  108. {
  109. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  110. struct nct7802_data *data = dev_get_drvdata(dev);
  111. unsigned int val;
  112. int ret;
  113. if (!attr->index)
  114. return sprintf(buf, "255\n");
  115. ret = regmap_read(data->regmap, attr->index, &val);
  116. if (ret < 0)
  117. return ret;
  118. return sprintf(buf, "%d\n", val);
  119. }
  120. static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
  121. const char *buf, size_t count)
  122. {
  123. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  124. struct nct7802_data *data = dev_get_drvdata(dev);
  125. int err;
  126. u8 val;
  127. err = kstrtou8(buf, 0, &val);
  128. if (err < 0)
  129. return err;
  130. err = regmap_write(data->regmap, attr->index, val);
  131. return err ? : count;
  132. }
  133. static ssize_t show_pwm_enable(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. struct nct7802_data *data = dev_get_drvdata(dev);
  137. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  138. unsigned int reg, enabled;
  139. int ret;
  140. ret = regmap_read(data->regmap, REG_SMARTFAN_EN(sattr->index), &reg);
  141. if (ret < 0)
  142. return ret;
  143. enabled = reg >> SMARTFAN_EN_SHIFT(sattr->index) & 1;
  144. return sprintf(buf, "%u\n", enabled + 1);
  145. }
  146. static ssize_t store_pwm_enable(struct device *dev,
  147. struct device_attribute *attr,
  148. const char *buf, size_t count)
  149. {
  150. struct nct7802_data *data = dev_get_drvdata(dev);
  151. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  152. u8 val;
  153. int ret;
  154. ret = kstrtou8(buf, 0, &val);
  155. if (ret < 0)
  156. return ret;
  157. if (val < 1 || val > 2)
  158. return -EINVAL;
  159. ret = regmap_update_bits(data->regmap, REG_SMARTFAN_EN(sattr->index),
  160. 1 << SMARTFAN_EN_SHIFT(sattr->index),
  161. (val - 1) << SMARTFAN_EN_SHIFT(sattr->index));
  162. return ret ? : count;
  163. }
  164. static int nct7802_read_temp(struct nct7802_data *data,
  165. u8 reg_temp, u8 reg_temp_low, int *temp)
  166. {
  167. unsigned int t1, t2 = 0;
  168. int err;
  169. *temp = 0;
  170. mutex_lock(&data->access_lock);
  171. err = regmap_read(data->regmap, reg_temp, &t1);
  172. if (err < 0)
  173. goto abort;
  174. t1 <<= 8;
  175. if (reg_temp_low) { /* 11 bit data */
  176. err = regmap_read(data->regmap, reg_temp_low, &t2);
  177. if (err < 0)
  178. goto abort;
  179. }
  180. t1 |= t2 & 0xe0;
  181. *temp = (s16)t1 / 32 * 125;
  182. abort:
  183. mutex_unlock(&data->access_lock);
  184. return err;
  185. }
  186. static int nct7802_read_fan(struct nct7802_data *data, u8 reg_fan)
  187. {
  188. unsigned int f1, f2;
  189. int ret;
  190. mutex_lock(&data->access_lock);
  191. ret = regmap_read(data->regmap, reg_fan, &f1);
  192. if (ret < 0)
  193. goto abort;
  194. ret = regmap_read(data->regmap, REG_FANCOUNT_LOW, &f2);
  195. if (ret < 0)
  196. goto abort;
  197. ret = (f1 << 5) | (f2 >> 3);
  198. /* convert fan count to rpm */
  199. if (ret == 0x1fff) /* maximum value, assume fan is stopped */
  200. ret = 0;
  201. else if (ret)
  202. ret = DIV_ROUND_CLOSEST(1350000U, ret);
  203. abort:
  204. mutex_unlock(&data->access_lock);
  205. return ret;
  206. }
  207. static int nct7802_read_fan_min(struct nct7802_data *data, u8 reg_fan_low,
  208. u8 reg_fan_high)
  209. {
  210. unsigned int f1, f2;
  211. int ret;
  212. mutex_lock(&data->access_lock);
  213. ret = regmap_read(data->regmap, reg_fan_low, &f1);
  214. if (ret < 0)
  215. goto abort;
  216. ret = regmap_read(data->regmap, reg_fan_high, &f2);
  217. if (ret < 0)
  218. goto abort;
  219. ret = f1 | ((f2 & 0xf8) << 5);
  220. /* convert fan count to rpm */
  221. if (ret == 0x1fff) /* maximum value, assume no limit */
  222. ret = 0;
  223. else if (ret)
  224. ret = DIV_ROUND_CLOSEST(1350000U, ret);
  225. else
  226. ret = 1350000U;
  227. abort:
  228. mutex_unlock(&data->access_lock);
  229. return ret;
  230. }
  231. static int nct7802_write_fan_min(struct nct7802_data *data, u8 reg_fan_low,
  232. u8 reg_fan_high, unsigned long limit)
  233. {
  234. int err;
  235. if (limit)
  236. limit = DIV_ROUND_CLOSEST(1350000U, limit);
  237. else
  238. limit = 0x1fff;
  239. limit = clamp_val(limit, 0, 0x1fff);
  240. mutex_lock(&data->access_lock);
  241. err = regmap_write(data->regmap, reg_fan_low, limit & 0xff);
  242. if (err < 0)
  243. goto abort;
  244. err = regmap_write(data->regmap, reg_fan_high, (limit & 0x1f00) >> 5);
  245. abort:
  246. mutex_unlock(&data->access_lock);
  247. return err;
  248. }
  249. static u8 nct7802_vmul[] = { 4, 2, 2, 2, 2 };
  250. static int nct7802_read_voltage(struct nct7802_data *data, int nr, int index)
  251. {
  252. unsigned int v1, v2;
  253. int ret;
  254. mutex_lock(&data->access_lock);
  255. if (index == 0) { /* voltage */
  256. ret = regmap_read(data->regmap, REG_VOLTAGE[nr], &v1);
  257. if (ret < 0)
  258. goto abort;
  259. ret = regmap_read(data->regmap, REG_VOLTAGE_LOW, &v2);
  260. if (ret < 0)
  261. goto abort;
  262. ret = ((v1 << 2) | (v2 >> 6)) * nct7802_vmul[nr];
  263. } else { /* limit */
  264. int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
  265. ret = regmap_read(data->regmap,
  266. REG_VOLTAGE_LIMIT_LSB[index - 1][nr], &v1);
  267. if (ret < 0)
  268. goto abort;
  269. ret = regmap_read(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
  270. &v2);
  271. if (ret < 0)
  272. goto abort;
  273. ret = (v1 | ((v2 << shift) & 0x300)) * nct7802_vmul[nr];
  274. }
  275. abort:
  276. mutex_unlock(&data->access_lock);
  277. return ret;
  278. }
  279. static int nct7802_write_voltage(struct nct7802_data *data, int nr, int index,
  280. unsigned long voltage)
  281. {
  282. int shift = 8 - REG_VOLTAGE_LIMIT_MSB_SHIFT[index - 1][nr];
  283. int err;
  284. voltage = clamp_val(voltage, 0, 0x3ff * nct7802_vmul[nr]);
  285. voltage = DIV_ROUND_CLOSEST(voltage, nct7802_vmul[nr]);
  286. mutex_lock(&data->access_lock);
  287. err = regmap_write(data->regmap,
  288. REG_VOLTAGE_LIMIT_LSB[index - 1][nr],
  289. voltage & 0xff);
  290. if (err < 0)
  291. goto abort;
  292. err = regmap_update_bits(data->regmap, REG_VOLTAGE_LIMIT_MSB[nr],
  293. 0x0300 >> shift, (voltage & 0x0300) >> shift);
  294. abort:
  295. mutex_unlock(&data->access_lock);
  296. return err;
  297. }
  298. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  299. char *buf)
  300. {
  301. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  302. struct nct7802_data *data = dev_get_drvdata(dev);
  303. int voltage;
  304. voltage = nct7802_read_voltage(data, sattr->nr, sattr->index);
  305. if (voltage < 0)
  306. return voltage;
  307. return sprintf(buf, "%d\n", voltage);
  308. }
  309. static ssize_t store_in(struct device *dev, struct device_attribute *attr,
  310. const char *buf, size_t count)
  311. {
  312. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  313. struct nct7802_data *data = dev_get_drvdata(dev);
  314. int index = sattr->index;
  315. int nr = sattr->nr;
  316. unsigned long val;
  317. int err;
  318. err = kstrtoul(buf, 10, &val);
  319. if (err < 0)
  320. return err;
  321. err = nct7802_write_voltage(data, nr, index, val);
  322. return err ? : count;
  323. }
  324. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  325. char *buf)
  326. {
  327. struct nct7802_data *data = dev_get_drvdata(dev);
  328. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  329. int err, temp;
  330. err = nct7802_read_temp(data, sattr->nr, sattr->index, &temp);
  331. if (err < 0)
  332. return err;
  333. return sprintf(buf, "%d\n", temp);
  334. }
  335. static ssize_t store_temp(struct device *dev, struct device_attribute *attr,
  336. const char *buf, size_t count)
  337. {
  338. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  339. struct nct7802_data *data = dev_get_drvdata(dev);
  340. int nr = sattr->nr;
  341. long val;
  342. int err;
  343. err = kstrtol(buf, 10, &val);
  344. if (err < 0)
  345. return err;
  346. val = DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), 1000);
  347. err = regmap_write(data->regmap, nr, val & 0xff);
  348. return err ? : count;
  349. }
  350. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  351. char *buf)
  352. {
  353. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  354. struct nct7802_data *data = dev_get_drvdata(dev);
  355. int speed;
  356. speed = nct7802_read_fan(data, sattr->index);
  357. if (speed < 0)
  358. return speed;
  359. return sprintf(buf, "%d\n", speed);
  360. }
  361. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  362. char *buf)
  363. {
  364. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  365. struct nct7802_data *data = dev_get_drvdata(dev);
  366. int speed;
  367. speed = nct7802_read_fan_min(data, sattr->nr, sattr->index);
  368. if (speed < 0)
  369. return speed;
  370. return sprintf(buf, "%d\n", speed);
  371. }
  372. static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
  373. const char *buf, size_t count)
  374. {
  375. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  376. struct nct7802_data *data = dev_get_drvdata(dev);
  377. unsigned long val;
  378. int err;
  379. err = kstrtoul(buf, 10, &val);
  380. if (err < 0)
  381. return err;
  382. err = nct7802_write_fan_min(data, sattr->nr, sattr->index, val);
  383. return err ? : count;
  384. }
  385. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  386. char *buf)
  387. {
  388. struct nct7802_data *data = dev_get_drvdata(dev);
  389. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  390. int bit = sattr->index;
  391. unsigned int val;
  392. int ret;
  393. ret = regmap_read(data->regmap, sattr->nr, &val);
  394. if (ret < 0)
  395. return ret;
  396. return sprintf(buf, "%u\n", !!(val & (1 << bit)));
  397. }
  398. static ssize_t
  399. show_beep(struct device *dev, struct device_attribute *attr, char *buf)
  400. {
  401. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  402. struct nct7802_data *data = dev_get_drvdata(dev);
  403. unsigned int regval;
  404. int err;
  405. err = regmap_read(data->regmap, sattr->nr, &regval);
  406. if (err)
  407. return err;
  408. return sprintf(buf, "%u\n", !!(regval & (1 << sattr->index)));
  409. }
  410. static ssize_t
  411. store_beep(struct device *dev, struct device_attribute *attr, const char *buf,
  412. size_t count)
  413. {
  414. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  415. struct nct7802_data *data = dev_get_drvdata(dev);
  416. unsigned long val;
  417. int err;
  418. err = kstrtoul(buf, 10, &val);
  419. if (err < 0)
  420. return err;
  421. if (val > 1)
  422. return -EINVAL;
  423. err = regmap_update_bits(data->regmap, sattr->nr, 1 << sattr->index,
  424. val ? 1 << sattr->index : 0);
  425. return err ? : count;
  426. }
  427. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR,
  428. show_temp_type, store_temp_type, 0);
  429. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0x01,
  430. REG_TEMP_LSB);
  431. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp,
  432. store_temp, 0x31, 0);
  433. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp,
  434. store_temp, 0x30, 0);
  435. static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp,
  436. store_temp, 0x3a, 0);
  437. static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR,
  438. show_temp_type, store_temp_type, 1);
  439. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0x02,
  440. REG_TEMP_LSB);
  441. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp,
  442. store_temp, 0x33, 0);
  443. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp,
  444. store_temp, 0x32, 0);
  445. static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp,
  446. store_temp, 0x3b, 0);
  447. static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR,
  448. show_temp_type, store_temp_type, 2);
  449. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0x03,
  450. REG_TEMP_LSB);
  451. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp,
  452. store_temp, 0x35, 0);
  453. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp,
  454. store_temp, 0x34, 0);
  455. static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp,
  456. store_temp, 0x3c, 0);
  457. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 0x04, 0);
  458. static SENSOR_DEVICE_ATTR_2(temp4_min, S_IRUGO | S_IWUSR, show_temp,
  459. store_temp, 0x37, 0);
  460. static SENSOR_DEVICE_ATTR_2(temp4_max, S_IRUGO | S_IWUSR, show_temp,
  461. store_temp, 0x36, 0);
  462. static SENSOR_DEVICE_ATTR_2(temp4_crit, S_IRUGO | S_IWUSR, show_temp,
  463. store_temp, 0x3d, 0);
  464. static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 0x06,
  465. REG_TEMP_PECI_LSB);
  466. static SENSOR_DEVICE_ATTR_2(temp5_min, S_IRUGO | S_IWUSR, show_temp,
  467. store_temp, 0x39, 0);
  468. static SENSOR_DEVICE_ATTR_2(temp5_max, S_IRUGO | S_IWUSR, show_temp,
  469. store_temp, 0x38, 0);
  470. static SENSOR_DEVICE_ATTR_2(temp5_crit, S_IRUGO | S_IWUSR, show_temp,
  471. store_temp, 0x3e, 0);
  472. static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 0x07,
  473. REG_TEMP_PECI_LSB);
  474. static SENSOR_DEVICE_ATTR_2(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  475. 0x18, 0);
  476. static SENSOR_DEVICE_ATTR_2(temp2_min_alarm, S_IRUGO, show_alarm, NULL,
  477. 0x18, 1);
  478. static SENSOR_DEVICE_ATTR_2(temp3_min_alarm, S_IRUGO, show_alarm, NULL,
  479. 0x18, 2);
  480. static SENSOR_DEVICE_ATTR_2(temp4_min_alarm, S_IRUGO, show_alarm, NULL,
  481. 0x18, 3);
  482. static SENSOR_DEVICE_ATTR_2(temp5_min_alarm, S_IRUGO, show_alarm, NULL,
  483. 0x18, 4);
  484. static SENSOR_DEVICE_ATTR_2(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  485. 0x19, 0);
  486. static SENSOR_DEVICE_ATTR_2(temp2_max_alarm, S_IRUGO, show_alarm, NULL,
  487. 0x19, 1);
  488. static SENSOR_DEVICE_ATTR_2(temp3_max_alarm, S_IRUGO, show_alarm, NULL,
  489. 0x19, 2);
  490. static SENSOR_DEVICE_ATTR_2(temp4_max_alarm, S_IRUGO, show_alarm, NULL,
  491. 0x19, 3);
  492. static SENSOR_DEVICE_ATTR_2(temp5_max_alarm, S_IRUGO, show_alarm, NULL,
  493. 0x19, 4);
  494. static SENSOR_DEVICE_ATTR_2(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
  495. 0x1b, 0);
  496. static SENSOR_DEVICE_ATTR_2(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
  497. 0x1b, 1);
  498. static SENSOR_DEVICE_ATTR_2(temp3_crit_alarm, S_IRUGO, show_alarm, NULL,
  499. 0x1b, 2);
  500. static SENSOR_DEVICE_ATTR_2(temp4_crit_alarm, S_IRUGO, show_alarm, NULL,
  501. 0x1b, 3);
  502. static SENSOR_DEVICE_ATTR_2(temp5_crit_alarm, S_IRUGO, show_alarm, NULL,
  503. 0x1b, 4);
  504. static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_alarm, NULL, 0x17, 0);
  505. static SENSOR_DEVICE_ATTR_2(temp2_fault, S_IRUGO, show_alarm, NULL, 0x17, 1);
  506. static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_alarm, NULL, 0x17, 2);
  507. static SENSOR_DEVICE_ATTR_2(temp1_beep, S_IRUGO | S_IWUSR, show_beep,
  508. store_beep, 0x5c, 0);
  509. static SENSOR_DEVICE_ATTR_2(temp2_beep, S_IRUGO | S_IWUSR, show_beep,
  510. store_beep, 0x5c, 1);
  511. static SENSOR_DEVICE_ATTR_2(temp3_beep, S_IRUGO | S_IWUSR, show_beep,
  512. store_beep, 0x5c, 2);
  513. static SENSOR_DEVICE_ATTR_2(temp4_beep, S_IRUGO | S_IWUSR, show_beep,
  514. store_beep, 0x5c, 3);
  515. static SENSOR_DEVICE_ATTR_2(temp5_beep, S_IRUGO | S_IWUSR, show_beep,
  516. store_beep, 0x5c, 4);
  517. static SENSOR_DEVICE_ATTR_2(temp6_beep, S_IRUGO | S_IWUSR, show_beep,
  518. store_beep, 0x5c, 5);
  519. static struct attribute *nct7802_temp_attrs[] = {
  520. &sensor_dev_attr_temp1_type.dev_attr.attr,
  521. &sensor_dev_attr_temp1_input.dev_attr.attr,
  522. &sensor_dev_attr_temp1_min.dev_attr.attr,
  523. &sensor_dev_attr_temp1_max.dev_attr.attr,
  524. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  525. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  526. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  527. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  528. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  529. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  530. &sensor_dev_attr_temp2_type.dev_attr.attr, /* 10 */
  531. &sensor_dev_attr_temp2_input.dev_attr.attr,
  532. &sensor_dev_attr_temp2_min.dev_attr.attr,
  533. &sensor_dev_attr_temp2_max.dev_attr.attr,
  534. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  535. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  536. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  537. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  538. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  539. &sensor_dev_attr_temp2_beep.dev_attr.attr,
  540. &sensor_dev_attr_temp3_type.dev_attr.attr, /* 20 */
  541. &sensor_dev_attr_temp3_input.dev_attr.attr,
  542. &sensor_dev_attr_temp3_min.dev_attr.attr,
  543. &sensor_dev_attr_temp3_max.dev_attr.attr,
  544. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  545. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  546. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  547. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  548. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  549. &sensor_dev_attr_temp3_beep.dev_attr.attr,
  550. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 30 */
  551. &sensor_dev_attr_temp4_min.dev_attr.attr,
  552. &sensor_dev_attr_temp4_max.dev_attr.attr,
  553. &sensor_dev_attr_temp4_crit.dev_attr.attr,
  554. &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
  555. &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
  556. &sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
  557. &sensor_dev_attr_temp4_beep.dev_attr.attr,
  558. &sensor_dev_attr_temp5_input.dev_attr.attr, /* 38 */
  559. &sensor_dev_attr_temp5_min.dev_attr.attr,
  560. &sensor_dev_attr_temp5_max.dev_attr.attr,
  561. &sensor_dev_attr_temp5_crit.dev_attr.attr,
  562. &sensor_dev_attr_temp5_min_alarm.dev_attr.attr,
  563. &sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
  564. &sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
  565. &sensor_dev_attr_temp5_beep.dev_attr.attr,
  566. &sensor_dev_attr_temp6_input.dev_attr.attr, /* 46 */
  567. &sensor_dev_attr_temp6_beep.dev_attr.attr,
  568. NULL
  569. };
  570. static umode_t nct7802_temp_is_visible(struct kobject *kobj,
  571. struct attribute *attr, int index)
  572. {
  573. struct device *dev = container_of(kobj, struct device, kobj);
  574. struct nct7802_data *data = dev_get_drvdata(dev);
  575. unsigned int reg;
  576. int err;
  577. err = regmap_read(data->regmap, REG_MODE, &reg);
  578. if (err < 0)
  579. return 0;
  580. if (index < 10 &&
  581. (reg & 03) != 0x01 && (reg & 0x03) != 0x02) /* RD1 */
  582. return 0;
  583. if (index >= 10 && index < 20 &&
  584. (reg & 0x0c) != 0x04 && (reg & 0x0c) != 0x08) /* RD2 */
  585. return 0;
  586. if (index >= 20 && index < 30 && (reg & 0x30) != 0x20) /* RD3 */
  587. return 0;
  588. if (index >= 30 && index < 38) /* local */
  589. return attr->mode;
  590. err = regmap_read(data->regmap, REG_PECI_ENABLE, &reg);
  591. if (err < 0)
  592. return 0;
  593. if (index >= 38 && index < 46 && !(reg & 0x01)) /* PECI 0 */
  594. return 0;
  595. if (index >= 0x46 && (!(reg & 0x02))) /* PECI 1 */
  596. return 0;
  597. return attr->mode;
  598. }
  599. static struct attribute_group nct7802_temp_group = {
  600. .attrs = nct7802_temp_attrs,
  601. .is_visible = nct7802_temp_is_visible,
  602. };
  603. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
  604. static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, store_in,
  605. 0, 1);
  606. static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, store_in,
  607. 0, 2);
  608. static SENSOR_DEVICE_ATTR_2(in0_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 3);
  609. static SENSOR_DEVICE_ATTR_2(in0_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  610. 0x5a, 3);
  611. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
  612. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
  613. static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, store_in,
  614. 2, 1);
  615. static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, store_in,
  616. 2, 2);
  617. static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 0);
  618. static SENSOR_DEVICE_ATTR_2(in2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  619. 0x5a, 0);
  620. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
  621. static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, store_in,
  622. 3, 1);
  623. static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, store_in,
  624. 3, 2);
  625. static SENSOR_DEVICE_ATTR_2(in3_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 1);
  626. static SENSOR_DEVICE_ATTR_2(in3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  627. 0x5a, 1);
  628. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
  629. static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, store_in,
  630. 4, 1);
  631. static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, store_in,
  632. 4, 2);
  633. static SENSOR_DEVICE_ATTR_2(in4_alarm, S_IRUGO, show_alarm, NULL, 0x1e, 2);
  634. static SENSOR_DEVICE_ATTR_2(in4_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  635. 0x5a, 2);
  636. static struct attribute *nct7802_in_attrs[] = {
  637. &sensor_dev_attr_in0_input.dev_attr.attr,
  638. &sensor_dev_attr_in0_min.dev_attr.attr,
  639. &sensor_dev_attr_in0_max.dev_attr.attr,
  640. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  641. &sensor_dev_attr_in0_beep.dev_attr.attr,
  642. &sensor_dev_attr_in1_input.dev_attr.attr, /* 5 */
  643. &sensor_dev_attr_in2_input.dev_attr.attr, /* 6 */
  644. &sensor_dev_attr_in2_min.dev_attr.attr,
  645. &sensor_dev_attr_in2_max.dev_attr.attr,
  646. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  647. &sensor_dev_attr_in2_beep.dev_attr.attr,
  648. &sensor_dev_attr_in3_input.dev_attr.attr, /* 11 */
  649. &sensor_dev_attr_in3_min.dev_attr.attr,
  650. &sensor_dev_attr_in3_max.dev_attr.attr,
  651. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  652. &sensor_dev_attr_in3_beep.dev_attr.attr,
  653. &sensor_dev_attr_in4_input.dev_attr.attr, /* 17 */
  654. &sensor_dev_attr_in4_min.dev_attr.attr,
  655. &sensor_dev_attr_in4_max.dev_attr.attr,
  656. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  657. &sensor_dev_attr_in4_beep.dev_attr.attr,
  658. NULL,
  659. };
  660. static umode_t nct7802_in_is_visible(struct kobject *kobj,
  661. struct attribute *attr, int index)
  662. {
  663. struct device *dev = container_of(kobj, struct device, kobj);
  664. struct nct7802_data *data = dev_get_drvdata(dev);
  665. unsigned int reg;
  666. int err;
  667. if (index < 6) /* VCC, VCORE */
  668. return attr->mode;
  669. err = regmap_read(data->regmap, REG_MODE, &reg);
  670. if (err < 0)
  671. return 0;
  672. if (index >= 6 && index < 11 && (reg & 0x03) != 0x03) /* VSEN1 */
  673. return 0;
  674. if (index >= 11 && index < 17 && (reg & 0x0c) != 0x0c) /* VSEN2 */
  675. return 0;
  676. if (index >= 17 && (reg & 0x30) != 0x30) /* VSEN3 */
  677. return 0;
  678. return attr->mode;
  679. }
  680. static struct attribute_group nct7802_in_group = {
  681. .attrs = nct7802_in_attrs,
  682. .is_visible = nct7802_in_is_visible,
  683. };
  684. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0x10);
  685. static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan_min,
  686. store_fan_min, 0x49, 0x4c);
  687. static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 0);
  688. static SENSOR_DEVICE_ATTR_2(fan1_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  689. 0x5b, 0);
  690. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 0x11);
  691. static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan_min,
  692. store_fan_min, 0x4a, 0x4d);
  693. static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 1);
  694. static SENSOR_DEVICE_ATTR_2(fan2_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  695. 0x5b, 1);
  696. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 0x12);
  697. static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan_min,
  698. store_fan_min, 0x4b, 0x4e);
  699. static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_alarm, NULL, 0x1a, 2);
  700. static SENSOR_DEVICE_ATTR_2(fan3_beep, S_IRUGO | S_IWUSR, show_beep, store_beep,
  701. 0x5b, 2);
  702. /* 7.2.89 Fan Control Output Type */
  703. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
  704. static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
  705. static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
  706. /* 7.2.91... Fan Control Output Value */
  707. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, store_pwm,
  708. REG_PWM(0));
  709. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, store_pwm,
  710. REG_PWM(1));
  711. static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, store_pwm,
  712. REG_PWM(2));
  713. /* 7.2.95... Temperature to Fan mapping Relationships Register */
  714. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  715. store_pwm_enable, 0);
  716. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  717. store_pwm_enable, 1);
  718. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
  719. store_pwm_enable, 2);
  720. static struct attribute *nct7802_fan_attrs[] = {
  721. &sensor_dev_attr_fan1_input.dev_attr.attr,
  722. &sensor_dev_attr_fan1_min.dev_attr.attr,
  723. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  724. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  725. &sensor_dev_attr_fan2_input.dev_attr.attr,
  726. &sensor_dev_attr_fan2_min.dev_attr.attr,
  727. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  728. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  729. &sensor_dev_attr_fan3_input.dev_attr.attr,
  730. &sensor_dev_attr_fan3_min.dev_attr.attr,
  731. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  732. &sensor_dev_attr_fan3_beep.dev_attr.attr,
  733. NULL
  734. };
  735. static umode_t nct7802_fan_is_visible(struct kobject *kobj,
  736. struct attribute *attr, int index)
  737. {
  738. struct device *dev = container_of(kobj, struct device, kobj);
  739. struct nct7802_data *data = dev_get_drvdata(dev);
  740. int fan = index / 4; /* 4 attributes per fan */
  741. unsigned int reg;
  742. int err;
  743. err = regmap_read(data->regmap, REG_FAN_ENABLE, &reg);
  744. if (err < 0 || !(reg & (1 << fan)))
  745. return 0;
  746. return attr->mode;
  747. }
  748. static struct attribute_group nct7802_fan_group = {
  749. .attrs = nct7802_fan_attrs,
  750. .is_visible = nct7802_fan_is_visible,
  751. };
  752. static struct attribute *nct7802_pwm_attrs[] = {
  753. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  754. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  755. &sensor_dev_attr_pwm1.dev_attr.attr,
  756. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  757. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  758. &sensor_dev_attr_pwm2.dev_attr.attr,
  759. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  760. &sensor_dev_attr_pwm3_mode.dev_attr.attr,
  761. &sensor_dev_attr_pwm3.dev_attr.attr,
  762. NULL
  763. };
  764. static struct attribute_group nct7802_pwm_group = {
  765. .attrs = nct7802_pwm_attrs,
  766. };
  767. /* 7.2.115... 0x80-0x83, 0x84 Temperature (X-axis) transition */
  768. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
  769. show_temp, store_temp, 0x80, 0);
  770. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
  771. show_temp, store_temp, 0x81, 0);
  772. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
  773. show_temp, store_temp, 0x82, 0);
  774. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
  775. show_temp, store_temp, 0x83, 0);
  776. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point5_temp, S_IRUGO | S_IWUSR,
  777. show_temp, store_temp, 0x84, 0);
  778. /* 7.2.120... 0x85-0x88 PWM (Y-axis) transition */
  779. static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
  780. show_pwm, store_pwm, 0x85);
  781. static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
  782. show_pwm, store_pwm, 0x86);
  783. static SENSOR_DEVICE_ATTR(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
  784. show_pwm, store_pwm, 0x87);
  785. static SENSOR_DEVICE_ATTR(pwm1_auto_point4_pwm, S_IRUGO | S_IWUSR,
  786. show_pwm, store_pwm, 0x88);
  787. static SENSOR_DEVICE_ATTR(pwm1_auto_point5_pwm, S_IRUGO, show_pwm, NULL, 0);
  788. /* 7.2.124 Table 2 X-axis Transition Point 1 Register */
  789. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
  790. show_temp, store_temp, 0x90, 0);
  791. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
  792. show_temp, store_temp, 0x91, 0);
  793. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
  794. show_temp, store_temp, 0x92, 0);
  795. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
  796. show_temp, store_temp, 0x93, 0);
  797. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point5_temp, S_IRUGO | S_IWUSR,
  798. show_temp, store_temp, 0x94, 0);
  799. /* 7.2.129 Table 2 Y-axis Transition Point 1 Register */
  800. static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
  801. show_pwm, store_pwm, 0x95);
  802. static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
  803. show_pwm, store_pwm, 0x96);
  804. static SENSOR_DEVICE_ATTR(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
  805. show_pwm, store_pwm, 0x97);
  806. static SENSOR_DEVICE_ATTR(pwm2_auto_point4_pwm, S_IRUGO | S_IWUSR,
  807. show_pwm, store_pwm, 0x98);
  808. static SENSOR_DEVICE_ATTR(pwm2_auto_point5_pwm, S_IRUGO, show_pwm, NULL, 0);
  809. /* 7.2.133 Table 3 X-axis Transition Point 1 Register */
  810. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
  811. show_temp, store_temp, 0xA0, 0);
  812. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
  813. show_temp, store_temp, 0xA1, 0);
  814. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
  815. show_temp, store_temp, 0xA2, 0);
  816. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
  817. show_temp, store_temp, 0xA3, 0);
  818. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point5_temp, S_IRUGO | S_IWUSR,
  819. show_temp, store_temp, 0xA4, 0);
  820. /* 7.2.138 Table 3 Y-axis Transition Point 1 Register */
  821. static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
  822. show_pwm, store_pwm, 0xA5);
  823. static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
  824. show_pwm, store_pwm, 0xA6);
  825. static SENSOR_DEVICE_ATTR(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
  826. show_pwm, store_pwm, 0xA7);
  827. static SENSOR_DEVICE_ATTR(pwm3_auto_point4_pwm, S_IRUGO | S_IWUSR,
  828. show_pwm, store_pwm, 0xA8);
  829. static SENSOR_DEVICE_ATTR(pwm3_auto_point5_pwm, S_IRUGO, show_pwm, NULL, 0);
  830. static struct attribute *nct7802_auto_point_attrs[] = {
  831. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  832. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  833. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  834. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  835. &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
  836. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  837. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  838. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  839. &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
  840. &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
  841. &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
  842. &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
  843. &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
  844. &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
  845. &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
  846. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  847. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  848. &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
  849. &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
  850. &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
  851. &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
  852. &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
  853. &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
  854. &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
  855. &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
  856. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  857. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  858. &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
  859. &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
  860. &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
  861. NULL
  862. };
  863. static struct attribute_group nct7802_auto_point_group = {
  864. .attrs = nct7802_auto_point_attrs,
  865. };
  866. static const struct attribute_group *nct7802_groups[] = {
  867. &nct7802_temp_group,
  868. &nct7802_in_group,
  869. &nct7802_fan_group,
  870. &nct7802_pwm_group,
  871. &nct7802_auto_point_group,
  872. NULL
  873. };
  874. static int nct7802_detect(struct i2c_client *client,
  875. struct i2c_board_info *info)
  876. {
  877. int reg;
  878. /*
  879. * Chip identification registers are only available in bank 0,
  880. * so only attempt chip detection if bank 0 is selected
  881. */
  882. reg = i2c_smbus_read_byte_data(client, REG_BANK);
  883. if (reg != 0x00)
  884. return -ENODEV;
  885. reg = i2c_smbus_read_byte_data(client, REG_VENDOR_ID);
  886. if (reg != 0x50)
  887. return -ENODEV;
  888. reg = i2c_smbus_read_byte_data(client, REG_CHIP_ID);
  889. if (reg != 0xc3)
  890. return -ENODEV;
  891. reg = i2c_smbus_read_byte_data(client, REG_VERSION_ID);
  892. if (reg < 0 || (reg & 0xf0) != 0x20)
  893. return -ENODEV;
  894. /* Also validate lower bits of voltage and temperature registers */
  895. reg = i2c_smbus_read_byte_data(client, REG_TEMP_LSB);
  896. if (reg < 0 || (reg & 0x1f))
  897. return -ENODEV;
  898. reg = i2c_smbus_read_byte_data(client, REG_TEMP_PECI_LSB);
  899. if (reg < 0 || (reg & 0x3f))
  900. return -ENODEV;
  901. reg = i2c_smbus_read_byte_data(client, REG_VOLTAGE_LOW);
  902. if (reg < 0 || (reg & 0x3f))
  903. return -ENODEV;
  904. strlcpy(info->type, "nct7802", I2C_NAME_SIZE);
  905. return 0;
  906. }
  907. static bool nct7802_regmap_is_volatile(struct device *dev, unsigned int reg)
  908. {
  909. return (reg != REG_BANK && reg <= 0x20) ||
  910. (reg >= REG_PWM(0) && reg <= REG_PWM(2));
  911. }
  912. static const struct regmap_config nct7802_regmap_config = {
  913. .reg_bits = 8,
  914. .val_bits = 8,
  915. .cache_type = REGCACHE_RBTREE,
  916. .volatile_reg = nct7802_regmap_is_volatile,
  917. };
  918. static int nct7802_init_chip(struct nct7802_data *data)
  919. {
  920. int err;
  921. /* Enable ADC */
  922. err = regmap_update_bits(data->regmap, REG_START, 0x01, 0x01);
  923. if (err)
  924. return err;
  925. /* Enable local temperature sensor */
  926. err = regmap_update_bits(data->regmap, REG_MODE, 0x40, 0x40);
  927. if (err)
  928. return err;
  929. /* Enable Vcore and VCC voltage monitoring */
  930. return regmap_update_bits(data->regmap, REG_VMON_ENABLE, 0x03, 0x03);
  931. }
  932. static int nct7802_probe(struct i2c_client *client,
  933. const struct i2c_device_id *id)
  934. {
  935. struct device *dev = &client->dev;
  936. struct nct7802_data *data;
  937. struct device *hwmon_dev;
  938. int ret;
  939. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  940. if (data == NULL)
  941. return -ENOMEM;
  942. data->regmap = devm_regmap_init_i2c(client, &nct7802_regmap_config);
  943. if (IS_ERR(data->regmap))
  944. return PTR_ERR(data->regmap);
  945. mutex_init(&data->access_lock);
  946. ret = nct7802_init_chip(data);
  947. if (ret < 0)
  948. return ret;
  949. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  950. data,
  951. nct7802_groups);
  952. return PTR_ERR_OR_ZERO(hwmon_dev);
  953. }
  954. static const unsigned short nct7802_address_list[] = {
  955. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
  956. };
  957. static const struct i2c_device_id nct7802_idtable[] = {
  958. { "nct7802", 0 },
  959. { }
  960. };
  961. MODULE_DEVICE_TABLE(i2c, nct7802_idtable);
  962. static struct i2c_driver nct7802_driver = {
  963. .class = I2C_CLASS_HWMON,
  964. .driver = {
  965. .name = DRVNAME,
  966. },
  967. .detect = nct7802_detect,
  968. .probe = nct7802_probe,
  969. .id_table = nct7802_idtable,
  970. .address_list = nct7802_address_list,
  971. };
  972. module_i2c_driver(nct7802_driver);
  973. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  974. MODULE_DESCRIPTION("NCT7802Y Hardware Monitoring Driver");
  975. MODULE_LICENSE("GPL v2");