summaryrefslogtreecommitdiff
path: root/drivers/iio/light/veml6040.c
blob: 216e271001a850072f2e93e9f133ec32de286f82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-License-Identifier: GPL-2.0+
/*
 * Vishay VEML6040 RGBW light sensor driver
 *
 * Copyright (C) 2024 Sentec AG
 * Author: Arthur Becker <arthur.becker@sentec.com>
 *
 */

#include <linux/bitfield.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/module.h>
#include <linux/regmap.h>

/* VEML6040 Configuration Registers
 *
 * SD: Shutdown
 * AF: Auto / Force Mode (Auto Measurements On:0, Off:1)
 * TR: Trigger Measurement (when AF Bit is set)
 * IT: Integration Time
 */
#define VEML6040_CONF_REG 0x000
#define VEML6040_CONF_SD_MSK BIT(0)
#define VEML6040_CONF_AF_MSK BIT(1)
#define VEML6040_CONF_TR_MSK BIT(2)
#define VEML6040_CONF_IT_MSK GENMASK(6, 4)
#define VEML6040_CONF_IT_40_MS 0
#define VEML6040_CONF_IT_80_MS 1
#define VEML6040_CONF_IT_160_MS 2
#define VEML6040_CONF_IT_320_MS 3
#define VEML6040_CONF_IT_640_MS 4
#define VEML6040_CONF_IT_1280_MS 5

/* VEML6040 Read Only Registers */
#define VEML6040_REG_R 0x08
#define VEML6040_REG_G 0x09
#define VEML6040_REG_B 0x0A
#define VEML6040_REG_W 0x0B

static const int veml6040_it_ms[] = { 40, 80, 160, 320, 640, 1280 };

enum veml6040_chan {
	CH_RED,
	CH_GREEN,
	CH_BLUE,
	CH_WHITE,
};

struct veml6040_data {
	struct i2c_client *client;
	struct regmap *regmap;
};

static const struct regmap_config veml6040_regmap_config = {
	.name = "veml6040_regmap",
	.reg_bits = 8,
	.val_bits = 16,
	.max_register = VEML6040_REG_W,
	.val_format_endian = REGMAP_ENDIAN_LITTLE,
};

static int veml6040_read_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan, int *val,
			     int *val2, long mask)
{
	int ret, reg, it_index;
	struct veml6040_data *data = iio_priv(indio_dev);
	struct regmap *regmap = data->regmap;
	struct device *dev = &data->client->dev;

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		ret = regmap_read(regmap, chan->address, &reg);
		if (ret) {
			dev_err(dev, "Data read failed: %d\n", ret);
			return ret;
		}
		*val = reg;
		return IIO_VAL_INT;

	case IIO_CHAN_INFO_INT_TIME:
		ret = regmap_read(regmap, VEML6040_CONF_REG, &reg);
		if (ret) {
			dev_err(dev, "Data read failed: %d\n", ret);
			return ret;
		}
		it_index = FIELD_GET(VEML6040_CONF_IT_MSK, reg);
		if (it_index >= ARRAY_SIZE(veml6040_it_ms)) {
			dev_err(dev, "Invalid Integration Time Set");
			return -EINVAL;
		}
		*val = veml6040_it_ms[it_index];
		return IIO_VAL_INT;

	default:
		return -EINVAL;
	}
}

static int veml6040_write_raw(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan, int val,
			      int val2, long mask)
{
	struct veml6040_data *data = iio_priv(indio_dev);

	switch (mask) {
	case IIO_CHAN_INFO_INT_TIME:
		for (int i = 0; i < ARRAY_SIZE(veml6040_it_ms); i++) {
			if (veml6040_it_ms[i] != val)
				continue;

			return regmap_update_bits(data->regmap,
					VEML6040_CONF_REG,
					VEML6040_CONF_IT_MSK,
					FIELD_PREP(VEML6040_CONF_IT_MSK, i));
		}
		return -EINVAL;
	default:
		return -EINVAL;
	}
}

static int veml6040_read_avail(struct iio_dev *indio_dev,
			       struct iio_chan_spec const *chan,
			       const int **vals, int *type, int *length,
			       long mask)
{
	switch (mask) {
	case IIO_CHAN_INFO_INT_TIME:
		*length = ARRAY_SIZE(veml6040_it_ms);
		*vals = veml6040_it_ms;
		*type = IIO_VAL_INT;
		return IIO_AVAIL_LIST;

	default:
		return -EINVAL;
	}
}

static const struct iio_info veml6040_info = {
	.read_raw = veml6040_read_raw,
	.write_raw = veml6040_write_raw,
	.read_avail = veml6040_read_avail,
};

static const struct iio_chan_spec veml6040_channels[] = {
	{
		.type = IIO_INTENSITY,
		.address = VEML6040_REG_R,
		.channel = CH_RED,
		.channel2 = IIO_MOD_LIGHT_RED,
		.modified = 1,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_INT_TIME),
		.info_mask_shared_by_type_available =
			BIT(IIO_CHAN_INFO_INT_TIME),
	},
	{
		.type = IIO_INTENSITY,
		.address = VEML6040_REG_G,
		.channel = CH_GREEN,
		.channel2 = IIO_MOD_LIGHT_GREEN,
		.modified = 1,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_INT_TIME),
		.info_mask_shared_by_type_available =
			BIT(IIO_CHAN_INFO_INT_TIME),
	},
	{
		.type = IIO_INTENSITY,
		.address = VEML6040_REG_B,
		.channel = CH_BLUE,
		.channel2 = IIO_MOD_LIGHT_BLUE,
		.modified = 1,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_INT_TIME),
		.info_mask_shared_by_type_available =
			BIT(IIO_CHAN_INFO_INT_TIME),
	},
	{
		.type = IIO_INTENSITY,
		.address = VEML6040_REG_W,
		.channel = CH_WHITE,
		.channel2 = IIO_MOD_LIGHT_CLEAR,
		.modified = 1,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_INT_TIME),
		.info_mask_shared_by_type_available =
			BIT(IIO_CHAN_INFO_INT_TIME),
	}
};

static void veml6040_shutdown_action(void *data)
{
	struct veml6040_data *veml6040_data = data;

	regmap_update_bits(veml6040_data->regmap, VEML6040_CONF_REG,
			   VEML6040_CONF_SD_MSK, VEML6040_CONF_SD_MSK);
}

static int veml6040_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct veml6040_data *data;
	struct iio_dev *indio_dev;
	struct regmap *regmap;
	const int init_config =
		FIELD_PREP(VEML6040_CONF_IT_MSK, VEML6040_CONF_IT_40_MS) |
		FIELD_PREP(VEML6040_CONF_AF_MSK, 0) |
		FIELD_PREP(VEML6040_CONF_SD_MSK, 0);
	int ret;

	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
		return dev_err_probe(dev, -EOPNOTSUPP,
				     "I2C adapter doesn't support plain I2C\n");

	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
	if (!indio_dev)
		return dev_err_probe(dev, -ENOMEM,
				     "IIO device allocation failed\n");

	regmap = devm_regmap_init_i2c(client, &veml6040_regmap_config);
	if (IS_ERR(regmap))
		return dev_err_probe(dev, PTR_ERR(regmap),
				     "Regmap setup failed\n");

	data = iio_priv(indio_dev);
	i2c_set_clientdata(client, indio_dev);
	data->client = client;
	data->regmap = regmap;

	indio_dev->name = "veml6040";
	indio_dev->info = &veml6040_info;
	indio_dev->channels = veml6040_channels;
	indio_dev->num_channels = ARRAY_SIZE(veml6040_channels);
	indio_dev->modes = INDIO_DIRECT_MODE;

	ret = devm_regulator_get_enable(dev, "vdd");
	if (ret)
		return ret;

	ret = regmap_write(regmap, VEML6040_CONF_REG, init_config);
	if (ret)
		return dev_err_probe(dev, ret,
				     "Could not set initial config\n");

	ret = devm_add_action_or_reset(dev, veml6040_shutdown_action, data);
	if (ret)
		return ret;

	return devm_iio_device_register(dev, indio_dev);
}

static const struct i2c_device_id veml6040_id_table[] = {
	{"veml6040"},
	{}
};
MODULE_DEVICE_TABLE(i2c, veml6040_id_table);

static const struct of_device_id veml6040_of_match[] = {
	{.compatible = "vishay,veml6040"},
	{}
};
MODULE_DEVICE_TABLE(of, veml6040_of_match);

static struct i2c_driver veml6040_driver = {
	.probe = veml6040_probe,
	.id_table = veml6040_id_table,
	.driver = {
		.name = "veml6040",
		.of_match_table = veml6040_of_match,
	},
};
module_i2c_driver(veml6040_driver);

MODULE_DESCRIPTION("veml6040 RGBW light sensor driver");
MODULE_AUTHOR("Arthur Becker <arthur.becker@sentec.com>");
MODULE_LICENSE("GPL");