f71882fg.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /***************************************************************************
  3. * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
  4. * Copyright (C) 2007-2011 Hans de Goede <hdegoede@redhat.com> *
  5. * *
  6. ***************************************************************************/
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/hwmon.h>
  14. #include <linux/hwmon-sysfs.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/io.h>
  18. #include <linux/acpi.h>
  19. #define DRVNAME "f71882fg"
  20. #define SIO_F71858FG_LD_HWM 0x02 /* Hardware monitor logical device */
  21. #define SIO_F71882FG_LD_HWM 0x04 /* Hardware monitor logical device */
  22. #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
  23. #define SIO_LOCK_KEY 0xAA /* Key to disable Super-I/O */
  24. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  25. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  26. #define SIO_REG_DEVREV 0x22 /* Device revision */
  27. #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
  28. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  29. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  30. #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
  31. #define SIO_F71808E_ID 0x0901 /* Chipset ID */
  32. #define SIO_F71808A_ID 0x1001 /* Chipset ID */
  33. #define SIO_F71858_ID 0x0507 /* Chipset ID */
  34. #define SIO_F71862_ID 0x0601 /* Chipset ID */
  35. #define SIO_F71868_ID 0x1106 /* Chipset ID */
  36. #define SIO_F71869_ID 0x0814 /* Chipset ID */
  37. #define SIO_F71869A_ID 0x1007 /* Chipset ID */
  38. #define SIO_F71882_ID 0x0541 /* Chipset ID */
  39. #define SIO_F71889_ID 0x0723 /* Chipset ID */
  40. #define SIO_F71889E_ID 0x0909 /* Chipset ID */
  41. #define SIO_F71889A_ID 0x1005 /* Chipset ID */
  42. #define SIO_F8000_ID 0x0581 /* Chipset ID */
  43. #define SIO_F81768D_ID 0x1210 /* Chipset ID */
  44. #define SIO_F81865_ID 0x0704 /* Chipset ID */
  45. #define SIO_F81866_ID 0x1010 /* Chipset ID */
  46. #define SIO_F71858AD_ID 0x0903 /* Chipset ID */
  47. #define SIO_F81966_ID 0x1502 /* Chipset ID */
  48. #define SIO_F81968_ID 0x1806 /* Chipset ID */
  49. #define REGION_LENGTH 8
  50. #define ADDR_REG_OFFSET 5
  51. #define DATA_REG_OFFSET 6
  52. #define F71882FG_REG_IN_STATUS 0x12 /* f7188x only */
  53. #define F71882FG_REG_IN_BEEP 0x13 /* f7188x only */
  54. #define F71882FG_REG_IN(nr) (0x20 + (nr))
  55. #define F71882FG_REG_IN1_HIGH 0x32 /* f7188x only */
  56. #define F81866_REG_IN_STATUS 0x16 /* F81866 only */
  57. #define F81866_REG_IN_BEEP 0x17 /* F81866 only */
  58. #define F81866_REG_IN1_HIGH 0x3a /* F81866 only */
  59. #define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
  60. #define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
  61. #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
  62. #define F71882FG_REG_FAN_STATUS 0x92
  63. #define F71882FG_REG_FAN_BEEP 0x93
  64. #define F71882FG_REG_TEMP(nr) (0x70 + 2 * (nr))
  65. #define F71882FG_REG_TEMP_OVT(nr) (0x80 + 2 * (nr))
  66. #define F71882FG_REG_TEMP_HIGH(nr) (0x81 + 2 * (nr))
  67. #define F71882FG_REG_TEMP_STATUS 0x62
  68. #define F71882FG_REG_TEMP_BEEP 0x63
  69. #define F71882FG_REG_TEMP_CONFIG 0x69
  70. #define F71882FG_REG_TEMP_HYST(nr) (0x6C + (nr))
  71. #define F71882FG_REG_TEMP_TYPE 0x6B
  72. #define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
  73. #define F71882FG_REG_PWM(nr) (0xA3 + (16 * (nr)))
  74. #define F71882FG_REG_PWM_TYPE 0x94
  75. #define F71882FG_REG_PWM_ENABLE 0x96
  76. #define F71882FG_REG_FAN_HYST(nr) (0x98 + (nr))
  77. #define F71882FG_REG_FAN_FAULT_T 0x9F
  78. #define F71882FG_FAN_NEG_TEMP_EN 0x20
  79. #define F71882FG_FAN_PROG_SEL 0x80
  80. #define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
  81. #define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
  82. #define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
  83. #define F71882FG_REG_START 0x01
  84. #define F71882FG_MAX_INS 11
  85. #define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
  86. static unsigned short force_id;
  87. module_param(force_id, ushort, 0);
  88. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  89. enum chips { f71808e, f71808a, f71858fg, f71862fg, f71868a, f71869, f71869a,
  90. f71882fg, f71889fg, f71889ed, f71889a, f8000, f81768d, f81865f,
  91. f81866a};
  92. static const char *const f71882fg_names[] = {
  93. "f71808e",
  94. "f71808a",
  95. "f71858fg",
  96. "f71862fg",
  97. "f71868a",
  98. "f71869", /* Both f71869f and f71869e, reg. compatible and same id */
  99. "f71869a",
  100. "f71882fg",
  101. "f71889fg", /* f81801u too, same id */
  102. "f71889ed",
  103. "f71889a",
  104. "f8000",
  105. "f81768d",
  106. "f81865f",
  107. "f81866a",
  108. };
  109. static const char f71882fg_has_in[][F71882FG_MAX_INS] = {
  110. [f71808e] = { 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0 },
  111. [f71808a] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0 },
  112. [f71858fg] = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
  113. [f71862fg] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  114. [f71868a] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
  115. [f71869] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  116. [f71869a] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  117. [f71882fg] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  118. [f71889fg] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  119. [f71889ed] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  120. [f71889a] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0 },
  121. [f8000] = { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
  122. [f81768d] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  123. [f81865f] = { 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0 },
  124. [f81866a] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0 },
  125. };
  126. static const char f71882fg_has_in1_alarm[] = {
  127. [f71808e] = 0,
  128. [f71808a] = 0,
  129. [f71858fg] = 0,
  130. [f71862fg] = 0,
  131. [f71868a] = 0,
  132. [f71869] = 0,
  133. [f71869a] = 0,
  134. [f71882fg] = 1,
  135. [f71889fg] = 1,
  136. [f71889ed] = 1,
  137. [f71889a] = 1,
  138. [f8000] = 0,
  139. [f81768d] = 1,
  140. [f81865f] = 1,
  141. [f81866a] = 1,
  142. };
  143. static const char f71882fg_fan_has_beep[] = {
  144. [f71808e] = 0,
  145. [f71808a] = 0,
  146. [f71858fg] = 0,
  147. [f71862fg] = 1,
  148. [f71868a] = 1,
  149. [f71869] = 1,
  150. [f71869a] = 1,
  151. [f71882fg] = 1,
  152. [f71889fg] = 1,
  153. [f71889ed] = 1,
  154. [f71889a] = 1,
  155. [f8000] = 0,
  156. [f81768d] = 1,
  157. [f81865f] = 1,
  158. [f81866a] = 1,
  159. };
  160. static const char f71882fg_nr_fans[] = {
  161. [f71808e] = 3,
  162. [f71808a] = 2, /* +1 fan which is monitor + simple pwm only */
  163. [f71858fg] = 3,
  164. [f71862fg] = 3,
  165. [f71868a] = 3,
  166. [f71869] = 3,
  167. [f71869a] = 3,
  168. [f71882fg] = 4,
  169. [f71889fg] = 3,
  170. [f71889ed] = 3,
  171. [f71889a] = 3,
  172. [f8000] = 3, /* +1 fan which is monitor only */
  173. [f81768d] = 3,
  174. [f81865f] = 2,
  175. [f81866a] = 3,
  176. };
  177. static const char f71882fg_temp_has_beep[] = {
  178. [f71808e] = 0,
  179. [f71808a] = 1,
  180. [f71858fg] = 0,
  181. [f71862fg] = 1,
  182. [f71868a] = 1,
  183. [f71869] = 1,
  184. [f71869a] = 1,
  185. [f71882fg] = 1,
  186. [f71889fg] = 1,
  187. [f71889ed] = 1,
  188. [f71889a] = 1,
  189. [f8000] = 0,
  190. [f81768d] = 1,
  191. [f81865f] = 1,
  192. [f81866a] = 1,
  193. };
  194. static const char f71882fg_nr_temps[] = {
  195. [f71808e] = 2,
  196. [f71808a] = 2,
  197. [f71858fg] = 3,
  198. [f71862fg] = 3,
  199. [f71868a] = 3,
  200. [f71869] = 3,
  201. [f71869a] = 3,
  202. [f71882fg] = 3,
  203. [f71889fg] = 3,
  204. [f71889ed] = 3,
  205. [f71889a] = 3,
  206. [f8000] = 3,
  207. [f81768d] = 3,
  208. [f81865f] = 2,
  209. [f81866a] = 3,
  210. };
  211. static struct platform_device *f71882fg_pdev;
  212. struct f71882fg_sio_data {
  213. enum chips type;
  214. };
  215. struct f71882fg_data {
  216. unsigned short addr;
  217. enum chips type;
  218. struct device *hwmon_dev;
  219. struct mutex update_lock;
  220. int temp_start; /* temp numbering start (0 or 1) */
  221. bool valid; /* true if following fields are valid */
  222. char auto_point_temp_signed;
  223. unsigned long last_updated; /* In jiffies */
  224. unsigned long last_limits; /* In jiffies */
  225. /* Register Values */
  226. u8 in[F71882FG_MAX_INS];
  227. u8 in1_max;
  228. u8 in_status;
  229. u8 in_beep;
  230. u16 fan[4];
  231. u16 fan_target[4];
  232. u16 fan_full_speed[4];
  233. u8 fan_status;
  234. u8 fan_beep;
  235. /*
  236. * Note: all models have max 3 temperature channels, but on some
  237. * they are addressed as 0-2 and on others as 1-3, so for coding
  238. * convenience we reserve space for 4 channels
  239. */
  240. u16 temp[4];
  241. u8 temp_ovt[4];
  242. u8 temp_high[4];
  243. u8 temp_hyst[2]; /* 2 hysts stored per reg */
  244. u8 temp_type[4];
  245. u8 temp_status;
  246. u8 temp_beep;
  247. u8 temp_diode_open;
  248. u8 temp_config;
  249. u8 pwm[4];
  250. u8 pwm_enable;
  251. u8 pwm_auto_point_hyst[2];
  252. u8 pwm_auto_point_mapping[4];
  253. u8 pwm_auto_point_pwm[4][5];
  254. s8 pwm_auto_point_temp[4][4];
  255. };
  256. static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
  257. {
  258. u8 val;
  259. outb(reg, data->addr + ADDR_REG_OFFSET);
  260. val = inb(data->addr + DATA_REG_OFFSET);
  261. return val;
  262. }
  263. static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
  264. {
  265. u16 val;
  266. val = f71882fg_read8(data, reg) << 8;
  267. val |= f71882fg_read8(data, reg + 1);
  268. return val;
  269. }
  270. static inline int fan_from_reg(u16 reg)
  271. {
  272. return reg ? (1500000 / reg) : 0;
  273. }
  274. static inline u16 fan_to_reg(int fan)
  275. {
  276. return fan ? (1500000 / fan) : 0;
  277. }
  278. static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
  279. {
  280. outb(reg, data->addr + ADDR_REG_OFFSET);
  281. outb(val, data->addr + DATA_REG_OFFSET);
  282. }
  283. static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
  284. {
  285. f71882fg_write8(data, reg, val >> 8);
  286. f71882fg_write8(data, reg + 1, val & 0xff);
  287. }
  288. static u16 f71882fg_read_temp(struct f71882fg_data *data, int nr)
  289. {
  290. if (data->type == f71858fg)
  291. return f71882fg_read16(data, F71882FG_REG_TEMP(nr));
  292. else
  293. return f71882fg_read8(data, F71882FG_REG_TEMP(nr));
  294. }
  295. static struct f71882fg_data *f71882fg_update_device(struct device *dev)
  296. {
  297. struct f71882fg_data *data = dev_get_drvdata(dev);
  298. int nr_fans = f71882fg_nr_fans[data->type];
  299. int nr_temps = f71882fg_nr_temps[data->type];
  300. int nr, reg, point;
  301. mutex_lock(&data->update_lock);
  302. /* Update once every 60 seconds */
  303. if (time_after(jiffies, data->last_limits + 60 * HZ) ||
  304. !data->valid) {
  305. if (f71882fg_has_in1_alarm[data->type]) {
  306. if (data->type == f81866a) {
  307. data->in1_max =
  308. f71882fg_read8(data,
  309. F81866_REG_IN1_HIGH);
  310. data->in_beep =
  311. f71882fg_read8(data,
  312. F81866_REG_IN_BEEP);
  313. } else {
  314. data->in1_max =
  315. f71882fg_read8(data,
  316. F71882FG_REG_IN1_HIGH);
  317. data->in_beep =
  318. f71882fg_read8(data,
  319. F71882FG_REG_IN_BEEP);
  320. }
  321. }
  322. /* Get High & boundary temps*/
  323. for (nr = data->temp_start; nr < nr_temps + data->temp_start;
  324. nr++) {
  325. data->temp_ovt[nr] = f71882fg_read8(data,
  326. F71882FG_REG_TEMP_OVT(nr));
  327. data->temp_high[nr] = f71882fg_read8(data,
  328. F71882FG_REG_TEMP_HIGH(nr));
  329. }
  330. if (data->type != f8000) {
  331. data->temp_hyst[0] = f71882fg_read8(data,
  332. F71882FG_REG_TEMP_HYST(0));
  333. data->temp_hyst[1] = f71882fg_read8(data,
  334. F71882FG_REG_TEMP_HYST(1));
  335. }
  336. /* All but the f71858fg / f8000 have this register */
  337. if ((data->type != f71858fg) && (data->type != f8000)) {
  338. reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
  339. data->temp_type[1] = (reg & 0x02) ? 2 : 4;
  340. data->temp_type[2] = (reg & 0x04) ? 2 : 4;
  341. data->temp_type[3] = (reg & 0x08) ? 2 : 4;
  342. }
  343. if (f71882fg_fan_has_beep[data->type])
  344. data->fan_beep = f71882fg_read8(data,
  345. F71882FG_REG_FAN_BEEP);
  346. if (f71882fg_temp_has_beep[data->type])
  347. data->temp_beep = f71882fg_read8(data,
  348. F71882FG_REG_TEMP_BEEP);
  349. data->pwm_enable = f71882fg_read8(data,
  350. F71882FG_REG_PWM_ENABLE);
  351. data->pwm_auto_point_hyst[0] =
  352. f71882fg_read8(data, F71882FG_REG_FAN_HYST(0));
  353. data->pwm_auto_point_hyst[1] =
  354. f71882fg_read8(data, F71882FG_REG_FAN_HYST(1));
  355. for (nr = 0; nr < nr_fans; nr++) {
  356. data->pwm_auto_point_mapping[nr] =
  357. f71882fg_read8(data,
  358. F71882FG_REG_POINT_MAPPING(nr));
  359. switch (data->type) {
  360. default:
  361. for (point = 0; point < 5; point++) {
  362. data->pwm_auto_point_pwm[nr][point] =
  363. f71882fg_read8(data,
  364. F71882FG_REG_POINT_PWM
  365. (nr, point));
  366. }
  367. for (point = 0; point < 4; point++) {
  368. data->pwm_auto_point_temp[nr][point] =
  369. f71882fg_read8(data,
  370. F71882FG_REG_POINT_TEMP
  371. (nr, point));
  372. }
  373. break;
  374. case f71808e:
  375. case f71869:
  376. data->pwm_auto_point_pwm[nr][0] =
  377. f71882fg_read8(data,
  378. F71882FG_REG_POINT_PWM(nr, 0));
  379. fallthrough;
  380. case f71862fg:
  381. data->pwm_auto_point_pwm[nr][1] =
  382. f71882fg_read8(data,
  383. F71882FG_REG_POINT_PWM
  384. (nr, 1));
  385. data->pwm_auto_point_pwm[nr][4] =
  386. f71882fg_read8(data,
  387. F71882FG_REG_POINT_PWM
  388. (nr, 4));
  389. data->pwm_auto_point_temp[nr][0] =
  390. f71882fg_read8(data,
  391. F71882FG_REG_POINT_TEMP
  392. (nr, 0));
  393. data->pwm_auto_point_temp[nr][3] =
  394. f71882fg_read8(data,
  395. F71882FG_REG_POINT_TEMP
  396. (nr, 3));
  397. break;
  398. }
  399. }
  400. data->last_limits = jiffies;
  401. }
  402. /* Update every second */
  403. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  404. data->temp_status = f71882fg_read8(data,
  405. F71882FG_REG_TEMP_STATUS);
  406. data->temp_diode_open = f71882fg_read8(data,
  407. F71882FG_REG_TEMP_DIODE_OPEN);
  408. for (nr = data->temp_start; nr < nr_temps + data->temp_start;
  409. nr++)
  410. data->temp[nr] = f71882fg_read_temp(data, nr);
  411. data->fan_status = f71882fg_read8(data,
  412. F71882FG_REG_FAN_STATUS);
  413. for (nr = 0; nr < nr_fans; nr++) {
  414. data->fan[nr] = f71882fg_read16(data,
  415. F71882FG_REG_FAN(nr));
  416. data->fan_target[nr] =
  417. f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
  418. data->fan_full_speed[nr] =
  419. f71882fg_read16(data,
  420. F71882FG_REG_FAN_FULL_SPEED(nr));
  421. data->pwm[nr] =
  422. f71882fg_read8(data, F71882FG_REG_PWM(nr));
  423. }
  424. /* Some models have 1 more fan with limited capabilities */
  425. if (data->type == f71808a) {
  426. data->fan[2] = f71882fg_read16(data,
  427. F71882FG_REG_FAN(2));
  428. data->pwm[2] = f71882fg_read8(data,
  429. F71882FG_REG_PWM(2));
  430. }
  431. if (data->type == f8000)
  432. data->fan[3] = f71882fg_read16(data,
  433. F71882FG_REG_FAN(3));
  434. if (f71882fg_has_in1_alarm[data->type]) {
  435. if (data->type == f81866a)
  436. data->in_status = f71882fg_read8(data,
  437. F81866_REG_IN_STATUS);
  438. else
  439. data->in_status = f71882fg_read8(data,
  440. F71882FG_REG_IN_STATUS);
  441. }
  442. for (nr = 0; nr < F71882FG_MAX_INS; nr++)
  443. if (f71882fg_has_in[data->type][nr])
  444. data->in[nr] = f71882fg_read8(data,
  445. F71882FG_REG_IN(nr));
  446. data->last_updated = jiffies;
  447. data->valid = true;
  448. }
  449. mutex_unlock(&data->update_lock);
  450. return data;
  451. }
  452. static ssize_t name_show(struct device *dev, struct device_attribute *devattr,
  453. char *buf)
  454. {
  455. struct f71882fg_data *data = dev_get_drvdata(dev);
  456. return sprintf(buf, "%s\n", f71882fg_names[data->type]);
  457. }
  458. static DEVICE_ATTR_RO(name);
  459. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  460. char *buf)
  461. {
  462. struct f71882fg_data *data = f71882fg_update_device(dev);
  463. int nr = to_sensor_dev_attr_2(devattr)->index;
  464. int sign, temp;
  465. if (data->type == f71858fg) {
  466. /* TEMP_TABLE_SEL 1 or 3 ? */
  467. if (data->temp_config & 1) {
  468. sign = data->temp[nr] & 0x0001;
  469. temp = (data->temp[nr] >> 5) & 0x7ff;
  470. } else {
  471. sign = data->temp[nr] & 0x8000;
  472. temp = (data->temp[nr] >> 5) & 0x3ff;
  473. }
  474. temp *= 125;
  475. if (sign)
  476. temp -= 128000;
  477. } else {
  478. temp = ((s8)data->temp[nr]) * 1000;
  479. }
  480. return sprintf(buf, "%d\n", temp);
  481. }
  482. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  483. *devattr, char *buf)
  484. {
  485. struct f71882fg_data *data = f71882fg_update_device(dev);
  486. int nr = to_sensor_dev_attr_2(devattr)->index;
  487. return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
  488. }
  489. static ssize_t store_temp_max(struct device *dev, struct device_attribute
  490. *devattr, const char *buf, size_t count)
  491. {
  492. struct f71882fg_data *data = dev_get_drvdata(dev);
  493. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  494. long val;
  495. err = kstrtol(buf, 10, &val);
  496. if (err)
  497. return err;
  498. val /= 1000;
  499. val = clamp_val(val, 0, 255);
  500. mutex_lock(&data->update_lock);
  501. f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
  502. data->temp_high[nr] = val;
  503. mutex_unlock(&data->update_lock);
  504. return count;
  505. }
  506. static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
  507. *devattr, char *buf)
  508. {
  509. struct f71882fg_data *data = f71882fg_update_device(dev);
  510. int nr = to_sensor_dev_attr_2(devattr)->index;
  511. int temp_max_hyst;
  512. mutex_lock(&data->update_lock);
  513. if (nr & 1)
  514. temp_max_hyst = data->temp_hyst[nr / 2] >> 4;
  515. else
  516. temp_max_hyst = data->temp_hyst[nr / 2] & 0x0f;
  517. temp_max_hyst = (data->temp_high[nr] - temp_max_hyst) * 1000;
  518. mutex_unlock(&data->update_lock);
  519. return sprintf(buf, "%d\n", temp_max_hyst);
  520. }
  521. static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
  522. *devattr, const char *buf, size_t count)
  523. {
  524. struct f71882fg_data *data = dev_get_drvdata(dev);
  525. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  526. ssize_t ret = count;
  527. u8 reg;
  528. long val;
  529. err = kstrtol(buf, 10, &val);
  530. if (err)
  531. return err;
  532. val /= 1000;
  533. mutex_lock(&data->update_lock);
  534. /* convert abs to relative and check */
  535. data->temp_high[nr] = f71882fg_read8(data, F71882FG_REG_TEMP_HIGH(nr));
  536. val = clamp_val(val, data->temp_high[nr] - 15, data->temp_high[nr]);
  537. val = data->temp_high[nr] - val;
  538. /* convert value to register contents */
  539. reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST(nr / 2));
  540. if (nr & 1)
  541. reg = (reg & 0x0f) | (val << 4);
  542. else
  543. reg = (reg & 0xf0) | val;
  544. f71882fg_write8(data, F71882FG_REG_TEMP_HYST(nr / 2), reg);
  545. data->temp_hyst[nr / 2] = reg;
  546. mutex_unlock(&data->update_lock);
  547. return ret;
  548. }
  549. static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
  550. *devattr, char *buf)
  551. {
  552. struct f71882fg_data *data = f71882fg_update_device(dev);
  553. int nr = to_sensor_dev_attr_2(devattr)->index;
  554. if (data->temp_status & (1 << nr))
  555. return sprintf(buf, "1\n");
  556. else
  557. return sprintf(buf, "0\n");
  558. }
  559. static ssize_t show_temp_crit(struct device *dev, struct device_attribute
  560. *devattr, char *buf)
  561. {
  562. struct f71882fg_data *data = f71882fg_update_device(dev);
  563. int nr = to_sensor_dev_attr_2(devattr)->index;
  564. return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
  565. }
  566. static ssize_t store_temp_crit(struct device *dev, struct device_attribute
  567. *devattr, const char *buf, size_t count)
  568. {
  569. struct f71882fg_data *data = dev_get_drvdata(dev);
  570. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  571. long val;
  572. err = kstrtol(buf, 10, &val);
  573. if (err)
  574. return err;
  575. val /= 1000;
  576. val = clamp_val(val, 0, 255);
  577. mutex_lock(&data->update_lock);
  578. f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
  579. data->temp_ovt[nr] = val;
  580. mutex_unlock(&data->update_lock);
  581. return count;
  582. }
  583. static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
  584. *devattr, char *buf)
  585. {
  586. struct f71882fg_data *data = f71882fg_update_device(dev);
  587. int nr = to_sensor_dev_attr_2(devattr)->index;
  588. int temp_crit_hyst;
  589. mutex_lock(&data->update_lock);
  590. if (nr & 1)
  591. temp_crit_hyst = data->temp_hyst[nr / 2] >> 4;
  592. else
  593. temp_crit_hyst = data->temp_hyst[nr / 2] & 0x0f;
  594. temp_crit_hyst = (data->temp_ovt[nr] - temp_crit_hyst) * 1000;
  595. mutex_unlock(&data->update_lock);
  596. return sprintf(buf, "%d\n", temp_crit_hyst);
  597. }
  598. static ssize_t show_temp_fault(struct device *dev, struct device_attribute
  599. *devattr, char *buf)
  600. {
  601. struct f71882fg_data *data = f71882fg_update_device(dev);
  602. int nr = to_sensor_dev_attr_2(devattr)->index;
  603. if (data->temp_diode_open & (1 << nr))
  604. return sprintf(buf, "1\n");
  605. else
  606. return sprintf(buf, "0\n");
  607. }
  608. /*
  609. * Temp attr for the f71858fg, the f71858fg is special as it has its
  610. * temperature indexes start at 0 (the others start at 1)
  611. */
  612. static struct sensor_device_attribute_2 f71858fg_temp_attr[] = {
  613. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
  614. SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
  615. store_temp_max, 0, 0),
  616. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  617. store_temp_max_hyst, 0, 0),
  618. SENSOR_ATTR_2(temp1_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 0),
  619. SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  620. store_temp_crit, 0, 0),
  621. SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  622. 0, 0),
  623. SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
  624. SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
  625. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
  626. SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
  627. store_temp_max, 0, 1),
  628. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  629. store_temp_max_hyst, 0, 1),
  630. SENSOR_ATTR_2(temp2_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
  631. SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  632. store_temp_crit, 0, 1),
  633. SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  634. 0, 1),
  635. SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
  636. SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
  637. SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
  638. SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
  639. store_temp_max, 0, 2),
  640. SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  641. store_temp_max_hyst, 0, 2),
  642. SENSOR_ATTR_2(temp3_max_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
  643. SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  644. store_temp_crit, 0, 2),
  645. SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  646. 0, 2),
  647. SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
  648. SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
  649. };
  650. static ssize_t show_temp_type(struct device *dev, struct device_attribute
  651. *devattr, char *buf)
  652. {
  653. struct f71882fg_data *data = f71882fg_update_device(dev);
  654. int nr = to_sensor_dev_attr_2(devattr)->index;
  655. return sprintf(buf, "%d\n", data->temp_type[nr]);
  656. }
  657. /* Temp attr for the standard models */
  658. static struct sensor_device_attribute_2 fxxxx_temp_attr[3][9] = { {
  659. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
  660. SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
  661. store_temp_max, 0, 1),
  662. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  663. store_temp_max_hyst, 0, 1),
  664. /*
  665. * Should really be temp1_max_alarm, but older versions did not handle
  666. * the max and crit alarms separately and lm_sensors v2 depends on the
  667. * presence of temp#_alarm files. The same goes for temp2/3 _alarm.
  668. */
  669. SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
  670. SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  671. store_temp_crit, 0, 1),
  672. SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  673. 0, 1),
  674. SENSOR_ATTR_2(temp1_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
  675. SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
  676. SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
  677. }, {
  678. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
  679. SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
  680. store_temp_max, 0, 2),
  681. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  682. store_temp_max_hyst, 0, 2),
  683. /* Should be temp2_max_alarm, see temp1_alarm note */
  684. SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
  685. SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  686. store_temp_crit, 0, 2),
  687. SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  688. 0, 2),
  689. SENSOR_ATTR_2(temp2_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
  690. SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
  691. SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
  692. }, {
  693. SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
  694. SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
  695. store_temp_max, 0, 3),
  696. SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  697. store_temp_max_hyst, 0, 3),
  698. /* Should be temp3_max_alarm, see temp1_alarm note */
  699. SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
  700. SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  701. store_temp_crit, 0, 3),
  702. SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  703. 0, 3),
  704. SENSOR_ATTR_2(temp3_crit_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 7),
  705. SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
  706. SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
  707. } };
  708. static ssize_t show_temp_beep(struct device *dev, struct device_attribute
  709. *devattr, char *buf)
  710. {
  711. struct f71882fg_data *data = f71882fg_update_device(dev);
  712. int nr = to_sensor_dev_attr_2(devattr)->index;
  713. if (data->temp_beep & (1 << nr))
  714. return sprintf(buf, "1\n");
  715. else
  716. return sprintf(buf, "0\n");
  717. }
  718. static ssize_t store_temp_beep(struct device *dev, struct device_attribute
  719. *devattr, const char *buf, size_t count)
  720. {
  721. struct f71882fg_data *data = dev_get_drvdata(dev);
  722. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  723. unsigned long val;
  724. err = kstrtoul(buf, 10, &val);
  725. if (err)
  726. return err;
  727. mutex_lock(&data->update_lock);
  728. data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
  729. if (val)
  730. data->temp_beep |= 1 << nr;
  731. else
  732. data->temp_beep &= ~(1 << nr);
  733. f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
  734. mutex_unlock(&data->update_lock);
  735. return count;
  736. }
  737. /* Temp attr for models which can beep on temp alarm */
  738. static struct sensor_device_attribute_2 fxxxx_temp_beep_attr[3][2] = { {
  739. SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  740. store_temp_beep, 0, 1),
  741. SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  742. store_temp_beep, 0, 5),
  743. }, {
  744. SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  745. store_temp_beep, 0, 2),
  746. SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  747. store_temp_beep, 0, 6),
  748. }, {
  749. SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  750. store_temp_beep, 0, 3),
  751. SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  752. store_temp_beep, 0, 7),
  753. } };
  754. static struct sensor_device_attribute_2 f81866_temp_beep_attr[3][2] = { {
  755. SENSOR_ATTR_2(temp1_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  756. store_temp_beep, 0, 0),
  757. SENSOR_ATTR_2(temp1_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  758. store_temp_beep, 0, 4),
  759. }, {
  760. SENSOR_ATTR_2(temp2_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  761. store_temp_beep, 0, 1),
  762. SENSOR_ATTR_2(temp2_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  763. store_temp_beep, 0, 5),
  764. }, {
  765. SENSOR_ATTR_2(temp3_max_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  766. store_temp_beep, 0, 2),
  767. SENSOR_ATTR_2(temp3_crit_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  768. store_temp_beep, 0, 6),
  769. } };
  770. /*
  771. * Temp attr for the f8000
  772. * Note on the f8000 temp_ovt (crit) is used as max, and temp_high (max)
  773. * is used as hysteresis value to clear alarms
  774. * Also like the f71858fg its temperature indexes start at 0
  775. */
  776. static struct sensor_device_attribute_2 f8000_temp_attr[] = {
  777. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 0),
  778. SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_crit,
  779. store_temp_crit, 0, 0),
  780. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
  781. store_temp_max, 0, 0),
  782. SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 4),
  783. SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 0),
  784. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 1),
  785. SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_crit,
  786. store_temp_crit, 0, 1),
  787. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
  788. store_temp_max, 0, 1),
  789. SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 5),
  790. SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
  791. SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 2),
  792. SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_crit,
  793. store_temp_crit, 0, 2),
  794. SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max,
  795. store_temp_max, 0, 2),
  796. SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 6),
  797. SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
  798. };
  799. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  800. char *buf)
  801. {
  802. struct f71882fg_data *data = f71882fg_update_device(dev);
  803. int nr = to_sensor_dev_attr_2(devattr)->index;
  804. return sprintf(buf, "%d\n", data->in[nr] * 8);
  805. }
  806. /* in attr for all models */
  807. static struct sensor_device_attribute_2 fxxxx_in_attr[] = {
  808. SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
  809. SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
  810. SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
  811. SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
  812. SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
  813. SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
  814. SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
  815. SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
  816. SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
  817. SENSOR_ATTR_2(in9_input, S_IRUGO, show_in, NULL, 0, 9),
  818. SENSOR_ATTR_2(in10_input, S_IRUGO, show_in, NULL, 0, 10),
  819. };
  820. static ssize_t show_in_max(struct device *dev, struct device_attribute
  821. *devattr, char *buf)
  822. {
  823. struct f71882fg_data *data = f71882fg_update_device(dev);
  824. return sprintf(buf, "%d\n", data->in1_max * 8);
  825. }
  826. static ssize_t store_in_max(struct device *dev, struct device_attribute
  827. *devattr, const char *buf, size_t count)
  828. {
  829. struct f71882fg_data *data = dev_get_drvdata(dev);
  830. int err;
  831. long val;
  832. err = kstrtol(buf, 10, &val);
  833. if (err)
  834. return err;
  835. val /= 8;
  836. val = clamp_val(val, 0, 255);
  837. mutex_lock(&data->update_lock);
  838. if (data->type == f81866a)
  839. f71882fg_write8(data, F81866_REG_IN1_HIGH, val);
  840. else
  841. f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
  842. data->in1_max = val;
  843. mutex_unlock(&data->update_lock);
  844. return count;
  845. }
  846. static ssize_t show_in_beep(struct device *dev, struct device_attribute
  847. *devattr, char *buf)
  848. {
  849. struct f71882fg_data *data = f71882fg_update_device(dev);
  850. int nr = to_sensor_dev_attr_2(devattr)->index;
  851. if (data->in_beep & (1 << nr))
  852. return sprintf(buf, "1\n");
  853. else
  854. return sprintf(buf, "0\n");
  855. }
  856. static ssize_t store_in_beep(struct device *dev, struct device_attribute
  857. *devattr, const char *buf, size_t count)
  858. {
  859. struct f71882fg_data *data = dev_get_drvdata(dev);
  860. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  861. unsigned long val;
  862. err = kstrtoul(buf, 10, &val);
  863. if (err)
  864. return err;
  865. mutex_lock(&data->update_lock);
  866. if (data->type == f81866a)
  867. data->in_beep = f71882fg_read8(data, F81866_REG_IN_BEEP);
  868. else
  869. data->in_beep = f71882fg_read8(data, F71882FG_REG_IN_BEEP);
  870. if (val)
  871. data->in_beep |= 1 << nr;
  872. else
  873. data->in_beep &= ~(1 << nr);
  874. if (data->type == f81866a)
  875. f71882fg_write8(data, F81866_REG_IN_BEEP, data->in_beep);
  876. else
  877. f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
  878. mutex_unlock(&data->update_lock);
  879. return count;
  880. }
  881. static ssize_t show_in_alarm(struct device *dev, struct device_attribute
  882. *devattr, char *buf)
  883. {
  884. struct f71882fg_data *data = f71882fg_update_device(dev);
  885. int nr = to_sensor_dev_attr_2(devattr)->index;
  886. if (data->in_status & (1 << nr))
  887. return sprintf(buf, "1\n");
  888. else
  889. return sprintf(buf, "0\n");
  890. }
  891. /* For models with in1 alarm capability */
  892. static struct sensor_device_attribute_2 fxxxx_in1_alarm_attr[] = {
  893. SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
  894. 0, 1),
  895. SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
  896. 0, 1),
  897. SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
  898. };
  899. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  900. char *buf)
  901. {
  902. struct f71882fg_data *data = f71882fg_update_device(dev);
  903. int nr = to_sensor_dev_attr_2(devattr)->index;
  904. int speed = fan_from_reg(data->fan[nr]);
  905. if (speed == FAN_MIN_DETECT)
  906. speed = 0;
  907. return sprintf(buf, "%d\n", speed);
  908. }
  909. static ssize_t show_fan_full_speed(struct device *dev,
  910. struct device_attribute *devattr, char *buf)
  911. {
  912. struct f71882fg_data *data = f71882fg_update_device(dev);
  913. int nr = to_sensor_dev_attr_2(devattr)->index;
  914. int speed = fan_from_reg(data->fan_full_speed[nr]);
  915. return sprintf(buf, "%d\n", speed);
  916. }
  917. static ssize_t store_fan_full_speed(struct device *dev,
  918. struct device_attribute *devattr,
  919. const char *buf, size_t count)
  920. {
  921. struct f71882fg_data *data = dev_get_drvdata(dev);
  922. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  923. long val;
  924. err = kstrtol(buf, 10, &val);
  925. if (err)
  926. return err;
  927. val = clamp_val(val, 23, 1500000);
  928. val = fan_to_reg(val);
  929. mutex_lock(&data->update_lock);
  930. f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
  931. data->fan_full_speed[nr] = val;
  932. mutex_unlock(&data->update_lock);
  933. return count;
  934. }
  935. static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
  936. *devattr, char *buf)
  937. {
  938. struct f71882fg_data *data = f71882fg_update_device(dev);
  939. int nr = to_sensor_dev_attr_2(devattr)->index;
  940. if (data->fan_status & (1 << nr))
  941. return sprintf(buf, "1\n");
  942. else
  943. return sprintf(buf, "0\n");
  944. }
  945. static ssize_t show_pwm(struct device *dev,
  946. struct device_attribute *devattr, char *buf)
  947. {
  948. struct f71882fg_data *data = f71882fg_update_device(dev);
  949. int val, nr = to_sensor_dev_attr_2(devattr)->index;
  950. mutex_lock(&data->update_lock);
  951. if (data->pwm_enable & (1 << (2 * nr)))
  952. /* PWM mode */
  953. val = data->pwm[nr];
  954. else {
  955. /* RPM mode */
  956. if (fan_from_reg(data->fan_full_speed[nr]))
  957. val = 255 * fan_from_reg(data->fan_target[nr])
  958. / fan_from_reg(data->fan_full_speed[nr]);
  959. else
  960. val = 0;
  961. }
  962. mutex_unlock(&data->update_lock);
  963. return sprintf(buf, "%d\n", val);
  964. }
  965. static ssize_t store_pwm(struct device *dev,
  966. struct device_attribute *devattr, const char *buf,
  967. size_t count)
  968. {
  969. struct f71882fg_data *data = dev_get_drvdata(dev);
  970. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  971. long val;
  972. err = kstrtol(buf, 10, &val);
  973. if (err)
  974. return err;
  975. val = clamp_val(val, 0, 255);
  976. mutex_lock(&data->update_lock);
  977. data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
  978. if ((data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 3) != 2) ||
  979. (data->type != f8000 && !((data->pwm_enable >> 2 * nr) & 2))) {
  980. count = -EROFS;
  981. goto leave;
  982. }
  983. if (data->pwm_enable & (1 << (2 * nr))) {
  984. /* PWM mode */
  985. f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
  986. data->pwm[nr] = val;
  987. } else {
  988. /* RPM mode */
  989. int target, full_speed;
  990. full_speed = f71882fg_read16(data,
  991. F71882FG_REG_FAN_FULL_SPEED(nr));
  992. target = fan_to_reg(val * fan_from_reg(full_speed) / 255);
  993. f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr), target);
  994. data->fan_target[nr] = target;
  995. data->fan_full_speed[nr] = full_speed;
  996. }
  997. leave:
  998. mutex_unlock(&data->update_lock);
  999. return count;
  1000. }
  1001. static ssize_t show_pwm_enable(struct device *dev,
  1002. struct device_attribute *devattr, char *buf)
  1003. {
  1004. int result = 0;
  1005. struct f71882fg_data *data = f71882fg_update_device(dev);
  1006. int nr = to_sensor_dev_attr_2(devattr)->index;
  1007. switch ((data->pwm_enable >> 2 * nr) & 3) {
  1008. case 0:
  1009. case 1:
  1010. result = 2; /* Normal auto mode */
  1011. break;
  1012. case 2:
  1013. result = 1; /* Manual mode */
  1014. break;
  1015. case 3:
  1016. if (data->type == f8000)
  1017. result = 3; /* Thermostat mode */
  1018. else
  1019. result = 1; /* Manual mode */
  1020. break;
  1021. }
  1022. return sprintf(buf, "%d\n", result);
  1023. }
  1024. static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
  1025. *devattr, const char *buf, size_t count)
  1026. {
  1027. struct f71882fg_data *data = dev_get_drvdata(dev);
  1028. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  1029. long val;
  1030. err = kstrtol(buf, 10, &val);
  1031. if (err)
  1032. return err;
  1033. /* Special case for F8000 pwm channel 3 which only does auto mode */
  1034. if (data->type == f8000 && nr == 2 && val != 2)
  1035. return -EINVAL;
  1036. mutex_lock(&data->update_lock);
  1037. data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
  1038. /* Special case for F8000 auto PWM mode / Thermostat mode */
  1039. if (data->type == f8000 && ((data->pwm_enable >> 2 * nr) & 1)) {
  1040. switch (val) {
  1041. case 2:
  1042. data->pwm_enable &= ~(2 << (2 * nr));
  1043. break; /* Normal auto mode */
  1044. case 3:
  1045. data->pwm_enable |= 2 << (2 * nr);
  1046. break; /* Thermostat mode */
  1047. default:
  1048. count = -EINVAL;
  1049. goto leave;
  1050. }
  1051. } else {
  1052. switch (val) {
  1053. case 1:
  1054. /* The f71858fg does not support manual RPM mode */
  1055. if (data->type == f71858fg &&
  1056. ((data->pwm_enable >> (2 * nr)) & 1)) {
  1057. count = -EINVAL;
  1058. goto leave;
  1059. }
  1060. data->pwm_enable |= 2 << (2 * nr);
  1061. break; /* Manual */
  1062. case 2:
  1063. data->pwm_enable &= ~(2 << (2 * nr));
  1064. break; /* Normal auto mode */
  1065. default:
  1066. count = -EINVAL;
  1067. goto leave;
  1068. }
  1069. }
  1070. f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
  1071. leave:
  1072. mutex_unlock(&data->update_lock);
  1073. return count;
  1074. }
  1075. static ssize_t show_pwm_interpolate(struct device *dev,
  1076. struct device_attribute *devattr, char *buf)
  1077. {
  1078. int result;
  1079. struct f71882fg_data *data = f71882fg_update_device(dev);
  1080. int nr = to_sensor_dev_attr_2(devattr)->index;
  1081. result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
  1082. return sprintf(buf, "%d\n", result);
  1083. }
  1084. static ssize_t store_pwm_interpolate(struct device *dev,
  1085. struct device_attribute *devattr,
  1086. const char *buf, size_t count)
  1087. {
  1088. struct f71882fg_data *data = dev_get_drvdata(dev);
  1089. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  1090. unsigned long val;
  1091. err = kstrtoul(buf, 10, &val);
  1092. if (err)
  1093. return err;
  1094. mutex_lock(&data->update_lock);
  1095. data->pwm_auto_point_mapping[nr] =
  1096. f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
  1097. if (val)
  1098. val = data->pwm_auto_point_mapping[nr] | (1 << 4);
  1099. else
  1100. val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
  1101. f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
  1102. data->pwm_auto_point_mapping[nr] = val;
  1103. mutex_unlock(&data->update_lock);
  1104. return count;
  1105. }
  1106. /* Fan / PWM attr common to all models */
  1107. static struct sensor_device_attribute_2 fxxxx_fan_attr[4][6] = { {
  1108. SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
  1109. SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
  1110. show_fan_full_speed,
  1111. store_fan_full_speed, 0, 0),
  1112. SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
  1113. SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
  1114. SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  1115. store_pwm_enable, 0, 0),
  1116. SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
  1117. show_pwm_interpolate, store_pwm_interpolate, 0, 0),
  1118. }, {
  1119. SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
  1120. SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
  1121. show_fan_full_speed,
  1122. store_fan_full_speed, 0, 1),
  1123. SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
  1124. SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
  1125. SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  1126. store_pwm_enable, 0, 1),
  1127. SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
  1128. show_pwm_interpolate, store_pwm_interpolate, 0, 1),
  1129. }, {
  1130. SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
  1131. SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
  1132. show_fan_full_speed,
  1133. store_fan_full_speed, 0, 2),
  1134. SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
  1135. SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
  1136. SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  1137. store_pwm_enable, 0, 2),
  1138. SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
  1139. show_pwm_interpolate, store_pwm_interpolate, 0, 2),
  1140. }, {
  1141. SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
  1142. SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
  1143. show_fan_full_speed,
  1144. store_fan_full_speed, 0, 3),
  1145. SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
  1146. SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
  1147. SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  1148. store_pwm_enable, 0, 3),
  1149. SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
  1150. show_pwm_interpolate, store_pwm_interpolate, 0, 3),
  1151. } };
  1152. static ssize_t show_simple_pwm(struct device *dev,
  1153. struct device_attribute *devattr, char *buf)
  1154. {
  1155. struct f71882fg_data *data = f71882fg_update_device(dev);
  1156. int val, nr = to_sensor_dev_attr_2(devattr)->index;
  1157. val = data->pwm[nr];
  1158. return sprintf(buf, "%d\n", val);
  1159. }
  1160. static ssize_t store_simple_pwm(struct device *dev,
  1161. struct device_attribute *devattr,
  1162. const char *buf, size_t count)
  1163. {
  1164. struct f71882fg_data *data = dev_get_drvdata(dev);
  1165. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  1166. long val;
  1167. err = kstrtol(buf, 10, &val);
  1168. if (err)
  1169. return err;
  1170. val = clamp_val(val, 0, 255);
  1171. mutex_lock(&data->update_lock);
  1172. f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
  1173. data->pwm[nr] = val;
  1174. mutex_unlock(&data->update_lock);
  1175. return count;
  1176. }
  1177. /* Attr for the third fan of the f71808a, which only has manual pwm */
  1178. static struct sensor_device_attribute_2 f71808a_fan3_attr[] = {
  1179. SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
  1180. SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
  1181. SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR,
  1182. show_simple_pwm, store_simple_pwm, 0, 2),
  1183. };
  1184. static ssize_t show_fan_beep(struct device *dev, struct device_attribute
  1185. *devattr, char *buf)
  1186. {
  1187. struct f71882fg_data *data = f71882fg_update_device(dev);
  1188. int nr = to_sensor_dev_attr_2(devattr)->index;
  1189. if (data->fan_beep & (1 << nr))
  1190. return sprintf(buf, "1\n");
  1191. else
  1192. return sprintf(buf, "0\n");
  1193. }
  1194. static ssize_t store_fan_beep(struct device *dev, struct device_attribute
  1195. *devattr, const char *buf, size_t count)
  1196. {
  1197. struct f71882fg_data *data = dev_get_drvdata(dev);
  1198. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  1199. unsigned long val;
  1200. err = kstrtoul(buf, 10, &val);
  1201. if (err)
  1202. return err;
  1203. mutex_lock(&data->update_lock);
  1204. data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
  1205. if (val)
  1206. data->fan_beep |= 1 << nr;
  1207. else
  1208. data->fan_beep &= ~(1 << nr);
  1209. f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
  1210. mutex_unlock(&data->update_lock);
  1211. return count;
  1212. }
  1213. /* Attr for models which can beep on Fan alarm */
  1214. static struct sensor_device_attribute_2 fxxxx_fan_beep_attr[] = {
  1215. SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  1216. store_fan_beep, 0, 0),
  1217. SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  1218. store_fan_beep, 0, 1),
  1219. SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  1220. store_fan_beep, 0, 2),
  1221. SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  1222. store_fan_beep, 0, 3),
  1223. };
  1224. static ssize_t show_pwm_auto_point_channel(struct device *dev,
  1225. struct device_attribute *devattr,
  1226. char *buf)
  1227. {
  1228. int result;
  1229. struct f71882fg_data *data = f71882fg_update_device(dev);
  1230. int nr = to_sensor_dev_attr_2(devattr)->index;
  1231. result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) -
  1232. data->temp_start);
  1233. return sprintf(buf, "%d\n", result);
  1234. }
  1235. static ssize_t store_pwm_auto_point_channel(struct device *dev,
  1236. struct device_attribute *devattr,
  1237. const char *buf, size_t count)
  1238. {
  1239. struct f71882fg_data *data = dev_get_drvdata(dev);
  1240. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  1241. long val;
  1242. err = kstrtol(buf, 10, &val);
  1243. if (err)
  1244. return err;
  1245. switch (val) {
  1246. case 1:
  1247. val = 0;
  1248. break;
  1249. case 2:
  1250. val = 1;
  1251. break;
  1252. case 4:
  1253. val = 2;
  1254. break;
  1255. default:
  1256. return -EINVAL;
  1257. }
  1258. val += data->temp_start;
  1259. mutex_lock(&data->update_lock);
  1260. data->pwm_auto_point_mapping[nr] =
  1261. f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(nr));
  1262. val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
  1263. f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
  1264. data->pwm_auto_point_mapping[nr] = val;
  1265. mutex_unlock(&data->update_lock);
  1266. return count;
  1267. }
  1268. static ssize_t show_pwm_auto_point_pwm(struct device *dev,
  1269. struct device_attribute *devattr,
  1270. char *buf)
  1271. {
  1272. int result;
  1273. struct f71882fg_data *data = f71882fg_update_device(dev);
  1274. int pwm = to_sensor_dev_attr_2(devattr)->index;
  1275. int point = to_sensor_dev_attr_2(devattr)->nr;
  1276. mutex_lock(&data->update_lock);
  1277. if (data->pwm_enable & (1 << (2 * pwm))) {
  1278. /* PWM mode */
  1279. result = data->pwm_auto_point_pwm[pwm][point];
  1280. } else {
  1281. /* RPM mode */
  1282. result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
  1283. }
  1284. mutex_unlock(&data->update_lock);
  1285. return sprintf(buf, "%d\n", result);
  1286. }
  1287. static ssize_t store_pwm_auto_point_pwm(struct device *dev,
  1288. struct device_attribute *devattr,
  1289. const char *buf, size_t count)
  1290. {
  1291. struct f71882fg_data *data = dev_get_drvdata(dev);
  1292. int err, pwm = to_sensor_dev_attr_2(devattr)->index;
  1293. int point = to_sensor_dev_attr_2(devattr)->nr;
  1294. long val;
  1295. err = kstrtol(buf, 10, &val);
  1296. if (err)
  1297. return err;
  1298. val = clamp_val(val, 0, 255);
  1299. mutex_lock(&data->update_lock);
  1300. data->pwm_enable = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
  1301. if (data->pwm_enable & (1 << (2 * pwm))) {
  1302. /* PWM mode */
  1303. } else {
  1304. /* RPM mode */
  1305. if (val < 29) /* Prevent negative numbers */
  1306. val = 255;
  1307. else
  1308. val = (255 - val) * 32 / val;
  1309. }
  1310. f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
  1311. data->pwm_auto_point_pwm[pwm][point] = val;
  1312. mutex_unlock(&data->update_lock);
  1313. return count;
  1314. }
  1315. static ssize_t show_pwm_auto_point_temp(struct device *dev,
  1316. struct device_attribute *devattr,
  1317. char *buf)
  1318. {
  1319. int result;
  1320. struct f71882fg_data *data = f71882fg_update_device(dev);
  1321. int pwm = to_sensor_dev_attr_2(devattr)->index;
  1322. int point = to_sensor_dev_attr_2(devattr)->nr;
  1323. result = data->pwm_auto_point_temp[pwm][point];
  1324. return sprintf(buf, "%d\n", 1000 * result);
  1325. }
  1326. static ssize_t store_pwm_auto_point_temp(struct device *dev,
  1327. struct device_attribute *devattr,
  1328. const char *buf, size_t count)
  1329. {
  1330. struct f71882fg_data *data = dev_get_drvdata(dev);
  1331. int err, pwm = to_sensor_dev_attr_2(devattr)->index;
  1332. int point = to_sensor_dev_attr_2(devattr)->nr;
  1333. long val;
  1334. err = kstrtol(buf, 10, &val);
  1335. if (err)
  1336. return err;
  1337. val /= 1000;
  1338. if (data->auto_point_temp_signed)
  1339. val = clamp_val(val, -128, 127);
  1340. else
  1341. val = clamp_val(val, 0, 127);
  1342. mutex_lock(&data->update_lock);
  1343. f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
  1344. data->pwm_auto_point_temp[pwm][point] = val;
  1345. mutex_unlock(&data->update_lock);
  1346. return count;
  1347. }
  1348. static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
  1349. struct device_attribute *devattr,
  1350. char *buf)
  1351. {
  1352. int result = 0;
  1353. struct f71882fg_data *data = f71882fg_update_device(dev);
  1354. int nr = to_sensor_dev_attr_2(devattr)->index;
  1355. int point = to_sensor_dev_attr_2(devattr)->nr;
  1356. mutex_lock(&data->update_lock);
  1357. if (nr & 1)
  1358. result = data->pwm_auto_point_hyst[nr / 2] >> 4;
  1359. else
  1360. result = data->pwm_auto_point_hyst[nr / 2] & 0x0f;
  1361. result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
  1362. mutex_unlock(&data->update_lock);
  1363. return sprintf(buf, "%d\n", result);
  1364. }
  1365. static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
  1366. struct device_attribute *devattr,
  1367. const char *buf, size_t count)
  1368. {
  1369. struct f71882fg_data *data = dev_get_drvdata(dev);
  1370. int err, nr = to_sensor_dev_attr_2(devattr)->index;
  1371. int point = to_sensor_dev_attr_2(devattr)->nr;
  1372. u8 reg;
  1373. long val;
  1374. err = kstrtol(buf, 10, &val);
  1375. if (err)
  1376. return err;
  1377. val /= 1000;
  1378. mutex_lock(&data->update_lock);
  1379. data->pwm_auto_point_temp[nr][point] =
  1380. f71882fg_read8(data, F71882FG_REG_POINT_TEMP(nr, point));
  1381. val = clamp_val(val, data->pwm_auto_point_temp[nr][point] - 15,
  1382. data->pwm_auto_point_temp[nr][point]);
  1383. val = data->pwm_auto_point_temp[nr][point] - val;
  1384. reg = f71882fg_read8(data, F71882FG_REG_FAN_HYST(nr / 2));
  1385. if (nr & 1)
  1386. reg = (reg & 0x0f) | (val << 4);
  1387. else
  1388. reg = (reg & 0xf0) | val;
  1389. f71882fg_write8(data, F71882FG_REG_FAN_HYST(nr / 2), reg);
  1390. data->pwm_auto_point_hyst[nr / 2] = reg;
  1391. mutex_unlock(&data->update_lock);
  1392. return count;
  1393. }
  1394. /*
  1395. * PWM attr for the f71862fg, fewer pwms and fewer zones per pwm than the
  1396. * standard models
  1397. */
  1398. static struct sensor_device_attribute_2 f71862fg_auto_pwm_attr[3][7] = { {
  1399. SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
  1400. show_pwm_auto_point_channel,
  1401. store_pwm_auto_point_channel, 0, 0),
  1402. SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1403. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1404. 1, 0),
  1405. SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1406. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1407. 4, 0),
  1408. SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
  1409. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1410. 0, 0),
  1411. SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
  1412. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1413. 3, 0),
  1414. SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1415. show_pwm_auto_point_temp_hyst,
  1416. store_pwm_auto_point_temp_hyst,
  1417. 0, 0),
  1418. SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
  1419. show_pwm_auto_point_temp_hyst, NULL, 3, 0),
  1420. }, {
  1421. SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
  1422. show_pwm_auto_point_channel,
  1423. store_pwm_auto_point_channel, 0, 1),
  1424. SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1425. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1426. 1, 1),
  1427. SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1428. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1429. 4, 1),
  1430. SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
  1431. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1432. 0, 1),
  1433. SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
  1434. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1435. 3, 1),
  1436. SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1437. show_pwm_auto_point_temp_hyst,
  1438. store_pwm_auto_point_temp_hyst,
  1439. 0, 1),
  1440. SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
  1441. show_pwm_auto_point_temp_hyst, NULL, 3, 1),
  1442. }, {
  1443. SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
  1444. show_pwm_auto_point_channel,
  1445. store_pwm_auto_point_channel, 0, 2),
  1446. SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1447. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1448. 1, 2),
  1449. SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1450. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1451. 4, 2),
  1452. SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
  1453. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1454. 0, 2),
  1455. SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
  1456. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1457. 3, 2),
  1458. SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1459. show_pwm_auto_point_temp_hyst,
  1460. store_pwm_auto_point_temp_hyst,
  1461. 0, 2),
  1462. SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
  1463. show_pwm_auto_point_temp_hyst, NULL, 3, 2),
  1464. } };
  1465. /*
  1466. * PWM attr for the f71808e/f71869, almost identical to the f71862fg, but the
  1467. * pwm setting when the temperature is above the pwmX_auto_point1_temp can be
  1468. * programmed instead of being hardcoded to 0xff
  1469. */
  1470. static struct sensor_device_attribute_2 f71869_auto_pwm_attr[3][8] = { {
  1471. SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
  1472. show_pwm_auto_point_channel,
  1473. store_pwm_auto_point_channel, 0, 0),
  1474. SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1475. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1476. 0, 0),
  1477. SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1478. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1479. 1, 0),
  1480. SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1481. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1482. 4, 0),
  1483. SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
  1484. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1485. 0, 0),
  1486. SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
  1487. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1488. 3, 0),
  1489. SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1490. show_pwm_auto_point_temp_hyst,
  1491. store_pwm_auto_point_temp_hyst,
  1492. 0, 0),
  1493. SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
  1494. show_pwm_auto_point_temp_hyst, NULL, 3, 0),
  1495. }, {
  1496. SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
  1497. show_pwm_auto_point_channel,
  1498. store_pwm_auto_point_channel, 0, 1),
  1499. SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1500. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1501. 0, 1),
  1502. SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1503. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1504. 1, 1),
  1505. SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1506. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1507. 4, 1),
  1508. SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
  1509. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1510. 0, 1),
  1511. SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
  1512. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1513. 3, 1),
  1514. SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1515. show_pwm_auto_point_temp_hyst,
  1516. store_pwm_auto_point_temp_hyst,
  1517. 0, 1),
  1518. SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
  1519. show_pwm_auto_point_temp_hyst, NULL, 3, 1),
  1520. }, {
  1521. SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
  1522. show_pwm_auto_point_channel,
  1523. store_pwm_auto_point_channel, 0, 2),
  1524. SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1525. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1526. 0, 2),
  1527. SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1528. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1529. 1, 2),
  1530. SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1531. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1532. 4, 2),
  1533. SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
  1534. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1535. 0, 2),
  1536. SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
  1537. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1538. 3, 2),
  1539. SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1540. show_pwm_auto_point_temp_hyst,
  1541. store_pwm_auto_point_temp_hyst,
  1542. 0, 2),
  1543. SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
  1544. show_pwm_auto_point_temp_hyst, NULL, 3, 2),
  1545. } };
  1546. /* PWM attr for the standard models */
  1547. static struct sensor_device_attribute_2 fxxxx_auto_pwm_attr[4][14] = { {
  1548. SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
  1549. show_pwm_auto_point_channel,
  1550. store_pwm_auto_point_channel, 0, 0),
  1551. SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1552. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1553. 0, 0),
  1554. SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1555. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1556. 1, 0),
  1557. SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1558. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1559. 2, 0),
  1560. SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1561. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1562. 3, 0),
  1563. SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1564. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1565. 4, 0),
  1566. SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
  1567. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1568. 0, 0),
  1569. SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
  1570. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1571. 1, 0),
  1572. SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
  1573. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1574. 2, 0),
  1575. SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
  1576. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1577. 3, 0),
  1578. SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1579. show_pwm_auto_point_temp_hyst,
  1580. store_pwm_auto_point_temp_hyst,
  1581. 0, 0),
  1582. SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
  1583. show_pwm_auto_point_temp_hyst, NULL, 1, 0),
  1584. SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
  1585. show_pwm_auto_point_temp_hyst, NULL, 2, 0),
  1586. SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
  1587. show_pwm_auto_point_temp_hyst, NULL, 3, 0),
  1588. }, {
  1589. SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
  1590. show_pwm_auto_point_channel,
  1591. store_pwm_auto_point_channel, 0, 1),
  1592. SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1593. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1594. 0, 1),
  1595. SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1596. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1597. 1, 1),
  1598. SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1599. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1600. 2, 1),
  1601. SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1602. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1603. 3, 1),
  1604. SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1605. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1606. 4, 1),
  1607. SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
  1608. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1609. 0, 1),
  1610. SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
  1611. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1612. 1, 1),
  1613. SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
  1614. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1615. 2, 1),
  1616. SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
  1617. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1618. 3, 1),
  1619. SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1620. show_pwm_auto_point_temp_hyst,
  1621. store_pwm_auto_point_temp_hyst,
  1622. 0, 1),
  1623. SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
  1624. show_pwm_auto_point_temp_hyst, NULL, 1, 1),
  1625. SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
  1626. show_pwm_auto_point_temp_hyst, NULL, 2, 1),
  1627. SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
  1628. show_pwm_auto_point_temp_hyst, NULL, 3, 1),
  1629. }, {
  1630. SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
  1631. show_pwm_auto_point_channel,
  1632. store_pwm_auto_point_channel, 0, 2),
  1633. SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1634. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1635. 0, 2),
  1636. SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1637. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1638. 1, 2),
  1639. SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1640. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1641. 2, 2),
  1642. SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1643. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1644. 3, 2),
  1645. SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1646. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1647. 4, 2),
  1648. SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
  1649. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1650. 0, 2),
  1651. SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
  1652. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1653. 1, 2),
  1654. SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
  1655. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1656. 2, 2),
  1657. SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
  1658. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1659. 3, 2),
  1660. SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1661. show_pwm_auto_point_temp_hyst,
  1662. store_pwm_auto_point_temp_hyst,
  1663. 0, 2),
  1664. SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
  1665. show_pwm_auto_point_temp_hyst, NULL, 1, 2),
  1666. SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
  1667. show_pwm_auto_point_temp_hyst, NULL, 2, 2),
  1668. SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
  1669. show_pwm_auto_point_temp_hyst, NULL, 3, 2),
  1670. }, {
  1671. SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
  1672. show_pwm_auto_point_channel,
  1673. store_pwm_auto_point_channel, 0, 3),
  1674. SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1675. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1676. 0, 3),
  1677. SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1678. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1679. 1, 3),
  1680. SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1681. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1682. 2, 3),
  1683. SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1684. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1685. 3, 3),
  1686. SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1687. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1688. 4, 3),
  1689. SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
  1690. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1691. 0, 3),
  1692. SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
  1693. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1694. 1, 3),
  1695. SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
  1696. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1697. 2, 3),
  1698. SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
  1699. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1700. 3, 3),
  1701. SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1702. show_pwm_auto_point_temp_hyst,
  1703. store_pwm_auto_point_temp_hyst,
  1704. 0, 3),
  1705. SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
  1706. show_pwm_auto_point_temp_hyst, NULL, 1, 3),
  1707. SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
  1708. show_pwm_auto_point_temp_hyst, NULL, 2, 3),
  1709. SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
  1710. show_pwm_auto_point_temp_hyst, NULL, 3, 3),
  1711. } };
  1712. /* Fan attr specific to the f8000 (4th fan input can only measure speed) */
  1713. static struct sensor_device_attribute_2 f8000_fan_attr[] = {
  1714. SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
  1715. };
  1716. /*
  1717. * PWM attr for the f8000, zones mapped to temp instead of to pwm!
  1718. * Also the register block at offset A0 maps to TEMP1 (so our temp2, as the
  1719. * F8000 starts counting temps at 0), B0 maps the TEMP2 and C0 maps to TEMP0
  1720. */
  1721. static struct sensor_device_attribute_2 f8000_auto_pwm_attr[3][14] = { {
  1722. SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
  1723. show_pwm_auto_point_channel,
  1724. store_pwm_auto_point_channel, 0, 0),
  1725. SENSOR_ATTR_2(temp1_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1726. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1727. 0, 2),
  1728. SENSOR_ATTR_2(temp1_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1729. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1730. 1, 2),
  1731. SENSOR_ATTR_2(temp1_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1732. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1733. 2, 2),
  1734. SENSOR_ATTR_2(temp1_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1735. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1736. 3, 2),
  1737. SENSOR_ATTR_2(temp1_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1738. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1739. 4, 2),
  1740. SENSOR_ATTR_2(temp1_auto_point1_temp, S_IRUGO|S_IWUSR,
  1741. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1742. 0, 2),
  1743. SENSOR_ATTR_2(temp1_auto_point2_temp, S_IRUGO|S_IWUSR,
  1744. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1745. 1, 2),
  1746. SENSOR_ATTR_2(temp1_auto_point3_temp, S_IRUGO|S_IWUSR,
  1747. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1748. 2, 2),
  1749. SENSOR_ATTR_2(temp1_auto_point4_temp, S_IRUGO|S_IWUSR,
  1750. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1751. 3, 2),
  1752. SENSOR_ATTR_2(temp1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1753. show_pwm_auto_point_temp_hyst,
  1754. store_pwm_auto_point_temp_hyst,
  1755. 0, 2),
  1756. SENSOR_ATTR_2(temp1_auto_point2_temp_hyst, S_IRUGO,
  1757. show_pwm_auto_point_temp_hyst, NULL, 1, 2),
  1758. SENSOR_ATTR_2(temp1_auto_point3_temp_hyst, S_IRUGO,
  1759. show_pwm_auto_point_temp_hyst, NULL, 2, 2),
  1760. SENSOR_ATTR_2(temp1_auto_point4_temp_hyst, S_IRUGO,
  1761. show_pwm_auto_point_temp_hyst, NULL, 3, 2),
  1762. }, {
  1763. SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
  1764. show_pwm_auto_point_channel,
  1765. store_pwm_auto_point_channel, 0, 1),
  1766. SENSOR_ATTR_2(temp2_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1767. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1768. 0, 0),
  1769. SENSOR_ATTR_2(temp2_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1770. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1771. 1, 0),
  1772. SENSOR_ATTR_2(temp2_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1773. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1774. 2, 0),
  1775. SENSOR_ATTR_2(temp2_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1776. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1777. 3, 0),
  1778. SENSOR_ATTR_2(temp2_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1779. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1780. 4, 0),
  1781. SENSOR_ATTR_2(temp2_auto_point1_temp, S_IRUGO|S_IWUSR,
  1782. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1783. 0, 0),
  1784. SENSOR_ATTR_2(temp2_auto_point2_temp, S_IRUGO|S_IWUSR,
  1785. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1786. 1, 0),
  1787. SENSOR_ATTR_2(temp2_auto_point3_temp, S_IRUGO|S_IWUSR,
  1788. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1789. 2, 0),
  1790. SENSOR_ATTR_2(temp2_auto_point4_temp, S_IRUGO|S_IWUSR,
  1791. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1792. 3, 0),
  1793. SENSOR_ATTR_2(temp2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1794. show_pwm_auto_point_temp_hyst,
  1795. store_pwm_auto_point_temp_hyst,
  1796. 0, 0),
  1797. SENSOR_ATTR_2(temp2_auto_point2_temp_hyst, S_IRUGO,
  1798. show_pwm_auto_point_temp_hyst, NULL, 1, 0),
  1799. SENSOR_ATTR_2(temp2_auto_point3_temp_hyst, S_IRUGO,
  1800. show_pwm_auto_point_temp_hyst, NULL, 2, 0),
  1801. SENSOR_ATTR_2(temp2_auto_point4_temp_hyst, S_IRUGO,
  1802. show_pwm_auto_point_temp_hyst, NULL, 3, 0),
  1803. }, {
  1804. SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
  1805. show_pwm_auto_point_channel,
  1806. store_pwm_auto_point_channel, 0, 2),
  1807. SENSOR_ATTR_2(temp3_auto_point1_pwm, S_IRUGO|S_IWUSR,
  1808. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1809. 0, 1),
  1810. SENSOR_ATTR_2(temp3_auto_point2_pwm, S_IRUGO|S_IWUSR,
  1811. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1812. 1, 1),
  1813. SENSOR_ATTR_2(temp3_auto_point3_pwm, S_IRUGO|S_IWUSR,
  1814. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1815. 2, 1),
  1816. SENSOR_ATTR_2(temp3_auto_point4_pwm, S_IRUGO|S_IWUSR,
  1817. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1818. 3, 1),
  1819. SENSOR_ATTR_2(temp3_auto_point5_pwm, S_IRUGO|S_IWUSR,
  1820. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  1821. 4, 1),
  1822. SENSOR_ATTR_2(temp3_auto_point1_temp, S_IRUGO|S_IWUSR,
  1823. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1824. 0, 1),
  1825. SENSOR_ATTR_2(temp3_auto_point2_temp, S_IRUGO|S_IWUSR,
  1826. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1827. 1, 1),
  1828. SENSOR_ATTR_2(temp3_auto_point3_temp, S_IRUGO|S_IWUSR,
  1829. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1830. 2, 1),
  1831. SENSOR_ATTR_2(temp3_auto_point4_temp, S_IRUGO|S_IWUSR,
  1832. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  1833. 3, 1),
  1834. SENSOR_ATTR_2(temp3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  1835. show_pwm_auto_point_temp_hyst,
  1836. store_pwm_auto_point_temp_hyst,
  1837. 0, 1),
  1838. SENSOR_ATTR_2(temp3_auto_point2_temp_hyst, S_IRUGO,
  1839. show_pwm_auto_point_temp_hyst, NULL, 1, 1),
  1840. SENSOR_ATTR_2(temp3_auto_point3_temp_hyst, S_IRUGO,
  1841. show_pwm_auto_point_temp_hyst, NULL, 2, 1),
  1842. SENSOR_ATTR_2(temp3_auto_point4_temp_hyst, S_IRUGO,
  1843. show_pwm_auto_point_temp_hyst, NULL, 3, 1),
  1844. } };
  1845. /* Super I/O functions */
  1846. static inline int superio_inb(int base, int reg)
  1847. {
  1848. outb(reg, base);
  1849. return inb(base + 1);
  1850. }
  1851. static int superio_inw(int base, int reg)
  1852. {
  1853. int val;
  1854. val = superio_inb(base, reg) << 8;
  1855. val |= superio_inb(base, reg + 1);
  1856. return val;
  1857. }
  1858. static inline int superio_enter(int base)
  1859. {
  1860. /* Don't step on other drivers' I/O space by accident */
  1861. if (!request_muxed_region(base, 2, DRVNAME)) {
  1862. pr_err("I/O address 0x%04x already in use\n", base);
  1863. return -EBUSY;
  1864. }
  1865. /* according to the datasheet the key must be send twice! */
  1866. outb(SIO_UNLOCK_KEY, base);
  1867. outb(SIO_UNLOCK_KEY, base);
  1868. return 0;
  1869. }
  1870. static inline void superio_select(int base, int ld)
  1871. {
  1872. outb(SIO_REG_LDSEL, base);
  1873. outb(ld, base + 1);
  1874. }
  1875. static inline void superio_exit(int base)
  1876. {
  1877. outb(SIO_LOCK_KEY, base);
  1878. release_region(base, 2);
  1879. }
  1880. static int f71882fg_create_sysfs_files(struct platform_device *pdev,
  1881. struct sensor_device_attribute_2 *attr, int count)
  1882. {
  1883. int err, i;
  1884. for (i = 0; i < count; i++) {
  1885. err = device_create_file(&pdev->dev, &attr[i].dev_attr);
  1886. if (err)
  1887. return err;
  1888. }
  1889. return 0;
  1890. }
  1891. static void f71882fg_remove_sysfs_files(struct platform_device *pdev,
  1892. struct sensor_device_attribute_2 *attr, int count)
  1893. {
  1894. int i;
  1895. for (i = 0; i < count; i++)
  1896. device_remove_file(&pdev->dev, &attr[i].dev_attr);
  1897. }
  1898. static int f71882fg_create_fan_sysfs_files(
  1899. struct platform_device *pdev, int idx)
  1900. {
  1901. struct f71882fg_data *data = platform_get_drvdata(pdev);
  1902. int err;
  1903. /* Sanity check the pwm setting */
  1904. err = 0;
  1905. switch (data->type) {
  1906. case f71858fg:
  1907. if (((data->pwm_enable >> (idx * 2)) & 3) == 3)
  1908. err = 1;
  1909. break;
  1910. case f71862fg:
  1911. if (((data->pwm_enable >> (idx * 2)) & 1) != 1)
  1912. err = 1;
  1913. break;
  1914. case f8000:
  1915. if (idx == 2)
  1916. err = data->pwm_enable & 0x20;
  1917. break;
  1918. default:
  1919. break;
  1920. }
  1921. if (err) {
  1922. dev_err(&pdev->dev,
  1923. "Invalid (reserved) pwm settings: 0x%02x, "
  1924. "skipping fan %d\n",
  1925. (data->pwm_enable >> (idx * 2)) & 3, idx + 1);
  1926. return 0; /* This is a non fatal condition */
  1927. }
  1928. err = f71882fg_create_sysfs_files(pdev, &fxxxx_fan_attr[idx][0],
  1929. ARRAY_SIZE(fxxxx_fan_attr[0]));
  1930. if (err)
  1931. return err;
  1932. if (f71882fg_fan_has_beep[data->type]) {
  1933. err = f71882fg_create_sysfs_files(pdev,
  1934. &fxxxx_fan_beep_attr[idx],
  1935. 1);
  1936. if (err)
  1937. return err;
  1938. }
  1939. dev_info(&pdev->dev, "Fan: %d is in %s mode\n", idx + 1,
  1940. (data->pwm_enable & (1 << (2 * idx))) ? "duty-cycle" : "RPM");
  1941. /* Check for unsupported auto pwm settings */
  1942. switch (data->type) {
  1943. case f71808e:
  1944. case f71808a:
  1945. case f71869:
  1946. case f71869a:
  1947. case f71889fg:
  1948. case f71889ed:
  1949. case f71889a:
  1950. data->pwm_auto_point_mapping[idx] =
  1951. f71882fg_read8(data, F71882FG_REG_POINT_MAPPING(idx));
  1952. if ((data->pwm_auto_point_mapping[idx] & 0x80) ||
  1953. (data->pwm_auto_point_mapping[idx] & 3) == 0) {
  1954. dev_warn(&pdev->dev,
  1955. "Auto pwm controlled by raw digital "
  1956. "data, disabling pwm auto_point "
  1957. "sysfs attributes for fan %d\n", idx + 1);
  1958. return 0; /* This is a non fatal condition */
  1959. }
  1960. break;
  1961. default:
  1962. break;
  1963. }
  1964. switch (data->type) {
  1965. case f71862fg:
  1966. err = f71882fg_create_sysfs_files(pdev,
  1967. &f71862fg_auto_pwm_attr[idx][0],
  1968. ARRAY_SIZE(f71862fg_auto_pwm_attr[0]));
  1969. break;
  1970. case f71808e:
  1971. case f71869:
  1972. err = f71882fg_create_sysfs_files(pdev,
  1973. &f71869_auto_pwm_attr[idx][0],
  1974. ARRAY_SIZE(f71869_auto_pwm_attr[0]));
  1975. break;
  1976. case f8000:
  1977. err = f71882fg_create_sysfs_files(pdev,
  1978. &f8000_auto_pwm_attr[idx][0],
  1979. ARRAY_SIZE(f8000_auto_pwm_attr[0]));
  1980. break;
  1981. default:
  1982. err = f71882fg_create_sysfs_files(pdev,
  1983. &fxxxx_auto_pwm_attr[idx][0],
  1984. ARRAY_SIZE(fxxxx_auto_pwm_attr[0]));
  1985. }
  1986. return err;
  1987. }
  1988. static void f71882fg_remove(struct platform_device *pdev)
  1989. {
  1990. struct f71882fg_data *data = platform_get_drvdata(pdev);
  1991. int nr_fans = f71882fg_nr_fans[data->type];
  1992. int nr_temps = f71882fg_nr_temps[data->type];
  1993. int i;
  1994. u8 start_reg = f71882fg_read8(data, F71882FG_REG_START);
  1995. if (data->hwmon_dev)
  1996. hwmon_device_unregister(data->hwmon_dev);
  1997. device_remove_file(&pdev->dev, &dev_attr_name);
  1998. if (start_reg & 0x01) {
  1999. switch (data->type) {
  2000. case f71858fg:
  2001. if (data->temp_config & 0x10)
  2002. f71882fg_remove_sysfs_files(pdev,
  2003. f8000_temp_attr,
  2004. ARRAY_SIZE(f8000_temp_attr));
  2005. else
  2006. f71882fg_remove_sysfs_files(pdev,
  2007. f71858fg_temp_attr,
  2008. ARRAY_SIZE(f71858fg_temp_attr));
  2009. break;
  2010. case f8000:
  2011. f71882fg_remove_sysfs_files(pdev,
  2012. f8000_temp_attr,
  2013. ARRAY_SIZE(f8000_temp_attr));
  2014. break;
  2015. case f81866a:
  2016. f71882fg_remove_sysfs_files(pdev,
  2017. f71858fg_temp_attr,
  2018. ARRAY_SIZE(f71858fg_temp_attr));
  2019. break;
  2020. default:
  2021. f71882fg_remove_sysfs_files(pdev,
  2022. &fxxxx_temp_attr[0][0],
  2023. ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
  2024. }
  2025. if (f71882fg_temp_has_beep[data->type]) {
  2026. if (data->type == f81866a)
  2027. f71882fg_remove_sysfs_files(pdev,
  2028. &f81866_temp_beep_attr[0][0],
  2029. ARRAY_SIZE(f81866_temp_beep_attr[0])
  2030. * nr_temps);
  2031. else
  2032. f71882fg_remove_sysfs_files(pdev,
  2033. &fxxxx_temp_beep_attr[0][0],
  2034. ARRAY_SIZE(fxxxx_temp_beep_attr[0])
  2035. * nr_temps);
  2036. }
  2037. for (i = 0; i < F71882FG_MAX_INS; i++) {
  2038. if (f71882fg_has_in[data->type][i]) {
  2039. device_remove_file(&pdev->dev,
  2040. &fxxxx_in_attr[i].dev_attr);
  2041. }
  2042. }
  2043. if (f71882fg_has_in1_alarm[data->type]) {
  2044. f71882fg_remove_sysfs_files(pdev,
  2045. fxxxx_in1_alarm_attr,
  2046. ARRAY_SIZE(fxxxx_in1_alarm_attr));
  2047. }
  2048. }
  2049. if (start_reg & 0x02) {
  2050. f71882fg_remove_sysfs_files(pdev, &fxxxx_fan_attr[0][0],
  2051. ARRAY_SIZE(fxxxx_fan_attr[0]) * nr_fans);
  2052. if (f71882fg_fan_has_beep[data->type]) {
  2053. f71882fg_remove_sysfs_files(pdev,
  2054. fxxxx_fan_beep_attr, nr_fans);
  2055. }
  2056. switch (data->type) {
  2057. case f71808a:
  2058. f71882fg_remove_sysfs_files(pdev,
  2059. &fxxxx_auto_pwm_attr[0][0],
  2060. ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
  2061. f71882fg_remove_sysfs_files(pdev,
  2062. f71808a_fan3_attr,
  2063. ARRAY_SIZE(f71808a_fan3_attr));
  2064. break;
  2065. case f71862fg:
  2066. f71882fg_remove_sysfs_files(pdev,
  2067. &f71862fg_auto_pwm_attr[0][0],
  2068. ARRAY_SIZE(f71862fg_auto_pwm_attr[0]) *
  2069. nr_fans);
  2070. break;
  2071. case f71808e:
  2072. case f71869:
  2073. f71882fg_remove_sysfs_files(pdev,
  2074. &f71869_auto_pwm_attr[0][0],
  2075. ARRAY_SIZE(f71869_auto_pwm_attr[0]) * nr_fans);
  2076. break;
  2077. case f8000:
  2078. f71882fg_remove_sysfs_files(pdev,
  2079. f8000_fan_attr,
  2080. ARRAY_SIZE(f8000_fan_attr));
  2081. f71882fg_remove_sysfs_files(pdev,
  2082. &f8000_auto_pwm_attr[0][0],
  2083. ARRAY_SIZE(f8000_auto_pwm_attr[0]) * nr_fans);
  2084. break;
  2085. default:
  2086. f71882fg_remove_sysfs_files(pdev,
  2087. &fxxxx_auto_pwm_attr[0][0],
  2088. ARRAY_SIZE(fxxxx_auto_pwm_attr[0]) * nr_fans);
  2089. }
  2090. }
  2091. }
  2092. static int f71882fg_probe(struct platform_device *pdev)
  2093. {
  2094. struct f71882fg_data *data;
  2095. struct f71882fg_sio_data *sio_data = dev_get_platdata(&pdev->dev);
  2096. int nr_fans = f71882fg_nr_fans[sio_data->type];
  2097. int nr_temps = f71882fg_nr_temps[sio_data->type];
  2098. int err, i;
  2099. int size;
  2100. u8 start_reg, reg;
  2101. data = devm_kzalloc(&pdev->dev, sizeof(struct f71882fg_data),
  2102. GFP_KERNEL);
  2103. if (!data)
  2104. return -ENOMEM;
  2105. data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
  2106. data->type = sio_data->type;
  2107. data->temp_start =
  2108. (data->type == f71858fg || data->type == f8000 ||
  2109. data->type == f81866a) ? 0 : 1;
  2110. mutex_init(&data->update_lock);
  2111. platform_set_drvdata(pdev, data);
  2112. start_reg = f71882fg_read8(data, F71882FG_REG_START);
  2113. if (start_reg & 0x04) {
  2114. dev_warn(&pdev->dev, "Hardware monitor is powered down\n");
  2115. return -ENODEV;
  2116. }
  2117. if (!(start_reg & 0x03)) {
  2118. dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
  2119. return -ENODEV;
  2120. }
  2121. /* Register sysfs interface files */
  2122. err = device_create_file(&pdev->dev, &dev_attr_name);
  2123. if (err)
  2124. goto exit_unregister_sysfs;
  2125. if (start_reg & 0x01) {
  2126. switch (data->type) {
  2127. case f71858fg:
  2128. data->temp_config =
  2129. f71882fg_read8(data, F71882FG_REG_TEMP_CONFIG);
  2130. if (data->temp_config & 0x10)
  2131. /*
  2132. * The f71858fg temperature alarms behave as
  2133. * the f8000 alarms in this mode
  2134. */
  2135. err = f71882fg_create_sysfs_files(pdev,
  2136. f8000_temp_attr,
  2137. ARRAY_SIZE(f8000_temp_attr));
  2138. else
  2139. err = f71882fg_create_sysfs_files(pdev,
  2140. f71858fg_temp_attr,
  2141. ARRAY_SIZE(f71858fg_temp_attr));
  2142. break;
  2143. case f8000:
  2144. err = f71882fg_create_sysfs_files(pdev,
  2145. f8000_temp_attr,
  2146. ARRAY_SIZE(f8000_temp_attr));
  2147. break;
  2148. case f81866a:
  2149. err = f71882fg_create_sysfs_files(pdev,
  2150. f71858fg_temp_attr,
  2151. ARRAY_SIZE(f71858fg_temp_attr));
  2152. break;
  2153. default:
  2154. err = f71882fg_create_sysfs_files(pdev,
  2155. &fxxxx_temp_attr[0][0],
  2156. ARRAY_SIZE(fxxxx_temp_attr[0]) * nr_temps);
  2157. }
  2158. if (err)
  2159. goto exit_unregister_sysfs;
  2160. if (f71882fg_temp_has_beep[data->type]) {
  2161. if (data->type == f81866a) {
  2162. size = ARRAY_SIZE(f81866_temp_beep_attr[0]);
  2163. err = f71882fg_create_sysfs_files(pdev,
  2164. &f81866_temp_beep_attr[0][0],
  2165. size * nr_temps);
  2166. } else {
  2167. size = ARRAY_SIZE(fxxxx_temp_beep_attr[0]);
  2168. err = f71882fg_create_sysfs_files(pdev,
  2169. &fxxxx_temp_beep_attr[0][0],
  2170. size * nr_temps);
  2171. }
  2172. if (err)
  2173. goto exit_unregister_sysfs;
  2174. }
  2175. for (i = 0; i < F71882FG_MAX_INS; i++) {
  2176. if (f71882fg_has_in[data->type][i]) {
  2177. err = device_create_file(&pdev->dev,
  2178. &fxxxx_in_attr[i].dev_attr);
  2179. if (err)
  2180. goto exit_unregister_sysfs;
  2181. }
  2182. }
  2183. if (f71882fg_has_in1_alarm[data->type]) {
  2184. err = f71882fg_create_sysfs_files(pdev,
  2185. fxxxx_in1_alarm_attr,
  2186. ARRAY_SIZE(fxxxx_in1_alarm_attr));
  2187. if (err)
  2188. goto exit_unregister_sysfs;
  2189. }
  2190. }
  2191. if (start_reg & 0x02) {
  2192. switch (data->type) {
  2193. case f71808e:
  2194. case f71808a:
  2195. case f71869:
  2196. case f71869a:
  2197. /* These always have signed auto point temps */
  2198. data->auto_point_temp_signed = 1;
  2199. fallthrough; /* to select correct fan/pwm reg bank! */
  2200. case f71889fg:
  2201. case f71889ed:
  2202. case f71889a:
  2203. reg = f71882fg_read8(data, F71882FG_REG_FAN_FAULT_T);
  2204. if (reg & F71882FG_FAN_NEG_TEMP_EN)
  2205. data->auto_point_temp_signed = 1;
  2206. /* Ensure banked pwm registers point to right bank */
  2207. reg &= ~F71882FG_FAN_PROG_SEL;
  2208. f71882fg_write8(data, F71882FG_REG_FAN_FAULT_T, reg);
  2209. break;
  2210. default:
  2211. break;
  2212. }
  2213. data->pwm_enable =
  2214. f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
  2215. for (i = 0; i < nr_fans; i++) {
  2216. err = f71882fg_create_fan_sysfs_files(pdev, i);
  2217. if (err)
  2218. goto exit_unregister_sysfs;
  2219. }
  2220. /* Some types have 1 extra fan with limited functionality */
  2221. switch (data->type) {
  2222. case f71808a:
  2223. err = f71882fg_create_sysfs_files(pdev,
  2224. f71808a_fan3_attr,
  2225. ARRAY_SIZE(f71808a_fan3_attr));
  2226. break;
  2227. case f8000:
  2228. err = f71882fg_create_sysfs_files(pdev,
  2229. f8000_fan_attr,
  2230. ARRAY_SIZE(f8000_fan_attr));
  2231. break;
  2232. default:
  2233. break;
  2234. }
  2235. if (err)
  2236. goto exit_unregister_sysfs;
  2237. }
  2238. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  2239. if (IS_ERR(data->hwmon_dev)) {
  2240. err = PTR_ERR(data->hwmon_dev);
  2241. data->hwmon_dev = NULL;
  2242. goto exit_unregister_sysfs;
  2243. }
  2244. return 0;
  2245. exit_unregister_sysfs:
  2246. f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
  2247. return err; /* f71882fg_remove() also frees our data */
  2248. }
  2249. static int __init f71882fg_find(int sioaddr, struct f71882fg_sio_data *sio_data)
  2250. {
  2251. u16 devid;
  2252. unsigned short address;
  2253. int err = superio_enter(sioaddr);
  2254. if (err)
  2255. return err;
  2256. devid = superio_inw(sioaddr, SIO_REG_MANID);
  2257. if (devid != SIO_FINTEK_ID) {
  2258. pr_debug("Not a Fintek device\n");
  2259. err = -ENODEV;
  2260. goto exit;
  2261. }
  2262. devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
  2263. switch (devid) {
  2264. case SIO_F71808E_ID:
  2265. sio_data->type = f71808e;
  2266. break;
  2267. case SIO_F71808A_ID:
  2268. sio_data->type = f71808a;
  2269. break;
  2270. case SIO_F71858_ID:
  2271. case SIO_F71858AD_ID:
  2272. sio_data->type = f71858fg;
  2273. break;
  2274. case SIO_F71862_ID:
  2275. sio_data->type = f71862fg;
  2276. break;
  2277. case SIO_F71868_ID:
  2278. sio_data->type = f71868a;
  2279. break;
  2280. case SIO_F71869_ID:
  2281. sio_data->type = f71869;
  2282. break;
  2283. case SIO_F71869A_ID:
  2284. sio_data->type = f71869a;
  2285. break;
  2286. case SIO_F71882_ID:
  2287. sio_data->type = f71882fg;
  2288. break;
  2289. case SIO_F71889_ID:
  2290. sio_data->type = f71889fg;
  2291. break;
  2292. case SIO_F71889E_ID:
  2293. sio_data->type = f71889ed;
  2294. break;
  2295. case SIO_F71889A_ID:
  2296. sio_data->type = f71889a;
  2297. break;
  2298. case SIO_F8000_ID:
  2299. sio_data->type = f8000;
  2300. break;
  2301. case SIO_F81768D_ID:
  2302. sio_data->type = f81768d;
  2303. break;
  2304. case SIO_F81865_ID:
  2305. sio_data->type = f81865f;
  2306. break;
  2307. case SIO_F81866_ID:
  2308. case SIO_F81966_ID:
  2309. case SIO_F81968_ID:
  2310. sio_data->type = f81866a;
  2311. break;
  2312. default:
  2313. pr_info("Unsupported Fintek device: %04x\n",
  2314. (unsigned int)devid);
  2315. err = -ENODEV;
  2316. goto exit;
  2317. }
  2318. if (sio_data->type == f71858fg)
  2319. superio_select(sioaddr, SIO_F71858FG_LD_HWM);
  2320. else
  2321. superio_select(sioaddr, SIO_F71882FG_LD_HWM);
  2322. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  2323. pr_warn("Device not activated\n");
  2324. err = -ENODEV;
  2325. goto exit;
  2326. }
  2327. address = superio_inw(sioaddr, SIO_REG_ADDR);
  2328. if (address == 0) {
  2329. pr_warn("Base address not set\n");
  2330. err = -ENODEV;
  2331. goto exit;
  2332. }
  2333. address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
  2334. err = address;
  2335. pr_info("Found %s chip at %#x, revision %d, devid: %04x\n",
  2336. f71882fg_names[sio_data->type], (unsigned int)address,
  2337. (int)superio_inb(sioaddr, SIO_REG_DEVREV), devid);
  2338. exit:
  2339. superio_exit(sioaddr);
  2340. return err;
  2341. }
  2342. static int __init f71882fg_device_add(int address,
  2343. const struct f71882fg_sio_data *sio_data)
  2344. {
  2345. struct resource res = {
  2346. .start = address,
  2347. .end = address + REGION_LENGTH - 1,
  2348. .flags = IORESOURCE_IO,
  2349. };
  2350. int err;
  2351. f71882fg_pdev = platform_device_alloc(DRVNAME, address);
  2352. if (!f71882fg_pdev)
  2353. return -ENOMEM;
  2354. res.name = f71882fg_pdev->name;
  2355. err = acpi_check_resource_conflict(&res);
  2356. if (err)
  2357. goto exit_device_put;
  2358. err = platform_device_add_resources(f71882fg_pdev, &res, 1);
  2359. if (err) {
  2360. pr_err("Device resource addition failed\n");
  2361. goto exit_device_put;
  2362. }
  2363. err = platform_device_add_data(f71882fg_pdev, sio_data,
  2364. sizeof(struct f71882fg_sio_data));
  2365. if (err) {
  2366. pr_err("Platform data allocation failed\n");
  2367. goto exit_device_put;
  2368. }
  2369. err = platform_device_add(f71882fg_pdev);
  2370. if (err) {
  2371. pr_err("Device addition failed\n");
  2372. goto exit_device_put;
  2373. }
  2374. return 0;
  2375. exit_device_put:
  2376. platform_device_put(f71882fg_pdev);
  2377. return err;
  2378. }
  2379. static struct platform_driver f71882fg_driver = {
  2380. .driver = {
  2381. .name = DRVNAME,
  2382. },
  2383. .probe = f71882fg_probe,
  2384. .remove = f71882fg_remove,
  2385. };
  2386. static int __init f71882fg_init(void)
  2387. {
  2388. int err;
  2389. int address;
  2390. struct f71882fg_sio_data sio_data;
  2391. memset(&sio_data, 0, sizeof(sio_data));
  2392. address = f71882fg_find(0x2e, &sio_data);
  2393. if (address < 0)
  2394. address = f71882fg_find(0x4e, &sio_data);
  2395. if (address < 0)
  2396. return address;
  2397. err = platform_driver_register(&f71882fg_driver);
  2398. if (err)
  2399. return err;
  2400. err = f71882fg_device_add(address, &sio_data);
  2401. if (err)
  2402. goto exit_driver;
  2403. return 0;
  2404. exit_driver:
  2405. platform_driver_unregister(&f71882fg_driver);
  2406. return err;
  2407. }
  2408. static void __exit f71882fg_exit(void)
  2409. {
  2410. platform_device_unregister(f71882fg_pdev);
  2411. platform_driver_unregister(&f71882fg_driver);
  2412. }
  2413. MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
  2414. MODULE_AUTHOR("Hans Edgington, Hans de Goede <hdegoede@redhat.com>");
  2415. MODULE_LICENSE("GPL");
  2416. module_init(f71882fg_init);
  2417. module_exit(f71882fg_exit);