lm95241.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 int lm95241_attach_adapter(struct i2c_adapter *adapter);
  84. static int lm95241_detect(struct i2c_adapter *adapter, int address,
  85. int kind);
  86. static void lm95241_init_client(struct i2c_client *client);
  87. static int lm95241_detach_client(struct i2c_client *client);
  88. static struct lm95241_data *lm95241_update_device(struct device *dev);
  89. /* Driver data (common to all clients) */
  90. static struct i2c_driver lm95241_driver = {
  91. .driver = {
  92. .name = "lm95241",
  93. },
  94. .attach_adapter = lm95241_attach_adapter,
  95. .detach_client = lm95241_detach_client,
  96. };
  97. /* Client data (each client gets its own) */
  98. struct lm95241_data {
  99. struct i2c_client client;
  100. struct device *hwmon_dev;
  101. struct mutex update_lock;
  102. unsigned long last_updated, rate; /* in jiffies */
  103. char valid; /* zero until following fields are valid */
  104. /* registers values */
  105. u8 local_h, local_l; /* local */
  106. u8 remote1_h, remote1_l; /* remote1 */
  107. u8 remote2_h, remote2_l; /* remote2 */
  108. u8 config, model, trutherm;
  109. };
  110. /* Sysfs stuff */
  111. #define show_temp(value) \
  112. static ssize_t show_##value(struct device *dev, \
  113. struct device_attribute *attr, char *buf) \
  114. { \
  115. struct lm95241_data *data = lm95241_update_device(dev); \
  116. snprintf(buf, PAGE_SIZE - 1, "%d\n", \
  117. TEMP_FROM_REG(data->value##_h, data->value##_l)); \
  118. return strlen(buf); \
  119. }
  120. show_temp(local);
  121. show_temp(remote1);
  122. show_temp(remote2);
  123. static ssize_t show_rate(struct device *dev, struct device_attribute *attr,
  124. char *buf)
  125. {
  126. struct lm95241_data *data = lm95241_update_device(dev);
  127. snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->rate / HZ);
  128. return strlen(buf);
  129. }
  130. static ssize_t set_rate(struct device *dev, struct device_attribute *attr,
  131. const char *buf, size_t count)
  132. {
  133. struct i2c_client *client = to_i2c_client(dev);
  134. struct lm95241_data *data = i2c_get_clientdata(client);
  135. strict_strtol(buf, 10, &data->rate);
  136. data->rate = data->rate * HZ / 1000;
  137. return count;
  138. }
  139. #define show_type(flag) \
  140. static ssize_t show_type##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->model & R##flag##MS_MASK ? "1\n" : "2\n"); \
  148. return strlen(buf); \
  149. }
  150. show_type(1);
  151. show_type(2);
  152. #define show_min(flag) \
  153. static ssize_t show_min##flag(struct device *dev, \
  154. struct device_attribute *attr, char *buf) \
  155. { \
  156. struct i2c_client *client = to_i2c_client(dev); \
  157. struct lm95241_data *data = i2c_get_clientdata(client); \
  158. \
  159. snprintf(buf, PAGE_SIZE - 1, \
  160. data->config & R##flag##DF_MASK ? \
  161. "-127000\n" : "0\n"); \
  162. return strlen(buf); \
  163. }
  164. show_min(1);
  165. show_min(2);
  166. #define show_max(flag) \
  167. static ssize_t show_max##flag(struct device *dev, \
  168. struct device_attribute *attr, char *buf) \
  169. { \
  170. struct i2c_client *client = to_i2c_client(dev); \
  171. struct lm95241_data *data = i2c_get_clientdata(client); \
  172. \
  173. snprintf(buf, PAGE_SIZE - 1, \
  174. data->config & R##flag##DF_MASK ? \
  175. "127000\n" : "255000\n"); \
  176. return strlen(buf); \
  177. }
  178. show_max(1);
  179. show_max(2);
  180. #define set_type(flag) \
  181. static ssize_t set_type##flag(struct device *dev, \
  182. struct device_attribute *attr, \
  183. const char *buf, size_t count) \
  184. { \
  185. struct i2c_client *client = to_i2c_client(dev); \
  186. struct lm95241_data *data = i2c_get_clientdata(client); \
  187. \
  188. long val; \
  189. strict_strtol(buf, 10, &val); \
  190. \
  191. if ((val == 1) || (val == 2)) { \
  192. \
  193. mutex_lock(&data->update_lock); \
  194. \
  195. data->trutherm &= ~(TT_MASK << TT##flag##_SHIFT); \
  196. if (val == 1) { \
  197. data->model |= R##flag##MS_MASK; \
  198. data->trutherm |= (TT_ON << TT##flag##_SHIFT); \
  199. } \
  200. else { \
  201. data->model &= ~R##flag##MS_MASK; \
  202. data->trutherm |= (TT_OFF << TT##flag##_SHIFT); \
  203. } \
  204. \
  205. data->valid = 0; \
  206. \
  207. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, \
  208. data->model); \
  209. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, \
  210. data->trutherm); \
  211. \
  212. mutex_unlock(&data->update_lock); \
  213. \
  214. } \
  215. return count; \
  216. }
  217. set_type(1);
  218. set_type(2);
  219. #define set_min(flag) \
  220. static ssize_t set_min##flag(struct device *dev, \
  221. struct device_attribute *devattr, const char *buf, size_t count) \
  222. { \
  223. struct i2c_client *client = to_i2c_client(dev); \
  224. struct lm95241_data *data = i2c_get_clientdata(client); \
  225. \
  226. long val; \
  227. strict_strtol(buf, 10, &val); \
  228. \
  229. mutex_lock(&data->update_lock); \
  230. \
  231. if (val < 0) \
  232. data->config |= R##flag##DF_MASK; \
  233. else \
  234. data->config &= ~R##flag##DF_MASK; \
  235. \
  236. data->valid = 0; \
  237. \
  238. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  239. data->config); \
  240. \
  241. mutex_unlock(&data->update_lock); \
  242. \
  243. return count; \
  244. }
  245. set_min(1);
  246. set_min(2);
  247. #define set_max(flag) \
  248. static ssize_t set_max##flag(struct device *dev, \
  249. struct device_attribute *devattr, const char *buf, size_t count) \
  250. { \
  251. struct i2c_client *client = to_i2c_client(dev); \
  252. struct lm95241_data *data = i2c_get_clientdata(client); \
  253. \
  254. long val; \
  255. strict_strtol(buf, 10, &val); \
  256. \
  257. mutex_lock(&data->update_lock); \
  258. \
  259. if (val <= 127000) \
  260. data->config |= R##flag##DF_MASK; \
  261. else \
  262. data->config &= ~R##flag##DF_MASK; \
  263. \
  264. data->valid = 0; \
  265. \
  266. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  267. data->config); \
  268. \
  269. mutex_unlock(&data->update_lock); \
  270. \
  271. return count; \
  272. }
  273. set_max(1);
  274. set_max(2);
  275. static DEVICE_ATTR(temp1_input, S_IRUGO, show_local, NULL);
  276. static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote1, NULL);
  277. static DEVICE_ATTR(temp3_input, S_IRUGO, show_remote2, NULL);
  278. static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type1, set_type1);
  279. static DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type2, set_type2);
  280. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min1, set_min1);
  281. static DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min2, set_min2);
  282. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max1, set_max1);
  283. static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max2, set_max2);
  284. static DEVICE_ATTR(rate, S_IWUSR | S_IRUGO, show_rate, set_rate);
  285. static struct attribute *lm95241_attributes[] = {
  286. &dev_attr_temp1_input.attr,
  287. &dev_attr_temp2_input.attr,
  288. &dev_attr_temp3_input.attr,
  289. &dev_attr_temp2_type.attr,
  290. &dev_attr_temp3_type.attr,
  291. &dev_attr_temp2_min.attr,
  292. &dev_attr_temp3_min.attr,
  293. &dev_attr_temp2_max.attr,
  294. &dev_attr_temp3_max.attr,
  295. &dev_attr_rate.attr,
  296. NULL
  297. };
  298. static const struct attribute_group lm95241_group = {
  299. .attrs = lm95241_attributes,
  300. };
  301. /* Init/exit code */
  302. static int lm95241_attach_adapter(struct i2c_adapter *adapter)
  303. {
  304. if (!(adapter->class & I2C_CLASS_HWMON))
  305. return 0;
  306. return i2c_probe(adapter, &addr_data, lm95241_detect);
  307. }
  308. /*
  309. * The following function does more than just detection. If detection
  310. * succeeds, it also registers the new chip.
  311. */
  312. static int lm95241_detect(struct i2c_adapter *adapter, int address, int kind)
  313. {
  314. struct i2c_client *new_client;
  315. struct lm95241_data *data;
  316. int err = 0;
  317. const char *name = "";
  318. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  319. goto exit;
  320. data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
  321. if (!data) {
  322. err = -ENOMEM;
  323. goto exit;
  324. }
  325. /* The common I2C client data is placed right before the
  326. LM95241-specific data. */
  327. new_client = &data->client;
  328. i2c_set_clientdata(new_client, data);
  329. new_client->addr = address;
  330. new_client->adapter = adapter;
  331. new_client->driver = &lm95241_driver;
  332. new_client->flags = 0;
  333. /*
  334. * Now we do the remaining detection. A negative kind means that
  335. * the driver was loaded with no force parameter (default), so we
  336. * must both detect and identify the chip. A zero kind means that
  337. * the driver was loaded with the force parameter, the detection
  338. * step shall be skipped. A positive kind means that the driver
  339. * was loaded with the force parameter and a given kind of chip is
  340. * requested, so both the detection and the identification steps
  341. * are skipped.
  342. */
  343. if (kind < 0) { /* detection */
  344. if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
  345. != MANUFACTURER_ID)
  346. || (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
  347. < DEFAULT_REVISION)) {
  348. dev_dbg(&adapter->dev,
  349. "LM95241 detection failed at 0x%02x.\n",
  350. address);
  351. goto exit_free;
  352. }
  353. }
  354. if (kind <= 0) { /* identification */
  355. if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
  356. == MANUFACTURER_ID)
  357. && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
  358. >= DEFAULT_REVISION)) {
  359. kind = lm95241;
  360. if (kind <= 0) { /* identification failed */
  361. dev_info(&adapter->dev, "Unsupported chip\n");
  362. goto exit_free;
  363. }
  364. }
  365. }
  366. if (kind == lm95241)
  367. name = "lm95241";
  368. /* We can fill in the remaining client fields */
  369. strlcpy(new_client->name, name, I2C_NAME_SIZE);
  370. data->valid = 0;
  371. mutex_init(&data->update_lock);
  372. /* Tell the I2C layer a new client has arrived */
  373. err = i2c_attach_client(new_client);
  374. if (err)
  375. goto exit_free;
  376. /* Initialize the LM95241 chip */
  377. lm95241_init_client(new_client);
  378. /* Register sysfs hooks */
  379. err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
  380. if (err)
  381. goto exit_detach;
  382. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  383. if (IS_ERR(data->hwmon_dev)) {
  384. err = PTR_ERR(data->hwmon_dev);
  385. goto exit_remove_files;
  386. }
  387. return 0;
  388. exit_remove_files:
  389. sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
  390. exit_detach:
  391. i2c_detach_client(new_client);
  392. exit_free:
  393. kfree(data);
  394. exit:
  395. return err;
  396. }
  397. static void lm95241_init_client(struct i2c_client *client)
  398. {
  399. struct lm95241_data *data = i2c_get_clientdata(client);
  400. data->rate = HZ; /* 1 sec default */
  401. data->valid = 0;
  402. data->config = CFG_CR0076;
  403. data->model = 0;
  404. data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
  405. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
  406. data->config);
  407. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
  408. R1FE_MASK | R2FE_MASK);
  409. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  410. data->trutherm);
  411. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  412. data->model);
  413. }
  414. static int lm95241_detach_client(struct i2c_client *client)
  415. {
  416. struct lm95241_data *data = i2c_get_clientdata(client);
  417. int err;
  418. hwmon_device_unregister(data->hwmon_dev);
  419. sysfs_remove_group(&client->dev.kobj, &lm95241_group);
  420. err = i2c_detach_client(client);
  421. if (err)
  422. return err;
  423. kfree(data);
  424. return 0;
  425. }
  426. static struct lm95241_data *lm95241_update_device(struct device *dev)
  427. {
  428. struct i2c_client *client = to_i2c_client(dev);
  429. struct lm95241_data *data = i2c_get_clientdata(client);
  430. mutex_lock(&data->update_lock);
  431. if (time_after(jiffies, data->last_updated + data->rate) ||
  432. !data->valid) {
  433. dev_dbg(&client->dev, "Updating lm95241 data.\n");
  434. data->local_h =
  435. i2c_smbus_read_byte_data(client,
  436. LM95241_REG_R_LOCAL_TEMPH);
  437. data->local_l =
  438. i2c_smbus_read_byte_data(client,
  439. LM95241_REG_R_LOCAL_TEMPL);
  440. data->remote1_h =
  441. i2c_smbus_read_byte_data(client,
  442. LM95241_REG_R_REMOTE1_TEMPH);
  443. data->remote1_l =
  444. i2c_smbus_read_byte_data(client,
  445. LM95241_REG_R_REMOTE1_TEMPL);
  446. data->remote2_h =
  447. i2c_smbus_read_byte_data(client,
  448. LM95241_REG_R_REMOTE2_TEMPH);
  449. data->remote2_l =
  450. i2c_smbus_read_byte_data(client,
  451. LM95241_REG_R_REMOTE2_TEMPL);
  452. data->last_updated = jiffies;
  453. data->valid = 1;
  454. }
  455. mutex_unlock(&data->update_lock);
  456. return data;
  457. }
  458. static int __init sensors_lm95241_init(void)
  459. {
  460. return i2c_add_driver(&lm95241_driver);
  461. }
  462. static void __exit sensors_lm95241_exit(void)
  463. {
  464. i2c_del_driver(&lm95241_driver);
  465. }
  466. MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
  467. MODULE_DESCRIPTION("LM95241 sensor driver");
  468. MODULE_LICENSE("GPL");
  469. module_init(sensors_lm95241_init);
  470. module_exit(sensors_lm95241_exit);