xref: /linux/drivers/iio/light/stk3310.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor
4  *
5  * Copyright (c) 2015, Intel Corporation.
6  *
7  * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48.
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/regmap.h>
16 #include <linux/iio/events.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 
20 #define STK3310_REG_STATE			0x00
21 #define STK3310_REG_PSCTRL			0x01
22 #define STK3310_REG_ALSCTRL			0x02
23 #define STK3310_REG_INT				0x04
24 #define STK3310_REG_THDH_PS			0x06
25 #define STK3310_REG_THDL_PS			0x08
26 #define STK3310_REG_FLAG			0x10
27 #define STK3310_REG_PS_DATA_MSB			0x11
28 #define STK3310_REG_PS_DATA_LSB			0x12
29 #define STK3310_REG_ALS_DATA_MSB		0x13
30 #define STK3310_REG_ALS_DATA_LSB		0x14
31 #define STK3310_REG_ID				0x3E
32 #define STK3310_MAX_REG				0x80
33 
34 #define STK3310_STATE_EN_PS			BIT(0)
35 #define STK3310_STATE_EN_ALS			BIT(1)
36 #define STK3310_STATE_STANDBY			0x00
37 
38 #define STK3310_CHIP_ID_VAL			0x13
39 #define STK3311_CHIP_ID_VAL			0x1D
40 #define STK3311A_CHIP_ID_VAL			0x15
41 #define STK3311S34_CHIP_ID_VAL			0x1E
42 #define STK3311X_CHIP_ID_VAL			0x12
43 #define STK3335_CHIP_ID_VAL			0x51
44 #define STK3310_PSINT_EN			0x01
45 #define STK3310_PS_MAX_VAL			0xFFFF
46 
47 #define STK3310_DRIVER_NAME			"stk3310"
48 #define STK3310_REGMAP_NAME			"stk3310_regmap"
49 #define STK3310_EVENT				"stk3310_event"
50 
51 #define STK3310_SCALE_AVAILABLE			"6.4 1.6 0.4 0.1"
52 
53 #define STK3310_IT_AVAILABLE \
54 	"0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
55 	"0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
56 	"3.031040 6.062080"
57 
58 #define STK3310_REGFIELD(name)						    \
59 	do {								    \
60 		data->reg_##name =					    \
61 			devm_regmap_field_alloc(&client->dev, regmap,	    \
62 				stk3310_reg_field_##name);		    \
63 		if (IS_ERR(data->reg_##name)) {				    \
64 			dev_err(&client->dev, "reg field alloc failed.\n"); \
65 			return PTR_ERR(data->reg_##name);		    \
66 		}							    \
67 	} while (0)
68 
69 static const struct reg_field stk3310_reg_field_state =
70 				REG_FIELD(STK3310_REG_STATE, 0, 2);
71 static const struct reg_field stk3310_reg_field_als_gain =
72 				REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
73 static const struct reg_field stk3310_reg_field_ps_gain =
74 				REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
75 static const struct reg_field stk3310_reg_field_als_it =
76 				REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
77 static const struct reg_field stk3310_reg_field_ps_it =
78 				REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
79 static const struct reg_field stk3310_reg_field_int_ps =
80 				REG_FIELD(STK3310_REG_INT, 0, 2);
81 static const struct reg_field stk3310_reg_field_flag_psint =
82 				REG_FIELD(STK3310_REG_FLAG, 4, 4);
83 static const struct reg_field stk3310_reg_field_flag_nf =
84 				REG_FIELD(STK3310_REG_FLAG, 0, 0);
85 
86 static const u8 stk3310_chip_ids[] = {
87 	STK3310_CHIP_ID_VAL,
88 	STK3311A_CHIP_ID_VAL,
89 	STK3311S34_CHIP_ID_VAL,
90 	STK3311X_CHIP_ID_VAL,
91 	STK3311_CHIP_ID_VAL,
92 	STK3335_CHIP_ID_VAL,
93 };
94 
95 /* Estimate maximum proximity values with regard to measurement scale. */
96 static const int stk3310_ps_max[4] = {
97 	STK3310_PS_MAX_VAL / 640,
98 	STK3310_PS_MAX_VAL / 160,
99 	STK3310_PS_MAX_VAL /  40,
100 	STK3310_PS_MAX_VAL /  10
101 };
102 
103 static const int stk3310_scale_table[][2] = {
104 	{6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
105 };
106 
107 /* Integration time in seconds, microseconds */
108 static const int stk3310_it_table[][2] = {
109 	{0, 185},	{0, 370},	{0, 741},	{0, 1480},
110 	{0, 2960},	{0, 5920},	{0, 11840},	{0, 23680},
111 	{0, 47360},	{0, 94720},	{0, 189440},	{0, 378880},
112 	{0, 757760},	{1, 515520},	{3, 31040},	{6, 62080},
113 };
114 
115 struct stk3310_data {
116 	struct i2c_client *client;
117 	struct mutex lock;
118 	bool als_enabled;
119 	bool ps_enabled;
120 	uint32_t ps_near_level;
121 	u64 timestamp;
122 	struct regmap *regmap;
123 	struct regmap_field *reg_state;
124 	struct regmap_field *reg_als_gain;
125 	struct regmap_field *reg_ps_gain;
126 	struct regmap_field *reg_als_it;
127 	struct regmap_field *reg_ps_it;
128 	struct regmap_field *reg_int_ps;
129 	struct regmap_field *reg_flag_psint;
130 	struct regmap_field *reg_flag_nf;
131 };
132 
133 static const struct iio_event_spec stk3310_events[] = {
134 	/* Proximity event */
135 	{
136 		.type = IIO_EV_TYPE_THRESH,
137 		.dir = IIO_EV_DIR_RISING,
138 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
139 				 BIT(IIO_EV_INFO_ENABLE),
140 	},
141 	/* Out-of-proximity event */
142 	{
143 		.type = IIO_EV_TYPE_THRESH,
144 		.dir = IIO_EV_DIR_FALLING,
145 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
146 				 BIT(IIO_EV_INFO_ENABLE),
147 	},
148 };
149 
150 static ssize_t stk3310_read_near_level(struct iio_dev *indio_dev,
151 				       uintptr_t priv,
152 				       const struct iio_chan_spec *chan,
153 				       char *buf)
154 {
155 	struct stk3310_data *data = iio_priv(indio_dev);
156 
157 	return sprintf(buf, "%u\n", data->ps_near_level);
158 }
159 
160 static const struct iio_chan_spec_ext_info stk3310_ext_info[] = {
161 	{
162 		.name = "nearlevel",
163 		.shared = IIO_SEPARATE,
164 		.read = stk3310_read_near_level,
165 	},
166 	{ /* sentinel */ }
167 };
168 
169 static const struct iio_chan_spec stk3310_channels[] = {
170 	{
171 		.type = IIO_LIGHT,
172 		.info_mask_separate =
173 			BIT(IIO_CHAN_INFO_RAW) |
174 			BIT(IIO_CHAN_INFO_SCALE) |
175 			BIT(IIO_CHAN_INFO_INT_TIME),
176 	},
177 	{
178 		.type = IIO_PROXIMITY,
179 		.info_mask_separate =
180 			BIT(IIO_CHAN_INFO_RAW) |
181 			BIT(IIO_CHAN_INFO_SCALE) |
182 			BIT(IIO_CHAN_INFO_INT_TIME),
183 		.event_spec = stk3310_events,
184 		.num_event_specs = ARRAY_SIZE(stk3310_events),
185 		.ext_info = stk3310_ext_info,
186 	}
187 };
188 
189 static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
190 
191 static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
192 
193 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
194 		      STK3310_IT_AVAILABLE);
195 
196 static IIO_CONST_ATTR(in_proximity_integration_time_available,
197 		      STK3310_IT_AVAILABLE);
198 
199 static struct attribute *stk3310_attributes[] = {
200 	&iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
201 	&iio_const_attr_in_proximity_scale_available.dev_attr.attr,
202 	&iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
203 	&iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
204 	NULL,
205 };
206 
207 static const struct attribute_group stk3310_attribute_group = {
208 	.attrs = stk3310_attributes
209 };
210 
211 static int stk3310_check_chip_id(const u8 chip_id)
212 {
213 	for (int i = 0; i < ARRAY_SIZE(stk3310_chip_ids); i++) {
214 		if (chip_id == stk3310_chip_ids[i])
215 			return 0;
216 	}
217 
218 	return -ENODEV;
219 }
220 
221 static int stk3310_get_index(const int table[][2], int table_size,
222 			     int val, int val2)
223 {
224 	int i;
225 
226 	for (i = 0; i < table_size; i++) {
227 		if (val == table[i][0] && val2 == table[i][1])
228 			return i;
229 	}
230 
231 	return -EINVAL;
232 }
233 
234 static int stk3310_read_event(struct iio_dev *indio_dev,
235 			      const struct iio_chan_spec *chan,
236 			      enum iio_event_type type,
237 			      enum iio_event_direction dir,
238 			      enum iio_event_info info,
239 			      int *val, int *val2)
240 {
241 	u8 reg;
242 	__be16 buf;
243 	int ret;
244 	struct stk3310_data *data = iio_priv(indio_dev);
245 
246 	if (info != IIO_EV_INFO_VALUE)
247 		return -EINVAL;
248 
249 	/* Only proximity interrupts are implemented at the moment. */
250 	if (dir == IIO_EV_DIR_RISING)
251 		reg = STK3310_REG_THDH_PS;
252 	else if (dir == IIO_EV_DIR_FALLING)
253 		reg = STK3310_REG_THDL_PS;
254 	else
255 		return -EINVAL;
256 
257 	mutex_lock(&data->lock);
258 	ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
259 	mutex_unlock(&data->lock);
260 	if (ret < 0) {
261 		dev_err(&data->client->dev, "register read failed\n");
262 		return ret;
263 	}
264 	*val = be16_to_cpu(buf);
265 
266 	return IIO_VAL_INT;
267 }
268 
269 static int stk3310_write_event(struct iio_dev *indio_dev,
270 			       const struct iio_chan_spec *chan,
271 			       enum iio_event_type type,
272 			       enum iio_event_direction dir,
273 			       enum iio_event_info info,
274 			       int val, int val2)
275 {
276 	u8 reg;
277 	__be16 buf;
278 	int ret;
279 	unsigned int index;
280 	struct stk3310_data *data = iio_priv(indio_dev);
281 	struct i2c_client *client = data->client;
282 
283 	ret = regmap_field_read(data->reg_ps_gain, &index);
284 	if (ret < 0)
285 		return ret;
286 
287 	if (val < 0 || val > stk3310_ps_max[index])
288 		return -EINVAL;
289 
290 	if (dir == IIO_EV_DIR_RISING)
291 		reg = STK3310_REG_THDH_PS;
292 	else if (dir == IIO_EV_DIR_FALLING)
293 		reg = STK3310_REG_THDL_PS;
294 	else
295 		return -EINVAL;
296 
297 	buf = cpu_to_be16(val);
298 	ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
299 	if (ret < 0)
300 		dev_err(&client->dev, "failed to set PS threshold!\n");
301 
302 	return ret;
303 }
304 
305 static int stk3310_read_event_config(struct iio_dev *indio_dev,
306 				     const struct iio_chan_spec *chan,
307 				     enum iio_event_type type,
308 				     enum iio_event_direction dir)
309 {
310 	unsigned int event_val;
311 	int ret;
312 	struct stk3310_data *data = iio_priv(indio_dev);
313 
314 	ret = regmap_field_read(data->reg_int_ps, &event_val);
315 	if (ret < 0)
316 		return ret;
317 
318 	return event_val;
319 }
320 
321 static int stk3310_write_event_config(struct iio_dev *indio_dev,
322 				      const struct iio_chan_spec *chan,
323 				      enum iio_event_type type,
324 				      enum iio_event_direction dir,
325 				      int state)
326 {
327 	int ret;
328 	struct stk3310_data *data = iio_priv(indio_dev);
329 	struct i2c_client *client = data->client;
330 
331 	if (state < 0 || state > 7)
332 		return -EINVAL;
333 
334 	/* Set INT_PS value */
335 	mutex_lock(&data->lock);
336 	ret = regmap_field_write(data->reg_int_ps, state);
337 	if (ret < 0)
338 		dev_err(&client->dev, "failed to set interrupt mode\n");
339 	mutex_unlock(&data->lock);
340 
341 	return ret;
342 }
343 
344 static int stk3310_read_raw(struct iio_dev *indio_dev,
345 			    struct iio_chan_spec const *chan,
346 			    int *val, int *val2, long mask)
347 {
348 	u8 reg;
349 	__be16 buf;
350 	int ret;
351 	unsigned int index;
352 	struct stk3310_data *data = iio_priv(indio_dev);
353 	struct i2c_client *client = data->client;
354 
355 	if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
356 		return -EINVAL;
357 
358 	switch (mask) {
359 	case IIO_CHAN_INFO_RAW:
360 		if (chan->type == IIO_LIGHT)
361 			reg = STK3310_REG_ALS_DATA_MSB;
362 		else
363 			reg = STK3310_REG_PS_DATA_MSB;
364 
365 		mutex_lock(&data->lock);
366 		ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
367 		if (ret < 0) {
368 			dev_err(&client->dev, "register read failed\n");
369 			mutex_unlock(&data->lock);
370 			return ret;
371 		}
372 		*val = be16_to_cpu(buf);
373 		mutex_unlock(&data->lock);
374 		return IIO_VAL_INT;
375 	case IIO_CHAN_INFO_INT_TIME:
376 		if (chan->type == IIO_LIGHT)
377 			ret = regmap_field_read(data->reg_als_it, &index);
378 		else
379 			ret = regmap_field_read(data->reg_ps_it, &index);
380 		if (ret < 0)
381 			return ret;
382 
383 		*val = stk3310_it_table[index][0];
384 		*val2 = stk3310_it_table[index][1];
385 		return IIO_VAL_INT_PLUS_MICRO;
386 	case IIO_CHAN_INFO_SCALE:
387 		if (chan->type == IIO_LIGHT)
388 			ret = regmap_field_read(data->reg_als_gain, &index);
389 		else
390 			ret = regmap_field_read(data->reg_ps_gain, &index);
391 		if (ret < 0)
392 			return ret;
393 
394 		*val = stk3310_scale_table[index][0];
395 		*val2 = stk3310_scale_table[index][1];
396 		return IIO_VAL_INT_PLUS_MICRO;
397 	}
398 
399 	return -EINVAL;
400 }
401 
402 static int stk3310_write_raw(struct iio_dev *indio_dev,
403 			     struct iio_chan_spec const *chan,
404 			     int val, int val2, long mask)
405 {
406 	int ret;
407 	int index;
408 	struct stk3310_data *data = iio_priv(indio_dev);
409 
410 	if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
411 		return -EINVAL;
412 
413 	switch (mask) {
414 	case IIO_CHAN_INFO_INT_TIME:
415 		index = stk3310_get_index(stk3310_it_table,
416 					  ARRAY_SIZE(stk3310_it_table),
417 					  val, val2);
418 		if (index < 0)
419 			return -EINVAL;
420 		mutex_lock(&data->lock);
421 		if (chan->type == IIO_LIGHT)
422 			ret = regmap_field_write(data->reg_als_it, index);
423 		else
424 			ret = regmap_field_write(data->reg_ps_it, index);
425 		if (ret < 0)
426 			dev_err(&data->client->dev,
427 				"sensor configuration failed\n");
428 		mutex_unlock(&data->lock);
429 		return ret;
430 
431 	case IIO_CHAN_INFO_SCALE:
432 		index = stk3310_get_index(stk3310_scale_table,
433 					  ARRAY_SIZE(stk3310_scale_table),
434 					  val, val2);
435 		if (index < 0)
436 			return -EINVAL;
437 		mutex_lock(&data->lock);
438 		if (chan->type == IIO_LIGHT)
439 			ret = regmap_field_write(data->reg_als_gain, index);
440 		else
441 			ret = regmap_field_write(data->reg_ps_gain, index);
442 		if (ret < 0)
443 			dev_err(&data->client->dev,
444 				"sensor configuration failed\n");
445 		mutex_unlock(&data->lock);
446 		return ret;
447 	}
448 
449 	return -EINVAL;
450 }
451 
452 static const struct iio_info stk3310_info = {
453 	.read_raw		= stk3310_read_raw,
454 	.write_raw		= stk3310_write_raw,
455 	.attrs			= &stk3310_attribute_group,
456 	.read_event_value	= stk3310_read_event,
457 	.write_event_value	= stk3310_write_event,
458 	.read_event_config	= stk3310_read_event_config,
459 	.write_event_config	= stk3310_write_event_config,
460 };
461 
462 static int stk3310_set_state(struct stk3310_data *data, u8 state)
463 {
464 	int ret;
465 	struct i2c_client *client = data->client;
466 
467 	/* 3-bit state; 0b100 is not supported. */
468 	if (state > 7 || state == 4)
469 		return -EINVAL;
470 
471 	mutex_lock(&data->lock);
472 	ret = regmap_field_write(data->reg_state, state);
473 	if (ret < 0) {
474 		dev_err(&client->dev, "failed to change sensor state\n");
475 	} else if (state != STK3310_STATE_STANDBY) {
476 		/* Don't reset the 'enabled' flags if we're going in standby */
477 		data->ps_enabled  = !!(state & STK3310_STATE_EN_PS);
478 		data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
479 	}
480 	mutex_unlock(&data->lock);
481 
482 	return ret;
483 }
484 
485 static int stk3310_init(struct iio_dev *indio_dev)
486 {
487 	int ret;
488 	int chipid;
489 	u8 state;
490 	struct stk3310_data *data = iio_priv(indio_dev);
491 	struct i2c_client *client = data->client;
492 
493 	ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
494 	if (ret < 0)
495 		return ret;
496 
497 	ret = stk3310_check_chip_id(chipid);
498 	if (ret < 0)
499 		dev_warn(&client->dev, "unknown chip id: 0x%x\n", chipid);
500 
501 	state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
502 	ret = stk3310_set_state(data, state);
503 	if (ret < 0) {
504 		dev_err(&client->dev, "failed to enable sensor");
505 		return ret;
506 	}
507 
508 	/* Enable PS interrupts */
509 	ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
510 	if (ret < 0)
511 		dev_err(&client->dev, "failed to enable interrupts!\n");
512 
513 	return ret;
514 }
515 
516 static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
517 {
518 	switch (reg) {
519 	case STK3310_REG_ALS_DATA_MSB:
520 	case STK3310_REG_ALS_DATA_LSB:
521 	case STK3310_REG_PS_DATA_LSB:
522 	case STK3310_REG_PS_DATA_MSB:
523 	case STK3310_REG_FLAG:
524 		return true;
525 	default:
526 		return false;
527 	}
528 }
529 
530 static const struct regmap_config stk3310_regmap_config = {
531 	.name = STK3310_REGMAP_NAME,
532 	.reg_bits = 8,
533 	.val_bits = 8,
534 	.max_register = STK3310_MAX_REG,
535 	.cache_type = REGCACHE_RBTREE,
536 	.volatile_reg = stk3310_is_volatile_reg,
537 };
538 
539 static int stk3310_regmap_init(struct stk3310_data *data)
540 {
541 	struct regmap *regmap;
542 	struct i2c_client *client;
543 
544 	client = data->client;
545 	regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
546 	if (IS_ERR(regmap)) {
547 		dev_err(&client->dev, "regmap initialization failed.\n");
548 		return PTR_ERR(regmap);
549 	}
550 	data->regmap = regmap;
551 
552 	STK3310_REGFIELD(state);
553 	STK3310_REGFIELD(als_gain);
554 	STK3310_REGFIELD(ps_gain);
555 	STK3310_REGFIELD(als_it);
556 	STK3310_REGFIELD(ps_it);
557 	STK3310_REGFIELD(int_ps);
558 	STK3310_REGFIELD(flag_psint);
559 	STK3310_REGFIELD(flag_nf);
560 
561 	return 0;
562 }
563 
564 static irqreturn_t stk3310_irq_handler(int irq, void *private)
565 {
566 	struct iio_dev *indio_dev = private;
567 	struct stk3310_data *data = iio_priv(indio_dev);
568 
569 	data->timestamp = iio_get_time_ns(indio_dev);
570 
571 	return IRQ_WAKE_THREAD;
572 }
573 
574 static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
575 {
576 	int ret;
577 	unsigned int dir;
578 	u64 event;
579 
580 	struct iio_dev *indio_dev = private;
581 	struct stk3310_data *data = iio_priv(indio_dev);
582 
583 	/* Read FLAG_NF to figure out what threshold has been met. */
584 	mutex_lock(&data->lock);
585 	ret = regmap_field_read(data->reg_flag_nf, &dir);
586 	if (ret < 0) {
587 		dev_err(&data->client->dev, "register read failed: %d\n", ret);
588 		goto out;
589 	}
590 	event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
591 				     IIO_EV_TYPE_THRESH,
592 				     (dir ? IIO_EV_DIR_FALLING :
593 					    IIO_EV_DIR_RISING));
594 	iio_push_event(indio_dev, event, data->timestamp);
595 
596 	/* Reset the interrupt flag */
597 	ret = regmap_field_write(data->reg_flag_psint, 0);
598 	if (ret < 0)
599 		dev_err(&data->client->dev, "failed to reset interrupts\n");
600 out:
601 	mutex_unlock(&data->lock);
602 
603 	return IRQ_HANDLED;
604 }
605 
606 static int stk3310_probe(struct i2c_client *client)
607 {
608 	int ret;
609 	struct iio_dev *indio_dev;
610 	struct stk3310_data *data;
611 
612 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
613 	if (!indio_dev) {
614 		dev_err(&client->dev, "iio allocation failed!\n");
615 		return -ENOMEM;
616 	}
617 
618 	data = iio_priv(indio_dev);
619 	data->client = client;
620 	i2c_set_clientdata(client, indio_dev);
621 
622 	device_property_read_u32(&client->dev, "proximity-near-level",
623 				 &data->ps_near_level);
624 
625 	mutex_init(&data->lock);
626 
627 	ret = stk3310_regmap_init(data);
628 	if (ret < 0)
629 		return ret;
630 
631 	indio_dev->info = &stk3310_info;
632 	indio_dev->name = STK3310_DRIVER_NAME;
633 	indio_dev->modes = INDIO_DIRECT_MODE;
634 	indio_dev->channels = stk3310_channels;
635 	indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
636 
637 	ret = stk3310_init(indio_dev);
638 	if (ret < 0)
639 		return ret;
640 
641 	if (client->irq > 0) {
642 		ret = devm_request_threaded_irq(&client->dev, client->irq,
643 						stk3310_irq_handler,
644 						stk3310_irq_event_handler,
645 						IRQF_TRIGGER_FALLING |
646 						IRQF_ONESHOT,
647 						STK3310_EVENT, indio_dev);
648 		if (ret < 0) {
649 			dev_err(&client->dev, "request irq %d failed\n",
650 				client->irq);
651 			goto err_standby;
652 		}
653 	}
654 
655 	ret = iio_device_register(indio_dev);
656 	if (ret < 0) {
657 		dev_err(&client->dev, "device_register failed\n");
658 		goto err_standby;
659 	}
660 
661 	return 0;
662 
663 err_standby:
664 	stk3310_set_state(data, STK3310_STATE_STANDBY);
665 	return ret;
666 }
667 
668 static void stk3310_remove(struct i2c_client *client)
669 {
670 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
671 
672 	iio_device_unregister(indio_dev);
673 	stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
674 }
675 
676 static int stk3310_suspend(struct device *dev)
677 {
678 	struct stk3310_data *data;
679 
680 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
681 
682 	return stk3310_set_state(data, STK3310_STATE_STANDBY);
683 }
684 
685 static int stk3310_resume(struct device *dev)
686 {
687 	u8 state = 0;
688 	struct stk3310_data *data;
689 
690 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
691 	if (data->ps_enabled)
692 		state |= STK3310_STATE_EN_PS;
693 	if (data->als_enabled)
694 		state |= STK3310_STATE_EN_ALS;
695 
696 	return stk3310_set_state(data, state);
697 }
698 
699 static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
700 				stk3310_resume);
701 
702 static const struct i2c_device_id stk3310_i2c_id[] = {
703 	{ "STK3310" },
704 	{ "STK3311" },
705 	{ "STK3335" },
706 	{}
707 };
708 MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
709 
710 static const struct acpi_device_id stk3310_acpi_id[] = {
711 	{"STK3310", 0},
712 	{"STK3311", 0},
713 	{}
714 };
715 
716 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
717 
718 static const struct of_device_id stk3310_of_match[] = {
719 	{ .compatible = "sensortek,stk3310", },
720 	{ .compatible = "sensortek,stk3311", },
721 	{ .compatible = "sensortek,stk3335", },
722 	{}
723 };
724 MODULE_DEVICE_TABLE(of, stk3310_of_match);
725 
726 static struct i2c_driver stk3310_driver = {
727 	.driver = {
728 		.name = "stk3310",
729 		.of_match_table = stk3310_of_match,
730 		.pm = pm_sleep_ptr(&stk3310_pm_ops),
731 		.acpi_match_table = stk3310_acpi_id,
732 	},
733 	.probe =        stk3310_probe,
734 	.remove =           stk3310_remove,
735 	.id_table =         stk3310_i2c_id,
736 };
737 
738 module_i2c_driver(stk3310_driver);
739 
740 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
741 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
742 MODULE_LICENSE("GPL v2");
743