1 // SPDX-License-Identifier: GPL-2.0-only
3 * AL3320A - Dyna Image Ambient Light Sensor
5 * Copyright (c) 2014, Intel Corporation.
7 * IIO driver for AL3320A (7-bit I2C slave address 0x1C).
9 * TODO: interrupt support, thresholds
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/i2c.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
19 #define AL3320A_DRV_NAME "al3320a"
21 #define AL3320A_REG_CONFIG 0x00
22 #define AL3320A_REG_STATUS 0x01
23 #define AL3320A_REG_INT 0x02
24 #define AL3320A_REG_WAIT 0x06
25 #define AL3320A_REG_CONFIG_RANGE 0x07
26 #define AL3320A_REG_PERSIST 0x08
27 #define AL3320A_REG_MEAN_TIME 0x09
28 #define AL3320A_REG_ADUMMY 0x0A
29 #define AL3320A_REG_DATA_LOW 0x22
31 #define AL3320A_REG_LOW_THRESH_LOW 0x30
32 #define AL3320A_REG_LOW_THRESH_HIGH 0x31
33 #define AL3320A_REG_HIGH_THRESH_LOW 0x32
34 #define AL3320A_REG_HIGH_THRESH_HIGH 0x33
36 #define AL3320A_CONFIG_DISABLE 0x00
37 #define AL3320A_CONFIG_ENABLE 0x01
39 #define AL3320A_GAIN_SHIFT 1
40 #define AL3320A_GAIN_MASK (BIT(2) | BIT(1))
42 /* chip params default values */
43 #define AL3320A_DEFAULT_MEAN_TIME 4
44 #define AL3320A_DEFAULT_WAIT_TIME 0 /* no waiting */
46 #define AL3320A_SCALE_AVAILABLE "0.512 0.128 0.032 0.01"
49 AL3320A_RANGE_1
, /* 33.28 Klx */
50 AL3320A_RANGE_2
, /* 8.32 Klx */
51 AL3320A_RANGE_3
, /* 2.08 Klx */
52 AL3320A_RANGE_4
/* 0.65 Klx */
55 static const int al3320a_scales
[][2] = {
56 {0, 512000}, {0, 128000}, {0, 32000}, {0, 10000}
60 struct i2c_client
*client
;
63 static const struct iio_chan_spec al3320a_channels
[] = {
66 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
67 BIT(IIO_CHAN_INFO_SCALE
),
71 static IIO_CONST_ATTR(in_illuminance_scale_available
, AL3320A_SCALE_AVAILABLE
);
73 static struct attribute
*al3320a_attributes
[] = {
74 &iio_const_attr_in_illuminance_scale_available
.dev_attr
.attr
,
78 static const struct attribute_group al3320a_attribute_group
= {
79 .attrs
= al3320a_attributes
,
82 static int al3320a_init(struct al3320a_data
*data
)
87 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_CONFIG
,
88 AL3320A_CONFIG_ENABLE
);
92 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_CONFIG_RANGE
,
93 AL3320A_RANGE_3
<< AL3320A_GAIN_SHIFT
);
97 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_MEAN_TIME
,
98 AL3320A_DEFAULT_MEAN_TIME
);
102 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_WAIT
,
103 AL3320A_DEFAULT_WAIT_TIME
);
110 static int al3320a_read_raw(struct iio_dev
*indio_dev
,
111 struct iio_chan_spec
const *chan
, int *val
,
112 int *val2
, long mask
)
114 struct al3320a_data
*data
= iio_priv(indio_dev
);
118 case IIO_CHAN_INFO_RAW
:
120 * ALS ADC value is stored in two adjacent registers:
121 * - low byte of output is stored at AL3320A_REG_DATA_LOW
122 * - high byte of output is stored at AL3320A_REG_DATA_LOW + 1
124 ret
= i2c_smbus_read_word_data(data
->client
,
125 AL3320A_REG_DATA_LOW
);
130 case IIO_CHAN_INFO_SCALE
:
131 ret
= i2c_smbus_read_byte_data(data
->client
,
132 AL3320A_REG_CONFIG_RANGE
);
136 ret
= (ret
& AL3320A_GAIN_MASK
) >> AL3320A_GAIN_SHIFT
;
137 *val
= al3320a_scales
[ret
][0];
138 *val2
= al3320a_scales
[ret
][1];
140 return IIO_VAL_INT_PLUS_MICRO
;
145 static int al3320a_write_raw(struct iio_dev
*indio_dev
,
146 struct iio_chan_spec
const *chan
, int val
,
149 struct al3320a_data
*data
= iio_priv(indio_dev
);
153 case IIO_CHAN_INFO_SCALE
:
154 for (i
= 0; i
< ARRAY_SIZE(al3320a_scales
); i
++) {
155 if (val
== al3320a_scales
[i
][0] &&
156 val2
== al3320a_scales
[i
][1])
157 return i2c_smbus_write_byte_data(data
->client
,
158 AL3320A_REG_CONFIG_RANGE
,
159 i
<< AL3320A_GAIN_SHIFT
);
166 static const struct iio_info al3320a_info
= {
167 .read_raw
= al3320a_read_raw
,
168 .write_raw
= al3320a_write_raw
,
169 .attrs
= &al3320a_attribute_group
,
172 static int al3320a_probe(struct i2c_client
*client
,
173 const struct i2c_device_id
*id
)
175 struct al3320a_data
*data
;
176 struct iio_dev
*indio_dev
;
179 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
183 data
= iio_priv(indio_dev
);
184 i2c_set_clientdata(client
, indio_dev
);
185 data
->client
= client
;
187 indio_dev
->dev
.parent
= &client
->dev
;
188 indio_dev
->info
= &al3320a_info
;
189 indio_dev
->name
= AL3320A_DRV_NAME
;
190 indio_dev
->channels
= al3320a_channels
;
191 indio_dev
->num_channels
= ARRAY_SIZE(al3320a_channels
);
192 indio_dev
->modes
= INDIO_DIRECT_MODE
;
194 ret
= al3320a_init(data
);
196 dev_err(&client
->dev
, "al3320a chip init failed\n");
199 return devm_iio_device_register(&client
->dev
, indio_dev
);
202 static int al3320a_remove(struct i2c_client
*client
)
204 return i2c_smbus_write_byte_data(client
, AL3320A_REG_CONFIG
,
205 AL3320A_CONFIG_DISABLE
);
208 static const struct i2c_device_id al3320a_id
[] = {
212 MODULE_DEVICE_TABLE(i2c
, al3320a_id
);
214 static struct i2c_driver al3320a_driver
= {
216 .name
= AL3320A_DRV_NAME
,
218 .probe
= al3320a_probe
,
219 .remove
= al3320a_remove
,
220 .id_table
= al3320a_id
,
223 module_i2c_driver(al3320a_driver
);
225 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
226 MODULE_DESCRIPTION("AL3320A Ambient Light Sensor driver");
227 MODULE_LICENSE("GPL v2");