w83793.c 61 KB

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