sis5595.c 25 KB

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