1d2912cb1SThomas 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> 129065b780SJonathan Cameron #include <linux/mod_devicetable.h> 13971672c0SKevin Tsai #include <linux/interrupt.h> 14971672c0SKevin Tsai #include <linux/regulator/consumer.h> 15971672c0SKevin Tsai #include <linux/iio/iio.h> 16971672c0SKevin Tsai #include <linux/iio/sysfs.h> 17971672c0SKevin Tsai #include <linux/iio/events.h> 18971672c0SKevin Tsai #include <linux/init.h> 19971672c0SKevin Tsai 20971672c0SKevin Tsai /* Registers Address */ 21971672c0SKevin Tsai #define CM32181_REG_ADDR_CMD 0x00 22*3bf4a59cSHans de Goede #define CM32181_REG_ADDR_WH 0x01 23*3bf4a59cSHans de Goede #define CM32181_REG_ADDR_WL 0x02 24*3bf4a59cSHans de Goede #define CM32181_REG_ADDR_TEST 0x03 25971672c0SKevin Tsai #define CM32181_REG_ADDR_ALS 0x04 26971672c0SKevin Tsai #define CM32181_REG_ADDR_STATUS 0x06 27971672c0SKevin Tsai #define CM32181_REG_ADDR_ID 0x07 28971672c0SKevin Tsai 29971672c0SKevin Tsai /* Number of Configurable Registers */ 30971672c0SKevin Tsai #define CM32181_CONF_REG_NUM 0x01 31971672c0SKevin Tsai 32971672c0SKevin Tsai /* CMD register */ 33*3bf4a59cSHans de Goede #define CM32181_CMD_ALS_DISABLE BIT(0) 34*3bf4a59cSHans de Goede #define CM32181_CMD_ALS_INT_EN BIT(1) 35*3bf4a59cSHans de Goede #define CM32181_CMD_ALS_THRES_WINDOW BIT(2) 36*3bf4a59cSHans de Goede 37*3bf4a59cSHans de Goede #define CM32181_CMD_ALS_PERS_SHIFT 4 38*3bf4a59cSHans de Goede #define CM32181_CMD_ALS_PERS_MASK (0x03 << CM32181_CMD_ALS_PERS_SHIFT) 39*3bf4a59cSHans de Goede #define CM32181_CMD_ALS_PERS_DEFAULT (0x01 << CM32181_CMD_ALS_PERS_SHIFT) 40971672c0SKevin Tsai 41971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_SHIFT 6 42971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT) 43971672c0SKevin Tsai #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT) 44971672c0SKevin Tsai 45971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_SHIFT 11 46971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT) 47971672c0SKevin Tsai #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT) 48971672c0SKevin Tsai 49971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */ 50971672c0SKevin Tsai #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ 51971672c0SKevin Tsai #define CM32181_CALIBSCALE_DEFAULT 1000 52971672c0SKevin Tsai #define CM32181_CALIBSCALE_RESOLUTION 1000 53971672c0SKevin Tsai #define MLUX_PER_LUX 1000 54971672c0SKevin Tsai 55971672c0SKevin Tsai static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { 56971672c0SKevin Tsai CM32181_REG_ADDR_CMD, 57971672c0SKevin Tsai }; 58971672c0SKevin Tsai 59971672c0SKevin Tsai static const int als_it_bits[] = {12, 8, 0, 1, 2, 3}; 60971672c0SKevin Tsai static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000, 61971672c0SKevin Tsai 800000}; 62971672c0SKevin Tsai 63971672c0SKevin Tsai struct cm32181_chip { 64971672c0SKevin Tsai struct i2c_client *client; 65971672c0SKevin Tsai struct mutex lock; 66971672c0SKevin Tsai u16 conf_regs[CM32181_CONF_REG_NUM]; 67971672c0SKevin Tsai int calibscale; 68971672c0SKevin Tsai }; 69971672c0SKevin Tsai 70971672c0SKevin Tsai /** 71971672c0SKevin Tsai * cm32181_reg_init() - Initialize CM32181 registers 72971672c0SKevin Tsai * @cm32181: pointer of struct cm32181. 73971672c0SKevin Tsai * 74971672c0SKevin Tsai * Initialize CM32181 ambient light sensor register to default values. 75971672c0SKevin Tsai * 76971672c0SKevin Tsai * Return: 0 for success; otherwise for error code. 77971672c0SKevin Tsai */ 78971672c0SKevin Tsai static int cm32181_reg_init(struct cm32181_chip *cm32181) 79971672c0SKevin Tsai { 80971672c0SKevin Tsai struct i2c_client *client = cm32181->client; 81971672c0SKevin Tsai int i; 82971672c0SKevin Tsai s32 ret; 83971672c0SKevin Tsai 84971672c0SKevin Tsai ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID); 85971672c0SKevin Tsai if (ret < 0) 86971672c0SKevin Tsai return ret; 87971672c0SKevin Tsai 88971672c0SKevin Tsai /* check device ID */ 89971672c0SKevin Tsai if ((ret & 0xFF) != 0x81) 90971672c0SKevin Tsai return -ENODEV; 91971672c0SKevin Tsai 92971672c0SKevin Tsai /* Default Values */ 93*3bf4a59cSHans de Goede cm32181->conf_regs[CM32181_REG_ADDR_CMD] = 94971672c0SKevin Tsai CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; 95971672c0SKevin Tsai cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; 96971672c0SKevin Tsai 97971672c0SKevin Tsai /* Initialize registers*/ 98971672c0SKevin Tsai for (i = 0; i < CM32181_CONF_REG_NUM; i++) { 99971672c0SKevin Tsai ret = i2c_smbus_write_word_data(client, cm32181_reg[i], 100971672c0SKevin Tsai cm32181->conf_regs[i]); 101971672c0SKevin Tsai if (ret < 0) 102971672c0SKevin Tsai return ret; 103971672c0SKevin Tsai } 104971672c0SKevin Tsai 105971672c0SKevin Tsai return 0; 106971672c0SKevin Tsai } 107971672c0SKevin Tsai 108971672c0SKevin Tsai /** 109971672c0SKevin Tsai * cm32181_read_als_it() - Get sensor integration time (ms) 110971672c0SKevin Tsai * @cm32181: pointer of struct cm32181 1111463a166SBeomho Seo * @val2: pointer of int to load the als_it value. 112971672c0SKevin Tsai * 113971672c0SKevin Tsai * Report the current integartion time by millisecond. 114971672c0SKevin Tsai * 1151463a166SBeomho Seo * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL. 116971672c0SKevin Tsai */ 1171463a166SBeomho Seo static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2) 118971672c0SKevin Tsai { 119971672c0SKevin Tsai u16 als_it; 120971672c0SKevin Tsai int i; 121971672c0SKevin Tsai 122971672c0SKevin Tsai als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD]; 123971672c0SKevin Tsai als_it &= CM32181_CMD_ALS_IT_MASK; 124971672c0SKevin Tsai als_it >>= CM32181_CMD_ALS_IT_SHIFT; 125971672c0SKevin Tsai for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) { 126971672c0SKevin Tsai if (als_it == als_it_bits[i]) { 1271463a166SBeomho Seo *val2 = als_it_value[i]; 1281463a166SBeomho Seo return IIO_VAL_INT_PLUS_MICRO; 129971672c0SKevin Tsai } 130971672c0SKevin Tsai } 131971672c0SKevin Tsai 132971672c0SKevin Tsai return -EINVAL; 133971672c0SKevin Tsai } 134971672c0SKevin Tsai 135971672c0SKevin Tsai /** 136971672c0SKevin Tsai * cm32181_write_als_it() - Write sensor integration time 137971672c0SKevin Tsai * @cm32181: pointer of struct cm32181. 138971672c0SKevin Tsai * @val: integration time by millisecond. 139971672c0SKevin Tsai * 140971672c0SKevin Tsai * Convert integration time (ms) to sensor value. 141971672c0SKevin Tsai * 142971672c0SKevin Tsai * Return: i2c_smbus_write_word_data command return value. 143971672c0SKevin Tsai */ 144971672c0SKevin Tsai static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val) 145971672c0SKevin Tsai { 146971672c0SKevin Tsai struct i2c_client *client = cm32181->client; 147971672c0SKevin Tsai u16 als_it; 148971672c0SKevin Tsai int ret, i, n; 149971672c0SKevin Tsai 150971672c0SKevin Tsai n = ARRAY_SIZE(als_it_value); 151971672c0SKevin Tsai for (i = 0; i < n; i++) 152971672c0SKevin Tsai if (val <= als_it_value[i]) 153971672c0SKevin Tsai break; 154971672c0SKevin Tsai if (i >= n) 155971672c0SKevin Tsai i = n - 1; 156971672c0SKevin Tsai 157971672c0SKevin Tsai als_it = als_it_bits[i]; 158971672c0SKevin Tsai als_it <<= CM32181_CMD_ALS_IT_SHIFT; 159971672c0SKevin Tsai 160971672c0SKevin Tsai mutex_lock(&cm32181->lock); 161971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD] &= 162971672c0SKevin Tsai ~CM32181_CMD_ALS_IT_MASK; 163971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD] |= 164971672c0SKevin Tsai als_it; 165971672c0SKevin Tsai ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD, 166971672c0SKevin Tsai cm32181->conf_regs[CM32181_REG_ADDR_CMD]); 167971672c0SKevin Tsai mutex_unlock(&cm32181->lock); 168971672c0SKevin Tsai 169971672c0SKevin Tsai return ret; 170971672c0SKevin Tsai } 171971672c0SKevin Tsai 172971672c0SKevin Tsai /** 173971672c0SKevin Tsai * cm32181_get_lux() - report current lux value 174971672c0SKevin Tsai * @cm32181: pointer of struct cm32181. 175971672c0SKevin Tsai * 176971672c0SKevin Tsai * Convert sensor raw data to lux. It depends on integration 177a86ea557SRoberta Dobrescu * time and calibscale variable. 178971672c0SKevin Tsai * 179971672c0SKevin Tsai * Return: Positive value is lux, otherwise is error code. 180971672c0SKevin Tsai */ 181971672c0SKevin Tsai static int cm32181_get_lux(struct cm32181_chip *cm32181) 182971672c0SKevin Tsai { 183971672c0SKevin Tsai struct i2c_client *client = cm32181->client; 184971672c0SKevin Tsai int ret; 185971672c0SKevin Tsai int als_it; 186971672c0SKevin Tsai unsigned long lux; 187971672c0SKevin Tsai 188971672c0SKevin Tsai ret = cm32181_read_als_it(cm32181, &als_it); 189971672c0SKevin Tsai if (ret < 0) 190971672c0SKevin Tsai return -EINVAL; 191971672c0SKevin Tsai 192971672c0SKevin Tsai lux = CM32181_MLUX_PER_BIT; 193971672c0SKevin Tsai lux *= CM32181_MLUX_PER_BIT_BASE_IT; 194971672c0SKevin Tsai lux /= als_it; 195971672c0SKevin Tsai 196971672c0SKevin Tsai ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); 197971672c0SKevin Tsai if (ret < 0) 198971672c0SKevin Tsai return ret; 199971672c0SKevin Tsai 200971672c0SKevin Tsai lux *= ret; 201971672c0SKevin Tsai lux *= cm32181->calibscale; 202971672c0SKevin Tsai lux /= CM32181_CALIBSCALE_RESOLUTION; 203971672c0SKevin Tsai lux /= MLUX_PER_LUX; 204971672c0SKevin Tsai 205971672c0SKevin Tsai if (lux > 0xFFFF) 206971672c0SKevin Tsai lux = 0xFFFF; 207971672c0SKevin Tsai 208971672c0SKevin Tsai return lux; 209971672c0SKevin Tsai } 210971672c0SKevin Tsai 211971672c0SKevin Tsai static int cm32181_read_raw(struct iio_dev *indio_dev, 212971672c0SKevin Tsai struct iio_chan_spec const *chan, 213971672c0SKevin Tsai int *val, int *val2, long mask) 214971672c0SKevin Tsai { 215971672c0SKevin Tsai struct cm32181_chip *cm32181 = iio_priv(indio_dev); 216971672c0SKevin Tsai int ret; 217971672c0SKevin Tsai 218971672c0SKevin Tsai switch (mask) { 219971672c0SKevin Tsai case IIO_CHAN_INFO_PROCESSED: 220971672c0SKevin Tsai ret = cm32181_get_lux(cm32181); 221971672c0SKevin Tsai if (ret < 0) 222971672c0SKevin Tsai return ret; 223971672c0SKevin Tsai *val = ret; 224971672c0SKevin Tsai return IIO_VAL_INT; 225971672c0SKevin Tsai case IIO_CHAN_INFO_CALIBSCALE: 226971672c0SKevin Tsai *val = cm32181->calibscale; 227971672c0SKevin Tsai return IIO_VAL_INT; 228971672c0SKevin Tsai case IIO_CHAN_INFO_INT_TIME: 22941c897f8SBeomho Seo *val = 0; 2301463a166SBeomho Seo ret = cm32181_read_als_it(cm32181, val2); 231971672c0SKevin Tsai return ret; 232971672c0SKevin Tsai } 233971672c0SKevin Tsai 234971672c0SKevin Tsai return -EINVAL; 235971672c0SKevin Tsai } 236971672c0SKevin Tsai 237971672c0SKevin Tsai static int cm32181_write_raw(struct iio_dev *indio_dev, 238971672c0SKevin Tsai struct iio_chan_spec const *chan, 239971672c0SKevin Tsai int val, int val2, long mask) 240971672c0SKevin Tsai { 241971672c0SKevin Tsai struct cm32181_chip *cm32181 = iio_priv(indio_dev); 242971672c0SKevin Tsai int ret; 243971672c0SKevin Tsai 244971672c0SKevin Tsai switch (mask) { 245971672c0SKevin Tsai case IIO_CHAN_INFO_CALIBSCALE: 246971672c0SKevin Tsai cm32181->calibscale = val; 247971672c0SKevin Tsai return val; 248971672c0SKevin Tsai case IIO_CHAN_INFO_INT_TIME: 2491463a166SBeomho Seo ret = cm32181_write_als_it(cm32181, val2); 250971672c0SKevin Tsai return ret; 251971672c0SKevin Tsai } 252971672c0SKevin Tsai 253971672c0SKevin Tsai return -EINVAL; 254971672c0SKevin Tsai } 255971672c0SKevin Tsai 256971672c0SKevin Tsai /** 257971672c0SKevin Tsai * cm32181_get_it_available() - Get available ALS IT value 258971672c0SKevin Tsai * @dev: pointer of struct device. 259971672c0SKevin Tsai * @attr: pointer of struct device_attribute. 260971672c0SKevin Tsai * @buf: pointer of return string buffer. 261971672c0SKevin Tsai * 262971672c0SKevin Tsai * Display the available integration time values by millisecond. 263971672c0SKevin Tsai * 264971672c0SKevin Tsai * Return: string length. 265971672c0SKevin Tsai */ 266971672c0SKevin Tsai static ssize_t cm32181_get_it_available(struct device *dev, 267971672c0SKevin Tsai struct device_attribute *attr, char *buf) 268971672c0SKevin Tsai { 269971672c0SKevin Tsai int i, n, len; 270971672c0SKevin Tsai 271971672c0SKevin Tsai n = ARRAY_SIZE(als_it_value); 272971672c0SKevin Tsai for (i = 0, len = 0; i < n; i++) 2731463a166SBeomho Seo len += sprintf(buf + len, "0.%06u ", als_it_value[i]); 274971672c0SKevin Tsai return len + sprintf(buf + len, "\n"); 275971672c0SKevin Tsai } 276971672c0SKevin Tsai 277971672c0SKevin Tsai static const struct iio_chan_spec cm32181_channels[] = { 278971672c0SKevin Tsai { 279971672c0SKevin Tsai .type = IIO_LIGHT, 280971672c0SKevin Tsai .info_mask_separate = 281971672c0SKevin Tsai BIT(IIO_CHAN_INFO_PROCESSED) | 282971672c0SKevin Tsai BIT(IIO_CHAN_INFO_CALIBSCALE) | 283971672c0SKevin Tsai BIT(IIO_CHAN_INFO_INT_TIME), 284971672c0SKevin Tsai } 285971672c0SKevin Tsai }; 286971672c0SKevin Tsai 287971672c0SKevin Tsai static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, 288971672c0SKevin Tsai S_IRUGO, cm32181_get_it_available, NULL, 0); 289971672c0SKevin Tsai 290971672c0SKevin Tsai static struct attribute *cm32181_attributes[] = { 291971672c0SKevin Tsai &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr, 292971672c0SKevin Tsai NULL, 293971672c0SKevin Tsai }; 294971672c0SKevin Tsai 295971672c0SKevin Tsai static const struct attribute_group cm32181_attribute_group = { 296971672c0SKevin Tsai .attrs = cm32181_attributes 297971672c0SKevin Tsai }; 298971672c0SKevin Tsai 299971672c0SKevin Tsai static const struct iio_info cm32181_info = { 300971672c0SKevin Tsai .read_raw = &cm32181_read_raw, 301971672c0SKevin Tsai .write_raw = &cm32181_write_raw, 302971672c0SKevin Tsai .attrs = &cm32181_attribute_group, 303971672c0SKevin Tsai }; 304971672c0SKevin Tsai 30506770454SHans de Goede static int cm32181_probe(struct i2c_client *client) 306971672c0SKevin Tsai { 307971672c0SKevin Tsai struct cm32181_chip *cm32181; 308971672c0SKevin Tsai struct iio_dev *indio_dev; 309971672c0SKevin Tsai int ret; 310971672c0SKevin Tsai 311971672c0SKevin Tsai indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181)); 312971672c0SKevin Tsai if (!indio_dev) { 313971672c0SKevin Tsai dev_err(&client->dev, "devm_iio_device_alloc failed\n"); 314971672c0SKevin Tsai return -ENOMEM; 315971672c0SKevin Tsai } 316971672c0SKevin Tsai 317971672c0SKevin Tsai cm32181 = iio_priv(indio_dev); 318971672c0SKevin Tsai i2c_set_clientdata(client, indio_dev); 319971672c0SKevin Tsai cm32181->client = client; 320971672c0SKevin Tsai 321971672c0SKevin Tsai mutex_init(&cm32181->lock); 322971672c0SKevin Tsai indio_dev->dev.parent = &client->dev; 323971672c0SKevin Tsai indio_dev->channels = cm32181_channels; 324971672c0SKevin Tsai indio_dev->num_channels = ARRAY_SIZE(cm32181_channels); 325971672c0SKevin Tsai indio_dev->info = &cm32181_info; 32606770454SHans de Goede indio_dev->name = dev_name(&client->dev); 327971672c0SKevin Tsai indio_dev->modes = INDIO_DIRECT_MODE; 328971672c0SKevin Tsai 329971672c0SKevin Tsai ret = cm32181_reg_init(cm32181); 330971672c0SKevin Tsai if (ret) { 331971672c0SKevin Tsai dev_err(&client->dev, 332971672c0SKevin Tsai "%s: register init failed\n", 333971672c0SKevin Tsai __func__); 334971672c0SKevin Tsai return ret; 335971672c0SKevin Tsai } 336971672c0SKevin Tsai 337b9785535SBeomho Seo ret = devm_iio_device_register(&client->dev, indio_dev); 338971672c0SKevin Tsai if (ret) { 339971672c0SKevin Tsai dev_err(&client->dev, 340971672c0SKevin Tsai "%s: regist device failed\n", 341971672c0SKevin Tsai __func__); 342971672c0SKevin Tsai return ret; 343971672c0SKevin Tsai } 344971672c0SKevin Tsai 345971672c0SKevin Tsai return 0; 346971672c0SKevin Tsai } 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 35444b9409cSHans de Goede #ifdef CONFIG_ACPI 35544b9409cSHans de Goede static const struct acpi_device_id cm32181_acpi_match[] = { 35644b9409cSHans de Goede { "CPLM3218", 0 }, 35744b9409cSHans de Goede { } 35844b9409cSHans de Goede }; 35944b9409cSHans de Goede MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match); 36044b9409cSHans de Goede #endif 36144b9409cSHans de Goede 362971672c0SKevin Tsai static struct i2c_driver cm32181_driver = { 363971672c0SKevin Tsai .driver = { 364971672c0SKevin Tsai .name = "cm32181", 36544b9409cSHans de Goede .acpi_match_table = ACPI_PTR(cm32181_acpi_match), 3669065b780SJonathan Cameron .of_match_table = cm32181_of_match, 367971672c0SKevin Tsai }, 36806770454SHans de Goede .probe_new = cm32181_probe, 369971672c0SKevin Tsai }; 370971672c0SKevin Tsai 371971672c0SKevin Tsai module_i2c_driver(cm32181_driver); 372971672c0SKevin Tsai 373971672c0SKevin Tsai MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>"); 374971672c0SKevin Tsai MODULE_DESCRIPTION("CM32181 ambient light sensor driver"); 375971672c0SKevin Tsai MODULE_LICENSE("GPL"); 376