it87.c 67 KB

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