xref: /linux/drivers/iio/light/cm32181.c (revision 9065b78028c5a50f3c352f6fa904a7db9d470af3)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2971672c0SKevin Tsai /*
3971672c0SKevin Tsai  * Copyright (C) 2013 Capella Microsystems Inc.
4971672c0SKevin Tsai  * Author: Kevin Tsai <ktsai@capellamicro.com>
5971672c0SKevin Tsai  */
6971672c0SKevin Tsai 
7971672c0SKevin Tsai #include <linux/delay.h>
8971672c0SKevin Tsai #include <linux/err.h>
9971672c0SKevin Tsai #include <linux/i2c.h>
10971672c0SKevin Tsai #include <linux/mutex.h>
11971672c0SKevin Tsai #include <linux/module.h>
12*9065b780SJonathan Cameron #include <linux/mod_devicetable.h>
13971672c0SKevin Tsai #include <linux/interrupt.h>
14971672c0SKevin Tsai #include <linux/regulator/consumer.h>
15971672c0SKevin Tsai #include <linux/iio/iio.h>
16971672c0SKevin Tsai #include <linux/iio/sysfs.h>
17971672c0SKevin Tsai #include <linux/iio/events.h>
18971672c0SKevin Tsai #include <linux/init.h>
19971672c0SKevin Tsai 
20971672c0SKevin Tsai /* Registers Address */
21971672c0SKevin Tsai #define CM32181_REG_ADDR_CMD		0x00
22971672c0SKevin Tsai #define CM32181_REG_ADDR_ALS		0x04
23971672c0SKevin Tsai #define CM32181_REG_ADDR_STATUS		0x06
24971672c0SKevin Tsai #define CM32181_REG_ADDR_ID		0x07
25971672c0SKevin Tsai 
26971672c0SKevin Tsai /* Number of Configurable Registers */
27971672c0SKevin Tsai #define CM32181_CONF_REG_NUM		0x01
28971672c0SKevin Tsai 
29971672c0SKevin Tsai /* CMD register */
30971672c0SKevin Tsai #define CM32181_CMD_ALS_ENABLE		0x00
31971672c0SKevin Tsai #define CM32181_CMD_ALS_DISABLE		0x01
32971672c0SKevin Tsai #define CM32181_CMD_ALS_INT_EN		0x02
33971672c0SKevin Tsai 
34971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_SHIFT	6
35971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_MASK		(0x0F << CM32181_CMD_ALS_IT_SHIFT)
36971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_DEFAULT	(0x00 << CM32181_CMD_ALS_IT_SHIFT)
37971672c0SKevin Tsai 
38971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_SHIFT	11
39971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_MASK		(0x03 << CM32181_CMD_ALS_SM_SHIFT)
40971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_DEFAULT	(0x01 << CM32181_CMD_ALS_SM_SHIFT)
41971672c0SKevin Tsai 
42971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT		5	/* ALS_SM=01 IT=800ms */
43971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT_BASE_IT	800000	/* Based on IT=800ms */
44971672c0SKevin Tsai #define	CM32181_CALIBSCALE_DEFAULT	1000
45971672c0SKevin Tsai #define CM32181_CALIBSCALE_RESOLUTION	1000
46971672c0SKevin Tsai #define MLUX_PER_LUX			1000
47971672c0SKevin Tsai 
48971672c0SKevin Tsai static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
49971672c0SKevin Tsai 	CM32181_REG_ADDR_CMD,
50971672c0SKevin Tsai };
51971672c0SKevin Tsai 
52971672c0SKevin Tsai static const int als_it_bits[] = {12, 8, 0, 1, 2, 3};
53971672c0SKevin Tsai static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000,
54971672c0SKevin Tsai 	800000};
55971672c0SKevin Tsai 
56971672c0SKevin Tsai struct cm32181_chip {
57971672c0SKevin Tsai 	struct i2c_client *client;
58971672c0SKevin Tsai 	struct mutex lock;
59971672c0SKevin Tsai 	u16 conf_regs[CM32181_CONF_REG_NUM];
60971672c0SKevin Tsai 	int calibscale;
61971672c0SKevin Tsai };
62971672c0SKevin Tsai 
63971672c0SKevin Tsai /**
64971672c0SKevin Tsai  * cm32181_reg_init() - Initialize CM32181 registers
65971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
66971672c0SKevin Tsai  *
67971672c0SKevin Tsai  * Initialize CM32181 ambient light sensor register to default values.
68971672c0SKevin Tsai  *
69971672c0SKevin Tsai  * Return: 0 for success; otherwise for error code.
70971672c0SKevin Tsai  */
71971672c0SKevin Tsai static int cm32181_reg_init(struct cm32181_chip *cm32181)
72971672c0SKevin Tsai {
73971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
74971672c0SKevin Tsai 	int i;
75971672c0SKevin Tsai 	s32 ret;
76971672c0SKevin Tsai 
77971672c0SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
78971672c0SKevin Tsai 	if (ret < 0)
79971672c0SKevin Tsai 		return ret;
80971672c0SKevin Tsai 
81971672c0SKevin Tsai 	/* check device ID */
82971672c0SKevin Tsai 	if ((ret & 0xFF) != 0x81)
83971672c0SKevin Tsai 		return -ENODEV;
84971672c0SKevin Tsai 
85971672c0SKevin Tsai 	/* Default Values */
86971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE |
87971672c0SKevin Tsai 			CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
88971672c0SKevin Tsai 	cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
89971672c0SKevin Tsai 
90971672c0SKevin Tsai 	/* Initialize registers*/
91971672c0SKevin Tsai 	for (i = 0; i < CM32181_CONF_REG_NUM; i++) {
92971672c0SKevin Tsai 		ret = i2c_smbus_write_word_data(client, cm32181_reg[i],
93971672c0SKevin Tsai 			cm32181->conf_regs[i]);
94971672c0SKevin Tsai 		if (ret < 0)
95971672c0SKevin Tsai 			return ret;
96971672c0SKevin Tsai 	}
97971672c0SKevin Tsai 
98971672c0SKevin Tsai 	return 0;
99971672c0SKevin Tsai }
100971672c0SKevin Tsai 
101971672c0SKevin Tsai /**
102971672c0SKevin Tsai  *  cm32181_read_als_it() - Get sensor integration time (ms)
103971672c0SKevin Tsai  *  @cm32181:	pointer of struct cm32181
1041463a166SBeomho Seo  *  @val2:	pointer of int to load the als_it value.
105971672c0SKevin Tsai  *
106971672c0SKevin Tsai  *  Report the current integartion time by millisecond.
107971672c0SKevin Tsai  *
1081463a166SBeomho Seo  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
109971672c0SKevin Tsai  */
1101463a166SBeomho Seo static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
111971672c0SKevin Tsai {
112971672c0SKevin Tsai 	u16 als_it;
113971672c0SKevin Tsai 	int i;
114971672c0SKevin Tsai 
115971672c0SKevin Tsai 	als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
116971672c0SKevin Tsai 	als_it &= CM32181_CMD_ALS_IT_MASK;
117971672c0SKevin Tsai 	als_it >>= CM32181_CMD_ALS_IT_SHIFT;
118971672c0SKevin Tsai 	for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) {
119971672c0SKevin Tsai 		if (als_it == als_it_bits[i]) {
1201463a166SBeomho Seo 			*val2 = als_it_value[i];
1211463a166SBeomho Seo 			return IIO_VAL_INT_PLUS_MICRO;
122971672c0SKevin Tsai 		}
123971672c0SKevin Tsai 	}
124971672c0SKevin Tsai 
125971672c0SKevin Tsai 	return -EINVAL;
126971672c0SKevin Tsai }
127971672c0SKevin Tsai 
128971672c0SKevin Tsai /**
129971672c0SKevin Tsai  * cm32181_write_als_it() - Write sensor integration time
130971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
131971672c0SKevin Tsai  * @val:	integration time by millisecond.
132971672c0SKevin Tsai  *
133971672c0SKevin Tsai  * Convert integration time (ms) to sensor value.
134971672c0SKevin Tsai  *
135971672c0SKevin Tsai  * Return: i2c_smbus_write_word_data command return value.
136971672c0SKevin Tsai  */
137971672c0SKevin Tsai static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
138971672c0SKevin Tsai {
139971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
140971672c0SKevin Tsai 	u16 als_it;
141971672c0SKevin Tsai 	int ret, i, n;
142971672c0SKevin Tsai 
143971672c0SKevin Tsai 	n = ARRAY_SIZE(als_it_value);
144971672c0SKevin Tsai 	for (i = 0; i < n; i++)
145971672c0SKevin Tsai 		if (val <= als_it_value[i])
146971672c0SKevin Tsai 			break;
147971672c0SKevin Tsai 	if (i >= n)
148971672c0SKevin Tsai 		i = n - 1;
149971672c0SKevin Tsai 
150971672c0SKevin Tsai 	als_it = als_it_bits[i];
151971672c0SKevin Tsai 	als_it <<= CM32181_CMD_ALS_IT_SHIFT;
152971672c0SKevin Tsai 
153971672c0SKevin Tsai 	mutex_lock(&cm32181->lock);
154971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
155971672c0SKevin Tsai 		~CM32181_CMD_ALS_IT_MASK;
156971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
157971672c0SKevin Tsai 		als_it;
158971672c0SKevin Tsai 	ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
159971672c0SKevin Tsai 			cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
160971672c0SKevin Tsai 	mutex_unlock(&cm32181->lock);
161971672c0SKevin Tsai 
162971672c0SKevin Tsai 	return ret;
163971672c0SKevin Tsai }
164971672c0SKevin Tsai 
165971672c0SKevin Tsai /**
166971672c0SKevin Tsai  * cm32181_get_lux() - report current lux value
167971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
168971672c0SKevin Tsai  *
169971672c0SKevin Tsai  * Convert sensor raw data to lux.  It depends on integration
170a86ea557SRoberta Dobrescu  * time and calibscale variable.
171971672c0SKevin Tsai  *
172971672c0SKevin Tsai  * Return: Positive value is lux, otherwise is error code.
173971672c0SKevin Tsai  */
174971672c0SKevin Tsai static int cm32181_get_lux(struct cm32181_chip *cm32181)
175971672c0SKevin Tsai {
176971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
177971672c0SKevin Tsai 	int ret;
178971672c0SKevin Tsai 	int als_it;
179971672c0SKevin Tsai 	unsigned long lux;
180971672c0SKevin Tsai 
181971672c0SKevin Tsai 	ret = cm32181_read_als_it(cm32181, &als_it);
182971672c0SKevin Tsai 	if (ret < 0)
183971672c0SKevin Tsai 		return -EINVAL;
184971672c0SKevin Tsai 
185971672c0SKevin Tsai 	lux = CM32181_MLUX_PER_BIT;
186971672c0SKevin Tsai 	lux *= CM32181_MLUX_PER_BIT_BASE_IT;
187971672c0SKevin Tsai 	lux /= als_it;
188971672c0SKevin Tsai 
189971672c0SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
190971672c0SKevin Tsai 	if (ret < 0)
191971672c0SKevin Tsai 		return ret;
192971672c0SKevin Tsai 
193971672c0SKevin Tsai 	lux *= ret;
194971672c0SKevin Tsai 	lux *= cm32181->calibscale;
195971672c0SKevin Tsai 	lux /= CM32181_CALIBSCALE_RESOLUTION;
196971672c0SKevin Tsai 	lux /= MLUX_PER_LUX;
197971672c0SKevin Tsai 
198971672c0SKevin Tsai 	if (lux > 0xFFFF)
199971672c0SKevin Tsai 		lux = 0xFFFF;
200971672c0SKevin Tsai 
201971672c0SKevin Tsai 	return lux;
202971672c0SKevin Tsai }
203971672c0SKevin Tsai 
204971672c0SKevin Tsai static int cm32181_read_raw(struct iio_dev *indio_dev,
205971672c0SKevin Tsai 			    struct iio_chan_spec const *chan,
206971672c0SKevin Tsai 			    int *val, int *val2, long mask)
207971672c0SKevin Tsai {
208971672c0SKevin Tsai 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
209971672c0SKevin Tsai 	int ret;
210971672c0SKevin Tsai 
211971672c0SKevin Tsai 	switch (mask) {
212971672c0SKevin Tsai 	case IIO_CHAN_INFO_PROCESSED:
213971672c0SKevin Tsai 		ret = cm32181_get_lux(cm32181);
214971672c0SKevin Tsai 		if (ret < 0)
215971672c0SKevin Tsai 			return ret;
216971672c0SKevin Tsai 		*val = ret;
217971672c0SKevin Tsai 		return IIO_VAL_INT;
218971672c0SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
219971672c0SKevin Tsai 		*val = cm32181->calibscale;
220971672c0SKevin Tsai 		return IIO_VAL_INT;
221971672c0SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
22241c897f8SBeomho Seo 		*val = 0;
2231463a166SBeomho Seo 		ret = cm32181_read_als_it(cm32181, val2);
224971672c0SKevin Tsai 		return ret;
225971672c0SKevin Tsai 	}
226971672c0SKevin Tsai 
227971672c0SKevin Tsai 	return -EINVAL;
228971672c0SKevin Tsai }
229971672c0SKevin Tsai 
230971672c0SKevin Tsai static int cm32181_write_raw(struct iio_dev *indio_dev,
231971672c0SKevin Tsai 			     struct iio_chan_spec const *chan,
232971672c0SKevin Tsai 			     int val, int val2, long mask)
233971672c0SKevin Tsai {
234971672c0SKevin Tsai 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
235971672c0SKevin Tsai 	int ret;
236971672c0SKevin Tsai 
237971672c0SKevin Tsai 	switch (mask) {
238971672c0SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
239971672c0SKevin Tsai 		cm32181->calibscale = val;
240971672c0SKevin Tsai 		return val;
241971672c0SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
2421463a166SBeomho Seo 		ret = cm32181_write_als_it(cm32181, val2);
243971672c0SKevin Tsai 		return ret;
244971672c0SKevin Tsai 	}
245971672c0SKevin Tsai 
246971672c0SKevin Tsai 	return -EINVAL;
247971672c0SKevin Tsai }
248971672c0SKevin Tsai 
249971672c0SKevin Tsai /**
250971672c0SKevin Tsai  * cm32181_get_it_available() - Get available ALS IT value
251971672c0SKevin Tsai  * @dev:	pointer of struct device.
252971672c0SKevin Tsai  * @attr:	pointer of struct device_attribute.
253971672c0SKevin Tsai  * @buf:	pointer of return string buffer.
254971672c0SKevin Tsai  *
255971672c0SKevin Tsai  * Display the available integration time values by millisecond.
256971672c0SKevin Tsai  *
257971672c0SKevin Tsai  * Return: string length.
258971672c0SKevin Tsai  */
259971672c0SKevin Tsai static ssize_t cm32181_get_it_available(struct device *dev,
260971672c0SKevin Tsai 			struct device_attribute *attr, char *buf)
261971672c0SKevin Tsai {
262971672c0SKevin Tsai 	int i, n, len;
263971672c0SKevin Tsai 
264971672c0SKevin Tsai 	n = ARRAY_SIZE(als_it_value);
265971672c0SKevin Tsai 	for (i = 0, len = 0; i < n; i++)
2661463a166SBeomho Seo 		len += sprintf(buf + len, "0.%06u ", als_it_value[i]);
267971672c0SKevin Tsai 	return len + sprintf(buf + len, "\n");
268971672c0SKevin Tsai }
269971672c0SKevin Tsai 
270971672c0SKevin Tsai static const struct iio_chan_spec cm32181_channels[] = {
271971672c0SKevin Tsai 	{
272971672c0SKevin Tsai 		.type = IIO_LIGHT,
273971672c0SKevin Tsai 		.info_mask_separate =
274971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_PROCESSED) |
275971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_CALIBSCALE) |
276971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_INT_TIME),
277971672c0SKevin Tsai 	}
278971672c0SKevin Tsai };
279971672c0SKevin Tsai 
280971672c0SKevin Tsai static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
281971672c0SKevin Tsai 			S_IRUGO, cm32181_get_it_available, NULL, 0);
282971672c0SKevin Tsai 
283971672c0SKevin Tsai static struct attribute *cm32181_attributes[] = {
284971672c0SKevin Tsai 	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
285971672c0SKevin Tsai 	NULL,
286971672c0SKevin Tsai };
287971672c0SKevin Tsai 
288971672c0SKevin Tsai static const struct attribute_group cm32181_attribute_group = {
289971672c0SKevin Tsai 	.attrs = cm32181_attributes
290971672c0SKevin Tsai };
291971672c0SKevin Tsai 
292971672c0SKevin Tsai static const struct iio_info cm32181_info = {
293971672c0SKevin Tsai 	.read_raw		= &cm32181_read_raw,
294971672c0SKevin Tsai 	.write_raw		= &cm32181_write_raw,
295971672c0SKevin Tsai 	.attrs			= &cm32181_attribute_group,
296971672c0SKevin Tsai };
297971672c0SKevin Tsai 
298971672c0SKevin Tsai static int cm32181_probe(struct i2c_client *client,
299971672c0SKevin Tsai 			const struct i2c_device_id *id)
300971672c0SKevin Tsai {
301971672c0SKevin Tsai 	struct cm32181_chip *cm32181;
302971672c0SKevin Tsai 	struct iio_dev *indio_dev;
303971672c0SKevin Tsai 	int ret;
304971672c0SKevin Tsai 
305971672c0SKevin Tsai 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181));
306971672c0SKevin Tsai 	if (!indio_dev) {
307971672c0SKevin Tsai 		dev_err(&client->dev, "devm_iio_device_alloc failed\n");
308971672c0SKevin Tsai 		return -ENOMEM;
309971672c0SKevin Tsai 	}
310971672c0SKevin Tsai 
311971672c0SKevin Tsai 	cm32181 = iio_priv(indio_dev);
312971672c0SKevin Tsai 	i2c_set_clientdata(client, indio_dev);
313971672c0SKevin Tsai 	cm32181->client = client;
314971672c0SKevin Tsai 
315971672c0SKevin Tsai 	mutex_init(&cm32181->lock);
316971672c0SKevin Tsai 	indio_dev->dev.parent = &client->dev;
317971672c0SKevin Tsai 	indio_dev->channels = cm32181_channels;
318971672c0SKevin Tsai 	indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
319971672c0SKevin Tsai 	indio_dev->info = &cm32181_info;
320971672c0SKevin Tsai 	indio_dev->name = id->name;
321971672c0SKevin Tsai 	indio_dev->modes = INDIO_DIRECT_MODE;
322971672c0SKevin Tsai 
323971672c0SKevin Tsai 	ret = cm32181_reg_init(cm32181);
324971672c0SKevin Tsai 	if (ret) {
325971672c0SKevin Tsai 		dev_err(&client->dev,
326971672c0SKevin Tsai 			"%s: register init failed\n",
327971672c0SKevin Tsai 			__func__);
328971672c0SKevin Tsai 		return ret;
329971672c0SKevin Tsai 	}
330971672c0SKevin Tsai 
331b9785535SBeomho Seo 	ret = devm_iio_device_register(&client->dev, indio_dev);
332971672c0SKevin Tsai 	if (ret) {
333971672c0SKevin Tsai 		dev_err(&client->dev,
334971672c0SKevin Tsai 			"%s: regist device failed\n",
335971672c0SKevin Tsai 			__func__);
336971672c0SKevin Tsai 		return ret;
337971672c0SKevin Tsai 	}
338971672c0SKevin Tsai 
339971672c0SKevin Tsai 	return 0;
340971672c0SKevin Tsai }
341971672c0SKevin Tsai 
342971672c0SKevin Tsai static const struct i2c_device_id cm32181_id[] = {
343971672c0SKevin Tsai 	{ "cm32181", 0 },
344971672c0SKevin Tsai 	{ }
345971672c0SKevin Tsai };
346971672c0SKevin Tsai 
347971672c0SKevin Tsai MODULE_DEVICE_TABLE(i2c, cm32181_id);
348971672c0SKevin Tsai 
349971672c0SKevin Tsai static const struct of_device_id cm32181_of_match[] = {
350971672c0SKevin Tsai 	{ .compatible = "capella,cm32181" },
351971672c0SKevin Tsai 	{ }
352971672c0SKevin Tsai };
353119c4fceSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, cm32181_of_match);
354971672c0SKevin Tsai 
355971672c0SKevin Tsai static struct i2c_driver cm32181_driver = {
356971672c0SKevin Tsai 	.driver = {
357971672c0SKevin Tsai 		.name	= "cm32181",
358*9065b780SJonathan Cameron 		.of_match_table = cm32181_of_match,
359971672c0SKevin Tsai 	},
360971672c0SKevin Tsai 	.id_table       = cm32181_id,
361971672c0SKevin Tsai 	.probe		= cm32181_probe,
362971672c0SKevin Tsai };
363971672c0SKevin Tsai 
364971672c0SKevin Tsai module_i2c_driver(cm32181_driver);
365971672c0SKevin Tsai 
366971672c0SKevin Tsai MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
367971672c0SKevin Tsai MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
368971672c0SKevin Tsai MODULE_LICENSE("GPL");
369