w83791d.c 49 KB

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