it87.c 77 KB

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