2 * AL3320A - Dyna Image Ambient Light Sensor
4 * Copyright (c) 2014, Intel Corporation.
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 * IIO driver for AL3320A (7-bit I2C slave address 0x1C).
12 * TODO: interrupt support, thresholds
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/i2c.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
23 #define AL3320A_DRV_NAME "al3320a"
25 #define AL3320A_REG_CONFIG 0x00
26 #define AL3320A_REG_STATUS 0x01
27 #define AL3320A_REG_INT 0x02
28 #define AL3320A_REG_WAIT 0x06
29 #define AL3320A_REG_CONFIG_RANGE 0x07
30 #define AL3320A_REG_PERSIST 0x08
31 #define AL3320A_REG_MEAN_TIME 0x09
32 #define AL3320A_REG_ADUMMY 0x0A
33 #define AL3320A_REG_DATA_LOW 0x22
35 #define AL3320A_REG_LOW_THRESH_LOW 0x30
36 #define AL3320A_REG_LOW_THRESH_HIGH 0x31
37 #define AL3320A_REG_HIGH_THRESH_LOW 0x32
38 #define AL3320A_REG_HIGH_THRESH_HIGH 0x33
40 #define AL3320A_CONFIG_DISABLE 0x00
41 #define AL3320A_CONFIG_ENABLE 0x01
43 #define AL3320A_GAIN_SHIFT 1
44 #define AL3320A_GAIN_MASK (BIT(2) | BIT(1))
46 /* chip params default values */
47 #define AL3320A_DEFAULT_MEAN_TIME 4
48 #define AL3320A_DEFAULT_WAIT_TIME 0 /* no waiting */
50 #define AL3320A_SCALE_AVAILABLE "0.512 0.128 0.032 0.01"
53 AL3320A_RANGE_1
, /* 33.28 Klx */
54 AL3320A_RANGE_2
, /* 8.32 Klx */
55 AL3320A_RANGE_3
, /* 2.08 Klx */
56 AL3320A_RANGE_4
/* 0.65 Klx */
59 static const int al3320a_scales
[][2] = {
60 {0, 512000}, {0, 128000}, {0, 32000}, {0, 10000}
64 struct i2c_client
*client
;
67 static const struct iio_chan_spec al3320a_channels
[] = {
70 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
71 BIT(IIO_CHAN_INFO_SCALE
),
75 static IIO_CONST_ATTR(in_illuminance_scale_available
, AL3320A_SCALE_AVAILABLE
);
77 static struct attribute
*al3320a_attributes
[] = {
78 &iio_const_attr_in_illuminance_scale_available
.dev_attr
.attr
,
82 static const struct attribute_group al3320a_attribute_group
= {
83 .attrs
= al3320a_attributes
,
86 static int al3320a_init(struct al3320a_data
*data
)
91 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_CONFIG
,
92 AL3320A_CONFIG_ENABLE
);
96 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_CONFIG_RANGE
,
97 AL3320A_RANGE_3
<< AL3320A_GAIN_SHIFT
);
101 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_MEAN_TIME
,
102 AL3320A_DEFAULT_MEAN_TIME
);
106 ret
= i2c_smbus_write_byte_data(data
->client
, AL3320A_REG_WAIT
,
107 AL3320A_DEFAULT_WAIT_TIME
);
114 static int al3320a_read_raw(struct iio_dev
*indio_dev
,
115 struct iio_chan_spec
const *chan
, int *val
,
116 int *val2
, long mask
)
118 struct al3320a_data
*data
= iio_priv(indio_dev
);
122 case IIO_CHAN_INFO_RAW
:
124 * ALS ADC value is stored in two adjacent registers:
125 * - low byte of output is stored at AL3320A_REG_DATA_LOW
126 * - high byte of output is stored at AL3320A_REG_DATA_LOW + 1
128 ret
= i2c_smbus_read_word_data(data
->client
,
129 AL3320A_REG_DATA_LOW
);
134 case IIO_CHAN_INFO_SCALE
:
135 ret
= i2c_smbus_read_byte_data(data
->client
,
136 AL3320A_REG_CONFIG_RANGE
);
140 ret
= (ret
& AL3320A_GAIN_MASK
) >> AL3320A_GAIN_SHIFT
;
141 *val
= al3320a_scales
[ret
][0];
142 *val2
= al3320a_scales
[ret
][1];
144 return IIO_VAL_INT_PLUS_MICRO
;
149 static int al3320a_write_raw(struct iio_dev
*indio_dev
,
150 struct iio_chan_spec
const *chan
, int val
,
153 struct al3320a_data
*data
= iio_priv(indio_dev
);
157 case IIO_CHAN_INFO_SCALE
:
158 for (i
= 0; i
< ARRAY_SIZE(al3320a_scales
); i
++) {
159 if (val
== al3320a_scales
[i
][0] &&
160 val2
== al3320a_scales
[i
][1])
161 return i2c_smbus_write_byte_data(data
->client
,
162 AL3320A_REG_CONFIG_RANGE
,
163 i
<< AL3320A_GAIN_SHIFT
);
170 static const struct iio_info al3320a_info
= {
171 .driver_module
= THIS_MODULE
,
172 .read_raw
= al3320a_read_raw
,
173 .write_raw
= al3320a_write_raw
,
174 .attrs
= &al3320a_attribute_group
,
177 static int al3320a_probe(struct i2c_client
*client
,
178 const struct i2c_device_id
*id
)
180 struct al3320a_data
*data
;
181 struct iio_dev
*indio_dev
;
184 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
188 data
= iio_priv(indio_dev
);
189 i2c_set_clientdata(client
, indio_dev
);
190 data
->client
= client
;
192 indio_dev
->dev
.parent
= &client
->dev
;
193 indio_dev
->info
= &al3320a_info
;
194 indio_dev
->name
= AL3320A_DRV_NAME
;
195 indio_dev
->channels
= al3320a_channels
;
196 indio_dev
->num_channels
= ARRAY_SIZE(al3320a_channels
);
197 indio_dev
->modes
= INDIO_DIRECT_MODE
;
199 ret
= al3320a_init(data
);
201 dev_err(&client
->dev
, "al3320a chip init failed\n");
204 return devm_iio_device_register(&client
->dev
, indio_dev
);
207 static int al3320a_remove(struct i2c_client
*client
)
209 return i2c_smbus_write_byte_data(client
, AL3320A_REG_CONFIG
,
210 AL3320A_CONFIG_DISABLE
);
213 static const struct i2c_device_id al3320a_id
[] = {
217 MODULE_DEVICE_TABLE(i2c
, al3320a_id
);
219 static struct i2c_driver al3320a_driver
= {
221 .name
= AL3320A_DRV_NAME
,
223 .probe
= al3320a_probe
,
224 .remove
= al3320a_remove
,
225 .id_table
= al3320a_id
,
228 module_i2c_driver(al3320a_driver
);
230 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
231 MODULE_DESCRIPTION("AL3320A Ambient Light Sensor driver");
232 MODULE_LICENSE("GPL v2");