jc42.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * jc42.c - driver for Jedec JC42.4 compliant temperature sensors
  3. *
  4. * Copyright (c) 2010 Ericsson AB.
  5. *
  6. * Derived from lm77.c by Andras BALI <drewie@freemail.hu>.
  7. *
  8. * JC42.4 compliant temperature sensors are typically used on memory modules.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/err.h>
  31. #include <linux/mutex.h>
  32. #include <linux/of.h>
  33. /* Addresses to scan */
  34. static const unsigned short normal_i2c[] = {
  35. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, I2C_CLIENT_END };
  36. /* JC42 registers. All registers are 16 bit. */
  37. #define JC42_REG_CAP 0x00
  38. #define JC42_REG_CONFIG 0x01
  39. #define JC42_REG_TEMP_UPPER 0x02
  40. #define JC42_REG_TEMP_LOWER 0x03
  41. #define JC42_REG_TEMP_CRITICAL 0x04
  42. #define JC42_REG_TEMP 0x05
  43. #define JC42_REG_MANID 0x06
  44. #define JC42_REG_DEVICEID 0x07
  45. /* Status bits in temperature register */
  46. #define JC42_ALARM_CRIT_BIT 15
  47. #define JC42_ALARM_MAX_BIT 14
  48. #define JC42_ALARM_MIN_BIT 13
  49. /* Configuration register defines */
  50. #define JC42_CFG_CRIT_ONLY (1 << 2)
  51. #define JC42_CFG_TCRIT_LOCK (1 << 6)
  52. #define JC42_CFG_EVENT_LOCK (1 << 7)
  53. #define JC42_CFG_SHUTDOWN (1 << 8)
  54. #define JC42_CFG_HYST_SHIFT 9
  55. #define JC42_CFG_HYST_MASK (0x03 << 9)
  56. /* Capabilities */
  57. #define JC42_CAP_RANGE (1 << 2)
  58. /* Manufacturer IDs */
  59. #define ADT_MANID 0x11d4 /* Analog Devices */
  60. #define ATMEL_MANID 0x001f /* Atmel */
  61. #define ATMEL_MANID2 0x1114 /* Atmel */
  62. #define MAX_MANID 0x004d /* Maxim */
  63. #define IDT_MANID 0x00b3 /* IDT */
  64. #define MCP_MANID 0x0054 /* Microchip */
  65. #define NXP_MANID 0x1131 /* NXP Semiconductors */
  66. #define ONS_MANID 0x1b09 /* ON Semiconductor */
  67. #define STM_MANID 0x104a /* ST Microelectronics */
  68. /* Supported chips */
  69. /* Analog Devices */
  70. #define ADT7408_DEVID 0x0801
  71. #define ADT7408_DEVID_MASK 0xffff
  72. /* Atmel */
  73. #define AT30TS00_DEVID 0x8201
  74. #define AT30TS00_DEVID_MASK 0xffff
  75. #define AT30TSE004_DEVID 0x2200
  76. #define AT30TSE004_DEVID_MASK 0xffff
  77. /* IDT */
  78. #define TSE2004_DEVID 0x2200
  79. #define TSE2004_DEVID_MASK 0xff00
  80. #define TS3000_DEVID 0x2900 /* Also matches TSE2002 */
  81. #define TS3000_DEVID_MASK 0xff00
  82. #define TS3001_DEVID 0x3000
  83. #define TS3001_DEVID_MASK 0xff00
  84. /* Maxim */
  85. #define MAX6604_DEVID 0x3e00
  86. #define MAX6604_DEVID_MASK 0xffff
  87. /* Microchip */
  88. #define MCP9804_DEVID 0x0200
  89. #define MCP9804_DEVID_MASK 0xfffc
  90. #define MCP9808_DEVID 0x0400
  91. #define MCP9808_DEVID_MASK 0xfffc
  92. #define MCP98242_DEVID 0x2000
  93. #define MCP98242_DEVID_MASK 0xfffc
  94. #define MCP98243_DEVID 0x2100
  95. #define MCP98243_DEVID_MASK 0xfffc
  96. #define MCP98244_DEVID 0x2200
  97. #define MCP98244_DEVID_MASK 0xfffc
  98. #define MCP9843_DEVID 0x0000 /* Also matches mcp9805 */
  99. #define MCP9843_DEVID_MASK 0xfffe
  100. /* NXP */
  101. #define SE97_DEVID 0xa200
  102. #define SE97_DEVID_MASK 0xfffc
  103. #define SE98_DEVID 0xa100
  104. #define SE98_DEVID_MASK 0xfffc
  105. /* ON Semiconductor */
  106. #define CAT6095_DEVID 0x0800 /* Also matches CAT34TS02 */
  107. #define CAT6095_DEVID_MASK 0xffe0
  108. /* ST Microelectronics */
  109. #define STTS424_DEVID 0x0101
  110. #define STTS424_DEVID_MASK 0xffff
  111. #define STTS424E_DEVID 0x0000
  112. #define STTS424E_DEVID_MASK 0xfffe
  113. #define STTS2002_DEVID 0x0300
  114. #define STTS2002_DEVID_MASK 0xffff
  115. #define STTS2004_DEVID 0x2201
  116. #define STTS2004_DEVID_MASK 0xffff
  117. #define STTS3000_DEVID 0x0200
  118. #define STTS3000_DEVID_MASK 0xffff
  119. static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 };
  120. struct jc42_chips {
  121. u16 manid;
  122. u16 devid;
  123. u16 devid_mask;
  124. };
  125. static struct jc42_chips jc42_chips[] = {
  126. { ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK },
  127. { ATMEL_MANID, AT30TS00_DEVID, AT30TS00_DEVID_MASK },
  128. { ATMEL_MANID2, AT30TSE004_DEVID, AT30TSE004_DEVID_MASK },
  129. { IDT_MANID, TSE2004_DEVID, TSE2004_DEVID_MASK },
  130. { IDT_MANID, TS3000_DEVID, TS3000_DEVID_MASK },
  131. { IDT_MANID, TS3001_DEVID, TS3001_DEVID_MASK },
  132. { MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK },
  133. { MCP_MANID, MCP9804_DEVID, MCP9804_DEVID_MASK },
  134. { MCP_MANID, MCP9808_DEVID, MCP9808_DEVID_MASK },
  135. { MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK },
  136. { MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK },
  137. { MCP_MANID, MCP98244_DEVID, MCP98244_DEVID_MASK },
  138. { MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK },
  139. { NXP_MANID, SE97_DEVID, SE97_DEVID_MASK },
  140. { ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK },
  141. { NXP_MANID, SE98_DEVID, SE98_DEVID_MASK },
  142. { STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK },
  143. { STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK },
  144. { STM_MANID, STTS2002_DEVID, STTS2002_DEVID_MASK },
  145. { STM_MANID, STTS2004_DEVID, STTS2004_DEVID_MASK },
  146. { STM_MANID, STTS3000_DEVID, STTS3000_DEVID_MASK },
  147. };
  148. enum temp_index {
  149. t_input = 0,
  150. t_crit,
  151. t_min,
  152. t_max,
  153. t_num_temp
  154. };
  155. static const u8 temp_regs[t_num_temp] = {
  156. [t_input] = JC42_REG_TEMP,
  157. [t_crit] = JC42_REG_TEMP_CRITICAL,
  158. [t_min] = JC42_REG_TEMP_LOWER,
  159. [t_max] = JC42_REG_TEMP_UPPER,
  160. };
  161. /* Each client has this additional data */
  162. struct jc42_data {
  163. struct i2c_client *client;
  164. struct mutex update_lock; /* protect register access */
  165. bool extended; /* true if extended range supported */
  166. bool valid;
  167. unsigned long last_updated; /* In jiffies */
  168. u16 orig_config; /* original configuration */
  169. u16 config; /* current configuration */
  170. u16 temp[t_num_temp];/* Temperatures */
  171. };
  172. #define JC42_TEMP_MIN_EXTENDED (-40000)
  173. #define JC42_TEMP_MIN 0
  174. #define JC42_TEMP_MAX 125000
  175. static u16 jc42_temp_to_reg(long temp, bool extended)
  176. {
  177. int ntemp = clamp_val(temp,
  178. extended ? JC42_TEMP_MIN_EXTENDED :
  179. JC42_TEMP_MIN, JC42_TEMP_MAX);
  180. /* convert from 0.001 to 0.0625 resolution */
  181. return (ntemp * 2 / 125) & 0x1fff;
  182. }
  183. static int jc42_temp_from_reg(s16 reg)
  184. {
  185. reg = sign_extend32(reg, 12);
  186. /* convert from 0.0625 to 0.001 resolution */
  187. return reg * 125 / 2;
  188. }
  189. static struct jc42_data *jc42_update_device(struct device *dev)
  190. {
  191. struct jc42_data *data = dev_get_drvdata(dev);
  192. struct i2c_client *client = data->client;
  193. struct jc42_data *ret = data;
  194. int i, val;
  195. mutex_lock(&data->update_lock);
  196. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  197. for (i = 0; i < t_num_temp; i++) {
  198. val = i2c_smbus_read_word_swapped(client, temp_regs[i]);
  199. if (val < 0) {
  200. ret = ERR_PTR(val);
  201. goto abort;
  202. }
  203. data->temp[i] = val;
  204. }
  205. data->last_updated = jiffies;
  206. data->valid = true;
  207. }
  208. abort:
  209. mutex_unlock(&data->update_lock);
  210. return ret;
  211. }
  212. static int jc42_read(struct device *dev, enum hwmon_sensor_types type,
  213. u32 attr, int channel, long *val)
  214. {
  215. struct jc42_data *data = jc42_update_device(dev);
  216. int temp, hyst;
  217. if (IS_ERR(data))
  218. return PTR_ERR(data);
  219. switch (attr) {
  220. case hwmon_temp_input:
  221. *val = jc42_temp_from_reg(data->temp[t_input]);
  222. return 0;
  223. case hwmon_temp_min:
  224. *val = jc42_temp_from_reg(data->temp[t_min]);
  225. return 0;
  226. case hwmon_temp_max:
  227. *val = jc42_temp_from_reg(data->temp[t_max]);
  228. return 0;
  229. case hwmon_temp_crit:
  230. *val = jc42_temp_from_reg(data->temp[t_crit]);
  231. return 0;
  232. case hwmon_temp_max_hyst:
  233. temp = jc42_temp_from_reg(data->temp[t_max]);
  234. hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
  235. >> JC42_CFG_HYST_SHIFT];
  236. *val = temp - hyst;
  237. return 0;
  238. case hwmon_temp_crit_hyst:
  239. temp = jc42_temp_from_reg(data->temp[t_crit]);
  240. hyst = jc42_hysteresis[(data->config & JC42_CFG_HYST_MASK)
  241. >> JC42_CFG_HYST_SHIFT];
  242. *val = temp - hyst;
  243. return 0;
  244. case hwmon_temp_min_alarm:
  245. *val = (data->temp[t_input] >> JC42_ALARM_MIN_BIT) & 1;
  246. return 0;
  247. case hwmon_temp_max_alarm:
  248. *val = (data->temp[t_input] >> JC42_ALARM_MAX_BIT) & 1;
  249. return 0;
  250. case hwmon_temp_crit_alarm:
  251. *val = (data->temp[t_input] >> JC42_ALARM_CRIT_BIT) & 1;
  252. return 0;
  253. default:
  254. return -EOPNOTSUPP;
  255. }
  256. }
  257. static int jc42_write(struct device *dev, enum hwmon_sensor_types type,
  258. u32 attr, int channel, long val)
  259. {
  260. struct jc42_data *data = dev_get_drvdata(dev);
  261. struct i2c_client *client = data->client;
  262. int diff, hyst;
  263. int ret;
  264. mutex_lock(&data->update_lock);
  265. switch (attr) {
  266. case hwmon_temp_min:
  267. data->temp[t_min] = jc42_temp_to_reg(val, data->extended);
  268. ret = i2c_smbus_write_word_swapped(client, temp_regs[t_min],
  269. data->temp[t_min]);
  270. break;
  271. case hwmon_temp_max:
  272. data->temp[t_max] = jc42_temp_to_reg(val, data->extended);
  273. ret = i2c_smbus_write_word_swapped(client, temp_regs[t_max],
  274. data->temp[t_max]);
  275. break;
  276. case hwmon_temp_crit:
  277. data->temp[t_crit] = jc42_temp_to_reg(val, data->extended);
  278. ret = i2c_smbus_write_word_swapped(client, temp_regs[t_crit],
  279. data->temp[t_crit]);
  280. break;
  281. case hwmon_temp_crit_hyst:
  282. /*
  283. * JC42.4 compliant chips only support four hysteresis values.
  284. * Pick best choice and go from there.
  285. */
  286. val = clamp_val(val, (data->extended ? JC42_TEMP_MIN_EXTENDED
  287. : JC42_TEMP_MIN) - 6000,
  288. JC42_TEMP_MAX);
  289. diff = jc42_temp_from_reg(data->temp[t_crit]) - val;
  290. hyst = 0;
  291. if (diff > 0) {
  292. if (diff < 2250)
  293. hyst = 1; /* 1.5 degrees C */
  294. else if (diff < 4500)
  295. hyst = 2; /* 3.0 degrees C */
  296. else
  297. hyst = 3; /* 6.0 degrees C */
  298. }
  299. data->config = (data->config & ~JC42_CFG_HYST_MASK) |
  300. (hyst << JC42_CFG_HYST_SHIFT);
  301. ret = i2c_smbus_write_word_swapped(data->client,
  302. JC42_REG_CONFIG,
  303. data->config);
  304. break;
  305. default:
  306. ret = -EOPNOTSUPP;
  307. break;
  308. }
  309. mutex_unlock(&data->update_lock);
  310. return ret;
  311. }
  312. static umode_t jc42_is_visible(const void *_data, enum hwmon_sensor_types type,
  313. u32 attr, int channel)
  314. {
  315. const struct jc42_data *data = _data;
  316. unsigned int config = data->config;
  317. umode_t mode = S_IRUGO;
  318. switch (attr) {
  319. case hwmon_temp_min:
  320. case hwmon_temp_max:
  321. if (!(config & JC42_CFG_EVENT_LOCK))
  322. mode |= S_IWUSR;
  323. break;
  324. case hwmon_temp_crit:
  325. if (!(config & JC42_CFG_TCRIT_LOCK))
  326. mode |= S_IWUSR;
  327. break;
  328. case hwmon_temp_crit_hyst:
  329. if (!(config & (JC42_CFG_EVENT_LOCK | JC42_CFG_TCRIT_LOCK)))
  330. mode |= S_IWUSR;
  331. break;
  332. case hwmon_temp_input:
  333. case hwmon_temp_max_hyst:
  334. case hwmon_temp_min_alarm:
  335. case hwmon_temp_max_alarm:
  336. case hwmon_temp_crit_alarm:
  337. break;
  338. default:
  339. mode = 0;
  340. break;
  341. }
  342. return mode;
  343. }
  344. /* Return 0 if detection is successful, -ENODEV otherwise */
  345. static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info)
  346. {
  347. struct i2c_adapter *adapter = client->adapter;
  348. int i, config, cap, manid, devid;
  349. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  350. I2C_FUNC_SMBUS_WORD_DATA))
  351. return -ENODEV;
  352. cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
  353. config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
  354. manid = i2c_smbus_read_word_swapped(client, JC42_REG_MANID);
  355. devid = i2c_smbus_read_word_swapped(client, JC42_REG_DEVICEID);
  356. if (cap < 0 || config < 0 || manid < 0 || devid < 0)
  357. return -ENODEV;
  358. if ((cap & 0xff00) || (config & 0xf800))
  359. return -ENODEV;
  360. for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) {
  361. struct jc42_chips *chip = &jc42_chips[i];
  362. if (manid == chip->manid &&
  363. (devid & chip->devid_mask) == chip->devid) {
  364. strlcpy(info->type, "jc42", I2C_NAME_SIZE);
  365. return 0;
  366. }
  367. }
  368. return -ENODEV;
  369. }
  370. static const u32 jc42_temp_config[] = {
  371. HWMON_T_INPUT | HWMON_T_MIN | HWMON_T_MAX | HWMON_T_CRIT |
  372. HWMON_T_MAX_HYST | HWMON_T_CRIT_HYST |
  373. HWMON_T_MIN_ALARM | HWMON_T_MAX_ALARM | HWMON_T_CRIT_ALARM,
  374. 0
  375. };
  376. static const struct hwmon_channel_info jc42_temp = {
  377. .type = hwmon_temp,
  378. .config = jc42_temp_config,
  379. };
  380. static const struct hwmon_channel_info *jc42_info[] = {
  381. &jc42_temp,
  382. NULL
  383. };
  384. static const struct hwmon_ops jc42_hwmon_ops = {
  385. .is_visible = jc42_is_visible,
  386. .read = jc42_read,
  387. .write = jc42_write,
  388. };
  389. static const struct hwmon_chip_info jc42_chip_info = {
  390. .ops = &jc42_hwmon_ops,
  391. .info = jc42_info,
  392. };
  393. static int jc42_probe(struct i2c_client *client, const struct i2c_device_id *id)
  394. {
  395. struct device *dev = &client->dev;
  396. struct device *hwmon_dev;
  397. struct jc42_data *data;
  398. int config, cap;
  399. data = devm_kzalloc(dev, sizeof(struct jc42_data), GFP_KERNEL);
  400. if (!data)
  401. return -ENOMEM;
  402. data->client = client;
  403. i2c_set_clientdata(client, data);
  404. mutex_init(&data->update_lock);
  405. cap = i2c_smbus_read_word_swapped(client, JC42_REG_CAP);
  406. if (cap < 0)
  407. return cap;
  408. data->extended = !!(cap & JC42_CAP_RANGE);
  409. config = i2c_smbus_read_word_swapped(client, JC42_REG_CONFIG);
  410. if (config < 0)
  411. return config;
  412. data->orig_config = config;
  413. if (config & JC42_CFG_SHUTDOWN) {
  414. config &= ~JC42_CFG_SHUTDOWN;
  415. i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
  416. }
  417. data->config = config;
  418. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  419. data, &jc42_chip_info,
  420. NULL);
  421. return PTR_ERR_OR_ZERO(hwmon_dev);
  422. }
  423. static int jc42_remove(struct i2c_client *client)
  424. {
  425. struct jc42_data *data = i2c_get_clientdata(client);
  426. /* Restore original configuration except hysteresis */
  427. if ((data->config & ~JC42_CFG_HYST_MASK) !=
  428. (data->orig_config & ~JC42_CFG_HYST_MASK)) {
  429. int config;
  430. config = (data->orig_config & ~JC42_CFG_HYST_MASK)
  431. | (data->config & JC42_CFG_HYST_MASK);
  432. i2c_smbus_write_word_swapped(client, JC42_REG_CONFIG, config);
  433. }
  434. return 0;
  435. }
  436. #ifdef CONFIG_PM
  437. static int jc42_suspend(struct device *dev)
  438. {
  439. struct jc42_data *data = dev_get_drvdata(dev);
  440. data->config |= JC42_CFG_SHUTDOWN;
  441. i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
  442. data->config);
  443. return 0;
  444. }
  445. static int jc42_resume(struct device *dev)
  446. {
  447. struct jc42_data *data = dev_get_drvdata(dev);
  448. data->config &= ~JC42_CFG_SHUTDOWN;
  449. i2c_smbus_write_word_swapped(data->client, JC42_REG_CONFIG,
  450. data->config);
  451. return 0;
  452. }
  453. static const struct dev_pm_ops jc42_dev_pm_ops = {
  454. .suspend = jc42_suspend,
  455. .resume = jc42_resume,
  456. };
  457. #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops)
  458. #else
  459. #define JC42_DEV_PM_OPS NULL
  460. #endif /* CONFIG_PM */
  461. static const struct i2c_device_id jc42_id[] = {
  462. { "jc42", 0 },
  463. { }
  464. };
  465. MODULE_DEVICE_TABLE(i2c, jc42_id);
  466. #ifdef CONFIG_OF
  467. static const struct of_device_id jc42_of_ids[] = {
  468. { .compatible = "jedec,jc-42.4-temp", },
  469. { }
  470. };
  471. MODULE_DEVICE_TABLE(of, jc42_of_ids);
  472. #endif
  473. static struct i2c_driver jc42_driver = {
  474. .class = I2C_CLASS_SPD | I2C_CLASS_HWMON,
  475. .driver = {
  476. .name = "jc42",
  477. .pm = JC42_DEV_PM_OPS,
  478. .of_match_table = of_match_ptr(jc42_of_ids),
  479. },
  480. .probe = jc42_probe,
  481. .remove = jc42_remove,
  482. .id_table = jc42_id,
  483. .detect = jc42_detect,
  484. .address_list = normal_i2c,
  485. };
  486. module_i2c_driver(jc42_driver);
  487. MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
  488. MODULE_DESCRIPTION("JC42 driver");
  489. MODULE_LICENSE("GPL");