lm85.c 49 KB

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