lm80.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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/i2c-sensor.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/err.h>
  31. /* Addresses to scan */
  32. static unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
  33. 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
  34. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  35. /* Insmod parameters */
  36. SENSORS_INSMOD_1(lm80);
  37. /* Many LM80 constants specified below */
  38. /* The LM80 registers */
  39. #define LM80_REG_IN_MAX(nr) (0x2a + (nr) * 2)
  40. #define LM80_REG_IN_MIN(nr) (0x2b + (nr) * 2)
  41. #define LM80_REG_IN(nr) (0x20 + (nr))
  42. #define LM80_REG_FAN1 0x28
  43. #define LM80_REG_FAN2 0x29
  44. #define LM80_REG_FAN_MIN(nr) (0x3b + (nr))
  45. #define LM80_REG_TEMP 0x27
  46. #define LM80_REG_TEMP_HOT_MAX 0x38
  47. #define LM80_REG_TEMP_HOT_HYST 0x39
  48. #define LM80_REG_TEMP_OS_MAX 0x3a
  49. #define LM80_REG_TEMP_OS_HYST 0x3b
  50. #define LM80_REG_CONFIG 0x00
  51. #define LM80_REG_ALARM1 0x01
  52. #define LM80_REG_ALARM2 0x02
  53. #define LM80_REG_MASK1 0x03
  54. #define LM80_REG_MASK2 0x04
  55. #define LM80_REG_FANDIV 0x05
  56. #define LM80_REG_RES 0x06
  57. /* Conversions. Rounding and limit checking is only done on the TO_REG
  58. variants. Note that you should be a bit careful with which arguments
  59. these macros are called: arguments may be evaluated more than once.
  60. Fixing this is just not worth it. */
  61. #define IN_TO_REG(val) (SENSORS_LIMIT(((val)+5)/10,0,255))
  62. #define IN_FROM_REG(val) ((val)*10)
  63. static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
  64. {
  65. if (rpm == 0)
  66. return 255;
  67. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  68. return SENSORS_LIMIT((1350000 + rpm*div / 2) / (rpm*div), 1, 254);
  69. }
  70. #define FAN_FROM_REG(val,div) ((val)==0?-1:\
  71. (val)==255?0:1350000/((div)*(val)))
  72. static inline long TEMP_FROM_REG(u16 temp)
  73. {
  74. long res;
  75. temp >>= 4;
  76. if (temp < 0x0800)
  77. res = 625 * (long) temp;
  78. else
  79. res = ((long) temp - 0x01000) * 625;
  80. return res / 10;
  81. }
  82. #define TEMP_LIMIT_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
  83. #define TEMP_LIMIT_TO_REG(val) SENSORS_LIMIT((val)<0?\
  84. ((val)-500)/1000:((val)+500)/1000,0,255)
  85. #define DIV_FROM_REG(val) (1 << (val))
  86. /*
  87. * Client data (each client gets its own)
  88. */
  89. struct lm80_data {
  90. struct i2c_client client;
  91. struct class_device *class_dev;
  92. struct semaphore update_lock;
  93. char valid; /* !=0 if following fields are valid */
  94. unsigned long last_updated; /* In jiffies */
  95. u8 in[7]; /* Register value */
  96. u8 in_max[7]; /* Register value */
  97. u8 in_min[7]; /* Register value */
  98. u8 fan[2]; /* Register value */
  99. u8 fan_min[2]; /* Register value */
  100. u8 fan_div[2]; /* Register encoding, shifted right */
  101. u16 temp; /* Register values, shifted right */
  102. u8 temp_hot_max; /* Register value */
  103. u8 temp_hot_hyst; /* Register value */
  104. u8 temp_os_max; /* Register value */
  105. u8 temp_os_hyst; /* Register value */
  106. u16 alarms; /* Register encoding, combined */
  107. };
  108. /*
  109. * Functions declaration
  110. */
  111. static int lm80_attach_adapter(struct i2c_adapter *adapter);
  112. static int lm80_detect(struct i2c_adapter *adapter, int address, int kind);
  113. static void lm80_init_client(struct i2c_client *client);
  114. static int lm80_detach_client(struct i2c_client *client);
  115. static struct lm80_data *lm80_update_device(struct device *dev);
  116. static int lm80_read_value(struct i2c_client *client, u8 reg);
  117. static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value);
  118. /*
  119. * Driver data (common to all clients)
  120. */
  121. static struct i2c_driver lm80_driver = {
  122. .owner = THIS_MODULE,
  123. .name = "lm80",
  124. .id = I2C_DRIVERID_LM80,
  125. .flags = I2C_DF_NOTIFY,
  126. .attach_adapter = lm80_attach_adapter,
  127. .detach_client = lm80_detach_client,
  128. };
  129. /*
  130. * Sysfs stuff
  131. */
  132. #define show_in(suffix, value) \
  133. static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  134. { \
  135. struct lm80_data *data = lm80_update_device(dev); \
  136. return sprintf(buf, "%d\n", IN_FROM_REG(data->value)); \
  137. }
  138. show_in(min0, in_min[0]);
  139. show_in(min1, in_min[1]);
  140. show_in(min2, in_min[2]);
  141. show_in(min3, in_min[3]);
  142. show_in(min4, in_min[4]);
  143. show_in(min5, in_min[5]);
  144. show_in(min6, in_min[6]);
  145. show_in(max0, in_max[0]);
  146. show_in(max1, in_max[1]);
  147. show_in(max2, in_max[2]);
  148. show_in(max3, in_max[3]);
  149. show_in(max4, in_max[4]);
  150. show_in(max5, in_max[5]);
  151. show_in(max6, in_max[6]);
  152. show_in(input0, in[0]);
  153. show_in(input1, in[1]);
  154. show_in(input2, in[2]);
  155. show_in(input3, in[3]);
  156. show_in(input4, in[4]);
  157. show_in(input5, in[5]);
  158. show_in(input6, in[6]);
  159. #define set_in(suffix, value, reg) \
  160. static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  161. size_t count) \
  162. { \
  163. struct i2c_client *client = to_i2c_client(dev); \
  164. struct lm80_data *data = i2c_get_clientdata(client); \
  165. long val = simple_strtol(buf, NULL, 10); \
  166. \
  167. down(&data->update_lock);\
  168. data->value = IN_TO_REG(val); \
  169. lm80_write_value(client, reg, data->value); \
  170. up(&data->update_lock);\
  171. return count; \
  172. }
  173. set_in(min0, in_min[0], LM80_REG_IN_MIN(0));
  174. set_in(min1, in_min[1], LM80_REG_IN_MIN(1));
  175. set_in(min2, in_min[2], LM80_REG_IN_MIN(2));
  176. set_in(min3, in_min[3], LM80_REG_IN_MIN(3));
  177. set_in(min4, in_min[4], LM80_REG_IN_MIN(4));
  178. set_in(min5, in_min[5], LM80_REG_IN_MIN(5));
  179. set_in(min6, in_min[6], LM80_REG_IN_MIN(6));
  180. set_in(max0, in_max[0], LM80_REG_IN_MAX(0));
  181. set_in(max1, in_max[1], LM80_REG_IN_MAX(1));
  182. set_in(max2, in_max[2], LM80_REG_IN_MAX(2));
  183. set_in(max3, in_max[3], LM80_REG_IN_MAX(3));
  184. set_in(max4, in_max[4], LM80_REG_IN_MAX(4));
  185. set_in(max5, in_max[5], LM80_REG_IN_MAX(5));
  186. set_in(max6, in_max[6], LM80_REG_IN_MAX(6));
  187. #define show_fan(suffix, value, div) \
  188. static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  189. { \
  190. struct lm80_data *data = lm80_update_device(dev); \
  191. return sprintf(buf, "%d\n", FAN_FROM_REG(data->value, \
  192. DIV_FROM_REG(data->div))); \
  193. }
  194. show_fan(min1, fan_min[0], fan_div[0]);
  195. show_fan(min2, fan_min[1], fan_div[1]);
  196. show_fan(input1, fan[0], fan_div[0]);
  197. show_fan(input2, fan[1], fan_div[1]);
  198. #define show_fan_div(suffix, value) \
  199. static ssize_t show_fan_div##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  200. { \
  201. struct lm80_data *data = lm80_update_device(dev); \
  202. return sprintf(buf, "%d\n", DIV_FROM_REG(data->value)); \
  203. }
  204. show_fan_div(1, fan_div[0]);
  205. show_fan_div(2, fan_div[1]);
  206. #define set_fan(suffix, value, reg, div) \
  207. static ssize_t set_fan_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  208. size_t count) \
  209. { \
  210. struct i2c_client *client = to_i2c_client(dev); \
  211. struct lm80_data *data = i2c_get_clientdata(client); \
  212. long val = simple_strtoul(buf, NULL, 10); \
  213. \
  214. down(&data->update_lock);\
  215. data->value = FAN_TO_REG(val, DIV_FROM_REG(data->div)); \
  216. lm80_write_value(client, reg, data->value); \
  217. up(&data->update_lock);\
  218. return count; \
  219. }
  220. set_fan(min1, fan_min[0], LM80_REG_FAN_MIN(1), fan_div[0]);
  221. set_fan(min2, fan_min[1], LM80_REG_FAN_MIN(2), fan_div[1]);
  222. /* Note: we save and restore the fan minimum here, because its value is
  223. determined in part by the fan divisor. This follows the principle of
  224. least suprise; the user doesn't expect the fan minimum to change just
  225. because the divisor changed. */
  226. static ssize_t set_fan_div(struct device *dev, const char *buf,
  227. size_t count, int nr)
  228. {
  229. struct i2c_client *client = to_i2c_client(dev);
  230. struct lm80_data *data = i2c_get_clientdata(client);
  231. unsigned long min, val = simple_strtoul(buf, NULL, 10);
  232. u8 reg;
  233. /* Save fan_min */
  234. down(&data->update_lock);
  235. min = FAN_FROM_REG(data->fan_min[nr],
  236. DIV_FROM_REG(data->fan_div[nr]));
  237. switch (val) {
  238. case 1: data->fan_div[nr] = 0; break;
  239. case 2: data->fan_div[nr] = 1; break;
  240. case 4: data->fan_div[nr] = 2; break;
  241. case 8: data->fan_div[nr] = 3; break;
  242. default:
  243. dev_err(&client->dev, "fan_div value %ld not "
  244. "supported. Choose one of 1, 2, 4 or 8!\n", val);
  245. up(&data->update_lock);
  246. return -EINVAL;
  247. }
  248. reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
  249. | (data->fan_div[nr] << (2 * (nr + 1)));
  250. lm80_write_value(client, LM80_REG_FANDIV, reg);
  251. /* Restore fan_min */
  252. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  253. lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
  254. up(&data->update_lock);
  255. return count;
  256. }
  257. #define set_fan_div(number) \
  258. static ssize_t set_fan_div##number(struct device *dev, struct device_attribute *attr, const char *buf, \
  259. size_t count) \
  260. { \
  261. return set_fan_div(dev, buf, count, number - 1); \
  262. }
  263. set_fan_div(1);
  264. set_fan_div(2);
  265. static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf)
  266. {
  267. struct lm80_data *data = lm80_update_device(dev);
  268. return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
  269. }
  270. #define show_temp(suffix, value) \
  271. static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  272. { \
  273. struct lm80_data *data = lm80_update_device(dev); \
  274. return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
  275. }
  276. show_temp(hot_max, temp_hot_max);
  277. show_temp(hot_hyst, temp_hot_hyst);
  278. show_temp(os_max, temp_os_max);
  279. show_temp(os_hyst, temp_os_hyst);
  280. #define set_temp(suffix, value, reg) \
  281. static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  282. size_t count) \
  283. { \
  284. struct i2c_client *client = to_i2c_client(dev); \
  285. struct lm80_data *data = i2c_get_clientdata(client); \
  286. long val = simple_strtoul(buf, NULL, 10); \
  287. \
  288. down(&data->update_lock); \
  289. data->value = TEMP_LIMIT_TO_REG(val); \
  290. lm80_write_value(client, reg, data->value); \
  291. up(&data->update_lock); \
  292. return count; \
  293. }
  294. set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
  295. set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
  296. set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
  297. set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
  298. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  299. {
  300. struct lm80_data *data = lm80_update_device(dev);
  301. return sprintf(buf, "%u\n", data->alarms);
  302. }
  303. static DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min0, set_in_min0);
  304. static DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min1, set_in_min1);
  305. static DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min2, set_in_min2);
  306. static DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min3, set_in_min3);
  307. static DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min4, set_in_min4);
  308. static DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min5, set_in_min5);
  309. static DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min6, set_in_min6);
  310. static DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max0, set_in_max0);
  311. static DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max1, set_in_max1);
  312. static DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max2, set_in_max2);
  313. static DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max3, set_in_max3);
  314. static DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max4, set_in_max4);
  315. static DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max5, set_in_max5);
  316. static DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max6, set_in_max6);
  317. static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input0, NULL);
  318. static DEVICE_ATTR(in1_input, S_IRUGO, show_in_input1, NULL);
  319. static DEVICE_ATTR(in2_input, S_IRUGO, show_in_input2, NULL);
  320. static DEVICE_ATTR(in3_input, S_IRUGO, show_in_input3, NULL);
  321. static DEVICE_ATTR(in4_input, S_IRUGO, show_in_input4, NULL);
  322. static DEVICE_ATTR(in5_input, S_IRUGO, show_in_input5, NULL);
  323. static DEVICE_ATTR(in6_input, S_IRUGO, show_in_input6, NULL);
  324. static DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min1,
  325. set_fan_min1);
  326. static DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min2,
  327. set_fan_min2);
  328. static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input1, NULL);
  329. static DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input2, NULL);
  330. static DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO, show_fan_div1, set_fan_div1);
  331. static DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO, show_fan_div2, set_fan_div2);
  332. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  333. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
  334. set_temp_hot_max);
  335. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
  336. set_temp_hot_hyst);
  337. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
  338. set_temp_os_max);
  339. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
  340. set_temp_os_hyst);
  341. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  342. /*
  343. * Real code
  344. */
  345. static int lm80_attach_adapter(struct i2c_adapter *adapter)
  346. {
  347. if (!(adapter->class & I2C_CLASS_HWMON))
  348. return 0;
  349. return i2c_detect(adapter, &addr_data, lm80_detect);
  350. }
  351. int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
  352. {
  353. int i, cur;
  354. struct i2c_client *new_client;
  355. struct lm80_data *data;
  356. int err = 0;
  357. const char *name;
  358. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  359. goto exit;
  360. /* OK. For now, we presume we have a valid client. We now create the
  361. client structure, even though we cannot fill it completely yet.
  362. But it allows us to access lm80_{read,write}_value. */
  363. if (!(data = kmalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
  364. err = -ENOMEM;
  365. goto exit;
  366. }
  367. memset(data, 0, sizeof(struct lm80_data));
  368. new_client = &data->client;
  369. i2c_set_clientdata(new_client, data);
  370. new_client->addr = address;
  371. new_client->adapter = adapter;
  372. new_client->driver = &lm80_driver;
  373. new_client->flags = 0;
  374. /* Now, we do the remaining detection. It is lousy. */
  375. if (lm80_read_value(new_client, LM80_REG_ALARM2) & 0xc0)
  376. goto error_free;
  377. for (i = 0x2a; i <= 0x3d; i++) {
  378. cur = i2c_smbus_read_byte_data(new_client, i);
  379. if ((i2c_smbus_read_byte_data(new_client, i + 0x40) != cur)
  380. || (i2c_smbus_read_byte_data(new_client, i + 0x80) != cur)
  381. || (i2c_smbus_read_byte_data(new_client, i + 0xc0) != cur))
  382. goto error_free;
  383. }
  384. /* Determine the chip type - only one kind supported! */
  385. kind = lm80;
  386. name = "lm80";
  387. /* Fill in the remaining client fields and put it into the global list */
  388. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  389. data->valid = 0;
  390. init_MUTEX(&data->update_lock);
  391. /* Tell the I2C layer a new client has arrived */
  392. if ((err = i2c_attach_client(new_client)))
  393. goto error_free;
  394. /* Initialize the LM80 chip */
  395. lm80_init_client(new_client);
  396. /* A few vars need to be filled upon startup */
  397. data->fan_min[0] = lm80_read_value(new_client, LM80_REG_FAN_MIN(1));
  398. data->fan_min[1] = lm80_read_value(new_client, LM80_REG_FAN_MIN(2));
  399. /* Register sysfs hooks */
  400. data->class_dev = hwmon_device_register(&new_client->dev);
  401. if (IS_ERR(data->class_dev)) {
  402. err = PTR_ERR(data->class_dev);
  403. goto error_detach;
  404. }
  405. device_create_file(&new_client->dev, &dev_attr_in0_min);
  406. device_create_file(&new_client->dev, &dev_attr_in1_min);
  407. device_create_file(&new_client->dev, &dev_attr_in2_min);
  408. device_create_file(&new_client->dev, &dev_attr_in3_min);
  409. device_create_file(&new_client->dev, &dev_attr_in4_min);
  410. device_create_file(&new_client->dev, &dev_attr_in5_min);
  411. device_create_file(&new_client->dev, &dev_attr_in6_min);
  412. device_create_file(&new_client->dev, &dev_attr_in0_max);
  413. device_create_file(&new_client->dev, &dev_attr_in1_max);
  414. device_create_file(&new_client->dev, &dev_attr_in2_max);
  415. device_create_file(&new_client->dev, &dev_attr_in3_max);
  416. device_create_file(&new_client->dev, &dev_attr_in4_max);
  417. device_create_file(&new_client->dev, &dev_attr_in5_max);
  418. device_create_file(&new_client->dev, &dev_attr_in6_max);
  419. device_create_file(&new_client->dev, &dev_attr_in0_input);
  420. device_create_file(&new_client->dev, &dev_attr_in1_input);
  421. device_create_file(&new_client->dev, &dev_attr_in2_input);
  422. device_create_file(&new_client->dev, &dev_attr_in3_input);
  423. device_create_file(&new_client->dev, &dev_attr_in4_input);
  424. device_create_file(&new_client->dev, &dev_attr_in5_input);
  425. device_create_file(&new_client->dev, &dev_attr_in6_input);
  426. device_create_file(&new_client->dev, &dev_attr_fan1_min);
  427. device_create_file(&new_client->dev, &dev_attr_fan2_min);
  428. device_create_file(&new_client->dev, &dev_attr_fan1_input);
  429. device_create_file(&new_client->dev, &dev_attr_fan2_input);
  430. device_create_file(&new_client->dev, &dev_attr_fan1_div);
  431. device_create_file(&new_client->dev, &dev_attr_fan2_div);
  432. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  433. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  434. device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
  435. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  436. device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
  437. device_create_file(&new_client->dev, &dev_attr_alarms);
  438. return 0;
  439. error_detach:
  440. i2c_detach_client(new_client);
  441. error_free:
  442. kfree(data);
  443. exit:
  444. return err;
  445. }
  446. static int lm80_detach_client(struct i2c_client *client)
  447. {
  448. struct lm80_data *data = i2c_get_clientdata(client);
  449. int err;
  450. hwmon_device_unregister(data->class_dev);
  451. if ((err = i2c_detach_client(client))) {
  452. dev_err(&client->dev, "Client deregistration failed, "
  453. "client not detached.\n");
  454. return err;
  455. }
  456. kfree(data);
  457. return 0;
  458. }
  459. static int lm80_read_value(struct i2c_client *client, u8 reg)
  460. {
  461. return i2c_smbus_read_byte_data(client, reg);
  462. }
  463. static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
  464. {
  465. return i2c_smbus_write_byte_data(client, reg, value);
  466. }
  467. /* Called when we have found a new LM80. */
  468. static void lm80_init_client(struct i2c_client *client)
  469. {
  470. /* Reset all except Watchdog values and last conversion values
  471. This sets fan-divs to 2, among others. This makes most other
  472. initializations unnecessary */
  473. lm80_write_value(client, LM80_REG_CONFIG, 0x80);
  474. /* Set 11-bit temperature resolution */
  475. lm80_write_value(client, LM80_REG_RES, 0x08);
  476. /* Start monitoring */
  477. lm80_write_value(client, LM80_REG_CONFIG, 0x01);
  478. }
  479. static struct lm80_data *lm80_update_device(struct device *dev)
  480. {
  481. struct i2c_client *client = to_i2c_client(dev);
  482. struct lm80_data *data = i2c_get_clientdata(client);
  483. int i;
  484. down(&data->update_lock);
  485. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  486. dev_dbg(&client->dev, "Starting lm80 update\n");
  487. for (i = 0; i <= 6; i++) {
  488. data->in[i] =
  489. lm80_read_value(client, LM80_REG_IN(i));
  490. data->in_min[i] =
  491. lm80_read_value(client, LM80_REG_IN_MIN(i));
  492. data->in_max[i] =
  493. lm80_read_value(client, LM80_REG_IN_MAX(i));
  494. }
  495. data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
  496. data->fan_min[0] =
  497. lm80_read_value(client, LM80_REG_FAN_MIN(1));
  498. data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
  499. data->fan_min[1] =
  500. lm80_read_value(client, LM80_REG_FAN_MIN(2));
  501. data->temp =
  502. (lm80_read_value(client, LM80_REG_TEMP) << 8) |
  503. (lm80_read_value(client, LM80_REG_RES) & 0xf0);
  504. data->temp_os_max =
  505. lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
  506. data->temp_os_hyst =
  507. lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
  508. data->temp_hot_max =
  509. lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
  510. data->temp_hot_hyst =
  511. lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
  512. i = lm80_read_value(client, LM80_REG_FANDIV);
  513. data->fan_div[0] = (i >> 2) & 0x03;
  514. data->fan_div[1] = (i >> 4) & 0x03;
  515. data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
  516. (lm80_read_value(client, LM80_REG_ALARM2) << 8);
  517. data->last_updated = jiffies;
  518. data->valid = 1;
  519. }
  520. up(&data->update_lock);
  521. return data;
  522. }
  523. static int __init sensors_lm80_init(void)
  524. {
  525. return i2c_add_driver(&lm80_driver);
  526. }
  527. static void __exit sensors_lm80_exit(void)
  528. {
  529. i2c_del_driver(&lm80_driver);
  530. }
  531. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  532. "Philip Edelbrock <phil@netroedge.com>");
  533. MODULE_DESCRIPTION("LM80 driver");
  534. MODULE_LICENSE("GPL");
  535. module_init(sensors_lm80_init);
  536. module_exit(sensors_lm80_exit);