f71882fg.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. /***************************************************************************
  2. * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> *
  3. * Copyright (C) 2007,2008 by Hans de Goede <hdegoede@redhat.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/hwmon.h>
  26. #include <linux/hwmon-sysfs.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/io.h>
  30. #define DRVNAME "f71882fg"
  31. #define SIO_F71882FG_LD_HWM 0x04 /* Hardware monitor logical device */
  32. #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */
  33. #define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */
  34. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  35. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  36. #define SIO_REG_DEVREV 0x22 /* Device revision */
  37. #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
  38. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  39. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  40. #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */
  41. #define SIO_F71862_ID 0x0601 /* Chipset ID */
  42. #define SIO_F71882_ID 0x0541 /* Chipset ID */
  43. #define REGION_LENGTH 8
  44. #define ADDR_REG_OFFSET 5
  45. #define DATA_REG_OFFSET 6
  46. #define F71882FG_REG_PECI 0x0A
  47. #define F71882FG_REG_IN_STATUS 0x12 /* f71882fg only */
  48. #define F71882FG_REG_IN_BEEP 0x13 /* f71882fg only */
  49. #define F71882FG_REG_IN(nr) (0x20 + (nr))
  50. #define F71882FG_REG_IN1_HIGH 0x32 /* f71882fg only */
  51. #define F71882FG_REG_FAN(nr) (0xA0 + (16 * (nr)))
  52. #define F71882FG_REG_FAN_TARGET(nr) (0xA2 + (16 * (nr)))
  53. #define F71882FG_REG_FAN_FULL_SPEED(nr) (0xA4 + (16 * (nr)))
  54. #define F71882FG_REG_FAN_STATUS 0x92
  55. #define F71882FG_REG_FAN_BEEP 0x93
  56. #define F71882FG_REG_TEMP(nr) (0x70 + 2 * (nr))
  57. #define F71882FG_REG_TEMP_OVT(nr) (0x80 + 2 * (nr))
  58. #define F71882FG_REG_TEMP_HIGH(nr) (0x81 + 2 * (nr))
  59. #define F71882FG_REG_TEMP_STATUS 0x62
  60. #define F71882FG_REG_TEMP_BEEP 0x63
  61. #define F71882FG_REG_TEMP_HYST1 0x6C
  62. #define F71882FG_REG_TEMP_HYST23 0x6D
  63. #define F71882FG_REG_TEMP_TYPE 0x6B
  64. #define F71882FG_REG_TEMP_DIODE_OPEN 0x6F
  65. #define F71882FG_REG_PWM(nr) (0xA3 + (16 * (nr)))
  66. #define F71882FG_REG_PWM_TYPE 0x94
  67. #define F71882FG_REG_PWM_ENABLE 0x96
  68. #define F71882FG_REG_FAN_HYST0 0x98
  69. #define F71882FG_REG_FAN_HYST1 0x99
  70. #define F71882FG_REG_POINT_PWM(pwm, point) (0xAA + (point) + (16 * (pwm)))
  71. #define F71882FG_REG_POINT_TEMP(pwm, point) (0xA6 + (point) + (16 * (pwm)))
  72. #define F71882FG_REG_POINT_MAPPING(nr) (0xAF + 16 * (nr))
  73. #define F71882FG_REG_START 0x01
  74. #define FAN_MIN_DETECT 366 /* Lowest detectable fanspeed */
  75. static unsigned short force_id;
  76. module_param(force_id, ushort, 0);
  77. MODULE_PARM_DESC(force_id, "Override the detected device ID");
  78. static int fan_mode[4] = { 0, 0, 0, 0 };
  79. module_param_array(fan_mode, int, NULL, 0644);
  80. MODULE_PARM_DESC(fan_mode, "List of fan control modes (f71882fg only) "
  81. "(0=don't change, 1=pwm, 2=rpm)\n"
  82. "Note: this needs a write to pwm#_enable to take effect");
  83. enum chips { f71862fg, f71882fg };
  84. static const char *f71882fg_names[] = {
  85. "f71862fg",
  86. "f71882fg",
  87. };
  88. static struct platform_device *f71882fg_pdev;
  89. /* Super-I/O Function prototypes */
  90. static inline int superio_inb(int base, int reg);
  91. static inline int superio_inw(int base, int reg);
  92. static inline void superio_enter(int base);
  93. static inline void superio_select(int base, int ld);
  94. static inline void superio_exit(int base);
  95. struct f71882fg_sio_data {
  96. enum chips type;
  97. };
  98. struct f71882fg_data {
  99. unsigned short addr;
  100. enum chips type;
  101. struct device *hwmon_dev;
  102. struct mutex update_lock;
  103. char valid; /* !=0 if following fields are valid */
  104. unsigned long last_updated; /* In jiffies */
  105. unsigned long last_limits; /* In jiffies */
  106. /* Register Values */
  107. u8 in[9];
  108. u8 in1_max;
  109. u8 in_status;
  110. u8 in_beep;
  111. u16 fan[4];
  112. u16 fan_target[4];
  113. u16 fan_full_speed[4];
  114. u8 fan_status;
  115. u8 fan_beep;
  116. /* Note: all models have only 3 temperature channels, but on some
  117. they are addressed as 0-2 and on others as 1-3, so for coding
  118. convenience we reserve space for 4 channels */
  119. u8 temp[4];
  120. u8 temp_ovt[4];
  121. u8 temp_high[4];
  122. u8 temp_hyst[4];
  123. u8 temp_type[4];
  124. u8 temp_status;
  125. u8 temp_beep;
  126. u8 temp_diode_open;
  127. u8 pwm[4];
  128. u8 pwm_enable;
  129. u8 pwm_auto_point_hyst[2];
  130. u8 pwm_auto_point_mapping[4];
  131. u8 pwm_auto_point_pwm[4][5];
  132. u8 pwm_auto_point_temp[4][4];
  133. };
  134. /* Sysfs in */
  135. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  136. char *buf);
  137. static ssize_t show_in_max(struct device *dev, struct device_attribute
  138. *devattr, char *buf);
  139. static ssize_t store_in_max(struct device *dev, struct device_attribute
  140. *devattr, const char *buf, size_t count);
  141. static ssize_t show_in_beep(struct device *dev, struct device_attribute
  142. *devattr, char *buf);
  143. static ssize_t store_in_beep(struct device *dev, struct device_attribute
  144. *devattr, const char *buf, size_t count);
  145. static ssize_t show_in_alarm(struct device *dev, struct device_attribute
  146. *devattr, char *buf);
  147. /* Sysfs Fan */
  148. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  149. char *buf);
  150. static ssize_t show_fan_full_speed(struct device *dev,
  151. struct device_attribute *devattr, char *buf);
  152. static ssize_t store_fan_full_speed(struct device *dev,
  153. struct device_attribute *devattr, const char *buf, size_t count);
  154. static ssize_t show_fan_beep(struct device *dev, struct device_attribute
  155. *devattr, char *buf);
  156. static ssize_t store_fan_beep(struct device *dev, struct device_attribute
  157. *devattr, const char *buf, size_t count);
  158. static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
  159. *devattr, char *buf);
  160. /* Sysfs Temp */
  161. static ssize_t show_temp(struct device *dev, struct device_attribute
  162. *devattr, char *buf);
  163. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  164. *devattr, char *buf);
  165. static ssize_t store_temp_max(struct device *dev, struct device_attribute
  166. *devattr, const char *buf, size_t count);
  167. static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
  168. *devattr, char *buf);
  169. static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
  170. *devattr, const char *buf, size_t count);
  171. static ssize_t show_temp_crit(struct device *dev, struct device_attribute
  172. *devattr, char *buf);
  173. static ssize_t store_temp_crit(struct device *dev, struct device_attribute
  174. *devattr, const char *buf, size_t count);
  175. static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
  176. *devattr, char *buf);
  177. static ssize_t show_temp_type(struct device *dev, struct device_attribute
  178. *devattr, char *buf);
  179. static ssize_t show_temp_beep(struct device *dev, struct device_attribute
  180. *devattr, char *buf);
  181. static ssize_t store_temp_beep(struct device *dev, struct device_attribute
  182. *devattr, const char *buf, size_t count);
  183. static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
  184. *devattr, char *buf);
  185. static ssize_t show_temp_fault(struct device *dev, struct device_attribute
  186. *devattr, char *buf);
  187. /* PWM and Auto point control */
  188. static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
  189. char *buf);
  190. static ssize_t store_pwm(struct device *dev, struct device_attribute *devattr,
  191. const char *buf, size_t count);
  192. static ssize_t show_pwm_enable(struct device *dev,
  193. struct device_attribute *devattr, char *buf);
  194. static ssize_t store_pwm_enable(struct device *dev,
  195. struct device_attribute *devattr, const char *buf, size_t count);
  196. static ssize_t show_pwm_interpolate(struct device *dev,
  197. struct device_attribute *devattr, char *buf);
  198. static ssize_t store_pwm_interpolate(struct device *dev,
  199. struct device_attribute *devattr, const char *buf, size_t count);
  200. static ssize_t show_pwm_auto_point_channel(struct device *dev,
  201. struct device_attribute *devattr, char *buf);
  202. static ssize_t store_pwm_auto_point_channel(struct device *dev,
  203. struct device_attribute *devattr, const char *buf, size_t count);
  204. static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
  205. struct device_attribute *devattr, char *buf);
  206. static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
  207. struct device_attribute *devattr, const char *buf, size_t count);
  208. static ssize_t show_pwm_auto_point_pwm(struct device *dev,
  209. struct device_attribute *devattr, char *buf);
  210. static ssize_t store_pwm_auto_point_pwm(struct device *dev,
  211. struct device_attribute *devattr, const char *buf, size_t count);
  212. static ssize_t show_pwm_auto_point_temp(struct device *dev,
  213. struct device_attribute *devattr, char *buf);
  214. static ssize_t store_pwm_auto_point_temp(struct device *dev,
  215. struct device_attribute *devattr, const char *buf, size_t count);
  216. /* Sysfs misc */
  217. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  218. char *buf);
  219. static int __devinit f71882fg_probe(struct platform_device * pdev);
  220. static int f71882fg_remove(struct platform_device *pdev);
  221. static struct platform_driver f71882fg_driver = {
  222. .driver = {
  223. .owner = THIS_MODULE,
  224. .name = DRVNAME,
  225. },
  226. .probe = f71882fg_probe,
  227. .remove = __devexit_p(f71882fg_remove),
  228. };
  229. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  230. static struct sensor_device_attribute_2 f718x2fg_in_temp_attr[] = {
  231. SENSOR_ATTR_2(in0_input, S_IRUGO, show_in, NULL, 0, 0),
  232. SENSOR_ATTR_2(in1_input, S_IRUGO, show_in, NULL, 0, 1),
  233. SENSOR_ATTR_2(in2_input, S_IRUGO, show_in, NULL, 0, 2),
  234. SENSOR_ATTR_2(in3_input, S_IRUGO, show_in, NULL, 0, 3),
  235. SENSOR_ATTR_2(in4_input, S_IRUGO, show_in, NULL, 0, 4),
  236. SENSOR_ATTR_2(in5_input, S_IRUGO, show_in, NULL, 0, 5),
  237. SENSOR_ATTR_2(in6_input, S_IRUGO, show_in, NULL, 0, 6),
  238. SENSOR_ATTR_2(in7_input, S_IRUGO, show_in, NULL, 0, 7),
  239. SENSOR_ATTR_2(in8_input, S_IRUGO, show_in, NULL, 0, 8),
  240. SENSOR_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, 0, 1),
  241. SENSOR_ATTR_2(temp1_max, S_IRUGO|S_IWUSR, show_temp_max,
  242. store_temp_max, 0, 1),
  243. SENSOR_ATTR_2(temp1_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  244. store_temp_max_hyst, 0, 1),
  245. SENSOR_ATTR_2(temp1_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  246. store_temp_crit, 0, 1),
  247. SENSOR_ATTR_2(temp1_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  248. 0, 1),
  249. SENSOR_ATTR_2(temp1_type, S_IRUGO, show_temp_type, NULL, 0, 1),
  250. SENSOR_ATTR_2(temp1_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  251. store_temp_beep, 0, 1),
  252. SENSOR_ATTR_2(temp1_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 1),
  253. SENSOR_ATTR_2(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0, 1),
  254. SENSOR_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, 0, 2),
  255. SENSOR_ATTR_2(temp2_max, S_IRUGO|S_IWUSR, show_temp_max,
  256. store_temp_max, 0, 2),
  257. SENSOR_ATTR_2(temp2_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  258. store_temp_max_hyst, 0, 2),
  259. SENSOR_ATTR_2(temp2_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  260. store_temp_crit, 0, 2),
  261. SENSOR_ATTR_2(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  262. 0, 2),
  263. SENSOR_ATTR_2(temp2_type, S_IRUGO, show_temp_type, NULL, 0, 2),
  264. SENSOR_ATTR_2(temp2_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  265. store_temp_beep, 0, 2),
  266. SENSOR_ATTR_2(temp2_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 2),
  267. SENSOR_ATTR_2(temp2_fault, S_IRUGO, show_temp_fault, NULL, 0, 2),
  268. SENSOR_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, 0, 3),
  269. SENSOR_ATTR_2(temp3_max, S_IRUGO|S_IWUSR, show_temp_max,
  270. store_temp_max, 0, 3),
  271. SENSOR_ATTR_2(temp3_max_hyst, S_IRUGO|S_IWUSR, show_temp_max_hyst,
  272. store_temp_max_hyst, 0, 3),
  273. SENSOR_ATTR_2(temp3_crit, S_IRUGO|S_IWUSR, show_temp_crit,
  274. store_temp_crit, 0, 3),
  275. SENSOR_ATTR_2(temp3_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL,
  276. 0, 3),
  277. SENSOR_ATTR_2(temp3_type, S_IRUGO, show_temp_type, NULL, 0, 3),
  278. SENSOR_ATTR_2(temp3_beep, S_IRUGO|S_IWUSR, show_temp_beep,
  279. store_temp_beep, 0, 3),
  280. SENSOR_ATTR_2(temp3_alarm, S_IRUGO, show_temp_alarm, NULL, 0, 3),
  281. SENSOR_ATTR_2(temp3_fault, S_IRUGO, show_temp_fault, NULL, 0, 3),
  282. };
  283. static struct sensor_device_attribute_2 f71882fg_in_temp_attr[] = {
  284. SENSOR_ATTR_2(in1_max, S_IRUGO|S_IWUSR, show_in_max, store_in_max,
  285. 0, 1),
  286. SENSOR_ATTR_2(in1_beep, S_IRUGO|S_IWUSR, show_in_beep, store_in_beep,
  287. 0, 1),
  288. SENSOR_ATTR_2(in1_alarm, S_IRUGO, show_in_alarm, NULL, 0, 1),
  289. };
  290. static struct sensor_device_attribute_2 f718x2fg_fan_attr[] = {
  291. SENSOR_ATTR_2(fan1_input, S_IRUGO, show_fan, NULL, 0, 0),
  292. SENSOR_ATTR_2(fan1_full_speed, S_IRUGO|S_IWUSR,
  293. show_fan_full_speed,
  294. store_fan_full_speed, 0, 0),
  295. SENSOR_ATTR_2(fan1_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  296. store_fan_beep, 0, 0),
  297. SENSOR_ATTR_2(fan1_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 0),
  298. SENSOR_ATTR_2(fan2_input, S_IRUGO, show_fan, NULL, 0, 1),
  299. SENSOR_ATTR_2(fan2_full_speed, S_IRUGO|S_IWUSR,
  300. show_fan_full_speed,
  301. store_fan_full_speed, 0, 1),
  302. SENSOR_ATTR_2(fan2_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  303. store_fan_beep, 0, 1),
  304. SENSOR_ATTR_2(fan2_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 1),
  305. SENSOR_ATTR_2(fan3_input, S_IRUGO, show_fan, NULL, 0, 2),
  306. SENSOR_ATTR_2(fan3_full_speed, S_IRUGO|S_IWUSR,
  307. show_fan_full_speed,
  308. store_fan_full_speed, 0, 2),
  309. SENSOR_ATTR_2(fan3_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  310. store_fan_beep, 0, 2),
  311. SENSOR_ATTR_2(fan3_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 2),
  312. SENSOR_ATTR_2(pwm1, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 0),
  313. SENSOR_ATTR_2(pwm1_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  314. store_pwm_enable, 0, 0),
  315. SENSOR_ATTR_2(pwm1_interpolate, S_IRUGO|S_IWUSR,
  316. show_pwm_interpolate, store_pwm_interpolate, 0, 0),
  317. SENSOR_ATTR_2(pwm1_auto_channels_temp, S_IRUGO|S_IWUSR,
  318. show_pwm_auto_point_channel,
  319. store_pwm_auto_point_channel, 0, 0),
  320. SENSOR_ATTR_2(pwm2, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 1),
  321. SENSOR_ATTR_2(pwm2_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  322. store_pwm_enable, 0, 1),
  323. SENSOR_ATTR_2(pwm2_interpolate, S_IRUGO|S_IWUSR,
  324. show_pwm_interpolate, store_pwm_interpolate, 0, 1),
  325. SENSOR_ATTR_2(pwm2_auto_channels_temp, S_IRUGO|S_IWUSR,
  326. show_pwm_auto_point_channel,
  327. store_pwm_auto_point_channel, 0, 1),
  328. SENSOR_ATTR_2(pwm3, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 2),
  329. SENSOR_ATTR_2(pwm3_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  330. store_pwm_enable, 0, 2),
  331. SENSOR_ATTR_2(pwm3_interpolate, S_IRUGO|S_IWUSR,
  332. show_pwm_interpolate, store_pwm_interpolate, 0, 2),
  333. SENSOR_ATTR_2(pwm3_auto_channels_temp, S_IRUGO|S_IWUSR,
  334. show_pwm_auto_point_channel,
  335. store_pwm_auto_point_channel, 0, 2),
  336. };
  337. static struct sensor_device_attribute_2 f71862fg_fan_attr[] = {
  338. SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
  339. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  340. 1, 0),
  341. SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
  342. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  343. 4, 0),
  344. SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
  345. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  346. 0, 0),
  347. SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
  348. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  349. 3, 0),
  350. SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  351. show_pwm_auto_point_temp_hyst,
  352. store_pwm_auto_point_temp_hyst,
  353. 0, 0),
  354. SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
  355. show_pwm_auto_point_temp_hyst, NULL, 3, 0),
  356. SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
  357. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  358. 1, 1),
  359. SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
  360. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  361. 4, 1),
  362. SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
  363. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  364. 0, 1),
  365. SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
  366. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  367. 3, 1),
  368. SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  369. show_pwm_auto_point_temp_hyst,
  370. store_pwm_auto_point_temp_hyst,
  371. 0, 1),
  372. SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
  373. show_pwm_auto_point_temp_hyst, NULL, 3, 1),
  374. };
  375. static struct sensor_device_attribute_2 f71882fg_fan_attr[] = {
  376. SENSOR_ATTR_2(fan4_input, S_IRUGO, show_fan, NULL, 0, 3),
  377. SENSOR_ATTR_2(fan4_full_speed, S_IRUGO|S_IWUSR,
  378. show_fan_full_speed,
  379. store_fan_full_speed, 0, 3),
  380. SENSOR_ATTR_2(fan4_beep, S_IRUGO|S_IWUSR, show_fan_beep,
  381. store_fan_beep, 0, 3),
  382. SENSOR_ATTR_2(fan4_alarm, S_IRUGO, show_fan_alarm, NULL, 0, 3),
  383. SENSOR_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO|S_IWUSR,
  384. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  385. 0, 0),
  386. SENSOR_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO|S_IWUSR,
  387. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  388. 1, 0),
  389. SENSOR_ATTR_2(pwm1_auto_point3_pwm, S_IRUGO|S_IWUSR,
  390. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  391. 2, 0),
  392. SENSOR_ATTR_2(pwm1_auto_point4_pwm, S_IRUGO|S_IWUSR,
  393. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  394. 3, 0),
  395. SENSOR_ATTR_2(pwm1_auto_point5_pwm, S_IRUGO|S_IWUSR,
  396. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  397. 4, 0),
  398. SENSOR_ATTR_2(pwm1_auto_point1_temp, S_IRUGO|S_IWUSR,
  399. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  400. 0, 0),
  401. SENSOR_ATTR_2(pwm1_auto_point2_temp, S_IRUGO|S_IWUSR,
  402. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  403. 1, 0),
  404. SENSOR_ATTR_2(pwm1_auto_point3_temp, S_IRUGO|S_IWUSR,
  405. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  406. 2, 0),
  407. SENSOR_ATTR_2(pwm1_auto_point4_temp, S_IRUGO|S_IWUSR,
  408. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  409. 3, 0),
  410. SENSOR_ATTR_2(pwm1_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  411. show_pwm_auto_point_temp_hyst,
  412. store_pwm_auto_point_temp_hyst,
  413. 0, 0),
  414. SENSOR_ATTR_2(pwm1_auto_point2_temp_hyst, S_IRUGO,
  415. show_pwm_auto_point_temp_hyst, NULL, 1, 0),
  416. SENSOR_ATTR_2(pwm1_auto_point3_temp_hyst, S_IRUGO,
  417. show_pwm_auto_point_temp_hyst, NULL, 2, 0),
  418. SENSOR_ATTR_2(pwm1_auto_point4_temp_hyst, S_IRUGO,
  419. show_pwm_auto_point_temp_hyst, NULL, 3, 0),
  420. SENSOR_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO|S_IWUSR,
  421. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  422. 0, 1),
  423. SENSOR_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO|S_IWUSR,
  424. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  425. 1, 1),
  426. SENSOR_ATTR_2(pwm2_auto_point3_pwm, S_IRUGO|S_IWUSR,
  427. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  428. 2, 1),
  429. SENSOR_ATTR_2(pwm2_auto_point4_pwm, S_IRUGO|S_IWUSR,
  430. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  431. 3, 1),
  432. SENSOR_ATTR_2(pwm2_auto_point5_pwm, S_IRUGO|S_IWUSR,
  433. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  434. 4, 1),
  435. SENSOR_ATTR_2(pwm2_auto_point1_temp, S_IRUGO|S_IWUSR,
  436. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  437. 0, 1),
  438. SENSOR_ATTR_2(pwm2_auto_point2_temp, S_IRUGO|S_IWUSR,
  439. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  440. 1, 1),
  441. SENSOR_ATTR_2(pwm2_auto_point3_temp, S_IRUGO|S_IWUSR,
  442. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  443. 2, 1),
  444. SENSOR_ATTR_2(pwm2_auto_point4_temp, S_IRUGO|S_IWUSR,
  445. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  446. 3, 1),
  447. SENSOR_ATTR_2(pwm2_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  448. show_pwm_auto_point_temp_hyst,
  449. store_pwm_auto_point_temp_hyst,
  450. 0, 1),
  451. SENSOR_ATTR_2(pwm2_auto_point2_temp_hyst, S_IRUGO,
  452. show_pwm_auto_point_temp_hyst, NULL, 1, 1),
  453. SENSOR_ATTR_2(pwm2_auto_point3_temp_hyst, S_IRUGO,
  454. show_pwm_auto_point_temp_hyst, NULL, 2, 1),
  455. SENSOR_ATTR_2(pwm2_auto_point4_temp_hyst, S_IRUGO,
  456. show_pwm_auto_point_temp_hyst, NULL, 3, 1),
  457. SENSOR_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO|S_IWUSR,
  458. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  459. 0, 2),
  460. SENSOR_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO|S_IWUSR,
  461. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  462. 1, 2),
  463. SENSOR_ATTR_2(pwm3_auto_point3_pwm, S_IRUGO|S_IWUSR,
  464. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  465. 2, 2),
  466. SENSOR_ATTR_2(pwm3_auto_point4_pwm, S_IRUGO|S_IWUSR,
  467. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  468. 3, 2),
  469. SENSOR_ATTR_2(pwm3_auto_point5_pwm, S_IRUGO|S_IWUSR,
  470. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  471. 4, 2),
  472. SENSOR_ATTR_2(pwm3_auto_point1_temp, S_IRUGO|S_IWUSR,
  473. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  474. 0, 2),
  475. SENSOR_ATTR_2(pwm3_auto_point2_temp, S_IRUGO|S_IWUSR,
  476. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  477. 1, 2),
  478. SENSOR_ATTR_2(pwm3_auto_point3_temp, S_IRUGO|S_IWUSR,
  479. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  480. 2, 2),
  481. SENSOR_ATTR_2(pwm3_auto_point4_temp, S_IRUGO|S_IWUSR,
  482. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  483. 3, 2),
  484. SENSOR_ATTR_2(pwm3_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  485. show_pwm_auto_point_temp_hyst,
  486. store_pwm_auto_point_temp_hyst,
  487. 0, 2),
  488. SENSOR_ATTR_2(pwm3_auto_point2_temp_hyst, S_IRUGO,
  489. show_pwm_auto_point_temp_hyst, NULL, 1, 2),
  490. SENSOR_ATTR_2(pwm3_auto_point3_temp_hyst, S_IRUGO,
  491. show_pwm_auto_point_temp_hyst, NULL, 2, 2),
  492. SENSOR_ATTR_2(pwm3_auto_point4_temp_hyst, S_IRUGO,
  493. show_pwm_auto_point_temp_hyst, NULL, 3, 2),
  494. SENSOR_ATTR_2(pwm4, S_IRUGO|S_IWUSR, show_pwm, store_pwm, 0, 3),
  495. SENSOR_ATTR_2(pwm4_enable, S_IRUGO|S_IWUSR, show_pwm_enable,
  496. store_pwm_enable, 0, 3),
  497. SENSOR_ATTR_2(pwm4_interpolate, S_IRUGO|S_IWUSR,
  498. show_pwm_interpolate, store_pwm_interpolate, 0, 3),
  499. SENSOR_ATTR_2(pwm4_auto_channels_temp, S_IRUGO|S_IWUSR,
  500. show_pwm_auto_point_channel,
  501. store_pwm_auto_point_channel, 0, 3),
  502. SENSOR_ATTR_2(pwm4_auto_point1_pwm, S_IRUGO|S_IWUSR,
  503. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  504. 0, 3),
  505. SENSOR_ATTR_2(pwm4_auto_point2_pwm, S_IRUGO|S_IWUSR,
  506. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  507. 1, 3),
  508. SENSOR_ATTR_2(pwm4_auto_point3_pwm, S_IRUGO|S_IWUSR,
  509. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  510. 2, 3),
  511. SENSOR_ATTR_2(pwm4_auto_point4_pwm, S_IRUGO|S_IWUSR,
  512. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  513. 3, 3),
  514. SENSOR_ATTR_2(pwm4_auto_point5_pwm, S_IRUGO|S_IWUSR,
  515. show_pwm_auto_point_pwm, store_pwm_auto_point_pwm,
  516. 4, 3),
  517. SENSOR_ATTR_2(pwm4_auto_point1_temp, S_IRUGO|S_IWUSR,
  518. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  519. 0, 3),
  520. SENSOR_ATTR_2(pwm4_auto_point2_temp, S_IRUGO|S_IWUSR,
  521. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  522. 1, 3),
  523. SENSOR_ATTR_2(pwm4_auto_point3_temp, S_IRUGO|S_IWUSR,
  524. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  525. 2, 3),
  526. SENSOR_ATTR_2(pwm4_auto_point4_temp, S_IRUGO|S_IWUSR,
  527. show_pwm_auto_point_temp, store_pwm_auto_point_temp,
  528. 3, 3),
  529. SENSOR_ATTR_2(pwm4_auto_point1_temp_hyst, S_IRUGO|S_IWUSR,
  530. show_pwm_auto_point_temp_hyst,
  531. store_pwm_auto_point_temp_hyst,
  532. 0, 3),
  533. SENSOR_ATTR_2(pwm4_auto_point2_temp_hyst, S_IRUGO,
  534. show_pwm_auto_point_temp_hyst, NULL, 1, 3),
  535. SENSOR_ATTR_2(pwm4_auto_point3_temp_hyst, S_IRUGO,
  536. show_pwm_auto_point_temp_hyst, NULL, 2, 3),
  537. SENSOR_ATTR_2(pwm4_auto_point4_temp_hyst, S_IRUGO,
  538. show_pwm_auto_point_temp_hyst, NULL, 3, 3),
  539. };
  540. /* Super I/O functions */
  541. static inline int superio_inb(int base, int reg)
  542. {
  543. outb(reg, base);
  544. return inb(base + 1);
  545. }
  546. static int superio_inw(int base, int reg)
  547. {
  548. int val;
  549. outb(reg++, base);
  550. val = inb(base + 1) << 8;
  551. outb(reg, base);
  552. val |= inb(base + 1);
  553. return val;
  554. }
  555. static inline void superio_enter(int base)
  556. {
  557. /* according to the datasheet the key must be send twice! */
  558. outb( SIO_UNLOCK_KEY, base);
  559. outb( SIO_UNLOCK_KEY, base);
  560. }
  561. static inline void superio_select( int base, int ld)
  562. {
  563. outb(SIO_REG_LDSEL, base);
  564. outb(ld, base + 1);
  565. }
  566. static inline void superio_exit(int base)
  567. {
  568. outb(SIO_LOCK_KEY, base);
  569. }
  570. static inline u16 fan_from_reg(u16 reg)
  571. {
  572. return reg ? (1500000 / reg) : 0;
  573. }
  574. static inline u16 fan_to_reg(u16 fan)
  575. {
  576. return fan ? (1500000 / fan) : 0;
  577. }
  578. static u8 f71882fg_read8(struct f71882fg_data *data, u8 reg)
  579. {
  580. u8 val;
  581. outb(reg, data->addr + ADDR_REG_OFFSET);
  582. val = inb(data->addr + DATA_REG_OFFSET);
  583. return val;
  584. }
  585. static u16 f71882fg_read16(struct f71882fg_data *data, u8 reg)
  586. {
  587. u16 val;
  588. outb(reg++, data->addr + ADDR_REG_OFFSET);
  589. val = inb(data->addr + DATA_REG_OFFSET) << 8;
  590. outb(reg, data->addr + ADDR_REG_OFFSET);
  591. val |= inb(data->addr + DATA_REG_OFFSET);
  592. return val;
  593. }
  594. static void f71882fg_write8(struct f71882fg_data *data, u8 reg, u8 val)
  595. {
  596. outb(reg, data->addr + ADDR_REG_OFFSET);
  597. outb(val, data->addr + DATA_REG_OFFSET);
  598. }
  599. static void f71882fg_write16(struct f71882fg_data *data, u8 reg, u16 val)
  600. {
  601. outb(reg++, data->addr + ADDR_REG_OFFSET);
  602. outb(val >> 8, data->addr + DATA_REG_OFFSET);
  603. outb(reg, data->addr + ADDR_REG_OFFSET);
  604. outb(val & 255, data->addr + DATA_REG_OFFSET);
  605. }
  606. static struct f71882fg_data *f71882fg_update_device(struct device *dev)
  607. {
  608. struct f71882fg_data *data = dev_get_drvdata(dev);
  609. int nr, reg, reg2;
  610. int nr_fans = (data->type == f71862fg) ? 3 : 4;
  611. mutex_lock(&data->update_lock);
  612. /* Update once every 60 seconds */
  613. if ( time_after(jiffies, data->last_limits + 60 * HZ ) ||
  614. !data->valid) {
  615. if (data->type == f71882fg) {
  616. data->in1_max =
  617. f71882fg_read8(data, F71882FG_REG_IN1_HIGH);
  618. data->in_beep =
  619. f71882fg_read8(data, F71882FG_REG_IN_BEEP);
  620. }
  621. /* Get High & boundary temps*/
  622. for (nr = 1; nr < 4; nr++) {
  623. data->temp_ovt[nr] = f71882fg_read8(data,
  624. F71882FG_REG_TEMP_OVT(nr));
  625. data->temp_high[nr] = f71882fg_read8(data,
  626. F71882FG_REG_TEMP_HIGH(nr));
  627. }
  628. /* Have to hardcode hyst*/
  629. data->temp_hyst[1] = f71882fg_read8(data,
  630. F71882FG_REG_TEMP_HYST1) >> 4;
  631. /* Hyst temps 2 & 3 stored in same register */
  632. reg = f71882fg_read8(data, F71882FG_REG_TEMP_HYST23);
  633. data->temp_hyst[2] = reg & 0x0F;
  634. data->temp_hyst[3] = reg >> 4;
  635. /* Have to hardcode type, because temp1 is special */
  636. reg = f71882fg_read8(data, F71882FG_REG_TEMP_TYPE);
  637. reg2 = f71882fg_read8(data, F71882FG_REG_PECI);
  638. if ((reg2 & 0x03) == 0x01)
  639. data->temp_type[1] = 6 /* PECI */;
  640. else if ((reg2 & 0x03) == 0x02)
  641. data->temp_type[1] = 5 /* AMDSI */;
  642. else
  643. data->temp_type[1] = (reg & 0x02) ? 2 : 4;
  644. data->temp_type[2] = (reg & 0x04) ? 2 : 4;
  645. data->temp_type[3] = (reg & 0x08) ? 2 : 4;
  646. data->temp_beep = f71882fg_read8(data, F71882FG_REG_TEMP_BEEP);
  647. data->fan_beep = f71882fg_read8(data, F71882FG_REG_FAN_BEEP);
  648. data->pwm_enable = f71882fg_read8(data,
  649. F71882FG_REG_PWM_ENABLE);
  650. data->pwm_auto_point_hyst[0] = f71882fg_read8(data,
  651. F71882FG_REG_FAN_HYST0);
  652. data->pwm_auto_point_hyst[1] = f71882fg_read8(data,
  653. F71882FG_REG_FAN_HYST1);
  654. for (nr = 0; nr < nr_fans; nr++) {
  655. data->pwm_auto_point_mapping[nr] =
  656. f71882fg_read8(data,
  657. F71882FG_REG_POINT_MAPPING(nr));
  658. if (data->type == f71882fg) {
  659. int point;
  660. for (point = 0; point < 5; point++) {
  661. data->pwm_auto_point_pwm[nr][point] =
  662. f71882fg_read8(data,
  663. F71882FG_REG_POINT_PWM
  664. (nr, point));
  665. }
  666. for (point = 0; point < 4; point++) {
  667. data->pwm_auto_point_temp[nr][point] =
  668. f71882fg_read8(data,
  669. F71882FG_REG_POINT_TEMP
  670. (nr, point));
  671. }
  672. } else {
  673. data->pwm_auto_point_pwm[nr][1] =
  674. f71882fg_read8(data,
  675. F71882FG_REG_POINT_PWM
  676. (nr, 1));
  677. data->pwm_auto_point_pwm[nr][4] =
  678. f71882fg_read8(data,
  679. F71882FG_REG_POINT_PWM
  680. (nr, 4));
  681. data->pwm_auto_point_temp[nr][0] =
  682. f71882fg_read8(data,
  683. F71882FG_REG_POINT_TEMP
  684. (nr, 0));
  685. data->pwm_auto_point_temp[nr][3] =
  686. f71882fg_read8(data,
  687. F71882FG_REG_POINT_TEMP
  688. (nr, 3));
  689. }
  690. }
  691. data->last_limits = jiffies;
  692. }
  693. /* Update every second */
  694. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  695. data->temp_status = f71882fg_read8(data,
  696. F71882FG_REG_TEMP_STATUS);
  697. data->temp_diode_open = f71882fg_read8(data,
  698. F71882FG_REG_TEMP_DIODE_OPEN);
  699. for (nr = 1; nr < 4; nr++)
  700. data->temp[nr] = f71882fg_read8(data,
  701. F71882FG_REG_TEMP(nr));
  702. data->fan_status = f71882fg_read8(data,
  703. F71882FG_REG_FAN_STATUS);
  704. for (nr = 0; nr < nr_fans; nr++) {
  705. data->fan[nr] = f71882fg_read16(data,
  706. F71882FG_REG_FAN(nr));
  707. data->fan_target[nr] =
  708. f71882fg_read16(data, F71882FG_REG_FAN_TARGET(nr));
  709. data->fan_full_speed[nr] =
  710. f71882fg_read16(data,
  711. F71882FG_REG_FAN_FULL_SPEED(nr));
  712. data->pwm[nr] =
  713. f71882fg_read8(data, F71882FG_REG_PWM(nr));
  714. }
  715. if (data->type == f71882fg)
  716. data->in_status = f71882fg_read8(data,
  717. F71882FG_REG_IN_STATUS);
  718. for (nr = 0; nr < 9; nr++)
  719. data->in[nr] = f71882fg_read8(data,
  720. F71882FG_REG_IN(nr));
  721. data->last_updated = jiffies;
  722. data->valid = 1;
  723. }
  724. mutex_unlock(&data->update_lock);
  725. return data;
  726. }
  727. /* Sysfs Interface */
  728. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  729. char *buf)
  730. {
  731. struct f71882fg_data *data = f71882fg_update_device(dev);
  732. int nr = to_sensor_dev_attr_2(devattr)->index;
  733. int speed = fan_from_reg(data->fan[nr]);
  734. if (speed == FAN_MIN_DETECT)
  735. speed = 0;
  736. return sprintf(buf, "%d\n", speed);
  737. }
  738. static ssize_t show_fan_full_speed(struct device *dev,
  739. struct device_attribute *devattr, char *buf)
  740. {
  741. struct f71882fg_data *data = f71882fg_update_device(dev);
  742. int nr = to_sensor_dev_attr_2(devattr)->index;
  743. int speed = fan_from_reg(data->fan_full_speed[nr]);
  744. return sprintf(buf, "%d\n", speed);
  745. }
  746. static ssize_t store_fan_full_speed(struct device *dev,
  747. struct device_attribute *devattr,
  748. const char *buf, size_t count)
  749. {
  750. struct f71882fg_data *data = dev_get_drvdata(dev);
  751. int nr = to_sensor_dev_attr_2(devattr)->index;
  752. long val = simple_strtol(buf, NULL, 10);
  753. val = SENSORS_LIMIT(val, 23, 1500000);
  754. val = fan_to_reg(val);
  755. mutex_lock(&data->update_lock);
  756. if (data->pwm_enable & (1 << (2 * nr)))
  757. /* PWM mode */
  758. count = -EINVAL;
  759. else {
  760. /* RPM mode */
  761. f71882fg_write16(data, F71882FG_REG_FAN_FULL_SPEED(nr), val);
  762. data->fan_full_speed[nr] = val;
  763. }
  764. mutex_unlock(&data->update_lock);
  765. return count;
  766. }
  767. static ssize_t show_fan_beep(struct device *dev, struct device_attribute
  768. *devattr, char *buf)
  769. {
  770. struct f71882fg_data *data = f71882fg_update_device(dev);
  771. int nr = to_sensor_dev_attr_2(devattr)->index;
  772. if (data->fan_beep & (1 << nr))
  773. return sprintf(buf, "1\n");
  774. else
  775. return sprintf(buf, "0\n");
  776. }
  777. static ssize_t store_fan_beep(struct device *dev, struct device_attribute
  778. *devattr, const char *buf, size_t count)
  779. {
  780. struct f71882fg_data *data = dev_get_drvdata(dev);
  781. int nr = to_sensor_dev_attr_2(devattr)->index;
  782. int val = simple_strtoul(buf, NULL, 10);
  783. mutex_lock(&data->update_lock);
  784. if (val)
  785. data->fan_beep |= 1 << nr;
  786. else
  787. data->fan_beep &= ~(1 << nr);
  788. f71882fg_write8(data, F71882FG_REG_FAN_BEEP, data->fan_beep);
  789. mutex_unlock(&data->update_lock);
  790. return count;
  791. }
  792. static ssize_t show_fan_alarm(struct device *dev, struct device_attribute
  793. *devattr, char *buf)
  794. {
  795. struct f71882fg_data *data = f71882fg_update_device(dev);
  796. int nr = to_sensor_dev_attr_2(devattr)->index;
  797. if (data->fan_status & (1 << nr))
  798. return sprintf(buf, "1\n");
  799. else
  800. return sprintf(buf, "0\n");
  801. }
  802. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  803. char *buf)
  804. {
  805. struct f71882fg_data *data = f71882fg_update_device(dev);
  806. int nr = to_sensor_dev_attr_2(devattr)->index;
  807. return sprintf(buf, "%d\n", data->in[nr] * 8);
  808. }
  809. static ssize_t show_in_max(struct device *dev, struct device_attribute
  810. *devattr, char *buf)
  811. {
  812. struct f71882fg_data *data = f71882fg_update_device(dev);
  813. return sprintf(buf, "%d\n", data->in1_max * 8);
  814. }
  815. static ssize_t store_in_max(struct device *dev, struct device_attribute
  816. *devattr, const char *buf, size_t count)
  817. {
  818. struct f71882fg_data *data = dev_get_drvdata(dev);
  819. int val = simple_strtoul(buf, NULL, 10) / 8;
  820. if (val > 255)
  821. val = 255;
  822. mutex_lock(&data->update_lock);
  823. f71882fg_write8(data, F71882FG_REG_IN1_HIGH, val);
  824. data->in1_max = val;
  825. mutex_unlock(&data->update_lock);
  826. return count;
  827. }
  828. static ssize_t show_in_beep(struct device *dev, struct device_attribute
  829. *devattr, char *buf)
  830. {
  831. struct f71882fg_data *data = f71882fg_update_device(dev);
  832. int nr = to_sensor_dev_attr_2(devattr)->index;
  833. if (data->in_beep & (1 << nr))
  834. return sprintf(buf, "1\n");
  835. else
  836. return sprintf(buf, "0\n");
  837. }
  838. static ssize_t store_in_beep(struct device *dev, struct device_attribute
  839. *devattr, const char *buf, size_t count)
  840. {
  841. struct f71882fg_data *data = dev_get_drvdata(dev);
  842. int nr = to_sensor_dev_attr_2(devattr)->index;
  843. int val = simple_strtoul(buf, NULL, 10);
  844. mutex_lock(&data->update_lock);
  845. if (val)
  846. data->in_beep |= 1 << nr;
  847. else
  848. data->in_beep &= ~(1 << nr);
  849. f71882fg_write8(data, F71882FG_REG_IN_BEEP, data->in_beep);
  850. mutex_unlock(&data->update_lock);
  851. return count;
  852. }
  853. static ssize_t show_in_alarm(struct device *dev, struct device_attribute
  854. *devattr, char *buf)
  855. {
  856. struct f71882fg_data *data = f71882fg_update_device(dev);
  857. int nr = to_sensor_dev_attr_2(devattr)->index;
  858. if (data->in_status & (1 << nr))
  859. return sprintf(buf, "1\n");
  860. else
  861. return sprintf(buf, "0\n");
  862. }
  863. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  864. char *buf)
  865. {
  866. struct f71882fg_data *data = f71882fg_update_device(dev);
  867. int nr = to_sensor_dev_attr_2(devattr)->index;
  868. return sprintf(buf, "%d\n", data->temp[nr] * 1000);
  869. }
  870. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  871. *devattr, char *buf)
  872. {
  873. struct f71882fg_data *data = f71882fg_update_device(dev);
  874. int nr = to_sensor_dev_attr_2(devattr)->index;
  875. return sprintf(buf, "%d\n", data->temp_high[nr] * 1000);
  876. }
  877. static ssize_t store_temp_max(struct device *dev, struct device_attribute
  878. *devattr, const char *buf, size_t count)
  879. {
  880. struct f71882fg_data *data = dev_get_drvdata(dev);
  881. int nr = to_sensor_dev_attr_2(devattr)->index;
  882. int val = simple_strtoul(buf, NULL, 10) / 1000;
  883. if (val > 255)
  884. val = 255;
  885. mutex_lock(&data->update_lock);
  886. f71882fg_write8(data, F71882FG_REG_TEMP_HIGH(nr), val);
  887. data->temp_high[nr] = val;
  888. mutex_unlock(&data->update_lock);
  889. return count;
  890. }
  891. static ssize_t show_temp_max_hyst(struct device *dev, struct device_attribute
  892. *devattr, char *buf)
  893. {
  894. struct f71882fg_data *data = f71882fg_update_device(dev);
  895. int nr = to_sensor_dev_attr_2(devattr)->index;
  896. return sprintf(buf, "%d\n",
  897. (data->temp_high[nr] - data->temp_hyst[nr]) * 1000);
  898. }
  899. static ssize_t store_temp_max_hyst(struct device *dev, struct device_attribute
  900. *devattr, const char *buf, size_t count)
  901. {
  902. struct f71882fg_data *data = dev_get_drvdata(dev);
  903. int nr = to_sensor_dev_attr_2(devattr)->index;
  904. int val = simple_strtoul(buf, NULL, 10) / 1000;
  905. ssize_t ret = count;
  906. mutex_lock(&data->update_lock);
  907. /* convert abs to relative and check */
  908. val = data->temp_high[nr] - val;
  909. if (val < 0 || val > 15) {
  910. ret = -EINVAL;
  911. goto store_temp_max_hyst_exit;
  912. }
  913. data->temp_hyst[nr] = val;
  914. /* convert value to register contents */
  915. switch (nr) {
  916. case 1:
  917. val = val << 4;
  918. break;
  919. case 2:
  920. val = val | (data->temp_hyst[3] << 4);
  921. break;
  922. case 3:
  923. val = data->temp_hyst[2] | (val << 4);
  924. break;
  925. }
  926. f71882fg_write8(data, (nr <= 1) ? F71882FG_REG_TEMP_HYST1 :
  927. F71882FG_REG_TEMP_HYST23, val);
  928. store_temp_max_hyst_exit:
  929. mutex_unlock(&data->update_lock);
  930. return ret;
  931. }
  932. static ssize_t show_temp_crit(struct device *dev, struct device_attribute
  933. *devattr, char *buf)
  934. {
  935. struct f71882fg_data *data = f71882fg_update_device(dev);
  936. int nr = to_sensor_dev_attr_2(devattr)->index;
  937. return sprintf(buf, "%d\n", data->temp_ovt[nr] * 1000);
  938. }
  939. static ssize_t store_temp_crit(struct device *dev, struct device_attribute
  940. *devattr, const char *buf, size_t count)
  941. {
  942. struct f71882fg_data *data = dev_get_drvdata(dev);
  943. int nr = to_sensor_dev_attr_2(devattr)->index;
  944. int val = simple_strtoul(buf, NULL, 10) / 1000;
  945. if (val > 255)
  946. val = 255;
  947. mutex_lock(&data->update_lock);
  948. f71882fg_write8(data, F71882FG_REG_TEMP_OVT(nr), val);
  949. data->temp_ovt[nr] = val;
  950. mutex_unlock(&data->update_lock);
  951. return count;
  952. }
  953. static ssize_t show_temp_crit_hyst(struct device *dev, struct device_attribute
  954. *devattr, char *buf)
  955. {
  956. struct f71882fg_data *data = f71882fg_update_device(dev);
  957. int nr = to_sensor_dev_attr_2(devattr)->index;
  958. return sprintf(buf, "%d\n",
  959. (data->temp_ovt[nr] - data->temp_hyst[nr]) * 1000);
  960. }
  961. static ssize_t show_temp_type(struct device *dev, struct device_attribute
  962. *devattr, char *buf)
  963. {
  964. struct f71882fg_data *data = f71882fg_update_device(dev);
  965. int nr = to_sensor_dev_attr_2(devattr)->index;
  966. return sprintf(buf, "%d\n", data->temp_type[nr]);
  967. }
  968. static ssize_t show_temp_beep(struct device *dev, struct device_attribute
  969. *devattr, char *buf)
  970. {
  971. struct f71882fg_data *data = f71882fg_update_device(dev);
  972. int nr = to_sensor_dev_attr_2(devattr)->index;
  973. if (data->temp_beep & (1 << nr))
  974. return sprintf(buf, "1\n");
  975. else
  976. return sprintf(buf, "0\n");
  977. }
  978. static ssize_t store_temp_beep(struct device *dev, struct device_attribute
  979. *devattr, const char *buf, size_t count)
  980. {
  981. struct f71882fg_data *data = dev_get_drvdata(dev);
  982. int nr = to_sensor_dev_attr_2(devattr)->index;
  983. int val = simple_strtoul(buf, NULL, 10);
  984. mutex_lock(&data->update_lock);
  985. if (val)
  986. data->temp_beep |= 1 << nr;
  987. else
  988. data->temp_beep &= ~(1 << nr);
  989. f71882fg_write8(data, F71882FG_REG_TEMP_BEEP, data->temp_beep);
  990. mutex_unlock(&data->update_lock);
  991. return count;
  992. }
  993. static ssize_t show_temp_alarm(struct device *dev, struct device_attribute
  994. *devattr, char *buf)
  995. {
  996. struct f71882fg_data *data = f71882fg_update_device(dev);
  997. int nr = to_sensor_dev_attr_2(devattr)->index;
  998. if (data->temp_status & (1 << nr))
  999. return sprintf(buf, "1\n");
  1000. else
  1001. return sprintf(buf, "0\n");
  1002. }
  1003. static ssize_t show_temp_fault(struct device *dev, struct device_attribute
  1004. *devattr, char *buf)
  1005. {
  1006. struct f71882fg_data *data = f71882fg_update_device(dev);
  1007. int nr = to_sensor_dev_attr_2(devattr)->index;
  1008. if (data->temp_diode_open & (1 << nr))
  1009. return sprintf(buf, "1\n");
  1010. else
  1011. return sprintf(buf, "0\n");
  1012. }
  1013. static ssize_t show_pwm(struct device *dev,
  1014. struct device_attribute *devattr, char *buf)
  1015. {
  1016. struct f71882fg_data *data = f71882fg_update_device(dev);
  1017. int val, nr = to_sensor_dev_attr_2(devattr)->index;
  1018. if (data->pwm_enable & (1 << (2 * nr)))
  1019. /* PWM mode */
  1020. val = data->pwm[nr];
  1021. else {
  1022. /* RPM mode */
  1023. mutex_lock(&data->update_lock);
  1024. val = 255 * fan_from_reg(data->fan_target[nr])
  1025. / fan_from_reg(data->fan_full_speed[nr]);
  1026. mutex_unlock(&data->update_lock);
  1027. }
  1028. return sprintf(buf, "%d\n", val);
  1029. }
  1030. static ssize_t store_pwm(struct device *dev,
  1031. struct device_attribute *devattr, const char *buf,
  1032. size_t count)
  1033. {
  1034. /* struct f71882fg_data *data = dev_get_drvdata(dev); */
  1035. struct f71882fg_data *data = f71882fg_update_device(dev);
  1036. int nr = to_sensor_dev_attr_2(devattr)->index;
  1037. long val = simple_strtol(buf, NULL, 10);
  1038. val = SENSORS_LIMIT(val, 0, 255);
  1039. mutex_lock(&data->update_lock);
  1040. if (data->pwm_enable & (1 << (2 * nr))) {
  1041. /* PWM mode */
  1042. f71882fg_write8(data, F71882FG_REG_PWM(nr), val);
  1043. data->pwm[nr] = val;
  1044. } else {
  1045. /* RPM mode */
  1046. int target = val * fan_from_reg(data->fan_full_speed[nr]) / 255;
  1047. f71882fg_write16(data, F71882FG_REG_FAN_TARGET(nr),
  1048. fan_to_reg(target));
  1049. data->fan_target[nr] = fan_to_reg(target);
  1050. }
  1051. mutex_unlock(&data->update_lock);
  1052. return count;
  1053. }
  1054. static ssize_t show_pwm_enable(struct device *dev,
  1055. struct device_attribute *devattr, char *buf)
  1056. {
  1057. int result;
  1058. struct f71882fg_data *data = f71882fg_update_device(dev);
  1059. int nr = to_sensor_dev_attr_2(devattr)->index;
  1060. if (data->pwm_enable & (2 << (2 * nr)))
  1061. result = 1;
  1062. else
  1063. result = 2;
  1064. return sprintf(buf, "%d\n", result);
  1065. }
  1066. static ssize_t store_pwm_enable(struct device *dev, struct device_attribute
  1067. *devattr, const char *buf, size_t count)
  1068. {
  1069. struct f71882fg_data *data = dev_get_drvdata(dev);
  1070. int nr = to_sensor_dev_attr_2(devattr)->index;
  1071. long val = simple_strtol(buf, NULL, 10);
  1072. if (val < 1 || val > 2)
  1073. return -EINVAL;
  1074. mutex_lock(&data->update_lock);
  1075. switch (val) {
  1076. case 1:
  1077. data->pwm_enable |= 2 << (2 * nr);
  1078. break; /* Manual */
  1079. case 2:
  1080. data->pwm_enable &= ~(2 << (2 * nr));
  1081. break; /* Temperature ctrl */
  1082. }
  1083. if (data->type == f71882fg) {
  1084. switch (fan_mode[nr]) {
  1085. case 1:
  1086. data->pwm_enable |= 1 << (2 * nr);
  1087. break; /* Duty cycle mode */
  1088. case 2:
  1089. data->pwm_enable &= ~(1 << (2 * nr));
  1090. break; /* RPM mode */
  1091. }
  1092. }
  1093. f71882fg_write8(data, F71882FG_REG_PWM_ENABLE, data->pwm_enable);
  1094. mutex_unlock(&data->update_lock);
  1095. return count;
  1096. }
  1097. static ssize_t show_pwm_auto_point_pwm(struct device *dev,
  1098. struct device_attribute *devattr,
  1099. char *buf)
  1100. {
  1101. int result;
  1102. struct f71882fg_data *data = f71882fg_update_device(dev);
  1103. int pwm = to_sensor_dev_attr_2(devattr)->index;
  1104. int point = to_sensor_dev_attr_2(devattr)->nr;
  1105. if (data->pwm_enable & (1 << (2 * pwm))) {
  1106. /* PWM mode */
  1107. result = data->pwm_auto_point_pwm[pwm][point];
  1108. } else {
  1109. /* RPM mode */
  1110. result = 32 * 255 / (32 + data->pwm_auto_point_pwm[pwm][point]);
  1111. }
  1112. return sprintf(buf, "%d\n", result);
  1113. }
  1114. static ssize_t store_pwm_auto_point_pwm(struct device *dev,
  1115. struct device_attribute *devattr,
  1116. const char *buf, size_t count)
  1117. {
  1118. /* struct f71882fg_data *data = dev_get_drvdata(dev); */
  1119. struct f71882fg_data *data = f71882fg_update_device(dev);
  1120. int pwm = to_sensor_dev_attr_2(devattr)->index;
  1121. int point = to_sensor_dev_attr_2(devattr)->nr;
  1122. int val = simple_strtoul(buf, NULL, 10);
  1123. val = SENSORS_LIMIT(val, 0, 255);
  1124. mutex_lock(&data->update_lock);
  1125. if (data->pwm_enable & (1 << (2 * pwm))) {
  1126. /* PWM mode */
  1127. } else {
  1128. /* RPM mode */
  1129. if (val < 29) /* Prevent negative numbers */
  1130. val = 255;
  1131. else
  1132. val = (255 - val) * 32 / val;
  1133. }
  1134. f71882fg_write8(data, F71882FG_REG_POINT_PWM(pwm, point), val);
  1135. data->pwm_auto_point_pwm[pwm][point] = val;
  1136. mutex_unlock(&data->update_lock);
  1137. return count;
  1138. }
  1139. static ssize_t show_pwm_auto_point_temp_hyst(struct device *dev,
  1140. struct device_attribute *devattr,
  1141. char *buf)
  1142. {
  1143. int result = 0;
  1144. struct f71882fg_data *data = f71882fg_update_device(dev);
  1145. int nr = to_sensor_dev_attr_2(devattr)->index;
  1146. int point = to_sensor_dev_attr_2(devattr)->nr;
  1147. mutex_lock(&data->update_lock);
  1148. switch (nr) {
  1149. case 0:
  1150. result = data->pwm_auto_point_hyst[0] & 0x0f;
  1151. break;
  1152. case 1:
  1153. result = data->pwm_auto_point_hyst[0] >> 4;
  1154. break;
  1155. case 2:
  1156. result = data->pwm_auto_point_hyst[1] & 0x0f;
  1157. break;
  1158. case 3:
  1159. result = data->pwm_auto_point_hyst[1] >> 4;
  1160. break;
  1161. }
  1162. result = 1000 * (data->pwm_auto_point_temp[nr][point] - result);
  1163. mutex_unlock(&data->update_lock);
  1164. return sprintf(buf, "%d\n", result);
  1165. }
  1166. static ssize_t store_pwm_auto_point_temp_hyst(struct device *dev,
  1167. struct device_attribute *devattr,
  1168. const char *buf, size_t count)
  1169. {
  1170. struct f71882fg_data *data = f71882fg_update_device(dev);
  1171. int nr = to_sensor_dev_attr_2(devattr)->index;
  1172. int point = to_sensor_dev_attr_2(devattr)->nr;
  1173. long val = simple_strtol(buf, NULL, 10) / 1000;
  1174. mutex_lock(&data->update_lock);
  1175. val = SENSORS_LIMIT(val, data->pwm_auto_point_temp[nr][point] - 15,
  1176. data->pwm_auto_point_temp[nr][point]);
  1177. val = data->pwm_auto_point_temp[nr][point] - val;
  1178. switch (nr) {
  1179. case 0:
  1180. val = (data->pwm_auto_point_hyst[0] & 0xf0) | val;
  1181. break;
  1182. case 1:
  1183. val = (data->pwm_auto_point_hyst[0] & 0x0f) | (val << 4);
  1184. break;
  1185. case 2:
  1186. val = (data->pwm_auto_point_hyst[1] & 0xf0) | val;
  1187. break;
  1188. case 3:
  1189. val = (data->pwm_auto_point_hyst[1] & 0x0f) | (val << 4);
  1190. break;
  1191. }
  1192. if (nr == 0 || nr == 1) {
  1193. f71882fg_write8(data, F71882FG_REG_FAN_HYST0, val);
  1194. data->pwm_auto_point_hyst[0] = val;
  1195. } else {
  1196. f71882fg_write8(data, F71882FG_REG_FAN_HYST1, val);
  1197. data->pwm_auto_point_hyst[1] = val;
  1198. }
  1199. mutex_unlock(&data->update_lock);
  1200. return count;
  1201. }
  1202. static ssize_t show_pwm_interpolate(struct device *dev,
  1203. struct device_attribute *devattr, char *buf)
  1204. {
  1205. int result;
  1206. struct f71882fg_data *data = f71882fg_update_device(dev);
  1207. int nr = to_sensor_dev_attr_2(devattr)->index;
  1208. result = (data->pwm_auto_point_mapping[nr] >> 4) & 1;
  1209. return sprintf(buf, "%d\n", result);
  1210. }
  1211. static ssize_t store_pwm_interpolate(struct device *dev,
  1212. struct device_attribute *devattr,
  1213. const char *buf, size_t count)
  1214. {
  1215. /* struct f71882fg_data *data = dev_get_drvdata(dev); */
  1216. struct f71882fg_data *data = f71882fg_update_device(dev);
  1217. int nr = to_sensor_dev_attr_2(devattr)->index;
  1218. int val = simple_strtoul(buf, NULL, 10);
  1219. mutex_lock(&data->update_lock);
  1220. if (val)
  1221. val = data->pwm_auto_point_mapping[nr] | (1 << 4);
  1222. else
  1223. val = data->pwm_auto_point_mapping[nr] & (~(1 << 4));
  1224. f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
  1225. data->pwm_auto_point_mapping[nr] = val;
  1226. mutex_unlock(&data->update_lock);
  1227. return count;
  1228. }
  1229. static ssize_t show_pwm_auto_point_channel(struct device *dev,
  1230. struct device_attribute *devattr,
  1231. char *buf)
  1232. {
  1233. int result;
  1234. struct f71882fg_data *data = f71882fg_update_device(dev);
  1235. int nr = to_sensor_dev_attr_2(devattr)->index;
  1236. result = 1 << ((data->pwm_auto_point_mapping[nr] & 3) - 1);
  1237. return sprintf(buf, "%d\n", result);
  1238. }
  1239. static ssize_t store_pwm_auto_point_channel(struct device *dev,
  1240. struct device_attribute *devattr,
  1241. const char *buf, size_t count)
  1242. {
  1243. /* struct f71882fg_data *data = dev_get_drvdata(dev); */
  1244. struct f71882fg_data *data = f71882fg_update_device(dev);
  1245. int nr = to_sensor_dev_attr_2(devattr)->index;
  1246. long val = simple_strtol(buf, NULL, 10);
  1247. switch (val) {
  1248. case 1:
  1249. val = 1;
  1250. break;
  1251. case 2:
  1252. val = 2;
  1253. break;
  1254. case 4:
  1255. val = 3;
  1256. break;
  1257. default:
  1258. return -EINVAL;
  1259. }
  1260. mutex_lock(&data->update_lock);
  1261. val = (data->pwm_auto_point_mapping[nr] & 0xfc) | val;
  1262. f71882fg_write8(data, F71882FG_REG_POINT_MAPPING(nr), val);
  1263. data->pwm_auto_point_mapping[nr] = val;
  1264. mutex_unlock(&data->update_lock);
  1265. return count;
  1266. }
  1267. static ssize_t show_pwm_auto_point_temp(struct device *dev,
  1268. struct device_attribute *devattr,
  1269. char *buf)
  1270. {
  1271. int result;
  1272. struct f71882fg_data *data = f71882fg_update_device(dev);
  1273. int pwm = to_sensor_dev_attr_2(devattr)->index;
  1274. int point = to_sensor_dev_attr_2(devattr)->nr;
  1275. result = data->pwm_auto_point_temp[pwm][point];
  1276. return sprintf(buf, "%d\n", 1000 * result);
  1277. }
  1278. static ssize_t store_pwm_auto_point_temp(struct device *dev,
  1279. struct device_attribute *devattr,
  1280. const char *buf, size_t count)
  1281. {
  1282. /* struct f71882fg_data *data = dev_get_drvdata(dev); */
  1283. struct f71882fg_data *data = f71882fg_update_device(dev);
  1284. int pwm = to_sensor_dev_attr_2(devattr)->index;
  1285. int point = to_sensor_dev_attr_2(devattr)->nr;
  1286. long val = simple_strtol(buf, NULL, 10) / 1000;
  1287. val = SENSORS_LIMIT(val, 0, 255);
  1288. mutex_lock(&data->update_lock);
  1289. f71882fg_write8(data, F71882FG_REG_POINT_TEMP(pwm, point), val);
  1290. data->pwm_auto_point_temp[pwm][point] = val;
  1291. mutex_unlock(&data->update_lock);
  1292. return count;
  1293. }
  1294. static ssize_t show_name(struct device *dev, struct device_attribute *devattr,
  1295. char *buf)
  1296. {
  1297. struct f71882fg_data *data = dev_get_drvdata(dev);
  1298. return sprintf(buf, "%s\n", f71882fg_names[data->type]);
  1299. }
  1300. static int __devinit f71882fg_create_sysfs_files(struct platform_device *pdev,
  1301. struct sensor_device_attribute_2 *attr, int count)
  1302. {
  1303. int err, i;
  1304. for (i = 0; i < count; i++) {
  1305. err = device_create_file(&pdev->dev, &attr[i].dev_attr);
  1306. if (err)
  1307. return err;
  1308. }
  1309. return 0;
  1310. }
  1311. static int __devinit f71882fg_probe(struct platform_device *pdev)
  1312. {
  1313. struct f71882fg_data *data;
  1314. struct f71882fg_sio_data *sio_data = pdev->dev.platform_data;
  1315. int err;
  1316. u8 start_reg;
  1317. data = kzalloc(sizeof(struct f71882fg_data), GFP_KERNEL);
  1318. if (!data)
  1319. return -ENOMEM;
  1320. data->addr = platform_get_resource(pdev, IORESOURCE_IO, 0)->start;
  1321. data->type = sio_data->type;
  1322. mutex_init(&data->update_lock);
  1323. platform_set_drvdata(pdev, data);
  1324. start_reg = f71882fg_read8(data, F71882FG_REG_START);
  1325. if (!(start_reg & 0x03)) {
  1326. dev_warn(&pdev->dev, "Hardware monitoring not activated\n");
  1327. err = -ENODEV;
  1328. goto exit_free;
  1329. }
  1330. /* If it is a 71862 and the fan / pwm part is enabled sanity check
  1331. the pwm settings */
  1332. if (data->type == f71862fg && (start_reg & 0x02)) {
  1333. u8 reg = f71882fg_read8(data, F71882FG_REG_PWM_ENABLE);
  1334. if ((reg & 0x15) != 0x15) {
  1335. dev_err(&pdev->dev,
  1336. "Invalid (reserved) pwm settings: 0x%02x\n",
  1337. (unsigned int)reg);
  1338. err = -ENODEV;
  1339. goto exit_free;
  1340. }
  1341. }
  1342. /* Register sysfs interface files */
  1343. err = device_create_file(&pdev->dev, &dev_attr_name);
  1344. if (err)
  1345. goto exit_unregister_sysfs;
  1346. if (start_reg & 0x01) {
  1347. err = f71882fg_create_sysfs_files(pdev, f718x2fg_in_temp_attr,
  1348. ARRAY_SIZE(f718x2fg_in_temp_attr));
  1349. if (err)
  1350. goto exit_unregister_sysfs;
  1351. if (data->type == f71882fg) {
  1352. err = f71882fg_create_sysfs_files(pdev,
  1353. f71882fg_in_temp_attr,
  1354. ARRAY_SIZE(f71882fg_in_temp_attr));
  1355. if (err)
  1356. goto exit_unregister_sysfs;
  1357. }
  1358. }
  1359. if (start_reg & 0x02) {
  1360. err = f71882fg_create_sysfs_files(pdev, f718x2fg_fan_attr,
  1361. ARRAY_SIZE(f718x2fg_fan_attr));
  1362. if (err)
  1363. goto exit_unregister_sysfs;
  1364. if (data->type == f71862fg) {
  1365. err = f71882fg_create_sysfs_files(pdev,
  1366. f71862fg_fan_attr,
  1367. ARRAY_SIZE(f71862fg_fan_attr));
  1368. } else {
  1369. err = f71882fg_create_sysfs_files(pdev,
  1370. f71882fg_fan_attr,
  1371. ARRAY_SIZE(f71882fg_fan_attr));
  1372. }
  1373. if (err)
  1374. goto exit_unregister_sysfs;
  1375. }
  1376. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  1377. if (IS_ERR(data->hwmon_dev)) {
  1378. err = PTR_ERR(data->hwmon_dev);
  1379. data->hwmon_dev = NULL;
  1380. goto exit_unregister_sysfs;
  1381. }
  1382. return 0;
  1383. exit_unregister_sysfs:
  1384. f71882fg_remove(pdev); /* Will unregister the sysfs files for us */
  1385. return err; /* f71882fg_remove() also frees our data */
  1386. exit_free:
  1387. kfree(data);
  1388. return err;
  1389. }
  1390. static int f71882fg_remove(struct platform_device *pdev)
  1391. {
  1392. int i;
  1393. struct f71882fg_data *data = platform_get_drvdata(pdev);
  1394. platform_set_drvdata(pdev, NULL);
  1395. if (data->hwmon_dev)
  1396. hwmon_device_unregister(data->hwmon_dev);
  1397. device_remove_file(&pdev->dev, &dev_attr_name);
  1398. for (i = 0; i < ARRAY_SIZE(f718x2fg_in_temp_attr); i++)
  1399. device_remove_file(&pdev->dev,
  1400. &f718x2fg_in_temp_attr[i].dev_attr);
  1401. for (i = 0; i < ARRAY_SIZE(f71882fg_in_temp_attr); i++)
  1402. device_remove_file(&pdev->dev,
  1403. &f71882fg_in_temp_attr[i].dev_attr);
  1404. for (i = 0; i < ARRAY_SIZE(f718x2fg_fan_attr); i++)
  1405. device_remove_file(&pdev->dev, &f718x2fg_fan_attr[i].dev_attr);
  1406. for (i = 0; i < ARRAY_SIZE(f71862fg_fan_attr); i++)
  1407. device_remove_file(&pdev->dev, &f71862fg_fan_attr[i].dev_attr);
  1408. for (i = 0; i < ARRAY_SIZE(f71882fg_fan_attr); i++)
  1409. device_remove_file(&pdev->dev, &f71882fg_fan_attr[i].dev_attr);
  1410. kfree(data);
  1411. return 0;
  1412. }
  1413. static int __init f71882fg_find(int sioaddr, unsigned short *address,
  1414. struct f71882fg_sio_data *sio_data)
  1415. {
  1416. int err = -ENODEV;
  1417. u16 devid;
  1418. superio_enter(sioaddr);
  1419. devid = superio_inw(sioaddr, SIO_REG_MANID);
  1420. if (devid != SIO_FINTEK_ID) {
  1421. printk(KERN_INFO DRVNAME ": Not a Fintek device\n");
  1422. goto exit;
  1423. }
  1424. devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID);
  1425. switch (devid) {
  1426. case SIO_F71862_ID:
  1427. sio_data->type = f71862fg;
  1428. break;
  1429. case SIO_F71882_ID:
  1430. sio_data->type = f71882fg;
  1431. break;
  1432. default:
  1433. printk(KERN_INFO DRVNAME ": Unsupported Fintek device\n");
  1434. goto exit;
  1435. }
  1436. superio_select(sioaddr, SIO_F71882FG_LD_HWM);
  1437. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  1438. printk(KERN_WARNING DRVNAME ": Device not activated\n");
  1439. goto exit;
  1440. }
  1441. *address = superio_inw(sioaddr, SIO_REG_ADDR);
  1442. if (*address == 0)
  1443. {
  1444. printk(KERN_WARNING DRVNAME ": Base address not set\n");
  1445. goto exit;
  1446. }
  1447. *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
  1448. err = 0;
  1449. printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %d\n",
  1450. f71882fg_names[sio_data->type], (unsigned int)*address,
  1451. (int)superio_inb(sioaddr, SIO_REG_DEVREV));
  1452. exit:
  1453. superio_exit(sioaddr);
  1454. return err;
  1455. }
  1456. static int __init f71882fg_device_add(unsigned short address,
  1457. const struct f71882fg_sio_data *sio_data)
  1458. {
  1459. struct resource res = {
  1460. .start = address,
  1461. .end = address + REGION_LENGTH - 1,
  1462. .flags = IORESOURCE_IO,
  1463. };
  1464. int err;
  1465. f71882fg_pdev = platform_device_alloc(DRVNAME, address);
  1466. if (!f71882fg_pdev)
  1467. return -ENOMEM;
  1468. res.name = f71882fg_pdev->name;
  1469. err = platform_device_add_resources(f71882fg_pdev, &res, 1);
  1470. if (err) {
  1471. printk(KERN_ERR DRVNAME ": Device resource addition failed\n");
  1472. goto exit_device_put;
  1473. }
  1474. err = platform_device_add_data(f71882fg_pdev, sio_data,
  1475. sizeof(struct f71882fg_sio_data));
  1476. if (err) {
  1477. printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
  1478. goto exit_device_put;
  1479. }
  1480. err = platform_device_add(f71882fg_pdev);
  1481. if (err) {
  1482. printk(KERN_ERR DRVNAME ": Device addition failed\n");
  1483. goto exit_device_put;
  1484. }
  1485. return 0;
  1486. exit_device_put:
  1487. platform_device_put(f71882fg_pdev);
  1488. return err;
  1489. }
  1490. static int __init f71882fg_init(void)
  1491. {
  1492. int err = -ENODEV;
  1493. unsigned short address;
  1494. struct f71882fg_sio_data sio_data;
  1495. memset(&sio_data, 0, sizeof(sio_data));
  1496. if (f71882fg_find(0x2e, &address, &sio_data) &&
  1497. f71882fg_find(0x4e, &address, &sio_data))
  1498. goto exit;
  1499. err = platform_driver_register(&f71882fg_driver);
  1500. if (err)
  1501. goto exit;
  1502. err = f71882fg_device_add(address, &sio_data);
  1503. if (err)
  1504. goto exit_driver;
  1505. return 0;
  1506. exit_driver:
  1507. platform_driver_unregister(&f71882fg_driver);
  1508. exit:
  1509. return err;
  1510. }
  1511. static void __exit f71882fg_exit(void)
  1512. {
  1513. platform_device_unregister(f71882fg_pdev);
  1514. platform_driver_unregister(&f71882fg_driver);
  1515. }
  1516. MODULE_DESCRIPTION("F71882FG Hardware Monitoring Driver");
  1517. MODULE_AUTHOR("Hans Edgington, Hans de Goede (hdegoede@redhat.com)");
  1518. MODULE_LICENSE("GPL");
  1519. module_init(f71882fg_init);
  1520. module_exit(f71882fg_exit);