vt1211.c 39 KB

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