w83793.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144
  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 <[email protected]>
  7. * Copyright (C) 2009-2010 Sven Anders <[email protected]>, 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", 0 },
  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_new = 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. .llseek = no_llseek,
  1260. .open = watchdog_open,
  1261. .release = watchdog_close,
  1262. .write = watchdog_write,
  1263. .unlocked_ioctl = watchdog_ioctl,
  1264. .compat_ioctl = compat_ptr_ioctl,
  1265. };
  1266. /*
  1267. * Notifier for system down
  1268. */
  1269. static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
  1270. void *unused)
  1271. {
  1272. struct w83793_data *data = NULL;
  1273. if (code == SYS_DOWN || code == SYS_HALT) {
  1274. /* Disable each registered watchdog */
  1275. mutex_lock(&watchdog_data_mutex);
  1276. list_for_each_entry(data, &watchdog_data_list, list) {
  1277. if (data->watchdog_miscdev.minor)
  1278. watchdog_disable(data);
  1279. }
  1280. mutex_unlock(&watchdog_data_mutex);
  1281. }
  1282. return NOTIFY_DONE;
  1283. }
  1284. /*
  1285. * The WDT needs to learn about soft shutdowns in order to
  1286. * turn the timebomb registers off.
  1287. */
  1288. static struct notifier_block watchdog_notifier = {
  1289. .notifier_call = watchdog_notify_sys,
  1290. };
  1291. /*
  1292. * Init / remove routines
  1293. */
  1294. static void w83793_remove(struct i2c_client *client)
  1295. {
  1296. struct w83793_data *data = i2c_get_clientdata(client);
  1297. struct device *dev = &client->dev;
  1298. int i, tmp;
  1299. /* Unregister the watchdog (if registered) */
  1300. if (data->watchdog_miscdev.minor) {
  1301. misc_deregister(&data->watchdog_miscdev);
  1302. if (data->watchdog_is_open) {
  1303. dev_warn(&client->dev,
  1304. "i2c client detached with watchdog open! "
  1305. "Stopping watchdog.\n");
  1306. watchdog_disable(data);
  1307. }
  1308. mutex_lock(&watchdog_data_mutex);
  1309. list_del(&data->list);
  1310. mutex_unlock(&watchdog_data_mutex);
  1311. /* Tell the watchdog code the client is gone */
  1312. mutex_lock(&data->watchdog_lock);
  1313. data->client = NULL;
  1314. mutex_unlock(&data->watchdog_lock);
  1315. }
  1316. /* Reset Configuration Register to Disable Watch Dog Registers */
  1317. tmp = w83793_read_value(client, W83793_REG_CONFIG);
  1318. w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
  1319. unregister_reboot_notifier(&watchdog_notifier);
  1320. hwmon_device_unregister(data->hwmon_dev);
  1321. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1322. device_remove_file(dev,
  1323. &w83793_sensor_attr_2[i].dev_attr);
  1324. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1325. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1326. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1327. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1328. device_remove_file(dev, &dev_attr_vrm);
  1329. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1330. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1331. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1332. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1333. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1334. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1335. /* Decrease data reference counter */
  1336. mutex_lock(&watchdog_data_mutex);
  1337. kref_put(&data->kref, w83793_release_resources);
  1338. mutex_unlock(&watchdog_data_mutex);
  1339. }
  1340. static int
  1341. w83793_detect_subclients(struct i2c_client *client)
  1342. {
  1343. int i, id;
  1344. int address = client->addr;
  1345. u8 tmp;
  1346. struct i2c_adapter *adapter = client->adapter;
  1347. id = i2c_adapter_id(adapter);
  1348. if (force_subclients[0] == id && force_subclients[1] == address) {
  1349. for (i = 2; i <= 3; i++) {
  1350. if (force_subclients[i] < 0x48
  1351. || force_subclients[i] > 0x4f) {
  1352. dev_err(&client->dev,
  1353. "invalid subclient "
  1354. "address %d; must be 0x48-0x4f\n",
  1355. force_subclients[i]);
  1356. return -EINVAL;
  1357. }
  1358. }
  1359. w83793_write_value(client, W83793_REG_I2C_SUBADDR,
  1360. (force_subclients[2] & 0x07) |
  1361. ((force_subclients[3] & 0x07) << 4));
  1362. }
  1363. tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
  1364. if (!(tmp & 0x88) && (tmp & 0x7) == ((tmp >> 4) & 0x7)) {
  1365. dev_err(&client->dev,
  1366. "duplicate addresses 0x%x, use force_subclient\n", 0x48 + (tmp & 0x7));
  1367. return -ENODEV;
  1368. }
  1369. if (!(tmp & 0x08))
  1370. devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + (tmp & 0x7));
  1371. if (!(tmp & 0x80))
  1372. devm_i2c_new_dummy_device(&client->dev, adapter, 0x48 + ((tmp >> 4) & 0x7));
  1373. return 0;
  1374. }
  1375. /* Return 0 if detection is successful, -ENODEV otherwise */
  1376. static int w83793_detect(struct i2c_client *client,
  1377. struct i2c_board_info *info)
  1378. {
  1379. u8 tmp, bank, chip_id;
  1380. struct i2c_adapter *adapter = client->adapter;
  1381. unsigned short address = client->addr;
  1382. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1383. return -ENODEV;
  1384. bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1385. tmp = bank & 0x80 ? 0x5c : 0xa3;
  1386. /* Check Winbond vendor ID */
  1387. if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
  1388. pr_debug("w83793: Detection failed at check vendor id\n");
  1389. return -ENODEV;
  1390. }
  1391. /*
  1392. * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
  1393. * should match
  1394. */
  1395. if ((bank & 0x07) == 0
  1396. && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
  1397. (address << 1)) {
  1398. pr_debug("w83793: Detection failed at check i2c addr\n");
  1399. return -ENODEV;
  1400. }
  1401. /* Determine the chip type now */
  1402. chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
  1403. if (chip_id != 0x7b)
  1404. return -ENODEV;
  1405. strscpy(info->type, "w83793", I2C_NAME_SIZE);
  1406. return 0;
  1407. }
  1408. static int w83793_probe(struct i2c_client *client)
  1409. {
  1410. struct device *dev = &client->dev;
  1411. static const int watchdog_minors[] = {
  1412. WATCHDOG_MINOR, 212, 213, 214, 215
  1413. };
  1414. struct w83793_data *data;
  1415. int i, tmp, val, err;
  1416. int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
  1417. int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
  1418. int files_temp = ARRAY_SIZE(w83793_temp) / 6;
  1419. data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
  1420. if (!data) {
  1421. err = -ENOMEM;
  1422. goto exit;
  1423. }
  1424. i2c_set_clientdata(client, data);
  1425. data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1426. mutex_init(&data->update_lock);
  1427. mutex_init(&data->watchdog_lock);
  1428. INIT_LIST_HEAD(&data->list);
  1429. kref_init(&data->kref);
  1430. /*
  1431. * Store client pointer in our data struct for watchdog usage
  1432. * (where the client is found through a data ptr instead of the
  1433. * otherway around)
  1434. */
  1435. data->client = client;
  1436. err = w83793_detect_subclients(client);
  1437. if (err)
  1438. goto free_mem;
  1439. /* Initialize the chip */
  1440. w83793_init_client(client);
  1441. /*
  1442. * Only fan 1-5 has their own input pins,
  1443. * Pwm 1-3 has their own pins
  1444. */
  1445. data->has_fan = 0x1f;
  1446. data->has_pwm = 0x07;
  1447. tmp = w83793_read_value(client, W83793_REG_MFC);
  1448. val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
  1449. /* check the function of pins 49-56 */
  1450. if (tmp & 0x80) {
  1451. data->has_vid |= 0x2; /* has VIDB */
  1452. } else {
  1453. data->has_pwm |= 0x18; /* pwm 4,5 */
  1454. if (val & 0x01) { /* fan 6 */
  1455. data->has_fan |= 0x20;
  1456. data->has_pwm |= 0x20;
  1457. }
  1458. if (val & 0x02) { /* fan 7 */
  1459. data->has_fan |= 0x40;
  1460. data->has_pwm |= 0x40;
  1461. }
  1462. if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
  1463. data->has_fan |= 0x80;
  1464. data->has_pwm |= 0x80;
  1465. }
  1466. }
  1467. /* check the function of pins 37-40 */
  1468. if (!(tmp & 0x29))
  1469. data->has_vid |= 0x1; /* has VIDA */
  1470. if (0x08 == (tmp & 0x0c)) {
  1471. if (val & 0x08) /* fan 9 */
  1472. data->has_fan |= 0x100;
  1473. if (val & 0x10) /* fan 10 */
  1474. data->has_fan |= 0x200;
  1475. }
  1476. if (0x20 == (tmp & 0x30)) {
  1477. if (val & 0x20) /* fan 11 */
  1478. data->has_fan |= 0x400;
  1479. if (val & 0x40) /* fan 12 */
  1480. data->has_fan |= 0x800;
  1481. }
  1482. if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
  1483. data->has_fan |= 0x80;
  1484. data->has_pwm |= 0x80;
  1485. }
  1486. tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
  1487. if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
  1488. data->has_fan |= 0x100;
  1489. }
  1490. if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
  1491. data->has_fan |= 0x200;
  1492. }
  1493. if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
  1494. data->has_fan |= 0x400;
  1495. }
  1496. if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
  1497. data->has_fan |= 0x800;
  1498. }
  1499. /* check the temp1-6 mode, ignore former AMDSI selected inputs */
  1500. tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
  1501. if (tmp & 0x01)
  1502. data->has_temp |= 0x01;
  1503. if (tmp & 0x04)
  1504. data->has_temp |= 0x02;
  1505. if (tmp & 0x10)
  1506. data->has_temp |= 0x04;
  1507. if (tmp & 0x40)
  1508. data->has_temp |= 0x08;
  1509. tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
  1510. if (tmp & 0x01)
  1511. data->has_temp |= 0x10;
  1512. if (tmp & 0x02)
  1513. data->has_temp |= 0x20;
  1514. /* Register sysfs hooks */
  1515. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
  1516. err = device_create_file(dev,
  1517. &w83793_sensor_attr_2[i].dev_attr);
  1518. if (err)
  1519. goto exit_remove;
  1520. }
  1521. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
  1522. if (!(data->has_vid & (1 << i)))
  1523. continue;
  1524. err = device_create_file(dev, &w83793_vid[i].dev_attr);
  1525. if (err)
  1526. goto exit_remove;
  1527. }
  1528. if (data->has_vid) {
  1529. data->vrm = vid_which_vrm();
  1530. err = device_create_file(dev, &dev_attr_vrm);
  1531. if (err)
  1532. goto exit_remove;
  1533. }
  1534. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
  1535. err = device_create_file(dev, &sda_single_files[i].dev_attr);
  1536. if (err)
  1537. goto exit_remove;
  1538. }
  1539. for (i = 0; i < 6; i++) {
  1540. int j;
  1541. if (!(data->has_temp & (1 << i)))
  1542. continue;
  1543. for (j = 0; j < files_temp; j++) {
  1544. err = device_create_file(dev,
  1545. &w83793_temp[(i) * files_temp
  1546. + j].dev_attr);
  1547. if (err)
  1548. goto exit_remove;
  1549. }
  1550. }
  1551. for (i = 5; i < 12; i++) {
  1552. int j;
  1553. if (!(data->has_fan & (1 << i)))
  1554. continue;
  1555. for (j = 0; j < files_fan; j++) {
  1556. err = device_create_file(dev,
  1557. &w83793_left_fan[(i - 5) * files_fan
  1558. + j].dev_attr);
  1559. if (err)
  1560. goto exit_remove;
  1561. }
  1562. }
  1563. for (i = 3; i < 8; i++) {
  1564. int j;
  1565. if (!(data->has_pwm & (1 << i)))
  1566. continue;
  1567. for (j = 0; j < files_pwm; j++) {
  1568. err = device_create_file(dev,
  1569. &w83793_left_pwm[(i - 3) * files_pwm
  1570. + j].dev_attr);
  1571. if (err)
  1572. goto exit_remove;
  1573. }
  1574. }
  1575. data->hwmon_dev = hwmon_device_register(dev);
  1576. if (IS_ERR(data->hwmon_dev)) {
  1577. err = PTR_ERR(data->hwmon_dev);
  1578. goto exit_remove;
  1579. }
  1580. /* Watchdog initialization */
  1581. /* Register boot notifier */
  1582. err = register_reboot_notifier(&watchdog_notifier);
  1583. if (err != 0) {
  1584. dev_err(&client->dev,
  1585. "cannot register reboot notifier (err=%d)\n", err);
  1586. goto exit_devunreg;
  1587. }
  1588. /*
  1589. * Enable Watchdog registers.
  1590. * Set Configuration Register to Enable Watch Dog Registers
  1591. * (Bit 2) = XXXX, X1XX.
  1592. */
  1593. tmp = w83793_read_value(client, W83793_REG_CONFIG);
  1594. w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
  1595. /* Set the default watchdog timeout */
  1596. data->watchdog_timeout = timeout;
  1597. /* Check, if last reboot was caused by watchdog */
  1598. data->watchdog_caused_reboot =
  1599. w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
  1600. /* Disable Soft Watchdog during initialiation */
  1601. watchdog_disable(data);
  1602. /*
  1603. * We take the data_mutex lock early so that watchdog_open() cannot
  1604. * run when misc_register() has completed, but we've not yet added
  1605. * our data to the watchdog_data_list (and set the default timeout)
  1606. */
  1607. mutex_lock(&watchdog_data_mutex);
  1608. for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  1609. /* Register our watchdog part */
  1610. snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  1611. "watchdog%c", (i == 0) ? '\0' : ('0' + i));
  1612. data->watchdog_miscdev.name = data->watchdog_name;
  1613. data->watchdog_miscdev.fops = &watchdog_fops;
  1614. data->watchdog_miscdev.minor = watchdog_minors[i];
  1615. err = misc_register(&data->watchdog_miscdev);
  1616. if (err == -EBUSY)
  1617. continue;
  1618. if (err) {
  1619. data->watchdog_miscdev.minor = 0;
  1620. dev_err(&client->dev,
  1621. "Registering watchdog chardev: %d\n", err);
  1622. break;
  1623. }
  1624. list_add(&data->list, &watchdog_data_list);
  1625. dev_info(&client->dev,
  1626. "Registered watchdog chardev major 10, minor: %d\n",
  1627. watchdog_minors[i]);
  1628. break;
  1629. }
  1630. if (i == ARRAY_SIZE(watchdog_minors)) {
  1631. data->watchdog_miscdev.minor = 0;
  1632. dev_warn(&client->dev,
  1633. "Couldn't register watchdog chardev (due to no free minor)\n");
  1634. }
  1635. mutex_unlock(&watchdog_data_mutex);
  1636. return 0;
  1637. /* Unregister hwmon device */
  1638. exit_devunreg:
  1639. hwmon_device_unregister(data->hwmon_dev);
  1640. /* Unregister sysfs hooks */
  1641. exit_remove:
  1642. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1643. device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
  1644. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1645. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1646. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1647. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1648. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1649. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1650. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1651. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1652. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1653. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1654. free_mem:
  1655. kfree(data);
  1656. exit:
  1657. return err;
  1658. }
  1659. static void w83793_update_nonvolatile(struct device *dev)
  1660. {
  1661. struct i2c_client *client = to_i2c_client(dev);
  1662. struct w83793_data *data = i2c_get_clientdata(client);
  1663. int i, j;
  1664. /*
  1665. * They are somewhat "stable" registers, and to update them every time
  1666. * takes so much time, it's just not worthy. Update them in a long
  1667. * interval to avoid exception.
  1668. */
  1669. if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
  1670. || !data->valid))
  1671. return;
  1672. /* update voltage limits */
  1673. for (i = 1; i < 3; i++) {
  1674. for (j = 0; j < ARRAY_SIZE(data->in); j++) {
  1675. data->in[j][i] =
  1676. w83793_read_value(client, W83793_REG_IN[j][i]);
  1677. }
  1678. data->in_low_bits[i] =
  1679. w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
  1680. }
  1681. for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
  1682. /* Update the Fan measured value and limits */
  1683. if (!(data->has_fan & (1 << i)))
  1684. continue;
  1685. data->fan_min[i] =
  1686. w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
  1687. data->fan_min[i] |=
  1688. w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
  1689. }
  1690. for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
  1691. if (!(data->has_temp & (1 << i)))
  1692. continue;
  1693. data->temp_fan_map[i] =
  1694. w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
  1695. for (j = 1; j < 5; j++) {
  1696. data->temp[i][j] =
  1697. w83793_read_value(client, W83793_REG_TEMP[i][j]);
  1698. }
  1699. data->temp_cruise[i] =
  1700. w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
  1701. for (j = 0; j < 7; j++) {
  1702. data->sf2_pwm[i][j] =
  1703. w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
  1704. data->sf2_temp[i][j] =
  1705. w83793_read_value(client,
  1706. W83793_REG_SF2_TEMP(i, j));
  1707. }
  1708. }
  1709. for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
  1710. data->temp_mode[i] =
  1711. w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
  1712. for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
  1713. data->tolerance[i] =
  1714. w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  1715. }
  1716. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1717. if (!(data->has_pwm & (1 << i)))
  1718. continue;
  1719. data->pwm[i][PWM_NONSTOP] =
  1720. w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
  1721. data->pwm[i][PWM_START] =
  1722. w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
  1723. data->pwm_stop_time[i] =
  1724. w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
  1725. }
  1726. data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
  1727. data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
  1728. data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
  1729. data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
  1730. data->temp_critical =
  1731. w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
  1732. data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
  1733. for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
  1734. data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
  1735. data->last_nonvolatile = jiffies;
  1736. }
  1737. static struct w83793_data *w83793_update_device(struct device *dev)
  1738. {
  1739. struct i2c_client *client = to_i2c_client(dev);
  1740. struct w83793_data *data = i2c_get_clientdata(client);
  1741. int i;
  1742. mutex_lock(&data->update_lock);
  1743. if (!(time_after(jiffies, data->last_updated + HZ * 2)
  1744. || !data->valid))
  1745. goto END;
  1746. /* Update the voltages measured value and limits */
  1747. for (i = 0; i < ARRAY_SIZE(data->in); i++)
  1748. data->in[i][IN_READ] =
  1749. w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
  1750. data->in_low_bits[IN_READ] =
  1751. w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
  1752. for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
  1753. if (!(data->has_fan & (1 << i)))
  1754. continue;
  1755. data->fan[i] =
  1756. w83793_read_value(client, W83793_REG_FAN(i)) << 8;
  1757. data->fan[i] |=
  1758. w83793_read_value(client, W83793_REG_FAN(i) + 1);
  1759. }
  1760. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  1761. if (!(data->has_temp & (1 << i)))
  1762. continue;
  1763. data->temp[i][TEMP_READ] =
  1764. w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
  1765. }
  1766. data->temp_low_bits =
  1767. w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
  1768. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1769. if (data->has_pwm & (1 << i))
  1770. data->pwm[i][PWM_DUTY] =
  1771. w83793_read_value(client,
  1772. W83793_REG_PWM(i, PWM_DUTY));
  1773. }
  1774. for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
  1775. data->alarms[i] =
  1776. w83793_read_value(client, W83793_REG_ALARM(i));
  1777. if (data->has_vid & 0x01)
  1778. data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
  1779. if (data->has_vid & 0x02)
  1780. data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
  1781. w83793_update_nonvolatile(dev);
  1782. data->last_updated = jiffies;
  1783. data->valid = true;
  1784. END:
  1785. mutex_unlock(&data->update_lock);
  1786. return data;
  1787. }
  1788. /*
  1789. * Ignore the possibility that somebody change bank outside the driver
  1790. * Must be called with data->update_lock held, except during initialization
  1791. */
  1792. static u8 w83793_read_value(struct i2c_client *client, u16 reg)
  1793. {
  1794. struct w83793_data *data = i2c_get_clientdata(client);
  1795. u8 res;
  1796. u8 new_bank = reg >> 8;
  1797. new_bank |= data->bank & 0xfc;
  1798. if (data->bank != new_bank) {
  1799. if (i2c_smbus_write_byte_data
  1800. (client, W83793_REG_BANKSEL, new_bank) >= 0)
  1801. data->bank = new_bank;
  1802. else {
  1803. dev_err(&client->dev,
  1804. "set bank to %d failed, fall back "
  1805. "to bank %d, read reg 0x%x error\n",
  1806. new_bank, data->bank, reg);
  1807. res = 0x0; /* read 0x0 from the chip */
  1808. goto END;
  1809. }
  1810. }
  1811. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  1812. END:
  1813. return res;
  1814. }
  1815. /* Must be called with data->update_lock held, except during initialization */
  1816. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
  1817. {
  1818. struct w83793_data *data = i2c_get_clientdata(client);
  1819. int res;
  1820. u8 new_bank = reg >> 8;
  1821. new_bank |= data->bank & 0xfc;
  1822. if (data->bank != new_bank) {
  1823. res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
  1824. new_bank);
  1825. if (res < 0) {
  1826. dev_err(&client->dev,
  1827. "set bank to %d failed, fall back "
  1828. "to bank %d, write reg 0x%x error\n",
  1829. new_bank, data->bank, reg);
  1830. goto END;
  1831. }
  1832. data->bank = new_bank;
  1833. }
  1834. res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
  1835. END:
  1836. return res;
  1837. }
  1838. module_i2c_driver(w83793_driver);
  1839. MODULE_AUTHOR("Yuan Mu, Sven Anders");
  1840. MODULE_DESCRIPTION("w83793 driver");
  1841. MODULE_LICENSE("GPL");