lm90.c 46 KB

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