w83791d.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  1. /*
  2. * w83791d.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. *
  5. * Copyright (C) 2006-2007 Charles Spirakis <bezaur@gmail.com>
  6. *
  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. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Supports following chips:
  23. *
  24. * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  25. * w83791d 10 5 5 3 0x71 0x5ca3 yes no
  26. *
  27. * The w83791d chip appears to be part way between the 83781d and the
  28. * 83792d. Thus, this file is derived from both the w83792d.c and
  29. * w83781d.c files.
  30. *
  31. * The w83791g chip is the same as the w83791d but lead-free.
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/i2c.h>
  37. #include <linux/hwmon.h>
  38. #include <linux/hwmon-vid.h>
  39. #include <linux/hwmon-sysfs.h>
  40. #include <linux/err.h>
  41. #include <linux/mutex.h>
  42. #define NUMBER_OF_VIN 10
  43. #define NUMBER_OF_FANIN 5
  44. #define NUMBER_OF_TEMPIN 3
  45. #define NUMBER_OF_PWM 5
  46. /* Addresses to scan */
  47. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  48. I2C_CLIENT_END };
  49. /* Insmod parameters */
  50. static unsigned short force_subclients[4];
  51. module_param_array(force_subclients, short, NULL, 0);
  52. MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
  53. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  54. static bool reset;
  55. module_param(reset, bool, 0);
  56. MODULE_PARM_DESC(reset, "Set to one to force a hardware chip reset");
  57. static bool init;
  58. module_param(init, bool, 0);
  59. MODULE_PARM_DESC(init, "Set to one to force extra software initialization");
  60. /* The W83791D registers */
  61. static const u8 W83791D_REG_IN[NUMBER_OF_VIN] = {
  62. 0x20, /* VCOREA in DataSheet */
  63. 0x21, /* VINR0 in DataSheet */
  64. 0x22, /* +3.3VIN in DataSheet */
  65. 0x23, /* VDD5V in DataSheet */
  66. 0x24, /* +12VIN in DataSheet */
  67. 0x25, /* -12VIN in DataSheet */
  68. 0x26, /* -5VIN in DataSheet */
  69. 0xB0, /* 5VSB in DataSheet */
  70. 0xB1, /* VBAT in DataSheet */
  71. 0xB2 /* VINR1 in DataSheet */
  72. };
  73. static const u8 W83791D_REG_IN_MAX[NUMBER_OF_VIN] = {
  74. 0x2B, /* VCOREA High Limit in DataSheet */
  75. 0x2D, /* VINR0 High Limit in DataSheet */
  76. 0x2F, /* +3.3VIN High Limit in DataSheet */
  77. 0x31, /* VDD5V High Limit in DataSheet */
  78. 0x33, /* +12VIN High Limit in DataSheet */
  79. 0x35, /* -12VIN High Limit in DataSheet */
  80. 0x37, /* -5VIN High Limit in DataSheet */
  81. 0xB4, /* 5VSB High Limit in DataSheet */
  82. 0xB6, /* VBAT High Limit in DataSheet */
  83. 0xB8 /* VINR1 High Limit in DataSheet */
  84. };
  85. static const u8 W83791D_REG_IN_MIN[NUMBER_OF_VIN] = {
  86. 0x2C, /* VCOREA Low Limit in DataSheet */
  87. 0x2E, /* VINR0 Low Limit in DataSheet */
  88. 0x30, /* +3.3VIN Low Limit in DataSheet */
  89. 0x32, /* VDD5V Low Limit in DataSheet */
  90. 0x34, /* +12VIN Low Limit in DataSheet */
  91. 0x36, /* -12VIN Low Limit in DataSheet */
  92. 0x38, /* -5VIN Low Limit in DataSheet */
  93. 0xB5, /* 5VSB Low Limit in DataSheet */
  94. 0xB7, /* VBAT Low Limit in DataSheet */
  95. 0xB9 /* VINR1 Low Limit in DataSheet */
  96. };
  97. static const u8 W83791D_REG_FAN[NUMBER_OF_FANIN] = {
  98. 0x28, /* FAN 1 Count in DataSheet */
  99. 0x29, /* FAN 2 Count in DataSheet */
  100. 0x2A, /* FAN 3 Count in DataSheet */
  101. 0xBA, /* FAN 4 Count in DataSheet */
  102. 0xBB, /* FAN 5 Count in DataSheet */
  103. };
  104. static const u8 W83791D_REG_FAN_MIN[NUMBER_OF_FANIN] = {
  105. 0x3B, /* FAN 1 Count Low Limit in DataSheet */
  106. 0x3C, /* FAN 2 Count Low Limit in DataSheet */
  107. 0x3D, /* FAN 3 Count Low Limit in DataSheet */
  108. 0xBC, /* FAN 4 Count Low Limit in DataSheet */
  109. 0xBD, /* FAN 5 Count Low Limit in DataSheet */
  110. };
  111. static const u8 W83791D_REG_PWM[NUMBER_OF_PWM] = {
  112. 0x81, /* PWM 1 duty cycle register in DataSheet */
  113. 0x83, /* PWM 2 duty cycle register in DataSheet */
  114. 0x94, /* PWM 3 duty cycle register in DataSheet */
  115. 0xA0, /* PWM 4 duty cycle register in DataSheet */
  116. 0xA1, /* PWM 5 duty cycle register in DataSheet */
  117. };
  118. static const u8 W83791D_REG_TEMP_TARGET[3] = {
  119. 0x85, /* PWM 1 target temperature for temp 1 */
  120. 0x86, /* PWM 2 target temperature for temp 2 */
  121. 0x96, /* PWM 3 target temperature for temp 3 */
  122. };
  123. static const u8 W83791D_REG_TEMP_TOL[2] = {
  124. 0x87, /* PWM 1/2 temperature tolerance */
  125. 0x97, /* PWM 3 temperature tolerance */
  126. };
  127. static const u8 W83791D_REG_FAN_CFG[2] = {
  128. 0x84, /* FAN 1/2 configuration */
  129. 0x95, /* FAN 3 configuration */
  130. };
  131. static const u8 W83791D_REG_FAN_DIV[3] = {
  132. 0x47, /* contains FAN1 and FAN2 Divisor */
  133. 0x4b, /* contains FAN3 Divisor */
  134. 0x5C, /* contains FAN4 and FAN5 Divisor */
  135. };
  136. #define W83791D_REG_BANK 0x4E
  137. #define W83791D_REG_TEMP2_CONFIG 0xC2
  138. #define W83791D_REG_TEMP3_CONFIG 0xCA
  139. static const u8 W83791D_REG_TEMP1[3] = {
  140. 0x27, /* TEMP 1 in DataSheet */
  141. 0x39, /* TEMP 1 Over in DataSheet */
  142. 0x3A, /* TEMP 1 Hyst in DataSheet */
  143. };
  144. static const u8 W83791D_REG_TEMP_ADD[2][6] = {
  145. {0xC0, /* TEMP 2 in DataSheet */
  146. 0xC1, /* TEMP 2(0.5 deg) in DataSheet */
  147. 0xC5, /* TEMP 2 Over High part in DataSheet */
  148. 0xC6, /* TEMP 2 Over Low part in DataSheet */
  149. 0xC3, /* TEMP 2 Thyst High part in DataSheet */
  150. 0xC4}, /* TEMP 2 Thyst Low part in DataSheet */
  151. {0xC8, /* TEMP 3 in DataSheet */
  152. 0xC9, /* TEMP 3(0.5 deg) in DataSheet */
  153. 0xCD, /* TEMP 3 Over High part in DataSheet */
  154. 0xCE, /* TEMP 3 Over Low part in DataSheet */
  155. 0xCB, /* TEMP 3 Thyst High part in DataSheet */
  156. 0xCC} /* TEMP 3 Thyst Low part in DataSheet */
  157. };
  158. #define W83791D_REG_BEEP_CONFIG 0x4D
  159. static const u8 W83791D_REG_BEEP_CTRL[3] = {
  160. 0x56, /* BEEP Control Register 1 */
  161. 0x57, /* BEEP Control Register 2 */
  162. 0xA3, /* BEEP Control Register 3 */
  163. };
  164. #define W83791D_REG_GPIO 0x15
  165. #define W83791D_REG_CONFIG 0x40
  166. #define W83791D_REG_VID_FANDIV 0x47
  167. #define W83791D_REG_DID_VID4 0x49
  168. #define W83791D_REG_WCHIPID 0x58
  169. #define W83791D_REG_CHIPMAN 0x4F
  170. #define W83791D_REG_PIN 0x4B
  171. #define W83791D_REG_I2C_SUBADDR 0x4A
  172. #define W83791D_REG_ALARM1 0xA9 /* realtime status register1 */
  173. #define W83791D_REG_ALARM2 0xAA /* realtime status register2 */
  174. #define W83791D_REG_ALARM3 0xAB /* realtime status register3 */
  175. #define W83791D_REG_VBAT 0x5D
  176. #define W83791D_REG_I2C_ADDR 0x48
  177. /*
  178. * The SMBus locks itself. The Winbond W83791D has a bank select register
  179. * (index 0x4e), but the driver only accesses registers in bank 0. Since
  180. * we don't switch banks, we don't need any special code to handle
  181. * locking access between bank switches
  182. */
  183. static inline int w83791d_read(struct i2c_client *client, u8 reg)
  184. {
  185. return i2c_smbus_read_byte_data(client, reg);
  186. }
  187. static inline int w83791d_write(struct i2c_client *client, u8 reg, u8 value)
  188. {
  189. return i2c_smbus_write_byte_data(client, reg, value);
  190. }
  191. /*
  192. * The analog voltage inputs have 16mV LSB. Since the sysfs output is
  193. * in mV as would be measured on the chip input pin, need to just
  194. * multiply/divide by 16 to translate from/to register values.
  195. */
  196. #define IN_TO_REG(val) (SENSORS_LIMIT((((val) + 8) / 16), 0, 255))
  197. #define IN_FROM_REG(val) ((val) * 16)
  198. static u8 fan_to_reg(long rpm, int div)
  199. {
  200. if (rpm == 0)
  201. return 255;
  202. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  203. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  204. }
  205. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
  206. ((val) == 255 ? 0 : \
  207. 1350000 / ((val) * (div))))
  208. /* for temp1 which is 8-bit resolution, LSB = 1 degree Celsius */
  209. #define TEMP1_FROM_REG(val) ((val) * 1000)
  210. #define TEMP1_TO_REG(val) ((val) <= -128000 ? -128 : \
  211. (val) >= 127000 ? 127 : \
  212. (val) < 0 ? ((val) - 500) / 1000 : \
  213. ((val) + 500) / 1000)
  214. /*
  215. * for temp2 and temp3 which are 9-bit resolution, LSB = 0.5 degree Celsius
  216. * Assumes the top 8 bits are the integral amount and the bottom 8 bits
  217. * are the fractional amount. Since we only have 0.5 degree resolution,
  218. * the bottom 7 bits will always be zero
  219. */
  220. #define TEMP23_FROM_REG(val) ((val) / 128 * 500)
  221. #define TEMP23_TO_REG(val) ((val) <= -128000 ? 0x8000 : \
  222. (val) >= 127500 ? 0x7F80 : \
  223. (val) < 0 ? ((val) - 250) / 500 * 128 : \
  224. ((val) + 250) / 500 * 128)
  225. /* for thermal cruise target temp, 7-bits, LSB = 1 degree Celsius */
  226. #define TARGET_TEMP_TO_REG(val) ((val) < 0 ? 0 : \
  227. (val) >= 127000 ? 127 : \
  228. ((val) + 500) / 1000)
  229. /* for thermal cruise temp tolerance, 4-bits, LSB = 1 degree Celsius */
  230. #define TOL_TEMP_TO_REG(val) ((val) >= 15000 ? 15 : \
  231. ((val) + 500) / 1000)
  232. #define BEEP_MASK_TO_REG(val) ((val) & 0xffffff)
  233. #define BEEP_MASK_FROM_REG(val) ((val) & 0xffffff)
  234. #define DIV_FROM_REG(val) (1 << (val))
  235. static u8 div_to_reg(int nr, long val)
  236. {
  237. int i;
  238. /* fan divisors max out at 128 */
  239. val = SENSORS_LIMIT(val, 1, 128) >> 1;
  240. for (i = 0; i < 7; i++) {
  241. if (val == 0)
  242. break;
  243. val >>= 1;
  244. }
  245. return (u8) i;
  246. }
  247. struct w83791d_data {
  248. struct device *hwmon_dev;
  249. struct mutex update_lock;
  250. char valid; /* !=0 if following fields are valid */
  251. unsigned long last_updated; /* In jiffies */
  252. /* array of 2 pointers to subclients */
  253. struct i2c_client *lm75[2];
  254. /* volts */
  255. u8 in[NUMBER_OF_VIN]; /* Register value */
  256. u8 in_max[NUMBER_OF_VIN]; /* Register value */
  257. u8 in_min[NUMBER_OF_VIN]; /* Register value */
  258. /* fans */
  259. u8 fan[NUMBER_OF_FANIN]; /* Register value */
  260. u8 fan_min[NUMBER_OF_FANIN]; /* Register value */
  261. u8 fan_div[NUMBER_OF_FANIN]; /* Register encoding, shifted right */
  262. /* Temperature sensors */
  263. s8 temp1[3]; /* current, over, thyst */
  264. s16 temp_add[2][3]; /* fixed point value. Top 8 bits are the
  265. * integral part, bottom 8 bits are the
  266. * fractional part. We only use the top
  267. * 9 bits as the resolution is only
  268. * to the 0.5 degree C...
  269. * two sensors with three values
  270. * (cur, over, hyst)
  271. */
  272. /* PWMs */
  273. u8 pwm[5]; /* pwm duty cycle */
  274. u8 pwm_enable[3]; /* pwm enable status for fan 1-3
  275. * (fan 4-5 only support manual mode)
  276. */
  277. u8 temp_target[3]; /* pwm 1-3 target temperature */
  278. u8 temp_tolerance[3]; /* pwm 1-3 temperature tolerance */
  279. /* Misc */
  280. u32 alarms; /* realtime status register encoding,combined */
  281. u8 beep_enable; /* Global beep enable */
  282. u32 beep_mask; /* Mask off specific beeps */
  283. u8 vid; /* Register encoding, combined */
  284. u8 vrm; /* hwmon-vid */
  285. };
  286. static int w83791d_probe(struct i2c_client *client,
  287. const struct i2c_device_id *id);
  288. static int w83791d_detect(struct i2c_client *client,
  289. struct i2c_board_info *info);
  290. static int w83791d_remove(struct i2c_client *client);
  291. static int w83791d_read(struct i2c_client *client, u8 reg);
  292. static int w83791d_write(struct i2c_client *client, u8 reg, u8 value);
  293. static struct w83791d_data *w83791d_update_device(struct device *dev);
  294. #ifdef DEBUG
  295. static void w83791d_print_debug(struct w83791d_data *data, struct device *dev);
  296. #endif
  297. static void w83791d_init_client(struct i2c_client *client);
  298. static const struct i2c_device_id w83791d_id[] = {
  299. { "w83791d", 0 },
  300. { }
  301. };
  302. MODULE_DEVICE_TABLE(i2c, w83791d_id);
  303. static struct i2c_driver w83791d_driver = {
  304. .class = I2C_CLASS_HWMON,
  305. .driver = {
  306. .name = "w83791d",
  307. },
  308. .probe = w83791d_probe,
  309. .remove = w83791d_remove,
  310. .id_table = w83791d_id,
  311. .detect = w83791d_detect,
  312. .address_list = normal_i2c,
  313. };
  314. /* following are the sysfs callback functions */
  315. #define show_in_reg(reg) \
  316. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  317. char *buf) \
  318. { \
  319. struct sensor_device_attribute *sensor_attr = \
  320. to_sensor_dev_attr(attr); \
  321. struct w83791d_data *data = w83791d_update_device(dev); \
  322. int nr = sensor_attr->index; \
  323. return sprintf(buf, "%d\n", IN_FROM_REG(data->reg[nr])); \
  324. }
  325. show_in_reg(in);
  326. show_in_reg(in_min);
  327. show_in_reg(in_max);
  328. #define store_in_reg(REG, reg) \
  329. static ssize_t store_in_##reg(struct device *dev, \
  330. struct device_attribute *attr, \
  331. const char *buf, size_t count) \
  332. { \
  333. struct sensor_device_attribute *sensor_attr = \
  334. to_sensor_dev_attr(attr); \
  335. struct i2c_client *client = to_i2c_client(dev); \
  336. struct w83791d_data *data = i2c_get_clientdata(client); \
  337. int nr = sensor_attr->index; \
  338. unsigned long val; \
  339. int err = kstrtoul(buf, 10, &val); \
  340. if (err) \
  341. return err; \
  342. mutex_lock(&data->update_lock); \
  343. data->in_##reg[nr] = IN_TO_REG(val); \
  344. w83791d_write(client, W83791D_REG_IN_##REG[nr], data->in_##reg[nr]); \
  345. mutex_unlock(&data->update_lock); \
  346. \
  347. return count; \
  348. }
  349. store_in_reg(MIN, min);
  350. store_in_reg(MAX, max);
  351. static struct sensor_device_attribute sda_in_input[] = {
  352. SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
  353. SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
  354. SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
  355. SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
  356. SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
  357. SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
  358. SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
  359. SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
  360. SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
  361. SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
  362. };
  363. static struct sensor_device_attribute sda_in_min[] = {
  364. SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
  365. SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
  366. SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
  367. SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
  368. SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
  369. SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
  370. SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
  371. SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
  372. SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
  373. SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
  374. };
  375. static struct sensor_device_attribute sda_in_max[] = {
  376. SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
  377. SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
  378. SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
  379. SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
  380. SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
  381. SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
  382. SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
  383. SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
  384. SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
  385. SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
  386. };
  387. static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
  388. char *buf)
  389. {
  390. struct sensor_device_attribute *sensor_attr =
  391. to_sensor_dev_attr(attr);
  392. struct w83791d_data *data = w83791d_update_device(dev);
  393. int bitnr = sensor_attr->index;
  394. return sprintf(buf, "%d\n", (data->beep_mask >> bitnr) & 1);
  395. }
  396. static ssize_t store_beep(struct device *dev, struct device_attribute *attr,
  397. const char *buf, size_t count)
  398. {
  399. struct sensor_device_attribute *sensor_attr =
  400. to_sensor_dev_attr(attr);
  401. struct i2c_client *client = to_i2c_client(dev);
  402. struct w83791d_data *data = i2c_get_clientdata(client);
  403. int bitnr = sensor_attr->index;
  404. int bytenr = bitnr / 8;
  405. unsigned long val;
  406. int err;
  407. err = kstrtoul(buf, 10, &val);
  408. if (err)
  409. return err;
  410. val = val ? 1 : 0;
  411. mutex_lock(&data->update_lock);
  412. data->beep_mask &= ~(0xff << (bytenr * 8));
  413. data->beep_mask |= w83791d_read(client, W83791D_REG_BEEP_CTRL[bytenr])
  414. << (bytenr * 8);
  415. data->beep_mask &= ~(1 << bitnr);
  416. data->beep_mask |= val << bitnr;
  417. w83791d_write(client, W83791D_REG_BEEP_CTRL[bytenr],
  418. (data->beep_mask >> (bytenr * 8)) & 0xff);
  419. mutex_unlock(&data->update_lock);
  420. return count;
  421. }
  422. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  423. char *buf)
  424. {
  425. struct sensor_device_attribute *sensor_attr =
  426. to_sensor_dev_attr(attr);
  427. struct w83791d_data *data = w83791d_update_device(dev);
  428. int bitnr = sensor_attr->index;
  429. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  430. }
  431. /*
  432. * Note: The bitmask for the beep enable/disable is different than
  433. * the bitmask for the alarm.
  434. */
  435. static struct sensor_device_attribute sda_in_beep[] = {
  436. SENSOR_ATTR(in0_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 0),
  437. SENSOR_ATTR(in1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 13),
  438. SENSOR_ATTR(in2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 2),
  439. SENSOR_ATTR(in3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 3),
  440. SENSOR_ATTR(in4_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 8),
  441. SENSOR_ATTR(in5_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 9),
  442. SENSOR_ATTR(in6_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 10),
  443. SENSOR_ATTR(in7_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 16),
  444. SENSOR_ATTR(in8_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 17),
  445. SENSOR_ATTR(in9_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 14),
  446. };
  447. static struct sensor_device_attribute sda_in_alarm[] = {
  448. SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
  449. SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
  450. SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
  451. SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
  452. SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
  453. SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9),
  454. SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 10),
  455. SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 19),
  456. SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 20),
  457. SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 14),
  458. };
  459. #define show_fan_reg(reg) \
  460. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  461. char *buf) \
  462. { \
  463. struct sensor_device_attribute *sensor_attr = \
  464. to_sensor_dev_attr(attr); \
  465. struct w83791d_data *data = w83791d_update_device(dev); \
  466. int nr = sensor_attr->index; \
  467. return sprintf(buf, "%d\n", \
  468. FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
  469. }
  470. show_fan_reg(fan);
  471. show_fan_reg(fan_min);
  472. static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
  473. const char *buf, size_t count)
  474. {
  475. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  476. struct i2c_client *client = to_i2c_client(dev);
  477. struct w83791d_data *data = i2c_get_clientdata(client);
  478. int nr = sensor_attr->index;
  479. unsigned long val;
  480. int err;
  481. err = kstrtoul(buf, 10, &val);
  482. if (err)
  483. return err;
  484. mutex_lock(&data->update_lock);
  485. data->fan_min[nr] = fan_to_reg(val, DIV_FROM_REG(data->fan_div[nr]));
  486. w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->fan_min[nr]);
  487. mutex_unlock(&data->update_lock);
  488. return count;
  489. }
  490. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  491. char *buf)
  492. {
  493. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  494. int nr = sensor_attr->index;
  495. struct w83791d_data *data = w83791d_update_device(dev);
  496. return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr]));
  497. }
  498. /*
  499. * Note: we save and restore the fan minimum here, because its value is
  500. * determined in part by the fan divisor. This follows the principle of
  501. * least surprise; the user doesn't expect the fan minimum to change just
  502. * because the divisor changed.
  503. */
  504. static ssize_t store_fan_div(struct device *dev, struct device_attribute *attr,
  505. const char *buf, size_t count)
  506. {
  507. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  508. struct i2c_client *client = to_i2c_client(dev);
  509. struct w83791d_data *data = i2c_get_clientdata(client);
  510. int nr = sensor_attr->index;
  511. unsigned long min;
  512. u8 tmp_fan_div;
  513. u8 fan_div_reg;
  514. u8 vbat_reg;
  515. int indx = 0;
  516. u8 keep_mask = 0;
  517. u8 new_shift = 0;
  518. unsigned long val;
  519. int err;
  520. err = kstrtoul(buf, 10, &val);
  521. if (err)
  522. return err;
  523. /* Save fan_min */
  524. min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
  525. mutex_lock(&data->update_lock);
  526. data->fan_div[nr] = div_to_reg(nr, val);
  527. switch (nr) {
  528. case 0:
  529. indx = 0;
  530. keep_mask = 0xcf;
  531. new_shift = 4;
  532. break;
  533. case 1:
  534. indx = 0;
  535. keep_mask = 0x3f;
  536. new_shift = 6;
  537. break;
  538. case 2:
  539. indx = 1;
  540. keep_mask = 0x3f;
  541. new_shift = 6;
  542. break;
  543. case 3:
  544. indx = 2;
  545. keep_mask = 0xf8;
  546. new_shift = 0;
  547. break;
  548. case 4:
  549. indx = 2;
  550. keep_mask = 0x8f;
  551. new_shift = 4;
  552. break;
  553. #ifdef DEBUG
  554. default:
  555. dev_warn(dev, "store_fan_div: Unexpected nr seen: %d\n", nr);
  556. count = -EINVAL;
  557. goto err_exit;
  558. #endif
  559. }
  560. fan_div_reg = w83791d_read(client, W83791D_REG_FAN_DIV[indx])
  561. & keep_mask;
  562. tmp_fan_div = (data->fan_div[nr] << new_shift) & ~keep_mask;
  563. w83791d_write(client, W83791D_REG_FAN_DIV[indx],
  564. fan_div_reg | tmp_fan_div);
  565. /* Bit 2 of fans 0-2 is stored in the vbat register (bits 5-7) */
  566. if (nr < 3) {
  567. keep_mask = ~(1 << (nr + 5));
  568. vbat_reg = w83791d_read(client, W83791D_REG_VBAT)
  569. & keep_mask;
  570. tmp_fan_div = (data->fan_div[nr] << (3 + nr)) & ~keep_mask;
  571. w83791d_write(client, W83791D_REG_VBAT,
  572. vbat_reg | tmp_fan_div);
  573. }
  574. /* Restore fan_min */
  575. data->fan_min[nr] = fan_to_reg(min, DIV_FROM_REG(data->fan_div[nr]));
  576. w83791d_write(client, W83791D_REG_FAN_MIN[nr], data->fan_min[nr]);
  577. #ifdef DEBUG
  578. err_exit:
  579. #endif
  580. mutex_unlock(&data->update_lock);
  581. return count;
  582. }
  583. static struct sensor_device_attribute sda_fan_input[] = {
  584. SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
  585. SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
  586. SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
  587. SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
  588. SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
  589. };
  590. static struct sensor_device_attribute sda_fan_min[] = {
  591. SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO,
  592. show_fan_min, store_fan_min, 0),
  593. SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO,
  594. show_fan_min, store_fan_min, 1),
  595. SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO,
  596. show_fan_min, store_fan_min, 2),
  597. SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO,
  598. show_fan_min, store_fan_min, 3),
  599. SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO,
  600. show_fan_min, store_fan_min, 4),
  601. };
  602. static struct sensor_device_attribute sda_fan_div[] = {
  603. SENSOR_ATTR(fan1_div, S_IWUSR | S_IRUGO,
  604. show_fan_div, store_fan_div, 0),
  605. SENSOR_ATTR(fan2_div, S_IWUSR | S_IRUGO,
  606. show_fan_div, store_fan_div, 1),
  607. SENSOR_ATTR(fan3_div, S_IWUSR | S_IRUGO,
  608. show_fan_div, store_fan_div, 2),
  609. SENSOR_ATTR(fan4_div, S_IWUSR | S_IRUGO,
  610. show_fan_div, store_fan_div, 3),
  611. SENSOR_ATTR(fan5_div, S_IWUSR | S_IRUGO,
  612. show_fan_div, store_fan_div, 4),
  613. };
  614. static struct sensor_device_attribute sda_fan_beep[] = {
  615. SENSOR_ATTR(fan1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 6),
  616. SENSOR_ATTR(fan2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 7),
  617. SENSOR_ATTR(fan3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 11),
  618. SENSOR_ATTR(fan4_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 21),
  619. SENSOR_ATTR(fan5_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 22),
  620. };
  621. static struct sensor_device_attribute sda_fan_alarm[] = {
  622. SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
  623. SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
  624. SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
  625. SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 21),
  626. SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 22),
  627. };
  628. /* read/write PWMs */
  629. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  630. char *buf)
  631. {
  632. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  633. int nr = sensor_attr->index;
  634. struct w83791d_data *data = w83791d_update_device(dev);
  635. return sprintf(buf, "%u\n", data->pwm[nr]);
  636. }
  637. static ssize_t store_pwm(struct device *dev, struct device_attribute *attr,
  638. const char *buf, size_t count)
  639. {
  640. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  641. struct i2c_client *client = to_i2c_client(dev);
  642. struct w83791d_data *data = i2c_get_clientdata(client);
  643. int nr = sensor_attr->index;
  644. unsigned long val;
  645. if (kstrtoul(buf, 10, &val))
  646. return -EINVAL;
  647. mutex_lock(&data->update_lock);
  648. data->pwm[nr] = SENSORS_LIMIT(val, 0, 255);
  649. w83791d_write(client, W83791D_REG_PWM[nr], data->pwm[nr]);
  650. mutex_unlock(&data->update_lock);
  651. return count;
  652. }
  653. static struct sensor_device_attribute sda_pwm[] = {
  654. SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO,
  655. show_pwm, store_pwm, 0),
  656. SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO,
  657. show_pwm, store_pwm, 1),
  658. SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO,
  659. show_pwm, store_pwm, 2),
  660. SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO,
  661. show_pwm, store_pwm, 3),
  662. SENSOR_ATTR(pwm5, S_IWUSR | S_IRUGO,
  663. show_pwm, store_pwm, 4),
  664. };
  665. static ssize_t show_pwmenable(struct device *dev, struct device_attribute *attr,
  666. char *buf)
  667. {
  668. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  669. int nr = sensor_attr->index;
  670. struct w83791d_data *data = w83791d_update_device(dev);
  671. return sprintf(buf, "%u\n", data->pwm_enable[nr] + 1);
  672. }
  673. static ssize_t store_pwmenable(struct device *dev,
  674. struct device_attribute *attr, const char *buf, size_t count)
  675. {
  676. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  677. struct i2c_client *client = to_i2c_client(dev);
  678. struct w83791d_data *data = i2c_get_clientdata(client);
  679. int nr = sensor_attr->index;
  680. unsigned long val;
  681. u8 reg_cfg_tmp;
  682. u8 reg_idx = 0;
  683. u8 val_shift = 0;
  684. u8 keep_mask = 0;
  685. int ret = kstrtoul(buf, 10, &val);
  686. if (ret || val < 1 || val > 3)
  687. return -EINVAL;
  688. mutex_lock(&data->update_lock);
  689. data->pwm_enable[nr] = val - 1;
  690. switch (nr) {
  691. case 0:
  692. reg_idx = 0;
  693. val_shift = 2;
  694. keep_mask = 0xf3;
  695. break;
  696. case 1:
  697. reg_idx = 0;
  698. val_shift = 4;
  699. keep_mask = 0xcf;
  700. break;
  701. case 2:
  702. reg_idx = 1;
  703. val_shift = 2;
  704. keep_mask = 0xf3;
  705. break;
  706. }
  707. reg_cfg_tmp = w83791d_read(client, W83791D_REG_FAN_CFG[reg_idx]);
  708. reg_cfg_tmp = (reg_cfg_tmp & keep_mask) |
  709. data->pwm_enable[nr] << val_shift;
  710. w83791d_write(client, W83791D_REG_FAN_CFG[reg_idx], reg_cfg_tmp);
  711. mutex_unlock(&data->update_lock);
  712. return count;
  713. }
  714. static struct sensor_device_attribute sda_pwmenable[] = {
  715. SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  716. show_pwmenable, store_pwmenable, 0),
  717. SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
  718. show_pwmenable, store_pwmenable, 1),
  719. SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
  720. show_pwmenable, store_pwmenable, 2),
  721. };
  722. /* For Smart Fan I / Thermal Cruise */
  723. static ssize_t show_temp_target(struct device *dev,
  724. struct device_attribute *attr, char *buf)
  725. {
  726. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  727. struct w83791d_data *data = w83791d_update_device(dev);
  728. int nr = sensor_attr->index;
  729. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp_target[nr]));
  730. }
  731. static ssize_t store_temp_target(struct device *dev,
  732. struct device_attribute *attr, const char *buf, size_t count)
  733. {
  734. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  735. struct i2c_client *client = to_i2c_client(dev);
  736. struct w83791d_data *data = i2c_get_clientdata(client);
  737. int nr = sensor_attr->index;
  738. long val;
  739. u8 target_mask;
  740. if (kstrtol(buf, 10, &val))
  741. return -EINVAL;
  742. mutex_lock(&data->update_lock);
  743. data->temp_target[nr] = TARGET_TEMP_TO_REG(val);
  744. target_mask = w83791d_read(client,
  745. W83791D_REG_TEMP_TARGET[nr]) & 0x80;
  746. w83791d_write(client, W83791D_REG_TEMP_TARGET[nr],
  747. data->temp_target[nr] | target_mask);
  748. mutex_unlock(&data->update_lock);
  749. return count;
  750. }
  751. static struct sensor_device_attribute sda_temp_target[] = {
  752. SENSOR_ATTR(temp1_target, S_IWUSR | S_IRUGO,
  753. show_temp_target, store_temp_target, 0),
  754. SENSOR_ATTR(temp2_target, S_IWUSR | S_IRUGO,
  755. show_temp_target, store_temp_target, 1),
  756. SENSOR_ATTR(temp3_target, S_IWUSR | S_IRUGO,
  757. show_temp_target, store_temp_target, 2),
  758. };
  759. static ssize_t show_temp_tolerance(struct device *dev,
  760. struct device_attribute *attr, char *buf)
  761. {
  762. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  763. struct w83791d_data *data = w83791d_update_device(dev);
  764. int nr = sensor_attr->index;
  765. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp_tolerance[nr]));
  766. }
  767. static ssize_t store_temp_tolerance(struct device *dev,
  768. struct device_attribute *attr, const char *buf, size_t count)
  769. {
  770. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  771. struct i2c_client *client = to_i2c_client(dev);
  772. struct w83791d_data *data = i2c_get_clientdata(client);
  773. int nr = sensor_attr->index;
  774. unsigned long val;
  775. u8 target_mask;
  776. u8 reg_idx = 0;
  777. u8 val_shift = 0;
  778. u8 keep_mask = 0;
  779. if (kstrtoul(buf, 10, &val))
  780. return -EINVAL;
  781. switch (nr) {
  782. case 0:
  783. reg_idx = 0;
  784. val_shift = 0;
  785. keep_mask = 0xf0;
  786. break;
  787. case 1:
  788. reg_idx = 0;
  789. val_shift = 4;
  790. keep_mask = 0x0f;
  791. break;
  792. case 2:
  793. reg_idx = 1;
  794. val_shift = 0;
  795. keep_mask = 0xf0;
  796. break;
  797. }
  798. mutex_lock(&data->update_lock);
  799. data->temp_tolerance[nr] = TOL_TEMP_TO_REG(val);
  800. target_mask = w83791d_read(client,
  801. W83791D_REG_TEMP_TOL[reg_idx]) & keep_mask;
  802. w83791d_write(client, W83791D_REG_TEMP_TOL[reg_idx],
  803. (data->temp_tolerance[nr] << val_shift) | target_mask);
  804. mutex_unlock(&data->update_lock);
  805. return count;
  806. }
  807. static struct sensor_device_attribute sda_temp_tolerance[] = {
  808. SENSOR_ATTR(temp1_tolerance, S_IWUSR | S_IRUGO,
  809. show_temp_tolerance, store_temp_tolerance, 0),
  810. SENSOR_ATTR(temp2_tolerance, S_IWUSR | S_IRUGO,
  811. show_temp_tolerance, store_temp_tolerance, 1),
  812. SENSOR_ATTR(temp3_tolerance, S_IWUSR | S_IRUGO,
  813. show_temp_tolerance, store_temp_tolerance, 2),
  814. };
  815. /* read/write the temperature1, includes measured value and limits */
  816. static ssize_t show_temp1(struct device *dev, struct device_attribute *devattr,
  817. char *buf)
  818. {
  819. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  820. struct w83791d_data *data = w83791d_update_device(dev);
  821. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp1[attr->index]));
  822. }
  823. static ssize_t store_temp1(struct device *dev, struct device_attribute *devattr,
  824. const char *buf, size_t count)
  825. {
  826. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  827. struct i2c_client *client = to_i2c_client(dev);
  828. struct w83791d_data *data = i2c_get_clientdata(client);
  829. int nr = attr->index;
  830. long val;
  831. int err;
  832. err = kstrtol(buf, 10, &val);
  833. if (err)
  834. return err;
  835. mutex_lock(&data->update_lock);
  836. data->temp1[nr] = TEMP1_TO_REG(val);
  837. w83791d_write(client, W83791D_REG_TEMP1[nr], data->temp1[nr]);
  838. mutex_unlock(&data->update_lock);
  839. return count;
  840. }
  841. /* read/write temperature2-3, includes measured value and limits */
  842. static ssize_t show_temp23(struct device *dev, struct device_attribute *devattr,
  843. char *buf)
  844. {
  845. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  846. struct w83791d_data *data = w83791d_update_device(dev);
  847. int nr = attr->nr;
  848. int index = attr->index;
  849. return sprintf(buf, "%d\n", TEMP23_FROM_REG(data->temp_add[nr][index]));
  850. }
  851. static ssize_t store_temp23(struct device *dev,
  852. struct device_attribute *devattr,
  853. const char *buf, size_t count)
  854. {
  855. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  856. struct i2c_client *client = to_i2c_client(dev);
  857. struct w83791d_data *data = i2c_get_clientdata(client);
  858. long val;
  859. int err;
  860. int nr = attr->nr;
  861. int index = attr->index;
  862. err = kstrtol(buf, 10, &val);
  863. if (err)
  864. return err;
  865. mutex_lock(&data->update_lock);
  866. data->temp_add[nr][index] = TEMP23_TO_REG(val);
  867. w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2],
  868. data->temp_add[nr][index] >> 8);
  869. w83791d_write(client, W83791D_REG_TEMP_ADD[nr][index * 2 + 1],
  870. data->temp_add[nr][index] & 0x80);
  871. mutex_unlock(&data->update_lock);
  872. return count;
  873. }
  874. static struct sensor_device_attribute_2 sda_temp_input[] = {
  875. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp1, NULL, 0, 0),
  876. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp23, NULL, 0, 0),
  877. SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp23, NULL, 1, 0),
  878. };
  879. static struct sensor_device_attribute_2 sda_temp_max[] = {
  880. SENSOR_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
  881. show_temp1, store_temp1, 0, 1),
  882. SENSOR_ATTR_2(temp2_max, S_IRUGO | S_IWUSR,
  883. show_temp23, store_temp23, 0, 1),
  884. SENSOR_ATTR_2(temp3_max, S_IRUGO | S_IWUSR,
  885. show_temp23, store_temp23, 1, 1),
  886. };
  887. static struct sensor_device_attribute_2 sda_temp_max_hyst[] = {
  888. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
  889. show_temp1, store_temp1, 0, 2),
  890. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
  891. show_temp23, store_temp23, 0, 2),
  892. SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR,
  893. show_temp23, store_temp23, 1, 2),
  894. };
  895. /*
  896. * Note: The bitmask for the beep enable/disable is different than
  897. * the bitmask for the alarm.
  898. */
  899. static struct sensor_device_attribute sda_temp_beep[] = {
  900. SENSOR_ATTR(temp1_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 4),
  901. SENSOR_ATTR(temp2_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 5),
  902. SENSOR_ATTR(temp3_beep, S_IWUSR | S_IRUGO, show_beep, store_beep, 1),
  903. };
  904. static struct sensor_device_attribute sda_temp_alarm[] = {
  905. SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
  906. SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
  907. SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
  908. };
  909. /* get reatime status of all sensors items: voltage, temp, fan */
  910. static ssize_t show_alarms_reg(struct device *dev,
  911. struct device_attribute *attr, char *buf)
  912. {
  913. struct w83791d_data *data = w83791d_update_device(dev);
  914. return sprintf(buf, "%u\n", data->alarms);
  915. }
  916. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  917. /* Beep control */
  918. #define GLOBAL_BEEP_ENABLE_SHIFT 15
  919. #define GLOBAL_BEEP_ENABLE_MASK (1 << GLOBAL_BEEP_ENABLE_SHIFT)
  920. static ssize_t show_beep_enable(struct device *dev,
  921. struct device_attribute *attr, char *buf)
  922. {
  923. struct w83791d_data *data = w83791d_update_device(dev);
  924. return sprintf(buf, "%d\n", data->beep_enable);
  925. }
  926. static ssize_t show_beep_mask(struct device *dev,
  927. struct device_attribute *attr, char *buf)
  928. {
  929. struct w83791d_data *data = w83791d_update_device(dev);
  930. return sprintf(buf, "%d\n", BEEP_MASK_FROM_REG(data->beep_mask));
  931. }
  932. static ssize_t store_beep_mask(struct device *dev,
  933. struct device_attribute *attr,
  934. const char *buf, size_t count)
  935. {
  936. struct i2c_client *client = to_i2c_client(dev);
  937. struct w83791d_data *data = i2c_get_clientdata(client);
  938. int i;
  939. long val;
  940. int err;
  941. err = kstrtol(buf, 10, &val);
  942. if (err)
  943. return err;
  944. mutex_lock(&data->update_lock);
  945. /*
  946. * The beep_enable state overrides any enabling request from
  947. * the masks
  948. */
  949. data->beep_mask = BEEP_MASK_TO_REG(val) & ~GLOBAL_BEEP_ENABLE_MASK;
  950. data->beep_mask |= (data->beep_enable << GLOBAL_BEEP_ENABLE_SHIFT);
  951. val = data->beep_mask;
  952. for (i = 0; i < 3; i++) {
  953. w83791d_write(client, W83791D_REG_BEEP_CTRL[i], (val & 0xff));
  954. val >>= 8;
  955. }
  956. mutex_unlock(&data->update_lock);
  957. return count;
  958. }
  959. static ssize_t store_beep_enable(struct device *dev,
  960. struct device_attribute *attr,
  961. const char *buf, size_t count)
  962. {
  963. struct i2c_client *client = to_i2c_client(dev);
  964. struct w83791d_data *data = i2c_get_clientdata(client);
  965. long val;
  966. int err;
  967. err = kstrtol(buf, 10, &val);
  968. if (err)
  969. return err;
  970. mutex_lock(&data->update_lock);
  971. data->beep_enable = val ? 1 : 0;
  972. /* Keep the full mask value in sync with the current enable */
  973. data->beep_mask &= ~GLOBAL_BEEP_ENABLE_MASK;
  974. data->beep_mask |= (data->beep_enable << GLOBAL_BEEP_ENABLE_SHIFT);
  975. /*
  976. * The global control is in the second beep control register
  977. * so only need to update that register
  978. */
  979. val = (data->beep_mask >> 8) & 0xff;
  980. w83791d_write(client, W83791D_REG_BEEP_CTRL[1], val);
  981. mutex_unlock(&data->update_lock);
  982. return count;
  983. }
  984. static struct sensor_device_attribute sda_beep_ctrl[] = {
  985. SENSOR_ATTR(beep_enable, S_IRUGO | S_IWUSR,
  986. show_beep_enable, store_beep_enable, 0),
  987. SENSOR_ATTR(beep_mask, S_IRUGO | S_IWUSR,
  988. show_beep_mask, store_beep_mask, 1)
  989. };
  990. /* cpu voltage regulation information */
  991. static ssize_t show_vid_reg(struct device *dev,
  992. struct device_attribute *attr, char *buf)
  993. {
  994. struct w83791d_data *data = w83791d_update_device(dev);
  995. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  996. }
  997. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  998. static ssize_t show_vrm_reg(struct device *dev,
  999. struct device_attribute *attr, char *buf)
  1000. {
  1001. struct w83791d_data *data = dev_get_drvdata(dev);
  1002. return sprintf(buf, "%d\n", data->vrm);
  1003. }
  1004. static ssize_t store_vrm_reg(struct device *dev,
  1005. struct device_attribute *attr,
  1006. const char *buf, size_t count)
  1007. {
  1008. struct w83791d_data *data = dev_get_drvdata(dev);
  1009. unsigned long val;
  1010. int err;
  1011. /*
  1012. * No lock needed as vrm is internal to the driver
  1013. * (not read from a chip register) and so is not
  1014. * updated in w83791d_update_device()
  1015. */
  1016. err = kstrtoul(buf, 10, &val);
  1017. if (err)
  1018. return err;
  1019. data->vrm = val;
  1020. return count;
  1021. }
  1022. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  1023. #define IN_UNIT_ATTRS(X) \
  1024. &sda_in_input[X].dev_attr.attr, \
  1025. &sda_in_min[X].dev_attr.attr, \
  1026. &sda_in_max[X].dev_attr.attr, \
  1027. &sda_in_beep[X].dev_attr.attr, \
  1028. &sda_in_alarm[X].dev_attr.attr
  1029. #define FAN_UNIT_ATTRS(X) \
  1030. &sda_fan_input[X].dev_attr.attr, \
  1031. &sda_fan_min[X].dev_attr.attr, \
  1032. &sda_fan_div[X].dev_attr.attr, \
  1033. &sda_fan_beep[X].dev_attr.attr, \
  1034. &sda_fan_alarm[X].dev_attr.attr
  1035. #define TEMP_UNIT_ATTRS(X) \
  1036. &sda_temp_input[X].dev_attr.attr, \
  1037. &sda_temp_max[X].dev_attr.attr, \
  1038. &sda_temp_max_hyst[X].dev_attr.attr, \
  1039. &sda_temp_beep[X].dev_attr.attr, \
  1040. &sda_temp_alarm[X].dev_attr.attr
  1041. static struct attribute *w83791d_attributes[] = {
  1042. IN_UNIT_ATTRS(0),
  1043. IN_UNIT_ATTRS(1),
  1044. IN_UNIT_ATTRS(2),
  1045. IN_UNIT_ATTRS(3),
  1046. IN_UNIT_ATTRS(4),
  1047. IN_UNIT_ATTRS(5),
  1048. IN_UNIT_ATTRS(6),
  1049. IN_UNIT_ATTRS(7),
  1050. IN_UNIT_ATTRS(8),
  1051. IN_UNIT_ATTRS(9),
  1052. FAN_UNIT_ATTRS(0),
  1053. FAN_UNIT_ATTRS(1),
  1054. FAN_UNIT_ATTRS(2),
  1055. TEMP_UNIT_ATTRS(0),
  1056. TEMP_UNIT_ATTRS(1),
  1057. TEMP_UNIT_ATTRS(2),
  1058. &dev_attr_alarms.attr,
  1059. &sda_beep_ctrl[0].dev_attr.attr,
  1060. &sda_beep_ctrl[1].dev_attr.attr,
  1061. &dev_attr_cpu0_vid.attr,
  1062. &dev_attr_vrm.attr,
  1063. &sda_pwm[0].dev_attr.attr,
  1064. &sda_pwm[1].dev_attr.attr,
  1065. &sda_pwm[2].dev_attr.attr,
  1066. &sda_pwmenable[0].dev_attr.attr,
  1067. &sda_pwmenable[1].dev_attr.attr,
  1068. &sda_pwmenable[2].dev_attr.attr,
  1069. &sda_temp_target[0].dev_attr.attr,
  1070. &sda_temp_target[1].dev_attr.attr,
  1071. &sda_temp_target[2].dev_attr.attr,
  1072. &sda_temp_tolerance[0].dev_attr.attr,
  1073. &sda_temp_tolerance[1].dev_attr.attr,
  1074. &sda_temp_tolerance[2].dev_attr.attr,
  1075. NULL
  1076. };
  1077. static const struct attribute_group w83791d_group = {
  1078. .attrs = w83791d_attributes,
  1079. };
  1080. /*
  1081. * Separate group of attributes for fan/pwm 4-5. Their pins can also be
  1082. * in use for GPIO in which case their sysfs-interface should not be made
  1083. * available
  1084. */
  1085. static struct attribute *w83791d_attributes_fanpwm45[] = {
  1086. FAN_UNIT_ATTRS(3),
  1087. FAN_UNIT_ATTRS(4),
  1088. &sda_pwm[3].dev_attr.attr,
  1089. &sda_pwm[4].dev_attr.attr,
  1090. NULL
  1091. };
  1092. static const struct attribute_group w83791d_group_fanpwm45 = {
  1093. .attrs = w83791d_attributes_fanpwm45,
  1094. };
  1095. static int w83791d_detect_subclients(struct i2c_client *client)
  1096. {
  1097. struct i2c_adapter *adapter = client->adapter;
  1098. struct w83791d_data *data = i2c_get_clientdata(client);
  1099. int address = client->addr;
  1100. int i, id, err;
  1101. u8 val;
  1102. id = i2c_adapter_id(adapter);
  1103. if (force_subclients[0] == id && force_subclients[1] == address) {
  1104. for (i = 2; i <= 3; i++) {
  1105. if (force_subclients[i] < 0x48 ||
  1106. force_subclients[i] > 0x4f) {
  1107. dev_err(&client->dev,
  1108. "invalid subclient "
  1109. "address %d; must be 0x48-0x4f\n",
  1110. force_subclients[i]);
  1111. err = -ENODEV;
  1112. goto error_sc_0;
  1113. }
  1114. }
  1115. w83791d_write(client, W83791D_REG_I2C_SUBADDR,
  1116. (force_subclients[2] & 0x07) |
  1117. ((force_subclients[3] & 0x07) << 4));
  1118. }
  1119. val = w83791d_read(client, W83791D_REG_I2C_SUBADDR);
  1120. if (!(val & 0x08))
  1121. data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (val & 0x7));
  1122. if (!(val & 0x80)) {
  1123. if ((data->lm75[0] != NULL) &&
  1124. ((val & 0x7) == ((val >> 4) & 0x7))) {
  1125. dev_err(&client->dev,
  1126. "duplicate addresses 0x%x, "
  1127. "use force_subclient\n",
  1128. data->lm75[0]->addr);
  1129. err = -ENODEV;
  1130. goto error_sc_1;
  1131. }
  1132. data->lm75[1] = i2c_new_dummy(adapter,
  1133. 0x48 + ((val >> 4) & 0x7));
  1134. }
  1135. return 0;
  1136. /* Undo inits in case of errors */
  1137. error_sc_1:
  1138. if (data->lm75[0] != NULL)
  1139. i2c_unregister_device(data->lm75[0]);
  1140. error_sc_0:
  1141. return err;
  1142. }
  1143. /* Return 0 if detection is successful, -ENODEV otherwise */
  1144. static int w83791d_detect(struct i2c_client *client,
  1145. struct i2c_board_info *info)
  1146. {
  1147. struct i2c_adapter *adapter = client->adapter;
  1148. int val1, val2;
  1149. unsigned short address = client->addr;
  1150. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1151. return -ENODEV;
  1152. if (w83791d_read(client, W83791D_REG_CONFIG) & 0x80)
  1153. return -ENODEV;
  1154. val1 = w83791d_read(client, W83791D_REG_BANK);
  1155. val2 = w83791d_read(client, W83791D_REG_CHIPMAN);
  1156. /* Check for Winbond ID if in bank 0 */
  1157. if (!(val1 & 0x07)) {
  1158. if ((!(val1 & 0x80) && val2 != 0xa3) ||
  1159. ((val1 & 0x80) && val2 != 0x5c)) {
  1160. return -ENODEV;
  1161. }
  1162. }
  1163. /*
  1164. * If Winbond chip, address of chip and W83791D_REG_I2C_ADDR
  1165. * should match
  1166. */
  1167. if (w83791d_read(client, W83791D_REG_I2C_ADDR) != address)
  1168. return -ENODEV;
  1169. /* We want bank 0 and Vendor ID high byte */
  1170. val1 = w83791d_read(client, W83791D_REG_BANK) & 0x78;
  1171. w83791d_write(client, W83791D_REG_BANK, val1 | 0x80);
  1172. /* Verify it is a Winbond w83791d */
  1173. val1 = w83791d_read(client, W83791D_REG_WCHIPID);
  1174. val2 = w83791d_read(client, W83791D_REG_CHIPMAN);
  1175. if (val1 != 0x71 || val2 != 0x5c)
  1176. return -ENODEV;
  1177. strlcpy(info->type, "w83791d", I2C_NAME_SIZE);
  1178. return 0;
  1179. }
  1180. static int w83791d_probe(struct i2c_client *client,
  1181. const struct i2c_device_id *id)
  1182. {
  1183. struct w83791d_data *data;
  1184. struct device *dev = &client->dev;
  1185. int i, err;
  1186. u8 has_fanpwm45;
  1187. #ifdef DEBUG
  1188. int val1;
  1189. val1 = w83791d_read(client, W83791D_REG_DID_VID4);
  1190. dev_dbg(dev, "Device ID version: %d.%d (0x%02x)\n",
  1191. (val1 >> 5) & 0x07, (val1 >> 1) & 0x0f, val1);
  1192. #endif
  1193. data = devm_kzalloc(&client->dev, sizeof(struct w83791d_data),
  1194. GFP_KERNEL);
  1195. if (!data)
  1196. return -ENOMEM;
  1197. i2c_set_clientdata(client, data);
  1198. mutex_init(&data->update_lock);
  1199. err = w83791d_detect_subclients(client);
  1200. if (err)
  1201. return err;
  1202. /* Initialize the chip */
  1203. w83791d_init_client(client);
  1204. /*
  1205. * If the fan_div is changed, make sure there is a rational
  1206. * fan_min in place
  1207. */
  1208. for (i = 0; i < NUMBER_OF_FANIN; i++)
  1209. data->fan_min[i] = w83791d_read(client, W83791D_REG_FAN_MIN[i]);
  1210. /* Register sysfs hooks */
  1211. err = sysfs_create_group(&client->dev.kobj, &w83791d_group);
  1212. if (err)
  1213. goto error3;
  1214. /* Check if pins of fan/pwm 4-5 are in use as GPIO */
  1215. has_fanpwm45 = w83791d_read(client, W83791D_REG_GPIO) & 0x10;
  1216. if (has_fanpwm45) {
  1217. err = sysfs_create_group(&client->dev.kobj,
  1218. &w83791d_group_fanpwm45);
  1219. if (err)
  1220. goto error4;
  1221. }
  1222. /* Everything is ready, now register the working device */
  1223. data->hwmon_dev = hwmon_device_register(dev);
  1224. if (IS_ERR(data->hwmon_dev)) {
  1225. err = PTR_ERR(data->hwmon_dev);
  1226. goto error5;
  1227. }
  1228. return 0;
  1229. error5:
  1230. if (has_fanpwm45)
  1231. sysfs_remove_group(&client->dev.kobj, &w83791d_group_fanpwm45);
  1232. error4:
  1233. sysfs_remove_group(&client->dev.kobj, &w83791d_group);
  1234. error3:
  1235. if (data->lm75[0] != NULL)
  1236. i2c_unregister_device(data->lm75[0]);
  1237. if (data->lm75[1] != NULL)
  1238. i2c_unregister_device(data->lm75[1]);
  1239. return err;
  1240. }
  1241. static int w83791d_remove(struct i2c_client *client)
  1242. {
  1243. struct w83791d_data *data = i2c_get_clientdata(client);
  1244. hwmon_device_unregister(data->hwmon_dev);
  1245. sysfs_remove_group(&client->dev.kobj, &w83791d_group);
  1246. if (data->lm75[0] != NULL)
  1247. i2c_unregister_device(data->lm75[0]);
  1248. if (data->lm75[1] != NULL)
  1249. i2c_unregister_device(data->lm75[1]);
  1250. return 0;
  1251. }
  1252. static void w83791d_init_client(struct i2c_client *client)
  1253. {
  1254. struct w83791d_data *data = i2c_get_clientdata(client);
  1255. u8 tmp;
  1256. u8 old_beep;
  1257. /*
  1258. * The difference between reset and init is that reset
  1259. * does a hard reset of the chip via index 0x40, bit 7,
  1260. * but init simply forces certain registers to have "sane"
  1261. * values. The hope is that the BIOS has done the right
  1262. * thing (which is why the default is reset=0, init=0),
  1263. * but if not, reset is the hard hammer and init
  1264. * is the soft mallet both of which are trying to whack
  1265. * things into place...
  1266. * NOTE: The data sheet makes a distinction between
  1267. * "power on defaults" and "reset by MR". As far as I can tell,
  1268. * the hard reset puts everything into a power-on state so I'm
  1269. * not sure what "reset by MR" means or how it can happen.
  1270. */
  1271. if (reset || init) {
  1272. /* keep some BIOS settings when we... */
  1273. old_beep = w83791d_read(client, W83791D_REG_BEEP_CONFIG);
  1274. if (reset) {
  1275. /* ... reset the chip and ... */
  1276. w83791d_write(client, W83791D_REG_CONFIG, 0x80);
  1277. }
  1278. /* ... disable power-on abnormal beep */
  1279. w83791d_write(client, W83791D_REG_BEEP_CONFIG, old_beep | 0x80);
  1280. /* disable the global beep (not done by hard reset) */
  1281. tmp = w83791d_read(client, W83791D_REG_BEEP_CTRL[1]);
  1282. w83791d_write(client, W83791D_REG_BEEP_CTRL[1], tmp & 0xef);
  1283. if (init) {
  1284. /* Make sure monitoring is turned on for add-ons */
  1285. tmp = w83791d_read(client, W83791D_REG_TEMP2_CONFIG);
  1286. if (tmp & 1) {
  1287. w83791d_write(client, W83791D_REG_TEMP2_CONFIG,
  1288. tmp & 0xfe);
  1289. }
  1290. tmp = w83791d_read(client, W83791D_REG_TEMP3_CONFIG);
  1291. if (tmp & 1) {
  1292. w83791d_write(client, W83791D_REG_TEMP3_CONFIG,
  1293. tmp & 0xfe);
  1294. }
  1295. /* Start monitoring */
  1296. tmp = w83791d_read(client, W83791D_REG_CONFIG) & 0xf7;
  1297. w83791d_write(client, W83791D_REG_CONFIG, tmp | 0x01);
  1298. }
  1299. }
  1300. data->vrm = vid_which_vrm();
  1301. }
  1302. static struct w83791d_data *w83791d_update_device(struct device *dev)
  1303. {
  1304. struct i2c_client *client = to_i2c_client(dev);
  1305. struct w83791d_data *data = i2c_get_clientdata(client);
  1306. int i, j;
  1307. u8 reg_array_tmp[3];
  1308. u8 vbat_reg;
  1309. mutex_lock(&data->update_lock);
  1310. if (time_after(jiffies, data->last_updated + (HZ * 3))
  1311. || !data->valid) {
  1312. dev_dbg(dev, "Starting w83791d device update\n");
  1313. /* Update the voltages measured value and limits */
  1314. for (i = 0; i < NUMBER_OF_VIN; i++) {
  1315. data->in[i] = w83791d_read(client,
  1316. W83791D_REG_IN[i]);
  1317. data->in_max[i] = w83791d_read(client,
  1318. W83791D_REG_IN_MAX[i]);
  1319. data->in_min[i] = w83791d_read(client,
  1320. W83791D_REG_IN_MIN[i]);
  1321. }
  1322. /* Update the fan counts and limits */
  1323. for (i = 0; i < NUMBER_OF_FANIN; i++) {
  1324. /* Update the Fan measured value and limits */
  1325. data->fan[i] = w83791d_read(client,
  1326. W83791D_REG_FAN[i]);
  1327. data->fan_min[i] = w83791d_read(client,
  1328. W83791D_REG_FAN_MIN[i]);
  1329. }
  1330. /* Update the fan divisor */
  1331. for (i = 0; i < 3; i++) {
  1332. reg_array_tmp[i] = w83791d_read(client,
  1333. W83791D_REG_FAN_DIV[i]);
  1334. }
  1335. data->fan_div[0] = (reg_array_tmp[0] >> 4) & 0x03;
  1336. data->fan_div[1] = (reg_array_tmp[0] >> 6) & 0x03;
  1337. data->fan_div[2] = (reg_array_tmp[1] >> 6) & 0x03;
  1338. data->fan_div[3] = reg_array_tmp[2] & 0x07;
  1339. data->fan_div[4] = (reg_array_tmp[2] >> 4) & 0x07;
  1340. /*
  1341. * The fan divisor for fans 0-2 get bit 2 from
  1342. * bits 5-7 respectively of vbat register
  1343. */
  1344. vbat_reg = w83791d_read(client, W83791D_REG_VBAT);
  1345. for (i = 0; i < 3; i++)
  1346. data->fan_div[i] |= (vbat_reg >> (3 + i)) & 0x04;
  1347. /* Update PWM duty cycle */
  1348. for (i = 0; i < NUMBER_OF_PWM; i++) {
  1349. data->pwm[i] = w83791d_read(client,
  1350. W83791D_REG_PWM[i]);
  1351. }
  1352. /* Update PWM enable status */
  1353. for (i = 0; i < 2; i++) {
  1354. reg_array_tmp[i] = w83791d_read(client,
  1355. W83791D_REG_FAN_CFG[i]);
  1356. }
  1357. data->pwm_enable[0] = (reg_array_tmp[0] >> 2) & 0x03;
  1358. data->pwm_enable[1] = (reg_array_tmp[0] >> 4) & 0x03;
  1359. data->pwm_enable[2] = (reg_array_tmp[1] >> 2) & 0x03;
  1360. /* Update PWM target temperature */
  1361. for (i = 0; i < 3; i++) {
  1362. data->temp_target[i] = w83791d_read(client,
  1363. W83791D_REG_TEMP_TARGET[i]) & 0x7f;
  1364. }
  1365. /* Update PWM temperature tolerance */
  1366. for (i = 0; i < 2; i++) {
  1367. reg_array_tmp[i] = w83791d_read(client,
  1368. W83791D_REG_TEMP_TOL[i]);
  1369. }
  1370. data->temp_tolerance[0] = reg_array_tmp[0] & 0x0f;
  1371. data->temp_tolerance[1] = (reg_array_tmp[0] >> 4) & 0x0f;
  1372. data->temp_tolerance[2] = reg_array_tmp[1] & 0x0f;
  1373. /* Update the first temperature sensor */
  1374. for (i = 0; i < 3; i++) {
  1375. data->temp1[i] = w83791d_read(client,
  1376. W83791D_REG_TEMP1[i]);
  1377. }
  1378. /* Update the rest of the temperature sensors */
  1379. for (i = 0; i < 2; i++) {
  1380. for (j = 0; j < 3; j++) {
  1381. data->temp_add[i][j] =
  1382. (w83791d_read(client,
  1383. W83791D_REG_TEMP_ADD[i][j * 2]) << 8) |
  1384. w83791d_read(client,
  1385. W83791D_REG_TEMP_ADD[i][j * 2 + 1]);
  1386. }
  1387. }
  1388. /* Update the realtime status */
  1389. data->alarms =
  1390. w83791d_read(client, W83791D_REG_ALARM1) +
  1391. (w83791d_read(client, W83791D_REG_ALARM2) << 8) +
  1392. (w83791d_read(client, W83791D_REG_ALARM3) << 16);
  1393. /* Update the beep configuration information */
  1394. data->beep_mask =
  1395. w83791d_read(client, W83791D_REG_BEEP_CTRL[0]) +
  1396. (w83791d_read(client, W83791D_REG_BEEP_CTRL[1]) << 8) +
  1397. (w83791d_read(client, W83791D_REG_BEEP_CTRL[2]) << 16);
  1398. /* Extract global beep enable flag */
  1399. data->beep_enable =
  1400. (data->beep_mask >> GLOBAL_BEEP_ENABLE_SHIFT) & 0x01;
  1401. /* Update the cpu voltage information */
  1402. i = w83791d_read(client, W83791D_REG_VID_FANDIV);
  1403. data->vid = i & 0x0f;
  1404. data->vid |= (w83791d_read(client, W83791D_REG_DID_VID4) & 0x01)
  1405. << 4;
  1406. data->last_updated = jiffies;
  1407. data->valid = 1;
  1408. }
  1409. mutex_unlock(&data->update_lock);
  1410. #ifdef DEBUG
  1411. w83791d_print_debug(data, dev);
  1412. #endif
  1413. return data;
  1414. }
  1415. #ifdef DEBUG
  1416. static void w83791d_print_debug(struct w83791d_data *data, struct device *dev)
  1417. {
  1418. int i = 0, j = 0;
  1419. dev_dbg(dev, "======Start of w83791d debug values======\n");
  1420. dev_dbg(dev, "%d set of Voltages: ===>\n", NUMBER_OF_VIN);
  1421. for (i = 0; i < NUMBER_OF_VIN; i++) {
  1422. dev_dbg(dev, "vin[%d] is: 0x%02x\n", i, data->in[i]);
  1423. dev_dbg(dev, "vin[%d] min is: 0x%02x\n", i, data->in_min[i]);
  1424. dev_dbg(dev, "vin[%d] max is: 0x%02x\n", i, data->in_max[i]);
  1425. }
  1426. dev_dbg(dev, "%d set of Fan Counts/Divisors: ===>\n", NUMBER_OF_FANIN);
  1427. for (i = 0; i < NUMBER_OF_FANIN; i++) {
  1428. dev_dbg(dev, "fan[%d] is: 0x%02x\n", i, data->fan[i]);
  1429. dev_dbg(dev, "fan[%d] min is: 0x%02x\n", i, data->fan_min[i]);
  1430. dev_dbg(dev, "fan_div[%d] is: 0x%02x\n", i, data->fan_div[i]);
  1431. }
  1432. /*
  1433. * temperature math is signed, but only print out the
  1434. * bits that matter
  1435. */
  1436. dev_dbg(dev, "%d set of Temperatures: ===>\n", NUMBER_OF_TEMPIN);
  1437. for (i = 0; i < 3; i++)
  1438. dev_dbg(dev, "temp1[%d] is: 0x%02x\n", i, (u8) data->temp1[i]);
  1439. for (i = 0; i < 2; i++) {
  1440. for (j = 0; j < 3; j++) {
  1441. dev_dbg(dev, "temp_add[%d][%d] is: 0x%04x\n", i, j,
  1442. (u16) data->temp_add[i][j]);
  1443. }
  1444. }
  1445. dev_dbg(dev, "Misc Information: ===>\n");
  1446. dev_dbg(dev, "alarm is: 0x%08x\n", data->alarms);
  1447. dev_dbg(dev, "beep_mask is: 0x%08x\n", data->beep_mask);
  1448. dev_dbg(dev, "beep_enable is: %d\n", data->beep_enable);
  1449. dev_dbg(dev, "vid is: 0x%02x\n", data->vid);
  1450. dev_dbg(dev, "vrm is: 0x%02x\n", data->vrm);
  1451. dev_dbg(dev, "=======End of w83791d debug values========\n");
  1452. dev_dbg(dev, "\n");
  1453. }
  1454. #endif
  1455. module_i2c_driver(w83791d_driver);
  1456. MODULE_AUTHOR("Charles Spirakis <bezaur@gmail.com>");
  1457. MODULE_DESCRIPTION("W83791D driver");
  1458. MODULE_LICENSE("GPL");