it87.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  1. /*
  2. * it87.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring.
  4. *
  5. * The IT8705F is an LPC-based Super I/O part that contains UARTs, a
  6. * parallel port, an IR port, a MIDI port, a floppy controller, etc., in
  7. * addition to an Environment Controller (Enhanced Hardware Monitor and
  8. * Fan Controller)
  9. *
  10. * This driver supports only the Environment Controller in the IT8705F and
  11. * similar parts. The other devices are supported by different drivers.
  12. *
  13. * Supports: IT8705F Super I/O chip w/LPC interface
  14. * IT8712F Super I/O chip w/LPC interface
  15. * IT8716F Super I/O chip w/LPC interface
  16. * IT8718F Super I/O chip w/LPC interface
  17. * IT8720F Super I/O chip w/LPC interface
  18. * IT8721F Super I/O chip w/LPC interface
  19. * IT8726F Super I/O chip w/LPC interface
  20. * IT8728F Super I/O chip w/LPC interface
  21. * IT8758E Super I/O chip w/LPC interface
  22. * Sis950 A clone of the IT8705F
  23. *
  24. * Copyright (C) 2001 Chris Gauthron
  25. * Copyright (C) 2005-2010 Jean Delvare <khali@linux-fr.org>
  26. *
  27. * This program is free software; you can redistribute it and/or modify
  28. * it under the terms of the GNU General Public License as published by
  29. * the Free Software Foundation; either version 2 of the License, or
  30. * (at your option) any later version.
  31. *
  32. * This program is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35. * GNU General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU General Public License
  38. * along with this program; if not, write to the Free Software
  39. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  40. */
  41. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  42. #include <linux/module.h>
  43. #include <linux/init.h>
  44. #include <linux/slab.h>
  45. #include <linux/jiffies.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/hwmon.h>
  48. #include <linux/hwmon-sysfs.h>
  49. #include <linux/hwmon-vid.h>
  50. #include <linux/err.h>
  51. #include <linux/mutex.h>
  52. #include <linux/sysfs.h>
  53. #include <linux/string.h>
  54. #include <linux/dmi.h>
  55. #include <linux/acpi.h>
  56. #include <linux/io.h>
  57. #define DRVNAME "it87"
  58. enum chips { it87, it8712, it8716, it8718, it8720, it8721, it8728 };
  59. static unsigned short force_id;
  60. module_param(force_id, ushort, 0);
  61. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  62. static struct platform_device *pdev;
  63. #define REG 0x2e /* The register to read/write */
  64. #define DEV 0x07 /* Register: Logical device select */
  65. #define VAL 0x2f /* The value to read/write */
  66. #define PME 0x04 /* The device with the fan registers in it */
  67. /* The device with the IT8718F/IT8720F VID value in it */
  68. #define GPIO 0x07
  69. #define DEVID 0x20 /* Register: Device ID */
  70. #define DEVREV 0x22 /* Register: Device Revision */
  71. static inline int superio_inb(int reg)
  72. {
  73. outb(reg, REG);
  74. return inb(VAL);
  75. }
  76. static inline void superio_outb(int reg, int val)
  77. {
  78. outb(reg, REG);
  79. outb(val, VAL);
  80. }
  81. static int superio_inw(int reg)
  82. {
  83. int val;
  84. outb(reg++, REG);
  85. val = inb(VAL) << 8;
  86. outb(reg, REG);
  87. val |= inb(VAL);
  88. return val;
  89. }
  90. static inline void superio_select(int ldn)
  91. {
  92. outb(DEV, REG);
  93. outb(ldn, VAL);
  94. }
  95. static inline int superio_enter(void)
  96. {
  97. /*
  98. * Try to reserve REG and REG + 1 for exclusive access.
  99. */
  100. if (!request_muxed_region(REG, 2, DRVNAME))
  101. return -EBUSY;
  102. outb(0x87, REG);
  103. outb(0x01, REG);
  104. outb(0x55, REG);
  105. outb(0x55, REG);
  106. return 0;
  107. }
  108. static inline void superio_exit(void)
  109. {
  110. outb(0x02, REG);
  111. outb(0x02, VAL);
  112. release_region(REG, 2);
  113. }
  114. /* Logical device 4 registers */
  115. #define IT8712F_DEVID 0x8712
  116. #define IT8705F_DEVID 0x8705
  117. #define IT8716F_DEVID 0x8716
  118. #define IT8718F_DEVID 0x8718
  119. #define IT8720F_DEVID 0x8720
  120. #define IT8721F_DEVID 0x8721
  121. #define IT8726F_DEVID 0x8726
  122. #define IT8728F_DEVID 0x8728
  123. #define IT87_ACT_REG 0x30
  124. #define IT87_BASE_REG 0x60
  125. /* Logical device 7 registers (IT8712F and later) */
  126. #define IT87_SIO_GPIO3_REG 0x27
  127. #define IT87_SIO_GPIO5_REG 0x29
  128. #define IT87_SIO_PINX2_REG 0x2c /* Pin selection */
  129. #define IT87_SIO_VID_REG 0xfc /* VID value */
  130. #define IT87_SIO_BEEP_PIN_REG 0xf6 /* Beep pin mapping */
  131. /* Update battery voltage after every reading if true */
  132. static bool update_vbat;
  133. /* Not all BIOSes properly configure the PWM registers */
  134. static bool fix_pwm_polarity;
  135. /* Many IT87 constants specified below */
  136. /* Length of ISA address segment */
  137. #define IT87_EXTENT 8
  138. /* Length of ISA address segment for Environmental Controller */
  139. #define IT87_EC_EXTENT 2
  140. /* Offset of EC registers from ISA base address */
  141. #define IT87_EC_OFFSET 5
  142. /* Where are the ISA address/data registers relative to the EC base address */
  143. #define IT87_ADDR_REG_OFFSET 0
  144. #define IT87_DATA_REG_OFFSET 1
  145. /*----- The IT87 registers -----*/
  146. #define IT87_REG_CONFIG 0x00
  147. #define IT87_REG_ALARM1 0x01
  148. #define IT87_REG_ALARM2 0x02
  149. #define IT87_REG_ALARM3 0x03
  150. /*
  151. * The IT8718F and IT8720F have the VID value in a different register, in
  152. * Super-I/O configuration space.
  153. */
  154. #define IT87_REG_VID 0x0a
  155. /*
  156. * The IT8705F and IT8712F earlier than revision 0x08 use register 0x0b
  157. * for fan divisors. Later IT8712F revisions must use 16-bit tachometer
  158. * mode.
  159. */
  160. #define IT87_REG_FAN_DIV 0x0b
  161. #define IT87_REG_FAN_16BIT 0x0c
  162. /* Monitors: 9 voltage (0 to 7, battery), 3 temp (1 to 3), 3 fan (1 to 3) */
  163. static const u8 IT87_REG_FAN[] = { 0x0d, 0x0e, 0x0f, 0x80, 0x82 };
  164. static const u8 IT87_REG_FAN_MIN[] = { 0x10, 0x11, 0x12, 0x84, 0x86 };
  165. static const u8 IT87_REG_FANX[] = { 0x18, 0x19, 0x1a, 0x81, 0x83 };
  166. static const u8 IT87_REG_FANX_MIN[] = { 0x1b, 0x1c, 0x1d, 0x85, 0x87 };
  167. #define IT87_REG_FAN_MAIN_CTRL 0x13
  168. #define IT87_REG_FAN_CTL 0x14
  169. #define IT87_REG_PWM(nr) (0x15 + (nr))
  170. #define IT87_REG_PWM_DUTY(nr) (0x63 + (nr) * 8)
  171. #define IT87_REG_VIN(nr) (0x20 + (nr))
  172. #define IT87_REG_TEMP(nr) (0x29 + (nr))
  173. #define IT87_REG_VIN_MAX(nr) (0x30 + (nr) * 2)
  174. #define IT87_REG_VIN_MIN(nr) (0x31 + (nr) * 2)
  175. #define IT87_REG_TEMP_HIGH(nr) (0x40 + (nr) * 2)
  176. #define IT87_REG_TEMP_LOW(nr) (0x41 + (nr) * 2)
  177. #define IT87_REG_VIN_ENABLE 0x50
  178. #define IT87_REG_TEMP_ENABLE 0x51
  179. #define IT87_REG_BEEP_ENABLE 0x5c
  180. #define IT87_REG_CHIPID 0x58
  181. #define IT87_REG_AUTO_TEMP(nr, i) (0x60 + (nr) * 8 + (i))
  182. #define IT87_REG_AUTO_PWM(nr, i) (0x65 + (nr) * 8 + (i))
  183. struct it87_sio_data {
  184. enum chips type;
  185. /* Values read from Super-I/O config space */
  186. u8 revision;
  187. u8 vid_value;
  188. u8 beep_pin;
  189. u8 internal; /* Internal sensors can be labeled */
  190. /* Features skipped based on config or DMI */
  191. u8 skip_vid;
  192. u8 skip_fan;
  193. u8 skip_pwm;
  194. };
  195. /*
  196. * For each registered chip, we need to keep some data in memory.
  197. * The structure is dynamically allocated.
  198. */
  199. struct it87_data {
  200. struct device *hwmon_dev;
  201. enum chips type;
  202. u8 revision;
  203. unsigned short addr;
  204. const char *name;
  205. struct mutex update_lock;
  206. char valid; /* !=0 if following fields are valid */
  207. unsigned long last_updated; /* In jiffies */
  208. u16 in_scaled; /* Internal voltage sensors are scaled */
  209. u8 in[9]; /* Register value */
  210. u8 in_max[8]; /* Register value */
  211. u8 in_min[8]; /* Register value */
  212. u8 has_fan; /* Bitfield, fans enabled */
  213. u16 fan[5]; /* Register values, possibly combined */
  214. u16 fan_min[5]; /* Register values, possibly combined */
  215. s8 temp[3]; /* Register value */
  216. s8 temp_high[3]; /* Register value */
  217. s8 temp_low[3]; /* Register value */
  218. u8 sensor; /* Register value */
  219. u8 fan_div[3]; /* Register encoding, shifted right */
  220. u8 vid; /* Register encoding, combined */
  221. u8 vrm;
  222. u32 alarms; /* Register encoding, combined */
  223. u8 beeps; /* Register encoding */
  224. u8 fan_main_ctrl; /* Register value */
  225. u8 fan_ctl; /* Register value */
  226. /*
  227. * The following 3 arrays correspond to the same registers up to
  228. * the IT8720F. The meaning of bits 6-0 depends on the value of bit
  229. * 7, and we want to preserve settings on mode changes, so we have
  230. * to track all values separately.
  231. * Starting with the IT8721F, the manual PWM duty cycles are stored
  232. * in separate registers (8-bit values), so the separate tracking
  233. * is no longer needed, but it is still done to keep the driver
  234. * simple.
  235. */
  236. u8 pwm_ctrl[3]; /* Register value */
  237. u8 pwm_duty[3]; /* Manual PWM value set by user */
  238. u8 pwm_temp_map[3]; /* PWM to temp. chan. mapping (bits 1-0) */
  239. /* Automatic fan speed control registers */
  240. u8 auto_pwm[3][4]; /* [nr][3] is hard-coded */
  241. s8 auto_temp[3][5]; /* [nr][0] is point1_temp_hyst */
  242. };
  243. static inline int has_12mv_adc(const struct it87_data *data)
  244. {
  245. /*
  246. * IT8721F and later have a 12 mV ADC, also with internal scaling
  247. * on selected inputs.
  248. */
  249. return data->type == it8721
  250. || data->type == it8728;
  251. }
  252. static inline int has_newer_autopwm(const struct it87_data *data)
  253. {
  254. /*
  255. * IT8721F and later have separate registers for the temperature
  256. * mapping and the manual duty cycle.
  257. */
  258. return data->type == it8721
  259. || data->type == it8728;
  260. }
  261. static u8 in_to_reg(const struct it87_data *data, int nr, long val)
  262. {
  263. long lsb;
  264. if (has_12mv_adc(data)) {
  265. if (data->in_scaled & (1 << nr))
  266. lsb = 24;
  267. else
  268. lsb = 12;
  269. } else
  270. lsb = 16;
  271. val = DIV_ROUND_CLOSEST(val, lsb);
  272. return SENSORS_LIMIT(val, 0, 255);
  273. }
  274. static int in_from_reg(const struct it87_data *data, int nr, int val)
  275. {
  276. if (has_12mv_adc(data)) {
  277. if (data->in_scaled & (1 << nr))
  278. return val * 24;
  279. else
  280. return val * 12;
  281. } else
  282. return val * 16;
  283. }
  284. static inline u8 FAN_TO_REG(long rpm, int div)
  285. {
  286. if (rpm == 0)
  287. return 255;
  288. rpm = SENSORS_LIMIT(rpm, 1, 1000000);
  289. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1,
  290. 254);
  291. }
  292. static inline u16 FAN16_TO_REG(long rpm)
  293. {
  294. if (rpm == 0)
  295. return 0xffff;
  296. return SENSORS_LIMIT((1350000 + rpm) / (rpm * 2), 1, 0xfffe);
  297. }
  298. #define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 255 ? 0 : \
  299. 1350000 / ((val) * (div)))
  300. /* The divider is fixed to 2 in 16-bit mode */
  301. #define FAN16_FROM_REG(val) ((val) == 0 ? -1 : (val) == 0xffff ? 0 : \
  302. 1350000 / ((val) * 2))
  303. #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val) < 0 ? (((val) - 500) / 1000) : \
  304. ((val) + 500) / 1000), -128, 127))
  305. #define TEMP_FROM_REG(val) ((val) * 1000)
  306. static u8 pwm_to_reg(const struct it87_data *data, long val)
  307. {
  308. if (has_newer_autopwm(data))
  309. return val;
  310. else
  311. return val >> 1;
  312. }
  313. static int pwm_from_reg(const struct it87_data *data, u8 reg)
  314. {
  315. if (has_newer_autopwm(data))
  316. return reg;
  317. else
  318. return (reg & 0x7f) << 1;
  319. }
  320. static int DIV_TO_REG(int val)
  321. {
  322. int answer = 0;
  323. while (answer < 7 && (val >>= 1))
  324. answer++;
  325. return answer;
  326. }
  327. #define DIV_FROM_REG(val) (1 << (val))
  328. static const unsigned int pwm_freq[8] = {
  329. 48000000 / 128,
  330. 24000000 / 128,
  331. 12000000 / 128,
  332. 8000000 / 128,
  333. 6000000 / 128,
  334. 3000000 / 128,
  335. 1500000 / 128,
  336. 750000 / 128,
  337. };
  338. static inline int has_16bit_fans(const struct it87_data *data)
  339. {
  340. /*
  341. * IT8705F Datasheet 0.4.1, 3h == Version G.
  342. * IT8712F Datasheet 0.9.1, section 8.3.5 indicates 8h == Version J.
  343. * These are the first revisions with 16-bit tachometer support.
  344. */
  345. return (data->type == it87 && data->revision >= 0x03)
  346. || (data->type == it8712 && data->revision >= 0x08)
  347. || data->type == it8716
  348. || data->type == it8718
  349. || data->type == it8720
  350. || data->type == it8721
  351. || data->type == it8728;
  352. }
  353. static inline int has_old_autopwm(const struct it87_data *data)
  354. {
  355. /*
  356. * The old automatic fan speed control interface is implemented
  357. * by IT8705F chips up to revision F and IT8712F chips up to
  358. * revision G.
  359. */
  360. return (data->type == it87 && data->revision < 0x03)
  361. || (data->type == it8712 && data->revision < 0x08);
  362. }
  363. static int it87_probe(struct platform_device *pdev);
  364. static int __devexit it87_remove(struct platform_device *pdev);
  365. static int it87_read_value(struct it87_data *data, u8 reg);
  366. static void it87_write_value(struct it87_data *data, u8 reg, u8 value);
  367. static struct it87_data *it87_update_device(struct device *dev);
  368. static int it87_check_pwm(struct device *dev);
  369. static void it87_init_device(struct platform_device *pdev);
  370. static struct platform_driver it87_driver = {
  371. .driver = {
  372. .owner = THIS_MODULE,
  373. .name = DRVNAME,
  374. },
  375. .probe = it87_probe,
  376. .remove = __devexit_p(it87_remove),
  377. };
  378. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  379. char *buf)
  380. {
  381. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  382. int nr = sensor_attr->index;
  383. struct it87_data *data = it87_update_device(dev);
  384. return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in[nr]));
  385. }
  386. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  387. char *buf)
  388. {
  389. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  390. int nr = sensor_attr->index;
  391. struct it87_data *data = it87_update_device(dev);
  392. return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_min[nr]));
  393. }
  394. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  395. char *buf)
  396. {
  397. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  398. int nr = sensor_attr->index;
  399. struct it87_data *data = it87_update_device(dev);
  400. return sprintf(buf, "%d\n", in_from_reg(data, nr, data->in_max[nr]));
  401. }
  402. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  403. const char *buf, size_t count)
  404. {
  405. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  406. int nr = sensor_attr->index;
  407. struct it87_data *data = dev_get_drvdata(dev);
  408. unsigned long val;
  409. if (kstrtoul(buf, 10, &val) < 0)
  410. return -EINVAL;
  411. mutex_lock(&data->update_lock);
  412. data->in_min[nr] = in_to_reg(data, nr, val);
  413. it87_write_value(data, IT87_REG_VIN_MIN(nr),
  414. data->in_min[nr]);
  415. mutex_unlock(&data->update_lock);
  416. return count;
  417. }
  418. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  419. const char *buf, size_t count)
  420. {
  421. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  422. int nr = sensor_attr->index;
  423. struct it87_data *data = dev_get_drvdata(dev);
  424. unsigned long val;
  425. if (kstrtoul(buf, 10, &val) < 0)
  426. return -EINVAL;
  427. mutex_lock(&data->update_lock);
  428. data->in_max[nr] = in_to_reg(data, nr, val);
  429. it87_write_value(data, IT87_REG_VIN_MAX(nr),
  430. data->in_max[nr]);
  431. mutex_unlock(&data->update_lock);
  432. return count;
  433. }
  434. #define show_in_offset(offset) \
  435. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  436. show_in, NULL, offset);
  437. #define limit_in_offset(offset) \
  438. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  439. show_in_min, set_in_min, offset); \
  440. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  441. show_in_max, set_in_max, offset);
  442. show_in_offset(0);
  443. limit_in_offset(0);
  444. show_in_offset(1);
  445. limit_in_offset(1);
  446. show_in_offset(2);
  447. limit_in_offset(2);
  448. show_in_offset(3);
  449. limit_in_offset(3);
  450. show_in_offset(4);
  451. limit_in_offset(4);
  452. show_in_offset(5);
  453. limit_in_offset(5);
  454. show_in_offset(6);
  455. limit_in_offset(6);
  456. show_in_offset(7);
  457. limit_in_offset(7);
  458. show_in_offset(8);
  459. /* 3 temperatures */
  460. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  461. char *buf)
  462. {
  463. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  464. int nr = sensor_attr->index;
  465. struct it87_data *data = it87_update_device(dev);
  466. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  467. }
  468. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  469. char *buf)
  470. {
  471. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  472. int nr = sensor_attr->index;
  473. struct it87_data *data = it87_update_device(dev);
  474. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[nr]));
  475. }
  476. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  477. char *buf)
  478. {
  479. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  480. int nr = sensor_attr->index;
  481. struct it87_data *data = it87_update_device(dev);
  482. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[nr]));
  483. }
  484. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  485. const char *buf, size_t count)
  486. {
  487. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  488. int nr = sensor_attr->index;
  489. struct it87_data *data = dev_get_drvdata(dev);
  490. long val;
  491. if (kstrtol(buf, 10, &val) < 0)
  492. return -EINVAL;
  493. mutex_lock(&data->update_lock);
  494. data->temp_high[nr] = TEMP_TO_REG(val);
  495. it87_write_value(data, IT87_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  496. mutex_unlock(&data->update_lock);
  497. return count;
  498. }
  499. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  500. const char *buf, size_t count)
  501. {
  502. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  503. int nr = sensor_attr->index;
  504. struct it87_data *data = dev_get_drvdata(dev);
  505. long val;
  506. if (kstrtol(buf, 10, &val) < 0)
  507. return -EINVAL;
  508. mutex_lock(&data->update_lock);
  509. data->temp_low[nr] = TEMP_TO_REG(val);
  510. it87_write_value(data, IT87_REG_TEMP_LOW(nr), data->temp_low[nr]);
  511. mutex_unlock(&data->update_lock);
  512. return count;
  513. }
  514. #define show_temp_offset(offset) \
  515. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  516. show_temp, NULL, offset - 1); \
  517. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  518. show_temp_max, set_temp_max, offset - 1); \
  519. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  520. show_temp_min, set_temp_min, offset - 1);
  521. show_temp_offset(1);
  522. show_temp_offset(2);
  523. show_temp_offset(3);
  524. static ssize_t show_sensor(struct device *dev, struct device_attribute *attr,
  525. char *buf)
  526. {
  527. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  528. int nr = sensor_attr->index;
  529. struct it87_data *data = it87_update_device(dev);
  530. u8 reg = data->sensor; /* In case value is updated while used */
  531. if (reg & (1 << nr))
  532. return sprintf(buf, "3\n"); /* thermal diode */
  533. if (reg & (8 << nr))
  534. return sprintf(buf, "4\n"); /* thermistor */
  535. return sprintf(buf, "0\n"); /* disabled */
  536. }
  537. static ssize_t set_sensor(struct device *dev, struct device_attribute *attr,
  538. const char *buf, size_t count)
  539. {
  540. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  541. int nr = sensor_attr->index;
  542. struct it87_data *data = dev_get_drvdata(dev);
  543. long val;
  544. u8 reg;
  545. if (kstrtol(buf, 10, &val) < 0)
  546. return -EINVAL;
  547. reg = it87_read_value(data, IT87_REG_TEMP_ENABLE);
  548. reg &= ~(1 << nr);
  549. reg &= ~(8 << nr);
  550. if (val == 2) { /* backwards compatibility */
  551. dev_warn(dev, "Sensor type 2 is deprecated, please use 4 "
  552. "instead\n");
  553. val = 4;
  554. }
  555. /* 3 = thermal diode; 4 = thermistor; 0 = disabled */
  556. if (val == 3)
  557. reg |= 1 << nr;
  558. else if (val == 4)
  559. reg |= 8 << nr;
  560. else if (val != 0)
  561. return -EINVAL;
  562. mutex_lock(&data->update_lock);
  563. data->sensor = reg;
  564. it87_write_value(data, IT87_REG_TEMP_ENABLE, data->sensor);
  565. data->valid = 0; /* Force cache refresh */
  566. mutex_unlock(&data->update_lock);
  567. return count;
  568. }
  569. #define show_sensor_offset(offset) \
  570. static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO | S_IWUSR, \
  571. show_sensor, set_sensor, offset - 1);
  572. show_sensor_offset(1);
  573. show_sensor_offset(2);
  574. show_sensor_offset(3);
  575. /* 3 Fans */
  576. static int pwm_mode(const struct it87_data *data, int nr)
  577. {
  578. int ctrl = data->fan_main_ctrl & (1 << nr);
  579. if (ctrl == 0) /* Full speed */
  580. return 0;
  581. if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
  582. return 2;
  583. else /* Manual mode */
  584. return 1;
  585. }
  586. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  587. char *buf)
  588. {
  589. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  590. int nr = sensor_attr->index;
  591. struct it87_data *data = it87_update_device(dev);
  592. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  593. DIV_FROM_REG(data->fan_div[nr])));
  594. }
  595. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  596. char *buf)
  597. {
  598. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  599. int nr = sensor_attr->index;
  600. struct it87_data *data = it87_update_device(dev);
  601. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  602. DIV_FROM_REG(data->fan_div[nr])));
  603. }
  604. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  605. char *buf)
  606. {
  607. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  608. int nr = sensor_attr->index;
  609. struct it87_data *data = it87_update_device(dev);
  610. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  611. }
  612. static ssize_t show_pwm_enable(struct device *dev,
  613. struct device_attribute *attr, char *buf)
  614. {
  615. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  616. int nr = sensor_attr->index;
  617. struct it87_data *data = it87_update_device(dev);
  618. return sprintf(buf, "%d\n", pwm_mode(data, nr));
  619. }
  620. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  621. char *buf)
  622. {
  623. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  624. int nr = sensor_attr->index;
  625. struct it87_data *data = it87_update_device(dev);
  626. return sprintf(buf, "%d\n",
  627. pwm_from_reg(data, data->pwm_duty[nr]));
  628. }
  629. static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
  630. char *buf)
  631. {
  632. struct it87_data *data = it87_update_device(dev);
  633. int index = (data->fan_ctl >> 4) & 0x07;
  634. return sprintf(buf, "%u\n", pwm_freq[index]);
  635. }
  636. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  637. const char *buf, size_t count)
  638. {
  639. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  640. int nr = sensor_attr->index;
  641. struct it87_data *data = dev_get_drvdata(dev);
  642. long val;
  643. u8 reg;
  644. if (kstrtol(buf, 10, &val) < 0)
  645. return -EINVAL;
  646. mutex_lock(&data->update_lock);
  647. reg = it87_read_value(data, IT87_REG_FAN_DIV);
  648. switch (nr) {
  649. case 0:
  650. data->fan_div[nr] = reg & 0x07;
  651. break;
  652. case 1:
  653. data->fan_div[nr] = (reg >> 3) & 0x07;
  654. break;
  655. case 2:
  656. data->fan_div[nr] = (reg & 0x40) ? 3 : 1;
  657. break;
  658. }
  659. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  660. it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
  661. mutex_unlock(&data->update_lock);
  662. return count;
  663. }
  664. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  665. const char *buf, size_t count)
  666. {
  667. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  668. int nr = sensor_attr->index;
  669. struct it87_data *data = dev_get_drvdata(dev);
  670. unsigned long val;
  671. int min;
  672. u8 old;
  673. if (kstrtoul(buf, 10, &val) < 0)
  674. return -EINVAL;
  675. mutex_lock(&data->update_lock);
  676. old = it87_read_value(data, IT87_REG_FAN_DIV);
  677. /* Save fan min limit */
  678. min = FAN_FROM_REG(data->fan_min[nr], DIV_FROM_REG(data->fan_div[nr]));
  679. switch (nr) {
  680. case 0:
  681. case 1:
  682. data->fan_div[nr] = DIV_TO_REG(val);
  683. break;
  684. case 2:
  685. if (val < 8)
  686. data->fan_div[nr] = 1;
  687. else
  688. data->fan_div[nr] = 3;
  689. }
  690. val = old & 0x80;
  691. val |= (data->fan_div[0] & 0x07);
  692. val |= (data->fan_div[1] & 0x07) << 3;
  693. if (data->fan_div[2] == 3)
  694. val |= 0x1 << 6;
  695. it87_write_value(data, IT87_REG_FAN_DIV, val);
  696. /* Restore fan min limit */
  697. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  698. it87_write_value(data, IT87_REG_FAN_MIN[nr], data->fan_min[nr]);
  699. mutex_unlock(&data->update_lock);
  700. return count;
  701. }
  702. /* Returns 0 if OK, -EINVAL otherwise */
  703. static int check_trip_points(struct device *dev, int nr)
  704. {
  705. const struct it87_data *data = dev_get_drvdata(dev);
  706. int i, err = 0;
  707. if (has_old_autopwm(data)) {
  708. for (i = 0; i < 3; i++) {
  709. if (data->auto_temp[nr][i] > data->auto_temp[nr][i + 1])
  710. err = -EINVAL;
  711. }
  712. for (i = 0; i < 2; i++) {
  713. if (data->auto_pwm[nr][i] > data->auto_pwm[nr][i + 1])
  714. err = -EINVAL;
  715. }
  716. }
  717. if (err) {
  718. dev_err(dev, "Inconsistent trip points, not switching to "
  719. "automatic mode\n");
  720. dev_err(dev, "Adjust the trip points and try again\n");
  721. }
  722. return err;
  723. }
  724. static ssize_t set_pwm_enable(struct device *dev,
  725. struct device_attribute *attr, const char *buf, size_t count)
  726. {
  727. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  728. int nr = sensor_attr->index;
  729. struct it87_data *data = dev_get_drvdata(dev);
  730. long val;
  731. if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 2)
  732. return -EINVAL;
  733. /* Check trip points before switching to automatic mode */
  734. if (val == 2) {
  735. if (check_trip_points(dev, nr) < 0)
  736. return -EINVAL;
  737. }
  738. mutex_lock(&data->update_lock);
  739. if (val == 0) {
  740. int tmp;
  741. /* make sure the fan is on when in on/off mode */
  742. tmp = it87_read_value(data, IT87_REG_FAN_CTL);
  743. it87_write_value(data, IT87_REG_FAN_CTL, tmp | (1 << nr));
  744. /* set on/off mode */
  745. data->fan_main_ctrl &= ~(1 << nr);
  746. it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
  747. data->fan_main_ctrl);
  748. } else {
  749. if (val == 1) /* Manual mode */
  750. data->pwm_ctrl[nr] = has_newer_autopwm(data) ?
  751. data->pwm_temp_map[nr] :
  752. data->pwm_duty[nr];
  753. else /* Automatic mode */
  754. data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
  755. it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
  756. /* set SmartGuardian mode */
  757. data->fan_main_ctrl |= (1 << nr);
  758. it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
  759. data->fan_main_ctrl);
  760. }
  761. mutex_unlock(&data->update_lock);
  762. return count;
  763. }
  764. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  765. const char *buf, size_t count)
  766. {
  767. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  768. int nr = sensor_attr->index;
  769. struct it87_data *data = dev_get_drvdata(dev);
  770. long val;
  771. if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
  772. return -EINVAL;
  773. mutex_lock(&data->update_lock);
  774. if (has_newer_autopwm(data)) {
  775. /*
  776. * If we are in automatic mode, the PWM duty cycle register
  777. * is read-only so we can't write the value.
  778. */
  779. if (data->pwm_ctrl[nr] & 0x80) {
  780. mutex_unlock(&data->update_lock);
  781. return -EBUSY;
  782. }
  783. data->pwm_duty[nr] = pwm_to_reg(data, val);
  784. it87_write_value(data, IT87_REG_PWM_DUTY(nr),
  785. data->pwm_duty[nr]);
  786. } else {
  787. data->pwm_duty[nr] = pwm_to_reg(data, val);
  788. /*
  789. * If we are in manual mode, write the duty cycle immediately;
  790. * otherwise, just store it for later use.
  791. */
  792. if (!(data->pwm_ctrl[nr] & 0x80)) {
  793. data->pwm_ctrl[nr] = data->pwm_duty[nr];
  794. it87_write_value(data, IT87_REG_PWM(nr),
  795. data->pwm_ctrl[nr]);
  796. }
  797. }
  798. mutex_unlock(&data->update_lock);
  799. return count;
  800. }
  801. static ssize_t set_pwm_freq(struct device *dev,
  802. struct device_attribute *attr, const char *buf, size_t count)
  803. {
  804. struct it87_data *data = dev_get_drvdata(dev);
  805. unsigned long val;
  806. int i;
  807. if (kstrtoul(buf, 10, &val) < 0)
  808. return -EINVAL;
  809. /* Search for the nearest available frequency */
  810. for (i = 0; i < 7; i++) {
  811. if (val > (pwm_freq[i] + pwm_freq[i+1]) / 2)
  812. break;
  813. }
  814. mutex_lock(&data->update_lock);
  815. data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL) & 0x8f;
  816. data->fan_ctl |= i << 4;
  817. it87_write_value(data, IT87_REG_FAN_CTL, data->fan_ctl);
  818. mutex_unlock(&data->update_lock);
  819. return count;
  820. }
  821. static ssize_t show_pwm_temp_map(struct device *dev,
  822. struct device_attribute *attr, char *buf)
  823. {
  824. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  825. int nr = sensor_attr->index;
  826. struct it87_data *data = it87_update_device(dev);
  827. int map;
  828. if (data->pwm_temp_map[nr] < 3)
  829. map = 1 << data->pwm_temp_map[nr];
  830. else
  831. map = 0; /* Should never happen */
  832. return sprintf(buf, "%d\n", map);
  833. }
  834. static ssize_t set_pwm_temp_map(struct device *dev,
  835. struct device_attribute *attr, const char *buf, size_t count)
  836. {
  837. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  838. int nr = sensor_attr->index;
  839. struct it87_data *data = dev_get_drvdata(dev);
  840. long val;
  841. u8 reg;
  842. /*
  843. * This check can go away if we ever support automatic fan speed
  844. * control on newer chips.
  845. */
  846. if (!has_old_autopwm(data)) {
  847. dev_notice(dev, "Mapping change disabled for safety reasons\n");
  848. return -EINVAL;
  849. }
  850. if (kstrtol(buf, 10, &val) < 0)
  851. return -EINVAL;
  852. switch (val) {
  853. case (1 << 0):
  854. reg = 0x00;
  855. break;
  856. case (1 << 1):
  857. reg = 0x01;
  858. break;
  859. case (1 << 2):
  860. reg = 0x02;
  861. break;
  862. default:
  863. return -EINVAL;
  864. }
  865. mutex_lock(&data->update_lock);
  866. data->pwm_temp_map[nr] = reg;
  867. /*
  868. * If we are in automatic mode, write the temp mapping immediately;
  869. * otherwise, just store it for later use.
  870. */
  871. if (data->pwm_ctrl[nr] & 0x80) {
  872. data->pwm_ctrl[nr] = 0x80 | data->pwm_temp_map[nr];
  873. it87_write_value(data, IT87_REG_PWM(nr), data->pwm_ctrl[nr]);
  874. }
  875. mutex_unlock(&data->update_lock);
  876. return count;
  877. }
  878. static ssize_t show_auto_pwm(struct device *dev,
  879. struct device_attribute *attr, char *buf)
  880. {
  881. struct it87_data *data = it87_update_device(dev);
  882. struct sensor_device_attribute_2 *sensor_attr =
  883. to_sensor_dev_attr_2(attr);
  884. int nr = sensor_attr->nr;
  885. int point = sensor_attr->index;
  886. return sprintf(buf, "%d\n",
  887. pwm_from_reg(data, data->auto_pwm[nr][point]));
  888. }
  889. static ssize_t set_auto_pwm(struct device *dev,
  890. struct device_attribute *attr, const char *buf, size_t count)
  891. {
  892. struct it87_data *data = dev_get_drvdata(dev);
  893. struct sensor_device_attribute_2 *sensor_attr =
  894. to_sensor_dev_attr_2(attr);
  895. int nr = sensor_attr->nr;
  896. int point = sensor_attr->index;
  897. long val;
  898. if (kstrtol(buf, 10, &val) < 0 || val < 0 || val > 255)
  899. return -EINVAL;
  900. mutex_lock(&data->update_lock);
  901. data->auto_pwm[nr][point] = pwm_to_reg(data, val);
  902. it87_write_value(data, IT87_REG_AUTO_PWM(nr, point),
  903. data->auto_pwm[nr][point]);
  904. mutex_unlock(&data->update_lock);
  905. return count;
  906. }
  907. static ssize_t show_auto_temp(struct device *dev,
  908. struct device_attribute *attr, char *buf)
  909. {
  910. struct it87_data *data = it87_update_device(dev);
  911. struct sensor_device_attribute_2 *sensor_attr =
  912. to_sensor_dev_attr_2(attr);
  913. int nr = sensor_attr->nr;
  914. int point = sensor_attr->index;
  915. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->auto_temp[nr][point]));
  916. }
  917. static ssize_t set_auto_temp(struct device *dev,
  918. struct device_attribute *attr, const char *buf, size_t count)
  919. {
  920. struct it87_data *data = dev_get_drvdata(dev);
  921. struct sensor_device_attribute_2 *sensor_attr =
  922. to_sensor_dev_attr_2(attr);
  923. int nr = sensor_attr->nr;
  924. int point = sensor_attr->index;
  925. long val;
  926. if (kstrtol(buf, 10, &val) < 0 || val < -128000 || val > 127000)
  927. return -EINVAL;
  928. mutex_lock(&data->update_lock);
  929. data->auto_temp[nr][point] = TEMP_TO_REG(val);
  930. it87_write_value(data, IT87_REG_AUTO_TEMP(nr, point),
  931. data->auto_temp[nr][point]);
  932. mutex_unlock(&data->update_lock);
  933. return count;
  934. }
  935. #define show_fan_offset(offset) \
  936. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  937. show_fan, NULL, offset - 1); \
  938. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  939. show_fan_min, set_fan_min, offset - 1); \
  940. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  941. show_fan_div, set_fan_div, offset - 1);
  942. show_fan_offset(1);
  943. show_fan_offset(2);
  944. show_fan_offset(3);
  945. #define show_pwm_offset(offset) \
  946. static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
  947. show_pwm_enable, set_pwm_enable, offset - 1); \
  948. static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  949. show_pwm, set_pwm, offset - 1); \
  950. static DEVICE_ATTR(pwm##offset##_freq, \
  951. (offset == 1 ? S_IRUGO | S_IWUSR : S_IRUGO), \
  952. show_pwm_freq, (offset == 1 ? set_pwm_freq : NULL)); \
  953. static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels_temp, \
  954. S_IRUGO | S_IWUSR, show_pwm_temp_map, set_pwm_temp_map, \
  955. offset - 1); \
  956. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_pwm, \
  957. S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
  958. offset - 1, 0); \
  959. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_pwm, \
  960. S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
  961. offset - 1, 1); \
  962. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_pwm, \
  963. S_IRUGO | S_IWUSR, show_auto_pwm, set_auto_pwm, \
  964. offset - 1, 2); \
  965. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_pwm, \
  966. S_IRUGO, show_auto_pwm, NULL, offset - 1, 3); \
  967. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp, \
  968. S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
  969. offset - 1, 1); \
  970. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point1_temp_hyst, \
  971. S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
  972. offset - 1, 0); \
  973. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point2_temp, \
  974. S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
  975. offset - 1, 2); \
  976. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point3_temp, \
  977. S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
  978. offset - 1, 3); \
  979. static SENSOR_DEVICE_ATTR_2(pwm##offset##_auto_point4_temp, \
  980. S_IRUGO | S_IWUSR, show_auto_temp, set_auto_temp, \
  981. offset - 1, 4);
  982. show_pwm_offset(1);
  983. show_pwm_offset(2);
  984. show_pwm_offset(3);
  985. /* A different set of callbacks for 16-bit fans */
  986. static ssize_t show_fan16(struct device *dev, struct device_attribute *attr,
  987. char *buf)
  988. {
  989. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  990. int nr = sensor_attr->index;
  991. struct it87_data *data = it87_update_device(dev);
  992. return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan[nr]));
  993. }
  994. static ssize_t show_fan16_min(struct device *dev, struct device_attribute *attr,
  995. char *buf)
  996. {
  997. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  998. int nr = sensor_attr->index;
  999. struct it87_data *data = it87_update_device(dev);
  1000. return sprintf(buf, "%d\n", FAN16_FROM_REG(data->fan_min[nr]));
  1001. }
  1002. static ssize_t set_fan16_min(struct device *dev, struct device_attribute *attr,
  1003. const char *buf, size_t count)
  1004. {
  1005. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  1006. int nr = sensor_attr->index;
  1007. struct it87_data *data = dev_get_drvdata(dev);
  1008. long val;
  1009. if (kstrtol(buf, 10, &val) < 0)
  1010. return -EINVAL;
  1011. mutex_lock(&data->update_lock);
  1012. data->fan_min[nr] = FAN16_TO_REG(val);
  1013. it87_write_value(data, IT87_REG_FAN_MIN[nr],
  1014. data->fan_min[nr] & 0xff);
  1015. it87_write_value(data, IT87_REG_FANX_MIN[nr],
  1016. data->fan_min[nr] >> 8);
  1017. mutex_unlock(&data->update_lock);
  1018. return count;
  1019. }
  1020. /*
  1021. * We want to use the same sysfs file names as 8-bit fans, but we need
  1022. * different variable names, so we have to use SENSOR_ATTR instead of
  1023. * SENSOR_DEVICE_ATTR.
  1024. */
  1025. #define show_fan16_offset(offset) \
  1026. static struct sensor_device_attribute sensor_dev_attr_fan##offset##_input16 \
  1027. = SENSOR_ATTR(fan##offset##_input, S_IRUGO, \
  1028. show_fan16, NULL, offset - 1); \
  1029. static struct sensor_device_attribute sensor_dev_attr_fan##offset##_min16 \
  1030. = SENSOR_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  1031. show_fan16_min, set_fan16_min, offset - 1)
  1032. show_fan16_offset(1);
  1033. show_fan16_offset(2);
  1034. show_fan16_offset(3);
  1035. show_fan16_offset(4);
  1036. show_fan16_offset(5);
  1037. /* Alarms */
  1038. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  1039. char *buf)
  1040. {
  1041. struct it87_data *data = it87_update_device(dev);
  1042. return sprintf(buf, "%u\n", data->alarms);
  1043. }
  1044. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  1045. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  1046. char *buf)
  1047. {
  1048. int bitnr = to_sensor_dev_attr(attr)->index;
  1049. struct it87_data *data = it87_update_device(dev);
  1050. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  1051. }
  1052. static ssize_t clear_intrusion(struct device *dev, struct device_attribute
  1053. *attr, const char *buf, size_t count)
  1054. {
  1055. struct it87_data *data = dev_get_drvdata(dev);
  1056. long val;
  1057. int config;
  1058. if (kstrtol(buf, 10, &val) < 0 || val != 0)
  1059. return -EINVAL;
  1060. mutex_lock(&data->update_lock);
  1061. config = it87_read_value(data, IT87_REG_CONFIG);
  1062. if (config < 0) {
  1063. count = config;
  1064. } else {
  1065. config |= 1 << 5;
  1066. it87_write_value(data, IT87_REG_CONFIG, config);
  1067. /* Invalidate cache to force re-read */
  1068. data->valid = 0;
  1069. }
  1070. mutex_unlock(&data->update_lock);
  1071. return count;
  1072. }
  1073. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
  1074. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
  1075. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
  1076. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
  1077. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
  1078. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
  1079. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
  1080. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
  1081. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 0);
  1082. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 1);
  1083. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 2);
  1084. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 3);
  1085. static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 6);
  1086. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 16);
  1087. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 17);
  1088. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 18);
  1089. static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR,
  1090. show_alarm, clear_intrusion, 4);
  1091. static ssize_t show_beep(struct device *dev, struct device_attribute *attr,
  1092. char *buf)
  1093. {
  1094. int bitnr = to_sensor_dev_attr(attr)->index;
  1095. struct it87_data *data = it87_update_device(dev);
  1096. return sprintf(buf, "%u\n", (data->beeps >> bitnr) & 1);
  1097. }
  1098. static ssize_t set_beep(struct device *dev, struct device_attribute *attr,
  1099. const char *buf, size_t count)
  1100. {
  1101. int bitnr = to_sensor_dev_attr(attr)->index;
  1102. struct it87_data *data = dev_get_drvdata(dev);
  1103. long val;
  1104. if (kstrtol(buf, 10, &val) < 0
  1105. || (val != 0 && val != 1))
  1106. return -EINVAL;
  1107. mutex_lock(&data->update_lock);
  1108. data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
  1109. if (val)
  1110. data->beeps |= (1 << bitnr);
  1111. else
  1112. data->beeps &= ~(1 << bitnr);
  1113. it87_write_value(data, IT87_REG_BEEP_ENABLE, data->beeps);
  1114. mutex_unlock(&data->update_lock);
  1115. return count;
  1116. }
  1117. static SENSOR_DEVICE_ATTR(in0_beep, S_IRUGO | S_IWUSR,
  1118. show_beep, set_beep, 1);
  1119. static SENSOR_DEVICE_ATTR(in1_beep, S_IRUGO, show_beep, NULL, 1);
  1120. static SENSOR_DEVICE_ATTR(in2_beep, S_IRUGO, show_beep, NULL, 1);
  1121. static SENSOR_DEVICE_ATTR(in3_beep, S_IRUGO, show_beep, NULL, 1);
  1122. static SENSOR_DEVICE_ATTR(in4_beep, S_IRUGO, show_beep, NULL, 1);
  1123. static SENSOR_DEVICE_ATTR(in5_beep, S_IRUGO, show_beep, NULL, 1);
  1124. static SENSOR_DEVICE_ATTR(in6_beep, S_IRUGO, show_beep, NULL, 1);
  1125. static SENSOR_DEVICE_ATTR(in7_beep, S_IRUGO, show_beep, NULL, 1);
  1126. /* fanX_beep writability is set later */
  1127. static SENSOR_DEVICE_ATTR(fan1_beep, S_IRUGO, show_beep, set_beep, 0);
  1128. static SENSOR_DEVICE_ATTR(fan2_beep, S_IRUGO, show_beep, set_beep, 0);
  1129. static SENSOR_DEVICE_ATTR(fan3_beep, S_IRUGO, show_beep, set_beep, 0);
  1130. static SENSOR_DEVICE_ATTR(fan4_beep, S_IRUGO, show_beep, set_beep, 0);
  1131. static SENSOR_DEVICE_ATTR(fan5_beep, S_IRUGO, show_beep, set_beep, 0);
  1132. static SENSOR_DEVICE_ATTR(temp1_beep, S_IRUGO | S_IWUSR,
  1133. show_beep, set_beep, 2);
  1134. static SENSOR_DEVICE_ATTR(temp2_beep, S_IRUGO, show_beep, NULL, 2);
  1135. static SENSOR_DEVICE_ATTR(temp3_beep, S_IRUGO, show_beep, NULL, 2);
  1136. static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
  1137. char *buf)
  1138. {
  1139. struct it87_data *data = dev_get_drvdata(dev);
  1140. return sprintf(buf, "%u\n", data->vrm);
  1141. }
  1142. static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
  1143. const char *buf, size_t count)
  1144. {
  1145. struct it87_data *data = dev_get_drvdata(dev);
  1146. unsigned long val;
  1147. if (kstrtoul(buf, 10, &val) < 0)
  1148. return -EINVAL;
  1149. data->vrm = val;
  1150. return count;
  1151. }
  1152. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  1153. static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
  1154. char *buf)
  1155. {
  1156. struct it87_data *data = it87_update_device(dev);
  1157. return sprintf(buf, "%ld\n", (long) vid_from_reg(data->vid, data->vrm));
  1158. }
  1159. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  1160. static ssize_t show_label(struct device *dev, struct device_attribute *attr,
  1161. char *buf)
  1162. {
  1163. static const char * const labels[] = {
  1164. "+5V",
  1165. "5VSB",
  1166. "Vbat",
  1167. };
  1168. static const char * const labels_it8721[] = {
  1169. "+3.3V",
  1170. "3VSB",
  1171. "Vbat",
  1172. };
  1173. struct it87_data *data = dev_get_drvdata(dev);
  1174. int nr = to_sensor_dev_attr(attr)->index;
  1175. return sprintf(buf, "%s\n", has_12mv_adc(data) ? labels_it8721[nr]
  1176. : labels[nr]);
  1177. }
  1178. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL, 0);
  1179. static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_label, NULL, 1);
  1180. static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_label, NULL, 2);
  1181. static ssize_t show_name(struct device *dev, struct device_attribute
  1182. *devattr, char *buf)
  1183. {
  1184. struct it87_data *data = dev_get_drvdata(dev);
  1185. return sprintf(buf, "%s\n", data->name);
  1186. }
  1187. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  1188. static struct attribute *it87_attributes[] = {
  1189. &sensor_dev_attr_in0_input.dev_attr.attr,
  1190. &sensor_dev_attr_in1_input.dev_attr.attr,
  1191. &sensor_dev_attr_in2_input.dev_attr.attr,
  1192. &sensor_dev_attr_in3_input.dev_attr.attr,
  1193. &sensor_dev_attr_in4_input.dev_attr.attr,
  1194. &sensor_dev_attr_in5_input.dev_attr.attr,
  1195. &sensor_dev_attr_in6_input.dev_attr.attr,
  1196. &sensor_dev_attr_in7_input.dev_attr.attr,
  1197. &sensor_dev_attr_in8_input.dev_attr.attr,
  1198. &sensor_dev_attr_in0_min.dev_attr.attr,
  1199. &sensor_dev_attr_in1_min.dev_attr.attr,
  1200. &sensor_dev_attr_in2_min.dev_attr.attr,
  1201. &sensor_dev_attr_in3_min.dev_attr.attr,
  1202. &sensor_dev_attr_in4_min.dev_attr.attr,
  1203. &sensor_dev_attr_in5_min.dev_attr.attr,
  1204. &sensor_dev_attr_in6_min.dev_attr.attr,
  1205. &sensor_dev_attr_in7_min.dev_attr.attr,
  1206. &sensor_dev_attr_in0_max.dev_attr.attr,
  1207. &sensor_dev_attr_in1_max.dev_attr.attr,
  1208. &sensor_dev_attr_in2_max.dev_attr.attr,
  1209. &sensor_dev_attr_in3_max.dev_attr.attr,
  1210. &sensor_dev_attr_in4_max.dev_attr.attr,
  1211. &sensor_dev_attr_in5_max.dev_attr.attr,
  1212. &sensor_dev_attr_in6_max.dev_attr.attr,
  1213. &sensor_dev_attr_in7_max.dev_attr.attr,
  1214. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  1215. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  1216. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  1217. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  1218. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  1219. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  1220. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  1221. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  1222. &sensor_dev_attr_temp1_input.dev_attr.attr,
  1223. &sensor_dev_attr_temp2_input.dev_attr.attr,
  1224. &sensor_dev_attr_temp3_input.dev_attr.attr,
  1225. &sensor_dev_attr_temp1_max.dev_attr.attr,
  1226. &sensor_dev_attr_temp2_max.dev_attr.attr,
  1227. &sensor_dev_attr_temp3_max.dev_attr.attr,
  1228. &sensor_dev_attr_temp1_min.dev_attr.attr,
  1229. &sensor_dev_attr_temp2_min.dev_attr.attr,
  1230. &sensor_dev_attr_temp3_min.dev_attr.attr,
  1231. &sensor_dev_attr_temp1_type.dev_attr.attr,
  1232. &sensor_dev_attr_temp2_type.dev_attr.attr,
  1233. &sensor_dev_attr_temp3_type.dev_attr.attr,
  1234. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  1235. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  1236. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  1237. &dev_attr_alarms.attr,
  1238. &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
  1239. &dev_attr_name.attr,
  1240. NULL
  1241. };
  1242. static const struct attribute_group it87_group = {
  1243. .attrs = it87_attributes,
  1244. };
  1245. static struct attribute *it87_attributes_beep[] = {
  1246. &sensor_dev_attr_in0_beep.dev_attr.attr,
  1247. &sensor_dev_attr_in1_beep.dev_attr.attr,
  1248. &sensor_dev_attr_in2_beep.dev_attr.attr,
  1249. &sensor_dev_attr_in3_beep.dev_attr.attr,
  1250. &sensor_dev_attr_in4_beep.dev_attr.attr,
  1251. &sensor_dev_attr_in5_beep.dev_attr.attr,
  1252. &sensor_dev_attr_in6_beep.dev_attr.attr,
  1253. &sensor_dev_attr_in7_beep.dev_attr.attr,
  1254. &sensor_dev_attr_temp1_beep.dev_attr.attr,
  1255. &sensor_dev_attr_temp2_beep.dev_attr.attr,
  1256. &sensor_dev_attr_temp3_beep.dev_attr.attr,
  1257. NULL
  1258. };
  1259. static const struct attribute_group it87_group_beep = {
  1260. .attrs = it87_attributes_beep,
  1261. };
  1262. static struct attribute *it87_attributes_fan16[5][3+1] = { {
  1263. &sensor_dev_attr_fan1_input16.dev_attr.attr,
  1264. &sensor_dev_attr_fan1_min16.dev_attr.attr,
  1265. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1266. NULL
  1267. }, {
  1268. &sensor_dev_attr_fan2_input16.dev_attr.attr,
  1269. &sensor_dev_attr_fan2_min16.dev_attr.attr,
  1270. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1271. NULL
  1272. }, {
  1273. &sensor_dev_attr_fan3_input16.dev_attr.attr,
  1274. &sensor_dev_attr_fan3_min16.dev_attr.attr,
  1275. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1276. NULL
  1277. }, {
  1278. &sensor_dev_attr_fan4_input16.dev_attr.attr,
  1279. &sensor_dev_attr_fan4_min16.dev_attr.attr,
  1280. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  1281. NULL
  1282. }, {
  1283. &sensor_dev_attr_fan5_input16.dev_attr.attr,
  1284. &sensor_dev_attr_fan5_min16.dev_attr.attr,
  1285. &sensor_dev_attr_fan5_alarm.dev_attr.attr,
  1286. NULL
  1287. } };
  1288. static const struct attribute_group it87_group_fan16[5] = {
  1289. { .attrs = it87_attributes_fan16[0] },
  1290. { .attrs = it87_attributes_fan16[1] },
  1291. { .attrs = it87_attributes_fan16[2] },
  1292. { .attrs = it87_attributes_fan16[3] },
  1293. { .attrs = it87_attributes_fan16[4] },
  1294. };
  1295. static struct attribute *it87_attributes_fan[3][4+1] = { {
  1296. &sensor_dev_attr_fan1_input.dev_attr.attr,
  1297. &sensor_dev_attr_fan1_min.dev_attr.attr,
  1298. &sensor_dev_attr_fan1_div.dev_attr.attr,
  1299. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  1300. NULL
  1301. }, {
  1302. &sensor_dev_attr_fan2_input.dev_attr.attr,
  1303. &sensor_dev_attr_fan2_min.dev_attr.attr,
  1304. &sensor_dev_attr_fan2_div.dev_attr.attr,
  1305. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  1306. NULL
  1307. }, {
  1308. &sensor_dev_attr_fan3_input.dev_attr.attr,
  1309. &sensor_dev_attr_fan3_min.dev_attr.attr,
  1310. &sensor_dev_attr_fan3_div.dev_attr.attr,
  1311. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  1312. NULL
  1313. } };
  1314. static const struct attribute_group it87_group_fan[3] = {
  1315. { .attrs = it87_attributes_fan[0] },
  1316. { .attrs = it87_attributes_fan[1] },
  1317. { .attrs = it87_attributes_fan[2] },
  1318. };
  1319. static const struct attribute_group *
  1320. it87_get_fan_group(const struct it87_data *data)
  1321. {
  1322. return has_16bit_fans(data) ? it87_group_fan16 : it87_group_fan;
  1323. }
  1324. static struct attribute *it87_attributes_pwm[3][4+1] = { {
  1325. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  1326. &sensor_dev_attr_pwm1.dev_attr.attr,
  1327. &dev_attr_pwm1_freq.attr,
  1328. &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
  1329. NULL
  1330. }, {
  1331. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  1332. &sensor_dev_attr_pwm2.dev_attr.attr,
  1333. &dev_attr_pwm2_freq.attr,
  1334. &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
  1335. NULL
  1336. }, {
  1337. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  1338. &sensor_dev_attr_pwm3.dev_attr.attr,
  1339. &dev_attr_pwm3_freq.attr,
  1340. &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
  1341. NULL
  1342. } };
  1343. static const struct attribute_group it87_group_pwm[3] = {
  1344. { .attrs = it87_attributes_pwm[0] },
  1345. { .attrs = it87_attributes_pwm[1] },
  1346. { .attrs = it87_attributes_pwm[2] },
  1347. };
  1348. static struct attribute *it87_attributes_autopwm[3][9+1] = { {
  1349. &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
  1350. &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
  1351. &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
  1352. &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
  1353. &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
  1354. &sensor_dev_attr_pwm1_auto_point1_temp_hyst.dev_attr.attr,
  1355. &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
  1356. &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
  1357. &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
  1358. NULL
  1359. }, {
  1360. &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
  1361. &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
  1362. &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
  1363. &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
  1364. &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
  1365. &sensor_dev_attr_pwm2_auto_point1_temp_hyst.dev_attr.attr,
  1366. &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
  1367. &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
  1368. &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
  1369. NULL
  1370. }, {
  1371. &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
  1372. &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
  1373. &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
  1374. &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
  1375. &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
  1376. &sensor_dev_attr_pwm3_auto_point1_temp_hyst.dev_attr.attr,
  1377. &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
  1378. &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
  1379. &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
  1380. NULL
  1381. } };
  1382. static const struct attribute_group it87_group_autopwm[3] = {
  1383. { .attrs = it87_attributes_autopwm[0] },
  1384. { .attrs = it87_attributes_autopwm[1] },
  1385. { .attrs = it87_attributes_autopwm[2] },
  1386. };
  1387. static struct attribute *it87_attributes_fan_beep[] = {
  1388. &sensor_dev_attr_fan1_beep.dev_attr.attr,
  1389. &sensor_dev_attr_fan2_beep.dev_attr.attr,
  1390. &sensor_dev_attr_fan3_beep.dev_attr.attr,
  1391. &sensor_dev_attr_fan4_beep.dev_attr.attr,
  1392. &sensor_dev_attr_fan5_beep.dev_attr.attr,
  1393. };
  1394. static struct attribute *it87_attributes_vid[] = {
  1395. &dev_attr_vrm.attr,
  1396. &dev_attr_cpu0_vid.attr,
  1397. NULL
  1398. };
  1399. static const struct attribute_group it87_group_vid = {
  1400. .attrs = it87_attributes_vid,
  1401. };
  1402. static struct attribute *it87_attributes_label[] = {
  1403. &sensor_dev_attr_in3_label.dev_attr.attr,
  1404. &sensor_dev_attr_in7_label.dev_attr.attr,
  1405. &sensor_dev_attr_in8_label.dev_attr.attr,
  1406. NULL
  1407. };
  1408. static const struct attribute_group it87_group_label = {
  1409. .attrs = it87_attributes_label,
  1410. };
  1411. /* SuperIO detection - will change isa_address if a chip is found */
  1412. static int __init it87_find(unsigned short *address,
  1413. struct it87_sio_data *sio_data)
  1414. {
  1415. int err;
  1416. u16 chip_type;
  1417. const char *board_vendor, *board_name;
  1418. err = superio_enter();
  1419. if (err)
  1420. return err;
  1421. err = -ENODEV;
  1422. chip_type = force_id ? force_id : superio_inw(DEVID);
  1423. switch (chip_type) {
  1424. case IT8705F_DEVID:
  1425. sio_data->type = it87;
  1426. break;
  1427. case IT8712F_DEVID:
  1428. sio_data->type = it8712;
  1429. break;
  1430. case IT8716F_DEVID:
  1431. case IT8726F_DEVID:
  1432. sio_data->type = it8716;
  1433. break;
  1434. case IT8718F_DEVID:
  1435. sio_data->type = it8718;
  1436. break;
  1437. case IT8720F_DEVID:
  1438. sio_data->type = it8720;
  1439. break;
  1440. case IT8721F_DEVID:
  1441. sio_data->type = it8721;
  1442. break;
  1443. case IT8728F_DEVID:
  1444. sio_data->type = it8728;
  1445. break;
  1446. case 0xffff: /* No device at all */
  1447. goto exit;
  1448. default:
  1449. pr_debug("Unsupported chip (DEVID=0x%x)\n", chip_type);
  1450. goto exit;
  1451. }
  1452. superio_select(PME);
  1453. if (!(superio_inb(IT87_ACT_REG) & 0x01)) {
  1454. pr_info("Device not activated, skipping\n");
  1455. goto exit;
  1456. }
  1457. *address = superio_inw(IT87_BASE_REG) & ~(IT87_EXTENT - 1);
  1458. if (*address == 0) {
  1459. pr_info("Base address not set, skipping\n");
  1460. goto exit;
  1461. }
  1462. err = 0;
  1463. sio_data->revision = superio_inb(DEVREV) & 0x0f;
  1464. pr_info("Found IT%04xF chip at 0x%x, revision %d\n",
  1465. chip_type, *address, sio_data->revision);
  1466. /* in8 (Vbat) is always internal */
  1467. sio_data->internal = (1 << 2);
  1468. /* Read GPIO config and VID value from LDN 7 (GPIO) */
  1469. if (sio_data->type == it87) {
  1470. /* The IT8705F doesn't have VID pins at all */
  1471. sio_data->skip_vid = 1;
  1472. /* The IT8705F has a different LD number for GPIO */
  1473. superio_select(5);
  1474. sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
  1475. } else {
  1476. int reg;
  1477. superio_select(GPIO);
  1478. reg = superio_inb(IT87_SIO_GPIO3_REG);
  1479. if (sio_data->type == it8721 || sio_data->type == it8728) {
  1480. /*
  1481. * The IT8721F/IT8758E doesn't have VID pins at all,
  1482. * not sure about the IT8728F.
  1483. */
  1484. sio_data->skip_vid = 1;
  1485. } else {
  1486. /* We need at least 4 VID pins */
  1487. if (reg & 0x0f) {
  1488. pr_info("VID is disabled (pins used for GPIO)\n");
  1489. sio_data->skip_vid = 1;
  1490. }
  1491. }
  1492. /* Check if fan3 is there or not */
  1493. if (reg & (1 << 6))
  1494. sio_data->skip_pwm |= (1 << 2);
  1495. if (reg & (1 << 7))
  1496. sio_data->skip_fan |= (1 << 2);
  1497. /* Check if fan2 is there or not */
  1498. reg = superio_inb(IT87_SIO_GPIO5_REG);
  1499. if (reg & (1 << 1))
  1500. sio_data->skip_pwm |= (1 << 1);
  1501. if (reg & (1 << 2))
  1502. sio_data->skip_fan |= (1 << 1);
  1503. if ((sio_data->type == it8718 || sio_data->type == it8720)
  1504. && !(sio_data->skip_vid))
  1505. sio_data->vid_value = superio_inb(IT87_SIO_VID_REG);
  1506. reg = superio_inb(IT87_SIO_PINX2_REG);
  1507. /*
  1508. * The IT8720F has no VIN7 pin, so VCCH should always be
  1509. * routed internally to VIN7 with an internal divider.
  1510. * Curiously, there still is a configuration bit to control
  1511. * this, which means it can be set incorrectly. And even
  1512. * more curiously, many boards out there are improperly
  1513. * configured, even though the IT8720F datasheet claims
  1514. * that the internal routing of VCCH to VIN7 is the default
  1515. * setting. So we force the internal routing in this case.
  1516. */
  1517. if (sio_data->type == it8720 && !(reg & (1 << 1))) {
  1518. reg |= (1 << 1);
  1519. superio_outb(IT87_SIO_PINX2_REG, reg);
  1520. pr_notice("Routing internal VCCH to in7\n");
  1521. }
  1522. if (reg & (1 << 0))
  1523. sio_data->internal |= (1 << 0);
  1524. if ((reg & (1 << 1)) || sio_data->type == it8721 ||
  1525. sio_data->type == it8728)
  1526. sio_data->internal |= (1 << 1);
  1527. sio_data->beep_pin = superio_inb(IT87_SIO_BEEP_PIN_REG) & 0x3f;
  1528. }
  1529. if (sio_data->beep_pin)
  1530. pr_info("Beeping is supported\n");
  1531. /* Disable specific features based on DMI strings */
  1532. board_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
  1533. board_name = dmi_get_system_info(DMI_BOARD_NAME);
  1534. if (board_vendor && board_name) {
  1535. if (strcmp(board_vendor, "nVIDIA") == 0
  1536. && strcmp(board_name, "FN68PT") == 0) {
  1537. /*
  1538. * On the Shuttle SN68PT, FAN_CTL2 is apparently not
  1539. * connected to a fan, but to something else. One user
  1540. * has reported instant system power-off when changing
  1541. * the PWM2 duty cycle, so we disable it.
  1542. * I use the board name string as the trigger in case
  1543. * the same board is ever used in other systems.
  1544. */
  1545. pr_info("Disabling pwm2 due to hardware constraints\n");
  1546. sio_data->skip_pwm = (1 << 1);
  1547. }
  1548. }
  1549. exit:
  1550. superio_exit();
  1551. return err;
  1552. }
  1553. static void it87_remove_files(struct device *dev)
  1554. {
  1555. struct it87_data *data = platform_get_drvdata(pdev);
  1556. struct it87_sio_data *sio_data = dev->platform_data;
  1557. const struct attribute_group *fan_group = it87_get_fan_group(data);
  1558. int i;
  1559. sysfs_remove_group(&dev->kobj, &it87_group);
  1560. if (sio_data->beep_pin)
  1561. sysfs_remove_group(&dev->kobj, &it87_group_beep);
  1562. for (i = 0; i < 5; i++) {
  1563. if (!(data->has_fan & (1 << i)))
  1564. continue;
  1565. sysfs_remove_group(&dev->kobj, &fan_group[i]);
  1566. if (sio_data->beep_pin)
  1567. sysfs_remove_file(&dev->kobj,
  1568. it87_attributes_fan_beep[i]);
  1569. }
  1570. for (i = 0; i < 3; i++) {
  1571. if (sio_data->skip_pwm & (1 << 0))
  1572. continue;
  1573. sysfs_remove_group(&dev->kobj, &it87_group_pwm[i]);
  1574. if (has_old_autopwm(data))
  1575. sysfs_remove_group(&dev->kobj,
  1576. &it87_group_autopwm[i]);
  1577. }
  1578. if (!sio_data->skip_vid)
  1579. sysfs_remove_group(&dev->kobj, &it87_group_vid);
  1580. sysfs_remove_group(&dev->kobj, &it87_group_label);
  1581. }
  1582. static int __devinit it87_probe(struct platform_device *pdev)
  1583. {
  1584. struct it87_data *data;
  1585. struct resource *res;
  1586. struct device *dev = &pdev->dev;
  1587. struct it87_sio_data *sio_data = dev->platform_data;
  1588. const struct attribute_group *fan_group;
  1589. int err = 0, i;
  1590. int enable_pwm_interface;
  1591. int fan_beep_need_rw;
  1592. static const char * const names[] = {
  1593. "it87",
  1594. "it8712",
  1595. "it8716",
  1596. "it8718",
  1597. "it8720",
  1598. "it8721",
  1599. "it8728",
  1600. };
  1601. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  1602. if (!request_region(res->start, IT87_EC_EXTENT, DRVNAME)) {
  1603. dev_err(dev, "Failed to request region 0x%lx-0x%lx\n",
  1604. (unsigned long)res->start,
  1605. (unsigned long)(res->start + IT87_EC_EXTENT - 1));
  1606. err = -EBUSY;
  1607. goto ERROR0;
  1608. }
  1609. data = kzalloc(sizeof(struct it87_data), GFP_KERNEL);
  1610. if (!data) {
  1611. err = -ENOMEM;
  1612. goto ERROR1;
  1613. }
  1614. data->addr = res->start;
  1615. data->type = sio_data->type;
  1616. data->revision = sio_data->revision;
  1617. data->name = names[sio_data->type];
  1618. /* Now, we do the remaining detection. */
  1619. if ((it87_read_value(data, IT87_REG_CONFIG) & 0x80)
  1620. || it87_read_value(data, IT87_REG_CHIPID) != 0x90) {
  1621. err = -ENODEV;
  1622. goto ERROR2;
  1623. }
  1624. platform_set_drvdata(pdev, data);
  1625. mutex_init(&data->update_lock);
  1626. /* Check PWM configuration */
  1627. enable_pwm_interface = it87_check_pwm(dev);
  1628. /* Starting with IT8721F, we handle scaling of internal voltages */
  1629. if (has_12mv_adc(data)) {
  1630. if (sio_data->internal & (1 << 0))
  1631. data->in_scaled |= (1 << 3); /* in3 is AVCC */
  1632. if (sio_data->internal & (1 << 1))
  1633. data->in_scaled |= (1 << 7); /* in7 is VSB */
  1634. if (sio_data->internal & (1 << 2))
  1635. data->in_scaled |= (1 << 8); /* in8 is Vbat */
  1636. }
  1637. /* Initialize the IT87 chip */
  1638. it87_init_device(pdev);
  1639. /* Register sysfs hooks */
  1640. err = sysfs_create_group(&dev->kobj, &it87_group);
  1641. if (err)
  1642. goto ERROR2;
  1643. if (sio_data->beep_pin) {
  1644. err = sysfs_create_group(&dev->kobj, &it87_group_beep);
  1645. if (err)
  1646. goto ERROR4;
  1647. }
  1648. /* Do not create fan files for disabled fans */
  1649. fan_group = it87_get_fan_group(data);
  1650. fan_beep_need_rw = 1;
  1651. for (i = 0; i < 5; i++) {
  1652. if (!(data->has_fan & (1 << i)))
  1653. continue;
  1654. err = sysfs_create_group(&dev->kobj, &fan_group[i]);
  1655. if (err)
  1656. goto ERROR4;
  1657. if (sio_data->beep_pin) {
  1658. err = sysfs_create_file(&dev->kobj,
  1659. it87_attributes_fan_beep[i]);
  1660. if (err)
  1661. goto ERROR4;
  1662. if (!fan_beep_need_rw)
  1663. continue;
  1664. /*
  1665. * As we have a single beep enable bit for all fans,
  1666. * only the first enabled fan has a writable attribute
  1667. * for it.
  1668. */
  1669. if (sysfs_chmod_file(&dev->kobj,
  1670. it87_attributes_fan_beep[i],
  1671. S_IRUGO | S_IWUSR))
  1672. dev_dbg(dev, "chmod +w fan%d_beep failed\n",
  1673. i + 1);
  1674. fan_beep_need_rw = 0;
  1675. }
  1676. }
  1677. if (enable_pwm_interface) {
  1678. for (i = 0; i < 3; i++) {
  1679. if (sio_data->skip_pwm & (1 << i))
  1680. continue;
  1681. err = sysfs_create_group(&dev->kobj,
  1682. &it87_group_pwm[i]);
  1683. if (err)
  1684. goto ERROR4;
  1685. if (!has_old_autopwm(data))
  1686. continue;
  1687. err = sysfs_create_group(&dev->kobj,
  1688. &it87_group_autopwm[i]);
  1689. if (err)
  1690. goto ERROR4;
  1691. }
  1692. }
  1693. if (!sio_data->skip_vid) {
  1694. data->vrm = vid_which_vrm();
  1695. /* VID reading from Super-I/O config space if available */
  1696. data->vid = sio_data->vid_value;
  1697. err = sysfs_create_group(&dev->kobj, &it87_group_vid);
  1698. if (err)
  1699. goto ERROR4;
  1700. }
  1701. /* Export labels for internal sensors */
  1702. for (i = 0; i < 3; i++) {
  1703. if (!(sio_data->internal & (1 << i)))
  1704. continue;
  1705. err = sysfs_create_file(&dev->kobj,
  1706. it87_attributes_label[i]);
  1707. if (err)
  1708. goto ERROR4;
  1709. }
  1710. data->hwmon_dev = hwmon_device_register(dev);
  1711. if (IS_ERR(data->hwmon_dev)) {
  1712. err = PTR_ERR(data->hwmon_dev);
  1713. goto ERROR4;
  1714. }
  1715. return 0;
  1716. ERROR4:
  1717. it87_remove_files(dev);
  1718. ERROR2:
  1719. platform_set_drvdata(pdev, NULL);
  1720. kfree(data);
  1721. ERROR1:
  1722. release_region(res->start, IT87_EC_EXTENT);
  1723. ERROR0:
  1724. return err;
  1725. }
  1726. static int __devexit it87_remove(struct platform_device *pdev)
  1727. {
  1728. struct it87_data *data = platform_get_drvdata(pdev);
  1729. hwmon_device_unregister(data->hwmon_dev);
  1730. it87_remove_files(&pdev->dev);
  1731. release_region(data->addr, IT87_EC_EXTENT);
  1732. platform_set_drvdata(pdev, NULL);
  1733. kfree(data);
  1734. return 0;
  1735. }
  1736. /*
  1737. * Must be called with data->update_lock held, except during initialization.
  1738. * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  1739. * would slow down the IT87 access and should not be necessary.
  1740. */
  1741. static int it87_read_value(struct it87_data *data, u8 reg)
  1742. {
  1743. outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
  1744. return inb_p(data->addr + IT87_DATA_REG_OFFSET);
  1745. }
  1746. /*
  1747. * Must be called with data->update_lock held, except during initialization.
  1748. * We ignore the IT87 BUSY flag at this moment - it could lead to deadlocks,
  1749. * would slow down the IT87 access and should not be necessary.
  1750. */
  1751. static void it87_write_value(struct it87_data *data, u8 reg, u8 value)
  1752. {
  1753. outb_p(reg, data->addr + IT87_ADDR_REG_OFFSET);
  1754. outb_p(value, data->addr + IT87_DATA_REG_OFFSET);
  1755. }
  1756. /* Return 1 if and only if the PWM interface is safe to use */
  1757. static int __devinit it87_check_pwm(struct device *dev)
  1758. {
  1759. struct it87_data *data = dev_get_drvdata(dev);
  1760. /*
  1761. * Some BIOSes fail to correctly configure the IT87 fans. All fans off
  1762. * and polarity set to active low is sign that this is the case so we
  1763. * disable pwm control to protect the user.
  1764. */
  1765. int tmp = it87_read_value(data, IT87_REG_FAN_CTL);
  1766. if ((tmp & 0x87) == 0) {
  1767. if (fix_pwm_polarity) {
  1768. /*
  1769. * The user asks us to attempt a chip reconfiguration.
  1770. * This means switching to active high polarity and
  1771. * inverting all fan speed values.
  1772. */
  1773. int i;
  1774. u8 pwm[3];
  1775. for (i = 0; i < 3; i++)
  1776. pwm[i] = it87_read_value(data,
  1777. IT87_REG_PWM(i));
  1778. /*
  1779. * If any fan is in automatic pwm mode, the polarity
  1780. * might be correct, as suspicious as it seems, so we
  1781. * better don't change anything (but still disable the
  1782. * PWM interface).
  1783. */
  1784. if (!((pwm[0] | pwm[1] | pwm[2]) & 0x80)) {
  1785. dev_info(dev, "Reconfiguring PWM to "
  1786. "active high polarity\n");
  1787. it87_write_value(data, IT87_REG_FAN_CTL,
  1788. tmp | 0x87);
  1789. for (i = 0; i < 3; i++)
  1790. it87_write_value(data,
  1791. IT87_REG_PWM(i),
  1792. 0x7f & ~pwm[i]);
  1793. return 1;
  1794. }
  1795. dev_info(dev, "PWM configuration is "
  1796. "too broken to be fixed\n");
  1797. }
  1798. dev_info(dev, "Detected broken BIOS "
  1799. "defaults, disabling PWM interface\n");
  1800. return 0;
  1801. } else if (fix_pwm_polarity) {
  1802. dev_info(dev, "PWM configuration looks "
  1803. "sane, won't touch\n");
  1804. }
  1805. return 1;
  1806. }
  1807. /* Called when we have found a new IT87. */
  1808. static void __devinit it87_init_device(struct platform_device *pdev)
  1809. {
  1810. struct it87_sio_data *sio_data = pdev->dev.platform_data;
  1811. struct it87_data *data = platform_get_drvdata(pdev);
  1812. int tmp, i;
  1813. u8 mask;
  1814. /*
  1815. * For each PWM channel:
  1816. * - If it is in automatic mode, setting to manual mode should set
  1817. * the fan to full speed by default.
  1818. * - If it is in manual mode, we need a mapping to temperature
  1819. * channels to use when later setting to automatic mode later.
  1820. * Use a 1:1 mapping by default (we are clueless.)
  1821. * In both cases, the value can (and should) be changed by the user
  1822. * prior to switching to a different mode.
  1823. * Note that this is no longer needed for the IT8721F and later, as
  1824. * these have separate registers for the temperature mapping and the
  1825. * manual duty cycle.
  1826. */
  1827. for (i = 0; i < 3; i++) {
  1828. data->pwm_temp_map[i] = i;
  1829. data->pwm_duty[i] = 0x7f; /* Full speed */
  1830. data->auto_pwm[i][3] = 0x7f; /* Full speed, hard-coded */
  1831. }
  1832. /*
  1833. * Some chips seem to have default value 0xff for all limit
  1834. * registers. For low voltage limits it makes no sense and triggers
  1835. * alarms, so change to 0 instead. For high temperature limits, it
  1836. * means -1 degree C, which surprisingly doesn't trigger an alarm,
  1837. * but is still confusing, so change to 127 degrees C.
  1838. */
  1839. for (i = 0; i < 8; i++) {
  1840. tmp = it87_read_value(data, IT87_REG_VIN_MIN(i));
  1841. if (tmp == 0xff)
  1842. it87_write_value(data, IT87_REG_VIN_MIN(i), 0);
  1843. }
  1844. for (i = 0; i < 3; i++) {
  1845. tmp = it87_read_value(data, IT87_REG_TEMP_HIGH(i));
  1846. if (tmp == 0xff)
  1847. it87_write_value(data, IT87_REG_TEMP_HIGH(i), 127);
  1848. }
  1849. /*
  1850. * Temperature channels are not forcibly enabled, as they can be
  1851. * set to two different sensor types and we can't guess which one
  1852. * is correct for a given system. These channels can be enabled at
  1853. * run-time through the temp{1-3}_type sysfs accessors if needed.
  1854. */
  1855. /* Check if voltage monitors are reset manually or by some reason */
  1856. tmp = it87_read_value(data, IT87_REG_VIN_ENABLE);
  1857. if ((tmp & 0xff) == 0) {
  1858. /* Enable all voltage monitors */
  1859. it87_write_value(data, IT87_REG_VIN_ENABLE, 0xff);
  1860. }
  1861. /* Check if tachometers are reset manually or by some reason */
  1862. mask = 0x70 & ~(sio_data->skip_fan << 4);
  1863. data->fan_main_ctrl = it87_read_value(data, IT87_REG_FAN_MAIN_CTRL);
  1864. if ((data->fan_main_ctrl & mask) == 0) {
  1865. /* Enable all fan tachometers */
  1866. data->fan_main_ctrl |= mask;
  1867. it87_write_value(data, IT87_REG_FAN_MAIN_CTRL,
  1868. data->fan_main_ctrl);
  1869. }
  1870. data->has_fan = (data->fan_main_ctrl >> 4) & 0x07;
  1871. /* Set tachometers to 16-bit mode if needed */
  1872. if (has_16bit_fans(data)) {
  1873. tmp = it87_read_value(data, IT87_REG_FAN_16BIT);
  1874. if (~tmp & 0x07 & data->has_fan) {
  1875. dev_dbg(&pdev->dev,
  1876. "Setting fan1-3 to 16-bit mode\n");
  1877. it87_write_value(data, IT87_REG_FAN_16BIT,
  1878. tmp | 0x07);
  1879. }
  1880. /* IT8705F only supports three fans. */
  1881. if (data->type != it87) {
  1882. if (tmp & (1 << 4))
  1883. data->has_fan |= (1 << 3); /* fan4 enabled */
  1884. if (tmp & (1 << 5))
  1885. data->has_fan |= (1 << 4); /* fan5 enabled */
  1886. }
  1887. }
  1888. /* Fan input pins may be used for alternative functions */
  1889. data->has_fan &= ~sio_data->skip_fan;
  1890. /* Start monitoring */
  1891. it87_write_value(data, IT87_REG_CONFIG,
  1892. (it87_read_value(data, IT87_REG_CONFIG) & 0x36)
  1893. | (update_vbat ? 0x41 : 0x01));
  1894. }
  1895. static void it87_update_pwm_ctrl(struct it87_data *data, int nr)
  1896. {
  1897. data->pwm_ctrl[nr] = it87_read_value(data, IT87_REG_PWM(nr));
  1898. if (has_newer_autopwm(data)) {
  1899. data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
  1900. data->pwm_duty[nr] = it87_read_value(data,
  1901. IT87_REG_PWM_DUTY(nr));
  1902. } else {
  1903. if (data->pwm_ctrl[nr] & 0x80) /* Automatic mode */
  1904. data->pwm_temp_map[nr] = data->pwm_ctrl[nr] & 0x03;
  1905. else /* Manual mode */
  1906. data->pwm_duty[nr] = data->pwm_ctrl[nr] & 0x7f;
  1907. }
  1908. if (has_old_autopwm(data)) {
  1909. int i;
  1910. for (i = 0; i < 5 ; i++)
  1911. data->auto_temp[nr][i] = it87_read_value(data,
  1912. IT87_REG_AUTO_TEMP(nr, i));
  1913. for (i = 0; i < 3 ; i++)
  1914. data->auto_pwm[nr][i] = it87_read_value(data,
  1915. IT87_REG_AUTO_PWM(nr, i));
  1916. }
  1917. }
  1918. static struct it87_data *it87_update_device(struct device *dev)
  1919. {
  1920. struct it87_data *data = dev_get_drvdata(dev);
  1921. int i;
  1922. mutex_lock(&data->update_lock);
  1923. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  1924. || !data->valid) {
  1925. if (update_vbat) {
  1926. /*
  1927. * Cleared after each update, so reenable. Value
  1928. * returned by this read will be previous value
  1929. */
  1930. it87_write_value(data, IT87_REG_CONFIG,
  1931. it87_read_value(data, IT87_REG_CONFIG) | 0x40);
  1932. }
  1933. for (i = 0; i <= 7; i++) {
  1934. data->in[i] =
  1935. it87_read_value(data, IT87_REG_VIN(i));
  1936. data->in_min[i] =
  1937. it87_read_value(data, IT87_REG_VIN_MIN(i));
  1938. data->in_max[i] =
  1939. it87_read_value(data, IT87_REG_VIN_MAX(i));
  1940. }
  1941. /* in8 (battery) has no limit registers */
  1942. data->in[8] = it87_read_value(data, IT87_REG_VIN(8));
  1943. for (i = 0; i < 5; i++) {
  1944. /* Skip disabled fans */
  1945. if (!(data->has_fan & (1 << i)))
  1946. continue;
  1947. data->fan_min[i] =
  1948. it87_read_value(data, IT87_REG_FAN_MIN[i]);
  1949. data->fan[i] = it87_read_value(data,
  1950. IT87_REG_FAN[i]);
  1951. /* Add high byte if in 16-bit mode */
  1952. if (has_16bit_fans(data)) {
  1953. data->fan[i] |= it87_read_value(data,
  1954. IT87_REG_FANX[i]) << 8;
  1955. data->fan_min[i] |= it87_read_value(data,
  1956. IT87_REG_FANX_MIN[i]) << 8;
  1957. }
  1958. }
  1959. for (i = 0; i < 3; i++) {
  1960. data->temp[i] =
  1961. it87_read_value(data, IT87_REG_TEMP(i));
  1962. data->temp_high[i] =
  1963. it87_read_value(data, IT87_REG_TEMP_HIGH(i));
  1964. data->temp_low[i] =
  1965. it87_read_value(data, IT87_REG_TEMP_LOW(i));
  1966. }
  1967. /* Newer chips don't have clock dividers */
  1968. if ((data->has_fan & 0x07) && !has_16bit_fans(data)) {
  1969. i = it87_read_value(data, IT87_REG_FAN_DIV);
  1970. data->fan_div[0] = i & 0x07;
  1971. data->fan_div[1] = (i >> 3) & 0x07;
  1972. data->fan_div[2] = (i & 0x40) ? 3 : 1;
  1973. }
  1974. data->alarms =
  1975. it87_read_value(data, IT87_REG_ALARM1) |
  1976. (it87_read_value(data, IT87_REG_ALARM2) << 8) |
  1977. (it87_read_value(data, IT87_REG_ALARM3) << 16);
  1978. data->beeps = it87_read_value(data, IT87_REG_BEEP_ENABLE);
  1979. data->fan_main_ctrl = it87_read_value(data,
  1980. IT87_REG_FAN_MAIN_CTRL);
  1981. data->fan_ctl = it87_read_value(data, IT87_REG_FAN_CTL);
  1982. for (i = 0; i < 3; i++)
  1983. it87_update_pwm_ctrl(data, i);
  1984. data->sensor = it87_read_value(data, IT87_REG_TEMP_ENABLE);
  1985. /*
  1986. * The IT8705F does not have VID capability.
  1987. * The IT8718F and later don't use IT87_REG_VID for the
  1988. * same purpose.
  1989. */
  1990. if (data->type == it8712 || data->type == it8716) {
  1991. data->vid = it87_read_value(data, IT87_REG_VID);
  1992. /*
  1993. * The older IT8712F revisions had only 5 VID pins,
  1994. * but we assume it is always safe to read 6 bits.
  1995. */
  1996. data->vid &= 0x3f;
  1997. }
  1998. data->last_updated = jiffies;
  1999. data->valid = 1;
  2000. }
  2001. mutex_unlock(&data->update_lock);
  2002. return data;
  2003. }
  2004. static int __init it87_device_add(unsigned short address,
  2005. const struct it87_sio_data *sio_data)
  2006. {
  2007. struct resource res = {
  2008. .start = address + IT87_EC_OFFSET,
  2009. .end = address + IT87_EC_OFFSET + IT87_EC_EXTENT - 1,
  2010. .name = DRVNAME,
  2011. .flags = IORESOURCE_IO,
  2012. };
  2013. int err;
  2014. err = acpi_check_resource_conflict(&res);
  2015. if (err)
  2016. goto exit;
  2017. pdev = platform_device_alloc(DRVNAME, address);
  2018. if (!pdev) {
  2019. err = -ENOMEM;
  2020. pr_err("Device allocation failed\n");
  2021. goto exit;
  2022. }
  2023. err = platform_device_add_resources(pdev, &res, 1);
  2024. if (err) {
  2025. pr_err("Device resource addition failed (%d)\n", err);
  2026. goto exit_device_put;
  2027. }
  2028. err = platform_device_add_data(pdev, sio_data,
  2029. sizeof(struct it87_sio_data));
  2030. if (err) {
  2031. pr_err("Platform data allocation failed\n");
  2032. goto exit_device_put;
  2033. }
  2034. err = platform_device_add(pdev);
  2035. if (err) {
  2036. pr_err("Device addition failed (%d)\n", err);
  2037. goto exit_device_put;
  2038. }
  2039. return 0;
  2040. exit_device_put:
  2041. platform_device_put(pdev);
  2042. exit:
  2043. return err;
  2044. }
  2045. static int __init sm_it87_init(void)
  2046. {
  2047. int err;
  2048. unsigned short isa_address = 0;
  2049. struct it87_sio_data sio_data;
  2050. memset(&sio_data, 0, sizeof(struct it87_sio_data));
  2051. err = it87_find(&isa_address, &sio_data);
  2052. if (err)
  2053. return err;
  2054. err = platform_driver_register(&it87_driver);
  2055. if (err)
  2056. return err;
  2057. err = it87_device_add(isa_address, &sio_data);
  2058. if (err) {
  2059. platform_driver_unregister(&it87_driver);
  2060. return err;
  2061. }
  2062. return 0;
  2063. }
  2064. static void __exit sm_it87_exit(void)
  2065. {
  2066. platform_device_unregister(pdev);
  2067. platform_driver_unregister(&it87_driver);
  2068. }
  2069. MODULE_AUTHOR("Chris Gauthron, "
  2070. "Jean Delvare <khali@linux-fr.org>");
  2071. MODULE_DESCRIPTION("IT8705F/IT871xF/IT872xF hardware monitoring driver");
  2072. module_param(update_vbat, bool, 0);
  2073. MODULE_PARM_DESC(update_vbat, "Update vbat if set else return powerup value");
  2074. module_param(fix_pwm_polarity, bool, 0);
  2075. MODULE_PARM_DESC(fix_pwm_polarity,
  2076. "Force PWM polarity to active high (DANGEROUS)");
  2077. MODULE_LICENSE("GPL");
  2078. module_init(sm_it87_init);
  2079. module_exit(sm_it87_exit);