shtc1.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Sensirion SHTC1 humidity and temperature sensor driver
  3. *
  4. * Copyright (C) 2014 Sensirion AG, Switzerland
  5. * Author: Johannes Winkelmann <johannes.winkelmann@sensirion.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/i2c.h>
  11. #include <linux/hwmon.h>
  12. #include <linux/hwmon-sysfs.h>
  13. #include <linux/err.h>
  14. #include <linux/delay.h>
  15. #include <linux/platform_data/shtc1.h>
  16. #include <linux/of.h>
  17. /* commands (high precision mode) */
  18. static const unsigned char shtc1_cmd_measure_blocking_hpm[] = { 0x7C, 0xA2 };
  19. static const unsigned char shtc1_cmd_measure_nonblocking_hpm[] = { 0x78, 0x66 };
  20. /* commands (low precision mode) */
  21. static const unsigned char shtc1_cmd_measure_blocking_lpm[] = { 0x64, 0x58 };
  22. static const unsigned char shtc1_cmd_measure_nonblocking_lpm[] = { 0x60, 0x9c };
  23. /* command for reading the ID register */
  24. static const unsigned char shtc1_cmd_read_id_reg[] = { 0xef, 0xc8 };
  25. /*
  26. * constants for reading the ID register
  27. * SHTC1: 0x0007 with mask 0x003f
  28. * SHTW1: 0x0007 with mask 0x003f
  29. * SHTC3: 0x0807 with mask 0x083f
  30. */
  31. #define SHTC3_ID 0x0807
  32. #define SHTC3_ID_MASK 0x083f
  33. #define SHTC1_ID 0x0007
  34. #define SHTC1_ID_MASK 0x003f
  35. /* delays for non-blocking i2c commands, both in us */
  36. #define SHTC1_NONBLOCKING_WAIT_TIME_HPM 14400
  37. #define SHTC1_NONBLOCKING_WAIT_TIME_LPM 1000
  38. #define SHTC3_NONBLOCKING_WAIT_TIME_HPM 12100
  39. #define SHTC3_NONBLOCKING_WAIT_TIME_LPM 800
  40. #define SHTC1_CMD_LENGTH 2
  41. #define SHTC1_RESPONSE_LENGTH 6
  42. enum shtcx_chips {
  43. shtc1,
  44. shtc3,
  45. };
  46. struct shtc1_data {
  47. struct i2c_client *client;
  48. struct mutex update_lock;
  49. bool valid;
  50. unsigned long last_updated; /* in jiffies */
  51. const unsigned char *command;
  52. unsigned int nonblocking_wait_time; /* in us */
  53. struct shtc1_platform_data setup;
  54. enum shtcx_chips chip;
  55. int temperature; /* 1000 * temperature in dgr C */
  56. int humidity; /* 1000 * relative humidity in %RH */
  57. };
  58. static int shtc1_update_values(struct i2c_client *client,
  59. struct shtc1_data *data,
  60. char *buf, int bufsize)
  61. {
  62. int ret = i2c_master_send(client, data->command, SHTC1_CMD_LENGTH);
  63. if (ret != SHTC1_CMD_LENGTH) {
  64. dev_err(&client->dev, "failed to send command: %d\n", ret);
  65. return ret < 0 ? ret : -EIO;
  66. }
  67. /*
  68. * In blocking mode (clock stretching mode) the I2C bus
  69. * is blocked for other traffic, thus the call to i2c_master_recv()
  70. * will wait until the data is ready. For non blocking mode, we
  71. * have to wait ourselves.
  72. */
  73. if (!data->setup.blocking_io)
  74. usleep_range(data->nonblocking_wait_time,
  75. data->nonblocking_wait_time + 1000);
  76. ret = i2c_master_recv(client, buf, bufsize);
  77. if (ret != bufsize) {
  78. dev_err(&client->dev, "failed to read values: %d\n", ret);
  79. return ret < 0 ? ret : -EIO;
  80. }
  81. return 0;
  82. }
  83. /* sysfs attributes */
  84. static struct shtc1_data *shtc1_update_client(struct device *dev)
  85. {
  86. struct shtc1_data *data = dev_get_drvdata(dev);
  87. struct i2c_client *client = data->client;
  88. unsigned char buf[SHTC1_RESPONSE_LENGTH];
  89. int val;
  90. int ret = 0;
  91. mutex_lock(&data->update_lock);
  92. if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  93. ret = shtc1_update_values(client, data, buf, sizeof(buf));
  94. if (ret)
  95. goto out;
  96. /*
  97. * From datasheet:
  98. * T = -45 + 175 * ST / 2^16
  99. * RH = 100 * SRH / 2^16
  100. *
  101. * Adapted for integer fixed point (3 digit) arithmetic.
  102. */
  103. val = be16_to_cpup((__be16 *)buf);
  104. data->temperature = ((21875 * val) >> 13) - 45000;
  105. val = be16_to_cpup((__be16 *)(buf + 3));
  106. data->humidity = ((12500 * val) >> 13);
  107. data->last_updated = jiffies;
  108. data->valid = true;
  109. }
  110. out:
  111. mutex_unlock(&data->update_lock);
  112. return ret == 0 ? data : ERR_PTR(ret);
  113. }
  114. static ssize_t temp1_input_show(struct device *dev,
  115. struct device_attribute *attr,
  116. char *buf)
  117. {
  118. struct shtc1_data *data = shtc1_update_client(dev);
  119. if (IS_ERR(data))
  120. return PTR_ERR(data);
  121. return sprintf(buf, "%d\n", data->temperature);
  122. }
  123. static ssize_t humidity1_input_show(struct device *dev,
  124. struct device_attribute *attr, char *buf)
  125. {
  126. struct shtc1_data *data = shtc1_update_client(dev);
  127. if (IS_ERR(data))
  128. return PTR_ERR(data);
  129. return sprintf(buf, "%d\n", data->humidity);
  130. }
  131. static DEVICE_ATTR_RO(temp1_input);
  132. static DEVICE_ATTR_RO(humidity1_input);
  133. static struct attribute *shtc1_attrs[] = {
  134. &dev_attr_temp1_input.attr,
  135. &dev_attr_humidity1_input.attr,
  136. NULL
  137. };
  138. ATTRIBUTE_GROUPS(shtc1);
  139. static void shtc1_select_command(struct shtc1_data *data)
  140. {
  141. if (data->setup.high_precision) {
  142. data->command = data->setup.blocking_io ?
  143. shtc1_cmd_measure_blocking_hpm :
  144. shtc1_cmd_measure_nonblocking_hpm;
  145. data->nonblocking_wait_time = (data->chip == shtc1) ?
  146. SHTC1_NONBLOCKING_WAIT_TIME_HPM :
  147. SHTC3_NONBLOCKING_WAIT_TIME_HPM;
  148. } else {
  149. data->command = data->setup.blocking_io ?
  150. shtc1_cmd_measure_blocking_lpm :
  151. shtc1_cmd_measure_nonblocking_lpm;
  152. data->nonblocking_wait_time = (data->chip == shtc1) ?
  153. SHTC1_NONBLOCKING_WAIT_TIME_LPM :
  154. SHTC3_NONBLOCKING_WAIT_TIME_LPM;
  155. }
  156. }
  157. static const struct i2c_device_id shtc1_id[];
  158. static int shtc1_probe(struct i2c_client *client)
  159. {
  160. int ret;
  161. u16 id_reg;
  162. char id_reg_buf[2];
  163. struct shtc1_data *data;
  164. struct device *hwmon_dev;
  165. enum shtcx_chips chip = i2c_match_id(shtc1_id, client)->driver_data;
  166. struct i2c_adapter *adap = client->adapter;
  167. struct device *dev = &client->dev;
  168. struct device_node *np = dev->of_node;
  169. if (!i2c_check_functionality(adap, I2C_FUNC_I2C)) {
  170. dev_err(dev, "plain i2c transactions not supported\n");
  171. return -ENODEV;
  172. }
  173. ret = i2c_master_send(client, shtc1_cmd_read_id_reg, SHTC1_CMD_LENGTH);
  174. if (ret != SHTC1_CMD_LENGTH) {
  175. dev_err(dev, "could not send read_id_reg command: %d\n", ret);
  176. return ret < 0 ? ret : -ENODEV;
  177. }
  178. ret = i2c_master_recv(client, id_reg_buf, sizeof(id_reg_buf));
  179. if (ret != sizeof(id_reg_buf)) {
  180. dev_err(dev, "could not read ID register: %d\n", ret);
  181. return -ENODEV;
  182. }
  183. id_reg = be16_to_cpup((__be16 *)id_reg_buf);
  184. if (chip == shtc3) {
  185. if ((id_reg & SHTC3_ID_MASK) != SHTC3_ID) {
  186. dev_err(dev, "SHTC3 ID register does not match\n");
  187. return -ENODEV;
  188. }
  189. } else if ((id_reg & SHTC1_ID_MASK) != SHTC1_ID) {
  190. dev_err(dev, "SHTC1 ID register does not match\n");
  191. return -ENODEV;
  192. }
  193. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  194. if (!data)
  195. return -ENOMEM;
  196. data->setup.blocking_io = false;
  197. data->setup.high_precision = true;
  198. data->client = client;
  199. data->chip = chip;
  200. if (np) {
  201. data->setup.blocking_io = of_property_read_bool(np, "sensirion,blocking-io");
  202. data->setup.high_precision = !of_property_read_bool(np, "sensicon,low-precision");
  203. } else {
  204. if (client->dev.platform_data)
  205. data->setup = *(struct shtc1_platform_data *)dev->platform_data;
  206. }
  207. shtc1_select_command(data);
  208. mutex_init(&data->update_lock);
  209. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  210. client->name,
  211. data,
  212. shtc1_groups);
  213. if (IS_ERR(hwmon_dev))
  214. dev_dbg(dev, "unable to register hwmon device\n");
  215. return PTR_ERR_OR_ZERO(hwmon_dev);
  216. }
  217. /* device ID table */
  218. static const struct i2c_device_id shtc1_id[] = {
  219. { "shtc1", shtc1 },
  220. { "shtw1", shtc1 },
  221. { "shtc3", shtc3 },
  222. { }
  223. };
  224. MODULE_DEVICE_TABLE(i2c, shtc1_id);
  225. static const struct of_device_id shtc1_of_match[] = {
  226. { .compatible = "sensirion,shtc1" },
  227. { .compatible = "sensirion,shtw1" },
  228. { .compatible = "sensirion,shtc3" },
  229. { }
  230. };
  231. MODULE_DEVICE_TABLE(of, shtc1_of_match);
  232. static struct i2c_driver shtc1_i2c_driver = {
  233. .driver = {
  234. .name = "shtc1",
  235. .of_match_table = shtc1_of_match,
  236. },
  237. .probe_new = shtc1_probe,
  238. .id_table = shtc1_id,
  239. };
  240. module_i2c_driver(shtc1_i2c_driver);
  241. MODULE_AUTHOR("Johannes Winkelmann <johannes.winkelmann@sensirion.com>");
  242. MODULE_DESCRIPTION("Sensirion SHTC1 humidity and temperature sensor driver");
  243. MODULE_LICENSE("GPL");