lm90.c 46 KB

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