xref: /linux/drivers/iio/light/ltrf216a.c (revision 889600e21e3be388a6817c2a0dac0411df860751)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LTRF216A Ambient Light Sensor
4  *
5  * Copyright (C) 2022 Collabora, Ltd.
6  * Author: Shreeya Patel <shreeya.patel@collabora.com>
7  *
8  * Copyright (C) 2021 Lite-On Technology Corp (Singapore)
9  * Author: Shi Zhigang <Zhigang.Shi@liteon.com>
10  *
11  * IIO driver for LTRF216A (7-bit I2C slave address 0x53).
12  */
13 
14 #include <linux/bitfield.h>
15 #include <linux/bits.h>
16 #include <linux/delay.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/iopoll.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/pm.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/regmap.h>
25 
26 #include <linux/iio/iio.h>
27 
28 #include <linux/unaligned.h>
29 
30 #define LTRF216A_ALS_RESET_MASK		BIT(4)
31 #define LTRF216A_ALS_DATA_STATUS	BIT(3)
32 #define LTRF216A_ALS_ENABLE_MASK	BIT(1)
33 #define LTRF216A_MAIN_CTRL		0x00
34 #define LTRF216A_ALS_MEAS_RES		0x04
35 #define LTRF216A_ALS_GAIN		0x05
36 #define LTRF216A_PART_ID		0x06
37 #define LTRF216A_MAIN_STATUS		0x07
38 #define LTRF216A_ALS_CLEAR_DATA_0	0x0a
39 #define LTRF216A_ALS_CLEAR_DATA_1	0x0b
40 #define LTRF216A_ALS_CLEAR_DATA_2	0x0c
41 #define LTRF216A_ALS_DATA_0		0x0d
42 #define LTRF216A_ALS_DATA_1		0x0e
43 #define LTRF216A_ALS_DATA_2		0x0f
44 #define LTRF216A_INT_CFG		0x19
45 #define LTRF216A_INT_PST		0x1a
46 #define LTRF216A_ALS_THRES_UP_0		0x21
47 #define LTRF216A_ALS_THRES_UP_1		0x22
48 #define LTRF216A_ALS_THRES_UP_2		0x23
49 #define LTRF216A_ALS_THRES_LOW_0	0x24
50 #define LTRF216A_ALS_THRES_LOW_1	0x25
51 #define LTRF216A_ALS_THRES_LOW_2	0x26
52 #define LTRF216A_ALS_READ_DATA_DELAY_US	20000
53 
54 static const int ltrf216a_int_time_available[][2] = {
55 	{ 0, 400000 },
56 	{ 0, 200000 },
57 	{ 0, 100000 },
58 	{ 0,  50000 },
59 	{ 0,  25000 },
60 };
61 
62 static const int ltrf216a_int_time_reg[][2] = {
63 	{ 400, 0x03 },
64 	{ 200, 0x13 },
65 	{ 100, 0x22 },
66 	{  50, 0x31 },
67 	{  25, 0x40 },
68 };
69 
70 struct ltr_chip_info {
71 	/* Chip contains CLEAR_DATA_0/1/2 registers at offset 0xa..0xc */
72 	bool		has_clear_data;
73 	/* Lux calculation multiplier for ALS data */
74 	int		lux_multiplier;
75 };
76 
77 /*
78  * Window Factor is needed when the device is under Window glass
79  * with coated tinted ink. This is to compensate for the light loss
80  * due to the lower transmission rate of the window glass and helps
81  * in calculating lux.
82  */
83 #define LTRF216A_WIN_FAC	1
84 
85 struct ltrf216a_data {
86 	struct regmap *regmap;
87 	struct i2c_client *client;
88 	const struct ltr_chip_info *info;
89 	u32 int_time;
90 	u16 int_time_fac;
91 	u8 als_gain_fac;
92 	/*
93 	 * Protects regmap accesses and makes sure integration time
94 	 * remains constant during the measurement of lux.
95 	 */
96 	struct mutex lock;
97 };
98 
99 static const struct iio_chan_spec ltrf216a_channels[] = {
100 	{
101 		.type = IIO_LIGHT,
102 		.info_mask_separate =
103 			BIT(IIO_CHAN_INFO_RAW) |
104 			BIT(IIO_CHAN_INFO_PROCESSED) |
105 			BIT(IIO_CHAN_INFO_INT_TIME),
106 		.info_mask_separate_available =
107 			BIT(IIO_CHAN_INFO_INT_TIME),
108 	},
109 };
110 
ltrf216a_reset(struct iio_dev * indio_dev)111 static void ltrf216a_reset(struct iio_dev *indio_dev)
112 {
113 	struct ltrf216a_data *data = iio_priv(indio_dev);
114 
115 	/* reset sensor, chip fails to respond to this, so ignore any errors */
116 	regmap_write(data->regmap, LTRF216A_MAIN_CTRL, LTRF216A_ALS_RESET_MASK);
117 
118 	/* reset time */
119 	usleep_range(1000, 2000);
120 }
121 
ltrf216a_enable(struct iio_dev * indio_dev)122 static int ltrf216a_enable(struct iio_dev *indio_dev)
123 {
124 	struct ltrf216a_data *data = iio_priv(indio_dev);
125 	struct device *dev = &data->client->dev;
126 	int ret;
127 
128 	/* enable sensor */
129 	ret = regmap_set_bits(data->regmap,
130 			      LTRF216A_MAIN_CTRL, LTRF216A_ALS_ENABLE_MASK);
131 	if (ret) {
132 		dev_err(dev, "failed to enable sensor: %d\n", ret);
133 		return ret;
134 	}
135 
136 	/* sleep for one integration cycle after enabling the device */
137 	msleep(ltrf216a_int_time_reg[0][0]);
138 
139 	return 0;
140 }
141 
ltrf216a_disable(struct iio_dev * indio_dev)142 static int ltrf216a_disable(struct iio_dev *indio_dev)
143 {
144 	struct ltrf216a_data *data = iio_priv(indio_dev);
145 	struct device *dev = &data->client->dev;
146 	int ret;
147 
148 	ret = regmap_write(data->regmap, LTRF216A_MAIN_CTRL, 0);
149 	if (ret)
150 		dev_err(dev, "failed to disable sensor: %d\n", ret);
151 
152 	return ret;
153 }
154 
ltrf216a_cleanup(void * data)155 static void ltrf216a_cleanup(void *data)
156 {
157 	struct iio_dev *indio_dev = data;
158 
159 	ltrf216a_disable(indio_dev);
160 }
161 
ltrf216a_set_int_time(struct ltrf216a_data * data,int itime)162 static int ltrf216a_set_int_time(struct ltrf216a_data *data, int itime)
163 {
164 	struct device *dev = &data->client->dev;
165 	unsigned int i;
166 	u8 reg_val;
167 	int ret;
168 
169 	for (i = 0; i < ARRAY_SIZE(ltrf216a_int_time_available); i++) {
170 		if (ltrf216a_int_time_available[i][1] == itime)
171 			break;
172 	}
173 	if (i == ARRAY_SIZE(ltrf216a_int_time_available))
174 		return -EINVAL;
175 
176 	reg_val = ltrf216a_int_time_reg[i][1];
177 
178 	ret = regmap_write(data->regmap, LTRF216A_ALS_MEAS_RES, reg_val);
179 	if (ret) {
180 		dev_err(dev, "failed to set integration time: %d\n", ret);
181 		return ret;
182 	}
183 
184 	data->int_time_fac = ltrf216a_int_time_reg[i][0];
185 	data->int_time = itime;
186 
187 	return 0;
188 }
189 
ltrf216a_get_int_time(struct ltrf216a_data * data,int * val,int * val2)190 static int ltrf216a_get_int_time(struct ltrf216a_data *data,
191 				 int *val, int *val2)
192 {
193 	*val = 0;
194 	*val2 = data->int_time;
195 	return IIO_VAL_INT_PLUS_MICRO;
196 }
197 
ltrf216a_set_power_state(struct ltrf216a_data * data,bool on)198 static int ltrf216a_set_power_state(struct ltrf216a_data *data, bool on)
199 {
200 	struct device *dev = &data->client->dev;
201 	int ret = 0;
202 
203 	if (on) {
204 		ret = pm_runtime_resume_and_get(dev);
205 		if (ret) {
206 			dev_err(dev, "failed to resume runtime PM: %d\n", ret);
207 			return ret;
208 		}
209 	} else {
210 		pm_runtime_put_autosuspend(dev);
211 	}
212 
213 	return ret;
214 }
215 
ltrf216a_read_data(struct ltrf216a_data * data,u8 addr)216 static int ltrf216a_read_data(struct ltrf216a_data *data, u8 addr)
217 {
218 	struct device *dev = &data->client->dev;
219 	int ret, val;
220 	u8 buf[3];
221 
222 	ret = regmap_read_poll_timeout(data->regmap, LTRF216A_MAIN_STATUS,
223 				       val, val & LTRF216A_ALS_DATA_STATUS,
224 				       LTRF216A_ALS_READ_DATA_DELAY_US,
225 				       LTRF216A_ALS_READ_DATA_DELAY_US * 50);
226 	if (ret) {
227 		dev_err(dev, "failed to wait for measurement data: %d\n", ret);
228 		return ret;
229 	}
230 
231 	ret = regmap_bulk_read(data->regmap, addr, buf, sizeof(buf));
232 	if (ret) {
233 		dev_err(dev, "failed to read measurement data: %d\n", ret);
234 		return ret;
235 	}
236 
237 	return get_unaligned_le24(&buf[0]);
238 }
239 
ltrf216a_get_lux(struct ltrf216a_data * data)240 static int ltrf216a_get_lux(struct ltrf216a_data *data)
241 {
242 	int ret, greendata;
243 	u64 lux;
244 
245 	ret = ltrf216a_set_power_state(data, true);
246 	if (ret)
247 		return ret;
248 
249 	greendata = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
250 	ltrf216a_set_power_state(data, false);
251 	if (greendata < 0)
252 		return greendata;
253 
254 	lux = greendata * data->info->lux_multiplier * LTRF216A_WIN_FAC;
255 
256 	return lux;
257 }
258 
ltrf216a_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)259 static int ltrf216a_read_raw(struct iio_dev *indio_dev,
260 			     struct iio_chan_spec const *chan, int *val,
261 			     int *val2, long mask)
262 {
263 	struct ltrf216a_data *data = iio_priv(indio_dev);
264 	int ret;
265 
266 	switch (mask) {
267 	case IIO_CHAN_INFO_RAW:
268 		ret = ltrf216a_set_power_state(data, true);
269 		if (ret)
270 			return ret;
271 		mutex_lock(&data->lock);
272 		ret = ltrf216a_read_data(data, LTRF216A_ALS_DATA_0);
273 		mutex_unlock(&data->lock);
274 		ltrf216a_set_power_state(data, false);
275 		if (ret < 0)
276 			return ret;
277 		*val = ret;
278 		return IIO_VAL_INT;
279 	case IIO_CHAN_INFO_PROCESSED:
280 		mutex_lock(&data->lock);
281 		ret = ltrf216a_get_lux(data);
282 		mutex_unlock(&data->lock);
283 		if (ret < 0)
284 			return ret;
285 		*val = ret;
286 		*val2 = data->als_gain_fac * data->int_time_fac;
287 		return IIO_VAL_FRACTIONAL;
288 	case IIO_CHAN_INFO_INT_TIME:
289 		mutex_lock(&data->lock);
290 		ret = ltrf216a_get_int_time(data, val, val2);
291 		mutex_unlock(&data->lock);
292 		return ret;
293 	default:
294 		return -EINVAL;
295 	}
296 }
297 
ltrf216a_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)298 static int ltrf216a_write_raw(struct iio_dev *indio_dev,
299 			      struct iio_chan_spec const *chan, int val,
300 			      int val2, long mask)
301 {
302 	struct ltrf216a_data *data = iio_priv(indio_dev);
303 	int ret;
304 
305 	switch (mask) {
306 	case IIO_CHAN_INFO_INT_TIME:
307 		if (val != 0)
308 			return -EINVAL;
309 		mutex_lock(&data->lock);
310 		ret = ltrf216a_set_int_time(data, val2);
311 		mutex_unlock(&data->lock);
312 		return ret;
313 	default:
314 		return -EINVAL;
315 	}
316 }
317 
ltrf216a_read_available(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,const int ** vals,int * type,int * length,long mask)318 static int ltrf216a_read_available(struct iio_dev *indio_dev,
319 				   struct iio_chan_spec const *chan,
320 				   const int **vals, int *type, int *length,
321 				   long mask)
322 {
323 	switch (mask) {
324 	case IIO_CHAN_INFO_INT_TIME:
325 		*length = ARRAY_SIZE(ltrf216a_int_time_available) * 2;
326 		*vals = (const int *)ltrf216a_int_time_available;
327 		*type = IIO_VAL_INT_PLUS_MICRO;
328 		return IIO_AVAIL_LIST;
329 	default:
330 		return -EINVAL;
331 	}
332 }
333 
334 static const struct iio_info ltrf216a_info = {
335 	.read_raw = ltrf216a_read_raw,
336 	.write_raw = ltrf216a_write_raw,
337 	.read_avail = ltrf216a_read_available,
338 };
339 
ltrf216a_readable_reg(struct device * dev,unsigned int reg)340 static bool ltrf216a_readable_reg(struct device *dev, unsigned int reg)
341 {
342 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
343 	struct ltrf216a_data *data = iio_priv(indio_dev);
344 
345 	switch (reg) {
346 	case LTRF216A_MAIN_CTRL:
347 	case LTRF216A_ALS_MEAS_RES:
348 	case LTRF216A_ALS_GAIN:
349 	case LTRF216A_PART_ID:
350 	case LTRF216A_MAIN_STATUS:
351 	case LTRF216A_ALS_DATA_0:
352 	case LTRF216A_ALS_DATA_1:
353 	case LTRF216A_ALS_DATA_2:
354 	case LTRF216A_INT_CFG:
355 	case LTRF216A_INT_PST:
356 	case LTRF216A_ALS_THRES_UP_0:
357 	case LTRF216A_ALS_THRES_UP_1:
358 	case LTRF216A_ALS_THRES_UP_2:
359 	case LTRF216A_ALS_THRES_LOW_0:
360 	case LTRF216A_ALS_THRES_LOW_1:
361 	case LTRF216A_ALS_THRES_LOW_2:
362 		return true;
363 	case LTRF216A_ALS_CLEAR_DATA_0:
364 	case LTRF216A_ALS_CLEAR_DATA_1:
365 	case LTRF216A_ALS_CLEAR_DATA_2:
366 		return data->info->has_clear_data;
367 	default:
368 		return false;
369 	}
370 }
371 
ltrf216a_writable_reg(struct device * dev,unsigned int reg)372 static bool ltrf216a_writable_reg(struct device *dev, unsigned int reg)
373 {
374 	switch (reg) {
375 	case LTRF216A_MAIN_CTRL:
376 	case LTRF216A_ALS_MEAS_RES:
377 	case LTRF216A_ALS_GAIN:
378 	case LTRF216A_INT_CFG:
379 	case LTRF216A_INT_PST:
380 	case LTRF216A_ALS_THRES_UP_0:
381 	case LTRF216A_ALS_THRES_UP_1:
382 	case LTRF216A_ALS_THRES_UP_2:
383 	case LTRF216A_ALS_THRES_LOW_0:
384 	case LTRF216A_ALS_THRES_LOW_1:
385 	case LTRF216A_ALS_THRES_LOW_2:
386 		return true;
387 	default:
388 		return false;
389 	}
390 }
391 
ltrf216a_volatile_reg(struct device * dev,unsigned int reg)392 static bool ltrf216a_volatile_reg(struct device *dev, unsigned int reg)
393 {
394 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
395 	struct ltrf216a_data *data = iio_priv(indio_dev);
396 
397 	switch (reg) {
398 	case LTRF216A_MAIN_STATUS:
399 	case LTRF216A_ALS_DATA_0:
400 	case LTRF216A_ALS_DATA_1:
401 	case LTRF216A_ALS_DATA_2:
402 		return true;
403 	/*
404 	 * If these registers are not present on a chip (like LTR-308),
405 	 * the missing registers are not considered volatile.
406 	 */
407 	case LTRF216A_ALS_CLEAR_DATA_0:
408 	case LTRF216A_ALS_CLEAR_DATA_1:
409 	case LTRF216A_ALS_CLEAR_DATA_2:
410 		return data->info->has_clear_data;
411 	default:
412 		return false;
413 	}
414 }
415 
ltrf216a_precious_reg(struct device * dev,unsigned int reg)416 static bool ltrf216a_precious_reg(struct device *dev, unsigned int reg)
417 {
418 	return reg == LTRF216A_MAIN_STATUS;
419 }
420 
421 static const struct regmap_config ltrf216a_regmap_config = {
422 	.name = "ltrf216a",
423 	.reg_bits = 8,
424 	.val_bits = 8,
425 	.cache_type = REGCACHE_RBTREE,
426 	.max_register = LTRF216A_ALS_THRES_LOW_2,
427 	.readable_reg = ltrf216a_readable_reg,
428 	.writeable_reg = ltrf216a_writable_reg,
429 	.volatile_reg = ltrf216a_volatile_reg,
430 	.precious_reg = ltrf216a_precious_reg,
431 	.disable_locking = true,
432 };
433 
ltrf216a_probe(struct i2c_client * client)434 static int ltrf216a_probe(struct i2c_client *client)
435 {
436 	struct ltrf216a_data *data;
437 	struct iio_dev *indio_dev;
438 	int ret;
439 
440 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
441 	if (!indio_dev)
442 		return -ENOMEM;
443 
444 	data = iio_priv(indio_dev);
445 
446 	data->regmap = devm_regmap_init_i2c(client, &ltrf216a_regmap_config);
447 	if (IS_ERR(data->regmap))
448 		return dev_err_probe(&client->dev, PTR_ERR(data->regmap),
449 				     "regmap initialization failed\n");
450 
451 	i2c_set_clientdata(client, indio_dev);
452 	data->client = client;
453 	data->info = i2c_get_match_data(client);
454 
455 	mutex_init(&data->lock);
456 
457 	indio_dev->info = &ltrf216a_info;
458 	indio_dev->name = "ltrf216a";
459 	indio_dev->channels = ltrf216a_channels;
460 	indio_dev->num_channels = ARRAY_SIZE(ltrf216a_channels);
461 	indio_dev->modes = INDIO_DIRECT_MODE;
462 
463 	ret = pm_runtime_set_active(&client->dev);
464 	if (ret)
465 		return ret;
466 
467 	/* reset sensor, chip fails to respond to this, so ignore any errors */
468 	ltrf216a_reset(indio_dev);
469 
470 	ret = regmap_reinit_cache(data->regmap, &ltrf216a_regmap_config);
471 	if (ret)
472 		return dev_err_probe(&client->dev, ret,
473 				     "failed to reinit regmap cache\n");
474 
475 	ret = ltrf216a_enable(indio_dev);
476 	if (ret)
477 		return ret;
478 
479 	ret = devm_add_action_or_reset(&client->dev, ltrf216a_cleanup,
480 				       indio_dev);
481 	if (ret)
482 		return ret;
483 
484 	ret = devm_pm_runtime_enable(&client->dev);
485 	if (ret)
486 		return dev_err_probe(&client->dev, ret,
487 				     "failed to enable runtime PM\n");
488 
489 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
490 	pm_runtime_use_autosuspend(&client->dev);
491 
492 	data->int_time = 100000;
493 	data->int_time_fac = 100;
494 	data->als_gain_fac = 3;
495 
496 	return devm_iio_device_register(&client->dev, indio_dev);
497 }
498 
ltrf216a_runtime_suspend(struct device * dev)499 static int ltrf216a_runtime_suspend(struct device *dev)
500 {
501 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
502 	struct ltrf216a_data *data = iio_priv(indio_dev);
503 	int ret;
504 
505 	ret = ltrf216a_disable(indio_dev);
506 	if (ret)
507 		return ret;
508 
509 	regcache_cache_only(data->regmap, true);
510 
511 	return 0;
512 }
513 
ltrf216a_runtime_resume(struct device * dev)514 static int ltrf216a_runtime_resume(struct device *dev)
515 {
516 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
517 	struct ltrf216a_data *data = iio_priv(indio_dev);
518 	int ret;
519 
520 	regcache_cache_only(data->regmap, false);
521 	regcache_mark_dirty(data->regmap);
522 	ret = regcache_sync(data->regmap);
523 	if (ret)
524 		goto cache_only;
525 
526 	ret = ltrf216a_enable(indio_dev);
527 	if (ret)
528 		goto cache_only;
529 
530 	return 0;
531 
532 cache_only:
533 	regcache_cache_only(data->regmap, true);
534 
535 	return ret;
536 }
537 
538 static DEFINE_RUNTIME_DEV_PM_OPS(ltrf216a_pm_ops, ltrf216a_runtime_suspend,
539 				 ltrf216a_runtime_resume, NULL);
540 
541 static const struct ltr_chip_info ltr308_chip_info = {
542 	.has_clear_data		= false,
543 	.lux_multiplier		= 60,
544 };
545 
546 static const struct ltr_chip_info ltrf216a_chip_info = {
547 	.has_clear_data		= true,
548 	.lux_multiplier		= 45,
549 };
550 
551 static const struct i2c_device_id ltrf216a_id[] = {
552 	{ .name = "ltr308", .driver_data = (kernel_ulong_t)&ltr308_chip_info },
553 	{ .name = "ltrf216a", .driver_data = (kernel_ulong_t)&ltrf216a_chip_info },
554 	{ }
555 };
556 MODULE_DEVICE_TABLE(i2c, ltrf216a_id);
557 
558 static const struct of_device_id ltrf216a_of_match[] = {
559 	{ .compatible = "liteon,ltr308", .data = &ltr308_chip_info },
560 	{ .compatible = "liteon,ltrf216a", .data = &ltrf216a_chip_info },
561 	/* For Valve's Steamdeck device, an ACPI platform using PRP0001 */
562 	{ .compatible = "ltr,ltrf216a", .data = &ltrf216a_chip_info },
563 	{ }
564 };
565 MODULE_DEVICE_TABLE(of, ltrf216a_of_match);
566 
567 static struct i2c_driver ltrf216a_driver = {
568 	.driver = {
569 		.name = "ltrf216a",
570 		.pm = pm_ptr(&ltrf216a_pm_ops),
571 		.of_match_table = ltrf216a_of_match,
572 	},
573 	.probe = ltrf216a_probe,
574 	.id_table = ltrf216a_id,
575 };
576 module_i2c_driver(ltrf216a_driver);
577 
578 MODULE_AUTHOR("Shreeya Patel <shreeya.patel@collabora.com>");
579 MODULE_AUTHOR("Shi Zhigang <Zhigang.Shi@liteon.com>");
580 MODULE_DESCRIPTION("LTRF216A ambient light sensor driver");
581 MODULE_LICENSE("GPL");
582