vcnl3020.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Support for Vishay VCNL3020 proximity sensor on i2c bus.
  4. * Based on Vishay VCNL4000 driver code.
  5. *
  6. * TODO: interrupts.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/i2c.h>
  10. #include <linux/err.h>
  11. #include <linux/delay.h>
  12. #include <linux/regmap.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/sysfs.h>
  15. #define VCNL3020_PROD_ID 0x21
  16. #define VCNL_COMMAND 0x80 /* Command register */
  17. #define VCNL_PROD_REV 0x81 /* Product ID and Revision ID */
  18. #define VCNL_PROXIMITY_RATE 0x82 /* Rate of Proximity Measurement */
  19. #define VCNL_LED_CURRENT 0x83 /* IR LED current for proximity mode */
  20. #define VCNL_PS_RESULT_HI 0x87 /* Proximity result register, MSB */
  21. #define VCNL_PS_RESULT_LO 0x88 /* Proximity result register, LSB */
  22. #define VCNL_PS_ICR 0x89 /* Interrupt Control Register */
  23. #define VCNL_PS_LO_THR_HI 0x8a /* High byte of low threshold value */
  24. #define VCNL_PS_LO_THR_LO 0x8b /* Low byte of low threshold value */
  25. #define VCNL_PS_HI_THR_HI 0x8c /* High byte of high threshold value */
  26. #define VCNL_PS_HI_THR_LO 0x8d /* Low byte of high threshold value */
  27. #define VCNL_ISR 0x8e /* Interrupt Status Register */
  28. #define VCNL_PS_MOD_ADJ 0x8f /* Proximity Modulator Timing Adjustment */
  29. /* Bit masks for COMMAND register */
  30. #define VCNL_PS_RDY BIT(5) /* proximity data ready? */
  31. #define VCNL_PS_OD BIT(3) /* start on-demand proximity
  32. * measurement
  33. */
  34. #define VCNL_ON_DEMAND_TIMEOUT_US 100000
  35. #define VCNL_POLL_US 20000
  36. /**
  37. * struct vcnl3020_data - vcnl3020 specific data.
  38. * @regmap: device register map.
  39. * @dev: vcnl3020 device.
  40. * @rev: revision id.
  41. * @lock: lock for protecting access to device hardware registers.
  42. */
  43. struct vcnl3020_data {
  44. struct regmap *regmap;
  45. struct device *dev;
  46. u8 rev;
  47. struct mutex lock;
  48. };
  49. /**
  50. * struct vcnl3020_property - vcnl3020 property.
  51. * @name: property name.
  52. * @reg: i2c register offset.
  53. * @conversion_func: conversion function.
  54. */
  55. struct vcnl3020_property {
  56. const char *name;
  57. u32 reg;
  58. u32 (*conversion_func)(u32 *val);
  59. };
  60. static u32 microamp_to_reg(u32 *val)
  61. {
  62. /*
  63. * An example of conversion from uA to reg val:
  64. * 200000 uA == 200 mA == 20
  65. */
  66. return *val /= 10000;
  67. };
  68. static struct vcnl3020_property vcnl3020_led_current_property = {
  69. .name = "vishay,led-current-microamp",
  70. .reg = VCNL_LED_CURRENT,
  71. .conversion_func = microamp_to_reg,
  72. };
  73. static int vcnl3020_get_and_apply_property(struct vcnl3020_data *data,
  74. struct vcnl3020_property prop)
  75. {
  76. int rc;
  77. u32 val;
  78. rc = device_property_read_u32(data->dev, prop.name, &val);
  79. if (rc)
  80. return 0;
  81. if (prop.conversion_func)
  82. prop.conversion_func(&val);
  83. rc = regmap_write(data->regmap, prop.reg, val);
  84. if (rc) {
  85. dev_err(data->dev, "Error (%d) setting property (%s)\n",
  86. rc, prop.name);
  87. }
  88. return rc;
  89. }
  90. static int vcnl3020_init(struct vcnl3020_data *data)
  91. {
  92. int rc;
  93. unsigned int reg;
  94. rc = regmap_read(data->regmap, VCNL_PROD_REV, &reg);
  95. if (rc) {
  96. dev_err(data->dev,
  97. "Error (%d) reading product revision\n", rc);
  98. return rc;
  99. }
  100. if (reg != VCNL3020_PROD_ID) {
  101. dev_err(data->dev,
  102. "Product id (%x) did not match vcnl3020 (%x)\n", reg,
  103. VCNL3020_PROD_ID);
  104. return -ENODEV;
  105. }
  106. data->rev = reg;
  107. mutex_init(&data->lock);
  108. return vcnl3020_get_and_apply_property(data,
  109. vcnl3020_led_current_property);
  110. };
  111. static int vcnl3020_measure_proximity(struct vcnl3020_data *data, int *val)
  112. {
  113. int rc;
  114. unsigned int reg;
  115. __be16 res;
  116. mutex_lock(&data->lock);
  117. rc = regmap_write(data->regmap, VCNL_COMMAND, VCNL_PS_OD);
  118. if (rc)
  119. goto err_unlock;
  120. /* wait for data to become ready */
  121. rc = regmap_read_poll_timeout(data->regmap, VCNL_COMMAND, reg,
  122. reg & VCNL_PS_RDY, VCNL_POLL_US,
  123. VCNL_ON_DEMAND_TIMEOUT_US);
  124. if (rc) {
  125. dev_err(data->dev,
  126. "Error (%d) reading vcnl3020 command register\n", rc);
  127. goto err_unlock;
  128. }
  129. /* high & low result bytes read */
  130. rc = regmap_bulk_read(data->regmap, VCNL_PS_RESULT_HI, &res,
  131. sizeof(res));
  132. if (rc)
  133. goto err_unlock;
  134. *val = be16_to_cpu(res);
  135. err_unlock:
  136. mutex_unlock(&data->lock);
  137. return rc;
  138. }
  139. static const struct iio_chan_spec vcnl3020_channels[] = {
  140. {
  141. .type = IIO_PROXIMITY,
  142. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  143. },
  144. };
  145. static int vcnl3020_read_raw(struct iio_dev *indio_dev,
  146. struct iio_chan_spec const *chan, int *val,
  147. int *val2, long mask)
  148. {
  149. int rc;
  150. struct vcnl3020_data *data = iio_priv(indio_dev);
  151. switch (mask) {
  152. case IIO_CHAN_INFO_RAW:
  153. rc = vcnl3020_measure_proximity(data, val);
  154. if (rc)
  155. return rc;
  156. return IIO_VAL_INT;
  157. default:
  158. return -EINVAL;
  159. }
  160. }
  161. static const struct iio_info vcnl3020_info = {
  162. .read_raw = vcnl3020_read_raw,
  163. };
  164. static const struct regmap_config vcnl3020_regmap_config = {
  165. .reg_bits = 8,
  166. .val_bits = 8,
  167. .max_register = VCNL_PS_MOD_ADJ,
  168. };
  169. static int vcnl3020_probe(struct i2c_client *client)
  170. {
  171. struct vcnl3020_data *data;
  172. struct iio_dev *indio_dev;
  173. struct regmap *regmap;
  174. int rc;
  175. regmap = devm_regmap_init_i2c(client, &vcnl3020_regmap_config);
  176. if (IS_ERR(regmap)) {
  177. dev_err(&client->dev, "regmap_init failed\n");
  178. return PTR_ERR(regmap);
  179. }
  180. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  181. if (!indio_dev)
  182. return -ENOMEM;
  183. data = iio_priv(indio_dev);
  184. i2c_set_clientdata(client, indio_dev);
  185. data->regmap = regmap;
  186. data->dev = &client->dev;
  187. rc = vcnl3020_init(data);
  188. if (rc)
  189. return rc;
  190. indio_dev->info = &vcnl3020_info;
  191. indio_dev->channels = vcnl3020_channels;
  192. indio_dev->num_channels = ARRAY_SIZE(vcnl3020_channels);
  193. indio_dev->name = "vcnl3020";
  194. indio_dev->modes = INDIO_DIRECT_MODE;
  195. return devm_iio_device_register(&client->dev, indio_dev);
  196. }
  197. static const struct of_device_id vcnl3020_of_match[] = {
  198. {
  199. .compatible = "vishay,vcnl3020",
  200. },
  201. {}
  202. };
  203. MODULE_DEVICE_TABLE(of, vcnl3020_of_match);
  204. static struct i2c_driver vcnl3020_driver = {
  205. .driver = {
  206. .name = "vcnl3020",
  207. .of_match_table = vcnl3020_of_match,
  208. },
  209. .probe_new = vcnl3020_probe,
  210. };
  211. module_i2c_driver(vcnl3020_driver);
  212. MODULE_AUTHOR("Ivan Mikhaylov <i.mikhaylov@yadro.com>");
  213. MODULE_DESCRIPTION("Vishay VCNL3020 proximity sensor driver");
  214. MODULE_LICENSE("GPL");