lm90.c 31 KB

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