it87.c 80 KB

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