lm95241.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 NATSEMI_MAN_ID 0x01
  73. #define LM95231_CHIP_ID 0xA1
  74. #define LM95241_CHIP_ID 0xA4
  75. static const u8 lm95241_reg_address[] = {
  76. LM95241_REG_R_LOCAL_TEMPH,
  77. LM95241_REG_R_LOCAL_TEMPL,
  78. LM95241_REG_R_REMOTE1_TEMPH,
  79. LM95241_REG_R_REMOTE1_TEMPL,
  80. LM95241_REG_R_REMOTE2_TEMPH,
  81. LM95241_REG_R_REMOTE2_TEMPL
  82. };
  83. /* Client data (each client gets its own) */
  84. struct lm95241_data {
  85. struct i2c_client *client;
  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 temp[ARRAY_SIZE(lm95241_reg_address)];
  91. u8 config, model, trutherm;
  92. };
  93. /* Conversions */
  94. static int temp_from_reg_signed(u8 val_h, u8 val_l)
  95. {
  96. s16 val_hl = (val_h << 8) | val_l;
  97. return val_hl * 1000 / 256;
  98. }
  99. static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
  100. {
  101. u16 val_hl = (val_h << 8) | val_l;
  102. return val_hl * 1000 / 256;
  103. }
  104. static struct lm95241_data *lm95241_update_device(struct device *dev)
  105. {
  106. struct lm95241_data *data = dev_get_drvdata(dev);
  107. struct i2c_client *client = data->client;
  108. mutex_lock(&data->update_lock);
  109. if (time_after(jiffies, data->last_updated + data->interval) ||
  110. !data->valid) {
  111. int i;
  112. dev_dbg(dev, "Updating lm95241 data.\n");
  113. for (i = 0; i < ARRAY_SIZE(lm95241_reg_address); i++)
  114. data->temp[i]
  115. = i2c_smbus_read_byte_data(client,
  116. lm95241_reg_address[i]);
  117. data->last_updated = jiffies;
  118. data->valid = 1;
  119. }
  120. mutex_unlock(&data->update_lock);
  121. return data;
  122. }
  123. /* Sysfs stuff */
  124. static ssize_t show_input(struct device *dev, struct device_attribute *attr,
  125. char *buf)
  126. {
  127. struct lm95241_data *data = lm95241_update_device(dev);
  128. int index = to_sensor_dev_attr(attr)->index;
  129. return snprintf(buf, PAGE_SIZE - 1, "%d\n",
  130. index == 0 || (data->config & (1 << (index / 2))) ?
  131. temp_from_reg_signed(data->temp[index], data->temp[index + 1]) :
  132. temp_from_reg_unsigned(data->temp[index],
  133. data->temp[index + 1]));
  134. }
  135. static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  136. char *buf)
  137. {
  138. struct lm95241_data *data = dev_get_drvdata(dev);
  139. return snprintf(buf, PAGE_SIZE - 1,
  140. data->model & to_sensor_dev_attr(attr)->index ? "1\n" : "2\n");
  141. }
  142. static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  143. const char *buf, size_t count)
  144. {
  145. struct lm95241_data *data = dev_get_drvdata(dev);
  146. struct i2c_client *client = data->client;
  147. unsigned long val;
  148. int shift;
  149. u8 mask = to_sensor_dev_attr(attr)->index;
  150. if (kstrtoul(buf, 10, &val) < 0)
  151. return -EINVAL;
  152. if (val != 1 && val != 2)
  153. return -EINVAL;
  154. shift = mask == R1MS_MASK ? TT1_SHIFT : TT2_SHIFT;
  155. mutex_lock(&data->update_lock);
  156. data->trutherm &= ~(TT_MASK << shift);
  157. if (val == 1) {
  158. data->model |= mask;
  159. data->trutherm |= (TT_ON << shift);
  160. } else {
  161. data->model &= ~mask;
  162. data->trutherm |= (TT_OFF << shift);
  163. }
  164. data->valid = 0;
  165. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  166. data->model);
  167. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  168. data->trutherm);
  169. mutex_unlock(&data->update_lock);
  170. return count;
  171. }
  172. static ssize_t show_min(struct device *dev, struct device_attribute *attr,
  173. char *buf)
  174. {
  175. struct lm95241_data *data = dev_get_drvdata(dev);
  176. return snprintf(buf, PAGE_SIZE - 1,
  177. data->config & to_sensor_dev_attr(attr)->index ?
  178. "-127000\n" : "0\n");
  179. }
  180. static ssize_t set_min(struct device *dev, struct device_attribute *attr,
  181. const char *buf, size_t count)
  182. {
  183. struct lm95241_data *data = dev_get_drvdata(dev);
  184. long val;
  185. if (kstrtol(buf, 10, &val) < 0)
  186. return -EINVAL;
  187. if (val < -128000)
  188. return -EINVAL;
  189. mutex_lock(&data->update_lock);
  190. if (val < 0)
  191. data->config |= to_sensor_dev_attr(attr)->index;
  192. else
  193. data->config &= ~to_sensor_dev_attr(attr)->index;
  194. data->valid = 0;
  195. i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
  196. data->config);
  197. mutex_unlock(&data->update_lock);
  198. return count;
  199. }
  200. static ssize_t show_max(struct device *dev, struct device_attribute *attr,
  201. char *buf)
  202. {
  203. struct lm95241_data *data = dev_get_drvdata(dev);
  204. return snprintf(buf, PAGE_SIZE - 1,
  205. data->config & to_sensor_dev_attr(attr)->index ?
  206. "127000\n" : "255000\n");
  207. }
  208. static ssize_t set_max(struct device *dev, struct device_attribute *attr,
  209. const char *buf, size_t count)
  210. {
  211. struct lm95241_data *data = dev_get_drvdata(dev);
  212. long val;
  213. if (kstrtol(buf, 10, &val) < 0)
  214. return -EINVAL;
  215. if (val >= 256000)
  216. return -EINVAL;
  217. mutex_lock(&data->update_lock);
  218. if (val <= 127000)
  219. data->config |= to_sensor_dev_attr(attr)->index;
  220. else
  221. data->config &= ~to_sensor_dev_attr(attr)->index;
  222. data->valid = 0;
  223. i2c_smbus_write_byte_data(data->client, LM95241_REG_RW_CONFIG,
  224. data->config);
  225. mutex_unlock(&data->update_lock);
  226. return count;
  227. }
  228. static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  229. char *buf)
  230. {
  231. struct lm95241_data *data = lm95241_update_device(dev);
  232. return snprintf(buf, PAGE_SIZE - 1, "%lu\n", 1000 * data->interval
  233. / HZ);
  234. }
  235. static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  236. const char *buf, size_t count)
  237. {
  238. struct lm95241_data *data = dev_get_drvdata(dev);
  239. unsigned long val;
  240. if (kstrtoul(buf, 10, &val) < 0)
  241. return -EINVAL;
  242. data->interval = val * HZ / 1000;
  243. return count;
  244. }
  245. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
  246. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
  247. static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_input, NULL, 4);
  248. static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type, set_type,
  249. R1MS_MASK);
  250. static SENSOR_DEVICE_ATTR(temp3_type, S_IWUSR | S_IRUGO, show_type, set_type,
  251. R2MS_MASK);
  252. static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_min, set_min,
  253. R1DF_MASK);
  254. static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_min, set_min,
  255. R2DF_MASK);
  256. static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_max, set_max,
  257. R1DF_MASK);
  258. static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_max, set_max,
  259. R2DF_MASK);
  260. static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
  261. set_interval);
  262. static struct attribute *lm95241_attrs[] = {
  263. &sensor_dev_attr_temp1_input.dev_attr.attr,
  264. &sensor_dev_attr_temp2_input.dev_attr.attr,
  265. &sensor_dev_attr_temp3_input.dev_attr.attr,
  266. &sensor_dev_attr_temp2_type.dev_attr.attr,
  267. &sensor_dev_attr_temp3_type.dev_attr.attr,
  268. &sensor_dev_attr_temp2_min.dev_attr.attr,
  269. &sensor_dev_attr_temp3_min.dev_attr.attr,
  270. &sensor_dev_attr_temp2_max.dev_attr.attr,
  271. &sensor_dev_attr_temp3_max.dev_attr.attr,
  272. &dev_attr_update_interval.attr,
  273. NULL
  274. };
  275. ATTRIBUTE_GROUPS(lm95241);
  276. /* Return 0 if detection is successful, -ENODEV otherwise */
  277. static int lm95241_detect(struct i2c_client *new_client,
  278. struct i2c_board_info *info)
  279. {
  280. struct i2c_adapter *adapter = new_client->adapter;
  281. const char *name;
  282. int mfg_id, chip_id;
  283. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  284. return -ENODEV;
  285. mfg_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_MAN_ID);
  286. if (mfg_id != NATSEMI_MAN_ID)
  287. return -ENODEV;
  288. chip_id = i2c_smbus_read_byte_data(new_client, LM95241_REG_R_CHIP_ID);
  289. switch (chip_id) {
  290. case LM95231_CHIP_ID:
  291. name = "lm95231";
  292. break;
  293. case LM95241_CHIP_ID:
  294. name = "lm95241";
  295. break;
  296. default:
  297. return -ENODEV;
  298. }
  299. /* Fill the i2c board info */
  300. strlcpy(info->type, name, I2C_NAME_SIZE);
  301. return 0;
  302. }
  303. static void lm95241_init_client(struct i2c_client *client,
  304. struct lm95241_data *data)
  305. {
  306. data->interval = HZ; /* 1 sec default */
  307. data->config = CFG_CR0076;
  308. data->trutherm = (TT_OFF << TT1_SHIFT) | (TT_OFF << TT2_SHIFT);
  309. i2c_smbus_write_byte_data(client, LM95241_REG_RW_CONFIG, data->config);
  310. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REM_FILTER,
  311. R1FE_MASK | R2FE_MASK);
  312. i2c_smbus_write_byte_data(client, LM95241_REG_RW_TRUTHERM,
  313. data->trutherm);
  314. i2c_smbus_write_byte_data(client, LM95241_REG_RW_REMOTE_MODEL,
  315. data->model);
  316. }
  317. static int lm95241_probe(struct i2c_client *client,
  318. const struct i2c_device_id *id)
  319. {
  320. struct device *dev = &client->dev;
  321. struct lm95241_data *data;
  322. struct device *hwmon_dev;
  323. data = devm_kzalloc(dev, sizeof(struct lm95241_data), GFP_KERNEL);
  324. if (!data)
  325. return -ENOMEM;
  326. data->client = client;
  327. mutex_init(&data->update_lock);
  328. /* Initialize the LM95241 chip */
  329. lm95241_init_client(client, data);
  330. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  331. data,
  332. lm95241_groups);
  333. return PTR_ERR_OR_ZERO(hwmon_dev);
  334. }
  335. /* Driver data (common to all clients) */
  336. static const struct i2c_device_id lm95241_id[] = {
  337. { "lm95231", 0 },
  338. { "lm95241", 0 },
  339. { }
  340. };
  341. MODULE_DEVICE_TABLE(i2c, lm95241_id);
  342. static struct i2c_driver lm95241_driver = {
  343. .class = I2C_CLASS_HWMON,
  344. .driver = {
  345. .name = DEVNAME,
  346. },
  347. .probe = lm95241_probe,
  348. .id_table = lm95241_id,
  349. .detect = lm95241_detect,
  350. .address_list = normal_i2c,
  351. };
  352. module_i2c_driver(lm95241_driver);
  353. MODULE_AUTHOR("Davide Rizzo <elpa.rizzo@gmail.com>");
  354. MODULE_DESCRIPTION("LM95241 sensor driver");
  355. MODULE_LICENSE("GPL");