lm80.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * lm80.c - From lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  5. * and Philip Edelbrock <phil@netroedge.com>
  6. *
  7. * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/i2c.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. /* Addresses to scan */
  33. static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
  34. 0x2e, 0x2f, I2C_CLIENT_END };
  35. /* Many LM80 constants specified below */
  36. /* The LM80 registers */
  37. #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
  38. #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
  39. #define LM80_REG_IN(nr) (0x20 + (nr))
  40. #define LM80_REG_FAN1 0x28
  41. #define LM80_REG_FAN2 0x29
  42. #define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
  43. #define LM80_REG_TEMP 0x27
  44. #define LM80_REG_TEMP_HOT_MAX 0x38
  45. #define LM80_REG_TEMP_HOT_HYST 0x39
  46. #define LM80_REG_TEMP_OS_MAX 0x3a
  47. #define LM80_REG_TEMP_OS_HYST 0x3b
  48. #define LM80_REG_CONFIG 0x00
  49. #define LM80_REG_ALARM1 0x01
  50. #define LM80_REG_ALARM2 0x02
  51. #define LM80_REG_MASK1 0x03
  52. #define LM80_REG_MASK2 0x04
  53. #define LM80_REG_FANDIV 0x05
  54. #define LM80_REG_RES 0x06
  55. #define LM96080_REG_CONV_RATE 0x07
  56. #define LM96080_REG_MAN_ID 0x3e
  57. #define LM96080_REG_DEV_ID 0x3f
  58. /*
  59. * Conversions. Rounding and limit checking is only done on the TO_REG
  60. * variants. Note that you should be a bit careful with which arguments
  61. * these macros are called: arguments may be evaluated more than once.
  62. * Fixing this is just not worth it.
  63. */
  64. #define IN_TO_REG(val) (clamp_val(((val) + 5) / 10, 0, 255))
  65. #define IN_FROM_REG(val) ((val) * 10)
  66. static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
  67. {
  68. if (rpm == 0)
  69. return 255;
  70. rpm = clamp_val(rpm, 1, 1000000);
  71. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  72. }
  73. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
  74. (val) == 255 ? 0 : 1350000/((div) * (val)))
  75. #define TEMP_FROM_REG(reg) ((reg) * 125 / 32)
  76. #define TEMP_TO_REG(temp) (DIV_ROUND_CLOSEST(clamp_val((temp), \
  77. -128000, 127000), 1000) << 8)
  78. #define DIV_FROM_REG(val) (1 << (val))
  79. enum temp_index {
  80. t_input = 0,
  81. t_hot_max,
  82. t_hot_hyst,
  83. t_os_max,
  84. t_os_hyst,
  85. t_num_temp
  86. };
  87. static const u8 temp_regs[t_num_temp] = {
  88. [t_input] = LM80_REG_TEMP,
  89. [t_hot_max] = LM80_REG_TEMP_HOT_MAX,
  90. [t_hot_hyst] = LM80_REG_TEMP_HOT_HYST,
  91. [t_os_max] = LM80_REG_TEMP_OS_MAX,
  92. [t_os_hyst] = LM80_REG_TEMP_OS_HYST,
  93. };
  94. enum in_index {
  95. i_input = 0,
  96. i_max,
  97. i_min,
  98. i_num_in
  99. };
  100. enum fan_index {
  101. f_input,
  102. f_min,
  103. f_num_fan
  104. };
  105. /*
  106. * Client data (each client gets its own)
  107. */
  108. struct lm80_data {
  109. struct i2c_client *client;
  110. struct mutex update_lock;
  111. char error; /* !=0 if error occurred during last update */
  112. char valid; /* !=0 if following fields are valid */
  113. unsigned long last_updated; /* In jiffies */
  114. u8 in[i_num_in][7]; /* Register value, 1st index is enum in_index */
  115. u8 fan[f_num_fan][2]; /* Register value, 1st index enum fan_index */
  116. u8 fan_div[2]; /* Register encoding, shifted right */
  117. s16 temp[t_num_temp]; /* Register values, normalized to 16 bit */
  118. u16 alarms; /* Register encoding, combined */
  119. };
  120. static int lm80_read_value(struct i2c_client *client, u8 reg)
  121. {
  122. return i2c_smbus_read_byte_data(client, reg);
  123. }
  124. static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
  125. {
  126. return i2c_smbus_write_byte_data(client, reg, value);
  127. }
  128. /* Called when we have found a new LM80 and after read errors */
  129. static void lm80_init_client(struct i2c_client *client)
  130. {
  131. /*
  132. * Reset all except Watchdog values and last conversion values
  133. * This sets fan-divs to 2, among others. This makes most other
  134. * initializations unnecessary
  135. */
  136. lm80_write_value(client, LM80_REG_CONFIG, 0x80);
  137. /* Set 11-bit temperature resolution */
  138. lm80_write_value(client, LM80_REG_RES, 0x08);
  139. /* Start monitoring */
  140. lm80_write_value(client, LM80_REG_CONFIG, 0x01);
  141. }
  142. static struct lm80_data *lm80_update_device(struct device *dev)
  143. {
  144. struct lm80_data *data = dev_get_drvdata(dev);
  145. struct i2c_client *client = data->client;
  146. int i;
  147. int rv;
  148. int prev_rv;
  149. struct lm80_data *ret = data;
  150. mutex_lock(&data->update_lock);
  151. if (data->error)
  152. lm80_init_client(client);
  153. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  154. dev_dbg(dev, "Starting lm80 update\n");
  155. for (i = 0; i <= 6; i++) {
  156. rv = lm80_read_value(client, LM80_REG_IN(i));
  157. if (rv < 0)
  158. goto abort;
  159. data->in[i_input][i] = rv;
  160. rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
  161. if (rv < 0)
  162. goto abort;
  163. data->in[i_min][i] = rv;
  164. rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
  165. if (rv < 0)
  166. goto abort;
  167. data->in[i_max][i] = rv;
  168. }
  169. rv = lm80_read_value(client, LM80_REG_FAN1);
  170. if (rv < 0)
  171. goto abort;
  172. data->fan[f_input][0] = rv;
  173. rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
  174. if (rv < 0)
  175. goto abort;
  176. data->fan[f_min][0] = rv;
  177. rv = lm80_read_value(client, LM80_REG_FAN2);
  178. if (rv < 0)
  179. goto abort;
  180. data->fan[f_input][1] = rv;
  181. rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
  182. if (rv < 0)
  183. goto abort;
  184. data->fan[f_min][1] = rv;
  185. prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
  186. if (rv < 0)
  187. goto abort;
  188. rv = lm80_read_value(client, LM80_REG_RES);
  189. if (rv < 0)
  190. goto abort;
  191. data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0);
  192. for (i = t_input + 1; i < t_num_temp; i++) {
  193. rv = lm80_read_value(client, temp_regs[i]);
  194. if (rv < 0)
  195. goto abort;
  196. data->temp[i] = rv << 8;
  197. }
  198. rv = lm80_read_value(client, LM80_REG_FANDIV);
  199. if (rv < 0)
  200. goto abort;
  201. data->fan_div[0] = (rv >> 2) & 0x03;
  202. data->fan_div[1] = (rv >> 4) & 0x03;
  203. prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
  204. if (rv < 0)
  205. goto abort;
  206. rv = lm80_read_value(client, LM80_REG_ALARM2);
  207. if (rv < 0)
  208. goto abort;
  209. data->alarms = prev_rv + (rv << 8);
  210. data->last_updated = jiffies;
  211. data->valid = 1;
  212. data->error = 0;
  213. }
  214. goto done;
  215. abort:
  216. ret = ERR_PTR(rv);
  217. data->valid = 0;
  218. data->error = 1;
  219. done:
  220. mutex_unlock(&data->update_lock);
  221. return ret;
  222. }
  223. /*
  224. * Sysfs stuff
  225. */
  226. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  227. char *buf)
  228. {
  229. struct lm80_data *data = lm80_update_device(dev);
  230. int index = to_sensor_dev_attr_2(attr)->index;
  231. int nr = to_sensor_dev_attr_2(attr)->nr;
  232. if (IS_ERR(data))
  233. return PTR_ERR(data);
  234. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index]));
  235. }
  236. static ssize_t set_in(struct device *dev, struct device_attribute *attr,
  237. const char *buf, size_t count)
  238. {
  239. struct lm80_data *data = dev_get_drvdata(dev);
  240. struct i2c_client *client = data->client;
  241. int index = to_sensor_dev_attr_2(attr)->index;
  242. int nr = to_sensor_dev_attr_2(attr)->nr;
  243. long val;
  244. u8 reg;
  245. int err = kstrtol(buf, 10, &val);
  246. if (err < 0)
  247. return err;
  248. reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index);
  249. mutex_lock(&data->update_lock);
  250. data->in[nr][index] = IN_TO_REG(val);
  251. lm80_write_value(client, reg, data->in[nr][index]);
  252. mutex_unlock(&data->update_lock);
  253. return count;
  254. }
  255. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  256. char *buf)
  257. {
  258. int index = to_sensor_dev_attr_2(attr)->index;
  259. int nr = to_sensor_dev_attr_2(attr)->nr;
  260. struct lm80_data *data = lm80_update_device(dev);
  261. if (IS_ERR(data))
  262. return PTR_ERR(data);
  263. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index],
  264. DIV_FROM_REG(data->fan_div[index])));
  265. }
  266. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  267. char *buf)
  268. {
  269. int nr = to_sensor_dev_attr(attr)->index;
  270. struct lm80_data *data = lm80_update_device(dev);
  271. if (IS_ERR(data))
  272. return PTR_ERR(data);
  273. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  274. }
  275. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  276. const char *buf, size_t count)
  277. {
  278. int index = to_sensor_dev_attr_2(attr)->index;
  279. int nr = to_sensor_dev_attr_2(attr)->nr;
  280. struct lm80_data *data = dev_get_drvdata(dev);
  281. struct i2c_client *client = data->client;
  282. unsigned long val;
  283. int err = kstrtoul(buf, 10, &val);
  284. if (err < 0)
  285. return err;
  286. mutex_lock(&data->update_lock);
  287. data->fan[nr][index] = FAN_TO_REG(val,
  288. DIV_FROM_REG(data->fan_div[index]));
  289. lm80_write_value(client, LM80_REG_FAN_MIN(index + 1),
  290. data->fan[nr][index]);
  291. mutex_unlock(&data->update_lock);
  292. return count;
  293. }
  294. /*
  295. * Note: we save and restore the fan minimum here, because its value is
  296. * determined in part by the fan divisor. This follows the principle of
  297. * least surprise; the user doesn't expect the fan minimum to change just
  298. * because the divisor changed.
  299. */
  300. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. int nr = to_sensor_dev_attr(attr)->index;
  304. struct lm80_data *data = dev_get_drvdata(dev);
  305. struct i2c_client *client = data->client;
  306. unsigned long min, val;
  307. u8 reg;
  308. int rv;
  309. rv = kstrtoul(buf, 10, &val);
  310. if (rv < 0)
  311. return rv;
  312. /* Save fan_min */
  313. mutex_lock(&data->update_lock);
  314. min = FAN_FROM_REG(data->fan[f_min][nr],
  315. DIV_FROM_REG(data->fan_div[nr]));
  316. switch (val) {
  317. case 1:
  318. data->fan_div[nr] = 0;
  319. break;
  320. case 2:
  321. data->fan_div[nr] = 1;
  322. break;
  323. case 4:
  324. data->fan_div[nr] = 2;
  325. break;
  326. case 8:
  327. data->fan_div[nr] = 3;
  328. break;
  329. default:
  330. dev_err(dev,
  331. "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
  332. val);
  333. mutex_unlock(&data->update_lock);
  334. return -EINVAL;
  335. }
  336. rv = lm80_read_value(client, LM80_REG_FANDIV);
  337. if (rv < 0) {
  338. mutex_unlock(&data->update_lock);
  339. return rv;
  340. }
  341. reg = (rv & ~(3 << (2 * (nr + 1))))
  342. | (data->fan_div[nr] << (2 * (nr + 1)));
  343. lm80_write_value(client, LM80_REG_FANDIV, reg);
  344. /* Restore fan_min */
  345. data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  346. lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1),
  347. data->fan[f_min][nr]);
  348. mutex_unlock(&data->update_lock);
  349. return count;
  350. }
  351. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  352. char *buf)
  353. {
  354. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  355. struct lm80_data *data = lm80_update_device(dev);
  356. if (IS_ERR(data))
  357. return PTR_ERR(data);
  358. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
  359. }
  360. static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
  361. const char *buf, size_t count)
  362. {
  363. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  364. struct lm80_data *data = dev_get_drvdata(dev);
  365. struct i2c_client *client = data->client;
  366. int nr = attr->index;
  367. long val;
  368. int err = kstrtol(buf, 10, &val);
  369. if (err < 0)
  370. return err;
  371. mutex_lock(&data->update_lock);
  372. data->temp[nr] = TEMP_TO_REG(val);
  373. lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8);
  374. mutex_unlock(&data->update_lock);
  375. return count;
  376. }
  377. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  378. char *buf)
  379. {
  380. struct lm80_data *data = lm80_update_device(dev);
  381. if (IS_ERR(data))
  382. return PTR_ERR(data);
  383. return sprintf(buf, "%u\n", data->alarms);
  384. }
  385. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  386. char *buf)
  387. {
  388. int bitnr = to_sensor_dev_attr(attr)->index;
  389. struct lm80_data *data = lm80_update_device(dev);
  390. if (IS_ERR(data))
  391. return PTR_ERR(data);
  392. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  393. }
  394. static SENSOR_DEVICE_ATTR_2(in0_min, S_IWUSR | S_IRUGO,
  395. show_in, set_in, i_min, 0);
  396. static SENSOR_DEVICE_ATTR_2(in1_min, S_IWUSR | S_IRUGO,
  397. show_in, set_in, i_min, 1);
  398. static SENSOR_DEVICE_ATTR_2(in2_min, S_IWUSR | S_IRUGO,
  399. show_in, set_in, i_min, 2);
  400. static SENSOR_DEVICE_ATTR_2(in3_min, S_IWUSR | S_IRUGO,
  401. show_in, set_in, i_min, 3);
  402. static SENSOR_DEVICE_ATTR_2(in4_min, S_IWUSR | S_IRUGO,
  403. show_in, set_in, i_min, 4);
  404. static SENSOR_DEVICE_ATTR_2(in5_min, S_IWUSR | S_IRUGO,
  405. show_in, set_in, i_min, 5);
  406. static SENSOR_DEVICE_ATTR_2(in6_min, S_IWUSR | S_IRUGO,
  407. show_in, set_in, i_min, 6);
  408. static SENSOR_DEVICE_ATTR_2(in0_max, S_IWUSR | S_IRUGO,
  409. show_in, set_in, i_max, 0);
  410. static SENSOR_DEVICE_ATTR_2(in1_max, S_IWUSR | S_IRUGO,
  411. show_in, set_in, i_max, 1);
  412. static SENSOR_DEVICE_ATTR_2(in2_max, S_IWUSR | S_IRUGO,
  413. show_in, set_in, i_max, 2);
  414. static SENSOR_DEVICE_ATTR_2(in3_max, S_IWUSR | S_IRUGO,
  415. show_in, set_in, i_max, 3);
  416. static SENSOR_DEVICE_ATTR_2(in4_max, S_IWUSR | S_IRUGO,
  417. show_in, set_in, i_max, 4);
  418. static SENSOR_DEVICE_ATTR_2(in5_max, S_IWUSR | S_IRUGO,
  419. show_in, set_in, i_max, 5);
  420. static SENSOR_DEVICE_ATTR_2(in6_max, S_IWUSR | S_IRUGO,
  421. show_in, set_in, i_max, 6);
  422. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, i_input, 0);
  423. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, i_input, 1);
  424. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, i_input, 2);
  425. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, i_input, 3);
  426. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, i_input, 4);
  427. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, i_input, 5);
  428. static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, i_input, 6);
  429. static SENSOR_DEVICE_ATTR_2(fan1_min, S_IWUSR | S_IRUGO,
  430. show_fan, set_fan_min, f_min, 0);
  431. static SENSOR_DEVICE_ATTR_2(fan2_min, S_IWUSR | S_IRUGO,
  432. show_fan, set_fan_min, f_min, 1);
  433. static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, f_input, 0);
  434. static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, f_input, 1);
  435. static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
  436. show_fan_div, set_fan_div, 0);
  437. static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
  438. show_fan_div, set_fan_div, 1);
  439. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, t_input);
  440. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
  441. set_temp, t_hot_max);
  442. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp,
  443. set_temp, t_hot_hyst);
  444. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp,
  445. set_temp, t_os_max);
  446. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp,
  447. set_temp, t_os_hyst);
  448. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  449. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  450. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  451. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  452. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  453. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
  454. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
  455. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  456. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
  457. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
  458. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
  459. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
  460. /*
  461. * Real code
  462. */
  463. static struct attribute *lm80_attrs[] = {
  464. &sensor_dev_attr_in0_min.dev_attr.attr,
  465. &sensor_dev_attr_in1_min.dev_attr.attr,
  466. &sensor_dev_attr_in2_min.dev_attr.attr,
  467. &sensor_dev_attr_in3_min.dev_attr.attr,
  468. &sensor_dev_attr_in4_min.dev_attr.attr,
  469. &sensor_dev_attr_in5_min.dev_attr.attr,
  470. &sensor_dev_attr_in6_min.dev_attr.attr,
  471. &sensor_dev_attr_in0_max.dev_attr.attr,
  472. &sensor_dev_attr_in1_max.dev_attr.attr,
  473. &sensor_dev_attr_in2_max.dev_attr.attr,
  474. &sensor_dev_attr_in3_max.dev_attr.attr,
  475. &sensor_dev_attr_in4_max.dev_attr.attr,
  476. &sensor_dev_attr_in5_max.dev_attr.attr,
  477. &sensor_dev_attr_in6_max.dev_attr.attr,
  478. &sensor_dev_attr_in0_input.dev_attr.attr,
  479. &sensor_dev_attr_in1_input.dev_attr.attr,
  480. &sensor_dev_attr_in2_input.dev_attr.attr,
  481. &sensor_dev_attr_in3_input.dev_attr.attr,
  482. &sensor_dev_attr_in4_input.dev_attr.attr,
  483. &sensor_dev_attr_in5_input.dev_attr.attr,
  484. &sensor_dev_attr_in6_input.dev_attr.attr,
  485. &sensor_dev_attr_fan1_min.dev_attr.attr,
  486. &sensor_dev_attr_fan2_min.dev_attr.attr,
  487. &sensor_dev_attr_fan1_input.dev_attr.attr,
  488. &sensor_dev_attr_fan2_input.dev_attr.attr,
  489. &sensor_dev_attr_fan1_div.dev_attr.attr,
  490. &sensor_dev_attr_fan2_div.dev_attr.attr,
  491. &sensor_dev_attr_temp1_input.dev_attr.attr,
  492. &sensor_dev_attr_temp1_max.dev_attr.attr,
  493. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  494. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  495. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  496. &dev_attr_alarms.attr,
  497. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  498. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  499. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  500. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  501. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  502. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  503. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  504. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  505. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  506. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  507. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  508. NULL
  509. };
  510. ATTRIBUTE_GROUPS(lm80);
  511. /* Return 0 if detection is successful, -ENODEV otherwise */
  512. static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
  513. {
  514. struct i2c_adapter *adapter = client->adapter;
  515. int i, cur, man_id, dev_id;
  516. const char *name = NULL;
  517. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  518. return -ENODEV;
  519. /* First check for unused bits, common to both chip types */
  520. if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
  521. || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
  522. return -ENODEV;
  523. /*
  524. * The LM96080 has manufacturer and stepping/die rev registers so we
  525. * can just check that. The LM80 does not have such registers so we
  526. * have to use a more expensive trick.
  527. */
  528. man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
  529. dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
  530. if (man_id == 0x01 && dev_id == 0x08) {
  531. /* Check more unused bits for confirmation */
  532. if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
  533. return -ENODEV;
  534. name = "lm96080";
  535. } else {
  536. /* Check 6-bit addressing */
  537. for (i = 0x2a; i <= 0x3d; i++) {
  538. cur = i2c_smbus_read_byte_data(client, i);
  539. if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
  540. || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
  541. || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
  542. return -ENODEV;
  543. }
  544. name = "lm80";
  545. }
  546. strlcpy(info->type, name, I2C_NAME_SIZE);
  547. return 0;
  548. }
  549. static int lm80_probe(struct i2c_client *client,
  550. const struct i2c_device_id *id)
  551. {
  552. struct device *dev = &client->dev;
  553. struct device *hwmon_dev;
  554. struct lm80_data *data;
  555. int rv;
  556. data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL);
  557. if (!data)
  558. return -ENOMEM;
  559. data->client = client;
  560. mutex_init(&data->update_lock);
  561. /* Initialize the LM80 chip */
  562. lm80_init_client(client);
  563. /* A few vars need to be filled upon startup */
  564. rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
  565. if (rv < 0)
  566. return rv;
  567. data->fan[f_min][0] = rv;
  568. rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
  569. if (rv < 0)
  570. return rv;
  571. data->fan[f_min][1] = rv;
  572. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  573. data, lm80_groups);
  574. return PTR_ERR_OR_ZERO(hwmon_dev);
  575. }
  576. /*
  577. * Driver data (common to all clients)
  578. */
  579. static const struct i2c_device_id lm80_id[] = {
  580. { "lm80", 0 },
  581. { "lm96080", 1 },
  582. { }
  583. };
  584. MODULE_DEVICE_TABLE(i2c, lm80_id);
  585. static struct i2c_driver lm80_driver = {
  586. .class = I2C_CLASS_HWMON,
  587. .driver = {
  588. .name = "lm80",
  589. },
  590. .probe = lm80_probe,
  591. .id_table = lm80_id,
  592. .detect = lm80_detect,
  593. .address_list = normal_i2c,
  594. };
  595. module_i2c_driver(lm80_driver);
  596. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  597. "Philip Edelbrock <phil@netroedge.com>");
  598. MODULE_DESCRIPTION("LM80 driver");
  599. MODULE_LICENSE("GPL");