w83792d.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * w83792d.c - Part of lm_sensors, Linux kernel modules for hardware
  4. * monitoring
  5. * Copyright (C) 2004, 2005 Winbond Electronics Corp.
  6. * Shane Huang,
  7. * Rudolf Marek <[email protected]>
  8. *
  9. * Note:
  10. * 1. This driver is only for 2.6 kernel, 2.4 kernel need a different driver.
  11. * 2. This driver is only for Winbond W83792D C version device, there
  12. * are also some motherboards with B version W83792D device. The
  13. * calculation method to in6-in7(measured value, limits) is a little
  14. * different between C and B version. C or B version can be identified
  15. * by CR[0x49h].
  16. */
  17. /*
  18. * Supports following chips:
  19. *
  20. * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  21. * w83792d 9 7 7 3 0x7a 0x5ca3 yes no
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-sysfs.h>
  29. #include <linux/err.h>
  30. #include <linux/mutex.h>
  31. #include <linux/sysfs.h>
  32. #include <linux/jiffies.h>
  33. /* Addresses to scan */
  34. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  35. I2C_CLIENT_END };
  36. /* Insmod parameters */
  37. static unsigned short force_subclients[4];
  38. module_param_array(force_subclients, short, NULL, 0);
  39. MODULE_PARM_DESC(force_subclients,
  40. "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
  41. static bool init;
  42. module_param(init, bool, 0);
  43. MODULE_PARM_DESC(init, "Set to one to force chip initialization");
  44. /* The W83792D registers */
  45. static const u8 W83792D_REG_IN[9] = {
  46. 0x20, /* Vcore A in DataSheet */
  47. 0x21, /* Vcore B in DataSheet */
  48. 0x22, /* VIN0 in DataSheet */
  49. 0x23, /* VIN1 in DataSheet */
  50. 0x24, /* VIN2 in DataSheet */
  51. 0x25, /* VIN3 in DataSheet */
  52. 0x26, /* 5VCC in DataSheet */
  53. 0xB0, /* 5VSB in DataSheet */
  54. 0xB1 /* VBAT in DataSheet */
  55. };
  56. #define W83792D_REG_LOW_BITS1 0x3E /* Low Bits I in DataSheet */
  57. #define W83792D_REG_LOW_BITS2 0x3F /* Low Bits II in DataSheet */
  58. static const u8 W83792D_REG_IN_MAX[9] = {
  59. 0x2B, /* Vcore A High Limit in DataSheet */
  60. 0x2D, /* Vcore B High Limit in DataSheet */
  61. 0x2F, /* VIN0 High Limit in DataSheet */
  62. 0x31, /* VIN1 High Limit in DataSheet */
  63. 0x33, /* VIN2 High Limit in DataSheet */
  64. 0x35, /* VIN3 High Limit in DataSheet */
  65. 0x37, /* 5VCC High Limit in DataSheet */
  66. 0xB4, /* 5VSB High Limit in DataSheet */
  67. 0xB6 /* VBAT High Limit in DataSheet */
  68. };
  69. static const u8 W83792D_REG_IN_MIN[9] = {
  70. 0x2C, /* Vcore A Low Limit in DataSheet */
  71. 0x2E, /* Vcore B Low Limit in DataSheet */
  72. 0x30, /* VIN0 Low Limit in DataSheet */
  73. 0x32, /* VIN1 Low Limit in DataSheet */
  74. 0x34, /* VIN2 Low Limit in DataSheet */
  75. 0x36, /* VIN3 Low Limit in DataSheet */
  76. 0x38, /* 5VCC Low Limit in DataSheet */
  77. 0xB5, /* 5VSB Low Limit in DataSheet */
  78. 0xB7 /* VBAT Low Limit in DataSheet */
  79. };
  80. static const u8 W83792D_REG_FAN[7] = {
  81. 0x28, /* FAN 1 Count in DataSheet */
  82. 0x29, /* FAN 2 Count in DataSheet */
  83. 0x2A, /* FAN 3 Count in DataSheet */
  84. 0xB8, /* FAN 4 Count in DataSheet */
  85. 0xB9, /* FAN 5 Count in DataSheet */
  86. 0xBA, /* FAN 6 Count in DataSheet */
  87. 0xBE /* FAN 7 Count in DataSheet */
  88. };
  89. static const u8 W83792D_REG_FAN_MIN[7] = {
  90. 0x3B, /* FAN 1 Count Low Limit in DataSheet */
  91. 0x3C, /* FAN 2 Count Low Limit in DataSheet */
  92. 0x3D, /* FAN 3 Count Low Limit in DataSheet */
  93. 0xBB, /* FAN 4 Count Low Limit in DataSheet */
  94. 0xBC, /* FAN 5 Count Low Limit in DataSheet */
  95. 0xBD, /* FAN 6 Count Low Limit in DataSheet */
  96. 0xBF /* FAN 7 Count Low Limit in DataSheet */
  97. };
  98. #define W83792D_REG_FAN_CFG 0x84 /* FAN Configuration in DataSheet */
  99. static const u8 W83792D_REG_FAN_DIV[4] = {
  100. 0x47, /* contains FAN2 and FAN1 Divisor */
  101. 0x5B, /* contains FAN4 and FAN3 Divisor */
  102. 0x5C, /* contains FAN6 and FAN5 Divisor */
  103. 0x9E /* contains FAN7 Divisor. */
  104. };
  105. static const u8 W83792D_REG_PWM[7] = {
  106. 0x81, /* FAN 1 Duty Cycle, be used to control */
  107. 0x83, /* FAN 2 Duty Cycle, be used to control */
  108. 0x94, /* FAN 3 Duty Cycle, be used to control */
  109. 0xA3, /* FAN 4 Duty Cycle, be used to control */
  110. 0xA4, /* FAN 5 Duty Cycle, be used to control */
  111. 0xA5, /* FAN 6 Duty Cycle, be used to control */
  112. 0xA6 /* FAN 7 Duty Cycle, be used to control */
  113. };
  114. #define W83792D_REG_BANK 0x4E
  115. #define W83792D_REG_TEMP2_CONFIG 0xC2
  116. #define W83792D_REG_TEMP3_CONFIG 0xCA
  117. static const u8 W83792D_REG_TEMP1[3] = {
  118. 0x27, /* TEMP 1 in DataSheet */
  119. 0x39, /* TEMP 1 Over in DataSheet */
  120. 0x3A, /* TEMP 1 Hyst in DataSheet */
  121. };
  122. static const u8 W83792D_REG_TEMP_ADD[2][6] = {
  123. { 0xC0, /* TEMP 2 in DataSheet */
  124. 0xC1, /* TEMP 2(0.5 deg) in DataSheet */
  125. 0xC5, /* TEMP 2 Over High part in DataSheet */
  126. 0xC6, /* TEMP 2 Over Low part in DataSheet */
  127. 0xC3, /* TEMP 2 Thyst High part in DataSheet */
  128. 0xC4 }, /* TEMP 2 Thyst Low part in DataSheet */
  129. { 0xC8, /* TEMP 3 in DataSheet */
  130. 0xC9, /* TEMP 3(0.5 deg) in DataSheet */
  131. 0xCD, /* TEMP 3 Over High part in DataSheet */
  132. 0xCE, /* TEMP 3 Over Low part in DataSheet */
  133. 0xCB, /* TEMP 3 Thyst High part in DataSheet */
  134. 0xCC } /* TEMP 3 Thyst Low part in DataSheet */
  135. };
  136. static const u8 W83792D_REG_THERMAL[3] = {
  137. 0x85, /* SmartFanI: Fan1 target value */
  138. 0x86, /* SmartFanI: Fan2 target value */
  139. 0x96 /* SmartFanI: Fan3 target value */
  140. };
  141. static const u8 W83792D_REG_TOLERANCE[3] = {
  142. 0x87, /* (bit3-0)SmartFan Fan1 tolerance */
  143. 0x87, /* (bit7-4)SmartFan Fan2 tolerance */
  144. 0x97 /* (bit3-0)SmartFan Fan3 tolerance */
  145. };
  146. static const u8 W83792D_REG_POINTS[3][4] = {
  147. { 0x85, /* SmartFanII: Fan1 temp point 1 */
  148. 0xE3, /* SmartFanII: Fan1 temp point 2 */
  149. 0xE4, /* SmartFanII: Fan1 temp point 3 */
  150. 0xE5 }, /* SmartFanII: Fan1 temp point 4 */
  151. { 0x86, /* SmartFanII: Fan2 temp point 1 */
  152. 0xE6, /* SmartFanII: Fan2 temp point 2 */
  153. 0xE7, /* SmartFanII: Fan2 temp point 3 */
  154. 0xE8 }, /* SmartFanII: Fan2 temp point 4 */
  155. { 0x96, /* SmartFanII: Fan3 temp point 1 */
  156. 0xE9, /* SmartFanII: Fan3 temp point 2 */
  157. 0xEA, /* SmartFanII: Fan3 temp point 3 */
  158. 0xEB } /* SmartFanII: Fan3 temp point 4 */
  159. };
  160. static const u8 W83792D_REG_LEVELS[3][4] = {
  161. { 0x88, /* (bit3-0) SmartFanII: Fan1 Non-Stop */
  162. 0x88, /* (bit7-4) SmartFanII: Fan1 Level 1 */
  163. 0xE0, /* (bit7-4) SmartFanII: Fan1 Level 2 */
  164. 0xE0 }, /* (bit3-0) SmartFanII: Fan1 Level 3 */
  165. { 0x89, /* (bit3-0) SmartFanII: Fan2 Non-Stop */
  166. 0x89, /* (bit7-4) SmartFanII: Fan2 Level 1 */
  167. 0xE1, /* (bit7-4) SmartFanII: Fan2 Level 2 */
  168. 0xE1 }, /* (bit3-0) SmartFanII: Fan2 Level 3 */
  169. { 0x98, /* (bit3-0) SmartFanII: Fan3 Non-Stop */
  170. 0x98, /* (bit7-4) SmartFanII: Fan3 Level 1 */
  171. 0xE2, /* (bit7-4) SmartFanII: Fan3 Level 2 */
  172. 0xE2 } /* (bit3-0) SmartFanII: Fan3 Level 3 */
  173. };
  174. #define W83792D_REG_GPIO_EN 0x1A
  175. #define W83792D_REG_CONFIG 0x40
  176. #define W83792D_REG_VID_FANDIV 0x47
  177. #define W83792D_REG_CHIPID 0x49
  178. #define W83792D_REG_WCHIPID 0x58
  179. #define W83792D_REG_CHIPMAN 0x4F
  180. #define W83792D_REG_PIN 0x4B
  181. #define W83792D_REG_I2C_SUBADDR 0x4A
  182. #define W83792D_REG_ALARM1 0xA9 /* realtime status register1 */
  183. #define W83792D_REG_ALARM2 0xAA /* realtime status register2 */
  184. #define W83792D_REG_ALARM3 0xAB /* realtime status register3 */
  185. #define W83792D_REG_CHASSIS 0x42 /* Bit 5: Case Open status bit */
  186. #define W83792D_REG_CHASSIS_CLR 0x44 /* Bit 7: Case Open CLR_CHS/Reset bit */
  187. /* control in0/in1 's limit modifiability */
  188. #define W83792D_REG_VID_IN_B 0x17
  189. #define W83792D_REG_VBAT 0x5D
  190. #define W83792D_REG_I2C_ADDR 0x48
  191. /*
  192. * Conversions. Rounding and limit checking is only done on the TO_REG
  193. * variants. Note that you should be a bit careful with which arguments
  194. * these macros are called: arguments may be evaluated more than once.
  195. * Fixing this is just not worth it.
  196. */
  197. #define IN_FROM_REG(nr, val) (((nr) <= 1) ? ((val) * 2) : \
  198. ((((nr) == 6) || ((nr) == 7)) ? ((val) * 6) : ((val) * 4)))
  199. #define IN_TO_REG(nr, val) (((nr) <= 1) ? ((val) / 2) : \
  200. ((((nr) == 6) || ((nr) == 7)) ? ((val) / 6) : ((val) / 4)))
  201. static inline u8
  202. FAN_TO_REG(long rpm, int div)
  203. {
  204. if (rpm == 0)
  205. return 255;
  206. rpm = clamp_val(rpm, 1, 1000000);
  207. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  208. }
  209. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : \
  210. ((val) == 255 ? 0 : \
  211. 1350000 / ((val) * (div))))
  212. /* for temp1 */
  213. #define TEMP1_TO_REG(val) (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 \
  214. : (val)) / 1000, 0, 0xff))
  215. #define TEMP1_FROM_REG(val) (((val) & 0x80 ? (val)-0x100 : (val)) * 1000)
  216. /* for temp2 and temp3, because they need additional resolution */
  217. #define TEMP_ADD_FROM_REG(val1, val2) \
  218. ((((val1) & 0x80 ? (val1)-0x100 \
  219. : (val1)) * 1000) + ((val2 & 0x80) ? 500 : 0))
  220. #define TEMP_ADD_TO_REG_HIGH(val) \
  221. (clamp_val(((val) < 0 ? (val) + 0x100 * 1000 : (val)) / 1000, 0, 0xff))
  222. #define TEMP_ADD_TO_REG_LOW(val) ((val%1000) ? 0x80 : 0x00)
  223. #define DIV_FROM_REG(val) (1 << (val))
  224. static inline u8
  225. DIV_TO_REG(long val)
  226. {
  227. int i;
  228. val = clamp_val(val, 1, 128) >> 1;
  229. for (i = 0; i < 7; i++) {
  230. if (val == 0)
  231. break;
  232. val >>= 1;
  233. }
  234. return (u8)i;
  235. }
  236. struct w83792d_data {
  237. struct device *hwmon_dev;
  238. struct mutex update_lock;
  239. bool valid; /* true if following fields are valid */
  240. unsigned long last_updated; /* In jiffies */
  241. u8 in[9]; /* Register value */
  242. u8 in_max[9]; /* Register value */
  243. u8 in_min[9]; /* Register value */
  244. u16 low_bits; /* Additional resolution to voltage in6-0 */
  245. u8 fan[7]; /* Register value */
  246. u8 fan_min[7]; /* Register value */
  247. u8 temp1[3]; /* current, over, thyst */
  248. u8 temp_add[2][6]; /* Register value */
  249. u8 fan_div[7]; /* Register encoding, shifted right */
  250. u8 pwm[7]; /* The 7 PWM outputs */
  251. u8 pwmenable[3];
  252. u32 alarms; /* realtime status register encoding,combined */
  253. u8 chassis; /* Chassis status */
  254. u8 thermal_cruise[3]; /* Smart FanI: Fan1,2,3 target value */
  255. u8 tolerance[3]; /* Fan1,2,3 tolerance(Smart Fan I/II) */
  256. u8 sf2_points[3][4]; /* Smart FanII: Fan1,2,3 temperature points */
  257. u8 sf2_levels[3][4]; /* Smart FanII: Fan1,2,3 duty cycle levels */
  258. };
  259. static int w83792d_probe(struct i2c_client *client);
  260. static int w83792d_detect(struct i2c_client *client,
  261. struct i2c_board_info *info);
  262. static void w83792d_remove(struct i2c_client *client);
  263. static struct w83792d_data *w83792d_update_device(struct device *dev);
  264. #ifdef DEBUG
  265. static void w83792d_print_debug(struct w83792d_data *data, struct device *dev);
  266. #endif
  267. static void w83792d_init_client(struct i2c_client *client);
  268. static const struct i2c_device_id w83792d_id[] = {
  269. { "w83792d", 0 },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(i2c, w83792d_id);
  273. static struct i2c_driver w83792d_driver = {
  274. .class = I2C_CLASS_HWMON,
  275. .driver = {
  276. .name = "w83792d",
  277. },
  278. .probe_new = w83792d_probe,
  279. .remove = w83792d_remove,
  280. .id_table = w83792d_id,
  281. .detect = w83792d_detect,
  282. .address_list = normal_i2c,
  283. };
  284. static inline long in_count_from_reg(int nr, struct w83792d_data *data)
  285. {
  286. /* in7 and in8 do not have low bits, but the formula still works */
  287. return (data->in[nr] << 2) | ((data->low_bits >> (2 * nr)) & 0x03);
  288. }
  289. /*
  290. * The SMBus locks itself. The Winbond W83792D chip has a bank register,
  291. * but the driver only accesses registers in bank 0, so we don't have
  292. * to switch banks and lock access between switches.
  293. */
  294. static inline int w83792d_read_value(struct i2c_client *client, u8 reg)
  295. {
  296. return i2c_smbus_read_byte_data(client, reg);
  297. }
  298. static inline int
  299. w83792d_write_value(struct i2c_client *client, u8 reg, u8 value)
  300. {
  301. return i2c_smbus_write_byte_data(client, reg, value);
  302. }
  303. /* following are the sysfs callback functions */
  304. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  305. char *buf)
  306. {
  307. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  308. int nr = sensor_attr->index;
  309. struct w83792d_data *data = w83792d_update_device(dev);
  310. return sprintf(buf, "%ld\n",
  311. IN_FROM_REG(nr, in_count_from_reg(nr, data)));
  312. }
  313. #define show_in_reg(reg) \
  314. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  315. char *buf) \
  316. { \
  317. struct sensor_device_attribute *sensor_attr \
  318. = to_sensor_dev_attr(attr); \
  319. int nr = sensor_attr->index; \
  320. struct w83792d_data *data = w83792d_update_device(dev); \
  321. return sprintf(buf, "%ld\n", \
  322. (long)(IN_FROM_REG(nr, data->reg[nr]) * 4)); \
  323. }
  324. show_in_reg(in_min);
  325. show_in_reg(in_max);
  326. #define store_in_reg(REG, reg) \
  327. static ssize_t store_in_##reg(struct device *dev, \
  328. struct device_attribute *attr, \
  329. const char *buf, size_t count) \
  330. { \
  331. struct sensor_device_attribute *sensor_attr \
  332. = to_sensor_dev_attr(attr); \
  333. int nr = sensor_attr->index; \
  334. struct i2c_client *client = to_i2c_client(dev); \
  335. struct w83792d_data *data = i2c_get_clientdata(client); \
  336. unsigned long val; \
  337. int err = kstrtoul(buf, 10, &val); \
  338. if (err) \
  339. return err; \
  340. mutex_lock(&data->update_lock); \
  341. data->in_##reg[nr] = clamp_val(IN_TO_REG(nr, val) / 4, 0, 255); \
  342. w83792d_write_value(client, W83792D_REG_IN_##REG[nr], \
  343. data->in_##reg[nr]); \
  344. mutex_unlock(&data->update_lock); \
  345. \
  346. return count; \
  347. }
  348. store_in_reg(MIN, min);
  349. store_in_reg(MAX, max);
  350. #define show_fan_reg(reg) \
  351. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  352. char *buf) \
  353. { \
  354. struct sensor_device_attribute *sensor_attr \
  355. = to_sensor_dev_attr(attr); \
  356. int nr = sensor_attr->index - 1; \
  357. struct w83792d_data *data = w83792d_update_device(dev); \
  358. return sprintf(buf, "%d\n", \
  359. FAN_FROM_REG(data->reg[nr], DIV_FROM_REG(data->fan_div[nr]))); \
  360. }
  361. show_fan_reg(fan);
  362. show_fan_reg(fan_min);
  363. static ssize_t
  364. store_fan_min(struct device *dev, struct device_attribute *attr,
  365. const char *buf, size_t count)
  366. {
  367. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  368. int nr = sensor_attr->index - 1;
  369. struct i2c_client *client = to_i2c_client(dev);
  370. struct w83792d_data *data = i2c_get_clientdata(client);
  371. unsigned long val;
  372. int err;
  373. err = kstrtoul(buf, 10, &val);
  374. if (err)
  375. return err;
  376. mutex_lock(&data->update_lock);
  377. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  378. w83792d_write_value(client, W83792D_REG_FAN_MIN[nr],
  379. data->fan_min[nr]);
  380. mutex_unlock(&data->update_lock);
  381. return count;
  382. }
  383. static ssize_t
  384. show_fan_div(struct device *dev, struct device_attribute *attr,
  385. char *buf)
  386. {
  387. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  388. int nr = sensor_attr->index;
  389. struct w83792d_data *data = w83792d_update_device(dev);
  390. return sprintf(buf, "%u\n", DIV_FROM_REG(data->fan_div[nr - 1]));
  391. }
  392. /*
  393. * Note: we save and restore the fan minimum here, because its value is
  394. * determined in part by the fan divisor. This follows the principle of
  395. * least surprise; the user doesn't expect the fan minimum to change just
  396. * because the divisor changed.
  397. */
  398. static ssize_t
  399. store_fan_div(struct device *dev, struct device_attribute *attr,
  400. const char *buf, size_t count)
  401. {
  402. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  403. int nr = sensor_attr->index - 1;
  404. struct i2c_client *client = to_i2c_client(dev);
  405. struct w83792d_data *data = i2c_get_clientdata(client);
  406. unsigned long min;
  407. /*u8 reg;*/
  408. u8 fan_div_reg = 0;
  409. u8 tmp_fan_div;
  410. unsigned long val;
  411. int err;
  412. err = kstrtoul(buf, 10, &val);
  413. if (err)
  414. return err;
  415. /* Save fan_min */
  416. mutex_lock(&data->update_lock);
  417. min = FAN_FROM_REG(data->fan_min[nr],
  418. DIV_FROM_REG(data->fan_div[nr]));
  419. data->fan_div[nr] = DIV_TO_REG(val);
  420. fan_div_reg = w83792d_read_value(client, W83792D_REG_FAN_DIV[nr >> 1]);
  421. fan_div_reg &= (nr & 0x01) ? 0x8f : 0xf8;
  422. tmp_fan_div = (nr & 0x01) ? (((data->fan_div[nr]) << 4) & 0x70)
  423. : ((data->fan_div[nr]) & 0x07);
  424. w83792d_write_value(client, W83792D_REG_FAN_DIV[nr >> 1],
  425. fan_div_reg | tmp_fan_div);
  426. /* Restore fan_min */
  427. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  428. w83792d_write_value(client, W83792D_REG_FAN_MIN[nr], data->fan_min[nr]);
  429. mutex_unlock(&data->update_lock);
  430. return count;
  431. }
  432. /* read/write the temperature1, includes measured value and limits */
  433. static ssize_t show_temp1(struct device *dev, struct device_attribute *attr,
  434. char *buf)
  435. {
  436. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  437. int nr = sensor_attr->index;
  438. struct w83792d_data *data = w83792d_update_device(dev);
  439. return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp1[nr]));
  440. }
  441. static ssize_t store_temp1(struct device *dev, struct device_attribute *attr,
  442. const char *buf, size_t count)
  443. {
  444. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  445. int nr = sensor_attr->index;
  446. struct i2c_client *client = to_i2c_client(dev);
  447. struct w83792d_data *data = i2c_get_clientdata(client);
  448. long val;
  449. int err;
  450. err = kstrtol(buf, 10, &val);
  451. if (err)
  452. return err;
  453. mutex_lock(&data->update_lock);
  454. data->temp1[nr] = TEMP1_TO_REG(val);
  455. w83792d_write_value(client, W83792D_REG_TEMP1[nr],
  456. data->temp1[nr]);
  457. mutex_unlock(&data->update_lock);
  458. return count;
  459. }
  460. /* read/write the temperature2-3, includes measured value and limits */
  461. static ssize_t show_temp23(struct device *dev, struct device_attribute *attr,
  462. char *buf)
  463. {
  464. struct sensor_device_attribute_2 *sensor_attr
  465. = to_sensor_dev_attr_2(attr);
  466. int nr = sensor_attr->nr;
  467. int index = sensor_attr->index;
  468. struct w83792d_data *data = w83792d_update_device(dev);
  469. return sprintf(buf, "%ld\n",
  470. (long)TEMP_ADD_FROM_REG(data->temp_add[nr][index],
  471. data->temp_add[nr][index+1]));
  472. }
  473. static ssize_t store_temp23(struct device *dev, struct device_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. struct sensor_device_attribute_2 *sensor_attr
  477. = to_sensor_dev_attr_2(attr);
  478. int nr = sensor_attr->nr;
  479. int index = sensor_attr->index;
  480. struct i2c_client *client = to_i2c_client(dev);
  481. struct w83792d_data *data = i2c_get_clientdata(client);
  482. long val;
  483. int err;
  484. err = kstrtol(buf, 10, &val);
  485. if (err)
  486. return err;
  487. mutex_lock(&data->update_lock);
  488. data->temp_add[nr][index] = TEMP_ADD_TO_REG_HIGH(val);
  489. data->temp_add[nr][index+1] = TEMP_ADD_TO_REG_LOW(val);
  490. w83792d_write_value(client, W83792D_REG_TEMP_ADD[nr][index],
  491. data->temp_add[nr][index]);
  492. w83792d_write_value(client, W83792D_REG_TEMP_ADD[nr][index+1],
  493. data->temp_add[nr][index+1]);
  494. mutex_unlock(&data->update_lock);
  495. return count;
  496. }
  497. /* get realtime status of all sensors items: voltage, temp, fan */
  498. static ssize_t
  499. alarms_show(struct device *dev, struct device_attribute *attr, char *buf)
  500. {
  501. struct w83792d_data *data = w83792d_update_device(dev);
  502. return sprintf(buf, "%d\n", data->alarms);
  503. }
  504. static ssize_t show_alarm(struct device *dev,
  505. struct device_attribute *attr, char *buf)
  506. {
  507. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  508. int nr = sensor_attr->index;
  509. struct w83792d_data *data = w83792d_update_device(dev);
  510. return sprintf(buf, "%d\n", (data->alarms >> nr) & 1);
  511. }
  512. static ssize_t
  513. show_pwm(struct device *dev, struct device_attribute *attr,
  514. char *buf)
  515. {
  516. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  517. int nr = sensor_attr->index;
  518. struct w83792d_data *data = w83792d_update_device(dev);
  519. return sprintf(buf, "%d\n", (data->pwm[nr] & 0x0f) << 4);
  520. }
  521. static ssize_t
  522. show_pwmenable(struct device *dev, struct device_attribute *attr,
  523. char *buf)
  524. {
  525. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  526. int nr = sensor_attr->index - 1;
  527. struct w83792d_data *data = w83792d_update_device(dev);
  528. long pwm_enable_tmp = 1;
  529. switch (data->pwmenable[nr]) {
  530. case 0:
  531. pwm_enable_tmp = 1; /* manual mode */
  532. break;
  533. case 1:
  534. pwm_enable_tmp = 3; /*thermal cruise/Smart Fan I */
  535. break;
  536. case 2:
  537. pwm_enable_tmp = 2; /* Smart Fan II */
  538. break;
  539. }
  540. return sprintf(buf, "%ld\n", pwm_enable_tmp);
  541. }
  542. static ssize_t
  543. store_pwm(struct device *dev, struct device_attribute *attr,
  544. const char *buf, size_t count)
  545. {
  546. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  547. int nr = sensor_attr->index;
  548. struct i2c_client *client = to_i2c_client(dev);
  549. struct w83792d_data *data = i2c_get_clientdata(client);
  550. unsigned long val;
  551. int err;
  552. err = kstrtoul(buf, 10, &val);
  553. if (err)
  554. return err;
  555. val = clamp_val(val, 0, 255) >> 4;
  556. mutex_lock(&data->update_lock);
  557. val |= w83792d_read_value(client, W83792D_REG_PWM[nr]) & 0xf0;
  558. data->pwm[nr] = val;
  559. w83792d_write_value(client, W83792D_REG_PWM[nr], data->pwm[nr]);
  560. mutex_unlock(&data->update_lock);
  561. return count;
  562. }
  563. static ssize_t
  564. store_pwmenable(struct device *dev, struct device_attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  568. int nr = sensor_attr->index - 1;
  569. struct i2c_client *client = to_i2c_client(dev);
  570. struct w83792d_data *data = i2c_get_clientdata(client);
  571. u8 fan_cfg_tmp, cfg1_tmp, cfg2_tmp, cfg3_tmp, cfg4_tmp;
  572. unsigned long val;
  573. int err;
  574. err = kstrtoul(buf, 10, &val);
  575. if (err)
  576. return err;
  577. if (val < 1 || val > 3)
  578. return -EINVAL;
  579. mutex_lock(&data->update_lock);
  580. switch (val) {
  581. case 1:
  582. data->pwmenable[nr] = 0; /* manual mode */
  583. break;
  584. case 2:
  585. data->pwmenable[nr] = 2; /* Smart Fan II */
  586. break;
  587. case 3:
  588. data->pwmenable[nr] = 1; /* thermal cruise/Smart Fan I */
  589. break;
  590. }
  591. cfg1_tmp = data->pwmenable[0];
  592. cfg2_tmp = (data->pwmenable[1]) << 2;
  593. cfg3_tmp = (data->pwmenable[2]) << 4;
  594. cfg4_tmp = w83792d_read_value(client, W83792D_REG_FAN_CFG) & 0xc0;
  595. fan_cfg_tmp = ((cfg4_tmp | cfg3_tmp) | cfg2_tmp) | cfg1_tmp;
  596. w83792d_write_value(client, W83792D_REG_FAN_CFG, fan_cfg_tmp);
  597. mutex_unlock(&data->update_lock);
  598. return count;
  599. }
  600. static ssize_t
  601. show_pwm_mode(struct device *dev, struct device_attribute *attr,
  602. char *buf)
  603. {
  604. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  605. int nr = sensor_attr->index;
  606. struct w83792d_data *data = w83792d_update_device(dev);
  607. return sprintf(buf, "%d\n", data->pwm[nr] >> 7);
  608. }
  609. static ssize_t
  610. store_pwm_mode(struct device *dev, struct device_attribute *attr,
  611. const char *buf, size_t count)
  612. {
  613. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  614. int nr = sensor_attr->index;
  615. struct i2c_client *client = to_i2c_client(dev);
  616. struct w83792d_data *data = i2c_get_clientdata(client);
  617. unsigned long val;
  618. int err;
  619. err = kstrtoul(buf, 10, &val);
  620. if (err)
  621. return err;
  622. if (val > 1)
  623. return -EINVAL;
  624. mutex_lock(&data->update_lock);
  625. data->pwm[nr] = w83792d_read_value(client, W83792D_REG_PWM[nr]);
  626. if (val) { /* PWM mode */
  627. data->pwm[nr] |= 0x80;
  628. } else { /* DC mode */
  629. data->pwm[nr] &= 0x7f;
  630. }
  631. w83792d_write_value(client, W83792D_REG_PWM[nr], data->pwm[nr]);
  632. mutex_unlock(&data->update_lock);
  633. return count;
  634. }
  635. static ssize_t
  636. intrusion0_alarm_show(struct device *dev, struct device_attribute *attr,
  637. char *buf)
  638. {
  639. struct w83792d_data *data = w83792d_update_device(dev);
  640. return sprintf(buf, "%d\n", data->chassis);
  641. }
  642. static ssize_t
  643. intrusion0_alarm_store(struct device *dev, struct device_attribute *attr,
  644. const char *buf, size_t count)
  645. {
  646. struct i2c_client *client = to_i2c_client(dev);
  647. struct w83792d_data *data = i2c_get_clientdata(client);
  648. unsigned long val;
  649. u8 reg;
  650. if (kstrtoul(buf, 10, &val) || val != 0)
  651. return -EINVAL;
  652. mutex_lock(&data->update_lock);
  653. reg = w83792d_read_value(client, W83792D_REG_CHASSIS_CLR);
  654. w83792d_write_value(client, W83792D_REG_CHASSIS_CLR, reg | 0x80);
  655. data->valid = false; /* Force cache refresh */
  656. mutex_unlock(&data->update_lock);
  657. return count;
  658. }
  659. /* For Smart Fan I / Thermal Cruise */
  660. static ssize_t
  661. show_thermal_cruise(struct device *dev, struct device_attribute *attr,
  662. char *buf)
  663. {
  664. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  665. int nr = sensor_attr->index;
  666. struct w83792d_data *data = w83792d_update_device(dev);
  667. return sprintf(buf, "%ld\n", (long)data->thermal_cruise[nr-1]);
  668. }
  669. static ssize_t
  670. store_thermal_cruise(struct device *dev, struct device_attribute *attr,
  671. const char *buf, size_t count)
  672. {
  673. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  674. int nr = sensor_attr->index - 1;
  675. struct i2c_client *client = to_i2c_client(dev);
  676. struct w83792d_data *data = i2c_get_clientdata(client);
  677. u8 target_tmp = 0, target_mask = 0;
  678. unsigned long val;
  679. int err;
  680. err = kstrtoul(buf, 10, &val);
  681. if (err)
  682. return err;
  683. target_tmp = val;
  684. target_tmp = target_tmp & 0x7f;
  685. mutex_lock(&data->update_lock);
  686. target_mask = w83792d_read_value(client,
  687. W83792D_REG_THERMAL[nr]) & 0x80;
  688. data->thermal_cruise[nr] = clamp_val(target_tmp, 0, 255);
  689. w83792d_write_value(client, W83792D_REG_THERMAL[nr],
  690. (data->thermal_cruise[nr]) | target_mask);
  691. mutex_unlock(&data->update_lock);
  692. return count;
  693. }
  694. /* For Smart Fan I/Thermal Cruise and Smart Fan II */
  695. static ssize_t
  696. show_tolerance(struct device *dev, struct device_attribute *attr,
  697. char *buf)
  698. {
  699. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  700. int nr = sensor_attr->index;
  701. struct w83792d_data *data = w83792d_update_device(dev);
  702. return sprintf(buf, "%ld\n", (long)data->tolerance[nr-1]);
  703. }
  704. static ssize_t
  705. store_tolerance(struct device *dev, struct device_attribute *attr,
  706. const char *buf, size_t count)
  707. {
  708. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  709. int nr = sensor_attr->index - 1;
  710. struct i2c_client *client = to_i2c_client(dev);
  711. struct w83792d_data *data = i2c_get_clientdata(client);
  712. u8 tol_tmp, tol_mask;
  713. unsigned long val;
  714. int err;
  715. err = kstrtoul(buf, 10, &val);
  716. if (err)
  717. return err;
  718. mutex_lock(&data->update_lock);
  719. tol_mask = w83792d_read_value(client,
  720. W83792D_REG_TOLERANCE[nr]) & ((nr == 1) ? 0x0f : 0xf0);
  721. tol_tmp = clamp_val(val, 0, 15);
  722. tol_tmp &= 0x0f;
  723. data->tolerance[nr] = tol_tmp;
  724. if (nr == 1)
  725. tol_tmp <<= 4;
  726. w83792d_write_value(client, W83792D_REG_TOLERANCE[nr],
  727. tol_mask | tol_tmp);
  728. mutex_unlock(&data->update_lock);
  729. return count;
  730. }
  731. /* For Smart Fan II */
  732. static ssize_t
  733. show_sf2_point(struct device *dev, struct device_attribute *attr,
  734. char *buf)
  735. {
  736. struct sensor_device_attribute_2 *sensor_attr
  737. = to_sensor_dev_attr_2(attr);
  738. int nr = sensor_attr->nr;
  739. int index = sensor_attr->index;
  740. struct w83792d_data *data = w83792d_update_device(dev);
  741. return sprintf(buf, "%ld\n", (long)data->sf2_points[index-1][nr-1]);
  742. }
  743. static ssize_t
  744. store_sf2_point(struct device *dev, struct device_attribute *attr,
  745. const char *buf, size_t count)
  746. {
  747. struct sensor_device_attribute_2 *sensor_attr
  748. = to_sensor_dev_attr_2(attr);
  749. int nr = sensor_attr->nr - 1;
  750. int index = sensor_attr->index - 1;
  751. struct i2c_client *client = to_i2c_client(dev);
  752. struct w83792d_data *data = i2c_get_clientdata(client);
  753. u8 mask_tmp = 0;
  754. unsigned long val;
  755. int err;
  756. err = kstrtoul(buf, 10, &val);
  757. if (err)
  758. return err;
  759. mutex_lock(&data->update_lock);
  760. data->sf2_points[index][nr] = clamp_val(val, 0, 127);
  761. mask_tmp = w83792d_read_value(client,
  762. W83792D_REG_POINTS[index][nr]) & 0x80;
  763. w83792d_write_value(client, W83792D_REG_POINTS[index][nr],
  764. mask_tmp|data->sf2_points[index][nr]);
  765. mutex_unlock(&data->update_lock);
  766. return count;
  767. }
  768. static ssize_t
  769. show_sf2_level(struct device *dev, struct device_attribute *attr,
  770. char *buf)
  771. {
  772. struct sensor_device_attribute_2 *sensor_attr
  773. = to_sensor_dev_attr_2(attr);
  774. int nr = sensor_attr->nr;
  775. int index = sensor_attr->index;
  776. struct w83792d_data *data = w83792d_update_device(dev);
  777. return sprintf(buf, "%d\n",
  778. (((data->sf2_levels[index-1][nr]) * 100) / 15));
  779. }
  780. static ssize_t
  781. store_sf2_level(struct device *dev, struct device_attribute *attr,
  782. const char *buf, size_t count)
  783. {
  784. struct sensor_device_attribute_2 *sensor_attr
  785. = to_sensor_dev_attr_2(attr);
  786. int nr = sensor_attr->nr;
  787. int index = sensor_attr->index - 1;
  788. struct i2c_client *client = to_i2c_client(dev);
  789. struct w83792d_data *data = i2c_get_clientdata(client);
  790. u8 mask_tmp = 0, level_tmp = 0;
  791. unsigned long val;
  792. int err;
  793. err = kstrtoul(buf, 10, &val);
  794. if (err)
  795. return err;
  796. mutex_lock(&data->update_lock);
  797. data->sf2_levels[index][nr] = clamp_val((val * 15) / 100, 0, 15);
  798. mask_tmp = w83792d_read_value(client, W83792D_REG_LEVELS[index][nr])
  799. & ((nr == 3) ? 0xf0 : 0x0f);
  800. if (nr == 3)
  801. level_tmp = data->sf2_levels[index][nr];
  802. else
  803. level_tmp = data->sf2_levels[index][nr] << 4;
  804. w83792d_write_value(client, W83792D_REG_LEVELS[index][nr],
  805. level_tmp | mask_tmp);
  806. mutex_unlock(&data->update_lock);
  807. return count;
  808. }
  809. static int
  810. w83792d_detect_subclients(struct i2c_client *new_client)
  811. {
  812. int i, id;
  813. int address = new_client->addr;
  814. u8 val;
  815. struct i2c_adapter *adapter = new_client->adapter;
  816. id = i2c_adapter_id(adapter);
  817. if (force_subclients[0] == id && force_subclients[1] == address) {
  818. for (i = 2; i <= 3; i++) {
  819. if (force_subclients[i] < 0x48 ||
  820. force_subclients[i] > 0x4f) {
  821. dev_err(&new_client->dev,
  822. "invalid subclient address %d; must be 0x48-0x4f\n",
  823. force_subclients[i]);
  824. return -ENODEV;
  825. }
  826. }
  827. w83792d_write_value(new_client, W83792D_REG_I2C_SUBADDR,
  828. (force_subclients[2] & 0x07) |
  829. ((force_subclients[3] & 0x07) << 4));
  830. }
  831. val = w83792d_read_value(new_client, W83792D_REG_I2C_SUBADDR);
  832. if (!(val & 0x88) && (val & 0x7) == ((val >> 4) & 0x7)) {
  833. dev_err(&new_client->dev,
  834. "duplicate addresses 0x%x, use force_subclient\n", 0x48 + (val & 0x7));
  835. return -ENODEV;
  836. }
  837. if (!(val & 0x08))
  838. devm_i2c_new_dummy_device(&new_client->dev, adapter, 0x48 + (val & 0x7));
  839. if (!(val & 0x80))
  840. devm_i2c_new_dummy_device(&new_client->dev, adapter, 0x48 + ((val >> 4) & 0x7));
  841. return 0;
  842. }
  843. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in, NULL, 0);
  844. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  845. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  846. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  847. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
  848. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
  849. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
  850. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
  851. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
  852. static SENSOR_DEVICE_ATTR(in0_min, S_IWUSR | S_IRUGO,
  853. show_in_min, store_in_min, 0);
  854. static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO,
  855. show_in_min, store_in_min, 1);
  856. static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
  857. show_in_min, store_in_min, 2);
  858. static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
  859. show_in_min, store_in_min, 3);
  860. static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
  861. show_in_min, store_in_min, 4);
  862. static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
  863. show_in_min, store_in_min, 5);
  864. static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
  865. show_in_min, store_in_min, 6);
  866. static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO,
  867. show_in_min, store_in_min, 7);
  868. static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO,
  869. show_in_min, store_in_min, 8);
  870. static SENSOR_DEVICE_ATTR(in0_max, S_IWUSR | S_IRUGO,
  871. show_in_max, store_in_max, 0);
  872. static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
  873. show_in_max, store_in_max, 1);
  874. static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
  875. show_in_max, store_in_max, 2);
  876. static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
  877. show_in_max, store_in_max, 3);
  878. static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
  879. show_in_max, store_in_max, 4);
  880. static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
  881. show_in_max, store_in_max, 5);
  882. static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
  883. show_in_max, store_in_max, 6);
  884. static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO,
  885. show_in_max, store_in_max, 7);
  886. static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO,
  887. show_in_max, store_in_max, 8);
  888. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp1, NULL, 0, 0);
  889. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp23, NULL, 0, 0);
  890. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp23, NULL, 1, 0);
  891. static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR,
  892. show_temp1, store_temp1, 0, 1);
  893. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp23,
  894. store_temp23, 0, 2);
  895. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp23,
  896. store_temp23, 1, 2);
  897. static SENSOR_DEVICE_ATTR_2(temp1_max_hyst, S_IRUGO | S_IWUSR,
  898. show_temp1, store_temp1, 0, 2);
  899. static SENSOR_DEVICE_ATTR_2(temp2_max_hyst, S_IRUGO | S_IWUSR,
  900. show_temp23, store_temp23, 0, 4);
  901. static SENSOR_DEVICE_ATTR_2(temp3_max_hyst, S_IRUGO | S_IWUSR,
  902. show_temp23, store_temp23, 1, 4);
  903. static DEVICE_ATTR_RO(alarms);
  904. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  905. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  906. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 2);
  907. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 3);
  908. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 4);
  909. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 5);
  910. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 6);
  911. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 7);
  912. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 8);
  913. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 9);
  914. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 10);
  915. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 11);
  916. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 12);
  917. static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 15);
  918. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 19);
  919. static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 20);
  920. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 21);
  921. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 22);
  922. static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 23);
  923. static DEVICE_ATTR_RW(intrusion0_alarm);
  924. static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
  925. static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
  926. static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2);
  927. static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3);
  928. static SENSOR_DEVICE_ATTR(pwm5, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 4);
  929. static SENSOR_DEVICE_ATTR(pwm6, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 5);
  930. static SENSOR_DEVICE_ATTR(pwm7, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 6);
  931. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
  932. show_pwmenable, store_pwmenable, 1);
  933. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
  934. show_pwmenable, store_pwmenable, 2);
  935. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO,
  936. show_pwmenable, store_pwmenable, 3);
  937. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IWUSR | S_IRUGO,
  938. show_pwm_mode, store_pwm_mode, 0);
  939. static SENSOR_DEVICE_ATTR(pwm2_mode, S_IWUSR | S_IRUGO,
  940. show_pwm_mode, store_pwm_mode, 1);
  941. static SENSOR_DEVICE_ATTR(pwm3_mode, S_IWUSR | S_IRUGO,
  942. show_pwm_mode, store_pwm_mode, 2);
  943. static SENSOR_DEVICE_ATTR(pwm4_mode, S_IWUSR | S_IRUGO,
  944. show_pwm_mode, store_pwm_mode, 3);
  945. static SENSOR_DEVICE_ATTR(pwm5_mode, S_IWUSR | S_IRUGO,
  946. show_pwm_mode, store_pwm_mode, 4);
  947. static SENSOR_DEVICE_ATTR(pwm6_mode, S_IWUSR | S_IRUGO,
  948. show_pwm_mode, store_pwm_mode, 5);
  949. static SENSOR_DEVICE_ATTR(pwm7_mode, S_IWUSR | S_IRUGO,
  950. show_pwm_mode, store_pwm_mode, 6);
  951. static SENSOR_DEVICE_ATTR(tolerance1, S_IWUSR | S_IRUGO,
  952. show_tolerance, store_tolerance, 1);
  953. static SENSOR_DEVICE_ATTR(tolerance2, S_IWUSR | S_IRUGO,
  954. show_tolerance, store_tolerance, 2);
  955. static SENSOR_DEVICE_ATTR(tolerance3, S_IWUSR | S_IRUGO,
  956. show_tolerance, store_tolerance, 3);
  957. static SENSOR_DEVICE_ATTR(thermal_cruise1, S_IWUSR | S_IRUGO,
  958. show_thermal_cruise, store_thermal_cruise, 1);
  959. static SENSOR_DEVICE_ATTR(thermal_cruise2, S_IWUSR | S_IRUGO,
  960. show_thermal_cruise, store_thermal_cruise, 2);
  961. static SENSOR_DEVICE_ATTR(thermal_cruise3, S_IWUSR | S_IRUGO,
  962. show_thermal_cruise, store_thermal_cruise, 3);
  963. static SENSOR_DEVICE_ATTR_2(sf2_point1_fan1, S_IRUGO | S_IWUSR,
  964. show_sf2_point, store_sf2_point, 1, 1);
  965. static SENSOR_DEVICE_ATTR_2(sf2_point2_fan1, S_IRUGO | S_IWUSR,
  966. show_sf2_point, store_sf2_point, 2, 1);
  967. static SENSOR_DEVICE_ATTR_2(sf2_point3_fan1, S_IRUGO | S_IWUSR,
  968. show_sf2_point, store_sf2_point, 3, 1);
  969. static SENSOR_DEVICE_ATTR_2(sf2_point4_fan1, S_IRUGO | S_IWUSR,
  970. show_sf2_point, store_sf2_point, 4, 1);
  971. static SENSOR_DEVICE_ATTR_2(sf2_point1_fan2, S_IRUGO | S_IWUSR,
  972. show_sf2_point, store_sf2_point, 1, 2);
  973. static SENSOR_DEVICE_ATTR_2(sf2_point2_fan2, S_IRUGO | S_IWUSR,
  974. show_sf2_point, store_sf2_point, 2, 2);
  975. static SENSOR_DEVICE_ATTR_2(sf2_point3_fan2, S_IRUGO | S_IWUSR,
  976. show_sf2_point, store_sf2_point, 3, 2);
  977. static SENSOR_DEVICE_ATTR_2(sf2_point4_fan2, S_IRUGO | S_IWUSR,
  978. show_sf2_point, store_sf2_point, 4, 2);
  979. static SENSOR_DEVICE_ATTR_2(sf2_point1_fan3, S_IRUGO | S_IWUSR,
  980. show_sf2_point, store_sf2_point, 1, 3);
  981. static SENSOR_DEVICE_ATTR_2(sf2_point2_fan3, S_IRUGO | S_IWUSR,
  982. show_sf2_point, store_sf2_point, 2, 3);
  983. static SENSOR_DEVICE_ATTR_2(sf2_point3_fan3, S_IRUGO | S_IWUSR,
  984. show_sf2_point, store_sf2_point, 3, 3);
  985. static SENSOR_DEVICE_ATTR_2(sf2_point4_fan3, S_IRUGO | S_IWUSR,
  986. show_sf2_point, store_sf2_point, 4, 3);
  987. static SENSOR_DEVICE_ATTR_2(sf2_level1_fan1, S_IRUGO | S_IWUSR,
  988. show_sf2_level, store_sf2_level, 1, 1);
  989. static SENSOR_DEVICE_ATTR_2(sf2_level2_fan1, S_IRUGO | S_IWUSR,
  990. show_sf2_level, store_sf2_level, 2, 1);
  991. static SENSOR_DEVICE_ATTR_2(sf2_level3_fan1, S_IRUGO | S_IWUSR,
  992. show_sf2_level, store_sf2_level, 3, 1);
  993. static SENSOR_DEVICE_ATTR_2(sf2_level1_fan2, S_IRUGO | S_IWUSR,
  994. show_sf2_level, store_sf2_level, 1, 2);
  995. static SENSOR_DEVICE_ATTR_2(sf2_level2_fan2, S_IRUGO | S_IWUSR,
  996. show_sf2_level, store_sf2_level, 2, 2);
  997. static SENSOR_DEVICE_ATTR_2(sf2_level3_fan2, S_IRUGO | S_IWUSR,
  998. show_sf2_level, store_sf2_level, 3, 2);
  999. static SENSOR_DEVICE_ATTR_2(sf2_level1_fan3, S_IRUGO | S_IWUSR,
  1000. show_sf2_level, store_sf2_level, 1, 3);
  1001. static SENSOR_DEVICE_ATTR_2(sf2_level2_fan3, S_IRUGO | S_IWUSR,
  1002. show_sf2_level, store_sf2_level, 2, 3);
  1003. static SENSOR_DEVICE_ATTR_2(sf2_level3_fan3, S_IRUGO | S_IWUSR,
  1004. show_sf2_level, store_sf2_level, 3, 3);
  1005. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 1);
  1006. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 2);
  1007. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 3);
  1008. static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 4);
  1009. static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 5);
  1010. static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan, NULL, 6);
  1011. static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan, NULL, 7);
  1012. static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
  1013. show_fan_min, store_fan_min, 1);
  1014. static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
  1015. show_fan_min, store_fan_min, 2);
  1016. static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
  1017. show_fan_min, store_fan_min, 3);
  1018. static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
  1019. show_fan_min, store_fan_min, 4);
  1020. static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO,
  1021. show_fan_min, store_fan_min, 5);
  1022. static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO,
  1023. show_fan_min, store_fan_min, 6);
  1024. static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO,
  1025. show_fan_min, store_fan_min, 7);
  1026. static SENSOR_DEVICE_ATTR(fan1_div, S_IWUSR | S_IRUGO,
  1027. show_fan_div, store_fan_div, 1);
  1028. static SENSOR_DEVICE_ATTR(fan2_div, S_IWUSR | S_IRUGO,
  1029. show_fan_div, store_fan_div, 2);
  1030. static SENSOR_DEVICE_ATTR(fan3_div, S_IWUSR | S_IRUGO,
  1031. show_fan_div, store_fan_div, 3);
  1032. static SENSOR_DEVICE_ATTR(fan4_div, S_IWUSR | S_IRUGO,
  1033. show_fan_div, store_fan_div, 4);
  1034. static SENSOR_DEVICE_ATTR(fan5_div, S_IWUSR | S_IRUGO,
  1035. show_fan_div, store_fan_div, 5);
  1036. static SENSOR_DEVICE_ATTR(fan6_div, S_IWUSR | S_IRUGO,
  1037. show_fan_div, store_fan_div, 6);
  1038. static SENSOR_DEVICE_ATTR(fan7_div, S_IWUSR | S_IRUGO,
  1039. show_fan_div, store_fan_div, 7);
  1040. static struct attribute *w83792d_attributes_fan[4][7] = {
  1041. {
  1042. &sensor_dev_attr_fan4_input.dev_attr.attr,
  1043. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1044. &sensor_dev_attr_fan4_div.dev_attr.attr,
  1045. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1046. &sensor_dev_attr_pwm4.dev_attr.attr,
  1047. &sensor_dev_attr_pwm4_mode.dev_attr.attr,
  1048. NULL
  1049. }, {
  1050. &sensor_dev_attr_fan5_input.dev_attr.attr,
  1051. &sensor_dev_attr_fan5_min.dev_attr.attr,
  1052. &sensor_dev_attr_fan5_div.dev_attr.attr,
  1053. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1054. &sensor_dev_attr_pwm5.dev_attr.attr,
  1055. &sensor_dev_attr_pwm5_mode.dev_attr.attr,
  1056. NULL
  1057. }, {
  1058. &sensor_dev_attr_fan6_input.dev_attr.attr,
  1059. &sensor_dev_attr_fan6_min.dev_attr.attr,
  1060. &sensor_dev_attr_fan6_div.dev_attr.attr,
  1061. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  1062. &sensor_dev_attr_pwm6.dev_attr.attr,
  1063. &sensor_dev_attr_pwm6_mode.dev_attr.attr,
  1064. NULL
  1065. }, {
  1066. &sensor_dev_attr_fan7_input.dev_attr.attr,
  1067. &sensor_dev_attr_fan7_min.dev_attr.attr,
  1068. &sensor_dev_attr_fan7_div.dev_attr.attr,
  1069. &sensor_dev_attr_fan7_alarm.dev_attr.attr,
  1070. &sensor_dev_attr_pwm7.dev_attr.attr,
  1071. &sensor_dev_attr_pwm7_mode.dev_attr.attr,
  1072. NULL
  1073. }
  1074. };
  1075. static const struct attribute_group w83792d_group_fan[4] = {
  1076. { .attrs = w83792d_attributes_fan[0] },
  1077. { .attrs = w83792d_attributes_fan[1] },
  1078. { .attrs = w83792d_attributes_fan[2] },
  1079. { .attrs = w83792d_attributes_fan[3] },
  1080. };
  1081. static struct attribute *w83792d_attributes[] = {
  1082. &sensor_dev_attr_in0_input.dev_attr.attr,
  1083. &sensor_dev_attr_in0_max.dev_attr.attr,
  1084. &sensor_dev_attr_in0_min.dev_attr.attr,
  1085. &sensor_dev_attr_in1_input.dev_attr.attr,
  1086. &sensor_dev_attr_in1_max.dev_attr.attr,
  1087. &sensor_dev_attr_in1_min.dev_attr.attr,
  1088. &sensor_dev_attr_in2_input.dev_attr.attr,
  1089. &sensor_dev_attr_in2_max.dev_attr.attr,
  1090. &sensor_dev_attr_in2_min.dev_attr.attr,
  1091. &sensor_dev_attr_in3_input.dev_attr.attr,
  1092. &sensor_dev_attr_in3_max.dev_attr.attr,
  1093. &sensor_dev_attr_in3_min.dev_attr.attr,
  1094. &sensor_dev_attr_in4_input.dev_attr.attr,
  1095. &sensor_dev_attr_in4_max.dev_attr.attr,
  1096. &sensor_dev_attr_in4_min.dev_attr.attr,
  1097. &sensor_dev_attr_in5_input.dev_attr.attr,
  1098. &sensor_dev_attr_in5_max.dev_attr.attr,
  1099. &sensor_dev_attr_in5_min.dev_attr.attr,
  1100. &sensor_dev_attr_in6_input.dev_attr.attr,
  1101. &sensor_dev_attr_in6_max.dev_attr.attr,
  1102. &sensor_dev_attr_in6_min.dev_attr.attr,
  1103. &sensor_dev_attr_in7_input.dev_attr.attr,
  1104. &sensor_dev_attr_in7_max.dev_attr.attr,
  1105. &sensor_dev_attr_in7_min.dev_attr.attr,
  1106. &sensor_dev_attr_in8_input.dev_attr.attr,
  1107. &sensor_dev_attr_in8_max.dev_attr.attr,
  1108. &sensor_dev_attr_in8_min.dev_attr.attr,
  1109. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1110. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1111. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1112. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1113. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1114. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1115. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1116. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1117. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  1118. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1119. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1120. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  1121. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1122. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1123. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  1124. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1125. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1126. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  1127. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1128. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1129. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1130. &sensor_dev_attr_pwm1.dev_attr.attr,
  1131. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  1132. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1133. &sensor_dev_attr_pwm2.dev_attr.attr,
  1134. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  1135. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1136. &sensor_dev_attr_pwm3.dev_attr.attr,
  1137. &sensor_dev_attr_pwm3_mode.dev_attr.attr,
  1138. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1139. &dev_attr_alarms.attr,
  1140. &dev_attr_intrusion0_alarm.attr,
  1141. &sensor_dev_attr_tolerance1.dev_attr.attr,
  1142. &sensor_dev_attr_thermal_cruise1.dev_attr.attr,
  1143. &sensor_dev_attr_tolerance2.dev_attr.attr,
  1144. &sensor_dev_attr_thermal_cruise2.dev_attr.attr,
  1145. &sensor_dev_attr_tolerance3.dev_attr.attr,
  1146. &sensor_dev_attr_thermal_cruise3.dev_attr.attr,
  1147. &sensor_dev_attr_sf2_point1_fan1.dev_attr.attr,
  1148. &sensor_dev_attr_sf2_point2_fan1.dev_attr.attr,
  1149. &sensor_dev_attr_sf2_point3_fan1.dev_attr.attr,
  1150. &sensor_dev_attr_sf2_point4_fan1.dev_attr.attr,
  1151. &sensor_dev_attr_sf2_point1_fan2.dev_attr.attr,
  1152. &sensor_dev_attr_sf2_point2_fan2.dev_attr.attr,
  1153. &sensor_dev_attr_sf2_point3_fan2.dev_attr.attr,
  1154. &sensor_dev_attr_sf2_point4_fan2.dev_attr.attr,
  1155. &sensor_dev_attr_sf2_point1_fan3.dev_attr.attr,
  1156. &sensor_dev_attr_sf2_point2_fan3.dev_attr.attr,
  1157. &sensor_dev_attr_sf2_point3_fan3.dev_attr.attr,
  1158. &sensor_dev_attr_sf2_point4_fan3.dev_attr.attr,
  1159. &sensor_dev_attr_sf2_level1_fan1.dev_attr.attr,
  1160. &sensor_dev_attr_sf2_level2_fan1.dev_attr.attr,
  1161. &sensor_dev_attr_sf2_level3_fan1.dev_attr.attr,
  1162. &sensor_dev_attr_sf2_level1_fan2.dev_attr.attr,
  1163. &sensor_dev_attr_sf2_level2_fan2.dev_attr.attr,
  1164. &sensor_dev_attr_sf2_level3_fan2.dev_attr.attr,
  1165. &sensor_dev_attr_sf2_level1_fan3.dev_attr.attr,
  1166. &sensor_dev_attr_sf2_level2_fan3.dev_attr.attr,
  1167. &sensor_dev_attr_sf2_level3_fan3.dev_attr.attr,
  1168. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1169. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1170. &sensor_dev_attr_fan1_div.dev_attr.attr,
  1171. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1172. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1173. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1174. &sensor_dev_attr_fan2_div.dev_attr.attr,
  1175. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1176. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1177. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1178. &sensor_dev_attr_fan3_div.dev_attr.attr,
  1179. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1180. NULL
  1181. };
  1182. static const struct attribute_group w83792d_group = {
  1183. .attrs = w83792d_attributes,
  1184. };
  1185. /* Return 0 if detection is successful, -ENODEV otherwise */
  1186. static int
  1187. w83792d_detect(struct i2c_client *client, struct i2c_board_info *info)
  1188. {
  1189. struct i2c_adapter *adapter = client->adapter;
  1190. int val1, val2;
  1191. unsigned short address = client->addr;
  1192. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1193. return -ENODEV;
  1194. if (w83792d_read_value(client, W83792D_REG_CONFIG) & 0x80)
  1195. return -ENODEV;
  1196. val1 = w83792d_read_value(client, W83792D_REG_BANK);
  1197. val2 = w83792d_read_value(client, W83792D_REG_CHIPMAN);
  1198. /* Check for Winbond ID if in bank 0 */
  1199. if (!(val1 & 0x07)) { /* is Bank0 */
  1200. if ((!(val1 & 0x80) && val2 != 0xa3) ||
  1201. ((val1 & 0x80) && val2 != 0x5c))
  1202. return -ENODEV;
  1203. }
  1204. /*
  1205. * If Winbond chip, address of chip and W83792D_REG_I2C_ADDR
  1206. * should match
  1207. */
  1208. if (w83792d_read_value(client, W83792D_REG_I2C_ADDR) != address)
  1209. return -ENODEV;
  1210. /* Put it now into bank 0 and Vendor ID High Byte */
  1211. w83792d_write_value(client,
  1212. W83792D_REG_BANK,
  1213. (w83792d_read_value(client,
  1214. W83792D_REG_BANK) & 0x78) | 0x80);
  1215. /* Determine the chip type. */
  1216. val1 = w83792d_read_value(client, W83792D_REG_WCHIPID);
  1217. val2 = w83792d_read_value(client, W83792D_REG_CHIPMAN);
  1218. if (val1 != 0x7a || val2 != 0x5c)
  1219. return -ENODEV;
  1220. strscpy(info->type, "w83792d", I2C_NAME_SIZE);
  1221. return 0;
  1222. }
  1223. static int
  1224. w83792d_probe(struct i2c_client *client)
  1225. {
  1226. struct w83792d_data *data;
  1227. struct device *dev = &client->dev;
  1228. int i, val1, err;
  1229. data = devm_kzalloc(dev, sizeof(struct w83792d_data), GFP_KERNEL);
  1230. if (!data)
  1231. return -ENOMEM;
  1232. i2c_set_clientdata(client, data);
  1233. mutex_init(&data->update_lock);
  1234. err = w83792d_detect_subclients(client);
  1235. if (err)
  1236. return err;
  1237. /* Initialize the chip */
  1238. w83792d_init_client(client);
  1239. /* A few vars need to be filled upon startup */
  1240. for (i = 0; i < 7; i++) {
  1241. data->fan_min[i] = w83792d_read_value(client,
  1242. W83792D_REG_FAN_MIN[i]);
  1243. }
  1244. /* Register sysfs hooks */
  1245. err = sysfs_create_group(&dev->kobj, &w83792d_group);
  1246. if (err)
  1247. return err;
  1248. /*
  1249. * Read GPIO enable register to check if pins for fan 4,5 are used as
  1250. * GPIO
  1251. */
  1252. val1 = w83792d_read_value(client, W83792D_REG_GPIO_EN);
  1253. if (!(val1 & 0x40)) {
  1254. err = sysfs_create_group(&dev->kobj, &w83792d_group_fan[0]);
  1255. if (err)
  1256. goto exit_remove_files;
  1257. }
  1258. if (!(val1 & 0x20)) {
  1259. err = sysfs_create_group(&dev->kobj, &w83792d_group_fan[1]);
  1260. if (err)
  1261. goto exit_remove_files;
  1262. }
  1263. val1 = w83792d_read_value(client, W83792D_REG_PIN);
  1264. if (val1 & 0x40) {
  1265. err = sysfs_create_group(&dev->kobj, &w83792d_group_fan[2]);
  1266. if (err)
  1267. goto exit_remove_files;
  1268. }
  1269. if (val1 & 0x04) {
  1270. err = sysfs_create_group(&dev->kobj, &w83792d_group_fan[3]);
  1271. if (err)
  1272. goto exit_remove_files;
  1273. }
  1274. data->hwmon_dev = hwmon_device_register(dev);
  1275. if (IS_ERR(data->hwmon_dev)) {
  1276. err = PTR_ERR(data->hwmon_dev);
  1277. goto exit_remove_files;
  1278. }
  1279. return 0;
  1280. exit_remove_files:
  1281. sysfs_remove_group(&dev->kobj, &w83792d_group);
  1282. for (i = 0; i < ARRAY_SIZE(w83792d_group_fan); i++)
  1283. sysfs_remove_group(&dev->kobj, &w83792d_group_fan[i]);
  1284. return err;
  1285. }
  1286. static void
  1287. w83792d_remove(struct i2c_client *client)
  1288. {
  1289. struct w83792d_data *data = i2c_get_clientdata(client);
  1290. int i;
  1291. hwmon_device_unregister(data->hwmon_dev);
  1292. sysfs_remove_group(&client->dev.kobj, &w83792d_group);
  1293. for (i = 0; i < ARRAY_SIZE(w83792d_group_fan); i++)
  1294. sysfs_remove_group(&client->dev.kobj,
  1295. &w83792d_group_fan[i]);
  1296. }
  1297. static void
  1298. w83792d_init_client(struct i2c_client *client)
  1299. {
  1300. u8 temp2_cfg, temp3_cfg, vid_in_b;
  1301. if (init)
  1302. w83792d_write_value(client, W83792D_REG_CONFIG, 0x80);
  1303. /*
  1304. * Clear the bit6 of W83792D_REG_VID_IN_B(set it into 0):
  1305. * W83792D_REG_VID_IN_B bit6 = 0: the high/low limit of
  1306. * vin0/vin1 can be modified by user;
  1307. * W83792D_REG_VID_IN_B bit6 = 1: the high/low limit of
  1308. * vin0/vin1 auto-updated, can NOT be modified by user.
  1309. */
  1310. vid_in_b = w83792d_read_value(client, W83792D_REG_VID_IN_B);
  1311. w83792d_write_value(client, W83792D_REG_VID_IN_B,
  1312. vid_in_b & 0xbf);
  1313. temp2_cfg = w83792d_read_value(client, W83792D_REG_TEMP2_CONFIG);
  1314. temp3_cfg = w83792d_read_value(client, W83792D_REG_TEMP3_CONFIG);
  1315. w83792d_write_value(client, W83792D_REG_TEMP2_CONFIG,
  1316. temp2_cfg & 0xe6);
  1317. w83792d_write_value(client, W83792D_REG_TEMP3_CONFIG,
  1318. temp3_cfg & 0xe6);
  1319. /* Start monitoring */
  1320. w83792d_write_value(client, W83792D_REG_CONFIG,
  1321. (w83792d_read_value(client,
  1322. W83792D_REG_CONFIG) & 0xf7)
  1323. | 0x01);
  1324. }
  1325. static struct w83792d_data *w83792d_update_device(struct device *dev)
  1326. {
  1327. struct i2c_client *client = to_i2c_client(dev);
  1328. struct w83792d_data *data = i2c_get_clientdata(client);
  1329. int i, j;
  1330. u8 reg_array_tmp[4], reg_tmp;
  1331. mutex_lock(&data->update_lock);
  1332. if (time_after
  1333. (jiffies - data->last_updated, (unsigned long) (HZ * 3))
  1334. || time_before(jiffies, data->last_updated) || !data->valid) {
  1335. dev_dbg(dev, "Starting device update\n");
  1336. /* Update the voltages measured value and limits */
  1337. for (i = 0; i < 9; i++) {
  1338. data->in[i] = w83792d_read_value(client,
  1339. W83792D_REG_IN[i]);
  1340. data->in_max[i] = w83792d_read_value(client,
  1341. W83792D_REG_IN_MAX[i]);
  1342. data->in_min[i] = w83792d_read_value(client,
  1343. W83792D_REG_IN_MIN[i]);
  1344. }
  1345. data->low_bits = w83792d_read_value(client,
  1346. W83792D_REG_LOW_BITS1) +
  1347. (w83792d_read_value(client,
  1348. W83792D_REG_LOW_BITS2) << 8);
  1349. for (i = 0; i < 7; i++) {
  1350. /* Update the Fan measured value and limits */
  1351. data->fan[i] = w83792d_read_value(client,
  1352. W83792D_REG_FAN[i]);
  1353. data->fan_min[i] = w83792d_read_value(client,
  1354. W83792D_REG_FAN_MIN[i]);
  1355. /* Update the PWM/DC Value and PWM/DC flag */
  1356. data->pwm[i] = w83792d_read_value(client,
  1357. W83792D_REG_PWM[i]);
  1358. }
  1359. reg_tmp = w83792d_read_value(client, W83792D_REG_FAN_CFG);
  1360. data->pwmenable[0] = reg_tmp & 0x03;
  1361. data->pwmenable[1] = (reg_tmp>>2) & 0x03;
  1362. data->pwmenable[2] = (reg_tmp>>4) & 0x03;
  1363. for (i = 0; i < 3; i++) {
  1364. data->temp1[i] = w83792d_read_value(client,
  1365. W83792D_REG_TEMP1[i]);
  1366. }
  1367. for (i = 0; i < 2; i++) {
  1368. for (j = 0; j < 6; j++) {
  1369. data->temp_add[i][j] = w83792d_read_value(
  1370. client, W83792D_REG_TEMP_ADD[i][j]);
  1371. }
  1372. }
  1373. /* Update the Fan Divisor */
  1374. for (i = 0; i < 4; i++) {
  1375. reg_array_tmp[i] = w83792d_read_value(client,
  1376. W83792D_REG_FAN_DIV[i]);
  1377. }
  1378. data->fan_div[0] = reg_array_tmp[0] & 0x07;
  1379. data->fan_div[1] = (reg_array_tmp[0] >> 4) & 0x07;
  1380. data->fan_div[2] = reg_array_tmp[1] & 0x07;
  1381. data->fan_div[3] = (reg_array_tmp[1] >> 4) & 0x07;
  1382. data->fan_div[4] = reg_array_tmp[2] & 0x07;
  1383. data->fan_div[5] = (reg_array_tmp[2] >> 4) & 0x07;
  1384. data->fan_div[6] = reg_array_tmp[3] & 0x07;
  1385. /* Update the realtime status */
  1386. data->alarms = w83792d_read_value(client, W83792D_REG_ALARM1) +
  1387. (w83792d_read_value(client, W83792D_REG_ALARM2) << 8) +
  1388. (w83792d_read_value(client, W83792D_REG_ALARM3) << 16);
  1389. /* Update CaseOpen status and it's CLR_CHS. */
  1390. data->chassis = (w83792d_read_value(client,
  1391. W83792D_REG_CHASSIS) >> 5) & 0x01;
  1392. /* Update Thermal Cruise/Smart Fan I target value */
  1393. for (i = 0; i < 3; i++) {
  1394. data->thermal_cruise[i] =
  1395. w83792d_read_value(client,
  1396. W83792D_REG_THERMAL[i]) & 0x7f;
  1397. }
  1398. /* Update Smart Fan I/II tolerance */
  1399. reg_tmp = w83792d_read_value(client, W83792D_REG_TOLERANCE[0]);
  1400. data->tolerance[0] = reg_tmp & 0x0f;
  1401. data->tolerance[1] = (reg_tmp >> 4) & 0x0f;
  1402. data->tolerance[2] = w83792d_read_value(client,
  1403. W83792D_REG_TOLERANCE[2]) & 0x0f;
  1404. /* Update Smart Fan II temperature points */
  1405. for (i = 0; i < 3; i++) {
  1406. for (j = 0; j < 4; j++) {
  1407. data->sf2_points[i][j]
  1408. = w83792d_read_value(client,
  1409. W83792D_REG_POINTS[i][j]) & 0x7f;
  1410. }
  1411. }
  1412. /* Update Smart Fan II duty cycle levels */
  1413. for (i = 0; i < 3; i++) {
  1414. reg_tmp = w83792d_read_value(client,
  1415. W83792D_REG_LEVELS[i][0]);
  1416. data->sf2_levels[i][0] = reg_tmp & 0x0f;
  1417. data->sf2_levels[i][1] = (reg_tmp >> 4) & 0x0f;
  1418. reg_tmp = w83792d_read_value(client,
  1419. W83792D_REG_LEVELS[i][2]);
  1420. data->sf2_levels[i][2] = (reg_tmp >> 4) & 0x0f;
  1421. data->sf2_levels[i][3] = reg_tmp & 0x0f;
  1422. }
  1423. data->last_updated = jiffies;
  1424. data->valid = true;
  1425. }
  1426. mutex_unlock(&data->update_lock);
  1427. #ifdef DEBUG
  1428. w83792d_print_debug(data, dev);
  1429. #endif
  1430. return data;
  1431. }
  1432. #ifdef DEBUG
  1433. static void w83792d_print_debug(struct w83792d_data *data, struct device *dev)
  1434. {
  1435. int i = 0, j = 0;
  1436. dev_dbg(dev, "==========The following is the debug message...========\n");
  1437. dev_dbg(dev, "9 set of Voltages: =====>\n");
  1438. for (i = 0; i < 9; i++) {
  1439. dev_dbg(dev, "vin[%d] is: 0x%x\n", i, data->in[i]);
  1440. dev_dbg(dev, "vin[%d] max is: 0x%x\n", i, data->in_max[i]);
  1441. dev_dbg(dev, "vin[%d] min is: 0x%x\n", i, data->in_min[i]);
  1442. }
  1443. dev_dbg(dev, "Low Bit1 is: 0x%x\n", data->low_bits & 0xff);
  1444. dev_dbg(dev, "Low Bit2 is: 0x%x\n", data->low_bits >> 8);
  1445. dev_dbg(dev, "7 set of Fan Counts and Duty Cycles: =====>\n");
  1446. for (i = 0; i < 7; i++) {
  1447. dev_dbg(dev, "fan[%d] is: 0x%x\n", i, data->fan[i]);
  1448. dev_dbg(dev, "fan[%d] min is: 0x%x\n", i, data->fan_min[i]);
  1449. dev_dbg(dev, "pwm[%d] is: 0x%x\n", i, data->pwm[i]);
  1450. }
  1451. dev_dbg(dev, "3 set of Temperatures: =====>\n");
  1452. for (i = 0; i < 3; i++)
  1453. dev_dbg(dev, "temp1[%d] is: 0x%x\n", i, data->temp1[i]);
  1454. for (i = 0; i < 2; i++) {
  1455. for (j = 0; j < 6; j++) {
  1456. dev_dbg(dev, "temp_add[%d][%d] is: 0x%x\n", i, j,
  1457. data->temp_add[i][j]);
  1458. }
  1459. }
  1460. for (i = 0; i < 7; i++)
  1461. dev_dbg(dev, "fan_div[%d] is: 0x%x\n", i, data->fan_div[i]);
  1462. dev_dbg(dev, "==========End of the debug message...================\n");
  1463. dev_dbg(dev, "\n");
  1464. }
  1465. #endif
  1466. module_i2c_driver(w83792d_driver);
  1467. MODULE_AUTHOR("Shane Huang (Winbond)");
  1468. MODULE_DESCRIPTION("W83792AD/D driver for linux-2.6");
  1469. MODULE_LICENSE("GPL");