it87.c 64 KB

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