pc87360.c 44 KB

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