lm95241.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. /* Insmod parameters */
  40. I2C_CLIENT_INSMOD_1(lm95241);
  41. /* LM95241 registers */
  42. #define LM95241_REG_R_MAN_ID 0xFE
  43. #define LM95241_REG_R_CHIP_ID 0xFF
  44. #define LM95241_REG_R_STATUS 0x02
  45. #define LM95241_REG_RW_CONFIG 0x03
  46. #define LM95241_REG_RW_REM_FILTER 0x06
  47. #define LM95241_REG_RW_TRUTHERM 0x07
  48. #define LM95241_REG_W_ONE_SHOT 0x0F
  49. #define LM95241_REG_R_LOCAL_TEMPH 0x10
  50. #define LM95241_REG_R_REMOTE1_TEMPH 0x11
  51. #define LM95241_REG_R_REMOTE2_TEMPH 0x12
  52. #define LM95241_REG_R_LOCAL_TEMPL 0x20
  53. #define LM95241_REG_R_REMOTE1_TEMPL 0x21
  54. #define LM95241_REG_R_REMOTE2_TEMPL 0x22
  55. #define LM95241_REG_RW_REMOTE_MODEL 0x30
  56. /* LM95241 specific bitfields */
  57. #define CFG_STOP 0x40
  58. #define CFG_CR0076 0x00
  59. #define CFG_CR0182 0x10
  60. #define CFG_CR1000 0x20
  61. #define CFG_CR2700 0x30
  62. #define R1MS_SHIFT 0
  63. #define R2MS_SHIFT 2
  64. #define R1MS_MASK (0x01 << (R1MS_SHIFT))
  65. #define R2MS_MASK (0x01 << (R2MS_SHIFT))
  66. #define R1DF_SHIFT 1
  67. #define R2DF_SHIFT 2
  68. #define R1DF_MASK (0x01 << (R1DF_SHIFT))
  69. #define R2DF_MASK (0x01 << (R2DF_SHIFT))
  70. #define R1FE_MASK 0x01
  71. #define R2FE_MASK 0x05
  72. #define TT1_SHIFT 0
  73. #define TT2_SHIFT 4
  74. #define TT_OFF 0
  75. #define TT_ON 1
  76. #define TT_MASK 7
  77. #define MANUFACTURER_ID 0x01
  78. #define DEFAULT_REVISION 0xA4
  79. /* Conversions and various macros */
  80. #define TEMP_FROM_REG(val_h, val_l) (((val_h) & 0x80 ? (val_h) - 0x100 : \
  81. (val_h)) * 1000 + (val_l) * 1000 / 256)
  82. /* Functions declaration */
  83. static void lm95241_init_client(struct i2c_client *client);
  84. static struct lm95241_data *lm95241_update_device(struct device *dev);
  85. /* Client data (each client gets its own) */
  86. struct lm95241_data {
  87. struct device *hwmon_dev;
  88. struct mutex update_lock;
  89. unsigned long last_updated, rate; /* in jiffies */
  90. char valid; /* zero until following fields are valid */
  91. /* registers values */
  92. u8 local_h, local_l; /* local */
  93. u8 remote1_h, remote1_l; /* remote1 */
  94. u8 remote2_h, remote2_l; /* remote2 */
  95. u8 config, model, trutherm;
  96. };
  97. /* Sysfs stuff */
  98. #define show_temp(value) \
  99. static ssize_t show_##value(struct device *dev, \
  100. struct device_attribute *attr, char *buf) \
  101. { \
  102. struct lm95241_data *data = lm95241_update_device(dev); \
  103. snprintf(buf, PAGE_SIZE - 1, "%d\n", \
  104. TEMP_FROM_REG(data->value##_h, data->value##_l)); \
  105. return strlen(buf); \
  106. }
  107. show_temp(local);
  108. show_temp(remote1);
  109. show_temp(remote2);
  110. static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
  111. char *buf)
  112. {
  113. struct lm95241_data *data = lm95241_update_device(dev);
  114. snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->rate / HZ);
  115. return strlen(buf);
  116. }
  117. static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
  118. const char *buf, size_t count)
  119. {
  120. struct i2c_client *client = to_i2c_client(dev);
  121. struct lm95241_data *data = i2c_get_clientdata(client);
  122. strict_strtol(buf, 10, &data->rate);
  123. data->rate = data->rate * 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. strict_strtol(buf, 10, &val); \
  177. \
  178. if ((val == 1) || (val == 2)) { \
  179. \
  180. mutex_lock(&data->update_lock); \
  181. \
  182. data->trutherm &= ~(TT_MASK << TT##flag##_SHIFT); \
  183. if (val == 1) { \
  184. data->model |= R##flag##MS_MASK; \
  185. data->trutherm |= (TT_ON << TT##flag##_SHIFT); \
  186. } \
  187. else { \
  188. data->model &= ~R##flag##MS_MASK; \
  189. data->trutherm |= (TT_OFF << TT##flag##_SHIFT); \
  190. } \
  191. \
  192. data->valid = 0; \
  193. \
  194. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, \
  195. data->model); \
  196. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, \
  197. data->trutherm); \
  198. \
  199. mutex_unlock(&data->update_lock); \
  200. \
  201. } \
  202. return count; \
  203. }
  204. set_type(1);
  205. set_type(2);
  206. #define set_min(flag) \
  207. static ssize_t set_min##flag(struct device *dev, \
  208. struct device_attribute *devattr, const char *buf, size_t count) \
  209. { \
  210. struct i2c_client *client = to_i2c_client(dev); \
  211. struct lm95241_data *data = i2c_get_clientdata(client); \
  212. \
  213. long val; \
  214. strict_strtol(buf, 10, &val); \
  215. \
  216. mutex_lock(&data->update_lock); \
  217. \
  218. if (val < 0) \
  219. data->config |= R##flag##DF_MASK; \
  220. else \
  221. data->config &= ~R##flag##DF_MASK; \
  222. \
  223. data->valid = 0; \
  224. \
  225. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  226. data->config); \
  227. \
  228. mutex_unlock(&data->update_lock); \
  229. \
  230. return count; \
  231. }
  232. set_min(1);
  233. set_min(2);
  234. #define set_max(flag) \
  235. static ssize_t set_max##flag(struct device *dev, \
  236. struct device_attribute *devattr, const char *buf, size_t count) \
  237. { \
  238. struct i2c_client *client = to_i2c_client(dev); \
  239. struct lm95241_data *data = i2c_get_clientdata(client); \
  240. \
  241. long val; \
  242. strict_strtol(buf, 10, &val); \
  243. \
  244. mutex_lock(&data->update_lock); \
  245. \
  246. if (val <= 127000) \
  247. data->config |= R##flag##DF_MASK; \
  248. else \
  249. data->config &= ~R##flag##DF_MASK; \
  250. \
  251. data->valid = 0; \
  252. \
  253. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  254. data->config); \
  255. \
  256. mutex_unlock(&data->update_lock); \
  257. \
  258. return count; \
  259. }
  260. set_max(1);
  261. set_max(2);
  262. static DEVICE_ATTR(temp1_input, S_IRUGO, show_local, NULL);
  263. static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote1, NULL);
  264. static DEVICE_ATTR(temp3_input, S_IRUGO, show_remote2, NULL);
  265. static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type1, set_type1);
  266. static DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type2, set_type2);
  267. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min1, set_min1);
  268. static DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min2, set_min2);
  269. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max1, set_max1);
  270. static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max2, set_max2);
  271. static DEVICE_ATTR(rate, S_IWUSR | S_IRUGO, show_rate, set_rate);
  272. static struct attribute *lm95241_attributes[] = {
  273. &dev_attr_temp1_input.attr,
  274. &dev_attr_temp2_input.attr,
  275. &dev_attr_temp3_input.attr,
  276. &dev_attr_temp2_type.attr,
  277. &dev_attr_temp3_type.attr,
  278. &dev_attr_temp2_min.attr,
  279. &dev_attr_temp3_min.attr,
  280. &dev_attr_temp2_max.attr,
  281. &dev_attr_temp3_max.attr,
  282. &dev_attr_rate.attr,
  283. NULL
  284. };
  285. static const struct attribute_group lm95241_group = {
  286. .attrs = lm95241_attributes,
  287. };
  288. /* Return 0 if detection is successful, -ENODEV otherwise */
  289. static int lm95241_detect(struct i2c_client *new_client, int kind,
  290. struct i2c_board_info *info)
  291. {
  292. struct i2c_adapter *adapter = new_client->adapter;
  293. int address = new_client->addr;
  294. const char *name = "";
  295. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  296. return -ENODEV;
  297. /*
  298. * Now we do the remaining detection. A negative kind means that
  299. * the driver was loaded with no force parameter (default), so we
  300. * must both detect and identify the chip. A zero kind means that
  301. * the driver was loaded with the force parameter, the detection
  302. * step shall be skipped. A positive kind means that the driver
  303. * was loaded with the force parameter and a given kind of chip is
  304. * requested, so both the detection and the identification steps
  305. * are skipped.
  306. */
  307. if (kind < 0) { /* detection */
  308. if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
  309. != MANUFACTURER_ID)
  310. || (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
  311. < DEFAULT_REVISION)) {
  312. dev_dbg(&adapter->dev,
  313. "LM95241 detection failed at 0x%02x.\n",
  314. address);
  315. return -ENODEV;
  316. }
  317. }
  318. if (kind <= 0) { /* identification */
  319. if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
  320. == MANUFACTURER_ID)
  321. && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
  322. >= DEFAULT_REVISION)) {
  323. kind = lm95241;
  324. if (kind <= 0) { /* identification failed */
  325. dev_info(&adapter->dev, "Unsupported chip\n");
  326. return -ENODEV;
  327. }
  328. }
  329. }
  330. /* Fill the i2c board info */
  331. if (kind == lm95241)
  332. name = "lm95241";
  333. strlcpy(info->type, name, I2C_NAME_SIZE);
  334. return 0;
  335. }
  336. static int lm95241_probe(struct i2c_client *new_client,
  337. const struct i2c_device_id *id)
  338. {
  339. struct lm95241_data *data;
  340. int err;
  341. data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
  342. if (!data) {
  343. err = -ENOMEM;
  344. goto exit;
  345. }
  346. i2c_set_clientdata(new_client, data);
  347. mutex_init(&data->update_lock);
  348. /* Initialize the LM95241 chip */
  349. lm95241_init_client(new_client);
  350. /* Register sysfs hooks */
  351. err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
  352. if (err)
  353. goto exit_free;
  354. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  355. if (IS_ERR(data->hwmon_dev)) {
  356. err = PTR_ERR(data->hwmon_dev);
  357. goto exit_remove_files;
  358. }
  359. return 0;
  360. exit_remove_files:
  361. sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
  362. exit_free:
  363. kfree(data);
  364. exit:
  365. return err;
  366. }
  367. static void lm95241_init_client(struct i2c_client *client)
  368. {
  369. struct lm95241_data *data = i2c_get_clientdata(client);
  370. data->rate = HZ; /* 1 sec default */
  371. data->valid = 0;
  372. data->config = CFG_CR0076;
  373. data->model = 0;
  374. data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
  375. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
  376. data->config);
  377. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
  378. R1FE_MASK | R2FE_MASK);
  379. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  380. data->trutherm);
  381. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  382. data->model);
  383. }
  384. static int lm95241_remove(struct i2c_client *client)
  385. {
  386. struct lm95241_data *data = i2c_get_clientdata(client);
  387. hwmon_device_unregister(data->hwmon_dev);
  388. sysfs_remove_group(&client->dev.kobj, &lm95241_group);
  389. i2c_set_clientdata(client, NULL);
  390. kfree(data);
  391. return 0;
  392. }
  393. static struct lm95241_data *lm95241_update_device(struct device *dev)
  394. {
  395. struct i2c_client *client = to_i2c_client(dev);
  396. struct lm95241_data *data = i2c_get_clientdata(client);
  397. mutex_lock(&data->update_lock);
  398. if (time_after(jiffies, data->last_updated + data->rate) ||
  399. !data->valid) {
  400. dev_dbg(&client->dev, "Updating lm95241 data.\n");
  401. data->local_h =
  402. i2c_smbus_read_byte_data(client,
  403. LM95241_REG_R_LOCAL_TEMPH);
  404. data->local_l =
  405. i2c_smbus_read_byte_data(client,
  406. LM95241_REG_R_LOCAL_TEMPL);
  407. data->remote1_h =
  408. i2c_smbus_read_byte_data(client,
  409. LM95241_REG_R_REMOTE1_TEMPH);
  410. data->remote1_l =
  411. i2c_smbus_read_byte_data(client,
  412. LM95241_REG_R_REMOTE1_TEMPL);
  413. data->remote2_h =
  414. i2c_smbus_read_byte_data(client,
  415. LM95241_REG_R_REMOTE2_TEMPH);
  416. data->remote2_l =
  417. i2c_smbus_read_byte_data(client,
  418. LM95241_REG_R_REMOTE2_TEMPL);
  419. data->last_updated = jiffies;
  420. data->valid = 1;
  421. }
  422. mutex_unlock(&data->update_lock);
  423. return data;
  424. }
  425. /* Driver data (common to all clients) */
  426. static const struct i2c_device_id lm95241_id[] = {
  427. { "lm95241", lm95241 },
  428. { }
  429. };
  430. MODULE_DEVICE_TABLE(i2c, lm95241_id);
  431. static struct i2c_driver lm95241_driver = {
  432. .class = I2C_CLASS_HWMON,
  433. .driver = {
  434. .name = "lm95241",
  435. },
  436. .probe = lm95241_probe,
  437. .remove = lm95241_remove,
  438. .id_table = lm95241_id,
  439. .detect = lm95241_detect,
  440. .address_data = &addr_data,
  441. };
  442. static int __init sensors_lm95241_init(void)
  443. {
  444. return i2c_add_driver(&lm95241_driver);
  445. }
  446. static void __exit sensors_lm95241_exit(void)
  447. {
  448. i2c_del_driver(&lm95241_driver);
  449. }
  450. MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
  451. MODULE_DESCRIPTION("LM95241 sensor driver");
  452. MODULE_LICENSE("GPL");
  453. module_init(sensors_lm95241_init);
  454. module_exit(sensors_lm95241_exit);