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