w83791d.c 50 KB

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