nct6775-core.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * nct6775 - Driver for the hardware monitoring functionality of
  4. * Nuvoton NCT677x Super-I/O chips
  5. *
  6. * Copyright (C) 2012 Guenter Roeck <[email protected]>
  7. *
  8. * Derived from w83627ehf driver
  9. * Copyright (C) 2005-2012 Jean Delvare <[email protected]>
  10. * Copyright (C) 2006 Yuan Mu (Winbond),
  11. * Rudolf Marek <[email protected]>
  12. * David Hubbard <[email protected]>
  13. * Daniel J Blueman <[email protected]>
  14. * Copyright (C) 2010 Sheng-Yuan Huang (Nuvoton) (PS00)
  15. *
  16. * Shamelessly ripped from the w83627hf driver
  17. * Copyright (C) 2003 Mark Studebaker
  18. *
  19. * Supports the following chips:
  20. *
  21. * Chip #vin #fan #pwm #temp chip IDs man ID
  22. * nct6106d 9 3 3 6+3 0xc450 0xc1 0x5ca3
  23. * nct6116d 9 5 5 3+3 0xd280 0xc1 0x5ca3
  24. * nct6775f 9 4 3 6+3 0xb470 0xc1 0x5ca3
  25. * nct6776f 9 5 3 6+3 0xc330 0xc1 0x5ca3
  26. * nct6779d 15 5 5 2+6 0xc560 0xc1 0x5ca3
  27. * nct6791d 15 6 6 2+6 0xc800 0xc1 0x5ca3
  28. * nct6792d 15 6 6 2+6 0xc910 0xc1 0x5ca3
  29. * nct6793d 15 6 6 2+6 0xd120 0xc1 0x5ca3
  30. * nct6795d 14 6 6 2+6 0xd350 0xc1 0x5ca3
  31. * nct6796d 14 7 7 2+6 0xd420 0xc1 0x5ca3
  32. * nct6797d 14 7 7 2+6 0xd450 0xc1 0x5ca3
  33. * (0xd451)
  34. * nct6798d 14 7 7 2+6 0xd428 0xc1 0x5ca3
  35. * (0xd429)
  36. *
  37. * #temp lists the number of monitored temperature sources (first value) plus
  38. * the number of directly connectable temperature sensors (second value).
  39. */
  40. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  41. #include <linux/module.h>
  42. #include <linux/init.h>
  43. #include <linux/slab.h>
  44. #include <linux/jiffies.h>
  45. #include <linux/hwmon.h>
  46. #include <linux/hwmon-sysfs.h>
  47. #include <linux/err.h>
  48. #include <linux/mutex.h>
  49. #include <linux/bitops.h>
  50. #include <linux/nospec.h>
  51. #include <linux/regmap.h>
  52. #include "lm75.h"
  53. #include "nct6775.h"
  54. #undef DEFAULT_SYMBOL_NAMESPACE
  55. #define DEFAULT_SYMBOL_NAMESPACE HWMON_NCT6775
  56. #define USE_ALTERNATE
  57. /* used to set data->name = nct6775_device_names[data->sio_kind] */
  58. static const char * const nct6775_device_names[] = {
  59. "nct6106",
  60. "nct6116",
  61. "nct6775",
  62. "nct6776",
  63. "nct6779",
  64. "nct6791",
  65. "nct6792",
  66. "nct6793",
  67. "nct6795",
  68. "nct6796",
  69. "nct6797",
  70. "nct6798",
  71. };
  72. /* Common and NCT6775 specific data */
  73. /* Voltage min/max registers for nr=7..14 are in bank 5 */
  74. static const u16 NCT6775_REG_IN_MAX[] = {
  75. 0x2b, 0x2d, 0x2f, 0x31, 0x33, 0x35, 0x37, 0x554, 0x556, 0x558, 0x55a,
  76. 0x55c, 0x55e, 0x560, 0x562 };
  77. static const u16 NCT6775_REG_IN_MIN[] = {
  78. 0x2c, 0x2e, 0x30, 0x32, 0x34, 0x36, 0x38, 0x555, 0x557, 0x559, 0x55b,
  79. 0x55d, 0x55f, 0x561, 0x563 };
  80. static const u16 NCT6775_REG_IN[] = {
  81. 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551, 0x552
  82. };
  83. #define NCT6775_REG_VBAT 0x5D
  84. #define NCT6775_REG_DIODE 0x5E
  85. #define NCT6775_DIODE_MASK 0x02
  86. static const u16 NCT6775_REG_ALARM[NUM_REG_ALARM] = { 0x459, 0x45A, 0x45B };
  87. /* 0..15 voltages, 16..23 fans, 24..29 temperatures, 30..31 intrusion */
  88. static const s8 NCT6775_ALARM_BITS[] = {
  89. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  90. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  91. -1, /* unused */
  92. 6, 7, 11, -1, -1, /* fan1..fan5 */
  93. -1, -1, -1, /* unused */
  94. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  95. 12, -1 }; /* intrusion0, intrusion1 */
  96. static const u16 NCT6775_REG_BEEP[NUM_REG_BEEP] = { 0x56, 0x57, 0x453, 0x4e };
  97. /*
  98. * 0..14 voltages, 15 global beep enable, 16..23 fans, 24..29 temperatures,
  99. * 30..31 intrusion
  100. */
  101. static const s8 NCT6775_BEEP_BITS[] = {
  102. 0, 1, 2, 3, 8, 9, 10, 16, /* in0.. in7 */
  103. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  104. 21, /* global beep enable */
  105. 6, 7, 11, 28, -1, /* fan1..fan5 */
  106. -1, -1, -1, /* unused */
  107. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  108. 12, -1 }; /* intrusion0, intrusion1 */
  109. /* DC or PWM output fan configuration */
  110. static const u8 NCT6775_REG_PWM_MODE[] = { 0x04, 0x04, 0x12 };
  111. static const u8 NCT6775_PWM_MODE_MASK[] = { 0x01, 0x02, 0x01 };
  112. /* Advanced Fan control, some values are common for all fans */
  113. static const u16 NCT6775_REG_TARGET[] = {
  114. 0x101, 0x201, 0x301, 0x801, 0x901, 0xa01, 0xb01 };
  115. static const u16 NCT6775_REG_FAN_MODE[] = {
  116. 0x102, 0x202, 0x302, 0x802, 0x902, 0xa02, 0xb02 };
  117. static const u16 NCT6775_REG_FAN_STEP_DOWN_TIME[] = {
  118. 0x103, 0x203, 0x303, 0x803, 0x903, 0xa03, 0xb03 };
  119. static const u16 NCT6775_REG_FAN_STEP_UP_TIME[] = {
  120. 0x104, 0x204, 0x304, 0x804, 0x904, 0xa04, 0xb04 };
  121. static const u16 NCT6775_REG_FAN_STOP_OUTPUT[] = {
  122. 0x105, 0x205, 0x305, 0x805, 0x905, 0xa05, 0xb05 };
  123. static const u16 NCT6775_REG_FAN_START_OUTPUT[] = {
  124. 0x106, 0x206, 0x306, 0x806, 0x906, 0xa06, 0xb06 };
  125. static const u16 NCT6775_REG_FAN_MAX_OUTPUT[] = { 0x10a, 0x20a, 0x30a };
  126. static const u16 NCT6775_REG_FAN_STEP_OUTPUT[] = { 0x10b, 0x20b, 0x30b };
  127. static const u16 NCT6775_REG_FAN_STOP_TIME[] = {
  128. 0x107, 0x207, 0x307, 0x807, 0x907, 0xa07, 0xb07 };
  129. static const u16 NCT6775_REG_PWM[] = {
  130. 0x109, 0x209, 0x309, 0x809, 0x909, 0xa09, 0xb09 };
  131. static const u16 NCT6775_REG_PWM_READ[] = {
  132. 0x01, 0x03, 0x11, 0x13, 0x15, 0xa09, 0xb09 };
  133. static const u16 NCT6775_REG_FAN[] = { 0x630, 0x632, 0x634, 0x636, 0x638 };
  134. static const u16 NCT6775_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d };
  135. static const u16 NCT6775_REG_FAN_PULSES[NUM_FAN] = {
  136. 0x641, 0x642, 0x643, 0x644 };
  137. static const u16 NCT6775_FAN_PULSE_SHIFT[NUM_FAN] = { };
  138. static const u16 NCT6775_REG_TEMP[] = {
  139. 0x27, 0x150, 0x250, 0x62b, 0x62c, 0x62d };
  140. static const u16 NCT6775_REG_TEMP_MON[] = { 0x73, 0x75, 0x77 };
  141. static const u16 NCT6775_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
  142. 0, 0x152, 0x252, 0x628, 0x629, 0x62A };
  143. static const u16 NCT6775_REG_TEMP_HYST[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
  144. 0x3a, 0x153, 0x253, 0x673, 0x678, 0x67D };
  145. static const u16 NCT6775_REG_TEMP_OVER[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
  146. 0x39, 0x155, 0x255, 0x672, 0x677, 0x67C };
  147. static const u16 NCT6775_REG_TEMP_SOURCE[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
  148. 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
  149. static const u16 NCT6775_REG_TEMP_SEL[] = {
  150. 0x100, 0x200, 0x300, 0x800, 0x900, 0xa00, 0xb00 };
  151. static const u16 NCT6775_REG_WEIGHT_TEMP_SEL[] = {
  152. 0x139, 0x239, 0x339, 0x839, 0x939, 0xa39 };
  153. static const u16 NCT6775_REG_WEIGHT_TEMP_STEP[] = {
  154. 0x13a, 0x23a, 0x33a, 0x83a, 0x93a, 0xa3a };
  155. static const u16 NCT6775_REG_WEIGHT_TEMP_STEP_TOL[] = {
  156. 0x13b, 0x23b, 0x33b, 0x83b, 0x93b, 0xa3b };
  157. static const u16 NCT6775_REG_WEIGHT_DUTY_STEP[] = {
  158. 0x13c, 0x23c, 0x33c, 0x83c, 0x93c, 0xa3c };
  159. static const u16 NCT6775_REG_WEIGHT_TEMP_BASE[] = {
  160. 0x13d, 0x23d, 0x33d, 0x83d, 0x93d, 0xa3d };
  161. static const u16 NCT6775_REG_TEMP_OFFSET[] = { 0x454, 0x455, 0x456 };
  162. static const u16 NCT6775_REG_AUTO_TEMP[] = {
  163. 0x121, 0x221, 0x321, 0x821, 0x921, 0xa21, 0xb21 };
  164. static const u16 NCT6775_REG_AUTO_PWM[] = {
  165. 0x127, 0x227, 0x327, 0x827, 0x927, 0xa27, 0xb27 };
  166. #define NCT6775_AUTO_TEMP(data, nr, p) ((data)->REG_AUTO_TEMP[nr] + (p))
  167. #define NCT6775_AUTO_PWM(data, nr, p) ((data)->REG_AUTO_PWM[nr] + (p))
  168. static const u16 NCT6775_REG_CRITICAL_ENAB[] = { 0x134, 0x234, 0x334 };
  169. static const u16 NCT6775_REG_CRITICAL_TEMP[] = {
  170. 0x135, 0x235, 0x335, 0x835, 0x935, 0xa35, 0xb35 };
  171. static const u16 NCT6775_REG_CRITICAL_TEMP_TOLERANCE[] = {
  172. 0x138, 0x238, 0x338, 0x838, 0x938, 0xa38, 0xb38 };
  173. static const char *const nct6775_temp_label[] = {
  174. "",
  175. "SYSTIN",
  176. "CPUTIN",
  177. "AUXTIN",
  178. "AMD SB-TSI",
  179. "PECI Agent 0",
  180. "PECI Agent 1",
  181. "PECI Agent 2",
  182. "PECI Agent 3",
  183. "PECI Agent 4",
  184. "PECI Agent 5",
  185. "PECI Agent 6",
  186. "PECI Agent 7",
  187. "PCH_CHIP_CPU_MAX_TEMP",
  188. "PCH_CHIP_TEMP",
  189. "PCH_CPU_TEMP",
  190. "PCH_MCH_TEMP",
  191. "PCH_DIM0_TEMP",
  192. "PCH_DIM1_TEMP",
  193. "PCH_DIM2_TEMP",
  194. "PCH_DIM3_TEMP"
  195. };
  196. #define NCT6775_TEMP_MASK 0x001ffffe
  197. #define NCT6775_VIRT_TEMP_MASK 0x00000000
  198. static const u16 NCT6775_REG_TEMP_ALTERNATE[32] = {
  199. [13] = 0x661,
  200. [14] = 0x662,
  201. [15] = 0x664,
  202. };
  203. static const u16 NCT6775_REG_TEMP_CRIT[32] = {
  204. [4] = 0xa00,
  205. [5] = 0xa01,
  206. [6] = 0xa02,
  207. [7] = 0xa03,
  208. [8] = 0xa04,
  209. [9] = 0xa05,
  210. [10] = 0xa06,
  211. [11] = 0xa07
  212. };
  213. static const u16 NCT6775_REG_TSI_TEMP[] = { 0x669 };
  214. /* NCT6776 specific data */
  215. /* STEP_UP_TIME and STEP_DOWN_TIME regs are swapped for all chips but NCT6775 */
  216. #define NCT6776_REG_FAN_STEP_UP_TIME NCT6775_REG_FAN_STEP_DOWN_TIME
  217. #define NCT6776_REG_FAN_STEP_DOWN_TIME NCT6775_REG_FAN_STEP_UP_TIME
  218. static const s8 NCT6776_ALARM_BITS[] = {
  219. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  220. 17, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  221. -1, /* unused */
  222. 6, 7, 11, 10, 23, /* fan1..fan5 */
  223. -1, -1, -1, /* unused */
  224. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  225. 12, 9 }; /* intrusion0, intrusion1 */
  226. static const u16 NCT6776_REG_BEEP[NUM_REG_BEEP] = { 0xb2, 0xb3, 0xb4, 0xb5 };
  227. static const s8 NCT6776_BEEP_BITS[] = {
  228. 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
  229. 8, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  230. 24, /* global beep enable */
  231. 25, 26, 27, 28, 29, /* fan1..fan5 */
  232. -1, -1, -1, /* unused */
  233. 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
  234. 30, 31 }; /* intrusion0, intrusion1 */
  235. static const u16 NCT6776_REG_TOLERANCE_H[] = {
  236. 0x10c, 0x20c, 0x30c, 0x80c, 0x90c, 0xa0c, 0xb0c };
  237. static const u8 NCT6776_REG_PWM_MODE[] = { 0x04, 0, 0, 0, 0, 0 };
  238. static const u8 NCT6776_PWM_MODE_MASK[] = { 0x01, 0, 0, 0, 0, 0 };
  239. static const u16 NCT6776_REG_FAN_MIN[] = {
  240. 0x63a, 0x63c, 0x63e, 0x640, 0x642, 0x64a, 0x64c };
  241. static const u16 NCT6776_REG_FAN_PULSES[NUM_FAN] = {
  242. 0x644, 0x645, 0x646, 0x647, 0x648, 0x649 };
  243. static const u16 NCT6776_REG_WEIGHT_DUTY_BASE[] = {
  244. 0x13e, 0x23e, 0x33e, 0x83e, 0x93e, 0xa3e };
  245. static const u16 NCT6776_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6775_REG_TEMP)] = {
  246. 0x18, 0x152, 0x252, 0x628, 0x629, 0x62A };
  247. static const char *const nct6776_temp_label[] = {
  248. "",
  249. "SYSTIN",
  250. "CPUTIN",
  251. "AUXTIN",
  252. "SMBUSMASTER 0",
  253. "SMBUSMASTER 1",
  254. "SMBUSMASTER 2",
  255. "SMBUSMASTER 3",
  256. "SMBUSMASTER 4",
  257. "SMBUSMASTER 5",
  258. "SMBUSMASTER 6",
  259. "SMBUSMASTER 7",
  260. "PECI Agent 0",
  261. "PECI Agent 1",
  262. "PCH_CHIP_CPU_MAX_TEMP",
  263. "PCH_CHIP_TEMP",
  264. "PCH_CPU_TEMP",
  265. "PCH_MCH_TEMP",
  266. "PCH_DIM0_TEMP",
  267. "PCH_DIM1_TEMP",
  268. "PCH_DIM2_TEMP",
  269. "PCH_DIM3_TEMP",
  270. "BYTE_TEMP"
  271. };
  272. #define NCT6776_TEMP_MASK 0x007ffffe
  273. #define NCT6776_VIRT_TEMP_MASK 0x00000000
  274. static const u16 NCT6776_REG_TEMP_ALTERNATE[32] = {
  275. [14] = 0x401,
  276. [15] = 0x402,
  277. [16] = 0x404,
  278. };
  279. static const u16 NCT6776_REG_TEMP_CRIT[32] = {
  280. [11] = 0x709,
  281. [12] = 0x70a,
  282. };
  283. static const u16 NCT6776_REG_TSI_TEMP[] = {
  284. 0x409, 0x40b, 0x40d, 0x40f, 0x411, 0x413, 0x415, 0x417 };
  285. /* NCT6779 specific data */
  286. static const u16 NCT6779_REG_IN[] = {
  287. 0x480, 0x481, 0x482, 0x483, 0x484, 0x485, 0x486, 0x487,
  288. 0x488, 0x489, 0x48a, 0x48b, 0x48c, 0x48d, 0x48e };
  289. static const u16 NCT6779_REG_ALARM[NUM_REG_ALARM] = {
  290. 0x459, 0x45A, 0x45B, 0x568 };
  291. static const s8 NCT6779_ALARM_BITS[] = {
  292. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  293. 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
  294. -1, /* unused */
  295. 6, 7, 11, 10, 23, /* fan1..fan5 */
  296. -1, -1, -1, /* unused */
  297. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  298. 12, 9 }; /* intrusion0, intrusion1 */
  299. static const s8 NCT6779_BEEP_BITS[] = {
  300. 0, 1, 2, 3, 4, 5, 6, 7, /* in0.. in7 */
  301. 8, 9, 10, 11, 12, 13, 14, /* in8..in14 */
  302. 24, /* global beep enable */
  303. 25, 26, 27, 28, 29, /* fan1..fan5 */
  304. -1, -1, -1, /* unused */
  305. 16, 17, -1, -1, -1, -1, /* temp1..temp6 */
  306. 30, 31 }; /* intrusion0, intrusion1 */
  307. static const u16 NCT6779_REG_FAN[] = {
  308. 0x4c0, 0x4c2, 0x4c4, 0x4c6, 0x4c8, 0x4ca, 0x4ce };
  309. static const u16 NCT6779_REG_FAN_PULSES[NUM_FAN] = {
  310. 0x644, 0x645, 0x646, 0x647, 0x648, 0x649, 0x64f };
  311. static const u16 NCT6779_REG_CRITICAL_PWM_ENABLE[] = {
  312. 0x136, 0x236, 0x336, 0x836, 0x936, 0xa36, 0xb36 };
  313. #define NCT6779_CRITICAL_PWM_ENABLE_MASK 0x01
  314. static const u16 NCT6779_REG_CRITICAL_PWM[] = {
  315. 0x137, 0x237, 0x337, 0x837, 0x937, 0xa37, 0xb37 };
  316. static const u16 NCT6779_REG_TEMP[] = { 0x27, 0x150 };
  317. static const u16 NCT6779_REG_TEMP_MON[] = { 0x73, 0x75, 0x77, 0x79, 0x7b };
  318. static const u16 NCT6779_REG_TEMP_CONFIG[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
  319. 0x18, 0x152 };
  320. static const u16 NCT6779_REG_TEMP_HYST[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
  321. 0x3a, 0x153 };
  322. static const u16 NCT6779_REG_TEMP_OVER[ARRAY_SIZE(NCT6779_REG_TEMP)] = {
  323. 0x39, 0x155 };
  324. static const u16 NCT6779_REG_TEMP_OFFSET[] = {
  325. 0x454, 0x455, 0x456, 0x44a, 0x44b, 0x44c };
  326. static const char *const nct6779_temp_label[] = {
  327. "",
  328. "SYSTIN",
  329. "CPUTIN",
  330. "AUXTIN0",
  331. "AUXTIN1",
  332. "AUXTIN2",
  333. "AUXTIN3",
  334. "",
  335. "SMBUSMASTER 0",
  336. "SMBUSMASTER 1",
  337. "SMBUSMASTER 2",
  338. "SMBUSMASTER 3",
  339. "SMBUSMASTER 4",
  340. "SMBUSMASTER 5",
  341. "SMBUSMASTER 6",
  342. "SMBUSMASTER 7",
  343. "PECI Agent 0",
  344. "PECI Agent 1",
  345. "PCH_CHIP_CPU_MAX_TEMP",
  346. "PCH_CHIP_TEMP",
  347. "PCH_CPU_TEMP",
  348. "PCH_MCH_TEMP",
  349. "PCH_DIM0_TEMP",
  350. "PCH_DIM1_TEMP",
  351. "PCH_DIM2_TEMP",
  352. "PCH_DIM3_TEMP",
  353. "BYTE_TEMP",
  354. "",
  355. "",
  356. "",
  357. "",
  358. "Virtual_TEMP"
  359. };
  360. #define NCT6779_TEMP_MASK 0x07ffff7e
  361. #define NCT6779_VIRT_TEMP_MASK 0x00000000
  362. #define NCT6791_TEMP_MASK 0x87ffff7e
  363. #define NCT6791_VIRT_TEMP_MASK 0x80000000
  364. static const u16 NCT6779_REG_TEMP_ALTERNATE[32]
  365. = { 0x490, 0x491, 0x492, 0x493, 0x494, 0x495, 0, 0,
  366. 0, 0, 0, 0, 0, 0, 0, 0,
  367. 0, 0x400, 0x401, 0x402, 0x404, 0x405, 0x406, 0x407,
  368. 0x408, 0 };
  369. static const u16 NCT6779_REG_TEMP_CRIT[32] = {
  370. [15] = 0x709,
  371. [16] = 0x70a,
  372. };
  373. /* NCT6791 specific data */
  374. static const u16 NCT6791_REG_WEIGHT_TEMP_SEL[NUM_FAN] = { 0, 0x239 };
  375. static const u16 NCT6791_REG_WEIGHT_TEMP_STEP[NUM_FAN] = { 0, 0x23a };
  376. static const u16 NCT6791_REG_WEIGHT_TEMP_STEP_TOL[NUM_FAN] = { 0, 0x23b };
  377. static const u16 NCT6791_REG_WEIGHT_DUTY_STEP[NUM_FAN] = { 0, 0x23c };
  378. static const u16 NCT6791_REG_WEIGHT_TEMP_BASE[NUM_FAN] = { 0, 0x23d };
  379. static const u16 NCT6791_REG_WEIGHT_DUTY_BASE[NUM_FAN] = { 0, 0x23e };
  380. static const u16 NCT6791_REG_ALARM[NUM_REG_ALARM] = {
  381. 0x459, 0x45A, 0x45B, 0x568, 0x45D };
  382. static const s8 NCT6791_ALARM_BITS[] = {
  383. 0, 1, 2, 3, 8, 21, 20, 16, /* in0.. in7 */
  384. 17, 24, 25, 26, 27, 28, 29, /* in8..in14 */
  385. -1, /* unused */
  386. 6, 7, 11, 10, 23, 33, /* fan1..fan6 */
  387. -1, -1, /* unused */
  388. 4, 5, 13, -1, -1, -1, /* temp1..temp6 */
  389. 12, 9 }; /* intrusion0, intrusion1 */
  390. /* NCT6792/NCT6793 specific data */
  391. static const u16 NCT6792_REG_TEMP_MON[] = {
  392. 0x73, 0x75, 0x77, 0x79, 0x7b, 0x7d };
  393. static const u16 NCT6792_REG_BEEP[NUM_REG_BEEP] = {
  394. 0xb2, 0xb3, 0xb4, 0xb5, 0xbf };
  395. static const char *const nct6792_temp_label[] = {
  396. "",
  397. "SYSTIN",
  398. "CPUTIN",
  399. "AUXTIN0",
  400. "AUXTIN1",
  401. "AUXTIN2",
  402. "AUXTIN3",
  403. "",
  404. "SMBUSMASTER 0",
  405. "SMBUSMASTER 1",
  406. "SMBUSMASTER 2",
  407. "SMBUSMASTER 3",
  408. "SMBUSMASTER 4",
  409. "SMBUSMASTER 5",
  410. "SMBUSMASTER 6",
  411. "SMBUSMASTER 7",
  412. "PECI Agent 0",
  413. "PECI Agent 1",
  414. "PCH_CHIP_CPU_MAX_TEMP",
  415. "PCH_CHIP_TEMP",
  416. "PCH_CPU_TEMP",
  417. "PCH_MCH_TEMP",
  418. "PCH_DIM0_TEMP",
  419. "PCH_DIM1_TEMP",
  420. "PCH_DIM2_TEMP",
  421. "PCH_DIM3_TEMP",
  422. "BYTE_TEMP",
  423. "PECI Agent 0 Calibration",
  424. "PECI Agent 1 Calibration",
  425. "",
  426. "",
  427. "Virtual_TEMP"
  428. };
  429. #define NCT6792_TEMP_MASK 0x9fffff7e
  430. #define NCT6792_VIRT_TEMP_MASK 0x80000000
  431. static const char *const nct6793_temp_label[] = {
  432. "",
  433. "SYSTIN",
  434. "CPUTIN",
  435. "AUXTIN0",
  436. "AUXTIN1",
  437. "AUXTIN2",
  438. "AUXTIN3",
  439. "",
  440. "SMBUSMASTER 0",
  441. "SMBUSMASTER 1",
  442. "",
  443. "",
  444. "",
  445. "",
  446. "",
  447. "",
  448. "PECI Agent 0",
  449. "PECI Agent 1",
  450. "PCH_CHIP_CPU_MAX_TEMP",
  451. "PCH_CHIP_TEMP",
  452. "PCH_CPU_TEMP",
  453. "PCH_MCH_TEMP",
  454. "Agent0 Dimm0 ",
  455. "Agent0 Dimm1",
  456. "Agent1 Dimm0",
  457. "Agent1 Dimm1",
  458. "BYTE_TEMP0",
  459. "BYTE_TEMP1",
  460. "PECI Agent 0 Calibration",
  461. "PECI Agent 1 Calibration",
  462. "",
  463. "Virtual_TEMP"
  464. };
  465. #define NCT6793_TEMP_MASK 0xbfff037e
  466. #define NCT6793_VIRT_TEMP_MASK 0x80000000
  467. static const char *const nct6795_temp_label[] = {
  468. "",
  469. "SYSTIN",
  470. "CPUTIN",
  471. "AUXTIN0",
  472. "AUXTIN1",
  473. "AUXTIN2",
  474. "AUXTIN3",
  475. "",
  476. "SMBUSMASTER 0",
  477. "SMBUSMASTER 1",
  478. "SMBUSMASTER 2",
  479. "SMBUSMASTER 3",
  480. "SMBUSMASTER 4",
  481. "SMBUSMASTER 5",
  482. "SMBUSMASTER 6",
  483. "SMBUSMASTER 7",
  484. "PECI Agent 0",
  485. "PECI Agent 1",
  486. "PCH_CHIP_CPU_MAX_TEMP",
  487. "PCH_CHIP_TEMP",
  488. "PCH_CPU_TEMP",
  489. "PCH_MCH_TEMP",
  490. "Agent0 Dimm0",
  491. "Agent0 Dimm1",
  492. "Agent1 Dimm0",
  493. "Agent1 Dimm1",
  494. "BYTE_TEMP0",
  495. "BYTE_TEMP1",
  496. "PECI Agent 0 Calibration",
  497. "PECI Agent 1 Calibration",
  498. "",
  499. "Virtual_TEMP"
  500. };
  501. #define NCT6795_TEMP_MASK 0xbfffff7e
  502. #define NCT6795_VIRT_TEMP_MASK 0x80000000
  503. static const char *const nct6796_temp_label[] = {
  504. "",
  505. "SYSTIN",
  506. "CPUTIN",
  507. "AUXTIN0",
  508. "AUXTIN1",
  509. "AUXTIN2",
  510. "AUXTIN3",
  511. "AUXTIN4",
  512. "SMBUSMASTER 0",
  513. "SMBUSMASTER 1",
  514. "Virtual_TEMP",
  515. "Virtual_TEMP",
  516. "",
  517. "",
  518. "",
  519. "",
  520. "PECI Agent 0",
  521. "PECI Agent 1",
  522. "PCH_CHIP_CPU_MAX_TEMP",
  523. "PCH_CHIP_TEMP",
  524. "PCH_CPU_TEMP",
  525. "PCH_MCH_TEMP",
  526. "Agent0 Dimm0",
  527. "Agent0 Dimm1",
  528. "Agent1 Dimm0",
  529. "Agent1 Dimm1",
  530. "BYTE_TEMP0",
  531. "BYTE_TEMP1",
  532. "PECI Agent 0 Calibration",
  533. "PECI Agent 1 Calibration",
  534. "",
  535. "Virtual_TEMP"
  536. };
  537. #define NCT6796_TEMP_MASK 0xbfff0ffe
  538. #define NCT6796_VIRT_TEMP_MASK 0x80000c00
  539. static const u16 NCT6796_REG_TSI_TEMP[] = { 0x409, 0x40b };
  540. static const char *const nct6798_temp_label[] = {
  541. "",
  542. "SYSTIN",
  543. "CPUTIN",
  544. "AUXTIN0",
  545. "AUXTIN1",
  546. "AUXTIN2",
  547. "AUXTIN3",
  548. "AUXTIN4",
  549. "SMBUSMASTER 0",
  550. "SMBUSMASTER 1",
  551. "Virtual_TEMP",
  552. "Virtual_TEMP",
  553. "",
  554. "",
  555. "",
  556. "",
  557. "PECI Agent 0",
  558. "PECI Agent 1",
  559. "PCH_CHIP_CPU_MAX_TEMP",
  560. "PCH_CHIP_TEMP",
  561. "PCH_CPU_TEMP",
  562. "PCH_MCH_TEMP",
  563. "Agent0 Dimm0",
  564. "Agent0 Dimm1",
  565. "Agent1 Dimm0",
  566. "Agent1 Dimm1",
  567. "BYTE_TEMP0",
  568. "BYTE_TEMP1",
  569. "PECI Agent 0 Calibration", /* undocumented */
  570. "PECI Agent 1 Calibration", /* undocumented */
  571. "",
  572. "Virtual_TEMP"
  573. };
  574. #define NCT6798_TEMP_MASK 0xbfff0ffe
  575. #define NCT6798_VIRT_TEMP_MASK 0x80000c00
  576. /* NCT6102D/NCT6106D specific data */
  577. #define NCT6106_REG_VBAT 0x318
  578. #define NCT6106_REG_DIODE 0x319
  579. #define NCT6106_DIODE_MASK 0x01
  580. static const u16 NCT6106_REG_IN_MAX[] = {
  581. 0x90, 0x92, 0x94, 0x96, 0x98, 0x9a, 0x9e, 0xa0, 0xa2 };
  582. static const u16 NCT6106_REG_IN_MIN[] = {
  583. 0x91, 0x93, 0x95, 0x97, 0x99, 0x9b, 0x9f, 0xa1, 0xa3 };
  584. static const u16 NCT6106_REG_IN[] = {
  585. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x09 };
  586. static const u16 NCT6106_REG_TEMP[] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
  587. static const u16 NCT6106_REG_TEMP_MON[] = { 0x18, 0x19, 0x1a };
  588. static const u16 NCT6106_REG_TEMP_HYST[] = {
  589. 0xc3, 0xc7, 0xcb, 0xcf, 0xd3, 0xd7 };
  590. static const u16 NCT6106_REG_TEMP_OVER[] = {
  591. 0xc2, 0xc6, 0xca, 0xce, 0xd2, 0xd6 };
  592. static const u16 NCT6106_REG_TEMP_CRIT_L[] = {
  593. 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4 };
  594. static const u16 NCT6106_REG_TEMP_CRIT_H[] = {
  595. 0xc1, 0xc5, 0xc9, 0xcf, 0xd1, 0xd5 };
  596. static const u16 NCT6106_REG_TEMP_OFFSET[] = { 0x311, 0x312, 0x313 };
  597. static const u16 NCT6106_REG_TEMP_CONFIG[] = {
  598. 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc };
  599. static const u16 NCT6106_REG_FAN[] = { 0x20, 0x22, 0x24 };
  600. static const u16 NCT6106_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4 };
  601. static const u16 NCT6106_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6 };
  602. static const u16 NCT6106_FAN_PULSE_SHIFT[] = { 0, 2, 4 };
  603. static const u8 NCT6106_REG_PWM_MODE[] = { 0xf3, 0xf3, 0xf3 };
  604. static const u8 NCT6106_PWM_MODE_MASK[] = { 0x01, 0x02, 0x04 };
  605. static const u16 NCT6106_REG_PWM_READ[] = { 0x4a, 0x4b, 0x4c };
  606. static const u16 NCT6106_REG_FAN_MODE[] = { 0x113, 0x123, 0x133 };
  607. static const u16 NCT6106_REG_TEMP_SOURCE[] = {
  608. 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5 };
  609. static const u16 NCT6106_REG_CRITICAL_TEMP[] = { 0x11a, 0x12a, 0x13a };
  610. static const u16 NCT6106_REG_CRITICAL_TEMP_TOLERANCE[] = {
  611. 0x11b, 0x12b, 0x13b };
  612. static const u16 NCT6106_REG_CRITICAL_PWM_ENABLE[] = { 0x11c, 0x12c, 0x13c };
  613. #define NCT6106_CRITICAL_PWM_ENABLE_MASK 0x10
  614. static const u16 NCT6106_REG_CRITICAL_PWM[] = { 0x11d, 0x12d, 0x13d };
  615. static const u16 NCT6106_REG_FAN_STEP_UP_TIME[] = { 0x114, 0x124, 0x134 };
  616. static const u16 NCT6106_REG_FAN_STEP_DOWN_TIME[] = { 0x115, 0x125, 0x135 };
  617. static const u16 NCT6106_REG_FAN_STOP_OUTPUT[] = { 0x116, 0x126, 0x136 };
  618. static const u16 NCT6106_REG_FAN_START_OUTPUT[] = { 0x117, 0x127, 0x137 };
  619. static const u16 NCT6106_REG_FAN_STOP_TIME[] = { 0x118, 0x128, 0x138 };
  620. static const u16 NCT6106_REG_TOLERANCE_H[] = { 0x112, 0x122, 0x132 };
  621. static const u16 NCT6106_REG_TARGET[] = { 0x111, 0x121, 0x131 };
  622. static const u16 NCT6106_REG_WEIGHT_TEMP_SEL[] = { 0x168, 0x178, 0x188 };
  623. static const u16 NCT6106_REG_WEIGHT_TEMP_STEP[] = { 0x169, 0x179, 0x189 };
  624. static const u16 NCT6106_REG_WEIGHT_TEMP_STEP_TOL[] = { 0x16a, 0x17a, 0x18a };
  625. static const u16 NCT6106_REG_WEIGHT_DUTY_STEP[] = { 0x16b, 0x17b, 0x18b };
  626. static const u16 NCT6106_REG_WEIGHT_TEMP_BASE[] = { 0x16c, 0x17c, 0x18c };
  627. static const u16 NCT6106_REG_WEIGHT_DUTY_BASE[] = { 0x16d, 0x17d, 0x18d };
  628. static const u16 NCT6106_REG_AUTO_TEMP[] = { 0x160, 0x170, 0x180 };
  629. static const u16 NCT6106_REG_AUTO_PWM[] = { 0x164, 0x174, 0x184 };
  630. static const u16 NCT6106_REG_ALARM[NUM_REG_ALARM] = {
  631. 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d };
  632. static const s8 NCT6106_ALARM_BITS[] = {
  633. 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
  634. 9, -1, -1, -1, -1, -1, -1, /* in8..in14 */
  635. -1, /* unused */
  636. 32, 33, 34, -1, -1, /* fan1..fan5 */
  637. -1, -1, -1, /* unused */
  638. 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
  639. 48, -1 /* intrusion0, intrusion1 */
  640. };
  641. static const u16 NCT6106_REG_BEEP[NUM_REG_BEEP] = {
  642. 0x3c0, 0x3c1, 0x3c2, 0x3c3, 0x3c4 };
  643. static const s8 NCT6106_BEEP_BITS[] = {
  644. 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
  645. 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
  646. 32, /* global beep enable */
  647. 24, 25, 26, 27, 28, /* fan1..fan5 */
  648. -1, -1, -1, /* unused */
  649. 16, 17, 18, 19, 20, 21, /* temp1..temp6 */
  650. 34, -1 /* intrusion0, intrusion1 */
  651. };
  652. static const u16 NCT6106_REG_TEMP_ALTERNATE[32] = {
  653. [14] = 0x51,
  654. [15] = 0x52,
  655. [16] = 0x54,
  656. };
  657. static const u16 NCT6106_REG_TEMP_CRIT[32] = {
  658. [11] = 0x204,
  659. [12] = 0x205,
  660. };
  661. static const u16 NCT6106_REG_TSI_TEMP[] = { 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65, 0x67 };
  662. /* NCT6112D/NCT6114D/NCT6116D specific data */
  663. static const u16 NCT6116_REG_FAN[] = { 0x20, 0x22, 0x24, 0x26, 0x28 };
  664. static const u16 NCT6116_REG_FAN_MIN[] = { 0xe0, 0xe2, 0xe4, 0xe6, 0xe8 };
  665. static const u16 NCT6116_REG_FAN_PULSES[] = { 0xf6, 0xf6, 0xf6, 0xf6, 0xf5 };
  666. static const u16 NCT6116_FAN_PULSE_SHIFT[] = { 0, 2, 4, 6, 6 };
  667. static const u16 NCT6116_REG_PWM[] = { 0x119, 0x129, 0x139, 0x199, 0x1a9 };
  668. static const u16 NCT6116_REG_FAN_MODE[] = { 0x113, 0x123, 0x133, 0x193, 0x1a3 };
  669. static const u16 NCT6116_REG_TEMP_SEL[] = { 0x110, 0x120, 0x130, 0x190, 0x1a0 };
  670. static const u16 NCT6116_REG_TEMP_SOURCE[] = {
  671. 0xb0, 0xb1, 0xb2 };
  672. static const u16 NCT6116_REG_CRITICAL_TEMP[] = {
  673. 0x11a, 0x12a, 0x13a, 0x19a, 0x1aa };
  674. static const u16 NCT6116_REG_CRITICAL_TEMP_TOLERANCE[] = {
  675. 0x11b, 0x12b, 0x13b, 0x19b, 0x1ab };
  676. static const u16 NCT6116_REG_CRITICAL_PWM_ENABLE[] = {
  677. 0x11c, 0x12c, 0x13c, 0x19c, 0x1ac };
  678. static const u16 NCT6116_REG_CRITICAL_PWM[] = {
  679. 0x11d, 0x12d, 0x13d, 0x19d, 0x1ad };
  680. static const u16 NCT6116_REG_FAN_STEP_UP_TIME[] = {
  681. 0x114, 0x124, 0x134, 0x194, 0x1a4 };
  682. static const u16 NCT6116_REG_FAN_STEP_DOWN_TIME[] = {
  683. 0x115, 0x125, 0x135, 0x195, 0x1a5 };
  684. static const u16 NCT6116_REG_FAN_STOP_OUTPUT[] = {
  685. 0x116, 0x126, 0x136, 0x196, 0x1a6 };
  686. static const u16 NCT6116_REG_FAN_START_OUTPUT[] = {
  687. 0x117, 0x127, 0x137, 0x197, 0x1a7 };
  688. static const u16 NCT6116_REG_FAN_STOP_TIME[] = {
  689. 0x118, 0x128, 0x138, 0x198, 0x1a8 };
  690. static const u16 NCT6116_REG_TOLERANCE_H[] = {
  691. 0x112, 0x122, 0x132, 0x192, 0x1a2 };
  692. static const u16 NCT6116_REG_TARGET[] = {
  693. 0x111, 0x121, 0x131, 0x191, 0x1a1 };
  694. static const u16 NCT6116_REG_AUTO_TEMP[] = {
  695. 0x160, 0x170, 0x180, 0x1d0, 0x1e0 };
  696. static const u16 NCT6116_REG_AUTO_PWM[] = {
  697. 0x164, 0x174, 0x184, 0x1d4, 0x1e4 };
  698. static const s8 NCT6116_ALARM_BITS[] = {
  699. 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
  700. 9, -1, -1, -1, -1, -1, -1, /* in8..in9 */
  701. -1, /* unused */
  702. 32, 33, 34, 35, 36, /* fan1..fan5 */
  703. -1, -1, -1, /* unused */
  704. 16, 17, 18, -1, -1, -1, /* temp1..temp6 */
  705. 48, -1 /* intrusion0, intrusion1 */
  706. };
  707. static const s8 NCT6116_BEEP_BITS[] = {
  708. 0, 1, 2, 3, 4, 5, 7, 8, /* in0.. in7 */
  709. 9, 10, 11, 12, -1, -1, -1, /* in8..in14 */
  710. 32, /* global beep enable */
  711. 24, 25, 26, 27, 28, /* fan1..fan5 */
  712. -1, -1, -1, /* unused */
  713. 16, 17, 18, -1, -1, -1, /* temp1..temp6 */
  714. 34, -1 /* intrusion0, intrusion1 */
  715. };
  716. static const u16 NCT6116_REG_TSI_TEMP[] = { 0x59, 0x5b };
  717. static enum pwm_enable reg_to_pwm_enable(int pwm, int mode)
  718. {
  719. if (mode == 0 && pwm == 255)
  720. return off;
  721. return mode + 1;
  722. }
  723. static int pwm_enable_to_reg(enum pwm_enable mode)
  724. {
  725. if (mode == off)
  726. return 0;
  727. return mode - 1;
  728. }
  729. /*
  730. * Conversions
  731. */
  732. /* 1 is DC mode, output in ms */
  733. static unsigned int step_time_from_reg(u8 reg, u8 mode)
  734. {
  735. return mode ? 400 * reg : 100 * reg;
  736. }
  737. static u8 step_time_to_reg(unsigned int msec, u8 mode)
  738. {
  739. return clamp_val((mode ? (msec + 200) / 400 :
  740. (msec + 50) / 100), 1, 255);
  741. }
  742. static unsigned int fan_from_reg8(u16 reg, unsigned int divreg)
  743. {
  744. if (reg == 0 || reg == 255)
  745. return 0;
  746. return 1350000U / (reg << divreg);
  747. }
  748. static unsigned int fan_from_reg13(u16 reg, unsigned int divreg)
  749. {
  750. if ((reg & 0xff1f) == 0xff1f)
  751. return 0;
  752. reg = (reg & 0x1f) | ((reg & 0xff00) >> 3);
  753. if (reg == 0)
  754. return 0;
  755. return 1350000U / reg;
  756. }
  757. static unsigned int fan_from_reg16(u16 reg, unsigned int divreg)
  758. {
  759. if (reg == 0 || reg == 0xffff)
  760. return 0;
  761. /*
  762. * Even though the registers are 16 bit wide, the fan divisor
  763. * still applies.
  764. */
  765. return 1350000U / (reg << divreg);
  766. }
  767. static unsigned int fan_from_reg_rpm(u16 reg, unsigned int divreg)
  768. {
  769. return reg;
  770. }
  771. static u16 fan_to_reg(u32 fan, unsigned int divreg)
  772. {
  773. if (!fan)
  774. return 0;
  775. return (1350000U / fan) >> divreg;
  776. }
  777. static inline unsigned int
  778. div_from_reg(u8 reg)
  779. {
  780. return BIT(reg);
  781. }
  782. /*
  783. * Some of the voltage inputs have internal scaling, the tables below
  784. * contain 8 (the ADC LSB in mV) * scaling factor * 100
  785. */
  786. static const u16 scale_in[15] = {
  787. 800, 800, 1600, 1600, 800, 800, 800, 1600, 1600, 800, 800, 800, 800,
  788. 800, 800
  789. };
  790. static inline long in_from_reg(u8 reg, u8 nr)
  791. {
  792. return DIV_ROUND_CLOSEST(reg * scale_in[nr], 100);
  793. }
  794. static inline u8 in_to_reg(u32 val, u8 nr)
  795. {
  796. return clamp_val(DIV_ROUND_CLOSEST(val * 100, scale_in[nr]), 0, 255);
  797. }
  798. /* TSI temperatures are in 8.3 format */
  799. static inline unsigned int tsi_temp_from_reg(unsigned int reg)
  800. {
  801. return (reg >> 5) * 125;
  802. }
  803. /*
  804. * Data structures and manipulation thereof
  805. */
  806. struct sensor_device_template {
  807. struct device_attribute dev_attr;
  808. union {
  809. struct {
  810. u8 nr;
  811. u8 index;
  812. } s;
  813. int index;
  814. } u;
  815. bool s2; /* true if both index and nr are used */
  816. };
  817. struct sensor_device_attr_u {
  818. union {
  819. struct sensor_device_attribute a1;
  820. struct sensor_device_attribute_2 a2;
  821. } u;
  822. char name[32];
  823. };
  824. #define __TEMPLATE_ATTR(_template, _mode, _show, _store) { \
  825. .attr = {.name = _template, .mode = _mode }, \
  826. .show = _show, \
  827. .store = _store, \
  828. }
  829. #define SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, _index) \
  830. { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
  831. .u.index = _index, \
  832. .s2 = false }
  833. #define SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
  834. _nr, _index) \
  835. { .dev_attr = __TEMPLATE_ATTR(_template, _mode, _show, _store), \
  836. .u.s.index = _index, \
  837. .u.s.nr = _nr, \
  838. .s2 = true }
  839. #define SENSOR_TEMPLATE(_name, _template, _mode, _show, _store, _index) \
  840. static struct sensor_device_template sensor_dev_template_##_name \
  841. = SENSOR_DEVICE_TEMPLATE(_template, _mode, _show, _store, \
  842. _index)
  843. #define SENSOR_TEMPLATE_2(_name, _template, _mode, _show, _store, \
  844. _nr, _index) \
  845. static struct sensor_device_template sensor_dev_template_##_name \
  846. = SENSOR_DEVICE_TEMPLATE_2(_template, _mode, _show, _store, \
  847. _nr, _index)
  848. struct sensor_template_group {
  849. struct sensor_device_template **templates;
  850. umode_t (*is_visible)(struct kobject *, struct attribute *, int);
  851. int base;
  852. };
  853. static int nct6775_add_template_attr_group(struct device *dev, struct nct6775_data *data,
  854. const struct sensor_template_group *tg, int repeat)
  855. {
  856. struct attribute_group *group;
  857. struct sensor_device_attr_u *su;
  858. struct sensor_device_attribute *a;
  859. struct sensor_device_attribute_2 *a2;
  860. struct attribute **attrs;
  861. struct sensor_device_template **t;
  862. int i, count;
  863. if (repeat <= 0)
  864. return -EINVAL;
  865. t = tg->templates;
  866. for (count = 0; *t; t++, count++)
  867. ;
  868. if (count == 0)
  869. return -EINVAL;
  870. group = devm_kzalloc(dev, sizeof(*group), GFP_KERNEL);
  871. if (group == NULL)
  872. return -ENOMEM;
  873. attrs = devm_kcalloc(dev, repeat * count + 1, sizeof(*attrs),
  874. GFP_KERNEL);
  875. if (attrs == NULL)
  876. return -ENOMEM;
  877. su = devm_kzalloc(dev, array3_size(repeat, count, sizeof(*su)),
  878. GFP_KERNEL);
  879. if (su == NULL)
  880. return -ENOMEM;
  881. group->attrs = attrs;
  882. group->is_visible = tg->is_visible;
  883. for (i = 0; i < repeat; i++) {
  884. t = tg->templates;
  885. while (*t != NULL) {
  886. snprintf(su->name, sizeof(su->name),
  887. (*t)->dev_attr.attr.name, tg->base + i);
  888. if ((*t)->s2) {
  889. a2 = &su->u.a2;
  890. sysfs_attr_init(&a2->dev_attr.attr);
  891. a2->dev_attr.attr.name = su->name;
  892. a2->nr = (*t)->u.s.nr + i;
  893. a2->index = (*t)->u.s.index;
  894. a2->dev_attr.attr.mode =
  895. (*t)->dev_attr.attr.mode;
  896. a2->dev_attr.show = (*t)->dev_attr.show;
  897. a2->dev_attr.store = (*t)->dev_attr.store;
  898. *attrs = &a2->dev_attr.attr;
  899. } else {
  900. a = &su->u.a1;
  901. sysfs_attr_init(&a->dev_attr.attr);
  902. a->dev_attr.attr.name = su->name;
  903. a->index = (*t)->u.index + i;
  904. a->dev_attr.attr.mode =
  905. (*t)->dev_attr.attr.mode;
  906. a->dev_attr.show = (*t)->dev_attr.show;
  907. a->dev_attr.store = (*t)->dev_attr.store;
  908. *attrs = &a->dev_attr.attr;
  909. }
  910. attrs++;
  911. su++;
  912. t++;
  913. }
  914. }
  915. return nct6775_add_attr_group(data, group);
  916. }
  917. bool nct6775_reg_is_word_sized(struct nct6775_data *data, u16 reg)
  918. {
  919. switch (data->kind) {
  920. case nct6106:
  921. return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
  922. (reg >= 0x59 && reg < 0x69 && (reg & 1)) ||
  923. reg == 0xe0 || reg == 0xe2 || reg == 0xe4 ||
  924. reg == 0x111 || reg == 0x121 || reg == 0x131;
  925. case nct6116:
  926. return reg == 0x20 || reg == 0x22 || reg == 0x24 ||
  927. reg == 0x26 || reg == 0x28 || reg == 0x59 || reg == 0x5b ||
  928. reg == 0xe0 || reg == 0xe2 || reg == 0xe4 || reg == 0xe6 ||
  929. reg == 0xe8 || reg == 0x111 || reg == 0x121 || reg == 0x131 ||
  930. reg == 0x191 || reg == 0x1a1;
  931. case nct6775:
  932. return (((reg & 0xff00) == 0x100 ||
  933. (reg & 0xff00) == 0x200) &&
  934. ((reg & 0x00ff) == 0x50 ||
  935. (reg & 0x00ff) == 0x53 ||
  936. (reg & 0x00ff) == 0x55)) ||
  937. (reg & 0xfff0) == 0x630 ||
  938. reg == 0x640 || reg == 0x642 ||
  939. reg == 0x662 || reg == 0x669 ||
  940. ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
  941. reg == 0x73 || reg == 0x75 || reg == 0x77;
  942. case nct6776:
  943. return (((reg & 0xff00) == 0x100 ||
  944. (reg & 0xff00) == 0x200) &&
  945. ((reg & 0x00ff) == 0x50 ||
  946. (reg & 0x00ff) == 0x53 ||
  947. (reg & 0x00ff) == 0x55)) ||
  948. (reg & 0xfff0) == 0x630 ||
  949. reg == 0x402 ||
  950. (reg >= 0x409 && reg < 0x419 && (reg & 1)) ||
  951. reg == 0x640 || reg == 0x642 ||
  952. ((reg & 0xfff0) == 0x650 && (reg & 0x000f) >= 0x06) ||
  953. reg == 0x73 || reg == 0x75 || reg == 0x77;
  954. case nct6779:
  955. case nct6791:
  956. case nct6792:
  957. case nct6793:
  958. case nct6795:
  959. case nct6796:
  960. case nct6797:
  961. case nct6798:
  962. return reg == 0x150 || reg == 0x153 || reg == 0x155 ||
  963. (reg & 0xfff0) == 0x4c0 ||
  964. reg == 0x402 ||
  965. (reg >= 0x409 && reg < 0x419 && (reg & 1)) ||
  966. reg == 0x63a || reg == 0x63c || reg == 0x63e ||
  967. reg == 0x640 || reg == 0x642 || reg == 0x64a ||
  968. reg == 0x64c ||
  969. reg == 0x73 || reg == 0x75 || reg == 0x77 || reg == 0x79 ||
  970. reg == 0x7b || reg == 0x7d;
  971. }
  972. return false;
  973. }
  974. EXPORT_SYMBOL_GPL(nct6775_reg_is_word_sized);
  975. /* We left-align 8-bit temperature values to make the code simpler */
  976. static int nct6775_read_temp(struct nct6775_data *data, u16 reg, u16 *val)
  977. {
  978. int err;
  979. err = nct6775_read_value(data, reg, val);
  980. if (err)
  981. return err;
  982. if (!nct6775_reg_is_word_sized(data, reg))
  983. *val <<= 8;
  984. return 0;
  985. }
  986. /* This function assumes that the caller holds data->update_lock */
  987. static int nct6775_write_fan_div(struct nct6775_data *data, int nr)
  988. {
  989. u16 reg;
  990. int err;
  991. u16 fandiv_reg = nr < 2 ? NCT6775_REG_FANDIV1 : NCT6775_REG_FANDIV2;
  992. unsigned int oddshift = (nr & 1) * 4; /* masks shift by four if nr is odd */
  993. err = nct6775_read_value(data, fandiv_reg, &reg);
  994. if (err)
  995. return err;
  996. reg &= 0x70 >> oddshift;
  997. reg |= (data->fan_div[nr] & 0x7) << oddshift;
  998. return nct6775_write_value(data, fandiv_reg, reg);
  999. }
  1000. static int nct6775_write_fan_div_common(struct nct6775_data *data, int nr)
  1001. {
  1002. if (data->kind == nct6775)
  1003. return nct6775_write_fan_div(data, nr);
  1004. return 0;
  1005. }
  1006. static int nct6775_update_fan_div(struct nct6775_data *data)
  1007. {
  1008. int err;
  1009. u16 i;
  1010. err = nct6775_read_value(data, NCT6775_REG_FANDIV1, &i);
  1011. if (err)
  1012. return err;
  1013. data->fan_div[0] = i & 0x7;
  1014. data->fan_div[1] = (i & 0x70) >> 4;
  1015. err = nct6775_read_value(data, NCT6775_REG_FANDIV2, &i);
  1016. if (err)
  1017. return err;
  1018. data->fan_div[2] = i & 0x7;
  1019. if (data->has_fan & BIT(3))
  1020. data->fan_div[3] = (i & 0x70) >> 4;
  1021. return 0;
  1022. }
  1023. static int nct6775_update_fan_div_common(struct nct6775_data *data)
  1024. {
  1025. if (data->kind == nct6775)
  1026. return nct6775_update_fan_div(data);
  1027. return 0;
  1028. }
  1029. static int nct6775_init_fan_div(struct nct6775_data *data)
  1030. {
  1031. int i, err;
  1032. err = nct6775_update_fan_div_common(data);
  1033. if (err)
  1034. return err;
  1035. /*
  1036. * For all fans, start with highest divider value if the divider
  1037. * register is not initialized. This ensures that we get a
  1038. * reading from the fan count register, even if it is not optimal.
  1039. * We'll compute a better divider later on.
  1040. */
  1041. for (i = 0; i < ARRAY_SIZE(data->fan_div); i++) {
  1042. if (!(data->has_fan & BIT(i)))
  1043. continue;
  1044. if (data->fan_div[i] == 0) {
  1045. data->fan_div[i] = 7;
  1046. err = nct6775_write_fan_div_common(data, i);
  1047. if (err)
  1048. return err;
  1049. }
  1050. }
  1051. return 0;
  1052. }
  1053. static int nct6775_init_fan_common(struct device *dev,
  1054. struct nct6775_data *data)
  1055. {
  1056. int i, err;
  1057. u16 reg;
  1058. if (data->has_fan_div) {
  1059. err = nct6775_init_fan_div(data);
  1060. if (err)
  1061. return err;
  1062. }
  1063. /*
  1064. * If fan_min is not set (0), set it to 0xff to disable it. This
  1065. * prevents the unnecessary warning when fanX_min is reported as 0.
  1066. */
  1067. for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
  1068. if (data->has_fan_min & BIT(i)) {
  1069. err = nct6775_read_value(data, data->REG_FAN_MIN[i], &reg);
  1070. if (err)
  1071. return err;
  1072. if (!reg) {
  1073. err = nct6775_write_value(data, data->REG_FAN_MIN[i],
  1074. data->has_fan_div ? 0xff : 0xff1f);
  1075. if (err)
  1076. return err;
  1077. }
  1078. }
  1079. }
  1080. return 0;
  1081. }
  1082. static int nct6775_select_fan_div(struct device *dev,
  1083. struct nct6775_data *data, int nr, u16 reg)
  1084. {
  1085. int err;
  1086. u8 fan_div = data->fan_div[nr];
  1087. u16 fan_min;
  1088. if (!data->has_fan_div)
  1089. return 0;
  1090. /*
  1091. * If we failed to measure the fan speed, or the reported value is not
  1092. * in the optimal range, and the clock divider can be modified,
  1093. * let's try that for next time.
  1094. */
  1095. if (reg == 0x00 && fan_div < 0x07)
  1096. fan_div++;
  1097. else if (reg != 0x00 && reg < 0x30 && fan_div > 0)
  1098. fan_div--;
  1099. if (fan_div != data->fan_div[nr]) {
  1100. dev_dbg(dev, "Modifying fan%d clock divider from %u to %u\n",
  1101. nr + 1, div_from_reg(data->fan_div[nr]),
  1102. div_from_reg(fan_div));
  1103. /* Preserve min limit if possible */
  1104. if (data->has_fan_min & BIT(nr)) {
  1105. fan_min = data->fan_min[nr];
  1106. if (fan_div > data->fan_div[nr]) {
  1107. if (fan_min != 255 && fan_min > 1)
  1108. fan_min >>= 1;
  1109. } else {
  1110. if (fan_min != 255) {
  1111. fan_min <<= 1;
  1112. if (fan_min > 254)
  1113. fan_min = 254;
  1114. }
  1115. }
  1116. if (fan_min != data->fan_min[nr]) {
  1117. data->fan_min[nr] = fan_min;
  1118. err = nct6775_write_value(data, data->REG_FAN_MIN[nr], fan_min);
  1119. if (err)
  1120. return err;
  1121. }
  1122. }
  1123. data->fan_div[nr] = fan_div;
  1124. err = nct6775_write_fan_div_common(data, nr);
  1125. if (err)
  1126. return err;
  1127. }
  1128. return 0;
  1129. }
  1130. static int nct6775_update_pwm(struct device *dev)
  1131. {
  1132. struct nct6775_data *data = dev_get_drvdata(dev);
  1133. int i, j, err;
  1134. u16 fanmodecfg, reg;
  1135. bool duty_is_dc;
  1136. for (i = 0; i < data->pwm_num; i++) {
  1137. if (!(data->has_pwm & BIT(i)))
  1138. continue;
  1139. err = nct6775_read_value(data, data->REG_PWM_MODE[i], &reg);
  1140. if (err)
  1141. return err;
  1142. duty_is_dc = data->REG_PWM_MODE[i] && (reg & data->PWM_MODE_MASK[i]);
  1143. data->pwm_mode[i] = !duty_is_dc;
  1144. err = nct6775_read_value(data, data->REG_FAN_MODE[i], &fanmodecfg);
  1145. if (err)
  1146. return err;
  1147. for (j = 0; j < ARRAY_SIZE(data->REG_PWM); j++) {
  1148. if (data->REG_PWM[j] && data->REG_PWM[j][i]) {
  1149. err = nct6775_read_value(data, data->REG_PWM[j][i], &reg);
  1150. if (err)
  1151. return err;
  1152. data->pwm[j][i] = reg;
  1153. }
  1154. }
  1155. data->pwm_enable[i] = reg_to_pwm_enable(data->pwm[0][i],
  1156. (fanmodecfg >> 4) & 7);
  1157. if (!data->temp_tolerance[0][i] ||
  1158. data->pwm_enable[i] != speed_cruise)
  1159. data->temp_tolerance[0][i] = fanmodecfg & 0x0f;
  1160. if (!data->target_speed_tolerance[i] ||
  1161. data->pwm_enable[i] == speed_cruise) {
  1162. u8 t = fanmodecfg & 0x0f;
  1163. if (data->REG_TOLERANCE_H) {
  1164. err = nct6775_read_value(data, data->REG_TOLERANCE_H[i], &reg);
  1165. if (err)
  1166. return err;
  1167. t |= (reg & 0x70) >> 1;
  1168. }
  1169. data->target_speed_tolerance[i] = t;
  1170. }
  1171. err = nct6775_read_value(data, data->REG_CRITICAL_TEMP_TOLERANCE[i], &reg);
  1172. if (err)
  1173. return err;
  1174. data->temp_tolerance[1][i] = reg;
  1175. err = nct6775_read_value(data, data->REG_TEMP_SEL[i], &reg);
  1176. if (err)
  1177. return err;
  1178. data->pwm_temp_sel[i] = reg & 0x1f;
  1179. /* If fan can stop, report floor as 0 */
  1180. if (reg & 0x80)
  1181. data->pwm[2][i] = 0;
  1182. if (!data->REG_WEIGHT_TEMP_SEL[i])
  1183. continue;
  1184. err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[i], &reg);
  1185. if (err)
  1186. return err;
  1187. data->pwm_weight_temp_sel[i] = reg & 0x1f;
  1188. /* If weight is disabled, report weight source as 0 */
  1189. if (!(reg & 0x80))
  1190. data->pwm_weight_temp_sel[i] = 0;
  1191. /* Weight temp data */
  1192. for (j = 0; j < ARRAY_SIZE(data->weight_temp); j++) {
  1193. err = nct6775_read_value(data, data->REG_WEIGHT_TEMP[j][i], &reg);
  1194. if (err)
  1195. return err;
  1196. data->weight_temp[j][i] = reg;
  1197. }
  1198. }
  1199. return 0;
  1200. }
  1201. static int nct6775_update_pwm_limits(struct device *dev)
  1202. {
  1203. struct nct6775_data *data = dev_get_drvdata(dev);
  1204. int i, j, err;
  1205. u16 reg, reg_t;
  1206. for (i = 0; i < data->pwm_num; i++) {
  1207. if (!(data->has_pwm & BIT(i)))
  1208. continue;
  1209. for (j = 0; j < ARRAY_SIZE(data->fan_time); j++) {
  1210. err = nct6775_read_value(data, data->REG_FAN_TIME[j][i], &reg);
  1211. if (err)
  1212. return err;
  1213. data->fan_time[j][i] = reg;
  1214. }
  1215. err = nct6775_read_value(data, data->REG_TARGET[i], &reg_t);
  1216. if (err)
  1217. return err;
  1218. /* Update only in matching mode or if never updated */
  1219. if (!data->target_temp[i] ||
  1220. data->pwm_enable[i] == thermal_cruise)
  1221. data->target_temp[i] = reg_t & data->target_temp_mask;
  1222. if (!data->target_speed[i] ||
  1223. data->pwm_enable[i] == speed_cruise) {
  1224. if (data->REG_TOLERANCE_H) {
  1225. err = nct6775_read_value(data, data->REG_TOLERANCE_H[i], &reg);
  1226. if (err)
  1227. return err;
  1228. reg_t |= (reg & 0x0f) << 8;
  1229. }
  1230. data->target_speed[i] = reg_t;
  1231. }
  1232. for (j = 0; j < data->auto_pwm_num; j++) {
  1233. err = nct6775_read_value(data, NCT6775_AUTO_PWM(data, i, j), &reg);
  1234. if (err)
  1235. return err;
  1236. data->auto_pwm[i][j] = reg;
  1237. err = nct6775_read_value(data, NCT6775_AUTO_TEMP(data, i, j), &reg);
  1238. if (err)
  1239. return err;
  1240. data->auto_temp[i][j] = reg;
  1241. }
  1242. /* critical auto_pwm temperature data */
  1243. err = nct6775_read_value(data, data->REG_CRITICAL_TEMP[i], &reg);
  1244. if (err)
  1245. return err;
  1246. data->auto_temp[i][data->auto_pwm_num] = reg;
  1247. switch (data->kind) {
  1248. case nct6775:
  1249. err = nct6775_read_value(data, NCT6775_REG_CRITICAL_ENAB[i], &reg);
  1250. if (err)
  1251. return err;
  1252. data->auto_pwm[i][data->auto_pwm_num] =
  1253. (reg & 0x02) ? 0xff : 0x00;
  1254. break;
  1255. case nct6776:
  1256. data->auto_pwm[i][data->auto_pwm_num] = 0xff;
  1257. break;
  1258. case nct6106:
  1259. case nct6116:
  1260. case nct6779:
  1261. case nct6791:
  1262. case nct6792:
  1263. case nct6793:
  1264. case nct6795:
  1265. case nct6796:
  1266. case nct6797:
  1267. case nct6798:
  1268. err = nct6775_read_value(data, data->REG_CRITICAL_PWM_ENABLE[i], &reg);
  1269. if (err)
  1270. return err;
  1271. if (reg & data->CRITICAL_PWM_ENABLE_MASK) {
  1272. err = nct6775_read_value(data, data->REG_CRITICAL_PWM[i], &reg);
  1273. if (err)
  1274. return err;
  1275. } else {
  1276. reg = 0xff;
  1277. }
  1278. data->auto_pwm[i][data->auto_pwm_num] = reg;
  1279. break;
  1280. }
  1281. }
  1282. return 0;
  1283. }
  1284. struct nct6775_data *nct6775_update_device(struct device *dev)
  1285. {
  1286. struct nct6775_data *data = dev_get_drvdata(dev);
  1287. int i, j, err = 0;
  1288. u16 reg;
  1289. mutex_lock(&data->update_lock);
  1290. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  1291. || !data->valid) {
  1292. /* Fan clock dividers */
  1293. err = nct6775_update_fan_div_common(data);
  1294. if (err)
  1295. goto out;
  1296. /* Measured voltages and limits */
  1297. for (i = 0; i < data->in_num; i++) {
  1298. if (!(data->have_in & BIT(i)))
  1299. continue;
  1300. err = nct6775_read_value(data, data->REG_VIN[i], &reg);
  1301. if (err)
  1302. goto out;
  1303. data->in[i][0] = reg;
  1304. err = nct6775_read_value(data, data->REG_IN_MINMAX[0][i], &reg);
  1305. if (err)
  1306. goto out;
  1307. data->in[i][1] = reg;
  1308. err = nct6775_read_value(data, data->REG_IN_MINMAX[1][i], &reg);
  1309. if (err)
  1310. goto out;
  1311. data->in[i][2] = reg;
  1312. }
  1313. /* Measured fan speeds and limits */
  1314. for (i = 0; i < ARRAY_SIZE(data->rpm); i++) {
  1315. if (!(data->has_fan & BIT(i)))
  1316. continue;
  1317. err = nct6775_read_value(data, data->REG_FAN[i], &reg);
  1318. if (err)
  1319. goto out;
  1320. data->rpm[i] = data->fan_from_reg(reg,
  1321. data->fan_div[i]);
  1322. if (data->has_fan_min & BIT(i)) {
  1323. u16 tmp;
  1324. err = nct6775_read_value(data, data->REG_FAN_MIN[i], &tmp);
  1325. if (err)
  1326. goto out;
  1327. data->fan_min[i] = tmp;
  1328. }
  1329. if (data->REG_FAN_PULSES[i]) {
  1330. u16 tmp;
  1331. err = nct6775_read_value(data, data->REG_FAN_PULSES[i], &tmp);
  1332. if (err)
  1333. goto out;
  1334. data->fan_pulses[i] = (tmp >> data->FAN_PULSE_SHIFT[i]) & 0x03;
  1335. }
  1336. err = nct6775_select_fan_div(dev, data, i, reg);
  1337. if (err)
  1338. goto out;
  1339. }
  1340. err = nct6775_update_pwm(dev);
  1341. if (err)
  1342. goto out;
  1343. err = nct6775_update_pwm_limits(dev);
  1344. if (err)
  1345. goto out;
  1346. /* Measured temperatures and limits */
  1347. for (i = 0; i < NUM_TEMP; i++) {
  1348. if (!(data->have_temp & BIT(i)))
  1349. continue;
  1350. for (j = 0; j < ARRAY_SIZE(data->reg_temp); j++) {
  1351. if (data->reg_temp[j][i]) {
  1352. err = nct6775_read_temp(data, data->reg_temp[j][i], &reg);
  1353. if (err)
  1354. goto out;
  1355. data->temp[j][i] = reg;
  1356. }
  1357. }
  1358. if (i >= NUM_TEMP_FIXED ||
  1359. !(data->have_temp_fixed & BIT(i)))
  1360. continue;
  1361. err = nct6775_read_value(data, data->REG_TEMP_OFFSET[i], &reg);
  1362. if (err)
  1363. goto out;
  1364. data->temp_offset[i] = reg;
  1365. }
  1366. for (i = 0; i < NUM_TSI_TEMP; i++) {
  1367. if (!(data->have_tsi_temp & BIT(i)))
  1368. continue;
  1369. err = nct6775_read_value(data, data->REG_TSI_TEMP[i], &reg);
  1370. if (err)
  1371. goto out;
  1372. data->tsi_temp[i] = reg;
  1373. }
  1374. data->alarms = 0;
  1375. for (i = 0; i < NUM_REG_ALARM; i++) {
  1376. u16 alarm;
  1377. if (!data->REG_ALARM[i])
  1378. continue;
  1379. err = nct6775_read_value(data, data->REG_ALARM[i], &alarm);
  1380. if (err)
  1381. goto out;
  1382. data->alarms |= ((u64)alarm) << (i << 3);
  1383. }
  1384. data->beeps = 0;
  1385. for (i = 0; i < NUM_REG_BEEP; i++) {
  1386. u16 beep;
  1387. if (!data->REG_BEEP[i])
  1388. continue;
  1389. err = nct6775_read_value(data, data->REG_BEEP[i], &beep);
  1390. if (err)
  1391. goto out;
  1392. data->beeps |= ((u64)beep) << (i << 3);
  1393. }
  1394. data->last_updated = jiffies;
  1395. data->valid = true;
  1396. }
  1397. out:
  1398. mutex_unlock(&data->update_lock);
  1399. return err ? ERR_PTR(err) : data;
  1400. }
  1401. EXPORT_SYMBOL_GPL(nct6775_update_device);
  1402. /*
  1403. * Sysfs callback functions
  1404. */
  1405. static ssize_t
  1406. show_in_reg(struct device *dev, struct device_attribute *attr, char *buf)
  1407. {
  1408. struct nct6775_data *data = nct6775_update_device(dev);
  1409. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1410. int index = sattr->index;
  1411. int nr = sattr->nr;
  1412. if (IS_ERR(data))
  1413. return PTR_ERR(data);
  1414. return sprintf(buf, "%ld\n", in_from_reg(data->in[nr][index], nr));
  1415. }
  1416. static ssize_t
  1417. store_in_reg(struct device *dev, struct device_attribute *attr, const char *buf,
  1418. size_t count)
  1419. {
  1420. struct nct6775_data *data = dev_get_drvdata(dev);
  1421. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1422. int index = sattr->index;
  1423. int nr = sattr->nr;
  1424. unsigned long val;
  1425. int err;
  1426. err = kstrtoul(buf, 10, &val);
  1427. if (err < 0)
  1428. return err;
  1429. mutex_lock(&data->update_lock);
  1430. data->in[nr][index] = in_to_reg(val, nr);
  1431. err = nct6775_write_value(data, data->REG_IN_MINMAX[index - 1][nr], data->in[nr][index]);
  1432. mutex_unlock(&data->update_lock);
  1433. return err ? : count;
  1434. }
  1435. ssize_t
  1436. nct6775_show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
  1437. {
  1438. struct nct6775_data *data = nct6775_update_device(dev);
  1439. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1440. int nr;
  1441. if (IS_ERR(data))
  1442. return PTR_ERR(data);
  1443. nr = data->ALARM_BITS[sattr->index];
  1444. return sprintf(buf, "%u\n",
  1445. (unsigned int)((data->alarms >> nr) & 0x01));
  1446. }
  1447. EXPORT_SYMBOL_GPL(nct6775_show_alarm);
  1448. static int find_temp_source(struct nct6775_data *data, int index, int count)
  1449. {
  1450. int source = data->temp_src[index];
  1451. int nr, err;
  1452. for (nr = 0; nr < count; nr++) {
  1453. u16 src;
  1454. err = nct6775_read_value(data, data->REG_TEMP_SOURCE[nr], &src);
  1455. if (err)
  1456. return err;
  1457. if ((src & 0x1f) == source)
  1458. return nr;
  1459. }
  1460. return -ENODEV;
  1461. }
  1462. static ssize_t
  1463. show_temp_alarm(struct device *dev, struct device_attribute *attr, char *buf)
  1464. {
  1465. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1466. struct nct6775_data *data = nct6775_update_device(dev);
  1467. unsigned int alarm = 0;
  1468. int nr;
  1469. if (IS_ERR(data))
  1470. return PTR_ERR(data);
  1471. /*
  1472. * For temperatures, there is no fixed mapping from registers to alarm
  1473. * bits. Alarm bits are determined by the temperature source mapping.
  1474. */
  1475. nr = find_temp_source(data, sattr->index, data->num_temp_alarms);
  1476. if (nr >= 0) {
  1477. int bit = data->ALARM_BITS[nr + TEMP_ALARM_BASE];
  1478. alarm = (data->alarms >> bit) & 0x01;
  1479. }
  1480. return sprintf(buf, "%u\n", alarm);
  1481. }
  1482. ssize_t
  1483. nct6775_show_beep(struct device *dev, struct device_attribute *attr, char *buf)
  1484. {
  1485. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1486. struct nct6775_data *data = nct6775_update_device(dev);
  1487. int nr;
  1488. if (IS_ERR(data))
  1489. return PTR_ERR(data);
  1490. nr = data->BEEP_BITS[sattr->index];
  1491. return sprintf(buf, "%u\n",
  1492. (unsigned int)((data->beeps >> nr) & 0x01));
  1493. }
  1494. EXPORT_SYMBOL_GPL(nct6775_show_beep);
  1495. ssize_t
  1496. nct6775_store_beep(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  1497. {
  1498. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1499. struct nct6775_data *data = dev_get_drvdata(dev);
  1500. int nr = data->BEEP_BITS[sattr->index];
  1501. int regindex = nr >> 3;
  1502. unsigned long val;
  1503. int err;
  1504. err = kstrtoul(buf, 10, &val);
  1505. if (err < 0)
  1506. return err;
  1507. if (val > 1)
  1508. return -EINVAL;
  1509. mutex_lock(&data->update_lock);
  1510. if (val)
  1511. data->beeps |= (1ULL << nr);
  1512. else
  1513. data->beeps &= ~(1ULL << nr);
  1514. err = nct6775_write_value(data, data->REG_BEEP[regindex],
  1515. (data->beeps >> (regindex << 3)) & 0xff);
  1516. mutex_unlock(&data->update_lock);
  1517. return err ? : count;
  1518. }
  1519. EXPORT_SYMBOL_GPL(nct6775_store_beep);
  1520. static ssize_t
  1521. show_temp_beep(struct device *dev, struct device_attribute *attr, char *buf)
  1522. {
  1523. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1524. struct nct6775_data *data = nct6775_update_device(dev);
  1525. unsigned int beep = 0;
  1526. int nr;
  1527. if (IS_ERR(data))
  1528. return PTR_ERR(data);
  1529. /*
  1530. * For temperatures, there is no fixed mapping from registers to beep
  1531. * enable bits. Beep enable bits are determined by the temperature
  1532. * source mapping.
  1533. */
  1534. nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
  1535. if (nr >= 0) {
  1536. int bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
  1537. beep = (data->beeps >> bit) & 0x01;
  1538. }
  1539. return sprintf(buf, "%u\n", beep);
  1540. }
  1541. static ssize_t
  1542. store_temp_beep(struct device *dev, struct device_attribute *attr,
  1543. const char *buf, size_t count)
  1544. {
  1545. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1546. struct nct6775_data *data = dev_get_drvdata(dev);
  1547. int nr, bit, regindex;
  1548. unsigned long val;
  1549. int err;
  1550. err = kstrtoul(buf, 10, &val);
  1551. if (err < 0)
  1552. return err;
  1553. if (val > 1)
  1554. return -EINVAL;
  1555. nr = find_temp_source(data, sattr->index, data->num_temp_beeps);
  1556. if (nr < 0)
  1557. return nr;
  1558. bit = data->BEEP_BITS[nr + TEMP_ALARM_BASE];
  1559. regindex = bit >> 3;
  1560. mutex_lock(&data->update_lock);
  1561. if (val)
  1562. data->beeps |= (1ULL << bit);
  1563. else
  1564. data->beeps &= ~(1ULL << bit);
  1565. err = nct6775_write_value(data, data->REG_BEEP[regindex],
  1566. (data->beeps >> (regindex << 3)) & 0xff);
  1567. mutex_unlock(&data->update_lock);
  1568. return err ? : count;
  1569. }
  1570. static umode_t nct6775_in_is_visible(struct kobject *kobj,
  1571. struct attribute *attr, int index)
  1572. {
  1573. struct device *dev = kobj_to_dev(kobj);
  1574. struct nct6775_data *data = dev_get_drvdata(dev);
  1575. int in = index / 5; /* voltage index */
  1576. if (!(data->have_in & BIT(in)))
  1577. return 0;
  1578. return nct6775_attr_mode(data, attr);
  1579. }
  1580. SENSOR_TEMPLATE_2(in_input, "in%d_input", 0444, show_in_reg, NULL, 0, 0);
  1581. SENSOR_TEMPLATE(in_alarm, "in%d_alarm", 0444, nct6775_show_alarm, NULL, 0);
  1582. SENSOR_TEMPLATE(in_beep, "in%d_beep", 0644, nct6775_show_beep, nct6775_store_beep, 0);
  1583. SENSOR_TEMPLATE_2(in_min, "in%d_min", 0644, show_in_reg, store_in_reg, 0, 1);
  1584. SENSOR_TEMPLATE_2(in_max, "in%d_max", 0644, show_in_reg, store_in_reg, 0, 2);
  1585. /*
  1586. * nct6775_in_is_visible uses the index into the following array
  1587. * to determine if attributes should be created or not.
  1588. * Any change in order or content must be matched.
  1589. */
  1590. static struct sensor_device_template *nct6775_attributes_in_template[] = {
  1591. &sensor_dev_template_in_input,
  1592. &sensor_dev_template_in_alarm,
  1593. &sensor_dev_template_in_beep,
  1594. &sensor_dev_template_in_min,
  1595. &sensor_dev_template_in_max,
  1596. NULL
  1597. };
  1598. static const struct sensor_template_group nct6775_in_template_group = {
  1599. .templates = nct6775_attributes_in_template,
  1600. .is_visible = nct6775_in_is_visible,
  1601. };
  1602. static ssize_t
  1603. show_fan(struct device *dev, struct device_attribute *attr, char *buf)
  1604. {
  1605. struct nct6775_data *data = nct6775_update_device(dev);
  1606. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1607. int nr = sattr->index;
  1608. if (IS_ERR(data))
  1609. return PTR_ERR(data);
  1610. return sprintf(buf, "%d\n", data->rpm[nr]);
  1611. }
  1612. static ssize_t
  1613. show_fan_min(struct device *dev, struct device_attribute *attr, char *buf)
  1614. {
  1615. struct nct6775_data *data = nct6775_update_device(dev);
  1616. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1617. int nr = sattr->index;
  1618. if (IS_ERR(data))
  1619. return PTR_ERR(data);
  1620. return sprintf(buf, "%d\n",
  1621. data->fan_from_reg_min(data->fan_min[nr],
  1622. data->fan_div[nr]));
  1623. }
  1624. static ssize_t
  1625. show_fan_div(struct device *dev, struct device_attribute *attr, char *buf)
  1626. {
  1627. struct nct6775_data *data = nct6775_update_device(dev);
  1628. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1629. int nr = sattr->index;
  1630. if (IS_ERR(data))
  1631. return PTR_ERR(data);
  1632. return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
  1633. }
  1634. static ssize_t
  1635. store_fan_min(struct device *dev, struct device_attribute *attr,
  1636. const char *buf, size_t count)
  1637. {
  1638. struct nct6775_data *data = dev_get_drvdata(dev);
  1639. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1640. int nr = sattr->index;
  1641. unsigned long val;
  1642. unsigned int reg;
  1643. u8 new_div;
  1644. int err;
  1645. err = kstrtoul(buf, 10, &val);
  1646. if (err < 0)
  1647. return err;
  1648. mutex_lock(&data->update_lock);
  1649. if (!data->has_fan_div) {
  1650. /* NCT6776F or NCT6779D; we know this is a 13 bit register */
  1651. if (!val) {
  1652. val = 0xff1f;
  1653. } else {
  1654. if (val > 1350000U)
  1655. val = 135000U;
  1656. val = 1350000U / val;
  1657. val = (val & 0x1f) | ((val << 3) & 0xff00);
  1658. }
  1659. data->fan_min[nr] = val;
  1660. goto write_min; /* Leave fan divider alone */
  1661. }
  1662. if (!val) {
  1663. /* No min limit, alarm disabled */
  1664. data->fan_min[nr] = 255;
  1665. new_div = data->fan_div[nr]; /* No change */
  1666. dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
  1667. goto write_div;
  1668. }
  1669. reg = 1350000U / val;
  1670. if (reg >= 128 * 255) {
  1671. /*
  1672. * Speed below this value cannot possibly be represented,
  1673. * even with the highest divider (128)
  1674. */
  1675. data->fan_min[nr] = 254;
  1676. new_div = 7; /* 128 == BIT(7) */
  1677. dev_warn(dev,
  1678. "fan%u low limit %lu below minimum %u, set to minimum\n",
  1679. nr + 1, val, data->fan_from_reg_min(254, 7));
  1680. } else if (!reg) {
  1681. /*
  1682. * Speed above this value cannot possibly be represented,
  1683. * even with the lowest divider (1)
  1684. */
  1685. data->fan_min[nr] = 1;
  1686. new_div = 0; /* 1 == BIT(0) */
  1687. dev_warn(dev,
  1688. "fan%u low limit %lu above maximum %u, set to maximum\n",
  1689. nr + 1, val, data->fan_from_reg_min(1, 0));
  1690. } else {
  1691. /*
  1692. * Automatically pick the best divider, i.e. the one such
  1693. * that the min limit will correspond to a register value
  1694. * in the 96..192 range
  1695. */
  1696. new_div = 0;
  1697. while (reg > 192 && new_div < 7) {
  1698. reg >>= 1;
  1699. new_div++;
  1700. }
  1701. data->fan_min[nr] = reg;
  1702. }
  1703. write_div:
  1704. /*
  1705. * Write both the fan clock divider (if it changed) and the new
  1706. * fan min (unconditionally)
  1707. */
  1708. if (new_div != data->fan_div[nr]) {
  1709. dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
  1710. nr + 1, div_from_reg(data->fan_div[nr]),
  1711. div_from_reg(new_div));
  1712. data->fan_div[nr] = new_div;
  1713. err = nct6775_write_fan_div_common(data, nr);
  1714. if (err)
  1715. goto write_min;
  1716. /* Give the chip time to sample a new speed value */
  1717. data->last_updated = jiffies;
  1718. }
  1719. write_min:
  1720. err = nct6775_write_value(data, data->REG_FAN_MIN[nr], data->fan_min[nr]);
  1721. mutex_unlock(&data->update_lock);
  1722. return err ? : count;
  1723. }
  1724. static ssize_t
  1725. show_fan_pulses(struct device *dev, struct device_attribute *attr, char *buf)
  1726. {
  1727. struct nct6775_data *data = nct6775_update_device(dev);
  1728. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1729. int p;
  1730. if (IS_ERR(data))
  1731. return PTR_ERR(data);
  1732. p = data->fan_pulses[sattr->index];
  1733. return sprintf(buf, "%d\n", p ? : 4);
  1734. }
  1735. static ssize_t
  1736. store_fan_pulses(struct device *dev, struct device_attribute *attr,
  1737. const char *buf, size_t count)
  1738. {
  1739. struct nct6775_data *data = dev_get_drvdata(dev);
  1740. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1741. int nr = sattr->index;
  1742. unsigned long val;
  1743. int err;
  1744. u16 reg;
  1745. err = kstrtoul(buf, 10, &val);
  1746. if (err < 0)
  1747. return err;
  1748. if (val > 4)
  1749. return -EINVAL;
  1750. mutex_lock(&data->update_lock);
  1751. data->fan_pulses[nr] = val & 3;
  1752. err = nct6775_read_value(data, data->REG_FAN_PULSES[nr], &reg);
  1753. if (err)
  1754. goto out;
  1755. reg &= ~(0x03 << data->FAN_PULSE_SHIFT[nr]);
  1756. reg |= (val & 3) << data->FAN_PULSE_SHIFT[nr];
  1757. err = nct6775_write_value(data, data->REG_FAN_PULSES[nr], reg);
  1758. out:
  1759. mutex_unlock(&data->update_lock);
  1760. return err ? : count;
  1761. }
  1762. static umode_t nct6775_fan_is_visible(struct kobject *kobj,
  1763. struct attribute *attr, int index)
  1764. {
  1765. struct device *dev = kobj_to_dev(kobj);
  1766. struct nct6775_data *data = dev_get_drvdata(dev);
  1767. int fan = index / 6; /* fan index */
  1768. int nr = index % 6; /* attribute index */
  1769. if (!(data->has_fan & BIT(fan)))
  1770. return 0;
  1771. if (nr == 1 && data->ALARM_BITS[FAN_ALARM_BASE + fan] == -1)
  1772. return 0;
  1773. if (nr == 2 && data->BEEP_BITS[FAN_ALARM_BASE + fan] == -1)
  1774. return 0;
  1775. if (nr == 3 && !data->REG_FAN_PULSES[fan])
  1776. return 0;
  1777. if (nr == 4 && !(data->has_fan_min & BIT(fan)))
  1778. return 0;
  1779. if (nr == 5 && data->kind != nct6775)
  1780. return 0;
  1781. return nct6775_attr_mode(data, attr);
  1782. }
  1783. SENSOR_TEMPLATE(fan_input, "fan%d_input", 0444, show_fan, NULL, 0);
  1784. SENSOR_TEMPLATE(fan_alarm, "fan%d_alarm", 0444, nct6775_show_alarm, NULL, FAN_ALARM_BASE);
  1785. SENSOR_TEMPLATE(fan_beep, "fan%d_beep", 0644, nct6775_show_beep,
  1786. nct6775_store_beep, FAN_ALARM_BASE);
  1787. SENSOR_TEMPLATE(fan_pulses, "fan%d_pulses", 0644, show_fan_pulses, store_fan_pulses, 0);
  1788. SENSOR_TEMPLATE(fan_min, "fan%d_min", 0644, show_fan_min, store_fan_min, 0);
  1789. SENSOR_TEMPLATE(fan_div, "fan%d_div", 0444, show_fan_div, NULL, 0);
  1790. /*
  1791. * nct6775_fan_is_visible uses the index into the following array
  1792. * to determine if attributes should be created or not.
  1793. * Any change in order or content must be matched.
  1794. */
  1795. static struct sensor_device_template *nct6775_attributes_fan_template[] = {
  1796. &sensor_dev_template_fan_input,
  1797. &sensor_dev_template_fan_alarm, /* 1 */
  1798. &sensor_dev_template_fan_beep, /* 2 */
  1799. &sensor_dev_template_fan_pulses,
  1800. &sensor_dev_template_fan_min, /* 4 */
  1801. &sensor_dev_template_fan_div, /* 5 */
  1802. NULL
  1803. };
  1804. static const struct sensor_template_group nct6775_fan_template_group = {
  1805. .templates = nct6775_attributes_fan_template,
  1806. .is_visible = nct6775_fan_is_visible,
  1807. .base = 1,
  1808. };
  1809. static ssize_t
  1810. show_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
  1811. {
  1812. struct nct6775_data *data = nct6775_update_device(dev);
  1813. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1814. int nr = sattr->index;
  1815. if (IS_ERR(data))
  1816. return PTR_ERR(data);
  1817. return sprintf(buf, "%s\n", data->temp_label[data->temp_src[nr]]);
  1818. }
  1819. static ssize_t
  1820. show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  1821. {
  1822. struct nct6775_data *data = nct6775_update_device(dev);
  1823. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1824. int nr = sattr->nr;
  1825. int index = sattr->index;
  1826. if (IS_ERR(data))
  1827. return PTR_ERR(data);
  1828. return sprintf(buf, "%d\n", LM75_TEMP_FROM_REG(data->temp[index][nr]));
  1829. }
  1830. static ssize_t
  1831. store_temp(struct device *dev, struct device_attribute *attr, const char *buf,
  1832. size_t count)
  1833. {
  1834. struct nct6775_data *data = dev_get_drvdata(dev);
  1835. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1836. int nr = sattr->nr;
  1837. int index = sattr->index;
  1838. int err;
  1839. long val;
  1840. err = kstrtol(buf, 10, &val);
  1841. if (err < 0)
  1842. return err;
  1843. mutex_lock(&data->update_lock);
  1844. data->temp[index][nr] = LM75_TEMP_TO_REG(val);
  1845. err = nct6775_write_temp(data, data->reg_temp[index][nr], data->temp[index][nr]);
  1846. mutex_unlock(&data->update_lock);
  1847. return err ? : count;
  1848. }
  1849. static ssize_t
  1850. show_temp_offset(struct device *dev, struct device_attribute *attr, char *buf)
  1851. {
  1852. struct nct6775_data *data = nct6775_update_device(dev);
  1853. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1854. if (IS_ERR(data))
  1855. return PTR_ERR(data);
  1856. return sprintf(buf, "%d\n", data->temp_offset[sattr->index] * 1000);
  1857. }
  1858. static ssize_t
  1859. store_temp_offset(struct device *dev, struct device_attribute *attr,
  1860. const char *buf, size_t count)
  1861. {
  1862. struct nct6775_data *data = dev_get_drvdata(dev);
  1863. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1864. int nr = sattr->index;
  1865. long val;
  1866. int err;
  1867. err = kstrtol(buf, 10, &val);
  1868. if (err < 0)
  1869. return err;
  1870. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -128, 127);
  1871. mutex_lock(&data->update_lock);
  1872. data->temp_offset[nr] = val;
  1873. err = nct6775_write_value(data, data->REG_TEMP_OFFSET[nr], val);
  1874. mutex_unlock(&data->update_lock);
  1875. return err ? : count;
  1876. }
  1877. static ssize_t
  1878. show_temp_type(struct device *dev, struct device_attribute *attr, char *buf)
  1879. {
  1880. struct nct6775_data *data = nct6775_update_device(dev);
  1881. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1882. int nr = sattr->index;
  1883. if (IS_ERR(data))
  1884. return PTR_ERR(data);
  1885. return sprintf(buf, "%d\n", (int)data->temp_type[nr]);
  1886. }
  1887. static ssize_t
  1888. store_temp_type(struct device *dev, struct device_attribute *attr,
  1889. const char *buf, size_t count)
  1890. {
  1891. struct nct6775_data *data = nct6775_update_device(dev);
  1892. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  1893. int nr = sattr->index;
  1894. unsigned long val;
  1895. int err;
  1896. u8 vbit, dbit;
  1897. u16 vbat, diode;
  1898. if (IS_ERR(data))
  1899. return PTR_ERR(data);
  1900. err = kstrtoul(buf, 10, &val);
  1901. if (err < 0)
  1902. return err;
  1903. if (val != 1 && val != 3 && val != 4)
  1904. return -EINVAL;
  1905. mutex_lock(&data->update_lock);
  1906. data->temp_type[nr] = val;
  1907. vbit = 0x02 << nr;
  1908. dbit = data->DIODE_MASK << nr;
  1909. err = nct6775_read_value(data, data->REG_VBAT, &vbat);
  1910. if (err)
  1911. goto out;
  1912. vbat &= ~vbit;
  1913. err = nct6775_read_value(data, data->REG_DIODE, &diode);
  1914. if (err)
  1915. goto out;
  1916. diode &= ~dbit;
  1917. switch (val) {
  1918. case 1: /* CPU diode (diode, current mode) */
  1919. vbat |= vbit;
  1920. diode |= dbit;
  1921. break;
  1922. case 3: /* diode, voltage mode */
  1923. vbat |= dbit;
  1924. break;
  1925. case 4: /* thermistor */
  1926. break;
  1927. }
  1928. err = nct6775_write_value(data, data->REG_VBAT, vbat);
  1929. if (err)
  1930. goto out;
  1931. err = nct6775_write_value(data, data->REG_DIODE, diode);
  1932. out:
  1933. mutex_unlock(&data->update_lock);
  1934. return err ? : count;
  1935. }
  1936. static umode_t nct6775_temp_is_visible(struct kobject *kobj,
  1937. struct attribute *attr, int index)
  1938. {
  1939. struct device *dev = kobj_to_dev(kobj);
  1940. struct nct6775_data *data = dev_get_drvdata(dev);
  1941. int temp = index / 10; /* temp index */
  1942. int nr = index % 10; /* attribute index */
  1943. if (!(data->have_temp & BIT(temp)))
  1944. return 0;
  1945. if (nr == 1 && !data->temp_label)
  1946. return 0;
  1947. if (nr == 2 && find_temp_source(data, temp, data->num_temp_alarms) < 0)
  1948. return 0; /* alarm */
  1949. if (nr == 3 && find_temp_source(data, temp, data->num_temp_beeps) < 0)
  1950. return 0; /* beep */
  1951. if (nr == 4 && !data->reg_temp[1][temp]) /* max */
  1952. return 0;
  1953. if (nr == 5 && !data->reg_temp[2][temp]) /* max_hyst */
  1954. return 0;
  1955. if (nr == 6 && !data->reg_temp[3][temp]) /* crit */
  1956. return 0;
  1957. if (nr == 7 && !data->reg_temp[4][temp]) /* lcrit */
  1958. return 0;
  1959. /* offset and type only apply to fixed sensors */
  1960. if (nr > 7 && !(data->have_temp_fixed & BIT(temp)))
  1961. return 0;
  1962. return nct6775_attr_mode(data, attr);
  1963. }
  1964. SENSOR_TEMPLATE_2(temp_input, "temp%d_input", 0444, show_temp, NULL, 0, 0);
  1965. SENSOR_TEMPLATE(temp_label, "temp%d_label", 0444, show_temp_label, NULL, 0);
  1966. SENSOR_TEMPLATE_2(temp_max, "temp%d_max", 0644, show_temp, store_temp, 0, 1);
  1967. SENSOR_TEMPLATE_2(temp_max_hyst, "temp%d_max_hyst", 0644, show_temp, store_temp, 0, 2);
  1968. SENSOR_TEMPLATE_2(temp_crit, "temp%d_crit", 0644, show_temp, store_temp, 0, 3);
  1969. SENSOR_TEMPLATE_2(temp_lcrit, "temp%d_lcrit", 0644, show_temp, store_temp, 0, 4);
  1970. SENSOR_TEMPLATE(temp_offset, "temp%d_offset", 0644, show_temp_offset, store_temp_offset, 0);
  1971. SENSOR_TEMPLATE(temp_type, "temp%d_type", 0644, show_temp_type, store_temp_type, 0);
  1972. SENSOR_TEMPLATE(temp_alarm, "temp%d_alarm", 0444, show_temp_alarm, NULL, 0);
  1973. SENSOR_TEMPLATE(temp_beep, "temp%d_beep", 0644, show_temp_beep, store_temp_beep, 0);
  1974. /*
  1975. * nct6775_temp_is_visible uses the index into the following array
  1976. * to determine if attributes should be created or not.
  1977. * Any change in order or content must be matched.
  1978. */
  1979. static struct sensor_device_template *nct6775_attributes_temp_template[] = {
  1980. &sensor_dev_template_temp_input,
  1981. &sensor_dev_template_temp_label,
  1982. &sensor_dev_template_temp_alarm, /* 2 */
  1983. &sensor_dev_template_temp_beep, /* 3 */
  1984. &sensor_dev_template_temp_max, /* 4 */
  1985. &sensor_dev_template_temp_max_hyst, /* 5 */
  1986. &sensor_dev_template_temp_crit, /* 6 */
  1987. &sensor_dev_template_temp_lcrit, /* 7 */
  1988. &sensor_dev_template_temp_offset, /* 8 */
  1989. &sensor_dev_template_temp_type, /* 9 */
  1990. NULL
  1991. };
  1992. static const struct sensor_template_group nct6775_temp_template_group = {
  1993. .templates = nct6775_attributes_temp_template,
  1994. .is_visible = nct6775_temp_is_visible,
  1995. .base = 1,
  1996. };
  1997. static ssize_t show_tsi_temp(struct device *dev, struct device_attribute *attr, char *buf)
  1998. {
  1999. struct nct6775_data *data = nct6775_update_device(dev);
  2000. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2001. if (IS_ERR(data))
  2002. return PTR_ERR(data);
  2003. return sysfs_emit(buf, "%u\n", tsi_temp_from_reg(data->tsi_temp[sattr->index]));
  2004. }
  2005. static ssize_t show_tsi_temp_label(struct device *dev, struct device_attribute *attr, char *buf)
  2006. {
  2007. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2008. return sysfs_emit(buf, "TSI%d_TEMP\n", sattr->index);
  2009. }
  2010. SENSOR_TEMPLATE(tsi_temp_input, "temp%d_input", 0444, show_tsi_temp, NULL, 0);
  2011. SENSOR_TEMPLATE(tsi_temp_label, "temp%d_label", 0444, show_tsi_temp_label, NULL, 0);
  2012. static umode_t nct6775_tsi_temp_is_visible(struct kobject *kobj, struct attribute *attr,
  2013. int index)
  2014. {
  2015. struct device *dev = kobj_to_dev(kobj);
  2016. struct nct6775_data *data = dev_get_drvdata(dev);
  2017. int temp = index / 2;
  2018. return (data->have_tsi_temp & BIT(temp)) ? nct6775_attr_mode(data, attr) : 0;
  2019. }
  2020. /*
  2021. * The index calculation in nct6775_tsi_temp_is_visible() must be kept in
  2022. * sync with the size of this array.
  2023. */
  2024. static struct sensor_device_template *nct6775_tsi_temp_template[] = {
  2025. &sensor_dev_template_tsi_temp_input,
  2026. &sensor_dev_template_tsi_temp_label,
  2027. NULL
  2028. };
  2029. static ssize_t
  2030. show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
  2031. {
  2032. struct nct6775_data *data = nct6775_update_device(dev);
  2033. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2034. if (IS_ERR(data))
  2035. return PTR_ERR(data);
  2036. return sprintf(buf, "%d\n", data->pwm_mode[sattr->index]);
  2037. }
  2038. static ssize_t
  2039. store_pwm_mode(struct device *dev, struct device_attribute *attr,
  2040. const char *buf, size_t count)
  2041. {
  2042. struct nct6775_data *data = dev_get_drvdata(dev);
  2043. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2044. int nr = sattr->index;
  2045. unsigned long val;
  2046. int err;
  2047. u16 reg;
  2048. err = kstrtoul(buf, 10, &val);
  2049. if (err < 0)
  2050. return err;
  2051. if (val > 1)
  2052. return -EINVAL;
  2053. /* Setting DC mode (0) is not supported for all chips/channels */
  2054. if (data->REG_PWM_MODE[nr] == 0) {
  2055. if (!val)
  2056. return -EINVAL;
  2057. return count;
  2058. }
  2059. mutex_lock(&data->update_lock);
  2060. data->pwm_mode[nr] = val;
  2061. err = nct6775_read_value(data, data->REG_PWM_MODE[nr], &reg);
  2062. if (err)
  2063. goto out;
  2064. reg &= ~data->PWM_MODE_MASK[nr];
  2065. if (!val)
  2066. reg |= data->PWM_MODE_MASK[nr];
  2067. err = nct6775_write_value(data, data->REG_PWM_MODE[nr], reg);
  2068. out:
  2069. mutex_unlock(&data->update_lock);
  2070. return err ? : count;
  2071. }
  2072. static ssize_t
  2073. show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  2074. {
  2075. struct nct6775_data *data = nct6775_update_device(dev);
  2076. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2077. int nr = sattr->nr;
  2078. int index = sattr->index;
  2079. int err;
  2080. u16 pwm;
  2081. if (IS_ERR(data))
  2082. return PTR_ERR(data);
  2083. /*
  2084. * For automatic fan control modes, show current pwm readings.
  2085. * Otherwise, show the configured value.
  2086. */
  2087. if (index == 0 && data->pwm_enable[nr] > manual) {
  2088. err = nct6775_read_value(data, data->REG_PWM_READ[nr], &pwm);
  2089. if (err)
  2090. return err;
  2091. } else {
  2092. pwm = data->pwm[index][nr];
  2093. }
  2094. return sprintf(buf, "%d\n", pwm);
  2095. }
  2096. static ssize_t
  2097. store_pwm(struct device *dev, struct device_attribute *attr, const char *buf,
  2098. size_t count)
  2099. {
  2100. struct nct6775_data *data = dev_get_drvdata(dev);
  2101. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2102. int nr = sattr->nr;
  2103. int index = sattr->index;
  2104. unsigned long val;
  2105. int minval[7] = { 0, 1, 1, data->pwm[2][nr], 0, 0, 0 };
  2106. int maxval[7]
  2107. = { 255, 255, data->pwm[3][nr] ? : 255, 255, 255, 255, 255 };
  2108. int err;
  2109. u16 reg;
  2110. err = kstrtoul(buf, 10, &val);
  2111. if (err < 0)
  2112. return err;
  2113. val = clamp_val(val, minval[index], maxval[index]);
  2114. mutex_lock(&data->update_lock);
  2115. data->pwm[index][nr] = val;
  2116. err = nct6775_write_value(data, data->REG_PWM[index][nr], val);
  2117. if (err)
  2118. goto out;
  2119. if (index == 2) { /* floor: disable if val == 0 */
  2120. err = nct6775_read_value(data, data->REG_TEMP_SEL[nr], &reg);
  2121. if (err)
  2122. goto out;
  2123. reg &= 0x7f;
  2124. if (val)
  2125. reg |= 0x80;
  2126. err = nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
  2127. }
  2128. out:
  2129. mutex_unlock(&data->update_lock);
  2130. return err ? : count;
  2131. }
  2132. /* Returns 0 if OK, -EINVAL otherwise */
  2133. static int check_trip_points(struct nct6775_data *data, int nr)
  2134. {
  2135. int i;
  2136. for (i = 0; i < data->auto_pwm_num - 1; i++) {
  2137. if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
  2138. return -EINVAL;
  2139. }
  2140. for (i = 0; i < data->auto_pwm_num - 1; i++) {
  2141. if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
  2142. return -EINVAL;
  2143. }
  2144. /* validate critical temperature and pwm if enabled (pwm > 0) */
  2145. if (data->auto_pwm[nr][data->auto_pwm_num]) {
  2146. if (data->auto_temp[nr][data->auto_pwm_num - 1] >
  2147. data->auto_temp[nr][data->auto_pwm_num] ||
  2148. data->auto_pwm[nr][data->auto_pwm_num - 1] >
  2149. data->auto_pwm[nr][data->auto_pwm_num])
  2150. return -EINVAL;
  2151. }
  2152. return 0;
  2153. }
  2154. static int pwm_update_registers(struct nct6775_data *data, int nr)
  2155. {
  2156. u16 reg;
  2157. int err;
  2158. switch (data->pwm_enable[nr]) {
  2159. case off:
  2160. case manual:
  2161. break;
  2162. case speed_cruise:
  2163. err = nct6775_read_value(data, data->REG_FAN_MODE[nr], &reg);
  2164. if (err)
  2165. return err;
  2166. reg = (reg & ~data->tolerance_mask) |
  2167. (data->target_speed_tolerance[nr] & data->tolerance_mask);
  2168. err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
  2169. if (err)
  2170. return err;
  2171. err = nct6775_write_value(data, data->REG_TARGET[nr],
  2172. data->target_speed[nr] & 0xff);
  2173. if (err)
  2174. return err;
  2175. if (data->REG_TOLERANCE_H) {
  2176. reg = (data->target_speed[nr] >> 8) & 0x0f;
  2177. reg |= (data->target_speed_tolerance[nr] & 0x38) << 1;
  2178. err = nct6775_write_value(data, data->REG_TOLERANCE_H[nr], reg);
  2179. if (err)
  2180. return err;
  2181. }
  2182. break;
  2183. case thermal_cruise:
  2184. err = nct6775_write_value(data, data->REG_TARGET[nr], data->target_temp[nr]);
  2185. if (err)
  2186. return err;
  2187. fallthrough;
  2188. default:
  2189. err = nct6775_read_value(data, data->REG_FAN_MODE[nr], &reg);
  2190. if (err)
  2191. return err;
  2192. reg = (reg & ~data->tolerance_mask) |
  2193. data->temp_tolerance[0][nr];
  2194. err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
  2195. if (err)
  2196. return err;
  2197. break;
  2198. }
  2199. return 0;
  2200. }
  2201. static ssize_t
  2202. show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
  2203. {
  2204. struct nct6775_data *data = nct6775_update_device(dev);
  2205. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2206. if (IS_ERR(data))
  2207. return PTR_ERR(data);
  2208. return sprintf(buf, "%d\n", data->pwm_enable[sattr->index]);
  2209. }
  2210. static ssize_t
  2211. store_pwm_enable(struct device *dev, struct device_attribute *attr,
  2212. const char *buf, size_t count)
  2213. {
  2214. struct nct6775_data *data = dev_get_drvdata(dev);
  2215. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2216. int nr = sattr->index;
  2217. unsigned long val;
  2218. int err;
  2219. u16 reg;
  2220. err = kstrtoul(buf, 10, &val);
  2221. if (err < 0)
  2222. return err;
  2223. if (val > sf4)
  2224. return -EINVAL;
  2225. if (val == sf3 && data->kind != nct6775)
  2226. return -EINVAL;
  2227. if (val == sf4 && check_trip_points(data, nr)) {
  2228. dev_err(dev, "Inconsistent trip points, not switching to SmartFan IV mode\n");
  2229. dev_err(dev, "Adjust trip points and try again\n");
  2230. return -EINVAL;
  2231. }
  2232. mutex_lock(&data->update_lock);
  2233. data->pwm_enable[nr] = val;
  2234. if (val == off) {
  2235. /*
  2236. * turn off pwm control: select manual mode, set pwm to maximum
  2237. */
  2238. data->pwm[0][nr] = 255;
  2239. err = nct6775_write_value(data, data->REG_PWM[0][nr], 255);
  2240. if (err)
  2241. goto out;
  2242. }
  2243. err = pwm_update_registers(data, nr);
  2244. if (err)
  2245. goto out;
  2246. err = nct6775_read_value(data, data->REG_FAN_MODE[nr], &reg);
  2247. if (err)
  2248. goto out;
  2249. reg &= 0x0f;
  2250. reg |= pwm_enable_to_reg(val) << 4;
  2251. err = nct6775_write_value(data, data->REG_FAN_MODE[nr], reg);
  2252. out:
  2253. mutex_unlock(&data->update_lock);
  2254. return err ? : count;
  2255. }
  2256. static ssize_t
  2257. show_pwm_temp_sel_common(struct nct6775_data *data, char *buf, int src)
  2258. {
  2259. int i, sel = 0;
  2260. for (i = 0; i < NUM_TEMP; i++) {
  2261. if (!(data->have_temp & BIT(i)))
  2262. continue;
  2263. if (src == data->temp_src[i]) {
  2264. sel = i + 1;
  2265. break;
  2266. }
  2267. }
  2268. return sprintf(buf, "%d\n", sel);
  2269. }
  2270. static ssize_t
  2271. show_pwm_temp_sel(struct device *dev, struct device_attribute *attr, char *buf)
  2272. {
  2273. struct nct6775_data *data = nct6775_update_device(dev);
  2274. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2275. int index = sattr->index;
  2276. if (IS_ERR(data))
  2277. return PTR_ERR(data);
  2278. return show_pwm_temp_sel_common(data, buf, data->pwm_temp_sel[index]);
  2279. }
  2280. static ssize_t
  2281. store_pwm_temp_sel(struct device *dev, struct device_attribute *attr,
  2282. const char *buf, size_t count)
  2283. {
  2284. struct nct6775_data *data = nct6775_update_device(dev);
  2285. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2286. int nr = sattr->index;
  2287. unsigned long val;
  2288. int err, src;
  2289. u16 reg;
  2290. if (IS_ERR(data))
  2291. return PTR_ERR(data);
  2292. err = kstrtoul(buf, 10, &val);
  2293. if (err < 0)
  2294. return err;
  2295. if (val == 0 || val > NUM_TEMP)
  2296. return -EINVAL;
  2297. if (!(data->have_temp & BIT(val - 1)) || !data->temp_src[val - 1])
  2298. return -EINVAL;
  2299. mutex_lock(&data->update_lock);
  2300. src = data->temp_src[val - 1];
  2301. data->pwm_temp_sel[nr] = src;
  2302. err = nct6775_read_value(data, data->REG_TEMP_SEL[nr], &reg);
  2303. if (err)
  2304. goto out;
  2305. reg &= 0xe0;
  2306. reg |= src;
  2307. err = nct6775_write_value(data, data->REG_TEMP_SEL[nr], reg);
  2308. out:
  2309. mutex_unlock(&data->update_lock);
  2310. return err ? : count;
  2311. }
  2312. static ssize_t
  2313. show_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
  2314. char *buf)
  2315. {
  2316. struct nct6775_data *data = nct6775_update_device(dev);
  2317. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2318. int index = sattr->index;
  2319. if (IS_ERR(data))
  2320. return PTR_ERR(data);
  2321. return show_pwm_temp_sel_common(data, buf,
  2322. data->pwm_weight_temp_sel[index]);
  2323. }
  2324. static ssize_t
  2325. store_pwm_weight_temp_sel(struct device *dev, struct device_attribute *attr,
  2326. const char *buf, size_t count)
  2327. {
  2328. struct nct6775_data *data = nct6775_update_device(dev);
  2329. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2330. int nr = sattr->index;
  2331. unsigned long val;
  2332. int err, src;
  2333. u16 reg;
  2334. if (IS_ERR(data))
  2335. return PTR_ERR(data);
  2336. err = kstrtoul(buf, 10, &val);
  2337. if (err < 0)
  2338. return err;
  2339. if (val > NUM_TEMP)
  2340. return -EINVAL;
  2341. val = array_index_nospec(val, NUM_TEMP + 1);
  2342. if (val && (!(data->have_temp & BIT(val - 1)) ||
  2343. !data->temp_src[val - 1]))
  2344. return -EINVAL;
  2345. mutex_lock(&data->update_lock);
  2346. if (val) {
  2347. src = data->temp_src[val - 1];
  2348. data->pwm_weight_temp_sel[nr] = src;
  2349. err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr], &reg);
  2350. if (err)
  2351. goto out;
  2352. reg &= 0xe0;
  2353. reg |= (src | 0x80);
  2354. err = nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
  2355. } else {
  2356. data->pwm_weight_temp_sel[nr] = 0;
  2357. err = nct6775_read_value(data, data->REG_WEIGHT_TEMP_SEL[nr], &reg);
  2358. if (err)
  2359. goto out;
  2360. reg &= 0x7f;
  2361. err = nct6775_write_value(data, data->REG_WEIGHT_TEMP_SEL[nr], reg);
  2362. }
  2363. out:
  2364. mutex_unlock(&data->update_lock);
  2365. return err ? : count;
  2366. }
  2367. static ssize_t
  2368. show_target_temp(struct device *dev, struct device_attribute *attr, char *buf)
  2369. {
  2370. struct nct6775_data *data = nct6775_update_device(dev);
  2371. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2372. if (IS_ERR(data))
  2373. return PTR_ERR(data);
  2374. return sprintf(buf, "%d\n", data->target_temp[sattr->index] * 1000);
  2375. }
  2376. static ssize_t
  2377. store_target_temp(struct device *dev, struct device_attribute *attr,
  2378. const char *buf, size_t count)
  2379. {
  2380. struct nct6775_data *data = dev_get_drvdata(dev);
  2381. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2382. int nr = sattr->index;
  2383. unsigned long val;
  2384. int err;
  2385. err = kstrtoul(buf, 10, &val);
  2386. if (err < 0)
  2387. return err;
  2388. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0,
  2389. data->target_temp_mask);
  2390. mutex_lock(&data->update_lock);
  2391. data->target_temp[nr] = val;
  2392. err = pwm_update_registers(data, nr);
  2393. mutex_unlock(&data->update_lock);
  2394. return err ? : count;
  2395. }
  2396. static ssize_t
  2397. show_target_speed(struct device *dev, struct device_attribute *attr, char *buf)
  2398. {
  2399. struct nct6775_data *data = nct6775_update_device(dev);
  2400. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2401. int nr = sattr->index;
  2402. if (IS_ERR(data))
  2403. return PTR_ERR(data);
  2404. return sprintf(buf, "%d\n",
  2405. fan_from_reg16(data->target_speed[nr],
  2406. data->fan_div[nr]));
  2407. }
  2408. static ssize_t
  2409. store_target_speed(struct device *dev, struct device_attribute *attr,
  2410. const char *buf, size_t count)
  2411. {
  2412. struct nct6775_data *data = dev_get_drvdata(dev);
  2413. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2414. int nr = sattr->index;
  2415. unsigned long val;
  2416. int err;
  2417. u16 speed;
  2418. err = kstrtoul(buf, 10, &val);
  2419. if (err < 0)
  2420. return err;
  2421. val = clamp_val(val, 0, 1350000U);
  2422. speed = fan_to_reg(val, data->fan_div[nr]);
  2423. mutex_lock(&data->update_lock);
  2424. data->target_speed[nr] = speed;
  2425. err = pwm_update_registers(data, nr);
  2426. mutex_unlock(&data->update_lock);
  2427. return err ? : count;
  2428. }
  2429. static ssize_t
  2430. show_temp_tolerance(struct device *dev, struct device_attribute *attr,
  2431. char *buf)
  2432. {
  2433. struct nct6775_data *data = nct6775_update_device(dev);
  2434. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2435. int nr = sattr->nr;
  2436. int index = sattr->index;
  2437. if (IS_ERR(data))
  2438. return PTR_ERR(data);
  2439. return sprintf(buf, "%d\n", data->temp_tolerance[index][nr] * 1000);
  2440. }
  2441. static ssize_t
  2442. store_temp_tolerance(struct device *dev, struct device_attribute *attr,
  2443. const char *buf, size_t count)
  2444. {
  2445. struct nct6775_data *data = dev_get_drvdata(dev);
  2446. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2447. int nr = sattr->nr;
  2448. int index = sattr->index;
  2449. unsigned long val;
  2450. int err;
  2451. err = kstrtoul(buf, 10, &val);
  2452. if (err < 0)
  2453. return err;
  2454. /* Limit tolerance as needed */
  2455. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, data->tolerance_mask);
  2456. mutex_lock(&data->update_lock);
  2457. data->temp_tolerance[index][nr] = val;
  2458. if (index)
  2459. err = pwm_update_registers(data, nr);
  2460. else
  2461. err = nct6775_write_value(data, data->REG_CRITICAL_TEMP_TOLERANCE[nr], val);
  2462. mutex_unlock(&data->update_lock);
  2463. return err ? : count;
  2464. }
  2465. /*
  2466. * Fan speed tolerance is a tricky beast, since the associated register is
  2467. * a tick counter, but the value is reported and configured as rpm.
  2468. * Compute resulting low and high rpm values and report the difference.
  2469. * A fan speed tolerance only makes sense if a fan target speed has been
  2470. * configured, so only display values other than 0 if that is the case.
  2471. */
  2472. static ssize_t
  2473. show_speed_tolerance(struct device *dev, struct device_attribute *attr,
  2474. char *buf)
  2475. {
  2476. struct nct6775_data *data = nct6775_update_device(dev);
  2477. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2478. int nr = sattr->index;
  2479. int target, tolerance = 0;
  2480. if (IS_ERR(data))
  2481. return PTR_ERR(data);
  2482. target = data->target_speed[nr];
  2483. if (target) {
  2484. int low = target - data->target_speed_tolerance[nr];
  2485. int high = target + data->target_speed_tolerance[nr];
  2486. if (low <= 0)
  2487. low = 1;
  2488. if (high > 0xffff)
  2489. high = 0xffff;
  2490. if (high < low)
  2491. high = low;
  2492. tolerance = (fan_from_reg16(low, data->fan_div[nr])
  2493. - fan_from_reg16(high, data->fan_div[nr])) / 2;
  2494. }
  2495. return sprintf(buf, "%d\n", tolerance);
  2496. }
  2497. static ssize_t
  2498. store_speed_tolerance(struct device *dev, struct device_attribute *attr,
  2499. const char *buf, size_t count)
  2500. {
  2501. struct nct6775_data *data = dev_get_drvdata(dev);
  2502. struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
  2503. int nr = sattr->index;
  2504. unsigned long val;
  2505. int err;
  2506. int low, high;
  2507. err = kstrtoul(buf, 10, &val);
  2508. if (err < 0)
  2509. return err;
  2510. high = fan_from_reg16(data->target_speed[nr], data->fan_div[nr]) + val;
  2511. low = fan_from_reg16(data->target_speed[nr], data->fan_div[nr]) - val;
  2512. if (low <= 0)
  2513. low = 1;
  2514. if (high < low)
  2515. high = low;
  2516. val = (fan_to_reg(low, data->fan_div[nr]) -
  2517. fan_to_reg(high, data->fan_div[nr])) / 2;
  2518. /* Limit tolerance as needed */
  2519. val = clamp_val(val, 0, data->speed_tolerance_limit);
  2520. mutex_lock(&data->update_lock);
  2521. data->target_speed_tolerance[nr] = val;
  2522. err = pwm_update_registers(data, nr);
  2523. mutex_unlock(&data->update_lock);
  2524. return err ? : count;
  2525. }
  2526. SENSOR_TEMPLATE_2(pwm, "pwm%d", 0644, show_pwm, store_pwm, 0, 0);
  2527. SENSOR_TEMPLATE(pwm_mode, "pwm%d_mode", 0644, show_pwm_mode, store_pwm_mode, 0);
  2528. SENSOR_TEMPLATE(pwm_enable, "pwm%d_enable", 0644, show_pwm_enable, store_pwm_enable, 0);
  2529. SENSOR_TEMPLATE(pwm_temp_sel, "pwm%d_temp_sel", 0644, show_pwm_temp_sel, store_pwm_temp_sel, 0);
  2530. SENSOR_TEMPLATE(pwm_target_temp, "pwm%d_target_temp", 0644, show_target_temp, store_target_temp, 0);
  2531. SENSOR_TEMPLATE(fan_target, "fan%d_target", 0644, show_target_speed, store_target_speed, 0);
  2532. SENSOR_TEMPLATE(fan_tolerance, "fan%d_tolerance", 0644, show_speed_tolerance,
  2533. store_speed_tolerance, 0);
  2534. /* Smart Fan registers */
  2535. static ssize_t
  2536. show_weight_temp(struct device *dev, struct device_attribute *attr, char *buf)
  2537. {
  2538. struct nct6775_data *data = nct6775_update_device(dev);
  2539. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2540. int nr = sattr->nr;
  2541. int index = sattr->index;
  2542. if (IS_ERR(data))
  2543. return PTR_ERR(data);
  2544. return sprintf(buf, "%d\n", data->weight_temp[index][nr] * 1000);
  2545. }
  2546. static ssize_t
  2547. store_weight_temp(struct device *dev, struct device_attribute *attr,
  2548. const char *buf, size_t count)
  2549. {
  2550. struct nct6775_data *data = dev_get_drvdata(dev);
  2551. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2552. int nr = sattr->nr;
  2553. int index = sattr->index;
  2554. unsigned long val;
  2555. int err;
  2556. err = kstrtoul(buf, 10, &val);
  2557. if (err < 0)
  2558. return err;
  2559. val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), 0, 255);
  2560. mutex_lock(&data->update_lock);
  2561. data->weight_temp[index][nr] = val;
  2562. err = nct6775_write_value(data, data->REG_WEIGHT_TEMP[index][nr], val);
  2563. mutex_unlock(&data->update_lock);
  2564. return err ? : count;
  2565. }
  2566. SENSOR_TEMPLATE(pwm_weight_temp_sel, "pwm%d_weight_temp_sel", 0644,
  2567. show_pwm_weight_temp_sel, store_pwm_weight_temp_sel, 0);
  2568. SENSOR_TEMPLATE_2(pwm_weight_temp_step, "pwm%d_weight_temp_step",
  2569. 0644, show_weight_temp, store_weight_temp, 0, 0);
  2570. SENSOR_TEMPLATE_2(pwm_weight_temp_step_tol, "pwm%d_weight_temp_step_tol",
  2571. 0644, show_weight_temp, store_weight_temp, 0, 1);
  2572. SENSOR_TEMPLATE_2(pwm_weight_temp_step_base, "pwm%d_weight_temp_step_base",
  2573. 0644, show_weight_temp, store_weight_temp, 0, 2);
  2574. SENSOR_TEMPLATE_2(pwm_weight_duty_step, "pwm%d_weight_duty_step", 0644, show_pwm, store_pwm, 0, 5);
  2575. SENSOR_TEMPLATE_2(pwm_weight_duty_base, "pwm%d_weight_duty_base", 0644, show_pwm, store_pwm, 0, 6);
  2576. static ssize_t
  2577. show_fan_time(struct device *dev, struct device_attribute *attr, char *buf)
  2578. {
  2579. struct nct6775_data *data = nct6775_update_device(dev);
  2580. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2581. int nr = sattr->nr;
  2582. int index = sattr->index;
  2583. if (IS_ERR(data))
  2584. return PTR_ERR(data);
  2585. return sprintf(buf, "%d\n",
  2586. step_time_from_reg(data->fan_time[index][nr],
  2587. data->pwm_mode[nr]));
  2588. }
  2589. static ssize_t
  2590. store_fan_time(struct device *dev, struct device_attribute *attr,
  2591. const char *buf, size_t count)
  2592. {
  2593. struct nct6775_data *data = dev_get_drvdata(dev);
  2594. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2595. int nr = sattr->nr;
  2596. int index = sattr->index;
  2597. unsigned long val;
  2598. int err;
  2599. err = kstrtoul(buf, 10, &val);
  2600. if (err < 0)
  2601. return err;
  2602. val = step_time_to_reg(val, data->pwm_mode[nr]);
  2603. mutex_lock(&data->update_lock);
  2604. data->fan_time[index][nr] = val;
  2605. err = nct6775_write_value(data, data->REG_FAN_TIME[index][nr], val);
  2606. mutex_unlock(&data->update_lock);
  2607. return err ? : count;
  2608. }
  2609. static ssize_t
  2610. show_auto_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  2611. {
  2612. struct nct6775_data *data = nct6775_update_device(dev);
  2613. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2614. if (IS_ERR(data))
  2615. return PTR_ERR(data);
  2616. return sprintf(buf, "%d\n", data->auto_pwm[sattr->nr][sattr->index]);
  2617. }
  2618. static ssize_t
  2619. store_auto_pwm(struct device *dev, struct device_attribute *attr,
  2620. const char *buf, size_t count)
  2621. {
  2622. struct nct6775_data *data = dev_get_drvdata(dev);
  2623. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2624. int nr = sattr->nr;
  2625. int point = sattr->index;
  2626. unsigned long val;
  2627. int err;
  2628. u16 reg;
  2629. err = kstrtoul(buf, 10, &val);
  2630. if (err < 0)
  2631. return err;
  2632. if (val > 255)
  2633. return -EINVAL;
  2634. if (point == data->auto_pwm_num) {
  2635. if (data->kind != nct6775 && !val)
  2636. return -EINVAL;
  2637. if (data->kind != nct6779 && val)
  2638. val = 0xff;
  2639. }
  2640. mutex_lock(&data->update_lock);
  2641. data->auto_pwm[nr][point] = val;
  2642. if (point < data->auto_pwm_num) {
  2643. err = nct6775_write_value(data, NCT6775_AUTO_PWM(data, nr, point),
  2644. data->auto_pwm[nr][point]);
  2645. } else {
  2646. switch (data->kind) {
  2647. case nct6775:
  2648. /* disable if needed (pwm == 0) */
  2649. err = nct6775_read_value(data, NCT6775_REG_CRITICAL_ENAB[nr], &reg);
  2650. if (err)
  2651. break;
  2652. if (val)
  2653. reg |= 0x02;
  2654. else
  2655. reg &= ~0x02;
  2656. err = nct6775_write_value(data, NCT6775_REG_CRITICAL_ENAB[nr], reg);
  2657. break;
  2658. case nct6776:
  2659. break; /* always enabled, nothing to do */
  2660. case nct6106:
  2661. case nct6116:
  2662. case nct6779:
  2663. case nct6791:
  2664. case nct6792:
  2665. case nct6793:
  2666. case nct6795:
  2667. case nct6796:
  2668. case nct6797:
  2669. case nct6798:
  2670. err = nct6775_write_value(data, data->REG_CRITICAL_PWM[nr], val);
  2671. if (err)
  2672. break;
  2673. err = nct6775_read_value(data, data->REG_CRITICAL_PWM_ENABLE[nr], &reg);
  2674. if (err)
  2675. break;
  2676. if (val == 255)
  2677. reg &= ~data->CRITICAL_PWM_ENABLE_MASK;
  2678. else
  2679. reg |= data->CRITICAL_PWM_ENABLE_MASK;
  2680. err = nct6775_write_value(data, data->REG_CRITICAL_PWM_ENABLE[nr], reg);
  2681. break;
  2682. }
  2683. }
  2684. mutex_unlock(&data->update_lock);
  2685. return err ? : count;
  2686. }
  2687. static ssize_t
  2688. show_auto_temp(struct device *dev, struct device_attribute *attr, char *buf)
  2689. {
  2690. struct nct6775_data *data = nct6775_update_device(dev);
  2691. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2692. int nr = sattr->nr;
  2693. int point = sattr->index;
  2694. if (IS_ERR(data))
  2695. return PTR_ERR(data);
  2696. /*
  2697. * We don't know for sure if the temperature is signed or unsigned.
  2698. * Assume it is unsigned.
  2699. */
  2700. return sprintf(buf, "%d\n", data->auto_temp[nr][point] * 1000);
  2701. }
  2702. static ssize_t
  2703. store_auto_temp(struct device *dev, struct device_attribute *attr,
  2704. const char *buf, size_t count)
  2705. {
  2706. struct nct6775_data *data = dev_get_drvdata(dev);
  2707. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  2708. int nr = sattr->nr;
  2709. int point = sattr->index;
  2710. unsigned long val;
  2711. int err;
  2712. err = kstrtoul(buf, 10, &val);
  2713. if (err)
  2714. return err;
  2715. if (val > 255000)
  2716. return -EINVAL;
  2717. mutex_lock(&data->update_lock);
  2718. data->auto_temp[nr][point] = DIV_ROUND_CLOSEST(val, 1000);
  2719. if (point < data->auto_pwm_num) {
  2720. err = nct6775_write_value(data, NCT6775_AUTO_TEMP(data, nr, point),
  2721. data->auto_temp[nr][point]);
  2722. } else {
  2723. err = nct6775_write_value(data, data->REG_CRITICAL_TEMP[nr],
  2724. data->auto_temp[nr][point]);
  2725. }
  2726. mutex_unlock(&data->update_lock);
  2727. return err ? : count;
  2728. }
  2729. static umode_t nct6775_pwm_is_visible(struct kobject *kobj,
  2730. struct attribute *attr, int index)
  2731. {
  2732. struct device *dev = kobj_to_dev(kobj);
  2733. struct nct6775_data *data = dev_get_drvdata(dev);
  2734. int pwm = index / 36; /* pwm index */
  2735. int nr = index % 36; /* attribute index */
  2736. if (!(data->has_pwm & BIT(pwm)))
  2737. return 0;
  2738. if ((nr >= 14 && nr <= 18) || nr == 21) /* weight */
  2739. if (!data->REG_WEIGHT_TEMP_SEL[pwm])
  2740. return 0;
  2741. if (nr == 19 && data->REG_PWM[3] == NULL) /* pwm_max */
  2742. return 0;
  2743. if (nr == 20 && data->REG_PWM[4] == NULL) /* pwm_step */
  2744. return 0;
  2745. if (nr == 21 && data->REG_PWM[6] == NULL) /* weight_duty_base */
  2746. return 0;
  2747. if (nr >= 22 && nr <= 35) { /* auto point */
  2748. int api = (nr - 22) / 2; /* auto point index */
  2749. if (api > data->auto_pwm_num)
  2750. return 0;
  2751. }
  2752. return nct6775_attr_mode(data, attr);
  2753. }
  2754. SENSOR_TEMPLATE_2(pwm_stop_time, "pwm%d_stop_time", 0644, show_fan_time, store_fan_time, 0, 0);
  2755. SENSOR_TEMPLATE_2(pwm_step_up_time, "pwm%d_step_up_time", 0644,
  2756. show_fan_time, store_fan_time, 0, 1);
  2757. SENSOR_TEMPLATE_2(pwm_step_down_time, "pwm%d_step_down_time", 0644,
  2758. show_fan_time, store_fan_time, 0, 2);
  2759. SENSOR_TEMPLATE_2(pwm_start, "pwm%d_start", 0644, show_pwm, store_pwm, 0, 1);
  2760. SENSOR_TEMPLATE_2(pwm_floor, "pwm%d_floor", 0644, show_pwm, store_pwm, 0, 2);
  2761. SENSOR_TEMPLATE_2(pwm_temp_tolerance, "pwm%d_temp_tolerance", 0644,
  2762. show_temp_tolerance, store_temp_tolerance, 0, 0);
  2763. SENSOR_TEMPLATE_2(pwm_crit_temp_tolerance, "pwm%d_crit_temp_tolerance",
  2764. 0644, show_temp_tolerance, store_temp_tolerance, 0, 1);
  2765. SENSOR_TEMPLATE_2(pwm_max, "pwm%d_max", 0644, show_pwm, store_pwm, 0, 3);
  2766. SENSOR_TEMPLATE_2(pwm_step, "pwm%d_step", 0644, show_pwm, store_pwm, 0, 4);
  2767. SENSOR_TEMPLATE_2(pwm_auto_point1_pwm, "pwm%d_auto_point1_pwm",
  2768. 0644, show_auto_pwm, store_auto_pwm, 0, 0);
  2769. SENSOR_TEMPLATE_2(pwm_auto_point1_temp, "pwm%d_auto_point1_temp",
  2770. 0644, show_auto_temp, store_auto_temp, 0, 0);
  2771. SENSOR_TEMPLATE_2(pwm_auto_point2_pwm, "pwm%d_auto_point2_pwm",
  2772. 0644, show_auto_pwm, store_auto_pwm, 0, 1);
  2773. SENSOR_TEMPLATE_2(pwm_auto_point2_temp, "pwm%d_auto_point2_temp",
  2774. 0644, show_auto_temp, store_auto_temp, 0, 1);
  2775. SENSOR_TEMPLATE_2(pwm_auto_point3_pwm, "pwm%d_auto_point3_pwm",
  2776. 0644, show_auto_pwm, store_auto_pwm, 0, 2);
  2777. SENSOR_TEMPLATE_2(pwm_auto_point3_temp, "pwm%d_auto_point3_temp",
  2778. 0644, show_auto_temp, store_auto_temp, 0, 2);
  2779. SENSOR_TEMPLATE_2(pwm_auto_point4_pwm, "pwm%d_auto_point4_pwm",
  2780. 0644, show_auto_pwm, store_auto_pwm, 0, 3);
  2781. SENSOR_TEMPLATE_2(pwm_auto_point4_temp, "pwm%d_auto_point4_temp",
  2782. 0644, show_auto_temp, store_auto_temp, 0, 3);
  2783. SENSOR_TEMPLATE_2(pwm_auto_point5_pwm, "pwm%d_auto_point5_pwm",
  2784. 0644, show_auto_pwm, store_auto_pwm, 0, 4);
  2785. SENSOR_TEMPLATE_2(pwm_auto_point5_temp, "pwm%d_auto_point5_temp",
  2786. 0644, show_auto_temp, store_auto_temp, 0, 4);
  2787. SENSOR_TEMPLATE_2(pwm_auto_point6_pwm, "pwm%d_auto_point6_pwm",
  2788. 0644, show_auto_pwm, store_auto_pwm, 0, 5);
  2789. SENSOR_TEMPLATE_2(pwm_auto_point6_temp, "pwm%d_auto_point6_temp",
  2790. 0644, show_auto_temp, store_auto_temp, 0, 5);
  2791. SENSOR_TEMPLATE_2(pwm_auto_point7_pwm, "pwm%d_auto_point7_pwm",
  2792. 0644, show_auto_pwm, store_auto_pwm, 0, 6);
  2793. SENSOR_TEMPLATE_2(pwm_auto_point7_temp, "pwm%d_auto_point7_temp",
  2794. 0644, show_auto_temp, store_auto_temp, 0, 6);
  2795. /*
  2796. * nct6775_pwm_is_visible uses the index into the following array
  2797. * to determine if attributes should be created or not.
  2798. * Any change in order or content must be matched.
  2799. */
  2800. static struct sensor_device_template *nct6775_attributes_pwm_template[] = {
  2801. &sensor_dev_template_pwm,
  2802. &sensor_dev_template_pwm_mode,
  2803. &sensor_dev_template_pwm_enable,
  2804. &sensor_dev_template_pwm_temp_sel,
  2805. &sensor_dev_template_pwm_temp_tolerance,
  2806. &sensor_dev_template_pwm_crit_temp_tolerance,
  2807. &sensor_dev_template_pwm_target_temp,
  2808. &sensor_dev_template_fan_target,
  2809. &sensor_dev_template_fan_tolerance,
  2810. &sensor_dev_template_pwm_stop_time,
  2811. &sensor_dev_template_pwm_step_up_time,
  2812. &sensor_dev_template_pwm_step_down_time,
  2813. &sensor_dev_template_pwm_start,
  2814. &sensor_dev_template_pwm_floor,
  2815. &sensor_dev_template_pwm_weight_temp_sel, /* 14 */
  2816. &sensor_dev_template_pwm_weight_temp_step,
  2817. &sensor_dev_template_pwm_weight_temp_step_tol,
  2818. &sensor_dev_template_pwm_weight_temp_step_base,
  2819. &sensor_dev_template_pwm_weight_duty_step, /* 18 */
  2820. &sensor_dev_template_pwm_max, /* 19 */
  2821. &sensor_dev_template_pwm_step, /* 20 */
  2822. &sensor_dev_template_pwm_weight_duty_base, /* 21 */
  2823. &sensor_dev_template_pwm_auto_point1_pwm, /* 22 */
  2824. &sensor_dev_template_pwm_auto_point1_temp,
  2825. &sensor_dev_template_pwm_auto_point2_pwm,
  2826. &sensor_dev_template_pwm_auto_point2_temp,
  2827. &sensor_dev_template_pwm_auto_point3_pwm,
  2828. &sensor_dev_template_pwm_auto_point3_temp,
  2829. &sensor_dev_template_pwm_auto_point4_pwm,
  2830. &sensor_dev_template_pwm_auto_point4_temp,
  2831. &sensor_dev_template_pwm_auto_point5_pwm,
  2832. &sensor_dev_template_pwm_auto_point5_temp,
  2833. &sensor_dev_template_pwm_auto_point6_pwm,
  2834. &sensor_dev_template_pwm_auto_point6_temp,
  2835. &sensor_dev_template_pwm_auto_point7_pwm,
  2836. &sensor_dev_template_pwm_auto_point7_temp, /* 35 */
  2837. NULL
  2838. };
  2839. static const struct sensor_template_group nct6775_pwm_template_group = {
  2840. .templates = nct6775_attributes_pwm_template,
  2841. .is_visible = nct6775_pwm_is_visible,
  2842. .base = 1,
  2843. };
  2844. static inline int nct6775_init_device(struct nct6775_data *data)
  2845. {
  2846. int i, err;
  2847. u16 tmp, diode;
  2848. /* Start monitoring if needed */
  2849. if (data->REG_CONFIG) {
  2850. err = nct6775_read_value(data, data->REG_CONFIG, &tmp);
  2851. if (err)
  2852. return err;
  2853. if (!(tmp & 0x01)) {
  2854. err = nct6775_write_value(data, data->REG_CONFIG, tmp | 0x01);
  2855. if (err)
  2856. return err;
  2857. }
  2858. }
  2859. /* Enable temperature sensors if needed */
  2860. for (i = 0; i < NUM_TEMP; i++) {
  2861. if (!(data->have_temp & BIT(i)))
  2862. continue;
  2863. if (!data->reg_temp_config[i])
  2864. continue;
  2865. err = nct6775_read_value(data, data->reg_temp_config[i], &tmp);
  2866. if (err)
  2867. return err;
  2868. if (tmp & 0x01) {
  2869. err = nct6775_write_value(data, data->reg_temp_config[i], tmp & 0xfe);
  2870. if (err)
  2871. return err;
  2872. }
  2873. }
  2874. /* Enable VBAT monitoring if needed */
  2875. err = nct6775_read_value(data, data->REG_VBAT, &tmp);
  2876. if (err)
  2877. return err;
  2878. if (!(tmp & 0x01)) {
  2879. err = nct6775_write_value(data, data->REG_VBAT, tmp | 0x01);
  2880. if (err)
  2881. return err;
  2882. }
  2883. err = nct6775_read_value(data, data->REG_DIODE, &diode);
  2884. if (err)
  2885. return err;
  2886. for (i = 0; i < data->temp_fixed_num; i++) {
  2887. if (!(data->have_temp_fixed & BIT(i)))
  2888. continue;
  2889. if ((tmp & (data->DIODE_MASK << i))) /* diode */
  2890. data->temp_type[i]
  2891. = 3 - ((diode >> i) & data->DIODE_MASK);
  2892. else /* thermistor */
  2893. data->temp_type[i] = 4;
  2894. }
  2895. return 0;
  2896. }
  2897. static int add_temp_sensors(struct nct6775_data *data, const u16 *regp,
  2898. int *available, int *mask)
  2899. {
  2900. int i, err;
  2901. u16 src;
  2902. for (i = 0; i < data->pwm_num && *available; i++) {
  2903. int index;
  2904. if (!regp[i])
  2905. continue;
  2906. err = nct6775_read_value(data, regp[i], &src);
  2907. if (err)
  2908. return err;
  2909. src &= 0x1f;
  2910. if (!src || (*mask & BIT(src)))
  2911. continue;
  2912. if (!(data->temp_mask & BIT(src)))
  2913. continue;
  2914. index = __ffs(*available);
  2915. err = nct6775_write_value(data, data->REG_TEMP_SOURCE[index], src);
  2916. if (err)
  2917. return err;
  2918. *available &= ~BIT(index);
  2919. *mask |= BIT(src);
  2920. }
  2921. return 0;
  2922. }
  2923. int nct6775_probe(struct device *dev, struct nct6775_data *data,
  2924. const struct regmap_config *regmapcfg)
  2925. {
  2926. int i, s, err = 0;
  2927. int mask, available;
  2928. u16 src;
  2929. const u16 *reg_temp, *reg_temp_over, *reg_temp_hyst, *reg_temp_config;
  2930. const u16 *reg_temp_mon, *reg_temp_alternate, *reg_temp_crit;
  2931. const u16 *reg_temp_crit_l = NULL, *reg_temp_crit_h = NULL;
  2932. int num_reg_temp, num_reg_temp_mon, num_reg_tsi_temp;
  2933. struct device *hwmon_dev;
  2934. struct sensor_template_group tsi_temp_tg;
  2935. data->regmap = devm_regmap_init(dev, NULL, data, regmapcfg);
  2936. if (IS_ERR(data->regmap))
  2937. return PTR_ERR(data->regmap);
  2938. mutex_init(&data->update_lock);
  2939. data->name = nct6775_device_names[data->kind];
  2940. data->bank = 0xff; /* Force initial bank selection */
  2941. switch (data->kind) {
  2942. case nct6106:
  2943. data->in_num = 9;
  2944. data->pwm_num = 3;
  2945. data->auto_pwm_num = 4;
  2946. data->temp_fixed_num = 3;
  2947. data->num_temp_alarms = 6;
  2948. data->num_temp_beeps = 6;
  2949. data->fan_from_reg = fan_from_reg13;
  2950. data->fan_from_reg_min = fan_from_reg13;
  2951. data->temp_label = nct6776_temp_label;
  2952. data->temp_mask = NCT6776_TEMP_MASK;
  2953. data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
  2954. data->REG_VBAT = NCT6106_REG_VBAT;
  2955. data->REG_DIODE = NCT6106_REG_DIODE;
  2956. data->DIODE_MASK = NCT6106_DIODE_MASK;
  2957. data->REG_VIN = NCT6106_REG_IN;
  2958. data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
  2959. data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
  2960. data->REG_TARGET = NCT6106_REG_TARGET;
  2961. data->REG_FAN = NCT6106_REG_FAN;
  2962. data->REG_FAN_MODE = NCT6106_REG_FAN_MODE;
  2963. data->REG_FAN_MIN = NCT6106_REG_FAN_MIN;
  2964. data->REG_FAN_PULSES = NCT6106_REG_FAN_PULSES;
  2965. data->FAN_PULSE_SHIFT = NCT6106_FAN_PULSE_SHIFT;
  2966. data->REG_FAN_TIME[0] = NCT6106_REG_FAN_STOP_TIME;
  2967. data->REG_FAN_TIME[1] = NCT6106_REG_FAN_STEP_UP_TIME;
  2968. data->REG_FAN_TIME[2] = NCT6106_REG_FAN_STEP_DOWN_TIME;
  2969. data->REG_TOLERANCE_H = NCT6106_REG_TOLERANCE_H;
  2970. data->REG_PWM[0] = NCT6116_REG_PWM;
  2971. data->REG_PWM[1] = NCT6106_REG_FAN_START_OUTPUT;
  2972. data->REG_PWM[2] = NCT6106_REG_FAN_STOP_OUTPUT;
  2973. data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
  2974. data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
  2975. data->REG_PWM_READ = NCT6106_REG_PWM_READ;
  2976. data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
  2977. data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
  2978. data->REG_AUTO_TEMP = NCT6106_REG_AUTO_TEMP;
  2979. data->REG_AUTO_PWM = NCT6106_REG_AUTO_PWM;
  2980. data->REG_CRITICAL_TEMP = NCT6106_REG_CRITICAL_TEMP;
  2981. data->REG_CRITICAL_TEMP_TOLERANCE
  2982. = NCT6106_REG_CRITICAL_TEMP_TOLERANCE;
  2983. data->REG_CRITICAL_PWM_ENABLE = NCT6106_REG_CRITICAL_PWM_ENABLE;
  2984. data->CRITICAL_PWM_ENABLE_MASK
  2985. = NCT6106_CRITICAL_PWM_ENABLE_MASK;
  2986. data->REG_CRITICAL_PWM = NCT6106_REG_CRITICAL_PWM;
  2987. data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
  2988. data->REG_TEMP_SOURCE = NCT6106_REG_TEMP_SOURCE;
  2989. data->REG_TEMP_SEL = NCT6116_REG_TEMP_SEL;
  2990. data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
  2991. data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
  2992. data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
  2993. data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
  2994. data->REG_ALARM = NCT6106_REG_ALARM;
  2995. data->ALARM_BITS = NCT6106_ALARM_BITS;
  2996. data->REG_BEEP = NCT6106_REG_BEEP;
  2997. data->BEEP_BITS = NCT6106_BEEP_BITS;
  2998. data->REG_TSI_TEMP = NCT6106_REG_TSI_TEMP;
  2999. reg_temp = NCT6106_REG_TEMP;
  3000. reg_temp_mon = NCT6106_REG_TEMP_MON;
  3001. num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
  3002. num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
  3003. num_reg_tsi_temp = ARRAY_SIZE(NCT6106_REG_TSI_TEMP);
  3004. reg_temp_over = NCT6106_REG_TEMP_OVER;
  3005. reg_temp_hyst = NCT6106_REG_TEMP_HYST;
  3006. reg_temp_config = NCT6106_REG_TEMP_CONFIG;
  3007. reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
  3008. reg_temp_crit = NCT6106_REG_TEMP_CRIT;
  3009. reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
  3010. reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
  3011. break;
  3012. case nct6116:
  3013. data->in_num = 9;
  3014. data->pwm_num = 3;
  3015. data->auto_pwm_num = 4;
  3016. data->temp_fixed_num = 3;
  3017. data->num_temp_alarms = 3;
  3018. data->num_temp_beeps = 3;
  3019. data->fan_from_reg = fan_from_reg13;
  3020. data->fan_from_reg_min = fan_from_reg13;
  3021. data->temp_label = nct6776_temp_label;
  3022. data->temp_mask = NCT6776_TEMP_MASK;
  3023. data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
  3024. data->REG_VBAT = NCT6106_REG_VBAT;
  3025. data->REG_DIODE = NCT6106_REG_DIODE;
  3026. data->DIODE_MASK = NCT6106_DIODE_MASK;
  3027. data->REG_VIN = NCT6106_REG_IN;
  3028. data->REG_IN_MINMAX[0] = NCT6106_REG_IN_MIN;
  3029. data->REG_IN_MINMAX[1] = NCT6106_REG_IN_MAX;
  3030. data->REG_TARGET = NCT6116_REG_TARGET;
  3031. data->REG_FAN = NCT6116_REG_FAN;
  3032. data->REG_FAN_MODE = NCT6116_REG_FAN_MODE;
  3033. data->REG_FAN_MIN = NCT6116_REG_FAN_MIN;
  3034. data->REG_FAN_PULSES = NCT6116_REG_FAN_PULSES;
  3035. data->FAN_PULSE_SHIFT = NCT6116_FAN_PULSE_SHIFT;
  3036. data->REG_FAN_TIME[0] = NCT6116_REG_FAN_STOP_TIME;
  3037. data->REG_FAN_TIME[1] = NCT6116_REG_FAN_STEP_UP_TIME;
  3038. data->REG_FAN_TIME[2] = NCT6116_REG_FAN_STEP_DOWN_TIME;
  3039. data->REG_TOLERANCE_H = NCT6116_REG_TOLERANCE_H;
  3040. data->REG_PWM[0] = NCT6116_REG_PWM;
  3041. data->REG_PWM[1] = NCT6116_REG_FAN_START_OUTPUT;
  3042. data->REG_PWM[2] = NCT6116_REG_FAN_STOP_OUTPUT;
  3043. data->REG_PWM[5] = NCT6106_REG_WEIGHT_DUTY_STEP;
  3044. data->REG_PWM[6] = NCT6106_REG_WEIGHT_DUTY_BASE;
  3045. data->REG_PWM_READ = NCT6106_REG_PWM_READ;
  3046. data->REG_PWM_MODE = NCT6106_REG_PWM_MODE;
  3047. data->PWM_MODE_MASK = NCT6106_PWM_MODE_MASK;
  3048. data->REG_AUTO_TEMP = NCT6116_REG_AUTO_TEMP;
  3049. data->REG_AUTO_PWM = NCT6116_REG_AUTO_PWM;
  3050. data->REG_CRITICAL_TEMP = NCT6116_REG_CRITICAL_TEMP;
  3051. data->REG_CRITICAL_TEMP_TOLERANCE
  3052. = NCT6116_REG_CRITICAL_TEMP_TOLERANCE;
  3053. data->REG_CRITICAL_PWM_ENABLE = NCT6116_REG_CRITICAL_PWM_ENABLE;
  3054. data->CRITICAL_PWM_ENABLE_MASK
  3055. = NCT6106_CRITICAL_PWM_ENABLE_MASK;
  3056. data->REG_CRITICAL_PWM = NCT6116_REG_CRITICAL_PWM;
  3057. data->REG_TEMP_OFFSET = NCT6106_REG_TEMP_OFFSET;
  3058. data->REG_TEMP_SOURCE = NCT6116_REG_TEMP_SOURCE;
  3059. data->REG_TEMP_SEL = NCT6116_REG_TEMP_SEL;
  3060. data->REG_WEIGHT_TEMP_SEL = NCT6106_REG_WEIGHT_TEMP_SEL;
  3061. data->REG_WEIGHT_TEMP[0] = NCT6106_REG_WEIGHT_TEMP_STEP;
  3062. data->REG_WEIGHT_TEMP[1] = NCT6106_REG_WEIGHT_TEMP_STEP_TOL;
  3063. data->REG_WEIGHT_TEMP[2] = NCT6106_REG_WEIGHT_TEMP_BASE;
  3064. data->REG_ALARM = NCT6106_REG_ALARM;
  3065. data->ALARM_BITS = NCT6116_ALARM_BITS;
  3066. data->REG_BEEP = NCT6106_REG_BEEP;
  3067. data->BEEP_BITS = NCT6116_BEEP_BITS;
  3068. data->REG_TSI_TEMP = NCT6116_REG_TSI_TEMP;
  3069. reg_temp = NCT6106_REG_TEMP;
  3070. reg_temp_mon = NCT6106_REG_TEMP_MON;
  3071. num_reg_temp = ARRAY_SIZE(NCT6106_REG_TEMP);
  3072. num_reg_temp_mon = ARRAY_SIZE(NCT6106_REG_TEMP_MON);
  3073. num_reg_tsi_temp = ARRAY_SIZE(NCT6116_REG_TSI_TEMP);
  3074. reg_temp_over = NCT6106_REG_TEMP_OVER;
  3075. reg_temp_hyst = NCT6106_REG_TEMP_HYST;
  3076. reg_temp_config = NCT6106_REG_TEMP_CONFIG;
  3077. reg_temp_alternate = NCT6106_REG_TEMP_ALTERNATE;
  3078. reg_temp_crit = NCT6106_REG_TEMP_CRIT;
  3079. reg_temp_crit_l = NCT6106_REG_TEMP_CRIT_L;
  3080. reg_temp_crit_h = NCT6106_REG_TEMP_CRIT_H;
  3081. break;
  3082. case nct6775:
  3083. data->in_num = 9;
  3084. data->pwm_num = 3;
  3085. data->auto_pwm_num = 6;
  3086. data->has_fan_div = true;
  3087. data->temp_fixed_num = 3;
  3088. data->num_temp_alarms = 3;
  3089. data->num_temp_beeps = 3;
  3090. data->ALARM_BITS = NCT6775_ALARM_BITS;
  3091. data->BEEP_BITS = NCT6775_BEEP_BITS;
  3092. data->fan_from_reg = fan_from_reg16;
  3093. data->fan_from_reg_min = fan_from_reg8;
  3094. data->target_temp_mask = 0x7f;
  3095. data->tolerance_mask = 0x0f;
  3096. data->speed_tolerance_limit = 15;
  3097. data->temp_label = nct6775_temp_label;
  3098. data->temp_mask = NCT6775_TEMP_MASK;
  3099. data->virt_temp_mask = NCT6775_VIRT_TEMP_MASK;
  3100. data->REG_CONFIG = NCT6775_REG_CONFIG;
  3101. data->REG_VBAT = NCT6775_REG_VBAT;
  3102. data->REG_DIODE = NCT6775_REG_DIODE;
  3103. data->DIODE_MASK = NCT6775_DIODE_MASK;
  3104. data->REG_VIN = NCT6775_REG_IN;
  3105. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  3106. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  3107. data->REG_TARGET = NCT6775_REG_TARGET;
  3108. data->REG_FAN = NCT6775_REG_FAN;
  3109. data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
  3110. data->REG_FAN_MIN = NCT6775_REG_FAN_MIN;
  3111. data->REG_FAN_PULSES = NCT6775_REG_FAN_PULSES;
  3112. data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
  3113. data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
  3114. data->REG_FAN_TIME[1] = NCT6775_REG_FAN_STEP_UP_TIME;
  3115. data->REG_FAN_TIME[2] = NCT6775_REG_FAN_STEP_DOWN_TIME;
  3116. data->REG_PWM[0] = NCT6775_REG_PWM;
  3117. data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
  3118. data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
  3119. data->REG_PWM[3] = NCT6775_REG_FAN_MAX_OUTPUT;
  3120. data->REG_PWM[4] = NCT6775_REG_FAN_STEP_OUTPUT;
  3121. data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
  3122. data->REG_PWM_READ = NCT6775_REG_PWM_READ;
  3123. data->REG_PWM_MODE = NCT6775_REG_PWM_MODE;
  3124. data->PWM_MODE_MASK = NCT6775_PWM_MODE_MASK;
  3125. data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
  3126. data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
  3127. data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
  3128. data->REG_CRITICAL_TEMP_TOLERANCE
  3129. = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
  3130. data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
  3131. data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
  3132. data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
  3133. data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
  3134. data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
  3135. data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
  3136. data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
  3137. data->REG_ALARM = NCT6775_REG_ALARM;
  3138. data->REG_BEEP = NCT6775_REG_BEEP;
  3139. data->REG_TSI_TEMP = NCT6775_REG_TSI_TEMP;
  3140. reg_temp = NCT6775_REG_TEMP;
  3141. reg_temp_mon = NCT6775_REG_TEMP_MON;
  3142. num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
  3143. num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
  3144. num_reg_tsi_temp = ARRAY_SIZE(NCT6775_REG_TSI_TEMP);
  3145. reg_temp_over = NCT6775_REG_TEMP_OVER;
  3146. reg_temp_hyst = NCT6775_REG_TEMP_HYST;
  3147. reg_temp_config = NCT6775_REG_TEMP_CONFIG;
  3148. reg_temp_alternate = NCT6775_REG_TEMP_ALTERNATE;
  3149. reg_temp_crit = NCT6775_REG_TEMP_CRIT;
  3150. break;
  3151. case nct6776:
  3152. data->in_num = 9;
  3153. data->pwm_num = 3;
  3154. data->auto_pwm_num = 4;
  3155. data->has_fan_div = false;
  3156. data->temp_fixed_num = 3;
  3157. data->num_temp_alarms = 3;
  3158. data->num_temp_beeps = 6;
  3159. data->ALARM_BITS = NCT6776_ALARM_BITS;
  3160. data->BEEP_BITS = NCT6776_BEEP_BITS;
  3161. data->fan_from_reg = fan_from_reg13;
  3162. data->fan_from_reg_min = fan_from_reg13;
  3163. data->target_temp_mask = 0xff;
  3164. data->tolerance_mask = 0x07;
  3165. data->speed_tolerance_limit = 63;
  3166. data->temp_label = nct6776_temp_label;
  3167. data->temp_mask = NCT6776_TEMP_MASK;
  3168. data->virt_temp_mask = NCT6776_VIRT_TEMP_MASK;
  3169. data->REG_CONFIG = NCT6775_REG_CONFIG;
  3170. data->REG_VBAT = NCT6775_REG_VBAT;
  3171. data->REG_DIODE = NCT6775_REG_DIODE;
  3172. data->DIODE_MASK = NCT6775_DIODE_MASK;
  3173. data->REG_VIN = NCT6775_REG_IN;
  3174. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  3175. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  3176. data->REG_TARGET = NCT6775_REG_TARGET;
  3177. data->REG_FAN = NCT6775_REG_FAN;
  3178. data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
  3179. data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
  3180. data->REG_FAN_PULSES = NCT6776_REG_FAN_PULSES;
  3181. data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
  3182. data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
  3183. data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
  3184. data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
  3185. data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
  3186. data->REG_PWM[0] = NCT6775_REG_PWM;
  3187. data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
  3188. data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
  3189. data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
  3190. data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
  3191. data->REG_PWM_READ = NCT6775_REG_PWM_READ;
  3192. data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
  3193. data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
  3194. data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
  3195. data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
  3196. data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
  3197. data->REG_CRITICAL_TEMP_TOLERANCE
  3198. = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
  3199. data->REG_TEMP_OFFSET = NCT6775_REG_TEMP_OFFSET;
  3200. data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
  3201. data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
  3202. data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
  3203. data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
  3204. data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
  3205. data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
  3206. data->REG_ALARM = NCT6775_REG_ALARM;
  3207. data->REG_BEEP = NCT6776_REG_BEEP;
  3208. data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
  3209. reg_temp = NCT6775_REG_TEMP;
  3210. reg_temp_mon = NCT6775_REG_TEMP_MON;
  3211. num_reg_temp = ARRAY_SIZE(NCT6775_REG_TEMP);
  3212. num_reg_temp_mon = ARRAY_SIZE(NCT6775_REG_TEMP_MON);
  3213. num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
  3214. reg_temp_over = NCT6775_REG_TEMP_OVER;
  3215. reg_temp_hyst = NCT6775_REG_TEMP_HYST;
  3216. reg_temp_config = NCT6776_REG_TEMP_CONFIG;
  3217. reg_temp_alternate = NCT6776_REG_TEMP_ALTERNATE;
  3218. reg_temp_crit = NCT6776_REG_TEMP_CRIT;
  3219. break;
  3220. case nct6779:
  3221. data->in_num = 15;
  3222. data->pwm_num = 5;
  3223. data->auto_pwm_num = 4;
  3224. data->has_fan_div = false;
  3225. data->temp_fixed_num = 6;
  3226. data->num_temp_alarms = 2;
  3227. data->num_temp_beeps = 2;
  3228. data->ALARM_BITS = NCT6779_ALARM_BITS;
  3229. data->BEEP_BITS = NCT6779_BEEP_BITS;
  3230. data->fan_from_reg = fan_from_reg_rpm;
  3231. data->fan_from_reg_min = fan_from_reg13;
  3232. data->target_temp_mask = 0xff;
  3233. data->tolerance_mask = 0x07;
  3234. data->speed_tolerance_limit = 63;
  3235. data->temp_label = nct6779_temp_label;
  3236. data->temp_mask = NCT6779_TEMP_MASK;
  3237. data->virt_temp_mask = NCT6779_VIRT_TEMP_MASK;
  3238. data->REG_CONFIG = NCT6775_REG_CONFIG;
  3239. data->REG_VBAT = NCT6775_REG_VBAT;
  3240. data->REG_DIODE = NCT6775_REG_DIODE;
  3241. data->DIODE_MASK = NCT6775_DIODE_MASK;
  3242. data->REG_VIN = NCT6779_REG_IN;
  3243. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  3244. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  3245. data->REG_TARGET = NCT6775_REG_TARGET;
  3246. data->REG_FAN = NCT6779_REG_FAN;
  3247. data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
  3248. data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
  3249. data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
  3250. data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
  3251. data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
  3252. data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
  3253. data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
  3254. data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
  3255. data->REG_PWM[0] = NCT6775_REG_PWM;
  3256. data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
  3257. data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
  3258. data->REG_PWM[5] = NCT6775_REG_WEIGHT_DUTY_STEP;
  3259. data->REG_PWM[6] = NCT6776_REG_WEIGHT_DUTY_BASE;
  3260. data->REG_PWM_READ = NCT6775_REG_PWM_READ;
  3261. data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
  3262. data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
  3263. data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
  3264. data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
  3265. data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
  3266. data->REG_CRITICAL_TEMP_TOLERANCE
  3267. = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
  3268. data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
  3269. data->CRITICAL_PWM_ENABLE_MASK
  3270. = NCT6779_CRITICAL_PWM_ENABLE_MASK;
  3271. data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
  3272. data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
  3273. data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
  3274. data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
  3275. data->REG_WEIGHT_TEMP_SEL = NCT6775_REG_WEIGHT_TEMP_SEL;
  3276. data->REG_WEIGHT_TEMP[0] = NCT6775_REG_WEIGHT_TEMP_STEP;
  3277. data->REG_WEIGHT_TEMP[1] = NCT6775_REG_WEIGHT_TEMP_STEP_TOL;
  3278. data->REG_WEIGHT_TEMP[2] = NCT6775_REG_WEIGHT_TEMP_BASE;
  3279. data->REG_ALARM = NCT6779_REG_ALARM;
  3280. data->REG_BEEP = NCT6776_REG_BEEP;
  3281. data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
  3282. reg_temp = NCT6779_REG_TEMP;
  3283. reg_temp_mon = NCT6779_REG_TEMP_MON;
  3284. num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
  3285. num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
  3286. num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
  3287. reg_temp_over = NCT6779_REG_TEMP_OVER;
  3288. reg_temp_hyst = NCT6779_REG_TEMP_HYST;
  3289. reg_temp_config = NCT6779_REG_TEMP_CONFIG;
  3290. reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
  3291. reg_temp_crit = NCT6779_REG_TEMP_CRIT;
  3292. break;
  3293. case nct6791:
  3294. case nct6792:
  3295. case nct6793:
  3296. case nct6795:
  3297. case nct6796:
  3298. case nct6797:
  3299. case nct6798:
  3300. data->in_num = 15;
  3301. data->pwm_num = (data->kind == nct6796 ||
  3302. data->kind == nct6797 ||
  3303. data->kind == nct6798) ? 7 : 6;
  3304. data->auto_pwm_num = 4;
  3305. data->has_fan_div = false;
  3306. data->temp_fixed_num = 6;
  3307. data->num_temp_alarms = 2;
  3308. data->num_temp_beeps = 2;
  3309. data->ALARM_BITS = NCT6791_ALARM_BITS;
  3310. data->BEEP_BITS = NCT6779_BEEP_BITS;
  3311. data->fan_from_reg = fan_from_reg_rpm;
  3312. data->fan_from_reg_min = fan_from_reg13;
  3313. data->target_temp_mask = 0xff;
  3314. data->tolerance_mask = 0x07;
  3315. data->speed_tolerance_limit = 63;
  3316. switch (data->kind) {
  3317. default:
  3318. case nct6791:
  3319. data->temp_label = nct6779_temp_label;
  3320. data->temp_mask = NCT6791_TEMP_MASK;
  3321. data->virt_temp_mask = NCT6791_VIRT_TEMP_MASK;
  3322. break;
  3323. case nct6792:
  3324. data->temp_label = nct6792_temp_label;
  3325. data->temp_mask = NCT6792_TEMP_MASK;
  3326. data->virt_temp_mask = NCT6792_VIRT_TEMP_MASK;
  3327. break;
  3328. case nct6793:
  3329. data->temp_label = nct6793_temp_label;
  3330. data->temp_mask = NCT6793_TEMP_MASK;
  3331. data->virt_temp_mask = NCT6793_VIRT_TEMP_MASK;
  3332. break;
  3333. case nct6795:
  3334. case nct6797:
  3335. data->temp_label = nct6795_temp_label;
  3336. data->temp_mask = NCT6795_TEMP_MASK;
  3337. data->virt_temp_mask = NCT6795_VIRT_TEMP_MASK;
  3338. break;
  3339. case nct6796:
  3340. data->temp_label = nct6796_temp_label;
  3341. data->temp_mask = NCT6796_TEMP_MASK;
  3342. data->virt_temp_mask = NCT6796_VIRT_TEMP_MASK;
  3343. break;
  3344. case nct6798:
  3345. data->temp_label = nct6798_temp_label;
  3346. data->temp_mask = NCT6798_TEMP_MASK;
  3347. data->virt_temp_mask = NCT6798_VIRT_TEMP_MASK;
  3348. break;
  3349. }
  3350. data->REG_CONFIG = NCT6775_REG_CONFIG;
  3351. data->REG_VBAT = NCT6775_REG_VBAT;
  3352. data->REG_DIODE = NCT6775_REG_DIODE;
  3353. data->DIODE_MASK = NCT6775_DIODE_MASK;
  3354. data->REG_VIN = NCT6779_REG_IN;
  3355. data->REG_IN_MINMAX[0] = NCT6775_REG_IN_MIN;
  3356. data->REG_IN_MINMAX[1] = NCT6775_REG_IN_MAX;
  3357. data->REG_TARGET = NCT6775_REG_TARGET;
  3358. data->REG_FAN = NCT6779_REG_FAN;
  3359. data->REG_FAN_MODE = NCT6775_REG_FAN_MODE;
  3360. data->REG_FAN_MIN = NCT6776_REG_FAN_MIN;
  3361. data->REG_FAN_PULSES = NCT6779_REG_FAN_PULSES;
  3362. data->FAN_PULSE_SHIFT = NCT6775_FAN_PULSE_SHIFT;
  3363. data->REG_FAN_TIME[0] = NCT6775_REG_FAN_STOP_TIME;
  3364. data->REG_FAN_TIME[1] = NCT6776_REG_FAN_STEP_UP_TIME;
  3365. data->REG_FAN_TIME[2] = NCT6776_REG_FAN_STEP_DOWN_TIME;
  3366. data->REG_TOLERANCE_H = NCT6776_REG_TOLERANCE_H;
  3367. data->REG_PWM[0] = NCT6775_REG_PWM;
  3368. data->REG_PWM[1] = NCT6775_REG_FAN_START_OUTPUT;
  3369. data->REG_PWM[2] = NCT6775_REG_FAN_STOP_OUTPUT;
  3370. data->REG_PWM[5] = NCT6791_REG_WEIGHT_DUTY_STEP;
  3371. data->REG_PWM[6] = NCT6791_REG_WEIGHT_DUTY_BASE;
  3372. data->REG_PWM_READ = NCT6775_REG_PWM_READ;
  3373. data->REG_PWM_MODE = NCT6776_REG_PWM_MODE;
  3374. data->PWM_MODE_MASK = NCT6776_PWM_MODE_MASK;
  3375. data->REG_AUTO_TEMP = NCT6775_REG_AUTO_TEMP;
  3376. data->REG_AUTO_PWM = NCT6775_REG_AUTO_PWM;
  3377. data->REG_CRITICAL_TEMP = NCT6775_REG_CRITICAL_TEMP;
  3378. data->REG_CRITICAL_TEMP_TOLERANCE
  3379. = NCT6775_REG_CRITICAL_TEMP_TOLERANCE;
  3380. data->REG_CRITICAL_PWM_ENABLE = NCT6779_REG_CRITICAL_PWM_ENABLE;
  3381. data->CRITICAL_PWM_ENABLE_MASK
  3382. = NCT6779_CRITICAL_PWM_ENABLE_MASK;
  3383. data->REG_CRITICAL_PWM = NCT6779_REG_CRITICAL_PWM;
  3384. data->REG_TEMP_OFFSET = NCT6779_REG_TEMP_OFFSET;
  3385. data->REG_TEMP_SOURCE = NCT6775_REG_TEMP_SOURCE;
  3386. data->REG_TEMP_SEL = NCT6775_REG_TEMP_SEL;
  3387. data->REG_WEIGHT_TEMP_SEL = NCT6791_REG_WEIGHT_TEMP_SEL;
  3388. data->REG_WEIGHT_TEMP[0] = NCT6791_REG_WEIGHT_TEMP_STEP;
  3389. data->REG_WEIGHT_TEMP[1] = NCT6791_REG_WEIGHT_TEMP_STEP_TOL;
  3390. data->REG_WEIGHT_TEMP[2] = NCT6791_REG_WEIGHT_TEMP_BASE;
  3391. data->REG_ALARM = NCT6791_REG_ALARM;
  3392. if (data->kind == nct6791)
  3393. data->REG_BEEP = NCT6776_REG_BEEP;
  3394. else
  3395. data->REG_BEEP = NCT6792_REG_BEEP;
  3396. switch (data->kind) {
  3397. case nct6791:
  3398. case nct6792:
  3399. case nct6793:
  3400. data->REG_TSI_TEMP = NCT6776_REG_TSI_TEMP;
  3401. num_reg_tsi_temp = ARRAY_SIZE(NCT6776_REG_TSI_TEMP);
  3402. break;
  3403. case nct6795:
  3404. case nct6796:
  3405. case nct6797:
  3406. case nct6798:
  3407. data->REG_TSI_TEMP = NCT6796_REG_TSI_TEMP;
  3408. num_reg_tsi_temp = ARRAY_SIZE(NCT6796_REG_TSI_TEMP);
  3409. break;
  3410. default:
  3411. num_reg_tsi_temp = 0;
  3412. break;
  3413. }
  3414. reg_temp = NCT6779_REG_TEMP;
  3415. num_reg_temp = ARRAY_SIZE(NCT6779_REG_TEMP);
  3416. if (data->kind == nct6791) {
  3417. reg_temp_mon = NCT6779_REG_TEMP_MON;
  3418. num_reg_temp_mon = ARRAY_SIZE(NCT6779_REG_TEMP_MON);
  3419. } else {
  3420. reg_temp_mon = NCT6792_REG_TEMP_MON;
  3421. num_reg_temp_mon = ARRAY_SIZE(NCT6792_REG_TEMP_MON);
  3422. }
  3423. reg_temp_over = NCT6779_REG_TEMP_OVER;
  3424. reg_temp_hyst = NCT6779_REG_TEMP_HYST;
  3425. reg_temp_config = NCT6779_REG_TEMP_CONFIG;
  3426. reg_temp_alternate = NCT6779_REG_TEMP_ALTERNATE;
  3427. reg_temp_crit = NCT6779_REG_TEMP_CRIT;
  3428. break;
  3429. default:
  3430. return -ENODEV;
  3431. }
  3432. data->have_in = BIT(data->in_num) - 1;
  3433. data->have_temp = 0;
  3434. /*
  3435. * On some boards, not all available temperature sources are monitored,
  3436. * even though some of the monitoring registers are unused.
  3437. * Get list of unused monitoring registers, then detect if any fan
  3438. * controls are configured to use unmonitored temperature sources.
  3439. * If so, assign the unmonitored temperature sources to available
  3440. * monitoring registers.
  3441. */
  3442. mask = 0;
  3443. available = 0;
  3444. for (i = 0; i < num_reg_temp; i++) {
  3445. if (reg_temp[i] == 0)
  3446. continue;
  3447. err = nct6775_read_value(data, data->REG_TEMP_SOURCE[i], &src);
  3448. if (err)
  3449. return err;
  3450. src &= 0x1f;
  3451. if (!src || (mask & BIT(src)))
  3452. available |= BIT(i);
  3453. mask |= BIT(src);
  3454. }
  3455. /*
  3456. * Now find unmonitored temperature registers and enable monitoring
  3457. * if additional monitoring registers are available.
  3458. */
  3459. err = add_temp_sensors(data, data->REG_TEMP_SEL, &available, &mask);
  3460. if (err)
  3461. return err;
  3462. err = add_temp_sensors(data, data->REG_WEIGHT_TEMP_SEL, &available, &mask);
  3463. if (err)
  3464. return err;
  3465. mask = 0;
  3466. s = NUM_TEMP_FIXED; /* First dynamic temperature attribute */
  3467. for (i = 0; i < num_reg_temp; i++) {
  3468. if (reg_temp[i] == 0)
  3469. continue;
  3470. err = nct6775_read_value(data, data->REG_TEMP_SOURCE[i], &src);
  3471. if (err)
  3472. return err;
  3473. src &= 0x1f;
  3474. if (!src || (mask & BIT(src)))
  3475. continue;
  3476. if (!(data->temp_mask & BIT(src))) {
  3477. dev_info(dev,
  3478. "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
  3479. src, i, data->REG_TEMP_SOURCE[i], reg_temp[i]);
  3480. continue;
  3481. }
  3482. mask |= BIT(src);
  3483. /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
  3484. if (src <= data->temp_fixed_num) {
  3485. data->have_temp |= BIT(src - 1);
  3486. data->have_temp_fixed |= BIT(src - 1);
  3487. data->reg_temp[0][src - 1] = reg_temp[i];
  3488. data->reg_temp[1][src - 1] = reg_temp_over[i];
  3489. data->reg_temp[2][src - 1] = reg_temp_hyst[i];
  3490. if (reg_temp_crit_h && reg_temp_crit_h[i])
  3491. data->reg_temp[3][src - 1] = reg_temp_crit_h[i];
  3492. else if (reg_temp_crit[src - 1])
  3493. data->reg_temp[3][src - 1]
  3494. = reg_temp_crit[src - 1];
  3495. if (reg_temp_crit_l && reg_temp_crit_l[i])
  3496. data->reg_temp[4][src - 1] = reg_temp_crit_l[i];
  3497. data->reg_temp_config[src - 1] = reg_temp_config[i];
  3498. data->temp_src[src - 1] = src;
  3499. continue;
  3500. }
  3501. if (s >= NUM_TEMP)
  3502. continue;
  3503. /* Use dynamic index for other sources */
  3504. data->have_temp |= BIT(s);
  3505. data->reg_temp[0][s] = reg_temp[i];
  3506. data->reg_temp[1][s] = reg_temp_over[i];
  3507. data->reg_temp[2][s] = reg_temp_hyst[i];
  3508. data->reg_temp_config[s] = reg_temp_config[i];
  3509. if (reg_temp_crit_h && reg_temp_crit_h[i])
  3510. data->reg_temp[3][s] = reg_temp_crit_h[i];
  3511. else if (reg_temp_crit[src - 1])
  3512. data->reg_temp[3][s] = reg_temp_crit[src - 1];
  3513. if (reg_temp_crit_l && reg_temp_crit_l[i])
  3514. data->reg_temp[4][s] = reg_temp_crit_l[i];
  3515. data->temp_src[s] = src;
  3516. s++;
  3517. }
  3518. /*
  3519. * Repeat with temperatures used for fan control.
  3520. * This set of registers does not support limits.
  3521. */
  3522. for (i = 0; i < num_reg_temp_mon; i++) {
  3523. if (reg_temp_mon[i] == 0)
  3524. continue;
  3525. err = nct6775_read_value(data, data->REG_TEMP_SEL[i], &src);
  3526. if (err)
  3527. return err;
  3528. src &= 0x1f;
  3529. if (!src)
  3530. continue;
  3531. if (!(data->temp_mask & BIT(src))) {
  3532. dev_info(dev,
  3533. "Invalid temperature source %d at index %d, source register 0x%x, temp register 0x%x\n",
  3534. src, i, data->REG_TEMP_SEL[i],
  3535. reg_temp_mon[i]);
  3536. continue;
  3537. }
  3538. /*
  3539. * For virtual temperature sources, the 'virtual' temperature
  3540. * for each fan reflects a different temperature, and there
  3541. * are no duplicates.
  3542. */
  3543. if (!(data->virt_temp_mask & BIT(src))) {
  3544. if (mask & BIT(src))
  3545. continue;
  3546. mask |= BIT(src);
  3547. }
  3548. /* Use fixed index for SYSTIN(1), CPUTIN(2), AUXTIN(3) */
  3549. if (src <= data->temp_fixed_num) {
  3550. if (data->have_temp & BIT(src - 1))
  3551. continue;
  3552. data->have_temp |= BIT(src - 1);
  3553. data->have_temp_fixed |= BIT(src - 1);
  3554. data->reg_temp[0][src - 1] = reg_temp_mon[i];
  3555. data->temp_src[src - 1] = src;
  3556. continue;
  3557. }
  3558. if (s >= NUM_TEMP)
  3559. continue;
  3560. /* Use dynamic index for other sources */
  3561. data->have_temp |= BIT(s);
  3562. data->reg_temp[0][s] = reg_temp_mon[i];
  3563. data->temp_src[s] = src;
  3564. s++;
  3565. }
  3566. #ifdef USE_ALTERNATE
  3567. /*
  3568. * Go through the list of alternate temp registers and enable
  3569. * if possible.
  3570. * The temperature is already monitored if the respective bit in <mask>
  3571. * is set.
  3572. */
  3573. for (i = 0; i < 31; i++) {
  3574. if (!(data->temp_mask & BIT(i + 1)))
  3575. continue;
  3576. if (!reg_temp_alternate[i])
  3577. continue;
  3578. if (mask & BIT(i + 1))
  3579. continue;
  3580. if (i < data->temp_fixed_num) {
  3581. if (data->have_temp & BIT(i))
  3582. continue;
  3583. data->have_temp |= BIT(i);
  3584. data->have_temp_fixed |= BIT(i);
  3585. data->reg_temp[0][i] = reg_temp_alternate[i];
  3586. if (i < num_reg_temp) {
  3587. data->reg_temp[1][i] = reg_temp_over[i];
  3588. data->reg_temp[2][i] = reg_temp_hyst[i];
  3589. }
  3590. data->temp_src[i] = i + 1;
  3591. continue;
  3592. }
  3593. if (s >= NUM_TEMP) /* Abort if no more space */
  3594. break;
  3595. data->have_temp |= BIT(s);
  3596. data->reg_temp[0][s] = reg_temp_alternate[i];
  3597. data->temp_src[s] = i + 1;
  3598. s++;
  3599. }
  3600. #endif /* USE_ALTERNATE */
  3601. /* Check which TSIx_TEMP registers are active */
  3602. for (i = 0; i < num_reg_tsi_temp; i++) {
  3603. u16 tmp;
  3604. err = nct6775_read_value(data, data->REG_TSI_TEMP[i], &tmp);
  3605. if (err)
  3606. return err;
  3607. if (tmp)
  3608. data->have_tsi_temp |= BIT(i);
  3609. }
  3610. /* Initialize the chip */
  3611. err = nct6775_init_device(data);
  3612. if (err)
  3613. return err;
  3614. if (data->driver_init) {
  3615. err = data->driver_init(data);
  3616. if (err)
  3617. return err;
  3618. }
  3619. /* Read fan clock dividers immediately */
  3620. err = nct6775_init_fan_common(dev, data);
  3621. if (err)
  3622. return err;
  3623. /* Register sysfs hooks */
  3624. err = nct6775_add_template_attr_group(dev, data, &nct6775_pwm_template_group,
  3625. data->pwm_num);
  3626. if (err)
  3627. return err;
  3628. err = nct6775_add_template_attr_group(dev, data, &nct6775_in_template_group,
  3629. fls(data->have_in));
  3630. if (err)
  3631. return err;
  3632. err = nct6775_add_template_attr_group(dev, data, &nct6775_fan_template_group,
  3633. fls(data->has_fan));
  3634. if (err)
  3635. return err;
  3636. err = nct6775_add_template_attr_group(dev, data, &nct6775_temp_template_group,
  3637. fls(data->have_temp));
  3638. if (err)
  3639. return err;
  3640. if (data->have_tsi_temp) {
  3641. tsi_temp_tg.templates = nct6775_tsi_temp_template;
  3642. tsi_temp_tg.is_visible = nct6775_tsi_temp_is_visible;
  3643. tsi_temp_tg.base = fls(data->have_temp) + 1;
  3644. err = nct6775_add_template_attr_group(dev, data, &tsi_temp_tg,
  3645. fls(data->have_tsi_temp));
  3646. if (err)
  3647. return err;
  3648. }
  3649. hwmon_dev = devm_hwmon_device_register_with_groups(dev, data->name,
  3650. data, data->groups);
  3651. return PTR_ERR_OR_ZERO(hwmon_dev);
  3652. }
  3653. EXPORT_SYMBOL_GPL(nct6775_probe);
  3654. MODULE_AUTHOR("Guenter Roeck <[email protected]>");
  3655. MODULE_DESCRIPTION("Core driver for NCT6775F and compatible chips");
  3656. MODULE_LICENSE("GPL");