vl6180.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity
  3. * sensor
  4. *
  5. * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
  6. * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
  7. *
  8. * This file is subject to the terms and conditions of version 2 of
  9. * the GNU General Public License. See the file COPYING in the main
  10. * directory of this archive for more details.
  11. *
  12. * IIO driver for VL6180 (7-bit I2C slave address 0x29)
  13. *
  14. * Range: 0 to 100mm
  15. * ALS: < 1 Lux up to 100 kLux
  16. * IR: 850nm
  17. *
  18. * TODO: irq, threshold events, continuous mode, hardware buffer
  19. */
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/mutex.h>
  23. #include <linux/err.h>
  24. #include <linux/of.h>
  25. #include <linux/delay.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/sysfs.h>
  28. #define VL6180_DRV_NAME "vl6180"
  29. /* Device identification register and value */
  30. #define VL6180_MODEL_ID 0x000
  31. #define VL6180_MODEL_ID_VAL 0xb4
  32. /* Configuration registers */
  33. #define VL6180_INTR_CONFIG 0x014
  34. #define VL6180_INTR_CLEAR 0x015
  35. #define VL6180_OUT_OF_RESET 0x016
  36. #define VL6180_HOLD 0x017
  37. #define VL6180_RANGE_START 0x018
  38. #define VL6180_ALS_START 0x038
  39. #define VL6180_ALS_GAIN 0x03f
  40. #define VL6180_ALS_IT 0x040
  41. /* Status registers */
  42. #define VL6180_RANGE_STATUS 0x04d
  43. #define VL6180_ALS_STATUS 0x04e
  44. #define VL6180_INTR_STATUS 0x04f
  45. /* Result value registers */
  46. #define VL6180_ALS_VALUE 0x050
  47. #define VL6180_RANGE_VALUE 0x062
  48. #define VL6180_RANGE_RATE 0x066
  49. /* bits of the RANGE_START and ALS_START register */
  50. #define VL6180_MODE_CONT BIT(1) /* continuous mode */
  51. #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */
  52. /* bits of the INTR_STATUS and INTR_CONFIG register */
  53. #define VL6180_ALS_READY BIT(5)
  54. #define VL6180_RANGE_READY BIT(2)
  55. /* bits of the INTR_CLEAR register */
  56. #define VL6180_CLEAR_ERROR BIT(2)
  57. #define VL6180_CLEAR_ALS BIT(1)
  58. #define VL6180_CLEAR_RANGE BIT(0)
  59. /* bits of the HOLD register */
  60. #define VL6180_HOLD_ON BIT(0)
  61. /* default value for the ALS_IT register */
  62. #define VL6180_ALS_IT_100 0x63 /* 100 ms */
  63. /* values for the ALS_GAIN register */
  64. #define VL6180_ALS_GAIN_1 0x46
  65. #define VL6180_ALS_GAIN_1_25 0x45
  66. #define VL6180_ALS_GAIN_1_67 0x44
  67. #define VL6180_ALS_GAIN_2_5 0x43
  68. #define VL6180_ALS_GAIN_5 0x42
  69. #define VL6180_ALS_GAIN_10 0x41
  70. #define VL6180_ALS_GAIN_20 0x40
  71. #define VL6180_ALS_GAIN_40 0x47
  72. struct vl6180_data {
  73. struct i2c_client *client;
  74. struct mutex lock;
  75. };
  76. enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX };
  77. /**
  78. * struct vl6180_chan_regs - Registers for accessing channels
  79. * @drdy_mask: Data ready bit in status register
  80. * @start_reg: Conversion start register
  81. * @value_reg: Result value register
  82. * @word: Register word length
  83. */
  84. struct vl6180_chan_regs {
  85. u8 drdy_mask;
  86. u16 start_reg, value_reg;
  87. bool word;
  88. };
  89. static const struct vl6180_chan_regs vl6180_chan_regs_table[] = {
  90. [VL6180_ALS] = {
  91. .drdy_mask = VL6180_ALS_READY,
  92. .start_reg = VL6180_ALS_START,
  93. .value_reg = VL6180_ALS_VALUE,
  94. .word = true,
  95. },
  96. [VL6180_RANGE] = {
  97. .drdy_mask = VL6180_RANGE_READY,
  98. .start_reg = VL6180_RANGE_START,
  99. .value_reg = VL6180_RANGE_VALUE,
  100. .word = false,
  101. },
  102. [VL6180_PROX] = {
  103. .drdy_mask = VL6180_RANGE_READY,
  104. .start_reg = VL6180_RANGE_START,
  105. .value_reg = VL6180_RANGE_RATE,
  106. .word = true,
  107. },
  108. };
  109. static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf,
  110. u8 len)
  111. {
  112. __be16 cmdbuf = cpu_to_be16(cmd);
  113. struct i2c_msg msgs[2] = {
  114. { .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf },
  115. { .addr = client->addr, .len = len, .buf = databuf,
  116. .flags = I2C_M_RD } };
  117. int ret;
  118. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  119. if (ret < 0)
  120. dev_err(&client->dev, "failed reading register 0x%04x\n", cmd);
  121. return ret;
  122. }
  123. static int vl6180_read_byte(struct i2c_client *client, u16 cmd)
  124. {
  125. u8 data;
  126. int ret;
  127. ret = vl6180_read(client, cmd, &data, sizeof(data));
  128. if (ret < 0)
  129. return ret;
  130. return data;
  131. }
  132. static int vl6180_read_word(struct i2c_client *client, u16 cmd)
  133. {
  134. __be16 data;
  135. int ret;
  136. ret = vl6180_read(client, cmd, &data, sizeof(data));
  137. if (ret < 0)
  138. return ret;
  139. return be16_to_cpu(data);
  140. }
  141. static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val)
  142. {
  143. u8 buf[3];
  144. struct i2c_msg msgs[1] = {
  145. { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
  146. int ret;
  147. buf[0] = cmd >> 8;
  148. buf[1] = cmd & 0xff;
  149. buf[2] = val;
  150. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  151. if (ret < 0) {
  152. dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val)
  158. {
  159. __be16 buf[2];
  160. struct i2c_msg msgs[1] = {
  161. { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
  162. int ret;
  163. buf[0] = cpu_to_be16(cmd);
  164. buf[1] = cpu_to_be16(val);
  165. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  166. if (ret < 0) {
  167. dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
  168. return ret;
  169. }
  170. return 0;
  171. }
  172. static int vl6180_measure(struct vl6180_data *data, int addr)
  173. {
  174. struct i2c_client *client = data->client;
  175. int tries = 20, ret;
  176. u16 value;
  177. mutex_lock(&data->lock);
  178. /* Start single shot measurement */
  179. ret = vl6180_write_byte(client,
  180. vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP);
  181. if (ret < 0)
  182. goto fail;
  183. while (tries--) {
  184. ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
  185. if (ret < 0)
  186. goto fail;
  187. if (ret & vl6180_chan_regs_table[addr].drdy_mask)
  188. break;
  189. msleep(20);
  190. }
  191. if (tries < 0) {
  192. ret = -EIO;
  193. goto fail;
  194. }
  195. /* Read result value from appropriate registers */
  196. ret = vl6180_chan_regs_table[addr].word ?
  197. vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) :
  198. vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg);
  199. if (ret < 0)
  200. goto fail;
  201. value = ret;
  202. /* Clear the interrupt flag after data read */
  203. ret = vl6180_write_byte(client, VL6180_INTR_CLEAR,
  204. VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE);
  205. if (ret < 0)
  206. goto fail;
  207. ret = value;
  208. fail:
  209. mutex_unlock(&data->lock);
  210. return ret;
  211. }
  212. static const struct iio_chan_spec vl6180_channels[] = {
  213. {
  214. .type = IIO_LIGHT,
  215. .address = VL6180_ALS,
  216. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  217. BIT(IIO_CHAN_INFO_INT_TIME) |
  218. BIT(IIO_CHAN_INFO_SCALE) |
  219. BIT(IIO_CHAN_INFO_HARDWAREGAIN),
  220. }, {
  221. .type = IIO_DISTANCE,
  222. .address = VL6180_RANGE,
  223. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  224. BIT(IIO_CHAN_INFO_SCALE),
  225. }, {
  226. .type = IIO_PROXIMITY,
  227. .address = VL6180_PROX,
  228. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  229. }
  230. };
  231. /*
  232. * Columns 3 & 4 represent the same value in decimal and hex notations.
  233. * Kept in order to avoid the datatype conversion while reading the
  234. * hardware_gain.
  235. */
  236. static const int vl6180_als_gain[8][4] = {
  237. { 1, 0, 70, VL6180_ALS_GAIN_1 },
  238. { 1, 250000, 69, VL6180_ALS_GAIN_1_25 },
  239. { 1, 670000, 68, VL6180_ALS_GAIN_1_67 },
  240. { 2, 500000, 67, VL6180_ALS_GAIN_2_5 },
  241. { 5, 0, 66, VL6180_ALS_GAIN_5 },
  242. { 10, 0, 65, VL6180_ALS_GAIN_10 },
  243. { 20, 0, 64, VL6180_ALS_GAIN_20 },
  244. { 40, 0, 71, VL6180_ALS_GAIN_40 }
  245. };
  246. static int vl6180_read_raw(struct iio_dev *indio_dev,
  247. struct iio_chan_spec const *chan,
  248. int *val, int *val2, long mask)
  249. {
  250. struct vl6180_data *data = iio_priv(indio_dev);
  251. int ret, i;
  252. switch (mask) {
  253. case IIO_CHAN_INFO_RAW:
  254. ret = vl6180_measure(data, chan->address);
  255. if (ret < 0)
  256. return ret;
  257. *val = ret;
  258. return IIO_VAL_INT;
  259. case IIO_CHAN_INFO_INT_TIME:
  260. ret = vl6180_read_word(data->client, VL6180_ALS_IT);
  261. if (ret < 0)
  262. return ret;
  263. *val = 0; /* 1 count = 1ms (0 = 1ms) */
  264. *val2 = (ret + 1) * 1000; /* convert to seconds */
  265. return IIO_VAL_INT_PLUS_MICRO;
  266. case IIO_CHAN_INFO_SCALE:
  267. switch (chan->type) {
  268. case IIO_LIGHT:
  269. *val = 0; /* one ALS count is 0.32 Lux */
  270. *val2 = 320000;
  271. break;
  272. case IIO_DISTANCE:
  273. *val = 0; /* sensor reports mm, scale to meter */
  274. *val2 = 1000;
  275. break;
  276. default:
  277. return -EINVAL;
  278. }
  279. return IIO_VAL_INT_PLUS_MICRO;
  280. case IIO_CHAN_INFO_HARDWAREGAIN:
  281. ret = vl6180_read_byte(data->client, VL6180_ALS_GAIN);
  282. if (ret < 0)
  283. return -EINVAL;
  284. for (i = 0; i < ARRAY_SIZE(vl6180_als_gain); i++) {
  285. if (ret == vl6180_als_gain[i][2]) {
  286. *val = vl6180_als_gain[i][0];
  287. *val2 = vl6180_als_gain[i][1];
  288. }
  289. }
  290. return IIO_VAL_INT_PLUS_MICRO;
  291. default:
  292. return -EINVAL;
  293. }
  294. }
  295. static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40");
  296. static struct attribute *vl6180_attributes[] = {
  297. &iio_const_attr_als_gain_available.dev_attr.attr,
  298. NULL
  299. };
  300. static const struct attribute_group vl6180_attribute_group = {
  301. .attrs = vl6180_attributes,
  302. };
  303. /* HOLD is needed before updating any config registers */
  304. static int vl6180_hold(struct vl6180_data *data, bool hold)
  305. {
  306. return vl6180_write_byte(data->client, VL6180_HOLD,
  307. hold ? VL6180_HOLD_ON : 0);
  308. }
  309. static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2)
  310. {
  311. int i, ret;
  312. for (i = 0; i < ARRAY_SIZE(vl6180_als_gain); i++) {
  313. if (val == vl6180_als_gain[i][0] &&
  314. val2 == vl6180_als_gain[i][1]) {
  315. mutex_lock(&data->lock);
  316. ret = vl6180_hold(data, true);
  317. if (ret < 0)
  318. goto fail;
  319. ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN,
  320. vl6180_als_gain[i][3]);
  321. fail:
  322. vl6180_hold(data, false);
  323. mutex_unlock(&data->lock);
  324. return ret;
  325. }
  326. }
  327. return -EINVAL;
  328. }
  329. static int vl6180_set_it(struct vl6180_data *data, int val2)
  330. {
  331. int ret;
  332. mutex_lock(&data->lock);
  333. ret = vl6180_hold(data, true);
  334. if (ret < 0)
  335. goto fail;
  336. ret = vl6180_write_word(data->client, VL6180_ALS_IT,
  337. (val2 - 500) / 1000); /* write value in ms */
  338. fail:
  339. vl6180_hold(data, false);
  340. mutex_unlock(&data->lock);
  341. return ret;
  342. }
  343. static int vl6180_write_raw(struct iio_dev *indio_dev,
  344. struct iio_chan_spec const *chan,
  345. int val, int val2, long mask)
  346. {
  347. struct vl6180_data *data = iio_priv(indio_dev);
  348. switch (mask) {
  349. case IIO_CHAN_INFO_INT_TIME:
  350. if (val != 0 || val2 < 500 || val2 >= 512500)
  351. return -EINVAL;
  352. return vl6180_set_it(data, val2);
  353. case IIO_CHAN_INFO_HARDWAREGAIN:
  354. if (chan->type != IIO_LIGHT)
  355. return -EINVAL;
  356. return vl6180_set_als_gain(data, val, val2);
  357. default:
  358. return -EINVAL;
  359. }
  360. }
  361. static const struct iio_info vl6180_info = {
  362. .read_raw = vl6180_read_raw,
  363. .write_raw = vl6180_write_raw,
  364. .attrs = &vl6180_attribute_group,
  365. .driver_module = THIS_MODULE,
  366. };
  367. static int vl6180_init(struct vl6180_data *data)
  368. {
  369. struct i2c_client *client = data->client;
  370. int ret;
  371. ret = vl6180_read_byte(client, VL6180_MODEL_ID);
  372. if (ret < 0)
  373. return ret;
  374. if (ret != VL6180_MODEL_ID_VAL) {
  375. dev_err(&client->dev, "invalid model ID %02x\n", ret);
  376. return -ENODEV;
  377. }
  378. ret = vl6180_hold(data, true);
  379. if (ret < 0)
  380. return ret;
  381. ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET);
  382. if (ret < 0)
  383. return ret;
  384. /*
  385. * Detect false reset condition here. This bit is always set when the
  386. * system comes out of reset.
  387. */
  388. if (ret != 0x01)
  389. dev_info(&client->dev, "device is not fresh out of reset\n");
  390. /* Enable ALS and Range ready interrupts */
  391. ret = vl6180_write_byte(client, VL6180_INTR_CONFIG,
  392. VL6180_ALS_READY | VL6180_RANGE_READY);
  393. if (ret < 0)
  394. return ret;
  395. /* ALS integration time: 100ms */
  396. ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100);
  397. if (ret < 0)
  398. return ret;
  399. /* ALS gain: 1 */
  400. ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1);
  401. if (ret < 0)
  402. return ret;
  403. ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00);
  404. if (ret < 0)
  405. return ret;
  406. return vl6180_hold(data, false);
  407. }
  408. static int vl6180_probe(struct i2c_client *client,
  409. const struct i2c_device_id *id)
  410. {
  411. struct vl6180_data *data;
  412. struct iio_dev *indio_dev;
  413. int ret;
  414. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  415. if (!indio_dev)
  416. return -ENOMEM;
  417. data = iio_priv(indio_dev);
  418. i2c_set_clientdata(client, indio_dev);
  419. data->client = client;
  420. mutex_init(&data->lock);
  421. indio_dev->dev.parent = &client->dev;
  422. indio_dev->info = &vl6180_info;
  423. indio_dev->channels = vl6180_channels;
  424. indio_dev->num_channels = ARRAY_SIZE(vl6180_channels);
  425. indio_dev->name = VL6180_DRV_NAME;
  426. indio_dev->modes = INDIO_DIRECT_MODE;
  427. ret = vl6180_init(data);
  428. if (ret < 0)
  429. return ret;
  430. return devm_iio_device_register(&client->dev, indio_dev);
  431. }
  432. static const struct of_device_id vl6180_of_match[] = {
  433. { .compatible = "st,vl6180", },
  434. { },
  435. };
  436. MODULE_DEVICE_TABLE(of, vl6180_of_match);
  437. static const struct i2c_device_id vl6180_id[] = {
  438. { "vl6180", 0 },
  439. { }
  440. };
  441. MODULE_DEVICE_TABLE(i2c, vl6180_id);
  442. static struct i2c_driver vl6180_driver = {
  443. .driver = {
  444. .name = VL6180_DRV_NAME,
  445. .of_match_table = of_match_ptr(vl6180_of_match),
  446. },
  447. .probe = vl6180_probe,
  448. .id_table = vl6180_id,
  449. };
  450. module_i2c_driver(vl6180_driver);
  451. MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
  452. MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
  453. MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver");
  454. MODULE_LICENSE("GPL");