it87.c 55 KB

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