it87.c 42 KB

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