lm90.c 51 KB

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