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