lm95245.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (C) 2011 Alexander Stein <alexander.stein@systec-electronic.com>
  3. *
  4. * The LM95245 is a sensor chip made by National Semiconductors.
  5. * It reports up to two temperatures (its own plus an external one).
  6. * Complete datasheet can be obtained from National's website at:
  7. * http://www.national.com/ds.cgi/LM/LM95245.pdf
  8. *
  9. * This driver is based on lm95241.c
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/i2c.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/hwmon-sysfs.h>
  32. #include <linux/err.h>
  33. #include <linux/mutex.h>
  34. #include <linux/sysfs.h>
  35. #define DEVNAME "lm95245"
  36. static const unsigned short normal_i2c[] = {
  37. 0x18, 0x19, 0x29, 0x4c, 0x4d, I2C_CLIENT_END };
  38. /* LM95245 registers */
  39. /* general registers */
  40. #define LM95245_REG_RW_CONFIG1 0x03
  41. #define LM95245_REG_RW_CONVERS_RATE 0x04
  42. #define LM95245_REG_W_ONE_SHOT 0x0F
  43. /* diode configuration */
  44. #define LM95245_REG_RW_CONFIG2 0xBF
  45. #define LM95245_REG_RW_REMOTE_OFFH 0x11
  46. #define LM95245_REG_RW_REMOTE_OFFL 0x12
  47. /* status registers */
  48. #define LM95245_REG_R_STATUS1 0x02
  49. #define LM95245_REG_R_STATUS2 0x33
  50. /* limit registers */
  51. #define LM95245_REG_RW_REMOTE_OS_LIMIT 0x07
  52. #define LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT 0x20
  53. #define LM95245_REG_RW_REMOTE_TCRIT_LIMIT 0x19
  54. #define LM95245_REG_RW_COMMON_HYSTERESIS 0x21
  55. /* temperature signed */
  56. #define LM95245_REG_R_LOCAL_TEMPH_S 0x00
  57. #define LM95245_REG_R_LOCAL_TEMPL_S 0x30
  58. #define LM95245_REG_R_REMOTE_TEMPH_S 0x01
  59. #define LM95245_REG_R_REMOTE_TEMPL_S 0x10
  60. /* temperature unsigned */
  61. #define LM95245_REG_R_REMOTE_TEMPH_U 0x31
  62. #define LM95245_REG_R_REMOTE_TEMPL_U 0x32
  63. /* id registers */
  64. #define LM95245_REG_R_MAN_ID 0xFE
  65. #define LM95245_REG_R_CHIP_ID 0xFF
  66. /* LM95245 specific bitfields */
  67. #define CFG_STOP 0x40
  68. #define CFG_REMOTE_TCRIT_MASK 0x10
  69. #define CFG_REMOTE_OS_MASK 0x08
  70. #define CFG_LOCAL_TCRIT_MASK 0x04
  71. #define CFG_LOCAL_OS_MASK 0x02
  72. #define CFG2_OS_A0 0x40
  73. #define CFG2_DIODE_FAULT_OS 0x20
  74. #define CFG2_DIODE_FAULT_TCRIT 0x10
  75. #define CFG2_REMOTE_TT 0x08
  76. #define CFG2_REMOTE_FILTER_DIS 0x00
  77. #define CFG2_REMOTE_FILTER_EN 0x06
  78. /* conversation rate in ms */
  79. #define RATE_CR0063 0x00
  80. #define RATE_CR0364 0x01
  81. #define RATE_CR1000 0x02
  82. #define RATE_CR2500 0x03
  83. #define STATUS1_DIODE_FAULT 0x04
  84. #define STATUS1_RTCRIT 0x02
  85. #define STATUS1_LOC 0x01
  86. #define MANUFACTURER_ID 0x01
  87. #define DEFAULT_REVISION 0xB3
  88. static const u8 lm95245_reg_address[] = {
  89. LM95245_REG_R_LOCAL_TEMPH_S,
  90. LM95245_REG_R_LOCAL_TEMPL_S,
  91. LM95245_REG_R_REMOTE_TEMPH_S,
  92. LM95245_REG_R_REMOTE_TEMPL_S,
  93. LM95245_REG_R_REMOTE_TEMPH_U,
  94. LM95245_REG_R_REMOTE_TEMPL_U,
  95. LM95245_REG_RW_LOCAL_OS_TCRIT_LIMIT,
  96. LM95245_REG_RW_REMOTE_TCRIT_LIMIT,
  97. LM95245_REG_RW_COMMON_HYSTERESIS,
  98. LM95245_REG_R_STATUS1,
  99. };
  100. /* Client data (each client gets its own) */
  101. struct lm95245_data {
  102. struct device *hwmon_dev;
  103. struct mutex update_lock;
  104. unsigned long last_updated; /* in jiffies */
  105. unsigned long interval; /* in msecs */
  106. bool valid; /* zero until following fields are valid */
  107. /* registers values */
  108. u8 regs[ARRAY_SIZE(lm95245_reg_address)];
  109. u8 config1, config2;
  110. };
  111. /* Conversions */
  112. static int temp_from_reg_unsigned(u8 val_h, u8 val_l)
  113. {
  114. return val_h * 1000 + val_l * 1000 / 256;
  115. }
  116. static int temp_from_reg_signed(u8 val_h, u8 val_l)
  117. {
  118. if (val_h & 0x80)
  119. return (val_h - 0x100) * 1000;
  120. return temp_from_reg_unsigned(val_h, val_l);
  121. }
  122. static struct lm95245_data *lm95245_update_device(struct device *dev)
  123. {
  124. struct i2c_client *client = to_i2c_client(dev);
  125. struct lm95245_data *data = i2c_get_clientdata(client);
  126. mutex_lock(&data->update_lock);
  127. if (time_after(jiffies, data->last_updated
  128. + msecs_to_jiffies(data->interval)) || !data->valid) {
  129. int i;
  130. dev_dbg(&client->dev, "Updating lm95245 data.\n");
  131. for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++)
  132. data->regs[i]
  133. = i2c_smbus_read_byte_data(client,
  134. lm95245_reg_address[i]);
  135. data->last_updated = jiffies;
  136. data->valid = 1;
  137. }
  138. mutex_unlock(&data->update_lock);
  139. return data;
  140. }
  141. static unsigned long lm95245_read_conversion_rate(struct i2c_client *client)
  142. {
  143. int rate;
  144. unsigned long interval;
  145. rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE);
  146. switch (rate) {
  147. case RATE_CR0063:
  148. interval = 63;
  149. break;
  150. case RATE_CR0364:
  151. interval = 364;
  152. break;
  153. case RATE_CR1000:
  154. interval = 1000;
  155. break;
  156. case RATE_CR2500:
  157. default:
  158. interval = 2500;
  159. break;
  160. }
  161. return interval;
  162. }
  163. static unsigned long lm95245_set_conversion_rate(struct i2c_client *client,
  164. unsigned long interval)
  165. {
  166. int rate;
  167. if (interval <= 63) {
  168. interval = 63;
  169. rate = RATE_CR0063;
  170. } else if (interval <= 364) {
  171. interval = 364;
  172. rate = RATE_CR0364;
  173. } else if (interval <= 1000) {
  174. interval = 1000;
  175. rate = RATE_CR1000;
  176. } else {
  177. interval = 2500;
  178. rate = RATE_CR2500;
  179. }
  180. i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate);
  181. return interval;
  182. }
  183. /* Sysfs stuff */
  184. static ssize_t show_input(struct device *dev, struct device_attribute *attr,
  185. char *buf)
  186. {
  187. struct lm95245_data *data = lm95245_update_device(dev);
  188. int temp;
  189. int index = to_sensor_dev_attr(attr)->index;
  190. /*
  191. * Index 0 (Local temp) is always signed
  192. * Index 2 (Remote temp) has both signed and unsigned data
  193. * use signed calculation for remote if signed bit is set
  194. */
  195. if (index == 0 || data->regs[index] & 0x80)
  196. temp = temp_from_reg_signed(data->regs[index],
  197. data->regs[index + 1]);
  198. else
  199. temp = temp_from_reg_unsigned(data->regs[index + 2],
  200. data->regs[index + 3]);
  201. return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp);
  202. }
  203. static ssize_t show_limit(struct device *dev, struct device_attribute *attr,
  204. char *buf)
  205. {
  206. struct lm95245_data *data = lm95245_update_device(dev);
  207. int index = to_sensor_dev_attr(attr)->index;
  208. return snprintf(buf, PAGE_SIZE - 1, "%d\n",
  209. data->regs[index] * 1000);
  210. }
  211. static ssize_t set_limit(struct device *dev, struct device_attribute *attr,
  212. const char *buf, size_t count)
  213. {
  214. struct i2c_client *client = to_i2c_client(dev);
  215. struct lm95245_data *data = i2c_get_clientdata(client);
  216. int index = to_sensor_dev_attr(attr)->index;
  217. unsigned long val;
  218. if (kstrtoul(buf, 10, &val) < 0)
  219. return -EINVAL;
  220. val /= 1000;
  221. val = SENSORS_LIMIT(val, 0, (index == 6 ? 127 : 255));
  222. mutex_lock(&data->update_lock);
  223. data->valid = 0;
  224. i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val);
  225. mutex_unlock(&data->update_lock);
  226. return count;
  227. }
  228. static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr,
  229. const char *buf, size_t count)
  230. {
  231. struct i2c_client *client = to_i2c_client(dev);
  232. struct lm95245_data *data = i2c_get_clientdata(client);
  233. unsigned long val;
  234. if (kstrtoul(buf, 10, &val) < 0)
  235. return -EINVAL;
  236. val /= 1000;
  237. val = SENSORS_LIMIT(val, 0, 31);
  238. mutex_lock(&data->update_lock);
  239. data->valid = 0;
  240. /* shared crit hysteresis */
  241. i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS,
  242. val);
  243. mutex_unlock(&data->update_lock);
  244. return count;
  245. }
  246. static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  247. char *buf)
  248. {
  249. struct i2c_client *client = to_i2c_client(dev);
  250. struct lm95245_data *data = i2c_get_clientdata(client);
  251. return snprintf(buf, PAGE_SIZE - 1,
  252. data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n");
  253. }
  254. static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  255. const char *buf, size_t count)
  256. {
  257. struct i2c_client *client = to_i2c_client(dev);
  258. struct lm95245_data *data = i2c_get_clientdata(client);
  259. unsigned long val;
  260. if (kstrtoul(buf, 10, &val) < 0)
  261. return -EINVAL;
  262. if (val != 1 && val != 2)
  263. return -EINVAL;
  264. mutex_lock(&data->update_lock);
  265. if (val == 1)
  266. data->config2 |= CFG2_REMOTE_TT;
  267. else
  268. data->config2 &= ~CFG2_REMOTE_TT;
  269. data->valid = 0;
  270. i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2,
  271. data->config2);
  272. mutex_unlock(&data->update_lock);
  273. return count;
  274. }
  275. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  276. char *buf)
  277. {
  278. struct lm95245_data *data = lm95245_update_device(dev);
  279. int index = to_sensor_dev_attr(attr)->index;
  280. return snprintf(buf, PAGE_SIZE - 1, "%d\n",
  281. !!(data->regs[9] & index));
  282. }
  283. static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  284. char *buf)
  285. {
  286. struct lm95245_data *data = lm95245_update_device(dev);
  287. return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
  288. }
  289. static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  290. const char *buf, size_t count)
  291. {
  292. struct i2c_client *client = to_i2c_client(dev);
  293. struct lm95245_data *data = i2c_get_clientdata(client);
  294. unsigned long val;
  295. if (kstrtoul(buf, 10, &val) < 0)
  296. return -EINVAL;
  297. mutex_lock(&data->update_lock);
  298. data->interval = lm95245_set_conversion_rate(client, val);
  299. mutex_unlock(&data->update_lock);
  300. return count;
  301. }
  302. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
  303. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit,
  304. set_limit, 6);
  305. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_limit,
  306. set_crit_hyst, 8);
  307. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
  308. STATUS1_LOC);
  309. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
  310. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit,
  311. set_limit, 7);
  312. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IWUSR | S_IRUGO, show_limit,
  313. set_crit_hyst, 8);
  314. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
  315. STATUS1_RTCRIT);
  316. static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type,
  317. set_type, 0);
  318. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
  319. STATUS1_DIODE_FAULT);
  320. static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
  321. set_interval);
  322. static struct attribute *lm95245_attributes[] = {
  323. &sensor_dev_attr_temp1_input.dev_attr.attr,
  324. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  325. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  326. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  327. &sensor_dev_attr_temp2_input.dev_attr.attr,
  328. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  329. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  330. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  331. &sensor_dev_attr_temp2_type.dev_attr.attr,
  332. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  333. &dev_attr_update_interval.attr,
  334. NULL
  335. };
  336. static const struct attribute_group lm95245_group = {
  337. .attrs = lm95245_attributes,
  338. };
  339. /* Return 0 if detection is successful, -ENODEV otherwise */
  340. static int lm95245_detect(struct i2c_client *new_client,
  341. struct i2c_board_info *info)
  342. {
  343. struct i2c_adapter *adapter = new_client->adapter;
  344. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  345. return -ENODEV;
  346. if (i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID)
  347. != MANUFACTURER_ID
  348. || i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID)
  349. != DEFAULT_REVISION)
  350. return -ENODEV;
  351. strlcpy(info->type, DEVNAME, I2C_NAME_SIZE);
  352. return 0;
  353. }
  354. static void lm95245_init_client(struct i2c_client *client)
  355. {
  356. struct lm95245_data *data = i2c_get_clientdata(client);
  357. data->valid = 0;
  358. data->interval = lm95245_read_conversion_rate(client);
  359. data->config1 = i2c_smbus_read_byte_data(client,
  360. LM95245_REG_RW_CONFIG1);
  361. data->config2 = i2c_smbus_read_byte_data(client,
  362. LM95245_REG_RW_CONFIG2);
  363. if (data->config1 & CFG_STOP) {
  364. /* Clear the standby bit */
  365. data->config1 &= ~CFG_STOP;
  366. i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1,
  367. data->config1);
  368. }
  369. }
  370. static int lm95245_probe(struct i2c_client *new_client,
  371. const struct i2c_device_id *id)
  372. {
  373. struct lm95245_data *data;
  374. int err;
  375. data = kzalloc(sizeof(struct lm95245_data), GFP_KERNEL);
  376. if (!data) {
  377. err = -ENOMEM;
  378. goto exit;
  379. }
  380. i2c_set_clientdata(new_client, data);
  381. mutex_init(&data->update_lock);
  382. /* Initialize the LM95245 chip */
  383. lm95245_init_client(new_client);
  384. /* Register sysfs hooks */
  385. err = sysfs_create_group(&new_client->dev.kobj, &lm95245_group);
  386. if (err)
  387. goto exit_free;
  388. data->hwmon_dev = hwmon_device_register(&new_client->dev);
  389. if (IS_ERR(data->hwmon_dev)) {
  390. err = PTR_ERR(data->hwmon_dev);
  391. goto exit_remove_files;
  392. }
  393. return 0;
  394. exit_remove_files:
  395. sysfs_remove_group(&new_client->dev.kobj, &lm95245_group);
  396. exit_free:
  397. kfree(data);
  398. exit:
  399. return err;
  400. }
  401. static int lm95245_remove(struct i2c_client *client)
  402. {
  403. struct lm95245_data *data = i2c_get_clientdata(client);
  404. hwmon_device_unregister(data->hwmon_dev);
  405. sysfs_remove_group(&client->dev.kobj, &lm95245_group);
  406. kfree(data);
  407. return 0;
  408. }
  409. /* Driver data (common to all clients) */
  410. static const struct i2c_device_id lm95245_id[] = {
  411. { DEVNAME, 0 },
  412. { }
  413. };
  414. MODULE_DEVICE_TABLE(i2c, lm95245_id);
  415. static struct i2c_driver lm95245_driver = {
  416. .class = I2C_CLASS_HWMON,
  417. .driver = {
  418. .name = DEVNAME,
  419. },
  420. .probe = lm95245_probe,
  421. .remove = lm95245_remove,
  422. .id_table = lm95245_id,
  423. .detect = lm95245_detect,
  424. .address_list = normal_i2c,
  425. };
  426. module_i2c_driver(lm95245_driver);
  427. MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
  428. MODULE_DESCRIPTION("LM95245 sensor driver");
  429. MODULE_LICENSE("GPL");