PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/hwmon/lm95241.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 477 lines | 388 code | 51 blank | 38 comment | 20 complexity | 072d7a1b6ef6954d9fcb48790ab73ca6 MD5 | raw file
  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, rate; /* 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_rate(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->rate / HZ);
  113. return strlen(buf);
  114. }
  115. static ssize_t set_rate(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. strict_strtol(buf, 10, &data->rate);
  121. data->rate = data->rate * HZ / 1000;
  122. return count;
  123. }
  124. #define show_type(flag) \
  125. static ssize_t show_type##flag(struct device *dev, \
  126. struct device_attribute *attr, char *buf) \
  127. { \
  128. struct i2c_client *client = to_i2c_client(dev); \
  129. struct lm95241_data *data = i2c_get_clientdata(client); \
  130. \
  131. snprintf(buf, PAGE_SIZE - 1, \
  132. data->model & R##flag##MS_MASK ? "1\n" : "2\n"); \
  133. return strlen(buf); \
  134. }
  135. show_type(1);
  136. show_type(2);
  137. #define show_min(flag) \
  138. static ssize_t show_min##flag(struct device *dev, \
  139. struct device_attribute *attr, char *buf) \
  140. { \
  141. struct i2c_client *client = to_i2c_client(dev); \
  142. struct lm95241_data *data = i2c_get_clientdata(client); \
  143. \
  144. snprintf(buf, PAGE_SIZE - 1, \
  145. data->config & R##flag##DF_MASK ? \
  146. "-127000\n" : "0\n"); \
  147. return strlen(buf); \
  148. }
  149. show_min(1);
  150. show_min(2);
  151. #define show_max(flag) \
  152. static ssize_t show_max##flag(struct device *dev, \
  153. struct device_attribute *attr, char *buf) \
  154. { \
  155. struct i2c_client *client = to_i2c_client(dev); \
  156. struct lm95241_data *data = i2c_get_clientdata(client); \
  157. \
  158. snprintf(buf, PAGE_SIZE - 1, \
  159. data->config & R##flag##DF_MASK ? \
  160. "127000\n" : "255000\n"); \
  161. return strlen(buf); \
  162. }
  163. show_max(1);
  164. show_max(2);
  165. #define set_type(flag) \
  166. static ssize_t set_type##flag(struct device *dev, \
  167. struct device_attribute *attr, \
  168. const char *buf, size_t count) \
  169. { \
  170. struct i2c_client *client = to_i2c_client(dev); \
  171. struct lm95241_data *data = i2c_get_clientdata(client); \
  172. \
  173. long val; \
  174. strict_strtol(buf, 10, &val); \
  175. \
  176. if ((val == 1) || (val == 2)) { \
  177. \
  178. mutex_lock(&data->update_lock); \
  179. \
  180. data->trutherm &= ~(TT_MASK << TT##flag##_SHIFT); \
  181. if (val == 1) { \
  182. data->model |= R##flag##MS_MASK; \
  183. data->trutherm |= (TT_ON << TT##flag##_SHIFT); \
  184. } \
  185. else { \
  186. data->model &= ~R##flag##MS_MASK; \
  187. data->trutherm |= (TT_OFF << TT##flag##_SHIFT); \
  188. } \
  189. \
  190. data->valid = 0; \
  191. \
  192. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL, \
  193. data->model); \
  194. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM, \
  195. data->trutherm); \
  196. \
  197. mutex_unlock(&data->update_lock); \
  198. \
  199. } \
  200. return count; \
  201. }
  202. set_type(1);
  203. set_type(2);
  204. #define set_min(flag) \
  205. static ssize_t set_min##flag(struct device *dev, \
  206. struct device_attribute *devattr, const char *buf, size_t count) \
  207. { \
  208. struct i2c_client *client = to_i2c_client(dev); \
  209. struct lm95241_data *data = i2c_get_clientdata(client); \
  210. \
  211. long val; \
  212. strict_strtol(buf, 10, &val); \
  213. \
  214. mutex_lock(&data->update_lock); \
  215. \
  216. if (val < 0) \
  217. data->config |= R##flag##DF_MASK; \
  218. else \
  219. data->config &= ~R##flag##DF_MASK; \
  220. \
  221. data->valid = 0; \
  222. \
  223. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  224. data->config); \
  225. \
  226. mutex_unlock(&data->update_lock); \
  227. \
  228. return count; \
  229. }
  230. set_min(1);
  231. set_min(2);
  232. #define set_max(flag) \
  233. static ssize_t set_max##flag(struct device *dev, \
  234. struct device_attribute *devattr, const char *buf, size_t count) \
  235. { \
  236. struct i2c_client *client = to_i2c_client(dev); \
  237. struct lm95241_data *data = i2c_get_clientdata(client); \
  238. \
  239. long val; \
  240. strict_strtol(buf, 10, &val); \
  241. \
  242. mutex_lock(&data->update_lock); \
  243. \
  244. if (val <= 127000) \
  245. data->config |= R##flag##DF_MASK; \
  246. else \
  247. data->config &= ~R##flag##DF_MASK; \
  248. \
  249. data->valid = 0; \
  250. \
  251. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, \
  252. data->config); \
  253. \
  254. mutex_unlock(&data->update_lock); \
  255. \
  256. return count; \
  257. }
  258. set_max(1);
  259. set_max(2);
  260. static DEVICE_ATTR(temp1_input, S_IRUGO, show_local, NULL);
  261. static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote1, NULL);
  262. static DEVICE_ATTR(temp3_input, S_IRUGO, show_remote2, NULL);
  263. static DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type1, set_type1);
  264. static DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type2, set_type2);
  265. static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min1, set_min1);
  266. static DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min2, set_min2);
  267. static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max1, set_max1);
  268. static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max2, set_max2);
  269. static DEVICE_ATTR(rate, S_IWUSR | S_IRUGO, show_rate, set_rate);
  270. static struct attribute *lm95241_attributes[] = {
  271. &dev_attr_temp1_input.attr,
  272. &dev_attr_temp2_input.attr,
  273. &dev_attr_temp3_input.attr,
  274. &dev_attr_temp2_type.attr,
  275. &dev_attr_temp3_type.attr,
  276. &dev_attr_temp2_min.attr,
  277. &dev_attr_temp3_min.attr,
  278. &dev_attr_temp2_max.attr,
  279. &dev_attr_temp3_max.attr,
  280. &dev_attr_rate.attr,
  281. NULL
  282. };
  283. static const struct attribute_group lm95241_group = {
  284. .attrs = lm95241_attributes,
  285. };
  286. /* Return 0 if detection is successful, -ENODEV otherwise */
  287. static int lm95241_detect(struct i2c_client *new_client,
  288. struct i2c_board_info *info)
  289. {
  290. struct i2c_adapter *adapter = new_client->adapter;
  291. int address = new_client->addr;
  292. const char *name;
  293. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  294. return -ENODEV;
  295. if ((i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID)
  296. == MANUFACTURER_ID)
  297. && (i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID)
  298. >= DEFAULT_REVISION)) {
  299. name = "lm95241";
  300. } else {
  301. dev_dbg(&adapter->dev, "LM95241 detection failed at 0x%02x\n",
  302. address);
  303. return -ENODEV;
  304. }
  305. /* Fill the i2c board info */
  306. strlcpy(info->type, name, I2C_NAME_SIZE);
  307. return 0;
  308. }
  309. static int lm95241_probe(struct i2c_client *new_client,
  310. const struct i2c_device_id *id)
  311. {
  312. struct lm95241_data *data;
  313. int err;
  314. data = kzalloc(sizeof(struct lm95241_data), GFP_KERNEL);
  315. if (!data) {
  316. err = -ENOMEM;
  317. goto exit;
  318. }
  319. i2c_set_clientdata(new_client, data);
  320. mutex_init(&data->update_lock);
  321. /* Initialize the LM95241 chip */
  322. lm95241_init_client(new_client);
  323. /* Register sysfs hooks */
  324. err = sysfs_create_group(&new_client->dev.kobj, &lm95241_group);
  325. if (err)
  326. goto exit_free;
  327. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  328. if (IS_ERR(data->hwmon_dev)) {
  329. err = PTR_ERR(data->hwmon_dev);
  330. goto exit_remove_files;
  331. }
  332. return 0;
  333. exit_remove_files:
  334. sysfs_remove_group(&new_client->dev.kobj, &lm95241_group);
  335. exit_free:
  336. kfree(data);
  337. exit:
  338. return err;
  339. }
  340. static void lm95241_init_client(struct i2c_client *client)
  341. {
  342. struct lm95241_data *data = i2c_get_clientdata(client);
  343. data->rate = HZ; /* 1 sec default */
  344. data->valid = 0;
  345. data->config = CFG_CR0076;
  346. data->model = 0;
  347. data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
  348. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG,
  349. data->config);
  350. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
  351. R1FE_MASK | R2FE_MASK);
  352. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  353. data->trutherm);
  354. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  355. data->model);
  356. }
  357. static int lm95241_remove(struct i2c_client *client)
  358. {
  359. struct lm95241_data *data = i2c_get_clientdata(client);
  360. hwmon_device_unregister(data->hwmon_dev);
  361. sysfs_remove_group(&client->dev.kobj, &lm95241_group);
  362. kfree(data);
  363. return 0;
  364. }
  365. static struct lm95241_data *lm95241_update_device(struct device *dev)
  366. {
  367. struct i2c_client *client = to_i2c_client(dev);
  368. struct lm95241_data *data = i2c_get_clientdata(client);
  369. mutex_lock(&data->update_lock);
  370. if (time_after(jiffies, data->last_updated + data->rate) ||
  371. !data->valid) {
  372. dev_dbg(&client->dev, "Updating lm95241 data.\n");
  373. data->local_h =
  374. i2c_smbus_read_byte_data(client,
  375. LM95241_REG_R_LOCAL_TEMPH);
  376. data->local_l =
  377. i2c_smbus_read_byte_data(client,
  378. LM95241_REG_R_LOCAL_TEMPL);
  379. data->remote1_h =
  380. i2c_smbus_read_byte_data(client,
  381. LM95241_REG_R_REMOTE1_TEMPH);
  382. data->remote1_l =
  383. i2c_smbus_read_byte_data(client,
  384. LM95241_REG_R_REMOTE1_TEMPL);
  385. data->remote2_h =
  386. i2c_smbus_read_byte_data(client,
  387. LM95241_REG_R_REMOTE2_TEMPH);
  388. data->remote2_l =
  389. i2c_smbus_read_byte_data(client,
  390. LM95241_REG_R_REMOTE2_TEMPL);
  391. data->last_updated = jiffies;
  392. data->valid = 1;
  393. }
  394. mutex_unlock(&data->update_lock);
  395. return data;
  396. }
  397. /* Driver data (common to all clients) */
  398. static const struct i2c_device_id lm95241_id[] = {
  399. { "lm95241", 0 },
  400. { }
  401. };
  402. MODULE_DEVICE_TABLE(i2c, lm95241_id);
  403. static struct i2c_driver lm95241_driver = {
  404. .class = I2C_CLASS_HWMON,
  405. .driver = {
  406. .name = "lm95241",
  407. },
  408. .probe = lm95241_probe,
  409. .remove = lm95241_remove,
  410. .id_table = lm95241_id,
  411. .detect = lm95241_detect,
  412. .address_list = normal_i2c,
  413. };
  414. static int __init sensors_lm95241_init(void)
  415. {
  416. return i2c_add_driver(&lm95241_driver);
  417. }
  418. static void __exit sensors_lm95241_exit(void)
  419. {
  420. i2c_del_driver(&lm95241_driver);
  421. }
  422. MODULE_AUTHOR("Davide Rizzo <elpa-rizzo@gmail.com>");
  423. MODULE_DESCRIPTION("LM95241 sensor driver");
  424. MODULE_LICENSE("GPL");
  425. module_init(sensors_lm95241_init);
  426. module_exit(sensors_lm95241_exit);