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
68static const struct reg_field stk3310_reg_field_state =
69 REG_FIELD(STK3310_REG_STATE, 0, 2);
70static const struct reg_field stk3310_reg_field_als_gain =
71 REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
72static const struct reg_field stk3310_reg_field_ps_gain =
73 REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
74static const struct reg_field stk3310_reg_field_als_it =
75 REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
76static const struct reg_field stk3310_reg_field_ps_it =
77 REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
78static const struct reg_field stk3310_reg_field_int_ps =
79 REG_FIELD(STK3310_REG_INT, 0, 2);
80static const struct reg_field stk3310_reg_field_flag_psint =
81 REG_FIELD(STK3310_REG_FLAG, 4, 4);
82static const struct reg_field stk3310_reg_field_flag_nf =
83 REG_FIELD(STK3310_REG_FLAG, 0, 0);
84
85static 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. */
96static 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
103static const int stk3310_scale_table[][2] = {
104 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
105};
106
107/* Integration time in seconds, microseconds */
108static 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
115struct 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
133static 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
150static 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, fmt: "%u\n", data->ps_near_level);
158}
159
160static 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
169static 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
189static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
190
191static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
192
193static IIO_CONST_ATTR(in_illuminance_integration_time_available,
194 STK3310_IT_AVAILABLE);
195
196static IIO_CONST_ATTR(in_proximity_integration_time_available,
197 STK3310_IT_AVAILABLE);
198
199static 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
207static const struct attribute_group stk3310_attribute_group = {
208 .attrs = stk3310_attributes
209};
210
211static 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
221static 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
234static 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(map: data->regmap, reg, val: &buf, val_count: 2);
259 mutex_unlock(lock: &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
269static 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(field: data->reg_ps_gain, val: &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(map: data->regmap, reg, val: &buf, val_count: 2);
299 if (ret < 0)
300 dev_err(&client->dev, "failed to set PS threshold!\n");
301
302 return ret;
303}
304
305static 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(field: data->reg_int_ps, val: &event_val);
315 if (ret < 0)
316 return ret;
317
318 return event_val;
319}
320
321static 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(field: data->reg_int_ps, val: state);
334 if (ret < 0)
335 dev_err(&client->dev, "failed to set interrupt mode\n");
336 mutex_unlock(lock: &data->lock);
337
338 return ret;
339}
340
341static 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(map: data->regmap, reg, val: &buf, val_count: 2);
364 if (ret < 0) {
365 dev_err(&client->dev, "register read failed\n");
366 mutex_unlock(lock: &data->lock);
367 return ret;
368 }
369 *val = be16_to_cpu(buf);
370 mutex_unlock(lock: &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(field: data->reg_als_it, val: &index);
375 else
376 ret = regmap_field_read(field: data->reg_ps_it, val: &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(field: data->reg_als_gain, val: &index);
386 else
387 ret = regmap_field_read(field: data->reg_ps_gain, val: &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
399static 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(table: 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(field: data->reg_als_it, val: index);
420 else
421 ret = regmap_field_write(field: data->reg_ps_it, val: index);
422 if (ret < 0)
423 dev_err(&data->client->dev,
424 "sensor configuration failed\n");
425 mutex_unlock(lock: &data->lock);
426 return ret;
427
428 case IIO_CHAN_INFO_SCALE:
429 index = stk3310_get_index(table: 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(field: data->reg_als_gain, val: index);
437 else
438 ret = regmap_field_write(field: data->reg_ps_gain, val: index);
439 if (ret < 0)
440 dev_err(&data->client->dev,
441 "sensor configuration failed\n");
442 mutex_unlock(lock: &data->lock);
443 return ret;
444 }
445
446 return -EINVAL;
447}
448
449static 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
459static 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(field: data->reg_state, val: 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(lock: &data->lock);
478
479 return ret;
480}
481
482static 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(map: data->regmap, STK3310_REG_ID, val: &chipid);
491 if (ret < 0)
492 return ret;
493
494 ret = stk3310_check_chip_id(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(field: 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
513static 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
527static 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
536static 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(ptr: regmap)) {
544 dev_err(&client->dev, "regmap initialization failed.\n");
545 return PTR_ERR(ptr: 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
561static 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
571static 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(field: data->reg_flag_nf, val: &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, ev_code: event, timestamp: data->timestamp);
592
593 /* Reset the interrupt flag */
594 ret = regmap_field_write(field: data->reg_flag_psint, val: 0);
595 if (ret < 0)
596 dev_err(&data->client->dev, "failed to reset interrupts\n");
597out:
598 mutex_unlock(lock: &data->lock);
599
600 return IRQ_HANDLED;
601}
602
603static 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(parent: &client->dev, sizeof_priv: sizeof(*data));
610 if (!indio_dev)
611 return -ENOMEM;
612
613 data = iio_priv(indio_dev);
614 data->client = client;
615 i2c_set_clientdata(client, data: indio_dev);
616
617 device_property_read_u32(dev: &client->dev, propname: "proximity-near-level",
618 val: &data->ps_near_level);
619
620 mutex_init(&data->lock);
621
622 ret = stk3310_regmap_init(data);
623 if (ret < 0)
624 return ret;
625
626 indio_dev->info = &stk3310_info;
627 indio_dev->name = STK3310_DRIVER_NAME;
628 indio_dev->modes = INDIO_DIRECT_MODE;
629 indio_dev->channels = stk3310_channels;
630 indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
631
632 ret = stk3310_init(indio_dev);
633 if (ret < 0)
634 return ret;
635
636 if (client->irq > 0) {
637 ret = devm_request_threaded_irq(dev: &client->dev, irq: client->irq,
638 handler: stk3310_irq_handler,
639 thread_fn: stk3310_irq_event_handler,
640 IRQF_TRIGGER_FALLING |
641 IRQF_ONESHOT,
642 devname: "stk3310_event", dev_id: indio_dev);
643 if (ret < 0) {
644 dev_err(&client->dev, "request irq %d failed\n",
645 client->irq);
646 goto err_standby;
647 }
648 }
649
650 ret = iio_device_register(indio_dev);
651 if (ret < 0) {
652 dev_err(&client->dev, "device_register failed\n");
653 goto err_standby;
654 }
655
656 return 0;
657
658err_standby:
659 stk3310_set_state(data, STK3310_STATE_STANDBY);
660 return ret;
661}
662
663static void stk3310_remove(struct i2c_client *client)
664{
665 struct iio_dev *indio_dev = i2c_get_clientdata(client);
666
667 iio_device_unregister(indio_dev);
668 stk3310_set_state(data: iio_priv(indio_dev), STK3310_STATE_STANDBY);
669}
670
671static int stk3310_suspend(struct device *dev)
672{
673 struct stk3310_data *data;
674
675 data = iio_priv(indio_dev: i2c_get_clientdata(to_i2c_client(dev)));
676
677 return stk3310_set_state(data, STK3310_STATE_STANDBY);
678}
679
680static int stk3310_resume(struct device *dev)
681{
682 u8 state = 0;
683 struct stk3310_data *data;
684
685 data = iio_priv(indio_dev: i2c_get_clientdata(to_i2c_client(dev)));
686 if (data->ps_enabled)
687 state |= STK3310_STATE_EN_PS;
688 if (data->als_enabled)
689 state |= STK3310_STATE_EN_ALS;
690
691 return stk3310_set_state(data, state);
692}
693
694static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
695 stk3310_resume);
696
697static const struct i2c_device_id stk3310_i2c_id[] = {
698 { "STK3013" },
699 { "STK3310" },
700 { "STK3311" },
701 { "STK3335" },
702 { }
703};
704MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
705
706static const struct acpi_device_id stk3310_acpi_id[] = {
707 {"STK3013", 0},
708 {"STK3310", 0},
709 {"STK3311", 0},
710 { }
711};
712
713MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
714
715static const struct of_device_id stk3310_of_match[] = {
716 { .compatible = "sensortek,stk3013", },
717 { .compatible = "sensortek,stk3310", },
718 { .compatible = "sensortek,stk3311", },
719 { .compatible = "sensortek,stk3335", },
720 { }
721};
722MODULE_DEVICE_TABLE(of, stk3310_of_match);
723
724static struct i2c_driver stk3310_driver = {
725 .driver = {
726 .name = "stk3310",
727 .of_match_table = stk3310_of_match,
728 .pm = pm_sleep_ptr(&stk3310_pm_ops),
729 .acpi_match_table = stk3310_acpi_id,
730 },
731 .probe = stk3310_probe,
732 .remove = stk3310_remove,
733 .id_table = stk3310_i2c_id,
734};
735
736module_i2c_driver(stk3310_driver);
737
738MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
739MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
740MODULE_LICENSE("GPL v2");
741

source code of linux/drivers/iio/light/stk3310.c