vt1211.c 37 KB

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