lm95245.c 13 KB

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