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 
70 static const struct reg_field stk3310_reg_field_state =
71 				REG_FIELD(STK3310_REG_STATE, 0, 2);
72 static const struct reg_field stk3310_reg_field_als_gain =
73 				REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
74 static const struct reg_field stk3310_reg_field_ps_gain =
75 				REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
76 static const struct reg_field stk3310_reg_field_als_it =
77 				REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
78 static const struct reg_field stk3310_reg_field_ps_it =
79 				REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
80 static const struct reg_field stk3310_reg_field_int_ps =
81 				REG_FIELD(STK3310_REG_INT, 0, 2);
82 static const struct reg_field stk3310_reg_field_flag_psint =
83 				REG_FIELD(STK3310_REG_FLAG, 4, 4);
84 static const struct reg_field stk3310_reg_field_flag_nf =
85 				REG_FIELD(STK3310_REG_FLAG, 0, 0);
86 
87 static 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. */
98 static 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 
105 static const int stk3310_scale_table[][2] = {
106 	{6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
107 };
108 
109 /* Integration time in seconds, microseconds */
110 static 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 
117 struct 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 
135 static 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 
stk3310_read_near_level(struct iio_dev * indio_dev,uintptr_t priv,const struct iio_chan_spec * chan,char * buf)152 static 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 
162 static 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 
171 static 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 
191 static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
192 
193 static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
194 
195 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
196 		      STK3310_IT_AVAILABLE);
197 
198 static IIO_CONST_ATTR(in_proximity_integration_time_available,
199 		      STK3310_IT_AVAILABLE);
200 
201 static 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 
209 static const struct attribute_group stk3310_attribute_group = {
210 	.attrs = stk3310_attributes
211 };
212 
stk3310_check_chip_id(const u8 chip_id)213 static 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 
stk3310_get_index(const int table[][2],int table_size,int val,int val2)223 static 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 
stk3310_read_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)236 static 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 
stk3310_write_event(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)271 static 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 
stk3310_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)307 static 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 
stk3310_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)323 static 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 				      int state)
328 {
329 	int ret;
330 	struct stk3310_data *data = iio_priv(indio_dev);
331 	struct i2c_client *client = data->client;
332 
333 	if (state < 0 || state > 7)
334 		return -EINVAL;
335 
336 	/* Set INT_PS value */
337 	mutex_lock(&data->lock);
338 	ret = regmap_field_write(data->reg_int_ps, state);
339 	if (ret < 0)
340 		dev_err(&client->dev, "failed to set interrupt mode\n");
341 	mutex_unlock(&data->lock);
342 
343 	return ret;
344 }
345 
stk3310_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)346 static int stk3310_read_raw(struct iio_dev *indio_dev,
347 			    struct iio_chan_spec const *chan,
348 			    int *val, int *val2, long mask)
349 {
350 	u8 reg;
351 	__be16 buf;
352 	int ret;
353 	unsigned int index;
354 	struct stk3310_data *data = iio_priv(indio_dev);
355 	struct i2c_client *client = data->client;
356 
357 	if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
358 		return -EINVAL;
359 
360 	switch (mask) {
361 	case IIO_CHAN_INFO_RAW:
362 		if (chan->type == IIO_LIGHT)
363 			reg = STK3310_REG_ALS_DATA_MSB;
364 		else
365 			reg = STK3310_REG_PS_DATA_MSB;
366 
367 		mutex_lock(&data->lock);
368 		ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
369 		if (ret < 0) {
370 			dev_err(&client->dev, "register read failed\n");
371 			mutex_unlock(&data->lock);
372 			return ret;
373 		}
374 		*val = be16_to_cpu(buf);
375 		mutex_unlock(&data->lock);
376 		return IIO_VAL_INT;
377 	case IIO_CHAN_INFO_INT_TIME:
378 		if (chan->type == IIO_LIGHT)
379 			ret = regmap_field_read(data->reg_als_it, &index);
380 		else
381 			ret = regmap_field_read(data->reg_ps_it, &index);
382 		if (ret < 0)
383 			return ret;
384 
385 		*val = stk3310_it_table[index][0];
386 		*val2 = stk3310_it_table[index][1];
387 		return IIO_VAL_INT_PLUS_MICRO;
388 	case IIO_CHAN_INFO_SCALE:
389 		if (chan->type == IIO_LIGHT)
390 			ret = regmap_field_read(data->reg_als_gain, &index);
391 		else
392 			ret = regmap_field_read(data->reg_ps_gain, &index);
393 		if (ret < 0)
394 			return ret;
395 
396 		*val = stk3310_scale_table[index][0];
397 		*val2 = stk3310_scale_table[index][1];
398 		return IIO_VAL_INT_PLUS_MICRO;
399 	}
400 
401 	return -EINVAL;
402 }
403 
stk3310_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)404 static int stk3310_write_raw(struct iio_dev *indio_dev,
405 			     struct iio_chan_spec const *chan,
406 			     int val, int val2, long mask)
407 {
408 	int ret;
409 	int index;
410 	struct stk3310_data *data = iio_priv(indio_dev);
411 
412 	if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
413 		return -EINVAL;
414 
415 	switch (mask) {
416 	case IIO_CHAN_INFO_INT_TIME:
417 		index = stk3310_get_index(stk3310_it_table,
418 					  ARRAY_SIZE(stk3310_it_table),
419 					  val, val2);
420 		if (index < 0)
421 			return -EINVAL;
422 		mutex_lock(&data->lock);
423 		if (chan->type == IIO_LIGHT)
424 			ret = regmap_field_write(data->reg_als_it, index);
425 		else
426 			ret = regmap_field_write(data->reg_ps_it, index);
427 		if (ret < 0)
428 			dev_err(&data->client->dev,
429 				"sensor configuration failed\n");
430 		mutex_unlock(&data->lock);
431 		return ret;
432 
433 	case IIO_CHAN_INFO_SCALE:
434 		index = stk3310_get_index(stk3310_scale_table,
435 					  ARRAY_SIZE(stk3310_scale_table),
436 					  val, val2);
437 		if (index < 0)
438 			return -EINVAL;
439 		mutex_lock(&data->lock);
440 		if (chan->type == IIO_LIGHT)
441 			ret = regmap_field_write(data->reg_als_gain, index);
442 		else
443 			ret = regmap_field_write(data->reg_ps_gain, index);
444 		if (ret < 0)
445 			dev_err(&data->client->dev,
446 				"sensor configuration failed\n");
447 		mutex_unlock(&data->lock);
448 		return ret;
449 	}
450 
451 	return -EINVAL;
452 }
453 
454 static const struct iio_info stk3310_info = {
455 	.read_raw		= stk3310_read_raw,
456 	.write_raw		= stk3310_write_raw,
457 	.attrs			= &stk3310_attribute_group,
458 	.read_event_value	= stk3310_read_event,
459 	.write_event_value	= stk3310_write_event,
460 	.read_event_config	= stk3310_read_event_config,
461 	.write_event_config	= stk3310_write_event_config,
462 };
463 
stk3310_set_state(struct stk3310_data * data,u8 state)464 static int stk3310_set_state(struct stk3310_data *data, u8 state)
465 {
466 	int ret;
467 	struct i2c_client *client = data->client;
468 
469 	/* 3-bit state; 0b100 is not supported. */
470 	if (state > 7 || state == 4)
471 		return -EINVAL;
472 
473 	mutex_lock(&data->lock);
474 	ret = regmap_field_write(data->reg_state, state);
475 	if (ret < 0) {
476 		dev_err(&client->dev, "failed to change sensor state\n");
477 	} else if (state != STK3310_STATE_STANDBY) {
478 		/* Don't reset the 'enabled' flags if we're going in standby */
479 		data->ps_enabled  = !!(state & STK3310_STATE_EN_PS);
480 		data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
481 	}
482 	mutex_unlock(&data->lock);
483 
484 	return ret;
485 }
486 
stk3310_init(struct iio_dev * indio_dev)487 static int stk3310_init(struct iio_dev *indio_dev)
488 {
489 	int ret;
490 	int chipid;
491 	u8 state;
492 	struct stk3310_data *data = iio_priv(indio_dev);
493 	struct i2c_client *client = data->client;
494 
495 	ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
496 	if (ret < 0)
497 		return ret;
498 
499 	ret = stk3310_check_chip_id(chipid);
500 	if (ret < 0)
501 		dev_info(&client->dev, "new unknown chip id: 0x%x\n", chipid);
502 
503 	state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
504 	ret = stk3310_set_state(data, state);
505 	if (ret < 0) {
506 		dev_err(&client->dev, "failed to enable sensor");
507 		return ret;
508 	}
509 
510 	/* Enable PS interrupts */
511 	ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
512 	if (ret < 0)
513 		dev_err(&client->dev, "failed to enable interrupts!\n");
514 
515 	return ret;
516 }
517 
stk3310_is_volatile_reg(struct device * dev,unsigned int reg)518 static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
519 {
520 	switch (reg) {
521 	case STK3310_REG_ALS_DATA_MSB:
522 	case STK3310_REG_ALS_DATA_LSB:
523 	case STK3310_REG_PS_DATA_LSB:
524 	case STK3310_REG_PS_DATA_MSB:
525 	case STK3310_REG_FLAG:
526 		return true;
527 	default:
528 		return false;
529 	}
530 }
531 
532 static const struct regmap_config stk3310_regmap_config = {
533 	.name = STK3310_REGMAP_NAME,
534 	.reg_bits = 8,
535 	.val_bits = 8,
536 	.max_register = STK3310_MAX_REG,
537 	.cache_type = REGCACHE_RBTREE,
538 	.volatile_reg = stk3310_is_volatile_reg,
539 };
540 
stk3310_regmap_init(struct stk3310_data * data)541 static int stk3310_regmap_init(struct stk3310_data *data)
542 {
543 	struct regmap *regmap;
544 	struct i2c_client *client;
545 
546 	client = data->client;
547 	regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
548 	if (IS_ERR(regmap)) {
549 		dev_err(&client->dev, "regmap initialization failed.\n");
550 		return PTR_ERR(regmap);
551 	}
552 	data->regmap = regmap;
553 
554 	STK3310_REGFIELD(state);
555 	STK3310_REGFIELD(als_gain);
556 	STK3310_REGFIELD(ps_gain);
557 	STK3310_REGFIELD(als_it);
558 	STK3310_REGFIELD(ps_it);
559 	STK3310_REGFIELD(int_ps);
560 	STK3310_REGFIELD(flag_psint);
561 	STK3310_REGFIELD(flag_nf);
562 
563 	return 0;
564 }
565 
stk3310_irq_handler(int irq,void * private)566 static irqreturn_t stk3310_irq_handler(int irq, void *private)
567 {
568 	struct iio_dev *indio_dev = private;
569 	struct stk3310_data *data = iio_priv(indio_dev);
570 
571 	data->timestamp = iio_get_time_ns(indio_dev);
572 
573 	return IRQ_WAKE_THREAD;
574 }
575 
stk3310_irq_event_handler(int irq,void * private)576 static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
577 {
578 	int ret;
579 	unsigned int dir;
580 	u64 event;
581 
582 	struct iio_dev *indio_dev = private;
583 	struct stk3310_data *data = iio_priv(indio_dev);
584 
585 	/* Read FLAG_NF to figure out what threshold has been met. */
586 	mutex_lock(&data->lock);
587 	ret = regmap_field_read(data->reg_flag_nf, &dir);
588 	if (ret < 0) {
589 		dev_err(&data->client->dev, "register read failed: %d\n", ret);
590 		goto out;
591 	}
592 	event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
593 				     IIO_EV_TYPE_THRESH,
594 				     (dir ? IIO_EV_DIR_FALLING :
595 					    IIO_EV_DIR_RISING));
596 	iio_push_event(indio_dev, event, data->timestamp);
597 
598 	/* Reset the interrupt flag */
599 	ret = regmap_field_write(data->reg_flag_psint, 0);
600 	if (ret < 0)
601 		dev_err(&data->client->dev, "failed to reset interrupts\n");
602 out:
603 	mutex_unlock(&data->lock);
604 
605 	return IRQ_HANDLED;
606 }
607 
stk3310_probe(struct i2c_client * client)608 static int stk3310_probe(struct i2c_client *client)
609 {
610 	int ret;
611 	struct iio_dev *indio_dev;
612 	struct stk3310_data *data;
613 
614 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
615 	if (!indio_dev) {
616 		dev_err(&client->dev, "iio allocation failed!\n");
617 		return -ENOMEM;
618 	}
619 
620 	data = iio_priv(indio_dev);
621 	data->client = client;
622 	i2c_set_clientdata(client, indio_dev);
623 
624 	device_property_read_u32(&client->dev, "proximity-near-level",
625 				 &data->ps_near_level);
626 
627 	mutex_init(&data->lock);
628 
629 	ret = stk3310_regmap_init(data);
630 	if (ret < 0)
631 		return ret;
632 
633 	indio_dev->info = &stk3310_info;
634 	indio_dev->name = STK3310_DRIVER_NAME;
635 	indio_dev->modes = INDIO_DIRECT_MODE;
636 	indio_dev->channels = stk3310_channels;
637 	indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
638 
639 	ret = stk3310_init(indio_dev);
640 	if (ret < 0)
641 		return ret;
642 
643 	if (client->irq > 0) {
644 		ret = devm_request_threaded_irq(&client->dev, client->irq,
645 						stk3310_irq_handler,
646 						stk3310_irq_event_handler,
647 						IRQF_TRIGGER_FALLING |
648 						IRQF_ONESHOT,
649 						STK3310_EVENT, indio_dev);
650 		if (ret < 0) {
651 			dev_err(&client->dev, "request irq %d failed\n",
652 				client->irq);
653 			goto err_standby;
654 		}
655 	}
656 
657 	ret = iio_device_register(indio_dev);
658 	if (ret < 0) {
659 		dev_err(&client->dev, "device_register failed\n");
660 		goto err_standby;
661 	}
662 
663 	return 0;
664 
665 err_standby:
666 	stk3310_set_state(data, STK3310_STATE_STANDBY);
667 	return ret;
668 }
669 
stk3310_remove(struct i2c_client * client)670 static void stk3310_remove(struct i2c_client *client)
671 {
672 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
673 
674 	iio_device_unregister(indio_dev);
675 	stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
676 }
677 
stk3310_suspend(struct device * dev)678 static int stk3310_suspend(struct device *dev)
679 {
680 	struct stk3310_data *data;
681 
682 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
683 
684 	return stk3310_set_state(data, STK3310_STATE_STANDBY);
685 }
686 
stk3310_resume(struct device * dev)687 static int stk3310_resume(struct device *dev)
688 {
689 	u8 state = 0;
690 	struct stk3310_data *data;
691 
692 	data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
693 	if (data->ps_enabled)
694 		state |= STK3310_STATE_EN_PS;
695 	if (data->als_enabled)
696 		state |= STK3310_STATE_EN_ALS;
697 
698 	return stk3310_set_state(data, state);
699 }
700 
701 static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
702 				stk3310_resume);
703 
704 static const struct i2c_device_id stk3310_i2c_id[] = {
705 	{ "STK3013" },
706 	{ "STK3310" },
707 	{ "STK3311" },
708 	{ "STK3335" },
709 	{}
710 };
711 MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
712 
713 static const struct acpi_device_id stk3310_acpi_id[] = {
714 	{"STK3013", 0},
715 	{"STK3310", 0},
716 	{"STK3311", 0},
717 	{}
718 };
719 
720 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
721 
722 static const struct of_device_id stk3310_of_match[] = {
723 	{ .compatible = "sensortek,stk3013", },
724 	{ .compatible = "sensortek,stk3310", },
725 	{ .compatible = "sensortek,stk3311", },
726 	{ .compatible = "sensortek,stk3335", },
727 	{}
728 };
729 MODULE_DEVICE_TABLE(of, stk3310_of_match);
730 
731 static struct i2c_driver stk3310_driver = {
732 	.driver = {
733 		.name = "stk3310",
734 		.of_match_table = stk3310_of_match,
735 		.pm = pm_sleep_ptr(&stk3310_pm_ops),
736 		.acpi_match_table = stk3310_acpi_id,
737 	},
738 	.probe =        stk3310_probe,
739 	.remove =           stk3310_remove,
740 	.id_table =         stk3310_i2c_id,
741 };
742 
743 module_i2c_driver(stk3310_driver);
744 
745 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
746 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
747 MODULE_LICENSE("GPL v2");
748