lm90.c 45 KB

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