it87.c 70 KB

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