1 // SPDX-License-Identifier: GPL-2.0-only
3 * isl29020.c - Intersil ALS Driver
5 * Copyright (C) 2008 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * Data sheet at: http://www.intersil.com/data/fn/fn6505.pdf
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/err.h>
18 #include <linux/delay.h>
19 #include <linux/sysfs.h>
20 #include <linux/pm_runtime.h>
22 static DEFINE_MUTEX(mutex
);
24 static ssize_t
als_sensing_range_show(struct device
*dev
,
25 struct device_attribute
*attr
, char *buf
)
27 struct i2c_client
*client
= to_i2c_client(dev
);
30 val
= i2c_smbus_read_byte_data(client
, 0x00);
34 return sprintf(buf
, "%d000\n", 1 << (2 * (val
& 3)));
38 static ssize_t
als_lux_input_data_show(struct device
*dev
,
39 struct device_attribute
*attr
, char *buf
)
41 struct i2c_client
*client
= to_i2c_client(dev
);
43 unsigned long int lux
;
46 pm_runtime_get_sync(dev
);
50 temp
= i2c_smbus_read_byte_data(client
, 0x02); /* MSB data */
52 pm_runtime_put_sync(dev
);
57 ret_val
= i2c_smbus_read_byte_data(client
, 0x01); /* LSB data */
61 pm_runtime_put_sync(dev
);
66 val
= i2c_smbus_read_byte_data(client
, 0x00);
67 pm_runtime_put_sync(dev
);
70 lux
= ((((1 << (2 * (val
& 3))))*1000) * ret_val
) / 65536;
71 return sprintf(buf
, "%lu\n", lux
);
74 static ssize_t
als_sensing_range_store(struct device
*dev
,
75 struct device_attribute
*attr
, const char *buf
, size_t count
)
77 struct i2c_client
*client
= to_i2c_client(dev
);
81 ret_val
= kstrtoul(buf
, 10, &val
);
85 if (val
< 1 || val
> 64000)
88 /* Pick the smallest sensor range that will meet our requirements */
93 else if (val
<= 16000)
98 ret_val
= i2c_smbus_read_byte_data(client
, 0x00);
102 ret_val
&= 0xFC; /*reset the bit before setting them */
104 ret_val
= i2c_smbus_write_byte_data(client
, 0x00, ret_val
);
111 static void als_set_power_state(struct i2c_client
*client
, int enable
)
115 ret_val
= i2c_smbus_read_byte_data(client
, 0x00);
124 i2c_smbus_write_byte_data(client
, 0x00, ret_val
);
127 static DEVICE_ATTR(lux0_sensor_range
, S_IRUGO
| S_IWUSR
,
128 als_sensing_range_show
, als_sensing_range_store
);
129 static DEVICE_ATTR(lux0_input
, S_IRUGO
, als_lux_input_data_show
, NULL
);
131 static struct attribute
*mid_att_als
[] = {
132 &dev_attr_lux0_sensor_range
.attr
,
133 &dev_attr_lux0_input
.attr
,
137 static const struct attribute_group m_als_gr
= {
142 static int als_set_default_config(struct i2c_client
*client
)
146 retval
= i2c_smbus_write_byte_data(client
, 0x00, 0xc0);
148 dev_err(&client
->dev
, "default write failed.");
154 static int isl29020_probe(struct i2c_client
*client
)
158 res
= als_set_default_config(client
);
162 res
= sysfs_create_group(&client
->dev
.kobj
, &m_als_gr
);
164 dev_err(&client
->dev
, "isl29020: device create file failed\n");
167 dev_info(&client
->dev
, "%s isl29020: ALS chip found\n", client
->name
);
168 als_set_power_state(client
, 0);
169 pm_runtime_enable(&client
->dev
);
173 static void isl29020_remove(struct i2c_client
*client
)
175 pm_runtime_disable(&client
->dev
);
176 sysfs_remove_group(&client
->dev
.kobj
, &m_als_gr
);
179 static const struct i2c_device_id isl29020_id
[] = {
184 MODULE_DEVICE_TABLE(i2c
, isl29020_id
);
188 static int isl29020_runtime_suspend(struct device
*dev
)
190 struct i2c_client
*client
= to_i2c_client(dev
);
191 als_set_power_state(client
, 0);
195 static int isl29020_runtime_resume(struct device
*dev
)
197 struct i2c_client
*client
= to_i2c_client(dev
);
198 als_set_power_state(client
, 1);
202 static const struct dev_pm_ops isl29020_pm_ops
= {
203 .runtime_suspend
= isl29020_runtime_suspend
,
204 .runtime_resume
= isl29020_runtime_resume
,
207 #define ISL29020_PM_OPS (&isl29020_pm_ops)
208 #else /* CONFIG_PM */
209 #define ISL29020_PM_OPS NULL
210 #endif /* CONFIG_PM */
212 static struct i2c_driver isl29020_driver
= {
215 .pm
= ISL29020_PM_OPS
,
217 .probe
= isl29020_probe
,
218 .remove
= isl29020_remove
,
219 .id_table
= isl29020_id
,
222 module_i2c_driver(isl29020_driver
);
224 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com>");
225 MODULE_DESCRIPTION("Intersil isl29020 ALS Driver");
226 MODULE_LICENSE("GPL v2");