lm87.c 30 KB

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