vt1211.c 39 KB

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