^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Device driver for monitoring ambient light intensity (lux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2011, TAOS Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/iio/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Device Registers and Masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define TSL2583_CNTRL 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define TSL2583_ALS_TIME 0X01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define TSL2583_INTERRUPT 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define TSL2583_GAIN 0x07
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define TSL2583_REVID 0x11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define TSL2583_CHIPID 0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define TSL2583_ALS_CHAN0LO 0x14
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define TSL2583_ALS_CHAN0HI 0x15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define TSL2583_ALS_CHAN1LO 0x16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define TSL2583_ALS_CHAN1HI 0x17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define TSL2583_TMR_LO 0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define TSL2583_TMR_HI 0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* tsl2583 cmd reg masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define TSL2583_CMD_REG 0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define TSL2583_CMD_SPL_FN 0x60
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define TSL2583_CMD_ALS_INT_CLR 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* tsl2583 cntrl reg masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define TSL2583_CNTL_ADC_ENBL 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define TSL2583_CNTL_PWR_OFF 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define TSL2583_CNTL_PWR_ON 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* tsl2583 status reg masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define TSL2583_STA_ADC_VALID 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define TSL2583_STA_ADC_INTR 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Lux calculation constants */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define TSL2583_LUX_CALC_OVER_FLOW 65535
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define TSL2583_INTERRUPT_DISABLED 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define TSL2583_CHIP_ID 0x90
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define TSL2583_CHIP_ID_MASK 0xf0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define TSL2583_POWER_OFF_DELAY_MS 2000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /* Per-device data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct tsl2583_als_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u16 als_ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u16 als_ch1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) u16 lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct tsl2583_lux {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned int ratio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned int ch1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static const struct tsl2583_lux tsl2583_default_lux[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) { 9830, 8520, 15729 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) { 12452, 10807, 23344 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) { 14746, 6383, 11705 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) { 17695, 4063, 6554 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) { 0, 0, 0 } /* Termination segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define TSL2583_MAX_LUX_TABLE_ENTRIES 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct tsl2583_settings {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int als_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int als_gain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int als_gain_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int als_cal_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * This structure is intentionally large to accommodate updates via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * Assumption is that one and only one type of glass used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct tsl2583_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct mutex als_mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct tsl2583_als_info als_cur_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct tsl2583_settings als_settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int als_time_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int als_saturation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct gainadj {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) s16 ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) s16 ch1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) s16 mean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Index = (0 - 3) Used to validate the gain selection index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static const struct gainadj gainadj[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) { 1, 1, 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) { 8, 8, 8 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) { 16, 16, 16 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) { 107, 115, 111 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Provides initial operational parameter defaults.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * These defaults may be changed through the device's sysfs files.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void tsl2583_defaults(struct tsl2583_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * The integration time must be a multiple of 50ms and within the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * range [50, 600] ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) chip->als_settings.als_time = 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * This is an index into the gainadj table. Assume clear glass as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) chip->als_settings.als_gain = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Default gain trim to account for aperture effects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) chip->als_settings.als_gain_trim = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Known external ALS reading used for calibration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) chip->als_settings.als_cal_target = 130;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* Default lux table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) sizeof(tsl2583_default_lux));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Reads and calculates current lux value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * The raw ch0 and ch1 values of the ambient light sensed in the last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * integration cycle are read from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Time scale factor array values are adjusted based on the integration time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * The raw values are multiplied by a scale factor, and device gain is obtained
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * using gain index. Limit checks are done next, then the ratio of a multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * declared above is then scanned to find the first ratio value that is just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * the array are then used along with the time scale factor array values, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * calculate the lux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int tsl2583_get_lux(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) u16 ch0, ch1; /* separated ch0/ch1 data from device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) u32 lux; /* raw lux calculated from device data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u64 lux64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) u32 ratio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) u8 buf[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct tsl2583_lux *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* is data new & valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!(ret & TSL2583_STA_ADC_INTR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ret = chip->als_cur_info.lux; /* return LAST VALUE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) for (i = 0; i < 4; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ret = i2c_smbus_read_byte_data(chip->client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) dev_err(&chip->client->dev, "%s: failed to read register %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) __func__, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) buf[i] = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * Clear the pending interrupt status bit on the chip to allow the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * integration cycle to start. This has to be done even though this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * driver currently does not support interrupts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ret = i2c_smbus_write_byte(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) TSL2583_CMD_ALS_INT_CLR));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) goto done; /* have no data, so return failure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* extract ALS/lux data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ch0 = le16_to_cpup((const __le16 *)&buf[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ch1 = le16_to_cpup((const __le16 *)&buf[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) chip->als_cur_info.als_ch0 = ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) chip->als_cur_info.als_ch1 = ch1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) goto return_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (!ch0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * The sensor appears to be in total darkness so set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * calculated lux to 0 and return early to avoid a division by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * zero below when calculating the ratio.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) chip->als_cur_info.lux = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /* calculate ratio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ratio = (ch1 << 15) / ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* convert to unscaled lux using the pointer to the table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) p->ratio != 0 && p->ratio < ratio; p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (p->ratio == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) lux = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) u32 ch0lux, ch1lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) ch0lux = ((ch0 * p->ch0) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) (gainadj[chip->als_settings.als_gain].ch0 >> 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) / gainadj[chip->als_settings.als_gain].ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ch1lux = ((ch1 * p->ch1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) (gainadj[chip->als_settings.als_gain].ch1 >> 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) / gainadj[chip->als_settings.als_gain].ch1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* note: lux is 31 bit max at this point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (ch1lux > ch0lux) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) chip->als_cur_info.lux = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) lux = ch0lux - ch1lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* adjust for active time scale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (chip->als_time_scale == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) lux = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) lux = (lux + (chip->als_time_scale >> 1)) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) chip->als_time_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * Adjust for active gain scale.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * The tsl2583_default_lux tables above have a factor of 8192 built in,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * so we need to shift right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * User-specified gain provides a multiplier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Apply user-specified gain before shifting right to retain precision.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * Use 64 bits to avoid overflow on multiplication.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * Then go back to 32 bits before division to avoid using div_u64().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) lux64 = lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) lux64 = lux64 * chip->als_settings.als_gain_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) lux64 >>= 13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) lux = lux64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) lux = (lux + 500) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return_max:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) lux = TSL2583_LUX_CALC_OVER_FLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) /* Update the structure with the latest VALID lux. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) chip->als_cur_info.lux = lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ret = lux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * Obtain single reading and calculate the als_gain_trim (later used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * to derive actual lux).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * Return updated gain_trim value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int tsl2583_als_calibrate(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) unsigned int gain_trim_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) int lux_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ret = i2c_smbus_read_byte_data(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) TSL2583_CMD_REG | TSL2583_CNTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) "%s: failed to read from the CNTRL register\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) != (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) "%s: Device is not powered on and/or ADC is not enabled\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) } else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) "%s: The two ADC channels have not completed an integration cycle\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) lux_val = tsl2583_get_lux(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (lux_val < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dev_err(&chip->client->dev, "%s: failed to get lux\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return lux_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /* Avoid division by zero of lux_value later on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (lux_val == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) "%s: lux_val of 0 will produce out of range trim_value\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * chip->als_settings.als_gain_trim) / lux_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) "%s: trim_val of %d is not within the range [250, 4000]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) __func__, gain_trim_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) chip->als_settings.als_gain_trim = (int)gain_trim_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static int tsl2583_set_als_time(struct tsl2583_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int als_count, als_time, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* determine als integration register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) als_count = (chip->als_settings.als_time * 100 + 135) / 270;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (!als_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) als_count = 1; /* ensure at least one cycle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* convert back to time (encompasses overrides) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) als_time = (als_count * 27 + 5) / 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) val = 256 - als_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ret = i2c_smbus_write_byte_data(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) TSL2583_CMD_REG | TSL2583_ALS_TIME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) __func__, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /* set chip struct re scaling and saturation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) chip->als_saturation = als_count * 922; /* 90% of full scale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) chip->als_time_scale = (als_time + 25) / 50;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static int tsl2583_set_als_gain(struct tsl2583_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /* Set the gain based on als_settings struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) ret = i2c_smbus_write_byte_data(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) TSL2583_CMD_REG | TSL2583_GAIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) chip->als_settings.als_gain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) "%s: failed to set the gain to %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) chip->als_settings.als_gain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) static int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = i2c_smbus_write_byte_data(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) TSL2583_CMD_REG | TSL2583_CNTRL, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) "%s: failed to set the power state to %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * Turn the device on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * Configuration must be set before calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* Power on the device; ADC off. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ret = i2c_smbus_write_byte_data(chip->client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) TSL2583_CMD_REG | TSL2583_INTERRUPT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) TSL2583_INTERRUPT_DISABLED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) "%s: failed to disable interrupts\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ret = tsl2583_set_als_time(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) ret = tsl2583_set_als_gain(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) usleep_range(3000, 3500);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) TSL2583_CNTL_ADC_ENBL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* Sysfs Interface Functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static ssize_t in_illuminance_input_target_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct iio_dev *indio_dev = dev_to_iio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) static ssize_t in_illuminance_input_target_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct iio_dev *indio_dev = dev_to_iio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (kstrtoint(buf, 0, &value) || !value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) chip->als_settings.als_cal_target = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) static ssize_t in_illuminance_calibrate_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) struct iio_dev *indio_dev = dev_to_iio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) int value, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (kstrtoint(buf, 0, &value) || value != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) ret = tsl2583_als_calibrate(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static ssize_t in_illuminance_lux_table_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) struct iio_dev *indio_dev = dev_to_iio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) offset += sprintf(buf + offset, "%u,%u,%u,",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) chip->als_settings.als_device_lux[i].ratio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) chip->als_settings.als_device_lux[i].ch0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) chip->als_settings.als_device_lux[i].ch1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (chip->als_settings.als_device_lux[i].ratio == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * We just printed the first "0" entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * Now get rid of the extra "," and break.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) offset--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) offset += sprintf(buf + offset, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) static ssize_t in_illuminance_lux_table_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) struct iio_dev *indio_dev = dev_to_iio_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) unsigned int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) get_options(buf, ARRAY_SIZE(value), value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * We now have an array of ints starting at value[1], and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * enumerated by value[0].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * We expect each group of three ints is one table entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * and the last table entry is all 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) n = value[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if ((n % 3) || n < 6 || n > max_ints) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) "%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) __func__, max_ints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) memcpy(chip->als_settings.als_device_lux, &value[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) value[0] * sizeof(value[1]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) ret = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static IIO_CONST_ATTR(in_illuminance_integration_time_available,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) "0.050 0.100 0.150 0.200 0.250 0.300 0.350 0.400 0.450 0.500 0.550 0.600 0.650");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) static IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) static struct attribute *sysfs_attrs_ctrl[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) &iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) &iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) &iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) &iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) &iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) static const struct attribute_group tsl2583_attribute_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) .attrs = sysfs_attrs_ctrl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static const struct iio_chan_spec tsl2583_channels[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) .type = IIO_LIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) .modified = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) .channel2 = IIO_MOD_LIGHT_IR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) .type = IIO_LIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) .modified = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) .channel2 = IIO_MOD_LIGHT_BOTH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) .type = IIO_LIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) BIT(IIO_CHAN_INFO_CALIBBIAS) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) BIT(IIO_CHAN_INFO_CALIBSCALE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) BIT(IIO_CHAN_INFO_INT_TIME),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) static int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) if (on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ret = pm_runtime_get_sync(&chip->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) pm_runtime_put_noidle(&chip->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) pm_runtime_mark_last_busy(&chip->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) ret = pm_runtime_put_autosuspend(&chip->client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) static int tsl2583_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) int ret, pm_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) ret = tsl2583_set_pm_runtime_busy(chip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) ret = tsl2583_get_lux(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) goto read_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * From page 20 of the TSL2581, TSL2583 data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * sheet (TAOS134 − MARCH 2011):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * One of the photodiodes (channel 0) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * sensitive to both visible and infrared light,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * while the second photodiode (channel 1) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * sensitive primarily to infrared light.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) *val = chip->als_cur_info.als_ch0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) *val = chip->als_cur_info.als_ch1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) ret = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) case IIO_CHAN_INFO_PROCESSED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) ret = tsl2583_get_lux(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) goto read_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) *val = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) ret = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) case IIO_CHAN_INFO_CALIBBIAS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) *val = chip->als_settings.als_gain_trim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) ret = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) case IIO_CHAN_INFO_CALIBSCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) *val = gainadj[chip->als_settings.als_gain].mean;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) ret = IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) case IIO_CHAN_INFO_INT_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) *val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) *val2 = chip->als_settings.als_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) ret = IIO_VAL_INT_PLUS_MICRO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) read_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * Preserve the ret variable if the call to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * tsl2583_set_pm_runtime_busy() is successful so the reading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * (if applicable) is returned to user space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) pm_ret = tsl2583_set_pm_runtime_busy(chip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) if (pm_ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) return pm_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static int tsl2583_write_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int val, int val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) ret = tsl2583_set_pm_runtime_busy(chip, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) case IIO_CHAN_INFO_CALIBBIAS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) chip->als_settings.als_gain_trim = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) case IIO_CHAN_INFO_CALIBSCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (chan->type == IIO_LIGHT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (gainadj[i].mean == val) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) chip->als_settings.als_gain = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ret = tsl2583_set_als_gain(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) case IIO_CHAN_INFO_INT_TIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) val2 <= 650 && !(val2 % 50)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) chip->als_settings.als_time = val2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) ret = tsl2583_set_als_time(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) ret = tsl2583_set_pm_runtime_busy(chip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) static const struct iio_info tsl2583_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) .attrs = &tsl2583_attribute_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) .read_raw = tsl2583_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) .write_raw = tsl2583_write_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) static int tsl2583_probe(struct i2c_client *clientp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) const struct i2c_device_id *idp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) struct tsl2583_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) if (!i2c_check_functionality(clientp->adapter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) I2C_FUNC_SMBUS_BYTE_DATA)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) chip->client = clientp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) i2c_set_clientdata(clientp, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) mutex_init(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ret = i2c_smbus_read_byte_data(clientp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) TSL2583_CMD_REG | TSL2583_CHIPID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) dev_err(&clientp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) "%s: failed to read the chip ID register\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) indio_dev->info = &tsl2583_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) indio_dev->channels = tsl2583_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) indio_dev->name = chip->client->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) pm_runtime_enable(&clientp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) pm_runtime_set_autosuspend_delay(&clientp->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) TSL2583_POWER_OFF_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) pm_runtime_use_autosuspend(&clientp->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) dev_err(&clientp->dev, "%s: iio registration failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /* Load up the V2 defaults (these are hard coded defaults for now) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) tsl2583_defaults(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) dev_info(&clientp->dev, "Light sensor found.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) static int tsl2583_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) struct iio_dev *indio_dev = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) iio_device_unregister(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) pm_runtime_disable(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) pm_runtime_set_suspended(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) pm_runtime_put_noidle(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) static int __maybe_unused tsl2583_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) static int __maybe_unused tsl2583_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) struct tsl2583_chip *chip = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) mutex_lock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) ret = tsl2583_chip_init_and_power_on(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) mutex_unlock(&chip->als_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) static const struct dev_pm_ops tsl2583_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) pm_runtime_force_resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) SET_RUNTIME_PM_OPS(tsl2583_suspend, tsl2583_resume, NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) static const struct i2c_device_id tsl2583_idtable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) { "tsl2580", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) { "tsl2581", 1 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) { "tsl2583", 2 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) MODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) static const struct of_device_id tsl2583_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) { .compatible = "amstaos,tsl2580", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) { .compatible = "amstaos,tsl2581", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) { .compatible = "amstaos,tsl2583", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) MODULE_DEVICE_TABLE(of, tsl2583_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) /* Driver definition */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) static struct i2c_driver tsl2583_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) .name = "tsl2583",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) .pm = &tsl2583_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) .of_match_table = tsl2583_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) .id_table = tsl2583_idtable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) .probe = tsl2583_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) .remove = tsl2583_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) module_i2c_driver(tsl2583_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) MODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) MODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) MODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) MODULE_LICENSE("GPL");