1 // SPDX-License-Identifier: GPL-2.0
3 * ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensor driver
5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
8 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1710fvc-e.pdf
9 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1715fvc-e.pdf
10 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1721fvc-e.pdf
11 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1750fvi-e.pdf
12 * http://rohmfs.rohm.com/en/products/databook/datasheet/ic/sensor/light/bh1751fvi-e.pdf
14 * 7-bit I2C slave addresses:
16 * 0x5C (ADDR pin high)
20 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
24 #include <linux/module.h>
26 #define BH1750_POWER_DOWN 0x00
27 #define BH1750_ONE_TIME_H_RES_MODE 0x20 /* auto-mode for BH1721 */
28 #define BH1750_CHANGE_INT_TIME_H_BIT 0x40
29 #define BH1750_CHANGE_INT_TIME_L_BIT 0x60
37 struct bh1750_chip_info
;
39 struct i2c_client
*client
;
41 const struct bh1750_chip_info
*chip_info
;
45 struct bh1750_chip_info
{
53 * For BH1710/BH1721 all possible integration time values won't fit
54 * into one page so displaying is limited to every second one.
55 * Note, that user can still write proper values which were not
60 u16 int_time_low_mask
;
61 u16 int_time_high_mask
;
64 static const struct bh1750_chip_info bh1750_chip_info_tbl
[] = {
65 [BH1710
] = { 140, 1022, 300, 400, 250000000, 2, 0x001F, 0x03E0 },
66 [BH1721
] = { 140, 1020, 300, 400, 250000000, 2, 0x0010, 0x03E0 },
67 [BH1750
] = { 31, 254, 69, 1740, 57500000, 1, 0x001F, 0x00E0 },
70 static int bh1750_change_int_time(struct bh1750_data
*data
, int usec
)
75 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
77 if ((usec
% chip_info
->mtreg_to_usec
) != 0)
80 val
= usec
/ chip_info
->mtreg_to_usec
;
81 if (val
< chip_info
->mtreg_min
|| val
> chip_info
->mtreg_max
)
84 ret
= i2c_smbus_write_byte(data
->client
, BH1750_POWER_DOWN
);
88 regval
= (val
& chip_info
->int_time_high_mask
) >> 5;
89 ret
= i2c_smbus_write_byte(data
->client
,
90 BH1750_CHANGE_INT_TIME_H_BIT
| regval
);
94 regval
= val
& chip_info
->int_time_low_mask
;
95 ret
= i2c_smbus_write_byte(data
->client
,
96 BH1750_CHANGE_INT_TIME_L_BIT
| regval
);
105 static int bh1750_read(struct bh1750_data
*data
, int *val
)
109 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
110 unsigned long delay
= chip_info
->mtreg_to_usec
* data
->mtreg
;
113 * BH1721 will enter continuous mode on receiving this command.
114 * Note, that this eliminates need for bh1750_resume().
116 ret
= i2c_smbus_write_byte(data
->client
, BH1750_ONE_TIME_H_RES_MODE
);
120 usleep_range(delay
+ 15000, delay
+ 40000);
122 ret
= i2c_master_recv(data
->client
, (char *)&result
, 2);
126 *val
= be16_to_cpu(result
);
131 static int bh1750_read_raw(struct iio_dev
*indio_dev
,
132 struct iio_chan_spec
const *chan
,
133 int *val
, int *val2
, long mask
)
136 struct bh1750_data
*data
= iio_priv(indio_dev
);
137 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
140 case IIO_CHAN_INFO_RAW
:
141 switch (chan
->type
) {
143 mutex_lock(&data
->lock
);
144 ret
= bh1750_read(data
, val
);
145 mutex_unlock(&data
->lock
);
153 case IIO_CHAN_INFO_SCALE
:
154 tmp
= chip_info
->mtreg_to_scale
/ data
->mtreg
;
155 *val
= tmp
/ 1000000;
156 *val2
= tmp
% 1000000;
157 return IIO_VAL_INT_PLUS_MICRO
;
158 case IIO_CHAN_INFO_INT_TIME
:
160 *val2
= chip_info
->mtreg_to_usec
* data
->mtreg
;
161 return IIO_VAL_INT_PLUS_MICRO
;
167 static int bh1750_write_raw(struct iio_dev
*indio_dev
,
168 struct iio_chan_spec
const *chan
,
169 int val
, int val2
, long mask
)
172 struct bh1750_data
*data
= iio_priv(indio_dev
);
175 case IIO_CHAN_INFO_INT_TIME
:
179 mutex_lock(&data
->lock
);
180 ret
= bh1750_change_int_time(data
, val2
);
181 mutex_unlock(&data
->lock
);
188 static ssize_t
bh1750_show_int_time_available(struct device
*dev
,
189 struct device_attribute
*attr
, char *buf
)
193 struct bh1750_data
*data
= iio_priv(dev_to_iio_dev(dev
));
194 const struct bh1750_chip_info
*chip_info
= data
->chip_info
;
196 for (i
= chip_info
->mtreg_min
; i
<= chip_info
->mtreg_max
; i
+= chip_info
->inc
)
197 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06d ",
198 chip_info
->mtreg_to_usec
* i
);
205 static IIO_DEV_ATTR_INT_TIME_AVAIL(bh1750_show_int_time_available
);
207 static struct attribute
*bh1750_attributes
[] = {
208 &iio_dev_attr_integration_time_available
.dev_attr
.attr
,
212 static const struct attribute_group bh1750_attribute_group
= {
213 .attrs
= bh1750_attributes
,
216 static const struct iio_info bh1750_info
= {
217 .attrs
= &bh1750_attribute_group
,
218 .read_raw
= bh1750_read_raw
,
219 .write_raw
= bh1750_write_raw
,
222 static const struct iio_chan_spec bh1750_channels
[] = {
225 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
226 BIT(IIO_CHAN_INFO_SCALE
) |
227 BIT(IIO_CHAN_INFO_INT_TIME
)
231 static int bh1750_probe(struct i2c_client
*client
,
232 const struct i2c_device_id
*id
)
235 struct bh1750_data
*data
;
236 struct iio_dev
*indio_dev
;
238 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
|
239 I2C_FUNC_SMBUS_WRITE_BYTE
))
242 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
246 data
= iio_priv(indio_dev
);
247 i2c_set_clientdata(client
, indio_dev
);
248 data
->client
= client
;
249 data
->chip_info
= &bh1750_chip_info_tbl
[id
->driver_data
];
251 usec
= data
->chip_info
->mtreg_to_usec
* data
->chip_info
->mtreg_default
;
252 ret
= bh1750_change_int_time(data
, usec
);
256 mutex_init(&data
->lock
);
257 indio_dev
->info
= &bh1750_info
;
258 indio_dev
->name
= id
->name
;
259 indio_dev
->channels
= bh1750_channels
;
260 indio_dev
->num_channels
= ARRAY_SIZE(bh1750_channels
);
261 indio_dev
->modes
= INDIO_DIRECT_MODE
;
263 return iio_device_register(indio_dev
);
266 static int bh1750_remove(struct i2c_client
*client
)
268 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
269 struct bh1750_data
*data
= iio_priv(indio_dev
);
271 iio_device_unregister(indio_dev
);
273 mutex_lock(&data
->lock
);
274 i2c_smbus_write_byte(client
, BH1750_POWER_DOWN
);
275 mutex_unlock(&data
->lock
);
280 static int __maybe_unused
bh1750_suspend(struct device
*dev
)
283 struct bh1750_data
*data
=
284 iio_priv(i2c_get_clientdata(to_i2c_client(dev
)));
287 * This is mainly for BH1721 which doesn't enter power down
288 * mode automatically.
290 mutex_lock(&data
->lock
);
291 ret
= i2c_smbus_write_byte(data
->client
, BH1750_POWER_DOWN
);
292 mutex_unlock(&data
->lock
);
297 static SIMPLE_DEV_PM_OPS(bh1750_pm_ops
, bh1750_suspend
, NULL
);
299 static const struct i2c_device_id bh1750_id
[] = {
300 { "bh1710", BH1710
},
301 { "bh1715", BH1750
},
302 { "bh1721", BH1721
},
303 { "bh1750", BH1750
},
304 { "bh1751", BH1750
},
307 MODULE_DEVICE_TABLE(i2c
, bh1750_id
);
309 static const struct of_device_id bh1750_of_match
[] = {
310 { .compatible
= "rohm,bh1710", },
311 { .compatible
= "rohm,bh1715", },
312 { .compatible
= "rohm,bh1721", },
313 { .compatible
= "rohm,bh1750", },
314 { .compatible
= "rohm,bh1751", },
317 MODULE_DEVICE_TABLE(of
, bh1750_of_match
);
319 static struct i2c_driver bh1750_driver
= {
322 .of_match_table
= bh1750_of_match
,
323 .pm
= &bh1750_pm_ops
,
325 .probe
= bh1750_probe
,
326 .remove
= bh1750_remove
,
327 .id_table
= bh1750_id
,
330 module_i2c_driver(bh1750_driver
);
332 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
333 MODULE_DESCRIPTION("ROHM BH1710/BH1715/BH1721/BH1750/BH1751 als driver");
334 MODULE_LICENSE("GPL v2");