dme1737.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  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, "Include probing of non-standard LPC "
  53. "addresses");
  54. /* Addresses to scan */
  55. static const unsigned short normal_i2c[] = {0x2c, 0x2d, 0x2e, I2C_CLIENT_END};
  56. enum chips { dme1737, sch5027, sch311x, sch5127 };
  57. /* ---------------------------------------------------------------------
  58. * Registers
  59. *
  60. * The sensors are defined as follows:
  61. *
  62. * Voltages Temperatures
  63. * -------- ------------
  64. * in0 +5VTR (+5V stdby) temp1 Remote diode 1
  65. * in1 Vccp (proc core) temp2 Internal temp
  66. * in2 VCC (internal +3.3V) temp3 Remote diode 2
  67. * in3 +5V
  68. * in4 +12V
  69. * in5 VTR (+3.3V stby)
  70. * in6 Vbat
  71. * in7 Vtrip (sch5127 only)
  72. *
  73. * --------------------------------------------------------------------- */
  74. /* Voltages (in) numbered 0-7 (ix) */
  75. #define DME1737_REG_IN(ix) ((ix) < 5 ? 0x20 + (ix) : \
  76. (ix) < 7 ? 0x94 + (ix) : \
  77. 0x1f)
  78. #define DME1737_REG_IN_MIN(ix) ((ix) < 5 ? 0x44 + (ix) * 2 \
  79. : 0x91 + (ix) * 2)
  80. #define DME1737_REG_IN_MAX(ix) ((ix) < 5 ? 0x45 + (ix) * 2 \
  81. : 0x92 + (ix) * 2)
  82. /* Temperatures (temp) numbered 0-2 (ix) */
  83. #define DME1737_REG_TEMP(ix) (0x25 + (ix))
  84. #define DME1737_REG_TEMP_MIN(ix) (0x4e + (ix) * 2)
  85. #define DME1737_REG_TEMP_MAX(ix) (0x4f + (ix) * 2)
  86. #define DME1737_REG_TEMP_OFFSET(ix) ((ix) == 0 ? 0x1f \
  87. : 0x1c + (ix))
  88. /*
  89. * Voltage and temperature LSBs
  90. * The LSBs (4 bits each) are stored in 5 registers with the following layouts:
  91. * IN_TEMP_LSB(0) = [in5, in6]
  92. * IN_TEMP_LSB(1) = [temp3, temp1]
  93. * IN_TEMP_LSB(2) = [in4, temp2]
  94. * IN_TEMP_LSB(3) = [in3, in0]
  95. * IN_TEMP_LSB(4) = [in2, in1]
  96. * IN_TEMP_LSB(5) = [res, in7]
  97. */
  98. #define DME1737_REG_IN_TEMP_LSB(ix) (0x84 + (ix))
  99. static const u8 DME1737_REG_IN_LSB[] = {3, 4, 4, 3, 2, 0, 0, 5};
  100. static const u8 DME1737_REG_IN_LSB_SHL[] = {4, 4, 0, 0, 0, 0, 4, 4};
  101. static const u8 DME1737_REG_TEMP_LSB[] = {1, 2, 1};
  102. static const u8 DME1737_REG_TEMP_LSB_SHL[] = {4, 4, 0};
  103. /* Fans numbered 0-5 (ix) */
  104. #define DME1737_REG_FAN(ix) ((ix) < 4 ? 0x28 + (ix) * 2 \
  105. : 0xa1 + (ix) * 2)
  106. #define DME1737_REG_FAN_MIN(ix) ((ix) < 4 ? 0x54 + (ix) * 2 \
  107. : 0xa5 + (ix) * 2)
  108. #define DME1737_REG_FAN_OPT(ix) ((ix) < 4 ? 0x90 + (ix) \
  109. : 0xb2 + (ix))
  110. #define DME1737_REG_FAN_MAX(ix) (0xb4 + (ix)) /* only for fan[4-5] */
  111. /* PWMs numbered 0-2, 4-5 (ix) */
  112. #define DME1737_REG_PWM(ix) ((ix) < 3 ? 0x30 + (ix) \
  113. : 0xa1 + (ix))
  114. #define DME1737_REG_PWM_CONFIG(ix) (0x5c + (ix)) /* only for pwm[0-2] */
  115. #define DME1737_REG_PWM_MIN(ix) (0x64 + (ix)) /* only for pwm[0-2] */
  116. #define DME1737_REG_PWM_FREQ(ix) ((ix) < 3 ? 0x5f + (ix) \
  117. : 0xa3 + (ix))
  118. /*
  119. * The layout of the ramp rate registers is different from the other pwm
  120. * registers. The bits for the 3 PWMs are stored in 2 registers:
  121. * PWM_RR(0) = [OFF3, OFF2, OFF1, RES, RR1E, RR1-2, RR1-1, RR1-0]
  122. * PWM_RR(1) = [RR2E, RR2-2, RR2-1, RR2-0, RR3E, RR3-2, RR3-1, RR3-0]
  123. */
  124. #define DME1737_REG_PWM_RR(ix) (0x62 + (ix)) /* only for pwm[0-2] */
  125. /* Thermal zones 0-2 */
  126. #define DME1737_REG_ZONE_LOW(ix) (0x67 + (ix))
  127. #define DME1737_REG_ZONE_ABS(ix) (0x6a + (ix))
  128. /*
  129. * The layout of the hysteresis registers is different from the other zone
  130. * registers. The bits for the 3 zones are stored in 2 registers:
  131. * ZONE_HYST(0) = [H1-3, H1-2, H1-1, H1-0, H2-3, H2-2, H2-1, H2-0]
  132. * ZONE_HYST(1) = [H3-3, H3-2, H3-1, H3-0, RES, RES, RES, RES]
  133. */
  134. #define DME1737_REG_ZONE_HYST(ix) (0x6d + (ix))
  135. /*
  136. * Alarm registers and bit mapping
  137. * The 3 8-bit alarm registers will be concatenated to a single 32-bit
  138. * alarm value [0, ALARM3, ALARM2, ALARM1].
  139. */
  140. #define DME1737_REG_ALARM1 0x41
  141. #define DME1737_REG_ALARM2 0x42
  142. #define DME1737_REG_ALARM3 0x83
  143. static const u8 DME1737_BIT_ALARM_IN[] = {0, 1, 2, 3, 8, 16, 17, 18};
  144. static const u8 DME1737_BIT_ALARM_TEMP[] = {4, 5, 6};
  145. static const u8 DME1737_BIT_ALARM_FAN[] = {10, 11, 12, 13, 22, 23};
  146. /* Miscellaneous registers */
  147. #define DME1737_REG_DEVICE 0x3d
  148. #define DME1737_REG_COMPANY 0x3e
  149. #define DME1737_REG_VERSTEP 0x3f
  150. #define DME1737_REG_CONFIG 0x40
  151. #define DME1737_REG_CONFIG2 0x7f
  152. #define DME1737_REG_VID 0x43
  153. #define DME1737_REG_TACH_PWM 0x81
  154. /* ---------------------------------------------------------------------
  155. * Misc defines
  156. * --------------------------------------------------------------------- */
  157. /* Chip identification */
  158. #define DME1737_COMPANY_SMSC 0x5c
  159. #define DME1737_VERSTEP 0x88
  160. #define DME1737_VERSTEP_MASK 0xf8
  161. #define SCH311X_DEVICE 0x8c
  162. #define SCH5027_VERSTEP 0x69
  163. #define SCH5127_DEVICE 0x8e
  164. /* Device ID values (global configuration register index 0x20) */
  165. #define DME1737_ID_1 0x77
  166. #define DME1737_ID_2 0x78
  167. #define SCH3112_ID 0x7c
  168. #define SCH3114_ID 0x7d
  169. #define SCH3116_ID 0x7f
  170. #define SCH5027_ID 0x89
  171. #define SCH5127_ID 0x86
  172. /* Length of ISA address segment */
  173. #define DME1737_EXTENT 2
  174. /* chip-dependent features */
  175. #define HAS_TEMP_OFFSET (1 << 0) /* bit 0 */
  176. #define HAS_VID (1 << 1) /* bit 1 */
  177. #define HAS_ZONE3 (1 << 2) /* bit 2 */
  178. #define HAS_ZONE_HYST (1 << 3) /* bit 3 */
  179. #define HAS_PWM_MIN (1 << 4) /* bit 4 */
  180. #define HAS_FAN(ix) (1 << ((ix) + 5)) /* bits 5-10 */
  181. #define HAS_PWM(ix) (1 << ((ix) + 11)) /* bits 11-16 */
  182. #define HAS_IN7 (1 << 17) /* bit 17 */
  183. /* ---------------------------------------------------------------------
  184. * Data structures and manipulation thereof
  185. * --------------------------------------------------------------------- */
  186. struct dme1737_data {
  187. struct i2c_client *client; /* for I2C devices only */
  188. struct device *hwmon_dev;
  189. const char *name;
  190. unsigned int addr; /* for ISA devices only */
  191. struct mutex update_lock;
  192. int valid; /* !=0 if following fields are valid */
  193. unsigned long last_update; /* in jiffies */
  194. unsigned long last_vbat; /* in jiffies */
  195. enum chips type;
  196. const int *in_nominal; /* pointer to IN_NOMINAL array */
  197. u8 vid;
  198. u8 pwm_rr_en;
  199. u32 has_features;
  200. /* Register values */
  201. u16 in[8];
  202. u8 in_min[8];
  203. u8 in_max[8];
  204. s16 temp[3];
  205. s8 temp_min[3];
  206. s8 temp_max[3];
  207. s8 temp_offset[3];
  208. u8 config;
  209. u8 config2;
  210. u8 vrm;
  211. u16 fan[6];
  212. u16 fan_min[6];
  213. u8 fan_max[2];
  214. u8 fan_opt[6];
  215. u8 pwm[6];
  216. u8 pwm_min[3];
  217. u8 pwm_config[3];
  218. u8 pwm_acz[3];
  219. u8 pwm_freq[6];
  220. u8 pwm_rr[2];
  221. u8 zone_low[3];
  222. u8 zone_abs[3];
  223. u8 zone_hyst[2];
  224. u32 alarms;
  225. };
  226. /* Nominal voltage values */
  227. static const int IN_NOMINAL_DME1737[] = {5000, 2250, 3300, 5000, 12000, 3300,
  228. 3300};
  229. static const int IN_NOMINAL_SCH311x[] = {2500, 1500, 3300, 5000, 12000, 3300,
  230. 3300};
  231. static const int IN_NOMINAL_SCH5027[] = {5000, 2250, 3300, 1125, 1125, 3300,
  232. 3300};
  233. static const int IN_NOMINAL_SCH5127[] = {2500, 2250, 3300, 1125, 1125, 3300,
  234. 3300, 1500};
  235. #define IN_NOMINAL(type) ((type) == sch311x ? IN_NOMINAL_SCH311x : \
  236. (type) == sch5027 ? IN_NOMINAL_SCH5027 : \
  237. (type) == sch5127 ? IN_NOMINAL_SCH5127 : \
  238. IN_NOMINAL_DME1737)
  239. /*
  240. * Voltage input
  241. * Voltage inputs have 16 bits resolution, limit values have 8 bits
  242. * resolution.
  243. */
  244. static inline int IN_FROM_REG(int reg, int nominal, int res)
  245. {
  246. return (reg * nominal + (3 << (res - 3))) / (3 << (res - 2));
  247. }
  248. static inline int IN_TO_REG(int val, int nominal)
  249. {
  250. return SENSORS_LIMIT((val * 192 + nominal / 2) / nominal, 0, 255);
  251. }
  252. /*
  253. * Temperature input
  254. * The register values represent temperatures in 2's complement notation from
  255. * -127 degrees C to +127 degrees C. Temp inputs have 16 bits resolution, limit
  256. * values have 8 bits resolution.
  257. */
  258. static inline int TEMP_FROM_REG(int reg, int res)
  259. {
  260. return (reg * 1000) >> (res - 8);
  261. }
  262. static inline int TEMP_TO_REG(int val)
  263. {
  264. return SENSORS_LIMIT((val < 0 ? val - 500 : val + 500) / 1000,
  265. -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(int 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(int val, int ix, int reg)
  295. {
  296. int hyst = SENSORS_LIMIT((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(int val, int tpc)
  308. {
  309. if (tpc) {
  310. return SENSORS_LIMIT(val / tpc, 0, 0xffff);
  311. } else {
  312. return (val <= 0) ? 0xffff :
  313. SENSORS_LIMIT(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(int 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(int 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(int 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(int 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(int 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(int 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, "Read from register "
  489. "0x%02x failed! Please report to the driver "
  490. "maintainer.\n", reg);
  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, "Write to register "
  506. "0x%02x failed! Please report to the driver "
  507. "maintainer.\n", reg);
  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, "Fan type value %ld not "
  1035. "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] = SENSORS_LIMIT(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, "PWM enable %ld not "
  1152. "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, "PWM auto channels zone %ld "
  1257. "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] = SENSORS_LIMIT(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. long val;
  1332. int err;
  1333. err = kstrtol(buf, 10, &val);
  1334. if (err)
  1335. return err;
  1336. data->vrm = val;
  1337. return count;
  1338. }
  1339. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  1340. char *buf)
  1341. {
  1342. struct dme1737_data *data = dme1737_update_device(dev);
  1343. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  1344. }
  1345. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  1346. char *buf)
  1347. {
  1348. struct dme1737_data *data = dev_get_drvdata(dev);
  1349. return sprintf(buf, "%s\n", data->name);
  1350. }
  1351. /* ---------------------------------------------------------------------
  1352. * Sysfs device attribute defines and structs
  1353. * --------------------------------------------------------------------- */
  1354. /* Voltages 0-7 */
  1355. #define SENSOR_DEVICE_ATTR_IN(ix) \
  1356. static SENSOR_DEVICE_ATTR_2(in##ix##_input, S_IRUGO, \
  1357. show_in, NULL, SYS_IN_INPUT, ix); \
  1358. static SENSOR_DEVICE_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
  1359. show_in, set_in, SYS_IN_MIN, ix); \
  1360. static SENSOR_DEVICE_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
  1361. show_in, set_in, SYS_IN_MAX, ix); \
  1362. static SENSOR_DEVICE_ATTR_2(in##ix##_alarm, S_IRUGO, \
  1363. show_in, NULL, SYS_IN_ALARM, ix)
  1364. SENSOR_DEVICE_ATTR_IN(0);
  1365. SENSOR_DEVICE_ATTR_IN(1);
  1366. SENSOR_DEVICE_ATTR_IN(2);
  1367. SENSOR_DEVICE_ATTR_IN(3);
  1368. SENSOR_DEVICE_ATTR_IN(4);
  1369. SENSOR_DEVICE_ATTR_IN(5);
  1370. SENSOR_DEVICE_ATTR_IN(6);
  1371. SENSOR_DEVICE_ATTR_IN(7);
  1372. /* Temperatures 1-3 */
  1373. #define SENSOR_DEVICE_ATTR_TEMP(ix) \
  1374. static SENSOR_DEVICE_ATTR_2(temp##ix##_input, S_IRUGO, \
  1375. show_temp, NULL, SYS_TEMP_INPUT, ix-1); \
  1376. static SENSOR_DEVICE_ATTR_2(temp##ix##_min, S_IRUGO | S_IWUSR, \
  1377. show_temp, set_temp, SYS_TEMP_MIN, ix-1); \
  1378. static SENSOR_DEVICE_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
  1379. show_temp, set_temp, SYS_TEMP_MAX, ix-1); \
  1380. static SENSOR_DEVICE_ATTR_2(temp##ix##_offset, S_IRUGO, \
  1381. show_temp, set_temp, SYS_TEMP_OFFSET, ix-1); \
  1382. static SENSOR_DEVICE_ATTR_2(temp##ix##_alarm, S_IRUGO, \
  1383. show_temp, NULL, SYS_TEMP_ALARM, ix-1); \
  1384. static SENSOR_DEVICE_ATTR_2(temp##ix##_fault, S_IRUGO, \
  1385. show_temp, NULL, SYS_TEMP_FAULT, ix-1)
  1386. SENSOR_DEVICE_ATTR_TEMP(1);
  1387. SENSOR_DEVICE_ATTR_TEMP(2);
  1388. SENSOR_DEVICE_ATTR_TEMP(3);
  1389. /* Zones 1-3 */
  1390. #define SENSOR_DEVICE_ATTR_ZONE(ix) \
  1391. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_channels_temp, S_IRUGO, \
  1392. show_zone, NULL, SYS_ZONE_AUTO_CHANNELS_TEMP, ix-1); \
  1393. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp_hyst, S_IRUGO, \
  1394. show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP_HYST, ix-1); \
  1395. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point1_temp, S_IRUGO, \
  1396. show_zone, set_zone, SYS_ZONE_AUTO_POINT1_TEMP, ix-1); \
  1397. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point2_temp, S_IRUGO, \
  1398. show_zone, set_zone, SYS_ZONE_AUTO_POINT2_TEMP, ix-1); \
  1399. static SENSOR_DEVICE_ATTR_2(zone##ix##_auto_point3_temp, S_IRUGO, \
  1400. show_zone, set_zone, SYS_ZONE_AUTO_POINT3_TEMP, ix-1)
  1401. SENSOR_DEVICE_ATTR_ZONE(1);
  1402. SENSOR_DEVICE_ATTR_ZONE(2);
  1403. SENSOR_DEVICE_ATTR_ZONE(3);
  1404. /* Fans 1-4 */
  1405. #define SENSOR_DEVICE_ATTR_FAN_1TO4(ix) \
  1406. static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
  1407. show_fan, NULL, SYS_FAN_INPUT, ix-1); \
  1408. static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  1409. show_fan, set_fan, SYS_FAN_MIN, ix-1); \
  1410. static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  1411. show_fan, NULL, SYS_FAN_ALARM, ix-1); \
  1412. static SENSOR_DEVICE_ATTR_2(fan##ix##_type, S_IRUGO | S_IWUSR, \
  1413. show_fan, set_fan, SYS_FAN_TYPE, ix-1)
  1414. SENSOR_DEVICE_ATTR_FAN_1TO4(1);
  1415. SENSOR_DEVICE_ATTR_FAN_1TO4(2);
  1416. SENSOR_DEVICE_ATTR_FAN_1TO4(3);
  1417. SENSOR_DEVICE_ATTR_FAN_1TO4(4);
  1418. /* Fans 5-6 */
  1419. #define SENSOR_DEVICE_ATTR_FAN_5TO6(ix) \
  1420. static SENSOR_DEVICE_ATTR_2(fan##ix##_input, S_IRUGO, \
  1421. show_fan, NULL, SYS_FAN_INPUT, ix-1); \
  1422. static SENSOR_DEVICE_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  1423. show_fan, set_fan, SYS_FAN_MIN, ix-1); \
  1424. static SENSOR_DEVICE_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  1425. show_fan, NULL, SYS_FAN_ALARM, ix-1); \
  1426. static SENSOR_DEVICE_ATTR_2(fan##ix##_max, S_IRUGO | S_IWUSR, \
  1427. show_fan, set_fan, SYS_FAN_MAX, ix-1)
  1428. SENSOR_DEVICE_ATTR_FAN_5TO6(5);
  1429. SENSOR_DEVICE_ATTR_FAN_5TO6(6);
  1430. /* PWMs 1-3 */
  1431. #define SENSOR_DEVICE_ATTR_PWM_1TO3(ix) \
  1432. static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
  1433. show_pwm, set_pwm, SYS_PWM, ix-1); \
  1434. static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
  1435. show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
  1436. static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
  1437. show_pwm, set_pwm, SYS_PWM_ENABLE, ix-1); \
  1438. static SENSOR_DEVICE_ATTR_2(pwm##ix##_ramp_rate, S_IRUGO, \
  1439. show_pwm, set_pwm, SYS_PWM_RAMP_RATE, ix-1); \
  1440. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_channels_zone, S_IRUGO, \
  1441. show_pwm, set_pwm, SYS_PWM_AUTO_CHANNELS_ZONE, ix-1); \
  1442. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_pwm_min, S_IRUGO, \
  1443. show_pwm, set_pwm, SYS_PWM_AUTO_PWM_MIN, ix-1); \
  1444. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point1_pwm, S_IRUGO, \
  1445. show_pwm, set_pwm, SYS_PWM_AUTO_POINT1_PWM, ix-1); \
  1446. static SENSOR_DEVICE_ATTR_2(pwm##ix##_auto_point2_pwm, S_IRUGO, \
  1447. show_pwm, NULL, SYS_PWM_AUTO_POINT2_PWM, ix-1)
  1448. SENSOR_DEVICE_ATTR_PWM_1TO3(1);
  1449. SENSOR_DEVICE_ATTR_PWM_1TO3(2);
  1450. SENSOR_DEVICE_ATTR_PWM_1TO3(3);
  1451. /* PWMs 5-6 */
  1452. #define SENSOR_DEVICE_ATTR_PWM_5TO6(ix) \
  1453. static SENSOR_DEVICE_ATTR_2(pwm##ix, S_IRUGO, \
  1454. show_pwm, set_pwm, SYS_PWM, ix-1); \
  1455. static SENSOR_DEVICE_ATTR_2(pwm##ix##_freq, S_IRUGO, \
  1456. show_pwm, set_pwm, SYS_PWM_FREQ, ix-1); \
  1457. static SENSOR_DEVICE_ATTR_2(pwm##ix##_enable, S_IRUGO, \
  1458. show_pwm, NULL, SYS_PWM_ENABLE, ix-1)
  1459. SENSOR_DEVICE_ATTR_PWM_5TO6(5);
  1460. SENSOR_DEVICE_ATTR_PWM_5TO6(6);
  1461. /* Misc */
  1462. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  1463. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  1464. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); /* for ISA devices */
  1465. /*
  1466. * This struct holds all the attributes that are always present and need to be
  1467. * created unconditionally. The attributes that need modification of their
  1468. * permissions are created read-only and write permissions are added or removed
  1469. * on the fly when required
  1470. */
  1471. static struct attribute *dme1737_attr[] = {
  1472. /* Voltages */
  1473. &sensor_dev_attr_in0_input.dev_attr.attr,
  1474. &sensor_dev_attr_in0_min.dev_attr.attr,
  1475. &sensor_dev_attr_in0_max.dev_attr.attr,
  1476. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1477. &sensor_dev_attr_in1_input.dev_attr.attr,
  1478. &sensor_dev_attr_in1_min.dev_attr.attr,
  1479. &sensor_dev_attr_in1_max.dev_attr.attr,
  1480. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1481. &sensor_dev_attr_in2_input.dev_attr.attr,
  1482. &sensor_dev_attr_in2_min.dev_attr.attr,
  1483. &sensor_dev_attr_in2_max.dev_attr.attr,
  1484. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1485. &sensor_dev_attr_in3_input.dev_attr.attr,
  1486. &sensor_dev_attr_in3_min.dev_attr.attr,
  1487. &sensor_dev_attr_in3_max.dev_attr.attr,
  1488. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1489. &sensor_dev_attr_in4_input.dev_attr.attr,
  1490. &sensor_dev_attr_in4_min.dev_attr.attr,
  1491. &sensor_dev_attr_in4_max.dev_attr.attr,
  1492. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1493. &sensor_dev_attr_in5_input.dev_attr.attr,
  1494. &sensor_dev_attr_in5_min.dev_attr.attr,
  1495. &sensor_dev_attr_in5_max.dev_attr.attr,
  1496. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1497. &sensor_dev_attr_in6_input.dev_attr.attr,
  1498. &sensor_dev_attr_in6_min.dev_attr.attr,
  1499. &sensor_dev_attr_in6_max.dev_attr.attr,
  1500. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1501. /* Temperatures */
  1502. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1503. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1504. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1505. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1506. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  1507. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1508. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1509. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1510. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1511. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  1512. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1513. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1514. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1515. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1516. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  1517. /* Zones */
  1518. &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
  1519. &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
  1520. &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
  1521. &sensor_dev_attr_zone1_auto_channels_temp.dev_attr.attr,
  1522. &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
  1523. &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
  1524. &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
  1525. &sensor_dev_attr_zone2_auto_channels_temp.dev_attr.attr,
  1526. NULL
  1527. };
  1528. static const struct attribute_group dme1737_group = {
  1529. .attrs = dme1737_attr,
  1530. };
  1531. /*
  1532. * The following struct holds temp offset attributes, which are not available
  1533. * in all chips. The following chips support them:
  1534. * DME1737, SCH311x
  1535. */
  1536. static struct attribute *dme1737_temp_offset_attr[] = {
  1537. &sensor_dev_attr_temp1_offset.dev_attr.attr,
  1538. &sensor_dev_attr_temp2_offset.dev_attr.attr,
  1539. &sensor_dev_attr_temp3_offset.dev_attr.attr,
  1540. NULL
  1541. };
  1542. static const struct attribute_group dme1737_temp_offset_group = {
  1543. .attrs = dme1737_temp_offset_attr,
  1544. };
  1545. /*
  1546. * The following struct holds VID related attributes, which are not available
  1547. * in all chips. The following chips support them:
  1548. * DME1737
  1549. */
  1550. static struct attribute *dme1737_vid_attr[] = {
  1551. &dev_attr_vrm.attr,
  1552. &dev_attr_cpu0_vid.attr,
  1553. NULL
  1554. };
  1555. static const struct attribute_group dme1737_vid_group = {
  1556. .attrs = dme1737_vid_attr,
  1557. };
  1558. /*
  1559. * The following struct holds temp zone 3 related attributes, which are not
  1560. * available in all chips. The following chips support them:
  1561. * DME1737, SCH311x, SCH5027
  1562. */
  1563. static struct attribute *dme1737_zone3_attr[] = {
  1564. &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
  1565. &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
  1566. &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
  1567. &sensor_dev_attr_zone3_auto_channels_temp.dev_attr.attr,
  1568. NULL
  1569. };
  1570. static const struct attribute_group dme1737_zone3_group = {
  1571. .attrs = dme1737_zone3_attr,
  1572. };
  1573. /*
  1574. * The following struct holds temp zone hysteresis related attributes, which
  1575. * are not available in all chips. The following chips support them:
  1576. * DME1737, SCH311x
  1577. */
  1578. static struct attribute *dme1737_zone_hyst_attr[] = {
  1579. &sensor_dev_attr_zone1_auto_point1_temp_hyst.dev_attr.attr,
  1580. &sensor_dev_attr_zone2_auto_point1_temp_hyst.dev_attr.attr,
  1581. &sensor_dev_attr_zone3_auto_point1_temp_hyst.dev_attr.attr,
  1582. NULL
  1583. };
  1584. static const struct attribute_group dme1737_zone_hyst_group = {
  1585. .attrs = dme1737_zone_hyst_attr,
  1586. };
  1587. /*
  1588. * The following struct holds voltage in7 related attributes, which
  1589. * are not available in all chips. The following chips support them:
  1590. * SCH5127
  1591. */
  1592. static struct attribute *dme1737_in7_attr[] = {
  1593. &sensor_dev_attr_in7_input.dev_attr.attr,
  1594. &sensor_dev_attr_in7_min.dev_attr.attr,
  1595. &sensor_dev_attr_in7_max.dev_attr.attr,
  1596. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1597. NULL
  1598. };
  1599. static const struct attribute_group dme1737_in7_group = {
  1600. .attrs = dme1737_in7_attr,
  1601. };
  1602. /*
  1603. * The following structs hold the PWM attributes, some of which are optional.
  1604. * Their creation depends on the chip configuration which is determined during
  1605. * module load.
  1606. */
  1607. static struct attribute *dme1737_pwm1_attr[] = {
  1608. &sensor_dev_attr_pwm1.dev_attr.attr,
  1609. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  1610. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1611. &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
  1612. &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
  1613. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  1614. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  1615. NULL
  1616. };
  1617. static struct attribute *dme1737_pwm2_attr[] = {
  1618. &sensor_dev_attr_pwm2.dev_attr.attr,
  1619. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  1620. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1621. &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
  1622. &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
  1623. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  1624. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  1625. NULL
  1626. };
  1627. static struct attribute *dme1737_pwm3_attr[] = {
  1628. &sensor_dev_attr_pwm3.dev_attr.attr,
  1629. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  1630. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1631. &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
  1632. &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
  1633. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  1634. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  1635. NULL
  1636. };
  1637. static struct attribute *dme1737_pwm5_attr[] = {
  1638. &sensor_dev_attr_pwm5.dev_attr.attr,
  1639. &sensor_dev_attr_pwm5_freq.dev_attr.attr,
  1640. &sensor_dev_attr_pwm5_enable.dev_attr.attr,
  1641. NULL
  1642. };
  1643. static struct attribute *dme1737_pwm6_attr[] = {
  1644. &sensor_dev_attr_pwm6.dev_attr.attr,
  1645. &sensor_dev_attr_pwm6_freq.dev_attr.attr,
  1646. &sensor_dev_attr_pwm6_enable.dev_attr.attr,
  1647. NULL
  1648. };
  1649. static const struct attribute_group dme1737_pwm_group[] = {
  1650. { .attrs = dme1737_pwm1_attr },
  1651. { .attrs = dme1737_pwm2_attr },
  1652. { .attrs = dme1737_pwm3_attr },
  1653. { .attrs = NULL },
  1654. { .attrs = dme1737_pwm5_attr },
  1655. { .attrs = dme1737_pwm6_attr },
  1656. };
  1657. /*
  1658. * The following struct holds auto PWM min attributes, which are not available
  1659. * in all chips. Their creation depends on the chip type which is determined
  1660. * during module load.
  1661. */
  1662. static struct attribute *dme1737_auto_pwm_min_attr[] = {
  1663. &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
  1664. &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
  1665. &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
  1666. };
  1667. /*
  1668. * The following structs hold the fan attributes, some of which are optional.
  1669. * Their creation depends on the chip configuration which is determined during
  1670. * module load.
  1671. */
  1672. static struct attribute *dme1737_fan1_attr[] = {
  1673. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1674. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1675. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1676. &sensor_dev_attr_fan1_type.dev_attr.attr,
  1677. NULL
  1678. };
  1679. static struct attribute *dme1737_fan2_attr[] = {
  1680. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1681. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1682. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1683. &sensor_dev_attr_fan2_type.dev_attr.attr,
  1684. NULL
  1685. };
  1686. static struct attribute *dme1737_fan3_attr[] = {
  1687. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1688. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1689. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1690. &sensor_dev_attr_fan3_type.dev_attr.attr,
  1691. NULL
  1692. };
  1693. static struct attribute *dme1737_fan4_attr[] = {
  1694. &sensor_dev_attr_fan4_input.dev_attr.attr,
  1695. &sensor_dev_attr_fan4_min.dev_attr.attr,
  1696. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1697. &sensor_dev_attr_fan4_type.dev_attr.attr,
  1698. NULL
  1699. };
  1700. static struct attribute *dme1737_fan5_attr[] = {
  1701. &sensor_dev_attr_fan5_input.dev_attr.attr,
  1702. &sensor_dev_attr_fan5_min.dev_attr.attr,
  1703. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1704. &sensor_dev_attr_fan5_max.dev_attr.attr,
  1705. NULL
  1706. };
  1707. static struct attribute *dme1737_fan6_attr[] = {
  1708. &sensor_dev_attr_fan6_input.dev_attr.attr,
  1709. &sensor_dev_attr_fan6_min.dev_attr.attr,
  1710. &sensor_dev_attr_fan6_alarm.dev_attr.attr,
  1711. &sensor_dev_attr_fan6_max.dev_attr.attr,
  1712. NULL
  1713. };
  1714. static const struct attribute_group dme1737_fan_group[] = {
  1715. { .attrs = dme1737_fan1_attr },
  1716. { .attrs = dme1737_fan2_attr },
  1717. { .attrs = dme1737_fan3_attr },
  1718. { .attrs = dme1737_fan4_attr },
  1719. { .attrs = dme1737_fan5_attr },
  1720. { .attrs = dme1737_fan6_attr },
  1721. };
  1722. /*
  1723. * The permissions of the following zone attributes are changed to read-
  1724. * writeable if the chip is *not* locked. Otherwise they stay read-only.
  1725. */
  1726. static struct attribute *dme1737_zone_chmod_attr[] = {
  1727. &sensor_dev_attr_zone1_auto_point1_temp.dev_attr.attr,
  1728. &sensor_dev_attr_zone1_auto_point2_temp.dev_attr.attr,
  1729. &sensor_dev_attr_zone1_auto_point3_temp.dev_attr.attr,
  1730. &sensor_dev_attr_zone2_auto_point1_temp.dev_attr.attr,
  1731. &sensor_dev_attr_zone2_auto_point2_temp.dev_attr.attr,
  1732. &sensor_dev_attr_zone2_auto_point3_temp.dev_attr.attr,
  1733. NULL
  1734. };
  1735. static const struct attribute_group dme1737_zone_chmod_group = {
  1736. .attrs = dme1737_zone_chmod_attr,
  1737. };
  1738. /*
  1739. * The permissions of the following zone 3 attributes are changed to read-
  1740. * writeable if the chip is *not* locked. Otherwise they stay read-only.
  1741. */
  1742. static struct attribute *dme1737_zone3_chmod_attr[] = {
  1743. &sensor_dev_attr_zone3_auto_point1_temp.dev_attr.attr,
  1744. &sensor_dev_attr_zone3_auto_point2_temp.dev_attr.attr,
  1745. &sensor_dev_attr_zone3_auto_point3_temp.dev_attr.attr,
  1746. NULL
  1747. };
  1748. static const struct attribute_group dme1737_zone3_chmod_group = {
  1749. .attrs = dme1737_zone3_chmod_attr,
  1750. };
  1751. /*
  1752. * The permissions of the following PWM attributes are changed to read-
  1753. * writeable if the chip is *not* locked and the respective PWM is available.
  1754. * Otherwise they stay read-only.
  1755. */
  1756. static struct attribute *dme1737_pwm1_chmod_attr[] = {
  1757. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  1758. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1759. &sensor_dev_attr_pwm1_ramp_rate.dev_attr.attr,
  1760. &sensor_dev_attr_pwm1_auto_channels_zone.dev_attr.attr,
  1761. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  1762. NULL
  1763. };
  1764. static struct attribute *dme1737_pwm2_chmod_attr[] = {
  1765. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  1766. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1767. &sensor_dev_attr_pwm2_ramp_rate.dev_attr.attr,
  1768. &sensor_dev_attr_pwm2_auto_channels_zone.dev_attr.attr,
  1769. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  1770. NULL
  1771. };
  1772. static struct attribute *dme1737_pwm3_chmod_attr[] = {
  1773. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  1774. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1775. &sensor_dev_attr_pwm3_ramp_rate.dev_attr.attr,
  1776. &sensor_dev_attr_pwm3_auto_channels_zone.dev_attr.attr,
  1777. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  1778. NULL
  1779. };
  1780. static struct attribute *dme1737_pwm5_chmod_attr[] = {
  1781. &sensor_dev_attr_pwm5.dev_attr.attr,
  1782. &sensor_dev_attr_pwm5_freq.dev_attr.attr,
  1783. NULL
  1784. };
  1785. static struct attribute *dme1737_pwm6_chmod_attr[] = {
  1786. &sensor_dev_attr_pwm6.dev_attr.attr,
  1787. &sensor_dev_attr_pwm6_freq.dev_attr.attr,
  1788. NULL
  1789. };
  1790. static const struct attribute_group dme1737_pwm_chmod_group[] = {
  1791. { .attrs = dme1737_pwm1_chmod_attr },
  1792. { .attrs = dme1737_pwm2_chmod_attr },
  1793. { .attrs = dme1737_pwm3_chmod_attr },
  1794. { .attrs = NULL },
  1795. { .attrs = dme1737_pwm5_chmod_attr },
  1796. { .attrs = dme1737_pwm6_chmod_attr },
  1797. };
  1798. /*
  1799. * Pwm[1-3] are read-writeable if the associated pwm is in manual mode and the
  1800. * chip is not locked. Otherwise they are read-only.
  1801. */
  1802. static struct attribute *dme1737_pwm_chmod_attr[] = {
  1803. &sensor_dev_attr_pwm1.dev_attr.attr,
  1804. &sensor_dev_attr_pwm2.dev_attr.attr,
  1805. &sensor_dev_attr_pwm3.dev_attr.attr,
  1806. };
  1807. /* ---------------------------------------------------------------------
  1808. * Super-IO functions
  1809. * --------------------------------------------------------------------- */
  1810. static inline void dme1737_sio_enter(int sio_cip)
  1811. {
  1812. outb(0x55, sio_cip);
  1813. }
  1814. static inline void dme1737_sio_exit(int sio_cip)
  1815. {
  1816. outb(0xaa, sio_cip);
  1817. }
  1818. static inline int dme1737_sio_inb(int sio_cip, int reg)
  1819. {
  1820. outb(reg, sio_cip);
  1821. return inb(sio_cip + 1);
  1822. }
  1823. static inline void dme1737_sio_outb(int sio_cip, int reg, int val)
  1824. {
  1825. outb(reg, sio_cip);
  1826. outb(val, sio_cip + 1);
  1827. }
  1828. /* ---------------------------------------------------------------------
  1829. * Device initialization
  1830. * --------------------------------------------------------------------- */
  1831. static int dme1737_i2c_get_features(int, struct dme1737_data*);
  1832. static void dme1737_chmod_file(struct device *dev,
  1833. struct attribute *attr, umode_t mode)
  1834. {
  1835. if (sysfs_chmod_file(&dev->kobj, attr, mode)) {
  1836. dev_warn(dev, "Failed to change permissions of %s.\n",
  1837. attr->name);
  1838. }
  1839. }
  1840. static void dme1737_chmod_group(struct device *dev,
  1841. const struct attribute_group *group,
  1842. umode_t mode)
  1843. {
  1844. struct attribute **attr;
  1845. for (attr = group->attrs; *attr; attr++)
  1846. dme1737_chmod_file(dev, *attr, mode);
  1847. }
  1848. static void dme1737_remove_files(struct device *dev)
  1849. {
  1850. struct dme1737_data *data = dev_get_drvdata(dev);
  1851. int ix;
  1852. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1853. if (data->has_features & HAS_FAN(ix)) {
  1854. sysfs_remove_group(&dev->kobj,
  1855. &dme1737_fan_group[ix]);
  1856. }
  1857. }
  1858. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1859. if (data->has_features & HAS_PWM(ix)) {
  1860. sysfs_remove_group(&dev->kobj,
  1861. &dme1737_pwm_group[ix]);
  1862. if ((data->has_features & HAS_PWM_MIN) && ix < 3) {
  1863. sysfs_remove_file(&dev->kobj,
  1864. dme1737_auto_pwm_min_attr[ix]);
  1865. }
  1866. }
  1867. }
  1868. if (data->has_features & HAS_TEMP_OFFSET)
  1869. sysfs_remove_group(&dev->kobj, &dme1737_temp_offset_group);
  1870. if (data->has_features & HAS_VID)
  1871. sysfs_remove_group(&dev->kobj, &dme1737_vid_group);
  1872. if (data->has_features & HAS_ZONE3)
  1873. sysfs_remove_group(&dev->kobj, &dme1737_zone3_group);
  1874. if (data->has_features & HAS_ZONE_HYST)
  1875. sysfs_remove_group(&dev->kobj, &dme1737_zone_hyst_group);
  1876. if (data->has_features & HAS_IN7)
  1877. sysfs_remove_group(&dev->kobj, &dme1737_in7_group);
  1878. sysfs_remove_group(&dev->kobj, &dme1737_group);
  1879. if (!data->client)
  1880. sysfs_remove_file(&dev->kobj, &dev_attr_name.attr);
  1881. }
  1882. static int dme1737_create_files(struct device *dev)
  1883. {
  1884. struct dme1737_data *data = dev_get_drvdata(dev);
  1885. int err, ix;
  1886. /* Create a name attribute for ISA devices */
  1887. if (!data->client) {
  1888. err = sysfs_create_file(&dev->kobj, &dev_attr_name.attr);
  1889. if (err)
  1890. goto exit;
  1891. }
  1892. /* Create standard sysfs attributes */
  1893. err = sysfs_create_group(&dev->kobj, &dme1737_group);
  1894. if (err)
  1895. goto exit_remove;
  1896. /* Create chip-dependent sysfs attributes */
  1897. if (data->has_features & HAS_TEMP_OFFSET) {
  1898. err = sysfs_create_group(&dev->kobj,
  1899. &dme1737_temp_offset_group);
  1900. if (err)
  1901. goto exit_remove;
  1902. }
  1903. if (data->has_features & HAS_VID) {
  1904. err = sysfs_create_group(&dev->kobj, &dme1737_vid_group);
  1905. if (err)
  1906. goto exit_remove;
  1907. }
  1908. if (data->has_features & HAS_ZONE3) {
  1909. err = sysfs_create_group(&dev->kobj, &dme1737_zone3_group);
  1910. if (err)
  1911. goto exit_remove;
  1912. }
  1913. if (data->has_features & HAS_ZONE_HYST) {
  1914. err = sysfs_create_group(&dev->kobj, &dme1737_zone_hyst_group);
  1915. if (err)
  1916. goto exit_remove;
  1917. }
  1918. if (data->has_features & HAS_IN7) {
  1919. err = sysfs_create_group(&dev->kobj, &dme1737_in7_group);
  1920. if (err)
  1921. goto exit_remove;
  1922. }
  1923. /* Create fan sysfs attributes */
  1924. for (ix = 0; ix < ARRAY_SIZE(dme1737_fan_group); ix++) {
  1925. if (data->has_features & HAS_FAN(ix)) {
  1926. err = sysfs_create_group(&dev->kobj,
  1927. &dme1737_fan_group[ix]);
  1928. if (err)
  1929. goto exit_remove;
  1930. }
  1931. }
  1932. /* Create PWM sysfs attributes */
  1933. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_group); ix++) {
  1934. if (data->has_features & HAS_PWM(ix)) {
  1935. err = sysfs_create_group(&dev->kobj,
  1936. &dme1737_pwm_group[ix]);
  1937. if (err)
  1938. goto exit_remove;
  1939. if ((data->has_features & HAS_PWM_MIN) && (ix < 3)) {
  1940. err = sysfs_create_file(&dev->kobj,
  1941. dme1737_auto_pwm_min_attr[ix]);
  1942. if (err)
  1943. goto exit_remove;
  1944. }
  1945. }
  1946. }
  1947. /*
  1948. * Inform if the device is locked. Otherwise change the permissions of
  1949. * selected attributes from read-only to read-writeable.
  1950. */
  1951. if (data->config & 0x02) {
  1952. dev_info(dev, "Device is locked. Some attributes "
  1953. "will be read-only.\n");
  1954. } else {
  1955. /* Change permissions of zone sysfs attributes */
  1956. dme1737_chmod_group(dev, &dme1737_zone_chmod_group,
  1957. S_IRUGO | S_IWUSR);
  1958. /* Change permissions of chip-dependent sysfs attributes */
  1959. if (data->has_features & HAS_TEMP_OFFSET) {
  1960. dme1737_chmod_group(dev, &dme1737_temp_offset_group,
  1961. S_IRUGO | S_IWUSR);
  1962. }
  1963. if (data->has_features & HAS_ZONE3) {
  1964. dme1737_chmod_group(dev, &dme1737_zone3_chmod_group,
  1965. S_IRUGO | S_IWUSR);
  1966. }
  1967. if (data->has_features & HAS_ZONE_HYST) {
  1968. dme1737_chmod_group(dev, &dme1737_zone_hyst_group,
  1969. S_IRUGO | S_IWUSR);
  1970. }
  1971. /* Change permissions of PWM sysfs attributes */
  1972. for (ix = 0; ix < ARRAY_SIZE(dme1737_pwm_chmod_group); ix++) {
  1973. if (data->has_features & HAS_PWM(ix)) {
  1974. dme1737_chmod_group(dev,
  1975. &dme1737_pwm_chmod_group[ix],
  1976. S_IRUGO | S_IWUSR);
  1977. if ((data->has_features & HAS_PWM_MIN) &&
  1978. ix < 3) {
  1979. dme1737_chmod_file(dev,
  1980. dme1737_auto_pwm_min_attr[ix],
  1981. S_IRUGO | S_IWUSR);
  1982. }
  1983. }
  1984. }
  1985. /* Change permissions of pwm[1-3] if in manual mode */
  1986. for (ix = 0; ix < 3; ix++) {
  1987. if ((data->has_features & HAS_PWM(ix)) &&
  1988. (PWM_EN_FROM_REG(data->pwm_config[ix]) == 1)) {
  1989. dme1737_chmod_file(dev,
  1990. dme1737_pwm_chmod_attr[ix],
  1991. S_IRUGO | S_IWUSR);
  1992. }
  1993. }
  1994. }
  1995. return 0;
  1996. exit_remove:
  1997. dme1737_remove_files(dev);
  1998. exit:
  1999. return err;
  2000. }
  2001. static int dme1737_init_device(struct device *dev)
  2002. {
  2003. struct dme1737_data *data = dev_get_drvdata(dev);
  2004. struct i2c_client *client = data->client;
  2005. int ix;
  2006. u8 reg;
  2007. /* Point to the right nominal voltages array */
  2008. data->in_nominal = IN_NOMINAL(data->type);
  2009. data->config = dme1737_read(data, DME1737_REG_CONFIG);
  2010. /* Inform if part is not monitoring/started */
  2011. if (!(data->config & 0x01)) {
  2012. if (!force_start) {
  2013. dev_err(dev, "Device is not monitoring. "
  2014. "Use the force_start load parameter to "
  2015. "override.\n");
  2016. return -EFAULT;
  2017. }
  2018. /* Force monitoring */
  2019. data->config |= 0x01;
  2020. dme1737_write(data, DME1737_REG_CONFIG, data->config);
  2021. }
  2022. /* Inform if part is not ready */
  2023. if (!(data->config & 0x04)) {
  2024. dev_err(dev, "Device is not ready.\n");
  2025. return -EFAULT;
  2026. }
  2027. /*
  2028. * Determine which optional fan and pwm features are enabled (only
  2029. * valid for I2C devices)
  2030. */
  2031. if (client) { /* I2C chip */
  2032. data->config2 = dme1737_read(data, DME1737_REG_CONFIG2);
  2033. /* Check if optional fan3 input is enabled */
  2034. if (data->config2 & 0x04)
  2035. data->has_features |= HAS_FAN(2);
  2036. /*
  2037. * Fan4 and pwm3 are only available if the client's I2C address
  2038. * is the default 0x2e. Otherwise the I/Os associated with
  2039. * these functions are used for addr enable/select.
  2040. */
  2041. if (client->addr == 0x2e)
  2042. data->has_features |= HAS_FAN(3) | HAS_PWM(2);
  2043. /*
  2044. * Determine which of the optional fan[5-6] and pwm[5-6]
  2045. * features are enabled. For this, we need to query the runtime
  2046. * registers through the Super-IO LPC interface. Try both
  2047. * config ports 0x2e and 0x4e.
  2048. */
  2049. if (dme1737_i2c_get_features(0x2e, data) &&
  2050. dme1737_i2c_get_features(0x4e, data)) {
  2051. dev_warn(dev, "Failed to query Super-IO for optional "
  2052. "features.\n");
  2053. }
  2054. }
  2055. /* Fan[1-2] and pwm[1-2] are present in all chips */
  2056. data->has_features |= HAS_FAN(0) | HAS_FAN(1) | HAS_PWM(0) | HAS_PWM(1);
  2057. /* Chip-dependent features */
  2058. switch (data->type) {
  2059. case dme1737:
  2060. data->has_features |= HAS_TEMP_OFFSET | HAS_VID | HAS_ZONE3 |
  2061. HAS_ZONE_HYST | HAS_PWM_MIN;
  2062. break;
  2063. case sch311x:
  2064. data->has_features |= HAS_TEMP_OFFSET | HAS_ZONE3 |
  2065. HAS_ZONE_HYST | HAS_PWM_MIN | HAS_FAN(2) | HAS_PWM(2);
  2066. break;
  2067. case sch5027:
  2068. data->has_features |= HAS_ZONE3;
  2069. break;
  2070. case sch5127:
  2071. data->has_features |= HAS_FAN(2) | HAS_PWM(2) | HAS_IN7;
  2072. break;
  2073. default:
  2074. break;
  2075. }
  2076. dev_info(dev, "Optional features: pwm3=%s, pwm5=%s, pwm6=%s, "
  2077. "fan3=%s, fan4=%s, fan5=%s, fan6=%s.\n",
  2078. (data->has_features & HAS_PWM(2)) ? "yes" : "no",
  2079. (data->has_features & HAS_PWM(4)) ? "yes" : "no",
  2080. (data->has_features & HAS_PWM(5)) ? "yes" : "no",
  2081. (data->has_features & HAS_FAN(2)) ? "yes" : "no",
  2082. (data->has_features & HAS_FAN(3)) ? "yes" : "no",
  2083. (data->has_features & HAS_FAN(4)) ? "yes" : "no",
  2084. (data->has_features & HAS_FAN(5)) ? "yes" : "no");
  2085. reg = dme1737_read(data, DME1737_REG_TACH_PWM);
  2086. /* Inform if fan-to-pwm mapping differs from the default */
  2087. if (client && reg != 0xa4) { /* I2C chip */
  2088. dev_warn(dev, "Non-standard fan to pwm mapping: "
  2089. "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d, "
  2090. "fan4->pwm%d. Please report to the driver "
  2091. "maintainer.\n",
  2092. (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
  2093. ((reg >> 4) & 0x03) + 1, ((reg >> 6) & 0x03) + 1);
  2094. } else if (!client && reg != 0x24) { /* ISA chip */
  2095. dev_warn(dev, "Non-standard fan to pwm mapping: "
  2096. "fan1->pwm%d, fan2->pwm%d, fan3->pwm%d. "
  2097. "Please report to the driver maintainer.\n",
  2098. (reg & 0x03) + 1, ((reg >> 2) & 0x03) + 1,
  2099. ((reg >> 4) & 0x03) + 1);
  2100. }
  2101. /*
  2102. * Switch pwm[1-3] to manual mode if they are currently disabled and
  2103. * set the duty-cycles to 0% (which is identical to the PWMs being
  2104. * disabled).
  2105. */
  2106. if (!(data->config & 0x02)) {
  2107. for (ix = 0; ix < 3; ix++) {
  2108. data->pwm_config[ix] = dme1737_read(data,
  2109. DME1737_REG_PWM_CONFIG(ix));
  2110. if ((data->has_features & HAS_PWM(ix)) &&
  2111. (PWM_EN_FROM_REG(data->pwm_config[ix]) == -1)) {
  2112. dev_info(dev, "Switching pwm%d to "
  2113. "manual mode.\n", 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 = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
  2211. if (!data) {
  2212. err = -ENOMEM;
  2213. goto exit;
  2214. }
  2215. i2c_set_clientdata(client, data);
  2216. data->type = id->driver_data;
  2217. data->client = client;
  2218. data->name = client->name;
  2219. mutex_init(&data->update_lock);
  2220. /* Initialize the DME1737 chip */
  2221. err = dme1737_init_device(dev);
  2222. if (err) {
  2223. dev_err(dev, "Failed to initialize device.\n");
  2224. goto exit_kfree;
  2225. }
  2226. /* Create sysfs files */
  2227. err = dme1737_create_files(dev);
  2228. if (err) {
  2229. dev_err(dev, "Failed to create sysfs files.\n");
  2230. goto exit_kfree;
  2231. }
  2232. /* Register device */
  2233. data->hwmon_dev = hwmon_device_register(dev);
  2234. if (IS_ERR(data->hwmon_dev)) {
  2235. dev_err(dev, "Failed to register device.\n");
  2236. err = PTR_ERR(data->hwmon_dev);
  2237. goto exit_remove;
  2238. }
  2239. return 0;
  2240. exit_remove:
  2241. dme1737_remove_files(dev);
  2242. exit_kfree:
  2243. kfree(data);
  2244. exit:
  2245. return err;
  2246. }
  2247. static int dme1737_i2c_remove(struct i2c_client *client)
  2248. {
  2249. struct dme1737_data *data = i2c_get_clientdata(client);
  2250. hwmon_device_unregister(data->hwmon_dev);
  2251. dme1737_remove_files(&client->dev);
  2252. kfree(data);
  2253. return 0;
  2254. }
  2255. static const struct i2c_device_id dme1737_id[] = {
  2256. { "dme1737", dme1737 },
  2257. { "sch5027", sch5027 },
  2258. { }
  2259. };
  2260. MODULE_DEVICE_TABLE(i2c, dme1737_id);
  2261. static struct i2c_driver dme1737_i2c_driver = {
  2262. .class = I2C_CLASS_HWMON,
  2263. .driver = {
  2264. .name = "dme1737",
  2265. },
  2266. .probe = dme1737_i2c_probe,
  2267. .remove = dme1737_i2c_remove,
  2268. .id_table = dme1737_id,
  2269. .detect = dme1737_i2c_detect,
  2270. .address_list = normal_i2c,
  2271. };
  2272. /* ---------------------------------------------------------------------
  2273. * ISA device detection and registration
  2274. * --------------------------------------------------------------------- */
  2275. static int __init dme1737_isa_detect(int sio_cip, unsigned short *addr)
  2276. {
  2277. int err = 0, reg;
  2278. unsigned short base_addr;
  2279. dme1737_sio_enter(sio_cip);
  2280. /*
  2281. * Check device ID
  2282. * We currently know about SCH3112, SCH3114, SCH3116, and SCH5127
  2283. */
  2284. reg = force_id ? force_id : dme1737_sio_inb(sio_cip, 0x20);
  2285. if (!(reg == SCH3112_ID || reg == SCH3114_ID || reg == SCH3116_ID ||
  2286. reg == SCH5127_ID)) {
  2287. err = -ENODEV;
  2288. goto exit;
  2289. }
  2290. /* Select logical device A (runtime registers) */
  2291. dme1737_sio_outb(sio_cip, 0x07, 0x0a);
  2292. /* Get the base address of the runtime registers */
  2293. base_addr = (dme1737_sio_inb(sio_cip, 0x60) << 8) |
  2294. dme1737_sio_inb(sio_cip, 0x61);
  2295. if (!base_addr) {
  2296. pr_err("Base address not set\n");
  2297. err = -ENODEV;
  2298. goto exit;
  2299. }
  2300. /*
  2301. * Access to the hwmon registers is through an index/data register
  2302. * pair located at offset 0x70/0x71.
  2303. */
  2304. *addr = base_addr + 0x70;
  2305. exit:
  2306. dme1737_sio_exit(sio_cip);
  2307. return err;
  2308. }
  2309. static int __init dme1737_isa_device_add(unsigned short addr)
  2310. {
  2311. struct resource res = {
  2312. .start = addr,
  2313. .end = addr + DME1737_EXTENT - 1,
  2314. .name = "dme1737",
  2315. .flags = IORESOURCE_IO,
  2316. };
  2317. int err;
  2318. err = acpi_check_resource_conflict(&res);
  2319. if (err)
  2320. goto exit;
  2321. pdev = platform_device_alloc("dme1737", addr);
  2322. if (!pdev) {
  2323. pr_err("Failed to allocate device\n");
  2324. err = -ENOMEM;
  2325. goto exit;
  2326. }
  2327. err = platform_device_add_resources(pdev, &res, 1);
  2328. if (err) {
  2329. pr_err("Failed to add device resource (err = %d)\n", err);
  2330. goto exit_device_put;
  2331. }
  2332. err = platform_device_add(pdev);
  2333. if (err) {
  2334. pr_err("Failed to add device (err = %d)\n", err);
  2335. goto exit_device_put;
  2336. }
  2337. return 0;
  2338. exit_device_put:
  2339. platform_device_put(pdev);
  2340. pdev = NULL;
  2341. exit:
  2342. return err;
  2343. }
  2344. static int __devinit dme1737_isa_probe(struct platform_device *pdev)
  2345. {
  2346. u8 company, device;
  2347. struct resource *res;
  2348. struct dme1737_data *data;
  2349. struct device *dev = &pdev->dev;
  2350. int err;
  2351. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  2352. if (!request_region(res->start, DME1737_EXTENT, "dme1737")) {
  2353. dev_err(dev, "Failed to request region 0x%04x-0x%04x.\n",
  2354. (unsigned short)res->start,
  2355. (unsigned short)res->start + DME1737_EXTENT - 1);
  2356. err = -EBUSY;
  2357. goto exit;
  2358. }
  2359. data = kzalloc(sizeof(struct dme1737_data), GFP_KERNEL);
  2360. if (!data) {
  2361. err = -ENOMEM;
  2362. goto exit_release_region;
  2363. }
  2364. data->addr = res->start;
  2365. platform_set_drvdata(pdev, data);
  2366. /* Skip chip detection if module is loaded with force_id parameter */
  2367. switch (force_id) {
  2368. case SCH3112_ID:
  2369. case SCH3114_ID:
  2370. case SCH3116_ID:
  2371. data->type = sch311x;
  2372. break;
  2373. case SCH5127_ID:
  2374. data->type = sch5127;
  2375. break;
  2376. default:
  2377. company = dme1737_read(data, DME1737_REG_COMPANY);
  2378. device = dme1737_read(data, DME1737_REG_DEVICE);
  2379. if ((company == DME1737_COMPANY_SMSC) &&
  2380. (device == SCH311X_DEVICE)) {
  2381. data->type = sch311x;
  2382. } else if ((company == DME1737_COMPANY_SMSC) &&
  2383. (device == SCH5127_DEVICE)) {
  2384. data->type = sch5127;
  2385. } else {
  2386. err = -ENODEV;
  2387. goto exit_kfree;
  2388. }
  2389. }
  2390. if (data->type == sch5127)
  2391. data->name = "sch5127";
  2392. else
  2393. data->name = "sch311x";
  2394. /* Initialize the mutex */
  2395. mutex_init(&data->update_lock);
  2396. dev_info(dev, "Found a %s chip at 0x%04x\n",
  2397. data->type == sch5127 ? "SCH5127" : "SCH311x", data->addr);
  2398. /* Initialize the chip */
  2399. err = dme1737_init_device(dev);
  2400. if (err) {
  2401. dev_err(dev, "Failed to initialize device.\n");
  2402. goto exit_kfree;
  2403. }
  2404. /* Create sysfs files */
  2405. err = dme1737_create_files(dev);
  2406. if (err) {
  2407. dev_err(dev, "Failed to create sysfs files.\n");
  2408. goto exit_kfree;
  2409. }
  2410. /* Register device */
  2411. data->hwmon_dev = hwmon_device_register(dev);
  2412. if (IS_ERR(data->hwmon_dev)) {
  2413. dev_err(dev, "Failed to register device.\n");
  2414. err = PTR_ERR(data->hwmon_dev);
  2415. goto exit_remove_files;
  2416. }
  2417. return 0;
  2418. exit_remove_files:
  2419. dme1737_remove_files(dev);
  2420. exit_kfree:
  2421. platform_set_drvdata(pdev, NULL);
  2422. kfree(data);
  2423. exit_release_region:
  2424. release_region(res->start, DME1737_EXTENT);
  2425. exit:
  2426. return err;
  2427. }
  2428. static int __devexit dme1737_isa_remove(struct platform_device *pdev)
  2429. {
  2430. struct dme1737_data *data = platform_get_drvdata(pdev);
  2431. hwmon_device_unregister(data->hwmon_dev);
  2432. dme1737_remove_files(&pdev->dev);
  2433. release_region(data->addr, DME1737_EXTENT);
  2434. platform_set_drvdata(pdev, NULL);
  2435. kfree(data);
  2436. return 0;
  2437. }
  2438. static struct platform_driver dme1737_isa_driver = {
  2439. .driver = {
  2440. .owner = THIS_MODULE,
  2441. .name = "dme1737",
  2442. },
  2443. .probe = dme1737_isa_probe,
  2444. .remove = __devexit_p(dme1737_isa_remove),
  2445. };
  2446. /* ---------------------------------------------------------------------
  2447. * Module initialization and cleanup
  2448. * --------------------------------------------------------------------- */
  2449. static int __init dme1737_init(void)
  2450. {
  2451. int err;
  2452. unsigned short addr;
  2453. err = i2c_add_driver(&dme1737_i2c_driver);
  2454. if (err)
  2455. goto exit;
  2456. if (dme1737_isa_detect(0x2e, &addr) &&
  2457. dme1737_isa_detect(0x4e, &addr) &&
  2458. (!probe_all_addr ||
  2459. (dme1737_isa_detect(0x162e, &addr) &&
  2460. dme1737_isa_detect(0x164e, &addr)))) {
  2461. /* Return 0 if we didn't find an ISA device */
  2462. return 0;
  2463. }
  2464. err = platform_driver_register(&dme1737_isa_driver);
  2465. if (err)
  2466. goto exit_del_i2c_driver;
  2467. /* Sets global pdev as a side effect */
  2468. err = dme1737_isa_device_add(addr);
  2469. if (err)
  2470. goto exit_del_isa_driver;
  2471. return 0;
  2472. exit_del_isa_driver:
  2473. platform_driver_unregister(&dme1737_isa_driver);
  2474. exit_del_i2c_driver:
  2475. i2c_del_driver(&dme1737_i2c_driver);
  2476. exit:
  2477. return err;
  2478. }
  2479. static void __exit dme1737_exit(void)
  2480. {
  2481. if (pdev) {
  2482. platform_device_unregister(pdev);
  2483. platform_driver_unregister(&dme1737_isa_driver);
  2484. }
  2485. i2c_del_driver(&dme1737_i2c_driver);
  2486. }
  2487. MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
  2488. MODULE_DESCRIPTION("DME1737 sensors");
  2489. MODULE_LICENSE("GPL");
  2490. module_init(dme1737_init);
  2491. module_exit(dme1737_exit);