xref: /linux/drivers/iio/light/cm36651.c (revision 8283b95455ca962751d6851bf085dbe0b1e88a95)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e590d451SBeomho Seo /*
3e590d451SBeomho Seo  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
4e590d451SBeomho Seo  * Author: Beomho Seo <beomho.seo@samsung.com>
5e590d451SBeomho Seo  */
6e590d451SBeomho Seo 
7e590d451SBeomho Seo #include <linux/delay.h>
8e590d451SBeomho Seo #include <linux/err.h>
9e590d451SBeomho Seo #include <linux/i2c.h>
10e590d451SBeomho Seo #include <linux/mutex.h>
11e590d451SBeomho Seo #include <linux/module.h>
12e590d451SBeomho Seo #include <linux/interrupt.h>
13e590d451SBeomho Seo #include <linux/regulator/consumer.h>
14e590d451SBeomho Seo #include <linux/iio/iio.h>
15e590d451SBeomho Seo #include <linux/iio/sysfs.h>
16e590d451SBeomho Seo #include <linux/iio/events.h>
17e590d451SBeomho Seo 
18e590d451SBeomho Seo /* Slave address 0x19 for PS of 7 bit addressing protocol for I2C */
19e590d451SBeomho Seo #define CM36651_I2C_ADDR_PS		0x19
20e590d451SBeomho Seo /* Alert Response Address */
21e590d451SBeomho Seo #define CM36651_ARA			0x0C
22e590d451SBeomho Seo 
23e590d451SBeomho Seo /* Ambient light sensor */
24e590d451SBeomho Seo #define CM36651_CS_CONF1		0x00
25e590d451SBeomho Seo #define CM36651_CS_CONF2		0x01
26e590d451SBeomho Seo #define CM36651_ALS_WH_M		0x02
27e590d451SBeomho Seo #define CM36651_ALS_WH_L		0x03
28e590d451SBeomho Seo #define CM36651_ALS_WL_M		0x04
29e590d451SBeomho Seo #define CM36651_ALS_WL_L		0x05
30e590d451SBeomho Seo #define CM36651_CS_CONF3		0x06
31e590d451SBeomho Seo #define CM36651_CS_CONF_REG_NUM		0x02
32e590d451SBeomho Seo 
33e590d451SBeomho Seo /* Proximity sensor */
34e590d451SBeomho Seo #define CM36651_PS_CONF1		0x00
35e590d451SBeomho Seo #define CM36651_PS_THD			0x01
36e590d451SBeomho Seo #define CM36651_PS_CANC			0x02
37e590d451SBeomho Seo #define CM36651_PS_CONF2		0x03
38e590d451SBeomho Seo #define CM36651_PS_REG_NUM		0x04
39e590d451SBeomho Seo 
40e590d451SBeomho Seo /* CS_CONF1 command code */
41e590d451SBeomho Seo #define CM36651_ALS_ENABLE		0x00
42e590d451SBeomho Seo #define CM36651_ALS_DISABLE		0x01
43e590d451SBeomho Seo #define CM36651_ALS_INT_EN		0x02
44e590d451SBeomho Seo #define CM36651_ALS_THRES		0x04
45e590d451SBeomho Seo 
46e590d451SBeomho Seo /* CS_CONF2 command code */
47e590d451SBeomho Seo #define CM36651_CS_CONF2_DEFAULT_BIT	0x08
48e590d451SBeomho Seo 
49e590d451SBeomho Seo /* CS_CONF3 channel integration time */
5026c17a1cSBeomho Seo #define CM36651_CS_IT1			0x00 /* Integration time 80 msec */
5126c17a1cSBeomho Seo #define CM36651_CS_IT2			0x40 /* Integration time 160 msec */
5226c17a1cSBeomho Seo #define CM36651_CS_IT3			0x80 /* Integration time 320 msec */
5326c17a1cSBeomho Seo #define CM36651_CS_IT4			0xC0 /* Integration time 640 msec */
54e590d451SBeomho Seo 
55e590d451SBeomho Seo /* PS_CONF1 command code */
56e590d451SBeomho Seo #define CM36651_PS_ENABLE		0x00
57e590d451SBeomho Seo #define CM36651_PS_DISABLE		0x01
58e590d451SBeomho Seo #define CM36651_PS_INT_EN		0x02
59e590d451SBeomho Seo #define CM36651_PS_PERS2		0x04
60e590d451SBeomho Seo #define CM36651_PS_PERS3		0x08
61e590d451SBeomho Seo #define CM36651_PS_PERS4		0x0C
62e590d451SBeomho Seo 
63e590d451SBeomho Seo /* PS_CONF1 command code: integration time */
6426c17a1cSBeomho Seo #define CM36651_PS_IT1			0x00 /* Integration time 0.32 msec */
6526c17a1cSBeomho Seo #define CM36651_PS_IT2			0x10 /* Integration time 0.42 msec */
6626c17a1cSBeomho Seo #define CM36651_PS_IT3			0x20 /* Integration time 0.52 msec */
6726c17a1cSBeomho Seo #define CM36651_PS_IT4			0x30 /* Integration time 0.64 msec */
68e590d451SBeomho Seo 
69e590d451SBeomho Seo /* PS_CONF1 command code: duty ratio */
70e590d451SBeomho Seo #define CM36651_PS_DR1			0x00 /* Duty ratio 1/80 */
71e590d451SBeomho Seo #define CM36651_PS_DR2			0x40 /* Duty ratio 1/160 */
72e590d451SBeomho Seo #define CM36651_PS_DR3			0x80 /* Duty ratio 1/320 */
73e590d451SBeomho Seo #define CM36651_PS_DR4			0xC0 /* Duty ratio 1/640 */
74e590d451SBeomho Seo 
75e590d451SBeomho Seo /* PS_THD command code */
76e590d451SBeomho Seo #define CM36651_PS_INITIAL_THD		0x05
77e590d451SBeomho Seo 
78e590d451SBeomho Seo /* PS_CANC command code */
79e590d451SBeomho Seo #define CM36651_PS_CANC_DEFAULT		0x00
80e590d451SBeomho Seo 
81e590d451SBeomho Seo /* PS_CONF2 command code */
82e590d451SBeomho Seo #define CM36651_PS_HYS1			0x00
83e590d451SBeomho Seo #define CM36651_PS_HYS2			0x01
84e590d451SBeomho Seo #define CM36651_PS_SMART_PERS_EN	0x02
85e590d451SBeomho Seo #define CM36651_PS_DIR_INT		0x04
86e590d451SBeomho Seo #define CM36651_PS_MS			0x10
87e590d451SBeomho Seo 
88e590d451SBeomho Seo #define CM36651_CS_COLOR_NUM		4
89e590d451SBeomho Seo 
90e590d451SBeomho Seo #define CM36651_CLOSE_PROXIMITY		0x32
91e590d451SBeomho Seo #define CM36651_FAR_PROXIMITY			0x33
92e590d451SBeomho Seo 
9326c17a1cSBeomho Seo #define CM36651_CS_INT_TIME_AVAIL	"0.08 0.16 0.32 0.64"
9426c17a1cSBeomho Seo #define CM36651_PS_INT_TIME_AVAIL	"0.000320 0.000420 0.000520 0.000640"
95e590d451SBeomho Seo 
96e590d451SBeomho Seo enum cm36651_operation_mode {
97e590d451SBeomho Seo 	CM36651_LIGHT_EN,
98e590d451SBeomho Seo 	CM36651_PROXIMITY_EN,
99e590d451SBeomho Seo 	CM36651_PROXIMITY_EV_EN,
100e590d451SBeomho Seo };
101e590d451SBeomho Seo 
102e590d451SBeomho Seo enum cm36651_light_channel_idx {
103e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL_IDX_RED,
104e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL_IDX_GREEN,
105e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL_IDX_BLUE,
106e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL_IDX_CLEAR,
107e590d451SBeomho Seo };
108e590d451SBeomho Seo 
109e590d451SBeomho Seo enum cm36651_command {
110e590d451SBeomho Seo 	CM36651_CMD_READ_RAW_LIGHT,
111e590d451SBeomho Seo 	CM36651_CMD_READ_RAW_PROXIMITY,
112e590d451SBeomho Seo 	CM36651_CMD_PROX_EV_EN,
113e590d451SBeomho Seo 	CM36651_CMD_PROX_EV_DIS,
114e590d451SBeomho Seo };
115e590d451SBeomho Seo 
116e590d451SBeomho Seo static const u8 cm36651_cs_reg[CM36651_CS_CONF_REG_NUM] = {
117e590d451SBeomho Seo 	CM36651_CS_CONF1,
118e590d451SBeomho Seo 	CM36651_CS_CONF2,
119e590d451SBeomho Seo };
120e590d451SBeomho Seo 
121e590d451SBeomho Seo static const u8 cm36651_ps_reg[CM36651_PS_REG_NUM] = {
122e590d451SBeomho Seo 	CM36651_PS_CONF1,
123e590d451SBeomho Seo 	CM36651_PS_THD,
124e590d451SBeomho Seo 	CM36651_PS_CANC,
125e590d451SBeomho Seo 	CM36651_PS_CONF2,
126e590d451SBeomho Seo };
127e590d451SBeomho Seo 
128e590d451SBeomho Seo struct cm36651_data {
129e590d451SBeomho Seo 	const struct cm36651_platform_data *pdata;
130e590d451SBeomho Seo 	struct i2c_client *client;
131e590d451SBeomho Seo 	struct i2c_client *ps_client;
132e590d451SBeomho Seo 	struct i2c_client *ara_client;
133e590d451SBeomho Seo 	struct mutex lock;
134e590d451SBeomho Seo 	struct regulator *vled_reg;
135e590d451SBeomho Seo 	unsigned long flags;
136e590d451SBeomho Seo 	int cs_int_time[CM36651_CS_COLOR_NUM];
137e590d451SBeomho Seo 	int ps_int_time;
138e590d451SBeomho Seo 	u8 cs_ctrl_regs[CM36651_CS_CONF_REG_NUM];
139e590d451SBeomho Seo 	u8 ps_ctrl_regs[CM36651_PS_REG_NUM];
140e590d451SBeomho Seo 	u16 color[CM36651_CS_COLOR_NUM];
141e590d451SBeomho Seo };
142e590d451SBeomho Seo 
143e590d451SBeomho Seo static int cm36651_setup_reg(struct cm36651_data *cm36651)
144e590d451SBeomho Seo {
145e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
146e590d451SBeomho Seo 	struct i2c_client *ps_client = cm36651->ps_client;
147e590d451SBeomho Seo 	int i, ret;
148e590d451SBeomho Seo 
149e590d451SBeomho Seo 	/* CS initialization */
150e590d451SBeomho Seo 	cm36651->cs_ctrl_regs[CM36651_CS_CONF1] = CM36651_ALS_ENABLE |
151e590d451SBeomho Seo 							     CM36651_ALS_THRES;
152e590d451SBeomho Seo 	cm36651->cs_ctrl_regs[CM36651_CS_CONF2] = CM36651_CS_CONF2_DEFAULT_BIT;
153e590d451SBeomho Seo 
154e590d451SBeomho Seo 	for (i = 0; i < CM36651_CS_CONF_REG_NUM; i++) {
155e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(client, cm36651_cs_reg[i],
156e590d451SBeomho Seo 						     cm36651->cs_ctrl_regs[i]);
157e590d451SBeomho Seo 		if (ret < 0)
158e590d451SBeomho Seo 			return ret;
159e590d451SBeomho Seo 	}
160e590d451SBeomho Seo 
161e590d451SBeomho Seo 	/* PS initialization */
162e590d451SBeomho Seo 	cm36651->ps_ctrl_regs[CM36651_PS_CONF1] = CM36651_PS_ENABLE |
163e590d451SBeomho Seo 								CM36651_PS_IT2;
164e590d451SBeomho Seo 	cm36651->ps_ctrl_regs[CM36651_PS_THD] = CM36651_PS_INITIAL_THD;
165e590d451SBeomho Seo 	cm36651->ps_ctrl_regs[CM36651_PS_CANC] = CM36651_PS_CANC_DEFAULT;
166e590d451SBeomho Seo 	cm36651->ps_ctrl_regs[CM36651_PS_CONF2] = CM36651_PS_HYS2 |
167e590d451SBeomho Seo 				CM36651_PS_DIR_INT | CM36651_PS_SMART_PERS_EN;
168e590d451SBeomho Seo 
169e590d451SBeomho Seo 	for (i = 0; i < CM36651_PS_REG_NUM; i++) {
170e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(ps_client, cm36651_ps_reg[i],
171e590d451SBeomho Seo 						     cm36651->ps_ctrl_regs[i]);
172e590d451SBeomho Seo 		if (ret < 0)
173e590d451SBeomho Seo 			return ret;
174e590d451SBeomho Seo 	}
175e590d451SBeomho Seo 
176e590d451SBeomho Seo 	/* Set shutdown mode */
177e590d451SBeomho Seo 	ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
178e590d451SBeomho Seo 							  CM36651_ALS_DISABLE);
179e590d451SBeomho Seo 	if (ret < 0)
180e590d451SBeomho Seo 		return ret;
181e590d451SBeomho Seo 
182e590d451SBeomho Seo 	ret = i2c_smbus_write_byte_data(cm36651->ps_client,
183e590d451SBeomho Seo 					 CM36651_PS_CONF1, CM36651_PS_DISABLE);
184e590d451SBeomho Seo 	if (ret < 0)
185e590d451SBeomho Seo 		return ret;
186e590d451SBeomho Seo 
187e590d451SBeomho Seo 	return 0;
188e590d451SBeomho Seo }
189e590d451SBeomho Seo 
190e590d451SBeomho Seo static int cm36651_read_output(struct cm36651_data *cm36651,
191e590d451SBeomho Seo 				struct iio_chan_spec const *chan, int *val)
192e590d451SBeomho Seo {
193e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
194e590d451SBeomho Seo 	int ret = -EINVAL;
195e590d451SBeomho Seo 
196e590d451SBeomho Seo 	switch (chan->type) {
197e590d451SBeomho Seo 	case IIO_LIGHT:
198e590d451SBeomho Seo 		*val = i2c_smbus_read_word_data(client, chan->address);
199e590d451SBeomho Seo 		if (*val < 0)
200e590d451SBeomho Seo 			return ret;
201e590d451SBeomho Seo 
202e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
203e590d451SBeomho Seo 							CM36651_ALS_DISABLE);
204e590d451SBeomho Seo 		if (ret < 0)
205e590d451SBeomho Seo 			return ret;
206e590d451SBeomho Seo 
207e590d451SBeomho Seo 		ret = IIO_VAL_INT;
208e590d451SBeomho Seo 		break;
209e590d451SBeomho Seo 	case IIO_PROXIMITY:
210e590d451SBeomho Seo 		*val = i2c_smbus_read_byte(cm36651->ps_client);
211e590d451SBeomho Seo 		if (*val < 0)
212e590d451SBeomho Seo 			return ret;
213e590d451SBeomho Seo 
214e590d451SBeomho Seo 		if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
215e590d451SBeomho Seo 			ret = i2c_smbus_write_byte_data(cm36651->ps_client,
216e590d451SBeomho Seo 					CM36651_PS_CONF1, CM36651_PS_DISABLE);
217e590d451SBeomho Seo 			if (ret < 0)
218e590d451SBeomho Seo 				return ret;
219e590d451SBeomho Seo 		}
220e590d451SBeomho Seo 
221e590d451SBeomho Seo 		ret = IIO_VAL_INT;
222e590d451SBeomho Seo 		break;
223e590d451SBeomho Seo 	default:
224e590d451SBeomho Seo 		break;
225e590d451SBeomho Seo 	}
226e590d451SBeomho Seo 
227e590d451SBeomho Seo 	return ret;
228e590d451SBeomho Seo }
229e590d451SBeomho Seo 
230e590d451SBeomho Seo static irqreturn_t cm36651_irq_handler(int irq, void *data)
231e590d451SBeomho Seo {
232e590d451SBeomho Seo 	struct iio_dev *indio_dev = data;
233e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
234e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
235e590d451SBeomho Seo 	int ev_dir, ret;
236e590d451SBeomho Seo 	u64 ev_code;
237e590d451SBeomho Seo 
238e590d451SBeomho Seo 	/*
239e590d451SBeomho Seo 	 * The PS INT pin is an active low signal that PS INT move logic low
240e590d451SBeomho Seo 	 * when the object is detect. Once the MCU host received the PS INT
241e590d451SBeomho Seo 	 * "LOW" signal, the Host needs to read the data at Alert Response
242e590d451SBeomho Seo 	 * Address(ARA) to clear the PS INT signal. After clearing the PS
243e590d451SBeomho Seo 	 * INT pin, the PS INT signal toggles from low to high.
244e590d451SBeomho Seo 	 */
245e590d451SBeomho Seo 	ret = i2c_smbus_read_byte(cm36651->ara_client);
246e590d451SBeomho Seo 	if (ret < 0) {
247e590d451SBeomho Seo 		dev_err(&client->dev,
248e590d451SBeomho Seo 				"%s: Data read failed: %d\n", __func__, ret);
249e590d451SBeomho Seo 		return IRQ_HANDLED;
250e590d451SBeomho Seo 	}
251e590d451SBeomho Seo 	switch (ret) {
252e590d451SBeomho Seo 	case CM36651_CLOSE_PROXIMITY:
253e590d451SBeomho Seo 		ev_dir = IIO_EV_DIR_RISING;
254e590d451SBeomho Seo 		break;
255e590d451SBeomho Seo 	case CM36651_FAR_PROXIMITY:
256e590d451SBeomho Seo 		ev_dir = IIO_EV_DIR_FALLING;
257e590d451SBeomho Seo 		break;
258e590d451SBeomho Seo 	default:
259e590d451SBeomho Seo 		dev_err(&client->dev,
260e590d451SBeomho Seo 			"%s: Data read wrong: %d\n", __func__, ret);
261e590d451SBeomho Seo 		return IRQ_HANDLED;
262e590d451SBeomho Seo 	}
263e590d451SBeomho Seo 
264e590d451SBeomho Seo 	ev_code = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
265e590d451SBeomho Seo 				CM36651_CMD_READ_RAW_PROXIMITY,
266e590d451SBeomho Seo 				IIO_EV_TYPE_THRESH, ev_dir);
267e590d451SBeomho Seo 
268bc2b7dabSGregor Boirie 	iio_push_event(indio_dev, ev_code, iio_get_time_ns(indio_dev));
269e590d451SBeomho Seo 
270e590d451SBeomho Seo 	return IRQ_HANDLED;
271e590d451SBeomho Seo }
272e590d451SBeomho Seo 
273e590d451SBeomho Seo static int cm36651_set_operation_mode(struct cm36651_data *cm36651, int cmd)
274e590d451SBeomho Seo {
275e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
276e590d451SBeomho Seo 	struct i2c_client *ps_client = cm36651->ps_client;
277e590d451SBeomho Seo 	int ret = -EINVAL;
278e590d451SBeomho Seo 
279e590d451SBeomho Seo 	switch (cmd) {
280e590d451SBeomho Seo 	case CM36651_CMD_READ_RAW_LIGHT:
281e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF1,
282e590d451SBeomho Seo 				cm36651->cs_ctrl_regs[CM36651_CS_CONF1]);
283e590d451SBeomho Seo 		break;
284e590d451SBeomho Seo 	case CM36651_CMD_READ_RAW_PROXIMITY:
285e590d451SBeomho Seo 		if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags))
286e590d451SBeomho Seo 			return CM36651_PROXIMITY_EV_EN;
287e590d451SBeomho Seo 
288e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(ps_client, CM36651_PS_CONF1,
289e590d451SBeomho Seo 				cm36651->ps_ctrl_regs[CM36651_PS_CONF1]);
290e590d451SBeomho Seo 		break;
291e590d451SBeomho Seo 	case CM36651_CMD_PROX_EV_EN:
292e590d451SBeomho Seo 		if (test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
293e590d451SBeomho Seo 			dev_err(&client->dev,
294e590d451SBeomho Seo 				"Already proximity event enable state\n");
295e590d451SBeomho Seo 			return ret;
296e590d451SBeomho Seo 		}
297e590d451SBeomho Seo 		set_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
298e590d451SBeomho Seo 
299e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(ps_client,
300e590d451SBeomho Seo 			cm36651_ps_reg[CM36651_PS_CONF1],
301e590d451SBeomho Seo 			CM36651_PS_INT_EN | CM36651_PS_PERS2 | CM36651_PS_IT2);
302e590d451SBeomho Seo 
303e590d451SBeomho Seo 		if (ret < 0) {
304e590d451SBeomho Seo 			dev_err(&client->dev, "Proximity enable event failed\n");
305e590d451SBeomho Seo 			return ret;
306e590d451SBeomho Seo 		}
307e590d451SBeomho Seo 		break;
308e590d451SBeomho Seo 	case CM36651_CMD_PROX_EV_DIS:
309e590d451SBeomho Seo 		if (!test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags)) {
310e590d451SBeomho Seo 			dev_err(&client->dev,
311e590d451SBeomho Seo 				"Already proximity event disable state\n");
312e590d451SBeomho Seo 			return ret;
313e590d451SBeomho Seo 		}
314e590d451SBeomho Seo 		clear_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
315e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(ps_client,
316e590d451SBeomho Seo 					CM36651_PS_CONF1, CM36651_PS_DISABLE);
317e590d451SBeomho Seo 		break;
318e590d451SBeomho Seo 	}
319e590d451SBeomho Seo 
320e590d451SBeomho Seo 	if (ret < 0)
321e590d451SBeomho Seo 		dev_err(&client->dev, "Write register failed\n");
322e590d451SBeomho Seo 
323e590d451SBeomho Seo 	return ret;
324e590d451SBeomho Seo }
325e590d451SBeomho Seo 
326e590d451SBeomho Seo static int cm36651_read_channel(struct cm36651_data *cm36651,
327e590d451SBeomho Seo 				struct iio_chan_spec const *chan, int *val)
328e590d451SBeomho Seo {
329e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
330e590d451SBeomho Seo 	int cmd, ret;
331e590d451SBeomho Seo 
332e590d451SBeomho Seo 	if (chan->type == IIO_LIGHT)
333e590d451SBeomho Seo 		cmd = CM36651_CMD_READ_RAW_LIGHT;
334e590d451SBeomho Seo 	else if (chan->type == IIO_PROXIMITY)
335e590d451SBeomho Seo 		cmd = CM36651_CMD_READ_RAW_PROXIMITY;
336e590d451SBeomho Seo 	else
337e590d451SBeomho Seo 		return -EINVAL;
338e590d451SBeomho Seo 
339e590d451SBeomho Seo 	ret = cm36651_set_operation_mode(cm36651, cmd);
340e590d451SBeomho Seo 	if (ret < 0) {
341e590d451SBeomho Seo 		dev_err(&client->dev, "CM36651 set operation mode failed\n");
342e590d451SBeomho Seo 		return ret;
343e590d451SBeomho Seo 	}
344e590d451SBeomho Seo 	/* Delay for work after enable operation */
345e590d451SBeomho Seo 	msleep(50);
346e590d451SBeomho Seo 	ret = cm36651_read_output(cm36651, chan, val);
347e590d451SBeomho Seo 	if (ret < 0) {
348e590d451SBeomho Seo 		dev_err(&client->dev, "CM36651 read output failed\n");
349e590d451SBeomho Seo 		return ret;
350e590d451SBeomho Seo 	}
351e590d451SBeomho Seo 
352e590d451SBeomho Seo 	return ret;
353e590d451SBeomho Seo }
354e590d451SBeomho Seo 
355e590d451SBeomho Seo static int cm36651_read_int_time(struct cm36651_data *cm36651,
35626c17a1cSBeomho Seo 				struct iio_chan_spec const *chan, int *val2)
357e590d451SBeomho Seo {
358e590d451SBeomho Seo 	switch (chan->type) {
359e590d451SBeomho Seo 	case IIO_LIGHT:
360e590d451SBeomho Seo 		if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT1)
36126c17a1cSBeomho Seo 			*val2 = 80000;
362e590d451SBeomho Seo 		else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT2)
36326c17a1cSBeomho Seo 			*val2 = 160000;
364e590d451SBeomho Seo 		else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT3)
36526c17a1cSBeomho Seo 			*val2 = 320000;
366e590d451SBeomho Seo 		else if (cm36651->cs_int_time[chan->address] == CM36651_CS_IT4)
36726c17a1cSBeomho Seo 			*val2 = 640000;
368e590d451SBeomho Seo 		else
369e590d451SBeomho Seo 			return -EINVAL;
370e590d451SBeomho Seo 		break;
371e590d451SBeomho Seo 	case IIO_PROXIMITY:
372e590d451SBeomho Seo 		if (cm36651->ps_int_time == CM36651_PS_IT1)
37326c17a1cSBeomho Seo 			*val2 = 320;
374e590d451SBeomho Seo 		else if (cm36651->ps_int_time == CM36651_PS_IT2)
37526c17a1cSBeomho Seo 			*val2 = 420;
376e590d451SBeomho Seo 		else if (cm36651->ps_int_time == CM36651_PS_IT3)
37726c17a1cSBeomho Seo 			*val2 = 520;
378e590d451SBeomho Seo 		else if (cm36651->ps_int_time == CM36651_PS_IT4)
37926c17a1cSBeomho Seo 			*val2 = 640;
380e590d451SBeomho Seo 		else
381e590d451SBeomho Seo 			return -EINVAL;
382e590d451SBeomho Seo 		break;
383e590d451SBeomho Seo 	default:
384e590d451SBeomho Seo 		return -EINVAL;
385e590d451SBeomho Seo 	}
386e590d451SBeomho Seo 
38726c17a1cSBeomho Seo 	return IIO_VAL_INT_PLUS_MICRO;
388e590d451SBeomho Seo }
389e590d451SBeomho Seo 
390e590d451SBeomho Seo static int cm36651_write_int_time(struct cm36651_data *cm36651,
391e590d451SBeomho Seo 				struct iio_chan_spec const *chan, int val)
392e590d451SBeomho Seo {
393e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
394e590d451SBeomho Seo 	struct i2c_client *ps_client = cm36651->ps_client;
395e590d451SBeomho Seo 	int int_time, ret;
396e590d451SBeomho Seo 
397e590d451SBeomho Seo 	switch (chan->type) {
398e590d451SBeomho Seo 	case IIO_LIGHT:
399e590d451SBeomho Seo 		if (val == 80000)
400e590d451SBeomho Seo 			int_time = CM36651_CS_IT1;
401e590d451SBeomho Seo 		else if (val == 160000)
402e590d451SBeomho Seo 			int_time = CM36651_CS_IT2;
403e590d451SBeomho Seo 		else if (val == 320000)
404e590d451SBeomho Seo 			int_time = CM36651_CS_IT3;
405e590d451SBeomho Seo 		else if (val == 640000)
406e590d451SBeomho Seo 			int_time = CM36651_CS_IT4;
407e590d451SBeomho Seo 		else
408e590d451SBeomho Seo 			return -EINVAL;
409e590d451SBeomho Seo 
410e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(client, CM36651_CS_CONF3,
411e590d451SBeomho Seo 					   int_time >> 2 * (chan->address));
412e590d451SBeomho Seo 		if (ret < 0) {
413e590d451SBeomho Seo 			dev_err(&client->dev, "CS integration time write failed\n");
414e590d451SBeomho Seo 			return ret;
415e590d451SBeomho Seo 		}
416e590d451SBeomho Seo 		cm36651->cs_int_time[chan->address] = int_time;
417e590d451SBeomho Seo 		break;
418e590d451SBeomho Seo 	case IIO_PROXIMITY:
419e590d451SBeomho Seo 		if (val == 320)
420e590d451SBeomho Seo 			int_time = CM36651_PS_IT1;
421e590d451SBeomho Seo 		else if (val == 420)
422e590d451SBeomho Seo 			int_time = CM36651_PS_IT2;
423e590d451SBeomho Seo 		else if (val == 520)
424e590d451SBeomho Seo 			int_time = CM36651_PS_IT3;
425e590d451SBeomho Seo 		else if (val == 640)
426e590d451SBeomho Seo 			int_time = CM36651_PS_IT4;
427e590d451SBeomho Seo 		else
428e590d451SBeomho Seo 			return -EINVAL;
429e590d451SBeomho Seo 
430e590d451SBeomho Seo 		ret = i2c_smbus_write_byte_data(ps_client,
431e590d451SBeomho Seo 						CM36651_PS_CONF1, int_time);
432e590d451SBeomho Seo 		if (ret < 0) {
433e590d451SBeomho Seo 			dev_err(&client->dev, "PS integration time write failed\n");
434e590d451SBeomho Seo 			return ret;
435e590d451SBeomho Seo 		}
436e590d451SBeomho Seo 		cm36651->ps_int_time = int_time;
437e590d451SBeomho Seo 		break;
438e590d451SBeomho Seo 	default:
439e590d451SBeomho Seo 		return -EINVAL;
440e590d451SBeomho Seo 	}
441e590d451SBeomho Seo 
442e590d451SBeomho Seo 	return ret;
443e590d451SBeomho Seo }
444e590d451SBeomho Seo 
445e590d451SBeomho Seo static int cm36651_read_raw(struct iio_dev *indio_dev,
446e590d451SBeomho Seo 			    struct iio_chan_spec const *chan,
447e590d451SBeomho Seo 			    int *val, int *val2, long mask)
448e590d451SBeomho Seo {
449e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
450e590d451SBeomho Seo 	int ret;
451e590d451SBeomho Seo 
452e590d451SBeomho Seo 	mutex_lock(&cm36651->lock);
453e590d451SBeomho Seo 
454e590d451SBeomho Seo 	switch (mask) {
455e590d451SBeomho Seo 	case IIO_CHAN_INFO_RAW:
456e590d451SBeomho Seo 		ret = cm36651_read_channel(cm36651, chan, val);
457e590d451SBeomho Seo 		break;
458e590d451SBeomho Seo 	case IIO_CHAN_INFO_INT_TIME:
45926c17a1cSBeomho Seo 		*val = 0;
46026c17a1cSBeomho Seo 		ret = cm36651_read_int_time(cm36651, chan, val2);
461e590d451SBeomho Seo 		break;
462e590d451SBeomho Seo 	default:
463e590d451SBeomho Seo 		ret = -EINVAL;
464e590d451SBeomho Seo 	}
465e590d451SBeomho Seo 
466e590d451SBeomho Seo 	mutex_unlock(&cm36651->lock);
467e590d451SBeomho Seo 
468e590d451SBeomho Seo 	return ret;
469e590d451SBeomho Seo }
470e590d451SBeomho Seo 
471e590d451SBeomho Seo static int cm36651_write_raw(struct iio_dev *indio_dev,
472e590d451SBeomho Seo 			     struct iio_chan_spec const *chan,
473e590d451SBeomho Seo 			     int val, int val2, long mask)
474e590d451SBeomho Seo {
475e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
476e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
477e590d451SBeomho Seo 	int ret = -EINVAL;
478e590d451SBeomho Seo 
479e590d451SBeomho Seo 	if (mask == IIO_CHAN_INFO_INT_TIME) {
48026c17a1cSBeomho Seo 		ret = cm36651_write_int_time(cm36651, chan, val2);
481e590d451SBeomho Seo 		if (ret < 0)
482e590d451SBeomho Seo 			dev_err(&client->dev, "Integration time write failed\n");
483e590d451SBeomho Seo 	}
484e590d451SBeomho Seo 
485e590d451SBeomho Seo 	return ret;
486e590d451SBeomho Seo }
487e590d451SBeomho Seo 
488e590d451SBeomho Seo static int cm36651_read_prox_thresh(struct iio_dev *indio_dev,
489bb7f9d90SLars-Peter Clausen 					const struct iio_chan_spec *chan,
490bb7f9d90SLars-Peter Clausen 					enum iio_event_type type,
491bb7f9d90SLars-Peter Clausen 					enum iio_event_direction dir,
492bb7f9d90SLars-Peter Clausen 					enum iio_event_info info,
493bb7f9d90SLars-Peter Clausen 					int *val, int *val2)
494e590d451SBeomho Seo {
495e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
496e590d451SBeomho Seo 
497e590d451SBeomho Seo 	*val = cm36651->ps_ctrl_regs[CM36651_PS_THD];
498e590d451SBeomho Seo 
499e590d451SBeomho Seo 	return 0;
500e590d451SBeomho Seo }
501e590d451SBeomho Seo 
502e590d451SBeomho Seo static int cm36651_write_prox_thresh(struct iio_dev *indio_dev,
503bb7f9d90SLars-Peter Clausen 					const struct iio_chan_spec *chan,
504bb7f9d90SLars-Peter Clausen 					enum iio_event_type type,
505bb7f9d90SLars-Peter Clausen 					enum iio_event_direction dir,
506bb7f9d90SLars-Peter Clausen 					enum iio_event_info info,
507bb7f9d90SLars-Peter Clausen 					int val, int val2)
508e590d451SBeomho Seo {
509e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
510e590d451SBeomho Seo 	struct i2c_client *client = cm36651->client;
511e590d451SBeomho Seo 	int ret;
512e590d451SBeomho Seo 
513e590d451SBeomho Seo 	if (val < 3 || val > 255)
514e590d451SBeomho Seo 		return -EINVAL;
515e590d451SBeomho Seo 
516e590d451SBeomho Seo 	cm36651->ps_ctrl_regs[CM36651_PS_THD] = val;
517e590d451SBeomho Seo 	ret = i2c_smbus_write_byte_data(cm36651->ps_client, CM36651_PS_THD,
518e590d451SBeomho Seo 					cm36651->ps_ctrl_regs[CM36651_PS_THD]);
519e590d451SBeomho Seo 
520e590d451SBeomho Seo 	if (ret < 0) {
521e590d451SBeomho Seo 		dev_err(&client->dev, "PS threshold write failed: %d\n", ret);
522e590d451SBeomho Seo 		return ret;
523e590d451SBeomho Seo 	}
524e590d451SBeomho Seo 
525e590d451SBeomho Seo 	return 0;
526e590d451SBeomho Seo }
527e590d451SBeomho Seo 
528e590d451SBeomho Seo static int cm36651_write_prox_event_config(struct iio_dev *indio_dev,
529bb7f9d90SLars-Peter Clausen 					const struct iio_chan_spec *chan,
530bb7f9d90SLars-Peter Clausen 					enum iio_event_type type,
531bb7f9d90SLars-Peter Clausen 					enum iio_event_direction dir,
532bb7f9d90SLars-Peter Clausen 					int state)
533e590d451SBeomho Seo {
534e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
5355e796568SColin Ian King 	int cmd, ret;
536e590d451SBeomho Seo 
537e590d451SBeomho Seo 	mutex_lock(&cm36651->lock);
538e590d451SBeomho Seo 
539e590d451SBeomho Seo 	cmd = state ? CM36651_CMD_PROX_EV_EN : CM36651_CMD_PROX_EV_DIS;
540e590d451SBeomho Seo 	ret = cm36651_set_operation_mode(cm36651, cmd);
541e590d451SBeomho Seo 
542e590d451SBeomho Seo 	mutex_unlock(&cm36651->lock);
543e590d451SBeomho Seo 
544e590d451SBeomho Seo 	return ret;
545e590d451SBeomho Seo }
546e590d451SBeomho Seo 
547e590d451SBeomho Seo static int cm36651_read_prox_event_config(struct iio_dev *indio_dev,
548bb7f9d90SLars-Peter Clausen 					const struct iio_chan_spec *chan,
549bb7f9d90SLars-Peter Clausen 					enum iio_event_type type,
550bb7f9d90SLars-Peter Clausen 					enum iio_event_direction dir)
551e590d451SBeomho Seo {
552e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
553e590d451SBeomho Seo 	int event_en;
554e590d451SBeomho Seo 
555e590d451SBeomho Seo 	mutex_lock(&cm36651->lock);
556e590d451SBeomho Seo 
557e590d451SBeomho Seo 	event_en = test_bit(CM36651_PROXIMITY_EV_EN, &cm36651->flags);
558e590d451SBeomho Seo 
559e590d451SBeomho Seo 	mutex_unlock(&cm36651->lock);
560e590d451SBeomho Seo 
561e590d451SBeomho Seo 	return event_en;
562e590d451SBeomho Seo }
563e590d451SBeomho Seo 
564e590d451SBeomho Seo #define CM36651_LIGHT_CHANNEL(_color, _idx) {		\
565e590d451SBeomho Seo 	.type = IIO_LIGHT,				\
566e590d451SBeomho Seo 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
567e590d451SBeomho Seo 			BIT(IIO_CHAN_INFO_INT_TIME),	\
568e590d451SBeomho Seo 	.address = _idx,				\
569e590d451SBeomho Seo 	.modified = 1,					\
570e590d451SBeomho Seo 	.channel2 = IIO_MOD_LIGHT_##_color,		\
571e590d451SBeomho Seo }							\
572e590d451SBeomho Seo 
573bb7f9d90SLars-Peter Clausen static const struct iio_event_spec cm36651_event_spec[] = {
574bb7f9d90SLars-Peter Clausen 	{
575bb7f9d90SLars-Peter Clausen 		.type = IIO_EV_TYPE_THRESH,
576bb7f9d90SLars-Peter Clausen 		.dir = IIO_EV_DIR_EITHER,
577bb7f9d90SLars-Peter Clausen 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
578bb7f9d90SLars-Peter Clausen 				BIT(IIO_EV_INFO_ENABLE),
579bb7f9d90SLars-Peter Clausen 	}
580bb7f9d90SLars-Peter Clausen };
581bb7f9d90SLars-Peter Clausen 
582e590d451SBeomho Seo static const struct iio_chan_spec cm36651_channels[] = {
583e590d451SBeomho Seo 	{
584e590d451SBeomho Seo 		.type = IIO_PROXIMITY,
585e590d451SBeomho Seo 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
586e590d451SBeomho Seo 				BIT(IIO_CHAN_INFO_INT_TIME),
587bb7f9d90SLars-Peter Clausen 		.event_spec = cm36651_event_spec,
588bb7f9d90SLars-Peter Clausen 		.num_event_specs = ARRAY_SIZE(cm36651_event_spec),
589e590d451SBeomho Seo 	},
590e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL(RED, CM36651_LIGHT_CHANNEL_IDX_RED),
591e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL(GREEN, CM36651_LIGHT_CHANNEL_IDX_GREEN),
592e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL(BLUE, CM36651_LIGHT_CHANNEL_IDX_BLUE),
593e590d451SBeomho Seo 	CM36651_LIGHT_CHANNEL(CLEAR, CM36651_LIGHT_CHANNEL_IDX_CLEAR),
594e590d451SBeomho Seo };
595e590d451SBeomho Seo 
596e590d451SBeomho Seo static IIO_CONST_ATTR(in_illuminance_integration_time_available,
597e590d451SBeomho Seo 					CM36651_CS_INT_TIME_AVAIL);
598e590d451SBeomho Seo static IIO_CONST_ATTR(in_proximity_integration_time_available,
599e590d451SBeomho Seo 					CM36651_PS_INT_TIME_AVAIL);
600e590d451SBeomho Seo 
601e590d451SBeomho Seo static struct attribute *cm36651_attributes[] = {
602e590d451SBeomho Seo 	&iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
603e590d451SBeomho Seo 	&iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
604e590d451SBeomho Seo 	NULL,
605e590d451SBeomho Seo };
606e590d451SBeomho Seo 
607e590d451SBeomho Seo static const struct attribute_group cm36651_attribute_group = {
608e590d451SBeomho Seo 	.attrs = cm36651_attributes
609e590d451SBeomho Seo };
610e590d451SBeomho Seo 
611e590d451SBeomho Seo static const struct iio_info cm36651_info = {
612e590d451SBeomho Seo 	.read_raw		= &cm36651_read_raw,
613e590d451SBeomho Seo 	.write_raw		= &cm36651_write_raw,
614e590d451SBeomho Seo 	.read_event_value	= &cm36651_read_prox_thresh,
615e590d451SBeomho Seo 	.write_event_value	= &cm36651_write_prox_thresh,
616e590d451SBeomho Seo 	.read_event_config	= &cm36651_read_prox_event_config,
617e590d451SBeomho Seo 	.write_event_config	= &cm36651_write_prox_event_config,
618e590d451SBeomho Seo 	.attrs			= &cm36651_attribute_group,
619e590d451SBeomho Seo };
620e590d451SBeomho Seo 
621e590d451SBeomho Seo static int cm36651_probe(struct i2c_client *client,
622e590d451SBeomho Seo 			     const struct i2c_device_id *id)
623e590d451SBeomho Seo {
624e590d451SBeomho Seo 	struct cm36651_data *cm36651;
625e590d451SBeomho Seo 	struct iio_dev *indio_dev;
626e590d451SBeomho Seo 	int ret;
627e590d451SBeomho Seo 
628e590d451SBeomho Seo 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm36651));
629e590d451SBeomho Seo 	if (!indio_dev)
630e590d451SBeomho Seo 		return -ENOMEM;
631e590d451SBeomho Seo 
632e590d451SBeomho Seo 	cm36651 = iio_priv(indio_dev);
633e590d451SBeomho Seo 
634e590d451SBeomho Seo 	cm36651->vled_reg = devm_regulator_get(&client->dev, "vled");
635*8283b954SCai Huoqing 	if (IS_ERR(cm36651->vled_reg))
636*8283b954SCai Huoqing 		return dev_err_probe(&client->dev, PTR_ERR(cm36651->vled_reg),
637*8283b954SCai Huoqing 				     "get regulator vled failed\n");
638e590d451SBeomho Seo 
639e590d451SBeomho Seo 	ret = regulator_enable(cm36651->vled_reg);
640e590d451SBeomho Seo 	if (ret) {
641e590d451SBeomho Seo 		dev_err(&client->dev, "enable regulator vled failed\n");
642e590d451SBeomho Seo 		return ret;
643e590d451SBeomho Seo 	}
644e590d451SBeomho Seo 
645e590d451SBeomho Seo 	i2c_set_clientdata(client, indio_dev);
646e590d451SBeomho Seo 
647e590d451SBeomho Seo 	cm36651->client = client;
648d96dbb3fSWolfram Sang 	cm36651->ps_client = i2c_new_dummy_device(client->adapter,
649e590d451SBeomho Seo 						     CM36651_I2C_ADDR_PS);
650d96dbb3fSWolfram Sang 	if (IS_ERR(cm36651->ps_client)) {
651d0a588a5SKrzysztof Kozlowski 		dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
652d96dbb3fSWolfram Sang 		ret = PTR_ERR(cm36651->ps_client);
653d0a588a5SKrzysztof Kozlowski 		goto error_disable_reg;
654d0a588a5SKrzysztof Kozlowski 	}
655d0a588a5SKrzysztof Kozlowski 
656d96dbb3fSWolfram Sang 	cm36651->ara_client = i2c_new_dummy_device(client->adapter, CM36651_ARA);
657d96dbb3fSWolfram Sang 	if (IS_ERR(cm36651->ara_client)) {
658d0a588a5SKrzysztof Kozlowski 		dev_err(&client->dev, "%s: new i2c device failed\n", __func__);
659d96dbb3fSWolfram Sang 		ret = PTR_ERR(cm36651->ara_client);
660d0a588a5SKrzysztof Kozlowski 		goto error_i2c_unregister_ps;
661d0a588a5SKrzysztof Kozlowski 	}
662d0a588a5SKrzysztof Kozlowski 
663e590d451SBeomho Seo 	mutex_init(&cm36651->lock);
664e590d451SBeomho Seo 	indio_dev->channels = cm36651_channels;
665e590d451SBeomho Seo 	indio_dev->num_channels = ARRAY_SIZE(cm36651_channels);
666e590d451SBeomho Seo 	indio_dev->info = &cm36651_info;
667e590d451SBeomho Seo 	indio_dev->name = id->name;
668e590d451SBeomho Seo 	indio_dev->modes = INDIO_DIRECT_MODE;
669e590d451SBeomho Seo 
670e590d451SBeomho Seo 	ret = cm36651_setup_reg(cm36651);
671e590d451SBeomho Seo 	if (ret) {
672e590d451SBeomho Seo 		dev_err(&client->dev, "%s: register setup failed\n", __func__);
673d0a588a5SKrzysztof Kozlowski 		goto error_i2c_unregister_ara;
674e590d451SBeomho Seo 	}
675e590d451SBeomho Seo 
676e590d451SBeomho Seo 	ret = request_threaded_irq(client->irq, NULL, cm36651_irq_handler,
677e590d451SBeomho Seo 					IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
678e590d451SBeomho Seo 							"cm36651", indio_dev);
679e590d451SBeomho Seo 	if (ret) {
680e590d451SBeomho Seo 		dev_err(&client->dev, "%s: request irq failed\n", __func__);
681d0a588a5SKrzysztof Kozlowski 		goto error_i2c_unregister_ara;
682e590d451SBeomho Seo 	}
683e590d451SBeomho Seo 
684e590d451SBeomho Seo 	ret = iio_device_register(indio_dev);
685e590d451SBeomho Seo 	if (ret) {
686e590d451SBeomho Seo 		dev_err(&client->dev, "%s: regist device failed\n", __func__);
687e590d451SBeomho Seo 		goto error_free_irq;
688e590d451SBeomho Seo 	}
689e590d451SBeomho Seo 
690e590d451SBeomho Seo 	return 0;
691e590d451SBeomho Seo 
692e590d451SBeomho Seo error_free_irq:
693e590d451SBeomho Seo 	free_irq(client->irq, indio_dev);
694d0a588a5SKrzysztof Kozlowski error_i2c_unregister_ara:
695d0a588a5SKrzysztof Kozlowski 	i2c_unregister_device(cm36651->ara_client);
696d0a588a5SKrzysztof Kozlowski error_i2c_unregister_ps:
697d0a588a5SKrzysztof Kozlowski 	i2c_unregister_device(cm36651->ps_client);
698e590d451SBeomho Seo error_disable_reg:
699e590d451SBeomho Seo 	regulator_disable(cm36651->vled_reg);
700e590d451SBeomho Seo 	return ret;
701e590d451SBeomho Seo }
702e590d451SBeomho Seo 
703e590d451SBeomho Seo static int cm36651_remove(struct i2c_client *client)
704e590d451SBeomho Seo {
705e590d451SBeomho Seo 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
706e590d451SBeomho Seo 	struct cm36651_data *cm36651 = iio_priv(indio_dev);
707e590d451SBeomho Seo 
708e590d451SBeomho Seo 	iio_device_unregister(indio_dev);
709e590d451SBeomho Seo 	regulator_disable(cm36651->vled_reg);
710e590d451SBeomho Seo 	free_irq(client->irq, indio_dev);
711d0a588a5SKrzysztof Kozlowski 	i2c_unregister_device(cm36651->ps_client);
712d0a588a5SKrzysztof Kozlowski 	i2c_unregister_device(cm36651->ara_client);
713e590d451SBeomho Seo 
714e590d451SBeomho Seo 	return 0;
715e590d451SBeomho Seo }
716e590d451SBeomho Seo 
717e590d451SBeomho Seo static const struct i2c_device_id cm36651_id[] = {
718e590d451SBeomho Seo 	{ "cm36651", 0 },
719e590d451SBeomho Seo 	{ }
720e590d451SBeomho Seo };
721e590d451SBeomho Seo 
722e590d451SBeomho Seo MODULE_DEVICE_TABLE(i2c, cm36651_id);
723e590d451SBeomho Seo 
724e590d451SBeomho Seo static const struct of_device_id cm36651_of_match[] = {
725e590d451SBeomho Seo 	{ .compatible = "capella,cm36651" },
726e590d451SBeomho Seo 	{ }
727e590d451SBeomho Seo };
728119c4fceSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, cm36651_of_match);
729e590d451SBeomho Seo 
730e590d451SBeomho Seo static struct i2c_driver cm36651_driver = {
731e590d451SBeomho Seo 	.driver = {
732e590d451SBeomho Seo 		.name	= "cm36651",
733a451521dSSachin Kamat 		.of_match_table = cm36651_of_match,
734e590d451SBeomho Seo 	},
735e590d451SBeomho Seo 	.probe		= cm36651_probe,
736e590d451SBeomho Seo 	.remove		= cm36651_remove,
737e590d451SBeomho Seo 	.id_table	= cm36651_id,
738e590d451SBeomho Seo };
739e590d451SBeomho Seo 
740e590d451SBeomho Seo module_i2c_driver(cm36651_driver);
741e590d451SBeomho Seo 
742e590d451SBeomho Seo MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
743e590d451SBeomho Seo MODULE_DESCRIPTION("CM36651 proximity/ambient light sensor driver");
744e590d451SBeomho Seo MODULE_LICENSE("GPL v2");
745