sis5595.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*
  2. * sis5595.c - Part of lm_sensors, Linux kernel modules
  3. * for hardware monitoring
  4. *
  5. * Copyright (C) 1998 - 2001 Frodo Looijaard <frodol@dds.nl>,
  6. * Kyösti Mälkki <kmalkki@cc.hut.fi>, and
  7. * Mark D. Studebaker <mdsxyz123@yahoo.com>
  8. * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
  9. * the help of Jean Delvare <khali@linux-fr.org>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*
  26. * SiS southbridge has a LM78-like chip integrated on the same IC.
  27. * This driver is a customized copy of lm78.c
  28. *
  29. * Supports following revisions:
  30. * Version PCI ID PCI Revision
  31. * 1 1039/0008 AF or less
  32. * 2 1039/0008 B0 or greater
  33. *
  34. * Note: these chips contain a 0008 device which is incompatible with the
  35. * 5595. We recognize these by the presence of the listed
  36. * "blacklist" PCI ID and refuse to load.
  37. *
  38. * NOT SUPPORTED PCI ID BLACKLIST PCI ID
  39. * 540 0008 0540
  40. * 550 0008 0550
  41. * 5513 0008 5511
  42. * 5581 0008 5597
  43. * 5582 0008 5597
  44. * 5597 0008 5597
  45. * 5598 0008 5597/5598
  46. * 630 0008 0630
  47. * 645 0008 0645
  48. * 730 0008 0730
  49. * 735 0008 0735
  50. */
  51. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  52. #include <linux/module.h>
  53. #include <linux/slab.h>
  54. #include <linux/ioport.h>
  55. #include <linux/pci.h>
  56. #include <linux/platform_device.h>
  57. #include <linux/hwmon.h>
  58. #include <linux/hwmon-sysfs.h>
  59. #include <linux/err.h>
  60. #include <linux/init.h>
  61. #include <linux/jiffies.h>
  62. #include <linux/mutex.h>
  63. #include <linux/sysfs.h>
  64. #include <linux/acpi.h>
  65. #include <linux/io.h>
  66. /*
  67. * If force_addr is set to anything different from 0, we forcibly enable
  68. * the device at the given address.
  69. */
  70. static u16 force_addr;
  71. module_param(force_addr, ushort, 0);
  72. MODULE_PARM_DESC(force_addr,
  73. "Initialize the base address of the sensors");
  74. static struct platform_device *pdev;
  75. /* Many SIS5595 constants specified below */
  76. /* Length of ISA address segment */
  77. #define SIS5595_EXTENT 8
  78. /* PCI Config Registers */
  79. #define SIS5595_BASE_REG 0x68
  80. #define SIS5595_PIN_REG 0x7A
  81. #define SIS5595_ENABLE_REG 0x7B
  82. /* Where are the ISA address/data registers relative to the base address */
  83. #define SIS5595_ADDR_REG_OFFSET 5
  84. #define SIS5595_DATA_REG_OFFSET 6
  85. /* The SIS5595 registers */
  86. #define SIS5595_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  87. #define SIS5595_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  88. #define SIS5595_REG_IN(nr) (0x20 + (nr))
  89. #define SIS5595_REG_FAN_MIN(nr) (0x3b + (nr))
  90. #define SIS5595_REG_FAN(nr) (0x28 + (nr))
  91. /*
  92. * On the first version of the chip, the temp registers are separate.
  93. * On the second version,
  94. * TEMP pin is shared with IN4, configured in PCI register 0x7A.
  95. * The registers are the same as well.
  96. * OVER and HYST are really MAX and MIN.
  97. */
  98. #define REV2MIN 0xb0
  99. #define SIS5595_REG_TEMP (((data->revision) >= REV2MIN) ? \
  100. SIS5595_REG_IN(4) : 0x27)
  101. #define SIS5595_REG_TEMP_OVER (((data->revision) >= REV2MIN) ? \
  102. SIS5595_REG_IN_MAX(4) : 0x39)
  103. #define SIS5595_REG_TEMP_HYST (((data->revision) >= REV2MIN) ? \
  104. SIS5595_REG_IN_MIN(4) : 0x3a)
  105. #define SIS5595_REG_CONFIG 0x40
  106. #define SIS5595_REG_ALARM1 0x41
  107. #define SIS5595_REG_ALARM2 0x42
  108. #define SIS5595_REG_FANDIV 0x47
  109. /*
  110. * Conversions. Limit checking is only done on the TO_REG
  111. * variants.
  112. */
  113. /*
  114. * IN: mV, (0V to 4.08V)
  115. * REG: 16mV/bit
  116. */
  117. static inline u8 IN_TO_REG(unsigned long val)
  118. {
  119. unsigned long nval = SENSORS_LIMIT(val, 0, 4080);
  120. return (nval + 8) / 16;
  121. }
  122. #define IN_FROM_REG(val) ((val) * 16)
  123. static inline u8 FAN_TO_REG(long rpm, int div)
  124. {
  125. if (rpm <= 0)
  126. return 255;
  127. return SENSORS_LIMIT((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  128. }
  129. static inline int FAN_FROM_REG(u8 val, int div)
  130. {
  131. return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
  132. }
  133. /*
  134. * TEMP: mC (-54.12C to +157.53C)
  135. * REG: 0.83C/bit + 52.12, two's complement
  136. */
  137. static inline int TEMP_FROM_REG(s8 val)
  138. {
  139. return val * 830 + 52120;
  140. }
  141. static inline s8 TEMP_TO_REG(int val)
  142. {
  143. int nval = SENSORS_LIMIT(val, -54120, 157530) ;
  144. return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
  145. }
  146. /*
  147. * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  148. * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
  149. */
  150. static inline u8 DIV_TO_REG(int val)
  151. {
  152. return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
  153. }
  154. #define DIV_FROM_REG(val) (1 << (val))
  155. /*
  156. * For each registered chip, we need to keep some data in memory.
  157. * The structure is dynamically allocated.
  158. */
  159. struct sis5595_data {
  160. unsigned short addr;
  161. const char *name;
  162. struct device *hwmon_dev;
  163. struct mutex lock;
  164. struct mutex update_lock;
  165. char valid; /* !=0 if following fields are valid */
  166. unsigned long last_updated; /* In jiffies */
  167. char maxins; /* == 3 if temp enabled, otherwise == 4 */
  168. u8 revision; /* Reg. value */
  169. u8 in[5]; /* Register value */
  170. u8 in_max[5]; /* Register value */
  171. u8 in_min[5]; /* Register value */
  172. u8 fan[2]; /* Register value */
  173. u8 fan_min[2]; /* Register value */
  174. s8 temp; /* Register value */
  175. s8 temp_over; /* Register value */
  176. s8 temp_hyst; /* Register value */
  177. u8 fan_div[2]; /* Register encoding, shifted right */
  178. u16 alarms; /* Register encoding, combined */
  179. };
  180. static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
  181. static int sis5595_probe(struct platform_device *pdev);
  182. static int __devexit sis5595_remove(struct platform_device *pdev);
  183. static int sis5595_read_value(struct sis5595_data *data, u8 reg);
  184. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
  185. static struct sis5595_data *sis5595_update_device(struct device *dev);
  186. static void sis5595_init_device(struct sis5595_data *data);
  187. static struct platform_driver sis5595_driver = {
  188. .driver = {
  189. .owner = THIS_MODULE,
  190. .name = "sis5595",
  191. },
  192. .probe = sis5595_probe,
  193. .remove = __devexit_p(sis5595_remove),
  194. };
  195. /* 4 Voltages */
  196. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  197. char *buf)
  198. {
  199. struct sis5595_data *data = sis5595_update_device(dev);
  200. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  201. int nr = attr->index;
  202. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
  203. }
  204. static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
  205. char *buf)
  206. {
  207. struct sis5595_data *data = sis5595_update_device(dev);
  208. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  209. int nr = attr->index;
  210. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
  211. }
  212. static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
  213. char *buf)
  214. {
  215. struct sis5595_data *data = sis5595_update_device(dev);
  216. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  217. int nr = attr->index;
  218. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
  219. }
  220. static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
  221. const char *buf, size_t count)
  222. {
  223. struct sis5595_data *data = dev_get_drvdata(dev);
  224. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  225. int nr = attr->index;
  226. unsigned long val;
  227. int err;
  228. err = kstrtoul(buf, 10, &val);
  229. if (err)
  230. return err;
  231. mutex_lock(&data->update_lock);
  232. data->in_min[nr] = IN_TO_REG(val);
  233. sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
  234. mutex_unlock(&data->update_lock);
  235. return count;
  236. }
  237. static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
  238. const char *buf, size_t count)
  239. {
  240. struct sis5595_data *data = dev_get_drvdata(dev);
  241. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  242. int nr = attr->index;
  243. unsigned long val;
  244. int err;
  245. err = kstrtoul(buf, 10, &val);
  246. if (err)
  247. return err;
  248. mutex_lock(&data->update_lock);
  249. data->in_max[nr] = IN_TO_REG(val);
  250. sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
  251. mutex_unlock(&data->update_lock);
  252. return count;
  253. }
  254. #define show_in_offset(offset) \
  255. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  256. show_in, NULL, offset); \
  257. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  258. show_in_min, set_in_min, offset); \
  259. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  260. show_in_max, set_in_max, offset);
  261. show_in_offset(0);
  262. show_in_offset(1);
  263. show_in_offset(2);
  264. show_in_offset(3);
  265. show_in_offset(4);
  266. /* Temperature */
  267. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct sis5595_data *data = sis5595_update_device(dev);
  271. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
  272. }
  273. static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr,
  274. char *buf)
  275. {
  276. struct sis5595_data *data = sis5595_update_device(dev);
  277. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
  278. }
  279. static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. struct sis5595_data *data = dev_get_drvdata(dev);
  283. long val;
  284. int err;
  285. err = kstrtol(buf, 10, &val);
  286. if (err)
  287. return err;
  288. mutex_lock(&data->update_lock);
  289. data->temp_over = TEMP_TO_REG(val);
  290. sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
  291. mutex_unlock(&data->update_lock);
  292. return count;
  293. }
  294. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr,
  295. char *buf)
  296. {
  297. struct sis5595_data *data = sis5595_update_device(dev);
  298. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
  299. }
  300. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr,
  301. const char *buf, size_t count)
  302. {
  303. struct sis5595_data *data = dev_get_drvdata(dev);
  304. long val;
  305. int err;
  306. err = kstrtol(buf, 10, &val);
  307. if (err)
  308. return err;
  309. mutex_lock(&data->update_lock);
  310. data->temp_hyst = TEMP_TO_REG(val);
  311. sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
  312. mutex_unlock(&data->update_lock);
  313. return count;
  314. }
  315. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  316. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  317. show_temp_over, set_temp_over);
  318. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  319. show_temp_hyst, set_temp_hyst);
  320. /* 2 Fans */
  321. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  322. char *buf)
  323. {
  324. struct sis5595_data *data = sis5595_update_device(dev);
  325. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  326. int nr = attr->index;
  327. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  328. DIV_FROM_REG(data->fan_div[nr])));
  329. }
  330. static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
  331. char *buf)
  332. {
  333. struct sis5595_data *data = sis5595_update_device(dev);
  334. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  335. int nr = attr->index;
  336. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  337. DIV_FROM_REG(data->fan_div[nr])));
  338. }
  339. static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
  340. const char *buf, size_t count)
  341. {
  342. struct sis5595_data *data = dev_get_drvdata(dev);
  343. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  344. int nr = attr->index;
  345. unsigned long val;
  346. int err;
  347. err = kstrtoul(buf, 10, &val);
  348. if (err)
  349. return err;
  350. mutex_lock(&data->update_lock);
  351. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  352. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  353. mutex_unlock(&data->update_lock);
  354. return count;
  355. }
  356. static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
  357. char *buf)
  358. {
  359. struct sis5595_data *data = sis5595_update_device(dev);
  360. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  361. int nr = attr->index;
  362. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  363. }
  364. /*
  365. * Note: we save and restore the fan minimum here, because its value is
  366. * determined in part by the fan divisor. This follows the principle of
  367. * least surprise; the user doesn't expect the fan minimum to change just
  368. * because the divisor changed.
  369. */
  370. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  371. const char *buf, size_t count)
  372. {
  373. struct sis5595_data *data = dev_get_drvdata(dev);
  374. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  375. int nr = attr->index;
  376. unsigned long min;
  377. int reg;
  378. unsigned long val;
  379. int err;
  380. err = kstrtoul(buf, 10, &val);
  381. if (err)
  382. return err;
  383. mutex_lock(&data->update_lock);
  384. min = FAN_FROM_REG(data->fan_min[nr],
  385. DIV_FROM_REG(data->fan_div[nr]));
  386. reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
  387. switch (val) {
  388. case 1:
  389. data->fan_div[nr] = 0;
  390. break;
  391. case 2:
  392. data->fan_div[nr] = 1;
  393. break;
  394. case 4:
  395. data->fan_div[nr] = 2;
  396. break;
  397. case 8:
  398. data->fan_div[nr] = 3;
  399. break;
  400. default:
  401. dev_err(dev, "fan_div value %ld not "
  402. "supported. Choose one of 1, 2, 4 or 8!\n", val);
  403. mutex_unlock(&data->update_lock);
  404. return -EINVAL;
  405. }
  406. switch (nr) {
  407. case 0:
  408. reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
  409. break;
  410. case 1:
  411. reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
  412. break;
  413. }
  414. sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
  415. data->fan_min[nr] =
  416. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  417. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  418. mutex_unlock(&data->update_lock);
  419. return count;
  420. }
  421. #define show_fan_offset(offset) \
  422. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  423. show_fan, NULL, offset - 1); \
  424. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  425. show_fan_min, set_fan_min, offset - 1); \
  426. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  427. show_fan_div, set_fan_div, offset - 1);
  428. show_fan_offset(1);
  429. show_fan_offset(2);
  430. /* Alarms */
  431. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct sis5595_data *data = sis5595_update_device(dev);
  435. return sprintf(buf, "%d\n", data->alarms);
  436. }
  437. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  438. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  439. char *buf)
  440. {
  441. struct sis5595_data *data = sis5595_update_device(dev);
  442. int nr = to_sensor_dev_attr(da)->index;
  443. return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
  444. }
  445. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  446. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  447. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  448. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  449. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 15);
  450. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  451. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  452. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 15);
  453. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  454. char *buf)
  455. {
  456. struct sis5595_data *data = dev_get_drvdata(dev);
  457. return sprintf(buf, "%s\n", data->name);
  458. }
  459. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  460. static struct attribute *sis5595_attributes[] = {
  461. &sensor_dev_attr_in0_input.dev_attr.attr,
  462. &sensor_dev_attr_in0_min.dev_attr.attr,
  463. &sensor_dev_attr_in0_max.dev_attr.attr,
  464. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  465. &sensor_dev_attr_in1_input.dev_attr.attr,
  466. &sensor_dev_attr_in1_min.dev_attr.attr,
  467. &sensor_dev_attr_in1_max.dev_attr.attr,
  468. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  469. &sensor_dev_attr_in2_input.dev_attr.attr,
  470. &sensor_dev_attr_in2_min.dev_attr.attr,
  471. &sensor_dev_attr_in2_max.dev_attr.attr,
  472. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  473. &sensor_dev_attr_in3_input.dev_attr.attr,
  474. &sensor_dev_attr_in3_min.dev_attr.attr,
  475. &sensor_dev_attr_in3_max.dev_attr.attr,
  476. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  477. &sensor_dev_attr_fan1_input.dev_attr.attr,
  478. &sensor_dev_attr_fan1_min.dev_attr.attr,
  479. &sensor_dev_attr_fan1_div.dev_attr.attr,
  480. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  481. &sensor_dev_attr_fan2_input.dev_attr.attr,
  482. &sensor_dev_attr_fan2_min.dev_attr.attr,
  483. &sensor_dev_attr_fan2_div.dev_attr.attr,
  484. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  485. &dev_attr_alarms.attr,
  486. &dev_attr_name.attr,
  487. NULL
  488. };
  489. static const struct attribute_group sis5595_group = {
  490. .attrs = sis5595_attributes,
  491. };
  492. static struct attribute *sis5595_attributes_in4[] = {
  493. &sensor_dev_attr_in4_input.dev_attr.attr,
  494. &sensor_dev_attr_in4_min.dev_attr.attr,
  495. &sensor_dev_attr_in4_max.dev_attr.attr,
  496. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  497. NULL
  498. };
  499. static const struct attribute_group sis5595_group_in4 = {
  500. .attrs = sis5595_attributes_in4,
  501. };
  502. static struct attribute *sis5595_attributes_temp1[] = {
  503. &dev_attr_temp1_input.attr,
  504. &dev_attr_temp1_max.attr,
  505. &dev_attr_temp1_max_hyst.attr,
  506. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  507. NULL
  508. };
  509. static const struct attribute_group sis5595_group_temp1 = {
  510. .attrs = sis5595_attributes_temp1,
  511. };
  512. /* This is called when the module is loaded */
  513. static int __devinit sis5595_probe(struct platform_device *pdev)
  514. {
  515. int err = 0;
  516. int i;
  517. struct sis5595_data *data;
  518. struct resource *res;
  519. char val;
  520. /* Reserve the ISA region */
  521. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  522. if (!request_region(res->start, SIS5595_EXTENT,
  523. sis5595_driver.driver.name)) {
  524. err = -EBUSY;
  525. goto exit;
  526. }
  527. data = kzalloc(sizeof(struct sis5595_data), GFP_KERNEL);
  528. if (!data) {
  529. err = -ENOMEM;
  530. goto exit_release;
  531. }
  532. mutex_init(&data->lock);
  533. mutex_init(&data->update_lock);
  534. data->addr = res->start;
  535. data->name = "sis5595";
  536. platform_set_drvdata(pdev, data);
  537. /*
  538. * Check revision and pin registers to determine whether 4 or 5 voltages
  539. */
  540. data->revision = s_bridge->revision;
  541. /* 4 voltages, 1 temp */
  542. data->maxins = 3;
  543. if (data->revision >= REV2MIN) {
  544. pci_read_config_byte(s_bridge, SIS5595_PIN_REG, &val);
  545. if (!(val & 0x80))
  546. /* 5 voltages, no temps */
  547. data->maxins = 4;
  548. }
  549. /* Initialize the SIS5595 chip */
  550. sis5595_init_device(data);
  551. /* A few vars need to be filled upon startup */
  552. for (i = 0; i < 2; i++) {
  553. data->fan_min[i] = sis5595_read_value(data,
  554. SIS5595_REG_FAN_MIN(i));
  555. }
  556. /* Register sysfs hooks */
  557. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group);
  558. if (err)
  559. goto exit_free;
  560. if (data->maxins == 4) {
  561. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_in4);
  562. if (err)
  563. goto exit_remove_files;
  564. } else {
  565. err = sysfs_create_group(&pdev->dev.kobj, &sis5595_group_temp1);
  566. if (err)
  567. goto exit_remove_files;
  568. }
  569. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  570. if (IS_ERR(data->hwmon_dev)) {
  571. err = PTR_ERR(data->hwmon_dev);
  572. goto exit_remove_files;
  573. }
  574. return 0;
  575. exit_remove_files:
  576. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  577. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  578. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  579. exit_free:
  580. kfree(data);
  581. exit_release:
  582. release_region(res->start, SIS5595_EXTENT);
  583. exit:
  584. return err;
  585. }
  586. static int __devexit sis5595_remove(struct platform_device *pdev)
  587. {
  588. struct sis5595_data *data = platform_get_drvdata(pdev);
  589. hwmon_device_unregister(data->hwmon_dev);
  590. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  591. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  592. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  593. release_region(data->addr, SIS5595_EXTENT);
  594. platform_set_drvdata(pdev, NULL);
  595. kfree(data);
  596. return 0;
  597. }
  598. /* ISA access must be locked explicitly. */
  599. static int sis5595_read_value(struct sis5595_data *data, u8 reg)
  600. {
  601. int res;
  602. mutex_lock(&data->lock);
  603. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  604. res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
  605. mutex_unlock(&data->lock);
  606. return res;
  607. }
  608. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
  609. {
  610. mutex_lock(&data->lock);
  611. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  612. outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
  613. mutex_unlock(&data->lock);
  614. }
  615. /* Called when we have found a new SIS5595. */
  616. static void __devinit sis5595_init_device(struct sis5595_data *data)
  617. {
  618. u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
  619. if (!(config & 0x01))
  620. sis5595_write_value(data, SIS5595_REG_CONFIG,
  621. (config & 0xf7) | 0x01);
  622. }
  623. static struct sis5595_data *sis5595_update_device(struct device *dev)
  624. {
  625. struct sis5595_data *data = dev_get_drvdata(dev);
  626. int i;
  627. mutex_lock(&data->update_lock);
  628. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  629. || !data->valid) {
  630. for (i = 0; i <= data->maxins; i++) {
  631. data->in[i] =
  632. sis5595_read_value(data, SIS5595_REG_IN(i));
  633. data->in_min[i] =
  634. sis5595_read_value(data,
  635. SIS5595_REG_IN_MIN(i));
  636. data->in_max[i] =
  637. sis5595_read_value(data,
  638. SIS5595_REG_IN_MAX(i));
  639. }
  640. for (i = 0; i < 2; i++) {
  641. data->fan[i] =
  642. sis5595_read_value(data, SIS5595_REG_FAN(i));
  643. data->fan_min[i] =
  644. sis5595_read_value(data,
  645. SIS5595_REG_FAN_MIN(i));
  646. }
  647. if (data->maxins == 3) {
  648. data->temp =
  649. sis5595_read_value(data, SIS5595_REG_TEMP);
  650. data->temp_over =
  651. sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
  652. data->temp_hyst =
  653. sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
  654. }
  655. i = sis5595_read_value(data, SIS5595_REG_FANDIV);
  656. data->fan_div[0] = (i >> 4) & 0x03;
  657. data->fan_div[1] = i >> 6;
  658. data->alarms =
  659. sis5595_read_value(data, SIS5595_REG_ALARM1) |
  660. (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
  661. data->last_updated = jiffies;
  662. data->valid = 1;
  663. }
  664. mutex_unlock(&data->update_lock);
  665. return data;
  666. }
  667. static DEFINE_PCI_DEVICE_TABLE(sis5595_pci_ids) = {
  668. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  669. { 0, }
  670. };
  671. MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
  672. static int blacklist[] __devinitdata = {
  673. PCI_DEVICE_ID_SI_540,
  674. PCI_DEVICE_ID_SI_550,
  675. PCI_DEVICE_ID_SI_630,
  676. PCI_DEVICE_ID_SI_645,
  677. PCI_DEVICE_ID_SI_730,
  678. PCI_DEVICE_ID_SI_735,
  679. PCI_DEVICE_ID_SI_5511, /*
  680. * 5513 chip has the 0008 device but
  681. * that ID shows up in other chips so we
  682. * use the 5511 ID for recognition
  683. */
  684. PCI_DEVICE_ID_SI_5597,
  685. PCI_DEVICE_ID_SI_5598,
  686. 0 };
  687. static int __devinit sis5595_device_add(unsigned short address)
  688. {
  689. struct resource res = {
  690. .start = address,
  691. .end = address + SIS5595_EXTENT - 1,
  692. .name = "sis5595",
  693. .flags = IORESOURCE_IO,
  694. };
  695. int err;
  696. err = acpi_check_resource_conflict(&res);
  697. if (err)
  698. goto exit;
  699. pdev = platform_device_alloc("sis5595", address);
  700. if (!pdev) {
  701. err = -ENOMEM;
  702. pr_err("Device allocation failed\n");
  703. goto exit;
  704. }
  705. err = platform_device_add_resources(pdev, &res, 1);
  706. if (err) {
  707. pr_err("Device resource addition failed (%d)\n", err);
  708. goto exit_device_put;
  709. }
  710. err = platform_device_add(pdev);
  711. if (err) {
  712. pr_err("Device addition failed (%d)\n", err);
  713. goto exit_device_put;
  714. }
  715. return 0;
  716. exit_device_put:
  717. platform_device_put(pdev);
  718. exit:
  719. return err;
  720. }
  721. static int __devinit sis5595_pci_probe(struct pci_dev *dev,
  722. const struct pci_device_id *id)
  723. {
  724. u16 address;
  725. u8 enable;
  726. int *i;
  727. for (i = blacklist; *i != 0; i++) {
  728. struct pci_dev *d;
  729. d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
  730. if (d) {
  731. dev_err(&d->dev,
  732. "Looked for SIS5595 but found unsupported device %.4x\n",
  733. *i);
  734. pci_dev_put(d);
  735. return -ENODEV;
  736. }
  737. }
  738. force_addr &= ~(SIS5595_EXTENT - 1);
  739. if (force_addr) {
  740. dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
  741. pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
  742. }
  743. if (PCIBIOS_SUCCESSFUL !=
  744. pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
  745. dev_err(&dev->dev, "Failed to read ISA address\n");
  746. return -ENODEV;
  747. }
  748. address &= ~(SIS5595_EXTENT - 1);
  749. if (!address) {
  750. dev_err(&dev->dev,
  751. "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
  752. return -ENODEV;
  753. }
  754. if (force_addr && address != force_addr) {
  755. /* doesn't work for some chips? */
  756. dev_err(&dev->dev, "Failed to force ISA address\n");
  757. return -ENODEV;
  758. }
  759. if (PCIBIOS_SUCCESSFUL !=
  760. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
  761. dev_err(&dev->dev, "Failed to read enable register\n");
  762. return -ENODEV;
  763. }
  764. if (!(enable & 0x80)) {
  765. if ((PCIBIOS_SUCCESSFUL !=
  766. pci_write_config_byte(dev, SIS5595_ENABLE_REG,
  767. enable | 0x80))
  768. || (PCIBIOS_SUCCESSFUL !=
  769. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
  770. || (!(enable & 0x80))) {
  771. /* doesn't work for some chips! */
  772. dev_err(&dev->dev, "Failed to enable HWM device\n");
  773. return -ENODEV;
  774. }
  775. }
  776. if (platform_driver_register(&sis5595_driver)) {
  777. dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
  778. goto exit;
  779. }
  780. s_bridge = pci_dev_get(dev);
  781. /* Sets global pdev as a side effect */
  782. if (sis5595_device_add(address))
  783. goto exit_unregister;
  784. /*
  785. * Always return failure here. This is to allow other drivers to bind
  786. * to this pci device. We don't really want to have control over the
  787. * pci device, we only wanted to read as few register values from it.
  788. */
  789. return -ENODEV;
  790. exit_unregister:
  791. pci_dev_put(dev);
  792. platform_driver_unregister(&sis5595_driver);
  793. exit:
  794. return -ENODEV;
  795. }
  796. static struct pci_driver sis5595_pci_driver = {
  797. .name = "sis5595",
  798. .id_table = sis5595_pci_ids,
  799. .probe = sis5595_pci_probe,
  800. };
  801. static int __init sm_sis5595_init(void)
  802. {
  803. return pci_register_driver(&sis5595_pci_driver);
  804. }
  805. static void __exit sm_sis5595_exit(void)
  806. {
  807. pci_unregister_driver(&sis5595_pci_driver);
  808. if (s_bridge != NULL) {
  809. platform_device_unregister(pdev);
  810. platform_driver_unregister(&sis5595_driver);
  811. pci_dev_put(s_bridge);
  812. s_bridge = NULL;
  813. }
  814. }
  815. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  816. MODULE_DESCRIPTION("SiS 5595 Sensor device");
  817. MODULE_LICENSE("GPL");
  818. module_init(sm_sis5595_init);
  819. module_exit(sm_sis5595_exit);