f71805f.c 25 KB

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