tmp102.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #include <linux/delay.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c.h>
  20. #include <linux/hwmon.h>
  21. #include <linux/hwmon-sysfs.h>
  22. #include <linux/err.h>
  23. #include <linux/mutex.h>
  24. #include <linux/device.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/thermal.h>
  27. #include <linux/of.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. #define CONVERSION_TIME_MS 35 /* in milli-seconds */
  47. struct tmp102 {
  48. struct i2c_client *client;
  49. struct mutex lock;
  50. u16 config_orig;
  51. unsigned long last_update;
  52. unsigned long ready_time;
  53. bool valid;
  54. int temp[3];
  55. };
  56. /* convert left adjusted 13-bit TMP102 register value to milliCelsius */
  57. static inline int tmp102_reg_to_mC(s16 val)
  58. {
  59. return ((val & ~0x01) * 1000) / 128;
  60. }
  61. /* convert milliCelsius to left adjusted 13-bit TMP102 register value */
  62. static inline u16 tmp102_mC_to_reg(int val)
  63. {
  64. return (val * 128) / 1000;
  65. }
  66. static const u8 tmp102_reg[] = {
  67. TMP102_TEMP_REG,
  68. TMP102_TLOW_REG,
  69. TMP102_THIGH_REG,
  70. };
  71. static void tmp102_update_device(struct device *dev)
  72. {
  73. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  74. struct i2c_client *client = tmp102->client;
  75. mutex_lock(&tmp102->lock);
  76. if (!tmp102->valid ||
  77. time_after(jiffies, tmp102->last_update + HZ / 3)) {
  78. int i;
  79. for (i = 0; i < ARRAY_SIZE(tmp102->temp); ++i) {
  80. int status = i2c_smbus_read_word_swapped(client,
  81. tmp102_reg[i]);
  82. if (status > -1)
  83. tmp102->temp[i] = tmp102_reg_to_mC(status);
  84. }
  85. tmp102->last_update = jiffies;
  86. tmp102->valid = true;
  87. }
  88. mutex_unlock(&tmp102->lock);
  89. }
  90. static int tmp102_read_temp(void *dev, int *temp)
  91. {
  92. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  93. if (time_before(jiffies, tmp102->ready_time)) {
  94. dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__);
  95. return -EAGAIN;
  96. }
  97. tmp102_update_device(dev);
  98. *temp = tmp102->temp[0];
  99. return 0;
  100. }
  101. static ssize_t tmp102_show_temp(struct device *dev,
  102. struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  106. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  107. if (time_before(jiffies, tmp102->ready_time))
  108. return -EAGAIN;
  109. tmp102_update_device(dev);
  110. return sprintf(buf, "%d\n", tmp102->temp[sda->index]);
  111. }
  112. static ssize_t tmp102_set_temp(struct device *dev,
  113. struct device_attribute *attr,
  114. const char *buf, size_t count)
  115. {
  116. struct sensor_device_attribute *sda = to_sensor_dev_attr(attr);
  117. struct tmp102 *tmp102 = dev_get_drvdata(dev);
  118. struct i2c_client *client = tmp102->client;
  119. long val;
  120. int status;
  121. if (kstrtol(buf, 10, &val) < 0)
  122. return -EINVAL;
  123. val = clamp_val(val, -256000, 255000);
  124. mutex_lock(&tmp102->lock);
  125. tmp102->temp[sda->index] = val;
  126. status = i2c_smbus_write_word_swapped(client, tmp102_reg[sda->index],
  127. tmp102_mC_to_reg(val));
  128. mutex_unlock(&tmp102->lock);
  129. return status ? : count;
  130. }
  131. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL , 0);
  132. static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp,
  133. tmp102_set_temp, 1);
  134. static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp,
  135. tmp102_set_temp, 2);
  136. static struct attribute *tmp102_attrs[] = {
  137. &sensor_dev_attr_temp1_input.dev_attr.attr,
  138. &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
  139. &sensor_dev_attr_temp1_max.dev_attr.attr,
  140. NULL
  141. };
  142. ATTRIBUTE_GROUPS(tmp102);
  143. #define TMP102_CONFIG (TMP102_CONF_TM | TMP102_CONF_EM | TMP102_CONF_CR1)
  144. #define TMP102_CONFIG_RD_ONLY (TMP102_CONF_R0 | TMP102_CONF_R1 | TMP102_CONF_AL)
  145. static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = {
  146. .get_temp = tmp102_read_temp,
  147. };
  148. static void tmp102_restore_config(void *data)
  149. {
  150. struct tmp102 *tmp102 = data;
  151. struct i2c_client *client = tmp102->client;
  152. i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
  153. tmp102->config_orig);
  154. }
  155. static int tmp102_probe(struct i2c_client *client,
  156. const struct i2c_device_id *id)
  157. {
  158. struct device *dev = &client->dev;
  159. struct device *hwmon_dev;
  160. struct tmp102 *tmp102;
  161. int status;
  162. if (!i2c_check_functionality(client->adapter,
  163. I2C_FUNC_SMBUS_WORD_DATA)) {
  164. dev_err(dev,
  165. "adapter doesn't support SMBus word transactions\n");
  166. return -ENODEV;
  167. }
  168. tmp102 = devm_kzalloc(dev, sizeof(*tmp102), GFP_KERNEL);
  169. if (!tmp102)
  170. return -ENOMEM;
  171. i2c_set_clientdata(client, tmp102);
  172. tmp102->client = client;
  173. status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  174. if (status < 0) {
  175. dev_err(dev, "error reading config register\n");
  176. return status;
  177. }
  178. tmp102->config_orig = status;
  179. devm_add_action(dev, tmp102_restore_config, tmp102);
  180. status = i2c_smbus_write_word_swapped(client, TMP102_CONF_REG,
  181. TMP102_CONFIG);
  182. if (status < 0) {
  183. dev_err(dev, "error writing config register\n");
  184. return status;
  185. }
  186. status = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  187. if (status < 0) {
  188. dev_err(dev, "error reading config register\n");
  189. return status;
  190. }
  191. status &= ~TMP102_CONFIG_RD_ONLY;
  192. if (status != TMP102_CONFIG) {
  193. dev_err(dev, "config settings did not stick\n");
  194. return -ENODEV;
  195. }
  196. mutex_init(&tmp102->lock);
  197. tmp102->ready_time = jiffies;
  198. if (tmp102->config_orig & TMP102_CONF_SD) {
  199. /*
  200. * Mark that we are not ready with data until the first
  201. * conversion is complete
  202. */
  203. tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS);
  204. }
  205. hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
  206. tmp102,
  207. tmp102_groups);
  208. if (IS_ERR(hwmon_dev)) {
  209. dev_dbg(dev, "unable to register hwmon device\n");
  210. return PTR_ERR(hwmon_dev);
  211. }
  212. devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev,
  213. &tmp102_of_thermal_ops);
  214. dev_info(dev, "initialized\n");
  215. return 0;
  216. }
  217. #ifdef CONFIG_PM_SLEEP
  218. static int tmp102_suspend(struct device *dev)
  219. {
  220. struct i2c_client *client = to_i2c_client(dev);
  221. int config;
  222. config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  223. if (config < 0)
  224. return config;
  225. config |= TMP102_CONF_SD;
  226. return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
  227. }
  228. static int tmp102_resume(struct device *dev)
  229. {
  230. struct i2c_client *client = to_i2c_client(dev);
  231. struct tmp102 *tmp102 = i2c_get_clientdata(client);
  232. int config;
  233. config = i2c_smbus_read_word_swapped(client, TMP102_CONF_REG);
  234. if (config < 0)
  235. return config;
  236. tmp102->ready_time = jiffies + msecs_to_jiffies(CONVERSION_TIME_MS);
  237. config &= ~TMP102_CONF_SD;
  238. return i2c_smbus_write_word_swapped(client, TMP102_CONF_REG, config);
  239. }
  240. #endif /* CONFIG_PM */
  241. static SIMPLE_DEV_PM_OPS(tmp102_dev_pm_ops, tmp102_suspend, tmp102_resume);
  242. static const struct i2c_device_id tmp102_id[] = {
  243. { "tmp102", 0 },
  244. { }
  245. };
  246. MODULE_DEVICE_TABLE(i2c, tmp102_id);
  247. static struct i2c_driver tmp102_driver = {
  248. .driver.name = DRIVER_NAME,
  249. .driver.pm = &tmp102_dev_pm_ops,
  250. .probe = tmp102_probe,
  251. .id_table = tmp102_id,
  252. };
  253. module_i2c_driver(tmp102_driver);
  254. MODULE_AUTHOR("Steven King <sfking@fdwdc.com>");
  255. MODULE_DESCRIPTION("Texas Instruments TMP102 temperature sensor driver");
  256. MODULE_LICENSE("GPL");