lm95241.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * lm95241.c - Part of lm_sensors, Linux kernel modules for hardware
  3. * monitoring
  4. * Copyright (C) 2008 Davide Rizzo <elpa-rizzo@gmail.com>
  5. *
  6. * Based on the max1619 driver. The LM95241 is a sensor chip made by National
  7. * Semiconductors.
  8. * It reports up to three temperatures (its own plus up to
  9. * two external ones). Complete datasheet can be
  10. * obtained from National's website at:
  11. * http://www.national.com/ds.cgi/LM/LM95241.pdf
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/i2c.h>
  32. #include <linux/hwmon.h>
  33. #include <linux/hwmon-sysfs.h>
  34. #include <linux/err.h>
  35. #include <linux/mutex.h>
  36. #include <linux/sysfs.h>
  37. static const unsigned short normal_i2c[] = {
  38. 0x19, 0x2a, 0x2b, I2C_CLIENT_END};
  39. /* LM95241 registers */
  40. #define LM95241_REG_R_MAN_ID 0xFE
  41. #define LM95241_REG_R_CHIP_ID 0xFF
  42. #define LM95241_REG_R_STATUS 0x02
  43. #define LM95241_REG_RW_CONFIG 0x03
  44. #define LM95241_REG_RW_REM_FILTER 0x06
  45. #define LM95241_REG_RW_TRUTHERM 0x07
  46. #define LM95241_REG_W_ONE_SHOT 0x0F
  47. #define LM95241_REG_R_LOCAL_TEMPH 0x10
  48. #define LM95241_REG_R_REMOTE1_TEMPH 0x11
  49. #define LM95241_REG_R_REMOTE2_TEMPH 0x12
  50. #define LM95241_REG_R_LOCAL_TEMPL 0x20
  51. #define LM95241_REG_R_REMOTE1_TEMPL 0x21
  52. #define LM95241_REG_R_REMOTE2_TEMPL 0x22
  53. #define LM95241_REG_RW_REMOTE_MODEL 0x30
  54. /* LM95241 specific bitfields */
  55. #define CFG_STOP 0x40
  56. #define CFG_CR0076 0x00
  57. #define CFG_CR0182 0x10
  58. #define CFG_CR1000 0x20
  59. #define CFG_CR2700 0x30
  60. #define R1MS_SHIFT 0
  61. #define R2MS_SHIFT 2
  62. #define R1MS_MASK (0x01 << (R1MS_SHIFT))
  63. #define R2MS_MASK (0x01 << (R2MS_SHIFT))
  64. #define R1DF_SHIFT 1
  65. #define R2DF_SHIFT 2
  66. #define R1DF_MASK (0x01 << (R1DF_SHIFT))
  67. #define R2DF_MASK (0x01 << (R2DF_SHIFT))
  68. #define R1FE_MASK 0x01
  69. #define R2FE_MASK 0x05
  70. #define TT1_SHIFT 0
  71. #define TT2_SHIFT 4
  72. #define TT_OFF 0
  73. #define TT_ON 1
  74. #define TT_MASK 7
  75. #define MANUFACTURER_ID 0x01
  76. #define DEFAULT_REVISION 0xA4
  77. /* Conversions and various macros */
  78. #define TEMP_FROM_REG(val_h, val_l) (((val_h) & 0x80 ? (val_h) - 0x100 : \
  79. (val_h)) * 1000 + (val_l) * 1000 / 256)
  80. /* Functions declaration */
  81. static void lm95241_init_client(struct i2c_client *client);
  82. static struct lm95241_data *lm95241_update_device(struct device *dev);
  83. /* Client data (each client gets its own) */
  84. struct lm95241_data {
  85. struct device *hwmon_dev;
  86. struct mutex update_lock;
  87. unsigned long last_updated, interval; /* in jiffies */
  88. char valid; /* zero until following fields are valid */
  89. /* registers values */
  90. u8 local_h, local_l; /* local */
  91. u8 remote1_h, remote1_l; /* remote1 */
  92. u8 remote2_h, remote2_l; /* remote2 */
  93. u8 config, model, trutherm;
  94. };
  95. /* Sysfs stuff */
  96. #define show_temp(value) \
  97. static ssize_t show_##value(struct device *dev, \
  98. struct device_attribute *attr, char *buf) \
  99. { \
  100. struct lm95241_data *data = lm95241_update_device(dev); \
  101. snprintf(buf, PAGE_SIZE - 1, "%d\n", \
  102. TEMP_FROM_REG(data->value##_h, data->value##_l)); \
  103. return strlen(buf); \
  104. }
  105. show_temp(local);
  106. show_temp(remote1);
  107. show_temp(remote2);
  108. static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  109. char *buf)
  110. {
  111. struct lm95241_data *data = lm95241_update_device(dev);
  112. snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval / HZ);
  113. return strlen(buf);
  114. }
  115. static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  116. const char *buf, size_t count)
  117. {
  118. struct i2c_client *client = to_i2c_client(dev);
  119. struct lm95241_data *data = i2c_get_clientdata(client);
  120. unsigned long val;
  121. if (strict_strtoul(buf, 10, &val) < 0)
  122. return -EINVAL;
  123. data->interval = val * HZ / 1000;
  124. return count;
  125. }
  126. #define show_type(flag) \
  127. static ssize_t show_type##flag(struct device *dev, \
  128. struct device_attribute *attr, char *buf) \
  129. { \
  130. struct i2c_client *client = to_i2c_client(dev); \
  131. struct lm95241_data *data = i2c_get_clientdata(client); \
  132. \
  133. snprintf(buf, PAGE_SIZE - 1, \
  134. data->model & R##flag##MS_MASK ? "1\n" : "2\n"); \
  135. return strlen(buf); \
  136. }
  137. show_type(1);
  138. show_type(2);
  139. #define show_min(flag) \
  140. static ssize_t show_min##flag(struct device *dev, \
  141. struct device_attribute *attr, char *buf) \
  142. { \
  143. struct i2c_client *client = to_i2c_client(dev); \
  144. struct lm95241_data *data = i2c_get_clientdata(client); \
  145. \
  146. snprintf(buf, PAGE_SIZE - 1, \
  147. data->config & R##flag##DF_MASK ? \
  148. "-127000\n" : "0\n"); \
  149. return strlen(buf); \
  150. }
  151. show_min(1);
  152. show_min(2);
  153. #define show_max(flag) \
  154. static ssize_t show_max##flag(struct device *dev, \
  155. struct device_attribute *attr, char *buf) \
  156. { \
  157. struct i2c_client *client = to_i2c_client(dev); \
  158. struct lm95241_data *data = i2c_get_clientdata(client); \
  159. \
  160. snprintf(buf, PAGE_SIZE - 1, \
  161. data->config & R##flag##DF_MASK ? \
  162. "127000\n" : "255000\n"); \
  163. return strlen(buf); \
  164. }
  165. show_max(1);
  166. show_max(2);
  167. #define set_type(flag) \
  168. static ssize_t set_type##flag(struct device *dev, \
  169. struct device_attribute *attr, \
  170. const char *buf, size_t count) \
  171. { \
  172. struct i2c_client *client = to_i2c_client(dev); \
  173. struct lm95241_data *data = i2c_get_clientdata(client); \
  174. \
  175. long val; \
  176. \
  177. if (strict_strtol(buf, 10, &val) < 0) \
  178. return -EINVAL; \
  179. \
  180. if ((val == 1) || (val == 2)) { \
  181. \
  182. mutex_lock(&data->update_lock); \
  183. \
  184. data->trutherm &= ~(TT_MASK << TT##flag##_SHIFT); \
  185. if (val == 1) { \
  186. data->model |= R##flag##MS_MASK; \
  187. data->trutherm |= (TT_ON << TT##flag##_SHIFT); \
  188. } \
  189. else { \
  190. data->model &= ~R##flag##MS_MASK; \
  191. data->trutherm |= (TT_OFF << TT##flag##_SHIFT); \
  192. } \
  193. \
  194. data->valid = 0; \
  195. \
  196. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, \
  197. data->model); \
  198. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, \
  199. data->trutherm); \
  200. \
  201. mutex_unlock(&data->update_lock); \
  202. \
  203. } \
  204. return count; \
  205. }
  206. set_type(1);
  207. set_type(2);
  208. #define set_min(flag) \
  209. static ssize_t set_min##flag(struct device *dev, \
  210. struct device_attribute *devattr, const char *buf, size_t count) \
  211. { \
  212. struct i2c_client *client = to_i2c_client(dev); \
  213. struct lm95241_data *data = i2c_get_clientdata(client); \
  214. \
  215. long val; \
  216. \
  217. if (strict_strtol(buf, 10, &val) < 0) \
  218. return -EINVAL;\
  219. \
  220. mutex_lock(&data->update_lock); \
  221. \
  222. if (val < 0) \
  223. data->config |= R##flag##DF_MASK; \
  224. else \
  225. data->config &= ~R##flag##DF_MASK; \
  226. \
  227. data->valid = 0; \
  228. \
  229. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  230. data->config); \
  231. \
  232. mutex_unlock(&data->update_lock); \
  233. \
  234. return count; \
  235. }
  236. set_min(1);
  237. set_min(2);
  238. #define set_max(flag) \
  239. static ssize_t set_max##flag(struct device *dev, \
  240. struct device_attribute *devattr, const char *buf, size_t count) \
  241. { \
  242. struct i2c_client *client = to_i2c_client(dev); \
  243. struct lm95241_data *data = i2c_get_clientdata(client); \
  244. \
  245. long val; \
  246. \
  247. if (strict_strtol(buf, 10, &val) < 0) \
  248. return -EINVAL; \
  249. \
  250. mutex_lock(&data->update_lock); \
  251. \
  252. if (val <= 127000) \
  253. data->config |= R##flag##DF_MASK; \
  254. else \
  255. data->config &= ~R##flag##DF_MASK; \
  256. \
  257. data->valid = 0; \
  258. \
  259. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  260. data->config); \
  261. \
  262. mutex_unlock(&data->update_lock); \
  263. \
  264. return count; \
  265. }
  266. set_max(1);
  267. set_max(2);
  268. static DEVICE_ATTR(temp1_input, S_IRUGO, show_local, NULL);
  269. static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote1, NULL);
  270. static DEVICE_ATTR(temp3_input, S_IRUGO, show_remote2, NULL);
  271. static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type1, set_type1);
  272. static DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type2, set_type2);
  273. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min1, set_min1);
  274. static DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min2, set_min2);
  275. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max1, set_max1);
  276. static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max2, set_max2);
  277. static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
  278. set_interval);
  279. static struct attribute *lm95241_attributes[] = {
  280. &dev_attr_temp1_input.attr,
  281. &dev_attr_temp2_input.attr,
  282. &dev_attr_temp3_input.attr,
  283. &dev_attr_temp2_type.attr,
  284. &dev_attr_temp3_type.attr,
  285. &dev_attr_temp2_min.attr,
  286. &dev_attr_temp3_min.attr,
  287. &dev_attr_temp2_max.attr,
  288. &dev_attr_temp3_max.attr,
  289. &dev_attr_update_interval.attr,
  290. NULL
  291. };
  292. static const struct attribute_group lm95241_group = {
  293. .attrs = lm95241_attributes,
  294. };
  295. /* Return 0 if detection is successful, -ENODEV otherwise */
  296. static int lm95241_detect(struct i2c_client *new_client,
  297. struct i2c_board_info *info)
  298. {
  299. struct i2c_adapter *adapter = new_client->adapter;
  300. int address = new_client->addr;
  301. const char *name;
  302. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  303. return -ENODEV;
  304. if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
  305. == MANUFACTURER_ID)
  306. && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
  307. >= DEFAULT_REVISION)) {
  308. name = "lm95241";
  309. } else {
  310. dev_dbg(&adapter->dev, "LM95241 detection failed at 0x%02x\n",
  311. address);
  312. return -ENODEV;
  313. }
  314. /* Fill the i2c board info */
  315. strlcpy(info->type, name, I2C_NAME_SIZE);
  316. return 0;
  317. }
  318. static int lm95241_probe(struct i2c_client *new_client,
  319. const struct i2c_device_id *id)
  320. {
  321. struct lm95241_data *data;
  322. int err;
  323. data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
  324. if (!data) {
  325. err = -ENOMEM;
  326. goto exit;
  327. }
  328. i2c_set_clientdata(new_client, data);
  329. mutex_init(&data->update_lock);
  330. /* Initialize the LM95241 chip */
  331. lm95241_init_client(new_client);
  332. /* Register sysfs hooks */
  333. err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
  334. if (err)
  335. goto exit_free;
  336. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  337. if (IS_ERR(data->hwmon_dev)) {
  338. err = PTR_ERR(data->hwmon_dev);
  339. goto exit_remove_files;
  340. }
  341. return 0;
  342. exit_remove_files:
  343. sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
  344. exit_free:
  345. kfree(data);
  346. exit:
  347. return err;
  348. }
  349. static void lm95241_init_client(struct i2c_client *client)
  350. {
  351. struct lm95241_data *data = i2c_get_clientdata(client);
  352. data->interval = HZ; /* 1 sec default */
  353. data->valid = 0;
  354. data->config = CFG_CR0076;
  355. data->model = 0;
  356. data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
  357. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
  358. data->config);
  359. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
  360. R1FE_MASK | R2FE_MASK);
  361. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  362. data->trutherm);
  363. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  364. data->model);
  365. }
  366. static int lm95241_remove(struct i2c_client *client)
  367. {
  368. struct lm95241_data *data = i2c_get_clientdata(client);
  369. hwmon_device_unregister(data->hwmon_dev);
  370. sysfs_remove_group(&client->dev.kobj, &lm95241_group);
  371. kfree(data);
  372. return 0;
  373. }
  374. static struct lm95241_data *lm95241_update_device(struct device *dev)
  375. {
  376. struct i2c_client *client = to_i2c_client(dev);
  377. struct lm95241_data *data = i2c_get_clientdata(client);
  378. mutex_lock(&data->update_lock);
  379. if (time_after(jiffies, data->last_updated + data->interval) ||
  380. !data->valid) {
  381. dev_dbg(&client->dev, "Updating lm95241 data.\n");
  382. data->local_h =
  383. i2c_smbus_read_byte_data(client,
  384. LM95241_REG_R_LOCAL_TEMPH);
  385. data->local_l =
  386. i2c_smbus_read_byte_data(client,
  387. LM95241_REG_R_LOCAL_TEMPL);
  388. data->remote1_h =
  389. i2c_smbus_read_byte_data(client,
  390. LM95241_REG_R_REMOTE1_TEMPH);
  391. data->remote1_l =
  392. i2c_smbus_read_byte_data(client,
  393. LM95241_REG_R_REMOTE1_TEMPL);
  394. data->remote2_h =
  395. i2c_smbus_read_byte_data(client,
  396. LM95241_REG_R_REMOTE2_TEMPH);
  397. data->remote2_l =
  398. i2c_smbus_read_byte_data(client,
  399. LM95241_REG_R_REMOTE2_TEMPL);
  400. data->last_updated = jiffies;
  401. data->valid = 1;
  402. }
  403. mutex_unlock(&data->update_lock);
  404. return data;
  405. }
  406. /* Driver data (common to all clients) */
  407. static const struct i2c_device_id lm95241_id[] = {
  408. { "lm95241", 0 },
  409. { }
  410. };
  411. MODULE_DEVICE_TABLE(i2c, lm95241_id);
  412. static struct i2c_driver lm95241_driver = {
  413. .class = I2C_CLASS_HWMON,
  414. .driver = {
  415. .name = "lm95241",
  416. },
  417. .probe = lm95241_probe,
  418. .remove = lm95241_remove,
  419. .id_table = lm95241_id,
  420. .detect = lm95241_detect,
  421. .address_list = normal_i2c,
  422. };
  423. static int __init sensors_lm95241_init(void)
  424. {
  425. return i2c_add_driver(&lm95241_driver);
  426. }
  427. static void __exit sensors_lm95241_exit(void)
  428. {
  429. i2c_del_driver(&lm95241_driver);
  430. }
  431. MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
  432. MODULE_DESCRIPTION("LM95241 sensor driver");
  433. MODULE_LICENSE("GPL");
  434. module_init(sensors_lm95241_init);
  435. module_exit(sensors_lm95241_exit);