vt1211.c 38 KB

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