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 STK3311X_CHIP_ID_VAL 0x12
41 #define STK3335_CHIP_ID_VAL 0x51
42 #define STK3310_PSINT_EN 0x01
43 #define STK3310_PS_MAX_VAL 0xFFFF
45 #define STK3310_DRIVER_NAME "stk3310"
46 #define STK3310_REGMAP_NAME "stk3310_regmap"
47 #define STK3310_EVENT "stk3310_event"
49 #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
51 #define STK3310_IT_AVAILABLE \
52 "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
53 "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
56 #define STK3310_REGFIELD(name) \
59 devm_regmap_field_alloc(&client->dev, regmap, \
60 stk3310_reg_field_##name); \
61 if (IS_ERR(data->reg_##name)) { \
62 dev_err(&client->dev, "reg field alloc failed.\n"); \
63 return PTR_ERR(data->reg_##name); \
67 static const struct reg_field stk3310_reg_field_state
=
68 REG_FIELD(STK3310_REG_STATE
, 0, 2);
69 static const struct reg_field stk3310_reg_field_als_gain
=
70 REG_FIELD(STK3310_REG_ALSCTRL
, 4, 5);
71 static const struct reg_field stk3310_reg_field_ps_gain
=
72 REG_FIELD(STK3310_REG_PSCTRL
, 4, 5);
73 static const struct reg_field stk3310_reg_field_als_it
=
74 REG_FIELD(STK3310_REG_ALSCTRL
, 0, 3);
75 static const struct reg_field stk3310_reg_field_ps_it
=
76 REG_FIELD(STK3310_REG_PSCTRL
, 0, 3);
77 static const struct reg_field stk3310_reg_field_int_ps
=
78 REG_FIELD(STK3310_REG_INT
, 0, 2);
79 static const struct reg_field stk3310_reg_field_flag_psint
=
80 REG_FIELD(STK3310_REG_FLAG
, 4, 4);
81 static const struct reg_field stk3310_reg_field_flag_nf
=
82 REG_FIELD(STK3310_REG_FLAG
, 0, 0);
84 /* Estimate maximum proximity values with regard to measurement scale. */
85 static const int stk3310_ps_max
[4] = {
86 STK3310_PS_MAX_VAL
/ 640,
87 STK3310_PS_MAX_VAL
/ 160,
88 STK3310_PS_MAX_VAL
/ 40,
89 STK3310_PS_MAX_VAL
/ 10
92 static const int stk3310_scale_table
[][2] = {
93 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
96 /* Integration time in seconds, microseconds */
97 static const int stk3310_it_table
[][2] = {
98 {0, 185}, {0, 370}, {0, 741}, {0, 1480},
99 {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
100 {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
101 {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
104 struct stk3310_data
{
105 struct i2c_client
*client
;
110 struct regmap
*regmap
;
111 struct regmap_field
*reg_state
;
112 struct regmap_field
*reg_als_gain
;
113 struct regmap_field
*reg_ps_gain
;
114 struct regmap_field
*reg_als_it
;
115 struct regmap_field
*reg_ps_it
;
116 struct regmap_field
*reg_int_ps
;
117 struct regmap_field
*reg_flag_psint
;
118 struct regmap_field
*reg_flag_nf
;
121 static const struct iio_event_spec stk3310_events
[] = {
122 /* Proximity event */
124 .type
= IIO_EV_TYPE_THRESH
,
125 .dir
= IIO_EV_DIR_RISING
,
126 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
127 BIT(IIO_EV_INFO_ENABLE
),
129 /* Out-of-proximity event */
131 .type
= IIO_EV_TYPE_THRESH
,
132 .dir
= IIO_EV_DIR_FALLING
,
133 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
134 BIT(IIO_EV_INFO_ENABLE
),
138 static const struct iio_chan_spec stk3310_channels
[] = {
141 .info_mask_separate
=
142 BIT(IIO_CHAN_INFO_RAW
) |
143 BIT(IIO_CHAN_INFO_SCALE
) |
144 BIT(IIO_CHAN_INFO_INT_TIME
),
147 .type
= IIO_PROXIMITY
,
148 .info_mask_separate
=
149 BIT(IIO_CHAN_INFO_RAW
) |
150 BIT(IIO_CHAN_INFO_SCALE
) |
151 BIT(IIO_CHAN_INFO_INT_TIME
),
152 .event_spec
= stk3310_events
,
153 .num_event_specs
= ARRAY_SIZE(stk3310_events
),
157 static IIO_CONST_ATTR(in_illuminance_scale_available
, STK3310_SCALE_AVAILABLE
);
159 static IIO_CONST_ATTR(in_proximity_scale_available
, STK3310_SCALE_AVAILABLE
);
161 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
162 STK3310_IT_AVAILABLE
);
164 static IIO_CONST_ATTR(in_proximity_integration_time_available
,
165 STK3310_IT_AVAILABLE
);
167 static struct attribute
*stk3310_attributes
[] = {
168 &iio_const_attr_in_illuminance_scale_available
.dev_attr
.attr
,
169 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
170 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
171 &iio_const_attr_in_proximity_integration_time_available
.dev_attr
.attr
,
175 static const struct attribute_group stk3310_attribute_group
= {
176 .attrs
= stk3310_attributes
179 static int stk3310_get_index(const int table
[][2], int table_size
,
184 for (i
= 0; i
< table_size
; i
++) {
185 if (val
== table
[i
][0] && val2
== table
[i
][1])
192 static int stk3310_read_event(struct iio_dev
*indio_dev
,
193 const struct iio_chan_spec
*chan
,
194 enum iio_event_type type
,
195 enum iio_event_direction dir
,
196 enum iio_event_info info
,
202 struct stk3310_data
*data
= iio_priv(indio_dev
);
204 if (info
!= IIO_EV_INFO_VALUE
)
207 /* Only proximity interrupts are implemented at the moment. */
208 if (dir
== IIO_EV_DIR_RISING
)
209 reg
= STK3310_REG_THDH_PS
;
210 else if (dir
== IIO_EV_DIR_FALLING
)
211 reg
= STK3310_REG_THDL_PS
;
215 mutex_lock(&data
->lock
);
216 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
217 mutex_unlock(&data
->lock
);
219 dev_err(&data
->client
->dev
, "register read failed\n");
222 *val
= be16_to_cpu(buf
);
227 static int stk3310_write_event(struct iio_dev
*indio_dev
,
228 const struct iio_chan_spec
*chan
,
229 enum iio_event_type type
,
230 enum iio_event_direction dir
,
231 enum iio_event_info info
,
238 struct stk3310_data
*data
= iio_priv(indio_dev
);
239 struct i2c_client
*client
= data
->client
;
241 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
245 if (val
< 0 || val
> stk3310_ps_max
[index
])
248 if (dir
== IIO_EV_DIR_RISING
)
249 reg
= STK3310_REG_THDH_PS
;
250 else if (dir
== IIO_EV_DIR_FALLING
)
251 reg
= STK3310_REG_THDL_PS
;
255 buf
= cpu_to_be16(val
);
256 ret
= regmap_bulk_write(data
->regmap
, reg
, &buf
, 2);
258 dev_err(&client
->dev
, "failed to set PS threshold!\n");
263 static int stk3310_read_event_config(struct iio_dev
*indio_dev
,
264 const struct iio_chan_spec
*chan
,
265 enum iio_event_type type
,
266 enum iio_event_direction dir
)
268 unsigned int event_val
;
270 struct stk3310_data
*data
= iio_priv(indio_dev
);
272 ret
= regmap_field_read(data
->reg_int_ps
, &event_val
);
279 static int stk3310_write_event_config(struct iio_dev
*indio_dev
,
280 const struct iio_chan_spec
*chan
,
281 enum iio_event_type type
,
282 enum iio_event_direction dir
,
286 struct stk3310_data
*data
= iio_priv(indio_dev
);
287 struct i2c_client
*client
= data
->client
;
289 if (state
< 0 || state
> 7)
292 /* Set INT_PS value */
293 mutex_lock(&data
->lock
);
294 ret
= regmap_field_write(data
->reg_int_ps
, state
);
296 dev_err(&client
->dev
, "failed to set interrupt mode\n");
297 mutex_unlock(&data
->lock
);
302 static int stk3310_read_raw(struct iio_dev
*indio_dev
,
303 struct iio_chan_spec
const *chan
,
304 int *val
, int *val2
, long mask
)
310 struct stk3310_data
*data
= iio_priv(indio_dev
);
311 struct i2c_client
*client
= data
->client
;
313 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
317 case IIO_CHAN_INFO_RAW
:
318 if (chan
->type
== IIO_LIGHT
)
319 reg
= STK3310_REG_ALS_DATA_MSB
;
321 reg
= STK3310_REG_PS_DATA_MSB
;
323 mutex_lock(&data
->lock
);
324 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
326 dev_err(&client
->dev
, "register read failed\n");
327 mutex_unlock(&data
->lock
);
330 *val
= be16_to_cpu(buf
);
331 mutex_unlock(&data
->lock
);
333 case IIO_CHAN_INFO_INT_TIME
:
334 if (chan
->type
== IIO_LIGHT
)
335 ret
= regmap_field_read(data
->reg_als_it
, &index
);
337 ret
= regmap_field_read(data
->reg_ps_it
, &index
);
341 *val
= stk3310_it_table
[index
][0];
342 *val2
= stk3310_it_table
[index
][1];
343 return IIO_VAL_INT_PLUS_MICRO
;
344 case IIO_CHAN_INFO_SCALE
:
345 if (chan
->type
== IIO_LIGHT
)
346 ret
= regmap_field_read(data
->reg_als_gain
, &index
);
348 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
352 *val
= stk3310_scale_table
[index
][0];
353 *val2
= stk3310_scale_table
[index
][1];
354 return IIO_VAL_INT_PLUS_MICRO
;
360 static int stk3310_write_raw(struct iio_dev
*indio_dev
,
361 struct iio_chan_spec
const *chan
,
362 int val
, int val2
, long mask
)
366 struct stk3310_data
*data
= iio_priv(indio_dev
);
368 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
372 case IIO_CHAN_INFO_INT_TIME
:
373 index
= stk3310_get_index(stk3310_it_table
,
374 ARRAY_SIZE(stk3310_it_table
),
378 mutex_lock(&data
->lock
);
379 if (chan
->type
== IIO_LIGHT
)
380 ret
= regmap_field_write(data
->reg_als_it
, index
);
382 ret
= regmap_field_write(data
->reg_ps_it
, index
);
384 dev_err(&data
->client
->dev
,
385 "sensor configuration failed\n");
386 mutex_unlock(&data
->lock
);
389 case IIO_CHAN_INFO_SCALE
:
390 index
= stk3310_get_index(stk3310_scale_table
,
391 ARRAY_SIZE(stk3310_scale_table
),
395 mutex_lock(&data
->lock
);
396 if (chan
->type
== IIO_LIGHT
)
397 ret
= regmap_field_write(data
->reg_als_gain
, index
);
399 ret
= regmap_field_write(data
->reg_ps_gain
, index
);
401 dev_err(&data
->client
->dev
,
402 "sensor configuration failed\n");
403 mutex_unlock(&data
->lock
);
410 static const struct iio_info stk3310_info
= {
411 .read_raw
= stk3310_read_raw
,
412 .write_raw
= stk3310_write_raw
,
413 .attrs
= &stk3310_attribute_group
,
414 .read_event_value
= stk3310_read_event
,
415 .write_event_value
= stk3310_write_event
,
416 .read_event_config
= stk3310_read_event_config
,
417 .write_event_config
= stk3310_write_event_config
,
420 static int stk3310_set_state(struct stk3310_data
*data
, u8 state
)
423 struct i2c_client
*client
= data
->client
;
425 /* 3-bit state; 0b100 is not supported. */
426 if (state
> 7 || state
== 4)
429 mutex_lock(&data
->lock
);
430 ret
= regmap_field_write(data
->reg_state
, state
);
432 dev_err(&client
->dev
, "failed to change sensor state\n");
433 } else if (state
!= STK3310_STATE_STANDBY
) {
434 /* Don't reset the 'enabled' flags if we're going in standby */
435 data
->ps_enabled
= !!(state
& STK3310_STATE_EN_PS
);
436 data
->als_enabled
= !!(state
& STK3310_STATE_EN_ALS
);
438 mutex_unlock(&data
->lock
);
443 static int stk3310_init(struct iio_dev
*indio_dev
)
448 struct stk3310_data
*data
= iio_priv(indio_dev
);
449 struct i2c_client
*client
= data
->client
;
451 ret
= regmap_read(data
->regmap
, STK3310_REG_ID
, &chipid
);
455 if (chipid
!= STK3310_CHIP_ID_VAL
&&
456 chipid
!= STK3311_CHIP_ID_VAL
&&
457 chipid
!= STK3311X_CHIP_ID_VAL
&&
458 chipid
!= STK3335_CHIP_ID_VAL
) {
459 dev_err(&client
->dev
, "invalid chip id: 0x%x\n", chipid
);
463 state
= STK3310_STATE_EN_ALS
| STK3310_STATE_EN_PS
;
464 ret
= stk3310_set_state(data
, state
);
466 dev_err(&client
->dev
, "failed to enable sensor");
470 /* Enable PS interrupts */
471 ret
= regmap_field_write(data
->reg_int_ps
, STK3310_PSINT_EN
);
473 dev_err(&client
->dev
, "failed to enable interrupts!\n");
478 static bool stk3310_is_volatile_reg(struct device
*dev
, unsigned int reg
)
481 case STK3310_REG_ALS_DATA_MSB
:
482 case STK3310_REG_ALS_DATA_LSB
:
483 case STK3310_REG_PS_DATA_LSB
:
484 case STK3310_REG_PS_DATA_MSB
:
485 case STK3310_REG_FLAG
:
492 static const struct regmap_config stk3310_regmap_config
= {
493 .name
= STK3310_REGMAP_NAME
,
496 .max_register
= STK3310_MAX_REG
,
497 .cache_type
= REGCACHE_RBTREE
,
498 .volatile_reg
= stk3310_is_volatile_reg
,
501 static int stk3310_regmap_init(struct stk3310_data
*data
)
503 struct regmap
*regmap
;
504 struct i2c_client
*client
;
506 client
= data
->client
;
507 regmap
= devm_regmap_init_i2c(client
, &stk3310_regmap_config
);
508 if (IS_ERR(regmap
)) {
509 dev_err(&client
->dev
, "regmap initialization failed.\n");
510 return PTR_ERR(regmap
);
512 data
->regmap
= regmap
;
514 STK3310_REGFIELD(state
);
515 STK3310_REGFIELD(als_gain
);
516 STK3310_REGFIELD(ps_gain
);
517 STK3310_REGFIELD(als_it
);
518 STK3310_REGFIELD(ps_it
);
519 STK3310_REGFIELD(int_ps
);
520 STK3310_REGFIELD(flag_psint
);
521 STK3310_REGFIELD(flag_nf
);
526 static irqreturn_t
stk3310_irq_handler(int irq
, void *private)
528 struct iio_dev
*indio_dev
= private;
529 struct stk3310_data
*data
= iio_priv(indio_dev
);
531 data
->timestamp
= iio_get_time_ns(indio_dev
);
533 return IRQ_WAKE_THREAD
;
536 static irqreturn_t
stk3310_irq_event_handler(int irq
, void *private)
542 struct iio_dev
*indio_dev
= private;
543 struct stk3310_data
*data
= iio_priv(indio_dev
);
545 /* Read FLAG_NF to figure out what threshold has been met. */
546 mutex_lock(&data
->lock
);
547 ret
= regmap_field_read(data
->reg_flag_nf
, &dir
);
549 dev_err(&data
->client
->dev
, "register read failed\n");
550 mutex_unlock(&data
->lock
);
553 event
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 1,
555 (dir
? IIO_EV_DIR_FALLING
:
557 iio_push_event(indio_dev
, event
, data
->timestamp
);
559 /* Reset the interrupt flag */
560 ret
= regmap_field_write(data
->reg_flag_psint
, 0);
562 dev_err(&data
->client
->dev
, "failed to reset interrupts\n");
563 mutex_unlock(&data
->lock
);
568 static int stk3310_probe(struct i2c_client
*client
,
569 const struct i2c_device_id
*id
)
572 struct iio_dev
*indio_dev
;
573 struct stk3310_data
*data
;
575 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
577 dev_err(&client
->dev
, "iio allocation failed!\n");
581 data
= iio_priv(indio_dev
);
582 data
->client
= client
;
583 i2c_set_clientdata(client
, indio_dev
);
584 mutex_init(&data
->lock
);
586 ret
= stk3310_regmap_init(data
);
590 indio_dev
->info
= &stk3310_info
;
591 indio_dev
->name
= STK3310_DRIVER_NAME
;
592 indio_dev
->modes
= INDIO_DIRECT_MODE
;
593 indio_dev
->channels
= stk3310_channels
;
594 indio_dev
->num_channels
= ARRAY_SIZE(stk3310_channels
);
596 ret
= stk3310_init(indio_dev
);
600 if (client
->irq
> 0) {
601 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
603 stk3310_irq_event_handler
,
604 IRQF_TRIGGER_FALLING
|
606 STK3310_EVENT
, indio_dev
);
608 dev_err(&client
->dev
, "request irq %d failed\n",
614 ret
= iio_device_register(indio_dev
);
616 dev_err(&client
->dev
, "device_register failed\n");
623 stk3310_set_state(data
, STK3310_STATE_STANDBY
);
627 static int stk3310_remove(struct i2c_client
*client
)
629 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
631 iio_device_unregister(indio_dev
);
632 return stk3310_set_state(iio_priv(indio_dev
), STK3310_STATE_STANDBY
);
635 #ifdef CONFIG_PM_SLEEP
636 static int stk3310_suspend(struct device
*dev
)
638 struct stk3310_data
*data
;
640 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
642 return stk3310_set_state(data
, STK3310_STATE_STANDBY
);
645 static int stk3310_resume(struct device
*dev
)
648 struct stk3310_data
*data
;
650 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
651 if (data
->ps_enabled
)
652 state
|= STK3310_STATE_EN_PS
;
653 if (data
->als_enabled
)
654 state
|= STK3310_STATE_EN_ALS
;
656 return stk3310_set_state(data
, state
);
659 static SIMPLE_DEV_PM_OPS(stk3310_pm_ops
, stk3310_suspend
, stk3310_resume
);
661 #define STK3310_PM_OPS (&stk3310_pm_ops)
663 #define STK3310_PM_OPS NULL
666 static const struct i2c_device_id stk3310_i2c_id
[] = {
672 MODULE_DEVICE_TABLE(i2c
, stk3310_i2c_id
);
674 static const struct acpi_device_id stk3310_acpi_id
[] = {
681 MODULE_DEVICE_TABLE(acpi
, stk3310_acpi_id
);
683 static const struct of_device_id stk3310_of_match
[] = {
684 { .compatible
= "sensortek,stk3310", },
685 { .compatible
= "sensortek,stk3311", },
686 { .compatible
= "sensortek,stk3335", },
689 MODULE_DEVICE_TABLE(of
, stk3310_of_match
);
691 static struct i2c_driver stk3310_driver
= {
694 .of_match_table
= stk3310_of_match
,
695 .pm
= STK3310_PM_OPS
,
696 .acpi_match_table
= ACPI_PTR(stk3310_acpi_id
),
698 .probe
= stk3310_probe
,
699 .remove
= stk3310_remove
,
700 .id_table
= stk3310_i2c_id
,
703 module_i2c_driver(stk3310_driver
);
705 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
706 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
707 MODULE_LICENSE("GPL v2");