1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2971672c0SKevin Tsai /* 3971672c0SKevin Tsai * Copyright (C) 2013 Capella Microsystems Inc. 4971672c0SKevin Tsai * Author: Kevin Tsai <ktsai@capellamicro.com> 5971672c0SKevin Tsai */ 6971672c0SKevin Tsai 7971672c0SKevin Tsai #include <linux/delay.h> 8971672c0SKevin Tsai #include <linux/err.h> 9971672c0SKevin Tsai #include <linux/i2c.h> 10971672c0SKevin Tsai #include <linux/mutex.h> 11971672c0SKevin Tsai #include <linux/module.h> 12971672c0SKevin Tsai #include <linux/interrupt.h> 13971672c0SKevin Tsai #include <linux/regulator/consumer.h> 14971672c0SKevin Tsai #include <linux/iio/iio.h> 15971672c0SKevin Tsai #include <linux/iio/sysfs.h> 16971672c0SKevin Tsai #include <linux/iio/events.h> 17971672c0SKevin Tsai #include <linux/init.h> 18971672c0SKevin Tsai 19971672c0SKevin Tsai /* Registers Address */ 20971672c0SKevin Tsai #define CM32181_REG_ADDR_CMD 0x00 21971672c0SKevin Tsai #define CM32181_REG_ADDR_ALS 0x04 22971672c0SKevin Tsai #define CM32181_REG_ADDR_STATUS 0x06 23971672c0SKevin Tsai #define CM32181_REG_ADDR_ID 0x07 24971672c0SKevin Tsai 25971672c0SKevin Tsai /* Number of Configurable Registers */ 26971672c0SKevin Tsai #define CM32181_CONF_REG_NUM 0x01 27971672c0SKevin Tsai 28971672c0SKevin Tsai /* CMD register */ 29971672c0SKevin Tsai #define CM32181_CMD_ALS_ENABLE 0x00 30971672c0SKevin Tsai #define CM32181_CMD_ALS_DISABLE 0x01 31971672c0SKevin Tsai #define CM32181_CMD_ALS_INT_EN 0x02 32971672c0SKevin Tsai 33971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_SHIFT 6 34971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT) 35971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT) 36971672c0SKevin Tsai 37971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_SHIFT 11 38971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT) 39971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT) 40971672c0SKevin Tsai 41971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */ 42971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ 43971672c0SKevin Tsai #define CM32181_CALIBSCALE_DEFAULT 1000 44971672c0SKevin Tsai #define CM32181_CALIBSCALE_RESOLUTION 1000 45971672c0SKevin Tsai #define MLUX_PER_LUX 1000 46971672c0SKevin Tsai 47971672c0SKevin Tsai static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { 48971672c0SKevin Tsai CM32181_REG_ADDR_CMD, 49971672c0SKevin Tsai }; 50971672c0SKevin Tsai 51971672c0SKevin Tsai static const int als_it_bits[] = {12, 8, 0, 1, 2, 3}; 52971672c0SKevin Tsai static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000, 53971672c0SKevin Tsai 800000}; 54971672c0SKevin Tsai 55971672c0SKevin Tsai struct cm32181_chip { 56971672c0SKevin Tsai struct i2c_client *client; 57971672c0SKevin Tsai struct mutex lock; 58971672c0SKevin Tsai u16 conf_regs[CM32181_CONF_REG_NUM]; 59971672c0SKevin Tsai int calibscale; 60971672c0SKevin Tsai }; 61971672c0SKevin Tsai 62971672c0SKevin Tsai /** 63971672c0SKevin Tsai * cm32181_reg_init() - Initialize CM32181 registers 64971672c0SKevin Tsai * @cm32181: pointer of struct cm32181. 65971672c0SKevin Tsai * 66971672c0SKevin Tsai * Initialize CM32181 ambient light sensor register to default values. 67971672c0SKevin Tsai * 68971672c0SKevin Tsai * Return: 0 for success; otherwise for error code. 69971672c0SKevin Tsai */ 70971672c0SKevin Tsai static int cm32181_reg_init(struct cm32181_chip *cm32181) 71971672c0SKevin Tsai { 72971672c0SKevin Tsai struct i2c_client *client = cm32181->client; 73971672c0SKevin Tsai int i; 74971672c0SKevin Tsai s32 ret; 75971672c0SKevin Tsai 76971672c0SKevin Tsai ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID); 77971672c0SKevin Tsai if (ret < 0) 78971672c0SKevin Tsai return ret; 79971672c0SKevin Tsai 80971672c0SKevin Tsai /* check device ID */ 81971672c0SKevin Tsai if ((ret & 0xFF) != 0x81) 82971672c0SKevin Tsai return -ENODEV; 83971672c0SKevin Tsai 84971672c0SKevin Tsai /* Default Values */ 85971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE | 86971672c0SKevin Tsai CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; 87971672c0SKevin Tsai cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; 88971672c0SKevin Tsai 89971672c0SKevin Tsai /* Initialize registers*/ 90971672c0SKevin Tsai for (i = 0; i < CM32181_CONF_REG_NUM; i++) { 91971672c0SKevin Tsai ret = i2c_smbus_write_word_data(client, cm32181_reg[i], 92971672c0SKevin Tsai cm32181->conf_regs[i]); 93971672c0SKevin Tsai if (ret < 0) 94971672c0SKevin Tsai return ret; 95971672c0SKevin Tsai } 96971672c0SKevin Tsai 97971672c0SKevin Tsai return 0; 98971672c0SKevin Tsai } 99971672c0SKevin Tsai 100971672c0SKevin Tsai /** 101971672c0SKevin Tsai * cm32181_read_als_it() - Get sensor integration time (ms) 102971672c0SKevin Tsai * @cm32181: pointer of struct cm32181 1031463a166SBeomho Seo * @val2: pointer of int to load the als_it value. 104971672c0SKevin Tsai * 105971672c0SKevin Tsai * Report the current integartion time by millisecond. 106971672c0SKevin Tsai * 1071463a166SBeomho Seo * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL. 108971672c0SKevin Tsai */ 1091463a166SBeomho Seo static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2) 110971672c0SKevin Tsai { 111971672c0SKevin Tsai u16 als_it; 112971672c0SKevin Tsai int i; 113971672c0SKevin Tsai 114971672c0SKevin Tsai als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD]; 115971672c0SKevin Tsai als_it &= CM32181_CMD_ALS_IT_MASK; 116971672c0SKevin Tsai als_it >>= CM32181_CMD_ALS_IT_SHIFT; 117971672c0SKevin Tsai for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) { 118971672c0SKevin Tsai if (als_it == als_it_bits[i]) { 1191463a166SBeomho Seo *val2 = als_it_value[i]; 1201463a166SBeomho Seo return IIO_VAL_INT_PLUS_MICRO; 121971672c0SKevin Tsai } 122971672c0SKevin Tsai } 123971672c0SKevin Tsai 124971672c0SKevin Tsai return -EINVAL; 125971672c0SKevin Tsai } 126971672c0SKevin Tsai 127971672c0SKevin Tsai /** 128971672c0SKevin Tsai * cm32181_write_als_it() - Write sensor integration time 129971672c0SKevin Tsai * @cm32181: pointer of struct cm32181. 130971672c0SKevin Tsai * @val: integration time by millisecond. 131971672c0SKevin Tsai * 132971672c0SKevin Tsai * Convert integration time (ms) to sensor value. 133971672c0SKevin Tsai * 134971672c0SKevin Tsai * Return: i2c_smbus_write_word_data command return value. 135971672c0SKevin Tsai */ 136971672c0SKevin Tsai static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val) 137971672c0SKevin Tsai { 138971672c0SKevin Tsai struct i2c_client *client = cm32181->client; 139971672c0SKevin Tsai u16 als_it; 140971672c0SKevin Tsai int ret, i, n; 141971672c0SKevin Tsai 142971672c0SKevin Tsai n = ARRAY_SIZE(als_it_value); 143971672c0SKevin Tsai for (i = 0; i < n; i++) 144971672c0SKevin Tsai if (val <= als_it_value[i]) 145971672c0SKevin Tsai break; 146971672c0SKevin Tsai if (i >= n) 147971672c0SKevin Tsai i = n - 1; 148971672c0SKevin Tsai 149971672c0SKevin Tsai als_it = als_it_bits[i]; 150971672c0SKevin Tsai als_it <<= CM32181_CMD_ALS_IT_SHIFT; 151971672c0SKevin Tsai 152971672c0SKevin Tsai mutex_lock(&cm32181->lock); 153971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD] &= 154971672c0SKevin Tsai ~CM32181_CMD_ALS_IT_MASK; 155971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD] |= 156971672c0SKevin Tsai als_it; 157971672c0SKevin Tsai ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD, 158971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD]); 159971672c0SKevin Tsai mutex_unlock(&cm32181->lock); 160971672c0SKevin Tsai 161971672c0SKevin Tsai return ret; 162971672c0SKevin Tsai } 163971672c0SKevin Tsai 164971672c0SKevin Tsai /** 165971672c0SKevin Tsai * cm32181_get_lux() - report current lux value 166971672c0SKevin Tsai * @cm32181: pointer of struct cm32181. 167971672c0SKevin Tsai * 168971672c0SKevin Tsai * Convert sensor raw data to lux. It depends on integration 169a86ea557SRoberta Dobrescu * time and calibscale variable. 170971672c0SKevin Tsai * 171971672c0SKevin Tsai * Return: Positive value is lux, otherwise is error code. 172971672c0SKevin Tsai */ 173971672c0SKevin Tsai static int cm32181_get_lux(struct cm32181_chip *cm32181) 174971672c0SKevin Tsai { 175971672c0SKevin Tsai struct i2c_client *client = cm32181->client; 176971672c0SKevin Tsai int ret; 177971672c0SKevin Tsai int als_it; 178971672c0SKevin Tsai unsigned long lux; 179971672c0SKevin Tsai 180971672c0SKevin Tsai ret = cm32181_read_als_it(cm32181, &als_it); 181971672c0SKevin Tsai if (ret < 0) 182971672c0SKevin Tsai return -EINVAL; 183971672c0SKevin Tsai 184971672c0SKevin Tsai lux = CM32181_MLUX_PER_BIT; 185971672c0SKevin Tsai lux *= CM32181_MLUX_PER_BIT_BASE_IT; 186971672c0SKevin Tsai lux /= als_it; 187971672c0SKevin Tsai 188971672c0SKevin Tsai ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); 189971672c0SKevin Tsai if (ret < 0) 190971672c0SKevin Tsai return ret; 191971672c0SKevin Tsai 192971672c0SKevin Tsai lux *= ret; 193971672c0SKevin Tsai lux *= cm32181->calibscale; 194971672c0SKevin Tsai lux /= CM32181_CALIBSCALE_RESOLUTION; 195971672c0SKevin Tsai lux /= MLUX_PER_LUX; 196971672c0SKevin Tsai 197971672c0SKevin Tsai if (lux > 0xFFFF) 198971672c0SKevin Tsai lux = 0xFFFF; 199971672c0SKevin Tsai 200971672c0SKevin Tsai return lux; 201971672c0SKevin Tsai } 202971672c0SKevin Tsai 203971672c0SKevin Tsai static int cm32181_read_raw(struct iio_dev *indio_dev, 204971672c0SKevin Tsai struct iio_chan_spec const *chan, 205971672c0SKevin Tsai int *val, int *val2, long mask) 206971672c0SKevin Tsai { 207971672c0SKevin Tsai struct cm32181_chip *cm32181 = iio_priv(indio_dev); 208971672c0SKevin Tsai int ret; 209971672c0SKevin Tsai 210971672c0SKevin Tsai switch (mask) { 211971672c0SKevin Tsai case IIO_CHAN_INFO_PROCESSED: 212971672c0SKevin Tsai ret = cm32181_get_lux(cm32181); 213971672c0SKevin Tsai if (ret < 0) 214971672c0SKevin Tsai return ret; 215971672c0SKevin Tsai *val = ret; 216971672c0SKevin Tsai return IIO_VAL_INT; 217971672c0SKevin Tsai case IIO_CHAN_INFO_CALIBSCALE: 218971672c0SKevin Tsai *val = cm32181->calibscale; 219971672c0SKevin Tsai return IIO_VAL_INT; 220971672c0SKevin Tsai case IIO_CHAN_INFO_INT_TIME: 22141c897f8SBeomho Seo *val = 0; 2221463a166SBeomho Seo ret = cm32181_read_als_it(cm32181, val2); 223971672c0SKevin Tsai return ret; 224971672c0SKevin Tsai } 225971672c0SKevin Tsai 226971672c0SKevin Tsai return -EINVAL; 227971672c0SKevin Tsai } 228971672c0SKevin Tsai 229971672c0SKevin Tsai static int cm32181_write_raw(struct iio_dev *indio_dev, 230971672c0SKevin Tsai struct iio_chan_spec const *chan, 231971672c0SKevin Tsai int val, int val2, long mask) 232971672c0SKevin Tsai { 233971672c0SKevin Tsai struct cm32181_chip *cm32181 = iio_priv(indio_dev); 234971672c0SKevin Tsai int ret; 235971672c0SKevin Tsai 236971672c0SKevin Tsai switch (mask) { 237971672c0SKevin Tsai case IIO_CHAN_INFO_CALIBSCALE: 238971672c0SKevin Tsai cm32181->calibscale = val; 239971672c0SKevin Tsai return val; 240971672c0SKevin Tsai case IIO_CHAN_INFO_INT_TIME: 2411463a166SBeomho Seo ret = cm32181_write_als_it(cm32181, val2); 242971672c0SKevin Tsai return ret; 243971672c0SKevin Tsai } 244971672c0SKevin Tsai 245971672c0SKevin Tsai return -EINVAL; 246971672c0SKevin Tsai } 247971672c0SKevin Tsai 248971672c0SKevin Tsai /** 249971672c0SKevin Tsai * cm32181_get_it_available() - Get available ALS IT value 250971672c0SKevin Tsai * @dev: pointer of struct device. 251971672c0SKevin Tsai * @attr: pointer of struct device_attribute. 252971672c0SKevin Tsai * @buf: pointer of return string buffer. 253971672c0SKevin Tsai * 254971672c0SKevin Tsai * Display the available integration time values by millisecond. 255971672c0SKevin Tsai * 256971672c0SKevin Tsai * Return: string length. 257971672c0SKevin Tsai */ 258971672c0SKevin Tsai static ssize_t cm32181_get_it_available(struct device *dev, 259971672c0SKevin Tsai struct device_attribute *attr, char *buf) 260971672c0SKevin Tsai { 261971672c0SKevin Tsai int i, n, len; 262971672c0SKevin Tsai 263971672c0SKevin Tsai n = ARRAY_SIZE(als_it_value); 264971672c0SKevin Tsai for (i = 0, len = 0; i < n; i++) 2651463a166SBeomho Seo len += sprintf(buf + len, "0.%06u ", als_it_value[i]); 266971672c0SKevin Tsai return len + sprintf(buf + len, "\n"); 267971672c0SKevin Tsai } 268971672c0SKevin Tsai 269971672c0SKevin Tsai static const struct iio_chan_spec cm32181_channels[] = { 270971672c0SKevin Tsai { 271971672c0SKevin Tsai .type = IIO_LIGHT, 272971672c0SKevin Tsai .info_mask_separate = 273971672c0SKevin Tsai BIT(IIO_CHAN_INFO_PROCESSED) | 274971672c0SKevin Tsai BIT(IIO_CHAN_INFO_CALIBSCALE) | 275971672c0SKevin Tsai BIT(IIO_CHAN_INFO_INT_TIME), 276971672c0SKevin Tsai } 277971672c0SKevin Tsai }; 278971672c0SKevin Tsai 279971672c0SKevin Tsai static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, 280971672c0SKevin Tsai S_IRUGO, cm32181_get_it_available, NULL, 0); 281971672c0SKevin Tsai 282971672c0SKevin Tsai static struct attribute *cm32181_attributes[] = { 283971672c0SKevin Tsai &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr, 284971672c0SKevin Tsai NULL, 285971672c0SKevin Tsai }; 286971672c0SKevin Tsai 287971672c0SKevin Tsai static const struct attribute_group cm32181_attribute_group = { 288971672c0SKevin Tsai .attrs = cm32181_attributes 289971672c0SKevin Tsai }; 290971672c0SKevin Tsai 291971672c0SKevin Tsai static const struct iio_info cm32181_info = { 292971672c0SKevin Tsai .read_raw = &cm32181_read_raw, 293971672c0SKevin Tsai .write_raw = &cm32181_write_raw, 294971672c0SKevin Tsai .attrs = &cm32181_attribute_group, 295971672c0SKevin Tsai }; 296971672c0SKevin Tsai 297971672c0SKevin Tsai static int cm32181_probe(struct i2c_client *client, 298971672c0SKevin Tsai const struct i2c_device_id *id) 299971672c0SKevin Tsai { 300971672c0SKevin Tsai struct cm32181_chip *cm32181; 301971672c0SKevin Tsai struct iio_dev *indio_dev; 302971672c0SKevin Tsai int ret; 303971672c0SKevin Tsai 304971672c0SKevin Tsai indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181)); 305971672c0SKevin Tsai if (!indio_dev) { 306971672c0SKevin Tsai dev_err(&client->dev, "devm_iio_device_alloc failed\n"); 307971672c0SKevin Tsai return -ENOMEM; 308971672c0SKevin Tsai } 309971672c0SKevin Tsai 310971672c0SKevin Tsai cm32181 = iio_priv(indio_dev); 311971672c0SKevin Tsai i2c_set_clientdata(client, indio_dev); 312971672c0SKevin Tsai cm32181->client = client; 313971672c0SKevin Tsai 314971672c0SKevin Tsai mutex_init(&cm32181->lock); 315971672c0SKevin Tsai indio_dev->dev.parent = &client->dev; 316971672c0SKevin Tsai indio_dev->channels = cm32181_channels; 317971672c0SKevin Tsai indio_dev->num_channels = ARRAY_SIZE(cm32181_channels); 318971672c0SKevin Tsai indio_dev->info = &cm32181_info; 319971672c0SKevin Tsai indio_dev->name = id->name; 320971672c0SKevin Tsai indio_dev->modes = INDIO_DIRECT_MODE; 321971672c0SKevin Tsai 322971672c0SKevin Tsai ret = cm32181_reg_init(cm32181); 323971672c0SKevin Tsai if (ret) { 324971672c0SKevin Tsai dev_err(&client->dev, 325971672c0SKevin Tsai "%s: register init failed\n", 326971672c0SKevin Tsai __func__); 327971672c0SKevin Tsai return ret; 328971672c0SKevin Tsai } 329971672c0SKevin Tsai 330b9785535SBeomho Seo ret = devm_iio_device_register(&client->dev, indio_dev); 331971672c0SKevin Tsai if (ret) { 332971672c0SKevin Tsai dev_err(&client->dev, 333971672c0SKevin Tsai "%s: regist device failed\n", 334971672c0SKevin Tsai __func__); 335971672c0SKevin Tsai return ret; 336971672c0SKevin Tsai } 337971672c0SKevin Tsai 338971672c0SKevin Tsai return 0; 339971672c0SKevin Tsai } 340971672c0SKevin Tsai 341971672c0SKevin Tsai static const struct i2c_device_id cm32181_id[] = { 342971672c0SKevin Tsai { "cm32181", 0 }, 343971672c0SKevin Tsai { } 344971672c0SKevin Tsai }; 345971672c0SKevin Tsai 346971672c0SKevin Tsai MODULE_DEVICE_TABLE(i2c, cm32181_id); 347971672c0SKevin Tsai 348971672c0SKevin Tsai static const struct of_device_id cm32181_of_match[] = { 349971672c0SKevin Tsai { .compatible = "capella,cm32181" }, 350971672c0SKevin Tsai { } 351971672c0SKevin Tsai }; 352119c4fceSJavier Martinez Canillas MODULE_DEVICE_TABLE(of, cm32181_of_match); 353971672c0SKevin Tsai 354971672c0SKevin Tsai static struct i2c_driver cm32181_driver = { 355971672c0SKevin Tsai .driver = { 356971672c0SKevin Tsai .name = "cm32181", 357971672c0SKevin Tsai .of_match_table = of_match_ptr(cm32181_of_match), 358971672c0SKevin Tsai }, 359971672c0SKevin Tsai .id_table = cm32181_id, 360971672c0SKevin Tsai .probe = cm32181_probe, 361971672c0SKevin Tsai }; 362971672c0SKevin Tsai 363971672c0SKevin Tsai module_i2c_driver(cm32181_driver); 364971672c0SKevin Tsai 365971672c0SKevin Tsai MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>"); 366971672c0SKevin Tsai MODULE_DESCRIPTION("CM32181 ambient light sensor driver"); 367971672c0SKevin Tsai MODULE_LICENSE("GPL"); 368