f71805f.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /*
  2. * f71805f.c - driver for the Fintek F71805F/FG and F71872F/FG Super-I/O
  3. * chips integrated hardware monitoring features
  4. * Copyright (C) 2005-2006 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * The F71805F/FG is a LPC Super-I/O chip made by Fintek. It integrates
  7. * complete hardware monitoring features: voltage, fan and temperature
  8. * sensors, and manual and automatic fan speed control.
  9. *
  10. * The F71872F/FG is almost the same, with two more voltages monitored,
  11. * and 6 VID inputs.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. #include <linux/sysfs.h>
  37. #include <asm/io.h>
  38. static struct platform_device *pdev;
  39. #define DRVNAME "f71805f"
  40. enum kinds { f71805f, f71872f };
  41. /*
  42. * Super-I/O constants and functions
  43. */
  44. #define F71805F_LD_HWM 0x04
  45. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  46. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  47. #define SIO_REG_DEVREV 0x22 /* Device revision */
  48. #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
  49. #define SIO_REG_FNSEL1 0x29 /* Multi Function Select 1 (F71872F) */
  50. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  51. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  52. #define SIO_FINTEK_ID 0x1934
  53. #define SIO_F71805F_ID 0x0406
  54. #define SIO_F71872F_ID 0x0341
  55. static inline int
  56. superio_inb(int base, int reg)
  57. {
  58. outb(reg, base);
  59. return inb(base + 1);
  60. }
  61. static int
  62. superio_inw(int base, int reg)
  63. {
  64. int val;
  65. outb(reg++, base);
  66. val = inb(base + 1) << 8;
  67. outb(reg, base);
  68. val |= inb(base + 1);
  69. return val;
  70. }
  71. static inline void
  72. superio_select(int base, int ld)
  73. {
  74. outb(SIO_REG_LDSEL, base);
  75. outb(ld, base + 1);
  76. }
  77. static inline void
  78. superio_enter(int base)
  79. {
  80. outb(0x87, base);
  81. outb(0x87, base);
  82. }
  83. static inline void
  84. superio_exit(int base)
  85. {
  86. outb(0xaa, base);
  87. }
  88. /*
  89. * ISA constants
  90. */
  91. #define REGION_LENGTH 8
  92. #define ADDR_REG_OFFSET 5
  93. #define DATA_REG_OFFSET 6
  94. /*
  95. * Registers
  96. */
  97. /* in nr from 0 to 10 (8-bit values) */
  98. #define F71805F_REG_IN(nr) (0x10 + (nr))
  99. #define F71805F_REG_IN_HIGH(nr) ((nr) < 10 ? 0x40 + 2 * (nr) : 0x2E)
  100. #define F71805F_REG_IN_LOW(nr) ((nr) < 10 ? 0x41 + 2 * (nr) : 0x2F)
  101. /* fan nr from 0 to 2 (12-bit values, two registers) */
  102. #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
  103. #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
  104. #define F71805F_REG_FAN_TARGET(nr) (0x69 + 16 * (nr))
  105. #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
  106. #define F71805F_REG_PWM_FREQ(nr) (0x63 + 16 * (nr))
  107. #define F71805F_REG_PWM_DUTY(nr) (0x6B + 16 * (nr))
  108. /* temp nr from 0 to 2 (8-bit values) */
  109. #define F71805F_REG_TEMP(nr) (0x1B + (nr))
  110. #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
  111. #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
  112. #define F71805F_REG_TEMP_MODE 0x01
  113. #define F71805F_REG_START 0x00
  114. /* status nr from 0 to 2 */
  115. #define F71805F_REG_STATUS(nr) (0x36 + (nr))
  116. /* individual register bits */
  117. #define FAN_CTRL_DC_MODE 0x10
  118. #define FAN_CTRL_LATCH_FULL 0x08
  119. #define FAN_CTRL_MODE_MASK 0x03
  120. #define FAN_CTRL_MODE_SPEED 0x00
  121. #define FAN_CTRL_MODE_TEMPERATURE 0x01
  122. #define FAN_CTRL_MODE_MANUAL 0x02
  123. /*
  124. * Data structures and manipulation thereof
  125. */
  126. struct f71805f_data {
  127. unsigned short addr;
  128. const char *name;
  129. struct mutex lock;
  130. struct class_device *class_dev;
  131. struct mutex update_lock;
  132. char valid; /* !=0 if following fields are valid */
  133. unsigned long last_updated; /* In jiffies */
  134. unsigned long last_limits; /* In jiffies */
  135. /* Register values */
  136. u8 in[11];
  137. u8 in_high[11];
  138. u8 in_low[11];
  139. u16 has_in;
  140. u16 fan[3];
  141. u16 fan_low[3];
  142. u16 fan_target[3];
  143. u8 fan_ctrl[3];
  144. u8 pwm[3];
  145. u8 pwm_freq[3];
  146. u8 temp[3];
  147. u8 temp_high[3];
  148. u8 temp_hyst[3];
  149. u8 temp_mode;
  150. unsigned long alarms;
  151. };
  152. struct f71805f_sio_data {
  153. enum kinds kind;
  154. u8 fnsel1;
  155. };
  156. static inline long in_from_reg(u8 reg)
  157. {
  158. return (reg * 8);
  159. }
  160. /* The 2 least significant bits are not used */
  161. static inline u8 in_to_reg(long val)
  162. {
  163. if (val <= 0)
  164. return 0;
  165. if (val >= 2016)
  166. return 0xfc;
  167. return (((val + 16) / 32) << 2);
  168. }
  169. /* in0 is downscaled by a factor 2 internally */
  170. static inline long in0_from_reg(u8 reg)
  171. {
  172. return (reg * 16);
  173. }
  174. static inline u8 in0_to_reg(long val)
  175. {
  176. if (val <= 0)
  177. return 0;
  178. if (val >= 4032)
  179. return 0xfc;
  180. return (((val + 32) / 64) << 2);
  181. }
  182. /* The 4 most significant bits are not used */
  183. static inline long fan_from_reg(u16 reg)
  184. {
  185. reg &= 0xfff;
  186. if (!reg || reg == 0xfff)
  187. return 0;
  188. return (1500000 / reg);
  189. }
  190. static inline u16 fan_to_reg(long rpm)
  191. {
  192. /* If the low limit is set below what the chip can measure,
  193. store the largest possible 12-bit value in the registers,
  194. so that no alarm will ever trigger. */
  195. if (rpm < 367)
  196. return 0xfff;
  197. return (1500000 / rpm);
  198. }
  199. static inline unsigned long pwm_freq_from_reg(u8 reg)
  200. {
  201. unsigned long clock = (reg & 0x80) ? 48000000UL : 1000000UL;
  202. reg &= 0x7f;
  203. if (reg == 0)
  204. reg++;
  205. return clock / (reg << 8);
  206. }
  207. static inline u8 pwm_freq_to_reg(unsigned long val)
  208. {
  209. if (val >= 187500) /* The highest we can do */
  210. return 0x80;
  211. if (val >= 1475) /* Use 48 MHz clock */
  212. return 0x80 | (48000000UL / (val << 8));
  213. if (val < 31) /* The lowest we can do */
  214. return 0x7f;
  215. else /* Use 1 MHz clock */
  216. return 1000000UL / (val << 8);
  217. }
  218. static inline int pwm_mode_from_reg(u8 reg)
  219. {
  220. return !(reg & FAN_CTRL_DC_MODE);
  221. }
  222. static inline long temp_from_reg(u8 reg)
  223. {
  224. return (reg * 1000);
  225. }
  226. static inline u8 temp_to_reg(long val)
  227. {
  228. if (val < 0)
  229. val = 0;
  230. else if (val > 1000 * 0xff)
  231. val = 0xff;
  232. return ((val + 500) / 1000);
  233. }
  234. /*
  235. * Device I/O access
  236. */
  237. static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
  238. {
  239. u8 val;
  240. mutex_lock(&data->lock);
  241. outb(reg, data->addr + ADDR_REG_OFFSET);
  242. val = inb(data->addr + DATA_REG_OFFSET);
  243. mutex_unlock(&data->lock);
  244. return val;
  245. }
  246. static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
  247. {
  248. mutex_lock(&data->lock);
  249. outb(reg, data->addr + ADDR_REG_OFFSET);
  250. outb(val, data->addr + DATA_REG_OFFSET);
  251. mutex_unlock(&data->lock);
  252. }
  253. /* It is important to read the MSB first, because doing so latches the
  254. value of the LSB, so we are sure both bytes belong to the same value. */
  255. static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
  256. {
  257. u16 val;
  258. mutex_lock(&data->lock);
  259. outb(reg, data->addr + ADDR_REG_OFFSET);
  260. val = inb(data->addr + DATA_REG_OFFSET) << 8;
  261. outb(++reg, data->addr + ADDR_REG_OFFSET);
  262. val |= inb(data->addr + DATA_REG_OFFSET);
  263. mutex_unlock(&data->lock);
  264. return val;
  265. }
  266. static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
  267. {
  268. mutex_lock(&data->lock);
  269. outb(reg, data->addr + ADDR_REG_OFFSET);
  270. outb(val >> 8, data->addr + DATA_REG_OFFSET);
  271. outb(++reg, data->addr + ADDR_REG_OFFSET);
  272. outb(val & 0xff, data->addr + DATA_REG_OFFSET);
  273. mutex_unlock(&data->lock);
  274. }
  275. static struct f71805f_data *f71805f_update_device(struct device *dev)
  276. {
  277. struct f71805f_data *data = dev_get_drvdata(dev);
  278. int nr;
  279. mutex_lock(&data->update_lock);
  280. /* Limit registers cache is refreshed after 60 seconds */
  281. if (time_after(jiffies, data->last_updated + 60 * HZ)
  282. || !data->valid) {
  283. for (nr = 0; nr < 11; nr++) {
  284. if (!(data->has_in & (1 << nr)))
  285. continue;
  286. data->in_high[nr] = f71805f_read8(data,
  287. F71805F_REG_IN_HIGH(nr));
  288. data->in_low[nr] = f71805f_read8(data,
  289. F71805F_REG_IN_LOW(nr));
  290. }
  291. for (nr = 0; nr < 3; nr++) {
  292. data->fan_low[nr] = f71805f_read16(data,
  293. F71805F_REG_FAN_LOW(nr));
  294. data->fan_target[nr] = f71805f_read16(data,
  295. F71805F_REG_FAN_TARGET(nr));
  296. data->pwm_freq[nr] = f71805f_read8(data,
  297. F71805F_REG_PWM_FREQ(nr));
  298. }
  299. for (nr = 0; nr < 3; nr++) {
  300. data->temp_high[nr] = f71805f_read8(data,
  301. F71805F_REG_TEMP_HIGH(nr));
  302. data->temp_hyst[nr] = f71805f_read8(data,
  303. F71805F_REG_TEMP_HYST(nr));
  304. }
  305. data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
  306. data->last_limits = jiffies;
  307. }
  308. /* Measurement registers cache is refreshed after 1 second */
  309. if (time_after(jiffies, data->last_updated + HZ)
  310. || !data->valid) {
  311. for (nr = 0; nr < 11; nr++) {
  312. if (!(data->has_in & (1 << nr)))
  313. continue;
  314. data->in[nr] = f71805f_read8(data,
  315. F71805F_REG_IN(nr));
  316. }
  317. for (nr = 0; nr < 3; nr++) {
  318. data->fan[nr] = f71805f_read16(data,
  319. F71805F_REG_FAN(nr));
  320. data->fan_ctrl[nr] = f71805f_read8(data,
  321. F71805F_REG_FAN_CTRL(nr));
  322. data->pwm[nr] = f71805f_read8(data,
  323. F71805F_REG_PWM_DUTY(nr));
  324. }
  325. for (nr = 0; nr < 3; nr++) {
  326. data->temp[nr] = f71805f_read8(data,
  327. F71805F_REG_TEMP(nr));
  328. }
  329. data->alarms = f71805f_read8(data, F71805F_REG_STATUS(0))
  330. + (f71805f_read8(data, F71805F_REG_STATUS(1)) << 8)
  331. + (f71805f_read8(data, F71805F_REG_STATUS(2)) << 16);
  332. data->last_updated = jiffies;
  333. data->valid = 1;
  334. }
  335. mutex_unlock(&data->update_lock);
  336. return data;
  337. }
  338. /*
  339. * Sysfs interface
  340. */
  341. static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
  342. char *buf)
  343. {
  344. struct f71805f_data *data = f71805f_update_device(dev);
  345. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  346. int nr = attr->index;
  347. return sprintf(buf, "%ld\n", in0_from_reg(data->in[nr]));
  348. }
  349. static ssize_t show_in0_max(struct device *dev, struct device_attribute
  350. *devattr, char *buf)
  351. {
  352. struct f71805f_data *data = f71805f_update_device(dev);
  353. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  354. int nr = attr->index;
  355. return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[nr]));
  356. }
  357. static ssize_t show_in0_min(struct device *dev, struct device_attribute
  358. *devattr, char *buf)
  359. {
  360. struct f71805f_data *data = f71805f_update_device(dev);
  361. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  362. int nr = attr->index;
  363. return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[nr]));
  364. }
  365. static ssize_t set_in0_max(struct device *dev, struct device_attribute
  366. *devattr, const char *buf, size_t count)
  367. {
  368. struct f71805f_data *data = dev_get_drvdata(dev);
  369. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  370. int nr = attr->index;
  371. long val = simple_strtol(buf, NULL, 10);
  372. mutex_lock(&data->update_lock);
  373. data->in_high[nr] = in0_to_reg(val);
  374. f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
  375. mutex_unlock(&data->update_lock);
  376. return count;
  377. }
  378. static ssize_t set_in0_min(struct device *dev, struct device_attribute
  379. *devattr, const char *buf, size_t count)
  380. {
  381. struct f71805f_data *data = dev_get_drvdata(dev);
  382. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  383. int nr = attr->index;
  384. long val = simple_strtol(buf, NULL, 10);
  385. mutex_lock(&data->update_lock);
  386. data->in_low[nr] = in0_to_reg(val);
  387. f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
  388. mutex_unlock(&data->update_lock);
  389. return count;
  390. }
  391. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  392. char *buf)
  393. {
  394. struct f71805f_data *data = f71805f_update_device(dev);
  395. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  396. int nr = attr->index;
  397. return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
  398. }
  399. static ssize_t show_in_max(struct device *dev, struct device_attribute
  400. *devattr, char *buf)
  401. {
  402. struct f71805f_data *data = f71805f_update_device(dev);
  403. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  404. int nr = attr->index;
  405. return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
  406. }
  407. static ssize_t show_in_min(struct device *dev, struct device_attribute
  408. *devattr, char *buf)
  409. {
  410. struct f71805f_data *data = f71805f_update_device(dev);
  411. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  412. int nr = attr->index;
  413. return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
  414. }
  415. static ssize_t set_in_max(struct device *dev, struct device_attribute
  416. *devattr, const char *buf, size_t count)
  417. {
  418. struct f71805f_data *data = dev_get_drvdata(dev);
  419. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  420. int nr = attr->index;
  421. long val = simple_strtol(buf, NULL, 10);
  422. mutex_lock(&data->update_lock);
  423. data->in_high[nr] = in_to_reg(val);
  424. f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
  425. mutex_unlock(&data->update_lock);
  426. return count;
  427. }
  428. static ssize_t set_in_min(struct device *dev, struct device_attribute
  429. *devattr, const char *buf, size_t count)
  430. {
  431. struct f71805f_data *data = dev_get_drvdata(dev);
  432. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  433. int nr = attr->index;
  434. long val = simple_strtol(buf, NULL, 10);
  435. mutex_lock(&data->update_lock);
  436. data->in_low[nr] = in_to_reg(val);
  437. f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
  438. mutex_unlock(&data->update_lock);
  439. return count;
  440. }
  441. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  442. char *buf)
  443. {
  444. struct f71805f_data *data = f71805f_update_device(dev);
  445. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  446. int nr = attr->index;
  447. return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
  448. }
  449. static ssize_t show_fan_min(struct device *dev, struct device_attribute
  450. *devattr, char *buf)
  451. {
  452. struct f71805f_data *data = f71805f_update_device(dev);
  453. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  454. int nr = attr->index;
  455. return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
  456. }
  457. static ssize_t show_fan_target(struct device *dev, struct device_attribute
  458. *devattr, char *buf)
  459. {
  460. struct f71805f_data *data = f71805f_update_device(dev);
  461. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  462. int nr = attr->index;
  463. return sprintf(buf, "%ld\n", fan_from_reg(data->fan_target[nr]));
  464. }
  465. static ssize_t set_fan_min(struct device *dev, struct device_attribute
  466. *devattr, const char *buf, size_t count)
  467. {
  468. struct f71805f_data *data = dev_get_drvdata(dev);
  469. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  470. int nr = attr->index;
  471. long val = simple_strtol(buf, NULL, 10);
  472. mutex_lock(&data->update_lock);
  473. data->fan_low[nr] = fan_to_reg(val);
  474. f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
  475. mutex_unlock(&data->update_lock);
  476. return count;
  477. }
  478. static ssize_t set_fan_target(struct device *dev, struct device_attribute
  479. *devattr, const char *buf, size_t count)
  480. {
  481. struct f71805f_data *data = dev_get_drvdata(dev);
  482. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  483. int nr = attr->index;
  484. long val = simple_strtol(buf, NULL, 10);
  485. mutex_lock(&data->update_lock);
  486. data->fan_target[nr] = fan_to_reg(val);
  487. f71805f_write16(data, F71805F_REG_FAN_TARGET(nr),
  488. data->fan_target[nr]);
  489. mutex_unlock(&data->update_lock);
  490. return count;
  491. }
  492. static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
  493. char *buf)
  494. {
  495. struct f71805f_data *data = f71805f_update_device(dev);
  496. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  497. int nr = attr->index;
  498. return sprintf(buf, "%d\n", (int)data->pwm[nr]);
  499. }
  500. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
  501. *devattr, char *buf)
  502. {
  503. struct f71805f_data *data = f71805f_update_device(dev);
  504. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  505. int nr = attr->index;
  506. int mode;
  507. switch (data->fan_ctrl[nr] & FAN_CTRL_MODE_MASK) {
  508. case FAN_CTRL_MODE_SPEED:
  509. mode = 3;
  510. break;
  511. case FAN_CTRL_MODE_TEMPERATURE:
  512. mode = 2;
  513. break;
  514. default: /* MANUAL */
  515. mode = 1;
  516. }
  517. return sprintf(buf, "%d\n", mode);
  518. }
  519. static ssize_t show_pwm_freq(struct device *dev, struct device_attribute
  520. *devattr, char *buf)
  521. {
  522. struct f71805f_data *data = f71805f_update_device(dev);
  523. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  524. int nr = attr->index;
  525. return sprintf(buf, "%lu\n", pwm_freq_from_reg(data->pwm_freq[nr]));
  526. }
  527. static ssize_t show_pwm_mode(struct device *dev, struct device_attribute
  528. *devattr, char *buf)
  529. {
  530. struct f71805f_data *data = f71805f_update_device(dev);
  531. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  532. int nr = attr->index;
  533. return sprintf(buf, "%d\n", pwm_mode_from_reg(data->fan_ctrl[nr]));
  534. }
  535. static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
  536. const char *buf, size_t count)
  537. {
  538. struct f71805f_data *data = dev_get_drvdata(dev);
  539. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  540. int nr = attr->index;
  541. unsigned long val = simple_strtoul(buf, NULL, 10);
  542. if (val > 255)
  543. return -EINVAL;
  544. mutex_lock(&data->update_lock);
  545. data->pwm[nr] = val;
  546. f71805f_write8(data, F71805F_REG_PWM_DUTY(nr), data->pwm[nr]);
  547. mutex_unlock(&data->update_lock);
  548. return count;
  549. }
  550. static struct attribute *f71805f_attr_pwm[];
  551. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
  552. *devattr, const char *buf, size_t count)
  553. {
  554. struct f71805f_data *data = dev_get_drvdata(dev);
  555. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  556. int nr = attr->index;
  557. unsigned long val = simple_strtoul(buf, NULL, 10);
  558. u8 reg;
  559. if (val < 1 || val > 3)
  560. return -EINVAL;
  561. if (val > 1) { /* Automatic mode, user can't set PWM value */
  562. if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
  563. S_IRUGO))
  564. dev_dbg(dev, "chmod -w pwm%d failed\n", nr + 1);
  565. }
  566. mutex_lock(&data->update_lock);
  567. reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(nr))
  568. & ~FAN_CTRL_MODE_MASK;
  569. switch (val) {
  570. case 1:
  571. reg |= FAN_CTRL_MODE_MANUAL;
  572. break;
  573. case 2:
  574. reg |= FAN_CTRL_MODE_TEMPERATURE;
  575. break;
  576. case 3:
  577. reg |= FAN_CTRL_MODE_SPEED;
  578. break;
  579. }
  580. data->fan_ctrl[nr] = reg;
  581. f71805f_write8(data, F71805F_REG_FAN_CTRL(nr), reg);
  582. mutex_unlock(&data->update_lock);
  583. if (val == 1) { /* Manual mode, user can set PWM value */
  584. if (sysfs_chmod_file(&dev->kobj, f71805f_attr_pwm[nr],
  585. S_IRUGO | S_IWUSR))
  586. dev_dbg(dev, "chmod +w pwm%d failed\n", nr + 1);
  587. }
  588. return count;
  589. }
  590. static ssize_t set_pwm_freq(struct device *dev, struct device_attribute
  591. *devattr, const char *buf, size_t count)
  592. {
  593. struct f71805f_data *data = dev_get_drvdata(dev);
  594. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  595. int nr = attr->index;
  596. unsigned long val = simple_strtoul(buf, NULL, 10);
  597. mutex_lock(&data->update_lock);
  598. data->pwm_freq[nr] = pwm_freq_to_reg(val);
  599. f71805f_write8(data, F71805F_REG_PWM_FREQ(nr), data->pwm_freq[nr]);
  600. mutex_unlock(&data->update_lock);
  601. return count;
  602. }
  603. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  604. char *buf)
  605. {
  606. struct f71805f_data *data = f71805f_update_device(dev);
  607. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  608. int nr = attr->index;
  609. return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
  610. }
  611. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  612. *devattr, char *buf)
  613. {
  614. struct f71805f_data *data = f71805f_update_device(dev);
  615. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  616. int nr = attr->index;
  617. return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
  618. }
  619. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
  620. *devattr, char *buf)
  621. {
  622. struct f71805f_data *data = f71805f_update_device(dev);
  623. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  624. int nr = attr->index;
  625. return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
  626. }
  627. static ssize_t show_temp_type(struct device *dev, struct device_attribute
  628. *devattr, char *buf)
  629. {
  630. struct f71805f_data *data = f71805f_update_device(dev);
  631. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  632. int nr = attr->index;
  633. /* 3 is diode, 4 is thermistor */
  634. return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
  635. }
  636. static ssize_t set_temp_max(struct device *dev, struct device_attribute
  637. *devattr, const char *buf, size_t count)
  638. {
  639. struct f71805f_data *data = dev_get_drvdata(dev);
  640. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  641. int nr = attr->index;
  642. long val = simple_strtol(buf, NULL, 10);
  643. mutex_lock(&data->update_lock);
  644. data->temp_high[nr] = temp_to_reg(val);
  645. f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  646. mutex_unlock(&data->update_lock);
  647. return count;
  648. }
  649. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
  650. *devattr, const char *buf, size_t count)
  651. {
  652. struct f71805f_data *data = dev_get_drvdata(dev);
  653. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  654. int nr = attr->index;
  655. long val = simple_strtol(buf, NULL, 10);
  656. mutex_lock(&data->update_lock);
  657. data->temp_hyst[nr] = temp_to_reg(val);
  658. f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
  659. mutex_unlock(&data->update_lock);
  660. return count;
  661. }
  662. static ssize_t show_alarms_in(struct device *dev, struct device_attribute
  663. *devattr, char *buf)
  664. {
  665. struct f71805f_data *data = f71805f_update_device(dev);
  666. return sprintf(buf, "%lu\n", data->alarms & 0x7ff);
  667. }
  668. static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
  669. *devattr, char *buf)
  670. {
  671. struct f71805f_data *data = f71805f_update_device(dev);
  672. return sprintf(buf, "%lu\n", (data->alarms >> 16) & 0x07);
  673. }
  674. static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
  675. *devattr, char *buf)
  676. {
  677. struct f71805f_data *data = f71805f_update_device(dev);
  678. return sprintf(buf, "%lu\n", (data->alarms >> 11) & 0x07);
  679. }
  680. static ssize_t show_alarm(struct device *dev, struct device_attribute
  681. *devattr, char *buf)
  682. {
  683. struct f71805f_data *data = f71805f_update_device(dev);
  684. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  685. int bitnr = attr->index;
  686. return sprintf(buf, "%lu\n", (data->alarms >> bitnr) & 1);
  687. }
  688. static ssize_t show_name(struct device *dev, struct device_attribute
  689. *devattr, char *buf)
  690. {
  691. struct f71805f_data *data = dev_get_drvdata(dev);
  692. return sprintf(buf, "%s\n", data->name);
  693. }
  694. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL, 0);
  695. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR,
  696. show_in0_max, set_in0_max, 0);
  697. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR,
  698. show_in0_min, set_in0_min, 0);
  699. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_in, NULL, 1);
  700. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR,
  701. show_in_max, set_in_max, 1);
  702. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR,
  703. show_in_min, set_in_min, 1);
  704. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 2);
  705. static SENSOR_DEVICE_ATTR(in2_max, S_IRUGO | S_IWUSR,
  706. show_in_max, set_in_max, 2);
  707. static SENSOR_DEVICE_ATTR(in2_min, S_IRUGO | S_IWUSR,
  708. show_in_min, set_in_min, 2);
  709. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 3);
  710. static SENSOR_DEVICE_ATTR(in3_max, S_IRUGO | S_IWUSR,
  711. show_in_max, set_in_max, 3);
  712. static SENSOR_DEVICE_ATTR(in3_min, S_IRUGO | S_IWUSR,
  713. show_in_min, set_in_min, 3);
  714. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 4);
  715. static SENSOR_DEVICE_ATTR(in4_max, S_IRUGO | S_IWUSR,
  716. show_in_max, set_in_max, 4);
  717. static SENSOR_DEVICE_ATTR(in4_min, S_IRUGO | S_IWUSR,
  718. show_in_min, set_in_min, 4);
  719. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 5);
  720. static SENSOR_DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR,
  721. show_in_max, set_in_max, 5);
  722. static SENSOR_DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR,
  723. show_in_min, set_in_min, 5);
  724. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 6);
  725. static SENSOR_DEVICE_ATTR(in6_max, S_IRUGO | S_IWUSR,
  726. show_in_max, set_in_max, 6);
  727. static SENSOR_DEVICE_ATTR(in6_min, S_IRUGO | S_IWUSR,
  728. show_in_min, set_in_min, 6);
  729. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 7);
  730. static SENSOR_DEVICE_ATTR(in7_max, S_IRUGO | S_IWUSR,
  731. show_in_max, set_in_max, 7);
  732. static SENSOR_DEVICE_ATTR(in7_min, S_IRUGO | S_IWUSR,
  733. show_in_min, set_in_min, 7);
  734. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 8);
  735. static SENSOR_DEVICE_ATTR(in8_max, S_IRUGO | S_IWUSR,
  736. show_in_max, set_in_max, 8);
  737. static SENSOR_DEVICE_ATTR(in8_min, S_IRUGO | S_IWUSR,
  738. show_in_min, set_in_min, 8);
  739. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in0, NULL, 9);
  740. static SENSOR_DEVICE_ATTR(in9_max, S_IRUGO | S_IWUSR,
  741. show_in0_max, set_in0_max, 9);
  742. static SENSOR_DEVICE_ATTR(in9_min, S_IRUGO | S_IWUSR,
  743. show_in0_min, set_in0_min, 9);
  744. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in0, NULL, 10);
  745. static SENSOR_DEVICE_ATTR(in10_max, S_IRUGO | S_IWUSR,
  746. show_in0_max, set_in0_max, 10);
  747. static SENSOR_DEVICE_ATTR(in10_min, S_IRUGO | S_IWUSR,
  748. show_in0_min, set_in0_min, 10);
  749. static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
  750. static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO | S_IWUSR,
  751. show_fan_min, set_fan_min, 0);
  752. static SENSOR_DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR,
  753. show_fan_target, set_fan_target, 0);
  754. static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
  755. static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO | S_IWUSR,
  756. show_fan_min, set_fan_min, 1);
  757. static SENSOR_DEVICE_ATTR(fan2_target, S_IRUGO | S_IWUSR,
  758. show_fan_target, set_fan_target, 1);
  759. static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
  760. static SENSOR_DEVICE_ATTR(fan3_min, S_IRUGO | S_IWUSR,
  761. show_fan_min, set_fan_min, 2);
  762. static SENSOR_DEVICE_ATTR(fan3_target, S_IRUGO | S_IWUSR,
  763. show_fan_target, set_fan_target, 2);
  764. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
  765. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  766. show_temp_max, set_temp_max, 0);
  767. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  768. show_temp_hyst, set_temp_hyst, 0);
  769. static SENSOR_DEVICE_ATTR(temp1_type, S_IRUGO, show_temp_type, NULL, 0);
  770. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
  771. static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR,
  772. show_temp_max, set_temp_max, 1);
  773. static SENSOR_DEVICE_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR,
  774. show_temp_hyst, set_temp_hyst, 1);
  775. static SENSOR_DEVICE_ATTR(temp2_type, S_IRUGO, show_temp_type, NULL, 1);
  776. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
  777. static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR,
  778. show_temp_max, set_temp_max, 2);
  779. static SENSOR_DEVICE_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR,
  780. show_temp_hyst, set_temp_hyst, 2);
  781. static SENSOR_DEVICE_ATTR(temp3_type, S_IRUGO, show_temp_type, NULL, 2);
  782. /* pwm (value) files are created read-only, write permission is
  783. then added or removed dynamically as needed */
  784. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO, show_pwm, set_pwm, 0);
  785. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  786. show_pwm_enable, set_pwm_enable, 0);
  787. static SENSOR_DEVICE_ATTR(pwm1_freq, S_IRUGO | S_IWUSR,
  788. show_pwm_freq, set_pwm_freq, 0);
  789. static SENSOR_DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL, 0);
  790. static SENSOR_DEVICE_ATTR(pwm2, S_IRUGO, show_pwm, set_pwm, 1);
  791. static SENSOR_DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR,
  792. show_pwm_enable, set_pwm_enable, 1);
  793. static SENSOR_DEVICE_ATTR(pwm2_freq, S_IRUGO | S_IWUSR,
  794. show_pwm_freq, set_pwm_freq, 1);
  795. static SENSOR_DEVICE_ATTR(pwm2_mode, S_IRUGO, show_pwm_mode, NULL, 1);
  796. static SENSOR_DEVICE_ATTR(pwm3, S_IRUGO, show_pwm, set_pwm, 2);
  797. static SENSOR_DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR,
  798. show_pwm_enable, set_pwm_enable, 2);
  799. static SENSOR_DEVICE_ATTR(pwm3_freq, S_IRUGO | S_IWUSR,
  800. show_pwm_freq, set_pwm_freq, 2);
  801. static SENSOR_DEVICE_ATTR(pwm3_mode, S_IRUGO, show_pwm_mode, NULL, 2);
  802. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  803. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  804. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  805. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  806. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 4);
  807. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 5);
  808. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  809. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
  810. static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 8);
  811. static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 9);
  812. static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 10);
  813. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 11);
  814. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 12);
  815. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13);
  816. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
  817. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
  818. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
  819. static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
  820. static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
  821. static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
  822. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  823. static struct attribute *f71805f_attributes[] = {
  824. &sensor_dev_attr_in0_input.dev_attr.attr,
  825. &sensor_dev_attr_in0_max.dev_attr.attr,
  826. &sensor_dev_attr_in0_min.dev_attr.attr,
  827. &sensor_dev_attr_in1_input.dev_attr.attr,
  828. &sensor_dev_attr_in1_max.dev_attr.attr,
  829. &sensor_dev_attr_in1_min.dev_attr.attr,
  830. &sensor_dev_attr_in2_input.dev_attr.attr,
  831. &sensor_dev_attr_in2_max.dev_attr.attr,
  832. &sensor_dev_attr_in2_min.dev_attr.attr,
  833. &sensor_dev_attr_in3_input.dev_attr.attr,
  834. &sensor_dev_attr_in3_max.dev_attr.attr,
  835. &sensor_dev_attr_in3_min.dev_attr.attr,
  836. &sensor_dev_attr_in5_input.dev_attr.attr,
  837. &sensor_dev_attr_in5_max.dev_attr.attr,
  838. &sensor_dev_attr_in5_min.dev_attr.attr,
  839. &sensor_dev_attr_in6_input.dev_attr.attr,
  840. &sensor_dev_attr_in6_max.dev_attr.attr,
  841. &sensor_dev_attr_in6_min.dev_attr.attr,
  842. &sensor_dev_attr_in7_input.dev_attr.attr,
  843. &sensor_dev_attr_in7_max.dev_attr.attr,
  844. &sensor_dev_attr_in7_min.dev_attr.attr,
  845. &sensor_dev_attr_fan1_input.dev_attr.attr,
  846. &sensor_dev_attr_fan1_min.dev_attr.attr,
  847. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  848. &sensor_dev_attr_fan1_target.dev_attr.attr,
  849. &sensor_dev_attr_fan2_input.dev_attr.attr,
  850. &sensor_dev_attr_fan2_min.dev_attr.attr,
  851. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  852. &sensor_dev_attr_fan2_target.dev_attr.attr,
  853. &sensor_dev_attr_fan3_input.dev_attr.attr,
  854. &sensor_dev_attr_fan3_min.dev_attr.attr,
  855. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  856. &sensor_dev_attr_fan3_target.dev_attr.attr,
  857. &sensor_dev_attr_pwm1.dev_attr.attr,
  858. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  859. &sensor_dev_attr_pwm1_mode.dev_attr.attr,
  860. &sensor_dev_attr_pwm2.dev_attr.attr,
  861. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  862. &sensor_dev_attr_pwm2_mode.dev_attr.attr,
  863. &sensor_dev_attr_pwm3.dev_attr.attr,
  864. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  865. &sensor_dev_attr_pwm3_mode.dev_attr.attr,
  866. &sensor_dev_attr_temp1_input.dev_attr.attr,
  867. &sensor_dev_attr_temp1_max.dev_attr.attr,
  868. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  869. &sensor_dev_attr_temp1_type.dev_attr.attr,
  870. &sensor_dev_attr_temp2_input.dev_attr.attr,
  871. &sensor_dev_attr_temp2_max.dev_attr.attr,
  872. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  873. &sensor_dev_attr_temp2_type.dev_attr.attr,
  874. &sensor_dev_attr_temp3_input.dev_attr.attr,
  875. &sensor_dev_attr_temp3_max.dev_attr.attr,
  876. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  877. &sensor_dev_attr_temp3_type.dev_attr.attr,
  878. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  879. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  880. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  881. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  882. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  883. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  884. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  885. &dev_attr_alarms_in.attr,
  886. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  887. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  888. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  889. &dev_attr_alarms_temp.attr,
  890. &dev_attr_alarms_fan.attr,
  891. &dev_attr_name.attr,
  892. NULL
  893. };
  894. static const struct attribute_group f71805f_group = {
  895. .attrs = f71805f_attributes,
  896. };
  897. static struct attribute *f71805f_attributes_optin[4][5] = {
  898. {
  899. &sensor_dev_attr_in4_input.dev_attr.attr,
  900. &sensor_dev_attr_in4_max.dev_attr.attr,
  901. &sensor_dev_attr_in4_min.dev_attr.attr,
  902. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  903. NULL
  904. }, {
  905. &sensor_dev_attr_in8_input.dev_attr.attr,
  906. &sensor_dev_attr_in8_max.dev_attr.attr,
  907. &sensor_dev_attr_in8_min.dev_attr.attr,
  908. &sensor_dev_attr_in8_alarm.dev_attr.attr,
  909. NULL
  910. }, {
  911. &sensor_dev_attr_in9_input.dev_attr.attr,
  912. &sensor_dev_attr_in9_max.dev_attr.attr,
  913. &sensor_dev_attr_in9_min.dev_attr.attr,
  914. &sensor_dev_attr_in9_alarm.dev_attr.attr,
  915. NULL
  916. }, {
  917. &sensor_dev_attr_in10_input.dev_attr.attr,
  918. &sensor_dev_attr_in10_max.dev_attr.attr,
  919. &sensor_dev_attr_in10_min.dev_attr.attr,
  920. &sensor_dev_attr_in10_alarm.dev_attr.attr,
  921. NULL
  922. }
  923. };
  924. static const struct attribute_group f71805f_group_optin[4] = {
  925. { .attrs = f71805f_attributes_optin[0] },
  926. { .attrs = f71805f_attributes_optin[1] },
  927. { .attrs = f71805f_attributes_optin[2] },
  928. { .attrs = f71805f_attributes_optin[3] },
  929. };
  930. /* We don't include pwm_freq files in the arrays above, because they must be
  931. created conditionally (only if pwm_mode is 1 == PWM) */
  932. static struct attribute *f71805f_attributes_pwm_freq[] = {
  933. &sensor_dev_attr_pwm1_freq.dev_attr.attr,
  934. &sensor_dev_attr_pwm2_freq.dev_attr.attr,
  935. &sensor_dev_attr_pwm3_freq.dev_attr.attr,
  936. NULL
  937. };
  938. static const struct attribute_group f71805f_group_pwm_freq = {
  939. .attrs = f71805f_attributes_pwm_freq,
  940. };
  941. /* We also need an indexed access to pwmN files to toggle writability */
  942. static struct attribute *f71805f_attr_pwm[] = {
  943. &sensor_dev_attr_pwm1.dev_attr.attr,
  944. &sensor_dev_attr_pwm2.dev_attr.attr,
  945. &sensor_dev_attr_pwm3.dev_attr.attr,
  946. };
  947. /*
  948. * Device registration and initialization
  949. */
  950. static void __devinit f71805f_init_device(struct f71805f_data *data)
  951. {
  952. u8 reg;
  953. int i;
  954. reg = f71805f_read8(data, F71805F_REG_START);
  955. if ((reg & 0x41) != 0x01) {
  956. printk(KERN_DEBUG DRVNAME ": Starting monitoring "
  957. "operations\n");
  958. f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
  959. }
  960. /* Fan monitoring can be disabled. If it is, we won't be polling
  961. the register values, and won't create the related sysfs files. */
  962. for (i = 0; i < 3; i++) {
  963. data->fan_ctrl[i] = f71805f_read8(data,
  964. F71805F_REG_FAN_CTRL(i));
  965. /* Clear latch full bit, else "speed mode" fan speed control
  966. doesn't work */
  967. if (data->fan_ctrl[i] & FAN_CTRL_LATCH_FULL) {
  968. data->fan_ctrl[i] &= ~FAN_CTRL_LATCH_FULL;
  969. f71805f_write8(data, F71805F_REG_FAN_CTRL(i),
  970. data->fan_ctrl[i]);
  971. }
  972. }
  973. }
  974. static int __devinit f71805f_probe(struct platform_device *pdev)
  975. {
  976. struct f71805f_sio_data *sio_data = pdev->dev.platform_data;
  977. struct f71805f_data *data;
  978. struct resource *res;
  979. int i, err;
  980. static const char *names[] = {
  981. "f71805f",
  982. "f71872f",
  983. };
  984. if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
  985. err = -ENOMEM;
  986. printk(KERN_ERR DRVNAME ": Out of memory\n");
  987. goto exit;
  988. }
  989. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  990. data->addr = res->start;
  991. mutex_init(&data->lock);
  992. data->name = names[sio_data->kind];
  993. mutex_init(&data->update_lock);
  994. platform_set_drvdata(pdev, data);
  995. /* Some voltage inputs depend on chip model and configuration */
  996. switch (sio_data->kind) {
  997. case f71805f:
  998. data->has_in = 0x1ff;
  999. break;
  1000. case f71872f:
  1001. data->has_in = 0x6ef;
  1002. if (sio_data->fnsel1 & 0x01)
  1003. data->has_in |= (1 << 4); /* in4 */
  1004. if (sio_data->fnsel1 & 0x02)
  1005. data->has_in |= (1 << 8); /* in8 */
  1006. break;
  1007. }
  1008. /* Initialize the F71805F chip */
  1009. f71805f_init_device(data);
  1010. /* Register sysfs interface files */
  1011. if ((err = sysfs_create_group(&pdev->dev.kobj, &f71805f_group)))
  1012. goto exit_free;
  1013. if (data->has_in & (1 << 4)) { /* in4 */
  1014. if ((err = sysfs_create_group(&pdev->dev.kobj,
  1015. &f71805f_group_optin[0])))
  1016. goto exit_remove_files;
  1017. }
  1018. if (data->has_in & (1 << 8)) { /* in8 */
  1019. if ((err = sysfs_create_group(&pdev->dev.kobj,
  1020. &f71805f_group_optin[1])))
  1021. goto exit_remove_files;
  1022. }
  1023. if (data->has_in & (1 << 9)) { /* in9 (F71872F/FG only) */
  1024. if ((err = sysfs_create_group(&pdev->dev.kobj,
  1025. &f71805f_group_optin[2])))
  1026. goto exit_remove_files;
  1027. }
  1028. if (data->has_in & (1 << 10)) { /* in9 (F71872F/FG only) */
  1029. if ((err = sysfs_create_group(&pdev->dev.kobj,
  1030. &f71805f_group_optin[3])))
  1031. goto exit_remove_files;
  1032. }
  1033. for (i = 0; i < 3; i++) {
  1034. /* If control mode is PWM, create pwm_freq file */
  1035. if (!(data->fan_ctrl[i] & FAN_CTRL_DC_MODE)) {
  1036. if ((err = sysfs_create_file(&pdev->dev.kobj,
  1037. f71805f_attributes_pwm_freq[i])))
  1038. goto exit_remove_files;
  1039. }
  1040. /* If PWM is in manual mode, add write permission */
  1041. if (data->fan_ctrl[i] & FAN_CTRL_MODE_MANUAL) {
  1042. if ((err = sysfs_chmod_file(&pdev->dev.kobj,
  1043. f71805f_attr_pwm[i],
  1044. S_IRUGO | S_IWUSR))) {
  1045. dev_err(&pdev->dev, "chmod +w pwm%d failed\n",
  1046. i + 1);
  1047. goto exit_remove_files;
  1048. }
  1049. }
  1050. }
  1051. data->class_dev = hwmon_device_register(&pdev->dev);
  1052. if (IS_ERR(data->class_dev)) {
  1053. err = PTR_ERR(data->class_dev);
  1054. dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
  1055. goto exit_remove_files;
  1056. }
  1057. return 0;
  1058. exit_remove_files:
  1059. sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
  1060. for (i = 0; i < 4; i++)
  1061. sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
  1062. sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
  1063. exit_free:
  1064. platform_set_drvdata(pdev, NULL);
  1065. kfree(data);
  1066. exit:
  1067. return err;
  1068. }
  1069. static int __devexit f71805f_remove(struct platform_device *pdev)
  1070. {
  1071. struct f71805f_data *data = platform_get_drvdata(pdev);
  1072. int i;
  1073. platform_set_drvdata(pdev, NULL);
  1074. hwmon_device_unregister(data->class_dev);
  1075. sysfs_remove_group(&pdev->dev.kobj, &f71805f_group);
  1076. for (i = 0; i < 4; i++)
  1077. sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_optin[i]);
  1078. sysfs_remove_group(&pdev->dev.kobj, &f71805f_group_pwm_freq);
  1079. kfree(data);
  1080. return 0;
  1081. }
  1082. static struct platform_driver f71805f_driver = {
  1083. .driver = {
  1084. .owner = THIS_MODULE,
  1085. .name = DRVNAME,
  1086. },
  1087. .probe = f71805f_probe,
  1088. .remove = __devexit_p(f71805f_remove),
  1089. };
  1090. static int __init f71805f_device_add(unsigned short address,
  1091. const struct f71805f_sio_data *sio_data)
  1092. {
  1093. struct resource res = {
  1094. .start = address,
  1095. .end = address + REGION_LENGTH - 1,
  1096. .flags = IORESOURCE_IO,
  1097. };
  1098. int err;
  1099. pdev = platform_device_alloc(DRVNAME, address);
  1100. if (!pdev) {
  1101. err = -ENOMEM;
  1102. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  1103. goto exit;
  1104. }
  1105. res.name = pdev->name;
  1106. err = platform_device_add_resources(pdev, &res, 1);
  1107. if (err) {
  1108. printk(KERN_ERR DRVNAME ": Device resource addition failed "
  1109. "(%d)\n", err);
  1110. goto exit_device_put;
  1111. }
  1112. pdev->dev.platform_data = kmalloc(sizeof(struct f71805f_sio_data),
  1113. GFP_KERNEL);
  1114. if (!pdev->dev.platform_data) {
  1115. err = -ENOMEM;
  1116. printk(KERN_ERR DRVNAME ": Platform data allocation failed\n");
  1117. goto exit_device_put;
  1118. }
  1119. memcpy(pdev->dev.platform_data, sio_data,
  1120. sizeof(struct f71805f_sio_data));
  1121. err = platform_device_add(pdev);
  1122. if (err) {
  1123. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  1124. err);
  1125. goto exit_kfree_data;
  1126. }
  1127. return 0;
  1128. exit_kfree_data:
  1129. kfree(pdev->dev.platform_data);
  1130. pdev->dev.platform_data = NULL;
  1131. exit_device_put:
  1132. platform_device_put(pdev);
  1133. exit:
  1134. return err;
  1135. }
  1136. static int __init f71805f_find(int sioaddr, unsigned short *address,
  1137. struct f71805f_sio_data *sio_data)
  1138. {
  1139. int err = -ENODEV;
  1140. u16 devid;
  1141. static const char *names[] = {
  1142. "F71805F/FG",
  1143. "F71872F/FG",
  1144. };
  1145. superio_enter(sioaddr);
  1146. devid = superio_inw(sioaddr, SIO_REG_MANID);
  1147. if (devid != SIO_FINTEK_ID)
  1148. goto exit;
  1149. devid = superio_inw(sioaddr, SIO_REG_DEVID);
  1150. switch (devid) {
  1151. case SIO_F71805F_ID:
  1152. sio_data->kind = f71805f;
  1153. break;
  1154. case SIO_F71872F_ID:
  1155. sio_data->kind = f71872f;
  1156. sio_data->fnsel1 = superio_inb(sioaddr, SIO_REG_FNSEL1);
  1157. break;
  1158. default:
  1159. printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
  1160. "skipping\n");
  1161. goto exit;
  1162. }
  1163. superio_select(sioaddr, F71805F_LD_HWM);
  1164. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  1165. printk(KERN_WARNING DRVNAME ": Device not activated, "
  1166. "skipping\n");
  1167. goto exit;
  1168. }
  1169. *address = superio_inw(sioaddr, SIO_REG_ADDR);
  1170. if (*address == 0) {
  1171. printk(KERN_WARNING DRVNAME ": Base address not set, "
  1172. "skipping\n");
  1173. goto exit;
  1174. }
  1175. *address &= ~(REGION_LENGTH - 1); /* Ignore 3 LSB */
  1176. err = 0;
  1177. printk(KERN_INFO DRVNAME ": Found %s chip at %#x, revision %u\n",
  1178. names[sio_data->kind], *address,
  1179. superio_inb(sioaddr, SIO_REG_DEVREV));
  1180. exit:
  1181. superio_exit(sioaddr);
  1182. return err;
  1183. }
  1184. static int __init f71805f_init(void)
  1185. {
  1186. int err;
  1187. unsigned short address;
  1188. struct f71805f_sio_data sio_data;
  1189. if (f71805f_find(0x2e, &address, &sio_data)
  1190. && f71805f_find(0x4e, &address, &sio_data))
  1191. return -ENODEV;
  1192. err = platform_driver_register(&f71805f_driver);
  1193. if (err)
  1194. goto exit;
  1195. /* Sets global pdev as a side effect */
  1196. err = f71805f_device_add(address, &sio_data);
  1197. if (err)
  1198. goto exit_driver;
  1199. return 0;
  1200. exit_driver:
  1201. platform_driver_unregister(&f71805f_driver);
  1202. exit:
  1203. return err;
  1204. }
  1205. static void __exit f71805f_exit(void)
  1206. {
  1207. kfree(pdev->dev.platform_data);
  1208. pdev->dev.platform_data = NULL;
  1209. platform_device_unregister(pdev);
  1210. platform_driver_unregister(&f71805f_driver);
  1211. }
  1212. MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
  1213. MODULE_LICENSE("GPL");
  1214. MODULE_DESCRIPTION("F71805F/F71872F hardware monitoring driver");
  1215. module_init(f71805f_init);
  1216. module_exit(f71805f_exit);