lm95241.c 12 KB

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