lm90.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2010 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.
  16. * Note that there is no way to differentiate between both chips.
  17. * When device is auto-detected, the driver will assume an LM99.
  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 MAX6646, MAX6647, MAX6648, MAX6649 and
  36. * MAX6692 chips made by Maxim. These are again similar to the LM86,
  37. * but they use unsigned temperature values and can report temperatures
  38. * from 0 to 145 degrees.
  39. *
  40. * This driver also supports the MAX6680 and MAX6681, two other sensor
  41. * chips made by Maxim. These are quite similar to the other Maxim
  42. * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
  43. * be treated identically.
  44. *
  45. * This driver also supports the ADT7461 chip from Analog Devices.
  46. * It's supported in both compatibility and extended mode. It is mostly
  47. * compatible with LM90 except for a data format difference for the
  48. * temperature value registers.
  49. *
  50. * Since the LM90 was the first chipset supported by this driver, most
  51. * comments will refer to this chipset, but are actually general and
  52. * concern all supported chipsets, unless mentioned otherwise.
  53. *
  54. * This program is free software; you can redistribute it and/or modify
  55. * it under the terms of the GNU General Public License as published by
  56. * the Free Software Foundation; either version 2 of the License, or
  57. * (at your option) any later version.
  58. *
  59. * This program is distributed in the hope that it will be useful,
  60. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  61. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  62. * GNU General Public License for more details.
  63. *
  64. * You should have received a copy of the GNU General Public License
  65. * along with this program; if not, write to the Free Software
  66. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/init.h>
  70. #include <linux/slab.h>
  71. #include <linux/jiffies.h>
  72. #include <linux/i2c.h>
  73. #include <linux/hwmon-sysfs.h>
  74. #include <linux/hwmon.h>
  75. #include <linux/err.h>
  76. #include <linux/mutex.h>
  77. #include <linux/sysfs.h>
  78. /*
  79. * Addresses to scan
  80. * Address is fully defined internally and cannot be changed except for
  81. * MAX6659, MAX6680 and MAX6681.
  82. * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
  83. * and MAX6658 have address 0x4c.
  84. * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
  85. * MAX6647 has address 0x4e.
  86. * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
  87. * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
  88. * 0x4c, 0x4d or 0x4e.
  89. */
  90. static const unsigned short normal_i2c[] = {
  91. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  92. enum chips { lm90, adm1032, lm99, lm86, max6657, adt7461, max6680, max6646,
  93. w83l771 };
  94. /*
  95. * The LM90 registers
  96. */
  97. #define LM90_REG_R_MAN_ID 0xFE
  98. #define LM90_REG_R_CHIP_ID 0xFF
  99. #define LM90_REG_R_CONFIG1 0x03
  100. #define LM90_REG_W_CONFIG1 0x09
  101. #define LM90_REG_R_CONFIG2 0xBF
  102. #define LM90_REG_W_CONFIG2 0xBF
  103. #define LM90_REG_R_CONVRATE 0x04
  104. #define LM90_REG_W_CONVRATE 0x0A
  105. #define LM90_REG_R_STATUS 0x02
  106. #define LM90_REG_R_LOCAL_TEMP 0x00
  107. #define LM90_REG_R_LOCAL_HIGH 0x05
  108. #define LM90_REG_W_LOCAL_HIGH 0x0B
  109. #define LM90_REG_R_LOCAL_LOW 0x06
  110. #define LM90_REG_W_LOCAL_LOW 0x0C
  111. #define LM90_REG_R_LOCAL_CRIT 0x20
  112. #define LM90_REG_W_LOCAL_CRIT 0x20
  113. #define LM90_REG_R_REMOTE_TEMPH 0x01
  114. #define LM90_REG_R_REMOTE_TEMPL 0x10
  115. #define LM90_REG_R_REMOTE_OFFSH 0x11
  116. #define LM90_REG_W_REMOTE_OFFSH 0x11
  117. #define LM90_REG_R_REMOTE_OFFSL 0x12
  118. #define LM90_REG_W_REMOTE_OFFSL 0x12
  119. #define LM90_REG_R_REMOTE_HIGHH 0x07
  120. #define LM90_REG_W_REMOTE_HIGHH 0x0D
  121. #define LM90_REG_R_REMOTE_HIGHL 0x13
  122. #define LM90_REG_W_REMOTE_HIGHL 0x13
  123. #define LM90_REG_R_REMOTE_LOWH 0x08
  124. #define LM90_REG_W_REMOTE_LOWH 0x0E
  125. #define LM90_REG_R_REMOTE_LOWL 0x14
  126. #define LM90_REG_W_REMOTE_LOWL 0x14
  127. #define LM90_REG_R_REMOTE_CRIT 0x19
  128. #define LM90_REG_W_REMOTE_CRIT 0x19
  129. #define LM90_REG_R_TCRIT_HYST 0x21
  130. #define LM90_REG_W_TCRIT_HYST 0x21
  131. /* MAX6646/6647/6649/6657/6658/6659 registers */
  132. #define MAX6657_REG_R_LOCAL_TEMPL 0x11
  133. /*
  134. * Device flags
  135. */
  136. #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
  137. /* Device features */
  138. #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
  139. #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
  140. #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
  141. /*
  142. * Functions declaration
  143. */
  144. static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
  145. static int lm90_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id);
  147. static void lm90_init_client(struct i2c_client *client);
  148. static void lm90_alert(struct i2c_client *client, unsigned int flag);
  149. static int lm90_remove(struct i2c_client *client);
  150. static struct lm90_data *lm90_update_device(struct device *dev);
  151. /*
  152. * Driver data (common to all clients)
  153. */
  154. static const struct i2c_device_id lm90_id[] = {
  155. { "adm1032", adm1032 },
  156. { "adt7461", adt7461 },
  157. { "lm90", lm90 },
  158. { "lm86", lm86 },
  159. { "lm89", lm86 },
  160. { "lm99", lm99 },
  161. { "max6646", max6646 },
  162. { "max6647", max6646 },
  163. { "max6649", max6646 },
  164. { "max6657", max6657 },
  165. { "max6658", max6657 },
  166. { "max6659", max6657 },
  167. { "max6680", max6680 },
  168. { "max6681", max6680 },
  169. { "w83l771", w83l771 },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(i2c, lm90_id);
  173. static struct i2c_driver lm90_driver = {
  174. .class = I2C_CLASS_HWMON,
  175. .driver = {
  176. .name = "lm90",
  177. },
  178. .probe = lm90_probe,
  179. .remove = lm90_remove,
  180. .alert = lm90_alert,
  181. .id_table = lm90_id,
  182. .detect = lm90_detect,
  183. .address_list = normal_i2c,
  184. };
  185. /*
  186. * Client data (each client gets its own)
  187. */
  188. struct lm90_data {
  189. struct device *hwmon_dev;
  190. struct mutex update_lock;
  191. char valid; /* zero until following fields are valid */
  192. unsigned long last_updated; /* in jiffies */
  193. int kind;
  194. int flags;
  195. u8 config_orig; /* Original configuration register value */
  196. u8 alert_alarms; /* Which alarm bits trigger ALERT# */
  197. /* registers values */
  198. s8 temp8[4]; /* 0: local low limit
  199. 1: local high limit
  200. 2: local critical limit
  201. 3: remote critical limit */
  202. s16 temp11[5]; /* 0: remote input
  203. 1: remote low limit
  204. 2: remote high limit
  205. 3: remote offset (except max6646 and max6657)
  206. 4: local input */
  207. u8 temp_hyst;
  208. u8 alarms; /* bitvector */
  209. };
  210. /*
  211. * Conversions
  212. * For local temperatures and limits, critical limits and the hysteresis
  213. * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
  214. * For remote temperatures and limits, it uses signed 11-bit values with
  215. * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
  216. * Maxim chips use unsigned values.
  217. */
  218. static inline int temp_from_s8(s8 val)
  219. {
  220. return val * 1000;
  221. }
  222. static inline int temp_from_u8(u8 val)
  223. {
  224. return val * 1000;
  225. }
  226. static inline int temp_from_s16(s16 val)
  227. {
  228. return val / 32 * 125;
  229. }
  230. static inline int temp_from_u16(u16 val)
  231. {
  232. return val / 32 * 125;
  233. }
  234. static s8 temp_to_s8(long val)
  235. {
  236. if (val <= -128000)
  237. return -128;
  238. if (val >= 127000)
  239. return 127;
  240. if (val < 0)
  241. return (val - 500) / 1000;
  242. return (val + 500) / 1000;
  243. }
  244. static u8 temp_to_u8(long val)
  245. {
  246. if (val <= 0)
  247. return 0;
  248. if (val >= 255000)
  249. return 255;
  250. return (val + 500) / 1000;
  251. }
  252. static s16 temp_to_s16(long val)
  253. {
  254. if (val <= -128000)
  255. return 0x8000;
  256. if (val >= 127875)
  257. return 0x7FE0;
  258. if (val < 0)
  259. return (val - 62) / 125 * 32;
  260. return (val + 62) / 125 * 32;
  261. }
  262. static u8 hyst_to_reg(long val)
  263. {
  264. if (val <= 0)
  265. return 0;
  266. if (val >= 30500)
  267. return 31;
  268. return (val + 500) / 1000;
  269. }
  270. /*
  271. * ADT7461 in compatibility mode is almost identical to LM90 except that
  272. * attempts to write values that are outside the range 0 < temp < 127 are
  273. * treated as the boundary value.
  274. *
  275. * ADT7461 in "extended mode" operation uses unsigned integers offset by
  276. * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
  277. */
  278. static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
  279. {
  280. if (data->flags & LM90_FLAG_ADT7461_EXT)
  281. return (val - 64) * 1000;
  282. else
  283. return temp_from_s8(val);
  284. }
  285. static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
  286. {
  287. if (data->flags & LM90_FLAG_ADT7461_EXT)
  288. return (val - 0x4000) / 64 * 250;
  289. else
  290. return temp_from_s16(val);
  291. }
  292. static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
  293. {
  294. if (data->flags & LM90_FLAG_ADT7461_EXT) {
  295. if (val <= -64000)
  296. return 0;
  297. if (val >= 191000)
  298. return 0xFF;
  299. return (val + 500 + 64000) / 1000;
  300. } else {
  301. if (val <= 0)
  302. return 0;
  303. if (val >= 127000)
  304. return 127;
  305. return (val + 500) / 1000;
  306. }
  307. }
  308. static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
  309. {
  310. if (data->flags & LM90_FLAG_ADT7461_EXT) {
  311. if (val <= -64000)
  312. return 0;
  313. if (val >= 191750)
  314. return 0xFFC0;
  315. return (val + 64000 + 125) / 250 * 64;
  316. } else {
  317. if (val <= 0)
  318. return 0;
  319. if (val >= 127750)
  320. return 0x7FC0;
  321. return (val + 125) / 250 * 64;
  322. }
  323. }
  324. /*
  325. * Sysfs stuff
  326. */
  327. static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
  328. char *buf)
  329. {
  330. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  331. struct lm90_data *data = lm90_update_device(dev);
  332. int temp;
  333. if (data->kind == adt7461)
  334. temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
  335. else if (data->kind == max6646)
  336. temp = temp_from_u8(data->temp8[attr->index]);
  337. else
  338. temp = temp_from_s8(data->temp8[attr->index]);
  339. /* +16 degrees offset for temp2 for the LM99 */
  340. if (data->kind == lm99 && attr->index == 3)
  341. temp += 16000;
  342. return sprintf(buf, "%d\n", temp);
  343. }
  344. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  345. const char *buf, size_t count)
  346. {
  347. static const u8 reg[4] = {
  348. LM90_REG_W_LOCAL_LOW,
  349. LM90_REG_W_LOCAL_HIGH,
  350. LM90_REG_W_LOCAL_CRIT,
  351. LM90_REG_W_REMOTE_CRIT,
  352. };
  353. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  354. struct i2c_client *client = to_i2c_client(dev);
  355. struct lm90_data *data = i2c_get_clientdata(client);
  356. int nr = attr->index;
  357. long val;
  358. int err;
  359. err = strict_strtol(buf, 10, &val);
  360. if (err < 0)
  361. return err;
  362. /* +16 degrees offset for temp2 for the LM99 */
  363. if (data->kind == lm99 && attr->index == 3)
  364. val -= 16000;
  365. mutex_lock(&data->update_lock);
  366. if (data->kind == adt7461)
  367. data->temp8[nr] = temp_to_u8_adt7461(data, val);
  368. else if (data->kind == max6646)
  369. data->temp8[nr] = temp_to_u8(val);
  370. else
  371. data->temp8[nr] = temp_to_s8(val);
  372. i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
  373. mutex_unlock(&data->update_lock);
  374. return count;
  375. }
  376. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  377. char *buf)
  378. {
  379. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  380. struct lm90_data *data = lm90_update_device(dev);
  381. int temp;
  382. if (data->kind == adt7461)
  383. temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
  384. else if (data->kind == max6646)
  385. temp = temp_from_u16(data->temp11[attr->index]);
  386. else
  387. temp = temp_from_s16(data->temp11[attr->index]);
  388. /* +16 degrees offset for temp2 for the LM99 */
  389. if (data->kind == lm99 && attr->index <= 2)
  390. temp += 16000;
  391. return sprintf(buf, "%d\n", temp);
  392. }
  393. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  394. const char *buf, size_t count)
  395. {
  396. static const u8 reg[6] = {
  397. LM90_REG_W_REMOTE_LOWH,
  398. LM90_REG_W_REMOTE_LOWL,
  399. LM90_REG_W_REMOTE_HIGHH,
  400. LM90_REG_W_REMOTE_HIGHL,
  401. LM90_REG_W_REMOTE_OFFSH,
  402. LM90_REG_W_REMOTE_OFFSL,
  403. };
  404. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  405. struct i2c_client *client = to_i2c_client(dev);
  406. struct lm90_data *data = i2c_get_clientdata(client);
  407. int nr = attr->index;
  408. long val;
  409. int err;
  410. err = strict_strtol(buf, 10, &val);
  411. if (err < 0)
  412. return err;
  413. /* +16 degrees offset for temp2 for the LM99 */
  414. if (data->kind == lm99 && attr->index <= 2)
  415. val -= 16000;
  416. mutex_lock(&data->update_lock);
  417. if (data->kind == adt7461)
  418. data->temp11[nr] = temp_to_u16_adt7461(data, val);
  419. else if (data->kind == max6646)
  420. data->temp11[nr] = temp_to_u8(val) << 8;
  421. else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
  422. data->temp11[nr] = temp_to_s16(val);
  423. else
  424. data->temp11[nr] = temp_to_s8(val) << 8;
  425. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  426. data->temp11[nr] >> 8);
  427. if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
  428. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  429. data->temp11[nr] & 0xff);
  430. mutex_unlock(&data->update_lock);
  431. return count;
  432. }
  433. static ssize_t show_temphyst(struct device *dev,
  434. struct device_attribute *devattr,
  435. char *buf)
  436. {
  437. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  438. struct lm90_data *data = lm90_update_device(dev);
  439. int temp;
  440. if (data->kind == adt7461)
  441. temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
  442. else if (data->kind == max6646)
  443. temp = temp_from_u8(data->temp8[attr->index]);
  444. else
  445. temp = temp_from_s8(data->temp8[attr->index]);
  446. /* +16 degrees offset for temp2 for the LM99 */
  447. if (data->kind == lm99 && attr->index == 3)
  448. temp += 16000;
  449. return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
  450. }
  451. static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
  452. const char *buf, size_t count)
  453. {
  454. struct i2c_client *client = to_i2c_client(dev);
  455. struct lm90_data *data = i2c_get_clientdata(client);
  456. long val;
  457. int err;
  458. int temp;
  459. err = strict_strtol(buf, 10, &val);
  460. if (err < 0)
  461. return err;
  462. mutex_lock(&data->update_lock);
  463. if (data->kind == adt7461)
  464. temp = temp_from_u8_adt7461(data, data->temp8[2]);
  465. else if (data->kind == max6646)
  466. temp = temp_from_u8(data->temp8[2]);
  467. else
  468. temp = temp_from_s8(data->temp8[2]);
  469. data->temp_hyst = hyst_to_reg(temp - val);
  470. i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
  471. data->temp_hyst);
  472. mutex_unlock(&data->update_lock);
  473. return count;
  474. }
  475. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  476. char *buf)
  477. {
  478. struct lm90_data *data = lm90_update_device(dev);
  479. return sprintf(buf, "%d\n", data->alarms);
  480. }
  481. static ssize_t show_alarm(struct device *dev, struct device_attribute
  482. *devattr, char *buf)
  483. {
  484. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  485. struct lm90_data *data = lm90_update_device(dev);
  486. int bitnr = attr->index;
  487. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  488. }
  489. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
  490. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  491. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
  492. set_temp8, 0);
  493. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  494. set_temp11, 1);
  495. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
  496. set_temp8, 1);
  497. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  498. set_temp11, 2);
  499. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
  500. set_temp8, 2);
  501. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
  502. set_temp8, 3);
  503. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
  504. set_temphyst, 2);
  505. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
  506. static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
  507. set_temp11, 3);
  508. /* Individual alarm files */
  509. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  510. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  511. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  512. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  513. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  514. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  515. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  516. /* Raw alarm file for compatibility */
  517. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  518. static struct attribute *lm90_attributes[] = {
  519. &sensor_dev_attr_temp1_input.dev_attr.attr,
  520. &sensor_dev_attr_temp2_input.dev_attr.attr,
  521. &sensor_dev_attr_temp1_min.dev_attr.attr,
  522. &sensor_dev_attr_temp2_min.dev_attr.attr,
  523. &sensor_dev_attr_temp1_max.dev_attr.attr,
  524. &sensor_dev_attr_temp2_max.dev_attr.attr,
  525. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  526. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  527. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  528. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  529. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  530. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  531. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  532. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  533. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  534. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  535. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  536. &dev_attr_alarms.attr,
  537. NULL
  538. };
  539. static const struct attribute_group lm90_group = {
  540. .attrs = lm90_attributes,
  541. };
  542. /* pec used for ADM1032 only */
  543. static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
  544. char *buf)
  545. {
  546. struct i2c_client *client = to_i2c_client(dev);
  547. return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
  548. }
  549. static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
  550. const char *buf, size_t count)
  551. {
  552. struct i2c_client *client = to_i2c_client(dev);
  553. long val;
  554. int err;
  555. err = strict_strtol(buf, 10, &val);
  556. if (err < 0)
  557. return err;
  558. switch (val) {
  559. case 0:
  560. client->flags &= ~I2C_CLIENT_PEC;
  561. break;
  562. case 1:
  563. client->flags |= I2C_CLIENT_PEC;
  564. break;
  565. default:
  566. return -EINVAL;
  567. }
  568. return count;
  569. }
  570. static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
  571. /*
  572. * Real code
  573. */
  574. /*
  575. * The ADM1032 supports PEC but not on write byte transactions, so we need
  576. * to explicitly ask for a transaction without PEC.
  577. */
  578. static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
  579. {
  580. return i2c_smbus_xfer(client->adapter, client->addr,
  581. client->flags & ~I2C_CLIENT_PEC,
  582. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  583. }
  584. /*
  585. * It is assumed that client->update_lock is held (unless we are in
  586. * detection or initialization steps). This matters when PEC is enabled,
  587. * because we don't want the address pointer to change between the write
  588. * byte and the read byte transactions.
  589. */
  590. static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
  591. {
  592. int err;
  593. if (client->flags & I2C_CLIENT_PEC) {
  594. err = adm1032_write_byte(client, reg);
  595. if (err >= 0)
  596. err = i2c_smbus_read_byte(client);
  597. } else
  598. err = i2c_smbus_read_byte_data(client, reg);
  599. if (err < 0) {
  600. dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
  601. reg, err);
  602. return err;
  603. }
  604. *value = err;
  605. return 0;
  606. }
  607. /* Return 0 if detection is successful, -ENODEV otherwise */
  608. static int lm90_detect(struct i2c_client *new_client,
  609. struct i2c_board_info *info)
  610. {
  611. struct i2c_adapter *adapter = new_client->adapter;
  612. int address = new_client->addr;
  613. const char *name = NULL;
  614. int man_id, chip_id, reg_config1, reg_convrate;
  615. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  616. return -ENODEV;
  617. /* detection and identification */
  618. if ((man_id = i2c_smbus_read_byte_data(new_client,
  619. LM90_REG_R_MAN_ID)) < 0
  620. || (chip_id = i2c_smbus_read_byte_data(new_client,
  621. LM90_REG_R_CHIP_ID)) < 0
  622. || (reg_config1 = i2c_smbus_read_byte_data(new_client,
  623. LM90_REG_R_CONFIG1)) < 0
  624. || (reg_convrate = i2c_smbus_read_byte_data(new_client,
  625. LM90_REG_R_CONVRATE)) < 0)
  626. return -ENODEV;
  627. if ((address == 0x4C || address == 0x4D)
  628. && man_id == 0x01) { /* National Semiconductor */
  629. int reg_config2;
  630. reg_config2 = i2c_smbus_read_byte_data(new_client,
  631. LM90_REG_R_CONFIG2);
  632. if (reg_config2 < 0)
  633. return -ENODEV;
  634. if ((reg_config1 & 0x2A) == 0x00
  635. && (reg_config2 & 0xF8) == 0x00
  636. && reg_convrate <= 0x09) {
  637. if (address == 0x4C
  638. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  639. name = "lm90";
  640. } else
  641. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  642. name = "lm99";
  643. dev_info(&adapter->dev,
  644. "Assuming LM99 chip at 0x%02x\n",
  645. address);
  646. dev_info(&adapter->dev,
  647. "If it is an LM89, instantiate it "
  648. "with the new_device sysfs "
  649. "interface\n");
  650. } else
  651. if (address == 0x4C
  652. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  653. name = "lm86";
  654. }
  655. }
  656. } else
  657. if ((address == 0x4C || address == 0x4D)
  658. && man_id == 0x41) { /* Analog Devices */
  659. if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
  660. && (reg_config1 & 0x3F) == 0x00
  661. && reg_convrate <= 0x0A) {
  662. name = "adm1032";
  663. /* The ADM1032 supports PEC, but only if combined
  664. transactions are not used. */
  665. if (i2c_check_functionality(adapter,
  666. I2C_FUNC_SMBUS_BYTE))
  667. info->flags |= I2C_CLIENT_PEC;
  668. } else
  669. if (chip_id == 0x51 /* ADT7461 */
  670. && (reg_config1 & 0x1B) == 0x00
  671. && reg_convrate <= 0x0A) {
  672. name = "adt7461";
  673. }
  674. } else
  675. if (man_id == 0x4D) { /* Maxim */
  676. /*
  677. * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
  678. * register. Reading from that address will return the last
  679. * read value, which in our case is those of the man_id
  680. * register. Likewise, the config1 register seems to lack a
  681. * low nibble, so the value will be those of the previous
  682. * read, so in our case those of the man_id register.
  683. */
  684. if (chip_id == man_id
  685. && (address == 0x4C || address == 0x4D)
  686. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  687. && reg_convrate <= 0x09) {
  688. name = "max6657";
  689. } else
  690. /*
  691. * The chip_id register of the MAX6680 and MAX6681 holds the
  692. * revision of the chip. The lowest bit of the config1 register
  693. * is unused and should return zero when read, so should the
  694. * second to last bit of config1 (software reset).
  695. */
  696. if (chip_id == 0x01
  697. && (reg_config1 & 0x03) == 0x00
  698. && reg_convrate <= 0x07) {
  699. name = "max6680";
  700. } else
  701. /*
  702. * The chip_id register of the MAX6646/6647/6649 holds the
  703. * revision of the chip. The lowest 6 bits of the config1
  704. * register are unused and should return zero when read.
  705. */
  706. if (chip_id == 0x59
  707. && (reg_config1 & 0x3f) == 0x00
  708. && reg_convrate <= 0x07) {
  709. name = "max6646";
  710. }
  711. } else
  712. if (address == 0x4C
  713. && man_id == 0x5C) { /* Winbond/Nuvoton */
  714. if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
  715. && (reg_config1 & 0x2A) == 0x00
  716. && reg_convrate <= 0x08) {
  717. name = "w83l771";
  718. }
  719. }
  720. if (!name) { /* identification failed */
  721. dev_dbg(&adapter->dev,
  722. "Unsupported chip at 0x%02x (man_id=0x%02X, "
  723. "chip_id=0x%02X)\n", address, man_id, chip_id);
  724. return -ENODEV;
  725. }
  726. strlcpy(info->type, name, I2C_NAME_SIZE);
  727. return 0;
  728. }
  729. static int lm90_probe(struct i2c_client *new_client,
  730. const struct i2c_device_id *id)
  731. {
  732. struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
  733. struct lm90_data *data;
  734. int err;
  735. data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
  736. if (!data) {
  737. err = -ENOMEM;
  738. goto exit;
  739. }
  740. i2c_set_clientdata(new_client, data);
  741. mutex_init(&data->update_lock);
  742. /* Set the device type */
  743. data->kind = id->driver_data;
  744. if (data->kind == adm1032) {
  745. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  746. new_client->flags &= ~I2C_CLIENT_PEC;
  747. }
  748. /* Different devices have different alarm bits triggering the
  749. * ALERT# output */
  750. switch (data->kind) {
  751. case lm90:
  752. case lm99:
  753. case lm86:
  754. data->alert_alarms = 0x7b;
  755. break;
  756. default:
  757. data->alert_alarms = 0x7c;
  758. break;
  759. }
  760. /* Set chip capabilities */
  761. if (data->kind != max6657 && data->kind != max6646)
  762. data->flags |= LM90_HAVE_OFFSET;
  763. if (data->kind == max6657 || data->kind == max6646)
  764. data->flags |= LM90_HAVE_LOCAL_EXT;
  765. if (data->kind != max6657 && data->kind != max6646
  766. && data->kind != max6680)
  767. data->flags |= LM90_HAVE_REM_LIMIT_EXT;
  768. /* Initialize the LM90 chip */
  769. lm90_init_client(new_client);
  770. /* Register sysfs hooks */
  771. err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
  772. if (err)
  773. goto exit_free;
  774. if (new_client->flags & I2C_CLIENT_PEC) {
  775. err = device_create_file(&new_client->dev, &dev_attr_pec);
  776. if (err)
  777. goto exit_remove_files;
  778. }
  779. if (data->flags & LM90_HAVE_OFFSET) {
  780. err = device_create_file(&new_client->dev,
  781. &sensor_dev_attr_temp2_offset.dev_attr);
  782. if (err)
  783. goto exit_remove_files;
  784. }
  785. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  786. if (IS_ERR(data->hwmon_dev)) {
  787. err = PTR_ERR(data->hwmon_dev);
  788. goto exit_remove_files;
  789. }
  790. return 0;
  791. exit_remove_files:
  792. sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
  793. device_remove_file(&new_client->dev, &dev_attr_pec);
  794. exit_free:
  795. kfree(data);
  796. exit:
  797. return err;
  798. }
  799. static void lm90_init_client(struct i2c_client *client)
  800. {
  801. u8 config;
  802. struct lm90_data *data = i2c_get_clientdata(client);
  803. /*
  804. * Start the conversions.
  805. */
  806. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  807. 5); /* 2 Hz */
  808. if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
  809. dev_warn(&client->dev, "Initialization failed!\n");
  810. return;
  811. }
  812. data->config_orig = config;
  813. /* Check Temperature Range Select */
  814. if (data->kind == adt7461) {
  815. if (config & 0x04)
  816. data->flags |= LM90_FLAG_ADT7461_EXT;
  817. }
  818. /*
  819. * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
  820. * 0.125 degree resolution) and range (0x08, extend range
  821. * to -64 degree) mode for the remote temperature sensor.
  822. */
  823. if (data->kind == max6680)
  824. config |= 0x18;
  825. config &= 0xBF; /* run */
  826. if (config != data->config_orig) /* Only write if changed */
  827. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
  828. }
  829. static int lm90_remove(struct i2c_client *client)
  830. {
  831. struct lm90_data *data = i2c_get_clientdata(client);
  832. hwmon_device_unregister(data->hwmon_dev);
  833. sysfs_remove_group(&client->dev.kobj, &lm90_group);
  834. device_remove_file(&client->dev, &dev_attr_pec);
  835. if (data->flags & LM90_HAVE_OFFSET)
  836. device_remove_file(&client->dev,
  837. &sensor_dev_attr_temp2_offset.dev_attr);
  838. /* Restore initial configuration */
  839. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  840. data->config_orig);
  841. kfree(data);
  842. return 0;
  843. }
  844. static void lm90_alert(struct i2c_client *client, unsigned int flag)
  845. {
  846. struct lm90_data *data = i2c_get_clientdata(client);
  847. u8 config, alarms;
  848. lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
  849. if ((alarms & 0x7f) == 0) {
  850. dev_info(&client->dev, "Everything OK\n");
  851. } else {
  852. if (alarms & 0x61)
  853. dev_warn(&client->dev,
  854. "temp%d out of range, please check!\n", 1);
  855. if (alarms & 0x1a)
  856. dev_warn(&client->dev,
  857. "temp%d out of range, please check!\n", 2);
  858. if (alarms & 0x04)
  859. dev_warn(&client->dev,
  860. "temp%d diode open, please check!\n", 2);
  861. /* Disable ALERT# output, because these chips don't implement
  862. SMBus alert correctly; they should only hold the alert line
  863. low briefly. */
  864. if ((data->kind == adm1032 || data->kind == adt7461)
  865. && (alarms & data->alert_alarms)) {
  866. dev_dbg(&client->dev, "Disabling ALERT#\n");
  867. lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
  868. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  869. config | 0x80);
  870. }
  871. }
  872. }
  873. static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
  874. {
  875. int err;
  876. u8 oldh, newh, l;
  877. /*
  878. * There is a trick here. We have to read two registers to have the
  879. * sensor temperature, but we have to beware a conversion could occur
  880. * inbetween the readings. The datasheet says we should either use
  881. * the one-shot conversion register, which we don't want to do
  882. * (disables hardware monitoring) or monitor the busy bit, which is
  883. * impossible (we can't read the values and monitor that bit at the
  884. * exact same time). So the solution used here is to read the high
  885. * byte once, then the low byte, then the high byte again. If the new
  886. * high byte matches the old one, then we have a valid reading. Else
  887. * we have to read the low byte again, and now we believe we have a
  888. * correct reading.
  889. */
  890. if ((err = lm90_read_reg(client, regh, &oldh))
  891. || (err = lm90_read_reg(client, regl, &l))
  892. || (err = lm90_read_reg(client, regh, &newh)))
  893. return err;
  894. if (oldh != newh) {
  895. err = lm90_read_reg(client, regl, &l);
  896. if (err)
  897. return err;
  898. }
  899. *value = (newh << 8) | l;
  900. return 0;
  901. }
  902. static struct lm90_data *lm90_update_device(struct device *dev)
  903. {
  904. struct i2c_client *client = to_i2c_client(dev);
  905. struct lm90_data *data = i2c_get_clientdata(client);
  906. mutex_lock(&data->update_lock);
  907. if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
  908. || !data->valid) {
  909. u8 h, l;
  910. dev_dbg(&client->dev, "Updating lm90 data.\n");
  911. lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
  912. lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
  913. lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
  914. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
  915. lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
  916. if (data->flags & LM90_HAVE_LOCAL_EXT) {
  917. lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
  918. MAX6657_REG_R_LOCAL_TEMPL,
  919. &data->temp11[4]);
  920. } else {
  921. if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
  922. &h) == 0)
  923. data->temp11[4] = h << 8;
  924. }
  925. lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
  926. LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
  927. if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
  928. data->temp11[1] = h << 8;
  929. if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
  930. && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
  931. &l) == 0)
  932. data->temp11[1] |= l;
  933. }
  934. if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
  935. data->temp11[2] = h << 8;
  936. if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
  937. && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
  938. &l) == 0)
  939. data->temp11[2] |= l;
  940. }
  941. if (data->flags & LM90_HAVE_OFFSET) {
  942. if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
  943. &h) == 0
  944. && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
  945. &l) == 0)
  946. data->temp11[3] = (h << 8) | l;
  947. }
  948. lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
  949. /* Re-enable ALERT# output if it was originally enabled and
  950. * relevant alarms are all clear */
  951. if ((data->config_orig & 0x80) == 0
  952. && (data->alarms & data->alert_alarms) == 0) {
  953. u8 config;
  954. lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
  955. if (config & 0x80) {
  956. dev_dbg(&client->dev, "Re-enabling ALERT#\n");
  957. i2c_smbus_write_byte_data(client,
  958. LM90_REG_W_CONFIG1,
  959. config & ~0x80);
  960. }
  961. }
  962. data->last_updated = jiffies;
  963. data->valid = 1;
  964. }
  965. mutex_unlock(&data->update_lock);
  966. return data;
  967. }
  968. static int __init sensors_lm90_init(void)
  969. {
  970. return i2c_add_driver(&lm90_driver);
  971. }
  972. static void __exit sensors_lm90_exit(void)
  973. {
  974. i2c_del_driver(&lm90_driver);
  975. }
  976. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  977. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  978. MODULE_LICENSE("GPL");
  979. module_init(sensors_lm90_init);
  980. module_exit(sensors_lm90_exit);