vt1211.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  1. /*
  2. * vt1211.c - driver for the VIA VT1211 Super-I/O chip integrated hardware
  3. * monitoring features
  4. * Copyright (C) 2006 Juerg Haefliger <juergh@gmail.com>
  5. *
  6. * This driver is based on the driver for kernel 2.4 by Mark D. Studebaker
  7. * and its port to kernel 2.6 by Lars Ekman.
  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. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.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 <asm/io.h>
  34. static int uch_config = -1;
  35. module_param(uch_config, int, 0);
  36. MODULE_PARM_DESC(uch_config, "Initialize the universal channel configuration");
  37. static int int_mode = -1;
  38. module_param(int_mode, int, 0);
  39. MODULE_PARM_DESC(int_mode, "Force the temperature interrupt mode");
  40. static struct platform_device *pdev;
  41. #define DRVNAME "vt1211"
  42. /* ---------------------------------------------------------------------
  43. * Registers
  44. *
  45. * The sensors are defined as follows.
  46. *
  47. * Sensor Voltage Mode Temp Mode Notes (from the datasheet)
  48. * -------- ------------ --------- --------------------------
  49. * Reading 1 temp1 Intel thermal diode
  50. * Reading 3 temp2 Internal thermal diode
  51. * UCH1/Reading2 in0 temp3 NTC type thermistor
  52. * UCH2 in1 temp4 +2.5V
  53. * UCH3 in2 temp5 VccP
  54. * UCH4 in3 temp6 +5V
  55. * UCH5 in4 temp7 +12V
  56. * 3.3V in5 Internal VDD (+3.3V)
  57. *
  58. * --------------------------------------------------------------------- */
  59. /* Voltages (in) numbered 0-5 (ix) */
  60. #define VT1211_REG_IN(ix) (0x21 + (ix))
  61. #define VT1211_REG_IN_MIN(ix) ((ix) == 0 ? 0x3e : 0x2a + 2 * (ix))
  62. #define VT1211_REG_IN_MAX(ix) ((ix) == 0 ? 0x3d : 0x29 + 2 * (ix))
  63. /* Temperatures (temp) numbered 0-6 (ix) */
  64. static u8 regtemp[] = {0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25};
  65. static u8 regtempmax[] = {0x39, 0x1d, 0x3d, 0x2b, 0x2d, 0x2f, 0x31};
  66. static u8 regtemphyst[] = {0x3a, 0x1e, 0x3e, 0x2c, 0x2e, 0x30, 0x32};
  67. /* Fans numbered 0-1 (ix) */
  68. #define VT1211_REG_FAN(ix) (0x29 + (ix))
  69. #define VT1211_REG_FAN_MIN(ix) (0x3b + (ix))
  70. #define VT1211_REG_FAN_DIV 0x47
  71. /* PWMs numbered 0-1 (ix) */
  72. /* Auto points numbered 0-3 (ap) */
  73. #define VT1211_REG_PWM(ix) (0x60 + (ix))
  74. #define VT1211_REG_PWM_CLK 0x50
  75. #define VT1211_REG_PWM_CTL 0x51
  76. #define VT1211_REG_PWM_AUTO_TEMP(ap) (0x55 - (ap))
  77. #define VT1211_REG_PWM_AUTO_PWM(ix, ap) (0x58 + 2 * (ix) - (ap))
  78. /* Miscellaneous registers */
  79. #define VT1211_REG_CONFIG 0x40
  80. #define VT1211_REG_ALARM1 0x41
  81. #define VT1211_REG_ALARM2 0x42
  82. #define VT1211_REG_VID 0x45
  83. #define VT1211_REG_UCH_CONFIG 0x4a
  84. #define VT1211_REG_TEMP1_CONFIG 0x4b
  85. #define VT1211_REG_TEMP2_CONFIG 0x4c
  86. /* In, temp & fan alarm bits */
  87. static const u8 bitalarmin[] = {11, 0, 1, 3, 8, 2, 9};
  88. static const u8 bitalarmtemp[] = {4, 15, 11, 0, 1, 3, 8};
  89. static const u8 bitalarmfan[] = {6, 7};
  90. /* ---------------------------------------------------------------------
  91. * Data structures and manipulation thereof
  92. * --------------------------------------------------------------------- */
  93. struct vt1211_data {
  94. unsigned short addr;
  95. const char *name;
  96. struct class_device *class_dev;
  97. struct mutex update_lock;
  98. char valid; /* !=0 if following fields are valid */
  99. unsigned long last_updated; /* In jiffies */
  100. /* Register values */
  101. u8 in[6];
  102. u8 in_max[6];
  103. u8 in_min[6];
  104. u8 temp[7];
  105. u8 temp_max[7];
  106. u8 temp_hyst[7];
  107. u8 fan[2];
  108. u8 fan_min[2];
  109. u8 fan_div[2];
  110. u8 fan_ctl;
  111. u8 pwm[2];
  112. u8 pwm_ctl[2];
  113. u8 pwm_clk;
  114. u8 pwm_auto_temp[4];
  115. u8 pwm_auto_pwm[2][4];
  116. u8 vid; /* Read once at init time */
  117. u8 vrm;
  118. u8 uch_config; /* Read once at init time */
  119. u16 alarms;
  120. };
  121. /* ix = [0-5] */
  122. #define ISVOLT(ix, uch_config) ((ix) > 4 ? 1 : \
  123. !(((uch_config) >> ((ix) + 2)) & 1))
  124. /* ix = [0-6] */
  125. #define ISTEMP(ix, uch_config) ((ix) < 2 ? 1 : \
  126. ((uch_config) >> (ix)) & 1)
  127. /* in5 (ix = 5) is special. It's the internal 3.3V so it's scaled in the
  128. driver according to the VT1211 BIOS porting guide */
  129. #define IN_FROM_REG(ix, reg) ((reg) < 3 ? 0 : (ix) == 5 ? \
  130. (((reg) - 3) * 15882 + 479) / 958 : \
  131. (((reg) - 3) * 10000 + 479) / 958)
  132. #define IN_TO_REG(ix, val) (SENSORS_LIMIT((ix) == 5 ? \
  133. ((val) * 958 + 7941) / 15882 + 3 : \
  134. ((val) * 958 + 5000) / 10000 + 3, 0, 255))
  135. /* temp1 (ix = 0) is an intel thermal diode which is scaled in user space.
  136. temp2 (ix = 1) is the internal temp diode so it's scaled in the driver
  137. according to some measurements that I took on an EPIA M10000.
  138. temp3-7 are thermistor based so the driver returns the voltage measured at
  139. the pin (range 0V - 2.2V). */
  140. #define TEMP_FROM_REG(ix, reg) ((ix) == 0 ? (reg) * 1000 : \
  141. (ix) == 1 ? (reg) < 51 ? 0 : \
  142. ((reg) - 51) * 1000 : \
  143. ((253 - (reg)) * 2200 + 105) / 210)
  144. #define TEMP_TO_REG(ix, val) SENSORS_LIMIT( \
  145. ((ix) == 0 ? ((val) + 500) / 1000 : \
  146. (ix) == 1 ? ((val) + 500) / 1000 + 51 : \
  147. 253 - ((val) * 210 + 1100) / 2200), 0, 255)
  148. #define DIV_FROM_REG(reg) (1 << (reg))
  149. #define RPM_FROM_REG(reg, div) (((reg) == 0) || ((reg) == 255) ? 0 : \
  150. 1310720 / (reg) / DIV_FROM_REG(div))
  151. #define RPM_TO_REG(val, div) ((val) == 0 ? 255 : \
  152. SENSORS_LIMIT((1310720 / (val) / \
  153. DIV_FROM_REG(div)), 1, 254))
  154. /* ---------------------------------------------------------------------
  155. * Super-I/O constants and functions
  156. * --------------------------------------------------------------------- */
  157. /* Configuration index port registers
  158. * The vt1211 can live at 2 different addresses so we need to probe both */
  159. #define SIO_REG_CIP1 0x2e
  160. #define SIO_REG_CIP2 0x4e
  161. /* Configuration registers */
  162. #define SIO_VT1211_LDN 0x07 /* logical device number */
  163. #define SIO_VT1211_DEVID 0x20 /* device ID */
  164. #define SIO_VT1211_DEVREV 0x21 /* device revision */
  165. #define SIO_VT1211_ACTIVE 0x30 /* HW monitor active */
  166. #define SIO_VT1211_BADDR 0x60 /* base I/O address */
  167. #define SIO_VT1211_ID 0x3c /* VT1211 device ID */
  168. /* VT1211 logical device numbers */
  169. #define SIO_VT1211_LDN_HWMON 0x0b /* HW monitor */
  170. static inline void superio_outb(int sio_cip, int reg, int val)
  171. {
  172. outb(reg, sio_cip);
  173. outb(val, sio_cip + 1);
  174. }
  175. static inline int superio_inb(int sio_cip, int reg)
  176. {
  177. outb(reg, sio_cip);
  178. return inb(sio_cip + 1);
  179. }
  180. static inline void superio_select(int sio_cip, int ldn)
  181. {
  182. outb(SIO_VT1211_LDN, sio_cip);
  183. outb(ldn, sio_cip + 1);
  184. }
  185. static inline void superio_enter(int sio_cip)
  186. {
  187. outb(0x87, sio_cip);
  188. outb(0x87, sio_cip);
  189. }
  190. static inline void superio_exit(int sio_cip)
  191. {
  192. outb(0xaa, sio_cip);
  193. }
  194. /* ---------------------------------------------------------------------
  195. * Device I/O access
  196. * --------------------------------------------------------------------- */
  197. static inline u8 vt1211_read8(struct vt1211_data *data, u8 reg)
  198. {
  199. return inb(data->addr + reg);
  200. }
  201. static inline void vt1211_write8(struct vt1211_data *data, u8 reg, u8 val)
  202. {
  203. outb(val, data->addr + reg);
  204. }
  205. static struct vt1211_data *vt1211_update_device(struct device *dev)
  206. {
  207. struct vt1211_data *data = dev_get_drvdata(dev);
  208. int ix, val;
  209. mutex_lock(&data->update_lock);
  210. /* registers cache is refreshed after 1 second */
  211. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  212. /* read VID */
  213. data->vid = vt1211_read8(data, VT1211_REG_VID) & 0x1f;
  214. /* voltage (in) registers */
  215. for (ix = 0; ix < ARRAY_SIZE(data->in); ix++) {
  216. if (ISVOLT(ix, data->uch_config)) {
  217. data->in[ix] = vt1211_read8(data,
  218. VT1211_REG_IN(ix));
  219. data->in_min[ix] = vt1211_read8(data,
  220. VT1211_REG_IN_MIN(ix));
  221. data->in_max[ix] = vt1211_read8(data,
  222. VT1211_REG_IN_MAX(ix));
  223. }
  224. }
  225. /* temp registers */
  226. for (ix = 0; ix < ARRAY_SIZE(data->temp); ix++) {
  227. if (ISTEMP(ix, data->uch_config)) {
  228. data->temp[ix] = vt1211_read8(data,
  229. regtemp[ix]);
  230. data->temp_max[ix] = vt1211_read8(data,
  231. regtempmax[ix]);
  232. data->temp_hyst[ix] = vt1211_read8(data,
  233. regtemphyst[ix]);
  234. }
  235. }
  236. /* fan & pwm registers */
  237. for (ix = 0; ix < ARRAY_SIZE(data->fan); ix++) {
  238. data->fan[ix] = vt1211_read8(data,
  239. VT1211_REG_FAN(ix));
  240. data->fan_min[ix] = vt1211_read8(data,
  241. VT1211_REG_FAN_MIN(ix));
  242. data->pwm[ix] = vt1211_read8(data,
  243. VT1211_REG_PWM(ix));
  244. }
  245. val = vt1211_read8(data, VT1211_REG_FAN_DIV);
  246. data->fan_div[0] = (val >> 4) & 3;
  247. data->fan_div[1] = (val >> 6) & 3;
  248. data->fan_ctl = val & 0xf;
  249. val = vt1211_read8(data, VT1211_REG_PWM_CTL);
  250. data->pwm_ctl[0] = val & 0xf;
  251. data->pwm_ctl[1] = (val >> 4) & 0xf;
  252. data->pwm_clk = vt1211_read8(data, VT1211_REG_PWM_CLK);
  253. /* pwm & temp auto point registers */
  254. data->pwm_auto_pwm[0][1] = vt1211_read8(data,
  255. VT1211_REG_PWM_AUTO_PWM(0, 1));
  256. data->pwm_auto_pwm[0][2] = vt1211_read8(data,
  257. VT1211_REG_PWM_AUTO_PWM(0, 2));
  258. data->pwm_auto_pwm[1][1] = vt1211_read8(data,
  259. VT1211_REG_PWM_AUTO_PWM(1, 1));
  260. data->pwm_auto_pwm[1][2] = vt1211_read8(data,
  261. VT1211_REG_PWM_AUTO_PWM(1, 2));
  262. for (ix = 0; ix < ARRAY_SIZE(data->pwm_auto_temp); ix++) {
  263. data->pwm_auto_temp[ix] = vt1211_read8(data,
  264. VT1211_REG_PWM_AUTO_TEMP(ix));
  265. }
  266. /* alarm registers */
  267. data->alarms = (vt1211_read8(data, VT1211_REG_ALARM2) << 8) |
  268. vt1211_read8(data, VT1211_REG_ALARM1);
  269. data->last_updated = jiffies;
  270. data->valid = 1;
  271. }
  272. mutex_unlock(&data->update_lock);
  273. return data;
  274. }
  275. /* ---------------------------------------------------------------------
  276. * Voltage sysfs interfaces
  277. * ix = [0-5]
  278. * --------------------------------------------------------------------- */
  279. #define SHOW_IN_INPUT 0
  280. #define SHOW_SET_IN_MIN 1
  281. #define SHOW_SET_IN_MAX 2
  282. #define SHOW_IN_ALARM 3
  283. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  284. char *buf)
  285. {
  286. struct vt1211_data *data = vt1211_update_device(dev);
  287. struct sensor_device_attribute_2 *sensor_attr_2 =
  288. to_sensor_dev_attr_2(attr);
  289. int ix = sensor_attr_2->index;
  290. int fn = sensor_attr_2->nr;
  291. int res;
  292. switch (fn) {
  293. case SHOW_IN_INPUT:
  294. res = IN_FROM_REG(ix, data->in[ix]);
  295. break;
  296. case SHOW_SET_IN_MIN:
  297. res = IN_FROM_REG(ix, data->in_min[ix]);
  298. break;
  299. case SHOW_SET_IN_MAX:
  300. res = IN_FROM_REG(ix, data->in_max[ix]);
  301. break;
  302. case SHOW_IN_ALARM:
  303. res = (data->alarms >> bitalarmin[ix]) & 1;
  304. break;
  305. default:
  306. res = 0;
  307. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  308. }
  309. return sprintf(buf, "%d\n", res);
  310. }
  311. static ssize_t set_in(struct device *dev, struct device_attribute *attr,
  312. const char *buf, size_t count)
  313. {
  314. struct vt1211_data *data = dev_get_drvdata(dev);
  315. struct sensor_device_attribute_2 *sensor_attr_2 =
  316. to_sensor_dev_attr_2(attr);
  317. int ix = sensor_attr_2->index;
  318. int fn = sensor_attr_2->nr;
  319. long val = simple_strtol(buf, NULL, 10);
  320. mutex_lock(&data->update_lock);
  321. switch (fn) {
  322. case SHOW_SET_IN_MIN:
  323. data->in_min[ix] = IN_TO_REG(ix, val);
  324. vt1211_write8(data, VT1211_REG_IN_MIN(ix), data->in_min[ix]);
  325. break;
  326. case SHOW_SET_IN_MAX:
  327. data->in_max[ix] = IN_TO_REG(ix, val);
  328. vt1211_write8(data, VT1211_REG_IN_MAX(ix), data->in_max[ix]);
  329. break;
  330. default:
  331. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  332. }
  333. mutex_unlock(&data->update_lock);
  334. return count;
  335. }
  336. /* ---------------------------------------------------------------------
  337. * Temperature sysfs interfaces
  338. * ix = [0-6]
  339. * --------------------------------------------------------------------- */
  340. #define SHOW_TEMP_INPUT 0
  341. #define SHOW_SET_TEMP_MAX 1
  342. #define SHOW_SET_TEMP_MAX_HYST 2
  343. #define SHOW_TEMP_ALARM 3
  344. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  345. char *buf)
  346. {
  347. struct vt1211_data *data = vt1211_update_device(dev);
  348. struct sensor_device_attribute_2 *sensor_attr_2 =
  349. to_sensor_dev_attr_2(attr);
  350. int ix = sensor_attr_2->index;
  351. int fn = sensor_attr_2->nr;
  352. int res;
  353. switch (fn) {
  354. case SHOW_TEMP_INPUT:
  355. res = TEMP_FROM_REG(ix, data->temp[ix]);
  356. break;
  357. case SHOW_SET_TEMP_MAX:
  358. res = TEMP_FROM_REG(ix, data->temp_max[ix]);
  359. break;
  360. case SHOW_SET_TEMP_MAX_HYST:
  361. res = TEMP_FROM_REG(ix, data->temp_hyst[ix]);
  362. break;
  363. case SHOW_TEMP_ALARM:
  364. res = (data->alarms >> bitalarmtemp[ix]) & 1;
  365. break;
  366. default:
  367. res = 0;
  368. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  369. }
  370. return sprintf(buf, "%d\n", res);
  371. }
  372. static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
  373. const char *buf, size_t count)
  374. {
  375. struct vt1211_data *data = dev_get_drvdata(dev);
  376. struct sensor_device_attribute_2 *sensor_attr_2 =
  377. to_sensor_dev_attr_2(attr);
  378. int ix = sensor_attr_2->index;
  379. int fn = sensor_attr_2->nr;
  380. long val = simple_strtol(buf, NULL, 10);
  381. mutex_lock(&data->update_lock);
  382. switch (fn) {
  383. case SHOW_SET_TEMP_MAX:
  384. data->temp_max[ix] = TEMP_TO_REG(ix, val);
  385. vt1211_write8(data, regtempmax[ix],
  386. data->temp_max[ix]);
  387. break;
  388. case SHOW_SET_TEMP_MAX_HYST:
  389. data->temp_hyst[ix] = TEMP_TO_REG(ix, val);
  390. vt1211_write8(data, regtemphyst[ix],
  391. data->temp_hyst[ix]);
  392. break;
  393. default:
  394. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  395. }
  396. mutex_unlock(&data->update_lock);
  397. return count;
  398. }
  399. /* ---------------------------------------------------------------------
  400. * Fan sysfs interfaces
  401. * ix = [0-1]
  402. * --------------------------------------------------------------------- */
  403. #define SHOW_FAN_INPUT 0
  404. #define SHOW_SET_FAN_MIN 1
  405. #define SHOW_SET_FAN_DIV 2
  406. #define SHOW_FAN_ALARM 3
  407. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  408. char *buf)
  409. {
  410. struct vt1211_data *data = vt1211_update_device(dev);
  411. struct sensor_device_attribute_2 *sensor_attr_2 =
  412. to_sensor_dev_attr_2(attr);
  413. int ix = sensor_attr_2->index;
  414. int fn = sensor_attr_2->nr;
  415. int res;
  416. switch (fn) {
  417. case SHOW_FAN_INPUT:
  418. res = RPM_FROM_REG(data->fan[ix], data->fan_div[ix]);
  419. break;
  420. case SHOW_SET_FAN_MIN:
  421. res = RPM_FROM_REG(data->fan_min[ix], data->fan_div[ix]);
  422. break;
  423. case SHOW_SET_FAN_DIV:
  424. res = DIV_FROM_REG(data->fan_div[ix]);
  425. break;
  426. case SHOW_FAN_ALARM:
  427. res = (data->alarms >> bitalarmfan[ix]) & 1;
  428. break;
  429. default:
  430. res = 0;
  431. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  432. }
  433. return sprintf(buf, "%d\n", res);
  434. }
  435. static ssize_t set_fan(struct device *dev, struct device_attribute *attr,
  436. const char *buf, size_t count)
  437. {
  438. struct vt1211_data *data = dev_get_drvdata(dev);
  439. struct sensor_device_attribute_2 *sensor_attr_2 =
  440. to_sensor_dev_attr_2(attr);
  441. int ix = sensor_attr_2->index;
  442. int fn = sensor_attr_2->nr;
  443. long val = simple_strtol(buf, NULL, 10);
  444. int reg;
  445. mutex_lock(&data->update_lock);
  446. /* sync the data cache */
  447. reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
  448. data->fan_div[0] = (reg >> 4) & 3;
  449. data->fan_div[1] = (reg >> 6) & 3;
  450. data->fan_ctl = reg & 0xf;
  451. switch (fn) {
  452. case SHOW_SET_FAN_MIN:
  453. data->fan_min[ix] = RPM_TO_REG(val, data->fan_div[ix]);
  454. vt1211_write8(data, VT1211_REG_FAN_MIN(ix),
  455. data->fan_min[ix]);
  456. break;
  457. case SHOW_SET_FAN_DIV:
  458. switch (val) {
  459. case 1: data->fan_div[ix] = 0; break;
  460. case 2: data->fan_div[ix] = 1; break;
  461. case 4: data->fan_div[ix] = 2; break;
  462. case 8: data->fan_div[ix] = 3; break;
  463. default:
  464. count = -EINVAL;
  465. dev_warn(dev, "fan div value %ld not "
  466. "supported. Choose one of 1, 2, "
  467. "4, or 8.\n", val);
  468. goto EXIT;
  469. }
  470. vt1211_write8(data, VT1211_REG_FAN_DIV,
  471. ((data->fan_div[1] << 6) |
  472. (data->fan_div[0] << 4) |
  473. data->fan_ctl));
  474. break;
  475. default:
  476. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  477. }
  478. EXIT:
  479. mutex_unlock(&data->update_lock);
  480. return count;
  481. }
  482. /* ---------------------------------------------------------------------
  483. * PWM sysfs interfaces
  484. * ix = [0-1]
  485. * --------------------------------------------------------------------- */
  486. #define SHOW_PWM 0
  487. #define SHOW_SET_PWM_ENABLE 1
  488. #define SHOW_SET_PWM_FREQ 2
  489. #define SHOW_SET_PWM_AUTO_CHANNELS_TEMP 3
  490. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  491. char *buf)
  492. {
  493. struct vt1211_data *data = vt1211_update_device(dev);
  494. struct sensor_device_attribute_2 *sensor_attr_2 =
  495. to_sensor_dev_attr_2(attr);
  496. int ix = sensor_attr_2->index;
  497. int fn = sensor_attr_2->nr;
  498. int res;
  499. switch (fn) {
  500. case SHOW_PWM:
  501. res = data->pwm[ix];
  502. break;
  503. case SHOW_SET_PWM_ENABLE:
  504. res = ((data->pwm_ctl[ix] >> 3) & 1) ? 2 : 0;
  505. break;
  506. case SHOW_SET_PWM_FREQ:
  507. res = 90000 >> (data->pwm_clk & 7);
  508. break;
  509. case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
  510. res = (data->pwm_ctl[ix] & 7) + 1;
  511. break;
  512. default:
  513. res = 0;
  514. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  515. }
  516. return sprintf(buf, "%d\n", res);
  517. }
  518. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  519. const char *buf, size_t count)
  520. {
  521. struct vt1211_data *data = dev_get_drvdata(dev);
  522. struct sensor_device_attribute_2 *sensor_attr_2 =
  523. to_sensor_dev_attr_2(attr);
  524. int ix = sensor_attr_2->index;
  525. int fn = sensor_attr_2->nr;
  526. long val = simple_strtol(buf, NULL, 10);
  527. int tmp, reg;
  528. mutex_lock(&data->update_lock);
  529. switch (fn) {
  530. case SHOW_SET_PWM_ENABLE:
  531. /* sync the data cache */
  532. reg = vt1211_read8(data, VT1211_REG_FAN_DIV);
  533. data->fan_div[0] = (reg >> 4) & 3;
  534. data->fan_div[1] = (reg >> 6) & 3;
  535. data->fan_ctl = reg & 0xf;
  536. reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
  537. data->pwm_ctl[0] = reg & 0xf;
  538. data->pwm_ctl[1] = (reg >> 4) & 0xf;
  539. switch (val) {
  540. case 0:
  541. data->pwm_ctl[ix] &= 7;
  542. /* disable SmartGuardian if both PWM outputs are
  543. * disabled */
  544. if ((data->pwm_ctl[ix ^ 1] & 1) == 0) {
  545. data->fan_ctl &= 0xe;
  546. }
  547. break;
  548. case 2:
  549. data->pwm_ctl[ix] |= 8;
  550. data->fan_ctl |= 1;
  551. break;
  552. default:
  553. count = -EINVAL;
  554. dev_warn(dev, "pwm mode %ld not supported. "
  555. "Choose one of 0 or 2.\n", val);
  556. goto EXIT;
  557. }
  558. vt1211_write8(data, VT1211_REG_PWM_CTL,
  559. ((data->pwm_ctl[1] << 4) |
  560. data->pwm_ctl[0]));
  561. vt1211_write8(data, VT1211_REG_FAN_DIV,
  562. ((data->fan_div[1] << 6) |
  563. (data->fan_div[0] << 4) |
  564. data->fan_ctl));
  565. break;
  566. case SHOW_SET_PWM_FREQ:
  567. val = 135000 / SENSORS_LIMIT(val, 135000 >> 7, 135000);
  568. /* calculate tmp = log2(val) */
  569. tmp = 0;
  570. for (val >>= 1; val > 0; val >>= 1) {
  571. tmp++;
  572. }
  573. /* sync the data cache */
  574. reg = vt1211_read8(data, VT1211_REG_PWM_CLK);
  575. data->pwm_clk = (reg & 0xf8) | tmp;
  576. vt1211_write8(data, VT1211_REG_PWM_CLK, data->pwm_clk);
  577. break;
  578. case SHOW_SET_PWM_AUTO_CHANNELS_TEMP:
  579. if ((val < 1) || (val > 7)) {
  580. count = -EINVAL;
  581. dev_warn(dev, "temp channel %ld not supported. "
  582. "Choose a value between 1 and 7.\n", val);
  583. goto EXIT;
  584. }
  585. if (!ISTEMP(val - 1, data->uch_config)) {
  586. count = -EINVAL;
  587. dev_warn(dev, "temp channel %ld is not available.\n",
  588. val);
  589. goto EXIT;
  590. }
  591. /* sync the data cache */
  592. reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
  593. data->pwm_ctl[0] = reg & 0xf;
  594. data->pwm_ctl[1] = (reg >> 4) & 0xf;
  595. data->pwm_ctl[ix] = (data->pwm_ctl[ix] & 8) | (val - 1);
  596. vt1211_write8(data, VT1211_REG_PWM_CTL,
  597. ((data->pwm_ctl[1] << 4) | data->pwm_ctl[0]));
  598. break;
  599. default:
  600. dev_dbg(dev, "Unknown attr fetch (%d)\n", fn);
  601. }
  602. EXIT:
  603. mutex_unlock(&data->update_lock);
  604. return count;
  605. }
  606. /* ---------------------------------------------------------------------
  607. * PWM auto point definitions
  608. * ix = [0-1]
  609. * ap = [0-3]
  610. * --------------------------------------------------------------------- */
  611. /*
  612. * pwm[ix+1]_auto_point[ap+1]_temp mapping table:
  613. * Note that there is only a single set of temp auto points that controls both
  614. * PWM controllers. We still create 2 sets of sysfs files to make it look
  615. * more consistent even though they map to the same registers.
  616. *
  617. * ix ap : description
  618. * -------------------
  619. * 0 0 : pwm1/2 off temperature (pwm_auto_temp[0])
  620. * 0 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
  621. * 0 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
  622. * 0 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
  623. * 1 0 : pwm1/2 off temperature (pwm_auto_temp[0])
  624. * 1 1 : pwm1/2 low speed temperature (pwm_auto_temp[1])
  625. * 1 2 : pwm1/2 high speed temperature (pwm_auto_temp[2])
  626. * 1 3 : pwm1/2 full speed temperature (pwm_auto_temp[3])
  627. */
  628. static ssize_t show_pwm_auto_point_temp(struct device *dev,
  629. struct device_attribute *attr,
  630. char *buf)
  631. {
  632. struct vt1211_data *data = vt1211_update_device(dev);
  633. struct sensor_device_attribute_2 *sensor_attr_2 =
  634. to_sensor_dev_attr_2(attr);
  635. int ix = sensor_attr_2->index;
  636. int ap = sensor_attr_2->nr;
  637. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->pwm_ctl[ix] & 7,
  638. data->pwm_auto_temp[ap]));
  639. }
  640. static ssize_t set_pwm_auto_point_temp(struct device *dev,
  641. struct device_attribute *attr,
  642. const char *buf, size_t count)
  643. {
  644. struct vt1211_data *data = dev_get_drvdata(dev);
  645. struct sensor_device_attribute_2 *sensor_attr_2 =
  646. to_sensor_dev_attr_2(attr);
  647. int ix = sensor_attr_2->index;
  648. int ap = sensor_attr_2->nr;
  649. long val = simple_strtol(buf, NULL, 10);
  650. int reg;
  651. mutex_lock(&data->update_lock);
  652. /* sync the data cache */
  653. reg = vt1211_read8(data, VT1211_REG_PWM_CTL);
  654. data->pwm_ctl[0] = reg & 0xf;
  655. data->pwm_ctl[1] = (reg >> 4) & 0xf;
  656. data->pwm_auto_temp[ap] = TEMP_TO_REG(data->pwm_ctl[ix] & 7, val);
  657. vt1211_write8(data, VT1211_REG_PWM_AUTO_TEMP(ap),
  658. data->pwm_auto_temp[ap]);
  659. mutex_unlock(&data->update_lock);
  660. return count;
  661. }
  662. /*
  663. * pwm[ix+1]_auto_point[ap+1]_pwm mapping table:
  664. * Note that the PWM auto points 0 & 3 are hard-wired in the VT1211 and can't
  665. * be changed.
  666. *
  667. * ix ap : description
  668. * -------------------
  669. * 0 0 : pwm1 off (pwm_auto_pwm[0][0], hard-wired to 0)
  670. * 0 1 : pwm1 low speed duty cycle (pwm_auto_pwm[0][1])
  671. * 0 2 : pwm1 high speed duty cycle (pwm_auto_pwm[0][2])
  672. * 0 3 : pwm1 full speed (pwm_auto_pwm[0][3], hard-wired to 255)
  673. * 1 0 : pwm2 off (pwm_auto_pwm[1][0], hard-wired to 0)
  674. * 1 1 : pwm2 low speed duty cycle (pwm_auto_pwm[1][1])
  675. * 1 2 : pwm2 high speed duty cycle (pwm_auto_pwm[1][2])
  676. * 1 3 : pwm2 full speed (pwm_auto_pwm[1][3], hard-wired to 255)
  677. */
  678. static ssize_t show_pwm_auto_point_pwm(struct device *dev,
  679. struct device_attribute *attr,
  680. char *buf)
  681. {
  682. struct vt1211_data *data = vt1211_update_device(dev);
  683. struct sensor_device_attribute_2 *sensor_attr_2 =
  684. to_sensor_dev_attr_2(attr);
  685. int ix = sensor_attr_2->index;
  686. int ap = sensor_attr_2->nr;
  687. return sprintf(buf, "%d\n", data->pwm_auto_pwm[ix][ap]);
  688. }
  689. static ssize_t set_pwm_auto_point_pwm(struct device *dev,
  690. struct device_attribute *attr,
  691. const char *buf, size_t count)
  692. {
  693. struct vt1211_data *data = dev_get_drvdata(dev);
  694. struct sensor_device_attribute_2 *sensor_attr_2 =
  695. to_sensor_dev_attr_2(attr);
  696. int ix = sensor_attr_2->index;
  697. int ap = sensor_attr_2->nr;
  698. long val = simple_strtol(buf, NULL, 10);
  699. if ((val < 0) || (val > 255)) {
  700. dev_err(dev, "pwm value %ld is out of range. "
  701. "Choose a value between 0 and 255." , val);
  702. return -EINVAL;
  703. }
  704. mutex_lock(&data->update_lock);
  705. data->pwm_auto_pwm[ix][ap] = val;
  706. vt1211_write8(data, VT1211_REG_PWM_AUTO_PWM(ix, ap),
  707. data->pwm_auto_pwm[ix][ap]);
  708. mutex_unlock(&data->update_lock);
  709. return count;
  710. }
  711. /* ---------------------------------------------------------------------
  712. * Miscellaneous sysfs interfaces (VRM, VID, name, and (legacy) alarms)
  713. * --------------------------------------------------------------------- */
  714. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr,
  715. char *buf)
  716. {
  717. struct vt1211_data *data = dev_get_drvdata(dev);
  718. return sprintf(buf, "%d\n", data->vrm);
  719. }
  720. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
  721. const char *buf, size_t count)
  722. {
  723. struct vt1211_data *data = dev_get_drvdata(dev);
  724. long val = simple_strtol(buf, NULL, 10);
  725. data->vrm = val;
  726. return count;
  727. }
  728. static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
  729. char *buf)
  730. {
  731. struct vt1211_data *data = dev_get_drvdata(dev);
  732. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  733. }
  734. static ssize_t show_name(struct device *dev,
  735. struct device_attribute *attr, char *buf)
  736. {
  737. struct vt1211_data *data = dev_get_drvdata(dev);
  738. return sprintf(buf, "%s\n", data->name);
  739. }
  740. static ssize_t show_alarms(struct device *dev,
  741. struct device_attribute *attr, char *buf)
  742. {
  743. struct vt1211_data *data = vt1211_update_device(dev);
  744. return sprintf(buf, "%d\n", data->alarms);
  745. }
  746. /* ---------------------------------------------------------------------
  747. * Device attribute structs
  748. * --------------------------------------------------------------------- */
  749. #define SENSOR_ATTR_IN_INPUT(ix) \
  750. SENSOR_ATTR_2(in##ix##_input, S_IRUGO, \
  751. show_in, NULL, SHOW_IN_INPUT, ix)
  752. static struct sensor_device_attribute_2 vt1211_sysfs_in_input[] = {
  753. SENSOR_ATTR_IN_INPUT(0),
  754. SENSOR_ATTR_IN_INPUT(1),
  755. SENSOR_ATTR_IN_INPUT(2),
  756. SENSOR_ATTR_IN_INPUT(3),
  757. SENSOR_ATTR_IN_INPUT(4),
  758. SENSOR_ATTR_IN_INPUT(5),
  759. };
  760. #define SENSOR_ATTR_IN_MIN(ix) \
  761. SENSOR_ATTR_2(in##ix##_min, S_IRUGO | S_IWUSR, \
  762. show_in, set_in, SHOW_SET_IN_MIN, ix)
  763. static struct sensor_device_attribute_2 vt1211_sysfs_in_min[] = {
  764. SENSOR_ATTR_IN_MIN(0),
  765. SENSOR_ATTR_IN_MIN(1),
  766. SENSOR_ATTR_IN_MIN(2),
  767. SENSOR_ATTR_IN_MIN(3),
  768. SENSOR_ATTR_IN_MIN(4),
  769. SENSOR_ATTR_IN_MIN(5),
  770. };
  771. #define SENSOR_ATTR_IN_MAX(ix) \
  772. SENSOR_ATTR_2(in##ix##_max, S_IRUGO | S_IWUSR, \
  773. show_in, set_in, SHOW_SET_IN_MAX, ix)
  774. static struct sensor_device_attribute_2 vt1211_sysfs_in_max[] = {
  775. SENSOR_ATTR_IN_MAX(0),
  776. SENSOR_ATTR_IN_MAX(1),
  777. SENSOR_ATTR_IN_MAX(2),
  778. SENSOR_ATTR_IN_MAX(3),
  779. SENSOR_ATTR_IN_MAX(4),
  780. SENSOR_ATTR_IN_MAX(5),
  781. };
  782. #define SENSOR_ATTR_IN_ALARM(ix) \
  783. SENSOR_ATTR_2(in##ix##_alarm, S_IRUGO, \
  784. show_in, NULL, SHOW_IN_ALARM, ix)
  785. static struct sensor_device_attribute_2 vt1211_sysfs_in_alarm[] = {
  786. SENSOR_ATTR_IN_ALARM(0),
  787. SENSOR_ATTR_IN_ALARM(1),
  788. SENSOR_ATTR_IN_ALARM(2),
  789. SENSOR_ATTR_IN_ALARM(3),
  790. SENSOR_ATTR_IN_ALARM(4),
  791. SENSOR_ATTR_IN_ALARM(5),
  792. };
  793. #define SENSOR_ATTR_TEMP_INPUT(ix) \
  794. SENSOR_ATTR_2(temp##ix##_input, S_IRUGO, \
  795. show_temp, NULL, SHOW_TEMP_INPUT, ix-1)
  796. static struct sensor_device_attribute_2 vt1211_sysfs_temp_input[] = {
  797. SENSOR_ATTR_TEMP_INPUT(1),
  798. SENSOR_ATTR_TEMP_INPUT(2),
  799. SENSOR_ATTR_TEMP_INPUT(3),
  800. SENSOR_ATTR_TEMP_INPUT(4),
  801. SENSOR_ATTR_TEMP_INPUT(5),
  802. SENSOR_ATTR_TEMP_INPUT(6),
  803. SENSOR_ATTR_TEMP_INPUT(7),
  804. };
  805. #define SENSOR_ATTR_TEMP_MAX(ix) \
  806. SENSOR_ATTR_2(temp##ix##_max, S_IRUGO | S_IWUSR, \
  807. show_temp, set_temp, SHOW_SET_TEMP_MAX, ix-1)
  808. static struct sensor_device_attribute_2 vt1211_sysfs_temp_max[] = {
  809. SENSOR_ATTR_TEMP_MAX(1),
  810. SENSOR_ATTR_TEMP_MAX(2),
  811. SENSOR_ATTR_TEMP_MAX(3),
  812. SENSOR_ATTR_TEMP_MAX(4),
  813. SENSOR_ATTR_TEMP_MAX(5),
  814. SENSOR_ATTR_TEMP_MAX(6),
  815. SENSOR_ATTR_TEMP_MAX(7),
  816. };
  817. #define SENSOR_ATTR_TEMP_MAX_HYST(ix) \
  818. SENSOR_ATTR_2(temp##ix##_max_hyst, S_IRUGO | S_IWUSR, \
  819. show_temp, set_temp, SHOW_SET_TEMP_MAX_HYST, ix-1)
  820. static struct sensor_device_attribute_2 vt1211_sysfs_temp_max_hyst[] = {
  821. SENSOR_ATTR_TEMP_MAX_HYST(1),
  822. SENSOR_ATTR_TEMP_MAX_HYST(2),
  823. SENSOR_ATTR_TEMP_MAX_HYST(3),
  824. SENSOR_ATTR_TEMP_MAX_HYST(4),
  825. SENSOR_ATTR_TEMP_MAX_HYST(5),
  826. SENSOR_ATTR_TEMP_MAX_HYST(6),
  827. SENSOR_ATTR_TEMP_MAX_HYST(7),
  828. };
  829. #define SENSOR_ATTR_TEMP_ALARM(ix) \
  830. SENSOR_ATTR_2(temp##ix##_alarm, S_IRUGO, \
  831. show_temp, NULL, SHOW_TEMP_ALARM, ix-1)
  832. static struct sensor_device_attribute_2 vt1211_sysfs_temp_alarm[] = {
  833. SENSOR_ATTR_TEMP_ALARM(1),
  834. SENSOR_ATTR_TEMP_ALARM(2),
  835. SENSOR_ATTR_TEMP_ALARM(3),
  836. SENSOR_ATTR_TEMP_ALARM(4),
  837. SENSOR_ATTR_TEMP_ALARM(5),
  838. SENSOR_ATTR_TEMP_ALARM(6),
  839. SENSOR_ATTR_TEMP_ALARM(7),
  840. };
  841. #define SENSOR_ATTR_FAN(ix) \
  842. SENSOR_ATTR_2(fan##ix##_input, S_IRUGO, \
  843. show_fan, NULL, SHOW_FAN_INPUT, ix-1), \
  844. SENSOR_ATTR_2(fan##ix##_min, S_IRUGO | S_IWUSR, \
  845. show_fan, set_fan, SHOW_SET_FAN_MIN, ix-1), \
  846. SENSOR_ATTR_2(fan##ix##_div, S_IRUGO | S_IWUSR, \
  847. show_fan, set_fan, SHOW_SET_FAN_DIV, ix-1), \
  848. SENSOR_ATTR_2(fan##ix##_alarm, S_IRUGO, \
  849. show_fan, NULL, SHOW_FAN_ALARM, ix-1)
  850. #define SENSOR_ATTR_PWM(ix) \
  851. SENSOR_ATTR_2(pwm##ix, S_IRUGO, \
  852. show_pwm, NULL, SHOW_PWM, ix-1), \
  853. SENSOR_ATTR_2(pwm##ix##_enable, S_IRUGO | S_IWUSR, \
  854. show_pwm, set_pwm, SHOW_SET_PWM_ENABLE, ix-1), \
  855. SENSOR_ATTR_2(pwm##ix##_auto_channels_temp, S_IRUGO | S_IWUSR, \
  856. show_pwm, set_pwm, SHOW_SET_PWM_AUTO_CHANNELS_TEMP, ix-1)
  857. #define SENSOR_ATTR_PWM_FREQ(ix) \
  858. SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO | S_IWUSR, \
  859. show_pwm, set_pwm, SHOW_SET_PWM_FREQ, ix-1)
  860. #define SENSOR_ATTR_PWM_FREQ_RO(ix) \
  861. SENSOR_ATTR_2(pwm##ix##_freq, S_IRUGO, \
  862. show_pwm, NULL, SHOW_SET_PWM_FREQ, ix-1)
  863. #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP(ix, ap) \
  864. SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO | S_IWUSR, \
  865. show_pwm_auto_point_temp, set_pwm_auto_point_temp, \
  866. ap-1, ix-1)
  867. #define SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(ix, ap) \
  868. SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_temp, S_IRUGO, \
  869. show_pwm_auto_point_temp, NULL, \
  870. ap-1, ix-1)
  871. #define SENSOR_ATTR_PWM_AUTO_POINT_PWM(ix, ap) \
  872. SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO | S_IWUSR, \
  873. show_pwm_auto_point_pwm, set_pwm_auto_point_pwm, \
  874. ap-1, ix-1)
  875. #define SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(ix, ap) \
  876. SENSOR_ATTR_2(pwm##ix##_auto_point##ap##_pwm, S_IRUGO, \
  877. show_pwm_auto_point_pwm, NULL, \
  878. ap-1, ix-1)
  879. static struct sensor_device_attribute_2 vt1211_sysfs_fan_pwm[] = {
  880. SENSOR_ATTR_FAN(1),
  881. SENSOR_ATTR_FAN(2),
  882. SENSOR_ATTR_PWM(1),
  883. SENSOR_ATTR_PWM(2),
  884. SENSOR_ATTR_PWM_FREQ(1),
  885. SENSOR_ATTR_PWM_FREQ_RO(2),
  886. SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 1),
  887. SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 2),
  888. SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 3),
  889. SENSOR_ATTR_PWM_AUTO_POINT_TEMP(1, 4),
  890. SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 1),
  891. SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 2),
  892. SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 3),
  893. SENSOR_ATTR_PWM_AUTO_POINT_TEMP_RO(2, 4),
  894. SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 1),
  895. SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 2),
  896. SENSOR_ATTR_PWM_AUTO_POINT_PWM(1, 3),
  897. SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(1, 4),
  898. SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 1),
  899. SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 2),
  900. SENSOR_ATTR_PWM_AUTO_POINT_PWM(2, 3),
  901. SENSOR_ATTR_PWM_AUTO_POINT_PWM_RO(2, 4),
  902. };
  903. static struct device_attribute vt1211_sysfs_misc[] = {
  904. __ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm),
  905. __ATTR(cpu0_vid, S_IRUGO, show_vid, NULL),
  906. __ATTR(name, S_IRUGO, show_name, NULL),
  907. __ATTR(alarms, S_IRUGO, show_alarms, NULL),
  908. };
  909. /* ---------------------------------------------------------------------
  910. * Device registration and initialization
  911. * --------------------------------------------------------------------- */
  912. static void __devinit vt1211_init_device(struct vt1211_data *data)
  913. {
  914. /* set VRM */
  915. data->vrm = vid_which_vrm();
  916. /* Read (and initialize) UCH config */
  917. data->uch_config = vt1211_read8(data, VT1211_REG_UCH_CONFIG);
  918. if (uch_config > -1) {
  919. data->uch_config = (data->uch_config & 0x83) |
  920. (uch_config << 2);
  921. vt1211_write8(data, VT1211_REG_UCH_CONFIG, data->uch_config);
  922. }
  923. /* Initialize the interrupt mode (if request at module load time).
  924. * The VT1211 implements 3 different modes for clearing interrupts:
  925. * 0: Clear INT when status register is read. Regenerate INT as long
  926. * as temp stays above hysteresis limit.
  927. * 1: Clear INT when status register is read. DON'T regenerate INT
  928. * until temp falls below hysteresis limit and exceeds hot limit
  929. * again.
  930. * 2: Clear INT when temp falls below max limit.
  931. *
  932. * The driver only allows to force mode 0 since that's the only one
  933. * that makes sense for 'sensors' */
  934. if (int_mode == 0) {
  935. vt1211_write8(data, VT1211_REG_TEMP1_CONFIG, 0);
  936. vt1211_write8(data, VT1211_REG_TEMP2_CONFIG, 0);
  937. }
  938. /* Fill in some hard wired values into our data struct */
  939. data->pwm_auto_pwm[0][3] = 255;
  940. data->pwm_auto_pwm[1][3] = 255;
  941. }
  942. static void vt1211_remove_sysfs(struct platform_device *pdev)
  943. {
  944. struct device *dev = &pdev->dev;
  945. int i;
  946. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
  947. device_remove_file(dev,
  948. &vt1211_sysfs_in_input[i].dev_attr);
  949. device_remove_file(dev,
  950. &vt1211_sysfs_in_min[i].dev_attr);
  951. device_remove_file(dev,
  952. &vt1211_sysfs_in_max[i].dev_attr);
  953. device_remove_file(dev,
  954. &vt1211_sysfs_in_alarm[i].dev_attr);
  955. }
  956. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
  957. device_remove_file(dev,
  958. &vt1211_sysfs_temp_input[i].dev_attr);
  959. device_remove_file(dev,
  960. &vt1211_sysfs_temp_max[i].dev_attr);
  961. device_remove_file(dev,
  962. &vt1211_sysfs_temp_max_hyst[i].dev_attr);
  963. device_remove_file(dev,
  964. &vt1211_sysfs_temp_alarm[i].dev_attr);
  965. }
  966. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
  967. device_remove_file(dev,
  968. &vt1211_sysfs_fan_pwm[i].dev_attr);
  969. }
  970. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
  971. device_remove_file(dev, &vt1211_sysfs_misc[i]);
  972. }
  973. }
  974. static int __devinit vt1211_probe(struct platform_device *pdev)
  975. {
  976. struct device *dev = &pdev->dev;
  977. struct vt1211_data *data;
  978. struct resource *res;
  979. int i, err;
  980. if (!(data = kzalloc(sizeof(struct vt1211_data), GFP_KERNEL))) {
  981. err = -ENOMEM;
  982. dev_err(dev, "Out of memory\n");
  983. goto EXIT;
  984. }
  985. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  986. data->addr = res->start;
  987. data->name = DRVNAME;
  988. mutex_init(&data->update_lock);
  989. platform_set_drvdata(pdev, data);
  990. /* Initialize the VT1211 chip */
  991. vt1211_init_device(data);
  992. /* Create sysfs interface files */
  993. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_in_input); i++) {
  994. if (ISVOLT(i, data->uch_config)) {
  995. if ((err = device_create_file(dev,
  996. &vt1211_sysfs_in_input[i].dev_attr)) ||
  997. (err = device_create_file(dev,
  998. &vt1211_sysfs_in_min[i].dev_attr)) ||
  999. (err = device_create_file(dev,
  1000. &vt1211_sysfs_in_max[i].dev_attr)) ||
  1001. (err = device_create_file(dev,
  1002. &vt1211_sysfs_in_alarm[i].dev_attr))) {
  1003. goto EXIT_DEV_REMOVE;
  1004. }
  1005. }
  1006. }
  1007. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_temp_input); i++) {
  1008. if (ISTEMP(i, data->uch_config)) {
  1009. if ((err = device_create_file(dev,
  1010. &vt1211_sysfs_temp_input[i].dev_attr)) ||
  1011. (err = device_create_file(dev,
  1012. &vt1211_sysfs_temp_max[i].dev_attr)) ||
  1013. (err = device_create_file(dev,
  1014. &vt1211_sysfs_temp_max_hyst[i].dev_attr)) ||
  1015. (err = device_create_file(dev,
  1016. &vt1211_sysfs_temp_alarm[i].dev_attr))) {
  1017. goto EXIT_DEV_REMOVE;
  1018. }
  1019. }
  1020. }
  1021. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_fan_pwm); i++) {
  1022. err = device_create_file(dev,
  1023. &vt1211_sysfs_fan_pwm[i].dev_attr);
  1024. if (err) {
  1025. goto EXIT_DEV_REMOVE;
  1026. }
  1027. }
  1028. for (i = 0; i < ARRAY_SIZE(vt1211_sysfs_misc); i++) {
  1029. err = device_create_file(dev,
  1030. &vt1211_sysfs_misc[i]);
  1031. if (err) {
  1032. goto EXIT_DEV_REMOVE;
  1033. }
  1034. }
  1035. /* Register device */
  1036. data->class_dev = hwmon_device_register(dev);
  1037. if (IS_ERR(data->class_dev)) {
  1038. err = PTR_ERR(data->class_dev);
  1039. dev_err(dev, "Class registration failed (%d)\n", err);
  1040. goto EXIT_DEV_REMOVE_SILENT;
  1041. }
  1042. return 0;
  1043. EXIT_DEV_REMOVE:
  1044. dev_err(dev, "Sysfs interface creation failed (%d)\n", err);
  1045. EXIT_DEV_REMOVE_SILENT:
  1046. vt1211_remove_sysfs(pdev);
  1047. platform_set_drvdata(pdev, NULL);
  1048. kfree(data);
  1049. EXIT:
  1050. return err;
  1051. }
  1052. static int __devexit vt1211_remove(struct platform_device *pdev)
  1053. {
  1054. struct vt1211_data *data = platform_get_drvdata(pdev);
  1055. hwmon_device_unregister(data->class_dev);
  1056. vt1211_remove_sysfs(pdev);
  1057. platform_set_drvdata(pdev, NULL);
  1058. kfree(data);
  1059. return 0;
  1060. }
  1061. static struct platform_driver vt1211_driver = {
  1062. .driver = {
  1063. .owner = THIS_MODULE,
  1064. .name = DRVNAME,
  1065. },
  1066. .probe = vt1211_probe,
  1067. .remove = __devexit_p(vt1211_remove),
  1068. };
  1069. static int __init vt1211_device_add(unsigned short address)
  1070. {
  1071. struct resource res = {
  1072. .start = address,
  1073. .end = address + 0x7f,
  1074. .flags = IORESOURCE_IO,
  1075. };
  1076. int err;
  1077. pdev = platform_device_alloc(DRVNAME, address);
  1078. if (!pdev) {
  1079. err = -ENOMEM;
  1080. printk(KERN_ERR DRVNAME ": Device allocation failed (%d)\n",
  1081. err);
  1082. goto EXIT;
  1083. }
  1084. res.name = pdev->name;
  1085. err = platform_device_add_resources(pdev, &res, 1);
  1086. if (err) {
  1087. printk(KERN_ERR DRVNAME ": Device resource addition failed "
  1088. "(%d)\n", err);
  1089. goto EXIT_DEV_PUT;
  1090. }
  1091. err = platform_device_add(pdev);
  1092. if (err) {
  1093. printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n",
  1094. err);
  1095. goto EXIT_DEV_PUT;
  1096. }
  1097. return 0;
  1098. EXIT_DEV_PUT:
  1099. platform_device_put(pdev);
  1100. EXIT:
  1101. return err;
  1102. }
  1103. static int __init vt1211_find(int sio_cip, unsigned short *address)
  1104. {
  1105. int err = -ENODEV;
  1106. superio_enter(sio_cip);
  1107. if (superio_inb(sio_cip, SIO_VT1211_DEVID) != SIO_VT1211_ID) {
  1108. goto EXIT;
  1109. }
  1110. superio_select(sio_cip, SIO_VT1211_LDN_HWMON);
  1111. if ((superio_inb(sio_cip, SIO_VT1211_ACTIVE) & 1) == 0) {
  1112. printk(KERN_WARNING DRVNAME ": HW monitor is disabled, "
  1113. "skipping\n");
  1114. goto EXIT;
  1115. }
  1116. *address = ((superio_inb(sio_cip, SIO_VT1211_BADDR) << 8) |
  1117. (superio_inb(sio_cip, SIO_VT1211_BADDR + 1))) & 0xff00;
  1118. if (*address == 0) {
  1119. printk(KERN_WARNING DRVNAME ": Base address is not set, "
  1120. "skipping\n");
  1121. goto EXIT;
  1122. }
  1123. err = 0;
  1124. printk(KERN_INFO DRVNAME ": Found VT1211 chip at 0x%04x, "
  1125. "revision %u\n", *address,
  1126. superio_inb(sio_cip, SIO_VT1211_DEVREV));
  1127. EXIT:
  1128. superio_exit(sio_cip);
  1129. return err;
  1130. }
  1131. static int __init vt1211_init(void)
  1132. {
  1133. int err;
  1134. unsigned short address = 0;
  1135. if ((err = vt1211_find(SIO_REG_CIP1, &address)) &&
  1136. (err = vt1211_find(SIO_REG_CIP2, &address))) {
  1137. goto EXIT;
  1138. }
  1139. if ((uch_config < -1) || (uch_config > 31)) {
  1140. err = -EINVAL;
  1141. printk(KERN_WARNING DRVNAME ": Invalid UCH configuration %d. "
  1142. "Choose a value between 0 and 31.\n", uch_config);
  1143. goto EXIT;
  1144. }
  1145. if ((int_mode < -1) || (int_mode > 0)) {
  1146. err = -EINVAL;
  1147. printk(KERN_WARNING DRVNAME ": Invalid interrupt mode %d. "
  1148. "Only mode 0 is supported.\n", int_mode);
  1149. goto EXIT;
  1150. }
  1151. err = platform_driver_register(&vt1211_driver);
  1152. if (err) {
  1153. goto EXIT;
  1154. }
  1155. /* Sets global pdev as a side effect */
  1156. err = vt1211_device_add(address);
  1157. if (err) {
  1158. goto EXIT_DRV_UNREGISTER;
  1159. }
  1160. return 0;
  1161. EXIT_DRV_UNREGISTER:
  1162. platform_driver_unregister(&vt1211_driver);
  1163. EXIT:
  1164. return err;
  1165. }
  1166. static void __exit vt1211_exit(void)
  1167. {
  1168. platform_device_unregister(pdev);
  1169. platform_driver_unregister(&vt1211_driver);
  1170. }
  1171. MODULE_AUTHOR("Juerg Haefliger <juergh@gmail.com>");
  1172. MODULE_DESCRIPTION("VT1211 sensors");
  1173. MODULE_LICENSE("GPL");
  1174. module_init(vt1211_init);
  1175. module_exit(vt1211_exit);