sis5595.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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 <jdelvare@suse.de>
  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 = clamp_val(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. if (rpm > 1350000)
  128. return 1;
  129. return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
  130. }
  131. static inline int FAN_FROM_REG(u8 val, int div)
  132. {
  133. return val == 0 ? -1 : val == 255 ? 0 : 1350000 / (val * div);
  134. }
  135. /*
  136. * TEMP: mC (-54.12C to +157.53C)
  137. * REG: 0.83C/bit + 52.12, two's complement
  138. */
  139. static inline int TEMP_FROM_REG(s8 val)
  140. {
  141. return val * 830 + 52120;
  142. }
  143. static inline s8 TEMP_TO_REG(int val)
  144. {
  145. int nval = clamp_val(val, -54120, 157530) ;
  146. return nval < 0 ? (nval - 5212 - 415) / 830 : (nval - 5212 + 415) / 830;
  147. }
  148. /*
  149. * FAN DIV: 1, 2, 4, or 8 (defaults to 2)
  150. * REG: 0, 1, 2, or 3 (respectively) (defaults to 1)
  151. */
  152. static inline u8 DIV_TO_REG(int val)
  153. {
  154. return val == 8 ? 3 : val == 4 ? 2 : val == 1 ? 0 : 1;
  155. }
  156. #define DIV_FROM_REG(val) (1 << (val))
  157. /*
  158. * For each registered chip, we need to keep some data in memory.
  159. * The structure is dynamically allocated.
  160. */
  161. struct sis5595_data {
  162. unsigned short addr;
  163. const char *name;
  164. struct device *hwmon_dev;
  165. struct mutex lock;
  166. struct mutex update_lock;
  167. char valid; /* !=0 if following fields are valid */
  168. unsigned long last_updated; /* In jiffies */
  169. char maxins; /* == 3 if temp enabled, otherwise == 4 */
  170. u8 revision; /* Reg. value */
  171. u8 in[5]; /* Register value */
  172. u8 in_max[5]; /* Register value */
  173. u8 in_min[5]; /* Register value */
  174. u8 fan[2]; /* Register value */
  175. u8 fan_min[2]; /* Register value */
  176. s8 temp; /* Register value */
  177. s8 temp_over; /* Register value */
  178. s8 temp_hyst; /* Register value */
  179. u8 fan_div[2]; /* Register encoding, shifted right */
  180. u16 alarms; /* Register encoding, combined */
  181. };
  182. static struct pci_dev *s_bridge; /* pointer to the (only) sis5595 */
  183. static int sis5595_probe(struct platform_device *pdev);
  184. static int sis5595_remove(struct platform_device *pdev);
  185. static int sis5595_read_value(struct sis5595_data *data, u8 reg);
  186. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value);
  187. static struct sis5595_data *sis5595_update_device(struct device *dev);
  188. static void sis5595_init_device(struct sis5595_data *data);
  189. static struct platform_driver sis5595_driver = {
  190. .driver = {
  191. .owner = THIS_MODULE,
  192. .name = "sis5595",
  193. },
  194. .probe = sis5595_probe,
  195. .remove = sis5595_remove,
  196. };
  197. /* 4 Voltages */
  198. static ssize_t show_in(struct device *dev, struct device_attribute *da,
  199. char *buf)
  200. {
  201. struct sis5595_data *data = sis5595_update_device(dev);
  202. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  203. int nr = attr->index;
  204. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr]));
  205. }
  206. static ssize_t show_in_min(struct device *dev, struct device_attribute *da,
  207. char *buf)
  208. {
  209. struct sis5595_data *data = sis5595_update_device(dev);
  210. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  211. int nr = attr->index;
  212. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr]));
  213. }
  214. static ssize_t show_in_max(struct device *dev, struct device_attribute *da,
  215. char *buf)
  216. {
  217. struct sis5595_data *data = sis5595_update_device(dev);
  218. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  219. int nr = attr->index;
  220. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr]));
  221. }
  222. static ssize_t set_in_min(struct device *dev, struct device_attribute *da,
  223. const char *buf, size_t count)
  224. {
  225. struct sis5595_data *data = dev_get_drvdata(dev);
  226. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  227. int nr = attr->index;
  228. unsigned long val;
  229. int err;
  230. err = kstrtoul(buf, 10, &val);
  231. if (err)
  232. return err;
  233. mutex_lock(&data->update_lock);
  234. data->in_min[nr] = IN_TO_REG(val);
  235. sis5595_write_value(data, SIS5595_REG_IN_MIN(nr), data->in_min[nr]);
  236. mutex_unlock(&data->update_lock);
  237. return count;
  238. }
  239. static ssize_t set_in_max(struct device *dev, struct device_attribute *da,
  240. const char *buf, size_t count)
  241. {
  242. struct sis5595_data *data = dev_get_drvdata(dev);
  243. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  244. int nr = attr->index;
  245. unsigned long val;
  246. int err;
  247. err = kstrtoul(buf, 10, &val);
  248. if (err)
  249. return err;
  250. mutex_lock(&data->update_lock);
  251. data->in_max[nr] = IN_TO_REG(val);
  252. sis5595_write_value(data, SIS5595_REG_IN_MAX(nr), data->in_max[nr]);
  253. mutex_unlock(&data->update_lock);
  254. return count;
  255. }
  256. #define show_in_offset(offset) \
  257. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  258. show_in, NULL, offset); \
  259. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  260. show_in_min, set_in_min, offset); \
  261. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  262. show_in_max, set_in_max, offset);
  263. show_in_offset(0);
  264. show_in_offset(1);
  265. show_in_offset(2);
  266. show_in_offset(3);
  267. show_in_offset(4);
  268. /* Temperature */
  269. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  270. char *buf)
  271. {
  272. struct sis5595_data *data = sis5595_update_device(dev);
  273. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp));
  274. }
  275. static ssize_t show_temp_over(struct device *dev, struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct sis5595_data *data = sis5595_update_device(dev);
  279. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_over));
  280. }
  281. static ssize_t set_temp_over(struct device *dev, struct device_attribute *attr,
  282. const char *buf, size_t count)
  283. {
  284. struct sis5595_data *data = dev_get_drvdata(dev);
  285. long val;
  286. int err;
  287. err = kstrtol(buf, 10, &val);
  288. if (err)
  289. return err;
  290. mutex_lock(&data->update_lock);
  291. data->temp_over = TEMP_TO_REG(val);
  292. sis5595_write_value(data, SIS5595_REG_TEMP_OVER, data->temp_over);
  293. mutex_unlock(&data->update_lock);
  294. return count;
  295. }
  296. static ssize_t show_temp_hyst(struct device *dev, struct device_attribute *attr,
  297. char *buf)
  298. {
  299. struct sis5595_data *data = sis5595_update_device(dev);
  300. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_hyst));
  301. }
  302. static ssize_t set_temp_hyst(struct device *dev, struct device_attribute *attr,
  303. const char *buf, size_t count)
  304. {
  305. struct sis5595_data *data = dev_get_drvdata(dev);
  306. long val;
  307. int err;
  308. err = kstrtol(buf, 10, &val);
  309. if (err)
  310. return err;
  311. mutex_lock(&data->update_lock);
  312. data->temp_hyst = TEMP_TO_REG(val);
  313. sis5595_write_value(data, SIS5595_REG_TEMP_HYST, data->temp_hyst);
  314. mutex_unlock(&data->update_lock);
  315. return count;
  316. }
  317. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  318. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR,
  319. show_temp_over, set_temp_over);
  320. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR,
  321. show_temp_hyst, set_temp_hyst);
  322. /* 2 Fans */
  323. static ssize_t show_fan(struct device *dev, struct device_attribute *da,
  324. char *buf)
  325. {
  326. struct sis5595_data *data = sis5595_update_device(dev);
  327. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  328. int nr = attr->index;
  329. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  330. DIV_FROM_REG(data->fan_div[nr])));
  331. }
  332. static ssize_t show_fan_min(struct device *dev, struct device_attribute *da,
  333. char *buf)
  334. {
  335. struct sis5595_data *data = sis5595_update_device(dev);
  336. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  337. int nr = attr->index;
  338. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  339. DIV_FROM_REG(data->fan_div[nr])));
  340. }
  341. static ssize_t set_fan_min(struct device *dev, struct device_attribute *da,
  342. const char *buf, size_t count)
  343. {
  344. struct sis5595_data *data = dev_get_drvdata(dev);
  345. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  346. int nr = attr->index;
  347. unsigned long val;
  348. int err;
  349. err = kstrtoul(buf, 10, &val);
  350. if (err)
  351. return err;
  352. mutex_lock(&data->update_lock);
  353. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  354. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  355. mutex_unlock(&data->update_lock);
  356. return count;
  357. }
  358. static ssize_t show_fan_div(struct device *dev, struct device_attribute *da,
  359. char *buf)
  360. {
  361. struct sis5595_data *data = sis5595_update_device(dev);
  362. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  363. int nr = attr->index;
  364. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  365. }
  366. /*
  367. * Note: we save and restore the fan minimum here, because its value is
  368. * determined in part by the fan divisor. This follows the principle of
  369. * least surprise; the user doesn't expect the fan minimum to change just
  370. * because the divisor changed.
  371. */
  372. static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
  373. const char *buf, size_t count)
  374. {
  375. struct sis5595_data *data = dev_get_drvdata(dev);
  376. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  377. int nr = attr->index;
  378. unsigned long min;
  379. int reg;
  380. unsigned long val;
  381. int err;
  382. err = kstrtoul(buf, 10, &val);
  383. if (err)
  384. return err;
  385. mutex_lock(&data->update_lock);
  386. min = FAN_FROM_REG(data->fan_min[nr],
  387. DIV_FROM_REG(data->fan_div[nr]));
  388. reg = sis5595_read_value(data, SIS5595_REG_FANDIV);
  389. switch (val) {
  390. case 1:
  391. data->fan_div[nr] = 0;
  392. break;
  393. case 2:
  394. data->fan_div[nr] = 1;
  395. break;
  396. case 4:
  397. data->fan_div[nr] = 2;
  398. break;
  399. case 8:
  400. data->fan_div[nr] = 3;
  401. break;
  402. default:
  403. dev_err(dev,
  404. "fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
  405. val);
  406. mutex_unlock(&data->update_lock);
  407. return -EINVAL;
  408. }
  409. switch (nr) {
  410. case 0:
  411. reg = (reg & 0xcf) | (data->fan_div[nr] << 4);
  412. break;
  413. case 1:
  414. reg = (reg & 0x3f) | (data->fan_div[nr] << 6);
  415. break;
  416. }
  417. sis5595_write_value(data, SIS5595_REG_FANDIV, reg);
  418. data->fan_min[nr] =
  419. FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  420. sis5595_write_value(data, SIS5595_REG_FAN_MIN(nr), data->fan_min[nr]);
  421. mutex_unlock(&data->update_lock);
  422. return count;
  423. }
  424. #define show_fan_offset(offset) \
  425. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  426. show_fan, NULL, offset - 1); \
  427. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  428. show_fan_min, set_fan_min, offset - 1); \
  429. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  430. show_fan_div, set_fan_div, offset - 1);
  431. show_fan_offset(1);
  432. show_fan_offset(2);
  433. /* Alarms */
  434. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  435. char *buf)
  436. {
  437. struct sis5595_data *data = sis5595_update_device(dev);
  438. return sprintf(buf, "%d\n", data->alarms);
  439. }
  440. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  441. static ssize_t show_alarm(struct device *dev, struct device_attribute *da,
  442. char *buf)
  443. {
  444. struct sis5595_data *data = sis5595_update_device(dev);
  445. int nr = to_sensor_dev_attr(da)->index;
  446. return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
  447. }
  448. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  449. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  450. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  451. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  452. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 15);
  453. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  454. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  455. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 15);
  456. static ssize_t show_name(struct device *dev, struct device_attribute *attr,
  457. char *buf)
  458. {
  459. struct sis5595_data *data = dev_get_drvdata(dev);
  460. return sprintf(buf, "%s\n", data->name);
  461. }
  462. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  463. static struct attribute *sis5595_attributes[] = {
  464. &sensor_dev_attr_in0_input.dev_attr.attr,
  465. &sensor_dev_attr_in0_min.dev_attr.attr,
  466. &sensor_dev_attr_in0_max.dev_attr.attr,
  467. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  468. &sensor_dev_attr_in1_input.dev_attr.attr,
  469. &sensor_dev_attr_in1_min.dev_attr.attr,
  470. &sensor_dev_attr_in1_max.dev_attr.attr,
  471. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  472. &sensor_dev_attr_in2_input.dev_attr.attr,
  473. &sensor_dev_attr_in2_min.dev_attr.attr,
  474. &sensor_dev_attr_in2_max.dev_attr.attr,
  475. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  476. &sensor_dev_attr_in3_input.dev_attr.attr,
  477. &sensor_dev_attr_in3_min.dev_attr.attr,
  478. &sensor_dev_attr_in3_max.dev_attr.attr,
  479. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  480. &sensor_dev_attr_fan1_input.dev_attr.attr,
  481. &sensor_dev_attr_fan1_min.dev_attr.attr,
  482. &sensor_dev_attr_fan1_div.dev_attr.attr,
  483. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  484. &sensor_dev_attr_fan2_input.dev_attr.attr,
  485. &sensor_dev_attr_fan2_min.dev_attr.attr,
  486. &sensor_dev_attr_fan2_div.dev_attr.attr,
  487. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  488. &dev_attr_alarms.attr,
  489. &dev_attr_name.attr,
  490. NULL
  491. };
  492. static const struct attribute_group sis5595_group = {
  493. .attrs = sis5595_attributes,
  494. };
  495. static struct attribute *sis5595_attributes_in4[] = {
  496. &sensor_dev_attr_in4_input.dev_attr.attr,
  497. &sensor_dev_attr_in4_min.dev_attr.attr,
  498. &sensor_dev_attr_in4_max.dev_attr.attr,
  499. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  500. NULL
  501. };
  502. static const struct attribute_group sis5595_group_in4 = {
  503. .attrs = sis5595_attributes_in4,
  504. };
  505. static struct attribute *sis5595_attributes_temp1[] = {
  506. &dev_attr_temp1_input.attr,
  507. &dev_attr_temp1_max.attr,
  508. &dev_attr_temp1_max_hyst.attr,
  509. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  510. NULL
  511. };
  512. static const struct attribute_group sis5595_group_temp1 = {
  513. .attrs = sis5595_attributes_temp1,
  514. };
  515. /* This is called when the module is loaded */
  516. static int sis5595_probe(struct platform_device *pdev)
  517. {
  518. int err = 0;
  519. int i;
  520. struct sis5595_data *data;
  521. struct resource *res;
  522. char val;
  523. /* Reserve the ISA region */
  524. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  525. if (!devm_request_region(&pdev->dev, res->start, SIS5595_EXTENT,
  526. sis5595_driver.driver.name))
  527. return -EBUSY;
  528. data = devm_kzalloc(&pdev->dev, sizeof(struct sis5595_data),
  529. GFP_KERNEL);
  530. if (!data)
  531. return -ENOMEM;
  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. return err;
  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. return err;
  580. }
  581. static int sis5595_remove(struct platform_device *pdev)
  582. {
  583. struct sis5595_data *data = platform_get_drvdata(pdev);
  584. hwmon_device_unregister(data->hwmon_dev);
  585. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group);
  586. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_in4);
  587. sysfs_remove_group(&pdev->dev.kobj, &sis5595_group_temp1);
  588. return 0;
  589. }
  590. /* ISA access must be locked explicitly. */
  591. static int sis5595_read_value(struct sis5595_data *data, u8 reg)
  592. {
  593. int res;
  594. mutex_lock(&data->lock);
  595. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  596. res = inb_p(data->addr + SIS5595_DATA_REG_OFFSET);
  597. mutex_unlock(&data->lock);
  598. return res;
  599. }
  600. static void sis5595_write_value(struct sis5595_data *data, u8 reg, u8 value)
  601. {
  602. mutex_lock(&data->lock);
  603. outb_p(reg, data->addr + SIS5595_ADDR_REG_OFFSET);
  604. outb_p(value, data->addr + SIS5595_DATA_REG_OFFSET);
  605. mutex_unlock(&data->lock);
  606. }
  607. /* Called when we have found a new SIS5595. */
  608. static void sis5595_init_device(struct sis5595_data *data)
  609. {
  610. u8 config = sis5595_read_value(data, SIS5595_REG_CONFIG);
  611. if (!(config & 0x01))
  612. sis5595_write_value(data, SIS5595_REG_CONFIG,
  613. (config & 0xf7) | 0x01);
  614. }
  615. static struct sis5595_data *sis5595_update_device(struct device *dev)
  616. {
  617. struct sis5595_data *data = dev_get_drvdata(dev);
  618. int i;
  619. mutex_lock(&data->update_lock);
  620. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  621. || !data->valid) {
  622. for (i = 0; i <= data->maxins; i++) {
  623. data->in[i] =
  624. sis5595_read_value(data, SIS5595_REG_IN(i));
  625. data->in_min[i] =
  626. sis5595_read_value(data,
  627. SIS5595_REG_IN_MIN(i));
  628. data->in_max[i] =
  629. sis5595_read_value(data,
  630. SIS5595_REG_IN_MAX(i));
  631. }
  632. for (i = 0; i < 2; i++) {
  633. data->fan[i] =
  634. sis5595_read_value(data, SIS5595_REG_FAN(i));
  635. data->fan_min[i] =
  636. sis5595_read_value(data,
  637. SIS5595_REG_FAN_MIN(i));
  638. }
  639. if (data->maxins == 3) {
  640. data->temp =
  641. sis5595_read_value(data, SIS5595_REG_TEMP);
  642. data->temp_over =
  643. sis5595_read_value(data, SIS5595_REG_TEMP_OVER);
  644. data->temp_hyst =
  645. sis5595_read_value(data, SIS5595_REG_TEMP_HYST);
  646. }
  647. i = sis5595_read_value(data, SIS5595_REG_FANDIV);
  648. data->fan_div[0] = (i >> 4) & 0x03;
  649. data->fan_div[1] = i >> 6;
  650. data->alarms =
  651. sis5595_read_value(data, SIS5595_REG_ALARM1) |
  652. (sis5595_read_value(data, SIS5595_REG_ALARM2) << 8);
  653. data->last_updated = jiffies;
  654. data->valid = 1;
  655. }
  656. mutex_unlock(&data->update_lock);
  657. return data;
  658. }
  659. static const struct pci_device_id sis5595_pci_ids[] = {
  660. { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
  661. { 0, }
  662. };
  663. MODULE_DEVICE_TABLE(pci, sis5595_pci_ids);
  664. static int blacklist[] = {
  665. PCI_DEVICE_ID_SI_540,
  666. PCI_DEVICE_ID_SI_550,
  667. PCI_DEVICE_ID_SI_630,
  668. PCI_DEVICE_ID_SI_645,
  669. PCI_DEVICE_ID_SI_730,
  670. PCI_DEVICE_ID_SI_735,
  671. PCI_DEVICE_ID_SI_5511, /*
  672. * 5513 chip has the 0008 device but
  673. * that ID shows up in other chips so we
  674. * use the 5511 ID for recognition
  675. */
  676. PCI_DEVICE_ID_SI_5597,
  677. PCI_DEVICE_ID_SI_5598,
  678. 0 };
  679. static int sis5595_device_add(unsigned short address)
  680. {
  681. struct resource res = {
  682. .start = address,
  683. .end = address + SIS5595_EXTENT - 1,
  684. .name = "sis5595",
  685. .flags = IORESOURCE_IO,
  686. };
  687. int err;
  688. err = acpi_check_resource_conflict(&res);
  689. if (err)
  690. goto exit;
  691. pdev = platform_device_alloc("sis5595", address);
  692. if (!pdev) {
  693. err = -ENOMEM;
  694. pr_err("Device allocation failed\n");
  695. goto exit;
  696. }
  697. err = platform_device_add_resources(pdev, &res, 1);
  698. if (err) {
  699. pr_err("Device resource addition failed (%d)\n", err);
  700. goto exit_device_put;
  701. }
  702. err = platform_device_add(pdev);
  703. if (err) {
  704. pr_err("Device addition failed (%d)\n", err);
  705. goto exit_device_put;
  706. }
  707. return 0;
  708. exit_device_put:
  709. platform_device_put(pdev);
  710. exit:
  711. return err;
  712. }
  713. static int sis5595_pci_probe(struct pci_dev *dev,
  714. const struct pci_device_id *id)
  715. {
  716. u16 address;
  717. u8 enable;
  718. int *i;
  719. for (i = blacklist; *i != 0; i++) {
  720. struct pci_dev *d;
  721. d = pci_get_device(PCI_VENDOR_ID_SI, *i, NULL);
  722. if (d) {
  723. dev_err(&d->dev,
  724. "Looked for SIS5595 but found unsupported device %.4x\n",
  725. *i);
  726. pci_dev_put(d);
  727. return -ENODEV;
  728. }
  729. }
  730. force_addr &= ~(SIS5595_EXTENT - 1);
  731. if (force_addr) {
  732. dev_warn(&dev->dev, "Forcing ISA address 0x%x\n", force_addr);
  733. pci_write_config_word(dev, SIS5595_BASE_REG, force_addr);
  734. }
  735. if (PCIBIOS_SUCCESSFUL !=
  736. pci_read_config_word(dev, SIS5595_BASE_REG, &address)) {
  737. dev_err(&dev->dev, "Failed to read ISA address\n");
  738. return -ENODEV;
  739. }
  740. address &= ~(SIS5595_EXTENT - 1);
  741. if (!address) {
  742. dev_err(&dev->dev,
  743. "Base address not set - upgrade BIOS or use force_addr=0xaddr\n");
  744. return -ENODEV;
  745. }
  746. if (force_addr && address != force_addr) {
  747. /* doesn't work for some chips? */
  748. dev_err(&dev->dev, "Failed to force ISA address\n");
  749. return -ENODEV;
  750. }
  751. if (PCIBIOS_SUCCESSFUL !=
  752. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable)) {
  753. dev_err(&dev->dev, "Failed to read enable register\n");
  754. return -ENODEV;
  755. }
  756. if (!(enable & 0x80)) {
  757. if ((PCIBIOS_SUCCESSFUL !=
  758. pci_write_config_byte(dev, SIS5595_ENABLE_REG,
  759. enable | 0x80))
  760. || (PCIBIOS_SUCCESSFUL !=
  761. pci_read_config_byte(dev, SIS5595_ENABLE_REG, &enable))
  762. || (!(enable & 0x80))) {
  763. /* doesn't work for some chips! */
  764. dev_err(&dev->dev, "Failed to enable HWM device\n");
  765. return -ENODEV;
  766. }
  767. }
  768. if (platform_driver_register(&sis5595_driver)) {
  769. dev_dbg(&dev->dev, "Failed to register sis5595 driver\n");
  770. goto exit;
  771. }
  772. s_bridge = pci_dev_get(dev);
  773. /* Sets global pdev as a side effect */
  774. if (sis5595_device_add(address))
  775. goto exit_unregister;
  776. /*
  777. * Always return failure here. This is to allow other drivers to bind
  778. * to this pci device. We don't really want to have control over the
  779. * pci device, we only wanted to read as few register values from it.
  780. */
  781. return -ENODEV;
  782. exit_unregister:
  783. pci_dev_put(dev);
  784. platform_driver_unregister(&sis5595_driver);
  785. exit:
  786. return -ENODEV;
  787. }
  788. static struct pci_driver sis5595_pci_driver = {
  789. .name = "sis5595",
  790. .id_table = sis5595_pci_ids,
  791. .probe = sis5595_pci_probe,
  792. };
  793. static int __init sm_sis5595_init(void)
  794. {
  795. return pci_register_driver(&sis5595_pci_driver);
  796. }
  797. static void __exit sm_sis5595_exit(void)
  798. {
  799. pci_unregister_driver(&sis5595_pci_driver);
  800. if (s_bridge != NULL) {
  801. platform_device_unregister(pdev);
  802. platform_driver_unregister(&sis5595_driver);
  803. pci_dev_put(s_bridge);
  804. s_bridge = NULL;
  805. }
  806. }
  807. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  808. MODULE_DESCRIPTION("SiS 5595 Sensor device");
  809. MODULE_LICENSE("GPL");
  810. module_init(sm_sis5595_init);
  811. module_exit(sm_sis5595_exit);