w83627ehf.c 24 KB

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