File Index
Files and directories
Sign in
FILE

isl29028.c

File preview with syntax highlighting.

linux-6.19.14/drivers/iio/light/isl29028.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * IIO driver for the light sensor ISL29028.
  4 * ISL29028 is Concurrent Ambient Light and Proximity Sensor
  5 *
  6 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
  7 * Copyright (c) 2016-2017 Brian Masney <masneyb@onstation.org>
  8 *
  9 * Datasheets:
 10 *  - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29028.pdf
 11 *  - http://www.intersil.com/content/dam/Intersil/documents/isl2/isl29030.pdf
 12 */
 13
 14#include <linux/module.h>
 15#include <linux/i2c.h>
 16#include <linux/err.h>
 17#include <linux/mutex.h>
 18#include <linux/delay.h>
 19#include <linux/slab.h>
 20#include <linux/regmap.h>
 21#include <linux/iio/iio.h>
 22#include <linux/iio/sysfs.h>
 23#include <linux/pm_runtime.h>
 24
 25#define ISL29028_CONV_TIME_MS			100
 26
 27#define ISL29028_REG_CONFIGURE			0x01
 28
 29#define ISL29028_CONF_ALS_IR_MODE_ALS		0
 30#define ISL29028_CONF_ALS_IR_MODE_IR		BIT(0)
 31#define ISL29028_CONF_ALS_IR_MODE_MASK		BIT(0)
 32
 33#define ISL29028_CONF_ALS_RANGE_LOW_LUX		0
 34#define ISL29028_CONF_ALS_RANGE_HIGH_LUX	BIT(1)
 35#define ISL29028_CONF_ALS_RANGE_MASK		BIT(1)
 36
 37#define ISL29028_CONF_ALS_DIS			0
 38#define ISL29028_CONF_ALS_EN			BIT(2)
 39#define ISL29028_CONF_ALS_EN_MASK		BIT(2)
 40
 41#define ISL29028_CONF_PROX_SLP_SH		4
 42#define ISL29028_CONF_PROX_SLP_MASK		(7 << ISL29028_CONF_PROX_SLP_SH)
 43
 44#define ISL29028_CONF_PROX_EN			BIT(7)
 45#define ISL29028_CONF_PROX_EN_MASK		BIT(7)
 46
 47#define ISL29028_REG_INTERRUPT			0x02
 48
 49#define ISL29028_REG_PROX_DATA			0x08
 50#define ISL29028_REG_ALSIR_L			0x09
 51#define ISL29028_REG_ALSIR_U			0x0A
 52
 53#define ISL29028_REG_TEST1_MODE			0x0E
 54#define ISL29028_REG_TEST2_MODE			0x0F
 55
 56#define ISL29028_NUM_REGS			(ISL29028_REG_TEST2_MODE + 1)
 57
 58#define ISL29028_POWER_OFF_DELAY_MS		2000
 59
 60struct isl29028_prox_data {
 61	int sampling_int;
 62	int sampling_fract;
 63	int sleep_time;
 64};
 65
 66static const struct isl29028_prox_data isl29028_prox_data[] = {
 67	{   1, 250000, 800 },
 68	{   2, 500000, 400 },
 69	{   5,      0, 200 },
 70	{  10,      0, 100 },
 71	{  13, 300000,  75 },
 72	{  20,      0,  50 },
 73	{  80,      0,  13 }, /*
 74			       * Note: Data sheet lists 12.5 ms sleep time.
 75			       * Round up a half millisecond for msleep().
 76			       */
 77	{ 100,  0,   0 }
 78};
 79
 80enum isl29028_als_ir_mode {
 81	ISL29028_MODE_NONE = 0,
 82	ISL29028_MODE_ALS,
 83	ISL29028_MODE_IR,
 84};
 85
 86struct isl29028_chip {
 87	struct mutex			lock;
 88	struct regmap			*regmap;
 89	int				prox_sampling_int;
 90	int				prox_sampling_frac;
 91	bool				enable_prox;
 92	int				lux_scale;
 93	enum isl29028_als_ir_mode	als_ir_mode;
 94};
 95
 96static int isl29028_find_prox_sleep_index(int sampling_int, int sampling_fract)
 97{
 98	int i;
 99
100	for (i = 0; i < ARRAY_SIZE(isl29028_prox_data); ++i) {
101		if (isl29028_prox_data[i].sampling_int == sampling_int &&
102		    isl29028_prox_data[i].sampling_fract == sampling_fract)
103			return i;
104	}
105
106	return -EINVAL;
107}
108
109static int isl29028_set_proxim_sampling(struct isl29028_chip *chip,
110					int sampling_int, int sampling_fract)
111{
112	struct device *dev = regmap_get_device(chip->regmap);
113	int sleep_index, ret;
114
115	sleep_index = isl29028_find_prox_sleep_index(sampling_int,
116						     sampling_fract);
117	if (sleep_index < 0)
118		return sleep_index;
119
120	ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
121				 ISL29028_CONF_PROX_SLP_MASK,
122				 sleep_index << ISL29028_CONF_PROX_SLP_SH);
123
124	if (ret < 0) {
125		dev_err(dev, "%s(): Error %d setting the proximity sampling\n",
126			__func__, ret);
127		return ret;
128	}
129
130	chip->prox_sampling_int = sampling_int;
131	chip->prox_sampling_frac = sampling_fract;
132
133	return ret;
134}
135
136static int isl29028_enable_proximity(struct isl29028_chip *chip)
137{
138	int prox_index, ret;
139
140	ret = isl29028_set_proxim_sampling(chip, chip->prox_sampling_int,
141					   chip->prox_sampling_frac);
142	if (ret < 0)
143		return ret;
144
145	ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
146				 ISL29028_CONF_PROX_EN_MASK,
147				 ISL29028_CONF_PROX_EN);
148	if (ret < 0)
149		return ret;
150
151	/* Wait for conversion to be complete for first sample */
152	prox_index = isl29028_find_prox_sleep_index(chip->prox_sampling_int,
153						    chip->prox_sampling_frac);
154	if (prox_index < 0)
155		return prox_index;
156
157	msleep(isl29028_prox_data[prox_index].sleep_time);
158
159	return 0;
160}
161
162static int isl29028_set_als_scale(struct isl29028_chip *chip, int lux_scale)
163{
164	struct device *dev = regmap_get_device(chip->regmap);
165	int val = (lux_scale == 2000) ? ISL29028_CONF_ALS_RANGE_HIGH_LUX :
166					ISL29028_CONF_ALS_RANGE_LOW_LUX;
167	int ret;
168
169	ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
170				 ISL29028_CONF_ALS_RANGE_MASK, val);
171	if (ret < 0) {
172		dev_err(dev, "%s(): Error %d setting the ALS scale\n", __func__,
173			ret);
174		return ret;
175	}
176
177	chip->lux_scale = lux_scale;
178
179	return ret;
180}
181
182static int isl29028_set_als_ir_mode(struct isl29028_chip *chip,
183				    enum isl29028_als_ir_mode mode)
184{
185	int ret;
186
187	if (chip->als_ir_mode == mode)
188		return 0;
189
190	ret = isl29028_set_als_scale(chip, chip->lux_scale);
191	if (ret < 0)
192		return ret;
193
194	switch (mode) {
195	case ISL29028_MODE_ALS:
196		ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
197					 ISL29028_CONF_ALS_IR_MODE_MASK,
198					 ISL29028_CONF_ALS_IR_MODE_ALS);
199		if (ret < 0)
200			return ret;
201
202		ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
203					 ISL29028_CONF_ALS_RANGE_MASK,
204					 ISL29028_CONF_ALS_RANGE_HIGH_LUX);
205		break;
206	case ISL29028_MODE_IR:
207		ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
208					 ISL29028_CONF_ALS_IR_MODE_MASK,
209					 ISL29028_CONF_ALS_IR_MODE_IR);
210		break;
211	case ISL29028_MODE_NONE:
212		return regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
213					  ISL29028_CONF_ALS_EN_MASK,
214					  ISL29028_CONF_ALS_DIS);
215	}
216
217	if (ret < 0)
218		return ret;
219
220	/* Enable the ALS/IR */
221	ret = regmap_update_bits(chip->regmap, ISL29028_REG_CONFIGURE,
222				 ISL29028_CONF_ALS_EN_MASK,
223				 ISL29028_CONF_ALS_EN);
224	if (ret < 0)
225		return ret;
226
227	/* Need to wait for conversion time if ALS/IR mode enabled */
228	msleep(ISL29028_CONV_TIME_MS);
229
230	chip->als_ir_mode = mode;
231
232	return 0;
233}
234
235static int isl29028_read_als_ir(struct isl29028_chip *chip, int *als_ir)
236{
237	struct device *dev = regmap_get_device(chip->regmap);
238	unsigned int lsb;
239	unsigned int msb;
240	int ret;
241
242	ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_L, &lsb);
243	if (ret < 0) {
244		dev_err(dev,
245			"%s(): Error %d reading register ALSIR_L\n",
246			__func__, ret);
247		return ret;
248	}
249
250	ret = regmap_read(chip->regmap, ISL29028_REG_ALSIR_U, &msb);
251	if (ret < 0) {
252		dev_err(dev,
253			"%s(): Error %d reading register ALSIR_U\n",
254			__func__, ret);
255		return ret;
256	}
257
258	*als_ir = ((msb & 0xF) << 8) | (lsb & 0xFF);
259
260	return 0;
261}
262
263static int isl29028_read_proxim(struct isl29028_chip *chip, int *prox)
264{
265	struct device *dev = regmap_get_device(chip->regmap);
266	unsigned int data;
267	int ret;
268
269	if (!chip->enable_prox) {
270		ret = isl29028_enable_proximity(chip);
271		if (ret < 0)
272			return ret;
273
274		chip->enable_prox = true;
275	}
276
277	ret = regmap_read(chip->regmap, ISL29028_REG_PROX_DATA, &data);
278	if (ret < 0) {
279		dev_err(dev, "%s(): Error %d reading register PROX_DATA\n",
280			__func__, ret);
281		return ret;
282	}
283
284	*prox = data;
285
286	return 0;
287}
288
289static int isl29028_als_get(struct isl29028_chip *chip, int *als_data)
290{
291	struct device *dev = regmap_get_device(chip->regmap);
292	int ret;
293	int als_ir_data;
294
295	ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_ALS);
296	if (ret < 0) {
297		dev_err(dev, "%s(): Error %d enabling ALS mode\n", __func__,
298			ret);
299		return ret;
300	}
301
302	ret = isl29028_read_als_ir(chip, &als_ir_data);
303	if (ret < 0)
304		return ret;
305
306	/*
307	 * convert als data count to lux.
308	 * if lux_scale = 125,  lux = count * 0.031
309	 * if lux_scale = 2000, lux = count * 0.49
310	 */
311	if (chip->lux_scale == 125)
312		als_ir_data = (als_ir_data * 31) / 1000;
313	else
314		als_ir_data = (als_ir_data * 49) / 100;
315
316	*als_data = als_ir_data;
317
318	return 0;
319}
320
321static int isl29028_ir_get(struct isl29028_chip *chip, int *ir_data)
322{
323	struct device *dev = regmap_get_device(chip->regmap);
324	int ret;
325
326	ret = isl29028_set_als_ir_mode(chip, ISL29028_MODE_IR);
327	if (ret < 0) {
328		dev_err(dev, "%s(): Error %d enabling IR mode\n", __func__,
329			ret);
330		return ret;
331	}
332
333	return isl29028_read_als_ir(chip, ir_data);
334}
335
336static int isl29028_set_pm_runtime_busy(struct isl29028_chip *chip, bool on)
337{
338	struct device *dev = regmap_get_device(chip->regmap);
339
340	if (on)
341		return pm_runtime_resume_and_get(dev);
342
343	return pm_runtime_put_autosuspend(dev);
344}
345
346/* Channel IO */
347static int isl29028_write_raw(struct iio_dev *indio_dev,
348			      struct iio_chan_spec const *chan,
349			      int val, int val2, long mask)
350{
351	struct isl29028_chip *chip = iio_priv(indio_dev);
352	struct device *dev = regmap_get_device(chip->regmap);
353	int ret;
354
355	ret = isl29028_set_pm_runtime_busy(chip, true);
356	if (ret < 0)
357		return ret;
358
359	mutex_lock(&chip->lock);
360
361	ret = -EINVAL;
362	switch (chan->type) {
363	case IIO_PROXIMITY:
364		if (mask != IIO_CHAN_INFO_SAMP_FREQ) {
365			dev_err(dev,
366				"%s(): proximity: Mask value 0x%08lx is not supported\n",
367				__func__, mask);
368			break;
369		}
370
371		if (val < 1 || val > 100) {
372			dev_err(dev,
373				"%s(): proximity: Sampling frequency %d is not in the range [1:100]\n",
374				__func__, val);
375			break;
376		}
377
378		ret = isl29028_set_proxim_sampling(chip, val, val2);
379		break;
380	case IIO_LIGHT:
381		if (mask != IIO_CHAN_INFO_SCALE) {
382			dev_err(dev,
383				"%s(): light: Mask value 0x%08lx is not supported\n",
384				__func__, mask);
385			break;
386		}
387
388		if (val != 125 && val != 2000) {
389			dev_err(dev,
390				"%s(): light: Lux scale %d is not in the set {125, 2000}\n",
391				__func__, val);
392			break;
393		}
394
395		ret = isl29028_set_als_scale(chip, val);
396		break;
397	default:
398		dev_err(dev, "%s(): Unsupported channel type %x\n",
399			__func__, chan->type);
400		break;
401	}
402
403	mutex_unlock(&chip->lock);
404
405	if (ret < 0)
406		return ret;
407
408	ret = isl29028_set_pm_runtime_busy(chip, false);
409	if (ret < 0)
410		return ret;
411
412	return ret;
413}
414
415static int isl29028_read_raw(struct iio_dev *indio_dev,
416			     struct iio_chan_spec const *chan,
417			     int *val, int *val2, long mask)
418{
419	struct isl29028_chip *chip = iio_priv(indio_dev);
420	struct device *dev = regmap_get_device(chip->regmap);
421	int ret, pm_ret;
422
423	ret = isl29028_set_pm_runtime_busy(chip, true);
424	if (ret < 0)
425		return ret;
426
427	mutex_lock(&chip->lock);
428
429	ret = -EINVAL;
430	switch (mask) {
431	case IIO_CHAN_INFO_RAW:
432	case IIO_CHAN_INFO_PROCESSED:
433		switch (chan->type) {
434		case IIO_LIGHT:
435			ret = isl29028_als_get(chip, val);
436			break;
437		case IIO_INTENSITY:
438			ret = isl29028_ir_get(chip, val);
439			break;
440		case IIO_PROXIMITY:
441			ret = isl29028_read_proxim(chip, val);
442			break;
443		default:
444			break;
445		}
446
447		if (ret < 0)
448			break;
449
450		ret = IIO_VAL_INT;
451		break;
452	case IIO_CHAN_INFO_SAMP_FREQ:
453		if (chan->type != IIO_PROXIMITY)
454			break;
455
456		*val = chip->prox_sampling_int;
457		*val2 = chip->prox_sampling_frac;
458		ret = IIO_VAL_INT;
459		break;
460	case IIO_CHAN_INFO_SCALE:
461		if (chan->type != IIO_LIGHT)
462			break;
463		*val = chip->lux_scale;
464		ret = IIO_VAL_INT;
465		break;
466	default:
467		dev_err(dev, "%s(): mask value 0x%08lx is not supported\n",
468			__func__, mask);
469		break;
470	}
471
472	mutex_unlock(&chip->lock);
473
474	if (ret < 0)
475		return ret;
476
477	/**
478	 * Preserve the ret variable if the call to
479	 * isl29028_set_pm_runtime_busy() is successful so the reading
480	 * (if applicable) is returned to user space.
481	 */
482	pm_ret = isl29028_set_pm_runtime_busy(chip, false);
483	if (pm_ret < 0)
484		return pm_ret;
485
486	return ret;
487}
488
489static IIO_CONST_ATTR(in_proximity_sampling_frequency_available,
490				"1.25 2.5 5 10 13.3 20 80 100");
491static IIO_CONST_ATTR(in_illuminance_scale_available, "125 2000");
492
493#define ISL29028_CONST_ATTR(name) (&iio_const_attr_##name.dev_attr.attr)
494static struct attribute *isl29028_attributes[] = {
495	ISL29028_CONST_ATTR(in_proximity_sampling_frequency_available),
496	ISL29028_CONST_ATTR(in_illuminance_scale_available),
497	NULL,
498};
499
500static const struct attribute_group isl29108_group = {
501	.attrs = isl29028_attributes,
502};
503
504static const struct iio_chan_spec isl29028_channels[] = {
505	{
506		.type = IIO_LIGHT,
507		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
508		BIT(IIO_CHAN_INFO_SCALE),
509	}, {
510		.type = IIO_INTENSITY,
511		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
512	}, {
513		.type = IIO_PROXIMITY,
514		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
515		BIT(IIO_CHAN_INFO_SAMP_FREQ),
516	}
517};
518
519static const struct iio_info isl29028_info = {
520	.attrs = &isl29108_group,
521	.read_raw = isl29028_read_raw,
522	.write_raw = isl29028_write_raw,
523};
524
525static int isl29028_clear_configure_reg(struct isl29028_chip *chip)
526{
527	struct device *dev = regmap_get_device(chip->regmap);
528	int ret;
529
530	ret = regmap_write(chip->regmap, ISL29028_REG_CONFIGURE, 0x0);
531	if (ret < 0)
532		dev_err(dev, "%s(): Error %d clearing the CONFIGURE register\n",
533			__func__, ret);
534
535	chip->als_ir_mode = ISL29028_MODE_NONE;
536	chip->enable_prox = false;
537
538	return ret;
539}
540
541static bool isl29028_is_volatile_reg(struct device *dev, unsigned int reg)
542{
543	switch (reg) {
544	case ISL29028_REG_INTERRUPT:
545	case ISL29028_REG_PROX_DATA:
546	case ISL29028_REG_ALSIR_L:
547	case ISL29028_REG_ALSIR_U:
548		return true;
549	default:
550		return false;
551	}
552}
553
554static const struct regmap_config isl29028_regmap_config = {
555	.reg_bits = 8,
556	.val_bits = 8,
557	.volatile_reg = isl29028_is_volatile_reg,
558	.max_register = ISL29028_NUM_REGS - 1,
559	.num_reg_defaults_raw = ISL29028_NUM_REGS,
560	.cache_type = REGCACHE_MAPLE,
561};
562
563static int isl29028_probe(struct i2c_client *client)
564{
565	const struct i2c_device_id *id = i2c_client_get_device_id(client);
566	struct isl29028_chip *chip;
567	struct iio_dev *indio_dev;
568	int ret;
569
570	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
571	if (!indio_dev)
572		return -ENOMEM;
573
574	chip = iio_priv(indio_dev);
575
576	i2c_set_clientdata(client, indio_dev);
577	mutex_init(&chip->lock);
578
579	chip->regmap = devm_regmap_init_i2c(client, &isl29028_regmap_config);
580	if (IS_ERR(chip->regmap)) {
581		ret = PTR_ERR(chip->regmap);
582		dev_err(&client->dev, "%s: Error %d initializing regmap\n",
583			__func__, ret);
584		return ret;
585	}
586
587	chip->enable_prox  = false;
588	chip->prox_sampling_int = 20;
589	chip->prox_sampling_frac = 0;
590	chip->lux_scale = 2000;
591
592	ret = regmap_write(chip->regmap, ISL29028_REG_TEST1_MODE, 0x0);
593	if (ret < 0) {
594		dev_err(&client->dev,
595			"%s(): Error %d writing to TEST1_MODE register\n",
596			__func__, ret);
597		return ret;
598	}
599
600	ret = regmap_write(chip->regmap, ISL29028_REG_TEST2_MODE, 0x0);
601	if (ret < 0) {
602		dev_err(&client->dev,
603			"%s(): Error %d writing to TEST2_MODE register\n",
604			__func__, ret);
605		return ret;
606	}
607
608	ret = isl29028_clear_configure_reg(chip);
609	if (ret < 0)
610		return ret;
611
612	indio_dev->info = &isl29028_info;
613	indio_dev->channels = isl29028_channels;
614	indio_dev->num_channels = ARRAY_SIZE(isl29028_channels);
615	indio_dev->name = id->name;
616	indio_dev->modes = INDIO_DIRECT_MODE;
617
618	pm_runtime_enable(&client->dev);
619	pm_runtime_set_autosuspend_delay(&client->dev,
620					 ISL29028_POWER_OFF_DELAY_MS);
621	pm_runtime_use_autosuspend(&client->dev);
622
623	ret = iio_device_register(indio_dev);
624	if (ret < 0) {
625		dev_err(&client->dev,
626			"%s(): iio registration failed with error %d\n",
627			__func__, ret);
628		return ret;
629	}
630
631	return 0;
632}
633
634static void isl29028_remove(struct i2c_client *client)
635{
636	struct iio_dev *indio_dev = i2c_get_clientdata(client);
637	struct isl29028_chip *chip = iio_priv(indio_dev);
638
639	iio_device_unregister(indio_dev);
640
641	pm_runtime_disable(&client->dev);
642	pm_runtime_set_suspended(&client->dev);
643
644	isl29028_clear_configure_reg(chip);
645}
646
647static int isl29028_suspend(struct device *dev)
648{
649	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
650	struct isl29028_chip *chip = iio_priv(indio_dev);
651	int ret;
652
653	mutex_lock(&chip->lock);
654
655	ret = isl29028_clear_configure_reg(chip);
656
657	mutex_unlock(&chip->lock);
658
659	return ret;
660}
661
662static int isl29028_resume(struct device *dev)
663{
664	/**
665	 * The specific component (ALS/IR or proximity) will enable itself as
666	 * needed the next time that the user requests a reading. This is done
667	 * above in isl29028_set_als_ir_mode() and isl29028_enable_proximity().
668	 */
669	return 0;
670}
671
672static DEFINE_RUNTIME_DEV_PM_OPS(isl29028_pm_ops, isl29028_suspend,
673				 isl29028_resume, NULL);
674
675static const struct i2c_device_id isl29028_id[] = {
676	{ "isl29028" },
677	{ "isl29030" },
678	{ }
679};
680MODULE_DEVICE_TABLE(i2c, isl29028_id);
681
682static const struct of_device_id isl29028_of_match[] = {
683	{ .compatible = "isl,isl29028", }, /* for backward compat., don't use */
684	{ .compatible = "isil,isl29028", },
685	{ .compatible = "isil,isl29030", },
686	{ }
687};
688MODULE_DEVICE_TABLE(of, isl29028_of_match);
689
690static struct i2c_driver isl29028_driver = {
691	.driver  = {
692		.name = "isl29028",
693		.pm = pm_ptr(&isl29028_pm_ops),
694		.of_match_table = isl29028_of_match,
695	},
696	.probe = isl29028_probe,
697	.remove  = isl29028_remove,
698	.id_table = isl29028_id,
699};
700
701module_i2c_driver(isl29028_driver);
702
703MODULE_DESCRIPTION("ISL29028 Ambient Light and Proximity Sensor driver");
704MODULE_LICENSE("GPL v2");
705MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
~/ Read-only file preview.