1 // SPDX-License-Identifier: GPL-2.0-only
3 * zopt2201.c - Support for IDT ZOPT2201 ambient light and UV B sensor
5 * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
7 * Datasheet: https://www.idt.com/document/dst/zopt2201-datasheet
8 * 7-bit I2C slave addresses 0x53 (default) or 0x52 (programmed)
10 * TODO: interrupt support, ALS/UVB raw mode
13 #include <linux/module.h>
14 #include <linux/i2c.h>
15 #include <linux/mutex.h>
16 #include <linux/err.h>
17 #include <linux/delay.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
22 #include <asm/unaligned.h>
24 #define ZOPT2201_DRV_NAME "zopt2201"
27 #define ZOPT2201_MAIN_CTRL 0x00
28 #define ZOPT2201_LS_MEAS_RATE 0x04
29 #define ZOPT2201_LS_GAIN 0x05
30 #define ZOPT2201_PART_ID 0x06
31 #define ZOPT2201_MAIN_STATUS 0x07
32 #define ZOPT2201_ALS_DATA 0x0d /* LSB first, 13 to 20 bits */
33 #define ZOPT2201_UVB_DATA 0x10 /* LSB first, 13 to 20 bits */
34 #define ZOPT2201_UV_COMP_DATA 0x13 /* LSB first, 13 to 20 bits */
35 #define ZOPT2201_COMP_DATA 0x16 /* LSB first, 13 to 20 bits */
36 #define ZOPT2201_INT_CFG 0x19
37 #define ZOPT2201_INT_PST 0x1a
39 #define ZOPT2201_MAIN_CTRL_LS_MODE BIT(3) /* 0 .. ALS, 1 .. UV B */
40 #define ZOPT2201_MAIN_CTRL_LS_EN BIT(1)
42 /* Values for ZOPT2201_LS_MEAS_RATE resolution / bit width */
43 #define ZOPT2201_MEAS_RES_20BIT 0 /* takes 400 ms */
44 #define ZOPT2201_MEAS_RES_19BIT 1 /* takes 200 ms */
45 #define ZOPT2201_MEAS_RES_18BIT 2 /* takes 100 ms, default */
46 #define ZOPT2201_MEAS_RES_17BIT 3 /* takes 50 ms */
47 #define ZOPT2201_MEAS_RES_16BIT 4 /* takes 25 ms */
48 #define ZOPT2201_MEAS_RES_13BIT 5 /* takes 3.125 ms */
49 #define ZOPT2201_MEAS_RES_SHIFT 4
51 /* Values for ZOPT2201_LS_MEAS_RATE measurement rate */
52 #define ZOPT2201_MEAS_FREQ_25MS 0
53 #define ZOPT2201_MEAS_FREQ_50MS 1
54 #define ZOPT2201_MEAS_FREQ_100MS 2 /* default */
55 #define ZOPT2201_MEAS_FREQ_200MS 3
56 #define ZOPT2201_MEAS_FREQ_500MS 4
57 #define ZOPT2201_MEAS_FREQ_1000MS 5
58 #define ZOPT2201_MEAS_FREQ_2000MS 6
60 /* Values for ZOPT2201_LS_GAIN */
61 #define ZOPT2201_LS_GAIN_1 0
62 #define ZOPT2201_LS_GAIN_3 1
63 #define ZOPT2201_LS_GAIN_6 2
64 #define ZOPT2201_LS_GAIN_9 3
65 #define ZOPT2201_LS_GAIN_18 4
67 /* Values for ZOPT2201_MAIN_STATUS */
68 #define ZOPT2201_MAIN_STATUS_POWERON BIT(5)
69 #define ZOPT2201_MAIN_STATUS_INT BIT(4)
70 #define ZOPT2201_MAIN_STATUS_DRDY BIT(3)
72 #define ZOPT2201_PART_NUMBER 0xb2
74 struct zopt2201_data {
75 struct i2c_client *client;
83 unsigned int gain; /* gain factor */
84 unsigned int scale; /* micro lux per count */
85 } zopt2201_gain_als[] = {
94 unsigned int gain; /* gain factor */
95 unsigned int scale; /* micro W/m2 per count */
96 } zopt2201_gain_uvb[] = {
104 static const struct {
105 unsigned int bits; /* sensor resolution in bits */
106 unsigned long us; /* measurement time in micro seconds */
107 } zopt2201_resolution[] = {
116 static const struct {
117 unsigned int scale, uscale; /* scale factor as integer + micro */
118 u8 gain; /* gain register value */
119 u8 res; /* resolution register value */
120 } zopt2201_scale_als[] = {
121 { 19, 200000, 0, 5 },
145 static const struct {
146 unsigned int scale, uscale; /* scale factor as integer + micro */
147 u8 gain; /* gain register value */
148 u8 res; /* resolution register value */
149 } zopt2201_scale_uvb[] = {
174 static int zopt2201_enable_mode(struct zopt2201_data *data, bool uvb_mode)
176 u8 out = ZOPT2201_MAIN_CTRL_LS_EN;
179 out |= ZOPT2201_MAIN_CTRL_LS_MODE;
181 return i2c_smbus_write_byte_data(data->client, ZOPT2201_MAIN_CTRL, out);
184 static int zopt2201_read(struct zopt2201_data *data, u8 reg)
186 struct i2c_client *client = data->client;
191 mutex_lock(&data->lock);
192 ret = zopt2201_enable_mode(data, reg == ZOPT2201_UVB_DATA);
197 unsigned long t = zopt2201_resolution[data->res].us;
200 usleep_range(t, t + 1000);
203 ret = i2c_smbus_read_byte_data(client, ZOPT2201_MAIN_STATUS);
206 if (ret & ZOPT2201_MAIN_STATUS_DRDY)
215 ret = i2c_smbus_read_i2c_block_data(client, reg, sizeof(buf), buf);
219 ret = i2c_smbus_write_byte_data(client, ZOPT2201_MAIN_CTRL, 0x00);
222 mutex_unlock(&data->lock);
224 return get_unaligned_le24(&buf[0]);
227 mutex_unlock(&data->lock);
231 static const struct iio_chan_spec zopt2201_channels[] = {
234 .address = ZOPT2201_ALS_DATA,
235 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
236 BIT(IIO_CHAN_INFO_SCALE),
237 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
240 .type = IIO_INTENSITY,
242 .channel2 = IIO_MOD_LIGHT_UV,
243 .address = ZOPT2201_UVB_DATA,
244 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
245 BIT(IIO_CHAN_INFO_SCALE),
246 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME),
250 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
254 static int zopt2201_read_raw(struct iio_dev *indio_dev,
255 struct iio_chan_spec const *chan,
256 int *val, int *val2, long mask)
258 struct zopt2201_data *data = iio_priv(indio_dev);
263 case IIO_CHAN_INFO_RAW:
264 ret = zopt2201_read(data, chan->address);
269 case IIO_CHAN_INFO_PROCESSED:
270 ret = zopt2201_read(data, ZOPT2201_UVB_DATA);
274 (1 << (20 - zopt2201_resolution[data->res].bits)) /
275 zopt2201_gain_uvb[data->gain].gain;
277 case IIO_CHAN_INFO_SCALE:
278 switch (chan->address) {
279 case ZOPT2201_ALS_DATA:
280 *val = zopt2201_gain_als[data->gain].scale;
282 case ZOPT2201_UVB_DATA:
283 *val = zopt2201_gain_uvb[data->gain].scale;
290 *val2 *= (1 << (zopt2201_resolution[data->res].bits - 13));
291 tmp = div_s64(*val * 1000000ULL, *val2);
292 *val = div_s64_rem(tmp, 1000000, val2);
294 return IIO_VAL_INT_PLUS_MICRO;
295 case IIO_CHAN_INFO_INT_TIME:
297 *val2 = zopt2201_resolution[data->res].us;
298 return IIO_VAL_INT_PLUS_MICRO;
304 static int zopt2201_set_resolution(struct zopt2201_data *data, u8 res)
308 ret = i2c_smbus_write_byte_data(data->client, ZOPT2201_LS_MEAS_RATE,
309 (res << ZOPT2201_MEAS_RES_SHIFT) |
319 static int zopt2201_write_resolution(struct zopt2201_data *data,
327 for (i = 0; i < ARRAY_SIZE(zopt2201_resolution); i++)
328 if (val2 == zopt2201_resolution[i].us) {
329 mutex_lock(&data->lock);
330 ret = zopt2201_set_resolution(data, i);
331 mutex_unlock(&data->lock);
338 static int zopt2201_set_gain(struct zopt2201_data *data, u8 gain)
342 ret = i2c_smbus_write_byte_data(data->client, ZOPT2201_LS_GAIN, gain);
351 static int zopt2201_write_scale_als_by_idx(struct zopt2201_data *data, int idx)
355 mutex_lock(&data->lock);
356 ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res);
360 ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain);
363 mutex_unlock(&data->lock);
367 static int zopt2201_write_scale_als(struct zopt2201_data *data,
372 for (i = 0; i < ARRAY_SIZE(zopt2201_scale_als); i++)
373 if (val == zopt2201_scale_als[i].scale &&
374 val2 == zopt2201_scale_als[i].uscale) {
375 return zopt2201_write_scale_als_by_idx(data, i);
381 static int zopt2201_write_scale_uvb_by_idx(struct zopt2201_data *data, int idx)
385 mutex_lock(&data->lock);
386 ret = zopt2201_set_resolution(data, zopt2201_scale_als[idx].res);
390 ret = zopt2201_set_gain(data, zopt2201_scale_als[idx].gain);
393 mutex_unlock(&data->lock);
397 static int zopt2201_write_scale_uvb(struct zopt2201_data *data,
402 for (i = 0; i < ARRAY_SIZE(zopt2201_scale_uvb); i++)
403 if (val == zopt2201_scale_uvb[i].scale &&
404 val2 == zopt2201_scale_uvb[i].uscale)
405 return zopt2201_write_scale_uvb_by_idx(data, i);
410 static int zopt2201_write_raw(struct iio_dev *indio_dev,
411 struct iio_chan_spec const *chan,
412 int val, int val2, long mask)
414 struct zopt2201_data *data = iio_priv(indio_dev);
417 case IIO_CHAN_INFO_INT_TIME:
418 return zopt2201_write_resolution(data, val, val2);
419 case IIO_CHAN_INFO_SCALE:
420 switch (chan->address) {
421 case ZOPT2201_ALS_DATA:
422 return zopt2201_write_scale_als(data, val, val2);
423 case ZOPT2201_UVB_DATA:
424 return zopt2201_write_scale_uvb(data, val, val2);
433 static ssize_t zopt2201_show_int_time_available(struct device *dev,
434 struct device_attribute *attr,
440 for (i = 0; i < ARRAY_SIZE(zopt2201_resolution); i++)
441 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06lu ",
442 zopt2201_resolution[i].us);
448 static IIO_DEV_ATTR_INT_TIME_AVAIL(zopt2201_show_int_time_available);
450 static ssize_t zopt2201_show_als_scale_avail(struct device *dev,
451 struct device_attribute *attr,
457 for (i = 0; i < ARRAY_SIZE(zopt2201_scale_als); i++)
458 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
459 zopt2201_scale_als[i].scale,
460 zopt2201_scale_als[i].uscale);
466 static ssize_t zopt2201_show_uvb_scale_avail(struct device *dev,
467 struct device_attribute *attr,
473 for (i = 0; i < ARRAY_SIZE(zopt2201_scale_uvb); i++)
474 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%06u ",
475 zopt2201_scale_uvb[i].scale,
476 zopt2201_scale_uvb[i].uscale);
482 static IIO_DEVICE_ATTR(in_illuminance_scale_available, 0444,
483 zopt2201_show_als_scale_avail, NULL, 0);
484 static IIO_DEVICE_ATTR(in_intensity_uv_scale_available, 0444,
485 zopt2201_show_uvb_scale_avail, NULL, 0);
487 static struct attribute *zopt2201_attributes[] = {
488 &iio_dev_attr_integration_time_available.dev_attr.attr,
489 &iio_dev_attr_in_illuminance_scale_available.dev_attr.attr,
490 &iio_dev_attr_in_intensity_uv_scale_available.dev_attr.attr,
494 static const struct attribute_group zopt2201_attribute_group = {
495 .attrs = zopt2201_attributes,
498 static const struct iio_info zopt2201_info = {
499 .read_raw = zopt2201_read_raw,
500 .write_raw = zopt2201_write_raw,
501 .attrs = &zopt2201_attribute_group,
504 static int zopt2201_probe(struct i2c_client *client)
506 struct zopt2201_data *data;
507 struct iio_dev *indio_dev;
510 if (!i2c_check_functionality(client->adapter,
511 I2C_FUNC_SMBUS_READ_I2C_BLOCK))
514 ret = i2c_smbus_read_byte_data(client, ZOPT2201_PART_ID);
517 if (ret != ZOPT2201_PART_NUMBER)
520 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
524 data = iio_priv(indio_dev);
525 i2c_set_clientdata(client, indio_dev);
526 data->client = client;
527 mutex_init(&data->lock);
529 indio_dev->info = &zopt2201_info;
530 indio_dev->channels = zopt2201_channels;
531 indio_dev->num_channels = ARRAY_SIZE(zopt2201_channels);
532 indio_dev->name = ZOPT2201_DRV_NAME;
533 indio_dev->modes = INDIO_DIRECT_MODE;
535 data->rate = ZOPT2201_MEAS_FREQ_100MS;
536 ret = zopt2201_set_resolution(data, ZOPT2201_MEAS_RES_18BIT);
540 ret = zopt2201_set_gain(data, ZOPT2201_LS_GAIN_3);
544 return devm_iio_device_register(&client->dev, indio_dev);
547 static const struct i2c_device_id zopt2201_id[] = {
551 MODULE_DEVICE_TABLE(i2c, zopt2201_id);
553 static struct i2c_driver zopt2201_driver = {
555 .name = ZOPT2201_DRV_NAME,
557 .probe = zopt2201_probe,
558 .id_table = zopt2201_id,
561 module_i2c_driver(zopt2201_driver);
563 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
564 MODULE_DESCRIPTION("IDT ZOPT2201 ambient light and UV B sensor driver");
565 MODULE_LICENSE("GPL");