lm90.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  4. * monitoring
  5. * Copyright (C) 2003-2010 Jean Delvare <jdelvare@suse.de>
  6. *
  7. * Based on the lm83 driver. The LM90 is a sensor chip made by National
  8. * Semiconductor. It reports up to two temperatures (its own plus up to
  9. * one external one) with a 0.125 deg resolution (1 deg for local
  10. * temperature) and a 3-4 deg accuracy.
  11. *
  12. * This driver also supports the LM89 and LM99, two other sensor chips
  13. * made by National Semiconductor. Both have an increased remote
  14. * temperature measurement accuracy (1 degree), and the LM99
  15. * additionally shifts remote temperatures (measured and limits) by 16
  16. * degrees, which allows for higher temperatures measurement.
  17. * Note that there is no way to differentiate between both chips.
  18. * When device is auto-detected, the driver will assume an LM99.
  19. *
  20. * This driver also supports the LM86, another sensor chip made by
  21. * National Semiconductor. It is exactly similar to the LM90 except it
  22. * has a higher accuracy.
  23. *
  24. * This driver also supports the ADM1032, a sensor chip made by Analog
  25. * Devices. That chip is similar to the LM90, with a few differences
  26. * that are not handled by this driver. Among others, it has a higher
  27. * accuracy than the LM90, much like the LM86 does.
  28. *
  29. * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
  30. * chips made by Maxim. These chips are similar to the LM86.
  31. * Note that there is no easy way to differentiate between the three
  32. * variants. We use the device address to detect MAX6659, which will result
  33. * in a detection as max6657 if it is on address 0x4c. The extra address
  34. * and features of the MAX6659 are only supported if the chip is configured
  35. * explicitly as max6659, or if its address is not 0x4c.
  36. * These chips lack the remote temperature offset feature.
  37. *
  38. * This driver also supports the MAX6654 chip made by Maxim. This chip can be
  39. * at 9 different addresses, similar to MAX6680/MAX6681. The MAX6654 is similar
  40. * to MAX6657/MAX6658/MAX6659, but does not support critical temperature
  41. * limits. Extended range is available by setting the configuration register
  42. * accordingly, and is done during initialization. Extended precision is only
  43. * available at conversion rates of 1 Hz and slower. Note that extended
  44. * precision is not enabled by default, as this driver initializes all chips
  45. * to 2 Hz by design. The driver also supports MAX6690, which is practically
  46. * identical to MAX6654.
  47. *
  48. * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
  49. * MAX6692 chips made by Maxim. These are again similar to the LM86,
  50. * but they use unsigned temperature values and can report temperatures
  51. * from 0 to 145 degrees.
  52. *
  53. * This driver also supports the MAX6680 and MAX6681, two other sensor
  54. * chips made by Maxim. These are quite similar to the other Maxim
  55. * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
  56. * be treated identically.
  57. *
  58. * This driver also supports the MAX6695 and MAX6696, two other sensor
  59. * chips made by Maxim. These are also quite similar to other Maxim
  60. * chips, but support three temperature sensors instead of two. MAX6695
  61. * and MAX6696 only differ in the pinout so they can be treated identically.
  62. *
  63. * This driver also supports ADT7461 and ADT7461A from Analog Devices as well as
  64. * NCT1008 from ON Semiconductor. The chips are supported in both compatibility
  65. * and extended mode. They are mostly compatible with LM90 except for a data
  66. * format difference for the temperature value registers.
  67. *
  68. * This driver also supports ADT7481, ADT7482, and ADT7483 from Analog Devices
  69. * / ON Semiconductor. The chips are similar to ADT7461 but support two external
  70. * temperature sensors.
  71. *
  72. * This driver also supports NCT72, NCT214, and NCT218 from ON Semiconductor.
  73. * The chips are similar to ADT7461/ADT7461A but have full PEC support
  74. * (undocumented).
  75. *
  76. * This driver also supports the SA56004 from Philips. This device is
  77. * pin-compatible with the LM86, the ED/EDP parts are also address-compatible.
  78. *
  79. * This driver also supports the G781 from GMT. This device is compatible
  80. * with the ADM1032.
  81. *
  82. * This driver also supports TMP451 and TMP461 from Texas Instruments.
  83. * Those devices are supported in both compatibility and extended mode.
  84. * They are mostly compatible with ADT7461 except for local temperature
  85. * low byte register and max conversion rate.
  86. *
  87. * This driver also supports MAX1617 and various clones such as G767
  88. * and NE1617. Such clones will be detected as MAX1617.
  89. *
  90. * This driver also supports NE1618 from Philips. It is similar to NE1617
  91. * but supports 11 bit external temperature values.
  92. *
  93. * This driver also supports NCT7716, NCT7717 and NCT7718 from Nuvoton.
  94. * The NCT7716 is similar to NCT7717 but has one more address support.
  95. *
  96. * Since the LM90 was the first chipset supported by this driver, most
  97. * comments will refer to this chipset, but are actually general and
  98. * concern all supported chipsets, unless mentioned otherwise.
  99. */
  100. #include <linux/bits.h>
  101. #include <linux/device.h>
  102. #include <linux/err.h>
  103. #include <linux/i2c.h>
  104. #include <linux/init.h>
  105. #include <linux/interrupt.h>
  106. #include <linux/jiffies.h>
  107. #include <linux/hwmon.h>
  108. #include <linux/kstrtox.h>
  109. #include <linux/module.h>
  110. #include <linux/of.h>
  111. #include <linux/regulator/consumer.h>
  112. #include <linux/slab.h>
  113. #include <linux/workqueue.h>
  114. /* The maximum number of channels currently supported */
  115. #define MAX_CHANNELS 3
  116. /*
  117. * Addresses to scan
  118. * Address is fully defined internally and cannot be changed except for
  119. * MAX6659, MAX6680 and MAX6681.
  120. * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, ADT7461A, MAX6649,
  121. * MAX6657, MAX6658, NCT1008, NCT7718 and W83L771 have address 0x4c.
  122. * ADM1032-2, ADT7461-2, ADT7461A-2, LM89-1, LM99-1, MAX6646, and NCT1008D
  123. * have address 0x4d.
  124. * MAX6647 has address 0x4e.
  125. * MAX6659 can have address 0x4c, 0x4d or 0x4e.
  126. * MAX6654, MAX6680, and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29,
  127. * 0x2a, 0x2b, 0x4c, 0x4d or 0x4e.
  128. * NCT7716 can have address 0x48 or 0x49.
  129. * NCT7717 has address 0x48.
  130. * SA56004 can have address 0x48 through 0x4F.
  131. */
  132. static const unsigned short normal_i2c[] = {
  133. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  134. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  135. enum chips { adm1023, adm1032, adt7461, adt7461a, adt7481,
  136. g781, lm84, lm90, lm99,
  137. max1617, max6642, max6646, max6648, max6654, max6657, max6659, max6680, max6696,
  138. nct210, nct72, nct7716, nct7717, nct7718, ne1618, sa56004, tmp451, tmp461, w83l771,
  139. };
  140. /*
  141. * The LM90 registers
  142. */
  143. #define LM90_REG_MAN_ID 0xFE
  144. #define LM90_REG_CHIP_ID 0xFF
  145. #define LM90_REG_CONFIG1 0x03
  146. #define LM90_REG_CONFIG2 0xBF
  147. #define LM90_REG_CONVRATE 0x04
  148. #define LM90_REG_STATUS 0x02
  149. #define LM90_REG_LOCAL_TEMP 0x00
  150. #define LM90_REG_LOCAL_HIGH 0x05
  151. #define LM90_REG_LOCAL_LOW 0x06
  152. #define LM90_REG_LOCAL_CRIT 0x20
  153. #define LM90_REG_REMOTE_TEMPH 0x01
  154. #define LM90_REG_REMOTE_TEMPL 0x10
  155. #define LM90_REG_REMOTE_OFFSH 0x11
  156. #define LM90_REG_REMOTE_OFFSL 0x12
  157. #define LM90_REG_REMOTE_HIGHH 0x07
  158. #define LM90_REG_REMOTE_HIGHL 0x13
  159. #define LM90_REG_REMOTE_LOWH 0x08
  160. #define LM90_REG_REMOTE_LOWL 0x14
  161. #define LM90_REG_REMOTE_CRIT 0x19
  162. #define LM90_REG_TCRIT_HYST 0x21
  163. /* MAX6646/6647/6649/6654/6657/6658/6659/6695/6696 registers */
  164. #define MAX6657_REG_LOCAL_TEMPL 0x11
  165. #define MAX6696_REG_STATUS2 0x12
  166. #define MAX6659_REG_REMOTE_EMERG 0x16
  167. #define MAX6659_REG_LOCAL_EMERG 0x17
  168. /* SA56004 registers */
  169. #define SA56004_REG_LOCAL_TEMPL 0x22
  170. #define LM90_MAX_CONVRATE_MS 16000 /* Maximum conversion rate in ms */
  171. /* TMP451/TMP461 registers */
  172. #define TMP451_REG_LOCAL_TEMPL 0x15
  173. #define TMP451_REG_CONALERT 0x22
  174. #define TMP461_REG_CHEN 0x16
  175. #define TMP461_REG_DFC 0x24
  176. /* ADT7481 registers */
  177. #define ADT7481_REG_STATUS2 0x23
  178. #define ADT7481_REG_CONFIG2 0x24
  179. #define ADT7481_REG_MAN_ID 0x3e
  180. #define ADT7481_REG_CHIP_ID 0x3d
  181. /* NCT7716/7717/7718 registers */
  182. #define NCT7716_REG_CHIP_ID 0xFD
  183. /* Device features */
  184. #define LM90_HAVE_EXTENDED_TEMP BIT(0) /* extended temperature support */
  185. #define LM90_HAVE_OFFSET BIT(1) /* temperature offset register */
  186. #define LM90_HAVE_UNSIGNED_TEMP BIT(2) /* temperatures are unsigned */
  187. #define LM90_HAVE_REM_LIMIT_EXT BIT(3) /* extended remote limit */
  188. #define LM90_HAVE_EMERGENCY BIT(4) /* 3rd upper (emergency) limit */
  189. #define LM90_HAVE_EMERGENCY_ALARM BIT(5)/* emergency alarm */
  190. #define LM90_HAVE_TEMP3 BIT(6) /* 3rd temperature sensor */
  191. #define LM90_HAVE_BROKEN_ALERT BIT(7) /* Broken alert */
  192. #define LM90_PAUSE_FOR_CONFIG BIT(8) /* Pause conversion for config */
  193. #define LM90_HAVE_CRIT BIT(9) /* Chip supports CRIT/OVERT register */
  194. #define LM90_HAVE_CRIT_ALRM_SWP BIT(10) /* critical alarm bits swapped */
  195. #define LM90_HAVE_PEC BIT(11) /* Chip supports PEC */
  196. #define LM90_HAVE_PARTIAL_PEC BIT(12) /* Partial PEC support (adm1032)*/
  197. #define LM90_HAVE_ALARMS BIT(13) /* Create 'alarms' attribute */
  198. #define LM90_HAVE_EXT_UNSIGNED BIT(14) /* extended unsigned temperature*/
  199. #define LM90_HAVE_LOW BIT(15) /* low limits */
  200. #define LM90_HAVE_CONVRATE BIT(16) /* conversion rate */
  201. #define LM90_HAVE_REMOTE_EXT BIT(17) /* extended remote temperature */
  202. #define LM90_HAVE_FAULTQUEUE BIT(18) /* configurable samples count */
  203. /* LM90 status */
  204. #define LM90_STATUS_LTHRM BIT(0) /* local THERM limit tripped */
  205. #define LM90_STATUS_RTHRM BIT(1) /* remote THERM limit tripped */
  206. #define LM90_STATUS_ROPEN BIT(2) /* remote is an open circuit */
  207. #define LM90_STATUS_RLOW BIT(3) /* remote low temp limit tripped */
  208. #define LM90_STATUS_RHIGH BIT(4) /* remote high temp limit tripped */
  209. #define LM90_STATUS_LLOW BIT(5) /* local low temp limit tripped */
  210. #define LM90_STATUS_LHIGH BIT(6) /* local high temp limit tripped */
  211. #define LM90_STATUS_BUSY BIT(7) /* conversion is ongoing */
  212. /* MAX6695/6696 and ADT7481 2nd status register */
  213. #define MAX6696_STATUS2_R2THRM BIT(1) /* remote2 THERM limit tripped */
  214. #define MAX6696_STATUS2_R2OPEN BIT(2) /* remote2 is an open circuit */
  215. #define MAX6696_STATUS2_R2LOW BIT(3) /* remote2 low temp limit tripped */
  216. #define MAX6696_STATUS2_R2HIGH BIT(4) /* remote2 high temp limit tripped */
  217. #define MAX6696_STATUS2_ROT2 BIT(5) /* remote emergency limit tripped */
  218. #define MAX6696_STATUS2_R2OT2 BIT(6) /* remote2 emergency limit tripped */
  219. #define MAX6696_STATUS2_LOT2 BIT(7) /* local emergency limit tripped */
  220. /*
  221. * Driver data (common to all clients)
  222. */
  223. static const struct i2c_device_id lm90_id[] = {
  224. { "adm1020", max1617 },
  225. { "adm1021", max1617 },
  226. { "adm1023", adm1023 },
  227. { "adm1032", adm1032 },
  228. { "adt7421", adt7461a },
  229. { "adt7461", adt7461 },
  230. { "adt7461a", adt7461a },
  231. { "adt7481", adt7481 },
  232. { "adt7482", adt7481 },
  233. { "adt7483a", adt7481 },
  234. { "g781", g781 },
  235. { "gl523sm", max1617 },
  236. { "lm84", lm84 },
  237. { "lm86", lm90 },
  238. { "lm89", lm90 },
  239. { "lm90", lm90 },
  240. { "lm99", lm99 },
  241. { "max1617", max1617 },
  242. { "max6642", max6642 },
  243. { "max6646", max6646 },
  244. { "max6647", max6646 },
  245. { "max6648", max6648 },
  246. { "max6649", max6646 },
  247. { "max6654", max6654 },
  248. { "max6657", max6657 },
  249. { "max6658", max6657 },
  250. { "max6659", max6659 },
  251. { "max6680", max6680 },
  252. { "max6681", max6680 },
  253. { "max6690", max6654 },
  254. { "max6692", max6648 },
  255. { "max6695", max6696 },
  256. { "max6696", max6696 },
  257. { "mc1066", max1617 },
  258. { "nct1008", adt7461a },
  259. { "nct210", nct210 },
  260. { "nct214", nct72 },
  261. { "nct218", nct72 },
  262. { "nct72", nct72 },
  263. { "nct7716", nct7716 },
  264. { "nct7717", nct7717 },
  265. { "nct7718", nct7718 },
  266. { "ne1618", ne1618 },
  267. { "w83l771", w83l771 },
  268. { "sa56004", sa56004 },
  269. { "thmc10", max1617 },
  270. { "tmp451", tmp451 },
  271. { "tmp461", tmp461 },
  272. { }
  273. };
  274. MODULE_DEVICE_TABLE(i2c, lm90_id);
  275. static const struct of_device_id __maybe_unused lm90_of_match[] = {
  276. {
  277. .compatible = "adi,adm1032",
  278. .data = (void *)adm1032
  279. },
  280. {
  281. .compatible = "adi,adt7461",
  282. .data = (void *)adt7461
  283. },
  284. {
  285. .compatible = "adi,adt7461a",
  286. .data = (void *)adt7461a
  287. },
  288. {
  289. .compatible = "adi,adt7481",
  290. .data = (void *)adt7481
  291. },
  292. {
  293. .compatible = "gmt,g781",
  294. .data = (void *)g781
  295. },
  296. {
  297. .compatible = "national,lm90",
  298. .data = (void *)lm90
  299. },
  300. {
  301. .compatible = "national,lm86",
  302. .data = (void *)lm90
  303. },
  304. {
  305. .compatible = "national,lm89",
  306. .data = (void *)lm90
  307. },
  308. {
  309. .compatible = "national,lm99",
  310. .data = (void *)lm99
  311. },
  312. {
  313. .compatible = "dallas,max6646",
  314. .data = (void *)max6646
  315. },
  316. {
  317. .compatible = "dallas,max6647",
  318. .data = (void *)max6646
  319. },
  320. {
  321. .compatible = "dallas,max6649",
  322. .data = (void *)max6646
  323. },
  324. {
  325. .compatible = "dallas,max6654",
  326. .data = (void *)max6654
  327. },
  328. {
  329. .compatible = "dallas,max6657",
  330. .data = (void *)max6657
  331. },
  332. {
  333. .compatible = "dallas,max6658",
  334. .data = (void *)max6657
  335. },
  336. {
  337. .compatible = "dallas,max6659",
  338. .data = (void *)max6659
  339. },
  340. {
  341. .compatible = "dallas,max6680",
  342. .data = (void *)max6680
  343. },
  344. {
  345. .compatible = "dallas,max6681",
  346. .data = (void *)max6680
  347. },
  348. {
  349. .compatible = "dallas,max6695",
  350. .data = (void *)max6696
  351. },
  352. {
  353. .compatible = "dallas,max6696",
  354. .data = (void *)max6696
  355. },
  356. {
  357. .compatible = "onnn,nct1008",
  358. .data = (void *)adt7461a
  359. },
  360. {
  361. .compatible = "onnn,nct214",
  362. .data = (void *)nct72
  363. },
  364. {
  365. .compatible = "onnn,nct218",
  366. .data = (void *)nct72
  367. },
  368. {
  369. .compatible = "onnn,nct72",
  370. .data = (void *)nct72
  371. },
  372. {
  373. .compatible = "nuvoton,nct7716",
  374. .data = (void *)nct7716
  375. },
  376. {
  377. .compatible = "nuvoton,nct7717",
  378. .data = (void *)nct7717
  379. },
  380. {
  381. .compatible = "nuvoton,nct7718",
  382. .data = (void *)nct7718
  383. },
  384. {
  385. .compatible = "winbond,w83l771",
  386. .data = (void *)w83l771
  387. },
  388. {
  389. .compatible = "nxp,sa56004",
  390. .data = (void *)sa56004
  391. },
  392. {
  393. .compatible = "ti,tmp451",
  394. .data = (void *)tmp451
  395. },
  396. {
  397. .compatible = "ti,tmp461",
  398. .data = (void *)tmp461
  399. },
  400. { },
  401. };
  402. MODULE_DEVICE_TABLE(of, lm90_of_match);
  403. /*
  404. * chip type specific parameters
  405. */
  406. struct lm90_params {
  407. u32 flags; /* Capabilities */
  408. u16 alert_alarms; /* Which alarm bits trigger ALERT# */
  409. /* Upper 8 bits for max6695/96 */
  410. u8 max_convrate; /* Maximum conversion rate register value */
  411. u8 resolution; /* 16-bit resolution (default 11 bit) */
  412. u8 reg_status2; /* 2nd status register (optional) */
  413. u8 reg_local_ext; /* Extended local temp register (optional) */
  414. u8 faultqueue_mask; /* fault queue bit mask */
  415. u8 faultqueue_depth; /* fault queue depth if mask is used */
  416. };
  417. static const struct lm90_params lm90_params[] = {
  418. [adm1023] = {
  419. .flags = LM90_HAVE_ALARMS | LM90_HAVE_OFFSET | LM90_HAVE_BROKEN_ALERT
  420. | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  421. | LM90_HAVE_REMOTE_EXT,
  422. .alert_alarms = 0x7c,
  423. .resolution = 8,
  424. .max_convrate = 7,
  425. },
  426. [adm1032] = {
  427. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  428. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT
  429. | LM90_HAVE_PARTIAL_PEC | LM90_HAVE_ALARMS
  430. | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
  431. | LM90_HAVE_FAULTQUEUE,
  432. .alert_alarms = 0x7c,
  433. .max_convrate = 10,
  434. },
  435. [adt7461] = {
  436. /*
  437. * Standard temperature range is supposed to be unsigned,
  438. * but that does not match reality. Negative temperatures
  439. * are always reported.
  440. */
  441. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  442. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
  443. | LM90_HAVE_CRIT | LM90_HAVE_PARTIAL_PEC
  444. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  445. | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  446. .alert_alarms = 0x7c,
  447. .max_convrate = 10,
  448. .resolution = 10,
  449. },
  450. [adt7461a] = {
  451. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  452. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
  453. | LM90_HAVE_CRIT | LM90_HAVE_PEC | LM90_HAVE_ALARMS
  454. | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
  455. | LM90_HAVE_FAULTQUEUE,
  456. .alert_alarms = 0x7c,
  457. .max_convrate = 10,
  458. },
  459. [adt7481] = {
  460. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  461. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
  462. | LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_PEC
  463. | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT | LM90_HAVE_LOW
  464. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
  465. | LM90_HAVE_FAULTQUEUE,
  466. .alert_alarms = 0x1c7c,
  467. .max_convrate = 11,
  468. .resolution = 10,
  469. .reg_status2 = ADT7481_REG_STATUS2,
  470. },
  471. [g781] = {
  472. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  473. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_CRIT
  474. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  475. | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  476. .alert_alarms = 0x7c,
  477. .max_convrate = 7,
  478. },
  479. [lm84] = {
  480. .flags = LM90_HAVE_ALARMS,
  481. .resolution = 8,
  482. },
  483. [lm90] = {
  484. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  485. | LM90_HAVE_CRIT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
  486. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
  487. | LM90_HAVE_FAULTQUEUE,
  488. .alert_alarms = 0x7b,
  489. .max_convrate = 9,
  490. .faultqueue_mask = BIT(0),
  491. .faultqueue_depth = 3,
  492. },
  493. [lm99] = {
  494. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  495. | LM90_HAVE_CRIT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
  496. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
  497. | LM90_HAVE_FAULTQUEUE,
  498. .alert_alarms = 0x7b,
  499. .max_convrate = 9,
  500. .faultqueue_mask = BIT(0),
  501. .faultqueue_depth = 3,
  502. },
  503. [max1617] = {
  504. .flags = LM90_HAVE_CONVRATE | LM90_HAVE_BROKEN_ALERT |
  505. LM90_HAVE_LOW | LM90_HAVE_ALARMS,
  506. .alert_alarms = 0x78,
  507. .resolution = 8,
  508. .max_convrate = 7,
  509. },
  510. [max6642] = {
  511. .flags = LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXT_UNSIGNED
  512. | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  513. .alert_alarms = 0x50,
  514. .resolution = 10,
  515. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  516. .faultqueue_mask = BIT(4),
  517. .faultqueue_depth = 2,
  518. },
  519. [max6646] = {
  520. .flags = LM90_HAVE_CRIT | LM90_HAVE_BROKEN_ALERT
  521. | LM90_HAVE_EXT_UNSIGNED | LM90_HAVE_ALARMS | LM90_HAVE_LOW
  522. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
  523. .alert_alarms = 0x7c,
  524. .max_convrate = 6,
  525. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  526. },
  527. [max6648] = {
  528. .flags = LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_CRIT
  529. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_LOW
  530. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
  531. .alert_alarms = 0x7c,
  532. .max_convrate = 6,
  533. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  534. },
  535. [max6654] = {
  536. .flags = LM90_HAVE_BROKEN_ALERT | LM90_HAVE_ALARMS | LM90_HAVE_LOW
  537. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
  538. .alert_alarms = 0x7c,
  539. .max_convrate = 7,
  540. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  541. },
  542. [max6657] = {
  543. .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_CRIT
  544. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  545. | LM90_HAVE_REMOTE_EXT,
  546. .alert_alarms = 0x7c,
  547. .max_convrate = 8,
  548. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  549. },
  550. [max6659] = {
  551. .flags = LM90_HAVE_EMERGENCY | LM90_HAVE_CRIT
  552. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  553. | LM90_HAVE_REMOTE_EXT,
  554. .alert_alarms = 0x7c,
  555. .max_convrate = 8,
  556. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  557. },
  558. [max6680] = {
  559. /*
  560. * Apparent temperatures of 128 degrees C or higher are reported
  561. * and treated as negative temperatures (meaning min_alarm will
  562. * be set).
  563. */
  564. .flags = LM90_HAVE_OFFSET | LM90_HAVE_CRIT
  565. | LM90_HAVE_CRIT_ALRM_SWP | LM90_HAVE_BROKEN_ALERT
  566. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  567. | LM90_HAVE_REMOTE_EXT,
  568. .alert_alarms = 0x7c,
  569. .max_convrate = 7,
  570. },
  571. [max6696] = {
  572. .flags = LM90_HAVE_EMERGENCY
  573. | LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3 | LM90_HAVE_CRIT
  574. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  575. | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  576. .alert_alarms = 0x1c7c,
  577. .max_convrate = 6,
  578. .reg_status2 = MAX6696_REG_STATUS2,
  579. .reg_local_ext = MAX6657_REG_LOCAL_TEMPL,
  580. .faultqueue_mask = BIT(5),
  581. .faultqueue_depth = 4,
  582. },
  583. [nct72] = {
  584. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  585. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP
  586. | LM90_HAVE_CRIT | LM90_HAVE_PEC | LM90_HAVE_UNSIGNED_TEMP
  587. | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT
  588. | LM90_HAVE_FAULTQUEUE,
  589. .alert_alarms = 0x7c,
  590. .max_convrate = 10,
  591. .resolution = 10,
  592. },
  593. [nct210] = {
  594. .flags = LM90_HAVE_ALARMS | LM90_HAVE_BROKEN_ALERT
  595. | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  596. | LM90_HAVE_REMOTE_EXT,
  597. .alert_alarms = 0x7c,
  598. .resolution = 11,
  599. .max_convrate = 7,
  600. },
  601. [nct7716] = {
  602. .flags = LM90_HAVE_ALARMS | LM90_HAVE_CONVRATE,
  603. .alert_alarms = 0x40,
  604. .resolution = 8,
  605. .max_convrate = 8,
  606. },
  607. [nct7717] = {
  608. .flags = LM90_HAVE_ALARMS | LM90_HAVE_CONVRATE,
  609. .alert_alarms = 0x40,
  610. .resolution = 8,
  611. .max_convrate = 8,
  612. },
  613. [nct7718] = {
  614. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
  615. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  616. | LM90_HAVE_REMOTE_EXT,
  617. .alert_alarms = 0x7c,
  618. .resolution = 11,
  619. .max_convrate = 8,
  620. },
  621. [ne1618] = {
  622. .flags = LM90_PAUSE_FOR_CONFIG | LM90_HAVE_BROKEN_ALERT
  623. | LM90_HAVE_LOW | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT,
  624. .alert_alarms = 0x7c,
  625. .resolution = 11,
  626. .max_convrate = 7,
  627. },
  628. [w83l771] = {
  629. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
  630. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  631. | LM90_HAVE_REMOTE_EXT,
  632. .alert_alarms = 0x7c,
  633. .max_convrate = 8,
  634. },
  635. [sa56004] = {
  636. /*
  637. * Apparent temperatures of 128 degrees C or higher are reported
  638. * and treated as negative temperatures (meaning min_alarm will
  639. * be set).
  640. */
  641. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT | LM90_HAVE_CRIT
  642. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  643. | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  644. .alert_alarms = 0x7b,
  645. .max_convrate = 9,
  646. .reg_local_ext = SA56004_REG_LOCAL_TEMPL,
  647. .faultqueue_mask = BIT(0),
  648. .faultqueue_depth = 3,
  649. },
  650. [tmp451] = {
  651. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  652. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT
  653. | LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_ALARMS | LM90_HAVE_LOW
  654. | LM90_HAVE_CONVRATE | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  655. .alert_alarms = 0x7c,
  656. .max_convrate = 9,
  657. .resolution = 12,
  658. .reg_local_ext = TMP451_REG_LOCAL_TEMPL,
  659. },
  660. [tmp461] = {
  661. .flags = LM90_HAVE_OFFSET | LM90_HAVE_REM_LIMIT_EXT
  662. | LM90_HAVE_BROKEN_ALERT | LM90_HAVE_EXTENDED_TEMP | LM90_HAVE_CRIT
  663. | LM90_HAVE_ALARMS | LM90_HAVE_LOW | LM90_HAVE_CONVRATE
  664. | LM90_HAVE_REMOTE_EXT | LM90_HAVE_FAULTQUEUE,
  665. .alert_alarms = 0x7c,
  666. .max_convrate = 9,
  667. .resolution = 12,
  668. .reg_local_ext = TMP451_REG_LOCAL_TEMPL,
  669. },
  670. };
  671. /*
  672. * temperature register index
  673. */
  674. enum lm90_temp_reg_index {
  675. LOCAL_LOW = 0,
  676. LOCAL_HIGH,
  677. LOCAL_CRIT,
  678. REMOTE_CRIT,
  679. LOCAL_EMERG, /* max6659 and max6695/96 */
  680. REMOTE_EMERG, /* max6659 and max6695/96 */
  681. REMOTE2_CRIT, /* max6695/96 only */
  682. REMOTE2_EMERG, /* max6695/96 only */
  683. REMOTE_TEMP,
  684. REMOTE_LOW,
  685. REMOTE_HIGH,
  686. REMOTE_OFFSET, /* except max6646, max6657/58/59, and max6695/96 */
  687. LOCAL_TEMP,
  688. REMOTE2_TEMP, /* max6695/96 only */
  689. REMOTE2_LOW, /* max6695/96 only */
  690. REMOTE2_HIGH, /* max6695/96 only */
  691. REMOTE2_OFFSET,
  692. TEMP_REG_NUM
  693. };
  694. /*
  695. * Client data (each client gets its own)
  696. */
  697. struct lm90_data {
  698. struct i2c_client *client;
  699. struct device *hwmon_dev;
  700. u32 chip_config[2];
  701. u32 channel_config[MAX_CHANNELS + 1];
  702. const char *channel_label[MAX_CHANNELS];
  703. struct hwmon_channel_info chip_info;
  704. struct hwmon_channel_info temp_info;
  705. const struct hwmon_channel_info *info[3];
  706. struct hwmon_chip_info chip;
  707. struct delayed_work alert_work;
  708. struct work_struct report_work;
  709. bool valid; /* true if register values are valid */
  710. bool alarms_valid; /* true if status register values are valid */
  711. unsigned long last_updated; /* in jiffies */
  712. unsigned long alarms_updated; /* in jiffies */
  713. int kind;
  714. u32 flags;
  715. unsigned int update_interval; /* in milliseconds */
  716. u8 config; /* Current configuration register value */
  717. u8 config_orig; /* Original configuration register value */
  718. u8 convrate_orig; /* Original conversion rate register value */
  719. u8 resolution; /* temperature resolution in bit */
  720. u16 alert_alarms; /* Which alarm bits trigger ALERT# */
  721. /* Upper 8 bits for max6695/96 */
  722. u8 max_convrate; /* Maximum conversion rate */
  723. u8 reg_status2; /* 2nd status register (optional) */
  724. u8 reg_local_ext; /* local extension register offset */
  725. u8 reg_remote_ext; /* remote temperature low byte */
  726. u8 faultqueue_mask; /* fault queue mask */
  727. u8 faultqueue_depth; /* fault queue mask */
  728. /* registers values */
  729. u16 temp[TEMP_REG_NUM];
  730. u8 temp_hyst;
  731. u8 conalert;
  732. u16 reported_alarms; /* alarms reported as sysfs/udev events */
  733. u16 current_alarms; /* current alarms, reported by chip */
  734. u16 alarms; /* alarms not yet reported to user */
  735. };
  736. /*
  737. * Support functions
  738. */
  739. /*
  740. * If the chip supports PEC but not on write byte transactions, we need
  741. * to explicitly ask for a transaction without PEC.
  742. */
  743. static inline s32 lm90_write_no_pec(struct i2c_client *client, u8 value)
  744. {
  745. return i2c_smbus_xfer(client->adapter, client->addr,
  746. client->flags & ~I2C_CLIENT_PEC,
  747. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  748. }
  749. /*
  750. * It is assumed that client->update_lock is held (unless we are in
  751. * detection or initialization steps). This matters when PEC is enabled
  752. * for chips with partial PEC support, because we don't want the address
  753. * pointer to change between the write byte and the read byte transactions.
  754. */
  755. static int lm90_read_reg(struct i2c_client *client, u8 reg)
  756. {
  757. struct lm90_data *data = i2c_get_clientdata(client);
  758. bool partial_pec = (client->flags & I2C_CLIENT_PEC) &&
  759. (data->flags & LM90_HAVE_PARTIAL_PEC);
  760. int err;
  761. if (partial_pec) {
  762. err = lm90_write_no_pec(client, reg);
  763. if (err)
  764. return err;
  765. return i2c_smbus_read_byte(client);
  766. }
  767. return i2c_smbus_read_byte_data(client, reg);
  768. }
  769. /*
  770. * Return register write address
  771. *
  772. * The write address for registers 0x03 .. 0x08 is the read address plus 6.
  773. * For other registers the write address matches the read address.
  774. */
  775. static u8 lm90_write_reg_addr(u8 reg)
  776. {
  777. if (reg >= LM90_REG_CONFIG1 && reg <= LM90_REG_REMOTE_LOWH)
  778. return reg + 6;
  779. return reg;
  780. }
  781. /*
  782. * Write into LM90 register.
  783. * Convert register address to write address if needed, then execute the
  784. * operation.
  785. */
  786. static int lm90_write_reg(struct i2c_client *client, u8 reg, u8 val)
  787. {
  788. return i2c_smbus_write_byte_data(client, lm90_write_reg_addr(reg), val);
  789. }
  790. /*
  791. * Write into 16-bit LM90 register.
  792. * Convert register addresses to write address if needed, then execute the
  793. * operation.
  794. */
  795. static int lm90_write16(struct i2c_client *client, u8 regh, u8 regl, u16 val)
  796. {
  797. int ret;
  798. ret = lm90_write_reg(client, regh, val >> 8);
  799. if (ret < 0 || !regl)
  800. return ret;
  801. return lm90_write_reg(client, regl, val & 0xff);
  802. }
  803. static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl,
  804. bool is_volatile)
  805. {
  806. int oldh, newh, l;
  807. oldh = lm90_read_reg(client, regh);
  808. if (oldh < 0)
  809. return oldh;
  810. if (!regl)
  811. return oldh << 8;
  812. l = lm90_read_reg(client, regl);
  813. if (l < 0)
  814. return l;
  815. if (!is_volatile)
  816. return (oldh << 8) | l;
  817. /*
  818. * For volatile registers we have to use a trick.
  819. * We have to read two registers to have the sensor temperature,
  820. * but we have to beware a conversion could occur between the
  821. * readings. The datasheet says we should either use
  822. * the one-shot conversion register, which we don't want to do
  823. * (disables hardware monitoring) or monitor the busy bit, which is
  824. * impossible (we can't read the values and monitor that bit at the
  825. * exact same time). So the solution used here is to read the high
  826. * the high byte again. If the new high byte matches the old one,
  827. * then we have a valid reading. Otherwise we have to read the low
  828. * byte again, and now we believe we have a correct reading.
  829. */
  830. newh = lm90_read_reg(client, regh);
  831. if (newh < 0)
  832. return newh;
  833. if (oldh != newh) {
  834. l = lm90_read_reg(client, regl);
  835. if (l < 0)
  836. return l;
  837. }
  838. return (newh << 8) | l;
  839. }
  840. static int lm90_update_confreg(struct lm90_data *data, u8 config)
  841. {
  842. if (data->config != config) {
  843. int err;
  844. err = lm90_write_reg(data->client, LM90_REG_CONFIG1, config);
  845. if (err)
  846. return err;
  847. data->config = config;
  848. }
  849. return 0;
  850. }
  851. /*
  852. * client->update_lock must be held when calling this function (unless we are
  853. * in detection or initialization steps), and while a remote channel other
  854. * than channel 0 is selected. Also, calling code must make sure to re-select
  855. * external channel 0 before releasing the lock. This is necessary because
  856. * various registers have different meanings as a result of selecting a
  857. * non-default remote channel.
  858. */
  859. static int lm90_select_remote_channel(struct lm90_data *data, bool second)
  860. {
  861. u8 config = data->config & ~0x08;
  862. if (second)
  863. config |= 0x08;
  864. return lm90_update_confreg(data, config);
  865. }
  866. static int lm90_write_convrate(struct lm90_data *data, int val)
  867. {
  868. u8 config = data->config;
  869. int err;
  870. /* Save config and pause conversion */
  871. if (data->flags & LM90_PAUSE_FOR_CONFIG) {
  872. err = lm90_update_confreg(data, config | 0x40);
  873. if (err < 0)
  874. return err;
  875. }
  876. /* Set conv rate */
  877. err = lm90_write_reg(data->client, LM90_REG_CONVRATE, val);
  878. /* Revert change to config */
  879. lm90_update_confreg(data, config);
  880. return err;
  881. }
  882. /*
  883. * Set conversion rate.
  884. * client->update_lock must be held when calling this function (unless we are
  885. * in detection or initialization steps).
  886. */
  887. static int lm90_set_convrate(struct i2c_client *client, struct lm90_data *data,
  888. unsigned int interval)
  889. {
  890. unsigned int update_interval;
  891. int i, err;
  892. /* Shift calculations to avoid rounding errors */
  893. interval <<= 6;
  894. /* find the nearest update rate */
  895. for (i = 0, update_interval = LM90_MAX_CONVRATE_MS << 6;
  896. i < data->max_convrate; i++, update_interval >>= 1)
  897. if (interval >= update_interval * 3 / 4)
  898. break;
  899. err = lm90_write_convrate(data, i);
  900. data->update_interval = DIV_ROUND_CLOSEST(update_interval, 64);
  901. return err;
  902. }
  903. static int lm90_set_faultqueue(struct i2c_client *client,
  904. struct lm90_data *data, int val)
  905. {
  906. int err;
  907. if (data->faultqueue_mask) {
  908. err = lm90_update_confreg(data, val <= data->faultqueue_depth / 2 ?
  909. data->config & ~data->faultqueue_mask :
  910. data->config | data->faultqueue_mask);
  911. } else {
  912. static const u8 values[4] = {0, 2, 6, 0x0e};
  913. data->conalert = (data->conalert & 0xf1) | values[val - 1];
  914. err = lm90_write_reg(data->client, TMP451_REG_CONALERT,
  915. data->conalert);
  916. }
  917. return err;
  918. }
  919. static int lm90_update_limits(struct device *dev)
  920. {
  921. struct lm90_data *data = dev_get_drvdata(dev);
  922. struct i2c_client *client = data->client;
  923. int val;
  924. if (data->flags & LM90_HAVE_CRIT) {
  925. val = lm90_read_reg(client, LM90_REG_LOCAL_CRIT);
  926. if (val < 0)
  927. return val;
  928. data->temp[LOCAL_CRIT] = val << 8;
  929. val = lm90_read_reg(client, LM90_REG_REMOTE_CRIT);
  930. if (val < 0)
  931. return val;
  932. data->temp[REMOTE_CRIT] = val << 8;
  933. val = lm90_read_reg(client, LM90_REG_TCRIT_HYST);
  934. if (val < 0)
  935. return val;
  936. data->temp_hyst = val;
  937. }
  938. if ((data->flags & LM90_HAVE_FAULTQUEUE) && !data->faultqueue_mask) {
  939. val = lm90_read_reg(client, TMP451_REG_CONALERT);
  940. if (val < 0)
  941. return val;
  942. data->conalert = val;
  943. }
  944. val = lm90_read16(client, LM90_REG_REMOTE_LOWH,
  945. (data->flags & LM90_HAVE_REM_LIMIT_EXT) ? LM90_REG_REMOTE_LOWL : 0,
  946. false);
  947. if (val < 0)
  948. return val;
  949. data->temp[REMOTE_LOW] = val;
  950. val = lm90_read16(client, LM90_REG_REMOTE_HIGHH,
  951. (data->flags & LM90_HAVE_REM_LIMIT_EXT) ? LM90_REG_REMOTE_HIGHL : 0,
  952. false);
  953. if (val < 0)
  954. return val;
  955. data->temp[REMOTE_HIGH] = val;
  956. if (data->flags & LM90_HAVE_OFFSET) {
  957. val = lm90_read16(client, LM90_REG_REMOTE_OFFSH,
  958. LM90_REG_REMOTE_OFFSL, false);
  959. if (val < 0)
  960. return val;
  961. data->temp[REMOTE_OFFSET] = val;
  962. }
  963. if (data->flags & LM90_HAVE_EMERGENCY) {
  964. val = lm90_read_reg(client, MAX6659_REG_LOCAL_EMERG);
  965. if (val < 0)
  966. return val;
  967. data->temp[LOCAL_EMERG] = val << 8;
  968. val = lm90_read_reg(client, MAX6659_REG_REMOTE_EMERG);
  969. if (val < 0)
  970. return val;
  971. data->temp[REMOTE_EMERG] = val << 8;
  972. }
  973. if (data->flags & LM90_HAVE_TEMP3) {
  974. val = lm90_select_remote_channel(data, true);
  975. if (val < 0)
  976. return val;
  977. val = lm90_read_reg(client, LM90_REG_REMOTE_CRIT);
  978. if (val < 0)
  979. return val;
  980. data->temp[REMOTE2_CRIT] = val << 8;
  981. if (data->flags & LM90_HAVE_EMERGENCY) {
  982. val = lm90_read_reg(client, MAX6659_REG_REMOTE_EMERG);
  983. if (val < 0)
  984. return val;
  985. data->temp[REMOTE2_EMERG] = val << 8;
  986. }
  987. val = lm90_read_reg(client, LM90_REG_REMOTE_LOWH);
  988. if (val < 0)
  989. return val;
  990. data->temp[REMOTE2_LOW] = val << 8;
  991. val = lm90_read_reg(client, LM90_REG_REMOTE_HIGHH);
  992. if (val < 0)
  993. return val;
  994. data->temp[REMOTE2_HIGH] = val << 8;
  995. if (data->flags & LM90_HAVE_OFFSET) {
  996. val = lm90_read16(client, LM90_REG_REMOTE_OFFSH,
  997. LM90_REG_REMOTE_OFFSL, false);
  998. if (val < 0)
  999. return val;
  1000. data->temp[REMOTE2_OFFSET] = val;
  1001. }
  1002. lm90_select_remote_channel(data, false);
  1003. }
  1004. return 0;
  1005. }
  1006. static void lm90_report_alarms(struct work_struct *work)
  1007. {
  1008. struct lm90_data *data = container_of(work, struct lm90_data, report_work);
  1009. u16 cleared_alarms, new_alarms, current_alarms;
  1010. struct device *hwmon_dev = data->hwmon_dev;
  1011. struct device *dev = &data->client->dev;
  1012. int st, st2;
  1013. current_alarms = data->current_alarms;
  1014. cleared_alarms = data->reported_alarms & ~current_alarms;
  1015. new_alarms = current_alarms & ~data->reported_alarms;
  1016. if (!cleared_alarms && !new_alarms)
  1017. return;
  1018. st = new_alarms & 0xff;
  1019. st2 = new_alarms >> 8;
  1020. if ((st & (LM90_STATUS_LLOW | LM90_STATUS_LHIGH | LM90_STATUS_LTHRM)) ||
  1021. (st2 & MAX6696_STATUS2_LOT2))
  1022. dev_dbg(dev, "temp%d out of range, please check!\n", 1);
  1023. if ((st & (LM90_STATUS_RLOW | LM90_STATUS_RHIGH | LM90_STATUS_RTHRM)) ||
  1024. (st2 & MAX6696_STATUS2_ROT2))
  1025. dev_dbg(dev, "temp%d out of range, please check!\n", 2);
  1026. if (st & LM90_STATUS_ROPEN)
  1027. dev_dbg(dev, "temp%d diode open, please check!\n", 2);
  1028. if (st2 & (MAX6696_STATUS2_R2LOW | MAX6696_STATUS2_R2HIGH |
  1029. MAX6696_STATUS2_R2THRM | MAX6696_STATUS2_R2OT2))
  1030. dev_dbg(dev, "temp%d out of range, please check!\n", 3);
  1031. if (st2 & MAX6696_STATUS2_R2OPEN)
  1032. dev_dbg(dev, "temp%d diode open, please check!\n", 3);
  1033. st |= cleared_alarms & 0xff;
  1034. st2 |= cleared_alarms >> 8;
  1035. if (st & LM90_STATUS_LLOW)
  1036. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 0);
  1037. if (st & LM90_STATUS_RLOW)
  1038. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 1);
  1039. if (st2 & MAX6696_STATUS2_R2LOW)
  1040. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_min_alarm, 2);
  1041. if (st & LM90_STATUS_LHIGH)
  1042. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 0);
  1043. if (st & LM90_STATUS_RHIGH)
  1044. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 1);
  1045. if (st2 & MAX6696_STATUS2_R2HIGH)
  1046. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_max_alarm, 2);
  1047. if (st & LM90_STATUS_LTHRM)
  1048. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 0);
  1049. if (st & LM90_STATUS_RTHRM)
  1050. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 1);
  1051. if (st2 & MAX6696_STATUS2_R2THRM)
  1052. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_crit_alarm, 2);
  1053. if (st2 & MAX6696_STATUS2_LOT2)
  1054. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 0);
  1055. if (st2 & MAX6696_STATUS2_ROT2)
  1056. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 1);
  1057. if (st2 & MAX6696_STATUS2_R2OT2)
  1058. hwmon_notify_event(hwmon_dev, hwmon_temp, hwmon_temp_emergency_alarm, 2);
  1059. data->reported_alarms = current_alarms;
  1060. }
  1061. static int lm90_update_alarms_locked(struct lm90_data *data, bool force)
  1062. {
  1063. if (force || !data->alarms_valid ||
  1064. time_after(jiffies, data->alarms_updated + msecs_to_jiffies(data->update_interval))) {
  1065. struct i2c_client *client = data->client;
  1066. bool check_enable;
  1067. u16 alarms;
  1068. int val;
  1069. data->alarms_valid = false;
  1070. val = lm90_read_reg(client, LM90_REG_STATUS);
  1071. if (val < 0)
  1072. return val;
  1073. alarms = val & ~LM90_STATUS_BUSY;
  1074. if (data->reg_status2) {
  1075. val = lm90_read_reg(client, data->reg_status2);
  1076. if (val < 0)
  1077. return val;
  1078. alarms |= val << 8;
  1079. }
  1080. /*
  1081. * If the update is forced (called from interrupt or alert
  1082. * handler) and alarm data is valid, the alarms may have been
  1083. * updated after the last update interval, and the status
  1084. * register may still be cleared. Only add additional alarms
  1085. * in this case. Alarms will be cleared later if appropriate.
  1086. */
  1087. if (force && data->alarms_valid)
  1088. data->current_alarms |= alarms;
  1089. else
  1090. data->current_alarms = alarms;
  1091. data->alarms |= alarms;
  1092. check_enable = (client->irq || !(data->config_orig & 0x80)) &&
  1093. (data->config & 0x80);
  1094. if (force || check_enable)
  1095. schedule_work(&data->report_work);
  1096. /*
  1097. * Re-enable ALERT# output if it was originally enabled, relevant
  1098. * alarms are all clear, and alerts are currently disabled.
  1099. * Otherwise (re)schedule worker if needed.
  1100. */
  1101. if (check_enable) {
  1102. if (!(data->current_alarms & data->alert_alarms)) {
  1103. dev_dbg(&client->dev, "Re-enabling ALERT#\n");
  1104. lm90_update_confreg(data, data->config & ~0x80);
  1105. /*
  1106. * We may have been called from the update handler.
  1107. * If so, the worker, if scheduled, is no longer
  1108. * needed. Cancel it. Don't synchronize because
  1109. * it may already be running.
  1110. */
  1111. cancel_delayed_work(&data->alert_work);
  1112. } else {
  1113. schedule_delayed_work(&data->alert_work,
  1114. max_t(int, HZ, msecs_to_jiffies(data->update_interval)));
  1115. }
  1116. }
  1117. data->alarms_updated = jiffies;
  1118. data->alarms_valid = true;
  1119. }
  1120. return 0;
  1121. }
  1122. static int lm90_update_alarms(struct lm90_data *data, bool force)
  1123. {
  1124. int err;
  1125. hwmon_lock(data->hwmon_dev);
  1126. err = lm90_update_alarms_locked(data, force);
  1127. hwmon_unlock(data->hwmon_dev);
  1128. return err;
  1129. }
  1130. static void lm90_alert_work(struct work_struct *__work)
  1131. {
  1132. struct delayed_work *delayed_work = to_delayed_work(__work);
  1133. struct lm90_data *data = container_of(delayed_work, struct lm90_data, alert_work);
  1134. /* Nothing to do if alerts are enabled */
  1135. if (!(data->config & 0x80))
  1136. return;
  1137. lm90_update_alarms(data, true);
  1138. }
  1139. static int lm90_update_device(struct device *dev)
  1140. {
  1141. struct lm90_data *data = dev_get_drvdata(dev);
  1142. struct i2c_client *client = data->client;
  1143. unsigned long next_update;
  1144. int val;
  1145. if (!data->valid) {
  1146. val = lm90_update_limits(dev);
  1147. if (val < 0)
  1148. return val;
  1149. }
  1150. next_update = data->last_updated +
  1151. msecs_to_jiffies(data->update_interval);
  1152. if (time_after(jiffies, next_update) || !data->valid) {
  1153. dev_dbg(&client->dev, "Updating lm90 data.\n");
  1154. data->valid = false;
  1155. val = lm90_read_reg(client, LM90_REG_LOCAL_LOW);
  1156. if (val < 0)
  1157. return val;
  1158. data->temp[LOCAL_LOW] = val << 8;
  1159. val = lm90_read_reg(client, LM90_REG_LOCAL_HIGH);
  1160. if (val < 0)
  1161. return val;
  1162. data->temp[LOCAL_HIGH] = val << 8;
  1163. val = lm90_read16(client, LM90_REG_LOCAL_TEMP,
  1164. data->reg_local_ext, true);
  1165. if (val < 0)
  1166. return val;
  1167. data->temp[LOCAL_TEMP] = val;
  1168. val = lm90_read16(client, LM90_REG_REMOTE_TEMPH,
  1169. data->reg_remote_ext, true);
  1170. if (val < 0)
  1171. return val;
  1172. data->temp[REMOTE_TEMP] = val;
  1173. if (data->flags & LM90_HAVE_TEMP3) {
  1174. val = lm90_select_remote_channel(data, true);
  1175. if (val < 0)
  1176. return val;
  1177. val = lm90_read16(client, LM90_REG_REMOTE_TEMPH,
  1178. data->reg_remote_ext, true);
  1179. if (val < 0) {
  1180. lm90_select_remote_channel(data, false);
  1181. return val;
  1182. }
  1183. data->temp[REMOTE2_TEMP] = val;
  1184. lm90_select_remote_channel(data, false);
  1185. }
  1186. val = lm90_update_alarms_locked(data, false);
  1187. if (val < 0)
  1188. return val;
  1189. data->last_updated = jiffies;
  1190. data->valid = true;
  1191. }
  1192. return 0;
  1193. }
  1194. static int lm90_temp_get_resolution(struct lm90_data *data, int index)
  1195. {
  1196. switch (index) {
  1197. case REMOTE_TEMP:
  1198. if (data->reg_remote_ext)
  1199. return data->resolution;
  1200. return 8;
  1201. case REMOTE_OFFSET:
  1202. case REMOTE2_OFFSET:
  1203. case REMOTE2_TEMP:
  1204. return data->resolution;
  1205. case LOCAL_TEMP:
  1206. if (data->reg_local_ext)
  1207. return data->resolution;
  1208. return 8;
  1209. case REMOTE_LOW:
  1210. case REMOTE_HIGH:
  1211. case REMOTE2_LOW:
  1212. case REMOTE2_HIGH:
  1213. if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
  1214. return data->resolution;
  1215. return 8;
  1216. default:
  1217. return 8;
  1218. }
  1219. }
  1220. static int lm90_temp_from_reg(u32 flags, u16 regval, u8 resolution)
  1221. {
  1222. int val;
  1223. if (flags & LM90_HAVE_EXTENDED_TEMP)
  1224. val = regval - 0x4000;
  1225. else if (flags & (LM90_HAVE_UNSIGNED_TEMP | LM90_HAVE_EXT_UNSIGNED))
  1226. val = regval;
  1227. else
  1228. val = (s16)regval;
  1229. return ((val >> (16 - resolution)) * 1000) >> (resolution - 8);
  1230. }
  1231. static int lm90_get_temp(struct lm90_data *data, int index, int channel)
  1232. {
  1233. int temp = lm90_temp_from_reg(data->flags, data->temp[index],
  1234. lm90_temp_get_resolution(data, index));
  1235. /* +16 degrees offset for remote temperature on LM99 */
  1236. if (data->kind == lm99 && channel)
  1237. temp += 16000;
  1238. return temp;
  1239. }
  1240. static u16 lm90_temp_to_reg(u32 flags, long val, u8 resolution)
  1241. {
  1242. int fraction = resolution > 8 ?
  1243. 1000 - DIV_ROUND_CLOSEST(1000, BIT(resolution - 8)) : 0;
  1244. if (flags & LM90_HAVE_EXTENDED_TEMP) {
  1245. val = clamp_val(val, -64000, 191000 + fraction);
  1246. val += 64000;
  1247. } else if (flags & LM90_HAVE_EXT_UNSIGNED) {
  1248. val = clamp_val(val, 0, 255000 + fraction);
  1249. } else if (flags & LM90_HAVE_UNSIGNED_TEMP) {
  1250. val = clamp_val(val, 0, 127000 + fraction);
  1251. } else {
  1252. val = clamp_val(val, -128000, 127000 + fraction);
  1253. }
  1254. return DIV_ROUND_CLOSEST(val << (resolution - 8), 1000) << (16 - resolution);
  1255. }
  1256. static int lm90_set_temp(struct lm90_data *data, int index, int channel, long val)
  1257. {
  1258. static const u8 regs[] = {
  1259. [LOCAL_LOW] = LM90_REG_LOCAL_LOW,
  1260. [LOCAL_HIGH] = LM90_REG_LOCAL_HIGH,
  1261. [LOCAL_CRIT] = LM90_REG_LOCAL_CRIT,
  1262. [REMOTE_CRIT] = LM90_REG_REMOTE_CRIT,
  1263. [LOCAL_EMERG] = MAX6659_REG_LOCAL_EMERG,
  1264. [REMOTE_EMERG] = MAX6659_REG_REMOTE_EMERG,
  1265. [REMOTE2_CRIT] = LM90_REG_REMOTE_CRIT,
  1266. [REMOTE2_EMERG] = MAX6659_REG_REMOTE_EMERG,
  1267. [REMOTE_LOW] = LM90_REG_REMOTE_LOWH,
  1268. [REMOTE_HIGH] = LM90_REG_REMOTE_HIGHH,
  1269. [REMOTE2_LOW] = LM90_REG_REMOTE_LOWH,
  1270. [REMOTE2_HIGH] = LM90_REG_REMOTE_HIGHH,
  1271. };
  1272. struct i2c_client *client = data->client;
  1273. u8 regh = regs[index];
  1274. u8 regl = 0;
  1275. int err;
  1276. if (channel && (data->flags & LM90_HAVE_REM_LIMIT_EXT)) {
  1277. if (index == REMOTE_LOW || index == REMOTE2_LOW)
  1278. regl = LM90_REG_REMOTE_LOWL;
  1279. else if (index == REMOTE_HIGH || index == REMOTE2_HIGH)
  1280. regl = LM90_REG_REMOTE_HIGHL;
  1281. }
  1282. /* +16 degrees offset for remote temperature on LM99 */
  1283. if (data->kind == lm99 && channel) {
  1284. /* prevent integer underflow */
  1285. val = max(val, -128000l);
  1286. val -= 16000;
  1287. }
  1288. data->temp[index] = lm90_temp_to_reg(data->flags, val,
  1289. lm90_temp_get_resolution(data, index));
  1290. if (channel > 1)
  1291. lm90_select_remote_channel(data, true);
  1292. err = lm90_write16(client, regh, regl, data->temp[index]);
  1293. if (channel > 1)
  1294. lm90_select_remote_channel(data, false);
  1295. return err;
  1296. }
  1297. static int lm90_get_temphyst(struct lm90_data *data, int index, int channel)
  1298. {
  1299. int temp = lm90_get_temp(data, index, channel);
  1300. return temp - data->temp_hyst * 1000;
  1301. }
  1302. static int lm90_set_temphyst(struct lm90_data *data, long val)
  1303. {
  1304. int temp = lm90_get_temp(data, LOCAL_CRIT, 0);
  1305. /* prevent integer overflow/underflow */
  1306. val = clamp_val(val, -128000l, 255000l);
  1307. data->temp_hyst = clamp_val(DIV_ROUND_CLOSEST(temp - val, 1000), 0, 31);
  1308. return lm90_write_reg(data->client, LM90_REG_TCRIT_HYST, data->temp_hyst);
  1309. }
  1310. static int lm90_get_temp_offset(struct lm90_data *data, int index)
  1311. {
  1312. int res = lm90_temp_get_resolution(data, index);
  1313. return lm90_temp_from_reg(0, data->temp[index], res);
  1314. }
  1315. static int lm90_set_temp_offset(struct lm90_data *data, int index, int channel, long val)
  1316. {
  1317. int err;
  1318. val = lm90_temp_to_reg(0, val, lm90_temp_get_resolution(data, index));
  1319. /* For ADT7481 we can use the same registers for remote channel 1 and 2 */
  1320. if (channel > 1)
  1321. lm90_select_remote_channel(data, true);
  1322. err = lm90_write16(data->client, LM90_REG_REMOTE_OFFSH, LM90_REG_REMOTE_OFFSL, val);
  1323. if (channel > 1)
  1324. lm90_select_remote_channel(data, false);
  1325. if (err)
  1326. return err;
  1327. data->temp[index] = val;
  1328. return 0;
  1329. }
  1330. static const u8 lm90_temp_index[MAX_CHANNELS] = {
  1331. LOCAL_TEMP, REMOTE_TEMP, REMOTE2_TEMP
  1332. };
  1333. static const u8 lm90_temp_min_index[MAX_CHANNELS] = {
  1334. LOCAL_LOW, REMOTE_LOW, REMOTE2_LOW
  1335. };
  1336. static const u8 lm90_temp_max_index[MAX_CHANNELS] = {
  1337. LOCAL_HIGH, REMOTE_HIGH, REMOTE2_HIGH
  1338. };
  1339. static const u8 lm90_temp_crit_index[MAX_CHANNELS] = {
  1340. LOCAL_CRIT, REMOTE_CRIT, REMOTE2_CRIT
  1341. };
  1342. static const u8 lm90_temp_emerg_index[MAX_CHANNELS] = {
  1343. LOCAL_EMERG, REMOTE_EMERG, REMOTE2_EMERG
  1344. };
  1345. static const s8 lm90_temp_offset_index[MAX_CHANNELS] = {
  1346. -1, REMOTE_OFFSET, REMOTE2_OFFSET
  1347. };
  1348. static const u16 lm90_min_alarm_bits[MAX_CHANNELS] = { BIT(5), BIT(3), BIT(11) };
  1349. static const u16 lm90_max_alarm_bits[MAX_CHANNELS] = { BIT(6), BIT(4), BIT(12) };
  1350. static const u16 lm90_crit_alarm_bits[MAX_CHANNELS] = { BIT(0), BIT(1), BIT(9) };
  1351. static const u16 lm90_crit_alarm_bits_swapped[MAX_CHANNELS] = { BIT(1), BIT(0), BIT(9) };
  1352. static const u16 lm90_emergency_alarm_bits[MAX_CHANNELS] = { BIT(15), BIT(13), BIT(14) };
  1353. static const u16 lm90_fault_bits[MAX_CHANNELS] = { BIT(0), BIT(2), BIT(10) };
  1354. static int lm90_temp_read(struct device *dev, u32 attr, int channel, long *val)
  1355. {
  1356. struct lm90_data *data = dev_get_drvdata(dev);
  1357. int err;
  1358. u16 bit;
  1359. err = lm90_update_device(dev);
  1360. if (err)
  1361. return err;
  1362. switch (attr) {
  1363. case hwmon_temp_input:
  1364. *val = lm90_get_temp(data, lm90_temp_index[channel], channel);
  1365. break;
  1366. case hwmon_temp_min_alarm:
  1367. case hwmon_temp_max_alarm:
  1368. case hwmon_temp_crit_alarm:
  1369. case hwmon_temp_emergency_alarm:
  1370. case hwmon_temp_fault:
  1371. switch (attr) {
  1372. case hwmon_temp_min_alarm:
  1373. bit = lm90_min_alarm_bits[channel];
  1374. break;
  1375. case hwmon_temp_max_alarm:
  1376. bit = lm90_max_alarm_bits[channel];
  1377. break;
  1378. case hwmon_temp_crit_alarm:
  1379. if (data->flags & LM90_HAVE_CRIT_ALRM_SWP)
  1380. bit = lm90_crit_alarm_bits_swapped[channel];
  1381. else
  1382. bit = lm90_crit_alarm_bits[channel];
  1383. break;
  1384. case hwmon_temp_emergency_alarm:
  1385. bit = lm90_emergency_alarm_bits[channel];
  1386. break;
  1387. case hwmon_temp_fault:
  1388. bit = lm90_fault_bits[channel];
  1389. break;
  1390. }
  1391. *val = !!(data->alarms & bit);
  1392. data->alarms &= ~bit;
  1393. data->alarms |= data->current_alarms;
  1394. break;
  1395. case hwmon_temp_min:
  1396. *val = lm90_get_temp(data, lm90_temp_min_index[channel], channel);
  1397. break;
  1398. case hwmon_temp_max:
  1399. *val = lm90_get_temp(data, lm90_temp_max_index[channel], channel);
  1400. break;
  1401. case hwmon_temp_crit:
  1402. *val = lm90_get_temp(data, lm90_temp_crit_index[channel], channel);
  1403. break;
  1404. case hwmon_temp_crit_hyst:
  1405. *val = lm90_get_temphyst(data, lm90_temp_crit_index[channel], channel);
  1406. break;
  1407. case hwmon_temp_emergency:
  1408. *val = lm90_get_temp(data, lm90_temp_emerg_index[channel], channel);
  1409. break;
  1410. case hwmon_temp_emergency_hyst:
  1411. *val = lm90_get_temphyst(data, lm90_temp_emerg_index[channel], channel);
  1412. break;
  1413. case hwmon_temp_offset:
  1414. *val = lm90_get_temp_offset(data, lm90_temp_offset_index[channel]);
  1415. break;
  1416. default:
  1417. return -EOPNOTSUPP;
  1418. }
  1419. return 0;
  1420. }
  1421. static int lm90_temp_write(struct device *dev, u32 attr, int channel, long val)
  1422. {
  1423. struct lm90_data *data = dev_get_drvdata(dev);
  1424. int err;
  1425. err = lm90_update_device(dev);
  1426. if (err)
  1427. return err;
  1428. switch (attr) {
  1429. case hwmon_temp_min:
  1430. err = lm90_set_temp(data, lm90_temp_min_index[channel],
  1431. channel, val);
  1432. break;
  1433. case hwmon_temp_max:
  1434. err = lm90_set_temp(data, lm90_temp_max_index[channel],
  1435. channel, val);
  1436. break;
  1437. case hwmon_temp_crit:
  1438. err = lm90_set_temp(data, lm90_temp_crit_index[channel],
  1439. channel, val);
  1440. break;
  1441. case hwmon_temp_crit_hyst:
  1442. err = lm90_set_temphyst(data, val);
  1443. break;
  1444. case hwmon_temp_emergency:
  1445. err = lm90_set_temp(data, lm90_temp_emerg_index[channel],
  1446. channel, val);
  1447. break;
  1448. case hwmon_temp_offset:
  1449. err = lm90_set_temp_offset(data, lm90_temp_offset_index[channel],
  1450. channel, val);
  1451. break;
  1452. default:
  1453. err = -EOPNOTSUPP;
  1454. break;
  1455. }
  1456. return err;
  1457. }
  1458. static umode_t lm90_temp_is_visible(const void *data, u32 attr, int channel)
  1459. {
  1460. switch (attr) {
  1461. case hwmon_temp_input:
  1462. case hwmon_temp_min_alarm:
  1463. case hwmon_temp_max_alarm:
  1464. case hwmon_temp_crit_alarm:
  1465. case hwmon_temp_emergency_alarm:
  1466. case hwmon_temp_emergency_hyst:
  1467. case hwmon_temp_fault:
  1468. case hwmon_temp_label:
  1469. return 0444;
  1470. case hwmon_temp_min:
  1471. case hwmon_temp_max:
  1472. case hwmon_temp_crit:
  1473. case hwmon_temp_emergency:
  1474. case hwmon_temp_offset:
  1475. return 0644;
  1476. case hwmon_temp_crit_hyst:
  1477. if (channel == 0)
  1478. return 0644;
  1479. return 0444;
  1480. default:
  1481. return 0;
  1482. }
  1483. }
  1484. static int lm90_chip_read(struct device *dev, u32 attr, int channel, long *val)
  1485. {
  1486. struct lm90_data *data = dev_get_drvdata(dev);
  1487. int err;
  1488. err = lm90_update_device(dev);
  1489. if (err)
  1490. return err;
  1491. switch (attr) {
  1492. case hwmon_chip_update_interval:
  1493. *val = data->update_interval;
  1494. break;
  1495. case hwmon_chip_alarms:
  1496. *val = data->alarms;
  1497. break;
  1498. case hwmon_chip_temp_samples:
  1499. if (data->faultqueue_mask) {
  1500. *val = (data->config & data->faultqueue_mask) ?
  1501. data->faultqueue_depth : 1;
  1502. } else {
  1503. switch (data->conalert & 0x0e) {
  1504. case 0x0:
  1505. default:
  1506. *val = 1;
  1507. break;
  1508. case 0x2:
  1509. *val = 2;
  1510. break;
  1511. case 0x6:
  1512. *val = 3;
  1513. break;
  1514. case 0xe:
  1515. *val = 4;
  1516. break;
  1517. }
  1518. }
  1519. break;
  1520. default:
  1521. return -EOPNOTSUPP;
  1522. }
  1523. return 0;
  1524. }
  1525. static int lm90_chip_write(struct device *dev, u32 attr, int channel, long val)
  1526. {
  1527. struct lm90_data *data = dev_get_drvdata(dev);
  1528. struct i2c_client *client = data->client;
  1529. int err;
  1530. err = lm90_update_device(dev);
  1531. if (err)
  1532. return err;
  1533. switch (attr) {
  1534. case hwmon_chip_update_interval:
  1535. err = lm90_set_convrate(client, data,
  1536. clamp_val(val, 0, 100000));
  1537. break;
  1538. case hwmon_chip_temp_samples:
  1539. err = lm90_set_faultqueue(client, data, clamp_val(val, 1, 4));
  1540. break;
  1541. default:
  1542. err = -EOPNOTSUPP;
  1543. break;
  1544. }
  1545. return err;
  1546. }
  1547. static umode_t lm90_chip_is_visible(const void *data, u32 attr, int channel)
  1548. {
  1549. switch (attr) {
  1550. case hwmon_chip_update_interval:
  1551. case hwmon_chip_temp_samples:
  1552. return 0644;
  1553. case hwmon_chip_alarms:
  1554. return 0444;
  1555. default:
  1556. return 0;
  1557. }
  1558. }
  1559. static int lm90_read(struct device *dev, enum hwmon_sensor_types type,
  1560. u32 attr, int channel, long *val)
  1561. {
  1562. switch (type) {
  1563. case hwmon_chip:
  1564. return lm90_chip_read(dev, attr, channel, val);
  1565. case hwmon_temp:
  1566. return lm90_temp_read(dev, attr, channel, val);
  1567. default:
  1568. return -EOPNOTSUPP;
  1569. }
  1570. }
  1571. static int lm90_read_string(struct device *dev, enum hwmon_sensor_types type,
  1572. u32 attr, int channel, const char **str)
  1573. {
  1574. struct lm90_data *data = dev_get_drvdata(dev);
  1575. *str = data->channel_label[channel];
  1576. return 0;
  1577. }
  1578. static int lm90_write(struct device *dev, enum hwmon_sensor_types type,
  1579. u32 attr, int channel, long val)
  1580. {
  1581. switch (type) {
  1582. case hwmon_chip:
  1583. return lm90_chip_write(dev, attr, channel, val);
  1584. case hwmon_temp:
  1585. return lm90_temp_write(dev, attr, channel, val);
  1586. default:
  1587. return -EOPNOTSUPP;
  1588. }
  1589. }
  1590. static umode_t lm90_is_visible(const void *data, enum hwmon_sensor_types type,
  1591. u32 attr, int channel)
  1592. {
  1593. switch (type) {
  1594. case hwmon_chip:
  1595. return lm90_chip_is_visible(data, attr, channel);
  1596. case hwmon_temp:
  1597. return lm90_temp_is_visible(data, attr, channel);
  1598. default:
  1599. return 0;
  1600. }
  1601. }
  1602. static const char *lm90_detect_lm84(struct i2c_client *client)
  1603. {
  1604. static const u8 regs[] = {
  1605. LM90_REG_STATUS, LM90_REG_LOCAL_TEMP, LM90_REG_LOCAL_HIGH,
  1606. LM90_REG_REMOTE_TEMPH, LM90_REG_REMOTE_HIGHH
  1607. };
  1608. int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
  1609. int reg1, reg2, reg3, reg4;
  1610. bool nonzero = false;
  1611. u8 ff = 0xff;
  1612. int i;
  1613. if (status < 0 || (status & 0xab))
  1614. return NULL;
  1615. /*
  1616. * For LM84, undefined registers return the most recent value.
  1617. * Repeat several times, each time checking against a different
  1618. * (presumably) existing register.
  1619. */
  1620. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  1621. reg1 = i2c_smbus_read_byte_data(client, regs[i]);
  1622. reg2 = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL);
  1623. reg3 = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW);
  1624. reg4 = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH);
  1625. if (reg1 < 0)
  1626. return NULL;
  1627. /* If any register has a different value, this is not an LM84 */
  1628. if (reg2 != reg1 || reg3 != reg1 || reg4 != reg1)
  1629. return NULL;
  1630. nonzero |= reg1 || reg2 || reg3 || reg4;
  1631. ff &= reg1;
  1632. }
  1633. /*
  1634. * If all registers always returned 0 or 0xff, all bets are off,
  1635. * and we can not make any predictions about the chip type.
  1636. */
  1637. return nonzero && ff != 0xff ? "lm84" : NULL;
  1638. }
  1639. static const char *lm90_detect_max1617(struct i2c_client *client, int config1)
  1640. {
  1641. int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
  1642. int llo, rlo, lhi, rhi;
  1643. if (status < 0 || (status & 0x03))
  1644. return NULL;
  1645. if (config1 & 0x3f)
  1646. return NULL;
  1647. /*
  1648. * Fail if unsupported registers return anything but 0xff.
  1649. * The calling code already checked man_id and chip_id.
  1650. * A byte read operation repeats the most recent read operation
  1651. * and should also return 0xff.
  1652. */
  1653. if (i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL) != 0xff ||
  1654. i2c_smbus_read_byte_data(client, MAX6657_REG_LOCAL_TEMPL) != 0xff ||
  1655. i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWL) != 0xff ||
  1656. i2c_smbus_read_byte(client) != 0xff)
  1657. return NULL;
  1658. llo = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW);
  1659. rlo = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH);
  1660. lhi = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_HIGH);
  1661. rhi = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_HIGHH);
  1662. if (llo < 0 || rlo < 0)
  1663. return NULL;
  1664. /*
  1665. * A byte read operation repeats the most recent read and should
  1666. * return the same value.
  1667. */
  1668. if (i2c_smbus_read_byte(client) != rhi)
  1669. return NULL;
  1670. /*
  1671. * The following two checks are marginal since the checked values
  1672. * are strictly speaking valid.
  1673. */
  1674. /* fail for negative high limits; this also catches read errors */
  1675. if ((s8)lhi < 0 || (s8)rhi < 0)
  1676. return NULL;
  1677. /* fail if low limits are larger than or equal to high limits */
  1678. if ((s8)llo >= lhi || (s8)rlo >= rhi)
  1679. return NULL;
  1680. if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  1681. /*
  1682. * Word read operations return 0xff in second byte
  1683. */
  1684. if (i2c_smbus_read_word_data(client, LM90_REG_REMOTE_TEMPL) !=
  1685. 0xffff)
  1686. return NULL;
  1687. if (i2c_smbus_read_word_data(client, LM90_REG_CONFIG1) !=
  1688. (config1 | 0xff00))
  1689. return NULL;
  1690. if (i2c_smbus_read_word_data(client, LM90_REG_LOCAL_HIGH) !=
  1691. (lhi | 0xff00))
  1692. return NULL;
  1693. }
  1694. return "max1617";
  1695. }
  1696. static const char *lm90_detect_national(struct i2c_client *client, int chip_id,
  1697. int config1, int convrate)
  1698. {
  1699. int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
  1700. int address = client->addr;
  1701. const char *name = NULL;
  1702. if (config2 < 0)
  1703. return NULL;
  1704. if ((config1 & 0x2a) || (config2 & 0xf8) || convrate > 0x09)
  1705. return NULL;
  1706. if (address != 0x4c && address != 0x4d)
  1707. return NULL;
  1708. switch (chip_id & 0xf0) {
  1709. case 0x10: /* LM86 */
  1710. if (address == 0x4c)
  1711. name = "lm86";
  1712. break;
  1713. case 0x20: /* LM90 */
  1714. if (address == 0x4c)
  1715. name = "lm90";
  1716. break;
  1717. case 0x30: /* LM89/LM99 */
  1718. name = "lm99"; /* detect LM89 as LM99 */
  1719. break;
  1720. default:
  1721. break;
  1722. }
  1723. return name;
  1724. }
  1725. static const char *lm90_detect_on(struct i2c_client *client, int chip_id, int config1,
  1726. int convrate)
  1727. {
  1728. int address = client->addr;
  1729. const char *name = NULL;
  1730. switch (chip_id) {
  1731. case 0xca: /* NCT218 */
  1732. if ((address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
  1733. convrate <= 0x0a)
  1734. name = "nct218";
  1735. break;
  1736. default:
  1737. break;
  1738. }
  1739. return name;
  1740. }
  1741. static const char *lm90_detect_analog(struct i2c_client *client, bool common_address,
  1742. int chip_id, int config1, int convrate)
  1743. {
  1744. int status = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
  1745. int config2 = i2c_smbus_read_byte_data(client, ADT7481_REG_CONFIG2);
  1746. int man_id2 = i2c_smbus_read_byte_data(client, ADT7481_REG_MAN_ID);
  1747. int chip_id2 = i2c_smbus_read_byte_data(client, ADT7481_REG_CHIP_ID);
  1748. int address = client->addr;
  1749. const char *name = NULL;
  1750. if (status < 0 || config2 < 0 || man_id2 < 0 || chip_id2 < 0)
  1751. return NULL;
  1752. /*
  1753. * The following chips should be detected by this function. Known
  1754. * register values are listed. Registers 0x3d .. 0x3e are undocumented
  1755. * for most of the chips, yet appear to return a well defined value.
  1756. * Register 0xff is undocumented for some of the chips. Register 0x3f
  1757. * is undocumented for all chips, but also returns a well defined value.
  1758. * Values are as reported from real chips unless mentioned otherwise.
  1759. * The code below checks values for registers 0x3d, 0x3e, and 0xff,
  1760. * but not for register 0x3f.
  1761. *
  1762. * Chip Register
  1763. * 3d 3e 3f fe ff Notes
  1764. * ----------------------------------------------------------
  1765. * adm1020 00 00 00 41 39
  1766. * adm1021 00 00 00 41 03
  1767. * adm1021a 00 00 00 41 3c
  1768. * adm1023 00 00 00 41 3c same as adm1021a
  1769. * adm1032 00 00 00 41 42
  1770. *
  1771. * adt7421 21 41 04 41 04
  1772. * adt7461 00 00 00 41 51
  1773. * adt7461a 61 41 05 41 57
  1774. * adt7481 81 41 02 41 62
  1775. * adt7482 - - - 41 65 datasheet
  1776. * 82 41 05 41 75 real chip
  1777. * adt7483 83 41 04 41 94
  1778. *
  1779. * nct72 61 41 07 41 55
  1780. * nct210 00 00 00 41 3f
  1781. * nct214 61 41 08 41 5a
  1782. * nct1008 - - - 41 57 datasheet rev. 3
  1783. * 61 41 06 41 54 real chip
  1784. *
  1785. * nvt210 - - - 41 - datasheet
  1786. * nvt211 - - - 41 - datasheet
  1787. */
  1788. switch (chip_id) {
  1789. case 0x00 ... 0x03: /* ADM1021 */
  1790. case 0x05 ... 0x0f:
  1791. if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
  1792. !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
  1793. name = "adm1021";
  1794. break;
  1795. case 0x04: /* ADT7421 (undocumented) */
  1796. if (man_id2 == 0x41 && chip_id2 == 0x21 &&
  1797. (address == 0x4c || address == 0x4d) &&
  1798. (config1 & 0x0b) == 0x08 && convrate <= 0x0a)
  1799. name = "adt7421";
  1800. break;
  1801. case 0x30 ... 0x38: /* ADM1021A, ADM1023 */
  1802. case 0x3a ... 0x3e:
  1803. /*
  1804. * ADM1021A and compatible chips will be mis-detected as
  1805. * ADM1023. Chips labeled 'ADM1021A' and 'ADM1023' were both
  1806. * found to have a Chip ID of 0x3c.
  1807. * ADM1021A does not officially support low byte registers
  1808. * (0x12 .. 0x14), but a chip labeled ADM1021A does support it.
  1809. * Official support for the temperature offset high byte
  1810. * register (0x11) was added to revision F of the ADM1021A
  1811. * datasheet.
  1812. * It is currently unknown if there is a means to distinguish
  1813. * ADM1021A from ADM1023, and/or if revisions of ADM1021A exist
  1814. * which differ in functionality from ADM1023.
  1815. */
  1816. if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
  1817. !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
  1818. name = "adm1023";
  1819. break;
  1820. case 0x39: /* ADM1020 (undocumented) */
  1821. if (man_id2 == 0x00 && chip_id2 == 0x00 &&
  1822. (address == 0x4c || address == 0x4d || address == 0x4e) &&
  1823. !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
  1824. name = "adm1020";
  1825. break;
  1826. case 0x3f: /* NCT210 */
  1827. if (man_id2 == 0x00 && chip_id2 == 0x00 && common_address &&
  1828. !(status & 0x03) && !(config1 & 0x3f) && !(convrate & 0xf8))
  1829. name = "nct210";
  1830. break;
  1831. case 0x40 ... 0x4f: /* ADM1032 */
  1832. if (man_id2 == 0x00 && chip_id2 == 0x00 &&
  1833. (address == 0x4c || address == 0x4d) && !(config1 & 0x3f) &&
  1834. convrate <= 0x0a)
  1835. name = "adm1032";
  1836. break;
  1837. case 0x51: /* ADT7461 */
  1838. if (man_id2 == 0x00 && chip_id2 == 0x00 &&
  1839. (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
  1840. convrate <= 0x0a)
  1841. name = "adt7461";
  1842. break;
  1843. case 0x54: /* NCT1008 */
  1844. if (man_id2 == 0x41 && chip_id2 == 0x61 &&
  1845. (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
  1846. convrate <= 0x0a)
  1847. name = "nct1008";
  1848. break;
  1849. case 0x55: /* NCT72 */
  1850. if (man_id2 == 0x41 && chip_id2 == 0x61 &&
  1851. (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
  1852. convrate <= 0x0a)
  1853. name = "nct72";
  1854. break;
  1855. case 0x57: /* ADT7461A, NCT1008 (datasheet rev. 3) */
  1856. if (man_id2 == 0x41 && chip_id2 == 0x61 &&
  1857. (address == 0x4c || address == 0x4d) && !(config1 & 0x1b) &&
  1858. convrate <= 0x0a)
  1859. name = "adt7461a";
  1860. break;
  1861. case 0x5a: /* NCT214 */
  1862. if (man_id2 == 0x41 && chip_id2 == 0x61 &&
  1863. common_address && !(config1 & 0x1b) && convrate <= 0x0a)
  1864. name = "nct214";
  1865. break;
  1866. case 0x62: /* ADT7481, undocumented */
  1867. if (man_id2 == 0x41 && chip_id2 == 0x81 &&
  1868. (address == 0x4b || address == 0x4c) && !(config1 & 0x10) &&
  1869. !(config2 & 0x7f) && (convrate & 0x0f) <= 0x0b) {
  1870. name = "adt7481";
  1871. }
  1872. break;
  1873. case 0x65: /* ADT7482, datasheet */
  1874. case 0x75: /* ADT7482, real chip */
  1875. if (man_id2 == 0x41 && chip_id2 == 0x82 &&
  1876. address == 0x4c && !(config1 & 0x10) && !(config2 & 0x7f) &&
  1877. convrate <= 0x0a)
  1878. name = "adt7482";
  1879. break;
  1880. case 0x94: /* ADT7483 */
  1881. if (man_id2 == 0x41 && chip_id2 == 0x83 &&
  1882. common_address &&
  1883. ((address >= 0x18 && address <= 0x1a) ||
  1884. (address >= 0x29 && address <= 0x2b) ||
  1885. (address >= 0x4c && address <= 0x4e)) &&
  1886. !(config1 & 0x10) && !(config2 & 0x7f) && convrate <= 0x0a)
  1887. name = "adt7483a";
  1888. break;
  1889. default:
  1890. break;
  1891. }
  1892. return name;
  1893. }
  1894. static const char *lm90_detect_maxim(struct i2c_client *client, bool common_address,
  1895. int chip_id, int config1, int convrate)
  1896. {
  1897. int man_id, emerg, emerg2, status2;
  1898. int address = client->addr;
  1899. const char *name = NULL;
  1900. switch (chip_id) {
  1901. case 0x01:
  1902. if (!common_address)
  1903. break;
  1904. /*
  1905. * We read MAX6659_REG_REMOTE_EMERG twice, and re-read
  1906. * LM90_REG_MAN_ID in between. If MAX6659_REG_REMOTE_EMERG
  1907. * exists, both readings will reflect the same value. Otherwise,
  1908. * the readings will be different.
  1909. */
  1910. emerg = i2c_smbus_read_byte_data(client,
  1911. MAX6659_REG_REMOTE_EMERG);
  1912. man_id = i2c_smbus_read_byte_data(client,
  1913. LM90_REG_MAN_ID);
  1914. emerg2 = i2c_smbus_read_byte_data(client,
  1915. MAX6659_REG_REMOTE_EMERG);
  1916. status2 = i2c_smbus_read_byte_data(client,
  1917. MAX6696_REG_STATUS2);
  1918. if (emerg < 0 || man_id < 0 || emerg2 < 0 || status2 < 0)
  1919. return NULL;
  1920. /*
  1921. * Even though MAX6695 and MAX6696 do not have a chip ID
  1922. * register, reading it returns 0x01. Bit 4 of the config1
  1923. * register is unused and should return zero when read. Bit 0 of
  1924. * the status2 register is unused and should return zero when
  1925. * read.
  1926. *
  1927. * MAX6695 and MAX6696 have an additional set of temperature
  1928. * limit registers. We can detect those chips by checking if
  1929. * one of those registers exists.
  1930. */
  1931. if (!(config1 & 0x10) && !(status2 & 0x01) && emerg == emerg2 &&
  1932. convrate <= 0x07)
  1933. name = "max6696";
  1934. /*
  1935. * The chip_id register of the MAX6680 and MAX6681 holds the
  1936. * revision of the chip. The lowest bit of the config1 register
  1937. * is unused and should return zero when read, so should the
  1938. * second to last bit of config1 (software reset). Register
  1939. * address 0x12 (LM90_REG_REMOTE_OFFSL) exists for this chip and
  1940. * should differ from emerg2, and emerg2 should match man_id
  1941. * since it does not exist.
  1942. */
  1943. else if (!(config1 & 0x03) && convrate <= 0x07 &&
  1944. emerg2 == man_id && emerg2 != status2)
  1945. name = "max6680";
  1946. /*
  1947. * MAX1617A does not have any extended registers (register
  1948. * address 0x10 or higher) except for manufacturer and
  1949. * device ID registers. Unlike other chips of this series,
  1950. * unsupported registers were observed to return a fixed value
  1951. * of 0x01.
  1952. * Note: Multiple chips with different markings labeled as
  1953. * "MAX1617" (no "A") were observed to report manufacturer ID
  1954. * 0x4d and device ID 0x01. It is unknown if other variants of
  1955. * MAX1617/MAX617A with different behavior exist. The detection
  1956. * code below works for those chips.
  1957. */
  1958. else if (!(config1 & 0x03f) && convrate <= 0x07 &&
  1959. emerg == 0x01 && emerg2 == 0x01 && status2 == 0x01)
  1960. name = "max1617";
  1961. break;
  1962. case 0x08:
  1963. /*
  1964. * The chip_id of the MAX6654 holds the revision of the chip.
  1965. * The lowest 3 bits of the config1 register are unused and
  1966. * should return zero when read.
  1967. */
  1968. if (common_address && !(config1 & 0x07) && convrate <= 0x07)
  1969. name = "max6654";
  1970. break;
  1971. case 0x09:
  1972. /*
  1973. * The chip_id of the MAX6690 holds the revision of the chip.
  1974. * The lowest 3 bits of the config1 register are unused and
  1975. * should return zero when read.
  1976. * Note that MAX6654 and MAX6690 are practically the same chips.
  1977. * The only diference is the rated accuracy. Rev. 1 of the
  1978. * MAX6690 datasheet lists a chip ID of 0x08, and a chip labeled
  1979. * MAX6654 was observed to have a chip ID of 0x09.
  1980. */
  1981. if (common_address && !(config1 & 0x07) && convrate <= 0x07)
  1982. name = "max6690";
  1983. break;
  1984. case 0x4d:
  1985. /*
  1986. * MAX6642, MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
  1987. * register. Reading from that address will return the last
  1988. * read value, which in our case is those of the man_id
  1989. * register, or 0x4d.
  1990. * MAX6642 does not have a conversion rate register, nor low
  1991. * limit registers. Reading from those registers returns the
  1992. * last read value.
  1993. *
  1994. * For MAX6657, MAX6658 and MAX6659, the config1 register lacks
  1995. * a low nibble, so the value will be those of the previous
  1996. * read, so in our case again those of the man_id register.
  1997. * MAX6659 has a third set of upper temperature limit registers.
  1998. * Those registers also return values on MAX6657 and MAX6658,
  1999. * thus the only way to detect MAX6659 is by its address.
  2000. * For this reason it will be mis-detected as MAX6657 if its
  2001. * address is 0x4c.
  2002. */
  2003. if (address >= 0x48 && address <= 0x4f && config1 == convrate &&
  2004. !(config1 & 0x0f)) {
  2005. int regval;
  2006. /*
  2007. * We know that this is not a MAX6657/58/59 because its
  2008. * configuration register has the wrong value and it does
  2009. * not appear to have a conversion rate register.
  2010. */
  2011. /* re-read manufacturer ID to have a good baseline */
  2012. if (i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID) != 0x4d)
  2013. break;
  2014. /* check various non-existing registers */
  2015. if (i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE) != 0x4d ||
  2016. i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW) != 0x4d ||
  2017. i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH) != 0x4d)
  2018. break;
  2019. /* check for unused status register bits */
  2020. regval = i2c_smbus_read_byte_data(client, LM90_REG_STATUS);
  2021. if (regval < 0 || (regval & 0x2b))
  2022. break;
  2023. /* re-check unsupported registers */
  2024. if (i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE) != regval ||
  2025. i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_LOW) != regval ||
  2026. i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_LOWH) != regval)
  2027. break;
  2028. name = "max6642";
  2029. } else if ((address == 0x4c || address == 0x4d || address == 0x4e) &&
  2030. (config1 & 0x1f) == 0x0d && convrate <= 0x09) {
  2031. if (address == 0x4c)
  2032. name = "max6657";
  2033. else
  2034. name = "max6659";
  2035. }
  2036. break;
  2037. case 0x59:
  2038. /*
  2039. * The chip_id register of the MAX6646/6647/6649 holds the
  2040. * revision of the chip. The lowest 6 bits of the config1
  2041. * register are unused and should return zero when read.
  2042. * The I2C address of MAX6648/6692 is fixed at 0x4c.
  2043. * MAX6646 is at address 0x4d, MAX6647 is at address 0x4e,
  2044. * and MAX6649 is at address 0x4c. A slight difference between
  2045. * the two sets of chips is that the remote temperature register
  2046. * reports different values if the DXP pin is open or shorted.
  2047. * We can use that information to help distinguish between the
  2048. * chips. MAX6648 will be mis-detected as MAX6649 if the remote
  2049. * diode is connected, but there isn't really anything we can
  2050. * do about that.
  2051. */
  2052. if (!(config1 & 0x3f) && convrate <= 0x07) {
  2053. int temp;
  2054. switch (address) {
  2055. case 0x4c:
  2056. /*
  2057. * MAX6649 reports an external temperature
  2058. * value of 0xff if DXP is open or shorted.
  2059. * MAX6648 reports 0x80 in that case.
  2060. */
  2061. temp = i2c_smbus_read_byte_data(client,
  2062. LM90_REG_REMOTE_TEMPH);
  2063. if (temp == 0x80)
  2064. name = "max6648";
  2065. else
  2066. name = "max6649";
  2067. break;
  2068. case 0x4d:
  2069. name = "max6646";
  2070. break;
  2071. case 0x4e:
  2072. name = "max6647";
  2073. break;
  2074. default:
  2075. break;
  2076. }
  2077. }
  2078. break;
  2079. default:
  2080. break;
  2081. }
  2082. return name;
  2083. }
  2084. static const char *lm90_detect_nuvoton(struct i2c_client *client, int chip_id,
  2085. int config1, int convrate)
  2086. {
  2087. int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
  2088. int address = client->addr;
  2089. const char *name = NULL;
  2090. if (config2 < 0)
  2091. return NULL;
  2092. if (address == 0x4c && !(config1 & 0x2a) && !(config2 & 0xf8)) {
  2093. if (chip_id == 0x01 && convrate <= 0x09) {
  2094. /* W83L771W/G */
  2095. name = "w83l771";
  2096. } else if ((chip_id & 0xfe) == 0x10 && convrate <= 0x08) {
  2097. /* W83L771AWG/ASG */
  2098. name = "w83l771";
  2099. }
  2100. }
  2101. return name;
  2102. }
  2103. static const char *lm90_detect_nuvoton_50(struct i2c_client *client, int chip_id,
  2104. int config1, int convrate)
  2105. {
  2106. int chip_id2 = i2c_smbus_read_byte_data(client, NCT7716_REG_CHIP_ID);
  2107. int config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
  2108. int address = client->addr;
  2109. const char *name = NULL;
  2110. if (chip_id2 < 0 || config2 < 0)
  2111. return NULL;
  2112. if (chip_id2 != 0x50 || convrate > 0x08)
  2113. return NULL;
  2114. switch (chip_id) {
  2115. case 0x90:
  2116. if (address == 0x48 && !(config1 & 0x3e) && !(config2 & 0xfe))
  2117. name = "nct7717";
  2118. break;
  2119. case 0x91:
  2120. if ((address == 0x48 || address == 0x49) && !(config1 & 0x3e) &&
  2121. !(config2 & 0xfe))
  2122. name = "nct7716";
  2123. else if (address == 0x4c && !(config1 & 0x38) && !(config2 & 0xf8))
  2124. name = "nct7718";
  2125. break;
  2126. default:
  2127. break;
  2128. }
  2129. return name;
  2130. }
  2131. static const char *lm90_detect_nxp(struct i2c_client *client, bool common_address,
  2132. int chip_id, int config1, int convrate)
  2133. {
  2134. int address = client->addr;
  2135. const char *name = NULL;
  2136. int config2;
  2137. switch (chip_id) {
  2138. case 0x00:
  2139. config2 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG2);
  2140. if (config2 < 0)
  2141. return NULL;
  2142. if (address >= 0x48 && address <= 0x4f &&
  2143. !(config1 & 0x2a) && !(config2 & 0xfe) && convrate <= 0x09)
  2144. name = "sa56004";
  2145. break;
  2146. case 0x80:
  2147. if (common_address && !(config1 & 0x3f) && convrate <= 0x07)
  2148. name = "ne1618";
  2149. break;
  2150. default:
  2151. break;
  2152. }
  2153. return name;
  2154. }
  2155. static const char *lm90_detect_gmt(struct i2c_client *client, int chip_id,
  2156. int config1, int convrate)
  2157. {
  2158. int address = client->addr;
  2159. /*
  2160. * According to the datasheet, G781 is supposed to be at I2C Address
  2161. * 0x4c and have a chip ID of 0x01. G781-1 is supposed to be at I2C
  2162. * address 0x4d and have a chip ID of 0x03. However, when support
  2163. * for G781 was added, chips at 0x4c and 0x4d were found to have a
  2164. * chip ID of 0x01. A G781-1 at I2C address 0x4d was now found with
  2165. * chip ID 0x03.
  2166. * To avoid detection failures, accept chip ID 0x01 and 0x03 at both
  2167. * addresses.
  2168. * G784 reports manufacturer ID 0x47 and chip ID 0x01. A public
  2169. * datasheet is not available. Extensive testing suggests that
  2170. * the chip appears to be fully compatible with G781.
  2171. * Available register dumps show that G751 also reports manufacturer
  2172. * ID 0x47 and chip ID 0x01 even though that chip does not officially
  2173. * support those registers. This makes chip detection somewhat
  2174. * vulnerable. To improve detection quality, read the offset low byte
  2175. * and alert fault queue registers and verify that only expected bits
  2176. * are set.
  2177. */
  2178. if ((chip_id == 0x01 || chip_id == 0x03) &&
  2179. (address == 0x4c || address == 0x4d) &&
  2180. !(config1 & 0x3f) && convrate <= 0x08) {
  2181. int reg;
  2182. reg = i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_OFFSL);
  2183. if (reg < 0 || reg & 0x1f)
  2184. return NULL;
  2185. reg = i2c_smbus_read_byte_data(client, TMP451_REG_CONALERT);
  2186. if (reg < 0 || reg & 0xf1)
  2187. return NULL;
  2188. return "g781";
  2189. }
  2190. return NULL;
  2191. }
  2192. static const char *lm90_detect_ti49(struct i2c_client *client, bool common_address,
  2193. int chip_id, int config1, int convrate)
  2194. {
  2195. if (common_address && chip_id == 0x00 && !(config1 & 0x3f) && !(convrate & 0xf8)) {
  2196. /* THMC10: Unsupported registers return 0xff */
  2197. if (i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_TEMPL) == 0xff &&
  2198. i2c_smbus_read_byte_data(client, LM90_REG_REMOTE_CRIT) == 0xff)
  2199. return "thmc10";
  2200. }
  2201. return NULL;
  2202. }
  2203. static const char *lm90_detect_ti(struct i2c_client *client, int chip_id,
  2204. int config1, int convrate)
  2205. {
  2206. int address = client->addr;
  2207. const char *name = NULL;
  2208. if (chip_id == 0x00 && !(config1 & 0x1b) && convrate <= 0x09) {
  2209. int local_ext, conalert, chen, dfc;
  2210. local_ext = i2c_smbus_read_byte_data(client,
  2211. TMP451_REG_LOCAL_TEMPL);
  2212. conalert = i2c_smbus_read_byte_data(client,
  2213. TMP451_REG_CONALERT);
  2214. chen = i2c_smbus_read_byte_data(client, TMP461_REG_CHEN);
  2215. dfc = i2c_smbus_read_byte_data(client, TMP461_REG_DFC);
  2216. if (!(local_ext & 0x0f) && (conalert & 0xf1) == 0x01 &&
  2217. (chen & 0xfc) == 0x00 && (dfc & 0xfc) == 0x00) {
  2218. if (address == 0x4c && !(chen & 0x03))
  2219. name = "tmp451";
  2220. else if (address >= 0x48 && address <= 0x4f)
  2221. name = "tmp461";
  2222. }
  2223. }
  2224. return name;
  2225. }
  2226. /* Return 0 if detection is successful, -ENODEV otherwise */
  2227. static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info)
  2228. {
  2229. struct i2c_adapter *adapter = client->adapter;
  2230. int man_id, chip_id, config1, convrate, lhigh;
  2231. const char *name = NULL;
  2232. int address = client->addr;
  2233. bool common_address =
  2234. (address >= 0x18 && address <= 0x1a) ||
  2235. (address >= 0x29 && address <= 0x2b) ||
  2236. (address >= 0x4c && address <= 0x4e);
  2237. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  2238. return -ENODEV;
  2239. /*
  2240. * Get well defined register value for chips with neither man_id nor
  2241. * chip_id registers.
  2242. */
  2243. lhigh = i2c_smbus_read_byte_data(client, LM90_REG_LOCAL_HIGH);
  2244. /* detection and identification */
  2245. man_id = i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID);
  2246. chip_id = i2c_smbus_read_byte_data(client, LM90_REG_CHIP_ID);
  2247. config1 = i2c_smbus_read_byte_data(client, LM90_REG_CONFIG1);
  2248. convrate = i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE);
  2249. if (man_id < 0 || chip_id < 0 || config1 < 0 || convrate < 0 || lhigh < 0)
  2250. return -ENODEV;
  2251. /* Bail out immediately if all register report the same value */
  2252. if (lhigh == man_id && lhigh == chip_id && lhigh == config1 && lhigh == convrate)
  2253. return -ENODEV;
  2254. /*
  2255. * If reading man_id and chip_id both return the same value as lhigh,
  2256. * the chip may not support those registers and return the most recent read
  2257. * value. Check again with a different register and handle accordingly.
  2258. */
  2259. if (man_id == lhigh && chip_id == lhigh) {
  2260. convrate = i2c_smbus_read_byte_data(client, LM90_REG_CONVRATE);
  2261. man_id = i2c_smbus_read_byte_data(client, LM90_REG_MAN_ID);
  2262. chip_id = i2c_smbus_read_byte_data(client, LM90_REG_CHIP_ID);
  2263. if (convrate < 0 || man_id < 0 || chip_id < 0)
  2264. return -ENODEV;
  2265. if (man_id == convrate && chip_id == convrate)
  2266. man_id = -1;
  2267. }
  2268. switch (man_id) {
  2269. case -1: /* Chip does not support man_id / chip_id */
  2270. if (common_address && !convrate && !(config1 & 0x7f))
  2271. name = lm90_detect_lm84(client);
  2272. break;
  2273. case 0x01: /* National Semiconductor */
  2274. name = lm90_detect_national(client, chip_id, config1, convrate);
  2275. break;
  2276. case 0x1a: /* ON */
  2277. name = lm90_detect_on(client, chip_id, config1, convrate);
  2278. break;
  2279. case 0x23: /* Genesys Logic */
  2280. if (common_address && !(config1 & 0x3f) && !(convrate & 0xf8))
  2281. name = "gl523sm";
  2282. break;
  2283. case 0x41: /* Analog Devices */
  2284. name = lm90_detect_analog(client, common_address, chip_id, config1,
  2285. convrate);
  2286. break;
  2287. case 0x47: /* GMT */
  2288. name = lm90_detect_gmt(client, chip_id, config1, convrate);
  2289. break;
  2290. case 0x49: /* TI */
  2291. name = lm90_detect_ti49(client, common_address, chip_id, config1, convrate);
  2292. break;
  2293. case 0x4d: /* Maxim Integrated */
  2294. name = lm90_detect_maxim(client, common_address, chip_id,
  2295. config1, convrate);
  2296. break;
  2297. case 0x50:
  2298. name = lm90_detect_nuvoton_50(client, chip_id, config1, convrate);
  2299. break;
  2300. case 0x54: /* ON MC1066, Microchip TC1068, TCM1617 (originally TelCom) */
  2301. if (common_address && !(config1 & 0x3f) && !(convrate & 0xf8))
  2302. name = "mc1066";
  2303. break;
  2304. case 0x55: /* TI */
  2305. name = lm90_detect_ti(client, chip_id, config1, convrate);
  2306. break;
  2307. case 0x5c: /* Winbond/Nuvoton */
  2308. name = lm90_detect_nuvoton(client, chip_id, config1, convrate);
  2309. break;
  2310. case 0xa1: /* NXP Semiconductor/Philips */
  2311. name = lm90_detect_nxp(client, common_address, chip_id, config1, convrate);
  2312. break;
  2313. case 0xff: /* MAX1617, G767, NE1617 */
  2314. if (common_address && chip_id == 0xff && convrate < 8)
  2315. name = lm90_detect_max1617(client, config1);
  2316. break;
  2317. default:
  2318. break;
  2319. }
  2320. if (!name) { /* identification failed */
  2321. dev_dbg(&adapter->dev,
  2322. "Unsupported chip at 0x%02x (man_id=0x%02X, chip_id=0x%02X)\n",
  2323. client->addr, man_id, chip_id);
  2324. return -ENODEV;
  2325. }
  2326. strscpy(info->type, name, I2C_NAME_SIZE);
  2327. return 0;
  2328. }
  2329. static void lm90_restore_conf(void *_data)
  2330. {
  2331. struct lm90_data *data = _data;
  2332. struct i2c_client *client = data->client;
  2333. cancel_delayed_work_sync(&data->alert_work);
  2334. cancel_work_sync(&data->report_work);
  2335. /* Restore initial configuration */
  2336. if (data->flags & LM90_HAVE_CONVRATE)
  2337. lm90_write_convrate(data, data->convrate_orig);
  2338. lm90_write_reg(client, LM90_REG_CONFIG1, data->config_orig);
  2339. }
  2340. static int lm90_init_client(struct i2c_client *client, struct lm90_data *data)
  2341. {
  2342. struct device_node *np = client->dev.of_node;
  2343. int config, convrate;
  2344. if (data->flags & LM90_HAVE_CONVRATE) {
  2345. convrate = lm90_read_reg(client, LM90_REG_CONVRATE);
  2346. if (convrate < 0)
  2347. return convrate;
  2348. data->convrate_orig = convrate;
  2349. lm90_set_convrate(client, data, 500); /* 500ms; 2Hz conversion rate */
  2350. } else {
  2351. data->update_interval = 500;
  2352. }
  2353. /*
  2354. * Start the conversions.
  2355. */
  2356. config = lm90_read_reg(client, LM90_REG_CONFIG1);
  2357. if (config < 0)
  2358. return config;
  2359. data->config_orig = config;
  2360. data->config = config;
  2361. /* Check Temperature Range Select */
  2362. if (data->flags & LM90_HAVE_EXTENDED_TEMP) {
  2363. if (of_property_read_bool(np, "ti,extended-range-enable"))
  2364. config |= 0x04;
  2365. if (!(config & 0x04))
  2366. data->flags &= ~LM90_HAVE_EXTENDED_TEMP;
  2367. }
  2368. /*
  2369. * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
  2370. * 0.125 degree resolution) and range (0x08, extend range
  2371. * to -64 degree) mode for the remote temperature sensor.
  2372. * Note that expeciments with an actual chip do not show a difference
  2373. * if bit 3 is set or not.
  2374. */
  2375. if (data->kind == max6680)
  2376. config |= 0x18;
  2377. /*
  2378. * Put MAX6654 into extended range (0x20, extend minimum range from
  2379. * 0 degrees to -64 degrees). Note that extended resolution is not
  2380. * possible on the MAX6654 unless conversion rate is set to 1 Hz or
  2381. * slower, which is intentionally not done by default.
  2382. */
  2383. if (data->kind == max6654)
  2384. config |= 0x20;
  2385. /*
  2386. * Select external channel 0 for devices with three sensors
  2387. */
  2388. if (data->flags & LM90_HAVE_TEMP3)
  2389. config &= ~0x08;
  2390. /*
  2391. * Interrupt is enabled by default on reset, but it may be disabled
  2392. * by bootloader, unmask it.
  2393. */
  2394. if (client->irq)
  2395. config &= ~0x80;
  2396. config &= 0xBF; /* run */
  2397. lm90_update_confreg(data, config);
  2398. return devm_add_action_or_reset(&client->dev, lm90_restore_conf, data);
  2399. }
  2400. static bool lm90_is_tripped(struct i2c_client *client)
  2401. {
  2402. struct lm90_data *data = i2c_get_clientdata(client);
  2403. int ret;
  2404. ret = lm90_update_alarms(data, true);
  2405. if (ret < 0)
  2406. return false;
  2407. return !!data->current_alarms;
  2408. }
  2409. static irqreturn_t lm90_irq_thread(int irq, void *dev_id)
  2410. {
  2411. struct i2c_client *client = dev_id;
  2412. if (lm90_is_tripped(client))
  2413. return IRQ_HANDLED;
  2414. else
  2415. return IRQ_NONE;
  2416. }
  2417. static int lm90_probe_channel_from_dt(struct i2c_client *client,
  2418. struct device_node *child,
  2419. struct lm90_data *data)
  2420. {
  2421. u32 id;
  2422. s32 val;
  2423. int err;
  2424. struct device *dev = &client->dev;
  2425. err = of_property_read_u32(child, "reg", &id);
  2426. if (err) {
  2427. dev_err(dev, "missing reg property of %pOFn\n", child);
  2428. return err;
  2429. }
  2430. if (id >= MAX_CHANNELS) {
  2431. dev_err(dev, "invalid reg property value %d in %pOFn\n", id, child);
  2432. return -EINVAL;
  2433. }
  2434. err = of_property_read_string(child, "label", &data->channel_label[id]);
  2435. if (err == -ENODATA || err == -EILSEQ) {
  2436. dev_err(dev, "invalid label property in %pOFn\n", child);
  2437. return err;
  2438. }
  2439. if (data->channel_label[id])
  2440. data->channel_config[id] |= HWMON_T_LABEL;
  2441. err = of_property_read_s32(child, "temperature-offset-millicelsius", &val);
  2442. if (!err) {
  2443. if (id == 0) {
  2444. dev_err(dev, "temperature-offset-millicelsius can't be set for internal channel\n");
  2445. return -EINVAL;
  2446. }
  2447. err = lm90_set_temp_offset(data, lm90_temp_offset_index[id], id, val);
  2448. if (err) {
  2449. dev_err(dev, "can't set temperature offset %d for channel %d (%d)\n",
  2450. val, id, err);
  2451. return err;
  2452. }
  2453. }
  2454. return 0;
  2455. }
  2456. static int lm90_parse_dt_channel_info(struct i2c_client *client,
  2457. struct lm90_data *data)
  2458. {
  2459. int err;
  2460. struct device *dev = &client->dev;
  2461. const struct device_node *np = dev->of_node;
  2462. for_each_child_of_node_scoped(np, child) {
  2463. if (strcmp(child->name, "channel"))
  2464. continue;
  2465. err = lm90_probe_channel_from_dt(client, child, data);
  2466. if (err)
  2467. return err;
  2468. }
  2469. return 0;
  2470. }
  2471. static const struct hwmon_ops lm90_ops = {
  2472. .is_visible = lm90_is_visible,
  2473. .read = lm90_read,
  2474. .read_string = lm90_read_string,
  2475. .write = lm90_write,
  2476. };
  2477. static int lm90_probe(struct i2c_client *client)
  2478. {
  2479. struct device *dev = &client->dev;
  2480. struct i2c_adapter *adapter = client->adapter;
  2481. struct hwmon_channel_info *info;
  2482. struct device *hwmon_dev;
  2483. struct lm90_data *data;
  2484. int err;
  2485. err = devm_regulator_get_enable(dev, "vcc");
  2486. if (err)
  2487. return dev_err_probe(dev, err, "Failed to enable regulator\n");
  2488. data = devm_kzalloc(dev, sizeof(struct lm90_data), GFP_KERNEL);
  2489. if (!data)
  2490. return -ENOMEM;
  2491. data->client = client;
  2492. i2c_set_clientdata(client, data);
  2493. INIT_DELAYED_WORK(&data->alert_work, lm90_alert_work);
  2494. INIT_WORK(&data->report_work, lm90_report_alarms);
  2495. /* Set the device type */
  2496. data->kind = (uintptr_t)i2c_get_match_data(client);
  2497. /*
  2498. * Different devices have different alarm bits triggering the
  2499. * ALERT# output
  2500. */
  2501. data->alert_alarms = lm90_params[data->kind].alert_alarms;
  2502. data->resolution = lm90_params[data->kind].resolution ? : 11;
  2503. /* Set chip capabilities */
  2504. data->flags = lm90_params[data->kind].flags;
  2505. if ((data->flags & (LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC)) &&
  2506. !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_PEC))
  2507. data->flags &= ~(LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC);
  2508. if ((data->flags & LM90_HAVE_PARTIAL_PEC) &&
  2509. !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  2510. data->flags &= ~LM90_HAVE_PARTIAL_PEC;
  2511. data->chip.ops = &lm90_ops;
  2512. data->chip.info = data->info;
  2513. data->info[0] = &data->chip_info;
  2514. info = &data->chip_info;
  2515. info->type = hwmon_chip;
  2516. info->config = data->chip_config;
  2517. data->chip_config[0] = HWMON_C_REGISTER_TZ;
  2518. if (data->flags & LM90_HAVE_ALARMS)
  2519. data->chip_config[0] |= HWMON_C_ALARMS;
  2520. if (data->flags & LM90_HAVE_CONVRATE)
  2521. data->chip_config[0] |= HWMON_C_UPDATE_INTERVAL;
  2522. if (data->flags & LM90_HAVE_FAULTQUEUE)
  2523. data->chip_config[0] |= HWMON_C_TEMP_SAMPLES;
  2524. if (data->flags & (LM90_HAVE_PEC | LM90_HAVE_PARTIAL_PEC))
  2525. data->chip_config[0] |= HWMON_C_PEC;
  2526. data->info[1] = &data->temp_info;
  2527. info = &data->temp_info;
  2528. info->type = hwmon_temp;
  2529. info->config = data->channel_config;
  2530. data->channel_config[0] = HWMON_T_INPUT | HWMON_T_MAX |
  2531. HWMON_T_MAX_ALARM;
  2532. data->channel_config[1] = HWMON_T_INPUT | HWMON_T_MAX |
  2533. HWMON_T_MAX_ALARM | HWMON_T_FAULT;
  2534. if (data->flags & LM90_HAVE_LOW) {
  2535. data->channel_config[0] |= HWMON_T_MIN | HWMON_T_MIN_ALARM;
  2536. data->channel_config[1] |= HWMON_T_MIN | HWMON_T_MIN_ALARM;
  2537. }
  2538. if (data->flags & LM90_HAVE_CRIT) {
  2539. data->channel_config[0] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
  2540. data->channel_config[1] |= HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_CRIT_HYST;
  2541. }
  2542. if (data->flags & LM90_HAVE_OFFSET)
  2543. data->channel_config[1] |= HWMON_T_OFFSET;
  2544. if (data->flags & LM90_HAVE_EMERGENCY) {
  2545. data->channel_config[0] |= HWMON_T_EMERGENCY |
  2546. HWMON_T_EMERGENCY_HYST;
  2547. data->channel_config[1] |= HWMON_T_EMERGENCY |
  2548. HWMON_T_EMERGENCY_HYST;
  2549. }
  2550. if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
  2551. data->channel_config[0] |= HWMON_T_EMERGENCY_ALARM;
  2552. data->channel_config[1] |= HWMON_T_EMERGENCY_ALARM;
  2553. }
  2554. if (data->flags & LM90_HAVE_TEMP3) {
  2555. data->channel_config[2] = HWMON_T_INPUT |
  2556. HWMON_T_MIN | HWMON_T_MAX |
  2557. HWMON_T_CRIT | HWMON_T_CRIT_HYST |
  2558. HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM |
  2559. HWMON_T_CRIT_ALARM | HWMON_T_FAULT;
  2560. if (data->flags & LM90_HAVE_EMERGENCY) {
  2561. data->channel_config[2] |= HWMON_T_EMERGENCY |
  2562. HWMON_T_EMERGENCY_HYST;
  2563. }
  2564. if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
  2565. data->channel_config[2] |= HWMON_T_EMERGENCY_ALARM;
  2566. if (data->flags & LM90_HAVE_OFFSET)
  2567. data->channel_config[2] |= HWMON_T_OFFSET;
  2568. }
  2569. data->faultqueue_mask = lm90_params[data->kind].faultqueue_mask;
  2570. data->faultqueue_depth = lm90_params[data->kind].faultqueue_depth;
  2571. data->reg_local_ext = lm90_params[data->kind].reg_local_ext;
  2572. if (data->flags & LM90_HAVE_REMOTE_EXT)
  2573. data->reg_remote_ext = LM90_REG_REMOTE_TEMPL;
  2574. data->reg_status2 = lm90_params[data->kind].reg_status2;
  2575. /* Set maximum conversion rate */
  2576. data->max_convrate = lm90_params[data->kind].max_convrate;
  2577. /* Parse device-tree channel information */
  2578. if (client->dev.of_node) {
  2579. err = lm90_parse_dt_channel_info(client, data);
  2580. if (err)
  2581. return err;
  2582. }
  2583. /* Initialize the LM90 chip */
  2584. err = lm90_init_client(client, data);
  2585. if (err < 0) {
  2586. dev_err(dev, "Failed to initialize device\n");
  2587. return err;
  2588. }
  2589. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  2590. data, &data->chip,
  2591. NULL);
  2592. if (IS_ERR(hwmon_dev))
  2593. return PTR_ERR(hwmon_dev);
  2594. data->hwmon_dev = hwmon_dev;
  2595. if (client->irq) {
  2596. dev_dbg(dev, "IRQ: %d\n", client->irq);
  2597. err = devm_request_threaded_irq(dev, client->irq,
  2598. NULL, lm90_irq_thread,
  2599. IRQF_ONESHOT, "lm90", client);
  2600. if (err < 0) {
  2601. dev_err(dev, "cannot request IRQ %d\n", client->irq);
  2602. return err;
  2603. }
  2604. }
  2605. return 0;
  2606. }
  2607. static void lm90_alert(struct i2c_client *client, enum i2c_alert_protocol type,
  2608. unsigned int flag)
  2609. {
  2610. if (type != I2C_PROTOCOL_SMBUS_ALERT)
  2611. return;
  2612. if (lm90_is_tripped(client)) {
  2613. /*
  2614. * Disable ALERT# output, because these chips don't implement
  2615. * SMBus alert correctly; they should only hold the alert line
  2616. * low briefly.
  2617. */
  2618. struct lm90_data *data = i2c_get_clientdata(client);
  2619. if ((data->flags & LM90_HAVE_BROKEN_ALERT) &&
  2620. (data->current_alarms & data->alert_alarms)) {
  2621. if (!(data->config & 0x80)) {
  2622. dev_dbg(&client->dev, "Disabling ALERT#\n");
  2623. lm90_update_confreg(data, data->config | 0x80);
  2624. }
  2625. schedule_delayed_work(&data->alert_work,
  2626. max_t(int, HZ, msecs_to_jiffies(data->update_interval)));
  2627. }
  2628. } else {
  2629. dev_dbg(&client->dev, "Everything OK\n");
  2630. }
  2631. }
  2632. static int lm90_suspend(struct device *dev)
  2633. {
  2634. struct lm90_data *data = dev_get_drvdata(dev);
  2635. struct i2c_client *client = data->client;
  2636. if (client->irq)
  2637. disable_irq(client->irq);
  2638. return 0;
  2639. }
  2640. static int lm90_resume(struct device *dev)
  2641. {
  2642. struct lm90_data *data = dev_get_drvdata(dev);
  2643. struct i2c_client *client = data->client;
  2644. if (client->irq)
  2645. enable_irq(client->irq);
  2646. return 0;
  2647. }
  2648. static DEFINE_SIMPLE_DEV_PM_OPS(lm90_pm_ops, lm90_suspend, lm90_resume);
  2649. static struct i2c_driver lm90_driver = {
  2650. .class = I2C_CLASS_HWMON,
  2651. .driver = {
  2652. .name = "lm90",
  2653. .of_match_table = of_match_ptr(lm90_of_match),
  2654. .pm = pm_sleep_ptr(&lm90_pm_ops),
  2655. },
  2656. .probe = lm90_probe,
  2657. .alert = lm90_alert,
  2658. .id_table = lm90_id,
  2659. .detect = lm90_detect,
  2660. .address_list = normal_i2c,
  2661. };
  2662. module_i2c_driver(lm90_driver);
  2663. MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
  2664. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  2665. MODULE_LICENSE("GPL");