xref: /linux/drivers/iio/light/al3010.c (revision b232fc005aec5fa5346d970f8986b8f0046f328b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AL3010 - Dyna Image Ambient Light Sensor
4  *
5  * Copyright (c) 2014, Intel Corporation.
6  * Copyright (c) 2016, Dyna-Image Corp.
7  * Copyright (c) 2020, David Heidelberg, Michał Mirosław, Dmitry Osipenko
8  *
9  * IIO driver for AL3010 (7-bit I2C slave address 0x1C).
10  *
11  * TODO: interrupt support, thresholds
12  * When the driver will get support for interrupt handling, then interrupt
13  * will need to be disabled before turning sensor OFF in order to avoid
14  * potential races with the interrupt handling.
15  */
16 
17 #include <linux/bitfield.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/regmap.h>
21 #include <linux/mod_devicetable.h>
22 
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 
26 #define AL3010_REG_SYSTEM		0x00
27 #define AL3010_REG_DATA_LOW		0x0c
28 #define AL3010_REG_CONFIG		0x10
29 
30 #define AL3010_CONFIG_DISABLE		0x00
31 #define AL3010_CONFIG_ENABLE		0x01
32 
33 #define AL3010_GAIN_MASK		GENMASK(6,4)
34 
35 #define AL3010_SCALE_AVAILABLE "1.1872 0.2968 0.0742 0.018"
36 
37 enum al3xxxx_range {
38 	AL3XXX_RANGE_1, /* 77806 lx */
39 	AL3XXX_RANGE_2, /* 19542 lx */
40 	AL3XXX_RANGE_3, /*  4863 lx */
41 	AL3XXX_RANGE_4  /*  1216 lx */
42 };
43 
44 static const int al3010_scales[][2] = {
45 	{0, 1187200}, {0, 296800}, {0, 74200}, {0, 18600}
46 };
47 
48 static const struct regmap_config al3010_regmap_config = {
49 	.reg_bits = 8,
50 	.val_bits = 8,
51 	.max_register = AL3010_REG_CONFIG,
52 };
53 
54 struct al3010_data {
55 	struct regmap *regmap;
56 };
57 
58 static const struct iio_chan_spec al3010_channels[] = {
59 	{
60 		.type	= IIO_LIGHT,
61 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
62 				      BIT(IIO_CHAN_INFO_SCALE),
63 	}
64 };
65 
66 static IIO_CONST_ATTR(in_illuminance_scale_available, AL3010_SCALE_AVAILABLE);
67 
68 static struct attribute *al3010_attributes[] = {
69 	&iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
70 	NULL,
71 };
72 
73 static const struct attribute_group al3010_attribute_group = {
74 	.attrs = al3010_attributes,
75 };
76 
77 static int al3010_set_pwr_on(struct al3010_data *data)
78 {
79 	return regmap_write(data->regmap, AL3010_REG_SYSTEM, AL3010_CONFIG_ENABLE);
80 }
81 
82 static void al3010_set_pwr_off(void *_data)
83 {
84 	struct al3010_data *data = _data;
85 	struct device *dev = regmap_get_device(data->regmap);
86 	int ret;
87 
88 	ret = regmap_write(data->regmap, AL3010_REG_SYSTEM, AL3010_CONFIG_DISABLE);
89 	if (ret)
90 		dev_err(dev, "failed to write system register\n");
91 }
92 
93 static int al3010_init(struct al3010_data *data)
94 {
95 	struct device *dev = regmap_get_device(data->regmap);
96 	int ret;
97 
98 	ret = al3010_set_pwr_on(data);
99 	if (ret)
100 		return ret;
101 
102 	ret = devm_add_action_or_reset(dev, al3010_set_pwr_off, data);
103 	if (ret)
104 		return ret;
105 	return regmap_write(data->regmap, AL3010_REG_CONFIG,
106 			    FIELD_PREP(AL3010_GAIN_MASK, AL3XXX_RANGE_3));
107 }
108 
109 static int al3010_read_raw(struct iio_dev *indio_dev,
110 			   struct iio_chan_spec const *chan, int *val,
111 			   int *val2, long mask)
112 {
113 	struct al3010_data *data = iio_priv(indio_dev);
114 	int ret, gain;
115 	__le16 raw;
116 
117 	switch (mask) {
118 	case IIO_CHAN_INFO_RAW:
119 		/*
120 		 * ALS ADC value is stored in two adjacent registers:
121 		 * - low byte of output is stored at AL3010_REG_DATA_LOW
122 		 * - high byte of output is stored at AL3010_REG_DATA_LOW + 1
123 		 */
124 		ret = regmap_bulk_read(data->regmap, AL3010_REG_DATA_LOW,
125 				       &raw, sizeof(raw));
126 		if (ret)
127 			return ret;
128 
129 		*val = le16_to_cpu(raw);
130 
131 		return IIO_VAL_INT;
132 	case IIO_CHAN_INFO_SCALE:
133 		ret = regmap_read(data->regmap, AL3010_REG_CONFIG, &gain);
134 		if (ret)
135 			return ret;
136 
137 		gain = FIELD_GET(AL3010_GAIN_MASK, gain);
138 		*val = al3010_scales[gain][0];
139 		*val2 = al3010_scales[gain][1];
140 
141 		return IIO_VAL_INT_PLUS_MICRO;
142 	}
143 	return -EINVAL;
144 }
145 
146 static int al3010_write_raw(struct iio_dev *indio_dev,
147 			    struct iio_chan_spec const *chan, int val,
148 			    int val2, long mask)
149 {
150 	struct al3010_data *data = iio_priv(indio_dev);
151 	unsigned int i;
152 
153 	switch (mask) {
154 	case IIO_CHAN_INFO_SCALE:
155 		for (i = 0; i < ARRAY_SIZE(al3010_scales); i++) {
156 			if (val != al3010_scales[i][0] ||
157 			    val2 != al3010_scales[i][1])
158 				continue;
159 
160 			return regmap_write(data->regmap, AL3010_REG_CONFIG,
161 					    FIELD_PREP(AL3010_GAIN_MASK, i));
162 		}
163 		break;
164 	}
165 	return -EINVAL;
166 }
167 
168 static const struct iio_info al3010_info = {
169 	.read_raw	= al3010_read_raw,
170 	.write_raw	= al3010_write_raw,
171 	.attrs		= &al3010_attribute_group,
172 };
173 
174 static int al3010_probe(struct i2c_client *client)
175 {
176 	struct al3010_data *data;
177 	struct device *dev = &client->dev;
178 	struct iio_dev *indio_dev;
179 	int ret;
180 
181 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
182 	if (!indio_dev)
183 		return -ENOMEM;
184 
185 	data = iio_priv(indio_dev);
186 	i2c_set_clientdata(client, indio_dev);
187 	data->regmap = devm_regmap_init_i2c(client, &al3010_regmap_config);
188 	if (IS_ERR(data->regmap))
189 		return dev_err_probe(dev, PTR_ERR(data->regmap),
190 				     "cannot allocate regmap\n");
191 
192 	indio_dev->info = &al3010_info;
193 	indio_dev->name = "al3010";
194 	indio_dev->channels = al3010_channels;
195 	indio_dev->num_channels = ARRAY_SIZE(al3010_channels);
196 	indio_dev->modes = INDIO_DIRECT_MODE;
197 
198 	ret = al3010_init(data);
199 	if (ret)
200 		return dev_err_probe(dev, ret, "failed to init ALS\n");
201 
202 	return devm_iio_device_register(dev, indio_dev);
203 }
204 
205 static int al3010_suspend(struct device *dev)
206 {
207 	struct al3010_data *data = iio_priv(dev_get_drvdata(dev));
208 
209 	al3010_set_pwr_off(data);
210 	return 0;
211 }
212 
213 static int al3010_resume(struct device *dev)
214 {
215 	struct al3010_data *data = iio_priv(dev_get_drvdata(dev));
216 
217 	return al3010_set_pwr_on(data);
218 }
219 
220 static DEFINE_SIMPLE_DEV_PM_OPS(al3010_pm_ops, al3010_suspend, al3010_resume);
221 
222 static const struct i2c_device_id al3010_id[] = {
223 	{ .name = "al3010" },
224 	{ }
225 };
226 MODULE_DEVICE_TABLE(i2c, al3010_id);
227 
228 static const struct of_device_id al3010_of_match[] = {
229 	{ .compatible = "dynaimage,al3010", },
230 	{ }
231 };
232 MODULE_DEVICE_TABLE(of, al3010_of_match);
233 
234 static struct i2c_driver al3010_driver = {
235 	.driver = {
236 		.name = "al3010",
237 		.of_match_table = al3010_of_match,
238 		.pm = pm_sleep_ptr(&al3010_pm_ops),
239 	},
240 	.probe		= al3010_probe,
241 	.id_table	= al3010_id,
242 };
243 module_i2c_driver(al3010_driver);
244 
245 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@nxp.com>");
246 MODULE_AUTHOR("David Heidelberg <david@ixit.cz>");
247 MODULE_DESCRIPTION("AL3010 Ambient Light Sensor driver");
248 MODULE_LICENSE("GPL v2");
249