18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * veml6070.c - Support for Vishay VEML6070 UV A light sensor 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2016 Peter Meerwald-Stadler <pmeerw@pmeerw.net> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * IIO driver for VEML6070 (7-bit I2C slave addresses 0x38 and 0x39) 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * TODO: integration time, ACK signal 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <linux/mutex.h> 158c2ecf20Sopenharmony_ci#include <linux/err.h> 168c2ecf20Sopenharmony_ci#include <linux/delay.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 198c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define VEML6070_DRV_NAME "veml6070" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define VEML6070_ADDR_CONFIG_DATA_MSB 0x38 /* read: MSB data, write: config */ 248c2ecf20Sopenharmony_ci#define VEML6070_ADDR_DATA_LSB 0x39 /* LSB data */ 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define VEML6070_COMMAND_ACK BIT(5) /* raise interrupt when over threshold */ 278c2ecf20Sopenharmony_ci#define VEML6070_COMMAND_IT GENMASK(3, 2) /* bit mask integration time */ 288c2ecf20Sopenharmony_ci#define VEML6070_COMMAND_RSRVD BIT(1) /* reserved, set to 1 */ 298c2ecf20Sopenharmony_ci#define VEML6070_COMMAND_SD BIT(0) /* shutdown mode when set */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define VEML6070_IT_10 0x04 /* integration time 1x */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistruct veml6070_data { 348c2ecf20Sopenharmony_ci struct i2c_client *client1; 358c2ecf20Sopenharmony_ci struct i2c_client *client2; 368c2ecf20Sopenharmony_ci u8 config; 378c2ecf20Sopenharmony_ci struct mutex lock; 388c2ecf20Sopenharmony_ci}; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int veml6070_read(struct veml6070_data *data) 418c2ecf20Sopenharmony_ci{ 428c2ecf20Sopenharmony_ci int ret; 438c2ecf20Sopenharmony_ci u8 msb, lsb; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci mutex_lock(&data->lock); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci /* disable shutdown */ 488c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte(data->client1, 498c2ecf20Sopenharmony_ci data->config & ~VEML6070_COMMAND_SD); 508c2ecf20Sopenharmony_ci if (ret < 0) 518c2ecf20Sopenharmony_ci goto out; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci msleep(125 + 10); /* measurement takes up to 125 ms for IT 1x */ 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte(data->client2); /* read MSB, address 0x39 */ 568c2ecf20Sopenharmony_ci if (ret < 0) 578c2ecf20Sopenharmony_ci goto out; 588c2ecf20Sopenharmony_ci msb = ret; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci ret = i2c_smbus_read_byte(data->client1); /* read LSB, address 0x38 */ 618c2ecf20Sopenharmony_ci if (ret < 0) 628c2ecf20Sopenharmony_ci goto out; 638c2ecf20Sopenharmony_ci lsb = ret; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci /* shutdown again */ 668c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte(data->client1, data->config); 678c2ecf20Sopenharmony_ci if (ret < 0) 688c2ecf20Sopenharmony_ci goto out; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci ret = (msb << 8) | lsb; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciout: 738c2ecf20Sopenharmony_ci mutex_unlock(&data->lock); 748c2ecf20Sopenharmony_ci return ret; 758c2ecf20Sopenharmony_ci} 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic const struct iio_chan_spec veml6070_channels[] = { 788c2ecf20Sopenharmony_ci { 798c2ecf20Sopenharmony_ci .type = IIO_INTENSITY, 808c2ecf20Sopenharmony_ci .modified = 1, 818c2ecf20Sopenharmony_ci .channel2 = IIO_MOD_LIGHT_UV, 828c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 838c2ecf20Sopenharmony_ci }, 848c2ecf20Sopenharmony_ci { 858c2ecf20Sopenharmony_ci .type = IIO_UVINDEX, 868c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic int veml6070_to_uv_index(unsigned val) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci /* 938c2ecf20Sopenharmony_ci * conversion of raw UV intensity values to UV index depends on 948c2ecf20Sopenharmony_ci * integration time (IT) and value of the resistor connected to 958c2ecf20Sopenharmony_ci * the RSET pin (default: 270 KOhm) 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci unsigned uvi[11] = { 988c2ecf20Sopenharmony_ci 187, 373, 560, /* low */ 998c2ecf20Sopenharmony_ci 746, 933, 1120, /* moderate */ 1008c2ecf20Sopenharmony_ci 1308, 1494, /* high */ 1018c2ecf20Sopenharmony_ci 1681, 1868, 2054}; /* very high */ 1028c2ecf20Sopenharmony_ci int i; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(uvi); i++) 1058c2ecf20Sopenharmony_ci if (val <= uvi[i]) 1068c2ecf20Sopenharmony_ci return i; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci return 11; /* extreme */ 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic int veml6070_read_raw(struct iio_dev *indio_dev, 1128c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 1138c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci struct veml6070_data *data = iio_priv(indio_dev); 1168c2ecf20Sopenharmony_ci int ret; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci switch (mask) { 1198c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 1208c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PROCESSED: 1218c2ecf20Sopenharmony_ci ret = veml6070_read(data); 1228c2ecf20Sopenharmony_ci if (ret < 0) 1238c2ecf20Sopenharmony_ci return ret; 1248c2ecf20Sopenharmony_ci if (mask == IIO_CHAN_INFO_PROCESSED) 1258c2ecf20Sopenharmony_ci *val = veml6070_to_uv_index(ret); 1268c2ecf20Sopenharmony_ci else 1278c2ecf20Sopenharmony_ci *val = ret; 1288c2ecf20Sopenharmony_ci return IIO_VAL_INT; 1298c2ecf20Sopenharmony_ci default: 1308c2ecf20Sopenharmony_ci return -EINVAL; 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic const struct iio_info veml6070_info = { 1358c2ecf20Sopenharmony_ci .read_raw = veml6070_read_raw, 1368c2ecf20Sopenharmony_ci}; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic int veml6070_probe(struct i2c_client *client, 1398c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct veml6070_data *data; 1428c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 1438c2ecf20Sopenharmony_ci int ret; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1468c2ecf20Sopenharmony_ci if (!indio_dev) 1478c2ecf20Sopenharmony_ci return -ENOMEM; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci data = iio_priv(indio_dev); 1508c2ecf20Sopenharmony_ci i2c_set_clientdata(client, indio_dev); 1518c2ecf20Sopenharmony_ci data->client1 = client; 1528c2ecf20Sopenharmony_ci mutex_init(&data->lock); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci indio_dev->info = &veml6070_info; 1558c2ecf20Sopenharmony_ci indio_dev->channels = veml6070_channels; 1568c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(veml6070_channels); 1578c2ecf20Sopenharmony_ci indio_dev->name = VEML6070_DRV_NAME; 1588c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci data->client2 = i2c_new_dummy_device(client->adapter, VEML6070_ADDR_DATA_LSB); 1618c2ecf20Sopenharmony_ci if (IS_ERR(data->client2)) { 1628c2ecf20Sopenharmony_ci dev_err(&client->dev, "i2c device for second chip address failed\n"); 1638c2ecf20Sopenharmony_ci return PTR_ERR(data->client2); 1648c2ecf20Sopenharmony_ci } 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci data->config = VEML6070_IT_10 | VEML6070_COMMAND_RSRVD | 1678c2ecf20Sopenharmony_ci VEML6070_COMMAND_SD; 1688c2ecf20Sopenharmony_ci ret = i2c_smbus_write_byte(data->client1, data->config); 1698c2ecf20Sopenharmony_ci if (ret < 0) 1708c2ecf20Sopenharmony_ci goto fail; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci ret = iio_device_register(indio_dev); 1738c2ecf20Sopenharmony_ci if (ret < 0) 1748c2ecf20Sopenharmony_ci goto fail; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return ret; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cifail: 1798c2ecf20Sopenharmony_ci i2c_unregister_device(data->client2); 1808c2ecf20Sopenharmony_ci return ret; 1818c2ecf20Sopenharmony_ci} 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_cistatic int veml6070_remove(struct i2c_client *client) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = i2c_get_clientdata(client); 1868c2ecf20Sopenharmony_ci struct veml6070_data *data = iio_priv(indio_dev); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci iio_device_unregister(indio_dev); 1898c2ecf20Sopenharmony_ci i2c_unregister_device(data->client2); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci return 0; 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic const struct i2c_device_id veml6070_id[] = { 1958c2ecf20Sopenharmony_ci { "veml6070", 0 }, 1968c2ecf20Sopenharmony_ci { } 1978c2ecf20Sopenharmony_ci}; 1988c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, veml6070_id); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic struct i2c_driver veml6070_driver = { 2018c2ecf20Sopenharmony_ci .driver = { 2028c2ecf20Sopenharmony_ci .name = VEML6070_DRV_NAME, 2038c2ecf20Sopenharmony_ci }, 2048c2ecf20Sopenharmony_ci .probe = veml6070_probe, 2058c2ecf20Sopenharmony_ci .remove = veml6070_remove, 2068c2ecf20Sopenharmony_ci .id_table = veml6070_id, 2078c2ecf20Sopenharmony_ci}; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cimodule_i2c_driver(veml6070_driver); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ciMODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); 2128c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Vishay VEML6070 UV A light sensor driver"); 2138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 214