pc87360.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /*
  2. * pc87360.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * Copied from smsc47m1.c:
  7. * Copyright (C) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Supports the following chips:
  24. *
  25. * Chip #vin #fan #pwm #temp devid
  26. * PC87360 - 2 2 - 0xE1
  27. * PC87363 - 2 2 - 0xE8
  28. * PC87364 - 3 3 - 0xE4
  29. * PC87365 11 3 3 2 0xE5
  30. * PC87366 11 3 3 3-4 0xE9
  31. *
  32. * This driver assumes that no more than one chip is present, and one of
  33. * the standard Super-I/O addresses is used (0x2E/0x2F or 0x4E/0x4F).
  34. */
  35. #include <linux/config.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/slab.h>
  39. #include <linux/jiffies.h>
  40. #include <linux/i2c.h>
  41. #include <linux/i2c-sensor.h>
  42. #include <linux/i2c-vid.h>
  43. #include <asm/io.h>
  44. static unsigned short normal_i2c[] = { I2C_CLIENT_END };
  45. static unsigned int normal_isa[] = { 0, I2C_CLIENT_ISA_END };
  46. static struct i2c_force_data forces[] = {{ NULL }};
  47. static u8 devid;
  48. static unsigned int extra_isa[3];
  49. static u8 confreg[4];
  50. enum chips { any_chip, pc87360, pc87363, pc87364, pc87365, pc87366 };
  51. static struct i2c_address_data addr_data = {
  52. .normal_i2c = normal_i2c,
  53. .normal_isa = normal_isa,
  54. .forces = forces,
  55. };
  56. static int init = 1;
  57. module_param(init, int, 0);
  58. MODULE_PARM_DESC(init,
  59. "Chip initialization level:\n"
  60. " 0: None\n"
  61. "*1: Forcibly enable internal voltage and temperature channels, except in9\n"
  62. " 2: Forcibly enable all voltage and temperature channels, except in9\n"
  63. " 3: Forcibly enable all voltage and temperature channels, including in9");
  64. /*
  65. * Super-I/O registers and operations
  66. */
  67. #define DEV 0x07 /* Register: Logical device select */
  68. #define DEVID 0x20 /* Register: Device ID */
  69. #define ACT 0x30 /* Register: Device activation */
  70. #define BASE 0x60 /* Register: Base address */
  71. #define FSCM 0x09 /* Logical device: fans */
  72. #define VLM 0x0d /* Logical device: voltages */
  73. #define TMS 0x0e /* Logical device: temperatures */
  74. static const u8 logdev[3] = { FSCM, VLM, TMS };
  75. #define LD_FAN 0
  76. #define LD_IN 1
  77. #define LD_TEMP 2
  78. static inline void superio_outb(int sioaddr, int reg, int val)
  79. {
  80. outb(reg, sioaddr);
  81. outb(val, sioaddr+1);
  82. }
  83. static inline int superio_inb(int sioaddr, int reg)
  84. {
  85. outb(reg, sioaddr);
  86. return inb(sioaddr+1);
  87. }
  88. static inline void superio_exit(int sioaddr)
  89. {
  90. outb(0x02, sioaddr);
  91. outb(0x02, sioaddr+1);
  92. }
  93. /*
  94. * Logical devices
  95. */
  96. #define PC87360_EXTENT 0x10
  97. #define PC87365_REG_BANK 0x09
  98. #define NO_BANK 0xff
  99. /*
  100. * Fan registers and conversions
  101. */
  102. /* nr has to be 0 or 1 (PC87360/87363) or 2 (PC87364/87365/87366) */
  103. #define PC87360_REG_PRESCALE(nr) (0x00 + 2 * (nr))
  104. #define PC87360_REG_PWM(nr) (0x01 + 2 * (nr))
  105. #define PC87360_REG_FAN_MIN(nr) (0x06 + 3 * (nr))
  106. #define PC87360_REG_FAN(nr) (0x07 + 3 * (nr))
  107. #define PC87360_REG_FAN_STATUS(nr) (0x08 + 3 * (nr))
  108. #define FAN_FROM_REG(val,div) ((val) == 0 ? 0: \
  109. 480000 / ((val)*(div)))
  110. #define FAN_TO_REG(val,div) ((val) <= 100 ? 0 : \
  111. 480000 / ((val)*(div)))
  112. #define FAN_DIV_FROM_REG(val) (1 << ((val >> 5) & 0x03))
  113. #define FAN_STATUS_FROM_REG(val) ((val) & 0x07)
  114. #define FAN_CONFIG_MONITOR(val,nr) (((val) >> (2 + nr * 3)) & 1)
  115. #define FAN_CONFIG_CONTROL(val,nr) (((val) >> (3 + nr * 3)) & 1)
  116. #define FAN_CONFIG_INVERT(val,nr) (((val) >> (4 + nr * 3)) & 1)
  117. #define PWM_FROM_REG(val,inv) ((inv) ? 255 - (val) : (val))
  118. static inline u8 PWM_TO_REG(int val, int inv)
  119. {
  120. if (inv)
  121. val = 255 - val;
  122. if (val < 0)
  123. return 0;
  124. if (val > 255)
  125. return 255;
  126. return val;
  127. }
  128. /*
  129. * Voltage registers and conversions
  130. */
  131. #define PC87365_REG_IN_CONVRATE 0x07
  132. #define PC87365_REG_IN_CONFIG 0x08
  133. #define PC87365_REG_IN 0x0B
  134. #define PC87365_REG_IN_MIN 0x0D
  135. #define PC87365_REG_IN_MAX 0x0C
  136. #define PC87365_REG_IN_STATUS 0x0A
  137. #define PC87365_REG_IN_ALARMS1 0x00
  138. #define PC87365_REG_IN_ALARMS2 0x01
  139. #define PC87365_REG_VID 0x06
  140. #define IN_FROM_REG(val,ref) (((val) * (ref) + 128) / 256)
  141. #define IN_TO_REG(val,ref) ((val) < 0 ? 0 : \
  142. (val)*256 >= (ref)*255 ? 255: \
  143. ((val) * 256 + (ref)/2) / (ref))
  144. /*
  145. * Temperature registers and conversions
  146. */
  147. #define PC87365_REG_TEMP_CONFIG 0x08
  148. #define PC87365_REG_TEMP 0x0B
  149. #define PC87365_REG_TEMP_MIN 0x0D
  150. #define PC87365_REG_TEMP_MAX 0x0C
  151. #define PC87365_REG_TEMP_CRIT 0x0E
  152. #define PC87365_REG_TEMP_STATUS 0x0A
  153. #define PC87365_REG_TEMP_ALARMS 0x00
  154. #define TEMP_FROM_REG(val) ((val) * 1000)
  155. #define TEMP_TO_REG(val) ((val) < -55000 ? -55 : \
  156. (val) > 127000 ? 127 : \
  157. (val) < 0 ? ((val) - 500) / 1000 : \
  158. ((val) + 500) / 1000)
  159. /*
  160. * Client data (each client gets its own)
  161. */
  162. struct pc87360_data {
  163. struct i2c_client client;
  164. struct semaphore lock;
  165. struct semaphore update_lock;
  166. char valid; /* !=0 if following fields are valid */
  167. unsigned long last_updated; /* In jiffies */
  168. int address[3];
  169. u8 fannr, innr, tempnr;
  170. u8 fan[3]; /* Register value */
  171. u8 fan_min[3]; /* Register value */
  172. u8 fan_status[3]; /* Register value */
  173. u8 pwm[3]; /* Register value */
  174. u16 fan_conf; /* Configuration register values, combined */
  175. u16 in_vref; /* 1 mV/bit */
  176. u8 in[14]; /* Register value */
  177. u8 in_min[14]; /* Register value */
  178. u8 in_max[14]; /* Register value */
  179. u8 in_crit[3]; /* Register value */
  180. u8 in_status[14]; /* Register value */
  181. u16 in_alarms; /* Register values, combined, masked */
  182. u8 vid_conf; /* Configuration register value */
  183. u8 vrm;
  184. u8 vid; /* Register value */
  185. s8 temp[3]; /* Register value */
  186. s8 temp_min[3]; /* Register value */
  187. s8 temp_max[3]; /* Register value */
  188. s8 temp_crit[3]; /* Register value */
  189. u8 temp_status[3]; /* Register value */
  190. u8 temp_alarms; /* Register value, masked */
  191. };
  192. /*
  193. * Functions declaration
  194. */
  195. static int pc87360_attach_adapter(struct i2c_adapter *adapter);
  196. static int pc87360_detect(struct i2c_adapter *adapter, int address, int kind);
  197. static int pc87360_detach_client(struct i2c_client *client);
  198. static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
  199. u8 reg);
  200. static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
  201. u8 reg, u8 value);
  202. static void pc87360_init_client(struct i2c_client *client, int use_thermistors);
  203. static struct pc87360_data *pc87360_update_device(struct device *dev);
  204. /*
  205. * Driver data (common to all clients)
  206. */
  207. static struct i2c_driver pc87360_driver = {
  208. .owner = THIS_MODULE,
  209. .name = "pc87360",
  210. .flags = I2C_DF_NOTIFY,
  211. .attach_adapter = pc87360_attach_adapter,
  212. .detach_client = pc87360_detach_client,
  213. };
  214. /*
  215. * Sysfs stuff
  216. */
  217. static ssize_t set_fan_min(struct device *dev, const char *buf,
  218. size_t count, int nr)
  219. {
  220. struct i2c_client *client = to_i2c_client(dev);
  221. struct pc87360_data *data = i2c_get_clientdata(client);
  222. long fan_min = simple_strtol(buf, NULL, 10);
  223. down(&data->update_lock);
  224. fan_min = FAN_TO_REG(fan_min, FAN_DIV_FROM_REG(data->fan_status[nr]));
  225. /* If it wouldn't fit, change clock divisor */
  226. while (fan_min > 255
  227. && (data->fan_status[nr] & 0x60) != 0x60) {
  228. fan_min >>= 1;
  229. data->fan[nr] >>= 1;
  230. data->fan_status[nr] += 0x20;
  231. }
  232. data->fan_min[nr] = fan_min > 255 ? 255 : fan_min;
  233. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_MIN(nr),
  234. data->fan_min[nr]);
  235. /* Write new divider, preserve alarm bits */
  236. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_FAN_STATUS(nr),
  237. data->fan_status[nr] & 0xF9);
  238. up(&data->update_lock);
  239. return count;
  240. }
  241. #define show_and_set_fan(offset) \
  242. static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  243. { \
  244. struct pc87360_data *data = pc87360_update_device(dev); \
  245. return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan[offset-1], \
  246. FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
  247. } \
  248. static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  249. { \
  250. struct pc87360_data *data = pc87360_update_device(dev); \
  251. return sprintf(buf, "%u\n", FAN_FROM_REG(data->fan_min[offset-1], \
  252. FAN_DIV_FROM_REG(data->fan_status[offset-1]))); \
  253. } \
  254. static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
  255. { \
  256. struct pc87360_data *data = pc87360_update_device(dev); \
  257. return sprintf(buf, "%u\n", \
  258. FAN_DIV_FROM_REG(data->fan_status[offset-1])); \
  259. } \
  260. static ssize_t show_fan##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
  261. { \
  262. struct pc87360_data *data = pc87360_update_device(dev); \
  263. return sprintf(buf, "%u\n", \
  264. FAN_STATUS_FROM_REG(data->fan_status[offset-1])); \
  265. } \
  266. static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  267. size_t count) \
  268. { \
  269. return set_fan_min(dev, buf, count, offset-1); \
  270. } \
  271. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  272. show_fan##offset##_input, NULL); \
  273. static DEVICE_ATTR(fan##offset##_min, S_IWUSR | S_IRUGO, \
  274. show_fan##offset##_min, set_fan##offset##_min); \
  275. static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
  276. show_fan##offset##_div, NULL); \
  277. static DEVICE_ATTR(fan##offset##_status, S_IRUGO, \
  278. show_fan##offset##_status, NULL);
  279. show_and_set_fan(1)
  280. show_and_set_fan(2)
  281. show_and_set_fan(3)
  282. #define show_and_set_pwm(offset) \
  283. static ssize_t show_pwm##offset(struct device *dev, struct device_attribute *attr, char *buf) \
  284. { \
  285. struct pc87360_data *data = pc87360_update_device(dev); \
  286. return sprintf(buf, "%u\n", \
  287. PWM_FROM_REG(data->pwm[offset-1], \
  288. FAN_CONFIG_INVERT(data->fan_conf, \
  289. offset-1))); \
  290. } \
  291. static ssize_t set_pwm##offset(struct device *dev, struct device_attribute *attr, const char *buf, \
  292. size_t count) \
  293. { \
  294. struct i2c_client *client = to_i2c_client(dev); \
  295. struct pc87360_data *data = i2c_get_clientdata(client); \
  296. long val = simple_strtol(buf, NULL, 10); \
  297. \
  298. down(&data->update_lock); \
  299. data->pwm[offset-1] = PWM_TO_REG(val, \
  300. FAN_CONFIG_INVERT(data->fan_conf, offset-1)); \
  301. pc87360_write_value(data, LD_FAN, NO_BANK, PC87360_REG_PWM(offset-1), \
  302. data->pwm[offset-1]); \
  303. up(&data->update_lock); \
  304. return count; \
  305. } \
  306. static DEVICE_ATTR(pwm##offset, S_IWUSR | S_IRUGO, \
  307. show_pwm##offset, set_pwm##offset);
  308. show_and_set_pwm(1)
  309. show_and_set_pwm(2)
  310. show_and_set_pwm(3)
  311. #define show_and_set_in(offset) \
  312. static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  313. { \
  314. struct pc87360_data *data = pc87360_update_device(dev); \
  315. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
  316. data->in_vref)); \
  317. } \
  318. static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  319. { \
  320. struct pc87360_data *data = pc87360_update_device(dev); \
  321. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
  322. data->in_vref)); \
  323. } \
  324. static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
  325. { \
  326. struct pc87360_data *data = pc87360_update_device(dev); \
  327. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
  328. data->in_vref)); \
  329. } \
  330. static ssize_t show_in##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
  331. { \
  332. struct pc87360_data *data = pc87360_update_device(dev); \
  333. return sprintf(buf, "%u\n", data->in_status[offset]); \
  334. } \
  335. static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  336. size_t count) \
  337. { \
  338. struct i2c_client *client = to_i2c_client(dev); \
  339. struct pc87360_data *data = i2c_get_clientdata(client); \
  340. long val = simple_strtol(buf, NULL, 10); \
  341. \
  342. down(&data->update_lock); \
  343. data->in_min[offset] = IN_TO_REG(val, data->in_vref); \
  344. pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MIN, \
  345. data->in_min[offset]); \
  346. up(&data->update_lock); \
  347. return count; \
  348. } \
  349. static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
  350. size_t count) \
  351. { \
  352. struct i2c_client *client = to_i2c_client(dev); \
  353. struct pc87360_data *data = i2c_get_clientdata(client); \
  354. long val = simple_strtol(buf, NULL, 10); \
  355. \
  356. down(&data->update_lock); \
  357. data->in_max[offset] = IN_TO_REG(val, \
  358. data->in_vref); \
  359. pc87360_write_value(data, LD_IN, offset, PC87365_REG_IN_MAX, \
  360. data->in_max[offset]); \
  361. up(&data->update_lock); \
  362. return count; \
  363. } \
  364. static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  365. show_in##offset##_input, NULL); \
  366. static DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
  367. show_in##offset##_min, set_in##offset##_min); \
  368. static DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
  369. show_in##offset##_max, set_in##offset##_max); \
  370. static DEVICE_ATTR(in##offset##_status, S_IRUGO, \
  371. show_in##offset##_status, NULL);
  372. show_and_set_in(0)
  373. show_and_set_in(1)
  374. show_and_set_in(2)
  375. show_and_set_in(3)
  376. show_and_set_in(4)
  377. show_and_set_in(5)
  378. show_and_set_in(6)
  379. show_and_set_in(7)
  380. show_and_set_in(8)
  381. show_and_set_in(9)
  382. show_and_set_in(10)
  383. #define show_and_set_therm(offset) \
  384. static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  385. { \
  386. struct pc87360_data *data = pc87360_update_device(dev); \
  387. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset+7], \
  388. data->in_vref)); \
  389. } \
  390. static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  391. { \
  392. struct pc87360_data *data = pc87360_update_device(dev); \
  393. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset+7], \
  394. data->in_vref)); \
  395. } \
  396. static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
  397. { \
  398. struct pc87360_data *data = pc87360_update_device(dev); \
  399. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset+7], \
  400. data->in_vref)); \
  401. } \
  402. static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
  403. { \
  404. struct pc87360_data *data = pc87360_update_device(dev); \
  405. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_crit[offset-4], \
  406. data->in_vref)); \
  407. } \
  408. static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
  409. { \
  410. struct pc87360_data *data = pc87360_update_device(dev); \
  411. return sprintf(buf, "%u\n", data->in_status[offset+7]); \
  412. } \
  413. static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  414. size_t count) \
  415. { \
  416. struct i2c_client *client = to_i2c_client(dev); \
  417. struct pc87360_data *data = i2c_get_clientdata(client); \
  418. long val = simple_strtol(buf, NULL, 10); \
  419. \
  420. down(&data->update_lock); \
  421. data->in_min[offset+7] = IN_TO_REG(val, data->in_vref); \
  422. pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MIN, \
  423. data->in_min[offset+7]); \
  424. up(&data->update_lock); \
  425. return count; \
  426. } \
  427. static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
  428. size_t count) \
  429. { \
  430. struct i2c_client *client = to_i2c_client(dev); \
  431. struct pc87360_data *data = i2c_get_clientdata(client); \
  432. long val = simple_strtol(buf, NULL, 10); \
  433. \
  434. down(&data->update_lock); \
  435. data->in_max[offset+7] = IN_TO_REG(val, data->in_vref); \
  436. pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_MAX, \
  437. data->in_max[offset+7]); \
  438. up(&data->update_lock); \
  439. return count; \
  440. } \
  441. static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
  442. size_t count) \
  443. { \
  444. struct i2c_client *client = to_i2c_client(dev); \
  445. struct pc87360_data *data = i2c_get_clientdata(client); \
  446. long val = simple_strtol(buf, NULL, 10); \
  447. \
  448. down(&data->update_lock); \
  449. data->in_crit[offset-4] = IN_TO_REG(val, data->in_vref); \
  450. pc87360_write_value(data, LD_IN, offset+7, PC87365_REG_TEMP_CRIT, \
  451. data->in_crit[offset-4]); \
  452. up(&data->update_lock); \
  453. return count; \
  454. } \
  455. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  456. show_temp##offset##_input, NULL); \
  457. static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
  458. show_temp##offset##_min, set_temp##offset##_min); \
  459. static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
  460. show_temp##offset##_max, set_temp##offset##_max); \
  461. static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
  462. show_temp##offset##_crit, set_temp##offset##_crit); \
  463. static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
  464. show_temp##offset##_status, NULL);
  465. show_and_set_therm(4)
  466. show_and_set_therm(5)
  467. show_and_set_therm(6)
  468. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  469. {
  470. struct pc87360_data *data = pc87360_update_device(dev);
  471. return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
  472. }
  473. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  474. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  475. {
  476. struct pc87360_data *data = pc87360_update_device(dev);
  477. return sprintf(buf, "%u\n", data->vrm);
  478. }
  479. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  480. {
  481. struct i2c_client *client = to_i2c_client(dev);
  482. struct pc87360_data *data = i2c_get_clientdata(client);
  483. data->vrm = simple_strtoul(buf, NULL, 10);
  484. return count;
  485. }
  486. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  487. static ssize_t show_in_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  488. {
  489. struct pc87360_data *data = pc87360_update_device(dev);
  490. return sprintf(buf, "%u\n", data->in_alarms);
  491. }
  492. static DEVICE_ATTR(alarms_in, S_IRUGO, show_in_alarms, NULL);
  493. #define show_and_set_temp(offset) \
  494. static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  495. { \
  496. struct pc87360_data *data = pc87360_update_device(dev); \
  497. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
  498. } \
  499. static ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  500. { \
  501. struct pc87360_data *data = pc87360_update_device(dev); \
  502. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
  503. } \
  504. static ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
  505. { \
  506. struct pc87360_data *data = pc87360_update_device(dev); \
  507. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
  508. }\
  509. static ssize_t show_temp##offset##_crit(struct device *dev, struct device_attribute *attr, char *buf) \
  510. { \
  511. struct pc87360_data *data = pc87360_update_device(dev); \
  512. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[offset-1])); \
  513. }\
  514. static ssize_t show_temp##offset##_status(struct device *dev, struct device_attribute *attr, char *buf) \
  515. { \
  516. struct pc87360_data *data = pc87360_update_device(dev); \
  517. return sprintf(buf, "%d\n", data->temp_status[offset-1]); \
  518. }\
  519. static ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  520. size_t count) \
  521. { \
  522. struct i2c_client *client = to_i2c_client(dev); \
  523. struct pc87360_data *data = i2c_get_clientdata(client); \
  524. long val = simple_strtol(buf, NULL, 10); \
  525. \
  526. down(&data->update_lock); \
  527. data->temp_min[offset-1] = TEMP_TO_REG(val); \
  528. pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MIN, \
  529. data->temp_min[offset-1]); \
  530. up(&data->update_lock); \
  531. return count; \
  532. } \
  533. static ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
  534. size_t count) \
  535. { \
  536. struct i2c_client *client = to_i2c_client(dev); \
  537. struct pc87360_data *data = i2c_get_clientdata(client); \
  538. long val = simple_strtol(buf, NULL, 10); \
  539. \
  540. down(&data->update_lock); \
  541. data->temp_max[offset-1] = TEMP_TO_REG(val); \
  542. pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_MAX, \
  543. data->temp_max[offset-1]); \
  544. up(&data->update_lock); \
  545. return count; \
  546. } \
  547. static ssize_t set_temp##offset##_crit(struct device *dev, struct device_attribute *attr, const char *buf, \
  548. size_t count) \
  549. { \
  550. struct i2c_client *client = to_i2c_client(dev); \
  551. struct pc87360_data *data = i2c_get_clientdata(client); \
  552. long val = simple_strtol(buf, NULL, 10); \
  553. \
  554. down(&data->update_lock); \
  555. data->temp_crit[offset-1] = TEMP_TO_REG(val); \
  556. pc87360_write_value(data, LD_TEMP, offset-1, PC87365_REG_TEMP_CRIT, \
  557. data->temp_crit[offset-1]); \
  558. up(&data->update_lock); \
  559. return count; \
  560. } \
  561. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  562. show_temp##offset##_input, NULL); \
  563. static DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
  564. show_temp##offset##_min, set_temp##offset##_min); \
  565. static DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
  566. show_temp##offset##_max, set_temp##offset##_max); \
  567. static DEVICE_ATTR(temp##offset##_crit, S_IWUSR | S_IRUGO, \
  568. show_temp##offset##_crit, set_temp##offset##_crit); \
  569. static DEVICE_ATTR(temp##offset##_status, S_IRUGO, \
  570. show_temp##offset##_status, NULL);
  571. show_and_set_temp(1)
  572. show_and_set_temp(2)
  573. show_and_set_temp(3)
  574. static ssize_t show_temp_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  575. {
  576. struct pc87360_data *data = pc87360_update_device(dev);
  577. return sprintf(buf, "%u\n", data->temp_alarms);
  578. }
  579. static DEVICE_ATTR(alarms_temp, S_IRUGO, show_temp_alarms, NULL);
  580. /*
  581. * Device detection, registration and update
  582. */
  583. static int pc87360_attach_adapter(struct i2c_adapter *adapter)
  584. {
  585. return i2c_detect(adapter, &addr_data, pc87360_detect);
  586. }
  587. static int pc87360_find(int sioaddr, u8 *devid, int *address)
  588. {
  589. u16 val;
  590. int i;
  591. int nrdev; /* logical device count */
  592. /* No superio_enter */
  593. /* Identify device */
  594. val = superio_inb(sioaddr, DEVID);
  595. switch (val) {
  596. case 0xE1: /* PC87360 */
  597. case 0xE8: /* PC87363 */
  598. case 0xE4: /* PC87364 */
  599. nrdev = 1;
  600. break;
  601. case 0xE5: /* PC87365 */
  602. case 0xE9: /* PC87366 */
  603. nrdev = 3;
  604. break;
  605. default:
  606. superio_exit(sioaddr);
  607. return -ENODEV;
  608. }
  609. /* Remember the device id */
  610. *devid = val;
  611. for (i = 0; i < nrdev; i++) {
  612. /* select logical device */
  613. superio_outb(sioaddr, DEV, logdev[i]);
  614. val = superio_inb(sioaddr, ACT);
  615. if (!(val & 0x01)) {
  616. printk(KERN_INFO "pc87360: Device 0x%02x not "
  617. "activated\n", logdev[i]);
  618. continue;
  619. }
  620. val = (superio_inb(sioaddr, BASE) << 8)
  621. | superio_inb(sioaddr, BASE + 1);
  622. if (!val) {
  623. printk(KERN_INFO "pc87360: Base address not set for "
  624. "device 0x%02x\n", logdev[i]);
  625. continue;
  626. }
  627. address[i] = val;
  628. if (i==0) { /* Fans */
  629. confreg[0] = superio_inb(sioaddr, 0xF0);
  630. confreg[1] = superio_inb(sioaddr, 0xF1);
  631. #ifdef DEBUG
  632. printk(KERN_DEBUG "pc87360: Fan 1: mon=%d "
  633. "ctrl=%d inv=%d\n", (confreg[0]>>2)&1,
  634. (confreg[0]>>3)&1, (confreg[0]>>4)&1);
  635. printk(KERN_DEBUG "pc87360: Fan 2: mon=%d "
  636. "ctrl=%d inv=%d\n", (confreg[0]>>5)&1,
  637. (confreg[0]>>6)&1, (confreg[0]>>7)&1);
  638. printk(KERN_DEBUG "pc87360: Fan 3: mon=%d "
  639. "ctrl=%d inv=%d\n", confreg[1]&1,
  640. (confreg[1]>>1)&1, (confreg[1]>>2)&1);
  641. #endif
  642. } else if (i==1) { /* Voltages */
  643. /* Are we using thermistors? */
  644. if (*devid == 0xE9) { /* PC87366 */
  645. /* These registers are not logical-device
  646. specific, just that we won't need them if
  647. we don't use the VLM device */
  648. confreg[2] = superio_inb(sioaddr, 0x2B);
  649. confreg[3] = superio_inb(sioaddr, 0x25);
  650. if (confreg[2] & 0x40) {
  651. printk(KERN_INFO "pc87360: Using "
  652. "thermistors for temperature "
  653. "monitoring\n");
  654. }
  655. if (confreg[3] & 0xE0) {
  656. printk(KERN_INFO "pc87360: VID "
  657. "inputs routed (mode %u)\n",
  658. confreg[3] >> 5);
  659. }
  660. }
  661. }
  662. }
  663. superio_exit(sioaddr);
  664. return 0;
  665. }
  666. /* We don't really care about the address.
  667. Read from extra_isa instead. */
  668. int pc87360_detect(struct i2c_adapter *adapter, int address, int kind)
  669. {
  670. int i;
  671. struct i2c_client *new_client;
  672. struct pc87360_data *data;
  673. int err = 0;
  674. const char *name = "pc87360";
  675. int use_thermistors = 0;
  676. if (!i2c_is_isa_adapter(adapter))
  677. return -ENODEV;
  678. if (!(data = kmalloc(sizeof(struct pc87360_data), GFP_KERNEL)))
  679. return -ENOMEM;
  680. memset(data, 0x00, sizeof(struct pc87360_data));
  681. new_client = &data->client;
  682. i2c_set_clientdata(new_client, data);
  683. new_client->addr = address;
  684. init_MUTEX(&data->lock);
  685. new_client->adapter = adapter;
  686. new_client->driver = &pc87360_driver;
  687. new_client->flags = 0;
  688. data->fannr = 2;
  689. data->innr = 0;
  690. data->tempnr = 0;
  691. switch (devid) {
  692. case 0xe8:
  693. name = "pc87363";
  694. break;
  695. case 0xe4:
  696. name = "pc87364";
  697. data->fannr = 3;
  698. break;
  699. case 0xe5:
  700. name = "pc87365";
  701. data->fannr = extra_isa[0] ? 3 : 0;
  702. data->innr = extra_isa[1] ? 11 : 0;
  703. data->tempnr = extra_isa[2] ? 2 : 0;
  704. break;
  705. case 0xe9:
  706. name = "pc87366";
  707. data->fannr = extra_isa[0] ? 3 : 0;
  708. data->innr = extra_isa[1] ? 14 : 0;
  709. data->tempnr = extra_isa[2] ? 3 : 0;
  710. break;
  711. }
  712. strcpy(new_client->name, name);
  713. data->valid = 0;
  714. init_MUTEX(&data->update_lock);
  715. for (i = 0; i < 3; i++) {
  716. if (((data->address[i] = extra_isa[i]))
  717. && !request_region(extra_isa[i], PC87360_EXTENT,
  718. pc87360_driver.name)) {
  719. dev_err(&new_client->dev, "Region 0x%x-0x%x already "
  720. "in use!\n", extra_isa[i],
  721. extra_isa[i]+PC87360_EXTENT-1);
  722. for (i--; i >= 0; i--)
  723. release_region(extra_isa[i], PC87360_EXTENT);
  724. err = -EBUSY;
  725. goto ERROR1;
  726. }
  727. }
  728. /* Retrieve the fans configuration from Super-I/O space */
  729. if (data->fannr)
  730. data->fan_conf = confreg[0] | (confreg[1] << 8);
  731. if ((err = i2c_attach_client(new_client)))
  732. goto ERROR2;
  733. /* Use the correct reference voltage
  734. Unless both the VLM and the TMS logical devices agree to
  735. use an external Vref, the internal one is used. */
  736. if (data->innr) {
  737. i = pc87360_read_value(data, LD_IN, NO_BANK,
  738. PC87365_REG_IN_CONFIG);
  739. if (data->tempnr) {
  740. i &= pc87360_read_value(data, LD_TEMP, NO_BANK,
  741. PC87365_REG_TEMP_CONFIG);
  742. }
  743. data->in_vref = (i&0x02) ? 3025 : 2966;
  744. dev_dbg(&new_client->dev, "Using %s reference voltage\n",
  745. (i&0x02) ? "external" : "internal");
  746. data->vid_conf = confreg[3];
  747. data->vrm = 90;
  748. }
  749. /* Fan clock dividers may be needed before any data is read */
  750. for (i = 0; i < data->fannr; i++) {
  751. if (FAN_CONFIG_MONITOR(data->fan_conf, i))
  752. data->fan_status[i] = pc87360_read_value(data,
  753. LD_FAN, NO_BANK,
  754. PC87360_REG_FAN_STATUS(i));
  755. }
  756. if (init > 0) {
  757. if (devid == 0xe9 && data->address[1]) /* PC87366 */
  758. use_thermistors = confreg[2] & 0x40;
  759. pc87360_init_client(new_client, use_thermistors);
  760. }
  761. /* Register sysfs hooks */
  762. if (data->innr) {
  763. device_create_file(&new_client->dev, &dev_attr_in0_input);
  764. device_create_file(&new_client->dev, &dev_attr_in1_input);
  765. device_create_file(&new_client->dev, &dev_attr_in2_input);
  766. device_create_file(&new_client->dev, &dev_attr_in3_input);
  767. device_create_file(&new_client->dev, &dev_attr_in4_input);
  768. device_create_file(&new_client->dev, &dev_attr_in5_input);
  769. device_create_file(&new_client->dev, &dev_attr_in6_input);
  770. device_create_file(&new_client->dev, &dev_attr_in7_input);
  771. device_create_file(&new_client->dev, &dev_attr_in8_input);
  772. device_create_file(&new_client->dev, &dev_attr_in9_input);
  773. device_create_file(&new_client->dev, &dev_attr_in10_input);
  774. device_create_file(&new_client->dev, &dev_attr_in0_min);
  775. device_create_file(&new_client->dev, &dev_attr_in1_min);
  776. device_create_file(&new_client->dev, &dev_attr_in2_min);
  777. device_create_file(&new_client->dev, &dev_attr_in3_min);
  778. device_create_file(&new_client->dev, &dev_attr_in4_min);
  779. device_create_file(&new_client->dev, &dev_attr_in5_min);
  780. device_create_file(&new_client->dev, &dev_attr_in6_min);
  781. device_create_file(&new_client->dev, &dev_attr_in7_min);
  782. device_create_file(&new_client->dev, &dev_attr_in8_min);
  783. device_create_file(&new_client->dev, &dev_attr_in9_min);
  784. device_create_file(&new_client->dev, &dev_attr_in10_min);
  785. device_create_file(&new_client->dev, &dev_attr_in0_max);
  786. device_create_file(&new_client->dev, &dev_attr_in1_max);
  787. device_create_file(&new_client->dev, &dev_attr_in2_max);
  788. device_create_file(&new_client->dev, &dev_attr_in3_max);
  789. device_create_file(&new_client->dev, &dev_attr_in4_max);
  790. device_create_file(&new_client->dev, &dev_attr_in5_max);
  791. device_create_file(&new_client->dev, &dev_attr_in6_max);
  792. device_create_file(&new_client->dev, &dev_attr_in7_max);
  793. device_create_file(&new_client->dev, &dev_attr_in8_max);
  794. device_create_file(&new_client->dev, &dev_attr_in9_max);
  795. device_create_file(&new_client->dev, &dev_attr_in10_max);
  796. device_create_file(&new_client->dev, &dev_attr_in0_status);
  797. device_create_file(&new_client->dev, &dev_attr_in1_status);
  798. device_create_file(&new_client->dev, &dev_attr_in2_status);
  799. device_create_file(&new_client->dev, &dev_attr_in3_status);
  800. device_create_file(&new_client->dev, &dev_attr_in4_status);
  801. device_create_file(&new_client->dev, &dev_attr_in5_status);
  802. device_create_file(&new_client->dev, &dev_attr_in6_status);
  803. device_create_file(&new_client->dev, &dev_attr_in7_status);
  804. device_create_file(&new_client->dev, &dev_attr_in8_status);
  805. device_create_file(&new_client->dev, &dev_attr_in9_status);
  806. device_create_file(&new_client->dev, &dev_attr_in10_status);
  807. device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
  808. device_create_file(&new_client->dev, &dev_attr_vrm);
  809. device_create_file(&new_client->dev, &dev_attr_alarms_in);
  810. }
  811. if (data->tempnr) {
  812. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  813. device_create_file(&new_client->dev, &dev_attr_temp2_input);
  814. device_create_file(&new_client->dev, &dev_attr_temp1_min);
  815. device_create_file(&new_client->dev, &dev_attr_temp2_min);
  816. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  817. device_create_file(&new_client->dev, &dev_attr_temp2_max);
  818. device_create_file(&new_client->dev, &dev_attr_temp1_crit);
  819. device_create_file(&new_client->dev, &dev_attr_temp2_crit);
  820. device_create_file(&new_client->dev, &dev_attr_temp1_status);
  821. device_create_file(&new_client->dev, &dev_attr_temp2_status);
  822. device_create_file(&new_client->dev, &dev_attr_alarms_temp);
  823. }
  824. if (data->tempnr == 3) {
  825. device_create_file(&new_client->dev, &dev_attr_temp3_input);
  826. device_create_file(&new_client->dev, &dev_attr_temp3_min);
  827. device_create_file(&new_client->dev, &dev_attr_temp3_max);
  828. device_create_file(&new_client->dev, &dev_attr_temp3_crit);
  829. device_create_file(&new_client->dev, &dev_attr_temp3_status);
  830. }
  831. if (data->innr == 14) {
  832. device_create_file(&new_client->dev, &dev_attr_temp4_input);
  833. device_create_file(&new_client->dev, &dev_attr_temp5_input);
  834. device_create_file(&new_client->dev, &dev_attr_temp6_input);
  835. device_create_file(&new_client->dev, &dev_attr_temp4_min);
  836. device_create_file(&new_client->dev, &dev_attr_temp5_min);
  837. device_create_file(&new_client->dev, &dev_attr_temp6_min);
  838. device_create_file(&new_client->dev, &dev_attr_temp4_max);
  839. device_create_file(&new_client->dev, &dev_attr_temp5_max);
  840. device_create_file(&new_client->dev, &dev_attr_temp6_max);
  841. device_create_file(&new_client->dev, &dev_attr_temp4_crit);
  842. device_create_file(&new_client->dev, &dev_attr_temp5_crit);
  843. device_create_file(&new_client->dev, &dev_attr_temp6_crit);
  844. device_create_file(&new_client->dev, &dev_attr_temp4_status);
  845. device_create_file(&new_client->dev, &dev_attr_temp5_status);
  846. device_create_file(&new_client->dev, &dev_attr_temp6_status);
  847. }
  848. if (data->fannr) {
  849. if (FAN_CONFIG_MONITOR(data->fan_conf, 0)) {
  850. device_create_file(&new_client->dev,
  851. &dev_attr_fan1_input);
  852. device_create_file(&new_client->dev,
  853. &dev_attr_fan1_min);
  854. device_create_file(&new_client->dev,
  855. &dev_attr_fan1_div);
  856. device_create_file(&new_client->dev,
  857. &dev_attr_fan1_status);
  858. }
  859. if (FAN_CONFIG_MONITOR(data->fan_conf, 1)) {
  860. device_create_file(&new_client->dev,
  861. &dev_attr_fan2_input);
  862. device_create_file(&new_client->dev,
  863. &dev_attr_fan2_min);
  864. device_create_file(&new_client->dev,
  865. &dev_attr_fan2_div);
  866. device_create_file(&new_client->dev,
  867. &dev_attr_fan2_status);
  868. }
  869. if (FAN_CONFIG_CONTROL(data->fan_conf, 0))
  870. device_create_file(&new_client->dev, &dev_attr_pwm1);
  871. if (FAN_CONFIG_CONTROL(data->fan_conf, 1))
  872. device_create_file(&new_client->dev, &dev_attr_pwm2);
  873. }
  874. if (data->fannr == 3) {
  875. if (FAN_CONFIG_MONITOR(data->fan_conf, 2)) {
  876. device_create_file(&new_client->dev,
  877. &dev_attr_fan3_input);
  878. device_create_file(&new_client->dev,
  879. &dev_attr_fan3_min);
  880. device_create_file(&new_client->dev,
  881. &dev_attr_fan3_div);
  882. device_create_file(&new_client->dev,
  883. &dev_attr_fan3_status);
  884. }
  885. if (FAN_CONFIG_CONTROL(data->fan_conf, 2))
  886. device_create_file(&new_client->dev, &dev_attr_pwm3);
  887. }
  888. return 0;
  889. ERROR2:
  890. for (i = 0; i < 3; i++) {
  891. if (data->address[i]) {
  892. release_region(data->address[i], PC87360_EXTENT);
  893. }
  894. }
  895. ERROR1:
  896. kfree(data);
  897. return err;
  898. }
  899. static int pc87360_detach_client(struct i2c_client *client)
  900. {
  901. struct pc87360_data *data = i2c_get_clientdata(client);
  902. int i;
  903. if ((i = i2c_detach_client(client))) {
  904. dev_err(&client->dev, "Client deregistration failed, "
  905. "client not detached.\n");
  906. return i;
  907. }
  908. for (i = 0; i < 3; i++) {
  909. if (data->address[i]) {
  910. release_region(data->address[i], PC87360_EXTENT);
  911. }
  912. }
  913. kfree(data);
  914. return 0;
  915. }
  916. /* ldi is the logical device index
  917. bank is for voltages and temperatures only */
  918. static int pc87360_read_value(struct pc87360_data *data, u8 ldi, u8 bank,
  919. u8 reg)
  920. {
  921. int res;
  922. down(&(data->lock));
  923. if (bank != NO_BANK)
  924. outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
  925. res = inb_p(data->address[ldi] + reg);
  926. up(&(data->lock));
  927. return res;
  928. }
  929. static void pc87360_write_value(struct pc87360_data *data, u8 ldi, u8 bank,
  930. u8 reg, u8 value)
  931. {
  932. down(&(data->lock));
  933. if (bank != NO_BANK)
  934. outb_p(bank, data->address[ldi] + PC87365_REG_BANK);
  935. outb_p(value, data->address[ldi] + reg);
  936. up(&(data->lock));
  937. }
  938. static void pc87360_init_client(struct i2c_client *client, int use_thermistors)
  939. {
  940. struct pc87360_data *data = i2c_get_clientdata(client);
  941. int i, nr;
  942. const u8 init_in[14] = { 2, 2, 2, 2, 2, 2, 2, 1, 1, 3, 1, 2, 2, 2 };
  943. const u8 init_temp[3] = { 2, 2, 1 };
  944. u8 reg;
  945. if (init >= 2 && data->innr) {
  946. reg = pc87360_read_value(data, LD_IN, NO_BANK,
  947. PC87365_REG_IN_CONVRATE);
  948. dev_info(&client->dev, "VLM conversion set to"
  949. "1s period, 160us delay\n");
  950. pc87360_write_value(data, LD_IN, NO_BANK,
  951. PC87365_REG_IN_CONVRATE,
  952. (reg & 0xC0) | 0x11);
  953. }
  954. nr = data->innr < 11 ? data->innr : 11;
  955. for (i=0; i<nr; i++) {
  956. if (init >= init_in[i]) {
  957. /* Forcibly enable voltage channel */
  958. reg = pc87360_read_value(data, LD_IN, i,
  959. PC87365_REG_IN_STATUS);
  960. if (!(reg & 0x01)) {
  961. dev_dbg(&client->dev, "Forcibly "
  962. "enabling in%d\n", i);
  963. pc87360_write_value(data, LD_IN, i,
  964. PC87365_REG_IN_STATUS,
  965. (reg & 0x68) | 0x87);
  966. }
  967. }
  968. }
  969. /* We can't blindly trust the Super-I/O space configuration bit,
  970. most BIOS won't set it properly */
  971. for (i=11; i<data->innr; i++) {
  972. reg = pc87360_read_value(data, LD_IN, i,
  973. PC87365_REG_TEMP_STATUS);
  974. use_thermistors = use_thermistors || (reg & 0x01);
  975. }
  976. i = use_thermistors ? 2 : 0;
  977. for (; i<data->tempnr; i++) {
  978. if (init >= init_temp[i]) {
  979. /* Forcibly enable temperature channel */
  980. reg = pc87360_read_value(data, LD_TEMP, i,
  981. PC87365_REG_TEMP_STATUS);
  982. if (!(reg & 0x01)) {
  983. dev_dbg(&client->dev, "Forcibly "
  984. "enabling temp%d\n", i+1);
  985. pc87360_write_value(data, LD_TEMP, i,
  986. PC87365_REG_TEMP_STATUS,
  987. 0xCF);
  988. }
  989. }
  990. }
  991. if (use_thermistors) {
  992. for (i=11; i<data->innr; i++) {
  993. if (init >= init_in[i]) {
  994. /* The pin may already be used by thermal
  995. diodes */
  996. reg = pc87360_read_value(data, LD_TEMP,
  997. (i-11)/2, PC87365_REG_TEMP_STATUS);
  998. if (reg & 0x01) {
  999. dev_dbg(&client->dev, "Skipping "
  1000. "temp%d, pin already in use "
  1001. "by temp%d\n", i-7, (i-11)/2);
  1002. continue;
  1003. }
  1004. /* Forcibly enable thermistor channel */
  1005. reg = pc87360_read_value(data, LD_IN, i,
  1006. PC87365_REG_IN_STATUS);
  1007. if (!(reg & 0x01)) {
  1008. dev_dbg(&client->dev, "Forcibly "
  1009. "enabling temp%d\n", i-7);
  1010. pc87360_write_value(data, LD_IN, i,
  1011. PC87365_REG_TEMP_STATUS,
  1012. (reg & 0x60) | 0x8F);
  1013. }
  1014. }
  1015. }
  1016. }
  1017. if (data->innr) {
  1018. reg = pc87360_read_value(data, LD_IN, NO_BANK,
  1019. PC87365_REG_IN_CONFIG);
  1020. if (reg & 0x01) {
  1021. dev_dbg(&client->dev, "Forcibly "
  1022. "enabling monitoring (VLM)\n");
  1023. pc87360_write_value(data, LD_IN, NO_BANK,
  1024. PC87365_REG_IN_CONFIG,
  1025. reg & 0xFE);
  1026. }
  1027. }
  1028. if (data->tempnr) {
  1029. reg = pc87360_read_value(data, LD_TEMP, NO_BANK,
  1030. PC87365_REG_TEMP_CONFIG);
  1031. if (reg & 0x01) {
  1032. dev_dbg(&client->dev, "Forcibly enabling "
  1033. "monitoring (TMS)\n");
  1034. pc87360_write_value(data, LD_TEMP, NO_BANK,
  1035. PC87365_REG_TEMP_CONFIG,
  1036. reg & 0xFE);
  1037. }
  1038. if (init >= 2) {
  1039. /* Chip config as documented by National Semi. */
  1040. pc87360_write_value(data, LD_TEMP, 0xF, 0xA, 0x08);
  1041. /* We voluntarily omit the bank here, in case the
  1042. sequence itself matters. It shouldn't be a problem,
  1043. since nobody else is supposed to access the
  1044. device at that point. */
  1045. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xB, 0x04);
  1046. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xC, 0x35);
  1047. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xD, 0x05);
  1048. pc87360_write_value(data, LD_TEMP, NO_BANK, 0xE, 0x05);
  1049. }
  1050. }
  1051. }
  1052. static void pc87360_autodiv(struct i2c_client *client, int nr)
  1053. {
  1054. struct pc87360_data *data = i2c_get_clientdata(client);
  1055. u8 old_min = data->fan_min[nr];
  1056. /* Increase clock divider if needed and possible */
  1057. if ((data->fan_status[nr] & 0x04) /* overflow flag */
  1058. || (data->fan[nr] >= 224)) { /* next to overflow */
  1059. if ((data->fan_status[nr] & 0x60) != 0x60) {
  1060. data->fan_status[nr] += 0x20;
  1061. data->fan_min[nr] >>= 1;
  1062. data->fan[nr] >>= 1;
  1063. dev_dbg(&client->dev, "Increasing "
  1064. "clock divider to %d for fan %d\n",
  1065. FAN_DIV_FROM_REG(data->fan_status[nr]), nr+1);
  1066. }
  1067. } else {
  1068. /* Decrease clock divider if possible */
  1069. while (!(data->fan_min[nr] & 0x80) /* min "nails" divider */
  1070. && data->fan[nr] < 85 /* bad accuracy */
  1071. && (data->fan_status[nr] & 0x60) != 0x00) {
  1072. data->fan_status[nr] -= 0x20;
  1073. data->fan_min[nr] <<= 1;
  1074. data->fan[nr] <<= 1;
  1075. dev_dbg(&client->dev, "Decreasing "
  1076. "clock divider to %d for fan %d\n",
  1077. FAN_DIV_FROM_REG(data->fan_status[nr]),
  1078. nr+1);
  1079. }
  1080. }
  1081. /* Write new fan min if it changed */
  1082. if (old_min != data->fan_min[nr]) {
  1083. pc87360_write_value(data, LD_FAN, NO_BANK,
  1084. PC87360_REG_FAN_MIN(nr),
  1085. data->fan_min[nr]);
  1086. }
  1087. }
  1088. static struct pc87360_data *pc87360_update_device(struct device *dev)
  1089. {
  1090. struct i2c_client *client = to_i2c_client(dev);
  1091. struct pc87360_data *data = i2c_get_clientdata(client);
  1092. u8 i;
  1093. down(&data->update_lock);
  1094. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  1095. dev_dbg(&client->dev, "Data update\n");
  1096. /* Fans */
  1097. for (i = 0; i < data->fannr; i++) {
  1098. if (FAN_CONFIG_MONITOR(data->fan_conf, i)) {
  1099. data->fan_status[i] =
  1100. pc87360_read_value(data, LD_FAN,
  1101. NO_BANK, PC87360_REG_FAN_STATUS(i));
  1102. data->fan[i] = pc87360_read_value(data, LD_FAN,
  1103. NO_BANK, PC87360_REG_FAN(i));
  1104. data->fan_min[i] = pc87360_read_value(data,
  1105. LD_FAN, NO_BANK,
  1106. PC87360_REG_FAN_MIN(i));
  1107. /* Change clock divider if needed */
  1108. pc87360_autodiv(client, i);
  1109. /* Clear bits and write new divider */
  1110. pc87360_write_value(data, LD_FAN, NO_BANK,
  1111. PC87360_REG_FAN_STATUS(i),
  1112. data->fan_status[i]);
  1113. }
  1114. if (FAN_CONFIG_CONTROL(data->fan_conf, i))
  1115. data->pwm[i] = pc87360_read_value(data, LD_FAN,
  1116. NO_BANK, PC87360_REG_PWM(i));
  1117. }
  1118. /* Voltages */
  1119. for (i = 0; i < data->innr; i++) {
  1120. data->in_status[i] = pc87360_read_value(data, LD_IN, i,
  1121. PC87365_REG_IN_STATUS);
  1122. /* Clear bits */
  1123. pc87360_write_value(data, LD_IN, i,
  1124. PC87365_REG_IN_STATUS,
  1125. data->in_status[i]);
  1126. if ((data->in_status[i] & 0x81) == 0x81) {
  1127. data->in[i] = pc87360_read_value(data, LD_IN,
  1128. i, PC87365_REG_IN);
  1129. }
  1130. if (data->in_status[i] & 0x01) {
  1131. data->in_min[i] = pc87360_read_value(data,
  1132. LD_IN, i,
  1133. PC87365_REG_IN_MIN);
  1134. data->in_max[i] = pc87360_read_value(data,
  1135. LD_IN, i,
  1136. PC87365_REG_IN_MAX);
  1137. if (i >= 11)
  1138. data->in_crit[i-11] =
  1139. pc87360_read_value(data, LD_IN,
  1140. i, PC87365_REG_TEMP_CRIT);
  1141. }
  1142. }
  1143. if (data->innr) {
  1144. data->in_alarms = pc87360_read_value(data, LD_IN,
  1145. NO_BANK, PC87365_REG_IN_ALARMS1)
  1146. | ((pc87360_read_value(data, LD_IN,
  1147. NO_BANK, PC87365_REG_IN_ALARMS2)
  1148. & 0x07) << 8);
  1149. data->vid = (data->vid_conf & 0xE0) ?
  1150. pc87360_read_value(data, LD_IN,
  1151. NO_BANK, PC87365_REG_VID) : 0x1F;
  1152. }
  1153. /* Temperatures */
  1154. for (i = 0; i < data->tempnr; i++) {
  1155. data->temp_status[i] = pc87360_read_value(data,
  1156. LD_TEMP, i,
  1157. PC87365_REG_TEMP_STATUS);
  1158. /* Clear bits */
  1159. pc87360_write_value(data, LD_TEMP, i,
  1160. PC87365_REG_TEMP_STATUS,
  1161. data->temp_status[i]);
  1162. if ((data->temp_status[i] & 0x81) == 0x81) {
  1163. data->temp[i] = pc87360_read_value(data,
  1164. LD_TEMP, i,
  1165. PC87365_REG_TEMP);
  1166. }
  1167. if (data->temp_status[i] & 0x01) {
  1168. data->temp_min[i] = pc87360_read_value(data,
  1169. LD_TEMP, i,
  1170. PC87365_REG_TEMP_MIN);
  1171. data->temp_max[i] = pc87360_read_value(data,
  1172. LD_TEMP, i,
  1173. PC87365_REG_TEMP_MAX);
  1174. data->temp_crit[i] = pc87360_read_value(data,
  1175. LD_TEMP, i,
  1176. PC87365_REG_TEMP_CRIT);
  1177. }
  1178. }
  1179. if (data->tempnr) {
  1180. data->temp_alarms = pc87360_read_value(data, LD_TEMP,
  1181. NO_BANK, PC87365_REG_TEMP_ALARMS)
  1182. & 0x3F;
  1183. }
  1184. data->last_updated = jiffies;
  1185. data->valid = 1;
  1186. }
  1187. up(&data->update_lock);
  1188. return data;
  1189. }
  1190. static int __init pc87360_init(void)
  1191. {
  1192. int i;
  1193. if (pc87360_find(0x2e, &devid, extra_isa)
  1194. && pc87360_find(0x4e, &devid, extra_isa)) {
  1195. printk(KERN_WARNING "pc87360: PC8736x not detected, "
  1196. "module not inserted.\n");
  1197. return -ENODEV;
  1198. }
  1199. /* Arbitrarily pick one of the addresses */
  1200. for (i = 0; i < 3; i++) {
  1201. if (extra_isa[i] != 0x0000) {
  1202. normal_isa[0] = extra_isa[i];
  1203. break;
  1204. }
  1205. }
  1206. if (normal_isa[0] == 0x0000) {
  1207. printk(KERN_WARNING "pc87360: No active logical device, "
  1208. "module not inserted.\n");
  1209. return -ENODEV;
  1210. }
  1211. return i2c_add_driver(&pc87360_driver);
  1212. }
  1213. static void __exit pc87360_exit(void)
  1214. {
  1215. i2c_del_driver(&pc87360_driver);
  1216. }
  1217. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  1218. MODULE_DESCRIPTION("PC8736x hardware monitor");
  1219. MODULE_LICENSE("GPL");
  1220. module_init(pc87360_init);
  1221. module_exit(pc87360_exit);