nct6775.c 122 KB

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