pc87360.c 44 KB

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