lm87.c 30 KB

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