w83627ehf.c 43 KB

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