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 STK3310_CHIP_ID_VAL 0x13
39 #define STK3311_CHIP_ID_VAL 0x1D
40 #define STK3311A_CHIP_ID_VAL 0x15
41 #define STK3311S34_CHIP_ID_VAL 0x1E
42 #define STK3311X_CHIP_ID_VAL 0x12
43 #define STK3335_CHIP_ID_VAL 0x51
44 #define STK3310_PSINT_EN 0x01
45 #define STK3310_PS_MAX_VAL 0xFFFF
47 #define STK3310_DRIVER_NAME "stk3310"
48 #define STK3310_REGMAP_NAME "stk3310_regmap"
49 #define STK3310_EVENT "stk3310_event"
51 #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
53 #define STK3310_IT_AVAILABLE \
54 "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
55 "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
58 #define STK3310_REGFIELD(name) \
61 devm_regmap_field_alloc(&client->dev, regmap, \
62 stk3310_reg_field_##name); \
63 if (IS_ERR(data->reg_##name)) { \
64 dev_err(&client->dev, "reg field alloc failed.\n"); \
65 return PTR_ERR(data->reg_##name); \
69 static const struct reg_field stk3310_reg_field_state =
70 REG_FIELD(STK3310_REG_STATE, 0, 2);
71 static const struct reg_field stk3310_reg_field_als_gain =
72 REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
73 static const struct reg_field stk3310_reg_field_ps_gain =
74 REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
75 static const struct reg_field stk3310_reg_field_als_it =
76 REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
77 static const struct reg_field stk3310_reg_field_ps_it =
78 REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
79 static const struct reg_field stk3310_reg_field_int_ps =
80 REG_FIELD(STK3310_REG_INT, 0, 2);
81 static const struct reg_field stk3310_reg_field_flag_psint =
82 REG_FIELD(STK3310_REG_FLAG, 4, 4);
83 static const struct reg_field stk3310_reg_field_flag_nf =
84 REG_FIELD(STK3310_REG_FLAG, 0, 0);
86 static const u8 stk3310_chip_ids[] = {
89 STK3311S34_CHIP_ID_VAL,
95 /* Estimate maximum proximity values with regard to measurement scale. */
96 static const int stk3310_ps_max[4] = {
97 STK3310_PS_MAX_VAL / 640,
98 STK3310_PS_MAX_VAL / 160,
99 STK3310_PS_MAX_VAL / 40,
100 STK3310_PS_MAX_VAL / 10
103 static const int stk3310_scale_table[][2] = {
104 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
107 /* Integration time in seconds, microseconds */
108 static const int stk3310_it_table[][2] = {
109 {0, 185}, {0, 370}, {0, 741}, {0, 1480},
110 {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
111 {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
112 {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
115 struct stk3310_data {
116 struct i2c_client *client;
120 uint32_t ps_near_level;
122 struct regmap *regmap;
123 struct regmap_field *reg_state;
124 struct regmap_field *reg_als_gain;
125 struct regmap_field *reg_ps_gain;
126 struct regmap_field *reg_als_it;
127 struct regmap_field *reg_ps_it;
128 struct regmap_field *reg_int_ps;
129 struct regmap_field *reg_flag_psint;
130 struct regmap_field *reg_flag_nf;
133 static const struct iio_event_spec stk3310_events[] = {
134 /* Proximity event */
136 .type = IIO_EV_TYPE_THRESH,
137 .dir = IIO_EV_DIR_RISING,
138 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
139 BIT(IIO_EV_INFO_ENABLE),
141 /* Out-of-proximity event */
143 .type = IIO_EV_TYPE_THRESH,
144 .dir = IIO_EV_DIR_FALLING,
145 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
146 BIT(IIO_EV_INFO_ENABLE),
150 static ssize_t stk3310_read_near_level(struct iio_dev *indio_dev,
152 const struct iio_chan_spec *chan,
155 struct stk3310_data *data = iio_priv(indio_dev);
157 return sprintf(buf, "%u\n", data->ps_near_level);
160 static const struct iio_chan_spec_ext_info stk3310_ext_info[] = {
163 .shared = IIO_SEPARATE,
164 .read = stk3310_read_near_level,
169 static const struct iio_chan_spec stk3310_channels[] = {
172 .info_mask_separate =
173 BIT(IIO_CHAN_INFO_RAW) |
174 BIT(IIO_CHAN_INFO_SCALE) |
175 BIT(IIO_CHAN_INFO_INT_TIME),
178 .type = IIO_PROXIMITY,
179 .info_mask_separate =
180 BIT(IIO_CHAN_INFO_RAW) |
181 BIT(IIO_CHAN_INFO_SCALE) |
182 BIT(IIO_CHAN_INFO_INT_TIME),
183 .event_spec = stk3310_events,
184 .num_event_specs = ARRAY_SIZE(stk3310_events),
185 .ext_info = stk3310_ext_info,
189 static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
191 static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
193 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
194 STK3310_IT_AVAILABLE);
196 static IIO_CONST_ATTR(in_proximity_integration_time_available,
197 STK3310_IT_AVAILABLE);
199 static struct attribute *stk3310_attributes[] = {
200 &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
201 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
202 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
203 &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
207 static const struct attribute_group stk3310_attribute_group = {
208 .attrs = stk3310_attributes
211 static int stk3310_check_chip_id(const u8 chip_id)
213 for (int i = 0; i < ARRAY_SIZE(stk3310_chip_ids); i++) {
214 if (chip_id == stk3310_chip_ids[i])
221 static int stk3310_get_index(const int table[][2], int table_size,
226 for (i = 0; i < table_size; i++) {
227 if (val == table[i][0] && val2 == table[i][1])
234 static int stk3310_read_event(struct iio_dev *indio_dev,
235 const struct iio_chan_spec *chan,
236 enum iio_event_type type,
237 enum iio_event_direction dir,
238 enum iio_event_info info,
244 struct stk3310_data *data = iio_priv(indio_dev);
246 if (info != IIO_EV_INFO_VALUE)
249 /* Only proximity interrupts are implemented at the moment. */
250 if (dir == IIO_EV_DIR_RISING)
251 reg = STK3310_REG_THDH_PS;
252 else if (dir == IIO_EV_DIR_FALLING)
253 reg = STK3310_REG_THDL_PS;
257 mutex_lock(&data->lock);
258 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
259 mutex_unlock(&data->lock);
261 dev_err(&data->client->dev, "register read failed\n");
264 *val = be16_to_cpu(buf);
269 static int stk3310_write_event(struct iio_dev *indio_dev,
270 const struct iio_chan_spec *chan,
271 enum iio_event_type type,
272 enum iio_event_direction dir,
273 enum iio_event_info info,
280 struct stk3310_data *data = iio_priv(indio_dev);
281 struct i2c_client *client = data->client;
283 ret = regmap_field_read(data->reg_ps_gain, &index);
287 if (val < 0 || val > stk3310_ps_max[index])
290 if (dir == IIO_EV_DIR_RISING)
291 reg = STK3310_REG_THDH_PS;
292 else if (dir == IIO_EV_DIR_FALLING)
293 reg = STK3310_REG_THDL_PS;
297 buf = cpu_to_be16(val);
298 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
300 dev_err(&client->dev, "failed to set PS threshold!\n");
305 static int stk3310_read_event_config(struct iio_dev *indio_dev,
306 const struct iio_chan_spec *chan,
307 enum iio_event_type type,
308 enum iio_event_direction dir)
310 unsigned int event_val;
312 struct stk3310_data *data = iio_priv(indio_dev);
314 ret = regmap_field_read(data->reg_int_ps, &event_val);
321 static int stk3310_write_event_config(struct iio_dev *indio_dev,
322 const struct iio_chan_spec *chan,
323 enum iio_event_type type,
324 enum iio_event_direction dir,
328 struct stk3310_data *data = iio_priv(indio_dev);
329 struct i2c_client *client = data->client;
331 if (state < 0 || state > 7)
334 /* Set INT_PS value */
335 mutex_lock(&data->lock);
336 ret = regmap_field_write(data->reg_int_ps, state);
338 dev_err(&client->dev, "failed to set interrupt mode\n");
339 mutex_unlock(&data->lock);
344 static int stk3310_read_raw(struct iio_dev *indio_dev,
345 struct iio_chan_spec const *chan,
346 int *val, int *val2, long mask)
352 struct stk3310_data *data = iio_priv(indio_dev);
353 struct i2c_client *client = data->client;
355 if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
359 case IIO_CHAN_INFO_RAW:
360 if (chan->type == IIO_LIGHT)
361 reg = STK3310_REG_ALS_DATA_MSB;
363 reg = STK3310_REG_PS_DATA_MSB;
365 mutex_lock(&data->lock);
366 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
368 dev_err(&client->dev, "register read failed\n");
369 mutex_unlock(&data->lock);
372 *val = be16_to_cpu(buf);
373 mutex_unlock(&data->lock);
375 case IIO_CHAN_INFO_INT_TIME:
376 if (chan->type == IIO_LIGHT)
377 ret = regmap_field_read(data->reg_als_it, &index);
379 ret = regmap_field_read(data->reg_ps_it, &index);
383 *val = stk3310_it_table[index][0];
384 *val2 = stk3310_it_table[index][1];
385 return IIO_VAL_INT_PLUS_MICRO;
386 case IIO_CHAN_INFO_SCALE:
387 if (chan->type == IIO_LIGHT)
388 ret = regmap_field_read(data->reg_als_gain, &index);
390 ret = regmap_field_read(data->reg_ps_gain, &index);
394 *val = stk3310_scale_table[index][0];
395 *val2 = stk3310_scale_table[index][1];
396 return IIO_VAL_INT_PLUS_MICRO;
402 static int stk3310_write_raw(struct iio_dev *indio_dev,
403 struct iio_chan_spec const *chan,
404 int val, int val2, long mask)
408 struct stk3310_data *data = iio_priv(indio_dev);
410 if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY)
414 case IIO_CHAN_INFO_INT_TIME:
415 index = stk3310_get_index(stk3310_it_table,
416 ARRAY_SIZE(stk3310_it_table),
420 mutex_lock(&data->lock);
421 if (chan->type == IIO_LIGHT)
422 ret = regmap_field_write(data->reg_als_it, index);
424 ret = regmap_field_write(data->reg_ps_it, index);
426 dev_err(&data->client->dev,
427 "sensor configuration failed\n");
428 mutex_unlock(&data->lock);
431 case IIO_CHAN_INFO_SCALE:
432 index = stk3310_get_index(stk3310_scale_table,
433 ARRAY_SIZE(stk3310_scale_table),
437 mutex_lock(&data->lock);
438 if (chan->type == IIO_LIGHT)
439 ret = regmap_field_write(data->reg_als_gain, index);
441 ret = regmap_field_write(data->reg_ps_gain, index);
443 dev_err(&data->client->dev,
444 "sensor configuration failed\n");
445 mutex_unlock(&data->lock);
452 static const struct iio_info stk3310_info = {
453 .read_raw = stk3310_read_raw,
454 .write_raw = stk3310_write_raw,
455 .attrs = &stk3310_attribute_group,
456 .read_event_value = stk3310_read_event,
457 .write_event_value = stk3310_write_event,
458 .read_event_config = stk3310_read_event_config,
459 .write_event_config = stk3310_write_event_config,
462 static int stk3310_set_state(struct stk3310_data *data, u8 state)
465 struct i2c_client *client = data->client;
467 /* 3-bit state; 0b100 is not supported. */
468 if (state > 7 || state == 4)
471 mutex_lock(&data->lock);
472 ret = regmap_field_write(data->reg_state, state);
474 dev_err(&client->dev, "failed to change sensor state\n");
475 } else if (state != STK3310_STATE_STANDBY) {
476 /* Don't reset the 'enabled' flags if we're going in standby */
477 data->ps_enabled = !!(state & STK3310_STATE_EN_PS);
478 data->als_enabled = !!(state & STK3310_STATE_EN_ALS);
480 mutex_unlock(&data->lock);
485 static int stk3310_init(struct iio_dev *indio_dev)
490 struct stk3310_data *data = iio_priv(indio_dev);
491 struct i2c_client *client = data->client;
493 ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid);
497 ret = stk3310_check_chip_id(chipid);
499 dev_warn(&client->dev, "unknown chip id: 0x%x\n", chipid);
501 state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
502 ret = stk3310_set_state(data, state);
504 dev_err(&client->dev, "failed to enable sensor");
508 /* Enable PS interrupts */
509 ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
511 dev_err(&client->dev, "failed to enable interrupts!\n");
516 static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
519 case STK3310_REG_ALS_DATA_MSB:
520 case STK3310_REG_ALS_DATA_LSB:
521 case STK3310_REG_PS_DATA_LSB:
522 case STK3310_REG_PS_DATA_MSB:
523 case STK3310_REG_FLAG:
530 static const struct regmap_config stk3310_regmap_config = {
531 .name = STK3310_REGMAP_NAME,
534 .max_register = STK3310_MAX_REG,
535 .cache_type = REGCACHE_RBTREE,
536 .volatile_reg = stk3310_is_volatile_reg,
539 static int stk3310_regmap_init(struct stk3310_data *data)
541 struct regmap *regmap;
542 struct i2c_client *client;
544 client = data->client;
545 regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
546 if (IS_ERR(regmap)) {
547 dev_err(&client->dev, "regmap initialization failed.\n");
548 return PTR_ERR(regmap);
550 data->regmap = regmap;
552 STK3310_REGFIELD(state);
553 STK3310_REGFIELD(als_gain);
554 STK3310_REGFIELD(ps_gain);
555 STK3310_REGFIELD(als_it);
556 STK3310_REGFIELD(ps_it);
557 STK3310_REGFIELD(int_ps);
558 STK3310_REGFIELD(flag_psint);
559 STK3310_REGFIELD(flag_nf);
564 static irqreturn_t stk3310_irq_handler(int irq, void *private)
566 struct iio_dev *indio_dev = private;
567 struct stk3310_data *data = iio_priv(indio_dev);
569 data->timestamp = iio_get_time_ns(indio_dev);
571 return IRQ_WAKE_THREAD;
574 static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
580 struct iio_dev *indio_dev = private;
581 struct stk3310_data *data = iio_priv(indio_dev);
583 /* Read FLAG_NF to figure out what threshold has been met. */
584 mutex_lock(&data->lock);
585 ret = regmap_field_read(data->reg_flag_nf, &dir);
587 dev_err(&data->client->dev, "register read failed: %d\n", ret);
590 event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
592 (dir ? IIO_EV_DIR_FALLING :
594 iio_push_event(indio_dev, event, data->timestamp);
596 /* Reset the interrupt flag */
597 ret = regmap_field_write(data->reg_flag_psint, 0);
599 dev_err(&data->client->dev, "failed to reset interrupts\n");
601 mutex_unlock(&data->lock);
606 static int stk3310_probe(struct i2c_client *client)
609 struct iio_dev *indio_dev;
610 struct stk3310_data *data;
612 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
614 dev_err(&client->dev, "iio allocation failed!\n");
618 data = iio_priv(indio_dev);
619 data->client = client;
620 i2c_set_clientdata(client, indio_dev);
622 device_property_read_u32(&client->dev, "proximity-near-level",
623 &data->ps_near_level);
625 mutex_init(&data->lock);
627 ret = stk3310_regmap_init(data);
631 indio_dev->info = &stk3310_info;
632 indio_dev->name = STK3310_DRIVER_NAME;
633 indio_dev->modes = INDIO_DIRECT_MODE;
634 indio_dev->channels = stk3310_channels;
635 indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
637 ret = stk3310_init(indio_dev);
641 if (client->irq > 0) {
642 ret = devm_request_threaded_irq(&client->dev, client->irq,
644 stk3310_irq_event_handler,
645 IRQF_TRIGGER_FALLING |
647 STK3310_EVENT, indio_dev);
649 dev_err(&client->dev, "request irq %d failed\n",
655 ret = iio_device_register(indio_dev);
657 dev_err(&client->dev, "device_register failed\n");
664 stk3310_set_state(data, STK3310_STATE_STANDBY);
668 static void stk3310_remove(struct i2c_client *client)
670 struct iio_dev *indio_dev = i2c_get_clientdata(client);
672 iio_device_unregister(indio_dev);
673 stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
676 static int stk3310_suspend(struct device *dev)
678 struct stk3310_data *data;
680 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
682 return stk3310_set_state(data, STK3310_STATE_STANDBY);
685 static int stk3310_resume(struct device *dev)
688 struct stk3310_data *data;
690 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
691 if (data->ps_enabled)
692 state |= STK3310_STATE_EN_PS;
693 if (data->als_enabled)
694 state |= STK3310_STATE_EN_ALS;
696 return stk3310_set_state(data, state);
699 static DEFINE_SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend,
702 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[] = {
716 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
718 static const struct of_device_id stk3310_of_match[] = {
719 { .compatible = "sensortek,stk3310", },
720 { .compatible = "sensortek,stk3311", },
721 { .compatible = "sensortek,stk3335", },
724 MODULE_DEVICE_TABLE(of, stk3310_of_match);
726 static struct i2c_driver stk3310_driver = {
729 .of_match_table = stk3310_of_match,
730 .pm = pm_sleep_ptr(&stk3310_pm_ops),
731 .acpi_match_table = stk3310_acpi_id,
733 .probe = stk3310_probe,
734 .remove = stk3310_remove,
735 .id_table = stk3310_i2c_id,
738 module_i2c_driver(stk3310_driver);
741 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
742 MODULE_LICENSE("GPL v2");