1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * AL3320A - Dyna Image Ambient Light Sensor |
4 | * |
5 | * Copyright (c) 2014, Intel Corporation. |
6 | * |
7 | * IIO driver for AL3320A (7-bit I2C slave address 0x1C). |
8 | * |
9 | * TODO: interrupt support, thresholds |
10 | * When the driver will get support for interrupt handling, then interrupt |
11 | * will need to be disabled before turning sensor OFF in order to avoid |
12 | * potential races with the interrupt handling. |
13 | */ |
14 | |
15 | #include <linux/bitfield.h> |
16 | #include <linux/i2c.h> |
17 | #include <linux/module.h> |
18 | #include <linux/of.h> |
19 | #include <linux/mod_devicetable.h> |
20 | |
21 | #include <linux/iio/iio.h> |
22 | #include <linux/iio/sysfs.h> |
23 | |
24 | #define AL3320A_DRV_NAME "al3320a" |
25 | |
26 | #define AL3320A_REG_CONFIG 0x00 |
27 | #define AL3320A_REG_STATUS 0x01 |
28 | #define AL3320A_REG_INT 0x02 |
29 | #define AL3320A_REG_WAIT 0x06 |
30 | #define AL3320A_REG_CONFIG_RANGE 0x07 |
31 | #define AL3320A_REG_PERSIST 0x08 |
32 | #define AL3320A_REG_MEAN_TIME 0x09 |
33 | #define AL3320A_REG_ADUMMY 0x0A |
34 | #define AL3320A_REG_DATA_LOW 0x22 |
35 | |
36 | #define AL3320A_REG_LOW_THRESH_LOW 0x30 |
37 | #define AL3320A_REG_LOW_THRESH_HIGH 0x31 |
38 | #define AL3320A_REG_HIGH_THRESH_LOW 0x32 |
39 | #define AL3320A_REG_HIGH_THRESH_HIGH 0x33 |
40 | |
41 | #define AL3320A_CONFIG_DISABLE 0x00 |
42 | #define AL3320A_CONFIG_ENABLE 0x01 |
43 | |
44 | #define AL3320A_GAIN_MASK GENMASK(2, 1) |
45 | |
46 | /* chip params default values */ |
47 | #define AL3320A_DEFAULT_MEAN_TIME 4 |
48 | #define AL3320A_DEFAULT_WAIT_TIME 0 /* no waiting */ |
49 | |
50 | #define AL3320A_SCALE_AVAILABLE "0.512 0.128 0.032 0.01" |
51 | |
52 | enum al3320a_range { |
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 */ |
57 | }; |
58 | |
59 | static const int al3320a_scales[][2] = { |
60 | {0, 512000}, {0, 128000}, {0, 32000}, {0, 10000} |
61 | }; |
62 | |
63 | struct al3320a_data { |
64 | struct i2c_client *client; |
65 | }; |
66 | |
67 | static const struct iio_chan_spec al3320a_channels[] = { |
68 | { |
69 | .type = IIO_LIGHT, |
70 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
71 | BIT(IIO_CHAN_INFO_SCALE), |
72 | } |
73 | }; |
74 | |
75 | static IIO_CONST_ATTR(in_illuminance_scale_available, AL3320A_SCALE_AVAILABLE); |
76 | |
77 | static struct attribute *al3320a_attributes[] = { |
78 | &iio_const_attr_in_illuminance_scale_available.dev_attr.attr, |
79 | NULL, |
80 | }; |
81 | |
82 | static const struct attribute_group al3320a_attribute_group = { |
83 | .attrs = al3320a_attributes, |
84 | }; |
85 | |
86 | static int al3320a_set_pwr(struct i2c_client *client, bool pwr) |
87 | { |
88 | u8 val = pwr ? AL3320A_CONFIG_ENABLE : AL3320A_CONFIG_DISABLE; |
89 | return i2c_smbus_write_byte_data(client, AL3320A_REG_CONFIG, value: val); |
90 | } |
91 | |
92 | static void al3320a_set_pwr_off(void *_data) |
93 | { |
94 | struct al3320a_data *data = _data; |
95 | |
96 | al3320a_set_pwr(client: data->client, pwr: false); |
97 | } |
98 | |
99 | static int al3320a_init(struct al3320a_data *data) |
100 | { |
101 | int ret; |
102 | |
103 | ret = al3320a_set_pwr(client: data->client, pwr: true); |
104 | |
105 | if (ret < 0) |
106 | return ret; |
107 | |
108 | ret = i2c_smbus_write_byte_data(client: data->client, AL3320A_REG_CONFIG_RANGE, |
109 | FIELD_PREP(AL3320A_GAIN_MASK, |
110 | AL3320A_RANGE_3)); |
111 | if (ret < 0) |
112 | return ret; |
113 | |
114 | ret = i2c_smbus_write_byte_data(client: data->client, AL3320A_REG_MEAN_TIME, |
115 | AL3320A_DEFAULT_MEAN_TIME); |
116 | if (ret < 0) |
117 | return ret; |
118 | |
119 | ret = i2c_smbus_write_byte_data(client: data->client, AL3320A_REG_WAIT, |
120 | AL3320A_DEFAULT_WAIT_TIME); |
121 | if (ret < 0) |
122 | return ret; |
123 | |
124 | return 0; |
125 | } |
126 | |
127 | static int al3320a_read_raw(struct iio_dev *indio_dev, |
128 | struct iio_chan_spec const *chan, int *val, |
129 | int *val2, long mask) |
130 | { |
131 | struct al3320a_data *data = iio_priv(indio_dev); |
132 | int ret; |
133 | |
134 | switch (mask) { |
135 | case IIO_CHAN_INFO_RAW: |
136 | /* |
137 | * ALS ADC value is stored in two adjacent registers: |
138 | * - low byte of output is stored at AL3320A_REG_DATA_LOW |
139 | * - high byte of output is stored at AL3320A_REG_DATA_LOW + 1 |
140 | */ |
141 | ret = i2c_smbus_read_word_data(client: data->client, |
142 | AL3320A_REG_DATA_LOW); |
143 | if (ret < 0) |
144 | return ret; |
145 | *val = ret; |
146 | return IIO_VAL_INT; |
147 | case IIO_CHAN_INFO_SCALE: |
148 | ret = i2c_smbus_read_byte_data(client: data->client, |
149 | AL3320A_REG_CONFIG_RANGE); |
150 | if (ret < 0) |
151 | return ret; |
152 | |
153 | ret = FIELD_GET(AL3320A_GAIN_MASK, ret); |
154 | *val = al3320a_scales[ret][0]; |
155 | *val2 = al3320a_scales[ret][1]; |
156 | |
157 | return IIO_VAL_INT_PLUS_MICRO; |
158 | } |
159 | return -EINVAL; |
160 | } |
161 | |
162 | static int al3320a_write_raw(struct iio_dev *indio_dev, |
163 | struct iio_chan_spec const *chan, int val, |
164 | int val2, long mask) |
165 | { |
166 | struct al3320a_data *data = iio_priv(indio_dev); |
167 | int i; |
168 | |
169 | switch (mask) { |
170 | case IIO_CHAN_INFO_SCALE: |
171 | for (i = 0; i < ARRAY_SIZE(al3320a_scales); i++) { |
172 | if (val != al3320a_scales[i][0] || |
173 | val2 != al3320a_scales[i][1]) |
174 | continue; |
175 | |
176 | return i2c_smbus_write_byte_data(client: data->client, |
177 | AL3320A_REG_CONFIG_RANGE, |
178 | FIELD_PREP(AL3320A_GAIN_MASK, i)); |
179 | } |
180 | break; |
181 | } |
182 | return -EINVAL; |
183 | } |
184 | |
185 | static const struct iio_info al3320a_info = { |
186 | .read_raw = al3320a_read_raw, |
187 | .write_raw = al3320a_write_raw, |
188 | .attrs = &al3320a_attribute_group, |
189 | }; |
190 | |
191 | static int al3320a_probe(struct i2c_client *client) |
192 | { |
193 | struct al3320a_data *data; |
194 | struct iio_dev *indio_dev; |
195 | int ret; |
196 | |
197 | indio_dev = devm_iio_device_alloc(parent: &client->dev, sizeof_priv: sizeof(*data)); |
198 | if (!indio_dev) |
199 | return -ENOMEM; |
200 | |
201 | data = iio_priv(indio_dev); |
202 | i2c_set_clientdata(client, data: indio_dev); |
203 | data->client = client; |
204 | |
205 | indio_dev->info = &al3320a_info; |
206 | indio_dev->name = AL3320A_DRV_NAME; |
207 | indio_dev->channels = al3320a_channels; |
208 | indio_dev->num_channels = ARRAY_SIZE(al3320a_channels); |
209 | indio_dev->modes = INDIO_DIRECT_MODE; |
210 | |
211 | ret = al3320a_init(data); |
212 | if (ret < 0) { |
213 | dev_err(&client->dev, "al3320a chip init failed\n" ); |
214 | return ret; |
215 | } |
216 | |
217 | ret = devm_add_action_or_reset(&client->dev, |
218 | al3320a_set_pwr_off, |
219 | data); |
220 | if (ret < 0) |
221 | return ret; |
222 | |
223 | return devm_iio_device_register(&client->dev, indio_dev); |
224 | } |
225 | |
226 | static int al3320a_suspend(struct device *dev) |
227 | { |
228 | return al3320a_set_pwr(to_i2c_client(dev), pwr: false); |
229 | } |
230 | |
231 | static int al3320a_resume(struct device *dev) |
232 | { |
233 | return al3320a_set_pwr(to_i2c_client(dev), pwr: true); |
234 | } |
235 | |
236 | static DEFINE_SIMPLE_DEV_PM_OPS(al3320a_pm_ops, al3320a_suspend, |
237 | al3320a_resume); |
238 | |
239 | static const struct i2c_device_id al3320a_id[] = { |
240 | {"al3320a" , 0}, |
241 | {} |
242 | }; |
243 | MODULE_DEVICE_TABLE(i2c, al3320a_id); |
244 | |
245 | static const struct of_device_id al3320a_of_match[] = { |
246 | { .compatible = "dynaimage,al3320a" , }, |
247 | {}, |
248 | }; |
249 | MODULE_DEVICE_TABLE(of, al3320a_of_match); |
250 | |
251 | static const struct acpi_device_id al3320a_acpi_match[] = { |
252 | {"CALS0001" }, |
253 | { }, |
254 | }; |
255 | MODULE_DEVICE_TABLE(acpi, al3320a_acpi_match); |
256 | |
257 | static struct i2c_driver al3320a_driver = { |
258 | .driver = { |
259 | .name = AL3320A_DRV_NAME, |
260 | .of_match_table = al3320a_of_match, |
261 | .pm = pm_sleep_ptr(&al3320a_pm_ops), |
262 | .acpi_match_table = al3320a_acpi_match, |
263 | }, |
264 | .probe = al3320a_probe, |
265 | .id_table = al3320a_id, |
266 | }; |
267 | |
268 | module_i2c_driver(al3320a_driver); |
269 | |
270 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>" ); |
271 | MODULE_DESCRIPTION("AL3320A Ambient Light Sensor driver" ); |
272 | MODULE_LICENSE("GPL v2" ); |
273 | |