veml6070.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * veml6070.c - Support for Vishay VEML6070 UV A light sensor
  3. *
  4. * Copyright 2016 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * IIO driver for VEML6070 (7-bit I2C slave addresses 0x38 and 0x39)
  11. *
  12. * TODO: integration time, ACK signal
  13. */
  14. #include <linux/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/mutex.h>
  17. #include <linux/err.h>
  18. #include <linux/delay.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/sysfs.h>
  21. #define VEML6070_DRV_NAME "veml6070"
  22. #define VEML6070_ADDR_CONFIG_DATA_MSB 0x38 /* read: MSB data, write: config */
  23. #define VEML6070_ADDR_DATA_LSB 0x39 /* LSB data */
  24. #define VEML6070_COMMAND_ACK BIT(5) /* raise interrupt when over threshold */
  25. #define VEML6070_COMMAND_IT GENMASK(3, 2) /* bit mask integration time */
  26. #define VEML6070_COMMAND_RSRVD BIT(1) /* reserved, set to 1 */
  27. #define VEML6070_COMMAND_SD BIT(0) /* shutdown mode when set */
  28. #define VEML6070_IT_10 0x04 /* integration time 1x */
  29. struct veml6070_data {
  30. struct i2c_client *client1;
  31. struct i2c_client *client2;
  32. u8 config;
  33. struct mutex lock;
  34. };
  35. static int veml6070_read(struct veml6070_data *data)
  36. {
  37. int ret;
  38. u8 msb, lsb;
  39. mutex_lock(&data->lock);
  40. /* disable shutdown */
  41. ret = i2c_smbus_write_byte(data->client1,
  42. data->config & ~VEML6070_COMMAND_SD);
  43. if (ret < 0)
  44. goto out;
  45. msleep(125 + 10); /* measurement takes up to 125 ms for IT 1x */
  46. ret = i2c_smbus_read_byte(data->client2); /* read MSB, address 0x39 */
  47. if (ret < 0)
  48. goto out;
  49. msb = ret;
  50. ret = i2c_smbus_read_byte(data->client1); /* read LSB, address 0x38 */
  51. if (ret < 0)
  52. goto out;
  53. lsb = ret;
  54. /* shutdown again */
  55. ret = i2c_smbus_write_byte(data->client1, data->config);
  56. if (ret < 0)
  57. goto out;
  58. ret = (msb << 8) | lsb;
  59. out:
  60. mutex_unlock(&data->lock);
  61. return ret;
  62. }
  63. static const struct iio_chan_spec veml6070_channels[] = {
  64. {
  65. .type = IIO_INTENSITY,
  66. .modified = 1,
  67. .channel2 = IIO_MOD_LIGHT_UV,
  68. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  69. },
  70. {
  71. .type = IIO_UVINDEX,
  72. .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
  73. }
  74. };
  75. static int veml6070_to_uv_index(unsigned val)
  76. {
  77. /*
  78. * conversion of raw UV intensity values to UV index depends on
  79. * integration time (IT) and value of the resistor connected to
  80. * the RSET pin (default: 270 KOhm)
  81. */
  82. unsigned uvi[11] = {
  83. 187, 373, 560, /* low */
  84. 746, 933, 1120, /* moderate */
  85. 1308, 1494, /* high */
  86. 1681, 1868, 2054}; /* very high */
  87. int i;
  88. for (i = 0; i < ARRAY_SIZE(uvi); i++)
  89. if (val <= uvi[i])
  90. return i;
  91. return 11; /* extreme */
  92. }
  93. static int veml6070_read_raw(struct iio_dev *indio_dev,
  94. struct iio_chan_spec const *chan,
  95. int *val, int *val2, long mask)
  96. {
  97. struct veml6070_data *data = iio_priv(indio_dev);
  98. int ret;
  99. switch (mask) {
  100. case IIO_CHAN_INFO_RAW:
  101. case IIO_CHAN_INFO_PROCESSED:
  102. ret = veml6070_read(data);
  103. if (ret < 0)
  104. return ret;
  105. if (mask == IIO_CHAN_INFO_PROCESSED)
  106. *val = veml6070_to_uv_index(ret);
  107. else
  108. *val = ret;
  109. return IIO_VAL_INT;
  110. default:
  111. return -EINVAL;
  112. }
  113. }
  114. static const struct iio_info veml6070_info = {
  115. .read_raw = veml6070_read_raw,
  116. };
  117. static int veml6070_probe(struct i2c_client *client,
  118. const struct i2c_device_id *id)
  119. {
  120. struct veml6070_data *data;
  121. struct iio_dev *indio_dev;
  122. int ret;
  123. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  124. if (!indio_dev)
  125. return -ENOMEM;
  126. data = iio_priv(indio_dev);
  127. i2c_set_clientdata(client, indio_dev);
  128. data->client1 = client;
  129. mutex_init(&data->lock);
  130. indio_dev->dev.parent = &client->dev;
  131. indio_dev->info = &veml6070_info;
  132. indio_dev->channels = veml6070_channels;
  133. indio_dev->num_channels = ARRAY_SIZE(veml6070_channels);
  134. indio_dev->name = VEML6070_DRV_NAME;
  135. indio_dev->modes = INDIO_DIRECT_MODE;
  136. data->client2 = i2c_new_dummy(client->adapter, VEML6070_ADDR_DATA_LSB);
  137. if (!data->client2) {
  138. dev_err(&client->dev, "i2c device for second chip address failed\n");
  139. return -ENODEV;
  140. }
  141. data->config = VEML6070_IT_10 | VEML6070_COMMAND_RSRVD |
  142. VEML6070_COMMAND_SD;
  143. ret = i2c_smbus_write_byte(data->client1, data->config);
  144. if (ret < 0)
  145. goto fail;
  146. ret = iio_device_register(indio_dev);
  147. if (ret < 0)
  148. goto fail;
  149. return ret;
  150. fail:
  151. i2c_unregister_device(data->client2);
  152. return ret;
  153. }
  154. static int veml6070_remove(struct i2c_client *client)
  155. {
  156. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  157. struct veml6070_data *data = iio_priv(indio_dev);
  158. iio_device_unregister(indio_dev);
  159. i2c_unregister_device(data->client2);
  160. return 0;
  161. }
  162. static const struct i2c_device_id veml6070_id[] = {
  163. { "veml6070", 0 },
  164. { }
  165. };
  166. MODULE_DEVICE_TABLE(i2c, veml6070_id);
  167. static struct i2c_driver veml6070_driver = {
  168. .driver = {
  169. .name = VEML6070_DRV_NAME,
  170. },
  171. .probe = veml6070_probe,
  172. .remove = veml6070_remove,
  173. .id_table = veml6070_id,
  174. };
  175. module_i2c_driver(veml6070_driver);
  176. MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
  177. MODULE_DESCRIPTION("Vishay VEML6070 UV A light sensor driver");
  178. MODULE_LICENSE("GPL");