w83793.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  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. * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
  7. * Watchdog driver part
  8. * (Based partially on fschmd driver,
  9. * Copyright 2007-2008 by Hans de Goede)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation - version 2.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301 USA.
  24. */
  25. /*
  26. * Supports following chips:
  27. *
  28. * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  29. * w83793 10 12 8 6 0x7b 0x5ca3 yes no
  30. */
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/i2c.h>
  35. #include <linux/hwmon.h>
  36. #include <linux/hwmon-vid.h>
  37. #include <linux/hwmon-sysfs.h>
  38. #include <linux/err.h>
  39. #include <linux/mutex.h>
  40. #include <linux/fs.h>
  41. #include <linux/watchdog.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/kref.h>
  45. #include <linux/notifier.h>
  46. #include <linux/reboot.h>
  47. /* Default values */
  48. #define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
  49. /* Addresses to scan */
  50. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  51. I2C_CLIENT_END };
  52. /* Insmod parameters */
  53. static unsigned short force_subclients[4];
  54. module_param_array(force_subclients, short, NULL, 0);
  55. MODULE_PARM_DESC(force_subclients, "List of subclient addresses: "
  56. "{bus, clientaddr, subclientaddr1, subclientaddr2}");
  57. static bool reset;
  58. module_param(reset, bool, 0);
  59. MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
  60. static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
  61. module_param(timeout, int, 0);
  62. MODULE_PARM_DESC(timeout,
  63. "Watchdog timeout in minutes. 2<= timeout <=255 (default="
  64. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  65. static bool nowayout = WATCHDOG_NOWAYOUT;
  66. module_param(nowayout, bool, 0);
  67. MODULE_PARM_DESC(nowayout,
  68. "Watchdog cannot be stopped once started (default="
  69. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  70. /*
  71. * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
  72. * as ID, Bank Select registers
  73. */
  74. #define W83793_REG_BANKSEL 0x00
  75. #define W83793_REG_VENDORID 0x0d
  76. #define W83793_REG_CHIPID 0x0e
  77. #define W83793_REG_DEVICEID 0x0f
  78. #define W83793_REG_CONFIG 0x40
  79. #define W83793_REG_MFC 0x58
  80. #define W83793_REG_FANIN_CTRL 0x5c
  81. #define W83793_REG_FANIN_SEL 0x5d
  82. #define W83793_REG_I2C_ADDR 0x0b
  83. #define W83793_REG_I2C_SUBADDR 0x0c
  84. #define W83793_REG_VID_INA 0x05
  85. #define W83793_REG_VID_INB 0x06
  86. #define W83793_REG_VID_LATCHA 0x07
  87. #define W83793_REG_VID_LATCHB 0x08
  88. #define W83793_REG_VID_CTRL 0x59
  89. #define W83793_REG_WDT_LOCK 0x01
  90. #define W83793_REG_WDT_ENABLE 0x02
  91. #define W83793_REG_WDT_STATUS 0x03
  92. #define W83793_REG_WDT_TIMEOUT 0x04
  93. static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
  94. #define TEMP_READ 0
  95. #define TEMP_CRIT 1
  96. #define TEMP_CRIT_HYST 2
  97. #define TEMP_WARN 3
  98. #define TEMP_WARN_HYST 4
  99. /*
  100. * only crit and crit_hyst affect real-time alarm status
  101. * current crit crit_hyst warn warn_hyst
  102. */
  103. static u16 W83793_REG_TEMP[][5] = {
  104. {0x1c, 0x78, 0x79, 0x7a, 0x7b},
  105. {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
  106. {0x1e, 0x80, 0x81, 0x82, 0x83},
  107. {0x1f, 0x84, 0x85, 0x86, 0x87},
  108. {0x20, 0x88, 0x89, 0x8a, 0x8b},
  109. {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
  110. };
  111. #define W83793_REG_TEMP_LOW_BITS 0x22
  112. #define W83793_REG_BEEP(index) (0x53 + (index))
  113. #define W83793_REG_ALARM(index) (0x4b + (index))
  114. #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
  115. #define W83793_REG_IRQ_CTRL 0x50
  116. #define W83793_REG_OVT_CTRL 0x51
  117. #define W83793_REG_OVT_BEEP 0x52
  118. #define IN_READ 0
  119. #define IN_MAX 1
  120. #define IN_LOW 2
  121. static const u16 W83793_REG_IN[][3] = {
  122. /* Current, High, Low */
  123. {0x10, 0x60, 0x61}, /* Vcore A */
  124. {0x11, 0x62, 0x63}, /* Vcore B */
  125. {0x12, 0x64, 0x65}, /* Vtt */
  126. {0x14, 0x6a, 0x6b}, /* VSEN1 */
  127. {0x15, 0x6c, 0x6d}, /* VSEN2 */
  128. {0x16, 0x6e, 0x6f}, /* +3VSEN */
  129. {0x17, 0x70, 0x71}, /* +12VSEN */
  130. {0x18, 0x72, 0x73}, /* 5VDD */
  131. {0x19, 0x74, 0x75}, /* 5VSB */
  132. {0x1a, 0x76, 0x77}, /* VBAT */
  133. };
  134. /* Low Bits of Vcore A/B Vtt Read/High/Low */
  135. static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
  136. static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
  137. static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
  138. #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
  139. #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
  140. #define W83793_REG_PWM_DEFAULT 0xb2
  141. #define W83793_REG_PWM_ENABLE 0x207
  142. #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
  143. #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
  144. #define W83793_REG_TEMP_CRITICAL 0xc5
  145. #define PWM_DUTY 0
  146. #define PWM_START 1
  147. #define PWM_NONSTOP 2
  148. #define PWM_STOP_TIME 3
  149. #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
  150. (nr) == 1 ? 0x220 : 0x218) + (index))
  151. /* bit field, fan1 is bit0, fan2 is bit1 ... */
  152. #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
  153. #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
  154. #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
  155. #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
  156. #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
  157. #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
  158. static inline unsigned long FAN_FROM_REG(u16 val)
  159. {
  160. if ((val >= 0xfff) || (val == 0))
  161. return 0;
  162. return 1350000UL / val;
  163. }
  164. static inline u16 FAN_TO_REG(long rpm)
  165. {
  166. if (rpm <= 0)
  167. return 0x0fff;
  168. return SENSORS_LIMIT((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
  169. }
  170. static inline unsigned long TIME_FROM_REG(u8 reg)
  171. {
  172. return reg * 100;
  173. }
  174. static inline u8 TIME_TO_REG(unsigned long val)
  175. {
  176. return SENSORS_LIMIT((val + 50) / 100, 0, 0xff);
  177. }
  178. static inline long TEMP_FROM_REG(s8 reg)
  179. {
  180. return reg * 1000;
  181. }
  182. static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
  183. {
  184. return SENSORS_LIMIT((val + (val < 0 ? -500 : 500)) / 1000, min, max);
  185. }
  186. struct w83793_data {
  187. struct i2c_client *lm75[2];
  188. struct device *hwmon_dev;
  189. struct mutex update_lock;
  190. char valid; /* !=0 if following fields are valid */
  191. unsigned long last_updated; /* In jiffies */
  192. unsigned long last_nonvolatile; /* In jiffies, last time we update the
  193. * nonvolatile registers
  194. */
  195. u8 bank;
  196. u8 vrm;
  197. u8 vid[2];
  198. u8 in[10][3]; /* Register value, read/high/low */
  199. u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
  200. u16 has_fan; /* Only fan1- fan5 has own pins */
  201. u16 fan[12]; /* Register value combine */
  202. u16 fan_min[12]; /* Register value combine */
  203. s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
  204. u8 temp_low_bits; /* Additional resolution TD1-TD4 */
  205. u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
  206. * byte 1: Temp R1,R2 mode, each has 1 bit
  207. */
  208. u8 temp_critical; /* If reached all fan will be at full speed */
  209. u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
  210. u8 has_pwm;
  211. u8 has_temp;
  212. u8 has_vid;
  213. u8 pwm_enable; /* Register value, each Temp has 1 bit */
  214. u8 pwm_uptime; /* Register value */
  215. u8 pwm_downtime; /* Register value */
  216. u8 pwm_default; /* All fan default pwm, next poweron valid */
  217. u8 pwm[8][3]; /* Register value */
  218. u8 pwm_stop_time[8];
  219. u8 temp_cruise[6];
  220. u8 alarms[5]; /* realtime status registers */
  221. u8 beeps[5];
  222. u8 beep_enable;
  223. u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
  224. u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
  225. u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
  226. /* watchdog */
  227. struct i2c_client *client;
  228. struct mutex watchdog_lock;
  229. struct list_head list; /* member of the watchdog_data_list */
  230. struct kref kref;
  231. struct miscdevice watchdog_miscdev;
  232. unsigned long watchdog_is_open;
  233. char watchdog_expect_close;
  234. char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
  235. unsigned int watchdog_caused_reboot;
  236. int watchdog_timeout; /* watchdog timeout in minutes */
  237. };
  238. /*
  239. * Somewhat ugly :( global data pointer list with all devices, so that
  240. * we can find our device data as when using misc_register. There is no
  241. * other method to get to one's device data from the open file-op and
  242. * for usage in the reboot notifier callback.
  243. */
  244. static LIST_HEAD(watchdog_data_list);
  245. /* Note this lock not only protect list access, but also data.kref access */
  246. static DEFINE_MUTEX(watchdog_data_mutex);
  247. /*
  248. * Release our data struct when we're detached from the i2c client *and* all
  249. * references to our watchdog device are released
  250. */
  251. static void w83793_release_resources(struct kref *ref)
  252. {
  253. struct w83793_data *data = container_of(ref, struct w83793_data, kref);
  254. kfree(data);
  255. }
  256. static u8 w83793_read_value(struct i2c_client *client, u16 reg);
  257. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
  258. static int w83793_probe(struct i2c_client *client,
  259. const struct i2c_device_id *id);
  260. static int w83793_detect(struct i2c_client *client,
  261. struct i2c_board_info *info);
  262. static int w83793_remove(struct i2c_client *client);
  263. static void w83793_init_client(struct i2c_client *client);
  264. static void w83793_update_nonvolatile(struct device *dev);
  265. static struct w83793_data *w83793_update_device(struct device *dev);
  266. static const struct i2c_device_id w83793_id[] = {
  267. { "w83793", 0 },
  268. { }
  269. };
  270. MODULE_DEVICE_TABLE(i2c, w83793_id);
  271. static struct i2c_driver w83793_driver = {
  272. .class = I2C_CLASS_HWMON,
  273. .driver = {
  274. .name = "w83793",
  275. },
  276. .probe = w83793_probe,
  277. .remove = w83793_remove,
  278. .id_table = w83793_id,
  279. .detect = w83793_detect,
  280. .address_list = normal_i2c,
  281. };
  282. static ssize_t
  283. show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  284. {
  285. struct w83793_data *data = dev_get_drvdata(dev);
  286. return sprintf(buf, "%d\n", data->vrm);
  287. }
  288. static ssize_t
  289. show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  290. {
  291. struct w83793_data *data = w83793_update_device(dev);
  292. struct sensor_device_attribute_2 *sensor_attr =
  293. to_sensor_dev_attr_2(attr);
  294. int index = sensor_attr->index;
  295. return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
  296. }
  297. static ssize_t
  298. store_vrm(struct device *dev, struct device_attribute *attr,
  299. const char *buf, size_t count)
  300. {
  301. struct w83793_data *data = dev_get_drvdata(dev);
  302. unsigned long val;
  303. int err;
  304. err = kstrtoul(buf, 10, &val);
  305. if (err)
  306. return err;
  307. data->vrm = val;
  308. return count;
  309. }
  310. #define ALARM_STATUS 0
  311. #define BEEP_ENABLE 1
  312. static ssize_t
  313. show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
  314. {
  315. struct w83793_data *data = w83793_update_device(dev);
  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 >> 3;
  320. int bit = sensor_attr->index & 0x07;
  321. u8 val;
  322. if (nr == ALARM_STATUS) {
  323. val = (data->alarms[index] >> (bit)) & 1;
  324. } else { /* BEEP_ENABLE */
  325. val = (data->beeps[index] >> (bit)) & 1;
  326. }
  327. return sprintf(buf, "%u\n", val);
  328. }
  329. static ssize_t
  330. store_beep(struct device *dev, struct device_attribute *attr,
  331. const char *buf, size_t count)
  332. {
  333. struct i2c_client *client = to_i2c_client(dev);
  334. struct w83793_data *data = i2c_get_clientdata(client);
  335. struct sensor_device_attribute_2 *sensor_attr =
  336. to_sensor_dev_attr_2(attr);
  337. int index = sensor_attr->index >> 3;
  338. int shift = sensor_attr->index & 0x07;
  339. u8 beep_bit = 1 << shift;
  340. unsigned long val;
  341. int err;
  342. err = kstrtoul(buf, 10, &val);
  343. if (err)
  344. return err;
  345. if (val > 1)
  346. return -EINVAL;
  347. mutex_lock(&data->update_lock);
  348. data->beeps[index] = w83793_read_value(client, W83793_REG_BEEP(index));
  349. data->beeps[index] &= ~beep_bit;
  350. data->beeps[index] |= val << shift;
  351. w83793_write_value(client, W83793_REG_BEEP(index), data->beeps[index]);
  352. mutex_unlock(&data->update_lock);
  353. return count;
  354. }
  355. static ssize_t
  356. show_beep_enable(struct device *dev, struct device_attribute *attr, char *buf)
  357. {
  358. struct w83793_data *data = w83793_update_device(dev);
  359. return sprintf(buf, "%u\n", (data->beep_enable >> 1) & 0x01);
  360. }
  361. static ssize_t
  362. store_beep_enable(struct device *dev, struct device_attribute *attr,
  363. const char *buf, size_t count)
  364. {
  365. struct i2c_client *client = to_i2c_client(dev);
  366. struct w83793_data *data = i2c_get_clientdata(client);
  367. unsigned long val;
  368. int err;
  369. err = kstrtoul(buf, 10, &val);
  370. if (err)
  371. return err;
  372. if (val > 1)
  373. return -EINVAL;
  374. mutex_lock(&data->update_lock);
  375. data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP)
  376. & 0xfd;
  377. data->beep_enable |= val << 1;
  378. w83793_write_value(client, W83793_REG_OVT_BEEP, data->beep_enable);
  379. mutex_unlock(&data->update_lock);
  380. return count;
  381. }
  382. /* Write 0 to clear chassis alarm */
  383. static ssize_t
  384. store_chassis_clear(struct device *dev,
  385. struct device_attribute *attr, const char *buf,
  386. size_t count)
  387. {
  388. struct i2c_client *client = to_i2c_client(dev);
  389. struct w83793_data *data = i2c_get_clientdata(client);
  390. unsigned long val;
  391. u8 reg;
  392. int err;
  393. err = kstrtoul(buf, 10, &val);
  394. if (err)
  395. return err;
  396. if (val)
  397. return -EINVAL;
  398. mutex_lock(&data->update_lock);
  399. reg = w83793_read_value(client, W83793_REG_CLR_CHASSIS);
  400. w83793_write_value(client, W83793_REG_CLR_CHASSIS, reg | 0x80);
  401. data->valid = 0; /* Force cache refresh */
  402. mutex_unlock(&data->update_lock);
  403. return count;
  404. }
  405. #define FAN_INPUT 0
  406. #define FAN_MIN 1
  407. static ssize_t
  408. show_fan(struct device *dev, struct device_attribute *attr, char *buf)
  409. {
  410. struct sensor_device_attribute_2 *sensor_attr =
  411. to_sensor_dev_attr_2(attr);
  412. int nr = sensor_attr->nr;
  413. int index = sensor_attr->index;
  414. struct w83793_data *data = w83793_update_device(dev);
  415. u16 val;
  416. if (nr == FAN_INPUT)
  417. val = data->fan[index] & 0x0fff;
  418. else
  419. val = data->fan_min[index] & 0x0fff;
  420. return sprintf(buf, "%lu\n", FAN_FROM_REG(val));
  421. }
  422. static ssize_t
  423. store_fan_min(struct device *dev, struct device_attribute *attr,
  424. const char *buf, size_t count)
  425. {
  426. struct sensor_device_attribute_2 *sensor_attr =
  427. to_sensor_dev_attr_2(attr);
  428. int index = sensor_attr->index;
  429. struct i2c_client *client = to_i2c_client(dev);
  430. struct w83793_data *data = i2c_get_clientdata(client);
  431. unsigned long val;
  432. int err;
  433. err = kstrtoul(buf, 10, &val);
  434. if (err)
  435. return err;
  436. val = FAN_TO_REG(val);
  437. mutex_lock(&data->update_lock);
  438. data->fan_min[index] = val;
  439. w83793_write_value(client, W83793_REG_FAN_MIN(index),
  440. (val >> 8) & 0xff);
  441. w83793_write_value(client, W83793_REG_FAN_MIN(index) + 1, val & 0xff);
  442. mutex_unlock(&data->update_lock);
  443. return count;
  444. }
  445. static ssize_t
  446. show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  447. {
  448. struct sensor_device_attribute_2 *sensor_attr =
  449. to_sensor_dev_attr_2(attr);
  450. struct w83793_data *data = w83793_update_device(dev);
  451. u16 val;
  452. int nr = sensor_attr->nr;
  453. int index = sensor_attr->index;
  454. if (nr == PWM_STOP_TIME)
  455. val = TIME_FROM_REG(data->pwm_stop_time[index]);
  456. else
  457. val = (data->pwm[index][nr] & 0x3f) << 2;
  458. return sprintf(buf, "%d\n", val);
  459. }
  460. static ssize_t
  461. store_pwm(struct device *dev, struct device_attribute *attr,
  462. const char *buf, size_t count)
  463. {
  464. struct i2c_client *client = to_i2c_client(dev);
  465. struct w83793_data *data = i2c_get_clientdata(client);
  466. struct sensor_device_attribute_2 *sensor_attr =
  467. to_sensor_dev_attr_2(attr);
  468. int nr = sensor_attr->nr;
  469. int index = sensor_attr->index;
  470. unsigned long val;
  471. int err;
  472. err = kstrtoul(buf, 10, &val);
  473. if (err)
  474. return err;
  475. mutex_lock(&data->update_lock);
  476. if (nr == PWM_STOP_TIME) {
  477. val = TIME_TO_REG(val);
  478. data->pwm_stop_time[index] = val;
  479. w83793_write_value(client, W83793_REG_PWM_STOP_TIME(index),
  480. val);
  481. } else {
  482. val = SENSORS_LIMIT(val, 0, 0xff) >> 2;
  483. data->pwm[index][nr] =
  484. w83793_read_value(client, W83793_REG_PWM(index, nr)) & 0xc0;
  485. data->pwm[index][nr] |= val;
  486. w83793_write_value(client, W83793_REG_PWM(index, nr),
  487. data->pwm[index][nr]);
  488. }
  489. mutex_unlock(&data->update_lock);
  490. return count;
  491. }
  492. static ssize_t
  493. show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  494. {
  495. struct sensor_device_attribute_2 *sensor_attr =
  496. to_sensor_dev_attr_2(attr);
  497. int nr = sensor_attr->nr;
  498. int index = sensor_attr->index;
  499. struct w83793_data *data = w83793_update_device(dev);
  500. long temp = TEMP_FROM_REG(data->temp[index][nr]);
  501. if (nr == TEMP_READ && index < 4) { /* Only TD1-TD4 have low bits */
  502. int low = ((data->temp_low_bits >> (index * 2)) & 0x03) * 250;
  503. temp += temp > 0 ? low : -low;
  504. }
  505. return sprintf(buf, "%ld\n", temp);
  506. }
  507. static ssize_t
  508. store_temp(struct device *dev, struct device_attribute *attr,
  509. const char *buf, size_t count)
  510. {
  511. struct sensor_device_attribute_2 *sensor_attr =
  512. to_sensor_dev_attr_2(attr);
  513. int nr = sensor_attr->nr;
  514. int index = sensor_attr->index;
  515. struct i2c_client *client = to_i2c_client(dev);
  516. struct w83793_data *data = i2c_get_clientdata(client);
  517. long tmp;
  518. int err;
  519. err = kstrtol(buf, 10, &tmp);
  520. if (err)
  521. return err;
  522. mutex_lock(&data->update_lock);
  523. data->temp[index][nr] = TEMP_TO_REG(tmp, -128, 127);
  524. w83793_write_value(client, W83793_REG_TEMP[index][nr],
  525. data->temp[index][nr]);
  526. mutex_unlock(&data->update_lock);
  527. return count;
  528. }
  529. /*
  530. * TD1-TD4
  531. * each has 4 mode:(2 bits)
  532. * 0: Stop monitor
  533. * 1: Use internal temp sensor(default)
  534. * 2: Reserved
  535. * 3: Use sensor in Intel CPU and get result by PECI
  536. *
  537. * TR1-TR2
  538. * each has 2 mode:(1 bit)
  539. * 0: Disable temp sensor monitor
  540. * 1: To enable temp sensors monitor
  541. */
  542. /* 0 disable, 6 PECI */
  543. static u8 TO_TEMP_MODE[] = { 0, 0, 0, 6 };
  544. static ssize_t
  545. show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
  546. {
  547. struct w83793_data *data = w83793_update_device(dev);
  548. struct sensor_device_attribute_2 *sensor_attr =
  549. to_sensor_dev_attr_2(attr);
  550. int index = sensor_attr->index;
  551. u8 mask = (index < 4) ? 0x03 : 0x01;
  552. u8 shift = (index < 4) ? (2 * index) : (index - 4);
  553. u8 tmp;
  554. index = (index < 4) ? 0 : 1;
  555. tmp = (data->temp_mode[index] >> shift) & mask;
  556. /* for the internal sensor, found out if diode or thermistor */
  557. if (tmp == 1)
  558. tmp = index == 0 ? 3 : 4;
  559. else
  560. tmp = TO_TEMP_MODE[tmp];
  561. return sprintf(buf, "%d\n", tmp);
  562. }
  563. static ssize_t
  564. store_temp_mode(struct device *dev, struct device_attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. struct i2c_client *client = to_i2c_client(dev);
  568. struct w83793_data *data = i2c_get_clientdata(client);
  569. struct sensor_device_attribute_2 *sensor_attr =
  570. to_sensor_dev_attr_2(attr);
  571. int index = sensor_attr->index;
  572. u8 mask = (index < 4) ? 0x03 : 0x01;
  573. u8 shift = (index < 4) ? (2 * index) : (index - 4);
  574. unsigned long val;
  575. int err;
  576. err = kstrtoul(buf, 10, &val);
  577. if (err)
  578. return err;
  579. /* transform the sysfs interface values into table above */
  580. if ((val == 6) && (index < 4)) {
  581. val -= 3;
  582. } else if ((val == 3 && index < 4)
  583. || (val == 4 && index >= 4)) {
  584. /* transform diode or thermistor into internal enable */
  585. val = !!val;
  586. } else {
  587. return -EINVAL;
  588. }
  589. index = (index < 4) ? 0 : 1;
  590. mutex_lock(&data->update_lock);
  591. data->temp_mode[index] =
  592. w83793_read_value(client, W83793_REG_TEMP_MODE[index]);
  593. data->temp_mode[index] &= ~(mask << shift);
  594. data->temp_mode[index] |= val << shift;
  595. w83793_write_value(client, W83793_REG_TEMP_MODE[index],
  596. data->temp_mode[index]);
  597. mutex_unlock(&data->update_lock);
  598. return count;
  599. }
  600. #define SETUP_PWM_DEFAULT 0
  601. #define SETUP_PWM_UPTIME 1 /* Unit in 0.1s */
  602. #define SETUP_PWM_DOWNTIME 2 /* Unit in 0.1s */
  603. #define SETUP_TEMP_CRITICAL 3
  604. static ssize_t
  605. show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
  606. {
  607. struct sensor_device_attribute_2 *sensor_attr =
  608. to_sensor_dev_attr_2(attr);
  609. int nr = sensor_attr->nr;
  610. struct w83793_data *data = w83793_update_device(dev);
  611. u32 val = 0;
  612. if (nr == SETUP_PWM_DEFAULT)
  613. val = (data->pwm_default & 0x3f) << 2;
  614. else if (nr == SETUP_PWM_UPTIME)
  615. val = TIME_FROM_REG(data->pwm_uptime);
  616. else if (nr == SETUP_PWM_DOWNTIME)
  617. val = TIME_FROM_REG(data->pwm_downtime);
  618. else if (nr == SETUP_TEMP_CRITICAL)
  619. val = TEMP_FROM_REG(data->temp_critical & 0x7f);
  620. return sprintf(buf, "%d\n", val);
  621. }
  622. static ssize_t
  623. store_sf_setup(struct device *dev, struct device_attribute *attr,
  624. const char *buf, size_t count)
  625. {
  626. struct sensor_device_attribute_2 *sensor_attr =
  627. to_sensor_dev_attr_2(attr);
  628. int nr = sensor_attr->nr;
  629. struct i2c_client *client = to_i2c_client(dev);
  630. struct w83793_data *data = i2c_get_clientdata(client);
  631. long val;
  632. int err;
  633. err = kstrtol(buf, 10, &val);
  634. if (err)
  635. return err;
  636. mutex_lock(&data->update_lock);
  637. if (nr == SETUP_PWM_DEFAULT) {
  638. data->pwm_default =
  639. w83793_read_value(client, W83793_REG_PWM_DEFAULT) & 0xc0;
  640. data->pwm_default |= SENSORS_LIMIT(val, 0, 0xff) >> 2;
  641. w83793_write_value(client, W83793_REG_PWM_DEFAULT,
  642. data->pwm_default);
  643. } else if (nr == SETUP_PWM_UPTIME) {
  644. data->pwm_uptime = TIME_TO_REG(val);
  645. data->pwm_uptime += data->pwm_uptime == 0 ? 1 : 0;
  646. w83793_write_value(client, W83793_REG_PWM_UPTIME,
  647. data->pwm_uptime);
  648. } else if (nr == SETUP_PWM_DOWNTIME) {
  649. data->pwm_downtime = TIME_TO_REG(val);
  650. data->pwm_downtime += data->pwm_downtime == 0 ? 1 : 0;
  651. w83793_write_value(client, W83793_REG_PWM_DOWNTIME,
  652. data->pwm_downtime);
  653. } else { /* SETUP_TEMP_CRITICAL */
  654. data->temp_critical =
  655. w83793_read_value(client, W83793_REG_TEMP_CRITICAL) & 0x80;
  656. data->temp_critical |= TEMP_TO_REG(val, 0, 0x7f);
  657. w83793_write_value(client, W83793_REG_TEMP_CRITICAL,
  658. data->temp_critical);
  659. }
  660. mutex_unlock(&data->update_lock);
  661. return count;
  662. }
  663. /*
  664. * Temp SmartFan control
  665. * TEMP_FAN_MAP
  666. * Temp channel control which pwm fan, bitfield, bit 0 indicate pwm1...
  667. * It's possible two or more temp channels control the same fan, w83793
  668. * always prefers to pick the most critical request and applies it to
  669. * the related Fan.
  670. * It's possible one fan is not in any mapping of 6 temp channels, this
  671. * means the fan is manual mode
  672. *
  673. * TEMP_PWM_ENABLE
  674. * Each temp channel has its own SmartFan mode, and temp channel
  675. * control fans that are set by TEMP_FAN_MAP
  676. * 0: SmartFanII mode
  677. * 1: Thermal Cruise Mode
  678. *
  679. * TEMP_CRUISE
  680. * Target temperature in thermal cruise mode, w83793 will try to turn
  681. * fan speed to keep the temperature of target device around this
  682. * temperature.
  683. *
  684. * TEMP_TOLERANCE
  685. * If Temp higher or lower than target with this tolerance, w83793
  686. * will take actions to speed up or slow down the fan to keep the
  687. * temperature within the tolerance range.
  688. */
  689. #define TEMP_FAN_MAP 0
  690. #define TEMP_PWM_ENABLE 1
  691. #define TEMP_CRUISE 2
  692. #define TEMP_TOLERANCE 3
  693. static ssize_t
  694. show_sf_ctrl(struct device *dev, struct device_attribute *attr, char *buf)
  695. {
  696. struct sensor_device_attribute_2 *sensor_attr =
  697. to_sensor_dev_attr_2(attr);
  698. int nr = sensor_attr->nr;
  699. int index = sensor_attr->index;
  700. struct w83793_data *data = w83793_update_device(dev);
  701. u32 val;
  702. if (nr == TEMP_FAN_MAP) {
  703. val = data->temp_fan_map[index];
  704. } else if (nr == TEMP_PWM_ENABLE) {
  705. /* +2 to transfrom into 2 and 3 to conform with sysfs intf */
  706. val = ((data->pwm_enable >> index) & 0x01) + 2;
  707. } else if (nr == TEMP_CRUISE) {
  708. val = TEMP_FROM_REG(data->temp_cruise[index] & 0x7f);
  709. } else { /* TEMP_TOLERANCE */
  710. val = data->tolerance[index >> 1] >> ((index & 0x01) ? 4 : 0);
  711. val = TEMP_FROM_REG(val & 0x0f);
  712. }
  713. return sprintf(buf, "%d\n", val);
  714. }
  715. static ssize_t
  716. store_sf_ctrl(struct device *dev, struct device_attribute *attr,
  717. const char *buf, size_t count)
  718. {
  719. struct sensor_device_attribute_2 *sensor_attr =
  720. to_sensor_dev_attr_2(attr);
  721. int nr = sensor_attr->nr;
  722. int index = sensor_attr->index;
  723. struct i2c_client *client = to_i2c_client(dev);
  724. struct w83793_data *data = i2c_get_clientdata(client);
  725. long val;
  726. int err;
  727. err = kstrtol(buf, 10, &val);
  728. if (err)
  729. return err;
  730. mutex_lock(&data->update_lock);
  731. if (nr == TEMP_FAN_MAP) {
  732. val = SENSORS_LIMIT(val, 0, 255);
  733. w83793_write_value(client, W83793_REG_TEMP_FAN_MAP(index), val);
  734. data->temp_fan_map[index] = val;
  735. } else if (nr == TEMP_PWM_ENABLE) {
  736. if (val == 2 || val == 3) {
  737. data->pwm_enable =
  738. w83793_read_value(client, W83793_REG_PWM_ENABLE);
  739. if (val - 2)
  740. data->pwm_enable |= 1 << index;
  741. else
  742. data->pwm_enable &= ~(1 << index);
  743. w83793_write_value(client, W83793_REG_PWM_ENABLE,
  744. data->pwm_enable);
  745. } else {
  746. mutex_unlock(&data->update_lock);
  747. return -EINVAL;
  748. }
  749. } else if (nr == TEMP_CRUISE) {
  750. data->temp_cruise[index] =
  751. w83793_read_value(client, W83793_REG_TEMP_CRUISE(index));
  752. data->temp_cruise[index] &= 0x80;
  753. data->temp_cruise[index] |= TEMP_TO_REG(val, 0, 0x7f);
  754. w83793_write_value(client, W83793_REG_TEMP_CRUISE(index),
  755. data->temp_cruise[index]);
  756. } else { /* TEMP_TOLERANCE */
  757. int i = index >> 1;
  758. u8 shift = (index & 0x01) ? 4 : 0;
  759. data->tolerance[i] =
  760. w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  761. data->tolerance[i] &= ~(0x0f << shift);
  762. data->tolerance[i] |= TEMP_TO_REG(val, 0, 0x0f) << shift;
  763. w83793_write_value(client, W83793_REG_TEMP_TOL(i),
  764. data->tolerance[i]);
  765. }
  766. mutex_unlock(&data->update_lock);
  767. return count;
  768. }
  769. static ssize_t
  770. show_sf2_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  771. {
  772. struct sensor_device_attribute_2 *sensor_attr =
  773. to_sensor_dev_attr_2(attr);
  774. int nr = sensor_attr->nr;
  775. int index = sensor_attr->index;
  776. struct w83793_data *data = w83793_update_device(dev);
  777. return sprintf(buf, "%d\n", (data->sf2_pwm[index][nr] & 0x3f) << 2);
  778. }
  779. static ssize_t
  780. store_sf2_pwm(struct device *dev, struct device_attribute *attr,
  781. const char *buf, size_t count)
  782. {
  783. struct i2c_client *client = to_i2c_client(dev);
  784. struct w83793_data *data = i2c_get_clientdata(client);
  785. struct sensor_device_attribute_2 *sensor_attr =
  786. to_sensor_dev_attr_2(attr);
  787. int nr = sensor_attr->nr;
  788. int index = sensor_attr->index;
  789. unsigned long val;
  790. int err;
  791. err = kstrtoul(buf, 10, &val);
  792. if (err)
  793. return err;
  794. val = SENSORS_LIMIT(val, 0, 0xff) >> 2;
  795. mutex_lock(&data->update_lock);
  796. data->sf2_pwm[index][nr] =
  797. w83793_read_value(client, W83793_REG_SF2_PWM(index, nr)) & 0xc0;
  798. data->sf2_pwm[index][nr] |= val;
  799. w83793_write_value(client, W83793_REG_SF2_PWM(index, nr),
  800. data->sf2_pwm[index][nr]);
  801. mutex_unlock(&data->update_lock);
  802. return count;
  803. }
  804. static ssize_t
  805. show_sf2_temp(struct device *dev, struct device_attribute *attr, char *buf)
  806. {
  807. struct sensor_device_attribute_2 *sensor_attr =
  808. to_sensor_dev_attr_2(attr);
  809. int nr = sensor_attr->nr;
  810. int index = sensor_attr->index;
  811. struct w83793_data *data = w83793_update_device(dev);
  812. return sprintf(buf, "%ld\n",
  813. TEMP_FROM_REG(data->sf2_temp[index][nr] & 0x7f));
  814. }
  815. static ssize_t
  816. store_sf2_temp(struct device *dev, struct device_attribute *attr,
  817. const char *buf, size_t count)
  818. {
  819. struct i2c_client *client = to_i2c_client(dev);
  820. struct w83793_data *data = i2c_get_clientdata(client);
  821. struct sensor_device_attribute_2 *sensor_attr =
  822. to_sensor_dev_attr_2(attr);
  823. int nr = sensor_attr->nr;
  824. int index = sensor_attr->index;
  825. long val;
  826. int err;
  827. err = kstrtol(buf, 10, &val);
  828. if (err)
  829. return err;
  830. val = TEMP_TO_REG(val, 0, 0x7f);
  831. mutex_lock(&data->update_lock);
  832. data->sf2_temp[index][nr] =
  833. w83793_read_value(client, W83793_REG_SF2_TEMP(index, nr)) & 0x80;
  834. data->sf2_temp[index][nr] |= val;
  835. w83793_write_value(client, W83793_REG_SF2_TEMP(index, nr),
  836. data->sf2_temp[index][nr]);
  837. mutex_unlock(&data->update_lock);
  838. return count;
  839. }
  840. /* only Vcore A/B and Vtt have additional 2 bits precision */
  841. static ssize_t
  842. show_in(struct device *dev, struct device_attribute *attr, char *buf)
  843. {
  844. struct sensor_device_attribute_2 *sensor_attr =
  845. to_sensor_dev_attr_2(attr);
  846. int nr = sensor_attr->nr;
  847. int index = sensor_attr->index;
  848. struct w83793_data *data = w83793_update_device(dev);
  849. u16 val = data->in[index][nr];
  850. if (index < 3) {
  851. val <<= 2;
  852. val += (data->in_low_bits[nr] >> (index * 2)) & 0x3;
  853. }
  854. /* voltage inputs 5VDD and 5VSB needs 150mV offset */
  855. val = val * scale_in[index] + scale_in_add[index];
  856. return sprintf(buf, "%d\n", val);
  857. }
  858. static ssize_t
  859. store_in(struct device *dev, struct device_attribute *attr,
  860. const char *buf, size_t count)
  861. {
  862. struct sensor_device_attribute_2 *sensor_attr =
  863. to_sensor_dev_attr_2(attr);
  864. int nr = sensor_attr->nr;
  865. int index = sensor_attr->index;
  866. struct i2c_client *client = to_i2c_client(dev);
  867. struct w83793_data *data = i2c_get_clientdata(client);
  868. unsigned long val;
  869. int err;
  870. err = kstrtoul(buf, 10, &val);
  871. if (err)
  872. return err;
  873. val = (val + scale_in[index] / 2) / scale_in[index];
  874. mutex_lock(&data->update_lock);
  875. if (index > 2) {
  876. /* fix the limit values of 5VDD and 5VSB to ALARM mechanism */
  877. if (nr == 1 || nr == 2)
  878. val -= scale_in_add[index] / scale_in[index];
  879. val = SENSORS_LIMIT(val, 0, 255);
  880. } else {
  881. val = SENSORS_LIMIT(val, 0, 0x3FF);
  882. data->in_low_bits[nr] =
  883. w83793_read_value(client, W83793_REG_IN_LOW_BITS[nr]);
  884. data->in_low_bits[nr] &= ~(0x03 << (2 * index));
  885. data->in_low_bits[nr] |= (val & 0x03) << (2 * index);
  886. w83793_write_value(client, W83793_REG_IN_LOW_BITS[nr],
  887. data->in_low_bits[nr]);
  888. val >>= 2;
  889. }
  890. data->in[index][nr] = val;
  891. w83793_write_value(client, W83793_REG_IN[index][nr],
  892. data->in[index][nr]);
  893. mutex_unlock(&data->update_lock);
  894. return count;
  895. }
  896. #define NOT_USED -1
  897. #define SENSOR_ATTR_IN(index) \
  898. SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
  899. IN_READ, index), \
  900. SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
  901. store_in, IN_MAX, index), \
  902. SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
  903. store_in, IN_LOW, index), \
  904. SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
  905. NULL, ALARM_STATUS, index + ((index > 2) ? 1 : 0)), \
  906. SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
  907. show_alarm_beep, store_beep, BEEP_ENABLE, \
  908. index + ((index > 2) ? 1 : 0))
  909. #define SENSOR_ATTR_FAN(index) \
  910. SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
  911. NULL, ALARM_STATUS, index + 17), \
  912. SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
  913. show_alarm_beep, store_beep, BEEP_ENABLE, index + 17), \
  914. SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
  915. NULL, FAN_INPUT, index - 1), \
  916. SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
  917. show_fan, store_fan_min, FAN_MIN, index - 1)
  918. #define SENSOR_ATTR_PWM(index) \
  919. SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
  920. store_pwm, PWM_DUTY, index - 1), \
  921. SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
  922. show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
  923. SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
  924. show_pwm, store_pwm, PWM_START, index - 1), \
  925. SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
  926. show_pwm, store_pwm, PWM_STOP_TIME, index - 1)
  927. #define SENSOR_ATTR_TEMP(index) \
  928. SENSOR_ATTR_2(temp##index##_type, S_IRUGO | S_IWUSR, \
  929. show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
  930. SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
  931. NULL, TEMP_READ, index - 1), \
  932. SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
  933. store_temp, TEMP_CRIT, index - 1), \
  934. SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
  935. show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
  936. SENSOR_ATTR_2(temp##index##_warn, S_IRUGO | S_IWUSR, show_temp, \
  937. store_temp, TEMP_WARN, index - 1), \
  938. SENSOR_ATTR_2(temp##index##_warn_hyst, S_IRUGO | S_IWUSR, \
  939. show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
  940. SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
  941. show_alarm_beep, NULL, ALARM_STATUS, index + 11), \
  942. SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
  943. show_alarm_beep, store_beep, BEEP_ENABLE, index + 11), \
  944. SENSOR_ATTR_2(temp##index##_auto_channels_pwm, \
  945. S_IRUGO | S_IWUSR, show_sf_ctrl, store_sf_ctrl, \
  946. TEMP_FAN_MAP, index - 1), \
  947. SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
  948. show_sf_ctrl, store_sf_ctrl, TEMP_PWM_ENABLE, \
  949. index - 1), \
  950. SENSOR_ATTR_2(thermal_cruise##index, S_IRUGO | S_IWUSR, \
  951. show_sf_ctrl, store_sf_ctrl, TEMP_CRUISE, index - 1), \
  952. SENSOR_ATTR_2(tolerance##index, S_IRUGO | S_IWUSR, show_sf_ctrl,\
  953. store_sf_ctrl, TEMP_TOLERANCE, index - 1), \
  954. SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
  955. show_sf2_pwm, store_sf2_pwm, 0, index - 1), \
  956. SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
  957. show_sf2_pwm, store_sf2_pwm, 1, index - 1), \
  958. SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
  959. show_sf2_pwm, store_sf2_pwm, 2, index - 1), \
  960. SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
  961. show_sf2_pwm, store_sf2_pwm, 3, index - 1), \
  962. SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
  963. show_sf2_pwm, store_sf2_pwm, 4, index - 1), \
  964. SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
  965. show_sf2_pwm, store_sf2_pwm, 5, index - 1), \
  966. SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
  967. show_sf2_pwm, store_sf2_pwm, 6, index - 1), \
  968. SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
  969. show_sf2_temp, store_sf2_temp, 0, index - 1), \
  970. SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
  971. show_sf2_temp, store_sf2_temp, 1, index - 1), \
  972. SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
  973. show_sf2_temp, store_sf2_temp, 2, index - 1), \
  974. SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
  975. show_sf2_temp, store_sf2_temp, 3, index - 1), \
  976. SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
  977. show_sf2_temp, store_sf2_temp, 4, index - 1), \
  978. SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
  979. show_sf2_temp, store_sf2_temp, 5, index - 1), \
  980. SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
  981. show_sf2_temp, store_sf2_temp, 6, index - 1)
  982. static struct sensor_device_attribute_2 w83793_sensor_attr_2[] = {
  983. SENSOR_ATTR_IN(0),
  984. SENSOR_ATTR_IN(1),
  985. SENSOR_ATTR_IN(2),
  986. SENSOR_ATTR_IN(3),
  987. SENSOR_ATTR_IN(4),
  988. SENSOR_ATTR_IN(5),
  989. SENSOR_ATTR_IN(6),
  990. SENSOR_ATTR_IN(7),
  991. SENSOR_ATTR_IN(8),
  992. SENSOR_ATTR_IN(9),
  993. SENSOR_ATTR_FAN(1),
  994. SENSOR_ATTR_FAN(2),
  995. SENSOR_ATTR_FAN(3),
  996. SENSOR_ATTR_FAN(4),
  997. SENSOR_ATTR_FAN(5),
  998. SENSOR_ATTR_PWM(1),
  999. SENSOR_ATTR_PWM(2),
  1000. SENSOR_ATTR_PWM(3),
  1001. };
  1002. static struct sensor_device_attribute_2 w83793_temp[] = {
  1003. SENSOR_ATTR_TEMP(1),
  1004. SENSOR_ATTR_TEMP(2),
  1005. SENSOR_ATTR_TEMP(3),
  1006. SENSOR_ATTR_TEMP(4),
  1007. SENSOR_ATTR_TEMP(5),
  1008. SENSOR_ATTR_TEMP(6),
  1009. };
  1010. /* Fan6-Fan12 */
  1011. static struct sensor_device_attribute_2 w83793_left_fan[] = {
  1012. SENSOR_ATTR_FAN(6),
  1013. SENSOR_ATTR_FAN(7),
  1014. SENSOR_ATTR_FAN(8),
  1015. SENSOR_ATTR_FAN(9),
  1016. SENSOR_ATTR_FAN(10),
  1017. SENSOR_ATTR_FAN(11),
  1018. SENSOR_ATTR_FAN(12),
  1019. };
  1020. /* Pwm4-Pwm8 */
  1021. static struct sensor_device_attribute_2 w83793_left_pwm[] = {
  1022. SENSOR_ATTR_PWM(4),
  1023. SENSOR_ATTR_PWM(5),
  1024. SENSOR_ATTR_PWM(6),
  1025. SENSOR_ATTR_PWM(7),
  1026. SENSOR_ATTR_PWM(8),
  1027. };
  1028. static struct sensor_device_attribute_2 w83793_vid[] = {
  1029. SENSOR_ATTR_2(cpu0_vid, S_IRUGO, show_vid, NULL, NOT_USED, 0),
  1030. SENSOR_ATTR_2(cpu1_vid, S_IRUGO, show_vid, NULL, NOT_USED, 1),
  1031. };
  1032. static DEVICE_ATTR(vrm, S_IWUSR | S_IRUGO, show_vrm, store_vrm);
  1033. static struct sensor_device_attribute_2 sda_single_files[] = {
  1034. SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
  1035. store_chassis_clear, ALARM_STATUS, 30),
  1036. SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_beep_enable,
  1037. store_beep_enable, NOT_USED, NOT_USED),
  1038. SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
  1039. store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
  1040. SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
  1041. store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
  1042. SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
  1043. store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
  1044. SENSOR_ATTR_2(temp_critical, S_IWUSR | S_IRUGO, show_sf_setup,
  1045. store_sf_setup, SETUP_TEMP_CRITICAL, NOT_USED),
  1046. };
  1047. static void w83793_init_client(struct i2c_client *client)
  1048. {
  1049. if (reset)
  1050. w83793_write_value(client, W83793_REG_CONFIG, 0x80);
  1051. /* Start monitoring */
  1052. w83793_write_value(client, W83793_REG_CONFIG,
  1053. w83793_read_value(client, W83793_REG_CONFIG) | 0x01);
  1054. }
  1055. /*
  1056. * Watchdog routines
  1057. */
  1058. static int watchdog_set_timeout(struct w83793_data *data, int timeout)
  1059. {
  1060. int ret, mtimeout;
  1061. mtimeout = DIV_ROUND_UP(timeout, 60);
  1062. if (mtimeout > 255)
  1063. return -EINVAL;
  1064. mutex_lock(&data->watchdog_lock);
  1065. if (!data->client) {
  1066. ret = -ENODEV;
  1067. goto leave;
  1068. }
  1069. data->watchdog_timeout = mtimeout;
  1070. /* Set Timeout value (in Minutes) */
  1071. w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  1072. data->watchdog_timeout);
  1073. ret = mtimeout * 60;
  1074. leave:
  1075. mutex_unlock(&data->watchdog_lock);
  1076. return ret;
  1077. }
  1078. static int watchdog_get_timeout(struct w83793_data *data)
  1079. {
  1080. int timeout;
  1081. mutex_lock(&data->watchdog_lock);
  1082. timeout = data->watchdog_timeout * 60;
  1083. mutex_unlock(&data->watchdog_lock);
  1084. return timeout;
  1085. }
  1086. static int watchdog_trigger(struct w83793_data *data)
  1087. {
  1088. int ret = 0;
  1089. mutex_lock(&data->watchdog_lock);
  1090. if (!data->client) {
  1091. ret = -ENODEV;
  1092. goto leave;
  1093. }
  1094. /* Set Timeout value (in Minutes) */
  1095. w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  1096. data->watchdog_timeout);
  1097. leave:
  1098. mutex_unlock(&data->watchdog_lock);
  1099. return ret;
  1100. }
  1101. static int watchdog_enable(struct w83793_data *data)
  1102. {
  1103. int ret = 0;
  1104. mutex_lock(&data->watchdog_lock);
  1105. if (!data->client) {
  1106. ret = -ENODEV;
  1107. goto leave;
  1108. }
  1109. /* Set initial timeout */
  1110. w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  1111. data->watchdog_timeout);
  1112. /* Enable Soft Watchdog */
  1113. w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
  1114. leave:
  1115. mutex_unlock(&data->watchdog_lock);
  1116. return ret;
  1117. }
  1118. static int watchdog_disable(struct w83793_data *data)
  1119. {
  1120. int ret = 0;
  1121. mutex_lock(&data->watchdog_lock);
  1122. if (!data->client) {
  1123. ret = -ENODEV;
  1124. goto leave;
  1125. }
  1126. /* Disable Soft Watchdog */
  1127. w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
  1128. leave:
  1129. mutex_unlock(&data->watchdog_lock);
  1130. return ret;
  1131. }
  1132. static int watchdog_open(struct inode *inode, struct file *filp)
  1133. {
  1134. struct w83793_data *pos, *data = NULL;
  1135. int watchdog_is_open;
  1136. /*
  1137. * We get called from drivers/char/misc.c with misc_mtx hold, and we
  1138. * call misc_register() from w83793_probe() with watchdog_data_mutex
  1139. * hold, as misc_register() takes the misc_mtx lock, this is a possible
  1140. * deadlock, so we use mutex_trylock here.
  1141. */
  1142. if (!mutex_trylock(&watchdog_data_mutex))
  1143. return -ERESTARTSYS;
  1144. list_for_each_entry(pos, &watchdog_data_list, list) {
  1145. if (pos->watchdog_miscdev.minor == iminor(inode)) {
  1146. data = pos;
  1147. break;
  1148. }
  1149. }
  1150. /* Check, if device is already open */
  1151. watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
  1152. /*
  1153. * Increase data reference counter (if not already done).
  1154. * Note we can never not have found data, so we don't check for this
  1155. */
  1156. if (!watchdog_is_open)
  1157. kref_get(&data->kref);
  1158. mutex_unlock(&watchdog_data_mutex);
  1159. /* Check, if device is already open and possibly issue error */
  1160. if (watchdog_is_open)
  1161. return -EBUSY;
  1162. /* Enable Soft Watchdog */
  1163. watchdog_enable(data);
  1164. /* Store pointer to data into filp's private data */
  1165. filp->private_data = data;
  1166. return nonseekable_open(inode, filp);
  1167. }
  1168. static int watchdog_close(struct inode *inode, struct file *filp)
  1169. {
  1170. struct w83793_data *data = filp->private_data;
  1171. if (data->watchdog_expect_close) {
  1172. watchdog_disable(data);
  1173. data->watchdog_expect_close = 0;
  1174. } else {
  1175. watchdog_trigger(data);
  1176. dev_crit(&data->client->dev,
  1177. "unexpected close, not stopping watchdog!\n");
  1178. }
  1179. clear_bit(0, &data->watchdog_is_open);
  1180. /* Decrease data reference counter */
  1181. mutex_lock(&watchdog_data_mutex);
  1182. kref_put(&data->kref, w83793_release_resources);
  1183. mutex_unlock(&watchdog_data_mutex);
  1184. return 0;
  1185. }
  1186. static ssize_t watchdog_write(struct file *filp, const char __user *buf,
  1187. size_t count, loff_t *offset)
  1188. {
  1189. ssize_t ret;
  1190. struct w83793_data *data = filp->private_data;
  1191. if (count) {
  1192. if (!nowayout) {
  1193. size_t i;
  1194. /* Clear it in case it was set with a previous write */
  1195. data->watchdog_expect_close = 0;
  1196. for (i = 0; i != count; i++) {
  1197. char c;
  1198. if (get_user(c, buf + i))
  1199. return -EFAULT;
  1200. if (c == 'V')
  1201. data->watchdog_expect_close = 1;
  1202. }
  1203. }
  1204. ret = watchdog_trigger(data);
  1205. if (ret < 0)
  1206. return ret;
  1207. }
  1208. return count;
  1209. }
  1210. static long watchdog_ioctl(struct file *filp, unsigned int cmd,
  1211. unsigned long arg)
  1212. {
  1213. struct watchdog_info ident = {
  1214. .options = WDIOF_KEEPALIVEPING |
  1215. WDIOF_SETTIMEOUT |
  1216. WDIOF_CARDRESET,
  1217. .identity = "w83793 watchdog"
  1218. };
  1219. int val, ret = 0;
  1220. struct w83793_data *data = filp->private_data;
  1221. switch (cmd) {
  1222. case WDIOC_GETSUPPORT:
  1223. if (!nowayout)
  1224. ident.options |= WDIOF_MAGICCLOSE;
  1225. if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  1226. ret = -EFAULT;
  1227. break;
  1228. case WDIOC_GETSTATUS:
  1229. val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
  1230. ret = put_user(val, (int __user *)arg);
  1231. break;
  1232. case WDIOC_GETBOOTSTATUS:
  1233. ret = put_user(0, (int __user *)arg);
  1234. break;
  1235. case WDIOC_KEEPALIVE:
  1236. ret = watchdog_trigger(data);
  1237. break;
  1238. case WDIOC_GETTIMEOUT:
  1239. val = watchdog_get_timeout(data);
  1240. ret = put_user(val, (int __user *)arg);
  1241. break;
  1242. case WDIOC_SETTIMEOUT:
  1243. if (get_user(val, (int __user *)arg)) {
  1244. ret = -EFAULT;
  1245. break;
  1246. }
  1247. ret = watchdog_set_timeout(data, val);
  1248. if (ret > 0)
  1249. ret = put_user(ret, (int __user *)arg);
  1250. break;
  1251. case WDIOC_SETOPTIONS:
  1252. if (get_user(val, (int __user *)arg)) {
  1253. ret = -EFAULT;
  1254. break;
  1255. }
  1256. if (val & WDIOS_DISABLECARD)
  1257. ret = watchdog_disable(data);
  1258. else if (val & WDIOS_ENABLECARD)
  1259. ret = watchdog_enable(data);
  1260. else
  1261. ret = -EINVAL;
  1262. break;
  1263. default:
  1264. ret = -ENOTTY;
  1265. }
  1266. return ret;
  1267. }
  1268. static const struct file_operations watchdog_fops = {
  1269. .owner = THIS_MODULE,
  1270. .llseek = no_llseek,
  1271. .open = watchdog_open,
  1272. .release = watchdog_close,
  1273. .write = watchdog_write,
  1274. .unlocked_ioctl = watchdog_ioctl,
  1275. };
  1276. /*
  1277. * Notifier for system down
  1278. */
  1279. static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
  1280. void *unused)
  1281. {
  1282. struct w83793_data *data = NULL;
  1283. if (code == SYS_DOWN || code == SYS_HALT) {
  1284. /* Disable each registered watchdog */
  1285. mutex_lock(&watchdog_data_mutex);
  1286. list_for_each_entry(data, &watchdog_data_list, list) {
  1287. if (data->watchdog_miscdev.minor)
  1288. watchdog_disable(data);
  1289. }
  1290. mutex_unlock(&watchdog_data_mutex);
  1291. }
  1292. return NOTIFY_DONE;
  1293. }
  1294. /*
  1295. * The WDT needs to learn about soft shutdowns in order to
  1296. * turn the timebomb registers off.
  1297. */
  1298. static struct notifier_block watchdog_notifier = {
  1299. .notifier_call = watchdog_notify_sys,
  1300. };
  1301. /*
  1302. * Init / remove routines
  1303. */
  1304. static int w83793_remove(struct i2c_client *client)
  1305. {
  1306. struct w83793_data *data = i2c_get_clientdata(client);
  1307. struct device *dev = &client->dev;
  1308. int i, tmp;
  1309. /* Unregister the watchdog (if registered) */
  1310. if (data->watchdog_miscdev.minor) {
  1311. misc_deregister(&data->watchdog_miscdev);
  1312. if (data->watchdog_is_open) {
  1313. dev_warn(&client->dev,
  1314. "i2c client detached with watchdog open! "
  1315. "Stopping watchdog.\n");
  1316. watchdog_disable(data);
  1317. }
  1318. mutex_lock(&watchdog_data_mutex);
  1319. list_del(&data->list);
  1320. mutex_unlock(&watchdog_data_mutex);
  1321. /* Tell the watchdog code the client is gone */
  1322. mutex_lock(&data->watchdog_lock);
  1323. data->client = NULL;
  1324. mutex_unlock(&data->watchdog_lock);
  1325. }
  1326. /* Reset Configuration Register to Disable Watch Dog Registers */
  1327. tmp = w83793_read_value(client, W83793_REG_CONFIG);
  1328. w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
  1329. unregister_reboot_notifier(&watchdog_notifier);
  1330. hwmon_device_unregister(data->hwmon_dev);
  1331. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1332. device_remove_file(dev,
  1333. &w83793_sensor_attr_2[i].dev_attr);
  1334. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1335. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1336. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1337. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1338. device_remove_file(dev, &dev_attr_vrm);
  1339. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1340. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1341. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1342. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1343. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1344. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1345. if (data->lm75[0] != NULL)
  1346. i2c_unregister_device(data->lm75[0]);
  1347. if (data->lm75[1] != NULL)
  1348. i2c_unregister_device(data->lm75[1]);
  1349. /* Decrease data reference counter */
  1350. mutex_lock(&watchdog_data_mutex);
  1351. kref_put(&data->kref, w83793_release_resources);
  1352. mutex_unlock(&watchdog_data_mutex);
  1353. return 0;
  1354. }
  1355. static int
  1356. w83793_detect_subclients(struct i2c_client *client)
  1357. {
  1358. int i, id, err;
  1359. int address = client->addr;
  1360. u8 tmp;
  1361. struct i2c_adapter *adapter = client->adapter;
  1362. struct w83793_data *data = i2c_get_clientdata(client);
  1363. id = i2c_adapter_id(adapter);
  1364. if (force_subclients[0] == id && force_subclients[1] == address) {
  1365. for (i = 2; i <= 3; i++) {
  1366. if (force_subclients[i] < 0x48
  1367. || force_subclients[i] > 0x4f) {
  1368. dev_err(&client->dev,
  1369. "invalid subclient "
  1370. "address %d; must be 0x48-0x4f\n",
  1371. force_subclients[i]);
  1372. err = -EINVAL;
  1373. goto ERROR_SC_0;
  1374. }
  1375. }
  1376. w83793_write_value(client, W83793_REG_I2C_SUBADDR,
  1377. (force_subclients[2] & 0x07) |
  1378. ((force_subclients[3] & 0x07) << 4));
  1379. }
  1380. tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
  1381. if (!(tmp & 0x08))
  1382. data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
  1383. if (!(tmp & 0x80)) {
  1384. if ((data->lm75[0] != NULL)
  1385. && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
  1386. dev_err(&client->dev,
  1387. "duplicate addresses 0x%x, "
  1388. "use force_subclients\n", data->lm75[0]->addr);
  1389. err = -ENODEV;
  1390. goto ERROR_SC_1;
  1391. }
  1392. data->lm75[1] = i2c_new_dummy(adapter,
  1393. 0x48 + ((tmp >> 4) & 0x7));
  1394. }
  1395. return 0;
  1396. /* Undo inits in case of errors */
  1397. ERROR_SC_1:
  1398. if (data->lm75[0] != NULL)
  1399. i2c_unregister_device(data->lm75[0]);
  1400. ERROR_SC_0:
  1401. return err;
  1402. }
  1403. /* Return 0 if detection is successful, -ENODEV otherwise */
  1404. static int w83793_detect(struct i2c_client *client,
  1405. struct i2c_board_info *info)
  1406. {
  1407. u8 tmp, bank, chip_id;
  1408. struct i2c_adapter *adapter = client->adapter;
  1409. unsigned short address = client->addr;
  1410. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1411. return -ENODEV;
  1412. bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1413. tmp = bank & 0x80 ? 0x5c : 0xa3;
  1414. /* Check Winbond vendor ID */
  1415. if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
  1416. pr_debug("w83793: Detection failed at check vendor id\n");
  1417. return -ENODEV;
  1418. }
  1419. /*
  1420. * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
  1421. * should match
  1422. */
  1423. if ((bank & 0x07) == 0
  1424. && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
  1425. (address << 1)) {
  1426. pr_debug("w83793: Detection failed at check i2c addr\n");
  1427. return -ENODEV;
  1428. }
  1429. /* Determine the chip type now */
  1430. chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
  1431. if (chip_id != 0x7b)
  1432. return -ENODEV;
  1433. strlcpy(info->type, "w83793", I2C_NAME_SIZE);
  1434. return 0;
  1435. }
  1436. static int w83793_probe(struct i2c_client *client,
  1437. const struct i2c_device_id *id)
  1438. {
  1439. struct device *dev = &client->dev;
  1440. const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
  1441. struct w83793_data *data;
  1442. int i, tmp, val, err;
  1443. int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
  1444. int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
  1445. int files_temp = ARRAY_SIZE(w83793_temp) / 6;
  1446. data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
  1447. if (!data) {
  1448. err = -ENOMEM;
  1449. goto exit;
  1450. }
  1451. i2c_set_clientdata(client, data);
  1452. data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1453. mutex_init(&data->update_lock);
  1454. mutex_init(&data->watchdog_lock);
  1455. INIT_LIST_HEAD(&data->list);
  1456. kref_init(&data->kref);
  1457. /*
  1458. * Store client pointer in our data struct for watchdog usage
  1459. * (where the client is found through a data ptr instead of the
  1460. * otherway around)
  1461. */
  1462. data->client = client;
  1463. err = w83793_detect_subclients(client);
  1464. if (err)
  1465. goto free_mem;
  1466. /* Initialize the chip */
  1467. w83793_init_client(client);
  1468. /*
  1469. * Only fan 1-5 has their own input pins,
  1470. * Pwm 1-3 has their own pins
  1471. */
  1472. data->has_fan = 0x1f;
  1473. data->has_pwm = 0x07;
  1474. tmp = w83793_read_value(client, W83793_REG_MFC);
  1475. val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
  1476. /* check the function of pins 49-56 */
  1477. if (tmp & 0x80) {
  1478. data->has_vid |= 0x2; /* has VIDB */
  1479. } else {
  1480. data->has_pwm |= 0x18; /* pwm 4,5 */
  1481. if (val & 0x01) { /* fan 6 */
  1482. data->has_fan |= 0x20;
  1483. data->has_pwm |= 0x20;
  1484. }
  1485. if (val & 0x02) { /* fan 7 */
  1486. data->has_fan |= 0x40;
  1487. data->has_pwm |= 0x40;
  1488. }
  1489. if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
  1490. data->has_fan |= 0x80;
  1491. data->has_pwm |= 0x80;
  1492. }
  1493. }
  1494. /* check the function of pins 37-40 */
  1495. if (!(tmp & 0x29))
  1496. data->has_vid |= 0x1; /* has VIDA */
  1497. if (0x08 == (tmp & 0x0c)) {
  1498. if (val & 0x08) /* fan 9 */
  1499. data->has_fan |= 0x100;
  1500. if (val & 0x10) /* fan 10 */
  1501. data->has_fan |= 0x200;
  1502. }
  1503. if (0x20 == (tmp & 0x30)) {
  1504. if (val & 0x20) /* fan 11 */
  1505. data->has_fan |= 0x400;
  1506. if (val & 0x40) /* fan 12 */
  1507. data->has_fan |= 0x800;
  1508. }
  1509. if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
  1510. data->has_fan |= 0x80;
  1511. data->has_pwm |= 0x80;
  1512. }
  1513. tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
  1514. if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
  1515. data->has_fan |= 0x100;
  1516. }
  1517. if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
  1518. data->has_fan |= 0x200;
  1519. }
  1520. if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
  1521. data->has_fan |= 0x400;
  1522. }
  1523. if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
  1524. data->has_fan |= 0x800;
  1525. }
  1526. /* check the temp1-6 mode, ignore former AMDSI selected inputs */
  1527. tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
  1528. if (tmp & 0x01)
  1529. data->has_temp |= 0x01;
  1530. if (tmp & 0x04)
  1531. data->has_temp |= 0x02;
  1532. if (tmp & 0x10)
  1533. data->has_temp |= 0x04;
  1534. if (tmp & 0x40)
  1535. data->has_temp |= 0x08;
  1536. tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
  1537. if (tmp & 0x01)
  1538. data->has_temp |= 0x10;
  1539. if (tmp & 0x02)
  1540. data->has_temp |= 0x20;
  1541. /* Register sysfs hooks */
  1542. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
  1543. err = device_create_file(dev,
  1544. &w83793_sensor_attr_2[i].dev_attr);
  1545. if (err)
  1546. goto exit_remove;
  1547. }
  1548. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
  1549. if (!(data->has_vid & (1 << i)))
  1550. continue;
  1551. err = device_create_file(dev, &w83793_vid[i].dev_attr);
  1552. if (err)
  1553. goto exit_remove;
  1554. }
  1555. if (data->has_vid) {
  1556. data->vrm = vid_which_vrm();
  1557. err = device_create_file(dev, &dev_attr_vrm);
  1558. if (err)
  1559. goto exit_remove;
  1560. }
  1561. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
  1562. err = device_create_file(dev, &sda_single_files[i].dev_attr);
  1563. if (err)
  1564. goto exit_remove;
  1565. }
  1566. for (i = 0; i < 6; i++) {
  1567. int j;
  1568. if (!(data->has_temp & (1 << i)))
  1569. continue;
  1570. for (j = 0; j < files_temp; j++) {
  1571. err = device_create_file(dev,
  1572. &w83793_temp[(i) * files_temp
  1573. + j].dev_attr);
  1574. if (err)
  1575. goto exit_remove;
  1576. }
  1577. }
  1578. for (i = 5; i < 12; i++) {
  1579. int j;
  1580. if (!(data->has_fan & (1 << i)))
  1581. continue;
  1582. for (j = 0; j < files_fan; j++) {
  1583. err = device_create_file(dev,
  1584. &w83793_left_fan[(i - 5) * files_fan
  1585. + j].dev_attr);
  1586. if (err)
  1587. goto exit_remove;
  1588. }
  1589. }
  1590. for (i = 3; i < 8; i++) {
  1591. int j;
  1592. if (!(data->has_pwm & (1 << i)))
  1593. continue;
  1594. for (j = 0; j < files_pwm; j++) {
  1595. err = device_create_file(dev,
  1596. &w83793_left_pwm[(i - 3) * files_pwm
  1597. + j].dev_attr);
  1598. if (err)
  1599. goto exit_remove;
  1600. }
  1601. }
  1602. data->hwmon_dev = hwmon_device_register(dev);
  1603. if (IS_ERR(data->hwmon_dev)) {
  1604. err = PTR_ERR(data->hwmon_dev);
  1605. goto exit_remove;
  1606. }
  1607. /* Watchdog initialization */
  1608. /* Register boot notifier */
  1609. err = register_reboot_notifier(&watchdog_notifier);
  1610. if (err != 0) {
  1611. dev_err(&client->dev,
  1612. "cannot register reboot notifier (err=%d)\n", err);
  1613. goto exit_devunreg;
  1614. }
  1615. /*
  1616. * Enable Watchdog registers.
  1617. * Set Configuration Register to Enable Watch Dog Registers
  1618. * (Bit 2) = XXXX, X1XX.
  1619. */
  1620. tmp = w83793_read_value(client, W83793_REG_CONFIG);
  1621. w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
  1622. /* Set the default watchdog timeout */
  1623. data->watchdog_timeout = timeout;
  1624. /* Check, if last reboot was caused by watchdog */
  1625. data->watchdog_caused_reboot =
  1626. w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
  1627. /* Disable Soft Watchdog during initialiation */
  1628. watchdog_disable(data);
  1629. /*
  1630. * We take the data_mutex lock early so that watchdog_open() cannot
  1631. * run when misc_register() has completed, but we've not yet added
  1632. * our data to the watchdog_data_list (and set the default timeout)
  1633. */
  1634. mutex_lock(&watchdog_data_mutex);
  1635. for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  1636. /* Register our watchdog part */
  1637. snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  1638. "watchdog%c", (i == 0) ? '\0' : ('0' + i));
  1639. data->watchdog_miscdev.name = data->watchdog_name;
  1640. data->watchdog_miscdev.fops = &watchdog_fops;
  1641. data->watchdog_miscdev.minor = watchdog_minors[i];
  1642. err = misc_register(&data->watchdog_miscdev);
  1643. if (err == -EBUSY)
  1644. continue;
  1645. if (err) {
  1646. data->watchdog_miscdev.minor = 0;
  1647. dev_err(&client->dev,
  1648. "Registering watchdog chardev: %d\n", err);
  1649. break;
  1650. }
  1651. list_add(&data->list, &watchdog_data_list);
  1652. dev_info(&client->dev,
  1653. "Registered watchdog chardev major 10, minor: %d\n",
  1654. watchdog_minors[i]);
  1655. break;
  1656. }
  1657. if (i == ARRAY_SIZE(watchdog_minors)) {
  1658. data->watchdog_miscdev.minor = 0;
  1659. dev_warn(&client->dev, "Couldn't register watchdog chardev "
  1660. "(due to no free minor)\n");
  1661. }
  1662. mutex_unlock(&watchdog_data_mutex);
  1663. return 0;
  1664. /* Unregister hwmon device */
  1665. exit_devunreg:
  1666. hwmon_device_unregister(data->hwmon_dev);
  1667. /* Unregister sysfs hooks */
  1668. exit_remove:
  1669. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1670. device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
  1671. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1672. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1673. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1674. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1675. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1676. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1677. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1678. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1679. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1680. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1681. if (data->lm75[0] != NULL)
  1682. i2c_unregister_device(data->lm75[0]);
  1683. if (data->lm75[1] != NULL)
  1684. i2c_unregister_device(data->lm75[1]);
  1685. free_mem:
  1686. kfree(data);
  1687. exit:
  1688. return err;
  1689. }
  1690. static void w83793_update_nonvolatile(struct device *dev)
  1691. {
  1692. struct i2c_client *client = to_i2c_client(dev);
  1693. struct w83793_data *data = i2c_get_clientdata(client);
  1694. int i, j;
  1695. /*
  1696. * They are somewhat "stable" registers, and to update them every time
  1697. * takes so much time, it's just not worthy. Update them in a long
  1698. * interval to avoid exception.
  1699. */
  1700. if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
  1701. || !data->valid))
  1702. return;
  1703. /* update voltage limits */
  1704. for (i = 1; i < 3; i++) {
  1705. for (j = 0; j < ARRAY_SIZE(data->in); j++) {
  1706. data->in[j][i] =
  1707. w83793_read_value(client, W83793_REG_IN[j][i]);
  1708. }
  1709. data->in_low_bits[i] =
  1710. w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
  1711. }
  1712. for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
  1713. /* Update the Fan measured value and limits */
  1714. if (!(data->has_fan & (1 << i)))
  1715. continue;
  1716. data->fan_min[i] =
  1717. w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
  1718. data->fan_min[i] |=
  1719. w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
  1720. }
  1721. for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
  1722. if (!(data->has_temp & (1 << i)))
  1723. continue;
  1724. data->temp_fan_map[i] =
  1725. w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
  1726. for (j = 1; j < 5; j++) {
  1727. data->temp[i][j] =
  1728. w83793_read_value(client, W83793_REG_TEMP[i][j]);
  1729. }
  1730. data->temp_cruise[i] =
  1731. w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
  1732. for (j = 0; j < 7; j++) {
  1733. data->sf2_pwm[i][j] =
  1734. w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
  1735. data->sf2_temp[i][j] =
  1736. w83793_read_value(client,
  1737. W83793_REG_SF2_TEMP(i, j));
  1738. }
  1739. }
  1740. for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
  1741. data->temp_mode[i] =
  1742. w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
  1743. for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
  1744. data->tolerance[i] =
  1745. w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  1746. }
  1747. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1748. if (!(data->has_pwm & (1 << i)))
  1749. continue;
  1750. data->pwm[i][PWM_NONSTOP] =
  1751. w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
  1752. data->pwm[i][PWM_START] =
  1753. w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
  1754. data->pwm_stop_time[i] =
  1755. w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
  1756. }
  1757. data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
  1758. data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
  1759. data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
  1760. data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
  1761. data->temp_critical =
  1762. w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
  1763. data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
  1764. for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
  1765. data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
  1766. data->last_nonvolatile = jiffies;
  1767. }
  1768. static struct w83793_data *w83793_update_device(struct device *dev)
  1769. {
  1770. struct i2c_client *client = to_i2c_client(dev);
  1771. struct w83793_data *data = i2c_get_clientdata(client);
  1772. int i;
  1773. mutex_lock(&data->update_lock);
  1774. if (!(time_after(jiffies, data->last_updated + HZ * 2)
  1775. || !data->valid))
  1776. goto END;
  1777. /* Update the voltages measured value and limits */
  1778. for (i = 0; i < ARRAY_SIZE(data->in); i++)
  1779. data->in[i][IN_READ] =
  1780. w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
  1781. data->in_low_bits[IN_READ] =
  1782. w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
  1783. for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
  1784. if (!(data->has_fan & (1 << i)))
  1785. continue;
  1786. data->fan[i] =
  1787. w83793_read_value(client, W83793_REG_FAN(i)) << 8;
  1788. data->fan[i] |=
  1789. w83793_read_value(client, W83793_REG_FAN(i) + 1);
  1790. }
  1791. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  1792. if (!(data->has_temp & (1 << i)))
  1793. continue;
  1794. data->temp[i][TEMP_READ] =
  1795. w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
  1796. }
  1797. data->temp_low_bits =
  1798. w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
  1799. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1800. if (data->has_pwm & (1 << i))
  1801. data->pwm[i][PWM_DUTY] =
  1802. w83793_read_value(client,
  1803. W83793_REG_PWM(i, PWM_DUTY));
  1804. }
  1805. for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
  1806. data->alarms[i] =
  1807. w83793_read_value(client, W83793_REG_ALARM(i));
  1808. if (data->has_vid & 0x01)
  1809. data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
  1810. if (data->has_vid & 0x02)
  1811. data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
  1812. w83793_update_nonvolatile(dev);
  1813. data->last_updated = jiffies;
  1814. data->valid = 1;
  1815. END:
  1816. mutex_unlock(&data->update_lock);
  1817. return data;
  1818. }
  1819. /*
  1820. * Ignore the possibility that somebody change bank outside the driver
  1821. * Must be called with data->update_lock held, except during initialization
  1822. */
  1823. static u8 w83793_read_value(struct i2c_client *client, u16 reg)
  1824. {
  1825. struct w83793_data *data = i2c_get_clientdata(client);
  1826. u8 res = 0xff;
  1827. u8 new_bank = reg >> 8;
  1828. new_bank |= data->bank & 0xfc;
  1829. if (data->bank != new_bank) {
  1830. if (i2c_smbus_write_byte_data
  1831. (client, W83793_REG_BANKSEL, new_bank) >= 0)
  1832. data->bank = new_bank;
  1833. else {
  1834. dev_err(&client->dev,
  1835. "set bank to %d failed, fall back "
  1836. "to bank %d, read reg 0x%x error\n",
  1837. new_bank, data->bank, reg);
  1838. res = 0x0; /* read 0x0 from the chip */
  1839. goto END;
  1840. }
  1841. }
  1842. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  1843. END:
  1844. return res;
  1845. }
  1846. /* Must be called with data->update_lock held, except during initialization */
  1847. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
  1848. {
  1849. struct w83793_data *data = i2c_get_clientdata(client);
  1850. int res;
  1851. u8 new_bank = reg >> 8;
  1852. new_bank |= data->bank & 0xfc;
  1853. if (data->bank != new_bank) {
  1854. res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
  1855. new_bank);
  1856. if (res < 0) {
  1857. dev_err(&client->dev,
  1858. "set bank to %d failed, fall back "
  1859. "to bank %d, write reg 0x%x error\n",
  1860. new_bank, data->bank, reg);
  1861. goto END;
  1862. }
  1863. data->bank = new_bank;
  1864. }
  1865. res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
  1866. END:
  1867. return res;
  1868. }
  1869. module_i2c_driver(w83793_driver);
  1870. MODULE_AUTHOR("Yuan Mu, Sven Anders");
  1871. MODULE_DESCRIPTION("w83793 driver");
  1872. MODULE_LICENSE("GPL");