1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * MAX44000 Ambient and Infrared Proximity Sensor 4 * 5 * Copyright (c) 2016, Intel Corporation. 6 * 7 * Data sheet: https://datasheets.maximintegrated.com/en/ds/MAX44000.pdf 8 * 9 * 7-bit I2C slave address 0x4a 10 */ 11 12 #include <linux/module.h> 13 #include <linux/init.h> 14 #include <linux/i2c.h> 15 #include <linux/regmap.h> 16 #include <linux/util_macros.h> 17 #include <linux/iio/iio.h> 18 #include <linux/iio/sysfs.h> 19 #include <linux/iio/buffer.h> 20 #include <linux/iio/trigger_consumer.h> 21 #include <linux/iio/triggered_buffer.h> 22 #include <linux/acpi.h> 23 24 #define MAX44000_DRV_NAME "max44000" 25 26 /* Registers in datasheet order */ 27 #define MAX44000_REG_STATUS 0x00 28 #define MAX44000_REG_CFG_MAIN 0x01 29 #define MAX44000_REG_CFG_RX 0x02 30 #define MAX44000_REG_CFG_TX 0x03 31 #define MAX44000_REG_ALS_DATA_HI 0x04 32 #define MAX44000_REG_ALS_DATA_LO 0x05 33 #define MAX44000_REG_PRX_DATA 0x16 34 #define MAX44000_REG_ALS_UPTHR_HI 0x06 35 #define MAX44000_REG_ALS_UPTHR_LO 0x07 36 #define MAX44000_REG_ALS_LOTHR_HI 0x08 37 #define MAX44000_REG_ALS_LOTHR_LO 0x09 38 #define MAX44000_REG_PST 0x0a 39 #define MAX44000_REG_PRX_IND 0x0b 40 #define MAX44000_REG_PRX_THR 0x0c 41 #define MAX44000_REG_TRIM_GAIN_GREEN 0x0f 42 #define MAX44000_REG_TRIM_GAIN_IR 0x10 43 44 /* REG_CFG bits */ 45 #define MAX44000_CFG_ALSINTE 0x01 46 #define MAX44000_CFG_PRXINTE 0x02 47 #define MAX44000_CFG_MASK 0x1c 48 #define MAX44000_CFG_MODE_SHUTDOWN 0x00 49 #define MAX44000_CFG_MODE_ALS_GIR 0x04 50 #define MAX44000_CFG_MODE_ALS_G 0x08 51 #define MAX44000_CFG_MODE_ALS_IR 0x0c 52 #define MAX44000_CFG_MODE_ALS_PRX 0x10 53 #define MAX44000_CFG_MODE_PRX 0x14 54 #define MAX44000_CFG_TRIM 0x20 55 56 /* 57 * Upper 4 bits are not documented but start as 1 on powerup 58 * Setting them to 0 causes proximity to misbehave so set them to 1 59 */ 60 #define MAX44000_REG_CFG_RX_DEFAULT 0xf0 61 62 /* REG_RX bits */ 63 #define MAX44000_CFG_RX_ALSTIM_MASK 0x0c 64 #define MAX44000_CFG_RX_ALSTIM_SHIFT 2 65 #define MAX44000_CFG_RX_ALSPGA_MASK 0x03 66 #define MAX44000_CFG_RX_ALSPGA_SHIFT 0 67 68 /* REG_TX bits */ 69 #define MAX44000_LED_CURRENT_MASK 0xf 70 #define MAX44000_LED_CURRENT_MAX 11 71 #define MAX44000_LED_CURRENT_DEFAULT 6 72 73 #define MAX44000_ALSDATA_OVERFLOW 0x4000 74 75 struct max44000_data { 76 struct mutex lock; 77 struct regmap *regmap; 78 }; 79 80 /* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */ 81 #define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5 82 83 /* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */ 84 static const int max44000_alspga_shift[] = {0, 2, 4, 7}; 85 #define MAX44000_ALSPGA_MAX_SHIFT 7 86 87 /* 88 * Scale can be multiplied by up to 64x via ALSTIM because of lost resolution 89 * 90 * This scaling factor is hidden from userspace and instead accounted for when 91 * reading raw values from the device. 92 * 93 * This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and 94 * ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other. 95 * 96 * Handling this internally is also required for buffer support because the 97 * channel's scan_type can't be modified dynamically. 98 */ 99 #define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim)) 100 101 /* Available integration times with pretty manual alignment: */ 102 static const int max44000_int_time_avail_ns_array[] = { 103 100000000, 104 25000000, 105 6250000, 106 1562500, 107 }; 108 static const char max44000_int_time_avail_str[] = 109 "0.100 " 110 "0.025 " 111 "0.00625 " 112 "0.0015625"; 113 114 /* Available scales (internal to ulux) with pretty manual alignment: */ 115 static const int max44000_scale_avail_ulux_array[] = { 116 31250, 117 125000, 118 500000, 119 4000000, 120 }; 121 static const char max44000_scale_avail_str[] = 122 "0.03125 " 123 "0.125 " 124 "0.5 " 125 "4"; 126 127 #define MAX44000_SCAN_INDEX_ALS 0 128 #define MAX44000_SCAN_INDEX_PRX 1 129 130 static const struct iio_chan_spec max44000_channels[] = { 131 { 132 .type = IIO_LIGHT, 133 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 134 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | 135 BIT(IIO_CHAN_INFO_INT_TIME), 136 .scan_index = MAX44000_SCAN_INDEX_ALS, 137 .scan_type = { 138 .sign = 'u', 139 .realbits = 14, 140 .storagebits = 16, 141 } 142 }, 143 { 144 .type = IIO_PROXIMITY, 145 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 146 .scan_index = MAX44000_SCAN_INDEX_PRX, 147 .scan_type = { 148 .sign = 'u', 149 .realbits = 8, 150 .storagebits = 16, 151 } 152 }, 153 IIO_CHAN_SOFT_TIMESTAMP(2), 154 { 155 .type = IIO_CURRENT, 156 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 157 BIT(IIO_CHAN_INFO_SCALE), 158 .extend_name = "led", 159 .output = 1, 160 .scan_index = -1, 161 }, 162 }; 163 164 static int max44000_read_alstim(struct max44000_data *data) 165 { 166 unsigned int val; 167 int ret; 168 169 ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val); 170 if (ret < 0) 171 return ret; 172 return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT; 173 } 174 175 static int max44000_write_alstim(struct max44000_data *data, int val) 176 { 177 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX, 178 MAX44000_CFG_RX_ALSTIM_MASK, 179 val << MAX44000_CFG_RX_ALSTIM_SHIFT); 180 } 181 182 static int max44000_read_alspga(struct max44000_data *data) 183 { 184 unsigned int val; 185 int ret; 186 187 ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val); 188 if (ret < 0) 189 return ret; 190 return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT; 191 } 192 193 static int max44000_write_alspga(struct max44000_data *data, int val) 194 { 195 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX, 196 MAX44000_CFG_RX_ALSPGA_MASK, 197 val << MAX44000_CFG_RX_ALSPGA_SHIFT); 198 } 199 200 static int max44000_read_alsval(struct max44000_data *data) 201 { 202 u16 regval; 203 __be16 val; 204 int alstim, ret; 205 206 ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI, 207 &val, sizeof(val)); 208 if (ret < 0) 209 return ret; 210 alstim = ret = max44000_read_alstim(data); 211 if (ret < 0) 212 return ret; 213 214 regval = be16_to_cpu(val); 215 216 /* 217 * Overflow is explained on datasheet page 17. 218 * 219 * It's a warning that either the G or IR channel has become saturated 220 * and that the value in the register is likely incorrect. 221 * 222 * The recommendation is to change the scale (ALSPGA). 223 * The driver just returns the max representable value. 224 */ 225 if (regval & MAX44000_ALSDATA_OVERFLOW) 226 return 0x3FFF; 227 228 return regval << MAX44000_ALSTIM_SHIFT(alstim); 229 } 230 231 static int max44000_write_led_current_raw(struct max44000_data *data, int val) 232 { 233 /* Maybe we should clamp the value instead? */ 234 if (val < 0 || val > MAX44000_LED_CURRENT_MAX) 235 return -ERANGE; 236 if (val >= 8) 237 val += 4; 238 return regmap_write_bits(data->regmap, MAX44000_REG_CFG_TX, 239 MAX44000_LED_CURRENT_MASK, val); 240 } 241 242 static int max44000_read_led_current_raw(struct max44000_data *data) 243 { 244 unsigned int regval; 245 int ret; 246 247 ret = regmap_read(data->regmap, MAX44000_REG_CFG_TX, ®val); 248 if (ret < 0) 249 return ret; 250 regval &= MAX44000_LED_CURRENT_MASK; 251 if (regval >= 8) 252 regval -= 4; 253 return regval; 254 } 255 256 static int max44000_read_raw(struct iio_dev *indio_dev, 257 struct iio_chan_spec const *chan, 258 int *val, int *val2, long mask) 259 { 260 struct max44000_data *data = iio_priv(indio_dev); 261 int alstim, alspga; 262 unsigned int regval; 263 int ret; 264 265 switch (mask) { 266 case IIO_CHAN_INFO_RAW: 267 switch (chan->type) { 268 case IIO_LIGHT: 269 mutex_lock(&data->lock); 270 ret = max44000_read_alsval(data); 271 mutex_unlock(&data->lock); 272 if (ret < 0) 273 return ret; 274 *val = ret; 275 return IIO_VAL_INT; 276 277 case IIO_PROXIMITY: 278 mutex_lock(&data->lock); 279 ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); 280 mutex_unlock(&data->lock); 281 if (ret < 0) 282 return ret; 283 *val = regval; 284 return IIO_VAL_INT; 285 286 case IIO_CURRENT: 287 mutex_lock(&data->lock); 288 ret = max44000_read_led_current_raw(data); 289 mutex_unlock(&data->lock); 290 if (ret < 0) 291 return ret; 292 *val = ret; 293 return IIO_VAL_INT; 294 295 default: 296 return -EINVAL; 297 } 298 299 case IIO_CHAN_INFO_SCALE: 300 switch (chan->type) { 301 case IIO_CURRENT: 302 /* Output register is in 10s of miliamps */ 303 *val = 10; 304 return IIO_VAL_INT; 305 306 case IIO_LIGHT: 307 mutex_lock(&data->lock); 308 alspga = ret = max44000_read_alspga(data); 309 mutex_unlock(&data->lock); 310 if (ret < 0) 311 return ret; 312 313 /* Avoid negative shifts */ 314 *val = (1 << MAX44000_ALSPGA_MAX_SHIFT); 315 *val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 316 + MAX44000_ALSPGA_MAX_SHIFT 317 - max44000_alspga_shift[alspga]; 318 return IIO_VAL_FRACTIONAL_LOG2; 319 320 default: 321 return -EINVAL; 322 } 323 324 case IIO_CHAN_INFO_INT_TIME: 325 mutex_lock(&data->lock); 326 alstim = ret = max44000_read_alstim(data); 327 mutex_unlock(&data->lock); 328 329 if (ret < 0) 330 return ret; 331 *val = 0; 332 *val2 = max44000_int_time_avail_ns_array[alstim]; 333 return IIO_VAL_INT_PLUS_NANO; 334 335 default: 336 return -EINVAL; 337 } 338 } 339 340 static int max44000_write_raw(struct iio_dev *indio_dev, 341 struct iio_chan_spec const *chan, 342 int val, int val2, long mask) 343 { 344 struct max44000_data *data = iio_priv(indio_dev); 345 int ret; 346 347 if (mask == IIO_CHAN_INFO_RAW && chan->type == IIO_CURRENT) { 348 mutex_lock(&data->lock); 349 ret = max44000_write_led_current_raw(data, val); 350 mutex_unlock(&data->lock); 351 return ret; 352 } else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) { 353 s64 valns = val * NSEC_PER_SEC + val2; 354 int alstim = find_closest_descending(valns, 355 max44000_int_time_avail_ns_array, 356 ARRAY_SIZE(max44000_int_time_avail_ns_array)); 357 mutex_lock(&data->lock); 358 ret = max44000_write_alstim(data, alstim); 359 mutex_unlock(&data->lock); 360 return ret; 361 } else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) { 362 s64 valus = val * USEC_PER_SEC + val2; 363 int alspga = find_closest(valus, 364 max44000_scale_avail_ulux_array, 365 ARRAY_SIZE(max44000_scale_avail_ulux_array)); 366 mutex_lock(&data->lock); 367 ret = max44000_write_alspga(data, alspga); 368 mutex_unlock(&data->lock); 369 return ret; 370 } 371 372 return -EINVAL; 373 } 374 375 static int max44000_write_raw_get_fmt(struct iio_dev *indio_dev, 376 struct iio_chan_spec const *chan, 377 long mask) 378 { 379 if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) 380 return IIO_VAL_INT_PLUS_NANO; 381 else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) 382 return IIO_VAL_INT_PLUS_MICRO; 383 else 384 return IIO_VAL_INT; 385 } 386 387 static IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str); 388 static IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str); 389 390 static struct attribute *max44000_attributes[] = { 391 &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, 392 &iio_const_attr_illuminance_scale_available.dev_attr.attr, 393 NULL 394 }; 395 396 static const struct attribute_group max44000_attribute_group = { 397 .attrs = max44000_attributes, 398 }; 399 400 static const struct iio_info max44000_info = { 401 .read_raw = max44000_read_raw, 402 .write_raw = max44000_write_raw, 403 .write_raw_get_fmt = max44000_write_raw_get_fmt, 404 .attrs = &max44000_attribute_group, 405 }; 406 407 static bool max44000_readable_reg(struct device *dev, unsigned int reg) 408 { 409 switch (reg) { 410 case MAX44000_REG_STATUS: 411 case MAX44000_REG_CFG_MAIN: 412 case MAX44000_REG_CFG_RX: 413 case MAX44000_REG_CFG_TX: 414 case MAX44000_REG_ALS_DATA_HI: 415 case MAX44000_REG_ALS_DATA_LO: 416 case MAX44000_REG_PRX_DATA: 417 case MAX44000_REG_ALS_UPTHR_HI: 418 case MAX44000_REG_ALS_UPTHR_LO: 419 case MAX44000_REG_ALS_LOTHR_HI: 420 case MAX44000_REG_ALS_LOTHR_LO: 421 case MAX44000_REG_PST: 422 case MAX44000_REG_PRX_IND: 423 case MAX44000_REG_PRX_THR: 424 case MAX44000_REG_TRIM_GAIN_GREEN: 425 case MAX44000_REG_TRIM_GAIN_IR: 426 return true; 427 default: 428 return false; 429 } 430 } 431 432 static bool max44000_writeable_reg(struct device *dev, unsigned int reg) 433 { 434 switch (reg) { 435 case MAX44000_REG_CFG_MAIN: 436 case MAX44000_REG_CFG_RX: 437 case MAX44000_REG_CFG_TX: 438 case MAX44000_REG_ALS_UPTHR_HI: 439 case MAX44000_REG_ALS_UPTHR_LO: 440 case MAX44000_REG_ALS_LOTHR_HI: 441 case MAX44000_REG_ALS_LOTHR_LO: 442 case MAX44000_REG_PST: 443 case MAX44000_REG_PRX_IND: 444 case MAX44000_REG_PRX_THR: 445 case MAX44000_REG_TRIM_GAIN_GREEN: 446 case MAX44000_REG_TRIM_GAIN_IR: 447 return true; 448 default: 449 return false; 450 } 451 } 452 453 static bool max44000_volatile_reg(struct device *dev, unsigned int reg) 454 { 455 switch (reg) { 456 case MAX44000_REG_STATUS: 457 case MAX44000_REG_ALS_DATA_HI: 458 case MAX44000_REG_ALS_DATA_LO: 459 case MAX44000_REG_PRX_DATA: 460 return true; 461 default: 462 return false; 463 } 464 } 465 466 static bool max44000_precious_reg(struct device *dev, unsigned int reg) 467 { 468 return reg == MAX44000_REG_STATUS; 469 } 470 471 static const struct regmap_config max44000_regmap_config = { 472 .reg_bits = 8, 473 .val_bits = 8, 474 475 .max_register = MAX44000_REG_PRX_DATA, 476 .readable_reg = max44000_readable_reg, 477 .writeable_reg = max44000_writeable_reg, 478 .volatile_reg = max44000_volatile_reg, 479 .precious_reg = max44000_precious_reg, 480 481 .use_single_read = true, 482 .use_single_write = true, 483 .cache_type = REGCACHE_RBTREE, 484 }; 485 486 static irqreturn_t max44000_trigger_handler(int irq, void *p) 487 { 488 struct iio_poll_func *pf = p; 489 struct iio_dev *indio_dev = pf->indio_dev; 490 struct max44000_data *data = iio_priv(indio_dev); 491 u16 buf[8]; /* 2x u16 + padding + 8 bytes timestamp */ 492 int index = 0; 493 unsigned int regval; 494 int ret; 495 496 mutex_lock(&data->lock); 497 if (test_bit(MAX44000_SCAN_INDEX_ALS, indio_dev->active_scan_mask)) { 498 ret = max44000_read_alsval(data); 499 if (ret < 0) 500 goto out_unlock; 501 buf[index++] = ret; 502 } 503 if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) { 504 ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); 505 if (ret < 0) 506 goto out_unlock; 507 buf[index] = regval; 508 } 509 mutex_unlock(&data->lock); 510 511 iio_push_to_buffers_with_timestamp(indio_dev, buf, 512 iio_get_time_ns(indio_dev)); 513 iio_trigger_notify_done(indio_dev->trig); 514 return IRQ_HANDLED; 515 516 out_unlock: 517 mutex_unlock(&data->lock); 518 iio_trigger_notify_done(indio_dev->trig); 519 return IRQ_HANDLED; 520 } 521 522 static int max44000_probe(struct i2c_client *client, 523 const struct i2c_device_id *id) 524 { 525 struct max44000_data *data; 526 struct iio_dev *indio_dev; 527 int ret, reg; 528 529 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 530 if (!indio_dev) 531 return -ENOMEM; 532 data = iio_priv(indio_dev); 533 data->regmap = devm_regmap_init_i2c(client, &max44000_regmap_config); 534 if (IS_ERR(data->regmap)) { 535 dev_err(&client->dev, "regmap_init failed!\n"); 536 return PTR_ERR(data->regmap); 537 } 538 539 i2c_set_clientdata(client, indio_dev); 540 mutex_init(&data->lock); 541 indio_dev->dev.parent = &client->dev; 542 indio_dev->info = &max44000_info; 543 indio_dev->name = MAX44000_DRV_NAME; 544 indio_dev->channels = max44000_channels; 545 indio_dev->num_channels = ARRAY_SIZE(max44000_channels); 546 547 /* 548 * The device doesn't have a reset function so we just clear some 549 * important bits at probe time to ensure sane operation. 550 * 551 * Since we don't support interrupts/events the threshold values are 552 * not important. We also don't touch trim values. 553 */ 554 555 /* Reset ALS scaling bits */ 556 ret = regmap_write(data->regmap, MAX44000_REG_CFG_RX, 557 MAX44000_REG_CFG_RX_DEFAULT); 558 if (ret < 0) { 559 dev_err(&client->dev, "failed to write default CFG_RX: %d\n", 560 ret); 561 return ret; 562 } 563 564 /* 565 * By default the LED pulse used for the proximity sensor is disabled. 566 * Set a middle value so that we get some sort of valid data by default. 567 */ 568 ret = max44000_write_led_current_raw(data, MAX44000_LED_CURRENT_DEFAULT); 569 if (ret < 0) { 570 dev_err(&client->dev, "failed to write init config: %d\n", ret); 571 return ret; 572 } 573 574 /* Reset CFG bits to ALS_PRX mode which allows easy reading of both values. */ 575 reg = MAX44000_CFG_TRIM | MAX44000_CFG_MODE_ALS_PRX; 576 ret = regmap_write(data->regmap, MAX44000_REG_CFG_MAIN, reg); 577 if (ret < 0) { 578 dev_err(&client->dev, "failed to write init config: %d\n", ret); 579 return ret; 580 } 581 582 /* Read status at least once to clear any stale interrupt bits. */ 583 ret = regmap_read(data->regmap, MAX44000_REG_STATUS, ®); 584 if (ret < 0) { 585 dev_err(&client->dev, "failed to read init status: %d\n", ret); 586 return ret; 587 } 588 589 ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL); 590 if (ret < 0) { 591 dev_err(&client->dev, "iio triggered buffer setup failed\n"); 592 return ret; 593 } 594 595 return iio_device_register(indio_dev); 596 } 597 598 static int max44000_remove(struct i2c_client *client) 599 { 600 struct iio_dev *indio_dev = i2c_get_clientdata(client); 601 602 iio_device_unregister(indio_dev); 603 iio_triggered_buffer_cleanup(indio_dev); 604 605 return 0; 606 } 607 608 static const struct i2c_device_id max44000_id[] = { 609 {"max44000", 0}, 610 { } 611 }; 612 MODULE_DEVICE_TABLE(i2c, max44000_id); 613 614 #ifdef CONFIG_ACPI 615 static const struct acpi_device_id max44000_acpi_match[] = { 616 {"MAX44000", 0}, 617 { } 618 }; 619 MODULE_DEVICE_TABLE(acpi, max44000_acpi_match); 620 #endif 621 622 static struct i2c_driver max44000_driver = { 623 .driver = { 624 .name = MAX44000_DRV_NAME, 625 .acpi_match_table = ACPI_PTR(max44000_acpi_match), 626 }, 627 .probe = max44000_probe, 628 .remove = max44000_remove, 629 .id_table = max44000_id, 630 }; 631 632 module_i2c_driver(max44000_driver); 633 634 MODULE_AUTHOR("Crestez Dan Leonard <leonard.crestez@intel.com>"); 635 MODULE_DESCRIPTION("MAX44000 Ambient and Infrared Proximity Sensor"); 636 MODULE_LICENSE("GPL v2"); 637