| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (C) 2021 Joe Sandom <[email protected]> |
| 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 <linux/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 |
|
| 154 |
struct 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 |
|
| 162 |
struct 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 |
*/ |
| 178 |
static 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 |
|
| 187 |
static const int tsl2591_int_time_available[] = { |
| 188 |
1, 2, 3, 4, 5, 6, |
| 189 |
}; |
| 190 |
|
| 191 |
static const int tsl2591_calibscale_available[] = { |
| 192 |
1, 25, 428, 9876, |
| 193 |
}; |
| 194 |
|
| 195 |
static int tsl2591_set_als_lower_threshold(struct tsl2591_chip *chip, |
| 196 |
u16 als_lower_threshold); |
| 197 |
static int tsl2591_set_als_upper_threshold(struct tsl2591_chip *chip, |
| 198 |
u16 als_upper_threshold); |
| 199 |
|
| 200 |
static 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 |
|
| 216 |
static 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 |
|
| 232 |
static 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 |
|
| 270 |
static 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 |
|
| 308 |
static 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 |
|
| 324 |
static 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 |
|
| 336 |
static 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 |
|
| 349 |
static 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 |
|
| 374 |
static 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 |
|
| 387 |
static 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 |
*/ |
| 431 |
static 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 |
|
| 502 |
static 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 |
|
| 517 |
static 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 |
|
| 561 |
static 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 |
|
| 608 |
static 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 |
|
| 625 |
static 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 |
|
| 640 |
static 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 |
|
| 651 |
static IIO_DEVICE_ATTR_RO(tsl2591_in_illuminance_period_available, 0); |
| 652 |
|
| 653 |
static struct attribute *tsl2591_event_attrs_ctrl[] = { |
| 654 |
&iio_dev_attr_tsl2591_in_illuminance_period_available.dev_attr.attr, |
| 655 |
NULL |
| 656 |
}; |
| 657 |
|
| 658 |
static const struct attribute_group tsl2591_event_attribute_group = { |
| 659 |
.attrs = tsl2591_event_attrs_ctrl, |
| 660 |
}; |
| 661 |
|
| 662 |
static 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 |
|
| 679 |
static 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 |
|
| 712 |
static 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 |
|
| 772 |
err_unlock: |
| 773 |
mutex_unlock(&chip->als_mutex); |
| 774 |
|
| 775 |
pm_runtime_put_autosuspend(&client->dev); |
| 776 |
|
| 777 |
return ret; |
| 778 |
} |
| 779 |
|
| 780 |
static int tsl2591_write_raw(struct iio_dev *indio_dev, |
| 781 |
struct iio_chan_spec const *chan, |
| 782 |
int val, int val2, long mask) |
| 783 |
{ |
| 784 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 785 |
int int_time; |
| 786 |
int gain; |
| 787 |
int ret; |
| 788 |
|
| 789 |
mutex_lock(&chip->als_mutex); |
| 790 |
|
| 791 |
switch (mask) { |
| 792 |
case IIO_CHAN_INFO_INT_TIME: |
| 793 |
int_time = tsl2591_als_time_to_fval(val); |
| 794 |
if (int_time < 0) { |
| 795 |
ret = int_time; |
| 796 |
goto err_unlock; |
| 797 |
} |
| 798 |
ret = tsl2591_compatible_int_time(chip, int_time); |
| 799 |
if (ret < 0) |
| 800 |
goto err_unlock; |
| 801 |
|
| 802 |
chip->als_settings.als_int_time = int_time; |
| 803 |
break; |
| 804 |
case IIO_CHAN_INFO_CALIBSCALE: |
| 805 |
gain = tsl2591_multiplier_to_gain(val); |
| 806 |
if (gain < 0) { |
| 807 |
ret = gain; |
| 808 |
goto err_unlock; |
| 809 |
} |
| 810 |
ret = tsl2591_compatible_gain(chip, gain); |
| 811 |
if (ret < 0) |
| 812 |
goto err_unlock; |
| 813 |
|
| 814 |
chip->als_settings.als_gain = gain; |
| 815 |
break; |
| 816 |
default: |
| 817 |
ret = -EINVAL; |
| 818 |
goto err_unlock; |
| 819 |
} |
| 820 |
|
| 821 |
ret = tsl2591_set_als_gain_int_time(chip); |
| 822 |
|
| 823 |
err_unlock: |
| 824 |
mutex_unlock(&chip->als_mutex); |
| 825 |
return ret; |
| 826 |
} |
| 827 |
|
| 828 |
static int tsl2591_read_available(struct iio_dev *indio_dev, |
| 829 |
struct iio_chan_spec const *chan, |
| 830 |
const int **vals, int *type, int *length, |
| 831 |
long mask) |
| 832 |
{ |
| 833 |
switch (mask) { |
| 834 |
case IIO_CHAN_INFO_INT_TIME: |
| 835 |
*length = ARRAY_SIZE(tsl2591_int_time_available); |
| 836 |
*vals = tsl2591_int_time_available; |
| 837 |
*type = IIO_VAL_INT; |
| 838 |
return IIO_AVAIL_LIST; |
| 839 |
|
| 840 |
case IIO_CHAN_INFO_CALIBSCALE: |
| 841 |
*length = ARRAY_SIZE(tsl2591_calibscale_available); |
| 842 |
*vals = tsl2591_calibscale_available; |
| 843 |
*type = IIO_VAL_INT; |
| 844 |
return IIO_AVAIL_LIST; |
| 845 |
default: |
| 846 |
return -EINVAL; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
static int tsl2591_read_event_value(struct iio_dev *indio_dev, |
| 851 |
const struct iio_chan_spec *chan, |
| 852 |
enum iio_event_type type, |
| 853 |
enum iio_event_direction dir, |
| 854 |
enum iio_event_info info, int *val, |
| 855 |
int *val2) |
| 856 |
{ |
| 857 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 858 |
struct i2c_client *client = chip->client; |
| 859 |
int als_persist, int_time, period; |
| 860 |
int ret; |
| 861 |
|
| 862 |
mutex_lock(&chip->als_mutex); |
| 863 |
|
| 864 |
switch (info) { |
| 865 |
case IIO_EV_INFO_VALUE: |
| 866 |
switch (dir) { |
| 867 |
case IIO_EV_DIR_RISING: |
| 868 |
*val = chip->als_settings.als_upper_thresh; |
| 869 |
break; |
| 870 |
case IIO_EV_DIR_FALLING: |
| 871 |
*val = chip->als_settings.als_lower_thresh; |
| 872 |
break; |
| 873 |
default: |
| 874 |
ret = -EINVAL; |
| 875 |
goto err_unlock; |
| 876 |
} |
| 877 |
ret = IIO_VAL_INT; |
| 878 |
break; |
| 879 |
case IIO_EV_INFO_PERIOD: |
| 880 |
ret = i2c_smbus_read_byte_data(client, |
| 881 |
TSL2591_CMD_NOP | TSL2591_PERSIST); |
| 882 |
if (ret <= 0 || ret > TSL2591_PRST_ALS_INT_CYCLE_MAX) |
| 883 |
goto err_unlock; |
| 884 |
|
| 885 |
als_persist = tsl2591_persist_cycle_to_lit(ret); |
| 886 |
int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time); |
| 887 |
period = als_persist * (int_time * MSEC_PER_SEC); |
| 888 |
|
| 889 |
*val = period / USEC_PER_SEC; |
| 890 |
*val2 = period % USEC_PER_SEC; |
| 891 |
|
| 892 |
ret = IIO_VAL_INT_PLUS_MICRO; |
| 893 |
break; |
| 894 |
default: |
| 895 |
ret = -EINVAL; |
| 896 |
break; |
| 897 |
} |
| 898 |
|
| 899 |
err_unlock: |
| 900 |
mutex_unlock(&chip->als_mutex); |
| 901 |
return ret; |
| 902 |
} |
| 903 |
|
| 904 |
static int tsl2591_write_event_value(struct iio_dev *indio_dev, |
| 905 |
const struct iio_chan_spec *chan, |
| 906 |
enum iio_event_type type, |
| 907 |
enum iio_event_direction dir, |
| 908 |
enum iio_event_info info, int val, |
| 909 |
int val2) |
| 910 |
{ |
| 911 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 912 |
int period, int_time, als_persist; |
| 913 |
int ret; |
| 914 |
|
| 915 |
if (val < 0 || val2 < 0) |
| 916 |
return -EINVAL; |
| 917 |
|
| 918 |
mutex_lock(&chip->als_mutex); |
| 919 |
|
| 920 |
switch (info) { |
| 921 |
case IIO_EV_INFO_VALUE: |
| 922 |
if (val > TSL2591_ALS_MAX_VALUE) { |
| 923 |
ret = -EINVAL; |
| 924 |
goto err_unlock; |
| 925 |
} |
| 926 |
|
| 927 |
switch (dir) { |
| 928 |
case IIO_EV_DIR_RISING: |
| 929 |
ret = tsl2591_set_als_upper_threshold(chip, val); |
| 930 |
if (ret < 0) |
| 931 |
goto err_unlock; |
| 932 |
break; |
| 933 |
case IIO_EV_DIR_FALLING: |
| 934 |
ret = tsl2591_set_als_lower_threshold(chip, val); |
| 935 |
if (ret < 0) |
| 936 |
goto err_unlock; |
| 937 |
break; |
| 938 |
default: |
| 939 |
ret = -EINVAL; |
| 940 |
goto err_unlock; |
| 941 |
} |
| 942 |
break; |
| 943 |
case IIO_EV_INFO_PERIOD: |
| 944 |
int_time = TSL2591_FVAL_TO_MSEC(chip->als_settings.als_int_time); |
| 945 |
|
| 946 |
period = ((val * MSEC_PER_SEC) + |
| 947 |
(val2 / MSEC_PER_SEC)) / int_time; |
| 948 |
|
| 949 |
als_persist = tsl2591_persist_lit_to_cycle(period); |
| 950 |
if (als_persist < 0) { |
| 951 |
ret = -EINVAL; |
| 952 |
goto err_unlock; |
| 953 |
} |
| 954 |
|
| 955 |
ret = tsl2591_compatible_als_persist_cycle(chip, als_persist); |
| 956 |
if (ret < 0) |
| 957 |
goto err_unlock; |
| 958 |
|
| 959 |
ret = tsl2591_set_als_persist_cycle(chip, als_persist); |
| 960 |
if (ret < 0) |
| 961 |
goto err_unlock; |
| 962 |
break; |
| 963 |
default: |
| 964 |
ret = -EINVAL; |
| 965 |
break; |
| 966 |
} |
| 967 |
|
| 968 |
err_unlock: |
| 969 |
mutex_unlock(&chip->als_mutex); |
| 970 |
return ret; |
| 971 |
} |
| 972 |
|
| 973 |
static int tsl2591_read_event_config(struct iio_dev *indio_dev, |
| 974 |
const struct iio_chan_spec *chan, |
| 975 |
enum iio_event_type type, |
| 976 |
enum iio_event_direction dir) |
| 977 |
{ |
| 978 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 979 |
|
| 980 |
return chip->events_enabled; |
| 981 |
} |
| 982 |
|
| 983 |
static int tsl2591_write_event_config(struct iio_dev *indio_dev, |
| 984 |
const struct iio_chan_spec *chan, |
| 985 |
enum iio_event_type type, |
| 986 |
enum iio_event_direction dir, |
| 987 |
bool state) |
| 988 |
{ |
| 989 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 990 |
struct i2c_client *client = chip->client; |
| 991 |
|
| 992 |
if (state && !chip->events_enabled) { |
| 993 |
chip->events_enabled = true; |
| 994 |
pm_runtime_get_sync(&client->dev); |
| 995 |
} else if (!state && chip->events_enabled) { |
| 996 |
chip->events_enabled = false; |
| 997 |
pm_runtime_put_autosuspend(&client->dev); |
| 998 |
} |
| 999 |
|
| 1000 |
return 0; |
| 1001 |
} |
| 1002 |
|
| 1003 |
static const struct iio_info tsl2591_info = { |
| 1004 |
.event_attrs = &tsl2591_event_attribute_group, |
| 1005 |
.read_raw = tsl2591_read_raw, |
| 1006 |
.write_raw = tsl2591_write_raw, |
| 1007 |
.read_avail = tsl2591_read_available, |
| 1008 |
.read_event_value = tsl2591_read_event_value, |
| 1009 |
.write_event_value = tsl2591_write_event_value, |
| 1010 |
.read_event_config = tsl2591_read_event_config, |
| 1011 |
.write_event_config = tsl2591_write_event_config, |
| 1012 |
}; |
| 1013 |
|
| 1014 |
static const struct iio_info tsl2591_info_no_irq = { |
| 1015 |
.read_raw = tsl2591_read_raw, |
| 1016 |
.write_raw = tsl2591_write_raw, |
| 1017 |
.read_avail = tsl2591_read_available, |
| 1018 |
}; |
| 1019 |
|
| 1020 |
static int tsl2591_suspend(struct device *dev) |
| 1021 |
{ |
| 1022 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 1023 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 1024 |
int ret; |
| 1025 |
|
| 1026 |
mutex_lock(&chip->als_mutex); |
| 1027 |
ret = tsl2591_set_power_state(chip, TSL2591_PWR_OFF); |
| 1028 |
mutex_unlock(&chip->als_mutex); |
| 1029 |
|
| 1030 |
return ret; |
| 1031 |
} |
| 1032 |
|
| 1033 |
static int tsl2591_resume(struct device *dev) |
| 1034 |
{ |
| 1035 |
int power_state = TSL2591_PWR_ON | TSL2591_ENABLE_ALS; |
| 1036 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 1037 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 1038 |
int ret; |
| 1039 |
|
| 1040 |
if (chip->events_enabled) |
| 1041 |
power_state |= TSL2591_ENABLE_ALS_INT; |
| 1042 |
|
| 1043 |
mutex_lock(&chip->als_mutex); |
| 1044 |
ret = tsl2591_set_power_state(chip, power_state); |
| 1045 |
mutex_unlock(&chip->als_mutex); |
| 1046 |
|
| 1047 |
return ret; |
| 1048 |
} |
| 1049 |
|
| 1050 |
static DEFINE_RUNTIME_DEV_PM_OPS(tsl2591_pm_ops, tsl2591_suspend, |
| 1051 |
tsl2591_resume, NULL); |
| 1052 |
|
| 1053 |
static irqreturn_t tsl2591_event_handler(int irq, void *private) |
| 1054 |
{ |
| 1055 |
struct iio_dev *dev_info = private; |
| 1056 |
struct tsl2591_chip *chip = iio_priv(dev_info); |
| 1057 |
struct i2c_client *client = chip->client; |
| 1058 |
|
| 1059 |
if (!chip->events_enabled) |
| 1060 |
return IRQ_NONE; |
| 1061 |
|
| 1062 |
iio_push_event(dev_info, |
| 1063 |
IIO_UNMOD_EVENT_CODE(IIO_LIGHT, 0, |
| 1064 |
IIO_EV_TYPE_THRESH, |
| 1065 |
IIO_EV_DIR_EITHER), |
| 1066 |
iio_get_time_ns(dev_info)); |
| 1067 |
|
| 1068 |
/* Clear ALS irq */ |
| 1069 |
i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI); |
| 1070 |
|
| 1071 |
return IRQ_HANDLED; |
| 1072 |
} |
| 1073 |
|
| 1074 |
static int tsl2591_load_defaults(struct tsl2591_chip *chip) |
| 1075 |
{ |
| 1076 |
int ret; |
| 1077 |
|
| 1078 |
chip->als_settings.als_int_time = TSL2591_DEFAULT_ALS_INT_TIME; |
| 1079 |
chip->als_settings.als_gain = TSL2591_DEFAULT_ALS_GAIN; |
| 1080 |
chip->als_settings.als_lower_thresh = TSL2591_DEFAULT_ALS_LOWER_THRESH; |
| 1081 |
chip->als_settings.als_upper_thresh = TSL2591_DEFAULT_ALS_UPPER_THRESH; |
| 1082 |
|
| 1083 |
ret = tsl2591_set_als_gain_int_time(chip); |
| 1084 |
if (ret < 0) |
| 1085 |
return ret; |
| 1086 |
|
| 1087 |
ret = tsl2591_set_als_persist_cycle(chip, TSL2591_DEFAULT_ALS_PERSIST); |
| 1088 |
if (ret < 0) |
| 1089 |
return ret; |
| 1090 |
|
| 1091 |
ret = tsl2591_set_als_lower_threshold(chip, TSL2591_DEFAULT_ALS_LOWER_THRESH); |
| 1092 |
if (ret < 0) |
| 1093 |
return ret; |
| 1094 |
|
| 1095 |
ret = tsl2591_set_als_upper_threshold(chip, TSL2591_DEFAULT_ALS_UPPER_THRESH); |
| 1096 |
if (ret < 0) |
| 1097 |
return ret; |
| 1098 |
|
| 1099 |
return 0; |
| 1100 |
} |
| 1101 |
|
| 1102 |
static void tsl2591_chip_off(void *data) |
| 1103 |
{ |
| 1104 |
struct iio_dev *indio_dev = data; |
| 1105 |
struct tsl2591_chip *chip = iio_priv(indio_dev); |
| 1106 |
struct i2c_client *client = chip->client; |
| 1107 |
|
| 1108 |
pm_runtime_disable(&client->dev); |
| 1109 |
pm_runtime_set_suspended(&client->dev); |
| 1110 |
pm_runtime_put_noidle(&client->dev); |
| 1111 |
|
| 1112 |
tsl2591_set_power_state(chip, TSL2591_PWR_OFF); |
| 1113 |
} |
| 1114 |
|
| 1115 |
static int tsl2591_probe(struct i2c_client *client) |
| 1116 |
{ |
| 1117 |
struct tsl2591_chip *chip; |
| 1118 |
struct iio_dev *indio_dev; |
| 1119 |
int ret; |
| 1120 |
|
| 1121 |
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 1122 |
dev_err(&client->dev, |
| 1123 |
"I2C smbus byte data functionality is not supported\n"); |
| 1124 |
return -EOPNOTSUPP; |
| 1125 |
} |
| 1126 |
|
| 1127 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
| 1128 |
if (!indio_dev) |
| 1129 |
return -ENOMEM; |
| 1130 |
|
| 1131 |
chip = iio_priv(indio_dev); |
| 1132 |
chip->client = client; |
| 1133 |
i2c_set_clientdata(client, indio_dev); |
| 1134 |
|
| 1135 |
if (client->irq) { |
| 1136 |
ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 1137 |
NULL, tsl2591_event_handler, |
| 1138 |
IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 1139 |
"tsl2591_irq", indio_dev); |
| 1140 |
if (ret) { |
| 1141 |
dev_err_probe(&client->dev, ret, "IRQ request error\n"); |
| 1142 |
return -EINVAL; |
| 1143 |
} |
| 1144 |
indio_dev->info = &tsl2591_info; |
| 1145 |
} else { |
| 1146 |
indio_dev->info = &tsl2591_info_no_irq; |
| 1147 |
} |
| 1148 |
|
| 1149 |
mutex_init(&chip->als_mutex); |
| 1150 |
|
| 1151 |
ret = i2c_smbus_read_byte_data(client, |
| 1152 |
TSL2591_CMD_NOP | TSL2591_DEVICE_ID); |
| 1153 |
if (ret < 0) { |
| 1154 |
dev_err(&client->dev, |
| 1155 |
"Failed to read the device ID register\n"); |
| 1156 |
return ret; |
| 1157 |
} |
| 1158 |
ret = FIELD_GET(TSL2591_DEVICE_ID_MASK, ret); |
| 1159 |
if (ret != TSL2591_DEVICE_ID_VAL) { |
| 1160 |
dev_err(&client->dev, "Device ID: %#04x unknown\n", ret); |
| 1161 |
return -EINVAL; |
| 1162 |
} |
| 1163 |
|
| 1164 |
indio_dev->channels = tsl2591_channels; |
| 1165 |
indio_dev->num_channels = ARRAY_SIZE(tsl2591_channels); |
| 1166 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 1167 |
indio_dev->name = chip->client->name; |
| 1168 |
chip->events_enabled = false; |
| 1169 |
|
| 1170 |
pm_runtime_enable(&client->dev); |
| 1171 |
pm_runtime_set_autosuspend_delay(&client->dev, |
| 1172 |
TSL2591_POWER_OFF_DELAY_MS); |
| 1173 |
pm_runtime_use_autosuspend(&client->dev); |
| 1174 |
|
| 1175 |
/* |
| 1176 |
* Add chip off to automatically managed path and disable runtime |
| 1177 |
* power management. This ensures that the chip power management |
| 1178 |
* is handled correctly on driver remove. tsl2591_chip_off() must be |
| 1179 |
* added to the managed path after pm runtime is enabled and before |
| 1180 |
* any error exit paths are met to ensure we're not left in a state |
| 1181 |
* of pm runtime not being disabled properly. |
| 1182 |
*/ |
| 1183 |
ret = devm_add_action_or_reset(&client->dev, tsl2591_chip_off, |
| 1184 |
indio_dev); |
| 1185 |
if (ret < 0) |
| 1186 |
return -EINVAL; |
| 1187 |
|
| 1188 |
ret = tsl2591_load_defaults(chip); |
| 1189 |
if (ret < 0) { |
| 1190 |
dev_err(&client->dev, "Failed to load sensor defaults\n"); |
| 1191 |
return -EINVAL; |
| 1192 |
} |
| 1193 |
|
| 1194 |
ret = i2c_smbus_write_byte(client, TSL2591_CMD_SF_CALS_NPI); |
| 1195 |
if (ret < 0) { |
| 1196 |
dev_err(&client->dev, "Failed to clear als irq\n"); |
| 1197 |
return -EINVAL; |
| 1198 |
} |
| 1199 |
|
| 1200 |
return devm_iio_device_register(&client->dev, indio_dev); |
| 1201 |
} |
| 1202 |
|
| 1203 |
static const struct of_device_id tsl2591_of_match[] = { |
| 1204 |
{ .compatible = "amstaos,tsl2591"}, |
| 1205 |
{ } |
| 1206 |
}; |
| 1207 |
MODULE_DEVICE_TABLE(of, tsl2591_of_match); |
| 1208 |
|
| 1209 |
static struct i2c_driver tsl2591_driver = { |
| 1210 |
.driver = { |
| 1211 |
.name = "tsl2591", |
| 1212 |
.pm = pm_ptr(&tsl2591_pm_ops), |
| 1213 |
.of_match_table = tsl2591_of_match, |
| 1214 |
}, |
| 1215 |
.probe = tsl2591_probe |
| 1216 |
}; |
| 1217 |
module_i2c_driver(tsl2591_driver); |
| 1218 |
|
| 1219 |
MODULE_AUTHOR("Joe Sandom <[email protected]>"); |
| 1220 |
MODULE_DESCRIPTION("TAOS tsl2591 ambient light sensor driver"); |
| 1221 |
MODULE_LICENSE("GPL"); |