w83792d.c 50 KB

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