lm87.c 27 KB

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