1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * LTRF216A Ambient Light Sensor 4 * 5 * Copyright (C) 2022 Collabora, Ltd. 6 * Author: Shreeya Patel <shreeya.patel@collabora.com> 7 * 8 * Copyright (C) 2021 Lite-On Technology Corp (Singapore) 9 * Author: Shi Zhigang <Zhigang.Shi@liteon.com> 10 * 11 * IIO driver for LTRF216A (7-bit I2C slave address 0x53). 12 */ 13 14 #include <linux/bitfield.h> 15 #include <linux/bits.h> 16 #include <linux/delay.h> 17 #include <linux/i2c.h> 18 #include <linux/init.h> 19 #include <linux/iopoll.h> 20 #include <linux/module.h> 21 #include <linux/mutex.h> 22 #include <linux/pm.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/regmap.h> 25 26 #include <linux/iio/iio.h> 27 28 #include <linux/unaligned.h> 29 30 #define LTRF216A_ALS_RESET_MASK BIT(4) 31 #define LTRF216A_ALS_DATA_STATUS BIT(3) 32 #define LTRF216A_ALS_ENABLE_MASK BIT(1) 33 #define LTRF216A_MAIN_CTRL 0x00 34 #define LTRF216A_ALS_MEAS_RES 0x04 35 #define LTRF216A_ALS_GAIN 0x05 36 #define LTRF216A_PART_ID 0x06 37 #define LTRF216A_MAIN_STATUS 0x07 38 #define LTRF216A_ALS_CLEAR_DATA_0 0x0a 39 #define LTRF216A_ALS_CLEAR_DATA_1 0x0b 40 #define LTRF216A_ALS_CLEAR_DATA_2 0x0c 41 #define LTRF216A_ALS_DATA_0 0x0d 42 #define LTRF216A_ALS_DATA_1 0x0e 43 #define LTRF216A_ALS_DATA_2 0x0f 44 #define LTRF216A_INT_CFG 0x19 45 #define LTRF216A_INT_PST 0x1a 46 #define LTRF216A_ALS_THRES_UP_0 0x21 47 #define LTRF216A_ALS_THRES_UP_1 0x22 48 #define LTRF216A_ALS_THRES_UP_2 0x23 49 #define LTRF216A_ALS_THRES_LOW_0 0x24 50 #define LTRF216A_ALS_THRES_LOW_1 0x25 51 #define LTRF216A_ALS_THRES_LOW_2 0x26 52 #define LTRF216A_ALS_READ_DATA_DELAY_US 20000 53 54 static const int ltrf216a_int_time_available[][2] = { 55 { 0, 400000 }, 56 { 0, 200000 }, 57 { 0, 100000 }, 58 { 0, 50000 }, 59 { 0, 25000 }, 60 }; 61 62 static const int ltrf216a_int_time_reg[][2] = { 63 { 400, 0x03 }, 64 { 200, 0x13 }, 65 { 100, 0x22 }, 66 { 50, 0x31 }, 67 { 25, 0x40 }, 68 }; 69 70 struct ltr_chip_info { 71 /* Chip contains CLEAR_DATA_0/1/2 registers at offset 0xa..0xc */ 72 bool has_clear_data; 73 /* Lux calculation multiplier for ALS data */ 74 int lux_multiplier; 75 }; 76 77 /* 78 * Window Factor is needed when the device is under Window glass 79 * with coated tinted ink. This is to compensate for the light loss 80 * due to the lower transmission rate of the window glass and helps 81 * in calculating lux. 82 */ 83 #define LTRF216A_WIN_FAC 1 84 85 struct ltrf216a_data { 86 struct regmap *regmap; 87 struct i2c_client *client; 88 const struct ltr_chip_info *info; 89 u32 int_time; 90 u16 int_time_fac; 91 u8 als_gain_fac; 92 /* 93 * Protects regmap accesses and makes sure integration time 94 * remains constant during the measurement of lux. 95 */ 96 struct mutex lock; 97 }; 98 99 static const struct iio_chan_spec ltrf216a_channels[] = { 100 { 101 .type = IIO_LIGHT, 102 .info_mask_separate = 103 BIT(IIO_CHAN_INFO_RAW) | 104 BIT(IIO_CHAN_INFO_PROCESSED) | 105 BIT(IIO_CHAN_INFO_INT_TIME), 106 .info_mask_separate_available = 107 BIT(IIO_CHAN_INFO_INT_TIME), 108 }, 109 }; 110 111 static void ltrf216a_reset(struct iio_dev *indio_dev) 112 { 113 struct ltrf216a_data *data = iio_priv(indio_dev); 114 115 /* reset sensor, chip fails to respond to this, so ignore any errors */ 116 regmap_write(data->regmap, LTRF216A_MAIN_CTRL, LTRF216A_ALS_RESET_MASK); 117 118 /* reset time */ 119 usleep_range(1000, 2000); 120 } 121 122 static int ltrf216a_enable(struct iio_dev *indio_dev) 123 { 124 struct ltrf216a_data *data = iio_priv(indio_dev); 125 struct device *dev = &data->client->dev; 126 int ret; 127 128 /* enable sensor */ 129 ret = regmap_set_bits(data->regmap, 130 LTRF216A_MAIN_CTRL, LTRF216A_ALS_ENABLE_MASK); 131 if (ret) { 132 dev_err(dev, "failed to enable sensor: %d\n", ret); 133 return ret; 134 } 135 136 /* sleep for one integration cycle after enabling the device */ 137 msleep(ltrf216a_int_time_reg[0][0]); 138 139 return 0; 140 } 141 142 static int ltrf216a_disable(struct iio_dev *indio_dev) 143 { 144 struct ltrf216a_data *data = iio_priv(indio_dev); 145 struct device *dev = &data->client->dev; 146 int ret; 147 148 ret = regmap_write(data->regmap, LTRF216A_MAIN_CTRL, 0); 149 if (ret) 150 dev_err(dev, "failed to disable sensor: %d\n", ret); 151 152 return ret; 153 } 154 155 static void ltrf216a_cleanup(void *data) 156 { 157 struct iio_dev *indio_dev = data; 158 159 ltrf216a_disable(indio_dev); 160 } 161 162 static int ltrf216a_set_int_time(struct ltrf216a_data *data, int itime) 163 { 164 struct device *dev = &data->client->dev; 165 unsigned int i; 166 u8 reg_val; 167 int ret; 168 169 for (i = 0; i < ARRAY_SIZE(ltrf216a_int_time_available); i++) { 170 if (ltrf216a_int_time_available[i][1] == itime) 171 break; 172 } 173 if (i == ARRAY_SIZE(ltrf216a_int_time_available)) 174 return -EINVAL; 175 176 reg_val = ltrf216a_int_time_reg[i][1]; 177 178 ret = regmap_write(data->regmap, LTRF216A_ALS_MEAS_RES, reg_val); 179 if (ret) { 180 dev_err(dev, "failed to set integration time: %d\n", ret); 181 return ret; 182 } 183 184 data->int_time_fac = ltrf216a_int_time_reg[i][0]; 185 data->int_time = itime; 186 187 return 0; 188 } 189 190 static int ltrf216a_get_int_time(struct ltrf216a_data *data, 191 int *val, int *val2) 192 { 193 *val = 0; 194 *val2 = data->int_time; 195 return IIO_VAL_INT_PLUS_MICRO; 196 } 197 198 static int ltrf216a_set_power_state(struct ltrf216a_data *data, bool on) 199 { 200 struct device *dev = &data->client->dev; 201 int ret = 0; 202 203 if (on) { 204 ret = pm_runtime_resume_and_get(dev); 205 if (ret) { 206 dev_err(dev, "failed to resume runtime PM: %d\n", ret); 207 return ret; 208 } 209 } else { 210 pm_runtime_put_autosuspend(dev); 211 } 212 213 return ret; 214 } 215 216 static int ltrf216a_read_data(struct ltrf216a_data *data, u8 addr) 217 { 218 struct device *dev = &data->client->dev; 219 int ret, val; 220 u8 buf[3]; 221 222 ret = regmap_read_poll_timeout(data->regmap, LTRF216A_MAIN_STATUS, 223 val, val & LTRF216A_ALS_DATA_STATUS, 224 LTRF216A_ALS_READ_DATA_DELAY_US, 225 LTRF216A_ALS_READ_DATA_DELAY_US * 50); 226 if (ret) { 227 dev_err(dev, "failed to wait for measurement data: %d\n", ret); 228 return ret; 229 } 230 231 ret = regmap_bulk_read(data->regmap, addr, buf, sizeof(buf)); 232 if (ret) { 233 dev_err(dev, "failed to read measurement data: %d\n", ret); 234 return ret; 235 } 236 237 return get_unaligned_le24(&buf[0]); 238 } 239 240 static int ltrf216a_get_lux(struct ltrf216a_data *data) 241 { 242 int ret, greendata; 243 u64 lux; 244 245 ret = ltrf216a_set_power_state(data, true); 246 if (ret) 247 return ret; 248 249 greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0); 250 if (greendata < 0) 251 return greendata; 252 253 ltrf216a_set_power_state(data, false); 254 255 lux = greendata * data->info->lux_multiplier * LTRF216A_WIN_FAC; 256 257 return lux; 258 } 259 260 static int ltrf216a_read_raw(struct iio_dev *indio_dev, 261 struct iio_chan_spec const *chan, int *val, 262 int *val2, long mask) 263 { 264 struct ltrf216a_data *data = iio_priv(indio_dev); 265 int ret; 266 267 switch (mask) { 268 case IIO_CHAN_INFO_RAW: 269 ret = ltrf216a_set_power_state(data, true); 270 if (ret) 271 return ret; 272 mutex_lock(&data->lock); 273 ret = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0); 274 mutex_unlock(&data->lock); 275 ltrf216a_set_power_state(data, false); 276 if (ret < 0) 277 return ret; 278 *val = ret; 279 return IIO_VAL_INT; 280 case IIO_CHAN_INFO_PROCESSED: 281 mutex_lock(&data->lock); 282 ret = ltrf216a_get_lux(data); 283 mutex_unlock(&data->lock); 284 if (ret < 0) 285 return ret; 286 *val = ret; 287 *val2 = data->als_gain_fac * data->int_time_fac; 288 return IIO_VAL_FRACTIONAL; 289 case IIO_CHAN_INFO_INT_TIME: 290 mutex_lock(&data->lock); 291 ret = ltrf216a_get_int_time(data, val, val2); 292 mutex_unlock(&data->lock); 293 return ret; 294 default: 295 return -EINVAL; 296 } 297 } 298 299 static int ltrf216a_write_raw(struct iio_dev *indio_dev, 300 struct iio_chan_spec const *chan, int val, 301 int val2, long mask) 302 { 303 struct ltrf216a_data *data = iio_priv(indio_dev); 304 int ret; 305 306 switch (mask) { 307 case IIO_CHAN_INFO_INT_TIME: 308 if (val != 0) 309 return -EINVAL; 310 mutex_lock(&data->lock); 311 ret = ltrf216a_set_int_time(data, val2); 312 mutex_unlock(&data->lock); 313 return ret; 314 default: 315 return -EINVAL; 316 } 317 } 318 319 static int ltrf216a_read_available(struct iio_dev *indio_dev, 320 struct iio_chan_spec const *chan, 321 const int **vals, int *type, int *length, 322 long mask) 323 { 324 switch (mask) { 325 case IIO_CHAN_INFO_INT_TIME: 326 *length = ARRAY_SIZE(ltrf216a_int_time_available) * 2; 327 *vals = (const int *)ltrf216a_int_time_available; 328 *type = IIO_VAL_INT_PLUS_MICRO; 329 return IIO_AVAIL_LIST; 330 default: 331 return -EINVAL; 332 } 333 } 334 335 static const struct iio_info ltrf216a_info = { 336 .read_raw = ltrf216a_read_raw, 337 .write_raw = ltrf216a_write_raw, 338 .read_avail = ltrf216a_read_available, 339 }; 340 341 static bool ltrf216a_readable_reg(struct device *dev, unsigned int reg) 342 { 343 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 344 struct ltrf216a_data *data = iio_priv(indio_dev); 345 346 switch (reg) { 347 case LTRF216A_MAIN_CTRL: 348 case LTRF216A_ALS_MEAS_RES: 349 case LTRF216A_ALS_GAIN: 350 case LTRF216A_PART_ID: 351 case LTRF216A_MAIN_STATUS: 352 case LTRF216A_ALS_DATA_0: 353 case LTRF216A_ALS_DATA_1: 354 case LTRF216A_ALS_DATA_2: 355 case LTRF216A_INT_CFG: 356 case LTRF216A_INT_PST: 357 case LTRF216A_ALS_THRES_UP_0: 358 case LTRF216A_ALS_THRES_UP_1: 359 case LTRF216A_ALS_THRES_UP_2: 360 case LTRF216A_ALS_THRES_LOW_0: 361 case LTRF216A_ALS_THRES_LOW_1: 362 case LTRF216A_ALS_THRES_LOW_2: 363 return true; 364 case LTRF216A_ALS_CLEAR_DATA_0: 365 case LTRF216A_ALS_CLEAR_DATA_1: 366 case LTRF216A_ALS_CLEAR_DATA_2: 367 return data->info->has_clear_data; 368 default: 369 return false; 370 } 371 } 372 373 static bool ltrf216a_writable_reg(struct device *dev, unsigned int reg) 374 { 375 switch (reg) { 376 case LTRF216A_MAIN_CTRL: 377 case LTRF216A_ALS_MEAS_RES: 378 case LTRF216A_ALS_GAIN: 379 case LTRF216A_INT_CFG: 380 case LTRF216A_INT_PST: 381 case LTRF216A_ALS_THRES_UP_0: 382 case LTRF216A_ALS_THRES_UP_1: 383 case LTRF216A_ALS_THRES_UP_2: 384 case LTRF216A_ALS_THRES_LOW_0: 385 case LTRF216A_ALS_THRES_LOW_1: 386 case LTRF216A_ALS_THRES_LOW_2: 387 return true; 388 default: 389 return false; 390 } 391 } 392 393 static bool ltrf216a_volatile_reg(struct device *dev, unsigned int reg) 394 { 395 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 396 struct ltrf216a_data *data = iio_priv(indio_dev); 397 398 switch (reg) { 399 case LTRF216A_MAIN_STATUS: 400 case LTRF216A_ALS_DATA_0: 401 case LTRF216A_ALS_DATA_1: 402 case LTRF216A_ALS_DATA_2: 403 return true; 404 /* 405 * If these registers are not present on a chip (like LTR-308), 406 * the missing registers are not considered volatile. 407 */ 408 case LTRF216A_ALS_CLEAR_DATA_0: 409 case LTRF216A_ALS_CLEAR_DATA_1: 410 case LTRF216A_ALS_CLEAR_DATA_2: 411 return data->info->has_clear_data; 412 default: 413 return false; 414 } 415 } 416 417 static bool ltrf216a_precious_reg(struct device *dev, unsigned int reg) 418 { 419 return reg == LTRF216A_MAIN_STATUS; 420 } 421 422 static const struct regmap_config ltrf216a_regmap_config = { 423 .name = "ltrf216a", 424 .reg_bits = 8, 425 .val_bits = 8, 426 .cache_type = REGCACHE_RBTREE, 427 .max_register = LTRF216A_ALS_THRES_LOW_2, 428 .readable_reg = ltrf216a_readable_reg, 429 .writeable_reg = ltrf216a_writable_reg, 430 .volatile_reg = ltrf216a_volatile_reg, 431 .precious_reg = ltrf216a_precious_reg, 432 .disable_locking = true, 433 }; 434 435 static int ltrf216a_probe(struct i2c_client *client) 436 { 437 struct ltrf216a_data *data; 438 struct iio_dev *indio_dev; 439 int ret; 440 441 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 442 if (!indio_dev) 443 return -ENOMEM; 444 445 data = iio_priv(indio_dev); 446 447 data->regmap = devm_regmap_init_i2c(client, <rf216a_regmap_config); 448 if (IS_ERR(data->regmap)) 449 return dev_err_probe(&client->dev, PTR_ERR(data->regmap), 450 "regmap initialization failed\n"); 451 452 i2c_set_clientdata(client, indio_dev); 453 data->client = client; 454 data->info = i2c_get_match_data(client); 455 456 mutex_init(&data->lock); 457 458 indio_dev->info = <rf216a_info; 459 indio_dev->name = "ltrf216a"; 460 indio_dev->channels = ltrf216a_channels; 461 indio_dev->num_channels = ARRAY_SIZE(ltrf216a_channels); 462 indio_dev->modes = INDIO_DIRECT_MODE; 463 464 ret = pm_runtime_set_active(&client->dev); 465 if (ret) 466 return ret; 467 468 /* reset sensor, chip fails to respond to this, so ignore any errors */ 469 ltrf216a_reset(indio_dev); 470 471 ret = regmap_reinit_cache(data->regmap, <rf216a_regmap_config); 472 if (ret) 473 return dev_err_probe(&client->dev, ret, 474 "failed to reinit regmap cache\n"); 475 476 ret = ltrf216a_enable(indio_dev); 477 if (ret) 478 return ret; 479 480 ret = devm_add_action_or_reset(&client->dev, ltrf216a_cleanup, 481 indio_dev); 482 if (ret) 483 return ret; 484 485 ret = devm_pm_runtime_enable(&client->dev); 486 if (ret) 487 return dev_err_probe(&client->dev, ret, 488 "failed to enable runtime PM\n"); 489 490 pm_runtime_set_autosuspend_delay(&client->dev, 1000); 491 pm_runtime_use_autosuspend(&client->dev); 492 493 data->int_time = 100000; 494 data->int_time_fac = 100; 495 data->als_gain_fac = 3; 496 497 return devm_iio_device_register(&client->dev, indio_dev); 498 } 499 500 static int ltrf216a_runtime_suspend(struct device *dev) 501 { 502 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 503 struct ltrf216a_data *data = iio_priv(indio_dev); 504 int ret; 505 506 ret = ltrf216a_disable(indio_dev); 507 if (ret) 508 return ret; 509 510 regcache_cache_only(data->regmap, true); 511 512 return 0; 513 } 514 515 static int ltrf216a_runtime_resume(struct device *dev) 516 { 517 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 518 struct ltrf216a_data *data = iio_priv(indio_dev); 519 int ret; 520 521 regcache_cache_only(data->regmap, false); 522 regcache_mark_dirty(data->regmap); 523 ret = regcache_sync(data->regmap); 524 if (ret) 525 goto cache_only; 526 527 ret = ltrf216a_enable(indio_dev); 528 if (ret) 529 goto cache_only; 530 531 return 0; 532 533 cache_only: 534 regcache_cache_only(data->regmap, true); 535 536 return ret; 537 } 538 539 static DEFINE_RUNTIME_DEV_PM_OPS(ltrf216a_pm_ops, ltrf216a_runtime_suspend, 540 ltrf216a_runtime_resume, NULL); 541 542 static const struct ltr_chip_info ltr308_chip_info = { 543 .has_clear_data = false, 544 .lux_multiplier = 60, 545 }; 546 547 static const struct ltr_chip_info ltrf216a_chip_info = { 548 .has_clear_data = true, 549 .lux_multiplier = 45, 550 }; 551 552 static const struct i2c_device_id ltrf216a_id[] = { 553 { .name = "ltr308", .driver_data = (kernel_ulong_t)<r308_chip_info }, 554 { .name = "ltrf216a", .driver_data = (kernel_ulong_t)<rf216a_chip_info }, 555 { } 556 }; 557 MODULE_DEVICE_TABLE(i2c, ltrf216a_id); 558 559 static const struct of_device_id ltrf216a_of_match[] = { 560 { .compatible = "liteon,ltr308", .data = <r308_chip_info }, 561 { .compatible = "liteon,ltrf216a", .data = <rf216a_chip_info }, 562 /* For Valve's Steamdeck device, an ACPI platform using PRP0001 */ 563 { .compatible = "ltr,ltrf216a", .data = <rf216a_chip_info }, 564 { } 565 }; 566 MODULE_DEVICE_TABLE(of, ltrf216a_of_match); 567 568 static struct i2c_driver ltrf216a_driver = { 569 .driver = { 570 .name = "ltrf216a", 571 .pm = pm_ptr(<rf216a_pm_ops), 572 .of_match_table = ltrf216a_of_match, 573 }, 574 .probe = ltrf216a_probe, 575 .id_table = ltrf216a_id, 576 }; 577 module_i2c_driver(ltrf216a_driver); 578 579 MODULE_AUTHOR("Shreeya Patel <shreeya.patel@collabora.com>"); 580 MODULE_AUTHOR("Shi Zhigang <Zhigang.Shi@liteon.com>"); 581 MODULE_DESCRIPTION("LTRF216A ambient light sensor driver"); 582 MODULE_LICENSE("GPL"); 583