jc42.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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/hwmon-sysfs.h>
  31. #include <linux/err.h>
  32. #include <linux/mutex.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_SHUTDOWN (1 << 8)
  52. #define JC42_CFG_HYST_SHIFT 9
  53. #define JC42_CFG_HYST_MASK 0x03
  54. /* Capabilities */
  55. #define JC42_CAP_RANGE (1 << 2)
  56. /* Manufacturer IDs */
  57. #define ADT_MANID 0x11d4 /* Analog Devices */
  58. #define MAX_MANID 0x004d /* Maxim */
  59. #define IDT_MANID 0x00b3 /* IDT */
  60. #define MCP_MANID 0x0054 /* Microchip */
  61. #define NXP_MANID 0x1131 /* NXP Semiconductors */
  62. #define ONS_MANID 0x1b09 /* ON Semiconductor */
  63. #define STM_MANID 0x104a /* ST Microelectronics */
  64. /* Supported chips */
  65. /* Analog Devices */
  66. #define ADT7408_DEVID 0x0801
  67. #define ADT7408_DEVID_MASK 0xffff
  68. /* IDT */
  69. #define TS3000B3_DEVID 0x2903 /* Also matches TSE2002B3 */
  70. #define TS3000B3_DEVID_MASK 0xffff
  71. /* Maxim */
  72. #define MAX6604_DEVID 0x3e00
  73. #define MAX6604_DEVID_MASK 0xffff
  74. /* Microchip */
  75. #define MCP98242_DEVID 0x2000
  76. #define MCP98242_DEVID_MASK 0xfffc
  77. #define MCP98243_DEVID 0x2100
  78. #define MCP98243_DEVID_MASK 0xfffc
  79. #define MCP9843_DEVID 0x0000 /* Also matches mcp9805 */
  80. #define MCP9843_DEVID_MASK 0xfffe
  81. /* NXP */
  82. #define SE97_DEVID 0xa200
  83. #define SE97_DEVID_MASK 0xfffc
  84. #define SE98_DEVID 0xa100
  85. #define SE98_DEVID_MASK 0xfffc
  86. /* ON Semiconductor */
  87. #define CAT6095_DEVID 0x0800 /* Also matches CAT34TS02 */
  88. #define CAT6095_DEVID_MASK 0xffe0
  89. /* ST Microelectronics */
  90. #define STTS424_DEVID 0x0101
  91. #define STTS424_DEVID_MASK 0xffff
  92. #define STTS424E_DEVID 0x0000
  93. #define STTS424E_DEVID_MASK 0xfffe
  94. static u16 jc42_hysteresis[] = { 0, 1500, 3000, 6000 };
  95. struct jc42_chips {
  96. u16 manid;
  97. u16 devid;
  98. u16 devid_mask;
  99. };
  100. static struct jc42_chips jc42_chips[] = {
  101. { ADT_MANID, ADT7408_DEVID, ADT7408_DEVID_MASK },
  102. { IDT_MANID, TS3000B3_DEVID, TS3000B3_DEVID_MASK },
  103. { MAX_MANID, MAX6604_DEVID, MAX6604_DEVID_MASK },
  104. { MCP_MANID, MCP98242_DEVID, MCP98242_DEVID_MASK },
  105. { MCP_MANID, MCP98243_DEVID, MCP98243_DEVID_MASK },
  106. { MCP_MANID, MCP9843_DEVID, MCP9843_DEVID_MASK },
  107. { NXP_MANID, SE97_DEVID, SE97_DEVID_MASK },
  108. { ONS_MANID, CAT6095_DEVID, CAT6095_DEVID_MASK },
  109. { NXP_MANID, SE98_DEVID, SE98_DEVID_MASK },
  110. { STM_MANID, STTS424_DEVID, STTS424_DEVID_MASK },
  111. { STM_MANID, STTS424E_DEVID, STTS424E_DEVID_MASK },
  112. };
  113. /* Each client has this additional data */
  114. struct jc42_data {
  115. struct device *hwmon_dev;
  116. struct mutex update_lock; /* protect register access */
  117. bool extended; /* true if extended range supported */
  118. bool valid;
  119. unsigned long last_updated; /* In jiffies */
  120. u16 orig_config; /* original configuration */
  121. u16 config; /* current configuration */
  122. u16 temp_input; /* Temperatures */
  123. u16 temp_crit;
  124. u16 temp_min;
  125. u16 temp_max;
  126. };
  127. static int jc42_probe(struct i2c_client *client,
  128. const struct i2c_device_id *id);
  129. static int jc42_detect(struct i2c_client *client, struct i2c_board_info *info);
  130. static int jc42_remove(struct i2c_client *client);
  131. static int jc42_read_value(struct i2c_client *client, u8 reg);
  132. static int jc42_write_value(struct i2c_client *client, u8 reg, u16 value);
  133. static struct jc42_data *jc42_update_device(struct device *dev);
  134. static const struct i2c_device_id jc42_id[] = {
  135. { "adt7408", 0 },
  136. { "cat94ts02", 0 },
  137. { "cat6095", 0 },
  138. { "jc42", 0 },
  139. { "max6604", 0 },
  140. { "mcp9805", 0 },
  141. { "mcp98242", 0 },
  142. { "mcp98243", 0 },
  143. { "mcp9843", 0 },
  144. { "se97", 0 },
  145. { "se97b", 0 },
  146. { "se98", 0 },
  147. { "stts424", 0 },
  148. { "tse2002b3", 0 },
  149. { "ts3000b3", 0 },
  150. { }
  151. };
  152. MODULE_DEVICE_TABLE(i2c, jc42_id);
  153. #ifdef CONFIG_PM
  154. static int jc42_suspend(struct device *dev)
  155. {
  156. struct i2c_client *client = to_i2c_client(dev);
  157. struct jc42_data *data = i2c_get_clientdata(client);
  158. data->config |= JC42_CFG_SHUTDOWN;
  159. jc42_write_value(client, JC42_REG_CONFIG, data->config);
  160. return 0;
  161. }
  162. static int jc42_resume(struct device *dev)
  163. {
  164. struct i2c_client *client = to_i2c_client(dev);
  165. struct jc42_data *data = i2c_get_clientdata(client);
  166. data->config &= ~JC42_CFG_SHUTDOWN;
  167. jc42_write_value(client, JC42_REG_CONFIG, data->config);
  168. return 0;
  169. }
  170. static const struct dev_pm_ops jc42_dev_pm_ops = {
  171. .suspend = jc42_suspend,
  172. .resume = jc42_resume,
  173. };
  174. #define JC42_DEV_PM_OPS (&jc42_dev_pm_ops)
  175. #else
  176. #define JC42_DEV_PM_OPS NULL
  177. #endif /* CONFIG_PM */
  178. /* This is the driver that will be inserted */
  179. static struct i2c_driver jc42_driver = {
  180. .class = I2C_CLASS_HWMON,
  181. .driver = {
  182. .name = "jc42",
  183. .pm = JC42_DEV_PM_OPS,
  184. },
  185. .probe = jc42_probe,
  186. .remove = jc42_remove,
  187. .id_table = jc42_id,
  188. .detect = jc42_detect,
  189. .address_list = normal_i2c,
  190. };
  191. #define JC42_TEMP_MIN_EXTENDED (-40000)
  192. #define JC42_TEMP_MIN 0
  193. #define JC42_TEMP_MAX 125000
  194. static u16 jc42_temp_to_reg(int temp, bool extended)
  195. {
  196. int ntemp = SENSORS_LIMIT(temp,
  197. extended ? JC42_TEMP_MIN_EXTENDED :
  198. JC42_TEMP_MIN, JC42_TEMP_MAX);
  199. /* convert from 0.001 to 0.0625 resolution */
  200. return (ntemp * 2 / 125) & 0x1fff;
  201. }
  202. static int jc42_temp_from_reg(s16 reg)
  203. {
  204. reg &= 0x1fff;
  205. /* sign extend register */
  206. if (reg & 0x1000)
  207. reg |= 0xf000;
  208. /* convert from 0.0625 to 0.001 resolution */
  209. return reg * 125 / 2;
  210. }
  211. /* sysfs stuff */
  212. /* read routines for temperature limits */
  213. #define show(value) \
  214. static ssize_t show_##value(struct device *dev, \
  215. struct device_attribute *attr, \
  216. char *buf) \
  217. { \
  218. struct jc42_data *data = jc42_update_device(dev); \
  219. if (IS_ERR(data)) \
  220. return PTR_ERR(data); \
  221. return sprintf(buf, "%d\n", jc42_temp_from_reg(data->value)); \
  222. }
  223. show(temp_input);
  224. show(temp_crit);
  225. show(temp_min);
  226. show(temp_max);
  227. /* read routines for hysteresis values */
  228. static ssize_t show_temp_crit_hyst(struct device *dev,
  229. struct device_attribute *attr, char *buf)
  230. {
  231. struct jc42_data *data = jc42_update_device(dev);
  232. int temp, hyst;
  233. if (IS_ERR(data))
  234. return PTR_ERR(data);
  235. temp = jc42_temp_from_reg(data->temp_crit);
  236. hyst = jc42_hysteresis[(data->config >> JC42_CFG_HYST_SHIFT)
  237. & JC42_CFG_HYST_MASK];
  238. return sprintf(buf, "%d\n", temp - hyst);
  239. }
  240. static ssize_t show_temp_max_hyst(struct device *dev,
  241. struct device_attribute *attr, char *buf)
  242. {
  243. struct jc42_data *data = jc42_update_device(dev);
  244. int temp, hyst;
  245. if (IS_ERR(data))
  246. return PTR_ERR(data);
  247. temp = jc42_temp_from_reg(data->temp_max);
  248. hyst = jc42_hysteresis[(data->config >> JC42_CFG_HYST_SHIFT)
  249. & JC42_CFG_HYST_MASK];
  250. return sprintf(buf, "%d\n", temp - hyst);
  251. }
  252. /* write routines */
  253. #define set(value, reg) \
  254. static ssize_t set_##value(struct device *dev, \
  255. struct device_attribute *attr, \
  256. const char *buf, size_t count) \
  257. { \
  258. struct i2c_client *client = to_i2c_client(dev); \
  259. struct jc42_data *data = i2c_get_clientdata(client); \
  260. int err, ret = count; \
  261. long val; \
  262. if (strict_strtol(buf, 10, &val) < 0) \
  263. return -EINVAL; \
  264. mutex_lock(&data->update_lock); \
  265. data->value = jc42_temp_to_reg(val, data->extended); \
  266. err = jc42_write_value(client, reg, data->value); \
  267. if (err < 0) \
  268. ret = err; \
  269. mutex_unlock(&data->update_lock); \
  270. return ret; \
  271. }
  272. set(temp_min, JC42_REG_TEMP_LOWER);
  273. set(temp_max, JC42_REG_TEMP_UPPER);
  274. set(temp_crit, JC42_REG_TEMP_CRITICAL);
  275. /* JC42.4 compliant chips only support four hysteresis values.
  276. * Pick best choice and go from there. */
  277. static ssize_t set_temp_crit_hyst(struct device *dev,
  278. struct device_attribute *attr,
  279. const char *buf, size_t count)
  280. {
  281. struct i2c_client *client = to_i2c_client(dev);
  282. struct jc42_data *data = i2c_get_clientdata(client);
  283. long val;
  284. int diff, hyst;
  285. int err;
  286. int ret = count;
  287. if (strict_strtoul(buf, 10, &val) < 0)
  288. return -EINVAL;
  289. diff = jc42_temp_from_reg(data->temp_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. mutex_lock(&data->update_lock);
  300. data->config = (data->config
  301. & ~(JC42_CFG_HYST_MASK << JC42_CFG_HYST_SHIFT))
  302. | (hyst << JC42_CFG_HYST_SHIFT);
  303. err = jc42_write_value(client, JC42_REG_CONFIG, data->config);
  304. if (err < 0)
  305. ret = err;
  306. mutex_unlock(&data->update_lock);
  307. return ret;
  308. }
  309. static ssize_t show_alarm(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. u16 bit = to_sensor_dev_attr(attr)->index;
  313. struct jc42_data *data = jc42_update_device(dev);
  314. u16 val;
  315. if (IS_ERR(data))
  316. return PTR_ERR(data);
  317. val = data->temp_input;
  318. if (bit != JC42_ALARM_CRIT_BIT && (data->config & JC42_CFG_CRIT_ONLY))
  319. val = 0;
  320. return sprintf(buf, "%u\n", (val >> bit) & 1);
  321. }
  322. static DEVICE_ATTR(temp1_input, S_IRUGO,
  323. show_temp_input, NULL);
  324. static DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
  325. show_temp_crit, set_temp_crit);
  326. static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
  327. show_temp_min, set_temp_min);
  328. static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
  329. show_temp_max, set_temp_max);
  330. static DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO,
  331. show_temp_crit_hyst, set_temp_crit_hyst);
  332. static DEVICE_ATTR(temp1_max_hyst, S_IRUGO,
  333. show_temp_max_hyst, NULL);
  334. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
  335. JC42_ALARM_CRIT_BIT);
  336. static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL,
  337. JC42_ALARM_MIN_BIT);
  338. static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL,
  339. JC42_ALARM_MAX_BIT);
  340. static struct attribute *jc42_attributes[] = {
  341. &dev_attr_temp1_input.attr,
  342. &dev_attr_temp1_crit.attr,
  343. &dev_attr_temp1_min.attr,
  344. &dev_attr_temp1_max.attr,
  345. &dev_attr_temp1_crit_hyst.attr,
  346. &dev_attr_temp1_max_hyst.attr,
  347. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  348. &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
  349. &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
  350. NULL
  351. };
  352. static const struct attribute_group jc42_group = {
  353. .attrs = jc42_attributes,
  354. };
  355. /* Return 0 if detection is successful, -ENODEV otherwise */
  356. static int jc42_detect(struct i2c_client *new_client,
  357. struct i2c_board_info *info)
  358. {
  359. struct i2c_adapter *adapter = new_client->adapter;
  360. int i, config, cap, manid, devid;
  361. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  362. I2C_FUNC_SMBUS_WORD_DATA))
  363. return -ENODEV;
  364. cap = jc42_read_value(new_client, JC42_REG_CAP);
  365. config = jc42_read_value(new_client, JC42_REG_CONFIG);
  366. manid = jc42_read_value(new_client, JC42_REG_MANID);
  367. devid = jc42_read_value(new_client, JC42_REG_DEVICEID);
  368. if (cap < 0 || config < 0 || manid < 0 || devid < 0)
  369. return -ENODEV;
  370. if ((cap & 0xff00) || (config & 0xf800))
  371. return -ENODEV;
  372. for (i = 0; i < ARRAY_SIZE(jc42_chips); i++) {
  373. struct jc42_chips *chip = &jc42_chips[i];
  374. if (manid == chip->manid &&
  375. (devid & chip->devid_mask) == chip->devid) {
  376. strlcpy(info->type, "jc42", I2C_NAME_SIZE);
  377. return 0;
  378. }
  379. }
  380. return -ENODEV;
  381. }
  382. static int jc42_probe(struct i2c_client *new_client,
  383. const struct i2c_device_id *id)
  384. {
  385. struct jc42_data *data;
  386. int config, cap, err;
  387. data = kzalloc(sizeof(struct jc42_data), GFP_KERNEL);
  388. if (!data) {
  389. err = -ENOMEM;
  390. goto exit;
  391. }
  392. i2c_set_clientdata(new_client, data);
  393. mutex_init(&data->update_lock);
  394. cap = jc42_read_value(new_client, JC42_REG_CAP);
  395. if (cap < 0) {
  396. err = -EINVAL;
  397. goto exit_free;
  398. }
  399. data->extended = !!(cap & JC42_CAP_RANGE);
  400. config = jc42_read_value(new_client, JC42_REG_CONFIG);
  401. if (config < 0) {
  402. err = -EINVAL;
  403. goto exit_free;
  404. }
  405. data->orig_config = config;
  406. if (config & JC42_CFG_SHUTDOWN) {
  407. config &= ~JC42_CFG_SHUTDOWN;
  408. jc42_write_value(new_client, JC42_REG_CONFIG, config);
  409. }
  410. data->config = config;
  411. /* Register sysfs hooks */
  412. err = sysfs_create_group(&new_client->dev.kobj, &jc42_group);
  413. if (err)
  414. goto exit_free;
  415. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  416. if (IS_ERR(data->hwmon_dev)) {
  417. err = PTR_ERR(data->hwmon_dev);
  418. goto exit_remove;
  419. }
  420. return 0;
  421. exit_remove:
  422. sysfs_remove_group(&new_client->dev.kobj, &jc42_group);
  423. exit_free:
  424. kfree(data);
  425. exit:
  426. return err;
  427. }
  428. static int jc42_remove(struct i2c_client *client)
  429. {
  430. struct jc42_data *data = i2c_get_clientdata(client);
  431. hwmon_device_unregister(data->hwmon_dev);
  432. sysfs_remove_group(&client->dev.kobj, &jc42_group);
  433. if (data->config != data->orig_config)
  434. jc42_write_value(client, JC42_REG_CONFIG, data->orig_config);
  435. kfree(data);
  436. return 0;
  437. }
  438. /* All registers are word-sized. */
  439. static int jc42_read_value(struct i2c_client *client, u8 reg)
  440. {
  441. int ret = i2c_smbus_read_word_data(client, reg);
  442. if (ret < 0)
  443. return ret;
  444. return swab16(ret);
  445. }
  446. static int jc42_write_value(struct i2c_client *client, u8 reg, u16 value)
  447. {
  448. return i2c_smbus_write_word_data(client, reg, swab16(value));
  449. }
  450. static struct jc42_data *jc42_update_device(struct device *dev)
  451. {
  452. struct i2c_client *client = to_i2c_client(dev);
  453. struct jc42_data *data = i2c_get_clientdata(client);
  454. struct jc42_data *ret = data;
  455. int val;
  456. mutex_lock(&data->update_lock);
  457. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  458. val = jc42_read_value(client, JC42_REG_TEMP);
  459. if (val < 0) {
  460. ret = ERR_PTR(val);
  461. goto abort;
  462. }
  463. data->temp_input = val;
  464. val = jc42_read_value(client, JC42_REG_TEMP_CRITICAL);
  465. if (val < 0) {
  466. ret = ERR_PTR(val);
  467. goto abort;
  468. }
  469. data->temp_crit = val;
  470. val = jc42_read_value(client, JC42_REG_TEMP_LOWER);
  471. if (val < 0) {
  472. ret = ERR_PTR(val);
  473. goto abort;
  474. }
  475. data->temp_min = val;
  476. val = jc42_read_value(client, JC42_REG_TEMP_UPPER);
  477. if (val < 0) {
  478. ret = ERR_PTR(val);
  479. goto abort;
  480. }
  481. data->temp_max = val;
  482. data->last_updated = jiffies;
  483. data->valid = true;
  484. }
  485. abort:
  486. mutex_unlock(&data->update_lock);
  487. return ret;
  488. }
  489. static int __init sensors_jc42_init(void)
  490. {
  491. return i2c_add_driver(&jc42_driver);
  492. }
  493. static void __exit sensors_jc42_exit(void)
  494. {
  495. i2c_del_driver(&jc42_driver);
  496. }
  497. MODULE_AUTHOR("Guenter Roeck <guenter.roeck@ericsson.com>");
  498. MODULE_DESCRIPTION("JC42 driver");
  499. MODULE_LICENSE("GPL");
  500. module_init(sensors_jc42_init);
  501. module_exit(sensors_jc42_exit);