it87.c 77 KB

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