dme1737.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  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. static int dme1737_i2c_get_features(int sio_cip, struct dme1737_data *data)
  1509. {
  1510. int err = 0, reg;
  1511. u16 addr;
  1512. dme1737_sio_enter(sio_cip);
  1513. /* Check device ID
  1514. * The DME1737 can return either 0x78 or 0x77 as its device ID. */
  1515. reg = dme1737_sio_inb(sio_cip, 0x20);
  1516. if (!(reg == 0x77 || reg == 0x78)) {
  1517. err = -ENODEV;
  1518. goto exit;
  1519. }
  1520. /* Select logical device A (runtime registers) */
  1521. dme1737_sio_outb(sio_cip, 0x07, 0x0a);
  1522. /* Get the base address of the runtime registers */
  1523. if (!(addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
  1524. dme1737_sio_inb(sio_cip, 0x61))) {
  1525. err = -ENODEV;
  1526. goto exit;
  1527. }
  1528. /* Read the runtime registers to determine which optional features
  1529. * are enabled and available. Bits [3:2] of registers 0x43-0x46 are set
  1530. * to '10' if the respective feature is enabled. */
  1531. if ((inb(addr + 0x43) & 0x0c) == 0x08) { /* fan6 */
  1532. data->has_fan |= (1 << 5);
  1533. }
  1534. if ((inb(addr + 0x44) & 0x0c) == 0x08) { /* pwm6 */
  1535. data->has_pwm |= (1 << 5);
  1536. }
  1537. if ((inb(addr + 0x45) & 0x0c) == 0x08) { /* fan5 */
  1538. data->has_fan |= (1 << 4);
  1539. }
  1540. if ((inb(addr + 0x46) & 0x0c) == 0x08) { /* pwm5 */
  1541. data->has_pwm |= (1 << 4);
  1542. }
  1543. exit:
  1544. dme1737_sio_exit(sio_cip);
  1545. return err;
  1546. }
  1547. /* ---------------------------------------------------------------------
  1548. * Device detection, registration and initialization
  1549. * --------------------------------------------------------------------- */
  1550. static struct i2c_driver dme1737_i2c_driver;
  1551. static void dme1737_chmod_file(struct device *dev,
  1552. struct attribute *attr, mode_t mode)
  1553. {
  1554. if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
  1555. dev_warn(dev, "Failed to change permissions of %s.\n",
  1556. attr->name);
  1557. }
  1558. }
  1559. static void dme1737_chmod_group(struct device *dev,
  1560. const struct attribute_group *group,
  1561. mode_t mode)
  1562. {
  1563. struct attribute **attr;
  1564. for (attr = group->attrs; *attr; attr++) {
  1565. dme1737_chmod_file(dev, *attr, mode);
  1566. }
  1567. }
  1568. static void dme1737_remove_files(struct device *dev)
  1569. {
  1570. struct dme1737_data *data = dev_get_drvdata(dev);
  1571. int ix;
  1572. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1573. if (data->has_fan & (1 << ix)) {
  1574. sysfs_remove_group(&dev->kobj,
  1575. &dme1737_fan_group[ix]);
  1576. }
  1577. }
  1578. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1579. if (data->has_pwm & (1 << ix)) {
  1580. sysfs_remove_group(&dev->kobj,
  1581. &dme1737_pwm_group[ix]);
  1582. }
  1583. }
  1584. sysfs_remove_group(&dev->kobj, &dme1737_group);
  1585. }
  1586. static int dme1737_create_files(struct device *dev)
  1587. {
  1588. struct dme1737_data *data = dev_get_drvdata(dev);
  1589. int err, ix;
  1590. /* Create standard sysfs attributes */
  1591. if ((err = sysfs_create_group(&dev->kobj, &dme1737_group))) {
  1592. goto exit;
  1593. }
  1594. /* Create fan sysfs attributes */
  1595. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1596. if (data->has_fan & (1 << ix)) {
  1597. if ((err = sysfs_create_group(&dev->kobj,
  1598. &dme1737_fan_group[ix]))) {
  1599. goto exit_remove;
  1600. }
  1601. }
  1602. }
  1603. /* Create PWM sysfs attributes */
  1604. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1605. if (data->has_pwm & (1 << ix)) {
  1606. if ((err = sysfs_create_group(&dev->kobj,
  1607. &dme1737_pwm_group[ix]))) {
  1608. goto exit_remove;
  1609. }
  1610. }
  1611. }
  1612. /* Inform if the device is locked. Otherwise change the permissions of
  1613. * selected attributes from read-only to read-writeable. */
  1614. if (data->config & 0x02) {
  1615. dev_info(dev, "Device is locked. Some attributes "
  1616. "will be read-only.\n");
  1617. } else {
  1618. /* Change permissions of standard attributes */
  1619. dme1737_chmod_group(dev, &dme1737_lock_group,
  1620. S_IRUGO | S_IWUSR);
  1621. /* Change permissions of PWM attributes */
  1622. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_lock_group); ix++) {
  1623. if (data->has_pwm & (1 << ix)) {
  1624. dme1737_chmod_group(dev,
  1625. &dme1737_pwm_lock_group[ix],
  1626. S_IRUGO | S_IWUSR);
  1627. }
  1628. }
  1629. /* Change permissions of pwm[1-3] if in manual mode */
  1630. for (ix = 0; ix < 3; ix++) {
  1631. if ((data->has_pwm & (1 << ix)) &&
  1632. (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
  1633. dme1737_chmod_file(dev,
  1634. dme1737_attr_pwm[ix],
  1635. S_IRUGO | S_IWUSR);
  1636. }
  1637. }
  1638. }
  1639. return 0;
  1640. exit_remove:
  1641. dme1737_remove_files(dev);
  1642. exit:
  1643. return err;
  1644. }
  1645. static int dme1737_init_device(struct device *dev)
  1646. {
  1647. struct dme1737_data *data = dev_get_drvdata(dev);
  1648. struct i2c_client *client = &data->client;
  1649. int ix;
  1650. u8 reg;
  1651. data->config = dme1737_read(client, DME1737_REG_CONFIG);
  1652. /* Inform if part is not monitoring/started */
  1653. if (!(data->config & 0x01)) {
  1654. if (!force_start) {
  1655. dev_err(dev, "Device is not monitoring. "
  1656. "Use the force_start load parameter to "
  1657. "override.\n");
  1658. return -EFAULT;
  1659. }
  1660. /* Force monitoring */
  1661. data->config |= 0x01;
  1662. dme1737_write(client, DME1737_REG_CONFIG, data->config);
  1663. }
  1664. /* Inform if part is not ready */
  1665. if (!(data->config & 0x04)) {
  1666. dev_err(dev, "Device is not ready.\n");
  1667. return -EFAULT;
  1668. }
  1669. data->config2 = dme1737_read(client, DME1737_REG_CONFIG2);
  1670. /* Check if optional fan3 input is enabled */
  1671. if (data->config2 & 0x04) {
  1672. data->has_fan |= (1 << 2);
  1673. }
  1674. /* Fan4 and pwm3 are only available if the client's I2C address
  1675. * is the default 0x2e. Otherwise the I/Os associated with these
  1676. * functions are used for addr enable/select. */
  1677. if (client->addr == 0x2e) {
  1678. data->has_fan |= (1 << 3);
  1679. data->has_pwm |= (1 << 2);
  1680. }
  1681. /* Determine if the optional fan[5-6] and/or pwm[5-6] are enabled.
  1682. * For this, we need to query the runtime registers through the
  1683. * Super-IO LPC interface. Try both config ports 0x2e and 0x4e. */
  1684. if (dme1737_i2c_get_features(0x2e, data) &&
  1685. dme1737_i2c_get_features(0x4e, data)) {
  1686. dev_warn(dev, "Failed to query Super-IO for optional "
  1687. "features.\n");
  1688. }
  1689. /* Fan1, fan2, pwm1, and pwm2 are always present */
  1690. data->has_fan |= 0x03;
  1691. data->has_pwm |= 0x03;
  1692. dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
  1693. "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
  1694. (data->has_pwm & (1 << 2)) ? "yes" : "no",
  1695. (data->has_pwm & (1 << 4)) ? "yes" : "no",
  1696. (data->has_pwm & (1 << 5)) ? "yes" : "no",
  1697. (data->has_fan & (1 << 2)) ? "yes" : "no",
  1698. (data->has_fan & (1 << 3)) ? "yes" : "no",
  1699. (data->has_fan & (1 << 4)) ? "yes" : "no",
  1700. (data->has_fan & (1 << 5)) ? "yes" : "no");
  1701. reg = dme1737_read(client, DME1737_REG_TACH_PWM);
  1702. /* Inform if fan-to-pwm mapping differs from the default */
  1703. if (reg != 0xa4) {
  1704. dev_warn(dev, "Non-standard fan to pwm mapping: "
  1705. "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
  1706. "fan4->pwm%d. Please report to the driver "
  1707. "maintainer.\n",
  1708. (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
  1709. ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
  1710. }
  1711. /* Switch pwm[1-3] to manual mode if they are currently disabled and
  1712. * set the duty-cycles to 0% (which is identical to the PWMs being
  1713. * disabled). */
  1714. if (!(data->config & 0x02)) {
  1715. for (ix = 0; ix < 3; ix++) {
  1716. data->pwm_config[ix] = dme1737_read(client,
  1717. DME1737_REG_PWM_CONFIG(ix));
  1718. if ((data->has_pwm & (1 << ix)) &&
  1719. (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
  1720. dev_info(dev, "Switching pwm%d to "
  1721. "manual mode.\n", ix + 1);
  1722. data->pwm_config[ix] = PWM_EN_TO_REG(1,
  1723. data->pwm_config[ix]);
  1724. dme1737_write(client, DME1737_REG_PWM(ix), 0);
  1725. dme1737_write(client,
  1726. DME1737_REG_PWM_CONFIG(ix),
  1727. data->pwm_config[ix]);
  1728. }
  1729. }
  1730. }
  1731. /* Initialize the default PWM auto channels zone (acz) assignments */
  1732. data->pwm_acz[0] = 1; /* pwm1 -> zone1 */
  1733. data->pwm_acz[1] = 2; /* pwm2 -> zone2 */
  1734. data->pwm_acz[2] = 4; /* pwm3 -> zone3 */
  1735. /* Set VRM */
  1736. data->vrm = vid_which_vrm();
  1737. return 0;
  1738. }
  1739. static int dme1737_i2c_detect(struct i2c_adapter *adapter, int address,
  1740. int kind)
  1741. {
  1742. u8 company, verstep = 0;
  1743. struct i2c_client *client;
  1744. struct dme1737_data *data;
  1745. struct device *dev;
  1746. int err = 0;
  1747. const char *name;
  1748. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  1749. goto exit;
  1750. }
  1751. if (!(data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL))) {
  1752. err = -ENOMEM;
  1753. goto exit;
  1754. }
  1755. client = &data->client;
  1756. i2c_set_clientdata(client, data);
  1757. client->addr = address;
  1758. client->adapter = adapter;
  1759. client->driver = &dme1737_i2c_driver;
  1760. dev = &client->dev;
  1761. /* A negative kind means that the driver was loaded with no force
  1762. * parameter (default), so we must identify the chip. */
  1763. if (kind < 0) {
  1764. company = dme1737_read(client, DME1737_REG_COMPANY);
  1765. verstep = dme1737_read(client, DME1737_REG_VERSTEP);
  1766. if (!((company == DME1737_COMPANY_SMSC) &&
  1767. ((verstep & DME1737_VERSTEP_MASK) == DME1737_VERSTEP))) {
  1768. err = -ENODEV;
  1769. goto exit_kfree;
  1770. }
  1771. }
  1772. kind = dme1737;
  1773. name = "dme1737";
  1774. /* Fill in the remaining client fields and put it into the global
  1775. * list */
  1776. strlcpy(client->name, name, I2C_NAME_SIZE);
  1777. mutex_init(&data->update_lock);
  1778. /* Tell the I2C layer a new client has arrived */
  1779. if ((err = i2c_attach_client(client))) {
  1780. goto exit_kfree;
  1781. }
  1782. dev_info(dev, "Found a DME1737 chip at 0x%02x (rev 0x%02x).\n",
  1783. client->addr, verstep);
  1784. /* Initialize the DME1737 chip */
  1785. if ((err = dme1737_init_device(dev))) {
  1786. dev_err(dev, "Failed to initialize device.\n");
  1787. goto exit_detach;
  1788. }
  1789. /* Create sysfs files */
  1790. if ((err = dme1737_create_files(dev))) {
  1791. dev_err(dev, "Failed to create sysfs files.\n");
  1792. goto exit_detach;
  1793. }
  1794. /* Register device */
  1795. data->class_dev = hwmon_device_register(dev);
  1796. if (IS_ERR(data->class_dev)) {
  1797. dev_err(dev, "Failed to register device.\n");
  1798. err = PTR_ERR(data->class_dev);
  1799. goto exit_remove;
  1800. }
  1801. return 0;
  1802. exit_remove:
  1803. dme1737_remove_files(dev);
  1804. exit_detach:
  1805. i2c_detach_client(client);
  1806. exit_kfree:
  1807. kfree(data);
  1808. exit:
  1809. return err;
  1810. }
  1811. static int dme1737_i2c_attach_adapter(struct i2c_adapter *adapter)
  1812. {
  1813. if (!(adapter->class & I2C_CLASS_HWMON)) {
  1814. return 0;
  1815. }
  1816. return i2c_probe(adapter, &addr_data, dme1737_i2c_detect);
  1817. }
  1818. static int dme1737_i2c_detach_client(struct i2c_client *client)
  1819. {
  1820. struct dme1737_data *data = i2c_get_clientdata(client);
  1821. int err;
  1822. hwmon_device_unregister(data->class_dev);
  1823. dme1737_remove_files(&client->dev);
  1824. if ((err = i2c_detach_client(client))) {
  1825. return err;
  1826. }
  1827. kfree(data);
  1828. return 0;
  1829. }
  1830. static struct i2c_driver dme1737_i2c_driver = {
  1831. .driver = {
  1832. .name = "dme1737",
  1833. },
  1834. .attach_adapter = dme1737_i2c_attach_adapter,
  1835. .detach_client = dme1737_i2c_detach_client,
  1836. };
  1837. static int __init dme1737_init(void)
  1838. {
  1839. return i2c_add_driver(&dme1737_i2c_driver);
  1840. }
  1841. static void __exit dme1737_exit(void)
  1842. {
  1843. i2c_del_driver(&dme1737_i2c_driver);
  1844. }
  1845. MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
  1846. MODULE_DESCRIPTION("DME1737 sensors");
  1847. MODULE_LICENSE("GPL");
  1848. module_init(dme1737_init);
  1849. module_exit(dme1737_exit);