1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * si1145.c - Support for Silabs SI1132 and SI1141/2/3/5/6/7 combined ambient 4 * light, UV index and proximity sensors 5 * 6 * Copyright 2014-16 Peter Meerwald-Stadler <pmeerw@pmeerw.net> 7 * Copyright 2016 Crestez Dan Leonard <leonard.crestez@intel.com> 8 * 9 * SI1132 (7-bit I2C slave address 0x60) 10 * SI1141/2/3 (7-bit I2C slave address 0x5a) 11 * SI1145/6/6 (7-bit I2C slave address 0x60) 12 */ 13 14 #include <linux/module.h> 15 #include <linux/i2c.h> 16 #include <linux/err.h> 17 #include <linux/slab.h> 18 #include <linux/delay.h> 19 #include <linux/irq.h> 20 21 #include <linux/iio/iio.h> 22 #include <linux/iio/sysfs.h> 23 #include <linux/iio/trigger.h> 24 #include <linux/iio/trigger_consumer.h> 25 #include <linux/iio/triggered_buffer.h> 26 #include <linux/iio/buffer.h> 27 #include <linux/util_macros.h> 28 29 #define SI1145_REG_PART_ID 0x00 30 #define SI1145_REG_REV_ID 0x01 31 #define SI1145_REG_SEQ_ID 0x02 32 #define SI1145_REG_INT_CFG 0x03 33 #define SI1145_REG_IRQ_ENABLE 0x04 34 #define SI1145_REG_IRQ_MODE 0x05 35 #define SI1145_REG_HW_KEY 0x07 36 #define SI1145_REG_MEAS_RATE 0x08 37 #define SI1145_REG_PS_LED21 0x0f 38 #define SI1145_REG_PS_LED3 0x10 39 #define SI1145_REG_UCOEF1 0x13 40 #define SI1145_REG_UCOEF2 0x14 41 #define SI1145_REG_UCOEF3 0x15 42 #define SI1145_REG_UCOEF4 0x16 43 #define SI1145_REG_PARAM_WR 0x17 44 #define SI1145_REG_COMMAND 0x18 45 #define SI1145_REG_RESPONSE 0x20 46 #define SI1145_REG_IRQ_STATUS 0x21 47 #define SI1145_REG_ALSVIS_DATA 0x22 48 #define SI1145_REG_ALSIR_DATA 0x24 49 #define SI1145_REG_PS1_DATA 0x26 50 #define SI1145_REG_PS2_DATA 0x28 51 #define SI1145_REG_PS3_DATA 0x2a 52 #define SI1145_REG_AUX_DATA 0x2c 53 #define SI1145_REG_PARAM_RD 0x2e 54 #define SI1145_REG_CHIP_STAT 0x30 55 56 #define SI1145_UCOEF1_DEFAULT 0x7b 57 #define SI1145_UCOEF2_DEFAULT 0x6b 58 #define SI1145_UCOEF3_DEFAULT 0x01 59 #define SI1145_UCOEF4_DEFAULT 0x00 60 61 /* Helper to figure out PS_LED register / shift per channel */ 62 #define SI1145_PS_LED_REG(ch) \ 63 (((ch) == 2) ? SI1145_REG_PS_LED3 : SI1145_REG_PS_LED21) 64 #define SI1145_PS_LED_SHIFT(ch) \ 65 (((ch) == 1) ? 4 : 0) 66 67 /* Parameter offsets */ 68 #define SI1145_PARAM_CHLIST 0x01 69 #define SI1145_PARAM_PSLED12_SELECT 0x02 70 #define SI1145_PARAM_PSLED3_SELECT 0x03 71 #define SI1145_PARAM_PS_ENCODING 0x05 72 #define SI1145_PARAM_ALS_ENCODING 0x06 73 #define SI1145_PARAM_PS1_ADC_MUX 0x07 74 #define SI1145_PARAM_PS2_ADC_MUX 0x08 75 #define SI1145_PARAM_PS3_ADC_MUX 0x09 76 #define SI1145_PARAM_PS_ADC_COUNTER 0x0a 77 #define SI1145_PARAM_PS_ADC_GAIN 0x0b 78 #define SI1145_PARAM_PS_ADC_MISC 0x0c 79 #define SI1145_PARAM_ALS_ADC_MUX 0x0d 80 #define SI1145_PARAM_ALSIR_ADC_MUX 0x0e 81 #define SI1145_PARAM_AUX_ADC_MUX 0x0f 82 #define SI1145_PARAM_ALSVIS_ADC_COUNTER 0x10 83 #define SI1145_PARAM_ALSVIS_ADC_GAIN 0x11 84 #define SI1145_PARAM_ALSVIS_ADC_MISC 0x12 85 #define SI1145_PARAM_LED_RECOVERY 0x1c 86 #define SI1145_PARAM_ALSIR_ADC_COUNTER 0x1d 87 #define SI1145_PARAM_ALSIR_ADC_GAIN 0x1e 88 #define SI1145_PARAM_ALSIR_ADC_MISC 0x1f 89 #define SI1145_PARAM_ADC_OFFSET 0x1a 90 91 /* Channel enable masks for CHLIST parameter */ 92 #define SI1145_CHLIST_EN_PS1 BIT(0) 93 #define SI1145_CHLIST_EN_PS2 BIT(1) 94 #define SI1145_CHLIST_EN_PS3 BIT(2) 95 #define SI1145_CHLIST_EN_ALSVIS BIT(4) 96 #define SI1145_CHLIST_EN_ALSIR BIT(5) 97 #define SI1145_CHLIST_EN_AUX BIT(6) 98 #define SI1145_CHLIST_EN_UV BIT(7) 99 100 /* Proximity measurement mode for ADC_MISC parameter */ 101 #define SI1145_PS_ADC_MODE_NORMAL BIT(2) 102 /* Signal range mask for ADC_MISC parameter */ 103 #define SI1145_ADC_MISC_RANGE BIT(5) 104 105 /* Commands for REG_COMMAND */ 106 #define SI1145_CMD_NOP 0x00 107 #define SI1145_CMD_RESET 0x01 108 #define SI1145_CMD_PS_FORCE 0x05 109 #define SI1145_CMD_ALS_FORCE 0x06 110 #define SI1145_CMD_PSALS_FORCE 0x07 111 #define SI1145_CMD_PS_PAUSE 0x09 112 #define SI1145_CMD_ALS_PAUSE 0x0a 113 #define SI1145_CMD_PSALS_PAUSE 0x0b 114 #define SI1145_CMD_PS_AUTO 0x0d 115 #define SI1145_CMD_ALS_AUTO 0x0e 116 #define SI1145_CMD_PSALS_AUTO 0x0f 117 #define SI1145_CMD_PARAM_QUERY 0x80 118 #define SI1145_CMD_PARAM_SET 0xa0 119 120 #define SI1145_RSP_INVALID_SETTING 0x80 121 #define SI1145_RSP_COUNTER_MASK 0x0F 122 123 /* Minimum sleep after each command to ensure it's received */ 124 #define SI1145_COMMAND_MINSLEEP_MS 5 125 /* Return -ETIMEDOUT after this long */ 126 #define SI1145_COMMAND_TIMEOUT_MS 25 127 128 /* Interrupt configuration masks for INT_CFG register */ 129 #define SI1145_INT_CFG_OE BIT(0) /* enable interrupt */ 130 #define SI1145_INT_CFG_MODE BIT(1) /* auto reset interrupt pin */ 131 132 /* Interrupt enable masks for IRQ_ENABLE register */ 133 #define SI1145_MASK_ALL_IE (BIT(4) | BIT(3) | BIT(2) | BIT(0)) 134 135 #define SI1145_MUX_TEMP 0x65 136 #define SI1145_MUX_VDD 0x75 137 138 /* Proximity LED current; see Table 2 in datasheet */ 139 #define SI1145_LED_CURRENT_45mA 0x04 140 141 enum { 142 SI1132, 143 SI1141, 144 SI1142, 145 SI1143, 146 SI1145, 147 SI1146, 148 SI1147, 149 }; 150 151 struct si1145_part_info { 152 u8 part; 153 const struct iio_info *iio_info; 154 const struct iio_chan_spec *channels; 155 unsigned int num_channels; 156 unsigned int num_leds; 157 bool uncompressed_meas_rate; 158 }; 159 160 /** 161 * struct si1145_data - si1145 chip state data 162 * @client: I2C client 163 * @lock: mutex to protect shared state. 164 * @cmdlock: Low-level mutex to protect command execution only 165 * @rsp_seq: Next expected response number or -1 if counter reset required 166 * @scan_mask: Saved scan mask to avoid duplicate set_chlist 167 * @autonomous: If automatic measurements are active (for buffer support) 168 * @part_info: Part information 169 * @trig: Pointer to iio trigger 170 * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode 171 * @buffer: Used to pack data read from sensor. 172 */ 173 struct si1145_data { 174 struct i2c_client *client; 175 struct mutex lock; 176 struct mutex cmdlock; 177 int rsp_seq; 178 const struct si1145_part_info *part_info; 179 unsigned long scan_mask; 180 bool autonomous; 181 struct iio_trigger *trig; 182 int meas_rate; 183 /* 184 * Ensure timestamp will be naturally aligned if present. 185 * Maximum buffer size (may be only partly used if not all 186 * channels are enabled): 187 * 6*2 bytes channels data + 4 bytes alignment + 188 * 8 bytes timestamp 189 */ 190 u8 buffer[24] __aligned(8); 191 }; 192 193 /* 194 * __si1145_command_reset() - Send CMD_NOP and wait for response 0 195 * 196 * Does not modify data->rsp_seq 197 * 198 * Return: 0 on success and -errno on error. 199 */ 200 static int __si1145_command_reset(struct si1145_data *data) 201 { 202 struct device *dev = &data->client->dev; 203 unsigned long stop_jiffies; 204 int ret; 205 206 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, 207 SI1145_CMD_NOP); 208 if (ret < 0) 209 return ret; 210 msleep(SI1145_COMMAND_MINSLEEP_MS); 211 212 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000; 213 while (true) { 214 ret = i2c_smbus_read_byte_data(data->client, 215 SI1145_REG_RESPONSE); 216 if (ret <= 0) 217 return ret; 218 if (time_after(jiffies, stop_jiffies)) { 219 dev_warn(dev, "timeout on reset\n"); 220 return -ETIMEDOUT; 221 } 222 msleep(SI1145_COMMAND_MINSLEEP_MS); 223 } 224 } 225 226 /* 227 * si1145_command() - Execute a command and poll the response register 228 * 229 * All conversion overflows are reported as -EOVERFLOW 230 * INVALID_SETTING is reported as -EINVAL 231 * Timeouts are reported as -ETIMEDOUT 232 * 233 * Return: 0 on success or -errno on failure 234 */ 235 static int si1145_command(struct si1145_data *data, u8 cmd) 236 { 237 struct device *dev = &data->client->dev; 238 unsigned long stop_jiffies; 239 int ret; 240 241 mutex_lock(&data->cmdlock); 242 243 if (data->rsp_seq < 0) { 244 ret = __si1145_command_reset(data); 245 if (ret < 0) { 246 dev_err(dev, "failed to reset command counter, ret=%d\n", 247 ret); 248 goto out; 249 } 250 data->rsp_seq = 0; 251 } 252 253 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd); 254 if (ret) { 255 dev_warn(dev, "failed to write command, ret=%d\n", ret); 256 goto out; 257 } 258 /* Sleep a little to ensure the command is received */ 259 msleep(SI1145_COMMAND_MINSLEEP_MS); 260 261 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000; 262 while (true) { 263 ret = i2c_smbus_read_byte_data(data->client, 264 SI1145_REG_RESPONSE); 265 if (ret < 0) { 266 dev_warn(dev, "failed to read response, ret=%d\n", ret); 267 break; 268 } 269 270 if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) { 271 if (ret == data->rsp_seq) { 272 if (time_after(jiffies, stop_jiffies)) { 273 dev_warn(dev, "timeout on command 0x%02x\n", 274 cmd); 275 ret = -ETIMEDOUT; 276 break; 277 } 278 msleep(SI1145_COMMAND_MINSLEEP_MS); 279 continue; 280 } 281 if (ret == ((data->rsp_seq + 1) & 282 SI1145_RSP_COUNTER_MASK)) { 283 data->rsp_seq = ret; 284 ret = 0; 285 break; 286 } 287 dev_warn(dev, "unexpected response counter %d instead of %d\n", 288 ret, (data->rsp_seq + 1) & 289 SI1145_RSP_COUNTER_MASK); 290 ret = -EIO; 291 } else { 292 if (ret == SI1145_RSP_INVALID_SETTING) { 293 dev_warn(dev, "INVALID_SETTING error on command 0x%02x\n", 294 cmd); 295 ret = -EINVAL; 296 } else { 297 /* All overflows are treated identically */ 298 dev_dbg(dev, "overflow, ret=%d, cmd=0x%02x\n", 299 ret, cmd); 300 ret = -EOVERFLOW; 301 } 302 } 303 304 /* Force a counter reset next time */ 305 data->rsp_seq = -1; 306 break; 307 } 308 309 out: 310 mutex_unlock(&data->cmdlock); 311 312 return ret; 313 } 314 315 static int si1145_param_update(struct si1145_data *data, u8 op, u8 param, 316 u8 value) 317 { 318 int ret; 319 320 ret = i2c_smbus_write_byte_data(data->client, 321 SI1145_REG_PARAM_WR, value); 322 if (ret < 0) 323 return ret; 324 325 return si1145_command(data, op | (param & 0x1F)); 326 } 327 328 static int si1145_param_set(struct si1145_data *data, u8 param, u8 value) 329 { 330 return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value); 331 } 332 333 /* Set param. Returns negative errno or current value */ 334 static int si1145_param_query(struct si1145_data *data, u8 param) 335 { 336 int ret; 337 338 ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F)); 339 if (ret < 0) 340 return ret; 341 342 return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD); 343 } 344 345 /* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */ 346 static u16 si1145_uncompress(u8 x) 347 { 348 u16 result = 0; 349 u8 exponent = 0; 350 351 if (x < 8) 352 return 0; 353 354 exponent = (x & 0xf0) >> 4; 355 result = 0x10 | (x & 0x0f); 356 357 if (exponent >= 4) 358 return result << (exponent - 4); 359 return result >> (4 - exponent); 360 } 361 362 /* Compress 16 bit value to 8 bit, see Silabs AN498 */ 363 static u8 si1145_compress(u16 x) 364 { 365 u32 exponent = 0; 366 u32 significand = 0; 367 u32 tmp = x; 368 369 if (x == 0x0000) 370 return 0x00; 371 if (x == 0x0001) 372 return 0x08; 373 374 while (1) { 375 tmp >>= 1; 376 exponent += 1; 377 if (tmp == 1) 378 break; 379 } 380 381 if (exponent < 5) { 382 significand = x << (4 - exponent); 383 return (exponent << 4) | (significand & 0xF); 384 } 385 386 significand = x >> (exponent - 5); 387 if (significand & 1) { 388 significand += 2; 389 if (significand & 0x0040) { 390 exponent += 1; 391 significand >>= 1; 392 } 393 } 394 395 return (exponent << 4) | ((significand >> 1) & 0xF); 396 } 397 398 /* Write meas_rate in hardware */ 399 static int si1145_set_meas_rate(struct si1145_data *data, int interval) 400 { 401 if (data->part_info->uncompressed_meas_rate) 402 return i2c_smbus_write_word_data(data->client, 403 SI1145_REG_MEAS_RATE, interval); 404 else 405 return i2c_smbus_write_byte_data(data->client, 406 SI1145_REG_MEAS_RATE, interval); 407 } 408 409 static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2) 410 { 411 *val = 32000; 412 if (data->part_info->uncompressed_meas_rate) 413 *val2 = data->meas_rate; 414 else 415 *val2 = si1145_uncompress(data->meas_rate); 416 return IIO_VAL_FRACTIONAL; 417 } 418 419 /* Set the samp freq in driver private data */ 420 static int si1145_store_samp_freq(struct si1145_data *data, int val) 421 { 422 int ret = 0; 423 int meas_rate; 424 425 if (val <= 0 || val > 32000) 426 return -ERANGE; 427 meas_rate = 32000 / val; 428 429 mutex_lock(&data->lock); 430 if (data->autonomous) { 431 ret = si1145_set_meas_rate(data, meas_rate); 432 if (ret) 433 goto out; 434 } 435 if (data->part_info->uncompressed_meas_rate) 436 data->meas_rate = meas_rate; 437 else 438 data->meas_rate = si1145_compress(meas_rate); 439 440 out: 441 mutex_unlock(&data->lock); 442 443 return ret; 444 } 445 446 static irqreturn_t si1145_trigger_handler(int irq, void *private) 447 { 448 struct iio_poll_func *pf = private; 449 struct iio_dev *indio_dev = pf->indio_dev; 450 struct si1145_data *data = iio_priv(indio_dev); 451 int i, j = 0; 452 int ret; 453 u8 irq_status = 0; 454 455 if (!data->autonomous) { 456 ret = si1145_command(data, SI1145_CMD_PSALS_FORCE); 457 if (ret < 0 && ret != -EOVERFLOW) 458 goto done; 459 } else { 460 irq_status = ret = i2c_smbus_read_byte_data(data->client, 461 SI1145_REG_IRQ_STATUS); 462 if (ret < 0) 463 goto done; 464 if (!(irq_status & SI1145_MASK_ALL_IE)) 465 goto done; 466 } 467 468 iio_for_each_active_channel(indio_dev, i) { 469 int run = 1; 470 471 while (i + run < iio_get_masklength(indio_dev)) { 472 if (!test_bit(i + run, indio_dev->active_scan_mask)) 473 break; 474 if (indio_dev->channels[i + run].address != 475 indio_dev->channels[i].address + 2 * run) 476 break; 477 run++; 478 } 479 480 ret = i2c_smbus_read_i2c_block_data_or_emulated( 481 data->client, indio_dev->channels[i].address, 482 sizeof(u16) * run, &data->buffer[j]); 483 if (ret < 0) 484 goto done; 485 j += run * sizeof(u16); 486 i += run - 1; 487 } 488 489 if (data->autonomous) { 490 ret = i2c_smbus_write_byte_data(data->client, 491 SI1145_REG_IRQ_STATUS, 492 irq_status & SI1145_MASK_ALL_IE); 493 if (ret < 0) 494 goto done; 495 } 496 497 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, 498 iio_get_time_ns(indio_dev)); 499 500 done: 501 iio_trigger_notify_done(indio_dev->trig); 502 return IRQ_HANDLED; 503 } 504 505 static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask) 506 { 507 struct si1145_data *data = iio_priv(indio_dev); 508 u8 reg = 0, mux; 509 int ret; 510 int i; 511 512 /* channel list already set, no need to reprogram */ 513 if (data->scan_mask == scan_mask) 514 return 0; 515 516 for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) { 517 switch (indio_dev->channels[i].address) { 518 case SI1145_REG_ALSVIS_DATA: 519 reg |= SI1145_CHLIST_EN_ALSVIS; 520 break; 521 case SI1145_REG_ALSIR_DATA: 522 reg |= SI1145_CHLIST_EN_ALSIR; 523 break; 524 case SI1145_REG_PS1_DATA: 525 reg |= SI1145_CHLIST_EN_PS1; 526 break; 527 case SI1145_REG_PS2_DATA: 528 reg |= SI1145_CHLIST_EN_PS2; 529 break; 530 case SI1145_REG_PS3_DATA: 531 reg |= SI1145_CHLIST_EN_PS3; 532 break; 533 case SI1145_REG_AUX_DATA: 534 switch (indio_dev->channels[i].type) { 535 case IIO_UVINDEX: 536 reg |= SI1145_CHLIST_EN_UV; 537 break; 538 default: 539 reg |= SI1145_CHLIST_EN_AUX; 540 if (indio_dev->channels[i].type == IIO_TEMP) 541 mux = SI1145_MUX_TEMP; 542 else 543 mux = SI1145_MUX_VDD; 544 ret = si1145_param_set(data, 545 SI1145_PARAM_AUX_ADC_MUX, mux); 546 if (ret < 0) 547 return ret; 548 549 break; 550 } 551 } 552 } 553 554 data->scan_mask = scan_mask; 555 ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg); 556 557 return ret < 0 ? ret : 0; 558 } 559 560 static int si1145_measure(struct iio_dev *indio_dev, 561 struct iio_chan_spec const *chan) 562 { 563 struct si1145_data *data = iio_priv(indio_dev); 564 u8 cmd; 565 int ret; 566 567 ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index)); 568 if (ret < 0) 569 return ret; 570 571 cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE : 572 SI1145_CMD_ALS_FORCE; 573 ret = si1145_command(data, cmd); 574 if (ret < 0 && ret != -EOVERFLOW) 575 return ret; 576 577 return i2c_smbus_read_word_data(data->client, chan->address); 578 } 579 580 /* 581 * Conversion between iio scale and ADC_GAIN values 582 * These could be further adjusted but proximity/intensity are dimensionless 583 */ 584 static const int si1145_proximity_scale_available[] = { 585 128, 64, 32, 16, 8, 4}; 586 static const int si1145_intensity_scale_available[] = { 587 128, 64, 32, 16, 8, 4, 2, 1}; 588 static IIO_CONST_ATTR(in_proximity_scale_available, 589 "128 64 32 16 8 4"); 590 static IIO_CONST_ATTR(in_intensity_scale_available, 591 "128 64 32 16 8 4 2 1"); 592 static IIO_CONST_ATTR(in_intensity_ir_scale_available, 593 "128 64 32 16 8 4 2 1"); 594 595 static int si1145_scale_from_adcgain(int regval) 596 { 597 return 128 >> regval; 598 } 599 600 static int si1145_proximity_adcgain_from_scale(int val, int val2) 601 { 602 val = find_closest_descending(val, si1145_proximity_scale_available, 603 ARRAY_SIZE(si1145_proximity_scale_available)); 604 if (val < 0 || val > 5 || val2 != 0) 605 return -EINVAL; 606 607 return val; 608 } 609 610 static int si1145_intensity_adcgain_from_scale(int val, int val2) 611 { 612 val = find_closest_descending(val, si1145_intensity_scale_available, 613 ARRAY_SIZE(si1145_intensity_scale_available)); 614 if (val < 0 || val > 7 || val2 != 0) 615 return -EINVAL; 616 617 return val; 618 } 619 620 static int si1145_read_raw(struct iio_dev *indio_dev, 621 struct iio_chan_spec const *chan, 622 int *val, int *val2, long mask) 623 { 624 struct si1145_data *data = iio_priv(indio_dev); 625 int ret; 626 u8 reg; 627 628 switch (mask) { 629 case IIO_CHAN_INFO_RAW: 630 switch (chan->type) { 631 case IIO_INTENSITY: 632 case IIO_PROXIMITY: 633 case IIO_VOLTAGE: 634 case IIO_TEMP: 635 case IIO_UVINDEX: 636 if (!iio_device_claim_direct(indio_dev)) 637 return -EBUSY; 638 ret = si1145_measure(indio_dev, chan); 639 iio_device_release_direct(indio_dev); 640 641 if (ret < 0) 642 return ret; 643 644 *val = ret; 645 646 return IIO_VAL_INT; 647 case IIO_CURRENT: 648 ret = i2c_smbus_read_byte_data(data->client, 649 SI1145_PS_LED_REG(chan->channel)); 650 if (ret < 0) 651 return ret; 652 653 *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel)) 654 & 0x0f; 655 656 return IIO_VAL_INT; 657 default: 658 return -EINVAL; 659 } 660 case IIO_CHAN_INFO_SCALE: 661 switch (chan->type) { 662 case IIO_PROXIMITY: 663 reg = SI1145_PARAM_PS_ADC_GAIN; 664 break; 665 case IIO_INTENSITY: 666 if (chan->channel2 == IIO_MOD_LIGHT_IR) 667 reg = SI1145_PARAM_ALSIR_ADC_GAIN; 668 else 669 reg = SI1145_PARAM_ALSVIS_ADC_GAIN; 670 break; 671 case IIO_TEMP: 672 *val = 28; 673 *val2 = 571429; 674 return IIO_VAL_INT_PLUS_MICRO; 675 case IIO_UVINDEX: 676 *val = 0; 677 *val2 = 10000; 678 return IIO_VAL_INT_PLUS_MICRO; 679 default: 680 return -EINVAL; 681 } 682 683 ret = si1145_param_query(data, reg); 684 if (ret < 0) 685 return ret; 686 687 *val = si1145_scale_from_adcgain(ret & 0x07); 688 689 return IIO_VAL_INT; 690 case IIO_CHAN_INFO_OFFSET: 691 switch (chan->type) { 692 case IIO_TEMP: 693 /* 694 * -ADC offset - ADC counts @ 25°C - 695 * 35 * ADC counts / °C 696 */ 697 *val = -256 - 11136 + 25 * 35; 698 return IIO_VAL_INT; 699 default: 700 /* 701 * All ADC measurements have are by default offset 702 * by -256 703 * See AN498 5.6.3 704 */ 705 ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET); 706 if (ret < 0) 707 return ret; 708 *val = -si1145_uncompress(ret); 709 return IIO_VAL_INT; 710 } 711 case IIO_CHAN_INFO_SAMP_FREQ: 712 return si1145_read_samp_freq(data, val, val2); 713 default: 714 return -EINVAL; 715 } 716 } 717 718 static int si1145_write_raw(struct iio_dev *indio_dev, 719 struct iio_chan_spec const *chan, 720 int val, int val2, long mask) 721 { 722 struct si1145_data *data = iio_priv(indio_dev); 723 u8 reg1, reg2, shift; 724 int ret; 725 726 switch (mask) { 727 case IIO_CHAN_INFO_SCALE: 728 switch (chan->type) { 729 case IIO_PROXIMITY: 730 val = si1145_proximity_adcgain_from_scale(val, val2); 731 if (val < 0) 732 return val; 733 reg1 = SI1145_PARAM_PS_ADC_GAIN; 734 reg2 = SI1145_PARAM_PS_ADC_COUNTER; 735 break; 736 case IIO_INTENSITY: 737 val = si1145_intensity_adcgain_from_scale(val, val2); 738 if (val < 0) 739 return val; 740 if (chan->channel2 == IIO_MOD_LIGHT_IR) { 741 reg1 = SI1145_PARAM_ALSIR_ADC_GAIN; 742 reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER; 743 } else { 744 reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN; 745 reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER; 746 } 747 break; 748 default: 749 return -EINVAL; 750 } 751 752 if (!iio_device_claim_direct(indio_dev)) 753 return -EBUSY; 754 755 ret = si1145_param_set(data, reg1, val); 756 if (ret < 0) { 757 iio_device_release_direct(indio_dev); 758 return ret; 759 } 760 /* Set recovery period to one's complement of gain */ 761 ret = si1145_param_set(data, reg2, (~val & 0x07) << 4); 762 iio_device_release_direct(indio_dev); 763 return ret; 764 case IIO_CHAN_INFO_RAW: 765 if (chan->type != IIO_CURRENT) 766 return -EINVAL; 767 768 if (val < 0 || val > 15 || val2 != 0) 769 return -EINVAL; 770 771 reg1 = SI1145_PS_LED_REG(chan->channel); 772 shift = SI1145_PS_LED_SHIFT(chan->channel); 773 774 if (!iio_device_claim_direct(indio_dev)) 775 return -EBUSY; 776 777 ret = i2c_smbus_read_byte_data(data->client, reg1); 778 if (ret < 0) { 779 iio_device_release_direct(indio_dev); 780 return ret; 781 } 782 ret = i2c_smbus_write_byte_data(data->client, reg1, 783 (ret & ~(0x0f << shift)) | 784 ((val & 0x0f) << shift)); 785 iio_device_release_direct(indio_dev); 786 return ret; 787 case IIO_CHAN_INFO_SAMP_FREQ: 788 return si1145_store_samp_freq(data, val); 789 default: 790 return -EINVAL; 791 } 792 } 793 794 #define SI1145_ST { \ 795 .sign = 'u', \ 796 .realbits = 16, \ 797 .storagebits = 16, \ 798 .endianness = IIO_LE, \ 799 } 800 801 #define SI1145_INTENSITY_CHANNEL(_si) { \ 802 .type = IIO_INTENSITY, \ 803 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 804 BIT(IIO_CHAN_INFO_OFFSET) | \ 805 BIT(IIO_CHAN_INFO_SCALE), \ 806 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 807 .scan_type = SI1145_ST, \ 808 .scan_index = _si, \ 809 .address = SI1145_REG_ALSVIS_DATA, \ 810 } 811 812 #define SI1145_INTENSITY_IR_CHANNEL(_si) { \ 813 .type = IIO_INTENSITY, \ 814 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 815 BIT(IIO_CHAN_INFO_OFFSET) | \ 816 BIT(IIO_CHAN_INFO_SCALE), \ 817 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 818 .modified = 1, \ 819 .channel2 = IIO_MOD_LIGHT_IR, \ 820 .scan_type = SI1145_ST, \ 821 .scan_index = _si, \ 822 .address = SI1145_REG_ALSIR_DATA, \ 823 } 824 825 #define SI1145_TEMP_CHANNEL(_si) { \ 826 .type = IIO_TEMP, \ 827 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 828 BIT(IIO_CHAN_INFO_OFFSET) | \ 829 BIT(IIO_CHAN_INFO_SCALE), \ 830 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 831 .scan_type = SI1145_ST, \ 832 .scan_index = _si, \ 833 .address = SI1145_REG_AUX_DATA, \ 834 } 835 836 #define SI1145_UV_CHANNEL(_si) { \ 837 .type = IIO_UVINDEX, \ 838 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 839 BIT(IIO_CHAN_INFO_SCALE), \ 840 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 841 .scan_type = SI1145_ST, \ 842 .scan_index = _si, \ 843 .address = SI1145_REG_AUX_DATA, \ 844 } 845 846 #define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \ 847 .type = IIO_PROXIMITY, \ 848 .indexed = 1, \ 849 .channel = _ch, \ 850 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 851 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 852 BIT(IIO_CHAN_INFO_OFFSET), \ 853 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 854 .scan_type = SI1145_ST, \ 855 .scan_index = _si, \ 856 .address = SI1145_REG_PS1_DATA + _ch * 2, \ 857 } 858 859 #define SI1145_VOLTAGE_CHANNEL(_si) { \ 860 .type = IIO_VOLTAGE, \ 861 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 862 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 863 .scan_type = SI1145_ST, \ 864 .scan_index = _si, \ 865 .address = SI1145_REG_AUX_DATA, \ 866 } 867 868 #define SI1145_CURRENT_CHANNEL(_ch) { \ 869 .type = IIO_CURRENT, \ 870 .indexed = 1, \ 871 .channel = _ch, \ 872 .output = 1, \ 873 .scan_index = -1, \ 874 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 875 } 876 877 static const struct iio_chan_spec si1132_channels[] = { 878 SI1145_INTENSITY_CHANNEL(0), 879 SI1145_INTENSITY_IR_CHANNEL(1), 880 SI1145_TEMP_CHANNEL(2), 881 SI1145_VOLTAGE_CHANNEL(3), 882 SI1145_UV_CHANNEL(4), 883 IIO_CHAN_SOFT_TIMESTAMP(6), 884 }; 885 886 static const struct iio_chan_spec si1141_channels[] = { 887 SI1145_INTENSITY_CHANNEL(0), 888 SI1145_INTENSITY_IR_CHANNEL(1), 889 SI1145_PROXIMITY_CHANNEL(2, 0), 890 SI1145_TEMP_CHANNEL(3), 891 SI1145_VOLTAGE_CHANNEL(4), 892 IIO_CHAN_SOFT_TIMESTAMP(5), 893 SI1145_CURRENT_CHANNEL(0), 894 }; 895 896 static const struct iio_chan_spec si1142_channels[] = { 897 SI1145_INTENSITY_CHANNEL(0), 898 SI1145_INTENSITY_IR_CHANNEL(1), 899 SI1145_PROXIMITY_CHANNEL(2, 0), 900 SI1145_PROXIMITY_CHANNEL(3, 1), 901 SI1145_TEMP_CHANNEL(4), 902 SI1145_VOLTAGE_CHANNEL(5), 903 IIO_CHAN_SOFT_TIMESTAMP(6), 904 SI1145_CURRENT_CHANNEL(0), 905 SI1145_CURRENT_CHANNEL(1), 906 }; 907 908 static const struct iio_chan_spec si1143_channels[] = { 909 SI1145_INTENSITY_CHANNEL(0), 910 SI1145_INTENSITY_IR_CHANNEL(1), 911 SI1145_PROXIMITY_CHANNEL(2, 0), 912 SI1145_PROXIMITY_CHANNEL(3, 1), 913 SI1145_PROXIMITY_CHANNEL(4, 2), 914 SI1145_TEMP_CHANNEL(5), 915 SI1145_VOLTAGE_CHANNEL(6), 916 IIO_CHAN_SOFT_TIMESTAMP(7), 917 SI1145_CURRENT_CHANNEL(0), 918 SI1145_CURRENT_CHANNEL(1), 919 SI1145_CURRENT_CHANNEL(2), 920 }; 921 922 static const struct iio_chan_spec si1145_channels[] = { 923 SI1145_INTENSITY_CHANNEL(0), 924 SI1145_INTENSITY_IR_CHANNEL(1), 925 SI1145_PROXIMITY_CHANNEL(2, 0), 926 SI1145_TEMP_CHANNEL(3), 927 SI1145_VOLTAGE_CHANNEL(4), 928 SI1145_UV_CHANNEL(5), 929 IIO_CHAN_SOFT_TIMESTAMP(6), 930 SI1145_CURRENT_CHANNEL(0), 931 }; 932 933 static const struct iio_chan_spec si1146_channels[] = { 934 SI1145_INTENSITY_CHANNEL(0), 935 SI1145_INTENSITY_IR_CHANNEL(1), 936 SI1145_TEMP_CHANNEL(2), 937 SI1145_VOLTAGE_CHANNEL(3), 938 SI1145_UV_CHANNEL(4), 939 SI1145_PROXIMITY_CHANNEL(5, 0), 940 SI1145_PROXIMITY_CHANNEL(6, 1), 941 IIO_CHAN_SOFT_TIMESTAMP(7), 942 SI1145_CURRENT_CHANNEL(0), 943 SI1145_CURRENT_CHANNEL(1), 944 }; 945 946 static const struct iio_chan_spec si1147_channels[] = { 947 SI1145_INTENSITY_CHANNEL(0), 948 SI1145_INTENSITY_IR_CHANNEL(1), 949 SI1145_PROXIMITY_CHANNEL(2, 0), 950 SI1145_PROXIMITY_CHANNEL(3, 1), 951 SI1145_PROXIMITY_CHANNEL(4, 2), 952 SI1145_TEMP_CHANNEL(5), 953 SI1145_VOLTAGE_CHANNEL(6), 954 SI1145_UV_CHANNEL(7), 955 IIO_CHAN_SOFT_TIMESTAMP(8), 956 SI1145_CURRENT_CHANNEL(0), 957 SI1145_CURRENT_CHANNEL(1), 958 SI1145_CURRENT_CHANNEL(2), 959 }; 960 961 static struct attribute *si1132_attributes[] = { 962 &iio_const_attr_in_intensity_scale_available.dev_attr.attr, 963 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr, 964 NULL, 965 }; 966 967 static struct attribute *si114x_attributes[] = { 968 &iio_const_attr_in_intensity_scale_available.dev_attr.attr, 969 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr, 970 &iio_const_attr_in_proximity_scale_available.dev_attr.attr, 971 NULL, 972 }; 973 974 static const struct attribute_group si1132_attribute_group = { 975 .attrs = si1132_attributes, 976 }; 977 978 static const struct attribute_group si114x_attribute_group = { 979 .attrs = si114x_attributes, 980 }; 981 982 983 static const struct iio_info si1132_info = { 984 .read_raw = si1145_read_raw, 985 .write_raw = si1145_write_raw, 986 .attrs = &si1132_attribute_group, 987 }; 988 989 static const struct iio_info si114x_info = { 990 .read_raw = si1145_read_raw, 991 .write_raw = si1145_write_raw, 992 .attrs = &si114x_attribute_group, 993 }; 994 995 #define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \ 996 {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate} 997 998 static const struct si1145_part_info si1145_part_info[] = { 999 [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true), 1000 [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false), 1001 [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false), 1002 [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false), 1003 [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true), 1004 [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true), 1005 [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true), 1006 }; 1007 1008 static int si1145_initialize(struct si1145_data *data) 1009 { 1010 struct i2c_client *client = data->client; 1011 int ret; 1012 1013 ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND, 1014 SI1145_CMD_RESET); 1015 if (ret < 0) 1016 return ret; 1017 msleep(SI1145_COMMAND_TIMEOUT_MS); 1018 1019 /* Hardware key, magic value */ 1020 ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17); 1021 if (ret < 0) 1022 return ret; 1023 msleep(SI1145_COMMAND_TIMEOUT_MS); 1024 1025 /* Turn off autonomous mode */ 1026 ret = si1145_set_meas_rate(data, 0); 1027 if (ret < 0) 1028 return ret; 1029 1030 /* Initialize sampling freq to 10 Hz */ 1031 ret = si1145_store_samp_freq(data, 10); 1032 if (ret < 0) 1033 return ret; 1034 1035 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */ 1036 switch (data->part_info->num_leds) { 1037 case 3: 1038 ret = i2c_smbus_write_byte_data(client, 1039 SI1145_REG_PS_LED3, 1040 SI1145_LED_CURRENT_45mA); 1041 if (ret < 0) 1042 return ret; 1043 fallthrough; 1044 case 2: 1045 ret = i2c_smbus_write_byte_data(client, 1046 SI1145_REG_PS_LED21, 1047 (SI1145_LED_CURRENT_45mA << 4) | 1048 SI1145_LED_CURRENT_45mA); 1049 break; 1050 case 1: 1051 ret = i2c_smbus_write_byte_data(client, 1052 SI1145_REG_PS_LED21, 1053 SI1145_LED_CURRENT_45mA); 1054 break; 1055 default: 1056 ret = 0; 1057 break; 1058 } 1059 if (ret < 0) 1060 return ret; 1061 1062 /* Set normal proximity measurement mode */ 1063 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC, 1064 SI1145_PS_ADC_MODE_NORMAL); 1065 if (ret < 0) 1066 return ret; 1067 1068 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01); 1069 if (ret < 0) 1070 return ret; 1071 1072 /* ADC_COUNTER should be one complement of ADC_GAIN */ 1073 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4); 1074 if (ret < 0) 1075 return ret; 1076 1077 /* Set ALS visible measurement mode */ 1078 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC, 1079 SI1145_ADC_MISC_RANGE); 1080 if (ret < 0) 1081 return ret; 1082 1083 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03); 1084 if (ret < 0) 1085 return ret; 1086 1087 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER, 1088 0x04 << 4); 1089 if (ret < 0) 1090 return ret; 1091 1092 /* Set ALS IR measurement mode */ 1093 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC, 1094 SI1145_ADC_MISC_RANGE); 1095 if (ret < 0) 1096 return ret; 1097 1098 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01); 1099 if (ret < 0) 1100 return ret; 1101 1102 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER, 1103 0x06 << 4); 1104 if (ret < 0) 1105 return ret; 1106 1107 /* 1108 * Initialize UCOEF to default values in datasheet 1109 * These registers are normally zero on reset 1110 */ 1111 if (data->part_info == &si1145_part_info[SI1132] || 1112 data->part_info == &si1145_part_info[SI1145] || 1113 data->part_info == &si1145_part_info[SI1146] || 1114 data->part_info == &si1145_part_info[SI1147]) { 1115 ret = i2c_smbus_write_byte_data(data->client, 1116 SI1145_REG_UCOEF1, 1117 SI1145_UCOEF1_DEFAULT); 1118 if (ret < 0) 1119 return ret; 1120 ret = i2c_smbus_write_byte_data(data->client, 1121 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT); 1122 if (ret < 0) 1123 return ret; 1124 ret = i2c_smbus_write_byte_data(data->client, 1125 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT); 1126 if (ret < 0) 1127 return ret; 1128 ret = i2c_smbus_write_byte_data(data->client, 1129 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT); 1130 if (ret < 0) 1131 return ret; 1132 } 1133 1134 return 0; 1135 } 1136 1137 /* 1138 * Program the channels we want to measure with CMD_PSALS_AUTO. No need for 1139 * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct) 1140 * mode reprograms the channels list anyway... 1141 */ 1142 static int si1145_buffer_preenable(struct iio_dev *indio_dev) 1143 { 1144 struct si1145_data *data = iio_priv(indio_dev); 1145 int ret; 1146 1147 mutex_lock(&data->lock); 1148 ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask); 1149 mutex_unlock(&data->lock); 1150 1151 return ret; 1152 } 1153 1154 static bool si1145_validate_scan_mask(struct iio_dev *indio_dev, 1155 const unsigned long *scan_mask) 1156 { 1157 struct si1145_data *data = iio_priv(indio_dev); 1158 unsigned int count = 0; 1159 int i; 1160 1161 /* Check that at most one AUX channel is enabled */ 1162 for_each_set_bit(i, scan_mask, data->part_info->num_channels) { 1163 if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA) 1164 count++; 1165 } 1166 1167 return count <= 1; 1168 } 1169 1170 static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = { 1171 .preenable = si1145_buffer_preenable, 1172 .validate_scan_mask = si1145_validate_scan_mask, 1173 }; 1174 1175 /* 1176 * si1145_trigger_set_state() - Set trigger state 1177 * 1178 * When not using triggers interrupts are disabled and measurement rate is 1179 * set to zero in order to minimize power consumption. 1180 */ 1181 static int si1145_trigger_set_state(struct iio_trigger *trig, bool state) 1182 { 1183 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 1184 struct si1145_data *data = iio_priv(indio_dev); 1185 int err = 0, ret; 1186 1187 mutex_lock(&data->lock); 1188 1189 if (state) { 1190 data->autonomous = true; 1191 err = i2c_smbus_write_byte_data(data->client, 1192 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE); 1193 if (err < 0) 1194 goto disable; 1195 err = i2c_smbus_write_byte_data(data->client, 1196 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE); 1197 if (err < 0) 1198 goto disable; 1199 err = si1145_set_meas_rate(data, data->meas_rate); 1200 if (err < 0) 1201 goto disable; 1202 err = si1145_command(data, SI1145_CMD_PSALS_AUTO); 1203 if (err < 0) 1204 goto disable; 1205 } else { 1206 disable: 1207 /* Disable as much as possible skipping errors */ 1208 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE); 1209 if (ret < 0 && !err) 1210 err = ret; 1211 ret = si1145_set_meas_rate(data, 0); 1212 if (ret < 0 && !err) 1213 err = ret; 1214 ret = i2c_smbus_write_byte_data(data->client, 1215 SI1145_REG_IRQ_ENABLE, 0); 1216 if (ret < 0 && !err) 1217 err = ret; 1218 ret = i2c_smbus_write_byte_data(data->client, 1219 SI1145_REG_INT_CFG, 0); 1220 if (ret < 0 && !err) 1221 err = ret; 1222 data->autonomous = false; 1223 } 1224 1225 mutex_unlock(&data->lock); 1226 return err; 1227 } 1228 1229 static const struct iio_trigger_ops si1145_trigger_ops = { 1230 .set_trigger_state = si1145_trigger_set_state, 1231 }; 1232 1233 static int si1145_probe_trigger(struct iio_dev *indio_dev) 1234 { 1235 struct si1145_data *data = iio_priv(indio_dev); 1236 struct i2c_client *client = data->client; 1237 struct iio_trigger *trig; 1238 int ret; 1239 1240 trig = devm_iio_trigger_alloc(&client->dev, 1241 "%s-dev%d", indio_dev->name, iio_device_id(indio_dev)); 1242 if (!trig) 1243 return -ENOMEM; 1244 1245 trig->ops = &si1145_trigger_ops; 1246 iio_trigger_set_drvdata(trig, indio_dev); 1247 1248 ret = devm_request_irq(&client->dev, client->irq, 1249 iio_trigger_generic_data_rdy_poll, 1250 IRQF_TRIGGER_FALLING, 1251 "si1145_irq", 1252 trig); 1253 if (ret < 0) { 1254 dev_err(&client->dev, "irq request failed\n"); 1255 return ret; 1256 } 1257 1258 ret = devm_iio_trigger_register(&client->dev, trig); 1259 if (ret) 1260 return ret; 1261 1262 data->trig = trig; 1263 indio_dev->trig = iio_trigger_get(data->trig); 1264 1265 return 0; 1266 } 1267 1268 static int si1145_probe(struct i2c_client *client) 1269 { 1270 const struct i2c_device_id *id = i2c_client_get_device_id(client); 1271 struct si1145_data *data; 1272 struct iio_dev *indio_dev; 1273 u8 part_id, rev_id, seq_id; 1274 int ret; 1275 1276 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 1277 if (!indio_dev) 1278 return -ENOMEM; 1279 1280 data = iio_priv(indio_dev); 1281 i2c_set_clientdata(client, indio_dev); 1282 data->client = client; 1283 data->part_info = &si1145_part_info[id->driver_data]; 1284 1285 part_id = ret = i2c_smbus_read_byte_data(data->client, 1286 SI1145_REG_PART_ID); 1287 if (ret < 0) 1288 return ret; 1289 rev_id = ret = i2c_smbus_read_byte_data(data->client, 1290 SI1145_REG_REV_ID); 1291 if (ret < 0) 1292 return ret; 1293 seq_id = ret = i2c_smbus_read_byte_data(data->client, 1294 SI1145_REG_SEQ_ID); 1295 if (ret < 0) 1296 return ret; 1297 dev_info(&client->dev, "device ID part 0x%02x rev 0x%02x seq 0x%02x\n", 1298 part_id, rev_id, seq_id); 1299 if (part_id != data->part_info->part) { 1300 dev_err(&client->dev, "part ID mismatch got 0x%02x, expected 0x%02x\n", 1301 part_id, data->part_info->part); 1302 return -ENODEV; 1303 } 1304 1305 indio_dev->name = id->name; 1306 indio_dev->channels = data->part_info->channels; 1307 indio_dev->num_channels = data->part_info->num_channels; 1308 indio_dev->info = data->part_info->iio_info; 1309 indio_dev->modes = INDIO_DIRECT_MODE; 1310 1311 mutex_init(&data->lock); 1312 mutex_init(&data->cmdlock); 1313 1314 ret = si1145_initialize(data); 1315 if (ret < 0) 1316 return ret; 1317 1318 ret = devm_iio_triggered_buffer_setup(&client->dev, 1319 indio_dev, NULL, 1320 si1145_trigger_handler, &si1145_buffer_setup_ops); 1321 if (ret < 0) 1322 return ret; 1323 1324 if (client->irq) { 1325 ret = si1145_probe_trigger(indio_dev); 1326 if (ret < 0) 1327 return ret; 1328 } else { 1329 dev_info(&client->dev, "no irq, using polling\n"); 1330 } 1331 1332 return devm_iio_device_register(&client->dev, indio_dev); 1333 } 1334 1335 static const struct i2c_device_id si1145_ids[] = { 1336 { "si1132", SI1132 }, 1337 { "si1141", SI1141 }, 1338 { "si1142", SI1142 }, 1339 { "si1143", SI1143 }, 1340 { "si1145", SI1145 }, 1341 { "si1146", SI1146 }, 1342 { "si1147", SI1147 }, 1343 { } 1344 }; 1345 MODULE_DEVICE_TABLE(i2c, si1145_ids); 1346 1347 static struct i2c_driver si1145_driver = { 1348 .driver = { 1349 .name = "si1145", 1350 }, 1351 .probe = si1145_probe, 1352 .id_table = si1145_ids, 1353 }; 1354 1355 module_i2c_driver(si1145_driver); 1356 1357 MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); 1358 MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver"); 1359 MODULE_LICENSE("GPL"); 1360