w83627hf.c 44 KB

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