dme1737.c 74 KB

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