1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013 Capella Microsystems Inc. 4 * Author: Kevin Tsai <ktsai@capellamicro.com> 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/err.h> 9 #include <linux/i2c.h> 10 #include <linux/mutex.h> 11 #include <linux/module.h> 12 #include <linux/mod_devicetable.h> 13 #include <linux/interrupt.h> 14 #include <linux/regulator/consumer.h> 15 #include <linux/iio/iio.h> 16 #include <linux/iio/sysfs.h> 17 #include <linux/iio/events.h> 18 #include <linux/init.h> 19 20 /* Registers Address */ 21 #define CM32181_REG_ADDR_CMD 0x00 22 #define CM32181_REG_ADDR_WH 0x01 23 #define CM32181_REG_ADDR_WL 0x02 24 #define CM32181_REG_ADDR_TEST 0x03 25 #define CM32181_REG_ADDR_ALS 0x04 26 #define CM32181_REG_ADDR_STATUS 0x06 27 #define CM32181_REG_ADDR_ID 0x07 28 29 /* Number of Configurable Registers */ 30 #define CM32181_CONF_REG_NUM 0x01 31 32 /* CMD register */ 33 #define CM32181_CMD_ALS_DISABLE BIT(0) 34 #define CM32181_CMD_ALS_INT_EN BIT(1) 35 #define CM32181_CMD_ALS_THRES_WINDOW BIT(2) 36 37 #define CM32181_CMD_ALS_PERS_SHIFT 4 38 #define CM32181_CMD_ALS_PERS_MASK (0x03 << CM32181_CMD_ALS_PERS_SHIFT) 39 #define CM32181_CMD_ALS_PERS_DEFAULT (0x01 << CM32181_CMD_ALS_PERS_SHIFT) 40 41 #define CM32181_CMD_ALS_IT_SHIFT 6 42 #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT) 43 #define CM32181_CMD_ALS_IT_DEFAULT (0x00 << CM32181_CMD_ALS_IT_SHIFT) 44 45 #define CM32181_CMD_ALS_SM_SHIFT 11 46 #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT) 47 #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT) 48 49 #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */ 50 #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ 51 #define CM32181_CALIBSCALE_DEFAULT 1000 52 #define CM32181_CALIBSCALE_RESOLUTION 1000 53 #define MLUX_PER_LUX 1000 54 55 static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { 56 CM32181_REG_ADDR_CMD, 57 }; 58 59 /* CM3218 Family */ 60 static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 }; 61 static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 }; 62 63 /* CM32181 Family */ 64 static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 }; 65 static const int cm32181_als_it_values[] = { 66 25000, 50000, 100000, 200000, 400000, 800000 67 }; 68 69 struct cm32181_chip { 70 struct i2c_client *client; 71 struct mutex lock; 72 u16 conf_regs[CM32181_CONF_REG_NUM]; 73 int calibscale; 74 int num_als_it; 75 const int *als_it_bits; 76 const int *als_it_values; 77 }; 78 79 /** 80 * cm32181_reg_init() - Initialize CM32181 registers 81 * @cm32181: pointer of struct cm32181. 82 * 83 * Initialize CM32181 ambient light sensor register to default values. 84 * 85 * Return: 0 for success; otherwise for error code. 86 */ 87 static int cm32181_reg_init(struct cm32181_chip *cm32181) 88 { 89 struct i2c_client *client = cm32181->client; 90 int i; 91 s32 ret; 92 93 ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ID); 94 if (ret < 0) 95 return ret; 96 97 /* check device ID */ 98 switch (ret & 0xFF) { 99 case 0x18: /* CM3218 */ 100 cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits); 101 cm32181->als_it_bits = cm3218_als_it_bits; 102 cm32181->als_it_values = cm3218_als_it_values; 103 break; 104 case 0x81: /* CM32181 */ 105 case 0x82: /* CM32182, fully compat. with CM32181 */ 106 cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits); 107 cm32181->als_it_bits = cm32181_als_it_bits; 108 cm32181->als_it_values = cm32181_als_it_values; 109 break; 110 default: 111 return -ENODEV; 112 } 113 114 /* Default Values */ 115 cm32181->conf_regs[CM32181_REG_ADDR_CMD] = 116 CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; 117 cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; 118 119 /* Initialize registers*/ 120 for (i = 0; i < CM32181_CONF_REG_NUM; i++) { 121 ret = i2c_smbus_write_word_data(client, cm32181_reg[i], 122 cm32181->conf_regs[i]); 123 if (ret < 0) 124 return ret; 125 } 126 127 return 0; 128 } 129 130 /** 131 * cm32181_read_als_it() - Get sensor integration time (ms) 132 * @cm32181: pointer of struct cm32181 133 * @val2: pointer of int to load the als_it value. 134 * 135 * Report the current integartion time by millisecond. 136 * 137 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL. 138 */ 139 static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2) 140 { 141 u16 als_it; 142 int i; 143 144 als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD]; 145 als_it &= CM32181_CMD_ALS_IT_MASK; 146 als_it >>= CM32181_CMD_ALS_IT_SHIFT; 147 for (i = 0; i < cm32181->num_als_it; i++) { 148 if (als_it == cm32181->als_it_bits[i]) { 149 *val2 = cm32181->als_it_values[i]; 150 return IIO_VAL_INT_PLUS_MICRO; 151 } 152 } 153 154 return -EINVAL; 155 } 156 157 /** 158 * cm32181_write_als_it() - Write sensor integration time 159 * @cm32181: pointer of struct cm32181. 160 * @val: integration time by millisecond. 161 * 162 * Convert integration time (ms) to sensor value. 163 * 164 * Return: i2c_smbus_write_word_data command return value. 165 */ 166 static int cm32181_write_als_it(struct cm32181_chip *cm32181, int val) 167 { 168 struct i2c_client *client = cm32181->client; 169 u16 als_it; 170 int ret, i, n; 171 172 n = cm32181->num_als_it; 173 for (i = 0; i < n; i++) 174 if (val <= cm32181->als_it_values[i]) 175 break; 176 if (i >= n) 177 i = n - 1; 178 179 als_it = cm32181->als_it_bits[i]; 180 als_it <<= CM32181_CMD_ALS_IT_SHIFT; 181 182 mutex_lock(&cm32181->lock); 183 cm32181->conf_regs[CM32181_REG_ADDR_CMD] &= 184 ~CM32181_CMD_ALS_IT_MASK; 185 cm32181->conf_regs[CM32181_REG_ADDR_CMD] |= 186 als_it; 187 ret = i2c_smbus_write_word_data(client, CM32181_REG_ADDR_CMD, 188 cm32181->conf_regs[CM32181_REG_ADDR_CMD]); 189 mutex_unlock(&cm32181->lock); 190 191 return ret; 192 } 193 194 /** 195 * cm32181_get_lux() - report current lux value 196 * @cm32181: pointer of struct cm32181. 197 * 198 * Convert sensor raw data to lux. It depends on integration 199 * time and calibscale variable. 200 * 201 * Return: Positive value is lux, otherwise is error code. 202 */ 203 static int cm32181_get_lux(struct cm32181_chip *cm32181) 204 { 205 struct i2c_client *client = cm32181->client; 206 int ret; 207 int als_it; 208 unsigned long lux; 209 210 ret = cm32181_read_als_it(cm32181, &als_it); 211 if (ret < 0) 212 return -EINVAL; 213 214 lux = CM32181_MLUX_PER_BIT; 215 lux *= CM32181_MLUX_PER_BIT_BASE_IT; 216 lux /= als_it; 217 218 ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); 219 if (ret < 0) 220 return ret; 221 222 lux *= ret; 223 lux *= cm32181->calibscale; 224 lux /= CM32181_CALIBSCALE_RESOLUTION; 225 lux /= MLUX_PER_LUX; 226 227 if (lux > 0xFFFF) 228 lux = 0xFFFF; 229 230 return lux; 231 } 232 233 static int cm32181_read_raw(struct iio_dev *indio_dev, 234 struct iio_chan_spec const *chan, 235 int *val, int *val2, long mask) 236 { 237 struct cm32181_chip *cm32181 = iio_priv(indio_dev); 238 int ret; 239 240 switch (mask) { 241 case IIO_CHAN_INFO_PROCESSED: 242 ret = cm32181_get_lux(cm32181); 243 if (ret < 0) 244 return ret; 245 *val = ret; 246 return IIO_VAL_INT; 247 case IIO_CHAN_INFO_CALIBSCALE: 248 *val = cm32181->calibscale; 249 return IIO_VAL_INT; 250 case IIO_CHAN_INFO_INT_TIME: 251 *val = 0; 252 ret = cm32181_read_als_it(cm32181, val2); 253 return ret; 254 } 255 256 return -EINVAL; 257 } 258 259 static int cm32181_write_raw(struct iio_dev *indio_dev, 260 struct iio_chan_spec const *chan, 261 int val, int val2, long mask) 262 { 263 struct cm32181_chip *cm32181 = iio_priv(indio_dev); 264 int ret; 265 266 switch (mask) { 267 case IIO_CHAN_INFO_CALIBSCALE: 268 cm32181->calibscale = val; 269 return val; 270 case IIO_CHAN_INFO_INT_TIME: 271 ret = cm32181_write_als_it(cm32181, val2); 272 return ret; 273 } 274 275 return -EINVAL; 276 } 277 278 /** 279 * cm32181_get_it_available() - Get available ALS IT value 280 * @dev: pointer of struct device. 281 * @attr: pointer of struct device_attribute. 282 * @buf: pointer of return string buffer. 283 * 284 * Display the available integration time values by millisecond. 285 * 286 * Return: string length. 287 */ 288 static ssize_t cm32181_get_it_available(struct device *dev, 289 struct device_attribute *attr, char *buf) 290 { 291 struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev)); 292 int i, n, len; 293 294 n = cm32181->num_als_it; 295 for (i = 0, len = 0; i < n; i++) 296 len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]); 297 return len + sprintf(buf + len, "\n"); 298 } 299 300 static const struct iio_chan_spec cm32181_channels[] = { 301 { 302 .type = IIO_LIGHT, 303 .info_mask_separate = 304 BIT(IIO_CHAN_INFO_PROCESSED) | 305 BIT(IIO_CHAN_INFO_CALIBSCALE) | 306 BIT(IIO_CHAN_INFO_INT_TIME), 307 } 308 }; 309 310 static IIO_DEVICE_ATTR(in_illuminance_integration_time_available, 311 S_IRUGO, cm32181_get_it_available, NULL, 0); 312 313 static struct attribute *cm32181_attributes[] = { 314 &iio_dev_attr_in_illuminance_integration_time_available.dev_attr.attr, 315 NULL, 316 }; 317 318 static const struct attribute_group cm32181_attribute_group = { 319 .attrs = cm32181_attributes 320 }; 321 322 static const struct iio_info cm32181_info = { 323 .read_raw = &cm32181_read_raw, 324 .write_raw = &cm32181_write_raw, 325 .attrs = &cm32181_attribute_group, 326 }; 327 328 static int cm32181_probe(struct i2c_client *client) 329 { 330 struct cm32181_chip *cm32181; 331 struct iio_dev *indio_dev; 332 int ret; 333 334 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181)); 335 if (!indio_dev) { 336 dev_err(&client->dev, "devm_iio_device_alloc failed\n"); 337 return -ENOMEM; 338 } 339 340 cm32181 = iio_priv(indio_dev); 341 i2c_set_clientdata(client, indio_dev); 342 cm32181->client = client; 343 344 mutex_init(&cm32181->lock); 345 indio_dev->dev.parent = &client->dev; 346 indio_dev->channels = cm32181_channels; 347 indio_dev->num_channels = ARRAY_SIZE(cm32181_channels); 348 indio_dev->info = &cm32181_info; 349 indio_dev->name = dev_name(&client->dev); 350 indio_dev->modes = INDIO_DIRECT_MODE; 351 352 ret = cm32181_reg_init(cm32181); 353 if (ret) { 354 dev_err(&client->dev, 355 "%s: register init failed\n", 356 __func__); 357 return ret; 358 } 359 360 ret = devm_iio_device_register(&client->dev, indio_dev); 361 if (ret) { 362 dev_err(&client->dev, 363 "%s: regist device failed\n", 364 __func__); 365 return ret; 366 } 367 368 return 0; 369 } 370 371 static const struct of_device_id cm32181_of_match[] = { 372 { .compatible = "capella,cm3218" }, 373 { .compatible = "capella,cm32181" }, 374 { } 375 }; 376 MODULE_DEVICE_TABLE(of, cm32181_of_match); 377 378 #ifdef CONFIG_ACPI 379 static const struct acpi_device_id cm32181_acpi_match[] = { 380 { "CPLM3218", 0 }, 381 { } 382 }; 383 MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match); 384 #endif 385 386 static struct i2c_driver cm32181_driver = { 387 .driver = { 388 .name = "cm32181", 389 .acpi_match_table = ACPI_PTR(cm32181_acpi_match), 390 .of_match_table = cm32181_of_match, 391 }, 392 .probe_new = cm32181_probe, 393 }; 394 395 module_i2c_driver(cm32181_driver); 396 397 MODULE_AUTHOR("Kevin Tsai <ktsai@capellamicro.com>"); 398 MODULE_DESCRIPTION("CM32181 ambient light sensor driver"); 399 MODULE_LICENSE("GPL"); 400