it87.c 73 KB

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