veml6070.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. .driver_module = THIS_MODULE,
  117. };
  118. static int veml6070_probe(struct i2c_client *client,
  119. const struct i2c_device_id *id)
  120. {
  121. struct veml6070_data *data;
  122. struct iio_dev *indio_dev;
  123. int ret;
  124. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  125. if (!indio_dev)
  126. return -ENOMEM;
  127. data = iio_priv(indio_dev);
  128. i2c_set_clientdata(client, indio_dev);
  129. data->client1 = client;
  130. mutex_init(&data->lock);
  131. indio_dev->dev.parent = &client->dev;
  132. indio_dev->info = &veml6070_info;
  133. indio_dev->channels = veml6070_channels;
  134. indio_dev->num_channels = ARRAY_SIZE(veml6070_channels);
  135. indio_dev->name = VEML6070_DRV_NAME;
  136. indio_dev->modes = INDIO_DIRECT_MODE;
  137. data->client2 = i2c_new_dummy(client->adapter, VEML6070_ADDR_DATA_LSB);
  138. if (!data->client2) {
  139. dev_err(&client->dev, "i2c device for second chip address failed\n");
  140. return -ENODEV;
  141. }
  142. data->config = VEML6070_IT_10 | VEML6070_COMMAND_RSRVD |
  143. VEML6070_COMMAND_SD;
  144. ret = i2c_smbus_write_byte(data->client1, data->config);
  145. if (ret < 0)
  146. goto fail;
  147. ret = iio_device_register(indio_dev);
  148. if (ret < 0)
  149. goto fail;
  150. return ret;
  151. fail:
  152. i2c_unregister_device(data->client2);
  153. return ret;
  154. }
  155. static int veml6070_remove(struct i2c_client *client)
  156. {
  157. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  158. struct veml6070_data *data = iio_priv(indio_dev);
  159. iio_device_unregister(indio_dev);
  160. i2c_unregister_device(data->client2);
  161. return 0;
  162. }
  163. static const struct i2c_device_id veml6070_id[] = {
  164. { "veml6070", 0 },
  165. { }
  166. };
  167. MODULE_DEVICE_TABLE(i2c, veml6070_id);
  168. static struct i2c_driver veml6070_driver = {
  169. .driver = {
  170. .name = VEML6070_DRV_NAME,
  171. },
  172. .probe = veml6070_probe,
  173. .remove = veml6070_remove,
  174. .id_table = veml6070_id,
  175. };
  176. module_i2c_driver(veml6070_driver);
  177. MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
  178. MODULE_DESCRIPTION("Vishay VEML6070 UV A light sensor driver");
  179. MODULE_LICENSE("GPL");