w83793.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651
  1. /*
  2. w83793.c - Linux kernel driver for hardware monitoring
  3. Copyright (C) 2006 Winbond Electronics Corp.
  4. Yuan Mu
  5. Rudolf Marek <r.marek@assembler.cz>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation - version 2.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. */
  18. /*
  19. Supports following chips:
  20. Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  21. w83793 10 12 8 6 0x7b 0x5ca3 yes no
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-vid.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. /* Addresses to scan */
  33. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  34. I2C_CLIENT_END };
  35. /* Insmod parameters */
  36. I2C_CLIENT_INSMOD_1(w83793);
  37. I2C_CLIENT_MODULE_PARM(force_subclients, "List of subclient addresses: "
  38. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  39. static int reset;
  40. module_param(reset, bool, 0);
  41. MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
  42. /*
  43. Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
  44. as ID, Bank Select registers
  45. */
  46. #define W83793_REG_BANKSEL 0x00
  47. #define W83793_REG_VENDORID 0x0d
  48. #define W83793_REG_CHIPID 0x0e
  49. #define W83793_REG_DEVICEID 0x0f
  50. #define W83793_REG_CONFIG 0x40
  51. #define W83793_REG_MFC 0x58
  52. #define W83793_REG_FANIN_CTRL 0x5c
  53. #define W83793_REG_FANIN_SEL 0x5d
  54. #define W83793_REG_I2C_ADDR 0x0b
  55. #define W83793_REG_I2C_SUBADDR 0x0c
  56. #define W83793_REG_VID_INA 0x05
  57. #define W83793_REG_VID_INB 0x06
  58. #define W83793_REG_VID_LATCHA 0x07
  59. #define W83793_REG_VID_LATCHB 0x08
  60. #define W83793_REG_VID_CTRL 0x59
  61. static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
  62. #define TEMP_READ 0
  63. #define TEMP_CRIT 1
  64. #define TEMP_CRIT_HYST 2
  65. #define TEMP_WARN 3
  66. #define TEMP_WARN_HYST 4
  67. /* only crit and crit_hyst affect real-time alarm status
  68. current crit crit_hyst warn warn_hyst */
  69. static u16 W83793_REG_TEMP[][5] = {
  70. {0x1c, 0x78, 0x79, 0x7a, 0x7b},
  71. {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
  72. {0x1e, 0x80, 0x81, 0x82, 0x83},
  73. {0x1f, 0x84, 0x85, 0x86, 0x87},
  74. {0x20, 0x88, 0x89, 0x8a, 0x8b},
  75. {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
  76. };
  77. #define W83793_REG_TEMP_LOW_BITS 0x22
  78. #define W83793_REG_BEEP(index) (0x53 + (index))
  79. #define W83793_REG_ALARM(index) (0x4b + (index))
  80. #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
  81. #define W83793_REG_IRQ_CTRL 0x50
  82. #define W83793_REG_OVT_CTRL 0x51
  83. #define W83793_REG_OVT_BEEP 0x52
  84. #define IN_READ 0
  85. #define IN_MAX 1
  86. #define IN_LOW 2
  87. static const u16 W83793_REG_IN[][3] = {
  88. /* Current, High, Low */
  89. {0x10, 0x60, 0x61}, /* Vcore A */
  90. {0x11, 0x62, 0x63}, /* Vcore B */
  91. {0x12, 0x64, 0x65}, /* Vtt */
  92. {0x14, 0x6a, 0x6b}, /* VSEN1 */
  93. {0x15, 0x6c, 0x6d}, /* VSEN2 */
  94. {0x16, 0x6e, 0x6f}, /* +3VSEN */
  95. {0x17, 0x70, 0x71}, /* +12VSEN */
  96. {0x18, 0x72, 0x73}, /* 5VDD */
  97. {0x19, 0x74, 0x75}, /* 5VSB */
  98. {0x1a, 0x76, 0x77}, /* VBAT */
  99. };
  100. /* Low Bits of Vcore A/B Vtt Read/High/Low */
  101. static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
  102. static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
  103. static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
  104. #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
  105. #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
  106. #define W83793_REG_PWM_DEFAULT 0xb2
  107. #define W83793_REG_PWM_ENABLE 0x207
  108. #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
  109. #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
  110. #define W83793_REG_TEMP_CRITICAL 0xc5
  111. #define PWM_DUTY 0
  112. #define PWM_START 1
  113. #define PWM_NONSTOP 2
  114. #define PWM_STOP_TIME 3
  115. #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
  116. (nr) == 1 ? 0x220 : 0x218) + (index))
  117. /* bit field, fan1 is bit0, fan2 is bit1 ... */
  118. #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
  119. #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
  120. #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
  121. #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
  122. #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
  123. #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
  124. static inline unsigned long FAN_FROM_REG(u16 val)
  125. {
  126. if ((val >= 0xfff) || (val == 0))
  127. return 0;
  128. return (1350000UL / val);
  129. }
  130. static inline u16 FAN_TO_REG(long rpm)
  131. {
  132. if (rpm <= 0)
  133. return 0x0fff;
  134. return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
  135. }
  136. static inline unsigned long TIME_FROM_REG(u8 reg)
  137. {
  138. return (reg * 100);
  139. }
  140. static inline u8 TIME_TO_REG(unsigned long val)
  141. {
  142. return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
  143. }
  144. static inline long TEMP_FROM_REG(s8 reg)
  145. {
  146. return (reg * 1000);
  147. }
  148. static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
  149. {
  150. return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
  151. }
  152. struct w83793_data {
  153. struct i2c_client *lm75[2];
  154. struct device *hwmon_dev;
  155. struct mutex update_lock;
  156. char valid; /* !=0 if following fields are valid */
  157. unsigned long last_updated; /* In jiffies */
  158. unsigned long last_nonvolatile; /* In jiffies, last time we update the
  159. nonvolatile registers */
  160. u8 bank;
  161. u8 vrm;
  162. u8 vid[2];
  163. u8 in[10][3]; /* Register value, read/high/low */
  164. u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
  165. u16 has_fan; /* Only fan1- fan5 has own pins */
  166. u16 fan[12]; /* Register value combine */
  167. u16 fan_min[12]; /* Register value combine */
  168. s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
  169. u8 temp_low_bits; /* Additional resolution TD1-TD4 */
  170. u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
  171. byte 1: Temp R1,R2 mode, each has 1 bit */
  172. u8 temp_critical; /* If reached all fan will be at full speed */
  173. u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
  174. u8 has_pwm;
  175. u8 has_temp;
  176. u8 has_vid;
  177. u8 pwm_enable; /* Register value, each Temp has 1 bit */
  178. u8 pwm_uptime; /* Register value */
  179. u8 pwm_downtime; /* Register value */
  180. u8 pwm_default; /* All fan default pwm, next poweron valid */
  181. u8 pwm[8][3]; /* Register value */
  182. u8 pwm_stop_time[8];
  183. u8 temp_cruise[6];
  184. u8 alarms[5]; /* realtime status registers */
  185. u8 beeps[5];
  186. u8 beep_enable;
  187. u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
  188. u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
  189. u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
  190. };
  191. static u8 w83793_read_value(struct i2c_client *client, u16 reg);
  192. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
  193. static int w83793_probe(struct i2c_client *client,
  194. const struct i2c_device_id *id);
  195. static int w83793_detect(struct i2c_client *client, int kind,
  196. struct i2c_board_info *info);
  197. static int w83793_remove(struct i2c_client *client);
  198. static void w83793_init_client(struct i2c_client *client);
  199. static void w83793_update_nonvolatile(struct device *dev);
  200. static struct w83793_data *w83793_update_device(struct device *dev);
  201. static const struct i2c_device_id w83793_id[] = {
  202. { "w83793", w83793 },
  203. { }
  204. };
  205. MODULE_DEVICE_TABLE(i2c, w83793_id);
  206. static struct i2c_driver w83793_driver = {
  207. .class = I2C_CLASS_HWMON,
  208. .driver = {
  209. .name = "w83793",
  210. },
  211. .probe = w83793_probe,
  212. .remove = w83793_remove,
  213. .id_table = w83793_id,
  214. .detect = w83793_detect,
  215. .address_data = &addr_data,
  216. };
  217. static ssize_t
  218. show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  219. {
  220. struct w83793_data *data = dev_get_drvdata(dev);
  221. return sprintf(buf, "%d\n", data->vrm);
  222. }
  223. static ssize_t
  224. show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  225. {
  226. struct w83793_data *data = w83793_update_device(dev);
  227. struct sensor_device_attribute_2 *sensor_attr =
  228. to_sensor_dev_attr_2(attr);
  229. int index = sensor_attr->index;
  230. return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
  231. }
  232. static ssize_t
  233. store_vrm(struct device *dev, struct device_attribute *attr,
  234. const char *buf, size_t count)
  235. {
  236. struct w83793_data *data = dev_get_drvdata(dev);
  237. data->vrm = simple_strtoul(buf, NULL, 10);
  238. return count;
  239. }
  240. #define ALARM_STATUS 0
  241. #define BEEP_ENABLE 1
  242. static ssize_t
  243. show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
  244. {
  245. struct w83793_data *data = w83793_update_device(dev);
  246. struct sensor_device_attribute_2 *sensor_attr =
  247. to_sensor_dev_attr_2(attr);
  248. int nr = sensor_attr->nr;
  249. int index = sensor_attr->index >> 3;
  250. int bit = sensor_attr->index & 0x07;
  251. u8 val;
  252. if (ALARM_STATUS == nr) {
  253. val = (data->alarms[index] >> (bit)) & 1;
  254. } else { /* BEEP_ENABLE */
  255. val = (data->beeps[index] >> (bit)) & 1;
  256. }
  257. return sprintf(buf, "%u\n", val);
  258. }
  259. static ssize_t
  260. store_beep(struct device *dev, struct device_attribute *attr,
  261. const char *buf, size_t count)
  262. {
  263. struct i2c_client *client = to_i2c_client(dev);
  264. struct w83793_data *data = i2c_get_clientdata(client);
  265. struct sensor_device_attribute_2 *sensor_attr =
  266. to_sensor_dev_attr_2(attr);
  267. int index = sensor_attr->index >> 3;
  268. int shift = sensor_attr->index & 0x07;
  269. u8 beep_bit = 1 << shift;
  270. u8 val;
  271. val = simple_strtoul(buf, NULL, 10);
  272. if (val != 0 && val != 1)
  273. return -EINVAL;
  274. mutex_lock(&data->update_lock);
  275. data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
  276. data->beeps[index] &= ~beep_bit;
  277. data->beeps[index] |= val << shift;
  278. w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
  279. mutex_unlock(&data->update_lock);
  280. return count;
  281. }
  282. static ssize_t
  283. show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
  284. {
  285. struct w83793_data *data = w83793_update_device(dev);
  286. return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
  287. }
  288. static ssize_t
  289. store_beep_enable(struct device *dev, struct device_attribute *attr,
  290. const char *buf, size_t count)
  291. {
  292. struct i2c_client *client = to_i2c_client(dev);
  293. struct w83793_data *data = i2c_get_clientdata(client);
  294. u8 val = simple_strtoul(buf, NULL, 10);
  295. if (val != 0 && val != 1)
  296. return -EINVAL;
  297. mutex_lock(&data->update_lock);
  298. data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
  299. & 0xfd;
  300. data->beep_enable |= val << 1;
  301. w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
  302. mutex_unlock(&data->update_lock);
  303. return count;
  304. }
  305. /* Write any value to clear chassis alarm */
  306. static ssize_t
  307. store_chassis_clear(struct device *dev,
  308. struct device_attribute *attr, const char *buf,
  309. size_t count)
  310. {
  311. struct i2c_client *client = to_i2c_client(dev);
  312. struct w83793_data *data = i2c_get_clientdata(client);
  313. u8 val;
  314. mutex_lock(&data->update_lock);
  315. val = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
  316. val |= 0x80;
  317. w83793_write_value(client, W83793_REG_CLR_CHASSIS, val);
  318. mutex_unlock(&data->update_lock);
  319. return count;
  320. }
  321. #define FAN_INPUT 0
  322. #define FAN_MIN 1
  323. static ssize_t
  324. show_fan(struct device *dev, struct device_attribute *attr, char *buf)
  325. {
  326. struct sensor_device_attribute_2 *sensor_attr =
  327. to_sensor_dev_attr_2(attr);
  328. int nr = sensor_attr->nr;
  329. int index = sensor_attr->index;
  330. struct w83793_data *data = w83793_update_device(dev);
  331. u16 val;
  332. if (FAN_INPUT == nr) {
  333. val = data->fan[index] & 0x0fff;
  334. } else {
  335. val = data->fan_min[index] & 0x0fff;
  336. }
  337. return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
  338. }
  339. static ssize_t
  340. store_fan_min(struct device *dev, struct device_attribute *attr,
  341. const char *buf, size_t count)
  342. {
  343. struct sensor_device_attribute_2 *sensor_attr =
  344. to_sensor_dev_attr_2(attr);
  345. int index = sensor_attr->index;
  346. struct i2c_client *client = to_i2c_client(dev);
  347. struct w83793_data *data = i2c_get_clientdata(client);
  348. u16 val = FAN_TO_REG(simple_strtoul(buf, NULL, 10));
  349. mutex_lock(&data->update_lock);
  350. data->fan_min[index] = val;
  351. w83793_write_value(client, W83793_REG_FAN_MIN(index),
  352. (val >> 8) & 0xff);
  353. w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
  354. mutex_unlock(&data->update_lock);
  355. return count;
  356. }
  357. static ssize_t
  358. show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  359. {
  360. struct sensor_device_attribute_2 *sensor_attr =
  361. to_sensor_dev_attr_2(attr);
  362. struct w83793_data *data = w83793_update_device(dev);
  363. u16 val;
  364. int nr = sensor_attr->nr;
  365. int index = sensor_attr->index;
  366. if (PWM_STOP_TIME == nr)
  367. val = TIME_FROM_REG(data->pwm_stop_time[index]);
  368. else
  369. val = (data->pwm[index][nr] & 0x3f) << 2;
  370. return sprintf(buf, "%d\n", val);
  371. }
  372. static ssize_t
  373. store_pwm(struct device *dev, struct device_attribute *attr,
  374. const char *buf, size_t count)
  375. {
  376. struct i2c_client *client = to_i2c_client(dev);
  377. struct w83793_data *data = i2c_get_clientdata(client);
  378. struct sensor_device_attribute_2 *sensor_attr =
  379. to_sensor_dev_attr_2(attr);
  380. int nr = sensor_attr->nr;
  381. int index = sensor_attr->index;
  382. u8 val;
  383. mutex_lock(&data->update_lock);
  384. if (PWM_STOP_TIME == nr) {
  385. val = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
  386. data->pwm_stop_time[index] = val;
  387. w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
  388. val);
  389. } else {
  390. val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff)
  391. >> 2;
  392. data->pwm[index][nr] =
  393. w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
  394. data->pwm[index][nr] |= val;
  395. w83793_write_value(client, W83793_REG_PWM(index, nr),
  396. data->pwm[index][nr]);
  397. }
  398. mutex_unlock(&data->update_lock);
  399. return count;
  400. }
  401. static ssize_t
  402. show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  403. {
  404. struct sensor_device_attribute_2 *sensor_attr =
  405. to_sensor_dev_attr_2(attr);
  406. int nr = sensor_attr->nr;
  407. int index = sensor_attr->index;
  408. struct w83793_data *data = w83793_update_device(dev);
  409. long temp = TEMP_FROM_REG(data->temp[index][nr]);
  410. if (TEMP_READ == nr && index < 4) { /* Only TD1-TD4 have low bits */
  411. int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
  412. temp += temp > 0 ? low : -low;
  413. }
  414. return sprintf(buf, "%ld\n", temp);
  415. }
  416. static ssize_t
  417. store_temp(struct device *dev, struct device_attribute *attr,
  418. const char *buf, size_t count)
  419. {
  420. struct sensor_device_attribute_2 *sensor_attr =
  421. to_sensor_dev_attr_2(attr);
  422. int nr = sensor_attr->nr;
  423. int index = sensor_attr->index;
  424. struct i2c_client *client = to_i2c_client(dev);
  425. struct w83793_data *data = i2c_get_clientdata(client);
  426. long tmp = simple_strtol(buf, NULL, 10);
  427. mutex_lock(&data->update_lock);
  428. data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
  429. w83793_write_value(client, W83793_REG_TEMP[index][nr],
  430. data->temp[index][nr]);
  431. mutex_unlock(&data->update_lock);
  432. return count;
  433. }
  434. /*
  435. TD1-TD4
  436. each has 4 mode:(2 bits)
  437. 0: Stop monitor
  438. 1: Use internal temp sensor(default)
  439. 2: Reserved
  440. 3: Use sensor in Intel CPU and get result by PECI
  441. TR1-TR2
  442. each has 2 mode:(1 bit)
  443. 0: Disable temp sensor monitor
  444. 1: To enable temp sensors monitor
  445. */
  446. /* 0 disable, 6 PECI */
  447. static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
  448. static ssize_t
  449. show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
  450. {
  451. struct w83793_data *data = w83793_update_device(dev);
  452. struct sensor_device_attribute_2 *sensor_attr =
  453. to_sensor_dev_attr_2(attr);
  454. int index = sensor_attr->index;
  455. u8 mask = (index < 4) ? 0x03 : 0x01;
  456. u8 shift = (index < 4) ? (2 * index) : (index - 4);
  457. u8 tmp;
  458. index = (index < 4) ? 0 : 1;
  459. tmp = (data->temp_mode[index] >> shift) & mask;
  460. /* for the internal sensor, found out if diode or thermistor */
  461. if (tmp == 1) {
  462. tmp = index == 0 ? 3 : 4;
  463. } else {
  464. tmp = TO_TEMP_MODE[tmp];
  465. }
  466. return sprintf(buf, "%d\n", tmp);
  467. }
  468. static ssize_t
  469. store_temp_mode(struct device *dev, struct device_attribute *attr,
  470. const char *buf, size_t count)
  471. {
  472. struct i2c_client *client = to_i2c_client(dev);
  473. struct w83793_data *data = i2c_get_clientdata(client);
  474. struct sensor_device_attribute_2 *sensor_attr =
  475. to_sensor_dev_attr_2(attr);
  476. int index = sensor_attr->index;
  477. u8 mask = (index < 4) ? 0x03 : 0x01;
  478. u8 shift = (index < 4) ? (2 * index) : (index - 4);
  479. u8 val = simple_strtoul(buf, NULL, 10);
  480. /* transform the sysfs interface values into table above */
  481. if ((val == 6) && (index < 4)) {
  482. val -= 3;
  483. } else if ((val == 3 && index < 4)
  484. || (val == 4 && index >= 4)) {
  485. /* transform diode or thermistor into internal enable */
  486. val = !!val;
  487. } else {
  488. return -EINVAL;
  489. }
  490. index = (index < 4) ? 0 : 1;
  491. mutex_lock(&data->update_lock);
  492. data->temp_mode[index] =
  493. w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
  494. data->temp_mode[index] &= ~(mask << shift);
  495. data->temp_mode[index] |= val << shift;
  496. w83793_write_value(client, W83793_REG_TEMP_MODE[index],
  497. data->temp_mode[index]);
  498. mutex_unlock(&data->update_lock);
  499. return count;
  500. }
  501. #define SETUP_PWM_DEFAULT 0
  502. #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
  503. #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
  504. #define SETUP_TEMP_CRITICAL 3
  505. static ssize_t
  506. show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
  507. {
  508. struct sensor_device_attribute_2 *sensor_attr =
  509. to_sensor_dev_attr_2(attr);
  510. int nr = sensor_attr->nr;
  511. struct w83793_data *data = w83793_update_device(dev);
  512. u32 val = 0;
  513. if (SETUP_PWM_DEFAULT == nr) {
  514. val = (data->pwm_default & 0x3f) << 2;
  515. } else if (SETUP_PWM_UPTIME == nr) {
  516. val = TIME_FROM_REG(data->pwm_uptime);
  517. } else if (SETUP_PWM_DOWNTIME == nr) {
  518. val = TIME_FROM_REG(data->pwm_downtime);
  519. } else if (SETUP_TEMP_CRITICAL == nr) {
  520. val = TEMP_FROM_REG(data->temp_critical & 0x7f);
  521. }
  522. return sprintf(buf, "%d\n", val);
  523. }
  524. static ssize_t
  525. store_sf_setup(struct device *dev, struct device_attribute *attr,
  526. const char *buf, size_t count)
  527. {
  528. struct sensor_device_attribute_2 *sensor_attr =
  529. to_sensor_dev_attr_2(attr);
  530. int nr = sensor_attr->nr;
  531. struct i2c_client *client = to_i2c_client(dev);
  532. struct w83793_data *data = i2c_get_clientdata(client);
  533. mutex_lock(&data->update_lock);
  534. if (SETUP_PWM_DEFAULT == nr) {
  535. data->pwm_default =
  536. w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
  537. data->pwm_default |= SENSORS_LIMIT(simple_strtoul(buf, NULL,
  538. 10),
  539. 0, 0xff) >> 2;
  540. w83793_write_value(client, W83793_REG_PWM_DEFAULT,
  541. data->pwm_default);
  542. } else if (SETUP_PWM_UPTIME == nr) {
  543. data->pwm_uptime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
  544. data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
  545. w83793_write_value(client, W83793_REG_PWM_UPTIME,
  546. data->pwm_uptime);
  547. } else if (SETUP_PWM_DOWNTIME == nr) {
  548. data->pwm_downtime = TIME_TO_REG(simple_strtoul(buf, NULL, 10));
  549. data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
  550. w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
  551. data->pwm_downtime);
  552. } else { /* SETUP_TEMP_CRITICAL */
  553. data->temp_critical =
  554. w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
  555. data->temp_critical |= TEMP_TO_REG(simple_strtol(buf, NULL, 10),
  556. 0, 0x7f);
  557. w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
  558. data->temp_critical);
  559. }
  560. mutex_unlock(&data->update_lock);
  561. return count;
  562. }
  563. /*
  564. Temp SmartFan control
  565. TEMP_FAN_MAP
  566. Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
  567. It's possible two or more temp channels control the same fan, w83793
  568. always prefers to pick the most critical request and applies it to
  569. the related Fan.
  570. It's possible one fan is not in any mapping of 6 temp channels, this
  571. means the fan is manual mode
  572. TEMP_PWM_ENABLE
  573. Each temp channel has its own SmartFan mode, and temp channel
  574. control fans that are set by TEMP_FAN_MAP
  575. 0: SmartFanII mode
  576. 1: Thermal Cruise Mode
  577. TEMP_CRUISE
  578. Target temperature in thermal cruise mode, w83793 will try to turn
  579. fan speed to keep the temperature of target device around this
  580. temperature.
  581. TEMP_TOLERANCE
  582. If Temp higher or lower than target with this tolerance, w83793
  583. will take actions to speed up or slow down the fan to keep the
  584. temperature within the tolerance range.
  585. */
  586. #define TEMP_FAN_MAP 0
  587. #define TEMP_PWM_ENABLE 1
  588. #define TEMP_CRUISE 2
  589. #define TEMP_TOLERANCE 3
  590. static ssize_t
  591. show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
  592. {
  593. struct sensor_device_attribute_2 *sensor_attr =
  594. to_sensor_dev_attr_2(attr);
  595. int nr = sensor_attr->nr;
  596. int index = sensor_attr->index;
  597. struct w83793_data *data = w83793_update_device(dev);
  598. u32 val;
  599. if (TEMP_FAN_MAP == nr) {
  600. val = data->temp_fan_map[index];
  601. } else if (TEMP_PWM_ENABLE == nr) {
  602. /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
  603. val = ((data->pwm_enable >> index) & 0x01) + 2;
  604. } else if (TEMP_CRUISE == nr) {
  605. val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
  606. } else { /* TEMP_TOLERANCE */
  607. val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
  608. val = TEMP_FROM_REG(val & 0x0f);
  609. }
  610. return sprintf(buf, "%d\n", val);
  611. }
  612. static ssize_t
  613. store_sf_ctrl(struct device *dev, struct device_attribute *attr,
  614. const char *buf, size_t count)
  615. {
  616. struct sensor_device_attribute_2 *sensor_attr =
  617. to_sensor_dev_attr_2(attr);
  618. int nr = sensor_attr->nr;
  619. int index = sensor_attr->index;
  620. struct i2c_client *client = to_i2c_client(dev);
  621. struct w83793_data *data = i2c_get_clientdata(client);
  622. u32 val;
  623. mutex_lock(&data->update_lock);
  624. if (TEMP_FAN_MAP == nr) {
  625. val = simple_strtoul(buf, NULL, 10) & 0xff;
  626. w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
  627. data->temp_fan_map[index] = val;
  628. } else if (TEMP_PWM_ENABLE == nr) {
  629. val = simple_strtoul(buf, NULL, 10);
  630. if (2 == val || 3 == val) {
  631. data->pwm_enable =
  632. w83793_read_value(client, W83793_REG_PWM_ENABLE);
  633. if (val - 2)
  634. data->pwm_enable |= 1 << index;
  635. else
  636. data->pwm_enable &= ~(1 << index);
  637. w83793_write_value(client, W83793_REG_PWM_ENABLE,
  638. data->pwm_enable);
  639. } else {
  640. mutex_unlock(&data->update_lock);
  641. return -EINVAL;
  642. }
  643. } else if (TEMP_CRUISE == nr) {
  644. data->temp_cruise[index] =
  645. w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
  646. val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
  647. data->temp_cruise[index] &= 0x80;
  648. data->temp_cruise[index] |= val;
  649. w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
  650. data->temp_cruise[index]);
  651. } else { /* TEMP_TOLERANCE */
  652. int i = index >> 1;
  653. u8 shift = (index & 0x01) ? 4 : 0;
  654. data->tolerance[i] =
  655. w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  656. val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x0f);
  657. data->tolerance[i] &= ~(0x0f << shift);
  658. data->tolerance[i] |= val << shift;
  659. w83793_write_value(client, W83793_REG_TEMP_TOL(i),
  660. data->tolerance[i]);
  661. }
  662. mutex_unlock(&data->update_lock);
  663. return count;
  664. }
  665. static ssize_t
  666. show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  667. {
  668. struct sensor_device_attribute_2 *sensor_attr =
  669. to_sensor_dev_attr_2(attr);
  670. int nr = sensor_attr->nr;
  671. int index = sensor_attr->index;
  672. struct w83793_data *data = w83793_update_device(dev);
  673. return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
  674. }
  675. static ssize_t
  676. store_sf2_pwm(struct device *dev, struct device_attribute *attr,
  677. const char *buf, size_t count)
  678. {
  679. struct i2c_client *client = to_i2c_client(dev);
  680. struct w83793_data *data = i2c_get_clientdata(client);
  681. struct sensor_device_attribute_2 *sensor_attr =
  682. to_sensor_dev_attr_2(attr);
  683. int nr = sensor_attr->nr;
  684. int index = sensor_attr->index;
  685. u8 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 0xff) >> 2;
  686. mutex_lock(&data->update_lock);
  687. data->sf2_pwm[index][nr] =
  688. w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
  689. data->sf2_pwm[index][nr] |= val;
  690. w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
  691. data->sf2_pwm[index][nr]);
  692. mutex_unlock(&data->update_lock);
  693. return count;
  694. }
  695. static ssize_t
  696. show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
  697. {
  698. struct sensor_device_attribute_2 *sensor_attr =
  699. to_sensor_dev_attr_2(attr);
  700. int nr = sensor_attr->nr;
  701. int index = sensor_attr->index;
  702. struct w83793_data *data = w83793_update_device(dev);
  703. return sprintf(buf, "%ld\n",
  704. TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
  705. }
  706. static ssize_t
  707. store_sf2_temp(struct device *dev, struct device_attribute *attr,
  708. const char *buf, size_t count)
  709. {
  710. struct i2c_client *client = to_i2c_client(dev);
  711. struct w83793_data *data = i2c_get_clientdata(client);
  712. struct sensor_device_attribute_2 *sensor_attr =
  713. to_sensor_dev_attr_2(attr);
  714. int nr = sensor_attr->nr;
  715. int index = sensor_attr->index;
  716. u8 val = TEMP_TO_REG(simple_strtol(buf, NULL, 10), 0, 0x7f);
  717. mutex_lock(&data->update_lock);
  718. data->sf2_temp[index][nr] =
  719. w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
  720. data->sf2_temp[index][nr] |= val;
  721. w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
  722. data->sf2_temp[index][nr]);
  723. mutex_unlock(&data->update_lock);
  724. return count;
  725. }
  726. /* only Vcore A/B and Vtt have additional 2 bits precision */
  727. static ssize_t
  728. show_in(struct device *dev, struct device_attribute *attr, char *buf)
  729. {
  730. struct sensor_device_attribute_2 *sensor_attr =
  731. to_sensor_dev_attr_2(attr);
  732. int nr = sensor_attr->nr;
  733. int index = sensor_attr->index;
  734. struct w83793_data *data = w83793_update_device(dev);
  735. u16 val = data->in[index][nr];
  736. if (index < 3) {
  737. val <<= 2;
  738. val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
  739. }
  740. /* voltage inputs 5VDD and 5VSB needs 150mV offset */
  741. val = val * scale_in[index] + scale_in_add[index];
  742. return sprintf(buf, "%d\n", val);
  743. }
  744. static ssize_t
  745. store_in(struct device *dev, struct device_attribute *attr,
  746. const char *buf, size_t count)
  747. {
  748. struct sensor_device_attribute_2 *sensor_attr =
  749. to_sensor_dev_attr_2(attr);
  750. int nr = sensor_attr->nr;
  751. int index = sensor_attr->index;
  752. struct i2c_client *client = to_i2c_client(dev);
  753. struct w83793_data *data = i2c_get_clientdata(client);
  754. u32 val;
  755. val =
  756. (simple_strtoul(buf, NULL, 10) +
  757. scale_in[index] / 2) / scale_in[index];
  758. mutex_lock(&data->update_lock);
  759. if (index > 2) {
  760. /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
  761. if (1 == nr || 2 == nr) {
  762. val -= scale_in_add[index] / scale_in[index];
  763. }
  764. val = SENSORS_LIMIT(val, 0, 255);
  765. } else {
  766. val = SENSORS_LIMIT(val, 0, 0x3FF);
  767. data->in_low_bits[nr] =
  768. w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
  769. data->in_low_bits[nr] &= ~(0x03 << (2 * index));
  770. data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
  771. w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
  772. data->in_low_bits[nr]);
  773. val >>= 2;
  774. }
  775. data->in[index][nr] = val;
  776. w83793_write_value(client, W83793_REG_IN[index][nr],
  777. data->in[index][nr]);
  778. mutex_unlock(&data->update_lock);
  779. return count;
  780. }
  781. #define NOT_USED -1
  782. #define SENSOR_ATTR_IN(index) \
  783. SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
  784. IN_READ, index), \
  785. SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
  786. store_in, IN_MAX, index), \
  787. SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
  788. store_in, IN_LOW, index), \
  789. SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
  790. NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
  791. SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
  792. show_alarm_beep, store_beep, BEEP_ENABLE, \
  793. index + ((index > 2) ? 1 : 0))
  794. #define SENSOR_ATTR_FAN(index) \
  795. SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
  796. NULL, ALARM_STATUS, index + 17), \
  797. SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
  798. show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
  799. SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
  800. NULL, FAN_INPUT, index - 1), \
  801. SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
  802. show_fan, store_fan_min, FAN_MIN, index - 1)
  803. #define SENSOR_ATTR_PWM(index) \
  804. SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
  805. store_pwm, PWM_DUTY, index - 1), \
  806. SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
  807. show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
  808. SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
  809. show_pwm, store_pwm, PWM_START, index - 1), \
  810. SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
  811. show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
  812. #define SENSOR_ATTR_TEMP(index) \
  813. SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
  814. show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
  815. SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
  816. NULL, TEMP_READ, index - 1), \
  817. SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
  818. store_temp, TEMP_CRIT, index - 1), \
  819. SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
  820. show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
  821. SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
  822. store_temp, TEMP_WARN, index - 1), \
  823. SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
  824. show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
  825. SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
  826. show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
  827. SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
  828. show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
  829. SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
  830. S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
  831. TEMP_FAN_MAP, index - 1), \
  832. SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
  833. show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
  834. index - 1), \
  835. SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
  836. show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
  837. SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
  838. store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
  839. SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
  840. show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
  841. SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
  842. show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
  843. SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
  844. show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
  845. SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
  846. show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
  847. SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
  848. show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
  849. SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
  850. show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
  851. SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
  852. show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
  853. SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
  854. show_sf2_temp, store_sf2_temp, 0, index - 1), \
  855. SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
  856. show_sf2_temp, store_sf2_temp, 1, index - 1), \
  857. SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
  858. show_sf2_temp, store_sf2_temp, 2, index - 1), \
  859. SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
  860. show_sf2_temp, store_sf2_temp, 3, index - 1), \
  861. SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
  862. show_sf2_temp, store_sf2_temp, 4, index - 1), \
  863. SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
  864. show_sf2_temp, store_sf2_temp, 5, index - 1), \
  865. SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
  866. show_sf2_temp, store_sf2_temp, 6, index - 1)
  867. static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
  868. SENSOR_ATTR_IN(0),
  869. SENSOR_ATTR_IN(1),
  870. SENSOR_ATTR_IN(2),
  871. SENSOR_ATTR_IN(3),
  872. SENSOR_ATTR_IN(4),
  873. SENSOR_ATTR_IN(5),
  874. SENSOR_ATTR_IN(6),
  875. SENSOR_ATTR_IN(7),
  876. SENSOR_ATTR_IN(8),
  877. SENSOR_ATTR_IN(9),
  878. SENSOR_ATTR_FAN(1),
  879. SENSOR_ATTR_FAN(2),
  880. SENSOR_ATTR_FAN(3),
  881. SENSOR_ATTR_FAN(4),
  882. SENSOR_ATTR_FAN(5),
  883. SENSOR_ATTR_PWM(1),
  884. SENSOR_ATTR_PWM(2),
  885. SENSOR_ATTR_PWM(3),
  886. };
  887. static struct sensor_device_attribute_2 w83793_temp[] = {
  888. SENSOR_ATTR_TEMP(1),
  889. SENSOR_ATTR_TEMP(2),
  890. SENSOR_ATTR_TEMP(3),
  891. SENSOR_ATTR_TEMP(4),
  892. SENSOR_ATTR_TEMP(5),
  893. SENSOR_ATTR_TEMP(6),
  894. };
  895. /* Fan6-Fan12 */
  896. static struct sensor_device_attribute_2 w83793_left_fan[] = {
  897. SENSOR_ATTR_FAN(6),
  898. SENSOR_ATTR_FAN(7),
  899. SENSOR_ATTR_FAN(8),
  900. SENSOR_ATTR_FAN(9),
  901. SENSOR_ATTR_FAN(10),
  902. SENSOR_ATTR_FAN(11),
  903. SENSOR_ATTR_FAN(12),
  904. };
  905. /* Pwm4-Pwm8 */
  906. static struct sensor_device_attribute_2 w83793_left_pwm[] = {
  907. SENSOR_ATTR_PWM(4),
  908. SENSOR_ATTR_PWM(5),
  909. SENSOR_ATTR_PWM(6),
  910. SENSOR_ATTR_PWM(7),
  911. SENSOR_ATTR_PWM(8),
  912. };
  913. static struct sensor_device_attribute_2 w83793_vid[] = {
  914. SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
  915. SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
  916. };
  917. static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
  918. static struct sensor_device_attribute_2 sda_single_files[] = {
  919. SENSOR_ATTR_2(chassis, S_IWUSR | S_IRUGO, show_alarm_beep,
  920. store_chassis_clear, ALARM_STATUS, 30),
  921. SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
  922. store_beep_enable, NOT_USED, NOT_USED),
  923. SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
  924. store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
  925. SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
  926. store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
  927. SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
  928. store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
  929. SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
  930. store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
  931. };
  932. static void w83793_init_client(struct i2c_client *client)
  933. {
  934. if (reset) {
  935. w83793_write_value(client, W83793_REG_CONFIG, 0x80);
  936. }
  937. /* Start monitoring */
  938. w83793_write_value(client, W83793_REG_CONFIG,
  939. w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
  940. }
  941. static int w83793_remove(struct i2c_client *client)
  942. {
  943. struct w83793_data *data = i2c_get_clientdata(client);
  944. struct device *dev = &client->dev;
  945. int i;
  946. hwmon_device_unregister(data->hwmon_dev);
  947. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  948. device_remove_file(dev,
  949. &w83793_sensor_attr_2[i].dev_attr);
  950. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  951. device_remove_file(dev, &sda_single_files[i].dev_attr);
  952. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  953. device_remove_file(dev, &w83793_vid[i].dev_attr);
  954. device_remove_file(dev, &dev_attr_vrm);
  955. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  956. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  957. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  958. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  959. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  960. device_remove_file(dev, &w83793_temp[i].dev_attr);
  961. if (data->lm75[0] != NULL)
  962. i2c_unregister_device(data->lm75[0]);
  963. if (data->lm75[1] != NULL)
  964. i2c_unregister_device(data->lm75[1]);
  965. kfree(data);
  966. return 0;
  967. }
  968. static int
  969. w83793_detect_subclients(struct i2c_client *client)
  970. {
  971. int i, id, err;
  972. int address = client->addr;
  973. u8 tmp;
  974. struct i2c_adapter *adapter = client->adapter;
  975. struct w83793_data *data = i2c_get_clientdata(client);
  976. id = i2c_adapter_id(adapter);
  977. if (force_subclients[0] == id && force_subclients[1] == address) {
  978. for (i = 2; i <= 3; i++) {
  979. if (force_subclients[i] < 0x48
  980. || force_subclients[i] > 0x4f) {
  981. dev_err(&client->dev,
  982. "invalid subclient "
  983. "address %d; must be 0x48-0x4f\n",
  984. force_subclients[i]);
  985. err = -EINVAL;
  986. goto ERROR_SC_0;
  987. }
  988. }
  989. w83793_write_value(client, W83793_REG_I2C_SUBADDR,
  990. (force_subclients[2] & 0x07) |
  991. ((force_subclients[3] & 0x07) << 4));
  992. }
  993. tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
  994. if (!(tmp & 0x08)) {
  995. data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
  996. }
  997. if (!(tmp & 0x80)) {
  998. if ((data->lm75[0] != NULL)
  999. && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
  1000. dev_err(&client->dev,
  1001. "duplicate addresses 0x%x, "
  1002. "use force_subclients\n", data->lm75[0]->addr);
  1003. err = -ENODEV;
  1004. goto ERROR_SC_1;
  1005. }
  1006. data->lm75[1] = i2c_new_dummy(adapter,
  1007. 0x48 + ((tmp >> 4) & 0x7));
  1008. }
  1009. return 0;
  1010. /* Undo inits in case of errors */
  1011. ERROR_SC_1:
  1012. if (data->lm75[0] != NULL)
  1013. i2c_unregister_device(data->lm75[0]);
  1014. ERROR_SC_0:
  1015. return err;
  1016. }
  1017. /* Return 0 if detection is successful, -ENODEV otherwise */
  1018. static int w83793_detect(struct i2c_client *client, int kind,
  1019. struct i2c_board_info *info)
  1020. {
  1021. u8 tmp, bank;
  1022. struct i2c_adapter *adapter = client->adapter;
  1023. unsigned short address = client->addr;
  1024. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1025. return -ENODEV;
  1026. }
  1027. bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1028. if (kind < 0) {
  1029. tmp = bank & 0x80 ? 0x5c : 0xa3;
  1030. /* Check Winbond vendor ID */
  1031. if (tmp != i2c_smbus_read_byte_data(client,
  1032. W83793_REG_VENDORID)) {
  1033. pr_debug("w83793: Detection failed at check "
  1034. "vendor id\n");
  1035. return -ENODEV;
  1036. }
  1037. /* If Winbond chip, address of chip and W83793_REG_I2C_ADDR
  1038. should match */
  1039. if ((bank & 0x07) == 0
  1040. && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
  1041. (address << 1)) {
  1042. pr_debug("w83793: Detection failed at check "
  1043. "i2c addr\n");
  1044. return -ENODEV;
  1045. }
  1046. }
  1047. /* We have either had a force parameter, or we have already detected the
  1048. Winbond. Determine the chip type now */
  1049. if (kind <= 0) {
  1050. if (0x7b == i2c_smbus_read_byte_data(client,
  1051. W83793_REG_CHIPID)) {
  1052. kind = w83793;
  1053. } else {
  1054. if (kind == 0)
  1055. dev_warn(&adapter->dev, "w83793: Ignoring "
  1056. "'force' parameter for unknown chip "
  1057. "at address 0x%02x\n", address);
  1058. return -ENODEV;
  1059. }
  1060. }
  1061. strlcpy(info->type, "w83793", I2C_NAME_SIZE);
  1062. return 0;
  1063. }
  1064. static int w83793_probe(struct i2c_client *client,
  1065. const struct i2c_device_id *id)
  1066. {
  1067. struct device *dev = &client->dev;
  1068. struct w83793_data *data;
  1069. int i, tmp, val, err;
  1070. int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
  1071. int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
  1072. int files_temp = ARRAY_SIZE(w83793_temp) / 6;
  1073. data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
  1074. if (!data) {
  1075. err = -ENOMEM;
  1076. goto exit;
  1077. }
  1078. i2c_set_clientdata(client, data);
  1079. data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1080. mutex_init(&data->update_lock);
  1081. err = w83793_detect_subclients(client);
  1082. if (err)
  1083. goto free_mem;
  1084. /* Initialize the chip */
  1085. w83793_init_client(client);
  1086. /*
  1087. Only fan 1-5 has their own input pins,
  1088. Pwm 1-3 has their own pins
  1089. */
  1090. data->has_fan = 0x1f;
  1091. data->has_pwm = 0x07;
  1092. tmp = w83793_read_value(client, W83793_REG_MFC);
  1093. val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
  1094. /* check the function of pins 49-56 */
  1095. if (tmp & 0x80) {
  1096. data->has_vid |= 0x2; /* has VIDB */
  1097. } else {
  1098. data->has_pwm |= 0x18; /* pwm 4,5 */
  1099. if (val & 0x01) { /* fan 6 */
  1100. data->has_fan |= 0x20;
  1101. data->has_pwm |= 0x20;
  1102. }
  1103. if (val & 0x02) { /* fan 7 */
  1104. data->has_fan |= 0x40;
  1105. data->has_pwm |= 0x40;
  1106. }
  1107. if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
  1108. data->has_fan |= 0x80;
  1109. data->has_pwm |= 0x80;
  1110. }
  1111. }
  1112. /* check the function of pins 37-40 */
  1113. if (!(tmp & 0x29))
  1114. data->has_vid |= 0x1; /* has VIDA */
  1115. if (0x08 == (tmp & 0x0c)) {
  1116. if (val & 0x08) /* fan 9 */
  1117. data->has_fan |= 0x100;
  1118. if (val & 0x10) /* fan 10 */
  1119. data->has_fan |= 0x200;
  1120. }
  1121. if (0x20 == (tmp & 0x30)) {
  1122. if (val & 0x20) /* fan 11 */
  1123. data->has_fan |= 0x400;
  1124. if (val & 0x40) /* fan 12 */
  1125. data->has_fan |= 0x800;
  1126. }
  1127. if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
  1128. data->has_fan |= 0x80;
  1129. data->has_pwm |= 0x80;
  1130. }
  1131. tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
  1132. if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
  1133. data->has_fan |= 0x100;
  1134. }
  1135. if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
  1136. data->has_fan |= 0x200;
  1137. }
  1138. if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
  1139. data->has_fan |= 0x400;
  1140. }
  1141. if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
  1142. data->has_fan |= 0x800;
  1143. }
  1144. /* check the temp1-6 mode, ignore former AMDSI selected inputs */
  1145. tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[0]);
  1146. if (tmp & 0x01)
  1147. data->has_temp |= 0x01;
  1148. if (tmp & 0x04)
  1149. data->has_temp |= 0x02;
  1150. if (tmp & 0x10)
  1151. data->has_temp |= 0x04;
  1152. if (tmp & 0x40)
  1153. data->has_temp |= 0x08;
  1154. tmp = w83793_read_value(client,W83793_REG_TEMP_MODE[1]);
  1155. if (tmp & 0x01)
  1156. data->has_temp |= 0x10;
  1157. if (tmp & 0x02)
  1158. data->has_temp |= 0x20;
  1159. /* Register sysfs hooks */
  1160. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
  1161. err = device_create_file(dev,
  1162. &w83793_sensor_attr_2[i].dev_attr);
  1163. if (err)
  1164. goto exit_remove;
  1165. }
  1166. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
  1167. if (!(data->has_vid & (1 << i)))
  1168. continue;
  1169. err = device_create_file(dev, &w83793_vid[i].dev_attr);
  1170. if (err)
  1171. goto exit_remove;
  1172. }
  1173. if (data->has_vid) {
  1174. data->vrm = vid_which_vrm();
  1175. err = device_create_file(dev, &dev_attr_vrm);
  1176. if (err)
  1177. goto exit_remove;
  1178. }
  1179. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
  1180. err = device_create_file(dev, &sda_single_files[i].dev_attr);
  1181. if (err)
  1182. goto exit_remove;
  1183. }
  1184. for (i = 0; i < 6; i++) {
  1185. int j;
  1186. if (!(data->has_temp & (1 << i)))
  1187. continue;
  1188. for (j = 0; j < files_temp; j++) {
  1189. err = device_create_file(dev,
  1190. &w83793_temp[(i) * files_temp
  1191. + j].dev_attr);
  1192. if (err)
  1193. goto exit_remove;
  1194. }
  1195. }
  1196. for (i = 5; i < 12; i++) {
  1197. int j;
  1198. if (!(data->has_fan & (1 << i)))
  1199. continue;
  1200. for (j = 0; j < files_fan; j++) {
  1201. err = device_create_file(dev,
  1202. &w83793_left_fan[(i - 5) * files_fan
  1203. + j].dev_attr);
  1204. if (err)
  1205. goto exit_remove;
  1206. }
  1207. }
  1208. for (i = 3; i < 8; i++) {
  1209. int j;
  1210. if (!(data->has_pwm & (1 << i)))
  1211. continue;
  1212. for (j = 0; j < files_pwm; j++) {
  1213. err = device_create_file(dev,
  1214. &w83793_left_pwm[(i - 3) * files_pwm
  1215. + j].dev_attr);
  1216. if (err)
  1217. goto exit_remove;
  1218. }
  1219. }
  1220. data->hwmon_dev = hwmon_device_register(dev);
  1221. if (IS_ERR(data->hwmon_dev)) {
  1222. err = PTR_ERR(data->hwmon_dev);
  1223. goto exit_remove;
  1224. }
  1225. return 0;
  1226. /* Unregister sysfs hooks */
  1227. exit_remove:
  1228. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1229. device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
  1230. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1231. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1232. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1233. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1234. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1235. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1236. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1237. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1238. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1239. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1240. if (data->lm75[0] != NULL)
  1241. i2c_unregister_device(data->lm75[0]);
  1242. if (data->lm75[1] != NULL)
  1243. i2c_unregister_device(data->lm75[1]);
  1244. free_mem:
  1245. kfree(data);
  1246. exit:
  1247. return err;
  1248. }
  1249. static void w83793_update_nonvolatile(struct device *dev)
  1250. {
  1251. struct i2c_client *client = to_i2c_client(dev);
  1252. struct w83793_data *data = i2c_get_clientdata(client);
  1253. int i, j;
  1254. /*
  1255. They are somewhat "stable" registers, and to update them everytime
  1256. takes so much time, it's just not worthy. Update them in a long
  1257. interval to avoid exception.
  1258. */
  1259. if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
  1260. || !data->valid))
  1261. return;
  1262. /* update voltage limits */
  1263. for (i = 1; i < 3; i++) {
  1264. for (j = 0; j < ARRAY_SIZE(data->in); j++) {
  1265. data->in[j][i] =
  1266. w83793_read_value(client, W83793_REG_IN[j][i]);
  1267. }
  1268. data->in_low_bits[i] =
  1269. w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
  1270. }
  1271. for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
  1272. /* Update the Fan measured value and limits */
  1273. if (!(data->has_fan & (1 << i))) {
  1274. continue;
  1275. }
  1276. data->fan_min[i] =
  1277. w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
  1278. data->fan_min[i] |=
  1279. w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
  1280. }
  1281. for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
  1282. if (!(data->has_temp & (1 << i)))
  1283. continue;
  1284. data->temp_fan_map[i] =
  1285. w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
  1286. for (j = 1; j < 5; j++) {
  1287. data->temp[i][j] =
  1288. w83793_read_value(client, W83793_REG_TEMP[i][j]);
  1289. }
  1290. data->temp_cruise[i] =
  1291. w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
  1292. for (j = 0; j < 7; j++) {
  1293. data->sf2_pwm[i][j] =
  1294. w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
  1295. data->sf2_temp[i][j] =
  1296. w83793_read_value(client,
  1297. W83793_REG_SF2_TEMP(i, j));
  1298. }
  1299. }
  1300. for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
  1301. data->temp_mode[i] =
  1302. w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
  1303. for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
  1304. data->tolerance[i] =
  1305. w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  1306. }
  1307. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1308. if (!(data->has_pwm & (1 << i)))
  1309. continue;
  1310. data->pwm[i][PWM_NONSTOP] =
  1311. w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
  1312. data->pwm[i][PWM_START] =
  1313. w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
  1314. data->pwm_stop_time[i] =
  1315. w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
  1316. }
  1317. data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
  1318. data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
  1319. data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
  1320. data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
  1321. data->temp_critical =
  1322. w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
  1323. data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
  1324. for (i = 0; i < ARRAY_SIZE(data->beeps); i++) {
  1325. data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
  1326. }
  1327. data->last_nonvolatile = jiffies;
  1328. }
  1329. static struct w83793_data *w83793_update_device(struct device *dev)
  1330. {
  1331. struct i2c_client *client = to_i2c_client(dev);
  1332. struct w83793_data *data = i2c_get_clientdata(client);
  1333. int i;
  1334. mutex_lock(&data->update_lock);
  1335. if (!(time_after(jiffies, data->last_updated + HZ * 2)
  1336. || !data->valid))
  1337. goto END;
  1338. /* Update the voltages measured value and limits */
  1339. for (i = 0; i < ARRAY_SIZE(data->in); i++)
  1340. data->in[i][IN_READ] =
  1341. w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
  1342. data->in_low_bits[IN_READ] =
  1343. w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
  1344. for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
  1345. if (!(data->has_fan & (1 << i))) {
  1346. continue;
  1347. }
  1348. data->fan[i] =
  1349. w83793_read_value(client, W83793_REG_FAN(i)) << 8;
  1350. data->fan[i] |=
  1351. w83793_read_value(client, W83793_REG_FAN(i) + 1);
  1352. }
  1353. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  1354. if (!(data->has_temp & (1 << i)))
  1355. continue;
  1356. data->temp[i][TEMP_READ] =
  1357. w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
  1358. }
  1359. data->temp_low_bits =
  1360. w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
  1361. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1362. if (data->has_pwm & (1 << i))
  1363. data->pwm[i][PWM_DUTY] =
  1364. w83793_read_value(client,
  1365. W83793_REG_PWM(i, PWM_DUTY));
  1366. }
  1367. for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
  1368. data->alarms[i] =
  1369. w83793_read_value(client, W83793_REG_ALARM(i));
  1370. if (data->has_vid & 0x01)
  1371. data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
  1372. if (data->has_vid & 0x02)
  1373. data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
  1374. w83793_update_nonvolatile(dev);
  1375. data->last_updated = jiffies;
  1376. data->valid = 1;
  1377. END:
  1378. mutex_unlock(&data->update_lock);
  1379. return data;
  1380. }
  1381. /* Ignore the possibility that somebody change bank outside the driver
  1382. Must be called with data->update_lock held, except during initialization */
  1383. static u8 w83793_read_value(struct i2c_client *client, u16 reg)
  1384. {
  1385. struct w83793_data *data = i2c_get_clientdata(client);
  1386. u8 res = 0xff;
  1387. u8 new_bank = reg >> 8;
  1388. new_bank |= data->bank & 0xfc;
  1389. if (data->bank != new_bank) {
  1390. if (i2c_smbus_write_byte_data
  1391. (client, W83793_REG_BANKSEL, new_bank) >= 0)
  1392. data->bank = new_bank;
  1393. else {
  1394. dev_err(&client->dev,
  1395. "set bank to %d failed, fall back "
  1396. "to bank %d, read reg 0x%x error\n",
  1397. new_bank, data->bank, reg);
  1398. res = 0x0; /* read 0x0 from the chip */
  1399. goto END;
  1400. }
  1401. }
  1402. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  1403. END:
  1404. return res;
  1405. }
  1406. /* Must be called with data->update_lock held, except during initialization */
  1407. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
  1408. {
  1409. struct w83793_data *data = i2c_get_clientdata(client);
  1410. int res;
  1411. u8 new_bank = reg >> 8;
  1412. new_bank |= data->bank & 0xfc;
  1413. if (data->bank != new_bank) {
  1414. if ((res = i2c_smbus_write_byte_data
  1415. (client, W83793_REG_BANKSEL, new_bank)) >= 0)
  1416. data->bank = new_bank;
  1417. else {
  1418. dev_err(&client->dev,
  1419. "set bank to %d failed, fall back "
  1420. "to bank %d, write reg 0x%x error\n",
  1421. new_bank, data->bank, reg);
  1422. goto END;
  1423. }
  1424. }
  1425. res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
  1426. END:
  1427. return res;
  1428. }
  1429. static int __init sensors_w83793_init(void)
  1430. {
  1431. return i2c_add_driver(&w83793_driver);
  1432. }
  1433. static void __exit sensors_w83793_exit(void)
  1434. {
  1435. i2c_del_driver(&w83793_driver);
  1436. }
  1437. MODULE_AUTHOR("Yuan Mu");
  1438. MODULE_DESCRIPTION("w83793 driver");
  1439. MODULE_LICENSE("GPL");
  1440. module_init(sensors_w83793_init);
  1441. module_exit(sensors_w83793_exit);