w83627hf.c 44 KB

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