summaryrefslogtreecommitdiffstats
path: root/drivers/iio/light/isl76682.c
blob: cf6ddee44ffc3f8010de912d36d16d474dbdcc66 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// SPDX-License-Identifier: GPL-2.0-only
/*
 * IIO driver for the light sensor ISL76682.
 * ISL76682 is Ambient Light Sensor
 *
 * Copyright (c) 2023 Marek Vasut <marex@denx.de>
 */

#include <linux/array_size.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/regmap.h>
#include <linux/types.h>

#include <linux/iio/iio.h>

#define ISL76682_REG_COMMAND			0x00

#define ISL76682_COMMAND_EN			BIT(7)
#define ISL76682_COMMAND_MODE_CONTINUOUS	BIT(6)
#define ISL76682_COMMAND_LIGHT_IR		BIT(5)

#define ISL76682_COMMAND_RANGE_LUX_1K		0x0
#define ISL76682_COMMAND_RANGE_LUX_4K		0x1
#define ISL76682_COMMAND_RANGE_LUX_16K		0x2
#define ISL76682_COMMAND_RANGE_LUX_64K		0x3
#define ISL76682_COMMAND_RANGE_LUX_MASK		GENMASK(1, 0)

#define ISL76682_REG_ALSIR_L			0x01

#define ISL76682_REG_ALSIR_U			0x02

#define ISL76682_NUM_REGS			(ISL76682_REG_ALSIR_U + 1)

#define ISL76682_CONV_TIME_MS			100
#define ISL76682_INT_TIME_US			90000

#define ISL76682_ADC_MAX			(BIT(16) - 1)

struct isl76682_chip {
	/*
	 * Lock to synchronize access to device command register
	 * and the content of range variable below.
	 */
	struct mutex			lock;
	struct regmap			*regmap;
	u8				range;
	u8				command;
};

struct isl76682_range {
	u8				range;
	u32				als;
	u32				ir;
};

static struct isl76682_range isl76682_range_table[] = {
	{ ISL76682_COMMAND_RANGE_LUX_1K, 15000, 10500 },
	{ ISL76682_COMMAND_RANGE_LUX_4K, 60000, 42000 },
	{ ISL76682_COMMAND_RANGE_LUX_16K, 240000, 168000 },
	{ ISL76682_COMMAND_RANGE_LUX_64K, 960000, 673000 }
};

static int isl76682_get(struct isl76682_chip *chip, bool mode_ir, int *data)
{
	u8 command;
	int ret;

	command = ISL76682_COMMAND_EN | ISL76682_COMMAND_MODE_CONTINUOUS |
		  chip->range;

	if (mode_ir)
		command |= ISL76682_COMMAND_LIGHT_IR;

	if (command != chip->command) {
		ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, command);
		if (ret)
			return ret;

		/* Need to wait for conversion time if ALS/IR mode enabled */
		msleep(ISL76682_CONV_TIME_MS);

		chip->command = command;
	}

	ret = regmap_bulk_read(chip->regmap, ISL76682_REG_ALSIR_L, data, 2);
	*data &= ISL76682_ADC_MAX;
	return ret;
}

static int isl76682_write_raw(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan,
			      int val, int val2, long mask)
{
	struct isl76682_chip *chip = iio_priv(indio_dev);
	int i;

	if (mask != IIO_CHAN_INFO_SCALE)
		return -EINVAL;

	if (val != 0)
		return -EINVAL;

	for (i = 0; i < ARRAY_SIZE(isl76682_range_table); i++) {
		if (chan->type == IIO_LIGHT && val2 != isl76682_range_table[i].als)
			continue;
		if (chan->type == IIO_INTENSITY && val2 != isl76682_range_table[i].ir)
			continue;

		scoped_guard(mutex, &chip->lock)
			chip->range = isl76682_range_table[i].range;
		return 0;
	}

	return -EINVAL;
}

static int isl76682_read_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int *val, int *val2, long mask)
{
	struct isl76682_chip *chip = iio_priv(indio_dev);
	int ret;
	int i;

	guard(mutex)(&chip->lock);

	switch (mask) {
	case IIO_CHAN_INFO_RAW:
		switch (chan->type) {
		case IIO_LIGHT:
			ret = isl76682_get(chip, false, val);
			return (ret < 0) ? ret : IIO_VAL_INT;
		case IIO_INTENSITY:
			ret = isl76682_get(chip, true, val);
			return (ret < 0) ? ret : IIO_VAL_INT;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_SCALE:
		for (i = 0; i < ARRAY_SIZE(isl76682_range_table); i++) {
			if (chip->range != isl76682_range_table[i].range)
				continue;

			*val = 0;
			switch (chan->type) {
			case IIO_LIGHT:
				*val2 = isl76682_range_table[i].als;
				return IIO_VAL_INT_PLUS_MICRO;
			case IIO_INTENSITY:
				*val2 = isl76682_range_table[i].ir;
				return IIO_VAL_INT_PLUS_MICRO;
			default:
				return -EINVAL;
			}
		}
		return -EINVAL;
	case IIO_CHAN_INFO_INT_TIME:
		*val = 0;
		*val2 = ISL76682_INT_TIME_US;
		return IIO_VAL_INT_PLUS_MICRO;
	default:
		return -EINVAL;
	}
}

static int illuminance_scale_available[] = {
	0, 15000,
	0, 60000,
	0, 240000,
	0, 960000,
};

static int intensity_scale_available[] = {
	0, 10500,
	0, 42000,
	0, 168000,
	0, 673000,
};

static int isl76682_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_SCALE:
		switch (chan->type) {
		case IIO_LIGHT:
			*vals = illuminance_scale_available;
			*length = ARRAY_SIZE(illuminance_scale_available);
			*type = IIO_VAL_INT_PLUS_MICRO;
			return IIO_AVAIL_LIST;
		case IIO_INTENSITY:
			*vals = intensity_scale_available;
			*length = ARRAY_SIZE(intensity_scale_available);
			*type = IIO_VAL_INT_PLUS_MICRO;
			return IIO_AVAIL_LIST;
		default:
			return -EINVAL;
		}
	default:
		return -EINVAL;
	}
}

static const struct iio_chan_spec isl76682_channels[] = {
	{
		.type = IIO_LIGHT,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				      BIT(IIO_CHAN_INFO_SCALE),
		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE),
		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
	}, {
		.type = IIO_INTENSITY,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				      BIT(IIO_CHAN_INFO_SCALE),
		.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE),
		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
	}
};

static const struct iio_info isl76682_info = {
	.read_avail	= isl76682_read_avail,
	.read_raw	= isl76682_read_raw,
	.write_raw	= isl76682_write_raw,
};

static int isl76682_clear_configure_reg(struct isl76682_chip *chip)
{
	struct device *dev = regmap_get_device(chip->regmap);
	int ret;

	ret = regmap_write(chip->regmap, ISL76682_REG_COMMAND, 0x0);
	if (ret < 0)
		dev_err(dev, "Error %d clearing the CONFIGURE register\n", ret);

	/*
	 * In the success case, the command register was zeroed out.
	 *
	 * In the error case, we do not know in which state the command
	 * register is, so we assume it is zeroed out, so that it would
	 * be reprogrammed at the next data read out, and at that time
	 * we hope it would be reprogrammed successfully. That is very
	 * much a best effort approach.
	 */
	chip->command = 0;

	return ret;
}

static void isl76682_reset_action(void *chip)
{
	isl76682_clear_configure_reg(chip);
}

static bool isl76682_is_volatile_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case ISL76682_REG_ALSIR_L:
	case ISL76682_REG_ALSIR_U:
		return true;
	default:
		return false;
	}
}

static const struct regmap_config isl76682_regmap_config = {
	.reg_bits		= 8,
	.val_bits		= 8,
	.volatile_reg		= isl76682_is_volatile_reg,
	.max_register		= ISL76682_NUM_REGS - 1,
	.num_reg_defaults_raw	= ISL76682_NUM_REGS,
	.cache_type		= REGCACHE_FLAT,
};

static int isl76682_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	struct isl76682_chip *chip;
	struct iio_dev *indio_dev;
	int ret;

	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
	if (!indio_dev)
		return -ENOMEM;

	chip = iio_priv(indio_dev);

	mutex_init(&chip->lock);

	chip->regmap = devm_regmap_init_i2c(client, &isl76682_regmap_config);
	ret = PTR_ERR_OR_ZERO(chip->regmap);
	if (ret)
		return dev_err_probe(dev, ret, "Error initializing regmap\n");

	chip->range = ISL76682_COMMAND_RANGE_LUX_1K;

	ret = isl76682_clear_configure_reg(chip);
	if (ret < 0)
		return ret;

	ret = devm_add_action_or_reset(dev, isl76682_reset_action, chip);
	if (ret)
		return ret;

	indio_dev->info = &isl76682_info;
	indio_dev->channels = isl76682_channels;
	indio_dev->num_channels = ARRAY_SIZE(isl76682_channels);
	indio_dev->name = "isl76682";
	indio_dev->modes = INDIO_DIRECT_MODE;

	return devm_iio_device_register(dev, indio_dev);
}

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

static const struct of_device_id isl76682_of_match[] = {
	{ .compatible = "isil,isl76682" },
	{ }
};
MODULE_DEVICE_TABLE(of, isl76682_of_match);

static struct i2c_driver isl76682_driver = {
	.driver  = {
		.name		= "isl76682",
		.of_match_table	= isl76682_of_match,
	},
	.probe		= isl76682_probe,
	.id_table	= isl76682_id,
};
module_i2c_driver(isl76682_driver);

MODULE_DESCRIPTION("ISL76682 Ambient Light Sensor driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Marek Vasut <marex@denx.de>");