Lines Matching +full:proximity +full:- +full:near +full:- +full:level

1 // SPDX-License-Identifier: GPL-2.0-only
3 * Sensortek STK3310/STK3311 Ambient Light and Proximity Sensor
7 * IIO driver for STK3310/STK3311. 7-bit I2C address: 0x48.
58 data->reg_##name = \
59 devm_regmap_field_alloc(&client->dev, regmap, \
61 if (IS_ERR(data->reg_##name)) { \
62 dev_err(&client->dev, "reg field alloc failed.\n"); \
63 return PTR_ERR(data->reg_##name); \
84 /* Estimate maximum proximity values with regard to measurement scale. */
123 /* Proximity event */
130 /* Out-of-proximity event */
146 return sprintf(buf, "%u\n", data->ps_near_level); in stk3310_read_near_level()
210 return -EINVAL; in stk3310_get_index()
226 return -EINVAL; in stk3310_read_event()
228 /* Only proximity interrupts are implemented at the moment. */ in stk3310_read_event()
234 return -EINVAL; in stk3310_read_event()
236 mutex_lock(&data->lock); in stk3310_read_event()
237 ret = regmap_bulk_read(data->regmap, reg, &buf, 2); in stk3310_read_event()
238 mutex_unlock(&data->lock); in stk3310_read_event()
240 dev_err(&data->client->dev, "register read failed\n"); in stk3310_read_event()
260 struct i2c_client *client = data->client; in stk3310_write_event()
262 ret = regmap_field_read(data->reg_ps_gain, &index); in stk3310_write_event()
267 return -EINVAL; in stk3310_write_event()
274 return -EINVAL; in stk3310_write_event()
277 ret = regmap_bulk_write(data->regmap, reg, &buf, 2); in stk3310_write_event()
279 dev_err(&client->dev, "failed to set PS threshold!\n"); in stk3310_write_event()
293 ret = regmap_field_read(data->reg_int_ps, &event_val); in stk3310_read_event_config()
308 struct i2c_client *client = data->client; in stk3310_write_event_config()
311 return -EINVAL; in stk3310_write_event_config()
314 mutex_lock(&data->lock); in stk3310_write_event_config()
315 ret = regmap_field_write(data->reg_int_ps, state); in stk3310_write_event_config()
317 dev_err(&client->dev, "failed to set interrupt mode\n"); in stk3310_write_event_config()
318 mutex_unlock(&data->lock); in stk3310_write_event_config()
332 struct i2c_client *client = data->client; in stk3310_read_raw()
334 if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY) in stk3310_read_raw()
335 return -EINVAL; in stk3310_read_raw()
339 if (chan->type == IIO_LIGHT) in stk3310_read_raw()
344 mutex_lock(&data->lock); in stk3310_read_raw()
345 ret = regmap_bulk_read(data->regmap, reg, &buf, 2); in stk3310_read_raw()
347 dev_err(&client->dev, "register read failed\n"); in stk3310_read_raw()
348 mutex_unlock(&data->lock); in stk3310_read_raw()
352 mutex_unlock(&data->lock); in stk3310_read_raw()
355 if (chan->type == IIO_LIGHT) in stk3310_read_raw()
356 ret = regmap_field_read(data->reg_als_it, &index); in stk3310_read_raw()
358 ret = regmap_field_read(data->reg_ps_it, &index); in stk3310_read_raw()
366 if (chan->type == IIO_LIGHT) in stk3310_read_raw()
367 ret = regmap_field_read(data->reg_als_gain, &index); in stk3310_read_raw()
369 ret = regmap_field_read(data->reg_ps_gain, &index); in stk3310_read_raw()
378 return -EINVAL; in stk3310_read_raw()
389 if (chan->type != IIO_LIGHT && chan->type != IIO_PROXIMITY) in stk3310_write_raw()
390 return -EINVAL; in stk3310_write_raw()
398 return -EINVAL; in stk3310_write_raw()
399 mutex_lock(&data->lock); in stk3310_write_raw()
400 if (chan->type == IIO_LIGHT) in stk3310_write_raw()
401 ret = regmap_field_write(data->reg_als_it, index); in stk3310_write_raw()
403 ret = regmap_field_write(data->reg_ps_it, index); in stk3310_write_raw()
405 dev_err(&data->client->dev, in stk3310_write_raw()
407 mutex_unlock(&data->lock); in stk3310_write_raw()
415 return -EINVAL; in stk3310_write_raw()
416 mutex_lock(&data->lock); in stk3310_write_raw()
417 if (chan->type == IIO_LIGHT) in stk3310_write_raw()
418 ret = regmap_field_write(data->reg_als_gain, index); in stk3310_write_raw()
420 ret = regmap_field_write(data->reg_ps_gain, index); in stk3310_write_raw()
422 dev_err(&data->client->dev, in stk3310_write_raw()
424 mutex_unlock(&data->lock); in stk3310_write_raw()
428 return -EINVAL; in stk3310_write_raw()
444 struct i2c_client *client = data->client; in stk3310_set_state()
446 /* 3-bit state; 0b100 is not supported. */ in stk3310_set_state()
448 return -EINVAL; in stk3310_set_state()
450 mutex_lock(&data->lock); in stk3310_set_state()
451 ret = regmap_field_write(data->reg_state, state); in stk3310_set_state()
453 dev_err(&client->dev, "failed to change sensor state\n"); in stk3310_set_state()
456 data->ps_enabled = !!(state & STK3310_STATE_EN_PS); in stk3310_set_state()
457 data->als_enabled = !!(state & STK3310_STATE_EN_ALS); in stk3310_set_state()
459 mutex_unlock(&data->lock); in stk3310_set_state()
470 struct i2c_client *client = data->client; in stk3310_init()
472 ret = regmap_read(data->regmap, STK3310_REG_ID, &chipid); in stk3310_init()
480 dev_err(&client->dev, "invalid chip id: 0x%x\n", chipid); in stk3310_init()
481 return -ENODEV; in stk3310_init()
487 dev_err(&client->dev, "failed to enable sensor"); in stk3310_init()
492 ret = regmap_field_write(data->reg_int_ps, STK3310_PSINT_EN); in stk3310_init()
494 dev_err(&client->dev, "failed to enable interrupts!\n"); in stk3310_init()
527 client = data->client; in stk3310_regmap_init()
530 dev_err(&client->dev, "regmap initialization failed.\n"); in stk3310_regmap_init()
533 data->regmap = regmap; in stk3310_regmap_init()
552 data->timestamp = iio_get_time_ns(indio_dev); in stk3310_irq_handler()
567 mutex_lock(&data->lock); in stk3310_irq_event_handler()
568 ret = regmap_field_read(data->reg_flag_nf, &dir); in stk3310_irq_event_handler()
570 dev_err(&data->client->dev, "register read failed: %d\n", ret); in stk3310_irq_event_handler()
577 iio_push_event(indio_dev, event, data->timestamp); in stk3310_irq_event_handler()
580 ret = regmap_field_write(data->reg_flag_psint, 0); in stk3310_irq_event_handler()
582 dev_err(&data->client->dev, "failed to reset interrupts\n"); in stk3310_irq_event_handler()
584 mutex_unlock(&data->lock); in stk3310_irq_event_handler()
596 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); in stk3310_probe()
598 dev_err(&client->dev, "iio allocation failed!\n"); in stk3310_probe()
599 return -ENOMEM; in stk3310_probe()
603 data->client = client; in stk3310_probe()
606 device_property_read_u32(&client->dev, "proximity-near-level", in stk3310_probe()
607 &data->ps_near_level); in stk3310_probe()
609 mutex_init(&data->lock); in stk3310_probe()
615 indio_dev->info = &stk3310_info; in stk3310_probe()
616 indio_dev->name = STK3310_DRIVER_NAME; in stk3310_probe()
617 indio_dev->modes = INDIO_DIRECT_MODE; in stk3310_probe()
618 indio_dev->channels = stk3310_channels; in stk3310_probe()
619 indio_dev->num_channels = ARRAY_SIZE(stk3310_channels); in stk3310_probe()
625 if (client->irq > 0) { in stk3310_probe()
626 ret = devm_request_threaded_irq(&client->dev, client->irq, in stk3310_probe()
633 dev_err(&client->dev, "request irq %d failed\n", in stk3310_probe()
634 client->irq); in stk3310_probe()
641 dev_err(&client->dev, "device_register failed\n"); in stk3310_probe()
675 if (data->ps_enabled) in stk3310_resume()
677 if (data->als_enabled) in stk3310_resume()
726 MODULE_DESCRIPTION("STK3310 Ambient Light and Proximity Sensor driver");