lm90.c 41 KB

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