adm9240.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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.lk@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 0 > intrusion0_alarm'
  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/hwmon-sysfs.h>
  48. #include <linux/hwmon.h>
  49. #include <linux/hwmon-vid.h>
  50. #include <linux/err.h>
  51. #include <linux/mutex.h>
  52. #include <linux/jiffies.h>
  53. /* Addresses to scan */
  54. static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
  55. I2C_CLIENT_END };
  56. enum chips { adm9240, ds1780, lm81 };
  57. /* ADM9240 registers */
  58. #define ADM9240_REG_MAN_ID 0x3e
  59. #define ADM9240_REG_DIE_REV 0x3f
  60. #define ADM9240_REG_CONFIG 0x40
  61. #define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
  62. #define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
  63. #define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
  64. #define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
  65. #define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
  66. #define ADM9240_REG_INT(nr) (0x41 + (nr))
  67. #define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
  68. #define ADM9240_REG_TEMP 0x27
  69. #define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
  70. #define ADM9240_REG_ANALOG_OUT 0x19
  71. #define ADM9240_REG_CHASSIS_CLEAR 0x46
  72. #define ADM9240_REG_VID_FAN_DIV 0x47
  73. #define ADM9240_REG_I2C_ADDR 0x48
  74. #define ADM9240_REG_VID4 0x49
  75. #define ADM9240_REG_TEMP_CONF 0x4b
  76. /* generalised scaling with integer rounding */
  77. static inline int SCALE(long val, int mul, int div)
  78. {
  79. if (val < 0)
  80. return (val * mul - div / 2) / div;
  81. else
  82. return (val * mul + div / 2) / div;
  83. }
  84. /* adm9240 internally scales voltage measurements */
  85. static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
  86. static inline unsigned int IN_FROM_REG(u8 reg, int n)
  87. {
  88. return SCALE(reg, nom_mv[n], 192);
  89. }
  90. static inline u8 IN_TO_REG(unsigned long val, int n)
  91. {
  92. return clamp_val(SCALE(val, 192, nom_mv[n]), 0, 255);
  93. }
  94. /* temperature range: -40..125, 127 disables temperature alarm */
  95. static inline s8 TEMP_TO_REG(long val)
  96. {
  97. return clamp_val(SCALE(val, 1, 1000), -40, 127);
  98. }
  99. /* two fans, each with low fan speed limit */
  100. static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
  101. {
  102. if (!reg) /* error */
  103. return -1;
  104. if (reg == 255)
  105. return 0;
  106. return SCALE(1350000, 1, reg * div);
  107. }
  108. /* analog out 0..1250mV */
  109. static inline u8 AOUT_TO_REG(unsigned long val)
  110. {
  111. return clamp_val(SCALE(val, 255, 1250), 0, 255);
  112. }
  113. static inline unsigned int AOUT_FROM_REG(u8 reg)
  114. {
  115. return SCALE(reg, 1250, 255);
  116. }
  117. /* per client data */
  118. struct adm9240_data {
  119. struct i2c_client *client;
  120. struct mutex update_lock;
  121. char valid;
  122. unsigned long last_updated_measure;
  123. unsigned long last_updated_config;
  124. u8 in[6]; /* ro in0_input */
  125. u8 in_max[6]; /* rw in0_max */
  126. u8 in_min[6]; /* rw in0_min */
  127. u8 fan[2]; /* ro fan1_input */
  128. u8 fan_min[2]; /* rw fan1_min */
  129. u8 fan_div[2]; /* rw fan1_div, read-only accessor */
  130. s16 temp; /* ro temp1_input, 9-bit sign-extended */
  131. s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
  132. u16 alarms; /* ro alarms */
  133. u8 aout; /* rw aout_output */
  134. u8 vid; /* ro vid */
  135. u8 vrm; /* -- vrm set on startup, no accessor */
  136. };
  137. /* write new fan div, callers must hold data->update_lock */
  138. static void adm9240_write_fan_div(struct i2c_client *client, int nr,
  139. u8 fan_div)
  140. {
  141. u8 reg, old, shift = (nr + 2) * 2;
  142. reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
  143. old = (reg >> shift) & 3;
  144. reg &= ~(3 << shift);
  145. reg |= (fan_div << shift);
  146. i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
  147. dev_dbg(&client->dev,
  148. "fan%d clock divider changed from %u to %u\n",
  149. nr + 1, 1 << old, 1 << fan_div);
  150. }
  151. static struct adm9240_data *adm9240_update_device(struct device *dev)
  152. {
  153. struct adm9240_data *data = dev_get_drvdata(dev);
  154. struct i2c_client *client = data->client;
  155. int i;
  156. mutex_lock(&data->update_lock);
  157. /* minimum measurement cycle: 1.75 seconds */
  158. if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
  159. || !data->valid) {
  160. for (i = 0; i < 6; i++) { /* read voltages */
  161. data->in[i] = i2c_smbus_read_byte_data(client,
  162. ADM9240_REG_IN(i));
  163. }
  164. data->alarms = i2c_smbus_read_byte_data(client,
  165. ADM9240_REG_INT(0)) |
  166. i2c_smbus_read_byte_data(client,
  167. ADM9240_REG_INT(1)) << 8;
  168. /*
  169. * read temperature: assume temperature changes less than
  170. * 0.5'C per two measurement cycles thus ignore possible
  171. * but unlikely aliasing error on lsb reading. --Grant
  172. */
  173. data->temp = ((i2c_smbus_read_byte_data(client,
  174. ADM9240_REG_TEMP) << 8) |
  175. i2c_smbus_read_byte_data(client,
  176. ADM9240_REG_TEMP_CONF)) / 128;
  177. for (i = 0; i < 2; i++) { /* read fans */
  178. data->fan[i] = i2c_smbus_read_byte_data(client,
  179. ADM9240_REG_FAN(i));
  180. /* adjust fan clock divider on overflow */
  181. if (data->valid && data->fan[i] == 255 &&
  182. data->fan_div[i] < 3) {
  183. adm9240_write_fan_div(client, i,
  184. ++data->fan_div[i]);
  185. /* adjust fan_min if active, but not to 0 */
  186. if (data->fan_min[i] < 255 &&
  187. data->fan_min[i] >= 2)
  188. data->fan_min[i] /= 2;
  189. }
  190. }
  191. data->last_updated_measure = jiffies;
  192. }
  193. /* minimum config reading cycle: 300 seconds */
  194. if (time_after(jiffies, data->last_updated_config + (HZ * 300))
  195. || !data->valid) {
  196. for (i = 0; i < 6; i++) {
  197. data->in_min[i] = i2c_smbus_read_byte_data(client,
  198. ADM9240_REG_IN_MIN(i));
  199. data->in_max[i] = i2c_smbus_read_byte_data(client,
  200. ADM9240_REG_IN_MAX(i));
  201. }
  202. for (i = 0; i < 2; i++) {
  203. data->fan_min[i] = i2c_smbus_read_byte_data(client,
  204. ADM9240_REG_FAN_MIN(i));
  205. }
  206. data->temp_max[0] = i2c_smbus_read_byte_data(client,
  207. ADM9240_REG_TEMP_MAX(0));
  208. data->temp_max[1] = i2c_smbus_read_byte_data(client,
  209. ADM9240_REG_TEMP_MAX(1));
  210. /* read fan divs and 5-bit VID */
  211. i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
  212. data->fan_div[0] = (i >> 4) & 3;
  213. data->fan_div[1] = (i >> 6) & 3;
  214. data->vid = i & 0x0f;
  215. data->vid |= (i2c_smbus_read_byte_data(client,
  216. ADM9240_REG_VID4) & 1) << 4;
  217. /* read analog out */
  218. data->aout = i2c_smbus_read_byte_data(client,
  219. ADM9240_REG_ANALOG_OUT);
  220. data->last_updated_config = jiffies;
  221. data->valid = 1;
  222. }
  223. mutex_unlock(&data->update_lock);
  224. return data;
  225. }
  226. /*** sysfs accessors ***/
  227. /* temperature */
  228. static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
  229. char *buf)
  230. {
  231. struct adm9240_data *data = adm9240_update_device(dev);
  232. return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
  233. }
  234. static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
  235. char *buf)
  236. {
  237. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  238. struct adm9240_data *data = adm9240_update_device(dev);
  239. return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
  240. }
  241. static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
  242. const char *buf, size_t count)
  243. {
  244. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  245. struct adm9240_data *data = dev_get_drvdata(dev);
  246. struct i2c_client *client = data->client;
  247. long val;
  248. int err;
  249. err = kstrtol(buf, 10, &val);
  250. if (err)
  251. return err;
  252. mutex_lock(&data->update_lock);
  253. data->temp_max[attr->index] = TEMP_TO_REG(val);
  254. i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
  255. data->temp_max[attr->index]);
  256. mutex_unlock(&data->update_lock);
  257. return count;
  258. }
  259. static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
  260. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  261. show_max, set_max, 0);
  262. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
  263. show_max, set_max, 1);
  264. /* voltage */
  265. static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
  266. char *buf)
  267. {
  268. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  269. struct adm9240_data *data = adm9240_update_device(dev);
  270. return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
  271. attr->index));
  272. }
  273. static ssize_t show_in_min(struct device *dev,
  274. struct device_attribute *devattr, char *buf)
  275. {
  276. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  277. struct adm9240_data *data = adm9240_update_device(dev);
  278. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
  279. attr->index));
  280. }
  281. static ssize_t show_in_max(struct device *dev,
  282. struct device_attribute *devattr, char *buf)
  283. {
  284. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  285. struct adm9240_data *data = adm9240_update_device(dev);
  286. return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
  287. attr->index));
  288. }
  289. static ssize_t set_in_min(struct device *dev,
  290. struct device_attribute *devattr,
  291. const char *buf, size_t count)
  292. {
  293. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  294. struct adm9240_data *data = dev_get_drvdata(dev);
  295. struct i2c_client *client = data->client;
  296. unsigned long val;
  297. int err;
  298. err = kstrtoul(buf, 10, &val);
  299. if (err)
  300. return err;
  301. mutex_lock(&data->update_lock);
  302. data->in_min[attr->index] = IN_TO_REG(val, attr->index);
  303. i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
  304. data->in_min[attr->index]);
  305. mutex_unlock(&data->update_lock);
  306. return count;
  307. }
  308. static ssize_t set_in_max(struct device *dev,
  309. struct device_attribute *devattr,
  310. const char *buf, size_t count)
  311. {
  312. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  313. struct adm9240_data *data = dev_get_drvdata(dev);
  314. struct i2c_client *client = data->client;
  315. unsigned long val;
  316. int err;
  317. err = kstrtoul(buf, 10, &val);
  318. if (err)
  319. return err;
  320. mutex_lock(&data->update_lock);
  321. data->in_max[attr->index] = IN_TO_REG(val, attr->index);
  322. i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
  323. data->in_max[attr->index]);
  324. mutex_unlock(&data->update_lock);
  325. return count;
  326. }
  327. #define vin(nr) \
  328. static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
  329. show_in, NULL, nr); \
  330. static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
  331. show_in_min, set_in_min, nr); \
  332. static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
  333. show_in_max, set_in_max, nr);
  334. vin(0);
  335. vin(1);
  336. vin(2);
  337. vin(3);
  338. vin(4);
  339. vin(5);
  340. /* fans */
  341. static ssize_t show_fan(struct device *dev,
  342. struct device_attribute *devattr, char *buf)
  343. {
  344. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  345. struct adm9240_data *data = adm9240_update_device(dev);
  346. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
  347. 1 << data->fan_div[attr->index]));
  348. }
  349. static ssize_t show_fan_min(struct device *dev,
  350. struct device_attribute *devattr, char *buf)
  351. {
  352. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  353. struct adm9240_data *data = adm9240_update_device(dev);
  354. return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
  355. 1 << data->fan_div[attr->index]));
  356. }
  357. static ssize_t show_fan_div(struct device *dev,
  358. struct device_attribute *devattr, char *buf)
  359. {
  360. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  361. struct adm9240_data *data = adm9240_update_device(dev);
  362. return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
  363. }
  364. /*
  365. * set fan speed low limit:
  366. *
  367. * - value is zero: disable fan speed low limit alarm
  368. *
  369. * - value is below fan speed measurement range: enable fan speed low
  370. * limit alarm to be asserted while fan speed too slow to measure
  371. *
  372. * - otherwise: select fan clock divider to suit fan speed low limit,
  373. * measurement code may adjust registers to ensure fan speed reading
  374. */
  375. static ssize_t set_fan_min(struct device *dev,
  376. struct device_attribute *devattr,
  377. const char *buf, size_t count)
  378. {
  379. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  380. struct adm9240_data *data = dev_get_drvdata(dev);
  381. struct i2c_client *client = data->client;
  382. int nr = attr->index;
  383. u8 new_div;
  384. unsigned long val;
  385. int err;
  386. err = kstrtoul(buf, 10, &val);
  387. if (err)
  388. return err;
  389. mutex_lock(&data->update_lock);
  390. if (!val) {
  391. data->fan_min[nr] = 255;
  392. new_div = data->fan_div[nr];
  393. dev_dbg(&client->dev, "fan%u low limit set disabled\n",
  394. nr + 1);
  395. } else if (val < 1350000 / (8 * 254)) {
  396. new_div = 3;
  397. data->fan_min[nr] = 254;
  398. dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
  399. nr + 1, FAN_FROM_REG(254, 1 << new_div));
  400. } else {
  401. unsigned int new_min = 1350000 / val;
  402. new_div = 0;
  403. while (new_min > 192 && new_div < 3) {
  404. new_div++;
  405. new_min /= 2;
  406. }
  407. if (!new_min) /* keep > 0 */
  408. new_min++;
  409. data->fan_min[nr] = new_min;
  410. dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
  411. nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
  412. }
  413. if (new_div != data->fan_div[nr]) {
  414. data->fan_div[nr] = new_div;
  415. adm9240_write_fan_div(client, nr, new_div);
  416. }
  417. i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
  418. data->fan_min[nr]);
  419. mutex_unlock(&data->update_lock);
  420. return count;
  421. }
  422. #define fan(nr) \
  423. static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
  424. show_fan, NULL, nr - 1); \
  425. static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
  426. show_fan_div, NULL, nr - 1); \
  427. static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
  428. show_fan_min, set_fan_min, nr - 1);
  429. fan(1);
  430. fan(2);
  431. /* alarms */
  432. static ssize_t show_alarms(struct device *dev,
  433. struct device_attribute *attr, char *buf)
  434. {
  435. struct adm9240_data *data = adm9240_update_device(dev);
  436. return sprintf(buf, "%u\n", data->alarms);
  437. }
  438. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  439. static ssize_t show_alarm(struct device *dev,
  440. struct device_attribute *attr, char *buf)
  441. {
  442. int bitnr = to_sensor_dev_attr(attr)->index;
  443. struct adm9240_data *data = adm9240_update_device(dev);
  444. return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
  445. }
  446. static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
  447. static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
  448. static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
  449. static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
  450. static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
  451. static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
  452. static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
  453. static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
  454. static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
  455. /* vid */
  456. static ssize_t show_vid(struct device *dev,
  457. struct device_attribute *attr, char *buf)
  458. {
  459. struct adm9240_data *data = adm9240_update_device(dev);
  460. return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
  461. }
  462. static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
  463. /* analog output */
  464. static ssize_t show_aout(struct device *dev,
  465. struct device_attribute *attr, char *buf)
  466. {
  467. struct adm9240_data *data = adm9240_update_device(dev);
  468. return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
  469. }
  470. static ssize_t set_aout(struct device *dev,
  471. struct device_attribute *attr,
  472. const char *buf, size_t count)
  473. {
  474. struct adm9240_data *data = dev_get_drvdata(dev);
  475. struct i2c_client *client = data->client;
  476. long val;
  477. int err;
  478. err = kstrtol(buf, 10, &val);
  479. if (err)
  480. return err;
  481. mutex_lock(&data->update_lock);
  482. data->aout = AOUT_TO_REG(val);
  483. i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
  484. mutex_unlock(&data->update_lock);
  485. return count;
  486. }
  487. static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
  488. static ssize_t chassis_clear(struct device *dev,
  489. struct device_attribute *attr,
  490. const char *buf, size_t count)
  491. {
  492. struct adm9240_data *data = dev_get_drvdata(dev);
  493. struct i2c_client *client = data->client;
  494. unsigned long val;
  495. if (kstrtoul(buf, 10, &val) || val != 0)
  496. return -EINVAL;
  497. mutex_lock(&data->update_lock);
  498. i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
  499. data->valid = 0; /* Force cache refresh */
  500. mutex_unlock(&data->update_lock);
  501. dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
  502. return count;
  503. }
  504. static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm,
  505. chassis_clear, 12);
  506. static struct attribute *adm9240_attrs[] = {
  507. &sensor_dev_attr_in0_input.dev_attr.attr,
  508. &sensor_dev_attr_in0_min.dev_attr.attr,
  509. &sensor_dev_attr_in0_max.dev_attr.attr,
  510. &sensor_dev_attr_in0_alarm.dev_attr.attr,
  511. &sensor_dev_attr_in1_input.dev_attr.attr,
  512. &sensor_dev_attr_in1_min.dev_attr.attr,
  513. &sensor_dev_attr_in1_max.dev_attr.attr,
  514. &sensor_dev_attr_in1_alarm.dev_attr.attr,
  515. &sensor_dev_attr_in2_input.dev_attr.attr,
  516. &sensor_dev_attr_in2_min.dev_attr.attr,
  517. &sensor_dev_attr_in2_max.dev_attr.attr,
  518. &sensor_dev_attr_in2_alarm.dev_attr.attr,
  519. &sensor_dev_attr_in3_input.dev_attr.attr,
  520. &sensor_dev_attr_in3_min.dev_attr.attr,
  521. &sensor_dev_attr_in3_max.dev_attr.attr,
  522. &sensor_dev_attr_in3_alarm.dev_attr.attr,
  523. &sensor_dev_attr_in4_input.dev_attr.attr,
  524. &sensor_dev_attr_in4_min.dev_attr.attr,
  525. &sensor_dev_attr_in4_max.dev_attr.attr,
  526. &sensor_dev_attr_in4_alarm.dev_attr.attr,
  527. &sensor_dev_attr_in5_input.dev_attr.attr,
  528. &sensor_dev_attr_in5_min.dev_attr.attr,
  529. &sensor_dev_attr_in5_max.dev_attr.attr,
  530. &sensor_dev_attr_in5_alarm.dev_attr.attr,
  531. &dev_attr_temp1_input.attr,
  532. &sensor_dev_attr_temp1_max.dev_attr.attr,
  533. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  534. &sensor_dev_attr_temp1_alarm.dev_attr.attr,
  535. &sensor_dev_attr_fan1_input.dev_attr.attr,
  536. &sensor_dev_attr_fan1_div.dev_attr.attr,
  537. &sensor_dev_attr_fan1_min.dev_attr.attr,
  538. &sensor_dev_attr_fan1_alarm.dev_attr.attr,
  539. &sensor_dev_attr_fan2_input.dev_attr.attr,
  540. &sensor_dev_attr_fan2_div.dev_attr.attr,
  541. &sensor_dev_attr_fan2_min.dev_attr.attr,
  542. &sensor_dev_attr_fan2_alarm.dev_attr.attr,
  543. &dev_attr_alarms.attr,
  544. &dev_attr_aout_output.attr,
  545. &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
  546. &dev_attr_cpu0_vid.attr,
  547. NULL
  548. };
  549. ATTRIBUTE_GROUPS(adm9240);
  550. /*** sensor chip detect and driver install ***/
  551. /* Return 0 if detection is successful, -ENODEV otherwise */
  552. static int adm9240_detect(struct i2c_client *new_client,
  553. struct i2c_board_info *info)
  554. {
  555. struct i2c_adapter *adapter = new_client->adapter;
  556. const char *name = "";
  557. int address = new_client->addr;
  558. u8 man_id, die_rev;
  559. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  560. return -ENODEV;
  561. /* verify chip: reg address should match i2c address */
  562. if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
  563. != address) {
  564. dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
  565. address);
  566. return -ENODEV;
  567. }
  568. /* check known chip manufacturer */
  569. man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
  570. if (man_id == 0x23) {
  571. name = "adm9240";
  572. } else if (man_id == 0xda) {
  573. name = "ds1780";
  574. } else if (man_id == 0x01) {
  575. name = "lm81";
  576. } else {
  577. dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
  578. man_id);
  579. return -ENODEV;
  580. }
  581. /* successful detect, print chip info */
  582. die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
  583. dev_info(&adapter->dev, "found %s revision %u\n",
  584. man_id == 0x23 ? "ADM9240" :
  585. man_id == 0xda ? "DS1780" : "LM81", die_rev);
  586. strlcpy(info->type, name, I2C_NAME_SIZE);
  587. return 0;
  588. }
  589. static void adm9240_init_client(struct i2c_client *client)
  590. {
  591. struct adm9240_data *data = i2c_get_clientdata(client);
  592. u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
  593. u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
  594. data->vrm = vid_which_vrm(); /* need this to report vid as mV */
  595. dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
  596. data->vrm % 10);
  597. if (conf & 1) { /* measurement cycle running: report state */
  598. dev_info(&client->dev, "status: config 0x%02x mode %u\n",
  599. conf, mode);
  600. } else { /* cold start: open limits before starting chip */
  601. int i;
  602. for (i = 0; i < 6; i++) {
  603. i2c_smbus_write_byte_data(client,
  604. ADM9240_REG_IN_MIN(i), 0);
  605. i2c_smbus_write_byte_data(client,
  606. ADM9240_REG_IN_MAX(i), 255);
  607. }
  608. i2c_smbus_write_byte_data(client,
  609. ADM9240_REG_FAN_MIN(0), 255);
  610. i2c_smbus_write_byte_data(client,
  611. ADM9240_REG_FAN_MIN(1), 255);
  612. i2c_smbus_write_byte_data(client,
  613. ADM9240_REG_TEMP_MAX(0), 127);
  614. i2c_smbus_write_byte_data(client,
  615. ADM9240_REG_TEMP_MAX(1), 127);
  616. /* start measurement cycle */
  617. i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
  618. dev_info(&client->dev,
  619. "cold start: config was 0x%02x mode %u\n", conf, mode);
  620. }
  621. }
  622. static int adm9240_probe(struct i2c_client *new_client,
  623. const struct i2c_device_id *id)
  624. {
  625. struct device *dev = &new_client->dev;
  626. struct device *hwmon_dev;
  627. struct adm9240_data *data;
  628. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  629. if (!data)
  630. return -ENOMEM;
  631. i2c_set_clientdata(new_client, data);
  632. data->client = new_client;
  633. mutex_init(&data->update_lock);
  634. adm9240_init_client(new_client);
  635. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  636. new_client->name,
  637. data,
  638. adm9240_groups);
  639. return PTR_ERR_OR_ZERO(hwmon_dev);
  640. }
  641. static const struct i2c_device_id adm9240_id[] = {
  642. { "adm9240", adm9240 },
  643. { "ds1780", ds1780 },
  644. { "lm81", lm81 },
  645. { }
  646. };
  647. MODULE_DEVICE_TABLE(i2c, adm9240_id);
  648. static struct i2c_driver adm9240_driver = {
  649. .class = I2C_CLASS_HWMON,
  650. .driver = {
  651. .name = "adm9240",
  652. },
  653. .probe = adm9240_probe,
  654. .id_table = adm9240_id,
  655. .detect = adm9240_detect,
  656. .address_list = normal_i2c,
  657. };
  658. module_i2c_driver(adm9240_driver);
  659. MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
  660. "Grant Coady <gcoady.lk@gmail.com> and others");
  661. MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
  662. MODULE_LICENSE("GPL");