lm90.c 44 KB

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