vt8231.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. vt8231.c - Part of lm_sensors, Linux kernel modules
  3. for hardware monitoring
  4. Copyright (c) 2005 Roger Lucas <roger@planbit.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. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/pci.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c-isa.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 <asm/io.h>
  33. static int force_addr;
  34. module_param(force_addr, int, 0);
  35. MODULE_PARM_DESC(force_addr, "Initialize the base address of the sensors");
  36. /* Device address
  37. Note that we can't determine the ISA address until we have initialized
  38. our module */
  39. static unsigned short isa_address;
  40. #define VT8231_EXTENT 0x80
  41. #define VT8231_BASE_REG 0x70
  42. #define VT8231_ENABLE_REG 0x74
  43. /* The VT8231 registers
  44. The reset value for the input channel configuration is used (Reg 0x4A=0x07)
  45. which sets the selected inputs marked with '*' below if multiple options are
  46. possible:
  47. Voltage Mode Temperature Mode
  48. Sensor Linux Id Linux Id VIA Id
  49. -------- -------- -------- ------
  50. CPU Diode N/A temp1 0
  51. UIC1 in0 temp2 * 1
  52. UIC2 in1 * temp3 2
  53. UIC3 in2 * temp4 3
  54. UIC4 in3 * temp5 4
  55. UIC5 in4 * temp6 5
  56. 3.3V in5 N/A
  57. Note that the BIOS may set the configuration register to a different value
  58. to match the motherboard configuration.
  59. */
  60. /* fans numbered 0-1 */
  61. #define VT8231_REG_FAN_MIN(nr) (0x3b + (nr))
  62. #define VT8231_REG_FAN(nr) (0x29 + (nr))
  63. /* Voltage inputs numbered 0-5 */
  64. static const u8 regvolt[] = { 0x21, 0x22, 0x23, 0x24, 0x25, 0x26 };
  65. static const u8 regvoltmax[] = { 0x3d, 0x2b, 0x2d, 0x2f, 0x31, 0x33 };
  66. static const u8 regvoltmin[] = { 0x3e, 0x2c, 0x2e, 0x30, 0x32, 0x34 };
  67. /* Temperatures are numbered 1-6 according to the Linux kernel specification.
  68. **
  69. ** In the VIA datasheet, however, the temperatures are numbered from zero.
  70. ** Since it is important that this driver can easily be compared to the VIA
  71. ** datasheet, we will use the VIA numbering within this driver and map the
  72. ** kernel sysfs device name to the VIA number in the sysfs callback.
  73. */
  74. #define VT8231_REG_TEMP_LOW01 0x49
  75. #define VT8231_REG_TEMP_LOW25 0x4d
  76. static const u8 regtemp[] = { 0x1f, 0x21, 0x22, 0x23, 0x24, 0x25 };
  77. static const u8 regtempmax[] = { 0x39, 0x3d, 0x2b, 0x2d, 0x2f, 0x31 };
  78. static const u8 regtempmin[] = { 0x3a, 0x3e, 0x2c, 0x2e, 0x30, 0x32 };
  79. #define TEMP_FROM_REG(reg) (((253 * 4 - (reg)) * 550 + 105) / 210)
  80. #define TEMP_MAXMIN_FROM_REG(reg) (((253 - (reg)) * 2200 + 105) / 210)
  81. #define TEMP_MAXMIN_TO_REG(val) (253 - ((val) * 210 + 1100) / 2200)
  82. #define VT8231_REG_CONFIG 0x40
  83. #define VT8231_REG_ALARM1 0x41
  84. #define VT8231_REG_ALARM2 0x42
  85. #define VT8231_REG_FANDIV 0x47
  86. #define VT8231_REG_UCH_CONFIG 0x4a
  87. #define VT8231_REG_TEMP1_CONFIG 0x4b
  88. #define VT8231_REG_TEMP2_CONFIG 0x4c
  89. /* temps 0-5 as numbered in VIA datasheet - see later for mapping to Linux
  90. ** numbering
  91. */
  92. #define ISTEMP(i, ch_config) ((i) == 0 ? 1 : \
  93. ((ch_config) >> ((i)+1)) & 0x01)
  94. /* voltages 0-5 */
  95. #define ISVOLT(i, ch_config) ((i) == 5 ? 1 : \
  96. !(((ch_config) >> ((i)+2)) & 0x01))
  97. #define DIV_FROM_REG(val) (1 << (val))
  98. /* NB The values returned here are NOT temperatures. The calibration curves
  99. ** for the thermistor curves are board-specific and must go in the
  100. ** sensors.conf file. Temperature sensors are actually ten bits, but the
  101. ** VIA datasheet only considers the 8 MSBs obtained from the regtemp[]
  102. ** register. The temperature value returned should have a magnitude of 3,
  103. ** so we use the VIA scaling as the "true" scaling and use the remaining 2
  104. ** LSBs as fractional precision.
  105. **
  106. ** All the on-chip hardware temperature comparisons for the alarms are only
  107. ** 8-bits wide, and compare against the 8 MSBs of the temperature. The bits
  108. ** in the registers VT8231_REG_TEMP_LOW01 and VT8231_REG_TEMP_LOW25 are
  109. ** ignored.
  110. */
  111. /******** FAN RPM CONVERSIONS ********
  112. ** This chip saturates back at 0, not at 255 like many the other chips.
  113. ** So, 0 means 0 RPM
  114. */
  115. static inline u8 FAN_TO_REG(long rpm, int div)
  116. {
  117. if (rpm == 0)
  118. return 0;
  119. return SENSORS_LIMIT(1310720 / (rpm * div), 1, 255);
  120. }
  121. #define FAN_FROM_REG(val, div) ((val) == 0 ? 0 : 1310720 / ((val) * (div)))
  122. struct vt8231_data {
  123. struct i2c_client client;
  124. struct semaphore update_lock;
  125. struct class_device *class_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_detect(struct i2c_adapter *adapter);
  142. static int vt8231_detach_client(struct i2c_client *client);
  143. static struct vt8231_data *vt8231_update_device(struct device *dev);
  144. static void vt8231_init_client(struct i2c_client *client);
  145. static inline int vt8231_read_value(struct i2c_client *client, u8 reg)
  146. {
  147. return inb_p(client->addr + reg);
  148. }
  149. static inline void vt8231_write_value(struct i2c_client *client, u8 reg,
  150. u8 value)
  151. {
  152. outb_p(value, client->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 i2c_client *client = to_i2c_client(dev);
  185. struct vt8231_data *data = i2c_get_clientdata(client);
  186. unsigned long val = simple_strtoul(buf, NULL, 10);
  187. down(&data->update_lock);
  188. data->in_min[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
  189. vt8231_write_value(client, regvoltmin[nr], data->in_min[nr]);
  190. up(&data->update_lock);
  191. return count;
  192. }
  193. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  194. const char *buf, size_t count)
  195. {
  196. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  197. int nr = sensor_attr->index;
  198. struct i2c_client *client = to_i2c_client(dev);
  199. struct vt8231_data *data = i2c_get_clientdata(client);
  200. unsigned long val = simple_strtoul(buf, NULL, 10);
  201. down(&data->update_lock);
  202. data->in_max[nr] = SENSORS_LIMIT(((val * 958) / 10000) + 3, 0, 255);
  203. vt8231_write_value(client, regvoltmax[nr], data->in_max[nr]);
  204. up(&data->update_lock);
  205. return count;
  206. }
  207. /* Special case for input 5 as this has 3.3V scaling built into the chip */
  208. static ssize_t show_in5(struct device *dev, struct device_attribute *attr,
  209. char *buf)
  210. {
  211. struct vt8231_data *data = vt8231_update_device(dev);
  212. return sprintf(buf, "%d\n",
  213. (((data->in[5] - 3) * 10000 * 54) / (958 * 34)));
  214. }
  215. static ssize_t show_in5_min(struct device *dev, struct device_attribute *attr,
  216. char *buf)
  217. {
  218. struct vt8231_data *data = vt8231_update_device(dev);
  219. return sprintf(buf, "%d\n",
  220. (((data->in_min[5] - 3) * 10000 * 54) / (958 * 34)));
  221. }
  222. static ssize_t show_in5_max(struct device *dev, struct device_attribute *attr,
  223. char *buf)
  224. {
  225. struct vt8231_data *data = vt8231_update_device(dev);
  226. return sprintf(buf, "%d\n",
  227. (((data->in_max[5] - 3) * 10000 * 54) / (958 * 34)));
  228. }
  229. static ssize_t set_in5_min(struct device *dev, struct device_attribute *attr,
  230. const char *buf, size_t count)
  231. {
  232. struct i2c_client *client = to_i2c_client(dev);
  233. struct vt8231_data *data = i2c_get_clientdata(client);
  234. unsigned long val = simple_strtoul(buf, NULL, 10);
  235. down(&data->update_lock);
  236. data->in_min[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
  237. 0, 255);
  238. vt8231_write_value(client, regvoltmin[5], data->in_min[5]);
  239. up(&data->update_lock);
  240. return count;
  241. }
  242. static ssize_t set_in5_max(struct device *dev, struct device_attribute *attr,
  243. const char *buf, size_t count)
  244. {
  245. struct i2c_client *client = to_i2c_client(dev);
  246. struct vt8231_data *data = i2c_get_clientdata(client);
  247. unsigned long val = simple_strtoul(buf, NULL, 10);
  248. down(&data->update_lock);
  249. data->in_max[5] = SENSORS_LIMIT(((val * 958 * 34) / (10000 * 54)) + 3,
  250. 0, 255);
  251. vt8231_write_value(client, regvoltmax[5], data->in_max[5]);
  252. up(&data->update_lock);
  253. return count;
  254. }
  255. #define define_voltage_sysfs(offset) \
  256. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  257. show_in, NULL, offset); \
  258. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  259. show_in_min, set_in_min, offset); \
  260. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  261. show_in_max, set_in_max, offset)
  262. define_voltage_sysfs(0);
  263. define_voltage_sysfs(1);
  264. define_voltage_sysfs(2);
  265. define_voltage_sysfs(3);
  266. define_voltage_sysfs(4);
  267. static DEVICE_ATTR(in5_input, S_IRUGO, show_in5, NULL);
  268. static DEVICE_ATTR(in5_min, S_IRUGO | S_IWUSR, show_in5_min, set_in5_min);
  269. static DEVICE_ATTR(in5_max, S_IRUGO | S_IWUSR, show_in5_max, set_in5_max);
  270. /* Temperatures */
  271. static ssize_t show_temp0(struct device *dev, struct device_attribute *attr,
  272. char *buf)
  273. {
  274. struct vt8231_data *data = vt8231_update_device(dev);
  275. return sprintf(buf, "%d\n", data->temp[0] * 250);
  276. }
  277. static ssize_t show_temp0_max(struct device *dev, struct device_attribute *attr,
  278. char *buf)
  279. {
  280. struct vt8231_data *data = vt8231_update_device(dev);
  281. return sprintf(buf, "%d\n", data->temp_max[0] * 1000);
  282. }
  283. static ssize_t show_temp0_min(struct device *dev, struct device_attribute *attr,
  284. char *buf)
  285. {
  286. struct vt8231_data *data = vt8231_update_device(dev);
  287. return sprintf(buf, "%d\n", data->temp_min[0] * 1000);
  288. }
  289. static ssize_t set_temp0_max(struct device *dev, struct device_attribute *attr,
  290. const char *buf, size_t count)
  291. {
  292. struct i2c_client *client = to_i2c_client(dev);
  293. struct vt8231_data *data = i2c_get_clientdata(client);
  294. int val = simple_strtol(buf, NULL, 10);
  295. down(&data->update_lock);
  296. data->temp_max[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
  297. vt8231_write_value(client, regtempmax[0], data->temp_max[0]);
  298. up(&data->update_lock);
  299. return count;
  300. }
  301. static ssize_t set_temp0_min(struct device *dev, struct device_attribute *attr,
  302. const char *buf, size_t count)
  303. {
  304. struct i2c_client *client = to_i2c_client(dev);
  305. struct vt8231_data *data = i2c_get_clientdata(client);
  306. int val = simple_strtol(buf, NULL, 10);
  307. down(&data->update_lock);
  308. data->temp_min[0] = SENSORS_LIMIT((val + 500) / 1000, 0, 255);
  309. vt8231_write_value(client, regtempmin[0], data->temp_min[0]);
  310. up(&data->update_lock);
  311. return count;
  312. }
  313. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  314. char *buf)
  315. {
  316. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  317. int nr = sensor_attr->index;
  318. struct vt8231_data *data = vt8231_update_device(dev);
  319. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
  320. }
  321. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  322. char *buf)
  323. {
  324. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  325. int nr = sensor_attr->index;
  326. struct vt8231_data *data = vt8231_update_device(dev);
  327. return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_max[nr]));
  328. }
  329. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  330. char *buf)
  331. {
  332. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  333. int nr = sensor_attr->index;
  334. struct vt8231_data *data = vt8231_update_device(dev);
  335. return sprintf(buf, "%d\n", TEMP_MAXMIN_FROM_REG(data->temp_min[nr]));
  336. }
  337. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  338. const char *buf, size_t count)
  339. {
  340. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  341. int nr = sensor_attr->index;
  342. struct i2c_client *client = to_i2c_client(dev);
  343. struct vt8231_data *data = i2c_get_clientdata(client);
  344. int val = simple_strtol(buf, NULL, 10);
  345. down(&data->update_lock);
  346. data->temp_max[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
  347. vt8231_write_value(client, regtempmax[nr], data->temp_max[nr]);
  348. up(&data->update_lock);
  349. return count;
  350. }
  351. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  352. const char *buf, size_t count)
  353. {
  354. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  355. int nr = sensor_attr->index;
  356. struct i2c_client *client = to_i2c_client(dev);
  357. struct vt8231_data *data = i2c_get_clientdata(client);
  358. int val = simple_strtol(buf, NULL, 10);
  359. down(&data->update_lock);
  360. data->temp_min[nr] = SENSORS_LIMIT(TEMP_MAXMIN_TO_REG(val), 0, 255);
  361. vt8231_write_value(client, regtempmin[nr], data->temp_min[nr]);
  362. up(&data->update_lock);
  363. return count;
  364. }
  365. /* Note that these map the Linux temperature sensor numbering (1-6) to the VIA
  366. ** temperature sensor numbering (0-5)
  367. */
  368. #define define_temperature_sysfs(offset) \
  369. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  370. show_temp, NULL, offset - 1); \
  371. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  372. show_temp_max, set_temp_max, offset - 1); \
  373. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  374. show_temp_min, set_temp_min, offset - 1)
  375. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp0, NULL);
  376. static DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp0_max, set_temp0_max);
  377. static DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp0_min, set_temp0_min);
  378. define_temperature_sysfs(2);
  379. define_temperature_sysfs(3);
  380. define_temperature_sysfs(4);
  381. define_temperature_sysfs(5);
  382. define_temperature_sysfs(6);
  383. #define CFG_INFO_TEMP(id) { &sensor_dev_attr_temp##id##_input.dev_attr, \
  384. &sensor_dev_attr_temp##id##_min.dev_attr, \
  385. &sensor_dev_attr_temp##id##_max.dev_attr }
  386. #define CFG_INFO_VOLT(id) { &sensor_dev_attr_in##id##_input.dev_attr, \
  387. &sensor_dev_attr_in##id##_min.dev_attr, \
  388. &sensor_dev_attr_in##id##_max.dev_attr }
  389. struct str_device_attr_table {
  390. struct device_attribute *input;
  391. struct device_attribute *min;
  392. struct device_attribute *max;
  393. };
  394. static struct str_device_attr_table cfg_info_temp[] = {
  395. { &dev_attr_temp1_input, &dev_attr_temp1_min, &dev_attr_temp1_max },
  396. CFG_INFO_TEMP(2),
  397. CFG_INFO_TEMP(3),
  398. CFG_INFO_TEMP(4),
  399. CFG_INFO_TEMP(5),
  400. CFG_INFO_TEMP(6)
  401. };
  402. static struct str_device_attr_table cfg_info_volt[] = {
  403. CFG_INFO_VOLT(0),
  404. CFG_INFO_VOLT(1),
  405. CFG_INFO_VOLT(2),
  406. CFG_INFO_VOLT(3),
  407. CFG_INFO_VOLT(4),
  408. { &dev_attr_in5_input, &dev_attr_in5_min, &dev_attr_in5_max }
  409. };
  410. /* Fans */
  411. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  412. char *buf)
  413. {
  414. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  415. int nr = sensor_attr->index;
  416. struct vt8231_data *data = vt8231_update_device(dev);
  417. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  418. DIV_FROM_REG(data->fan_div[nr])));
  419. }
  420. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  421. char *buf)
  422. {
  423. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  424. int nr = sensor_attr->index;
  425. struct vt8231_data *data = vt8231_update_device(dev);
  426. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  427. DIV_FROM_REG(data->fan_div[nr])));
  428. }
  429. static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
  430. char *buf)
  431. {
  432. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  433. int nr = sensor_attr->index;
  434. struct vt8231_data *data = vt8231_update_device(dev);
  435. return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
  436. }
  437. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  438. const char *buf, size_t count)
  439. {
  440. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  441. int nr = sensor_attr->index;
  442. struct i2c_client *client = to_i2c_client(dev);
  443. struct vt8231_data *data = i2c_get_clientdata(client);
  444. int val = simple_strtoul(buf, NULL, 10);
  445. down(&data->update_lock);
  446. data->fan_min[nr] = FAN_TO_REG(val, DIV_FROM_REG(data->fan_div[nr]));
  447. vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
  448. up(&data->update_lock);
  449. return count;
  450. }
  451. static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
  452. const char *buf, size_t count)
  453. {
  454. struct i2c_client *client = to_i2c_client(dev);
  455. struct vt8231_data *data = i2c_get_clientdata(client);
  456. struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
  457. unsigned long val = simple_strtoul(buf, NULL, 10);
  458. int nr = sensor_attr->index;
  459. int old = vt8231_read_value(client, VT8231_REG_FANDIV);
  460. long min = FAN_FROM_REG(data->fan_min[nr],
  461. DIV_FROM_REG(data->fan_div[nr]));
  462. down(&data->update_lock);
  463. switch (val) {
  464. case 1: data->fan_div[nr] = 0; break;
  465. case 2: data->fan_div[nr] = 1; break;
  466. case 4: data->fan_div[nr] = 2; break;
  467. case 8: data->fan_div[nr] = 3; break;
  468. default:
  469. dev_err(&client->dev, "fan_div value %ld not supported."
  470. "Choose one of 1, 2, 4 or 8!\n", val);
  471. up(&data->update_lock);
  472. return -EINVAL;
  473. }
  474. /* Correct the fan minimum speed */
  475. data->fan_min[nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
  476. vt8231_write_value(client, VT8231_REG_FAN_MIN(nr), data->fan_min[nr]);
  477. old = (old & 0x0f) | (data->fan_div[1] << 6) | (data->fan_div[0] << 4);
  478. vt8231_write_value(client, VT8231_REG_FANDIV, old);
  479. up(&data->update_lock);
  480. return count;
  481. }
  482. #define define_fan_sysfs(offset) \
  483. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  484. show_fan, NULL, offset - 1); \
  485. static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  486. show_fan_div, set_fan_div, offset - 1); \
  487. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  488. show_fan_min, set_fan_min, offset - 1)
  489. define_fan_sysfs(1);
  490. define_fan_sysfs(2);
  491. /* Alarms */
  492. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr,
  493. char *buf)
  494. {
  495. struct vt8231_data *data = vt8231_update_device(dev);
  496. return sprintf(buf, "%d\n", data->alarms);
  497. }
  498. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  499. static struct i2c_driver vt8231_driver = {
  500. .driver = {
  501. .name = "vt8231",
  502. },
  503. .attach_adapter = vt8231_detect,
  504. .detach_client = vt8231_detach_client,
  505. };
  506. static struct pci_device_id vt8231_pci_ids[] = {
  507. { PCI_DEVICE(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8231_4) },
  508. { 0, }
  509. };
  510. MODULE_DEVICE_TABLE(pci, vt8231_pci_ids);
  511. static int __devinit vt8231_pci_probe(struct pci_dev *dev,
  512. const struct pci_device_id *id);
  513. static struct pci_driver vt8231_pci_driver = {
  514. .name = "vt8231",
  515. .id_table = vt8231_pci_ids,
  516. .probe = vt8231_pci_probe,
  517. };
  518. int vt8231_detect(struct i2c_adapter *adapter)
  519. {
  520. struct i2c_client *client;
  521. struct vt8231_data *data;
  522. int err = 0, i;
  523. u16 val;
  524. /* 8231 requires multiple of 256 */
  525. if (force_addr) {
  526. isa_address = force_addr & 0xFF00;
  527. dev_warn(&adapter->dev, "forcing ISA address 0x%04X\n",
  528. isa_address);
  529. if (PCIBIOS_SUCCESSFUL != pci_write_config_word(s_bridge,
  530. VT8231_BASE_REG, isa_address))
  531. return -ENODEV;
  532. }
  533. if (PCIBIOS_SUCCESSFUL !=
  534. pci_read_config_word(s_bridge, VT8231_ENABLE_REG, &val))
  535. return -ENODEV;
  536. if (!(val & 0x0001)) {
  537. dev_warn(&adapter->dev, "enabling sensors\n");
  538. if (PCIBIOS_SUCCESSFUL !=
  539. pci_write_config_word(s_bridge, VT8231_ENABLE_REG,
  540. val | 0x0001))
  541. return -ENODEV;
  542. }
  543. /* Reserve the ISA region */
  544. if (!request_region(isa_address, VT8231_EXTENT,
  545. vt8231_pci_driver.name)) {
  546. dev_err(&adapter->dev, "region 0x%x already in use!\n",
  547. isa_address);
  548. return -ENODEV;
  549. }
  550. if (!(data = kzalloc(sizeof(struct vt8231_data), GFP_KERNEL))) {
  551. err = -ENOMEM;
  552. goto exit_release;
  553. }
  554. client = &data->client;
  555. i2c_set_clientdata(client, data);
  556. client->addr = isa_address;
  557. client->adapter = adapter;
  558. client->driver = &vt8231_driver;
  559. client->dev.parent = &adapter->dev;
  560. /* Fill in the remaining client fields and put into the global list */
  561. strlcpy(client->name, "vt8231", I2C_NAME_SIZE);
  562. init_MUTEX(&data->update_lock);
  563. /* Tell the I2C layer a new client has arrived */
  564. if ((err = i2c_attach_client(client)))
  565. goto exit_free;
  566. vt8231_init_client(client);
  567. /* Register sysfs hooks */
  568. data->class_dev = hwmon_device_register(&client->dev);
  569. if (IS_ERR(data->class_dev)) {
  570. err = PTR_ERR(data->class_dev);
  571. goto exit_detach;
  572. }
  573. /* Must update device information to find out the config field */
  574. data->uch_config = vt8231_read_value(client, VT8231_REG_UCH_CONFIG);
  575. for (i = 0; i < ARRAY_SIZE(cfg_info_temp); i++) {
  576. if (ISTEMP(i, data->uch_config)) {
  577. device_create_file(&client->dev,
  578. cfg_info_temp[i].input);
  579. device_create_file(&client->dev, cfg_info_temp[i].max);
  580. device_create_file(&client->dev, cfg_info_temp[i].min);
  581. }
  582. }
  583. for (i = 0; i < ARRAY_SIZE(cfg_info_volt); i++) {
  584. if (ISVOLT(i, data->uch_config)) {
  585. device_create_file(&client->dev,
  586. cfg_info_volt[i].input);
  587. device_create_file(&client->dev, cfg_info_volt[i].max);
  588. device_create_file(&client->dev, cfg_info_volt[i].min);
  589. }
  590. }
  591. device_create_file(&client->dev, &sensor_dev_attr_fan1_input.dev_attr);
  592. device_create_file(&client->dev, &sensor_dev_attr_fan2_input.dev_attr);
  593. device_create_file(&client->dev, &sensor_dev_attr_fan1_min.dev_attr);
  594. device_create_file(&client->dev, &sensor_dev_attr_fan2_min.dev_attr);
  595. device_create_file(&client->dev, &sensor_dev_attr_fan1_div.dev_attr);
  596. device_create_file(&client->dev, &sensor_dev_attr_fan2_div.dev_attr);
  597. device_create_file(&client->dev, &dev_attr_alarms);
  598. return 0;
  599. exit_detach:
  600. i2c_detach_client(client);
  601. exit_free:
  602. kfree(data);
  603. exit_release:
  604. release_region(isa_address, VT8231_EXTENT);
  605. return err;
  606. }
  607. static int vt8231_detach_client(struct i2c_client *client)
  608. {
  609. struct vt8231_data *data = i2c_get_clientdata(client);
  610. int err;
  611. hwmon_device_unregister(data->class_dev);
  612. if ((err = i2c_detach_client(client))) {
  613. return err;
  614. }
  615. release_region(client->addr, VT8231_EXTENT);
  616. kfree(data);
  617. return 0;
  618. }
  619. static void vt8231_init_client(struct i2c_client *client)
  620. {
  621. vt8231_write_value(client, VT8231_REG_TEMP1_CONFIG, 0);
  622. vt8231_write_value(client, VT8231_REG_TEMP2_CONFIG, 0);
  623. }
  624. static struct vt8231_data *vt8231_update_device(struct device *dev)
  625. {
  626. struct i2c_client *client = to_i2c_client(dev);
  627. struct vt8231_data *data = i2c_get_clientdata(client);
  628. int i;
  629. u16 low;
  630. down(&data->update_lock);
  631. if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
  632. || !data->valid) {
  633. for (i = 0; i < 6; i++) {
  634. if (ISVOLT(i, data->uch_config)) {
  635. data->in[i] = vt8231_read_value(client,
  636. regvolt[i]);
  637. data->in_min[i] = vt8231_read_value(client,
  638. regvoltmin[i]);
  639. data->in_max[i] = vt8231_read_value(client,
  640. regvoltmax[i]);
  641. }
  642. }
  643. for (i = 0; i < 2; i++) {
  644. data->fan[i] = vt8231_read_value(client,
  645. VT8231_REG_FAN(i));
  646. data->fan_min[i] = vt8231_read_value(client,
  647. VT8231_REG_FAN_MIN(i));
  648. }
  649. low = vt8231_read_value(client, VT8231_REG_TEMP_LOW01);
  650. low = (low >> 6) | ((low & 0x30) >> 2)
  651. | (vt8231_read_value(client, VT8231_REG_TEMP_LOW25) << 4);
  652. for (i = 0; i < 6; i++) {
  653. if (ISTEMP(i, data->uch_config)) {
  654. data->temp[i] = (vt8231_read_value(client,
  655. regtemp[i]) << 2)
  656. | ((low >> (2 * i)) & 0x03);
  657. data->temp_max[i] = vt8231_read_value(client,
  658. regtempmax[i]);
  659. data->temp_min[i] = vt8231_read_value(client,
  660. regtempmin[i]);
  661. }
  662. }
  663. i = vt8231_read_value(client, VT8231_REG_FANDIV);
  664. data->fan_div[0] = (i >> 4) & 0x03;
  665. data->fan_div[1] = i >> 6;
  666. data->alarms = vt8231_read_value(client, VT8231_REG_ALARM1) |
  667. (vt8231_read_value(client, VT8231_REG_ALARM2) << 8);
  668. /* Set alarm flags correctly */
  669. if (!data->fan[0] && data->fan_min[0]) {
  670. data->alarms |= 0x40;
  671. } else if (data->fan[0] && !data->fan_min[0]) {
  672. data->alarms &= ~0x40;
  673. }
  674. if (!data->fan[1] && data->fan_min[1]) {
  675. data->alarms |= 0x80;
  676. } else if (data->fan[1] && !data->fan_min[1]) {
  677. data->alarms &= ~0x80;
  678. }
  679. data->last_updated = jiffies;
  680. data->valid = 1;
  681. }
  682. up(&data->update_lock);
  683. return data;
  684. }
  685. static int __devinit vt8231_pci_probe(struct pci_dev *dev,
  686. const struct pci_device_id *id)
  687. {
  688. u16 val;
  689. if (PCIBIOS_SUCCESSFUL != pci_read_config_word(dev, VT8231_BASE_REG,
  690. &val))
  691. return -ENODEV;
  692. isa_address = val & ~(VT8231_EXTENT - 1);
  693. if (isa_address == 0 && force_addr == 0) {
  694. dev_err(&dev->dev, "base address not set -\
  695. upgrade BIOS or use force_addr=0xaddr\n");
  696. return -ENODEV;
  697. }
  698. s_bridge = pci_dev_get(dev);
  699. if (i2c_isa_add_driver(&vt8231_driver)) {
  700. pci_dev_put(s_bridge);
  701. s_bridge = NULL;
  702. }
  703. /* Always return failure here. This is to allow other drivers to bind
  704. * to this pci device. We don't really want to have control over the
  705. * pci device, we only wanted to read as few register values from it.
  706. */
  707. return -ENODEV;
  708. }
  709. static int __init sm_vt8231_init(void)
  710. {
  711. return pci_register_driver(&vt8231_pci_driver);
  712. }
  713. static void __exit sm_vt8231_exit(void)
  714. {
  715. pci_unregister_driver(&vt8231_pci_driver);
  716. if (s_bridge != NULL) {
  717. i2c_isa_del_driver(&vt8231_driver);
  718. pci_dev_put(s_bridge);
  719. s_bridge = NULL;
  720. }
  721. }
  722. MODULE_AUTHOR("Roger Lucas <roger@planbit.co.uk>");
  723. MODULE_DESCRIPTION("VT8231 sensors");
  724. MODULE_LICENSE("GPL");
  725. module_init(sm_vt8231_init);
  726. module_exit(sm_vt8231_exit);