it87.c 67 KB

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