xref: /linux/drivers/iio/light/al3320a.c (revision b8ea7da314c2efcb9c2f559ed65b7a36c869d68e)
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 
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 
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 
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 
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;
139 	__le16 raw;
140 
141 	switch (mask) {
142 	case IIO_CHAN_INFO_RAW:
143 		/*
144 		 * ALS ADC value is stored in two adjacent registers:
145 		 * - low byte of output is stored at AL3320A_REG_DATA_LOW
146 		 * - high byte of output is stored at AL3320A_REG_DATA_LOW + 1
147 		 */
148 		ret = regmap_bulk_read(data->regmap, AL3320A_REG_DATA_LOW,
149 				       &raw, sizeof(raw));
150 		if (ret)
151 			return ret;
152 
153 		*val = le16_to_cpu(raw);
154 
155 		return IIO_VAL_INT;
156 	case IIO_CHAN_INFO_SCALE:
157 		ret = regmap_read(data->regmap, AL3320A_REG_CONFIG_RANGE, &gain);
158 		if (ret)
159 			return ret;
160 
161 		gain = FIELD_GET(AL3320A_GAIN_MASK, gain);
162 		*val = al3320a_scales[gain][0];
163 		*val2 = al3320a_scales[gain][1];
164 
165 		return IIO_VAL_INT_PLUS_MICRO;
166 	}
167 	return -EINVAL;
168 }
169 
170 static int al3320a_write_raw(struct iio_dev *indio_dev,
171 			     struct iio_chan_spec const *chan, int val,
172 			     int val2, long mask)
173 {
174 	struct al3320a_data *data = iio_priv(indio_dev);
175 	unsigned int i;
176 
177 	switch (mask) {
178 	case IIO_CHAN_INFO_SCALE:
179 		for (i = 0; i < ARRAY_SIZE(al3320a_scales); i++) {
180 			if (val != al3320a_scales[i][0] ||
181 			    val2 != al3320a_scales[i][1])
182 				continue;
183 
184 			return regmap_write(data->regmap, AL3320A_REG_CONFIG_RANGE,
185 					    FIELD_PREP(AL3320A_GAIN_MASK, i));
186 		}
187 		break;
188 	}
189 	return -EINVAL;
190 }
191 
192 static const struct iio_info al3320a_info = {
193 	.read_raw	= al3320a_read_raw,
194 	.write_raw	= al3320a_write_raw,
195 	.attrs		= &al3320a_attribute_group,
196 };
197 
198 static int al3320a_probe(struct i2c_client *client)
199 {
200 	struct al3320a_data *data;
201 	struct device *dev = &client->dev;
202 	struct iio_dev *indio_dev;
203 	int ret;
204 
205 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
206 	if (!indio_dev)
207 		return -ENOMEM;
208 
209 	data = iio_priv(indio_dev);
210 	i2c_set_clientdata(client, indio_dev);
211 
212 	data->regmap = devm_regmap_init_i2c(client, &al3320a_regmap_config);
213 	if (IS_ERR(data->regmap))
214 		return dev_err_probe(dev, PTR_ERR(data->regmap),
215 				     "cannot allocate regmap\n");
216 
217 	indio_dev->info = &al3320a_info;
218 	indio_dev->name = "al3320a";
219 	indio_dev->channels = al3320a_channels;
220 	indio_dev->num_channels = ARRAY_SIZE(al3320a_channels);
221 	indio_dev->modes = INDIO_DIRECT_MODE;
222 
223 	ret = al3320a_init(data);
224 	if (ret < 0) {
225 		dev_err(dev, "al3320a chip init failed\n");
226 		return ret;
227 	}
228 
229 	return devm_iio_device_register(dev, indio_dev);
230 }
231 
232 static int al3320a_suspend(struct device *dev)
233 {
234 	struct al3320a_data *data = iio_priv(dev_get_drvdata(dev));
235 
236 	al3320a_set_pwr_off(data);
237 	return 0;
238 }
239 
240 static int al3320a_resume(struct device *dev)
241 {
242 	struct al3320a_data *data = iio_priv(dev_get_drvdata(dev));
243 
244 	return al3320a_set_pwr_on(data);
245 }
246 
247 static DEFINE_SIMPLE_DEV_PM_OPS(al3320a_pm_ops, al3320a_suspend,
248 				al3320a_resume);
249 
250 static const struct i2c_device_id al3320a_id[] = {
251 	{ .name = "al3320a" },
252 	{ }
253 };
254 MODULE_DEVICE_TABLE(i2c, al3320a_id);
255 
256 static const struct of_device_id al3320a_of_match[] = {
257 	{ .compatible = "dynaimage,al3320a", },
258 	{ }
259 };
260 MODULE_DEVICE_TABLE(of, al3320a_of_match);
261 
262 static const struct acpi_device_id al3320a_acpi_match[] = {
263 	{"CALS0001"},
264 	{ }
265 };
266 MODULE_DEVICE_TABLE(acpi, al3320a_acpi_match);
267 
268 static struct i2c_driver al3320a_driver = {
269 	.driver = {
270 		.name = "al3320a",
271 		.of_match_table = al3320a_of_match,
272 		.pm = pm_sleep_ptr(&al3320a_pm_ops),
273 		.acpi_match_table = al3320a_acpi_match,
274 	},
275 	.probe		= al3320a_probe,
276 	.id_table	= al3320a_id,
277 };
278 
279 module_i2c_driver(al3320a_driver);
280 
281 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
282 MODULE_DESCRIPTION("AL3320A Ambient Light Sensor driver");
283 MODULE_LICENSE("GPL v2");
284