dme1737.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742
  1. /*
  2. * dme1737.c - Driver for the SMSC DME1737, Asus A8000, SMSC SCH311x, SCH5027,
  3. * and SCH5127 Super-I/O chips integrated hardware monitoring
  4. * features.
  5. * Copyright (c) 2007, 2008, 2009, 2010 Juerg Haefliger <juergh@gmail.com>
  6. *
  7. * This driver is an I2C/ISA hybrid, meaning that it uses the I2C bus to access
  8. * the chip registers if a DME1737, A8000, or SCH5027 is found and the ISA bus
  9. * if a SCH311x or SCH5127 chip is found. Both types of chips have very
  10. * similar hardware monitoring capabilities but differ in the way they can be
  11. * accessed.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/i2c.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/hwmon.h>
  35. #include <linux/hwmon-sysfs.h>
  36. #include <linux/hwmon-vid.h>
  37. #include <linux/err.h>
  38. #include <linux/mutex.h>
  39. #include <linux/acpi.h>
  40. #include <linux/io.h>
  41. /* ISA device, if found */
  42. static struct platform_device *pdev;
  43. /* Module load parameters */
  44. static int force_start;
  45. module_param(force_start, bool, 0);
  46. MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
  47. static unsigned short force_id;
  48. module_param(force_id, ushort, 0);
  49. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  50. static int probe_all_addr;
  51. module_param(probe_all_addr, bool, 0);
  52. MODULE_PARM_DESC(probe_all_addr, "Include probing of non-standard LPC "
  53. "addresses");
  54. /* Addresses to scan */
  55. static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
  56. enum chips { dme1737, sch5027, sch311x, sch5127 };
  57. /* ---------------------------------------------------------------------
  58. * Registers
  59. *
  60. * The sensors are defined as follows:
  61. *
  62. * Voltages Temperatures
  63. * -------- ------------
  64. * in0 +5VTR (+5V stdby) temp1 Remote diode 1
  65. * in1 Vccp (proc core) temp2 Internal temp
  66. * in2 VCC (internal +3.3V) temp3 Remote diode 2
  67. * in3 +5V
  68. * in4 +12V
  69. * in5 VTR (+3.3V stby)
  70. * in6 Vbat
  71. * in7 Vtrip (sch5127 only)
  72. *
  73. * --------------------------------------------------------------------- */
  74. /* Voltages (in) numbered 0-7 (ix) */
  75. #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) : \
  76. (ix) < 7 ? 0x94 + (ix) : \
  77. 0x1f)
  78. #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
  79. : 0x91 + (ix) * 2)
  80. #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
  81. : 0x92 + (ix) * 2)
  82. /* Temperatures (temp) numbered 0-2 (ix) */
  83. #define DME1737_REG_TEMP(ix) (0x25 + (ix))
  84. #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
  85. #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
  86. #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
  87. : 0x1c + (ix))
  88. /* Voltage and temperature LSBs
  89. * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
  90. * IN_TEMP_LSB(0) = [in5, in6]
  91. * IN_TEMP_LSB(1) = [temp3, temp1]
  92. * IN_TEMP_LSB(2) = [in4, temp2]
  93. * IN_TEMP_LSB(3) = [in3, in0]
  94. * IN_TEMP_LSB(4) = [in2, in1]
  95. * IN_TEMP_LSB(5) = [res, in7] */
  96. #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
  97. static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0, 5};
  98. static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4, 4};
  99. static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
  100. static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
  101. /* Fans numbered 0-5 (ix) */
  102. #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
  103. : 0xa1 + (ix) * 2)
  104. #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
  105. : 0xa5 + (ix) * 2)
  106. #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
  107. : 0xb2 + (ix))
  108. #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
  109. /* PWMs numbered 0-2, 4-5 (ix) */
  110. #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
  111. : 0xa1 + (ix))
  112. #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
  113. #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
  114. #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
  115. : 0xa3 + (ix))
  116. /* The layout of the ramp rate registers is different from the other pwm
  117. * registers. The bits for the 3 PWMs are stored in 2 registers:
  118. * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
  119. * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
  120. #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
  121. /* Thermal zones 0-2 */
  122. #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
  123. #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
  124. /* The layout of the hysteresis registers is different from the other zone
  125. * registers. The bits for the 3 zones are stored in 2 registers:
  126. * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
  127. * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
  128. #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
  129. /* Alarm registers and bit mapping
  130. * The 3 8-bit alarm registers will be concatenated to a single 32-bit
  131. * alarm value [0, ALARM3, ALARM2, ALARM1]. */
  132. #define DME1737_REG_ALARM1 0x41
  133. #define DME1737_REG_ALARM2 0x42
  134. #define DME1737_REG_ALARM3 0x83
  135. static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17, 18};
  136. static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
  137. static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
  138. /* Miscellaneous registers */
  139. #define DME1737_REG_DEVICE 0x3d
  140. #define DME1737_REG_COMPANY 0x3e
  141. #define DME1737_REG_VERSTEP 0x3f
  142. #define DME1737_REG_CONFIG 0x40
  143. #define DME1737_REG_CONFIG2 0x7f
  144. #define DME1737_REG_VID 0x43
  145. #define DME1737_REG_TACH_PWM 0x81
  146. /* ---------------------------------------------------------------------
  147. * Misc defines
  148. * --------------------------------------------------------------------- */
  149. /* Chip identification */
  150. #define DME1737_COMPANY_SMSC 0x5c
  151. #define DME1737_VERSTEP 0x88
  152. #define DME1737_VERSTEP_MASK 0xf8
  153. #define SCH311X_DEVICE 0x8c
  154. #define SCH5027_VERSTEP 0x69
  155. #define SCH5127_DEVICE 0x8e
  156. /* Device ID values (global configuration register index 0x20) */
  157. #define DME1737_ID_1 0x77
  158. #define DME1737_ID_2 0x78
  159. #define SCH3112_ID 0x7c
  160. #define SCH3114_ID 0x7d
  161. #define SCH3116_ID 0x7f
  162. #define SCH5027_ID 0x89
  163. #define SCH5127_ID 0x86
  164. /* Length of ISA address segment */
  165. #define DME1737_EXTENT 2
  166. /* chip-dependent features */
  167. #define HAS_TEMP_OFFSET (1 << 0) /* bit 0 */
  168. #define HAS_VID (1 << 1) /* bit 1 */
  169. #define HAS_ZONE3 (1 << 2) /* bit 2 */
  170. #define HAS_ZONE_HYST (1 << 3) /* bit 3 */
  171. #define HAS_PWM_MIN (1 << 4) /* bit 4 */
  172. #define HAS_FAN(ix) (1 << ((ix) + 5)) /* bits 5-10 */
  173. #define HAS_PWM(ix) (1 << ((ix) + 11)) /* bits 11-16 */
  174. #define HAS_IN7 (1 << 17) /* bit 17 */
  175. /* ---------------------------------------------------------------------
  176. * Data structures and manipulation thereof
  177. * --------------------------------------------------------------------- */
  178. struct dme1737_data {
  179. struct i2c_client *client; /* for I2C devices only */
  180. struct device *hwmon_dev;
  181. const char *name;
  182. unsigned int addr; /* for ISA devices only */
  183. struct mutex update_lock;
  184. int valid; /* !=0 if following fields are valid */
  185. unsigned long last_update; /* in jiffies */
  186. unsigned long last_vbat; /* in jiffies */
  187. enum chips type;
  188. const int *in_nominal; /* pointer to IN_NOMINAL array */
  189. u8 vid;
  190. u8 pwm_rr_en;
  191. u32 has_features;
  192. /* Register values */
  193. u16 in[8];
  194. u8 in_min[8];
  195. u8 in_max[8];
  196. s16 temp[3];
  197. s8 temp_min[3];
  198. s8 temp_max[3];
  199. s8 temp_offset[3];
  200. u8 config;
  201. u8 config2;
  202. u8 vrm;
  203. u16 fan[6];
  204. u16 fan_min[6];
  205. u8 fan_max[2];
  206. u8 fan_opt[6];
  207. u8 pwm[6];
  208. u8 pwm_min[3];
  209. u8 pwm_config[3];
  210. u8 pwm_acz[3];
  211. u8 pwm_freq[6];
  212. u8 pwm_rr[2];
  213. u8 zone_low[3];
  214. u8 zone_abs[3];
  215. u8 zone_hyst[2];
  216. u32 alarms;
  217. };
  218. /* Nominal voltage values */
  219. static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
  220. 3300};
  221. static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
  222. 3300};
  223. static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
  224. 3300};
  225. static const int IN_NOMINAL_SCH5127[] = {2500, 2250, 3300, 1125, 1125, 3300,
  226. 3300, 1500};
  227. #define IN_NOMINAL(type) ((type) == sch311x ? IN_NOMINAL_SCH311x : \
  228. (type) == sch5027 ? IN_NOMINAL_SCH5027 : \
  229. (type) == sch5127 ? IN_NOMINAL_SCH5127 : \
  230. IN_NOMINAL_DME1737)
  231. /* Voltage input
  232. * Voltage inputs have 16 bits resolution, limit values have 8 bits
  233. * resolution. */
  234. static inline int IN_FROM_REG(int reg, int nominal, int res)
  235. {
  236. return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
  237. }
  238. static inline int IN_TO_REG(int val, int nominal)
  239. {
  240. return SENSORS_LIMIT((val * 192 + nominal / 2) / nominal, 0, 255);
  241. }
  242. /* Temperature input
  243. * The register values represent temperatures in 2's complement notation from
  244. * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
  245. * values have 8 bits resolution. */
  246. static inline int TEMP_FROM_REG(int reg, int res)
  247. {
  248. return (reg * 1000) >> (res - 8);
  249. }
  250. static inline int TEMP_TO_REG(int val)
  251. {
  252. return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
  253. -128, 127);
  254. }
  255. /* Temperature range */
  256. static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
  257. 10000, 13333, 16000, 20000, 26666, 32000,
  258. 40000, 53333, 80000};
  259. static inline int TEMP_RANGE_FROM_REG(int reg)
  260. {
  261. return TEMP_RANGE[(reg >> 4) & 0x0f];
  262. }
  263. static int TEMP_RANGE_TO_REG(int val, int reg)
  264. {
  265. int i;
  266. for (i = 15; i > 0; i--) {
  267. if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
  268. break;
  269. }
  270. }
  271. return (reg & 0x0f) | (i << 4);
  272. }
  273. /* Temperature hysteresis
  274. * Register layout:
  275. * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
  276. * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
  277. static inline int TEMP_HYST_FROM_REG(int reg, int ix)
  278. {
  279. return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
  280. }
  281. static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
  282. {
  283. int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
  284. return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
  285. }
  286. /* Fan input RPM */
  287. static inline int FAN_FROM_REG(int reg, int tpc)
  288. {
  289. if (tpc) {
  290. return tpc * reg;
  291. } else {
  292. return (reg == 0 || reg == 0xffff) ? 0 : 90000 * 60 / reg;
  293. }
  294. }
  295. static inline int FAN_TO_REG(int val, int tpc)
  296. {
  297. if (tpc) {
  298. return SENSORS_LIMIT(val / tpc, 0, 0xffff);
  299. } else {
  300. return (val <= 0) ? 0xffff :
  301. SENSORS_LIMIT(90000 * 60 / val, 0, 0xfffe);
  302. }
  303. }
  304. /* Fan TPC (tach pulse count)
  305. * Converts a register value to a TPC multiplier or returns 0 if the tachometer
  306. * is configured in legacy (non-tpc) mode */
  307. static inline int FAN_TPC_FROM_REG(int reg)
  308. {
  309. return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
  310. }
  311. /* Fan type
  312. * The type of a fan is expressed in number of pulses-per-revolution that it
  313. * emits */
  314. static inline int FAN_TYPE_FROM_REG(int reg)
  315. {
  316. int edge = (reg >> 1) & 0x03;
  317. return (edge > 0) ? 1 << (edge - 1) : 0;
  318. }
  319. static inline int FAN_TYPE_TO_REG(int val, int reg)
  320. {
  321. int edge = (val == 4) ? 3 : val;
  322. return (reg & 0xf9) | (edge << 1);
  323. }
  324. /* Fan max RPM */
  325. static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
  326. 0x11, 0x0f, 0x0e};
  327. static int FAN_MAX_FROM_REG(int reg)
  328. {
  329. int i;
  330. for (i = 10; i > 0; i--) {
  331. if (reg == FAN_MAX[i]) {
  332. break;
  333. }
  334. }
  335. return 1000 + i * 500;
  336. }
  337. static int FAN_MAX_TO_REG(int val)
  338. {
  339. int i;
  340. for (i = 10; i > 0; i--) {
  341. if (val > (1000 + (i - 1) * 500)) {
  342. break;
  343. }
  344. }
  345. return FAN_MAX[i];
  346. }
  347. /* PWM enable
  348. * Register to enable mapping:
  349. * 000: 2 fan on zone 1 auto
  350. * 001: 2 fan on zone 2 auto
  351. * 010: 2 fan on zone 3 auto
  352. * 011: 0 fan full on
  353. * 100: -1 fan disabled
  354. * 101: 2 fan on hottest of zones 2,3 auto
  355. * 110: 2 fan on hottest of zones 1,2,3 auto
  356. * 111: 1 fan in manual mode */
  357. static inline int PWM_EN_FROM_REG(int reg)
  358. {
  359. static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
  360. return en[(reg >> 5) & 0x07];
  361. }
  362. static inline int PWM_EN_TO_REG(int val, int reg)
  363. {
  364. int en = (val == 1) ? 7 : 3;
  365. return (reg & 0x1f) | ((en & 0x07) << 5);
  366. }
  367. /* PWM auto channels zone
  368. * Register to auto channels zone mapping (ACZ is a bitfield with bit x
  369. * corresponding to zone x+1):
  370. * 000: 001 fan on zone 1 auto
  371. * 001: 010 fan on zone 2 auto
  372. * 010: 100 fan on zone 3 auto
  373. * 011: 000 fan full on
  374. * 100: 000 fan disabled
  375. * 101: 110 fan on hottest of zones 2,3 auto
  376. * 110: 111 fan on hottest of zones 1,2,3 auto
  377. * 111: 000 fan in manual mode */
  378. static inline int PWM_ACZ_FROM_REG(int reg)
  379. {
  380. static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
  381. return acz[(reg >> 5) & 0x07];
  382. }
  383. static inline int PWM_ACZ_TO_REG(int val, int reg)
  384. {
  385. int acz = (val == 4) ? 2 : val - 1;
  386. return (reg & 0x1f) | ((acz & 0x07) << 5);
  387. }
  388. /* PWM frequency */
  389. static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
  390. 15000, 20000, 30000, 25000, 0, 0, 0, 0};
  391. static inline int PWM_FREQ_FROM_REG(int reg)
  392. {
  393. return PWM_FREQ[reg & 0x0f];
  394. }
  395. static int PWM_FREQ_TO_REG(int val, int reg)
  396. {
  397. int i;
  398. /* the first two cases are special - stupid chip design! */
  399. if (val > 27500) {
  400. i = 10;
  401. } else if (val > 22500) {
  402. i = 11;
  403. } else {
  404. for (i = 9; i > 0; i--) {
  405. if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
  406. break;
  407. }
  408. }
  409. }
  410. return (reg & 0xf0) | i;
  411. }
  412. /* PWM ramp rate
  413. * Register layout:
  414. * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
  415. * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
  416. static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
  417. static inline int PWM_RR_FROM_REG(int reg, int ix)
  418. {
  419. int rr = (ix == 1) ? reg >> 4 : reg;
  420. return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
  421. }
  422. static int PWM_RR_TO_REG(int val, int ix, int reg)
  423. {
  424. int i;
  425. for (i = 0; i < 7; i++) {
  426. if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
  427. break;
  428. }
  429. }
  430. return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
  431. }
  432. /* PWM ramp rate enable */
  433. static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
  434. {
  435. return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
  436. }
  437. static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
  438. {
  439. int en = (ix == 1) ? 0x80 : 0x08;
  440. return val ? reg | en : reg & ~en;
  441. }
  442. /* PWM min/off
  443. * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
  444. * the register layout). */
  445. static inline int PWM_OFF_FROM_REG(int reg, int ix)
  446. {
  447. return (reg >> (ix + 5)) & 0x01;
  448. }
  449. static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
  450. {
  451. return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
  452. }
  453. /* ---------------------------------------------------------------------
  454. * Device I/O access
  455. *
  456. * ISA access is performed through an index/data register pair and needs to
  457. * be protected by a mutex during runtime (not required for initialization).
  458. * We use data->update_lock for this and need to ensure that we acquire it
  459. * before calling dme1737_read or dme1737_write.
  460. * --------------------------------------------------------------------- */
  461. static u8 dme1737_read(const struct dme1737_data *data, u8 reg)
  462. {
  463. struct i2c_client *client = data->client;
  464. s32 val;
  465. if (client) { /* I2C device */
  466. val = i2c_smbus_read_byte_data(client, reg);
  467. if (val < 0) {
  468. dev_warn(&client->dev, "Read from register "
  469. "0x%02x failed! Please report to the driver "
  470. "maintainer.\n", reg);
  471. }
  472. } else { /* ISA device */
  473. outb(reg, data->addr);
  474. val = inb(data->addr + 1);
  475. }
  476. return val;
  477. }
  478. static s32 dme1737_write(const struct dme1737_data *data, u8 reg, u8 val)
  479. {
  480. struct i2c_client *client = data->client;
  481. s32 res = 0;
  482. if (client) { /* I2C device */
  483. res = i2c_smbus_write_byte_data(client, reg, val);
  484. if (res < 0) {
  485. dev_warn(&client->dev, "Write to register "
  486. "0x%02x failed! Please report to the driver "
  487. "maintainer.\n", reg);
  488. }
  489. } else { /* ISA device */
  490. outb(reg, data->addr);
  491. outb(val, data->addr + 1);
  492. }
  493. return res;
  494. }
  495. static struct dme1737_data *dme1737_update_device(struct device *dev)
  496. {
  497. struct dme1737_data *data = dev_get_drvdata(dev);
  498. int ix;
  499. u8 lsb[6];
  500. mutex_lock(&data->update_lock);
  501. /* Enable a Vbat monitoring cycle every 10 mins */
  502. if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
  503. dme1737_write(data, DME1737_REG_CONFIG, dme1737_read(data,
  504. DME1737_REG_CONFIG) | 0x10);
  505. data->last_vbat = jiffies;
  506. }
  507. /* Sample register contents every 1 sec */
  508. if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
  509. if (data->has_features & HAS_VID) {
  510. data->vid = dme1737_read(data, DME1737_REG_VID) &
  511. 0x3f;
  512. }
  513. /* In (voltage) registers */
  514. for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
  515. /* Voltage inputs are stored as 16 bit values even
  516. * though they have only 12 bits resolution. This is
  517. * to make it consistent with the temp inputs. */
  518. if (ix == 7 && !(data->has_features & HAS_IN7)) {
  519. continue;
  520. }
  521. data->in[ix] = dme1737_read(data,
  522. DME1737_REG_IN(ix)) << 8;
  523. data->in_min[ix] = dme1737_read(data,
  524. DME1737_REG_IN_MIN(ix));
  525. data->in_max[ix] = dme1737_read(data,
  526. DME1737_REG_IN_MAX(ix));
  527. }
  528. /* Temp registers */
  529. for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
  530. /* Temp inputs are stored as 16 bit values even
  531. * though they have only 12 bits resolution. This is
  532. * to take advantage of implicit conversions between
  533. * register values (2's complement) and temp values
  534. * (signed decimal). */
  535. data->temp[ix] = dme1737_read(data,
  536. DME1737_REG_TEMP(ix)) << 8;
  537. data->temp_min[ix] = dme1737_read(data,
  538. DME1737_REG_TEMP_MIN(ix));
  539. data->temp_max[ix] = dme1737_read(data,
  540. DME1737_REG_TEMP_MAX(ix));
  541. if (data->has_features & HAS_TEMP_OFFSET) {
  542. data->temp_offset[ix] = dme1737_read(data,
  543. DME1737_REG_TEMP_OFFSET(ix));
  544. }
  545. }
  546. /* In and temp LSB registers
  547. * The LSBs are latched when the MSBs are read, so the order in
  548. * which the registers are read (MSB first, then LSB) is
  549. * important! */
  550. for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
  551. if (ix == 5 && !(data->has_features & HAS_IN7)) {
  552. continue;
  553. }
  554. lsb[ix] = dme1737_read(data,
  555. DME1737_REG_IN_TEMP_LSB(ix));
  556. }
  557. for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
  558. if (ix == 7 && !(data->has_features & HAS_IN7)) {
  559. continue;
  560. }
  561. data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
  562. DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
  563. }
  564. for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
  565. data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
  566. DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
  567. }
  568. /* Fan registers */
  569. for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
  570. /* Skip reading registers if optional fans are not
  571. * present */
  572. if (!(data->has_features & HAS_FAN(ix))) {
  573. continue;
  574. }
  575. data->fan[ix] = dme1737_read(data,
  576. DME1737_REG_FAN(ix));
  577. data->fan[ix] |= dme1737_read(data,
  578. DME1737_REG_FAN(ix) + 1) << 8;
  579. data->fan_min[ix] = dme1737_read(data,
  580. DME1737_REG_FAN_MIN(ix));
  581. data->fan_min[ix] |= dme1737_read(data,
  582. DME1737_REG_FAN_MIN(ix) + 1) << 8;
  583. data->fan_opt[ix] = dme1737_read(data,
  584. DME1737_REG_FAN_OPT(ix));
  585. /* fan_max exists only for fan[5-6] */
  586. if (ix > 3) {
  587. data->fan_max[ix - 4] = dme1737_read(data,
  588. DME1737_REG_FAN_MAX(ix));
  589. }
  590. }
  591. /* PWM registers */
  592. for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
  593. /* Skip reading registers if optional PWMs are not
  594. * present */
  595. if (!(data->has_features & HAS_PWM(ix))) {
  596. continue;
  597. }
  598. data->pwm[ix] = dme1737_read(data,
  599. DME1737_REG_PWM(ix));
  600. data->pwm_freq[ix] = dme1737_read(data,
  601. DME1737_REG_PWM_FREQ(ix));
  602. /* pwm_config and pwm_min exist only for pwm[1-3] */
  603. if (ix < 3) {
  604. data->pwm_config[ix] = dme1737_read(data,
  605. DME1737_REG_PWM_CONFIG(ix));
  606. data->pwm_min[ix] = dme1737_read(data,
  607. DME1737_REG_PWM_MIN(ix));
  608. }
  609. }
  610. for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
  611. data->pwm_rr[ix] = dme1737_read(data,
  612. DME1737_REG_PWM_RR(ix));
  613. }
  614. /* Thermal zone registers */
  615. for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
  616. /* Skip reading registers if zone3 is not present */
  617. if ((ix == 2) && !(data->has_features & HAS_ZONE3)) {
  618. continue;
  619. }
  620. /* sch5127 zone2 registers are special */
  621. if ((ix == 1) && (data->type == sch5127)) {
  622. data->zone_low[1] = dme1737_read(data,
  623. DME1737_REG_ZONE_LOW(2));
  624. data->zone_abs[1] = dme1737_read(data,
  625. DME1737_REG_ZONE_ABS(2));
  626. } else {
  627. data->zone_low[ix] = dme1737_read(data,
  628. DME1737_REG_ZONE_LOW(ix));
  629. data->zone_abs[ix] = dme1737_read(data,
  630. DME1737_REG_ZONE_ABS(ix));
  631. }
  632. }
  633. if (data->has_features & HAS_ZONE_HYST) {
  634. for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
  635. data->zone_hyst[ix] = dme1737_read(data,
  636. DME1737_REG_ZONE_HYST(ix));
  637. }
  638. }
  639. /* Alarm registers */
  640. data->alarms = dme1737_read(data,
  641. DME1737_REG_ALARM1);
  642. /* Bit 7 tells us if the other alarm registers are non-zero and
  643. * therefore also need to be read */
  644. if (data->alarms & 0x80) {
  645. data->alarms |= dme1737_read(data,
  646. DME1737_REG_ALARM2) << 8;
  647. data->alarms |= dme1737_read(data,
  648. DME1737_REG_ALARM3) << 16;
  649. }
  650. /* The ISA chips require explicit clearing of alarm bits.
  651. * Don't worry, an alarm will come back if the condition
  652. * that causes it still exists */
  653. if (!data->client) {
  654. if (data->alarms & 0xff0000) {
  655. dme1737_write(data, DME1737_REG_ALARM3,
  656. 0xff);
  657. }
  658. if (data->alarms & 0xff00) {
  659. dme1737_write(data, DME1737_REG_ALARM2,
  660. 0xff);
  661. }
  662. if (data->alarms & 0xff) {
  663. dme1737_write(data, DME1737_REG_ALARM1,
  664. 0xff);
  665. }
  666. }
  667. data->last_update = jiffies;
  668. data->valid = 1;
  669. }
  670. mutex_unlock(&data->update_lock);
  671. return data;
  672. }
  673. /* ---------------------------------------------------------------------
  674. * Voltage sysfs attributes
  675. * ix = [0-7]
  676. * --------------------------------------------------------------------- */
  677. #define SYS_IN_INPUT 0
  678. #define SYS_IN_MIN 1
  679. #define SYS_IN_MAX 2
  680. #define SYS_IN_ALARM 3
  681. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  682. char *buf)
  683. {
  684. struct dme1737_data *data = dme1737_update_device(dev);
  685. struct sensor_device_attribute_2
  686. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  687. int ix = sensor_attr_2->index;
  688. int fn = sensor_attr_2->nr;
  689. int res;
  690. switch (fn) {
  691. case SYS_IN_INPUT:
  692. res = IN_FROM_REG(data->in[ix], data->in_nominal[ix], 16);
  693. break;
  694. case SYS_IN_MIN:
  695. res = IN_FROM_REG(data->in_min[ix], data->in_nominal[ix], 8);
  696. break;
  697. case SYS_IN_MAX:
  698. res = IN_FROM_REG(data->in_max[ix], data->in_nominal[ix], 8);
  699. break;
  700. case SYS_IN_ALARM:
  701. res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
  702. break;
  703. default:
  704. res = 0;
  705. dev_dbg(dev, "Unknown function %d.\n", fn);
  706. }
  707. return sprintf(buf, "%d\n", res);
  708. }
  709. static ssize_t set_in(struct device *dev, struct device_attribute *attr,
  710. const char *buf, size_t count)
  711. {
  712. struct dme1737_data *data = dev_get_drvdata(dev);
  713. struct sensor_device_attribute_2
  714. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  715. int ix = sensor_attr_2->index;
  716. int fn = sensor_attr_2->nr;
  717. long val = simple_strtol(buf, NULL, 10);
  718. mutex_lock(&data->update_lock);
  719. switch (fn) {
  720. case SYS_IN_MIN:
  721. data->in_min[ix] = IN_TO_REG(val, data->in_nominal[ix]);
  722. dme1737_write(data, DME1737_REG_IN_MIN(ix),
  723. data->in_min[ix]);
  724. break;
  725. case SYS_IN_MAX:
  726. data->in_max[ix] = IN_TO_REG(val, data->in_nominal[ix]);
  727. dme1737_write(data, DME1737_REG_IN_MAX(ix),
  728. data->in_max[ix]);
  729. break;
  730. default:
  731. dev_dbg(dev, "Unknown function %d.\n", fn);
  732. }
  733. mutex_unlock(&data->update_lock);
  734. return count;
  735. }
  736. /* ---------------------------------------------------------------------
  737. * Temperature sysfs attributes
  738. * ix = [0-2]
  739. * --------------------------------------------------------------------- */
  740. #define SYS_TEMP_INPUT 0
  741. #define SYS_TEMP_MIN 1
  742. #define SYS_TEMP_MAX 2
  743. #define SYS_TEMP_OFFSET 3
  744. #define SYS_TEMP_ALARM 4
  745. #define SYS_TEMP_FAULT 5
  746. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  747. char *buf)
  748. {
  749. struct dme1737_data *data = dme1737_update_device(dev);
  750. struct sensor_device_attribute_2
  751. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  752. int ix = sensor_attr_2->index;
  753. int fn = sensor_attr_2->nr;
  754. int res;
  755. switch (fn) {
  756. case SYS_TEMP_INPUT:
  757. res = TEMP_FROM_REG(data->temp[ix], 16);
  758. break;
  759. case SYS_TEMP_MIN:
  760. res = TEMP_FROM_REG(data->temp_min[ix], 8);
  761. break;
  762. case SYS_TEMP_MAX:
  763. res = TEMP_FROM_REG(data->temp_max[ix], 8);
  764. break;
  765. case SYS_TEMP_OFFSET:
  766. res = TEMP_FROM_REG(data->temp_offset[ix], 8);
  767. break;
  768. case SYS_TEMP_ALARM:
  769. res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
  770. break;
  771. case SYS_TEMP_FAULT:
  772. res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
  773. break;
  774. default:
  775. res = 0;
  776. dev_dbg(dev, "Unknown function %d.\n", fn);
  777. }
  778. return sprintf(buf, "%d\n", res);
  779. }
  780. static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
  781. const char *buf, size_t count)
  782. {
  783. struct dme1737_data *data = dev_get_drvdata(dev);
  784. struct sensor_device_attribute_2
  785. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  786. int ix = sensor_attr_2->index;
  787. int fn = sensor_attr_2->nr;
  788. long val = simple_strtol(buf, NULL, 10);
  789. mutex_lock(&data->update_lock);
  790. switch (fn) {
  791. case SYS_TEMP_MIN:
  792. data->temp_min[ix] = TEMP_TO_REG(val);
  793. dme1737_write(data, DME1737_REG_TEMP_MIN(ix),
  794. data->temp_min[ix]);
  795. break;
  796. case SYS_TEMP_MAX:
  797. data->temp_max[ix] = TEMP_TO_REG(val);
  798. dme1737_write(data, DME1737_REG_TEMP_MAX(ix),
  799. data->temp_max[ix]);
  800. break;
  801. case SYS_TEMP_OFFSET:
  802. data->temp_offset[ix] = TEMP_TO_REG(val);
  803. dme1737_write(data, DME1737_REG_TEMP_OFFSET(ix),
  804. data->temp_offset[ix]);
  805. break;
  806. default:
  807. dev_dbg(dev, "Unknown function %d.\n", fn);
  808. }
  809. mutex_unlock(&data->update_lock);
  810. return count;
  811. }
  812. /* ---------------------------------------------------------------------
  813. * Zone sysfs attributes
  814. * ix = [0-2]
  815. * --------------------------------------------------------------------- */
  816. #define SYS_ZONE_AUTO_CHANNELS_TEMP 0
  817. #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
  818. #define SYS_ZONE_AUTO_POINT1_TEMP 2
  819. #define SYS_ZONE_AUTO_POINT2_TEMP 3
  820. #define SYS_ZONE_AUTO_POINT3_TEMP 4
  821. static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
  822. char *buf)
  823. {
  824. struct dme1737_data *data = dme1737_update_device(dev);
  825. struct sensor_device_attribute_2
  826. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  827. int ix = sensor_attr_2->index;
  828. int fn = sensor_attr_2->nr;
  829. int res;
  830. switch (fn) {
  831. case SYS_ZONE_AUTO_CHANNELS_TEMP:
  832. /* check config2 for non-standard temp-to-zone mapping */
  833. if ((ix == 1) && (data->config2 & 0x02)) {
  834. res = 4;
  835. } else {
  836. res = 1 << ix;
  837. }
  838. break;
  839. case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
  840. res = TEMP_FROM_REG(data->zone_low[ix], 8) -
  841. TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
  842. break;
  843. case SYS_ZONE_AUTO_POINT1_TEMP:
  844. res = TEMP_FROM_REG(data->zone_low[ix], 8);
  845. break;
  846. case SYS_ZONE_AUTO_POINT2_TEMP:
  847. /* pwm_freq holds the temp range bits in the upper nibble */
  848. res = TEMP_FROM_REG(data->zone_low[ix], 8) +
  849. TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
  850. break;
  851. case SYS_ZONE_AUTO_POINT3_TEMP:
  852. res = TEMP_FROM_REG(data->zone_abs[ix], 8);
  853. break;
  854. default:
  855. res = 0;
  856. dev_dbg(dev, "Unknown function %d.\n", fn);
  857. }
  858. return sprintf(buf, "%d\n", res);
  859. }
  860. static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
  861. const char *buf, size_t count)
  862. {
  863. struct dme1737_data *data = dev_get_drvdata(dev);
  864. struct sensor_device_attribute_2
  865. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  866. int ix = sensor_attr_2->index;
  867. int fn = sensor_attr_2->nr;
  868. long val = simple_strtol(buf, NULL, 10);
  869. mutex_lock(&data->update_lock);
  870. switch (fn) {
  871. case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
  872. /* Refresh the cache */
  873. data->zone_low[ix] = dme1737_read(data,
  874. DME1737_REG_ZONE_LOW(ix));
  875. /* Modify the temp hyst value */
  876. data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
  877. TEMP_FROM_REG(data->zone_low[ix], 8) -
  878. val, ix, dme1737_read(data,
  879. DME1737_REG_ZONE_HYST(ix == 2)));
  880. dme1737_write(data, DME1737_REG_ZONE_HYST(ix == 2),
  881. data->zone_hyst[ix == 2]);
  882. break;
  883. case SYS_ZONE_AUTO_POINT1_TEMP:
  884. data->zone_low[ix] = TEMP_TO_REG(val);
  885. dme1737_write(data, DME1737_REG_ZONE_LOW(ix),
  886. data->zone_low[ix]);
  887. break;
  888. case SYS_ZONE_AUTO_POINT2_TEMP:
  889. /* Refresh the cache */
  890. data->zone_low[ix] = dme1737_read(data,
  891. DME1737_REG_ZONE_LOW(ix));
  892. /* Modify the temp range value (which is stored in the upper
  893. * nibble of the pwm_freq register) */
  894. data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
  895. TEMP_FROM_REG(data->zone_low[ix], 8),
  896. dme1737_read(data,
  897. DME1737_REG_PWM_FREQ(ix)));
  898. dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
  899. data->pwm_freq[ix]);
  900. break;
  901. case SYS_ZONE_AUTO_POINT3_TEMP:
  902. data->zone_abs[ix] = TEMP_TO_REG(val);
  903. dme1737_write(data, DME1737_REG_ZONE_ABS(ix),
  904. data->zone_abs[ix]);
  905. break;
  906. default:
  907. dev_dbg(dev, "Unknown function %d.\n", fn);
  908. }
  909. mutex_unlock(&data->update_lock);
  910. return count;
  911. }
  912. /* ---------------------------------------------------------------------
  913. * Fan sysfs attributes
  914. * ix = [0-5]
  915. * --------------------------------------------------------------------- */
  916. #define SYS_FAN_INPUT 0
  917. #define SYS_FAN_MIN 1
  918. #define SYS_FAN_MAX 2
  919. #define SYS_FAN_ALARM 3
  920. #define SYS_FAN_TYPE 4
  921. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  922. char *buf)
  923. {
  924. struct dme1737_data *data = dme1737_update_device(dev);
  925. struct sensor_device_attribute_2
  926. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  927. int ix = sensor_attr_2->index;
  928. int fn = sensor_attr_2->nr;
  929. int res;
  930. switch (fn) {
  931. case SYS_FAN_INPUT:
  932. res = FAN_FROM_REG(data->fan[ix],
  933. ix < 4 ? 0 :
  934. FAN_TPC_FROM_REG(data->fan_opt[ix]));
  935. break;
  936. case SYS_FAN_MIN:
  937. res = FAN_FROM_REG(data->fan_min[ix],
  938. ix < 4 ? 0 :
  939. FAN_TPC_FROM_REG(data->fan_opt[ix]));
  940. break;
  941. case SYS_FAN_MAX:
  942. /* only valid for fan[5-6] */
  943. res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
  944. break;
  945. case SYS_FAN_ALARM:
  946. res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
  947. break;
  948. case SYS_FAN_TYPE:
  949. /* only valid for fan[1-4] */
  950. res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
  951. break;
  952. default:
  953. res = 0;
  954. dev_dbg(dev, "Unknown function %d.\n", fn);
  955. }
  956. return sprintf(buf, "%d\n", res);
  957. }
  958. static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
  959. const char *buf, size_t count)
  960. {
  961. struct dme1737_data *data = dev_get_drvdata(dev);
  962. struct sensor_device_attribute_2
  963. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  964. int ix = sensor_attr_2->index;
  965. int fn = sensor_attr_2->nr;
  966. long val = simple_strtol(buf, NULL, 10);
  967. mutex_lock(&data->update_lock);
  968. switch (fn) {
  969. case SYS_FAN_MIN:
  970. if (ix < 4) {
  971. data->fan_min[ix] = FAN_TO_REG(val, 0);
  972. } else {
  973. /* Refresh the cache */
  974. data->fan_opt[ix] = dme1737_read(data,
  975. DME1737_REG_FAN_OPT(ix));
  976. /* Modify the fan min value */
  977. data->fan_min[ix] = FAN_TO_REG(val,
  978. FAN_TPC_FROM_REG(data->fan_opt[ix]));
  979. }
  980. dme1737_write(data, DME1737_REG_FAN_MIN(ix),
  981. data->fan_min[ix] & 0xff);
  982. dme1737_write(data, DME1737_REG_FAN_MIN(ix) + 1,
  983. data->fan_min[ix] >> 8);
  984. break;
  985. case SYS_FAN_MAX:
  986. /* Only valid for fan[5-6] */
  987. data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
  988. dme1737_write(data, DME1737_REG_FAN_MAX(ix),
  989. data->fan_max[ix - 4]);
  990. break;
  991. case SYS_FAN_TYPE:
  992. /* Only valid for fan[1-4] */
  993. if (!(val == 1 || val == 2 || val == 4)) {
  994. count = -EINVAL;
  995. dev_warn(dev, "Fan type value %ld not "
  996. "supported. Choose one of 1, 2, or 4.\n",
  997. val);
  998. goto exit;
  999. }
  1000. data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(data,
  1001. DME1737_REG_FAN_OPT(ix)));
  1002. dme1737_write(data, DME1737_REG_FAN_OPT(ix),
  1003. data->fan_opt[ix]);
  1004. break;
  1005. default:
  1006. dev_dbg(dev, "Unknown function %d.\n", fn);
  1007. }
  1008. exit:
  1009. mutex_unlock(&data->update_lock);
  1010. return count;
  1011. }
  1012. /* ---------------------------------------------------------------------
  1013. * PWM sysfs attributes
  1014. * ix = [0-4]
  1015. * --------------------------------------------------------------------- */
  1016. #define SYS_PWM 0
  1017. #define SYS_PWM_FREQ 1
  1018. #define SYS_PWM_ENABLE 2
  1019. #define SYS_PWM_RAMP_RATE 3
  1020. #define SYS_PWM_AUTO_CHANNELS_ZONE 4
  1021. #define SYS_PWM_AUTO_PWM_MIN 5
  1022. #define SYS_PWM_AUTO_POINT1_PWM 6
  1023. #define SYS_PWM_AUTO_POINT2_PWM 7
  1024. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  1025. char *buf)
  1026. {
  1027. struct dme1737_data *data = dme1737_update_device(dev);
  1028. struct sensor_device_attribute_2
  1029. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  1030. int ix = sensor_attr_2->index;
  1031. int fn = sensor_attr_2->nr;
  1032. int res;
  1033. switch (fn) {
  1034. case SYS_PWM:
  1035. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
  1036. res = 255;
  1037. } else {
  1038. res = data->pwm[ix];
  1039. }
  1040. break;
  1041. case SYS_PWM_FREQ:
  1042. res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
  1043. break;
  1044. case SYS_PWM_ENABLE:
  1045. if (ix >= 3) {
  1046. res = 1; /* pwm[5-6] hard-wired to manual mode */
  1047. } else {
  1048. res = PWM_EN_FROM_REG(data->pwm_config[ix]);
  1049. }
  1050. break;
  1051. case SYS_PWM_RAMP_RATE:
  1052. /* Only valid for pwm[1-3] */
  1053. res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
  1054. break;
  1055. case SYS_PWM_AUTO_CHANNELS_ZONE:
  1056. /* Only valid for pwm[1-3] */
  1057. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1058. res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
  1059. } else {
  1060. res = data->pwm_acz[ix];
  1061. }
  1062. break;
  1063. case SYS_PWM_AUTO_PWM_MIN:
  1064. /* Only valid for pwm[1-3] */
  1065. if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
  1066. res = data->pwm_min[ix];
  1067. } else {
  1068. res = 0;
  1069. }
  1070. break;
  1071. case SYS_PWM_AUTO_POINT1_PWM:
  1072. /* Only valid for pwm[1-3] */
  1073. res = data->pwm_min[ix];
  1074. break;
  1075. case SYS_PWM_AUTO_POINT2_PWM:
  1076. /* Only valid for pwm[1-3] */
  1077. res = 255; /* hard-wired */
  1078. break;
  1079. default:
  1080. res = 0;
  1081. dev_dbg(dev, "Unknown function %d.\n", fn);
  1082. }
  1083. return sprintf(buf, "%d\n", res);
  1084. }
  1085. static struct attribute *dme1737_pwm_chmod_attr[];
  1086. static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
  1087. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  1088. const char *buf, size_t count)
  1089. {
  1090. struct dme1737_data *data = dev_get_drvdata(dev);
  1091. struct sensor_device_attribute_2
  1092. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  1093. int ix = sensor_attr_2->index;
  1094. int fn = sensor_attr_2->nr;
  1095. long val = simple_strtol(buf, NULL, 10);
  1096. mutex_lock(&data->update_lock);
  1097. switch (fn) {
  1098. case SYS_PWM:
  1099. data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
  1100. dme1737_write(data, DME1737_REG_PWM(ix), data->pwm[ix]);
  1101. break;
  1102. case SYS_PWM_FREQ:
  1103. data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(data,
  1104. DME1737_REG_PWM_FREQ(ix)));
  1105. dme1737_write(data, DME1737_REG_PWM_FREQ(ix),
  1106. data->pwm_freq[ix]);
  1107. break;
  1108. case SYS_PWM_ENABLE:
  1109. /* Only valid for pwm[1-3] */
  1110. if (val < 0 || val > 2) {
  1111. count = -EINVAL;
  1112. dev_warn(dev, "PWM enable %ld not "
  1113. "supported. Choose one of 0, 1, or 2.\n",
  1114. val);
  1115. goto exit;
  1116. }
  1117. /* Refresh the cache */
  1118. data->pwm_config[ix] = dme1737_read(data,
  1119. DME1737_REG_PWM_CONFIG(ix));
  1120. if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
  1121. /* Bail out if no change */
  1122. goto exit;
  1123. }
  1124. /* Do some housekeeping if we are currently in auto mode */
  1125. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1126. /* Save the current zone channel assignment */
  1127. data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
  1128. data->pwm_config[ix]);
  1129. /* Save the current ramp rate state and disable it */
  1130. data->pwm_rr[ix > 0] = dme1737_read(data,
  1131. DME1737_REG_PWM_RR(ix > 0));
  1132. data->pwm_rr_en &= ~(1 << ix);
  1133. if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
  1134. data->pwm_rr_en |= (1 << ix);
  1135. data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
  1136. data->pwm_rr[ix > 0]);
  1137. dme1737_write(data,
  1138. DME1737_REG_PWM_RR(ix > 0),
  1139. data->pwm_rr[ix > 0]);
  1140. }
  1141. }
  1142. /* Set the new PWM mode */
  1143. switch (val) {
  1144. case 0:
  1145. /* Change permissions of pwm[ix] to read-only */
  1146. dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
  1147. S_IRUGO);
  1148. /* Turn fan fully on */
  1149. data->pwm_config[ix] = PWM_EN_TO_REG(0,
  1150. data->pwm_config[ix]);
  1151. dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
  1152. data->pwm_config[ix]);
  1153. break;
  1154. case 1:
  1155. /* Turn on manual mode */
  1156. data->pwm_config[ix] = PWM_EN_TO_REG(1,
  1157. data->pwm_config[ix]);
  1158. dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
  1159. data->pwm_config[ix]);
  1160. /* Change permissions of pwm[ix] to read-writeable */
  1161. dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
  1162. S_IRUGO | S_IWUSR);
  1163. break;
  1164. case 2:
  1165. /* Change permissions of pwm[ix] to read-only */
  1166. dme1737_chmod_file(dev, dme1737_pwm_chmod_attr[ix],
  1167. S_IRUGO);
  1168. /* Turn on auto mode using the saved zone channel
  1169. * assignment */
  1170. data->pwm_config[ix] = PWM_ACZ_TO_REG(
  1171. data->pwm_acz[ix],
  1172. data->pwm_config[ix]);
  1173. dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
  1174. data->pwm_config[ix]);
  1175. /* Enable PWM ramp rate if previously enabled */
  1176. if (data->pwm_rr_en & (1 << ix)) {
  1177. data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
  1178. dme1737_read(data,
  1179. DME1737_REG_PWM_RR(ix > 0)));
  1180. dme1737_write(data,
  1181. DME1737_REG_PWM_RR(ix > 0),
  1182. data->pwm_rr[ix > 0]);
  1183. }
  1184. break;
  1185. }
  1186. break;
  1187. case SYS_PWM_RAMP_RATE:
  1188. /* Only valid for pwm[1-3] */
  1189. /* Refresh the cache */
  1190. data->pwm_config[ix] = dme1737_read(data,
  1191. DME1737_REG_PWM_CONFIG(ix));
  1192. data->pwm_rr[ix > 0] = dme1737_read(data,
  1193. DME1737_REG_PWM_RR(ix > 0));
  1194. /* Set the ramp rate value */
  1195. if (val > 0) {
  1196. data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
  1197. data->pwm_rr[ix > 0]);
  1198. }
  1199. /* Enable/disable the feature only if the associated PWM
  1200. * output is in automatic mode. */
  1201. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1202. data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
  1203. data->pwm_rr[ix > 0]);
  1204. }
  1205. dme1737_write(data, DME1737_REG_PWM_RR(ix > 0),
  1206. data->pwm_rr[ix > 0]);
  1207. break;
  1208. case SYS_PWM_AUTO_CHANNELS_ZONE:
  1209. /* Only valid for pwm[1-3] */
  1210. if (!(val == 1 || val == 2 || val == 4 ||
  1211. val == 6 || val == 7)) {
  1212. count = -EINVAL;
  1213. dev_warn(dev, "PWM auto channels zone %ld "
  1214. "not supported. Choose one of 1, 2, 4, 6, "
  1215. "or 7.\n", val);
  1216. goto exit;
  1217. }
  1218. /* Refresh the cache */
  1219. data->pwm_config[ix] = dme1737_read(data,
  1220. DME1737_REG_PWM_CONFIG(ix));
  1221. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1222. /* PWM is already in auto mode so update the temp
  1223. * channel assignment */
  1224. data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
  1225. data->pwm_config[ix]);
  1226. dme1737_write(data, DME1737_REG_PWM_CONFIG(ix),
  1227. data->pwm_config[ix]);
  1228. } else {
  1229. /* PWM is not in auto mode so we save the temp
  1230. * channel assignment for later use */
  1231. data->pwm_acz[ix] = val;
  1232. }
  1233. break;
  1234. case SYS_PWM_AUTO_PWM_MIN:
  1235. /* Only valid for pwm[1-3] */
  1236. /* Refresh the cache */
  1237. data->pwm_min[ix] = dme1737_read(data,
  1238. DME1737_REG_PWM_MIN(ix));
  1239. /* There are only 2 values supported for the auto_pwm_min
  1240. * value: 0 or auto_point1_pwm. So if the temperature drops
  1241. * below the auto_point1_temp_hyst value, the fan either turns
  1242. * off or runs at auto_point1_pwm duty-cycle. */
  1243. if (val > ((data->pwm_min[ix] + 1) / 2)) {
  1244. data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
  1245. dme1737_read(data,
  1246. DME1737_REG_PWM_RR(0)));
  1247. } else {
  1248. data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
  1249. dme1737_read(data,
  1250. DME1737_REG_PWM_RR(0)));
  1251. }
  1252. dme1737_write(data, DME1737_REG_PWM_RR(0),
  1253. data->pwm_rr[0]);
  1254. break;
  1255. case SYS_PWM_AUTO_POINT1_PWM:
  1256. /* Only valid for pwm[1-3] */
  1257. data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
  1258. dme1737_write(data, DME1737_REG_PWM_MIN(ix),
  1259. data->pwm_min[ix]);
  1260. break;
  1261. default:
  1262. dev_dbg(dev, "Unknown function %d.\n", fn);
  1263. }
  1264. exit:
  1265. mutex_unlock(&data->update_lock);
  1266. return count;
  1267. }
  1268. /* ---------------------------------------------------------------------
  1269. * Miscellaneous sysfs attributes
  1270. * --------------------------------------------------------------------- */
  1271. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  1272. char *buf)
  1273. {
  1274. struct i2c_client *client = to_i2c_client(dev);
  1275. struct dme1737_data *data = i2c_get_clientdata(client);
  1276. return sprintf(buf, "%d\n", data->vrm);
  1277. }
  1278. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  1279. const char *buf, size_t count)
  1280. {
  1281. struct dme1737_data *data = dev_get_drvdata(dev);
  1282. long val = simple_strtol(buf, NULL, 10);
  1283. data->vrm = val;
  1284. return count;
  1285. }
  1286. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  1287. char *buf)
  1288. {
  1289. struct dme1737_data *data = dme1737_update_device(dev);
  1290. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  1291. }
  1292. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  1293. char *buf)
  1294. {
  1295. struct dme1737_data *data = dev_get_drvdata(dev);
  1296. return sprintf(buf, "%s\n", data->name);
  1297. }
  1298. /* ---------------------------------------------------------------------
  1299. * Sysfs device attribute defines and structs
  1300. * --------------------------------------------------------------------- */
  1301. /* Voltages 0-7 */
  1302. #define SENSOR_DEVICE_ATTR_IN(ix) \
  1303. static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
  1304. show_in, NULL, SYS_IN_INPUT, ix); \
  1305. static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
  1306. show_in, set_in, SYS_IN_MIN, ix); \
  1307. static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
  1308. show_in, set_in, SYS_IN_MAX, ix); \
  1309. static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
  1310. show_in, NULL, SYS_IN_ALARM, ix)
  1311. SENSOR_DEVICE_ATTR_IN(0);
  1312. SENSOR_DEVICE_ATTR_IN(1);
  1313. SENSOR_DEVICE_ATTR_IN(2);
  1314. SENSOR_DEVICE_ATTR_IN(3);
  1315. SENSOR_DEVICE_ATTR_IN(4);
  1316. SENSOR_DEVICE_ATTR_IN(5);
  1317. SENSOR_DEVICE_ATTR_IN(6);
  1318. SENSOR_DEVICE_ATTR_IN(7);
  1319. /* Temperatures 1-3 */
  1320. #define SENSOR_DEVICE_ATTR_TEMP(ix) \
  1321. static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
  1322. show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
  1323. static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
  1324. show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
  1325. static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
  1326. show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
  1327. static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
  1328. show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
  1329. static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
  1330. show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
  1331. static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
  1332. show_temp, NULL, SYS_TEMP_FAULT, ix-1)
  1333. SENSOR_DEVICE_ATTR_TEMP(1);
  1334. SENSOR_DEVICE_ATTR_TEMP(2);
  1335. SENSOR_DEVICE_ATTR_TEMP(3);
  1336. /* Zones 1-3 */
  1337. #define SENSOR_DEVICE_ATTR_ZONE(ix) \
  1338. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
  1339. show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
  1340. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
  1341. show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
  1342. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
  1343. show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
  1344. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
  1345. show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
  1346. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
  1347. show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
  1348. SENSOR_DEVICE_ATTR_ZONE(1);
  1349. SENSOR_DEVICE_ATTR_ZONE(2);
  1350. SENSOR_DEVICE_ATTR_ZONE(3);
  1351. /* Fans 1-4 */
  1352. #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
  1353. static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
  1354. show_fan, NULL, SYS_FAN_INPUT, ix-1); \
  1355. static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  1356. show_fan, set_fan, SYS_FAN_MIN, ix-1); \
  1357. static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  1358. show_fan, NULL, SYS_FAN_ALARM, ix-1); \
  1359. static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
  1360. show_fan, set_fan, SYS_FAN_TYPE, ix-1)
  1361. SENSOR_DEVICE_ATTR_FAN_1TO4(1);
  1362. SENSOR_DEVICE_ATTR_FAN_1TO4(2);
  1363. SENSOR_DEVICE_ATTR_FAN_1TO4(3);
  1364. SENSOR_DEVICE_ATTR_FAN_1TO4(4);
  1365. /* Fans 5-6 */
  1366. #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
  1367. static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
  1368. show_fan, NULL, SYS_FAN_INPUT, ix-1); \
  1369. static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  1370. show_fan, set_fan, SYS_FAN_MIN, ix-1); \
  1371. static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  1372. show_fan, NULL, SYS_FAN_ALARM, ix-1); \
  1373. static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
  1374. show_fan, set_fan, SYS_FAN_MAX, ix-1)
  1375. SENSOR_DEVICE_ATTR_FAN_5TO6(5);
  1376. SENSOR_DEVICE_ATTR_FAN_5TO6(6);
  1377. /* PWMs 1-3 */
  1378. #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
  1379. static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
  1380. show_pwm, set_pwm, SYS_PWM, ix-1); \
  1381. static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
  1382. show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
  1383. static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
  1384. show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
  1385. static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
  1386. show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
  1387. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
  1388. show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
  1389. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
  1390. show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
  1391. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
  1392. show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
  1393. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
  1394. show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
  1395. SENSOR_DEVICE_ATTR_PWM_1TO3(1);
  1396. SENSOR_DEVICE_ATTR_PWM_1TO3(2);
  1397. SENSOR_DEVICE_ATTR_PWM_1TO3(3);
  1398. /* PWMs 5-6 */
  1399. #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
  1400. static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
  1401. show_pwm, set_pwm, SYS_PWM, ix-1); \
  1402. static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
  1403. show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
  1404. static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
  1405. show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
  1406. SENSOR_DEVICE_ATTR_PWM_5TO6(5);
  1407. SENSOR_DEVICE_ATTR_PWM_5TO6(6);
  1408. /* Misc */
  1409. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  1410. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  1411. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */
  1412. /* This struct holds all the attributes that are always present and need to be
  1413. * created unconditionally. The attributes that need modification of their
  1414. * permissions are created read-only and write permissions are added or removed
  1415. * on the fly when required */
  1416. static struct attribute *dme1737_attr[] = {
  1417. /* Voltages */
  1418. &sensor_dev_attr_in0_input.dev_attr.attr,
  1419. &sensor_dev_attr_in0_min.dev_attr.attr,
  1420. &sensor_dev_attr_in0_max.dev_attr.attr,
  1421. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1422. &sensor_dev_attr_in1_input.dev_attr.attr,
  1423. &sensor_dev_attr_in1_min.dev_attr.attr,
  1424. &sensor_dev_attr_in1_max.dev_attr.attr,
  1425. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1426. &sensor_dev_attr_in2_input.dev_attr.attr,
  1427. &sensor_dev_attr_in2_min.dev_attr.attr,
  1428. &sensor_dev_attr_in2_max.dev_attr.attr,
  1429. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1430. &sensor_dev_attr_in3_input.dev_attr.attr,
  1431. &sensor_dev_attr_in3_min.dev_attr.attr,
  1432. &sensor_dev_attr_in3_max.dev_attr.attr,
  1433. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1434. &sensor_dev_attr_in4_input.dev_attr.attr,
  1435. &sensor_dev_attr_in4_min.dev_attr.attr,
  1436. &sensor_dev_attr_in4_max.dev_attr.attr,
  1437. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1438. &sensor_dev_attr_in5_input.dev_attr.attr,
  1439. &sensor_dev_attr_in5_min.dev_attr.attr,
  1440. &sensor_dev_attr_in5_max.dev_attr.attr,
  1441. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1442. &sensor_dev_attr_in6_input.dev_attr.attr,
  1443. &sensor_dev_attr_in6_min.dev_attr.attr,
  1444. &sensor_dev_attr_in6_max.dev_attr.attr,
  1445. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1446. /* Temperatures */
  1447. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1448. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1449. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1450. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1451. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  1452. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1453. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1454. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1455. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1456. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  1457. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1458. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1459. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1460. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1461. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  1462. /* Zones */
  1463. &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
  1464. &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
  1465. &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
  1466. &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
  1467. &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
  1468. &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
  1469. &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
  1470. &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
  1471. NULL
  1472. };
  1473. static const struct attribute_group dme1737_group = {
  1474. .attrs = dme1737_attr,
  1475. };
  1476. /* The following struct holds temp offset attributes, which are not available
  1477. * in all chips. The following chips support them:
  1478. * DME1737, SCH311x */
  1479. static struct attribute *dme1737_temp_offset_attr[] = {
  1480. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  1481. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  1482. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  1483. NULL
  1484. };
  1485. static const struct attribute_group dme1737_temp_offset_group = {
  1486. .attrs = dme1737_temp_offset_attr,
  1487. };
  1488. /* The following struct holds VID related attributes, which are not available
  1489. * in all chips. The following chips support them:
  1490. * DME1737 */
  1491. static struct attribute *dme1737_vid_attr[] = {
  1492. &dev_attr_vrm.attr,
  1493. &dev_attr_cpu0_vid.attr,
  1494. NULL
  1495. };
  1496. static const struct attribute_group dme1737_vid_group = {
  1497. .attrs = dme1737_vid_attr,
  1498. };
  1499. /* The following struct holds temp zone 3 related attributes, which are not
  1500. * available in all chips. The following chips support them:
  1501. * DME1737, SCH311x, SCH5027 */
  1502. static struct attribute *dme1737_zone3_attr[] = {
  1503. &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
  1504. &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
  1505. &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
  1506. &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
  1507. NULL
  1508. };
  1509. static const struct attribute_group dme1737_zone3_group = {
  1510. .attrs = dme1737_zone3_attr,
  1511. };
  1512. /* The following struct holds temp zone hysteresis related attributes, which
  1513. * are not available in all chips. The following chips support them:
  1514. * DME1737, SCH311x */
  1515. static struct attribute *dme1737_zone_hyst_attr[] = {
  1516. &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
  1517. &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
  1518. &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
  1519. NULL
  1520. };
  1521. static const struct attribute_group dme1737_zone_hyst_group = {
  1522. .attrs = dme1737_zone_hyst_attr,
  1523. };
  1524. /* The following struct holds voltage in7 related attributes, which
  1525. * are not available in all chips. The following chips support them:
  1526. * SCH5127 */
  1527. static struct attribute *dme1737_in7_attr[] = {
  1528. &sensor_dev_attr_in7_input.dev_attr.attr,
  1529. &sensor_dev_attr_in7_min.dev_attr.attr,
  1530. &sensor_dev_attr_in7_max.dev_attr.attr,
  1531. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1532. NULL
  1533. };
  1534. static const struct attribute_group dme1737_in7_group = {
  1535. .attrs = dme1737_in7_attr,
  1536. };
  1537. /* The following structs hold the PWM attributes, some of which are optional.
  1538. * Their creation depends on the chip configuration which is determined during
  1539. * module load. */
  1540. static struct attribute *dme1737_pwm1_attr[] = {
  1541. &sensor_dev_attr_pwm1.dev_attr.attr,
  1542. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  1543. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1544. &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
  1545. &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
  1546. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  1547. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  1548. NULL
  1549. };
  1550. static struct attribute *dme1737_pwm2_attr[] = {
  1551. &sensor_dev_attr_pwm2.dev_attr.attr,
  1552. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  1553. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1554. &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
  1555. &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
  1556. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  1557. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  1558. NULL
  1559. };
  1560. static struct attribute *dme1737_pwm3_attr[] = {
  1561. &sensor_dev_attr_pwm3.dev_attr.attr,
  1562. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  1563. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1564. &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
  1565. &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
  1566. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  1567. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  1568. NULL
  1569. };
  1570. static struct attribute *dme1737_pwm5_attr[] = {
  1571. &sensor_dev_attr_pwm5.dev_attr.attr,
  1572. &sensor_dev_attr_pwm5_freq.dev_attr.attr,
  1573. &sensor_dev_attr_pwm5_enable.dev_attr.attr,
  1574. NULL
  1575. };
  1576. static struct attribute *dme1737_pwm6_attr[] = {
  1577. &sensor_dev_attr_pwm6.dev_attr.attr,
  1578. &sensor_dev_attr_pwm6_freq.dev_attr.attr,
  1579. &sensor_dev_attr_pwm6_enable.dev_attr.attr,
  1580. NULL
  1581. };
  1582. static const struct attribute_group dme1737_pwm_group[] = {
  1583. { .attrs = dme1737_pwm1_attr },
  1584. { .attrs = dme1737_pwm2_attr },
  1585. { .attrs = dme1737_pwm3_attr },
  1586. { .attrs = NULL },
  1587. { .attrs = dme1737_pwm5_attr },
  1588. { .attrs = dme1737_pwm6_attr },
  1589. };
  1590. /* The following struct holds auto PWM min attributes, which are not available
  1591. * in all chips. Their creation depends on the chip type which is determined
  1592. * during module load. */
  1593. static struct attribute *dme1737_auto_pwm_min_attr[] = {
  1594. &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
  1595. &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
  1596. &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
  1597. };
  1598. /* The following structs hold the fan attributes, some of which are optional.
  1599. * Their creation depends on the chip configuration which is determined during
  1600. * module load. */
  1601. static struct attribute *dme1737_fan1_attr[] = {
  1602. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1603. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1604. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1605. &sensor_dev_attr_fan1_type.dev_attr.attr,
  1606. NULL
  1607. };
  1608. static struct attribute *dme1737_fan2_attr[] = {
  1609. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1610. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1611. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1612. &sensor_dev_attr_fan2_type.dev_attr.attr,
  1613. NULL
  1614. };
  1615. static struct attribute *dme1737_fan3_attr[] = {
  1616. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1617. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1618. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1619. &sensor_dev_attr_fan3_type.dev_attr.attr,
  1620. NULL
  1621. };
  1622. static struct attribute *dme1737_fan4_attr[] = {
  1623. &sensor_dev_attr_fan4_input.dev_attr.attr,
  1624. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1625. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1626. &sensor_dev_attr_fan4_type.dev_attr.attr,
  1627. NULL
  1628. };
  1629. static struct attribute *dme1737_fan5_attr[] = {
  1630. &sensor_dev_attr_fan5_input.dev_attr.attr,
  1631. &sensor_dev_attr_fan5_min.dev_attr.attr,
  1632. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1633. &sensor_dev_attr_fan5_max.dev_attr.attr,
  1634. NULL
  1635. };
  1636. static struct attribute *dme1737_fan6_attr[] = {
  1637. &sensor_dev_attr_fan6_input.dev_attr.attr,
  1638. &sensor_dev_attr_fan6_min.dev_attr.attr,
  1639. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  1640. &sensor_dev_attr_fan6_max.dev_attr.attr,
  1641. NULL
  1642. };
  1643. static const struct attribute_group dme1737_fan_group[] = {
  1644. { .attrs = dme1737_fan1_attr },
  1645. { .attrs = dme1737_fan2_attr },
  1646. { .attrs = dme1737_fan3_attr },
  1647. { .attrs = dme1737_fan4_attr },
  1648. { .attrs = dme1737_fan5_attr },
  1649. { .attrs = dme1737_fan6_attr },
  1650. };
  1651. /* The permissions of the following zone attributes are changed to read-
  1652. * writeable if the chip is *not* locked. Otherwise they stay read-only. */
  1653. static struct attribute *dme1737_zone_chmod_attr[] = {
  1654. &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
  1655. &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
  1656. &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
  1657. &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
  1658. &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
  1659. &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
  1660. NULL
  1661. };
  1662. static const struct attribute_group dme1737_zone_chmod_group = {
  1663. .attrs = dme1737_zone_chmod_attr,
  1664. };
  1665. /* The permissions of the following zone 3 attributes are changed to read-
  1666. * writeable if the chip is *not* locked. Otherwise they stay read-only. */
  1667. static struct attribute *dme1737_zone3_chmod_attr[] = {
  1668. &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
  1669. &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
  1670. &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
  1671. NULL
  1672. };
  1673. static const struct attribute_group dme1737_zone3_chmod_group = {
  1674. .attrs = dme1737_zone3_chmod_attr,
  1675. };
  1676. /* The permissions of the following PWM attributes are changed to read-
  1677. * writeable if the chip is *not* locked and the respective PWM is available.
  1678. * Otherwise they stay read-only. */
  1679. static struct attribute *dme1737_pwm1_chmod_attr[] = {
  1680. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  1681. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1682. &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
  1683. &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
  1684. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  1685. NULL
  1686. };
  1687. static struct attribute *dme1737_pwm2_chmod_attr[] = {
  1688. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  1689. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1690. &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
  1691. &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
  1692. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  1693. NULL
  1694. };
  1695. static struct attribute *dme1737_pwm3_chmod_attr[] = {
  1696. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  1697. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1698. &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
  1699. &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
  1700. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  1701. NULL
  1702. };
  1703. static struct attribute *dme1737_pwm5_chmod_attr[] = {
  1704. &sensor_dev_attr_pwm5.dev_attr.attr,
  1705. &sensor_dev_attr_pwm5_freq.dev_attr.attr,
  1706. NULL
  1707. };
  1708. static struct attribute *dme1737_pwm6_chmod_attr[] = {
  1709. &sensor_dev_attr_pwm6.dev_attr.attr,
  1710. &sensor_dev_attr_pwm6_freq.dev_attr.attr,
  1711. NULL
  1712. };
  1713. static const struct attribute_group dme1737_pwm_chmod_group[] = {
  1714. { .attrs = dme1737_pwm1_chmod_attr },
  1715. { .attrs = dme1737_pwm2_chmod_attr },
  1716. { .attrs = dme1737_pwm3_chmod_attr },
  1717. { .attrs = NULL },
  1718. { .attrs = dme1737_pwm5_chmod_attr },
  1719. { .attrs = dme1737_pwm6_chmod_attr },
  1720. };
  1721. /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
  1722. * chip is not locked. Otherwise they are read-only. */
  1723. static struct attribute *dme1737_pwm_chmod_attr[] = {
  1724. &sensor_dev_attr_pwm1.dev_attr.attr,
  1725. &sensor_dev_attr_pwm2.dev_attr.attr,
  1726. &sensor_dev_attr_pwm3.dev_attr.attr,
  1727. };
  1728. /* ---------------------------------------------------------------------
  1729. * Super-IO functions
  1730. * --------------------------------------------------------------------- */
  1731. static inline void dme1737_sio_enter(int sio_cip)
  1732. {
  1733. outb(0x55, sio_cip);
  1734. }
  1735. static inline void dme1737_sio_exit(int sio_cip)
  1736. {
  1737. outb(0xaa, sio_cip);
  1738. }
  1739. static inline int dme1737_sio_inb(int sio_cip, int reg)
  1740. {
  1741. outb(reg, sio_cip);
  1742. return inb(sio_cip + 1);
  1743. }
  1744. static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
  1745. {
  1746. outb(reg, sio_cip);
  1747. outb(val, sio_cip + 1);
  1748. }
  1749. /* ---------------------------------------------------------------------
  1750. * Device initialization
  1751. * --------------------------------------------------------------------- */
  1752. static int dme1737_i2c_get_features(int, struct dme1737_data*);
  1753. static void dme1737_chmod_file(struct device *dev,
  1754. struct attribute *attr, mode_t mode)
  1755. {
  1756. if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
  1757. dev_warn(dev, "Failed to change permissions of %s.\n",
  1758. attr->name);
  1759. }
  1760. }
  1761. static void dme1737_chmod_group(struct device *dev,
  1762. const struct attribute_group *group,
  1763. mode_t mode)
  1764. {
  1765. struct attribute **attr;
  1766. for (attr = group->attrs; *attr; attr++) {
  1767. dme1737_chmod_file(dev, *attr, mode);
  1768. }
  1769. }
  1770. static void dme1737_remove_files(struct device *dev)
  1771. {
  1772. struct dme1737_data *data = dev_get_drvdata(dev);
  1773. int ix;
  1774. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1775. if (data->has_features & HAS_FAN(ix)) {
  1776. sysfs_remove_group(&dev->kobj,
  1777. &dme1737_fan_group[ix]);
  1778. }
  1779. }
  1780. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1781. if (data->has_features & HAS_PWM(ix)) {
  1782. sysfs_remove_group(&dev->kobj,
  1783. &dme1737_pwm_group[ix]);
  1784. if ((data->has_features & HAS_PWM_MIN) && ix < 3) {
  1785. sysfs_remove_file(&dev->kobj,
  1786. dme1737_auto_pwm_min_attr[ix]);
  1787. }
  1788. }
  1789. }
  1790. if (data->has_features & HAS_TEMP_OFFSET) {
  1791. sysfs_remove_group(&dev->kobj, &dme1737_temp_offset_group);
  1792. }
  1793. if (data->has_features & HAS_VID) {
  1794. sysfs_remove_group(&dev->kobj, &dme1737_vid_group);
  1795. }
  1796. if (data->has_features & HAS_ZONE3) {
  1797. sysfs_remove_group(&dev->kobj, &dme1737_zone3_group);
  1798. }
  1799. if (data->has_features & HAS_ZONE_HYST) {
  1800. sysfs_remove_group(&dev->kobj, &dme1737_zone_hyst_group);
  1801. }
  1802. if (data->has_features & HAS_IN7) {
  1803. sysfs_remove_group(&dev->kobj, &dme1737_in7_group);
  1804. }
  1805. sysfs_remove_group(&dev->kobj, &dme1737_group);
  1806. if (!data->client) {
  1807. sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
  1808. }
  1809. }
  1810. static int dme1737_create_files(struct device *dev)
  1811. {
  1812. struct dme1737_data *data = dev_get_drvdata(dev);
  1813. int err, ix;
  1814. /* Create a name attribute for ISA devices */
  1815. if (!data->client) {
  1816. err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr);
  1817. if (err) {
  1818. goto exit;
  1819. }
  1820. }
  1821. /* Create standard sysfs attributes */
  1822. err = sysfs_create_group(&dev->kobj, &dme1737_group);
  1823. if (err) {
  1824. goto exit_remove;
  1825. }
  1826. /* Create chip-dependent sysfs attributes */
  1827. if (data->has_features & HAS_TEMP_OFFSET) {
  1828. err = sysfs_create_group(&dev->kobj,
  1829. &dme1737_temp_offset_group);
  1830. if (err) {
  1831. goto exit_remove;
  1832. }
  1833. }
  1834. if (data->has_features & HAS_VID) {
  1835. err = sysfs_create_group(&dev->kobj, &dme1737_vid_group);
  1836. if (err) {
  1837. goto exit_remove;
  1838. }
  1839. }
  1840. if (data->has_features & HAS_ZONE3) {
  1841. err = sysfs_create_group(&dev->kobj, &dme1737_zone3_group);
  1842. if (err) {
  1843. goto exit_remove;
  1844. }
  1845. }
  1846. if (data->has_features & HAS_ZONE_HYST) {
  1847. err = sysfs_create_group(&dev->kobj, &dme1737_zone_hyst_group);
  1848. if (err) {
  1849. goto exit_remove;
  1850. }
  1851. }
  1852. if (data->has_features & HAS_IN7) {
  1853. err = sysfs_create_group(&dev->kobj, &dme1737_in7_group);
  1854. if (err) {
  1855. goto exit_remove;
  1856. }
  1857. }
  1858. /* Create fan sysfs attributes */
  1859. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1860. if (data->has_features & HAS_FAN(ix)) {
  1861. err = sysfs_create_group(&dev->kobj,
  1862. &dme1737_fan_group[ix]);
  1863. if (err) {
  1864. goto exit_remove;
  1865. }
  1866. }
  1867. }
  1868. /* Create PWM sysfs attributes */
  1869. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1870. if (data->has_features & HAS_PWM(ix)) {
  1871. err = sysfs_create_group(&dev->kobj,
  1872. &dme1737_pwm_group[ix]);
  1873. if (err) {
  1874. goto exit_remove;
  1875. }
  1876. if ((data->has_features & HAS_PWM_MIN) && (ix < 3)) {
  1877. err = sysfs_create_file(&dev->kobj,
  1878. dme1737_auto_pwm_min_attr[ix]);
  1879. if (err) {
  1880. goto exit_remove;
  1881. }
  1882. }
  1883. }
  1884. }
  1885. /* Inform if the device is locked. Otherwise change the permissions of
  1886. * selected attributes from read-only to read-writeable. */
  1887. if (data->config & 0x02) {
  1888. dev_info(dev, "Device is locked. Some attributes "
  1889. "will be read-only.\n");
  1890. } else {
  1891. /* Change permissions of zone sysfs attributes */
  1892. dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
  1893. S_IRUGO | S_IWUSR);
  1894. /* Change permissions of chip-dependent sysfs attributes */
  1895. if (data->has_features & HAS_TEMP_OFFSET) {
  1896. dme1737_chmod_group(dev, &dme1737_temp_offset_group,
  1897. S_IRUGO | S_IWUSR);
  1898. }
  1899. if (data->has_features & HAS_ZONE3) {
  1900. dme1737_chmod_group(dev, &dme1737_zone3_chmod_group,
  1901. S_IRUGO | S_IWUSR);
  1902. }
  1903. if (data->has_features & HAS_ZONE_HYST) {
  1904. dme1737_chmod_group(dev, &dme1737_zone_hyst_group,
  1905. S_IRUGO | S_IWUSR);
  1906. }
  1907. /* Change permissions of PWM sysfs attributes */
  1908. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
  1909. if (data->has_features & HAS_PWM(ix)) {
  1910. dme1737_chmod_group(dev,
  1911. &dme1737_pwm_chmod_group[ix],
  1912. S_IRUGO | S_IWUSR);
  1913. if ((data->has_features & HAS_PWM_MIN) &&
  1914. ix < 3) {
  1915. dme1737_chmod_file(dev,
  1916. dme1737_auto_pwm_min_attr[ix],
  1917. S_IRUGO | S_IWUSR);
  1918. }
  1919. }
  1920. }
  1921. /* Change permissions of pwm[1-3] if in manual mode */
  1922. for (ix = 0; ix < 3; ix++) {
  1923. if ((data->has_features & HAS_PWM(ix)) &&
  1924. (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
  1925. dme1737_chmod_file(dev,
  1926. dme1737_pwm_chmod_attr[ix],
  1927. S_IRUGO | S_IWUSR);
  1928. }
  1929. }
  1930. }
  1931. return 0;
  1932. exit_remove:
  1933. dme1737_remove_files(dev);
  1934. exit:
  1935. return err;
  1936. }
  1937. static int dme1737_init_device(struct device *dev)
  1938. {
  1939. struct dme1737_data *data = dev_get_drvdata(dev);
  1940. struct i2c_client *client = data->client;
  1941. int ix;
  1942. u8 reg;
  1943. /* Point to the right nominal voltages array */
  1944. data->in_nominal = IN_NOMINAL(data->type);
  1945. data->config = dme1737_read(data, DME1737_REG_CONFIG);
  1946. /* Inform if part is not monitoring/started */
  1947. if (!(data->config & 0x01)) {
  1948. if (!force_start) {
  1949. dev_err(dev, "Device is not monitoring. "
  1950. "Use the force_start load parameter to "
  1951. "override.\n");
  1952. return -EFAULT;
  1953. }
  1954. /* Force monitoring */
  1955. data->config |= 0x01;
  1956. dme1737_write(data, DME1737_REG_CONFIG, data->config);
  1957. }
  1958. /* Inform if part is not ready */
  1959. if (!(data->config & 0x04)) {
  1960. dev_err(dev, "Device is not ready.\n");
  1961. return -EFAULT;
  1962. }
  1963. /* Determine which optional fan and pwm features are enabled (only
  1964. * valid for I2C devices) */
  1965. if (client) { /* I2C chip */
  1966. data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
  1967. /* Check if optional fan3 input is enabled */
  1968. if (data->config2 & 0x04) {
  1969. data->has_features |= HAS_FAN(2);
  1970. }
  1971. /* Fan4 and pwm3 are only available if the client's I2C address
  1972. * is the default 0x2e. Otherwise the I/Os associated with
  1973. * these functions are used for addr enable/select. */
  1974. if (client->addr == 0x2e) {
  1975. data->has_features |= HAS_FAN(3) | HAS_PWM(2);
  1976. }
  1977. /* Determine which of the optional fan[5-6] and pwm[5-6]
  1978. * features are enabled. For this, we need to query the runtime
  1979. * registers through the Super-IO LPC interface. Try both
  1980. * config ports 0x2e and 0x4e. */
  1981. if (dme1737_i2c_get_features(0x2e, data) &&
  1982. dme1737_i2c_get_features(0x4e, data)) {
  1983. dev_warn(dev, "Failed to query Super-IO for optional "
  1984. "features.\n");
  1985. }
  1986. }
  1987. /* Fan[1-2] and pwm[1-2] are present in all chips */
  1988. data->has_features |= HAS_FAN(0) | HAS_FAN(1) | HAS_PWM(0) | HAS_PWM(1);
  1989. /* Chip-dependent features */
  1990. switch (data->type) {
  1991. case dme1737:
  1992. data->has_features |= HAS_TEMP_OFFSET | HAS_VID | HAS_ZONE3 |
  1993. HAS_ZONE_HYST | HAS_PWM_MIN;
  1994. break;
  1995. case sch311x:
  1996. data->has_features |= HAS_TEMP_OFFSET | HAS_ZONE3 |
  1997. HAS_ZONE_HYST | HAS_PWM_MIN | HAS_FAN(2) | HAS_PWM(2);
  1998. break;
  1999. case sch5027:
  2000. data->has_features |= HAS_ZONE3;
  2001. break;
  2002. case sch5127:
  2003. data->has_features |= HAS_FAN(2) | HAS_PWM(2) | HAS_IN7;
  2004. break;
  2005. default:
  2006. break;
  2007. }
  2008. dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
  2009. "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
  2010. (data->has_features & HAS_PWM(2)) ? "yes" : "no",
  2011. (data->has_features & HAS_PWM(4)) ? "yes" : "no",
  2012. (data->has_features & HAS_PWM(5)) ? "yes" : "no",
  2013. (data->has_features & HAS_FAN(2)) ? "yes" : "no",
  2014. (data->has_features & HAS_FAN(3)) ? "yes" : "no",
  2015. (data->has_features & HAS_FAN(4)) ? "yes" : "no",
  2016. (data->has_features & HAS_FAN(5)) ? "yes" : "no");
  2017. reg = dme1737_read(data, DME1737_REG_TACH_PWM);
  2018. /* Inform if fan-to-pwm mapping differs from the default */
  2019. if (client && reg != 0xa4) { /* I2C chip */
  2020. dev_warn(dev, "Non-standard fan to pwm mapping: "
  2021. "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
  2022. "fan4->pwm%d. Please report to the driver "
  2023. "maintainer.\n",
  2024. (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
  2025. ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
  2026. } else if (!client && reg != 0x24) { /* ISA chip */
  2027. dev_warn(dev, "Non-standard fan to pwm mapping: "
  2028. "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
  2029. "Please report to the driver maintainer.\n",
  2030. (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
  2031. ((reg >> 4) & 0x03) + 1);
  2032. }
  2033. /* Switch pwm[1-3] to manual mode if they are currently disabled and
  2034. * set the duty-cycles to 0% (which is identical to the PWMs being
  2035. * disabled). */
  2036. if (!(data->config & 0x02)) {
  2037. for (ix = 0; ix < 3; ix++) {
  2038. data->pwm_config[ix] = dme1737_read(data,
  2039. DME1737_REG_PWM_CONFIG(ix));
  2040. if ((data->has_features & HAS_PWM(ix)) &&
  2041. (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
  2042. dev_info(dev, "Switching pwm%d to "
  2043. "manual mode.\n", ix + 1);
  2044. data->pwm_config[ix] = PWM_EN_TO_REG(1,
  2045. data->pwm_config[ix]);
  2046. dme1737_write(data, DME1737_REG_PWM(ix), 0);
  2047. dme1737_write(data,
  2048. DME1737_REG_PWM_CONFIG(ix),
  2049. data->pwm_config[ix]);
  2050. }
  2051. }
  2052. }
  2053. /* Initialize the default PWM auto channels zone (acz) assignments */
  2054. data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
  2055. data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
  2056. data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
  2057. /* Set VRM */
  2058. if (data->has_features & HAS_VID) {
  2059. data->vrm = vid_which_vrm();
  2060. }
  2061. return 0;
  2062. }
  2063. /* ---------------------------------------------------------------------
  2064. * I2C device detection and registration
  2065. * --------------------------------------------------------------------- */
  2066. static struct i2c_driver dme1737_i2c_driver;
  2067. static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
  2068. {
  2069. int err = 0, reg;
  2070. u16 addr;
  2071. dme1737_sio_enter(sio_cip);
  2072. /* Check device ID
  2073. * We currently know about two kinds of DME1737 and SCH5027. */
  2074. reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
  2075. if (!(reg == DME1737_ID_1 || reg == DME1737_ID_2 ||
  2076. reg == SCH5027_ID)) {
  2077. err = -ENODEV;
  2078. goto exit;
  2079. }
  2080. /* Select logical device A (runtime registers) */
  2081. dme1737_sio_outb(sio_cip, 0x07, 0x0a);
  2082. /* Get the base address of the runtime registers */
  2083. addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
  2084. dme1737_sio_inb(sio_cip, 0x61);
  2085. if (!addr) {
  2086. err = -ENODEV;
  2087. goto exit;
  2088. }
  2089. /* Read the runtime registers to determine which optional features
  2090. * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
  2091. * to '10' if the respective feature is enabled. */
  2092. if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
  2093. data->has_features |= HAS_FAN(5);
  2094. }
  2095. if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
  2096. data->has_features |= HAS_PWM(5);
  2097. }
  2098. if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
  2099. data->has_features |= HAS_FAN(4);
  2100. }
  2101. if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
  2102. data->has_features |= HAS_PWM(4);
  2103. }
  2104. exit:
  2105. dme1737_sio_exit(sio_cip);
  2106. return err;
  2107. }
  2108. /* Return 0 if detection is successful, -ENODEV otherwise */
  2109. static int dme1737_i2c_detect(struct i2c_client *client,
  2110. struct i2c_board_info *info)
  2111. {
  2112. struct i2c_adapter *adapter = client->adapter;
  2113. struct device *dev = &adapter->dev;
  2114. u8 company, verstep = 0;
  2115. const char *name;
  2116. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  2117. return -ENODEV;
  2118. }
  2119. company = i2c_smbus_read_byte_data(client, DME1737_REG_COMPANY);
  2120. verstep = i2c_smbus_read_byte_data(client, DME1737_REG_VERSTEP);
  2121. if (company == DME1737_COMPANY_SMSC &&
  2122. verstep == SCH5027_VERSTEP) {
  2123. name = "sch5027";
  2124. } else if (company == DME1737_COMPANY_SMSC &&
  2125. (verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP) {
  2126. name = "dme1737";
  2127. } else {
  2128. return -ENODEV;
  2129. }
  2130. dev_info(dev, "Found a %s chip at 0x%02x (rev 0x%02x).\n",
  2131. verstep == SCH5027_VERSTEP ? "SCH5027" : "DME1737",
  2132. client->addr, verstep);
  2133. strlcpy(info->type, name, I2C_NAME_SIZE);
  2134. return 0;
  2135. }
  2136. static int dme1737_i2c_probe(struct i2c_client *client,
  2137. const struct i2c_device_id *id)
  2138. {
  2139. struct dme1737_data *data;
  2140. struct device *dev = &client->dev;
  2141. int err;
  2142. data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
  2143. if (!data) {
  2144. err = -ENOMEM;
  2145. goto exit;
  2146. }
  2147. i2c_set_clientdata(client, data);
  2148. data->type = id->driver_data;
  2149. data->client = client;
  2150. data->name = client->name;
  2151. mutex_init(&data->update_lock);
  2152. /* Initialize the DME1737 chip */
  2153. err = dme1737_init_device(dev);
  2154. if (err) {
  2155. dev_err(dev, "Failed to initialize device.\n");
  2156. goto exit_kfree;
  2157. }
  2158. /* Create sysfs files */
  2159. err = dme1737_create_files(dev);
  2160. if (err) {
  2161. dev_err(dev, "Failed to create sysfs files.\n");
  2162. goto exit_kfree;
  2163. }
  2164. /* Register device */
  2165. data->hwmon_dev = hwmon_device_register(dev);
  2166. if (IS_ERR(data->hwmon_dev)) {
  2167. dev_err(dev, "Failed to register device.\n");
  2168. err = PTR_ERR(data->hwmon_dev);
  2169. goto exit_remove;
  2170. }
  2171. return 0;
  2172. exit_remove:
  2173. dme1737_remove_files(dev);
  2174. exit_kfree:
  2175. kfree(data);
  2176. exit:
  2177. return err;
  2178. }
  2179. static int dme1737_i2c_remove(struct i2c_client *client)
  2180. {
  2181. struct dme1737_data *data = i2c_get_clientdata(client);
  2182. hwmon_device_unregister(data->hwmon_dev);
  2183. dme1737_remove_files(&client->dev);
  2184. kfree(data);
  2185. return 0;
  2186. }
  2187. static const struct i2c_device_id dme1737_id[] = {
  2188. { "dme1737", dme1737 },
  2189. { "sch5027", sch5027 },
  2190. { }
  2191. };
  2192. MODULE_DEVICE_TABLE(i2c, dme1737_id);
  2193. static struct i2c_driver dme1737_i2c_driver = {
  2194. .class = I2C_CLASS_HWMON,
  2195. .driver = {
  2196. .name = "dme1737",
  2197. },
  2198. .probe = dme1737_i2c_probe,
  2199. .remove = dme1737_i2c_remove,
  2200. .id_table = dme1737_id,
  2201. .detect = dme1737_i2c_detect,
  2202. .address_list = normal_i2c,
  2203. };
  2204. /* ---------------------------------------------------------------------
  2205. * ISA device detection and registration
  2206. * --------------------------------------------------------------------- */
  2207. static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
  2208. {
  2209. int err = 0, reg;
  2210. unsigned short base_addr;
  2211. dme1737_sio_enter(sio_cip);
  2212. /* Check device ID
  2213. * We currently know about SCH3112, SCH3114, SCH3116, and SCH5127 */
  2214. reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
  2215. if (!(reg == SCH3112_ID || reg == SCH3114_ID || reg == SCH3116_ID ||
  2216. reg == SCH5127_ID)) {
  2217. err = -ENODEV;
  2218. goto exit;
  2219. }
  2220. /* Select logical device A (runtime registers) */
  2221. dme1737_sio_outb(sio_cip, 0x07, 0x0a);
  2222. /* Get the base address of the runtime registers */
  2223. base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
  2224. dme1737_sio_inb(sio_cip, 0x61);
  2225. if (!base_addr) {
  2226. pr_err("Base address not set\n");
  2227. err = -ENODEV;
  2228. goto exit;
  2229. }
  2230. /* Access to the hwmon registers is through an index/data register
  2231. * pair located at offset 0x70/0x71. */
  2232. *addr = base_addr + 0x70;
  2233. exit:
  2234. dme1737_sio_exit(sio_cip);
  2235. return err;
  2236. }
  2237. static int __init dme1737_isa_device_add(unsigned short addr)
  2238. {
  2239. struct resource res = {
  2240. .start = addr,
  2241. .end = addr + DME1737_EXTENT - 1,
  2242. .name = "dme1737",
  2243. .flags = IORESOURCE_IO,
  2244. };
  2245. int err;
  2246. err = acpi_check_resource_conflict(&res);
  2247. if (err)
  2248. goto exit;
  2249. pdev = platform_device_alloc("dme1737", addr);
  2250. if (!pdev) {
  2251. pr_err("Failed to allocate device\n");
  2252. err = -ENOMEM;
  2253. goto exit;
  2254. }
  2255. err = platform_device_add_resources(pdev, &res, 1);
  2256. if (err) {
  2257. pr_err("Failed to add device resource (err = %d)\n", err);
  2258. goto exit_device_put;
  2259. }
  2260. err = platform_device_add(pdev);
  2261. if (err) {
  2262. pr_err("Failed to add device (err = %d)\n", err);
  2263. goto exit_device_put;
  2264. }
  2265. return 0;
  2266. exit_device_put:
  2267. platform_device_put(pdev);
  2268. pdev = NULL;
  2269. exit:
  2270. return err;
  2271. }
  2272. static int __devinit dme1737_isa_probe(struct platform_device *pdev)
  2273. {
  2274. u8 company, device;
  2275. struct resource *res;
  2276. struct dme1737_data *data;
  2277. struct device *dev = &pdev->dev;
  2278. int err;
  2279. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  2280. if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
  2281. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  2282. (unsigned short)res->start,
  2283. (unsigned short)res->start + DME1737_EXTENT - 1);
  2284. err = -EBUSY;
  2285. goto exit;
  2286. }
  2287. data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
  2288. if (!data) {
  2289. err = -ENOMEM;
  2290. goto exit_release_region;
  2291. }
  2292. data->addr = res->start;
  2293. platform_set_drvdata(pdev, data);
  2294. /* Skip chip detection if module is loaded with force_id parameter */
  2295. switch (force_id) {
  2296. case SCH3112_ID:
  2297. case SCH3114_ID:
  2298. case SCH3116_ID:
  2299. data->type = sch311x;
  2300. break;
  2301. case SCH5127_ID:
  2302. data->type = sch5127;
  2303. break;
  2304. default:
  2305. company = dme1737_read(data, DME1737_REG_COMPANY);
  2306. device = dme1737_read(data, DME1737_REG_DEVICE);
  2307. if ((company == DME1737_COMPANY_SMSC) &&
  2308. (device == SCH311X_DEVICE)) {
  2309. data->type = sch311x;
  2310. } else if ((company == DME1737_COMPANY_SMSC) &&
  2311. (device == SCH5127_DEVICE)) {
  2312. data->type = sch5127;
  2313. } else {
  2314. err = -ENODEV;
  2315. goto exit_kfree;
  2316. }
  2317. }
  2318. if (data->type == sch5127) {
  2319. data->name = "sch5127";
  2320. } else {
  2321. data->name = "sch311x";
  2322. }
  2323. /* Initialize the mutex */
  2324. mutex_init(&data->update_lock);
  2325. dev_info(dev, "Found a %s chip at 0x%04x\n",
  2326. data->type == sch5127 ? "SCH5127" : "SCH311x", data->addr);
  2327. /* Initialize the chip */
  2328. err = dme1737_init_device(dev);
  2329. if (err) {
  2330. dev_err(dev, "Failed to initialize device.\n");
  2331. goto exit_kfree;
  2332. }
  2333. /* Create sysfs files */
  2334. err = dme1737_create_files(dev);
  2335. if (err) {
  2336. dev_err(dev, "Failed to create sysfs files.\n");
  2337. goto exit_kfree;
  2338. }
  2339. /* Register device */
  2340. data->hwmon_dev = hwmon_device_register(dev);
  2341. if (IS_ERR(data->hwmon_dev)) {
  2342. dev_err(dev, "Failed to register device.\n");
  2343. err = PTR_ERR(data->hwmon_dev);
  2344. goto exit_remove_files;
  2345. }
  2346. return 0;
  2347. exit_remove_files:
  2348. dme1737_remove_files(dev);
  2349. exit_kfree:
  2350. platform_set_drvdata(pdev, NULL);
  2351. kfree(data);
  2352. exit_release_region:
  2353. release_region(res->start, DME1737_EXTENT);
  2354. exit:
  2355. return err;
  2356. }
  2357. static int __devexit dme1737_isa_remove(struct platform_device *pdev)
  2358. {
  2359. struct dme1737_data *data = platform_get_drvdata(pdev);
  2360. hwmon_device_unregister(data->hwmon_dev);
  2361. dme1737_remove_files(&pdev->dev);
  2362. release_region(data->addr, DME1737_EXTENT);
  2363. platform_set_drvdata(pdev, NULL);
  2364. kfree(data);
  2365. return 0;
  2366. }
  2367. static struct platform_driver dme1737_isa_driver = {
  2368. .driver = {
  2369. .owner = THIS_MODULE,
  2370. .name = "dme1737",
  2371. },
  2372. .probe = dme1737_isa_probe,
  2373. .remove = __devexit_p(dme1737_isa_remove),
  2374. };
  2375. /* ---------------------------------------------------------------------
  2376. * Module initialization and cleanup
  2377. * --------------------------------------------------------------------- */
  2378. static int __init dme1737_init(void)
  2379. {
  2380. int err;
  2381. unsigned short addr;
  2382. err = i2c_add_driver(&dme1737_i2c_driver);
  2383. if (err) {
  2384. goto exit;
  2385. }
  2386. if (dme1737_isa_detect(0x2e, &addr) &&
  2387. dme1737_isa_detect(0x4e, &addr) &&
  2388. (!probe_all_addr ||
  2389. (dme1737_isa_detect(0x162e, &addr) &&
  2390. dme1737_isa_detect(0x164e, &addr)))) {
  2391. /* Return 0 if we didn't find an ISA device */
  2392. return 0;
  2393. }
  2394. err = platform_driver_register(&dme1737_isa_driver);
  2395. if (err) {
  2396. goto exit_del_i2c_driver;
  2397. }
  2398. /* Sets global pdev as a side effect */
  2399. err = dme1737_isa_device_add(addr);
  2400. if (err) {
  2401. goto exit_del_isa_driver;
  2402. }
  2403. return 0;
  2404. exit_del_isa_driver:
  2405. platform_driver_unregister(&dme1737_isa_driver);
  2406. exit_del_i2c_driver:
  2407. i2c_del_driver(&dme1737_i2c_driver);
  2408. exit:
  2409. return err;
  2410. }
  2411. static void __exit dme1737_exit(void)
  2412. {
  2413. if (pdev) {
  2414. platform_device_unregister(pdev);
  2415. platform_driver_unregister(&dme1737_isa_driver);
  2416. }
  2417. i2c_del_driver(&dme1737_i2c_driver);
  2418. }
  2419. MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
  2420. MODULE_DESCRIPTION("DME1737 sensors");
  2421. MODULE_LICENSE("GPL");
  2422. module_init(dme1737_init);
  2423. module_exit(dme1737_exit);