lm80.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c,
  34. 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
  35. /* Insmod parameters */
  36. I2C_CLIENT_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 device *hwmon_dev;
  92. struct mutex 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. .driver = {
  123. .name = "lm80",
  124. },
  125. .attach_adapter = lm80_attach_adapter,
  126. .detach_client = lm80_detach_client,
  127. };
  128. /*
  129. * Sysfs stuff
  130. */
  131. #define show_in(suffix, value) \
  132. static ssize_t show_in_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  133. { \
  134. int nr = to_sensor_dev_attr(attr)->index; \
  135. struct lm80_data *data = lm80_update_device(dev); \
  136. return sprintf(buf, "%d\n", IN_FROM_REG(data->value[nr])); \
  137. }
  138. show_in(min, in_min)
  139. show_in(max, in_max)
  140. show_in(input, in)
  141. #define set_in(suffix, value, reg) \
  142. static ssize_t set_in_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  143. size_t count) \
  144. { \
  145. int nr = to_sensor_dev_attr(attr)->index; \
  146. struct i2c_client *client = to_i2c_client(dev); \
  147. struct lm80_data *data = i2c_get_clientdata(client); \
  148. long val = simple_strtol(buf, NULL, 10); \
  149. \
  150. mutex_lock(&data->update_lock);\
  151. data->value[nr] = IN_TO_REG(val); \
  152. lm80_write_value(client, reg(nr), data->value[nr]); \
  153. mutex_unlock(&data->update_lock);\
  154. return count; \
  155. }
  156. set_in(min, in_min, LM80_REG_IN_MIN)
  157. set_in(max, in_max, LM80_REG_IN_MAX)
  158. #define show_fan(suffix, value) \
  159. static ssize_t show_fan_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  160. { \
  161. int nr = to_sensor_dev_attr(attr)->index; \
  162. struct lm80_data *data = lm80_update_device(dev); \
  163. return sprintf(buf, "%d\n", FAN_FROM_REG(data->value[nr], \
  164. DIV_FROM_REG(data->fan_div[nr]))); \
  165. }
  166. show_fan(min, fan_min)
  167. show_fan(input, fan)
  168. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  169. char *buf)
  170. {
  171. int nr = to_sensor_dev_attr(attr)->index;
  172. struct lm80_data *data = lm80_update_device(dev);
  173. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  174. }
  175. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  176. const char *buf, size_t count)
  177. {
  178. int nr = to_sensor_dev_attr(attr)->index;
  179. struct i2c_client *client = to_i2c_client(dev);
  180. struct lm80_data *data = i2c_get_clientdata(client);
  181. long val = simple_strtoul(buf, NULL, 10);
  182. mutex_lock(&data->update_lock);
  183. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  184. lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
  185. mutex_unlock(&data->update_lock);
  186. return count;
  187. }
  188. /* Note: we save and restore the fan minimum here, because its value is
  189. determined in part by the fan divisor. This follows the principle of
  190. least surprise; the user doesn't expect the fan minimum to change just
  191. because the divisor changed. */
  192. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  193. const char *buf, size_t count)
  194. {
  195. int nr = to_sensor_dev_attr(attr)->index;
  196. struct i2c_client *client = to_i2c_client(dev);
  197. struct lm80_data *data = i2c_get_clientdata(client);
  198. unsigned long min, val = simple_strtoul(buf, NULL, 10);
  199. u8 reg;
  200. /* Save fan_min */
  201. mutex_lock(&data->update_lock);
  202. min = FAN_FROM_REG(data->fan_min[nr],
  203. DIV_FROM_REG(data->fan_div[nr]));
  204. switch (val) {
  205. case 1: data->fan_div[nr] = 0; break;
  206. case 2: data->fan_div[nr] = 1; break;
  207. case 4: data->fan_div[nr] = 2; break;
  208. case 8: data->fan_div[nr] = 3; break;
  209. default:
  210. dev_err(&client->dev, "fan_div value %ld not "
  211. "supported. Choose one of 1, 2, 4 or 8!\n", val);
  212. mutex_unlock(&data->update_lock);
  213. return -EINVAL;
  214. }
  215. reg = (lm80_read_value(client, LM80_REG_FANDIV) & ~(3 << (2 * (nr + 1))))
  216. | (data->fan_div[nr] << (2 * (nr + 1)));
  217. lm80_write_value(client, LM80_REG_FANDIV, reg);
  218. /* Restore fan_min */
  219. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  220. lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1), data->fan_min[nr]);
  221. mutex_unlock(&data->update_lock);
  222. return count;
  223. }
  224. static ssize_t show_temp_input1(struct device *dev, struct device_attribute *attr, char *buf)
  225. {
  226. struct lm80_data *data = lm80_update_device(dev);
  227. return sprintf(buf, "%ld\n", TEMP_FROM_REG(data->temp));
  228. }
  229. #define show_temp(suffix, value) \
  230. static ssize_t show_temp_##suffix(struct device *dev, struct device_attribute *attr, char *buf) \
  231. { \
  232. struct lm80_data *data = lm80_update_device(dev); \
  233. return sprintf(buf, "%d\n", TEMP_LIMIT_FROM_REG(data->value)); \
  234. }
  235. show_temp(hot_max, temp_hot_max);
  236. show_temp(hot_hyst, temp_hot_hyst);
  237. show_temp(os_max, temp_os_max);
  238. show_temp(os_hyst, temp_os_hyst);
  239. #define set_temp(suffix, value, reg) \
  240. static ssize_t set_temp_##suffix(struct device *dev, struct device_attribute *attr, const char *buf, \
  241. size_t count) \
  242. { \
  243. struct i2c_client *client = to_i2c_client(dev); \
  244. struct lm80_data *data = i2c_get_clientdata(client); \
  245. long val = simple_strtoul(buf, NULL, 10); \
  246. \
  247. mutex_lock(&data->update_lock); \
  248. data->value = TEMP_LIMIT_TO_REG(val); \
  249. lm80_write_value(client, reg, data->value); \
  250. mutex_unlock(&data->update_lock); \
  251. return count; \
  252. }
  253. set_temp(hot_max, temp_hot_max, LM80_REG_TEMP_HOT_MAX);
  254. set_temp(hot_hyst, temp_hot_hyst, LM80_REG_TEMP_HOT_HYST);
  255. set_temp(os_max, temp_os_max, LM80_REG_TEMP_OS_MAX);
  256. set_temp(os_hyst, temp_os_hyst, LM80_REG_TEMP_OS_HYST);
  257. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  258. char *buf)
  259. {
  260. struct lm80_data *data = lm80_update_device(dev);
  261. return sprintf(buf, "%u\n", data->alarms);
  262. }
  263. static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
  264. show_in_min, set_in_min, 0);
  265. static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
  266. show_in_min, set_in_min, 1);
  267. static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
  268. show_in_min, set_in_min, 2);
  269. static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
  270. show_in_min, set_in_min, 3);
  271. static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
  272. show_in_min, set_in_min, 4);
  273. static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
  274. show_in_min, set_in_min, 5);
  275. static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
  276. show_in_min, set_in_min, 6);
  277. static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
  278. show_in_max, set_in_max, 0);
  279. static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
  280. show_in_max, set_in_max, 1);
  281. static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
  282. show_in_max, set_in_max, 2);
  283. static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
  284. show_in_max, set_in_max, 3);
  285. static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
  286. show_in_max, set_in_max, 4);
  287. static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
  288. show_in_max, set_in_max, 5);
  289. static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
  290. show_in_max, set_in_max, 6);
  291. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL, 0);
  292. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in_input, NULL, 1);
  293. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in_input, NULL, 2);
  294. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in_input, NULL, 3);
  295. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in_input, NULL, 4);
  296. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in_input, NULL, 5);
  297. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in_input, NULL, 6);
  298. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
  299. show_fan_min, set_fan_min, 0);
  300. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
  301. show_fan_min, set_fan_min, 1);
  302. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
  303. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
  304. static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
  305. show_fan_div, set_fan_div, 0);
  306. static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
  307. show_fan_div, set_fan_div, 1);
  308. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  309. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_hot_max,
  310. set_temp_hot_max);
  311. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, show_temp_hot_hyst,
  312. set_temp_hot_hyst);
  313. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_os_max,
  314. set_temp_os_max);
  315. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_os_hyst,
  316. set_temp_os_hyst);
  317. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  318. /*
  319. * Real code
  320. */
  321. static int lm80_attach_adapter(struct i2c_adapter *adapter)
  322. {
  323. if (!(adapter->class & I2C_CLASS_HWMON))
  324. return 0;
  325. return i2c_probe(adapter, &addr_data, lm80_detect);
  326. }
  327. static struct attribute *lm80_attributes[] = {
  328. &sensor_dev_attr_in0_min.dev_attr.attr,
  329. &sensor_dev_attr_in1_min.dev_attr.attr,
  330. &sensor_dev_attr_in2_min.dev_attr.attr,
  331. &sensor_dev_attr_in3_min.dev_attr.attr,
  332. &sensor_dev_attr_in4_min.dev_attr.attr,
  333. &sensor_dev_attr_in5_min.dev_attr.attr,
  334. &sensor_dev_attr_in6_min.dev_attr.attr,
  335. &sensor_dev_attr_in0_max.dev_attr.attr,
  336. &sensor_dev_attr_in1_max.dev_attr.attr,
  337. &sensor_dev_attr_in2_max.dev_attr.attr,
  338. &sensor_dev_attr_in3_max.dev_attr.attr,
  339. &sensor_dev_attr_in4_max.dev_attr.attr,
  340. &sensor_dev_attr_in5_max.dev_attr.attr,
  341. &sensor_dev_attr_in6_max.dev_attr.attr,
  342. &sensor_dev_attr_in0_input.dev_attr.attr,
  343. &sensor_dev_attr_in1_input.dev_attr.attr,
  344. &sensor_dev_attr_in2_input.dev_attr.attr,
  345. &sensor_dev_attr_in3_input.dev_attr.attr,
  346. &sensor_dev_attr_in4_input.dev_attr.attr,
  347. &sensor_dev_attr_in5_input.dev_attr.attr,
  348. &sensor_dev_attr_in6_input.dev_attr.attr,
  349. &sensor_dev_attr_fan1_min.dev_attr.attr,
  350. &sensor_dev_attr_fan2_min.dev_attr.attr,
  351. &sensor_dev_attr_fan1_input.dev_attr.attr,
  352. &sensor_dev_attr_fan2_input.dev_attr.attr,
  353. &sensor_dev_attr_fan1_div.dev_attr.attr,
  354. &sensor_dev_attr_fan2_div.dev_attr.attr,
  355. &dev_attr_temp1_input.attr,
  356. &dev_attr_temp1_max.attr,
  357. &dev_attr_temp1_max_hyst.attr,
  358. &dev_attr_temp1_crit.attr,
  359. &dev_attr_temp1_crit_hyst.attr,
  360. &dev_attr_alarms.attr,
  361. NULL
  362. };
  363. static const struct attribute_group lm80_group = {
  364. .attrs = lm80_attributes,
  365. };
  366. static int lm80_detect(struct i2c_adapter *adapter, int address, int kind)
  367. {
  368. int i, cur;
  369. struct i2c_client *client;
  370. struct lm80_data *data;
  371. int err = 0;
  372. const char *name;
  373. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  374. goto exit;
  375. /* OK. For now, we presume we have a valid client. We now create the
  376. client structure, even though we cannot fill it completely yet.
  377. But it allows us to access lm80_{read,write}_value. */
  378. if (!(data = kzalloc(sizeof(struct lm80_data), GFP_KERNEL))) {
  379. err = -ENOMEM;
  380. goto exit;
  381. }
  382. client = &data->client;
  383. i2c_set_clientdata(client, data);
  384. client->addr = address;
  385. client->adapter = adapter;
  386. client->driver = &lm80_driver;
  387. /* Now, we do the remaining detection. It is lousy. */
  388. if (lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
  389. goto error_free;
  390. for (i = 0x2a; i <= 0x3d; i++) {
  391. cur = i2c_smbus_read_byte_data(client, i);
  392. if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
  393. || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
  394. || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
  395. goto error_free;
  396. }
  397. /* Determine the chip type - only one kind supported! */
  398. kind = lm80;
  399. name = "lm80";
  400. /* Fill in the remaining client fields */
  401. strlcpy(client->name, name, I2C_NAME_SIZE);
  402. mutex_init(&data->update_lock);
  403. /* Tell the I2C layer a new client has arrived */
  404. if ((err = i2c_attach_client(client)))
  405. goto error_free;
  406. /* Initialize the LM80 chip */
  407. lm80_init_client(client);
  408. /* A few vars need to be filled upon startup */
  409. data->fan_min[0] = lm80_read_value(client, LM80_REG_FAN_MIN(1));
  410. data->fan_min[1] = lm80_read_value(client, LM80_REG_FAN_MIN(2));
  411. /* Register sysfs hooks */
  412. if ((err = sysfs_create_group(&client->dev.kobj, &lm80_group)))
  413. goto error_detach;
  414. data->hwmon_dev = hwmon_device_register(&client->dev);
  415. if (IS_ERR(data->hwmon_dev)) {
  416. err = PTR_ERR(data->hwmon_dev);
  417. goto error_remove;
  418. }
  419. return 0;
  420. error_remove:
  421. sysfs_remove_group(&client->dev.kobj, &lm80_group);
  422. error_detach:
  423. i2c_detach_client(client);
  424. error_free:
  425. kfree(data);
  426. exit:
  427. return err;
  428. }
  429. static int lm80_detach_client(struct i2c_client *client)
  430. {
  431. struct lm80_data *data = i2c_get_clientdata(client);
  432. int err;
  433. hwmon_device_unregister(data->hwmon_dev);
  434. sysfs_remove_group(&client->dev.kobj, &lm80_group);
  435. if ((err = i2c_detach_client(client)))
  436. return err;
  437. kfree(data);
  438. return 0;
  439. }
  440. static int lm80_read_value(struct i2c_client *client, u8 reg)
  441. {
  442. return i2c_smbus_read_byte_data(client, reg);
  443. }
  444. static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
  445. {
  446. return i2c_smbus_write_byte_data(client, reg, value);
  447. }
  448. /* Called when we have found a new LM80. */
  449. static void lm80_init_client(struct i2c_client *client)
  450. {
  451. /* Reset all except Watchdog values and last conversion values
  452. This sets fan-divs to 2, among others. This makes most other
  453. initializations unnecessary */
  454. lm80_write_value(client, LM80_REG_CONFIG, 0x80);
  455. /* Set 11-bit temperature resolution */
  456. lm80_write_value(client, LM80_REG_RES, 0x08);
  457. /* Start monitoring */
  458. lm80_write_value(client, LM80_REG_CONFIG, 0x01);
  459. }
  460. static struct lm80_data *lm80_update_device(struct device *dev)
  461. {
  462. struct i2c_client *client = to_i2c_client(dev);
  463. struct lm80_data *data = i2c_get_clientdata(client);
  464. int i;
  465. mutex_lock(&data->update_lock);
  466. if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
  467. dev_dbg(&client->dev, "Starting lm80 update\n");
  468. for (i = 0; i <= 6; i++) {
  469. data->in[i] =
  470. lm80_read_value(client, LM80_REG_IN(i));
  471. data->in_min[i] =
  472. lm80_read_value(client, LM80_REG_IN_MIN(i));
  473. data->in_max[i] =
  474. lm80_read_value(client, LM80_REG_IN_MAX(i));
  475. }
  476. data->fan[0] = lm80_read_value(client, LM80_REG_FAN1);
  477. data->fan_min[0] =
  478. lm80_read_value(client, LM80_REG_FAN_MIN(1));
  479. data->fan[1] = lm80_read_value(client, LM80_REG_FAN2);
  480. data->fan_min[1] =
  481. lm80_read_value(client, LM80_REG_FAN_MIN(2));
  482. data->temp =
  483. (lm80_read_value(client, LM80_REG_TEMP) << 8) |
  484. (lm80_read_value(client, LM80_REG_RES) & 0xf0);
  485. data->temp_os_max =
  486. lm80_read_value(client, LM80_REG_TEMP_OS_MAX);
  487. data->temp_os_hyst =
  488. lm80_read_value(client, LM80_REG_TEMP_OS_HYST);
  489. data->temp_hot_max =
  490. lm80_read_value(client, LM80_REG_TEMP_HOT_MAX);
  491. data->temp_hot_hyst =
  492. lm80_read_value(client, LM80_REG_TEMP_HOT_HYST);
  493. i = lm80_read_value(client, LM80_REG_FANDIV);
  494. data->fan_div[0] = (i >> 2) & 0x03;
  495. data->fan_div[1] = (i >> 4) & 0x03;
  496. data->alarms = lm80_read_value(client, LM80_REG_ALARM1) +
  497. (lm80_read_value(client, LM80_REG_ALARM2) << 8);
  498. data->last_updated = jiffies;
  499. data->valid = 1;
  500. }
  501. mutex_unlock(&data->update_lock);
  502. return data;
  503. }
  504. static int __init sensors_lm80_init(void)
  505. {
  506. return i2c_add_driver(&lm80_driver);
  507. }
  508. static void __exit sensors_lm80_exit(void)
  509. {
  510. i2c_del_driver(&lm80_driver);
  511. }
  512. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
  513. "Philip Edelbrock <phil@netroedge.com>");
  514. MODULE_DESCRIPTION("LM80 driver");
  515. MODULE_LICENSE("GPL");
  516. module_init(sensors_lm80_init);
  517. module_exit(sensors_lm80_exit);