Commit cd879aa9 authored by Sergei Miroshnichenko's avatar Sergei Miroshnichenko Committed by Martyn Welch

iio: light: noa1305: Add support for NOA1305

This driver adds the initial support for the ON Semiconductor
NOA1305 Ambient Light Sensor.

Originally written by Sergei Miroshnichenko. Found here:
  https://github.com/EmcraftSystems/linux-upstream/commit/196d6cf897e632d2cb82d45484bd7a1bfdd5b6d9?diff=unified

Regulator control added by Martyn Welch.
Signed-off-by: default avatarMartyn Welch <martyn.welch@collabora.com>
parent a575de2d
......@@ -288,6 +288,16 @@ config MAX44000
To compile this driver as a module, choose M here:
the module will be called max44000.
config NOA1305
tristate "ON Semiconductor NOA1305 ambient light sensor"
depends on I2C
help
Say Y here if you want to build support for the ON Semiconductor
NOA1305 ambient light sensor.
To compile this driver as a module, choose M here:
The module will be called noa1305.
config OPT3001
tristate "Texas Instruments OPT3001 Light Sensor"
depends on I2C
......
......@@ -26,6 +26,7 @@ obj-$(CONFIG_JSA1212) += jsa1212.o
obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o
obj-$(CONFIG_LTR501) += ltr501.o
obj-$(CONFIG_MAX44000) += max44000.o
obj-$(CONFIG_NOA1305) += noa1305.o
obj-$(CONFIG_OPT3001) += opt3001.o
obj-$(CONFIG_PA12203001) += pa12203001.o
obj-$(CONFIG_RPR0521) += rpr0521.o
......
// SPDX-License-Identifier: GPL-2.0+
/*
* Support for ON Semiconductor NOA1305 ambient light sensor
*
* Copyright (C) 2016 Emcraft Systems
* Copyright (C) 2019 Collabora Ltd.
*/
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#define NOA1305_REG_POWER_CONTROL 0x0
#define NOA1305_REG_RESET 0x1
#define NOA1305_REG_INTEGRATION_TIME 0x2
#define NOA1305_REG_INT_SELECT 0x3
#define NOA1305_REG_INT_THRESH_LSB 0x4
#define NOA1305_REG_INT_THRESH_MSB 0x5
#define NOA1305_REG_ALS_DATA_LSB 0x6
#define NOA1305_REG_ALS_DATA_MSB 0x7
#define NOA1305_REG_DEVICE_ID_LSB 0x8
#define NOA1305_REG_DEVICE_ID_MSB 0x9
#define NOA1305_DEVICE_ID 0x0519
#define NOA1305_POWER_ON 0x08
#define NOA1305_POWER_DOWN 0x00
#define NOA1305_RESET 0x10
#define NOA1305_INT_ACTIVE_HIGH 0x01
#define NOA1305_INT_ACTIVE_LOW 0x02
#define NOA1305_INT_INACTIVE 0x03
#define NOA1305_INTEGR_TIME_800MS 0x00
#define NOA1305_INTEGR_TIME_400MS 0x01
#define NOA1305_INTEGR_TIME_200MS 0x02
#define NOA1305_INTEGR_TIME_100MS 0x03
#define NOA1305_INTEGR_TIME_50MS 0x04
#define NOA1305_INTEGR_TIME_25MS 0x05
#define NOA1305_INTEGR_TIME_12_5MS 0x06
#define NOA1305_INTEGR_TIME_6_25MS 0x07
struct noa1305_priv {
struct i2c_client *client;
struct regulator *vin_reg;
};
static int noa1305_measure(struct noa1305_priv *priv)
{
u8 data[2];
int status;
status = i2c_smbus_read_i2c_block_data(priv->client,
NOA1305_REG_ALS_DATA_LSB, 2,
data);
if (status < 0)
return status;
return (data[1] << 8) | data[0];
}
static const struct iio_chan_spec noa1305_channels[] = {
{
.type = IIO_LIGHT,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
}
};
static int noa1305_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
int ret = -EINVAL;
struct noa1305_priv *priv = iio_priv(indio_dev);
switch (mask) {
case IIO_CHAN_INFO_RAW:
switch (chan->type) {
case IIO_LIGHT:
ret = noa1305_measure(priv);
if (ret < 0)
return ret;
*val = ret;
ret = IIO_VAL_INT;
break;
default:
break;
}
break;
default:
break;
}
return ret;
}
static const struct iio_info noa1305_info = {
.read_raw = noa1305_read_raw,
};
static int noa1305_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct noa1305_priv *priv;
struct iio_dev *indio_dev;
u8 data[2];
unsigned int dev_id = 0;
int status;
int ret;
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*priv));
if (!indio_dev)
return -ENOMEM;
priv = iio_priv(indio_dev);
priv->vin_reg = devm_regulator_get(&client->dev, "vin");
if (IS_ERR(priv->vin_reg)) {
dev_err(&client->dev, "get regulator vin failed\n");
return PTR_ERR(priv->vin_reg);
}
ret = regulator_enable(priv->vin_reg);
if (ret) {
dev_err(&client->dev, "enable regulator vin failed\n");
return ret;
}
i2c_set_clientdata(client, indio_dev);
priv->client = client;
status = i2c_smbus_read_i2c_block_data(client,
NOA1305_REG_DEVICE_ID_LSB,
2, data);
if (status < 0) {
dev_err(&client->dev, "ID reading failed: %d\n", status);
ret = status;
goto error_disable_reg;
}
dev_id = (data[1] << 8) | data[0];
if (dev_id != NOA1305_DEVICE_ID) {
dev_err(&client->dev, "Unknown device ID: 0x%x\n", dev_id);
ret = -ENODEV;
goto error_disable_reg;
}
i2c_smbus_write_byte_data(client, NOA1305_REG_POWER_CONTROL,
NOA1305_POWER_ON);
i2c_smbus_write_byte_data(client, NOA1305_REG_RESET, NOA1305_RESET);
i2c_smbus_write_byte_data(client, NOA1305_REG_INTEGRATION_TIME,
NOA1305_INTEGR_TIME_800MS);
i2c_smbus_write_byte_data(client, NOA1305_REG_INT_SELECT,
NOA1305_INT_INACTIVE);
indio_dev->dev.parent = &client->dev;
indio_dev->info = &noa1305_info;
indio_dev->channels = noa1305_channels;
indio_dev->num_channels = ARRAY_SIZE(noa1305_channels);
indio_dev->name = id->name;
indio_dev->modes = INDIO_DIRECT_MODE;
dev_info(&client->dev, "NOA1305 Ambient light sensor\n");
ret = devm_iio_device_register(&client->dev, indio_dev);
if (ret) {
dev_err(&client->dev, "registering device failed\n");
goto error_disable_reg;
}
return ret;
error_disable_reg:
regulator_disable(priv->vin_reg);
return ret;
}
static int noa1305_remove(struct i2c_client *client)
{
struct iio_dev *indio_dev = i2c_get_clientdata(client);
struct noa1305_priv *priv = iio_priv(indio_dev);
regulator_disable(priv->vin_reg);
return 0;
}
static const struct of_device_id noa1305_of_match[] = {
{ .compatible = "onnn,noa1305" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, noa1305_of_match);
static const struct i2c_device_id noa1305_ids[] = {
{ "noa1305", 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, noa1305_id);
static struct i2c_driver noa1305_driver = {
.driver = {
.name = "noa1305",
.of_match_table = noa1305_of_match,
},
.probe = noa1305_probe,
.remove = noa1305_remove,
.id_table = noa1305_ids,
};
module_i2c_driver(noa1305_driver);
MODULE_AUTHOR("Sergei Miroshnichenko <sergeimir@emcraft.com>");
MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.com");
MODULE_DESCRIPTION("ON Semiconductor NOA1305 ambient light sensor");
MODULE_LICENSE("GPL");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment