dme1737.c 78 KB

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