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