w83627ehf.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  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. Copyright (C) 2006 Yuan Mu <Ymu@Winbond.com.tw>,
  6. Rudolf Marek <r.marek@sh.cvut.cz>
  7. Shamelessly ripped from the w83627hf driver
  8. Copyright (C) 2003 Mark Studebaker
  9. Thanks to Leon Moonen, Steve Cliffe and Grant Coady for their help
  10. in testing and debugging this driver.
  11. This driver also supports the W83627EHG, which is the lead-free
  12. version of the W83627EHF.
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. Supports the following chips:
  25. Chip #vin #fan #pwm #temp chip_id man_id
  26. w83627ehf 10 5 4 3 0x88,0xa1 0x5ca3
  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-isa.h>
  33. #include <linux/hwmon.h>
  34. #include <linux/hwmon-sysfs.h>
  35. #include <linux/err.h>
  36. #include <linux/mutex.h>
  37. #include <asm/io.h>
  38. #include "lm75.h"
  39. /* The actual ISA address is read from Super-I/O configuration space */
  40. static unsigned short address;
  41. /*
  42. * Super-I/O constants and functions
  43. */
  44. static int REG; /* The register to read/write */
  45. static int VAL; /* The value to read/write */
  46. #define W83627EHF_LD_HWM 0x0b
  47. #define SIO_REG_LDSEL 0x07 /* Logical device select */
  48. #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */
  49. #define SIO_REG_ENABLE 0x30 /* Logical device enable */
  50. #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */
  51. #define SIO_W83627EHF_ID 0x8840
  52. #define SIO_ID_MASK 0xFFC0
  53. static inline void
  54. superio_outb(int reg, int val)
  55. {
  56. outb(reg, REG);
  57. outb(val, VAL);
  58. }
  59. static inline int
  60. superio_inb(int reg)
  61. {
  62. outb(reg, REG);
  63. return inb(VAL);
  64. }
  65. static inline void
  66. superio_select(int ld)
  67. {
  68. outb(SIO_REG_LDSEL, REG);
  69. outb(ld, VAL);
  70. }
  71. static inline void
  72. superio_enter(void)
  73. {
  74. outb(0x87, REG);
  75. outb(0x87, REG);
  76. }
  77. static inline void
  78. superio_exit(void)
  79. {
  80. outb(0x02, REG);
  81. outb(0x02, VAL);
  82. }
  83. /*
  84. * ISA constants
  85. */
  86. #define REGION_ALIGNMENT ~7
  87. #define REGION_OFFSET 5
  88. #define REGION_LENGTH 2
  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. /* The W83627EHF registers for nr=7,8,9 are in bank 5 */
  98. #define W83627EHF_REG_IN_MAX(nr) ((nr < 7) ? (0x2b + (nr) * 2) : \
  99. (0x554 + (((nr) - 7) * 2)))
  100. #define W83627EHF_REG_IN_MIN(nr) ((nr < 7) ? (0x2c + (nr) * 2) : \
  101. (0x555 + (((nr) - 7) * 2)))
  102. #define W83627EHF_REG_IN(nr) ((nr < 7) ? (0x20 + (nr)) : \
  103. (0x550 + (nr) - 7))
  104. #define W83627EHF_REG_TEMP1 0x27
  105. #define W83627EHF_REG_TEMP1_HYST 0x3a
  106. #define W83627EHF_REG_TEMP1_OVER 0x39
  107. static const u16 W83627EHF_REG_TEMP[] = { 0x150, 0x250 };
  108. static const u16 W83627EHF_REG_TEMP_HYST[] = { 0x153, 0x253 };
  109. static const u16 W83627EHF_REG_TEMP_OVER[] = { 0x155, 0x255 };
  110. static const u16 W83627EHF_REG_TEMP_CONFIG[] = { 0x152, 0x252 };
  111. /* Fan clock dividers are spread over the following five registers */
  112. #define W83627EHF_REG_FANDIV1 0x47
  113. #define W83627EHF_REG_FANDIV2 0x4B
  114. #define W83627EHF_REG_VBAT 0x5D
  115. #define W83627EHF_REG_DIODE 0x59
  116. #define W83627EHF_REG_SMI_OVT 0x4C
  117. #define W83627EHF_REG_ALARM1 0x459
  118. #define W83627EHF_REG_ALARM2 0x45A
  119. #define W83627EHF_REG_ALARM3 0x45B
  120. /* SmartFan registers */
  121. /* DC or PWM output fan configuration */
  122. static const u8 W83627EHF_REG_PWM_ENABLE[] = {
  123. 0x04, /* SYS FAN0 output mode and PWM mode */
  124. 0x04, /* CPU FAN0 output mode and PWM mode */
  125. 0x12, /* AUX FAN mode */
  126. 0x62, /* CPU fan1 mode */
  127. };
  128. static const u8 W83627EHF_PWM_MODE_SHIFT[] = { 0, 1, 0, 6 };
  129. static const u8 W83627EHF_PWM_ENABLE_SHIFT[] = { 2, 4, 1, 4 };
  130. /* FAN Duty Cycle, be used to control */
  131. static const u8 W83627EHF_REG_PWM[] = { 0x01, 0x03, 0x11, 0x61 };
  132. static const u8 W83627EHF_REG_TARGET[] = { 0x05, 0x06, 0x13, 0x63 };
  133. static const u8 W83627EHF_REG_TOLERANCE[] = { 0x07, 0x07, 0x14, 0x62 };
  134. /* Advanced Fan control, some values are common for all fans */
  135. static const u8 W83627EHF_REG_FAN_MIN_OUTPUT[] = { 0x08, 0x09, 0x15, 0x64 };
  136. static const u8 W83627EHF_REG_FAN_STOP_TIME[] = { 0x0C, 0x0D, 0x17, 0x66 };
  137. /*
  138. * Conversions
  139. */
  140. /* 1 is PWM mode, output in ms */
  141. static inline unsigned int step_time_from_reg(u8 reg, u8 mode)
  142. {
  143. return mode ? 100 * reg : 400 * reg;
  144. }
  145. static inline u8 step_time_to_reg(unsigned int msec, u8 mode)
  146. {
  147. return SENSORS_LIMIT((mode ? (msec + 50) / 100 :
  148. (msec + 200) / 400), 1, 255);
  149. }
  150. static inline unsigned int
  151. fan_from_reg(u8 reg, unsigned int div)
  152. {
  153. if (reg == 0 || reg == 255)
  154. return 0;
  155. return 1350000U / (reg * div);
  156. }
  157. static inline unsigned int
  158. div_from_reg(u8 reg)
  159. {
  160. return 1 << reg;
  161. }
  162. static inline int
  163. temp1_from_reg(s8 reg)
  164. {
  165. return reg * 1000;
  166. }
  167. static inline s8
  168. temp1_to_reg(int temp, int min, int max)
  169. {
  170. if (temp <= min)
  171. return min / 1000;
  172. if (temp >= max)
  173. return max / 1000;
  174. if (temp < 0)
  175. return (temp - 500) / 1000;
  176. return (temp + 500) / 1000;
  177. }
  178. /* Some of analog inputs have internal scaling (2x), 8mV is ADC LSB */
  179. static u8 scale_in[10] = { 8, 8, 16, 16, 8, 8, 8, 16, 16, 8 };
  180. static inline long in_from_reg(u8 reg, u8 nr)
  181. {
  182. return reg * scale_in[nr];
  183. }
  184. static inline u8 in_to_reg(u32 val, u8 nr)
  185. {
  186. return SENSORS_LIMIT(((val + (scale_in[nr] / 2)) / scale_in[nr]), 0, 255);
  187. }
  188. /*
  189. * Data structures and manipulation thereof
  190. */
  191. struct w83627ehf_data {
  192. struct i2c_client client;
  193. struct class_device *class_dev;
  194. struct mutex lock;
  195. struct mutex update_lock;
  196. char valid; /* !=0 if following fields are valid */
  197. unsigned long last_updated; /* In jiffies */
  198. /* Register values */
  199. u8 in[10]; /* Register value */
  200. u8 in_max[10]; /* Register value */
  201. u8 in_min[10]; /* Register value */
  202. u8 fan[5];
  203. u8 fan_min[5];
  204. u8 fan_div[5];
  205. u8 has_fan; /* some fan inputs can be disabled */
  206. s8 temp1;
  207. s8 temp1_max;
  208. s8 temp1_max_hyst;
  209. s16 temp[2];
  210. s16 temp_max[2];
  211. s16 temp_max_hyst[2];
  212. u32 alarms;
  213. u8 pwm_mode[4]; /* 0->DC variable voltage, 1->PWM variable duty cycle */
  214. u8 pwm_enable[4]; /* 1->manual
  215. 2->thermal cruise (also called SmartFan I) */
  216. u8 pwm[4];
  217. u8 target_temp[4];
  218. u8 tolerance[4];
  219. u8 fan_min_output[4]; /* minimum fan speed */
  220. u8 fan_stop_time[4];
  221. };
  222. static inline int is_word_sized(u16 reg)
  223. {
  224. return (((reg & 0xff00) == 0x100
  225. || (reg & 0xff00) == 0x200)
  226. && ((reg & 0x00ff) == 0x50
  227. || (reg & 0x00ff) == 0x53
  228. || (reg & 0x00ff) == 0x55));
  229. }
  230. /* We assume that the default bank is 0, thus the following two functions do
  231. nothing for registers which live in bank 0. For others, they respectively
  232. set the bank register to the correct value (before the register is
  233. accessed), and back to 0 (afterwards). */
  234. static inline void w83627ehf_set_bank(struct i2c_client *client, u16 reg)
  235. {
  236. if (reg & 0xff00) {
  237. outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
  238. outb_p(reg >> 8, client->addr + DATA_REG_OFFSET);
  239. }
  240. }
  241. static inline void w83627ehf_reset_bank(struct i2c_client *client, u16 reg)
  242. {
  243. if (reg & 0xff00) {
  244. outb_p(W83627EHF_REG_BANK, client->addr + ADDR_REG_OFFSET);
  245. outb_p(0, client->addr + DATA_REG_OFFSET);
  246. }
  247. }
  248. static u16 w83627ehf_read_value(struct i2c_client *client, u16 reg)
  249. {
  250. struct w83627ehf_data *data = i2c_get_clientdata(client);
  251. int res, word_sized = is_word_sized(reg);
  252. mutex_lock(&data->lock);
  253. w83627ehf_set_bank(client, reg);
  254. outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
  255. res = inb_p(client->addr + DATA_REG_OFFSET);
  256. if (word_sized) {
  257. outb_p((reg & 0xff) + 1,
  258. client->addr + ADDR_REG_OFFSET);
  259. res = (res << 8) + inb_p(client->addr + DATA_REG_OFFSET);
  260. }
  261. w83627ehf_reset_bank(client, reg);
  262. mutex_unlock(&data->lock);
  263. return res;
  264. }
  265. static int w83627ehf_write_value(struct i2c_client *client, u16 reg, u16 value)
  266. {
  267. struct w83627ehf_data *data = i2c_get_clientdata(client);
  268. int word_sized = is_word_sized(reg);
  269. mutex_lock(&data->lock);
  270. w83627ehf_set_bank(client, reg);
  271. outb_p(reg & 0xff, client->addr + ADDR_REG_OFFSET);
  272. if (word_sized) {
  273. outb_p(value >> 8, client->addr + DATA_REG_OFFSET);
  274. outb_p((reg & 0xff) + 1,
  275. client->addr + ADDR_REG_OFFSET);
  276. }
  277. outb_p(value & 0xff, client->addr + DATA_REG_OFFSET);
  278. w83627ehf_reset_bank(client, reg);
  279. mutex_unlock(&data->lock);
  280. return 0;
  281. }
  282. /* This function assumes that the caller holds data->update_lock */
  283. static void w83627ehf_write_fan_div(struct i2c_client *client, int nr)
  284. {
  285. struct w83627ehf_data *data = i2c_get_clientdata(client);
  286. u8 reg;
  287. switch (nr) {
  288. case 0:
  289. reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0xcf)
  290. | ((data->fan_div[0] & 0x03) << 4);
  291. w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
  292. reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xdf)
  293. | ((data->fan_div[0] & 0x04) << 3);
  294. w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
  295. break;
  296. case 1:
  297. reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV1) & 0x3f)
  298. | ((data->fan_div[1] & 0x03) << 6);
  299. w83627ehf_write_value(client, W83627EHF_REG_FANDIV1, reg);
  300. reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0xbf)
  301. | ((data->fan_div[1] & 0x04) << 4);
  302. w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
  303. break;
  304. case 2:
  305. reg = (w83627ehf_read_value(client, W83627EHF_REG_FANDIV2) & 0x3f)
  306. | ((data->fan_div[2] & 0x03) << 6);
  307. w83627ehf_write_value(client, W83627EHF_REG_FANDIV2, reg);
  308. reg = (w83627ehf_read_value(client, W83627EHF_REG_VBAT) & 0x7f)
  309. | ((data->fan_div[2] & 0x04) << 5);
  310. w83627ehf_write_value(client, W83627EHF_REG_VBAT, reg);
  311. break;
  312. case 3:
  313. reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0xfc)
  314. | (data->fan_div[3] & 0x03);
  315. w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
  316. reg = (w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT) & 0x7f)
  317. | ((data->fan_div[3] & 0x04) << 5);
  318. w83627ehf_write_value(client, W83627EHF_REG_SMI_OVT, reg);
  319. break;
  320. case 4:
  321. reg = (w83627ehf_read_value(client, W83627EHF_REG_DIODE) & 0x73)
  322. | ((data->fan_div[4] & 0x03) << 3)
  323. | ((data->fan_div[4] & 0x04) << 5);
  324. w83627ehf_write_value(client, W83627EHF_REG_DIODE, reg);
  325. break;
  326. }
  327. }
  328. static struct w83627ehf_data *w83627ehf_update_device(struct device *dev)
  329. {
  330. struct i2c_client *client = to_i2c_client(dev);
  331. struct w83627ehf_data *data = i2c_get_clientdata(client);
  332. int pwmcfg = 0, tolerance = 0; /* shut up the compiler */
  333. int i;
  334. mutex_lock(&data->update_lock);
  335. if (time_after(jiffies, data->last_updated + HZ)
  336. || !data->valid) {
  337. /* Fan clock dividers */
  338. i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
  339. data->fan_div[0] = (i >> 4) & 0x03;
  340. data->fan_div[1] = (i >> 6) & 0x03;
  341. i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV2);
  342. data->fan_div[2] = (i >> 6) & 0x03;
  343. i = w83627ehf_read_value(client, W83627EHF_REG_VBAT);
  344. data->fan_div[0] |= (i >> 3) & 0x04;
  345. data->fan_div[1] |= (i >> 4) & 0x04;
  346. data->fan_div[2] |= (i >> 5) & 0x04;
  347. if (data->has_fan & ((1 << 3) | (1 << 4))) {
  348. i = w83627ehf_read_value(client, W83627EHF_REG_DIODE);
  349. data->fan_div[3] = i & 0x03;
  350. data->fan_div[4] = ((i >> 2) & 0x03)
  351. | ((i >> 5) & 0x04);
  352. }
  353. if (data->has_fan & (1 << 3)) {
  354. i = w83627ehf_read_value(client, W83627EHF_REG_SMI_OVT);
  355. data->fan_div[3] |= (i >> 5) & 0x04;
  356. }
  357. /* Measured voltages and limits */
  358. for (i = 0; i < 10; i++) {
  359. data->in[i] = w83627ehf_read_value(client,
  360. W83627EHF_REG_IN(i));
  361. data->in_min[i] = w83627ehf_read_value(client,
  362. W83627EHF_REG_IN_MIN(i));
  363. data->in_max[i] = w83627ehf_read_value(client,
  364. W83627EHF_REG_IN_MAX(i));
  365. }
  366. /* Measured fan speeds and limits */
  367. for (i = 0; i < 5; i++) {
  368. if (!(data->has_fan & (1 << i)))
  369. continue;
  370. data->fan[i] = w83627ehf_read_value(client,
  371. W83627EHF_REG_FAN[i]);
  372. data->fan_min[i] = w83627ehf_read_value(client,
  373. W83627EHF_REG_FAN_MIN[i]);
  374. /* If we failed to measure the fan speed and clock
  375. divider can be increased, let's try that for next
  376. time */
  377. if (data->fan[i] == 0xff
  378. && data->fan_div[i] < 0x07) {
  379. dev_dbg(&client->dev, "Increasing fan %d "
  380. "clock divider from %u to %u\n",
  381. i, div_from_reg(data->fan_div[i]),
  382. div_from_reg(data->fan_div[i] + 1));
  383. data->fan_div[i]++;
  384. w83627ehf_write_fan_div(client, i);
  385. /* Preserve min limit if possible */
  386. if (data->fan_min[i] >= 2
  387. && data->fan_min[i] != 255)
  388. w83627ehf_write_value(client,
  389. W83627EHF_REG_FAN_MIN[i],
  390. (data->fan_min[i] /= 2));
  391. }
  392. }
  393. for (i = 0; i < 4; i++) {
  394. /* pwmcfg, tolarance mapped for i=0, i=1 to same reg */
  395. if (i != 1) {
  396. pwmcfg = w83627ehf_read_value(client,
  397. W83627EHF_REG_PWM_ENABLE[i]);
  398. tolerance = w83627ehf_read_value(client,
  399. W83627EHF_REG_TOLERANCE[i]);
  400. }
  401. data->pwm_mode[i] =
  402. ((pwmcfg >> W83627EHF_PWM_MODE_SHIFT[i]) & 1)
  403. ? 0 : 1;
  404. data->pwm_enable[i] =
  405. ((pwmcfg >> W83627EHF_PWM_ENABLE_SHIFT[i])
  406. & 3) + 1;
  407. data->pwm[i] = w83627ehf_read_value(client,
  408. W83627EHF_REG_PWM[i]);
  409. data->fan_min_output[i] = w83627ehf_read_value(client,
  410. W83627EHF_REG_FAN_MIN_OUTPUT[i]);
  411. data->fan_stop_time[i] = w83627ehf_read_value(client,
  412. W83627EHF_REG_FAN_STOP_TIME[i]);
  413. data->target_temp[i] =
  414. w83627ehf_read_value(client,
  415. W83627EHF_REG_TARGET[i]) &
  416. (data->pwm_mode[i] == 1 ? 0x7f : 0xff);
  417. data->tolerance[i] = (tolerance >> (i == 1 ? 4 : 0))
  418. & 0x0f;
  419. }
  420. /* Measured temperatures and limits */
  421. data->temp1 = w83627ehf_read_value(client,
  422. W83627EHF_REG_TEMP1);
  423. data->temp1_max = w83627ehf_read_value(client,
  424. W83627EHF_REG_TEMP1_OVER);
  425. data->temp1_max_hyst = w83627ehf_read_value(client,
  426. W83627EHF_REG_TEMP1_HYST);
  427. for (i = 0; i < 2; i++) {
  428. data->temp[i] = w83627ehf_read_value(client,
  429. W83627EHF_REG_TEMP[i]);
  430. data->temp_max[i] = w83627ehf_read_value(client,
  431. W83627EHF_REG_TEMP_OVER[i]);
  432. data->temp_max_hyst[i] = w83627ehf_read_value(client,
  433. W83627EHF_REG_TEMP_HYST[i]);
  434. }
  435. data->alarms = w83627ehf_read_value(client,
  436. W83627EHF_REG_ALARM1) |
  437. (w83627ehf_read_value(client,
  438. W83627EHF_REG_ALARM2) << 8) |
  439. (w83627ehf_read_value(client,
  440. W83627EHF_REG_ALARM3) << 16);
  441. data->last_updated = jiffies;
  442. data->valid = 1;
  443. }
  444. mutex_unlock(&data->update_lock);
  445. return data;
  446. }
  447. /*
  448. * Sysfs callback functions
  449. */
  450. #define show_in_reg(reg) \
  451. static ssize_t \
  452. show_##reg(struct device *dev, struct device_attribute *attr, \
  453. char *buf) \
  454. { \
  455. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  456. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  457. int nr = sensor_attr->index; \
  458. return sprintf(buf, "%ld\n", in_from_reg(data->reg[nr], nr)); \
  459. }
  460. show_in_reg(in)
  461. show_in_reg(in_min)
  462. show_in_reg(in_max)
  463. #define store_in_reg(REG, reg) \
  464. static ssize_t \
  465. store_in_##reg (struct device *dev, struct device_attribute *attr, \
  466. const char *buf, size_t count) \
  467. { \
  468. struct i2c_client *client = to_i2c_client(dev); \
  469. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  470. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  471. int nr = sensor_attr->index; \
  472. u32 val = simple_strtoul(buf, NULL, 10); \
  473. \
  474. mutex_lock(&data->update_lock); \
  475. data->in_##reg[nr] = in_to_reg(val, nr); \
  476. w83627ehf_write_value(client, W83627EHF_REG_IN_##REG(nr), \
  477. data->in_##reg[nr]); \
  478. mutex_unlock(&data->update_lock); \
  479. return count; \
  480. }
  481. store_in_reg(MIN, min)
  482. store_in_reg(MAX, max)
  483. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, char *buf)
  484. {
  485. struct w83627ehf_data *data = w83627ehf_update_device(dev);
  486. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  487. int nr = sensor_attr->index;
  488. return sprintf(buf, "%u\n", (data->alarms >> nr) & 0x01);
  489. }
  490. static struct sensor_device_attribute sda_in_input[] = {
  491. SENSOR_ATTR(in0_input, S_IRUGO, show_in, NULL, 0),
  492. SENSOR_ATTR(in1_input, S_IRUGO, show_in, NULL, 1),
  493. SENSOR_ATTR(in2_input, S_IRUGO, show_in, NULL, 2),
  494. SENSOR_ATTR(in3_input, S_IRUGO, show_in, NULL, 3),
  495. SENSOR_ATTR(in4_input, S_IRUGO, show_in, NULL, 4),
  496. SENSOR_ATTR(in5_input, S_IRUGO, show_in, NULL, 5),
  497. SENSOR_ATTR(in6_input, S_IRUGO, show_in, NULL, 6),
  498. SENSOR_ATTR(in7_input, S_IRUGO, show_in, NULL, 7),
  499. SENSOR_ATTR(in8_input, S_IRUGO, show_in, NULL, 8),
  500. SENSOR_ATTR(in9_input, S_IRUGO, show_in, NULL, 9),
  501. };
  502. static struct sensor_device_attribute sda_in_alarm[] = {
  503. SENSOR_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0),
  504. SENSOR_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1),
  505. SENSOR_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2),
  506. SENSOR_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3),
  507. SENSOR_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8),
  508. SENSOR_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 21),
  509. SENSOR_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 20),
  510. SENSOR_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 16),
  511. SENSOR_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 17),
  512. SENSOR_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 19),
  513. };
  514. static struct sensor_device_attribute sda_in_min[] = {
  515. SENSOR_ATTR(in0_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 0),
  516. SENSOR_ATTR(in1_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 1),
  517. SENSOR_ATTR(in2_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 2),
  518. SENSOR_ATTR(in3_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 3),
  519. SENSOR_ATTR(in4_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 4),
  520. SENSOR_ATTR(in5_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 5),
  521. SENSOR_ATTR(in6_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 6),
  522. SENSOR_ATTR(in7_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 7),
  523. SENSOR_ATTR(in8_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 8),
  524. SENSOR_ATTR(in9_min, S_IWUSR | S_IRUGO, show_in_min, store_in_min, 9),
  525. };
  526. static struct sensor_device_attribute sda_in_max[] = {
  527. SENSOR_ATTR(in0_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 0),
  528. SENSOR_ATTR(in1_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 1),
  529. SENSOR_ATTR(in2_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 2),
  530. SENSOR_ATTR(in3_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 3),
  531. SENSOR_ATTR(in4_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 4),
  532. SENSOR_ATTR(in5_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 5),
  533. SENSOR_ATTR(in6_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 6),
  534. SENSOR_ATTR(in7_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 7),
  535. SENSOR_ATTR(in8_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 8),
  536. SENSOR_ATTR(in9_max, S_IWUSR | S_IRUGO, show_in_max, store_in_max, 9),
  537. };
  538. static void device_create_file_in(struct device *dev, int i)
  539. {
  540. device_create_file(dev, &sda_in_input[i].dev_attr);
  541. device_create_file(dev, &sda_in_alarm[i].dev_attr);
  542. device_create_file(dev, &sda_in_min[i].dev_attr);
  543. device_create_file(dev, &sda_in_max[i].dev_attr);
  544. }
  545. #define show_fan_reg(reg) \
  546. static ssize_t \
  547. show_##reg(struct device *dev, struct device_attribute *attr, \
  548. char *buf) \
  549. { \
  550. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  551. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  552. int nr = sensor_attr->index; \
  553. return sprintf(buf, "%d\n", \
  554. fan_from_reg(data->reg[nr], \
  555. div_from_reg(data->fan_div[nr]))); \
  556. }
  557. show_fan_reg(fan);
  558. show_fan_reg(fan_min);
  559. static ssize_t
  560. show_fan_div(struct device *dev, struct device_attribute *attr,
  561. char *buf)
  562. {
  563. struct w83627ehf_data *data = w83627ehf_update_device(dev);
  564. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  565. int nr = sensor_attr->index;
  566. return sprintf(buf, "%u\n", div_from_reg(data->fan_div[nr]));
  567. }
  568. static ssize_t
  569. store_fan_min(struct device *dev, struct device_attribute *attr,
  570. const char *buf, size_t count)
  571. {
  572. struct i2c_client *client = to_i2c_client(dev);
  573. struct w83627ehf_data *data = i2c_get_clientdata(client);
  574. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  575. int nr = sensor_attr->index;
  576. unsigned int val = simple_strtoul(buf, NULL, 10);
  577. unsigned int reg;
  578. u8 new_div;
  579. mutex_lock(&data->update_lock);
  580. if (!val) {
  581. /* No min limit, alarm disabled */
  582. data->fan_min[nr] = 255;
  583. new_div = data->fan_div[nr]; /* No change */
  584. dev_info(dev, "fan%u low limit and alarm disabled\n", nr + 1);
  585. } else if ((reg = 1350000U / val) >= 128 * 255) {
  586. /* Speed below this value cannot possibly be represented,
  587. even with the highest divider (128) */
  588. data->fan_min[nr] = 254;
  589. new_div = 7; /* 128 == (1 << 7) */
  590. dev_warn(dev, "fan%u low limit %u below minimum %u, set to "
  591. "minimum\n", nr + 1, val, fan_from_reg(254, 128));
  592. } else if (!reg) {
  593. /* Speed above this value cannot possibly be represented,
  594. even with the lowest divider (1) */
  595. data->fan_min[nr] = 1;
  596. new_div = 0; /* 1 == (1 << 0) */
  597. dev_warn(dev, "fan%u low limit %u above maximum %u, set to "
  598. "maximum\n", nr + 1, val, fan_from_reg(1, 1));
  599. } else {
  600. /* Automatically pick the best divider, i.e. the one such
  601. that the min limit will correspond to a register value
  602. in the 96..192 range */
  603. new_div = 0;
  604. while (reg > 192 && new_div < 7) {
  605. reg >>= 1;
  606. new_div++;
  607. }
  608. data->fan_min[nr] = reg;
  609. }
  610. /* Write both the fan clock divider (if it changed) and the new
  611. fan min (unconditionally) */
  612. if (new_div != data->fan_div[nr]) {
  613. if (new_div > data->fan_div[nr])
  614. data->fan[nr] >>= (data->fan_div[nr] - new_div);
  615. else
  616. data->fan[nr] <<= (new_div - data->fan_div[nr]);
  617. dev_dbg(dev, "fan%u clock divider changed from %u to %u\n",
  618. nr + 1, div_from_reg(data->fan_div[nr]),
  619. div_from_reg(new_div));
  620. data->fan_div[nr] = new_div;
  621. w83627ehf_write_fan_div(client, nr);
  622. }
  623. w83627ehf_write_value(client, W83627EHF_REG_FAN_MIN[nr],
  624. data->fan_min[nr]);
  625. mutex_unlock(&data->update_lock);
  626. return count;
  627. }
  628. static struct sensor_device_attribute sda_fan_input[] = {
  629. SENSOR_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0),
  630. SENSOR_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1),
  631. SENSOR_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2),
  632. SENSOR_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3),
  633. SENSOR_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4),
  634. };
  635. static struct sensor_device_attribute sda_fan_alarm[] = {
  636. SENSOR_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6),
  637. SENSOR_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7),
  638. SENSOR_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 11),
  639. SENSOR_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 10),
  640. SENSOR_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 23),
  641. };
  642. static struct sensor_device_attribute sda_fan_min[] = {
  643. SENSOR_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
  644. store_fan_min, 0),
  645. SENSOR_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
  646. store_fan_min, 1),
  647. SENSOR_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
  648. store_fan_min, 2),
  649. SENSOR_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
  650. store_fan_min, 3),
  651. SENSOR_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
  652. store_fan_min, 4),
  653. };
  654. static struct sensor_device_attribute sda_fan_div[] = {
  655. SENSOR_ATTR(fan1_div, S_IRUGO, show_fan_div, NULL, 0),
  656. SENSOR_ATTR(fan2_div, S_IRUGO, show_fan_div, NULL, 1),
  657. SENSOR_ATTR(fan3_div, S_IRUGO, show_fan_div, NULL, 2),
  658. SENSOR_ATTR(fan4_div, S_IRUGO, show_fan_div, NULL, 3),
  659. SENSOR_ATTR(fan5_div, S_IRUGO, show_fan_div, NULL, 4),
  660. };
  661. static void device_create_file_fan(struct device *dev, int i)
  662. {
  663. device_create_file(dev, &sda_fan_input[i].dev_attr);
  664. device_create_file(dev, &sda_fan_alarm[i].dev_attr);
  665. device_create_file(dev, &sda_fan_div[i].dev_attr);
  666. device_create_file(dev, &sda_fan_min[i].dev_attr);
  667. }
  668. #define show_temp1_reg(reg) \
  669. static ssize_t \
  670. show_##reg(struct device *dev, struct device_attribute *attr, \
  671. char *buf) \
  672. { \
  673. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  674. return sprintf(buf, "%d\n", temp1_from_reg(data->reg)); \
  675. }
  676. show_temp1_reg(temp1);
  677. show_temp1_reg(temp1_max);
  678. show_temp1_reg(temp1_max_hyst);
  679. #define store_temp1_reg(REG, reg) \
  680. static ssize_t \
  681. store_temp1_##reg(struct device *dev, struct device_attribute *attr, \
  682. const char *buf, size_t count) \
  683. { \
  684. struct i2c_client *client = to_i2c_client(dev); \
  685. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  686. u32 val = simple_strtoul(buf, NULL, 10); \
  687. \
  688. mutex_lock(&data->update_lock); \
  689. data->temp1_##reg = temp1_to_reg(val, -128000, 127000); \
  690. w83627ehf_write_value(client, W83627EHF_REG_TEMP1_##REG, \
  691. data->temp1_##reg); \
  692. mutex_unlock(&data->update_lock); \
  693. return count; \
  694. }
  695. store_temp1_reg(OVER, max);
  696. store_temp1_reg(HYST, max_hyst);
  697. #define show_temp_reg(reg) \
  698. static ssize_t \
  699. show_##reg(struct device *dev, struct device_attribute *attr, \
  700. char *buf) \
  701. { \
  702. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  703. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  704. int nr = sensor_attr->index; \
  705. return sprintf(buf, "%d\n", \
  706. LM75_TEMP_FROM_REG(data->reg[nr])); \
  707. }
  708. show_temp_reg(temp);
  709. show_temp_reg(temp_max);
  710. show_temp_reg(temp_max_hyst);
  711. #define store_temp_reg(REG, reg) \
  712. static ssize_t \
  713. store_##reg(struct device *dev, struct device_attribute *attr, \
  714. const char *buf, size_t count) \
  715. { \
  716. struct i2c_client *client = to_i2c_client(dev); \
  717. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  718. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  719. int nr = sensor_attr->index; \
  720. u32 val = simple_strtoul(buf, NULL, 10); \
  721. \
  722. mutex_lock(&data->update_lock); \
  723. data->reg[nr] = LM75_TEMP_TO_REG(val); \
  724. w83627ehf_write_value(client, W83627EHF_REG_TEMP_##REG[nr], \
  725. data->reg[nr]); \
  726. mutex_unlock(&data->update_lock); \
  727. return count; \
  728. }
  729. store_temp_reg(OVER, temp_max);
  730. store_temp_reg(HYST, temp_max_hyst);
  731. static struct sensor_device_attribute sda_temp[] = {
  732. SENSOR_ATTR(temp1_input, S_IRUGO, show_temp1, NULL, 0),
  733. SENSOR_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 0),
  734. SENSOR_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 1),
  735. SENSOR_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp1_max,
  736. store_temp1_max, 0),
  737. SENSOR_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
  738. store_temp_max, 0),
  739. SENSOR_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
  740. store_temp_max, 1),
  741. SENSOR_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp1_max_hyst,
  742. store_temp1_max_hyst, 0),
  743. SENSOR_ATTR(temp2_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
  744. store_temp_max_hyst, 0),
  745. SENSOR_ATTR(temp3_max_hyst, S_IRUGO | S_IWUSR, show_temp_max_hyst,
  746. store_temp_max_hyst, 1),
  747. SENSOR_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4),
  748. SENSOR_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5),
  749. SENSOR_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 13),
  750. };
  751. #define show_pwm_reg(reg) \
  752. static ssize_t show_##reg (struct device *dev, struct device_attribute *attr, \
  753. char *buf) \
  754. { \
  755. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  756. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  757. int nr = sensor_attr->index; \
  758. return sprintf(buf, "%d\n", data->reg[nr]); \
  759. }
  760. show_pwm_reg(pwm_mode)
  761. show_pwm_reg(pwm_enable)
  762. show_pwm_reg(pwm)
  763. static ssize_t
  764. store_pwm_mode(struct device *dev, struct device_attribute *attr,
  765. const char *buf, size_t count)
  766. {
  767. struct i2c_client *client = to_i2c_client(dev);
  768. struct w83627ehf_data *data = i2c_get_clientdata(client);
  769. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  770. int nr = sensor_attr->index;
  771. u32 val = simple_strtoul(buf, NULL, 10);
  772. u16 reg;
  773. if (val > 1)
  774. return -EINVAL;
  775. mutex_lock(&data->update_lock);
  776. reg = w83627ehf_read_value(client, W83627EHF_REG_PWM_ENABLE[nr]);
  777. data->pwm_mode[nr] = val;
  778. reg &= ~(1 << W83627EHF_PWM_MODE_SHIFT[nr]);
  779. if (!val)
  780. reg |= 1 << W83627EHF_PWM_MODE_SHIFT[nr];
  781. w83627ehf_write_value(client, W83627EHF_REG_PWM_ENABLE[nr], reg);
  782. mutex_unlock(&data->update_lock);
  783. return count;
  784. }
  785. static ssize_t
  786. store_pwm(struct device *dev, struct device_attribute *attr,
  787. const char *buf, size_t count)
  788. {
  789. struct i2c_client *client = to_i2c_client(dev);
  790. struct w83627ehf_data *data = i2c_get_clientdata(client);
  791. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  792. int nr = sensor_attr->index;
  793. u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 0, 255);
  794. mutex_lock(&data->update_lock);
  795. data->pwm[nr] = val;
  796. w83627ehf_write_value(client, W83627EHF_REG_PWM[nr], val);
  797. mutex_unlock(&data->update_lock);
  798. return count;
  799. }
  800. static ssize_t
  801. store_pwm_enable(struct device *dev, struct device_attribute *attr,
  802. const char *buf, size_t count)
  803. {
  804. struct i2c_client *client = to_i2c_client(dev);
  805. struct w83627ehf_data *data = i2c_get_clientdata(client);
  806. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  807. int nr = sensor_attr->index;
  808. u32 val = simple_strtoul(buf, NULL, 10);
  809. u16 reg;
  810. if (!val || (val > 2)) /* only modes 1 and 2 are supported */
  811. return -EINVAL;
  812. mutex_lock(&data->update_lock);
  813. reg = w83627ehf_read_value(client, W83627EHF_REG_PWM_ENABLE[nr]);
  814. data->pwm_enable[nr] = val;
  815. reg &= ~(0x03 << W83627EHF_PWM_ENABLE_SHIFT[nr]);
  816. reg |= (val - 1) << W83627EHF_PWM_ENABLE_SHIFT[nr];
  817. w83627ehf_write_value(client, W83627EHF_REG_PWM_ENABLE[nr], reg);
  818. mutex_unlock(&data->update_lock);
  819. return count;
  820. }
  821. #define show_tol_temp(reg) \
  822. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  823. char *buf) \
  824. { \
  825. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  826. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  827. int nr = sensor_attr->index; \
  828. return sprintf(buf, "%d\n", temp1_from_reg(data->reg[nr])); \
  829. }
  830. show_tol_temp(tolerance)
  831. show_tol_temp(target_temp)
  832. static ssize_t
  833. store_target_temp(struct device *dev, struct device_attribute *attr,
  834. const char *buf, size_t count)
  835. {
  836. struct i2c_client *client = to_i2c_client(dev);
  837. struct w83627ehf_data *data = i2c_get_clientdata(client);
  838. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  839. int nr = sensor_attr->index;
  840. u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 127000);
  841. mutex_lock(&data->update_lock);
  842. data->target_temp[nr] = val;
  843. w83627ehf_write_value(client, W83627EHF_REG_TARGET[nr], val);
  844. mutex_unlock(&data->update_lock);
  845. return count;
  846. }
  847. static ssize_t
  848. store_tolerance(struct device *dev, struct device_attribute *attr,
  849. const char *buf, size_t count)
  850. {
  851. struct i2c_client *client = to_i2c_client(dev);
  852. struct w83627ehf_data *data = i2c_get_clientdata(client);
  853. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  854. int nr = sensor_attr->index;
  855. u16 reg;
  856. /* Limit the temp to 0C - 15C */
  857. u8 val = temp1_to_reg(simple_strtoul(buf, NULL, 10), 0, 15000);
  858. mutex_lock(&data->update_lock);
  859. reg = w83627ehf_read_value(client, W83627EHF_REG_TOLERANCE[nr]);
  860. data->tolerance[nr] = val;
  861. if (nr == 1)
  862. reg = (reg & 0x0f) | (val << 4);
  863. else
  864. reg = (reg & 0xf0) | val;
  865. w83627ehf_write_value(client, W83627EHF_REG_TOLERANCE[nr], reg);
  866. mutex_unlock(&data->update_lock);
  867. return count;
  868. }
  869. static struct sensor_device_attribute sda_pwm[] = {
  870. SENSOR_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0),
  871. SENSOR_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1),
  872. SENSOR_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 2),
  873. SENSOR_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 3),
  874. };
  875. static struct sensor_device_attribute sda_pwm_mode[] = {
  876. SENSOR_ATTR(pwm1_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  877. store_pwm_mode, 0),
  878. SENSOR_ATTR(pwm2_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  879. store_pwm_mode, 1),
  880. SENSOR_ATTR(pwm3_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  881. store_pwm_mode, 2),
  882. SENSOR_ATTR(pwm4_mode, S_IWUSR | S_IRUGO, show_pwm_mode,
  883. store_pwm_mode, 3),
  884. };
  885. static struct sensor_device_attribute sda_pwm_enable[] = {
  886. SENSOR_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  887. store_pwm_enable, 0),
  888. SENSOR_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  889. store_pwm_enable, 1),
  890. SENSOR_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  891. store_pwm_enable, 2),
  892. SENSOR_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
  893. store_pwm_enable, 3),
  894. };
  895. static struct sensor_device_attribute sda_target_temp[] = {
  896. SENSOR_ATTR(pwm1_target, S_IWUSR | S_IRUGO, show_target_temp,
  897. store_target_temp, 0),
  898. SENSOR_ATTR(pwm2_target, S_IWUSR | S_IRUGO, show_target_temp,
  899. store_target_temp, 1),
  900. SENSOR_ATTR(pwm3_target, S_IWUSR | S_IRUGO, show_target_temp,
  901. store_target_temp, 2),
  902. SENSOR_ATTR(pwm4_target, S_IWUSR | S_IRUGO, show_target_temp,
  903. store_target_temp, 3),
  904. };
  905. static struct sensor_device_attribute sda_tolerance[] = {
  906. SENSOR_ATTR(pwm1_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
  907. store_tolerance, 0),
  908. SENSOR_ATTR(pwm2_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
  909. store_tolerance, 1),
  910. SENSOR_ATTR(pwm3_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
  911. store_tolerance, 2),
  912. SENSOR_ATTR(pwm4_tolerance, S_IWUSR | S_IRUGO, show_tolerance,
  913. store_tolerance, 3),
  914. };
  915. static void device_create_file_pwm(struct device *dev, int i)
  916. {
  917. device_create_file(dev, &sda_pwm[i].dev_attr);
  918. device_create_file(dev, &sda_pwm_mode[i].dev_attr);
  919. device_create_file(dev, &sda_pwm_enable[i].dev_attr);
  920. device_create_file(dev, &sda_target_temp[i].dev_attr);
  921. device_create_file(dev, &sda_tolerance[i].dev_attr);
  922. }
  923. /* Smart Fan registers */
  924. #define fan_functions(reg, REG) \
  925. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  926. char *buf) \
  927. { \
  928. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  929. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  930. int nr = sensor_attr->index; \
  931. return sprintf(buf, "%d\n", data->reg[nr]); \
  932. }\
  933. static ssize_t \
  934. store_##reg(struct device *dev, struct device_attribute *attr, \
  935. const char *buf, size_t count) \
  936. {\
  937. struct i2c_client *client = to_i2c_client(dev); \
  938. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  939. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  940. int nr = sensor_attr->index; \
  941. u32 val = SENSORS_LIMIT(simple_strtoul(buf, NULL, 10), 1, 255); \
  942. mutex_lock(&data->update_lock); \
  943. data->reg[nr] = val; \
  944. w83627ehf_write_value(client, W83627EHF_REG_##REG[nr], val); \
  945. mutex_unlock(&data->update_lock); \
  946. return count; \
  947. }
  948. fan_functions(fan_min_output, FAN_MIN_OUTPUT)
  949. #define fan_time_functions(reg, REG) \
  950. static ssize_t show_##reg(struct device *dev, struct device_attribute *attr, \
  951. char *buf) \
  952. { \
  953. struct w83627ehf_data *data = w83627ehf_update_device(dev); \
  954. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  955. int nr = sensor_attr->index; \
  956. return sprintf(buf, "%d\n", \
  957. step_time_from_reg(data->reg[nr], data->pwm_mode[nr])); \
  958. } \
  959. \
  960. static ssize_t \
  961. store_##reg(struct device *dev, struct device_attribute *attr, \
  962. const char *buf, size_t count) \
  963. { \
  964. struct i2c_client *client = to_i2c_client(dev); \
  965. struct w83627ehf_data *data = i2c_get_clientdata(client); \
  966. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); \
  967. int nr = sensor_attr->index; \
  968. u8 val = step_time_to_reg(simple_strtoul(buf, NULL, 10), \
  969. data->pwm_mode[nr]); \
  970. mutex_lock(&data->update_lock); \
  971. data->reg[nr] = val; \
  972. w83627ehf_write_value(client, W83627EHF_REG_##REG[nr], val); \
  973. mutex_unlock(&data->update_lock); \
  974. return count; \
  975. } \
  976. fan_time_functions(fan_stop_time, FAN_STOP_TIME)
  977. static struct sensor_device_attribute sda_sf3_arrays_fan4[] = {
  978. SENSOR_ATTR(pwm4_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
  979. store_fan_stop_time, 3),
  980. SENSOR_ATTR(pwm4_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
  981. store_fan_min_output, 3),
  982. };
  983. static struct sensor_device_attribute sda_sf3_arrays[] = {
  984. SENSOR_ATTR(pwm1_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
  985. store_fan_stop_time, 0),
  986. SENSOR_ATTR(pwm2_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
  987. store_fan_stop_time, 1),
  988. SENSOR_ATTR(pwm3_stop_time, S_IWUSR | S_IRUGO, show_fan_stop_time,
  989. store_fan_stop_time, 2),
  990. SENSOR_ATTR(pwm1_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
  991. store_fan_min_output, 0),
  992. SENSOR_ATTR(pwm2_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
  993. store_fan_min_output, 1),
  994. SENSOR_ATTR(pwm3_min_output, S_IWUSR | S_IRUGO, show_fan_min_output,
  995. store_fan_min_output, 2),
  996. };
  997. /*
  998. * Driver and client management
  999. */
  1000. static struct i2c_driver w83627ehf_driver;
  1001. static void w83627ehf_init_client(struct i2c_client *client)
  1002. {
  1003. int i;
  1004. u8 tmp;
  1005. /* Start monitoring is needed */
  1006. tmp = w83627ehf_read_value(client, W83627EHF_REG_CONFIG);
  1007. if (!(tmp & 0x01))
  1008. w83627ehf_write_value(client, W83627EHF_REG_CONFIG,
  1009. tmp | 0x01);
  1010. /* Enable temp2 and temp3 if needed */
  1011. for (i = 0; i < 2; i++) {
  1012. tmp = w83627ehf_read_value(client,
  1013. W83627EHF_REG_TEMP_CONFIG[i]);
  1014. if (tmp & 0x01)
  1015. w83627ehf_write_value(client,
  1016. W83627EHF_REG_TEMP_CONFIG[i],
  1017. tmp & 0xfe);
  1018. }
  1019. }
  1020. static int w83627ehf_detect(struct i2c_adapter *adapter)
  1021. {
  1022. struct i2c_client *client;
  1023. struct w83627ehf_data *data;
  1024. struct device *dev;
  1025. u8 fan4pin, fan5pin;
  1026. int i, err = 0;
  1027. if (!request_region(address + REGION_OFFSET, REGION_LENGTH,
  1028. w83627ehf_driver.driver.name)) {
  1029. err = -EBUSY;
  1030. goto exit;
  1031. }
  1032. if (!(data = kzalloc(sizeof(struct w83627ehf_data), GFP_KERNEL))) {
  1033. err = -ENOMEM;
  1034. goto exit_release;
  1035. }
  1036. client = &data->client;
  1037. i2c_set_clientdata(client, data);
  1038. client->addr = address;
  1039. mutex_init(&data->lock);
  1040. client->adapter = adapter;
  1041. client->driver = &w83627ehf_driver;
  1042. client->flags = 0;
  1043. dev = &client->dev;
  1044. strlcpy(client->name, "w83627ehf", I2C_NAME_SIZE);
  1045. data->valid = 0;
  1046. mutex_init(&data->update_lock);
  1047. /* Tell the i2c layer a new client has arrived */
  1048. if ((err = i2c_attach_client(client)))
  1049. goto exit_free;
  1050. /* Initialize the chip */
  1051. w83627ehf_init_client(client);
  1052. /* A few vars need to be filled upon startup */
  1053. for (i = 0; i < 5; i++)
  1054. data->fan_min[i] = w83627ehf_read_value(client,
  1055. W83627EHF_REG_FAN_MIN[i]);
  1056. /* fan4 and fan5 share some pins with the GPIO and serial flash */
  1057. superio_enter();
  1058. fan5pin = superio_inb(0x24) & 0x2;
  1059. fan4pin = superio_inb(0x29) & 0x6;
  1060. superio_exit();
  1061. /* It looks like fan4 and fan5 pins can be alternatively used
  1062. as fan on/off switches */
  1063. data->has_fan = 0x07; /* fan1, fan2 and fan3 */
  1064. i = w83627ehf_read_value(client, W83627EHF_REG_FANDIV1);
  1065. if ((i & (1 << 2)) && (!fan4pin))
  1066. data->has_fan |= (1 << 3);
  1067. if ((i & (1 << 0)) && (!fan5pin))
  1068. data->has_fan |= (1 << 4);
  1069. /* Register sysfs hooks */
  1070. data->class_dev = hwmon_device_register(dev);
  1071. if (IS_ERR(data->class_dev)) {
  1072. err = PTR_ERR(data->class_dev);
  1073. goto exit_detach;
  1074. }
  1075. for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays); i++)
  1076. device_create_file(dev, &sda_sf3_arrays[i].dev_attr);
  1077. /* if fan4 is enabled create the sf3 files for it */
  1078. if (data->has_fan & (1 << 3))
  1079. for (i = 0; i < ARRAY_SIZE(sda_sf3_arrays_fan4); i++)
  1080. device_create_file(dev, &sda_sf3_arrays_fan4[i].dev_attr);
  1081. for (i = 0; i < 10; i++)
  1082. device_create_file_in(dev, i);
  1083. for (i = 0; i < 5; i++) {
  1084. if (data->has_fan & (1 << i)) {
  1085. device_create_file_fan(dev, i);
  1086. if (i != 4) /* we have only 4 pwm */
  1087. device_create_file_pwm(dev, i);
  1088. }
  1089. }
  1090. for (i = 0; i < ARRAY_SIZE(sda_temp); i++)
  1091. device_create_file(dev, &sda_temp[i].dev_attr);
  1092. return 0;
  1093. exit_detach:
  1094. i2c_detach_client(client);
  1095. exit_free:
  1096. kfree(data);
  1097. exit_release:
  1098. release_region(address + REGION_OFFSET, REGION_LENGTH);
  1099. exit:
  1100. return err;
  1101. }
  1102. static int w83627ehf_detach_client(struct i2c_client *client)
  1103. {
  1104. struct w83627ehf_data *data = i2c_get_clientdata(client);
  1105. int err;
  1106. hwmon_device_unregister(data->class_dev);
  1107. if ((err = i2c_detach_client(client)))
  1108. return err;
  1109. release_region(client->addr + REGION_OFFSET, REGION_LENGTH);
  1110. kfree(data);
  1111. return 0;
  1112. }
  1113. static struct i2c_driver w83627ehf_driver = {
  1114. .driver = {
  1115. .owner = THIS_MODULE,
  1116. .name = "w83627ehf",
  1117. },
  1118. .attach_adapter = w83627ehf_detect,
  1119. .detach_client = w83627ehf_detach_client,
  1120. };
  1121. static int __init w83627ehf_find(int sioaddr, unsigned short *addr)
  1122. {
  1123. u16 val;
  1124. REG = sioaddr;
  1125. VAL = sioaddr + 1;
  1126. superio_enter();
  1127. val = (superio_inb(SIO_REG_DEVID) << 8)
  1128. | superio_inb(SIO_REG_DEVID + 1);
  1129. if ((val & SIO_ID_MASK) != SIO_W83627EHF_ID) {
  1130. superio_exit();
  1131. return -ENODEV;
  1132. }
  1133. superio_select(W83627EHF_LD_HWM);
  1134. val = (superio_inb(SIO_REG_ADDR) << 8)
  1135. | superio_inb(SIO_REG_ADDR + 1);
  1136. *addr = val & REGION_ALIGNMENT;
  1137. if (*addr == 0) {
  1138. superio_exit();
  1139. return -ENODEV;
  1140. }
  1141. /* Activate logical device if needed */
  1142. val = superio_inb(SIO_REG_ENABLE);
  1143. if (!(val & 0x01))
  1144. superio_outb(SIO_REG_ENABLE, val | 0x01);
  1145. superio_exit();
  1146. return 0;
  1147. }
  1148. static int __init sensors_w83627ehf_init(void)
  1149. {
  1150. if (w83627ehf_find(0x2e, &address)
  1151. && w83627ehf_find(0x4e, &address))
  1152. return -ENODEV;
  1153. return i2c_isa_add_driver(&w83627ehf_driver);
  1154. }
  1155. static void __exit sensors_w83627ehf_exit(void)
  1156. {
  1157. i2c_isa_del_driver(&w83627ehf_driver);
  1158. }
  1159. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  1160. MODULE_DESCRIPTION("W83627EHF driver");
  1161. MODULE_LICENSE("GPL");
  1162. module_init(sensors_w83627ehf_init);
  1163. module_exit(sensors_w83627ehf_exit);