lm87.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /*
  2. * lm87.c
  3. *
  4. * Copyright (C) 2000 Frodo Looijaard <frodol@dds.nl>
  5. * Philip Edelbrock <phil@netroedge.com>
  6. * Stephen Rousset <stephen.rousset@rocketlogix.com>
  7. * Dan Eaton <dan.eaton@rocketlogix.com>
  8. * Copyright (C) 2004 Jean Delvare <khali@linux-fr.org>
  9. *
  10. * Original port to Linux 2.6 by Jeff Oliver.
  11. *
  12. * The LM87 is a sensor chip made by National Semiconductor. It monitors up
  13. * to 8 voltages (including its own power source), up to three temperatures
  14. * (its own plus up to two external ones) and up to two fans. The default
  15. * configuration is 6 voltages, two temperatures and two fans (see below).
  16. * Voltages are scaled internally with ratios such that the nominal value of
  17. * each voltage correspond to a register value of 192 (which means a
  18. * resolution of about 0.5% of the nominal value). Temperature values are
  19. * reported with a 1 deg resolution and a 3-4 deg accuracy. Complete
  20. * datasheet can be obtained from National's website at:
  21. * http://www.national.com/pf/LM/LM87.html
  22. *
  23. * Some functions share pins, so not all functions are available at the same
  24. * time. Which are depends on the hardware setup. This driver assumes that
  25. * the BIOS configured the chip correctly. In that respect, it differs from
  26. * the original driver (from lm_sensors for Linux 2.4), which would force the
  27. * LM87 to an arbitrary, compile-time chosen mode, regardless of the actual
  28. * chipset wiring.
  29. * For reference, here is the list of exclusive functions:
  30. * - in0+in5 (default) or temp3
  31. * - fan1 (default) or in6
  32. * - fan2 (default) or in7
  33. * - VID lines (default) or IRQ lines (not handled by this driver)
  34. *
  35. * The LM87 additionally features an analog output, supposedly usable to
  36. * control the speed of a fan. All new chips use pulse width modulation
  37. * instead. The LM87 is the only hardware monitoring chipset I know of
  38. * which uses amplitude modulation. Be careful when using this feature.
  39. *
  40. * This program is free software; you can redistribute it and/or modify
  41. * it under the terms of the GNU General Public License as published by
  42. * the Free Software Foundation; either version 2 of the License, or
  43. * (at your option) any later version.
  44. *
  45. * This program is distributed in the hope that it will be useful,
  46. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  47. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  48. * GNU General Public License for more details.
  49. *
  50. * You should have received a copy of the GNU General Public License
  51. * along with this program; if not, write to the Free Software
  52. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  53. */
  54. #include <linux/module.h>
  55. #include <linux/init.h>
  56. #include <linux/slab.h>
  57. #include <linux/jiffies.h>
  58. #include <linux/i2c.h>
  59. #include <linux/hwmon.h>
  60. #include <linux/hwmon-sysfs.h>
  61. #include <linux/hwmon-vid.h>
  62. #include <linux/err.h>
  63. #include <linux/mutex.h>
  64. /*
  65. * Addresses to scan
  66. * LM87 has three possible addresses: 0x2c, 0x2d and 0x2e.
  67. */
  68. static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
  69. /*
  70. * Insmod parameters
  71. */
  72. I2C_CLIENT_INSMOD_1(lm87);
  73. /*
  74. * The LM87 registers
  75. */
  76. /* nr in 0..5 */
  77. #define LM87_REG_IN(nr) (0x20 + (nr))
  78. #define LM87_REG_IN_MAX(nr) (0x2B + (nr) * 2)
  79. #define LM87_REG_IN_MIN(nr) (0x2C + (nr) * 2)
  80. /* nr in 0..1 */
  81. #define LM87_REG_AIN(nr) (0x28 + (nr))
  82. #define LM87_REG_AIN_MIN(nr) (0x1A + (nr))
  83. #define LM87_REG_AIN_MAX(nr) (0x3B + (nr))
  84. static u8 LM87_REG_TEMP[3] = { 0x27, 0x26, 0x20 };
  85. static u8 LM87_REG_TEMP_HIGH[3] = { 0x39, 0x37, 0x2B };
  86. static u8 LM87_REG_TEMP_LOW[3] = { 0x3A, 0x38, 0x2C };
  87. #define LM87_REG_TEMP_HW_INT_LOCK 0x13
  88. #define LM87_REG_TEMP_HW_EXT_LOCK 0x14
  89. #define LM87_REG_TEMP_HW_INT 0x17
  90. #define LM87_REG_TEMP_HW_EXT 0x18
  91. /* nr in 0..1 */
  92. #define LM87_REG_FAN(nr) (0x28 + (nr))
  93. #define LM87_REG_FAN_MIN(nr) (0x3B + (nr))
  94. #define LM87_REG_AOUT 0x19
  95. #define LM87_REG_CONFIG 0x40
  96. #define LM87_REG_CHANNEL_MODE 0x16
  97. #define LM87_REG_VID_FAN_DIV 0x47
  98. #define LM87_REG_VID4 0x49
  99. #define LM87_REG_ALARMS1 0x41
  100. #define LM87_REG_ALARMS2 0x42
  101. #define LM87_REG_COMPANY_ID 0x3E
  102. #define LM87_REG_REVISION 0x3F
  103. /*
  104. * Conversions and various macros
  105. * The LM87 uses signed 8-bit values for temperatures.
  106. */
  107. #define IN_FROM_REG(reg,scale) (((reg) * (scale) + 96) / 192)
  108. #define IN_TO_REG(val,scale) ((val) <= 0 ? 0 : \
  109. (val) * 192 >= (scale) * 255 ? 255 : \
  110. ((val) * 192 + (scale)/2) / (scale))
  111. #define TEMP_FROM_REG(reg) ((reg) * 1000)
  112. #define TEMP_TO_REG(val) ((val) <= -127500 ? -128 : \
  113. (val) >= 126500 ? 127 : \
  114. (((val) < 0 ? (val)-500 : (val)+500) / 1000))
  115. #define FAN_FROM_REG(reg,div) ((reg) == 255 || (reg) == 0 ? 0 : \
  116. (1350000 + (reg)*(div) / 2) / ((reg)*(div)))
  117. #define FAN_TO_REG(val,div) ((val)*(div) * 255 <= 1350000 ? 255 : \
  118. (1350000 + (val)*(div) / 2) / ((val)*(div)))
  119. #define FAN_DIV_FROM_REG(reg) (1 << (reg))
  120. /* analog out is 9.80mV/LSB */
  121. #define AOUT_FROM_REG(reg) (((reg) * 98 + 5) / 10)
  122. #define AOUT_TO_REG(val) ((val) <= 0 ? 0 : \
  123. (val) >= 2500 ? 255 : \
  124. ((val) * 10 + 49) / 98)
  125. /* nr in 0..1 */
  126. #define CHAN_NO_FAN(nr) (1 << (nr))
  127. #define CHAN_TEMP3 (1 << 2)
  128. #define CHAN_VCC_5V (1 << 3)
  129. #define CHAN_NO_VID (1 << 7)
  130. /*
  131. * Functions declaration
  132. */
  133. static int lm87_attach_adapter(struct i2c_adapter *adapter);
  134. static int lm87_detect(struct i2c_adapter *adapter, int address, int kind);
  135. static void lm87_init_client(struct i2c_client *client);
  136. static int lm87_detach_client(struct i2c_client *client);
  137. static struct lm87_data *lm87_update_device(struct device *dev);
  138. /*
  139. * Driver data (common to all clients)
  140. */
  141. static struct i2c_driver lm87_driver = {
  142. .driver = {
  143. .name = "lm87",
  144. },
  145. .id = I2C_DRIVERID_LM87,
  146. .attach_adapter = lm87_attach_adapter,
  147. .detach_client = lm87_detach_client,
  148. };
  149. /*
  150. * Client data (each client gets its own)
  151. */
  152. struct lm87_data {
  153. struct i2c_client client;
  154. struct device *hwmon_dev;
  155. struct mutex update_lock;
  156. char valid; /* zero until following fields are valid */
  157. unsigned long last_updated; /* In jiffies */
  158. u8 channel; /* register value */
  159. u8 in[8]; /* register value */
  160. u8 in_max[8]; /* register value */
  161. u8 in_min[8]; /* register value */
  162. u16 in_scale[8];
  163. s8 temp[3]; /* register value */
  164. s8 temp_high[3]; /* register value */
  165. s8 temp_low[3]; /* register value */
  166. s8 temp_crit_int; /* min of two register values */
  167. s8 temp_crit_ext; /* min of two register values */
  168. u8 fan[2]; /* register value */
  169. u8 fan_min[2]; /* register value */
  170. u8 fan_div[2]; /* register value, shifted right */
  171. u8 aout; /* register value */
  172. u16 alarms; /* register values, combined */
  173. u8 vid; /* register values, combined */
  174. u8 vrm;
  175. };
  176. /*
  177. * Sysfs stuff
  178. */
  179. static inline int lm87_read_value(struct i2c_client *client, u8 reg)
  180. {
  181. return i2c_smbus_read_byte_data(client, reg);
  182. }
  183. static inline int lm87_write_value(struct i2c_client *client, u8 reg, u8 value)
  184. {
  185. return i2c_smbus_write_byte_data(client, reg, value);
  186. }
  187. #define show_in(offset) \
  188. static ssize_t show_in##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  189. { \
  190. struct lm87_data *data = lm87_update_device(dev); \
  191. return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
  192. data->in_scale[offset])); \
  193. } \
  194. static ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  195. { \
  196. struct lm87_data *data = lm87_update_device(dev); \
  197. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
  198. data->in_scale[offset])); \
  199. } \
  200. static ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
  201. { \
  202. struct lm87_data *data = lm87_update_device(dev); \
  203. return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
  204. data->in_scale[offset])); \
  205. } \
  206. static DEVICE_ATTR(in##offset##_input, S_IRUGO, \
  207. show_in##offset##_input, NULL);
  208. show_in(0);
  209. show_in(1);
  210. show_in(2);
  211. show_in(3);
  212. show_in(4);
  213. show_in(5);
  214. show_in(6);
  215. show_in(7);
  216. static void set_in_min(struct device *dev, const char *buf, int nr)
  217. {
  218. struct i2c_client *client = to_i2c_client(dev);
  219. struct lm87_data *data = i2c_get_clientdata(client);
  220. long val = simple_strtol(buf, NULL, 10);
  221. mutex_lock(&data->update_lock);
  222. data->in_min[nr] = IN_TO_REG(val, data->in_scale[nr]);
  223. lm87_write_value(client, nr<6 ? LM87_REG_IN_MIN(nr) :
  224. LM87_REG_AIN_MIN(nr-6), data->in_min[nr]);
  225. mutex_unlock(&data->update_lock);
  226. }
  227. static void set_in_max(struct device *dev, const char *buf, int nr)
  228. {
  229. struct i2c_client *client = to_i2c_client(dev);
  230. struct lm87_data *data = i2c_get_clientdata(client);
  231. long val = simple_strtol(buf, NULL, 10);
  232. mutex_lock(&data->update_lock);
  233. data->in_max[nr] = IN_TO_REG(val, data->in_scale[nr]);
  234. lm87_write_value(client, nr<6 ? LM87_REG_IN_MAX(nr) :
  235. LM87_REG_AIN_MAX(nr-6), data->in_max[nr]);
  236. mutex_unlock(&data->update_lock);
  237. }
  238. #define set_in(offset) \
  239. static ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, \
  240. const char *buf, size_t count) \
  241. { \
  242. set_in_min(dev, buf, offset); \
  243. return count; \
  244. } \
  245. static ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, \
  246. const char *buf, size_t count) \
  247. { \
  248. set_in_max(dev, buf, offset); \
  249. return count; \
  250. } \
  251. static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  252. show_in##offset##_min, set_in##offset##_min); \
  253. static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  254. show_in##offset##_max, set_in##offset##_max);
  255. set_in(0);
  256. set_in(1);
  257. set_in(2);
  258. set_in(3);
  259. set_in(4);
  260. set_in(5);
  261. set_in(6);
  262. set_in(7);
  263. #define show_temp(offset) \
  264. static ssize_t show_temp##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  265. { \
  266. struct lm87_data *data = lm87_update_device(dev); \
  267. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
  268. } \
  269. static ssize_t show_temp##offset##_low(struct device *dev, struct device_attribute *attr, char *buf) \
  270. { \
  271. struct lm87_data *data = lm87_update_device(dev); \
  272. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_low[offset-1])); \
  273. } \
  274. static ssize_t show_temp##offset##_high(struct device *dev, struct device_attribute *attr, char *buf) \
  275. { \
  276. struct lm87_data *data = lm87_update_device(dev); \
  277. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_high[offset-1])); \
  278. }\
  279. static DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
  280. show_temp##offset##_input, NULL);
  281. show_temp(1);
  282. show_temp(2);
  283. show_temp(3);
  284. static void set_temp_low(struct device *dev, const char *buf, int nr)
  285. {
  286. struct i2c_client *client = to_i2c_client(dev);
  287. struct lm87_data *data = i2c_get_clientdata(client);
  288. long val = simple_strtol(buf, NULL, 10);
  289. mutex_lock(&data->update_lock);
  290. data->temp_low[nr] = TEMP_TO_REG(val);
  291. lm87_write_value(client, LM87_REG_TEMP_LOW[nr], data->temp_low[nr]);
  292. mutex_unlock(&data->update_lock);
  293. }
  294. static void set_temp_high(struct device *dev, const char *buf, int nr)
  295. {
  296. struct i2c_client *client = to_i2c_client(dev);
  297. struct lm87_data *data = i2c_get_clientdata(client);
  298. long val = simple_strtol(buf, NULL, 10);
  299. mutex_lock(&data->update_lock);
  300. data->temp_high[nr] = TEMP_TO_REG(val);
  301. lm87_write_value(client, LM87_REG_TEMP_HIGH[nr], data->temp_high[nr]);
  302. mutex_unlock(&data->update_lock);
  303. }
  304. #define set_temp(offset) \
  305. static ssize_t set_temp##offset##_low(struct device *dev, struct device_attribute *attr, \
  306. const char *buf, size_t count) \
  307. { \
  308. set_temp_low(dev, buf, offset-1); \
  309. return count; \
  310. } \
  311. static ssize_t set_temp##offset##_high(struct device *dev, struct device_attribute *attr, \
  312. const char *buf, size_t count) \
  313. { \
  314. set_temp_high(dev, buf, offset-1); \
  315. return count; \
  316. } \
  317. static DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
  318. show_temp##offset##_high, set_temp##offset##_high); \
  319. static DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
  320. show_temp##offset##_low, set_temp##offset##_low);
  321. set_temp(1);
  322. set_temp(2);
  323. set_temp(3);
  324. static ssize_t show_temp_crit_int(struct device *dev, struct device_attribute *attr, char *buf)
  325. {
  326. struct lm87_data *data = lm87_update_device(dev);
  327. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_int));
  328. }
  329. static ssize_t show_temp_crit_ext(struct device *dev, struct device_attribute *attr, char *buf)
  330. {
  331. struct lm87_data *data = lm87_update_device(dev);
  332. return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit_ext));
  333. }
  334. static DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp_crit_int, NULL);
  335. static DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp_crit_ext, NULL);
  336. static DEVICE_ATTR(temp3_crit, S_IRUGO, show_temp_crit_ext, NULL);
  337. #define show_fan(offset) \
  338. static ssize_t show_fan##offset##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  339. { \
  340. struct lm87_data *data = lm87_update_device(dev); \
  341. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[offset-1], \
  342. FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
  343. } \
  344. static ssize_t show_fan##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
  345. { \
  346. struct lm87_data *data = lm87_update_device(dev); \
  347. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[offset-1], \
  348. FAN_DIV_FROM_REG(data->fan_div[offset-1]))); \
  349. } \
  350. static ssize_t show_fan##offset##_div(struct device *dev, struct device_attribute *attr, char *buf) \
  351. { \
  352. struct lm87_data *data = lm87_update_device(dev); \
  353. return sprintf(buf, "%d\n", FAN_DIV_FROM_REG(data->fan_div[offset-1])); \
  354. } \
  355. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  356. show_fan##offset##_input, NULL);
  357. show_fan(1);
  358. show_fan(2);
  359. static void set_fan_min(struct device *dev, const char *buf, int nr)
  360. {
  361. struct i2c_client *client = to_i2c_client(dev);
  362. struct lm87_data *data = i2c_get_clientdata(client);
  363. long val = simple_strtol(buf, NULL, 10);
  364. mutex_lock(&data->update_lock);
  365. data->fan_min[nr] = FAN_TO_REG(val,
  366. FAN_DIV_FROM_REG(data->fan_div[nr]));
  367. lm87_write_value(client, LM87_REG_FAN_MIN(nr), data->fan_min[nr]);
  368. mutex_unlock(&data->update_lock);
  369. }
  370. /* Note: we save and restore the fan minimum here, because its value is
  371. determined in part by the fan clock divider. This follows the principle
  372. of least surprise; the user doesn't expect the fan minimum to change just
  373. because the divider changed. */
  374. static ssize_t set_fan_div(struct device *dev, const char *buf,
  375. size_t count, int nr)
  376. {
  377. struct i2c_client *client = to_i2c_client(dev);
  378. struct lm87_data *data = i2c_get_clientdata(client);
  379. long val = simple_strtol(buf, NULL, 10);
  380. unsigned long min;
  381. u8 reg;
  382. mutex_lock(&data->update_lock);
  383. min = FAN_FROM_REG(data->fan_min[nr],
  384. FAN_DIV_FROM_REG(data->fan_div[nr]));
  385. switch (val) {
  386. case 1: data->fan_div[nr] = 0; break;
  387. case 2: data->fan_div[nr] = 1; break;
  388. case 4: data->fan_div[nr] = 2; break;
  389. case 8: data->fan_div[nr] = 3; break;
  390. default:
  391. mutex_unlock(&data->update_lock);
  392. return -EINVAL;
  393. }
  394. reg = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
  395. switch (nr) {
  396. case 0:
  397. reg = (reg & 0xCF) | (data->fan_div[0] << 4);
  398. break;
  399. case 1:
  400. reg = (reg & 0x3F) | (data->fan_div[1] << 6);
  401. break;
  402. }
  403. lm87_write_value(client, LM87_REG_VID_FAN_DIV, reg);
  404. data->fan_min[nr] = FAN_TO_REG(min, val);
  405. lm87_write_value(client, LM87_REG_FAN_MIN(nr),
  406. data->fan_min[nr]);
  407. mutex_unlock(&data->update_lock);
  408. return count;
  409. }
  410. #define set_fan(offset) \
  411. static ssize_t set_fan##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
  412. size_t count) \
  413. { \
  414. set_fan_min(dev, buf, offset-1); \
  415. return count; \
  416. } \
  417. static ssize_t set_fan##offset##_div(struct device *dev, struct device_attribute *attr, const char *buf, \
  418. size_t count) \
  419. { \
  420. return set_fan_div(dev, buf, count, offset-1); \
  421. } \
  422. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  423. show_fan##offset##_min, set_fan##offset##_min); \
  424. static DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
  425. show_fan##offset##_div, set_fan##offset##_div);
  426. set_fan(1);
  427. set_fan(2);
  428. static ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
  429. {
  430. struct lm87_data *data = lm87_update_device(dev);
  431. return sprintf(buf, "%d\n", data->alarms);
  432. }
  433. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  434. static ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
  435. {
  436. struct lm87_data *data = lm87_update_device(dev);
  437. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  438. }
  439. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  440. static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
  441. {
  442. struct lm87_data *data = dev_get_drvdata(dev);
  443. return sprintf(buf, "%d\n", data->vrm);
  444. }
  445. static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  446. {
  447. struct i2c_client *client = to_i2c_client(dev);
  448. struct lm87_data *data = i2c_get_clientdata(client);
  449. data->vrm = simple_strtoul(buf, NULL, 10);
  450. return count;
  451. }
  452. static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
  453. static ssize_t show_aout(struct device *dev, struct device_attribute *attr, char *buf)
  454. {
  455. struct lm87_data *data = lm87_update_device(dev);
  456. return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
  457. }
  458. static ssize_t set_aout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  459. {
  460. struct i2c_client *client = to_i2c_client(dev);
  461. struct lm87_data *data = i2c_get_clientdata(client);
  462. long val = simple_strtol(buf, NULL, 10);
  463. mutex_lock(&data->update_lock);
  464. data->aout = AOUT_TO_REG(val);
  465. lm87_write_value(client, LM87_REG_AOUT, data->aout);
  466. mutex_unlock(&data->update_lock);
  467. return count;
  468. }
  469. static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
  470. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  471. char *buf)
  472. {
  473. struct lm87_data *data = lm87_update_device(dev);
  474. int bitnr = to_sensor_dev_attr(attr)->index;
  475. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  476. }
  477. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  478. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  479. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  480. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  481. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  482. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
  483. static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 6);
  484. static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 7);
  485. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  486. static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 5);
  487. static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 5);
  488. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  489. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  490. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 14);
  491. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 15);
  492. /*
  493. * Real code
  494. */
  495. static int lm87_attach_adapter(struct i2c_adapter *adapter)
  496. {
  497. if (!(adapter->class & I2C_CLASS_HWMON))
  498. return 0;
  499. return i2c_probe(adapter, &addr_data, lm87_detect);
  500. }
  501. static struct attribute *lm87_attributes[] = {
  502. &dev_attr_in1_input.attr,
  503. &dev_attr_in1_min.attr,
  504. &dev_attr_in1_max.attr,
  505. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  506. &dev_attr_in2_input.attr,
  507. &dev_attr_in2_min.attr,
  508. &dev_attr_in2_max.attr,
  509. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  510. &dev_attr_in3_input.attr,
  511. &dev_attr_in3_min.attr,
  512. &dev_attr_in3_max.attr,
  513. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  514. &dev_attr_in4_input.attr,
  515. &dev_attr_in4_min.attr,
  516. &dev_attr_in4_max.attr,
  517. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  518. &dev_attr_temp1_input.attr,
  519. &dev_attr_temp1_max.attr,
  520. &dev_attr_temp1_min.attr,
  521. &dev_attr_temp1_crit.attr,
  522. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  523. &dev_attr_temp2_input.attr,
  524. &dev_attr_temp2_max.attr,
  525. &dev_attr_temp2_min.attr,
  526. &dev_attr_temp2_crit.attr,
  527. &sensor_dev_attr_temp2_alarm.dev_attr.attr,
  528. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  529. &dev_attr_alarms.attr,
  530. &dev_attr_aout_output.attr,
  531. NULL
  532. };
  533. static const struct attribute_group lm87_group = {
  534. .attrs = lm87_attributes,
  535. };
  536. static struct attribute *lm87_attributes_opt[] = {
  537. &dev_attr_in6_input.attr,
  538. &dev_attr_in6_min.attr,
  539. &dev_attr_in6_max.attr,
  540. &sensor_dev_attr_in6_alarm.dev_attr.attr,
  541. &dev_attr_fan1_input.attr,
  542. &dev_attr_fan1_min.attr,
  543. &dev_attr_fan1_div.attr,
  544. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  545. &dev_attr_in7_input.attr,
  546. &dev_attr_in7_min.attr,
  547. &dev_attr_in7_max.attr,
  548. &sensor_dev_attr_in7_alarm.dev_attr.attr,
  549. &dev_attr_fan2_input.attr,
  550. &dev_attr_fan2_min.attr,
  551. &dev_attr_fan2_div.attr,
  552. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  553. &dev_attr_temp3_input.attr,
  554. &dev_attr_temp3_max.attr,
  555. &dev_attr_temp3_min.attr,
  556. &dev_attr_temp3_crit.attr,
  557. &sensor_dev_attr_temp3_alarm.dev_attr.attr,
  558. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  559. &dev_attr_in0_input.attr,
  560. &dev_attr_in0_min.attr,
  561. &dev_attr_in0_max.attr,
  562. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  563. &dev_attr_in5_input.attr,
  564. &dev_attr_in5_min.attr,
  565. &dev_attr_in5_max.attr,
  566. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  567. &dev_attr_cpu0_vid.attr,
  568. &dev_attr_vrm.attr,
  569. NULL
  570. };
  571. static const struct attribute_group lm87_group_opt = {
  572. .attrs = lm87_attributes_opt,
  573. };
  574. /*
  575. * The following function does more than just detection. If detection
  576. * succeeds, it also registers the new chip.
  577. */
  578. static int lm87_detect(struct i2c_adapter *adapter, int address, int kind)
  579. {
  580. struct i2c_client *new_client;
  581. struct lm87_data *data;
  582. int err = 0;
  583. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  584. goto exit;
  585. if (!(data = kzalloc(sizeof(struct lm87_data), GFP_KERNEL))) {
  586. err = -ENOMEM;
  587. goto exit;
  588. }
  589. /* The common I2C client data is placed right before the
  590. LM87-specific data. */
  591. new_client = &data->client;
  592. i2c_set_clientdata(new_client, data);
  593. new_client->addr = address;
  594. new_client->adapter = adapter;
  595. new_client->driver = &lm87_driver;
  596. new_client->flags = 0;
  597. /* Default to an LM87 if forced */
  598. if (kind == 0)
  599. kind = lm87;
  600. /* Now, we do the remaining detection. */
  601. if (kind < 0) {
  602. u8 rev = lm87_read_value(new_client, LM87_REG_REVISION);
  603. if (rev < 0x01 || rev > 0x08
  604. || (lm87_read_value(new_client, LM87_REG_CONFIG) & 0x80)
  605. || lm87_read_value(new_client, LM87_REG_COMPANY_ID) != 0x02) {
  606. dev_dbg(&adapter->dev,
  607. "LM87 detection failed at 0x%02x.\n",
  608. address);
  609. goto exit_free;
  610. }
  611. }
  612. /* We can fill in the remaining client fields */
  613. strlcpy(new_client->name, "lm87", I2C_NAME_SIZE);
  614. data->valid = 0;
  615. mutex_init(&data->update_lock);
  616. /* Tell the I2C layer a new client has arrived */
  617. if ((err = i2c_attach_client(new_client)))
  618. goto exit_free;
  619. /* Initialize the LM87 chip */
  620. lm87_init_client(new_client);
  621. data->in_scale[0] = 2500;
  622. data->in_scale[1] = 2700;
  623. data->in_scale[2] = (data->channel & CHAN_VCC_5V) ? 5000 : 3300;
  624. data->in_scale[3] = 5000;
  625. data->in_scale[4] = 12000;
  626. data->in_scale[5] = 2700;
  627. data->in_scale[6] = 1875;
  628. data->in_scale[7] = 1875;
  629. /* Register sysfs hooks */
  630. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm87_group)))
  631. goto exit_detach;
  632. if (data->channel & CHAN_NO_FAN(0)) {
  633. if ((err = device_create_file(&new_client->dev,
  634. &dev_attr_in6_input))
  635. || (err = device_create_file(&new_client->dev,
  636. &dev_attr_in6_min))
  637. || (err = device_create_file(&new_client->dev,
  638. &dev_attr_in6_max))
  639. || (err = device_create_file(&new_client->dev,
  640. &sensor_dev_attr_in6_alarm.dev_attr)))
  641. goto exit_remove;
  642. } else {
  643. if ((err = device_create_file(&new_client->dev,
  644. &dev_attr_fan1_input))
  645. || (err = device_create_file(&new_client->dev,
  646. &dev_attr_fan1_min))
  647. || (err = device_create_file(&new_client->dev,
  648. &dev_attr_fan1_div))
  649. || (err = device_create_file(&new_client->dev,
  650. &sensor_dev_attr_fan1_alarm.dev_attr)))
  651. goto exit_remove;
  652. }
  653. if (data->channel & CHAN_NO_FAN(1)) {
  654. if ((err = device_create_file(&new_client->dev,
  655. &dev_attr_in7_input))
  656. || (err = device_create_file(&new_client->dev,
  657. &dev_attr_in7_min))
  658. || (err = device_create_file(&new_client->dev,
  659. &dev_attr_in7_max))
  660. || (err = device_create_file(&new_client->dev,
  661. &sensor_dev_attr_in7_alarm.dev_attr)))
  662. goto exit_remove;
  663. } else {
  664. if ((err = device_create_file(&new_client->dev,
  665. &dev_attr_fan2_input))
  666. || (err = device_create_file(&new_client->dev,
  667. &dev_attr_fan2_min))
  668. || (err = device_create_file(&new_client->dev,
  669. &dev_attr_fan2_div))
  670. || (err = device_create_file(&new_client->dev,
  671. &sensor_dev_attr_fan2_alarm.dev_attr)))
  672. goto exit_remove;
  673. }
  674. if (data->channel & CHAN_TEMP3) {
  675. if ((err = device_create_file(&new_client->dev,
  676. &dev_attr_temp3_input))
  677. || (err = device_create_file(&new_client->dev,
  678. &dev_attr_temp3_max))
  679. || (err = device_create_file(&new_client->dev,
  680. &dev_attr_temp3_min))
  681. || (err = device_create_file(&new_client->dev,
  682. &dev_attr_temp3_crit))
  683. || (err = device_create_file(&new_client->dev,
  684. &sensor_dev_attr_temp3_alarm.dev_attr))
  685. || (err = device_create_file(&new_client->dev,
  686. &sensor_dev_attr_temp3_fault.dev_attr)))
  687. goto exit_remove;
  688. } else {
  689. if ((err = device_create_file(&new_client->dev,
  690. &dev_attr_in0_input))
  691. || (err = device_create_file(&new_client->dev,
  692. &dev_attr_in0_min))
  693. || (err = device_create_file(&new_client->dev,
  694. &dev_attr_in0_max))
  695. || (err = device_create_file(&new_client->dev,
  696. &sensor_dev_attr_in0_alarm.dev_attr))
  697. || (err = device_create_file(&new_client->dev,
  698. &dev_attr_in5_input))
  699. || (err = device_create_file(&new_client->dev,
  700. &dev_attr_in5_min))
  701. || (err = device_create_file(&new_client->dev,
  702. &dev_attr_in5_max))
  703. || (err = device_create_file(&new_client->dev,
  704. &sensor_dev_attr_in5_alarm.dev_attr)))
  705. goto exit_remove;
  706. }
  707. if (!(data->channel & CHAN_NO_VID)) {
  708. data->vrm = vid_which_vrm();
  709. if ((err = device_create_file(&new_client->dev,
  710. &dev_attr_cpu0_vid))
  711. || (err = device_create_file(&new_client->dev,
  712. &dev_attr_vrm)))
  713. goto exit_remove;
  714. }
  715. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  716. if (IS_ERR(data->hwmon_dev)) {
  717. err = PTR_ERR(data->hwmon_dev);
  718. goto exit_remove;
  719. }
  720. return 0;
  721. exit_remove:
  722. sysfs_remove_group(&new_client->dev.kobj, &lm87_group);
  723. sysfs_remove_group(&new_client->dev.kobj, &lm87_group_opt);
  724. exit_detach:
  725. i2c_detach_client(new_client);
  726. exit_free:
  727. kfree(data);
  728. exit:
  729. return err;
  730. }
  731. static void lm87_init_client(struct i2c_client *client)
  732. {
  733. struct lm87_data *data = i2c_get_clientdata(client);
  734. u8 config;
  735. data->channel = lm87_read_value(client, LM87_REG_CHANNEL_MODE);
  736. config = lm87_read_value(client, LM87_REG_CONFIG);
  737. if (!(config & 0x01)) {
  738. int i;
  739. /* Limits are left uninitialized after power-up */
  740. for (i = 1; i < 6; i++) {
  741. lm87_write_value(client, LM87_REG_IN_MIN(i), 0x00);
  742. lm87_write_value(client, LM87_REG_IN_MAX(i), 0xFF);
  743. }
  744. for (i = 0; i < 2; i++) {
  745. lm87_write_value(client, LM87_REG_TEMP_HIGH[i], 0x7F);
  746. lm87_write_value(client, LM87_REG_TEMP_LOW[i], 0x00);
  747. lm87_write_value(client, LM87_REG_AIN_MIN(i), 0x00);
  748. lm87_write_value(client, LM87_REG_AIN_MAX(i), 0xFF);
  749. }
  750. if (data->channel & CHAN_TEMP3) {
  751. lm87_write_value(client, LM87_REG_TEMP_HIGH[2], 0x7F);
  752. lm87_write_value(client, LM87_REG_TEMP_LOW[2], 0x00);
  753. } else {
  754. lm87_write_value(client, LM87_REG_IN_MIN(0), 0x00);
  755. lm87_write_value(client, LM87_REG_IN_MAX(0), 0xFF);
  756. }
  757. }
  758. if ((config & 0x81) != 0x01) {
  759. /* Start monitoring */
  760. lm87_write_value(client, LM87_REG_CONFIG,
  761. (config & 0xF7) | 0x01);
  762. }
  763. }
  764. static int lm87_detach_client(struct i2c_client *client)
  765. {
  766. struct lm87_data *data = i2c_get_clientdata(client);
  767. int err;
  768. hwmon_device_unregister(data->hwmon_dev);
  769. sysfs_remove_group(&client->dev.kobj, &lm87_group);
  770. sysfs_remove_group(&client->dev.kobj, &lm87_group_opt);
  771. if ((err = i2c_detach_client(client)))
  772. return err;
  773. kfree(data);
  774. return 0;
  775. }
  776. static struct lm87_data *lm87_update_device(struct device *dev)
  777. {
  778. struct i2c_client *client = to_i2c_client(dev);
  779. struct lm87_data *data = i2c_get_clientdata(client);
  780. mutex_lock(&data->update_lock);
  781. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  782. int i, j;
  783. dev_dbg(&client->dev, "Updating data.\n");
  784. i = (data->channel & CHAN_TEMP3) ? 1 : 0;
  785. j = (data->channel & CHAN_TEMP3) ? 5 : 6;
  786. for (; i < j; i++) {
  787. data->in[i] = lm87_read_value(client,
  788. LM87_REG_IN(i));
  789. data->in_min[i] = lm87_read_value(client,
  790. LM87_REG_IN_MIN(i));
  791. data->in_max[i] = lm87_read_value(client,
  792. LM87_REG_IN_MAX(i));
  793. }
  794. for (i = 0; i < 2; i++) {
  795. if (data->channel & CHAN_NO_FAN(i)) {
  796. data->in[6+i] = lm87_read_value(client,
  797. LM87_REG_AIN(i));
  798. data->in_max[6+i] = lm87_read_value(client,
  799. LM87_REG_AIN_MAX(i));
  800. data->in_min[6+i] = lm87_read_value(client,
  801. LM87_REG_AIN_MIN(i));
  802. } else {
  803. data->fan[i] = lm87_read_value(client,
  804. LM87_REG_FAN(i));
  805. data->fan_min[i] = lm87_read_value(client,
  806. LM87_REG_FAN_MIN(i));
  807. }
  808. }
  809. j = (data->channel & CHAN_TEMP3) ? 3 : 2;
  810. for (i = 0 ; i < j; i++) {
  811. data->temp[i] = lm87_read_value(client,
  812. LM87_REG_TEMP[i]);
  813. data->temp_high[i] = lm87_read_value(client,
  814. LM87_REG_TEMP_HIGH[i]);
  815. data->temp_low[i] = lm87_read_value(client,
  816. LM87_REG_TEMP_LOW[i]);
  817. }
  818. i = lm87_read_value(client, LM87_REG_TEMP_HW_INT_LOCK);
  819. j = lm87_read_value(client, LM87_REG_TEMP_HW_INT);
  820. data->temp_crit_int = min(i, j);
  821. i = lm87_read_value(client, LM87_REG_TEMP_HW_EXT_LOCK);
  822. j = lm87_read_value(client, LM87_REG_TEMP_HW_EXT);
  823. data->temp_crit_ext = min(i, j);
  824. i = lm87_read_value(client, LM87_REG_VID_FAN_DIV);
  825. data->fan_div[0] = (i >> 4) & 0x03;
  826. data->fan_div[1] = (i >> 6) & 0x03;
  827. data->vid = (i & 0x0F)
  828. | (lm87_read_value(client, LM87_REG_VID4) & 0x01)
  829. << 4;
  830. data->alarms = lm87_read_value(client, LM87_REG_ALARMS1)
  831. | (lm87_read_value(client, LM87_REG_ALARMS2)
  832. << 8);
  833. data->aout = lm87_read_value(client, LM87_REG_AOUT);
  834. data->last_updated = jiffies;
  835. data->valid = 1;
  836. }
  837. mutex_unlock(&data->update_lock);
  838. return data;
  839. }
  840. static int __init sensors_lm87_init(void)
  841. {
  842. return i2c_add_driver(&lm87_driver);
  843. }
  844. static void __exit sensors_lm87_exit(void)
  845. {
  846. i2c_del_driver(&lm87_driver);
  847. }
  848. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org> and others");
  849. MODULE_DESCRIPTION("LM87 driver");
  850. MODULE_LICENSE("GPL");
  851. module_init(sensors_lm87_init);
  852. module_exit(sensors_lm87_exit);