it87.c 66 KB

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