vt1211.c 37 KB

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