1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor
5 * Copyright (c) 2015, Intel Corporation.
7 * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48.
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>
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
34 #define STK3310_STATE_EN_PS BIT(0)
35 #define STK3310_STATE_EN_ALS BIT(1)
36 #define STK3310_STATE_STANDBY 0x00
38 #define STK3310_CHIP_ID_VAL 0x13
39 #define STK3311_CHIP_ID_VAL 0x1D
40 #define STK3335_CHIP_ID_VAL 0x51
41 #define STK3310_PSINT_EN 0x01
42 #define STK3310_PS_MAX_VAL 0xFFFF
44 #define STK3310_DRIVER_NAME "stk3310"
45 #define STK3310_REGMAP_NAME "stk3310_regmap"
46 #define STK3310_EVENT "stk3310_event"
48 #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
50 #define STK3310_IT_AVAILABLE \
51 "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
52 "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
55 #define STK3310_REGFIELD(name) \
58 devm_regmap_field_alloc(&client->dev, regmap, \
59 stk3310_reg_field_##name); \
60 if (IS_ERR(data->reg_##name)) { \
61 dev_err(&client->dev, "reg field alloc failed.\n"); \
62 return PTR_ERR(data->reg_##name); \
66 static const struct reg_field stk3310_reg_field_state
=
67 REG_FIELD(STK3310_REG_STATE
, 0, 2);
68 static const struct reg_field stk3310_reg_field_als_gain
=
69 REG_FIELD(STK3310_REG_ALSCTRL
, 4, 5);
70 static const struct reg_field stk3310_reg_field_ps_gain
=
71 REG_FIELD(STK3310_REG_PSCTRL
, 4, 5);
72 static const struct reg_field stk3310_reg_field_als_it
=
73 REG_FIELD(STK3310_REG_ALSCTRL
, 0, 3);
74 static const struct reg_field stk3310_reg_field_ps_it
=
75 REG_FIELD(STK3310_REG_PSCTRL
, 0, 3);
76 static const struct reg_field stk3310_reg_field_int_ps
=
77 REG_FIELD(STK3310_REG_INT
, 0, 2);
78 static const struct reg_field stk3310_reg_field_flag_psint
=
79 REG_FIELD(STK3310_REG_FLAG
, 4, 4);
80 static const struct reg_field stk3310_reg_field_flag_nf
=
81 REG_FIELD(STK3310_REG_FLAG
, 0, 0);
83 /* Estimate maximum proximity values with regard to measurement scale. */
84 static const int stk3310_ps_max
[4] = {
85 STK3310_PS_MAX_VAL
/ 640,
86 STK3310_PS_MAX_VAL
/ 160,
87 STK3310_PS_MAX_VAL
/ 40,
88 STK3310_PS_MAX_VAL
/ 10
91 static const int stk3310_scale_table
[][2] = {
92 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
95 /* Integration time in seconds, microseconds */
96 static const int stk3310_it_table
[][2] = {
97 {0, 185}, {0, 370}, {0, 741}, {0, 1480},
98 {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
99 {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
100 {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
103 struct stk3310_data
{
104 struct i2c_client
*client
;
109 struct regmap
*regmap
;
110 struct regmap_field
*reg_state
;
111 struct regmap_field
*reg_als_gain
;
112 struct regmap_field
*reg_ps_gain
;
113 struct regmap_field
*reg_als_it
;
114 struct regmap_field
*reg_ps_it
;
115 struct regmap_field
*reg_int_ps
;
116 struct regmap_field
*reg_flag_psint
;
117 struct regmap_field
*reg_flag_nf
;
120 static const struct iio_event_spec stk3310_events
[] = {
121 /* Proximity event */
123 .type
= IIO_EV_TYPE_THRESH
,
124 .dir
= IIO_EV_DIR_RISING
,
125 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
126 BIT(IIO_EV_INFO_ENABLE
),
128 /* Out-of-proximity event */
130 .type
= IIO_EV_TYPE_THRESH
,
131 .dir
= IIO_EV_DIR_FALLING
,
132 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
133 BIT(IIO_EV_INFO_ENABLE
),
137 static const struct iio_chan_spec stk3310_channels
[] = {
140 .info_mask_separate
=
141 BIT(IIO_CHAN_INFO_RAW
) |
142 BIT(IIO_CHAN_INFO_SCALE
) |
143 BIT(IIO_CHAN_INFO_INT_TIME
),
146 .type
= IIO_PROXIMITY
,
147 .info_mask_separate
=
148 BIT(IIO_CHAN_INFO_RAW
) |
149 BIT(IIO_CHAN_INFO_SCALE
) |
150 BIT(IIO_CHAN_INFO_INT_TIME
),
151 .event_spec
= stk3310_events
,
152 .num_event_specs
= ARRAY_SIZE(stk3310_events
),
156 static IIO_CONST_ATTR(in_illuminance_scale_available
, STK3310_SCALE_AVAILABLE
);
158 static IIO_CONST_ATTR(in_proximity_scale_available
, STK3310_SCALE_AVAILABLE
);
160 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
161 STK3310_IT_AVAILABLE
);
163 static IIO_CONST_ATTR(in_proximity_integration_time_available
,
164 STK3310_IT_AVAILABLE
);
166 static struct attribute
*stk3310_attributes
[] = {
167 &iio_const_attr_in_illuminance_scale_available
.dev_attr
.attr
,
168 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
169 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
170 &iio_const_attr_in_proximity_integration_time_available
.dev_attr
.attr
,
174 static const struct attribute_group stk3310_attribute_group
= {
175 .attrs
= stk3310_attributes
178 static int stk3310_get_index(const int table
[][2], int table_size
,
183 for (i
= 0; i
< table_size
; i
++) {
184 if (val
== table
[i
][0] && val2
== table
[i
][1])
191 static int stk3310_read_event(struct iio_dev
*indio_dev
,
192 const struct iio_chan_spec
*chan
,
193 enum iio_event_type type
,
194 enum iio_event_direction dir
,
195 enum iio_event_info info
,
201 struct stk3310_data
*data
= iio_priv(indio_dev
);
203 if (info
!= IIO_EV_INFO_VALUE
)
206 /* Only proximity interrupts are implemented at the moment. */
207 if (dir
== IIO_EV_DIR_RISING
)
208 reg
= STK3310_REG_THDH_PS
;
209 else if (dir
== IIO_EV_DIR_FALLING
)
210 reg
= STK3310_REG_THDL_PS
;
214 mutex_lock(&data
->lock
);
215 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
216 mutex_unlock(&data
->lock
);
218 dev_err(&data
->client
->dev
, "register read failed\n");
221 *val
= be16_to_cpu(buf
);
226 static int stk3310_write_event(struct iio_dev
*indio_dev
,
227 const struct iio_chan_spec
*chan
,
228 enum iio_event_type type
,
229 enum iio_event_direction dir
,
230 enum iio_event_info info
,
237 struct stk3310_data
*data
= iio_priv(indio_dev
);
238 struct i2c_client
*client
= data
->client
;
240 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
244 if (val
< 0 || val
> stk3310_ps_max
[index
])
247 if (dir
== IIO_EV_DIR_RISING
)
248 reg
= STK3310_REG_THDH_PS
;
249 else if (dir
== IIO_EV_DIR_FALLING
)
250 reg
= STK3310_REG_THDL_PS
;
254 buf
= cpu_to_be16(val
);
255 ret
= regmap_bulk_write(data
->regmap
, reg
, &buf
, 2);
257 dev_err(&client
->dev
, "failed to set PS threshold!\n");
262 static int stk3310_read_event_config(struct iio_dev
*indio_dev
,
263 const struct iio_chan_spec
*chan
,
264 enum iio_event_type type
,
265 enum iio_event_direction dir
)
267 unsigned int event_val
;
269 struct stk3310_data
*data
= iio_priv(indio_dev
);
271 ret
= regmap_field_read(data
->reg_int_ps
, &event_val
);
278 static int stk3310_write_event_config(struct iio_dev
*indio_dev
,
279 const struct iio_chan_spec
*chan
,
280 enum iio_event_type type
,
281 enum iio_event_direction dir
,
285 struct stk3310_data
*data
= iio_priv(indio_dev
);
286 struct i2c_client
*client
= data
->client
;
288 if (state
< 0 || state
> 7)
291 /* Set INT_PS value */
292 mutex_lock(&data
->lock
);
293 ret
= regmap_field_write(data
->reg_int_ps
, state
);
295 dev_err(&client
->dev
, "failed to set interrupt mode\n");
296 mutex_unlock(&data
->lock
);
301 static int stk3310_read_raw(struct iio_dev
*indio_dev
,
302 struct iio_chan_spec
const *chan
,
303 int *val
, int *val2
, long mask
)
309 struct stk3310_data
*data
= iio_priv(indio_dev
);
310 struct i2c_client
*client
= data
->client
;
312 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
316 case IIO_CHAN_INFO_RAW
:
317 if (chan
->type
== IIO_LIGHT
)
318 reg
= STK3310_REG_ALS_DATA_MSB
;
320 reg
= STK3310_REG_PS_DATA_MSB
;
322 mutex_lock(&data
->lock
);
323 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
325 dev_err(&client
->dev
, "register read failed\n");
326 mutex_unlock(&data
->lock
);
329 *val
= be16_to_cpu(buf
);
330 mutex_unlock(&data
->lock
);
332 case IIO_CHAN_INFO_INT_TIME
:
333 if (chan
->type
== IIO_LIGHT
)
334 ret
= regmap_field_read(data
->reg_als_it
, &index
);
336 ret
= regmap_field_read(data
->reg_ps_it
, &index
);
340 *val
= stk3310_it_table
[index
][0];
341 *val2
= stk3310_it_table
[index
][1];
342 return IIO_VAL_INT_PLUS_MICRO
;
343 case IIO_CHAN_INFO_SCALE
:
344 if (chan
->type
== IIO_LIGHT
)
345 ret
= regmap_field_read(data
->reg_als_gain
, &index
);
347 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
351 *val
= stk3310_scale_table
[index
][0];
352 *val2
= stk3310_scale_table
[index
][1];
353 return IIO_VAL_INT_PLUS_MICRO
;
359 static int stk3310_write_raw(struct iio_dev
*indio_dev
,
360 struct iio_chan_spec
const *chan
,
361 int val
, int val2
, long mask
)
365 struct stk3310_data
*data
= iio_priv(indio_dev
);
367 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
371 case IIO_CHAN_INFO_INT_TIME
:
372 index
= stk3310_get_index(stk3310_it_table
,
373 ARRAY_SIZE(stk3310_it_table
),
377 mutex_lock(&data
->lock
);
378 if (chan
->type
== IIO_LIGHT
)
379 ret
= regmap_field_write(data
->reg_als_it
, index
);
381 ret
= regmap_field_write(data
->reg_ps_it
, index
);
383 dev_err(&data
->client
->dev
,
384 "sensor configuration failed\n");
385 mutex_unlock(&data
->lock
);
388 case IIO_CHAN_INFO_SCALE
:
389 index
= stk3310_get_index(stk3310_scale_table
,
390 ARRAY_SIZE(stk3310_scale_table
),
394 mutex_lock(&data
->lock
);
395 if (chan
->type
== IIO_LIGHT
)
396 ret
= regmap_field_write(data
->reg_als_gain
, index
);
398 ret
= regmap_field_write(data
->reg_ps_gain
, index
);
400 dev_err(&data
->client
->dev
,
401 "sensor configuration failed\n");
402 mutex_unlock(&data
->lock
);
409 static const struct iio_info stk3310_info
= {
410 .read_raw
= stk3310_read_raw
,
411 .write_raw
= stk3310_write_raw
,
412 .attrs
= &stk3310_attribute_group
,
413 .read_event_value
= stk3310_read_event
,
414 .write_event_value
= stk3310_write_event
,
415 .read_event_config
= stk3310_read_event_config
,
416 .write_event_config
= stk3310_write_event_config
,
419 static int stk3310_set_state(struct stk3310_data
*data
, u8 state
)
422 struct i2c_client
*client
= data
->client
;
424 /* 3-bit state; 0b100 is not supported. */
425 if (state
> 7 || state
== 4)
428 mutex_lock(&data
->lock
);
429 ret
= regmap_field_write(data
->reg_state
, state
);
431 dev_err(&client
->dev
, "failed to change sensor state\n");
432 } else if (state
!= STK3310_STATE_STANDBY
) {
433 /* Don't reset the 'enabled' flags if we're going in standby */
434 data
->ps_enabled
= !!(state
& STK3310_STATE_EN_PS
);
435 data
->als_enabled
= !!(state
& STK3310_STATE_EN_ALS
);
437 mutex_unlock(&data
->lock
);
442 static int stk3310_init(struct iio_dev
*indio_dev
)
447 struct stk3310_data
*data
= iio_priv(indio_dev
);
448 struct i2c_client
*client
= data
->client
;
450 ret
= regmap_read(data
->regmap
, STK3310_REG_ID
, &chipid
);
454 if (chipid
!= STK3310_CHIP_ID_VAL
&&
455 chipid
!= STK3311_CHIP_ID_VAL
&&
456 chipid
!= STK3335_CHIP_ID_VAL
) {
457 dev_err(&client
->dev
, "invalid chip id: 0x%x\n", chipid
);
461 state
= STK3310_STATE_EN_ALS
| STK3310_STATE_EN_PS
;
462 ret
= stk3310_set_state(data
, state
);
464 dev_err(&client
->dev
, "failed to enable sensor");
468 /* Enable PS interrupts */
469 ret
= regmap_field_write(data
->reg_int_ps
, STK3310_PSINT_EN
);
471 dev_err(&client
->dev
, "failed to enable interrupts!\n");
476 static bool stk3310_is_volatile_reg(struct device
*dev
, unsigned int reg
)
479 case STK3310_REG_ALS_DATA_MSB
:
480 case STK3310_REG_ALS_DATA_LSB
:
481 case STK3310_REG_PS_DATA_LSB
:
482 case STK3310_REG_PS_DATA_MSB
:
483 case STK3310_REG_FLAG
:
490 static struct regmap_config stk3310_regmap_config
= {
491 .name
= STK3310_REGMAP_NAME
,
494 .max_register
= STK3310_MAX_REG
,
495 .cache_type
= REGCACHE_RBTREE
,
496 .volatile_reg
= stk3310_is_volatile_reg
,
499 static int stk3310_regmap_init(struct stk3310_data
*data
)
501 struct regmap
*regmap
;
502 struct i2c_client
*client
;
504 client
= data
->client
;
505 regmap
= devm_regmap_init_i2c(client
, &stk3310_regmap_config
);
506 if (IS_ERR(regmap
)) {
507 dev_err(&client
->dev
, "regmap initialization failed.\n");
508 return PTR_ERR(regmap
);
510 data
->regmap
= regmap
;
512 STK3310_REGFIELD(state
);
513 STK3310_REGFIELD(als_gain
);
514 STK3310_REGFIELD(ps_gain
);
515 STK3310_REGFIELD(als_it
);
516 STK3310_REGFIELD(ps_it
);
517 STK3310_REGFIELD(int_ps
);
518 STK3310_REGFIELD(flag_psint
);
519 STK3310_REGFIELD(flag_nf
);
524 static irqreturn_t
stk3310_irq_handler(int irq
, void *private)
526 struct iio_dev
*indio_dev
= private;
527 struct stk3310_data
*data
= iio_priv(indio_dev
);
529 data
->timestamp
= iio_get_time_ns(indio_dev
);
531 return IRQ_WAKE_THREAD
;
534 static irqreturn_t
stk3310_irq_event_handler(int irq
, void *private)
540 struct iio_dev
*indio_dev
= private;
541 struct stk3310_data
*data
= iio_priv(indio_dev
);
543 /* Read FLAG_NF to figure out what threshold has been met. */
544 mutex_lock(&data
->lock
);
545 ret
= regmap_field_read(data
->reg_flag_nf
, &dir
);
547 dev_err(&data
->client
->dev
, "register read failed\n");
548 mutex_unlock(&data
->lock
);
551 event
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 1,
553 (dir
? IIO_EV_DIR_FALLING
:
555 iio_push_event(indio_dev
, event
, data
->timestamp
);
557 /* Reset the interrupt flag */
558 ret
= regmap_field_write(data
->reg_flag_psint
, 0);
560 dev_err(&data
->client
->dev
, "failed to reset interrupts\n");
561 mutex_unlock(&data
->lock
);
566 static int stk3310_probe(struct i2c_client
*client
,
567 const struct i2c_device_id
*id
)
570 struct iio_dev
*indio_dev
;
571 struct stk3310_data
*data
;
573 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
575 dev_err(&client
->dev
, "iio allocation failed!\n");
579 data
= iio_priv(indio_dev
);
580 data
->client
= client
;
581 i2c_set_clientdata(client
, indio_dev
);
582 mutex_init(&data
->lock
);
584 ret
= stk3310_regmap_init(data
);
588 indio_dev
->dev
.parent
= &client
->dev
;
589 indio_dev
->info
= &stk3310_info
;
590 indio_dev
->name
= STK3310_DRIVER_NAME
;
591 indio_dev
->modes
= INDIO_DIRECT_MODE
;
592 indio_dev
->channels
= stk3310_channels
;
593 indio_dev
->num_channels
= ARRAY_SIZE(stk3310_channels
);
595 ret
= stk3310_init(indio_dev
);
599 if (client
->irq
> 0) {
600 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
602 stk3310_irq_event_handler
,
603 IRQF_TRIGGER_FALLING
|
605 STK3310_EVENT
, indio_dev
);
607 dev_err(&client
->dev
, "request irq %d failed\n",
613 ret
= iio_device_register(indio_dev
);
615 dev_err(&client
->dev
, "device_register failed\n");
622 stk3310_set_state(data
, STK3310_STATE_STANDBY
);
626 static int stk3310_remove(struct i2c_client
*client
)
628 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
630 iio_device_unregister(indio_dev
);
631 return stk3310_set_state(iio_priv(indio_dev
), STK3310_STATE_STANDBY
);
634 #ifdef CONFIG_PM_SLEEP
635 static int stk3310_suspend(struct device
*dev
)
637 struct stk3310_data
*data
;
639 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
641 return stk3310_set_state(data
, STK3310_STATE_STANDBY
);
644 static int stk3310_resume(struct device
*dev
)
647 struct stk3310_data
*data
;
649 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
650 if (data
->ps_enabled
)
651 state
|= STK3310_STATE_EN_PS
;
652 if (data
->als_enabled
)
653 state
|= STK3310_STATE_EN_ALS
;
655 return stk3310_set_state(data
, state
);
658 static SIMPLE_DEV_PM_OPS(stk3310_pm_ops
, stk3310_suspend
, stk3310_resume
);
660 #define STK3310_PM_OPS (&stk3310_pm_ops)
662 #define STK3310_PM_OPS NULL
665 static const struct i2c_device_id stk3310_i2c_id
[] = {
671 MODULE_DEVICE_TABLE(i2c
, stk3310_i2c_id
);
673 static const struct acpi_device_id stk3310_acpi_id
[] = {
680 MODULE_DEVICE_TABLE(acpi
, stk3310_acpi_id
);
682 static const struct of_device_id stk3310_of_match
[] = {
683 { .compatible
= "sensortek,stk3310", },
684 { .compatible
= "sensortek,stk3311", },
685 { .compatible
= "sensortek,stk3335", },
688 MODULE_DEVICE_TABLE(of
, stk3310_of_match
);
690 static struct i2c_driver stk3310_driver
= {
693 .of_match_table
= stk3310_of_match
,
694 .pm
= STK3310_PM_OPS
,
695 .acpi_match_table
= ACPI_PTR(stk3310_acpi_id
),
697 .probe
= stk3310_probe
,
698 .remove
= stk3310_remove
,
699 .id_table
= stk3310_i2c_id
,
702 module_i2c_driver(stk3310_driver
);
704 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
705 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
706 MODULE_LICENSE("GPL v2");