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