xref: /linux/drivers/iio/light/cm32181.c (revision 7574cb1df43c6219f0088d028906f4e15fee702f)
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>
129065b780SJonathan 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
223bf4a59cSHans de Goede #define CM32181_REG_ADDR_WH		0x01
233bf4a59cSHans de Goede #define CM32181_REG_ADDR_WL		0x02
243bf4a59cSHans de Goede #define CM32181_REG_ADDR_TEST		0x03
25971672c0SKevin Tsai #define CM32181_REG_ADDR_ALS		0x04
26971672c0SKevin Tsai #define CM32181_REG_ADDR_STATUS		0x06
27971672c0SKevin Tsai #define CM32181_REG_ADDR_ID		0x07
28971672c0SKevin Tsai 
29971672c0SKevin Tsai /* Number of Configurable Registers */
30*7574cb1dSHans de Goede #define CM32181_CONF_REG_NUM		4
31971672c0SKevin Tsai 
32971672c0SKevin Tsai /* CMD register */
333bf4a59cSHans de Goede #define CM32181_CMD_ALS_DISABLE		BIT(0)
343bf4a59cSHans de Goede #define CM32181_CMD_ALS_INT_EN		BIT(1)
353bf4a59cSHans de Goede #define CM32181_CMD_ALS_THRES_WINDOW	BIT(2)
363bf4a59cSHans de Goede 
373bf4a59cSHans de Goede #define CM32181_CMD_ALS_PERS_SHIFT	4
383bf4a59cSHans de Goede #define CM32181_CMD_ALS_PERS_MASK	(0x03 << CM32181_CMD_ALS_PERS_SHIFT)
393bf4a59cSHans de Goede #define CM32181_CMD_ALS_PERS_DEFAULT	(0x01 << CM32181_CMD_ALS_PERS_SHIFT)
40971672c0SKevin Tsai 
41971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_SHIFT	6
42971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_MASK		(0x0F << CM32181_CMD_ALS_IT_SHIFT)
43971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_DEFAULT	(0x00 << CM32181_CMD_ALS_IT_SHIFT)
44971672c0SKevin Tsai 
45971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_SHIFT	11
46971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_MASK		(0x03 << CM32181_CMD_ALS_SM_SHIFT)
47971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_DEFAULT	(0x01 << CM32181_CMD_ALS_SM_SHIFT)
48971672c0SKevin Tsai 
49971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT		5	/* ALS_SM=01 IT=800ms */
50971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT_BASE_IT	800000	/* Based on IT=800ms */
51971672c0SKevin Tsai #define	CM32181_CALIBSCALE_DEFAULT	1000
52971672c0SKevin Tsai #define CM32181_CALIBSCALE_RESOLUTION	1000
53971672c0SKevin Tsai #define MLUX_PER_LUX			1000
54971672c0SKevin Tsai 
55c1e62062SHans de Goede #define SMBUS_ALERT_RESPONSE_ADDRESS	0x0c
56c1e62062SHans de Goede 
5702cdab2aSHans de Goede /* CM3218 Family */
5802cdab2aSHans de Goede static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 };
5902cdab2aSHans de Goede static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 };
6002cdab2aSHans de Goede 
6102cdab2aSHans de Goede /* CM32181 Family */
6202cdab2aSHans de Goede static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 };
6302cdab2aSHans de Goede static const int cm32181_als_it_values[] = {
6402cdab2aSHans de Goede 	25000, 50000, 100000, 200000, 400000, 800000
6502cdab2aSHans de Goede };
66971672c0SKevin Tsai 
67971672c0SKevin Tsai struct cm32181_chip {
68971672c0SKevin Tsai 	struct i2c_client *client;
69971672c0SKevin Tsai 	struct mutex lock;
70971672c0SKevin Tsai 	u16 conf_regs[CM32181_CONF_REG_NUM];
71*7574cb1dSHans de Goede 	unsigned long init_regs_bitmap;
72971672c0SKevin Tsai 	int calibscale;
7302cdab2aSHans de Goede 	int num_als_it;
7402cdab2aSHans de Goede 	const int *als_it_bits;
7502cdab2aSHans de Goede 	const int *als_it_values;
76971672c0SKevin Tsai };
77971672c0SKevin Tsai 
78971672c0SKevin Tsai /**
79971672c0SKevin Tsai  * cm32181_reg_init() - Initialize CM32181 registers
80971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
81971672c0SKevin Tsai  *
82971672c0SKevin Tsai  * Initialize CM32181 ambient light sensor register to default values.
83971672c0SKevin Tsai  *
84971672c0SKevin Tsai  * Return: 0 for success; otherwise for error code.
85971672c0SKevin Tsai  */
86971672c0SKevin Tsai static int cm32181_reg_init(struct cm32181_chip *cm32181)
87971672c0SKevin Tsai {
88971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
89971672c0SKevin Tsai 	int i;
90971672c0SKevin Tsai 	s32 ret;
91971672c0SKevin Tsai 
92971672c0SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
93971672c0SKevin Tsai 	if (ret < 0)
94971672c0SKevin Tsai 		return ret;
95971672c0SKevin Tsai 
96971672c0SKevin Tsai 	/* check device ID */
9702cdab2aSHans de Goede 	switch (ret & 0xFF) {
9802cdab2aSHans de Goede 	case 0x18: /* CM3218 */
9902cdab2aSHans de Goede 		cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits);
10002cdab2aSHans de Goede 		cm32181->als_it_bits = cm3218_als_it_bits;
10102cdab2aSHans de Goede 		cm32181->als_it_values = cm3218_als_it_values;
10202cdab2aSHans de Goede 		break;
10302cdab2aSHans de Goede 	case 0x81: /* CM32181 */
10402cdab2aSHans de Goede 	case 0x82: /* CM32182, fully compat. with CM32181 */
10502cdab2aSHans de Goede 		cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits);
10602cdab2aSHans de Goede 		cm32181->als_it_bits = cm32181_als_it_bits;
10702cdab2aSHans de Goede 		cm32181->als_it_values = cm32181_als_it_values;
10802cdab2aSHans de Goede 		break;
10902cdab2aSHans de Goede 	default:
110971672c0SKevin Tsai 		return -ENODEV;
11102cdab2aSHans de Goede 	}
112971672c0SKevin Tsai 
113971672c0SKevin Tsai 	/* Default Values */
1143bf4a59cSHans de Goede 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] =
115971672c0SKevin Tsai 			CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
116*7574cb1dSHans de Goede 	cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD);
117971672c0SKevin Tsai 	cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
118971672c0SKevin Tsai 
119971672c0SKevin Tsai 	/* Initialize registers*/
120*7574cb1dSHans de Goede 	for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) {
121*7574cb1dSHans de Goede 		ret = i2c_smbus_write_word_data(client, i,
122971672c0SKevin Tsai 						cm32181->conf_regs[i]);
123971672c0SKevin Tsai 		if (ret < 0)
124971672c0SKevin Tsai 			return ret;
125971672c0SKevin Tsai 	}
126971672c0SKevin Tsai 
127971672c0SKevin Tsai 	return 0;
128971672c0SKevin Tsai }
129971672c0SKevin Tsai 
130971672c0SKevin Tsai /**
131971672c0SKevin Tsai  *  cm32181_read_als_it() - Get sensor integration time (ms)
132971672c0SKevin Tsai  *  @cm32181:	pointer of struct cm32181
1331463a166SBeomho Seo  *  @val2:	pointer of int to load the als_it value.
134971672c0SKevin Tsai  *
135971672c0SKevin Tsai  *  Report the current integartion time by millisecond.
136971672c0SKevin Tsai  *
1371463a166SBeomho Seo  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
138971672c0SKevin Tsai  */
1391463a166SBeomho Seo static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
140971672c0SKevin Tsai {
141971672c0SKevin Tsai 	u16 als_it;
142971672c0SKevin Tsai 	int i;
143971672c0SKevin Tsai 
144971672c0SKevin Tsai 	als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
145971672c0SKevin Tsai 	als_it &= CM32181_CMD_ALS_IT_MASK;
146971672c0SKevin Tsai 	als_it >>= CM32181_CMD_ALS_IT_SHIFT;
14702cdab2aSHans de Goede 	for (i = 0; i < cm32181->num_als_it; i++) {
14802cdab2aSHans de Goede 		if (als_it == cm32181->als_it_bits[i]) {
14902cdab2aSHans de Goede 			*val2 = cm32181->als_it_values[i];
1501463a166SBeomho Seo 			return IIO_VAL_INT_PLUS_MICRO;
151971672c0SKevin Tsai 		}
152971672c0SKevin Tsai 	}
153971672c0SKevin Tsai 
154971672c0SKevin Tsai 	return -EINVAL;
155971672c0SKevin Tsai }
156971672c0SKevin Tsai 
157971672c0SKevin Tsai /**
158971672c0SKevin Tsai  * cm32181_write_als_it() - Write sensor integration time
159971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
160971672c0SKevin Tsai  * @val:	integration time by millisecond.
161971672c0SKevin Tsai  *
162971672c0SKevin Tsai  * Convert integration time (ms) to sensor value.
163971672c0SKevin Tsai  *
164971672c0SKevin Tsai  * Return: i2c_smbus_write_word_data command return value.
165971672c0SKevin Tsai  */
166971672c0SKevin Tsai static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
167971672c0SKevin Tsai {
168971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
169971672c0SKevin Tsai 	u16 als_it;
170971672c0SKevin Tsai 	int ret, i, n;
171971672c0SKevin Tsai 
17202cdab2aSHans de Goede 	n = cm32181->num_als_it;
173971672c0SKevin Tsai 	for (i = 0; i < n; i++)
17402cdab2aSHans de Goede 		if (val <= cm32181->als_it_values[i])
175971672c0SKevin Tsai 			break;
176971672c0SKevin Tsai 	if (i >= n)
177971672c0SKevin Tsai 		i = n - 1;
178971672c0SKevin Tsai 
17902cdab2aSHans de Goede 	als_it = cm32181->als_it_bits[i];
180971672c0SKevin Tsai 	als_it <<= CM32181_CMD_ALS_IT_SHIFT;
181971672c0SKevin Tsai 
182971672c0SKevin Tsai 	mutex_lock(&cm32181->lock);
183971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
184971672c0SKevin Tsai 		~CM32181_CMD_ALS_IT_MASK;
185971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
186971672c0SKevin Tsai 		als_it;
187971672c0SKevin Tsai 	ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
188971672c0SKevin Tsai 			cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
189971672c0SKevin Tsai 	mutex_unlock(&cm32181->lock);
190971672c0SKevin Tsai 
191971672c0SKevin Tsai 	return ret;
192971672c0SKevin Tsai }
193971672c0SKevin Tsai 
194971672c0SKevin Tsai /**
195971672c0SKevin Tsai  * cm32181_get_lux() - report current lux value
196971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
197971672c0SKevin Tsai  *
198971672c0SKevin Tsai  * Convert sensor raw data to lux.  It depends on integration
199a86ea557SRoberta Dobrescu  * time and calibscale variable.
200971672c0SKevin Tsai  *
201971672c0SKevin Tsai  * Return: Positive value is lux, otherwise is error code.
202971672c0SKevin Tsai  */
203971672c0SKevin Tsai static int cm32181_get_lux(struct cm32181_chip *cm32181)
204971672c0SKevin Tsai {
205971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
206971672c0SKevin Tsai 	int ret;
207971672c0SKevin Tsai 	int als_it;
208971672c0SKevin Tsai 	unsigned long lux;
209971672c0SKevin Tsai 
210971672c0SKevin Tsai 	ret = cm32181_read_als_it(cm32181, &als_it);
211971672c0SKevin Tsai 	if (ret < 0)
212971672c0SKevin Tsai 		return -EINVAL;
213971672c0SKevin Tsai 
214971672c0SKevin Tsai 	lux = CM32181_MLUX_PER_BIT;
215971672c0SKevin Tsai 	lux *= CM32181_MLUX_PER_BIT_BASE_IT;
216971672c0SKevin Tsai 	lux /= als_it;
217971672c0SKevin Tsai 
218971672c0SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
219971672c0SKevin Tsai 	if (ret < 0)
220971672c0SKevin Tsai 		return ret;
221971672c0SKevin Tsai 
222971672c0SKevin Tsai 	lux *= ret;
223971672c0SKevin Tsai 	lux *= cm32181->calibscale;
224971672c0SKevin Tsai 	lux /= CM32181_CALIBSCALE_RESOLUTION;
225971672c0SKevin Tsai 	lux /= MLUX_PER_LUX;
226971672c0SKevin Tsai 
227971672c0SKevin Tsai 	if (lux > 0xFFFF)
228971672c0SKevin Tsai 		lux = 0xFFFF;
229971672c0SKevin Tsai 
230971672c0SKevin Tsai 	return lux;
231971672c0SKevin Tsai }
232971672c0SKevin Tsai 
233971672c0SKevin Tsai static int cm32181_read_raw(struct iio_dev *indio_dev,
234971672c0SKevin Tsai 			    struct iio_chan_spec const *chan,
235971672c0SKevin Tsai 			    int *val, int *val2, long mask)
236971672c0SKevin Tsai {
237971672c0SKevin Tsai 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
238971672c0SKevin Tsai 	int ret;
239971672c0SKevin Tsai 
240971672c0SKevin Tsai 	switch (mask) {
241971672c0SKevin Tsai 	case IIO_CHAN_INFO_PROCESSED:
242971672c0SKevin Tsai 		ret = cm32181_get_lux(cm32181);
243971672c0SKevin Tsai 		if (ret < 0)
244971672c0SKevin Tsai 			return ret;
245971672c0SKevin Tsai 		*val = ret;
246971672c0SKevin Tsai 		return IIO_VAL_INT;
247971672c0SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
248971672c0SKevin Tsai 		*val = cm32181->calibscale;
249971672c0SKevin Tsai 		return IIO_VAL_INT;
250971672c0SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
25141c897f8SBeomho Seo 		*val = 0;
2521463a166SBeomho Seo 		ret = cm32181_read_als_it(cm32181, val2);
253971672c0SKevin Tsai 		return ret;
254971672c0SKevin Tsai 	}
255971672c0SKevin Tsai 
256971672c0SKevin Tsai 	return -EINVAL;
257971672c0SKevin Tsai }
258971672c0SKevin Tsai 
259971672c0SKevin Tsai static int cm32181_write_raw(struct iio_dev *indio_dev,
260971672c0SKevin Tsai 			     struct iio_chan_spec const *chan,
261971672c0SKevin Tsai 			     int val, int val2, long mask)
262971672c0SKevin Tsai {
263971672c0SKevin Tsai 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
264971672c0SKevin Tsai 	int ret;
265971672c0SKevin Tsai 
266971672c0SKevin Tsai 	switch (mask) {
267971672c0SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
268971672c0SKevin Tsai 		cm32181->calibscale = val;
269971672c0SKevin Tsai 		return val;
270971672c0SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
2711463a166SBeomho Seo 		ret = cm32181_write_als_it(cm32181, val2);
272971672c0SKevin Tsai 		return ret;
273971672c0SKevin Tsai 	}
274971672c0SKevin Tsai 
275971672c0SKevin Tsai 	return -EINVAL;
276971672c0SKevin Tsai }
277971672c0SKevin Tsai 
278971672c0SKevin Tsai /**
279971672c0SKevin Tsai  * cm32181_get_it_available() - Get available ALS IT value
280971672c0SKevin Tsai  * @dev:	pointer of struct device.
281971672c0SKevin Tsai  * @attr:	pointer of struct device_attribute.
282971672c0SKevin Tsai  * @buf:	pointer of return string buffer.
283971672c0SKevin Tsai  *
284971672c0SKevin Tsai  * Display the available integration time values by millisecond.
285971672c0SKevin Tsai  *
286971672c0SKevin Tsai  * Return: string length.
287971672c0SKevin Tsai  */
288971672c0SKevin Tsai static ssize_t cm32181_get_it_available(struct device *dev,
289971672c0SKevin Tsai 			struct device_attribute *attr, char *buf)
290971672c0SKevin Tsai {
29102cdab2aSHans de Goede 	struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev));
292971672c0SKevin Tsai 	int i, n, len;
293971672c0SKevin Tsai 
29402cdab2aSHans de Goede 	n = cm32181->num_als_it;
295971672c0SKevin Tsai 	for (i = 0, len = 0; i < n; i++)
29602cdab2aSHans de Goede 		len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]);
297971672c0SKevin Tsai 	return len + sprintf(buf + len, "\n");
298971672c0SKevin Tsai }
299971672c0SKevin Tsai 
300971672c0SKevin Tsai static const struct iio_chan_spec cm32181_channels[] = {
301971672c0SKevin Tsai 	{
302971672c0SKevin Tsai 		.type = IIO_LIGHT,
303971672c0SKevin Tsai 		.info_mask_separate =
304971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_PROCESSED) |
305971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_CALIBSCALE) |
306971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_INT_TIME),
307971672c0SKevin Tsai 	}
308971672c0SKevin Tsai };
309971672c0SKevin Tsai 
310971672c0SKevin Tsai static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
311971672c0SKevin Tsai 			S_IRUGO, cm32181_get_it_available, NULL, 0);
312971672c0SKevin Tsai 
313971672c0SKevin Tsai static struct attribute *cm32181_attributes[] = {
314971672c0SKevin Tsai 	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
315971672c0SKevin Tsai 	NULL,
316971672c0SKevin Tsai };
317971672c0SKevin Tsai 
318971672c0SKevin Tsai static const struct attribute_group cm32181_attribute_group = {
319971672c0SKevin Tsai 	.attrs = cm32181_attributes
320971672c0SKevin Tsai };
321971672c0SKevin Tsai 
322971672c0SKevin Tsai static const struct iio_info cm32181_info = {
323971672c0SKevin Tsai 	.read_raw		= &cm32181_read_raw,
324971672c0SKevin Tsai 	.write_raw		= &cm32181_write_raw,
325971672c0SKevin Tsai 	.attrs			= &cm32181_attribute_group,
326971672c0SKevin Tsai };
327971672c0SKevin Tsai 
32806770454SHans de Goede static int cm32181_probe(struct i2c_client *client)
329971672c0SKevin Tsai {
330b885d0faSHans de Goede 	struct device *dev = &client->dev;
331971672c0SKevin Tsai 	struct cm32181_chip *cm32181;
332971672c0SKevin Tsai 	struct iio_dev *indio_dev;
333971672c0SKevin Tsai 	int ret;
334971672c0SKevin Tsai 
335b885d0faSHans de Goede 	indio_dev = devm_iio_device_alloc(dev, sizeof(*cm32181));
336b885d0faSHans de Goede 	if (!indio_dev)
337971672c0SKevin Tsai 		return -ENOMEM;
338971672c0SKevin Tsai 
339c1e62062SHans de Goede 	/*
340c1e62062SHans de Goede 	 * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the
341c1e62062SHans de Goede 	 * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address.
342c1e62062SHans de Goede 	 * Detect this and take the following step to deal with it:
343c1e62062SHans de Goede 	 * 1. When a SMBus Alert capable sensor has an Alert asserted, it will
344c1e62062SHans de Goede 	 *    not respond on its actual I2C address. Read a byte from the ARA
345c1e62062SHans de Goede 	 *    to clear any pending Alerts.
346c1e62062SHans de Goede 	 * 2. Create a "dummy" client for the actual I2C address and
347c1e62062SHans de Goede 	 *    use that client to communicate with the sensor.
348c1e62062SHans de Goede 	 */
349c1e62062SHans de Goede 	if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {
350c1e62062SHans de Goede 		struct i2c_board_info board_info = { .type = "dummy" };
351c1e62062SHans de Goede 
352c1e62062SHans de Goede 		i2c_smbus_read_byte(client);
353c1e62062SHans de Goede 
354c1e62062SHans de Goede 		client = i2c_acpi_new_device(dev, 1, &board_info);
355c1e62062SHans de Goede 		if (IS_ERR(client))
356c1e62062SHans de Goede 			return PTR_ERR(client);
357c1e62062SHans de Goede 	}
358c1e62062SHans de Goede 
359971672c0SKevin Tsai 	cm32181 = iio_priv(indio_dev);
360971672c0SKevin Tsai 	cm32181->client = client;
361971672c0SKevin Tsai 
362971672c0SKevin Tsai 	mutex_init(&cm32181->lock);
363b885d0faSHans de Goede 	indio_dev->dev.parent = dev;
364971672c0SKevin Tsai 	indio_dev->channels = cm32181_channels;
365971672c0SKevin Tsai 	indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
366971672c0SKevin Tsai 	indio_dev->info = &cm32181_info;
367b885d0faSHans de Goede 	indio_dev->name = dev_name(dev);
368971672c0SKevin Tsai 	indio_dev->modes = INDIO_DIRECT_MODE;
369971672c0SKevin Tsai 
370971672c0SKevin Tsai 	ret = cm32181_reg_init(cm32181);
371971672c0SKevin Tsai 	if (ret) {
372b885d0faSHans de Goede 		dev_err(dev, "%s: register init failed\n", __func__);
373971672c0SKevin Tsai 		return ret;
374971672c0SKevin Tsai 	}
375971672c0SKevin Tsai 
376b885d0faSHans de Goede 	ret = devm_iio_device_register(dev, indio_dev);
377971672c0SKevin Tsai 	if (ret) {
378b885d0faSHans de Goede 		dev_err(dev, "%s: regist device failed\n", __func__);
379971672c0SKevin Tsai 		return ret;
380971672c0SKevin Tsai 	}
381971672c0SKevin Tsai 
382971672c0SKevin Tsai 	return 0;
383971672c0SKevin Tsai }
384971672c0SKevin Tsai 
385971672c0SKevin Tsai static const struct of_device_id cm32181_of_match[] = {
38602cdab2aSHans de Goede 	{ .compatible = "capella,cm3218" },
387971672c0SKevin Tsai 	{ .compatible = "capella,cm32181" },
388971672c0SKevin Tsai 	{ }
389971672c0SKevin Tsai };
390119c4fceSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, cm32181_of_match);
391971672c0SKevin Tsai 
39244b9409cSHans de Goede #ifdef CONFIG_ACPI
39344b9409cSHans de Goede static const struct acpi_device_id cm32181_acpi_match[] = {
39444b9409cSHans de Goede 	{ "CPLM3218", 0 },
39544b9409cSHans de Goede 	{ }
39644b9409cSHans de Goede };
39744b9409cSHans de Goede MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match);
39844b9409cSHans de Goede #endif
39944b9409cSHans de Goede 
400971672c0SKevin Tsai static struct i2c_driver cm32181_driver = {
401971672c0SKevin Tsai 	.driver = {
402971672c0SKevin Tsai 		.name	= "cm32181",
40344b9409cSHans de Goede 		.acpi_match_table = ACPI_PTR(cm32181_acpi_match),
4049065b780SJonathan Cameron 		.of_match_table = cm32181_of_match,
405971672c0SKevin Tsai 	},
40606770454SHans de Goede 	.probe_new	= cm32181_probe,
407971672c0SKevin Tsai };
408971672c0SKevin Tsai 
409971672c0SKevin Tsai module_i2c_driver(cm32181_driver);
410971672c0SKevin Tsai 
411971672c0SKevin Tsai MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
412971672c0SKevin Tsai MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
413971672c0SKevin Tsai MODULE_LICENSE("GPL");
414