linux/drivers/iio/light/tsl2591.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2021 Joe Sandom <joe.g.sandom@gmail.com>
   4 *
   5 * Datasheet: https://ams.com/tsl25911#tab/documents
   6 *
   7 * Device driver for the TAOS TSL2591. This is a very-high sensitivity
   8 * light-to-digital converter that transforms light intensity into a digital
   9 * signal.
  10 */
  11
  12#include <linux/bitfield.h>
  13#include <linux/debugfs.h>
  14#include <linux/delay.h>
  15#include <linux/i2c.h>
  16#include <linux/interrupt.h>
  17#include <linux/iopoll.h>
  18#include <linux/kernel.h>
  19#include <linux/module.h>
  20#include <linux/mutex.h>
  21#include <linux/pm_runtime.h>
  22#include <linux/sysfs.h>
  23
  24#include <asm/unaligned.h>
  25
  26#include <linux/iio/events.h>
  27#include <linux/iio/iio.h>
  28#include <linux/iio/sysfs.h>
  29
  30/* ADC integration time, field value to time in ms */
  31#define TSL2591_FVAL_TO_MSEC(x) (((x) + 1) * 100)
  32/* ADC integration time, field value to time in seconds */
  33#define TSL2591_FVAL_TO_SEC(x) ((x) + 1)
  34/* ADC integration time, time in seconds to field value */
  35#define TSL2591_SEC_TO_FVAL(x) ((x) - 1)
  36
  37/* TSL2591 register set */
  38#define TSL2591_ENABLE      0x00
  39#define TSL2591_CONTROL     0x01
  40#define TSL2591_AILTL       0x04
  41#define TSL2591_AILTH       0x05
  42#define TSL2591_AIHTL       0x06
  43#define TSL2591_AIHTH       0x07
  44#define TSL2591_NP_AILTL    0x08
  45#define TSL2591_NP_AILTH    0x09
  46#define TSL2591_NP_AIHTL    0x0A
  47#define TSL2591_NP_AIHTH    0x0B
  48#define TSL2591_PERSIST     0x0C
  49#define TSL2591_PACKAGE_ID  0x11
  50#define TSL2591_DEVICE_ID   0x12
  51#define TSL2591_STATUS      0x13
  52#define TSL2591_C0_DATAL    0x14
  53#define TSL2591_C0_DATAH    0x15
  54#define TSL2591_C1_DATAL    0x16
  55#define TSL2591_C1_DATAH    0x17
  56
  57/* TSL2591 command register definitions */
  58#define TSL2591_CMD_NOP             0xA0
  59#define TSL2591_CMD_SF_INTSET       0xE4
  60#define TSL2591_CMD_SF_CALS_I       0xE5
  61#define TSL2591_CMD_SF_CALS_NPI     0xE7
  62#define TSL2591_CMD_SF_CNP_ALSI     0xEA
  63
  64/* TSL2591 enable register definitions */
  65#define TSL2591_PWR_ON              0x01
  66#define TSL2591_PWR_OFF             0x00
  67#define TSL2591_ENABLE_ALS          0x02
  68#define TSL2591_ENABLE_ALS_INT      0x10
  69#define TSL2591_ENABLE_SLEEP_INT    0x40
  70#define TSL2591_ENABLE_NP_INT       0x80
  71
  72/* TSL2591 control register definitions */
  73#define TSL2591_CTRL_ALS_INTEGRATION_100MS  0x00
  74#define TSL2591_CTRL_ALS_INTEGRATION_200MS  0x01
  75#define TSL2591_CTRL_ALS_INTEGRATION_300MS  0x02
  76#define TSL2591_CTRL_ALS_INTEGRATION_400MS  0x03
  77#define TSL2591_CTRL_ALS_INTEGRATION_500MS  0x04
  78#define TSL2591_CTRL_ALS_INTEGRATION_600MS  0x05
  79#define TSL2591_CTRL_ALS_LOW_GAIN           0x00
  80#define TSL2591_CTRL_ALS_MED_GAIN           0x10
  81#define TSL2591_CTRL_ALS_HIGH_GAIN          0x20
  82#define TSL2591_CTRL_ALS_MAX_GAIN           0x30
  83#define TSL2591_CTRL_SYS_RESET              0x80
  84
  85/* TSL2591 persist register definitions */
  86#define TSL2591_PRST_ALS_INT_CYCLE_0        0x00
  87#define TSL2591_PRST_ALS_INT_CYCLE_ANY      0x01
  88#define TSL2591_PRST_ALS_INT_CYCLE_2        0x02
  89#define TSL2591_PRST_ALS_INT_CYCLE_3        0x03
  90#define TSL2591_PRST_ALS_INT_CYCLE_5        0x04
  91#define TSL2591_PRST_ALS_INT_CYCLE_10       0x05
  92#define TSL2591_PRST_ALS_INT_CYCLE_15       0x06
  93#define TSL2591_PRST_ALS_INT_CYCLE_20       0x07
  94#define TSL2591_PRST_ALS_INT_CYCLE_25       0x08
  95#define TSL2591_PRST_ALS_INT_CYCLE_30       0x09
  96#define TSL2591_PRST_ALS_INT_CYCLE_35       0x0A
  97#define TSL2591_PRST_ALS_INT_CYCLE_40       0x0B
  98#define TSL2591_PRST_ALS_INT_CYCLE_45       0x0C
  99#define TSL2591_PRST_ALS_INT_CYCLE_50       0x0D
 100#define TSL2591_PRST_ALS_INT_CYCLE_55       0x0E
 101#define TSL2591_PRST_ALS_INT_CYCLE_60       0x0F
 102#define TSL2591_PRST_ALS_INT_CYCLE_MAX      (BIT(4) - 1)
 103
 104/* TSL2591 PID register mask */
 105#define TSL2591_PACKAGE_ID_MASK  GENMASK(5, 4)
 106
 107/* TSL2591 ID register mask */
 108#define TSL2591_DEVICE_ID_MASK   GENMASK(7, 0)
 109
 110/* TSL2591 status register masks */
 111#define TSL2591_STS_ALS_VALID_MASK   BIT(0)
 112#define TSL2591_STS_ALS_INT_MASK     BIT(4)
 113#define TSL2591_STS_NPERS_INT_MASK   BIT(5)
 114#define TSL2591_STS_VAL_HIGH_MASK    BIT(0)
 115
 116/* TSL2591 constant values */
 117#define TSL2591_PACKAGE_ID_VAL  0x00
 118#define TSL2591_DEVICE_ID_VAL   0x50
 119
 120/* Power off suspend delay time MS */
 121#define TSL2591_POWER_OFF_DELAY_MS   2000
 122
 123/* TSL2591 default values */
 124#define TSL2591_DEFAULT_ALS_INT_TIME          TSL2591_CTRL_ALS_INTEGRATION_300MS
 125#define TSL2591_DEFAULT_ALS_GAIN              TSL2591_CTRL_ALS_MED_GAIN
 126#define TSL2591_DEFAULT_ALS_PERSIST           TSL2591_PRST_ALS_INT_CYCLE_ANY
 127#define TSL2591_DEFAULT_ALS_LOWER_THRESH      100
 128#define TSL2591_DEFAULT_ALS_UPPER_THRESH      1500
 129
 130/* TSL2591 number of data registers */
 131#define TSL2591_NUM_DATA_REGISTERS     4
 132
 133/* TSL2591 number of valid status reads on ADC complete */
 134#define TSL2591_ALS_STS_VALID_COUNT    10
 135
 136/* TSL2591 delay period between polls when checking for ALS valid flag */
 137#define TSL2591_DELAY_PERIOD_US        10000
 138
 139/* TSL2591 maximum values */
 140#define TSL2591_MAX_ALS_INT_TIME_MS    600
 141#define TSL2591_ALS_MAX_VALUE          (BIT(16) - 1)
 142
 143/*
 144 * LUX calculations;
 145 * AGAIN values from Adafruit's TSL2591 Arduino library
 146 * https://github.com/adafruit/Adafruit_TSL2591_Library
 147 */
 148#define TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER   1
 149#define TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER   25
 150#define TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER  428
 151#define TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER   9876
 152#define TSL2591_LUX_COEFFICIENT                408
 153
 154struct tsl2591_als_settings {
 155        u16 als_lower_thresh;
 156        u16 als_upper_thresh;
 157        u8 als_int_time;
 158        u8 als_persist;
 159        u8 als_gain;
 160};
 161
 162struct tsl2591_chip {
 163        struct tsl2591_als_settings als_settings;
 164        struct i2c_client *client;
 165        /*
 166         * Keep als_settings in sync with hardware state
 167         * and ensure multiple readers are serialized.
 168         */
 169        struct mutex als_mutex;
 170        bool events_enabled;
 171};
 172
 173/*
 174 * Period table is ALS persist cycle x integration time setting
 175 * Integration times: 100ms, 200ms, 300ms, 400ms, 500ms, 600ms
 176 * ALS cycles: 1, 2, 3, 5, 10, 20, 25, 30, 35, 40, 45, 50, 55, 60
 177 */
 178static const char * const tsl2591_als_period_list[] = {
 179        "0.1 0.2 0.3 0.5 1.0 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0",
 180        "0.2 0.4 0.6 1.0 2.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0",
 181        "0.3 0.6 0.9 1.5 3.0 6.0 7.5 9.0 10.5 12.0 13.5 15.0 16.5 18.0",
 182        "0.4 0.8 1.2 2.0 4.0 8.0 10.0 12.0 14.0 16.0 18.0 20.0 22.0 24.0",
 183        "0.5 1.0 1.5 2.5 5.0 10.0 12.5 15.0 17.5 20.0 22.5 25.0 27.5 30.0",
 184        "0.6 1.2 1.8 3.0 6.0 12.0 15.0 18.0 21.0 24.0 27.0 30.0 33.0 36.0",
 185};
 186
 187static const int tsl2591_int_time_available[] = {
 188        1, 2, 3, 4, 5, 6,
 189};
 190
 191static const int tsl2591_calibscale_available[] = {
 192        1, 25, 428, 9876,
 193};
 194
 195static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
 196                                           u16 als_lower_threshold);
 197static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
 198                                           u16 als_upper_threshold);
 199
 200static int tsl2591_gain_to_multiplier(const u8 als_gain)
 201{
 202        switch (als_gain) {
 203        case TSL2591_CTRL_ALS_LOW_GAIN:
 204                return TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER;
 205        case TSL2591_CTRL_ALS_MED_GAIN:
 206                return TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER;
 207        case TSL2591_CTRL_ALS_HIGH_GAIN:
 208                return TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER;
 209        case TSL2591_CTRL_ALS_MAX_GAIN:
 210                return TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER;
 211        default:
 212                return -EINVAL;
 213        }
 214}
 215
 216static int tsl2591_multiplier_to_gain(const u32 multiplier)
 217{
 218        switch (multiplier) {
 219        case TSL2591_CTRL_ALS_LOW_GAIN_MULTIPLIER:
 220                return TSL2591_CTRL_ALS_LOW_GAIN;
 221        case TSL2591_CTRL_ALS_MED_GAIN_MULTIPLIER:
 222                return TSL2591_CTRL_ALS_MED_GAIN;
 223        case TSL2591_CTRL_ALS_HIGH_GAIN_MULTIPLIER:
 224                return TSL2591_CTRL_ALS_HIGH_GAIN;
 225        case TSL2591_CTRL_ALS_MAX_GAIN_MULTIPLIER:
 226                return TSL2591_CTRL_ALS_MAX_GAIN;
 227        default:
 228                return -EINVAL;
 229        }
 230}
 231
 232static int tsl2591_persist_cycle_to_lit(const u8 als_persist)
 233{
 234        switch (als_persist) {
 235        case TSL2591_PRST_ALS_INT_CYCLE_ANY:
 236                return 1;
 237        case TSL2591_PRST_ALS_INT_CYCLE_2:
 238                return 2;
 239        case TSL2591_PRST_ALS_INT_CYCLE_3:
 240                return 3;
 241        case TSL2591_PRST_ALS_INT_CYCLE_5:
 242                return 5;
 243        case TSL2591_PRST_ALS_INT_CYCLE_10:
 244                return 10;
 245        case TSL2591_PRST_ALS_INT_CYCLE_15:
 246                return 15;
 247        case TSL2591_PRST_ALS_INT_CYCLE_20:
 248                return 20;
 249        case TSL2591_PRST_ALS_INT_CYCLE_25:
 250                return 25;
 251        case TSL2591_PRST_ALS_INT_CYCLE_30:
 252                return 30;
 253        case TSL2591_PRST_ALS_INT_CYCLE_35:
 254                return 35;
 255        case TSL2591_PRST_ALS_INT_CYCLE_40:
 256                return 40;
 257        case TSL2591_PRST_ALS_INT_CYCLE_45:
 258                return 45;
 259        case TSL2591_PRST_ALS_INT_CYCLE_50:
 260                return 50;
 261        case TSL2591_PRST_ALS_INT_CYCLE_55:
 262                return 55;
 263        case TSL2591_PRST_ALS_INT_CYCLE_60:
 264                return 60;
 265        default:
 266                return -EINVAL;
 267        }
 268}
 269
 270static int tsl2591_persist_lit_to_cycle(const u8 als_persist)
 271{
 272        switch (als_persist) {
 273        case 1:
 274                return TSL2591_PRST_ALS_INT_CYCLE_ANY;
 275        case 2:
 276                return TSL2591_PRST_ALS_INT_CYCLE_2;
 277        case 3:
 278                return TSL2591_PRST_ALS_INT_CYCLE_3;
 279        case 5:
 280                return TSL2591_PRST_ALS_INT_CYCLE_5;
 281        case 10:
 282                return TSL2591_PRST_ALS_INT_CYCLE_10;
 283        case 15:
 284                return TSL2591_PRST_ALS_INT_CYCLE_15;
 285        case 20:
 286                return TSL2591_PRST_ALS_INT_CYCLE_20;
 287        case 25:
 288                return TSL2591_PRST_ALS_INT_CYCLE_25;
 289        case 30:
 290                return TSL2591_PRST_ALS_INT_CYCLE_30;
 291        case 35:
 292                return TSL2591_PRST_ALS_INT_CYCLE_35;
 293        case 40:
 294                return TSL2591_PRST_ALS_INT_CYCLE_40;
 295        case 45:
 296                return TSL2591_PRST_ALS_INT_CYCLE_45;
 297        case 50:
 298                return TSL2591_PRST_ALS_INT_CYCLE_50;
 299        case 55:
 300                return TSL2591_PRST_ALS_INT_CYCLE_55;
 301        case 60:
 302                return TSL2591_PRST_ALS_INT_CYCLE_60;
 303        default:
 304                return -EINVAL;
 305        }
 306}
 307
 308static int tsl2591_compatible_int_time(struct tsl2591_chip *chip,
 309                                       const u32 als_integration_time)
 310{
 311        switch (als_integration_time) {
 312        case TSL2591_CTRL_ALS_INTEGRATION_100MS:
 313        case TSL2591_CTRL_ALS_INTEGRATION_200MS:
 314        case TSL2591_CTRL_ALS_INTEGRATION_300MS:
 315        case TSL2591_CTRL_ALS_INTEGRATION_400MS:
 316        case TSL2591_CTRL_ALS_INTEGRATION_500MS:
 317        case TSL2591_CTRL_ALS_INTEGRATION_600MS:
 318                return 0;
 319        default:
 320                return -EINVAL;
 321        }
 322}
 323
 324static int tsl2591_als_time_to_fval(const u32 als_integration_time)
 325{
 326        int i;
 327
 328        for (i = 0; i < ARRAY_SIZE(tsl2591_int_time_available); i++) {
 329                if (als_integration_time == tsl2591_int_time_available[i])
 330                        return TSL2591_SEC_TO_FVAL(als_integration_time);
 331        }
 332
 333        return -EINVAL;
 334}
 335
 336static int tsl2591_compatible_gain(struct tsl2591_chip *chip, const u8 als_gain)
 337{
 338        switch (als_gain) {
 339        case TSL2591_CTRL_ALS_LOW_GAIN:
 340        case TSL2591_CTRL_ALS_MED_GAIN:
 341        case TSL2591_CTRL_ALS_HIGH_GAIN:
 342        case TSL2591_CTRL_ALS_MAX_GAIN:
 343                return 0;
 344        default:
 345                return -EINVAL;
 346        }
 347}
 348
 349static int tsl2591_compatible_als_persist_cycle(struct tsl2591_chip *chip,
 350                                                const u32 als_persist)
 351{
 352        switch (als_persist) {
 353        case TSL2591_PRST_ALS_INT_CYCLE_ANY:
 354        case TSL2591_PRST_ALS_INT_CYCLE_2:
 355        case TSL2591_PRST_ALS_INT_CYCLE_3:
 356        case TSL2591_PRST_ALS_INT_CYCLE_5:
 357        case TSL2591_PRST_ALS_INT_CYCLE_10:
 358        case TSL2591_PRST_ALS_INT_CYCLE_15:
 359        case TSL2591_PRST_ALS_INT_CYCLE_20:
 360        case TSL2591_PRST_ALS_INT_CYCLE_25:
 361        case TSL2591_PRST_ALS_INT_CYCLE_30:
 362        case TSL2591_PRST_ALS_INT_CYCLE_35:
 363        case TSL2591_PRST_ALS_INT_CYCLE_40:
 364        case TSL2591_PRST_ALS_INT_CYCLE_45:
 365        case TSL2591_PRST_ALS_INT_CYCLE_50:
 366        case TSL2591_PRST_ALS_INT_CYCLE_55:
 367        case TSL2591_PRST_ALS_INT_CYCLE_60:
 368                return 0;
 369        default:
 370                return -EINVAL;
 371        }
 372}
 373
 374static int tsl2591_check_als_valid(struct i2c_client *client)
 375{
 376        int ret;
 377
 378        ret = i2c_smbus_read_byte_data(client, TSL2591_CMD_NOP | TSL2591_STATUS);
 379        if (ret < 0) {
 380                dev_err(&client->dev, "Failed to read register\n");
 381                return -EINVAL;
 382        }
 383
 384        return FIELD_GET(TSL2591_STS_ALS_VALID_MASK, ret);
 385}
 386
 387static int tsl2591_wait_adc_complete(struct tsl2591_chip *chip)
 388{
 389        struct tsl2591_als_settings settings = chip->als_settings;
 390        struct i2c_client *client = chip->client;
 391        int delay;
 392        int val;
 393        int ret;
 394
 395        delay = TSL2591_FVAL_TO_MSEC(settings.als_int_time);
 396        if (!delay)
 397                return -EINVAL;
 398
 399        /*
 400         * Sleep for ALS integration time to allow enough time or an ADC read
 401         * cycle to complete. Check status after delay for ALS valid.
 402         */
 403        msleep(delay);
 404
 405        /* Check for status ALS valid flag for up to 100ms */
 406        ret = readx_poll_timeout(tsl2591_check_als_valid, client,
 407                                 val, val == TSL2591_STS_VAL_HIGH_MASK,
 408                                 TSL2591_DELAY_PERIOD_US,
 409                                 TSL2591_DELAY_PERIOD_US * TSL2591_ALS_STS_VALID_COUNT);
 410        if (ret)
 411                dev_err(&client->dev, "Timed out waiting for valid ALS data\n");
 412
 413        return ret;
 414}
 415
 416/*
 417 * tsl2591_read_channel_data - Reads raw channel data and calculates lux
 418 *
 419 * Formula for lux calculation;
 420 * Derived from Adafruit's TSL2591 library
 421 * Link: https://github.com/adafruit/Adafruit_TSL2591_Library
 422 * Counts Per Lux (CPL) = (ATIME_ms * AGAIN) / LUX DF
 423 * lux = ((C0DATA - C1DATA) * (1 - (C1DATA / C0DATA))) / CPL
 424 *
 425 * Scale values to get more representative value of lux i.e.
 426 * lux = ((C0DATA - C1DATA) * (1000 - ((C1DATA * 1000) / C0DATA))) / CPL
 427 *
 428 * Channel 0 = IR + Visible
 429 * Channel 1 = IR only
 430 */
 431static int tsl2591_read_channel_data(struct iio_dev *indio_dev,
 432                                     struct iio_chan_spec const *chan,
 433                                     int *val, int *val2)
 434{
 435        struct tsl2591_chip *chip = iio_priv(indio_dev);
 436        struct tsl2591_als_settings *settings = &chip->als_settings;
 437        struct i2c_client *client = chip->client;
 438        u8 als_data[TSL2591_NUM_DATA_REGISTERS];
 439        int counts_per_lux, int_time_fval, gain_multi, lux;
 440        u16 als_ch0, als_ch1;
 441        int ret;
 442
 443        ret = tsl2591_wait_adc_complete(chip);
 444        if (ret < 0) {
 445                dev_err(&client->dev, "No data available. Err: %d\n", ret);
 446                return ret;
 447        }
 448
 449        ret = i2c_smbus_read_i2c_block_data(client,
 450                                            TSL2591_CMD_NOP | TSL2591_C0_DATAL,
 451                                            sizeof(als_data), als_data);
 452        if (ret < 0) {
 453                dev_err(&client->dev, "Failed to read data bytes");
 454                return ret;
 455        }
 456
 457        als_ch0 = get_unaligned_le16(&als_data[0]);
 458        als_ch1 = get_unaligned_le16(&als_data[2]);
 459
 460        switch (chan->type) {
 461        case IIO_INTENSITY:
 462                if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
 463                        *val = als_ch0;
 464                else if (chan->channel2 == IIO_MOD_LIGHT_IR)
 465                        *val = als_ch1;
 466                else
 467                        return -EINVAL;
 468                break;
 469        case IIO_LIGHT:
 470                gain_multi = tsl2591_gain_to_multiplier(settings->als_gain);
 471                if (gain_multi < 0) {
 472                        dev_err(&client->dev, "Invalid multiplier");
 473                        return gain_multi;
 474                }
 475
 476                int_time_fval = TSL2591_FVAL_TO_MSEC(settings->als_int_time);
 477                /* Calculate counts per lux value */
 478                counts_per_lux = (int_time_fval * gain_multi) / TSL2591_LUX_COEFFICIENT;
 479
 480                dev_dbg(&client->dev, "Counts Per Lux: %d\n", counts_per_lux);
 481
 482                /* Calculate lux value */
 483                lux = ((als_ch0 - als_ch1) *
 484                       (1000 - ((als_ch1 * 1000) / als_ch0))) / counts_per_lux;
 485
 486                dev_dbg(&client->dev, "Raw lux calculation: %d\n", lux);
 487
 488                /* Divide by 1000 to get real lux value before scaling */
 489                *val = lux / 1000;
 490
 491                /* Get the decimal part of lux reading */
 492                *val2 = (lux - (*val * 1000)) * 1000;
 493
 494                break;
 495        default:
 496                return -EINVAL;
 497        }
 498
 499        return 0;
 500}
 501
 502static int tsl2591_set_als_gain_int_time(struct tsl2591_chip *chip)
 503{
 504        struct tsl2591_als_settings als_settings = chip->als_settings;
 505        struct i2c_client *client = chip->client;
 506        int ret;
 507
 508        ret = i2c_smbus_write_byte_data(client,
 509                                        TSL2591_CMD_NOP | TSL2591_CONTROL,
 510                                        als_settings.als_int_time | als_settings.als_gain);
 511        if (ret)
 512                dev_err(&client->dev, "Failed to set als gain & int time\n");
 513
 514        return ret;
 515}
 516
 517static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip,
 518                                           u16 als_lower_threshold)
 519{
 520        struct tsl2591_als_settings als_settings = chip->als_settings;
 521        struct i2c_client *client = chip->client;
 522        u16 als_upper_threshold;
 523        u8 als_lower_l;
 524        u8 als_lower_h;
 525        int ret;
 526
 527        chip->als_settings.als_lower_thresh = als_lower_threshold;
 528
 529        /*
 530         * Lower threshold should not be greater or equal to upper.
 531         * If this is the case, then assert upper threshold to new lower
 532         * threshold + 1 to avoid ordering issues when setting thresholds.
 533         */
 534        if (als_lower_threshold >= als_settings.als_upper_thresh) {
 535                als_upper_threshold = als_lower_threshold + 1;
 536                tsl2591_set_als_upper_threshold(chip, als_upper_threshold);
 537        }
 538
 539        als_lower_l = als_lower_threshold;
 540        als_lower_h = als_lower_threshold >> 8;
 541
 542        ret = i2c_smbus_write_byte_data(client,
 543                                        TSL2591_CMD_NOP | TSL2591_AILTL,
 544                                        als_lower_l);
 545        if (ret) {
 546                dev_err(&client->dev, "Failed to set als lower threshold\n");
 547                return ret;
 548        }
 549
 550        ret = i2c_smbus_write_byte_data(client,
 551                                        TSL2591_CMD_NOP | TSL2591_AILTH,
 552                                        als_lower_h);
 553        if (ret) {
 554                dev_err(&client->dev, "Failed to set als lower threshold\n");
 555                return ret;
 556        }
 557
 558        return 0;
 559}
 560
 561static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip,
 562                                           u16 als_upper_threshold)
 563{
 564        struct tsl2591_als_settings als_settings = chip->als_settings;
 565        struct i2c_client *client = chip->client;
 566        u16 als_lower_threshold;
 567        u8 als_upper_l;
 568        u8 als_upper_h;
 569        int ret;
 570
 571        if (als_upper_threshold > TSL2591_ALS_MAX_VALUE)
 572                return -EINVAL;
 573
 574        chip->als_settings.als_upper_thresh = als_upper_threshold;
 575
 576        /*
 577         * Upper threshold should not be less than lower. If this
 578         * is the case, then assert lower threshold to new upper
 579         * threshold - 1 to avoid ordering issues when setting thresholds.
 580         */
 581        if (als_upper_threshold < als_settings.als_lower_thresh) {
 582                als_lower_threshold = als_upper_threshold - 1;
 583                tsl2591_set_als_lower_threshold(chip, als_lower_threshold);
 584        }
 585
 586        als_upper_l = als_upper_threshold;
 587        als_upper_h = als_upper_threshold >> 8;
 588
 589        ret = i2c_smbus_write_byte_data(client,
 590                                        TSL2591_CMD_NOP | TSL2591_AIHTL,
 591                                        als_upper_l);
 592        if (ret) {
 593                dev_err(&client->dev, "Failed to set als upper threshold\n");
 594                return ret;
 595        }
 596
 597        ret = i2c_smbus_write_byte_data(client,
 598                                        TSL2591_CMD_NOP | TSL2591_AIHTH,
 599                                        als_upper_h);
 600        if (ret) {
 601                dev_err(&client->dev, "Failed to set als upper threshold\n");
 602                return ret;
 603        }
 604
 605        return 0;
 606}
 607
 608static int tsl2591_set_als_persist_cycle(struct tsl2591_chip *chip,
 609                                         u8 als_persist)
 610{
 611        struct i2c_client *client = chip->client;
 612        int ret;
 613
 614        ret = i2c_smbus_write_byte_data(client,
 615                                        TSL2591_CMD_NOP | TSL2591_PERSIST,
 616                                        als_persist);
 617        if (ret)
 618                dev_err(&client->dev, "Failed to set als persist cycle\n");
 619
 620        chip->als_settings.als_persist = als_persist;
 621
 622        return ret;
 623}
 624
 625static int tsl2591_set_power_state(struct tsl2591_chip *chip, u8 state)
 626{
 627        struct i2c_client *client = chip->client;
 628        int ret;
 629
 630        ret = i2c_smbus_write_byte_data(client,
 631                                        TSL2591_CMD_NOP | TSL2591_ENABLE,
 632                                        state);
 633        if (ret)
 634                dev_err(&client->dev,
 635                        "Failed to set the power state to %#04x\n", state);
 636
 637        return ret;
 638}
 639
 640static ssize_t tsl2591_in_illuminance_period_available_show(struct device *dev,
 641                                                            struct device_attribute *attr,
 642                                                            char *buf)
 643{
 644        struct iio_dev *indio_dev = dev_to_iio_dev(dev);
 645        struct tsl2591_chip *chip = iio_priv(indio_dev);
 646
 647        return sysfs_emit(buf, "%s\n",
 648                       tsl2591_als_period_list[chip->als_settings.als_int_time]);
 649}
 650
 651static IIO_DEVICE_ATTR_RO(tsl2591_in_illuminance_period_available, 0);
 652
 653static struct attribute *tsl2591_event_attrs_ctrl[] = {
 654        &iio_dev_attr_tsl2591_in_illuminance_period_available.dev_attr.attr,
 655        NULL
 656};
 657
 658static const struct attribute_group tsl2591_event_attribute_group = {
 659        .attrs = tsl2591_event_attrs_ctrl,
 660};
 661
 662static const struct iio_event_spec tsl2591_events[] = {
 663        {
 664                .type = IIO_EV_TYPE_THRESH,
 665                .dir = IIO_EV_DIR_RISING,
 666                .mask_separate = BIT(IIO_EV_INFO_VALUE),
 667        }, {
 668                .type = IIO_EV_TYPE_THRESH,
 669                .dir = IIO_EV_DIR_FALLING,
 670                .mask_separate = BIT(IIO_EV_INFO_VALUE),
 671        }, {
 672                .type = IIO_EV_TYPE_THRESH,
 673                .dir = IIO_EV_DIR_EITHER,
 674                .mask_separate = BIT(IIO_EV_INFO_PERIOD) |
 675                                BIT(IIO_EV_INFO_ENABLE),
 676        },
 677};
 678
 679static const struct iio_chan_spec tsl2591_channels[] = {
 680        {
 681                .type = IIO_INTENSITY,
 682                .modified = 1,
 683                .channel2 = IIO_MOD_LIGHT_IR,
 684                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 685                .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
 686                                                     BIT(IIO_CHAN_INFO_CALIBSCALE),
 687                .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
 688                                           BIT(IIO_CHAN_INFO_CALIBSCALE)
 689        },
 690        {
 691                .type = IIO_INTENSITY,
 692                .modified = 1,
 693                .channel2 = IIO_MOD_LIGHT_BOTH,
 694                .event_spec = tsl2591_events,
 695                .num_event_specs = ARRAY_SIZE(tsl2591_events),
 696                .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
 697                .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
 698                                                     BIT(IIO_CHAN_INFO_CALIBSCALE),
 699                .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
 700                                           BIT(IIO_CHAN_INFO_CALIBSCALE)
 701        },
 702        {
 703                .type = IIO_LIGHT,
 704                .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
 705                .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) |
 706                                                     BIT(IIO_CHAN_INFO_CALIBSCALE),
 707                .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME) |
 708                                           BIT(IIO_CHAN_INFO_CALIBSCALE)
 709        },
 710};
 711
 712static int tsl2591_read_raw(struct iio_dev *indio_dev,
 713                            struct iio_chan_spec const *chan,
 714                            int *val, int *val2, long mask)
 715{
 716        struct tsl2591_chip *chip = iio_priv(indio_dev);
 717        struct i2c_client *client = chip->client;
 718        int ret;
 719
 720        pm_runtime_get_sync(&client->dev);
 721
 722        mutex_lock(&chip->als_mutex);
 723
 724        switch (mask) {
 725        case IIO_CHAN_INFO_RAW:
 726                if (chan->type != IIO_INTENSITY) {
 727                        ret = -EINVAL;
 728                        goto err_unlock;
 729                }
 730
 731                ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
 732                if (ret < 0)
 733                        goto err_unlock;
 734
 735                ret = IIO_VAL_INT;
 736                break;
 737        case IIO_CHAN_INFO_PROCESSED:
 738                if (chan->type != IIO_LIGHT) {
 739                        ret = -EINVAL;
 740                        goto err_unlock;
 741                }
 742
 743                ret = tsl2591_read_channel_data(indio_dev, chan, val, val2);
 744                if (ret < 0)
 745                        break;
 746
 747                ret = IIO_VAL_INT_PLUS_MICRO;
 748                break;
 749        case IIO_CHAN_INFO_INT_TIME:
 750                if (chan->type != IIO_INTENSITY) {
 751                        ret = -EINVAL;
 752                        goto err_unlock;
 753                }
 754
 755                *val = TSL2591_FVAL_TO_SEC(chip->als_settings.als_int_time);
 756                ret = IIO_VAL_INT;
 757                break;
 758        case IIO_CHAN_INFO_CALIBSCALE:
 759                if (chan->type != IIO_INTENSITY) {
 760                        ret = -EINVAL;
 761                        goto err_unlock;
 762                }
 763
 764                *val = tsl2591_gain_to_multiplier(chip->als_settings.als_gain);
 765                ret = IIO_VAL_INT;
 766                break;
 767        default:
 768                ret = -EINVAL;
 769                break;
 770        }
 771
 772err_unlock:
 773        mutex_unlock(&chip->als_mutex);
 774
 775        pm_runtime_mark_last_busy(&client->dev);
 776        pm_runtime_put_autosuspend(&client->dev);
 777
 778        return ret;
 779}
 780
 781static int tsl2591_write_raw(struct iio_dev *indio_dev,
 782                             struct iio_chan_spec const *chan,
 783                             int val, int val2, long mask)
 784{
 785        struct tsl2591_chip *chip = iio_priv(indio_dev);
 786        int int_time;
 787        int gain;
 788        int ret;
 789
 790        mutex_lock(&chip->als_mutex);
 791
 792        switch (mask) {
 793        case IIO_CHAN_INFO_INT_TIME:
 794                int_time = tsl2591_als_time_to_fval(val);
 795                if (int_time < 0) {
 796                        ret = int_time;
 797                        goto err_unlock;
 798                }
 799                ret = tsl2591_compatible_int_time(chip, int_time);
 800                if (ret < 0)
 801                        goto err_unlock;
 802
 803                chip->als_settings.als_int_time = int_time;
 804                break;
 805        case IIO_CHAN_INFO_CALIBSCALE:
 806                gain = tsl2591_multiplier_to_gain(val);
 807                if (gain < 0) {
 808                        ret = gain;
 809                        goto err_unlock;
 810                }
 811                ret = tsl2591_compatible_gain(chip, gain);
 812                if (ret < 0)
 813                        goto err_unlock;
 814
 815                chip->als_settings.als_gain = gain;
 816                break;
 817        default:
 818                ret = -EINVAL;
 819                goto err_unlock;
 820        }
 821
 822        ret = tsl2591_set_als_gain_int_time(chip);
 823
 824err_unlock:
 825        mutex_unlock(&chip->als_mutex);
 826        return ret;
 827}
 828
 829static int tsl2591_read_available(struct iio_dev *indio_dev,
 830                                  struct iio_chan_spec const *chan,
 831                                  const int **vals, int *type, int *length,
 832                                  long mask)
 833{
 834        switch (mask) {
 835        case IIO_CHAN_INFO_INT_TIME:
 836                *length = ARRAY_SIZE(tsl2591_int_time_available);
 837                *vals = tsl2591_int_time_available;
 838                *type = IIO_VAL_INT;
 839                return IIO_AVAIL_LIST;
 840
 841        case IIO_CHAN_INFO_CALIBSCALE:
 842                *length = ARRAY_SIZE(tsl2591_calibscale_available);
 843                *vals = tsl2591_calibscale_available;
 844                *type = IIO_VAL_INT;
 845                return IIO_AVAIL_LIST;
 846        default:
 847                return -EINVAL;
 848        }
 849}
 850
 851static int tsl2591_read_event_value(struct iio_dev *indio_dev,
 852                                    const struct iio_chan_spec *chan,
 853                                    enum iio_event_type type,
 854                                    enum iio_event_direction dir,
 855                                    enum iio_event_info info, int *val,
 856                                    int *val2)
 857{
 858        struct tsl2591_chip *chip = iio_priv(indio_dev);
 859        struct i2c_client *client = chip->client;
 860        int als_persist, int_time, period;
 861        int ret;
 862
 863        mutex_lock(&chip->als_mutex);
 864
 865        switch (info) {
 866        case IIO_EV_INFO_VALUE:
 867                switch (dir) {
 868                case IIO_EV_DIR_RISING:
 869                        *val = chip->als_settings.als_upper_thresh;
 870                        break;
 871                case IIO_EV_DIR_FALLING:
 872                        *val = chip->als_settings.als_lower_thresh;
 873                        break;
 874                default:
 875                        ret = -EINVAL;
 876                        goto err_unlock;
 877                }
 878                ret = IIO_VAL_INT;
 879                break;
 880        case IIO_EV_INFO_PERIOD:
 881                ret = i2c_smbus_read_byte_data(client,
 882                                               TSL2591_CMD_NOP | TSL2591_PERSIST);
 883                if (ret <= 0 || ret > TSL2591_PRST_ALS_INT_CYCLE_MAX)
 884                        goto err_unlock;
 885
 886                als_persist = tsl2591_persist_cycle_to_lit(ret);
 887                int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
 888                period = als_persist * (int_time * MSEC_PER_SEC);
 889
 890                *val = period / USEC_PER_SEC;
 891                *val2 = period % USEC_PER_SEC;
 892
 893                ret = IIO_VAL_INT_PLUS_MICRO;
 894                break;
 895        default:
 896                ret = -EINVAL;
 897                break;
 898        }
 899
 900err_unlock:
 901        mutex_unlock(&chip->als_mutex);
 902        return ret;
 903}
 904
 905static int tsl2591_write_event_value(struct iio_dev *indio_dev,
 906                                     const struct iio_chan_spec *chan,
 907                                     enum iio_event_type type,
 908                                     enum iio_event_direction dir,
 909                                     enum iio_event_info info, int val,
 910                                     int val2)
 911{
 912        struct tsl2591_chip *chip = iio_priv(indio_dev);
 913        int period, int_time, als_persist;
 914        int ret;
 915
 916        if (val < 0 || val2 < 0)
 917                return -EINVAL;
 918
 919        mutex_lock(&chip->als_mutex);
 920
 921        switch (info) {
 922        case IIO_EV_INFO_VALUE:
 923                if (val > TSL2591_ALS_MAX_VALUE) {
 924                        ret = -EINVAL;
 925                        goto err_unlock;
 926                }
 927
 928                switch (dir) {
 929                case IIO_EV_DIR_RISING:
 930                        ret = tsl2591_set_als_upper_threshold(chip, val);
 931                        if (ret < 0)
 932                                goto err_unlock;
 933                        break;
 934                case IIO_EV_DIR_FALLING:
 935                        ret = tsl2591_set_als_lower_threshold(chip, val);
 936                        if (ret < 0)
 937                                goto err_unlock;
 938                        break;
 939                default:
 940                        ret = -EINVAL;
 941                        goto err_unlock;
 942                }
 943                break;
 944        case IIO_EV_INFO_PERIOD:
 945                int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time);
 946
 947                period = ((val * MSEC_PER_SEC) +
 948                         (val2 / MSEC_PER_SEC)) / int_time;
 949
 950                als_persist = tsl2591_persist_lit_to_cycle(period);
 951                if (als_persist < 0) {
 952                        ret = -EINVAL;
 953                        goto err_unlock;
 954                }
 955
 956                ret = tsl2591_compatible_als_persist_cycle(chip, als_persist);
 957                if (ret < 0)
 958                        goto err_unlock;
 959
 960                ret = tsl2591_set_als_persist_cycle(chip, als_persist);
 961                if (ret < 0)
 962                        goto err_unlock;
 963                break;
 964        default:
 965                ret = -EINVAL;
 966                break;
 967        }
 968
 969err_unlock:
 970        mutex_unlock(&chip->als_mutex);
 971        return ret;
 972}
 973
 974static int tsl2591_read_event_config(struct iio_dev *indio_dev,
 975                                     const struct iio_chan_spec *chan,
 976                                     enum iio_event_type type,
 977                                     enum iio_event_direction dir)
 978{
 979        struct tsl2591_chip *chip = iio_priv(indio_dev);
 980
 981        return chip->events_enabled;
 982}
 983
 984static int tsl2591_write_event_config(struct iio_dev *indio_dev,
 985                                      const struct iio_chan_spec *chan,
 986                                      enum iio_event_type type,
 987                                      enum iio_event_direction dir,
 988                                      int state)
 989{
 990        struct tsl2591_chip *chip = iio_priv(indio_dev);
 991        struct i2c_client *client = chip->client;
 992
 993        if (state && !chip->events_enabled) {
 994                chip->events_enabled = true;
 995                pm_runtime_get_sync(&client->dev);
 996        } else if (!state && chip->events_enabled) {
 997                chip->events_enabled = false;
 998                pm_runtime_mark_last_busy(&client->dev);
 999                pm_runtime_put_autosuspend(&client->dev);
1000        }
1001
1002        return 0;
1003}
1004
1005static const struct iio_info tsl2591_info = {
1006        .event_attrs = &tsl2591_event_attribute_group,
1007        .read_raw = tsl2591_read_raw,
1008        .write_raw = tsl2591_write_raw,
1009        .read_avail = tsl2591_read_available,
1010        .read_event_value = tsl2591_read_event_value,
1011        .write_event_value = tsl2591_write_event_value,
1012        .read_event_config = tsl2591_read_event_config,
1013        .write_event_config = tsl2591_write_event_config,
1014};
1015
1016static const struct iio_info tsl2591_info_no_irq = {
1017        .read_raw = tsl2591_read_raw,
1018        .write_raw = tsl2591_write_raw,
1019        .read_avail = tsl2591_read_available,
1020};
1021
1022static int __maybe_unused tsl2591_suspend(struct device *dev)
1023{
1024        struct iio_dev *indio_dev = dev_get_drvdata(dev);
1025        struct tsl2591_chip *chip = iio_priv(indio_dev);
1026        int ret;
1027
1028        mutex_lock(&chip->als_mutex);
1029        ret = tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
1030        mutex_unlock(&chip->als_mutex);
1031
1032        return ret;
1033}
1034
1035static int __maybe_unused tsl2591_resume(struct device *dev)
1036{
1037        int power_state = TSL2591_PWR_ON | TSL2591_ENABLE_ALS;
1038        struct iio_dev *indio_dev = dev_get_drvdata(dev);
1039        struct tsl2591_chip *chip = iio_priv(indio_dev);
1040        int ret;
1041
1042        if (chip->events_enabled)
1043                power_state |= TSL2591_ENABLE_ALS_INT;
1044
1045        mutex_lock(&chip->als_mutex);
1046        ret = tsl2591_set_power_state(chip, power_state);
1047        mutex_unlock(&chip->als_mutex);
1048
1049        return ret;
1050}
1051
1052static const struct dev_pm_ops tsl2591_pm_ops = {
1053        SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
1054        SET_RUNTIME_PM_OPS(tsl2591_suspend, tsl2591_resume, NULL)
1055};
1056
1057static irqreturn_t tsl2591_event_handler(int irq, void *private)
1058{
1059        struct iio_dev *dev_info = private;
1060        struct tsl2591_chip *chip = iio_priv(dev_info);
1061        struct i2c_client *client = chip->client;
1062
1063        if (!chip->events_enabled)
1064                return IRQ_NONE;
1065
1066        iio_push_event(dev_info,
1067                       IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0,
1068                                            IIO_EV_TYPE_THRESH,
1069                                            IIO_EV_DIR_EITHER),
1070                                            iio_get_time_ns(dev_info));
1071
1072        /* Clear ALS irq */
1073        i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
1074
1075        return IRQ_HANDLED;
1076}
1077
1078static int tsl2591_load_defaults(struct tsl2591_chip *chip)
1079{
1080        int ret;
1081
1082        chip->als_settings.als_int_time = TSL2591_DEFAULT_ALS_INT_TIME;
1083        chip->als_settings.als_gain = TSL2591_DEFAULT_ALS_GAIN;
1084        chip->als_settings.als_lower_thresh = TSL2591_DEFAULT_ALS_LOWER_THRESH;
1085        chip->als_settings.als_upper_thresh = TSL2591_DEFAULT_ALS_UPPER_THRESH;
1086
1087        ret = tsl2591_set_als_gain_int_time(chip);
1088        if (ret < 0)
1089                return ret;
1090
1091        ret = tsl2591_set_als_persist_cycle(chip, TSL2591_DEFAULT_ALS_PERSIST);
1092        if (ret < 0)
1093                return ret;
1094
1095        ret = tsl2591_set_als_lower_threshold(chip, TSL2591_DEFAULT_ALS_LOWER_THRESH);
1096        if (ret < 0)
1097                return ret;
1098
1099        ret = tsl2591_set_als_upper_threshold(chip, TSL2591_DEFAULT_ALS_UPPER_THRESH);
1100        if (ret < 0)
1101                return ret;
1102
1103        return 0;
1104}
1105
1106static void tsl2591_chip_off(void *data)
1107{
1108        struct iio_dev *indio_dev = data;
1109        struct tsl2591_chip *chip = iio_priv(indio_dev);
1110        struct i2c_client *client = chip->client;
1111
1112        pm_runtime_disable(&client->dev);
1113        pm_runtime_set_suspended(&client->dev);
1114        pm_runtime_put_noidle(&client->dev);
1115
1116        tsl2591_set_power_state(chip, TSL2591_PWR_OFF);
1117}
1118
1119static int tsl2591_probe(struct i2c_client *client)
1120{
1121        struct tsl2591_chip *chip;
1122        struct iio_dev *indio_dev;
1123        int ret;
1124
1125        if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1126                dev_err(&client->dev,
1127                        "I2C smbus byte data functionality is not supported\n");
1128                return -EOPNOTSUPP;
1129        }
1130
1131        indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
1132        if (!indio_dev)
1133                return -ENOMEM;
1134
1135        chip = iio_priv(indio_dev);
1136        chip->client = client;
1137        i2c_set_clientdata(client, indio_dev);
1138
1139        if (client->irq) {
1140                ret = devm_request_threaded_irq(&client->dev, client->irq,
1141                                                NULL, tsl2591_event_handler,
1142                                                IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1143                                                "tsl2591_irq", indio_dev);
1144                if (ret) {
1145                        dev_err_probe(&client->dev, ret, "IRQ request error\n");
1146                        return -EINVAL;
1147                }
1148                indio_dev->info = &tsl2591_info;
1149        } else {
1150                indio_dev->info = &tsl2591_info_no_irq;
1151        }
1152
1153        mutex_init(&chip->als_mutex);
1154
1155        ret = i2c_smbus_read_byte_data(client,
1156                                       TSL2591_CMD_NOP | TSL2591_DEVICE_ID);
1157        if (ret < 0) {
1158                dev_err(&client->dev,
1159                        "Failed to read the device ID register\n");
1160                return ret;
1161        }
1162        ret = FIELD_GET(TSL2591_DEVICE_ID_MASK, ret);
1163        if (ret != TSL2591_DEVICE_ID_VAL) {
1164                dev_err(&client->dev, "Device ID: %#04x unknown\n", ret);
1165                return -EINVAL;
1166        }
1167
1168        indio_dev->channels = tsl2591_channels;
1169        indio_dev->num_channels = ARRAY_SIZE(tsl2591_channels);
1170        indio_dev->modes = INDIO_DIRECT_MODE;
1171        indio_dev->name = chip->client->name;
1172        chip->events_enabled = false;
1173
1174        pm_runtime_enable(&client->dev);
1175        pm_runtime_set_autosuspend_delay(&client->dev,
1176                                         TSL2591_POWER_OFF_DELAY_MS);
1177        pm_runtime_use_autosuspend(&client->dev);
1178
1179        /*
1180         * Add chip off to automatically managed path and disable runtime
1181         * power management. This ensures that the chip power management
1182         * is handled correctly on driver remove. tsl2591_chip_off() must be
1183         * added to the managed path after pm runtime is enabled and before
1184         * any error exit paths are met to ensure we're not left in a state
1185         * of pm runtime not being disabled properly.
1186         */
1187        ret = devm_add_action_or_reset(&client->dev, tsl2591_chip_off,
1188                                       indio_dev);
1189        if (ret < 0)
1190                return -EINVAL;
1191
1192        ret = tsl2591_load_defaults(chip);
1193        if (ret < 0) {
1194                dev_err(&client->dev, "Failed to load sensor defaults\n");
1195                return -EINVAL;
1196        }
1197
1198        ret = i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI);
1199        if (ret < 0) {
1200                dev_err(&client->dev, "Failed to clear als irq\n");
1201                return -EINVAL;
1202        }
1203
1204        return devm_iio_device_register(&client->dev, indio_dev);
1205}
1206
1207static const struct of_device_id tsl2591_of_match[] = {
1208        { .compatible = "amstaos,tsl2591"},
1209        {}
1210};
1211MODULE_DEVICE_TABLE(of, tsl2591_of_match);
1212
1213static struct i2c_driver tsl2591_driver = {
1214        .driver = {
1215                .name = "tsl2591",
1216                .pm = &tsl2591_pm_ops,
1217                .of_match_table = tsl2591_of_match,
1218        },
1219        .probe_new = tsl2591_probe
1220};
1221module_i2c_driver(tsl2591_driver);
1222
1223MODULE_AUTHOR("Joe Sandom <joe.g.sandom@gmail.com>");
1224MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver");
1225MODULE_LICENSE("GPL");
1226