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 STK3310_PSINT_EN 0x01
41 #define STK3310_PS_MAX_VAL 0xFFFF
43 #define STK3310_DRIVER_NAME "stk3310"
44 #define STK3310_REGMAP_NAME "stk3310_regmap"
45 #define STK3310_EVENT "stk3310_event"
47 #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
49 #define STK3310_IT_AVAILABLE \
50 "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
51 "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
54 #define STK3310_REGFIELD(name) \
57 devm_regmap_field_alloc(&client->dev, regmap, \
58 stk3310_reg_field_##name); \
59 if (IS_ERR(data->reg_##name)) { \
60 dev_err(&client->dev, "reg field alloc failed.\n"); \
61 return PTR_ERR(data->reg_##name); \
65 static const struct reg_field stk3310_reg_field_state
=
66 REG_FIELD(STK3310_REG_STATE
, 0, 2);
67 static const struct reg_field stk3310_reg_field_als_gain
=
68 REG_FIELD(STK3310_REG_ALSCTRL
, 4, 5);
69 static const struct reg_field stk3310_reg_field_ps_gain
=
70 REG_FIELD(STK3310_REG_PSCTRL
, 4, 5);
71 static const struct reg_field stk3310_reg_field_als_it
=
72 REG_FIELD(STK3310_REG_ALSCTRL
, 0, 3);
73 static const struct reg_field stk3310_reg_field_ps_it
=
74 REG_FIELD(STK3310_REG_PSCTRL
, 0, 3);
75 static const struct reg_field stk3310_reg_field_int_ps
=
76 REG_FIELD(STK3310_REG_INT
, 0, 2);
77 static const struct reg_field stk3310_reg_field_flag_psint
=
78 REG_FIELD(STK3310_REG_FLAG
, 4, 4);
79 static const struct reg_field stk3310_reg_field_flag_nf
=
80 REG_FIELD(STK3310_REG_FLAG
, 0, 0);
82 /* Estimate maximum proximity values with regard to measurement scale. */
83 static const int stk3310_ps_max
[4] = {
84 STK3310_PS_MAX_VAL
/ 640,
85 STK3310_PS_MAX_VAL
/ 160,
86 STK3310_PS_MAX_VAL
/ 40,
87 STK3310_PS_MAX_VAL
/ 10
90 static const int stk3310_scale_table
[][2] = {
91 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
94 /* Integration time in seconds, microseconds */
95 static const int stk3310_it_table
[][2] = {
96 {0, 185}, {0, 370}, {0, 741}, {0, 1480},
97 {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
98 {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
99 {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
102 struct stk3310_data
{
103 struct i2c_client
*client
;
108 struct regmap
*regmap
;
109 struct regmap_field
*reg_state
;
110 struct regmap_field
*reg_als_gain
;
111 struct regmap_field
*reg_ps_gain
;
112 struct regmap_field
*reg_als_it
;
113 struct regmap_field
*reg_ps_it
;
114 struct regmap_field
*reg_int_ps
;
115 struct regmap_field
*reg_flag_psint
;
116 struct regmap_field
*reg_flag_nf
;
119 static const struct iio_event_spec stk3310_events
[] = {
120 /* Proximity event */
122 .type
= IIO_EV_TYPE_THRESH
,
123 .dir
= IIO_EV_DIR_RISING
,
124 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
125 BIT(IIO_EV_INFO_ENABLE
),
127 /* Out-of-proximity event */
129 .type
= IIO_EV_TYPE_THRESH
,
130 .dir
= IIO_EV_DIR_FALLING
,
131 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
132 BIT(IIO_EV_INFO_ENABLE
),
136 static const struct iio_chan_spec stk3310_channels
[] = {
139 .info_mask_separate
=
140 BIT(IIO_CHAN_INFO_RAW
) |
141 BIT(IIO_CHAN_INFO_SCALE
) |
142 BIT(IIO_CHAN_INFO_INT_TIME
),
145 .type
= IIO_PROXIMITY
,
146 .info_mask_separate
=
147 BIT(IIO_CHAN_INFO_RAW
) |
148 BIT(IIO_CHAN_INFO_SCALE
) |
149 BIT(IIO_CHAN_INFO_INT_TIME
),
150 .event_spec
= stk3310_events
,
151 .num_event_specs
= ARRAY_SIZE(stk3310_events
),
155 static IIO_CONST_ATTR(in_illuminance_scale_available
, STK3310_SCALE_AVAILABLE
);
157 static IIO_CONST_ATTR(in_proximity_scale_available
, STK3310_SCALE_AVAILABLE
);
159 static IIO_CONST_ATTR(in_illuminance_integration_time_available
,
160 STK3310_IT_AVAILABLE
);
162 static IIO_CONST_ATTR(in_proximity_integration_time_available
,
163 STK3310_IT_AVAILABLE
);
165 static struct attribute
*stk3310_attributes
[] = {
166 &iio_const_attr_in_illuminance_scale_available
.dev_attr
.attr
,
167 &iio_const_attr_in_proximity_scale_available
.dev_attr
.attr
,
168 &iio_const_attr_in_illuminance_integration_time_available
.dev_attr
.attr
,
169 &iio_const_attr_in_proximity_integration_time_available
.dev_attr
.attr
,
173 static const struct attribute_group stk3310_attribute_group
= {
174 .attrs
= stk3310_attributes
177 static int stk3310_get_index(const int table
[][2], int table_size
,
182 for (i
= 0; i
< table_size
; i
++) {
183 if (val
== table
[i
][0] && val2
== table
[i
][1])
190 static int stk3310_read_event(struct iio_dev
*indio_dev
,
191 const struct iio_chan_spec
*chan
,
192 enum iio_event_type type
,
193 enum iio_event_direction dir
,
194 enum iio_event_info info
,
200 struct stk3310_data
*data
= iio_priv(indio_dev
);
202 if (info
!= IIO_EV_INFO_VALUE
)
205 /* Only proximity interrupts are implemented at the moment. */
206 if (dir
== IIO_EV_DIR_RISING
)
207 reg
= STK3310_REG_THDH_PS
;
208 else if (dir
== IIO_EV_DIR_FALLING
)
209 reg
= STK3310_REG_THDL_PS
;
213 mutex_lock(&data
->lock
);
214 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
215 mutex_unlock(&data
->lock
);
217 dev_err(&data
->client
->dev
, "register read failed\n");
220 *val
= be16_to_cpu(buf
);
225 static int stk3310_write_event(struct iio_dev
*indio_dev
,
226 const struct iio_chan_spec
*chan
,
227 enum iio_event_type type
,
228 enum iio_event_direction dir
,
229 enum iio_event_info info
,
236 struct stk3310_data
*data
= iio_priv(indio_dev
);
237 struct i2c_client
*client
= data
->client
;
239 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
243 if (val
< 0 || val
> stk3310_ps_max
[index
])
246 if (dir
== IIO_EV_DIR_RISING
)
247 reg
= STK3310_REG_THDH_PS
;
248 else if (dir
== IIO_EV_DIR_FALLING
)
249 reg
= STK3310_REG_THDL_PS
;
253 buf
= cpu_to_be16(val
);
254 ret
= regmap_bulk_write(data
->regmap
, reg
, &buf
, 2);
256 dev_err(&client
->dev
, "failed to set PS threshold!\n");
261 static int stk3310_read_event_config(struct iio_dev
*indio_dev
,
262 const struct iio_chan_spec
*chan
,
263 enum iio_event_type type
,
264 enum iio_event_direction dir
)
266 unsigned int event_val
;
268 struct stk3310_data
*data
= iio_priv(indio_dev
);
270 ret
= regmap_field_read(data
->reg_int_ps
, &event_val
);
277 static int stk3310_write_event_config(struct iio_dev
*indio_dev
,
278 const struct iio_chan_spec
*chan
,
279 enum iio_event_type type
,
280 enum iio_event_direction dir
,
284 struct stk3310_data
*data
= iio_priv(indio_dev
);
285 struct i2c_client
*client
= data
->client
;
287 if (state
< 0 || state
> 7)
290 /* Set INT_PS value */
291 mutex_lock(&data
->lock
);
292 ret
= regmap_field_write(data
->reg_int_ps
, state
);
294 dev_err(&client
->dev
, "failed to set interrupt mode\n");
295 mutex_unlock(&data
->lock
);
300 static int stk3310_read_raw(struct iio_dev
*indio_dev
,
301 struct iio_chan_spec
const *chan
,
302 int *val
, int *val2
, long mask
)
308 struct stk3310_data
*data
= iio_priv(indio_dev
);
309 struct i2c_client
*client
= data
->client
;
311 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
315 case IIO_CHAN_INFO_RAW
:
316 if (chan
->type
== IIO_LIGHT
)
317 reg
= STK3310_REG_ALS_DATA_MSB
;
319 reg
= STK3310_REG_PS_DATA_MSB
;
321 mutex_lock(&data
->lock
);
322 ret
= regmap_bulk_read(data
->regmap
, reg
, &buf
, 2);
324 dev_err(&client
->dev
, "register read failed\n");
325 mutex_unlock(&data
->lock
);
328 *val
= be16_to_cpu(buf
);
329 mutex_unlock(&data
->lock
);
331 case IIO_CHAN_INFO_INT_TIME
:
332 if (chan
->type
== IIO_LIGHT
)
333 ret
= regmap_field_read(data
->reg_als_it
, &index
);
335 ret
= regmap_field_read(data
->reg_ps_it
, &index
);
339 *val
= stk3310_it_table
[index
][0];
340 *val2
= stk3310_it_table
[index
][1];
341 return IIO_VAL_INT_PLUS_MICRO
;
342 case IIO_CHAN_INFO_SCALE
:
343 if (chan
->type
== IIO_LIGHT
)
344 ret
= regmap_field_read(data
->reg_als_gain
, &index
);
346 ret
= regmap_field_read(data
->reg_ps_gain
, &index
);
350 *val
= stk3310_scale_table
[index
][0];
351 *val2
= stk3310_scale_table
[index
][1];
352 return IIO_VAL_INT_PLUS_MICRO
;
358 static int stk3310_write_raw(struct iio_dev
*indio_dev
,
359 struct iio_chan_spec
const *chan
,
360 int val
, int val2
, long mask
)
364 struct stk3310_data
*data
= iio_priv(indio_dev
);
366 if (chan
->type
!= IIO_LIGHT
&& chan
->type
!= IIO_PROXIMITY
)
370 case IIO_CHAN_INFO_INT_TIME
:
371 index
= stk3310_get_index(stk3310_it_table
,
372 ARRAY_SIZE(stk3310_it_table
),
376 mutex_lock(&data
->lock
);
377 if (chan
->type
== IIO_LIGHT
)
378 ret
= regmap_field_write(data
->reg_als_it
, index
);
380 ret
= regmap_field_write(data
->reg_ps_it
, index
);
382 dev_err(&data
->client
->dev
,
383 "sensor configuration failed\n");
384 mutex_unlock(&data
->lock
);
387 case IIO_CHAN_INFO_SCALE
:
388 index
= stk3310_get_index(stk3310_scale_table
,
389 ARRAY_SIZE(stk3310_scale_table
),
393 mutex_lock(&data
->lock
);
394 if (chan
->type
== IIO_LIGHT
)
395 ret
= regmap_field_write(data
->reg_als_gain
, index
);
397 ret
= regmap_field_write(data
->reg_ps_gain
, index
);
399 dev_err(&data
->client
->dev
,
400 "sensor configuration failed\n");
401 mutex_unlock(&data
->lock
);
408 static const struct iio_info stk3310_info
= {
409 .read_raw
= stk3310_read_raw
,
410 .write_raw
= stk3310_write_raw
,
411 .attrs
= &stk3310_attribute_group
,
412 .read_event_value
= stk3310_read_event
,
413 .write_event_value
= stk3310_write_event
,
414 .read_event_config
= stk3310_read_event_config
,
415 .write_event_config
= stk3310_write_event_config
,
418 static int stk3310_set_state(struct stk3310_data
*data
, u8 state
)
421 struct i2c_client
*client
= data
->client
;
423 /* 3-bit state; 0b100 is not supported. */
424 if (state
> 7 || state
== 4)
427 mutex_lock(&data
->lock
);
428 ret
= regmap_field_write(data
->reg_state
, state
);
430 dev_err(&client
->dev
, "failed to change sensor state\n");
431 } else if (state
!= STK3310_STATE_STANDBY
) {
432 /* Don't reset the 'enabled' flags if we're going in standby */
433 data
->ps_enabled
= !!(state
& STK3310_STATE_EN_PS
);
434 data
->als_enabled
= !!(state
& STK3310_STATE_EN_ALS
);
436 mutex_unlock(&data
->lock
);
441 static int stk3310_init(struct iio_dev
*indio_dev
)
446 struct stk3310_data
*data
= iio_priv(indio_dev
);
447 struct i2c_client
*client
= data
->client
;
449 ret
= regmap_read(data
->regmap
, STK3310_REG_ID
, &chipid
);
453 if (chipid
!= STK3310_CHIP_ID_VAL
&&
454 chipid
!= STK3311_CHIP_ID_VAL
) {
455 dev_err(&client
->dev
, "invalid chip id: 0x%x\n", chipid
);
459 state
= STK3310_STATE_EN_ALS
| STK3310_STATE_EN_PS
;
460 ret
= stk3310_set_state(data
, state
);
462 dev_err(&client
->dev
, "failed to enable sensor");
466 /* Enable PS interrupts */
467 ret
= regmap_field_write(data
->reg_int_ps
, STK3310_PSINT_EN
);
469 dev_err(&client
->dev
, "failed to enable interrupts!\n");
474 static bool stk3310_is_volatile_reg(struct device
*dev
, unsigned int reg
)
477 case STK3310_REG_ALS_DATA_MSB
:
478 case STK3310_REG_ALS_DATA_LSB
:
479 case STK3310_REG_PS_DATA_LSB
:
480 case STK3310_REG_PS_DATA_MSB
:
481 case STK3310_REG_FLAG
:
488 static struct regmap_config stk3310_regmap_config
= {
489 .name
= STK3310_REGMAP_NAME
,
492 .max_register
= STK3310_MAX_REG
,
493 .cache_type
= REGCACHE_RBTREE
,
494 .volatile_reg
= stk3310_is_volatile_reg
,
497 static int stk3310_regmap_init(struct stk3310_data
*data
)
499 struct regmap
*regmap
;
500 struct i2c_client
*client
;
502 client
= data
->client
;
503 regmap
= devm_regmap_init_i2c(client
, &stk3310_regmap_config
);
504 if (IS_ERR(regmap
)) {
505 dev_err(&client
->dev
, "regmap initialization failed.\n");
506 return PTR_ERR(regmap
);
508 data
->regmap
= regmap
;
510 STK3310_REGFIELD(state
);
511 STK3310_REGFIELD(als_gain
);
512 STK3310_REGFIELD(ps_gain
);
513 STK3310_REGFIELD(als_it
);
514 STK3310_REGFIELD(ps_it
);
515 STK3310_REGFIELD(int_ps
);
516 STK3310_REGFIELD(flag_psint
);
517 STK3310_REGFIELD(flag_nf
);
522 static irqreturn_t
stk3310_irq_handler(int irq
, void *private)
524 struct iio_dev
*indio_dev
= private;
525 struct stk3310_data
*data
= iio_priv(indio_dev
);
527 data
->timestamp
= iio_get_time_ns(indio_dev
);
529 return IRQ_WAKE_THREAD
;
532 static irqreturn_t
stk3310_irq_event_handler(int irq
, void *private)
538 struct iio_dev
*indio_dev
= private;
539 struct stk3310_data
*data
= iio_priv(indio_dev
);
541 /* Read FLAG_NF to figure out what threshold has been met. */
542 mutex_lock(&data
->lock
);
543 ret
= regmap_field_read(data
->reg_flag_nf
, &dir
);
545 dev_err(&data
->client
->dev
, "register read failed\n");
546 mutex_unlock(&data
->lock
);
549 event
= IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY
, 1,
551 (dir
? IIO_EV_DIR_FALLING
:
553 iio_push_event(indio_dev
, event
, data
->timestamp
);
555 /* Reset the interrupt flag */
556 ret
= regmap_field_write(data
->reg_flag_psint
, 0);
558 dev_err(&data
->client
->dev
, "failed to reset interrupts\n");
559 mutex_unlock(&data
->lock
);
564 static int stk3310_probe(struct i2c_client
*client
,
565 const struct i2c_device_id
*id
)
568 struct iio_dev
*indio_dev
;
569 struct stk3310_data
*data
;
571 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
573 dev_err(&client
->dev
, "iio allocation failed!\n");
577 data
= iio_priv(indio_dev
);
578 data
->client
= client
;
579 i2c_set_clientdata(client
, indio_dev
);
580 mutex_init(&data
->lock
);
582 ret
= stk3310_regmap_init(data
);
586 indio_dev
->dev
.parent
= &client
->dev
;
587 indio_dev
->info
= &stk3310_info
;
588 indio_dev
->name
= STK3310_DRIVER_NAME
;
589 indio_dev
->modes
= INDIO_DIRECT_MODE
;
590 indio_dev
->channels
= stk3310_channels
;
591 indio_dev
->num_channels
= ARRAY_SIZE(stk3310_channels
);
593 ret
= stk3310_init(indio_dev
);
597 if (client
->irq
> 0) {
598 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
600 stk3310_irq_event_handler
,
601 IRQF_TRIGGER_FALLING
|
603 STK3310_EVENT
, indio_dev
);
605 dev_err(&client
->dev
, "request irq %d failed\n",
611 ret
= iio_device_register(indio_dev
);
613 dev_err(&client
->dev
, "device_register failed\n");
620 stk3310_set_state(data
, STK3310_STATE_STANDBY
);
624 static int stk3310_remove(struct i2c_client
*client
)
626 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
628 iio_device_unregister(indio_dev
);
629 return stk3310_set_state(iio_priv(indio_dev
), STK3310_STATE_STANDBY
);
632 #ifdef CONFIG_PM_SLEEP
633 static int stk3310_suspend(struct device
*dev
)
635 struct stk3310_data
*data
;
637 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
639 return stk3310_set_state(data
, STK3310_STATE_STANDBY
);
642 static int stk3310_resume(struct device
*dev
)
645 struct stk3310_data
*data
;
647 data
= iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
648 if (data
->ps_enabled
)
649 state
|= STK3310_STATE_EN_PS
;
650 if (data
->als_enabled
)
651 state
|= STK3310_STATE_EN_ALS
;
653 return stk3310_set_state(data
, state
);
656 static SIMPLE_DEV_PM_OPS(stk3310_pm_ops
, stk3310_suspend
, stk3310_resume
);
658 #define STK3310_PM_OPS (&stk3310_pm_ops)
660 #define STK3310_PM_OPS NULL
663 static const struct i2c_device_id stk3310_i2c_id
[] = {
668 MODULE_DEVICE_TABLE(i2c
, stk3310_i2c_id
);
670 static const struct acpi_device_id stk3310_acpi_id
[] = {
676 MODULE_DEVICE_TABLE(acpi
, stk3310_acpi_id
);
678 static struct i2c_driver stk3310_driver
= {
681 .pm
= STK3310_PM_OPS
,
682 .acpi_match_table
= ACPI_PTR(stk3310_acpi_id
),
684 .probe
= stk3310_probe
,
685 .remove
= stk3310_remove
,
686 .id_table
= stk3310_i2c_id
,
689 module_i2c_driver(stk3310_driver
);
691 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
692 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
693 MODULE_LICENSE("GPL v2");