w83627ehf.c 44 KB

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