1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ROHM BH1710/BH1715/BH1721/BH1750/BH1751 ambient light sensor driver
4  *
5  * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
6  *
7  * Data sheets:
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
13  *
14  * 7-bit I2C slave addresses:
15  *  0x23 (ADDR pin low)
16  *  0x5C (ADDR pin high)
17  *
18  */
19 
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>
25 #include <linux/gpio/consumer.h>
26 
27 #define BH1750_POWER_DOWN		0x00
28 #define BH1750_ONE_TIME_H_RES_MODE	0x20 /* auto-mode for BH1721 */
29 #define BH1750_CHANGE_INT_TIME_H_BIT	0x40
30 #define BH1750_CHANGE_INT_TIME_L_BIT	0x60
31 
32 /* Define the reset delay time in microseconds */
33 #define BH1750_RESET_DELAY_US 10000 /* 10ms */
34 
35 enum {
36 	BH1710,
37 	BH1721,
38 	BH1750,
39 };
40 
41 struct bh1750_chip_info;
42 struct bh1750_data {
43 	struct i2c_client *client;
44 	struct mutex lock;
45 	const struct bh1750_chip_info *chip_info;
46 	u16 mtreg;
47 	struct gpio_desc *reset_gpio;
48 };
49 
50 struct bh1750_chip_info {
51 	u16 mtreg_min;
52 	u16 mtreg_max;
53 	u16 mtreg_default;
54 	int mtreg_to_usec;
55 	int mtreg_to_scale;
56 
57 	/*
58 	 * For BH1710/BH1721 all possible integration time values won't fit
59 	 * into one page so displaying is limited to every second one.
60 	 * Note, that user can still write proper values which were not
61 	 * listed.
62 	 */
63 	int inc;
64 
65 	u16 int_time_low_mask;
66 	u16 int_time_high_mask;
67 };
68 
69 static const struct bh1750_chip_info bh1750_chip_info_tbl[] = {
70 	[BH1710] = { 140, 1022, 300, 400,  250000000, 2, 0x001F, 0x03E0 },
71 	[BH1721] = { 140, 1020, 300, 400,  250000000, 2, 0x0010, 0x03E0 },
72 	[BH1750] = { 31,  254,  69,  1740, 57500000,  1, 0x001F, 0x00E0 },
73 };
74 
75 static int bh1750_change_int_time(struct bh1750_data *data, int usec)
76 {
77 	int ret;
78 	u16 val;
79 	u8 regval;
80 	const struct bh1750_chip_info *chip_info = data->chip_info;
81 
82 	if ((usec % chip_info->mtreg_to_usec) != 0)
83 		return -EINVAL;
84 
85 	val = usec / chip_info->mtreg_to_usec;
86 	if (val < chip_info->mtreg_min || val > chip_info->mtreg_max)
87 		return -EINVAL;
88 
89 	ret = i2c_smbus_write_byte(data->client, BH1750_POWER_DOWN);
90 	if (ret < 0)
91 		return ret;
92 
93 	regval = (val & chip_info->int_time_high_mask) >> 5;
94 	ret = i2c_smbus_write_byte(data->client,
95 				   BH1750_CHANGE_INT_TIME_H_BIT | regval);
96 	if (ret < 0)
97 		return ret;
98 
99 	regval = val & chip_info->int_time_low_mask;
100 	ret = i2c_smbus_write_byte(data->client,
101 				   BH1750_CHANGE_INT_TIME_L_BIT | regval);
102 	if (ret < 0)
103 		return ret;
104 
105 	data->mtreg = val;
106 
107 	return 0;
108 }
109 
110 static int bh1750_read(struct bh1750_data *data, int *val)
111 {
112 	int ret;
113 	__be16 result;
114 	const struct bh1750_chip_info *chip_info = data->chip_info;
115 	unsigned long delay = chip_info->mtreg_to_usec * data->mtreg;
116 
117 	/*
118 	 * BH1721 will enter continuous mode on receiving this command.
119 	 * Note, that this eliminates need for bh1750_resume().
120 	 */
121 	ret = i2c_smbus_write_byte(data->client, BH1750_ONE_TIME_H_RES_MODE);
122 	if (ret < 0)
123 		return ret;
124 
125 	usleep_range(delay + 15000, delay + 40000);
126 
127 	ret = i2c_master_recv(data->client, (char *)&result, 2);
128 	if (ret < 0)
129 		return ret;
130 
131 	*val = be16_to_cpu(result);
132 
133 	return 0;
134 }
135 
136 static int bh1750_read_raw(struct iio_dev *indio_dev,
137 			   struct iio_chan_spec const *chan,
138 			   int *val, int *val2, long mask)
139 {
140 	int ret, tmp;
141 	struct bh1750_data *data = iio_priv(indio_dev);
142 	const struct bh1750_chip_info *chip_info = data->chip_info;
143 
144 	switch (mask) {
145 	case IIO_CHAN_INFO_RAW:
146 		switch (chan->type) {
147 		case IIO_LIGHT:
148 			mutex_lock(&data->lock);
149 			ret = bh1750_read(data, val);
150 			mutex_unlock(&data->lock);
151 			if (ret < 0)
152 				return ret;
153 
154 			return IIO_VAL_INT;
155 		default:
156 			return -EINVAL;
157 		}
158 	case IIO_CHAN_INFO_SCALE:
159 		tmp = chip_info->mtreg_to_scale / data->mtreg;
160 		*val = tmp / 1000000;
161 		*val2 = tmp % 1000000;
162 		return IIO_VAL_INT_PLUS_MICRO;
163 	case IIO_CHAN_INFO_INT_TIME:
164 		*val = 0;
165 		*val2 = chip_info->mtreg_to_usec * data->mtreg;
166 		return IIO_VAL_INT_PLUS_MICRO;
167 	default:
168 		return -EINVAL;
169 	}
170 }
171 
172 static int bh1750_write_raw(struct iio_dev *indio_dev,
173 			    struct iio_chan_spec const *chan,
174 			    int val, int val2, long mask)
175 {
176 	int ret;
177 	struct bh1750_data *data = iio_priv(indio_dev);
178 
179 	switch (mask) {
180 	case IIO_CHAN_INFO_INT_TIME:
181 		if (val != 0)
182 			return -EINVAL;
183 
184 		mutex_lock(&data->lock);
185 		ret = bh1750_change_int_time(data, val2);
186 		mutex_unlock(&data->lock);
187 		return ret;
188 	default:
189 		return -EINVAL;
190 	}
191 }
192 
193 static ssize_t bh1750_show_int_time_available(struct device *dev,
194 		struct device_attribute *attr, char *buf)
195 {
196 	int i;
197 	size_t len = 0;
198 	struct bh1750_data *data = iio_priv(dev_to_iio_dev(dev));
199 	const struct bh1750_chip_info *chip_info = data->chip_info;
200 
201 	for (i = chip_info->mtreg_min; i <= chip_info->mtreg_max; i += chip_info->inc)
202 		len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06d ",
203 				 chip_info->mtreg_to_usec * i);
204 
205 	buf[len - 1] = '\n';
206 
207 	return len;
208 }
209 
210 static IIO_DEV_ATTR_INT_TIME_AVAIL(bh1750_show_int_time_available);
211 
212 static struct attribute *bh1750_attributes[] = {
213 	&iio_dev_attr_integration_time_available.dev_attr.attr,
214 	NULL,
215 };
216 
217 static const struct attribute_group bh1750_attribute_group = {
218 	.attrs = bh1750_attributes,
219 };
220 
221 static const struct iio_info bh1750_info = {
222 	.attrs = &bh1750_attribute_group,
223 	.read_raw = bh1750_read_raw,
224 	.write_raw = bh1750_write_raw,
225 };
226 
227 static const struct iio_chan_spec bh1750_channels[] = {
228 	{
229 		.type = IIO_LIGHT,
230 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
231 				      BIT(IIO_CHAN_INFO_SCALE) |
232 				      BIT(IIO_CHAN_INFO_INT_TIME)
233 	}
234 };
235 
236 static int bh1750_probe(struct i2c_client *client)
237 {
238 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
239 	int ret, usec;
240 	struct bh1750_data *data;
241 	struct iio_dev *indio_dev;
242 
243 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
244 				I2C_FUNC_SMBUS_WRITE_BYTE))
245 		return -EOPNOTSUPP;
246 
247 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
248 	if (!indio_dev)
249 		return -ENOMEM;
250 
251 	data = iio_priv(indio_dev);
252 	i2c_set_clientdata(client, indio_dev);
253 	data->client = client;
254 	data->chip_info = &bh1750_chip_info_tbl[id->driver_data];
255 
256 	/* Get reset GPIO from device tree */
257 	data->reset_gpio = devm_gpiod_get_optional(&client->dev,
258 						   "reset", GPIOD_OUT_HIGH);
259 
260 	if (IS_ERR(data->reset_gpio))
261 		return dev_err_probe(&client->dev, PTR_ERR(data->reset_gpio),
262 				     "Failed to get reset GPIO\n");
263 
264 	/* Perform hardware reset if GPIO is provided */
265 	if (data->reset_gpio) {
266 		/* Perform reset sequence: low-high */
267 		gpiod_set_value_cansleep(data->reset_gpio, 1);
268 		fsleep(BH1750_RESET_DELAY_US);
269 		gpiod_set_value_cansleep(data->reset_gpio, 0);
270 		fsleep(BH1750_RESET_DELAY_US);
271 
272 		dev_dbg(&client->dev, "BH1750 reset completed via GPIO\n");
273 	}
274 
275 	usec = data->chip_info->mtreg_to_usec * data->chip_info->mtreg_default;
276 	ret = bh1750_change_int_time(data, usec);
277 	if (ret < 0)
278 		return ret;
279 
280 	mutex_init(&data->lock);
281 	indio_dev->info = &bh1750_info;
282 	indio_dev->name = id->name;
283 	indio_dev->channels = bh1750_channels;
284 	indio_dev->num_channels = ARRAY_SIZE(bh1750_channels);
285 	indio_dev->modes = INDIO_DIRECT_MODE;
286 
287 	return iio_device_register(indio_dev);
288 }
289 
290 static void bh1750_remove(struct i2c_client *client)
291 {
292 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
293 	struct bh1750_data *data = iio_priv(indio_dev);
294 
295 	iio_device_unregister(indio_dev);
296 
297 	mutex_lock(&data->lock);
298 	i2c_smbus_write_byte(client, BH1750_POWER_DOWN);
299 	mutex_unlock(&data->lock);
300 }
301 
302 static int bh1750_suspend(struct device *dev)
303 {
304 	int ret;
305 	struct bh1750_data *data =
306 		iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
307 
308 	/*
309 	 * This is mainly for BH1721 which doesn't enter power down
310 	 * mode automatically.
311 	 */
312 	mutex_lock(&data->lock);
313 	ret = i2c_smbus_write_byte(data->client, BH1750_POWER_DOWN);
314 	mutex_unlock(&data->lock);
315 
316 	return ret;
317 }
318 
319 static DEFINE_SIMPLE_DEV_PM_OPS(bh1750_pm_ops, bh1750_suspend, NULL);
320 
321 static const struct i2c_device_id bh1750_id[] = {
322 	{ "bh1710", BH1710 },
323 	{ "bh1715", BH1750 },
324 	{ "bh1721", BH1721 },
325 	{ "bh1750", BH1750 },
326 	{ "bh1751", BH1750 },
327 	{ }
328 };
329 MODULE_DEVICE_TABLE(i2c, bh1750_id);
330 
331 static const struct of_device_id bh1750_of_match[] = {
332 	{ .compatible = "rohm,bh1710", },
333 	{ .compatible = "rohm,bh1715", },
334 	{ .compatible = "rohm,bh1721", },
335 	{ .compatible = "rohm,bh1750", },
336 	{ .compatible = "rohm,bh1751", },
337 	{ }
338 };
339 MODULE_DEVICE_TABLE(of, bh1750_of_match);
340 
341 static struct i2c_driver bh1750_driver = {
342 	.driver = {
343 		.name = "bh1750",
344 		.of_match_table = bh1750_of_match,
345 		.pm = pm_sleep_ptr(&bh1750_pm_ops),
346 	},
347 	.probe = bh1750_probe,
348 	.remove = bh1750_remove,
349 	.id_table = bh1750_id,
350 
351 };
352 module_i2c_driver(bh1750_driver);
353 
354 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
355 MODULE_DESCRIPTION("ROHM BH1710/BH1715/BH1721/BH1750/BH1751 als driver");
356 MODULE_LICENSE("GPL v2");
357