lm90.c 20 KB

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