xref: /linux/drivers/iio/light/cm32181.c (revision c1e62062ff5477f3cd40e956fb1c18808cc894a4)
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 */
30971672c0SKevin Tsai #define CM32181_CONF_REG_NUM		0x01
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 
55*c1e62062SHans de Goede #define SMBUS_ALERT_RESPONSE_ADDRESS	0x0c
56*c1e62062SHans de Goede 
57971672c0SKevin Tsai static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = {
58971672c0SKevin Tsai 	CM32181_REG_ADDR_CMD,
59971672c0SKevin Tsai };
60971672c0SKevin Tsai 
6102cdab2aSHans de Goede /* CM3218 Family */
6202cdab2aSHans de Goede static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 };
6302cdab2aSHans de Goede static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 };
6402cdab2aSHans de Goede 
6502cdab2aSHans de Goede /* CM32181 Family */
6602cdab2aSHans de Goede static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 };
6702cdab2aSHans de Goede static const int cm32181_als_it_values[] = {
6802cdab2aSHans de Goede 	25000, 50000, 100000, 200000, 400000, 800000
6902cdab2aSHans de Goede };
70971672c0SKevin Tsai 
71971672c0SKevin Tsai struct cm32181_chip {
72971672c0SKevin Tsai 	struct i2c_client *client;
73971672c0SKevin Tsai 	struct mutex lock;
74971672c0SKevin Tsai 	u16 conf_regs[CM32181_CONF_REG_NUM];
75971672c0SKevin Tsai 	int calibscale;
7602cdab2aSHans de Goede 	int num_als_it;
7702cdab2aSHans de Goede 	const int *als_it_bits;
7802cdab2aSHans de Goede 	const int *als_it_values;
79971672c0SKevin Tsai };
80971672c0SKevin Tsai 
81971672c0SKevin Tsai /**
82971672c0SKevin Tsai  * cm32181_reg_init() - Initialize CM32181 registers
83971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
84971672c0SKevin Tsai  *
85971672c0SKevin Tsai  * Initialize CM32181 ambient light sensor register to default values.
86971672c0SKevin Tsai  *
87971672c0SKevin Tsai  * Return: 0 for success; otherwise for error code.
88971672c0SKevin Tsai  */
89971672c0SKevin Tsai static int cm32181_reg_init(struct cm32181_chip *cm32181)
90971672c0SKevin Tsai {
91971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
92971672c0SKevin Tsai 	int i;
93971672c0SKevin Tsai 	s32 ret;
94971672c0SKevin Tsai 
95971672c0SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID);
96971672c0SKevin Tsai 	if (ret < 0)
97971672c0SKevin Tsai 		return ret;
98971672c0SKevin Tsai 
99971672c0SKevin Tsai 	/* check device ID */
10002cdab2aSHans de Goede 	switch (ret & 0xFF) {
10102cdab2aSHans de Goede 	case 0x18: /* CM3218 */
10202cdab2aSHans de Goede 		cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits);
10302cdab2aSHans de Goede 		cm32181->als_it_bits = cm3218_als_it_bits;
10402cdab2aSHans de Goede 		cm32181->als_it_values = cm3218_als_it_values;
10502cdab2aSHans de Goede 		break;
10602cdab2aSHans de Goede 	case 0x81: /* CM32181 */
10702cdab2aSHans de Goede 	case 0x82: /* CM32182, fully compat. with CM32181 */
10802cdab2aSHans de Goede 		cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits);
10902cdab2aSHans de Goede 		cm32181->als_it_bits = cm32181_als_it_bits;
11002cdab2aSHans de Goede 		cm32181->als_it_values = cm32181_als_it_values;
11102cdab2aSHans de Goede 		break;
11202cdab2aSHans de Goede 	default:
113971672c0SKevin Tsai 		return -ENODEV;
11402cdab2aSHans de Goede 	}
115971672c0SKevin Tsai 
116971672c0SKevin Tsai 	/* Default Values */
1173bf4a59cSHans de Goede 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] =
118971672c0SKevin Tsai 			CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT;
119971672c0SKevin Tsai 	cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT;
120971672c0SKevin Tsai 
121971672c0SKevin Tsai 	/* Initialize registers*/
122971672c0SKevin Tsai 	for (i = 0; i < CM32181_CONF_REG_NUM; i++) {
123971672c0SKevin Tsai 		ret = i2c_smbus_write_word_data(client, cm32181_reg[i],
124971672c0SKevin Tsai 			cm32181->conf_regs[i]);
125971672c0SKevin Tsai 		if (ret < 0)
126971672c0SKevin Tsai 			return ret;
127971672c0SKevin Tsai 	}
128971672c0SKevin Tsai 
129971672c0SKevin Tsai 	return 0;
130971672c0SKevin Tsai }
131971672c0SKevin Tsai 
132971672c0SKevin Tsai /**
133971672c0SKevin Tsai  *  cm32181_read_als_it() - Get sensor integration time (ms)
134971672c0SKevin Tsai  *  @cm32181:	pointer of struct cm32181
1351463a166SBeomho Seo  *  @val2:	pointer of int to load the als_it value.
136971672c0SKevin Tsai  *
137971672c0SKevin Tsai  *  Report the current integartion time by millisecond.
138971672c0SKevin Tsai  *
1391463a166SBeomho Seo  *  Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL.
140971672c0SKevin Tsai  */
1411463a166SBeomho Seo static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2)
142971672c0SKevin Tsai {
143971672c0SKevin Tsai 	u16 als_it;
144971672c0SKevin Tsai 	int i;
145971672c0SKevin Tsai 
146971672c0SKevin Tsai 	als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD];
147971672c0SKevin Tsai 	als_it &= CM32181_CMD_ALS_IT_MASK;
148971672c0SKevin Tsai 	als_it >>= CM32181_CMD_ALS_IT_SHIFT;
14902cdab2aSHans de Goede 	for (i = 0; i < cm32181->num_als_it; i++) {
15002cdab2aSHans de Goede 		if (als_it == cm32181->als_it_bits[i]) {
15102cdab2aSHans de Goede 			*val2 = cm32181->als_it_values[i];
1521463a166SBeomho Seo 			return IIO_VAL_INT_PLUS_MICRO;
153971672c0SKevin Tsai 		}
154971672c0SKevin Tsai 	}
155971672c0SKevin Tsai 
156971672c0SKevin Tsai 	return -EINVAL;
157971672c0SKevin Tsai }
158971672c0SKevin Tsai 
159971672c0SKevin Tsai /**
160971672c0SKevin Tsai  * cm32181_write_als_it() - Write sensor integration time
161971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
162971672c0SKevin Tsai  * @val:	integration time by millisecond.
163971672c0SKevin Tsai  *
164971672c0SKevin Tsai  * Convert integration time (ms) to sensor value.
165971672c0SKevin Tsai  *
166971672c0SKevin Tsai  * Return: i2c_smbus_write_word_data command return value.
167971672c0SKevin Tsai  */
168971672c0SKevin Tsai static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val)
169971672c0SKevin Tsai {
170971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
171971672c0SKevin Tsai 	u16 als_it;
172971672c0SKevin Tsai 	int ret, i, n;
173971672c0SKevin Tsai 
17402cdab2aSHans de Goede 	n = cm32181->num_als_it;
175971672c0SKevin Tsai 	for (i = 0; i < n; i++)
17602cdab2aSHans de Goede 		if (val <= cm32181->als_it_values[i])
177971672c0SKevin Tsai 			break;
178971672c0SKevin Tsai 	if (i >= n)
179971672c0SKevin Tsai 		i = n - 1;
180971672c0SKevin Tsai 
18102cdab2aSHans de Goede 	als_it = cm32181->als_it_bits[i];
182971672c0SKevin Tsai 	als_it <<= CM32181_CMD_ALS_IT_SHIFT;
183971672c0SKevin Tsai 
184971672c0SKevin Tsai 	mutex_lock(&cm32181->lock);
185971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] &=
186971672c0SKevin Tsai 		~CM32181_CMD_ALS_IT_MASK;
187971672c0SKevin Tsai 	cm32181->conf_regs[CM32181_REG_ADDR_CMD] |=
188971672c0SKevin Tsai 		als_it;
189971672c0SKevin Tsai 	ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD,
190971672c0SKevin Tsai 			cm32181->conf_regs[CM32181_REG_ADDR_CMD]);
191971672c0SKevin Tsai 	mutex_unlock(&cm32181->lock);
192971672c0SKevin Tsai 
193971672c0SKevin Tsai 	return ret;
194971672c0SKevin Tsai }
195971672c0SKevin Tsai 
196971672c0SKevin Tsai /**
197971672c0SKevin Tsai  * cm32181_get_lux() - report current lux value
198971672c0SKevin Tsai  * @cm32181:	pointer of struct cm32181.
199971672c0SKevin Tsai  *
200971672c0SKevin Tsai  * Convert sensor raw data to lux.  It depends on integration
201a86ea557SRoberta Dobrescu  * time and calibscale variable.
202971672c0SKevin Tsai  *
203971672c0SKevin Tsai  * Return: Positive value is lux, otherwise is error code.
204971672c0SKevin Tsai  */
205971672c0SKevin Tsai static int cm32181_get_lux(struct cm32181_chip *cm32181)
206971672c0SKevin Tsai {
207971672c0SKevin Tsai 	struct i2c_client *client = cm32181->client;
208971672c0SKevin Tsai 	int ret;
209971672c0SKevin Tsai 	int als_it;
210971672c0SKevin Tsai 	unsigned long lux;
211971672c0SKevin Tsai 
212971672c0SKevin Tsai 	ret = cm32181_read_als_it(cm32181, &als_it);
213971672c0SKevin Tsai 	if (ret < 0)
214971672c0SKevin Tsai 		return -EINVAL;
215971672c0SKevin Tsai 
216971672c0SKevin Tsai 	lux = CM32181_MLUX_PER_BIT;
217971672c0SKevin Tsai 	lux *= CM32181_MLUX_PER_BIT_BASE_IT;
218971672c0SKevin Tsai 	lux /= als_it;
219971672c0SKevin Tsai 
220971672c0SKevin Tsai 	ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS);
221971672c0SKevin Tsai 	if (ret < 0)
222971672c0SKevin Tsai 		return ret;
223971672c0SKevin Tsai 
224971672c0SKevin Tsai 	lux *= ret;
225971672c0SKevin Tsai 	lux *= cm32181->calibscale;
226971672c0SKevin Tsai 	lux /= CM32181_CALIBSCALE_RESOLUTION;
227971672c0SKevin Tsai 	lux /= MLUX_PER_LUX;
228971672c0SKevin Tsai 
229971672c0SKevin Tsai 	if (lux > 0xFFFF)
230971672c0SKevin Tsai 		lux = 0xFFFF;
231971672c0SKevin Tsai 
232971672c0SKevin Tsai 	return lux;
233971672c0SKevin Tsai }
234971672c0SKevin Tsai 
235971672c0SKevin Tsai static int cm32181_read_raw(struct iio_dev *indio_dev,
236971672c0SKevin Tsai 			    struct iio_chan_spec const *chan,
237971672c0SKevin Tsai 			    int *val, int *val2, long mask)
238971672c0SKevin Tsai {
239971672c0SKevin Tsai 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
240971672c0SKevin Tsai 	int ret;
241971672c0SKevin Tsai 
242971672c0SKevin Tsai 	switch (mask) {
243971672c0SKevin Tsai 	case IIO_CHAN_INFO_PROCESSED:
244971672c0SKevin Tsai 		ret = cm32181_get_lux(cm32181);
245971672c0SKevin Tsai 		if (ret < 0)
246971672c0SKevin Tsai 			return ret;
247971672c0SKevin Tsai 		*val = ret;
248971672c0SKevin Tsai 		return IIO_VAL_INT;
249971672c0SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
250971672c0SKevin Tsai 		*val = cm32181->calibscale;
251971672c0SKevin Tsai 		return IIO_VAL_INT;
252971672c0SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
25341c897f8SBeomho Seo 		*val = 0;
2541463a166SBeomho Seo 		ret = cm32181_read_als_it(cm32181, val2);
255971672c0SKevin Tsai 		return ret;
256971672c0SKevin Tsai 	}
257971672c0SKevin Tsai 
258971672c0SKevin Tsai 	return -EINVAL;
259971672c0SKevin Tsai }
260971672c0SKevin Tsai 
261971672c0SKevin Tsai static int cm32181_write_raw(struct iio_dev *indio_dev,
262971672c0SKevin Tsai 			     struct iio_chan_spec const *chan,
263971672c0SKevin Tsai 			     int val, int val2, long mask)
264971672c0SKevin Tsai {
265971672c0SKevin Tsai 	struct cm32181_chip *cm32181 = iio_priv(indio_dev);
266971672c0SKevin Tsai 	int ret;
267971672c0SKevin Tsai 
268971672c0SKevin Tsai 	switch (mask) {
269971672c0SKevin Tsai 	case IIO_CHAN_INFO_CALIBSCALE:
270971672c0SKevin Tsai 		cm32181->calibscale = val;
271971672c0SKevin Tsai 		return val;
272971672c0SKevin Tsai 	case IIO_CHAN_INFO_INT_TIME:
2731463a166SBeomho Seo 		ret = cm32181_write_als_it(cm32181, val2);
274971672c0SKevin Tsai 		return ret;
275971672c0SKevin Tsai 	}
276971672c0SKevin Tsai 
277971672c0SKevin Tsai 	return -EINVAL;
278971672c0SKevin Tsai }
279971672c0SKevin Tsai 
280971672c0SKevin Tsai /**
281971672c0SKevin Tsai  * cm32181_get_it_available() - Get available ALS IT value
282971672c0SKevin Tsai  * @dev:	pointer of struct device.
283971672c0SKevin Tsai  * @attr:	pointer of struct device_attribute.
284971672c0SKevin Tsai  * @buf:	pointer of return string buffer.
285971672c0SKevin Tsai  *
286971672c0SKevin Tsai  * Display the available integration time values by millisecond.
287971672c0SKevin Tsai  *
288971672c0SKevin Tsai  * Return: string length.
289971672c0SKevin Tsai  */
290971672c0SKevin Tsai static ssize_t cm32181_get_it_available(struct device *dev,
291971672c0SKevin Tsai 			struct device_attribute *attr, char *buf)
292971672c0SKevin Tsai {
29302cdab2aSHans de Goede 	struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev));
294971672c0SKevin Tsai 	int i, n, len;
295971672c0SKevin Tsai 
29602cdab2aSHans de Goede 	n = cm32181->num_als_it;
297971672c0SKevin Tsai 	for (i = 0, len = 0; i < n; i++)
29802cdab2aSHans de Goede 		len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]);
299971672c0SKevin Tsai 	return len + sprintf(buf + len, "\n");
300971672c0SKevin Tsai }
301971672c0SKevin Tsai 
302971672c0SKevin Tsai static const struct iio_chan_spec cm32181_channels[] = {
303971672c0SKevin Tsai 	{
304971672c0SKevin Tsai 		.type = IIO_LIGHT,
305971672c0SKevin Tsai 		.info_mask_separate =
306971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_PROCESSED) |
307971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_CALIBSCALE) |
308971672c0SKevin Tsai 			BIT(IIO_CHAN_INFO_INT_TIME),
309971672c0SKevin Tsai 	}
310971672c0SKevin Tsai };
311971672c0SKevin Tsai 
312971672c0SKevin Tsai static IIO_DEVICE_ATTR(in_illuminance_integration_time_available,
313971672c0SKevin Tsai 			S_IRUGO, cm32181_get_it_available, NULL, 0);
314971672c0SKevin Tsai 
315971672c0SKevin Tsai static struct attribute *cm32181_attributes[] = {
316971672c0SKevin Tsai 	&iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr,
317971672c0SKevin Tsai 	NULL,
318971672c0SKevin Tsai };
319971672c0SKevin Tsai 
320971672c0SKevin Tsai static const struct attribute_group cm32181_attribute_group = {
321971672c0SKevin Tsai 	.attrs = cm32181_attributes
322971672c0SKevin Tsai };
323971672c0SKevin Tsai 
324971672c0SKevin Tsai static const struct iio_info cm32181_info = {
325971672c0SKevin Tsai 	.read_raw		= &cm32181_read_raw,
326971672c0SKevin Tsai 	.write_raw		= &cm32181_write_raw,
327971672c0SKevin Tsai 	.attrs			= &cm32181_attribute_group,
328971672c0SKevin Tsai };
329971672c0SKevin Tsai 
33006770454SHans de Goede static int cm32181_probe(struct i2c_client *client)
331971672c0SKevin Tsai {
332b885d0faSHans de Goede 	struct device *dev = &client->dev;
333971672c0SKevin Tsai 	struct cm32181_chip *cm32181;
334971672c0SKevin Tsai 	struct iio_dev *indio_dev;
335971672c0SKevin Tsai 	int ret;
336971672c0SKevin Tsai 
337b885d0faSHans de Goede 	indio_dev = devm_iio_device_alloc(dev, sizeof(*cm32181));
338b885d0faSHans de Goede 	if (!indio_dev)
339971672c0SKevin Tsai 		return -ENOMEM;
340971672c0SKevin Tsai 
341*c1e62062SHans de Goede 	/*
342*c1e62062SHans de Goede 	 * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the
343*c1e62062SHans de Goede 	 * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address.
344*c1e62062SHans de Goede 	 * Detect this and take the following step to deal with it:
345*c1e62062SHans de Goede 	 * 1. When a SMBus Alert capable sensor has an Alert asserted, it will
346*c1e62062SHans de Goede 	 *    not respond on its actual I2C address. Read a byte from the ARA
347*c1e62062SHans de Goede 	 *    to clear any pending Alerts.
348*c1e62062SHans de Goede 	 * 2. Create a "dummy" client for the actual I2C address and
349*c1e62062SHans de Goede 	 *    use that client to communicate with the sensor.
350*c1e62062SHans de Goede 	 */
351*c1e62062SHans de Goede 	if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) {
352*c1e62062SHans de Goede 		struct i2c_board_info board_info = { .type = "dummy" };
353*c1e62062SHans de Goede 
354*c1e62062SHans de Goede 		i2c_smbus_read_byte(client);
355*c1e62062SHans de Goede 
356*c1e62062SHans de Goede 		client = i2c_acpi_new_device(dev, 1, &board_info);
357*c1e62062SHans de Goede 		if (IS_ERR(client))
358*c1e62062SHans de Goede 			return PTR_ERR(client);
359*c1e62062SHans de Goede 	}
360*c1e62062SHans de Goede 
361971672c0SKevin Tsai 	cm32181 = iio_priv(indio_dev);
362971672c0SKevin Tsai 	cm32181->client = client;
363971672c0SKevin Tsai 
364971672c0SKevin Tsai 	mutex_init(&cm32181->lock);
365b885d0faSHans de Goede 	indio_dev->dev.parent = dev;
366971672c0SKevin Tsai 	indio_dev->channels = cm32181_channels;
367971672c0SKevin Tsai 	indio_dev->num_channels = ARRAY_SIZE(cm32181_channels);
368971672c0SKevin Tsai 	indio_dev->info = &cm32181_info;
369b885d0faSHans de Goede 	indio_dev->name = dev_name(dev);
370971672c0SKevin Tsai 	indio_dev->modes = INDIO_DIRECT_MODE;
371971672c0SKevin Tsai 
372971672c0SKevin Tsai 	ret = cm32181_reg_init(cm32181);
373971672c0SKevin Tsai 	if (ret) {
374b885d0faSHans de Goede 		dev_err(dev, "%s: register init failed\n", __func__);
375971672c0SKevin Tsai 		return ret;
376971672c0SKevin Tsai 	}
377971672c0SKevin Tsai 
378b885d0faSHans de Goede 	ret = devm_iio_device_register(dev, indio_dev);
379971672c0SKevin Tsai 	if (ret) {
380b885d0faSHans de Goede 		dev_err(dev, "%s: regist device failed\n", __func__);
381971672c0SKevin Tsai 		return ret;
382971672c0SKevin Tsai 	}
383971672c0SKevin Tsai 
384971672c0SKevin Tsai 	return 0;
385971672c0SKevin Tsai }
386971672c0SKevin Tsai 
387971672c0SKevin Tsai static const struct of_device_id cm32181_of_match[] = {
38802cdab2aSHans de Goede 	{ .compatible = "capella,cm3218" },
389971672c0SKevin Tsai 	{ .compatible = "capella,cm32181" },
390971672c0SKevin Tsai 	{ }
391971672c0SKevin Tsai };
392119c4fceSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, cm32181_of_match);
393971672c0SKevin Tsai 
39444b9409cSHans de Goede #ifdef CONFIG_ACPI
39544b9409cSHans de Goede static const struct acpi_device_id cm32181_acpi_match[] = {
39644b9409cSHans de Goede 	{ "CPLM3218", 0 },
39744b9409cSHans de Goede 	{ }
39844b9409cSHans de Goede };
39944b9409cSHans de Goede MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match);
40044b9409cSHans de Goede #endif
40144b9409cSHans de Goede 
402971672c0SKevin Tsai static struct i2c_driver cm32181_driver = {
403971672c0SKevin Tsai 	.driver = {
404971672c0SKevin Tsai 		.name	= "cm32181",
40544b9409cSHans de Goede 		.acpi_match_table = ACPI_PTR(cm32181_acpi_match),
4069065b780SJonathan Cameron 		.of_match_table = cm32181_of_match,
407971672c0SKevin Tsai 	},
40806770454SHans de Goede 	.probe_new	= cm32181_probe,
409971672c0SKevin Tsai };
410971672c0SKevin Tsai 
411971672c0SKevin Tsai module_i2c_driver(cm32181_driver);
412971672c0SKevin Tsai 
413971672c0SKevin Tsai MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>");
414971672c0SKevin Tsai MODULE_DESCRIPTION("CM32181 ambient light sensor driver");
415971672c0SKevin Tsai MODULE_LICENSE("GPL");
416