w83627ehf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. w83627ehf - Driver for the hardware monitoring functionality of
  3. the Winbond W83627EHF Super-I/O chip
  4. Copyright (C) 2005 Jean Delvare <khali@linux-fr.org>
  5. Shamelessly ripped from the w83627hf driver
  6. Copyright (C) 2003 Mark Studebaker
  7. Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
  8. in testing and debugging this driver.
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. Supports the following chips:
  21. Chip #vin #fan #pwm #temp chip_id man_id
  22. w83627ehf - 5 - 3 0x88 0x5ca3
  23. This is a preliminary version of the driver, only supporting the
  24. fan and temperature inputs. The chip does much more than that.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include <linux/i2c-sensor.h>
  31. #include <asm/io.h>
  32. #include "lm75.h"
  33. /* Addresses to scan
  34. The actual ISA address is read from Super-I/O configuration space */
  35. static unsigned short normal_i2c[] = { I2C_CLIENT_END };
  36. static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
  37. /* Insmod parameters */
  38. SENSORS_INSMOD_1(w83627ehf);
  39. /*
  40. * Super-I/O constants and functions
  41. */
  42. static int REG; /* The register to read/write */
  43. static int VAL; /* The value to read/write */
  44. #define W83627EHF_LD_HWM 0x0b
  45. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  46. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  47. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  48. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  49. #define SIO_W83627EHF_ID 0x8840
  50. #define SIO_ID_MASK 0xFFC0
  51. static inline void
  52. superio_outb(int reg, int val)
  53. {
  54. outb(reg, REG);
  55. outb(val, VAL);
  56. }
  57. static inline int
  58. superio_inb(int reg)
  59. {
  60. outb(reg, REG);
  61. return inb(VAL);
  62. }
  63. static inline void
  64. superio_select(int ld)
  65. {
  66. outb(SIO_REG_LDSEL, REG);
  67. outb(ld, VAL);
  68. }
  69. static inline void
  70. superio_enter(void)
  71. {
  72. outb(0x87, REG);
  73. outb(0x87, REG);
  74. }
  75. static inline void
  76. superio_exit(void)
  77. {
  78. outb(0x02, REG);
  79. outb(0x02, VAL);
  80. }
  81. /*
  82. * ISA constants
  83. */
  84. #define REGION_LENGTH 8
  85. #define ADDR_REG_OFFSET 5
  86. #define DATA_REG_OFFSET 6
  87. #define W83627EHF_REG_BANK 0x4E
  88. #define W83627EHF_REG_CONFIG 0x40
  89. #define W83627EHF_REG_CHIP_ID 0x49
  90. #define W83627EHF_REG_MAN_ID 0x4F
  91. static const u16 W83627EHF_REG_FAN[] = { 0x28, 0x29, 0x2a, 0x3f, 0x553 };
  92. static const u16 W83627EHF_REG_FAN_MIN[] = { 0x3b, 0x3c, 0x3d, 0x3e, 0x55c };
  93. #define W83627EHF_REG_TEMP1 0x27
  94. #define W83627EHF_REG_TEMP1_HYST 0x3a
  95. #define W83627EHF_REG_TEMP1_OVER 0x39
  96. static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
  97. static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
  98. static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
  99. static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
  100. /* Fan clock dividers are spread over the following five registers */
  101. #define W83627EHF_REG_FANDIV1 0x47
  102. #define W83627EHF_REG_FANDIV2 0x4B
  103. #define W83627EHF_REG_VBAT 0x5D
  104. #define W83627EHF_REG_DIODE 0x59
  105. #define W83627EHF_REG_SMI_OVT 0x4C
  106. /*
  107. * Conversions
  108. */
  109. static inline unsigned int
  110. fan_from_reg(u8 reg, unsigned int div)
  111. {
  112. if (reg == 0 || reg == 255)
  113. return 0;
  114. return 1350000U / (reg * div);
  115. }
  116. static inline unsigned int
  117. div_from_reg(u8 reg)
  118. {
  119. return 1 << reg;
  120. }
  121. static inline int
  122. temp1_from_reg(s8 reg)
  123. {
  124. return reg * 1000;
  125. }
  126. static inline s8
  127. temp1_to_reg(int temp)
  128. {
  129. if (temp <= -128000)
  130. return -128;
  131. if (temp >= 127000)
  132. return 127;
  133. if (temp < 0)
  134. return (temp - 500) / 1000;
  135. return (temp + 500) / 1000;
  136. }
  137. /*
  138. * Data structures and manipulation thereof
  139. */
  140. struct w83627ehf_data {
  141. struct i2c_client client;
  142. struct semaphore lock;
  143. struct semaphore update_lock;
  144. char valid; /* !=0 if following fields are valid */
  145. unsigned long last_updated; /* In jiffies */
  146. /* Register values */
  147. u8 fan[5];
  148. u8 fan_min[5];
  149. u8 fan_div[5];
  150. u8 has_fan; /* some fan inputs can be disabled */
  151. s8 temp1;
  152. s8 temp1_max;
  153. s8 temp1_max_hyst;
  154. s16 temp[2];
  155. s16 temp_max[2];
  156. s16 temp_max_hyst[2];
  157. };
  158. static inline int is_word_sized(u16 reg)
  159. {
  160. return (((reg & 0xff00) == 0x100
  161. || (reg & 0xff00) == 0x200)
  162. && ((reg & 0x00ff) == 0x50
  163. || (reg & 0x00ff) == 0x53
  164. || (reg & 0x00ff) == 0x55));
  165. }
  166. /* We assume that the default bank is 0, thus the following two functions do
  167. nothing for registers which live in bank 0. For others, they respectively
  168. set the bank register to the correct value (before the register is
  169. accessed), and back to 0 (afterwards). */
  170. static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg)
  171. {
  172. if (reg & 0xff00) {
  173. outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
  174. outb_p(reg >> 8, client->addr + DATA_REG_OFFSET);
  175. }
  176. }
  177. static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
  178. {
  179. if (reg & 0xff00) {
  180. outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
  181. outb_p(0, client->addr + DATA_REG_OFFSET);
  182. }
  183. }
  184. static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
  185. {
  186. struct w83627ehf_data *data = i2c_get_clientdata(client);
  187. int res, word_sized = is_word_sized(reg);
  188. down(&data->lock);
  189. w83627ehf_set_bank(client, reg);
  190. outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
  191. res = inb_p(client->addr + DATA_REG_OFFSET);
  192. if (word_sized) {
  193. outb_p((reg & 0xff) + 1,
  194. client->addr + ADDR_REG_OFFSET);
  195. res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET);
  196. }
  197. w83627ehf_reset_bank(client, reg);
  198. up(&data->lock);
  199. return res;
  200. }
  201. static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
  202. {
  203. struct w83627ehf_data *data = i2c_get_clientdata(client);
  204. int word_sized = is_word_sized(reg);
  205. down(&data->lock);
  206. w83627ehf_set_bank(client, reg);
  207. outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
  208. if (word_sized) {
  209. outb_p(value >> 8, client->addr + DATA_REG_OFFSET);
  210. outb_p((reg & 0xff) + 1,
  211. client->addr + ADDR_REG_OFFSET);
  212. }
  213. outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
  214. w83627ehf_reset_bank(client, reg);
  215. up(&data->lock);
  216. return 0;
  217. }
  218. /* This function assumes that the caller holds data->update_lock */
  219. static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
  220. {
  221. struct w83627ehf_data *data = i2c_get_clientdata(client);
  222. u8 reg;
  223. switch (nr) {
  224. case 0:
  225. reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0xcf)
  226. | ((data->fan_div[0] & 0x03) << 4);
  227. w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
  228. reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xdf)
  229. | ((data->fan_div[0] & 0x04) << 3);
  230. w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
  231. break;
  232. case 1:
  233. reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0x3f)
  234. | ((data->fan_div[1] & 0x03) << 6);
  235. w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
  236. reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xbf)
  237. | ((data->fan_div[1] & 0x04) << 4);
  238. w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
  239. break;
  240. case 2:
  241. reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) & 0x3f)
  242. | ((data->fan_div[2] & 0x03) << 6);
  243. w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg);
  244. reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0x7f)
  245. | ((data->fan_div[2] & 0x04) << 5);
  246. w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
  247. break;
  248. case 3:
  249. reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0xfc)
  250. | (data->fan_div[3] & 0x03);
  251. w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
  252. reg = (w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) & 0x7f)
  253. | ((data->fan_div[3] & 0x04) << 5);
  254. w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg);
  255. break;
  256. case 4:
  257. reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0x73)
  258. | ((data->fan_div[4] & 0x03) << 3)
  259. | ((data->fan_div[4] & 0x04) << 5);
  260. w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
  261. break;
  262. }
  263. }
  264. static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
  265. {
  266. struct i2c_client *client = to_i2c_client(dev);
  267. struct w83627ehf_data *data = i2c_get_clientdata(client);
  268. int i;
  269. down(&data->update_lock);
  270. if (time_after(jiffies, data->last_updated + HZ)
  271. || !data->valid) {
  272. /* Fan clock dividers */
  273. i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
  274. data->fan_div[0] = (i >> 4) & 0x03;
  275. data->fan_div[1] = (i >> 6) & 0x03;
  276. i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2);
  277. data->fan_div[2] = (i >> 6) & 0x03;
  278. i = w83627ehf_read_value(client, W83627EHF_REG_VBAT);
  279. data->fan_div[0] |= (i >> 3) & 0x04;
  280. data->fan_div[1] |= (i >> 4) & 0x04;
  281. data->fan_div[2] |= (i >> 5) & 0x04;
  282. if (data->has_fan & ((1 << 3) | (1 << 4))) {
  283. i = w83627ehf_read_value(client, W83627EHF_REG_DIODE);
  284. data->fan_div[3] = i & 0x03;
  285. data->fan_div[4] = ((i >> 2) & 0x03)
  286. | ((i >> 5) & 0x04);
  287. }
  288. if (data->has_fan & (1 << 3)) {
  289. i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT);
  290. data->fan_div[3] |= (i >> 5) & 0x04;
  291. }
  292. /* Measured fan speeds and limits */
  293. for (i = 0; i < 5; i++) {
  294. if (!(data->has_fan & (1 << i)))
  295. continue;
  296. data->fan[i] = w83627ehf_read_value(client,
  297. W83627EHF_REG_FAN[i]);
  298. data->fan_min[i] = w83627ehf_read_value(client,
  299. W83627EHF_REG_FAN_MIN[i]);
  300. /* If we failed to measure the fan speed and clock
  301. divider can be increased, let's try that for next
  302. time */
  303. if (data->fan[i] == 0xff
  304. && data->fan_div[i] < 0x07) {
  305. dev_dbg(&client->dev, "Increasing fan %d "
  306. "clock divider from %u to %u\n",
  307. i, div_from_reg(data->fan_div[i]),
  308. div_from_reg(data->fan_div[i] + 1));
  309. data->fan_div[i]++;
  310. w83627ehf_write_fan_div(client, i);
  311. /* Preserve min limit if possible */
  312. if (data->fan_min[i] >= 2
  313. && data->fan_min[i] != 255)
  314. w83627ehf_write_value(client,
  315. W83627EHF_REG_FAN_MIN[i],
  316. (data->fan_min[i] /= 2));
  317. }
  318. }
  319. /* Measured temperatures and limits */
  320. data->temp1 = w83627ehf_read_value(client,
  321. W83627EHF_REG_TEMP1);
  322. data->temp1_max = w83627ehf_read_value(client,
  323. W83627EHF_REG_TEMP1_OVER);
  324. data->temp1_max_hyst = w83627ehf_read_value(client,
  325. W83627EHF_REG_TEMP1_HYST);
  326. for (i = 0; i < 2; i++) {
  327. data->temp[i] = w83627ehf_read_value(client,
  328. W83627EHF_REG_TEMP[i]);
  329. data->temp_max[i] = w83627ehf_read_value(client,
  330. W83627EHF_REG_TEMP_OVER[i]);
  331. data->temp_max_hyst[i] = w83627ehf_read_value(client,
  332. W83627EHF_REG_TEMP_HYST[i]);
  333. }
  334. data->last_updated = jiffies;
  335. data->valid = 1;
  336. }
  337. up(&data->update_lock);
  338. return data;
  339. }
  340. /*
  341. * Sysfs callback functions
  342. */
  343. #define show_fan_reg(reg) \
  344. static ssize_t \
  345. show_##reg(struct device *dev, char *buf, int nr) \
  346. { \
  347. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  348. return sprintf(buf, "%d\n", \
  349. fan_from_reg(data->reg[nr], \
  350. div_from_reg(data->fan_div[nr]))); \
  351. }
  352. show_fan_reg(fan);
  353. show_fan_reg(fan_min);
  354. static ssize_t
  355. show_fan_div(struct device *dev, char *buf, int nr)
  356. {
  357. struct w83627ehf_data *data = w83627ehf_update_device(dev);
  358. return sprintf(buf, "%u\n",
  359. div_from_reg(data->fan_div[nr]));
  360. }
  361. static ssize_t
  362. store_fan_min(struct device *dev, const char *buf, size_t count, int nr)
  363. {
  364. struct i2c_client *client = to_i2c_client(dev);
  365. struct w83627ehf_data *data = i2c_get_clientdata(client);
  366. unsigned int val = simple_strtoul(buf, NULL, 10);
  367. unsigned int reg;
  368. u8 new_div;
  369. down(&data->update_lock);
  370. if (!val) {
  371. /* No min limit, alarm disabled */
  372. data->fan_min[nr] = 255;
  373. new_div = data->fan_div[nr]; /* No change */
  374. dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
  375. } else if ((reg = 1350000U / val) >= 128 * 255) {
  376. /* Speed below this value cannot possibly be represented,
  377. even with the highest divider (128) */
  378. data->fan_min[nr] = 254;
  379. new_div = 7; /* 128 == (1 << 7) */
  380. dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
  381. "minimum\n", nr + 1, val, fan_from_reg(254, 128));
  382. } else if (!reg) {
  383. /* Speed above this value cannot possibly be represented,
  384. even with the lowest divider (1) */
  385. data->fan_min[nr] = 1;
  386. new_div = 0; /* 1 == (1 << 0) */
  387. dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
  388. "maximum\n", nr + 1, val, fan_from_reg(1, 1));
  389. } else {
  390. /* Automatically pick the best divider, i.e. the one such
  391. that the min limit will correspond to a register value
  392. in the 96..192 range */
  393. new_div = 0;
  394. while (reg > 192 && new_div < 7) {
  395. reg >>= 1;
  396. new_div++;
  397. }
  398. data->fan_min[nr] = reg;
  399. }
  400. /* Write both the fan clock divider (if it changed) and the new
  401. fan min (unconditionally) */
  402. if (new_div != data->fan_div[nr]) {
  403. if (new_div > data->fan_div[nr])
  404. data->fan[nr] >>= (data->fan_div[nr] - new_div);
  405. else
  406. data->fan[nr] <<= (new_div - data->fan_div[nr]);
  407. dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
  408. nr + 1, div_from_reg(data->fan_div[nr]),
  409. div_from_reg(new_div));
  410. data->fan_div[nr] = new_div;
  411. w83627ehf_write_fan_div(client, nr);
  412. }
  413. w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
  414. data->fan_min[nr]);
  415. up(&data->update_lock);
  416. return count;
  417. }
  418. #define sysfs_fan_offset(offset) \
  419. static ssize_t \
  420. show_reg_fan_##offset(struct device *dev, struct device_attribute *attr, \
  421. char *buf) \
  422. { \
  423. return show_fan(dev, buf, offset-1); \
  424. } \
  425. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  426. show_reg_fan_##offset, NULL);
  427. #define sysfs_fan_min_offset(offset) \
  428. static ssize_t \
  429. show_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
  430. char *buf) \
  431. { \
  432. return show_fan_min(dev, buf, offset-1); \
  433. } \
  434. static ssize_t \
  435. store_reg_fan##offset##_min(struct device *dev, struct device_attribute *attr, \
  436. const char *buf, size_t count) \
  437. { \
  438. return store_fan_min(dev, buf, count, offset-1); \
  439. } \
  440. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  441. show_reg_fan##offset##_min, \
  442. store_reg_fan##offset##_min);
  443. #define sysfs_fan_div_offset(offset) \
  444. static ssize_t \
  445. show_reg_fan##offset##_div(struct device *dev, struct device_attribute *attr, \
  446. char *buf) \
  447. { \
  448. return show_fan_div(dev, buf, offset - 1); \
  449. } \
  450. static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
  451. show_reg_fan##offset##_div, NULL);
  452. sysfs_fan_offset(1);
  453. sysfs_fan_min_offset(1);
  454. sysfs_fan_div_offset(1);
  455. sysfs_fan_offset(2);
  456. sysfs_fan_min_offset(2);
  457. sysfs_fan_div_offset(2);
  458. sysfs_fan_offset(3);
  459. sysfs_fan_min_offset(3);
  460. sysfs_fan_div_offset(3);
  461. sysfs_fan_offset(4);
  462. sysfs_fan_min_offset(4);
  463. sysfs_fan_div_offset(4);
  464. sysfs_fan_offset(5);
  465. sysfs_fan_min_offset(5);
  466. sysfs_fan_div_offset(5);
  467. #define show_temp1_reg(reg) \
  468. static ssize_t \
  469. show_##reg(struct device *dev, struct device_attribute *attr, \
  470. char *buf) \
  471. { \
  472. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  473. return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
  474. }
  475. show_temp1_reg(temp1);
  476. show_temp1_reg(temp1_max);
  477. show_temp1_reg(temp1_max_hyst);
  478. #define store_temp1_reg(REG, reg) \
  479. static ssize_t \
  480. store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
  481. const char *buf, size_t count) \
  482. { \
  483. struct i2c_client *client = to_i2c_client(dev); \
  484. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  485. u32 val = simple_strtoul(buf, NULL, 10); \
  486. \
  487. down(&data->update_lock); \
  488. data->temp1_##reg = temp1_to_reg(val); \
  489. w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
  490. data->temp1_##reg); \
  491. up(&data->update_lock); \
  492. return count; \
  493. }
  494. store_temp1_reg(OVER, max);
  495. store_temp1_reg(HYST, max_hyst);
  496. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp1, NULL);
  497. static DEVICE_ATTR(temp1_max, S_IRUGO| S_IWUSR,
  498. show_temp1_max, store_temp1_max);
  499. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO| S_IWUSR,
  500. show_temp1_max_hyst, store_temp1_max_hyst);
  501. #define show_temp_reg(reg) \
  502. static ssize_t \
  503. show_##reg (struct device *dev, char *buf, int nr) \
  504. { \
  505. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  506. return sprintf(buf, "%d\n", \
  507. LM75_TEMP_FROM_REG(data->reg[nr])); \
  508. }
  509. show_temp_reg(temp);
  510. show_temp_reg(temp_max);
  511. show_temp_reg(temp_max_hyst);
  512. #define store_temp_reg(REG, reg) \
  513. static ssize_t \
  514. store_##reg (struct device *dev, const char *buf, size_t count, int nr) \
  515. { \
  516. struct i2c_client *client = to_i2c_client(dev); \
  517. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  518. u32 val = simple_strtoul(buf, NULL, 10); \
  519. \
  520. down(&data->update_lock); \
  521. data->reg[nr] = LM75_TEMP_TO_REG(val); \
  522. w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
  523. data->reg[nr]); \
  524. up(&data->update_lock); \
  525. return count; \
  526. }
  527. store_temp_reg(OVER, temp_max);
  528. store_temp_reg(HYST, temp_max_hyst);
  529. #define sysfs_temp_offset(offset) \
  530. static ssize_t \
  531. show_reg_temp##offset (struct device *dev, struct device_attribute *attr, \
  532. char *buf) \
  533. { \
  534. return show_temp(dev, buf, offset - 2); \
  535. } \
  536. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  537. show_reg_temp##offset, NULL);
  538. #define sysfs_temp_reg_offset(reg, offset) \
  539. static ssize_t \
  540. show_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
  541. char *buf) \
  542. { \
  543. return show_temp_##reg(dev, buf, offset - 2); \
  544. } \
  545. static ssize_t \
  546. store_reg_temp##offset##_##reg(struct device *dev, struct device_attribute *attr, \
  547. const char *buf, size_t count) \
  548. { \
  549. return store_temp_##reg(dev, buf, count, offset - 2); \
  550. } \
  551. static DEVICE_ATTR(temp##offset##_##reg, S_IRUGO| S_IWUSR, \
  552. show_reg_temp##offset##_##reg, \
  553. store_reg_temp##offset##_##reg);
  554. sysfs_temp_offset(2);
  555. sysfs_temp_reg_offset(max, 2);
  556. sysfs_temp_reg_offset(max_hyst, 2);
  557. sysfs_temp_offset(3);
  558. sysfs_temp_reg_offset(max, 3);
  559. sysfs_temp_reg_offset(max_hyst, 3);
  560. /*
  561. * Driver and client management
  562. */
  563. static struct i2c_driver w83627ehf_driver;
  564. static void w83627ehf_init_client(struct i2c_client *client)
  565. {
  566. int i;
  567. u8 tmp;
  568. /* Start monitoring is needed */
  569. tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG);
  570. if (!(tmp & 0x01))
  571. w83627ehf_write_value(client, W83627EHF_REG_CONFIG,
  572. tmp | 0x01);
  573. /* Enable temp2 and temp3 if needed */
  574. for (i = 0; i < 2; i++) {
  575. tmp = w83627ehf_read_value(client,
  576. W83627EHF_REG_TEMP_CONFIG[i]);
  577. if (tmp & 0x01)
  578. w83627ehf_write_value(client,
  579. W83627EHF_REG_TEMP_CONFIG[i],
  580. tmp & 0xfe);
  581. }
  582. }
  583. static int w83627ehf_detect(struct i2c_adapter *adapter, int address, int kind)
  584. {
  585. struct i2c_client *client;
  586. struct w83627ehf_data *data;
  587. int i, err = 0;
  588. if (!i2c_is_isa_adapter(adapter))
  589. return 0;
  590. if (!request_region(address, REGION_LENGTH, w83627ehf_driver.name)) {
  591. err = -EBUSY;
  592. goto exit;
  593. }
  594. if (!(data = kmalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
  595. err = -ENOMEM;
  596. goto exit_release;
  597. }
  598. memset(data, 0, sizeof(struct w83627ehf_data));
  599. client = &data->client;
  600. i2c_set_clientdata(client, data);
  601. client->addr = address;
  602. init_MUTEX(&data->lock);
  603. client->adapter = adapter;
  604. client->driver = &w83627ehf_driver;
  605. client->flags = 0;
  606. strlcpy(client->name, "w83627ehf", I2C_NAME_SIZE);
  607. data->valid = 0;
  608. init_MUTEX(&data->update_lock);
  609. /* Tell the i2c layer a new client has arrived */
  610. if ((err = i2c_attach_client(client)))
  611. goto exit_free;
  612. /* Initialize the chip */
  613. w83627ehf_init_client(client);
  614. /* A few vars need to be filled upon startup */
  615. for (i = 0; i < 5; i++)
  616. data->fan_min[i] = w83627ehf_read_value(client,
  617. W83627EHF_REG_FAN_MIN[i]);
  618. /* It looks like fan4 and fan5 pins can be alternatively used
  619. as fan on/off switches */
  620. data->has_fan = 0x07; /* fan1, fan2 and fan3 */
  621. i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
  622. if (i & (1 << 2))
  623. data->has_fan |= (1 << 3);
  624. if (i & (1 << 0))
  625. data->has_fan |= (1 << 4);
  626. /* Register sysfs hooks */
  627. device_create_file(&client->dev, &dev_attr_fan1_input);
  628. device_create_file(&client->dev, &dev_attr_fan1_min);
  629. device_create_file(&client->dev, &dev_attr_fan1_div);
  630. device_create_file(&client->dev, &dev_attr_fan2_input);
  631. device_create_file(&client->dev, &dev_attr_fan2_min);
  632. device_create_file(&client->dev, &dev_attr_fan2_div);
  633. device_create_file(&client->dev, &dev_attr_fan3_input);
  634. device_create_file(&client->dev, &dev_attr_fan3_min);
  635. device_create_file(&client->dev, &dev_attr_fan3_div);
  636. if (data->has_fan & (1 << 3)) {
  637. device_create_file(&client->dev, &dev_attr_fan4_input);
  638. device_create_file(&client->dev, &dev_attr_fan4_min);
  639. device_create_file(&client->dev, &dev_attr_fan4_div);
  640. }
  641. if (data->has_fan & (1 << 4)) {
  642. device_create_file(&client->dev, &dev_attr_fan5_input);
  643. device_create_file(&client->dev, &dev_attr_fan5_min);
  644. device_create_file(&client->dev, &dev_attr_fan5_div);
  645. }
  646. device_create_file(&client->dev, &dev_attr_temp1_input);
  647. device_create_file(&client->dev, &dev_attr_temp1_max);
  648. device_create_file(&client->dev, &dev_attr_temp1_max_hyst);
  649. device_create_file(&client->dev, &dev_attr_temp2_input);
  650. device_create_file(&client->dev, &dev_attr_temp2_max);
  651. device_create_file(&client->dev, &dev_attr_temp2_max_hyst);
  652. device_create_file(&client->dev, &dev_attr_temp3_input);
  653. device_create_file(&client->dev, &dev_attr_temp3_max);
  654. device_create_file(&client->dev, &dev_attr_temp3_max_hyst);
  655. return 0;
  656. exit_free:
  657. kfree(data);
  658. exit_release:
  659. release_region(address, REGION_LENGTH);
  660. exit:
  661. return err;
  662. }
  663. static int w83627ehf_attach_adapter(struct i2c_adapter *adapter)
  664. {
  665. if (!(adapter->class & I2C_CLASS_HWMON))
  666. return 0;
  667. return i2c_detect(adapter, &addr_data, w83627ehf_detect);
  668. }
  669. static int w83627ehf_detach_client(struct i2c_client *client)
  670. {
  671. int err;
  672. if ((err = i2c_detach_client(client))) {
  673. dev_err(&client->dev, "Client deregistration failed, "
  674. "client not detached.\n");
  675. return err;
  676. }
  677. release_region(client->addr, REGION_LENGTH);
  678. kfree(i2c_get_clientdata(client));
  679. return 0;
  680. }
  681. static struct i2c_driver w83627ehf_driver = {
  682. .owner = THIS_MODULE,
  683. .name = "w83627ehf",
  684. .flags = I2C_DF_NOTIFY,
  685. .attach_adapter = w83627ehf_attach_adapter,
  686. .detach_client = w83627ehf_detach_client,
  687. };
  688. static int __init w83627ehf_find(int sioaddr, int *address)
  689. {
  690. u16 val;
  691. REG = sioaddr;
  692. VAL = sioaddr + 1;
  693. superio_enter();
  694. val = (superio_inb(SIO_REG_DEVID) << 8)
  695. | superio_inb(SIO_REG_DEVID + 1);
  696. if ((val & SIO_ID_MASK) != SIO_W83627EHF_ID) {
  697. superio_exit();
  698. return -ENODEV;
  699. }
  700. superio_select(W83627EHF_LD_HWM);
  701. val = (superio_inb(SIO_REG_ADDR) << 8)
  702. | superio_inb(SIO_REG_ADDR + 1);
  703. *address = val & ~(REGION_LENGTH - 1);
  704. if (*address == 0) {
  705. superio_exit();
  706. return -ENODEV;
  707. }
  708. /* Activate logical device if needed */
  709. val = superio_inb(SIO_REG_ENABLE);
  710. if (!(val & 0x01))
  711. superio_outb(SIO_REG_ENABLE, val | 0x01);
  712. superio_exit();
  713. return 0;
  714. }
  715. static int __init sensors_w83627ehf_init(void)
  716. {
  717. if (w83627ehf_find(0x2e, &normal_isa[0])
  718. && w83627ehf_find(0x4e, &normal_isa[0]))
  719. return -ENODEV;
  720. return i2c_add_driver(&w83627ehf_driver);
  721. }
  722. static void __exit sensors_w83627ehf_exit(void)
  723. {
  724. i2c_del_driver(&w83627ehf_driver);
  725. }
  726. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  727. MODULE_DESCRIPTION("W83627EHF driver");
  728. MODULE_LICENSE("GPL");
  729. module_init(sensors_w83627ehf_init);
  730. module_exit(sensors_w83627ehf_exit);