2 * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor
4 * Copyright (c) 2015, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48.
13 #include <linux/acpi.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/regmap.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/iio/events.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 #define STK3310_REG_STATE 0x00
25 #define STK3310_REG_PSCTRL 0x01
26 #define STK3310_REG_ALSCTRL 0x02
27 #define STK3310_REG_INT 0x04
28 #define STK3310_REG_THDH_PS 0x06
29 #define STK3310_REG_THDL_PS 0x08
30 #define STK3310_REG_FLAG 0x10
31 #define STK3310_REG_PS_DATA_MSB 0x11
32 #define STK3310_REG_PS_DATA_LSB 0x12
33 #define STK3310_REG_ALS_DATA_MSB 0x13
34 #define STK3310_REG_ALS_DATA_LSB 0x14
35 #define STK3310_REG_ID 0x3E
36 #define STK3310_MAX_REG 0x80
38 #define STK3310_STATE_EN_PS 0x01
39 #define STK3310_STATE_EN_ALS 0x02
40 #define STK3310_STATE_STANDBY 0x00
42 #define STK3310_CHIP_ID_VAL 0x13
43 #define STK3311_CHIP_ID_VAL 0x1D
44 #define STK3310_PSINT_EN 0x01
45 #define STK3310_PS_MAX_VAL 0xFFFF
46 #define STK3310_THRESH_MAX 0xFFFF
48 #define STK3310_DRIVER_NAME "stk3310"
49 #define STK3310_REGMAP_NAME "stk3310_regmap"
50 #define STK3310_EVENT "stk3310_event"
51 #define STK3310_GPIO "stk3310_gpio"
53 #define STK3310_SCALE_AVAILABLE "6.4 1.6 0.4 0.1"
55 #define STK3310_IT_AVAILABLE \
56 "0.000185 0.000370 0.000741 0.001480 0.002960 0.005920 0.011840 " \
57 "0.023680 0.047360 0.094720 0.189440 0.378880 0.757760 1.515520 " \
60 #define STK3310_REGFIELD(name) \
63 devm_regmap_field_alloc(&client->dev, regmap, \
64 stk3310_reg_field_##name); \
65 if (IS_ERR(data->reg_##name)) { \
66 dev_err(&client->dev, "reg field alloc failed.\n"); \
67 return PTR_ERR(data->reg_##name); \
71 static const struct reg_field stk3310_reg_field_state =
72 REG_FIELD(STK3310_REG_STATE, 0, 2);
73 static const struct reg_field stk3310_reg_field_als_gain =
74 REG_FIELD(STK3310_REG_ALSCTRL, 4, 5);
75 static const struct reg_field stk3310_reg_field_ps_gain =
76 REG_FIELD(STK3310_REG_PSCTRL, 4, 5);
77 static const struct reg_field stk3310_reg_field_als_it =
78 REG_FIELD(STK3310_REG_ALSCTRL, 0, 3);
79 static const struct reg_field stk3310_reg_field_ps_it =
80 REG_FIELD(STK3310_REG_PSCTRL, 0, 3);
81 static const struct reg_field stk3310_reg_field_int_ps =
82 REG_FIELD(STK3310_REG_INT, 0, 2);
83 static const struct reg_field stk3310_reg_field_flag_psint =
84 REG_FIELD(STK3310_REG_FLAG, 4, 4);
85 static const struct reg_field stk3310_reg_field_flag_nf =
86 REG_FIELD(STK3310_REG_FLAG, 0, 0);
88 * Maximum PS values with regard to scale. Used to export the 'inverse'
89 * PS value (high values for far objects, low values for near objects).
91 static const int stk3310_ps_max[4] = {
92 STK3310_PS_MAX_VAL / 64,
93 STK3310_PS_MAX_VAL / 16,
94 STK3310_PS_MAX_VAL / 4,
98 static const int stk3310_scale_table[][2] = {
99 {6, 400000}, {1, 600000}, {0, 400000}, {0, 100000}
102 /* Integration time in seconds, microseconds */
103 static const int stk3310_it_table[][2] = {
104 {0, 185}, {0, 370}, {0, 741}, {0, 1480},
105 {0, 2960}, {0, 5920}, {0, 11840}, {0, 23680},
106 {0, 47360}, {0, 94720}, {0, 189440}, {0, 378880},
107 {0, 757760}, {1, 515520}, {3, 31040}, {6, 62080},
110 struct stk3310_data {
111 struct i2c_client *client;
116 struct regmap *regmap;
117 struct regmap_field *reg_state;
118 struct regmap_field *reg_als_gain;
119 struct regmap_field *reg_ps_gain;
120 struct regmap_field *reg_als_it;
121 struct regmap_field *reg_ps_it;
122 struct regmap_field *reg_int_ps;
123 struct regmap_field *reg_flag_psint;
124 struct regmap_field *reg_flag_nf;
127 static const struct iio_event_spec stk3310_events[] = {
128 /* Proximity event */
130 .type = IIO_EV_TYPE_THRESH,
131 .dir = IIO_EV_DIR_FALLING,
132 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
133 BIT(IIO_EV_INFO_ENABLE),
135 /* Out-of-proximity event */
137 .type = IIO_EV_TYPE_THRESH,
138 .dir = IIO_EV_DIR_RISING,
139 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
140 BIT(IIO_EV_INFO_ENABLE),
144 static const struct iio_chan_spec stk3310_channels[] = {
147 .info_mask_separate =
148 BIT(IIO_CHAN_INFO_RAW) |
149 BIT(IIO_CHAN_INFO_SCALE) |
150 BIT(IIO_CHAN_INFO_INT_TIME),
153 .type = IIO_PROXIMITY,
154 .info_mask_separate =
155 BIT(IIO_CHAN_INFO_RAW) |
156 BIT(IIO_CHAN_INFO_SCALE) |
157 BIT(IIO_CHAN_INFO_INT_TIME),
158 .event_spec = stk3310_events,
159 .num_event_specs = ARRAY_SIZE(stk3310_events),
163 static IIO_CONST_ATTR(in_illuminance_scale_available, STK3310_SCALE_AVAILABLE);
165 static IIO_CONST_ATTR(in_proximity_scale_available, STK3310_SCALE_AVAILABLE);
167 static IIO_CONST_ATTR(in_illuminance_integration_time_available,
168 STK3310_IT_AVAILABLE);
170 static IIO_CONST_ATTR(in_proximity_integration_time_available,
171 STK3310_IT_AVAILABLE);
173 static struct attribute *stk3310_attributes[] = {
174 &iio_const_attr_in_illuminance_scale_available.dev_attr.attr,
175 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
176 &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
177 &iio_const_attr_in_proximity_integration_time_available.dev_attr.attr,
181 static const struct attribute_group stk3310_attribute_group = {
182 .attrs = stk3310_attributes
185 static int stk3310_get_index(const int table[][2], int table_size,
190 for (i = 0; i < table_size; i++) {
191 if (val == table[i][0] && val2 == table[i][1])
198 static int stk3310_read_event(struct iio_dev *indio_dev,
199 const struct iio_chan_spec *chan,
200 enum iio_event_type type,
201 enum iio_event_direction dir,
202 enum iio_event_info info,
209 struct stk3310_data *data = iio_priv(indio_dev);
211 if (info != IIO_EV_INFO_VALUE)
215 * Only proximity interrupts are implemented at the moment.
216 * Since we're inverting proximity values, the sensor's 'high'
217 * threshold will become our 'low' threshold, associated with
218 * 'near' events. Similarly, the sensor's 'low' threshold will
219 * be our 'high' threshold, associated with 'far' events.
221 if (dir == IIO_EV_DIR_RISING)
222 reg = STK3310_REG_THDL_PS;
223 else if (dir == IIO_EV_DIR_FALLING)
224 reg = STK3310_REG_THDH_PS;
228 mutex_lock(&data->lock);
229 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
230 mutex_unlock(&data->lock);
232 dev_err(&data->client->dev, "register read failed\n");
235 regmap_field_read(data->reg_ps_gain, &index);
236 *val = swab16(stk3310_ps_max[index] - buf);
241 static int stk3310_write_event(struct iio_dev *indio_dev,
242 const struct iio_chan_spec *chan,
243 enum iio_event_type type,
244 enum iio_event_direction dir,
245 enum iio_event_info info,
252 struct stk3310_data *data = iio_priv(indio_dev);
253 struct i2c_client *client = data->client;
255 regmap_field_read(data->reg_ps_gain, &index);
256 if (val > stk3310_ps_max[index])
259 if (dir == IIO_EV_DIR_RISING)
260 reg = STK3310_REG_THDL_PS;
261 else if (dir == IIO_EV_DIR_FALLING)
262 reg = STK3310_REG_THDH_PS;
266 buf = swab16(stk3310_ps_max[index] - val);
267 ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
269 dev_err(&client->dev, "failed to set PS threshold!\n");
274 static int stk3310_read_event_config(struct iio_dev *indio_dev,
275 const struct iio_chan_spec *chan,
276 enum iio_event_type type,
277 enum iio_event_direction dir)
279 unsigned int event_val;
280 struct stk3310_data *data = iio_priv(indio_dev);
282 regmap_field_read(data->reg_int_ps, &event_val);
287 static int stk3310_write_event_config(struct iio_dev *indio_dev,
288 const struct iio_chan_spec *chan,
289 enum iio_event_type type,
290 enum iio_event_direction dir,
294 struct stk3310_data *data = iio_priv(indio_dev);
295 struct i2c_client *client = data->client;
297 if (state < 0 || state > 7)
300 /* Set INT_PS value */
301 mutex_lock(&data->lock);
302 ret = regmap_field_write(data->reg_int_ps, state);
304 dev_err(&client->dev, "failed to set interrupt mode\n");
305 mutex_unlock(&data->lock);
310 static int stk3310_read_raw(struct iio_dev *indio_dev,
311 struct iio_chan_spec const *chan,
312 int *val, int *val2, long mask)
318 struct stk3310_data *data = iio_priv(indio_dev);
319 struct i2c_client *client = data->client;
322 case IIO_CHAN_INFO_RAW:
323 if (chan->type == IIO_LIGHT)
324 reg = STK3310_REG_ALS_DATA_MSB;
325 else if (chan->type == IIO_PROXIMITY)
326 reg = STK3310_REG_PS_DATA_MSB;
329 mutex_lock(&data->lock);
330 ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
332 dev_err(&client->dev, "register read failed\n");
333 mutex_unlock(&data->lock);
337 if (chan->type == IIO_PROXIMITY) {
339 * Invert the proximity data so we return low values
340 * for close objects and high values for far ones.
342 regmap_field_read(data->reg_ps_gain, &index);
343 *val = stk3310_ps_max[index] - *val;
345 mutex_unlock(&data->lock);
347 case IIO_CHAN_INFO_INT_TIME:
348 if (chan->type == IIO_LIGHT)
349 regmap_field_read(data->reg_als_it, &index);
351 regmap_field_read(data->reg_ps_it, &index);
352 *val = stk3310_it_table[index][0];
353 *val2 = stk3310_it_table[index][1];
354 return IIO_VAL_INT_PLUS_MICRO;
355 case IIO_CHAN_INFO_SCALE:
356 if (chan->type == IIO_LIGHT)
357 regmap_field_read(data->reg_als_gain, &index);
359 regmap_field_read(data->reg_ps_gain, &index);
360 *val = stk3310_scale_table[index][0];
361 *val2 = stk3310_scale_table[index][1];
362 return IIO_VAL_INT_PLUS_MICRO;
368 static int stk3310_write_raw(struct iio_dev *indio_dev,
369 struct iio_chan_spec const *chan,
370 int val, int val2, long mask)
374 struct stk3310_data *data = iio_priv(indio_dev);
377 case IIO_CHAN_INFO_INT_TIME:
378 index = stk3310_get_index(stk3310_it_table,
379 ARRAY_SIZE(stk3310_it_table),
383 mutex_lock(&data->lock);
384 if (chan->type == IIO_LIGHT)
385 ret = regmap_field_write(data->reg_als_it, index);
387 ret = regmap_field_write(data->reg_ps_it, index);
389 dev_err(&data->client->dev,
390 "sensor configuration failed\n");
391 mutex_unlock(&data->lock);
394 case IIO_CHAN_INFO_SCALE:
395 index = stk3310_get_index(stk3310_scale_table,
396 ARRAY_SIZE(stk3310_scale_table),
400 mutex_lock(&data->lock);
401 if (chan->type == IIO_LIGHT)
402 ret = regmap_field_write(data->reg_als_gain, index);
404 ret = regmap_field_write(data->reg_ps_gain, index);
406 dev_err(&data->client->dev,
407 "sensor configuration failed\n");
408 mutex_unlock(&data->lock);
415 static const struct iio_info stk3310_info = {
416 .driver_module = THIS_MODULE,
417 .read_raw = stk3310_read_raw,
418 .write_raw = stk3310_write_raw,
419 .attrs = &stk3310_attribute_group,
420 .read_event_value = stk3310_read_event,
421 .write_event_value = stk3310_write_event,
422 .read_event_config = stk3310_read_event_config,
423 .write_event_config = stk3310_write_event_config,
426 static int stk3310_set_state(struct stk3310_data *data, u8 state)
429 struct i2c_client *client = data->client;
431 /* 3-bit state; 0b100 is not supported. */
432 if (state > 7 || state == 4)
435 mutex_lock(&data->lock);
436 ret = regmap_field_write(data->reg_state, state);
438 dev_err(&client->dev, "failed to change sensor state\n");
439 } else if (state != STK3310_STATE_STANDBY) {
440 /* Don't reset the 'enabled' flags if we're going in standby */
441 data->ps_enabled = !!(state & 0x01);
442 data->als_enabled = !!(state & 0x02);
444 mutex_unlock(&data->lock);
449 static int stk3310_init(struct iio_dev *indio_dev)
454 struct stk3310_data *data = iio_priv(indio_dev);
455 struct i2c_client *client = data->client;
457 regmap_read(data->regmap, STK3310_REG_ID, &chipid);
458 if (chipid != STK3310_CHIP_ID_VAL &&
459 chipid != STK3311_CHIP_ID_VAL) {
460 dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid);
464 state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS;
465 ret = stk3310_set_state(data, state);
467 dev_err(&client->dev, "failed to enable sensor");
471 /* Enable PS interrupts */
472 ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN);
474 dev_err(&client->dev, "failed to enable interrupts!\n");
479 static int stk3310_gpio_probe(struct i2c_client *client)
482 struct gpio_desc *gpio;
490 /* gpio interrupt pin */
491 gpio = devm_gpiod_get_index(dev, STK3310_GPIO, 0);
493 dev_err(dev, "acpi gpio get index failed\n");
494 return PTR_ERR(gpio);
497 ret = gpiod_direction_input(gpio);
501 ret = gpiod_to_irq(gpio);
502 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
507 static bool stk3310_is_volatile_reg(struct device *dev, unsigned int reg)
510 case STK3310_REG_ALS_DATA_MSB:
511 case STK3310_REG_ALS_DATA_LSB:
512 case STK3310_REG_PS_DATA_LSB:
513 case STK3310_REG_PS_DATA_MSB:
514 case STK3310_REG_FLAG:
521 static struct regmap_config stk3310_regmap_config = {
522 .name = STK3310_REGMAP_NAME,
525 .max_register = STK3310_MAX_REG,
526 .cache_type = REGCACHE_RBTREE,
527 .volatile_reg = stk3310_is_volatile_reg,
530 static int stk3310_regmap_init(struct stk3310_data *data)
532 struct regmap *regmap;
533 struct i2c_client *client;
535 client = data->client;
536 regmap = devm_regmap_init_i2c(client, &stk3310_regmap_config);
537 if (IS_ERR(regmap)) {
538 dev_err(&client->dev, "regmap initialization failed.\n");
539 return PTR_ERR(regmap);
541 data->regmap = regmap;
543 STK3310_REGFIELD(state);
544 STK3310_REGFIELD(als_gain);
545 STK3310_REGFIELD(ps_gain);
546 STK3310_REGFIELD(als_it);
547 STK3310_REGFIELD(ps_it);
548 STK3310_REGFIELD(int_ps);
549 STK3310_REGFIELD(flag_psint);
550 STK3310_REGFIELD(flag_nf);
555 static irqreturn_t stk3310_irq_handler(int irq, void *private)
557 struct iio_dev *indio_dev = private;
558 struct stk3310_data *data = iio_priv(indio_dev);
560 data->timestamp = iio_get_time_ns();
562 return IRQ_WAKE_THREAD;
565 static irqreturn_t stk3310_irq_event_handler(int irq, void *private)
571 struct iio_dev *indio_dev = private;
572 struct stk3310_data *data = iio_priv(indio_dev);
574 /* Read FLAG_NF to figure out what threshold has been met. */
575 mutex_lock(&data->lock);
576 ret = regmap_field_read(data->reg_flag_nf, &dir);
578 dev_err(&data->client->dev, "register read failed\n");
579 mutex_unlock(&data->lock);
582 event = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 1,
584 (dir ? IIO_EV_DIR_RISING :
585 IIO_EV_DIR_FALLING));
586 iio_push_event(indio_dev, event, data->timestamp);
588 /* Reset the interrupt flag */
589 ret = regmap_field_write(data->reg_flag_psint, 0);
591 dev_err(&data->client->dev, "failed to reset interrupts\n");
592 mutex_unlock(&data->lock);
597 static int stk3310_probe(struct i2c_client *client,
598 const struct i2c_device_id *id)
601 struct iio_dev *indio_dev;
602 struct stk3310_data *data;
604 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
606 dev_err(&client->dev, "iio allocation failed!\n");
610 data = iio_priv(indio_dev);
611 data->client = client;
612 i2c_set_clientdata(client, indio_dev);
613 mutex_init(&data->lock);
615 ret = stk3310_regmap_init(data);
619 indio_dev->dev.parent = &client->dev;
620 indio_dev->info = &stk3310_info;
621 indio_dev->name = STK3310_DRIVER_NAME;
622 indio_dev->modes = INDIO_DIRECT_MODE;
623 indio_dev->channels = stk3310_channels;
624 indio_dev->num_channels = ARRAY_SIZE(stk3310_channels);
626 ret = stk3310_init(indio_dev);
630 ret = iio_device_register(indio_dev);
632 dev_err(&client->dev, "device_register failed\n");
633 stk3310_set_state(data, STK3310_STATE_STANDBY);
636 if (client->irq <= 0)
637 client->irq = stk3310_gpio_probe(client);
639 if (client->irq >= 0) {
640 ret = devm_request_threaded_irq(&client->dev, client->irq,
642 stk3310_irq_event_handler,
643 IRQF_TRIGGER_FALLING |
645 STK3310_EVENT, indio_dev);
647 dev_err(&client->dev, "request irq %d failed\n",
654 static int stk3310_remove(struct i2c_client *client)
656 struct iio_dev *indio_dev = i2c_get_clientdata(client);
658 iio_device_unregister(indio_dev);
659 return stk3310_set_state(iio_priv(indio_dev), STK3310_STATE_STANDBY);
662 #ifdef CONFIG_PM_SLEEP
663 static int stk3310_suspend(struct device *dev)
665 struct stk3310_data *data;
667 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
669 return stk3310_set_state(data, STK3310_STATE_STANDBY);
672 static int stk3310_resume(struct device *dev)
675 struct stk3310_data *data;
677 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
678 if (data->ps_enabled)
679 state |= STK3310_STATE_EN_PS;
680 if (data->als_enabled)
681 state |= STK3310_STATE_EN_ALS;
683 return stk3310_set_state(data, state);
686 static SIMPLE_DEV_PM_OPS(stk3310_pm_ops, stk3310_suspend, stk3310_resume);
688 #define STK3310_PM_OPS (&stk3310_pm_ops)
690 #define STK3310_PM_OPS NULL
693 static const struct i2c_device_id stk3310_i2c_id[] = {
699 static const struct acpi_device_id stk3310_acpi_id[] = {
705 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id);
707 static struct i2c_driver stk3310_driver = {
710 .pm = STK3310_PM_OPS,
711 .acpi_match_table = ACPI_PTR(stk3310_acpi_id),
713 .probe = stk3310_probe,
714 .remove = stk3310_remove,
715 .id_table = stk3310_i2c_id,
718 module_i2c_driver(stk3310_driver);
720 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");
721 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");
722 MODULE_LICENSE("GPL v2");