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;
109 uint32_t ps_near_level;
111 struct regmap *regmap;
112 struct regmap_field *reg_state;
113 struct regmap_field *reg_als_gain;
114 struct regmap_field *reg_ps_gain;
115 struct regmap_field *reg_als_it;
116 struct regmap_field *reg_ps_it;
117 struct regmap_field *reg_int_ps;
118 struct regmap_field *reg_flag_psint;
119 struct regmap_field *reg_flag_nf;
122 static const struct iio_event_spec stk3310_events[] = {
123 /* Proximity event */
125 .type = IIO_EV_TYPE_THRESH,
126 .dir = IIO_EV_DIR_RISING,
127 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
128 BIT(IIO_EV_INFO_ENABLE),
130 /* Out-of-proximity event */
132 .type = IIO_EV_TYPE_THRESH,
133 .dir = IIO_EV_DIR_FALLING,
134 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
135 BIT(IIO_EV_INFO_ENABLE),
139 static ssize_t stk3310_read_near_level(struct iio_dev *indio_dev,
141 const struct iio_chan_spec *chan,
144 struct stk3310_data *data = iio_priv(indio_dev);
146 return sprintf(buf, "%u\n", data->ps_near_level);
149 static const struct iio_chan_spec_ext_info stk3310_ext_info[] = {
152 .shared = IIO_SEPARATE,
153 .read = stk3310_read_near_level,
158 static const struct iio_chan_spec stk3310_channels[] = {
161 .info_mask_separate =
162 BIT(IIO_CHAN_INFO_RAW) |
163 BIT(IIO_CHAN_INFO_SCALE) |
164 BIT(IIO_CHAN_INFO_INT_TIME),
167 .type = IIO_PROXIMITY,
168 .info_mask_separate =
169 BIT(IIO_CHAN_INFO_RAW) |
170 BIT(IIO_CHAN_INFO_SCALE) |
171 BIT(IIO_CHAN_INFO_INT_TIME),
172 .event_spec = stk3310_events,
173 .num_event_specs = ARRAY_SIZE(stk3310_events),
174 .ext_info = stk3310_ext_info,
178 static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
180 static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
182 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
183 STK3310_IT_AVAILABLE);
185 static IIO_CONST_ATTR(in_proximity_integration_time_available,
186 STK3310_IT_AVAILABLE);
188 static struct attribute *stk3310_attributes[] = {
189 &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
190 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
191 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
192 &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
196 static const struct attribute_group stk3310_attribute_group = {
197 .attrs = stk3310_attributes
200 static int stk3310_get_index(const int table[][2], int table_size,
205 for (i = 0; i < table_size; i++) {
206 if (val == table[i][0] && val2 == table[i][1])
213 static int stk3310_read_event(struct iio_dev *indio_dev,
214 const struct iio_chan_spec *chan,
215 enum iio_event_type type,
216 enum iio_event_direction dir,
217 enum iio_event_info info,
223 struct stk3310_data *data = iio_priv(indio_dev);
225 if (info != IIO_EV_INFO_VALUE)
228 /* Only proximity interrupts are implemented at the moment. */
229 if (dir == IIO_EV_DIR_RISING)
230 reg = STK3310_REG_THDH_PS;
231 else if (dir == IIO_EV_DIR_FALLING)
232 reg = STK3310_REG_THDL_PS;
236 mutex_lock(&data->lock);
237 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
238 mutex_unlock(&data->lock);
240 dev_err(&data->client->dev, "register read failed\n");
243 *val = be16_to_cpu(buf);
248 static int stk3310_write_event(struct iio_dev *indio_dev,
249 const struct iio_chan_spec *chan,
250 enum iio_event_type type,
251 enum iio_event_direction dir,
252 enum iio_event_info info,
259 struct stk3310_data *data = iio_priv(indio_dev);
260 struct i2c_client *client = data->client;
262 ret = regmap_field_read(data->reg_ps_gain, &index);
266 if (val < 0 || val > stk3310_ps_max[index])
269 if (dir == IIO_EV_DIR_RISING)
270 reg = STK3310_REG_THDH_PS;
271 else if (dir == IIO_EV_DIR_FALLING)
272 reg = STK3310_REG_THDL_PS;
276 buf = cpu_to_be16(val);
277 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
279 dev_err(&client->dev, "failed to set PS threshold!\n");
284 static int stk3310_read_event_config(struct iio_dev *indio_dev,
285 const struct iio_chan_spec *chan,
286 enum iio_event_type type,
287 enum iio_event_direction dir)
289 unsigned int event_val;
291 struct stk3310_data *data = iio_priv(indio_dev);
293 ret = regmap_field_read(data->reg_int_ps, &event_val);
300 static int stk3310_write_event_config(struct iio_dev *indio_dev,
301 const struct iio_chan_spec *chan,
302 enum iio_event_type type,
303 enum iio_event_direction dir,
307 struct stk3310_data *data = iio_priv(indio_dev);
308 struct i2c_client *client = data->client;
310 if (state < 0 || state > 7)
313 /* Set INT_PS value */
314 mutex_lock(&data->lock);
315 ret = regmap_field_write(data->reg_int_ps, state);
317 dev_err(&client->dev, "failed to set interrupt mode\n");
318 mutex_unlock(&data->lock);
323 static int stk3310_read_raw(struct iio_dev *indio_dev,
324 struct iio_chan_spec const *chan,
325 int *val, int *val2, long mask)
331 struct stk3310_data *data = iio_priv(indio_dev);
332 struct i2c_client *client = data->client;
334 if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
338 case IIO_CHAN_INFO_RAW:
339 if (chan->type == IIO_LIGHT)
340 reg = STK3310_REG_ALS_DATA_MSB;
342 reg = STK3310_REG_PS_DATA_MSB;
344 mutex_lock(&data->lock);
345 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
347 dev_err(&client->dev, "register read failed\n");
348 mutex_unlock(&data->lock);
351 *val = be16_to_cpu(buf);
352 mutex_unlock(&data->lock);
354 case IIO_CHAN_INFO_INT_TIME:
355 if (chan->type == IIO_LIGHT)
356 ret = regmap_field_read(data->reg_als_it, &index);
358 ret = regmap_field_read(data->reg_ps_it, &index);
362 *val = stk3310_it_table[index][0];
363 *val2 = stk3310_it_table[index][1];
364 return IIO_VAL_INT_PLUS_MICRO;
365 case IIO_CHAN_INFO_SCALE:
366 if (chan->type == IIO_LIGHT)
367 ret = regmap_field_read(data->reg_als_gain, &index);
369 ret = regmap_field_read(data->reg_ps_gain, &index);
373 *val = stk3310_scale_table[index][0];
374 *val2 = stk3310_scale_table[index][1];
375 return IIO_VAL_INT_PLUS_MICRO;
381 static int stk3310_write_raw(struct iio_dev *indio_dev,
382 struct iio_chan_spec const *chan,
383 int val, int val2, long mask)
387 struct stk3310_data *data = iio_priv(indio_dev);
389 if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
393 case IIO_CHAN_INFO_INT_TIME:
394 index = stk3310_get_index(stk3310_it_table,
395 ARRAY_SIZE(stk3310_it_table),
399 mutex_lock(&data->lock);
400 if (chan->type == IIO_LIGHT)
401 ret = regmap_field_write(data->reg_als_it, index);
403 ret = regmap_field_write(data->reg_ps_it, index);
405 dev_err(&data->client->dev,
406 "sensor configuration failed\n");
407 mutex_unlock(&data->lock);
410 case IIO_CHAN_INFO_SCALE:
411 index = stk3310_get_index(stk3310_scale_table,
412 ARRAY_SIZE(stk3310_scale_table),
416 mutex_lock(&data->lock);
417 if (chan->type == IIO_LIGHT)
418 ret = regmap_field_write(data->reg_als_gain, index);
420 ret = regmap_field_write(data->reg_ps_gain, index);
422 dev_err(&data->client->dev,
423 "sensor configuration failed\n");
424 mutex_unlock(&data->lock);
431 static const struct iio_info stk3310_info = {
432 .read_raw = stk3310_read_raw,
433 .write_raw = stk3310_write_raw,
434 .attrs = &stk3310_attribute_group,
435 .read_event_value = stk3310_read_event,
436 .write_event_value = stk3310_write_event,
437 .read_event_config = stk3310_read_event_config,
438 .write_event_config = stk3310_write_event_config,
441 static int stk3310_set_state(struct stk3310_data *data, u8 state)
444 struct i2c_client *client = data->client;
446 /* 3-bit state; 0b100 is not supported. */
447 if (state > 7 || state == 4)
450 mutex_lock(&data->lock);
451 ret = regmap_field_write(data->reg_state, state);
453 dev_err(&client->dev, "failed to change sensor state\n");
454 } else if (state != STK3310_STATE_STANDBY) {
455 /* Don't reset the 'enabled' flags if we're going in standby */
456 data->ps_enabled = !!(state & STK3310_STATE_EN_PS);
457 data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
459 mutex_unlock(&data->lock);
464 static int stk3310_init(struct iio_dev *indio_dev)
469 struct stk3310_data *data = iio_priv(indio_dev);
470 struct i2c_client *client = data->client;
472 ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
476 if (chipid != STK3310_CHIP_ID_VAL &&
477 chipid != STK3311_CHIP_ID_VAL &&
478 chipid != STK3311X_CHIP_ID_VAL &&
479 chipid != STK3335_CHIP_ID_VAL) {
480 dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid);
484 state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
485 ret = stk3310_set_state(data, state);
487 dev_err(&client->dev, "failed to enable sensor");
491 /* Enable PS interrupts */
492 ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
494 dev_err(&client->dev, "failed to enable interrupts!\n");
499 static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
502 case STK3310_REG_ALS_DATA_MSB:
503 case STK3310_REG_ALS_DATA_LSB:
504 case STK3310_REG_PS_DATA_LSB:
505 case STK3310_REG_PS_DATA_MSB:
506 case STK3310_REG_FLAG:
513 static const struct regmap_config stk3310_regmap_config = {
514 .name = STK3310_REGMAP_NAME,
517 .max_register = STK3310_MAX_REG,
518 .cache_type = REGCACHE_RBTREE,
519 .volatile_reg = stk3310_is_volatile_reg,
522 static int stk3310_regmap_init(struct stk3310_data *data)
524 struct regmap *regmap;
525 struct i2c_client *client;
527 client = data->client;
528 regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
529 if (IS_ERR(regmap)) {
530 dev_err(&client->dev, "regmap initialization failed.\n");
531 return PTR_ERR(regmap);
533 data->regmap = regmap;
535 STK3310_REGFIELD(state);
536 STK3310_REGFIELD(als_gain);
537 STK3310_REGFIELD(ps_gain);
538 STK3310_REGFIELD(als_it);
539 STK3310_REGFIELD(ps_it);
540 STK3310_REGFIELD(int_ps);
541 STK3310_REGFIELD(flag_psint);
542 STK3310_REGFIELD(flag_nf);
547 static irqreturn_t stk3310_irq_handler(int irq, void *private)
549 struct iio_dev *indio_dev = private;
550 struct stk3310_data *data = iio_priv(indio_dev);
552 data->timestamp = iio_get_time_ns(indio_dev);
554 return IRQ_WAKE_THREAD;
557 static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
563 struct iio_dev *indio_dev = private;
564 struct stk3310_data *data = iio_priv(indio_dev);
566 /* Read FLAG_NF to figure out what threshold has been met. */
567 mutex_lock(&data->lock);
568 ret = regmap_field_read(data->reg_flag_nf, &dir);
570 dev_err(&data->client->dev, "register read failed: %d\n", ret);
573 event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
575 (dir ? IIO_EV_DIR_FALLING :
577 iio_push_event(indio_dev, event, data->timestamp);
579 /* Reset the interrupt flag */
580 ret = regmap_field_write(data->reg_flag_psint, 0);
582 dev_err(&data->client->dev, "failed to reset interrupts\n");
584 mutex_unlock(&data->lock);
589 static int stk3310_probe(struct i2c_client *client)
592 struct iio_dev *indio_dev;
593 struct stk3310_data *data;
595 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
597 dev_err(&client->dev, "iio allocation failed!\n");
601 data = iio_priv(indio_dev);
602 data->client = client;
603 i2c_set_clientdata(client, indio_dev);
605 device_property_read_u32(&client->dev, "proximity-near-level",
606 &data->ps_near_level);
608 mutex_init(&data->lock);
610 ret = stk3310_regmap_init(data);
614 indio_dev->info = &stk3310_info;
615 indio_dev->name = STK3310_DRIVER_NAME;
616 indio_dev->modes = INDIO_DIRECT_MODE;
617 indio_dev->channels = stk3310_channels;
618 indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
620 ret = stk3310_init(indio_dev);
624 if (client->irq > 0) {
625 ret = devm_request_threaded_irq(&client->dev, client->irq,
627 stk3310_irq_event_handler,
628 IRQF_TRIGGER_FALLING |
630 STK3310_EVENT, indio_dev);
632 dev_err(&client->dev, "request irq %d failed\n",
638 ret = iio_device_register(indio_dev);
640 dev_err(&client->dev, "device_register failed\n");
647 stk3310_set_state(data, STK3310_STATE_STANDBY);
651 static void stk3310_remove(struct i2c_client *client)
653 struct iio_dev *indio_dev = i2c_get_clientdata(client);
655 iio_device_unregister(indio_dev);
656 stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
659 static int stk3310_suspend(struct device *dev)
661 struct stk3310_data *data;
663 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
665 return stk3310_set_state(data, STK3310_STATE_STANDBY);
668 static int stk3310_resume(struct device *dev)
671 struct stk3310_data *data;
673 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
674 if (data->ps_enabled)
675 state |= STK3310_STATE_EN_PS;
676 if (data->als_enabled)
677 state |= STK3310_STATE_EN_ALS;
679 return stk3310_set_state(data, state);
682 static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
685 static const struct i2c_device_id stk3310_i2c_id[] = {
691 MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id);
693 static const struct acpi_device_id stk3310_acpi_id[] = {
700 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
702 static const struct of_device_id stk3310_of_match[] = {
703 { .compatible = "sensortek,stk3310", },
704 { .compatible = "sensortek,stk3311", },
705 { .compatible = "sensortek,stk3335", },
708 MODULE_DEVICE_TABLE(of, stk3310_of_match);
710 static struct i2c_driver stk3310_driver = {
713 .of_match_table = stk3310_of_match,
714 .pm = pm_sleep_ptr(&stk3310_pm_ops),
715 .acpi_match_table = ACPI_PTR(stk3310_acpi_id),
717 .probe = stk3310_probe,
718 .remove = stk3310_remove,
719 .id_table = stk3310_i2c_id,
722 module_i2c_driver(stk3310_driver);
725 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
726 MODULE_LICENSE("GPL v2");