Linux Audio

Check our new training course

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