nct6775.c 123 KB

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