3 * ROHM Ambient Light Sensor Driver
5 * Copyright (C) 2010 Texas Instruments
6 * Author: Hemanth V <hemanthv@ti.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/i2c.h>
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/platform_device.h>
24 #include <linux/delay.h>
25 #include <linux/module.h>
27 #define BH1780_REG_CONTROL 0x80
28 #define BH1780_REG_PARTID 0x8A
29 #define BH1780_REG_MANFID 0x8B
30 #define BH1780_REG_DLOW 0x8C
31 #define BH1780_REG_DHIGH 0x8D
33 #define BH1780_REVMASK (0xf)
34 #define BH1780_POWMASK (0x3)
35 #define BH1780_POFF (0x0)
36 #define BH1780_PON (0x3)
38 /* power on settling time in ms */
39 #define BH1780_PON_DELAY 2
42 struct i2c_client
*client
;
44 /* lock for sysfs operations */
48 static int bh1780_write(struct bh1780_data
*ddata
, u8 reg
, u8 val
, char *msg
)
50 int ret
= i2c_smbus_write_byte_data(ddata
->client
, reg
, val
);
52 dev_err(&ddata
->client
->dev
,
53 "i2c_smbus_write_byte_data failed error %d Register (%s)\n",
58 static int bh1780_read(struct bh1780_data
*ddata
, u8 reg
, char *msg
)
60 int ret
= i2c_smbus_read_byte_data(ddata
->client
, reg
);
62 dev_err(&ddata
->client
->dev
,
63 "i2c_smbus_read_byte_data failed error %d Register (%s)\n",
68 static ssize_t
bh1780_show_lux(struct device
*dev
,
69 struct device_attribute
*attr
, char *buf
)
71 struct platform_device
*pdev
= to_platform_device(dev
);
72 struct bh1780_data
*ddata
= platform_get_drvdata(pdev
);
75 lsb
= bh1780_read(ddata
, BH1780_REG_DLOW
, "DLOW");
79 msb
= bh1780_read(ddata
, BH1780_REG_DHIGH
, "DHIGH");
83 return sprintf(buf
, "%d\n", (msb
<< 8) | lsb
);
86 static ssize_t
bh1780_show_power_state(struct device
*dev
,
87 struct device_attribute
*attr
,
90 struct platform_device
*pdev
= to_platform_device(dev
);
91 struct bh1780_data
*ddata
= platform_get_drvdata(pdev
);
94 state
= bh1780_read(ddata
, BH1780_REG_CONTROL
, "CONTROL");
98 return sprintf(buf
, "%d\n", state
& BH1780_POWMASK
);
101 static ssize_t
bh1780_store_power_state(struct device
*dev
,
102 struct device_attribute
*attr
,
103 const char *buf
, size_t count
)
105 struct platform_device
*pdev
= to_platform_device(dev
);
106 struct bh1780_data
*ddata
= platform_get_drvdata(pdev
);
110 error
= kstrtoul(buf
, 0, &val
);
114 if (val
< BH1780_POFF
|| val
> BH1780_PON
)
117 mutex_lock(&ddata
->lock
);
119 error
= bh1780_write(ddata
, BH1780_REG_CONTROL
, val
, "CONTROL");
121 mutex_unlock(&ddata
->lock
);
125 msleep(BH1780_PON_DELAY
);
126 ddata
->power_state
= val
;
127 mutex_unlock(&ddata
->lock
);
132 static DEVICE_ATTR(lux
, S_IRUGO
, bh1780_show_lux
, NULL
);
134 static DEVICE_ATTR(power_state
, S_IWUSR
| S_IRUGO
,
135 bh1780_show_power_state
, bh1780_store_power_state
);
137 static struct attribute
*bh1780_attributes
[] = {
138 &dev_attr_power_state
.attr
,
143 static const struct attribute_group bh1780_attr_group
= {
144 .attrs
= bh1780_attributes
,
147 static int bh1780_probe(struct i2c_client
*client
,
148 const struct i2c_device_id
*id
)
151 struct bh1780_data
*ddata
= NULL
;
152 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
154 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
)) {
159 ddata
= kzalloc(sizeof(struct bh1780_data
), GFP_KERNEL
);
165 ddata
->client
= client
;
166 i2c_set_clientdata(client
, ddata
);
168 ret
= bh1780_read(ddata
, BH1780_REG_PARTID
, "PART ID");
172 dev_info(&client
->dev
, "Ambient Light Sensor, Rev : %d\n",
173 (ret
& BH1780_REVMASK
));
175 mutex_init(&ddata
->lock
);
177 ret
= sysfs_create_group(&client
->dev
.kobj
, &bh1780_attr_group
);
188 static int bh1780_remove(struct i2c_client
*client
)
190 struct bh1780_data
*ddata
;
192 ddata
= i2c_get_clientdata(client
);
193 sysfs_remove_group(&client
->dev
.kobj
, &bh1780_attr_group
);
199 #ifdef CONFIG_PM_SLEEP
200 static int bh1780_suspend(struct device
*dev
)
202 struct bh1780_data
*ddata
;
204 struct i2c_client
*client
= to_i2c_client(dev
);
206 ddata
= i2c_get_clientdata(client
);
207 state
= bh1780_read(ddata
, BH1780_REG_CONTROL
, "CONTROL");
211 ddata
->power_state
= state
& BH1780_POWMASK
;
213 ret
= bh1780_write(ddata
, BH1780_REG_CONTROL
, BH1780_POFF
,
222 static int bh1780_resume(struct device
*dev
)
224 struct bh1780_data
*ddata
;
226 struct i2c_client
*client
= to_i2c_client(dev
);
228 ddata
= i2c_get_clientdata(client
);
229 state
= ddata
->power_state
;
230 ret
= bh1780_write(ddata
, BH1780_REG_CONTROL
, state
,
238 #endif /* CONFIG_PM_SLEEP */
240 static SIMPLE_DEV_PM_OPS(bh1780_pm
, bh1780_suspend
, bh1780_resume
);
242 static const struct i2c_device_id bh1780_id
[] = {
247 static struct i2c_driver bh1780_driver
= {
248 .probe
= bh1780_probe
,
249 .remove
= bh1780_remove
,
250 .id_table
= bh1780_id
,
257 module_i2c_driver(bh1780_driver
);
259 MODULE_DESCRIPTION("BH1780GLI Ambient Light Sensor Driver");
260 MODULE_LICENSE("GPL");
261 MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");