it87.c 73 KB

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