w83792d.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. /*
  2. w83792d.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (C) 2004, 2005 Winbond Electronics Corp.
  5. Chunhao Huang <DZShen@Winbond.com.tw>,
  6. Rudolf Marek <r.marek@sh.cvut.cz>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. Note:
  19. 1. This driver is only for 2.6 kernel, 2.4 kernel need a different driver.
  20. 2. This driver is only for Winbond W83792D C version device, there
  21. are also some motherboards with B version W83792D device. The
  22. calculation method to in6-in7(measured value, limits) is a little
  23. different between C and B version. C or B version can be identified
  24. by CR[0x49h].
  25. */
  26. /*
  27. Supports following chips:
  28. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  29. w83792d 9 7 7 3 0x7a 0x5ca3 yes no
  30. */
  31. #include <linux/config.h>
  32. #include <linux/module.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <linux/i2c.h>
  36. #include <linux/hwmon.h>
  37. #include <linux/hwmon-sysfs.h>
  38. #include <linux/err.h>
  39. /* Addresses to scan */
  40. static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END };
  41. /* Insmod parameters */
  42. I2C_CLIENT_INSMOD_1(w83792d);
  43. I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
  44. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  45. static int init;
  46. module_param(init, bool, 0);
  47. MODULE_PARM_DESC(init, "Set to one to force chip initialization");
  48. /* The W83792D registers */
  49. static const u8 W83792D_REG_IN[9] = {
  50. 0x20, /* Vcore A in DataSheet */
  51. 0x21, /* Vcore B in DataSheet */
  52. 0x22, /* VIN0 in DataSheet */
  53. 0x23, /* VIN1 in DataSheet */
  54. 0x24, /* VIN2 in DataSheet */
  55. 0x25, /* VIN3 in DataSheet */
  56. 0x26, /* 5VCC in DataSheet */
  57. 0xB0, /* 5VSB in DataSheet */
  58. 0xB1 /* VBAT in DataSheet */
  59. };
  60. #define W83792D_REG_LOW_BITS1 0x3E /* Low Bits I in DataSheet */
  61. #define W83792D_REG_LOW_BITS2 0x3F /* Low Bits II in DataSheet */
  62. static const u8 W83792D_REG_IN_MAX[9] = {
  63. 0x2B, /* Vcore A High Limit in DataSheet */
  64. 0x2D, /* Vcore B High Limit in DataSheet */
  65. 0x2F, /* VIN0 High Limit in DataSheet */
  66. 0x31, /* VIN1 High Limit in DataSheet */
  67. 0x33, /* VIN2 High Limit in DataSheet */
  68. 0x35, /* VIN3 High Limit in DataSheet */
  69. 0x37, /* 5VCC High Limit in DataSheet */
  70. 0xB4, /* 5VSB High Limit in DataSheet */
  71. 0xB6 /* VBAT High Limit in DataSheet */
  72. };
  73. static const u8 W83792D_REG_IN_MIN[9] = {
  74. 0x2C, /* Vcore A Low Limit in DataSheet */
  75. 0x2E, /* Vcore B Low Limit in DataSheet */
  76. 0x30, /* VIN0 Low Limit in DataSheet */
  77. 0x32, /* VIN1 Low Limit in DataSheet */
  78. 0x34, /* VIN2 Low Limit in DataSheet */
  79. 0x36, /* VIN3 Low Limit in DataSheet */
  80. 0x38, /* 5VCC Low Limit in DataSheet */
  81. 0xB5, /* 5VSB Low Limit in DataSheet */
  82. 0xB7 /* VBAT Low Limit in DataSheet */
  83. };
  84. static const u8 W83792D_REG_FAN[7] = {
  85. 0x28, /* FAN 1 Count in DataSheet */
  86. 0x29, /* FAN 2 Count in DataSheet */
  87. 0x2A, /* FAN 3 Count in DataSheet */
  88. 0xB8, /* FAN 4 Count in DataSheet */
  89. 0xB9, /* FAN 5 Count in DataSheet */
  90. 0xBA, /* FAN 6 Count in DataSheet */
  91. 0xBE /* FAN 7 Count in DataSheet */
  92. };
  93. static const u8 W83792D_REG_FAN_MIN[7] = {
  94. 0x3B, /* FAN 1 Count Low Limit in DataSheet */
  95. 0x3C, /* FAN 2 Count Low Limit in DataSheet */
  96. 0x3D, /* FAN 3 Count Low Limit in DataSheet */
  97. 0xBB, /* FAN 4 Count Low Limit in DataSheet */
  98. 0xBC, /* FAN 5 Count Low Limit in DataSheet */
  99. 0xBD, /* FAN 6 Count Low Limit in DataSheet */
  100. 0xBF /* FAN 7 Count Low Limit in DataSheet */
  101. };
  102. #define W83792D_REG_FAN_CFG 0x84 /* FAN Configuration in DataSheet */
  103. static const u8 W83792D_REG_FAN_DIV[4] = {
  104. 0x47, /* contains FAN2 and FAN1 Divisor */
  105. 0x5B, /* contains FAN4 and FAN3 Divisor */
  106. 0x5C, /* contains FAN6 and FAN5 Divisor */
  107. 0x9E /* contains FAN7 Divisor. */
  108. };
  109. static const u8 W83792D_REG_PWM[7] = {
  110. 0x81, /* FAN 1 Duty Cycle, be used to control */
  111. 0x83, /* FAN 2 Duty Cycle, be used to control */
  112. 0x94, /* FAN 3 Duty Cycle, be used to control */
  113. 0xA3, /* FAN 4 Duty Cycle, be used to control */
  114. 0xA4, /* FAN 5 Duty Cycle, be used to control */
  115. 0xA5, /* FAN 6 Duty Cycle, be used to control */
  116. 0xA6 /* FAN 7 Duty Cycle, be used to control */
  117. };
  118. #define W83792D_REG_BANK 0x4E
  119. #define W83792D_REG_TEMP2_CONFIG 0xC2
  120. #define W83792D_REG_TEMP3_CONFIG 0xCA
  121. static const u8 W83792D_REG_TEMP1[3] = {
  122. 0x27, /* TEMP 1 in DataSheet */
  123. 0x39, /* TEMP 1 Over in DataSheet */
  124. 0x3A, /* TEMP 1 Hyst in DataSheet */
  125. };
  126. static const u8 W83792D_REG_TEMP_ADD[2][6] = {
  127. { 0xC0, /* TEMP 2 in DataSheet */
  128. 0xC1, /* TEMP 2(0.5 deg) in DataSheet */
  129. 0xC5, /* TEMP 2 Over High part in DataSheet */
  130. 0xC6, /* TEMP 2 Over Low part in DataSheet */
  131. 0xC3, /* TEMP 2 Thyst High part in DataSheet */
  132. 0xC4 }, /* TEMP 2 Thyst Low part in DataSheet */
  133. { 0xC8, /* TEMP 3 in DataSheet */
  134. 0xC9, /* TEMP 3(0.5 deg) in DataSheet */
  135. 0xCD, /* TEMP 3 Over High part in DataSheet */
  136. 0xCE, /* TEMP 3 Over Low part in DataSheet */
  137. 0xCB, /* TEMP 3 Thyst High part in DataSheet */
  138. 0xCC } /* TEMP 3 Thyst Low part in DataSheet */
  139. };
  140. static const u8 W83792D_REG_THERMAL[3] = {
  141. 0x85, /* SmartFanI: Fan1 target value */
  142. 0x86, /* SmartFanI: Fan2 target value */
  143. 0x96 /* SmartFanI: Fan3 target value */
  144. };
  145. static const u8 W83792D_REG_TOLERANCE[3] = {
  146. 0x87, /* (bit3-0)SmartFan Fan1 tolerance */
  147. 0x87, /* (bit7-4)SmartFan Fan2 tolerance */
  148. 0x97 /* (bit3-0)SmartFan Fan3 tolerance */
  149. };
  150. static const u8 W83792D_REG_POINTS[3][4] = {
  151. { 0x85, /* SmartFanII: Fan1 temp point 1 */
  152. 0xE3, /* SmartFanII: Fan1 temp point 2 */
  153. 0xE4, /* SmartFanII: Fan1 temp point 3 */
  154. 0xE5 }, /* SmartFanII: Fan1 temp point 4 */
  155. { 0x86, /* SmartFanII: Fan2 temp point 1 */
  156. 0xE6, /* SmartFanII: Fan2 temp point 2 */
  157. 0xE7, /* SmartFanII: Fan2 temp point 3 */
  158. 0xE8 }, /* SmartFanII: Fan2 temp point 4 */
  159. { 0x96, /* SmartFanII: Fan3 temp point 1 */
  160. 0xE9, /* SmartFanII: Fan3 temp point 2 */
  161. 0xEA, /* SmartFanII: Fan3 temp point 3 */
  162. 0xEB } /* SmartFanII: Fan3 temp point 4 */
  163. };
  164. static const u8 W83792D_REG_LEVELS[3][4] = {
  165. { 0x88, /* (bit3-0) SmartFanII: Fan1 Non-Stop */
  166. 0x88, /* (bit7-4) SmartFanII: Fan1 Level 1 */
  167. 0xE0, /* (bit7-4) SmartFanII: Fan1 Level 2 */
  168. 0xE0 }, /* (bit3-0) SmartFanII: Fan1 Level 3 */
  169. { 0x89, /* (bit3-0) SmartFanII: Fan2 Non-Stop */
  170. 0x89, /* (bit7-4) SmartFanII: Fan2 Level 1 */
  171. 0xE1, /* (bit7-4) SmartFanII: Fan2 Level 2 */
  172. 0xE1 }, /* (bit3-0) SmartFanII: Fan2 Level 3 */
  173. { 0x98, /* (bit3-0) SmartFanII: Fan3 Non-Stop */
  174. 0x98, /* (bit7-4) SmartFanII: Fan3 Level 1 */
  175. 0xE2, /* (bit7-4) SmartFanII: Fan3 Level 2 */
  176. 0xE2 } /* (bit3-0) SmartFanII: Fan3 Level 3 */
  177. };
  178. #define W83792D_REG_CONFIG 0x40
  179. #define W83792D_REG_VID_FANDIV 0x47
  180. #define W83792D_REG_CHIPID 0x49
  181. #define W83792D_REG_WCHIPID 0x58
  182. #define W83792D_REG_CHIPMAN 0x4F
  183. #define W83792D_REG_PIN 0x4B
  184. #define W83792D_REG_I2C_SUBADDR 0x4A
  185. #define W83792D_REG_ALARM1 0xA9 /* realtime status register1 */
  186. #define W83792D_REG_ALARM2 0xAA /* realtime status register2 */
  187. #define W83792D_REG_ALARM3 0xAB /* realtime status register3 */
  188. #define W83792D_REG_CHASSIS 0x42 /* Bit 5: Case Open status bit */
  189. #define W83792D_REG_CHASSIS_CLR 0x44 /* Bit 7: Case Open CLR_CHS/Reset bit */
  190. /* control in0/in1 's limit modifiability */
  191. #define W83792D_REG_VID_IN_B 0x17
  192. #define W83792D_REG_VBAT 0x5D
  193. #define W83792D_REG_I2C_ADDR 0x48
  194. /* Conversions. Rounding and limit checking is only done on the TO_REG
  195. variants. Note that you should be a bit careful with which arguments
  196. these macros are called: arguments may be evaluated more than once.
  197. Fixing this is just not worth it. */
  198. #define IN_FROM_REG(nr,val) (((nr)<=1)?(val*2): \
  199. ((((nr)==6)||((nr)==7))?(val*6):(val*4)))
  200. #define IN_TO_REG(nr,val) (((nr)<=1)?(val/2): \
  201. ((((nr)==6)||((nr)==7))?(val/6):(val/4)))
  202. static inline u8
  203. FAN_TO_REG(long rpm, int div)
  204. {
  205. if (rpm == 0)
  206. return 255;
  207. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  208. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  209. }
  210. #define FAN_FROM_REG(val,div) ((val) == 0 ? -1 : \
  211. ((val) == 255 ? 0 : \
  212. 1350000 / ((val) * (div))))
  213. /* for temp1 */
  214. #define TEMP1_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
  215. : (val)) / 1000, 0, 0xff))
  216. #define TEMP1_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
  217. /* for temp2 and temp3, because they need addtional resolution */
  218. #define TEMP_ADD_FROM_REG(val1, val2) \
  219. ((((val1) & 0x80 ? (val1)-0x100 \
  220. : (val1)) * 1000) + ((val2 & 0x80) ? 500 : 0))
  221. #define TEMP_ADD_TO_REG_HIGH(val) \
  222. (SENSORS_LIMIT(((val) < 0 ? (val)+0x100*1000 \
  223. : (val)) / 1000, 0, 0xff))
  224. #define TEMP_ADD_TO_REG_LOW(val) ((val%1000) ? 0x80 : 0x00)
  225. #define PWM_FROM_REG(val) (val)
  226. #define PWM_TO_REG(val) (SENSORS_LIMIT((val),0,255))
  227. #define DIV_FROM_REG(val) (1 << (val))
  228. static inline u8
  229. DIV_TO_REG(long val)
  230. {
  231. int i;
  232. val = SENSORS_LIMIT(val, 1, 128) >> 1;
  233. for (i = 0; i < 6; i++) {
  234. if (val == 0)
  235. break;
  236. val >>= 1;
  237. }
  238. return ((u8) i);
  239. }
  240. struct w83792d_data {
  241. struct i2c_client client;
  242. struct class_device *class_dev;
  243. struct semaphore lock;
  244. enum chips type;
  245. struct semaphore update_lock;
  246. char valid; /* !=0 if following fields are valid */
  247. unsigned long last_updated; /* In jiffies */
  248. /* array of 2 pointers to subclients */
  249. struct i2c_client *lm75[2];
  250. u8 in[9]; /* Register value */
  251. u8 in_max[9]; /* Register value */
  252. u8 in_min[9]; /* Register value */
  253. u8 low_bits[2]; /* Additional resolution to voltage in0-6 */
  254. u8 fan[7]; /* Register value */
  255. u8 fan_min[7]; /* Register value */
  256. u8 temp1[3]; /* current, over, thyst */
  257. u8 temp_add[2][6]; /* Register value */
  258. u8 fan_div[7]; /* Register encoding, shifted right */
  259. u8 pwm[7]; /* We only consider the first 3 set of pwm,
  260. although 792 chip has 7 set of pwm. */
  261. u8 pwmenable[3];
  262. u8 pwm_mode[7]; /* indicates PWM or DC mode: 1->PWM; 0->DC */
  263. u32 alarms; /* realtime status register encoding,combined */
  264. u8 chassis; /* Chassis status */
  265. u8 chassis_clear; /* CLR_CHS, clear chassis intrusion detection */
  266. u8 thermal_cruise[3]; /* Smart FanI: Fan1,2,3 target value */
  267. u8 tolerance[3]; /* Fan1,2,3 tolerance(Smart Fan I/II) */
  268. u8 sf2_points[3][4]; /* Smart FanII: Fan1,2,3 temperature points */
  269. u8 sf2_levels[3][4]; /* Smart FanII: Fan1,2,3 duty cycle levels */
  270. };
  271. static int w83792d_attach_adapter(struct i2c_adapter *adapter);
  272. static int w83792d_detect(struct i2c_adapter *adapter, int address, int kind);
  273. static int w83792d_detach_client(struct i2c_client *client);
  274. static int w83792d_read_value(struct i2c_client *client, u8 register);
  275. static int w83792d_write_value(struct i2c_client *client, u8 register,
  276. u8 value);
  277. static struct w83792d_data *w83792d_update_device(struct device *dev);
  278. #ifdef DEBUG
  279. static void w83792d_print_debug(struct w83792d_data *data, struct device *dev);
  280. #endif
  281. static void w83792d_init_client(struct i2c_client *client);
  282. static struct i2c_driver w83792d_driver = {
  283. .owner = THIS_MODULE,
  284. .name = "w83792d",
  285. .flags = I2C_DF_NOTIFY,
  286. .attach_adapter = w83792d_attach_adapter,
  287. .detach_client = w83792d_detach_client,
  288. };
  289. static long in_count_from_reg(int nr, struct w83792d_data *data)
  290. {
  291. u16 vol_count = data->in[nr];
  292. u16 low_bits = 0;
  293. vol_count = (vol_count << 2);
  294. switch (nr)
  295. {
  296. case 0: /* vin0 */
  297. low_bits = (data->low_bits[0]) & 0x03;
  298. break;
  299. case 1: /* vin1 */
  300. low_bits = ((data->low_bits[0]) & 0x0c) >> 2;
  301. break;
  302. case 2: /* vin2 */
  303. low_bits = ((data->low_bits[0]) & 0x30) >> 4;
  304. break;
  305. case 3: /* vin3 */
  306. low_bits = ((data->low_bits[0]) & 0xc0) >> 6;
  307. break;
  308. case 4: /* vin4 */
  309. low_bits = (data->low_bits[1]) & 0x03;
  310. break;
  311. case 5: /* vin5 */
  312. low_bits = ((data->low_bits[1]) & 0x0c) >> 2;
  313. break;
  314. case 6: /* vin6 */
  315. low_bits = ((data->low_bits[1]) & 0x30) >> 4;
  316. default:
  317. break;
  318. }
  319. vol_count = vol_count | low_bits;
  320. return vol_count;
  321. }
  322. /* following are the sysfs callback functions */
  323. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  324. char *buf)
  325. {
  326. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  327. int nr = sensor_attr->index;
  328. struct w83792d_data *data = w83792d_update_device(dev);
  329. return sprintf(buf,"%ld\n", IN_FROM_REG(nr,(in_count_from_reg(nr, data))));
  330. }
  331. #define show_in_reg(reg) \
  332. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  333. char *buf) \
  334. { \
  335. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  336. int nr = sensor_attr->index; \
  337. struct w83792d_data *data = w83792d_update_device(dev); \
  338. return sprintf(buf,"%ld\n", (long)(IN_FROM_REG(nr, (data->reg[nr])*4))); \
  339. }
  340. show_in_reg(in_min);
  341. show_in_reg(in_max);
  342. #define store_in_reg(REG, reg) \
  343. static ssize_t store_in_##reg (struct device *dev, \
  344. struct device_attribute *attr, \
  345. const char *buf, size_t count) \
  346. { \
  347. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  348. int nr = sensor_attr->index; \
  349. struct i2c_client *client = to_i2c_client(dev); \
  350. struct w83792d_data *data = i2c_get_clientdata(client); \
  351. u32 val; \
  352. \
  353. val = simple_strtoul(buf, NULL, 10); \
  354. data->in_##reg[nr] = SENSORS_LIMIT(IN_TO_REG(nr, val)/4, 0, 255); \
  355. w83792d_write_value(client, W83792D_REG_IN_##REG[nr], data->in_##reg[nr]); \
  356. \
  357. return count; \
  358. }
  359. store_in_reg(MIN, min);
  360. store_in_reg(MAX, max);
  361. #define sysfs_in_reg(offset) \
  362. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
  363. NULL, offset); \
  364. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  365. show_in_min, store_in_min, offset); \
  366. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  367. show_in_max, store_in_max, offset);
  368. sysfs_in_reg(0);
  369. sysfs_in_reg(1);
  370. sysfs_in_reg(2);
  371. sysfs_in_reg(3);
  372. sysfs_in_reg(4);
  373. sysfs_in_reg(5);
  374. sysfs_in_reg(6);
  375. sysfs_in_reg(7);
  376. sysfs_in_reg(8);
  377. #define device_create_file_in(client, offset) \
  378. do { \
  379. device_create_file(&client->dev, &sensor_dev_attr_in##offset##_input.dev_attr); \
  380. device_create_file(&client->dev, &sensor_dev_attr_in##offset##_max.dev_attr); \
  381. device_create_file(&client->dev, &sensor_dev_attr_in##offset##_min.dev_attr); \
  382. } while (0)
  383. #define show_fan_reg(reg) \
  384. static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \
  385. char *buf) \
  386. { \
  387. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  388. int nr = sensor_attr->index - 1; \
  389. struct w83792d_data *data = w83792d_update_device(dev); \
  390. return sprintf(buf,"%d\n", \
  391. FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
  392. }
  393. show_fan_reg(fan);
  394. show_fan_reg(fan_min);
  395. static ssize_t
  396. store_fan_min(struct device *dev, struct device_attribute *attr,
  397. const char *buf, size_t count)
  398. {
  399. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  400. int nr = sensor_attr->index - 1;
  401. struct i2c_client *client = to_i2c_client(dev);
  402. struct w83792d_data *data = i2c_get_clientdata(client);
  403. u32 val;
  404. val = simple_strtoul(buf, NULL, 10);
  405. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  406. w83792d_write_value(client, W83792D_REG_FAN_MIN[nr],
  407. data->fan_min[nr]);
  408. return count;
  409. }
  410. static ssize_t
  411. show_fan_div(struct device *dev, struct device_attribute *attr,
  412. char *buf)
  413. {
  414. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  415. int nr = sensor_attr->index;
  416. struct w83792d_data *data = w83792d_update_device(dev);
  417. return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr - 1]));
  418. }
  419. /* Note: we save and restore the fan minimum here, because its value is
  420. determined in part by the fan divisor. This follows the principle of
  421. least suprise; the user doesn't expect the fan minimum to change just
  422. because the divisor changed. */
  423. static ssize_t
  424. store_fan_div(struct device *dev, struct device_attribute *attr,
  425. const char *buf, size_t count)
  426. {
  427. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  428. int nr = sensor_attr->index - 1;
  429. struct i2c_client *client = to_i2c_client(dev);
  430. struct w83792d_data *data = i2c_get_clientdata(client);
  431. unsigned long min;
  432. /*u8 reg;*/
  433. u8 fan_div_reg = 0;
  434. u8 tmp_fan_div;
  435. /* Save fan_min */
  436. min = FAN_FROM_REG(data->fan_min[nr],
  437. DIV_FROM_REG(data->fan_div[nr]));
  438. data->fan_div[nr] = DIV_TO_REG(simple_strtoul(buf, NULL, 10));
  439. fan_div_reg = w83792d_read_value(client, W83792D_REG_FAN_DIV[nr >> 1]);
  440. fan_div_reg &= (nr & 0x01) ? 0x8f : 0xf8;
  441. tmp_fan_div = (nr & 0x01) ? (((data->fan_div[nr]) << 4) & 0x70)
  442. : ((data->fan_div[nr]) & 0x07);
  443. w83792d_write_value(client, W83792D_REG_FAN_DIV[nr >> 1],
  444. fan_div_reg | tmp_fan_div);
  445. /* Restore fan_min */
  446. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  447. w83792d_write_value(client, W83792D_REG_FAN_MIN[nr], data->fan_min[nr]);
  448. return count;
  449. }
  450. #define sysfs_fan(offset) \
  451. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
  452. offset); \
  453. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  454. show_fan_div, store_fan_div, offset); \
  455. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  456. show_fan_min, store_fan_min, offset);
  457. sysfs_fan(1);
  458. sysfs_fan(2);
  459. sysfs_fan(3);
  460. sysfs_fan(4);
  461. sysfs_fan(5);
  462. sysfs_fan(6);
  463. sysfs_fan(7);
  464. #define device_create_file_fan(client, offset) \
  465. do { \
  466. device_create_file(&client->dev, &sensor_dev_attr_fan##offset##_input.dev_attr); \
  467. device_create_file(&client->dev, &sensor_dev_attr_fan##offset##_div.dev_attr); \
  468. device_create_file(&client->dev, &sensor_dev_attr_fan##offset##_min.dev_attr); \
  469. } while (0)
  470. /* read/write the temperature1, includes measured value and limits */
  471. static ssize_t show_temp1(struct device *dev, struct device_attribute *attr,
  472. char *buf)
  473. {
  474. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  475. int nr = sensor_attr->index;
  476. struct w83792d_data *data = w83792d_update_device(dev);
  477. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp1[nr]));
  478. }
  479. static ssize_t store_temp1(struct device *dev, struct device_attribute *attr,
  480. const char *buf, size_t count)
  481. {
  482. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  483. int nr = sensor_attr->index;
  484. struct i2c_client *client = to_i2c_client(dev);
  485. struct w83792d_data *data = i2c_get_clientdata(client);
  486. s32 val;
  487. val = simple_strtol(buf, NULL, 10);
  488. data->temp1[nr] = TEMP1_TO_REG(val);
  489. w83792d_write_value(client, W83792D_REG_TEMP1[nr],
  490. data->temp1[nr]);
  491. return count;
  492. }
  493. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL, 0);
  494. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp1,
  495. store_temp1, 1);
  496. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp1,
  497. store_temp1, 2);
  498. #define device_create_file_temp1(client) \
  499. do { \
  500. device_create_file(&client->dev, &sensor_dev_attr_temp1_input.dev_attr); \
  501. device_create_file(&client->dev, &sensor_dev_attr_temp1_max.dev_attr); \
  502. device_create_file(&client->dev, &sensor_dev_attr_temp1_max_hyst.dev_attr); \
  503. } while (0)
  504. /* read/write the temperature2-3, includes measured value and limits */
  505. static ssize_t show_temp23(struct device *dev, struct device_attribute *attr,
  506. char *buf)
  507. {
  508. struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
  509. int nr = sensor_attr->nr;
  510. int index = sensor_attr->index;
  511. struct w83792d_data *data = w83792d_update_device(dev);
  512. return sprintf(buf,"%ld\n",
  513. (long)TEMP_ADD_FROM_REG(data->temp_add[nr][index],
  514. data->temp_add[nr][index+1]));
  515. }
  516. static ssize_t store_temp23(struct device *dev, struct device_attribute *attr,
  517. const char *buf, size_t count)
  518. {
  519. struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
  520. int nr = sensor_attr->nr;
  521. int index = sensor_attr->index;
  522. struct i2c_client *client = to_i2c_client(dev);
  523. struct w83792d_data *data = i2c_get_clientdata(client);
  524. s32 val;
  525. val = simple_strtol(buf, NULL, 10);
  526. data->temp_add[nr][index] = TEMP_ADD_TO_REG_HIGH(val);
  527. data->temp_add[nr][index+1] = TEMP_ADD_TO_REG_LOW(val);
  528. w83792d_write_value(client, W83792D_REG_TEMP_ADD[nr][index],
  529. data->temp_add[nr][index]);
  530. w83792d_write_value(client, W83792D_REG_TEMP_ADD[nr][index+1],
  531. data->temp_add[nr][index+1]);
  532. return count;
  533. }
  534. #define sysfs_temp23(name,idx) \
  535. static SENSOR_DEVICE_ATTR_2(name##_input, S_IRUGO, show_temp23, NULL, \
  536. idx, 0); \
  537. static SENSOR_DEVICE_ATTR_2(name##_max, S_IRUGO | S_IWUSR, \
  538. show_temp23, store_temp23, idx, 2); \
  539. static SENSOR_DEVICE_ATTR_2(name##_max_hyst, S_IRUGO | S_IWUSR, \
  540. show_temp23, store_temp23, idx, 4);
  541. sysfs_temp23(temp2,0)
  542. sysfs_temp23(temp3,1)
  543. #define device_create_file_temp_add(client, offset) \
  544. do { \
  545. device_create_file(&client->dev, &sensor_dev_attr_temp##offset##_input.dev_attr); \
  546. device_create_file(&client->dev, &sensor_dev_attr_temp##offset##_max.dev_attr); \
  547. device_create_file(&client->dev, \
  548. &sensor_dev_attr_temp##offset##_max_hyst.dev_attr); \
  549. } while (0)
  550. /* get reatime status of all sensors items: voltage, temp, fan */
  551. static ssize_t
  552. show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
  553. {
  554. struct w83792d_data *data = w83792d_update_device(dev);
  555. return sprintf(buf, "%d\n", data->alarms);
  556. }
  557. static
  558. DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  559. #define device_create_file_alarms(client) \
  560. device_create_file(&client->dev, &dev_attr_alarms);
  561. static ssize_t
  562. show_pwm(struct device *dev, struct device_attribute *attr,
  563. char *buf)
  564. {
  565. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  566. int nr = sensor_attr->index;
  567. struct w83792d_data *data = w83792d_update_device(dev);
  568. return sprintf(buf, "%ld\n", (long) PWM_FROM_REG(data->pwm[nr-1]));
  569. }
  570. static ssize_t
  571. show_pwmenable(struct device *dev, struct device_attribute *attr,
  572. char *buf)
  573. {
  574. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  575. int nr = sensor_attr->index - 1;
  576. struct w83792d_data *data = w83792d_update_device(dev);
  577. long pwm_enable_tmp = 1;
  578. switch (data->pwmenable[nr]) {
  579. case 0:
  580. pwm_enable_tmp = 1; /* manual mode */
  581. break;
  582. case 1:
  583. pwm_enable_tmp = 3; /*thermal cruise/Smart Fan I */
  584. break;
  585. case 2:
  586. pwm_enable_tmp = 2; /* Smart Fan II */
  587. break;
  588. }
  589. return sprintf(buf, "%ld\n", pwm_enable_tmp);
  590. }
  591. static ssize_t
  592. store_pwm(struct device *dev, struct device_attribute *attr,
  593. const char *buf, size_t count)
  594. {
  595. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  596. int nr = sensor_attr->index - 1;
  597. struct i2c_client *client = to_i2c_client(dev);
  598. struct w83792d_data *data = i2c_get_clientdata(client);
  599. u32 val;
  600. val = simple_strtoul(buf, NULL, 10);
  601. data->pwm[nr] = PWM_TO_REG(val);
  602. w83792d_write_value(client, W83792D_REG_PWM[nr], data->pwm[nr]);
  603. return count;
  604. }
  605. static ssize_t
  606. store_pwmenable(struct device *dev, struct device_attribute *attr,
  607. const char *buf, size_t count)
  608. {
  609. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  610. int nr = sensor_attr->index - 1;
  611. struct i2c_client *client = to_i2c_client(dev);
  612. struct w83792d_data *data = i2c_get_clientdata(client);
  613. u32 val;
  614. u8 fan_cfg_tmp, cfg1_tmp, cfg2_tmp, cfg3_tmp, cfg4_tmp;
  615. val = simple_strtoul(buf, NULL, 10);
  616. switch (val) {
  617. case 1:
  618. data->pwmenable[nr] = 0; /* manual mode */
  619. break;
  620. case 2:
  621. data->pwmenable[nr] = 2; /* Smart Fan II */
  622. break;
  623. case 3:
  624. data->pwmenable[nr] = 1; /* thermal cruise/Smart Fan I */
  625. break;
  626. default:
  627. return -EINVAL;
  628. }
  629. cfg1_tmp = data->pwmenable[0];
  630. cfg2_tmp = (data->pwmenable[1]) << 2;
  631. cfg3_tmp = (data->pwmenable[2]) << 4;
  632. cfg4_tmp = w83792d_read_value(client,W83792D_REG_FAN_CFG) & 0xc0;
  633. fan_cfg_tmp = ((cfg4_tmp | cfg3_tmp) | cfg2_tmp) | cfg1_tmp;
  634. w83792d_write_value(client, W83792D_REG_FAN_CFG, fan_cfg_tmp);
  635. return count;
  636. }
  637. #define sysfs_pwm(offset) \
  638. static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  639. show_pwm, store_pwm, offset); \
  640. static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
  641. show_pwmenable, store_pwmenable, offset); \
  642. sysfs_pwm(1);
  643. sysfs_pwm(2);
  644. sysfs_pwm(3);
  645. #define device_create_file_pwm(client, offset) \
  646. do { \
  647. device_create_file(&client->dev, &sensor_dev_attr_pwm##offset.dev_attr); \
  648. } while (0)
  649. #define device_create_file_pwmenable(client, offset) \
  650. do { \
  651. device_create_file(&client->dev, &sensor_dev_attr_pwm##offset##_enable.dev_attr); \
  652. } while (0)
  653. static ssize_t
  654. show_pwm_mode(struct device *dev, struct device_attribute *attr,
  655. char *buf)
  656. {
  657. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  658. int nr = sensor_attr->index;
  659. struct w83792d_data *data = w83792d_update_device(dev);
  660. return sprintf(buf, "%d\n", data->pwm_mode[nr-1]);
  661. }
  662. static ssize_t
  663. store_pwm_mode(struct device *dev, struct device_attribute *attr,
  664. const char *buf, size_t count)
  665. {
  666. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  667. int nr = sensor_attr->index - 1;
  668. struct i2c_client *client = to_i2c_client(dev);
  669. struct w83792d_data *data = i2c_get_clientdata(client);
  670. u32 val;
  671. u8 pwm_mode_mask = 0;
  672. val = simple_strtoul(buf, NULL, 10);
  673. data->pwm_mode[nr] = SENSORS_LIMIT(val, 0, 1);
  674. pwm_mode_mask = w83792d_read_value(client,
  675. W83792D_REG_PWM[nr]) & 0x7f;
  676. w83792d_write_value(client, W83792D_REG_PWM[nr],
  677. ((data->pwm_mode[nr]) << 7) | pwm_mode_mask);
  678. return count;
  679. }
  680. #define sysfs_pwm_mode(offset) \
  681. static SENSOR_DEVICE_ATTR(pwm##offset##_mode, S_IRUGO | S_IWUSR, \
  682. show_pwm_mode, store_pwm_mode, offset);
  683. sysfs_pwm_mode(1);
  684. sysfs_pwm_mode(2);
  685. sysfs_pwm_mode(3);
  686. #define device_create_file_pwm_mode(client, offset) \
  687. do { \
  688. device_create_file(&client->dev, &sensor_dev_attr_pwm##offset##_mode.dev_attr); \
  689. } while (0)
  690. static ssize_t
  691. show_regs_chassis(struct device *dev, struct device_attribute *attr,
  692. char *buf)
  693. {
  694. struct w83792d_data *data = w83792d_update_device(dev);
  695. return sprintf(buf, "%d\n", data->chassis);
  696. }
  697. static DEVICE_ATTR(chassis, S_IRUGO, show_regs_chassis, NULL);
  698. #define device_create_file_chassis(client) \
  699. do { \
  700. device_create_file(&client->dev, &dev_attr_chassis); \
  701. } while (0)
  702. static ssize_t
  703. show_chassis_clear(struct device *dev, struct device_attribute *attr, char *buf)
  704. {
  705. struct w83792d_data *data = w83792d_update_device(dev);
  706. return sprintf(buf, "%d\n", data->chassis_clear);
  707. }
  708. static ssize_t
  709. store_chassis_clear(struct device *dev, struct device_attribute *attr,
  710. const char *buf, size_t count)
  711. {
  712. struct i2c_client *client = to_i2c_client(dev);
  713. struct w83792d_data *data = i2c_get_clientdata(client);
  714. u32 val;
  715. u8 temp1 = 0, temp2 = 0;
  716. val = simple_strtoul(buf, NULL, 10);
  717. data->chassis_clear = SENSORS_LIMIT(val, 0 ,1);
  718. temp1 = ((data->chassis_clear) << 7) & 0x80;
  719. temp2 = w83792d_read_value(client,
  720. W83792D_REG_CHASSIS_CLR) & 0x7f;
  721. w83792d_write_value(client, W83792D_REG_CHASSIS_CLR, temp1 | temp2);
  722. return count;
  723. }
  724. static DEVICE_ATTR(chassis_clear, S_IRUGO | S_IWUSR,
  725. show_chassis_clear, store_chassis_clear);
  726. #define device_create_file_chassis_clear(client) \
  727. do { \
  728. device_create_file(&client->dev, &dev_attr_chassis_clear); \
  729. } while (0)
  730. /* For Smart Fan I / Thermal Cruise */
  731. static ssize_t
  732. show_thermal_cruise(struct device *dev, struct device_attribute *attr,
  733. char *buf)
  734. {
  735. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  736. int nr = sensor_attr->index;
  737. struct w83792d_data *data = w83792d_update_device(dev);
  738. return sprintf(buf, "%ld\n", (long)data->thermal_cruise[nr-1]);
  739. }
  740. static ssize_t
  741. store_thermal_cruise(struct device *dev, struct device_attribute *attr,
  742. const char *buf, size_t count)
  743. {
  744. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  745. int nr = sensor_attr->index - 1;
  746. struct i2c_client *client = to_i2c_client(dev);
  747. struct w83792d_data *data = i2c_get_clientdata(client);
  748. u32 val;
  749. u8 target_tmp=0, target_mask=0;
  750. val = simple_strtoul(buf, NULL, 10);
  751. target_tmp = val;
  752. target_tmp = target_tmp & 0x7f;
  753. target_mask = w83792d_read_value(client, W83792D_REG_THERMAL[nr]) & 0x80;
  754. data->thermal_cruise[nr] = SENSORS_LIMIT(target_tmp, 0, 255);
  755. w83792d_write_value(client, W83792D_REG_THERMAL[nr],
  756. (data->thermal_cruise[nr]) | target_mask);
  757. return count;
  758. }
  759. #define sysfs_thermal_cruise(offset) \
  760. static SENSOR_DEVICE_ATTR(thermal_cruise##offset, S_IRUGO | S_IWUSR, \
  761. show_thermal_cruise, store_thermal_cruise, offset);
  762. sysfs_thermal_cruise(1);
  763. sysfs_thermal_cruise(2);
  764. sysfs_thermal_cruise(3);
  765. #define device_create_file_thermal_cruise(client, offset) \
  766. do { \
  767. device_create_file(&client->dev, \
  768. &sensor_dev_attr_thermal_cruise##offset.dev_attr); \
  769. } while (0)
  770. /* For Smart Fan I/Thermal Cruise and Smart Fan II */
  771. static ssize_t
  772. show_tolerance(struct device *dev, struct device_attribute *attr,
  773. char *buf)
  774. {
  775. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  776. int nr = sensor_attr->index;
  777. struct w83792d_data *data = w83792d_update_device(dev);
  778. return sprintf(buf, "%ld\n", (long)data->tolerance[nr-1]);
  779. }
  780. static ssize_t
  781. store_tolerance(struct device *dev, struct device_attribute *attr,
  782. const char *buf, size_t count)
  783. {
  784. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  785. int nr = sensor_attr->index - 1;
  786. struct i2c_client *client = to_i2c_client(dev);
  787. struct w83792d_data *data = i2c_get_clientdata(client);
  788. u32 val;
  789. u8 tol_tmp, tol_mask;
  790. val = simple_strtoul(buf, NULL, 10);
  791. tol_mask = w83792d_read_value(client,
  792. W83792D_REG_TOLERANCE[nr]) & ((nr == 1) ? 0x0f : 0xf0);
  793. tol_tmp = SENSORS_LIMIT(val, 0, 15);
  794. tol_tmp &= 0x0f;
  795. data->tolerance[nr] = tol_tmp;
  796. if (nr == 1) {
  797. tol_tmp <<= 4;
  798. }
  799. w83792d_write_value(client, W83792D_REG_TOLERANCE[nr],
  800. tol_mask | tol_tmp);
  801. return count;
  802. }
  803. #define sysfs_tolerance(offset) \
  804. static SENSOR_DEVICE_ATTR(tolerance##offset, S_IRUGO | S_IWUSR, \
  805. show_tolerance, store_tolerance, offset);
  806. sysfs_tolerance(1);
  807. sysfs_tolerance(2);
  808. sysfs_tolerance(3);
  809. #define device_create_file_tolerance(client, offset) \
  810. do { \
  811. device_create_file(&client->dev, &sensor_dev_attr_tolerance##offset.dev_attr); \
  812. } while (0)
  813. /* For Smart Fan II */
  814. static ssize_t
  815. show_sf2_point(struct device *dev, struct device_attribute *attr,
  816. char *buf)
  817. {
  818. struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
  819. int nr = sensor_attr->nr;
  820. int index = sensor_attr->index;
  821. struct w83792d_data *data = w83792d_update_device(dev);
  822. return sprintf(buf, "%ld\n", (long)data->sf2_points[index-1][nr-1]);
  823. }
  824. static ssize_t
  825. store_sf2_point(struct device *dev, struct device_attribute *attr,
  826. const char *buf, size_t count)
  827. {
  828. struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
  829. int nr = sensor_attr->nr - 1;
  830. int index = sensor_attr->index - 1;
  831. struct i2c_client *client = to_i2c_client(dev);
  832. struct w83792d_data *data = i2c_get_clientdata(client);
  833. u32 val;
  834. u8 mask_tmp = 0;
  835. val = simple_strtoul(buf, NULL, 10);
  836. data->sf2_points[index][nr] = SENSORS_LIMIT(val, 0, 127);
  837. mask_tmp = w83792d_read_value(client,
  838. W83792D_REG_POINTS[index][nr]) & 0x80;
  839. w83792d_write_value(client, W83792D_REG_POINTS[index][nr],
  840. mask_tmp|data->sf2_points[index][nr]);
  841. return count;
  842. }
  843. #define sysfs_sf2_point(offset, index) \
  844. static SENSOR_DEVICE_ATTR_2(sf2_point##offset##_fan##index, S_IRUGO | S_IWUSR, \
  845. show_sf2_point, store_sf2_point, offset, index);
  846. sysfs_sf2_point(1, 1); /* Fan1 */
  847. sysfs_sf2_point(2, 1); /* Fan1 */
  848. sysfs_sf2_point(3, 1); /* Fan1 */
  849. sysfs_sf2_point(4, 1); /* Fan1 */
  850. sysfs_sf2_point(1, 2); /* Fan2 */
  851. sysfs_sf2_point(2, 2); /* Fan2 */
  852. sysfs_sf2_point(3, 2); /* Fan2 */
  853. sysfs_sf2_point(4, 2); /* Fan2 */
  854. sysfs_sf2_point(1, 3); /* Fan3 */
  855. sysfs_sf2_point(2, 3); /* Fan3 */
  856. sysfs_sf2_point(3, 3); /* Fan3 */
  857. sysfs_sf2_point(4, 3); /* Fan3 */
  858. #define device_create_file_sf2_point(client, offset, index) \
  859. do { \
  860. device_create_file(&client->dev, \
  861. &sensor_dev_attr_sf2_point##offset##_fan##index.dev_attr); \
  862. } while (0)
  863. static ssize_t
  864. show_sf2_level(struct device *dev, struct device_attribute *attr,
  865. char *buf)
  866. {
  867. struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
  868. int nr = sensor_attr->nr;
  869. int index = sensor_attr->index;
  870. struct w83792d_data *data = w83792d_update_device(dev);
  871. return sprintf(buf, "%d\n",
  872. (((data->sf2_levels[index-1][nr]) * 100) / 15));
  873. }
  874. static ssize_t
  875. store_sf2_level(struct device *dev, struct device_attribute *attr,
  876. const char *buf, size_t count)
  877. {
  878. struct sensor_device_attribute_2 *sensor_attr = to_sensor_dev_attr_2(attr);
  879. int nr = sensor_attr->nr;
  880. int index = sensor_attr->index - 1;
  881. struct i2c_client *client = to_i2c_client(dev);
  882. struct w83792d_data *data = i2c_get_clientdata(client);
  883. u32 val;
  884. u8 mask_tmp=0, level_tmp=0;
  885. val = simple_strtoul(buf, NULL, 10);
  886. data->sf2_levels[index][nr] = SENSORS_LIMIT((val * 15) / 100, 0, 15);
  887. mask_tmp = w83792d_read_value(client, W83792D_REG_LEVELS[index][nr])
  888. & ((nr==3) ? 0xf0 : 0x0f);
  889. if (nr==3) {
  890. level_tmp = data->sf2_levels[index][nr];
  891. } else {
  892. level_tmp = data->sf2_levels[index][nr] << 4;
  893. }
  894. w83792d_write_value(client, W83792D_REG_LEVELS[index][nr], level_tmp | mask_tmp);
  895. return count;
  896. }
  897. #define sysfs_sf2_level(offset, index) \
  898. static SENSOR_DEVICE_ATTR_2(sf2_level##offset##_fan##index, S_IRUGO | S_IWUSR, \
  899. show_sf2_level, store_sf2_level, offset, index);
  900. sysfs_sf2_level(1, 1); /* Fan1 */
  901. sysfs_sf2_level(2, 1); /* Fan1 */
  902. sysfs_sf2_level(3, 1); /* Fan1 */
  903. sysfs_sf2_level(1, 2); /* Fan2 */
  904. sysfs_sf2_level(2, 2); /* Fan2 */
  905. sysfs_sf2_level(3, 2); /* Fan2 */
  906. sysfs_sf2_level(1, 3); /* Fan3 */
  907. sysfs_sf2_level(2, 3); /* Fan3 */
  908. sysfs_sf2_level(3, 3); /* Fan3 */
  909. #define device_create_file_sf2_level(client, offset, index) \
  910. do { \
  911. device_create_file(&client->dev, \
  912. &sensor_dev_attr_sf2_level##offset##_fan##index.dev_attr); \
  913. } while (0)
  914. /* This function is called when:
  915. * w83792d_driver is inserted (when this module is loaded), for each
  916. available adapter
  917. * when a new adapter is inserted (and w83792d_driver is still present) */
  918. static int
  919. w83792d_attach_adapter(struct i2c_adapter *adapter)
  920. {
  921. if (!(adapter->class & I2C_CLASS_HWMON))
  922. return 0;
  923. return i2c_probe(adapter, &addr_data, w83792d_detect);
  924. }
  925. static int
  926. w83792d_create_subclient(struct i2c_adapter *adapter,
  927. struct i2c_client *new_client, int addr,
  928. struct i2c_client **sub_cli)
  929. {
  930. int err;
  931. struct i2c_client *sub_client;
  932. (*sub_cli) = sub_client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
  933. if (!(sub_client)) {
  934. return -ENOMEM;
  935. }
  936. memset(sub_client, 0x00, sizeof(struct i2c_client));
  937. sub_client->addr = 0x48 + addr;
  938. i2c_set_clientdata(sub_client, NULL);
  939. sub_client->adapter = adapter;
  940. sub_client->driver = &w83792d_driver;
  941. sub_client->flags = 0;
  942. strlcpy(sub_client->name, "w83792d subclient", I2C_NAME_SIZE);
  943. if ((err = i2c_attach_client(sub_client))) {
  944. dev_err(&new_client->dev, "subclient registration "
  945. "at address 0x%x failed\n", sub_client->addr);
  946. kfree(sub_client);
  947. return err;
  948. }
  949. return 0;
  950. }
  951. static int
  952. w83792d_detect_subclients(struct i2c_adapter *adapter, int address, int kind,
  953. struct i2c_client *new_client)
  954. {
  955. int i, id, err;
  956. u8 val;
  957. struct w83792d_data *data = i2c_get_clientdata(new_client);
  958. id = i2c_adapter_id(adapter);
  959. if (force_subclients[0] == id && force_subclients[1] == address) {
  960. for (i = 2; i <= 3; i++) {
  961. if (force_subclients[i] < 0x48 ||
  962. force_subclients[i] > 0x4f) {
  963. dev_err(&new_client->dev, "invalid subclient "
  964. "address %d; must be 0x48-0x4f\n",
  965. force_subclients[i]);
  966. err = -ENODEV;
  967. goto ERROR_SC_0;
  968. }
  969. }
  970. w83792d_write_value(new_client, W83792D_REG_I2C_SUBADDR,
  971. (force_subclients[2] & 0x07) |
  972. ((force_subclients[3] & 0x07) << 4));
  973. }
  974. val = w83792d_read_value(new_client, W83792D_REG_I2C_SUBADDR);
  975. if (!(val & 0x08)) {
  976. err = w83792d_create_subclient(adapter, new_client, val & 0x7,
  977. &data->lm75[0]);
  978. if (err < 0)
  979. goto ERROR_SC_0;
  980. }
  981. if (!(val & 0x80)) {
  982. if ((data->lm75[0] != NULL) &&
  983. ((val & 0x7) == ((val >> 4) & 0x7))) {
  984. dev_err(&new_client->dev, "duplicate addresses 0x%x, "
  985. "use force_subclient\n", data->lm75[0]->addr);
  986. err = -ENODEV;
  987. goto ERROR_SC_1;
  988. }
  989. err = w83792d_create_subclient(adapter, new_client,
  990. (val >> 4) & 0x7, &data->lm75[1]);
  991. if (err < 0)
  992. goto ERROR_SC_1;
  993. }
  994. return 0;
  995. /* Undo inits in case of errors */
  996. ERROR_SC_1:
  997. if (data->lm75[0] != NULL) {
  998. i2c_detach_client(data->lm75[0]);
  999. kfree(data->lm75[0]);
  1000. }
  1001. ERROR_SC_0:
  1002. return err;
  1003. }
  1004. static int
  1005. w83792d_detect(struct i2c_adapter *adapter, int address, int kind)
  1006. {
  1007. int i = 0, val1 = 0, val2;
  1008. struct i2c_client *new_client;
  1009. struct w83792d_data *data;
  1010. int err = 0;
  1011. const char *client_name = "";
  1012. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1013. goto ERROR0;
  1014. }
  1015. /* OK. For now, we presume we have a valid client. We now create the
  1016. client structure, even though we cannot fill it completely yet.
  1017. But it allows us to access w83792d_{read,write}_value. */
  1018. if (!(data = kmalloc(sizeof(struct w83792d_data), GFP_KERNEL))) {
  1019. err = -ENOMEM;
  1020. goto ERROR0;
  1021. }
  1022. memset(data, 0, sizeof(struct w83792d_data));
  1023. new_client = &data->client;
  1024. i2c_set_clientdata(new_client, data);
  1025. new_client->addr = address;
  1026. init_MUTEX(&data->lock);
  1027. new_client->adapter = adapter;
  1028. new_client->driver = &w83792d_driver;
  1029. new_client->flags = 0;
  1030. /* Now, we do the remaining detection. */
  1031. /* The w83792d may be stuck in some other bank than bank 0. This may
  1032. make reading other information impossible. Specify a force=... or
  1033. force_*=... parameter, and the Winbond will be reset to the right
  1034. bank. */
  1035. if (kind < 0) {
  1036. if (w83792d_read_value(new_client, W83792D_REG_CONFIG) & 0x80) {
  1037. dev_warn(&new_client->dev, "Detection failed at step "
  1038. "3\n");
  1039. goto ERROR1;
  1040. }
  1041. val1 = w83792d_read_value(new_client, W83792D_REG_BANK);
  1042. val2 = w83792d_read_value(new_client, W83792D_REG_CHIPMAN);
  1043. /* Check for Winbond ID if in bank 0 */
  1044. if (!(val1 & 0x07)) { /* is Bank0 */
  1045. if (((!(val1 & 0x80)) && (val2 != 0xa3)) ||
  1046. ((val1 & 0x80) && (val2 != 0x5c))) {
  1047. goto ERROR1;
  1048. }
  1049. }
  1050. /* If Winbond chip, address of chip and W83792D_REG_I2C_ADDR
  1051. should match */
  1052. if (w83792d_read_value(new_client,
  1053. W83792D_REG_I2C_ADDR) != address) {
  1054. dev_warn(&new_client->dev, "Detection failed "
  1055. "at step 5\n");
  1056. goto ERROR1;
  1057. }
  1058. }
  1059. /* We have either had a force parameter, or we have already detected the
  1060. Winbond. Put it now into bank 0 and Vendor ID High Byte */
  1061. w83792d_write_value(new_client,
  1062. W83792D_REG_BANK,
  1063. (w83792d_read_value(new_client,
  1064. W83792D_REG_BANK) & 0x78) | 0x80);
  1065. /* Determine the chip type. */
  1066. if (kind <= 0) {
  1067. /* get vendor ID */
  1068. val2 = w83792d_read_value(new_client, W83792D_REG_CHIPMAN);
  1069. if (val2 != 0x5c) { /* the vendor is NOT Winbond */
  1070. goto ERROR1;
  1071. }
  1072. val1 = w83792d_read_value(new_client, W83792D_REG_WCHIPID);
  1073. if (val1 == 0x7a && address >= 0x2c) {
  1074. kind = w83792d;
  1075. } else {
  1076. if (kind == 0)
  1077. dev_warn(&new_client->dev,
  1078. "w83792d: Ignoring 'force' parameter for"
  1079. " unknown chip at adapter %d, address"
  1080. " 0x%02x\n", i2c_adapter_id(adapter),
  1081. address);
  1082. goto ERROR1;
  1083. }
  1084. }
  1085. if (kind == w83792d) {
  1086. client_name = "w83792d";
  1087. } else {
  1088. dev_err(&new_client->dev, "w83792d: Internal error: unknown"
  1089. " kind (%d)?!?", kind);
  1090. goto ERROR1;
  1091. }
  1092. /* Fill in the remaining client fields and put into the global list */
  1093. strlcpy(new_client->name, client_name, I2C_NAME_SIZE);
  1094. data->type = kind;
  1095. data->valid = 0;
  1096. init_MUTEX(&data->update_lock);
  1097. /* Tell the I2C layer a new client has arrived */
  1098. if ((err = i2c_attach_client(new_client)))
  1099. goto ERROR1;
  1100. if ((err = w83792d_detect_subclients(adapter, address,
  1101. kind, new_client)))
  1102. goto ERROR2;
  1103. /* Initialize the chip */
  1104. w83792d_init_client(new_client);
  1105. /* A few vars need to be filled upon startup */
  1106. for (i = 1; i <= 7; i++) {
  1107. data->fan_min[i - 1] = w83792d_read_value(new_client,
  1108. W83792D_REG_FAN_MIN[i]);
  1109. }
  1110. /* Register sysfs hooks */
  1111. data->class_dev = hwmon_device_register(&new_client->dev);
  1112. if (IS_ERR(data->class_dev)) {
  1113. err = PTR_ERR(data->class_dev);
  1114. goto ERROR3;
  1115. }
  1116. device_create_file_in(new_client, 0);
  1117. device_create_file_in(new_client, 1);
  1118. device_create_file_in(new_client, 2);
  1119. device_create_file_in(new_client, 3);
  1120. device_create_file_in(new_client, 4);
  1121. device_create_file_in(new_client, 5);
  1122. device_create_file_in(new_client, 6);
  1123. device_create_file_in(new_client, 7);
  1124. device_create_file_in(new_client, 8);
  1125. device_create_file_fan(new_client, 1);
  1126. device_create_file_fan(new_client, 2);
  1127. device_create_file_fan(new_client, 3);
  1128. device_create_file_fan(new_client, 4);
  1129. device_create_file_fan(new_client, 5);
  1130. device_create_file_fan(new_client, 6);
  1131. device_create_file_fan(new_client, 7);
  1132. device_create_file_temp1(new_client); /* Temp1 */
  1133. device_create_file_temp_add(new_client, 2); /* Temp2 */
  1134. device_create_file_temp_add(new_client, 3); /* Temp3 */
  1135. device_create_file_alarms(new_client);
  1136. device_create_file_pwm(new_client, 1);
  1137. device_create_file_pwm(new_client, 2);
  1138. device_create_file_pwm(new_client, 3);
  1139. device_create_file_pwmenable(new_client, 1);
  1140. device_create_file_pwmenable(new_client, 2);
  1141. device_create_file_pwmenable(new_client, 3);
  1142. device_create_file_pwm_mode(new_client, 1);
  1143. device_create_file_pwm_mode(new_client, 2);
  1144. device_create_file_pwm_mode(new_client, 3);
  1145. device_create_file_chassis(new_client);
  1146. device_create_file_chassis_clear(new_client);
  1147. device_create_file_thermal_cruise(new_client, 1);
  1148. device_create_file_thermal_cruise(new_client, 2);
  1149. device_create_file_thermal_cruise(new_client, 3);
  1150. device_create_file_tolerance(new_client, 1);
  1151. device_create_file_tolerance(new_client, 2);
  1152. device_create_file_tolerance(new_client, 3);
  1153. device_create_file_sf2_point(new_client, 1, 1); /* Fan1 */
  1154. device_create_file_sf2_point(new_client, 2, 1); /* Fan1 */
  1155. device_create_file_sf2_point(new_client, 3, 1); /* Fan1 */
  1156. device_create_file_sf2_point(new_client, 4, 1); /* Fan1 */
  1157. device_create_file_sf2_point(new_client, 1, 2); /* Fan2 */
  1158. device_create_file_sf2_point(new_client, 2, 2); /* Fan2 */
  1159. device_create_file_sf2_point(new_client, 3, 2); /* Fan2 */
  1160. device_create_file_sf2_point(new_client, 4, 2); /* Fan2 */
  1161. device_create_file_sf2_point(new_client, 1, 3); /* Fan3 */
  1162. device_create_file_sf2_point(new_client, 2, 3); /* Fan3 */
  1163. device_create_file_sf2_point(new_client, 3, 3); /* Fan3 */
  1164. device_create_file_sf2_point(new_client, 4, 3); /* Fan3 */
  1165. device_create_file_sf2_level(new_client, 1, 1); /* Fan1 */
  1166. device_create_file_sf2_level(new_client, 2, 1); /* Fan1 */
  1167. device_create_file_sf2_level(new_client, 3, 1); /* Fan1 */
  1168. device_create_file_sf2_level(new_client, 1, 2); /* Fan2 */
  1169. device_create_file_sf2_level(new_client, 2, 2); /* Fan2 */
  1170. device_create_file_sf2_level(new_client, 3, 2); /* Fan2 */
  1171. device_create_file_sf2_level(new_client, 1, 3); /* Fan3 */
  1172. device_create_file_sf2_level(new_client, 2, 3); /* Fan3 */
  1173. device_create_file_sf2_level(new_client, 3, 3); /* Fan3 */
  1174. return 0;
  1175. ERROR3:
  1176. if (data->lm75[0] != NULL) {
  1177. i2c_detach_client(data->lm75[0]);
  1178. kfree(data->lm75[0]);
  1179. }
  1180. if (data->lm75[1] != NULL) {
  1181. i2c_detach_client(data->lm75[1]);
  1182. kfree(data->lm75[1]);
  1183. }
  1184. ERROR2:
  1185. i2c_detach_client(new_client);
  1186. ERROR1:
  1187. kfree(data);
  1188. ERROR0:
  1189. return err;
  1190. }
  1191. static int
  1192. w83792d_detach_client(struct i2c_client *client)
  1193. {
  1194. struct w83792d_data *data = i2c_get_clientdata(client);
  1195. int err;
  1196. /* main client */
  1197. if (data)
  1198. hwmon_device_unregister(data->class_dev);
  1199. if ((err = i2c_detach_client(client)))
  1200. return err;
  1201. /* main client */
  1202. if (data)
  1203. kfree(data);
  1204. /* subclient */
  1205. else
  1206. kfree(client);
  1207. return 0;
  1208. }
  1209. /* The SMBus locks itself, usually, but nothing may access the Winbond between
  1210. bank switches. ISA access must always be locked explicitly!
  1211. We ignore the W83792D BUSY flag at this moment - it could lead to deadlocks,
  1212. would slow down the W83792D access and should not be necessary.
  1213. There are some ugly typecasts here, but the good news is - they should
  1214. nowhere else be necessary! */
  1215. static int
  1216. w83792d_read_value(struct i2c_client *client, u8 reg)
  1217. {
  1218. int res=0;
  1219. res = i2c_smbus_read_byte_data(client, reg);
  1220. return res;
  1221. }
  1222. static int
  1223. w83792d_write_value(struct i2c_client *client, u8 reg, u8 value)
  1224. {
  1225. i2c_smbus_write_byte_data(client, reg, value);
  1226. return 0;
  1227. }
  1228. /* Called when we have found a new W83792D. It should set limits, etc. */
  1229. static void
  1230. w83792d_init_client(struct i2c_client *client)
  1231. {
  1232. u8 temp2_cfg, temp3_cfg, vid_in_b;
  1233. if (init) {
  1234. w83792d_write_value(client, W83792D_REG_CONFIG, 0x80);
  1235. }
  1236. /* Clear the bit6 of W83792D_REG_VID_IN_B(set it into 0):
  1237. W83792D_REG_VID_IN_B bit6 = 0: the high/low limit of
  1238. vin0/vin1 can be modified by user;
  1239. W83792D_REG_VID_IN_B bit6 = 1: the high/low limit of
  1240. vin0/vin1 auto-updated, can NOT be modified by user. */
  1241. vid_in_b = w83792d_read_value(client, W83792D_REG_VID_IN_B);
  1242. w83792d_write_value(client, W83792D_REG_VID_IN_B,
  1243. vid_in_b & 0xbf);
  1244. temp2_cfg = w83792d_read_value(client, W83792D_REG_TEMP2_CONFIG);
  1245. temp3_cfg = w83792d_read_value(client, W83792D_REG_TEMP3_CONFIG);
  1246. w83792d_write_value(client, W83792D_REG_TEMP2_CONFIG,
  1247. temp2_cfg & 0xe6);
  1248. w83792d_write_value(client, W83792D_REG_TEMP3_CONFIG,
  1249. temp3_cfg & 0xe6);
  1250. /* Start monitoring */
  1251. w83792d_write_value(client, W83792D_REG_CONFIG,
  1252. (w83792d_read_value(client,
  1253. W83792D_REG_CONFIG) & 0xf7)
  1254. | 0x01);
  1255. }
  1256. static struct w83792d_data *w83792d_update_device(struct device *dev)
  1257. {
  1258. struct i2c_client *client = to_i2c_client(dev);
  1259. struct w83792d_data *data = i2c_get_clientdata(client);
  1260. int i, j;
  1261. u8 reg_array_tmp[4], pwm_array_tmp[7], reg_tmp;
  1262. down(&data->update_lock);
  1263. if (time_after
  1264. (jiffies - data->last_updated, (unsigned long) (HZ * 3))
  1265. || time_before(jiffies, data->last_updated) || !data->valid) {
  1266. dev_dbg(dev, "Starting device update\n");
  1267. /* Update the voltages measured value and limits */
  1268. for (i = 0; i < 9; i++) {
  1269. data->in[i] = w83792d_read_value(client,
  1270. W83792D_REG_IN[i]);
  1271. data->in_max[i] = w83792d_read_value(client,
  1272. W83792D_REG_IN_MAX[i]);
  1273. data->in_min[i] = w83792d_read_value(client,
  1274. W83792D_REG_IN_MIN[i]);
  1275. }
  1276. data->low_bits[0] = w83792d_read_value(client,
  1277. W83792D_REG_LOW_BITS1);
  1278. data->low_bits[1] = w83792d_read_value(client,
  1279. W83792D_REG_LOW_BITS2);
  1280. for (i = 0; i < 7; i++) {
  1281. /* Update the Fan measured value and limits */
  1282. data->fan[i] = w83792d_read_value(client,
  1283. W83792D_REG_FAN[i]);
  1284. data->fan_min[i] = w83792d_read_value(client,
  1285. W83792D_REG_FAN_MIN[i]);
  1286. /* Update the PWM/DC Value and PWM/DC flag */
  1287. pwm_array_tmp[i] = w83792d_read_value(client,
  1288. W83792D_REG_PWM[i]);
  1289. data->pwm[i] = pwm_array_tmp[i] & 0x0f;
  1290. data->pwm_mode[i] = (pwm_array_tmp[i] >> 7) & 0x01;
  1291. }
  1292. reg_tmp = w83792d_read_value(client, W83792D_REG_FAN_CFG);
  1293. data->pwmenable[0] = reg_tmp & 0x03;
  1294. data->pwmenable[1] = (reg_tmp>>2) & 0x03;
  1295. data->pwmenable[2] = (reg_tmp>>4) & 0x03;
  1296. for (i = 0; i < 3; i++) {
  1297. data->temp1[i] = w83792d_read_value(client,
  1298. W83792D_REG_TEMP1[i]);
  1299. }
  1300. for (i = 0; i < 2; i++) {
  1301. for (j = 0; j < 6; j++) {
  1302. data->temp_add[i][j] = w83792d_read_value(
  1303. client,W83792D_REG_TEMP_ADD[i][j]);
  1304. }
  1305. }
  1306. /* Update the Fan Divisor */
  1307. for (i = 0; i < 4; i++) {
  1308. reg_array_tmp[i] = w83792d_read_value(client,
  1309. W83792D_REG_FAN_DIV[i]);
  1310. }
  1311. data->fan_div[0] = reg_array_tmp[0] & 0x07;
  1312. data->fan_div[1] = (reg_array_tmp[0] >> 4) & 0x07;
  1313. data->fan_div[2] = reg_array_tmp[1] & 0x07;
  1314. data->fan_div[3] = (reg_array_tmp[1] >> 4) & 0x07;
  1315. data->fan_div[4] = reg_array_tmp[2] & 0x07;
  1316. data->fan_div[5] = (reg_array_tmp[2] >> 4) & 0x07;
  1317. data->fan_div[6] = reg_array_tmp[3] & 0x07;
  1318. /* Update the realtime status */
  1319. data->alarms = w83792d_read_value(client, W83792D_REG_ALARM1) +
  1320. (w83792d_read_value(client, W83792D_REG_ALARM2) << 8) +
  1321. (w83792d_read_value(client, W83792D_REG_ALARM3) << 16);
  1322. /* Update CaseOpen status and it's CLR_CHS. */
  1323. data->chassis = (w83792d_read_value(client,
  1324. W83792D_REG_CHASSIS) >> 5) & 0x01;
  1325. data->chassis_clear = (w83792d_read_value(client,
  1326. W83792D_REG_CHASSIS_CLR) >> 7) & 0x01;
  1327. /* Update Thermal Cruise/Smart Fan I target value */
  1328. for (i = 0; i < 3; i++) {
  1329. data->thermal_cruise[i] =
  1330. w83792d_read_value(client,
  1331. W83792D_REG_THERMAL[i]) & 0x7f;
  1332. }
  1333. /* Update Smart Fan I/II tolerance */
  1334. reg_tmp = w83792d_read_value(client, W83792D_REG_TOLERANCE[0]);
  1335. data->tolerance[0] = reg_tmp & 0x0f;
  1336. data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
  1337. data->tolerance[2] = w83792d_read_value(client,
  1338. W83792D_REG_TOLERANCE[2]) & 0x0f;
  1339. /* Update Smart Fan II temperature points */
  1340. for (i = 0; i < 3; i++) {
  1341. for (j = 0; j < 4; j++) {
  1342. data->sf2_points[i][j] = w83792d_read_value(
  1343. client,W83792D_REG_POINTS[i][j]) & 0x7f;
  1344. }
  1345. }
  1346. /* Update Smart Fan II duty cycle levels */
  1347. for (i = 0; i < 3; i++) {
  1348. reg_tmp = w83792d_read_value(client,
  1349. W83792D_REG_LEVELS[i][0]);
  1350. data->sf2_levels[i][0] = reg_tmp & 0x0f;
  1351. data->sf2_levels[i][1] = (reg_tmp >> 4) & 0x0f;
  1352. reg_tmp = w83792d_read_value(client,
  1353. W83792D_REG_LEVELS[i][2]);
  1354. data->sf2_levels[i][2] = (reg_tmp >> 4) & 0x0f;
  1355. data->sf2_levels[i][3] = reg_tmp & 0x0f;
  1356. }
  1357. data->last_updated = jiffies;
  1358. data->valid = 1;
  1359. }
  1360. up(&data->update_lock);
  1361. #ifdef DEBUG
  1362. w83792d_print_debug(data, dev);
  1363. #endif
  1364. return data;
  1365. }
  1366. #ifdef DEBUG
  1367. static void w83792d_print_debug(struct w83792d_data *data, struct device *dev)
  1368. {
  1369. int i=0, j=0;
  1370. dev_dbg(dev, "==========The following is the debug message...========\n");
  1371. dev_dbg(dev, "9 set of Voltages: =====>\n");
  1372. for (i=0; i<9; i++) {
  1373. dev_dbg(dev, "vin[%d] is: 0x%x\n", i, data->in[i]);
  1374. dev_dbg(dev, "vin[%d] max is: 0x%x\n", i, data->in_max[i]);
  1375. dev_dbg(dev, "vin[%d] min is: 0x%x\n", i, data->in_min[i]);
  1376. }
  1377. dev_dbg(dev, "Low Bit1 is: 0x%x\n", data->low_bits[0]);
  1378. dev_dbg(dev, "Low Bit2 is: 0x%x\n", data->low_bits[1]);
  1379. dev_dbg(dev, "7 set of Fan Counts and Duty Cycles: =====>\n");
  1380. for (i=0; i<7; i++) {
  1381. dev_dbg(dev, "fan[%d] is: 0x%x\n", i, data->fan[i]);
  1382. dev_dbg(dev, "fan[%d] min is: 0x%x\n", i, data->fan_min[i]);
  1383. dev_dbg(dev, "pwm[%d] is: 0x%x\n", i, data->pwm[i]);
  1384. dev_dbg(dev, "pwm_mode[%d] is: 0x%x\n", i, data->pwm_mode[i]);
  1385. }
  1386. dev_dbg(dev, "3 set of Temperatures: =====>\n");
  1387. for (i=0; i<3; i++) {
  1388. dev_dbg(dev, "temp1[%d] is: 0x%x\n", i, data->temp1[i]);
  1389. }
  1390. for (i=0; i<2; i++) {
  1391. for (j=0; j<6; j++) {
  1392. dev_dbg(dev, "temp_add[%d][%d] is: 0x%x\n", i, j,
  1393. data->temp_add[i][j]);
  1394. }
  1395. }
  1396. for (i=0; i<7; i++) {
  1397. dev_dbg(dev, "fan_div[%d] is: 0x%x\n", i, data->fan_div[i]);
  1398. }
  1399. dev_dbg(dev, "==========End of the debug message...==================\n");
  1400. dev_dbg(dev, "\n");
  1401. }
  1402. #endif
  1403. static int __init
  1404. sensors_w83792d_init(void)
  1405. {
  1406. return i2c_add_driver(&w83792d_driver);
  1407. }
  1408. static void __exit
  1409. sensors_w83792d_exit(void)
  1410. {
  1411. i2c_del_driver(&w83792d_driver);
  1412. }
  1413. MODULE_AUTHOR("Chunhao Huang @ Winbond <DZShen@Winbond.com.tw>");
  1414. MODULE_DESCRIPTION("W83792AD/D driver for linux-2.6");
  1415. MODULE_LICENSE("GPL");
  1416. module_init(sensors_w83792d_init);
  1417. module_exit(sensors_w83792d_exit);