vt8231.c 31 KB

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