f71805f.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * f71805f.c - driver for the Fintek F71805F/FG Super-I/O chip integrated
  3. * hardware monitoring features
  4. * Copyright (C) 2005 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. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <asm/io.h>
  33. static struct platform_device *pdev;
  34. #define DRVNAME "f71805f"
  35. /*
  36. * Super-I/O constants and functions
  37. */
  38. #define F71805F_LD_HWM 0x04
  39. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  40. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  41. #define SIO_REG_DEVREV 0x22 /* Device revision */
  42. #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */
  43. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  44. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  45. #define SIO_FINTEK_ID 0x1934
  46. #define SIO_F71805F_ID 0x0406
  47. static inline int
  48. superio_inb(int base, int reg)
  49. {
  50. outb(reg, base);
  51. return inb(base + 1);
  52. }
  53. static int
  54. superio_inw(int base, int reg)
  55. {
  56. int val;
  57. outb(reg++, base);
  58. val = inb(base + 1) << 8;
  59. outb(reg, base);
  60. val |= inb(base + 1);
  61. return val;
  62. }
  63. static inline void
  64. superio_select(int base, int ld)
  65. {
  66. outb(SIO_REG_LDSEL, base);
  67. outb(ld, base + 1);
  68. }
  69. static inline void
  70. superio_enter(int base)
  71. {
  72. outb(0x87, base);
  73. outb(0x87, base);
  74. }
  75. static inline void
  76. superio_exit(int base)
  77. {
  78. outb(0xaa, base);
  79. }
  80. /*
  81. * ISA constants
  82. */
  83. #define REGION_LENGTH 2
  84. #define ADDR_REG_OFFSET 0
  85. #define DATA_REG_OFFSET 1
  86. static struct resource f71805f_resource __initdata = {
  87. .flags = IORESOURCE_IO,
  88. };
  89. /*
  90. * Registers
  91. */
  92. /* in nr from 0 to 8 (8-bit values) */
  93. #define F71805F_REG_IN(nr) (0x10 + (nr))
  94. #define F71805F_REG_IN_HIGH(nr) (0x40 + 2 * (nr))
  95. #define F71805F_REG_IN_LOW(nr) (0x41 + 2 * (nr))
  96. /* fan nr from 0 to 2 (12-bit values, two registers) */
  97. #define F71805F_REG_FAN(nr) (0x20 + 2 * (nr))
  98. #define F71805F_REG_FAN_LOW(nr) (0x28 + 2 * (nr))
  99. #define F71805F_REG_FAN_CTRL(nr) (0x60 + 16 * (nr))
  100. /* temp nr from 0 to 2 (8-bit values) */
  101. #define F71805F_REG_TEMP(nr) (0x1B + (nr))
  102. #define F71805F_REG_TEMP_HIGH(nr) (0x54 + 2 * (nr))
  103. #define F71805F_REG_TEMP_HYST(nr) (0x55 + 2 * (nr))
  104. #define F71805F_REG_TEMP_MODE 0x01
  105. #define F71805F_REG_START 0x00
  106. /* status nr from 0 to 2 */
  107. #define F71805F_REG_STATUS(nr) (0x36 + (nr))
  108. /*
  109. * Data structures and manipulation thereof
  110. */
  111. struct f71805f_data {
  112. unsigned short addr;
  113. const char *name;
  114. struct semaphore lock;
  115. struct class_device *class_dev;
  116. struct semaphore update_lock;
  117. char valid; /* !=0 if following fields are valid */
  118. unsigned long last_updated; /* In jiffies */
  119. unsigned long last_limits; /* In jiffies */
  120. /* Register values */
  121. u8 in[9];
  122. u8 in_high[9];
  123. u8 in_low[9];
  124. u16 fan[3];
  125. u16 fan_low[3];
  126. u8 fan_enabled; /* Read once at init time */
  127. u8 temp[3];
  128. u8 temp_high[3];
  129. u8 temp_hyst[3];
  130. u8 temp_mode;
  131. u8 alarms[3];
  132. };
  133. static inline long in_from_reg(u8 reg)
  134. {
  135. return (reg * 8);
  136. }
  137. /* The 2 least significant bits are not used */
  138. static inline u8 in_to_reg(long val)
  139. {
  140. if (val <= 0)
  141. return 0;
  142. if (val >= 2016)
  143. return 0xfc;
  144. return (((val + 16) / 32) << 2);
  145. }
  146. /* in0 is downscaled by a factor 2 internally */
  147. static inline long in0_from_reg(u8 reg)
  148. {
  149. return (reg * 16);
  150. }
  151. static inline u8 in0_to_reg(long val)
  152. {
  153. if (val <= 0)
  154. return 0;
  155. if (val >= 4032)
  156. return 0xfc;
  157. return (((val + 32) / 64) << 2);
  158. }
  159. /* The 4 most significant bits are not used */
  160. static inline long fan_from_reg(u16 reg)
  161. {
  162. reg &= 0xfff;
  163. if (!reg || reg == 0xfff)
  164. return 0;
  165. return (1500000 / reg);
  166. }
  167. static inline u16 fan_to_reg(long rpm)
  168. {
  169. /* If the low limit is set below what the chip can measure,
  170. store the largest possible 12-bit value in the registers,
  171. so that no alarm will ever trigger. */
  172. if (rpm < 367)
  173. return 0xfff;
  174. return (1500000 / rpm);
  175. }
  176. static inline long temp_from_reg(u8 reg)
  177. {
  178. return (reg * 1000);
  179. }
  180. static inline u8 temp_to_reg(long val)
  181. {
  182. if (val < 0)
  183. val = 0;
  184. else if (val > 1000 * 0xff)
  185. val = 0xff;
  186. return ((val + 500) / 1000);
  187. }
  188. /*
  189. * Device I/O access
  190. */
  191. static u8 f71805f_read8(struct f71805f_data *data, u8 reg)
  192. {
  193. u8 val;
  194. down(&data->lock);
  195. outb(reg, data->addr + ADDR_REG_OFFSET);
  196. val = inb(data->addr + DATA_REG_OFFSET);
  197. up(&data->lock);
  198. return val;
  199. }
  200. static void f71805f_write8(struct f71805f_data *data, u8 reg, u8 val)
  201. {
  202. down(&data->lock);
  203. outb(reg, data->addr + ADDR_REG_OFFSET);
  204. outb(val, data->addr + DATA_REG_OFFSET);
  205. up(&data->lock);
  206. }
  207. /* It is important to read the MSB first, because doing so latches the
  208. value of the LSB, so we are sure both bytes belong to the same value. */
  209. static u16 f71805f_read16(struct f71805f_data *data, u8 reg)
  210. {
  211. u16 val;
  212. down(&data->lock);
  213. outb(reg, data->addr + ADDR_REG_OFFSET);
  214. val = inb(data->addr + DATA_REG_OFFSET) << 8;
  215. outb(++reg, data->addr + ADDR_REG_OFFSET);
  216. val |= inb(data->addr + DATA_REG_OFFSET);
  217. up(&data->lock);
  218. return val;
  219. }
  220. static void f71805f_write16(struct f71805f_data *data, u8 reg, u16 val)
  221. {
  222. down(&data->lock);
  223. outb(reg, data->addr + ADDR_REG_OFFSET);
  224. outb(val >> 8, data->addr + DATA_REG_OFFSET);
  225. outb(++reg, data->addr + ADDR_REG_OFFSET);
  226. outb(val & 0xff, data->addr + DATA_REG_OFFSET);
  227. up(&data->lock);
  228. }
  229. static struct f71805f_data *f71805f_update_device(struct device *dev)
  230. {
  231. struct f71805f_data *data = dev_get_drvdata(dev);
  232. int nr;
  233. down(&data->update_lock);
  234. /* Limit registers cache is refreshed after 60 seconds */
  235. if (time_after(jiffies, data->last_updated + 60 * HZ)
  236. || !data->valid) {
  237. for (nr = 0; nr < 9; nr++) {
  238. data->in_high[nr] = f71805f_read8(data,
  239. F71805F_REG_IN_HIGH(nr));
  240. data->in_low[nr] = f71805f_read8(data,
  241. F71805F_REG_IN_LOW(nr));
  242. }
  243. for (nr = 0; nr < 3; nr++) {
  244. if (data->fan_enabled & (1 << nr))
  245. data->fan_low[nr] = f71805f_read16(data,
  246. F71805F_REG_FAN_LOW(nr));
  247. }
  248. for (nr = 0; nr < 3; nr++) {
  249. data->temp_high[nr] = f71805f_read8(data,
  250. F71805F_REG_TEMP_HIGH(nr));
  251. data->temp_hyst[nr] = f71805f_read8(data,
  252. F71805F_REG_TEMP_HYST(nr));
  253. }
  254. data->temp_mode = f71805f_read8(data, F71805F_REG_TEMP_MODE);
  255. data->last_limits = jiffies;
  256. }
  257. /* Measurement registers cache is refreshed after 1 second */
  258. if (time_after(jiffies, data->last_updated + HZ)
  259. || !data->valid) {
  260. for (nr = 0; nr < 9; nr++) {
  261. data->in[nr] = f71805f_read8(data,
  262. F71805F_REG_IN(nr));
  263. }
  264. for (nr = 0; nr < 3; nr++) {
  265. if (data->fan_enabled & (1 << nr))
  266. data->fan[nr] = f71805f_read16(data,
  267. F71805F_REG_FAN(nr));
  268. }
  269. for (nr = 0; nr < 3; nr++) {
  270. data->temp[nr] = f71805f_read8(data,
  271. F71805F_REG_TEMP(nr));
  272. }
  273. for (nr = 0; nr < 3; nr++) {
  274. data->alarms[nr] = f71805f_read8(data,
  275. F71805F_REG_STATUS(nr));
  276. }
  277. data->last_updated = jiffies;
  278. data->valid = 1;
  279. }
  280. up(&data->update_lock);
  281. return data;
  282. }
  283. /*
  284. * Sysfs interface
  285. */
  286. static ssize_t show_in0(struct device *dev, struct device_attribute *devattr,
  287. char *buf)
  288. {
  289. struct f71805f_data *data = f71805f_update_device(dev);
  290. return sprintf(buf, "%ld\n", in0_from_reg(data->in[0]));
  291. }
  292. static ssize_t show_in0_max(struct device *dev, struct device_attribute
  293. *devattr, char *buf)
  294. {
  295. struct f71805f_data *data = f71805f_update_device(dev);
  296. return sprintf(buf, "%ld\n", in0_from_reg(data->in_high[0]));
  297. }
  298. static ssize_t show_in0_min(struct device *dev, struct device_attribute
  299. *devattr, char *buf)
  300. {
  301. struct f71805f_data *data = f71805f_update_device(dev);
  302. return sprintf(buf, "%ld\n", in0_from_reg(data->in_low[0]));
  303. }
  304. static ssize_t set_in0_max(struct device *dev, struct device_attribute
  305. *devattr, const char *buf, size_t count)
  306. {
  307. struct f71805f_data *data = dev_get_drvdata(dev);
  308. long val = simple_strtol(buf, NULL, 10);
  309. down(&data->update_lock);
  310. data->in_high[0] = in0_to_reg(val);
  311. f71805f_write8(data, F71805F_REG_IN_HIGH(0), data->in_high[0]);
  312. up(&data->update_lock);
  313. return count;
  314. }
  315. static ssize_t set_in0_min(struct device *dev, struct device_attribute
  316. *devattr, const char *buf, size_t count)
  317. {
  318. struct f71805f_data *data = dev_get_drvdata(dev);
  319. long val = simple_strtol(buf, NULL, 10);
  320. down(&data->update_lock);
  321. data->in_low[0] = in0_to_reg(val);
  322. f71805f_write8(data, F71805F_REG_IN_LOW(0), data->in_low[0]);
  323. up(&data->update_lock);
  324. return count;
  325. }
  326. static DEVICE_ATTR(in0_input, S_IRUGO, show_in0, NULL);
  327. static DEVICE_ATTR(in0_max, S_IRUGO| S_IWUSR, show_in0_max, set_in0_max);
  328. static DEVICE_ATTR(in0_min, S_IRUGO| S_IWUSR, show_in0_min, set_in0_min);
  329. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  330. char *buf)
  331. {
  332. struct f71805f_data *data = f71805f_update_device(dev);
  333. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  334. int nr = attr->index;
  335. return sprintf(buf, "%ld\n", in_from_reg(data->in[nr]));
  336. }
  337. static ssize_t show_in_max(struct device *dev, struct device_attribute
  338. *devattr, char *buf)
  339. {
  340. struct f71805f_data *data = f71805f_update_device(dev);
  341. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  342. int nr = attr->index;
  343. return sprintf(buf, "%ld\n", in_from_reg(data->in_high[nr]));
  344. }
  345. static ssize_t show_in_min(struct device *dev, struct device_attribute
  346. *devattr, char *buf)
  347. {
  348. struct f71805f_data *data = f71805f_update_device(dev);
  349. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  350. int nr = attr->index;
  351. return sprintf(buf, "%ld\n", in_from_reg(data->in_low[nr]));
  352. }
  353. static ssize_t set_in_max(struct device *dev, struct device_attribute
  354. *devattr, const char *buf, size_t count)
  355. {
  356. struct f71805f_data *data = dev_get_drvdata(dev);
  357. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  358. int nr = attr->index;
  359. long val = simple_strtol(buf, NULL, 10);
  360. down(&data->update_lock);
  361. data->in_high[nr] = in_to_reg(val);
  362. f71805f_write8(data, F71805F_REG_IN_HIGH(nr), data->in_high[nr]);
  363. up(&data->update_lock);
  364. return count;
  365. }
  366. static ssize_t set_in_min(struct device *dev, struct device_attribute
  367. *devattr, const char *buf, size_t count)
  368. {
  369. struct f71805f_data *data = dev_get_drvdata(dev);
  370. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  371. int nr = attr->index;
  372. long val = simple_strtol(buf, NULL, 10);
  373. down(&data->update_lock);
  374. data->in_low[nr] = in_to_reg(val);
  375. f71805f_write8(data, F71805F_REG_IN_LOW(nr), data->in_low[nr]);
  376. up(&data->update_lock);
  377. return count;
  378. }
  379. #define sysfs_in(offset) \
  380. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  381. show_in, NULL, offset); \
  382. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  383. show_in_max, set_in_max, offset); \
  384. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  385. show_in_min, set_in_min, offset)
  386. sysfs_in(1);
  387. sysfs_in(2);
  388. sysfs_in(3);
  389. sysfs_in(4);
  390. sysfs_in(5);
  391. sysfs_in(6);
  392. sysfs_in(7);
  393. sysfs_in(8);
  394. static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
  395. char *buf)
  396. {
  397. struct f71805f_data *data = f71805f_update_device(dev);
  398. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  399. int nr = attr->index;
  400. return sprintf(buf, "%ld\n", fan_from_reg(data->fan[nr]));
  401. }
  402. static ssize_t show_fan_min(struct device *dev, struct device_attribute
  403. *devattr, char *buf)
  404. {
  405. struct f71805f_data *data = f71805f_update_device(dev);
  406. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  407. int nr = attr->index;
  408. return sprintf(buf, "%ld\n", fan_from_reg(data->fan_low[nr]));
  409. }
  410. static ssize_t set_fan_min(struct device *dev, struct device_attribute
  411. *devattr, const char *buf, size_t count)
  412. {
  413. struct f71805f_data *data = dev_get_drvdata(dev);
  414. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  415. int nr = attr->index;
  416. long val = simple_strtol(buf, NULL, 10);
  417. down(&data->update_lock);
  418. data->fan_low[nr] = fan_to_reg(val);
  419. f71805f_write16(data, F71805F_REG_FAN_LOW(nr), data->fan_low[nr]);
  420. up(&data->update_lock);
  421. return count;
  422. }
  423. #define sysfs_fan(offset) \
  424. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  425. show_fan, NULL, offset - 1); \
  426. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  427. show_fan_min, set_fan_min, offset - 1)
  428. sysfs_fan(1);
  429. sysfs_fan(2);
  430. sysfs_fan(3);
  431. static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
  432. char *buf)
  433. {
  434. struct f71805f_data *data = f71805f_update_device(dev);
  435. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  436. int nr = attr->index;
  437. return sprintf(buf, "%ld\n", temp_from_reg(data->temp[nr]));
  438. }
  439. static ssize_t show_temp_max(struct device *dev, struct device_attribute
  440. *devattr, char *buf)
  441. {
  442. struct f71805f_data *data = f71805f_update_device(dev);
  443. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  444. int nr = attr->index;
  445. return sprintf(buf, "%ld\n", temp_from_reg(data->temp_high[nr]));
  446. }
  447. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute
  448. *devattr, char *buf)
  449. {
  450. struct f71805f_data *data = f71805f_update_device(dev);
  451. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  452. int nr = attr->index;
  453. return sprintf(buf, "%ld\n", temp_from_reg(data->temp_hyst[nr]));
  454. }
  455. static ssize_t show_temp_type(struct device *dev, struct device_attribute
  456. *devattr, char *buf)
  457. {
  458. struct f71805f_data *data = f71805f_update_device(dev);
  459. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  460. int nr = attr->index;
  461. /* 3 is diode, 4 is thermistor */
  462. return sprintf(buf, "%u\n", (data->temp_mode & (1 << nr)) ? 3 : 4);
  463. }
  464. static ssize_t set_temp_max(struct device *dev, struct device_attribute
  465. *devattr, const char *buf, size_t count)
  466. {
  467. struct f71805f_data *data = dev_get_drvdata(dev);
  468. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  469. int nr = attr->index;
  470. long val = simple_strtol(buf, NULL, 10);
  471. down(&data->update_lock);
  472. data->temp_high[nr] = temp_to_reg(val);
  473. f71805f_write8(data, F71805F_REG_TEMP_HIGH(nr), data->temp_high[nr]);
  474. up(&data->update_lock);
  475. return count;
  476. }
  477. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute
  478. *devattr, const char *buf, size_t count)
  479. {
  480. struct f71805f_data *data = dev_get_drvdata(dev);
  481. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  482. int nr = attr->index;
  483. long val = simple_strtol(buf, NULL, 10);
  484. down(&data->update_lock);
  485. data->temp_hyst[nr] = temp_to_reg(val);
  486. f71805f_write8(data, F71805F_REG_TEMP_HYST(nr), data->temp_hyst[nr]);
  487. up(&data->update_lock);
  488. return count;
  489. }
  490. #define sysfs_temp(offset) \
  491. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  492. show_temp, NULL, offset - 1); \
  493. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  494. show_temp_max, set_temp_max, offset - 1); \
  495. static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
  496. show_temp_hyst, set_temp_hyst, offset - 1); \
  497. static SENSOR_DEVICE_ATTR(temp##offset##_type, S_IRUGO, \
  498. show_temp_type, NULL, offset - 1)
  499. sysfs_temp(1);
  500. sysfs_temp(2);
  501. sysfs_temp(3);
  502. static ssize_t show_alarms_in(struct device *dev, struct device_attribute
  503. *devattr, char *buf)
  504. {
  505. struct f71805f_data *data = f71805f_update_device(dev);
  506. return sprintf(buf, "%d\n", data->alarms[0] |
  507. ((data->alarms[1] & 0x01) << 8));
  508. }
  509. static ssize_t show_alarms_fan(struct device *dev, struct device_attribute
  510. *devattr, char *buf)
  511. {
  512. struct f71805f_data *data = f71805f_update_device(dev);
  513. return sprintf(buf, "%d\n", data->alarms[2] & 0x07);
  514. }
  515. static ssize_t show_alarms_temp(struct device *dev, struct device_attribute
  516. *devattr, char *buf)
  517. {
  518. struct f71805f_data *data = f71805f_update_device(dev);
  519. return sprintf(buf, "%d\n", (data->alarms[1] >> 3) & 0x07);
  520. }
  521. static DEVICE_ATTR(alarms_in, S_IRUGO, show_alarms_in, NULL);
  522. static DEVICE_ATTR(alarms_fan, S_IRUGO, show_alarms_fan, NULL);
  523. static DEVICE_ATTR(alarms_temp, S_IRUGO, show_alarms_temp, NULL);
  524. static ssize_t show_name(struct device *dev, struct device_attribute
  525. *devattr, char *buf)
  526. {
  527. struct f71805f_data *data = dev_get_drvdata(dev);
  528. return sprintf(buf, "%s\n", data->name);
  529. }
  530. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  531. /*
  532. * Device registration and initialization
  533. */
  534. static void __devinit f71805f_init_device(struct f71805f_data *data)
  535. {
  536. u8 reg;
  537. int i;
  538. reg = f71805f_read8(data, F71805F_REG_START);
  539. if ((reg & 0x41) != 0x01) {
  540. printk(KERN_DEBUG DRVNAME ": Starting monitoring "
  541. "operations\n");
  542. f71805f_write8(data, F71805F_REG_START, (reg | 0x01) & ~0x40);
  543. }
  544. /* Fan monitoring can be disabled. If it is, we won't be polling
  545. the register values, and won't create the related sysfs files. */
  546. for (i = 0; i < 3; i++) {
  547. reg = f71805f_read8(data, F71805F_REG_FAN_CTRL(i));
  548. if (!(reg & 0x80))
  549. data->fan_enabled |= (1 << i);
  550. }
  551. }
  552. static int __devinit f71805f_probe(struct platform_device *pdev)
  553. {
  554. struct f71805f_data *data;
  555. struct resource *res;
  556. int err;
  557. if (!(data = kzalloc(sizeof(struct f71805f_data), GFP_KERNEL))) {
  558. err = -ENOMEM;
  559. printk(KERN_ERR DRVNAME ": Out of memory\n");
  560. goto exit;
  561. }
  562. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  563. data->addr = res->start;
  564. init_MUTEX(&data->lock);
  565. data->name = "f71805f";
  566. init_MUTEX(&data->update_lock);
  567. platform_set_drvdata(pdev, data);
  568. data->class_dev = hwmon_device_register(&pdev->dev);
  569. if (IS_ERR(data->class_dev)) {
  570. err = PTR_ERR(data->class_dev);
  571. dev_err(&pdev->dev, "Class registration failed (%d)\n", err);
  572. goto exit_free;
  573. }
  574. /* Initialize the F71805F chip */
  575. f71805f_init_device(data);
  576. /* Register sysfs interface files */
  577. device_create_file(&pdev->dev, &dev_attr_in0_input);
  578. device_create_file(&pdev->dev, &dev_attr_in0_max);
  579. device_create_file(&pdev->dev, &dev_attr_in0_min);
  580. device_create_file(&pdev->dev, &sensor_dev_attr_in1_input.dev_attr);
  581. device_create_file(&pdev->dev, &sensor_dev_attr_in2_input.dev_attr);
  582. device_create_file(&pdev->dev, &sensor_dev_attr_in3_input.dev_attr);
  583. device_create_file(&pdev->dev, &sensor_dev_attr_in4_input.dev_attr);
  584. device_create_file(&pdev->dev, &sensor_dev_attr_in5_input.dev_attr);
  585. device_create_file(&pdev->dev, &sensor_dev_attr_in6_input.dev_attr);
  586. device_create_file(&pdev->dev, &sensor_dev_attr_in7_input.dev_attr);
  587. device_create_file(&pdev->dev, &sensor_dev_attr_in8_input.dev_attr);
  588. device_create_file(&pdev->dev, &sensor_dev_attr_in1_max.dev_attr);
  589. device_create_file(&pdev->dev, &sensor_dev_attr_in2_max.dev_attr);
  590. device_create_file(&pdev->dev, &sensor_dev_attr_in3_max.dev_attr);
  591. device_create_file(&pdev->dev, &sensor_dev_attr_in4_max.dev_attr);
  592. device_create_file(&pdev->dev, &sensor_dev_attr_in5_max.dev_attr);
  593. device_create_file(&pdev->dev, &sensor_dev_attr_in6_max.dev_attr);
  594. device_create_file(&pdev->dev, &sensor_dev_attr_in7_max.dev_attr);
  595. device_create_file(&pdev->dev, &sensor_dev_attr_in8_max.dev_attr);
  596. device_create_file(&pdev->dev, &sensor_dev_attr_in1_min.dev_attr);
  597. device_create_file(&pdev->dev, &sensor_dev_attr_in2_min.dev_attr);
  598. device_create_file(&pdev->dev, &sensor_dev_attr_in3_min.dev_attr);
  599. device_create_file(&pdev->dev, &sensor_dev_attr_in4_min.dev_attr);
  600. device_create_file(&pdev->dev, &sensor_dev_attr_in5_min.dev_attr);
  601. device_create_file(&pdev->dev, &sensor_dev_attr_in6_min.dev_attr);
  602. device_create_file(&pdev->dev, &sensor_dev_attr_in7_min.dev_attr);
  603. device_create_file(&pdev->dev, &sensor_dev_attr_in8_min.dev_attr);
  604. if (data->fan_enabled & (1 << 0)) {
  605. device_create_file(&pdev->dev,
  606. &sensor_dev_attr_fan1_input.dev_attr);
  607. device_create_file(&pdev->dev,
  608. &sensor_dev_attr_fan1_min.dev_attr);
  609. }
  610. if (data->fan_enabled & (1 << 1)) {
  611. device_create_file(&pdev->dev,
  612. &sensor_dev_attr_fan2_input.dev_attr);
  613. device_create_file(&pdev->dev,
  614. &sensor_dev_attr_fan2_min.dev_attr);
  615. }
  616. if (data->fan_enabled & (1 << 2)) {
  617. device_create_file(&pdev->dev,
  618. &sensor_dev_attr_fan3_input.dev_attr);
  619. device_create_file(&pdev->dev,
  620. &sensor_dev_attr_fan3_min.dev_attr);
  621. }
  622. device_create_file(&pdev->dev,
  623. &sensor_dev_attr_temp1_input.dev_attr);
  624. device_create_file(&pdev->dev,
  625. &sensor_dev_attr_temp2_input.dev_attr);
  626. device_create_file(&pdev->dev,
  627. &sensor_dev_attr_temp3_input.dev_attr);
  628. device_create_file(&pdev->dev, &sensor_dev_attr_temp1_max.dev_attr);
  629. device_create_file(&pdev->dev, &sensor_dev_attr_temp2_max.dev_attr);
  630. device_create_file(&pdev->dev, &sensor_dev_attr_temp3_max.dev_attr);
  631. device_create_file(&pdev->dev,
  632. &sensor_dev_attr_temp1_max_hyst.dev_attr);
  633. device_create_file(&pdev->dev,
  634. &sensor_dev_attr_temp2_max_hyst.dev_attr);
  635. device_create_file(&pdev->dev,
  636. &sensor_dev_attr_temp3_max_hyst.dev_attr);
  637. device_create_file(&pdev->dev, &sensor_dev_attr_temp1_type.dev_attr);
  638. device_create_file(&pdev->dev, &sensor_dev_attr_temp2_type.dev_attr);
  639. device_create_file(&pdev->dev, &sensor_dev_attr_temp3_type.dev_attr);
  640. device_create_file(&pdev->dev, &dev_attr_alarms_in);
  641. device_create_file(&pdev->dev, &dev_attr_alarms_fan);
  642. device_create_file(&pdev->dev, &dev_attr_alarms_temp);
  643. device_create_file(&pdev->dev, &dev_attr_name);
  644. return 0;
  645. exit_free:
  646. kfree(data);
  647. exit:
  648. return err;
  649. }
  650. static int __devexit f71805f_remove(struct platform_device *pdev)
  651. {
  652. struct f71805f_data *data = platform_get_drvdata(pdev);
  653. platform_set_drvdata(pdev, NULL);
  654. hwmon_device_unregister(data->class_dev);
  655. kfree(data);
  656. return 0;
  657. }
  658. static struct platform_driver f71805f_driver = {
  659. .driver = {
  660. .owner = THIS_MODULE,
  661. .name = DRVNAME,
  662. },
  663. .probe = f71805f_probe,
  664. .remove = __devexit_p(f71805f_remove),
  665. };
  666. static int __init f71805f_device_add(unsigned short address)
  667. {
  668. int err;
  669. pdev = platform_device_alloc(DRVNAME, address);
  670. if (!pdev) {
  671. err = -ENOMEM;
  672. printk(KERN_ERR DRVNAME ": Device allocation failed\n");
  673. goto exit;
  674. }
  675. f71805f_resource.start = address;
  676. f71805f_resource.end = address + REGION_LENGTH - 1;
  677. f71805f_resource.name = pdev->name;
  678. err = platform_device_add_resources(pdev, &f71805f_resource, 1);
  679. if (err) {
  680. printk(KERN_ERR DRVNAME ": Device resource addition failed "
  681. "(%d)\n", err);
  682. goto exit_device_put;
  683. }
  684. err = platform_device_add(pdev);
  685. if (err) {
  686. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  687. err);
  688. goto exit_device_put;
  689. }
  690. return 0;
  691. exit_device_put:
  692. platform_device_put(pdev);
  693. exit:
  694. return err;
  695. }
  696. static int __init f71805f_find(int sioaddr, unsigned short *address)
  697. {
  698. int err = -ENODEV;
  699. u16 devid;
  700. superio_enter(sioaddr);
  701. devid = superio_inw(sioaddr, SIO_REG_MANID);
  702. if (devid != SIO_FINTEK_ID)
  703. goto exit;
  704. devid = superio_inw(sioaddr, SIO_REG_DEVID);
  705. if (devid != SIO_F71805F_ID) {
  706. printk(KERN_INFO DRVNAME ": Unsupported Fintek device, "
  707. "skipping\n");
  708. goto exit;
  709. }
  710. superio_select(sioaddr, F71805F_LD_HWM);
  711. if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
  712. printk(KERN_WARNING DRVNAME ": Device not activated, "
  713. "skipping\n");
  714. goto exit;
  715. }
  716. *address = superio_inw(sioaddr, SIO_REG_ADDR);
  717. if (*address == 0) {
  718. printk(KERN_WARNING DRVNAME ": Base address not set, "
  719. "skipping\n");
  720. goto exit;
  721. }
  722. err = 0;
  723. printk(KERN_INFO DRVNAME ": Found F71805F chip at %#x, revision %u\n",
  724. *address, superio_inb(sioaddr, SIO_REG_DEVREV));
  725. exit:
  726. superio_exit(sioaddr);
  727. return err;
  728. }
  729. static int __init f71805f_init(void)
  730. {
  731. int err;
  732. unsigned short address;
  733. if (f71805f_find(0x2e, &address)
  734. && f71805f_find(0x4e, &address))
  735. return -ENODEV;
  736. err = platform_driver_register(&f71805f_driver);
  737. if (err)
  738. goto exit;
  739. /* Sets global pdev as a side effect */
  740. err = f71805f_device_add(address);
  741. if (err)
  742. goto exit_driver;
  743. return 0;
  744. exit_driver:
  745. platform_driver_unregister(&f71805f_driver);
  746. exit:
  747. return err;
  748. }
  749. static void __exit f71805f_exit(void)
  750. {
  751. platform_device_unregister(pdev);
  752. platform_driver_unregister(&f71805f_driver);
  753. }
  754. MODULE_AUTHOR("Jean Delvare <khali@linux-fr>");
  755. MODULE_LICENSE("GPL");
  756. MODULE_DESCRIPTION("F71805F hardware monitoring driver");
  757. module_init(f71805f_init);
  758. module_exit(f71805f_exit);