shtc1.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Sensirion SHTC1 humidity and temperature sensor driver
  2. *
  3. * Copyright (C) 2014 Sensirion AG, Switzerland
  4. * Author: Johannes Winkelmann <johannes.winkelmann@sensirion.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.h>
  23. #include <linux/err.h>
  24. #include <linux/delay.h>
  25. #include <linux/platform_data/shtc1.h>
  26. /* commands (high precision mode) */
  27. static const unsigned char shtc1_cmd_measure_blocking_hpm[] = { 0x7C, 0xA2 };
  28. static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
  29. /* commands (low precision mode) */
  30. static const unsigned char shtc1_cmd_measure_blocking_lpm[] = { 0x64, 0x58 };
  31. static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
  32. /* command for reading the ID register */
  33. static const unsigned char shtc1_cmd_read_id_reg[] = { 0xef, 0xc8 };
  34. /* constants for reading the ID register */
  35. #define SHTC1_ID 0x07
  36. #define SHTC1_ID_REG_MASK 0x1f
  37. /* delays for non-blocking i2c commands, both in us */
  38. #define SHTC1_NONBLOCKING_WAIT_TIME_HPM 14400
  39. #define SHTC1_NONBLOCKING_WAIT_TIME_LPM 1000
  40. #define SHTC1_CMD_LENGTH 2
  41. #define SHTC1_RESPONSE_LENGTH 6
  42. struct shtc1_data {
  43. struct i2c_client *client;
  44. struct mutex update_lock;
  45. bool valid;
  46. unsigned long last_updated; /* in jiffies */
  47. const unsigned char *command;
  48. unsigned int nonblocking_wait_time; /* in us */
  49. struct shtc1_platform_data setup;
  50. int temperature; /* 1000 * temperature in dgr C */
  51. int humidity; /* 1000 * relative humidity in %RH */
  52. };
  53. static int shtc1_update_values(struct i2c_client *client,
  54. struct shtc1_data *data,
  55. char *buf, int bufsize)
  56. {
  57. int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
  58. if (ret != SHTC1_CMD_LENGTH) {
  59. dev_err(&client->dev, "failed to send command: %d\n", ret);
  60. return ret < 0 ? ret : -EIO;
  61. }
  62. /*
  63. * In blocking mode (clock stretching mode) the I2C bus
  64. * is blocked for other traffic, thus the call to i2c_master_recv()
  65. * will wait until the data is ready. For non blocking mode, we
  66. * have to wait ourselves.
  67. */
  68. if (!data->setup.blocking_io)
  69. usleep_range(data->nonblocking_wait_time,
  70. data->nonblocking_wait_time + 1000);
  71. ret = i2c_master_recv(client, buf, bufsize);
  72. if (ret != bufsize) {
  73. dev_err(&client->dev, "failed to read values: %d\n", ret);
  74. return ret < 0 ? ret : -EIO;
  75. }
  76. return 0;
  77. }
  78. /* sysfs attributes */
  79. static struct shtc1_data *shtc1_update_client(struct device *dev)
  80. {
  81. struct shtc1_data *data = dev_get_drvdata(dev);
  82. struct i2c_client *client = data->client;
  83. unsigned char buf[SHTC1_RESPONSE_LENGTH];
  84. int val;
  85. int ret = 0;
  86. mutex_lock(&data->update_lock);
  87. if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  88. ret = shtc1_update_values(client, data, buf, sizeof(buf));
  89. if (ret)
  90. goto out;
  91. /*
  92. * From datasheet:
  93. * T = -45 + 175 * ST / 2^16
  94. * RH = 100 * SRH / 2^16
  95. *
  96. * Adapted for integer fixed point (3 digit) arithmetic.
  97. */
  98. val = be16_to_cpup((__be16 *)buf);
  99. data->temperature = ((21875 * val) >> 13) - 45000;
  100. val = be16_to_cpup((__be16 *)(buf + 3));
  101. data->humidity = ((12500 * val) >> 13);
  102. data->last_updated = jiffies;
  103. data->valid = true;
  104. }
  105. out:
  106. mutex_unlock(&data->update_lock);
  107. return ret == 0 ? data : ERR_PTR(ret);
  108. }
  109. static ssize_t temp1_input_show(struct device *dev,
  110. struct device_attribute *attr,
  111. char *buf)
  112. {
  113. struct shtc1_data *data = shtc1_update_client(dev);
  114. if (IS_ERR(data))
  115. return PTR_ERR(data);
  116. return sprintf(buf, "%d\n", data->temperature);
  117. }
  118. static ssize_t humidity1_input_show(struct device *dev,
  119. struct device_attribute *attr, char *buf)
  120. {
  121. struct shtc1_data *data = shtc1_update_client(dev);
  122. if (IS_ERR(data))
  123. return PTR_ERR(data);
  124. return sprintf(buf, "%d\n", data->humidity);
  125. }
  126. static DEVICE_ATTR_RO(temp1_input);
  127. static DEVICE_ATTR_RO(humidity1_input);
  128. static struct attribute *shtc1_attrs[] = {
  129. &dev_attr_temp1_input.attr,
  130. &dev_attr_humidity1_input.attr,
  131. NULL
  132. };
  133. ATTRIBUTE_GROUPS(shtc1);
  134. static void shtc1_select_command(struct shtc1_data *data)
  135. {
  136. if (data->setup.high_precision) {
  137. data->command = data->setup.blocking_io ?
  138. shtc1_cmd_measure_blocking_hpm :
  139. shtc1_cmd_measure_nonblocking_hpm;
  140. data->nonblocking_wait_time = SHTC1_NONBLOCKING_WAIT_TIME_HPM;
  141. } else {
  142. data->command = data->setup.blocking_io ?
  143. shtc1_cmd_measure_blocking_lpm :
  144. shtc1_cmd_measure_nonblocking_lpm;
  145. data->nonblocking_wait_time = SHTC1_NONBLOCKING_WAIT_TIME_LPM;
  146. }
  147. }
  148. static int shtc1_probe(struct i2c_client *client,
  149. const struct i2c_device_id *id)
  150. {
  151. int ret;
  152. char id_reg[2];
  153. struct shtc1_data *data;
  154. struct device *hwmon_dev;
  155. struct i2c_adapter *adap = client->adapter;
  156. struct device *dev = &client->dev;
  157. if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
  158. dev_err(dev, "plain i2c transactions not supported\n");
  159. return -ENODEV;
  160. }
  161. ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
  162. if (ret != SHTC1_CMD_LENGTH) {
  163. dev_err(dev, "could not send read_id_reg command: %d\n", ret);
  164. return ret < 0 ? ret : -ENODEV;
  165. }
  166. ret = i2c_master_recv(client, id_reg, sizeof(id_reg));
  167. if (ret != sizeof(id_reg)) {
  168. dev_err(dev, "could not read ID register: %d\n", ret);
  169. return -ENODEV;
  170. }
  171. if ((id_reg[1] & SHTC1_ID_REG_MASK) != SHTC1_ID) {
  172. dev_err(dev, "ID register doesn't match\n");
  173. return -ENODEV;
  174. }
  175. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  176. if (!data)
  177. return -ENOMEM;
  178. data->setup.blocking_io = false;
  179. data->setup.high_precision = true;
  180. data->client = client;
  181. if (client->dev.platform_data)
  182. data->setup = *(struct shtc1_platform_data *)dev->platform_data;
  183. shtc1_select_command(data);
  184. mutex_init(&data->update_lock);
  185. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  186. client->name,
  187. data,
  188. shtc1_groups);
  189. if (IS_ERR(hwmon_dev))
  190. dev_dbg(dev, "unable to register hwmon device\n");
  191. return PTR_ERR_OR_ZERO(hwmon_dev);
  192. }
  193. /* device ID table */
  194. static const struct i2c_device_id shtc1_id[] = {
  195. { "shtc1", 0 },
  196. { "shtw1", 0 },
  197. { }
  198. };
  199. MODULE_DEVICE_TABLE(i2c, shtc1_id);
  200. static struct i2c_driver shtc1_i2c_driver = {
  201. .driver.name = "shtc1",
  202. .probe = shtc1_probe,
  203. .id_table = shtc1_id,
  204. };
  205. module_i2c_driver(shtc1_i2c_driver);
  206. MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
  207. MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
  208. MODULE_LICENSE("GPL");