adm9240.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. *
  5. * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
  6. * Philip Edelbrock <phil@netroedge.com>
  7. * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
  8. * Copyright (C) 2005 Grant Coady <gcoady@gmail.com> with valuable
  9. * guidance from Jean Delvare
  10. *
  11. * Driver supports Analog Devices ADM9240
  12. * Dallas Semiconductor DS1780
  13. * National Semiconductor LM81
  14. *
  15. * ADM9240 is the reference, DS1780 and LM81 are register compatibles
  16. *
  17. * Voltage Six inputs are scaled by chip, VID also reported
  18. * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
  19. * Fans 2 fans, low speed alarm, automatic fan clock divider
  20. * Alarms 16-bit map of active alarms
  21. * Analog Out 0..1250 mV output
  22. *
  23. * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
  24. *
  25. * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
  26. *
  27. * LM81 extended temp reading not implemented
  28. *
  29. * This program is free software; you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation; either version 2 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program; if not, write to the Free Software
  41. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42. */
  43. #include <linux/init.h>
  44. #include <linux/module.h>
  45. #include <linux/slab.h>
  46. #include <linux/i2c.h>
  47. #include <linux/i2c-sensor.h>
  48. #include <linux/i2c-vid.h>
  49. /* Addresses to scan */
  50. static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  51. I2C_CLIENT_END };
  52. static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
  53. /* Insmod parameters */
  54. SENSORS_INSMOD_3(adm9240, ds1780, lm81);
  55. /* ADM9240 registers */
  56. #define ADM9240_REG_MAN_ID 0x3e
  57. #define ADM9240_REG_DIE_REV 0x3f
  58. #define ADM9240_REG_CONFIG 0x40
  59. #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
  60. #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  61. #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  62. #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
  63. #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
  64. #define ADM9240_REG_INT(nr) (0x41 + (nr))
  65. #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
  66. #define ADM9240_REG_TEMP 0x27
  67. #define ADM9240_REG_TEMP_HIGH 0x39
  68. #define ADM9240_REG_TEMP_HYST 0x3a
  69. #define ADM9240_REG_ANALOG_OUT 0x19
  70. #define ADM9240_REG_CHASSIS_CLEAR 0x46
  71. #define ADM9240_REG_VID_FAN_DIV 0x47
  72. #define ADM9240_REG_I2C_ADDR 0x48
  73. #define ADM9240_REG_VID4 0x49
  74. #define ADM9240_REG_TEMP_CONF 0x4b
  75. /* generalised scaling with integer rounding */
  76. static inline int SCALE(long val, int mul, int div)
  77. {
  78. if (val < 0)
  79. return (val * mul - div / 2) / div;
  80. else
  81. return (val * mul + div / 2) / div;
  82. }
  83. /* adm9240 internally scales voltage measurements */
  84. static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
  85. static inline unsigned int IN_FROM_REG(u8 reg, int n)
  86. {
  87. return SCALE(reg, nom_mv[n], 192);
  88. }
  89. static inline u8 IN_TO_REG(unsigned long val, int n)
  90. {
  91. return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
  92. }
  93. /* temperature range: -40..125, 127 disables temperature alarm */
  94. static inline s8 TEMP_TO_REG(long val)
  95. {
  96. return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
  97. }
  98. /* two fans, each with low fan speed limit */
  99. static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
  100. {
  101. if (!reg) /* error */
  102. return -1;
  103. if (reg == 255)
  104. return 0;
  105. return SCALE(1350000, 1, reg * div);
  106. }
  107. /* analog out 0..1250mV */
  108. static inline u8 AOUT_TO_REG(unsigned long val)
  109. {
  110. return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
  111. }
  112. static inline unsigned int AOUT_FROM_REG(u8 reg)
  113. {
  114. return SCALE(reg, 1250, 255);
  115. }
  116. static int adm9240_attach_adapter(struct i2c_adapter *adapter);
  117. static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
  118. static void adm9240_init_client(struct i2c_client *client);
  119. static int adm9240_detach_client(struct i2c_client *client);
  120. static struct adm9240_data *adm9240_update_device(struct device *dev);
  121. /* driver data */
  122. static struct i2c_driver adm9240_driver = {
  123. .owner = THIS_MODULE,
  124. .name = "adm9240",
  125. .id = I2C_DRIVERID_ADM9240,
  126. .flags = I2C_DF_NOTIFY,
  127. .attach_adapter = adm9240_attach_adapter,
  128. .detach_client = adm9240_detach_client,
  129. };
  130. /* per client data */
  131. struct adm9240_data {
  132. enum chips type;
  133. struct i2c_client client;
  134. struct semaphore update_lock;
  135. char valid;
  136. unsigned long last_updated_measure;
  137. unsigned long last_updated_config;
  138. u8 in[6]; /* ro in0_input */
  139. u8 in_max[6]; /* rw in0_max */
  140. u8 in_min[6]; /* rw in0_min */
  141. u8 fan[2]; /* ro fan1_input */
  142. u8 fan_min[2]; /* rw fan1_min */
  143. u8 fan_div[2]; /* rw fan1_div, read-only accessor */
  144. s16 temp; /* ro temp1_input, 9-bit sign-extended */
  145. s8 temp_high; /* rw temp1_max */
  146. s8 temp_hyst; /* rw temp1_max_hyst */
  147. u16 alarms; /* ro alarms */
  148. u8 aout; /* rw analog_out */
  149. u8 vid; /* ro vid */
  150. u8 vrm; /* -- vrm set on startup, no accessor */
  151. };
  152. /* i2c byte read/write interface */
  153. static int adm9240_read_value(struct i2c_client *client, u8 reg)
  154. {
  155. return i2c_smbus_read_byte_data(client, reg);
  156. }
  157. static int adm9240_write_value(struct i2c_client *client, u8 reg, u8 value)
  158. {
  159. return i2c_smbus_write_byte_data(client, reg, value);
  160. }
  161. /*** sysfs accessors ***/
  162. /* temperature */
  163. #define show_temp(value, scale) \
  164. static ssize_t show_##value(struct device *dev, char *buf) \
  165. { \
  166. struct adm9240_data *data = adm9240_update_device(dev); \
  167. return sprintf(buf, "%d\n", data->value * scale); \
  168. }
  169. show_temp(temp_high, 1000);
  170. show_temp(temp_hyst, 1000);
  171. show_temp(temp, 500);
  172. #define set_temp(value, reg) \
  173. static ssize_t set_##value(struct device *dev, const char *buf, \
  174. size_t count) \
  175. { \
  176. struct i2c_client *client = to_i2c_client(dev); \
  177. struct adm9240_data *data = adm9240_update_device(dev); \
  178. long temp = simple_strtoul(buf, NULL, 10); \
  179. \
  180. down(&data->update_lock); \
  181. data->value = TEMP_TO_REG(temp); \
  182. adm9240_write_value(client, reg, data->value); \
  183. up(&data->update_lock); \
  184. return count; \
  185. }
  186. set_temp(temp_high, ADM9240_REG_TEMP_HIGH);
  187. set_temp(temp_hyst, ADM9240_REG_TEMP_HYST);
  188. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  189. show_temp_high, set_temp_high);
  190. static DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  191. show_temp_hyst, set_temp_hyst);
  192. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  193. /* voltage */
  194. static ssize_t show_in(struct device *dev, char *buf, int nr)
  195. {
  196. struct adm9240_data *data = adm9240_update_device(dev);
  197. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr));
  198. }
  199. static ssize_t show_in_min(struct device *dev, char *buf, int nr)
  200. {
  201. struct adm9240_data *data = adm9240_update_device(dev);
  202. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr));
  203. }
  204. static ssize_t show_in_max(struct device *dev, char *buf, int nr)
  205. {
  206. struct adm9240_data *data = adm9240_update_device(dev);
  207. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr));
  208. }
  209. static ssize_t set_in_min(struct device *dev, const char *buf,
  210. size_t count, int nr)
  211. {
  212. struct i2c_client *client = to_i2c_client(dev);
  213. struct adm9240_data *data = i2c_get_clientdata(client);
  214. unsigned long val = simple_strtoul(buf, NULL, 10);
  215. down(&data->update_lock);
  216. data->in_min[nr] = IN_TO_REG(val, nr);
  217. adm9240_write_value(client, ADM9240_REG_IN_MIN(nr), data->in_min[nr]);
  218. up(&data->update_lock);
  219. return count;
  220. }
  221. static ssize_t set_in_max(struct device *dev, const char *buf,
  222. size_t count, int nr)
  223. {
  224. struct i2c_client *client = to_i2c_client(dev);
  225. struct adm9240_data *data = i2c_get_clientdata(client);
  226. unsigned long val = simple_strtoul(buf, NULL, 10);
  227. down(&data->update_lock);
  228. data->in_max[nr] = IN_TO_REG(val, nr);
  229. adm9240_write_value(client, ADM9240_REG_IN_MAX(nr), data->in_max[nr]);
  230. up(&data->update_lock);
  231. return count;
  232. }
  233. #define show_in_offset(offset) \
  234. static ssize_t show_in##offset(struct device *dev, char *buf) \
  235. { \
  236. return show_in(dev, buf, offset); \
  237. } \
  238. static DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL); \
  239. static ssize_t show_in##offset##_min(struct device *dev, char *buf) \
  240. { \
  241. return show_in_min(dev, buf, offset); \
  242. } \
  243. static ssize_t show_in##offset##_max(struct device *dev, char *buf) \
  244. { \
  245. return show_in_max(dev, buf, offset); \
  246. } \
  247. static ssize_t \
  248. set_in##offset##_min(struct device *dev, const char *buf, size_t count) \
  249. { \
  250. return set_in_min(dev, buf, count, offset); \
  251. } \
  252. static ssize_t \
  253. set_in##offset##_max(struct device *dev, const char *buf, size_t count) \
  254. { \
  255. return set_in_max(dev, buf, count, offset); \
  256. } \
  257. static DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
  258. show_in##offset##_min, set_in##offset##_min); \
  259. static DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
  260. show_in##offset##_max, set_in##offset##_max);
  261. show_in_offset(0);
  262. show_in_offset(1);
  263. show_in_offset(2);
  264. show_in_offset(3);
  265. show_in_offset(4);
  266. show_in_offset(5);
  267. /* fans */
  268. static ssize_t show_fan(struct device *dev, char *buf, int nr)
  269. {
  270. struct adm9240_data *data = adm9240_update_device(dev);
  271. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
  272. 1 << data->fan_div[nr]));
  273. }
  274. static ssize_t show_fan_min(struct device *dev, char *buf, int nr)
  275. {
  276. struct adm9240_data *data = adm9240_update_device(dev);
  277. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
  278. 1 << data->fan_div[nr]));
  279. }
  280. static ssize_t show_fan_div(struct device *dev, char *buf, int nr)
  281. {
  282. struct adm9240_data *data = adm9240_update_device(dev);
  283. return sprintf(buf, "%d\n", 1 << data->fan_div[nr]);
  284. }
  285. /* write new fan div, callers must hold data->update_lock */
  286. static void adm9240_write_fan_div(struct i2c_client *client, int nr,
  287. u8 fan_div)
  288. {
  289. u8 reg, old, shift = (nr + 2) * 2;
  290. reg = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
  291. old = (reg >> shift) & 3;
  292. reg &= ~(3 << shift);
  293. reg |= (fan_div << shift);
  294. adm9240_write_value(client, ADM9240_REG_VID_FAN_DIV, reg);
  295. dev_dbg(&client->dev, "fan%d clock divider changed from %u "
  296. "to %u\n", nr + 1, 1 << old, 1 << fan_div);
  297. }
  298. /*
  299. * set fan speed low limit:
  300. *
  301. * - value is zero: disable fan speed low limit alarm
  302. *
  303. * - value is below fan speed measurement range: enable fan speed low
  304. * limit alarm to be asserted while fan speed too slow to measure
  305. *
  306. * - otherwise: select fan clock divider to suit fan speed low limit,
  307. * measurement code may adjust registers to ensure fan speed reading
  308. */
  309. static ssize_t set_fan_min(struct device *dev, const char *buf,
  310. size_t count, int nr)
  311. {
  312. struct i2c_client *client = to_i2c_client(dev);
  313. struct adm9240_data *data = i2c_get_clientdata(client);
  314. unsigned long val = simple_strtoul(buf, NULL, 10);
  315. u8 new_div;
  316. down(&data->update_lock);
  317. if (!val) {
  318. data->fan_min[nr] = 255;
  319. new_div = data->fan_div[nr];
  320. dev_dbg(&client->dev, "fan%u low limit set disabled\n",
  321. nr + 1);
  322. } else if (val < 1350000 / (8 * 254)) {
  323. new_div = 3;
  324. data->fan_min[nr] = 254;
  325. dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
  326. nr + 1, FAN_FROM_REG(254, 1 << new_div));
  327. } else {
  328. unsigned int new_min = 1350000 / val;
  329. new_div = 0;
  330. while (new_min > 192 && new_div < 3) {
  331. new_div++;
  332. new_min /= 2;
  333. }
  334. if (!new_min) /* keep > 0 */
  335. new_min++;
  336. data->fan_min[nr] = new_min;
  337. dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
  338. nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
  339. }
  340. if (new_div != data->fan_div[nr]) {
  341. data->fan_div[nr] = new_div;
  342. adm9240_write_fan_div(client, nr, new_div);
  343. }
  344. adm9240_write_value(client, ADM9240_REG_FAN_MIN(nr),
  345. data->fan_min[nr]);
  346. up(&data->update_lock);
  347. return count;
  348. }
  349. #define show_fan_offset(offset) \
  350. static ssize_t show_fan_##offset (struct device *dev, char *buf) \
  351. { \
  352. return show_fan(dev, buf, offset - 1); \
  353. } \
  354. static ssize_t show_fan_##offset##_div (struct device *dev, char *buf) \
  355. { \
  356. return show_fan_div(dev, buf, offset - 1); \
  357. } \
  358. static ssize_t show_fan_##offset##_min (struct device *dev, char *buf) \
  359. { \
  360. return show_fan_min(dev, buf, offset - 1); \
  361. } \
  362. static ssize_t set_fan_##offset##_min (struct device *dev, \
  363. const char *buf, size_t count) \
  364. { \
  365. return set_fan_min(dev, buf, count, offset - 1); \
  366. } \
  367. static DEVICE_ATTR(fan##offset##_input, S_IRUGO, \
  368. show_fan_##offset, NULL); \
  369. static DEVICE_ATTR(fan##offset##_div, S_IRUGO, \
  370. show_fan_##offset##_div, NULL); \
  371. static DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
  372. show_fan_##offset##_min, set_fan_##offset##_min);
  373. show_fan_offset(1);
  374. show_fan_offset(2);
  375. /* alarms */
  376. static ssize_t show_alarms(struct device *dev, char *buf)
  377. {
  378. struct adm9240_data *data = adm9240_update_device(dev);
  379. return sprintf(buf, "%u\n", data->alarms);
  380. }
  381. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  382. /* vid */
  383. static ssize_t show_vid(struct device *dev, char *buf)
  384. {
  385. struct adm9240_data *data = adm9240_update_device(dev);
  386. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  387. }
  388. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  389. /* analog output */
  390. static ssize_t show_aout(struct device *dev, char *buf)
  391. {
  392. struct adm9240_data *data = adm9240_update_device(dev);
  393. return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
  394. }
  395. static ssize_t set_aout(struct device *dev, const char *buf, size_t count)
  396. {
  397. struct i2c_client *client = to_i2c_client(dev);
  398. struct adm9240_data *data = i2c_get_clientdata(client);
  399. unsigned long val = simple_strtol(buf, NULL, 10);
  400. down(&data->update_lock);
  401. data->aout = AOUT_TO_REG(val);
  402. adm9240_write_value(client, ADM9240_REG_ANALOG_OUT, data->aout);
  403. up(&data->update_lock);
  404. return count;
  405. }
  406. static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
  407. /* chassis_clear */
  408. static ssize_t chassis_clear(struct device *dev, const char *buf, size_t count)
  409. {
  410. struct i2c_client *client = to_i2c_client(dev);
  411. unsigned long val = simple_strtol(buf, NULL, 10);
  412. if (val == 1) {
  413. adm9240_write_value(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
  414. dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
  415. }
  416. return count;
  417. }
  418. static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
  419. /*** sensor chip detect and driver install ***/
  420. static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
  421. {
  422. struct i2c_client *new_client;
  423. struct adm9240_data *data;
  424. int err = 0;
  425. const char *name = "";
  426. u8 man_id, die_rev;
  427. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  428. goto exit;
  429. if (!(data = kmalloc(sizeof(struct adm9240_data), GFP_KERNEL))) {
  430. err = -ENOMEM;
  431. goto exit;
  432. }
  433. memset(data, 0, sizeof(struct adm9240_data));
  434. new_client = &data->client;
  435. i2c_set_clientdata(new_client, data);
  436. new_client->addr = address;
  437. new_client->adapter = adapter;
  438. new_client->driver = &adm9240_driver;
  439. new_client->flags = 0;
  440. if (kind == 0) {
  441. kind = adm9240;
  442. }
  443. if (kind < 0) {
  444. /* verify chip: reg address should match i2c address */
  445. if (adm9240_read_value(new_client, ADM9240_REG_I2C_ADDR)
  446. != address) {
  447. dev_err(&adapter->dev, "detect fail: address match, "
  448. "0x%02x\n", address);
  449. goto exit_free;
  450. }
  451. /* check known chip manufacturer */
  452. man_id = adm9240_read_value(new_client, ADM9240_REG_MAN_ID);
  453. if (man_id == 0x23) {
  454. kind = adm9240;
  455. } else if (man_id == 0xda) {
  456. kind = ds1780;
  457. } else if (man_id == 0x01) {
  458. kind = lm81;
  459. } else {
  460. dev_err(&adapter->dev, "detect fail: unknown manuf, "
  461. "0x%02x\n", man_id);
  462. goto exit_free;
  463. }
  464. /* successful detect, print chip info */
  465. die_rev = adm9240_read_value(new_client, ADM9240_REG_DIE_REV);
  466. dev_info(&adapter->dev, "found %s revision %u\n",
  467. man_id == 0x23 ? "ADM9240" :
  468. man_id == 0xda ? "DS1780" : "LM81", die_rev);
  469. }
  470. /* either forced or detected chip kind */
  471. if (kind == adm9240) {
  472. name = "adm9240";
  473. } else if (kind == ds1780) {
  474. name = "ds1780";
  475. } else if (kind == lm81) {
  476. name = "lm81";
  477. }
  478. /* fill in the remaining client fields and attach */
  479. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  480. data->type = kind;
  481. init_MUTEX(&data->update_lock);
  482. if ((err = i2c_attach_client(new_client)))
  483. goto exit_free;
  484. adm9240_init_client(new_client);
  485. /* populate sysfs filesystem */
  486. device_create_file(&new_client->dev, &dev_attr_in0_input);
  487. device_create_file(&new_client->dev, &dev_attr_in0_min);
  488. device_create_file(&new_client->dev, &dev_attr_in0_max);
  489. device_create_file(&new_client->dev, &dev_attr_in1_input);
  490. device_create_file(&new_client->dev, &dev_attr_in1_min);
  491. device_create_file(&new_client->dev, &dev_attr_in1_max);
  492. device_create_file(&new_client->dev, &dev_attr_in2_input);
  493. device_create_file(&new_client->dev, &dev_attr_in2_min);
  494. device_create_file(&new_client->dev, &dev_attr_in2_max);
  495. device_create_file(&new_client->dev, &dev_attr_in3_input);
  496. device_create_file(&new_client->dev, &dev_attr_in3_min);
  497. device_create_file(&new_client->dev, &dev_attr_in3_max);
  498. device_create_file(&new_client->dev, &dev_attr_in4_input);
  499. device_create_file(&new_client->dev, &dev_attr_in4_min);
  500. device_create_file(&new_client->dev, &dev_attr_in4_max);
  501. device_create_file(&new_client->dev, &dev_attr_in5_input);
  502. device_create_file(&new_client->dev, &dev_attr_in5_min);
  503. device_create_file(&new_client->dev, &dev_attr_in5_max);
  504. device_create_file(&new_client->dev, &dev_attr_temp1_max);
  505. device_create_file(&new_client->dev, &dev_attr_temp1_max_hyst);
  506. device_create_file(&new_client->dev, &dev_attr_temp1_input);
  507. device_create_file(&new_client->dev, &dev_attr_fan1_input);
  508. device_create_file(&new_client->dev, &dev_attr_fan1_div);
  509. device_create_file(&new_client->dev, &dev_attr_fan1_min);
  510. device_create_file(&new_client->dev, &dev_attr_fan2_input);
  511. device_create_file(&new_client->dev, &dev_attr_fan2_div);
  512. device_create_file(&new_client->dev, &dev_attr_fan2_min);
  513. device_create_file(&new_client->dev, &dev_attr_alarms);
  514. device_create_file(&new_client->dev, &dev_attr_aout_output);
  515. device_create_file(&new_client->dev, &dev_attr_chassis_clear);
  516. device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
  517. return 0;
  518. exit_free:
  519. kfree(new_client);
  520. exit:
  521. return err;
  522. }
  523. static int adm9240_attach_adapter(struct i2c_adapter *adapter)
  524. {
  525. if (!(adapter->class & I2C_CLASS_HWMON))
  526. return 0;
  527. return i2c_detect(adapter, &addr_data, adm9240_detect);
  528. }
  529. static int adm9240_detach_client(struct i2c_client *client)
  530. {
  531. int err;
  532. if ((err = i2c_detach_client(client))) {
  533. dev_err(&client->dev, "Client deregistration failed, "
  534. "client not detached.\n");
  535. return err;
  536. }
  537. kfree(i2c_get_clientdata(client));
  538. return 0;
  539. }
  540. static void adm9240_init_client(struct i2c_client *client)
  541. {
  542. struct adm9240_data *data = i2c_get_clientdata(client);
  543. u8 conf = adm9240_read_value(client, ADM9240_REG_CONFIG);
  544. u8 mode = adm9240_read_value(client, ADM9240_REG_TEMP_CONF) & 3;
  545. data->vrm = i2c_which_vrm(); /* need this to report vid as mV */
  546. if (conf & 1) { /* measurement cycle running: report state */
  547. dev_info(&client->dev, "status: config 0x%02x mode %u\n",
  548. conf, mode);
  549. } else { /* cold start: open limits before starting chip */
  550. int i;
  551. for (i = 0; i < 6; i++)
  552. {
  553. adm9240_write_value(client,
  554. ADM9240_REG_IN_MIN(i), 0);
  555. adm9240_write_value(client,
  556. ADM9240_REG_IN_MAX(i), 255);
  557. }
  558. adm9240_write_value(client, ADM9240_REG_FAN_MIN(0), 255);
  559. adm9240_write_value(client, ADM9240_REG_FAN_MIN(1), 255);
  560. adm9240_write_value(client, ADM9240_REG_TEMP_HIGH, 127);
  561. adm9240_write_value(client, ADM9240_REG_TEMP_HYST, 127);
  562. /* start measurement cycle */
  563. adm9240_write_value(client, ADM9240_REG_CONFIG, 1);
  564. dev_info(&client->dev, "cold start: config was 0x%02x "
  565. "mode %u\n", conf, mode);
  566. }
  567. }
  568. static struct adm9240_data *adm9240_update_device(struct device *dev)
  569. {
  570. struct i2c_client *client = to_i2c_client(dev);
  571. struct adm9240_data *data = i2c_get_clientdata(client);
  572. int i;
  573. down(&data->update_lock);
  574. /* minimum measurement cycle: 1.75 seconds */
  575. if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
  576. || !data->valid) {
  577. for (i = 0; i < 6; i++) /* read voltages */
  578. {
  579. data->in[i] = adm9240_read_value(client,
  580. ADM9240_REG_IN(i));
  581. }
  582. data->alarms = adm9240_read_value(client,
  583. ADM9240_REG_INT(0)) |
  584. adm9240_read_value(client,
  585. ADM9240_REG_INT(1)) << 8;
  586. /* read temperature: assume temperature changes less than
  587. * 0.5'C per two measurement cycles thus ignore possible
  588. * but unlikely aliasing error on lsb reading. --Grant */
  589. data->temp = ((adm9240_read_value(client,
  590. ADM9240_REG_TEMP) << 8) |
  591. adm9240_read_value(client,
  592. ADM9240_REG_TEMP_CONF)) / 128;
  593. for (i = 0; i < 2; i++) /* read fans */
  594. {
  595. data->fan[i] = adm9240_read_value(client,
  596. ADM9240_REG_FAN(i));
  597. /* adjust fan clock divider on overflow */
  598. if (data->valid && data->fan[i] == 255 &&
  599. data->fan_div[i] < 3) {
  600. adm9240_write_fan_div(client, i,
  601. ++data->fan_div[i]);
  602. /* adjust fan_min if active, but not to 0 */
  603. if (data->fan_min[i] < 255 &&
  604. data->fan_min[i] >= 2)
  605. data->fan_min[i] /= 2;
  606. }
  607. }
  608. data->last_updated_measure = jiffies;
  609. }
  610. /* minimum config reading cycle: 300 seconds */
  611. if (time_after(jiffies, data->last_updated_config + (HZ * 300))
  612. || !data->valid) {
  613. for (i = 0; i < 6; i++)
  614. {
  615. data->in_min[i] = adm9240_read_value(client,
  616. ADM9240_REG_IN_MIN(i));
  617. data->in_max[i] = adm9240_read_value(client,
  618. ADM9240_REG_IN_MAX(i));
  619. }
  620. for (i = 0; i < 2; i++)
  621. {
  622. data->fan_min[i] = adm9240_read_value(client,
  623. ADM9240_REG_FAN_MIN(i));
  624. }
  625. data->temp_high = adm9240_read_value(client,
  626. ADM9240_REG_TEMP_HIGH);
  627. data->temp_hyst = adm9240_read_value(client,
  628. ADM9240_REG_TEMP_HYST);
  629. /* read fan divs and 5-bit VID */
  630. i = adm9240_read_value(client, ADM9240_REG_VID_FAN_DIV);
  631. data->fan_div[0] = (i >> 4) & 3;
  632. data->fan_div[1] = (i >> 6) & 3;
  633. data->vid = i & 0x0f;
  634. data->vid |= (adm9240_read_value(client,
  635. ADM9240_REG_VID4) & 1) << 4;
  636. /* read analog out */
  637. data->aout = adm9240_read_value(client,
  638. ADM9240_REG_ANALOG_OUT);
  639. data->last_updated_config = jiffies;
  640. data->valid = 1;
  641. }
  642. up(&data->update_lock);
  643. return data;
  644. }
  645. static int __init sensors_adm9240_init(void)
  646. {
  647. return i2c_add_driver(&adm9240_driver);
  648. }
  649. static void __exit sensors_adm9240_exit(void)
  650. {
  651. i2c_del_driver(&adm9240_driver);
  652. }
  653. MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
  654. "Grant Coady <gcoady@gmail.com> and others");
  655. MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
  656. MODULE_LICENSE("GPL");
  657. module_init(sensors_adm9240_init);
  658. module_exit(sensors_adm9240_exit);