lm90.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2005 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://www.analog.com/en/prod/0,2877,ADM1032,00.html
  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://www.analog.com/en/prod/0,2877,ADT7461,00.html
  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/hwmon-sysfs.h>
  78. #include <linux/hwmon.h>
  79. #include <linux/err.h>
  80. #include <linux/mutex.h>
  81. /*
  82. * Addresses to scan
  83. * Address is fully defined internally and cannot be changed except for
  84. * MAX6659.
  85. * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
  86. * have address 0x4c.
  87. * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
  88. * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
  89. */
  90. static unsigned short normal_i2c[] = { 0x4c, 0x4d, I2C_CLIENT_END };
  91. /*
  92. * Insmod parameters
  93. */
  94. I2C_CLIENT_INSMOD_6(lm90, adm1032, lm99, lm86, max6657, adt7461);
  95. /*
  96. * The LM90 registers
  97. */
  98. #define LM90_REG_R_MAN_ID 0xFE
  99. #define LM90_REG_R_CHIP_ID 0xFF
  100. #define LM90_REG_R_CONFIG1 0x03
  101. #define LM90_REG_W_CONFIG1 0x09
  102. #define LM90_REG_R_CONFIG2 0xBF
  103. #define LM90_REG_W_CONFIG2 0xBF
  104. #define LM90_REG_R_CONVRATE 0x04
  105. #define LM90_REG_W_CONVRATE 0x0A
  106. #define LM90_REG_R_STATUS 0x02
  107. #define LM90_REG_R_LOCAL_TEMP 0x00
  108. #define LM90_REG_R_LOCAL_HIGH 0x05
  109. #define LM90_REG_W_LOCAL_HIGH 0x0B
  110. #define LM90_REG_R_LOCAL_LOW 0x06
  111. #define LM90_REG_W_LOCAL_LOW 0x0C
  112. #define LM90_REG_R_LOCAL_CRIT 0x20
  113. #define LM90_REG_W_LOCAL_CRIT 0x20
  114. #define LM90_REG_R_REMOTE_TEMPH 0x01
  115. #define LM90_REG_R_REMOTE_TEMPL 0x10
  116. #define LM90_REG_R_REMOTE_OFFSH 0x11
  117. #define LM90_REG_W_REMOTE_OFFSH 0x11
  118. #define LM90_REG_R_REMOTE_OFFSL 0x12
  119. #define LM90_REG_W_REMOTE_OFFSL 0x12
  120. #define LM90_REG_R_REMOTE_HIGHH 0x07
  121. #define LM90_REG_W_REMOTE_HIGHH 0x0D
  122. #define LM90_REG_R_REMOTE_HIGHL 0x13
  123. #define LM90_REG_W_REMOTE_HIGHL 0x13
  124. #define LM90_REG_R_REMOTE_LOWH 0x08
  125. #define LM90_REG_W_REMOTE_LOWH 0x0E
  126. #define LM90_REG_R_REMOTE_LOWL 0x14
  127. #define LM90_REG_W_REMOTE_LOWL 0x14
  128. #define LM90_REG_R_REMOTE_CRIT 0x19
  129. #define LM90_REG_W_REMOTE_CRIT 0x19
  130. #define LM90_REG_R_TCRIT_HYST 0x21
  131. #define LM90_REG_W_TCRIT_HYST 0x21
  132. /*
  133. * Conversions and various macros
  134. * For local temperatures and limits, critical limits and the hysteresis
  135. * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
  136. * For remote temperatures and limits, it uses signed 11-bit values with
  137. * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
  138. */
  139. #define TEMP1_FROM_REG(val) ((val) * 1000)
  140. #define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
  141. (val) >= 127000 ? 127 : \
  142. (val) < 0 ? ((val) - 500) / 1000 : \
  143. ((val) + 500) / 1000)
  144. #define TEMP2_FROM_REG(val) ((val) / 32 * 125)
  145. #define TEMP2_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  146. (val) >= 127875 ? 0x7FE0 : \
  147. (val) < 0 ? ((val) - 62) / 125 * 32 : \
  148. ((val) + 62) / 125 * 32)
  149. #define HYST_TO_REG(val) ((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
  150. ((val) + 500) / 1000)
  151. /*
  152. * ADT7461 is almost identical to LM90 except that attempts to write
  153. * values that are outside the range 0 < temp < 127 are treated as
  154. * the boundary value.
  155. */
  156. #define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
  157. (val) >= 127000 ? 127 : \
  158. ((val) + 500) / 1000)
  159. #define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
  160. (val) >= 127750 ? 0x7FC0 : \
  161. ((val) + 125) / 250 * 64)
  162. /*
  163. * Functions declaration
  164. */
  165. static int lm90_attach_adapter(struct i2c_adapter *adapter);
  166. static int lm90_detect(struct i2c_adapter *adapter, int address,
  167. int kind);
  168. static void lm90_init_client(struct i2c_client *client);
  169. static int lm90_detach_client(struct i2c_client *client);
  170. static struct lm90_data *lm90_update_device(struct device *dev);
  171. /*
  172. * Driver data (common to all clients)
  173. */
  174. static struct i2c_driver lm90_driver = {
  175. .driver = {
  176. .name = "lm90",
  177. },
  178. .id = I2C_DRIVERID_LM90,
  179. .attach_adapter = lm90_attach_adapter,
  180. .detach_client = lm90_detach_client,
  181. };
  182. /*
  183. * Client data (each client gets its own)
  184. */
  185. struct lm90_data {
  186. struct i2c_client client;
  187. struct class_device *class_dev;
  188. struct mutex update_lock;
  189. char valid; /* zero until following fields are valid */
  190. unsigned long last_updated; /* in jiffies */
  191. int kind;
  192. /* registers values */
  193. s8 temp8[5]; /* 0: local input
  194. 1: local low limit
  195. 2: local high limit
  196. 3: local critical limit
  197. 4: remote critical limit */
  198. s16 temp11[3]; /* 0: remote input
  199. 1: remote low limit
  200. 2: remote high limit */
  201. u8 temp_hyst;
  202. u8 alarms; /* bitvector */
  203. };
  204. /*
  205. * Sysfs stuff
  206. */
  207. static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
  208. char *buf)
  209. {
  210. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  211. struct lm90_data *data = lm90_update_device(dev);
  212. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index]));
  213. }
  214. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  215. const char *buf, size_t count)
  216. {
  217. static const u8 reg[4] = {
  218. LM90_REG_W_LOCAL_LOW,
  219. LM90_REG_W_LOCAL_HIGH,
  220. LM90_REG_W_LOCAL_CRIT,
  221. LM90_REG_W_REMOTE_CRIT,
  222. };
  223. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  224. struct i2c_client *client = to_i2c_client(dev);
  225. struct lm90_data *data = i2c_get_clientdata(client);
  226. long val = simple_strtol(buf, NULL, 10);
  227. int nr = attr->index;
  228. mutex_lock(&data->update_lock);
  229. if (data->kind == adt7461)
  230. data->temp8[nr] = TEMP1_TO_REG_ADT7461(val);
  231. else
  232. data->temp8[nr] = TEMP1_TO_REG(val);
  233. i2c_smbus_write_byte_data(client, reg[nr - 1], data->temp8[nr]);
  234. mutex_unlock(&data->update_lock);
  235. return count;
  236. }
  237. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  238. char *buf)
  239. {
  240. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  241. struct lm90_data *data = lm90_update_device(dev);
  242. return sprintf(buf, "%d\n", TEMP2_FROM_REG(data->temp11[attr->index]));
  243. }
  244. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  245. const char *buf, size_t count)
  246. {
  247. static const u8 reg[4] = {
  248. LM90_REG_W_REMOTE_LOWH,
  249. LM90_REG_W_REMOTE_LOWL,
  250. LM90_REG_W_REMOTE_HIGHH,
  251. LM90_REG_W_REMOTE_HIGHL,
  252. };
  253. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  254. struct i2c_client *client = to_i2c_client(dev);
  255. struct lm90_data *data = i2c_get_clientdata(client);
  256. long val = simple_strtol(buf, NULL, 10);
  257. int nr = attr->index;
  258. mutex_lock(&data->update_lock);
  259. if (data->kind == adt7461)
  260. data->temp11[nr] = TEMP2_TO_REG_ADT7461(val);
  261. else
  262. data->temp11[nr] = TEMP2_TO_REG(val);
  263. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  264. data->temp11[nr] >> 8);
  265. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  266. data->temp11[nr] & 0xff);
  267. mutex_unlock(&data->update_lock);
  268. return count;
  269. }
  270. static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
  271. char *buf)
  272. {
  273. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  274. struct lm90_data *data = lm90_update_device(dev);
  275. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index])
  276. - TEMP1_FROM_REG(data->temp_hyst));
  277. }
  278. static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
  279. const char *buf, size_t count)
  280. {
  281. struct i2c_client *client = to_i2c_client(dev);
  282. struct lm90_data *data = i2c_get_clientdata(client);
  283. long val = simple_strtol(buf, NULL, 10);
  284. long hyst;
  285. mutex_lock(&data->update_lock);
  286. hyst = TEMP1_FROM_REG(data->temp8[3]) - val;
  287. i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
  288. HYST_TO_REG(hyst));
  289. mutex_unlock(&data->update_lock);
  290. return count;
  291. }
  292. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  293. char *buf)
  294. {
  295. struct lm90_data *data = lm90_update_device(dev);
  296. return sprintf(buf, "%d\n", data->alarms);
  297. }
  298. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
  299. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  300. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
  301. set_temp8, 1);
  302. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  303. set_temp11, 1);
  304. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
  305. set_temp8, 2);
  306. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  307. set_temp11, 2);
  308. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
  309. set_temp8, 3);
  310. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
  311. set_temp8, 4);
  312. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
  313. set_temphyst, 3);
  314. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 4);
  315. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  316. /* pec used for ADM1032 only */
  317. static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
  318. char *buf)
  319. {
  320. struct i2c_client *client = to_i2c_client(dev);
  321. return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
  322. }
  323. static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
  324. const char *buf, size_t count)
  325. {
  326. struct i2c_client *client = to_i2c_client(dev);
  327. long val = simple_strtol(buf, NULL, 10);
  328. switch (val) {
  329. case 0:
  330. client->flags &= ~I2C_CLIENT_PEC;
  331. break;
  332. case 1:
  333. client->flags |= I2C_CLIENT_PEC;
  334. break;
  335. default:
  336. return -EINVAL;
  337. }
  338. return count;
  339. }
  340. static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
  341. /*
  342. * Real code
  343. */
  344. /* The ADM1032 supports PEC but not on write byte transactions, so we need
  345. to explicitely ask for a transaction without PEC. */
  346. static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
  347. {
  348. return i2c_smbus_xfer(client->adapter, client->addr,
  349. client->flags & ~I2C_CLIENT_PEC,
  350. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  351. }
  352. /* It is assumed that client->update_lock is held (unless we are in
  353. detection or initialization steps). This matters when PEC is enabled,
  354. because we don't want the address pointer to change between the write
  355. byte and the read byte transactions. */
  356. static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
  357. {
  358. int err;
  359. if (client->flags & I2C_CLIENT_PEC) {
  360. err = adm1032_write_byte(client, reg);
  361. if (err >= 0)
  362. err = i2c_smbus_read_byte(client);
  363. } else
  364. err = i2c_smbus_read_byte_data(client, reg);
  365. if (err < 0) {
  366. dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
  367. reg, err);
  368. return err;
  369. }
  370. *value = err;
  371. return 0;
  372. }
  373. static int lm90_attach_adapter(struct i2c_adapter *adapter)
  374. {
  375. if (!(adapter->class & I2C_CLASS_HWMON))
  376. return 0;
  377. return i2c_probe(adapter, &addr_data, lm90_detect);
  378. }
  379. /*
  380. * The following function does more than just detection. If detection
  381. * succeeds, it also registers the new chip.
  382. */
  383. static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
  384. {
  385. struct i2c_client *new_client;
  386. struct lm90_data *data;
  387. int err = 0;
  388. const char *name = "";
  389. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  390. goto exit;
  391. if (!(data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
  392. err = -ENOMEM;
  393. goto exit;
  394. }
  395. /* The common I2C client data is placed right before the
  396. LM90-specific data. */
  397. new_client = &data->client;
  398. i2c_set_clientdata(new_client, data);
  399. new_client->addr = address;
  400. new_client->adapter = adapter;
  401. new_client->driver = &lm90_driver;
  402. new_client->flags = 0;
  403. /*
  404. * Now we do the remaining detection. A negative kind means that
  405. * the driver was loaded with no force parameter (default), so we
  406. * must both detect and identify the chip. A zero kind means that
  407. * the driver was loaded with the force parameter, the detection
  408. * step shall be skipped. A positive kind means that the driver
  409. * was loaded with the force parameter and a given kind of chip is
  410. * requested, so both the detection and the identification steps
  411. * are skipped.
  412. */
  413. /* Default to an LM90 if forced */
  414. if (kind == 0)
  415. kind = lm90;
  416. if (kind < 0) { /* detection and identification */
  417. u8 man_id, chip_id, reg_config1, reg_convrate;
  418. if (lm90_read_reg(new_client, LM90_REG_R_MAN_ID,
  419. &man_id) < 0
  420. || lm90_read_reg(new_client, LM90_REG_R_CHIP_ID,
  421. &chip_id) < 0
  422. || lm90_read_reg(new_client, LM90_REG_R_CONFIG1,
  423. &reg_config1) < 0
  424. || lm90_read_reg(new_client, LM90_REG_R_CONVRATE,
  425. &reg_convrate) < 0)
  426. goto exit_free;
  427. if (man_id == 0x01) { /* National Semiconductor */
  428. u8 reg_config2;
  429. if (lm90_read_reg(new_client, LM90_REG_R_CONFIG2,
  430. &reg_config2) < 0)
  431. goto exit_free;
  432. if ((reg_config1 & 0x2A) == 0x00
  433. && (reg_config2 & 0xF8) == 0x00
  434. && reg_convrate <= 0x09) {
  435. if (address == 0x4C
  436. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  437. kind = lm90;
  438. } else
  439. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  440. kind = lm99;
  441. } else
  442. if (address == 0x4C
  443. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  444. kind = lm86;
  445. }
  446. }
  447. } else
  448. if (man_id == 0x41) { /* Analog Devices */
  449. if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
  450. && (reg_config1 & 0x3F) == 0x00
  451. && reg_convrate <= 0x0A) {
  452. kind = adm1032;
  453. } else
  454. if (chip_id == 0x51 /* ADT7461 */
  455. && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
  456. && reg_convrate <= 0x0A) {
  457. kind = adt7461;
  458. }
  459. } else
  460. if (man_id == 0x4D) { /* Maxim */
  461. /*
  462. * The Maxim variants do NOT have a chip_id register.
  463. * Reading from that address will return the last read
  464. * value, which in our case is those of the man_id
  465. * register. Likewise, the config1 register seems to
  466. * lack a low nibble, so the value will be those of the
  467. * previous read, so in our case those of the man_id
  468. * register.
  469. */
  470. if (chip_id == man_id
  471. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  472. && reg_convrate <= 0x09) {
  473. kind = max6657;
  474. }
  475. }
  476. if (kind <= 0) { /* identification failed */
  477. dev_info(&adapter->dev,
  478. "Unsupported chip (man_id=0x%02X, "
  479. "chip_id=0x%02X).\n", man_id, chip_id);
  480. goto exit_free;
  481. }
  482. }
  483. if (kind == lm90) {
  484. name = "lm90";
  485. } else if (kind == adm1032) {
  486. name = "adm1032";
  487. /* The ADM1032 supports PEC, but only if combined
  488. transactions are not used. */
  489. if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  490. new_client->flags |= I2C_CLIENT_PEC;
  491. } else if (kind == lm99) {
  492. name = "lm99";
  493. } else if (kind == lm86) {
  494. name = "lm86";
  495. } else if (kind == max6657) {
  496. name = "max6657";
  497. } else if (kind == adt7461) {
  498. name = "adt7461";
  499. }
  500. /* We can fill in the remaining client fields */
  501. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  502. data->valid = 0;
  503. data->kind = kind;
  504. mutex_init(&data->update_lock);
  505. /* Tell the I2C layer a new client has arrived */
  506. if ((err = i2c_attach_client(new_client)))
  507. goto exit_free;
  508. /* Initialize the LM90 chip */
  509. lm90_init_client(new_client);
  510. /* Register sysfs hooks */
  511. data->class_dev = hwmon_device_register(&new_client->dev);
  512. if (IS_ERR(data->class_dev)) {
  513. err = PTR_ERR(data->class_dev);
  514. goto exit_detach;
  515. }
  516. device_create_file(&new_client->dev,
  517. &sensor_dev_attr_temp1_input.dev_attr);
  518. device_create_file(&new_client->dev,
  519. &sensor_dev_attr_temp2_input.dev_attr);
  520. device_create_file(&new_client->dev,
  521. &sensor_dev_attr_temp1_min.dev_attr);
  522. device_create_file(&new_client->dev,
  523. &sensor_dev_attr_temp2_min.dev_attr);
  524. device_create_file(&new_client->dev,
  525. &sensor_dev_attr_temp1_max.dev_attr);
  526. device_create_file(&new_client->dev,
  527. &sensor_dev_attr_temp2_max.dev_attr);
  528. device_create_file(&new_client->dev,
  529. &sensor_dev_attr_temp1_crit.dev_attr);
  530. device_create_file(&new_client->dev,
  531. &sensor_dev_attr_temp2_crit.dev_attr);
  532. device_create_file(&new_client->dev,
  533. &sensor_dev_attr_temp1_crit_hyst.dev_attr);
  534. device_create_file(&new_client->dev,
  535. &sensor_dev_attr_temp2_crit_hyst.dev_attr);
  536. device_create_file(&new_client->dev, &dev_attr_alarms);
  537. if (new_client->flags & I2C_CLIENT_PEC)
  538. device_create_file(&new_client->dev, &dev_attr_pec);
  539. return 0;
  540. exit_detach:
  541. i2c_detach_client(new_client);
  542. exit_free:
  543. kfree(data);
  544. exit:
  545. return err;
  546. }
  547. static void lm90_init_client(struct i2c_client *client)
  548. {
  549. u8 config;
  550. /*
  551. * Start the conversions.
  552. */
  553. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  554. 5); /* 2 Hz */
  555. if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
  556. dev_warn(&client->dev, "Initialization failed!\n");
  557. return;
  558. }
  559. if (config & 0x40)
  560. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  561. config & 0xBF); /* run */
  562. }
  563. static int lm90_detach_client(struct i2c_client *client)
  564. {
  565. struct lm90_data *data = i2c_get_clientdata(client);
  566. int err;
  567. hwmon_device_unregister(data->class_dev);
  568. if ((err = i2c_detach_client(client)))
  569. return err;
  570. kfree(data);
  571. return 0;
  572. }
  573. static struct lm90_data *lm90_update_device(struct device *dev)
  574. {
  575. struct i2c_client *client = to_i2c_client(dev);
  576. struct lm90_data *data = i2c_get_clientdata(client);
  577. mutex_lock(&data->update_lock);
  578. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  579. u8 oldh, newh, l;
  580. dev_dbg(&client->dev, "Updating lm90 data.\n");
  581. lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, &data->temp8[0]);
  582. lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[1]);
  583. lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[2]);
  584. lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[3]);
  585. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[4]);
  586. lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
  587. /*
  588. * There is a trick here. We have to read two registers to
  589. * have the remote sensor temperature, but we have to beware
  590. * a conversion could occur inbetween the readings. The
  591. * datasheet says we should either use the one-shot
  592. * conversion register, which we don't want to do (disables
  593. * hardware monitoring) or monitor the busy bit, which is
  594. * impossible (we can't read the values and monitor that bit
  595. * at the exact same time). So the solution used here is to
  596. * read the high byte once, then the low byte, then the high
  597. * byte again. If the new high byte matches the old one,
  598. * then we have a valid reading. Else we have to read the low
  599. * byte again, and now we believe we have a correct reading.
  600. */
  601. if (lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &oldh) == 0
  602. && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0
  603. && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &newh) == 0
  604. && (newh == oldh
  605. || lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0))
  606. data->temp11[0] = (newh << 8) | l;
  607. if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &newh) == 0
  608. && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, &l) == 0)
  609. data->temp11[1] = (newh << 8) | l;
  610. if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &newh) == 0
  611. && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, &l) == 0)
  612. data->temp11[2] = (newh << 8) | l;
  613. lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
  614. data->last_updated = jiffies;
  615. data->valid = 1;
  616. }
  617. mutex_unlock(&data->update_lock);
  618. return data;
  619. }
  620. static int __init sensors_lm90_init(void)
  621. {
  622. return i2c_add_driver(&lm90_driver);
  623. }
  624. static void __exit sensors_lm90_exit(void)
  625. {
  626. i2c_del_driver(&lm90_driver);
  627. }
  628. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  629. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  630. MODULE_LICENSE("GPL");
  631. module_init(sensors_lm90_init);
  632. module_exit(sensors_lm90_exit);