w83793.c 60 KB

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