dme1737.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. /*
  2. * dme1737.c - driver for the SMSC DME1737 and Asus A8000 Super-I/O chips
  3. * integrated hardware monitoring features.
  4. * Copyright (c) 2007 Juerg Haefliger <juergh@gmail.com>
  5. *
  6. * This driver is based on the LM85 driver. The hardware monitoring
  7. * capabilities of the DME1737 are very similar to the LM85 with some
  8. * additional features. Even though the DME1737 is a Super-I/O chip, the
  9. * hardware monitoring registers are only accessible via SMBus.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/hwmon-vid.h>
  33. #include <linux/err.h>
  34. #include <linux/mutex.h>
  35. #include <asm/io.h>
  36. /* Module load parameters */
  37. static int force_start;
  38. module_param(force_start, bool, 0);
  39. MODULE_PARM_DESC(force_start, "Force the chip to start monitoring inputs");
  40. /* Addresses to scan */
  41. static unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
  42. /* Insmod parameters */
  43. I2C_CLIENT_INSMOD_1(dme1737);
  44. /* ---------------------------------------------------------------------
  45. * Registers
  46. *
  47. * The sensors are defined as follows:
  48. *
  49. * Voltages Temperatures
  50. * -------- ------------
  51. * in0 +5VTR (+5V stdby) temp1 Remote diode 1
  52. * in1 Vccp (proc core) temp2 Internal temp
  53. * in2 VCC (internal +3.3V) temp3 Remote diode 2
  54. * in3 +5V
  55. * in4 +12V
  56. * in5 VTR (+3.3V stby)
  57. * in6 Vbat
  58. *
  59. * --------------------------------------------------------------------- */
  60. /* Voltages (in) numbered 0-6 (ix) */
  61. #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) \
  62. : 0x94 + (ix))
  63. #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
  64. : 0x91 + (ix) * 2)
  65. #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
  66. : 0x92 + (ix) * 2)
  67. /* Temperatures (temp) numbered 0-2 (ix) */
  68. #define DME1737_REG_TEMP(ix) (0x25 + (ix))
  69. #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
  70. #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
  71. #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
  72. : 0x1c + (ix))
  73. /* Voltage and temperature LSBs
  74. * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
  75. * IN_TEMP_LSB(0) = [in5, in6]
  76. * IN_TEMP_LSB(1) = [temp3, temp1]
  77. * IN_TEMP_LSB(2) = [in4, temp2]
  78. * IN_TEMP_LSB(3) = [in3, in0]
  79. * IN_TEMP_LSB(4) = [in2, in1] */
  80. #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
  81. static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0};
  82. static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4};
  83. static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
  84. static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
  85. /* Fans numbered 0-5 (ix) */
  86. #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
  87. : 0xa1 + (ix) * 2)
  88. #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
  89. : 0xa5 + (ix) * 2)
  90. #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
  91. : 0xb2 + (ix))
  92. #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
  93. /* PWMs numbered 0-2, 4-5 (ix) */
  94. #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
  95. : 0xa1 + (ix))
  96. #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
  97. #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
  98. #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
  99. : 0xa3 + (ix))
  100. /* The layout of the ramp rate registers is different from the other pwm
  101. * registers. The bits for the 3 PWMs are stored in 2 registers:
  102. * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
  103. * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0] */
  104. #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
  105. /* Thermal zones 0-2 */
  106. #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
  107. #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
  108. /* The layout of the hysteresis registers is different from the other zone
  109. * registers. The bits for the 3 zones are stored in 2 registers:
  110. * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
  111. * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES] */
  112. #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
  113. /* Alarm registers and bit mapping
  114. * The 3 8-bit alarm registers will be concatenated to a single 32-bit
  115. * alarm value [0, ALARM3, ALARM2, ALARM1]. */
  116. #define DME1737_REG_ALARM1 0x41
  117. #define DME1737_REG_ALARM2 0x42
  118. #define DME1737_REG_ALARM3 0x83
  119. static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17};
  120. static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
  121. static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
  122. /* Miscellaneous registers */
  123. #define DME1737_REG_COMPANY 0x3e
  124. #define DME1737_REG_VERSTEP 0x3f
  125. #define DME1737_REG_CONFIG 0x40
  126. #define DME1737_REG_CONFIG2 0x7f
  127. #define DME1737_REG_VID 0x43
  128. #define DME1737_REG_TACH_PWM 0x81
  129. /* ---------------------------------------------------------------------
  130. * Misc defines
  131. * --------------------------------------------------------------------- */
  132. /* Chip identification */
  133. #define DME1737_COMPANY_SMSC 0x5c
  134. #define DME1737_VERSTEP 0x88
  135. #define DME1737_VERSTEP_MASK 0xf8
  136. /* ---------------------------------------------------------------------
  137. * Data structures and manipulation thereof
  138. * --------------------------------------------------------------------- */
  139. struct dme1737_data {
  140. struct i2c_client client;
  141. struct class_device *class_dev;
  142. struct mutex update_lock;
  143. int valid; /* !=0 if following fields are valid */
  144. unsigned long last_update; /* in jiffies */
  145. unsigned long last_vbat; /* in jiffies */
  146. u8 vid;
  147. u8 pwm_rr_en;
  148. u8 has_pwm;
  149. u8 has_fan;
  150. /* Register values */
  151. u16 in[7];
  152. u8 in_min[7];
  153. u8 in_max[7];
  154. s16 temp[3];
  155. s8 temp_min[3];
  156. s8 temp_max[3];
  157. s8 temp_offset[3];
  158. u8 config;
  159. u8 config2;
  160. u8 vrm;
  161. u16 fan[6];
  162. u16 fan_min[6];
  163. u8 fan_max[2];
  164. u8 fan_opt[6];
  165. u8 pwm[6];
  166. u8 pwm_min[3];
  167. u8 pwm_config[3];
  168. u8 pwm_acz[3];
  169. u8 pwm_freq[6];
  170. u8 pwm_rr[2];
  171. u8 zone_low[3];
  172. u8 zone_abs[3];
  173. u8 zone_hyst[2];
  174. u32 alarms;
  175. };
  176. /* Nominal voltage values */
  177. static const int IN_NOMINAL[] = {5000, 2250, 3300, 5000, 12000, 3300, 3300};
  178. /* Voltage input
  179. * Voltage inputs have 16 bits resolution, limit values have 8 bits
  180. * resolution. */
  181. static inline int IN_FROM_REG(int reg, int ix, int res)
  182. {
  183. return (reg * IN_NOMINAL[ix] + (3 << (res - 3))) / (3 << (res - 2));
  184. }
  185. static inline int IN_TO_REG(int val, int ix)
  186. {
  187. return SENSORS_LIMIT((val * 192 + IN_NOMINAL[ix] / 2) /
  188. IN_NOMINAL[ix], 0, 255);
  189. }
  190. /* Temperature input
  191. * The register values represent temperatures in 2's complement notation from
  192. * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
  193. * values have 8 bits resolution. */
  194. static inline int TEMP_FROM_REG(int reg, int res)
  195. {
  196. return (reg * 1000) >> (res - 8);
  197. }
  198. static inline int TEMP_TO_REG(int val)
  199. {
  200. return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
  201. -128, 127);
  202. }
  203. /* Temperature range */
  204. static const int TEMP_RANGE[] = {2000, 2500, 3333, 4000, 5000, 6666, 8000,
  205. 10000, 13333, 16000, 20000, 26666, 32000,
  206. 40000, 53333, 80000};
  207. static inline int TEMP_RANGE_FROM_REG(int reg)
  208. {
  209. return TEMP_RANGE[(reg >> 4) & 0x0f];
  210. }
  211. static int TEMP_RANGE_TO_REG(int val, int reg)
  212. {
  213. int i;
  214. for (i = 15; i > 0; i--) {
  215. if (val > (TEMP_RANGE[i] + TEMP_RANGE[i - 1] + 1) / 2) {
  216. break;
  217. }
  218. }
  219. return (reg & 0x0f) | (i << 4);
  220. }
  221. /* Temperature hysteresis
  222. * Register layout:
  223. * reg[0] = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
  224. * reg[1] = [H3-3, H3-2, H3-1, H3-0, xxxx, xxxx, xxxx, xxxx] */
  225. static inline int TEMP_HYST_FROM_REG(int reg, int ix)
  226. {
  227. return (((ix == 1) ? reg : reg >> 4) & 0x0f) * 1000;
  228. }
  229. static inline int TEMP_HYST_TO_REG(int val, int ix, int reg)
  230. {
  231. int hyst = SENSORS_LIMIT((val + 500) / 1000, 0, 15);
  232. return (ix == 1) ? (reg & 0xf0) | hyst : (reg & 0x0f) | (hyst << 4);
  233. }
  234. /* Fan input RPM */
  235. static inline int FAN_FROM_REG(int reg, int tpc)
  236. {
  237. return (reg == 0 || reg == 0xffff) ? 0 :
  238. (tpc == 0) ? 90000 * 60 / reg : tpc * reg;
  239. }
  240. static inline int FAN_TO_REG(int val, int tpc)
  241. {
  242. return SENSORS_LIMIT((tpc == 0) ? 90000 * 60 / val : val / tpc,
  243. 0, 0xffff);
  244. }
  245. /* Fan TPC (tach pulse count)
  246. * Converts a register value to a TPC multiplier or returns 0 if the tachometer
  247. * is configured in legacy (non-tpc) mode */
  248. static inline int FAN_TPC_FROM_REG(int reg)
  249. {
  250. return (reg & 0x20) ? 0 : 60 >> (reg & 0x03);
  251. }
  252. /* Fan type
  253. * The type of a fan is expressed in number of pulses-per-revolution that it
  254. * emits */
  255. static inline int FAN_TYPE_FROM_REG(int reg)
  256. {
  257. int edge = (reg >> 1) & 0x03;
  258. return (edge > 0) ? 1 << (edge - 1) : 0;
  259. }
  260. static inline int FAN_TYPE_TO_REG(int val, int reg)
  261. {
  262. int edge = (val == 4) ? 3 : val;
  263. return (reg & 0xf9) | (edge << 1);
  264. }
  265. /* Fan max RPM */
  266. static const int FAN_MAX[] = {0x54, 0x38, 0x2a, 0x21, 0x1c, 0x18, 0x15, 0x12,
  267. 0x11, 0x0f, 0x0e};
  268. static int FAN_MAX_FROM_REG(int reg)
  269. {
  270. int i;
  271. for (i = 10; i > 0; i--) {
  272. if (reg == FAN_MAX[i]) {
  273. break;
  274. }
  275. }
  276. return 1000 + i * 500;
  277. }
  278. static int FAN_MAX_TO_REG(int val)
  279. {
  280. int i;
  281. for (i = 10; i > 0; i--) {
  282. if (val > (1000 + (i - 1) * 500)) {
  283. break;
  284. }
  285. }
  286. return FAN_MAX[i];
  287. }
  288. /* PWM enable
  289. * Register to enable mapping:
  290. * 000: 2 fan on zone 1 auto
  291. * 001: 2 fan on zone 2 auto
  292. * 010: 2 fan on zone 3 auto
  293. * 011: 0 fan full on
  294. * 100: -1 fan disabled
  295. * 101: 2 fan on hottest of zones 2,3 auto
  296. * 110: 2 fan on hottest of zones 1,2,3 auto
  297. * 111: 1 fan in manual mode */
  298. static inline int PWM_EN_FROM_REG(int reg)
  299. {
  300. static const int en[] = {2, 2, 2, 0, -1, 2, 2, 1};
  301. return en[(reg >> 5) & 0x07];
  302. }
  303. static inline int PWM_EN_TO_REG(int val, int reg)
  304. {
  305. int en = (val == 1) ? 7 : 3;
  306. return (reg & 0x1f) | ((en & 0x07) << 5);
  307. }
  308. /* PWM auto channels zone
  309. * Register to auto channels zone mapping (ACZ is a bitfield with bit x
  310. * corresponding to zone x+1):
  311. * 000: 001 fan on zone 1 auto
  312. * 001: 010 fan on zone 2 auto
  313. * 010: 100 fan on zone 3 auto
  314. * 011: 000 fan full on
  315. * 100: 000 fan disabled
  316. * 101: 110 fan on hottest of zones 2,3 auto
  317. * 110: 111 fan on hottest of zones 1,2,3 auto
  318. * 111: 000 fan in manual mode */
  319. static inline int PWM_ACZ_FROM_REG(int reg)
  320. {
  321. static const int acz[] = {1, 2, 4, 0, 0, 6, 7, 0};
  322. return acz[(reg >> 5) & 0x07];
  323. }
  324. static inline int PWM_ACZ_TO_REG(int val, int reg)
  325. {
  326. int acz = (val == 4) ? 2 : val - 1;
  327. return (reg & 0x1f) | ((acz & 0x07) << 5);
  328. }
  329. /* PWM frequency */
  330. static const int PWM_FREQ[] = {11, 15, 22, 29, 35, 44, 59, 88,
  331. 15000, 20000, 30000, 25000, 0, 0, 0, 0};
  332. static inline int PWM_FREQ_FROM_REG(int reg)
  333. {
  334. return PWM_FREQ[reg & 0x0f];
  335. }
  336. static int PWM_FREQ_TO_REG(int val, int reg)
  337. {
  338. int i;
  339. /* the first two cases are special - stupid chip design! */
  340. if (val > 27500) {
  341. i = 10;
  342. } else if (val > 22500) {
  343. i = 11;
  344. } else {
  345. for (i = 9; i > 0; i--) {
  346. if (val > (PWM_FREQ[i] + PWM_FREQ[i - 1] + 1) / 2) {
  347. break;
  348. }
  349. }
  350. }
  351. return (reg & 0xf0) | i;
  352. }
  353. /* PWM ramp rate
  354. * Register layout:
  355. * reg[0] = [OFF3, OFF2, OFF1, RES, RR1-E, RR1-2, RR1-1, RR1-0]
  356. * reg[1] = [RR2-E, RR2-2, RR2-1, RR2-0, RR3-E, RR3-2, RR3-1, RR3-0] */
  357. static const u8 PWM_RR[] = {206, 104, 69, 41, 26, 18, 10, 5};
  358. static inline int PWM_RR_FROM_REG(int reg, int ix)
  359. {
  360. int rr = (ix == 1) ? reg >> 4 : reg;
  361. return (rr & 0x08) ? PWM_RR[rr & 0x07] : 0;
  362. }
  363. static int PWM_RR_TO_REG(int val, int ix, int reg)
  364. {
  365. int i;
  366. for (i = 0; i < 7; i++) {
  367. if (val > (PWM_RR[i] + PWM_RR[i + 1] + 1) / 2) {
  368. break;
  369. }
  370. }
  371. return (ix == 1) ? (reg & 0x8f) | (i << 4) : (reg & 0xf8) | i;
  372. }
  373. /* PWM ramp rate enable */
  374. static inline int PWM_RR_EN_FROM_REG(int reg, int ix)
  375. {
  376. return PWM_RR_FROM_REG(reg, ix) ? 1 : 0;
  377. }
  378. static inline int PWM_RR_EN_TO_REG(int val, int ix, int reg)
  379. {
  380. int en = (ix == 1) ? 0x80 : 0x08;
  381. return val ? reg | en : reg & ~en;
  382. }
  383. /* PWM min/off
  384. * The PWM min/off bits are part of the PMW ramp rate register 0 (see above for
  385. * the register layout). */
  386. static inline int PWM_OFF_FROM_REG(int reg, int ix)
  387. {
  388. return (reg >> (ix + 5)) & 0x01;
  389. }
  390. static inline int PWM_OFF_TO_REG(int val, int ix, int reg)
  391. {
  392. return (reg & ~(1 << (ix + 5))) | ((val & 0x01) << (ix + 5));
  393. }
  394. /* ---------------------------------------------------------------------
  395. * Device I/O access
  396. * --------------------------------------------------------------------- */
  397. static u8 dme1737_read(struct i2c_client *client, u8 reg)
  398. {
  399. s32 val = i2c_smbus_read_byte_data(client, reg);
  400. if (val < 0) {
  401. dev_warn(&client->dev, "Read from register 0x%02x failed! "
  402. "Please report to the driver maintainer.\n", reg);
  403. }
  404. return val;
  405. }
  406. static s32 dme1737_write(struct i2c_client *client, u8 reg, u8 value)
  407. {
  408. s32 res = i2c_smbus_write_byte_data(client, reg, value);
  409. if (res < 0) {
  410. dev_warn(&client->dev, "Write to register 0x%02x failed! "
  411. "Please report to the driver maintainer.\n", reg);
  412. }
  413. return res;
  414. }
  415. static struct dme1737_data *dme1737_update_device(struct device *dev)
  416. {
  417. struct dme1737_data *data = dev_get_drvdata(dev);
  418. struct i2c_client *client = &data->client;
  419. int ix;
  420. u8 lsb[5];
  421. mutex_lock(&data->update_lock);
  422. /* Enable a Vbat monitoring cycle every 10 mins */
  423. if (time_after(jiffies, data->last_vbat + 600 * HZ) || !data->valid) {
  424. dme1737_write(client, DME1737_REG_CONFIG, dme1737_read(client,
  425. DME1737_REG_CONFIG) | 0x10);
  426. data->last_vbat = jiffies;
  427. }
  428. /* Sample register contents every 1 sec */
  429. if (time_after(jiffies, data->last_update + HZ) || !data->valid) {
  430. data->vid = dme1737_read(client, DME1737_REG_VID) & 0x3f;
  431. /* In (voltage) registers */
  432. for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
  433. /* Voltage inputs are stored as 16 bit values even
  434. * though they have only 12 bits resolution. This is
  435. * to make it consistent with the temp inputs. */
  436. data->in[ix] = dme1737_read(client,
  437. DME1737_REG_IN(ix)) << 8;
  438. data->in_min[ix] = dme1737_read(client,
  439. DME1737_REG_IN_MIN(ix));
  440. data->in_max[ix] = dme1737_read(client,
  441. DME1737_REG_IN_MAX(ix));
  442. }
  443. /* Temp registers */
  444. for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
  445. /* Temp inputs are stored as 16 bit values even
  446. * though they have only 12 bits resolution. This is
  447. * to take advantage of implicit conversions between
  448. * register values (2's complement) and temp values
  449. * (signed decimal). */
  450. data->temp[ix] = dme1737_read(client,
  451. DME1737_REG_TEMP(ix)) << 8;
  452. data->temp_min[ix] = dme1737_read(client,
  453. DME1737_REG_TEMP_MIN(ix));
  454. data->temp_max[ix] = dme1737_read(client,
  455. DME1737_REG_TEMP_MAX(ix));
  456. data->temp_offset[ix] = dme1737_read(client,
  457. DME1737_REG_TEMP_OFFSET(ix));
  458. }
  459. /* In and temp LSB registers
  460. * The LSBs are latched when the MSBs are read, so the order in
  461. * which the registers are read (MSB first, then LSB) is
  462. * important! */
  463. for (ix = 0; ix < ARRAY_SIZE(lsb); ix++) {
  464. lsb[ix] = dme1737_read(client,
  465. DME1737_REG_IN_TEMP_LSB(ix));
  466. }
  467. for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
  468. data->in[ix] |= (lsb[DME1737_REG_IN_LSB[ix]] <<
  469. DME1737_REG_IN_LSB_SHL[ix]) & 0xf0;
  470. }
  471. for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
  472. data->temp[ix] |= (lsb[DME1737_REG_TEMP_LSB[ix]] <<
  473. DME1737_REG_TEMP_LSB_SHL[ix]) & 0xf0;
  474. }
  475. /* Fan registers */
  476. for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
  477. /* Skip reading registers if optional fans are not
  478. * present */
  479. if (!(data->has_fan & (1 << ix))) {
  480. continue;
  481. }
  482. data->fan[ix] = dme1737_read(client,
  483. DME1737_REG_FAN(ix));
  484. data->fan[ix] |= dme1737_read(client,
  485. DME1737_REG_FAN(ix) + 1) << 8;
  486. data->fan_min[ix] = dme1737_read(client,
  487. DME1737_REG_FAN_MIN(ix));
  488. data->fan_min[ix] |= dme1737_read(client,
  489. DME1737_REG_FAN_MIN(ix) + 1) << 8;
  490. data->fan_opt[ix] = dme1737_read(client,
  491. DME1737_REG_FAN_OPT(ix));
  492. /* fan_max exists only for fan[5-6] */
  493. if (ix > 3) {
  494. data->fan_max[ix - 4] = dme1737_read(client,
  495. DME1737_REG_FAN_MAX(ix));
  496. }
  497. }
  498. /* PWM registers */
  499. for (ix = 0; ix < ARRAY_SIZE(data->pwm); ix++) {
  500. /* Skip reading registers if optional PWMs are not
  501. * present */
  502. if (!(data->has_pwm & (1 << ix))) {
  503. continue;
  504. }
  505. data->pwm[ix] = dme1737_read(client,
  506. DME1737_REG_PWM(ix));
  507. data->pwm_freq[ix] = dme1737_read(client,
  508. DME1737_REG_PWM_FREQ(ix));
  509. /* pwm_config and pwm_min exist only for pwm[1-3] */
  510. if (ix < 3) {
  511. data->pwm_config[ix] = dme1737_read(client,
  512. DME1737_REG_PWM_CONFIG(ix));
  513. data->pwm_min[ix] = dme1737_read(client,
  514. DME1737_REG_PWM_MIN(ix));
  515. }
  516. }
  517. for (ix = 0; ix < ARRAY_SIZE(data->pwm_rr); ix++) {
  518. data->pwm_rr[ix] = dme1737_read(client,
  519. DME1737_REG_PWM_RR(ix));
  520. }
  521. /* Thermal zone registers */
  522. for (ix = 0; ix < ARRAY_SIZE(data->zone_low); ix++) {
  523. data->zone_low[ix] = dme1737_read(client,
  524. DME1737_REG_ZONE_LOW(ix));
  525. data->zone_abs[ix] = dme1737_read(client,
  526. DME1737_REG_ZONE_ABS(ix));
  527. }
  528. for (ix = 0; ix < ARRAY_SIZE(data->zone_hyst); ix++) {
  529. data->zone_hyst[ix] = dme1737_read(client,
  530. DME1737_REG_ZONE_HYST(ix));
  531. }
  532. /* Alarm registers */
  533. data->alarms = dme1737_read(client,
  534. DME1737_REG_ALARM1);
  535. /* Bit 7 tells us if the other alarm registers are non-zero and
  536. * therefore also need to be read */
  537. if (data->alarms & 0x80) {
  538. data->alarms |= dme1737_read(client,
  539. DME1737_REG_ALARM2) << 8;
  540. data->alarms |= dme1737_read(client,
  541. DME1737_REG_ALARM3) << 16;
  542. }
  543. data->last_update = jiffies;
  544. data->valid = 1;
  545. }
  546. mutex_unlock(&data->update_lock);
  547. return data;
  548. }
  549. /* ---------------------------------------------------------------------
  550. * Voltage sysfs attributes
  551. * ix = [0-5]
  552. * --------------------------------------------------------------------- */
  553. #define SYS_IN_INPUT 0
  554. #define SYS_IN_MIN 1
  555. #define SYS_IN_MAX 2
  556. #define SYS_IN_ALARM 3
  557. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  558. char *buf)
  559. {
  560. struct dme1737_data *data = dme1737_update_device(dev);
  561. struct sensor_device_attribute_2
  562. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  563. int ix = sensor_attr_2->index;
  564. int fn = sensor_attr_2->nr;
  565. int res;
  566. switch (fn) {
  567. case SYS_IN_INPUT:
  568. res = IN_FROM_REG(data->in[ix], ix, 16);
  569. break;
  570. case SYS_IN_MIN:
  571. res = IN_FROM_REG(data->in_min[ix], ix, 8);
  572. break;
  573. case SYS_IN_MAX:
  574. res = IN_FROM_REG(data->in_max[ix], ix, 8);
  575. break;
  576. case SYS_IN_ALARM:
  577. res = (data->alarms >> DME1737_BIT_ALARM_IN[ix]) & 0x01;
  578. break;
  579. default:
  580. res = 0;
  581. dev_dbg(dev, "Unknown function %d.\n", fn);
  582. }
  583. return sprintf(buf, "%d\n", res);
  584. }
  585. static ssize_t set_in(struct device *dev, struct device_attribute *attr,
  586. const char *buf, size_t count)
  587. {
  588. struct dme1737_data *data = dev_get_drvdata(dev);
  589. struct i2c_client *client = &data->client;
  590. struct sensor_device_attribute_2
  591. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  592. int ix = sensor_attr_2->index;
  593. int fn = sensor_attr_2->nr;
  594. long val = simple_strtol(buf, NULL, 10);
  595. mutex_lock(&data->update_lock);
  596. switch (fn) {
  597. case SYS_IN_MIN:
  598. data->in_min[ix] = IN_TO_REG(val, ix);
  599. dme1737_write(client, DME1737_REG_IN_MIN(ix),
  600. data->in_min[ix]);
  601. break;
  602. case SYS_IN_MAX:
  603. data->in_max[ix] = IN_TO_REG(val, ix);
  604. dme1737_write(client, DME1737_REG_IN_MAX(ix),
  605. data->in_max[ix]);
  606. break;
  607. default:
  608. dev_dbg(dev, "Unknown function %d.\n", fn);
  609. }
  610. mutex_unlock(&data->update_lock);
  611. return count;
  612. }
  613. /* ---------------------------------------------------------------------
  614. * Temperature sysfs attributes
  615. * ix = [0-2]
  616. * --------------------------------------------------------------------- */
  617. #define SYS_TEMP_INPUT 0
  618. #define SYS_TEMP_MIN 1
  619. #define SYS_TEMP_MAX 2
  620. #define SYS_TEMP_OFFSET 3
  621. #define SYS_TEMP_ALARM 4
  622. #define SYS_TEMP_FAULT 5
  623. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  624. char *buf)
  625. {
  626. struct dme1737_data *data = dme1737_update_device(dev);
  627. struct sensor_device_attribute_2
  628. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  629. int ix = sensor_attr_2->index;
  630. int fn = sensor_attr_2->nr;
  631. int res;
  632. switch (fn) {
  633. case SYS_TEMP_INPUT:
  634. res = TEMP_FROM_REG(data->temp[ix], 16);
  635. break;
  636. case SYS_TEMP_MIN:
  637. res = TEMP_FROM_REG(data->temp_min[ix], 8);
  638. break;
  639. case SYS_TEMP_MAX:
  640. res = TEMP_FROM_REG(data->temp_max[ix], 8);
  641. break;
  642. case SYS_TEMP_OFFSET:
  643. res = TEMP_FROM_REG(data->temp_offset[ix], 8);
  644. break;
  645. case SYS_TEMP_ALARM:
  646. res = (data->alarms >> DME1737_BIT_ALARM_TEMP[ix]) & 0x01;
  647. break;
  648. case SYS_TEMP_FAULT:
  649. res = (((u16)data->temp[ix] & 0xff00) == 0x8000);
  650. break;
  651. default:
  652. res = 0;
  653. dev_dbg(dev, "Unknown function %d.\n", fn);
  654. }
  655. return sprintf(buf, "%d\n", res);
  656. }
  657. static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
  658. const char *buf, size_t count)
  659. {
  660. struct dme1737_data *data = dev_get_drvdata(dev);
  661. struct i2c_client *client = &data->client;
  662. struct sensor_device_attribute_2
  663. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  664. int ix = sensor_attr_2->index;
  665. int fn = sensor_attr_2->nr;
  666. long val = simple_strtol(buf, NULL, 10);
  667. mutex_lock(&data->update_lock);
  668. switch (fn) {
  669. case SYS_TEMP_MIN:
  670. data->temp_min[ix] = TEMP_TO_REG(val);
  671. dme1737_write(client, DME1737_REG_TEMP_MIN(ix),
  672. data->temp_min[ix]);
  673. break;
  674. case SYS_TEMP_MAX:
  675. data->temp_max[ix] = TEMP_TO_REG(val);
  676. dme1737_write(client, DME1737_REG_TEMP_MAX(ix),
  677. data->temp_max[ix]);
  678. break;
  679. case SYS_TEMP_OFFSET:
  680. data->temp_offset[ix] = TEMP_TO_REG(val);
  681. dme1737_write(client, DME1737_REG_TEMP_OFFSET(ix),
  682. data->temp_offset[ix]);
  683. break;
  684. default:
  685. dev_dbg(dev, "Unknown function %d.\n", fn);
  686. }
  687. mutex_unlock(&data->update_lock);
  688. return count;
  689. }
  690. /* ---------------------------------------------------------------------
  691. * Zone sysfs attributes
  692. * ix = [0-2]
  693. * --------------------------------------------------------------------- */
  694. #define SYS_ZONE_AUTO_CHANNELS_TEMP 0
  695. #define SYS_ZONE_AUTO_POINT1_TEMP_HYST 1
  696. #define SYS_ZONE_AUTO_POINT1_TEMP 2
  697. #define SYS_ZONE_AUTO_POINT2_TEMP 3
  698. #define SYS_ZONE_AUTO_POINT3_TEMP 4
  699. static ssize_t show_zone(struct device *dev, struct device_attribute *attr,
  700. char *buf)
  701. {
  702. struct dme1737_data *data = dme1737_update_device(dev);
  703. struct sensor_device_attribute_2
  704. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  705. int ix = sensor_attr_2->index;
  706. int fn = sensor_attr_2->nr;
  707. int res;
  708. switch (fn) {
  709. case SYS_ZONE_AUTO_CHANNELS_TEMP:
  710. /* check config2 for non-standard temp-to-zone mapping */
  711. if ((ix == 1) && (data->config2 & 0x02)) {
  712. res = 4;
  713. } else {
  714. res = 1 << ix;
  715. }
  716. break;
  717. case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
  718. res = TEMP_FROM_REG(data->zone_low[ix], 8) -
  719. TEMP_HYST_FROM_REG(data->zone_hyst[ix == 2], ix);
  720. break;
  721. case SYS_ZONE_AUTO_POINT1_TEMP:
  722. res = TEMP_FROM_REG(data->zone_low[ix], 8);
  723. break;
  724. case SYS_ZONE_AUTO_POINT2_TEMP:
  725. /* pwm_freq holds the temp range bits in the upper nibble */
  726. res = TEMP_FROM_REG(data->zone_low[ix], 8) +
  727. TEMP_RANGE_FROM_REG(data->pwm_freq[ix]);
  728. break;
  729. case SYS_ZONE_AUTO_POINT3_TEMP:
  730. res = TEMP_FROM_REG(data->zone_abs[ix], 8);
  731. break;
  732. default:
  733. res = 0;
  734. dev_dbg(dev, "Unknown function %d.\n", fn);
  735. }
  736. return sprintf(buf, "%d\n", res);
  737. }
  738. static ssize_t set_zone(struct device *dev, struct device_attribute *attr,
  739. const char *buf, size_t count)
  740. {
  741. struct dme1737_data *data = dev_get_drvdata(dev);
  742. struct i2c_client *client = &data->client;
  743. struct sensor_device_attribute_2
  744. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  745. int ix = sensor_attr_2->index;
  746. int fn = sensor_attr_2->nr;
  747. long val = simple_strtol(buf, NULL, 10);
  748. mutex_lock(&data->update_lock);
  749. switch (fn) {
  750. case SYS_ZONE_AUTO_POINT1_TEMP_HYST:
  751. /* Refresh the cache */
  752. data->zone_low[ix] = dme1737_read(client,
  753. DME1737_REG_ZONE_LOW(ix));
  754. /* Modify the temp hyst value */
  755. data->zone_hyst[ix == 2] = TEMP_HYST_TO_REG(
  756. TEMP_FROM_REG(data->zone_low[ix], 8) -
  757. val, ix, dme1737_read(client,
  758. DME1737_REG_ZONE_HYST(ix == 2)));
  759. dme1737_write(client, DME1737_REG_ZONE_HYST(ix == 2),
  760. data->zone_hyst[ix == 2]);
  761. break;
  762. case SYS_ZONE_AUTO_POINT1_TEMP:
  763. data->zone_low[ix] = TEMP_TO_REG(val);
  764. dme1737_write(client, DME1737_REG_ZONE_LOW(ix),
  765. data->zone_low[ix]);
  766. break;
  767. case SYS_ZONE_AUTO_POINT2_TEMP:
  768. /* Refresh the cache */
  769. data->zone_low[ix] = dme1737_read(client,
  770. DME1737_REG_ZONE_LOW(ix));
  771. /* Modify the temp range value (which is stored in the upper
  772. * nibble of the pwm_freq register) */
  773. data->pwm_freq[ix] = TEMP_RANGE_TO_REG(val -
  774. TEMP_FROM_REG(data->zone_low[ix], 8),
  775. dme1737_read(client,
  776. DME1737_REG_PWM_FREQ(ix)));
  777. dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
  778. data->pwm_freq[ix]);
  779. break;
  780. case SYS_ZONE_AUTO_POINT3_TEMP:
  781. data->zone_abs[ix] = TEMP_TO_REG(val);
  782. dme1737_write(client, DME1737_REG_ZONE_ABS(ix),
  783. data->zone_abs[ix]);
  784. break;
  785. default:
  786. dev_dbg(dev, "Unknown function %d.\n", fn);
  787. }
  788. mutex_unlock(&data->update_lock);
  789. return count;
  790. }
  791. /* ---------------------------------------------------------------------
  792. * Fan sysfs attributes
  793. * ix = [0-5]
  794. * --------------------------------------------------------------------- */
  795. #define SYS_FAN_INPUT 0
  796. #define SYS_FAN_MIN 1
  797. #define SYS_FAN_MAX 2
  798. #define SYS_FAN_ALARM 3
  799. #define SYS_FAN_TYPE 4
  800. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  801. char *buf)
  802. {
  803. struct dme1737_data *data = dme1737_update_device(dev);
  804. struct sensor_device_attribute_2
  805. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  806. int ix = sensor_attr_2->index;
  807. int fn = sensor_attr_2->nr;
  808. int res;
  809. switch (fn) {
  810. case SYS_FAN_INPUT:
  811. res = FAN_FROM_REG(data->fan[ix],
  812. ix < 4 ? 0 :
  813. FAN_TPC_FROM_REG(data->fan_opt[ix]));
  814. break;
  815. case SYS_FAN_MIN:
  816. res = FAN_FROM_REG(data->fan_min[ix],
  817. ix < 4 ? 0 :
  818. FAN_TPC_FROM_REG(data->fan_opt[ix]));
  819. break;
  820. case SYS_FAN_MAX:
  821. /* only valid for fan[5-6] */
  822. res = FAN_MAX_FROM_REG(data->fan_max[ix - 4]);
  823. break;
  824. case SYS_FAN_ALARM:
  825. res = (data->alarms >> DME1737_BIT_ALARM_FAN[ix]) & 0x01;
  826. break;
  827. case SYS_FAN_TYPE:
  828. /* only valid for fan[1-4] */
  829. res = FAN_TYPE_FROM_REG(data->fan_opt[ix]);
  830. break;
  831. default:
  832. res = 0;
  833. dev_dbg(dev, "Unknown function %d.\n", fn);
  834. }
  835. return sprintf(buf, "%d\n", res);
  836. }
  837. static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
  838. const char *buf, size_t count)
  839. {
  840. struct dme1737_data *data = dev_get_drvdata(dev);
  841. struct i2c_client *client = &data->client;
  842. struct sensor_device_attribute_2
  843. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  844. int ix = sensor_attr_2->index;
  845. int fn = sensor_attr_2->nr;
  846. long val = simple_strtol(buf, NULL, 10);
  847. mutex_lock(&data->update_lock);
  848. switch (fn) {
  849. case SYS_FAN_MIN:
  850. if (ix < 4) {
  851. data->fan_min[ix] = FAN_TO_REG(val, 0);
  852. } else {
  853. /* Refresh the cache */
  854. data->fan_opt[ix] = dme1737_read(client,
  855. DME1737_REG_FAN_OPT(ix));
  856. /* Modify the fan min value */
  857. data->fan_min[ix] = FAN_TO_REG(val,
  858. FAN_TPC_FROM_REG(data->fan_opt[ix]));
  859. }
  860. dme1737_write(client, DME1737_REG_FAN_MIN(ix),
  861. data->fan_min[ix] & 0xff);
  862. dme1737_write(client, DME1737_REG_FAN_MIN(ix) + 1,
  863. data->fan_min[ix] >> 8);
  864. break;
  865. case SYS_FAN_MAX:
  866. /* Only valid for fan[5-6] */
  867. data->fan_max[ix - 4] = FAN_MAX_TO_REG(val);
  868. dme1737_write(client, DME1737_REG_FAN_MAX(ix),
  869. data->fan_max[ix - 4]);
  870. break;
  871. case SYS_FAN_TYPE:
  872. /* Only valid for fan[1-4] */
  873. if (!(val == 1 || val == 2 || val == 4)) {
  874. count = -EINVAL;
  875. dev_warn(&client->dev, "Fan type value %ld not "
  876. "supported. Choose one of 1, 2, or 4.\n",
  877. val);
  878. goto exit;
  879. }
  880. data->fan_opt[ix] = FAN_TYPE_TO_REG(val, dme1737_read(client,
  881. DME1737_REG_FAN_OPT(ix)));
  882. dme1737_write(client, DME1737_REG_FAN_OPT(ix),
  883. data->fan_opt[ix]);
  884. break;
  885. default:
  886. dev_dbg(dev, "Unknown function %d.\n", fn);
  887. }
  888. exit:
  889. mutex_unlock(&data->update_lock);
  890. return count;
  891. }
  892. /* ---------------------------------------------------------------------
  893. * PWM sysfs attributes
  894. * ix = [0-4]
  895. * --------------------------------------------------------------------- */
  896. #define SYS_PWM 0
  897. #define SYS_PWM_FREQ 1
  898. #define SYS_PWM_ENABLE 2
  899. #define SYS_PWM_RAMP_RATE 3
  900. #define SYS_PWM_AUTO_CHANNELS_ZONE 4
  901. #define SYS_PWM_AUTO_PWM_MIN 5
  902. #define SYS_PWM_AUTO_POINT1_PWM 6
  903. #define SYS_PWM_AUTO_POINT2_PWM 7
  904. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  905. char *buf)
  906. {
  907. struct dme1737_data *data = dme1737_update_device(dev);
  908. struct sensor_device_attribute_2
  909. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  910. int ix = sensor_attr_2->index;
  911. int fn = sensor_attr_2->nr;
  912. int res;
  913. switch (fn) {
  914. case SYS_PWM:
  915. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 0) {
  916. res = 255;
  917. } else {
  918. res = data->pwm[ix];
  919. }
  920. break;
  921. case SYS_PWM_FREQ:
  922. res = PWM_FREQ_FROM_REG(data->pwm_freq[ix]);
  923. break;
  924. case SYS_PWM_ENABLE:
  925. if (ix > 3) {
  926. res = 1; /* pwm[5-6] hard-wired to manual mode */
  927. } else {
  928. res = PWM_EN_FROM_REG(data->pwm_config[ix]);
  929. }
  930. break;
  931. case SYS_PWM_RAMP_RATE:
  932. /* Only valid for pwm[1-3] */
  933. res = PWM_RR_FROM_REG(data->pwm_rr[ix > 0], ix);
  934. break;
  935. case SYS_PWM_AUTO_CHANNELS_ZONE:
  936. /* Only valid for pwm[1-3] */
  937. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  938. res = PWM_ACZ_FROM_REG(data->pwm_config[ix]);
  939. } else {
  940. res = data->pwm_acz[ix];
  941. }
  942. break;
  943. case SYS_PWM_AUTO_PWM_MIN:
  944. /* Only valid for pwm[1-3] */
  945. if (PWM_OFF_FROM_REG(data->pwm_rr[0], ix)) {
  946. res = data->pwm_min[ix];
  947. } else {
  948. res = 0;
  949. }
  950. break;
  951. case SYS_PWM_AUTO_POINT1_PWM:
  952. /* Only valid for pwm[1-3] */
  953. res = data->pwm_min[ix];
  954. break;
  955. case SYS_PWM_AUTO_POINT2_PWM:
  956. /* Only valid for pwm[1-3] */
  957. res = 255; /* hard-wired */
  958. break;
  959. default:
  960. res = 0;
  961. dev_dbg(dev, "Unknown function %d.\n", fn);
  962. }
  963. return sprintf(buf, "%d\n", res);
  964. }
  965. static struct attribute *dme1737_attr_pwm[];
  966. static void dme1737_chmod_file(struct device*, struct attribute*, mode_t);
  967. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  968. const char *buf, size_t count)
  969. {
  970. struct dme1737_data *data = dev_get_drvdata(dev);
  971. struct i2c_client *client = &data->client;
  972. struct sensor_device_attribute_2
  973. *sensor_attr_2 = to_sensor_dev_attr_2(attr);
  974. int ix = sensor_attr_2->index;
  975. int fn = sensor_attr_2->nr;
  976. long val = simple_strtol(buf, NULL, 10);
  977. mutex_lock(&data->update_lock);
  978. switch (fn) {
  979. case SYS_PWM:
  980. data->pwm[ix] = SENSORS_LIMIT(val, 0, 255);
  981. dme1737_write(client, DME1737_REG_PWM(ix), data->pwm[ix]);
  982. break;
  983. case SYS_PWM_FREQ:
  984. data->pwm_freq[ix] = PWM_FREQ_TO_REG(val, dme1737_read(client,
  985. DME1737_REG_PWM_FREQ(ix)));
  986. dme1737_write(client, DME1737_REG_PWM_FREQ(ix),
  987. data->pwm_freq[ix]);
  988. break;
  989. case SYS_PWM_ENABLE:
  990. /* Only valid for pwm[1-3] */
  991. if (val < 0 || val > 2) {
  992. count = -EINVAL;
  993. dev_warn(&client->dev, "PWM enable %ld not "
  994. "supported. Choose one of 0, 1, or 2.\n",
  995. val);
  996. goto exit;
  997. }
  998. /* Refresh the cache */
  999. data->pwm_config[ix] = dme1737_read(client,
  1000. DME1737_REG_PWM_CONFIG(ix));
  1001. if (val == PWM_EN_FROM_REG(data->pwm_config[ix])) {
  1002. /* Bail out if no change */
  1003. goto exit;
  1004. }
  1005. /* Do some housekeeping if we are currently in auto mode */
  1006. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1007. /* Save the current zone channel assignment */
  1008. data->pwm_acz[ix] = PWM_ACZ_FROM_REG(
  1009. data->pwm_config[ix]);
  1010. /* Save the current ramp rate state and disable it */
  1011. data->pwm_rr[ix > 0] = dme1737_read(client,
  1012. DME1737_REG_PWM_RR(ix > 0));
  1013. data->pwm_rr_en &= ~(1 << ix);
  1014. if (PWM_RR_EN_FROM_REG(data->pwm_rr[ix > 0], ix)) {
  1015. data->pwm_rr_en |= (1 << ix);
  1016. data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(0, ix,
  1017. data->pwm_rr[ix > 0]);
  1018. dme1737_write(client,
  1019. DME1737_REG_PWM_RR(ix > 0),
  1020. data->pwm_rr[ix > 0]);
  1021. }
  1022. }
  1023. /* Set the new PWM mode */
  1024. switch (val) {
  1025. case 0:
  1026. /* Change permissions of pwm[ix] to read-only */
  1027. dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
  1028. S_IRUGO);
  1029. /* Turn fan fully on */
  1030. data->pwm_config[ix] = PWM_EN_TO_REG(0,
  1031. data->pwm_config[ix]);
  1032. dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
  1033. data->pwm_config[ix]);
  1034. break;
  1035. case 1:
  1036. /* Turn on manual mode */
  1037. data->pwm_config[ix] = PWM_EN_TO_REG(1,
  1038. data->pwm_config[ix]);
  1039. dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
  1040. data->pwm_config[ix]);
  1041. /* Change permissions of pwm[ix] to read-writeable */
  1042. dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
  1043. S_IRUGO | S_IWUSR);
  1044. break;
  1045. case 2:
  1046. /* Change permissions of pwm[ix] to read-only */
  1047. dme1737_chmod_file(dev, dme1737_attr_pwm[ix],
  1048. S_IRUGO);
  1049. /* Turn on auto mode using the saved zone channel
  1050. * assignment */
  1051. data->pwm_config[ix] = PWM_ACZ_TO_REG(
  1052. data->pwm_acz[ix],
  1053. data->pwm_config[ix]);
  1054. dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
  1055. data->pwm_config[ix]);
  1056. /* Enable PWM ramp rate if previously enabled */
  1057. if (data->pwm_rr_en & (1 << ix)) {
  1058. data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(1, ix,
  1059. dme1737_read(client,
  1060. DME1737_REG_PWM_RR(ix > 0)));
  1061. dme1737_write(client,
  1062. DME1737_REG_PWM_RR(ix > 0),
  1063. data->pwm_rr[ix > 0]);
  1064. }
  1065. break;
  1066. }
  1067. break;
  1068. case SYS_PWM_RAMP_RATE:
  1069. /* Only valid for pwm[1-3] */
  1070. /* Refresh the cache */
  1071. data->pwm_config[ix] = dme1737_read(client,
  1072. DME1737_REG_PWM_CONFIG(ix));
  1073. data->pwm_rr[ix > 0] = dme1737_read(client,
  1074. DME1737_REG_PWM_RR(ix > 0));
  1075. /* Set the ramp rate value */
  1076. if (val > 0) {
  1077. data->pwm_rr[ix > 0] = PWM_RR_TO_REG(val, ix,
  1078. data->pwm_rr[ix > 0]);
  1079. }
  1080. /* Enable/disable the feature only if the associated PWM
  1081. * output is in automatic mode. */
  1082. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1083. data->pwm_rr[ix > 0] = PWM_RR_EN_TO_REG(val > 0, ix,
  1084. data->pwm_rr[ix > 0]);
  1085. }
  1086. dme1737_write(client, DME1737_REG_PWM_RR(ix > 0),
  1087. data->pwm_rr[ix > 0]);
  1088. break;
  1089. case SYS_PWM_AUTO_CHANNELS_ZONE:
  1090. /* Only valid for pwm[1-3] */
  1091. if (!(val == 1 || val == 2 || val == 4 ||
  1092. val == 6 || val == 7)) {
  1093. count = -EINVAL;
  1094. dev_warn(&client->dev, "PWM auto channels zone %ld "
  1095. "not supported. Choose one of 1, 2, 4, 6, "
  1096. "or 7.\n", val);
  1097. goto exit;
  1098. }
  1099. /* Refresh the cache */
  1100. data->pwm_config[ix] = dme1737_read(client,
  1101. DME1737_REG_PWM_CONFIG(ix));
  1102. if (PWM_EN_FROM_REG(data->pwm_config[ix]) == 2) {
  1103. /* PWM is already in auto mode so update the temp
  1104. * channel assignment */
  1105. data->pwm_config[ix] = PWM_ACZ_TO_REG(val,
  1106. data->pwm_config[ix]);
  1107. dme1737_write(client, DME1737_REG_PWM_CONFIG(ix),
  1108. data->pwm_config[ix]);
  1109. } else {
  1110. /* PWM is not in auto mode so we save the temp
  1111. * channel assignment for later use */
  1112. data->pwm_acz[ix] = val;
  1113. }
  1114. break;
  1115. case SYS_PWM_AUTO_PWM_MIN:
  1116. /* Only valid for pwm[1-3] */
  1117. /* Refresh the cache */
  1118. data->pwm_min[ix] = dme1737_read(client,
  1119. DME1737_REG_PWM_MIN(ix));
  1120. /* There are only 2 values supported for the auto_pwm_min
  1121. * value: 0 or auto_point1_pwm. So if the temperature drops
  1122. * below the auto_point1_temp_hyst value, the fan either turns
  1123. * off or runs at auto_point1_pwm duty-cycle. */
  1124. if (val > ((data->pwm_min[ix] + 1) / 2)) {
  1125. data->pwm_rr[0] = PWM_OFF_TO_REG(1, ix,
  1126. dme1737_read(client,
  1127. DME1737_REG_PWM_RR(0)));
  1128. } else {
  1129. data->pwm_rr[0] = PWM_OFF_TO_REG(0, ix,
  1130. dme1737_read(client,
  1131. DME1737_REG_PWM_RR(0)));
  1132. }
  1133. dme1737_write(client, DME1737_REG_PWM_RR(0),
  1134. data->pwm_rr[0]);
  1135. break;
  1136. case SYS_PWM_AUTO_POINT1_PWM:
  1137. /* Only valid for pwm[1-3] */
  1138. data->pwm_min[ix] = SENSORS_LIMIT(val, 0, 255);
  1139. dme1737_write(client, DME1737_REG_PWM_MIN(ix),
  1140. data->pwm_min[ix]);
  1141. break;
  1142. default:
  1143. dev_dbg(dev, "Unknown function %d.\n", fn);
  1144. }
  1145. exit:
  1146. mutex_unlock(&data->update_lock);
  1147. return count;
  1148. }
  1149. /* ---------------------------------------------------------------------
  1150. * Miscellaneous sysfs attributes
  1151. * --------------------------------------------------------------------- */
  1152. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  1153. char *buf)
  1154. {
  1155. struct i2c_client *client = to_i2c_client(dev);
  1156. struct dme1737_data *data = i2c_get_clientdata(client);
  1157. return sprintf(buf, "%d\n", data->vrm);
  1158. }
  1159. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  1160. const char *buf, size_t count)
  1161. {
  1162. struct dme1737_data *data = dev_get_drvdata(dev);
  1163. long val = simple_strtol(buf, NULL, 10);
  1164. data->vrm = val;
  1165. return count;
  1166. }
  1167. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  1168. char *buf)
  1169. {
  1170. struct dme1737_data *data = dme1737_update_device(dev);
  1171. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  1172. }
  1173. /* ---------------------------------------------------------------------
  1174. * Sysfs device attribute defines and structs
  1175. * --------------------------------------------------------------------- */
  1176. /* Voltages 0-6 */
  1177. #define SENSOR_DEVICE_ATTR_IN(ix) \
  1178. static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
  1179. show_in, NULL, SYS_IN_INPUT, ix); \
  1180. static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
  1181. show_in, set_in, SYS_IN_MIN, ix); \
  1182. static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
  1183. show_in, set_in, SYS_IN_MAX, ix); \
  1184. static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
  1185. show_in, NULL, SYS_IN_ALARM, ix)
  1186. SENSOR_DEVICE_ATTR_IN(0);
  1187. SENSOR_DEVICE_ATTR_IN(1);
  1188. SENSOR_DEVICE_ATTR_IN(2);
  1189. SENSOR_DEVICE_ATTR_IN(3);
  1190. SENSOR_DEVICE_ATTR_IN(4);
  1191. SENSOR_DEVICE_ATTR_IN(5);
  1192. SENSOR_DEVICE_ATTR_IN(6);
  1193. /* Temperatures 1-3 */
  1194. #define SENSOR_DEVICE_ATTR_TEMP(ix) \
  1195. static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
  1196. show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
  1197. static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
  1198. show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
  1199. static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
  1200. show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
  1201. static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
  1202. show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
  1203. static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
  1204. show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
  1205. static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
  1206. show_temp, NULL, SYS_TEMP_FAULT, ix-1)
  1207. SENSOR_DEVICE_ATTR_TEMP(1);
  1208. SENSOR_DEVICE_ATTR_TEMP(2);
  1209. SENSOR_DEVICE_ATTR_TEMP(3);
  1210. /* Zones 1-3 */
  1211. #define SENSOR_DEVICE_ATTR_ZONE(ix) \
  1212. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
  1213. show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
  1214. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
  1215. show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
  1216. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
  1217. show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
  1218. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
  1219. show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
  1220. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
  1221. show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
  1222. SENSOR_DEVICE_ATTR_ZONE(1);
  1223. SENSOR_DEVICE_ATTR_ZONE(2);
  1224. SENSOR_DEVICE_ATTR_ZONE(3);
  1225. /* Fans 1-4 */
  1226. #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
  1227. static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
  1228. show_fan, NULL, SYS_FAN_INPUT, ix-1); \
  1229. static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  1230. show_fan, set_fan, SYS_FAN_MIN, ix-1); \
  1231. static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  1232. show_fan, NULL, SYS_FAN_ALARM, ix-1); \
  1233. static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
  1234. show_fan, set_fan, SYS_FAN_TYPE, ix-1)
  1235. SENSOR_DEVICE_ATTR_FAN_1TO4(1);
  1236. SENSOR_DEVICE_ATTR_FAN_1TO4(2);
  1237. SENSOR_DEVICE_ATTR_FAN_1TO4(3);
  1238. SENSOR_DEVICE_ATTR_FAN_1TO4(4);
  1239. /* Fans 5-6 */
  1240. #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
  1241. static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
  1242. show_fan, NULL, SYS_FAN_INPUT, ix-1); \
  1243. static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  1244. show_fan, set_fan, SYS_FAN_MIN, ix-1); \
  1245. static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  1246. show_fan, NULL, SYS_FAN_ALARM, ix-1); \
  1247. static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
  1248. show_fan, set_fan, SYS_FAN_MAX, ix-1)
  1249. SENSOR_DEVICE_ATTR_FAN_5TO6(5);
  1250. SENSOR_DEVICE_ATTR_FAN_5TO6(6);
  1251. /* PWMs 1-3 */
  1252. #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
  1253. static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
  1254. show_pwm, set_pwm, SYS_PWM, ix-1); \
  1255. static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
  1256. show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
  1257. static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
  1258. show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
  1259. static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
  1260. show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
  1261. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
  1262. show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
  1263. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
  1264. show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
  1265. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
  1266. show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
  1267. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
  1268. show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
  1269. SENSOR_DEVICE_ATTR_PWM_1TO3(1);
  1270. SENSOR_DEVICE_ATTR_PWM_1TO3(2);
  1271. SENSOR_DEVICE_ATTR_PWM_1TO3(3);
  1272. /* PWMs 5-6 */
  1273. #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
  1274. static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO | S_IWUSR, \
  1275. show_pwm, set_pwm, SYS_PWM, ix-1); \
  1276. static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
  1277. show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
  1278. static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
  1279. show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
  1280. SENSOR_DEVICE_ATTR_PWM_5TO6(5);
  1281. SENSOR_DEVICE_ATTR_PWM_5TO6(6);
  1282. /* Misc */
  1283. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  1284. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  1285. #define SENSOR_DEV_ATTR_IN(ix) \
  1286. &sensor_dev_attr_in##ix##_input.dev_attr.attr, \
  1287. &sensor_dev_attr_in##ix##_min.dev_attr.attr, \
  1288. &sensor_dev_attr_in##ix##_max.dev_attr.attr, \
  1289. &sensor_dev_attr_in##ix##_alarm.dev_attr.attr
  1290. /* These attributes are read-writeable only if the chip is *not* locked */
  1291. #define SENSOR_DEV_ATTR_TEMP_LOCK(ix) \
  1292. &sensor_dev_attr_temp##ix##_offset.dev_attr.attr
  1293. #define SENSOR_DEV_ATTR_TEMP(ix) \
  1294. SENSOR_DEV_ATTR_TEMP_LOCK(ix), \
  1295. &sensor_dev_attr_temp##ix##_input.dev_attr.attr, \
  1296. &sensor_dev_attr_temp##ix##_min.dev_attr.attr, \
  1297. &sensor_dev_attr_temp##ix##_max.dev_attr.attr, \
  1298. &sensor_dev_attr_temp##ix##_alarm.dev_attr.attr, \
  1299. &sensor_dev_attr_temp##ix##_fault.dev_attr.attr
  1300. /* These attributes are read-writeable only if the chip is *not* locked */
  1301. #define SENSOR_DEV_ATTR_ZONE_LOCK(ix) \
  1302. &sensor_dev_attr_zone##ix##_auto_point1_temp_hyst.dev_attr.attr, \
  1303. &sensor_dev_attr_zone##ix##_auto_point1_temp.dev_attr.attr, \
  1304. &sensor_dev_attr_zone##ix##_auto_point2_temp.dev_attr.attr, \
  1305. &sensor_dev_attr_zone##ix##_auto_point3_temp.dev_attr.attr
  1306. #define SENSOR_DEV_ATTR_ZONE(ix) \
  1307. SENSOR_DEV_ATTR_ZONE_LOCK(ix), \
  1308. &sensor_dev_attr_zone##ix##_auto_channels_temp.dev_attr.attr
  1309. #define SENSOR_DEV_ATTR_FAN_1TO4(ix) \
  1310. &sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
  1311. &sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
  1312. &sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
  1313. &sensor_dev_attr_fan##ix##_type.dev_attr.attr
  1314. #define SENSOR_DEV_ATTR_FAN_5TO6(ix) \
  1315. &sensor_dev_attr_fan##ix##_input.dev_attr.attr, \
  1316. &sensor_dev_attr_fan##ix##_min.dev_attr.attr, \
  1317. &sensor_dev_attr_fan##ix##_alarm.dev_attr.attr, \
  1318. &sensor_dev_attr_fan##ix##_max.dev_attr.attr
  1319. /* These attributes are read-writeable only if the chip is *not* locked */
  1320. #define SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix) \
  1321. &sensor_dev_attr_pwm##ix##_freq.dev_attr.attr, \
  1322. &sensor_dev_attr_pwm##ix##_enable.dev_attr.attr, \
  1323. &sensor_dev_attr_pwm##ix##_ramp_rate.dev_attr.attr, \
  1324. &sensor_dev_attr_pwm##ix##_auto_channels_zone.dev_attr.attr, \
  1325. &sensor_dev_attr_pwm##ix##_auto_pwm_min.dev_attr.attr, \
  1326. &sensor_dev_attr_pwm##ix##_auto_point1_pwm.dev_attr.attr
  1327. #define SENSOR_DEV_ATTR_PWM_1TO3(ix) \
  1328. SENSOR_DEV_ATTR_PWM_1TO3_LOCK(ix), \
  1329. &sensor_dev_attr_pwm##ix.dev_attr.attr, \
  1330. &sensor_dev_attr_pwm##ix##_auto_point2_pwm.dev_attr.attr
  1331. /* These attributes are read-writeable only if the chip is *not* locked */
  1332. #define SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix) \
  1333. &sensor_dev_attr_pwm##ix.dev_attr.attr, \
  1334. &sensor_dev_attr_pwm##ix##_freq.dev_attr.attr
  1335. #define SENSOR_DEV_ATTR_PWM_5TO6(ix) \
  1336. SENSOR_DEV_ATTR_PWM_5TO6_LOCK(ix), \
  1337. &sensor_dev_attr_pwm##ix##_enable.dev_attr.attr
  1338. /* This struct holds all the attributes that are always present and need to be
  1339. * created unconditionally. The attributes that need modification of their
  1340. * permissions are created read-only and write permissions are added or removed
  1341. * on the fly when required */
  1342. static struct attribute *dme1737_attr[] ={
  1343. /* Voltages */
  1344. SENSOR_DEV_ATTR_IN(0),
  1345. SENSOR_DEV_ATTR_IN(1),
  1346. SENSOR_DEV_ATTR_IN(2),
  1347. SENSOR_DEV_ATTR_IN(3),
  1348. SENSOR_DEV_ATTR_IN(4),
  1349. SENSOR_DEV_ATTR_IN(5),
  1350. SENSOR_DEV_ATTR_IN(6),
  1351. /* Temperatures */
  1352. SENSOR_DEV_ATTR_TEMP(1),
  1353. SENSOR_DEV_ATTR_TEMP(2),
  1354. SENSOR_DEV_ATTR_TEMP(3),
  1355. /* Zones */
  1356. SENSOR_DEV_ATTR_ZONE(1),
  1357. SENSOR_DEV_ATTR_ZONE(2),
  1358. SENSOR_DEV_ATTR_ZONE(3),
  1359. /* Misc */
  1360. &dev_attr_vrm.attr,
  1361. &dev_attr_cpu0_vid.attr,
  1362. NULL
  1363. };
  1364. static const struct attribute_group dme1737_group = {
  1365. .attrs = dme1737_attr,
  1366. };
  1367. /* The following structs hold the PWM attributes, some of which are optional.
  1368. * Their creation depends on the chip configuration which is determined during
  1369. * module load. */
  1370. static struct attribute *dme1737_attr_pwm1[] = {
  1371. SENSOR_DEV_ATTR_PWM_1TO3(1),
  1372. NULL
  1373. };
  1374. static struct attribute *dme1737_attr_pwm2[] = {
  1375. SENSOR_DEV_ATTR_PWM_1TO3(2),
  1376. NULL
  1377. };
  1378. static struct attribute *dme1737_attr_pwm3[] = {
  1379. SENSOR_DEV_ATTR_PWM_1TO3(3),
  1380. NULL
  1381. };
  1382. static struct attribute *dme1737_attr_pwm5[] = {
  1383. SENSOR_DEV_ATTR_PWM_5TO6(5),
  1384. NULL
  1385. };
  1386. static struct attribute *dme1737_attr_pwm6[] = {
  1387. SENSOR_DEV_ATTR_PWM_5TO6(6),
  1388. NULL
  1389. };
  1390. static const struct attribute_group dme1737_pwm_group[] = {
  1391. { .attrs = dme1737_attr_pwm1 },
  1392. { .attrs = dme1737_attr_pwm2 },
  1393. { .attrs = dme1737_attr_pwm3 },
  1394. { .attrs = NULL },
  1395. { .attrs = dme1737_attr_pwm5 },
  1396. { .attrs = dme1737_attr_pwm6 },
  1397. };
  1398. /* The following structs hold the fan attributes, some of which are optional.
  1399. * Their creation depends on the chip configuration which is determined during
  1400. * module load. */
  1401. static struct attribute *dme1737_attr_fan1[] = {
  1402. SENSOR_DEV_ATTR_FAN_1TO4(1),
  1403. NULL
  1404. };
  1405. static struct attribute *dme1737_attr_fan2[] = {
  1406. SENSOR_DEV_ATTR_FAN_1TO4(2),
  1407. NULL
  1408. };
  1409. static struct attribute *dme1737_attr_fan3[] = {
  1410. SENSOR_DEV_ATTR_FAN_1TO4(3),
  1411. NULL
  1412. };
  1413. static struct attribute *dme1737_attr_fan4[] = {
  1414. SENSOR_DEV_ATTR_FAN_1TO4(4),
  1415. NULL
  1416. };
  1417. static struct attribute *dme1737_attr_fan5[] = {
  1418. SENSOR_DEV_ATTR_FAN_5TO6(5),
  1419. NULL
  1420. };
  1421. static struct attribute *dme1737_attr_fan6[] = {
  1422. SENSOR_DEV_ATTR_FAN_5TO6(6),
  1423. NULL
  1424. };
  1425. static const struct attribute_group dme1737_fan_group[] = {
  1426. { .attrs = dme1737_attr_fan1 },
  1427. { .attrs = dme1737_attr_fan2 },
  1428. { .attrs = dme1737_attr_fan3 },
  1429. { .attrs = dme1737_attr_fan4 },
  1430. { .attrs = dme1737_attr_fan5 },
  1431. { .attrs = dme1737_attr_fan6 },
  1432. };
  1433. /* The permissions of all of the following attributes are changed to read-
  1434. * writeable if the chip is *not* locked. Otherwise they stay read-only. */
  1435. static struct attribute *dme1737_attr_lock[] = {
  1436. /* Temperatures */
  1437. SENSOR_DEV_ATTR_TEMP_LOCK(1),
  1438. SENSOR_DEV_ATTR_TEMP_LOCK(2),
  1439. SENSOR_DEV_ATTR_TEMP_LOCK(3),
  1440. /* Zones */
  1441. SENSOR_DEV_ATTR_ZONE_LOCK(1),
  1442. SENSOR_DEV_ATTR_ZONE_LOCK(2),
  1443. SENSOR_DEV_ATTR_ZONE_LOCK(3),
  1444. NULL
  1445. };
  1446. static const struct attribute_group dme1737_lock_group = {
  1447. .attrs = dme1737_attr_lock,
  1448. };
  1449. /* The permissions of the following PWM attributes are changed to read-
  1450. * writeable if the chip is *not* locked and the respective PWM is available.
  1451. * Otherwise they stay read-only. */
  1452. static struct attribute *dme1737_attr_pwm1_lock[] = {
  1453. SENSOR_DEV_ATTR_PWM_1TO3_LOCK(1),
  1454. NULL
  1455. };
  1456. static struct attribute *dme1737_attr_pwm2_lock[] = {
  1457. SENSOR_DEV_ATTR_PWM_1TO3_LOCK(2),
  1458. NULL
  1459. };
  1460. static struct attribute *dme1737_attr_pwm3_lock[] = {
  1461. SENSOR_DEV_ATTR_PWM_1TO3_LOCK(3),
  1462. NULL
  1463. };
  1464. static struct attribute *dme1737_attr_pwm5_lock[] = {
  1465. SENSOR_DEV_ATTR_PWM_5TO6_LOCK(5),
  1466. NULL
  1467. };
  1468. static struct attribute *dme1737_attr_pwm6_lock[] = {
  1469. SENSOR_DEV_ATTR_PWM_5TO6_LOCK(6),
  1470. NULL
  1471. };
  1472. static const struct attribute_group dme1737_pwm_lock_group[] = {
  1473. { .attrs = dme1737_attr_pwm1_lock },
  1474. { .attrs = dme1737_attr_pwm2_lock },
  1475. { .attrs = dme1737_attr_pwm3_lock },
  1476. { .attrs = NULL },
  1477. { .attrs = dme1737_attr_pwm5_lock },
  1478. { .attrs = dme1737_attr_pwm6_lock },
  1479. };
  1480. /* Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
  1481. * chip is not locked. Otherwise they are read-only. */
  1482. static struct attribute *dme1737_attr_pwm[] = {
  1483. &sensor_dev_attr_pwm1.dev_attr.attr,
  1484. &sensor_dev_attr_pwm2.dev_attr.attr,
  1485. &sensor_dev_attr_pwm3.dev_attr.attr,
  1486. };
  1487. /* ---------------------------------------------------------------------
  1488. * Super-IO functions
  1489. * --------------------------------------------------------------------- */
  1490. static inline void dme1737_sio_enter(int sio_cip)
  1491. {
  1492. outb(0x55, sio_cip);
  1493. }
  1494. static inline void dme1737_sio_exit(int sio_cip)
  1495. {
  1496. outb(0xaa, sio_cip);
  1497. }
  1498. static inline int dme1737_sio_inb(int sio_cip, int reg)
  1499. {
  1500. outb(reg, sio_cip);
  1501. return inb(sio_cip + 1);
  1502. }
  1503. static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
  1504. {
  1505. outb(reg, sio_cip);
  1506. outb(val, sio_cip + 1);
  1507. }
  1508. /* ---------------------------------------------------------------------
  1509. * Device detection, registration and initialization
  1510. * --------------------------------------------------------------------- */
  1511. static int dme1737_i2c_get_features(int, struct dme1737_data*);
  1512. static void dme1737_chmod_file(struct device *dev,
  1513. struct attribute *attr, mode_t mode)
  1514. {
  1515. if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
  1516. dev_warn(dev, "Failed to change permissions of %s.\n",
  1517. attr->name);
  1518. }
  1519. }
  1520. static void dme1737_chmod_group(struct device *dev,
  1521. const struct attribute_group *group,
  1522. mode_t mode)
  1523. {
  1524. struct attribute **attr;
  1525. for (attr = group->attrs; *attr; attr++) {
  1526. dme1737_chmod_file(dev, *attr, mode);
  1527. }
  1528. }
  1529. static void dme1737_remove_files(struct device *dev)
  1530. {
  1531. struct dme1737_data *data = dev_get_drvdata(dev);
  1532. int ix;
  1533. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1534. if (data->has_fan & (1 << ix)) {
  1535. sysfs_remove_group(&dev->kobj,
  1536. &dme1737_fan_group[ix]);
  1537. }
  1538. }
  1539. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1540. if (data->has_pwm & (1 << ix)) {
  1541. sysfs_remove_group(&dev->kobj,
  1542. &dme1737_pwm_group[ix]);
  1543. }
  1544. }
  1545. sysfs_remove_group(&dev->kobj, &dme1737_group);
  1546. }
  1547. static int dme1737_create_files(struct device *dev)
  1548. {
  1549. struct dme1737_data *data = dev_get_drvdata(dev);
  1550. int err, ix;
  1551. /* Create standard sysfs attributes */
  1552. if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
  1553. goto exit;
  1554. }
  1555. /* Create fan sysfs attributes */
  1556. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1557. if (data->has_fan & (1 << ix)) {
  1558. if ((err = sysfs_create_group(&dev->kobj,
  1559. &dme1737_fan_group[ix]))) {
  1560. goto exit_remove;
  1561. }
  1562. }
  1563. }
  1564. /* Create PWM sysfs attributes */
  1565. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1566. if (data->has_pwm & (1 << ix)) {
  1567. if ((err = sysfs_create_group(&dev->kobj,
  1568. &dme1737_pwm_group[ix]))) {
  1569. goto exit_remove;
  1570. }
  1571. }
  1572. }
  1573. /* Inform if the device is locked. Otherwise change the permissions of
  1574. * selected attributes from read-only to read-writeable. */
  1575. if (data->config & 0x02) {
  1576. dev_info(dev, "Device is locked. Some attributes "
  1577. "will be read-only.\n");
  1578. } else {
  1579. /* Change permissions of standard attributes */
  1580. dme1737_chmod_group(dev, &dme1737_lock_group,
  1581. S_IRUGO | S_IWUSR);
  1582. /* Change permissions of PWM attributes */
  1583. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
  1584. if (data->has_pwm & (1 << ix)) {
  1585. dme1737_chmod_group(dev,
  1586. &dme1737_pwm_lock_group[ix],
  1587. S_IRUGO | S_IWUSR);
  1588. }
  1589. }
  1590. /* Change permissions of pwm[1-3] if in manual mode */
  1591. for (ix = 0; ix < 3; ix++) {
  1592. if ((data->has_pwm & (1 << ix)) &&
  1593. (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
  1594. dme1737_chmod_file(dev,
  1595. dme1737_attr_pwm[ix],
  1596. S_IRUGO | S_IWUSR);
  1597. }
  1598. }
  1599. }
  1600. return 0;
  1601. exit_remove:
  1602. dme1737_remove_files(dev);
  1603. exit:
  1604. return err;
  1605. }
  1606. static int dme1737_init_device(struct device *dev)
  1607. {
  1608. struct dme1737_data *data = dev_get_drvdata(dev);
  1609. struct i2c_client *client = &data->client;
  1610. int ix;
  1611. u8 reg;
  1612. data->config = dme1737_read(client, DME1737_REG_CONFIG);
  1613. /* Inform if part is not monitoring/started */
  1614. if (!(data->config & 0x01)) {
  1615. if (!force_start) {
  1616. dev_err(dev, "Device is not monitoring. "
  1617. "Use the force_start load parameter to "
  1618. "override.\n");
  1619. return -EFAULT;
  1620. }
  1621. /* Force monitoring */
  1622. data->config |= 0x01;
  1623. dme1737_write(client, DME1737_REG_CONFIG, data->config);
  1624. }
  1625. /* Inform if part is not ready */
  1626. if (!(data->config & 0x04)) {
  1627. dev_err(dev, "Device is not ready.\n");
  1628. return -EFAULT;
  1629. }
  1630. data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
  1631. /* Check if optional fan3 input is enabled */
  1632. if (data->config2 & 0x04) {
  1633. data->has_fan |= (1 << 2);
  1634. }
  1635. /* Fan4 and pwm3 are only available if the client's I2C address
  1636. * is the default 0x2e. Otherwise the I/Os associated with these
  1637. * functions are used for addr enable/select. */
  1638. if (client->addr == 0x2e) {
  1639. data->has_fan |= (1 << 3);
  1640. data->has_pwm |= (1 << 2);
  1641. }
  1642. /* Determine if the optional fan[5-6] and/or pwm[5-6] are enabled.
  1643. * For this, we need to query the runtime registers through the
  1644. * Super-IO LPC interface. Try both config ports 0x2e and 0x4e. */
  1645. if (dme1737_i2c_get_features(0x2e, data) &&
  1646. dme1737_i2c_get_features(0x4e, data)) {
  1647. dev_warn(dev, "Failed to query Super-IO for optional "
  1648. "features.\n");
  1649. }
  1650. /* Fan1, fan2, pwm1, and pwm2 are always present */
  1651. data->has_fan |= 0x03;
  1652. data->has_pwm |= 0x03;
  1653. dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
  1654. "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
  1655. (data->has_pwm & (1 << 2)) ? "yes" : "no",
  1656. (data->has_pwm & (1 << 4)) ? "yes" : "no",
  1657. (data->has_pwm & (1 << 5)) ? "yes" : "no",
  1658. (data->has_fan & (1 << 2)) ? "yes" : "no",
  1659. (data->has_fan & (1 << 3)) ? "yes" : "no",
  1660. (data->has_fan & (1 << 4)) ? "yes" : "no",
  1661. (data->has_fan & (1 << 5)) ? "yes" : "no");
  1662. reg = dme1737_read(client, DME1737_REG_TACH_PWM);
  1663. /* Inform if fan-to-pwm mapping differs from the default */
  1664. if (reg != 0xa4) {
  1665. dev_warn(dev, "Non-standard fan to pwm mapping: "
  1666. "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
  1667. "fan4->pwm%d. Please report to the driver "
  1668. "maintainer.\n",
  1669. (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
  1670. ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
  1671. }
  1672. /* Switch pwm[1-3] to manual mode if they are currently disabled and
  1673. * set the duty-cycles to 0% (which is identical to the PWMs being
  1674. * disabled). */
  1675. if (!(data->config & 0x02)) {
  1676. for (ix = 0; ix < 3; ix++) {
  1677. data->pwm_config[ix] = dme1737_read(client,
  1678. DME1737_REG_PWM_CONFIG(ix));
  1679. if ((data->has_pwm & (1 << ix)) &&
  1680. (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
  1681. dev_info(dev, "Switching pwm%d to "
  1682. "manual mode.\n", ix + 1);
  1683. data->pwm_config[ix] = PWM_EN_TO_REG(1,
  1684. data->pwm_config[ix]);
  1685. dme1737_write(client, DME1737_REG_PWM(ix), 0);
  1686. dme1737_write(client,
  1687. DME1737_REG_PWM_CONFIG(ix),
  1688. data->pwm_config[ix]);
  1689. }
  1690. }
  1691. }
  1692. /* Initialize the default PWM auto channels zone (acz) assignments */
  1693. data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
  1694. data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
  1695. data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
  1696. /* Set VRM */
  1697. data->vrm = vid_which_vrm();
  1698. return 0;
  1699. }
  1700. /* ---------------------------------------------------------------------
  1701. * I2C device detection and registration
  1702. * --------------------------------------------------------------------- */
  1703. static struct i2c_driver dme1737_i2c_driver;
  1704. static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
  1705. {
  1706. int err = 0, reg;
  1707. u16 addr;
  1708. dme1737_sio_enter(sio_cip);
  1709. /* Check device ID
  1710. * The DME1737 can return either 0x78 or 0x77 as its device ID. */
  1711. reg = dme1737_sio_inb(sio_cip, 0x20);
  1712. if (!(reg == 0x77 || reg == 0x78)) {
  1713. err = -ENODEV;
  1714. goto exit;
  1715. }
  1716. /* Select logical device A (runtime registers) */
  1717. dme1737_sio_outb(sio_cip, 0x07, 0x0a);
  1718. /* Get the base address of the runtime registers */
  1719. if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
  1720. dme1737_sio_inb(sio_cip, 0x61))) {
  1721. err = -ENODEV;
  1722. goto exit;
  1723. }
  1724. /* Read the runtime registers to determine which optional features
  1725. * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
  1726. * to '10' if the respective feature is enabled. */
  1727. if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
  1728. data->has_fan |= (1 << 5);
  1729. }
  1730. if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
  1731. data->has_pwm |= (1 << 5);
  1732. }
  1733. if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
  1734. data->has_fan |= (1 << 4);
  1735. }
  1736. if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
  1737. data->has_pwm |= (1 << 4);
  1738. }
  1739. exit:
  1740. dme1737_sio_exit(sio_cip);
  1741. return err;
  1742. }
  1743. static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
  1744. int kind)
  1745. {
  1746. u8 company, verstep = 0;
  1747. struct i2c_client *client;
  1748. struct dme1737_data *data;
  1749. struct device *dev;
  1750. int err = 0;
  1751. const char *name;
  1752. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1753. goto exit;
  1754. }
  1755. if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
  1756. err = -ENOMEM;
  1757. goto exit;
  1758. }
  1759. client = &data->client;
  1760. i2c_set_clientdata(client, data);
  1761. client->addr = address;
  1762. client->adapter = adapter;
  1763. client->driver = &dme1737_i2c_driver;
  1764. dev = &client->dev;
  1765. /* A negative kind means that the driver was loaded with no force
  1766. * parameter (default), so we must identify the chip. */
  1767. if (kind < 0) {
  1768. company = dme1737_read(client, DME1737_REG_COMPANY);
  1769. verstep = dme1737_read(client, DME1737_REG_VERSTEP);
  1770. if (!((company == DME1737_COMPANY_SMSC) &&
  1771. ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
  1772. err = -ENODEV;
  1773. goto exit_kfree;
  1774. }
  1775. }
  1776. kind = dme1737;
  1777. name = "dme1737";
  1778. /* Fill in the remaining client fields and put it into the global
  1779. * list */
  1780. strlcpy(client->name, name, I2C_NAME_SIZE);
  1781. mutex_init(&data->update_lock);
  1782. /* Tell the I2C layer a new client has arrived */
  1783. if ((err = i2c_attach_client(client))) {
  1784. goto exit_kfree;
  1785. }
  1786. dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
  1787. client->addr, verstep);
  1788. /* Initialize the DME1737 chip */
  1789. if ((err = dme1737_init_device(dev))) {
  1790. dev_err(dev, "Failed to initialize device.\n");
  1791. goto exit_detach;
  1792. }
  1793. /* Create sysfs files */
  1794. if ((err = dme1737_create_files(dev))) {
  1795. dev_err(dev, "Failed to create sysfs files.\n");
  1796. goto exit_detach;
  1797. }
  1798. /* Register device */
  1799. data->class_dev = hwmon_device_register(dev);
  1800. if (IS_ERR(data->class_dev)) {
  1801. dev_err(dev, "Failed to register device.\n");
  1802. err = PTR_ERR(data->class_dev);
  1803. goto exit_remove;
  1804. }
  1805. return 0;
  1806. exit_remove:
  1807. dme1737_remove_files(dev);
  1808. exit_detach:
  1809. i2c_detach_client(client);
  1810. exit_kfree:
  1811. kfree(data);
  1812. exit:
  1813. return err;
  1814. }
  1815. static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
  1816. {
  1817. if (!(adapter->class & I2C_CLASS_HWMON)) {
  1818. return 0;
  1819. }
  1820. return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
  1821. }
  1822. static int dme1737_i2c_detach_client(struct i2c_client *client)
  1823. {
  1824. struct dme1737_data *data = i2c_get_clientdata(client);
  1825. int err;
  1826. hwmon_device_unregister(data->class_dev);
  1827. dme1737_remove_files(&client->dev);
  1828. if ((err = i2c_detach_client(client))) {
  1829. return err;
  1830. }
  1831. kfree(data);
  1832. return 0;
  1833. }
  1834. static struct i2c_driver dme1737_i2c_driver = {
  1835. .driver = {
  1836. .name = "dme1737",
  1837. },
  1838. .attach_adapter = dme1737_i2c_attach_adapter,
  1839. .detach_client = dme1737_i2c_detach_client,
  1840. };
  1841. /* ---------------------------------------------------------------------
  1842. * Module initialization and cleanup
  1843. * --------------------------------------------------------------------- */
  1844. static int __init dme1737_init(void)
  1845. {
  1846. return i2c_add_driver(&dme1737_i2c_driver);
  1847. }
  1848. static void __exit dme1737_exit(void)
  1849. {
  1850. i2c_del_driver(&dme1737_i2c_driver);
  1851. }
  1852. MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
  1853. MODULE_DESCRIPTION("DME1737 sensors");
  1854. MODULE_LICENSE("GPL");
  1855. module_init(dme1737_init);
  1856. module_exit(dme1737_exit);