lm90.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2004 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * Based on the lm83 driver. The LM90 is a sensor chip made by National
  7. * Semiconductor. It reports up to two temperatures (its own plus up to
  8. * one external one) with a 0.125 deg resolution (1 deg for local
  9. * temperature) and a 3-4 deg accuracy. Complete datasheet can be
  10. * obtained from National's website at:
  11. * http://www.national.com/pf/LM/LM90.html
  12. *
  13. * This driver also supports the LM89 and LM99, two other sensor chips
  14. * made by National Semiconductor. Both have an increased remote
  15. * temperature measurement accuracy (1 degree), and the LM99
  16. * additionally shifts remote temperatures (measured and limits) by 16
  17. * degrees, which allows for higher temperatures measurement. The
  18. * driver doesn't handle it since it can be done easily in user-space.
  19. * Complete datasheets can be obtained from National's website at:
  20. * http://www.national.com/pf/LM/LM89.html
  21. * http://www.national.com/pf/LM/LM99.html
  22. * Note that there is no way to differenciate between both chips.
  23. *
  24. * This driver also supports the LM86, another sensor chip made by
  25. * National Semiconductor. It is exactly similar to the LM90 except it
  26. * has a higher accuracy.
  27. * Complete datasheet can be obtained from National's website at:
  28. * http://www.national.com/pf/LM/LM86.html
  29. *
  30. * This driver also supports the ADM1032, a sensor chip made by Analog
  31. * Devices. That chip is similar to the LM90, with a few differences
  32. * that are not handled by this driver. Complete datasheet can be
  33. * obtained from Analog's website at:
  34. * http://products.analog.com/products/info.asp?product=ADM1032
  35. * Among others, it has a higher accuracy than the LM90, much like the
  36. * LM86 does.
  37. *
  38. * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
  39. * chips made by Maxim. These chips are similar to the LM86. Complete
  40. * datasheet can be obtained at Maxim's website at:
  41. * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2578
  42. * Note that there is no easy way to differenciate between the three
  43. * variants. The extra address and features of the MAX6659 are not
  44. * supported by this driver.
  45. *
  46. * This driver also supports the ADT7461 chip from Analog Devices but
  47. * only in its "compatability mode". If an ADT7461 chip is found but
  48. * is configured in non-compatible mode (where its temperature
  49. * register values are decoded differently) it is ignored by this
  50. * driver. Complete datasheet can be obtained from Analog's website
  51. * at:
  52. * http://products.analog.com/products/info.asp?product=ADT7461
  53. *
  54. * Since the LM90 was the first chipset supported by this driver, most
  55. * comments will refer to this chipset, but are actually general and
  56. * concern all supported chipsets, unless mentioned otherwise.
  57. *
  58. * This program is free software; you can redistribute it and/or modify
  59. * it under the terms of the GNU General Public License as published by
  60. * the Free Software Foundation; either version 2 of the License, or
  61. * (at your option) any later version.
  62. *
  63. * This program is distributed in the hope that it will be useful,
  64. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. * GNU General Public License for more details.
  67. *
  68. * You should have received a copy of the GNU General Public License
  69. * along with this program; if not, write to the Free Software
  70. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  71. */
  72. #include <linux/config.h>
  73. #include <linux/module.h>
  74. #include <linux/init.h>
  75. #include <linux/slab.h>
  76. #include <linux/jiffies.h>
  77. #include <linux/i2c.h>
  78. #include <linux/i2c-sensor.h>
  79. /*
  80. * Addresses to scan
  81. * Address is fully defined internally and cannot be changed except for
  82. * MAX6659.
  83. * LM86, LM89, LM90, LM99, ADM1032, MAX6657 and MAX6658 have address 0x4c.
  84. * LM89-1, and LM99-1 have address 0x4d.
  85. * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
  86. * ADT7461 always has address 0x4c.
  87. */
  88. static unsigned short normal_i2c[] = { 0x4c, 0x4d, I2C_CLIENT_END };
  89. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  90. /*
  91. * Insmod parameters
  92. */
  93. SENSORS_INSMOD_6(lm90, adm1032, lm99, lm86, max6657, adt7461);
  94. /*
  95. * The LM90 registers
  96. */
  97. #define LM90_REG_R_MAN_ID 0xFE
  98. #define LM90_REG_R_CHIP_ID 0xFF
  99. #define LM90_REG_R_CONFIG1 0x03
  100. #define LM90_REG_W_CONFIG1 0x09
  101. #define LM90_REG_R_CONFIG2 0xBF
  102. #define LM90_REG_W_CONFIG2 0xBF
  103. #define LM90_REG_R_CONVRATE 0x04
  104. #define LM90_REG_W_CONVRATE 0x0A
  105. #define LM90_REG_R_STATUS 0x02
  106. #define LM90_REG_R_LOCAL_TEMP 0x00
  107. #define LM90_REG_R_LOCAL_HIGH 0x05
  108. #define LM90_REG_W_LOCAL_HIGH 0x0B
  109. #define LM90_REG_R_LOCAL_LOW 0x06
  110. #define LM90_REG_W_LOCAL_LOW 0x0C
  111. #define LM90_REG_R_LOCAL_CRIT 0x20
  112. #define LM90_REG_W_LOCAL_CRIT 0x20
  113. #define LM90_REG_R_REMOTE_TEMPH 0x01
  114. #define LM90_REG_R_REMOTE_TEMPL 0x10
  115. #define LM90_REG_R_REMOTE_OFFSH 0x11
  116. #define LM90_REG_W_REMOTE_OFFSH 0x11
  117. #define LM90_REG_R_REMOTE_OFFSL 0x12
  118. #define LM90_REG_W_REMOTE_OFFSL 0x12
  119. #define LM90_REG_R_REMOTE_HIGHH 0x07
  120. #define LM90_REG_W_REMOTE_HIGHH 0x0D
  121. #define LM90_REG_R_REMOTE_HIGHL 0x13
  122. #define LM90_REG_W_REMOTE_HIGHL 0x13
  123. #define LM90_REG_R_REMOTE_LOWH 0x08
  124. #define LM90_REG_W_REMOTE_LOWH 0x0E
  125. #define LM90_REG_R_REMOTE_LOWL 0x14
  126. #define LM90_REG_W_REMOTE_LOWL 0x14
  127. #define LM90_REG_R_REMOTE_CRIT 0x19
  128. #define LM90_REG_W_REMOTE_CRIT 0x19
  129. #define LM90_REG_R_TCRIT_HYST 0x21
  130. #define LM90_REG_W_TCRIT_HYST 0x21
  131. /*
  132. * Conversions and various macros
  133. * For local temperatures and limits, critical limits and the hysteresis
  134. * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celcius.
  135. * For remote temperatures and limits, it uses signed 11-bit values with
  136. * LSB = 0.125 degree Celcius, left-justified in 16-bit registers.
  137. */
  138. #define TEMP1_FROM_REG(val) ((val) * 1000)
  139. #define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
  140. (val) >= 127000 ? 127 : \
  141. (val) < 0 ? ((val) - 500) / 1000 : \
  142. ((val) + 500) / 1000)
  143. #define TEMP2_FROM_REG(val) ((val) / 32 * 125)
  144. #define TEMP2_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  145. (val) >= 127875 ? 0x7FE0 : \
  146. (val) < 0 ? ((val) - 62) / 125 * 32 : \
  147. ((val) + 62) / 125 * 32)
  148. #define HYST_TO_REG(val) ((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
  149. ((val) + 500) / 1000)
  150. /*
  151. * ADT7461 is almost identical to LM90 except that attempts to write
  152. * values that are outside the range 0 < temp < 127 are treated as
  153. * the boundary value.
  154. */
  155. #define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
  156. (val) >= 127000 ? 127 : \
  157. ((val) + 500) / 1000)
  158. #define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
  159. (val) >= 127750 ? 0x7FC0 : \
  160. ((val) + 125) / 250 * 64)
  161. /*
  162. * Functions declaration
  163. */
  164. static int lm90_attach_adapter(struct i2c_adapter *adapter);
  165. static int lm90_detect(struct i2c_adapter *adapter, int address,
  166. int kind);
  167. static void lm90_init_client(struct i2c_client *client);
  168. static int lm90_detach_client(struct i2c_client *client);
  169. static struct lm90_data *lm90_update_device(struct device *dev);
  170. /*
  171. * Driver data (common to all clients)
  172. */
  173. static struct i2c_driver lm90_driver = {
  174. .owner = THIS_MODULE,
  175. .name = "lm90",
  176. .id = I2C_DRIVERID_LM90,
  177. .flags = I2C_DF_NOTIFY,
  178. .attach_adapter = lm90_attach_adapter,
  179. .detach_client = lm90_detach_client,
  180. };
  181. /*
  182. * Client data (each client gets its own)
  183. */
  184. struct lm90_data {
  185. struct i2c_client client;
  186. struct semaphore update_lock;
  187. char valid; /* zero until following fields are valid */
  188. unsigned long last_updated; /* in jiffies */
  189. int kind;
  190. /* registers values */
  191. s8 temp_input1, temp_low1, temp_high1; /* local */
  192. s16 temp_input2, temp_low2, temp_high2; /* remote, combined */
  193. s8 temp_crit1, temp_crit2;
  194. u8 temp_hyst;
  195. u8 alarms; /* bitvector */
  196. };
  197. /*
  198. * Sysfs stuff
  199. */
  200. #define show_temp(value, converter) \
  201. static ssize_t show_##value(struct device *dev, char *buf) \
  202. { \
  203. struct lm90_data *data = lm90_update_device(dev); \
  204. return sprintf(buf, "%d\n", converter(data->value)); \
  205. }
  206. show_temp(temp_input1, TEMP1_FROM_REG);
  207. show_temp(temp_input2, TEMP2_FROM_REG);
  208. show_temp(temp_low1, TEMP1_FROM_REG);
  209. show_temp(temp_low2, TEMP2_FROM_REG);
  210. show_temp(temp_high1, TEMP1_FROM_REG);
  211. show_temp(temp_high2, TEMP2_FROM_REG);
  212. show_temp(temp_crit1, TEMP1_FROM_REG);
  213. show_temp(temp_crit2, TEMP1_FROM_REG);
  214. #define set_temp1(value, reg) \
  215. static ssize_t set_##value(struct device *dev, const char *buf, \
  216. size_t count) \
  217. { \
  218. struct i2c_client *client = to_i2c_client(dev); \
  219. struct lm90_data *data = i2c_get_clientdata(client); \
  220. long val = simple_strtol(buf, NULL, 10); \
  221. \
  222. down(&data->update_lock); \
  223. if (data->kind == adt7461) \
  224. data->value = TEMP1_TO_REG_ADT7461(val); \
  225. else \
  226. data->value = TEMP1_TO_REG(val); \
  227. i2c_smbus_write_byte_data(client, reg, data->value); \
  228. up(&data->update_lock); \
  229. return count; \
  230. }
  231. #define set_temp2(value, regh, regl) \
  232. static ssize_t set_##value(struct device *dev, const char *buf, \
  233. size_t count) \
  234. { \
  235. struct i2c_client *client = to_i2c_client(dev); \
  236. struct lm90_data *data = i2c_get_clientdata(client); \
  237. long val = simple_strtol(buf, NULL, 10); \
  238. \
  239. down(&data->update_lock); \
  240. if (data->kind == adt7461) \
  241. data->value = TEMP2_TO_REG_ADT7461(val); \
  242. else \
  243. data->value = TEMP2_TO_REG(val); \
  244. i2c_smbus_write_byte_data(client, regh, data->value >> 8); \
  245. i2c_smbus_write_byte_data(client, regl, data->value & 0xff); \
  246. up(&data->update_lock); \
  247. return count; \
  248. }
  249. set_temp1(temp_low1, LM90_REG_W_LOCAL_LOW);
  250. set_temp2(temp_low2, LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL);
  251. set_temp1(temp_high1, LM90_REG_W_LOCAL_HIGH);
  252. set_temp2(temp_high2, LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL);
  253. set_temp1(temp_crit1, LM90_REG_W_LOCAL_CRIT);
  254. set_temp1(temp_crit2, LM90_REG_W_REMOTE_CRIT);
  255. #define show_temp_hyst(value, basereg) \
  256. static ssize_t show_##value(struct device *dev, char *buf) \
  257. { \
  258. struct lm90_data *data = lm90_update_device(dev); \
  259. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->basereg) \
  260. - TEMP1_FROM_REG(data->temp_hyst)); \
  261. }
  262. show_temp_hyst(temp_hyst1, temp_crit1);
  263. show_temp_hyst(temp_hyst2, temp_crit2);
  264. static ssize_t set_temp_hyst1(struct device *dev, const char *buf,
  265. size_t count)
  266. {
  267. struct i2c_client *client = to_i2c_client(dev);
  268. struct lm90_data *data = i2c_get_clientdata(client);
  269. long val = simple_strtol(buf, NULL, 10);
  270. long hyst;
  271. down(&data->update_lock);
  272. hyst = TEMP1_FROM_REG(data->temp_crit1) - val;
  273. i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
  274. HYST_TO_REG(hyst));
  275. up(&data->update_lock);
  276. return count;
  277. }
  278. static ssize_t show_alarms(struct device *dev, char *buf)
  279. {
  280. struct lm90_data *data = lm90_update_device(dev);
  281. return sprintf(buf, "%d\n", data->alarms);
  282. }
  283. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
  284. static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
  285. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_low1,
  286. set_temp_low1);
  287. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_low2,
  288. set_temp_low2);
  289. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
  290. set_temp_high1);
  291. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
  292. set_temp_high2);
  293. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit1,
  294. set_temp_crit1);
  295. static DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit2,
  296. set_temp_crit2);
  297. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_hyst1,
  298. set_temp_hyst1);
  299. static DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_hyst2, NULL);
  300. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  301. /*
  302. * Real code
  303. */
  304. static int lm90_attach_adapter(struct i2c_adapter *adapter)
  305. {
  306. if (!(adapter->class & I2C_CLASS_HWMON))
  307. return 0;
  308. return i2c_detect(adapter, &addr_data, lm90_detect);
  309. }
  310. /*
  311. * The following function does more than just detection. If detection
  312. * succeeds, it also registers the new chip.
  313. */
  314. static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
  315. {
  316. struct i2c_client *new_client;
  317. struct lm90_data *data;
  318. int err = 0;
  319. const char *name = "";
  320. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  321. goto exit;
  322. if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
  323. err = -ENOMEM;
  324. goto exit;
  325. }
  326. memset(data, 0, sizeof(struct lm90_data));
  327. /* The common I2C client data is placed right before the
  328. LM90-specific data. */
  329. new_client = &data->client;
  330. i2c_set_clientdata(new_client, data);
  331. new_client->addr = address;
  332. new_client->adapter = adapter;
  333. new_client->driver = &lm90_driver;
  334. new_client->flags = 0;
  335. /*
  336. * Now we do the remaining detection. A negative kind means that
  337. * the driver was loaded with no force parameter (default), so we
  338. * must both detect and identify the chip. A zero kind means that
  339. * the driver was loaded with the force parameter, the detection
  340. * step shall be skipped. A positive kind means that the driver
  341. * was loaded with the force parameter and a given kind of chip is
  342. * requested, so both the detection and the identification steps
  343. * are skipped.
  344. */
  345. /* Default to an LM90 if forced */
  346. if (kind == 0)
  347. kind = lm90;
  348. if (kind < 0) { /* detection and identification */
  349. u8 man_id, chip_id, reg_config1, reg_convrate;
  350. man_id = i2c_smbus_read_byte_data(new_client,
  351. LM90_REG_R_MAN_ID);
  352. chip_id = i2c_smbus_read_byte_data(new_client,
  353. LM90_REG_R_CHIP_ID);
  354. reg_config1 = i2c_smbus_read_byte_data(new_client,
  355. LM90_REG_R_CONFIG1);
  356. reg_convrate = i2c_smbus_read_byte_data(new_client,
  357. LM90_REG_R_CONVRATE);
  358. if (man_id == 0x01) { /* National Semiconductor */
  359. u8 reg_config2;
  360. reg_config2 = i2c_smbus_read_byte_data(new_client,
  361. LM90_REG_R_CONFIG2);
  362. if ((reg_config1 & 0x2A) == 0x00
  363. && (reg_config2 & 0xF8) == 0x00
  364. && reg_convrate <= 0x09) {
  365. if (address == 0x4C
  366. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  367. kind = lm90;
  368. } else
  369. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  370. kind = lm99;
  371. } else
  372. if (address == 0x4C
  373. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  374. kind = lm86;
  375. }
  376. }
  377. } else
  378. if (man_id == 0x41) { /* Analog Devices */
  379. if (address == 0x4C
  380. && (chip_id & 0xF0) == 0x40 /* ADM1032 */
  381. && (reg_config1 & 0x3F) == 0x00
  382. && reg_convrate <= 0x0A) {
  383. kind = adm1032;
  384. } else
  385. if (address == 0x4c
  386. && chip_id == 0x51 /* ADT7461 */
  387. && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
  388. && reg_convrate <= 0x0A) {
  389. kind = adt7461;
  390. }
  391. } else
  392. if (man_id == 0x4D) { /* Maxim */
  393. /*
  394. * The Maxim variants do NOT have a chip_id register.
  395. * Reading from that address will return the last read
  396. * value, which in our case is those of the man_id
  397. * register. Likewise, the config1 register seems to
  398. * lack a low nibble, so the value will be those of the
  399. * previous read, so in our case those of the man_id
  400. * register.
  401. */
  402. if (chip_id == man_id
  403. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  404. && reg_convrate <= 0x09) {
  405. kind = max6657;
  406. }
  407. }
  408. if (kind <= 0) { /* identification failed */
  409. dev_info(&adapter->dev,
  410. "Unsupported chip (man_id=0x%02X, "
  411. "chip_id=0x%02X).\n", man_id, chip_id);
  412. goto exit_free;
  413. }
  414. }
  415. if (kind == lm90) {
  416. name = "lm90";
  417. } else if (kind == adm1032) {
  418. name = "adm1032";
  419. } else if (kind == lm99) {
  420. name = "lm99";
  421. } else if (kind == lm86) {
  422. name = "lm86";
  423. } else if (kind == max6657) {
  424. name = "max6657";
  425. } else if (kind == adt7461) {
  426. name = "adt7461";
  427. }
  428. /* We can fill in the remaining client fields */
  429. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  430. data->valid = 0;
  431. data->kind = kind;
  432. init_MUTEX(&data->update_lock);
  433. /* Tell the I2C layer a new client has arrived */
  434. if ((err = i2c_attach_client(new_client)))
  435. goto exit_free;
  436. /* Initialize the LM90 chip */
  437. lm90_init_client(new_client);
  438. /* Register sysfs hooks */
  439. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  440. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  441. device_create_file(&new_client->dev, &dev_attr_temp1_min);
  442. device_create_file(&new_client->dev, &dev_attr_temp2_min);
  443. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  444. device_create_file(&new_client->dev, &dev_attr_temp2_max);
  445. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  446. device_create_file(&new_client->dev, &dev_attr_temp2_crit);
  447. device_create_file(&new_client->dev, &dev_attr_temp1_crit_hyst);
  448. device_create_file(&new_client->dev, &dev_attr_temp2_crit_hyst);
  449. device_create_file(&new_client->dev, &dev_attr_alarms);
  450. return 0;
  451. exit_free:
  452. kfree(data);
  453. exit:
  454. return err;
  455. }
  456. static void lm90_init_client(struct i2c_client *client)
  457. {
  458. u8 config;
  459. /*
  460. * Start the conversions.
  461. */
  462. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  463. 5); /* 2 Hz */
  464. config = i2c_smbus_read_byte_data(client, LM90_REG_R_CONFIG1);
  465. if (config & 0x40)
  466. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  467. config & 0xBF); /* run */
  468. }
  469. static int lm90_detach_client(struct i2c_client *client)
  470. {
  471. int err;
  472. if ((err = i2c_detach_client(client))) {
  473. dev_err(&client->dev, "Client deregistration failed, "
  474. "client not detached.\n");
  475. return err;
  476. }
  477. kfree(i2c_get_clientdata(client));
  478. return 0;
  479. }
  480. static struct lm90_data *lm90_update_device(struct device *dev)
  481. {
  482. struct i2c_client *client = to_i2c_client(dev);
  483. struct lm90_data *data = i2c_get_clientdata(client);
  484. down(&data->update_lock);
  485. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  486. u8 oldh, newh;
  487. dev_dbg(&client->dev, "Updating lm90 data.\n");
  488. data->temp_input1 = i2c_smbus_read_byte_data(client,
  489. LM90_REG_R_LOCAL_TEMP);
  490. data->temp_high1 = i2c_smbus_read_byte_data(client,
  491. LM90_REG_R_LOCAL_HIGH);
  492. data->temp_low1 = i2c_smbus_read_byte_data(client,
  493. LM90_REG_R_LOCAL_LOW);
  494. data->temp_crit1 = i2c_smbus_read_byte_data(client,
  495. LM90_REG_R_LOCAL_CRIT);
  496. data->temp_crit2 = i2c_smbus_read_byte_data(client,
  497. LM90_REG_R_REMOTE_CRIT);
  498. data->temp_hyst = i2c_smbus_read_byte_data(client,
  499. LM90_REG_R_TCRIT_HYST);
  500. /*
  501. * There is a trick here. We have to read two registers to
  502. * have the remote sensor temperature, but we have to beware
  503. * a conversion could occur inbetween the readings. The
  504. * datasheet says we should either use the one-shot
  505. * conversion register, which we don't want to do (disables
  506. * hardware monitoring) or monitor the busy bit, which is
  507. * impossible (we can't read the values and monitor that bit
  508. * at the exact same time). So the solution used here is to
  509. * read the high byte once, then the low byte, then the high
  510. * byte again. If the new high byte matches the old one,
  511. * then we have a valid reading. Else we have to read the low
  512. * byte again, and now we believe we have a correct reading.
  513. */
  514. oldh = i2c_smbus_read_byte_data(client,
  515. LM90_REG_R_REMOTE_TEMPH);
  516. data->temp_input2 = i2c_smbus_read_byte_data(client,
  517. LM90_REG_R_REMOTE_TEMPL);
  518. newh = i2c_smbus_read_byte_data(client,
  519. LM90_REG_R_REMOTE_TEMPH);
  520. if (newh != oldh) {
  521. data->temp_input2 = i2c_smbus_read_byte_data(client,
  522. LM90_REG_R_REMOTE_TEMPL);
  523. #ifdef DEBUG
  524. oldh = i2c_smbus_read_byte_data(client,
  525. LM90_REG_R_REMOTE_TEMPH);
  526. /* oldh is actually newer */
  527. if (newh != oldh)
  528. dev_warn(&client->dev, "Remote temperature may be "
  529. "wrong.\n");
  530. #endif
  531. }
  532. data->temp_input2 |= (newh << 8);
  533. data->temp_high2 = (i2c_smbus_read_byte_data(client,
  534. LM90_REG_R_REMOTE_HIGHH) << 8) +
  535. i2c_smbus_read_byte_data(client,
  536. LM90_REG_R_REMOTE_HIGHL);
  537. data->temp_low2 = (i2c_smbus_read_byte_data(client,
  538. LM90_REG_R_REMOTE_LOWH) << 8) +
  539. i2c_smbus_read_byte_data(client,
  540. LM90_REG_R_REMOTE_LOWL);
  541. data->alarms = i2c_smbus_read_byte_data(client,
  542. LM90_REG_R_STATUS);
  543. data->last_updated = jiffies;
  544. data->valid = 1;
  545. }
  546. up(&data->update_lock);
  547. return data;
  548. }
  549. static int __init sensors_lm90_init(void)
  550. {
  551. return i2c_add_driver(&lm90_driver);
  552. }
  553. static void __exit sensors_lm90_exit(void)
  554. {
  555. i2c_del_driver(&lm90_driver);
  556. }
  557. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  558. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  559. MODULE_LICENSE("GPL");
  560. module_init(sensors_lm90_init);
  561. module_exit(sensors_lm90_exit);