lm90.c 45 KB

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