xref: /linux/drivers/iio/light/al3010.c (revision c26f4fbd58375bd6ef74f95eb73d61762ad97c59)
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 
al3010_set_pwr_on(struct al3010_data * data)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 
al3010_set_pwr_off(void * _data)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 
al3010_init(struct al3010_data * data)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 
al3010_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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, raw;
115 
116 	switch (mask) {
117 	case IIO_CHAN_INFO_RAW:
118 		/*
119 		 * ALS ADC value is stored in two adjacent registers:
120 		 * - low byte of output is stored at AL3010_REG_DATA_LOW
121 		 * - high byte of output is stored at AL3010_REG_DATA_LOW + 1
122 		 */
123 		ret = regmap_read(data->regmap, AL3010_REG_DATA_LOW, &raw);
124 		if (ret)
125 			return ret;
126 
127 		*val = raw;
128 
129 		return IIO_VAL_INT;
130 	case IIO_CHAN_INFO_SCALE:
131 		ret = regmap_read(data->regmap, AL3010_REG_CONFIG, &gain);
132 		if (ret)
133 			return ret;
134 
135 		gain = FIELD_GET(AL3010_GAIN_MASK, gain);
136 		*val = al3010_scales[gain][0];
137 		*val2 = al3010_scales[gain][1];
138 
139 		return IIO_VAL_INT_PLUS_MICRO;
140 	}
141 	return -EINVAL;
142 }
143 
al3010_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)144 static int al3010_write_raw(struct iio_dev *indio_dev,
145 			    struct iio_chan_spec const *chan, int val,
146 			    int val2, long mask)
147 {
148 	struct al3010_data *data = iio_priv(indio_dev);
149 	unsigned int i;
150 
151 	switch (mask) {
152 	case IIO_CHAN_INFO_SCALE:
153 		for (i = 0; i < ARRAY_SIZE(al3010_scales); i++) {
154 			if (val != al3010_scales[i][0] ||
155 			    val2 != al3010_scales[i][1])
156 				continue;
157 
158 			return regmap_write(data->regmap, AL3010_REG_CONFIG,
159 					    FIELD_PREP(AL3010_GAIN_MASK, i));
160 		}
161 		break;
162 	}
163 	return -EINVAL;
164 }
165 
166 static const struct iio_info al3010_info = {
167 	.read_raw	= al3010_read_raw,
168 	.write_raw	= al3010_write_raw,
169 	.attrs		= &al3010_attribute_group,
170 };
171 
al3010_probe(struct i2c_client * client)172 static int al3010_probe(struct i2c_client *client)
173 {
174 	struct al3010_data *data;
175 	struct device *dev = &client->dev;
176 	struct iio_dev *indio_dev;
177 	int ret;
178 
179 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
180 	if (!indio_dev)
181 		return -ENOMEM;
182 
183 	data = iio_priv(indio_dev);
184 	i2c_set_clientdata(client, indio_dev);
185 	data->regmap = devm_regmap_init_i2c(client, &al3010_regmap_config);
186 	if (IS_ERR(data->regmap))
187 		return dev_err_probe(dev, PTR_ERR(data->regmap),
188 				     "cannot allocate regmap\n");
189 
190 	indio_dev->info = &al3010_info;
191 	indio_dev->name = "al3010";
192 	indio_dev->channels = al3010_channels;
193 	indio_dev->num_channels = ARRAY_SIZE(al3010_channels);
194 	indio_dev->modes = INDIO_DIRECT_MODE;
195 
196 	ret = al3010_init(data);
197 	if (ret)
198 		return dev_err_probe(dev, ret, "failed to init ALS\n");
199 
200 	return devm_iio_device_register(dev, indio_dev);
201 }
202 
al3010_suspend(struct device * dev)203 static int al3010_suspend(struct device *dev)
204 {
205 	struct al3010_data *data = iio_priv(dev_get_drvdata(dev));
206 
207 	al3010_set_pwr_off(data);
208 	return 0;
209 }
210 
al3010_resume(struct device * dev)211 static int al3010_resume(struct device *dev)
212 {
213 	struct al3010_data *data = iio_priv(dev_get_drvdata(dev));
214 
215 	return al3010_set_pwr_on(data);
216 }
217 
218 static DEFINE_SIMPLE_DEV_PM_OPS(al3010_pm_ops, al3010_suspend, al3010_resume);
219 
220 static const struct i2c_device_id al3010_id[] = {
221 	{"al3010", },
222 	{ }
223 };
224 MODULE_DEVICE_TABLE(i2c, al3010_id);
225 
226 static const struct of_device_id al3010_of_match[] = {
227 	{ .compatible = "dynaimage,al3010", },
228 	{ }
229 };
230 MODULE_DEVICE_TABLE(of, al3010_of_match);
231 
232 static struct i2c_driver al3010_driver = {
233 	.driver = {
234 		.name = "al3010",
235 		.of_match_table = al3010_of_match,
236 		.pm = pm_sleep_ptr(&al3010_pm_ops),
237 	},
238 	.probe		= al3010_probe,
239 	.id_table	= al3010_id,
240 };
241 module_i2c_driver(al3010_driver);
242 
243 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@nxp.com>");
244 MODULE_AUTHOR("David Heidelberg <david@ixit.cz>");
245 MODULE_DESCRIPTION("AL3010 Ambient Light Sensor driver");
246 MODULE_LICENSE("GPL v2");
247