it87.c 68 KB

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