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