f71882fg.c 49 KB

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