sis5595.c 24 KB

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