it87.c 99 KB

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