lm85.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /*
  2. lm85.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
  5. Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
  6. Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
  7. Copyright (c) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
  8. Chip details at <http://www.national.com/ds/LM/LM85.pdf>
  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. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/i2c.h>
  26. #include <linux/hwmon.h>
  27. #include <linux/hwmon-vid.h>
  28. #include <linux/hwmon-sysfs.h>
  29. #include <linux/err.h>
  30. #include <linux/mutex.h>
  31. /* Addresses to scan */
  32. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  33. /* Insmod parameters */
  34. I2C_CLIENT_INSMOD_6(lm85b, lm85c, adm1027, adt7463, emc6d100, emc6d102);
  35. /* The LM85 registers */
  36. #define LM85_REG_IN(nr) (0x20 + (nr))
  37. #define LM85_REG_IN_MIN(nr) (0x44 + (nr) * 2)
  38. #define LM85_REG_IN_MAX(nr) (0x45 + (nr) * 2)
  39. #define LM85_REG_TEMP(nr) (0x25 + (nr))
  40. #define LM85_REG_TEMP_MIN(nr) (0x4e + (nr) * 2)
  41. #define LM85_REG_TEMP_MAX(nr) (0x4f + (nr) * 2)
  42. /* Fan speeds are LSB, MSB (2 bytes) */
  43. #define LM85_REG_FAN(nr) (0x28 + (nr) *2)
  44. #define LM85_REG_FAN_MIN(nr) (0x54 + (nr) *2)
  45. #define LM85_REG_PWM(nr) (0x30 + (nr))
  46. #define ADT7463_REG_OPPOINT(nr) (0x33 + (nr))
  47. #define ADT7463_REG_TMIN_CTL1 0x36
  48. #define ADT7463_REG_TMIN_CTL2 0x37
  49. #define LM85_REG_DEVICE 0x3d
  50. #define LM85_REG_COMPANY 0x3e
  51. #define LM85_REG_VERSTEP 0x3f
  52. /* These are the recognized values for the above regs */
  53. #define LM85_DEVICE_ADX 0x27
  54. #define LM85_COMPANY_NATIONAL 0x01
  55. #define LM85_COMPANY_ANALOG_DEV 0x41
  56. #define LM85_COMPANY_SMSC 0x5c
  57. #define LM85_VERSTEP_VMASK 0xf0
  58. #define LM85_VERSTEP_GENERIC 0x60
  59. #define LM85_VERSTEP_LM85C 0x60
  60. #define LM85_VERSTEP_LM85B 0x62
  61. #define LM85_VERSTEP_ADM1027 0x60
  62. #define LM85_VERSTEP_ADT7463 0x62
  63. #define LM85_VERSTEP_ADT7463C 0x6A
  64. #define LM85_VERSTEP_EMC6D100_A0 0x60
  65. #define LM85_VERSTEP_EMC6D100_A1 0x61
  66. #define LM85_VERSTEP_EMC6D102 0x65
  67. #define LM85_REG_CONFIG 0x40
  68. #define LM85_REG_ALARM1 0x41
  69. #define LM85_REG_ALARM2 0x42
  70. #define LM85_REG_VID 0x43
  71. /* Automated FAN control */
  72. #define LM85_REG_AFAN_CONFIG(nr) (0x5c + (nr))
  73. #define LM85_REG_AFAN_RANGE(nr) (0x5f + (nr))
  74. #define LM85_REG_AFAN_SPIKE1 0x62
  75. #define LM85_REG_AFAN_SPIKE2 0x63
  76. #define LM85_REG_AFAN_MINPWM(nr) (0x64 + (nr))
  77. #define LM85_REG_AFAN_LIMIT(nr) (0x67 + (nr))
  78. #define LM85_REG_AFAN_CRITICAL(nr) (0x6a + (nr))
  79. #define LM85_REG_AFAN_HYST1 0x6d
  80. #define LM85_REG_AFAN_HYST2 0x6e
  81. #define LM85_REG_TACH_MODE 0x74
  82. #define LM85_REG_SPINUP_CTL 0x75
  83. #define ADM1027_REG_TEMP_OFFSET(nr) (0x70 + (nr))
  84. #define ADM1027_REG_CONFIG2 0x73
  85. #define ADM1027_REG_INTMASK1 0x74
  86. #define ADM1027_REG_INTMASK2 0x75
  87. #define ADM1027_REG_EXTEND_ADC1 0x76
  88. #define ADM1027_REG_EXTEND_ADC2 0x77
  89. #define ADM1027_REG_CONFIG3 0x78
  90. #define ADM1027_REG_FAN_PPR 0x7b
  91. #define ADT7463_REG_THERM 0x79
  92. #define ADT7463_REG_THERM_LIMIT 0x7A
  93. #define EMC6D100_REG_ALARM3 0x7d
  94. /* IN5, IN6 and IN7 */
  95. #define EMC6D100_REG_IN(nr) (0x70 + ((nr)-5))
  96. #define EMC6D100_REG_IN_MIN(nr) (0x73 + ((nr)-5) * 2)
  97. #define EMC6D100_REG_IN_MAX(nr) (0x74 + ((nr)-5) * 2)
  98. #define EMC6D102_REG_EXTEND_ADC1 0x85
  99. #define EMC6D102_REG_EXTEND_ADC2 0x86
  100. #define EMC6D102_REG_EXTEND_ADC3 0x87
  101. #define EMC6D102_REG_EXTEND_ADC4 0x88
  102. /* Conversions. Rounding and limit checking is only done on the TO_REG
  103. variants. Note that you should be a bit careful with which arguments
  104. these macros are called: arguments may be evaluated more than once.
  105. */
  106. /* IN are scaled acording to built-in resistors */
  107. static int lm85_scaling[] = { /* .001 Volts */
  108. 2500, 2250, 3300, 5000, 12000,
  109. 3300, 1500, 1800 /*EMC6D100*/
  110. };
  111. #define SCALE(val,from,to) (((val)*(to) + ((from)/2))/(from))
  112. #define INS_TO_REG(n,val) \
  113. SENSORS_LIMIT(SCALE(val,lm85_scaling[n],192),0,255)
  114. #define INSEXT_FROM_REG(n,val,ext) \
  115. SCALE(((val) << 4) + (ext), 192 << 4, lm85_scaling[n])
  116. #define INS_FROM_REG(n,val) SCALE((val), 192, lm85_scaling[n])
  117. /* FAN speed is measured using 90kHz clock */
  118. static inline u16 FAN_TO_REG(unsigned long val)
  119. {
  120. if (!val)
  121. return 0xffff;
  122. return SENSORS_LIMIT(5400000 / val, 1, 0xfffe);
  123. }
  124. #define FAN_FROM_REG(val) ((val)==0?-1:(val)==0xffff?0:5400000/(val))
  125. /* Temperature is reported in .001 degC increments */
  126. #define TEMP_TO_REG(val) \
  127. SENSORS_LIMIT(SCALE(val,1000,1),-127,127)
  128. #define TEMPEXT_FROM_REG(val,ext) \
  129. SCALE(((val) << 4) + (ext), 16, 1000)
  130. #define TEMP_FROM_REG(val) ((val) * 1000)
  131. #define PWM_TO_REG(val) (SENSORS_LIMIT(val,0,255))
  132. #define PWM_FROM_REG(val) (val)
  133. /* ZONEs have the following parameters:
  134. * Limit (low) temp, 1. degC
  135. * Hysteresis (below limit), 1. degC (0-15)
  136. * Range of speed control, .1 degC (2-80)
  137. * Critical (high) temp, 1. degC
  138. *
  139. * FAN PWMs have the following parameters:
  140. * Reference Zone, 1, 2, 3, etc.
  141. * Spinup time, .05 sec
  142. * PWM value at limit/low temp, 1 count
  143. * PWM Frequency, 1. Hz
  144. * PWM is Min or OFF below limit, flag
  145. * Invert PWM output, flag
  146. *
  147. * Some chips filter the temp, others the fan.
  148. * Filter constant (or disabled) .1 seconds
  149. */
  150. /* These are the zone temperature range encodings in .001 degree C */
  151. static int lm85_range_map[] = {
  152. 2000, 2500, 3300, 4000, 5000, 6600,
  153. 8000, 10000, 13300, 16000, 20000, 26600,
  154. 32000, 40000, 53300, 80000
  155. };
  156. static int RANGE_TO_REG( int range )
  157. {
  158. int i;
  159. if ( range < lm85_range_map[0] ) {
  160. return 0 ;
  161. } else if ( range > lm85_range_map[15] ) {
  162. return 15 ;
  163. } else { /* find closest match */
  164. for ( i = 14 ; i >= 0 ; --i ) {
  165. if ( range > lm85_range_map[i] ) { /* range bracketed */
  166. if ((lm85_range_map[i+1] - range) <
  167. (range - lm85_range_map[i])) {
  168. i++;
  169. break;
  170. }
  171. break;
  172. }
  173. }
  174. }
  175. return( i & 0x0f );
  176. }
  177. #define RANGE_FROM_REG(val) (lm85_range_map[(val)&0x0f])
  178. /* These are the Acoustic Enhancement, or Temperature smoothing encodings
  179. * NOTE: The enable/disable bit is INCLUDED in these encodings as the
  180. * MSB (bit 3, value 8). If the enable bit is 0, the encoded value
  181. * is ignored, or set to 0.
  182. */
  183. /* These are the PWM frequency encodings */
  184. static int lm85_freq_map[] = { /* .1 Hz */
  185. 100, 150, 230, 300, 380, 470, 620, 940
  186. };
  187. static int FREQ_TO_REG( int freq )
  188. {
  189. int i;
  190. if( freq >= lm85_freq_map[7] ) { return 7 ; }
  191. for( i = 0 ; i < 7 ; ++i )
  192. if( freq <= lm85_freq_map[i] )
  193. break ;
  194. return( i & 0x07 );
  195. }
  196. #define FREQ_FROM_REG(val) (lm85_freq_map[(val)&0x07])
  197. /* Since we can't use strings, I'm abusing these numbers
  198. * to stand in for the following meanings:
  199. * 1 -- PWM responds to Zone 1
  200. * 2 -- PWM responds to Zone 2
  201. * 3 -- PWM responds to Zone 3
  202. * 23 -- PWM responds to the higher temp of Zone 2 or 3
  203. * 123 -- PWM responds to highest of Zone 1, 2, or 3
  204. * 0 -- PWM is always at 0% (ie, off)
  205. * -1 -- PWM is always at 100%
  206. * -2 -- PWM responds to manual control
  207. */
  208. static int lm85_zone_map[] = { 1, 2, 3, -1, 0, 23, 123, -2 };
  209. #define ZONE_FROM_REG(val) (lm85_zone_map[((val)>>5)&0x07])
  210. static int ZONE_TO_REG( int zone )
  211. {
  212. int i;
  213. for( i = 0 ; i <= 7 ; ++i )
  214. if( zone == lm85_zone_map[i] )
  215. break ;
  216. if( i > 7 ) /* Not found. */
  217. i = 3; /* Always 100% */
  218. return( (i & 0x07)<<5 );
  219. }
  220. #define HYST_TO_REG(val) (SENSORS_LIMIT(((val)+500)/1000,0,15))
  221. #define HYST_FROM_REG(val) ((val)*1000)
  222. #define OFFSET_TO_REG(val) (SENSORS_LIMIT((val)/25,-127,127))
  223. #define OFFSET_FROM_REG(val) ((val)*25)
  224. #define PPR_MASK(fan) (0x03<<(fan *2))
  225. #define PPR_TO_REG(val,fan) (SENSORS_LIMIT((val)-1,0,3)<<(fan *2))
  226. #define PPR_FROM_REG(val,fan) ((((val)>>(fan * 2))&0x03)+1)
  227. /* Chip sampling rates
  228. *
  229. * Some sensors are not updated more frequently than once per second
  230. * so it doesn't make sense to read them more often than that.
  231. * We cache the results and return the saved data if the driver
  232. * is called again before a second has elapsed.
  233. *
  234. * Also, there is significant configuration data for this chip
  235. * given the automatic PWM fan control that is possible. There
  236. * are about 47 bytes of config data to only 22 bytes of actual
  237. * readings. So, we keep the config data up to date in the cache
  238. * when it is written and only sample it once every 1 *minute*
  239. */
  240. #define LM85_DATA_INTERVAL (HZ + HZ / 2)
  241. #define LM85_CONFIG_INTERVAL (1 * 60 * HZ)
  242. /* LM85 can automatically adjust fan speeds based on temperature
  243. * This structure encapsulates an entire Zone config. There are
  244. * three zones (one for each temperature input) on the lm85
  245. */
  246. struct lm85_zone {
  247. s8 limit; /* Low temp limit */
  248. u8 hyst; /* Low limit hysteresis. (0-15) */
  249. u8 range; /* Temp range, encoded */
  250. s8 critical; /* "All fans ON" temp limit */
  251. u8 off_desired; /* Actual "off" temperature specified. Preserved
  252. * to prevent "drift" as other autofan control
  253. * values change.
  254. */
  255. u8 max_desired; /* Actual "max" temperature specified. Preserved
  256. * to prevent "drift" as other autofan control
  257. * values change.
  258. */
  259. };
  260. struct lm85_autofan {
  261. u8 config; /* Register value */
  262. u8 freq; /* PWM frequency, encoded */
  263. u8 min_pwm; /* Minimum PWM value, encoded */
  264. u8 min_off; /* Min PWM or OFF below "limit", flag */
  265. };
  266. /* For each registered chip, we need to keep some data in memory.
  267. The structure is dynamically allocated. */
  268. struct lm85_data {
  269. struct i2c_client client;
  270. struct device *hwmon_dev;
  271. enum chips type;
  272. struct mutex update_lock;
  273. int valid; /* !=0 if following fields are valid */
  274. unsigned long last_reading; /* In jiffies */
  275. unsigned long last_config; /* In jiffies */
  276. u8 in[8]; /* Register value */
  277. u8 in_max[8]; /* Register value */
  278. u8 in_min[8]; /* Register value */
  279. s8 temp[3]; /* Register value */
  280. s8 temp_min[3]; /* Register value */
  281. s8 temp_max[3]; /* Register value */
  282. s8 temp_offset[3]; /* Register value */
  283. u16 fan[4]; /* Register value */
  284. u16 fan_min[4]; /* Register value */
  285. u8 pwm[3]; /* Register value */
  286. u8 spinup_ctl; /* Register encoding, combined */
  287. u8 tach_mode; /* Register encoding, combined */
  288. u8 temp_ext[3]; /* Decoded values */
  289. u8 in_ext[8]; /* Decoded values */
  290. u8 fan_ppr; /* Register value */
  291. u8 smooth[3]; /* Register encoding */
  292. u8 vid; /* Register value */
  293. u8 vrm; /* VRM version */
  294. u8 syncpwm3; /* Saved PWM3 for TACH 2,3,4 config */
  295. u8 oppoint[3]; /* Register value */
  296. u16 tmin_ctl; /* Register value */
  297. unsigned long therm_total; /* Cummulative therm count */
  298. u8 therm_limit; /* Register value */
  299. u32 alarms; /* Register encoding, combined */
  300. struct lm85_autofan autofan[3];
  301. struct lm85_zone zone[3];
  302. };
  303. static int lm85_attach_adapter(struct i2c_adapter *adapter);
  304. static int lm85_detect(struct i2c_adapter *adapter, int address,
  305. int kind);
  306. static int lm85_detach_client(struct i2c_client *client);
  307. static int lm85_read_value(struct i2c_client *client, u8 reg);
  308. static int lm85_write_value(struct i2c_client *client, u8 reg, int value);
  309. static struct lm85_data *lm85_update_device(struct device *dev);
  310. static void lm85_init_client(struct i2c_client *client);
  311. static struct i2c_driver lm85_driver = {
  312. .driver = {
  313. .name = "lm85",
  314. },
  315. .attach_adapter = lm85_attach_adapter,
  316. .detach_client = lm85_detach_client,
  317. };
  318. /* 4 Fans */
  319. static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
  320. char *buf)
  321. {
  322. int nr = to_sensor_dev_attr(attr)->index;
  323. struct lm85_data *data = lm85_update_device(dev);
  324. return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan[nr]) );
  325. }
  326. static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
  327. char *buf)
  328. {
  329. int nr = to_sensor_dev_attr(attr)->index;
  330. struct lm85_data *data = lm85_update_device(dev);
  331. return sprintf(buf,"%d\n", FAN_FROM_REG(data->fan_min[nr]) );
  332. }
  333. static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
  334. const char *buf, size_t count)
  335. {
  336. int nr = to_sensor_dev_attr(attr)->index;
  337. struct i2c_client *client = to_i2c_client(dev);
  338. struct lm85_data *data = i2c_get_clientdata(client);
  339. unsigned long val = simple_strtoul(buf, NULL, 10);
  340. mutex_lock(&data->update_lock);
  341. data->fan_min[nr] = FAN_TO_REG(val);
  342. lm85_write_value(client, LM85_REG_FAN_MIN(nr), data->fan_min[nr]);
  343. mutex_unlock(&data->update_lock);
  344. return count;
  345. }
  346. #define show_fan_offset(offset) \
  347. static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  348. show_fan, NULL, offset - 1); \
  349. static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  350. show_fan_min, set_fan_min, offset - 1)
  351. show_fan_offset(1);
  352. show_fan_offset(2);
  353. show_fan_offset(3);
  354. show_fan_offset(4);
  355. /* vid, vrm, alarms */
  356. static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
  357. {
  358. struct lm85_data *data = lm85_update_device(dev);
  359. int vid;
  360. if (data->type == adt7463 && (data->vid & 0x80)) {
  361. /* 6-pin VID (VRM 10) */
  362. vid = vid_from_reg(data->vid & 0x3f, data->vrm);
  363. } else {
  364. /* 5-pin VID (VRM 9) */
  365. vid = vid_from_reg(data->vid & 0x1f, data->vrm);
  366. }
  367. return sprintf(buf, "%d\n", vid);
  368. }
  369. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
  370. static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
  371. {
  372. struct lm85_data *data = dev_get_drvdata(dev);
  373. return sprintf(buf, "%ld\n", (long) data->vrm);
  374. }
  375. static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  376. {
  377. struct lm85_data *data = dev_get_drvdata(dev);
  378. data->vrm = simple_strtoul(buf, NULL, 10);
  379. return count;
  380. }
  381. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
  382. static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
  383. {
  384. struct lm85_data *data = lm85_update_device(dev);
  385. return sprintf(buf, "%u\n", data->alarms);
  386. }
  387. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
  388. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  389. char *buf)
  390. {
  391. int nr = to_sensor_dev_attr(attr)->index;
  392. struct lm85_data *data = lm85_update_device(dev);
  393. return sprintf(buf, "%u\n", (data->alarms >> nr) & 1);
  394. }
  395. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  396. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  397. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  398. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  399. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  400. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 18);
  401. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 16);
  402. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 17);
  403. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  404. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
  405. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  406. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 6);
  407. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
  408. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 10);
  409. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 11);
  410. static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 12);
  411. static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 13);
  412. /* pwm */
  413. static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
  414. char *buf)
  415. {
  416. int nr = to_sensor_dev_attr(attr)->index;
  417. struct lm85_data *data = lm85_update_device(dev);
  418. return sprintf(buf,"%d\n", PWM_FROM_REG(data->pwm[nr]) );
  419. }
  420. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  421. const char *buf, size_t count)
  422. {
  423. int nr = to_sensor_dev_attr(attr)->index;
  424. struct i2c_client *client = to_i2c_client(dev);
  425. struct lm85_data *data = i2c_get_clientdata(client);
  426. long val = simple_strtol(buf, NULL, 10);
  427. mutex_lock(&data->update_lock);
  428. data->pwm[nr] = PWM_TO_REG(val);
  429. lm85_write_value(client, LM85_REG_PWM(nr), data->pwm[nr]);
  430. mutex_unlock(&data->update_lock);
  431. return count;
  432. }
  433. static ssize_t show_pwm_enable(struct device *dev, struct device_attribute
  434. *attr, char *buf)
  435. {
  436. int nr = to_sensor_dev_attr(attr)->index;
  437. struct lm85_data *data = lm85_update_device(dev);
  438. int pwm_zone, enable;
  439. pwm_zone = ZONE_FROM_REG(data->autofan[nr].config);
  440. switch (pwm_zone) {
  441. case -1: /* PWM is always at 100% */
  442. enable = 0;
  443. break;
  444. case 0: /* PWM is always at 0% */
  445. case -2: /* PWM responds to manual control */
  446. enable = 1;
  447. break;
  448. default: /* PWM in automatic mode */
  449. enable = 2;
  450. }
  451. return sprintf(buf, "%d\n", enable);
  452. }
  453. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute
  454. *attr, const char *buf, size_t count)
  455. {
  456. int nr = to_sensor_dev_attr(attr)->index;
  457. struct i2c_client *client = to_i2c_client(dev);
  458. struct lm85_data *data = i2c_get_clientdata(client);
  459. long val = simple_strtol(buf, NULL, 10);
  460. u8 config;
  461. switch (val) {
  462. case 0:
  463. config = 3;
  464. break;
  465. case 1:
  466. config = 7;
  467. break;
  468. case 2:
  469. /* Here we have to choose arbitrarily one of the 5 possible
  470. configurations; I go for the safest */
  471. config = 6;
  472. break;
  473. default:
  474. return -EINVAL;
  475. }
  476. mutex_lock(&data->update_lock);
  477. data->autofan[nr].config = lm85_read_value(client,
  478. LM85_REG_AFAN_CONFIG(nr));
  479. data->autofan[nr].config = (data->autofan[nr].config & ~0xe0)
  480. | (config << 5);
  481. lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
  482. data->autofan[nr].config);
  483. mutex_unlock(&data->update_lock);
  484. return count;
  485. }
  486. #define show_pwm_reg(offset) \
  487. static SENSOR_DEVICE_ATTR(pwm##offset, S_IRUGO | S_IWUSR, \
  488. show_pwm, set_pwm, offset - 1); \
  489. static SENSOR_DEVICE_ATTR(pwm##offset##_enable, S_IRUGO | S_IWUSR, \
  490. show_pwm_enable, set_pwm_enable, offset - 1)
  491. show_pwm_reg(1);
  492. show_pwm_reg(2);
  493. show_pwm_reg(3);
  494. /* Voltages */
  495. static ssize_t show_in(struct device *dev, struct device_attribute *attr,
  496. char *buf)
  497. {
  498. int nr = to_sensor_dev_attr(attr)->index;
  499. struct lm85_data *data = lm85_update_device(dev);
  500. return sprintf( buf, "%d\n", INSEXT_FROM_REG(nr,
  501. data->in[nr],
  502. data->in_ext[nr]));
  503. }
  504. static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
  505. char *buf)
  506. {
  507. int nr = to_sensor_dev_attr(attr)->index;
  508. struct lm85_data *data = lm85_update_device(dev);
  509. return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_min[nr]) );
  510. }
  511. static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
  512. const char *buf, size_t count)
  513. {
  514. int nr = to_sensor_dev_attr(attr)->index;
  515. struct i2c_client *client = to_i2c_client(dev);
  516. struct lm85_data *data = i2c_get_clientdata(client);
  517. long val = simple_strtol(buf, NULL, 10);
  518. mutex_lock(&data->update_lock);
  519. data->in_min[nr] = INS_TO_REG(nr, val);
  520. lm85_write_value(client, LM85_REG_IN_MIN(nr), data->in_min[nr]);
  521. mutex_unlock(&data->update_lock);
  522. return count;
  523. }
  524. static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
  525. char *buf)
  526. {
  527. int nr = to_sensor_dev_attr(attr)->index;
  528. struct lm85_data *data = lm85_update_device(dev);
  529. return sprintf(buf,"%d\n", INS_FROM_REG(nr, data->in_max[nr]) );
  530. }
  531. static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
  532. const char *buf, size_t count)
  533. {
  534. int nr = to_sensor_dev_attr(attr)->index;
  535. struct i2c_client *client = to_i2c_client(dev);
  536. struct lm85_data *data = i2c_get_clientdata(client);
  537. long val = simple_strtol(buf, NULL, 10);
  538. mutex_lock(&data->update_lock);
  539. data->in_max[nr] = INS_TO_REG(nr, val);
  540. lm85_write_value(client, LM85_REG_IN_MAX(nr), data->in_max[nr]);
  541. mutex_unlock(&data->update_lock);
  542. return count;
  543. }
  544. #define show_in_reg(offset) \
  545. static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  546. show_in, NULL, offset); \
  547. static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  548. show_in_min, set_in_min, offset); \
  549. static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  550. show_in_max, set_in_max, offset)
  551. show_in_reg(0);
  552. show_in_reg(1);
  553. show_in_reg(2);
  554. show_in_reg(3);
  555. show_in_reg(4);
  556. show_in_reg(5);
  557. show_in_reg(6);
  558. show_in_reg(7);
  559. /* Temps */
  560. static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
  561. char *buf)
  562. {
  563. int nr = to_sensor_dev_attr(attr)->index;
  564. struct lm85_data *data = lm85_update_device(dev);
  565. return sprintf(buf,"%d\n", TEMPEXT_FROM_REG(data->temp[nr],
  566. data->temp_ext[nr]));
  567. }
  568. static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
  569. char *buf)
  570. {
  571. int nr = to_sensor_dev_attr(attr)->index;
  572. struct lm85_data *data = lm85_update_device(dev);
  573. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_min[nr]) );
  574. }
  575. static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
  576. const char *buf, size_t count)
  577. {
  578. int nr = to_sensor_dev_attr(attr)->index;
  579. struct i2c_client *client = to_i2c_client(dev);
  580. struct lm85_data *data = i2c_get_clientdata(client);
  581. long val = simple_strtol(buf, NULL, 10);
  582. mutex_lock(&data->update_lock);
  583. data->temp_min[nr] = TEMP_TO_REG(val);
  584. lm85_write_value(client, LM85_REG_TEMP_MIN(nr), data->temp_min[nr]);
  585. mutex_unlock(&data->update_lock);
  586. return count;
  587. }
  588. static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
  589. char *buf)
  590. {
  591. int nr = to_sensor_dev_attr(attr)->index;
  592. struct lm85_data *data = lm85_update_device(dev);
  593. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->temp_max[nr]) );
  594. }
  595. static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
  596. const char *buf, size_t count)
  597. {
  598. int nr = to_sensor_dev_attr(attr)->index;
  599. struct i2c_client *client = to_i2c_client(dev);
  600. struct lm85_data *data = i2c_get_clientdata(client);
  601. long val = simple_strtol(buf, NULL, 10);
  602. mutex_lock(&data->update_lock);
  603. data->temp_max[nr] = TEMP_TO_REG(val);
  604. lm85_write_value(client, LM85_REG_TEMP_MAX(nr), data->temp_max[nr]);
  605. mutex_unlock(&data->update_lock);
  606. return count;
  607. }
  608. #define show_temp_reg(offset) \
  609. static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  610. show_temp, NULL, offset - 1); \
  611. static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  612. show_temp_min, set_temp_min, offset - 1); \
  613. static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  614. show_temp_max, set_temp_max, offset - 1);
  615. show_temp_reg(1);
  616. show_temp_reg(2);
  617. show_temp_reg(3);
  618. /* Automatic PWM control */
  619. static ssize_t show_pwm_auto_channels(struct device *dev,
  620. struct device_attribute *attr, char *buf)
  621. {
  622. int nr = to_sensor_dev_attr(attr)->index;
  623. struct lm85_data *data = lm85_update_device(dev);
  624. return sprintf(buf,"%d\n", ZONE_FROM_REG(data->autofan[nr].config));
  625. }
  626. static ssize_t set_pwm_auto_channels(struct device *dev,
  627. struct device_attribute *attr, const char *buf, size_t count)
  628. {
  629. int nr = to_sensor_dev_attr(attr)->index;
  630. struct i2c_client *client = to_i2c_client(dev);
  631. struct lm85_data *data = i2c_get_clientdata(client);
  632. long val = simple_strtol(buf, NULL, 10);
  633. mutex_lock(&data->update_lock);
  634. data->autofan[nr].config = (data->autofan[nr].config & (~0xe0))
  635. | ZONE_TO_REG(val) ;
  636. lm85_write_value(client, LM85_REG_AFAN_CONFIG(nr),
  637. data->autofan[nr].config);
  638. mutex_unlock(&data->update_lock);
  639. return count;
  640. }
  641. static ssize_t show_pwm_auto_pwm_min(struct device *dev,
  642. struct device_attribute *attr, char *buf)
  643. {
  644. int nr = to_sensor_dev_attr(attr)->index;
  645. struct lm85_data *data = lm85_update_device(dev);
  646. return sprintf(buf,"%d\n", PWM_FROM_REG(data->autofan[nr].min_pwm));
  647. }
  648. static ssize_t set_pwm_auto_pwm_min(struct device *dev,
  649. struct device_attribute *attr, const char *buf, size_t count)
  650. {
  651. int nr = to_sensor_dev_attr(attr)->index;
  652. struct i2c_client *client = to_i2c_client(dev);
  653. struct lm85_data *data = i2c_get_clientdata(client);
  654. long val = simple_strtol(buf, NULL, 10);
  655. mutex_lock(&data->update_lock);
  656. data->autofan[nr].min_pwm = PWM_TO_REG(val);
  657. lm85_write_value(client, LM85_REG_AFAN_MINPWM(nr),
  658. data->autofan[nr].min_pwm);
  659. mutex_unlock(&data->update_lock);
  660. return count;
  661. }
  662. static ssize_t show_pwm_auto_pwm_minctl(struct device *dev,
  663. struct device_attribute *attr, char *buf)
  664. {
  665. int nr = to_sensor_dev_attr(attr)->index;
  666. struct lm85_data *data = lm85_update_device(dev);
  667. return sprintf(buf,"%d\n", data->autofan[nr].min_off);
  668. }
  669. static ssize_t set_pwm_auto_pwm_minctl(struct device *dev,
  670. struct device_attribute *attr, const char *buf, size_t count)
  671. {
  672. int nr = to_sensor_dev_attr(attr)->index;
  673. struct i2c_client *client = to_i2c_client(dev);
  674. struct lm85_data *data = i2c_get_clientdata(client);
  675. long val = simple_strtol(buf, NULL, 10);
  676. mutex_lock(&data->update_lock);
  677. data->autofan[nr].min_off = val;
  678. lm85_write_value(client, LM85_REG_AFAN_SPIKE1, data->smooth[0]
  679. | data->syncpwm3
  680. | (data->autofan[0].min_off ? 0x20 : 0)
  681. | (data->autofan[1].min_off ? 0x40 : 0)
  682. | (data->autofan[2].min_off ? 0x80 : 0)
  683. );
  684. mutex_unlock(&data->update_lock);
  685. return count;
  686. }
  687. static ssize_t show_pwm_auto_pwm_freq(struct device *dev,
  688. struct device_attribute *attr, char *buf)
  689. {
  690. int nr = to_sensor_dev_attr(attr)->index;
  691. struct lm85_data *data = lm85_update_device(dev);
  692. return sprintf(buf,"%d\n", FREQ_FROM_REG(data->autofan[nr].freq));
  693. }
  694. static ssize_t set_pwm_auto_pwm_freq(struct device *dev,
  695. struct device_attribute *attr, const char *buf, size_t count)
  696. {
  697. int nr = to_sensor_dev_attr(attr)->index;
  698. struct i2c_client *client = to_i2c_client(dev);
  699. struct lm85_data *data = i2c_get_clientdata(client);
  700. long val = simple_strtol(buf, NULL, 10);
  701. mutex_lock(&data->update_lock);
  702. data->autofan[nr].freq = FREQ_TO_REG(val);
  703. lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
  704. (data->zone[nr].range << 4)
  705. | data->autofan[nr].freq
  706. );
  707. mutex_unlock(&data->update_lock);
  708. return count;
  709. }
  710. #define pwm_auto(offset) \
  711. static SENSOR_DEVICE_ATTR(pwm##offset##_auto_channels, \
  712. S_IRUGO | S_IWUSR, show_pwm_auto_channels, \
  713. set_pwm_auto_channels, offset - 1); \
  714. static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_min, \
  715. S_IRUGO | S_IWUSR, show_pwm_auto_pwm_min, \
  716. set_pwm_auto_pwm_min, offset - 1); \
  717. static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_minctl, \
  718. S_IRUGO | S_IWUSR, show_pwm_auto_pwm_minctl, \
  719. set_pwm_auto_pwm_minctl, offset - 1); \
  720. static SENSOR_DEVICE_ATTR(pwm##offset##_auto_pwm_freq, \
  721. S_IRUGO | S_IWUSR, show_pwm_auto_pwm_freq, \
  722. set_pwm_auto_pwm_freq, offset - 1);
  723. pwm_auto(1);
  724. pwm_auto(2);
  725. pwm_auto(3);
  726. /* Temperature settings for automatic PWM control */
  727. static ssize_t show_temp_auto_temp_off(struct device *dev,
  728. struct device_attribute *attr, char *buf)
  729. {
  730. int nr = to_sensor_dev_attr(attr)->index;
  731. struct lm85_data *data = lm85_update_device(dev);
  732. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) -
  733. HYST_FROM_REG(data->zone[nr].hyst));
  734. }
  735. static ssize_t set_temp_auto_temp_off(struct device *dev,
  736. struct device_attribute *attr, const char *buf, size_t count)
  737. {
  738. int nr = to_sensor_dev_attr(attr)->index;
  739. struct i2c_client *client = to_i2c_client(dev);
  740. struct lm85_data *data = i2c_get_clientdata(client);
  741. int min;
  742. long val = simple_strtol(buf, NULL, 10);
  743. mutex_lock(&data->update_lock);
  744. min = TEMP_FROM_REG(data->zone[nr].limit);
  745. data->zone[nr].off_desired = TEMP_TO_REG(val);
  746. data->zone[nr].hyst = HYST_TO_REG(min - val);
  747. if ( nr == 0 || nr == 1 ) {
  748. lm85_write_value(client, LM85_REG_AFAN_HYST1,
  749. (data->zone[0].hyst << 4)
  750. | data->zone[1].hyst
  751. );
  752. } else {
  753. lm85_write_value(client, LM85_REG_AFAN_HYST2,
  754. (data->zone[2].hyst << 4)
  755. );
  756. }
  757. mutex_unlock(&data->update_lock);
  758. return count;
  759. }
  760. static ssize_t show_temp_auto_temp_min(struct device *dev,
  761. struct device_attribute *attr, char *buf)
  762. {
  763. int nr = to_sensor_dev_attr(attr)->index;
  764. struct lm85_data *data = lm85_update_device(dev);
  765. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) );
  766. }
  767. static ssize_t set_temp_auto_temp_min(struct device *dev,
  768. struct device_attribute *attr, const char *buf, size_t count)
  769. {
  770. int nr = to_sensor_dev_attr(attr)->index;
  771. struct i2c_client *client = to_i2c_client(dev);
  772. struct lm85_data *data = i2c_get_clientdata(client);
  773. long val = simple_strtol(buf, NULL, 10);
  774. mutex_lock(&data->update_lock);
  775. data->zone[nr].limit = TEMP_TO_REG(val);
  776. lm85_write_value(client, LM85_REG_AFAN_LIMIT(nr),
  777. data->zone[nr].limit);
  778. /* Update temp_auto_max and temp_auto_range */
  779. data->zone[nr].range = RANGE_TO_REG(
  780. TEMP_FROM_REG(data->zone[nr].max_desired) -
  781. TEMP_FROM_REG(data->zone[nr].limit));
  782. lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
  783. ((data->zone[nr].range & 0x0f) << 4)
  784. | (data->autofan[nr].freq & 0x07));
  785. /* Update temp_auto_hyst and temp_auto_off */
  786. data->zone[nr].hyst = HYST_TO_REG(TEMP_FROM_REG(
  787. data->zone[nr].limit) - TEMP_FROM_REG(
  788. data->zone[nr].off_desired));
  789. if ( nr == 0 || nr == 1 ) {
  790. lm85_write_value(client, LM85_REG_AFAN_HYST1,
  791. (data->zone[0].hyst << 4)
  792. | data->zone[1].hyst
  793. );
  794. } else {
  795. lm85_write_value(client, LM85_REG_AFAN_HYST2,
  796. (data->zone[2].hyst << 4)
  797. );
  798. }
  799. mutex_unlock(&data->update_lock);
  800. return count;
  801. }
  802. static ssize_t show_temp_auto_temp_max(struct device *dev,
  803. struct device_attribute *attr, char *buf)
  804. {
  805. int nr = to_sensor_dev_attr(attr)->index;
  806. struct lm85_data *data = lm85_update_device(dev);
  807. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].limit) +
  808. RANGE_FROM_REG(data->zone[nr].range));
  809. }
  810. static ssize_t set_temp_auto_temp_max(struct device *dev,
  811. struct device_attribute *attr, const char *buf, size_t count)
  812. {
  813. int nr = to_sensor_dev_attr(attr)->index;
  814. struct i2c_client *client = to_i2c_client(dev);
  815. struct lm85_data *data = i2c_get_clientdata(client);
  816. int min;
  817. long val = simple_strtol(buf, NULL, 10);
  818. mutex_lock(&data->update_lock);
  819. min = TEMP_FROM_REG(data->zone[nr].limit);
  820. data->zone[nr].max_desired = TEMP_TO_REG(val);
  821. data->zone[nr].range = RANGE_TO_REG(
  822. val - min);
  823. lm85_write_value(client, LM85_REG_AFAN_RANGE(nr),
  824. ((data->zone[nr].range & 0x0f) << 4)
  825. | (data->autofan[nr].freq & 0x07));
  826. mutex_unlock(&data->update_lock);
  827. return count;
  828. }
  829. static ssize_t show_temp_auto_temp_crit(struct device *dev,
  830. struct device_attribute *attr, char *buf)
  831. {
  832. int nr = to_sensor_dev_attr(attr)->index;
  833. struct lm85_data *data = lm85_update_device(dev);
  834. return sprintf(buf,"%d\n", TEMP_FROM_REG(data->zone[nr].critical));
  835. }
  836. static ssize_t set_temp_auto_temp_crit(struct device *dev,
  837. struct device_attribute *attr,const char *buf, size_t count)
  838. {
  839. int nr = to_sensor_dev_attr(attr)->index;
  840. struct i2c_client *client = to_i2c_client(dev);
  841. struct lm85_data *data = i2c_get_clientdata(client);
  842. long val = simple_strtol(buf, NULL, 10);
  843. mutex_lock(&data->update_lock);
  844. data->zone[nr].critical = TEMP_TO_REG(val);
  845. lm85_write_value(client, LM85_REG_AFAN_CRITICAL(nr),
  846. data->zone[nr].critical);
  847. mutex_unlock(&data->update_lock);
  848. return count;
  849. }
  850. #define temp_auto(offset) \
  851. static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_off, \
  852. S_IRUGO | S_IWUSR, show_temp_auto_temp_off, \
  853. set_temp_auto_temp_off, offset - 1); \
  854. static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_min, \
  855. S_IRUGO | S_IWUSR, show_temp_auto_temp_min, \
  856. set_temp_auto_temp_min, offset - 1); \
  857. static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_max, \
  858. S_IRUGO | S_IWUSR, show_temp_auto_temp_max, \
  859. set_temp_auto_temp_max, offset - 1); \
  860. static SENSOR_DEVICE_ATTR(temp##offset##_auto_temp_crit, \
  861. S_IRUGO | S_IWUSR, show_temp_auto_temp_crit, \
  862. set_temp_auto_temp_crit, offset - 1);
  863. temp_auto(1);
  864. temp_auto(2);
  865. temp_auto(3);
  866. static int lm85_attach_adapter(struct i2c_adapter *adapter)
  867. {
  868. if (!(adapter->class & I2C_CLASS_HWMON))
  869. return 0;
  870. return i2c_probe(adapter, &addr_data, lm85_detect);
  871. }
  872. static struct attribute *lm85_attributes[] = {
  873. &sensor_dev_attr_fan1_input.dev_attr.attr,
  874. &sensor_dev_attr_fan2_input.dev_attr.attr,
  875. &sensor_dev_attr_fan3_input.dev_attr.attr,
  876. &sensor_dev_attr_fan4_input.dev_attr.attr,
  877. &sensor_dev_attr_fan1_min.dev_attr.attr,
  878. &sensor_dev_attr_fan2_min.dev_attr.attr,
  879. &sensor_dev_attr_fan3_min.dev_attr.attr,
  880. &sensor_dev_attr_fan4_min.dev_attr.attr,
  881. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  882. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  883. &sensor_dev_attr_fan3_alarm.dev_attr.attr,
  884. &sensor_dev_attr_fan4_alarm.dev_attr.attr,
  885. &sensor_dev_attr_pwm1.dev_attr.attr,
  886. &sensor_dev_attr_pwm2.dev_attr.attr,
  887. &sensor_dev_attr_pwm3.dev_attr.attr,
  888. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  889. &sensor_dev_attr_pwm2_enable.dev_attr.attr,
  890. &sensor_dev_attr_pwm3_enable.dev_attr.attr,
  891. &sensor_dev_attr_in0_input.dev_attr.attr,
  892. &sensor_dev_attr_in1_input.dev_attr.attr,
  893. &sensor_dev_attr_in2_input.dev_attr.attr,
  894. &sensor_dev_attr_in3_input.dev_attr.attr,
  895. &sensor_dev_attr_in0_min.dev_attr.attr,
  896. &sensor_dev_attr_in1_min.dev_attr.attr,
  897. &sensor_dev_attr_in2_min.dev_attr.attr,
  898. &sensor_dev_attr_in3_min.dev_attr.attr,
  899. &sensor_dev_attr_in0_max.dev_attr.attr,
  900. &sensor_dev_attr_in1_max.dev_attr.attr,
  901. &sensor_dev_attr_in2_max.dev_attr.attr,
  902. &sensor_dev_attr_in3_max.dev_attr.attr,
  903. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  904. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  905. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  906. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  907. &sensor_dev_attr_temp1_input.dev_attr.attr,
  908. &sensor_dev_attr_temp2_input.dev_attr.attr,
  909. &sensor_dev_attr_temp3_input.dev_attr.attr,
  910. &sensor_dev_attr_temp1_min.dev_attr.attr,
  911. &sensor_dev_attr_temp2_min.dev_attr.attr,
  912. &sensor_dev_attr_temp3_min.dev_attr.attr,
  913. &sensor_dev_attr_temp1_max.dev_attr.attr,
  914. &sensor_dev_attr_temp2_max.dev_attr.attr,
  915. &sensor_dev_attr_temp3_max.dev_attr.attr,
  916. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  917. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  918. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  919. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  920. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  921. &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
  922. &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
  923. &sensor_dev_attr_pwm3_auto_channels.dev_attr.attr,
  924. &sensor_dev_attr_pwm1_auto_pwm_min.dev_attr.attr,
  925. &sensor_dev_attr_pwm2_auto_pwm_min.dev_attr.attr,
  926. &sensor_dev_attr_pwm3_auto_pwm_min.dev_attr.attr,
  927. &sensor_dev_attr_pwm1_auto_pwm_minctl.dev_attr.attr,
  928. &sensor_dev_attr_pwm2_auto_pwm_minctl.dev_attr.attr,
  929. &sensor_dev_attr_pwm3_auto_pwm_minctl.dev_attr.attr,
  930. &sensor_dev_attr_pwm1_auto_pwm_freq.dev_attr.attr,
  931. &sensor_dev_attr_pwm2_auto_pwm_freq.dev_attr.attr,
  932. &sensor_dev_attr_pwm3_auto_pwm_freq.dev_attr.attr,
  933. &sensor_dev_attr_temp1_auto_temp_off.dev_attr.attr,
  934. &sensor_dev_attr_temp2_auto_temp_off.dev_attr.attr,
  935. &sensor_dev_attr_temp3_auto_temp_off.dev_attr.attr,
  936. &sensor_dev_attr_temp1_auto_temp_min.dev_attr.attr,
  937. &sensor_dev_attr_temp2_auto_temp_min.dev_attr.attr,
  938. &sensor_dev_attr_temp3_auto_temp_min.dev_attr.attr,
  939. &sensor_dev_attr_temp1_auto_temp_max.dev_attr.attr,
  940. &sensor_dev_attr_temp2_auto_temp_max.dev_attr.attr,
  941. &sensor_dev_attr_temp3_auto_temp_max.dev_attr.attr,
  942. &sensor_dev_attr_temp1_auto_temp_crit.dev_attr.attr,
  943. &sensor_dev_attr_temp2_auto_temp_crit.dev_attr.attr,
  944. &sensor_dev_attr_temp3_auto_temp_crit.dev_attr.attr,
  945. &dev_attr_vrm.attr,
  946. &dev_attr_cpu0_vid.attr,
  947. &dev_attr_alarms.attr,
  948. NULL
  949. };
  950. static const struct attribute_group lm85_group = {
  951. .attrs = lm85_attributes,
  952. };
  953. static struct attribute *lm85_attributes_in4[] = {
  954. &sensor_dev_attr_in4_input.dev_attr.attr,
  955. &sensor_dev_attr_in4_min.dev_attr.attr,
  956. &sensor_dev_attr_in4_max.dev_attr.attr,
  957. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  958. NULL
  959. };
  960. static const struct attribute_group lm85_group_in4 = {
  961. .attrs = lm85_attributes_in4,
  962. };
  963. static struct attribute *lm85_attributes_in567[] = {
  964. &sensor_dev_attr_in5_input.dev_attr.attr,
  965. &sensor_dev_attr_in6_input.dev_attr.attr,
  966. &sensor_dev_attr_in7_input.dev_attr.attr,
  967. &sensor_dev_attr_in5_min.dev_attr.attr,
  968. &sensor_dev_attr_in6_min.dev_attr.attr,
  969. &sensor_dev_attr_in7_min.dev_attr.attr,
  970. &sensor_dev_attr_in5_max.dev_attr.attr,
  971. &sensor_dev_attr_in6_max.dev_attr.attr,
  972. &sensor_dev_attr_in7_max.dev_attr.attr,
  973. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  974. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  975. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  976. NULL
  977. };
  978. static const struct attribute_group lm85_group_in567 = {
  979. .attrs = lm85_attributes_in567,
  980. };
  981. static int lm85_detect(struct i2c_adapter *adapter, int address,
  982. int kind)
  983. {
  984. int company, verstep ;
  985. struct i2c_client *new_client = NULL;
  986. struct lm85_data *data;
  987. int err = 0;
  988. const char *type_name = "";
  989. if (!i2c_check_functionality(adapter,
  990. I2C_FUNC_SMBUS_BYTE_DATA)) {
  991. /* We need to be able to do byte I/O */
  992. goto ERROR0 ;
  993. };
  994. /* OK. For now, we presume we have a valid client. We now create the
  995. client structure, even though we cannot fill it completely yet.
  996. But it allows us to access lm85_{read,write}_value. */
  997. if (!(data = kzalloc(sizeof(struct lm85_data), GFP_KERNEL))) {
  998. err = -ENOMEM;
  999. goto ERROR0;
  1000. }
  1001. new_client = &data->client;
  1002. i2c_set_clientdata(new_client, data);
  1003. new_client->addr = address;
  1004. new_client->adapter = adapter;
  1005. new_client->driver = &lm85_driver;
  1006. new_client->flags = 0;
  1007. /* Now, we do the remaining detection. */
  1008. company = lm85_read_value(new_client, LM85_REG_COMPANY);
  1009. verstep = lm85_read_value(new_client, LM85_REG_VERSTEP);
  1010. dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
  1011. " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
  1012. i2c_adapter_id(new_client->adapter), new_client->addr,
  1013. company, verstep);
  1014. /* If auto-detecting, Determine the chip type. */
  1015. if (kind <= 0) {
  1016. dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x ...\n",
  1017. i2c_adapter_id(adapter), address );
  1018. if( company == LM85_COMPANY_NATIONAL
  1019. && verstep == LM85_VERSTEP_LM85C ) {
  1020. kind = lm85c ;
  1021. } else if( company == LM85_COMPANY_NATIONAL
  1022. && verstep == LM85_VERSTEP_LM85B ) {
  1023. kind = lm85b ;
  1024. } else if( company == LM85_COMPANY_NATIONAL
  1025. && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
  1026. dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
  1027. " Defaulting to LM85.\n", verstep);
  1028. kind = any_chip ;
  1029. } else if( company == LM85_COMPANY_ANALOG_DEV
  1030. && verstep == LM85_VERSTEP_ADM1027 ) {
  1031. kind = adm1027 ;
  1032. } else if( company == LM85_COMPANY_ANALOG_DEV
  1033. && (verstep == LM85_VERSTEP_ADT7463
  1034. || verstep == LM85_VERSTEP_ADT7463C) ) {
  1035. kind = adt7463 ;
  1036. } else if( company == LM85_COMPANY_ANALOG_DEV
  1037. && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC ) {
  1038. dev_err(&adapter->dev, "Unrecognized version/stepping 0x%02x"
  1039. " Defaulting to Generic LM85.\n", verstep );
  1040. kind = any_chip ;
  1041. } else if( company == LM85_COMPANY_SMSC
  1042. && (verstep == LM85_VERSTEP_EMC6D100_A0
  1043. || verstep == LM85_VERSTEP_EMC6D100_A1) ) {
  1044. /* Unfortunately, we can't tell a '100 from a '101
  1045. * from the registers. Since a '101 is a '100
  1046. * in a package with fewer pins and therefore no
  1047. * 3.3V, 1.5V or 1.8V inputs, perhaps if those
  1048. * inputs read 0, then it's a '101.
  1049. */
  1050. kind = emc6d100 ;
  1051. } else if( company == LM85_COMPANY_SMSC
  1052. && verstep == LM85_VERSTEP_EMC6D102) {
  1053. kind = emc6d102 ;
  1054. } else if( company == LM85_COMPANY_SMSC
  1055. && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
  1056. dev_err(&adapter->dev, "lm85: Detected SMSC chip\n");
  1057. dev_err(&adapter->dev, "lm85: Unrecognized version/stepping 0x%02x"
  1058. " Defaulting to Generic LM85.\n", verstep );
  1059. kind = any_chip ;
  1060. } else if( kind == any_chip
  1061. && (verstep & LM85_VERSTEP_VMASK) == LM85_VERSTEP_GENERIC) {
  1062. dev_err(&adapter->dev, "Generic LM85 Version 6 detected\n");
  1063. /* Leave kind as "any_chip" */
  1064. } else {
  1065. dev_dbg(&adapter->dev, "Autodetection failed\n");
  1066. /* Not an LM85 ... */
  1067. if( kind == any_chip ) { /* User used force=x,y */
  1068. dev_err(&adapter->dev, "Generic LM85 Version 6 not"
  1069. " found at %d,0x%02x. Try force_lm85c.\n",
  1070. i2c_adapter_id(adapter), address );
  1071. }
  1072. err = 0 ;
  1073. goto ERROR1;
  1074. }
  1075. }
  1076. /* Fill in the chip specific driver values */
  1077. if ( kind == any_chip ) {
  1078. type_name = "lm85";
  1079. } else if ( kind == lm85b ) {
  1080. type_name = "lm85b";
  1081. } else if ( kind == lm85c ) {
  1082. type_name = "lm85c";
  1083. } else if ( kind == adm1027 ) {
  1084. type_name = "adm1027";
  1085. } else if ( kind == adt7463 ) {
  1086. type_name = "adt7463";
  1087. } else if ( kind == emc6d100){
  1088. type_name = "emc6d100";
  1089. } else if ( kind == emc6d102 ) {
  1090. type_name = "emc6d102";
  1091. }
  1092. strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
  1093. /* Fill in the remaining client fields */
  1094. data->type = kind;
  1095. data->valid = 0;
  1096. mutex_init(&data->update_lock);
  1097. /* Tell the I2C layer a new client has arrived */
  1098. if ((err = i2c_attach_client(new_client)))
  1099. goto ERROR1;
  1100. /* Set the VRM version */
  1101. data->vrm = vid_which_vrm();
  1102. /* Initialize the LM85 chip */
  1103. lm85_init_client(new_client);
  1104. /* Register sysfs hooks */
  1105. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm85_group)))
  1106. goto ERROR2;
  1107. /* The ADT7463 has an optional VRM 10 mode where pin 21 is used
  1108. as a sixth digital VID input rather than an analog input. */
  1109. data->vid = lm85_read_value(new_client, LM85_REG_VID);
  1110. if (!(kind == adt7463 && (data->vid & 0x80)))
  1111. if ((err = sysfs_create_group(&new_client->dev.kobj,
  1112. &lm85_group_in4)))
  1113. goto ERROR3;
  1114. /* The EMC6D100 has 3 additional voltage inputs */
  1115. if (kind == emc6d100)
  1116. if ((err = sysfs_create_group(&new_client->dev.kobj,
  1117. &lm85_group_in567)))
  1118. goto ERROR3;
  1119. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  1120. if (IS_ERR(data->hwmon_dev)) {
  1121. err = PTR_ERR(data->hwmon_dev);
  1122. goto ERROR3;
  1123. }
  1124. return 0;
  1125. /* Error out and cleanup code */
  1126. ERROR3:
  1127. sysfs_remove_group(&new_client->dev.kobj, &lm85_group);
  1128. sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in4);
  1129. if (kind == emc6d100)
  1130. sysfs_remove_group(&new_client->dev.kobj, &lm85_group_in567);
  1131. ERROR2:
  1132. i2c_detach_client(new_client);
  1133. ERROR1:
  1134. kfree(data);
  1135. ERROR0:
  1136. return err;
  1137. }
  1138. static int lm85_detach_client(struct i2c_client *client)
  1139. {
  1140. struct lm85_data *data = i2c_get_clientdata(client);
  1141. hwmon_device_unregister(data->hwmon_dev);
  1142. sysfs_remove_group(&client->dev.kobj, &lm85_group);
  1143. sysfs_remove_group(&client->dev.kobj, &lm85_group_in4);
  1144. if (data->type == emc6d100)
  1145. sysfs_remove_group(&client->dev.kobj, &lm85_group_in567);
  1146. i2c_detach_client(client);
  1147. kfree(data);
  1148. return 0;
  1149. }
  1150. static int lm85_read_value(struct i2c_client *client, u8 reg)
  1151. {
  1152. int res;
  1153. /* What size location is it? */
  1154. switch( reg ) {
  1155. case LM85_REG_FAN(0) : /* Read WORD data */
  1156. case LM85_REG_FAN(1) :
  1157. case LM85_REG_FAN(2) :
  1158. case LM85_REG_FAN(3) :
  1159. case LM85_REG_FAN_MIN(0) :
  1160. case LM85_REG_FAN_MIN(1) :
  1161. case LM85_REG_FAN_MIN(2) :
  1162. case LM85_REG_FAN_MIN(3) :
  1163. case LM85_REG_ALARM1 : /* Read both bytes at once */
  1164. res = i2c_smbus_read_byte_data(client, reg) & 0xff ;
  1165. res |= i2c_smbus_read_byte_data(client, reg+1) << 8 ;
  1166. break ;
  1167. case ADT7463_REG_TMIN_CTL1 : /* Read WORD MSB, LSB */
  1168. res = i2c_smbus_read_byte_data(client, reg) << 8 ;
  1169. res |= i2c_smbus_read_byte_data(client, reg+1) & 0xff ;
  1170. break ;
  1171. default: /* Read BYTE data */
  1172. res = i2c_smbus_read_byte_data(client, reg);
  1173. break ;
  1174. }
  1175. return res ;
  1176. }
  1177. static int lm85_write_value(struct i2c_client *client, u8 reg, int value)
  1178. {
  1179. int res ;
  1180. switch( reg ) {
  1181. case LM85_REG_FAN(0) : /* Write WORD data */
  1182. case LM85_REG_FAN(1) :
  1183. case LM85_REG_FAN(2) :
  1184. case LM85_REG_FAN(3) :
  1185. case LM85_REG_FAN_MIN(0) :
  1186. case LM85_REG_FAN_MIN(1) :
  1187. case LM85_REG_FAN_MIN(2) :
  1188. case LM85_REG_FAN_MIN(3) :
  1189. /* NOTE: ALARM is read only, so not included here */
  1190. res = i2c_smbus_write_byte_data(client, reg, value & 0xff) ;
  1191. res |= i2c_smbus_write_byte_data(client, reg+1, (value>>8) & 0xff) ;
  1192. break ;
  1193. case ADT7463_REG_TMIN_CTL1 : /* Write WORD MSB, LSB */
  1194. res = i2c_smbus_write_byte_data(client, reg, (value>>8) & 0xff);
  1195. res |= i2c_smbus_write_byte_data(client, reg+1, value & 0xff) ;
  1196. break ;
  1197. default: /* Write BYTE data */
  1198. res = i2c_smbus_write_byte_data(client, reg, value);
  1199. break ;
  1200. }
  1201. return res ;
  1202. }
  1203. static void lm85_init_client(struct i2c_client *client)
  1204. {
  1205. int value;
  1206. struct lm85_data *data = i2c_get_clientdata(client);
  1207. dev_dbg(&client->dev, "Initializing device\n");
  1208. /* Warn if part was not "READY" */
  1209. value = lm85_read_value(client, LM85_REG_CONFIG);
  1210. dev_dbg(&client->dev, "LM85_REG_CONFIG is: 0x%02x\n", value);
  1211. if( value & 0x02 ) {
  1212. dev_err(&client->dev, "Client (%d,0x%02x) config is locked.\n",
  1213. i2c_adapter_id(client->adapter), client->addr );
  1214. };
  1215. if( ! (value & 0x04) ) {
  1216. dev_err(&client->dev, "Client (%d,0x%02x) is not ready.\n",
  1217. i2c_adapter_id(client->adapter), client->addr );
  1218. };
  1219. if( value & 0x10
  1220. && ( data->type == adm1027
  1221. || data->type == adt7463 ) ) {
  1222. dev_err(&client->dev, "Client (%d,0x%02x) VxI mode is set. "
  1223. "Please report this to the lm85 maintainer.\n",
  1224. i2c_adapter_id(client->adapter), client->addr );
  1225. };
  1226. /* WE INTENTIONALLY make no changes to the limits,
  1227. * offsets, pwms, fans and zones. If they were
  1228. * configured, we don't want to mess with them.
  1229. * If they weren't, the default is 100% PWM, no
  1230. * control and will suffice until 'sensors -s'
  1231. * can be run by the user.
  1232. */
  1233. /* Start monitoring */
  1234. value = lm85_read_value(client, LM85_REG_CONFIG);
  1235. /* Try to clear LOCK, Set START, save everything else */
  1236. value = (value & ~ 0x02) | 0x01 ;
  1237. dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
  1238. lm85_write_value(client, LM85_REG_CONFIG, value);
  1239. }
  1240. static struct lm85_data *lm85_update_device(struct device *dev)
  1241. {
  1242. struct i2c_client *client = to_i2c_client(dev);
  1243. struct lm85_data *data = i2c_get_clientdata(client);
  1244. int i;
  1245. mutex_lock(&data->update_lock);
  1246. if ( !data->valid ||
  1247. time_after(jiffies, data->last_reading + LM85_DATA_INTERVAL) ) {
  1248. /* Things that change quickly */
  1249. dev_dbg(&client->dev, "Reading sensor values\n");
  1250. /* Have to read extended bits first to "freeze" the
  1251. * more significant bits that are read later.
  1252. * There are 2 additional resolution bits per channel and we
  1253. * have room for 4, so we shift them to the left.
  1254. */
  1255. if ( (data->type == adm1027) || (data->type == adt7463) ) {
  1256. int ext1 = lm85_read_value(client,
  1257. ADM1027_REG_EXTEND_ADC1);
  1258. int ext2 = lm85_read_value(client,
  1259. ADM1027_REG_EXTEND_ADC2);
  1260. int val = (ext1 << 8) + ext2;
  1261. for(i = 0; i <= 4; i++)
  1262. data->in_ext[i] = ((val>>(i * 2))&0x03) << 2;
  1263. for(i = 0; i <= 2; i++)
  1264. data->temp_ext[i] = (val>>((i + 4) * 2))&0x0c;
  1265. }
  1266. data->vid = lm85_read_value(client, LM85_REG_VID);
  1267. for (i = 0; i <= 3; ++i) {
  1268. data->in[i] =
  1269. lm85_read_value(client, LM85_REG_IN(i));
  1270. }
  1271. if (!(data->type == adt7463 && (data->vid & 0x80))) {
  1272. data->in[4] = lm85_read_value(client,
  1273. LM85_REG_IN(4));
  1274. }
  1275. for (i = 0; i <= 3; ++i) {
  1276. data->fan[i] =
  1277. lm85_read_value(client, LM85_REG_FAN(i));
  1278. }
  1279. for (i = 0; i <= 2; ++i) {
  1280. data->temp[i] =
  1281. lm85_read_value(client, LM85_REG_TEMP(i));
  1282. }
  1283. for (i = 0; i <= 2; ++i) {
  1284. data->pwm[i] =
  1285. lm85_read_value(client, LM85_REG_PWM(i));
  1286. }
  1287. data->alarms = lm85_read_value(client, LM85_REG_ALARM1);
  1288. if ( data->type == adt7463 ) {
  1289. if( data->therm_total < ULONG_MAX - 256 ) {
  1290. data->therm_total +=
  1291. lm85_read_value(client, ADT7463_REG_THERM );
  1292. }
  1293. } else if ( data->type == emc6d100 ) {
  1294. /* Three more voltage sensors */
  1295. for (i = 5; i <= 7; ++i) {
  1296. data->in[i] =
  1297. lm85_read_value(client, EMC6D100_REG_IN(i));
  1298. }
  1299. /* More alarm bits */
  1300. data->alarms |=
  1301. lm85_read_value(client, EMC6D100_REG_ALARM3) << 16;
  1302. } else if (data->type == emc6d102 ) {
  1303. /* Have to read LSB bits after the MSB ones because
  1304. the reading of the MSB bits has frozen the
  1305. LSBs (backward from the ADM1027).
  1306. */
  1307. int ext1 = lm85_read_value(client,
  1308. EMC6D102_REG_EXTEND_ADC1);
  1309. int ext2 = lm85_read_value(client,
  1310. EMC6D102_REG_EXTEND_ADC2);
  1311. int ext3 = lm85_read_value(client,
  1312. EMC6D102_REG_EXTEND_ADC3);
  1313. int ext4 = lm85_read_value(client,
  1314. EMC6D102_REG_EXTEND_ADC4);
  1315. data->in_ext[0] = ext3 & 0x0f;
  1316. data->in_ext[1] = ext4 & 0x0f;
  1317. data->in_ext[2] = (ext4 >> 4) & 0x0f;
  1318. data->in_ext[3] = (ext3 >> 4) & 0x0f;
  1319. data->in_ext[4] = (ext2 >> 4) & 0x0f;
  1320. data->temp_ext[0] = ext1 & 0x0f;
  1321. data->temp_ext[1] = ext2 & 0x0f;
  1322. data->temp_ext[2] = (ext1 >> 4) & 0x0f;
  1323. }
  1324. data->last_reading = jiffies ;
  1325. }; /* last_reading */
  1326. if ( !data->valid ||
  1327. time_after(jiffies, data->last_config + LM85_CONFIG_INTERVAL) ) {
  1328. /* Things that don't change often */
  1329. dev_dbg(&client->dev, "Reading config values\n");
  1330. for (i = 0; i <= 3; ++i) {
  1331. data->in_min[i] =
  1332. lm85_read_value(client, LM85_REG_IN_MIN(i));
  1333. data->in_max[i] =
  1334. lm85_read_value(client, LM85_REG_IN_MAX(i));
  1335. }
  1336. if (!(data->type == adt7463 && (data->vid & 0x80))) {
  1337. data->in_min[4] = lm85_read_value(client,
  1338. LM85_REG_IN_MIN(4));
  1339. data->in_max[4] = lm85_read_value(client,
  1340. LM85_REG_IN_MAX(4));
  1341. }
  1342. if ( data->type == emc6d100 ) {
  1343. for (i = 5; i <= 7; ++i) {
  1344. data->in_min[i] =
  1345. lm85_read_value(client, EMC6D100_REG_IN_MIN(i));
  1346. data->in_max[i] =
  1347. lm85_read_value(client, EMC6D100_REG_IN_MAX(i));
  1348. }
  1349. }
  1350. for (i = 0; i <= 3; ++i) {
  1351. data->fan_min[i] =
  1352. lm85_read_value(client, LM85_REG_FAN_MIN(i));
  1353. }
  1354. for (i = 0; i <= 2; ++i) {
  1355. data->temp_min[i] =
  1356. lm85_read_value(client, LM85_REG_TEMP_MIN(i));
  1357. data->temp_max[i] =
  1358. lm85_read_value(client, LM85_REG_TEMP_MAX(i));
  1359. }
  1360. for (i = 0; i <= 2; ++i) {
  1361. int val ;
  1362. data->autofan[i].config =
  1363. lm85_read_value(client, LM85_REG_AFAN_CONFIG(i));
  1364. val = lm85_read_value(client, LM85_REG_AFAN_RANGE(i));
  1365. data->autofan[i].freq = val & 0x07 ;
  1366. data->zone[i].range = (val >> 4) & 0x0f ;
  1367. data->autofan[i].min_pwm =
  1368. lm85_read_value(client, LM85_REG_AFAN_MINPWM(i));
  1369. data->zone[i].limit =
  1370. lm85_read_value(client, LM85_REG_AFAN_LIMIT(i));
  1371. data->zone[i].critical =
  1372. lm85_read_value(client, LM85_REG_AFAN_CRITICAL(i));
  1373. }
  1374. i = lm85_read_value(client, LM85_REG_AFAN_SPIKE1);
  1375. data->smooth[0] = i & 0x0f ;
  1376. data->syncpwm3 = i & 0x10 ; /* Save PWM3 config */
  1377. data->autofan[0].min_off = (i & 0x20) != 0 ;
  1378. data->autofan[1].min_off = (i & 0x40) != 0 ;
  1379. data->autofan[2].min_off = (i & 0x80) != 0 ;
  1380. i = lm85_read_value(client, LM85_REG_AFAN_SPIKE2);
  1381. data->smooth[1] = (i>>4) & 0x0f ;
  1382. data->smooth[2] = i & 0x0f ;
  1383. i = lm85_read_value(client, LM85_REG_AFAN_HYST1);
  1384. data->zone[0].hyst = (i>>4) & 0x0f ;
  1385. data->zone[1].hyst = i & 0x0f ;
  1386. i = lm85_read_value(client, LM85_REG_AFAN_HYST2);
  1387. data->zone[2].hyst = (i>>4) & 0x0f ;
  1388. if ( (data->type == lm85b) || (data->type == lm85c) ) {
  1389. data->tach_mode = lm85_read_value(client,
  1390. LM85_REG_TACH_MODE );
  1391. data->spinup_ctl = lm85_read_value(client,
  1392. LM85_REG_SPINUP_CTL );
  1393. } else if ( (data->type == adt7463) || (data->type == adm1027) ) {
  1394. if ( data->type == adt7463 ) {
  1395. for (i = 0; i <= 2; ++i) {
  1396. data->oppoint[i] = lm85_read_value(client,
  1397. ADT7463_REG_OPPOINT(i) );
  1398. }
  1399. data->tmin_ctl = lm85_read_value(client,
  1400. ADT7463_REG_TMIN_CTL1 );
  1401. data->therm_limit = lm85_read_value(client,
  1402. ADT7463_REG_THERM_LIMIT );
  1403. }
  1404. for (i = 0; i <= 2; ++i) {
  1405. data->temp_offset[i] = lm85_read_value(client,
  1406. ADM1027_REG_TEMP_OFFSET(i) );
  1407. }
  1408. data->tach_mode = lm85_read_value(client,
  1409. ADM1027_REG_CONFIG3 );
  1410. data->fan_ppr = lm85_read_value(client,
  1411. ADM1027_REG_FAN_PPR );
  1412. }
  1413. data->last_config = jiffies;
  1414. }; /* last_config */
  1415. data->valid = 1;
  1416. mutex_unlock(&data->update_lock);
  1417. return data;
  1418. }
  1419. static int __init sm_lm85_init(void)
  1420. {
  1421. return i2c_add_driver(&lm85_driver);
  1422. }
  1423. static void __exit sm_lm85_exit(void)
  1424. {
  1425. i2c_del_driver(&lm85_driver);
  1426. }
  1427. /* Thanks to Richard Barrington for adding the LM85 to sensors-detect.
  1428. * Thanks to Margit Schubert-While <margitsw@t-online.de> for help with
  1429. * post 2.7.0 CVS changes.
  1430. */
  1431. MODULE_LICENSE("GPL");
  1432. MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, Margit Schubert-While <margitsw@t-online.de>, Justin Thiessen <jthiessen@penguincomputing.com");
  1433. MODULE_DESCRIPTION("LM85-B, LM85-C driver");
  1434. module_init(sm_lm85_init);
  1435. module_exit(sm_lm85_exit);