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>
28 #define BH1780_REG_CONTROL 0x80
29 #define BH1780_REG_PARTID 0x8A
30 #define BH1780_REG_MANFID 0x8B
31 #define BH1780_REG_DLOW 0x8C
32 #define BH1780_REG_DHIGH 0x8D
34 #define BH1780_REVMASK (0xf)
35 #define BH1780_POWMASK (0x3)
36 #define BH1780_POFF (0x0)
37 #define BH1780_PON (0x3)
39 /* power on settling time in ms */
40 #define BH1780_PON_DELAY 2
43 struct i2c_client
*client
;
45 /* lock for sysfs operations */
49 static int bh1780_write(struct bh1780_data
*ddata
, u8 reg
, u8 val
, char *msg
)
51 int ret
= i2c_smbus_write_byte_data(ddata
->client
, reg
, val
);
53 dev_err(&ddata
->client
->dev
,
54 "i2c_smbus_write_byte_data failed error %d Register (%s)\n",
59 static int bh1780_read(struct bh1780_data
*ddata
, u8 reg
, char *msg
)
61 int ret
= i2c_smbus_read_byte_data(ddata
->client
, reg
);
63 dev_err(&ddata
->client
->dev
,
64 "i2c_smbus_read_byte_data failed error %d Register (%s)\n",
69 static ssize_t
bh1780_show_lux(struct device
*dev
,
70 struct device_attribute
*attr
, char *buf
)
72 struct platform_device
*pdev
= to_platform_device(dev
);
73 struct bh1780_data
*ddata
= platform_get_drvdata(pdev
);
76 lsb
= bh1780_read(ddata
, BH1780_REG_DLOW
, "DLOW");
80 msb
= bh1780_read(ddata
, BH1780_REG_DHIGH
, "DHIGH");
84 return sprintf(buf
, "%d\n", (msb
<< 8) | lsb
);
87 static ssize_t
bh1780_show_power_state(struct device
*dev
,
88 struct device_attribute
*attr
,
91 struct platform_device
*pdev
= to_platform_device(dev
);
92 struct bh1780_data
*ddata
= platform_get_drvdata(pdev
);
95 state
= bh1780_read(ddata
, BH1780_REG_CONTROL
, "CONTROL");
99 return sprintf(buf
, "%d\n", state
& BH1780_POWMASK
);
102 static ssize_t
bh1780_store_power_state(struct device
*dev
,
103 struct device_attribute
*attr
,
104 const char *buf
, size_t count
)
106 struct platform_device
*pdev
= to_platform_device(dev
);
107 struct bh1780_data
*ddata
= platform_get_drvdata(pdev
);
111 error
= kstrtoul(buf
, 0, &val
);
115 if (val
< BH1780_POFF
|| val
> BH1780_PON
)
118 mutex_lock(&ddata
->lock
);
120 error
= bh1780_write(ddata
, BH1780_REG_CONTROL
, val
, "CONTROL");
122 mutex_unlock(&ddata
->lock
);
126 msleep(BH1780_PON_DELAY
);
127 ddata
->power_state
= val
;
128 mutex_unlock(&ddata
->lock
);
133 static DEVICE_ATTR(lux
, S_IRUGO
, bh1780_show_lux
, NULL
);
135 static DEVICE_ATTR(power_state
, S_IWUSR
| S_IRUGO
,
136 bh1780_show_power_state
, bh1780_store_power_state
);
138 static struct attribute
*bh1780_attributes
[] = {
139 &dev_attr_power_state
.attr
,
144 static const struct attribute_group bh1780_attr_group
= {
145 .attrs
= bh1780_attributes
,
148 static int bh1780_probe(struct i2c_client
*client
,
149 const struct i2c_device_id
*id
)
152 struct bh1780_data
*ddata
= NULL
;
153 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
155 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE
)) {
160 ddata
= kzalloc(sizeof(struct bh1780_data
), GFP_KERNEL
);
166 ddata
->client
= client
;
167 i2c_set_clientdata(client
, ddata
);
169 ret
= bh1780_read(ddata
, BH1780_REG_PARTID
, "PART ID");
173 dev_info(&client
->dev
, "Ambient Light Sensor, Rev : %d\n",
174 (ret
& BH1780_REVMASK
));
176 mutex_init(&ddata
->lock
);
178 ret
= sysfs_create_group(&client
->dev
.kobj
, &bh1780_attr_group
);
189 static int bh1780_remove(struct i2c_client
*client
)
191 struct bh1780_data
*ddata
;
193 ddata
= i2c_get_clientdata(client
);
194 sysfs_remove_group(&client
->dev
.kobj
, &bh1780_attr_group
);
200 #ifdef CONFIG_PM_SLEEP
201 static int bh1780_suspend(struct device
*dev
)
203 struct bh1780_data
*ddata
;
205 struct i2c_client
*client
= to_i2c_client(dev
);
207 ddata
= i2c_get_clientdata(client
);
208 state
= bh1780_read(ddata
, BH1780_REG_CONTROL
, "CONTROL");
212 ddata
->power_state
= state
& BH1780_POWMASK
;
214 ret
= bh1780_write(ddata
, BH1780_REG_CONTROL
, BH1780_POFF
,
223 static int bh1780_resume(struct device
*dev
)
225 struct bh1780_data
*ddata
;
227 struct i2c_client
*client
= to_i2c_client(dev
);
229 ddata
= i2c_get_clientdata(client
);
230 state
= ddata
->power_state
;
231 ret
= bh1780_write(ddata
, BH1780_REG_CONTROL
, state
,
239 #endif /* CONFIG_PM_SLEEP */
241 static SIMPLE_DEV_PM_OPS(bh1780_pm
, bh1780_suspend
, bh1780_resume
);
243 static const struct i2c_device_id bh1780_id
[] = {
249 static const struct of_device_id of_bh1780_match
[] = {
250 { .compatible
= "rohm,bh1780gli", },
254 MODULE_DEVICE_TABLE(of
, of_bh1780_match
);
257 static struct i2c_driver bh1780_driver
= {
258 .probe
= bh1780_probe
,
259 .remove
= bh1780_remove
,
260 .id_table
= bh1780_id
,
264 .of_match_table
= of_match_ptr(of_bh1780_match
),
268 module_i2c_driver(bh1780_driver
);
270 MODULE_DESCRIPTION("BH1780GLI Ambient Light Sensor Driver");
271 MODULE_LICENSE("GPL");
272 MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");