pc87360.c 45 KB

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