xref: /linux/drivers/iio/light/vl6180.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
136edc939SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25e7f47e4SManivannan Sadhasivam /*
35e7f47e4SManivannan Sadhasivam  * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity
45e7f47e4SManivannan Sadhasivam  * sensor
55e7f47e4SManivannan Sadhasivam  *
65e7f47e4SManivannan Sadhasivam  * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
75e7f47e4SManivannan Sadhasivam  * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
85e7f47e4SManivannan Sadhasivam  *
95e7f47e4SManivannan Sadhasivam  * IIO driver for VL6180 (7-bit I2C slave address 0x29)
105e7f47e4SManivannan Sadhasivam  *
115e7f47e4SManivannan Sadhasivam  * Range: 0 to 100mm
125e7f47e4SManivannan Sadhasivam  * ALS: < 1 Lux up to 100 kLux
135e7f47e4SManivannan Sadhasivam  * IR: 850nm
145e7f47e4SManivannan Sadhasivam  *
155e7f47e4SManivannan Sadhasivam  * TODO: irq, threshold events, continuous mode, hardware buffer
165e7f47e4SManivannan Sadhasivam  */
175e7f47e4SManivannan Sadhasivam 
185e7f47e4SManivannan Sadhasivam #include <linux/module.h>
1977baa8d6SJonathan Cameron #include <linux/mod_devicetable.h>
205e7f47e4SManivannan Sadhasivam #include <linux/i2c.h>
215e7f47e4SManivannan Sadhasivam #include <linux/mutex.h>
225e7f47e4SManivannan Sadhasivam #include <linux/err.h>
235e7f47e4SManivannan Sadhasivam #include <linux/delay.h>
24e794bf67SStefan Brüns #include <linux/util_macros.h>
255e7f47e4SManivannan Sadhasivam 
265e7f47e4SManivannan Sadhasivam #include <linux/iio/iio.h>
275e7f47e4SManivannan Sadhasivam #include <linux/iio/sysfs.h>
285e7f47e4SManivannan Sadhasivam 
295e7f47e4SManivannan Sadhasivam #define VL6180_DRV_NAME "vl6180"
305e7f47e4SManivannan Sadhasivam 
315e7f47e4SManivannan Sadhasivam /* Device identification register and value */
325e7f47e4SManivannan Sadhasivam #define VL6180_MODEL_ID	0x000
335e7f47e4SManivannan Sadhasivam #define VL6180_MODEL_ID_VAL 0xb4
345e7f47e4SManivannan Sadhasivam 
355e7f47e4SManivannan Sadhasivam /* Configuration registers */
365e7f47e4SManivannan Sadhasivam #define VL6180_INTR_CONFIG 0x014
375e7f47e4SManivannan Sadhasivam #define VL6180_INTR_CLEAR 0x015
385e7f47e4SManivannan Sadhasivam #define VL6180_OUT_OF_RESET 0x016
395e7f47e4SManivannan Sadhasivam #define VL6180_HOLD 0x017
405e7f47e4SManivannan Sadhasivam #define VL6180_RANGE_START 0x018
415e7f47e4SManivannan Sadhasivam #define VL6180_ALS_START 0x038
425e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN 0x03f
435e7f47e4SManivannan Sadhasivam #define VL6180_ALS_IT 0x040
445e7f47e4SManivannan Sadhasivam 
455e7f47e4SManivannan Sadhasivam /* Status registers */
465e7f47e4SManivannan Sadhasivam #define VL6180_RANGE_STATUS 0x04d
475e7f47e4SManivannan Sadhasivam #define VL6180_ALS_STATUS 0x04e
485e7f47e4SManivannan Sadhasivam #define VL6180_INTR_STATUS 0x04f
495e7f47e4SManivannan Sadhasivam 
505e7f47e4SManivannan Sadhasivam /* Result value registers */
515e7f47e4SManivannan Sadhasivam #define VL6180_ALS_VALUE 0x050
525e7f47e4SManivannan Sadhasivam #define VL6180_RANGE_VALUE 0x062
535e7f47e4SManivannan Sadhasivam #define VL6180_RANGE_RATE 0x066
545e7f47e4SManivannan Sadhasivam 
555e7f47e4SManivannan Sadhasivam /* bits of the RANGE_START and ALS_START register */
565e7f47e4SManivannan Sadhasivam #define VL6180_MODE_CONT BIT(1) /* continuous mode */
575e7f47e4SManivannan Sadhasivam #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */
585e7f47e4SManivannan Sadhasivam 
595e7f47e4SManivannan Sadhasivam /* bits of the INTR_STATUS and INTR_CONFIG register */
605e7f47e4SManivannan Sadhasivam #define VL6180_ALS_READY BIT(5)
615e7f47e4SManivannan Sadhasivam #define VL6180_RANGE_READY BIT(2)
625e7f47e4SManivannan Sadhasivam 
635e7f47e4SManivannan Sadhasivam /* bits of the INTR_CLEAR register */
645e7f47e4SManivannan Sadhasivam #define VL6180_CLEAR_ERROR BIT(2)
655e7f47e4SManivannan Sadhasivam #define VL6180_CLEAR_ALS BIT(1)
665e7f47e4SManivannan Sadhasivam #define VL6180_CLEAR_RANGE BIT(0)
675e7f47e4SManivannan Sadhasivam 
685e7f47e4SManivannan Sadhasivam /* bits of the HOLD register */
695e7f47e4SManivannan Sadhasivam #define VL6180_HOLD_ON BIT(0)
705e7f47e4SManivannan Sadhasivam 
715e7f47e4SManivannan Sadhasivam /* default value for the ALS_IT register */
725e7f47e4SManivannan Sadhasivam #define VL6180_ALS_IT_100 0x63 /* 100 ms */
735e7f47e4SManivannan Sadhasivam 
745e7f47e4SManivannan Sadhasivam /* values for the ALS_GAIN register */
755e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_1 0x46
765e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_1_25 0x45
775e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_1_67 0x44
785e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_2_5 0x43
795e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_5 0x42
805e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_10 0x41
815e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_20 0x40
825e7f47e4SManivannan Sadhasivam #define VL6180_ALS_GAIN_40 0x47
835e7f47e4SManivannan Sadhasivam 
845e7f47e4SManivannan Sadhasivam struct vl6180_data {
855e7f47e4SManivannan Sadhasivam 	struct i2c_client *client;
865e7f47e4SManivannan Sadhasivam 	struct mutex lock;
87e794bf67SStefan Brüns 	unsigned int als_gain_milli;
88df698c04SStefan Brüns 	unsigned int als_it_ms;
895e7f47e4SManivannan Sadhasivam };
905e7f47e4SManivannan Sadhasivam 
915e7f47e4SManivannan Sadhasivam enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX };
925e7f47e4SManivannan Sadhasivam 
935e7f47e4SManivannan Sadhasivam /**
945e7f47e4SManivannan Sadhasivam  * struct vl6180_chan_regs - Registers for accessing channels
955e7f47e4SManivannan Sadhasivam  * @drdy_mask:			Data ready bit in status register
965e7f47e4SManivannan Sadhasivam  * @start_reg:			Conversion start register
975e7f47e4SManivannan Sadhasivam  * @value_reg:			Result value register
985e7f47e4SManivannan Sadhasivam  * @word:			Register word length
995e7f47e4SManivannan Sadhasivam  */
1005e7f47e4SManivannan Sadhasivam struct vl6180_chan_regs {
1015e7f47e4SManivannan Sadhasivam 	u8 drdy_mask;
1025e7f47e4SManivannan Sadhasivam 	u16 start_reg, value_reg;
1035e7f47e4SManivannan Sadhasivam 	bool word;
1045e7f47e4SManivannan Sadhasivam };
1055e7f47e4SManivannan Sadhasivam 
1065e7f47e4SManivannan Sadhasivam static const struct vl6180_chan_regs vl6180_chan_regs_table[] = {
1075e7f47e4SManivannan Sadhasivam 	[VL6180_ALS] = {
1085e7f47e4SManivannan Sadhasivam 		.drdy_mask = VL6180_ALS_READY,
1095e7f47e4SManivannan Sadhasivam 		.start_reg = VL6180_ALS_START,
1105e7f47e4SManivannan Sadhasivam 		.value_reg = VL6180_ALS_VALUE,
1115e7f47e4SManivannan Sadhasivam 		.word = true,
1125e7f47e4SManivannan Sadhasivam 	},
1135e7f47e4SManivannan Sadhasivam 	[VL6180_RANGE] = {
1145e7f47e4SManivannan Sadhasivam 		.drdy_mask = VL6180_RANGE_READY,
1155e7f47e4SManivannan Sadhasivam 		.start_reg = VL6180_RANGE_START,
1165e7f47e4SManivannan Sadhasivam 		.value_reg = VL6180_RANGE_VALUE,
1175e7f47e4SManivannan Sadhasivam 		.word = false,
1185e7f47e4SManivannan Sadhasivam 	},
1195e7f47e4SManivannan Sadhasivam 	[VL6180_PROX] = {
1205e7f47e4SManivannan Sadhasivam 		.drdy_mask = VL6180_RANGE_READY,
1215e7f47e4SManivannan Sadhasivam 		.start_reg = VL6180_RANGE_START,
1225e7f47e4SManivannan Sadhasivam 		.value_reg = VL6180_RANGE_RATE,
1235e7f47e4SManivannan Sadhasivam 		.word = true,
1245e7f47e4SManivannan Sadhasivam 	},
1255e7f47e4SManivannan Sadhasivam };
1265e7f47e4SManivannan Sadhasivam 
vl6180_read(struct i2c_client * client,u16 cmd,void * databuf,u8 len)1275e7f47e4SManivannan Sadhasivam static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf,
1285e7f47e4SManivannan Sadhasivam 		       u8 len)
1295e7f47e4SManivannan Sadhasivam {
1305e7f47e4SManivannan Sadhasivam 	__be16 cmdbuf = cpu_to_be16(cmd);
1315e7f47e4SManivannan Sadhasivam 	struct i2c_msg msgs[2] = {
1325e7f47e4SManivannan Sadhasivam 		{ .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf },
1335e7f47e4SManivannan Sadhasivam 		{ .addr = client->addr, .len = len, .buf = databuf,
1345e7f47e4SManivannan Sadhasivam 		  .flags = I2C_M_RD } };
1355e7f47e4SManivannan Sadhasivam 	int ret;
1365e7f47e4SManivannan Sadhasivam 
1375e7f47e4SManivannan Sadhasivam 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1385e7f47e4SManivannan Sadhasivam 	if (ret < 0)
1395e7f47e4SManivannan Sadhasivam 		dev_err(&client->dev, "failed reading register 0x%04x\n", cmd);
1405e7f47e4SManivannan Sadhasivam 
1415e7f47e4SManivannan Sadhasivam 	return ret;
1425e7f47e4SManivannan Sadhasivam }
1435e7f47e4SManivannan Sadhasivam 
vl6180_read_byte(struct i2c_client * client,u16 cmd)1445e7f47e4SManivannan Sadhasivam static int vl6180_read_byte(struct i2c_client *client, u16 cmd)
1455e7f47e4SManivannan Sadhasivam {
1465e7f47e4SManivannan Sadhasivam 	u8 data;
1475e7f47e4SManivannan Sadhasivam 	int ret;
1485e7f47e4SManivannan Sadhasivam 
1495e7f47e4SManivannan Sadhasivam 	ret = vl6180_read(client, cmd, &data, sizeof(data));
1505e7f47e4SManivannan Sadhasivam 	if (ret < 0)
1515e7f47e4SManivannan Sadhasivam 		return ret;
1525e7f47e4SManivannan Sadhasivam 
1535e7f47e4SManivannan Sadhasivam 	return data;
1545e7f47e4SManivannan Sadhasivam }
1555e7f47e4SManivannan Sadhasivam 
vl6180_read_word(struct i2c_client * client,u16 cmd)1565e7f47e4SManivannan Sadhasivam static int vl6180_read_word(struct i2c_client *client, u16 cmd)
1575e7f47e4SManivannan Sadhasivam {
1585e7f47e4SManivannan Sadhasivam 	__be16 data;
1595e7f47e4SManivannan Sadhasivam 	int ret;
1605e7f47e4SManivannan Sadhasivam 
1615e7f47e4SManivannan Sadhasivam 	ret = vl6180_read(client, cmd, &data, sizeof(data));
1625e7f47e4SManivannan Sadhasivam 	if (ret < 0)
1635e7f47e4SManivannan Sadhasivam 		return ret;
1645e7f47e4SManivannan Sadhasivam 
1655e7f47e4SManivannan Sadhasivam 	return be16_to_cpu(data);
1665e7f47e4SManivannan Sadhasivam }
1675e7f47e4SManivannan Sadhasivam 
vl6180_write_byte(struct i2c_client * client,u16 cmd,u8 val)1685e7f47e4SManivannan Sadhasivam static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val)
1695e7f47e4SManivannan Sadhasivam {
1705e7f47e4SManivannan Sadhasivam 	u8 buf[3];
1715e7f47e4SManivannan Sadhasivam 	struct i2c_msg msgs[1] = {
1725e7f47e4SManivannan Sadhasivam 		{ .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
1735e7f47e4SManivannan Sadhasivam 	int ret;
1745e7f47e4SManivannan Sadhasivam 
1755e7f47e4SManivannan Sadhasivam 	buf[0] = cmd >> 8;
1765e7f47e4SManivannan Sadhasivam 	buf[1] = cmd & 0xff;
1775e7f47e4SManivannan Sadhasivam 	buf[2] = val;
1785e7f47e4SManivannan Sadhasivam 
1795e7f47e4SManivannan Sadhasivam 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1805e7f47e4SManivannan Sadhasivam 	if (ret < 0) {
1815e7f47e4SManivannan Sadhasivam 		dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
1825e7f47e4SManivannan Sadhasivam 		return ret;
1835e7f47e4SManivannan Sadhasivam 	}
1845e7f47e4SManivannan Sadhasivam 
1855e7f47e4SManivannan Sadhasivam 	return 0;
1865e7f47e4SManivannan Sadhasivam }
1875e7f47e4SManivannan Sadhasivam 
vl6180_write_word(struct i2c_client * client,u16 cmd,u16 val)1885e7f47e4SManivannan Sadhasivam static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val)
1895e7f47e4SManivannan Sadhasivam {
1905e7f47e4SManivannan Sadhasivam 	__be16 buf[2];
1915e7f47e4SManivannan Sadhasivam 	struct i2c_msg msgs[1] = {
1925e7f47e4SManivannan Sadhasivam 		{ .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } };
1935e7f47e4SManivannan Sadhasivam 	int ret;
1945e7f47e4SManivannan Sadhasivam 
1955e7f47e4SManivannan Sadhasivam 	buf[0] = cpu_to_be16(cmd);
1965e7f47e4SManivannan Sadhasivam 	buf[1] = cpu_to_be16(val);
1975e7f47e4SManivannan Sadhasivam 
1985e7f47e4SManivannan Sadhasivam 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1995e7f47e4SManivannan Sadhasivam 	if (ret < 0) {
2005e7f47e4SManivannan Sadhasivam 		dev_err(&client->dev, "failed writing register 0x%04x\n", cmd);
2015e7f47e4SManivannan Sadhasivam 		return ret;
2025e7f47e4SManivannan Sadhasivam 	}
2035e7f47e4SManivannan Sadhasivam 
2045e7f47e4SManivannan Sadhasivam 	return 0;
2055e7f47e4SManivannan Sadhasivam }
2065e7f47e4SManivannan Sadhasivam 
vl6180_measure(struct vl6180_data * data,int addr)2075e7f47e4SManivannan Sadhasivam static int vl6180_measure(struct vl6180_data *data, int addr)
2085e7f47e4SManivannan Sadhasivam {
2095e7f47e4SManivannan Sadhasivam 	struct i2c_client *client = data->client;
2105e7f47e4SManivannan Sadhasivam 	int tries = 20, ret;
2115e7f47e4SManivannan Sadhasivam 	u16 value;
2125e7f47e4SManivannan Sadhasivam 
2135e7f47e4SManivannan Sadhasivam 	mutex_lock(&data->lock);
2145e7f47e4SManivannan Sadhasivam 	/* Start single shot measurement */
2155e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_byte(client,
2165e7f47e4SManivannan Sadhasivam 		vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP);
2175e7f47e4SManivannan Sadhasivam 	if (ret < 0)
2185e7f47e4SManivannan Sadhasivam 		goto fail;
2195e7f47e4SManivannan Sadhasivam 
2205e7f47e4SManivannan Sadhasivam 	while (tries--) {
2215e7f47e4SManivannan Sadhasivam 		ret = vl6180_read_byte(client, VL6180_INTR_STATUS);
2225e7f47e4SManivannan Sadhasivam 		if (ret < 0)
2235e7f47e4SManivannan Sadhasivam 			goto fail;
2245e7f47e4SManivannan Sadhasivam 
2255e7f47e4SManivannan Sadhasivam 		if (ret & vl6180_chan_regs_table[addr].drdy_mask)
2265e7f47e4SManivannan Sadhasivam 			break;
2275e7f47e4SManivannan Sadhasivam 		msleep(20);
2285e7f47e4SManivannan Sadhasivam 	}
2295e7f47e4SManivannan Sadhasivam 
2305e7f47e4SManivannan Sadhasivam 	if (tries < 0) {
2315e7f47e4SManivannan Sadhasivam 		ret = -EIO;
2325e7f47e4SManivannan Sadhasivam 		goto fail;
2335e7f47e4SManivannan Sadhasivam 	}
2345e7f47e4SManivannan Sadhasivam 
2355e7f47e4SManivannan Sadhasivam 	/* Read result value from appropriate registers */
2365e7f47e4SManivannan Sadhasivam 	ret = vl6180_chan_regs_table[addr].word ?
2375e7f47e4SManivannan Sadhasivam 		vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) :
2385e7f47e4SManivannan Sadhasivam 		vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg);
2395e7f47e4SManivannan Sadhasivam 	if (ret < 0)
2405e7f47e4SManivannan Sadhasivam 		goto fail;
2415e7f47e4SManivannan Sadhasivam 	value = ret;
2425e7f47e4SManivannan Sadhasivam 
2435e7f47e4SManivannan Sadhasivam 	/* Clear the interrupt flag after data read */
2445e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_byte(client, VL6180_INTR_CLEAR,
2455e7f47e4SManivannan Sadhasivam 		VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE);
2465e7f47e4SManivannan Sadhasivam 	if (ret < 0)
2475e7f47e4SManivannan Sadhasivam 		goto fail;
2485e7f47e4SManivannan Sadhasivam 
2495e7f47e4SManivannan Sadhasivam 	ret = value;
2505e7f47e4SManivannan Sadhasivam 
2515e7f47e4SManivannan Sadhasivam fail:
2525e7f47e4SManivannan Sadhasivam 	mutex_unlock(&data->lock);
2535e7f47e4SManivannan Sadhasivam 
2545e7f47e4SManivannan Sadhasivam 	return ret;
2555e7f47e4SManivannan Sadhasivam }
2565e7f47e4SManivannan Sadhasivam 
2575e7f47e4SManivannan Sadhasivam static const struct iio_chan_spec vl6180_channels[] = {
2585e7f47e4SManivannan Sadhasivam 	{
2595e7f47e4SManivannan Sadhasivam 		.type = IIO_LIGHT,
2605e7f47e4SManivannan Sadhasivam 		.address = VL6180_ALS,
2615e7f47e4SManivannan Sadhasivam 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
2625e7f47e4SManivannan Sadhasivam 			BIT(IIO_CHAN_INFO_INT_TIME) |
2635e7f47e4SManivannan Sadhasivam 			BIT(IIO_CHAN_INFO_SCALE) |
2645e7f47e4SManivannan Sadhasivam 			BIT(IIO_CHAN_INFO_HARDWAREGAIN),
2655e7f47e4SManivannan Sadhasivam 	}, {
2665e7f47e4SManivannan Sadhasivam 		.type = IIO_DISTANCE,
2675e7f47e4SManivannan Sadhasivam 		.address = VL6180_RANGE,
2685e7f47e4SManivannan Sadhasivam 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
2695e7f47e4SManivannan Sadhasivam 			BIT(IIO_CHAN_INFO_SCALE),
2705e7f47e4SManivannan Sadhasivam 	}, {
2715e7f47e4SManivannan Sadhasivam 		.type = IIO_PROXIMITY,
2725e7f47e4SManivannan Sadhasivam 		.address = VL6180_PROX,
2735e7f47e4SManivannan Sadhasivam 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
2745e7f47e4SManivannan Sadhasivam 	}
2755e7f47e4SManivannan Sadhasivam };
2765e7f47e4SManivannan Sadhasivam 
2775e7f47e4SManivannan Sadhasivam /*
278e794bf67SStefan Brüns  * Available Ambient Light Sensor gain settings, 1/1000th, and
279e794bf67SStefan Brüns  * corresponding setting for the VL6180_ALS_GAIN register
2805e7f47e4SManivannan Sadhasivam  */
281e794bf67SStefan Brüns static const int vl6180_als_gain_tab[8] = {
282e794bf67SStefan Brüns 	1000, 1250, 1670, 2500, 5000, 10000, 20000, 40000
283e794bf67SStefan Brüns };
284e794bf67SStefan Brüns static const u8 vl6180_als_gain_tab_bits[8] = {
285e794bf67SStefan Brüns 	VL6180_ALS_GAIN_1,    VL6180_ALS_GAIN_1_25,
286e794bf67SStefan Brüns 	VL6180_ALS_GAIN_1_67, VL6180_ALS_GAIN_2_5,
287e794bf67SStefan Brüns 	VL6180_ALS_GAIN_5,    VL6180_ALS_GAIN_10,
288e794bf67SStefan Brüns 	VL6180_ALS_GAIN_20,   VL6180_ALS_GAIN_40
2895e7f47e4SManivannan Sadhasivam };
2905e7f47e4SManivannan Sadhasivam 
vl6180_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)2915e7f47e4SManivannan Sadhasivam static int vl6180_read_raw(struct iio_dev *indio_dev,
2925e7f47e4SManivannan Sadhasivam 				struct iio_chan_spec const *chan,
2935e7f47e4SManivannan Sadhasivam 				int *val, int *val2, long mask)
2945e7f47e4SManivannan Sadhasivam {
2955e7f47e4SManivannan Sadhasivam 	struct vl6180_data *data = iio_priv(indio_dev);
296e794bf67SStefan Brüns 	int ret;
2975e7f47e4SManivannan Sadhasivam 
2985e7f47e4SManivannan Sadhasivam 	switch (mask) {
2995e7f47e4SManivannan Sadhasivam 	case IIO_CHAN_INFO_RAW:
3005e7f47e4SManivannan Sadhasivam 		ret = vl6180_measure(data, chan->address);
3015e7f47e4SManivannan Sadhasivam 		if (ret < 0)
3025e7f47e4SManivannan Sadhasivam 			return ret;
3035e7f47e4SManivannan Sadhasivam 		*val = ret;
3045e7f47e4SManivannan Sadhasivam 
3055e7f47e4SManivannan Sadhasivam 		return IIO_VAL_INT;
3065e7f47e4SManivannan Sadhasivam 	case IIO_CHAN_INFO_INT_TIME:
307df698c04SStefan Brüns 		*val = data->als_it_ms;
308df698c04SStefan Brüns 		*val2 = 1000;
3095e7f47e4SManivannan Sadhasivam 
310df698c04SStefan Brüns 		return IIO_VAL_FRACTIONAL;
311df698c04SStefan Brüns 
3125e7f47e4SManivannan Sadhasivam 	case IIO_CHAN_INFO_SCALE:
3135e7f47e4SManivannan Sadhasivam 		switch (chan->type) {
3145e7f47e4SManivannan Sadhasivam 		case IIO_LIGHT:
3153525d7cfSStefan Brüns 			/* one ALS count is 0.32 Lux @ gain 1, IT 100 ms */
3163525d7cfSStefan Brüns 			*val = 32000; /* 0.32 * 1000 * 100 */
3173525d7cfSStefan Brüns 			*val2 = data->als_gain_milli * data->als_it_ms;
3183525d7cfSStefan Brüns 
3193525d7cfSStefan Brüns 			return IIO_VAL_FRACTIONAL;
3203525d7cfSStefan Brüns 
3215e7f47e4SManivannan Sadhasivam 		case IIO_DISTANCE:
3225e7f47e4SManivannan Sadhasivam 			*val = 0; /* sensor reports mm, scale to meter */
3235e7f47e4SManivannan Sadhasivam 			*val2 = 1000;
3245e7f47e4SManivannan Sadhasivam 			break;
3255e7f47e4SManivannan Sadhasivam 		default:
3265e7f47e4SManivannan Sadhasivam 			return -EINVAL;
3275e7f47e4SManivannan Sadhasivam 		}
3285e7f47e4SManivannan Sadhasivam 
3295e7f47e4SManivannan Sadhasivam 		return IIO_VAL_INT_PLUS_MICRO;
3305e7f47e4SManivannan Sadhasivam 	case IIO_CHAN_INFO_HARDWAREGAIN:
331e794bf67SStefan Brüns 		*val = data->als_gain_milli;
332e794bf67SStefan Brüns 		*val2 = 1000;
3335e7f47e4SManivannan Sadhasivam 
334e794bf67SStefan Brüns 		return IIO_VAL_FRACTIONAL;
335e794bf67SStefan Brüns 
3365e7f47e4SManivannan Sadhasivam 	default:
3375e7f47e4SManivannan Sadhasivam 		return -EINVAL;
3385e7f47e4SManivannan Sadhasivam 	}
3395e7f47e4SManivannan Sadhasivam }
3405e7f47e4SManivannan Sadhasivam 
3415e7f47e4SManivannan Sadhasivam static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40");
3425e7f47e4SManivannan Sadhasivam 
3435e7f47e4SManivannan Sadhasivam static struct attribute *vl6180_attributes[] = {
3445e7f47e4SManivannan Sadhasivam 	&iio_const_attr_als_gain_available.dev_attr.attr,
3455e7f47e4SManivannan Sadhasivam 	NULL
3465e7f47e4SManivannan Sadhasivam };
3475e7f47e4SManivannan Sadhasivam 
3485e7f47e4SManivannan Sadhasivam static const struct attribute_group vl6180_attribute_group = {
3495e7f47e4SManivannan Sadhasivam 	.attrs = vl6180_attributes,
3505e7f47e4SManivannan Sadhasivam };
3515e7f47e4SManivannan Sadhasivam 
3525e7f47e4SManivannan Sadhasivam /* HOLD is needed before updating any config registers */
vl6180_hold(struct vl6180_data * data,bool hold)3535e7f47e4SManivannan Sadhasivam static int vl6180_hold(struct vl6180_data *data, bool hold)
3545e7f47e4SManivannan Sadhasivam {
3555e7f47e4SManivannan Sadhasivam 	return vl6180_write_byte(data->client, VL6180_HOLD,
3565e7f47e4SManivannan Sadhasivam 		hold ? VL6180_HOLD_ON : 0);
3575e7f47e4SManivannan Sadhasivam }
3585e7f47e4SManivannan Sadhasivam 
vl6180_set_als_gain(struct vl6180_data * data,int val,int val2)3595e7f47e4SManivannan Sadhasivam static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2)
3605e7f47e4SManivannan Sadhasivam {
361e794bf67SStefan Brüns 	int i, ret, gain;
3625e7f47e4SManivannan Sadhasivam 
363e794bf67SStefan Brüns 	if (val < 1 || val > 40)
364e794bf67SStefan Brüns 		return -EINVAL;
365e794bf67SStefan Brüns 
366e794bf67SStefan Brüns 	gain = (val * 1000000 + val2) / 1000;
367e794bf67SStefan Brüns 	if (gain < 1 || gain > 40000)
368e794bf67SStefan Brüns 		return -EINVAL;
369e794bf67SStefan Brüns 
370e794bf67SStefan Brüns 	i = find_closest(gain, vl6180_als_gain_tab,
371e794bf67SStefan Brüns 			 ARRAY_SIZE(vl6180_als_gain_tab));
372e794bf67SStefan Brüns 
3735e7f47e4SManivannan Sadhasivam 	mutex_lock(&data->lock);
3745e7f47e4SManivannan Sadhasivam 	ret = vl6180_hold(data, true);
3755e7f47e4SManivannan Sadhasivam 	if (ret < 0)
3765e7f47e4SManivannan Sadhasivam 		goto fail;
377e794bf67SStefan Brüns 
3785e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN,
379e794bf67SStefan Brüns 				vl6180_als_gain_tab_bits[i]);
380e794bf67SStefan Brüns 
381e794bf67SStefan Brüns 	if (ret >= 0)
382e794bf67SStefan Brüns 		data->als_gain_milli = vl6180_als_gain_tab[i];
383e794bf67SStefan Brüns 
3845e7f47e4SManivannan Sadhasivam fail:
3855e7f47e4SManivannan Sadhasivam 	vl6180_hold(data, false);
3865e7f47e4SManivannan Sadhasivam 	mutex_unlock(&data->lock);
3875e7f47e4SManivannan Sadhasivam 	return ret;
3885e7f47e4SManivannan Sadhasivam }
3895e7f47e4SManivannan Sadhasivam 
vl6180_set_it(struct vl6180_data * data,int val,int val2)3901e2ed3d0SStefan Brüns static int vl6180_set_it(struct vl6180_data *data, int val, int val2)
3915e7f47e4SManivannan Sadhasivam {
3921e2ed3d0SStefan Brüns 	int ret, it_ms;
3931e2ed3d0SStefan Brüns 
394ed0ccf6dSLars-Peter Clausen 	it_ms = DIV_ROUND_CLOSEST(val2, 1000); /* round to ms */
3951e2ed3d0SStefan Brüns 	if (val != 0 || it_ms < 1 || it_ms > 512)
3961e2ed3d0SStefan Brüns 		return -EINVAL;
3975e7f47e4SManivannan Sadhasivam 
3985e7f47e4SManivannan Sadhasivam 	mutex_lock(&data->lock);
3995e7f47e4SManivannan Sadhasivam 	ret = vl6180_hold(data, true);
4005e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4015e7f47e4SManivannan Sadhasivam 		goto fail;
4021e2ed3d0SStefan Brüns 
4031e2ed3d0SStefan Brüns 	ret = vl6180_write_word(data->client, VL6180_ALS_IT, it_ms - 1);
4041e2ed3d0SStefan Brüns 
405df698c04SStefan Brüns 	if (ret >= 0)
406df698c04SStefan Brüns 		data->als_it_ms = it_ms;
407df698c04SStefan Brüns 
4085e7f47e4SManivannan Sadhasivam fail:
4095e7f47e4SManivannan Sadhasivam 	vl6180_hold(data, false);
4105e7f47e4SManivannan Sadhasivam 	mutex_unlock(&data->lock);
4115e7f47e4SManivannan Sadhasivam 
4125e7f47e4SManivannan Sadhasivam 	return ret;
4135e7f47e4SManivannan Sadhasivam }
4145e7f47e4SManivannan Sadhasivam 
vl6180_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)4155e7f47e4SManivannan Sadhasivam static int vl6180_write_raw(struct iio_dev *indio_dev,
4165e7f47e4SManivannan Sadhasivam 			     struct iio_chan_spec const *chan,
4175e7f47e4SManivannan Sadhasivam 			     int val, int val2, long mask)
4185e7f47e4SManivannan Sadhasivam {
4195e7f47e4SManivannan Sadhasivam 	struct vl6180_data *data = iio_priv(indio_dev);
4205e7f47e4SManivannan Sadhasivam 
4215e7f47e4SManivannan Sadhasivam 	switch (mask) {
4225e7f47e4SManivannan Sadhasivam 	case IIO_CHAN_INFO_INT_TIME:
4231e2ed3d0SStefan Brüns 		return vl6180_set_it(data, val, val2);
4245e7f47e4SManivannan Sadhasivam 
4255e7f47e4SManivannan Sadhasivam 	case IIO_CHAN_INFO_HARDWAREGAIN:
4265e7f47e4SManivannan Sadhasivam 		if (chan->type != IIO_LIGHT)
4275e7f47e4SManivannan Sadhasivam 			return -EINVAL;
4285e7f47e4SManivannan Sadhasivam 
4295e7f47e4SManivannan Sadhasivam 		return vl6180_set_als_gain(data, val, val2);
4305e7f47e4SManivannan Sadhasivam 	default:
4315e7f47e4SManivannan Sadhasivam 		return -EINVAL;
4325e7f47e4SManivannan Sadhasivam 	}
4335e7f47e4SManivannan Sadhasivam }
4345e7f47e4SManivannan Sadhasivam 
4355e7f47e4SManivannan Sadhasivam static const struct iio_info vl6180_info = {
4365e7f47e4SManivannan Sadhasivam 	.read_raw = vl6180_read_raw,
4375e7f47e4SManivannan Sadhasivam 	.write_raw = vl6180_write_raw,
4385e7f47e4SManivannan Sadhasivam 	.attrs = &vl6180_attribute_group,
4395e7f47e4SManivannan Sadhasivam };
4405e7f47e4SManivannan Sadhasivam 
vl6180_init(struct vl6180_data * data)4415e7f47e4SManivannan Sadhasivam static int vl6180_init(struct vl6180_data *data)
4425e7f47e4SManivannan Sadhasivam {
4435e7f47e4SManivannan Sadhasivam 	struct i2c_client *client = data->client;
4445e7f47e4SManivannan Sadhasivam 	int ret;
4455e7f47e4SManivannan Sadhasivam 
4465e7f47e4SManivannan Sadhasivam 	ret = vl6180_read_byte(client, VL6180_MODEL_ID);
4475e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4485e7f47e4SManivannan Sadhasivam 		return ret;
4495e7f47e4SManivannan Sadhasivam 
4505e7f47e4SManivannan Sadhasivam 	if (ret != VL6180_MODEL_ID_VAL) {
4515e7f47e4SManivannan Sadhasivam 		dev_err(&client->dev, "invalid model ID %02x\n", ret);
4525e7f47e4SManivannan Sadhasivam 		return -ENODEV;
4535e7f47e4SManivannan Sadhasivam 	}
4545e7f47e4SManivannan Sadhasivam 
4555e7f47e4SManivannan Sadhasivam 	ret = vl6180_hold(data, true);
4565e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4575e7f47e4SManivannan Sadhasivam 		return ret;
4585e7f47e4SManivannan Sadhasivam 
4595e7f47e4SManivannan Sadhasivam 	ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET);
4605e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4615e7f47e4SManivannan Sadhasivam 		return ret;
4625e7f47e4SManivannan Sadhasivam 
4635e7f47e4SManivannan Sadhasivam 	/*
4645e7f47e4SManivannan Sadhasivam 	 * Detect false reset condition here. This bit is always set when the
4655e7f47e4SManivannan Sadhasivam 	 * system comes out of reset.
4665e7f47e4SManivannan Sadhasivam 	 */
4675e7f47e4SManivannan Sadhasivam 	if (ret != 0x01)
4685e7f47e4SManivannan Sadhasivam 		dev_info(&client->dev, "device is not fresh out of reset\n");
4695e7f47e4SManivannan Sadhasivam 
4705e7f47e4SManivannan Sadhasivam 	/* Enable ALS and Range ready interrupts */
4715e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_byte(client, VL6180_INTR_CONFIG,
4725e7f47e4SManivannan Sadhasivam 				VL6180_ALS_READY | VL6180_RANGE_READY);
4735e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4745e7f47e4SManivannan Sadhasivam 		return ret;
4755e7f47e4SManivannan Sadhasivam 
4765e7f47e4SManivannan Sadhasivam 	/* ALS integration time: 100ms */
477df698c04SStefan Brüns 	data->als_it_ms = 100;
4785e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100);
4795e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4805e7f47e4SManivannan Sadhasivam 		return ret;
4815e7f47e4SManivannan Sadhasivam 
4825e7f47e4SManivannan Sadhasivam 	/* ALS gain: 1 */
483e794bf67SStefan Brüns 	data->als_gain_milli = 1000;
4845e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1);
4855e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4865e7f47e4SManivannan Sadhasivam 		return ret;
4875e7f47e4SManivannan Sadhasivam 
4885e7f47e4SManivannan Sadhasivam 	ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00);
4895e7f47e4SManivannan Sadhasivam 	if (ret < 0)
4905e7f47e4SManivannan Sadhasivam 		return ret;
4915e7f47e4SManivannan Sadhasivam 
4925e7f47e4SManivannan Sadhasivam 	return vl6180_hold(data, false);
4935e7f47e4SManivannan Sadhasivam }
4945e7f47e4SManivannan Sadhasivam 
vl6180_probe(struct i2c_client * client)4952ca0b16fSUwe Kleine-König static int vl6180_probe(struct i2c_client *client)
4965e7f47e4SManivannan Sadhasivam {
4975e7f47e4SManivannan Sadhasivam 	struct vl6180_data *data;
4985e7f47e4SManivannan Sadhasivam 	struct iio_dev *indio_dev;
4995e7f47e4SManivannan Sadhasivam 	int ret;
5005e7f47e4SManivannan Sadhasivam 
5015e7f47e4SManivannan Sadhasivam 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
5025e7f47e4SManivannan Sadhasivam 	if (!indio_dev)
5035e7f47e4SManivannan Sadhasivam 		return -ENOMEM;
5045e7f47e4SManivannan Sadhasivam 
5055e7f47e4SManivannan Sadhasivam 	data = iio_priv(indio_dev);
5065e7f47e4SManivannan Sadhasivam 	i2c_set_clientdata(client, indio_dev);
5075e7f47e4SManivannan Sadhasivam 	data->client = client;
5085e7f47e4SManivannan Sadhasivam 	mutex_init(&data->lock);
5095e7f47e4SManivannan Sadhasivam 
5105e7f47e4SManivannan Sadhasivam 	indio_dev->info = &vl6180_info;
5115e7f47e4SManivannan Sadhasivam 	indio_dev->channels = vl6180_channels;
5125e7f47e4SManivannan Sadhasivam 	indio_dev->num_channels = ARRAY_SIZE(vl6180_channels);
5135e7f47e4SManivannan Sadhasivam 	indio_dev->name = VL6180_DRV_NAME;
5145e7f47e4SManivannan Sadhasivam 	indio_dev->modes = INDIO_DIRECT_MODE;
5155e7f47e4SManivannan Sadhasivam 
5165e7f47e4SManivannan Sadhasivam 	ret = vl6180_init(data);
5175e7f47e4SManivannan Sadhasivam 	if (ret < 0)
5185e7f47e4SManivannan Sadhasivam 		return ret;
5195e7f47e4SManivannan Sadhasivam 
5205e7f47e4SManivannan Sadhasivam 	return devm_iio_device_register(&client->dev, indio_dev);
5215e7f47e4SManivannan Sadhasivam }
5225e7f47e4SManivannan Sadhasivam 
5235e7f47e4SManivannan Sadhasivam static const struct of_device_id vl6180_of_match[] = {
5245e7f47e4SManivannan Sadhasivam 	{ .compatible = "st,vl6180", },
5255e7f47e4SManivannan Sadhasivam 	{ },
5265e7f47e4SManivannan Sadhasivam };
5275e7f47e4SManivannan Sadhasivam MODULE_DEVICE_TABLE(of, vl6180_of_match);
5285e7f47e4SManivannan Sadhasivam 
5295e7f47e4SManivannan Sadhasivam static const struct i2c_device_id vl6180_id[] = {
530*4391affaSUwe Kleine-König 	{ "vl6180" },
5315e7f47e4SManivannan Sadhasivam 	{ }
5325e7f47e4SManivannan Sadhasivam };
5335e7f47e4SManivannan Sadhasivam MODULE_DEVICE_TABLE(i2c, vl6180_id);
5345e7f47e4SManivannan Sadhasivam 
5355e7f47e4SManivannan Sadhasivam static struct i2c_driver vl6180_driver = {
5365e7f47e4SManivannan Sadhasivam 	.driver = {
5375e7f47e4SManivannan Sadhasivam 		.name   = VL6180_DRV_NAME,
53877baa8d6SJonathan Cameron 		.of_match_table = vl6180_of_match,
5395e7f47e4SManivannan Sadhasivam 	},
5407cf15f42SUwe Kleine-König 	.probe = vl6180_probe,
5415e7f47e4SManivannan Sadhasivam 	.id_table = vl6180_id,
5425e7f47e4SManivannan Sadhasivam };
5435e7f47e4SManivannan Sadhasivam 
5445e7f47e4SManivannan Sadhasivam module_i2c_driver(vl6180_driver);
5455e7f47e4SManivannan Sadhasivam 
5465e7f47e4SManivannan Sadhasivam MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
5475e7f47e4SManivannan Sadhasivam MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
5485e7f47e4SManivannan Sadhasivam MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver");
5495e7f47e4SManivannan Sadhasivam MODULE_LICENSE("GPL");
550