vt8231.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*
  2. vt8231.c - Part of lm_sensors, Linux kernel modules
  3. for hardware monitoring
  4. Copyright (c) 2005 Roger Lucas <vt8231@hiddenengine.co.uk>
  5. Copyright (c) 2002 Mark D. Studebaker <mdsxyz123@yahoo.com>
  6. Aaron M. Marsh <amarsh@sdf.lonestar.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /* Supports VIA VT8231 South Bridge embedded sensors
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #include <linux/hwmon-vid.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.h>
  33. #include <linux/acpi.h>
  34. #include <linux/io.h>
  35. static int force_addr;
  36. module_param(force_addr, int, 0);
  37. MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
  38. static struct platform_device *pdev;
  39. #define VT8231_EXTENT 0x80
  40. #define VT8231_BASE_REG 0x70
  41. #define VT8231_ENABLE_REG 0x74
  42. /* The VT8231 registers
  43. The reset value for the input channel configuration is used (Reg 0x4A=0x07)
  44. which sets the selected inputs marked with '*' below if multiple options are
  45. possible:
  46. Voltage Mode Temperature Mode
  47. Sensor Linux Id Linux Id VIA Id
  48. -------- -------- -------- ------
  49. CPU Diode N/A temp1 0
  50. UIC1 in0 temp2 * 1
  51. UIC2 in1 * temp3 2
  52. UIC3 in2 * temp4 3
  53. UIC4 in3 * temp5 4
  54. UIC5 in4 * temp6 5
  55. 3.3V in5 N/A
  56. Note that the BIOS may set the configuration register to a different value
  57. to match the motherboard configuration.
  58. */
  59. /* fans numbered 0-1 */
  60. #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
  61. #define VT8231_REG_FAN(nr) (0x29 + (nr))
  62. /* Voltage inputs numbered 0-5 */
  63. static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
  64. static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
  65. static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
  66. /* Temperatures are numbered 1-6 according to the Linux kernel specification.
  67. **
  68. ** In the VIA datasheet, however, the temperatures are numbered from zero.
  69. ** Since it is important that this driver can easily be compared to the VIA
  70. ** datasheet, we will use the VIA numbering within this driver and map the
  71. ** kernel sysfs device name to the VIA number in the sysfs callback.
  72. */
  73. #define VT8231_REG_TEMP_LOW01 0x49
  74. #define VT8231_REG_TEMP_LOW25 0x4d
  75. static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
  76. static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
  77. static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
  78. #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
  79. #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
  80. #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
  81. #define VT8231_REG_CONFIG 0x40
  82. #define VT8231_REG_ALARM1 0x41
  83. #define VT8231_REG_ALARM2 0x42
  84. #define VT8231_REG_FANDIV 0x47
  85. #define VT8231_REG_UCH_CONFIG 0x4a
  86. #define VT8231_REG_TEMP1_CONFIG 0x4b
  87. #define VT8231_REG_TEMP2_CONFIG 0x4c
  88. /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
  89. ** numbering
  90. */
  91. #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
  92. ((ch_config) >> ((i)+1)) & 0x01)
  93. /* voltages 0-5 */
  94. #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
  95. !(((ch_config) >> ((i)+2)) & 0x01))
  96. #define DIV_FROM_REG(val) (1 << (val))
  97. /* NB The values returned here are NOT temperatures. The calibration curves
  98. ** for the thermistor curves are board-specific and must go in the
  99. ** sensors.conf file. Temperature sensors are actually ten bits, but the
  100. ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
  101. ** register. The temperature value returned should have a magnitude of 3,
  102. ** so we use the VIA scaling as the "true" scaling and use the remaining 2
  103. ** LSBs as fractional precision.
  104. **
  105. ** All the on-chip hardware temperature comparisons for the alarms are only
  106. ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
  107. ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
  108. ** ignored.
  109. */
  110. /******** FAN RPM CONVERSIONS ********
  111. ** This chip saturates back at 0, not at 255 like many the other chips.
  112. ** So, 0 means 0 RPM
  113. */
  114. static inline u8 FAN_TO_REG(long rpm, int div)
  115. {
  116. if (rpm == 0)
  117. return 0;
  118. return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
  119. }
  120. #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
  121. struct vt8231_data {
  122. unsigned short addr;
  123. const char *name;
  124. struct mutex update_lock;
  125. struct device *hwmon_dev;
  126. char valid; /* !=0 if following fields are valid */
  127. unsigned long last_updated; /* In jiffies */
  128. u8 in[6]; /* Register value */
  129. u8 in_max[6]; /* Register value */
  130. u8 in_min[6]; /* Register value */
  131. u16 temp[6]; /* Register value 10 bit, right aligned */
  132. u8 temp_max[6]; /* Register value */
  133. u8 temp_min[6]; /* Register value */
  134. u8 fan[2]; /* Register value */
  135. u8 fan_min[2]; /* Register value */
  136. u8 fan_div[2]; /* Register encoding, shifted right */
  137. u16 alarms; /* Register encoding */
  138. u8 uch_config;
  139. };
  140. static struct pci_dev *s_bridge;
  141. static int vt8231_probe(struct platform_device *pdev);
  142. static int __devexit vt8231_remove(struct platform_device *pdev);
  143. static struct vt8231_data *vt8231_update_device(struct device *dev);
  144. static void vt8231_init_device(struct vt8231_data *data);
  145. static inline int vt8231_read_value(struct vt8231_data *data, u8 reg)
  146. {
  147. return inb_p(data->addr + reg);
  148. }
  149. static inline void vt8231_write_value(struct vt8231_data *data, u8 reg,
  150. u8 value)
  151. {
  152. outb_p(value, data->addr + reg);
  153. }
  154. /* following are the sysfs callback functions */
  155. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  156. char *buf)
  157. {
  158. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  159. int nr = sensor_attr->index;
  160. struct vt8231_data *data = vt8231_update_device(dev);
  161. return sprintf(buf, "%d\n", ((data->in[nr] - 3) * 10000) / 958);
  162. }
  163. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  164. char *buf)
  165. {
  166. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  167. int nr = sensor_attr->index;
  168. struct vt8231_data *data = vt8231_update_device(dev);
  169. return sprintf(buf, "%d\n", ((data->in_min[nr] - 3) * 10000) / 958);
  170. }
  171. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  172. char *buf)
  173. {
  174. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  175. int nr = sensor_attr->index;
  176. struct vt8231_data *data = vt8231_update_device(dev);
  177. return sprintf(buf, "%d\n", (((data->in_max[nr] - 3) * 10000) / 958));
  178. }
  179. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  180. const char *buf, size_t count)
  181. {
  182. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  183. int nr = sensor_attr->index;
  184. struct vt8231_data *data = dev_get_drvdata(dev);
  185. unsigned long val = simple_strtoul(buf, NULL, 10);
  186. mutex_lock(&data->update_lock);
  187. data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
  188. vt8231_write_value(data, regvoltmin[nr], data->in_min[nr]);
  189. mutex_unlock(&data->update_lock);
  190. return count;
  191. }
  192. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  193. const char *buf, size_t count)
  194. {
  195. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  196. int nr = sensor_attr->index;
  197. struct vt8231_data *data = dev_get_drvdata(dev);
  198. unsigned long val = simple_strtoul(buf, NULL, 10);
  199. mutex_lock(&data->update_lock);
  200. data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
  201. vt8231_write_value(data, regvoltmax[nr], data->in_max[nr]);
  202. mutex_unlock(&data->update_lock);
  203. return count;
  204. }
  205. /* Special case for input 5 as this has 3.3V scaling built into the chip */
  206. static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
  207. char *buf)
  208. {
  209. struct vt8231_data *data = vt8231_update_device(dev);
  210. return sprintf(buf, "%d\n",
  211. (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
  212. }
  213. static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
  214. char *buf)
  215. {
  216. struct vt8231_data *data = vt8231_update_device(dev);
  217. return sprintf(buf, "%d\n",
  218. (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
  219. }
  220. static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct vt8231_data *data = vt8231_update_device(dev);
  224. return sprintf(buf, "%d\n",
  225. (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
  226. }
  227. static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
  228. const char *buf, size_t count)
  229. {
  230. struct vt8231_data *data = dev_get_drvdata(dev);
  231. unsigned long val = simple_strtoul(buf, NULL, 10);
  232. mutex_lock(&data->update_lock);
  233. data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
  234. 0, 255);
  235. vt8231_write_value(data, regvoltmin[5], data->in_min[5]);
  236. mutex_unlock(&data->update_lock);
  237. return count;
  238. }
  239. static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
  240. const char *buf, size_t count)
  241. {
  242. struct vt8231_data *data = dev_get_drvdata(dev);
  243. unsigned long val = simple_strtoul(buf, NULL, 10);
  244. mutex_lock(&data->update_lock);
  245. data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
  246. 0, 255);
  247. vt8231_write_value(data, regvoltmax[5], data->in_max[5]);
  248. mutex_unlock(&data->update_lock);
  249. return count;
  250. }
  251. #define define_voltage_sysfs(offset) \
  252. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  253. show_in, NULL, offset); \
  254. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  255. show_in_min, set_in_min, offset); \
  256. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  257. show_in_max, set_in_max, offset)
  258. define_voltage_sysfs(0);
  259. define_voltage_sysfs(1);
  260. define_voltage_sysfs(2);
  261. define_voltage_sysfs(3);
  262. define_voltage_sysfs(4);
  263. static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
  264. static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
  265. static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
  266. /* Temperatures */
  267. static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
  268. char *buf)
  269. {
  270. struct vt8231_data *data = vt8231_update_device(dev);
  271. return sprintf(buf, "%d\n", data->temp[0] * 250);
  272. }
  273. static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
  274. char *buf)
  275. {
  276. struct vt8231_data *data = vt8231_update_device(dev);
  277. return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
  278. }
  279. static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
  280. char *buf)
  281. {
  282. struct vt8231_data *data = vt8231_update_device(dev);
  283. return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
  284. }
  285. static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
  286. const char *buf, size_t count)
  287. {
  288. struct vt8231_data *data = dev_get_drvdata(dev);
  289. int val = simple_strtol(buf, NULL, 10);
  290. mutex_lock(&data->update_lock);
  291. data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
  292. vt8231_write_value(data, regtempmax[0], data->temp_max[0]);
  293. mutex_unlock(&data->update_lock);
  294. return count;
  295. }
  296. static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
  297. const char *buf, size_t count)
  298. {
  299. struct vt8231_data *data = dev_get_drvdata(dev);
  300. int val = simple_strtol(buf, NULL, 10);
  301. mutex_lock(&data->update_lock);
  302. data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
  303. vt8231_write_value(data, regtempmin[0], data->temp_min[0]);
  304. mutex_unlock(&data->update_lock);
  305. return count;
  306. }
  307. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  308. char *buf)
  309. {
  310. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  311. int nr = sensor_attr->index;
  312. struct vt8231_data *data = vt8231_update_device(dev);
  313. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  314. }
  315. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  316. char *buf)
  317. {
  318. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  319. int nr = sensor_attr->index;
  320. struct vt8231_data *data = vt8231_update_device(dev);
  321. return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
  322. }
  323. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  324. char *buf)
  325. {
  326. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  327. int nr = sensor_attr->index;
  328. struct vt8231_data *data = vt8231_update_device(dev);
  329. return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
  330. }
  331. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  332. const char *buf, size_t count)
  333. {
  334. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  335. int nr = sensor_attr->index;
  336. struct vt8231_data *data = dev_get_drvdata(dev);
  337. int val = simple_strtol(buf, NULL, 10);
  338. mutex_lock(&data->update_lock);
  339. data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
  340. vt8231_write_value(data, regtempmax[nr], data->temp_max[nr]);
  341. mutex_unlock(&data->update_lock);
  342. return count;
  343. }
  344. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  345. const char *buf, size_t count)
  346. {
  347. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  348. int nr = sensor_attr->index;
  349. struct vt8231_data *data = dev_get_drvdata(dev);
  350. int val = simple_strtol(buf, NULL, 10);
  351. mutex_lock(&data->update_lock);
  352. data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
  353. vt8231_write_value(data, regtempmin[nr], data->temp_min[nr]);
  354. mutex_unlock(&data->update_lock);
  355. return count;
  356. }
  357. /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
  358. ** temperature sensor numbering (0-5)
  359. */
  360. #define define_temperature_sysfs(offset) \
  361. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  362. show_temp, NULL, offset - 1); \
  363. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  364. show_temp_max, set_temp_max, offset - 1); \
  365. static SENSOR_DEVICE_ATTR(temp##offset##_max_hyst, S_IRUGO | S_IWUSR, \
  366. show_temp_min, set_temp_min, offset - 1)
  367. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
  368. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
  369. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
  370. define_temperature_sysfs(2);
  371. define_temperature_sysfs(3);
  372. define_temperature_sysfs(4);
  373. define_temperature_sysfs(5);
  374. define_temperature_sysfs(6);
  375. /* Fans */
  376. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  377. char *buf)
  378. {
  379. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  380. int nr = sensor_attr->index;
  381. struct vt8231_data *data = vt8231_update_device(dev);
  382. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  383. DIV_FROM_REG(data->fan_div[nr])));
  384. }
  385. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  386. char *buf)
  387. {
  388. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  389. int nr = sensor_attr->index;
  390. struct vt8231_data *data = vt8231_update_device(dev);
  391. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  392. DIV_FROM_REG(data->fan_div[nr])));
  393. }
  394. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  395. char *buf)
  396. {
  397. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  398. int nr = sensor_attr->index;
  399. struct vt8231_data *data = vt8231_update_device(dev);
  400. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  401. }
  402. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  403. const char *buf, size_t count)
  404. {
  405. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  406. int nr = sensor_attr->index;
  407. struct vt8231_data *data = dev_get_drvdata(dev);
  408. int val = simple_strtoul(buf, NULL, 10);
  409. mutex_lock(&data->update_lock);
  410. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  411. vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
  412. mutex_unlock(&data->update_lock);
  413. return count;
  414. }
  415. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  416. const char *buf, size_t count)
  417. {
  418. struct vt8231_data *data = dev_get_drvdata(dev);
  419. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  420. unsigned long val = simple_strtoul(buf, NULL, 10);
  421. int nr = sensor_attr->index;
  422. int old = vt8231_read_value(data, VT8231_REG_FANDIV);
  423. long min = FAN_FROM_REG(data->fan_min[nr],
  424. DIV_FROM_REG(data->fan_div[nr]));
  425. mutex_lock(&data->update_lock);
  426. switch (val) {
  427. case 1: data->fan_div[nr] = 0; break;
  428. case 2: data->fan_div[nr] = 1; break;
  429. case 4: data->fan_div[nr] = 2; break;
  430. case 8: data->fan_div[nr] = 3; break;
  431. default:
  432. dev_err(dev, "fan_div value %ld not supported. "
  433. "Choose one of 1, 2, 4 or 8!\n", val);
  434. mutex_unlock(&data->update_lock);
  435. return -EINVAL;
  436. }
  437. /* Correct the fan minimum speed */
  438. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  439. vt8231_write_value(data, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
  440. old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
  441. vt8231_write_value(data, VT8231_REG_FANDIV, old);
  442. mutex_unlock(&data->update_lock);
  443. return count;
  444. }
  445. #define define_fan_sysfs(offset) \
  446. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  447. show_fan, NULL, offset - 1); \
  448. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  449. show_fan_div, set_fan_div, offset - 1); \
  450. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  451. show_fan_min, set_fan_min, offset - 1)
  452. define_fan_sysfs(1);
  453. define_fan_sysfs(2);
  454. /* Alarms */
  455. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  456. char *buf)
  457. {
  458. struct vt8231_data *data = vt8231_update_device(dev);
  459. return sprintf(buf, "%d\n", data->alarms);
  460. }
  461. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  462. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  463. char *buf)
  464. {
  465. int bitnr = to_sensor_dev_attr(attr)->index;
  466. struct vt8231_data *data = vt8231_update_device(dev);
  467. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  468. }
  469. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  470. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 11);
  471. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0);
  472. static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL, 1);
  473. static SENSOR_DEVICE_ATTR(temp5_alarm, S_IRUGO, show_alarm, NULL, 3);
  474. static SENSOR_DEVICE_ATTR(temp6_alarm, S_IRUGO, show_alarm, NULL, 8);
  475. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 11);
  476. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0);
  477. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 1);
  478. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  479. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  480. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 2);
  481. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  482. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  483. static ssize_t show_name(struct device *dev, struct device_attribute
  484. *devattr, char *buf)
  485. {
  486. struct vt8231_data *data = dev_get_drvdata(dev);
  487. return sprintf(buf, "%s\n", data->name);
  488. }
  489. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  490. static struct attribute *vt8231_attributes_temps[6][5] = {
  491. {
  492. &dev_attr_temp1_input.attr,
  493. &dev_attr_temp1_max_hyst.attr,
  494. &dev_attr_temp1_max.attr,
  495. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  496. NULL
  497. }, {
  498. &sensor_dev_attr_temp2_input.dev_attr.attr,
  499. &sensor_dev_attr_temp2_max_hyst.dev_attr.attr,
  500. &sensor_dev_attr_temp2_max.dev_attr.attr,
  501. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  502. NULL
  503. }, {
  504. &sensor_dev_attr_temp3_input.dev_attr.attr,
  505. &sensor_dev_attr_temp3_max_hyst.dev_attr.attr,
  506. &sensor_dev_attr_temp3_max.dev_attr.attr,
  507. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  508. NULL
  509. }, {
  510. &sensor_dev_attr_temp4_input.dev_attr.attr,
  511. &sensor_dev_attr_temp4_max_hyst.dev_attr.attr,
  512. &sensor_dev_attr_temp4_max.dev_attr.attr,
  513. &sensor_dev_attr_temp4_alarm.dev_attr.attr,
  514. NULL
  515. }, {
  516. &sensor_dev_attr_temp5_input.dev_attr.attr,
  517. &sensor_dev_attr_temp5_max_hyst.dev_attr.attr,
  518. &sensor_dev_attr_temp5_max.dev_attr.attr,
  519. &sensor_dev_attr_temp5_alarm.dev_attr.attr,
  520. NULL
  521. }, {
  522. &sensor_dev_attr_temp6_input.dev_attr.attr,
  523. &sensor_dev_attr_temp6_max_hyst.dev_attr.attr,
  524. &sensor_dev_attr_temp6_max.dev_attr.attr,
  525. &sensor_dev_attr_temp6_alarm.dev_attr.attr,
  526. NULL
  527. }
  528. };
  529. static const struct attribute_group vt8231_group_temps[6] = {
  530. { .attrs = vt8231_attributes_temps[0] },
  531. { .attrs = vt8231_attributes_temps[1] },
  532. { .attrs = vt8231_attributes_temps[2] },
  533. { .attrs = vt8231_attributes_temps[3] },
  534. { .attrs = vt8231_attributes_temps[4] },
  535. { .attrs = vt8231_attributes_temps[5] },
  536. };
  537. static struct attribute *vt8231_attributes_volts[6][5] = {
  538. {
  539. &sensor_dev_attr_in0_input.dev_attr.attr,
  540. &sensor_dev_attr_in0_min.dev_attr.attr,
  541. &sensor_dev_attr_in0_max.dev_attr.attr,
  542. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  543. NULL
  544. }, {
  545. &sensor_dev_attr_in1_input.dev_attr.attr,
  546. &sensor_dev_attr_in1_min.dev_attr.attr,
  547. &sensor_dev_attr_in1_max.dev_attr.attr,
  548. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  549. NULL
  550. }, {
  551. &sensor_dev_attr_in2_input.dev_attr.attr,
  552. &sensor_dev_attr_in2_min.dev_attr.attr,
  553. &sensor_dev_attr_in2_max.dev_attr.attr,
  554. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  555. NULL
  556. }, {
  557. &sensor_dev_attr_in3_input.dev_attr.attr,
  558. &sensor_dev_attr_in3_min.dev_attr.attr,
  559. &sensor_dev_attr_in3_max.dev_attr.attr,
  560. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  561. NULL
  562. }, {
  563. &sensor_dev_attr_in4_input.dev_attr.attr,
  564. &sensor_dev_attr_in4_min.dev_attr.attr,
  565. &sensor_dev_attr_in4_max.dev_attr.attr,
  566. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  567. NULL
  568. }, {
  569. &dev_attr_in5_input.attr,
  570. &dev_attr_in5_min.attr,
  571. &dev_attr_in5_max.attr,
  572. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  573. NULL
  574. }
  575. };
  576. static const struct attribute_group vt8231_group_volts[6] = {
  577. { .attrs = vt8231_attributes_volts[0] },
  578. { .attrs = vt8231_attributes_volts[1] },
  579. { .attrs = vt8231_attributes_volts[2] },
  580. { .attrs = vt8231_attributes_volts[3] },
  581. { .attrs = vt8231_attributes_volts[4] },
  582. { .attrs = vt8231_attributes_volts[5] },
  583. };
  584. static struct attribute *vt8231_attributes[] = {
  585. &sensor_dev_attr_fan1_input.dev_attr.attr,
  586. &sensor_dev_attr_fan2_input.dev_attr.attr,
  587. &sensor_dev_attr_fan1_min.dev_attr.attr,
  588. &sensor_dev_attr_fan2_min.dev_attr.attr,
  589. &sensor_dev_attr_fan1_div.dev_attr.attr,
  590. &sensor_dev_attr_fan2_div.dev_attr.attr,
  591. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  592. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  593. &dev_attr_alarms.attr,
  594. &dev_attr_name.attr,
  595. NULL
  596. };
  597. static const struct attribute_group vt8231_group = {
  598. .attrs = vt8231_attributes,
  599. };
  600. static struct platform_driver vt8231_driver = {
  601. .driver = {
  602. .owner = THIS_MODULE,
  603. .name = "vt8231",
  604. },
  605. .probe = vt8231_probe,
  606. .remove = __devexit_p(vt8231_remove),
  607. };
  608. static const struct pci_device_id vt8231_pci_ids[] = {
  609. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
  610. { 0, }
  611. };
  612. MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
  613. static int __devinit vt8231_pci_probe(struct pci_dev *dev,
  614. const struct pci_device_id *id);
  615. static struct pci_driver vt8231_pci_driver = {
  616. .name = "vt8231",
  617. .id_table = vt8231_pci_ids,
  618. .probe = vt8231_pci_probe,
  619. };
  620. static int vt8231_probe(struct platform_device *pdev)
  621. {
  622. struct resource *res;
  623. struct vt8231_data *data;
  624. int err = 0, i;
  625. /* Reserve the ISA region */
  626. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  627. if (!request_region(res->start, VT8231_EXTENT,
  628. vt8231_driver.driver.name)) {
  629. dev_err(&pdev->dev, "Region 0x%lx-0x%lx already in use!\n",
  630. (unsigned long)res->start, (unsigned long)res->end);
  631. return -ENODEV;
  632. }
  633. if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
  634. err = -ENOMEM;
  635. goto exit_release;
  636. }
  637. platform_set_drvdata(pdev, data);
  638. data->addr = res->start;
  639. data->name = "vt8231";
  640. mutex_init(&data->update_lock);
  641. vt8231_init_device(data);
  642. /* Register sysfs hooks */
  643. if ((err = sysfs_create_group(&pdev->dev.kobj, &vt8231_group)))
  644. goto exit_free;
  645. /* Must update device information to find out the config field */
  646. data->uch_config = vt8231_read_value(data, VT8231_REG_UCH_CONFIG);
  647. for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++) {
  648. if (ISTEMP(i, data->uch_config)) {
  649. if ((err = sysfs_create_group(&pdev->dev.kobj,
  650. &vt8231_group_temps[i])))
  651. goto exit_remove_files;
  652. }
  653. }
  654. for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++) {
  655. if (ISVOLT(i, data->uch_config)) {
  656. if ((err = sysfs_create_group(&pdev->dev.kobj,
  657. &vt8231_group_volts[i])))
  658. goto exit_remove_files;
  659. }
  660. }
  661. data->hwmon_dev = hwmon_device_register(&pdev->dev);
  662. if (IS_ERR(data->hwmon_dev)) {
  663. err = PTR_ERR(data->hwmon_dev);
  664. goto exit_remove_files;
  665. }
  666. return 0;
  667. exit_remove_files:
  668. for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
  669. sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
  670. for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
  671. sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
  672. sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
  673. exit_free:
  674. platform_set_drvdata(pdev, NULL);
  675. kfree(data);
  676. exit_release:
  677. release_region(res->start, VT8231_EXTENT);
  678. return err;
  679. }
  680. static int __devexit vt8231_remove(struct platform_device *pdev)
  681. {
  682. struct vt8231_data *data = platform_get_drvdata(pdev);
  683. int i;
  684. hwmon_device_unregister(data->hwmon_dev);
  685. for (i = 0; i < ARRAY_SIZE(vt8231_group_volts); i++)
  686. sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_volts[i]);
  687. for (i = 0; i < ARRAY_SIZE(vt8231_group_temps); i++)
  688. sysfs_remove_group(&pdev->dev.kobj, &vt8231_group_temps[i]);
  689. sysfs_remove_group(&pdev->dev.kobj, &vt8231_group);
  690. release_region(data->addr, VT8231_EXTENT);
  691. platform_set_drvdata(pdev, NULL);
  692. kfree(data);
  693. return 0;
  694. }
  695. static void vt8231_init_device(struct vt8231_data *data)
  696. {
  697. vt8231_write_value(data, VT8231_REG_TEMP1_CONFIG, 0);
  698. vt8231_write_value(data, VT8231_REG_TEMP2_CONFIG, 0);
  699. }
  700. static struct vt8231_data *vt8231_update_device(struct device *dev)
  701. {
  702. struct vt8231_data *data = dev_get_drvdata(dev);
  703. int i;
  704. u16 low;
  705. mutex_lock(&data->update_lock);
  706. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  707. || !data->valid) {
  708. for (i = 0; i < 6; i++) {
  709. if (ISVOLT(i, data->uch_config)) {
  710. data->in[i] = vt8231_read_value(data,
  711. regvolt[i]);
  712. data->in_min[i] = vt8231_read_value(data,
  713. regvoltmin[i]);
  714. data->in_max[i] = vt8231_read_value(data,
  715. regvoltmax[i]);
  716. }
  717. }
  718. for (i = 0; i < 2; i++) {
  719. data->fan[i] = vt8231_read_value(data,
  720. VT8231_REG_FAN(i));
  721. data->fan_min[i] = vt8231_read_value(data,
  722. VT8231_REG_FAN_MIN(i));
  723. }
  724. low = vt8231_read_value(data, VT8231_REG_TEMP_LOW01);
  725. low = (low >> 6) | ((low & 0x30) >> 2)
  726. | (vt8231_read_value(data, VT8231_REG_TEMP_LOW25) << 4);
  727. for (i = 0; i < 6; i++) {
  728. if (ISTEMP(i, data->uch_config)) {
  729. data->temp[i] = (vt8231_read_value(data,
  730. regtemp[i]) << 2)
  731. | ((low >> (2 * i)) & 0x03);
  732. data->temp_max[i] = vt8231_read_value(data,
  733. regtempmax[i]);
  734. data->temp_min[i] = vt8231_read_value(data,
  735. regtempmin[i]);
  736. }
  737. }
  738. i = vt8231_read_value(data, VT8231_REG_FANDIV);
  739. data->fan_div[0] = (i >> 4) & 0x03;
  740. data->fan_div[1] = i >> 6;
  741. data->alarms = vt8231_read_value(data, VT8231_REG_ALARM1) |
  742. (vt8231_read_value(data, VT8231_REG_ALARM2) << 8);
  743. /* Set alarm flags correctly */
  744. if (!data->fan[0] && data->fan_min[0]) {
  745. data->alarms |= 0x40;
  746. } else if (data->fan[0] && !data->fan_min[0]) {
  747. data->alarms &= ~0x40;
  748. }
  749. if (!data->fan[1] && data->fan_min[1]) {
  750. data->alarms |= 0x80;
  751. } else if (data->fan[1] && !data->fan_min[1]) {
  752. data->alarms &= ~0x80;
  753. }
  754. data->last_updated = jiffies;
  755. data->valid = 1;
  756. }
  757. mutex_unlock(&data->update_lock);
  758. return data;
  759. }
  760. static int __devinit vt8231_device_add(unsigned short address)
  761. {
  762. struct resource res = {
  763. .start = address,
  764. .end = address + VT8231_EXTENT - 1,
  765. .name = "vt8231",
  766. .flags = IORESOURCE_IO,
  767. };
  768. int err;
  769. err = acpi_check_resource_conflict(&res);
  770. if (err)
  771. goto exit;
  772. pdev = platform_device_alloc("vt8231", address);
  773. if (!pdev) {
  774. err = -ENOMEM;
  775. pr_err("Device allocation failed\n");
  776. goto exit;
  777. }
  778. err = platform_device_add_resources(pdev, &res, 1);
  779. if (err) {
  780. pr_err("Device resource addition failed (%d)\n", err);
  781. goto exit_device_put;
  782. }
  783. err = platform_device_add(pdev);
  784. if (err) {
  785. pr_err("Device addition failed (%d)\n", err);
  786. goto exit_device_put;
  787. }
  788. return 0;
  789. exit_device_put:
  790. platform_device_put(pdev);
  791. exit:
  792. return err;
  793. }
  794. static int __devinit vt8231_pci_probe(struct pci_dev *dev,
  795. const struct pci_device_id *id)
  796. {
  797. u16 address, val;
  798. if (force_addr) {
  799. address = force_addr & 0xff00;
  800. dev_warn(&dev->dev, "Forcing ISA address 0x%x\n",
  801. address);
  802. if (PCIBIOS_SUCCESSFUL !=
  803. pci_write_config_word(dev, VT8231_BASE_REG, address | 1))
  804. return -ENODEV;
  805. }
  806. if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
  807. &val))
  808. return -ENODEV;
  809. address = val & ~(VT8231_EXTENT - 1);
  810. if (address == 0) {
  811. dev_err(&dev->dev, "base address not set - upgrade BIOS or use force_addr=0xaddr\n");
  812. return -ENODEV;
  813. }
  814. if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_ENABLE_REG,
  815. &val))
  816. return -ENODEV;
  817. if (!(val & 0x0001)) {
  818. dev_warn(&dev->dev, "enabling sensors\n");
  819. if (PCIBIOS_SUCCESSFUL !=
  820. pci_write_config_word(dev, VT8231_ENABLE_REG,
  821. val | 0x0001))
  822. return -ENODEV;
  823. }
  824. if (platform_driver_register(&vt8231_driver))
  825. goto exit;
  826. /* Sets global pdev as a side effect */
  827. if (vt8231_device_add(address))
  828. goto exit_unregister;
  829. /* Always return failure here. This is to allow other drivers to bind
  830. * to this pci device. We don't really want to have control over the
  831. * pci device, we only wanted to read as few register values from it.
  832. */
  833. /* We do, however, mark ourselves as using the PCI device to stop it
  834. getting unloaded. */
  835. s_bridge = pci_dev_get(dev);
  836. return -ENODEV;
  837. exit_unregister:
  838. platform_driver_unregister(&vt8231_driver);
  839. exit:
  840. return -ENODEV;
  841. }
  842. static int __init sm_vt8231_init(void)
  843. {
  844. return pci_register_driver(&vt8231_pci_driver);
  845. }
  846. static void __exit sm_vt8231_exit(void)
  847. {
  848. pci_unregister_driver(&vt8231_pci_driver);
  849. if (s_bridge != NULL) {
  850. platform_device_unregister(pdev);
  851. platform_driver_unregister(&vt8231_driver);
  852. pci_dev_put(s_bridge);
  853. s_bridge = NULL;
  854. }
  855. }
  856. MODULE_AUTHOR("Roger Lucas <vt8231@hiddenengine.co.uk>");
  857. MODULE_DESCRIPTION("VT8231 sensors");
  858. MODULE_LICENSE("GPL");
  859. module_init(sm_vt8231_init);
  860. module_exit(sm_vt8231_exit);