it87.c 80 KB

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