vt1211.c 37 KB

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