2 * ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensor driver
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1710fvc-e.pdf
12 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1715fvc-e.pdf
13 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1721fvc-e.pdf
14 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
15 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1751fvi-e.pdf
17 * 7-bit I2C slave addresses:
19 * 0x5C (ADDR pin high)
23 #include <linux/delay.h>
24 #include <linux/i2c.h>
25 #include <linux/iio/iio.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/module.h>
29 #define BH1750_POWER_DOWN 0x00
30 #define BH1750_ONE_TIME_H_RES_MODE 0x20 /* auto-mode for BH1721 */
31 #define BH1750_CHANGE_INT_TIME_H_BIT 0x40
32 #define BH1750_CHANGE_INT_TIME_L_BIT 0x60
40 struct bh1750_chip_info
;
42 struct i2c_client
*client
;
44 const struct bh1750_chip_info
*chip_info
;
48 struct bh1750_chip_info
{
56 * For BH1710/BH1721 all possible integration time values won't fit
57 * into one page so displaying is limited to every second one.
58 * Note, that user can still write proper values which were not
63 u16 int_time_low_mask
;
64 u16 int_time_high_mask
;
67 static const bh1750_chip_info_tbl
[] = {
68 [BH1710
] = { 140, 1022, 300, 400, 250000000, 2, 0x001F, 0x03E0 },
69 [BH1721
] = { 140, 1020, 300, 400, 250000000, 2, 0x0010, 0x03E0 },
70 [BH1750
] = { 31, 254, 69, 1740, 57500000, 1, 0x001F, 0x00E0 },
73 static int bh1750_change_int_time(struct bh1750_data
*data
, int usec
)
78 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
80 if ((usec
% chip_info
->mtreg_to_usec
) != 0)
83 val
= usec
/ chip_info
->mtreg_to_usec
;
84 if (val
< chip_info
->mtreg_min
|| val
> chip_info
->mtreg_max
)
87 ret
= i2c_smbus_write_byte(data
->client
, BH1750_POWER_DOWN
);
91 regval
= (val
& chip_info
->int_time_high_mask
) >> 5;
92 ret
= i2c_smbus_write_byte(data
->client
,
93 BH1750_CHANGE_INT_TIME_H_BIT
| regval
);
97 regval
= val
& chip_info
->int_time_low_mask
;
98 ret
= i2c_smbus_write_byte(data
->client
,
99 BH1750_CHANGE_INT_TIME_L_BIT
| regval
);
108 static int bh1750_read(struct bh1750_data
*data
, int *val
)
112 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
113 unsigned long delay
= chip_info
->mtreg_to_usec
* data
->mtreg
;
116 * BH1721 will enter continuous mode on receiving this command.
117 * Note, that this eliminates need for bh1750_resume().
119 ret
= i2c_smbus_write_byte(data
->client
, BH1750_ONE_TIME_H_RES_MODE
);
123 usleep_range(delay
+ 15000, delay
+ 40000);
125 ret
= i2c_master_recv(data
->client
, (char *)&result
, 2);
129 *val
= be16_to_cpu(result
);
134 static int bh1750_read_raw(struct iio_dev
*indio_dev
,
135 struct iio_chan_spec
const *chan
,
136 int *val
, int *val2
, long mask
)
139 struct bh1750_data
*data
= iio_priv(indio_dev
);
140 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
143 case IIO_CHAN_INFO_RAW
:
144 switch (chan
->type
) {
146 mutex_lock(&data
->lock
);
147 ret
= bh1750_read(data
, val
);
148 mutex_unlock(&data
->lock
);
156 case IIO_CHAN_INFO_SCALE
:
157 tmp
= chip_info
->mtreg_to_scale
/ data
->mtreg
;
158 *val
= tmp
/ 1000000;
159 *val2
= tmp
% 1000000;
160 return IIO_VAL_INT_PLUS_MICRO
;
161 case IIO_CHAN_INFO_INT_TIME
:
163 *val2
= chip_info
->mtreg_to_usec
* data
->mtreg
;
164 return IIO_VAL_INT_PLUS_MICRO
;
170 static int bh1750_write_raw(struct iio_dev
*indio_dev
,
171 struct iio_chan_spec
const *chan
,
172 int val
, int val2
, long mask
)
175 struct bh1750_data
*data
= iio_priv(indio_dev
);
178 case IIO_CHAN_INFO_INT_TIME
:
182 mutex_lock(&data
->lock
);
183 ret
= bh1750_change_int_time(data
, val2
);
184 mutex_unlock(&data
->lock
);
191 static ssize_t
bh1750_show_int_time_available(struct device
*dev
,
192 struct device_attribute
*attr
, char *buf
)
196 struct bh1750_data
*data
= iio_priv(dev_to_iio_dev(dev
));
197 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
199 for (i
= chip_info
->mtreg_min
; i
<= chip_info
->mtreg_max
; i
+= chip_info
->inc
)
200 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06d ",
201 chip_info
->mtreg_to_usec
* i
);
208 static IIO_DEV_ATTR_INT_TIME_AVAIL(bh1750_show_int_time_available
);
210 static struct attribute
*bh1750_attributes
[] = {
211 &iio_dev_attr_integration_time_available
.dev_attr
.attr
,
215 static struct attribute_group bh1750_attribute_group
= {
216 .attrs
= bh1750_attributes
,
219 static const struct iio_info bh1750_info
= {
220 .driver_module
= THIS_MODULE
,
221 .attrs
= &bh1750_attribute_group
,
222 .read_raw
= bh1750_read_raw
,
223 .write_raw
= bh1750_write_raw
,
226 static const struct iio_chan_spec bh1750_channels
[] = {
229 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
230 BIT(IIO_CHAN_INFO_SCALE
) |
231 BIT(IIO_CHAN_INFO_INT_TIME
)
235 static int bh1750_probe(struct i2c_client
*client
,
236 const struct i2c_device_id
*id
)
239 struct bh1750_data
*data
;
240 struct iio_dev
*indio_dev
;
242 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
|
243 I2C_FUNC_SMBUS_WRITE_BYTE
))
246 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
250 data
= iio_priv(indio_dev
);
251 i2c_set_clientdata(client
, indio_dev
);
252 data
->client
= client
;
253 data
->chip_info
= &bh1750_chip_info_tbl
[id
->driver_data
];
255 usec
= data
->chip_info
->mtreg_to_usec
* data
->chip_info
->mtreg_default
;
256 ret
= bh1750_change_int_time(data
, usec
);
260 mutex_init(&data
->lock
);
261 indio_dev
->dev
.parent
= &client
->dev
;
262 indio_dev
->info
= &bh1750_info
;
263 indio_dev
->name
= id
->name
;
264 indio_dev
->channels
= bh1750_channels
;
265 indio_dev
->num_channels
= ARRAY_SIZE(bh1750_channels
);
266 indio_dev
->modes
= INDIO_DIRECT_MODE
;
268 return iio_device_register(indio_dev
);
271 static int bh1750_remove(struct i2c_client
*client
)
273 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
274 struct bh1750_data
*data
= iio_priv(indio_dev
);
276 iio_device_unregister(indio_dev
);
278 mutex_lock(&data
->lock
);
279 i2c_smbus_write_byte(client
, BH1750_POWER_DOWN
);
280 mutex_unlock(&data
->lock
);
285 #ifdef CONFIG_PM_SLEEP
286 static int bh1750_suspend(struct device
*dev
)
289 struct bh1750_data
*data
=
290 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
293 * This is mainly for BH1721 which doesn't enter power down
294 * mode automatically.
296 mutex_lock(&data
->lock
);
297 ret
= i2c_smbus_write_byte(data
->client
, BH1750_POWER_DOWN
);
298 mutex_unlock(&data
->lock
);
303 static SIMPLE_DEV_PM_OPS(bh1750_pm_ops
, bh1750_suspend
, NULL
);
304 #define BH1750_PM_OPS (&bh1750_pm_ops)
306 #define BH1750_PM_OPS NULL
309 static const struct i2c_device_id bh1750_id
[] = {
310 { "bh1710", BH1710
},
311 { "bh1715", BH1750
},
312 { "bh1721", BH1721
},
313 { "bh1750", BH1750
},
314 { "bh1751", BH1750
},
317 MODULE_DEVICE_TABLE(i2c
, bh1750_id
);
319 static struct i2c_driver bh1750_driver
= {
324 .probe
= bh1750_probe
,
325 .remove
= bh1750_remove
,
326 .id_table
= bh1750_id
,
329 module_i2c_driver(bh1750_driver
);
331 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
332 MODULE_DESCRIPTION("ROHM BH1710/BH1715/BH1721/BH1750/BH1751 als driver");
333 MODULE_LICENSE("GPL v2");