it87.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. it87.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring.
  4. Supports: IT8705F Super I/O chip w/LPC interface
  5. IT8712F Super I/O chip w/LPC interface
  6. IT8716F Super I/O chip w/LPC interface
  7. IT8718F Super I/O chip w/LPC interface
  8. IT8726F Super I/O chip w/LPC interface
  9. Sis950 A clone of the IT8705F
  10. Copyright (C) 2001 Chris Gauthron <chrisg@0-in.com>
  11. Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-isa.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/hwmon-vid.h>
  33. #include <linux/err.h>
  34. #include <linux/mutex.h>
  35. #include <linux/sysfs.h>
  36. #include <asm/io.h>
  37. static unsigned short isa_address;
  38. enum chips { it87, it8712, it8716, it8718 };
  39. #define REG 0x2e /* The register to read/write */
  40. #define DEV 0x07 /* Register: Logical device select */
  41. #define VAL 0x2f /* The value to read/write */
  42. #define PME 0x04 /* The device with the fan registers in it */
  43. #define GPIO 0x07 /* The device with the IT8718F VID value in it */
  44. #define DEVID 0x20 /* Register: Device ID */
  45. #define DEVREV 0x22 /* Register: Device Revision */
  46. static inline int
  47. superio_inb(int reg)
  48. {
  49. outb(reg, REG);
  50. return inb(VAL);
  51. }
  52. static int superio_inw(int reg)
  53. {
  54. int val;
  55. outb(reg++, REG);
  56. val = inb(VAL) << 8;
  57. outb(reg, REG);
  58. val |= inb(VAL);
  59. return val;
  60. }
  61. static inline void
  62. superio_select(int ldn)
  63. {
  64. outb(DEV, REG);
  65. outb(ldn, VAL);
  66. }
  67. static inline void
  68. superio_enter(void)
  69. {
  70. outb(0x87, REG);
  71. outb(0x01, REG);
  72. outb(0x55, REG);
  73. outb(0x55, REG);
  74. }
  75. static inline void
  76. superio_exit(void)
  77. {
  78. outb(0x02, REG);
  79. outb(0x02, VAL);
  80. }
  81. /* Logical device 4 registers */
  82. #define IT8712F_DEVID 0x8712
  83. #define IT8705F_DEVID 0x8705
  84. #define IT8716F_DEVID 0x8716
  85. #define IT8718F_DEVID 0x8718
  86. #define IT8726F_DEVID 0x8726
  87. #define IT87_ACT_REG 0x30
  88. #define IT87_BASE_REG 0x60
  89. /* Logical device 7 registers (IT8712F and later) */
  90. #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
  91. #define IT87_SIO_VID_REG 0xfc /* VID value */
  92. /* Update battery voltage after every reading if true */
  93. static int update_vbat;
  94. /* Not all BIOSes properly configure the PWM registers */
  95. static int fix_pwm_polarity;
  96. /* Values read from Super-I/O config space */
  97. static u16 chip_type;
  98. static u8 vid_value;
  99. /* Many IT87 constants specified below */
  100. /* Length of ISA address segment */
  101. #define IT87_EXTENT 8
  102. /* Where are the ISA address/data registers relative to the base address */
  103. #define IT87_ADDR_REG_OFFSET 5
  104. #define IT87_DATA_REG_OFFSET 6
  105. /*----- The IT87 registers -----*/
  106. #define IT87_REG_CONFIG 0x00
  107. #define IT87_REG_ALARM1 0x01
  108. #define IT87_REG_ALARM2 0x02
  109. #define IT87_REG_ALARM3 0x03
  110. /* The IT8718F has the VID value in a different register, in Super-I/O
  111. configuration space. */
  112. #define IT87_REG_VID 0x0a
  113. /* Warning: register 0x0b is used for something completely different in
  114. new chips/revisions. I suspect only 16-bit tachometer mode will work
  115. for these. */
  116. #define IT87_REG_FAN_DIV 0x0b
  117. #define IT87_REG_FAN_16BIT 0x0c
  118. /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
  119. #define IT87_REG_FAN(nr) (0x0d + (nr))
  120. #define IT87_REG_FAN_MIN(nr) (0x10 + (nr))
  121. #define IT87_REG_FANX(nr) (0x18 + (nr))
  122. #define IT87_REG_FANX_MIN(nr) (0x1b + (nr))
  123. #define IT87_REG_FAN_MAIN_CTRL 0x13
  124. #define IT87_REG_FAN_CTL 0x14
  125. #define IT87_REG_PWM(nr) (0x15 + (nr))
  126. #define IT87_REG_VIN(nr) (0x20 + (nr))
  127. #define IT87_REG_TEMP(nr) (0x29 + (nr))
  128. #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
  129. #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
  130. #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
  131. #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
  132. #define IT87_REG_VIN_ENABLE 0x50
  133. #define IT87_REG_TEMP_ENABLE 0x51
  134. #define IT87_REG_CHIPID 0x58
  135. #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
  136. #define IN_FROM_REG(val) ((val) * 16)
  137. static inline u8 FAN_TO_REG(long rpm, int div)
  138. {
  139. if (rpm == 0)
  140. return 255;
  141. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  142. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
  143. 254);
  144. }
  145. static inline u16 FAN16_TO_REG(long rpm)
  146. {
  147. if (rpm == 0)
  148. return 0xffff;
  149. return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
  150. }
  151. #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
  152. /* The divider is fixed to 2 in 16-bit mode */
  153. #define FAN16_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:1350000/((val)*2))
  154. #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)<0?(((val)-500)/1000):\
  155. ((val)+500)/1000),-128,127))
  156. #define TEMP_FROM_REG(val) (((val)>0x80?(val)-0x100:(val))*1000)
  157. #define PWM_TO_REG(val) ((val) >> 1)
  158. #define PWM_FROM_REG(val) (((val)&0x7f) << 1)
  159. static int DIV_TO_REG(int val)
  160. {
  161. int answer = 0;
  162. while (answer < 7 && (val >>= 1))
  163. answer++;
  164. return answer;
  165. }
  166. #define DIV_FROM_REG(val) (1 << (val))
  167. static const unsigned int pwm_freq[8] = {
  168. 48000000 / 128,
  169. 24000000 / 128,
  170. 12000000 / 128,
  171. 8000000 / 128,
  172. 6000000 / 128,
  173. 3000000 / 128,
  174. 1500000 / 128,
  175. 750000 / 128,
  176. };
  177. /* For each registered chip, we need to keep some data in memory.
  178. The structure is dynamically allocated. */
  179. struct it87_data {
  180. struct i2c_client client;
  181. struct class_device *class_dev;
  182. enum chips type;
  183. struct mutex update_lock;
  184. char valid; /* !=0 if following fields are valid */
  185. unsigned long last_updated; /* In jiffies */
  186. u8 in[9]; /* Register value */
  187. u8 in_max[8]; /* Register value */
  188. u8 in_min[8]; /* Register value */
  189. u8 has_fan; /* Bitfield, fans enabled */
  190. u16 fan[3]; /* Register values, possibly combined */
  191. u16 fan_min[3]; /* Register values, possibly combined */
  192. u8 temp[3]; /* Register value */
  193. u8 temp_high[3]; /* Register value */
  194. u8 temp_low[3]; /* Register value */
  195. u8 sensor; /* Register value */
  196. u8 fan_div[3]; /* Register encoding, shifted right */
  197. u8 vid; /* Register encoding, combined */
  198. u8 vrm;
  199. u32 alarms; /* Register encoding, combined */
  200. u8 fan_main_ctrl; /* Register value */
  201. u8 fan_ctl; /* Register value */
  202. u8 manual_pwm_ctl[3]; /* manual PWM value set by user */
  203. };
  204. static int it87_detect(struct i2c_adapter *adapter);
  205. static int it87_detach_client(struct i2c_client *client);
  206. static int it87_read_value(struct i2c_client *client, u8 reg);
  207. static void it87_write_value(struct i2c_client *client, u8 reg, u8 value);
  208. static struct it87_data *it87_update_device(struct device *dev);
  209. static int it87_check_pwm(struct i2c_client *client);
  210. static void it87_init_client(struct i2c_client *client, struct it87_data *data);
  211. static struct i2c_driver it87_isa_driver = {
  212. .driver = {
  213. .owner = THIS_MODULE,
  214. .name = "it87-isa",
  215. },
  216. .attach_adapter = it87_detect,
  217. .detach_client = it87_detach_client,
  218. };
  219. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  220. char *buf)
  221. {
  222. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  223. int nr = sensor_attr->index;
  224. struct it87_data *data = it87_update_device(dev);
  225. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
  226. }
  227. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  228. char *buf)
  229. {
  230. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  231. int nr = sensor_attr->index;
  232. struct it87_data *data = it87_update_device(dev);
  233. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
  234. }
  235. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  236. char *buf)
  237. {
  238. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  239. int nr = sensor_attr->index;
  240. struct it87_data *data = it87_update_device(dev);
  241. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
  242. }
  243. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  244. const char *buf, size_t count)
  245. {
  246. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  247. int nr = sensor_attr->index;
  248. struct i2c_client *client = to_i2c_client(dev);
  249. struct it87_data *data = i2c_get_clientdata(client);
  250. unsigned long val = simple_strtoul(buf, NULL, 10);
  251. mutex_lock(&data->update_lock);
  252. data->in_min[nr] = IN_TO_REG(val);
  253. it87_write_value(client, IT87_REG_VIN_MIN(nr),
  254. data->in_min[nr]);
  255. mutex_unlock(&data->update_lock);
  256. return count;
  257. }
  258. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  259. const char *buf, size_t count)
  260. {
  261. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  262. int nr = sensor_attr->index;
  263. struct i2c_client *client = to_i2c_client(dev);
  264. struct it87_data *data = i2c_get_clientdata(client);
  265. unsigned long val = simple_strtoul(buf, NULL, 10);
  266. mutex_lock(&data->update_lock);
  267. data->in_max[nr] = IN_TO_REG(val);
  268. it87_write_value(client, IT87_REG_VIN_MAX(nr),
  269. data->in_max[nr]);
  270. mutex_unlock(&data->update_lock);
  271. return count;
  272. }
  273. #define show_in_offset(offset) \
  274. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  275. show_in, NULL, offset);
  276. #define limit_in_offset(offset) \
  277. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  278. show_in_min, set_in_min, offset); \
  279. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  280. show_in_max, set_in_max, offset);
  281. show_in_offset(0);
  282. limit_in_offset(0);
  283. show_in_offset(1);
  284. limit_in_offset(1);
  285. show_in_offset(2);
  286. limit_in_offset(2);
  287. show_in_offset(3);
  288. limit_in_offset(3);
  289. show_in_offset(4);
  290. limit_in_offset(4);
  291. show_in_offset(5);
  292. limit_in_offset(5);
  293. show_in_offset(6);
  294. limit_in_offset(6);
  295. show_in_offset(7);
  296. limit_in_offset(7);
  297. show_in_offset(8);
  298. /* 3 temperatures */
  299. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  300. char *buf)
  301. {
  302. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  303. int nr = sensor_attr->index;
  304. struct it87_data *data = it87_update_device(dev);
  305. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  306. }
  307. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  308. char *buf)
  309. {
  310. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  311. int nr = sensor_attr->index;
  312. struct it87_data *data = it87_update_device(dev);
  313. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
  314. }
  315. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  316. char *buf)
  317. {
  318. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  319. int nr = sensor_attr->index;
  320. struct it87_data *data = it87_update_device(dev);
  321. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
  322. }
  323. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  324. const char *buf, size_t count)
  325. {
  326. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  327. int nr = sensor_attr->index;
  328. struct i2c_client *client = to_i2c_client(dev);
  329. struct it87_data *data = i2c_get_clientdata(client);
  330. int val = simple_strtol(buf, NULL, 10);
  331. mutex_lock(&data->update_lock);
  332. data->temp_high[nr] = TEMP_TO_REG(val);
  333. it87_write_value(client, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  334. mutex_unlock(&data->update_lock);
  335. return count;
  336. }
  337. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  338. const char *buf, size_t count)
  339. {
  340. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  341. int nr = sensor_attr->index;
  342. struct i2c_client *client = to_i2c_client(dev);
  343. struct it87_data *data = i2c_get_clientdata(client);
  344. int val = simple_strtol(buf, NULL, 10);
  345. mutex_lock(&data->update_lock);
  346. data->temp_low[nr] = TEMP_TO_REG(val);
  347. it87_write_value(client, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
  348. mutex_unlock(&data->update_lock);
  349. return count;
  350. }
  351. #define show_temp_offset(offset) \
  352. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  353. show_temp, NULL, offset - 1); \
  354. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  355. show_temp_max, set_temp_max, offset - 1); \
  356. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  357. show_temp_min, set_temp_min, offset - 1);
  358. show_temp_offset(1);
  359. show_temp_offset(2);
  360. show_temp_offset(3);
  361. static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
  362. char *buf)
  363. {
  364. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  365. int nr = sensor_attr->index;
  366. struct it87_data *data = it87_update_device(dev);
  367. u8 reg = data->sensor; /* In case the value is updated while we use it */
  368. if (reg & (1 << nr))
  369. return sprintf(buf, "3\n"); /* thermal diode */
  370. if (reg & (8 << nr))
  371. return sprintf(buf, "2\n"); /* thermistor */
  372. return sprintf(buf, "0\n"); /* disabled */
  373. }
  374. static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
  375. const char *buf, size_t count)
  376. {
  377. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  378. int nr = sensor_attr->index;
  379. struct i2c_client *client = to_i2c_client(dev);
  380. struct it87_data *data = i2c_get_clientdata(client);
  381. int val = simple_strtol(buf, NULL, 10);
  382. mutex_lock(&data->update_lock);
  383. data->sensor &= ~(1 << nr);
  384. data->sensor &= ~(8 << nr);
  385. /* 3 = thermal diode; 2 = thermistor; 0 = disabled */
  386. if (val == 3)
  387. data->sensor |= 1 << nr;
  388. else if (val == 2)
  389. data->sensor |= 8 << nr;
  390. else if (val != 0) {
  391. mutex_unlock(&data->update_lock);
  392. return -EINVAL;
  393. }
  394. it87_write_value(client, IT87_REG_TEMP_ENABLE, data->sensor);
  395. mutex_unlock(&data->update_lock);
  396. return count;
  397. }
  398. #define show_sensor_offset(offset) \
  399. static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
  400. show_sensor, set_sensor, offset - 1);
  401. show_sensor_offset(1);
  402. show_sensor_offset(2);
  403. show_sensor_offset(3);
  404. /* 3 Fans */
  405. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  406. char *buf)
  407. {
  408. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  409. int nr = sensor_attr->index;
  410. struct it87_data *data = it87_update_device(dev);
  411. return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr],
  412. DIV_FROM_REG(data->fan_div[nr])));
  413. }
  414. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  415. char *buf)
  416. {
  417. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  418. int nr = sensor_attr->index;
  419. struct it87_data *data = it87_update_device(dev);
  420. return sprintf(buf,"%d\n",
  421. FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr])));
  422. }
  423. static ssize_t show_fan_div(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. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  430. }
  431. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  435. int nr = sensor_attr->index;
  436. struct it87_data *data = it87_update_device(dev);
  437. return sprintf(buf,"%d\n", (data->fan_main_ctrl & (1 << nr)) ? 1 : 0);
  438. }
  439. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  440. char *buf)
  441. {
  442. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  443. int nr = sensor_attr->index;
  444. struct it87_data *data = it87_update_device(dev);
  445. return sprintf(buf,"%d\n", data->manual_pwm_ctl[nr]);
  446. }
  447. static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
  448. char *buf)
  449. {
  450. struct it87_data *data = it87_update_device(dev);
  451. int index = (data->fan_ctl >> 4) & 0x07;
  452. return sprintf(buf, "%u\n", pwm_freq[index]);
  453. }
  454. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  455. const char *buf, size_t count)
  456. {
  457. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  458. int nr = sensor_attr->index;
  459. struct i2c_client *client = to_i2c_client(dev);
  460. struct it87_data *data = i2c_get_clientdata(client);
  461. int val = simple_strtol(buf, NULL, 10);
  462. u8 reg;
  463. mutex_lock(&data->update_lock);
  464. reg = it87_read_value(client, IT87_REG_FAN_DIV);
  465. switch (nr) {
  466. case 0: data->fan_div[nr] = reg & 0x07; break;
  467. case 1: data->fan_div[nr] = (reg >> 3) & 0x07; break;
  468. case 2: data->fan_div[nr] = (reg & 0x40) ? 3 : 1; break;
  469. }
  470. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  471. it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
  472. mutex_unlock(&data->update_lock);
  473. return count;
  474. }
  475. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  476. const char *buf, size_t count)
  477. {
  478. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  479. int nr = sensor_attr->index;
  480. struct i2c_client *client = to_i2c_client(dev);
  481. struct it87_data *data = i2c_get_clientdata(client);
  482. unsigned long val = simple_strtoul(buf, NULL, 10);
  483. int min;
  484. u8 old;
  485. mutex_lock(&data->update_lock);
  486. old = it87_read_value(client, IT87_REG_FAN_DIV);
  487. /* Save fan min limit */
  488. min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
  489. switch (nr) {
  490. case 0:
  491. case 1:
  492. data->fan_div[nr] = DIV_TO_REG(val);
  493. break;
  494. case 2:
  495. if (val < 8)
  496. data->fan_div[nr] = 1;
  497. else
  498. data->fan_div[nr] = 3;
  499. }
  500. val = old & 0x80;
  501. val |= (data->fan_div[0] & 0x07);
  502. val |= (data->fan_div[1] & 0x07) << 3;
  503. if (data->fan_div[2] == 3)
  504. val |= 0x1 << 6;
  505. it87_write_value(client, IT87_REG_FAN_DIV, val);
  506. /* Restore fan min limit */
  507. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  508. it87_write_value(client, IT87_REG_FAN_MIN(nr), data->fan_min[nr]);
  509. mutex_unlock(&data->update_lock);
  510. return count;
  511. }
  512. static ssize_t set_pwm_enable(struct device *dev,
  513. struct device_attribute *attr, const char *buf, size_t count)
  514. {
  515. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  516. int nr = sensor_attr->index;
  517. struct i2c_client *client = to_i2c_client(dev);
  518. struct it87_data *data = i2c_get_clientdata(client);
  519. int val = simple_strtol(buf, NULL, 10);
  520. mutex_lock(&data->update_lock);
  521. if (val == 0) {
  522. int tmp;
  523. /* make sure the fan is on when in on/off mode */
  524. tmp = it87_read_value(client, IT87_REG_FAN_CTL);
  525. it87_write_value(client, IT87_REG_FAN_CTL, tmp | (1 << nr));
  526. /* set on/off mode */
  527. data->fan_main_ctrl &= ~(1 << nr);
  528. it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
  529. } else if (val == 1) {
  530. /* set SmartGuardian mode */
  531. data->fan_main_ctrl |= (1 << nr);
  532. it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
  533. /* set saved pwm value, clear FAN_CTLX PWM mode bit */
  534. it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
  535. } else {
  536. mutex_unlock(&data->update_lock);
  537. return -EINVAL;
  538. }
  539. mutex_unlock(&data->update_lock);
  540. return count;
  541. }
  542. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  543. const char *buf, size_t count)
  544. {
  545. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  546. int nr = sensor_attr->index;
  547. struct i2c_client *client = to_i2c_client(dev);
  548. struct it87_data *data = i2c_get_clientdata(client);
  549. int val = simple_strtol(buf, NULL, 10);
  550. if (val < 0 || val > 255)
  551. return -EINVAL;
  552. mutex_lock(&data->update_lock);
  553. data->manual_pwm_ctl[nr] = val;
  554. if (data->fan_main_ctrl & (1 << nr))
  555. it87_write_value(client, IT87_REG_PWM(nr), PWM_TO_REG(data->manual_pwm_ctl[nr]));
  556. mutex_unlock(&data->update_lock);
  557. return count;
  558. }
  559. static ssize_t set_pwm_freq(struct device *dev,
  560. struct device_attribute *attr, const char *buf, size_t count)
  561. {
  562. struct i2c_client *client = to_i2c_client(dev);
  563. struct it87_data *data = i2c_get_clientdata(client);
  564. unsigned long val = simple_strtoul(buf, NULL, 10);
  565. int i;
  566. /* Search for the nearest available frequency */
  567. for (i = 0; i < 7; i++) {
  568. if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
  569. break;
  570. }
  571. mutex_lock(&data->update_lock);
  572. data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL) & 0x8f;
  573. data->fan_ctl |= i << 4;
  574. it87_write_value(client, IT87_REG_FAN_CTL, data->fan_ctl);
  575. mutex_unlock(&data->update_lock);
  576. return count;
  577. }
  578. #define show_fan_offset(offset) \
  579. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  580. show_fan, NULL, offset - 1); \
  581. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  582. show_fan_min, set_fan_min, offset - 1); \
  583. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  584. show_fan_div, set_fan_div, offset - 1);
  585. show_fan_offset(1);
  586. show_fan_offset(2);
  587. show_fan_offset(3);
  588. #define show_pwm_offset(offset) \
  589. static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
  590. show_pwm_enable, set_pwm_enable, offset - 1); \
  591. static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  592. show_pwm, set_pwm, offset - 1); \
  593. static DEVICE_ATTR(pwm##offset##_freq, \
  594. (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
  595. show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL));
  596. show_pwm_offset(1);
  597. show_pwm_offset(2);
  598. show_pwm_offset(3);
  599. /* A different set of callbacks for 16-bit fans */
  600. static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
  601. char *buf)
  602. {
  603. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  604. int nr = sensor_attr->index;
  605. struct it87_data *data = it87_update_device(dev);
  606. return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
  607. }
  608. static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
  609. char *buf)
  610. {
  611. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  612. int nr = sensor_attr->index;
  613. struct it87_data *data = it87_update_device(dev);
  614. return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
  615. }
  616. static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  620. int nr = sensor_attr->index;
  621. struct i2c_client *client = to_i2c_client(dev);
  622. struct it87_data *data = i2c_get_clientdata(client);
  623. int val = simple_strtol(buf, NULL, 10);
  624. mutex_lock(&data->update_lock);
  625. data->fan_min[nr] = FAN16_TO_REG(val);
  626. it87_write_value(client, IT87_REG_FAN_MIN(nr),
  627. data->fan_min[nr] & 0xff);
  628. it87_write_value(client, IT87_REG_FANX_MIN(nr),
  629. data->fan_min[nr] >> 8);
  630. mutex_unlock(&data->update_lock);
  631. return count;
  632. }
  633. /* We want to use the same sysfs file names as 8-bit fans, but we need
  634. different variable names, so we have to use SENSOR_ATTR instead of
  635. SENSOR_DEVICE_ATTR. */
  636. #define show_fan16_offset(offset) \
  637. static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
  638. = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
  639. show_fan16, NULL, offset - 1); \
  640. static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
  641. = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  642. show_fan16_min, set_fan16_min, offset - 1)
  643. show_fan16_offset(1);
  644. show_fan16_offset(2);
  645. show_fan16_offset(3);
  646. /* Alarms */
  647. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  648. {
  649. struct it87_data *data = it87_update_device(dev);
  650. return sprintf(buf, "%u\n", data->alarms);
  651. }
  652. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  653. static ssize_t
  654. show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  655. {
  656. struct it87_data *data = it87_update_device(dev);
  657. return sprintf(buf, "%u\n", data->vrm);
  658. }
  659. static ssize_t
  660. store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  661. {
  662. struct i2c_client *client = to_i2c_client(dev);
  663. struct it87_data *data = i2c_get_clientdata(client);
  664. u32 val;
  665. val = simple_strtoul(buf, NULL, 10);
  666. data->vrm = val;
  667. return count;
  668. }
  669. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  670. static ssize_t
  671. show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
  672. {
  673. struct it87_data *data = it87_update_device(dev);
  674. return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
  675. }
  676. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  677. static struct attribute *it87_attributes[] = {
  678. &sensor_dev_attr_in0_input.dev_attr.attr,
  679. &sensor_dev_attr_in1_input.dev_attr.attr,
  680. &sensor_dev_attr_in2_input.dev_attr.attr,
  681. &sensor_dev_attr_in3_input.dev_attr.attr,
  682. &sensor_dev_attr_in4_input.dev_attr.attr,
  683. &sensor_dev_attr_in5_input.dev_attr.attr,
  684. &sensor_dev_attr_in6_input.dev_attr.attr,
  685. &sensor_dev_attr_in7_input.dev_attr.attr,
  686. &sensor_dev_attr_in8_input.dev_attr.attr,
  687. &sensor_dev_attr_in0_min.dev_attr.attr,
  688. &sensor_dev_attr_in1_min.dev_attr.attr,
  689. &sensor_dev_attr_in2_min.dev_attr.attr,
  690. &sensor_dev_attr_in3_min.dev_attr.attr,
  691. &sensor_dev_attr_in4_min.dev_attr.attr,
  692. &sensor_dev_attr_in5_min.dev_attr.attr,
  693. &sensor_dev_attr_in6_min.dev_attr.attr,
  694. &sensor_dev_attr_in7_min.dev_attr.attr,
  695. &sensor_dev_attr_in0_max.dev_attr.attr,
  696. &sensor_dev_attr_in1_max.dev_attr.attr,
  697. &sensor_dev_attr_in2_max.dev_attr.attr,
  698. &sensor_dev_attr_in3_max.dev_attr.attr,
  699. &sensor_dev_attr_in4_max.dev_attr.attr,
  700. &sensor_dev_attr_in5_max.dev_attr.attr,
  701. &sensor_dev_attr_in6_max.dev_attr.attr,
  702. &sensor_dev_attr_in7_max.dev_attr.attr,
  703. &sensor_dev_attr_temp1_input.dev_attr.attr,
  704. &sensor_dev_attr_temp2_input.dev_attr.attr,
  705. &sensor_dev_attr_temp3_input.dev_attr.attr,
  706. &sensor_dev_attr_temp1_max.dev_attr.attr,
  707. &sensor_dev_attr_temp2_max.dev_attr.attr,
  708. &sensor_dev_attr_temp3_max.dev_attr.attr,
  709. &sensor_dev_attr_temp1_min.dev_attr.attr,
  710. &sensor_dev_attr_temp2_min.dev_attr.attr,
  711. &sensor_dev_attr_temp3_min.dev_attr.attr,
  712. &sensor_dev_attr_temp1_type.dev_attr.attr,
  713. &sensor_dev_attr_temp2_type.dev_attr.attr,
  714. &sensor_dev_attr_temp3_type.dev_attr.attr,
  715. &dev_attr_alarms.attr,
  716. NULL
  717. };
  718. static const struct attribute_group it87_group = {
  719. .attrs = it87_attributes,
  720. };
  721. static struct attribute *it87_attributes_opt[] = {
  722. &sensor_dev_attr_fan1_input16.dev_attr.attr,
  723. &sensor_dev_attr_fan1_min16.dev_attr.attr,
  724. &sensor_dev_attr_fan2_input16.dev_attr.attr,
  725. &sensor_dev_attr_fan2_min16.dev_attr.attr,
  726. &sensor_dev_attr_fan3_input16.dev_attr.attr,
  727. &sensor_dev_attr_fan3_min16.dev_attr.attr,
  728. &sensor_dev_attr_fan1_input.dev_attr.attr,
  729. &sensor_dev_attr_fan1_min.dev_attr.attr,
  730. &sensor_dev_attr_fan1_div.dev_attr.attr,
  731. &sensor_dev_attr_fan2_input.dev_attr.attr,
  732. &sensor_dev_attr_fan2_min.dev_attr.attr,
  733. &sensor_dev_attr_fan2_div.dev_attr.attr,
  734. &sensor_dev_attr_fan3_input.dev_attr.attr,
  735. &sensor_dev_attr_fan3_min.dev_attr.attr,
  736. &sensor_dev_attr_fan3_div.dev_attr.attr,
  737. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  738. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  739. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  740. &sensor_dev_attr_pwm1.dev_attr.attr,
  741. &sensor_dev_attr_pwm2.dev_attr.attr,
  742. &sensor_dev_attr_pwm3.dev_attr.attr,
  743. &dev_attr_vrm.attr,
  744. &dev_attr_cpu0_vid.attr,
  745. NULL
  746. };
  747. static const struct attribute_group it87_group_opt = {
  748. .attrs = it87_attributes_opt,
  749. };
  750. /* SuperIO detection - will change isa_address if a chip is found */
  751. static int __init it87_find(unsigned short *address)
  752. {
  753. int err = -ENODEV;
  754. superio_enter();
  755. chip_type = superio_inw(DEVID);
  756. if (chip_type != IT8712F_DEVID
  757. && chip_type != IT8716F_DEVID
  758. && chip_type != IT8726F_DEVID
  759. && chip_type != IT8718F_DEVID
  760. && chip_type != IT8705F_DEVID)
  761. goto exit;
  762. superio_select(PME);
  763. if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
  764. pr_info("it87: Device not activated, skipping\n");
  765. goto exit;
  766. }
  767. *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
  768. if (*address == 0) {
  769. pr_info("it87: Base address not set, skipping\n");
  770. goto exit;
  771. }
  772. err = 0;
  773. pr_info("it87: Found IT%04xF chip at 0x%x, revision %d\n",
  774. chip_type, *address, superio_inb(DEVREV) & 0x0f);
  775. /* Read GPIO config and VID value from LDN 7 (GPIO) */
  776. if (chip_type != IT8705F_DEVID) {
  777. int reg;
  778. superio_select(GPIO);
  779. if (chip_type == it8718)
  780. vid_value = superio_inb(IT87_SIO_VID_REG);
  781. reg = superio_inb(IT87_SIO_PINX2_REG);
  782. if (reg & (1 << 0))
  783. pr_info("it87: in3 is VCC (+5V)\n");
  784. if (reg & (1 << 1))
  785. pr_info("it87: in7 is VCCH (+5V Stand-By)\n");
  786. }
  787. exit:
  788. superio_exit();
  789. return err;
  790. }
  791. /* This function is called by i2c_probe */
  792. static int it87_detect(struct i2c_adapter *adapter)
  793. {
  794. struct i2c_client *new_client;
  795. struct it87_data *data;
  796. int err = 0;
  797. const char *name;
  798. int enable_pwm_interface;
  799. /* Reserve the ISA region */
  800. if (!request_region(isa_address, IT87_EXTENT,
  801. it87_isa_driver.driver.name)){
  802. err = -EBUSY;
  803. goto ERROR0;
  804. }
  805. if (!(data = kzalloc(sizeof(struct it87_data), GFP_KERNEL))) {
  806. err = -ENOMEM;
  807. goto ERROR1;
  808. }
  809. new_client = &data->client;
  810. i2c_set_clientdata(new_client, data);
  811. new_client->addr = isa_address;
  812. new_client->adapter = adapter;
  813. new_client->driver = &it87_isa_driver;
  814. /* Now, we do the remaining detection. */
  815. if ((it87_read_value(new_client, IT87_REG_CONFIG) & 0x80)
  816. || it87_read_value(new_client, IT87_REG_CHIPID) != 0x90) {
  817. err = -ENODEV;
  818. goto ERROR2;
  819. }
  820. /* Determine the chip type. */
  821. switch (chip_type) {
  822. case IT8712F_DEVID:
  823. data->type = it8712;
  824. name = "it8712";
  825. break;
  826. case IT8726F_DEVID:
  827. /* fall through */
  828. case IT8716F_DEVID:
  829. data->type = it8716;
  830. name = "it8716";
  831. break;
  832. case IT8718F_DEVID:
  833. data->type = it8718;
  834. name = "it8718";
  835. break;
  836. default:
  837. data->type = it87;
  838. name = "it87";
  839. }
  840. /* Fill in the remaining client fields and put it into the global list */
  841. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  842. mutex_init(&data->update_lock);
  843. /* Tell the I2C layer a new client has arrived */
  844. if ((err = i2c_attach_client(new_client)))
  845. goto ERROR2;
  846. /* Check PWM configuration */
  847. enable_pwm_interface = it87_check_pwm(new_client);
  848. /* Initialize the IT87 chip */
  849. it87_init_client(new_client, data);
  850. /* Register sysfs hooks */
  851. if ((err = sysfs_create_group(&new_client->dev.kobj, &it87_group)))
  852. goto ERROR3;
  853. /* Do not create fan files for disabled fans */
  854. if (data->type == it8716 || data->type == it8718) {
  855. /* 16-bit tachometers */
  856. if (data->has_fan & (1 << 0)) {
  857. if ((err = device_create_file(&new_client->dev,
  858. &sensor_dev_attr_fan1_input16.dev_attr))
  859. || (err = device_create_file(&new_client->dev,
  860. &sensor_dev_attr_fan1_min16.dev_attr)))
  861. goto ERROR4;
  862. }
  863. if (data->has_fan & (1 << 1)) {
  864. if ((err = device_create_file(&new_client->dev,
  865. &sensor_dev_attr_fan2_input16.dev_attr))
  866. || (err = device_create_file(&new_client->dev,
  867. &sensor_dev_attr_fan2_min16.dev_attr)))
  868. goto ERROR4;
  869. }
  870. if (data->has_fan & (1 << 2)) {
  871. if ((err = device_create_file(&new_client->dev,
  872. &sensor_dev_attr_fan3_input16.dev_attr))
  873. || (err = device_create_file(&new_client->dev,
  874. &sensor_dev_attr_fan3_min16.dev_attr)))
  875. goto ERROR4;
  876. }
  877. } else {
  878. /* 8-bit tachometers with clock divider */
  879. if (data->has_fan & (1 << 0)) {
  880. if ((err = device_create_file(&new_client->dev,
  881. &sensor_dev_attr_fan1_input.dev_attr))
  882. || (err = device_create_file(&new_client->dev,
  883. &sensor_dev_attr_fan1_min.dev_attr))
  884. || (err = device_create_file(&new_client->dev,
  885. &sensor_dev_attr_fan1_div.dev_attr)))
  886. goto ERROR4;
  887. }
  888. if (data->has_fan & (1 << 1)) {
  889. if ((err = device_create_file(&new_client->dev,
  890. &sensor_dev_attr_fan2_input.dev_attr))
  891. || (err = device_create_file(&new_client->dev,
  892. &sensor_dev_attr_fan2_min.dev_attr))
  893. || (err = device_create_file(&new_client->dev,
  894. &sensor_dev_attr_fan2_div.dev_attr)))
  895. goto ERROR4;
  896. }
  897. if (data->has_fan & (1 << 2)) {
  898. if ((err = device_create_file(&new_client->dev,
  899. &sensor_dev_attr_fan3_input.dev_attr))
  900. || (err = device_create_file(&new_client->dev,
  901. &sensor_dev_attr_fan3_min.dev_attr))
  902. || (err = device_create_file(&new_client->dev,
  903. &sensor_dev_attr_fan3_div.dev_attr)))
  904. goto ERROR4;
  905. }
  906. }
  907. if (enable_pwm_interface) {
  908. if ((err = device_create_file(&new_client->dev,
  909. &sensor_dev_attr_pwm1_enable.dev_attr))
  910. || (err = device_create_file(&new_client->dev,
  911. &sensor_dev_attr_pwm2_enable.dev_attr))
  912. || (err = device_create_file(&new_client->dev,
  913. &sensor_dev_attr_pwm3_enable.dev_attr))
  914. || (err = device_create_file(&new_client->dev,
  915. &sensor_dev_attr_pwm1.dev_attr))
  916. || (err = device_create_file(&new_client->dev,
  917. &sensor_dev_attr_pwm2.dev_attr))
  918. || (err = device_create_file(&new_client->dev,
  919. &sensor_dev_attr_pwm3.dev_attr))
  920. || (err = device_create_file(&new_client->dev,
  921. &dev_attr_pwm1_freq))
  922. || (err = device_create_file(&new_client->dev,
  923. &dev_attr_pwm2_freq))
  924. || (err = device_create_file(&new_client->dev,
  925. &dev_attr_pwm3_freq)))
  926. goto ERROR4;
  927. }
  928. if (data->type == it8712 || data->type == it8716
  929. || data->type == it8718) {
  930. data->vrm = vid_which_vrm();
  931. /* VID reading from Super-I/O config space if available */
  932. data->vid = vid_value;
  933. if ((err = device_create_file(&new_client->dev,
  934. &dev_attr_vrm))
  935. || (err = device_create_file(&new_client->dev,
  936. &dev_attr_cpu0_vid)))
  937. goto ERROR4;
  938. }
  939. data->class_dev = hwmon_device_register(&new_client->dev);
  940. if (IS_ERR(data->class_dev)) {
  941. err = PTR_ERR(data->class_dev);
  942. goto ERROR4;
  943. }
  944. return 0;
  945. ERROR4:
  946. sysfs_remove_group(&new_client->dev.kobj, &it87_group);
  947. sysfs_remove_group(&new_client->dev.kobj, &it87_group_opt);
  948. ERROR3:
  949. i2c_detach_client(new_client);
  950. ERROR2:
  951. kfree(data);
  952. ERROR1:
  953. release_region(isa_address, IT87_EXTENT);
  954. ERROR0:
  955. return err;
  956. }
  957. static int it87_detach_client(struct i2c_client *client)
  958. {
  959. struct it87_data *data = i2c_get_clientdata(client);
  960. int err;
  961. hwmon_device_unregister(data->class_dev);
  962. sysfs_remove_group(&client->dev.kobj, &it87_group);
  963. sysfs_remove_group(&client->dev.kobj, &it87_group_opt);
  964. if ((err = i2c_detach_client(client)))
  965. return err;
  966. release_region(client->addr, IT87_EXTENT);
  967. kfree(data);
  968. return 0;
  969. }
  970. /* Must be called with data->update_lock held, except during initialization.
  971. We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  972. would slow down the IT87 access and should not be necessary. */
  973. static int it87_read_value(struct i2c_client *client, u8 reg)
  974. {
  975. outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
  976. return inb_p(client->addr + IT87_DATA_REG_OFFSET);
  977. }
  978. /* Must be called with data->update_lock held, except during initialization.
  979. We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  980. would slow down the IT87 access and should not be necessary. */
  981. static void it87_write_value(struct i2c_client *client, u8 reg, u8 value)
  982. {
  983. outb_p(reg, client->addr + IT87_ADDR_REG_OFFSET);
  984. outb_p(value, client->addr + IT87_DATA_REG_OFFSET);
  985. }
  986. /* Return 1 if and only if the PWM interface is safe to use */
  987. static int it87_check_pwm(struct i2c_client *client)
  988. {
  989. /* Some BIOSes fail to correctly configure the IT87 fans. All fans off
  990. * and polarity set to active low is sign that this is the case so we
  991. * disable pwm control to protect the user. */
  992. int tmp = it87_read_value(client, IT87_REG_FAN_CTL);
  993. if ((tmp & 0x87) == 0) {
  994. if (fix_pwm_polarity) {
  995. /* The user asks us to attempt a chip reconfiguration.
  996. * This means switching to active high polarity and
  997. * inverting all fan speed values. */
  998. int i;
  999. u8 pwm[3];
  1000. for (i = 0; i < 3; i++)
  1001. pwm[i] = it87_read_value(client,
  1002. IT87_REG_PWM(i));
  1003. /* If any fan is in automatic pwm mode, the polarity
  1004. * might be correct, as suspicious as it seems, so we
  1005. * better don't change anything (but still disable the
  1006. * PWM interface). */
  1007. if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
  1008. dev_info(&client->dev, "Reconfiguring PWM to "
  1009. "active high polarity\n");
  1010. it87_write_value(client, IT87_REG_FAN_CTL,
  1011. tmp | 0x87);
  1012. for (i = 0; i < 3; i++)
  1013. it87_write_value(client,
  1014. IT87_REG_PWM(i),
  1015. 0x7f & ~pwm[i]);
  1016. return 1;
  1017. }
  1018. dev_info(&client->dev, "PWM configuration is "
  1019. "too broken to be fixed\n");
  1020. }
  1021. dev_info(&client->dev, "Detected broken BIOS "
  1022. "defaults, disabling PWM interface\n");
  1023. return 0;
  1024. } else if (fix_pwm_polarity) {
  1025. dev_info(&client->dev, "PWM configuration looks "
  1026. "sane, won't touch\n");
  1027. }
  1028. return 1;
  1029. }
  1030. /* Called when we have found a new IT87. */
  1031. static void it87_init_client(struct i2c_client *client, struct it87_data *data)
  1032. {
  1033. int tmp, i;
  1034. /* initialize to sane defaults:
  1035. * - if the chip is in manual pwm mode, this will be overwritten with
  1036. * the actual settings on the chip (so in this case, initialization
  1037. * is not needed)
  1038. * - if in automatic or on/off mode, we could switch to manual mode,
  1039. * read the registers and set manual_pwm_ctl accordingly, but currently
  1040. * this is not implemented, so we initialize to something sane */
  1041. for (i = 0; i < 3; i++) {
  1042. data->manual_pwm_ctl[i] = 0xff;
  1043. }
  1044. /* Some chips seem to have default value 0xff for all limit
  1045. * registers. For low voltage limits it makes no sense and triggers
  1046. * alarms, so change to 0 instead. For high temperature limits, it
  1047. * means -1 degree C, which surprisingly doesn't trigger an alarm,
  1048. * but is still confusing, so change to 127 degrees C. */
  1049. for (i = 0; i < 8; i++) {
  1050. tmp = it87_read_value(client, IT87_REG_VIN_MIN(i));
  1051. if (tmp == 0xff)
  1052. it87_write_value(client, IT87_REG_VIN_MIN(i), 0);
  1053. }
  1054. for (i = 0; i < 3; i++) {
  1055. tmp = it87_read_value(client, IT87_REG_TEMP_HIGH(i));
  1056. if (tmp == 0xff)
  1057. it87_write_value(client, IT87_REG_TEMP_HIGH(i), 127);
  1058. }
  1059. /* Check if temperature channnels are reset manually or by some reason */
  1060. tmp = it87_read_value(client, IT87_REG_TEMP_ENABLE);
  1061. if ((tmp & 0x3f) == 0) {
  1062. /* Temp1,Temp3=thermistor; Temp2=thermal diode */
  1063. tmp = (tmp & 0xc0) | 0x2a;
  1064. it87_write_value(client, IT87_REG_TEMP_ENABLE, tmp);
  1065. }
  1066. data->sensor = tmp;
  1067. /* Check if voltage monitors are reset manually or by some reason */
  1068. tmp = it87_read_value(client, IT87_REG_VIN_ENABLE);
  1069. if ((tmp & 0xff) == 0) {
  1070. /* Enable all voltage monitors */
  1071. it87_write_value(client, IT87_REG_VIN_ENABLE, 0xff);
  1072. }
  1073. /* Check if tachometers are reset manually or by some reason */
  1074. data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
  1075. if ((data->fan_main_ctrl & 0x70) == 0) {
  1076. /* Enable all fan tachometers */
  1077. data->fan_main_ctrl |= 0x70;
  1078. it87_write_value(client, IT87_REG_FAN_MAIN_CTRL, data->fan_main_ctrl);
  1079. }
  1080. data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
  1081. /* Set tachometers to 16-bit mode if needed */
  1082. if (data->type == it8716 || data->type == it8718) {
  1083. tmp = it87_read_value(client, IT87_REG_FAN_16BIT);
  1084. if (~tmp & 0x07 & data->has_fan) {
  1085. dev_dbg(&client->dev,
  1086. "Setting fan1-3 to 16-bit mode\n");
  1087. it87_write_value(client, IT87_REG_FAN_16BIT,
  1088. tmp | 0x07);
  1089. }
  1090. }
  1091. /* Set current fan mode registers and the default settings for the
  1092. * other mode registers */
  1093. for (i = 0; i < 3; i++) {
  1094. if (data->fan_main_ctrl & (1 << i)) {
  1095. /* pwm mode */
  1096. tmp = it87_read_value(client, IT87_REG_PWM(i));
  1097. if (tmp & 0x80) {
  1098. /* automatic pwm - not yet implemented, but
  1099. * leave the settings made by the BIOS alone
  1100. * until a change is requested via the sysfs
  1101. * interface */
  1102. } else {
  1103. /* manual pwm */
  1104. data->manual_pwm_ctl[i] = PWM_FROM_REG(tmp);
  1105. }
  1106. }
  1107. }
  1108. /* Start monitoring */
  1109. it87_write_value(client, IT87_REG_CONFIG,
  1110. (it87_read_value(client, IT87_REG_CONFIG) & 0x36)
  1111. | (update_vbat ? 0x41 : 0x01));
  1112. }
  1113. static struct it87_data *it87_update_device(struct device *dev)
  1114. {
  1115. struct i2c_client *client = to_i2c_client(dev);
  1116. struct it87_data *data = i2c_get_clientdata(client);
  1117. int i;
  1118. mutex_lock(&data->update_lock);
  1119. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  1120. || !data->valid) {
  1121. if (update_vbat) {
  1122. /* Cleared after each update, so reenable. Value
  1123. returned by this read will be previous value */
  1124. it87_write_value(client, IT87_REG_CONFIG,
  1125. it87_read_value(client, IT87_REG_CONFIG) | 0x40);
  1126. }
  1127. for (i = 0; i <= 7; i++) {
  1128. data->in[i] =
  1129. it87_read_value(client, IT87_REG_VIN(i));
  1130. data->in_min[i] =
  1131. it87_read_value(client, IT87_REG_VIN_MIN(i));
  1132. data->in_max[i] =
  1133. it87_read_value(client, IT87_REG_VIN_MAX(i));
  1134. }
  1135. /* in8 (battery) has no limit registers */
  1136. data->in[8] =
  1137. it87_read_value(client, IT87_REG_VIN(8));
  1138. for (i = 0; i < 3; i++) {
  1139. /* Skip disabled fans */
  1140. if (!(data->has_fan & (1 << i)))
  1141. continue;
  1142. data->fan_min[i] =
  1143. it87_read_value(client, IT87_REG_FAN_MIN(i));
  1144. data->fan[i] = it87_read_value(client,
  1145. IT87_REG_FAN(i));
  1146. /* Add high byte if in 16-bit mode */
  1147. if (data->type == it8716 || data->type == it8718) {
  1148. data->fan[i] |= it87_read_value(client,
  1149. IT87_REG_FANX(i)) << 8;
  1150. data->fan_min[i] |= it87_read_value(client,
  1151. IT87_REG_FANX_MIN(i)) << 8;
  1152. }
  1153. }
  1154. for (i = 0; i < 3; i++) {
  1155. data->temp[i] =
  1156. it87_read_value(client, IT87_REG_TEMP(i));
  1157. data->temp_high[i] =
  1158. it87_read_value(client, IT87_REG_TEMP_HIGH(i));
  1159. data->temp_low[i] =
  1160. it87_read_value(client, IT87_REG_TEMP_LOW(i));
  1161. }
  1162. /* Newer chips don't have clock dividers */
  1163. if ((data->has_fan & 0x07) && data->type != it8716
  1164. && data->type != it8718) {
  1165. i = it87_read_value(client, IT87_REG_FAN_DIV);
  1166. data->fan_div[0] = i & 0x07;
  1167. data->fan_div[1] = (i >> 3) & 0x07;
  1168. data->fan_div[2] = (i & 0x40) ? 3 : 1;
  1169. }
  1170. data->alarms =
  1171. it87_read_value(client, IT87_REG_ALARM1) |
  1172. (it87_read_value(client, IT87_REG_ALARM2) << 8) |
  1173. (it87_read_value(client, IT87_REG_ALARM3) << 16);
  1174. data->fan_main_ctrl = it87_read_value(client, IT87_REG_FAN_MAIN_CTRL);
  1175. data->fan_ctl = it87_read_value(client, IT87_REG_FAN_CTL);
  1176. data->sensor = it87_read_value(client, IT87_REG_TEMP_ENABLE);
  1177. /* The 8705 does not have VID capability */
  1178. if (data->type == it8712 || data->type == it8716) {
  1179. data->vid = it87_read_value(client, IT87_REG_VID);
  1180. /* The older IT8712F revisions had only 5 VID pins,
  1181. but we assume it is always safe to read 6 bits. */
  1182. data->vid &= 0x3f;
  1183. }
  1184. data->last_updated = jiffies;
  1185. data->valid = 1;
  1186. }
  1187. mutex_unlock(&data->update_lock);
  1188. return data;
  1189. }
  1190. static int __init sm_it87_init(void)
  1191. {
  1192. int res;
  1193. if ((res = it87_find(&isa_address)))
  1194. return res;
  1195. return i2c_isa_add_driver(&it87_isa_driver);
  1196. }
  1197. static void __exit sm_it87_exit(void)
  1198. {
  1199. i2c_isa_del_driver(&it87_isa_driver);
  1200. }
  1201. MODULE_AUTHOR("Chris Gauthron <chrisg@0-in.com>, "
  1202. "Jean Delvare <khali@linux-fr.org>");
  1203. MODULE_DESCRIPTION("IT8705F/8712F/8716F/8718F/8726F, SiS950 driver");
  1204. module_param(update_vbat, bool, 0);
  1205. MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
  1206. module_param(fix_pwm_polarity, bool, 0);
  1207. MODULE_PARM_DESC(fix_pwm_polarity, "Force PWM polarity to active high (DANGEROUS)");
  1208. MODULE_LICENSE("GPL");
  1209. module_init(sm_it87_init);
  1210. module_exit(sm_it87_exit);