it87.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186
  1. /*
  2. * it87.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring.
  4. *
  5. * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
  6. * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
  7. * addition to an Environment Controller (Enhanced Hardware Monitor and
  8. * Fan Controller)
  9. *
  10. * This driver supports only the Environment Controller in the IT8705F and
  11. * similar parts. The other devices are supported by different drivers.
  12. *
  13. * Supports: IT8603E Super I/O chip w/LPC interface
  14. * IT8620E Super I/O chip w/LPC interface
  15. * IT8623E Super I/O chip w/LPC interface
  16. * IT8628E Super I/O chip w/LPC interface
  17. * IT8705F Super I/O chip w/LPC interface
  18. * IT8712F Super I/O chip w/LPC interface
  19. * IT8716F Super I/O chip w/LPC interface
  20. * IT8718F Super I/O chip w/LPC interface
  21. * IT8720F Super I/O chip w/LPC interface
  22. * IT8721F Super I/O chip w/LPC interface
  23. * IT8726F Super I/O chip w/LPC interface
  24. * IT8728F Super I/O chip w/LPC interface
  25. * IT8732F Super I/O chip w/LPC interface
  26. * IT8758E Super I/O chip w/LPC interface
  27. * IT8771E Super I/O chip w/LPC interface
  28. * IT8772E Super I/O chip w/LPC interface
  29. * IT8781F Super I/O chip w/LPC interface
  30. * IT8782F Super I/O chip w/LPC interface
  31. * IT8783E/F Super I/O chip w/LPC interface
  32. * IT8786E Super I/O chip w/LPC interface
  33. * IT8790E Super I/O chip w/LPC interface
  34. * Sis950 A clone of the IT8705F
  35. *
  36. * Copyright (C) 2001 Chris Gauthron
  37. * Copyright (C) 2005-2010 Jean Delvare <jdelvare@suse.de>
  38. *
  39. * This program is free software; you can redistribute it and/or modify
  40. * it under the terms of the GNU General Public License as published by
  41. * the Free Software Foundation; either version 2 of the License, or
  42. * (at your option) any later version.
  43. *
  44. * This program is distributed in the hope that it will be useful,
  45. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  47. * GNU General Public License for more details.
  48. */
  49. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  50. #include <linux/bitops.h>
  51. #include <linux/module.h>
  52. #include <linux/init.h>
  53. #include <linux/slab.h>
  54. #include <linux/jiffies.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/hwmon.h>
  57. #include <linux/hwmon-sysfs.h>
  58. #include <linux/hwmon-vid.h>
  59. #include <linux/err.h>
  60. #include <linux/mutex.h>
  61. #include <linux/sysfs.h>
  62. #include <linux/string.h>
  63. #include <linux/dmi.h>
  64. #include <linux/acpi.h>
  65. #include <linux/io.h>
  66. #define DRVNAME "it87"
  67. enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728, it8732,
  68. it8771, it8772, it8781, it8782, it8783, it8786, it8790, it8603,
  69. it8620, it8628 };
  70. static unsigned short force_id;
  71. module_param(force_id, ushort, 0);
  72. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  73. static struct platform_device *it87_pdev[2];
  74. #define REG_2E 0x2e /* The register to read/write */
  75. #define REG_4E 0x4e /* Secondary register to read/write */
  76. #define DEV 0x07 /* Register: Logical device select */
  77. #define PME 0x04 /* The device with the fan registers in it */
  78. /* The device with the IT8718F/IT8720F VID value in it */
  79. #define GPIO 0x07
  80. #define DEVID 0x20 /* Register: Device ID */
  81. #define DEVREV 0x22 /* Register: Device Revision */
  82. static inline int superio_inb(int ioreg, int reg)
  83. {
  84. outb(reg, ioreg);
  85. return inb(ioreg + 1);
  86. }
  87. static inline void superio_outb(int ioreg, int reg, int val)
  88. {
  89. outb(reg, ioreg);
  90. outb(val, ioreg + 1);
  91. }
  92. static int superio_inw(int ioreg, int reg)
  93. {
  94. int val;
  95. outb(reg++, ioreg);
  96. val = inb(ioreg + 1) << 8;
  97. outb(reg, ioreg);
  98. val |= inb(ioreg + 1);
  99. return val;
  100. }
  101. static inline void superio_select(int ioreg, int ldn)
  102. {
  103. outb(DEV, ioreg);
  104. outb(ldn, ioreg + 1);
  105. }
  106. static inline int superio_enter(int ioreg)
  107. {
  108. /*
  109. * Try to reserve ioreg and ioreg + 1 for exclusive access.
  110. */
  111. if (!request_muxed_region(ioreg, 2, DRVNAME))
  112. return -EBUSY;
  113. outb(0x87, ioreg);
  114. outb(0x01, ioreg);
  115. outb(0x55, ioreg);
  116. outb(ioreg == REG_4E ? 0xaa : 0x55, ioreg);
  117. return 0;
  118. }
  119. static inline void superio_exit(int ioreg)
  120. {
  121. outb(0x02, ioreg);
  122. outb(0x02, ioreg + 1);
  123. release_region(ioreg, 2);
  124. }
  125. /* Logical device 4 registers */
  126. #define IT8712F_DEVID 0x8712
  127. #define IT8705F_DEVID 0x8705
  128. #define IT8716F_DEVID 0x8716
  129. #define IT8718F_DEVID 0x8718
  130. #define IT8720F_DEVID 0x8720
  131. #define IT8721F_DEVID 0x8721
  132. #define IT8726F_DEVID 0x8726
  133. #define IT8728F_DEVID 0x8728
  134. #define IT8732F_DEVID 0x8732
  135. #define IT8771E_DEVID 0x8771
  136. #define IT8772E_DEVID 0x8772
  137. #define IT8781F_DEVID 0x8781
  138. #define IT8782F_DEVID 0x8782
  139. #define IT8783E_DEVID 0x8783
  140. #define IT8786E_DEVID 0x8786
  141. #define IT8790E_DEVID 0x8790
  142. #define IT8603E_DEVID 0x8603
  143. #define IT8620E_DEVID 0x8620
  144. #define IT8623E_DEVID 0x8623
  145. #define IT8628E_DEVID 0x8628
  146. #define IT87_ACT_REG 0x30
  147. #define IT87_BASE_REG 0x60
  148. /* Logical device 7 registers (IT8712F and later) */
  149. #define IT87_SIO_GPIO1_REG 0x25
  150. #define IT87_SIO_GPIO2_REG 0x26
  151. #define IT87_SIO_GPIO3_REG 0x27
  152. #define IT87_SIO_GPIO4_REG 0x28
  153. #define IT87_SIO_GPIO5_REG 0x29
  154. #define IT87_SIO_PINX1_REG 0x2a /* Pin selection */
  155. #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
  156. #define IT87_SIO_SPI_REG 0xef /* SPI function pin select */
  157. #define IT87_SIO_VID_REG 0xfc /* VID value */
  158. #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
  159. /* Update battery voltage after every reading if true */
  160. static bool update_vbat;
  161. /* Not all BIOSes properly configure the PWM registers */
  162. static bool fix_pwm_polarity;
  163. /* Many IT87 constants specified below */
  164. /* Length of ISA address segment */
  165. #define IT87_EXTENT 8
  166. /* Length of ISA address segment for Environmental Controller */
  167. #define IT87_EC_EXTENT 2
  168. /* Offset of EC registers from ISA base address */
  169. #define IT87_EC_OFFSET 5
  170. /* Where are the ISA address/data registers relative to the EC base address */
  171. #define IT87_ADDR_REG_OFFSET 0
  172. #define IT87_DATA_REG_OFFSET 1
  173. /*----- The IT87 registers -----*/
  174. #define IT87_REG_CONFIG 0x00
  175. #define IT87_REG_ALARM1 0x01
  176. #define IT87_REG_ALARM2 0x02
  177. #define IT87_REG_ALARM3 0x03
  178. /*
  179. * The IT8718F and IT8720F have the VID value in a different register, in
  180. * Super-I/O configuration space.
  181. */
  182. #define IT87_REG_VID 0x0a
  183. /*
  184. * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
  185. * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
  186. * mode.
  187. */
  188. #define IT87_REG_FAN_DIV 0x0b
  189. #define IT87_REG_FAN_16BIT 0x0c
  190. /*
  191. * Monitors:
  192. * - up to 13 voltage (0 to 7, battery, avcc, 10 to 12)
  193. * - up to 6 temp (1 to 6)
  194. * - up to 6 fan (1 to 6)
  195. */
  196. static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82, 0x4c };
  197. static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86, 0x4e };
  198. static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83, 0x4d };
  199. static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87, 0x4f };
  200. static const u8 IT87_REG_TEMP_OFFSET[] = { 0x56, 0x57, 0x59 };
  201. #define IT87_REG_FAN_MAIN_CTRL 0x13
  202. #define IT87_REG_FAN_CTL 0x14
  203. static const u8 IT87_REG_PWM[] = { 0x15, 0x16, 0x17, 0x7f, 0xa7, 0xaf };
  204. static const u8 IT87_REG_PWM_DUTY[] = { 0x63, 0x6b, 0x73, 0x7b, 0xa3, 0xab };
  205. static const u8 IT87_REG_VIN[] = { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
  206. 0x27, 0x28, 0x2f, 0x2c, 0x2d, 0x2e };
  207. #define IT87_REG_TEMP(nr) (0x29 + (nr))
  208. #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
  209. #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
  210. #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
  211. #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
  212. #define IT87_REG_VIN_ENABLE 0x50
  213. #define IT87_REG_TEMP_ENABLE 0x51
  214. #define IT87_REG_TEMP_EXTRA 0x55
  215. #define IT87_REG_BEEP_ENABLE 0x5c
  216. #define IT87_REG_CHIPID 0x58
  217. static const u8 IT87_REG_AUTO_BASE[] = { 0x60, 0x68, 0x70, 0x78, 0xa0, 0xa8 };
  218. #define IT87_REG_AUTO_TEMP(nr, i) (IT87_REG_AUTO_BASE[nr] + (i))
  219. #define IT87_REG_AUTO_PWM(nr, i) (IT87_REG_AUTO_BASE[nr] + 5 + (i))
  220. #define IT87_REG_TEMP456_ENABLE 0x77
  221. #define NUM_VIN ARRAY_SIZE(IT87_REG_VIN)
  222. #define NUM_VIN_LIMIT 8
  223. #define NUM_TEMP 6
  224. #define NUM_TEMP_OFFSET ARRAY_SIZE(IT87_REG_TEMP_OFFSET)
  225. #define NUM_TEMP_LIMIT 3
  226. #define NUM_FAN ARRAY_SIZE(IT87_REG_FAN)
  227. #define NUM_FAN_DIV 3
  228. #define NUM_PWM ARRAY_SIZE(IT87_REG_PWM)
  229. #define NUM_AUTO_PWM ARRAY_SIZE(IT87_REG_PWM)
  230. struct it87_devices {
  231. const char *name;
  232. const char * const suffix;
  233. u32 features;
  234. u8 peci_mask;
  235. u8 old_peci_mask;
  236. };
  237. #define FEAT_12MV_ADC BIT(0)
  238. #define FEAT_NEWER_AUTOPWM BIT(1)
  239. #define FEAT_OLD_AUTOPWM BIT(2)
  240. #define FEAT_16BIT_FANS BIT(3)
  241. #define FEAT_TEMP_OFFSET BIT(4)
  242. #define FEAT_TEMP_PECI BIT(5)
  243. #define FEAT_TEMP_OLD_PECI BIT(6)
  244. #define FEAT_FAN16_CONFIG BIT(7) /* Need to enable 16-bit fans */
  245. #define FEAT_FIVE_FANS BIT(8) /* Supports five fans */
  246. #define FEAT_VID BIT(9) /* Set if chip supports VID */
  247. #define FEAT_IN7_INTERNAL BIT(10) /* Set if in7 is internal */
  248. #define FEAT_SIX_FANS BIT(11) /* Supports six fans */
  249. #define FEAT_10_9MV_ADC BIT(12)
  250. #define FEAT_AVCC3 BIT(13) /* Chip supports in9/AVCC3 */
  251. #define FEAT_SIX_PWM BIT(14) /* Chip supports 6 pwm chn */
  252. #define FEAT_PWM_FREQ2 BIT(15) /* Separate pwm freq 2 */
  253. #define FEAT_SIX_TEMP BIT(16) /* Up to 6 temp sensors */
  254. static const struct it87_devices it87_devices[] = {
  255. [it87] = {
  256. .name = "it87",
  257. .suffix = "F",
  258. .features = FEAT_OLD_AUTOPWM, /* may need to overwrite */
  259. },
  260. [it8712] = {
  261. .name = "it8712",
  262. .suffix = "F",
  263. .features = FEAT_OLD_AUTOPWM | FEAT_VID,
  264. /* may need to overwrite */
  265. },
  266. [it8716] = {
  267. .name = "it8716",
  268. .suffix = "F",
  269. .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
  270. | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_PWM_FREQ2,
  271. },
  272. [it8718] = {
  273. .name = "it8718",
  274. .suffix = "F",
  275. .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
  276. | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
  277. | FEAT_PWM_FREQ2,
  278. .old_peci_mask = 0x4,
  279. },
  280. [it8720] = {
  281. .name = "it8720",
  282. .suffix = "F",
  283. .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET | FEAT_VID
  284. | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS
  285. | FEAT_PWM_FREQ2,
  286. .old_peci_mask = 0x4,
  287. },
  288. [it8721] = {
  289. .name = "it8721",
  290. .suffix = "F",
  291. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  292. | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
  293. | FEAT_FAN16_CONFIG | FEAT_FIVE_FANS | FEAT_IN7_INTERNAL
  294. | FEAT_PWM_FREQ2,
  295. .peci_mask = 0x05,
  296. .old_peci_mask = 0x02, /* Actually reports PCH */
  297. },
  298. [it8728] = {
  299. .name = "it8728",
  300. .suffix = "F",
  301. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  302. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_FIVE_FANS
  303. | FEAT_IN7_INTERNAL | FEAT_PWM_FREQ2,
  304. .peci_mask = 0x07,
  305. },
  306. [it8732] = {
  307. .name = "it8732",
  308. .suffix = "F",
  309. .features = FEAT_NEWER_AUTOPWM | FEAT_16BIT_FANS
  310. | FEAT_TEMP_OFFSET | FEAT_TEMP_OLD_PECI | FEAT_TEMP_PECI
  311. | FEAT_10_9MV_ADC | FEAT_IN7_INTERNAL,
  312. .peci_mask = 0x07,
  313. .old_peci_mask = 0x02, /* Actually reports PCH */
  314. },
  315. [it8771] = {
  316. .name = "it8771",
  317. .suffix = "E",
  318. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  319. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
  320. | FEAT_PWM_FREQ2,
  321. /* PECI: guesswork */
  322. /* 12mV ADC (OHM) */
  323. /* 16 bit fans (OHM) */
  324. /* three fans, always 16 bit (guesswork) */
  325. .peci_mask = 0x07,
  326. },
  327. [it8772] = {
  328. .name = "it8772",
  329. .suffix = "E",
  330. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  331. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
  332. | FEAT_PWM_FREQ2,
  333. /* PECI (coreboot) */
  334. /* 12mV ADC (HWSensors4, OHM) */
  335. /* 16 bit fans (HWSensors4, OHM) */
  336. /* three fans, always 16 bit (datasheet) */
  337. .peci_mask = 0x07,
  338. },
  339. [it8781] = {
  340. .name = "it8781",
  341. .suffix = "F",
  342. .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
  343. | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
  344. .old_peci_mask = 0x4,
  345. },
  346. [it8782] = {
  347. .name = "it8782",
  348. .suffix = "F",
  349. .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
  350. | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
  351. .old_peci_mask = 0x4,
  352. },
  353. [it8783] = {
  354. .name = "it8783",
  355. .suffix = "E/F",
  356. .features = FEAT_16BIT_FANS | FEAT_TEMP_OFFSET
  357. | FEAT_TEMP_OLD_PECI | FEAT_FAN16_CONFIG | FEAT_PWM_FREQ2,
  358. .old_peci_mask = 0x4,
  359. },
  360. [it8786] = {
  361. .name = "it8786",
  362. .suffix = "E",
  363. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  364. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
  365. | FEAT_PWM_FREQ2,
  366. .peci_mask = 0x07,
  367. },
  368. [it8790] = {
  369. .name = "it8790",
  370. .suffix = "E",
  371. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  372. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
  373. | FEAT_PWM_FREQ2,
  374. .peci_mask = 0x07,
  375. },
  376. [it8603] = {
  377. .name = "it8603",
  378. .suffix = "E",
  379. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  380. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_IN7_INTERNAL
  381. | FEAT_AVCC3 | FEAT_PWM_FREQ2,
  382. .peci_mask = 0x07,
  383. },
  384. [it8620] = {
  385. .name = "it8620",
  386. .suffix = "E",
  387. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  388. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
  389. | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
  390. | FEAT_SIX_TEMP,
  391. .peci_mask = 0x07,
  392. },
  393. [it8628] = {
  394. .name = "it8628",
  395. .suffix = "E",
  396. .features = FEAT_NEWER_AUTOPWM | FEAT_12MV_ADC | FEAT_16BIT_FANS
  397. | FEAT_TEMP_OFFSET | FEAT_TEMP_PECI | FEAT_SIX_FANS
  398. | FEAT_IN7_INTERNAL | FEAT_SIX_PWM | FEAT_PWM_FREQ2
  399. | FEAT_SIX_TEMP,
  400. .peci_mask = 0x07,
  401. },
  402. };
  403. #define has_16bit_fans(data) ((data)->features & FEAT_16BIT_FANS)
  404. #define has_12mv_adc(data) ((data)->features & FEAT_12MV_ADC)
  405. #define has_10_9mv_adc(data) ((data)->features & FEAT_10_9MV_ADC)
  406. #define has_newer_autopwm(data) ((data)->features & FEAT_NEWER_AUTOPWM)
  407. #define has_old_autopwm(data) ((data)->features & FEAT_OLD_AUTOPWM)
  408. #define has_temp_offset(data) ((data)->features & FEAT_TEMP_OFFSET)
  409. #define has_temp_peci(data, nr) (((data)->features & FEAT_TEMP_PECI) && \
  410. ((data)->peci_mask & BIT(nr)))
  411. #define has_temp_old_peci(data, nr) \
  412. (((data)->features & FEAT_TEMP_OLD_PECI) && \
  413. ((data)->old_peci_mask & BIT(nr)))
  414. #define has_fan16_config(data) ((data)->features & FEAT_FAN16_CONFIG)
  415. #define has_five_fans(data) ((data)->features & (FEAT_FIVE_FANS | \
  416. FEAT_SIX_FANS))
  417. #define has_vid(data) ((data)->features & FEAT_VID)
  418. #define has_in7_internal(data) ((data)->features & FEAT_IN7_INTERNAL)
  419. #define has_six_fans(data) ((data)->features & FEAT_SIX_FANS)
  420. #define has_avcc3(data) ((data)->features & FEAT_AVCC3)
  421. #define has_six_pwm(data) ((data)->features & FEAT_SIX_PWM)
  422. #define has_pwm_freq2(data) ((data)->features & FEAT_PWM_FREQ2)
  423. #define has_six_temp(data) ((data)->features & FEAT_SIX_TEMP)
  424. struct it87_sio_data {
  425. enum chips type;
  426. /* Values read from Super-I/O config space */
  427. u8 revision;
  428. u8 vid_value;
  429. u8 beep_pin;
  430. u8 internal; /* Internal sensors can be labeled */
  431. /* Features skipped based on config or DMI */
  432. u16 skip_in;
  433. u8 skip_vid;
  434. u8 skip_fan;
  435. u8 skip_pwm;
  436. u8 skip_temp;
  437. };
  438. /*
  439. * For each registered chip, we need to keep some data in memory.
  440. * The structure is dynamically allocated.
  441. */
  442. struct it87_data {
  443. const struct attribute_group *groups[7];
  444. enum chips type;
  445. u32 features;
  446. u8 peci_mask;
  447. u8 old_peci_mask;
  448. unsigned short addr;
  449. const char *name;
  450. struct mutex update_lock;
  451. char valid; /* !=0 if following fields are valid */
  452. unsigned long last_updated; /* In jiffies */
  453. u16 in_scaled; /* Internal voltage sensors are scaled */
  454. u16 in_internal; /* Bitfield, internal sensors (for labels) */
  455. u16 has_in; /* Bitfield, voltage sensors enabled */
  456. u8 in[NUM_VIN][3]; /* [nr][0]=in, [1]=min, [2]=max */
  457. u8 has_fan; /* Bitfield, fans enabled */
  458. u16 fan[NUM_FAN][2]; /* Register values, [nr][0]=fan, [1]=min */
  459. u8 has_temp; /* Bitfield, temp sensors enabled */
  460. s8 temp[NUM_TEMP][4]; /* [nr][0]=temp, [1]=min, [2]=max, [3]=offset */
  461. u8 sensor; /* Register value (IT87_REG_TEMP_ENABLE) */
  462. u8 extra; /* Register value (IT87_REG_TEMP_EXTRA) */
  463. u8 fan_div[NUM_FAN_DIV];/* Register encoding, shifted right */
  464. bool has_vid; /* True if VID supported */
  465. u8 vid; /* Register encoding, combined */
  466. u8 vrm;
  467. u32 alarms; /* Register encoding, combined */
  468. bool has_beep; /* true if beep supported */
  469. u8 beeps; /* Register encoding */
  470. u8 fan_main_ctrl; /* Register value */
  471. u8 fan_ctl; /* Register value */
  472. /*
  473. * The following 3 arrays correspond to the same registers up to
  474. * the IT8720F. The meaning of bits 6-0 depends on the value of bit
  475. * 7, and we want to preserve settings on mode changes, so we have
  476. * to track all values separately.
  477. * Starting with the IT8721F, the manual PWM duty cycles are stored
  478. * in separate registers (8-bit values), so the separate tracking
  479. * is no longer needed, but it is still done to keep the driver
  480. * simple.
  481. */
  482. u8 has_pwm; /* Bitfield, pwm control enabled */
  483. u8 pwm_ctrl[NUM_PWM]; /* Register value */
  484. u8 pwm_duty[NUM_PWM]; /* Manual PWM value set by user */
  485. u8 pwm_temp_map[NUM_PWM];/* PWM to temp. chan. mapping (bits 1-0) */
  486. /* Automatic fan speed control registers */
  487. u8 auto_pwm[NUM_AUTO_PWM][4]; /* [nr][3] is hard-coded */
  488. s8 auto_temp[NUM_AUTO_PWM][5]; /* [nr][0] is point1_temp_hyst */
  489. };
  490. static int adc_lsb(const struct it87_data *data, int nr)
  491. {
  492. int lsb;
  493. if (has_12mv_adc(data))
  494. lsb = 120;
  495. else if (has_10_9mv_adc(data))
  496. lsb = 109;
  497. else
  498. lsb = 160;
  499. if (data->in_scaled & BIT(nr))
  500. lsb <<= 1;
  501. return lsb;
  502. }
  503. static u8 in_to_reg(const struct it87_data *data, int nr, long val)
  504. {
  505. val = DIV_ROUND_CLOSEST(val * 10, adc_lsb(data, nr));
  506. return clamp_val(val, 0, 255);
  507. }
  508. static int in_from_reg(const struct it87_data *data, int nr, int val)
  509. {
  510. return DIV_ROUND_CLOSEST(val * adc_lsb(data, nr), 10);
  511. }
  512. static inline u8 FAN_TO_REG(long rpm, int div)
  513. {
  514. if (rpm == 0)
  515. return 255;
  516. rpm = clamp_val(rpm, 1, 1000000);
  517. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  518. }
  519. static inline u16 FAN16_TO_REG(long rpm)
  520. {
  521. if (rpm == 0)
  522. return 0xffff;
  523. return clamp_val((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
  524. }
  525. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
  526. 1350000 / ((val) * (div)))
  527. /* The divider is fixed to 2 in 16-bit mode */
  528. #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
  529. 1350000 / ((val) * 2))
  530. #define TEMP_TO_REG(val) (clamp_val(((val) < 0 ? (((val) - 500) / 1000) : \
  531. ((val) + 500) / 1000), -128, 127))
  532. #define TEMP_FROM_REG(val) ((val) * 1000)
  533. static u8 pwm_to_reg(const struct it87_data *data, long val)
  534. {
  535. if (has_newer_autopwm(data))
  536. return val;
  537. else
  538. return val >> 1;
  539. }
  540. static int pwm_from_reg(const struct it87_data *data, u8 reg)
  541. {
  542. if (has_newer_autopwm(data))
  543. return reg;
  544. else
  545. return (reg & 0x7f) << 1;
  546. }
  547. static int DIV_TO_REG(int val)
  548. {
  549. int answer = 0;
  550. while (answer < 7 && (val >>= 1))
  551. answer++;
  552. return answer;
  553. }
  554. #define DIV_FROM_REG(val) BIT(val)
  555. /*
  556. * PWM base frequencies. The frequency has to be divided by either 128 or 256,
  557. * depending on the chip type, to calculate the actual PWM frequency.
  558. *
  559. * Some of the chip datasheets suggest a base frequency of 51 kHz instead
  560. * of 750 kHz for the slowest base frequency, resulting in a PWM frequency
  561. * of 200 Hz. Sometimes both PWM frequency select registers are affected,
  562. * sometimes just one. It is unknown if this is a datasheet error or real,
  563. * so this is ignored for now.
  564. */
  565. static const unsigned int pwm_freq[8] = {
  566. 48000000,
  567. 24000000,
  568. 12000000,
  569. 8000000,
  570. 6000000,
  571. 3000000,
  572. 1500000,
  573. 750000,
  574. };
  575. /*
  576. * Must be called with data->update_lock held, except during initialization.
  577. * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  578. * would slow down the IT87 access and should not be necessary.
  579. */
  580. static int it87_read_value(struct it87_data *data, u8 reg)
  581. {
  582. outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
  583. return inb_p(data->addr + IT87_DATA_REG_OFFSET);
  584. }
  585. /*
  586. * Must be called with data->update_lock held, except during initialization.
  587. * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  588. * would slow down the IT87 access and should not be necessary.
  589. */
  590. static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
  591. {
  592. outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
  593. outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
  594. }
  595. static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
  596. {
  597. data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM[nr]);
  598. if (has_newer_autopwm(data)) {
  599. data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
  600. data->pwm_duty[nr] = it87_read_value(data,
  601. IT87_REG_PWM_DUTY[nr]);
  602. } else {
  603. if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
  604. data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
  605. else /* Manual mode */
  606. data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
  607. }
  608. if (has_old_autopwm(data)) {
  609. int i;
  610. for (i = 0; i < 5 ; i++)
  611. data->auto_temp[nr][i] = it87_read_value(data,
  612. IT87_REG_AUTO_TEMP(nr, i));
  613. for (i = 0; i < 3 ; i++)
  614. data->auto_pwm[nr][i] = it87_read_value(data,
  615. IT87_REG_AUTO_PWM(nr, i));
  616. } else if (has_newer_autopwm(data)) {
  617. int i;
  618. /*
  619. * 0: temperature hysteresis (base + 5)
  620. * 1: fan off temperature (base + 0)
  621. * 2: fan start temperature (base + 1)
  622. * 3: fan max temperature (base + 2)
  623. */
  624. data->auto_temp[nr][0] =
  625. it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 5));
  626. for (i = 0; i < 3 ; i++)
  627. data->auto_temp[nr][i + 1] =
  628. it87_read_value(data,
  629. IT87_REG_AUTO_TEMP(nr, i));
  630. /*
  631. * 0: start pwm value (base + 3)
  632. * 1: pwm slope (base + 4, 1/8th pwm)
  633. */
  634. data->auto_pwm[nr][0] =
  635. it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 3));
  636. data->auto_pwm[nr][1] =
  637. it87_read_value(data, IT87_REG_AUTO_TEMP(nr, 4));
  638. }
  639. }
  640. static struct it87_data *it87_update_device(struct device *dev)
  641. {
  642. struct it87_data *data = dev_get_drvdata(dev);
  643. int i;
  644. mutex_lock(&data->update_lock);
  645. if (time_after(jiffies, data->last_updated + HZ + HZ / 2) ||
  646. !data->valid) {
  647. if (update_vbat) {
  648. /*
  649. * Cleared after each update, so reenable. Value
  650. * returned by this read will be previous value
  651. */
  652. it87_write_value(data, IT87_REG_CONFIG,
  653. it87_read_value(data, IT87_REG_CONFIG) | 0x40);
  654. }
  655. for (i = 0; i < NUM_VIN; i++) {
  656. if (!(data->has_in & BIT(i)))
  657. continue;
  658. data->in[i][0] =
  659. it87_read_value(data, IT87_REG_VIN[i]);
  660. /* VBAT and AVCC don't have limit registers */
  661. if (i >= NUM_VIN_LIMIT)
  662. continue;
  663. data->in[i][1] =
  664. it87_read_value(data, IT87_REG_VIN_MIN(i));
  665. data->in[i][2] =
  666. it87_read_value(data, IT87_REG_VIN_MAX(i));
  667. }
  668. for (i = 0; i < NUM_FAN; i++) {
  669. /* Skip disabled fans */
  670. if (!(data->has_fan & BIT(i)))
  671. continue;
  672. data->fan[i][1] =
  673. it87_read_value(data, IT87_REG_FAN_MIN[i]);
  674. data->fan[i][0] = it87_read_value(data,
  675. IT87_REG_FAN[i]);
  676. /* Add high byte if in 16-bit mode */
  677. if (has_16bit_fans(data)) {
  678. data->fan[i][0] |= it87_read_value(data,
  679. IT87_REG_FANX[i]) << 8;
  680. data->fan[i][1] |= it87_read_value(data,
  681. IT87_REG_FANX_MIN[i]) << 8;
  682. }
  683. }
  684. for (i = 0; i < NUM_TEMP; i++) {
  685. if (!(data->has_temp & BIT(i)))
  686. continue;
  687. data->temp[i][0] =
  688. it87_read_value(data, IT87_REG_TEMP(i));
  689. if (has_temp_offset(data) && i < NUM_TEMP_OFFSET)
  690. data->temp[i][3] =
  691. it87_read_value(data,
  692. IT87_REG_TEMP_OFFSET[i]);
  693. if (i >= NUM_TEMP_LIMIT)
  694. continue;
  695. data->temp[i][1] =
  696. it87_read_value(data, IT87_REG_TEMP_LOW(i));
  697. data->temp[i][2] =
  698. it87_read_value(data, IT87_REG_TEMP_HIGH(i));
  699. }
  700. /* Newer chips don't have clock dividers */
  701. if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
  702. i = it87_read_value(data, IT87_REG_FAN_DIV);
  703. data->fan_div[0] = i & 0x07;
  704. data->fan_div[1] = (i >> 3) & 0x07;
  705. data->fan_div[2] = (i & 0x40) ? 3 : 1;
  706. }
  707. data->alarms =
  708. it87_read_value(data, IT87_REG_ALARM1) |
  709. (it87_read_value(data, IT87_REG_ALARM2) << 8) |
  710. (it87_read_value(data, IT87_REG_ALARM3) << 16);
  711. data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
  712. data->fan_main_ctrl = it87_read_value(data,
  713. IT87_REG_FAN_MAIN_CTRL);
  714. data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
  715. for (i = 0; i < NUM_PWM; i++) {
  716. if (!(data->has_pwm & BIT(i)))
  717. continue;
  718. it87_update_pwm_ctrl(data, i);
  719. }
  720. data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
  721. data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
  722. /*
  723. * The IT8705F does not have VID capability.
  724. * The IT8718F and later don't use IT87_REG_VID for the
  725. * same purpose.
  726. */
  727. if (data->type == it8712 || data->type == it8716) {
  728. data->vid = it87_read_value(data, IT87_REG_VID);
  729. /*
  730. * The older IT8712F revisions had only 5 VID pins,
  731. * but we assume it is always safe to read 6 bits.
  732. */
  733. data->vid &= 0x3f;
  734. }
  735. data->last_updated = jiffies;
  736. data->valid = 1;
  737. }
  738. mutex_unlock(&data->update_lock);
  739. return data;
  740. }
  741. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  742. char *buf)
  743. {
  744. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  745. struct it87_data *data = it87_update_device(dev);
  746. int index = sattr->index;
  747. int nr = sattr->nr;
  748. return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr][index]));
  749. }
  750. static ssize_t set_in(struct device *dev, struct device_attribute *attr,
  751. const char *buf, size_t count)
  752. {
  753. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  754. struct it87_data *data = dev_get_drvdata(dev);
  755. int index = sattr->index;
  756. int nr = sattr->nr;
  757. unsigned long val;
  758. if (kstrtoul(buf, 10, &val) < 0)
  759. return -EINVAL;
  760. mutex_lock(&data->update_lock);
  761. data->in[nr][index] = in_to_reg(data, nr, val);
  762. it87_write_value(data,
  763. index == 1 ? IT87_REG_VIN_MIN(nr)
  764. : IT87_REG_VIN_MAX(nr),
  765. data->in[nr][index]);
  766. mutex_unlock(&data->update_lock);
  767. return count;
  768. }
  769. static SENSOR_DEVICE_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0);
  770. static SENSOR_DEVICE_ATTR_2(in0_min, S_IRUGO | S_IWUSR, show_in, set_in,
  771. 0, 1);
  772. static SENSOR_DEVICE_ATTR_2(in0_max, S_IRUGO | S_IWUSR, show_in, set_in,
  773. 0, 2);
  774. static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 1, 0);
  775. static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_in, set_in,
  776. 1, 1);
  777. static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_in, set_in,
  778. 1, 2);
  779. static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 2, 0);
  780. static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_in, set_in,
  781. 2, 1);
  782. static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_in, set_in,
  783. 2, 2);
  784. static SENSOR_DEVICE_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 3, 0);
  785. static SENSOR_DEVICE_ATTR_2(in3_min, S_IRUGO | S_IWUSR, show_in, set_in,
  786. 3, 1);
  787. static SENSOR_DEVICE_ATTR_2(in3_max, S_IRUGO | S_IWUSR, show_in, set_in,
  788. 3, 2);
  789. static SENSOR_DEVICE_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 4, 0);
  790. static SENSOR_DEVICE_ATTR_2(in4_min, S_IRUGO | S_IWUSR, show_in, set_in,
  791. 4, 1);
  792. static SENSOR_DEVICE_ATTR_2(in4_max, S_IRUGO | S_IWUSR, show_in, set_in,
  793. 4, 2);
  794. static SENSOR_DEVICE_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 5, 0);
  795. static SENSOR_DEVICE_ATTR_2(in5_min, S_IRUGO | S_IWUSR, show_in, set_in,
  796. 5, 1);
  797. static SENSOR_DEVICE_ATTR_2(in5_max, S_IRUGO | S_IWUSR, show_in, set_in,
  798. 5, 2);
  799. static SENSOR_DEVICE_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 6, 0);
  800. static SENSOR_DEVICE_ATTR_2(in6_min, S_IRUGO | S_IWUSR, show_in, set_in,
  801. 6, 1);
  802. static SENSOR_DEVICE_ATTR_2(in6_max, S_IRUGO | S_IWUSR, show_in, set_in,
  803. 6, 2);
  804. static SENSOR_DEVICE_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 7, 0);
  805. static SENSOR_DEVICE_ATTR_2(in7_min, S_IRUGO | S_IWUSR, show_in, set_in,
  806. 7, 1);
  807. static SENSOR_DEVICE_ATTR_2(in7_max, S_IRUGO | S_IWUSR, show_in, set_in,
  808. 7, 2);
  809. static SENSOR_DEVICE_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 8, 0);
  810. static SENSOR_DEVICE_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 9, 0);
  811. static SENSOR_DEVICE_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 10, 0);
  812. static SENSOR_DEVICE_ATTR_2(in11_input, S_IRUGO, show_in, NULL, 11, 0);
  813. static SENSOR_DEVICE_ATTR_2(in12_input, S_IRUGO, show_in, NULL, 12, 0);
  814. /* Up to 6 temperatures */
  815. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  816. char *buf)
  817. {
  818. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  819. int nr = sattr->nr;
  820. int index = sattr->index;
  821. struct it87_data *data = it87_update_device(dev);
  822. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr][index]));
  823. }
  824. static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
  825. const char *buf, size_t count)
  826. {
  827. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  828. int nr = sattr->nr;
  829. int index = sattr->index;
  830. struct it87_data *data = dev_get_drvdata(dev);
  831. long val;
  832. u8 reg, regval;
  833. if (kstrtol(buf, 10, &val) < 0)
  834. return -EINVAL;
  835. mutex_lock(&data->update_lock);
  836. switch (index) {
  837. default:
  838. case 1:
  839. reg = IT87_REG_TEMP_LOW(nr);
  840. break;
  841. case 2:
  842. reg = IT87_REG_TEMP_HIGH(nr);
  843. break;
  844. case 3:
  845. regval = it87_read_value(data, IT87_REG_BEEP_ENABLE);
  846. if (!(regval & 0x80)) {
  847. regval |= 0x80;
  848. it87_write_value(data, IT87_REG_BEEP_ENABLE, regval);
  849. }
  850. data->valid = 0;
  851. reg = IT87_REG_TEMP_OFFSET[nr];
  852. break;
  853. }
  854. data->temp[nr][index] = TEMP_TO_REG(val);
  855. it87_write_value(data, reg, data->temp[nr][index]);
  856. mutex_unlock(&data->update_lock);
  857. return count;
  858. }
  859. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0);
  860. static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  861. 0, 1);
  862. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  863. 0, 2);
  864. static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
  865. set_temp, 0, 3);
  866. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 1, 0);
  867. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  868. 1, 1);
  869. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  870. 1, 2);
  871. static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
  872. set_temp, 1, 3);
  873. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 2, 0);
  874. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
  875. 2, 1);
  876. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
  877. 2, 2);
  878. static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
  879. set_temp, 2, 3);
  880. static SENSOR_DEVICE_ATTR_2(temp4_input, S_IRUGO, show_temp, NULL, 3, 0);
  881. static SENSOR_DEVICE_ATTR_2(temp5_input, S_IRUGO, show_temp, NULL, 4, 0);
  882. static SENSOR_DEVICE_ATTR_2(temp6_input, S_IRUGO, show_temp, NULL, 5, 0);
  883. static ssize_t show_temp_type(struct device *dev, struct device_attribute *attr,
  884. char *buf)
  885. {
  886. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  887. int nr = sensor_attr->index;
  888. struct it87_data *data = it87_update_device(dev);
  889. u8 reg = data->sensor; /* In case value is updated while used */
  890. u8 extra = data->extra;
  891. if ((has_temp_peci(data, nr) && (reg >> 6 == nr + 1)) ||
  892. (has_temp_old_peci(data, nr) && (extra & 0x80)))
  893. return sprintf(buf, "6\n"); /* Intel PECI */
  894. if (reg & (1 << nr))
  895. return sprintf(buf, "3\n"); /* thermal diode */
  896. if (reg & (8 << nr))
  897. return sprintf(buf, "4\n"); /* thermistor */
  898. return sprintf(buf, "0\n"); /* disabled */
  899. }
  900. static ssize_t set_temp_type(struct device *dev, struct device_attribute *attr,
  901. const char *buf, size_t count)
  902. {
  903. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  904. int nr = sensor_attr->index;
  905. struct it87_data *data = dev_get_drvdata(dev);
  906. long val;
  907. u8 reg, extra;
  908. if (kstrtol(buf, 10, &val) < 0)
  909. return -EINVAL;
  910. reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
  911. reg &= ~(1 << nr);
  912. reg &= ~(8 << nr);
  913. if (has_temp_peci(data, nr) && (reg >> 6 == nr + 1 || val == 6))
  914. reg &= 0x3f;
  915. extra = it87_read_value(data, IT87_REG_TEMP_EXTRA);
  916. if (has_temp_old_peci(data, nr) && ((extra & 0x80) || val == 6))
  917. extra &= 0x7f;
  918. if (val == 2) { /* backwards compatibility */
  919. dev_warn(dev,
  920. "Sensor type 2 is deprecated, please use 4 instead\n");
  921. val = 4;
  922. }
  923. /* 3 = thermal diode; 4 = thermistor; 6 = Intel PECI; 0 = disabled */
  924. if (val == 3)
  925. reg |= 1 << nr;
  926. else if (val == 4)
  927. reg |= 8 << nr;
  928. else if (has_temp_peci(data, nr) && val == 6)
  929. reg |= (nr + 1) << 6;
  930. else if (has_temp_old_peci(data, nr) && val == 6)
  931. extra |= 0x80;
  932. else if (val != 0)
  933. return -EINVAL;
  934. mutex_lock(&data->update_lock);
  935. data->sensor = reg;
  936. data->extra = extra;
  937. it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
  938. if (has_temp_old_peci(data, nr))
  939. it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
  940. data->valid = 0; /* Force cache refresh */
  941. mutex_unlock(&data->update_lock);
  942. return count;
  943. }
  944. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO | S_IWUSR, show_temp_type,
  945. set_temp_type, 0);
  946. static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO | S_IWUSR, show_temp_type,
  947. set_temp_type, 1);
  948. static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO | S_IWUSR, show_temp_type,
  949. set_temp_type, 2);
  950. /* 6 Fans */
  951. static int pwm_mode(const struct it87_data *data, int nr)
  952. {
  953. if (data->type != it8603 && nr < 3 && !(data->fan_main_ctrl & BIT(nr)))
  954. return 0; /* Full speed */
  955. if (data->pwm_ctrl[nr] & 0x80)
  956. return 2; /* Automatic mode */
  957. if ((data->type == it8603 || nr >= 3) &&
  958. data->pwm_duty[nr] == pwm_to_reg(data, 0xff))
  959. return 0; /* Full speed */
  960. return 1; /* Manual mode */
  961. }
  962. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  963. char *buf)
  964. {
  965. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  966. int nr = sattr->nr;
  967. int index = sattr->index;
  968. int speed;
  969. struct it87_data *data = it87_update_device(dev);
  970. speed = has_16bit_fans(data) ?
  971. FAN16_FROM_REG(data->fan[nr][index]) :
  972. FAN_FROM_REG(data->fan[nr][index],
  973. DIV_FROM_REG(data->fan_div[nr]));
  974. return sprintf(buf, "%d\n", speed);
  975. }
  976. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  977. char *buf)
  978. {
  979. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  980. struct it87_data *data = it87_update_device(dev);
  981. int nr = sensor_attr->index;
  982. return sprintf(buf, "%lu\n", DIV_FROM_REG(data->fan_div[nr]));
  983. }
  984. static ssize_t show_pwm_enable(struct device *dev,
  985. struct device_attribute *attr, char *buf)
  986. {
  987. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  988. struct it87_data *data = it87_update_device(dev);
  989. int nr = sensor_attr->index;
  990. return sprintf(buf, "%d\n", pwm_mode(data, nr));
  991. }
  992. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  993. char *buf)
  994. {
  995. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  996. struct it87_data *data = it87_update_device(dev);
  997. int nr = sensor_attr->index;
  998. return sprintf(buf, "%d\n",
  999. pwm_from_reg(data, data->pwm_duty[nr]));
  1000. }
  1001. static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
  1002. char *buf)
  1003. {
  1004. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1005. struct it87_data *data = it87_update_device(dev);
  1006. int nr = sensor_attr->index;
  1007. unsigned int freq;
  1008. int index;
  1009. if (has_pwm_freq2(data) && nr == 1)
  1010. index = (data->extra >> 4) & 0x07;
  1011. else
  1012. index = (data->fan_ctl >> 4) & 0x07;
  1013. freq = pwm_freq[index] / (has_newer_autopwm(data) ? 256 : 128);
  1014. return sprintf(buf, "%u\n", freq);
  1015. }
  1016. static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
  1017. const char *buf, size_t count)
  1018. {
  1019. struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
  1020. int nr = sattr->nr;
  1021. int index = sattr->index;
  1022. struct it87_data *data = dev_get_drvdata(dev);
  1023. long val;
  1024. u8 reg;
  1025. if (kstrtol(buf, 10, &val) < 0)
  1026. return -EINVAL;
  1027. mutex_lock(&data->update_lock);
  1028. if (has_16bit_fans(data)) {
  1029. data->fan[nr][index] = FAN16_TO_REG(val);
  1030. it87_write_value(data, IT87_REG_FAN_MIN[nr],
  1031. data->fan[nr][index] & 0xff);
  1032. it87_write_value(data, IT87_REG_FANX_MIN[nr],
  1033. data->fan[nr][index] >> 8);
  1034. } else {
  1035. reg = it87_read_value(data, IT87_REG_FAN_DIV);
  1036. switch (nr) {
  1037. case 0:
  1038. data->fan_div[nr] = reg & 0x07;
  1039. break;
  1040. case 1:
  1041. data->fan_div[nr] = (reg >> 3) & 0x07;
  1042. break;
  1043. case 2:
  1044. data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
  1045. break;
  1046. }
  1047. data->fan[nr][index] =
  1048. FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  1049. it87_write_value(data, IT87_REG_FAN_MIN[nr],
  1050. data->fan[nr][index]);
  1051. }
  1052. mutex_unlock(&data->update_lock);
  1053. return count;
  1054. }
  1055. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  1056. const char *buf, size_t count)
  1057. {
  1058. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1059. struct it87_data *data = dev_get_drvdata(dev);
  1060. int nr = sensor_attr->index;
  1061. unsigned long val;
  1062. int min;
  1063. u8 old;
  1064. if (kstrtoul(buf, 10, &val) < 0)
  1065. return -EINVAL;
  1066. mutex_lock(&data->update_lock);
  1067. old = it87_read_value(data, IT87_REG_FAN_DIV);
  1068. /* Save fan min limit */
  1069. min = FAN_FROM_REG(data->fan[nr][1], DIV_FROM_REG(data->fan_div[nr]));
  1070. switch (nr) {
  1071. case 0:
  1072. case 1:
  1073. data->fan_div[nr] = DIV_TO_REG(val);
  1074. break;
  1075. case 2:
  1076. if (val < 8)
  1077. data->fan_div[nr] = 1;
  1078. else
  1079. data->fan_div[nr] = 3;
  1080. }
  1081. val = old & 0x80;
  1082. val |= (data->fan_div[0] & 0x07);
  1083. val |= (data->fan_div[1] & 0x07) << 3;
  1084. if (data->fan_div[2] == 3)
  1085. val |= 0x1 << 6;
  1086. it87_write_value(data, IT87_REG_FAN_DIV, val);
  1087. /* Restore fan min limit */
  1088. data->fan[nr][1] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  1089. it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan[nr][1]);
  1090. mutex_unlock(&data->update_lock);
  1091. return count;
  1092. }
  1093. /* Returns 0 if OK, -EINVAL otherwise */
  1094. static int check_trip_points(struct device *dev, int nr)
  1095. {
  1096. const struct it87_data *data = dev_get_drvdata(dev);
  1097. int i, err = 0;
  1098. if (has_old_autopwm(data)) {
  1099. for (i = 0; i < 3; i++) {
  1100. if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
  1101. err = -EINVAL;
  1102. }
  1103. for (i = 0; i < 2; i++) {
  1104. if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
  1105. err = -EINVAL;
  1106. }
  1107. } else if (has_newer_autopwm(data)) {
  1108. for (i = 1; i < 3; i++) {
  1109. if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
  1110. err = -EINVAL;
  1111. }
  1112. }
  1113. if (err) {
  1114. dev_err(dev,
  1115. "Inconsistent trip points, not switching to automatic mode\n");
  1116. dev_err(dev, "Adjust the trip points and try again\n");
  1117. }
  1118. return err;
  1119. }
  1120. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  1121. const char *buf, size_t count)
  1122. {
  1123. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1124. struct it87_data *data = dev_get_drvdata(dev);
  1125. int nr = sensor_attr->index;
  1126. long val;
  1127. if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
  1128. return -EINVAL;
  1129. /* Check trip points before switching to automatic mode */
  1130. if (val == 2) {
  1131. if (check_trip_points(dev, nr) < 0)
  1132. return -EINVAL;
  1133. }
  1134. mutex_lock(&data->update_lock);
  1135. if (val == 0) {
  1136. if (nr < 3 && data->type != it8603) {
  1137. int tmp;
  1138. /* make sure the fan is on when in on/off mode */
  1139. tmp = it87_read_value(data, IT87_REG_FAN_CTL);
  1140. it87_write_value(data, IT87_REG_FAN_CTL, tmp | BIT(nr));
  1141. /* set on/off mode */
  1142. data->fan_main_ctrl &= ~BIT(nr);
  1143. it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
  1144. data->fan_main_ctrl);
  1145. } else {
  1146. u8 ctrl;
  1147. /* No on/off mode, set maximum pwm value */
  1148. data->pwm_duty[nr] = pwm_to_reg(data, 0xff);
  1149. it87_write_value(data, IT87_REG_PWM_DUTY[nr],
  1150. data->pwm_duty[nr]);
  1151. /* and set manual mode */
  1152. if (has_newer_autopwm(data)) {
  1153. ctrl = (data->pwm_ctrl[nr] & 0x7c) |
  1154. data->pwm_temp_map[nr];
  1155. } else {
  1156. ctrl = data->pwm_duty[nr];
  1157. }
  1158. data->pwm_ctrl[nr] = ctrl;
  1159. it87_write_value(data, IT87_REG_PWM[nr], ctrl);
  1160. }
  1161. } else {
  1162. u8 ctrl;
  1163. if (has_newer_autopwm(data)) {
  1164. ctrl = (data->pwm_ctrl[nr] & 0x7c) |
  1165. data->pwm_temp_map[nr];
  1166. if (val != 1)
  1167. ctrl |= 0x80;
  1168. } else {
  1169. ctrl = (val == 1 ? data->pwm_duty[nr] : 0x80);
  1170. }
  1171. data->pwm_ctrl[nr] = ctrl;
  1172. it87_write_value(data, IT87_REG_PWM[nr], ctrl);
  1173. if (data->type != it8603 && nr < 3) {
  1174. /* set SmartGuardian mode */
  1175. data->fan_main_ctrl |= BIT(nr);
  1176. it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
  1177. data->fan_main_ctrl);
  1178. }
  1179. }
  1180. mutex_unlock(&data->update_lock);
  1181. return count;
  1182. }
  1183. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  1184. const char *buf, size_t count)
  1185. {
  1186. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1187. struct it87_data *data = dev_get_drvdata(dev);
  1188. int nr = sensor_attr->index;
  1189. long val;
  1190. if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
  1191. return -EINVAL;
  1192. mutex_lock(&data->update_lock);
  1193. it87_update_pwm_ctrl(data, nr);
  1194. if (has_newer_autopwm(data)) {
  1195. /*
  1196. * If we are in automatic mode, the PWM duty cycle register
  1197. * is read-only so we can't write the value.
  1198. */
  1199. if (data->pwm_ctrl[nr] & 0x80) {
  1200. mutex_unlock(&data->update_lock);
  1201. return -EBUSY;
  1202. }
  1203. data->pwm_duty[nr] = pwm_to_reg(data, val);
  1204. it87_write_value(data, IT87_REG_PWM_DUTY[nr],
  1205. data->pwm_duty[nr]);
  1206. } else {
  1207. data->pwm_duty[nr] = pwm_to_reg(data, val);
  1208. /*
  1209. * If we are in manual mode, write the duty cycle immediately;
  1210. * otherwise, just store it for later use.
  1211. */
  1212. if (!(data->pwm_ctrl[nr] & 0x80)) {
  1213. data->pwm_ctrl[nr] = data->pwm_duty[nr];
  1214. it87_write_value(data, IT87_REG_PWM[nr],
  1215. data->pwm_ctrl[nr]);
  1216. }
  1217. }
  1218. mutex_unlock(&data->update_lock);
  1219. return count;
  1220. }
  1221. static ssize_t set_pwm_freq(struct device *dev, struct device_attribute *attr,
  1222. const char *buf, size_t count)
  1223. {
  1224. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1225. struct it87_data *data = dev_get_drvdata(dev);
  1226. int nr = sensor_attr->index;
  1227. unsigned long val;
  1228. int i;
  1229. if (kstrtoul(buf, 10, &val) < 0)
  1230. return -EINVAL;
  1231. val = clamp_val(val, 0, 1000000);
  1232. val *= has_newer_autopwm(data) ? 256 : 128;
  1233. /* Search for the nearest available frequency */
  1234. for (i = 0; i < 7; i++) {
  1235. if (val > (pwm_freq[i] + pwm_freq[i + 1]) / 2)
  1236. break;
  1237. }
  1238. mutex_lock(&data->update_lock);
  1239. if (nr == 0) {
  1240. data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
  1241. data->fan_ctl |= i << 4;
  1242. it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
  1243. } else {
  1244. data->extra = it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x8f;
  1245. data->extra |= i << 4;
  1246. it87_write_value(data, IT87_REG_TEMP_EXTRA, data->extra);
  1247. }
  1248. mutex_unlock(&data->update_lock);
  1249. return count;
  1250. }
  1251. static ssize_t show_pwm_temp_map(struct device *dev,
  1252. struct device_attribute *attr, char *buf)
  1253. {
  1254. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1255. struct it87_data *data = it87_update_device(dev);
  1256. int nr = sensor_attr->index;
  1257. int map;
  1258. map = data->pwm_temp_map[nr];
  1259. if (map >= 3)
  1260. map = 0; /* Should never happen */
  1261. if (nr >= 3) /* pwm channels 3..6 map to temp4..6 */
  1262. map += 3;
  1263. return sprintf(buf, "%d\n", (int)BIT(map));
  1264. }
  1265. static ssize_t set_pwm_temp_map(struct device *dev,
  1266. struct device_attribute *attr, const char *buf,
  1267. size_t count)
  1268. {
  1269. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1270. struct it87_data *data = dev_get_drvdata(dev);
  1271. int nr = sensor_attr->index;
  1272. long val;
  1273. u8 reg;
  1274. if (kstrtol(buf, 10, &val) < 0)
  1275. return -EINVAL;
  1276. if (nr >= 3)
  1277. val -= 3;
  1278. switch (val) {
  1279. case BIT(0):
  1280. reg = 0x00;
  1281. break;
  1282. case BIT(1):
  1283. reg = 0x01;
  1284. break;
  1285. case BIT(2):
  1286. reg = 0x02;
  1287. break;
  1288. default:
  1289. return -EINVAL;
  1290. }
  1291. mutex_lock(&data->update_lock);
  1292. it87_update_pwm_ctrl(data, nr);
  1293. data->pwm_temp_map[nr] = reg;
  1294. /*
  1295. * If we are in automatic mode, write the temp mapping immediately;
  1296. * otherwise, just store it for later use.
  1297. */
  1298. if (data->pwm_ctrl[nr] & 0x80) {
  1299. data->pwm_ctrl[nr] = (data->pwm_ctrl[nr] & 0xfc) |
  1300. data->pwm_temp_map[nr];
  1301. it87_write_value(data, IT87_REG_PWM[nr], data->pwm_ctrl[nr]);
  1302. }
  1303. mutex_unlock(&data->update_lock);
  1304. return count;
  1305. }
  1306. static ssize_t show_auto_pwm(struct device *dev, struct device_attribute *attr,
  1307. char *buf)
  1308. {
  1309. struct it87_data *data = it87_update_device(dev);
  1310. struct sensor_device_attribute_2 *sensor_attr =
  1311. to_sensor_dev_attr_2(attr);
  1312. int nr = sensor_attr->nr;
  1313. int point = sensor_attr->index;
  1314. return sprintf(buf, "%d\n",
  1315. pwm_from_reg(data, data->auto_pwm[nr][point]));
  1316. }
  1317. static ssize_t set_auto_pwm(struct device *dev, struct device_attribute *attr,
  1318. const char *buf, size_t count)
  1319. {
  1320. struct it87_data *data = dev_get_drvdata(dev);
  1321. struct sensor_device_attribute_2 *sensor_attr =
  1322. to_sensor_dev_attr_2(attr);
  1323. int nr = sensor_attr->nr;
  1324. int point = sensor_attr->index;
  1325. int regaddr;
  1326. long val;
  1327. if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
  1328. return -EINVAL;
  1329. mutex_lock(&data->update_lock);
  1330. data->auto_pwm[nr][point] = pwm_to_reg(data, val);
  1331. if (has_newer_autopwm(data))
  1332. regaddr = IT87_REG_AUTO_TEMP(nr, 3);
  1333. else
  1334. regaddr = IT87_REG_AUTO_PWM(nr, point);
  1335. it87_write_value(data, regaddr, data->auto_pwm[nr][point]);
  1336. mutex_unlock(&data->update_lock);
  1337. return count;
  1338. }
  1339. static ssize_t show_auto_pwm_slope(struct device *dev,
  1340. struct device_attribute *attr, char *buf)
  1341. {
  1342. struct it87_data *data = it87_update_device(dev);
  1343. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1344. int nr = sensor_attr->index;
  1345. return sprintf(buf, "%d\n", data->auto_pwm[nr][1] & 0x7f);
  1346. }
  1347. static ssize_t set_auto_pwm_slope(struct device *dev,
  1348. struct device_attribute *attr,
  1349. const char *buf, size_t count)
  1350. {
  1351. struct it87_data *data = dev_get_drvdata(dev);
  1352. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1353. int nr = sensor_attr->index;
  1354. unsigned long val;
  1355. if (kstrtoul(buf, 10, &val) < 0 || val > 127)
  1356. return -EINVAL;
  1357. mutex_lock(&data->update_lock);
  1358. data->auto_pwm[nr][1] = (data->auto_pwm[nr][1] & 0x80) | val;
  1359. it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 4),
  1360. data->auto_pwm[nr][1]);
  1361. mutex_unlock(&data->update_lock);
  1362. return count;
  1363. }
  1364. static ssize_t show_auto_temp(struct device *dev, struct device_attribute *attr,
  1365. char *buf)
  1366. {
  1367. struct it87_data *data = it87_update_device(dev);
  1368. struct sensor_device_attribute_2 *sensor_attr =
  1369. to_sensor_dev_attr_2(attr);
  1370. int nr = sensor_attr->nr;
  1371. int point = sensor_attr->index;
  1372. int reg;
  1373. if (has_old_autopwm(data) || point)
  1374. reg = data->auto_temp[nr][point];
  1375. else
  1376. reg = data->auto_temp[nr][1] - (data->auto_temp[nr][0] & 0x1f);
  1377. return sprintf(buf, "%d\n", TEMP_FROM_REG(reg));
  1378. }
  1379. static ssize_t set_auto_temp(struct device *dev, struct device_attribute *attr,
  1380. const char *buf, size_t count)
  1381. {
  1382. struct it87_data *data = dev_get_drvdata(dev);
  1383. struct sensor_device_attribute_2 *sensor_attr =
  1384. to_sensor_dev_attr_2(attr);
  1385. int nr = sensor_attr->nr;
  1386. int point = sensor_attr->index;
  1387. long val;
  1388. int reg;
  1389. if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
  1390. return -EINVAL;
  1391. mutex_lock(&data->update_lock);
  1392. if (has_newer_autopwm(data) && !point) {
  1393. reg = data->auto_temp[nr][1] - TEMP_TO_REG(val);
  1394. reg = clamp_val(reg, 0, 0x1f) | (data->auto_temp[nr][0] & 0xe0);
  1395. data->auto_temp[nr][0] = reg;
  1396. it87_write_value(data, IT87_REG_AUTO_TEMP(nr, 5), reg);
  1397. } else {
  1398. reg = TEMP_TO_REG(val);
  1399. data->auto_temp[nr][point] = reg;
  1400. if (has_newer_autopwm(data))
  1401. point--;
  1402. it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point), reg);
  1403. }
  1404. mutex_unlock(&data->update_lock);
  1405. return count;
  1406. }
  1407. static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0);
  1408. static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  1409. 0, 1);
  1410. static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div,
  1411. set_fan_div, 0);
  1412. static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 1, 0);
  1413. static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  1414. 1, 1);
  1415. static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, show_fan_div,
  1416. set_fan_div, 1);
  1417. static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 2, 0);
  1418. static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  1419. 2, 1);
  1420. static SENSOR_DEVICE_ATTR(fan3_div, S_IRUGO | S_IWUSR, show_fan_div,
  1421. set_fan_div, 2);
  1422. static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 3, 0);
  1423. static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  1424. 3, 1);
  1425. static SENSOR_DEVICE_ATTR_2(fan5_input, S_IRUGO, show_fan, NULL, 4, 0);
  1426. static SENSOR_DEVICE_ATTR_2(fan5_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  1427. 4, 1);
  1428. static SENSOR_DEVICE_ATTR_2(fan6_input, S_IRUGO, show_fan, NULL, 5, 0);
  1429. static SENSOR_DEVICE_ATTR_2(fan6_min, S_IRUGO | S_IWUSR, show_fan, set_fan,
  1430. 5, 1);
  1431. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  1432. show_pwm_enable, set_pwm_enable, 0);
  1433. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 0);
  1434. static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR, show_pwm_freq,
  1435. set_pwm_freq, 0);
  1436. static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IRUGO,
  1437. show_pwm_temp_map, set_pwm_temp_map, 0);
  1438. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1439. show_auto_pwm, set_auto_pwm, 0, 0);
  1440. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR,
  1441. show_auto_pwm, set_auto_pwm, 0, 1);
  1442. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO | S_IWUSR,
  1443. show_auto_pwm, set_auto_pwm, 0, 2);
  1444. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO,
  1445. show_auto_pwm, NULL, 0, 3);
  1446. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp, S_IRUGO | S_IWUSR,
  1447. show_auto_temp, set_auto_temp, 0, 1);
  1448. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  1449. show_auto_temp, set_auto_temp, 0, 0);
  1450. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_temp, S_IRUGO | S_IWUSR,
  1451. show_auto_temp, set_auto_temp, 0, 2);
  1452. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point3_temp, S_IRUGO | S_IWUSR,
  1453. show_auto_temp, set_auto_temp, 0, 3);
  1454. static SENSOR_DEVICE_ATTR_2(pwm1_auto_point4_temp, S_IRUGO | S_IWUSR,
  1455. show_auto_temp, set_auto_temp, 0, 4);
  1456. static SENSOR_DEVICE_ATTR_2(pwm1_auto_start, S_IRUGO | S_IWUSR,
  1457. show_auto_pwm, set_auto_pwm, 0, 0);
  1458. static SENSOR_DEVICE_ATTR(pwm1_auto_slope, S_IRUGO | S_IWUSR,
  1459. show_auto_pwm_slope, set_auto_pwm_slope, 0);
  1460. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
  1461. show_pwm_enable, set_pwm_enable, 1);
  1462. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 1);
  1463. static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO, show_pwm_freq, set_pwm_freq, 1);
  1464. static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IRUGO,
  1465. show_pwm_temp_map, set_pwm_temp_map, 1);
  1466. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1467. show_auto_pwm, set_auto_pwm, 1, 0);
  1468. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR,
  1469. show_auto_pwm, set_auto_pwm, 1, 1);
  1470. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO | S_IWUSR,
  1471. show_auto_pwm, set_auto_pwm, 1, 2);
  1472. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO,
  1473. show_auto_pwm, NULL, 1, 3);
  1474. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp, S_IRUGO | S_IWUSR,
  1475. show_auto_temp, set_auto_temp, 1, 1);
  1476. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  1477. show_auto_temp, set_auto_temp, 1, 0);
  1478. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_temp, S_IRUGO | S_IWUSR,
  1479. show_auto_temp, set_auto_temp, 1, 2);
  1480. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point3_temp, S_IRUGO | S_IWUSR,
  1481. show_auto_temp, set_auto_temp, 1, 3);
  1482. static SENSOR_DEVICE_ATTR_2(pwm2_auto_point4_temp, S_IRUGO | S_IWUSR,
  1483. show_auto_temp, set_auto_temp, 1, 4);
  1484. static SENSOR_DEVICE_ATTR_2(pwm2_auto_start, S_IRUGO | S_IWUSR,
  1485. show_auto_pwm, set_auto_pwm, 1, 0);
  1486. static SENSOR_DEVICE_ATTR(pwm2_auto_slope, S_IRUGO | S_IWUSR,
  1487. show_auto_pwm_slope, set_auto_pwm_slope, 1);
  1488. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
  1489. show_pwm_enable, set_pwm_enable, 2);
  1490. static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 2);
  1491. static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO, show_pwm_freq, NULL, 2);
  1492. static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IRUGO,
  1493. show_pwm_temp_map, set_pwm_temp_map, 2);
  1494. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR,
  1495. show_auto_pwm, set_auto_pwm, 2, 0);
  1496. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR,
  1497. show_auto_pwm, set_auto_pwm, 2, 1);
  1498. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO | S_IWUSR,
  1499. show_auto_pwm, set_auto_pwm, 2, 2);
  1500. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO,
  1501. show_auto_pwm, NULL, 2, 3);
  1502. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp, S_IRUGO | S_IWUSR,
  1503. show_auto_temp, set_auto_temp, 2, 1);
  1504. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  1505. show_auto_temp, set_auto_temp, 2, 0);
  1506. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_temp, S_IRUGO | S_IWUSR,
  1507. show_auto_temp, set_auto_temp, 2, 2);
  1508. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point3_temp, S_IRUGO | S_IWUSR,
  1509. show_auto_temp, set_auto_temp, 2, 3);
  1510. static SENSOR_DEVICE_ATTR_2(pwm3_auto_point4_temp, S_IRUGO | S_IWUSR,
  1511. show_auto_temp, set_auto_temp, 2, 4);
  1512. static SENSOR_DEVICE_ATTR_2(pwm3_auto_start, S_IRUGO | S_IWUSR,
  1513. show_auto_pwm, set_auto_pwm, 2, 0);
  1514. static SENSOR_DEVICE_ATTR(pwm3_auto_slope, S_IRUGO | S_IWUSR,
  1515. show_auto_pwm_slope, set_auto_pwm_slope, 2);
  1516. static SENSOR_DEVICE_ATTR(pwm4_enable, S_IRUGO | S_IWUSR,
  1517. show_pwm_enable, set_pwm_enable, 3);
  1518. static SENSOR_DEVICE_ATTR(pwm4, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 3);
  1519. static SENSOR_DEVICE_ATTR(pwm4_freq, S_IRUGO, show_pwm_freq, NULL, 3);
  1520. static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IRUGO,
  1521. show_pwm_temp_map, set_pwm_temp_map, 3);
  1522. static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp, S_IRUGO | S_IWUSR,
  1523. show_auto_temp, set_auto_temp, 2, 1);
  1524. static SENSOR_DEVICE_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  1525. show_auto_temp, set_auto_temp, 2, 0);
  1526. static SENSOR_DEVICE_ATTR_2(pwm4_auto_point2_temp, S_IRUGO | S_IWUSR,
  1527. show_auto_temp, set_auto_temp, 2, 2);
  1528. static SENSOR_DEVICE_ATTR_2(pwm4_auto_point3_temp, S_IRUGO | S_IWUSR,
  1529. show_auto_temp, set_auto_temp, 2, 3);
  1530. static SENSOR_DEVICE_ATTR_2(pwm4_auto_start, S_IRUGO | S_IWUSR,
  1531. show_auto_pwm, set_auto_pwm, 3, 0);
  1532. static SENSOR_DEVICE_ATTR(pwm4_auto_slope, S_IRUGO | S_IWUSR,
  1533. show_auto_pwm_slope, set_auto_pwm_slope, 3);
  1534. static SENSOR_DEVICE_ATTR(pwm5_enable, S_IRUGO | S_IWUSR,
  1535. show_pwm_enable, set_pwm_enable, 4);
  1536. static SENSOR_DEVICE_ATTR(pwm5, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 4);
  1537. static SENSOR_DEVICE_ATTR(pwm5_freq, S_IRUGO, show_pwm_freq, NULL, 4);
  1538. static SENSOR_DEVICE_ATTR(pwm5_auto_channels_temp, S_IRUGO,
  1539. show_pwm_temp_map, set_pwm_temp_map, 4);
  1540. static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp, S_IRUGO | S_IWUSR,
  1541. show_auto_temp, set_auto_temp, 2, 1);
  1542. static SENSOR_DEVICE_ATTR_2(pwm5_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  1543. show_auto_temp, set_auto_temp, 2, 0);
  1544. static SENSOR_DEVICE_ATTR_2(pwm5_auto_point2_temp, S_IRUGO | S_IWUSR,
  1545. show_auto_temp, set_auto_temp, 2, 2);
  1546. static SENSOR_DEVICE_ATTR_2(pwm5_auto_point3_temp, S_IRUGO | S_IWUSR,
  1547. show_auto_temp, set_auto_temp, 2, 3);
  1548. static SENSOR_DEVICE_ATTR_2(pwm5_auto_start, S_IRUGO | S_IWUSR,
  1549. show_auto_pwm, set_auto_pwm, 4, 0);
  1550. static SENSOR_DEVICE_ATTR(pwm5_auto_slope, S_IRUGO | S_IWUSR,
  1551. show_auto_pwm_slope, set_auto_pwm_slope, 4);
  1552. static SENSOR_DEVICE_ATTR(pwm6_enable, S_IRUGO | S_IWUSR,
  1553. show_pwm_enable, set_pwm_enable, 5);
  1554. static SENSOR_DEVICE_ATTR(pwm6, S_IRUGO | S_IWUSR, show_pwm, set_pwm, 5);
  1555. static SENSOR_DEVICE_ATTR(pwm6_freq, S_IRUGO, show_pwm_freq, NULL, 5);
  1556. static SENSOR_DEVICE_ATTR(pwm6_auto_channels_temp, S_IRUGO,
  1557. show_pwm_temp_map, set_pwm_temp_map, 5);
  1558. static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp, S_IRUGO | S_IWUSR,
  1559. show_auto_temp, set_auto_temp, 2, 1);
  1560. static SENSOR_DEVICE_ATTR_2(pwm6_auto_point1_temp_hyst, S_IRUGO | S_IWUSR,
  1561. show_auto_temp, set_auto_temp, 2, 0);
  1562. static SENSOR_DEVICE_ATTR_2(pwm6_auto_point2_temp, S_IRUGO | S_IWUSR,
  1563. show_auto_temp, set_auto_temp, 2, 2);
  1564. static SENSOR_DEVICE_ATTR_2(pwm6_auto_point3_temp, S_IRUGO | S_IWUSR,
  1565. show_auto_temp, set_auto_temp, 2, 3);
  1566. static SENSOR_DEVICE_ATTR_2(pwm6_auto_start, S_IRUGO | S_IWUSR,
  1567. show_auto_pwm, set_auto_pwm, 5, 0);
  1568. static SENSOR_DEVICE_ATTR(pwm6_auto_slope, S_IRUGO | S_IWUSR,
  1569. show_auto_pwm_slope, set_auto_pwm_slope, 5);
  1570. /* Alarms */
  1571. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  1572. char *buf)
  1573. {
  1574. struct it87_data *data = it87_update_device(dev);
  1575. return sprintf(buf, "%u\n", data->alarms);
  1576. }
  1577. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  1578. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  1579. char *buf)
  1580. {
  1581. struct it87_data *data = it87_update_device(dev);
  1582. int bitnr = to_sensor_dev_attr(attr)->index;
  1583. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  1584. }
  1585. static ssize_t clear_intrusion(struct device *dev,
  1586. struct device_attribute *attr, const char *buf,
  1587. size_t count)
  1588. {
  1589. struct it87_data *data = dev_get_drvdata(dev);
  1590. int config;
  1591. long val;
  1592. if (kstrtol(buf, 10, &val) < 0 || val != 0)
  1593. return -EINVAL;
  1594. mutex_lock(&data->update_lock);
  1595. config = it87_read_value(data, IT87_REG_CONFIG);
  1596. if (config < 0) {
  1597. count = config;
  1598. } else {
  1599. config |= BIT(5);
  1600. it87_write_value(data, IT87_REG_CONFIG, config);
  1601. /* Invalidate cache to force re-read */
  1602. data->valid = 0;
  1603. }
  1604. mutex_unlock(&data->update_lock);
  1605. return count;
  1606. }
  1607. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
  1608. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
  1609. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
  1610. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
  1611. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
  1612. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
  1613. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
  1614. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
  1615. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
  1616. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
  1617. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
  1618. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
  1619. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
  1620. static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 7);
  1621. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
  1622. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
  1623. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
  1624. static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
  1625. show_alarm, clear_intrusion, 4);
  1626. static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
  1627. char *buf)
  1628. {
  1629. struct it87_data *data = it87_update_device(dev);
  1630. int bitnr = to_sensor_dev_attr(attr)->index;
  1631. return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
  1632. }
  1633. static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
  1634. const char *buf, size_t count)
  1635. {
  1636. int bitnr = to_sensor_dev_attr(attr)->index;
  1637. struct it87_data *data = dev_get_drvdata(dev);
  1638. long val;
  1639. if (kstrtol(buf, 10, &val) < 0 || (val != 0 && val != 1))
  1640. return -EINVAL;
  1641. mutex_lock(&data->update_lock);
  1642. data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
  1643. if (val)
  1644. data->beeps |= BIT(bitnr);
  1645. else
  1646. data->beeps &= ~BIT(bitnr);
  1647. it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
  1648. mutex_unlock(&data->update_lock);
  1649. return count;
  1650. }
  1651. static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
  1652. show_beep, set_beep, 1);
  1653. static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
  1654. static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
  1655. static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
  1656. static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
  1657. static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
  1658. static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
  1659. static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
  1660. /* fanX_beep writability is set later */
  1661. static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
  1662. static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
  1663. static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
  1664. static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
  1665. static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
  1666. static SENSOR_DEVICE_ATTR(fan6_beep, S_IRUGO, show_beep, set_beep, 0);
  1667. static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
  1668. show_beep, set_beep, 2);
  1669. static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
  1670. static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
  1671. static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
  1672. char *buf)
  1673. {
  1674. struct it87_data *data = dev_get_drvdata(dev);
  1675. return sprintf(buf, "%u\n", data->vrm);
  1676. }
  1677. static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
  1678. const char *buf, size_t count)
  1679. {
  1680. struct it87_data *data = dev_get_drvdata(dev);
  1681. unsigned long val;
  1682. if (kstrtoul(buf, 10, &val) < 0)
  1683. return -EINVAL;
  1684. data->vrm = val;
  1685. return count;
  1686. }
  1687. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  1688. static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
  1689. char *buf)
  1690. {
  1691. struct it87_data *data = it87_update_device(dev);
  1692. return sprintf(buf, "%ld\n", (long)vid_from_reg(data->vid, data->vrm));
  1693. }
  1694. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  1695. static ssize_t show_label(struct device *dev, struct device_attribute *attr,
  1696. char *buf)
  1697. {
  1698. static const char * const labels[] = {
  1699. "+5V",
  1700. "5VSB",
  1701. "Vbat",
  1702. };
  1703. static const char * const labels_it8721[] = {
  1704. "+3.3V",
  1705. "3VSB",
  1706. "Vbat",
  1707. };
  1708. struct it87_data *data = dev_get_drvdata(dev);
  1709. int nr = to_sensor_dev_attr(attr)->index;
  1710. const char *label;
  1711. if (has_12mv_adc(data) || has_10_9mv_adc(data))
  1712. label = labels_it8721[nr];
  1713. else
  1714. label = labels[nr];
  1715. return sprintf(buf, "%s\n", label);
  1716. }
  1717. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
  1718. static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
  1719. static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
  1720. /* AVCC3 */
  1721. static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL, 0);
  1722. static umode_t it87_in_is_visible(struct kobject *kobj,
  1723. struct attribute *attr, int index)
  1724. {
  1725. struct device *dev = container_of(kobj, struct device, kobj);
  1726. struct it87_data *data = dev_get_drvdata(dev);
  1727. int i = index / 5; /* voltage index */
  1728. int a = index % 5; /* attribute index */
  1729. if (index >= 40) { /* in8 and higher only have input attributes */
  1730. i = index - 40 + 8;
  1731. a = 0;
  1732. }
  1733. if (!(data->has_in & BIT(i)))
  1734. return 0;
  1735. if (a == 4 && !data->has_beep)
  1736. return 0;
  1737. return attr->mode;
  1738. }
  1739. static struct attribute *it87_attributes_in[] = {
  1740. &sensor_dev_attr_in0_input.dev_attr.attr,
  1741. &sensor_dev_attr_in0_min.dev_attr.attr,
  1742. &sensor_dev_attr_in0_max.dev_attr.attr,
  1743. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1744. &sensor_dev_attr_in0_beep.dev_attr.attr, /* 4 */
  1745. &sensor_dev_attr_in1_input.dev_attr.attr,
  1746. &sensor_dev_attr_in1_min.dev_attr.attr,
  1747. &sensor_dev_attr_in1_max.dev_attr.attr,
  1748. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1749. &sensor_dev_attr_in1_beep.dev_attr.attr, /* 9 */
  1750. &sensor_dev_attr_in2_input.dev_attr.attr,
  1751. &sensor_dev_attr_in2_min.dev_attr.attr,
  1752. &sensor_dev_attr_in2_max.dev_attr.attr,
  1753. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1754. &sensor_dev_attr_in2_beep.dev_attr.attr, /* 14 */
  1755. &sensor_dev_attr_in3_input.dev_attr.attr,
  1756. &sensor_dev_attr_in3_min.dev_attr.attr,
  1757. &sensor_dev_attr_in3_max.dev_attr.attr,
  1758. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1759. &sensor_dev_attr_in3_beep.dev_attr.attr, /* 19 */
  1760. &sensor_dev_attr_in4_input.dev_attr.attr,
  1761. &sensor_dev_attr_in4_min.dev_attr.attr,
  1762. &sensor_dev_attr_in4_max.dev_attr.attr,
  1763. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1764. &sensor_dev_attr_in4_beep.dev_attr.attr, /* 24 */
  1765. &sensor_dev_attr_in5_input.dev_attr.attr,
  1766. &sensor_dev_attr_in5_min.dev_attr.attr,
  1767. &sensor_dev_attr_in5_max.dev_attr.attr,
  1768. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1769. &sensor_dev_attr_in5_beep.dev_attr.attr, /* 29 */
  1770. &sensor_dev_attr_in6_input.dev_attr.attr,
  1771. &sensor_dev_attr_in6_min.dev_attr.attr,
  1772. &sensor_dev_attr_in6_max.dev_attr.attr,
  1773. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1774. &sensor_dev_attr_in6_beep.dev_attr.attr, /* 34 */
  1775. &sensor_dev_attr_in7_input.dev_attr.attr,
  1776. &sensor_dev_attr_in7_min.dev_attr.attr,
  1777. &sensor_dev_attr_in7_max.dev_attr.attr,
  1778. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1779. &sensor_dev_attr_in7_beep.dev_attr.attr, /* 39 */
  1780. &sensor_dev_attr_in8_input.dev_attr.attr, /* 40 */
  1781. &sensor_dev_attr_in9_input.dev_attr.attr,
  1782. &sensor_dev_attr_in10_input.dev_attr.attr,
  1783. &sensor_dev_attr_in11_input.dev_attr.attr,
  1784. &sensor_dev_attr_in12_input.dev_attr.attr,
  1785. NULL
  1786. };
  1787. static const struct attribute_group it87_group_in = {
  1788. .attrs = it87_attributes_in,
  1789. .is_visible = it87_in_is_visible,
  1790. };
  1791. static umode_t it87_temp_is_visible(struct kobject *kobj,
  1792. struct attribute *attr, int index)
  1793. {
  1794. struct device *dev = container_of(kobj, struct device, kobj);
  1795. struct it87_data *data = dev_get_drvdata(dev);
  1796. int i = index / 7; /* temperature index */
  1797. int a = index % 7; /* attribute index */
  1798. if (index >= 21) {
  1799. i = index - 21 + 3;
  1800. a = 0;
  1801. }
  1802. if (!(data->has_temp & BIT(i)))
  1803. return 0;
  1804. if (a == 5 && !has_temp_offset(data))
  1805. return 0;
  1806. if (a == 6 && !data->has_beep)
  1807. return 0;
  1808. return attr->mode;
  1809. }
  1810. static struct attribute *it87_attributes_temp[] = {
  1811. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1812. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1813. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1814. &sensor_dev_attr_temp1_type.dev_attr.attr,
  1815. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1816. &sensor_dev_attr_temp1_offset.dev_attr.attr, /* 5 */
  1817. &sensor_dev_attr_temp1_beep.dev_attr.attr, /* 6 */
  1818. &sensor_dev_attr_temp2_input.dev_attr.attr, /* 7 */
  1819. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1820. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1821. &sensor_dev_attr_temp2_type.dev_attr.attr,
  1822. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1823. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  1824. &sensor_dev_attr_temp2_beep.dev_attr.attr,
  1825. &sensor_dev_attr_temp3_input.dev_attr.attr, /* 14 */
  1826. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1827. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1828. &sensor_dev_attr_temp3_type.dev_attr.attr,
  1829. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1830. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  1831. &sensor_dev_attr_temp3_beep.dev_attr.attr,
  1832. &sensor_dev_attr_temp4_input.dev_attr.attr, /* 21 */
  1833. &sensor_dev_attr_temp5_input.dev_attr.attr,
  1834. &sensor_dev_attr_temp6_input.dev_attr.attr,
  1835. NULL
  1836. };
  1837. static const struct attribute_group it87_group_temp = {
  1838. .attrs = it87_attributes_temp,
  1839. .is_visible = it87_temp_is_visible,
  1840. };
  1841. static umode_t it87_is_visible(struct kobject *kobj,
  1842. struct attribute *attr, int index)
  1843. {
  1844. struct device *dev = container_of(kobj, struct device, kobj);
  1845. struct it87_data *data = dev_get_drvdata(dev);
  1846. if ((index == 2 || index == 3) && !data->has_vid)
  1847. return 0;
  1848. if (index > 3 && !(data->in_internal & BIT(index - 4)))
  1849. return 0;
  1850. return attr->mode;
  1851. }
  1852. static struct attribute *it87_attributes[] = {
  1853. &dev_attr_alarms.attr,
  1854. &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
  1855. &dev_attr_vrm.attr, /* 2 */
  1856. &dev_attr_cpu0_vid.attr, /* 3 */
  1857. &sensor_dev_attr_in3_label.dev_attr.attr, /* 4 .. 7 */
  1858. &sensor_dev_attr_in7_label.dev_attr.attr,
  1859. &sensor_dev_attr_in8_label.dev_attr.attr,
  1860. &sensor_dev_attr_in9_label.dev_attr.attr,
  1861. NULL
  1862. };
  1863. static const struct attribute_group it87_group = {
  1864. .attrs = it87_attributes,
  1865. .is_visible = it87_is_visible,
  1866. };
  1867. static umode_t it87_fan_is_visible(struct kobject *kobj,
  1868. struct attribute *attr, int index)
  1869. {
  1870. struct device *dev = container_of(kobj, struct device, kobj);
  1871. struct it87_data *data = dev_get_drvdata(dev);
  1872. int i = index / 5; /* fan index */
  1873. int a = index % 5; /* attribute index */
  1874. if (index >= 15) { /* fan 4..6 don't have divisor attributes */
  1875. i = (index - 15) / 4 + 3;
  1876. a = (index - 15) % 4;
  1877. }
  1878. if (!(data->has_fan & BIT(i)))
  1879. return 0;
  1880. if (a == 3) { /* beep */
  1881. if (!data->has_beep)
  1882. return 0;
  1883. /* first fan beep attribute is writable */
  1884. if (i == __ffs(data->has_fan))
  1885. return attr->mode | S_IWUSR;
  1886. }
  1887. if (a == 4 && has_16bit_fans(data)) /* divisor */
  1888. return 0;
  1889. return attr->mode;
  1890. }
  1891. static struct attribute *it87_attributes_fan[] = {
  1892. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1893. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1894. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1895. &sensor_dev_attr_fan1_beep.dev_attr.attr, /* 3 */
  1896. &sensor_dev_attr_fan1_div.dev_attr.attr, /* 4 */
  1897. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1898. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1899. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1900. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  1901. &sensor_dev_attr_fan2_div.dev_attr.attr, /* 9 */
  1902. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1903. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1904. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1905. &sensor_dev_attr_fan3_beep.dev_attr.attr,
  1906. &sensor_dev_attr_fan3_div.dev_attr.attr, /* 14 */
  1907. &sensor_dev_attr_fan4_input.dev_attr.attr, /* 15 */
  1908. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1909. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1910. &sensor_dev_attr_fan4_beep.dev_attr.attr,
  1911. &sensor_dev_attr_fan5_input.dev_attr.attr, /* 19 */
  1912. &sensor_dev_attr_fan5_min.dev_attr.attr,
  1913. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1914. &sensor_dev_attr_fan5_beep.dev_attr.attr,
  1915. &sensor_dev_attr_fan6_input.dev_attr.attr, /* 23 */
  1916. &sensor_dev_attr_fan6_min.dev_attr.attr,
  1917. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  1918. &sensor_dev_attr_fan6_beep.dev_attr.attr,
  1919. NULL
  1920. };
  1921. static const struct attribute_group it87_group_fan = {
  1922. .attrs = it87_attributes_fan,
  1923. .is_visible = it87_fan_is_visible,
  1924. };
  1925. static umode_t it87_pwm_is_visible(struct kobject *kobj,
  1926. struct attribute *attr, int index)
  1927. {
  1928. struct device *dev = container_of(kobj, struct device, kobj);
  1929. struct it87_data *data = dev_get_drvdata(dev);
  1930. int i = index / 4; /* pwm index */
  1931. int a = index % 4; /* attribute index */
  1932. if (!(data->has_pwm & BIT(i)))
  1933. return 0;
  1934. /* pwmX_auto_channels_temp is only writable if auto pwm is supported */
  1935. if (a == 3 && (has_old_autopwm(data) || has_newer_autopwm(data)))
  1936. return attr->mode | S_IWUSR;
  1937. /* pwm2_freq is writable if there are two pwm frequency selects */
  1938. if (has_pwm_freq2(data) && i == 1 && a == 2)
  1939. return attr->mode | S_IWUSR;
  1940. return attr->mode;
  1941. }
  1942. static struct attribute *it87_attributes_pwm[] = {
  1943. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1944. &sensor_dev_attr_pwm1.dev_attr.attr,
  1945. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  1946. &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
  1947. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1948. &sensor_dev_attr_pwm2.dev_attr.attr,
  1949. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  1950. &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
  1951. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1952. &sensor_dev_attr_pwm3.dev_attr.attr,
  1953. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  1954. &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
  1955. &sensor_dev_attr_pwm4_enable.dev_attr.attr,
  1956. &sensor_dev_attr_pwm4.dev_attr.attr,
  1957. &sensor_dev_attr_pwm4_freq.dev_attr.attr,
  1958. &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
  1959. &sensor_dev_attr_pwm5_enable.dev_attr.attr,
  1960. &sensor_dev_attr_pwm5.dev_attr.attr,
  1961. &sensor_dev_attr_pwm5_freq.dev_attr.attr,
  1962. &sensor_dev_attr_pwm5_auto_channels_temp.dev_attr.attr,
  1963. &sensor_dev_attr_pwm6_enable.dev_attr.attr,
  1964. &sensor_dev_attr_pwm6.dev_attr.attr,
  1965. &sensor_dev_attr_pwm6_freq.dev_attr.attr,
  1966. &sensor_dev_attr_pwm6_auto_channels_temp.dev_attr.attr,
  1967. NULL
  1968. };
  1969. static const struct attribute_group it87_group_pwm = {
  1970. .attrs = it87_attributes_pwm,
  1971. .is_visible = it87_pwm_is_visible,
  1972. };
  1973. static umode_t it87_auto_pwm_is_visible(struct kobject *kobj,
  1974. struct attribute *attr, int index)
  1975. {
  1976. struct device *dev = container_of(kobj, struct device, kobj);
  1977. struct it87_data *data = dev_get_drvdata(dev);
  1978. int i = index / 11; /* pwm index */
  1979. int a = index % 11; /* attribute index */
  1980. if (index >= 33) { /* pwm 4..6 */
  1981. i = (index - 33) / 6 + 3;
  1982. a = (index - 33) % 6 + 4;
  1983. }
  1984. if (!(data->has_pwm & BIT(i)))
  1985. return 0;
  1986. if (has_newer_autopwm(data)) {
  1987. if (a < 4) /* no auto point pwm */
  1988. return 0;
  1989. if (a == 8) /* no auto_point4 */
  1990. return 0;
  1991. }
  1992. if (has_old_autopwm(data)) {
  1993. if (a >= 9) /* no pwm_auto_start, pwm_auto_slope */
  1994. return 0;
  1995. }
  1996. return attr->mode;
  1997. }
  1998. static struct attribute *it87_attributes_auto_pwm[] = {
  1999. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  2000. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  2001. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  2002. &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
  2003. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  2004. &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
  2005. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  2006. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  2007. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  2008. &sensor_dev_attr_pwm1_auto_start.dev_attr.attr,
  2009. &sensor_dev_attr_pwm1_auto_slope.dev_attr.attr,
  2010. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr, /* 11 */
  2011. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  2012. &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
  2013. &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
  2014. &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
  2015. &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
  2016. &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
  2017. &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
  2018. &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
  2019. &sensor_dev_attr_pwm2_auto_start.dev_attr.attr,
  2020. &sensor_dev_attr_pwm2_auto_slope.dev_attr.attr,
  2021. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr, /* 22 */
  2022. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  2023. &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
  2024. &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
  2025. &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
  2026. &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
  2027. &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
  2028. &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
  2029. &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
  2030. &sensor_dev_attr_pwm3_auto_start.dev_attr.attr,
  2031. &sensor_dev_attr_pwm3_auto_slope.dev_attr.attr,
  2032. &sensor_dev_attr_pwm4_auto_point1_temp.dev_attr.attr, /* 33 */
  2033. &sensor_dev_attr_pwm4_auto_point1_temp_hyst.dev_attr.attr,
  2034. &sensor_dev_attr_pwm4_auto_point2_temp.dev_attr.attr,
  2035. &sensor_dev_attr_pwm4_auto_point3_temp.dev_attr.attr,
  2036. &sensor_dev_attr_pwm4_auto_start.dev_attr.attr,
  2037. &sensor_dev_attr_pwm4_auto_slope.dev_attr.attr,
  2038. &sensor_dev_attr_pwm5_auto_point1_temp.dev_attr.attr,
  2039. &sensor_dev_attr_pwm5_auto_point1_temp_hyst.dev_attr.attr,
  2040. &sensor_dev_attr_pwm5_auto_point2_temp.dev_attr.attr,
  2041. &sensor_dev_attr_pwm5_auto_point3_temp.dev_attr.attr,
  2042. &sensor_dev_attr_pwm5_auto_start.dev_attr.attr,
  2043. &sensor_dev_attr_pwm5_auto_slope.dev_attr.attr,
  2044. &sensor_dev_attr_pwm6_auto_point1_temp.dev_attr.attr,
  2045. &sensor_dev_attr_pwm6_auto_point1_temp_hyst.dev_attr.attr,
  2046. &sensor_dev_attr_pwm6_auto_point2_temp.dev_attr.attr,
  2047. &sensor_dev_attr_pwm6_auto_point3_temp.dev_attr.attr,
  2048. &sensor_dev_attr_pwm6_auto_start.dev_attr.attr,
  2049. &sensor_dev_attr_pwm6_auto_slope.dev_attr.attr,
  2050. NULL,
  2051. };
  2052. static const struct attribute_group it87_group_auto_pwm = {
  2053. .attrs = it87_attributes_auto_pwm,
  2054. .is_visible = it87_auto_pwm_is_visible,
  2055. };
  2056. /* SuperIO detection - will change isa_address if a chip is found */
  2057. static int __init it87_find(int sioaddr, unsigned short *address,
  2058. struct it87_sio_data *sio_data)
  2059. {
  2060. int err;
  2061. u16 chip_type;
  2062. const char *board_vendor, *board_name;
  2063. const struct it87_devices *config;
  2064. err = superio_enter(sioaddr);
  2065. if (err)
  2066. return err;
  2067. err = -ENODEV;
  2068. chip_type = force_id ? force_id : superio_inw(sioaddr, DEVID);
  2069. switch (chip_type) {
  2070. case IT8705F_DEVID:
  2071. sio_data->type = it87;
  2072. break;
  2073. case IT8712F_DEVID:
  2074. sio_data->type = it8712;
  2075. break;
  2076. case IT8716F_DEVID:
  2077. case IT8726F_DEVID:
  2078. sio_data->type = it8716;
  2079. break;
  2080. case IT8718F_DEVID:
  2081. sio_data->type = it8718;
  2082. break;
  2083. case IT8720F_DEVID:
  2084. sio_data->type = it8720;
  2085. break;
  2086. case IT8721F_DEVID:
  2087. sio_data->type = it8721;
  2088. break;
  2089. case IT8728F_DEVID:
  2090. sio_data->type = it8728;
  2091. break;
  2092. case IT8732F_DEVID:
  2093. sio_data->type = it8732;
  2094. break;
  2095. case IT8771E_DEVID:
  2096. sio_data->type = it8771;
  2097. break;
  2098. case IT8772E_DEVID:
  2099. sio_data->type = it8772;
  2100. break;
  2101. case IT8781F_DEVID:
  2102. sio_data->type = it8781;
  2103. break;
  2104. case IT8782F_DEVID:
  2105. sio_data->type = it8782;
  2106. break;
  2107. case IT8783E_DEVID:
  2108. sio_data->type = it8783;
  2109. break;
  2110. case IT8786E_DEVID:
  2111. sio_data->type = it8786;
  2112. break;
  2113. case IT8790E_DEVID:
  2114. sio_data->type = it8790;
  2115. break;
  2116. case IT8603E_DEVID:
  2117. case IT8623E_DEVID:
  2118. sio_data->type = it8603;
  2119. break;
  2120. case IT8620E_DEVID:
  2121. sio_data->type = it8620;
  2122. break;
  2123. case IT8628E_DEVID:
  2124. sio_data->type = it8628;
  2125. break;
  2126. case 0xffff: /* No device at all */
  2127. goto exit;
  2128. default:
  2129. pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
  2130. goto exit;
  2131. }
  2132. superio_select(sioaddr, PME);
  2133. if (!(superio_inb(sioaddr, IT87_ACT_REG) & 0x01)) {
  2134. pr_info("Device not activated, skipping\n");
  2135. goto exit;
  2136. }
  2137. *address = superio_inw(sioaddr, IT87_BASE_REG) & ~(IT87_EXTENT - 1);
  2138. if (*address == 0) {
  2139. pr_info("Base address not set, skipping\n");
  2140. goto exit;
  2141. }
  2142. err = 0;
  2143. sio_data->revision = superio_inb(sioaddr, DEVREV) & 0x0f;
  2144. pr_info("Found IT%04x%s chip at 0x%x, revision %d\n", chip_type,
  2145. it87_devices[sio_data->type].suffix,
  2146. *address, sio_data->revision);
  2147. config = &it87_devices[sio_data->type];
  2148. /* in7 (VSB or VCCH5V) is always internal on some chips */
  2149. if (has_in7_internal(config))
  2150. sio_data->internal |= BIT(1);
  2151. /* in8 (Vbat) is always internal */
  2152. sio_data->internal |= BIT(2);
  2153. /* in9 (AVCC3), always internal if supported */
  2154. if (has_avcc3(config))
  2155. sio_data->internal |= BIT(3); /* in9 is AVCC */
  2156. else
  2157. sio_data->skip_in |= BIT(9);
  2158. if (!has_six_pwm(config))
  2159. sio_data->skip_pwm |= BIT(3) | BIT(4) | BIT(5);
  2160. if (!has_vid(config))
  2161. sio_data->skip_vid = 1;
  2162. /* Read GPIO config and VID value from LDN 7 (GPIO) */
  2163. if (sio_data->type == it87) {
  2164. /* The IT8705F has a different LD number for GPIO */
  2165. superio_select(sioaddr, 5);
  2166. sio_data->beep_pin = superio_inb(sioaddr,
  2167. IT87_SIO_BEEP_PIN_REG) & 0x3f;
  2168. } else if (sio_data->type == it8783) {
  2169. int reg25, reg27, reg2a, reg2c, regef;
  2170. superio_select(sioaddr, GPIO);
  2171. reg25 = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
  2172. reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
  2173. reg2a = superio_inb(sioaddr, IT87_SIO_PINX1_REG);
  2174. reg2c = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
  2175. regef = superio_inb(sioaddr, IT87_SIO_SPI_REG);
  2176. /* Check if fan3 is there or not */
  2177. if ((reg27 & BIT(0)) || !(reg2c & BIT(2)))
  2178. sio_data->skip_fan |= BIT(2);
  2179. if ((reg25 & BIT(4)) ||
  2180. (!(reg2a & BIT(1)) && (regef & BIT(0))))
  2181. sio_data->skip_pwm |= BIT(2);
  2182. /* Check if fan2 is there or not */
  2183. if (reg27 & BIT(7))
  2184. sio_data->skip_fan |= BIT(1);
  2185. if (reg27 & BIT(3))
  2186. sio_data->skip_pwm |= BIT(1);
  2187. /* VIN5 */
  2188. if ((reg27 & BIT(0)) || (reg2c & BIT(2)))
  2189. sio_data->skip_in |= BIT(5); /* No VIN5 */
  2190. /* VIN6 */
  2191. if (reg27 & BIT(1))
  2192. sio_data->skip_in |= BIT(6); /* No VIN6 */
  2193. /*
  2194. * VIN7
  2195. * Does not depend on bit 2 of Reg2C, contrary to datasheet.
  2196. */
  2197. if (reg27 & BIT(2)) {
  2198. /*
  2199. * The data sheet is a bit unclear regarding the
  2200. * internal voltage divider for VCCH5V. It says
  2201. * "This bit enables and switches VIN7 (pin 91) to the
  2202. * internal voltage divider for VCCH5V".
  2203. * This is different to other chips, where the internal
  2204. * voltage divider would connect VIN7 to an internal
  2205. * voltage source. Maybe that is the case here as well.
  2206. *
  2207. * Since we don't know for sure, re-route it if that is
  2208. * not the case, and ask the user to report if the
  2209. * resulting voltage is sane.
  2210. */
  2211. if (!(reg2c & BIT(1))) {
  2212. reg2c |= BIT(1);
  2213. superio_outb(sioaddr, IT87_SIO_PINX2_REG,
  2214. reg2c);
  2215. pr_notice("Routing internal VCCH5V to in7.\n");
  2216. }
  2217. pr_notice("in7 routed to internal voltage divider, with external pin disabled.\n");
  2218. pr_notice("Please report if it displays a reasonable voltage.\n");
  2219. }
  2220. if (reg2c & BIT(0))
  2221. sio_data->internal |= BIT(0);
  2222. if (reg2c & BIT(1))
  2223. sio_data->internal |= BIT(1);
  2224. sio_data->beep_pin = superio_inb(sioaddr,
  2225. IT87_SIO_BEEP_PIN_REG) & 0x3f;
  2226. } else if (sio_data->type == it8603) {
  2227. int reg27, reg29;
  2228. superio_select(sioaddr, GPIO);
  2229. reg27 = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
  2230. /* Check if fan3 is there or not */
  2231. if (reg27 & BIT(6))
  2232. sio_data->skip_pwm |= BIT(2);
  2233. if (reg27 & BIT(7))
  2234. sio_data->skip_fan |= BIT(2);
  2235. /* Check if fan2 is there or not */
  2236. reg29 = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
  2237. if (reg29 & BIT(1))
  2238. sio_data->skip_pwm |= BIT(1);
  2239. if (reg29 & BIT(2))
  2240. sio_data->skip_fan |= BIT(1);
  2241. sio_data->skip_in |= BIT(5); /* No VIN5 */
  2242. sio_data->skip_in |= BIT(6); /* No VIN6 */
  2243. sio_data->beep_pin = superio_inb(sioaddr,
  2244. IT87_SIO_BEEP_PIN_REG) & 0x3f;
  2245. } else if (sio_data->type == it8620 || sio_data->type == it8628) {
  2246. int reg;
  2247. superio_select(sioaddr, GPIO);
  2248. /* Check for pwm5 */
  2249. reg = superio_inb(sioaddr, IT87_SIO_GPIO1_REG);
  2250. if (reg & BIT(6))
  2251. sio_data->skip_pwm |= BIT(4);
  2252. /* Check for fan4, fan5 */
  2253. reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
  2254. if (!(reg & BIT(5)))
  2255. sio_data->skip_fan |= BIT(3);
  2256. if (!(reg & BIT(4)))
  2257. sio_data->skip_fan |= BIT(4);
  2258. /* Check for pwm3, fan3 */
  2259. reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
  2260. if (reg & BIT(6))
  2261. sio_data->skip_pwm |= BIT(2);
  2262. if (reg & BIT(7))
  2263. sio_data->skip_fan |= BIT(2);
  2264. /* Check for pwm4 */
  2265. reg = superio_inb(sioaddr, IT87_SIO_GPIO4_REG);
  2266. if (reg & BIT(2))
  2267. sio_data->skip_pwm |= BIT(3);
  2268. /* Check for pwm2, fan2 */
  2269. reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
  2270. if (reg & BIT(1))
  2271. sio_data->skip_pwm |= BIT(1);
  2272. if (reg & BIT(2))
  2273. sio_data->skip_fan |= BIT(1);
  2274. /* Check for pwm6, fan6 */
  2275. if (!(reg & BIT(7))) {
  2276. sio_data->skip_pwm |= BIT(5);
  2277. sio_data->skip_fan |= BIT(5);
  2278. }
  2279. sio_data->beep_pin = superio_inb(sioaddr,
  2280. IT87_SIO_BEEP_PIN_REG) & 0x3f;
  2281. } else {
  2282. int reg;
  2283. bool uart6;
  2284. superio_select(sioaddr, GPIO);
  2285. /* Check for fan4, fan5 */
  2286. if (has_five_fans(config)) {
  2287. reg = superio_inb(sioaddr, IT87_SIO_GPIO2_REG);
  2288. switch (sio_data->type) {
  2289. case it8718:
  2290. if (reg & BIT(5))
  2291. sio_data->skip_fan |= BIT(3);
  2292. if (reg & BIT(4))
  2293. sio_data->skip_fan |= BIT(4);
  2294. break;
  2295. case it8720:
  2296. case it8721:
  2297. case it8728:
  2298. if (!(reg & BIT(5)))
  2299. sio_data->skip_fan |= BIT(3);
  2300. if (!(reg & BIT(4)))
  2301. sio_data->skip_fan |= BIT(4);
  2302. break;
  2303. default:
  2304. break;
  2305. }
  2306. }
  2307. reg = superio_inb(sioaddr, IT87_SIO_GPIO3_REG);
  2308. if (!sio_data->skip_vid) {
  2309. /* We need at least 4 VID pins */
  2310. if (reg & 0x0f) {
  2311. pr_info("VID is disabled (pins used for GPIO)\n");
  2312. sio_data->skip_vid = 1;
  2313. }
  2314. }
  2315. /* Check if fan3 is there or not */
  2316. if (reg & BIT(6))
  2317. sio_data->skip_pwm |= BIT(2);
  2318. if (reg & BIT(7))
  2319. sio_data->skip_fan |= BIT(2);
  2320. /* Check if fan2 is there or not */
  2321. reg = superio_inb(sioaddr, IT87_SIO_GPIO5_REG);
  2322. if (reg & BIT(1))
  2323. sio_data->skip_pwm |= BIT(1);
  2324. if (reg & BIT(2))
  2325. sio_data->skip_fan |= BIT(1);
  2326. if ((sio_data->type == it8718 || sio_data->type == it8720) &&
  2327. !(sio_data->skip_vid))
  2328. sio_data->vid_value = superio_inb(sioaddr,
  2329. IT87_SIO_VID_REG);
  2330. reg = superio_inb(sioaddr, IT87_SIO_PINX2_REG);
  2331. uart6 = sio_data->type == it8782 && (reg & BIT(2));
  2332. /*
  2333. * The IT8720F has no VIN7 pin, so VCCH should always be
  2334. * routed internally to VIN7 with an internal divider.
  2335. * Curiously, there still is a configuration bit to control
  2336. * this, which means it can be set incorrectly. And even
  2337. * more curiously, many boards out there are improperly
  2338. * configured, even though the IT8720F datasheet claims
  2339. * that the internal routing of VCCH to VIN7 is the default
  2340. * setting. So we force the internal routing in this case.
  2341. *
  2342. * On IT8782F, VIN7 is multiplexed with one of the UART6 pins.
  2343. * If UART6 is enabled, re-route VIN7 to the internal divider
  2344. * if that is not already the case.
  2345. */
  2346. if ((sio_data->type == it8720 || uart6) && !(reg & BIT(1))) {
  2347. reg |= BIT(1);
  2348. superio_outb(sioaddr, IT87_SIO_PINX2_REG, reg);
  2349. pr_notice("Routing internal VCCH to in7\n");
  2350. }
  2351. if (reg & BIT(0))
  2352. sio_data->internal |= BIT(0);
  2353. if (reg & BIT(1))
  2354. sio_data->internal |= BIT(1);
  2355. /*
  2356. * On IT8782F, UART6 pins overlap with VIN5, VIN6, and VIN7.
  2357. * While VIN7 can be routed to the internal voltage divider,
  2358. * VIN5 and VIN6 are not available if UART6 is enabled.
  2359. *
  2360. * Also, temp3 is not available if UART6 is enabled and TEMPIN3
  2361. * is the temperature source. Since we can not read the
  2362. * temperature source here, skip_temp is preliminary.
  2363. */
  2364. if (uart6) {
  2365. sio_data->skip_in |= BIT(5) | BIT(6);
  2366. sio_data->skip_temp |= BIT(2);
  2367. }
  2368. sio_data->beep_pin = superio_inb(sioaddr,
  2369. IT87_SIO_BEEP_PIN_REG) & 0x3f;
  2370. }
  2371. if (sio_data->beep_pin)
  2372. pr_info("Beeping is supported\n");
  2373. /* Disable specific features based on DMI strings */
  2374. board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
  2375. board_name = dmi_get_system_info(DMI_BOARD_NAME);
  2376. if (board_vendor && board_name) {
  2377. if (strcmp(board_vendor, "nVIDIA") == 0 &&
  2378. strcmp(board_name, "FN68PT") == 0) {
  2379. /*
  2380. * On the Shuttle SN68PT, FAN_CTL2 is apparently not
  2381. * connected to a fan, but to something else. One user
  2382. * has reported instant system power-off when changing
  2383. * the PWM2 duty cycle, so we disable it.
  2384. * I use the board name string as the trigger in case
  2385. * the same board is ever used in other systems.
  2386. */
  2387. pr_info("Disabling pwm2 due to hardware constraints\n");
  2388. sio_data->skip_pwm = BIT(1);
  2389. }
  2390. }
  2391. exit:
  2392. superio_exit(sioaddr);
  2393. return err;
  2394. }
  2395. /* Called when we have found a new IT87. */
  2396. static void it87_init_device(struct platform_device *pdev)
  2397. {
  2398. struct it87_sio_data *sio_data = dev_get_platdata(&pdev->dev);
  2399. struct it87_data *data = platform_get_drvdata(pdev);
  2400. int tmp, i;
  2401. u8 mask;
  2402. /*
  2403. * For each PWM channel:
  2404. * - If it is in automatic mode, setting to manual mode should set
  2405. * the fan to full speed by default.
  2406. * - If it is in manual mode, we need a mapping to temperature
  2407. * channels to use when later setting to automatic mode later.
  2408. * Use a 1:1 mapping by default (we are clueless.)
  2409. * In both cases, the value can (and should) be changed by the user
  2410. * prior to switching to a different mode.
  2411. * Note that this is no longer needed for the IT8721F and later, as
  2412. * these have separate registers for the temperature mapping and the
  2413. * manual duty cycle.
  2414. */
  2415. for (i = 0; i < NUM_AUTO_PWM; i++) {
  2416. data->pwm_temp_map[i] = i;
  2417. data->pwm_duty[i] = 0x7f; /* Full speed */
  2418. data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
  2419. }
  2420. /*
  2421. * Some chips seem to have default value 0xff for all limit
  2422. * registers. For low voltage limits it makes no sense and triggers
  2423. * alarms, so change to 0 instead. For high temperature limits, it
  2424. * means -1 degree C, which surprisingly doesn't trigger an alarm,
  2425. * but is still confusing, so change to 127 degrees C.
  2426. */
  2427. for (i = 0; i < NUM_VIN_LIMIT; i++) {
  2428. tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
  2429. if (tmp == 0xff)
  2430. it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
  2431. }
  2432. for (i = 0; i < NUM_TEMP_LIMIT; i++) {
  2433. tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
  2434. if (tmp == 0xff)
  2435. it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
  2436. }
  2437. /*
  2438. * Temperature channels are not forcibly enabled, as they can be
  2439. * set to two different sensor types and we can't guess which one
  2440. * is correct for a given system. These channels can be enabled at
  2441. * run-time through the temp{1-3}_type sysfs accessors if needed.
  2442. */
  2443. /* Check if voltage monitors are reset manually or by some reason */
  2444. tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
  2445. if ((tmp & 0xff) == 0) {
  2446. /* Enable all voltage monitors */
  2447. it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
  2448. }
  2449. /* Check if tachometers are reset manually or by some reason */
  2450. mask = 0x70 & ~(sio_data->skip_fan << 4);
  2451. data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
  2452. if ((data->fan_main_ctrl & mask) == 0) {
  2453. /* Enable all fan tachometers */
  2454. data->fan_main_ctrl |= mask;
  2455. it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
  2456. data->fan_main_ctrl);
  2457. }
  2458. data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
  2459. tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
  2460. /* Set tachometers to 16-bit mode if needed */
  2461. if (has_fan16_config(data)) {
  2462. if (~tmp & 0x07 & data->has_fan) {
  2463. dev_dbg(&pdev->dev,
  2464. "Setting fan1-3 to 16-bit mode\n");
  2465. it87_write_value(data, IT87_REG_FAN_16BIT,
  2466. tmp | 0x07);
  2467. }
  2468. }
  2469. /* Check for additional fans */
  2470. if (has_five_fans(data)) {
  2471. if (tmp & BIT(4))
  2472. data->has_fan |= BIT(3); /* fan4 enabled */
  2473. if (tmp & BIT(5))
  2474. data->has_fan |= BIT(4); /* fan5 enabled */
  2475. if (has_six_fans(data) && (tmp & BIT(2)))
  2476. data->has_fan |= BIT(5); /* fan6 enabled */
  2477. }
  2478. /* Fan input pins may be used for alternative functions */
  2479. data->has_fan &= ~sio_data->skip_fan;
  2480. /* Check if pwm5, pwm6 are enabled */
  2481. if (has_six_pwm(data)) {
  2482. /* The following code may be IT8620E specific */
  2483. tmp = it87_read_value(data, IT87_REG_FAN_DIV);
  2484. if ((tmp & 0xc0) == 0xc0)
  2485. sio_data->skip_pwm |= BIT(4);
  2486. if (!(tmp & BIT(3)))
  2487. sio_data->skip_pwm |= BIT(5);
  2488. }
  2489. /* Start monitoring */
  2490. it87_write_value(data, IT87_REG_CONFIG,
  2491. (it87_read_value(data, IT87_REG_CONFIG) & 0x3e)
  2492. | (update_vbat ? 0x41 : 0x01));
  2493. }
  2494. /* Return 1 if and only if the PWM interface is safe to use */
  2495. static int it87_check_pwm(struct device *dev)
  2496. {
  2497. struct it87_data *data = dev_get_drvdata(dev);
  2498. /*
  2499. * Some BIOSes fail to correctly configure the IT87 fans. All fans off
  2500. * and polarity set to active low is sign that this is the case so we
  2501. * disable pwm control to protect the user.
  2502. */
  2503. int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
  2504. if ((tmp & 0x87) == 0) {
  2505. if (fix_pwm_polarity) {
  2506. /*
  2507. * The user asks us to attempt a chip reconfiguration.
  2508. * This means switching to active high polarity and
  2509. * inverting all fan speed values.
  2510. */
  2511. int i;
  2512. u8 pwm[3];
  2513. for (i = 0; i < ARRAY_SIZE(pwm); i++)
  2514. pwm[i] = it87_read_value(data,
  2515. IT87_REG_PWM[i]);
  2516. /*
  2517. * If any fan is in automatic pwm mode, the polarity
  2518. * might be correct, as suspicious as it seems, so we
  2519. * better don't change anything (but still disable the
  2520. * PWM interface).
  2521. */
  2522. if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
  2523. dev_info(dev,
  2524. "Reconfiguring PWM to active high polarity\n");
  2525. it87_write_value(data, IT87_REG_FAN_CTL,
  2526. tmp | 0x87);
  2527. for (i = 0; i < 3; i++)
  2528. it87_write_value(data,
  2529. IT87_REG_PWM[i],
  2530. 0x7f & ~pwm[i]);
  2531. return 1;
  2532. }
  2533. dev_info(dev,
  2534. "PWM configuration is too broken to be fixed\n");
  2535. }
  2536. dev_info(dev,
  2537. "Detected broken BIOS defaults, disabling PWM interface\n");
  2538. return 0;
  2539. } else if (fix_pwm_polarity) {
  2540. dev_info(dev,
  2541. "PWM configuration looks sane, won't touch\n");
  2542. }
  2543. return 1;
  2544. }
  2545. static int it87_probe(struct platform_device *pdev)
  2546. {
  2547. struct it87_data *data;
  2548. struct resource *res;
  2549. struct device *dev = &pdev->dev;
  2550. struct it87_sio_data *sio_data = dev_get_platdata(dev);
  2551. int enable_pwm_interface;
  2552. struct device *hwmon_dev;
  2553. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  2554. if (!devm_request_region(&pdev->dev, res->start, IT87_EC_EXTENT,
  2555. DRVNAME)) {
  2556. dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
  2557. (unsigned long)res->start,
  2558. (unsigned long)(res->start + IT87_EC_EXTENT - 1));
  2559. return -EBUSY;
  2560. }
  2561. data = devm_kzalloc(&pdev->dev, sizeof(struct it87_data), GFP_KERNEL);
  2562. if (!data)
  2563. return -ENOMEM;
  2564. data->addr = res->start;
  2565. data->type = sio_data->type;
  2566. data->features = it87_devices[sio_data->type].features;
  2567. data->peci_mask = it87_devices[sio_data->type].peci_mask;
  2568. data->old_peci_mask = it87_devices[sio_data->type].old_peci_mask;
  2569. /*
  2570. * IT8705F Datasheet 0.4.1, 3h == Version G.
  2571. * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
  2572. * These are the first revisions with 16-bit tachometer support.
  2573. */
  2574. switch (data->type) {
  2575. case it87:
  2576. if (sio_data->revision >= 0x03) {
  2577. data->features &= ~FEAT_OLD_AUTOPWM;
  2578. data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS;
  2579. }
  2580. break;
  2581. case it8712:
  2582. if (sio_data->revision >= 0x08) {
  2583. data->features &= ~FEAT_OLD_AUTOPWM;
  2584. data->features |= FEAT_FAN16_CONFIG | FEAT_16BIT_FANS |
  2585. FEAT_FIVE_FANS;
  2586. }
  2587. break;
  2588. default:
  2589. break;
  2590. }
  2591. /* Now, we do the remaining detection. */
  2592. if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80) ||
  2593. it87_read_value(data, IT87_REG_CHIPID) != 0x90)
  2594. return -ENODEV;
  2595. platform_set_drvdata(pdev, data);
  2596. mutex_init(&data->update_lock);
  2597. /* Check PWM configuration */
  2598. enable_pwm_interface = it87_check_pwm(dev);
  2599. /* Starting with IT8721F, we handle scaling of internal voltages */
  2600. if (has_12mv_adc(data)) {
  2601. if (sio_data->internal & BIT(0))
  2602. data->in_scaled |= BIT(3); /* in3 is AVCC */
  2603. if (sio_data->internal & BIT(1))
  2604. data->in_scaled |= BIT(7); /* in7 is VSB */
  2605. if (sio_data->internal & BIT(2))
  2606. data->in_scaled |= BIT(8); /* in8 is Vbat */
  2607. if (sio_data->internal & BIT(3))
  2608. data->in_scaled |= BIT(9); /* in9 is AVCC */
  2609. } else if (sio_data->type == it8781 || sio_data->type == it8782 ||
  2610. sio_data->type == it8783) {
  2611. if (sio_data->internal & BIT(0))
  2612. data->in_scaled |= BIT(3); /* in3 is VCC5V */
  2613. if (sio_data->internal & BIT(1))
  2614. data->in_scaled |= BIT(7); /* in7 is VCCH5V */
  2615. }
  2616. data->has_temp = 0x07;
  2617. if (sio_data->skip_temp & BIT(2)) {
  2618. if (sio_data->type == it8782 &&
  2619. !(it87_read_value(data, IT87_REG_TEMP_EXTRA) & 0x80))
  2620. data->has_temp &= ~BIT(2);
  2621. }
  2622. data->in_internal = sio_data->internal;
  2623. data->has_in = 0x3ff & ~sio_data->skip_in;
  2624. if (has_six_temp(data)) {
  2625. u8 reg = it87_read_value(data, IT87_REG_TEMP456_ENABLE);
  2626. /* Check for additional temperature sensors */
  2627. if ((reg & 0x03) >= 0x02)
  2628. data->has_temp |= BIT(3);
  2629. if (((reg >> 2) & 0x03) >= 0x02)
  2630. data->has_temp |= BIT(4);
  2631. if (((reg >> 4) & 0x03) >= 0x02)
  2632. data->has_temp |= BIT(5);
  2633. /* Check for additional voltage sensors */
  2634. if ((reg & 0x03) == 0x01)
  2635. data->has_in |= BIT(10);
  2636. if (((reg >> 2) & 0x03) == 0x01)
  2637. data->has_in |= BIT(11);
  2638. if (((reg >> 4) & 0x03) == 0x01)
  2639. data->has_in |= BIT(12);
  2640. }
  2641. data->has_beep = !!sio_data->beep_pin;
  2642. /* Initialize the IT87 chip */
  2643. it87_init_device(pdev);
  2644. if (!sio_data->skip_vid) {
  2645. data->has_vid = true;
  2646. data->vrm = vid_which_vrm();
  2647. /* VID reading from Super-I/O config space if available */
  2648. data->vid = sio_data->vid_value;
  2649. }
  2650. /* Prepare for sysfs hooks */
  2651. data->groups[0] = &it87_group;
  2652. data->groups[1] = &it87_group_in;
  2653. data->groups[2] = &it87_group_temp;
  2654. data->groups[3] = &it87_group_fan;
  2655. if (enable_pwm_interface) {
  2656. data->has_pwm = BIT(ARRAY_SIZE(IT87_REG_PWM)) - 1;
  2657. data->has_pwm &= ~sio_data->skip_pwm;
  2658. data->groups[4] = &it87_group_pwm;
  2659. if (has_old_autopwm(data) || has_newer_autopwm(data))
  2660. data->groups[5] = &it87_group_auto_pwm;
  2661. }
  2662. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  2663. it87_devices[sio_data->type].name,
  2664. data, data->groups);
  2665. return PTR_ERR_OR_ZERO(hwmon_dev);
  2666. }
  2667. static struct platform_driver it87_driver = {
  2668. .driver = {
  2669. .name = DRVNAME,
  2670. },
  2671. .probe = it87_probe,
  2672. };
  2673. static int __init it87_device_add(int index, unsigned short address,
  2674. const struct it87_sio_data *sio_data)
  2675. {
  2676. struct platform_device *pdev;
  2677. struct resource res = {
  2678. .start = address + IT87_EC_OFFSET,
  2679. .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
  2680. .name = DRVNAME,
  2681. .flags = IORESOURCE_IO,
  2682. };
  2683. int err;
  2684. err = acpi_check_resource_conflict(&res);
  2685. if (err)
  2686. return err;
  2687. pdev = platform_device_alloc(DRVNAME, address);
  2688. if (!pdev)
  2689. return -ENOMEM;
  2690. err = platform_device_add_resources(pdev, &res, 1);
  2691. if (err) {
  2692. pr_err("Device resource addition failed (%d)\n", err);
  2693. goto exit_device_put;
  2694. }
  2695. err = platform_device_add_data(pdev, sio_data,
  2696. sizeof(struct it87_sio_data));
  2697. if (err) {
  2698. pr_err("Platform data allocation failed\n");
  2699. goto exit_device_put;
  2700. }
  2701. err = platform_device_add(pdev);
  2702. if (err) {
  2703. pr_err("Device addition failed (%d)\n", err);
  2704. goto exit_device_put;
  2705. }
  2706. it87_pdev[index] = pdev;
  2707. return 0;
  2708. exit_device_put:
  2709. platform_device_put(pdev);
  2710. return err;
  2711. }
  2712. static int __init sm_it87_init(void)
  2713. {
  2714. int sioaddr[2] = { REG_2E, REG_4E };
  2715. struct it87_sio_data sio_data;
  2716. unsigned short isa_address[2];
  2717. bool found = false;
  2718. int i, err;
  2719. err = platform_driver_register(&it87_driver);
  2720. if (err)
  2721. return err;
  2722. for (i = 0; i < ARRAY_SIZE(sioaddr); i++) {
  2723. memset(&sio_data, 0, sizeof(struct it87_sio_data));
  2724. isa_address[i] = 0;
  2725. err = it87_find(sioaddr[i], &isa_address[i], &sio_data);
  2726. if (err || isa_address[i] == 0)
  2727. continue;
  2728. /*
  2729. * Don't register second chip if its ISA address matches
  2730. * the first chip's ISA address.
  2731. */
  2732. if (i && isa_address[i] == isa_address[0])
  2733. break;
  2734. err = it87_device_add(i, isa_address[i], &sio_data);
  2735. if (err)
  2736. goto exit_dev_unregister;
  2737. found = true;
  2738. /*
  2739. * IT8705F may respond on both SIO addresses.
  2740. * Stop probing after finding one.
  2741. */
  2742. if (sio_data.type == it87)
  2743. break;
  2744. }
  2745. if (!found) {
  2746. err = -ENODEV;
  2747. goto exit_unregister;
  2748. }
  2749. return 0;
  2750. exit_dev_unregister:
  2751. /* NULL check handled by platform_device_unregister */
  2752. platform_device_unregister(it87_pdev[0]);
  2753. exit_unregister:
  2754. platform_driver_unregister(&it87_driver);
  2755. return err;
  2756. }
  2757. static void __exit sm_it87_exit(void)
  2758. {
  2759. /* NULL check handled by platform_device_unregister */
  2760. platform_device_unregister(it87_pdev[1]);
  2761. platform_device_unregister(it87_pdev[0]);
  2762. platform_driver_unregister(&it87_driver);
  2763. }
  2764. MODULE_AUTHOR("Chris Gauthron, Jean Delvare <jdelvare@suse.de>");
  2765. MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
  2766. module_param(update_vbat, bool, 0);
  2767. MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
  2768. module_param(fix_pwm_polarity, bool, 0);
  2769. MODULE_PARM_DESC(fix_pwm_polarity,
  2770. "Force PWM polarity to active high (DANGEROUS)");
  2771. MODULE_LICENSE("GPL");
  2772. module_init(sm_it87_init);
  2773. module_exit(sm_it87_exit);