w83793.c 60 KB

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