Linux Audio

Check our new training course

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