it87.c 80 KB

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