tmp102.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* Texas Instruments TMP102 SMBus temperature sensor driver
  2. *
  3. * Copyright (C) 2010 Steven King <sfking@fdwdc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include <linux/hwmon.h>
  24. #include <linux/hwmon-sysfs.h>
  25. #include <linux/err.h>
  26. #include <linux/mutex.h>
  27. #include <linux/device.h>
  28. #define DRIVER_NAME "tmp102"
  29. #define TMP102_TEMP_REG 0x00
  30. #define TMP102_CONF_REG 0x01
  31. /* note: these bit definitions are byte swapped */
  32. #define TMP102_CONF_SD 0x0100
  33. #define TMP102_CONF_TM 0x0200
  34. #define TMP102_CONF_POL 0x0400
  35. #define TMP102_CONF_F0 0x0800
  36. #define TMP102_CONF_F1 0x1000
  37. #define TMP102_CONF_R0 0x2000
  38. #define TMP102_CONF_R1 0x4000
  39. #define TMP102_CONF_OS 0x8000
  40. #define TMP102_CONF_EM 0x0010
  41. #define TMP102_CONF_AL 0x0020
  42. #define TMP102_CONF_CR0 0x0040
  43. #define TMP102_CONF_CR1 0x0080
  44. #define TMP102_TLOW_REG 0x02
  45. #define TMP102_THIGH_REG 0x03
  46. struct tmp102 {
  47. struct device *hwmon_dev;
  48. struct mutex lock;
  49. u16 config_orig;
  50. unsigned long last_update;
  51. int temp[3];
  52. };
  53. /* SMBus specifies low byte first, but the TMP102 returns high byte first,
  54. * so we have to swab16 the values */
  55. static inline int tmp102_read_reg(struct i2c_client *client, u8 reg)
  56. {
  57. int result = i2c_smbus_read_word_data(client, reg);
  58. return result < 0 ? result : swab16(result);
  59. }
  60. static inline int tmp102_write_reg(struct i2c_client *client, u8 reg, u16 val)
  61. {
  62. return i2c_smbus_write_word_data(client, reg, swab16(val));
  63. }
  64. /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
  65. static inline int tmp102_reg_to_mC(s16 val)
  66. {
  67. return ((val & ~0x01) * 1000) / 128;
  68. }
  69. /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
  70. static inline u16 tmp102_mC_to_reg(int val)
  71. {
  72. return (val * 128) / 1000;
  73. }
  74. static const u8 tmp102_reg[] = {
  75. TMP102_TEMP_REG,
  76. TMP102_TLOW_REG,
  77. TMP102_THIGH_REG,
  78. };
  79. static struct tmp102 *tmp102_update_device(struct i2c_client *client)
  80. {
  81. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  82. mutex_lock(&tmp102->lock);
  83. if (time_after(jiffies, tmp102->last_update + HZ / 3)) {
  84. int i;
  85. for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
  86. int status = tmp102_read_reg(client, tmp102_reg[i]);
  87. if (status > -1)
  88. tmp102->temp[i] = tmp102_reg_to_mC(status);
  89. }
  90. tmp102->last_update = jiffies;
  91. }
  92. mutex_unlock(&tmp102->lock);
  93. return tmp102;
  94. }
  95. static ssize_t tmp102_show_temp(struct device *dev,
  96. struct device_attribute *attr,
  97. char *buf)
  98. {
  99. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  100. struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev));
  101. return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
  102. }
  103. static ssize_t tmp102_set_temp(struct device *dev,
  104. struct device_attribute *attr,
  105. const char *buf, size_t count)
  106. {
  107. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  108. struct i2c_client *client = to_i2c_client(dev);
  109. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  110. long val;
  111. int status;
  112. if (strict_strtol(buf, 10, &val) < 0)
  113. return -EINVAL;
  114. val = SENSORS_LIMIT(val, -256000, 255000);
  115. mutex_lock(&tmp102->lock);
  116. tmp102->temp[sda->index] = val;
  117. status = tmp102_write_reg(client, tmp102_reg[sda->index],
  118. tmp102_mC_to_reg(val));
  119. mutex_unlock(&tmp102->lock);
  120. return status ? : count;
  121. }
  122. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
  123. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
  124. tmp102_set_temp, 1);
  125. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
  126. tmp102_set_temp, 2);
  127. static struct attribute *tmp102_attributes[] = {
  128. &sensor_dev_attr_temp1_input.dev_attr.attr,
  129. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  130. &sensor_dev_attr_temp1_max.dev_attr.attr,
  131. NULL
  132. };
  133. static const struct attribute_group tmp102_attr_group = {
  134. .attrs = tmp102_attributes,
  135. };
  136. #define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1)
  137. #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL)
  138. static int __devinit tmp102_probe(struct i2c_client *client,
  139. const struct i2c_device_id *id)
  140. {
  141. struct tmp102 *tmp102;
  142. int status;
  143. if (!i2c_check_functionality(client->adapter,
  144. I2C_FUNC_SMBUS_WORD_DATA)) {
  145. dev_err(&client->dev, "adapter doesn't support SMBus word "
  146. "transactions\n");
  147. return -ENODEV;
  148. }
  149. tmp102 = kzalloc(sizeof(*tmp102), GFP_KERNEL);
  150. if (!tmp102) {
  151. dev_dbg(&client->dev, "kzalloc failed\n");
  152. return -ENOMEM;
  153. }
  154. i2c_set_clientdata(client, tmp102);
  155. status = tmp102_read_reg(client, TMP102_CONF_REG);
  156. if (status < 0) {
  157. dev_err(&client->dev, "error reading config register\n");
  158. goto fail_free;
  159. }
  160. tmp102->config_orig = status;
  161. status = tmp102_write_reg(client, TMP102_CONF_REG, TMP102_CONFIG);
  162. if (status < 0) {
  163. dev_err(&client->dev, "error writing config register\n");
  164. goto fail_restore_config;
  165. }
  166. status = tmp102_read_reg(client, TMP102_CONF_REG);
  167. if (status < 0) {
  168. dev_err(&client->dev, "error reading config register\n");
  169. goto fail_restore_config;
  170. }
  171. status &= ~TMP102_CONFIG_RD_ONLY;
  172. if (status != TMP102_CONFIG) {
  173. dev_err(&client->dev, "config settings did not stick\n");
  174. status = -ENODEV;
  175. goto fail_restore_config;
  176. }
  177. tmp102->last_update = jiffies - HZ;
  178. mutex_init(&tmp102->lock);
  179. status = sysfs_create_group(&client->dev.kobj, &tmp102_attr_group);
  180. if (status) {
  181. dev_dbg(&client->dev, "could not create sysfs files\n");
  182. goto fail_restore_config;
  183. }
  184. tmp102->hwmon_dev = hwmon_device_register(&client->dev);
  185. if (IS_ERR(tmp102->hwmon_dev)) {
  186. dev_dbg(&client->dev, "unable to register hwmon device\n");
  187. status = PTR_ERR(tmp102->hwmon_dev);
  188. goto fail_remove_sysfs;
  189. }
  190. dev_info(&client->dev, "initialized\n");
  191. return 0;
  192. fail_remove_sysfs:
  193. sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
  194. fail_restore_config:
  195. tmp102_write_reg(client, TMP102_CONF_REG, tmp102->config_orig);
  196. fail_free:
  197. kfree(tmp102);
  198. return status;
  199. }
  200. static int __devexit tmp102_remove(struct i2c_client *client)
  201. {
  202. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  203. hwmon_device_unregister(tmp102->hwmon_dev);
  204. sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
  205. /* Stop monitoring if device was stopped originally */
  206. if (tmp102->config_orig & TMP102_CONF_SD) {
  207. int config;
  208. config = tmp102_read_reg(client, TMP102_CONF_REG);
  209. if (config >= 0)
  210. tmp102_write_reg(client, TMP102_CONF_REG,
  211. config | TMP102_CONF_SD);
  212. }
  213. kfree(tmp102);
  214. return 0;
  215. }
  216. #ifdef CONFIG_PM
  217. static int tmp102_suspend(struct device *dev)
  218. {
  219. struct i2c_client *client = to_i2c_client(dev);
  220. int config;
  221. config = tmp102_read_reg(client, TMP102_CONF_REG);
  222. if (config < 0)
  223. return config;
  224. config |= TMP102_CONF_SD;
  225. return tmp102_write_reg(client, TMP102_CONF_REG, config);
  226. }
  227. static int tmp102_resume(struct device *dev)
  228. {
  229. struct i2c_client *client = to_i2c_client(dev);
  230. int config;
  231. config = tmp102_read_reg(client, TMP102_CONF_REG);
  232. if (config < 0)
  233. return config;
  234. config &= ~TMP102_CONF_SD;
  235. return tmp102_write_reg(client, TMP102_CONF_REG, config);
  236. }
  237. static const struct dev_pm_ops tmp102_dev_pm_ops = {
  238. .suspend = tmp102_suspend,
  239. .resume = tmp102_resume,
  240. };
  241. #define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops)
  242. #else
  243. #define TMP102_DEV_PM_OPS NULL
  244. #endif /* CONFIG_PM */
  245. static const struct i2c_device_id tmp102_id[] = {
  246. { "tmp102", 0 },
  247. { }
  248. };
  249. MODULE_DEVICE_TABLE(i2c, tmp102_id);
  250. static struct i2c_driver tmp102_driver = {
  251. .driver.name = DRIVER_NAME,
  252. .driver.pm = TMP102_DEV_PM_OPS,
  253. .probe = tmp102_probe,
  254. .remove = __devexit_p(tmp102_remove),
  255. .id_table = tmp102_id,
  256. };
  257. static int __init tmp102_init(void)
  258. {
  259. return i2c_add_driver(&tmp102_driver);
  260. }
  261. module_init(tmp102_init);
  262. static void __exit tmp102_exit(void)
  263. {
  264. i2c_del_driver(&tmp102_driver);
  265. }
  266. module_exit(tmp102_exit);
  267. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  268. MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
  269. MODULE_LICENSE("GPL");