w83627hf.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  1. /*
  2. w83627hf.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998 - 2003 Frodo Looijaard <frodol@dds.nl>,
  5. Philip Edelbrock <phil@netroedge.com>,
  6. and Mark Studebaker <mdsxyz123@yahoo.com>
  7. Ported to 2.6 by Bernhard C. Schrenk <clemy@clemy.org>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. /*
  21. Supports following chips:
  22. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  23. w83627hf 9 3 2 3 0x20 0x5ca3 no yes(LPC)
  24. w83627thf 7 3 3 3 0x90 0x5ca3 no yes(LPC)
  25. w83637hf 7 3 3 3 0x80 0x5ca3 no yes(LPC)
  26. w83697hf 8 2 2 2 0x60 0x5ca3 no yes(LPC)
  27. For other winbond chips, and for i2c support in the above chips,
  28. use w83781d.c.
  29. Note: automatic ("cruise") fan control for 697, 637 & 627thf not
  30. supported yet.
  31. */
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <linux/jiffies.h>
  36. #include <linux/i2c.h>
  37. #include <linux/i2c-isa.h>
  38. #include <linux/hwmon.h>
  39. #include <linux/hwmon-vid.h>
  40. #include <linux/err.h>
  41. #include <asm/io.h>
  42. #include "lm75.h"
  43. static u16 force_addr;
  44. module_param(force_addr, ushort, 0);
  45. MODULE_PARM_DESC(force_addr,
  46. "Initialize the base address of the sensors");
  47. static u8 force_i2c = 0x1f;
  48. module_param(force_i2c, byte, 0);
  49. MODULE_PARM_DESC(force_i2c,
  50. "Initialize the i2c address of the sensors");
  51. /* The actual ISA address is read from Super-I/O configuration space */
  52. static unsigned short address;
  53. /* Insmod parameters */
  54. enum chips { any_chip, w83627hf, w83627thf, w83697hf, w83637hf };
  55. static int init = 1;
  56. module_param(init, bool, 0);
  57. MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
  58. /* modified from kernel/include/traps.c */
  59. static int REG; /* The register to read/write */
  60. #define DEV 0x07 /* Register: Logical device select */
  61. static int VAL; /* The value to read/write */
  62. /* logical device numbers for superio_select (below) */
  63. #define W83627HF_LD_FDC 0x00
  64. #define W83627HF_LD_PRT 0x01
  65. #define W83627HF_LD_UART1 0x02
  66. #define W83627HF_LD_UART2 0x03
  67. #define W83627HF_LD_KBC 0x05
  68. #define W83627HF_LD_CIR 0x06 /* w83627hf only */
  69. #define W83627HF_LD_GAME 0x07
  70. #define W83627HF_LD_MIDI 0x07
  71. #define W83627HF_LD_GPIO1 0x07
  72. #define W83627HF_LD_GPIO5 0x07 /* w83627thf only */
  73. #define W83627HF_LD_GPIO2 0x08
  74. #define W83627HF_LD_GPIO3 0x09
  75. #define W83627HF_LD_GPIO4 0x09 /* w83627thf only */
  76. #define W83627HF_LD_ACPI 0x0a
  77. #define W83627HF_LD_HWM 0x0b
  78. #define DEVID 0x20 /* Register: Device ID */
  79. #define W83627THF_GPIO5_EN 0x30 /* w83627thf only */
  80. #define W83627THF_GPIO5_IOSR 0xf3 /* w83627thf only */
  81. #define W83627THF_GPIO5_DR 0xf4 /* w83627thf only */
  82. static inline void
  83. superio_outb(int reg, int val)
  84. {
  85. outb(reg, REG);
  86. outb(val, VAL);
  87. }
  88. static inline int
  89. superio_inb(int reg)
  90. {
  91. outb(reg, REG);
  92. return inb(VAL);
  93. }
  94. static inline void
  95. superio_select(int ld)
  96. {
  97. outb(DEV, REG);
  98. outb(ld, VAL);
  99. }
  100. static inline void
  101. superio_enter(void)
  102. {
  103. outb(0x87, REG);
  104. outb(0x87, REG);
  105. }
  106. static inline void
  107. superio_exit(void)
  108. {
  109. outb(0xAA, REG);
  110. }
  111. #define W627_DEVID 0x52
  112. #define W627THF_DEVID 0x82
  113. #define W697_DEVID 0x60
  114. #define W637_DEVID 0x70
  115. #define WINB_ACT_REG 0x30
  116. #define WINB_BASE_REG 0x60
  117. /* Constants specified below */
  118. /* Length of ISA address segment */
  119. #define WINB_EXTENT 8
  120. /* Where are the ISA address/data registers relative to the base address */
  121. #define W83781D_ADDR_REG_OFFSET 5
  122. #define W83781D_DATA_REG_OFFSET 6
  123. /* The W83781D registers */
  124. /* The W83782D registers for nr=7,8 are in bank 5 */
  125. #define W83781D_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
  126. (0x554 + (((nr) - 7) * 2)))
  127. #define W83781D_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
  128. (0x555 + (((nr) - 7) * 2)))
  129. #define W83781D_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
  130. (0x550 + (nr) - 7))
  131. #define W83781D_REG_FAN_MIN(nr) (0x3a + (nr))
  132. #define W83781D_REG_FAN(nr) (0x27 + (nr))
  133. #define W83781D_REG_TEMP2_CONFIG 0x152
  134. #define W83781D_REG_TEMP3_CONFIG 0x252
  135. #define W83781D_REG_TEMP(nr) ((nr == 3) ? (0x0250) : \
  136. ((nr == 2) ? (0x0150) : \
  137. (0x27)))
  138. #define W83781D_REG_TEMP_HYST(nr) ((nr == 3) ? (0x253) : \
  139. ((nr == 2) ? (0x153) : \
  140. (0x3A)))
  141. #define W83781D_REG_TEMP_OVER(nr) ((nr == 3) ? (0x255) : \
  142. ((nr == 2) ? (0x155) : \
  143. (0x39)))
  144. #define W83781D_REG_BANK 0x4E
  145. #define W83781D_REG_CONFIG 0x40
  146. #define W83781D_REG_ALARM1 0x41
  147. #define W83781D_REG_ALARM2 0x42
  148. #define W83781D_REG_ALARM3 0x450
  149. #define W83781D_REG_IRQ 0x4C
  150. #define W83781D_REG_BEEP_CONFIG 0x4D
  151. #define W83781D_REG_BEEP_INTS1 0x56
  152. #define W83781D_REG_BEEP_INTS2 0x57
  153. #define W83781D_REG_BEEP_INTS3 0x453
  154. #define W83781D_REG_VID_FANDIV 0x47
  155. #define W83781D_REG_CHIPID 0x49
  156. #define W83781D_REG_WCHIPID 0x58
  157. #define W83781D_REG_CHIPMAN 0x4F
  158. #define W83781D_REG_PIN 0x4B
  159. #define W83781D_REG_VBAT 0x5D
  160. #define W83627HF_REG_PWM1 0x5A
  161. #define W83627HF_REG_PWM2 0x5B
  162. #define W83627HF_REG_PWMCLK12 0x5C
  163. #define W83627THF_REG_PWM1 0x01 /* 697HF and 637HF too */
  164. #define W83627THF_REG_PWM2 0x03 /* 697HF and 637HF too */
  165. #define W83627THF_REG_PWM3 0x11 /* 637HF too */
  166. #define W83627THF_REG_VRM_OVT_CFG 0x18 /* 637HF too */
  167. static const u8 regpwm_627hf[] = { W83627HF_REG_PWM1, W83627HF_REG_PWM2 };
  168. static const u8 regpwm[] = { W83627THF_REG_PWM1, W83627THF_REG_PWM2,
  169. W83627THF_REG_PWM3 };
  170. #define W836X7HF_REG_PWM(type, nr) (((type) == w83627hf) ? \
  171. regpwm_627hf[(nr) - 1] : regpwm[(nr) - 1])
  172. #define W83781D_REG_I2C_ADDR 0x48
  173. #define W83781D_REG_I2C_SUBADDR 0x4A
  174. /* Sensor selection */
  175. #define W83781D_REG_SCFG1 0x5D
  176. static const u8 BIT_SCFG1[] = { 0x02, 0x04, 0x08 };
  177. #define W83781D_REG_SCFG2 0x59
  178. static const u8 BIT_SCFG2[] = { 0x10, 0x20, 0x40 };
  179. #define W83781D_DEFAULT_BETA 3435
  180. /* Conversions. Limit checking is only done on the TO_REG
  181. variants. Note that you should be a bit careful with which arguments
  182. these macros are called: arguments may be evaluated more than once.
  183. Fixing this is just not worth it. */
  184. #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8)/16),0,255))
  185. #define IN_FROM_REG(val) ((val) * 16)
  186. static inline u8 FAN_TO_REG(long rpm, int div)
  187. {
  188. if (rpm == 0)
  189. return 255;
  190. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  191. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
  192. 254);
  193. }
  194. #define TEMP_MIN (-128000)
  195. #define TEMP_MAX ( 127000)
  196. /* TEMP: 0.001C/bit (-128C to +127C)
  197. REG: 1C/bit, two's complement */
  198. static u8 TEMP_TO_REG(int temp)
  199. {
  200. int ntemp = SENSORS_LIMIT(temp, TEMP_MIN, TEMP_MAX);
  201. ntemp += (ntemp<0 ? -500 : 500);
  202. return (u8)(ntemp / 1000);
  203. }
  204. static int TEMP_FROM_REG(u8 reg)
  205. {
  206. return (s8)reg * 1000;
  207. }
  208. #define FAN_FROM_REG(val,div) ((val)==0?-1:(val)==255?0:1350000/((val)*(div)))
  209. #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
  210. #define BEEP_MASK_FROM_REG(val) (val)
  211. #define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
  212. #define BEEP_ENABLE_TO_REG(val) ((val)?1:0)
  213. #define BEEP_ENABLE_FROM_REG(val) ((val)?1:0)
  214. #define DIV_FROM_REG(val) (1 << (val))
  215. static inline u8 DIV_TO_REG(long val)
  216. {
  217. int i;
  218. val = SENSORS_LIMIT(val, 1, 128) >> 1;
  219. for (i = 0; i < 7; i++) {
  220. if (val == 0)
  221. break;
  222. val >>= 1;
  223. }
  224. return ((u8) i);
  225. }
  226. /* For each registered chip, we need to keep some data in memory. That
  227. data is pointed to by w83627hf_list[NR]->data. The structure itself is
  228. dynamically allocated, at the same time when a new client is allocated. */
  229. struct w83627hf_data {
  230. struct i2c_client client;
  231. struct class_device *class_dev;
  232. struct semaphore lock;
  233. enum chips type;
  234. struct semaphore update_lock;
  235. char valid; /* !=0 if following fields are valid */
  236. unsigned long last_updated; /* In jiffies */
  237. struct i2c_client *lm75; /* for secondary I2C addresses */
  238. /* pointer to array of 2 subclients */
  239. u8 in[9]; /* Register value */
  240. u8 in_max[9]; /* Register value */
  241. u8 in_min[9]; /* Register value */
  242. u8 fan[3]; /* Register value */
  243. u8 fan_min[3]; /* Register value */
  244. u8 temp;
  245. u8 temp_max; /* Register value */
  246. u8 temp_max_hyst; /* Register value */
  247. u16 temp_add[2]; /* Register value */
  248. u16 temp_max_add[2]; /* Register value */
  249. u16 temp_max_hyst_add[2]; /* Register value */
  250. u8 fan_div[3]; /* Register encoding, shifted right */
  251. u8 vid; /* Register encoding, combined */
  252. u32 alarms; /* Register encoding, combined */
  253. u32 beep_mask; /* Register encoding, combined */
  254. u8 beep_enable; /* Boolean */
  255. u8 pwm[3]; /* Register value */
  256. u16 sens[3]; /* 782D/783S only.
  257. 1 = pentium diode; 2 = 3904 diode;
  258. 3000-5000 = thermistor beta.
  259. Default = 3435.
  260. Other Betas unimplemented */
  261. u8 vrm;
  262. u8 vrm_ovt; /* Register value, 627thf & 637hf only */
  263. };
  264. static int w83627hf_detect(struct i2c_adapter *adapter);
  265. static int w83627hf_detach_client(struct i2c_client *client);
  266. static int w83627hf_read_value(struct i2c_client *client, u16 register);
  267. static int w83627hf_write_value(struct i2c_client *client, u16 register,
  268. u16 value);
  269. static struct w83627hf_data *w83627hf_update_device(struct device *dev);
  270. static void w83627hf_init_client(struct i2c_client *client);
  271. static struct i2c_driver w83627hf_driver = {
  272. .owner = THIS_MODULE,
  273. .name = "w83627hf",
  274. .attach_adapter = w83627hf_detect,
  275. .detach_client = w83627hf_detach_client,
  276. };
  277. /* following are the sysfs callback functions */
  278. #define show_in_reg(reg) \
  279. static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
  280. { \
  281. struct w83627hf_data *data = w83627hf_update_device(dev); \
  282. return sprintf(buf,"%ld\n", (long)IN_FROM_REG(data->reg[nr])); \
  283. }
  284. show_in_reg(in)
  285. show_in_reg(in_min)
  286. show_in_reg(in_max)
  287. #define store_in_reg(REG, reg) \
  288. static ssize_t \
  289. store_in_##reg (struct device *dev, const char *buf, size_t count, int nr) \
  290. { \
  291. struct i2c_client *client = to_i2c_client(dev); \
  292. struct w83627hf_data *data = i2c_get_clientdata(client); \
  293. u32 val; \
  294. \
  295. val = simple_strtoul(buf, NULL, 10); \
  296. \
  297. down(&data->update_lock); \
  298. data->in_##reg[nr] = IN_TO_REG(val); \
  299. w83627hf_write_value(client, W83781D_REG_IN_##REG(nr), \
  300. data->in_##reg[nr]); \
  301. \
  302. up(&data->update_lock); \
  303. return count; \
  304. }
  305. store_in_reg(MIN, min)
  306. store_in_reg(MAX, max)
  307. #define sysfs_in_offset(offset) \
  308. static ssize_t \
  309. show_regs_in_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  310. { \
  311. return show_in(dev, buf, offset); \
  312. } \
  313. static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_regs_in_##offset, NULL);
  314. #define sysfs_in_reg_offset(reg, offset) \
  315. static ssize_t show_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  316. { \
  317. return show_in_##reg (dev, buf, offset); \
  318. } \
  319. static ssize_t \
  320. store_regs_in_##reg##offset (struct device *dev, struct device_attribute *attr, \
  321. const char *buf, size_t count) \
  322. { \
  323. return store_in_##reg (dev, buf, count, offset); \
  324. } \
  325. static DEVICE_ATTR(in##offset##_##reg, S_IRUGO| S_IWUSR, \
  326. show_regs_in_##reg##offset, store_regs_in_##reg##offset);
  327. #define sysfs_in_offsets(offset) \
  328. sysfs_in_offset(offset) \
  329. sysfs_in_reg_offset(min, offset) \
  330. sysfs_in_reg_offset(max, offset)
  331. sysfs_in_offsets(1);
  332. sysfs_in_offsets(2);
  333. sysfs_in_offsets(3);
  334. sysfs_in_offsets(4);
  335. sysfs_in_offsets(5);
  336. sysfs_in_offsets(6);
  337. sysfs_in_offsets(7);
  338. sysfs_in_offsets(8);
  339. /* use a different set of functions for in0 */
  340. static ssize_t show_in_0(struct w83627hf_data *data, char *buf, u8 reg)
  341. {
  342. long in0;
  343. if ((data->vrm_ovt & 0x01) &&
  344. (w83627thf == data->type || w83637hf == data->type))
  345. /* use VRM9 calculation */
  346. in0 = (long)((reg * 488 + 70000 + 50) / 100);
  347. else
  348. /* use VRM8 (standard) calculation */
  349. in0 = (long)IN_FROM_REG(reg);
  350. return sprintf(buf,"%ld\n", in0);
  351. }
  352. static ssize_t show_regs_in_0(struct device *dev, struct device_attribute *attr, char *buf)
  353. {
  354. struct w83627hf_data *data = w83627hf_update_device(dev);
  355. return show_in_0(data, buf, data->in[0]);
  356. }
  357. static ssize_t show_regs_in_min0(struct device *dev, struct device_attribute *attr, char *buf)
  358. {
  359. struct w83627hf_data *data = w83627hf_update_device(dev);
  360. return show_in_0(data, buf, data->in_min[0]);
  361. }
  362. static ssize_t show_regs_in_max0(struct device *dev, struct device_attribute *attr, char *buf)
  363. {
  364. struct w83627hf_data *data = w83627hf_update_device(dev);
  365. return show_in_0(data, buf, data->in_max[0]);
  366. }
  367. static ssize_t store_regs_in_min0(struct device *dev, struct device_attribute *attr,
  368. const char *buf, size_t count)
  369. {
  370. struct i2c_client *client = to_i2c_client(dev);
  371. struct w83627hf_data *data = i2c_get_clientdata(client);
  372. u32 val;
  373. val = simple_strtoul(buf, NULL, 10);
  374. down(&data->update_lock);
  375. if ((data->vrm_ovt & 0x01) &&
  376. (w83627thf == data->type || w83637hf == data->type))
  377. /* use VRM9 calculation */
  378. data->in_min[0] = (u8)(((val * 100) - 70000 + 244) / 488);
  379. else
  380. /* use VRM8 (standard) calculation */
  381. data->in_min[0] = IN_TO_REG(val);
  382. w83627hf_write_value(client, W83781D_REG_IN_MIN(0), data->in_min[0]);
  383. up(&data->update_lock);
  384. return count;
  385. }
  386. static ssize_t store_regs_in_max0(struct device *dev, struct device_attribute *attr,
  387. const char *buf, size_t count)
  388. {
  389. struct i2c_client *client = to_i2c_client(dev);
  390. struct w83627hf_data *data = i2c_get_clientdata(client);
  391. u32 val;
  392. val = simple_strtoul(buf, NULL, 10);
  393. down(&data->update_lock);
  394. if ((data->vrm_ovt & 0x01) &&
  395. (w83627thf == data->type || w83637hf == data->type))
  396. /* use VRM9 calculation */
  397. data->in_max[0] = (u8)(((val * 100) - 70000 + 244) / 488);
  398. else
  399. /* use VRM8 (standard) calculation */
  400. data->in_max[0] = IN_TO_REG(val);
  401. w83627hf_write_value(client, W83781D_REG_IN_MAX(0), data->in_max[0]);
  402. up(&data->update_lock);
  403. return count;
  404. }
  405. static DEVICE_ATTR(in0_input, S_IRUGO, show_regs_in_0, NULL);
  406. static DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR,
  407. show_regs_in_min0, store_regs_in_min0);
  408. static DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR,
  409. show_regs_in_max0, store_regs_in_max0);
  410. #define device_create_file_in(client, offset) \
  411. do { \
  412. device_create_file(&client->dev, &dev_attr_in##offset##_input); \
  413. device_create_file(&client->dev, &dev_attr_in##offset##_min); \
  414. device_create_file(&client->dev, &dev_attr_in##offset##_max); \
  415. } while (0)
  416. #define show_fan_reg(reg) \
  417. static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
  418. { \
  419. struct w83627hf_data *data = w83627hf_update_device(dev); \
  420. return sprintf(buf,"%ld\n", \
  421. FAN_FROM_REG(data->reg[nr-1], \
  422. (long)DIV_FROM_REG(data->fan_div[nr-1]))); \
  423. }
  424. show_fan_reg(fan);
  425. show_fan_reg(fan_min);
  426. static ssize_t
  427. store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
  428. {
  429. struct i2c_client *client = to_i2c_client(dev);
  430. struct w83627hf_data *data = i2c_get_clientdata(client);
  431. u32 val;
  432. val = simple_strtoul(buf, NULL, 10);
  433. down(&data->update_lock);
  434. data->fan_min[nr - 1] =
  435. FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr - 1]));
  436. w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr),
  437. data->fan_min[nr - 1]);
  438. up(&data->update_lock);
  439. return count;
  440. }
  441. #define sysfs_fan_offset(offset) \
  442. static ssize_t show_regs_fan_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  443. { \
  444. return show_fan(dev, buf, offset); \
  445. } \
  446. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_regs_fan_##offset, NULL);
  447. #define sysfs_fan_min_offset(offset) \
  448. static ssize_t show_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  449. { \
  450. return show_fan_min(dev, buf, offset); \
  451. } \
  452. static ssize_t \
  453. store_regs_fan_min##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  454. { \
  455. return store_fan_min(dev, buf, count, offset); \
  456. } \
  457. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  458. show_regs_fan_min##offset, store_regs_fan_min##offset);
  459. sysfs_fan_offset(1);
  460. sysfs_fan_min_offset(1);
  461. sysfs_fan_offset(2);
  462. sysfs_fan_min_offset(2);
  463. sysfs_fan_offset(3);
  464. sysfs_fan_min_offset(3);
  465. #define device_create_file_fan(client, offset) \
  466. do { \
  467. device_create_file(&client->dev, &dev_attr_fan##offset##_input); \
  468. device_create_file(&client->dev, &dev_attr_fan##offset##_min); \
  469. } while (0)
  470. #define show_temp_reg(reg) \
  471. static ssize_t show_##reg (struct device *dev, char *buf, int nr) \
  472. { \
  473. struct w83627hf_data *data = w83627hf_update_device(dev); \
  474. if (nr >= 2) { /* TEMP2 and TEMP3 */ \
  475. return sprintf(buf,"%ld\n", \
  476. (long)LM75_TEMP_FROM_REG(data->reg##_add[nr-2])); \
  477. } else { /* TEMP1 */ \
  478. return sprintf(buf,"%ld\n", (long)TEMP_FROM_REG(data->reg)); \
  479. } \
  480. }
  481. show_temp_reg(temp);
  482. show_temp_reg(temp_max);
  483. show_temp_reg(temp_max_hyst);
  484. #define store_temp_reg(REG, reg) \
  485. static ssize_t \
  486. store_temp_##reg (struct device *dev, const char *buf, size_t count, int nr) \
  487. { \
  488. struct i2c_client *client = to_i2c_client(dev); \
  489. struct w83627hf_data *data = i2c_get_clientdata(client); \
  490. u32 val; \
  491. \
  492. val = simple_strtoul(buf, NULL, 10); \
  493. \
  494. down(&data->update_lock); \
  495. \
  496. if (nr >= 2) { /* TEMP2 and TEMP3 */ \
  497. data->temp_##reg##_add[nr-2] = LM75_TEMP_TO_REG(val); \
  498. w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
  499. data->temp_##reg##_add[nr-2]); \
  500. } else { /* TEMP1 */ \
  501. data->temp_##reg = TEMP_TO_REG(val); \
  502. w83627hf_write_value(client, W83781D_REG_TEMP_##REG(nr), \
  503. data->temp_##reg); \
  504. } \
  505. \
  506. up(&data->update_lock); \
  507. return count; \
  508. }
  509. store_temp_reg(OVER, max);
  510. store_temp_reg(HYST, max_hyst);
  511. #define sysfs_temp_offset(offset) \
  512. static ssize_t \
  513. show_regs_temp_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  514. { \
  515. return show_temp(dev, buf, offset); \
  516. } \
  517. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_regs_temp_##offset, NULL);
  518. #define sysfs_temp_reg_offset(reg, offset) \
  519. static ssize_t show_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  520. { \
  521. return show_temp_##reg (dev, buf, offset); \
  522. } \
  523. static ssize_t \
  524. store_regs_temp_##reg##offset (struct device *dev, struct device_attribute *attr, \
  525. const char *buf, size_t count) \
  526. { \
  527. return store_temp_##reg (dev, buf, count, offset); \
  528. } \
  529. static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
  530. show_regs_temp_##reg##offset, store_regs_temp_##reg##offset);
  531. #define sysfs_temp_offsets(offset) \
  532. sysfs_temp_offset(offset) \
  533. sysfs_temp_reg_offset(max, offset) \
  534. sysfs_temp_reg_offset(max_hyst, offset)
  535. sysfs_temp_offsets(1);
  536. sysfs_temp_offsets(2);
  537. sysfs_temp_offsets(3);
  538. #define device_create_file_temp(client, offset) \
  539. do { \
  540. device_create_file(&client->dev, &dev_attr_temp##offset##_input); \
  541. device_create_file(&client->dev, &dev_attr_temp##offset##_max); \
  542. device_create_file(&client->dev, &dev_attr_temp##offset##_max_hyst); \
  543. } while (0)
  544. static ssize_t
  545. show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
  546. {
  547. struct w83627hf_data *data = w83627hf_update_device(dev);
  548. return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
  549. }
  550. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  551. #define device_create_file_vid(client) \
  552. device_create_file(&client->dev, &dev_attr_cpu0_vid)
  553. static ssize_t
  554. show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  555. {
  556. struct w83627hf_data *data = w83627hf_update_device(dev);
  557. return sprintf(buf, "%ld\n", (long) data->vrm);
  558. }
  559. static ssize_t
  560. store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  561. {
  562. struct i2c_client *client = to_i2c_client(dev);
  563. struct w83627hf_data *data = i2c_get_clientdata(client);
  564. u32 val;
  565. val = simple_strtoul(buf, NULL, 10);
  566. data->vrm = val;
  567. return count;
  568. }
  569. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  570. #define device_create_file_vrm(client) \
  571. device_create_file(&client->dev, &dev_attr_vrm)
  572. static ssize_t
  573. show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
  574. {
  575. struct w83627hf_data *data = w83627hf_update_device(dev);
  576. return sprintf(buf, "%ld\n", (long) data->alarms);
  577. }
  578. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  579. #define device_create_file_alarms(client) \
  580. device_create_file(&client->dev, &dev_attr_alarms)
  581. #define show_beep_reg(REG, reg) \
  582. static ssize_t show_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
  583. { \
  584. struct w83627hf_data *data = w83627hf_update_device(dev); \
  585. return sprintf(buf,"%ld\n", \
  586. (long)BEEP_##REG##_FROM_REG(data->beep_##reg)); \
  587. }
  588. show_beep_reg(ENABLE, enable)
  589. show_beep_reg(MASK, mask)
  590. #define BEEP_ENABLE 0 /* Store beep_enable */
  591. #define BEEP_MASK 1 /* Store beep_mask */
  592. static ssize_t
  593. store_beep_reg(struct device *dev, const char *buf, size_t count,
  594. int update_mask)
  595. {
  596. struct i2c_client *client = to_i2c_client(dev);
  597. struct w83627hf_data *data = i2c_get_clientdata(client);
  598. u32 val, val2;
  599. val = simple_strtoul(buf, NULL, 10);
  600. down(&data->update_lock);
  601. if (update_mask == BEEP_MASK) { /* We are storing beep_mask */
  602. data->beep_mask = BEEP_MASK_TO_REG(val);
  603. w83627hf_write_value(client, W83781D_REG_BEEP_INTS1,
  604. data->beep_mask & 0xff);
  605. w83627hf_write_value(client, W83781D_REG_BEEP_INTS3,
  606. ((data->beep_mask) >> 16) & 0xff);
  607. val2 = (data->beep_mask >> 8) & 0x7f;
  608. } else { /* We are storing beep_enable */
  609. val2 =
  610. w83627hf_read_value(client, W83781D_REG_BEEP_INTS2) & 0x7f;
  611. data->beep_enable = BEEP_ENABLE_TO_REG(val);
  612. }
  613. w83627hf_write_value(client, W83781D_REG_BEEP_INTS2,
  614. val2 | data->beep_enable << 7);
  615. up(&data->update_lock);
  616. return count;
  617. }
  618. #define sysfs_beep(REG, reg) \
  619. static ssize_t show_regs_beep_##reg (struct device *dev, struct device_attribute *attr, char *buf) \
  620. { \
  621. return show_beep_##reg(dev, attr, buf); \
  622. } \
  623. static ssize_t \
  624. store_regs_beep_##reg (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  625. { \
  626. return store_beep_reg(dev, buf, count, BEEP_##REG); \
  627. } \
  628. static DEVICE_ATTR(beep_##reg, S_IRUGO | S_IWUSR, \
  629. show_regs_beep_##reg, store_regs_beep_##reg);
  630. sysfs_beep(ENABLE, enable);
  631. sysfs_beep(MASK, mask);
  632. #define device_create_file_beep(client) \
  633. do { \
  634. device_create_file(&client->dev, &dev_attr_beep_enable); \
  635. device_create_file(&client->dev, &dev_attr_beep_mask); \
  636. } while (0)
  637. static ssize_t
  638. show_fan_div_reg(struct device *dev, char *buf, int nr)
  639. {
  640. struct w83627hf_data *data = w83627hf_update_device(dev);
  641. return sprintf(buf, "%ld\n",
  642. (long) DIV_FROM_REG(data->fan_div[nr - 1]));
  643. }
  644. /* Note: we save and restore the fan minimum here, because its value is
  645. determined in part by the fan divisor. This follows the principle of
  646. least suprise; the user doesn't expect the fan minimum to change just
  647. because the divisor changed. */
  648. static ssize_t
  649. store_fan_div_reg(struct device *dev, const char *buf, size_t count, int nr)
  650. {
  651. struct i2c_client *client = to_i2c_client(dev);
  652. struct w83627hf_data *data = i2c_get_clientdata(client);
  653. unsigned long min;
  654. u8 reg;
  655. unsigned long val = simple_strtoul(buf, NULL, 10);
  656. down(&data->update_lock);
  657. /* Save fan_min */
  658. min = FAN_FROM_REG(data->fan_min[nr],
  659. DIV_FROM_REG(data->fan_div[nr]));
  660. data->fan_div[nr] = DIV_TO_REG(val);
  661. reg = (w83627hf_read_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV)
  662. & (nr==0 ? 0xcf : 0x3f))
  663. | ((data->fan_div[nr] & 0x03) << (nr==0 ? 4 : 6));
  664. w83627hf_write_value(client, nr==2 ? W83781D_REG_PIN : W83781D_REG_VID_FANDIV, reg);
  665. reg = (w83627hf_read_value(client, W83781D_REG_VBAT)
  666. & ~(1 << (5 + nr)))
  667. | ((data->fan_div[nr] & 0x04) << (3 + nr));
  668. w83627hf_write_value(client, W83781D_REG_VBAT, reg);
  669. /* Restore fan_min */
  670. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  671. w83627hf_write_value(client, W83781D_REG_FAN_MIN(nr+1), data->fan_min[nr]);
  672. up(&data->update_lock);
  673. return count;
  674. }
  675. #define sysfs_fan_div(offset) \
  676. static ssize_t show_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  677. { \
  678. return show_fan_div_reg(dev, buf, offset); \
  679. } \
  680. static ssize_t \
  681. store_regs_fan_div_##offset (struct device *dev, struct device_attribute *attr, \
  682. const char *buf, size_t count) \
  683. { \
  684. return store_fan_div_reg(dev, buf, count, offset - 1); \
  685. } \
  686. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  687. show_regs_fan_div_##offset, store_regs_fan_div_##offset);
  688. sysfs_fan_div(1);
  689. sysfs_fan_div(2);
  690. sysfs_fan_div(3);
  691. #define device_create_file_fan_div(client, offset) \
  692. do { \
  693. device_create_file(&client->dev, &dev_attr_fan##offset##_div); \
  694. } while (0)
  695. static ssize_t
  696. show_pwm_reg(struct device *dev, char *buf, int nr)
  697. {
  698. struct w83627hf_data *data = w83627hf_update_device(dev);
  699. return sprintf(buf, "%ld\n", (long) data->pwm[nr - 1]);
  700. }
  701. static ssize_t
  702. store_pwm_reg(struct device *dev, const char *buf, size_t count, int nr)
  703. {
  704. struct i2c_client *client = to_i2c_client(dev);
  705. struct w83627hf_data *data = i2c_get_clientdata(client);
  706. u32 val;
  707. val = simple_strtoul(buf, NULL, 10);
  708. down(&data->update_lock);
  709. if (data->type == w83627thf) {
  710. /* bits 0-3 are reserved in 627THF */
  711. data->pwm[nr - 1] = PWM_TO_REG(val) & 0xf0;
  712. w83627hf_write_value(client,
  713. W836X7HF_REG_PWM(data->type, nr),
  714. data->pwm[nr - 1] |
  715. (w83627hf_read_value(client,
  716. W836X7HF_REG_PWM(data->type, nr)) & 0x0f));
  717. } else {
  718. data->pwm[nr - 1] = PWM_TO_REG(val);
  719. w83627hf_write_value(client,
  720. W836X7HF_REG_PWM(data->type, nr),
  721. data->pwm[nr - 1]);
  722. }
  723. up(&data->update_lock);
  724. return count;
  725. }
  726. #define sysfs_pwm(offset) \
  727. static ssize_t show_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  728. { \
  729. return show_pwm_reg(dev, buf, offset); \
  730. } \
  731. static ssize_t \
  732. store_regs_pwm_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  733. { \
  734. return store_pwm_reg(dev, buf, count, offset); \
  735. } \
  736. static DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  737. show_regs_pwm_##offset, store_regs_pwm_##offset);
  738. sysfs_pwm(1);
  739. sysfs_pwm(2);
  740. sysfs_pwm(3);
  741. #define device_create_file_pwm(client, offset) \
  742. do { \
  743. device_create_file(&client->dev, &dev_attr_pwm##offset); \
  744. } while (0)
  745. static ssize_t
  746. show_sensor_reg(struct device *dev, char *buf, int nr)
  747. {
  748. struct w83627hf_data *data = w83627hf_update_device(dev);
  749. return sprintf(buf, "%ld\n", (long) data->sens[nr - 1]);
  750. }
  751. static ssize_t
  752. store_sensor_reg(struct device *dev, const char *buf, size_t count, int nr)
  753. {
  754. struct i2c_client *client = to_i2c_client(dev);
  755. struct w83627hf_data *data = i2c_get_clientdata(client);
  756. u32 val, tmp;
  757. val = simple_strtoul(buf, NULL, 10);
  758. down(&data->update_lock);
  759. switch (val) {
  760. case 1: /* PII/Celeron diode */
  761. tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
  762. w83627hf_write_value(client, W83781D_REG_SCFG1,
  763. tmp | BIT_SCFG1[nr - 1]);
  764. tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
  765. w83627hf_write_value(client, W83781D_REG_SCFG2,
  766. tmp | BIT_SCFG2[nr - 1]);
  767. data->sens[nr - 1] = val;
  768. break;
  769. case 2: /* 3904 */
  770. tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
  771. w83627hf_write_value(client, W83781D_REG_SCFG1,
  772. tmp | BIT_SCFG1[nr - 1]);
  773. tmp = w83627hf_read_value(client, W83781D_REG_SCFG2);
  774. w83627hf_write_value(client, W83781D_REG_SCFG2,
  775. tmp & ~BIT_SCFG2[nr - 1]);
  776. data->sens[nr - 1] = val;
  777. break;
  778. case W83781D_DEFAULT_BETA: /* thermistor */
  779. tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
  780. w83627hf_write_value(client, W83781D_REG_SCFG1,
  781. tmp & ~BIT_SCFG1[nr - 1]);
  782. data->sens[nr - 1] = val;
  783. break;
  784. default:
  785. dev_err(&client->dev,
  786. "Invalid sensor type %ld; must be 1, 2, or %d\n",
  787. (long) val, W83781D_DEFAULT_BETA);
  788. break;
  789. }
  790. up(&data->update_lock);
  791. return count;
  792. }
  793. #define sysfs_sensor(offset) \
  794. static ssize_t show_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, char *buf) \
  795. { \
  796. return show_sensor_reg(dev, buf, offset); \
  797. } \
  798. static ssize_t \
  799. store_regs_sensor_##offset (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \
  800. { \
  801. return store_sensor_reg(dev, buf, count, offset); \
  802. } \
  803. static DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
  804. show_regs_sensor_##offset, store_regs_sensor_##offset);
  805. sysfs_sensor(1);
  806. sysfs_sensor(2);
  807. sysfs_sensor(3);
  808. #define device_create_file_sensor(client, offset) \
  809. do { \
  810. device_create_file(&client->dev, &dev_attr_temp##offset##_type); \
  811. } while (0)
  812. static int __init w83627hf_find(int sioaddr, unsigned short *addr)
  813. {
  814. u16 val;
  815. REG = sioaddr;
  816. VAL = sioaddr + 1;
  817. superio_enter();
  818. val= superio_inb(DEVID);
  819. if(val != W627_DEVID &&
  820. val != W627THF_DEVID &&
  821. val != W697_DEVID &&
  822. val != W637_DEVID) {
  823. superio_exit();
  824. return -ENODEV;
  825. }
  826. superio_select(W83627HF_LD_HWM);
  827. val = (superio_inb(WINB_BASE_REG) << 8) |
  828. superio_inb(WINB_BASE_REG + 1);
  829. *addr = val & ~(WINB_EXTENT - 1);
  830. if (*addr == 0 && force_addr == 0) {
  831. superio_exit();
  832. return -ENODEV;
  833. }
  834. superio_exit();
  835. return 0;
  836. }
  837. static int w83627hf_detect(struct i2c_adapter *adapter)
  838. {
  839. int val, kind;
  840. struct i2c_client *new_client;
  841. struct w83627hf_data *data;
  842. int err = 0;
  843. const char *client_name = "";
  844. if(force_addr)
  845. address = force_addr & ~(WINB_EXTENT - 1);
  846. if (!request_region(address, WINB_EXTENT, w83627hf_driver.name)) {
  847. err = -EBUSY;
  848. goto ERROR0;
  849. }
  850. if(force_addr) {
  851. printk("w83627hf.o: forcing ISA address 0x%04X\n", address);
  852. superio_enter();
  853. superio_select(W83627HF_LD_HWM);
  854. superio_outb(WINB_BASE_REG, address >> 8);
  855. superio_outb(WINB_BASE_REG+1, address & 0xff);
  856. superio_exit();
  857. }
  858. superio_enter();
  859. val= superio_inb(DEVID);
  860. if(val == W627_DEVID)
  861. kind = w83627hf;
  862. else if(val == W697_DEVID)
  863. kind = w83697hf;
  864. else if(val == W627THF_DEVID)
  865. kind = w83627thf;
  866. else if(val == W637_DEVID)
  867. kind = w83637hf;
  868. else {
  869. dev_info(&adapter->dev,
  870. "Unsupported chip (dev_id=0x%02X).\n", val);
  871. goto ERROR1;
  872. }
  873. superio_select(W83627HF_LD_HWM);
  874. if((val = 0x01 & superio_inb(WINB_ACT_REG)) == 0)
  875. superio_outb(WINB_ACT_REG, 1);
  876. superio_exit();
  877. /* OK. For now, we presume we have a valid client. We now create the
  878. client structure, even though we cannot fill it completely yet.
  879. But it allows us to access w83627hf_{read,write}_value. */
  880. if (!(data = kmalloc(sizeof(struct w83627hf_data), GFP_KERNEL))) {
  881. err = -ENOMEM;
  882. goto ERROR1;
  883. }
  884. memset(data, 0, sizeof(struct w83627hf_data));
  885. new_client = &data->client;
  886. i2c_set_clientdata(new_client, data);
  887. new_client->addr = address;
  888. init_MUTEX(&data->lock);
  889. new_client->adapter = adapter;
  890. new_client->driver = &w83627hf_driver;
  891. new_client->flags = 0;
  892. if (kind == w83627hf) {
  893. client_name = "w83627hf";
  894. } else if (kind == w83627thf) {
  895. client_name = "w83627thf";
  896. } else if (kind == w83697hf) {
  897. client_name = "w83697hf";
  898. } else if (kind == w83637hf) {
  899. client_name = "w83637hf";
  900. }
  901. /* Fill in the remaining client fields and put into the global list */
  902. strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
  903. data->type = kind;
  904. data->valid = 0;
  905. init_MUTEX(&data->update_lock);
  906. /* Tell the I2C layer a new client has arrived */
  907. if ((err = i2c_attach_client(new_client)))
  908. goto ERROR2;
  909. data->lm75 = NULL;
  910. /* Initialize the chip */
  911. w83627hf_init_client(new_client);
  912. /* A few vars need to be filled upon startup */
  913. data->fan_min[0] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(1));
  914. data->fan_min[1] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(2));
  915. data->fan_min[2] = w83627hf_read_value(new_client, W83781D_REG_FAN_MIN(3));
  916. /* Register sysfs hooks */
  917. data->class_dev = hwmon_device_register(&new_client->dev);
  918. if (IS_ERR(data->class_dev)) {
  919. err = PTR_ERR(data->class_dev);
  920. goto ERROR3;
  921. }
  922. device_create_file_in(new_client, 0);
  923. if (kind != w83697hf)
  924. device_create_file_in(new_client, 1);
  925. device_create_file_in(new_client, 2);
  926. device_create_file_in(new_client, 3);
  927. device_create_file_in(new_client, 4);
  928. if (kind != w83627thf && kind != w83637hf) {
  929. device_create_file_in(new_client, 5);
  930. device_create_file_in(new_client, 6);
  931. }
  932. device_create_file_in(new_client, 7);
  933. device_create_file_in(new_client, 8);
  934. device_create_file_fan(new_client, 1);
  935. device_create_file_fan(new_client, 2);
  936. if (kind != w83697hf)
  937. device_create_file_fan(new_client, 3);
  938. device_create_file_temp(new_client, 1);
  939. device_create_file_temp(new_client, 2);
  940. if (kind != w83697hf)
  941. device_create_file_temp(new_client, 3);
  942. if (kind != w83697hf)
  943. device_create_file_vid(new_client);
  944. if (kind != w83697hf)
  945. device_create_file_vrm(new_client);
  946. device_create_file_fan_div(new_client, 1);
  947. device_create_file_fan_div(new_client, 2);
  948. if (kind != w83697hf)
  949. device_create_file_fan_div(new_client, 3);
  950. device_create_file_alarms(new_client);
  951. device_create_file_beep(new_client);
  952. device_create_file_pwm(new_client, 1);
  953. device_create_file_pwm(new_client, 2);
  954. if (kind == w83627thf || kind == w83637hf)
  955. device_create_file_pwm(new_client, 3);
  956. device_create_file_sensor(new_client, 1);
  957. device_create_file_sensor(new_client, 2);
  958. if (kind != w83697hf)
  959. device_create_file_sensor(new_client, 3);
  960. return 0;
  961. ERROR3:
  962. i2c_detach_client(new_client);
  963. ERROR2:
  964. kfree(data);
  965. ERROR1:
  966. release_region(address, WINB_EXTENT);
  967. ERROR0:
  968. return err;
  969. }
  970. static int w83627hf_detach_client(struct i2c_client *client)
  971. {
  972. struct w83627hf_data *data = i2c_get_clientdata(client);
  973. int err;
  974. hwmon_device_unregister(data->class_dev);
  975. if ((err = i2c_detach_client(client)))
  976. return err;
  977. release_region(client->addr, WINB_EXTENT);
  978. kfree(data);
  979. return 0;
  980. }
  981. /*
  982. ISA access must always be locked explicitly!
  983. We ignore the W83781D BUSY flag at this moment - it could lead to deadlocks,
  984. would slow down the W83781D access and should not be necessary.
  985. There are some ugly typecasts here, but the good news is - they should
  986. nowhere else be necessary! */
  987. static int w83627hf_read_value(struct i2c_client *client, u16 reg)
  988. {
  989. struct w83627hf_data *data = i2c_get_clientdata(client);
  990. int res, word_sized;
  991. down(&data->lock);
  992. word_sized = (((reg & 0xff00) == 0x100)
  993. || ((reg & 0xff00) == 0x200))
  994. && (((reg & 0x00ff) == 0x50)
  995. || ((reg & 0x00ff) == 0x53)
  996. || ((reg & 0x00ff) == 0x55));
  997. if (reg & 0xff00) {
  998. outb_p(W83781D_REG_BANK,
  999. client->addr + W83781D_ADDR_REG_OFFSET);
  1000. outb_p(reg >> 8,
  1001. client->addr + W83781D_DATA_REG_OFFSET);
  1002. }
  1003. outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
  1004. res = inb_p(client->addr + W83781D_DATA_REG_OFFSET);
  1005. if (word_sized) {
  1006. outb_p((reg & 0xff) + 1,
  1007. client->addr + W83781D_ADDR_REG_OFFSET);
  1008. res =
  1009. (res << 8) + inb_p(client->addr +
  1010. W83781D_DATA_REG_OFFSET);
  1011. }
  1012. if (reg & 0xff00) {
  1013. outb_p(W83781D_REG_BANK,
  1014. client->addr + W83781D_ADDR_REG_OFFSET);
  1015. outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
  1016. }
  1017. up(&data->lock);
  1018. return res;
  1019. }
  1020. static int w83627thf_read_gpio5(struct i2c_client *client)
  1021. {
  1022. int res = 0xff, sel;
  1023. superio_enter();
  1024. superio_select(W83627HF_LD_GPIO5);
  1025. /* Make sure these GPIO pins are enabled */
  1026. if (!(superio_inb(W83627THF_GPIO5_EN) & (1<<3))) {
  1027. dev_dbg(&client->dev, "GPIO5 disabled, no VID function\n");
  1028. goto exit;
  1029. }
  1030. /* Make sure the pins are configured for input
  1031. There must be at least five (VRM 9), and possibly 6 (VRM 10) */
  1032. sel = superio_inb(W83627THF_GPIO5_IOSR);
  1033. if ((sel & 0x1f) != 0x1f) {
  1034. dev_dbg(&client->dev, "GPIO5 not configured for VID "
  1035. "function\n");
  1036. goto exit;
  1037. }
  1038. dev_info(&client->dev, "Reading VID from GPIO5\n");
  1039. res = superio_inb(W83627THF_GPIO5_DR) & sel;
  1040. exit:
  1041. superio_exit();
  1042. return res;
  1043. }
  1044. static int w83627hf_write_value(struct i2c_client *client, u16 reg, u16 value)
  1045. {
  1046. struct w83627hf_data *data = i2c_get_clientdata(client);
  1047. int word_sized;
  1048. down(&data->lock);
  1049. word_sized = (((reg & 0xff00) == 0x100)
  1050. || ((reg & 0xff00) == 0x200))
  1051. && (((reg & 0x00ff) == 0x53)
  1052. || ((reg & 0x00ff) == 0x55));
  1053. if (reg & 0xff00) {
  1054. outb_p(W83781D_REG_BANK,
  1055. client->addr + W83781D_ADDR_REG_OFFSET);
  1056. outb_p(reg >> 8,
  1057. client->addr + W83781D_DATA_REG_OFFSET);
  1058. }
  1059. outb_p(reg & 0xff, client->addr + W83781D_ADDR_REG_OFFSET);
  1060. if (word_sized) {
  1061. outb_p(value >> 8,
  1062. client->addr + W83781D_DATA_REG_OFFSET);
  1063. outb_p((reg & 0xff) + 1,
  1064. client->addr + W83781D_ADDR_REG_OFFSET);
  1065. }
  1066. outb_p(value & 0xff,
  1067. client->addr + W83781D_DATA_REG_OFFSET);
  1068. if (reg & 0xff00) {
  1069. outb_p(W83781D_REG_BANK,
  1070. client->addr + W83781D_ADDR_REG_OFFSET);
  1071. outb_p(0, client->addr + W83781D_DATA_REG_OFFSET);
  1072. }
  1073. up(&data->lock);
  1074. return 0;
  1075. }
  1076. /* Called when we have found a new W83781D. It should set limits, etc. */
  1077. static void w83627hf_init_client(struct i2c_client *client)
  1078. {
  1079. struct w83627hf_data *data = i2c_get_clientdata(client);
  1080. int i;
  1081. int type = data->type;
  1082. u8 tmp;
  1083. if(init) {
  1084. /* save this register */
  1085. i = w83627hf_read_value(client, W83781D_REG_BEEP_CONFIG);
  1086. /* Reset all except Watchdog values and last conversion values
  1087. This sets fan-divs to 2, among others */
  1088. w83627hf_write_value(client, W83781D_REG_CONFIG, 0x80);
  1089. /* Restore the register and disable power-on abnormal beep.
  1090. This saves FAN 1/2/3 input/output values set by BIOS. */
  1091. w83627hf_write_value(client, W83781D_REG_BEEP_CONFIG, i | 0x80);
  1092. /* Disable master beep-enable (reset turns it on).
  1093. Individual beeps should be reset to off but for some reason
  1094. disabling this bit helps some people not get beeped */
  1095. w83627hf_write_value(client, W83781D_REG_BEEP_INTS2, 0);
  1096. }
  1097. /* Minimize conflicts with other winbond i2c-only clients... */
  1098. /* disable i2c subclients... how to disable main i2c client?? */
  1099. /* force i2c address to relatively uncommon address */
  1100. w83627hf_write_value(client, W83781D_REG_I2C_SUBADDR, 0x89);
  1101. w83627hf_write_value(client, W83781D_REG_I2C_ADDR, force_i2c);
  1102. /* Read VID only once */
  1103. if (w83627hf == data->type || w83637hf == data->type) {
  1104. int lo = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
  1105. int hi = w83627hf_read_value(client, W83781D_REG_CHIPID);
  1106. data->vid = (lo & 0x0f) | ((hi & 0x01) << 4);
  1107. } else if (w83627thf == data->type) {
  1108. data->vid = w83627thf_read_gpio5(client) & 0x3f;
  1109. }
  1110. /* Read VRM & OVT Config only once */
  1111. if (w83627thf == data->type || w83637hf == data->type) {
  1112. data->vrm_ovt =
  1113. w83627hf_read_value(client, W83627THF_REG_VRM_OVT_CFG);
  1114. data->vrm = (data->vrm_ovt & 0x01) ? 90 : 82;
  1115. } else {
  1116. /* Convert VID to voltage based on default VRM */
  1117. data->vrm = vid_which_vrm();
  1118. }
  1119. tmp = w83627hf_read_value(client, W83781D_REG_SCFG1);
  1120. for (i = 1; i <= 3; i++) {
  1121. if (!(tmp & BIT_SCFG1[i - 1])) {
  1122. data->sens[i - 1] = W83781D_DEFAULT_BETA;
  1123. } else {
  1124. if (w83627hf_read_value
  1125. (client,
  1126. W83781D_REG_SCFG2) & BIT_SCFG2[i - 1])
  1127. data->sens[i - 1] = 1;
  1128. else
  1129. data->sens[i - 1] = 2;
  1130. }
  1131. if ((type == w83697hf) && (i == 2))
  1132. break;
  1133. }
  1134. if(init) {
  1135. /* Enable temp2 */
  1136. tmp = w83627hf_read_value(client, W83781D_REG_TEMP2_CONFIG);
  1137. if (tmp & 0x01) {
  1138. dev_warn(&client->dev, "Enabling temp2, readings "
  1139. "might not make sense\n");
  1140. w83627hf_write_value(client, W83781D_REG_TEMP2_CONFIG,
  1141. tmp & 0xfe);
  1142. }
  1143. /* Enable temp3 */
  1144. if (type != w83697hf) {
  1145. tmp = w83627hf_read_value(client,
  1146. W83781D_REG_TEMP3_CONFIG);
  1147. if (tmp & 0x01) {
  1148. dev_warn(&client->dev, "Enabling temp3, "
  1149. "readings might not make sense\n");
  1150. w83627hf_write_value(client,
  1151. W83781D_REG_TEMP3_CONFIG, tmp & 0xfe);
  1152. }
  1153. }
  1154. if (type == w83627hf) {
  1155. /* enable PWM2 control (can't hurt since PWM reg
  1156. should have been reset to 0xff) */
  1157. w83627hf_write_value(client, W83627HF_REG_PWMCLK12,
  1158. 0x19);
  1159. }
  1160. /* enable comparator mode for temp2 and temp3 so
  1161. alarm indication will work correctly */
  1162. i = w83627hf_read_value(client, W83781D_REG_IRQ);
  1163. if (!(i & 0x40))
  1164. w83627hf_write_value(client, W83781D_REG_IRQ,
  1165. i | 0x40);
  1166. }
  1167. /* Start monitoring */
  1168. w83627hf_write_value(client, W83781D_REG_CONFIG,
  1169. (w83627hf_read_value(client,
  1170. W83781D_REG_CONFIG) & 0xf7)
  1171. | 0x01);
  1172. }
  1173. static struct w83627hf_data *w83627hf_update_device(struct device *dev)
  1174. {
  1175. struct i2c_client *client = to_i2c_client(dev);
  1176. struct w83627hf_data *data = i2c_get_clientdata(client);
  1177. int i;
  1178. down(&data->update_lock);
  1179. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  1180. || !data->valid) {
  1181. for (i = 0; i <= 8; i++) {
  1182. /* skip missing sensors */
  1183. if (((data->type == w83697hf) && (i == 1)) ||
  1184. ((data->type == w83627thf || data->type == w83637hf)
  1185. && (i == 4 || i == 5)))
  1186. continue;
  1187. data->in[i] =
  1188. w83627hf_read_value(client, W83781D_REG_IN(i));
  1189. data->in_min[i] =
  1190. w83627hf_read_value(client,
  1191. W83781D_REG_IN_MIN(i));
  1192. data->in_max[i] =
  1193. w83627hf_read_value(client,
  1194. W83781D_REG_IN_MAX(i));
  1195. }
  1196. for (i = 1; i <= 3; i++) {
  1197. data->fan[i - 1] =
  1198. w83627hf_read_value(client, W83781D_REG_FAN(i));
  1199. data->fan_min[i - 1] =
  1200. w83627hf_read_value(client,
  1201. W83781D_REG_FAN_MIN(i));
  1202. }
  1203. for (i = 1; i <= 3; i++) {
  1204. u8 tmp = w83627hf_read_value(client,
  1205. W836X7HF_REG_PWM(data->type, i));
  1206. /* bits 0-3 are reserved in 627THF */
  1207. if (data->type == w83627thf)
  1208. tmp &= 0xf0;
  1209. data->pwm[i - 1] = tmp;
  1210. if(i == 2 &&
  1211. (data->type == w83627hf || data->type == w83697hf))
  1212. break;
  1213. }
  1214. data->temp = w83627hf_read_value(client, W83781D_REG_TEMP(1));
  1215. data->temp_max =
  1216. w83627hf_read_value(client, W83781D_REG_TEMP_OVER(1));
  1217. data->temp_max_hyst =
  1218. w83627hf_read_value(client, W83781D_REG_TEMP_HYST(1));
  1219. data->temp_add[0] =
  1220. w83627hf_read_value(client, W83781D_REG_TEMP(2));
  1221. data->temp_max_add[0] =
  1222. w83627hf_read_value(client, W83781D_REG_TEMP_OVER(2));
  1223. data->temp_max_hyst_add[0] =
  1224. w83627hf_read_value(client, W83781D_REG_TEMP_HYST(2));
  1225. if (data->type != w83697hf) {
  1226. data->temp_add[1] =
  1227. w83627hf_read_value(client, W83781D_REG_TEMP(3));
  1228. data->temp_max_add[1] =
  1229. w83627hf_read_value(client, W83781D_REG_TEMP_OVER(3));
  1230. data->temp_max_hyst_add[1] =
  1231. w83627hf_read_value(client, W83781D_REG_TEMP_HYST(3));
  1232. }
  1233. i = w83627hf_read_value(client, W83781D_REG_VID_FANDIV);
  1234. data->fan_div[0] = (i >> 4) & 0x03;
  1235. data->fan_div[1] = (i >> 6) & 0x03;
  1236. if (data->type != w83697hf) {
  1237. data->fan_div[2] = (w83627hf_read_value(client,
  1238. W83781D_REG_PIN) >> 6) & 0x03;
  1239. }
  1240. i = w83627hf_read_value(client, W83781D_REG_VBAT);
  1241. data->fan_div[0] |= (i >> 3) & 0x04;
  1242. data->fan_div[1] |= (i >> 4) & 0x04;
  1243. if (data->type != w83697hf)
  1244. data->fan_div[2] |= (i >> 5) & 0x04;
  1245. data->alarms =
  1246. w83627hf_read_value(client, W83781D_REG_ALARM1) |
  1247. (w83627hf_read_value(client, W83781D_REG_ALARM2) << 8) |
  1248. (w83627hf_read_value(client, W83781D_REG_ALARM3) << 16);
  1249. i = w83627hf_read_value(client, W83781D_REG_BEEP_INTS2);
  1250. data->beep_enable = i >> 7;
  1251. data->beep_mask = ((i & 0x7f) << 8) |
  1252. w83627hf_read_value(client, W83781D_REG_BEEP_INTS1) |
  1253. w83627hf_read_value(client, W83781D_REG_BEEP_INTS3) << 16;
  1254. data->last_updated = jiffies;
  1255. data->valid = 1;
  1256. }
  1257. up(&data->update_lock);
  1258. return data;
  1259. }
  1260. static int __init sensors_w83627hf_init(void)
  1261. {
  1262. if (w83627hf_find(0x2e, &address)
  1263. && w83627hf_find(0x4e, &address)) {
  1264. return -ENODEV;
  1265. }
  1266. return i2c_isa_add_driver(&w83627hf_driver);
  1267. }
  1268. static void __exit sensors_w83627hf_exit(void)
  1269. {
  1270. i2c_isa_del_driver(&w83627hf_driver);
  1271. }
  1272. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  1273. "Philip Edelbrock <phil@netroedge.com>, "
  1274. "and Mark Studebaker <mdsxyz123@yahoo.com>");
  1275. MODULE_DESCRIPTION("W83627HF driver");
  1276. MODULE_LICENSE("GPL");
  1277. module_init(sensors_w83627hf_init);
  1278. module_exit(sensors_w83627hf_exit);