tmp102.c 8.5 KB

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