lm85.c 45 KB

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