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 #define ZOPT2201_DRV_NAME "zopt2201"
25 #define ZOPT2201_MAIN_CTRL 0x00
26 #define ZOPT2201_LS_MEAS_RATE 0x04
27 #define ZOPT2201_LS_GAIN 0x05
28 #define ZOPT2201_PART_ID 0x06
29 #define ZOPT2201_MAIN_STATUS 0x07
30 #define ZOPT2201_ALS_DATA 0x0d /* LSB first, 13 to 20 bits */
31 #define ZOPT2201_UVB_DATA 0x10 /* LSB first, 13 to 20 bits */
32 #define ZOPT2201_UV_COMP_DATA 0x13 /* LSB first, 13 to 20 bits */
33 #define ZOPT2201_COMP_DATA 0x16 /* LSB first, 13 to 20 bits */
34 #define ZOPT2201_INT_CFG 0x19
35 #define ZOPT2201_INT_PST 0x1a
37 #define ZOPT2201_MAIN_CTRL_LS_MODE BIT(3) /* 0 .. ALS, 1 .. UV B */
38 #define ZOPT2201_MAIN_CTRL_LS_EN BIT(1)
40 /* Values for ZOPT2201_LS_MEAS_RATE resolution / bit width */
41 #define ZOPT2201_MEAS_RES_20BIT 0 /* takes 400 ms */
42 #define ZOPT2201_MEAS_RES_19BIT 1 /* takes 200 ms */
43 #define ZOPT2201_MEAS_RES_18BIT 2 /* takes 100 ms, default */
44 #define ZOPT2201_MEAS_RES_17BIT 3 /* takes 50 ms */
45 #define ZOPT2201_MEAS_RES_16BIT 4 /* takes 25 ms */
46 #define ZOPT2201_MEAS_RES_13BIT 5 /* takes 3.125 ms */
47 #define ZOPT2201_MEAS_RES_SHIFT 4
49 /* Values for ZOPT2201_LS_MEAS_RATE measurement rate */
50 #define ZOPT2201_MEAS_FREQ_25MS 0
51 #define ZOPT2201_MEAS_FREQ_50MS 1
52 #define ZOPT2201_MEAS_FREQ_100MS 2 /* default */
53 #define ZOPT2201_MEAS_FREQ_200MS 3
54 #define ZOPT2201_MEAS_FREQ_500MS 4
55 #define ZOPT2201_MEAS_FREQ_1000MS 5
56 #define ZOPT2201_MEAS_FREQ_2000MS 6
58 /* Values for ZOPT2201_LS_GAIN */
59 #define ZOPT2201_LS_GAIN_1 0
60 #define ZOPT2201_LS_GAIN_3 1
61 #define ZOPT2201_LS_GAIN_6 2
62 #define ZOPT2201_LS_GAIN_9 3
63 #define ZOPT2201_LS_GAIN_18 4
65 /* Values for ZOPT2201_MAIN_STATUS */
66 #define ZOPT2201_MAIN_STATUS_POWERON BIT(5)
67 #define ZOPT2201_MAIN_STATUS_INT BIT(4)
68 #define ZOPT2201_MAIN_STATUS_DRDY BIT(3)
70 #define ZOPT2201_PART_NUMBER 0xb2
72 struct zopt2201_data
{
73 struct i2c_client
*client
;
81 unsigned int gain
; /* gain factor */
82 unsigned int scale
; /* micro lux per count */
83 } zopt2201_gain_als
[] = {
92 unsigned int gain
; /* gain factor */
93 unsigned int scale
; /* micro W/m2 per count */
94 } zopt2201_gain_uvb
[] = {
102 static const struct {
103 unsigned int bits
; /* sensor resolution in bits */
104 unsigned long us
; /* measurement time in micro seconds */
105 } zopt2201_resolution
[] = {
114 static const struct {
115 unsigned int scale
, uscale
; /* scale factor as integer + micro */
116 u8 gain
; /* gain register value */
117 u8 res
; /* resolution register value */
118 } zopt2201_scale_als
[] = {
119 { 19, 200000, 0, 5 },
143 static const struct {
144 unsigned int scale
, uscale
; /* scale factor as integer + micro */
145 u8 gain
; /* gain register value */
146 u8 res
; /* resolution register value */
147 } zopt2201_scale_uvb
[] = {
172 static int zopt2201_enable_mode(struct zopt2201_data
*data
, bool uvb_mode
)
174 u8 out
= ZOPT2201_MAIN_CTRL_LS_EN
;
177 out
|= ZOPT2201_MAIN_CTRL_LS_MODE
;
179 return i2c_smbus_write_byte_data(data
->client
, ZOPT2201_MAIN_CTRL
, out
);
182 static int zopt2201_read(struct zopt2201_data
*data
, u8 reg
)
184 struct i2c_client
*client
= data
->client
;
189 mutex_lock(&data
->lock
);
190 ret
= zopt2201_enable_mode(data
, reg
== ZOPT2201_UVB_DATA
);
195 unsigned long t
= zopt2201_resolution
[data
->res
].us
;
198 usleep_range(t
, t
+ 1000);
201 ret
= i2c_smbus_read_byte_data(client
, ZOPT2201_MAIN_STATUS
);
204 if (ret
& ZOPT2201_MAIN_STATUS_DRDY
)
213 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, sizeof(buf
), buf
);
217 ret
= i2c_smbus_write_byte_data(client
, ZOPT2201_MAIN_CTRL
, 0x00);
220 mutex_unlock(&data
->lock
);
222 return (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
225 mutex_unlock(&data
->lock
);
229 static const struct iio_chan_spec zopt2201_channels
[] = {
232 .address
= ZOPT2201_ALS_DATA
,
233 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
234 BIT(IIO_CHAN_INFO_SCALE
),
235 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
),
238 .type
= IIO_INTENSITY
,
240 .channel2
= IIO_MOD_LIGHT_UV
,
241 .address
= ZOPT2201_UVB_DATA
,
242 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
243 BIT(IIO_CHAN_INFO_SCALE
),
244 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
),
248 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
252 static int zopt2201_read_raw(struct iio_dev
*indio_dev
,
253 struct iio_chan_spec
const *chan
,
254 int *val
, int *val2
, long mask
)
256 struct zopt2201_data
*data
= iio_priv(indio_dev
);
261 case IIO_CHAN_INFO_RAW
:
262 ret
= zopt2201_read(data
, chan
->address
);
267 case IIO_CHAN_INFO_PROCESSED
:
268 ret
= zopt2201_read(data
, ZOPT2201_UVB_DATA
);
272 (1 << (20 - zopt2201_resolution
[data
->res
].bits
)) /
273 zopt2201_gain_uvb
[data
->gain
].gain
;
275 case IIO_CHAN_INFO_SCALE
:
276 switch (chan
->address
) {
277 case ZOPT2201_ALS_DATA
:
278 *val
= zopt2201_gain_als
[data
->gain
].scale
;
280 case ZOPT2201_UVB_DATA
:
281 *val
= zopt2201_gain_uvb
[data
->gain
].scale
;
288 *val2
*= (1 << (zopt2201_resolution
[data
->res
].bits
- 13));
289 tmp
= div_s64(*val
* 1000000ULL, *val2
);
290 *val
= div_s64_rem(tmp
, 1000000, val2
);
292 return IIO_VAL_INT_PLUS_MICRO
;
293 case IIO_CHAN_INFO_INT_TIME
:
295 *val2
= zopt2201_resolution
[data
->res
].us
;
296 return IIO_VAL_INT_PLUS_MICRO
;
302 static int zopt2201_set_resolution(struct zopt2201_data
*data
, u8 res
)
306 ret
= i2c_smbus_write_byte_data(data
->client
, ZOPT2201_LS_MEAS_RATE
,
307 (res
<< ZOPT2201_MEAS_RES_SHIFT
) |
317 static int zopt2201_write_resolution(struct zopt2201_data
*data
,
325 for (i
= 0; i
< ARRAY_SIZE(zopt2201_resolution
); i
++)
326 if (val2
== zopt2201_resolution
[i
].us
) {
327 mutex_lock(&data
->lock
);
328 ret
= zopt2201_set_resolution(data
, i
);
329 mutex_unlock(&data
->lock
);
336 static int zopt2201_set_gain(struct zopt2201_data
*data
, u8 gain
)
340 ret
= i2c_smbus_write_byte_data(data
->client
, ZOPT2201_LS_GAIN
, gain
);
349 static int zopt2201_write_scale_als_by_idx(struct zopt2201_data
*data
, int idx
)
353 mutex_lock(&data
->lock
);
354 ret
= zopt2201_set_resolution(data
, zopt2201_scale_als
[idx
].res
);
358 ret
= zopt2201_set_gain(data
, zopt2201_scale_als
[idx
].gain
);
361 mutex_unlock(&data
->lock
);
365 static int zopt2201_write_scale_als(struct zopt2201_data
*data
,
370 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_als
); i
++)
371 if (val
== zopt2201_scale_als
[i
].scale
&&
372 val2
== zopt2201_scale_als
[i
].uscale
) {
373 return zopt2201_write_scale_als_by_idx(data
, i
);
379 static int zopt2201_write_scale_uvb_by_idx(struct zopt2201_data
*data
, int idx
)
383 mutex_lock(&data
->lock
);
384 ret
= zopt2201_set_resolution(data
, zopt2201_scale_als
[idx
].res
);
388 ret
= zopt2201_set_gain(data
, zopt2201_scale_als
[idx
].gain
);
391 mutex_unlock(&data
->lock
);
395 static int zopt2201_write_scale_uvb(struct zopt2201_data
*data
,
400 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_uvb
); i
++)
401 if (val
== zopt2201_scale_uvb
[i
].scale
&&
402 val2
== zopt2201_scale_uvb
[i
].uscale
)
403 return zopt2201_write_scale_uvb_by_idx(data
, i
);
408 static int zopt2201_write_raw(struct iio_dev
*indio_dev
,
409 struct iio_chan_spec
const *chan
,
410 int val
, int val2
, long mask
)
412 struct zopt2201_data
*data
= iio_priv(indio_dev
);
415 case IIO_CHAN_INFO_INT_TIME
:
416 return zopt2201_write_resolution(data
, val
, val2
);
417 case IIO_CHAN_INFO_SCALE
:
418 switch (chan
->address
) {
419 case ZOPT2201_ALS_DATA
:
420 return zopt2201_write_scale_als(data
, val
, val2
);
421 case ZOPT2201_UVB_DATA
:
422 return zopt2201_write_scale_uvb(data
, val
, val2
);
431 static ssize_t
zopt2201_show_int_time_available(struct device
*dev
,
432 struct device_attribute
*attr
,
438 for (i
= 0; i
< ARRAY_SIZE(zopt2201_resolution
); i
++)
439 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06lu ",
440 zopt2201_resolution
[i
].us
);
446 static IIO_DEV_ATTR_INT_TIME_AVAIL(zopt2201_show_int_time_available
);
448 static ssize_t
zopt2201_show_als_scale_avail(struct device
*dev
,
449 struct device_attribute
*attr
,
455 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_als
); i
++)
456 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06u ",
457 zopt2201_scale_als
[i
].scale
,
458 zopt2201_scale_als
[i
].uscale
);
464 static ssize_t
zopt2201_show_uvb_scale_avail(struct device
*dev
,
465 struct device_attribute
*attr
,
471 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_uvb
); i
++)
472 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06u ",
473 zopt2201_scale_uvb
[i
].scale
,
474 zopt2201_scale_uvb
[i
].uscale
);
480 static IIO_DEVICE_ATTR(in_illuminance_scale_available
, 0444,
481 zopt2201_show_als_scale_avail
, NULL
, 0);
482 static IIO_DEVICE_ATTR(in_intensity_uv_scale_available
, 0444,
483 zopt2201_show_uvb_scale_avail
, NULL
, 0);
485 static struct attribute
*zopt2201_attributes
[] = {
486 &iio_dev_attr_integration_time_available
.dev_attr
.attr
,
487 &iio_dev_attr_in_illuminance_scale_available
.dev_attr
.attr
,
488 &iio_dev_attr_in_intensity_uv_scale_available
.dev_attr
.attr
,
492 static const struct attribute_group zopt2201_attribute_group
= {
493 .attrs
= zopt2201_attributes
,
496 static const struct iio_info zopt2201_info
= {
497 .read_raw
= zopt2201_read_raw
,
498 .write_raw
= zopt2201_write_raw
,
499 .attrs
= &zopt2201_attribute_group
,
502 static int zopt2201_probe(struct i2c_client
*client
,
503 const struct i2c_device_id
*id
)
505 struct zopt2201_data
*data
;
506 struct iio_dev
*indio_dev
;
509 if (!i2c_check_functionality(client
->adapter
,
510 I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
513 ret
= i2c_smbus_read_byte_data(client
, ZOPT2201_PART_ID
);
516 if (ret
!= ZOPT2201_PART_NUMBER
)
519 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
523 data
= iio_priv(indio_dev
);
524 i2c_set_clientdata(client
, indio_dev
);
525 data
->client
= client
;
526 mutex_init(&data
->lock
);
528 indio_dev
->dev
.parent
= &client
->dev
;
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");