vl6180.c 13 KB

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