it87.c 66 KB

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