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/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>
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 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
48 #define STK3310_DRIVER_NAME "stk3310"
49 #define STK3310_REGMAP_NAME "stk3310_regmap"
50 #define STK3310_EVENT "stk3310_event"
52 #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
54 #define STK3310_IT_AVAILABLE \
55 "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
56 "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
59 #define STK3310_REGFIELD(name) \
62 devm_regmap_field_alloc(&client->dev, regmap, \
63 stk3310_reg_field_##name); \
64 if (IS_ERR(data->reg_##name)) { \
65 dev_err(&client->dev, "reg field alloc failed.\n"); \
66 return PTR_ERR(data->reg_##name); \
70 static const struct reg_field stk3310_reg_field_state
=
71 REG_FIELD(STK3310_REG_STATE
, 0, 2);
72 static const struct reg_field stk3310_reg_field_als_gain
=
73 REG_FIELD(STK3310_REG_ALSCTRL
, 4, 5);
74 static const struct reg_field stk3310_reg_field_ps_gain
=
75 REG_FIELD(STK3310_REG_PSCTRL
, 4, 5);
76 static const struct reg_field stk3310_reg_field_als_it
=
77 REG_FIELD(STK3310_REG_ALSCTRL
, 0, 3);
78 static const struct reg_field stk3310_reg_field_ps_it
=
79 REG_FIELD(STK3310_REG_PSCTRL
, 0, 3);
80 static const struct reg_field stk3310_reg_field_int_ps
=
81 REG_FIELD(STK3310_REG_INT
, 0, 2);
82 static const struct reg_field stk3310_reg_field_flag_psint
=
83 REG_FIELD(STK3310_REG_FLAG
, 4, 4);
84 static const struct reg_field stk3310_reg_field_flag_nf
=
85 REG_FIELD(STK3310_REG_FLAG
, 0, 0);
87 static const u8 stk3310_chip_ids
[] = {
91 STK3311S34_CHIP_ID_VAL
,
97 /* Estimate maximum proximity values with regard to measurement scale. */
98 static const int stk3310_ps_max
[4] = {
99 STK3310_PS_MAX_VAL
/ 640,
100 STK3310_PS_MAX_VAL
/ 160,
101 STK3310_PS_MAX_VAL
/ 40,
102 STK3310_PS_MAX_VAL
/ 10
105 static const int stk3310_scale_table
[][2] = {
106 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
109 /* Integration time in seconds, microseconds */
110 static const int stk3310_it_table
[][2] = {
111 {0, 185}, {0, 370}, {0, 741}, {0, 1480},
112 {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
113 {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
114 {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
117 struct stk3310_data
{
118 struct i2c_client
*client
;
122 uint32_t ps_near_level
;
124 struct regmap
*regmap
;
125 struct regmap_field
*reg_state
;
126 struct regmap_field
*reg_als_gain
;
127 struct regmap_field
*reg_ps_gain
;
128 struct regmap_field
*reg_als_it
;
129 struct regmap_field
*reg_ps_it
;
130 struct regmap_field
*reg_int_ps
;
131 struct regmap_field
*reg_flag_psint
;
132 struct regmap_field
*reg_flag_nf
;
135 static const struct iio_event_spec stk3310_events
[] = {
136 /* Proximity event */
138 .type
= IIO_EV_TYPE_THRESH
,
139 .dir
= IIO_EV_DIR_RISING
,
140 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
141 BIT(IIO_EV_INFO_ENABLE
),
143 /* Out-of-proximity event */
145 .type
= IIO_EV_TYPE_THRESH
,
146 .dir
= IIO_EV_DIR_FALLING
,
147 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
148 BIT(IIO_EV_INFO_ENABLE
),
152 static ssize_t
stk3310_read_near_level(struct iio_dev
*indio_dev
,
154 const struct iio_chan_spec
*chan
,
157 struct stk3310_data
*data
= iio_priv(indio_dev
);
159 return sprintf(buf
, "%u\n", data
->ps_near_level
);
162 static const struct iio_chan_spec_ext_info stk3310_ext_info
[] = {
165 .shared
= IIO_SEPARATE
,
166 .read
= stk3310_read_near_level
,
171 static const struct iio_chan_spec stk3310_channels
[] = {
174 .info_mask_separate
=
175 BIT(IIO_CHAN_INFO_RAW
) |
176 BIT(IIO_CHAN_INFO_SCALE
) |
177 BIT(IIO_CHAN_INFO_INT_TIME
),
180 .type
= IIO_PROXIMITY
,
181 .info_mask_separate
=
182 BIT(IIO_CHAN_INFO_RAW
) |
183 BIT(IIO_CHAN_INFO_SCALE
) |
184 BIT(IIO_CHAN_INFO_INT_TIME
),
185 .event_spec
= stk3310_events
,
186 .num_event_specs
= ARRAY_SIZE(stk3310_events
),
187 .ext_info
= stk3310_ext_info
,
191 static IIO_CONST_ATTR(in_illuminance_scale_available
, STK3310_SCALE_AVAILABLE
);
193 static IIO_CONST_ATTR(in_proximity_scale_available
, STK3310_SCALE_AVAILABLE
);
195 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
196 STK3310_IT_AVAILABLE
);
198 static IIO_CONST_ATTR(in_proximity_integration_time_available
,
199 STK3310_IT_AVAILABLE
);
201 static struct attribute
*stk3310_attributes
[] = {
202 &iio_const_attr_in_illuminance_scale_available
.dev_attr
.attr
,
203 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
204 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
205 &iio_const_attr_in_proximity_integration_time_available
.dev_attr
.attr
,
209 static const struct attribute_group stk3310_attribute_group
= {
210 .attrs
= stk3310_attributes
213 static int stk3310_check_chip_id(const u8 chip_id
)
215 for (int i
= 0; i
< ARRAY_SIZE(stk3310_chip_ids
); i
++) {
216 if (chip_id
== stk3310_chip_ids
[i
])
223 static int stk3310_get_index(const int table
[][2], int table_size
,
228 for (i
= 0; i
< table_size
; i
++) {
229 if (val
== table
[i
][0] && val2
== table
[i
][1])
236 static int stk3310_read_event(struct iio_dev
*indio_dev
,
237 const struct iio_chan_spec
*chan
,
238 enum iio_event_type type
,
239 enum iio_event_direction dir
,
240 enum iio_event_info info
,
246 struct stk3310_data
*data
= iio_priv(indio_dev
);
248 if (info
!= IIO_EV_INFO_VALUE
)
251 /* Only proximity interrupts are implemented at the moment. */
252 if (dir
== IIO_EV_DIR_RISING
)
253 reg
= STK3310_REG_THDH_PS
;
254 else if (dir
== IIO_EV_DIR_FALLING
)
255 reg
= STK3310_REG_THDL_PS
;
259 mutex_lock(&data
->lock
);
260 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
261 mutex_unlock(&data
->lock
);
263 dev_err(&data
->client
->dev
, "register read failed\n");
266 *val
= be16_to_cpu(buf
);
271 static int stk3310_write_event(struct iio_dev
*indio_dev
,
272 const struct iio_chan_spec
*chan
,
273 enum iio_event_type type
,
274 enum iio_event_direction dir
,
275 enum iio_event_info info
,
282 struct stk3310_data
*data
= iio_priv(indio_dev
);
283 struct i2c_client
*client
= data
->client
;
285 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
289 if (val
< 0 || val
> stk3310_ps_max
[index
])
292 if (dir
== IIO_EV_DIR_RISING
)
293 reg
= STK3310_REG_THDH_PS
;
294 else if (dir
== IIO_EV_DIR_FALLING
)
295 reg
= STK3310_REG_THDL_PS
;
299 buf
= cpu_to_be16(val
);
300 ret
= regmap_bulk_write(data
->regmap
, reg
, &buf
, 2);
302 dev_err(&client
->dev
, "failed to set PS threshold!\n");
307 static int stk3310_read_event_config(struct iio_dev
*indio_dev
,
308 const struct iio_chan_spec
*chan
,
309 enum iio_event_type type
,
310 enum iio_event_direction dir
)
312 unsigned int event_val
;
314 struct stk3310_data
*data
= iio_priv(indio_dev
);
316 ret
= regmap_field_read(data
->reg_int_ps
, &event_val
);
323 static int stk3310_write_event_config(struct iio_dev
*indio_dev
,
324 const struct iio_chan_spec
*chan
,
325 enum iio_event_type type
,
326 enum iio_event_direction dir
,
330 struct stk3310_data
*data
= iio_priv(indio_dev
);
331 struct i2c_client
*client
= data
->client
;
333 /* Set INT_PS value */
334 mutex_lock(&data
->lock
);
335 ret
= regmap_field_write(data
->reg_int_ps
, state
);
337 dev_err(&client
->dev
, "failed to set interrupt mode\n");
338 mutex_unlock(&data
->lock
);
343 static int stk3310_read_raw(struct iio_dev
*indio_dev
,
344 struct iio_chan_spec
const *chan
,
345 int *val
, int *val2
, long mask
)
351 struct stk3310_data
*data
= iio_priv(indio_dev
);
352 struct i2c_client
*client
= data
->client
;
354 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
358 case IIO_CHAN_INFO_RAW
:
359 if (chan
->type
== IIO_LIGHT
)
360 reg
= STK3310_REG_ALS_DATA_MSB
;
362 reg
= STK3310_REG_PS_DATA_MSB
;
364 mutex_lock(&data
->lock
);
365 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
367 dev_err(&client
->dev
, "register read failed\n");
368 mutex_unlock(&data
->lock
);
371 *val
= be16_to_cpu(buf
);
372 mutex_unlock(&data
->lock
);
374 case IIO_CHAN_INFO_INT_TIME
:
375 if (chan
->type
== IIO_LIGHT
)
376 ret
= regmap_field_read(data
->reg_als_it
, &index
);
378 ret
= regmap_field_read(data
->reg_ps_it
, &index
);
382 *val
= stk3310_it_table
[index
][0];
383 *val2
= stk3310_it_table
[index
][1];
384 return IIO_VAL_INT_PLUS_MICRO
;
385 case IIO_CHAN_INFO_SCALE
:
386 if (chan
->type
== IIO_LIGHT
)
387 ret
= regmap_field_read(data
->reg_als_gain
, &index
);
389 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
393 *val
= stk3310_scale_table
[index
][0];
394 *val2
= stk3310_scale_table
[index
][1];
395 return IIO_VAL_INT_PLUS_MICRO
;
401 static int stk3310_write_raw(struct iio_dev
*indio_dev
,
402 struct iio_chan_spec
const *chan
,
403 int val
, int val2
, long mask
)
407 struct stk3310_data
*data
= iio_priv(indio_dev
);
409 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
413 case IIO_CHAN_INFO_INT_TIME
:
414 index
= stk3310_get_index(stk3310_it_table
,
415 ARRAY_SIZE(stk3310_it_table
),
419 mutex_lock(&data
->lock
);
420 if (chan
->type
== IIO_LIGHT
)
421 ret
= regmap_field_write(data
->reg_als_it
, index
);
423 ret
= regmap_field_write(data
->reg_ps_it
, index
);
425 dev_err(&data
->client
->dev
,
426 "sensor configuration failed\n");
427 mutex_unlock(&data
->lock
);
430 case IIO_CHAN_INFO_SCALE
:
431 index
= stk3310_get_index(stk3310_scale_table
,
432 ARRAY_SIZE(stk3310_scale_table
),
436 mutex_lock(&data
->lock
);
437 if (chan
->type
== IIO_LIGHT
)
438 ret
= regmap_field_write(data
->reg_als_gain
, index
);
440 ret
= regmap_field_write(data
->reg_ps_gain
, index
);
442 dev_err(&data
->client
->dev
,
443 "sensor configuration failed\n");
444 mutex_unlock(&data
->lock
);
451 static const struct iio_info stk3310_info
= {
452 .read_raw
= stk3310_read_raw
,
453 .write_raw
= stk3310_write_raw
,
454 .attrs
= &stk3310_attribute_group
,
455 .read_event_value
= stk3310_read_event
,
456 .write_event_value
= stk3310_write_event
,
457 .read_event_config
= stk3310_read_event_config
,
458 .write_event_config
= stk3310_write_event_config
,
461 static int stk3310_set_state(struct stk3310_data
*data
, u8 state
)
464 struct i2c_client
*client
= data
->client
;
466 /* 3-bit state; 0b100 is not supported. */
467 if (state
> 7 || state
== 4)
470 mutex_lock(&data
->lock
);
471 ret
= regmap_field_write(data
->reg_state
, state
);
473 dev_err(&client
->dev
, "failed to change sensor state\n");
474 } else if (state
!= STK3310_STATE_STANDBY
) {
475 /* Don't reset the 'enabled' flags if we're going in standby */
476 data
->ps_enabled
= !!(state
& STK3310_STATE_EN_PS
);
477 data
->als_enabled
= !!(state
& STK3310_STATE_EN_ALS
);
479 mutex_unlock(&data
->lock
);
484 static int stk3310_init(struct iio_dev
*indio_dev
)
489 struct stk3310_data
*data
= iio_priv(indio_dev
);
490 struct i2c_client
*client
= data
->client
;
492 ret
= regmap_read(data
->regmap
, STK3310_REG_ID
, &chipid
);
496 ret
= stk3310_check_chip_id(chipid
);
498 dev_info(&client
->dev
, "new unknown chip id: 0x%x\n", chipid
);
500 state
= STK3310_STATE_EN_ALS
| STK3310_STATE_EN_PS
;
501 ret
= stk3310_set_state(data
, state
);
503 dev_err(&client
->dev
, "failed to enable sensor");
507 /* Enable PS interrupts */
508 ret
= regmap_field_write(data
->reg_int_ps
, STK3310_PSINT_EN
);
510 dev_err(&client
->dev
, "failed to enable interrupts!\n");
515 static bool stk3310_is_volatile_reg(struct device
*dev
, unsigned int reg
)
518 case STK3310_REG_ALS_DATA_MSB
:
519 case STK3310_REG_ALS_DATA_LSB
:
520 case STK3310_REG_PS_DATA_LSB
:
521 case STK3310_REG_PS_DATA_MSB
:
522 case STK3310_REG_FLAG
:
529 static const struct regmap_config stk3310_regmap_config
= {
530 .name
= STK3310_REGMAP_NAME
,
533 .max_register
= STK3310_MAX_REG
,
534 .cache_type
= REGCACHE_RBTREE
,
535 .volatile_reg
= stk3310_is_volatile_reg
,
538 static int stk3310_regmap_init(struct stk3310_data
*data
)
540 struct regmap
*regmap
;
541 struct i2c_client
*client
;
543 client
= data
->client
;
544 regmap
= devm_regmap_init_i2c(client
, &stk3310_regmap_config
);
545 if (IS_ERR(regmap
)) {
546 dev_err(&client
->dev
, "regmap initialization failed.\n");
547 return PTR_ERR(regmap
);
549 data
->regmap
= regmap
;
551 STK3310_REGFIELD(state
);
552 STK3310_REGFIELD(als_gain
);
553 STK3310_REGFIELD(ps_gain
);
554 STK3310_REGFIELD(als_it
);
555 STK3310_REGFIELD(ps_it
);
556 STK3310_REGFIELD(int_ps
);
557 STK3310_REGFIELD(flag_psint
);
558 STK3310_REGFIELD(flag_nf
);
563 static irqreturn_t
stk3310_irq_handler(int irq
, void *private)
565 struct iio_dev
*indio_dev
= private;
566 struct stk3310_data
*data
= iio_priv(indio_dev
);
568 data
->timestamp
= iio_get_time_ns(indio_dev
);
570 return IRQ_WAKE_THREAD
;
573 static irqreturn_t
stk3310_irq_event_handler(int irq
, void *private)
579 struct iio_dev
*indio_dev
= private;
580 struct stk3310_data
*data
= iio_priv(indio_dev
);
582 /* Read FLAG_NF to figure out what threshold has been met. */
583 mutex_lock(&data
->lock
);
584 ret
= regmap_field_read(data
->reg_flag_nf
, &dir
);
586 dev_err(&data
->client
->dev
, "register read failed: %d\n", ret
);
589 event
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 1,
591 (dir
? IIO_EV_DIR_FALLING
:
593 iio_push_event(indio_dev
, event
, data
->timestamp
);
595 /* Reset the interrupt flag */
596 ret
= regmap_field_write(data
->reg_flag_psint
, 0);
598 dev_err(&data
->client
->dev
, "failed to reset interrupts\n");
600 mutex_unlock(&data
->lock
);
605 static int stk3310_probe(struct i2c_client
*client
)
608 struct iio_dev
*indio_dev
;
609 struct stk3310_data
*data
;
611 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
613 dev_err(&client
->dev
, "iio allocation failed!\n");
617 data
= iio_priv(indio_dev
);
618 data
->client
= client
;
619 i2c_set_clientdata(client
, indio_dev
);
621 device_property_read_u32(&client
->dev
, "proximity-near-level",
622 &data
->ps_near_level
);
624 mutex_init(&data
->lock
);
626 ret
= stk3310_regmap_init(data
);
630 indio_dev
->info
= &stk3310_info
;
631 indio_dev
->name
= STK3310_DRIVER_NAME
;
632 indio_dev
->modes
= INDIO_DIRECT_MODE
;
633 indio_dev
->channels
= stk3310_channels
;
634 indio_dev
->num_channels
= ARRAY_SIZE(stk3310_channels
);
636 ret
= stk3310_init(indio_dev
);
640 if (client
->irq
> 0) {
641 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
643 stk3310_irq_event_handler
,
644 IRQF_TRIGGER_FALLING
|
646 STK3310_EVENT
, indio_dev
);
648 dev_err(&client
->dev
, "request irq %d failed\n",
654 ret
= iio_device_register(indio_dev
);
656 dev_err(&client
->dev
, "device_register failed\n");
663 stk3310_set_state(data
, STK3310_STATE_STANDBY
);
667 static void stk3310_remove(struct i2c_client
*client
)
669 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
671 iio_device_unregister(indio_dev
);
672 stk3310_set_state(iio_priv(indio_dev
), STK3310_STATE_STANDBY
);
675 static int stk3310_suspend(struct device
*dev
)
677 struct stk3310_data
*data
;
679 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
681 return stk3310_set_state(data
, STK3310_STATE_STANDBY
);
684 static int stk3310_resume(struct device
*dev
)
687 struct stk3310_data
*data
;
689 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
690 if (data
->ps_enabled
)
691 state
|= STK3310_STATE_EN_PS
;
692 if (data
->als_enabled
)
693 state
|= STK3310_STATE_EN_ALS
;
695 return stk3310_set_state(data
, state
);
698 static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops
, stk3310_suspend
,
701 static const struct i2c_device_id stk3310_i2c_id
[] = {
708 MODULE_DEVICE_TABLE(i2c
, stk3310_i2c_id
);
710 static const struct acpi_device_id stk3310_acpi_id
[] = {
717 MODULE_DEVICE_TABLE(acpi
, stk3310_acpi_id
);
719 static const struct of_device_id stk3310_of_match
[] = {
720 { .compatible
= "sensortek,stk3013", },
721 { .compatible
= "sensortek,stk3310", },
722 { .compatible
= "sensortek,stk3311", },
723 { .compatible
= "sensortek,stk3335", },
726 MODULE_DEVICE_TABLE(of
, stk3310_of_match
);
728 static struct i2c_driver stk3310_driver
= {
731 .of_match_table
= stk3310_of_match
,
732 .pm
= pm_sleep_ptr(&stk3310_pm_ops
),
733 .acpi_match_table
= stk3310_acpi_id
,
735 .probe
= stk3310_probe
,
736 .remove
= stk3310_remove
,
737 .id_table
= stk3310_i2c_id
,
740 module_i2c_driver(stk3310_driver
);
742 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
743 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
744 MODULE_LICENSE("GPL v2");