nct6775.c 129 KB

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