lm90.c 26 KB

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