w83793.c 50 KB

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