w83793.c 59 KB

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