it87.c 78 KB

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