lm90.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2003-2009 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 = NULL;
  589. int man_id, chip_id, reg_config1, reg_convrate;
  590. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  591. return -ENODEV;
  592. /* detection and identification */
  593. if ((man_id = i2c_smbus_read_byte_data(new_client,
  594. LM90_REG_R_MAN_ID)) < 0
  595. || (chip_id = i2c_smbus_read_byte_data(new_client,
  596. LM90_REG_R_CHIP_ID)) < 0
  597. || (reg_config1 = i2c_smbus_read_byte_data(new_client,
  598. LM90_REG_R_CONFIG1)) < 0
  599. || (reg_convrate = i2c_smbus_read_byte_data(new_client,
  600. LM90_REG_R_CONVRATE)) < 0)
  601. return -ENODEV;
  602. if ((address == 0x4C || address == 0x4D)
  603. && man_id == 0x01) { /* National Semiconductor */
  604. int reg_config2;
  605. reg_config2 = i2c_smbus_read_byte_data(new_client,
  606. LM90_REG_R_CONFIG2);
  607. if (reg_config2 < 0)
  608. return -ENODEV;
  609. if ((reg_config1 & 0x2A) == 0x00
  610. && (reg_config2 & 0xF8) == 0x00
  611. && reg_convrate <= 0x09) {
  612. if (address == 0x4C
  613. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  614. name = "lm90";
  615. } else
  616. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  617. name = "lm99";
  618. dev_info(&adapter->dev,
  619. "Assuming LM99 chip at 0x%02x\n",
  620. address);
  621. dev_info(&adapter->dev,
  622. "If it is an LM89, instantiate it "
  623. "with the new_device sysfs "
  624. "interface\n");
  625. } else
  626. if (address == 0x4C
  627. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  628. name = "lm86";
  629. }
  630. }
  631. } else
  632. if ((address == 0x4C || address == 0x4D)
  633. && man_id == 0x41) { /* Analog Devices */
  634. if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
  635. && (reg_config1 & 0x3F) == 0x00
  636. && reg_convrate <= 0x0A) {
  637. name = "adm1032";
  638. /* The ADM1032 supports PEC, but only if combined
  639. transactions are not used. */
  640. if (i2c_check_functionality(adapter,
  641. I2C_FUNC_SMBUS_BYTE))
  642. info->flags |= I2C_CLIENT_PEC;
  643. } else
  644. if (chip_id == 0x51 /* ADT7461 */
  645. && (reg_config1 & 0x1B) == 0x00
  646. && reg_convrate <= 0x0A) {
  647. name = "adt7461";
  648. }
  649. } else
  650. if (man_id == 0x4D) { /* Maxim */
  651. /*
  652. * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
  653. * register. Reading from that address will return the last
  654. * read value, which in our case is those of the man_id
  655. * register. Likewise, the config1 register seems to lack a
  656. * low nibble, so the value will be those of the previous
  657. * read, so in our case those of the man_id register.
  658. */
  659. if (chip_id == man_id
  660. && (address == 0x4C || address == 0x4D)
  661. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  662. && reg_convrate <= 0x09) {
  663. name = "max6657";
  664. } else
  665. /*
  666. * The chip_id register of the MAX6680 and MAX6681 holds the
  667. * revision of the chip. The lowest bit of the config1 register
  668. * is unused and should return zero when read, so should the
  669. * second to last bit of config1 (software reset).
  670. */
  671. if (chip_id == 0x01
  672. && (reg_config1 & 0x03) == 0x00
  673. && reg_convrate <= 0x07) {
  674. name = "max6680";
  675. } else
  676. /*
  677. * The chip_id register of the MAX6646/6647/6649 holds the
  678. * revision of the chip. The lowest 6 bits of the config1
  679. * register are unused and should return zero when read.
  680. */
  681. if (chip_id == 0x59
  682. && (reg_config1 & 0x3f) == 0x00
  683. && reg_convrate <= 0x07) {
  684. name = "max6646";
  685. }
  686. }
  687. if (!name) { /* identification failed */
  688. dev_dbg(&adapter->dev,
  689. "Unsupported chip at 0x%02x (man_id=0x%02X, "
  690. "chip_id=0x%02X)\n", address, man_id, chip_id);
  691. return -ENODEV;
  692. }
  693. strlcpy(info->type, name, I2C_NAME_SIZE);
  694. return 0;
  695. }
  696. static int lm90_probe(struct i2c_client *new_client,
  697. const struct i2c_device_id *id)
  698. {
  699. struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
  700. struct lm90_data *data;
  701. int err;
  702. data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
  703. if (!data) {
  704. err = -ENOMEM;
  705. goto exit;
  706. }
  707. i2c_set_clientdata(new_client, data);
  708. mutex_init(&data->update_lock);
  709. /* Set the device type */
  710. data->kind = id->driver_data;
  711. if (data->kind == adm1032) {
  712. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  713. new_client->flags &= ~I2C_CLIENT_PEC;
  714. }
  715. /* Initialize the LM90 chip */
  716. lm90_init_client(new_client);
  717. /* Register sysfs hooks */
  718. if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
  719. goto exit_free;
  720. if (new_client->flags & I2C_CLIENT_PEC) {
  721. if ((err = device_create_file(&new_client->dev,
  722. &dev_attr_pec)))
  723. goto exit_remove_files;
  724. }
  725. if (data->kind != max6657 && data->kind != max6646) {
  726. if ((err = device_create_file(&new_client->dev,
  727. &sensor_dev_attr_temp2_offset.dev_attr)))
  728. goto exit_remove_files;
  729. }
  730. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  731. if (IS_ERR(data->hwmon_dev)) {
  732. err = PTR_ERR(data->hwmon_dev);
  733. goto exit_remove_files;
  734. }
  735. return 0;
  736. exit_remove_files:
  737. sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
  738. device_remove_file(&new_client->dev, &dev_attr_pec);
  739. exit_free:
  740. kfree(data);
  741. exit:
  742. return err;
  743. }
  744. static void lm90_init_client(struct i2c_client *client)
  745. {
  746. u8 config, config_orig;
  747. struct lm90_data *data = i2c_get_clientdata(client);
  748. /*
  749. * Start the conversions.
  750. */
  751. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  752. 5); /* 2 Hz */
  753. if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
  754. dev_warn(&client->dev, "Initialization failed!\n");
  755. return;
  756. }
  757. config_orig = config;
  758. /* Check Temperature Range Select */
  759. if (data->kind == adt7461) {
  760. if (config & 0x04)
  761. data->flags |= LM90_FLAG_ADT7461_EXT;
  762. }
  763. /*
  764. * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
  765. * 0.125 degree resolution) and range (0x08, extend range
  766. * to -64 degree) mode for the remote temperature sensor.
  767. */
  768. if (data->kind == max6680) {
  769. config |= 0x18;
  770. }
  771. config &= 0xBF; /* run */
  772. if (config != config_orig) /* Only write if changed */
  773. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
  774. }
  775. static int lm90_remove(struct i2c_client *client)
  776. {
  777. struct lm90_data *data = i2c_get_clientdata(client);
  778. hwmon_device_unregister(data->hwmon_dev);
  779. sysfs_remove_group(&client->dev.kobj, &lm90_group);
  780. device_remove_file(&client->dev, &dev_attr_pec);
  781. if (data->kind != max6657 && data->kind != max6646)
  782. device_remove_file(&client->dev,
  783. &sensor_dev_attr_temp2_offset.dev_attr);
  784. kfree(data);
  785. return 0;
  786. }
  787. static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
  788. {
  789. int err;
  790. u8 oldh, newh, l;
  791. /*
  792. * There is a trick here. We have to read two registers to have the
  793. * sensor temperature, but we have to beware a conversion could occur
  794. * inbetween the readings. The datasheet says we should either use
  795. * the one-shot conversion register, which we don't want to do
  796. * (disables hardware monitoring) or monitor the busy bit, which is
  797. * impossible (we can't read the values and monitor that bit at the
  798. * exact same time). So the solution used here is to read the high
  799. * byte once, then the low byte, then the high byte again. If the new
  800. * high byte matches the old one, then we have a valid reading. Else
  801. * we have to read the low byte again, and now we believe we have a
  802. * correct reading.
  803. */
  804. if ((err = lm90_read_reg(client, regh, &oldh))
  805. || (err = lm90_read_reg(client, regl, &l))
  806. || (err = lm90_read_reg(client, regh, &newh)))
  807. return err;
  808. if (oldh != newh) {
  809. err = lm90_read_reg(client, regl, &l);
  810. if (err)
  811. return err;
  812. }
  813. *value = (newh << 8) | l;
  814. return 0;
  815. }
  816. static struct lm90_data *lm90_update_device(struct device *dev)
  817. {
  818. struct i2c_client *client = to_i2c_client(dev);
  819. struct lm90_data *data = i2c_get_clientdata(client);
  820. mutex_lock(&data->update_lock);
  821. if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
  822. u8 h, l;
  823. dev_dbg(&client->dev, "Updating lm90 data.\n");
  824. lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
  825. lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
  826. lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
  827. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
  828. lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
  829. if (data->kind == max6657 || data->kind == max6646) {
  830. lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
  831. MAX6657_REG_R_LOCAL_TEMPL,
  832. &data->temp11[4]);
  833. } else {
  834. if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
  835. &h) == 0)
  836. data->temp11[4] = h << 8;
  837. }
  838. lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
  839. LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
  840. if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
  841. data->temp11[1] = h << 8;
  842. if (data->kind != max6657 && data->kind != max6680
  843. && data->kind != max6646
  844. && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
  845. &l) == 0)
  846. data->temp11[1] |= l;
  847. }
  848. if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
  849. data->temp11[2] = h << 8;
  850. if (data->kind != max6657 && data->kind != max6680
  851. && data->kind != max6646
  852. && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
  853. &l) == 0)
  854. data->temp11[2] |= l;
  855. }
  856. if (data->kind != max6657 && data->kind != max6646) {
  857. if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
  858. &h) == 0
  859. && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
  860. &l) == 0)
  861. data->temp11[3] = (h << 8) | l;
  862. }
  863. lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
  864. data->last_updated = jiffies;
  865. data->valid = 1;
  866. }
  867. mutex_unlock(&data->update_lock);
  868. return data;
  869. }
  870. static int __init sensors_lm90_init(void)
  871. {
  872. return i2c_add_driver(&lm90_driver);
  873. }
  874. static void __exit sensors_lm90_exit(void)
  875. {
  876. i2c_del_driver(&lm90_driver);
  877. }
  878. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  879. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  880. MODULE_LICENSE("GPL");
  881. module_init(sensors_lm90_init);
  882. module_exit(sensors_lm90_exit);