vl6180.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity
  4. * sensor
  5. *
  6. * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
  7. * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
  8. *
  9. * IIO driver for VL6180 (7-bit I2C slave address 0x29)
  10. *
  11. * Range: 0 to 100mm
  12. * ALS: < 1 Lux up to 100 kLux
  13. * IR: 850nm
  14. *
  15. * TODO: irq, threshold events, continuous mode, hardware buffer
  16. */
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/i2c.h>
  20. #include <linux/mutex.h>
  21. #include <linux/err.h>
  22. #include <linux/delay.h>
  23. #include <linux/util_macros.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/sysfs.h>
  26. #define VL6180_DRV_NAME "vl6180"
  27. /* Device identification register and value */
  28. #define VL6180_MODEL_ID 0x000
  29. #define VL6180_MODEL_ID_VAL 0xb4
  30. /* Configuration registers */
  31. #define VL6180_INTR_CONFIG 0x014
  32. #define VL6180_INTR_CLEAR 0x015
  33. #define VL6180_OUT_OF_RESET 0x016
  34. #define VL6180_HOLD 0x017
  35. #define VL6180_RANGE_START 0x018
  36. #define VL6180_ALS_START 0x038
  37. #define VL6180_ALS_GAIN 0x03f
  38. #define VL6180_ALS_IT 0x040
  39. /* Status registers */
  40. #define VL6180_RANGE_STATUS 0x04d
  41. #define VL6180_ALS_STATUS 0x04e
  42. #define VL6180_INTR_STATUS 0x04f
  43. /* Result value registers */
  44. #define VL6180_ALS_VALUE 0x050
  45. #define VL6180_RANGE_VALUE 0x062
  46. #define VL6180_RANGE_RATE 0x066
  47. /* bits of the RANGE_START and ALS_START register */
  48. #define VL6180_MODE_CONT BIT(1) /* continuous mode */
  49. #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */
  50. /* bits of the INTR_STATUS and INTR_CONFIG register */
  51. #define VL6180_ALS_READY BIT(5)
  52. #define VL6180_RANGE_READY BIT(2)
  53. /* bits of the INTR_CLEAR register */
  54. #define VL6180_CLEAR_ERROR BIT(2)
  55. #define VL6180_CLEAR_ALS BIT(1)
  56. #define VL6180_CLEAR_RANGE BIT(0)
  57. /* bits of the HOLD register */
  58. #define VL6180_HOLD_ON BIT(0)
  59. /* default value for the ALS_IT register */
  60. #define VL6180_ALS_IT_100 0x63 /* 100 ms */
  61. /* values for the ALS_GAIN register */
  62. #define VL6180_ALS_GAIN_1 0x46
  63. #define VL6180_ALS_GAIN_1_25 0x45
  64. #define VL6180_ALS_GAIN_1_67 0x44
  65. #define VL6180_ALS_GAIN_2_5 0x43
  66. #define VL6180_ALS_GAIN_5 0x42
  67. #define VL6180_ALS_GAIN_10 0x41
  68. #define VL6180_ALS_GAIN_20 0x40
  69. #define VL6180_ALS_GAIN_40 0x47
  70. struct vl6180_data {
  71. struct i2c_client *client;
  72. struct mutex lock;
  73. unsigned int als_gain_milli;
  74. unsigned int als_it_ms;
  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. * Available Ambient Light Sensor gain settings, 1/1000th, and
  233. * corresponding setting for the VL6180_ALS_GAIN register
  234. */
  235. static const int vl6180_als_gain_tab[8] = {
  236. 1000, 1250, 1670, 2500, 5000, 10000, 20000, 40000
  237. };
  238. static const u8 vl6180_als_gain_tab_bits[8] = {
  239. VL6180_ALS_GAIN_1, VL6180_ALS_GAIN_1_25,
  240. VL6180_ALS_GAIN_1_67, VL6180_ALS_GAIN_2_5,
  241. VL6180_ALS_GAIN_5, VL6180_ALS_GAIN_10,
  242. VL6180_ALS_GAIN_20, VL6180_ALS_GAIN_40
  243. };
  244. static int vl6180_read_raw(struct iio_dev *indio_dev,
  245. struct iio_chan_spec const *chan,
  246. int *val, int *val2, long mask)
  247. {
  248. struct vl6180_data *data = iio_priv(indio_dev);
  249. int ret;
  250. switch (mask) {
  251. case IIO_CHAN_INFO_RAW:
  252. ret = vl6180_measure(data, chan->address);
  253. if (ret < 0)
  254. return ret;
  255. *val = ret;
  256. return IIO_VAL_INT;
  257. case IIO_CHAN_INFO_INT_TIME:
  258. *val = data->als_it_ms;
  259. *val2 = 1000;
  260. return IIO_VAL_FRACTIONAL;
  261. case IIO_CHAN_INFO_SCALE:
  262. switch (chan->type) {
  263. case IIO_LIGHT:
  264. /* one ALS count is 0.32 Lux @ gain 1, IT 100 ms */
  265. *val = 32000; /* 0.32 * 1000 * 100 */
  266. *val2 = data->als_gain_milli * data->als_it_ms;
  267. return IIO_VAL_FRACTIONAL;
  268. case IIO_DISTANCE:
  269. *val = 0; /* sensor reports mm, scale to meter */
  270. *val2 = 1000;
  271. break;
  272. default:
  273. return -EINVAL;
  274. }
  275. return IIO_VAL_INT_PLUS_MICRO;
  276. case IIO_CHAN_INFO_HARDWAREGAIN:
  277. *val = data->als_gain_milli;
  278. *val2 = 1000;
  279. return IIO_VAL_FRACTIONAL;
  280. default:
  281. return -EINVAL;
  282. }
  283. }
  284. static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40");
  285. static struct attribute *vl6180_attributes[] = {
  286. &iio_const_attr_als_gain_available.dev_attr.attr,
  287. NULL
  288. };
  289. static const struct attribute_group vl6180_attribute_group = {
  290. .attrs = vl6180_attributes,
  291. };
  292. /* HOLD is needed before updating any config registers */
  293. static int vl6180_hold(struct vl6180_data *data, bool hold)
  294. {
  295. return vl6180_write_byte(data->client, VL6180_HOLD,
  296. hold ? VL6180_HOLD_ON : 0);
  297. }
  298. static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2)
  299. {
  300. int i, ret, gain;
  301. if (val < 1 || val > 40)
  302. return -EINVAL;
  303. gain = (val * 1000000 + val2) / 1000;
  304. if (gain < 1 || gain > 40000)
  305. return -EINVAL;
  306. i = find_closest(gain, vl6180_als_gain_tab,
  307. ARRAY_SIZE(vl6180_als_gain_tab));
  308. mutex_lock(&data->lock);
  309. ret = vl6180_hold(data, true);
  310. if (ret < 0)
  311. goto fail;
  312. ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN,
  313. vl6180_als_gain_tab_bits[i]);
  314. if (ret >= 0)
  315. data->als_gain_milli = vl6180_als_gain_tab[i];
  316. fail:
  317. vl6180_hold(data, false);
  318. mutex_unlock(&data->lock);
  319. return ret;
  320. }
  321. static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
  322. {
  323. int ret, it_ms;
  324. it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */
  325. if (val != 0 || it_ms < 1 || it_ms > 512)
  326. return -EINVAL;
  327. mutex_lock(&data->lock);
  328. ret = vl6180_hold(data, true);
  329. if (ret < 0)
  330. goto fail;
  331. ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1);
  332. if (ret >= 0)
  333. data->als_it_ms = it_ms;
  334. fail:
  335. vl6180_hold(data, false);
  336. mutex_unlock(&data->lock);
  337. return ret;
  338. }
  339. static int vl6180_write_raw(struct iio_dev *indio_dev,
  340. struct iio_chan_spec const *chan,
  341. int val, int val2, long mask)
  342. {
  343. struct vl6180_data *data = iio_priv(indio_dev);
  344. switch (mask) {
  345. case IIO_CHAN_INFO_INT_TIME:
  346. return vl6180_set_it(data, val, val2);
  347. case IIO_CHAN_INFO_HARDWAREGAIN:
  348. if (chan->type != IIO_LIGHT)
  349. return -EINVAL;
  350. return vl6180_set_als_gain(data, val, val2);
  351. default:
  352. return -EINVAL;
  353. }
  354. }
  355. static const struct iio_info vl6180_info = {
  356. .read_raw = vl6180_read_raw,
  357. .write_raw = vl6180_write_raw,
  358. .attrs = &vl6180_attribute_group,
  359. };
  360. static int vl6180_init(struct vl6180_data *data)
  361. {
  362. struct i2c_client *client = data->client;
  363. int ret;
  364. ret = vl6180_read_byte(client, VL6180_MODEL_ID);
  365. if (ret < 0)
  366. return ret;
  367. if (ret != VL6180_MODEL_ID_VAL) {
  368. dev_err(&client->dev, "invalid model ID %02x\n", ret);
  369. return -ENODEV;
  370. }
  371. ret = vl6180_hold(data, true);
  372. if (ret < 0)
  373. return ret;
  374. ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET);
  375. if (ret < 0)
  376. return ret;
  377. /*
  378. * Detect false reset condition here. This bit is always set when the
  379. * system comes out of reset.
  380. */
  381. if (ret != 0x01)
  382. dev_info(&client->dev, "device is not fresh out of reset\n");
  383. /* Enable ALS and Range ready interrupts */
  384. ret = vl6180_write_byte(client, VL6180_INTR_CONFIG,
  385. VL6180_ALS_READY | VL6180_RANGE_READY);
  386. if (ret < 0)
  387. return ret;
  388. /* ALS integration time: 100ms */
  389. data->als_it_ms = 100;
  390. ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100);
  391. if (ret < 0)
  392. return ret;
  393. /* ALS gain: 1 */
  394. data->als_gain_milli = 1000;
  395. ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1);
  396. if (ret < 0)
  397. return ret;
  398. ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00);
  399. if (ret < 0)
  400. return ret;
  401. return vl6180_hold(data, false);
  402. }
  403. static int vl6180_probe(struct i2c_client *client)
  404. {
  405. struct vl6180_data *data;
  406. struct iio_dev *indio_dev;
  407. int ret;
  408. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  409. if (!indio_dev)
  410. return -ENOMEM;
  411. data = iio_priv(indio_dev);
  412. i2c_set_clientdata(client, indio_dev);
  413. data->client = client;
  414. mutex_init(&data->lock);
  415. indio_dev->info = &vl6180_info;
  416. indio_dev->channels = vl6180_channels;
  417. indio_dev->num_channels = ARRAY_SIZE(vl6180_channels);
  418. indio_dev->name = VL6180_DRV_NAME;
  419. indio_dev->modes = INDIO_DIRECT_MODE;
  420. ret = vl6180_init(data);
  421. if (ret < 0)
  422. return ret;
  423. return devm_iio_device_register(&client->dev, indio_dev);
  424. }
  425. static const struct of_device_id vl6180_of_match[] = {
  426. { .compatible = "st,vl6180", },
  427. { },
  428. };
  429. MODULE_DEVICE_TABLE(of, vl6180_of_match);
  430. static const struct i2c_device_id vl6180_id[] = {
  431. { "vl6180" },
  432. { }
  433. };
  434. MODULE_DEVICE_TABLE(i2c, vl6180_id);
  435. static struct i2c_driver vl6180_driver = {
  436. .driver = {
  437. .name = VL6180_DRV_NAME,
  438. .of_match_table = vl6180_of_match,
  439. },
  440. .probe = vl6180_probe,
  441. .id_table = vl6180_id,
  442. };
  443. module_i2c_driver(vl6180_driver);
  444. MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
  445. MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
  446. MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver");
  447. MODULE_LICENSE("GPL");