vt1211.c 39 KB

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