w83793.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  1. /*
  2. * w83793.c - Linux kernel driver for hardware monitoring
  3. * Copyright (C) 2006 Winbond Electronics Corp.
  4. * Yuan Mu
  5. * Rudolf Marek <r.marek@assembler.cz>
  6. * Copyright (C) 2009-2010 Sven Anders <anders@anduras.de>, ANDURAS AG.
  7. * Watchdog driver part
  8. * (Based partially on fschmd driver,
  9. * Copyright 2007-2008 by Hans de Goede)
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation - version 2.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301 USA.
  24. */
  25. /*
  26. * Supports following chips:
  27. *
  28. * Chip #vin #fanin #pwm #temp wchipid vendid i2c ISA
  29. * w83793 10 12 8 6 0x7b 0x5ca3 yes no
  30. */
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/i2c.h>
  35. #include <linux/hwmon.h>
  36. #include <linux/hwmon-vid.h>
  37. #include <linux/hwmon-sysfs.h>
  38. #include <linux/err.h>
  39. #include <linux/mutex.h>
  40. #include <linux/fs.h>
  41. #include <linux/watchdog.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/kref.h>
  45. #include <linux/notifier.h>
  46. #include <linux/reboot.h>
  47. #include <linux/jiffies.h>
  48. /* Default values */
  49. #define WATCHDOG_TIMEOUT 2 /* 2 minute default timeout */
  50. /* Addresses to scan */
  51. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  52. I2C_CLIENT_END };
  53. /* Insmod parameters */
  54. static unsigned short force_subclients[4];
  55. module_param_array(force_subclients, short, NULL, 0);
  56. MODULE_PARM_DESC(force_subclients,
  57. "List of subclient addresses: {bus, clientaddr, subclientaddr1, subclientaddr2}");
  58. static bool reset;
  59. module_param(reset, bool, 0);
  60. MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
  61. static int timeout = WATCHDOG_TIMEOUT; /* default timeout in minutes */
  62. module_param(timeout, int, 0);
  63. MODULE_PARM_DESC(timeout,
  64. "Watchdog timeout in minutes. 2<= timeout <=255 (default="
  65. __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
  66. static bool nowayout = WATCHDOG_NOWAYOUT;
  67. module_param(nowayout, bool, 0);
  68. MODULE_PARM_DESC(nowayout,
  69. "Watchdog cannot be stopped once started (default="
  70. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  71. /*
  72. * Address 0x00, 0x0d, 0x0e, 0x0f in all three banks are reserved
  73. * as ID, Bank Select registers
  74. */
  75. #define W83793_REG_BANKSEL 0x00
  76. #define W83793_REG_VENDORID 0x0d
  77. #define W83793_REG_CHIPID 0x0e
  78. #define W83793_REG_DEVICEID 0x0f
  79. #define W83793_REG_CONFIG 0x40
  80. #define W83793_REG_MFC 0x58
  81. #define W83793_REG_FANIN_CTRL 0x5c
  82. #define W83793_REG_FANIN_SEL 0x5d
  83. #define W83793_REG_I2C_ADDR 0x0b
  84. #define W83793_REG_I2C_SUBADDR 0x0c
  85. #define W83793_REG_VID_INA 0x05
  86. #define W83793_REG_VID_INB 0x06
  87. #define W83793_REG_VID_LATCHA 0x07
  88. #define W83793_REG_VID_LATCHB 0x08
  89. #define W83793_REG_VID_CTRL 0x59
  90. #define W83793_REG_WDT_LOCK 0x01
  91. #define W83793_REG_WDT_ENABLE 0x02
  92. #define W83793_REG_WDT_STATUS 0x03
  93. #define W83793_REG_WDT_TIMEOUT 0x04
  94. static u16 W83793_REG_TEMP_MODE[2] = { 0x5e, 0x5f };
  95. #define TEMP_READ 0
  96. #define TEMP_CRIT 1
  97. #define TEMP_CRIT_HYST 2
  98. #define TEMP_WARN 3
  99. #define TEMP_WARN_HYST 4
  100. /*
  101. * only crit and crit_hyst affect real-time alarm status
  102. * current crit crit_hyst warn warn_hyst
  103. */
  104. static u16 W83793_REG_TEMP[][5] = {
  105. {0x1c, 0x78, 0x79, 0x7a, 0x7b},
  106. {0x1d, 0x7c, 0x7d, 0x7e, 0x7f},
  107. {0x1e, 0x80, 0x81, 0x82, 0x83},
  108. {0x1f, 0x84, 0x85, 0x86, 0x87},
  109. {0x20, 0x88, 0x89, 0x8a, 0x8b},
  110. {0x21, 0x8c, 0x8d, 0x8e, 0x8f},
  111. };
  112. #define W83793_REG_TEMP_LOW_BITS 0x22
  113. #define W83793_REG_BEEP(index) (0x53 + (index))
  114. #define W83793_REG_ALARM(index) (0x4b + (index))
  115. #define W83793_REG_CLR_CHASSIS 0x4a /* SMI MASK4 */
  116. #define W83793_REG_IRQ_CTRL 0x50
  117. #define W83793_REG_OVT_CTRL 0x51
  118. #define W83793_REG_OVT_BEEP 0x52
  119. #define IN_READ 0
  120. #define IN_MAX 1
  121. #define IN_LOW 2
  122. static const u16 W83793_REG_IN[][3] = {
  123. /* Current, High, Low */
  124. {0x10, 0x60, 0x61}, /* Vcore A */
  125. {0x11, 0x62, 0x63}, /* Vcore B */
  126. {0x12, 0x64, 0x65}, /* Vtt */
  127. {0x14, 0x6a, 0x6b}, /* VSEN1 */
  128. {0x15, 0x6c, 0x6d}, /* VSEN2 */
  129. {0x16, 0x6e, 0x6f}, /* +3VSEN */
  130. {0x17, 0x70, 0x71}, /* +12VSEN */
  131. {0x18, 0x72, 0x73}, /* 5VDD */
  132. {0x19, 0x74, 0x75}, /* 5VSB */
  133. {0x1a, 0x76, 0x77}, /* VBAT */
  134. };
  135. /* Low Bits of Vcore A/B Vtt Read/High/Low */
  136. static const u16 W83793_REG_IN_LOW_BITS[] = { 0x1b, 0x68, 0x69 };
  137. static u8 scale_in[] = { 2, 2, 2, 16, 16, 16, 8, 24, 24, 16 };
  138. static u8 scale_in_add[] = { 0, 0, 0, 0, 0, 0, 0, 150, 150, 0 };
  139. #define W83793_REG_FAN(index) (0x23 + 2 * (index)) /* High byte */
  140. #define W83793_REG_FAN_MIN(index) (0x90 + 2 * (index)) /* High byte */
  141. #define W83793_REG_PWM_DEFAULT 0xb2
  142. #define W83793_REG_PWM_ENABLE 0x207
  143. #define W83793_REG_PWM_UPTIME 0xc3 /* Unit in 0.1 second */
  144. #define W83793_REG_PWM_DOWNTIME 0xc4 /* Unit in 0.1 second */
  145. #define W83793_REG_TEMP_CRITICAL 0xc5
  146. #define PWM_DUTY 0
  147. #define PWM_START 1
  148. #define PWM_NONSTOP 2
  149. #define PWM_STOP_TIME 3
  150. #define W83793_REG_PWM(index, nr) (((nr) == 0 ? 0xb3 : \
  151. (nr) == 1 ? 0x220 : 0x218) + (index))
  152. /* bit field, fan1 is bit0, fan2 is bit1 ... */
  153. #define W83793_REG_TEMP_FAN_MAP(index) (0x201 + (index))
  154. #define W83793_REG_TEMP_TOL(index) (0x208 + (index))
  155. #define W83793_REG_TEMP_CRUISE(index) (0x210 + (index))
  156. #define W83793_REG_PWM_STOP_TIME(index) (0x228 + (index))
  157. #define W83793_REG_SF2_TEMP(index, nr) (0x230 + ((index) << 4) + (nr))
  158. #define W83793_REG_SF2_PWM(index, nr) (0x238 + ((index) << 4) + (nr))
  159. static inline unsigned long FAN_FROM_REG(u16 val)
  160. {
  161. if ((val >= 0xfff) || (val == 0))
  162. return 0;
  163. return 1350000UL / val;
  164. }
  165. static inline u16 FAN_TO_REG(long rpm)
  166. {
  167. if (rpm <= 0)
  168. return 0x0fff;
  169. return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
  170. }
  171. static inline unsigned long TIME_FROM_REG(u8 reg)
  172. {
  173. return reg * 100;
  174. }
  175. static inline u8 TIME_TO_REG(unsigned long val)
  176. {
  177. return clamp_val((val + 50) / 100, 0, 0xff);
  178. }
  179. static inline long TEMP_FROM_REG(s8 reg)
  180. {
  181. return reg * 1000;
  182. }
  183. static inline s8 TEMP_TO_REG(long val, s8 min, s8 max)
  184. {
  185. return clamp_val((val + (val < 0 ? -500 : 500)) / 1000, min, max);
  186. }
  187. struct w83793_data {
  188. struct i2c_client *lm75[2];
  189. struct device *hwmon_dev;
  190. struct mutex update_lock;
  191. char valid; /* !=0 if following fields are valid */
  192. unsigned long last_updated; /* In jiffies */
  193. unsigned long last_nonvolatile; /* In jiffies, last time we update the
  194. * nonvolatile registers
  195. */
  196. u8 bank;
  197. u8 vrm;
  198. u8 vid[2];
  199. u8 in[10][3]; /* Register value, read/high/low */
  200. u8 in_low_bits[3]; /* Additional resolution for VCore A/B Vtt */
  201. u16 has_fan; /* Only fan1- fan5 has own pins */
  202. u16 fan[12]; /* Register value combine */
  203. u16 fan_min[12]; /* Register value combine */
  204. s8 temp[6][5]; /* current, crit, crit_hyst,warn, warn_hyst */
  205. u8 temp_low_bits; /* Additional resolution TD1-TD4 */
  206. u8 temp_mode[2]; /* byte 0: Temp D1-D4 mode each has 2 bits
  207. * byte 1: Temp R1,R2 mode, each has 1 bit
  208. */
  209. u8 temp_critical; /* If reached all fan will be at full speed */
  210. u8 temp_fan_map[6]; /* Temp controls which pwm fan, bit field */
  211. u8 has_pwm;
  212. u8 has_temp;
  213. u8 has_vid;
  214. u8 pwm_enable; /* Register value, each Temp has 1 bit */
  215. u8 pwm_uptime; /* Register value */
  216. u8 pwm_downtime; /* Register value */
  217. u8 pwm_default; /* All fan default pwm, next poweron valid */
  218. u8 pwm[8][3]; /* Register value */
  219. u8 pwm_stop_time[8];
  220. u8 temp_cruise[6];
  221. u8 alarms[5]; /* realtime status registers */
  222. u8 beeps[5];
  223. u8 beep_enable;
  224. u8 tolerance[3]; /* Temp tolerance(Smart Fan I/II) */
  225. u8 sf2_pwm[6][7]; /* Smart FanII: Fan duty cycle */
  226. u8 sf2_temp[6][7]; /* Smart FanII: Temp level point */
  227. /* watchdog */
  228. struct i2c_client *client;
  229. struct mutex watchdog_lock;
  230. struct list_head list; /* member of the watchdog_data_list */
  231. struct kref kref;
  232. struct miscdevice watchdog_miscdev;
  233. unsigned long watchdog_is_open;
  234. char watchdog_expect_close;
  235. char watchdog_name[10]; /* must be unique to avoid sysfs conflict */
  236. unsigned int watchdog_caused_reboot;
  237. int watchdog_timeout; /* watchdog timeout in minutes */
  238. };
  239. /*
  240. * Somewhat ugly :( global data pointer list with all devices, so that
  241. * we can find our device data as when using misc_register. There is no
  242. * other method to get to one's device data from the open file-op and
  243. * for usage in the reboot notifier callback.
  244. */
  245. static LIST_HEAD(watchdog_data_list);
  246. /* Note this lock not only protect list access, but also data.kref access */
  247. static DEFINE_MUTEX(watchdog_data_mutex);
  248. /*
  249. * Release our data struct when we're detached from the i2c client *and* all
  250. * references to our watchdog device are released
  251. */
  252. static void w83793_release_resources(struct kref *ref)
  253. {
  254. struct w83793_data *data = container_of(ref, struct w83793_data, kref);
  255. kfree(data);
  256. }
  257. static u8 w83793_read_value(struct i2c_client *client, u16 reg);
  258. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value);
  259. static int w83793_probe(struct i2c_client *client,
  260. const struct i2c_device_id *id);
  261. static int w83793_detect(struct i2c_client *client,
  262. struct i2c_board_info *info);
  263. static int w83793_remove(struct i2c_client *client);
  264. static void w83793_init_client(struct i2c_client *client);
  265. static void w83793_update_nonvolatile(struct device *dev);
  266. static struct w83793_data *w83793_update_device(struct device *dev);
  267. static const struct i2c_device_id w83793_id[] = {
  268. { "w83793", 0 },
  269. { }
  270. };
  271. MODULE_DEVICE_TABLE(i2c, w83793_id);
  272. static struct i2c_driver w83793_driver = {
  273. .class = I2C_CLASS_HWMON,
  274. .driver = {
  275. .name = "w83793",
  276. },
  277. .probe = w83793_probe,
  278. .remove = w83793_remove,
  279. .id_table = w83793_id,
  280. .detect = w83793_detect,
  281. .address_list = normal_i2c,
  282. };
  283. static ssize_t
  284. show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  285. {
  286. struct w83793_data *data = dev_get_drvdata(dev);
  287. return sprintf(buf, "%d\n", data->vrm);
  288. }
  289. static ssize_t
  290. show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  291. {
  292. struct w83793_data *data = w83793_update_device(dev);
  293. struct sensor_device_attribute_2 *sensor_attr =
  294. to_sensor_dev_attr_2(attr);
  295. int index = sensor_attr->index;
  296. return sprintf(buf, "%d\n", vid_from_reg(data->vid[index], data->vrm));
  297. }
  298. static ssize_t
  299. store_vrm(struct device *dev, struct device_attribute *attr,
  300. const char *buf, size_t count)
  301. {
  302. struct w83793_data *data = dev_get_drvdata(dev);
  303. unsigned long val;
  304. int err;
  305. err = kstrtoul(buf, 10, &val);
  306. if (err)
  307. return err;
  308. 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 transform 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. unsigned int mtimeout;
  1062. int ret;
  1063. mtimeout = DIV_ROUND_UP(timeout, 60);
  1064. if (mtimeout > 255)
  1065. return -EINVAL;
  1066. mutex_lock(&data->watchdog_lock);
  1067. if (!data->client) {
  1068. ret = -ENODEV;
  1069. goto leave;
  1070. }
  1071. data->watchdog_timeout = mtimeout;
  1072. /* Set Timeout value (in Minutes) */
  1073. w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  1074. data->watchdog_timeout);
  1075. ret = mtimeout * 60;
  1076. leave:
  1077. mutex_unlock(&data->watchdog_lock);
  1078. return ret;
  1079. }
  1080. static int watchdog_get_timeout(struct w83793_data *data)
  1081. {
  1082. int timeout;
  1083. mutex_lock(&data->watchdog_lock);
  1084. timeout = data->watchdog_timeout * 60;
  1085. mutex_unlock(&data->watchdog_lock);
  1086. return timeout;
  1087. }
  1088. static int watchdog_trigger(struct w83793_data *data)
  1089. {
  1090. int ret = 0;
  1091. mutex_lock(&data->watchdog_lock);
  1092. if (!data->client) {
  1093. ret = -ENODEV;
  1094. goto leave;
  1095. }
  1096. /* Set Timeout value (in Minutes) */
  1097. w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  1098. data->watchdog_timeout);
  1099. leave:
  1100. mutex_unlock(&data->watchdog_lock);
  1101. return ret;
  1102. }
  1103. static int watchdog_enable(struct w83793_data *data)
  1104. {
  1105. int ret = 0;
  1106. mutex_lock(&data->watchdog_lock);
  1107. if (!data->client) {
  1108. ret = -ENODEV;
  1109. goto leave;
  1110. }
  1111. /* Set initial timeout */
  1112. w83793_write_value(data->client, W83793_REG_WDT_TIMEOUT,
  1113. data->watchdog_timeout);
  1114. /* Enable Soft Watchdog */
  1115. w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0x55);
  1116. leave:
  1117. mutex_unlock(&data->watchdog_lock);
  1118. return ret;
  1119. }
  1120. static int watchdog_disable(struct w83793_data *data)
  1121. {
  1122. int ret = 0;
  1123. mutex_lock(&data->watchdog_lock);
  1124. if (!data->client) {
  1125. ret = -ENODEV;
  1126. goto leave;
  1127. }
  1128. /* Disable Soft Watchdog */
  1129. w83793_write_value(data->client, W83793_REG_WDT_LOCK, 0xAA);
  1130. leave:
  1131. mutex_unlock(&data->watchdog_lock);
  1132. return ret;
  1133. }
  1134. static int watchdog_open(struct inode *inode, struct file *filp)
  1135. {
  1136. struct w83793_data *pos, *data = NULL;
  1137. int watchdog_is_open;
  1138. /*
  1139. * We get called from drivers/char/misc.c with misc_mtx hold, and we
  1140. * call misc_register() from w83793_probe() with watchdog_data_mutex
  1141. * hold, as misc_register() takes the misc_mtx lock, this is a possible
  1142. * deadlock, so we use mutex_trylock here.
  1143. */
  1144. if (!mutex_trylock(&watchdog_data_mutex))
  1145. return -ERESTARTSYS;
  1146. list_for_each_entry(pos, &watchdog_data_list, list) {
  1147. if (pos->watchdog_miscdev.minor == iminor(inode)) {
  1148. data = pos;
  1149. break;
  1150. }
  1151. }
  1152. /* Check, if device is already open */
  1153. watchdog_is_open = test_and_set_bit(0, &data->watchdog_is_open);
  1154. /*
  1155. * Increase data reference counter (if not already done).
  1156. * Note we can never not have found data, so we don't check for this
  1157. */
  1158. if (!watchdog_is_open)
  1159. kref_get(&data->kref);
  1160. mutex_unlock(&watchdog_data_mutex);
  1161. /* Check, if device is already open and possibly issue error */
  1162. if (watchdog_is_open)
  1163. return -EBUSY;
  1164. /* Enable Soft Watchdog */
  1165. watchdog_enable(data);
  1166. /* Store pointer to data into filp's private data */
  1167. filp->private_data = data;
  1168. return nonseekable_open(inode, filp);
  1169. }
  1170. static int watchdog_close(struct inode *inode, struct file *filp)
  1171. {
  1172. struct w83793_data *data = filp->private_data;
  1173. if (data->watchdog_expect_close) {
  1174. watchdog_disable(data);
  1175. data->watchdog_expect_close = 0;
  1176. } else {
  1177. watchdog_trigger(data);
  1178. dev_crit(&data->client->dev,
  1179. "unexpected close, not stopping watchdog!\n");
  1180. }
  1181. clear_bit(0, &data->watchdog_is_open);
  1182. /* Decrease data reference counter */
  1183. mutex_lock(&watchdog_data_mutex);
  1184. kref_put(&data->kref, w83793_release_resources);
  1185. mutex_unlock(&watchdog_data_mutex);
  1186. return 0;
  1187. }
  1188. static ssize_t watchdog_write(struct file *filp, const char __user *buf,
  1189. size_t count, loff_t *offset)
  1190. {
  1191. ssize_t ret;
  1192. struct w83793_data *data = filp->private_data;
  1193. if (count) {
  1194. if (!nowayout) {
  1195. size_t i;
  1196. /* Clear it in case it was set with a previous write */
  1197. data->watchdog_expect_close = 0;
  1198. for (i = 0; i != count; i++) {
  1199. char c;
  1200. if (get_user(c, buf + i))
  1201. return -EFAULT;
  1202. if (c == 'V')
  1203. data->watchdog_expect_close = 1;
  1204. }
  1205. }
  1206. ret = watchdog_trigger(data);
  1207. if (ret < 0)
  1208. return ret;
  1209. }
  1210. return count;
  1211. }
  1212. static long watchdog_ioctl(struct file *filp, unsigned int cmd,
  1213. unsigned long arg)
  1214. {
  1215. struct watchdog_info ident = {
  1216. .options = WDIOF_KEEPALIVEPING |
  1217. WDIOF_SETTIMEOUT |
  1218. WDIOF_CARDRESET,
  1219. .identity = "w83793 watchdog"
  1220. };
  1221. int val, ret = 0;
  1222. struct w83793_data *data = filp->private_data;
  1223. switch (cmd) {
  1224. case WDIOC_GETSUPPORT:
  1225. if (!nowayout)
  1226. ident.options |= WDIOF_MAGICCLOSE;
  1227. if (copy_to_user((void __user *)arg, &ident, sizeof(ident)))
  1228. ret = -EFAULT;
  1229. break;
  1230. case WDIOC_GETSTATUS:
  1231. val = data->watchdog_caused_reboot ? WDIOF_CARDRESET : 0;
  1232. ret = put_user(val, (int __user *)arg);
  1233. break;
  1234. case WDIOC_GETBOOTSTATUS:
  1235. ret = put_user(0, (int __user *)arg);
  1236. break;
  1237. case WDIOC_KEEPALIVE:
  1238. ret = watchdog_trigger(data);
  1239. break;
  1240. case WDIOC_GETTIMEOUT:
  1241. val = watchdog_get_timeout(data);
  1242. ret = put_user(val, (int __user *)arg);
  1243. break;
  1244. case WDIOC_SETTIMEOUT:
  1245. if (get_user(val, (int __user *)arg)) {
  1246. ret = -EFAULT;
  1247. break;
  1248. }
  1249. ret = watchdog_set_timeout(data, val);
  1250. if (ret > 0)
  1251. ret = put_user(ret, (int __user *)arg);
  1252. break;
  1253. case WDIOC_SETOPTIONS:
  1254. if (get_user(val, (int __user *)arg)) {
  1255. ret = -EFAULT;
  1256. break;
  1257. }
  1258. if (val & WDIOS_DISABLECARD)
  1259. ret = watchdog_disable(data);
  1260. else if (val & WDIOS_ENABLECARD)
  1261. ret = watchdog_enable(data);
  1262. else
  1263. ret = -EINVAL;
  1264. break;
  1265. default:
  1266. ret = -ENOTTY;
  1267. }
  1268. return ret;
  1269. }
  1270. static const struct file_operations watchdog_fops = {
  1271. .owner = THIS_MODULE,
  1272. .llseek = no_llseek,
  1273. .open = watchdog_open,
  1274. .release = watchdog_close,
  1275. .write = watchdog_write,
  1276. .unlocked_ioctl = watchdog_ioctl,
  1277. };
  1278. /*
  1279. * Notifier for system down
  1280. */
  1281. static int watchdog_notify_sys(struct notifier_block *this, unsigned long code,
  1282. void *unused)
  1283. {
  1284. struct w83793_data *data = NULL;
  1285. if (code == SYS_DOWN || code == SYS_HALT) {
  1286. /* Disable each registered watchdog */
  1287. mutex_lock(&watchdog_data_mutex);
  1288. list_for_each_entry(data, &watchdog_data_list, list) {
  1289. if (data->watchdog_miscdev.minor)
  1290. watchdog_disable(data);
  1291. }
  1292. mutex_unlock(&watchdog_data_mutex);
  1293. }
  1294. return NOTIFY_DONE;
  1295. }
  1296. /*
  1297. * The WDT needs to learn about soft shutdowns in order to
  1298. * turn the timebomb registers off.
  1299. */
  1300. static struct notifier_block watchdog_notifier = {
  1301. .notifier_call = watchdog_notify_sys,
  1302. };
  1303. /*
  1304. * Init / remove routines
  1305. */
  1306. static int w83793_remove(struct i2c_client *client)
  1307. {
  1308. struct w83793_data *data = i2c_get_clientdata(client);
  1309. struct device *dev = &client->dev;
  1310. int i, tmp;
  1311. /* Unregister the watchdog (if registered) */
  1312. if (data->watchdog_miscdev.minor) {
  1313. misc_deregister(&data->watchdog_miscdev);
  1314. if (data->watchdog_is_open) {
  1315. dev_warn(&client->dev,
  1316. "i2c client detached with watchdog open! "
  1317. "Stopping watchdog.\n");
  1318. watchdog_disable(data);
  1319. }
  1320. mutex_lock(&watchdog_data_mutex);
  1321. list_del(&data->list);
  1322. mutex_unlock(&watchdog_data_mutex);
  1323. /* Tell the watchdog code the client is gone */
  1324. mutex_lock(&data->watchdog_lock);
  1325. data->client = NULL;
  1326. mutex_unlock(&data->watchdog_lock);
  1327. }
  1328. /* Reset Configuration Register to Disable Watch Dog Registers */
  1329. tmp = w83793_read_value(client, W83793_REG_CONFIG);
  1330. w83793_write_value(client, W83793_REG_CONFIG, tmp & ~0x04);
  1331. unregister_reboot_notifier(&watchdog_notifier);
  1332. hwmon_device_unregister(data->hwmon_dev);
  1333. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1334. device_remove_file(dev,
  1335. &w83793_sensor_attr_2[i].dev_attr);
  1336. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1337. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1338. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1339. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1340. device_remove_file(dev, &dev_attr_vrm);
  1341. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1342. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1343. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1344. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1345. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1346. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1347. if (data->lm75[0] != NULL)
  1348. i2c_unregister_device(data->lm75[0]);
  1349. if (data->lm75[1] != NULL)
  1350. i2c_unregister_device(data->lm75[1]);
  1351. /* Decrease data reference counter */
  1352. mutex_lock(&watchdog_data_mutex);
  1353. kref_put(&data->kref, w83793_release_resources);
  1354. mutex_unlock(&watchdog_data_mutex);
  1355. return 0;
  1356. }
  1357. static int
  1358. w83793_detect_subclients(struct i2c_client *client)
  1359. {
  1360. int i, id, err;
  1361. int address = client->addr;
  1362. u8 tmp;
  1363. struct i2c_adapter *adapter = client->adapter;
  1364. struct w83793_data *data = i2c_get_clientdata(client);
  1365. id = i2c_adapter_id(adapter);
  1366. if (force_subclients[0] == id && force_subclients[1] == address) {
  1367. for (i = 2; i <= 3; i++) {
  1368. if (force_subclients[i] < 0x48
  1369. || force_subclients[i] > 0x4f) {
  1370. dev_err(&client->dev,
  1371. "invalid subclient "
  1372. "address %d; must be 0x48-0x4f\n",
  1373. force_subclients[i]);
  1374. err = -EINVAL;
  1375. goto ERROR_SC_0;
  1376. }
  1377. }
  1378. w83793_write_value(client, W83793_REG_I2C_SUBADDR,
  1379. (force_subclients[2] & 0x07) |
  1380. ((force_subclients[3] & 0x07) << 4));
  1381. }
  1382. tmp = w83793_read_value(client, W83793_REG_I2C_SUBADDR);
  1383. if (!(tmp & 0x08))
  1384. data->lm75[0] = i2c_new_dummy(adapter, 0x48 + (tmp & 0x7));
  1385. if (!(tmp & 0x80)) {
  1386. if ((data->lm75[0] != NULL)
  1387. && ((tmp & 0x7) == ((tmp >> 4) & 0x7))) {
  1388. dev_err(&client->dev,
  1389. "duplicate addresses 0x%x, "
  1390. "use force_subclients\n", data->lm75[0]->addr);
  1391. err = -ENODEV;
  1392. goto ERROR_SC_1;
  1393. }
  1394. data->lm75[1] = i2c_new_dummy(adapter,
  1395. 0x48 + ((tmp >> 4) & 0x7));
  1396. }
  1397. return 0;
  1398. /* Undo inits in case of errors */
  1399. ERROR_SC_1:
  1400. if (data->lm75[0] != NULL)
  1401. i2c_unregister_device(data->lm75[0]);
  1402. ERROR_SC_0:
  1403. return err;
  1404. }
  1405. /* Return 0 if detection is successful, -ENODEV otherwise */
  1406. static int w83793_detect(struct i2c_client *client,
  1407. struct i2c_board_info *info)
  1408. {
  1409. u8 tmp, bank, chip_id;
  1410. struct i2c_adapter *adapter = client->adapter;
  1411. unsigned short address = client->addr;
  1412. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1413. return -ENODEV;
  1414. bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1415. tmp = bank & 0x80 ? 0x5c : 0xa3;
  1416. /* Check Winbond vendor ID */
  1417. if (tmp != i2c_smbus_read_byte_data(client, W83793_REG_VENDORID)) {
  1418. pr_debug("w83793: Detection failed at check vendor id\n");
  1419. return -ENODEV;
  1420. }
  1421. /*
  1422. * If Winbond chip, address of chip and W83793_REG_I2C_ADDR
  1423. * should match
  1424. */
  1425. if ((bank & 0x07) == 0
  1426. && i2c_smbus_read_byte_data(client, W83793_REG_I2C_ADDR) !=
  1427. (address << 1)) {
  1428. pr_debug("w83793: Detection failed at check i2c addr\n");
  1429. return -ENODEV;
  1430. }
  1431. /* Determine the chip type now */
  1432. chip_id = i2c_smbus_read_byte_data(client, W83793_REG_CHIPID);
  1433. if (chip_id != 0x7b)
  1434. return -ENODEV;
  1435. strlcpy(info->type, "w83793", I2C_NAME_SIZE);
  1436. return 0;
  1437. }
  1438. static int w83793_probe(struct i2c_client *client,
  1439. const struct i2c_device_id *id)
  1440. {
  1441. struct device *dev = &client->dev;
  1442. const int watchdog_minors[] = { WATCHDOG_MINOR, 212, 213, 214, 215 };
  1443. struct w83793_data *data;
  1444. int i, tmp, val, err;
  1445. int files_fan = ARRAY_SIZE(w83793_left_fan) / 7;
  1446. int files_pwm = ARRAY_SIZE(w83793_left_pwm) / 5;
  1447. int files_temp = ARRAY_SIZE(w83793_temp) / 6;
  1448. data = kzalloc(sizeof(struct w83793_data), GFP_KERNEL);
  1449. if (!data) {
  1450. err = -ENOMEM;
  1451. goto exit;
  1452. }
  1453. i2c_set_clientdata(client, data);
  1454. data->bank = i2c_smbus_read_byte_data(client, W83793_REG_BANKSEL);
  1455. mutex_init(&data->update_lock);
  1456. mutex_init(&data->watchdog_lock);
  1457. INIT_LIST_HEAD(&data->list);
  1458. kref_init(&data->kref);
  1459. /*
  1460. * Store client pointer in our data struct for watchdog usage
  1461. * (where the client is found through a data ptr instead of the
  1462. * otherway around)
  1463. */
  1464. data->client = client;
  1465. err = w83793_detect_subclients(client);
  1466. if (err)
  1467. goto free_mem;
  1468. /* Initialize the chip */
  1469. w83793_init_client(client);
  1470. /*
  1471. * Only fan 1-5 has their own input pins,
  1472. * Pwm 1-3 has their own pins
  1473. */
  1474. data->has_fan = 0x1f;
  1475. data->has_pwm = 0x07;
  1476. tmp = w83793_read_value(client, W83793_REG_MFC);
  1477. val = w83793_read_value(client, W83793_REG_FANIN_CTRL);
  1478. /* check the function of pins 49-56 */
  1479. if (tmp & 0x80) {
  1480. data->has_vid |= 0x2; /* has VIDB */
  1481. } else {
  1482. data->has_pwm |= 0x18; /* pwm 4,5 */
  1483. if (val & 0x01) { /* fan 6 */
  1484. data->has_fan |= 0x20;
  1485. data->has_pwm |= 0x20;
  1486. }
  1487. if (val & 0x02) { /* fan 7 */
  1488. data->has_fan |= 0x40;
  1489. data->has_pwm |= 0x40;
  1490. }
  1491. if (!(tmp & 0x40) && (val & 0x04)) { /* fan 8 */
  1492. data->has_fan |= 0x80;
  1493. data->has_pwm |= 0x80;
  1494. }
  1495. }
  1496. /* check the function of pins 37-40 */
  1497. if (!(tmp & 0x29))
  1498. data->has_vid |= 0x1; /* has VIDA */
  1499. if (0x08 == (tmp & 0x0c)) {
  1500. if (val & 0x08) /* fan 9 */
  1501. data->has_fan |= 0x100;
  1502. if (val & 0x10) /* fan 10 */
  1503. data->has_fan |= 0x200;
  1504. }
  1505. if (0x20 == (tmp & 0x30)) {
  1506. if (val & 0x20) /* fan 11 */
  1507. data->has_fan |= 0x400;
  1508. if (val & 0x40) /* fan 12 */
  1509. data->has_fan |= 0x800;
  1510. }
  1511. if ((tmp & 0x01) && (val & 0x04)) { /* fan 8, second location */
  1512. data->has_fan |= 0x80;
  1513. data->has_pwm |= 0x80;
  1514. }
  1515. tmp = w83793_read_value(client, W83793_REG_FANIN_SEL);
  1516. if ((tmp & 0x01) && (val & 0x08)) { /* fan 9, second location */
  1517. data->has_fan |= 0x100;
  1518. }
  1519. if ((tmp & 0x02) && (val & 0x10)) { /* fan 10, second location */
  1520. data->has_fan |= 0x200;
  1521. }
  1522. if ((tmp & 0x04) && (val & 0x20)) { /* fan 11, second location */
  1523. data->has_fan |= 0x400;
  1524. }
  1525. if ((tmp & 0x08) && (val & 0x40)) { /* fan 12, second location */
  1526. data->has_fan |= 0x800;
  1527. }
  1528. /* check the temp1-6 mode, ignore former AMDSI selected inputs */
  1529. tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[0]);
  1530. if (tmp & 0x01)
  1531. data->has_temp |= 0x01;
  1532. if (tmp & 0x04)
  1533. data->has_temp |= 0x02;
  1534. if (tmp & 0x10)
  1535. data->has_temp |= 0x04;
  1536. if (tmp & 0x40)
  1537. data->has_temp |= 0x08;
  1538. tmp = w83793_read_value(client, W83793_REG_TEMP_MODE[1]);
  1539. if (tmp & 0x01)
  1540. data->has_temp |= 0x10;
  1541. if (tmp & 0x02)
  1542. data->has_temp |= 0x20;
  1543. /* Register sysfs hooks */
  1544. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++) {
  1545. err = device_create_file(dev,
  1546. &w83793_sensor_attr_2[i].dev_attr);
  1547. if (err)
  1548. goto exit_remove;
  1549. }
  1550. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++) {
  1551. if (!(data->has_vid & (1 << i)))
  1552. continue;
  1553. err = device_create_file(dev, &w83793_vid[i].dev_attr);
  1554. if (err)
  1555. goto exit_remove;
  1556. }
  1557. if (data->has_vid) {
  1558. data->vrm = vid_which_vrm();
  1559. err = device_create_file(dev, &dev_attr_vrm);
  1560. if (err)
  1561. goto exit_remove;
  1562. }
  1563. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
  1564. err = device_create_file(dev, &sda_single_files[i].dev_attr);
  1565. if (err)
  1566. goto exit_remove;
  1567. }
  1568. for (i = 0; i < 6; i++) {
  1569. int j;
  1570. if (!(data->has_temp & (1 << i)))
  1571. continue;
  1572. for (j = 0; j < files_temp; j++) {
  1573. err = device_create_file(dev,
  1574. &w83793_temp[(i) * files_temp
  1575. + j].dev_attr);
  1576. if (err)
  1577. goto exit_remove;
  1578. }
  1579. }
  1580. for (i = 5; i < 12; i++) {
  1581. int j;
  1582. if (!(data->has_fan & (1 << i)))
  1583. continue;
  1584. for (j = 0; j < files_fan; j++) {
  1585. err = device_create_file(dev,
  1586. &w83793_left_fan[(i - 5) * files_fan
  1587. + j].dev_attr);
  1588. if (err)
  1589. goto exit_remove;
  1590. }
  1591. }
  1592. for (i = 3; i < 8; i++) {
  1593. int j;
  1594. if (!(data->has_pwm & (1 << i)))
  1595. continue;
  1596. for (j = 0; j < files_pwm; j++) {
  1597. err = device_create_file(dev,
  1598. &w83793_left_pwm[(i - 3) * files_pwm
  1599. + j].dev_attr);
  1600. if (err)
  1601. goto exit_remove;
  1602. }
  1603. }
  1604. data->hwmon_dev = hwmon_device_register(dev);
  1605. if (IS_ERR(data->hwmon_dev)) {
  1606. err = PTR_ERR(data->hwmon_dev);
  1607. goto exit_remove;
  1608. }
  1609. /* Watchdog initialization */
  1610. /* Register boot notifier */
  1611. err = register_reboot_notifier(&watchdog_notifier);
  1612. if (err != 0) {
  1613. dev_err(&client->dev,
  1614. "cannot register reboot notifier (err=%d)\n", err);
  1615. goto exit_devunreg;
  1616. }
  1617. /*
  1618. * Enable Watchdog registers.
  1619. * Set Configuration Register to Enable Watch Dog Registers
  1620. * (Bit 2) = XXXX, X1XX.
  1621. */
  1622. tmp = w83793_read_value(client, W83793_REG_CONFIG);
  1623. w83793_write_value(client, W83793_REG_CONFIG, tmp | 0x04);
  1624. /* Set the default watchdog timeout */
  1625. data->watchdog_timeout = timeout;
  1626. /* Check, if last reboot was caused by watchdog */
  1627. data->watchdog_caused_reboot =
  1628. w83793_read_value(data->client, W83793_REG_WDT_STATUS) & 0x01;
  1629. /* Disable Soft Watchdog during initialiation */
  1630. watchdog_disable(data);
  1631. /*
  1632. * We take the data_mutex lock early so that watchdog_open() cannot
  1633. * run when misc_register() has completed, but we've not yet added
  1634. * our data to the watchdog_data_list (and set the default timeout)
  1635. */
  1636. mutex_lock(&watchdog_data_mutex);
  1637. for (i = 0; i < ARRAY_SIZE(watchdog_minors); i++) {
  1638. /* Register our watchdog part */
  1639. snprintf(data->watchdog_name, sizeof(data->watchdog_name),
  1640. "watchdog%c", (i == 0) ? '\0' : ('0' + i));
  1641. data->watchdog_miscdev.name = data->watchdog_name;
  1642. data->watchdog_miscdev.fops = &watchdog_fops;
  1643. data->watchdog_miscdev.minor = watchdog_minors[i];
  1644. err = misc_register(&data->watchdog_miscdev);
  1645. if (err == -EBUSY)
  1646. continue;
  1647. if (err) {
  1648. data->watchdog_miscdev.minor = 0;
  1649. dev_err(&client->dev,
  1650. "Registering watchdog chardev: %d\n", err);
  1651. break;
  1652. }
  1653. list_add(&data->list, &watchdog_data_list);
  1654. dev_info(&client->dev,
  1655. "Registered watchdog chardev major 10, minor: %d\n",
  1656. watchdog_minors[i]);
  1657. break;
  1658. }
  1659. if (i == ARRAY_SIZE(watchdog_minors)) {
  1660. data->watchdog_miscdev.minor = 0;
  1661. dev_warn(&client->dev,
  1662. "Couldn't register watchdog chardev (due to no free minor)\n");
  1663. }
  1664. mutex_unlock(&watchdog_data_mutex);
  1665. return 0;
  1666. /* Unregister hwmon device */
  1667. exit_devunreg:
  1668. hwmon_device_unregister(data->hwmon_dev);
  1669. /* Unregister sysfs hooks */
  1670. exit_remove:
  1671. for (i = 0; i < ARRAY_SIZE(w83793_sensor_attr_2); i++)
  1672. device_remove_file(dev, &w83793_sensor_attr_2[i].dev_attr);
  1673. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++)
  1674. device_remove_file(dev, &sda_single_files[i].dev_attr);
  1675. for (i = 0; i < ARRAY_SIZE(w83793_vid); i++)
  1676. device_remove_file(dev, &w83793_vid[i].dev_attr);
  1677. for (i = 0; i < ARRAY_SIZE(w83793_left_fan); i++)
  1678. device_remove_file(dev, &w83793_left_fan[i].dev_attr);
  1679. for (i = 0; i < ARRAY_SIZE(w83793_left_pwm); i++)
  1680. device_remove_file(dev, &w83793_left_pwm[i].dev_attr);
  1681. for (i = 0; i < ARRAY_SIZE(w83793_temp); i++)
  1682. device_remove_file(dev, &w83793_temp[i].dev_attr);
  1683. if (data->lm75[0] != NULL)
  1684. i2c_unregister_device(data->lm75[0]);
  1685. if (data->lm75[1] != NULL)
  1686. i2c_unregister_device(data->lm75[1]);
  1687. free_mem:
  1688. kfree(data);
  1689. exit:
  1690. return err;
  1691. }
  1692. static void w83793_update_nonvolatile(struct device *dev)
  1693. {
  1694. struct i2c_client *client = to_i2c_client(dev);
  1695. struct w83793_data *data = i2c_get_clientdata(client);
  1696. int i, j;
  1697. /*
  1698. * They are somewhat "stable" registers, and to update them every time
  1699. * takes so much time, it's just not worthy. Update them in a long
  1700. * interval to avoid exception.
  1701. */
  1702. if (!(time_after(jiffies, data->last_nonvolatile + HZ * 300)
  1703. || !data->valid))
  1704. return;
  1705. /* update voltage limits */
  1706. for (i = 1; i < 3; i++) {
  1707. for (j = 0; j < ARRAY_SIZE(data->in); j++) {
  1708. data->in[j][i] =
  1709. w83793_read_value(client, W83793_REG_IN[j][i]);
  1710. }
  1711. data->in_low_bits[i] =
  1712. w83793_read_value(client, W83793_REG_IN_LOW_BITS[i]);
  1713. }
  1714. for (i = 0; i < ARRAY_SIZE(data->fan_min); i++) {
  1715. /* Update the Fan measured value and limits */
  1716. if (!(data->has_fan & (1 << i)))
  1717. continue;
  1718. data->fan_min[i] =
  1719. w83793_read_value(client, W83793_REG_FAN_MIN(i)) << 8;
  1720. data->fan_min[i] |=
  1721. w83793_read_value(client, W83793_REG_FAN_MIN(i) + 1);
  1722. }
  1723. for (i = 0; i < ARRAY_SIZE(data->temp_fan_map); i++) {
  1724. if (!(data->has_temp & (1 << i)))
  1725. continue;
  1726. data->temp_fan_map[i] =
  1727. w83793_read_value(client, W83793_REG_TEMP_FAN_MAP(i));
  1728. for (j = 1; j < 5; j++) {
  1729. data->temp[i][j] =
  1730. w83793_read_value(client, W83793_REG_TEMP[i][j]);
  1731. }
  1732. data->temp_cruise[i] =
  1733. w83793_read_value(client, W83793_REG_TEMP_CRUISE(i));
  1734. for (j = 0; j < 7; j++) {
  1735. data->sf2_pwm[i][j] =
  1736. w83793_read_value(client, W83793_REG_SF2_PWM(i, j));
  1737. data->sf2_temp[i][j] =
  1738. w83793_read_value(client,
  1739. W83793_REG_SF2_TEMP(i, j));
  1740. }
  1741. }
  1742. for (i = 0; i < ARRAY_SIZE(data->temp_mode); i++)
  1743. data->temp_mode[i] =
  1744. w83793_read_value(client, W83793_REG_TEMP_MODE[i]);
  1745. for (i = 0; i < ARRAY_SIZE(data->tolerance); i++) {
  1746. data->tolerance[i] =
  1747. w83793_read_value(client, W83793_REG_TEMP_TOL(i));
  1748. }
  1749. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1750. if (!(data->has_pwm & (1 << i)))
  1751. continue;
  1752. data->pwm[i][PWM_NONSTOP] =
  1753. w83793_read_value(client, W83793_REG_PWM(i, PWM_NONSTOP));
  1754. data->pwm[i][PWM_START] =
  1755. w83793_read_value(client, W83793_REG_PWM(i, PWM_START));
  1756. data->pwm_stop_time[i] =
  1757. w83793_read_value(client, W83793_REG_PWM_STOP_TIME(i));
  1758. }
  1759. data->pwm_default = w83793_read_value(client, W83793_REG_PWM_DEFAULT);
  1760. data->pwm_enable = w83793_read_value(client, W83793_REG_PWM_ENABLE);
  1761. data->pwm_uptime = w83793_read_value(client, W83793_REG_PWM_UPTIME);
  1762. data->pwm_downtime = w83793_read_value(client, W83793_REG_PWM_DOWNTIME);
  1763. data->temp_critical =
  1764. w83793_read_value(client, W83793_REG_TEMP_CRITICAL);
  1765. data->beep_enable = w83793_read_value(client, W83793_REG_OVT_BEEP);
  1766. for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
  1767. data->beeps[i] = w83793_read_value(client, W83793_REG_BEEP(i));
  1768. data->last_nonvolatile = jiffies;
  1769. }
  1770. static struct w83793_data *w83793_update_device(struct device *dev)
  1771. {
  1772. struct i2c_client *client = to_i2c_client(dev);
  1773. struct w83793_data *data = i2c_get_clientdata(client);
  1774. int i;
  1775. mutex_lock(&data->update_lock);
  1776. if (!(time_after(jiffies, data->last_updated + HZ * 2)
  1777. || !data->valid))
  1778. goto END;
  1779. /* Update the voltages measured value and limits */
  1780. for (i = 0; i < ARRAY_SIZE(data->in); i++)
  1781. data->in[i][IN_READ] =
  1782. w83793_read_value(client, W83793_REG_IN[i][IN_READ]);
  1783. data->in_low_bits[IN_READ] =
  1784. w83793_read_value(client, W83793_REG_IN_LOW_BITS[IN_READ]);
  1785. for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
  1786. if (!(data->has_fan & (1 << i)))
  1787. continue;
  1788. data->fan[i] =
  1789. w83793_read_value(client, W83793_REG_FAN(i)) << 8;
  1790. data->fan[i] |=
  1791. w83793_read_value(client, W83793_REG_FAN(i) + 1);
  1792. }
  1793. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  1794. if (!(data->has_temp & (1 << i)))
  1795. continue;
  1796. data->temp[i][TEMP_READ] =
  1797. w83793_read_value(client, W83793_REG_TEMP[i][TEMP_READ]);
  1798. }
  1799. data->temp_low_bits =
  1800. w83793_read_value(client, W83793_REG_TEMP_LOW_BITS);
  1801. for (i = 0; i < ARRAY_SIZE(data->pwm); i++) {
  1802. if (data->has_pwm & (1 << i))
  1803. data->pwm[i][PWM_DUTY] =
  1804. w83793_read_value(client,
  1805. W83793_REG_PWM(i, PWM_DUTY));
  1806. }
  1807. for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
  1808. data->alarms[i] =
  1809. w83793_read_value(client, W83793_REG_ALARM(i));
  1810. if (data->has_vid & 0x01)
  1811. data->vid[0] = w83793_read_value(client, W83793_REG_VID_INA);
  1812. if (data->has_vid & 0x02)
  1813. data->vid[1] = w83793_read_value(client, W83793_REG_VID_INB);
  1814. w83793_update_nonvolatile(dev);
  1815. data->last_updated = jiffies;
  1816. data->valid = 1;
  1817. END:
  1818. mutex_unlock(&data->update_lock);
  1819. return data;
  1820. }
  1821. /*
  1822. * Ignore the possibility that somebody change bank outside the driver
  1823. * Must be called with data->update_lock held, except during initialization
  1824. */
  1825. static u8 w83793_read_value(struct i2c_client *client, u16 reg)
  1826. {
  1827. struct w83793_data *data = i2c_get_clientdata(client);
  1828. u8 res = 0xff;
  1829. u8 new_bank = reg >> 8;
  1830. new_bank |= data->bank & 0xfc;
  1831. if (data->bank != new_bank) {
  1832. if (i2c_smbus_write_byte_data
  1833. (client, W83793_REG_BANKSEL, new_bank) >= 0)
  1834. data->bank = new_bank;
  1835. else {
  1836. dev_err(&client->dev,
  1837. "set bank to %d failed, fall back "
  1838. "to bank %d, read reg 0x%x error\n",
  1839. new_bank, data->bank, reg);
  1840. res = 0x0; /* read 0x0 from the chip */
  1841. goto END;
  1842. }
  1843. }
  1844. res = i2c_smbus_read_byte_data(client, reg & 0xff);
  1845. END:
  1846. return res;
  1847. }
  1848. /* Must be called with data->update_lock held, except during initialization */
  1849. static int w83793_write_value(struct i2c_client *client, u16 reg, u8 value)
  1850. {
  1851. struct w83793_data *data = i2c_get_clientdata(client);
  1852. int res;
  1853. u8 new_bank = reg >> 8;
  1854. new_bank |= data->bank & 0xfc;
  1855. if (data->bank != new_bank) {
  1856. res = i2c_smbus_write_byte_data(client, W83793_REG_BANKSEL,
  1857. new_bank);
  1858. if (res < 0) {
  1859. dev_err(&client->dev,
  1860. "set bank to %d failed, fall back "
  1861. "to bank %d, write reg 0x%x error\n",
  1862. new_bank, data->bank, reg);
  1863. goto END;
  1864. }
  1865. data->bank = new_bank;
  1866. }
  1867. res = i2c_smbus_write_byte_data(client, reg & 0xff, value);
  1868. END:
  1869. return res;
  1870. }
  1871. module_i2c_driver(w83793_driver);
  1872. MODULE_AUTHOR("Yuan Mu, Sven Anders");
  1873. MODULE_DESCRIPTION("w83793 driver");
  1874. MODULE_LICENSE("GPL");