lm90.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2008 Jean Delvare <khali@linux-fr.org>
  5. *
  6. * Based on the lm83 driver. The LM90 is a sensor chip made by National
  7. * Semiconductor. It reports up to two temperatures (its own plus up to
  8. * one external one) with a 0.125 deg resolution (1 deg for local
  9. * temperature) and a 3-4 deg accuracy.
  10. *
  11. * This driver also supports the LM89 and LM99, two other sensor chips
  12. * made by National Semiconductor. Both have an increased remote
  13. * temperature measurement accuracy (1 degree), and the LM99
  14. * additionally shifts remote temperatures (measured and limits) by 16
  15. * degrees, which allows for higher temperatures measurement.
  16. * Note that there is no way to differentiate between both chips.
  17. * When device is auto-detected, the driver will assume an LM99.
  18. *
  19. * This driver also supports the LM86, another sensor chip made by
  20. * National Semiconductor. It is exactly similar to the LM90 except it
  21. * has a higher accuracy.
  22. *
  23. * This driver also supports the ADM1032, a sensor chip made by Analog
  24. * Devices. That chip is similar to the LM90, with a few differences
  25. * that are not handled by this driver. Among others, it has a higher
  26. * accuracy than the LM90, much like the LM86 does.
  27. *
  28. * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
  29. * chips made by Maxim. These chips are similar to the LM86.
  30. * Note that there is no easy way to differentiate between the three
  31. * variants. The extra address and features of the MAX6659 are not
  32. * supported by this driver. These chips lack the remote temperature
  33. * offset feature.
  34. *
  35. * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
  36. * MAX6692 chips made by Maxim. These are again similar to the LM86,
  37. * but they use unsigned temperature values and can report temperatures
  38. * from 0 to 145 degrees.
  39. *
  40. * This driver also supports the MAX6680 and MAX6681, two other sensor
  41. * chips made by Maxim. These are quite similar to the other Maxim
  42. * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
  43. * be treated identically.
  44. *
  45. * This driver also supports the ADT7461 chip from Analog Devices.
  46. * It's supported in both compatibility and extended mode. It is mostly
  47. * compatible with LM90 except for a data format difference for the
  48. * temperature value registers.
  49. *
  50. * Since the LM90 was the first chipset supported by this driver, most
  51. * comments will refer to this chipset, but are actually general and
  52. * concern all supported chipsets, unless mentioned otherwise.
  53. *
  54. * This program is free software; you can redistribute it and/or modify
  55. * it under the terms of the GNU General Public License as published by
  56. * the Free Software Foundation; either version 2 of the License, or
  57. * (at your option) any later version.
  58. *
  59. * This program is distributed in the hope that it will be useful,
  60. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  61. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  62. * GNU General Public License for more details.
  63. *
  64. * You should have received a copy of the GNU General Public License
  65. * along with this program; if not, write to the Free Software
  66. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/init.h>
  70. #include <linux/slab.h>
  71. #include <linux/jiffies.h>
  72. #include <linux/i2c.h>
  73. #include <linux/hwmon-sysfs.h>
  74. #include <linux/hwmon.h>
  75. #include <linux/err.h>
  76. #include <linux/mutex.h>
  77. #include <linux/sysfs.h>
  78. /*
  79. * Addresses to scan
  80. * Address is fully defined internally and cannot be changed except for
  81. * MAX6659, MAX6680 and MAX6681.
  82. * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
  83. * and MAX6658 have address 0x4c.
  84. * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
  85. * MAX6647 has address 0x4e.
  86. * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
  87. * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
  88. * 0x4c, 0x4d or 0x4e.
  89. */
  90. static const unsigned short normal_i2c[] = {
  91. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  92. /*
  93. * Insmod parameters
  94. */
  95. I2C_CLIENT_INSMOD_8(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680,
  96. max6646);
  97. /*
  98. * The LM90 registers
  99. */
  100. #define LM90_REG_R_MAN_ID 0xFE
  101. #define LM90_REG_R_CHIP_ID 0xFF
  102. #define LM90_REG_R_CONFIG1 0x03
  103. #define LM90_REG_W_CONFIG1 0x09
  104. #define LM90_REG_R_CONFIG2 0xBF
  105. #define LM90_REG_W_CONFIG2 0xBF
  106. #define LM90_REG_R_CONVRATE 0x04
  107. #define LM90_REG_W_CONVRATE 0x0A
  108. #define LM90_REG_R_STATUS 0x02
  109. #define LM90_REG_R_LOCAL_TEMP 0x00
  110. #define LM90_REG_R_LOCAL_HIGH 0x05
  111. #define LM90_REG_W_LOCAL_HIGH 0x0B
  112. #define LM90_REG_R_LOCAL_LOW 0x06
  113. #define LM90_REG_W_LOCAL_LOW 0x0C
  114. #define LM90_REG_R_LOCAL_CRIT 0x20
  115. #define LM90_REG_W_LOCAL_CRIT 0x20
  116. #define LM90_REG_R_REMOTE_TEMPH 0x01
  117. #define LM90_REG_R_REMOTE_TEMPL 0x10
  118. #define LM90_REG_R_REMOTE_OFFSH 0x11
  119. #define LM90_REG_W_REMOTE_OFFSH 0x11
  120. #define LM90_REG_R_REMOTE_OFFSL 0x12
  121. #define LM90_REG_W_REMOTE_OFFSL 0x12
  122. #define LM90_REG_R_REMOTE_HIGHH 0x07
  123. #define LM90_REG_W_REMOTE_HIGHH 0x0D
  124. #define LM90_REG_R_REMOTE_HIGHL 0x13
  125. #define LM90_REG_W_REMOTE_HIGHL 0x13
  126. #define LM90_REG_R_REMOTE_LOWH 0x08
  127. #define LM90_REG_W_REMOTE_LOWH 0x0E
  128. #define LM90_REG_R_REMOTE_LOWL 0x14
  129. #define LM90_REG_W_REMOTE_LOWL 0x14
  130. #define LM90_REG_R_REMOTE_CRIT 0x19
  131. #define LM90_REG_W_REMOTE_CRIT 0x19
  132. #define LM90_REG_R_TCRIT_HYST 0x21
  133. #define LM90_REG_W_TCRIT_HYST 0x21
  134. /* MAX6646/6647/6649/6657/6658/6659 registers */
  135. #define MAX6657_REG_R_LOCAL_TEMPL 0x11
  136. /*
  137. * Device flags
  138. */
  139. #define LM90_FLAG_ADT7461_EXT 0x01 /* ADT7461 extended mode */
  140. /*
  141. * Functions declaration
  142. */
  143. static int lm90_detect(struct i2c_client *client, int kind,
  144. struct i2c_board_info *info);
  145. static int lm90_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id);
  147. static void lm90_init_client(struct i2c_client *client);
  148. static int lm90_remove(struct i2c_client *client);
  149. static struct lm90_data *lm90_update_device(struct device *dev);
  150. /*
  151. * Driver data (common to all clients)
  152. */
  153. static const struct i2c_device_id lm90_id[] = {
  154. { "adm1032", adm1032 },
  155. { "adt7461", adt7461 },
  156. { "lm90", lm90 },
  157. { "lm86", lm86 },
  158. { "lm89", lm86 },
  159. { "lm99", lm99 },
  160. { "max6646", max6646 },
  161. { "max6647", max6646 },
  162. { "max6649", max6646 },
  163. { "max6657", max6657 },
  164. { "max6658", max6657 },
  165. { "max6659", max6657 },
  166. { "max6680", max6680 },
  167. { "max6681", max6680 },
  168. { }
  169. };
  170. MODULE_DEVICE_TABLE(i2c, lm90_id);
  171. static struct i2c_driver lm90_driver = {
  172. .class = I2C_CLASS_HWMON,
  173. .driver = {
  174. .name = "lm90",
  175. },
  176. .probe = lm90_probe,
  177. .remove = lm90_remove,
  178. .id_table = lm90_id,
  179. .detect = lm90_detect,
  180. .address_data = &addr_data,
  181. };
  182. /*
  183. * Client data (each client gets its own)
  184. */
  185. struct lm90_data {
  186. struct device *hwmon_dev;
  187. struct mutex update_lock;
  188. char valid; /* zero until following fields are valid */
  189. unsigned long last_updated; /* in jiffies */
  190. int kind;
  191. int flags;
  192. /* registers values */
  193. s8 temp8[4]; /* 0: local low limit
  194. 1: local high limit
  195. 2: local critical limit
  196. 3: remote critical limit */
  197. s16 temp11[5]; /* 0: remote input
  198. 1: remote low limit
  199. 2: remote high limit
  200. 3: remote offset (except max6646 and max6657)
  201. 4: local input */
  202. u8 temp_hyst;
  203. u8 alarms; /* bitvector */
  204. };
  205. /*
  206. * Conversions
  207. * For local temperatures and limits, critical limits and the hysteresis
  208. * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
  209. * For remote temperatures and limits, it uses signed 11-bit values with
  210. * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
  211. * Maxim chips use unsigned values.
  212. */
  213. static inline int temp_from_s8(s8 val)
  214. {
  215. return val * 1000;
  216. }
  217. static inline int temp_from_u8(u8 val)
  218. {
  219. return val * 1000;
  220. }
  221. static inline int temp_from_s16(s16 val)
  222. {
  223. return val / 32 * 125;
  224. }
  225. static inline int temp_from_u16(u16 val)
  226. {
  227. return val / 32 * 125;
  228. }
  229. static s8 temp_to_s8(long val)
  230. {
  231. if (val <= -128000)
  232. return -128;
  233. if (val >= 127000)
  234. return 127;
  235. if (val < 0)
  236. return (val - 500) / 1000;
  237. return (val + 500) / 1000;
  238. }
  239. static u8 temp_to_u8(long val)
  240. {
  241. if (val <= 0)
  242. return 0;
  243. if (val >= 255000)
  244. return 255;
  245. return (val + 500) / 1000;
  246. }
  247. static s16 temp_to_s16(long val)
  248. {
  249. if (val <= -128000)
  250. return 0x8000;
  251. if (val >= 127875)
  252. return 0x7FE0;
  253. if (val < 0)
  254. return (val - 62) / 125 * 32;
  255. return (val + 62) / 125 * 32;
  256. }
  257. static u8 hyst_to_reg(long val)
  258. {
  259. if (val <= 0)
  260. return 0;
  261. if (val >= 30500)
  262. return 31;
  263. return (val + 500) / 1000;
  264. }
  265. /*
  266. * ADT7461 in compatibility mode is almost identical to LM90 except that
  267. * attempts to write values that are outside the range 0 < temp < 127 are
  268. * treated as the boundary value.
  269. *
  270. * ADT7461 in "extended mode" operation uses unsigned integers offset by
  271. * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
  272. */
  273. static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
  274. {
  275. if (data->flags & LM90_FLAG_ADT7461_EXT)
  276. return (val - 64) * 1000;
  277. else
  278. return temp_from_s8(val);
  279. }
  280. static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
  281. {
  282. if (data->flags & LM90_FLAG_ADT7461_EXT)
  283. return (val - 0x4000) / 64 * 250;
  284. else
  285. return temp_from_s16(val);
  286. }
  287. static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
  288. {
  289. if (data->flags & LM90_FLAG_ADT7461_EXT) {
  290. if (val <= -64000)
  291. return 0;
  292. if (val >= 191000)
  293. return 0xFF;
  294. return (val + 500 + 64000) / 1000;
  295. } else {
  296. if (val <= 0)
  297. return 0;
  298. if (val >= 127000)
  299. return 127;
  300. return (val + 500) / 1000;
  301. }
  302. }
  303. static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
  304. {
  305. if (data->flags & LM90_FLAG_ADT7461_EXT) {
  306. if (val <= -64000)
  307. return 0;
  308. if (val >= 191750)
  309. return 0xFFC0;
  310. return (val + 64000 + 125) / 250 * 64;
  311. } else {
  312. if (val <= 0)
  313. return 0;
  314. if (val >= 127750)
  315. return 0x7FC0;
  316. return (val + 125) / 250 * 64;
  317. }
  318. }
  319. /*
  320. * Sysfs stuff
  321. */
  322. static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
  323. char *buf)
  324. {
  325. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  326. struct lm90_data *data = lm90_update_device(dev);
  327. int temp;
  328. if (data->kind == adt7461)
  329. temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
  330. else if (data->kind == max6646)
  331. temp = temp_from_u8(data->temp8[attr->index]);
  332. else
  333. temp = temp_from_s8(data->temp8[attr->index]);
  334. /* +16 degrees offset for temp2 for the LM99 */
  335. if (data->kind == lm99 && attr->index == 3)
  336. temp += 16000;
  337. return sprintf(buf, "%d\n", temp);
  338. }
  339. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  340. const char *buf, size_t count)
  341. {
  342. static const u8 reg[4] = {
  343. LM90_REG_W_LOCAL_LOW,
  344. LM90_REG_W_LOCAL_HIGH,
  345. LM90_REG_W_LOCAL_CRIT,
  346. LM90_REG_W_REMOTE_CRIT,
  347. };
  348. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  349. struct i2c_client *client = to_i2c_client(dev);
  350. struct lm90_data *data = i2c_get_clientdata(client);
  351. long val = simple_strtol(buf, NULL, 10);
  352. int nr = attr->index;
  353. /* +16 degrees offset for temp2 for the LM99 */
  354. if (data->kind == lm99 && attr->index == 3)
  355. val -= 16000;
  356. mutex_lock(&data->update_lock);
  357. if (data->kind == adt7461)
  358. data->temp8[nr] = temp_to_u8_adt7461(data, val);
  359. else if (data->kind == max6646)
  360. data->temp8[nr] = temp_to_u8(val);
  361. else
  362. data->temp8[nr] = temp_to_s8(val);
  363. i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
  364. mutex_unlock(&data->update_lock);
  365. return count;
  366. }
  367. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  368. char *buf)
  369. {
  370. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  371. struct lm90_data *data = lm90_update_device(dev);
  372. int temp;
  373. if (data->kind == adt7461)
  374. temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
  375. else if (data->kind == max6646)
  376. temp = temp_from_u16(data->temp11[attr->index]);
  377. else
  378. temp = temp_from_s16(data->temp11[attr->index]);
  379. /* +16 degrees offset for temp2 for the LM99 */
  380. if (data->kind == lm99 && attr->index <= 2)
  381. temp += 16000;
  382. return sprintf(buf, "%d\n", temp);
  383. }
  384. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  385. const char *buf, size_t count)
  386. {
  387. static const u8 reg[6] = {
  388. LM90_REG_W_REMOTE_LOWH,
  389. LM90_REG_W_REMOTE_LOWL,
  390. LM90_REG_W_REMOTE_HIGHH,
  391. LM90_REG_W_REMOTE_HIGHL,
  392. LM90_REG_W_REMOTE_OFFSH,
  393. LM90_REG_W_REMOTE_OFFSL,
  394. };
  395. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  396. struct i2c_client *client = to_i2c_client(dev);
  397. struct lm90_data *data = i2c_get_clientdata(client);
  398. long val = simple_strtol(buf, NULL, 10);
  399. int nr = attr->index;
  400. /* +16 degrees offset for temp2 for the LM99 */
  401. if (data->kind == lm99 && attr->index <= 2)
  402. val -= 16000;
  403. mutex_lock(&data->update_lock);
  404. if (data->kind == adt7461)
  405. data->temp11[nr] = temp_to_u16_adt7461(data, val);
  406. else if (data->kind == max6657 || data->kind == max6680)
  407. data->temp11[nr] = temp_to_s8(val) << 8;
  408. else if (data->kind == max6646)
  409. data->temp11[nr] = temp_to_u8(val) << 8;
  410. else
  411. data->temp11[nr] = temp_to_s16(val);
  412. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
  413. data->temp11[nr] >> 8);
  414. if (data->kind != max6657 && data->kind != max6680
  415. && data->kind != max6646)
  416. i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
  417. data->temp11[nr] & 0xff);
  418. mutex_unlock(&data->update_lock);
  419. return count;
  420. }
  421. static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
  422. char *buf)
  423. {
  424. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  425. struct lm90_data *data = lm90_update_device(dev);
  426. int temp;
  427. if (data->kind == adt7461)
  428. temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
  429. else if (data->kind == max6646)
  430. temp = temp_from_u8(data->temp8[attr->index]);
  431. else
  432. temp = temp_from_s8(data->temp8[attr->index]);
  433. /* +16 degrees offset for temp2 for the LM99 */
  434. if (data->kind == lm99 && attr->index == 3)
  435. temp += 16000;
  436. return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
  437. }
  438. static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
  439. const char *buf, size_t count)
  440. {
  441. struct i2c_client *client = to_i2c_client(dev);
  442. struct lm90_data *data = i2c_get_clientdata(client);
  443. long val = simple_strtol(buf, NULL, 10);
  444. int temp;
  445. mutex_lock(&data->update_lock);
  446. if (data->kind == adt7461)
  447. temp = temp_from_u8_adt7461(data, data->temp8[2]);
  448. else if (data->kind == max6646)
  449. temp = temp_from_u8(data->temp8[2]);
  450. else
  451. temp = temp_from_s8(data->temp8[2]);
  452. data->temp_hyst = hyst_to_reg(temp - val);
  453. i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
  454. data->temp_hyst);
  455. mutex_unlock(&data->update_lock);
  456. return count;
  457. }
  458. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  459. char *buf)
  460. {
  461. struct lm90_data *data = lm90_update_device(dev);
  462. return sprintf(buf, "%d\n", data->alarms);
  463. }
  464. static ssize_t show_alarm(struct device *dev, struct device_attribute
  465. *devattr, char *buf)
  466. {
  467. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  468. struct lm90_data *data = lm90_update_device(dev);
  469. int bitnr = attr->index;
  470. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  471. }
  472. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp11, NULL, 4);
  473. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
  474. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
  475. set_temp8, 0);
  476. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  477. set_temp11, 1);
  478. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
  479. set_temp8, 1);
  480. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  481. set_temp11, 2);
  482. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
  483. set_temp8, 2);
  484. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
  485. set_temp8, 3);
  486. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
  487. set_temphyst, 2);
  488. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
  489. static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
  490. set_temp11, 3);
  491. /* Individual alarm files */
  492. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  493. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  494. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  495. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  496. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  497. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  498. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  499. /* Raw alarm file for compatibility */
  500. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  501. static struct attribute *lm90_attributes[] = {
  502. &sensor_dev_attr_temp1_input.dev_attr.attr,
  503. &sensor_dev_attr_temp2_input.dev_attr.attr,
  504. &sensor_dev_attr_temp1_min.dev_attr.attr,
  505. &sensor_dev_attr_temp2_min.dev_attr.attr,
  506. &sensor_dev_attr_temp1_max.dev_attr.attr,
  507. &sensor_dev_attr_temp2_max.dev_attr.attr,
  508. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  509. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  510. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  511. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  512. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  513. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  514. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  515. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  516. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  517. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  518. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  519. &dev_attr_alarms.attr,
  520. NULL
  521. };
  522. static const struct attribute_group lm90_group = {
  523. .attrs = lm90_attributes,
  524. };
  525. /* pec used for ADM1032 only */
  526. static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
  527. char *buf)
  528. {
  529. struct i2c_client *client = to_i2c_client(dev);
  530. return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
  531. }
  532. static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
  533. const char *buf, size_t count)
  534. {
  535. struct i2c_client *client = to_i2c_client(dev);
  536. long val = simple_strtol(buf, NULL, 10);
  537. switch (val) {
  538. case 0:
  539. client->flags &= ~I2C_CLIENT_PEC;
  540. break;
  541. case 1:
  542. client->flags |= I2C_CLIENT_PEC;
  543. break;
  544. default:
  545. return -EINVAL;
  546. }
  547. return count;
  548. }
  549. static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
  550. /*
  551. * Real code
  552. */
  553. /* The ADM1032 supports PEC but not on write byte transactions, so we need
  554. to explicitly ask for a transaction without PEC. */
  555. static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
  556. {
  557. return i2c_smbus_xfer(client->adapter, client->addr,
  558. client->flags & ~I2C_CLIENT_PEC,
  559. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  560. }
  561. /* It is assumed that client->update_lock is held (unless we are in
  562. detection or initialization steps). This matters when PEC is enabled,
  563. because we don't want the address pointer to change between the write
  564. byte and the read byte transactions. */
  565. static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
  566. {
  567. int err;
  568. if (client->flags & I2C_CLIENT_PEC) {
  569. err = adm1032_write_byte(client, reg);
  570. if (err >= 0)
  571. err = i2c_smbus_read_byte(client);
  572. } else
  573. err = i2c_smbus_read_byte_data(client, reg);
  574. if (err < 0) {
  575. dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
  576. reg, err);
  577. return err;
  578. }
  579. *value = err;
  580. return 0;
  581. }
  582. /* Return 0 if detection is successful, -ENODEV otherwise */
  583. static int lm90_detect(struct i2c_client *new_client, int kind,
  584. struct i2c_board_info *info)
  585. {
  586. struct i2c_adapter *adapter = new_client->adapter;
  587. int address = new_client->addr;
  588. const char *name = "";
  589. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  590. return -ENODEV;
  591. /*
  592. * Now we do the remaining detection. A negative kind means that
  593. * the driver was loaded with no force parameter (default), so we
  594. * must both detect and identify the chip. A zero kind means that
  595. * the driver was loaded with the force parameter, the detection
  596. * step shall be skipped. A positive kind means that the driver
  597. * was loaded with the force parameter and a given kind of chip is
  598. * requested, so both the detection and the identification steps
  599. * are skipped.
  600. */
  601. /* Default to an LM90 if forced */
  602. if (kind == 0)
  603. kind = lm90;
  604. if (kind < 0) { /* detection and identification */
  605. int man_id, chip_id, reg_config1, reg_convrate;
  606. if ((man_id = i2c_smbus_read_byte_data(new_client,
  607. LM90_REG_R_MAN_ID)) < 0
  608. || (chip_id = i2c_smbus_read_byte_data(new_client,
  609. LM90_REG_R_CHIP_ID)) < 0
  610. || (reg_config1 = i2c_smbus_read_byte_data(new_client,
  611. LM90_REG_R_CONFIG1)) < 0
  612. || (reg_convrate = i2c_smbus_read_byte_data(new_client,
  613. LM90_REG_R_CONVRATE)) < 0)
  614. return -ENODEV;
  615. if ((address == 0x4C || address == 0x4D)
  616. && man_id == 0x01) { /* National Semiconductor */
  617. int reg_config2;
  618. if ((reg_config2 = i2c_smbus_read_byte_data(new_client,
  619. LM90_REG_R_CONFIG2)) < 0)
  620. return -ENODEV;
  621. if ((reg_config1 & 0x2A) == 0x00
  622. && (reg_config2 & 0xF8) == 0x00
  623. && reg_convrate <= 0x09) {
  624. if (address == 0x4C
  625. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  626. kind = lm90;
  627. } else
  628. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  629. kind = lm99;
  630. dev_info(&adapter->dev,
  631. "Assuming LM99 chip at "
  632. "0x%02x\n", address);
  633. dev_info(&adapter->dev,
  634. "If it is an LM89, pass "
  635. "force_lm86=%d,0x%02x when "
  636. "loading the lm90 driver\n",
  637. i2c_adapter_id(adapter),
  638. address);
  639. } else
  640. if (address == 0x4C
  641. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  642. kind = lm86;
  643. }
  644. }
  645. } else
  646. if ((address == 0x4C || address == 0x4D)
  647. && man_id == 0x41) { /* Analog Devices */
  648. if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
  649. && (reg_config1 & 0x3F) == 0x00
  650. && reg_convrate <= 0x0A) {
  651. kind = adm1032;
  652. } else
  653. if (chip_id == 0x51 /* ADT7461 */
  654. && (reg_config1 & 0x1B) == 0x00
  655. && reg_convrate <= 0x0A) {
  656. kind = adt7461;
  657. }
  658. } else
  659. if (man_id == 0x4D) { /* Maxim */
  660. /*
  661. * The MAX6657, MAX6658 and MAX6659 do NOT have a
  662. * chip_id register. Reading from that address will
  663. * return the last read value, which in our case is
  664. * those of the man_id register. Likewise, the config1
  665. * register seems to lack a low nibble, so the value
  666. * will be those of the previous read, so in our case
  667. * those of the man_id register.
  668. */
  669. if (chip_id == man_id
  670. && (address == 0x4C || address == 0x4D)
  671. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  672. && reg_convrate <= 0x09) {
  673. kind = max6657;
  674. } else
  675. /* The chip_id register of the MAX6680 and MAX6681
  676. * holds the revision of the chip.
  677. * the lowest bit of the config1 register is unused
  678. * and should return zero when read, so should the
  679. * second to last bit of config1 (software reset)
  680. */
  681. if (chip_id == 0x01
  682. && (reg_config1 & 0x03) == 0x00
  683. && reg_convrate <= 0x07) {
  684. kind = max6680;
  685. } else
  686. /* The chip_id register of the MAX6646/6647/6649
  687. * holds the revision of the chip.
  688. * The lowest 6 bits of the config1 register are
  689. * unused and should return zero when read.
  690. */
  691. if (chip_id == 0x59
  692. && (reg_config1 & 0x3f) == 0x00
  693. && reg_convrate <= 0x07) {
  694. kind = max6646;
  695. }
  696. }
  697. if (kind <= 0) { /* identification failed */
  698. dev_dbg(&adapter->dev,
  699. "Unsupported chip at 0x%02x (man_id=0x%02X, "
  700. "chip_id=0x%02X)\n", address, man_id, chip_id);
  701. return -ENODEV;
  702. }
  703. }
  704. /* Fill the i2c board info */
  705. if (kind == lm90) {
  706. name = "lm90";
  707. } else if (kind == adm1032) {
  708. name = "adm1032";
  709. /* The ADM1032 supports PEC, but only if combined
  710. transactions are not used. */
  711. if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  712. info->flags |= I2C_CLIENT_PEC;
  713. } else if (kind == lm99) {
  714. name = "lm99";
  715. } else if (kind == lm86) {
  716. name = "lm86";
  717. } else if (kind == max6657) {
  718. name = "max6657";
  719. } else if (kind == max6680) {
  720. name = "max6680";
  721. } else if (kind == adt7461) {
  722. name = "adt7461";
  723. } else if (kind == max6646) {
  724. name = "max6646";
  725. }
  726. strlcpy(info->type, name, I2C_NAME_SIZE);
  727. return 0;
  728. }
  729. static int lm90_probe(struct i2c_client *new_client,
  730. const struct i2c_device_id *id)
  731. {
  732. struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
  733. struct lm90_data *data;
  734. int err;
  735. data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
  736. if (!data) {
  737. err = -ENOMEM;
  738. goto exit;
  739. }
  740. i2c_set_clientdata(new_client, data);
  741. mutex_init(&data->update_lock);
  742. /* Set the device type */
  743. data->kind = id->driver_data;
  744. if (data->kind == adm1032) {
  745. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  746. new_client->flags &= ~I2C_CLIENT_PEC;
  747. }
  748. /* Initialize the LM90 chip */
  749. lm90_init_client(new_client);
  750. /* Register sysfs hooks */
  751. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
  752. goto exit_free;
  753. if (new_client->flags & I2C_CLIENT_PEC) {
  754. if ((err = device_create_file(&new_client->dev,
  755. &dev_attr_pec)))
  756. goto exit_remove_files;
  757. }
  758. if (data->kind != max6657 && data->kind != max6646) {
  759. if ((err = device_create_file(&new_client->dev,
  760. &sensor_dev_attr_temp2_offset.dev_attr)))
  761. goto exit_remove_files;
  762. }
  763. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  764. if (IS_ERR(data->hwmon_dev)) {
  765. err = PTR_ERR(data->hwmon_dev);
  766. goto exit_remove_files;
  767. }
  768. return 0;
  769. exit_remove_files:
  770. sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
  771. device_remove_file(&new_client->dev, &dev_attr_pec);
  772. exit_free:
  773. kfree(data);
  774. exit:
  775. return err;
  776. }
  777. static void lm90_init_client(struct i2c_client *client)
  778. {
  779. u8 config, config_orig;
  780. struct lm90_data *data = i2c_get_clientdata(client);
  781. /*
  782. * Start the conversions.
  783. */
  784. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  785. 5); /* 2 Hz */
  786. if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
  787. dev_warn(&client->dev, "Initialization failed!\n");
  788. return;
  789. }
  790. config_orig = config;
  791. /* Check Temperature Range Select */
  792. if (data->kind == adt7461) {
  793. if (config & 0x04)
  794. data->flags |= LM90_FLAG_ADT7461_EXT;
  795. }
  796. /*
  797. * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
  798. * 0.125 degree resolution) and range (0x08, extend range
  799. * to -64 degree) mode for the remote temperature sensor.
  800. */
  801. if (data->kind == max6680) {
  802. config |= 0x18;
  803. }
  804. config &= 0xBF; /* run */
  805. if (config != config_orig) /* Only write if changed */
  806. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
  807. }
  808. static int lm90_remove(struct i2c_client *client)
  809. {
  810. struct lm90_data *data = i2c_get_clientdata(client);
  811. hwmon_device_unregister(data->hwmon_dev);
  812. sysfs_remove_group(&client->dev.kobj, &lm90_group);
  813. device_remove_file(&client->dev, &dev_attr_pec);
  814. if (data->kind != max6657 && data->kind != max6646)
  815. device_remove_file(&client->dev,
  816. &sensor_dev_attr_temp2_offset.dev_attr);
  817. kfree(data);
  818. return 0;
  819. }
  820. static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
  821. {
  822. int err;
  823. u8 oldh, newh, l;
  824. /*
  825. * There is a trick here. We have to read two registers to have the
  826. * sensor temperature, but we have to beware a conversion could occur
  827. * inbetween the readings. The datasheet says we should either use
  828. * the one-shot conversion register, which we don't want to do
  829. * (disables hardware monitoring) or monitor the busy bit, which is
  830. * impossible (we can't read the values and monitor that bit at the
  831. * exact same time). So the solution used here is to read the high
  832. * byte once, then the low byte, then the high byte again. If the new
  833. * high byte matches the old one, then we have a valid reading. Else
  834. * we have to read the low byte again, and now we believe we have a
  835. * correct reading.
  836. */
  837. if ((err = lm90_read_reg(client, regh, &oldh))
  838. || (err = lm90_read_reg(client, regl, &l))
  839. || (err = lm90_read_reg(client, regh, &newh)))
  840. return err;
  841. if (oldh != newh) {
  842. err = lm90_read_reg(client, regl, &l);
  843. if (err)
  844. return err;
  845. }
  846. *value = (newh << 8) | l;
  847. return 0;
  848. }
  849. static struct lm90_data *lm90_update_device(struct device *dev)
  850. {
  851. struct i2c_client *client = to_i2c_client(dev);
  852. struct lm90_data *data = i2c_get_clientdata(client);
  853. mutex_lock(&data->update_lock);
  854. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  855. u8 h, l;
  856. dev_dbg(&client->dev, "Updating lm90 data.\n");
  857. lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
  858. lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
  859. lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
  860. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
  861. lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
  862. if (data->kind == max6657 || data->kind == max6646) {
  863. lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
  864. MAX6657_REG_R_LOCAL_TEMPL,
  865. &data->temp11[4]);
  866. } else {
  867. if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
  868. &h) == 0)
  869. data->temp11[4] = h << 8;
  870. }
  871. lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
  872. LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
  873. if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
  874. data->temp11[1] = h << 8;
  875. if (data->kind != max6657 && data->kind != max6680
  876. && data->kind != max6646
  877. && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
  878. &l) == 0)
  879. data->temp11[1] |= l;
  880. }
  881. if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
  882. data->temp11[2] = h << 8;
  883. if (data->kind != max6657 && data->kind != max6680
  884. && data->kind != max6646
  885. && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
  886. &l) == 0)
  887. data->temp11[2] |= l;
  888. }
  889. if (data->kind != max6657 && data->kind != max6646) {
  890. if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
  891. &h) == 0
  892. && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
  893. &l) == 0)
  894. data->temp11[3] = (h << 8) | l;
  895. }
  896. lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
  897. data->last_updated = jiffies;
  898. data->valid = 1;
  899. }
  900. mutex_unlock(&data->update_lock);
  901. return data;
  902. }
  903. static int __init sensors_lm90_init(void)
  904. {
  905. return i2c_add_driver(&lm90_driver);
  906. }
  907. static void __exit sensors_lm90_exit(void)
  908. {
  909. i2c_del_driver(&lm90_driver);
  910. }
  911. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  912. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  913. MODULE_LICENSE("GPL");
  914. module_init(sensors_lm90_init);
  915. module_exit(sensors_lm90_exit);