18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Device driver for monitoring ambient light intensity (lux)
48c2ecf20Sopenharmony_ci * within the TAOS tsl258x family of devices (tsl2580, tsl2581, tsl2583).
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (c) 2011, TAOS Corporation.
78c2ecf20Sopenharmony_ci * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/errno.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/string.h>
158c2ecf20Sopenharmony_ci#include <linux/mutex.h>
168c2ecf20Sopenharmony_ci#include <linux/unistd.h>
178c2ecf20Sopenharmony_ci#include <linux/slab.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/iio/iio.h>
208c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h>
218c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Device Registers and Masks */
248c2ecf20Sopenharmony_ci#define TSL2583_CNTRL			0x00
258c2ecf20Sopenharmony_ci#define TSL2583_ALS_TIME		0X01
268c2ecf20Sopenharmony_ci#define TSL2583_INTERRUPT		0x02
278c2ecf20Sopenharmony_ci#define TSL2583_GAIN			0x07
288c2ecf20Sopenharmony_ci#define TSL2583_REVID			0x11
298c2ecf20Sopenharmony_ci#define TSL2583_CHIPID			0x12
308c2ecf20Sopenharmony_ci#define TSL2583_ALS_CHAN0LO		0x14
318c2ecf20Sopenharmony_ci#define TSL2583_ALS_CHAN0HI		0x15
328c2ecf20Sopenharmony_ci#define TSL2583_ALS_CHAN1LO		0x16
338c2ecf20Sopenharmony_ci#define TSL2583_ALS_CHAN1HI		0x17
348c2ecf20Sopenharmony_ci#define TSL2583_TMR_LO			0x18
358c2ecf20Sopenharmony_ci#define TSL2583_TMR_HI			0x19
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/* tsl2583 cmd reg masks */
388c2ecf20Sopenharmony_ci#define TSL2583_CMD_REG			0x80
398c2ecf20Sopenharmony_ci#define TSL2583_CMD_SPL_FN		0x60
408c2ecf20Sopenharmony_ci#define TSL2583_CMD_ALS_INT_CLR		0x01
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* tsl2583 cntrl reg masks */
438c2ecf20Sopenharmony_ci#define TSL2583_CNTL_ADC_ENBL		0x02
448c2ecf20Sopenharmony_ci#define TSL2583_CNTL_PWR_OFF		0x00
458c2ecf20Sopenharmony_ci#define TSL2583_CNTL_PWR_ON		0x01
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/* tsl2583 status reg masks */
488c2ecf20Sopenharmony_ci#define TSL2583_STA_ADC_VALID		0x01
498c2ecf20Sopenharmony_ci#define TSL2583_STA_ADC_INTR		0x10
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/* Lux calculation constants */
528c2ecf20Sopenharmony_ci#define TSL2583_LUX_CALC_OVER_FLOW	65535
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define TSL2583_INTERRUPT_DISABLED	0x00
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define TSL2583_CHIP_ID			0x90
578c2ecf20Sopenharmony_ci#define TSL2583_CHIP_ID_MASK		0xf0
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define TSL2583_POWER_OFF_DELAY_MS	2000
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* Per-device data */
628c2ecf20Sopenharmony_cistruct tsl2583_als_info {
638c2ecf20Sopenharmony_ci	u16 als_ch0;
648c2ecf20Sopenharmony_ci	u16 als_ch1;
658c2ecf20Sopenharmony_ci	u16 lux;
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistruct tsl2583_lux {
698c2ecf20Sopenharmony_ci	unsigned int ratio;
708c2ecf20Sopenharmony_ci	unsigned int ch0;
718c2ecf20Sopenharmony_ci	unsigned int ch1;
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic const struct tsl2583_lux tsl2583_default_lux[] = {
758c2ecf20Sopenharmony_ci	{  9830,  8520, 15729 },
768c2ecf20Sopenharmony_ci	{ 12452, 10807, 23344 },
778c2ecf20Sopenharmony_ci	{ 14746,  6383, 11705 },
788c2ecf20Sopenharmony_ci	{ 17695,  4063,  6554 },
798c2ecf20Sopenharmony_ci	{     0,     0,     0 }  /* Termination segment */
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define TSL2583_MAX_LUX_TABLE_ENTRIES 11
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistruct tsl2583_settings {
858c2ecf20Sopenharmony_ci	int als_time;
868c2ecf20Sopenharmony_ci	int als_gain;
878c2ecf20Sopenharmony_ci	int als_gain_trim;
888c2ecf20Sopenharmony_ci	int als_cal_target;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/*
918c2ecf20Sopenharmony_ci	 * This structure is intentionally large to accommodate updates via
928c2ecf20Sopenharmony_ci	 * sysfs. Sized to 11 = max 10 segments + 1 termination segment.
938c2ecf20Sopenharmony_ci	 * Assumption is that one and only one type of glass used.
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	struct tsl2583_lux als_device_lux[TSL2583_MAX_LUX_TABLE_ENTRIES];
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct tsl2583_chip {
998c2ecf20Sopenharmony_ci	struct mutex als_mutex;
1008c2ecf20Sopenharmony_ci	struct i2c_client *client;
1018c2ecf20Sopenharmony_ci	struct tsl2583_als_info als_cur_info;
1028c2ecf20Sopenharmony_ci	struct tsl2583_settings als_settings;
1038c2ecf20Sopenharmony_ci	int als_time_scale;
1048c2ecf20Sopenharmony_ci	int als_saturation;
1058c2ecf20Sopenharmony_ci};
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistruct gainadj {
1088c2ecf20Sopenharmony_ci	s16 ch0;
1098c2ecf20Sopenharmony_ci	s16 ch1;
1108c2ecf20Sopenharmony_ci	s16 mean;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* Index = (0 - 3) Used to validate the gain selection index */
1148c2ecf20Sopenharmony_cistatic const struct gainadj gainadj[] = {
1158c2ecf20Sopenharmony_ci	{ 1, 1, 1 },
1168c2ecf20Sopenharmony_ci	{ 8, 8, 8 },
1178c2ecf20Sopenharmony_ci	{ 16, 16, 16 },
1188c2ecf20Sopenharmony_ci	{ 107, 115, 111 }
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci/*
1228c2ecf20Sopenharmony_ci * Provides initial operational parameter defaults.
1238c2ecf20Sopenharmony_ci * These defaults may be changed through the device's sysfs files.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_cistatic void tsl2583_defaults(struct tsl2583_chip *chip)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	/*
1288c2ecf20Sopenharmony_ci	 * The integration time must be a multiple of 50ms and within the
1298c2ecf20Sopenharmony_ci	 * range [50, 600] ms.
1308c2ecf20Sopenharmony_ci	 */
1318c2ecf20Sopenharmony_ci	chip->als_settings.als_time = 100;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	/*
1348c2ecf20Sopenharmony_ci	 * This is an index into the gainadj table. Assume clear glass as the
1358c2ecf20Sopenharmony_ci	 * default.
1368c2ecf20Sopenharmony_ci	 */
1378c2ecf20Sopenharmony_ci	chip->als_settings.als_gain = 0;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* Default gain trim to account for aperture effects */
1408c2ecf20Sopenharmony_ci	chip->als_settings.als_gain_trim = 1000;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* Known external ALS reading used for calibration */
1438c2ecf20Sopenharmony_ci	chip->als_settings.als_cal_target = 130;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* Default lux table. */
1468c2ecf20Sopenharmony_ci	memcpy(chip->als_settings.als_device_lux, tsl2583_default_lux,
1478c2ecf20Sopenharmony_ci	       sizeof(tsl2583_default_lux));
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/*
1518c2ecf20Sopenharmony_ci * Reads and calculates current lux value.
1528c2ecf20Sopenharmony_ci * The raw ch0 and ch1 values of the ambient light sensed in the last
1538c2ecf20Sopenharmony_ci * integration cycle are read from the device.
1548c2ecf20Sopenharmony_ci * Time scale factor array values are adjusted based on the integration time.
1558c2ecf20Sopenharmony_ci * The raw values are multiplied by a scale factor, and device gain is obtained
1568c2ecf20Sopenharmony_ci * using gain index. Limit checks are done next, then the ratio of a multiple
1578c2ecf20Sopenharmony_ci * of ch1 value, to the ch0 value, is calculated. The array als_device_lux[]
1588c2ecf20Sopenharmony_ci * declared above is then scanned to find the first ratio value that is just
1598c2ecf20Sopenharmony_ci * above the ratio we just calculated. The ch0 and ch1 multiplier constants in
1608c2ecf20Sopenharmony_ci * the array are then used along with the time scale factor array values, to
1618c2ecf20Sopenharmony_ci * calculate the lux.
1628c2ecf20Sopenharmony_ci */
1638c2ecf20Sopenharmony_cistatic int tsl2583_get_lux(struct iio_dev *indio_dev)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	u16 ch0, ch1; /* separated ch0/ch1 data from device */
1668c2ecf20Sopenharmony_ci	u32 lux; /* raw lux calculated from device data */
1678c2ecf20Sopenharmony_ci	u64 lux64;
1688c2ecf20Sopenharmony_ci	u32 ratio;
1698c2ecf20Sopenharmony_ci	u8 buf[5];
1708c2ecf20Sopenharmony_ci	struct tsl2583_lux *p;
1718c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
1728c2ecf20Sopenharmony_ci	int i, ret;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(chip->client, TSL2583_CMD_REG);
1758c2ecf20Sopenharmony_ci	if (ret < 0) {
1768c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev, "%s: failed to read CMD_REG register\n",
1778c2ecf20Sopenharmony_ci			__func__);
1788c2ecf20Sopenharmony_ci		goto done;
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	/* is data new & valid */
1828c2ecf20Sopenharmony_ci	if (!(ret & TSL2583_STA_ADC_INTR)) {
1838c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev, "%s: data not valid; returning last value\n",
1848c2ecf20Sopenharmony_ci			__func__);
1858c2ecf20Sopenharmony_ci		ret = chip->als_cur_info.lux; /* return LAST VALUE */
1868c2ecf20Sopenharmony_ci		goto done;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
1908c2ecf20Sopenharmony_ci		int reg = TSL2583_CMD_REG | (TSL2583_ALS_CHAN0LO + i);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_byte_data(chip->client, reg);
1938c2ecf20Sopenharmony_ci		if (ret < 0) {
1948c2ecf20Sopenharmony_ci			dev_err(&chip->client->dev, "%s: failed to read register %x\n",
1958c2ecf20Sopenharmony_ci				__func__, reg);
1968c2ecf20Sopenharmony_ci			goto done;
1978c2ecf20Sopenharmony_ci		}
1988c2ecf20Sopenharmony_ci		buf[i] = ret;
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	/*
2028c2ecf20Sopenharmony_ci	 * Clear the pending interrupt status bit on the chip to allow the next
2038c2ecf20Sopenharmony_ci	 * integration cycle to start. This has to be done even though this
2048c2ecf20Sopenharmony_ci	 * driver currently does not support interrupts.
2058c2ecf20Sopenharmony_ci	 */
2068c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte(chip->client,
2078c2ecf20Sopenharmony_ci				   (TSL2583_CMD_REG | TSL2583_CMD_SPL_FN |
2088c2ecf20Sopenharmony_ci				    TSL2583_CMD_ALS_INT_CLR));
2098c2ecf20Sopenharmony_ci	if (ret < 0) {
2108c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev, "%s: failed to clear the interrupt bit\n",
2118c2ecf20Sopenharmony_ci			__func__);
2128c2ecf20Sopenharmony_ci		goto done; /* have no data, so return failure */
2138c2ecf20Sopenharmony_ci	}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	/* extract ALS/lux data */
2168c2ecf20Sopenharmony_ci	ch0 = le16_to_cpup((const __le16 *)&buf[0]);
2178c2ecf20Sopenharmony_ci	ch1 = le16_to_cpup((const __le16 *)&buf[2]);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	chip->als_cur_info.als_ch0 = ch0;
2208c2ecf20Sopenharmony_ci	chip->als_cur_info.als_ch1 = ch1;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if ((ch0 >= chip->als_saturation) || (ch1 >= chip->als_saturation))
2238c2ecf20Sopenharmony_ci		goto return_max;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	if (!ch0) {
2268c2ecf20Sopenharmony_ci		/*
2278c2ecf20Sopenharmony_ci		 * The sensor appears to be in total darkness so set the
2288c2ecf20Sopenharmony_ci		 * calculated lux to 0 and return early to avoid a division by
2298c2ecf20Sopenharmony_ci		 * zero below when calculating the ratio.
2308c2ecf20Sopenharmony_ci		 */
2318c2ecf20Sopenharmony_ci		ret = 0;
2328c2ecf20Sopenharmony_ci		chip->als_cur_info.lux = 0;
2338c2ecf20Sopenharmony_ci		goto done;
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	/* calculate ratio */
2378c2ecf20Sopenharmony_ci	ratio = (ch1 << 15) / ch0;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* convert to unscaled lux using the pointer to the table */
2408c2ecf20Sopenharmony_ci	for (p = (struct tsl2583_lux *)chip->als_settings.als_device_lux;
2418c2ecf20Sopenharmony_ci	     p->ratio != 0 && p->ratio < ratio; p++)
2428c2ecf20Sopenharmony_ci		;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (p->ratio == 0) {
2458c2ecf20Sopenharmony_ci		lux = 0;
2468c2ecf20Sopenharmony_ci	} else {
2478c2ecf20Sopenharmony_ci		u32 ch0lux, ch1lux;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		ch0lux = ((ch0 * p->ch0) +
2508c2ecf20Sopenharmony_ci			  (gainadj[chip->als_settings.als_gain].ch0 >> 1))
2518c2ecf20Sopenharmony_ci			 / gainadj[chip->als_settings.als_gain].ch0;
2528c2ecf20Sopenharmony_ci		ch1lux = ((ch1 * p->ch1) +
2538c2ecf20Sopenharmony_ci			  (gainadj[chip->als_settings.als_gain].ch1 >> 1))
2548c2ecf20Sopenharmony_ci			 / gainadj[chip->als_settings.als_gain].ch1;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci		/* note: lux is 31 bit max at this point */
2578c2ecf20Sopenharmony_ci		if (ch1lux > ch0lux) {
2588c2ecf20Sopenharmony_ci			dev_dbg(&chip->client->dev, "%s: No Data - Returning 0\n",
2598c2ecf20Sopenharmony_ci				__func__);
2608c2ecf20Sopenharmony_ci			ret = 0;
2618c2ecf20Sopenharmony_ci			chip->als_cur_info.lux = 0;
2628c2ecf20Sopenharmony_ci			goto done;
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci		lux = ch0lux - ch1lux;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* adjust for active time scale */
2698c2ecf20Sopenharmony_ci	if (chip->als_time_scale == 0)
2708c2ecf20Sopenharmony_ci		lux = 0;
2718c2ecf20Sopenharmony_ci	else
2728c2ecf20Sopenharmony_ci		lux = (lux + (chip->als_time_scale >> 1)) /
2738c2ecf20Sopenharmony_ci			chip->als_time_scale;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/*
2768c2ecf20Sopenharmony_ci	 * Adjust for active gain scale.
2778c2ecf20Sopenharmony_ci	 * The tsl2583_default_lux tables above have a factor of 8192 built in,
2788c2ecf20Sopenharmony_ci	 * so we need to shift right.
2798c2ecf20Sopenharmony_ci	 * User-specified gain provides a multiplier.
2808c2ecf20Sopenharmony_ci	 * Apply user-specified gain before shifting right to retain precision.
2818c2ecf20Sopenharmony_ci	 * Use 64 bits to avoid overflow on multiplication.
2828c2ecf20Sopenharmony_ci	 * Then go back to 32 bits before division to avoid using div_u64().
2838c2ecf20Sopenharmony_ci	 */
2848c2ecf20Sopenharmony_ci	lux64 = lux;
2858c2ecf20Sopenharmony_ci	lux64 = lux64 * chip->als_settings.als_gain_trim;
2868c2ecf20Sopenharmony_ci	lux64 >>= 13;
2878c2ecf20Sopenharmony_ci	lux = lux64;
2888c2ecf20Sopenharmony_ci	lux = (lux + 500) / 1000;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (lux > TSL2583_LUX_CALC_OVER_FLOW) { /* check for overflow */
2918c2ecf20Sopenharmony_cireturn_max:
2928c2ecf20Sopenharmony_ci		lux = TSL2583_LUX_CALC_OVER_FLOW;
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	/* Update the structure with the latest VALID lux. */
2968c2ecf20Sopenharmony_ci	chip->als_cur_info.lux = lux;
2978c2ecf20Sopenharmony_ci	ret = lux;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cidone:
3008c2ecf20Sopenharmony_ci	return ret;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci/*
3048c2ecf20Sopenharmony_ci * Obtain single reading and calculate the als_gain_trim (later used
3058c2ecf20Sopenharmony_ci * to derive actual lux).
3068c2ecf20Sopenharmony_ci * Return updated gain_trim value.
3078c2ecf20Sopenharmony_ci */
3088c2ecf20Sopenharmony_cistatic int tsl2583_als_calibrate(struct iio_dev *indio_dev)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
3118c2ecf20Sopenharmony_ci	unsigned int gain_trim_val;
3128c2ecf20Sopenharmony_ci	int ret;
3138c2ecf20Sopenharmony_ci	int lux_val;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(chip->client,
3168c2ecf20Sopenharmony_ci				       TSL2583_CMD_REG | TSL2583_CNTRL);
3178c2ecf20Sopenharmony_ci	if (ret < 0) {
3188c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
3198c2ecf20Sopenharmony_ci			"%s: failed to read from the CNTRL register\n",
3208c2ecf20Sopenharmony_ci			__func__);
3218c2ecf20Sopenharmony_ci		return ret;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if ((ret & (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON))
3258c2ecf20Sopenharmony_ci			!= (TSL2583_CNTL_ADC_ENBL | TSL2583_CNTL_PWR_ON)) {
3268c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
3278c2ecf20Sopenharmony_ci			"%s: Device is not powered on and/or ADC is not enabled\n",
3288c2ecf20Sopenharmony_ci			__func__);
3298c2ecf20Sopenharmony_ci		return -EINVAL;
3308c2ecf20Sopenharmony_ci	} else if ((ret & TSL2583_STA_ADC_VALID) != TSL2583_STA_ADC_VALID) {
3318c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
3328c2ecf20Sopenharmony_ci			"%s: The two ADC channels have not completed an integration cycle\n",
3338c2ecf20Sopenharmony_ci			__func__);
3348c2ecf20Sopenharmony_ci		return -ENODATA;
3358c2ecf20Sopenharmony_ci	}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	lux_val = tsl2583_get_lux(indio_dev);
3388c2ecf20Sopenharmony_ci	if (lux_val < 0) {
3398c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev, "%s: failed to get lux\n",
3408c2ecf20Sopenharmony_ci			__func__);
3418c2ecf20Sopenharmony_ci		return lux_val;
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* Avoid division by zero of lux_value later on */
3458c2ecf20Sopenharmony_ci	if (lux_val == 0) {
3468c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
3478c2ecf20Sopenharmony_ci			"%s: lux_val of 0 will produce out of range trim_value\n",
3488c2ecf20Sopenharmony_ci			__func__);
3498c2ecf20Sopenharmony_ci		return -ENODATA;
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	gain_trim_val = (unsigned int)(((chip->als_settings.als_cal_target)
3538c2ecf20Sopenharmony_ci			* chip->als_settings.als_gain_trim) / lux_val);
3548c2ecf20Sopenharmony_ci	if ((gain_trim_val < 250) || (gain_trim_val > 4000)) {
3558c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
3568c2ecf20Sopenharmony_ci			"%s: trim_val of %d is not within the range [250, 4000]\n",
3578c2ecf20Sopenharmony_ci			__func__, gain_trim_val);
3588c2ecf20Sopenharmony_ci		return -ENODATA;
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	chip->als_settings.als_gain_trim = (int)gain_trim_val;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	return 0;
3648c2ecf20Sopenharmony_ci}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_cistatic int tsl2583_set_als_time(struct tsl2583_chip *chip)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	int als_count, als_time, ret;
3698c2ecf20Sopenharmony_ci	u8 val;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* determine als integration register */
3728c2ecf20Sopenharmony_ci	als_count = (chip->als_settings.als_time * 100 + 135) / 270;
3738c2ecf20Sopenharmony_ci	if (!als_count)
3748c2ecf20Sopenharmony_ci		als_count = 1; /* ensure at least one cycle */
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	/* convert back to time (encompasses overrides) */
3778c2ecf20Sopenharmony_ci	als_time = (als_count * 27 + 5) / 10;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	val = 256 - als_count;
3808c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(chip->client,
3818c2ecf20Sopenharmony_ci					TSL2583_CMD_REG | TSL2583_ALS_TIME,
3828c2ecf20Sopenharmony_ci					val);
3838c2ecf20Sopenharmony_ci	if (ret < 0) {
3848c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev, "%s: failed to set the als time to %d\n",
3858c2ecf20Sopenharmony_ci			__func__, val);
3868c2ecf20Sopenharmony_ci		return ret;
3878c2ecf20Sopenharmony_ci	}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	/* set chip struct re scaling and saturation */
3908c2ecf20Sopenharmony_ci	chip->als_saturation = als_count * 922; /* 90% of full scale */
3918c2ecf20Sopenharmony_ci	chip->als_time_scale = (als_time + 25) / 50;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	return ret;
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_cistatic int tsl2583_set_als_gain(struct tsl2583_chip *chip)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	int ret;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* Set the gain based on als_settings struct */
4018c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(chip->client,
4028c2ecf20Sopenharmony_ci					TSL2583_CMD_REG | TSL2583_GAIN,
4038c2ecf20Sopenharmony_ci					chip->als_settings.als_gain);
4048c2ecf20Sopenharmony_ci	if (ret < 0)
4058c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
4068c2ecf20Sopenharmony_ci			"%s: failed to set the gain to %d\n", __func__,
4078c2ecf20Sopenharmony_ci			chip->als_settings.als_gain);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	return ret;
4108c2ecf20Sopenharmony_ci}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_cistatic int tsl2583_set_power_state(struct tsl2583_chip *chip, u8 state)
4138c2ecf20Sopenharmony_ci{
4148c2ecf20Sopenharmony_ci	int ret;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(chip->client,
4178c2ecf20Sopenharmony_ci					TSL2583_CMD_REG | TSL2583_CNTRL, state);
4188c2ecf20Sopenharmony_ci	if (ret < 0)
4198c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
4208c2ecf20Sopenharmony_ci			"%s: failed to set the power state to %d\n", __func__,
4218c2ecf20Sopenharmony_ci			state);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	return ret;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/*
4278c2ecf20Sopenharmony_ci * Turn the device on.
4288c2ecf20Sopenharmony_ci * Configuration must be set before calling this function.
4298c2ecf20Sopenharmony_ci */
4308c2ecf20Sopenharmony_cistatic int tsl2583_chip_init_and_power_on(struct iio_dev *indio_dev)
4318c2ecf20Sopenharmony_ci{
4328c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
4338c2ecf20Sopenharmony_ci	int ret;
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	/* Power on the device; ADC off. */
4368c2ecf20Sopenharmony_ci	ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON);
4378c2ecf20Sopenharmony_ci	if (ret < 0)
4388c2ecf20Sopenharmony_ci		return ret;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	ret = i2c_smbus_write_byte_data(chip->client,
4418c2ecf20Sopenharmony_ci					TSL2583_CMD_REG | TSL2583_INTERRUPT,
4428c2ecf20Sopenharmony_ci					TSL2583_INTERRUPT_DISABLED);
4438c2ecf20Sopenharmony_ci	if (ret < 0) {
4448c2ecf20Sopenharmony_ci		dev_err(&chip->client->dev,
4458c2ecf20Sopenharmony_ci			"%s: failed to disable interrupts\n", __func__);
4468c2ecf20Sopenharmony_ci		return ret;
4478c2ecf20Sopenharmony_ci	}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	ret = tsl2583_set_als_time(chip);
4508c2ecf20Sopenharmony_ci	if (ret < 0)
4518c2ecf20Sopenharmony_ci		return ret;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	ret = tsl2583_set_als_gain(chip);
4548c2ecf20Sopenharmony_ci	if (ret < 0)
4558c2ecf20Sopenharmony_ci		return ret;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	usleep_range(3000, 3500);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_ON |
4608c2ecf20Sopenharmony_ci					    TSL2583_CNTL_ADC_ENBL);
4618c2ecf20Sopenharmony_ci	if (ret < 0)
4628c2ecf20Sopenharmony_ci		return ret;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	return ret;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci/* Sysfs Interface Functions */
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic ssize_t in_illuminance_input_target_show(struct device *dev,
4708c2ecf20Sopenharmony_ci						struct device_attribute *attr,
4718c2ecf20Sopenharmony_ci						char *buf)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
4748c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
4758c2ecf20Sopenharmony_ci	int ret;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
4788c2ecf20Sopenharmony_ci	ret = sprintf(buf, "%d\n", chip->als_settings.als_cal_target);
4798c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	return ret;
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic ssize_t in_illuminance_input_target_store(struct device *dev,
4858c2ecf20Sopenharmony_ci						 struct device_attribute *attr,
4868c2ecf20Sopenharmony_ci						 const char *buf, size_t len)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
4898c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
4908c2ecf20Sopenharmony_ci	int value;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	if (kstrtoint(buf, 0, &value) || !value)
4938c2ecf20Sopenharmony_ci		return -EINVAL;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
4968c2ecf20Sopenharmony_ci	chip->als_settings.als_cal_target = value;
4978c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	return len;
5008c2ecf20Sopenharmony_ci}
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic ssize_t in_illuminance_calibrate_store(struct device *dev,
5038c2ecf20Sopenharmony_ci					      struct device_attribute *attr,
5048c2ecf20Sopenharmony_ci					      const char *buf, size_t len)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
5078c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
5088c2ecf20Sopenharmony_ci	int value, ret;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (kstrtoint(buf, 0, &value) || value != 1)
5118c2ecf20Sopenharmony_ci		return -EINVAL;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	ret = tsl2583_als_calibrate(indio_dev);
5168c2ecf20Sopenharmony_ci	if (ret < 0)
5178c2ecf20Sopenharmony_ci		goto done;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	ret = len;
5208c2ecf20Sopenharmony_cidone:
5218c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	return ret;
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic ssize_t in_illuminance_lux_table_show(struct device *dev,
5278c2ecf20Sopenharmony_ci					     struct device_attribute *attr,
5288c2ecf20Sopenharmony_ci					     char *buf)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
5318c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
5328c2ecf20Sopenharmony_ci	unsigned int i;
5338c2ecf20Sopenharmony_ci	int offset = 0;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(chip->als_settings.als_device_lux); i++) {
5368c2ecf20Sopenharmony_ci		offset += sprintf(buf + offset, "%u,%u,%u,",
5378c2ecf20Sopenharmony_ci				  chip->als_settings.als_device_lux[i].ratio,
5388c2ecf20Sopenharmony_ci				  chip->als_settings.als_device_lux[i].ch0,
5398c2ecf20Sopenharmony_ci				  chip->als_settings.als_device_lux[i].ch1);
5408c2ecf20Sopenharmony_ci		if (chip->als_settings.als_device_lux[i].ratio == 0) {
5418c2ecf20Sopenharmony_ci			/*
5428c2ecf20Sopenharmony_ci			 * We just printed the first "0" entry.
5438c2ecf20Sopenharmony_ci			 * Now get rid of the extra "," and break.
5448c2ecf20Sopenharmony_ci			 */
5458c2ecf20Sopenharmony_ci			offset--;
5468c2ecf20Sopenharmony_ci			break;
5478c2ecf20Sopenharmony_ci		}
5488c2ecf20Sopenharmony_ci	}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	offset += sprintf(buf + offset, "\n");
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	return offset;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic ssize_t in_illuminance_lux_table_store(struct device *dev,
5568c2ecf20Sopenharmony_ci					      struct device_attribute *attr,
5578c2ecf20Sopenharmony_ci					      const char *buf, size_t len)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
5608c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
5618c2ecf20Sopenharmony_ci	const unsigned int max_ints = TSL2583_MAX_LUX_TABLE_ENTRIES * 3;
5628c2ecf20Sopenharmony_ci	int value[TSL2583_MAX_LUX_TABLE_ENTRIES * 3 + 1];
5638c2ecf20Sopenharmony_ci	int ret = -EINVAL;
5648c2ecf20Sopenharmony_ci	unsigned int n;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	get_options(buf, ARRAY_SIZE(value), value);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	/*
5718c2ecf20Sopenharmony_ci	 * We now have an array of ints starting at value[1], and
5728c2ecf20Sopenharmony_ci	 * enumerated by value[0].
5738c2ecf20Sopenharmony_ci	 * We expect each group of three ints is one table entry,
5748c2ecf20Sopenharmony_ci	 * and the last table entry is all 0.
5758c2ecf20Sopenharmony_ci	 */
5768c2ecf20Sopenharmony_ci	n = value[0];
5778c2ecf20Sopenharmony_ci	if ((n % 3) || n < 6 || n > max_ints) {
5788c2ecf20Sopenharmony_ci		dev_err(dev,
5798c2ecf20Sopenharmony_ci			"%s: The number of entries in the lux table must be a multiple of 3 and within the range [6, %d]\n",
5808c2ecf20Sopenharmony_ci			__func__, max_ints);
5818c2ecf20Sopenharmony_ci		goto done;
5828c2ecf20Sopenharmony_ci	}
5838c2ecf20Sopenharmony_ci	if ((value[n - 2] | value[n - 1] | value[n]) != 0) {
5848c2ecf20Sopenharmony_ci		dev_err(dev, "%s: The last 3 entries in the lux table must be zeros.\n",
5858c2ecf20Sopenharmony_ci			__func__);
5868c2ecf20Sopenharmony_ci		goto done;
5878c2ecf20Sopenharmony_ci	}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	memcpy(chip->als_settings.als_device_lux, &value[1],
5908c2ecf20Sopenharmony_ci	       value[0] * sizeof(value[1]));
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	ret = len;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cidone:
5958c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	return ret;
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(in_illuminance_calibscale_available, "1 8 16 111");
6018c2ecf20Sopenharmony_cistatic IIO_CONST_ATTR(in_illuminance_integration_time_available,
6028c2ecf20Sopenharmony_ci		      "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");
6038c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR_RW(in_illuminance_input_target, 0);
6048c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR_WO(in_illuminance_calibrate, 0);
6058c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR_RW(in_illuminance_lux_table, 0);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_cistatic struct attribute *sysfs_attrs_ctrl[] = {
6088c2ecf20Sopenharmony_ci	&iio_const_attr_in_illuminance_calibscale_available.dev_attr.attr,
6098c2ecf20Sopenharmony_ci	&iio_const_attr_in_illuminance_integration_time_available.dev_attr.attr,
6108c2ecf20Sopenharmony_ci	&iio_dev_attr_in_illuminance_input_target.dev_attr.attr,
6118c2ecf20Sopenharmony_ci	&iio_dev_attr_in_illuminance_calibrate.dev_attr.attr,
6128c2ecf20Sopenharmony_ci	&iio_dev_attr_in_illuminance_lux_table.dev_attr.attr,
6138c2ecf20Sopenharmony_ci	NULL
6148c2ecf20Sopenharmony_ci};
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_cistatic const struct attribute_group tsl2583_attribute_group = {
6178c2ecf20Sopenharmony_ci	.attrs = sysfs_attrs_ctrl,
6188c2ecf20Sopenharmony_ci};
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic const struct iio_chan_spec tsl2583_channels[] = {
6218c2ecf20Sopenharmony_ci	{
6228c2ecf20Sopenharmony_ci		.type = IIO_LIGHT,
6238c2ecf20Sopenharmony_ci		.modified = 1,
6248c2ecf20Sopenharmony_ci		.channel2 = IIO_MOD_LIGHT_IR,
6258c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
6268c2ecf20Sopenharmony_ci	},
6278c2ecf20Sopenharmony_ci	{
6288c2ecf20Sopenharmony_ci		.type = IIO_LIGHT,
6298c2ecf20Sopenharmony_ci		.modified = 1,
6308c2ecf20Sopenharmony_ci		.channel2 = IIO_MOD_LIGHT_BOTH,
6318c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
6328c2ecf20Sopenharmony_ci	},
6338c2ecf20Sopenharmony_ci	{
6348c2ecf20Sopenharmony_ci		.type = IIO_LIGHT,
6358c2ecf20Sopenharmony_ci		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
6368c2ecf20Sopenharmony_ci				      BIT(IIO_CHAN_INFO_CALIBBIAS) |
6378c2ecf20Sopenharmony_ci				      BIT(IIO_CHAN_INFO_CALIBSCALE) |
6388c2ecf20Sopenharmony_ci				      BIT(IIO_CHAN_INFO_INT_TIME),
6398c2ecf20Sopenharmony_ci	},
6408c2ecf20Sopenharmony_ci};
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic int tsl2583_set_pm_runtime_busy(struct tsl2583_chip *chip, bool on)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	int ret;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (on) {
6478c2ecf20Sopenharmony_ci		ret = pm_runtime_get_sync(&chip->client->dev);
6488c2ecf20Sopenharmony_ci		if (ret < 0)
6498c2ecf20Sopenharmony_ci			pm_runtime_put_noidle(&chip->client->dev);
6508c2ecf20Sopenharmony_ci	} else {
6518c2ecf20Sopenharmony_ci		pm_runtime_mark_last_busy(&chip->client->dev);
6528c2ecf20Sopenharmony_ci		ret = pm_runtime_put_autosuspend(&chip->client->dev);
6538c2ecf20Sopenharmony_ci	}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	return ret;
6568c2ecf20Sopenharmony_ci}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic int tsl2583_read_raw(struct iio_dev *indio_dev,
6598c2ecf20Sopenharmony_ci			    struct iio_chan_spec const *chan,
6608c2ecf20Sopenharmony_ci			    int *val, int *val2, long mask)
6618c2ecf20Sopenharmony_ci{
6628c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
6638c2ecf20Sopenharmony_ci	int ret, pm_ret;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	ret = tsl2583_set_pm_runtime_busy(chip, true);
6668c2ecf20Sopenharmony_ci	if (ret < 0)
6678c2ecf20Sopenharmony_ci		return ret;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	ret = -EINVAL;
6728c2ecf20Sopenharmony_ci	switch (mask) {
6738c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_RAW:
6748c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
6758c2ecf20Sopenharmony_ci			ret = tsl2583_get_lux(indio_dev);
6768c2ecf20Sopenharmony_ci			if (ret < 0)
6778c2ecf20Sopenharmony_ci				goto read_done;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci			/*
6808c2ecf20Sopenharmony_ci			 * From page 20 of the TSL2581, TSL2583 data
6818c2ecf20Sopenharmony_ci			 * sheet (TAOS134 − MARCH 2011):
6828c2ecf20Sopenharmony_ci			 *
6838c2ecf20Sopenharmony_ci			 * One of the photodiodes (channel 0) is
6848c2ecf20Sopenharmony_ci			 * sensitive to both visible and infrared light,
6858c2ecf20Sopenharmony_ci			 * while the second photodiode (channel 1) is
6868c2ecf20Sopenharmony_ci			 * sensitive primarily to infrared light.
6878c2ecf20Sopenharmony_ci			 */
6888c2ecf20Sopenharmony_ci			if (chan->channel2 == IIO_MOD_LIGHT_BOTH)
6898c2ecf20Sopenharmony_ci				*val = chip->als_cur_info.als_ch0;
6908c2ecf20Sopenharmony_ci			else
6918c2ecf20Sopenharmony_ci				*val = chip->als_cur_info.als_ch1;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci			ret = IIO_VAL_INT;
6948c2ecf20Sopenharmony_ci		}
6958c2ecf20Sopenharmony_ci		break;
6968c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_PROCESSED:
6978c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
6988c2ecf20Sopenharmony_ci			ret = tsl2583_get_lux(indio_dev);
6998c2ecf20Sopenharmony_ci			if (ret < 0)
7008c2ecf20Sopenharmony_ci				goto read_done;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci			*val = ret;
7038c2ecf20Sopenharmony_ci			ret = IIO_VAL_INT;
7048c2ecf20Sopenharmony_ci		}
7058c2ecf20Sopenharmony_ci		break;
7068c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBBIAS:
7078c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
7088c2ecf20Sopenharmony_ci			*val = chip->als_settings.als_gain_trim;
7098c2ecf20Sopenharmony_ci			ret = IIO_VAL_INT;
7108c2ecf20Sopenharmony_ci		}
7118c2ecf20Sopenharmony_ci		break;
7128c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBSCALE:
7138c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
7148c2ecf20Sopenharmony_ci			*val = gainadj[chip->als_settings.als_gain].mean;
7158c2ecf20Sopenharmony_ci			ret = IIO_VAL_INT;
7168c2ecf20Sopenharmony_ci		}
7178c2ecf20Sopenharmony_ci		break;
7188c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_INT_TIME:
7198c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
7208c2ecf20Sopenharmony_ci			*val = 0;
7218c2ecf20Sopenharmony_ci			*val2 = chip->als_settings.als_time;
7228c2ecf20Sopenharmony_ci			ret = IIO_VAL_INT_PLUS_MICRO;
7238c2ecf20Sopenharmony_ci		}
7248c2ecf20Sopenharmony_ci		break;
7258c2ecf20Sopenharmony_ci	default:
7268c2ecf20Sopenharmony_ci		break;
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ciread_done:
7308c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	if (ret < 0)
7338c2ecf20Sopenharmony_ci		return ret;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	/*
7368c2ecf20Sopenharmony_ci	 * Preserve the ret variable if the call to
7378c2ecf20Sopenharmony_ci	 * tsl2583_set_pm_runtime_busy() is successful so the reading
7388c2ecf20Sopenharmony_ci	 * (if applicable) is returned to user space.
7398c2ecf20Sopenharmony_ci	 */
7408c2ecf20Sopenharmony_ci	pm_ret = tsl2583_set_pm_runtime_busy(chip, false);
7418c2ecf20Sopenharmony_ci	if (pm_ret < 0)
7428c2ecf20Sopenharmony_ci		return pm_ret;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	return ret;
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic int tsl2583_write_raw(struct iio_dev *indio_dev,
7488c2ecf20Sopenharmony_ci			     struct iio_chan_spec const *chan,
7498c2ecf20Sopenharmony_ci			     int val, int val2, long mask)
7508c2ecf20Sopenharmony_ci{
7518c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
7528c2ecf20Sopenharmony_ci	int ret;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	ret = tsl2583_set_pm_runtime_busy(chip, true);
7558c2ecf20Sopenharmony_ci	if (ret < 0)
7568c2ecf20Sopenharmony_ci		return ret;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	ret = -EINVAL;
7618c2ecf20Sopenharmony_ci	switch (mask) {
7628c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBBIAS:
7638c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
7648c2ecf20Sopenharmony_ci			chip->als_settings.als_gain_trim = val;
7658c2ecf20Sopenharmony_ci			ret = 0;
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci		break;
7688c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_CALIBSCALE:
7698c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT) {
7708c2ecf20Sopenharmony_ci			unsigned int i;
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci			for (i = 0; i < ARRAY_SIZE(gainadj); i++) {
7738c2ecf20Sopenharmony_ci				if (gainadj[i].mean == val) {
7748c2ecf20Sopenharmony_ci					chip->als_settings.als_gain = i;
7758c2ecf20Sopenharmony_ci					ret = tsl2583_set_als_gain(chip);
7768c2ecf20Sopenharmony_ci					break;
7778c2ecf20Sopenharmony_ci				}
7788c2ecf20Sopenharmony_ci			}
7798c2ecf20Sopenharmony_ci		}
7808c2ecf20Sopenharmony_ci		break;
7818c2ecf20Sopenharmony_ci	case IIO_CHAN_INFO_INT_TIME:
7828c2ecf20Sopenharmony_ci		if (chan->type == IIO_LIGHT && !val && val2 >= 50 &&
7838c2ecf20Sopenharmony_ci		    val2 <= 650 && !(val2 % 50)) {
7848c2ecf20Sopenharmony_ci			chip->als_settings.als_time = val2;
7858c2ecf20Sopenharmony_ci			ret = tsl2583_set_als_time(chip);
7868c2ecf20Sopenharmony_ci		}
7878c2ecf20Sopenharmony_ci		break;
7888c2ecf20Sopenharmony_ci	default:
7898c2ecf20Sopenharmony_ci		break;
7908c2ecf20Sopenharmony_ci	}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (ret < 0)
7958c2ecf20Sopenharmony_ci		return ret;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	ret = tsl2583_set_pm_runtime_busy(chip, false);
7988c2ecf20Sopenharmony_ci	if (ret < 0)
7998c2ecf20Sopenharmony_ci		return ret;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	return ret;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_cistatic const struct iio_info tsl2583_info = {
8058c2ecf20Sopenharmony_ci	.attrs = &tsl2583_attribute_group,
8068c2ecf20Sopenharmony_ci	.read_raw = tsl2583_read_raw,
8078c2ecf20Sopenharmony_ci	.write_raw = tsl2583_write_raw,
8088c2ecf20Sopenharmony_ci};
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic int tsl2583_probe(struct i2c_client *clientp,
8118c2ecf20Sopenharmony_ci			 const struct i2c_device_id *idp)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	int ret;
8148c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip;
8158c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(clientp->adapter,
8188c2ecf20Sopenharmony_ci				     I2C_FUNC_SMBUS_BYTE_DATA)) {
8198c2ecf20Sopenharmony_ci		dev_err(&clientp->dev, "%s: i2c smbus byte data functionality is unsupported\n",
8208c2ecf20Sopenharmony_ci			__func__);
8218c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
8228c2ecf20Sopenharmony_ci	}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	indio_dev = devm_iio_device_alloc(&clientp->dev, sizeof(*chip));
8258c2ecf20Sopenharmony_ci	if (!indio_dev)
8268c2ecf20Sopenharmony_ci		return -ENOMEM;
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci	chip = iio_priv(indio_dev);
8298c2ecf20Sopenharmony_ci	chip->client = clientp;
8308c2ecf20Sopenharmony_ci	i2c_set_clientdata(clientp, indio_dev);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	mutex_init(&chip->als_mutex);
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(clientp,
8358c2ecf20Sopenharmony_ci				       TSL2583_CMD_REG | TSL2583_CHIPID);
8368c2ecf20Sopenharmony_ci	if (ret < 0) {
8378c2ecf20Sopenharmony_ci		dev_err(&clientp->dev,
8388c2ecf20Sopenharmony_ci			"%s: failed to read the chip ID register\n", __func__);
8398c2ecf20Sopenharmony_ci		return ret;
8408c2ecf20Sopenharmony_ci	}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	if ((ret & TSL2583_CHIP_ID_MASK) != TSL2583_CHIP_ID) {
8438c2ecf20Sopenharmony_ci		dev_err(&clientp->dev, "%s: received an unknown chip ID %x\n",
8448c2ecf20Sopenharmony_ci			__func__, ret);
8458c2ecf20Sopenharmony_ci		return -EINVAL;
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	indio_dev->info = &tsl2583_info;
8498c2ecf20Sopenharmony_ci	indio_dev->channels = tsl2583_channels;
8508c2ecf20Sopenharmony_ci	indio_dev->num_channels = ARRAY_SIZE(tsl2583_channels);
8518c2ecf20Sopenharmony_ci	indio_dev->modes = INDIO_DIRECT_MODE;
8528c2ecf20Sopenharmony_ci	indio_dev->name = chip->client->name;
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	pm_runtime_enable(&clientp->dev);
8558c2ecf20Sopenharmony_ci	pm_runtime_set_autosuspend_delay(&clientp->dev,
8568c2ecf20Sopenharmony_ci					 TSL2583_POWER_OFF_DELAY_MS);
8578c2ecf20Sopenharmony_ci	pm_runtime_use_autosuspend(&clientp->dev);
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	ret = iio_device_register(indio_dev);
8608c2ecf20Sopenharmony_ci	if (ret) {
8618c2ecf20Sopenharmony_ci		dev_err(&clientp->dev, "%s: iio registration failed\n",
8628c2ecf20Sopenharmony_ci			__func__);
8638c2ecf20Sopenharmony_ci		return ret;
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	/* Load up the V2 defaults (these are hard coded defaults for now) */
8678c2ecf20Sopenharmony_ci	tsl2583_defaults(chip);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	dev_info(&clientp->dev, "Light sensor found.\n");
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	return 0;
8728c2ecf20Sopenharmony_ci}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_cistatic int tsl2583_remove(struct i2c_client *client)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = i2c_get_clientdata(client);
8778c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci	iio_device_unregister(indio_dev);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	pm_runtime_disable(&client->dev);
8828c2ecf20Sopenharmony_ci	pm_runtime_set_suspended(&client->dev);
8838c2ecf20Sopenharmony_ci	pm_runtime_put_noidle(&client->dev);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	return tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
8868c2ecf20Sopenharmony_ci}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cistatic int __maybe_unused tsl2583_suspend(struct device *dev)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
8918c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
8928c2ecf20Sopenharmony_ci	int ret;
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	ret = tsl2583_set_power_state(chip, TSL2583_CNTL_PWR_OFF);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	return ret;
9018c2ecf20Sopenharmony_ci}
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_cistatic int __maybe_unused tsl2583_resume(struct device *dev)
9048c2ecf20Sopenharmony_ci{
9058c2ecf20Sopenharmony_ci	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
9068c2ecf20Sopenharmony_ci	struct tsl2583_chip *chip = iio_priv(indio_dev);
9078c2ecf20Sopenharmony_ci	int ret;
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	mutex_lock(&chip->als_mutex);
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	ret = tsl2583_chip_init_and_power_on(indio_dev);
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_ci	mutex_unlock(&chip->als_mutex);
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	return ret;
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_cistatic const struct dev_pm_ops tsl2583_pm_ops = {
9198c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
9208c2ecf20Sopenharmony_ci				pm_runtime_force_resume)
9218c2ecf20Sopenharmony_ci	SET_RUNTIME_PM_OPS(tsl2583_suspend, tsl2583_resume, NULL)
9228c2ecf20Sopenharmony_ci};
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_cistatic const struct i2c_device_id tsl2583_idtable[] = {
9258c2ecf20Sopenharmony_ci	{ "tsl2580", 0 },
9268c2ecf20Sopenharmony_ci	{ "tsl2581", 1 },
9278c2ecf20Sopenharmony_ci	{ "tsl2583", 2 },
9288c2ecf20Sopenharmony_ci	{}
9298c2ecf20Sopenharmony_ci};
9308c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tsl2583_idtable);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic const struct of_device_id tsl2583_of_match[] = {
9338c2ecf20Sopenharmony_ci	{ .compatible = "amstaos,tsl2580", },
9348c2ecf20Sopenharmony_ci	{ .compatible = "amstaos,tsl2581", },
9358c2ecf20Sopenharmony_ci	{ .compatible = "amstaos,tsl2583", },
9368c2ecf20Sopenharmony_ci	{ },
9378c2ecf20Sopenharmony_ci};
9388c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tsl2583_of_match);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci/* Driver definition */
9418c2ecf20Sopenharmony_cistatic struct i2c_driver tsl2583_driver = {
9428c2ecf20Sopenharmony_ci	.driver = {
9438c2ecf20Sopenharmony_ci		.name = "tsl2583",
9448c2ecf20Sopenharmony_ci		.pm = &tsl2583_pm_ops,
9458c2ecf20Sopenharmony_ci		.of_match_table = tsl2583_of_match,
9468c2ecf20Sopenharmony_ci	},
9478c2ecf20Sopenharmony_ci	.id_table = tsl2583_idtable,
9488c2ecf20Sopenharmony_ci	.probe = tsl2583_probe,
9498c2ecf20Sopenharmony_ci	.remove = tsl2583_remove,
9508c2ecf20Sopenharmony_ci};
9518c2ecf20Sopenharmony_cimodule_i2c_driver(tsl2583_driver);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ciMODULE_AUTHOR("J. August Brenner <jbrenner@taosinc.com>");
9548c2ecf20Sopenharmony_ciMODULE_AUTHOR("Brian Masney <masneyb@onstation.org>");
9558c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TAOS tsl2583 ambient light sensor driver");
9568c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
957