lm90.c 49 KB

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