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