lm90.c 45 KB

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