it87.c 74 KB

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