18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  tsl2550.c - Linux kernel modules for ambient light sensor
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2007 Rodolfo Giometti <giometti@linux.it>
68c2ecf20Sopenharmony_ci *  Copyright (C) 2007 Eurotech S.p.A. <info@eurotech.it>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/mutex.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define TSL2550_DRV_NAME	"tsl2550"
158c2ecf20Sopenharmony_ci#define DRIVER_VERSION		"1.2"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/*
188c2ecf20Sopenharmony_ci * Defines
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define TSL2550_POWER_DOWN		0x00
228c2ecf20Sopenharmony_ci#define TSL2550_POWER_UP		0x03
238c2ecf20Sopenharmony_ci#define TSL2550_STANDARD_RANGE		0x18
248c2ecf20Sopenharmony_ci#define TSL2550_EXTENDED_RANGE		0x1d
258c2ecf20Sopenharmony_ci#define TSL2550_READ_ADC0		0x43
268c2ecf20Sopenharmony_ci#define TSL2550_READ_ADC1		0x83
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * Structs
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct tsl2550_data {
338c2ecf20Sopenharmony_ci	struct i2c_client *client;
348c2ecf20Sopenharmony_ci	struct mutex update_lock;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	unsigned int power_state:1;
378c2ecf20Sopenharmony_ci	unsigned int operating_mode:1;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Global data
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic const u8 TSL2550_MODE_RANGE[2] = {
458c2ecf20Sopenharmony_ci	TSL2550_STANDARD_RANGE, TSL2550_EXTENDED_RANGE,
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * Management functions
508c2ecf20Sopenharmony_ci */
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int tsl2550_set_operating_mode(struct i2c_client *client, int mode)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci	int ret = i2c_smbus_write_byte(client, TSL2550_MODE_RANGE[mode]);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	data->operating_mode = mode;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return ret;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int tsl2550_set_power_state(struct i2c_client *client, int state)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
668c2ecf20Sopenharmony_ci	int ret;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (state == 0)
698c2ecf20Sopenharmony_ci		ret = i2c_smbus_write_byte(client, TSL2550_POWER_DOWN);
708c2ecf20Sopenharmony_ci	else {
718c2ecf20Sopenharmony_ci		ret = i2c_smbus_write_byte(client, TSL2550_POWER_UP);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		/* On power up we should reset operating mode also... */
748c2ecf20Sopenharmony_ci		tsl2550_set_operating_mode(client, data->operating_mode);
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	data->power_state = state;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	return ret;
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic int tsl2550_get_adc_value(struct i2c_client *client, u8 cmd)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	int ret;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	ret = i2c_smbus_read_byte_data(client, cmd);
878c2ecf20Sopenharmony_ci	if (ret < 0)
888c2ecf20Sopenharmony_ci		return ret;
898c2ecf20Sopenharmony_ci	if (!(ret & 0x80))
908c2ecf20Sopenharmony_ci		return -EAGAIN;
918c2ecf20Sopenharmony_ci	return ret & 0x7f;	/* remove the "valid" bit */
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/*
958c2ecf20Sopenharmony_ci * LUX calculation
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define	TSL2550_MAX_LUX		1846
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const u8 ratio_lut[] = {
1018c2ecf20Sopenharmony_ci	100, 100, 100, 100, 100, 100, 100, 100,
1028c2ecf20Sopenharmony_ci	100, 100, 100, 100, 100, 100, 99, 99,
1038c2ecf20Sopenharmony_ci	99, 99, 99, 99, 99, 99, 99, 99,
1048c2ecf20Sopenharmony_ci	99, 99, 99, 98, 98, 98, 98, 98,
1058c2ecf20Sopenharmony_ci	98, 98, 97, 97, 97, 97, 97, 96,
1068c2ecf20Sopenharmony_ci	96, 96, 96, 95, 95, 95, 94, 94,
1078c2ecf20Sopenharmony_ci	93, 93, 93, 92, 92, 91, 91, 90,
1088c2ecf20Sopenharmony_ci	89, 89, 88, 87, 87, 86, 85, 84,
1098c2ecf20Sopenharmony_ci	83, 82, 81, 80, 79, 78, 77, 75,
1108c2ecf20Sopenharmony_ci	74, 73, 71, 69, 68, 66, 64, 62,
1118c2ecf20Sopenharmony_ci	60, 58, 56, 54, 52, 49, 47, 44,
1128c2ecf20Sopenharmony_ci	42, 41, 40, 40, 39, 39, 38, 38,
1138c2ecf20Sopenharmony_ci	37, 37, 37, 36, 36, 36, 35, 35,
1148c2ecf20Sopenharmony_ci	35, 35, 34, 34, 34, 34, 33, 33,
1158c2ecf20Sopenharmony_ci	33, 33, 32, 32, 32, 32, 32, 31,
1168c2ecf20Sopenharmony_ci	31, 31, 31, 31, 30, 30, 30, 30,
1178c2ecf20Sopenharmony_ci	30,
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const u16 count_lut[] = {
1218c2ecf20Sopenharmony_ci	0, 1, 2, 3, 4, 5, 6, 7,
1228c2ecf20Sopenharmony_ci	8, 9, 10, 11, 12, 13, 14, 15,
1238c2ecf20Sopenharmony_ci	16, 18, 20, 22, 24, 26, 28, 30,
1248c2ecf20Sopenharmony_ci	32, 34, 36, 38, 40, 42, 44, 46,
1258c2ecf20Sopenharmony_ci	49, 53, 57, 61, 65, 69, 73, 77,
1268c2ecf20Sopenharmony_ci	81, 85, 89, 93, 97, 101, 105, 109,
1278c2ecf20Sopenharmony_ci	115, 123, 131, 139, 147, 155, 163, 171,
1288c2ecf20Sopenharmony_ci	179, 187, 195, 203, 211, 219, 227, 235,
1298c2ecf20Sopenharmony_ci	247, 263, 279, 295, 311, 327, 343, 359,
1308c2ecf20Sopenharmony_ci	375, 391, 407, 423, 439, 455, 471, 487,
1318c2ecf20Sopenharmony_ci	511, 543, 575, 607, 639, 671, 703, 735,
1328c2ecf20Sopenharmony_ci	767, 799, 831, 863, 895, 927, 959, 991,
1338c2ecf20Sopenharmony_ci	1039, 1103, 1167, 1231, 1295, 1359, 1423, 1487,
1348c2ecf20Sopenharmony_ci	1551, 1615, 1679, 1743, 1807, 1871, 1935, 1999,
1358c2ecf20Sopenharmony_ci	2095, 2223, 2351, 2479, 2607, 2735, 2863, 2991,
1368c2ecf20Sopenharmony_ci	3119, 3247, 3375, 3503, 3631, 3759, 3887, 4015,
1378c2ecf20Sopenharmony_ci};
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/*
1408c2ecf20Sopenharmony_ci * This function is described into Taos TSL2550 Designer's Notebook
1418c2ecf20Sopenharmony_ci * pages 2, 3.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_cistatic int tsl2550_calculate_lux(u8 ch0, u8 ch1)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	unsigned int lux;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* Look up count from channel values */
1488c2ecf20Sopenharmony_ci	u16 c0 = count_lut[ch0];
1498c2ecf20Sopenharmony_ci	u16 c1 = count_lut[ch1];
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* Avoid division by 0 and count 1 cannot be greater than count 0 */
1528c2ecf20Sopenharmony_ci	if (c1 <= c0)
1538c2ecf20Sopenharmony_ci		if (c0) {
1548c2ecf20Sopenharmony_ci			/*
1558c2ecf20Sopenharmony_ci			 * Calculate ratio.
1568c2ecf20Sopenharmony_ci			 * Note: the "128" is a scaling factor
1578c2ecf20Sopenharmony_ci			 */
1588c2ecf20Sopenharmony_ci			u8 r = c1 * 128 / c0;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci			/* Calculate LUX */
1618c2ecf20Sopenharmony_ci			lux = ((c0 - c1) * ratio_lut[r]) / 256;
1628c2ecf20Sopenharmony_ci		} else
1638c2ecf20Sopenharmony_ci			lux = 0;
1648c2ecf20Sopenharmony_ci	else
1658c2ecf20Sopenharmony_ci		return 0;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	/* LUX range check */
1688c2ecf20Sopenharmony_ci	return lux > TSL2550_MAX_LUX ? TSL2550_MAX_LUX : lux;
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci/*
1728c2ecf20Sopenharmony_ci * SysFS support
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic ssize_t tsl2550_show_power_state(struct device *dev,
1768c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->power_state);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic ssize_t tsl2550_store_power_state(struct device *dev,
1848c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t count)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
1878c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
1888c2ecf20Sopenharmony_ci	unsigned long val = simple_strtoul(buf, NULL, 10);
1898c2ecf20Sopenharmony_ci	int ret;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (val > 1)
1928c2ecf20Sopenharmony_ci		return -EINVAL;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
1958c2ecf20Sopenharmony_ci	ret = tsl2550_set_power_state(client, val);
1968c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (ret < 0)
1998c2ecf20Sopenharmony_ci		return ret;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	return count;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic DEVICE_ATTR(power_state, S_IWUSR | S_IRUGO,
2058c2ecf20Sopenharmony_ci		   tsl2550_show_power_state, tsl2550_store_power_state);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic ssize_t tsl2550_show_operating_mode(struct device *dev,
2088c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(to_i2c_client(dev));
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return sprintf(buf, "%u\n", data->operating_mode);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic ssize_t tsl2550_store_operating_mode(struct device *dev,
2168c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t count)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2198c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
2208c2ecf20Sopenharmony_ci	unsigned long val = simple_strtoul(buf, NULL, 10);
2218c2ecf20Sopenharmony_ci	int ret;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	if (val > 1)
2248c2ecf20Sopenharmony_ci		return -EINVAL;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	if (data->power_state == 0)
2278c2ecf20Sopenharmony_ci		return -EBUSY;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2308c2ecf20Sopenharmony_ci	ret = tsl2550_set_operating_mode(client, val);
2318c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (ret < 0)
2348c2ecf20Sopenharmony_ci		return ret;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	return count;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic DEVICE_ATTR(operating_mode, S_IWUSR | S_IRUGO,
2408c2ecf20Sopenharmony_ci		   tsl2550_show_operating_mode, tsl2550_store_operating_mode);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic ssize_t __tsl2550_show_lux(struct i2c_client *client, char *buf)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
2458c2ecf20Sopenharmony_ci	u8 ch0, ch1;
2468c2ecf20Sopenharmony_ci	int ret;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC0);
2498c2ecf20Sopenharmony_ci	if (ret < 0)
2508c2ecf20Sopenharmony_ci		return ret;
2518c2ecf20Sopenharmony_ci	ch0 = ret;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	ret = tsl2550_get_adc_value(client, TSL2550_READ_ADC1);
2548c2ecf20Sopenharmony_ci	if (ret < 0)
2558c2ecf20Sopenharmony_ci		return ret;
2568c2ecf20Sopenharmony_ci	ch1 = ret;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* Do the job */
2598c2ecf20Sopenharmony_ci	ret = tsl2550_calculate_lux(ch0, ch1);
2608c2ecf20Sopenharmony_ci	if (ret < 0)
2618c2ecf20Sopenharmony_ci		return ret;
2628c2ecf20Sopenharmony_ci	if (data->operating_mode == 1)
2638c2ecf20Sopenharmony_ci		ret *= 5;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", ret);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic ssize_t tsl2550_show_lux1_input(struct device *dev,
2698c2ecf20Sopenharmony_ci			struct device_attribute *attr, char *buf)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
2728c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
2738c2ecf20Sopenharmony_ci	int ret;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* No LUX data if not operational */
2768c2ecf20Sopenharmony_ci	if (!data->power_state)
2778c2ecf20Sopenharmony_ci		return -EBUSY;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	mutex_lock(&data->update_lock);
2808c2ecf20Sopenharmony_ci	ret = __tsl2550_show_lux(client, buf);
2818c2ecf20Sopenharmony_ci	mutex_unlock(&data->update_lock);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return ret;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic DEVICE_ATTR(lux1_input, S_IRUGO,
2878c2ecf20Sopenharmony_ci		   tsl2550_show_lux1_input, NULL);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic struct attribute *tsl2550_attributes[] = {
2908c2ecf20Sopenharmony_ci	&dev_attr_power_state.attr,
2918c2ecf20Sopenharmony_ci	&dev_attr_operating_mode.attr,
2928c2ecf20Sopenharmony_ci	&dev_attr_lux1_input.attr,
2938c2ecf20Sopenharmony_ci	NULL
2948c2ecf20Sopenharmony_ci};
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic const struct attribute_group tsl2550_attr_group = {
2978c2ecf20Sopenharmony_ci	.attrs = tsl2550_attributes,
2988c2ecf20Sopenharmony_ci};
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci/*
3018c2ecf20Sopenharmony_ci * Initialization function
3028c2ecf20Sopenharmony_ci */
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic int tsl2550_init_client(struct i2c_client *client)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct tsl2550_data *data = i2c_get_clientdata(client);
3078c2ecf20Sopenharmony_ci	int err;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/*
3108c2ecf20Sopenharmony_ci	 * Probe the chip. To do so we try to power up the device and then to
3118c2ecf20Sopenharmony_ci	 * read back the 0x03 code
3128c2ecf20Sopenharmony_ci	 */
3138c2ecf20Sopenharmony_ci	err = i2c_smbus_read_byte_data(client, TSL2550_POWER_UP);
3148c2ecf20Sopenharmony_ci	if (err < 0)
3158c2ecf20Sopenharmony_ci		return err;
3168c2ecf20Sopenharmony_ci	if (err != TSL2550_POWER_UP)
3178c2ecf20Sopenharmony_ci		return -ENODEV;
3188c2ecf20Sopenharmony_ci	data->power_state = 1;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* Set the default operating mode */
3218c2ecf20Sopenharmony_ci	err = i2c_smbus_write_byte(client,
3228c2ecf20Sopenharmony_ci				   TSL2550_MODE_RANGE[data->operating_mode]);
3238c2ecf20Sopenharmony_ci	if (err < 0)
3248c2ecf20Sopenharmony_ci		return err;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	return 0;
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci/*
3308c2ecf20Sopenharmony_ci * I2C init/probing/exit functions
3318c2ecf20Sopenharmony_ci */
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic struct i2c_driver tsl2550_driver;
3348c2ecf20Sopenharmony_cistatic int tsl2550_probe(struct i2c_client *client,
3358c2ecf20Sopenharmony_ci				   const struct i2c_device_id *id)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	struct i2c_adapter *adapter = client->adapter;
3388c2ecf20Sopenharmony_ci	struct tsl2550_data *data;
3398c2ecf20Sopenharmony_ci	int *opmode, err = 0;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE
3428c2ecf20Sopenharmony_ci					    | I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
3438c2ecf20Sopenharmony_ci		err = -EIO;
3448c2ecf20Sopenharmony_ci		goto exit;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	data = kzalloc(sizeof(struct tsl2550_data), GFP_KERNEL);
3488c2ecf20Sopenharmony_ci	if (!data) {
3498c2ecf20Sopenharmony_ci		err = -ENOMEM;
3508c2ecf20Sopenharmony_ci		goto exit;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci	data->client = client;
3538c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, data);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* Check platform data */
3568c2ecf20Sopenharmony_ci	opmode = client->dev.platform_data;
3578c2ecf20Sopenharmony_ci	if (opmode) {
3588c2ecf20Sopenharmony_ci		if (*opmode < 0 || *opmode > 1) {
3598c2ecf20Sopenharmony_ci			dev_err(&client->dev, "invalid operating_mode (%d)\n",
3608c2ecf20Sopenharmony_ci					*opmode);
3618c2ecf20Sopenharmony_ci			err = -EINVAL;
3628c2ecf20Sopenharmony_ci			goto exit_kfree;
3638c2ecf20Sopenharmony_ci		}
3648c2ecf20Sopenharmony_ci		data->operating_mode = *opmode;
3658c2ecf20Sopenharmony_ci	} else
3668c2ecf20Sopenharmony_ci		data->operating_mode = 0;	/* default mode is standard */
3678c2ecf20Sopenharmony_ci	dev_info(&client->dev, "%s operating mode\n",
3688c2ecf20Sopenharmony_ci			data->operating_mode ? "extended" : "standard");
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	mutex_init(&data->update_lock);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* Initialize the TSL2550 chip */
3738c2ecf20Sopenharmony_ci	err = tsl2550_init_client(client);
3748c2ecf20Sopenharmony_ci	if (err)
3758c2ecf20Sopenharmony_ci		goto exit_kfree;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	/* Register sysfs hooks */
3788c2ecf20Sopenharmony_ci	err = sysfs_create_group(&client->dev.kobj, &tsl2550_attr_group);
3798c2ecf20Sopenharmony_ci	if (err)
3808c2ecf20Sopenharmony_ci		goto exit_kfree;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	return 0;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ciexit_kfree:
3878c2ecf20Sopenharmony_ci	kfree(data);
3888c2ecf20Sopenharmony_ciexit:
3898c2ecf20Sopenharmony_ci	return err;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic int tsl2550_remove(struct i2c_client *client)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	sysfs_remove_group(&client->dev.kobj, &tsl2550_attr_group);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* Power down the device */
3978c2ecf20Sopenharmony_ci	tsl2550_set_power_state(client, 0);
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	kfree(i2c_get_clientdata(client));
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	return 0;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_cistatic int tsl2550_suspend(struct device *dev)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	return tsl2550_set_power_state(to_i2c_client(dev), 0);
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic int tsl2550_resume(struct device *dev)
4128c2ecf20Sopenharmony_ci{
4138c2ecf20Sopenharmony_ci	return tsl2550_set_power_state(to_i2c_client(dev), 1);
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(tsl2550_pm_ops, tsl2550_suspend, tsl2550_resume);
4178c2ecf20Sopenharmony_ci#define TSL2550_PM_OPS (&tsl2550_pm_ops)
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci#else
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci#define TSL2550_PM_OPS NULL
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci#endif /* CONFIG_PM_SLEEP */
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_cistatic const struct i2c_device_id tsl2550_id[] = {
4268c2ecf20Sopenharmony_ci	{ "tsl2550", 0 },
4278c2ecf20Sopenharmony_ci	{ }
4288c2ecf20Sopenharmony_ci};
4298c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, tsl2550_id);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_cistatic const struct of_device_id tsl2550_of_match[] = {
4328c2ecf20Sopenharmony_ci	{ .compatible = "taos,tsl2550" },
4338c2ecf20Sopenharmony_ci	{ }
4348c2ecf20Sopenharmony_ci};
4358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, tsl2550_of_match);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic struct i2c_driver tsl2550_driver = {
4388c2ecf20Sopenharmony_ci	.driver = {
4398c2ecf20Sopenharmony_ci		.name	= TSL2550_DRV_NAME,
4408c2ecf20Sopenharmony_ci		.of_match_table = tsl2550_of_match,
4418c2ecf20Sopenharmony_ci		.pm	= TSL2550_PM_OPS,
4428c2ecf20Sopenharmony_ci	},
4438c2ecf20Sopenharmony_ci	.probe	= tsl2550_probe,
4448c2ecf20Sopenharmony_ci	.remove	= tsl2550_remove,
4458c2ecf20Sopenharmony_ci	.id_table = tsl2550_id,
4468c2ecf20Sopenharmony_ci};
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_cimodule_i2c_driver(tsl2550_driver);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
4518c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TSL2550 ambient light sensor driver");
4528c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4538c2ecf20Sopenharmony_ciMODULE_VERSION(DRIVER_VERSION);
454