lm90.c 49 KB

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