lm80.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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) (SENSORS_LIMIT(((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 = SENSORS_LIMIT(rpm, 1, 1000000);
  71. return SENSORS_LIMIT((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. static inline long TEMP_FROM_REG(u16 temp)
  76. {
  77. long res;
  78. temp >>= 4;
  79. if (temp < 0x0800)
  80. res = 625 * (long) temp;
  81. else
  82. res = ((long) temp - 0x01000) * 625;
  83. return res / 10;
  84. }
  85. #define TEMP_LIMIT_FROM_REG(val) (((val) > 0x80 ? \
  86. (val) - 0x100 : (val)) * 1000)
  87. #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val) < 0 ? \
  88. ((val) - 500) / 1000 : ((val) + 500) / 1000, 0, 255)
  89. #define DIV_FROM_REG(val) (1 << (val))
  90. /*
  91. * Client data (each client gets its own)
  92. */
  93. struct lm80_data {
  94. struct device *hwmon_dev;
  95. struct mutex update_lock;
  96. char error; /* !=0 if error occurred during last update */
  97. char valid; /* !=0 if following fields are valid */
  98. unsigned long last_updated; /* In jiffies */
  99. u8 in[7]; /* Register value */
  100. u8 in_max[7]; /* Register value */
  101. u8 in_min[7]; /* Register value */
  102. u8 fan[2]; /* Register value */
  103. u8 fan_min[2]; /* Register value */
  104. u8 fan_div[2]; /* Register encoding, shifted right */
  105. u16 temp; /* Register values, shifted right */
  106. u8 temp_hot_max; /* Register value */
  107. u8 temp_hot_hyst; /* Register value */
  108. u8 temp_os_max; /* Register value */
  109. u8 temp_os_hyst; /* Register value */
  110. u16 alarms; /* Register encoding, combined */
  111. };
  112. /*
  113. * Functions declaration
  114. */
  115. static int lm80_probe(struct i2c_client *client,
  116. const struct i2c_device_id *id);
  117. static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info);
  118. static void lm80_init_client(struct i2c_client *client);
  119. static int lm80_remove(struct i2c_client *client);
  120. static struct lm80_data *lm80_update_device(struct device *dev);
  121. static int lm80_read_value(struct i2c_client *client, u8 reg);
  122. static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
  123. /*
  124. * Driver data (common to all clients)
  125. */
  126. static const struct i2c_device_id lm80_id[] = {
  127. { "lm80", 0 },
  128. { "lm96080", 1 },
  129. { }
  130. };
  131. MODULE_DEVICE_TABLE(i2c, lm80_id);
  132. static struct i2c_driver lm80_driver = {
  133. .class = I2C_CLASS_HWMON,
  134. .driver = {
  135. .name = "lm80",
  136. },
  137. .probe = lm80_probe,
  138. .remove = lm80_remove,
  139. .id_table = lm80_id,
  140. .detect = lm80_detect,
  141. .address_list = normal_i2c,
  142. };
  143. /*
  144. * Sysfs stuff
  145. */
  146. #define show_in(suffix, value) \
  147. static ssize_t show_in_##suffix(struct device *dev, \
  148. struct device_attribute *attr, char *buf) \
  149. { \
  150. int nr = to_sensor_dev_attr(attr)->index; \
  151. struct lm80_data *data = lm80_update_device(dev); \
  152. if (IS_ERR(data)) \
  153. return PTR_ERR(data); \
  154. return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
  155. }
  156. show_in(min, in_min)
  157. show_in(max, in_max)
  158. show_in(input, in)
  159. #define set_in(suffix, value, reg) \
  160. static ssize_t set_in_##suffix(struct device *dev, \
  161. struct device_attribute *attr, const char *buf, size_t count) \
  162. { \
  163. int nr = to_sensor_dev_attr(attr)->index; \
  164. struct i2c_client *client = to_i2c_client(dev); \
  165. struct lm80_data *data = i2c_get_clientdata(client); \
  166. long val; \
  167. int err = kstrtol(buf, 10, &val); \
  168. if (err < 0) \
  169. return err; \
  170. \
  171. mutex_lock(&data->update_lock);\
  172. data->value[nr] = IN_TO_REG(val); \
  173. lm80_write_value(client, reg(nr), data->value[nr]); \
  174. mutex_unlock(&data->update_lock);\
  175. return count; \
  176. }
  177. set_in(min, in_min, LM80_REG_IN_MIN)
  178. set_in(max, in_max, LM80_REG_IN_MAX)
  179. #define show_fan(suffix, value) \
  180. static ssize_t show_fan_##suffix(struct device *dev, \
  181. struct device_attribute *attr, char *buf) \
  182. { \
  183. int nr = to_sensor_dev_attr(attr)->index; \
  184. struct lm80_data *data = lm80_update_device(dev); \
  185. if (IS_ERR(data)) \
  186. return PTR_ERR(data); \
  187. return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
  188. DIV_FROM_REG(data->fan_div[nr]))); \
  189. }
  190. show_fan(min, fan_min)
  191. show_fan(input, fan)
  192. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  193. char *buf)
  194. {
  195. int nr = to_sensor_dev_attr(attr)->index;
  196. struct lm80_data *data = lm80_update_device(dev);
  197. if (IS_ERR(data))
  198. return PTR_ERR(data);
  199. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  200. }
  201. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  202. const char *buf, size_t count)
  203. {
  204. int nr = to_sensor_dev_attr(attr)->index;
  205. struct i2c_client *client = to_i2c_client(dev);
  206. struct lm80_data *data = i2c_get_clientdata(client);
  207. unsigned long val;
  208. int err = kstrtoul(buf, 10, &val);
  209. if (err < 0)
  210. return err;
  211. mutex_lock(&data->update_lock);
  212. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  213. lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
  214. mutex_unlock(&data->update_lock);
  215. return count;
  216. }
  217. /*
  218. * Note: we save and restore the fan minimum here, because its value is
  219. * determined in part by the fan divisor. This follows the principle of
  220. * least surprise; the user doesn't expect the fan minimum to change just
  221. * because the divisor changed.
  222. */
  223. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  224. const char *buf, size_t count)
  225. {
  226. int nr = to_sensor_dev_attr(attr)->index;
  227. struct i2c_client *client = to_i2c_client(dev);
  228. struct lm80_data *data = i2c_get_clientdata(client);
  229. unsigned long min, val;
  230. u8 reg;
  231. int err = kstrtoul(buf, 10, &val);
  232. if (err < 0)
  233. return err;
  234. /* Save fan_min */
  235. mutex_lock(&data->update_lock);
  236. min = FAN_FROM_REG(data->fan_min[nr],
  237. DIV_FROM_REG(data->fan_div[nr]));
  238. switch (val) {
  239. case 1:
  240. data->fan_div[nr] = 0;
  241. break;
  242. case 2:
  243. data->fan_div[nr] = 1;
  244. break;
  245. case 4:
  246. data->fan_div[nr] = 2;
  247. break;
  248. case 8:
  249. data->fan_div[nr] = 3;
  250. break;
  251. default:
  252. dev_err(&client->dev, "fan_div value %ld not "
  253. "supported. Choose one of 1, 2, 4 or 8!\n", val);
  254. mutex_unlock(&data->update_lock);
  255. return -EINVAL;
  256. }
  257. reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
  258. | (data->fan_div[nr] << (2 * (nr + 1)));
  259. lm80_write_value(client, LM80_REG_FANDIV, reg);
  260. /* Restore fan_min */
  261. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  262. lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
  263. mutex_unlock(&data->update_lock);
  264. return count;
  265. }
  266. static ssize_t show_temp_input1(struct device *dev,
  267. struct device_attribute *attr, char *buf)
  268. {
  269. struct lm80_data *data = lm80_update_device(dev);
  270. if (IS_ERR(data))
  271. return PTR_ERR(data);
  272. return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
  273. }
  274. #define show_temp(suffix, value) \
  275. static ssize_t show_temp_##suffix(struct device *dev, \
  276. struct device_attribute *attr, char *buf) \
  277. { \
  278. struct lm80_data *data = lm80_update_device(dev); \
  279. if (IS_ERR(data)) \
  280. return PTR_ERR(data); \
  281. return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
  282. }
  283. show_temp(hot_max, temp_hot_max);
  284. show_temp(hot_hyst, temp_hot_hyst);
  285. show_temp(os_max, temp_os_max);
  286. show_temp(os_hyst, temp_os_hyst);
  287. #define set_temp(suffix, value, reg) \
  288. static ssize_t set_temp_##suffix(struct device *dev, \
  289. struct device_attribute *attr, const char *buf, size_t count) \
  290. { \
  291. struct i2c_client *client = to_i2c_client(dev); \
  292. struct lm80_data *data = i2c_get_clientdata(client); \
  293. long val; \
  294. int err = kstrtol(buf, 10, &val); \
  295. if (err < 0) \
  296. return err; \
  297. \
  298. mutex_lock(&data->update_lock); \
  299. data->value = TEMP_LIMIT_TO_REG(val); \
  300. lm80_write_value(client, reg, data->value); \
  301. mutex_unlock(&data->update_lock); \
  302. return count; \
  303. }
  304. set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
  305. set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
  306. set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
  307. set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
  308. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  309. char *buf)
  310. {
  311. struct lm80_data *data = lm80_update_device(dev);
  312. if (IS_ERR(data))
  313. return PTR_ERR(data);
  314. return sprintf(buf, "%u\n", data->alarms);
  315. }
  316. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  317. char *buf)
  318. {
  319. int bitnr = to_sensor_dev_attr(attr)->index;
  320. struct lm80_data *data = lm80_update_device(dev);
  321. if (IS_ERR(data))
  322. return PTR_ERR(data);
  323. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  324. }
  325. static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
  326. show_in_min, set_in_min, 0);
  327. static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
  328. show_in_min, set_in_min, 1);
  329. static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
  330. show_in_min, set_in_min, 2);
  331. static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
  332. show_in_min, set_in_min, 3);
  333. static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
  334. show_in_min, set_in_min, 4);
  335. static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
  336. show_in_min, set_in_min, 5);
  337. static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
  338. show_in_min, set_in_min, 6);
  339. static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
  340. show_in_max, set_in_max, 0);
  341. static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
  342. show_in_max, set_in_max, 1);
  343. static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
  344. show_in_max, set_in_max, 2);
  345. static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
  346. show_in_max, set_in_max, 3);
  347. static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
  348. show_in_max, set_in_max, 4);
  349. static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
  350. show_in_max, set_in_max, 5);
  351. static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
  352. show_in_max, set_in_max, 6);
  353. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
  354. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
  355. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
  356. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
  357. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
  358. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
  359. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
  360. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
  361. show_fan_min, set_fan_min, 0);
  362. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
  363. show_fan_min, set_fan_min, 1);
  364. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
  365. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
  366. static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
  367. show_fan_div, set_fan_div, 0);
  368. static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
  369. show_fan_div, set_fan_div, 1);
  370. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  371. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
  372. set_temp_hot_max);
  373. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
  374. set_temp_hot_hyst);
  375. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
  376. set_temp_os_max);
  377. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
  378. set_temp_os_hyst);
  379. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  380. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  381. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  382. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  383. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  384. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
  385. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
  386. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  387. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
  388. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
  389. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 8);
  390. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 13);
  391. /*
  392. * Real code
  393. */
  394. static struct attribute *lm80_attributes[] = {
  395. &sensor_dev_attr_in0_min.dev_attr.attr,
  396. &sensor_dev_attr_in1_min.dev_attr.attr,
  397. &sensor_dev_attr_in2_min.dev_attr.attr,
  398. &sensor_dev_attr_in3_min.dev_attr.attr,
  399. &sensor_dev_attr_in4_min.dev_attr.attr,
  400. &sensor_dev_attr_in5_min.dev_attr.attr,
  401. &sensor_dev_attr_in6_min.dev_attr.attr,
  402. &sensor_dev_attr_in0_max.dev_attr.attr,
  403. &sensor_dev_attr_in1_max.dev_attr.attr,
  404. &sensor_dev_attr_in2_max.dev_attr.attr,
  405. &sensor_dev_attr_in3_max.dev_attr.attr,
  406. &sensor_dev_attr_in4_max.dev_attr.attr,
  407. &sensor_dev_attr_in5_max.dev_attr.attr,
  408. &sensor_dev_attr_in6_max.dev_attr.attr,
  409. &sensor_dev_attr_in0_input.dev_attr.attr,
  410. &sensor_dev_attr_in1_input.dev_attr.attr,
  411. &sensor_dev_attr_in2_input.dev_attr.attr,
  412. &sensor_dev_attr_in3_input.dev_attr.attr,
  413. &sensor_dev_attr_in4_input.dev_attr.attr,
  414. &sensor_dev_attr_in5_input.dev_attr.attr,
  415. &sensor_dev_attr_in6_input.dev_attr.attr,
  416. &sensor_dev_attr_fan1_min.dev_attr.attr,
  417. &sensor_dev_attr_fan2_min.dev_attr.attr,
  418. &sensor_dev_attr_fan1_input.dev_attr.attr,
  419. &sensor_dev_attr_fan2_input.dev_attr.attr,
  420. &sensor_dev_attr_fan1_div.dev_attr.attr,
  421. &sensor_dev_attr_fan2_div.dev_attr.attr,
  422. &dev_attr_temp1_input.attr,
  423. &dev_attr_temp1_max.attr,
  424. &dev_attr_temp1_max_hyst.attr,
  425. &dev_attr_temp1_crit.attr,
  426. &dev_attr_temp1_crit_hyst.attr,
  427. &dev_attr_alarms.attr,
  428. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  429. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  430. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  431. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  432. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  433. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  434. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  435. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  436. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  437. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  438. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  439. NULL
  440. };
  441. static const struct attribute_group lm80_group = {
  442. .attrs = lm80_attributes,
  443. };
  444. /* Return 0 if detection is successful, -ENODEV otherwise */
  445. static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
  446. {
  447. struct i2c_adapter *adapter = client->adapter;
  448. int i, cur, man_id, dev_id;
  449. const char *name = NULL;
  450. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  451. return -ENODEV;
  452. /* First check for unused bits, common to both chip types */
  453. if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
  454. || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
  455. return -ENODEV;
  456. /*
  457. * The LM96080 has manufacturer and stepping/die rev registers so we
  458. * can just check that. The LM80 does not have such registers so we
  459. * have to use a more expensive trick.
  460. */
  461. man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
  462. dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
  463. if (man_id == 0x01 && dev_id == 0x08) {
  464. /* Check more unused bits for confirmation */
  465. if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
  466. return -ENODEV;
  467. name = "lm96080";
  468. } else {
  469. /* Check 6-bit addressing */
  470. for (i = 0x2a; i <= 0x3d; i++) {
  471. cur = i2c_smbus_read_byte_data(client, i);
  472. if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
  473. || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
  474. || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
  475. return -ENODEV;
  476. }
  477. name = "lm80";
  478. }
  479. strlcpy(info->type, name, I2C_NAME_SIZE);
  480. return 0;
  481. }
  482. static int lm80_probe(struct i2c_client *client,
  483. const struct i2c_device_id *id)
  484. {
  485. struct lm80_data *data;
  486. int err;
  487. data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL);
  488. if (!data) {
  489. err = -ENOMEM;
  490. goto exit;
  491. }
  492. i2c_set_clientdata(client, data);
  493. mutex_init(&data->update_lock);
  494. /* Initialize the LM80 chip */
  495. lm80_init_client(client);
  496. /* A few vars need to be filled upon startup */
  497. data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
  498. data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
  499. /* Register sysfs hooks */
  500. err = sysfs_create_group(&client->dev.kobj, &lm80_group);
  501. if (err)
  502. goto error_free;
  503. data->hwmon_dev = hwmon_device_register(&client->dev);
  504. if (IS_ERR(data->hwmon_dev)) {
  505. err = PTR_ERR(data->hwmon_dev);
  506. goto error_remove;
  507. }
  508. return 0;
  509. error_remove:
  510. sysfs_remove_group(&client->dev.kobj, &lm80_group);
  511. error_free:
  512. kfree(data);
  513. exit:
  514. return err;
  515. }
  516. static int lm80_remove(struct i2c_client *client)
  517. {
  518. struct lm80_data *data = i2c_get_clientdata(client);
  519. hwmon_device_unregister(data->hwmon_dev);
  520. sysfs_remove_group(&client->dev.kobj, &lm80_group);
  521. kfree(data);
  522. return 0;
  523. }
  524. static int lm80_read_value(struct i2c_client *client, u8 reg)
  525. {
  526. return i2c_smbus_read_byte_data(client, reg);
  527. }
  528. static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
  529. {
  530. return i2c_smbus_write_byte_data(client, reg, value);
  531. }
  532. /* Called when we have found a new LM80. */
  533. static void lm80_init_client(struct i2c_client *client)
  534. {
  535. /*
  536. * Reset all except Watchdog values and last conversion values
  537. * This sets fan-divs to 2, among others. This makes most other
  538. * initializations unnecessary
  539. */
  540. lm80_write_value(client, LM80_REG_CONFIG, 0x80);
  541. /* Set 11-bit temperature resolution */
  542. lm80_write_value(client, LM80_REG_RES, 0x08);
  543. /* Start monitoring */
  544. lm80_write_value(client, LM80_REG_CONFIG, 0x01);
  545. }
  546. static struct lm80_data *lm80_update_device(struct device *dev)
  547. {
  548. struct i2c_client *client = to_i2c_client(dev);
  549. struct lm80_data *data = i2c_get_clientdata(client);
  550. int i;
  551. int rv;
  552. int prev_rv;
  553. struct lm80_data *ret = data;
  554. mutex_lock(&data->update_lock);
  555. if (data->error)
  556. lm80_init_client(client);
  557. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  558. dev_dbg(&client->dev, "Starting lm80 update\n");
  559. for (i = 0; i <= 6; i++) {
  560. rv = lm80_read_value(client, LM80_REG_IN(i));
  561. if (rv < 0)
  562. goto abort;
  563. data->in[i] = rv;
  564. rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
  565. if (rv < 0)
  566. goto abort;
  567. data->in_min[i] = rv;
  568. rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
  569. if (rv < 0)
  570. goto abort;
  571. data->in_max[i] = rv;
  572. }
  573. rv = lm80_read_value(client, LM80_REG_FAN1);
  574. if (rv < 0)
  575. goto abort;
  576. data->fan[0] = rv;
  577. rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
  578. if (rv < 0)
  579. goto abort;
  580. data->fan_min[0] = rv;
  581. rv = lm80_read_value(client, LM80_REG_FAN2);
  582. if (rv < 0)
  583. goto abort;
  584. data->fan[1] = rv;
  585. rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
  586. if (rv < 0)
  587. goto abort;
  588. data->fan_min[1] = rv;
  589. prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
  590. if (rv < 0)
  591. goto abort;
  592. rv = lm80_read_value(client, LM80_REG_RES);
  593. if (rv < 0)
  594. goto abort;
  595. data->temp = (prev_rv << 8) | (rv & 0xf0);
  596. rv = lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
  597. if (rv < 0)
  598. goto abort;
  599. data->temp_os_max = rv;
  600. rv = lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
  601. if (rv < 0)
  602. goto abort;
  603. data->temp_os_hyst = rv;
  604. rv = lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
  605. if (rv < 0)
  606. goto abort;
  607. data->temp_hot_max = rv;
  608. rv = lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
  609. if (rv < 0)
  610. goto abort;
  611. data->temp_hot_hyst = rv;
  612. rv = lm80_read_value(client, LM80_REG_FANDIV);
  613. if (rv < 0)
  614. goto abort;
  615. data->fan_div[0] = (rv >> 2) & 0x03;
  616. data->fan_div[1] = (rv >> 4) & 0x03;
  617. prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
  618. if (rv < 0)
  619. goto abort;
  620. rv = lm80_read_value(client, LM80_REG_ALARM2);
  621. if (rv < 0)
  622. goto abort;
  623. data->alarms = prev_rv + (rv << 8);
  624. data->last_updated = jiffies;
  625. data->valid = 1;
  626. data->error = 0;
  627. }
  628. goto done;
  629. abort:
  630. ret = ERR_PTR(rv);
  631. data->valid = 0;
  632. data->error = 1;
  633. done:
  634. mutex_unlock(&data->update_lock);
  635. return ret;
  636. }
  637. module_i2c_driver(lm80_driver);
  638. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  639. "Philip Edelbrock <phil@netroedge.com>");
  640. MODULE_DESCRIPTION("LM80 driver");
  641. MODULE_LICENSE("GPL");