w83795.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286
  1. /*
  2. * w83795.c - Linux kernel driver for hardware monitoring
  3. * Copyright (C) 2008 Nuvoton Technology Corp.
  4. * Wei Song
  5. * Copyright (C) 2010 Jean Delvare <jdelvare@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation - version 2.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA.
  20. *
  21. * Supports following chips:
  22. *
  23. * Chip #vin #fanin #pwm #temp #dts wchipid vendid i2c ISA
  24. * w83795g 21 14 8 6 8 0x79 0x5ca3 yes no
  25. * w83795adg 18 14 2 6 8 0x79 0x5ca3 yes no
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/i2c.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/util_macros.h>
  38. /* Addresses to scan */
  39. static const unsigned short normal_i2c[] = {
  40. 0x2c, 0x2d, 0x2e, 0x2f, I2C_CLIENT_END
  41. };
  42. static bool reset;
  43. module_param(reset, bool, 0);
  44. MODULE_PARM_DESC(reset, "Set to 1 to reset chip, not recommended");
  45. #define W83795_REG_BANKSEL 0x00
  46. #define W83795_REG_VENDORID 0xfd
  47. #define W83795_REG_CHIPID 0xfe
  48. #define W83795_REG_DEVICEID 0xfb
  49. #define W83795_REG_DEVICEID_A 0xff
  50. #define W83795_REG_I2C_ADDR 0xfc
  51. #define W83795_REG_CONFIG 0x01
  52. #define W83795_REG_CONFIG_CONFIG48 0x04
  53. #define W83795_REG_CONFIG_START 0x01
  54. /* Multi-Function Pin Ctrl Registers */
  55. #define W83795_REG_VOLT_CTRL1 0x02
  56. #define W83795_REG_VOLT_CTRL2 0x03
  57. #define W83795_REG_TEMP_CTRL1 0x04
  58. #define W83795_REG_TEMP_CTRL2 0x05
  59. #define W83795_REG_FANIN_CTRL1 0x06
  60. #define W83795_REG_FANIN_CTRL2 0x07
  61. #define W83795_REG_VMIGB_CTRL 0x08
  62. #define TEMP_READ 0
  63. #define TEMP_CRIT 1
  64. #define TEMP_CRIT_HYST 2
  65. #define TEMP_WARN 3
  66. #define TEMP_WARN_HYST 4
  67. /*
  68. * only crit and crit_hyst affect real-time alarm status
  69. * current crit crit_hyst warn warn_hyst
  70. */
  71. static const u16 W83795_REG_TEMP[][5] = {
  72. {0x21, 0x96, 0x97, 0x98, 0x99}, /* TD1/TR1 */
  73. {0x22, 0x9a, 0x9b, 0x9c, 0x9d}, /* TD2/TR2 */
  74. {0x23, 0x9e, 0x9f, 0xa0, 0xa1}, /* TD3/TR3 */
  75. {0x24, 0xa2, 0xa3, 0xa4, 0xa5}, /* TD4/TR4 */
  76. {0x1f, 0xa6, 0xa7, 0xa8, 0xa9}, /* TR5 */
  77. {0x20, 0xaa, 0xab, 0xac, 0xad}, /* TR6 */
  78. };
  79. #define IN_READ 0
  80. #define IN_MAX 1
  81. #define IN_LOW 2
  82. static const u16 W83795_REG_IN[][3] = {
  83. /* Current, HL, LL */
  84. {0x10, 0x70, 0x71}, /* VSEN1 */
  85. {0x11, 0x72, 0x73}, /* VSEN2 */
  86. {0x12, 0x74, 0x75}, /* VSEN3 */
  87. {0x13, 0x76, 0x77}, /* VSEN4 */
  88. {0x14, 0x78, 0x79}, /* VSEN5 */
  89. {0x15, 0x7a, 0x7b}, /* VSEN6 */
  90. {0x16, 0x7c, 0x7d}, /* VSEN7 */
  91. {0x17, 0x7e, 0x7f}, /* VSEN8 */
  92. {0x18, 0x80, 0x81}, /* VSEN9 */
  93. {0x19, 0x82, 0x83}, /* VSEN10 */
  94. {0x1A, 0x84, 0x85}, /* VSEN11 */
  95. {0x1B, 0x86, 0x87}, /* VTT */
  96. {0x1C, 0x88, 0x89}, /* 3VDD */
  97. {0x1D, 0x8a, 0x8b}, /* 3VSB */
  98. {0x1E, 0x8c, 0x8d}, /* VBAT */
  99. {0x1F, 0xa6, 0xa7}, /* VSEN12 */
  100. {0x20, 0xaa, 0xab}, /* VSEN13 */
  101. {0x21, 0x96, 0x97}, /* VSEN14 */
  102. {0x22, 0x9a, 0x9b}, /* VSEN15 */
  103. {0x23, 0x9e, 0x9f}, /* VSEN16 */
  104. {0x24, 0xa2, 0xa3}, /* VSEN17 */
  105. };
  106. #define W83795_REG_VRLSB 0x3C
  107. static const u8 W83795_REG_IN_HL_LSB[] = {
  108. 0x8e, /* VSEN1-4 */
  109. 0x90, /* VSEN5-8 */
  110. 0x92, /* VSEN9-11 */
  111. 0x94, /* VTT, 3VDD, 3VSB, 3VBAT */
  112. 0xa8, /* VSEN12 */
  113. 0xac, /* VSEN13 */
  114. 0x98, /* VSEN14 */
  115. 0x9c, /* VSEN15 */
  116. 0xa0, /* VSEN16 */
  117. 0xa4, /* VSEN17 */
  118. };
  119. #define IN_LSB_REG(index, type) \
  120. (((type) == 1) ? W83795_REG_IN_HL_LSB[(index)] \
  121. : (W83795_REG_IN_HL_LSB[(index)] + 1))
  122. #define IN_LSB_SHIFT 0
  123. #define IN_LSB_IDX 1
  124. static const u8 IN_LSB_SHIFT_IDX[][2] = {
  125. /* High/Low LSB shift, LSB No. */
  126. {0x00, 0x00}, /* VSEN1 */
  127. {0x02, 0x00}, /* VSEN2 */
  128. {0x04, 0x00}, /* VSEN3 */
  129. {0x06, 0x00}, /* VSEN4 */
  130. {0x00, 0x01}, /* VSEN5 */
  131. {0x02, 0x01}, /* VSEN6 */
  132. {0x04, 0x01}, /* VSEN7 */
  133. {0x06, 0x01}, /* VSEN8 */
  134. {0x00, 0x02}, /* VSEN9 */
  135. {0x02, 0x02}, /* VSEN10 */
  136. {0x04, 0x02}, /* VSEN11 */
  137. {0x00, 0x03}, /* VTT */
  138. {0x02, 0x03}, /* 3VDD */
  139. {0x04, 0x03}, /* 3VSB */
  140. {0x06, 0x03}, /* VBAT */
  141. {0x06, 0x04}, /* VSEN12 */
  142. {0x06, 0x05}, /* VSEN13 */
  143. {0x06, 0x06}, /* VSEN14 */
  144. {0x06, 0x07}, /* VSEN15 */
  145. {0x06, 0x08}, /* VSEN16 */
  146. {0x06, 0x09}, /* VSEN17 */
  147. };
  148. #define W83795_REG_FAN(index) (0x2E + (index))
  149. #define W83795_REG_FAN_MIN_HL(index) (0xB6 + (index))
  150. #define W83795_REG_FAN_MIN_LSB(index) (0xC4 + (index) / 2)
  151. #define W83795_REG_FAN_MIN_LSB_SHIFT(index) \
  152. (((index) & 1) ? 4 : 0)
  153. #define W83795_REG_VID_CTRL 0x6A
  154. #define W83795_REG_ALARM_CTRL 0x40
  155. #define ALARM_CTRL_RTSACS (1 << 7)
  156. #define W83795_REG_ALARM(index) (0x41 + (index))
  157. #define W83795_REG_CLR_CHASSIS 0x4D
  158. #define W83795_REG_BEEP(index) (0x50 + (index))
  159. #define W83795_REG_OVT_CFG 0x58
  160. #define OVT_CFG_SEL (1 << 7)
  161. #define W83795_REG_FCMS1 0x201
  162. #define W83795_REG_FCMS2 0x208
  163. #define W83795_REG_TFMR(index) (0x202 + (index))
  164. #define W83795_REG_FOMC 0x20F
  165. #define W83795_REG_TSS(index) (0x209 + (index))
  166. #define TSS_MAP_RESERVED 0xff
  167. static const u8 tss_map[4][6] = {
  168. { 0, 1, 2, 3, 4, 5},
  169. { 6, 7, 8, 9, 0, 1},
  170. {10, 11, 12, 13, 2, 3},
  171. { 4, 5, 4, 5, TSS_MAP_RESERVED, TSS_MAP_RESERVED},
  172. };
  173. #define PWM_OUTPUT 0
  174. #define PWM_FREQ 1
  175. #define PWM_START 2
  176. #define PWM_NONSTOP 3
  177. #define PWM_STOP_TIME 4
  178. #define W83795_REG_PWM(index, nr) (0x210 + (nr) * 8 + (index))
  179. #define W83795_REG_FTSH(index) (0x240 + (index) * 2)
  180. #define W83795_REG_FTSL(index) (0x241 + (index) * 2)
  181. #define W83795_REG_TFTS 0x250
  182. #define TEMP_PWM_TTTI 0
  183. #define TEMP_PWM_CTFS 1
  184. #define TEMP_PWM_HCT 2
  185. #define TEMP_PWM_HOT 3
  186. #define W83795_REG_TTTI(index) (0x260 + (index))
  187. #define W83795_REG_CTFS(index) (0x268 + (index))
  188. #define W83795_REG_HT(index) (0x270 + (index))
  189. #define SF4_TEMP 0
  190. #define SF4_PWM 1
  191. #define W83795_REG_SF4_TEMP(temp_num, index) \
  192. (0x280 + 0x10 * (temp_num) + (index))
  193. #define W83795_REG_SF4_PWM(temp_num, index) \
  194. (0x288 + 0x10 * (temp_num) + (index))
  195. #define W83795_REG_DTSC 0x301
  196. #define W83795_REG_DTSE 0x302
  197. #define W83795_REG_DTS(index) (0x26 + (index))
  198. #define W83795_REG_PECI_TBASE(index) (0x320 + (index))
  199. #define DTS_CRIT 0
  200. #define DTS_CRIT_HYST 1
  201. #define DTS_WARN 2
  202. #define DTS_WARN_HYST 3
  203. #define W83795_REG_DTS_EXT(index) (0xB2 + (index))
  204. #define SETUP_PWM_DEFAULT 0
  205. #define SETUP_PWM_UPTIME 1
  206. #define SETUP_PWM_DOWNTIME 2
  207. #define W83795_REG_SETUP_PWM(index) (0x20C + (index))
  208. static inline u16 in_from_reg(u8 index, u16 val)
  209. {
  210. /* 3VDD, 3VSB and VBAT: 6 mV/bit; other inputs: 2 mV/bit */
  211. if (index >= 12 && index <= 14)
  212. return val * 6;
  213. else
  214. return val * 2;
  215. }
  216. static inline u16 in_to_reg(u8 index, u16 val)
  217. {
  218. if (index >= 12 && index <= 14)
  219. return val / 6;
  220. else
  221. return val / 2;
  222. }
  223. static inline unsigned long fan_from_reg(u16 val)
  224. {
  225. if ((val == 0xfff) || (val == 0))
  226. return 0;
  227. return 1350000UL / val;
  228. }
  229. static inline u16 fan_to_reg(long rpm)
  230. {
  231. if (rpm <= 0)
  232. return 0x0fff;
  233. return clamp_val((1350000 + (rpm >> 1)) / rpm, 1, 0xffe);
  234. }
  235. static inline unsigned long time_from_reg(u8 reg)
  236. {
  237. return reg * 100;
  238. }
  239. static inline u8 time_to_reg(unsigned long val)
  240. {
  241. return clamp_val((val + 50) / 100, 0, 0xff);
  242. }
  243. static inline long temp_from_reg(s8 reg)
  244. {
  245. return reg * 1000;
  246. }
  247. static inline s8 temp_to_reg(long val, s8 min, s8 max)
  248. {
  249. return clamp_val(val / 1000, min, max);
  250. }
  251. static const u16 pwm_freq_cksel0[16] = {
  252. 1024, 512, 341, 256, 205, 171, 146, 128,
  253. 85, 64, 32, 16, 8, 4, 2, 1
  254. };
  255. static unsigned int pwm_freq_from_reg(u8 reg, u16 clkin)
  256. {
  257. unsigned long base_clock;
  258. if (reg & 0x80) {
  259. base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
  260. return base_clock / ((reg & 0x7f) + 1);
  261. } else
  262. return pwm_freq_cksel0[reg & 0x0f];
  263. }
  264. static u8 pwm_freq_to_reg(unsigned long val, u16 clkin)
  265. {
  266. unsigned long base_clock;
  267. u8 reg0, reg1;
  268. unsigned long best0, best1;
  269. /* Best fit for cksel = 0 */
  270. reg0 = find_closest_descending(val, pwm_freq_cksel0,
  271. ARRAY_SIZE(pwm_freq_cksel0));
  272. if (val < 375) /* cksel = 1 can't beat this */
  273. return reg0;
  274. best0 = pwm_freq_cksel0[reg0];
  275. /* Best fit for cksel = 1 */
  276. base_clock = clkin * 1000 / ((clkin == 48000) ? 384 : 256);
  277. reg1 = clamp_val(DIV_ROUND_CLOSEST(base_clock, val), 1, 128);
  278. best1 = base_clock / reg1;
  279. reg1 = 0x80 | (reg1 - 1);
  280. /* Choose the closest one */
  281. if (abs(val - best0) > abs(val - best1))
  282. return reg1;
  283. else
  284. return reg0;
  285. }
  286. enum chip_types {w83795g, w83795adg};
  287. struct w83795_data {
  288. struct device *hwmon_dev;
  289. struct mutex update_lock;
  290. unsigned long last_updated; /* In jiffies */
  291. enum chip_types chip_type;
  292. u8 bank;
  293. u32 has_in; /* Enable monitor VIN or not */
  294. u8 has_dyn_in; /* Only in2-0 can have this */
  295. u16 in[21][3]; /* Register value, read/high/low */
  296. u8 in_lsb[10][3]; /* LSB Register value, high/low */
  297. u8 has_gain; /* has gain: in17-20 * 8 */
  298. u16 has_fan; /* Enable fan14-1 or not */
  299. u16 fan[14]; /* Register value combine */
  300. u16 fan_min[14]; /* Register value combine */
  301. u8 has_temp; /* Enable monitor temp6-1 or not */
  302. s8 temp[6][5]; /* current, crit, crit_hyst, warn, warn_hyst */
  303. u8 temp_read_vrlsb[6];
  304. u8 temp_mode; /* Bit vector, 0 = TR, 1 = TD */
  305. u8 temp_src[3]; /* Register value */
  306. u8 enable_dts; /*
  307. * Enable PECI and SB-TSI,
  308. * bit 0: =1 enable, =0 disable,
  309. * bit 1: =1 AMD SB-TSI, =0 Intel PECI
  310. */
  311. u8 has_dts; /* Enable monitor DTS temp */
  312. s8 dts[8]; /* Register value */
  313. u8 dts_read_vrlsb[8]; /* Register value */
  314. s8 dts_ext[4]; /* Register value */
  315. u8 has_pwm; /*
  316. * 795g supports 8 pwm, 795adg only supports 2,
  317. * no config register, only affected by chip
  318. * type
  319. */
  320. u8 pwm[8][5]; /*
  321. * Register value, output, freq, start,
  322. * non stop, stop time
  323. */
  324. u16 clkin; /* CLKIN frequency in kHz */
  325. u8 pwm_fcms[2]; /* Register value */
  326. u8 pwm_tfmr[6]; /* Register value */
  327. u8 pwm_fomc; /* Register value */
  328. u16 target_speed[8]; /*
  329. * Register value, target speed for speed
  330. * cruise
  331. */
  332. u8 tol_speed; /* tolerance of target speed */
  333. u8 pwm_temp[6][4]; /* TTTI, CTFS, HCT, HOT */
  334. u8 sf4_reg[6][2][7]; /* 6 temp, temp/dcpwm, 7 registers */
  335. u8 setup_pwm[3]; /* Register value */
  336. u8 alarms[6]; /* Register value */
  337. u8 enable_beep;
  338. u8 beeps[6]; /* Register value */
  339. char valid;
  340. char valid_limits;
  341. char valid_pwm_config;
  342. };
  343. /*
  344. * Hardware access
  345. * We assume that nobdody can change the bank outside the driver.
  346. */
  347. /* Must be called with data->update_lock held, except during initialization */
  348. static int w83795_set_bank(struct i2c_client *client, u8 bank)
  349. {
  350. struct w83795_data *data = i2c_get_clientdata(client);
  351. int err;
  352. /* If the same bank is already set, nothing to do */
  353. if ((data->bank & 0x07) == bank)
  354. return 0;
  355. /* Change to new bank, preserve all other bits */
  356. bank |= data->bank & ~0x07;
  357. err = i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL, bank);
  358. if (err < 0) {
  359. dev_err(&client->dev,
  360. "Failed to set bank to %d, err %d\n",
  361. (int)bank, err);
  362. return err;
  363. }
  364. data->bank = bank;
  365. return 0;
  366. }
  367. /* Must be called with data->update_lock held, except during initialization */
  368. static u8 w83795_read(struct i2c_client *client, u16 reg)
  369. {
  370. int err;
  371. err = w83795_set_bank(client, reg >> 8);
  372. if (err < 0)
  373. return 0x00; /* Arbitrary */
  374. err = i2c_smbus_read_byte_data(client, reg & 0xff);
  375. if (err < 0) {
  376. dev_err(&client->dev,
  377. "Failed to read from register 0x%03x, err %d\n",
  378. (int)reg, err);
  379. return 0x00; /* Arbitrary */
  380. }
  381. return err;
  382. }
  383. /* Must be called with data->update_lock held, except during initialization */
  384. static int w83795_write(struct i2c_client *client, u16 reg, u8 value)
  385. {
  386. int err;
  387. err = w83795_set_bank(client, reg >> 8);
  388. if (err < 0)
  389. return err;
  390. err = i2c_smbus_write_byte_data(client, reg & 0xff, value);
  391. if (err < 0)
  392. dev_err(&client->dev,
  393. "Failed to write to register 0x%03x, err %d\n",
  394. (int)reg, err);
  395. return err;
  396. }
  397. static void w83795_update_limits(struct i2c_client *client)
  398. {
  399. struct w83795_data *data = i2c_get_clientdata(client);
  400. int i, limit;
  401. u8 lsb;
  402. /* Read the voltage limits */
  403. for (i = 0; i < ARRAY_SIZE(data->in); i++) {
  404. if (!(data->has_in & (1 << i)))
  405. continue;
  406. data->in[i][IN_MAX] =
  407. w83795_read(client, W83795_REG_IN[i][IN_MAX]);
  408. data->in[i][IN_LOW] =
  409. w83795_read(client, W83795_REG_IN[i][IN_LOW]);
  410. }
  411. for (i = 0; i < ARRAY_SIZE(data->in_lsb); i++) {
  412. if ((i == 2 && data->chip_type == w83795adg) ||
  413. (i >= 4 && !(data->has_in & (1 << (i + 11)))))
  414. continue;
  415. data->in_lsb[i][IN_MAX] =
  416. w83795_read(client, IN_LSB_REG(i, IN_MAX));
  417. data->in_lsb[i][IN_LOW] =
  418. w83795_read(client, IN_LSB_REG(i, IN_LOW));
  419. }
  420. /* Read the fan limits */
  421. lsb = 0; /* Silent false gcc warning */
  422. for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
  423. /*
  424. * Each register contains LSB for 2 fans, but we want to
  425. * read it only once to save time
  426. */
  427. if ((i & 1) == 0 && (data->has_fan & (3 << i)))
  428. lsb = w83795_read(client, W83795_REG_FAN_MIN_LSB(i));
  429. if (!(data->has_fan & (1 << i)))
  430. continue;
  431. data->fan_min[i] =
  432. w83795_read(client, W83795_REG_FAN_MIN_HL(i)) << 4;
  433. data->fan_min[i] |=
  434. (lsb >> W83795_REG_FAN_MIN_LSB_SHIFT(i)) & 0x0F;
  435. }
  436. /* Read the temperature limits */
  437. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  438. if (!(data->has_temp & (1 << i)))
  439. continue;
  440. for (limit = TEMP_CRIT; limit <= TEMP_WARN_HYST; limit++)
  441. data->temp[i][limit] =
  442. w83795_read(client, W83795_REG_TEMP[i][limit]);
  443. }
  444. /* Read the DTS limits */
  445. if (data->enable_dts) {
  446. for (limit = DTS_CRIT; limit <= DTS_WARN_HYST; limit++)
  447. data->dts_ext[limit] =
  448. w83795_read(client, W83795_REG_DTS_EXT(limit));
  449. }
  450. /* Read beep settings */
  451. if (data->enable_beep) {
  452. for (i = 0; i < ARRAY_SIZE(data->beeps); i++)
  453. data->beeps[i] =
  454. w83795_read(client, W83795_REG_BEEP(i));
  455. }
  456. data->valid_limits = 1;
  457. }
  458. static struct w83795_data *w83795_update_pwm_config(struct device *dev)
  459. {
  460. struct i2c_client *client = to_i2c_client(dev);
  461. struct w83795_data *data = i2c_get_clientdata(client);
  462. int i, tmp;
  463. mutex_lock(&data->update_lock);
  464. if (data->valid_pwm_config)
  465. goto END;
  466. /* Read temperature source selection */
  467. for (i = 0; i < ARRAY_SIZE(data->temp_src); i++)
  468. data->temp_src[i] = w83795_read(client, W83795_REG_TSS(i));
  469. /* Read automatic fan speed control settings */
  470. data->pwm_fcms[0] = w83795_read(client, W83795_REG_FCMS1);
  471. data->pwm_fcms[1] = w83795_read(client, W83795_REG_FCMS2);
  472. for (i = 0; i < ARRAY_SIZE(data->pwm_tfmr); i++)
  473. data->pwm_tfmr[i] = w83795_read(client, W83795_REG_TFMR(i));
  474. data->pwm_fomc = w83795_read(client, W83795_REG_FOMC);
  475. for (i = 0; i < data->has_pwm; i++) {
  476. for (tmp = PWM_FREQ; tmp <= PWM_STOP_TIME; tmp++)
  477. data->pwm[i][tmp] =
  478. w83795_read(client, W83795_REG_PWM(i, tmp));
  479. }
  480. for (i = 0; i < ARRAY_SIZE(data->target_speed); i++) {
  481. data->target_speed[i] =
  482. w83795_read(client, W83795_REG_FTSH(i)) << 4;
  483. data->target_speed[i] |=
  484. w83795_read(client, W83795_REG_FTSL(i)) >> 4;
  485. }
  486. data->tol_speed = w83795_read(client, W83795_REG_TFTS) & 0x3f;
  487. for (i = 0; i < ARRAY_SIZE(data->pwm_temp); i++) {
  488. data->pwm_temp[i][TEMP_PWM_TTTI] =
  489. w83795_read(client, W83795_REG_TTTI(i)) & 0x7f;
  490. data->pwm_temp[i][TEMP_PWM_CTFS] =
  491. w83795_read(client, W83795_REG_CTFS(i));
  492. tmp = w83795_read(client, W83795_REG_HT(i));
  493. data->pwm_temp[i][TEMP_PWM_HCT] = tmp >> 4;
  494. data->pwm_temp[i][TEMP_PWM_HOT] = tmp & 0x0f;
  495. }
  496. /* Read SmartFanIV trip points */
  497. for (i = 0; i < ARRAY_SIZE(data->sf4_reg); i++) {
  498. for (tmp = 0; tmp < 7; tmp++) {
  499. data->sf4_reg[i][SF4_TEMP][tmp] =
  500. w83795_read(client,
  501. W83795_REG_SF4_TEMP(i, tmp));
  502. data->sf4_reg[i][SF4_PWM][tmp] =
  503. w83795_read(client, W83795_REG_SF4_PWM(i, tmp));
  504. }
  505. }
  506. /* Read setup PWM */
  507. for (i = 0; i < ARRAY_SIZE(data->setup_pwm); i++)
  508. data->setup_pwm[i] =
  509. w83795_read(client, W83795_REG_SETUP_PWM(i));
  510. data->valid_pwm_config = 1;
  511. END:
  512. mutex_unlock(&data->update_lock);
  513. return data;
  514. }
  515. static struct w83795_data *w83795_update_device(struct device *dev)
  516. {
  517. struct i2c_client *client = to_i2c_client(dev);
  518. struct w83795_data *data = i2c_get_clientdata(client);
  519. u16 tmp;
  520. u8 intrusion;
  521. int i;
  522. mutex_lock(&data->update_lock);
  523. if (!data->valid_limits)
  524. w83795_update_limits(client);
  525. if (!(time_after(jiffies, data->last_updated + HZ * 2)
  526. || !data->valid))
  527. goto END;
  528. /* Update the voltages value */
  529. for (i = 0; i < ARRAY_SIZE(data->in); i++) {
  530. if (!(data->has_in & (1 << i)))
  531. continue;
  532. tmp = w83795_read(client, W83795_REG_IN[i][IN_READ]) << 2;
  533. tmp |= w83795_read(client, W83795_REG_VRLSB) >> 6;
  534. data->in[i][IN_READ] = tmp;
  535. }
  536. /* in0-2 can have dynamic limits (W83795G only) */
  537. if (data->has_dyn_in) {
  538. u8 lsb_max = w83795_read(client, IN_LSB_REG(0, IN_MAX));
  539. u8 lsb_low = w83795_read(client, IN_LSB_REG(0, IN_LOW));
  540. for (i = 0; i < 3; i++) {
  541. if (!(data->has_dyn_in & (1 << i)))
  542. continue;
  543. data->in[i][IN_MAX] =
  544. w83795_read(client, W83795_REG_IN[i][IN_MAX]);
  545. data->in[i][IN_LOW] =
  546. w83795_read(client, W83795_REG_IN[i][IN_LOW]);
  547. data->in_lsb[i][IN_MAX] = (lsb_max >> (2 * i)) & 0x03;
  548. data->in_lsb[i][IN_LOW] = (lsb_low >> (2 * i)) & 0x03;
  549. }
  550. }
  551. /* Update fan */
  552. for (i = 0; i < ARRAY_SIZE(data->fan); i++) {
  553. if (!(data->has_fan & (1 << i)))
  554. continue;
  555. data->fan[i] = w83795_read(client, W83795_REG_FAN(i)) << 4;
  556. data->fan[i] |= w83795_read(client, W83795_REG_VRLSB) >> 4;
  557. }
  558. /* Update temperature */
  559. for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
  560. data->temp[i][TEMP_READ] =
  561. w83795_read(client, W83795_REG_TEMP[i][TEMP_READ]);
  562. data->temp_read_vrlsb[i] =
  563. w83795_read(client, W83795_REG_VRLSB);
  564. }
  565. /* Update dts temperature */
  566. if (data->enable_dts) {
  567. for (i = 0; i < ARRAY_SIZE(data->dts); i++) {
  568. if (!(data->has_dts & (1 << i)))
  569. continue;
  570. data->dts[i] =
  571. w83795_read(client, W83795_REG_DTS(i));
  572. data->dts_read_vrlsb[i] =
  573. w83795_read(client, W83795_REG_VRLSB);
  574. }
  575. }
  576. /* Update pwm output */
  577. for (i = 0; i < data->has_pwm; i++) {
  578. data->pwm[i][PWM_OUTPUT] =
  579. w83795_read(client, W83795_REG_PWM(i, PWM_OUTPUT));
  580. }
  581. /*
  582. * Update intrusion and alarms
  583. * It is important to read intrusion first, because reading from
  584. * register SMI STS6 clears the interrupt status temporarily.
  585. */
  586. tmp = w83795_read(client, W83795_REG_ALARM_CTRL);
  587. /* Switch to interrupt status for intrusion if needed */
  588. if (tmp & ALARM_CTRL_RTSACS)
  589. w83795_write(client, W83795_REG_ALARM_CTRL,
  590. tmp & ~ALARM_CTRL_RTSACS);
  591. intrusion = w83795_read(client, W83795_REG_ALARM(5)) & (1 << 6);
  592. /* Switch to real-time alarms */
  593. w83795_write(client, W83795_REG_ALARM_CTRL, tmp | ALARM_CTRL_RTSACS);
  594. for (i = 0; i < ARRAY_SIZE(data->alarms); i++)
  595. data->alarms[i] = w83795_read(client, W83795_REG_ALARM(i));
  596. data->alarms[5] |= intrusion;
  597. /* Restore original configuration if needed */
  598. if (!(tmp & ALARM_CTRL_RTSACS))
  599. w83795_write(client, W83795_REG_ALARM_CTRL,
  600. tmp & ~ALARM_CTRL_RTSACS);
  601. data->last_updated = jiffies;
  602. data->valid = 1;
  603. END:
  604. mutex_unlock(&data->update_lock);
  605. return data;
  606. }
  607. /*
  608. * Sysfs attributes
  609. */
  610. #define ALARM_STATUS 0
  611. #define BEEP_ENABLE 1
  612. static ssize_t
  613. show_alarm_beep(struct device *dev, struct device_attribute *attr, char *buf)
  614. {
  615. struct w83795_data *data = w83795_update_device(dev);
  616. struct sensor_device_attribute_2 *sensor_attr =
  617. to_sensor_dev_attr_2(attr);
  618. int nr = sensor_attr->nr;
  619. int index = sensor_attr->index >> 3;
  620. int bit = sensor_attr->index & 0x07;
  621. u8 val;
  622. if (nr == ALARM_STATUS)
  623. val = (data->alarms[index] >> bit) & 1;
  624. else /* BEEP_ENABLE */
  625. val = (data->beeps[index] >> bit) & 1;
  626. return sprintf(buf, "%u\n", val);
  627. }
  628. static ssize_t
  629. store_beep(struct device *dev, struct device_attribute *attr,
  630. const char *buf, size_t count)
  631. {
  632. struct i2c_client *client = to_i2c_client(dev);
  633. struct w83795_data *data = i2c_get_clientdata(client);
  634. struct sensor_device_attribute_2 *sensor_attr =
  635. to_sensor_dev_attr_2(attr);
  636. int index = sensor_attr->index >> 3;
  637. int shift = sensor_attr->index & 0x07;
  638. u8 beep_bit = 1 << shift;
  639. unsigned long val;
  640. if (kstrtoul(buf, 10, &val) < 0)
  641. return -EINVAL;
  642. if (val != 0 && val != 1)
  643. return -EINVAL;
  644. mutex_lock(&data->update_lock);
  645. data->beeps[index] = w83795_read(client, W83795_REG_BEEP(index));
  646. data->beeps[index] &= ~beep_bit;
  647. data->beeps[index] |= val << shift;
  648. w83795_write(client, W83795_REG_BEEP(index), data->beeps[index]);
  649. mutex_unlock(&data->update_lock);
  650. return count;
  651. }
  652. /* Write 0 to clear chassis alarm */
  653. static ssize_t
  654. store_chassis_clear(struct device *dev,
  655. struct device_attribute *attr, const char *buf,
  656. size_t count)
  657. {
  658. struct i2c_client *client = to_i2c_client(dev);
  659. struct w83795_data *data = i2c_get_clientdata(client);
  660. unsigned long val;
  661. if (kstrtoul(buf, 10, &val) < 0 || val != 0)
  662. return -EINVAL;
  663. mutex_lock(&data->update_lock);
  664. val = w83795_read(client, W83795_REG_CLR_CHASSIS);
  665. val |= 0x80;
  666. w83795_write(client, W83795_REG_CLR_CHASSIS, val);
  667. /* Clear status and force cache refresh */
  668. w83795_read(client, W83795_REG_ALARM(5));
  669. data->valid = 0;
  670. mutex_unlock(&data->update_lock);
  671. return count;
  672. }
  673. #define FAN_INPUT 0
  674. #define FAN_MIN 1
  675. static ssize_t
  676. show_fan(struct device *dev, struct device_attribute *attr, char *buf)
  677. {
  678. struct sensor_device_attribute_2 *sensor_attr =
  679. to_sensor_dev_attr_2(attr);
  680. int nr = sensor_attr->nr;
  681. int index = sensor_attr->index;
  682. struct w83795_data *data = w83795_update_device(dev);
  683. u16 val;
  684. if (nr == FAN_INPUT)
  685. val = data->fan[index] & 0x0fff;
  686. else
  687. val = data->fan_min[index] & 0x0fff;
  688. return sprintf(buf, "%lu\n", fan_from_reg(val));
  689. }
  690. static ssize_t
  691. store_fan_min(struct device *dev, struct device_attribute *attr,
  692. const char *buf, size_t count)
  693. {
  694. struct sensor_device_attribute_2 *sensor_attr =
  695. to_sensor_dev_attr_2(attr);
  696. int index = sensor_attr->index;
  697. struct i2c_client *client = to_i2c_client(dev);
  698. struct w83795_data *data = i2c_get_clientdata(client);
  699. unsigned long val;
  700. if (kstrtoul(buf, 10, &val))
  701. return -EINVAL;
  702. val = fan_to_reg(val);
  703. mutex_lock(&data->update_lock);
  704. data->fan_min[index] = val;
  705. w83795_write(client, W83795_REG_FAN_MIN_HL(index), (val >> 4) & 0xff);
  706. val &= 0x0f;
  707. if (index & 1) {
  708. val <<= 4;
  709. val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
  710. & 0x0f;
  711. } else {
  712. val |= w83795_read(client, W83795_REG_FAN_MIN_LSB(index))
  713. & 0xf0;
  714. }
  715. w83795_write(client, W83795_REG_FAN_MIN_LSB(index), val & 0xff);
  716. mutex_unlock(&data->update_lock);
  717. return count;
  718. }
  719. static ssize_t
  720. show_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  721. {
  722. struct w83795_data *data;
  723. struct sensor_device_attribute_2 *sensor_attr =
  724. to_sensor_dev_attr_2(attr);
  725. int nr = sensor_attr->nr;
  726. int index = sensor_attr->index;
  727. unsigned int val;
  728. data = nr == PWM_OUTPUT ? w83795_update_device(dev)
  729. : w83795_update_pwm_config(dev);
  730. switch (nr) {
  731. case PWM_STOP_TIME:
  732. val = time_from_reg(data->pwm[index][nr]);
  733. break;
  734. case PWM_FREQ:
  735. val = pwm_freq_from_reg(data->pwm[index][nr], data->clkin);
  736. break;
  737. default:
  738. val = data->pwm[index][nr];
  739. break;
  740. }
  741. return sprintf(buf, "%u\n", val);
  742. }
  743. static ssize_t
  744. store_pwm(struct device *dev, struct device_attribute *attr,
  745. const char *buf, size_t count)
  746. {
  747. struct i2c_client *client = to_i2c_client(dev);
  748. struct w83795_data *data = i2c_get_clientdata(client);
  749. struct sensor_device_attribute_2 *sensor_attr =
  750. to_sensor_dev_attr_2(attr);
  751. int nr = sensor_attr->nr;
  752. int index = sensor_attr->index;
  753. unsigned long val;
  754. if (kstrtoul(buf, 10, &val) < 0)
  755. return -EINVAL;
  756. mutex_lock(&data->update_lock);
  757. switch (nr) {
  758. case PWM_STOP_TIME:
  759. val = time_to_reg(val);
  760. break;
  761. case PWM_FREQ:
  762. val = pwm_freq_to_reg(val, data->clkin);
  763. break;
  764. default:
  765. val = clamp_val(val, 0, 0xff);
  766. break;
  767. }
  768. w83795_write(client, W83795_REG_PWM(index, nr), val);
  769. data->pwm[index][nr] = val;
  770. mutex_unlock(&data->update_lock);
  771. return count;
  772. }
  773. static ssize_t
  774. show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
  775. {
  776. struct sensor_device_attribute_2 *sensor_attr =
  777. to_sensor_dev_attr_2(attr);
  778. struct w83795_data *data = w83795_update_pwm_config(dev);
  779. int index = sensor_attr->index;
  780. u8 tmp;
  781. /* Speed cruise mode */
  782. if (data->pwm_fcms[0] & (1 << index)) {
  783. tmp = 2;
  784. goto out;
  785. }
  786. /* Thermal cruise or SmartFan IV mode */
  787. for (tmp = 0; tmp < 6; tmp++) {
  788. if (data->pwm_tfmr[tmp] & (1 << index)) {
  789. tmp = 3;
  790. goto out;
  791. }
  792. }
  793. /* Manual mode */
  794. tmp = 1;
  795. out:
  796. return sprintf(buf, "%u\n", tmp);
  797. }
  798. static ssize_t
  799. store_pwm_enable(struct device *dev, struct device_attribute *attr,
  800. const char *buf, size_t count)
  801. {
  802. struct i2c_client *client = to_i2c_client(dev);
  803. struct w83795_data *data = w83795_update_pwm_config(dev);
  804. struct sensor_device_attribute_2 *sensor_attr =
  805. to_sensor_dev_attr_2(attr);
  806. int index = sensor_attr->index;
  807. unsigned long val;
  808. int i;
  809. if (kstrtoul(buf, 10, &val) < 0)
  810. return -EINVAL;
  811. if (val < 1 || val > 2)
  812. return -EINVAL;
  813. #ifndef CONFIG_SENSORS_W83795_FANCTRL
  814. if (val > 1) {
  815. dev_warn(dev, "Automatic fan speed control support disabled\n");
  816. dev_warn(dev, "Build with CONFIG_SENSORS_W83795_FANCTRL=y if you want it\n");
  817. return -EOPNOTSUPP;
  818. }
  819. #endif
  820. mutex_lock(&data->update_lock);
  821. switch (val) {
  822. case 1:
  823. /* Clear speed cruise mode bits */
  824. data->pwm_fcms[0] &= ~(1 << index);
  825. w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
  826. /* Clear thermal cruise mode bits */
  827. for (i = 0; i < 6; i++) {
  828. data->pwm_tfmr[i] &= ~(1 << index);
  829. w83795_write(client, W83795_REG_TFMR(i),
  830. data->pwm_tfmr[i]);
  831. }
  832. break;
  833. case 2:
  834. data->pwm_fcms[0] |= (1 << index);
  835. w83795_write(client, W83795_REG_FCMS1, data->pwm_fcms[0]);
  836. break;
  837. }
  838. mutex_unlock(&data->update_lock);
  839. return count;
  840. }
  841. static ssize_t
  842. show_pwm_mode(struct device *dev, struct device_attribute *attr, char *buf)
  843. {
  844. struct w83795_data *data = w83795_update_pwm_config(dev);
  845. int index = to_sensor_dev_attr_2(attr)->index;
  846. unsigned int mode;
  847. if (data->pwm_fomc & (1 << index))
  848. mode = 0; /* DC */
  849. else
  850. mode = 1; /* PWM */
  851. return sprintf(buf, "%u\n", mode);
  852. }
  853. /*
  854. * Check whether a given temperature source can ever be useful.
  855. * Returns the number of selectable temperature channels which are
  856. * enabled.
  857. */
  858. static int w83795_tss_useful(const struct w83795_data *data, int tsrc)
  859. {
  860. int useful = 0, i;
  861. for (i = 0; i < 4; i++) {
  862. if (tss_map[i][tsrc] == TSS_MAP_RESERVED)
  863. continue;
  864. if (tss_map[i][tsrc] < 6) /* Analog */
  865. useful += (data->has_temp >> tss_map[i][tsrc]) & 1;
  866. else /* Digital */
  867. useful += (data->has_dts >> (tss_map[i][tsrc] - 6)) & 1;
  868. }
  869. return useful;
  870. }
  871. static ssize_t
  872. show_temp_src(struct device *dev, struct device_attribute *attr, char *buf)
  873. {
  874. struct sensor_device_attribute_2 *sensor_attr =
  875. to_sensor_dev_attr_2(attr);
  876. struct w83795_data *data = w83795_update_pwm_config(dev);
  877. int index = sensor_attr->index;
  878. u8 tmp = data->temp_src[index / 2];
  879. if (index & 1)
  880. tmp >>= 4; /* Pick high nibble */
  881. else
  882. tmp &= 0x0f; /* Pick low nibble */
  883. /* Look-up the actual temperature channel number */
  884. if (tmp >= 4 || tss_map[tmp][index] == TSS_MAP_RESERVED)
  885. return -EINVAL; /* Shouldn't happen */
  886. return sprintf(buf, "%u\n", (unsigned int)tss_map[tmp][index] + 1);
  887. }
  888. static ssize_t
  889. store_temp_src(struct device *dev, struct device_attribute *attr,
  890. const char *buf, size_t count)
  891. {
  892. struct i2c_client *client = to_i2c_client(dev);
  893. struct w83795_data *data = w83795_update_pwm_config(dev);
  894. struct sensor_device_attribute_2 *sensor_attr =
  895. to_sensor_dev_attr_2(attr);
  896. int index = sensor_attr->index;
  897. int tmp;
  898. unsigned long channel;
  899. u8 val = index / 2;
  900. if (kstrtoul(buf, 10, &channel) < 0 ||
  901. channel < 1 || channel > 14)
  902. return -EINVAL;
  903. /* Check if request can be fulfilled */
  904. for (tmp = 0; tmp < 4; tmp++) {
  905. if (tss_map[tmp][index] == channel - 1)
  906. break;
  907. }
  908. if (tmp == 4) /* No match */
  909. return -EINVAL;
  910. mutex_lock(&data->update_lock);
  911. if (index & 1) {
  912. tmp <<= 4;
  913. data->temp_src[val] &= 0x0f;
  914. } else {
  915. data->temp_src[val] &= 0xf0;
  916. }
  917. data->temp_src[val] |= tmp;
  918. w83795_write(client, W83795_REG_TSS(val), data->temp_src[val]);
  919. mutex_unlock(&data->update_lock);
  920. return count;
  921. }
  922. #define TEMP_PWM_ENABLE 0
  923. #define TEMP_PWM_FAN_MAP 1
  924. static ssize_t
  925. show_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
  926. char *buf)
  927. {
  928. struct w83795_data *data = w83795_update_pwm_config(dev);
  929. struct sensor_device_attribute_2 *sensor_attr =
  930. to_sensor_dev_attr_2(attr);
  931. int nr = sensor_attr->nr;
  932. int index = sensor_attr->index;
  933. u8 tmp = 0xff;
  934. switch (nr) {
  935. case TEMP_PWM_ENABLE:
  936. tmp = (data->pwm_fcms[1] >> index) & 1;
  937. if (tmp)
  938. tmp = 4;
  939. else
  940. tmp = 3;
  941. break;
  942. case TEMP_PWM_FAN_MAP:
  943. tmp = data->pwm_tfmr[index];
  944. break;
  945. }
  946. return sprintf(buf, "%u\n", tmp);
  947. }
  948. static ssize_t
  949. store_temp_pwm_enable(struct device *dev, struct device_attribute *attr,
  950. const char *buf, size_t count)
  951. {
  952. struct i2c_client *client = to_i2c_client(dev);
  953. struct w83795_data *data = w83795_update_pwm_config(dev);
  954. struct sensor_device_attribute_2 *sensor_attr =
  955. to_sensor_dev_attr_2(attr);
  956. int nr = sensor_attr->nr;
  957. int index = sensor_attr->index;
  958. unsigned long tmp;
  959. if (kstrtoul(buf, 10, &tmp) < 0)
  960. return -EINVAL;
  961. switch (nr) {
  962. case TEMP_PWM_ENABLE:
  963. if (tmp != 3 && tmp != 4)
  964. return -EINVAL;
  965. tmp -= 3;
  966. mutex_lock(&data->update_lock);
  967. data->pwm_fcms[1] &= ~(1 << index);
  968. data->pwm_fcms[1] |= tmp << index;
  969. w83795_write(client, W83795_REG_FCMS2, data->pwm_fcms[1]);
  970. mutex_unlock(&data->update_lock);
  971. break;
  972. case TEMP_PWM_FAN_MAP:
  973. mutex_lock(&data->update_lock);
  974. tmp = clamp_val(tmp, 0, 0xff);
  975. w83795_write(client, W83795_REG_TFMR(index), tmp);
  976. data->pwm_tfmr[index] = tmp;
  977. mutex_unlock(&data->update_lock);
  978. break;
  979. }
  980. return count;
  981. }
  982. #define FANIN_TARGET 0
  983. #define FANIN_TOL 1
  984. static ssize_t
  985. show_fanin(struct device *dev, struct device_attribute *attr, char *buf)
  986. {
  987. struct w83795_data *data = w83795_update_pwm_config(dev);
  988. struct sensor_device_attribute_2 *sensor_attr =
  989. to_sensor_dev_attr_2(attr);
  990. int nr = sensor_attr->nr;
  991. int index = sensor_attr->index;
  992. u16 tmp = 0;
  993. switch (nr) {
  994. case FANIN_TARGET:
  995. tmp = fan_from_reg(data->target_speed[index]);
  996. break;
  997. case FANIN_TOL:
  998. tmp = data->tol_speed;
  999. break;
  1000. }
  1001. return sprintf(buf, "%u\n", tmp);
  1002. }
  1003. static ssize_t
  1004. store_fanin(struct device *dev, struct device_attribute *attr,
  1005. const char *buf, size_t count)
  1006. {
  1007. struct i2c_client *client = to_i2c_client(dev);
  1008. struct w83795_data *data = i2c_get_clientdata(client);
  1009. struct sensor_device_attribute_2 *sensor_attr =
  1010. to_sensor_dev_attr_2(attr);
  1011. int nr = sensor_attr->nr;
  1012. int index = sensor_attr->index;
  1013. unsigned long val;
  1014. if (kstrtoul(buf, 10, &val) < 0)
  1015. return -EINVAL;
  1016. mutex_lock(&data->update_lock);
  1017. switch (nr) {
  1018. case FANIN_TARGET:
  1019. val = fan_to_reg(clamp_val(val, 0, 0xfff));
  1020. w83795_write(client, W83795_REG_FTSH(index), val >> 4);
  1021. w83795_write(client, W83795_REG_FTSL(index), (val << 4) & 0xf0);
  1022. data->target_speed[index] = val;
  1023. break;
  1024. case FANIN_TOL:
  1025. val = clamp_val(val, 0, 0x3f);
  1026. w83795_write(client, W83795_REG_TFTS, val);
  1027. data->tol_speed = val;
  1028. break;
  1029. }
  1030. mutex_unlock(&data->update_lock);
  1031. return count;
  1032. }
  1033. static ssize_t
  1034. show_temp_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  1035. {
  1036. struct w83795_data *data = w83795_update_pwm_config(dev);
  1037. struct sensor_device_attribute_2 *sensor_attr =
  1038. to_sensor_dev_attr_2(attr);
  1039. int nr = sensor_attr->nr;
  1040. int index = sensor_attr->index;
  1041. long tmp = temp_from_reg(data->pwm_temp[index][nr]);
  1042. return sprintf(buf, "%ld\n", tmp);
  1043. }
  1044. static ssize_t
  1045. store_temp_pwm(struct device *dev, struct device_attribute *attr,
  1046. const char *buf, size_t count)
  1047. {
  1048. struct i2c_client *client = to_i2c_client(dev);
  1049. struct w83795_data *data = i2c_get_clientdata(client);
  1050. struct sensor_device_attribute_2 *sensor_attr =
  1051. to_sensor_dev_attr_2(attr);
  1052. int nr = sensor_attr->nr;
  1053. int index = sensor_attr->index;
  1054. unsigned long val;
  1055. u8 tmp;
  1056. if (kstrtoul(buf, 10, &val) < 0)
  1057. return -EINVAL;
  1058. val /= 1000;
  1059. mutex_lock(&data->update_lock);
  1060. switch (nr) {
  1061. case TEMP_PWM_TTTI:
  1062. val = clamp_val(val, 0, 0x7f);
  1063. w83795_write(client, W83795_REG_TTTI(index), val);
  1064. break;
  1065. case TEMP_PWM_CTFS:
  1066. val = clamp_val(val, 0, 0x7f);
  1067. w83795_write(client, W83795_REG_CTFS(index), val);
  1068. break;
  1069. case TEMP_PWM_HCT:
  1070. val = clamp_val(val, 0, 0x0f);
  1071. tmp = w83795_read(client, W83795_REG_HT(index));
  1072. tmp &= 0x0f;
  1073. tmp |= (val << 4) & 0xf0;
  1074. w83795_write(client, W83795_REG_HT(index), tmp);
  1075. break;
  1076. case TEMP_PWM_HOT:
  1077. val = clamp_val(val, 0, 0x0f);
  1078. tmp = w83795_read(client, W83795_REG_HT(index));
  1079. tmp &= 0xf0;
  1080. tmp |= val & 0x0f;
  1081. w83795_write(client, W83795_REG_HT(index), tmp);
  1082. break;
  1083. }
  1084. data->pwm_temp[index][nr] = val;
  1085. mutex_unlock(&data->update_lock);
  1086. return count;
  1087. }
  1088. static ssize_t
  1089. show_sf4_pwm(struct device *dev, struct device_attribute *attr, char *buf)
  1090. {
  1091. struct w83795_data *data = w83795_update_pwm_config(dev);
  1092. struct sensor_device_attribute_2 *sensor_attr =
  1093. to_sensor_dev_attr_2(attr);
  1094. int nr = sensor_attr->nr;
  1095. int index = sensor_attr->index;
  1096. return sprintf(buf, "%u\n", data->sf4_reg[index][SF4_PWM][nr]);
  1097. }
  1098. static ssize_t
  1099. store_sf4_pwm(struct device *dev, struct device_attribute *attr,
  1100. const char *buf, size_t count)
  1101. {
  1102. struct i2c_client *client = to_i2c_client(dev);
  1103. struct w83795_data *data = i2c_get_clientdata(client);
  1104. struct sensor_device_attribute_2 *sensor_attr =
  1105. to_sensor_dev_attr_2(attr);
  1106. int nr = sensor_attr->nr;
  1107. int index = sensor_attr->index;
  1108. unsigned long val;
  1109. if (kstrtoul(buf, 10, &val) < 0)
  1110. return -EINVAL;
  1111. mutex_lock(&data->update_lock);
  1112. w83795_write(client, W83795_REG_SF4_PWM(index, nr), val);
  1113. data->sf4_reg[index][SF4_PWM][nr] = val;
  1114. mutex_unlock(&data->update_lock);
  1115. return count;
  1116. }
  1117. static ssize_t
  1118. show_sf4_temp(struct device *dev, struct device_attribute *attr, char *buf)
  1119. {
  1120. struct w83795_data *data = w83795_update_pwm_config(dev);
  1121. struct sensor_device_attribute_2 *sensor_attr =
  1122. to_sensor_dev_attr_2(attr);
  1123. int nr = sensor_attr->nr;
  1124. int index = sensor_attr->index;
  1125. return sprintf(buf, "%u\n",
  1126. (data->sf4_reg[index][SF4_TEMP][nr]) * 1000);
  1127. }
  1128. static ssize_t
  1129. store_sf4_temp(struct device *dev, struct device_attribute *attr,
  1130. const char *buf, size_t count)
  1131. {
  1132. struct i2c_client *client = to_i2c_client(dev);
  1133. struct w83795_data *data = i2c_get_clientdata(client);
  1134. struct sensor_device_attribute_2 *sensor_attr =
  1135. to_sensor_dev_attr_2(attr);
  1136. int nr = sensor_attr->nr;
  1137. int index = sensor_attr->index;
  1138. unsigned long val;
  1139. if (kstrtoul(buf, 10, &val) < 0)
  1140. return -EINVAL;
  1141. val /= 1000;
  1142. mutex_lock(&data->update_lock);
  1143. w83795_write(client, W83795_REG_SF4_TEMP(index, nr), val);
  1144. data->sf4_reg[index][SF4_TEMP][nr] = val;
  1145. mutex_unlock(&data->update_lock);
  1146. return count;
  1147. }
  1148. static ssize_t
  1149. show_temp(struct device *dev, struct device_attribute *attr, char *buf)
  1150. {
  1151. struct sensor_device_attribute_2 *sensor_attr =
  1152. to_sensor_dev_attr_2(attr);
  1153. int nr = sensor_attr->nr;
  1154. int index = sensor_attr->index;
  1155. struct w83795_data *data = w83795_update_device(dev);
  1156. long temp = temp_from_reg(data->temp[index][nr]);
  1157. if (nr == TEMP_READ)
  1158. temp += (data->temp_read_vrlsb[index] >> 6) * 250;
  1159. return sprintf(buf, "%ld\n", temp);
  1160. }
  1161. static ssize_t
  1162. store_temp(struct device *dev, struct device_attribute *attr,
  1163. const char *buf, size_t count)
  1164. {
  1165. struct sensor_device_attribute_2 *sensor_attr =
  1166. to_sensor_dev_attr_2(attr);
  1167. int nr = sensor_attr->nr;
  1168. int index = sensor_attr->index;
  1169. struct i2c_client *client = to_i2c_client(dev);
  1170. struct w83795_data *data = i2c_get_clientdata(client);
  1171. long tmp;
  1172. if (kstrtol(buf, 10, &tmp) < 0)
  1173. return -EINVAL;
  1174. mutex_lock(&data->update_lock);
  1175. data->temp[index][nr] = temp_to_reg(tmp, -128, 127);
  1176. w83795_write(client, W83795_REG_TEMP[index][nr], data->temp[index][nr]);
  1177. mutex_unlock(&data->update_lock);
  1178. return count;
  1179. }
  1180. static ssize_t
  1181. show_dts_mode(struct device *dev, struct device_attribute *attr, char *buf)
  1182. {
  1183. struct w83795_data *data = dev_get_drvdata(dev);
  1184. int tmp;
  1185. if (data->enable_dts & 2)
  1186. tmp = 5;
  1187. else
  1188. tmp = 6;
  1189. return sprintf(buf, "%d\n", tmp);
  1190. }
  1191. static ssize_t
  1192. show_dts(struct device *dev, struct device_attribute *attr, char *buf)
  1193. {
  1194. struct sensor_device_attribute_2 *sensor_attr =
  1195. to_sensor_dev_attr_2(attr);
  1196. int index = sensor_attr->index;
  1197. struct w83795_data *data = w83795_update_device(dev);
  1198. long temp = temp_from_reg(data->dts[index]);
  1199. temp += (data->dts_read_vrlsb[index] >> 6) * 250;
  1200. return sprintf(buf, "%ld\n", temp);
  1201. }
  1202. static ssize_t
  1203. show_dts_ext(struct device *dev, struct device_attribute *attr, char *buf)
  1204. {
  1205. struct sensor_device_attribute_2 *sensor_attr =
  1206. to_sensor_dev_attr_2(attr);
  1207. int nr = sensor_attr->nr;
  1208. struct w83795_data *data = dev_get_drvdata(dev);
  1209. long temp = temp_from_reg(data->dts_ext[nr]);
  1210. return sprintf(buf, "%ld\n", temp);
  1211. }
  1212. static ssize_t
  1213. store_dts_ext(struct device *dev, struct device_attribute *attr,
  1214. const char *buf, size_t count)
  1215. {
  1216. struct sensor_device_attribute_2 *sensor_attr =
  1217. to_sensor_dev_attr_2(attr);
  1218. int nr = sensor_attr->nr;
  1219. struct i2c_client *client = to_i2c_client(dev);
  1220. struct w83795_data *data = i2c_get_clientdata(client);
  1221. long tmp;
  1222. if (kstrtol(buf, 10, &tmp) < 0)
  1223. return -EINVAL;
  1224. mutex_lock(&data->update_lock);
  1225. data->dts_ext[nr] = temp_to_reg(tmp, -128, 127);
  1226. w83795_write(client, W83795_REG_DTS_EXT(nr), data->dts_ext[nr]);
  1227. mutex_unlock(&data->update_lock);
  1228. return count;
  1229. }
  1230. static ssize_t
  1231. show_temp_mode(struct device *dev, struct device_attribute *attr, char *buf)
  1232. {
  1233. struct w83795_data *data = dev_get_drvdata(dev);
  1234. struct sensor_device_attribute_2 *sensor_attr =
  1235. to_sensor_dev_attr_2(attr);
  1236. int index = sensor_attr->index;
  1237. int tmp;
  1238. if (data->temp_mode & (1 << index))
  1239. tmp = 3; /* Thermal diode */
  1240. else
  1241. tmp = 4; /* Thermistor */
  1242. return sprintf(buf, "%d\n", tmp);
  1243. }
  1244. /* Only for temp1-4 (temp5-6 can only be thermistor) */
  1245. static ssize_t
  1246. store_temp_mode(struct device *dev, struct device_attribute *attr,
  1247. const char *buf, size_t count)
  1248. {
  1249. struct i2c_client *client = to_i2c_client(dev);
  1250. struct w83795_data *data = i2c_get_clientdata(client);
  1251. struct sensor_device_attribute_2 *sensor_attr =
  1252. to_sensor_dev_attr_2(attr);
  1253. int index = sensor_attr->index;
  1254. int reg_shift;
  1255. unsigned long val;
  1256. u8 tmp;
  1257. if (kstrtoul(buf, 10, &val) < 0)
  1258. return -EINVAL;
  1259. if ((val != 4) && (val != 3))
  1260. return -EINVAL;
  1261. mutex_lock(&data->update_lock);
  1262. if (val == 3) {
  1263. /* Thermal diode */
  1264. val = 0x01;
  1265. data->temp_mode |= 1 << index;
  1266. } else if (val == 4) {
  1267. /* Thermistor */
  1268. val = 0x03;
  1269. data->temp_mode &= ~(1 << index);
  1270. }
  1271. reg_shift = 2 * index;
  1272. tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
  1273. tmp &= ~(0x03 << reg_shift);
  1274. tmp |= val << reg_shift;
  1275. w83795_write(client, W83795_REG_TEMP_CTRL2, tmp);
  1276. mutex_unlock(&data->update_lock);
  1277. return count;
  1278. }
  1279. /* show/store VIN */
  1280. static ssize_t
  1281. show_in(struct device *dev, struct device_attribute *attr, char *buf)
  1282. {
  1283. struct sensor_device_attribute_2 *sensor_attr =
  1284. to_sensor_dev_attr_2(attr);
  1285. int nr = sensor_attr->nr;
  1286. int index = sensor_attr->index;
  1287. struct w83795_data *data = w83795_update_device(dev);
  1288. u16 val = data->in[index][nr];
  1289. u8 lsb_idx;
  1290. switch (nr) {
  1291. case IN_READ:
  1292. /* calculate this value again by sensors as sensors3.conf */
  1293. if ((index >= 17) &&
  1294. !((data->has_gain >> (index - 17)) & 1))
  1295. val *= 8;
  1296. break;
  1297. case IN_MAX:
  1298. case IN_LOW:
  1299. lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
  1300. val <<= 2;
  1301. val |= (data->in_lsb[lsb_idx][nr] >>
  1302. IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]) & 0x03;
  1303. if ((index >= 17) &&
  1304. !((data->has_gain >> (index - 17)) & 1))
  1305. val *= 8;
  1306. break;
  1307. }
  1308. val = in_from_reg(index, val);
  1309. return sprintf(buf, "%d\n", val);
  1310. }
  1311. static ssize_t
  1312. store_in(struct device *dev, struct device_attribute *attr,
  1313. const char *buf, size_t count)
  1314. {
  1315. struct sensor_device_attribute_2 *sensor_attr =
  1316. to_sensor_dev_attr_2(attr);
  1317. int nr = sensor_attr->nr;
  1318. int index = sensor_attr->index;
  1319. struct i2c_client *client = to_i2c_client(dev);
  1320. struct w83795_data *data = i2c_get_clientdata(client);
  1321. unsigned long val;
  1322. u8 tmp;
  1323. u8 lsb_idx;
  1324. if (kstrtoul(buf, 10, &val) < 0)
  1325. return -EINVAL;
  1326. val = in_to_reg(index, val);
  1327. if ((index >= 17) &&
  1328. !((data->has_gain >> (index - 17)) & 1))
  1329. val /= 8;
  1330. val = clamp_val(val, 0, 0x3FF);
  1331. mutex_lock(&data->update_lock);
  1332. lsb_idx = IN_LSB_SHIFT_IDX[index][IN_LSB_IDX];
  1333. tmp = w83795_read(client, IN_LSB_REG(lsb_idx, nr));
  1334. tmp &= ~(0x03 << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT]);
  1335. tmp |= (val & 0x03) << IN_LSB_SHIFT_IDX[index][IN_LSB_SHIFT];
  1336. w83795_write(client, IN_LSB_REG(lsb_idx, nr), tmp);
  1337. data->in_lsb[lsb_idx][nr] = tmp;
  1338. tmp = (val >> 2) & 0xff;
  1339. w83795_write(client, W83795_REG_IN[index][nr], tmp);
  1340. data->in[index][nr] = tmp;
  1341. mutex_unlock(&data->update_lock);
  1342. return count;
  1343. }
  1344. #ifdef CONFIG_SENSORS_W83795_FANCTRL
  1345. static ssize_t
  1346. show_sf_setup(struct device *dev, struct device_attribute *attr, char *buf)
  1347. {
  1348. struct sensor_device_attribute_2 *sensor_attr =
  1349. to_sensor_dev_attr_2(attr);
  1350. int nr = sensor_attr->nr;
  1351. struct w83795_data *data = w83795_update_pwm_config(dev);
  1352. u16 val = data->setup_pwm[nr];
  1353. switch (nr) {
  1354. case SETUP_PWM_UPTIME:
  1355. case SETUP_PWM_DOWNTIME:
  1356. val = time_from_reg(val);
  1357. break;
  1358. }
  1359. return sprintf(buf, "%d\n", val);
  1360. }
  1361. static ssize_t
  1362. store_sf_setup(struct device *dev, struct device_attribute *attr,
  1363. const char *buf, size_t count)
  1364. {
  1365. struct sensor_device_attribute_2 *sensor_attr =
  1366. to_sensor_dev_attr_2(attr);
  1367. int nr = sensor_attr->nr;
  1368. struct i2c_client *client = to_i2c_client(dev);
  1369. struct w83795_data *data = i2c_get_clientdata(client);
  1370. unsigned long val;
  1371. if (kstrtoul(buf, 10, &val) < 0)
  1372. return -EINVAL;
  1373. switch (nr) {
  1374. case SETUP_PWM_DEFAULT:
  1375. val = clamp_val(val, 0, 0xff);
  1376. break;
  1377. case SETUP_PWM_UPTIME:
  1378. case SETUP_PWM_DOWNTIME:
  1379. val = time_to_reg(val);
  1380. if (val == 0)
  1381. return -EINVAL;
  1382. break;
  1383. }
  1384. mutex_lock(&data->update_lock);
  1385. data->setup_pwm[nr] = val;
  1386. w83795_write(client, W83795_REG_SETUP_PWM(nr), val);
  1387. mutex_unlock(&data->update_lock);
  1388. return count;
  1389. }
  1390. #endif
  1391. #define NOT_USED -1
  1392. /*
  1393. * Don't change the attribute order, _max, _min and _beep are accessed by index
  1394. * somewhere else in the code
  1395. */
  1396. #define SENSOR_ATTR_IN(index) { \
  1397. SENSOR_ATTR_2(in##index##_input, S_IRUGO, show_in, NULL, \
  1398. IN_READ, index), \
  1399. SENSOR_ATTR_2(in##index##_max, S_IRUGO | S_IWUSR, show_in, \
  1400. store_in, IN_MAX, index), \
  1401. SENSOR_ATTR_2(in##index##_min, S_IRUGO | S_IWUSR, show_in, \
  1402. store_in, IN_LOW, index), \
  1403. SENSOR_ATTR_2(in##index##_alarm, S_IRUGO, show_alarm_beep, \
  1404. NULL, ALARM_STATUS, index + ((index > 14) ? 1 : 0)), \
  1405. SENSOR_ATTR_2(in##index##_beep, S_IWUSR | S_IRUGO, \
  1406. show_alarm_beep, store_beep, BEEP_ENABLE, \
  1407. index + ((index > 14) ? 1 : 0)) }
  1408. /*
  1409. * Don't change the attribute order, _beep is accessed by index
  1410. * somewhere else in the code
  1411. */
  1412. #define SENSOR_ATTR_FAN(index) { \
  1413. SENSOR_ATTR_2(fan##index##_input, S_IRUGO, show_fan, \
  1414. NULL, FAN_INPUT, index - 1), \
  1415. SENSOR_ATTR_2(fan##index##_min, S_IWUSR | S_IRUGO, \
  1416. show_fan, store_fan_min, FAN_MIN, index - 1), \
  1417. SENSOR_ATTR_2(fan##index##_alarm, S_IRUGO, show_alarm_beep, \
  1418. NULL, ALARM_STATUS, index + 31), \
  1419. SENSOR_ATTR_2(fan##index##_beep, S_IWUSR | S_IRUGO, \
  1420. show_alarm_beep, store_beep, BEEP_ENABLE, index + 31) }
  1421. #define SENSOR_ATTR_PWM(index) { \
  1422. SENSOR_ATTR_2(pwm##index, S_IWUSR | S_IRUGO, show_pwm, \
  1423. store_pwm, PWM_OUTPUT, index - 1), \
  1424. SENSOR_ATTR_2(pwm##index##_enable, S_IWUSR | S_IRUGO, \
  1425. show_pwm_enable, store_pwm_enable, NOT_USED, index - 1), \
  1426. SENSOR_ATTR_2(pwm##index##_mode, S_IRUGO, \
  1427. show_pwm_mode, NULL, NOT_USED, index - 1), \
  1428. SENSOR_ATTR_2(pwm##index##_freq, S_IWUSR | S_IRUGO, \
  1429. show_pwm, store_pwm, PWM_FREQ, index - 1), \
  1430. SENSOR_ATTR_2(pwm##index##_nonstop, S_IWUSR | S_IRUGO, \
  1431. show_pwm, store_pwm, PWM_NONSTOP, index - 1), \
  1432. SENSOR_ATTR_2(pwm##index##_start, S_IWUSR | S_IRUGO, \
  1433. show_pwm, store_pwm, PWM_START, index - 1), \
  1434. SENSOR_ATTR_2(pwm##index##_stop_time, S_IWUSR | S_IRUGO, \
  1435. show_pwm, store_pwm, PWM_STOP_TIME, index - 1), \
  1436. SENSOR_ATTR_2(fan##index##_target, S_IWUSR | S_IRUGO, \
  1437. show_fanin, store_fanin, FANIN_TARGET, index - 1) }
  1438. /*
  1439. * Don't change the attribute order, _beep is accessed by index
  1440. * somewhere else in the code
  1441. */
  1442. #define SENSOR_ATTR_DTS(index) { \
  1443. SENSOR_ATTR_2(temp##index##_type, S_IRUGO , \
  1444. show_dts_mode, NULL, NOT_USED, index - 7), \
  1445. SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_dts, \
  1446. NULL, NOT_USED, index - 7), \
  1447. SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_dts_ext, \
  1448. store_dts_ext, DTS_CRIT, NOT_USED), \
  1449. SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
  1450. show_dts_ext, store_dts_ext, DTS_CRIT_HYST, NOT_USED), \
  1451. SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_dts_ext, \
  1452. store_dts_ext, DTS_WARN, NOT_USED), \
  1453. SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
  1454. show_dts_ext, store_dts_ext, DTS_WARN_HYST, NOT_USED), \
  1455. SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
  1456. show_alarm_beep, NULL, ALARM_STATUS, index + 17), \
  1457. SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
  1458. show_alarm_beep, store_beep, BEEP_ENABLE, index + 17) }
  1459. /*
  1460. * Don't change the attribute order, _beep is accessed by index
  1461. * somewhere else in the code
  1462. */
  1463. #define SENSOR_ATTR_TEMP(index) { \
  1464. SENSOR_ATTR_2(temp##index##_type, S_IRUGO | (index < 4 ? S_IWUSR : 0), \
  1465. show_temp_mode, store_temp_mode, NOT_USED, index - 1), \
  1466. SENSOR_ATTR_2(temp##index##_input, S_IRUGO, show_temp, \
  1467. NULL, TEMP_READ, index - 1), \
  1468. SENSOR_ATTR_2(temp##index##_crit, S_IRUGO | S_IWUSR, show_temp, \
  1469. store_temp, TEMP_CRIT, index - 1), \
  1470. SENSOR_ATTR_2(temp##index##_crit_hyst, S_IRUGO | S_IWUSR, \
  1471. show_temp, store_temp, TEMP_CRIT_HYST, index - 1), \
  1472. SENSOR_ATTR_2(temp##index##_max, S_IRUGO | S_IWUSR, show_temp, \
  1473. store_temp, TEMP_WARN, index - 1), \
  1474. SENSOR_ATTR_2(temp##index##_max_hyst, S_IRUGO | S_IWUSR, \
  1475. show_temp, store_temp, TEMP_WARN_HYST, index - 1), \
  1476. SENSOR_ATTR_2(temp##index##_alarm, S_IRUGO, \
  1477. show_alarm_beep, NULL, ALARM_STATUS, \
  1478. index + (index > 4 ? 11 : 17)), \
  1479. SENSOR_ATTR_2(temp##index##_beep, S_IWUSR | S_IRUGO, \
  1480. show_alarm_beep, store_beep, BEEP_ENABLE, \
  1481. index + (index > 4 ? 11 : 17)), \
  1482. SENSOR_ATTR_2(temp##index##_pwm_enable, S_IWUSR | S_IRUGO, \
  1483. show_temp_pwm_enable, store_temp_pwm_enable, \
  1484. TEMP_PWM_ENABLE, index - 1), \
  1485. SENSOR_ATTR_2(temp##index##_auto_channels_pwm, S_IWUSR | S_IRUGO, \
  1486. show_temp_pwm_enable, store_temp_pwm_enable, \
  1487. TEMP_PWM_FAN_MAP, index - 1), \
  1488. SENSOR_ATTR_2(thermal_cruise##index, S_IWUSR | S_IRUGO, \
  1489. show_temp_pwm, store_temp_pwm, TEMP_PWM_TTTI, index - 1), \
  1490. SENSOR_ATTR_2(temp##index##_warn, S_IWUSR | S_IRUGO, \
  1491. show_temp_pwm, store_temp_pwm, TEMP_PWM_CTFS, index - 1), \
  1492. SENSOR_ATTR_2(temp##index##_warn_hyst, S_IWUSR | S_IRUGO, \
  1493. show_temp_pwm, store_temp_pwm, TEMP_PWM_HCT, index - 1), \
  1494. SENSOR_ATTR_2(temp##index##_operation_hyst, S_IWUSR | S_IRUGO, \
  1495. show_temp_pwm, store_temp_pwm, TEMP_PWM_HOT, index - 1), \
  1496. SENSOR_ATTR_2(temp##index##_auto_point1_pwm, S_IRUGO | S_IWUSR, \
  1497. show_sf4_pwm, store_sf4_pwm, 0, index - 1), \
  1498. SENSOR_ATTR_2(temp##index##_auto_point2_pwm, S_IRUGO | S_IWUSR, \
  1499. show_sf4_pwm, store_sf4_pwm, 1, index - 1), \
  1500. SENSOR_ATTR_2(temp##index##_auto_point3_pwm, S_IRUGO | S_IWUSR, \
  1501. show_sf4_pwm, store_sf4_pwm, 2, index - 1), \
  1502. SENSOR_ATTR_2(temp##index##_auto_point4_pwm, S_IRUGO | S_IWUSR, \
  1503. show_sf4_pwm, store_sf4_pwm, 3, index - 1), \
  1504. SENSOR_ATTR_2(temp##index##_auto_point5_pwm, S_IRUGO | S_IWUSR, \
  1505. show_sf4_pwm, store_sf4_pwm, 4, index - 1), \
  1506. SENSOR_ATTR_2(temp##index##_auto_point6_pwm, S_IRUGO | S_IWUSR, \
  1507. show_sf4_pwm, store_sf4_pwm, 5, index - 1), \
  1508. SENSOR_ATTR_2(temp##index##_auto_point7_pwm, S_IRUGO | S_IWUSR, \
  1509. show_sf4_pwm, store_sf4_pwm, 6, index - 1), \
  1510. SENSOR_ATTR_2(temp##index##_auto_point1_temp, S_IRUGO | S_IWUSR,\
  1511. show_sf4_temp, store_sf4_temp, 0, index - 1), \
  1512. SENSOR_ATTR_2(temp##index##_auto_point2_temp, S_IRUGO | S_IWUSR,\
  1513. show_sf4_temp, store_sf4_temp, 1, index - 1), \
  1514. SENSOR_ATTR_2(temp##index##_auto_point3_temp, S_IRUGO | S_IWUSR,\
  1515. show_sf4_temp, store_sf4_temp, 2, index - 1), \
  1516. SENSOR_ATTR_2(temp##index##_auto_point4_temp, S_IRUGO | S_IWUSR,\
  1517. show_sf4_temp, store_sf4_temp, 3, index - 1), \
  1518. SENSOR_ATTR_2(temp##index##_auto_point5_temp, S_IRUGO | S_IWUSR,\
  1519. show_sf4_temp, store_sf4_temp, 4, index - 1), \
  1520. SENSOR_ATTR_2(temp##index##_auto_point6_temp, S_IRUGO | S_IWUSR,\
  1521. show_sf4_temp, store_sf4_temp, 5, index - 1), \
  1522. SENSOR_ATTR_2(temp##index##_auto_point7_temp, S_IRUGO | S_IWUSR,\
  1523. show_sf4_temp, store_sf4_temp, 6, index - 1) }
  1524. static struct sensor_device_attribute_2 w83795_in[][5] = {
  1525. SENSOR_ATTR_IN(0),
  1526. SENSOR_ATTR_IN(1),
  1527. SENSOR_ATTR_IN(2),
  1528. SENSOR_ATTR_IN(3),
  1529. SENSOR_ATTR_IN(4),
  1530. SENSOR_ATTR_IN(5),
  1531. SENSOR_ATTR_IN(6),
  1532. SENSOR_ATTR_IN(7),
  1533. SENSOR_ATTR_IN(8),
  1534. SENSOR_ATTR_IN(9),
  1535. SENSOR_ATTR_IN(10),
  1536. SENSOR_ATTR_IN(11),
  1537. SENSOR_ATTR_IN(12),
  1538. SENSOR_ATTR_IN(13),
  1539. SENSOR_ATTR_IN(14),
  1540. SENSOR_ATTR_IN(15),
  1541. SENSOR_ATTR_IN(16),
  1542. SENSOR_ATTR_IN(17),
  1543. SENSOR_ATTR_IN(18),
  1544. SENSOR_ATTR_IN(19),
  1545. SENSOR_ATTR_IN(20),
  1546. };
  1547. static const struct sensor_device_attribute_2 w83795_fan[][4] = {
  1548. SENSOR_ATTR_FAN(1),
  1549. SENSOR_ATTR_FAN(2),
  1550. SENSOR_ATTR_FAN(3),
  1551. SENSOR_ATTR_FAN(4),
  1552. SENSOR_ATTR_FAN(5),
  1553. SENSOR_ATTR_FAN(6),
  1554. SENSOR_ATTR_FAN(7),
  1555. SENSOR_ATTR_FAN(8),
  1556. SENSOR_ATTR_FAN(9),
  1557. SENSOR_ATTR_FAN(10),
  1558. SENSOR_ATTR_FAN(11),
  1559. SENSOR_ATTR_FAN(12),
  1560. SENSOR_ATTR_FAN(13),
  1561. SENSOR_ATTR_FAN(14),
  1562. };
  1563. static const struct sensor_device_attribute_2 w83795_temp[][28] = {
  1564. SENSOR_ATTR_TEMP(1),
  1565. SENSOR_ATTR_TEMP(2),
  1566. SENSOR_ATTR_TEMP(3),
  1567. SENSOR_ATTR_TEMP(4),
  1568. SENSOR_ATTR_TEMP(5),
  1569. SENSOR_ATTR_TEMP(6),
  1570. };
  1571. static const struct sensor_device_attribute_2 w83795_dts[][8] = {
  1572. SENSOR_ATTR_DTS(7),
  1573. SENSOR_ATTR_DTS(8),
  1574. SENSOR_ATTR_DTS(9),
  1575. SENSOR_ATTR_DTS(10),
  1576. SENSOR_ATTR_DTS(11),
  1577. SENSOR_ATTR_DTS(12),
  1578. SENSOR_ATTR_DTS(13),
  1579. SENSOR_ATTR_DTS(14),
  1580. };
  1581. static const struct sensor_device_attribute_2 w83795_pwm[][8] = {
  1582. SENSOR_ATTR_PWM(1),
  1583. SENSOR_ATTR_PWM(2),
  1584. SENSOR_ATTR_PWM(3),
  1585. SENSOR_ATTR_PWM(4),
  1586. SENSOR_ATTR_PWM(5),
  1587. SENSOR_ATTR_PWM(6),
  1588. SENSOR_ATTR_PWM(7),
  1589. SENSOR_ATTR_PWM(8),
  1590. };
  1591. static const struct sensor_device_attribute_2 w83795_tss[6] = {
  1592. SENSOR_ATTR_2(temp1_source_sel, S_IWUSR | S_IRUGO,
  1593. show_temp_src, store_temp_src, NOT_USED, 0),
  1594. SENSOR_ATTR_2(temp2_source_sel, S_IWUSR | S_IRUGO,
  1595. show_temp_src, store_temp_src, NOT_USED, 1),
  1596. SENSOR_ATTR_2(temp3_source_sel, S_IWUSR | S_IRUGO,
  1597. show_temp_src, store_temp_src, NOT_USED, 2),
  1598. SENSOR_ATTR_2(temp4_source_sel, S_IWUSR | S_IRUGO,
  1599. show_temp_src, store_temp_src, NOT_USED, 3),
  1600. SENSOR_ATTR_2(temp5_source_sel, S_IWUSR | S_IRUGO,
  1601. show_temp_src, store_temp_src, NOT_USED, 4),
  1602. SENSOR_ATTR_2(temp6_source_sel, S_IWUSR | S_IRUGO,
  1603. show_temp_src, store_temp_src, NOT_USED, 5),
  1604. };
  1605. static const struct sensor_device_attribute_2 sda_single_files[] = {
  1606. SENSOR_ATTR_2(intrusion0_alarm, S_IWUSR | S_IRUGO, show_alarm_beep,
  1607. store_chassis_clear, ALARM_STATUS, 46),
  1608. #ifdef CONFIG_SENSORS_W83795_FANCTRL
  1609. SENSOR_ATTR_2(speed_cruise_tolerance, S_IWUSR | S_IRUGO, show_fanin,
  1610. store_fanin, FANIN_TOL, NOT_USED),
  1611. SENSOR_ATTR_2(pwm_default, S_IWUSR | S_IRUGO, show_sf_setup,
  1612. store_sf_setup, SETUP_PWM_DEFAULT, NOT_USED),
  1613. SENSOR_ATTR_2(pwm_uptime, S_IWUSR | S_IRUGO, show_sf_setup,
  1614. store_sf_setup, SETUP_PWM_UPTIME, NOT_USED),
  1615. SENSOR_ATTR_2(pwm_downtime, S_IWUSR | S_IRUGO, show_sf_setup,
  1616. store_sf_setup, SETUP_PWM_DOWNTIME, NOT_USED),
  1617. #endif
  1618. };
  1619. static const struct sensor_device_attribute_2 sda_beep_files[] = {
  1620. SENSOR_ATTR_2(intrusion0_beep, S_IWUSR | S_IRUGO, show_alarm_beep,
  1621. store_beep, BEEP_ENABLE, 46),
  1622. SENSOR_ATTR_2(beep_enable, S_IWUSR | S_IRUGO, show_alarm_beep,
  1623. store_beep, BEEP_ENABLE, 47),
  1624. };
  1625. /*
  1626. * Driver interface
  1627. */
  1628. static void w83795_init_client(struct i2c_client *client)
  1629. {
  1630. struct w83795_data *data = i2c_get_clientdata(client);
  1631. static const u16 clkin[4] = { /* in kHz */
  1632. 14318, 24000, 33333, 48000
  1633. };
  1634. u8 config;
  1635. if (reset)
  1636. w83795_write(client, W83795_REG_CONFIG, 0x80);
  1637. /* Start monitoring if needed */
  1638. config = w83795_read(client, W83795_REG_CONFIG);
  1639. if (!(config & W83795_REG_CONFIG_START)) {
  1640. dev_info(&client->dev, "Enabling monitoring operations\n");
  1641. w83795_write(client, W83795_REG_CONFIG,
  1642. config | W83795_REG_CONFIG_START);
  1643. }
  1644. data->clkin = clkin[(config >> 3) & 0x3];
  1645. dev_dbg(&client->dev, "clkin = %u kHz\n", data->clkin);
  1646. }
  1647. static int w83795_get_device_id(struct i2c_client *client)
  1648. {
  1649. int device_id;
  1650. device_id = i2c_smbus_read_byte_data(client, W83795_REG_DEVICEID);
  1651. /*
  1652. * Special case for rev. A chips; can't be checked first because later
  1653. * revisions emulate this for compatibility
  1654. */
  1655. if (device_id < 0 || (device_id & 0xf0) != 0x50) {
  1656. int alt_id;
  1657. alt_id = i2c_smbus_read_byte_data(client,
  1658. W83795_REG_DEVICEID_A);
  1659. if (alt_id == 0x50)
  1660. device_id = alt_id;
  1661. }
  1662. return device_id;
  1663. }
  1664. /* Return 0 if detection is successful, -ENODEV otherwise */
  1665. static int w83795_detect(struct i2c_client *client,
  1666. struct i2c_board_info *info)
  1667. {
  1668. int bank, vendor_id, device_id, expected, i2c_addr, config;
  1669. struct i2c_adapter *adapter = client->adapter;
  1670. unsigned short address = client->addr;
  1671. const char *chip_name;
  1672. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1673. return -ENODEV;
  1674. bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
  1675. if (bank < 0 || (bank & 0x7c)) {
  1676. dev_dbg(&adapter->dev,
  1677. "w83795: Detection failed at addr 0x%02hx, check %s\n",
  1678. address, "bank");
  1679. return -ENODEV;
  1680. }
  1681. /* Check Nuvoton vendor ID */
  1682. vendor_id = i2c_smbus_read_byte_data(client, W83795_REG_VENDORID);
  1683. expected = bank & 0x80 ? 0x5c : 0xa3;
  1684. if (vendor_id != expected) {
  1685. dev_dbg(&adapter->dev,
  1686. "w83795: Detection failed at addr 0x%02hx, check %s\n",
  1687. address, "vendor id");
  1688. return -ENODEV;
  1689. }
  1690. /* Check device ID */
  1691. device_id = w83795_get_device_id(client) |
  1692. (i2c_smbus_read_byte_data(client, W83795_REG_CHIPID) << 8);
  1693. if ((device_id >> 4) != 0x795) {
  1694. dev_dbg(&adapter->dev,
  1695. "w83795: Detection failed at addr 0x%02hx, check %s\n",
  1696. address, "device id\n");
  1697. return -ENODEV;
  1698. }
  1699. /*
  1700. * If Nuvoton chip, address of chip and W83795_REG_I2C_ADDR
  1701. * should match
  1702. */
  1703. if ((bank & 0x07) == 0) {
  1704. i2c_addr = i2c_smbus_read_byte_data(client,
  1705. W83795_REG_I2C_ADDR);
  1706. if ((i2c_addr & 0x7f) != address) {
  1707. dev_dbg(&adapter->dev,
  1708. "w83795: Detection failed at addr 0x%02hx, "
  1709. "check %s\n", address, "i2c addr");
  1710. return -ENODEV;
  1711. }
  1712. }
  1713. /*
  1714. * Check 795 chip type: 795G or 795ADG
  1715. * Usually we don't write to chips during detection, but here we don't
  1716. * quite have the choice; hopefully it's OK, we are about to return
  1717. * success anyway
  1718. */
  1719. if ((bank & 0x07) != 0)
  1720. i2c_smbus_write_byte_data(client, W83795_REG_BANKSEL,
  1721. bank & ~0x07);
  1722. config = i2c_smbus_read_byte_data(client, W83795_REG_CONFIG);
  1723. if (config & W83795_REG_CONFIG_CONFIG48)
  1724. chip_name = "w83795adg";
  1725. else
  1726. chip_name = "w83795g";
  1727. strlcpy(info->type, chip_name, I2C_NAME_SIZE);
  1728. dev_info(&adapter->dev, "Found %s rev. %c at 0x%02hx\n", chip_name,
  1729. 'A' + (device_id & 0xf), address);
  1730. return 0;
  1731. }
  1732. #ifdef CONFIG_SENSORS_W83795_FANCTRL
  1733. #define NUM_PWM_ATTRIBUTES ARRAY_SIZE(w83795_pwm[0])
  1734. #define NUM_TEMP_ATTRIBUTES ARRAY_SIZE(w83795_temp[0])
  1735. #else
  1736. #define NUM_PWM_ATTRIBUTES 4
  1737. #define NUM_TEMP_ATTRIBUTES 8
  1738. #endif
  1739. static int w83795_handle_files(struct device *dev, int (*fn)(struct device *,
  1740. const struct device_attribute *))
  1741. {
  1742. struct w83795_data *data = dev_get_drvdata(dev);
  1743. int err, i, j;
  1744. for (i = 0; i < ARRAY_SIZE(w83795_in); i++) {
  1745. if (!(data->has_in & (1 << i)))
  1746. continue;
  1747. for (j = 0; j < ARRAY_SIZE(w83795_in[0]); j++) {
  1748. if (j == 4 && !data->enable_beep)
  1749. continue;
  1750. err = fn(dev, &w83795_in[i][j].dev_attr);
  1751. if (err)
  1752. return err;
  1753. }
  1754. }
  1755. for (i = 0; i < ARRAY_SIZE(w83795_fan); i++) {
  1756. if (!(data->has_fan & (1 << i)))
  1757. continue;
  1758. for (j = 0; j < ARRAY_SIZE(w83795_fan[0]); j++) {
  1759. if (j == 3 && !data->enable_beep)
  1760. continue;
  1761. err = fn(dev, &w83795_fan[i][j].dev_attr);
  1762. if (err)
  1763. return err;
  1764. }
  1765. }
  1766. for (i = 0; i < ARRAY_SIZE(w83795_tss); i++) {
  1767. j = w83795_tss_useful(data, i);
  1768. if (!j)
  1769. continue;
  1770. err = fn(dev, &w83795_tss[i].dev_attr);
  1771. if (err)
  1772. return err;
  1773. }
  1774. for (i = 0; i < ARRAY_SIZE(sda_single_files); i++) {
  1775. err = fn(dev, &sda_single_files[i].dev_attr);
  1776. if (err)
  1777. return err;
  1778. }
  1779. if (data->enable_beep) {
  1780. for (i = 0; i < ARRAY_SIZE(sda_beep_files); i++) {
  1781. err = fn(dev, &sda_beep_files[i].dev_attr);
  1782. if (err)
  1783. return err;
  1784. }
  1785. }
  1786. for (i = 0; i < data->has_pwm; i++) {
  1787. for (j = 0; j < NUM_PWM_ATTRIBUTES; j++) {
  1788. err = fn(dev, &w83795_pwm[i][j].dev_attr);
  1789. if (err)
  1790. return err;
  1791. }
  1792. }
  1793. for (i = 0; i < ARRAY_SIZE(w83795_temp); i++) {
  1794. if (!(data->has_temp & (1 << i)))
  1795. continue;
  1796. for (j = 0; j < NUM_TEMP_ATTRIBUTES; j++) {
  1797. if (j == 7 && !data->enable_beep)
  1798. continue;
  1799. err = fn(dev, &w83795_temp[i][j].dev_attr);
  1800. if (err)
  1801. return err;
  1802. }
  1803. }
  1804. if (data->enable_dts) {
  1805. for (i = 0; i < ARRAY_SIZE(w83795_dts); i++) {
  1806. if (!(data->has_dts & (1 << i)))
  1807. continue;
  1808. for (j = 0; j < ARRAY_SIZE(w83795_dts[0]); j++) {
  1809. if (j == 7 && !data->enable_beep)
  1810. continue;
  1811. err = fn(dev, &w83795_dts[i][j].dev_attr);
  1812. if (err)
  1813. return err;
  1814. }
  1815. }
  1816. }
  1817. return 0;
  1818. }
  1819. /* We need a wrapper that fits in w83795_handle_files */
  1820. static int device_remove_file_wrapper(struct device *dev,
  1821. const struct device_attribute *attr)
  1822. {
  1823. device_remove_file(dev, attr);
  1824. return 0;
  1825. }
  1826. static void w83795_check_dynamic_in_limits(struct i2c_client *client)
  1827. {
  1828. struct w83795_data *data = i2c_get_clientdata(client);
  1829. u8 vid_ctl;
  1830. int i, err_max, err_min;
  1831. vid_ctl = w83795_read(client, W83795_REG_VID_CTRL);
  1832. /* Return immediately if VRM isn't configured */
  1833. if ((vid_ctl & 0x07) == 0x00 || (vid_ctl & 0x07) == 0x07)
  1834. return;
  1835. data->has_dyn_in = (vid_ctl >> 3) & 0x07;
  1836. for (i = 0; i < 2; i++) {
  1837. if (!(data->has_dyn_in & (1 << i)))
  1838. continue;
  1839. /* Voltage limits in dynamic mode, switch to read-only */
  1840. err_max = sysfs_chmod_file(&client->dev.kobj,
  1841. &w83795_in[i][2].dev_attr.attr,
  1842. S_IRUGO);
  1843. err_min = sysfs_chmod_file(&client->dev.kobj,
  1844. &w83795_in[i][3].dev_attr.attr,
  1845. S_IRUGO);
  1846. if (err_max || err_min)
  1847. dev_warn(&client->dev,
  1848. "Failed to set in%d limits read-only (%d, %d)\n",
  1849. i, err_max, err_min);
  1850. else
  1851. dev_info(&client->dev,
  1852. "in%d limits set dynamically from VID\n", i);
  1853. }
  1854. }
  1855. /* Check pins that can be used for either temperature or voltage monitoring */
  1856. static void w83795_apply_temp_config(struct w83795_data *data, u8 config,
  1857. int temp_chan, int in_chan)
  1858. {
  1859. /* config is a 2-bit value */
  1860. switch (config) {
  1861. case 0x2: /* Voltage monitoring */
  1862. data->has_in |= 1 << in_chan;
  1863. break;
  1864. case 0x1: /* Thermal diode */
  1865. if (temp_chan >= 4)
  1866. break;
  1867. data->temp_mode |= 1 << temp_chan;
  1868. /* fall through */
  1869. case 0x3: /* Thermistor */
  1870. data->has_temp |= 1 << temp_chan;
  1871. break;
  1872. }
  1873. }
  1874. static int w83795_probe(struct i2c_client *client,
  1875. const struct i2c_device_id *id)
  1876. {
  1877. int i;
  1878. u8 tmp;
  1879. struct device *dev = &client->dev;
  1880. struct w83795_data *data;
  1881. int err;
  1882. data = devm_kzalloc(dev, sizeof(struct w83795_data), GFP_KERNEL);
  1883. if (!data)
  1884. return -ENOMEM;
  1885. i2c_set_clientdata(client, data);
  1886. data->chip_type = id->driver_data;
  1887. data->bank = i2c_smbus_read_byte_data(client, W83795_REG_BANKSEL);
  1888. mutex_init(&data->update_lock);
  1889. /* Initialize the chip */
  1890. w83795_init_client(client);
  1891. /* Check which voltages and fans are present */
  1892. data->has_in = w83795_read(client, W83795_REG_VOLT_CTRL1)
  1893. | (w83795_read(client, W83795_REG_VOLT_CTRL2) << 8);
  1894. data->has_fan = w83795_read(client, W83795_REG_FANIN_CTRL1)
  1895. | (w83795_read(client, W83795_REG_FANIN_CTRL2) << 8);
  1896. /* Check which analog temperatures and extra voltages are present */
  1897. tmp = w83795_read(client, W83795_REG_TEMP_CTRL1);
  1898. if (tmp & 0x20)
  1899. data->enable_dts = 1;
  1900. w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 5, 16);
  1901. w83795_apply_temp_config(data, tmp & 0x3, 4, 15);
  1902. tmp = w83795_read(client, W83795_REG_TEMP_CTRL2);
  1903. w83795_apply_temp_config(data, tmp >> 6, 3, 20);
  1904. w83795_apply_temp_config(data, (tmp >> 4) & 0x3, 2, 19);
  1905. w83795_apply_temp_config(data, (tmp >> 2) & 0x3, 1, 18);
  1906. w83795_apply_temp_config(data, tmp & 0x3, 0, 17);
  1907. /* Check DTS enable status */
  1908. if (data->enable_dts) {
  1909. if (1 & w83795_read(client, W83795_REG_DTSC))
  1910. data->enable_dts |= 2;
  1911. data->has_dts = w83795_read(client, W83795_REG_DTSE);
  1912. }
  1913. /* Report PECI Tbase values */
  1914. if (data->enable_dts == 1) {
  1915. for (i = 0; i < 8; i++) {
  1916. if (!(data->has_dts & (1 << i)))
  1917. continue;
  1918. tmp = w83795_read(client, W83795_REG_PECI_TBASE(i));
  1919. dev_info(&client->dev,
  1920. "PECI agent %d Tbase temperature: %u\n",
  1921. i + 1, (unsigned int)tmp & 0x7f);
  1922. }
  1923. }
  1924. data->has_gain = w83795_read(client, W83795_REG_VMIGB_CTRL) & 0x0f;
  1925. /* pwm and smart fan */
  1926. if (data->chip_type == w83795g)
  1927. data->has_pwm = 8;
  1928. else
  1929. data->has_pwm = 2;
  1930. /* Check if BEEP pin is available */
  1931. if (data->chip_type == w83795g) {
  1932. /* The W83795G has a dedicated BEEP pin */
  1933. data->enable_beep = 1;
  1934. } else {
  1935. /*
  1936. * The W83795ADG has a shared pin for OVT# and BEEP, so you
  1937. * can't have both
  1938. */
  1939. tmp = w83795_read(client, W83795_REG_OVT_CFG);
  1940. if ((tmp & OVT_CFG_SEL) == 0)
  1941. data->enable_beep = 1;
  1942. }
  1943. err = w83795_handle_files(dev, device_create_file);
  1944. if (err)
  1945. goto exit_remove;
  1946. if (data->chip_type == w83795g)
  1947. w83795_check_dynamic_in_limits(client);
  1948. data->hwmon_dev = hwmon_device_register(dev);
  1949. if (IS_ERR(data->hwmon_dev)) {
  1950. err = PTR_ERR(data->hwmon_dev);
  1951. goto exit_remove;
  1952. }
  1953. return 0;
  1954. exit_remove:
  1955. w83795_handle_files(dev, device_remove_file_wrapper);
  1956. return err;
  1957. }
  1958. static int w83795_remove(struct i2c_client *client)
  1959. {
  1960. struct w83795_data *data = i2c_get_clientdata(client);
  1961. hwmon_device_unregister(data->hwmon_dev);
  1962. w83795_handle_files(&client->dev, device_remove_file_wrapper);
  1963. return 0;
  1964. }
  1965. static const struct i2c_device_id w83795_id[] = {
  1966. { "w83795g", w83795g },
  1967. { "w83795adg", w83795adg },
  1968. { }
  1969. };
  1970. MODULE_DEVICE_TABLE(i2c, w83795_id);
  1971. static struct i2c_driver w83795_driver = {
  1972. .driver = {
  1973. .name = "w83795",
  1974. },
  1975. .probe = w83795_probe,
  1976. .remove = w83795_remove,
  1977. .id_table = w83795_id,
  1978. .class = I2C_CLASS_HWMON,
  1979. .detect = w83795_detect,
  1980. .address_list = normal_i2c,
  1981. };
  1982. module_i2c_driver(w83795_driver);
  1983. MODULE_AUTHOR("Wei Song, Jean Delvare <jdelvare@suse.de>");
  1984. MODULE_DESCRIPTION("W83795G/ADG hardware monitoring driver");
  1985. MODULE_LICENSE("GPL");