2 * isl29020.c - Intersil ALS Driver
4 * Copyright (C) 2008 Intel Corp
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 * Data sheet at: http://www.intersil.com/data/fn/fn6505.pdf
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/i2c.h>
28 #include <linux/err.h>
29 #include <linux/delay.h>
30 #include <linux/sysfs.h>
31 #include <linux/pm_runtime.h>
33 static DEFINE_MUTEX(mutex
);
35 static ssize_t
als_sensing_range_show(struct device
*dev
,
36 struct device_attribute
*attr
, char *buf
)
38 struct i2c_client
*client
= to_i2c_client(dev
);
41 val
= i2c_smbus_read_byte_data(client
, 0x00);
45 return sprintf(buf
, "%d000\n", 1 << (2 * (val
& 3)));
49 static ssize_t
als_lux_input_data_show(struct device
*dev
,
50 struct device_attribute
*attr
, char *buf
)
52 struct i2c_client
*client
= to_i2c_client(dev
);
54 unsigned long int lux
;
57 pm_runtime_get_sync(dev
);
61 temp
= i2c_smbus_read_byte_data(client
, 0x02); /* MSB data */
63 pm_runtime_put_sync(dev
);
68 ret_val
= i2c_smbus_read_byte_data(client
, 0x01); /* LSB data */
72 pm_runtime_put_sync(dev
);
77 val
= i2c_smbus_read_byte_data(client
, 0x00);
78 pm_runtime_put_sync(dev
);
81 lux
= ((((1 << (2 * (val
& 3))))*1000) * ret_val
) / 65536;
82 return sprintf(buf
, "%ld\n", lux
);
85 static ssize_t
als_sensing_range_store(struct device
*dev
,
86 struct device_attribute
*attr
, const char *buf
, size_t count
)
88 struct i2c_client
*client
= to_i2c_client(dev
);
92 ret_val
= kstrtoul(buf
, 10, &val
);
96 if (val
< 1 || val
> 64000)
99 /* Pick the smallest sensor range that will meet our requirements */
102 else if (val
<= 4000)
104 else if (val
<= 16000)
109 ret_val
= i2c_smbus_read_byte_data(client
, 0x00);
113 ret_val
&= 0xFC; /*reset the bit before setting them */
115 ret_val
= i2c_smbus_write_byte_data(client
, 0x00, ret_val
);
122 static void als_set_power_state(struct i2c_client
*client
, int enable
)
126 ret_val
= i2c_smbus_read_byte_data(client
, 0x00);
135 i2c_smbus_write_byte_data(client
, 0x00, ret_val
);
138 static DEVICE_ATTR(lux0_sensor_range
, S_IRUGO
| S_IWUSR
,
139 als_sensing_range_show
, als_sensing_range_store
);
140 static DEVICE_ATTR(lux0_input
, S_IRUGO
, als_lux_input_data_show
, NULL
);
142 static struct attribute
*mid_att_als
[] = {
143 &dev_attr_lux0_sensor_range
.attr
,
144 &dev_attr_lux0_input
.attr
,
148 static const struct attribute_group m_als_gr
= {
153 static int als_set_default_config(struct i2c_client
*client
)
157 retval
= i2c_smbus_write_byte_data(client
, 0x00, 0xc0);
159 dev_err(&client
->dev
, "default write failed.");
165 static int isl29020_probe(struct i2c_client
*client
,
166 const struct i2c_device_id
*id
)
170 res
= als_set_default_config(client
);
174 res
= sysfs_create_group(&client
->dev
.kobj
, &m_als_gr
);
176 dev_err(&client
->dev
, "isl29020: device create file failed\n");
179 dev_info(&client
->dev
, "%s isl29020: ALS chip found\n", client
->name
);
180 als_set_power_state(client
, 0);
181 pm_runtime_enable(&client
->dev
);
185 static int isl29020_remove(struct i2c_client
*client
)
187 sysfs_remove_group(&client
->dev
.kobj
, &m_als_gr
);
191 static const struct i2c_device_id isl29020_id
[] = {
196 MODULE_DEVICE_TABLE(i2c
, isl29020_id
);
200 static int isl29020_runtime_suspend(struct device
*dev
)
202 struct i2c_client
*client
= to_i2c_client(dev
);
203 als_set_power_state(client
, 0);
207 static int isl29020_runtime_resume(struct device
*dev
)
209 struct i2c_client
*client
= to_i2c_client(dev
);
210 als_set_power_state(client
, 1);
214 static const struct dev_pm_ops isl29020_pm_ops
= {
215 .runtime_suspend
= isl29020_runtime_suspend
,
216 .runtime_resume
= isl29020_runtime_resume
,
219 #define ISL29020_PM_OPS (&isl29020_pm_ops)
220 #else /* CONFIG_PM */
221 #define ISL29020_PM_OPS NULL
222 #endif /* CONFIG_PM */
224 static struct i2c_driver isl29020_driver
= {
227 .pm
= ISL29020_PM_OPS
,
229 .probe
= isl29020_probe
,
230 .remove
= isl29020_remove
,
231 .id_table
= isl29020_id
,
234 module_i2c_driver(isl29020_driver
);
236 MODULE_AUTHOR("Kalhan Trisal <kalhan.trisal@intel.com>");
237 MODULE_DESCRIPTION("Intersil isl29020 ALS Driver");
238 MODULE_LICENSE("GPL v2");