tmp102.c 8.2 KB

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