lm90.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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. We use the device address to detect MAX6659, which will result
  32. * in a detection as max6657 if it is on address 0x4c. The extra address
  33. * and features of the MAX6659 are only supported if the chip is configured
  34. * explicitly as max6659, or if its address is not 0x4c.
  35. * These chips lack the remote temperature offset feature.
  36. *
  37. * This driver also supports the MAX6646, MAX6647, MAX6648, MAX6649 and
  38. * MAX6692 chips made by Maxim. These are again similar to the LM86,
  39. * but they use unsigned temperature values and can report temperatures
  40. * from 0 to 145 degrees.
  41. *
  42. * This driver also supports the MAX6680 and MAX6681, two other sensor
  43. * chips made by Maxim. These are quite similar to the other Maxim
  44. * chips. The MAX6680 and MAX6681 only differ in the pinout so they can
  45. * be treated identically.
  46. *
  47. * This driver also supports the MAX6695 and MAX6696, two other sensor
  48. * chips made by Maxim. These are also quite similar to other Maxim
  49. * chips, but support three temperature sensors instead of two. MAX6695
  50. * and MAX6696 only differ in the pinout so they can be treated identically.
  51. *
  52. * This driver also supports the ADT7461 chip from Analog Devices.
  53. * It's supported in both compatibility and extended mode. It is mostly
  54. * compatible with LM90 except for a data format difference for the
  55. * temperature value registers.
  56. *
  57. * Since the LM90 was the first chipset supported by this driver, most
  58. * comments will refer to this chipset, but are actually general and
  59. * concern all supported chipsets, unless mentioned otherwise.
  60. *
  61. * This program is free software; you can redistribute it and/or modify
  62. * it under the terms of the GNU General Public License as published by
  63. * the Free Software Foundation; either version 2 of the License, or
  64. * (at your option) any later version.
  65. *
  66. * This program is distributed in the hope that it will be useful,
  67. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  68. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  69. * GNU General Public License for more details.
  70. *
  71. * You should have received a copy of the GNU General Public License
  72. * along with this program; if not, write to the Free Software
  73. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  74. */
  75. #include <linux/module.h>
  76. #include <linux/init.h>
  77. #include <linux/slab.h>
  78. #include <linux/jiffies.h>
  79. #include <linux/i2c.h>
  80. #include <linux/hwmon-sysfs.h>
  81. #include <linux/hwmon.h>
  82. #include <linux/err.h>
  83. #include <linux/mutex.h>
  84. #include <linux/sysfs.h>
  85. /*
  86. * Addresses to scan
  87. * Address is fully defined internally and cannot be changed except for
  88. * MAX6659, MAX6680 and MAX6681.
  89. * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6649, MAX6657
  90. * and MAX6658 have address 0x4c.
  91. * ADM1032-2, ADT7461-2, LM89-1, LM99-1 and MAX6646 have address 0x4d.
  92. * MAX6647 has address 0x4e.
  93. * MAX6659 can have address 0x4c, 0x4d or 0x4e.
  94. * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
  95. * 0x4c, 0x4d or 0x4e.
  96. */
  97. static const unsigned short normal_i2c[] = {
  98. 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
  99. enum chips { lm90, adm1032, lm99, lm86, max6657, max6659, adt7461, max6680,
  100. max6646, w83l771, max6696 };
  101. /*
  102. * The LM90 registers
  103. */
  104. #define LM90_REG_R_MAN_ID 0xFE
  105. #define LM90_REG_R_CHIP_ID 0xFF
  106. #define LM90_REG_R_CONFIG1 0x03
  107. #define LM90_REG_W_CONFIG1 0x09
  108. #define LM90_REG_R_CONFIG2 0xBF
  109. #define LM90_REG_W_CONFIG2 0xBF
  110. #define LM90_REG_R_CONVRATE 0x04
  111. #define LM90_REG_W_CONVRATE 0x0A
  112. #define LM90_REG_R_STATUS 0x02
  113. #define LM90_REG_R_LOCAL_TEMP 0x00
  114. #define LM90_REG_R_LOCAL_HIGH 0x05
  115. #define LM90_REG_W_LOCAL_HIGH 0x0B
  116. #define LM90_REG_R_LOCAL_LOW 0x06
  117. #define LM90_REG_W_LOCAL_LOW 0x0C
  118. #define LM90_REG_R_LOCAL_CRIT 0x20
  119. #define LM90_REG_W_LOCAL_CRIT 0x20
  120. #define LM90_REG_R_REMOTE_TEMPH 0x01
  121. #define LM90_REG_R_REMOTE_TEMPL 0x10
  122. #define LM90_REG_R_REMOTE_OFFSH 0x11
  123. #define LM90_REG_W_REMOTE_OFFSH 0x11
  124. #define LM90_REG_R_REMOTE_OFFSL 0x12
  125. #define LM90_REG_W_REMOTE_OFFSL 0x12
  126. #define LM90_REG_R_REMOTE_HIGHH 0x07
  127. #define LM90_REG_W_REMOTE_HIGHH 0x0D
  128. #define LM90_REG_R_REMOTE_HIGHL 0x13
  129. #define LM90_REG_W_REMOTE_HIGHL 0x13
  130. #define LM90_REG_R_REMOTE_LOWH 0x08
  131. #define LM90_REG_W_REMOTE_LOWH 0x0E
  132. #define LM90_REG_R_REMOTE_LOWL 0x14
  133. #define LM90_REG_W_REMOTE_LOWL 0x14
  134. #define LM90_REG_R_REMOTE_CRIT 0x19
  135. #define LM90_REG_W_REMOTE_CRIT 0x19
  136. #define LM90_REG_R_TCRIT_HYST 0x21
  137. #define LM90_REG_W_TCRIT_HYST 0x21
  138. /* MAX6646/6647/6649/6657/6658/6659/6695/6696 registers */
  139. #define MAX6657_REG_R_LOCAL_TEMPL 0x11
  140. #define MAX6696_REG_R_STATUS2 0x12
  141. #define MAX6659_REG_R_REMOTE_EMERG 0x16
  142. #define MAX6659_REG_W_REMOTE_EMERG 0x16
  143. #define MAX6659_REG_R_LOCAL_EMERG 0x17
  144. #define MAX6659_REG_W_LOCAL_EMERG 0x17
  145. /*
  146. * Device flags
  147. */
  148. #define LM90_FLAG_ADT7461_EXT (1 << 0) /* ADT7461 extended mode */
  149. /* Device features */
  150. #define LM90_HAVE_OFFSET (1 << 1) /* temperature offset register */
  151. #define LM90_HAVE_LOCAL_EXT (1 << 2) /* extended local temperature */
  152. #define LM90_HAVE_REM_LIMIT_EXT (1 << 3) /* extended remote limit */
  153. #define LM90_HAVE_EMERGENCY (1 << 4) /* 3rd upper (emergency) limit */
  154. #define LM90_HAVE_EMERGENCY_ALARM (1 << 5)/* emergency alarm */
  155. #define LM90_HAVE_TEMP3 (1 << 6) /* 3rd temperature sensor */
  156. /*
  157. * Functions declaration
  158. */
  159. static int lm90_detect(struct i2c_client *client, struct i2c_board_info *info);
  160. static int lm90_probe(struct i2c_client *client,
  161. const struct i2c_device_id *id);
  162. static void lm90_init_client(struct i2c_client *client);
  163. static void lm90_alert(struct i2c_client *client, unsigned int flag);
  164. static int lm90_remove(struct i2c_client *client);
  165. static struct lm90_data *lm90_update_device(struct device *dev);
  166. static inline void lm90_select_remote_channel(struct i2c_client *client,
  167. struct lm90_data *data,
  168. int channel);
  169. /*
  170. * Driver data (common to all clients)
  171. */
  172. static const struct i2c_device_id lm90_id[] = {
  173. { "adm1032", adm1032 },
  174. { "adt7461", adt7461 },
  175. { "lm90", lm90 },
  176. { "lm86", lm86 },
  177. { "lm89", lm86 },
  178. { "lm99", lm99 },
  179. { "max6646", max6646 },
  180. { "max6647", max6646 },
  181. { "max6649", max6646 },
  182. { "max6657", max6657 },
  183. { "max6658", max6657 },
  184. { "max6659", max6659 },
  185. { "max6680", max6680 },
  186. { "max6681", max6680 },
  187. { "max6695", max6696 },
  188. { "max6696", max6696 },
  189. { "w83l771", w83l771 },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(i2c, lm90_id);
  193. static struct i2c_driver lm90_driver = {
  194. .class = I2C_CLASS_HWMON,
  195. .driver = {
  196. .name = "lm90",
  197. },
  198. .probe = lm90_probe,
  199. .remove = lm90_remove,
  200. .alert = lm90_alert,
  201. .id_table = lm90_id,
  202. .detect = lm90_detect,
  203. .address_list = normal_i2c,
  204. };
  205. /*
  206. * Client data (each client gets its own)
  207. */
  208. struct lm90_data {
  209. struct device *hwmon_dev;
  210. struct mutex update_lock;
  211. char valid; /* zero until following fields are valid */
  212. unsigned long last_updated; /* in jiffies */
  213. int kind;
  214. int flags;
  215. u8 config_orig; /* Original configuration register value */
  216. u16 alert_alarms; /* Which alarm bits trigger ALERT# */
  217. /* Upper 8 bits for max6695/96 */
  218. /* registers values */
  219. s8 temp8[8]; /* 0: local low limit
  220. 1: local high limit
  221. 2: local critical limit
  222. 3: remote critical limit
  223. 4: local emergency limit (max6659 and max6695/96)
  224. 5: remote emergency limit (max6659 and max6695/96)
  225. 6: remote 2 critical limit (max6695/96 only)
  226. 7: remote 2 emergency limit (max6695/96 only) */
  227. s16 temp11[8]; /* 0: remote input
  228. 1: remote low limit
  229. 2: remote high limit
  230. 3: remote offset (except max6646, max6657/58/59,
  231. and max6695/96)
  232. 4: local input
  233. 5: remote 2 input (max6695/96 only)
  234. 6: remote 2 low limit (max6695/96 only)
  235. 7: remote 2 high limit (ma6695/96 only) */
  236. u8 temp_hyst;
  237. u16 alarms; /* bitvector (upper 8 bits for max6695/96) */
  238. };
  239. /*
  240. * Conversions
  241. * For local temperatures and limits, critical limits and the hysteresis
  242. * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
  243. * For remote temperatures and limits, it uses signed 11-bit values with
  244. * LSB = 0.125 degree Celsius, left-justified in 16-bit registers. Some
  245. * Maxim chips use unsigned values.
  246. */
  247. static inline int temp_from_s8(s8 val)
  248. {
  249. return val * 1000;
  250. }
  251. static inline int temp_from_u8(u8 val)
  252. {
  253. return val * 1000;
  254. }
  255. static inline int temp_from_s16(s16 val)
  256. {
  257. return val / 32 * 125;
  258. }
  259. static inline int temp_from_u16(u16 val)
  260. {
  261. return val / 32 * 125;
  262. }
  263. static s8 temp_to_s8(long val)
  264. {
  265. if (val <= -128000)
  266. return -128;
  267. if (val >= 127000)
  268. return 127;
  269. if (val < 0)
  270. return (val - 500) / 1000;
  271. return (val + 500) / 1000;
  272. }
  273. static u8 temp_to_u8(long val)
  274. {
  275. if (val <= 0)
  276. return 0;
  277. if (val >= 255000)
  278. return 255;
  279. return (val + 500) / 1000;
  280. }
  281. static s16 temp_to_s16(long val)
  282. {
  283. if (val <= -128000)
  284. return 0x8000;
  285. if (val >= 127875)
  286. return 0x7FE0;
  287. if (val < 0)
  288. return (val - 62) / 125 * 32;
  289. return (val + 62) / 125 * 32;
  290. }
  291. static u8 hyst_to_reg(long val)
  292. {
  293. if (val <= 0)
  294. return 0;
  295. if (val >= 30500)
  296. return 31;
  297. return (val + 500) / 1000;
  298. }
  299. /*
  300. * ADT7461 in compatibility mode is almost identical to LM90 except that
  301. * attempts to write values that are outside the range 0 < temp < 127 are
  302. * treated as the boundary value.
  303. *
  304. * ADT7461 in "extended mode" operation uses unsigned integers offset by
  305. * 64 (e.g., 0 -> -64 degC). The range is restricted to -64..191 degC.
  306. */
  307. static inline int temp_from_u8_adt7461(struct lm90_data *data, u8 val)
  308. {
  309. if (data->flags & LM90_FLAG_ADT7461_EXT)
  310. return (val - 64) * 1000;
  311. else
  312. return temp_from_s8(val);
  313. }
  314. static inline int temp_from_u16_adt7461(struct lm90_data *data, u16 val)
  315. {
  316. if (data->flags & LM90_FLAG_ADT7461_EXT)
  317. return (val - 0x4000) / 64 * 250;
  318. else
  319. return temp_from_s16(val);
  320. }
  321. static u8 temp_to_u8_adt7461(struct lm90_data *data, long val)
  322. {
  323. if (data->flags & LM90_FLAG_ADT7461_EXT) {
  324. if (val <= -64000)
  325. return 0;
  326. if (val >= 191000)
  327. return 0xFF;
  328. return (val + 500 + 64000) / 1000;
  329. } else {
  330. if (val <= 0)
  331. return 0;
  332. if (val >= 127000)
  333. return 127;
  334. return (val + 500) / 1000;
  335. }
  336. }
  337. static u16 temp_to_u16_adt7461(struct lm90_data *data, long val)
  338. {
  339. if (data->flags & LM90_FLAG_ADT7461_EXT) {
  340. if (val <= -64000)
  341. return 0;
  342. if (val >= 191750)
  343. return 0xFFC0;
  344. return (val + 64000 + 125) / 250 * 64;
  345. } else {
  346. if (val <= 0)
  347. return 0;
  348. if (val >= 127750)
  349. return 0x7FC0;
  350. return (val + 125) / 250 * 64;
  351. }
  352. }
  353. /*
  354. * Sysfs stuff
  355. */
  356. static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
  357. char *buf)
  358. {
  359. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  360. struct lm90_data *data = lm90_update_device(dev);
  361. int temp;
  362. if (data->kind == adt7461)
  363. temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
  364. else if (data->kind == max6646)
  365. temp = temp_from_u8(data->temp8[attr->index]);
  366. else
  367. temp = temp_from_s8(data->temp8[attr->index]);
  368. /* +16 degrees offset for temp2 for the LM99 */
  369. if (data->kind == lm99 && attr->index == 3)
  370. temp += 16000;
  371. return sprintf(buf, "%d\n", temp);
  372. }
  373. static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
  374. const char *buf, size_t count)
  375. {
  376. static const u8 reg[8] = {
  377. LM90_REG_W_LOCAL_LOW,
  378. LM90_REG_W_LOCAL_HIGH,
  379. LM90_REG_W_LOCAL_CRIT,
  380. LM90_REG_W_REMOTE_CRIT,
  381. MAX6659_REG_W_LOCAL_EMERG,
  382. MAX6659_REG_W_REMOTE_EMERG,
  383. LM90_REG_W_REMOTE_CRIT,
  384. MAX6659_REG_W_REMOTE_EMERG,
  385. };
  386. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  387. struct i2c_client *client = to_i2c_client(dev);
  388. struct lm90_data *data = i2c_get_clientdata(client);
  389. int nr = attr->index;
  390. long val;
  391. int err;
  392. err = strict_strtol(buf, 10, &val);
  393. if (err < 0)
  394. return err;
  395. /* +16 degrees offset for temp2 for the LM99 */
  396. if (data->kind == lm99 && attr->index == 3)
  397. val -= 16000;
  398. mutex_lock(&data->update_lock);
  399. if (data->kind == adt7461)
  400. data->temp8[nr] = temp_to_u8_adt7461(data, val);
  401. else if (data->kind == max6646)
  402. data->temp8[nr] = temp_to_u8(val);
  403. else
  404. data->temp8[nr] = temp_to_s8(val);
  405. lm90_select_remote_channel(client, data, nr >= 6);
  406. i2c_smbus_write_byte_data(client, reg[nr], data->temp8[nr]);
  407. lm90_select_remote_channel(client, data, 0);
  408. mutex_unlock(&data->update_lock);
  409. return count;
  410. }
  411. static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
  412. char *buf)
  413. {
  414. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  415. struct lm90_data *data = lm90_update_device(dev);
  416. int temp;
  417. if (data->kind == adt7461)
  418. temp = temp_from_u16_adt7461(data, data->temp11[attr->index]);
  419. else if (data->kind == max6646)
  420. temp = temp_from_u16(data->temp11[attr->index]);
  421. else
  422. temp = temp_from_s16(data->temp11[attr->index]);
  423. /* +16 degrees offset for temp2 for the LM99 */
  424. if (data->kind == lm99 && attr->index <= 2)
  425. temp += 16000;
  426. return sprintf(buf, "%d\n", temp);
  427. }
  428. static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
  429. const char *buf, size_t count)
  430. {
  431. struct {
  432. u8 high;
  433. u8 low;
  434. int channel;
  435. } reg[5] = {
  436. { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 0 },
  437. { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 0 },
  438. { LM90_REG_W_REMOTE_OFFSH, LM90_REG_W_REMOTE_OFFSL, 0 },
  439. { LM90_REG_W_REMOTE_LOWH, LM90_REG_W_REMOTE_LOWL, 1 },
  440. { LM90_REG_W_REMOTE_HIGHH, LM90_REG_W_REMOTE_HIGHL, 1 }
  441. };
  442. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(devattr);
  443. struct i2c_client *client = to_i2c_client(dev);
  444. struct lm90_data *data = i2c_get_clientdata(client);
  445. int nr = attr->nr;
  446. int index = attr->index;
  447. long val;
  448. int err;
  449. err = strict_strtol(buf, 10, &val);
  450. if (err < 0)
  451. return err;
  452. /* +16 degrees offset for temp2 for the LM99 */
  453. if (data->kind == lm99 && index <= 2)
  454. val -= 16000;
  455. mutex_lock(&data->update_lock);
  456. if (data->kind == adt7461)
  457. data->temp11[index] = temp_to_u16_adt7461(data, val);
  458. else if (data->kind == max6646)
  459. data->temp11[index] = temp_to_u8(val) << 8;
  460. else if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
  461. data->temp11[index] = temp_to_s16(val);
  462. else
  463. data->temp11[index] = temp_to_s8(val) << 8;
  464. lm90_select_remote_channel(client, data, reg[nr].channel);
  465. i2c_smbus_write_byte_data(client, reg[nr].high,
  466. data->temp11[index] >> 8);
  467. if (data->flags & LM90_HAVE_REM_LIMIT_EXT)
  468. i2c_smbus_write_byte_data(client, reg[nr].low,
  469. data->temp11[index] & 0xff);
  470. lm90_select_remote_channel(client, data, 0);
  471. mutex_unlock(&data->update_lock);
  472. return count;
  473. }
  474. static ssize_t show_temphyst(struct device *dev,
  475. struct device_attribute *devattr,
  476. char *buf)
  477. {
  478. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  479. struct lm90_data *data = lm90_update_device(dev);
  480. int temp;
  481. if (data->kind == adt7461)
  482. temp = temp_from_u8_adt7461(data, data->temp8[attr->index]);
  483. else if (data->kind == max6646)
  484. temp = temp_from_u8(data->temp8[attr->index]);
  485. else
  486. temp = temp_from_s8(data->temp8[attr->index]);
  487. /* +16 degrees offset for temp2 for the LM99 */
  488. if (data->kind == lm99 && attr->index == 3)
  489. temp += 16000;
  490. return sprintf(buf, "%d\n", temp - temp_from_s8(data->temp_hyst));
  491. }
  492. static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
  493. const char *buf, size_t count)
  494. {
  495. struct i2c_client *client = to_i2c_client(dev);
  496. struct lm90_data *data = i2c_get_clientdata(client);
  497. long val;
  498. int err;
  499. int temp;
  500. err = strict_strtol(buf, 10, &val);
  501. if (err < 0)
  502. return err;
  503. mutex_lock(&data->update_lock);
  504. if (data->kind == adt7461)
  505. temp = temp_from_u8_adt7461(data, data->temp8[2]);
  506. else if (data->kind == max6646)
  507. temp = temp_from_u8(data->temp8[2]);
  508. else
  509. temp = temp_from_s8(data->temp8[2]);
  510. data->temp_hyst = hyst_to_reg(temp - val);
  511. i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
  512. data->temp_hyst);
  513. mutex_unlock(&data->update_lock);
  514. return count;
  515. }
  516. static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
  517. char *buf)
  518. {
  519. struct lm90_data *data = lm90_update_device(dev);
  520. return sprintf(buf, "%d\n", data->alarms);
  521. }
  522. static ssize_t show_alarm(struct device *dev, struct device_attribute
  523. *devattr, char *buf)
  524. {
  525. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  526. struct lm90_data *data = lm90_update_device(dev);
  527. int bitnr = attr->index;
  528. return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
  529. }
  530. static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp11, NULL, 0, 4);
  531. static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp11, NULL, 0, 0);
  532. static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
  533. set_temp8, 0);
  534. static SENSOR_DEVICE_ATTR_2(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
  535. set_temp11, 0, 1);
  536. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
  537. set_temp8, 1);
  538. static SENSOR_DEVICE_ATTR_2(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
  539. set_temp11, 1, 2);
  540. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
  541. set_temp8, 2);
  542. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
  543. set_temp8, 3);
  544. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
  545. set_temphyst, 2);
  546. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 3);
  547. static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
  548. set_temp11, 2, 3);
  549. /* Individual alarm files */
  550. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
  551. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
  552. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
  553. static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
  554. static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
  555. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
  556. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
  557. /* Raw alarm file for compatibility */
  558. static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
  559. static struct attribute *lm90_attributes[] = {
  560. &sensor_dev_attr_temp1_input.dev_attr.attr,
  561. &sensor_dev_attr_temp2_input.dev_attr.attr,
  562. &sensor_dev_attr_temp1_min.dev_attr.attr,
  563. &sensor_dev_attr_temp2_min.dev_attr.attr,
  564. &sensor_dev_attr_temp1_max.dev_attr.attr,
  565. &sensor_dev_attr_temp2_max.dev_attr.attr,
  566. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  567. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  568. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  569. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  570. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  571. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  572. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  573. &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
  574. &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
  575. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  576. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  577. &dev_attr_alarms.attr,
  578. NULL
  579. };
  580. static const struct attribute_group lm90_group = {
  581. .attrs = lm90_attributes,
  582. };
  583. /*
  584. * Additional attributes for devices with emergency sensors
  585. */
  586. static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO, show_temp8,
  587. set_temp8, 4);
  588. static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO, show_temp8,
  589. set_temp8, 5);
  590. static SENSOR_DEVICE_ATTR(temp1_emergency_hyst, S_IRUGO, show_temphyst,
  591. NULL, 4);
  592. static SENSOR_DEVICE_ATTR(temp2_emergency_hyst, S_IRUGO, show_temphyst,
  593. NULL, 5);
  594. static struct attribute *lm90_emergency_attributes[] = {
  595. &sensor_dev_attr_temp1_emergency.dev_attr.attr,
  596. &sensor_dev_attr_temp2_emergency.dev_attr.attr,
  597. &sensor_dev_attr_temp1_emergency_hyst.dev_attr.attr,
  598. &sensor_dev_attr_temp2_emergency_hyst.dev_attr.attr,
  599. NULL
  600. };
  601. static const struct attribute_group lm90_emergency_group = {
  602. .attrs = lm90_emergency_attributes,
  603. };
  604. static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 15);
  605. static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 13);
  606. static struct attribute *lm90_emergency_alarm_attributes[] = {
  607. &sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
  608. &sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
  609. NULL
  610. };
  611. static const struct attribute_group lm90_emergency_alarm_group = {
  612. .attrs = lm90_emergency_alarm_attributes,
  613. };
  614. /*
  615. * Additional attributes for devices with 3 temperature sensors
  616. */
  617. static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp11, NULL, 0, 5);
  618. static SENSOR_DEVICE_ATTR_2(temp3_min, S_IWUSR | S_IRUGO, show_temp11,
  619. set_temp11, 3, 6);
  620. static SENSOR_DEVICE_ATTR_2(temp3_max, S_IWUSR | S_IRUGO, show_temp11,
  621. set_temp11, 4, 7);
  622. static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp8,
  623. set_temp8, 6);
  624. static SENSOR_DEVICE_ATTR(temp3_crit_hyst, S_IRUGO, show_temphyst, NULL, 6);
  625. static SENSOR_DEVICE_ATTR(temp3_emergency, S_IWUSR | S_IRUGO, show_temp8,
  626. set_temp8, 7);
  627. static SENSOR_DEVICE_ATTR(temp3_emergency_hyst, S_IRUGO, show_temphyst,
  628. NULL, 7);
  629. static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
  630. static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 10);
  631. static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_alarm, NULL, 11);
  632. static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 12);
  633. static SENSOR_DEVICE_ATTR(temp3_emergency_alarm, S_IRUGO, show_alarm, NULL, 14);
  634. static struct attribute *lm90_temp3_attributes[] = {
  635. &sensor_dev_attr_temp3_input.dev_attr.attr,
  636. &sensor_dev_attr_temp3_min.dev_attr.attr,
  637. &sensor_dev_attr_temp3_max.dev_attr.attr,
  638. &sensor_dev_attr_temp3_crit.dev_attr.attr,
  639. &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
  640. &sensor_dev_attr_temp3_emergency.dev_attr.attr,
  641. &sensor_dev_attr_temp3_emergency_hyst.dev_attr.attr,
  642. &sensor_dev_attr_temp3_fault.dev_attr.attr,
  643. &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
  644. &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
  645. &sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
  646. &sensor_dev_attr_temp3_emergency_alarm.dev_attr.attr,
  647. NULL
  648. };
  649. static const struct attribute_group lm90_temp3_group = {
  650. .attrs = lm90_temp3_attributes,
  651. };
  652. /* pec used for ADM1032 only */
  653. static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
  654. char *buf)
  655. {
  656. struct i2c_client *client = to_i2c_client(dev);
  657. return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
  658. }
  659. static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
  660. const char *buf, size_t count)
  661. {
  662. struct i2c_client *client = to_i2c_client(dev);
  663. long val;
  664. int err;
  665. err = strict_strtol(buf, 10, &val);
  666. if (err < 0)
  667. return err;
  668. switch (val) {
  669. case 0:
  670. client->flags &= ~I2C_CLIENT_PEC;
  671. break;
  672. case 1:
  673. client->flags |= I2C_CLIENT_PEC;
  674. break;
  675. default:
  676. return -EINVAL;
  677. }
  678. return count;
  679. }
  680. static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
  681. /*
  682. * Real code
  683. */
  684. /*
  685. * The ADM1032 supports PEC but not on write byte transactions, so we need
  686. * to explicitly ask for a transaction without PEC.
  687. */
  688. static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
  689. {
  690. return i2c_smbus_xfer(client->adapter, client->addr,
  691. client->flags & ~I2C_CLIENT_PEC,
  692. I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
  693. }
  694. /*
  695. * It is assumed that client->update_lock is held (unless we are in
  696. * detection or initialization steps). This matters when PEC is enabled,
  697. * because we don't want the address pointer to change between the write
  698. * byte and the read byte transactions.
  699. */
  700. static int lm90_read_reg(struct i2c_client *client, u8 reg, u8 *value)
  701. {
  702. int err;
  703. if (client->flags & I2C_CLIENT_PEC) {
  704. err = adm1032_write_byte(client, reg);
  705. if (err >= 0)
  706. err = i2c_smbus_read_byte(client);
  707. } else
  708. err = i2c_smbus_read_byte_data(client, reg);
  709. if (err < 0) {
  710. dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
  711. reg, err);
  712. return err;
  713. }
  714. *value = err;
  715. return 0;
  716. }
  717. /*
  718. * client->update_lock must be held when calling this function (unless we are
  719. * in detection or initialization steps), and while a remote channel other
  720. * than channel 0 is selected. Also, calling code must make sure to re-select
  721. * external channel 0 before releasing the lock. This is necessary because
  722. * various registers have different meanings as a result of selecting a
  723. * non-default remote channel.
  724. */
  725. static inline void lm90_select_remote_channel(struct i2c_client *client,
  726. struct lm90_data *data,
  727. int channel)
  728. {
  729. u8 config;
  730. if (data->kind == max6696) {
  731. lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
  732. config &= ~0x08;
  733. if (channel)
  734. config |= 0x08;
  735. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  736. config);
  737. }
  738. }
  739. /* Return 0 if detection is successful, -ENODEV otherwise */
  740. static int lm90_detect(struct i2c_client *new_client,
  741. struct i2c_board_info *info)
  742. {
  743. struct i2c_adapter *adapter = new_client->adapter;
  744. int address = new_client->addr;
  745. const char *name = NULL;
  746. int man_id, chip_id, reg_config1, reg_convrate;
  747. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  748. return -ENODEV;
  749. /* detection and identification */
  750. if ((man_id = i2c_smbus_read_byte_data(new_client,
  751. LM90_REG_R_MAN_ID)) < 0
  752. || (chip_id = i2c_smbus_read_byte_data(new_client,
  753. LM90_REG_R_CHIP_ID)) < 0
  754. || (reg_config1 = i2c_smbus_read_byte_data(new_client,
  755. LM90_REG_R_CONFIG1)) < 0
  756. || (reg_convrate = i2c_smbus_read_byte_data(new_client,
  757. LM90_REG_R_CONVRATE)) < 0)
  758. return -ENODEV;
  759. if ((address == 0x4C || address == 0x4D)
  760. && man_id == 0x01) { /* National Semiconductor */
  761. int reg_config2;
  762. reg_config2 = i2c_smbus_read_byte_data(new_client,
  763. LM90_REG_R_CONFIG2);
  764. if (reg_config2 < 0)
  765. return -ENODEV;
  766. if ((reg_config1 & 0x2A) == 0x00
  767. && (reg_config2 & 0xF8) == 0x00
  768. && reg_convrate <= 0x09) {
  769. if (address == 0x4C
  770. && (chip_id & 0xF0) == 0x20) { /* LM90 */
  771. name = "lm90";
  772. } else
  773. if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
  774. name = "lm99";
  775. dev_info(&adapter->dev,
  776. "Assuming LM99 chip at 0x%02x\n",
  777. address);
  778. dev_info(&adapter->dev,
  779. "If it is an LM89, instantiate it "
  780. "with the new_device sysfs "
  781. "interface\n");
  782. } else
  783. if (address == 0x4C
  784. && (chip_id & 0xF0) == 0x10) { /* LM86 */
  785. name = "lm86";
  786. }
  787. }
  788. } else
  789. if ((address == 0x4C || address == 0x4D)
  790. && man_id == 0x41) { /* Analog Devices */
  791. if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
  792. && (reg_config1 & 0x3F) == 0x00
  793. && reg_convrate <= 0x0A) {
  794. name = "adm1032";
  795. /* The ADM1032 supports PEC, but only if combined
  796. transactions are not used. */
  797. if (i2c_check_functionality(adapter,
  798. I2C_FUNC_SMBUS_BYTE))
  799. info->flags |= I2C_CLIENT_PEC;
  800. } else
  801. if (chip_id == 0x51 /* ADT7461 */
  802. && (reg_config1 & 0x1B) == 0x00
  803. && reg_convrate <= 0x0A) {
  804. name = "adt7461";
  805. }
  806. } else
  807. if (man_id == 0x4D) { /* Maxim */
  808. int reg_emerg, reg_emerg2, reg_status2;
  809. /*
  810. * We read MAX6659_REG_R_REMOTE_EMERG twice, and re-read
  811. * LM90_REG_R_MAN_ID in between. If MAX6659_REG_R_REMOTE_EMERG
  812. * exists, both readings will reflect the same value. Otherwise,
  813. * the readings will be different.
  814. */
  815. if ((reg_emerg = i2c_smbus_read_byte_data(new_client,
  816. MAX6659_REG_R_REMOTE_EMERG)) < 0
  817. || i2c_smbus_read_byte_data(new_client, LM90_REG_R_MAN_ID) < 0
  818. || (reg_emerg2 = i2c_smbus_read_byte_data(new_client,
  819. MAX6659_REG_R_REMOTE_EMERG)) < 0
  820. || (reg_status2 = i2c_smbus_read_byte_data(new_client,
  821. MAX6696_REG_R_STATUS2)) < 0)
  822. return -ENODEV;
  823. /*
  824. * The MAX6657, MAX6658 and MAX6659 do NOT have a chip_id
  825. * register. Reading from that address will return the last
  826. * read value, which in our case is those of the man_id
  827. * register. Likewise, the config1 register seems to lack a
  828. * low nibble, so the value will be those of the previous
  829. * read, so in our case those of the man_id register.
  830. * MAX6659 has a third set of upper temperature limit registers.
  831. * Those registers also return values on MAX6657 and MAX6658,
  832. * thus the only way to detect MAX6659 is by its address.
  833. * For this reason it will be mis-detected as MAX6657 if its
  834. * address is 0x4C.
  835. */
  836. if (chip_id == man_id
  837. && (address == 0x4C || address == 0x4D || address == 0x4E)
  838. && (reg_config1 & 0x1F) == (man_id & 0x0F)
  839. && reg_convrate <= 0x09) {
  840. if (address == 0x4C)
  841. name = "max6657";
  842. else
  843. name = "max6659";
  844. } else
  845. /*
  846. * Even though MAX6695 and MAX6696 do not have a chip ID
  847. * register, reading it returns 0x01. Bit 4 of the config1
  848. * register is unused and should return zero when read. Bit 0 of
  849. * the status2 register is unused and should return zero when
  850. * read.
  851. *
  852. * MAX6695 and MAX6696 have an additional set of temperature
  853. * limit registers. We can detect those chips by checking if
  854. * one of those registers exists.
  855. */
  856. if (chip_id == 0x01
  857. && (reg_config1 & 0x10) == 0x00
  858. && (reg_status2 & 0x01) == 0x00
  859. && reg_emerg == reg_emerg2
  860. && reg_convrate <= 0x07) {
  861. name = "max6696";
  862. } else
  863. /*
  864. * The chip_id register of the MAX6680 and MAX6681 holds the
  865. * revision of the chip. The lowest bit of the config1 register
  866. * is unused and should return zero when read, so should the
  867. * second to last bit of config1 (software reset).
  868. */
  869. if (chip_id == 0x01
  870. && (reg_config1 & 0x03) == 0x00
  871. && reg_convrate <= 0x07) {
  872. name = "max6680";
  873. } else
  874. /*
  875. * The chip_id register of the MAX6646/6647/6649 holds the
  876. * revision of the chip. The lowest 6 bits of the config1
  877. * register are unused and should return zero when read.
  878. */
  879. if (chip_id == 0x59
  880. && (reg_config1 & 0x3f) == 0x00
  881. && reg_convrate <= 0x07) {
  882. name = "max6646";
  883. }
  884. } else
  885. if (address == 0x4C
  886. && man_id == 0x5C) { /* Winbond/Nuvoton */
  887. if ((chip_id & 0xFE) == 0x10 /* W83L771AWG/ASG */
  888. && (reg_config1 & 0x2A) == 0x00
  889. && reg_convrate <= 0x08) {
  890. name = "w83l771";
  891. }
  892. }
  893. if (!name) { /* identification failed */
  894. dev_dbg(&adapter->dev,
  895. "Unsupported chip at 0x%02x (man_id=0x%02X, "
  896. "chip_id=0x%02X)\n", address, man_id, chip_id);
  897. return -ENODEV;
  898. }
  899. strlcpy(info->type, name, I2C_NAME_SIZE);
  900. return 0;
  901. }
  902. static void lm90_remove_files(struct i2c_client *client, struct lm90_data *data)
  903. {
  904. if (data->flags & LM90_HAVE_TEMP3)
  905. sysfs_remove_group(&client->dev.kobj, &lm90_temp3_group);
  906. if (data->flags & LM90_HAVE_EMERGENCY_ALARM)
  907. sysfs_remove_group(&client->dev.kobj,
  908. &lm90_emergency_alarm_group);
  909. if (data->flags & LM90_HAVE_EMERGENCY)
  910. sysfs_remove_group(&client->dev.kobj,
  911. &lm90_emergency_group);
  912. if (data->flags & LM90_HAVE_OFFSET)
  913. device_remove_file(&client->dev,
  914. &sensor_dev_attr_temp2_offset.dev_attr);
  915. device_remove_file(&client->dev, &dev_attr_pec);
  916. sysfs_remove_group(&client->dev.kobj, &lm90_group);
  917. }
  918. static int lm90_probe(struct i2c_client *new_client,
  919. const struct i2c_device_id *id)
  920. {
  921. struct i2c_adapter *adapter = to_i2c_adapter(new_client->dev.parent);
  922. struct lm90_data *data;
  923. int err;
  924. data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL);
  925. if (!data) {
  926. err = -ENOMEM;
  927. goto exit;
  928. }
  929. i2c_set_clientdata(new_client, data);
  930. mutex_init(&data->update_lock);
  931. /* Set the device type */
  932. data->kind = id->driver_data;
  933. if (data->kind == adm1032) {
  934. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
  935. new_client->flags &= ~I2C_CLIENT_PEC;
  936. }
  937. /* Different devices have different alarm bits triggering the
  938. * ALERT# output */
  939. switch (data->kind) {
  940. case lm90:
  941. case lm99:
  942. case lm86:
  943. data->alert_alarms = 0x7b;
  944. break;
  945. case max6696:
  946. data->alert_alarms = 0x187c;
  947. break;
  948. default:
  949. data->alert_alarms = 0x7c;
  950. break;
  951. }
  952. /* Set chip capabilities */
  953. if (data->kind != max6657 && data->kind != max6659
  954. && data->kind != max6646 && data->kind != max6696)
  955. data->flags |= LM90_HAVE_OFFSET;
  956. if (data->kind == max6657 || data->kind == max6659
  957. || data->kind == max6646 || data->kind == max6696)
  958. data->flags |= LM90_HAVE_LOCAL_EXT;
  959. if (data->kind != max6657 && data->kind != max6659
  960. && data->kind != max6646 && data->kind != max6680
  961. && data->kind != max6696)
  962. data->flags |= LM90_HAVE_REM_LIMIT_EXT;
  963. if (data->kind == max6659 || data->kind == max6696)
  964. data->flags |= LM90_HAVE_EMERGENCY;
  965. if (data->kind == max6696)
  966. data->flags |= LM90_HAVE_EMERGENCY_ALARM | LM90_HAVE_TEMP3;
  967. /* Initialize the LM90 chip */
  968. lm90_init_client(new_client);
  969. /* Register sysfs hooks */
  970. err = sysfs_create_group(&new_client->dev.kobj, &lm90_group);
  971. if (err)
  972. goto exit_free;
  973. if (new_client->flags & I2C_CLIENT_PEC) {
  974. err = device_create_file(&new_client->dev, &dev_attr_pec);
  975. if (err)
  976. goto exit_remove_files;
  977. }
  978. if (data->flags & LM90_HAVE_OFFSET) {
  979. err = device_create_file(&new_client->dev,
  980. &sensor_dev_attr_temp2_offset.dev_attr);
  981. if (err)
  982. goto exit_remove_files;
  983. }
  984. if (data->flags & LM90_HAVE_EMERGENCY) {
  985. err = sysfs_create_group(&new_client->dev.kobj,
  986. &lm90_emergency_group);
  987. if (err)
  988. goto exit_remove_files;
  989. }
  990. if (data->flags & LM90_HAVE_EMERGENCY_ALARM) {
  991. err = sysfs_create_group(&new_client->dev.kobj,
  992. &lm90_emergency_alarm_group);
  993. if (err)
  994. goto exit_remove_files;
  995. }
  996. if (data->flags & LM90_HAVE_TEMP3) {
  997. err = sysfs_create_group(&new_client->dev.kobj,
  998. &lm90_temp3_group);
  999. if (err)
  1000. goto exit_remove_files;
  1001. }
  1002. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  1003. if (IS_ERR(data->hwmon_dev)) {
  1004. err = PTR_ERR(data->hwmon_dev);
  1005. goto exit_remove_files;
  1006. }
  1007. return 0;
  1008. exit_remove_files:
  1009. lm90_remove_files(new_client, data);
  1010. exit_free:
  1011. kfree(data);
  1012. exit:
  1013. return err;
  1014. }
  1015. static void lm90_init_client(struct i2c_client *client)
  1016. {
  1017. u8 config;
  1018. struct lm90_data *data = i2c_get_clientdata(client);
  1019. /*
  1020. * Start the conversions.
  1021. */
  1022. i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
  1023. 5); /* 2 Hz */
  1024. if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
  1025. dev_warn(&client->dev, "Initialization failed!\n");
  1026. return;
  1027. }
  1028. data->config_orig = config;
  1029. /* Check Temperature Range Select */
  1030. if (data->kind == adt7461) {
  1031. if (config & 0x04)
  1032. data->flags |= LM90_FLAG_ADT7461_EXT;
  1033. }
  1034. /*
  1035. * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
  1036. * 0.125 degree resolution) and range (0x08, extend range
  1037. * to -64 degree) mode for the remote temperature sensor.
  1038. */
  1039. if (data->kind == max6680)
  1040. config |= 0x18;
  1041. /*
  1042. * Select external channel 0 for max6695/96
  1043. */
  1044. if (data->kind == max6696)
  1045. config &= ~0x08;
  1046. config &= 0xBF; /* run */
  1047. if (config != data->config_orig) /* Only write if changed */
  1048. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
  1049. }
  1050. static int lm90_remove(struct i2c_client *client)
  1051. {
  1052. struct lm90_data *data = i2c_get_clientdata(client);
  1053. hwmon_device_unregister(data->hwmon_dev);
  1054. lm90_remove_files(client, data);
  1055. /* Restore initial configuration */
  1056. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  1057. data->config_orig);
  1058. kfree(data);
  1059. return 0;
  1060. }
  1061. static void lm90_alert(struct i2c_client *client, unsigned int flag)
  1062. {
  1063. struct lm90_data *data = i2c_get_clientdata(client);
  1064. u8 config, alarms, alarms2 = 0;
  1065. lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
  1066. if (data->kind == max6696)
  1067. lm90_read_reg(client, MAX6696_REG_R_STATUS2, &alarms2);
  1068. if ((alarms & 0x7f) == 0 && (alarms2 & 0xfe) == 0) {
  1069. dev_info(&client->dev, "Everything OK\n");
  1070. } else {
  1071. if (alarms & 0x61)
  1072. dev_warn(&client->dev,
  1073. "temp%d out of range, please check!\n", 1);
  1074. if (alarms & 0x1a)
  1075. dev_warn(&client->dev,
  1076. "temp%d out of range, please check!\n", 2);
  1077. if (alarms & 0x04)
  1078. dev_warn(&client->dev,
  1079. "temp%d diode open, please check!\n", 2);
  1080. if (alarms2 & 0x18)
  1081. dev_warn(&client->dev,
  1082. "temp%d out of range, please check!\n", 3);
  1083. /* Disable ALERT# output, because these chips don't implement
  1084. SMBus alert correctly; they should only hold the alert line
  1085. low briefly. */
  1086. if ((data->kind == adm1032 || data->kind == adt7461)
  1087. && (alarms & data->alert_alarms)) {
  1088. dev_dbg(&client->dev, "Disabling ALERT#\n");
  1089. lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
  1090. i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
  1091. config | 0x80);
  1092. }
  1093. }
  1094. }
  1095. static int lm90_read16(struct i2c_client *client, u8 regh, u8 regl, u16 *value)
  1096. {
  1097. int err;
  1098. u8 oldh, newh, l;
  1099. /*
  1100. * There is a trick here. We have to read two registers to have the
  1101. * sensor temperature, but we have to beware a conversion could occur
  1102. * inbetween the readings. The datasheet says we should either use
  1103. * the one-shot conversion register, which we don't want to do
  1104. * (disables hardware monitoring) or monitor the busy bit, which is
  1105. * impossible (we can't read the values and monitor that bit at the
  1106. * exact same time). So the solution used here is to read the high
  1107. * byte once, then the low byte, then the high byte again. If the new
  1108. * high byte matches the old one, then we have a valid reading. Else
  1109. * we have to read the low byte again, and now we believe we have a
  1110. * correct reading.
  1111. */
  1112. if ((err = lm90_read_reg(client, regh, &oldh))
  1113. || (err = lm90_read_reg(client, regl, &l))
  1114. || (err = lm90_read_reg(client, regh, &newh)))
  1115. return err;
  1116. if (oldh != newh) {
  1117. err = lm90_read_reg(client, regl, &l);
  1118. if (err)
  1119. return err;
  1120. }
  1121. *value = (newh << 8) | l;
  1122. return 0;
  1123. }
  1124. static struct lm90_data *lm90_update_device(struct device *dev)
  1125. {
  1126. struct i2c_client *client = to_i2c_client(dev);
  1127. struct lm90_data *data = i2c_get_clientdata(client);
  1128. mutex_lock(&data->update_lock);
  1129. if (time_after(jiffies, data->last_updated + HZ / 2 + HZ / 10)
  1130. || !data->valid) {
  1131. u8 h, l;
  1132. u8 alarms;
  1133. dev_dbg(&client->dev, "Updating lm90 data.\n");
  1134. lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[0]);
  1135. lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[1]);
  1136. lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[2]);
  1137. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[3]);
  1138. lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
  1139. if (data->flags & LM90_HAVE_LOCAL_EXT) {
  1140. lm90_read16(client, LM90_REG_R_LOCAL_TEMP,
  1141. MAX6657_REG_R_LOCAL_TEMPL,
  1142. &data->temp11[4]);
  1143. } else {
  1144. if (lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
  1145. &h) == 0)
  1146. data->temp11[4] = h << 8;
  1147. }
  1148. lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
  1149. LM90_REG_R_REMOTE_TEMPL, &data->temp11[0]);
  1150. if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h) == 0) {
  1151. data->temp11[1] = h << 8;
  1152. if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
  1153. && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL,
  1154. &l) == 0)
  1155. data->temp11[1] |= l;
  1156. }
  1157. if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h) == 0) {
  1158. data->temp11[2] = h << 8;
  1159. if ((data->flags & LM90_HAVE_REM_LIMIT_EXT)
  1160. && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL,
  1161. &l) == 0)
  1162. data->temp11[2] |= l;
  1163. }
  1164. if (data->flags & LM90_HAVE_OFFSET) {
  1165. if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
  1166. &h) == 0
  1167. && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
  1168. &l) == 0)
  1169. data->temp11[3] = (h << 8) | l;
  1170. }
  1171. if (data->flags & LM90_HAVE_EMERGENCY) {
  1172. lm90_read_reg(client, MAX6659_REG_R_LOCAL_EMERG,
  1173. &data->temp8[4]);
  1174. lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
  1175. &data->temp8[5]);
  1176. }
  1177. lm90_read_reg(client, LM90_REG_R_STATUS, &alarms);
  1178. data->alarms = alarms; /* save as 16 bit value */
  1179. if (data->kind == max6696) {
  1180. lm90_select_remote_channel(client, data, 1);
  1181. lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
  1182. &data->temp8[6]);
  1183. lm90_read_reg(client, MAX6659_REG_R_REMOTE_EMERG,
  1184. &data->temp8[7]);
  1185. lm90_read16(client, LM90_REG_R_REMOTE_TEMPH,
  1186. LM90_REG_R_REMOTE_TEMPL, &data->temp11[5]);
  1187. if (!lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &h))
  1188. data->temp11[6] = h << 8;
  1189. if (!lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &h))
  1190. data->temp11[7] = h << 8;
  1191. lm90_select_remote_channel(client, data, 0);
  1192. if (!lm90_read_reg(client, MAX6696_REG_R_STATUS2,
  1193. &alarms))
  1194. data->alarms |= alarms << 8;
  1195. }
  1196. /* Re-enable ALERT# output if it was originally enabled and
  1197. * relevant alarms are all clear */
  1198. if ((data->config_orig & 0x80) == 0
  1199. && (data->alarms & data->alert_alarms) == 0) {
  1200. u8 config;
  1201. lm90_read_reg(client, LM90_REG_R_CONFIG1, &config);
  1202. if (config & 0x80) {
  1203. dev_dbg(&client->dev, "Re-enabling ALERT#\n");
  1204. i2c_smbus_write_byte_data(client,
  1205. LM90_REG_W_CONFIG1,
  1206. config & ~0x80);
  1207. }
  1208. }
  1209. data->last_updated = jiffies;
  1210. data->valid = 1;
  1211. }
  1212. mutex_unlock(&data->update_lock);
  1213. return data;
  1214. }
  1215. static int __init sensors_lm90_init(void)
  1216. {
  1217. return i2c_add_driver(&lm90_driver);
  1218. }
  1219. static void __exit sensors_lm90_exit(void)
  1220. {
  1221. i2c_del_driver(&lm90_driver);
  1222. }
  1223. MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
  1224. MODULE_DESCRIPTION("LM90/ADM1032 driver");
  1225. MODULE_LICENSE("GPL");
  1226. module_init(sensors_lm90_init);
  1227. module_exit(sensors_lm90_exit);