it87.c 41 KB

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