xref: /linux/drivers/iio/light/al3010.c (revision 2eff01ee2881becc9daaa0d53477ec202136b1f4)
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/mod_devicetable.h>
21 
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 
25 #define AL3010_DRV_NAME "al3010"
26 
27 #define AL3010_REG_SYSTEM		0x00
28 #define AL3010_REG_DATA_LOW		0x0c
29 #define AL3010_REG_CONFIG		0x10
30 
31 #define AL3010_CONFIG_DISABLE		0x00
32 #define AL3010_CONFIG_ENABLE		0x01
33 
34 #define AL3010_GAIN_MASK		GENMASK(6,4)
35 
36 #define AL3010_SCALE_AVAILABLE "1.1872 0.2968 0.0742 0.018"
37 
38 enum al3xxxx_range {
39 	AL3XXX_RANGE_1, /* 77806 lx */
40 	AL3XXX_RANGE_2, /* 19542 lx */
41 	AL3XXX_RANGE_3, /*  4863 lx */
42 	AL3XXX_RANGE_4  /*  1216 lx */
43 };
44 
45 static const int al3010_scales[][2] = {
46 	{0, 1187200}, {0, 296800}, {0, 74200}, {0, 18600}
47 };
48 
49 struct al3010_data {
50 	struct i2c_client *client;
51 };
52 
53 static const struct iio_chan_spec al3010_channels[] = {
54 	{
55 		.type	= IIO_LIGHT,
56 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
57 				      BIT(IIO_CHAN_INFO_SCALE),
58 	}
59 };
60 
61 static IIO_CONST_ATTR(in_illuminance_scale_available, AL3010_SCALE_AVAILABLE);
62 
63 static struct attribute *al3010_attributes[] = {
64 	&iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
65 	NULL,
66 };
67 
68 static const struct attribute_group al3010_attribute_group = {
69 	.attrs = al3010_attributes,
70 };
71 
72 static int al3010_set_pwr(struct i2c_client *client, bool pwr)
73 {
74 	u8 val = pwr ? AL3010_CONFIG_ENABLE : AL3010_CONFIG_DISABLE;
75 	return i2c_smbus_write_byte_data(client, AL3010_REG_SYSTEM, val);
76 }
77 
78 static void al3010_set_pwr_off(void *_data)
79 {
80 	struct al3010_data *data = _data;
81 
82 	al3010_set_pwr(data->client, false);
83 }
84 
85 static int al3010_init(struct al3010_data *data)
86 {
87 	int ret;
88 
89 	ret = al3010_set_pwr(data->client, true);
90 	if (ret < 0)
91 		return ret;
92 
93 	ret = devm_add_action_or_reset(&data->client->dev,
94 				       al3010_set_pwr_off,
95 				       data);
96 	if (ret < 0)
97 		return ret;
98 
99 	ret = i2c_smbus_write_byte_data(data->client, AL3010_REG_CONFIG,
100 					FIELD_PREP(AL3010_GAIN_MASK,
101 						   AL3XXX_RANGE_3));
102 	if (ret < 0)
103 		return ret;
104 
105 	return 0;
106 }
107 
108 static int al3010_read_raw(struct iio_dev *indio_dev,
109 			   struct iio_chan_spec const *chan, int *val,
110 			   int *val2, long mask)
111 {
112 	struct al3010_data *data = iio_priv(indio_dev);
113 	int ret;
114 
115 	switch (mask) {
116 	case IIO_CHAN_INFO_RAW:
117 		/*
118 		 * ALS ADC value is stored in two adjacent registers:
119 		 * - low byte of output is stored at AL3010_REG_DATA_LOW
120 		 * - high byte of output is stored at AL3010_REG_DATA_LOW + 1
121 		 */
122 		ret = i2c_smbus_read_word_data(data->client,
123 					       AL3010_REG_DATA_LOW);
124 		if (ret < 0)
125 			return ret;
126 		*val = ret;
127 		return IIO_VAL_INT;
128 	case IIO_CHAN_INFO_SCALE:
129 		ret = i2c_smbus_read_byte_data(data->client,
130 					       AL3010_REG_CONFIG);
131 		if (ret < 0)
132 			return ret;
133 
134 		ret = FIELD_GET(AL3010_GAIN_MASK, ret);
135 		*val = al3010_scales[ret][0];
136 		*val2 = al3010_scales[ret][1];
137 
138 		return IIO_VAL_INT_PLUS_MICRO;
139 	}
140 	return -EINVAL;
141 }
142 
143 static int al3010_write_raw(struct iio_dev *indio_dev,
144 			    struct iio_chan_spec const *chan, int val,
145 			    int val2, long mask)
146 {
147 	struct al3010_data *data = iio_priv(indio_dev);
148 	int i;
149 
150 	switch (mask) {
151 	case IIO_CHAN_INFO_SCALE:
152 		for (i = 0; i < ARRAY_SIZE(al3010_scales); i++) {
153 			if (val != al3010_scales[i][0] ||
154 			    val2 != al3010_scales[i][1])
155 				continue;
156 
157 			return i2c_smbus_write_byte_data(data->client,
158 					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 
172 static int al3010_probe(struct i2c_client *client)
173 {
174 	struct al3010_data *data;
175 	struct iio_dev *indio_dev;
176 	int ret;
177 
178 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
179 	if (!indio_dev)
180 		return -ENOMEM;
181 
182 	data = iio_priv(indio_dev);
183 	i2c_set_clientdata(client, indio_dev);
184 	data->client = client;
185 
186 	indio_dev->info = &al3010_info;
187 	indio_dev->name = AL3010_DRV_NAME;
188 	indio_dev->channels = al3010_channels;
189 	indio_dev->num_channels = ARRAY_SIZE(al3010_channels);
190 	indio_dev->modes = INDIO_DIRECT_MODE;
191 
192 	ret = al3010_init(data);
193 	if (ret < 0) {
194 		dev_err(&client->dev, "al3010 chip init failed\n");
195 		return ret;
196 	}
197 
198 	return devm_iio_device_register(&client->dev, indio_dev);
199 }
200 
201 static int al3010_suspend(struct device *dev)
202 {
203 	return al3010_set_pwr(to_i2c_client(dev), false);
204 }
205 
206 static int al3010_resume(struct device *dev)
207 {
208 	return al3010_set_pwr(to_i2c_client(dev), true);
209 }
210 
211 static DEFINE_SIMPLE_DEV_PM_OPS(al3010_pm_ops, al3010_suspend, al3010_resume);
212 
213 static const struct i2c_device_id al3010_id[] = {
214 	{"al3010", },
215 	{}
216 };
217 MODULE_DEVICE_TABLE(i2c, al3010_id);
218 
219 static const struct of_device_id al3010_of_match[] = {
220 	{ .compatible = "dynaimage,al3010", },
221 	{},
222 };
223 MODULE_DEVICE_TABLE(of, al3010_of_match);
224 
225 static struct i2c_driver al3010_driver = {
226 	.driver = {
227 		.name = AL3010_DRV_NAME,
228 		.of_match_table = al3010_of_match,
229 		.pm = pm_sleep_ptr(&al3010_pm_ops),
230 	},
231 	.probe		= al3010_probe,
232 	.id_table	= al3010_id,
233 };
234 module_i2c_driver(al3010_driver);
235 
236 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@nxp.com>");
237 MODULE_AUTHOR("David Heidelberg <david@ixit.cz>");
238 MODULE_DESCRIPTION("AL3010 Ambient Light Sensor driver");
239 MODULE_LICENSE("GPL v2");
240