tmp102.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include <linux/jiffies.h>
  29. #define DRIVER_NAME "tmp102"
  30. #define TMP102_TEMP_REG 0x00
  31. #define TMP102_CONF_REG 0x01
  32. /* note: these bit definitions are byte swapped */
  33. #define TMP102_CONF_SD 0x0100
  34. #define TMP102_CONF_TM 0x0200
  35. #define TMP102_CONF_POL 0x0400
  36. #define TMP102_CONF_F0 0x0800
  37. #define TMP102_CONF_F1 0x1000
  38. #define TMP102_CONF_R0 0x2000
  39. #define TMP102_CONF_R1 0x4000
  40. #define TMP102_CONF_OS 0x8000
  41. #define TMP102_CONF_EM 0x0010
  42. #define TMP102_CONF_AL 0x0020
  43. #define TMP102_CONF_CR0 0x0040
  44. #define TMP102_CONF_CR1 0x0080
  45. #define TMP102_TLOW_REG 0x02
  46. #define TMP102_THIGH_REG 0x03
  47. struct tmp102 {
  48. struct device *hwmon_dev;
  49. struct mutex lock;
  50. u16 config_orig;
  51. unsigned long last_update;
  52. int temp[3];
  53. };
  54. /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
  55. static inline int tmp102_reg_to_mC(s16 val)
  56. {
  57. return ((val & ~0x01) * 1000) / 128;
  58. }
  59. /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
  60. static inline u16 tmp102_mC_to_reg(int val)
  61. {
  62. return (val * 128) / 1000;
  63. }
  64. static const u8 tmp102_reg[] = {
  65. TMP102_TEMP_REG,
  66. TMP102_TLOW_REG,
  67. TMP102_THIGH_REG,
  68. };
  69. static struct tmp102 *tmp102_update_device(struct i2c_client *client)
  70. {
  71. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  72. mutex_lock(&tmp102->lock);
  73. if (time_after(jiffies, tmp102->last_update + HZ / 3)) {
  74. int i;
  75. for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
  76. int status = i2c_smbus_read_word_swapped(client,
  77. tmp102_reg[i]);
  78. if (status > -1)
  79. tmp102->temp[i] = tmp102_reg_to_mC(status);
  80. }
  81. tmp102->last_update = jiffies;
  82. }
  83. mutex_unlock(&tmp102->lock);
  84. return tmp102;
  85. }
  86. static ssize_t tmp102_show_temp(struct device *dev,
  87. struct device_attribute *attr,
  88. char *buf)
  89. {
  90. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  91. struct tmp102 *tmp102 = tmp102_update_device(to_i2c_client(dev));
  92. return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
  93. }
  94. static ssize_t tmp102_set_temp(struct device *dev,
  95. struct device_attribute *attr,
  96. const char *buf, size_t count)
  97. {
  98. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  99. struct i2c_client *client = to_i2c_client(dev);
  100. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  101. long val;
  102. int status;
  103. if (kstrtol(buf, 10, &val) < 0)
  104. return -EINVAL;
  105. val = clamp_val(val, -256000, 255000);
  106. mutex_lock(&tmp102->lock);
  107. tmp102->temp[sda->index] = val;
  108. status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index],
  109. tmp102_mC_to_reg(val));
  110. mutex_unlock(&tmp102->lock);
  111. return status ? : count;
  112. }
  113. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
  114. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
  115. tmp102_set_temp, 1);
  116. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
  117. tmp102_set_temp, 2);
  118. static struct attribute *tmp102_attributes[] = {
  119. &sensor_dev_attr_temp1_input.dev_attr.attr,
  120. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  121. &sensor_dev_attr_temp1_max.dev_attr.attr,
  122. NULL
  123. };
  124. static const struct attribute_group tmp102_attr_group = {
  125. .attrs = tmp102_attributes,
  126. };
  127. #define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1)
  128. #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL)
  129. static int tmp102_probe(struct i2c_client *client,
  130. const struct i2c_device_id *id)
  131. {
  132. struct tmp102 *tmp102;
  133. int status;
  134. if (!i2c_check_functionality(client->adapter,
  135. I2C_FUNC_SMBUS_WORD_DATA)) {
  136. dev_err(&client->dev, "adapter doesn't support SMBus word "
  137. "transactions\n");
  138. return -ENODEV;
  139. }
  140. tmp102 = devm_kzalloc(&client->dev, sizeof(*tmp102), GFP_KERNEL);
  141. if (!tmp102)
  142. return -ENOMEM;
  143. i2c_set_clientdata(client, tmp102);
  144. status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  145. if (status < 0) {
  146. dev_err(&client->dev, "error reading config register\n");
  147. return status;
  148. }
  149. tmp102->config_orig = status;
  150. status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
  151. TMP102_CONFIG);
  152. if (status < 0) {
  153. dev_err(&client->dev, "error writing config register\n");
  154. goto fail_restore_config;
  155. }
  156. status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  157. if (status < 0) {
  158. dev_err(&client->dev, "error reading config register\n");
  159. goto fail_restore_config;
  160. }
  161. status &= ~TMP102_CONFIG_RD_ONLY;
  162. if (status != TMP102_CONFIG) {
  163. dev_err(&client->dev, "config settings did not stick\n");
  164. status = -ENODEV;
  165. goto fail_restore_config;
  166. }
  167. tmp102->last_update = jiffies - HZ;
  168. mutex_init(&tmp102->lock);
  169. status = sysfs_create_group(&client->dev.kobj, &tmp102_attr_group);
  170. if (status) {
  171. dev_dbg(&client->dev, "could not create sysfs files\n");
  172. goto fail_restore_config;
  173. }
  174. tmp102->hwmon_dev = hwmon_device_register(&client->dev);
  175. if (IS_ERR(tmp102->hwmon_dev)) {
  176. dev_dbg(&client->dev, "unable to register hwmon device\n");
  177. status = PTR_ERR(tmp102->hwmon_dev);
  178. goto fail_remove_sysfs;
  179. }
  180. dev_info(&client->dev, "initialized\n");
  181. return 0;
  182. fail_remove_sysfs:
  183. sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
  184. fail_restore_config:
  185. i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
  186. tmp102->config_orig);
  187. return status;
  188. }
  189. static int tmp102_remove(struct i2c_client *client)
  190. {
  191. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  192. hwmon_device_unregister(tmp102->hwmon_dev);
  193. sysfs_remove_group(&client->dev.kobj, &tmp102_attr_group);
  194. /* Stop monitoring if device was stopped originally */
  195. if (tmp102->config_orig & TMP102_CONF_SD) {
  196. int config;
  197. config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  198. if (config >= 0)
  199. i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
  200. config | TMP102_CONF_SD);
  201. }
  202. return 0;
  203. }
  204. #ifdef CONFIG_PM
  205. static int tmp102_suspend(struct device *dev)
  206. {
  207. struct i2c_client *client = to_i2c_client(dev);
  208. int config;
  209. config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  210. if (config < 0)
  211. return config;
  212. config |= TMP102_CONF_SD;
  213. return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
  214. }
  215. static int tmp102_resume(struct device *dev)
  216. {
  217. struct i2c_client *client = to_i2c_client(dev);
  218. int config;
  219. config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  220. if (config < 0)
  221. return config;
  222. config &= ~TMP102_CONF_SD;
  223. return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
  224. }
  225. static const struct dev_pm_ops tmp102_dev_pm_ops = {
  226. .suspend = tmp102_suspend,
  227. .resume = tmp102_resume,
  228. };
  229. #define TMP102_DEV_PM_OPS (&tmp102_dev_pm_ops)
  230. #else
  231. #define TMP102_DEV_PM_OPS NULL
  232. #endif /* CONFIG_PM */
  233. static const struct i2c_device_id tmp102_id[] = {
  234. { "tmp102", 0 },
  235. { }
  236. };
  237. MODULE_DEVICE_TABLE(i2c, tmp102_id);
  238. static struct i2c_driver tmp102_driver = {
  239. .driver.name = DRIVER_NAME,
  240. .driver.pm = TMP102_DEV_PM_OPS,
  241. .probe = tmp102_probe,
  242. .remove = tmp102_remove,
  243. .id_table = tmp102_id,
  244. };
  245. module_i2c_driver(tmp102_driver);
  246. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  247. MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
  248. MODULE_LICENSE("GPL");