it87.c 67 KB

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