lm90.c 45 KB

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