w83791d.c 50 KB

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