vt1211.c 39 KB

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