f71882fg.c 54 KB

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