vcnl4000.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * vcnl4000.c - Support for Vishay VCNL4000 combined ambient light and
  3. * proximity sensor
  4. *
  5. * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
  6. *
  7. * This file is subject to the terms and conditions of version 2 of
  8. * the GNU General Public License. See the file COPYING in the main
  9. * directory of this archive for more details.
  10. *
  11. * IIO driver for VCNL4000 (7-bit I2C slave address 0x13)
  12. *
  13. * TODO:
  14. * allow to adjust IR current
  15. * proximity threshold and event handling
  16. */
  17. #include <linux/module.h>
  18. #include <linux/i2c.h>
  19. #include <linux/err.h>
  20. #include <linux/delay.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #define VCNL4000_DRV_NAME "vcnl4000"
  24. #define VCNL4000_COMMAND 0x80 /* Command register */
  25. #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */
  26. #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */
  27. #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */
  28. #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */
  29. #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */
  30. #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
  31. #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
  32. #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */
  33. #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */
  34. /* Bit masks for COMMAND register */
  35. #define VCNL4000_AL_RDY 0x40 /* ALS data ready? */
  36. #define VCNL4000_PS_RDY 0x20 /* proximity data ready? */
  37. #define VCNL4000_AL_OD 0x10 /* start on-demand ALS measurement */
  38. #define VCNL4000_PS_OD 0x08 /* start on-demand proximity measurement */
  39. struct vcnl4000_data {
  40. struct i2c_client *client;
  41. };
  42. static const struct i2c_device_id vcnl4000_id[] = {
  43. { "vcnl4000", 0 },
  44. { }
  45. };
  46. MODULE_DEVICE_TABLE(i2c, vcnl4000_id);
  47. static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
  48. u8 rdy_mask, u8 data_reg, int *val)
  49. {
  50. int tries = 20;
  51. u16 buf;
  52. int ret;
  53. ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
  54. req_mask);
  55. if (ret < 0)
  56. return ret;
  57. /* wait for data to become ready */
  58. while (tries--) {
  59. ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
  60. if (ret < 0)
  61. return ret;
  62. if (ret & rdy_mask)
  63. break;
  64. msleep(20); /* measurement takes up to 100 ms */
  65. }
  66. if (tries < 0) {
  67. dev_err(&data->client->dev,
  68. "vcnl4000_measure() failed, data not ready\n");
  69. return -EIO;
  70. }
  71. ret = i2c_smbus_read_i2c_block_data(data->client,
  72. data_reg, sizeof(buf), (u8 *) &buf);
  73. if (ret < 0)
  74. return ret;
  75. *val = be16_to_cpu(buf);
  76. return 0;
  77. }
  78. static const struct iio_chan_spec vcnl4000_channels[] = {
  79. {
  80. .type = IIO_LIGHT,
  81. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |
  82. IIO_CHAN_INFO_SCALE_SEPARATE_BIT,
  83. }, {
  84. .type = IIO_PROXIMITY,
  85. .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT,
  86. }
  87. };
  88. static int vcnl4000_read_raw(struct iio_dev *indio_dev,
  89. struct iio_chan_spec const *chan,
  90. int *val, int *val2, long mask)
  91. {
  92. int ret = -EINVAL;
  93. struct vcnl4000_data *data = iio_priv(indio_dev);
  94. switch (mask) {
  95. case IIO_CHAN_INFO_RAW:
  96. switch (chan->type) {
  97. case IIO_LIGHT:
  98. ret = vcnl4000_measure(data,
  99. VCNL4000_AL_OD, VCNL4000_AL_RDY,
  100. VCNL4000_AL_RESULT_HI, val);
  101. if (ret < 0)
  102. return ret;
  103. ret = IIO_VAL_INT;
  104. break;
  105. case IIO_PROXIMITY:
  106. ret = vcnl4000_measure(data,
  107. VCNL4000_PS_OD, VCNL4000_PS_RDY,
  108. VCNL4000_PS_RESULT_HI, val);
  109. if (ret < 0)
  110. return ret;
  111. ret = IIO_VAL_INT;
  112. break;
  113. default:
  114. break;
  115. }
  116. break;
  117. case IIO_CHAN_INFO_SCALE:
  118. if (chan->type == IIO_LIGHT) {
  119. *val = 0;
  120. *val2 = 250000;
  121. ret = IIO_VAL_INT_PLUS_MICRO;
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. return ret;
  128. }
  129. static const struct iio_info vcnl4000_info = {
  130. .read_raw = vcnl4000_read_raw,
  131. .driver_module = THIS_MODULE,
  132. };
  133. static int vcnl4000_probe(struct i2c_client *client,
  134. const struct i2c_device_id *id)
  135. {
  136. struct vcnl4000_data *data;
  137. struct iio_dev *indio_dev;
  138. int ret;
  139. indio_dev = iio_device_alloc(sizeof(*data));
  140. if (!indio_dev)
  141. return -ENOMEM;
  142. data = iio_priv(indio_dev);
  143. i2c_set_clientdata(client, indio_dev);
  144. data->client = client;
  145. ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
  146. if (ret < 0)
  147. goto error_free_dev;
  148. dev_info(&client->dev, "VCNL4000 Ambient light/proximity sensor, Prod %02x, Rev: %02x\n",
  149. ret >> 4, ret & 0xf);
  150. indio_dev->dev.parent = &client->dev;
  151. indio_dev->info = &vcnl4000_info;
  152. indio_dev->channels = vcnl4000_channels;
  153. indio_dev->num_channels = ARRAY_SIZE(vcnl4000_channels);
  154. indio_dev->name = VCNL4000_DRV_NAME;
  155. indio_dev->modes = INDIO_DIRECT_MODE;
  156. ret = iio_device_register(indio_dev);
  157. if (ret < 0)
  158. goto error_free_dev;
  159. return 0;
  160. error_free_dev:
  161. iio_device_free(indio_dev);
  162. return ret;
  163. }
  164. static int vcnl4000_remove(struct i2c_client *client)
  165. {
  166. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  167. iio_device_unregister(indio_dev);
  168. iio_device_free(indio_dev);
  169. return 0;
  170. }
  171. static struct i2c_driver vcnl4000_driver = {
  172. .driver = {
  173. .name = VCNL4000_DRV_NAME,
  174. .owner = THIS_MODULE,
  175. },
  176. .probe = vcnl4000_probe,
  177. .remove = vcnl4000_remove,
  178. .id_table = vcnl4000_id,
  179. };
  180. module_i2c_driver(vcnl4000_driver);
  181. MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>");
  182. MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver");
  183. MODULE_LICENSE("GPL");