lm90.c 48 KB

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