lm90.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2006 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. These chips lack the remote temperature
  45. * offset feature.
  46. *
  47. * This driver also supports the MAX6680 and MAX6681, two other sensor
  48. * chips made by Maxim. These are quite similar to the other Maxim
  49. * chips. Complete datasheet can be obtained at:
  50. * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3370
  51. * The MAX6680 and MAX6681 only differ in the pinout so they can be
  52. * treated identically.
  53. *
  54. * This driver also supports the ADT7461 chip from Analog Devices but
  55. * only in its "compatability mode". If an ADT7461 chip is found but
  56. * is configured in non-compatible mode (where its temperature
  57. * register values are decoded differently) it is ignored by this
  58. * driver. Complete datasheet can be obtained from Analog's website
  59. * at:
  60. * http://www.analog.com/en/prod/0,2877,ADT7461,00.html
  61. *
  62. * Since the LM90 was the first chipset supported by this driver, most
  63. * comments will refer to this chipset, but are actually general and
  64. * concern all supported chipsets, unless mentioned otherwise.
  65. *
  66. * This program is free software; you can redistribute it and/or modify
  67. * it under the terms of the GNU General Public License as published by
  68. * the Free Software Foundation; either version 2 of the License, or
  69. * (at your option) any later version.
  70. *
  71. * This program is distributed in the hope that it will be useful,
  72. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  73. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  74. * GNU General Public License for more details.
  75. *
  76. * You should have received a copy of the GNU General Public License
  77. * along with this program; if not, write to the Free Software
  78. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  79. */
  80. #include <linux/module.h>
  81. #include <linux/init.h>
  82. #include <linux/slab.h>
  83. #include <linux/jiffies.h>
  84. #include <linux/i2c.h>
  85. #include <linux/hwmon-sysfs.h>
  86. #include <linux/hwmon.h>
  87. #include <linux/err.h>
  88. #include <linux/mutex.h>
  89. #include <linux/sysfs.h>
  90. /*
  91. * Addresses to scan
  92. * Address is fully defined internally and cannot be changed except for
  93. * MAX6659, MAX6680 and MAX6681.
  94. * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
  95. * have address 0x4c.
  96. * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
  97. * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
  98. * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
  99. * 0x4c, 0x4d or 0x4e.
  100. */
  101. static const unsigned short normal_i2c[] = {
  102. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  103. /*
  104. * Insmod parameters
  105. */
  106. I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680);
  107. /*
  108. * The LM90 registers
  109. */
  110. #define LM90_REG_R_MAN_ID 0xFE
  111. #define LM90_REG_R_CHIP_ID 0xFF
  112. #define LM90_REG_R_CONFIG1 0x03
  113. #define LM90_REG_W_CONFIG1 0x09
  114. #define LM90_REG_R_CONFIG2 0xBF
  115. #define LM90_REG_W_CONFIG2 0xBF
  116. #define LM90_REG_R_CONVRATE 0x04
  117. #define LM90_REG_W_CONVRATE 0x0A
  118. #define LM90_REG_R_STATUS 0x02
  119. #define LM90_REG_R_LOCAL_TEMP 0x00
  120. #define LM90_REG_R_LOCAL_HIGH 0x05
  121. #define LM90_REG_W_LOCAL_HIGH 0x0B
  122. #define LM90_REG_R_LOCAL_LOW 0x06
  123. #define LM90_REG_W_LOCAL_LOW 0x0C
  124. #define LM90_REG_R_LOCAL_CRIT 0x20
  125. #define LM90_REG_W_LOCAL_CRIT 0x20
  126. #define LM90_REG_R_REMOTE_TEMPH 0x01
  127. #define LM90_REG_R_REMOTE_TEMPL 0x10
  128. #define LM90_REG_R_REMOTE_OFFSH 0x11
  129. #define LM90_REG_W_REMOTE_OFFSH 0x11
  130. #define LM90_REG_R_REMOTE_OFFSL 0x12
  131. #define LM90_REG_W_REMOTE_OFFSL 0x12
  132. #define LM90_REG_R_REMOTE_HIGHH 0x07
  133. #define LM90_REG_W_REMOTE_HIGHH 0x0D
  134. #define LM90_REG_R_REMOTE_HIGHL 0x13
  135. #define LM90_REG_W_REMOTE_HIGHL 0x13
  136. #define LM90_REG_R_REMOTE_LOWH 0x08
  137. #define LM90_REG_W_REMOTE_LOWH 0x0E
  138. #define LM90_REG_R_REMOTE_LOWL 0x14
  139. #define LM90_REG_W_REMOTE_LOWL 0x14
  140. #define LM90_REG_R_REMOTE_CRIT 0x19
  141. #define LM90_REG_W_REMOTE_CRIT 0x19
  142. #define LM90_REG_R_TCRIT_HYST 0x21
  143. #define LM90_REG_W_TCRIT_HYST 0x21
  144. /*
  145. * Conversions and various macros
  146. * For local temperatures and limits, critical limits and the hysteresis
  147. * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
  148. * For remote temperatures and limits, it uses signed 11-bit values with
  149. * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
  150. */
  151. #define TEMP1_FROM_REG(val) ((val) * 1000)
  152. #define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
  153. (val) >= 127000 ? 127 : \
  154. (val) < 0 ? ((val) - 500) / 1000 : \
  155. ((val) + 500) / 1000)
  156. #define TEMP2_FROM_REG(val) ((val) / 32 * 125)
  157. #define TEMP2_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  158. (val) >= 127875 ? 0x7FE0 : \
  159. (val) < 0 ? ((val) - 62) / 125 * 32 : \
  160. ((val) + 62) / 125 * 32)
  161. #define HYST_TO_REG(val) ((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
  162. ((val) + 500) / 1000)
  163. /*
  164. * ADT7461 is almost identical to LM90 except that attempts to write
  165. * values that are outside the range 0 < temp < 127 are treated as
  166. * the boundary value.
  167. */
  168. #define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
  169. (val) >= 127000 ? 127 : \
  170. ((val) + 500) / 1000)
  171. #define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
  172. (val) >= 127750 ? 0x7FC0 : \
  173. ((val) + 125) / 250 * 64)
  174. /*
  175. * Functions declaration
  176. */
  177. static int lm90_detect(struct i2c_client *client, int kind,
  178. struct i2c_board_info *info);
  179. static int lm90_probe(struct i2c_client *client,
  180. const struct i2c_device_id *id);
  181. static void lm90_init_client(struct i2c_client *client);
  182. static int lm90_remove(struct i2c_client *client);
  183. static struct lm90_data *lm90_update_device(struct device *dev);
  184. /*
  185. * Driver data (common to all clients)
  186. */
  187. static const struct i2c_device_id lm90_id[] = {
  188. { "adm1032", adm1032 },
  189. { "adt7461", adt7461 },
  190. { "lm90", lm90 },
  191. { "lm86", lm86 },
  192. { "lm89", lm99 },
  193. { "lm99", lm99 }, /* Missing temperature offset */
  194. { "max6657", max6657 },
  195. { "max6658", max6657 },
  196. { "max6659", max6657 },
  197. { "max6680", max6680 },
  198. { "max6681", max6680 },
  199. { }
  200. };
  201. MODULE_DEVICE_TABLE(i2c, lm90_id);
  202. static struct i2c_driver lm90_driver = {
  203. .class = I2C_CLASS_HWMON,
  204. .driver = {
  205. .name = "lm90",
  206. },
  207. .probe = lm90_probe,
  208. .remove = lm90_remove,
  209. .id_table = lm90_id,
  210. .detect = lm90_detect,
  211. .address_data = &addr_data,
  212. };
  213. /*
  214. * Client data (each client gets its own)
  215. */
  216. struct lm90_data {
  217. struct device *hwmon_dev;
  218. struct mutex update_lock;
  219. char valid; /* zero until following fields are valid */
  220. unsigned long last_updated; /* in jiffies */
  221. int kind;
  222. /* registers values */
  223. s8 temp8[5]; /* 0: local input
  224. 1: local low limit
  225. 2: local high limit
  226. 3: local critical limit
  227. 4: remote critical limit */
  228. s16 temp11[4]; /* 0: remote input
  229. 1: remote low limit
  230. 2: remote high limit
  231. 3: remote offset (except max6657) */
  232. u8 temp_hyst;
  233. u8 alarms; /* bitvector */
  234. };
  235. /*
  236. * Sysfs stuff
  237. */
  238. static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
  239. char *buf)
  240. {
  241. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  242. struct lm90_data *data = lm90_update_device(dev);
  243. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index]));
  244. }
  245. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  246. const char *buf, size_t count)
  247. {
  248. static const u8 reg[4] = {
  249. LM90_REG_W_LOCAL_LOW,
  250. LM90_REG_W_LOCAL_HIGH,
  251. LM90_REG_W_LOCAL_CRIT,
  252. LM90_REG_W_REMOTE_CRIT,
  253. };
  254. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  255. struct i2c_client *client = to_i2c_client(dev);
  256. struct lm90_data *data = i2c_get_clientdata(client);
  257. long val = simple_strtol(buf, NULL, 10);
  258. int nr = attr->index;
  259. mutex_lock(&data->update_lock);
  260. if (data->kind == adt7461)
  261. data->temp8[nr] = TEMP1_TO_REG_ADT7461(val);
  262. else
  263. data->temp8[nr] = TEMP1_TO_REG(val);
  264. i2c_smbus_write_byte_data(client, reg[nr - 1], data->temp8[nr]);
  265. mutex_unlock(&data->update_lock);
  266. return count;
  267. }
  268. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  269. char *buf)
  270. {
  271. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  272. struct lm90_data *data = lm90_update_device(dev);
  273. return sprintf(buf, "%d\n", TEMP2_FROM_REG(data->temp11[attr->index]));
  274. }
  275. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  276. const char *buf, size_t count)
  277. {
  278. static const u8 reg[6] = {
  279. LM90_REG_W_REMOTE_LOWH,
  280. LM90_REG_W_REMOTE_LOWL,
  281. LM90_REG_W_REMOTE_HIGHH,
  282. LM90_REG_W_REMOTE_HIGHL,
  283. LM90_REG_W_REMOTE_OFFSH,
  284. LM90_REG_W_REMOTE_OFFSL,
  285. };
  286. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  287. struct i2c_client *client = to_i2c_client(dev);
  288. struct lm90_data *data = i2c_get_clientdata(client);
  289. long val = simple_strtol(buf, NULL, 10);
  290. int nr = attr->index;
  291. mutex_lock(&data->update_lock);
  292. if (data->kind == adt7461)
  293. data->temp11[nr] = TEMP2_TO_REG_ADT7461(val);
  294. else
  295. data->temp11[nr] = TEMP2_TO_REG(val);
  296. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  297. data->temp11[nr] >> 8);
  298. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  299. data->temp11[nr] & 0xff);
  300. mutex_unlock(&data->update_lock);
  301. return count;
  302. }
  303. static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
  304. char *buf)
  305. {
  306. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  307. struct lm90_data *data = lm90_update_device(dev);
  308. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index])
  309. - TEMP1_FROM_REG(data->temp_hyst));
  310. }
  311. static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
  312. const char *buf, size_t count)
  313. {
  314. struct i2c_client *client = to_i2c_client(dev);
  315. struct lm90_data *data = i2c_get_clientdata(client);
  316. long val = simple_strtol(buf, NULL, 10);
  317. long hyst;
  318. mutex_lock(&data->update_lock);
  319. hyst = TEMP1_FROM_REG(data->temp8[3]) - val;
  320. i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
  321. HYST_TO_REG(hyst));
  322. mutex_unlock(&data->update_lock);
  323. return count;
  324. }
  325. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  326. char *buf)
  327. {
  328. struct lm90_data *data = lm90_update_device(dev);
  329. return sprintf(buf, "%d\n", data->alarms);
  330. }
  331. static ssize_t show_alarm(struct device *dev, struct device_attribute
  332. *devattr, char *buf)
  333. {
  334. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  335. struct lm90_data *data = lm90_update_device(dev);
  336. int bitnr = attr->index;
  337. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  338. }
  339. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
  340. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  341. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
  342. set_temp8, 1);
  343. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  344. set_temp11, 1);
  345. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
  346. set_temp8, 2);
  347. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  348. set_temp11, 2);
  349. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
  350. set_temp8, 3);
  351. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
  352. set_temp8, 4);
  353. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
  354. set_temphyst, 3);
  355. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 4);
  356. static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
  357. set_temp11, 3);
  358. /* Individual alarm files */
  359. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  360. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  361. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  362. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  363. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  364. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  365. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  366. /* Raw alarm file for compatibility */
  367. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  368. static struct attribute *lm90_attributes[] = {
  369. &sensor_dev_attr_temp1_input.dev_attr.attr,
  370. &sensor_dev_attr_temp2_input.dev_attr.attr,
  371. &sensor_dev_attr_temp1_min.dev_attr.attr,
  372. &sensor_dev_attr_temp2_min.dev_attr.attr,
  373. &sensor_dev_attr_temp1_max.dev_attr.attr,
  374. &sensor_dev_attr_temp2_max.dev_attr.attr,
  375. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  376. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  377. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  378. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  379. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  380. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  381. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  382. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  383. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  384. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  385. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  386. &dev_attr_alarms.attr,
  387. NULL
  388. };
  389. static const struct attribute_group lm90_group = {
  390. .attrs = lm90_attributes,
  391. };
  392. /* pec used for ADM1032 only */
  393. static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
  394. char *buf)
  395. {
  396. struct i2c_client *client = to_i2c_client(dev);
  397. return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
  398. }
  399. static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
  400. const char *buf, size_t count)
  401. {
  402. struct i2c_client *client = to_i2c_client(dev);
  403. long val = simple_strtol(buf, NULL, 10);
  404. switch (val) {
  405. case 0:
  406. client->flags &= ~I2C_CLIENT_PEC;
  407. break;
  408. case 1:
  409. client->flags |= I2C_CLIENT_PEC;
  410. break;
  411. default:
  412. return -EINVAL;
  413. }
  414. return count;
  415. }
  416. static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
  417. /*
  418. * Real code
  419. */
  420. /* The ADM1032 supports PEC but not on write byte transactions, so we need
  421. to explicitly ask for a transaction without PEC. */
  422. static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
  423. {
  424. return i2c_smbus_xfer(client->adapter, client->addr,
  425. client->flags & ~I2C_CLIENT_PEC,
  426. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  427. }
  428. /* It is assumed that client->update_lock is held (unless we are in
  429. detection or initialization steps). This matters when PEC is enabled,
  430. because we don't want the address pointer to change between the write
  431. byte and the read byte transactions. */
  432. static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
  433. {
  434. int err;
  435. if (client->flags & I2C_CLIENT_PEC) {
  436. err = adm1032_write_byte(client, reg);
  437. if (err >= 0)
  438. err = i2c_smbus_read_byte(client);
  439. } else
  440. err = i2c_smbus_read_byte_data(client, reg);
  441. if (err < 0) {
  442. dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
  443. reg, err);
  444. return err;
  445. }
  446. *value = err;
  447. return 0;
  448. }
  449. /* Return 0 if detection is successful, -ENODEV otherwise */
  450. static int lm90_detect(struct i2c_client *new_client, int kind,
  451. struct i2c_board_info *info)
  452. {
  453. struct i2c_adapter *adapter = new_client->adapter;
  454. int address = new_client->addr;
  455. const char *name = "";
  456. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  457. return -ENODEV;
  458. /*
  459. * Now we do the remaining detection. A negative kind means that
  460. * the driver was loaded with no force parameter (default), so we
  461. * must both detect and identify the chip. A zero kind means that
  462. * the driver was loaded with the force parameter, the detection
  463. * step shall be skipped. A positive kind means that the driver
  464. * was loaded with the force parameter and a given kind of chip is
  465. * requested, so both the detection and the identification steps
  466. * are skipped.
  467. */
  468. /* Default to an LM90 if forced */
  469. if (kind == 0)
  470. kind = lm90;
  471. if (kind < 0) { /* detection and identification */
  472. int man_id, chip_id, reg_config1, reg_convrate;
  473. if ((man_id = i2c_smbus_read_byte_data(new_client,
  474. LM90_REG_R_MAN_ID)) < 0
  475. || (chip_id = i2c_smbus_read_byte_data(new_client,
  476. LM90_REG_R_CHIP_ID)) < 0
  477. || (reg_config1 = i2c_smbus_read_byte_data(new_client,
  478. LM90_REG_R_CONFIG1)) < 0
  479. || (reg_convrate = i2c_smbus_read_byte_data(new_client,
  480. LM90_REG_R_CONVRATE)) < 0)
  481. return -ENODEV;
  482. if ((address == 0x4C || address == 0x4D)
  483. && man_id == 0x01) { /* National Semiconductor */
  484. int reg_config2;
  485. if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
  486. LM90_REG_R_CONFIG2)) < 0)
  487. return -ENODEV;
  488. if ((reg_config1 & 0x2A) == 0x00
  489. && (reg_config2 & 0xF8) == 0x00
  490. && reg_convrate <= 0x09) {
  491. if (address == 0x4C
  492. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  493. kind = lm90;
  494. } else
  495. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  496. kind = lm99;
  497. } else
  498. if (address == 0x4C
  499. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  500. kind = lm86;
  501. }
  502. }
  503. } else
  504. if ((address == 0x4C || address == 0x4D)
  505. && man_id == 0x41) { /* Analog Devices */
  506. if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
  507. && (reg_config1 & 0x3F) == 0x00
  508. && reg_convrate <= 0x0A) {
  509. kind = adm1032;
  510. } else
  511. if (chip_id == 0x51 /* ADT7461 */
  512. && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
  513. && reg_convrate <= 0x0A) {
  514. kind = adt7461;
  515. }
  516. } else
  517. if (man_id == 0x4D) { /* Maxim */
  518. /*
  519. * The MAX6657, MAX6658 and MAX6659 do NOT have a
  520. * chip_id register. Reading from that address will
  521. * return the last read value, which in our case is
  522. * those of the man_id register. Likewise, the config1
  523. * register seems to lack a low nibble, so the value
  524. * will be those of the previous read, so in our case
  525. * those of the man_id register.
  526. */
  527. if (chip_id == man_id
  528. && (address == 0x4C || address == 0x4D)
  529. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  530. && reg_convrate <= 0x09) {
  531. kind = max6657;
  532. } else
  533. /* The chip_id register of the MAX6680 and MAX6681
  534. * holds the revision of the chip.
  535. * the lowest bit of the config1 register is unused
  536. * and should return zero when read, so should the
  537. * second to last bit of config1 (software reset)
  538. */
  539. if (chip_id == 0x01
  540. && (reg_config1 & 0x03) == 0x00
  541. && reg_convrate <= 0x07) {
  542. kind = max6680;
  543. }
  544. }
  545. if (kind <= 0) { /* identification failed */
  546. dev_info(&adapter->dev,
  547. "Unsupported chip (man_id=0x%02X, "
  548. "chip_id=0x%02X).\n", man_id, chip_id);
  549. return -ENODEV;
  550. }
  551. }
  552. /* Fill the i2c board info */
  553. if (kind == lm90) {
  554. name = "lm90";
  555. } else if (kind == adm1032) {
  556. name = "adm1032";
  557. /* The ADM1032 supports PEC, but only if combined
  558. transactions are not used. */
  559. if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  560. info->flags |= I2C_CLIENT_PEC;
  561. } else if (kind == lm99) {
  562. name = "lm99";
  563. } else if (kind == lm86) {
  564. name = "lm86";
  565. } else if (kind == max6657) {
  566. name = "max6657";
  567. } else if (kind == max6680) {
  568. name = "max6680";
  569. } else if (kind == adt7461) {
  570. name = "adt7461";
  571. }
  572. strlcpy(info->type, name, I2C_NAME_SIZE);
  573. return 0;
  574. }
  575. static int lm90_probe(struct i2c_client *new_client,
  576. const struct i2c_device_id *id)
  577. {
  578. struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
  579. struct lm90_data *data;
  580. int err;
  581. data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
  582. if (!data) {
  583. err = -ENOMEM;
  584. goto exit;
  585. }
  586. i2c_set_clientdata(new_client, data);
  587. mutex_init(&data->update_lock);
  588. /* Set the device type */
  589. data->kind = id->driver_data;
  590. if (data->kind == adm1032) {
  591. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  592. new_client->flags &= ~I2C_CLIENT_PEC;
  593. }
  594. /* Initialize the LM90 chip */
  595. lm90_init_client(new_client);
  596. /* Register sysfs hooks */
  597. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
  598. goto exit_free;
  599. if (new_client->flags & I2C_CLIENT_PEC) {
  600. if ((err = device_create_file(&new_client->dev,
  601. &dev_attr_pec)))
  602. goto exit_remove_files;
  603. }
  604. if (data->kind != max6657) {
  605. if ((err = device_create_file(&new_client->dev,
  606. &sensor_dev_attr_temp2_offset.dev_attr)))
  607. goto exit_remove_files;
  608. }
  609. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  610. if (IS_ERR(data->hwmon_dev)) {
  611. err = PTR_ERR(data->hwmon_dev);
  612. goto exit_remove_files;
  613. }
  614. return 0;
  615. exit_remove_files:
  616. sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
  617. device_remove_file(&new_client->dev, &dev_attr_pec);
  618. exit_free:
  619. kfree(data);
  620. exit:
  621. return err;
  622. }
  623. static void lm90_init_client(struct i2c_client *client)
  624. {
  625. u8 config, config_orig;
  626. struct lm90_data *data = i2c_get_clientdata(client);
  627. /*
  628. * Start the conversions.
  629. */
  630. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  631. 5); /* 2 Hz */
  632. if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
  633. dev_warn(&client->dev, "Initialization failed!\n");
  634. return;
  635. }
  636. config_orig = config;
  637. /*
  638. * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
  639. * 0.125 degree resolution) and range (0x08, extend range
  640. * to -64 degree) mode for the remote temperature sensor.
  641. */
  642. if (data->kind == max6680) {
  643. config |= 0x18;
  644. }
  645. config &= 0xBF; /* run */
  646. if (config != config_orig) /* Only write if changed */
  647. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
  648. }
  649. static int lm90_remove(struct i2c_client *client)
  650. {
  651. struct lm90_data *data = i2c_get_clientdata(client);
  652. hwmon_device_unregister(data->hwmon_dev);
  653. sysfs_remove_group(&client->dev.kobj, &lm90_group);
  654. device_remove_file(&client->dev, &dev_attr_pec);
  655. if (data->kind != max6657)
  656. device_remove_file(&client->dev,
  657. &sensor_dev_attr_temp2_offset.dev_attr);
  658. kfree(data);
  659. return 0;
  660. }
  661. static struct lm90_data *lm90_update_device(struct device *dev)
  662. {
  663. struct i2c_client *client = to_i2c_client(dev);
  664. struct lm90_data *data = i2c_get_clientdata(client);
  665. mutex_lock(&data->update_lock);
  666. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  667. u8 oldh, newh, l;
  668. dev_dbg(&client->dev, "Updating lm90 data.\n");
  669. lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, &data->temp8[0]);
  670. lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[1]);
  671. lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[2]);
  672. lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[3]);
  673. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[4]);
  674. lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
  675. /*
  676. * There is a trick here. We have to read two registers to
  677. * have the remote sensor temperature, but we have to beware
  678. * a conversion could occur inbetween the readings. The
  679. * datasheet says we should either use the one-shot
  680. * conversion register, which we don't want to do (disables
  681. * hardware monitoring) or monitor the busy bit, which is
  682. * impossible (we can't read the values and monitor that bit
  683. * at the exact same time). So the solution used here is to
  684. * read the high byte once, then the low byte, then the high
  685. * byte again. If the new high byte matches the old one,
  686. * then we have a valid reading. Else we have to read the low
  687. * byte again, and now we believe we have a correct reading.
  688. */
  689. if (lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &oldh) == 0
  690. && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0
  691. && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &newh) == 0
  692. && (newh == oldh
  693. || lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0))
  694. data->temp11[0] = (newh << 8) | l;
  695. if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &newh) == 0
  696. && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, &l) == 0)
  697. data->temp11[1] = (newh << 8) | l;
  698. if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &newh) == 0
  699. && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, &l) == 0)
  700. data->temp11[2] = (newh << 8) | l;
  701. if (data->kind != max6657) {
  702. if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
  703. &newh) == 0
  704. && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
  705. &l) == 0)
  706. data->temp11[3] = (newh << 8) | l;
  707. }
  708. lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
  709. data->last_updated = jiffies;
  710. data->valid = 1;
  711. }
  712. mutex_unlock(&data->update_lock);
  713. return data;
  714. }
  715. static int __init sensors_lm90_init(void)
  716. {
  717. return i2c_add_driver(&lm90_driver);
  718. }
  719. static void __exit sensors_lm90_exit(void)
  720. {
  721. i2c_del_driver(&lm90_driver);
  722. }
  723. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  724. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  725. MODULE_LICENSE("GPL");
  726. module_init(sensors_lm90_init);
  727. module_exit(sensors_lm90_exit);