lm95245.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 i2c_client *client;
  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 lm95245_data *data = dev_get_drvdata(dev);
  125. struct i2c_client *client = data->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. for (i = 0; i < ARRAY_SIZE(lm95245_reg_address); i++)
  131. data->regs[i]
  132. = i2c_smbus_read_byte_data(client,
  133. lm95245_reg_address[i]);
  134. data->last_updated = jiffies;
  135. data->valid = 1;
  136. }
  137. mutex_unlock(&data->update_lock);
  138. return data;
  139. }
  140. static unsigned long lm95245_read_conversion_rate(struct i2c_client *client)
  141. {
  142. int rate;
  143. unsigned long interval;
  144. rate = i2c_smbus_read_byte_data(client, LM95245_REG_RW_CONVERS_RATE);
  145. switch (rate) {
  146. case RATE_CR0063:
  147. interval = 63;
  148. break;
  149. case RATE_CR0364:
  150. interval = 364;
  151. break;
  152. case RATE_CR1000:
  153. interval = 1000;
  154. break;
  155. case RATE_CR2500:
  156. default:
  157. interval = 2500;
  158. break;
  159. }
  160. return interval;
  161. }
  162. static unsigned long lm95245_set_conversion_rate(struct i2c_client *client,
  163. unsigned long interval)
  164. {
  165. int rate;
  166. if (interval <= 63) {
  167. interval = 63;
  168. rate = RATE_CR0063;
  169. } else if (interval <= 364) {
  170. interval = 364;
  171. rate = RATE_CR0364;
  172. } else if (interval <= 1000) {
  173. interval = 1000;
  174. rate = RATE_CR1000;
  175. } else {
  176. interval = 2500;
  177. rate = RATE_CR2500;
  178. }
  179. i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONVERS_RATE, rate);
  180. return interval;
  181. }
  182. /* Sysfs stuff */
  183. static ssize_t show_input(struct device *dev, struct device_attribute *attr,
  184. char *buf)
  185. {
  186. struct lm95245_data *data = lm95245_update_device(dev);
  187. int temp;
  188. int index = to_sensor_dev_attr(attr)->index;
  189. /*
  190. * Index 0 (Local temp) is always signed
  191. * Index 2 (Remote temp) has both signed and unsigned data
  192. * use signed calculation for remote if signed bit is set
  193. */
  194. if (index == 0 || data->regs[index] & 0x80)
  195. temp = temp_from_reg_signed(data->regs[index],
  196. data->regs[index + 1]);
  197. else
  198. temp = temp_from_reg_unsigned(data->regs[index + 2],
  199. data->regs[index + 3]);
  200. return snprintf(buf, PAGE_SIZE - 1, "%d\n", temp);
  201. }
  202. static ssize_t show_limit(struct device *dev, struct device_attribute *attr,
  203. char *buf)
  204. {
  205. struct lm95245_data *data = lm95245_update_device(dev);
  206. int index = to_sensor_dev_attr(attr)->index;
  207. return snprintf(buf, PAGE_SIZE - 1, "%d\n",
  208. data->regs[index] * 1000);
  209. }
  210. static ssize_t set_limit(struct device *dev, struct device_attribute *attr,
  211. const char *buf, size_t count)
  212. {
  213. struct lm95245_data *data = dev_get_drvdata(dev);
  214. int index = to_sensor_dev_attr(attr)->index;
  215. struct i2c_client *client = data->client;
  216. unsigned long val;
  217. if (kstrtoul(buf, 10, &val) < 0)
  218. return -EINVAL;
  219. val /= 1000;
  220. val = clamp_val(val, 0, (index == 6 ? 127 : 255));
  221. mutex_lock(&data->update_lock);
  222. data->valid = 0;
  223. i2c_smbus_write_byte_data(client, lm95245_reg_address[index], val);
  224. mutex_unlock(&data->update_lock);
  225. return count;
  226. }
  227. static ssize_t show_crit_hyst(struct device *dev, struct device_attribute *attr,
  228. char *buf)
  229. {
  230. struct lm95245_data *data = lm95245_update_device(dev);
  231. int index = to_sensor_dev_attr(attr)->index;
  232. int hyst = data->regs[index] - data->regs[8];
  233. return snprintf(buf, PAGE_SIZE - 1, "%d\n", hyst * 1000);
  234. }
  235. static ssize_t set_crit_hyst(struct device *dev, struct device_attribute *attr,
  236. const char *buf, size_t count)
  237. {
  238. struct lm95245_data *data = dev_get_drvdata(dev);
  239. int index = to_sensor_dev_attr(attr)->index;
  240. struct i2c_client *client = data->client;
  241. unsigned long val;
  242. int hyst, limit;
  243. if (kstrtoul(buf, 10, &val) < 0)
  244. return -EINVAL;
  245. mutex_lock(&data->update_lock);
  246. limit = i2c_smbus_read_byte_data(client, lm95245_reg_address[index]);
  247. hyst = limit - val / 1000;
  248. hyst = clamp_val(hyst, 0, 31);
  249. data->regs[8] = hyst;
  250. /* shared crit hysteresis */
  251. i2c_smbus_write_byte_data(client, LM95245_REG_RW_COMMON_HYSTERESIS,
  252. hyst);
  253. mutex_unlock(&data->update_lock);
  254. return count;
  255. }
  256. static ssize_t show_type(struct device *dev, struct device_attribute *attr,
  257. char *buf)
  258. {
  259. struct lm95245_data *data = dev_get_drvdata(dev);
  260. return snprintf(buf, PAGE_SIZE - 1,
  261. data->config2 & CFG2_REMOTE_TT ? "1\n" : "2\n");
  262. }
  263. static ssize_t set_type(struct device *dev, struct device_attribute *attr,
  264. const char *buf, size_t count)
  265. {
  266. struct lm95245_data *data = dev_get_drvdata(dev);
  267. struct i2c_client *client = data->client;
  268. unsigned long val;
  269. if (kstrtoul(buf, 10, &val) < 0)
  270. return -EINVAL;
  271. if (val != 1 && val != 2)
  272. return -EINVAL;
  273. mutex_lock(&data->update_lock);
  274. if (val == 1)
  275. data->config2 |= CFG2_REMOTE_TT;
  276. else
  277. data->config2 &= ~CFG2_REMOTE_TT;
  278. data->valid = 0;
  279. i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG2,
  280. data->config2);
  281. mutex_unlock(&data->update_lock);
  282. return count;
  283. }
  284. static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
  285. char *buf)
  286. {
  287. struct lm95245_data *data = lm95245_update_device(dev);
  288. int index = to_sensor_dev_attr(attr)->index;
  289. return snprintf(buf, PAGE_SIZE - 1, "%d\n",
  290. !!(data->regs[9] & index));
  291. }
  292. static ssize_t show_interval(struct device *dev, struct device_attribute *attr,
  293. char *buf)
  294. {
  295. struct lm95245_data *data = lm95245_update_device(dev);
  296. return snprintf(buf, PAGE_SIZE - 1, "%lu\n", data->interval);
  297. }
  298. static ssize_t set_interval(struct device *dev, struct device_attribute *attr,
  299. const char *buf, size_t count)
  300. {
  301. struct lm95245_data *data = dev_get_drvdata(dev);
  302. struct i2c_client *client = data->client;
  303. unsigned long val;
  304. if (kstrtoul(buf, 10, &val) < 0)
  305. return -EINVAL;
  306. mutex_lock(&data->update_lock);
  307. data->interval = lm95245_set_conversion_rate(client, val);
  308. mutex_unlock(&data->update_lock);
  309. return count;
  310. }
  311. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_input, NULL, 0);
  312. static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_limit,
  313. set_limit, 6);
  314. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_crit_hyst,
  315. set_crit_hyst, 6);
  316. static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL,
  317. STATUS1_LOC);
  318. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_input, NULL, 2);
  319. static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_limit,
  320. set_limit, 7);
  321. static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_crit_hyst, NULL, 7);
  322. static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL,
  323. STATUS1_RTCRIT);
  324. static SENSOR_DEVICE_ATTR(temp2_type, S_IWUSR | S_IRUGO, show_type,
  325. set_type, 0);
  326. static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL,
  327. STATUS1_DIODE_FAULT);
  328. static DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO, show_interval,
  329. set_interval);
  330. static struct attribute *lm95245_attrs[] = {
  331. &sensor_dev_attr_temp1_input.dev_attr.attr,
  332. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  333. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  334. &sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
  335. &sensor_dev_attr_temp2_input.dev_attr.attr,
  336. &sensor_dev_attr_temp2_crit.dev_attr.attr,
  337. &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
  338. &sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
  339. &sensor_dev_attr_temp2_type.dev_attr.attr,
  340. &sensor_dev_attr_temp2_fault.dev_attr.attr,
  341. &dev_attr_update_interval.attr,
  342. NULL
  343. };
  344. ATTRIBUTE_GROUPS(lm95245);
  345. /* Return 0 if detection is successful, -ENODEV otherwise */
  346. static int lm95245_detect(struct i2c_client *new_client,
  347. struct i2c_board_info *info)
  348. {
  349. struct i2c_adapter *adapter = new_client->adapter;
  350. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  351. return -ENODEV;
  352. if (i2c_smbus_read_byte_data(new_client, LM95245_REG_R_MAN_ID)
  353. != MANUFACTURER_ID
  354. || i2c_smbus_read_byte_data(new_client, LM95245_REG_R_CHIP_ID)
  355. != DEFAULT_REVISION)
  356. return -ENODEV;
  357. strlcpy(info->type, DEVNAME, I2C_NAME_SIZE);
  358. return 0;
  359. }
  360. static void lm95245_init_client(struct i2c_client *client,
  361. struct lm95245_data *data)
  362. {
  363. data->interval = lm95245_read_conversion_rate(client);
  364. data->config1 = i2c_smbus_read_byte_data(client,
  365. LM95245_REG_RW_CONFIG1);
  366. data->config2 = i2c_smbus_read_byte_data(client,
  367. LM95245_REG_RW_CONFIG2);
  368. if (data->config1 & CFG_STOP) {
  369. /* Clear the standby bit */
  370. data->config1 &= ~CFG_STOP;
  371. i2c_smbus_write_byte_data(client, LM95245_REG_RW_CONFIG1,
  372. data->config1);
  373. }
  374. }
  375. static int lm95245_probe(struct i2c_client *client,
  376. const struct i2c_device_id *id)
  377. {
  378. struct device *dev = &client->dev;
  379. struct lm95245_data *data;
  380. struct device *hwmon_dev;
  381. data = devm_kzalloc(dev, sizeof(struct lm95245_data), GFP_KERNEL);
  382. if (!data)
  383. return -ENOMEM;
  384. data->client = client;
  385. mutex_init(&data->update_lock);
  386. /* Initialize the LM95245 chip */
  387. lm95245_init_client(client, data);
  388. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  389. data,
  390. lm95245_groups);
  391. return PTR_ERR_OR_ZERO(hwmon_dev);
  392. }
  393. /* Driver data (common to all clients) */
  394. static const struct i2c_device_id lm95245_id[] = {
  395. { DEVNAME, 0 },
  396. { }
  397. };
  398. MODULE_DEVICE_TABLE(i2c, lm95245_id);
  399. static struct i2c_driver lm95245_driver = {
  400. .class = I2C_CLASS_HWMON,
  401. .driver = {
  402. .name = DEVNAME,
  403. },
  404. .probe = lm95245_probe,
  405. .id_table = lm95245_id,
  406. .detect = lm95245_detect,
  407. .address_list = normal_i2c,
  408. };
  409. module_i2c_driver(lm95245_driver);
  410. MODULE_AUTHOR("Alexander Stein <alexander.stein@systec-electronic.com>");
  411. MODULE_DESCRIPTION("LM95245 sensor driver");
  412. MODULE_LICENSE("GPL");