2 * zopt2201.c - Support for IDT ZOPT2201 ambient light and UV B sensor
4 * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * Datasheet: https://www.idt.com/document/dst/zopt2201-datasheet
11 * 7-bit I2C slave addresses 0x53 (default) or 0x52 (programmed)
13 * TODO: interrupt support, ALS/UVB raw mode
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/mutex.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 #define ZOPT2201_DRV_NAME "zopt2201"
28 #define ZOPT2201_MAIN_CTRL 0x00
29 #define ZOPT2201_LS_MEAS_RATE 0x04
30 #define ZOPT2201_LS_GAIN 0x05
31 #define ZOPT2201_PART_ID 0x06
32 #define ZOPT2201_MAIN_STATUS 0x07
33 #define ZOPT2201_ALS_DATA 0x0d /* LSB first, 13 to 20 bits */
34 #define ZOPT2201_UVB_DATA 0x10 /* LSB first, 13 to 20 bits */
35 #define ZOPT2201_UV_COMP_DATA 0x13 /* LSB first, 13 to 20 bits */
36 #define ZOPT2201_COMP_DATA 0x16 /* LSB first, 13 to 20 bits */
37 #define ZOPT2201_INT_CFG 0x19
38 #define ZOPT2201_INT_PST 0x1a
40 #define ZOPT2201_MAIN_CTRL_LS_MODE BIT(3) /* 0 .. ALS, 1 .. UV B */
41 #define ZOPT2201_MAIN_CTRL_LS_EN BIT(1)
43 /* Values for ZOPT2201_LS_MEAS_RATE resolution / bit width */
44 #define ZOPT2201_MEAS_RES_20BIT 0 /* takes 400 ms */
45 #define ZOPT2201_MEAS_RES_19BIT 1 /* takes 200 ms */
46 #define ZOPT2201_MEAS_RES_18BIT 2 /* takes 100 ms, default */
47 #define ZOPT2201_MEAS_RES_17BIT 3 /* takes 50 ms */
48 #define ZOPT2201_MEAS_RES_16BIT 4 /* takes 25 ms */
49 #define ZOPT2201_MEAS_RES_13BIT 5 /* takes 3.125 ms */
50 #define ZOPT2201_MEAS_RES_SHIFT 4
52 /* Values for ZOPT2201_LS_MEAS_RATE measurement rate */
53 #define ZOPT2201_MEAS_FREQ_25MS 0
54 #define ZOPT2201_MEAS_FREQ_50MS 1
55 #define ZOPT2201_MEAS_FREQ_100MS 2 /* default */
56 #define ZOPT2201_MEAS_FREQ_200MS 3
57 #define ZOPT2201_MEAS_FREQ_500MS 4
58 #define ZOPT2201_MEAS_FREQ_1000MS 5
59 #define ZOPT2201_MEAS_FREQ_2000MS 6
61 /* Values for ZOPT2201_LS_GAIN */
62 #define ZOPT2201_LS_GAIN_1 0
63 #define ZOPT2201_LS_GAIN_3 1
64 #define ZOPT2201_LS_GAIN_6 2
65 #define ZOPT2201_LS_GAIN_9 3
66 #define ZOPT2201_LS_GAIN_18 4
68 /* Values for ZOPT2201_MAIN_STATUS */
69 #define ZOPT2201_MAIN_STATUS_POWERON BIT(5)
70 #define ZOPT2201_MAIN_STATUS_INT BIT(4)
71 #define ZOPT2201_MAIN_STATUS_DRDY BIT(3)
73 #define ZOPT2201_PART_NUMBER 0xb2
75 struct zopt2201_data
{
76 struct i2c_client
*client
;
84 unsigned int gain
; /* gain factor */
85 unsigned int scale
; /* micro lux per count */
86 } zopt2201_gain_als
[] = {
95 unsigned int gain
; /* gain factor */
96 unsigned int scale
; /* micro W/m2 per count */
97 } zopt2201_gain_uvb
[] = {
105 static const struct {
106 unsigned int bits
; /* sensor resolution in bits */
107 unsigned long us
; /* measurement time in micro seconds */
108 } zopt2201_resolution
[] = {
117 static const struct {
118 unsigned int scale
, uscale
; /* scale factor as integer + micro */
119 u8 gain
; /* gain register value */
120 u8 res
; /* resolution register value */
121 } zopt2201_scale_als
[] = {
122 { 19, 200000, 0, 5 },
146 static const struct {
147 unsigned int scale
, uscale
; /* scale factor as integer + micro */
148 u8 gain
; /* gain register value */
149 u8 res
; /* resolution register value */
150 } zopt2201_scale_uvb
[] = {
175 static int zopt2201_enable_mode(struct zopt2201_data
*data
, bool uvb_mode
)
177 u8 out
= ZOPT2201_MAIN_CTRL_LS_EN
;
180 out
|= ZOPT2201_MAIN_CTRL_LS_MODE
;
182 return i2c_smbus_write_byte_data(data
->client
, ZOPT2201_MAIN_CTRL
, out
);
185 static int zopt2201_read(struct zopt2201_data
*data
, u8 reg
)
187 struct i2c_client
*client
= data
->client
;
192 mutex_lock(&data
->lock
);
193 ret
= zopt2201_enable_mode(data
, reg
== ZOPT2201_UVB_DATA
);
198 unsigned long t
= zopt2201_resolution
[data
->res
].us
;
201 usleep_range(t
, t
+ 1000);
204 ret
= i2c_smbus_read_byte_data(client
, ZOPT2201_MAIN_STATUS
);
207 if (ret
& ZOPT2201_MAIN_STATUS_DRDY
)
216 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, sizeof(buf
), buf
);
220 ret
= i2c_smbus_write_byte_data(client
, ZOPT2201_MAIN_CTRL
, 0x00);
223 mutex_unlock(&data
->lock
);
225 return (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
228 mutex_unlock(&data
->lock
);
232 static const struct iio_chan_spec zopt2201_channels
[] = {
235 .address
= ZOPT2201_ALS_DATA
,
236 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
237 BIT(IIO_CHAN_INFO_SCALE
),
238 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
),
241 .type
= IIO_INTENSITY
,
243 .channel2
= IIO_MOD_LIGHT_UV
,
244 .address
= ZOPT2201_UVB_DATA
,
245 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
246 BIT(IIO_CHAN_INFO_SCALE
),
247 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_INT_TIME
),
251 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
255 static int zopt2201_read_raw(struct iio_dev
*indio_dev
,
256 struct iio_chan_spec
const *chan
,
257 int *val
, int *val2
, long mask
)
259 struct zopt2201_data
*data
= iio_priv(indio_dev
);
264 case IIO_CHAN_INFO_RAW
:
265 ret
= zopt2201_read(data
, chan
->address
);
270 case IIO_CHAN_INFO_PROCESSED
:
271 ret
= zopt2201_read(data
, ZOPT2201_UVB_DATA
);
275 (1 << (20 - zopt2201_resolution
[data
->res
].bits
)) /
276 zopt2201_gain_uvb
[data
->gain
].gain
;
278 case IIO_CHAN_INFO_SCALE
:
279 switch (chan
->address
) {
280 case ZOPT2201_ALS_DATA
:
281 *val
= zopt2201_gain_als
[data
->gain
].scale
;
283 case ZOPT2201_UVB_DATA
:
284 *val
= zopt2201_gain_uvb
[data
->gain
].scale
;
291 *val2
*= (1 << (zopt2201_resolution
[data
->res
].bits
- 13));
292 tmp
= div_s64(*val
* 1000000ULL, *val2
);
293 *val
= div_s64_rem(tmp
, 1000000, val2
);
295 return IIO_VAL_INT_PLUS_MICRO
;
296 case IIO_CHAN_INFO_INT_TIME
:
298 *val2
= zopt2201_resolution
[data
->res
].us
;
299 return IIO_VAL_INT_PLUS_MICRO
;
305 static int zopt2201_set_resolution(struct zopt2201_data
*data
, u8 res
)
309 ret
= i2c_smbus_write_byte_data(data
->client
, ZOPT2201_LS_MEAS_RATE
,
310 (res
<< ZOPT2201_MEAS_RES_SHIFT
) |
320 static int zopt2201_write_resolution(struct zopt2201_data
*data
,
328 for (i
= 0; i
< ARRAY_SIZE(zopt2201_resolution
); i
++)
329 if (val2
== zopt2201_resolution
[i
].us
) {
330 mutex_lock(&data
->lock
);
331 ret
= zopt2201_set_resolution(data
, i
);
332 mutex_unlock(&data
->lock
);
339 static int zopt2201_set_gain(struct zopt2201_data
*data
, u8 gain
)
343 ret
= i2c_smbus_write_byte_data(data
->client
, ZOPT2201_LS_GAIN
, gain
);
352 static int zopt2201_write_scale_als_by_idx(struct zopt2201_data
*data
, int idx
)
356 mutex_lock(&data
->lock
);
357 ret
= zopt2201_set_resolution(data
, zopt2201_scale_als
[idx
].res
);
361 ret
= zopt2201_set_gain(data
, zopt2201_scale_als
[idx
].gain
);
364 mutex_unlock(&data
->lock
);
368 static int zopt2201_write_scale_als(struct zopt2201_data
*data
,
373 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_als
); i
++)
374 if (val
== zopt2201_scale_als
[i
].scale
&&
375 val2
== zopt2201_scale_als
[i
].uscale
) {
376 return zopt2201_write_scale_als_by_idx(data
, i
);
382 static int zopt2201_write_scale_uvb_by_idx(struct zopt2201_data
*data
, int idx
)
386 mutex_lock(&data
->lock
);
387 ret
= zopt2201_set_resolution(data
, zopt2201_scale_als
[idx
].res
);
391 ret
= zopt2201_set_gain(data
, zopt2201_scale_als
[idx
].gain
);
394 mutex_unlock(&data
->lock
);
398 static int zopt2201_write_scale_uvb(struct zopt2201_data
*data
,
403 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_uvb
); i
++)
404 if (val
== zopt2201_scale_uvb
[i
].scale
&&
405 val2
== zopt2201_scale_uvb
[i
].uscale
)
406 return zopt2201_write_scale_uvb_by_idx(data
, i
);
411 static int zopt2201_write_raw(struct iio_dev
*indio_dev
,
412 struct iio_chan_spec
const *chan
,
413 int val
, int val2
, long mask
)
415 struct zopt2201_data
*data
= iio_priv(indio_dev
);
418 case IIO_CHAN_INFO_INT_TIME
:
419 return zopt2201_write_resolution(data
, val
, val2
);
420 case IIO_CHAN_INFO_SCALE
:
421 switch (chan
->address
) {
422 case ZOPT2201_ALS_DATA
:
423 return zopt2201_write_scale_als(data
, val
, val2
);
424 case ZOPT2201_UVB_DATA
:
425 return zopt2201_write_scale_uvb(data
, val
, val2
);
434 static ssize_t
zopt2201_show_int_time_available(struct device
*dev
,
435 struct device_attribute
*attr
,
441 for (i
= 0; i
< ARRAY_SIZE(zopt2201_resolution
); i
++)
442 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06lu ",
443 zopt2201_resolution
[i
].us
);
449 static IIO_DEV_ATTR_INT_TIME_AVAIL(zopt2201_show_int_time_available
);
451 static ssize_t
zopt2201_show_als_scale_avail(struct device
*dev
,
452 struct device_attribute
*attr
,
458 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_als
); i
++)
459 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06u ",
460 zopt2201_scale_als
[i
].scale
,
461 zopt2201_scale_als
[i
].uscale
);
467 static ssize_t
zopt2201_show_uvb_scale_avail(struct device
*dev
,
468 struct device_attribute
*attr
,
474 for (i
= 0; i
< ARRAY_SIZE(zopt2201_scale_uvb
); i
++)
475 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%06u ",
476 zopt2201_scale_uvb
[i
].scale
,
477 zopt2201_scale_uvb
[i
].uscale
);
483 static IIO_DEVICE_ATTR(in_illuminance_scale_available
, 0444,
484 zopt2201_show_als_scale_avail
, NULL
, 0);
485 static IIO_DEVICE_ATTR(in_intensity_uv_scale_available
, 0444,
486 zopt2201_show_uvb_scale_avail
, NULL
, 0);
488 static struct attribute
*zopt2201_attributes
[] = {
489 &iio_dev_attr_integration_time_available
.dev_attr
.attr
,
490 &iio_dev_attr_in_illuminance_scale_available
.dev_attr
.attr
,
491 &iio_dev_attr_in_intensity_uv_scale_available
.dev_attr
.attr
,
495 static const struct attribute_group zopt2201_attribute_group
= {
496 .attrs
= zopt2201_attributes
,
499 static const struct iio_info zopt2201_info
= {
500 .read_raw
= zopt2201_read_raw
,
501 .write_raw
= zopt2201_write_raw
,
502 .attrs
= &zopt2201_attribute_group
,
505 static int zopt2201_probe(struct i2c_client
*client
,
506 const struct i2c_device_id
*id
)
508 struct zopt2201_data
*data
;
509 struct iio_dev
*indio_dev
;
512 if (!i2c_check_functionality(client
->adapter
,
513 I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
516 ret
= i2c_smbus_read_byte_data(client
, ZOPT2201_PART_ID
);
519 if (ret
!= ZOPT2201_PART_NUMBER
)
522 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
526 data
= iio_priv(indio_dev
);
527 i2c_set_clientdata(client
, indio_dev
);
528 data
->client
= client
;
529 mutex_init(&data
->lock
);
531 indio_dev
->dev
.parent
= &client
->dev
;
532 indio_dev
->info
= &zopt2201_info
;
533 indio_dev
->channels
= zopt2201_channels
;
534 indio_dev
->num_channels
= ARRAY_SIZE(zopt2201_channels
);
535 indio_dev
->name
= ZOPT2201_DRV_NAME
;
536 indio_dev
->modes
= INDIO_DIRECT_MODE
;
538 data
->rate
= ZOPT2201_MEAS_FREQ_100MS
;
539 ret
= zopt2201_set_resolution(data
, ZOPT2201_MEAS_RES_18BIT
);
543 ret
= zopt2201_set_gain(data
, ZOPT2201_LS_GAIN_3
);
547 return devm_iio_device_register(&client
->dev
, indio_dev
);
550 static const struct i2c_device_id zopt2201_id
[] = {
554 MODULE_DEVICE_TABLE(i2c
, zopt2201_id
);
556 static struct i2c_driver zopt2201_driver
= {
558 .name
= ZOPT2201_DRV_NAME
,
560 .probe
= zopt2201_probe
,
561 .id_table
= zopt2201_id
,
564 module_i2c_driver(zopt2201_driver
);
566 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
567 MODULE_DESCRIPTION("IDT ZOPT2201 ambient light and UV B sensor driver");
568 MODULE_LICENSE("GPL");