tsys02d.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * tsys02d.c - Support for Measurement-Specialties tsys02d temperature sensor
  3. *
  4. * Copyright (c) 2015 Measurement-Specialties
  5. *
  6. * Licensed under the GPL-2.
  7. *
  8. * (7-bit I2C slave address 0x40)
  9. *
  10. * Datasheet:
  11. * http://www.meas-spec.com/downloads/Digital_Sensor_TSYS02D.pdf
  12. */
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/kernel.h>
  16. #include <linux/stat.h>
  17. #include <linux/module.h>
  18. #include <linux/iio/iio.h>
  19. #include <linux/iio/sysfs.h>
  20. #include "../common/ms_sensors/ms_sensors_i2c.h"
  21. #define TSYS02D_RESET 0xFE
  22. static const int tsys02d_samp_freq[4] = { 20, 40, 70, 140 };
  23. /* String copy of the above const for readability purpose */
  24. static const char tsys02d_show_samp_freq[] = "20 40 70 140";
  25. static int tsys02d_read_raw(struct iio_dev *indio_dev,
  26. struct iio_chan_spec const *channel, int *val,
  27. int *val2, long mask)
  28. {
  29. int ret;
  30. s32 temperature;
  31. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  32. switch (mask) {
  33. case IIO_CHAN_INFO_PROCESSED:
  34. switch (channel->type) {
  35. case IIO_TEMP: /* in milli °C */
  36. ret = ms_sensors_ht_read_temperature(dev_data,
  37. &temperature);
  38. if (ret)
  39. return ret;
  40. *val = temperature;
  41. return IIO_VAL_INT;
  42. default:
  43. return -EINVAL;
  44. }
  45. case IIO_CHAN_INFO_SAMP_FREQ:
  46. *val = tsys02d_samp_freq[dev_data->res_index];
  47. return IIO_VAL_INT;
  48. default:
  49. return -EINVAL;
  50. }
  51. }
  52. static int tsys02d_write_raw(struct iio_dev *indio_dev,
  53. struct iio_chan_spec const *chan,
  54. int val, int val2, long mask)
  55. {
  56. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  57. int i, ret;
  58. switch (mask) {
  59. case IIO_CHAN_INFO_SAMP_FREQ:
  60. i = ARRAY_SIZE(tsys02d_samp_freq);
  61. while (i-- > 0)
  62. if (val == tsys02d_samp_freq[i])
  63. break;
  64. if (i < 0)
  65. return -EINVAL;
  66. mutex_lock(&dev_data->lock);
  67. dev_data->res_index = i;
  68. ret = ms_sensors_write_resolution(dev_data, i);
  69. mutex_unlock(&dev_data->lock);
  70. return ret;
  71. default:
  72. return -EINVAL;
  73. }
  74. }
  75. static const struct iio_chan_spec tsys02d_channels[] = {
  76. {
  77. .type = IIO_TEMP,
  78. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_PROCESSED),
  79. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  80. }
  81. };
  82. static ssize_t tsys02_read_battery_low(struct device *dev,
  83. struct device_attribute *attr,
  84. char *buf)
  85. {
  86. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  87. struct ms_ht_dev *dev_data = iio_priv(indio_dev);
  88. return ms_sensors_show_battery_low(dev_data, buf);
  89. }
  90. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(tsys02d_show_samp_freq);
  91. static IIO_DEVICE_ATTR(battery_low, S_IRUGO,
  92. tsys02_read_battery_low, NULL, 0);
  93. static struct attribute *tsys02d_attributes[] = {
  94. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  95. &iio_dev_attr_battery_low.dev_attr.attr,
  96. NULL,
  97. };
  98. static const struct attribute_group tsys02d_attribute_group = {
  99. .attrs = tsys02d_attributes,
  100. };
  101. static const struct iio_info tsys02d_info = {
  102. .read_raw = tsys02d_read_raw,
  103. .write_raw = tsys02d_write_raw,
  104. .attrs = &tsys02d_attribute_group,
  105. .driver_module = THIS_MODULE,
  106. };
  107. static int tsys02d_probe(struct i2c_client *client,
  108. const struct i2c_device_id *id)
  109. {
  110. struct ms_ht_dev *dev_data;
  111. struct iio_dev *indio_dev;
  112. int ret;
  113. u64 serial_number;
  114. if (!i2c_check_functionality(client->adapter,
  115. I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
  116. I2C_FUNC_SMBUS_WRITE_BYTE |
  117. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  118. dev_err(&client->dev,
  119. "Adapter does not support some i2c transaction\n");
  120. return -EOPNOTSUPP;
  121. }
  122. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dev_data));
  123. if (!indio_dev)
  124. return -ENOMEM;
  125. dev_data = iio_priv(indio_dev);
  126. dev_data->client = client;
  127. dev_data->res_index = 0;
  128. mutex_init(&dev_data->lock);
  129. indio_dev->info = &tsys02d_info;
  130. indio_dev->name = id->name;
  131. indio_dev->dev.parent = &client->dev;
  132. indio_dev->modes = INDIO_DIRECT_MODE;
  133. indio_dev->channels = tsys02d_channels;
  134. indio_dev->num_channels = ARRAY_SIZE(tsys02d_channels);
  135. i2c_set_clientdata(client, indio_dev);
  136. ret = ms_sensors_reset(client, TSYS02D_RESET, 15000);
  137. if (ret)
  138. return ret;
  139. ret = ms_sensors_read_serial(client, &serial_number);
  140. if (ret)
  141. return ret;
  142. dev_info(&client->dev, "Serial number : %llx", serial_number);
  143. return devm_iio_device_register(&client->dev, indio_dev);
  144. }
  145. static const struct i2c_device_id tsys02d_id[] = {
  146. {"tsys02d", 0},
  147. {}
  148. };
  149. MODULE_DEVICE_TABLE(i2c, tsys02d_id);
  150. static struct i2c_driver tsys02d_driver = {
  151. .probe = tsys02d_probe,
  152. .id_table = tsys02d_id,
  153. .driver = {
  154. .name = "tsys02d",
  155. },
  156. };
  157. module_i2c_driver(tsys02d_driver);
  158. MODULE_DESCRIPTION("Measurement-Specialties tsys02d temperature driver");
  159. MODULE_AUTHOR("William Markezana <william.markezana@meas-spec.com>");
  160. MODULE_AUTHOR("Ludovic Tancerel <ludovic.tancerel@maplehightech.com>");
  161. MODULE_LICENSE("GPL v2");