lm90.c 50 KB

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