File Index
Files and directories
Sign in
FILE

apds9300.c

File preview with syntax highlighting.

linux-6.19.14/drivers/iio/light/apds9300.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * apds9300.c - IIO driver for Avago APDS9300 ambient light sensor
  4 *
  5 * Copyright 2013 Oleksandr Kravchenko <o.v.kravchenko@globallogic.com>
  6 */
  7
  8#include <linux/module.h>
  9#include <linux/slab.h>
 10#include <linux/pm.h>
 11#include <linux/i2c.h>
 12#include <linux/err.h>
 13#include <linux/mutex.h>
 14#include <linux/interrupt.h>
 15#include <linux/iio/iio.h>
 16#include <linux/iio/sysfs.h>
 17#include <linux/iio/events.h>
 18
 19#define APDS9300_DRV_NAME "apds9300"
 20
 21/* Command register bits */
 22#define APDS9300_CMD	BIT(7) /* Select command register. Must write as 1 */
 23#define APDS9300_WORD	BIT(5) /* I2C write/read: if 1 word, if 0 byte */
 24#define APDS9300_CLEAR	BIT(6) /* Interrupt clear. Clears pending interrupt */
 25
 26/* Register set */
 27#define APDS9300_CONTROL	0x00 /* Control of basic functions */
 28#define APDS9300_THRESHLOWLOW	0x02 /* Low byte of low interrupt threshold */
 29#define APDS9300_THRESHHIGHLOW	0x04 /* Low byte of high interrupt threshold */
 30#define APDS9300_INTERRUPT	0x06 /* Interrupt control */
 31#define APDS9300_DATA0LOW	0x0c /* Low byte of ADC channel 0 */
 32#define APDS9300_DATA1LOW	0x0e /* Low byte of ADC channel 1 */
 33
 34/* Power on/off value for APDS9300_CONTROL register */
 35#define APDS9300_POWER_ON	0x03
 36#define APDS9300_POWER_OFF	0x00
 37
 38/* Interrupts */
 39#define APDS9300_INTR_ENABLE	0x10
 40/* Interrupt Persist Function: Any value outside of threshold range */
 41#define APDS9300_THRESH_INTR	0x01
 42
 43#define APDS9300_THRESH_MAX	0xffff /* Max threshold value */
 44
 45struct apds9300_data {
 46	struct i2c_client *client;
 47	struct mutex mutex;
 48	bool power_state;
 49	int thresh_low;
 50	int thresh_hi;
 51	bool intr_en;
 52};
 53
 54/* Lux calculation */
 55
 56/* Calculated values 1000 * (CH1/CH0)^1.4 for CH1/CH0 from 0 to 0.52 */
 57static const u16 apds9300_lux_ratio[] = {
 58	0, 2, 4, 7, 11, 15, 19, 24, 29, 34, 40, 45, 51, 57, 64, 70, 77, 84, 91,
 59	98, 105, 112, 120, 128, 136, 144, 152, 160, 168, 177, 185, 194, 203,
 60	212, 221, 230, 239, 249, 258, 268, 277, 287, 297, 307, 317, 327, 337,
 61	347, 358, 368, 379, 390, 400,
 62};
 63
 64static unsigned long apds9300_calculate_lux(u16 ch0, u16 ch1)
 65{
 66	unsigned long lux, tmp;
 67
 68	/* avoid division by zero */
 69	if (ch0 == 0)
 70		return 0;
 71
 72	tmp = DIV_ROUND_UP(ch1 * 100, ch0);
 73	if (tmp <= 52) {
 74		lux = 3150 * ch0 - (unsigned long)DIV_ROUND_UP_ULL(ch0
 75				* apds9300_lux_ratio[tmp] * 5930ull, 1000);
 76	} else if (tmp <= 65) {
 77		lux = 2290 * ch0 - 2910 * ch1;
 78	} else if (tmp <= 80) {
 79		lux = 1570 * ch0 - 1800 * ch1;
 80	} else if (tmp <= 130) {
 81		lux = 338 * ch0 - 260 * ch1;
 82	} else {
 83		lux = 0;
 84	}
 85
 86	return lux / 100000;
 87}
 88
 89static int apds9300_get_adc_val(struct apds9300_data *data, int adc_number)
 90{
 91	int ret;
 92	u8 flags = APDS9300_CMD | APDS9300_WORD;
 93
 94	if (!data->power_state)
 95		return -EBUSY;
 96
 97	/* Select ADC0 or ADC1 data register */
 98	flags |= adc_number ? APDS9300_DATA1LOW : APDS9300_DATA0LOW;
 99
100	ret = i2c_smbus_read_word_data(data->client, flags);
101	if (ret < 0)
102		dev_err(&data->client->dev,
103			"failed to read ADC%d value\n", adc_number);
104
105	return ret;
106}
107
108static int apds9300_set_thresh_low(struct apds9300_data *data, int value)
109{
110	int ret;
111
112	if (!data->power_state)
113		return -EBUSY;
114
115	if (value > APDS9300_THRESH_MAX)
116		return -EINVAL;
117
118	ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHLOWLOW
119			| APDS9300_CMD | APDS9300_WORD, value);
120	if (ret) {
121		dev_err(&data->client->dev, "failed to set thresh_low\n");
122		return ret;
123	}
124	data->thresh_low = value;
125
126	return 0;
127}
128
129static int apds9300_set_thresh_hi(struct apds9300_data *data, int value)
130{
131	int ret;
132
133	if (!data->power_state)
134		return -EBUSY;
135
136	if (value > APDS9300_THRESH_MAX)
137		return -EINVAL;
138
139	ret = i2c_smbus_write_word_data(data->client, APDS9300_THRESHHIGHLOW
140			| APDS9300_CMD | APDS9300_WORD, value);
141	if (ret) {
142		dev_err(&data->client->dev, "failed to set thresh_hi\n");
143		return ret;
144	}
145	data->thresh_hi = value;
146
147	return 0;
148}
149
150static int apds9300_set_intr_state(struct apds9300_data *data, bool state)
151{
152	int ret;
153	u8 cmd;
154
155	if (!data->power_state)
156		return -EBUSY;
157
158	cmd = state ? APDS9300_INTR_ENABLE | APDS9300_THRESH_INTR : 0x00;
159	ret = i2c_smbus_write_byte_data(data->client,
160			APDS9300_INTERRUPT | APDS9300_CMD, cmd);
161	if (ret) {
162		dev_err(&data->client->dev,
163			"failed to set interrupt state %d\n", state);
164		return ret;
165	}
166	data->intr_en = state;
167
168	return 0;
169}
170
171static int apds9300_set_power_state(struct apds9300_data *data, bool state)
172{
173	int ret;
174	u8 cmd;
175
176	cmd = state ? APDS9300_POWER_ON : APDS9300_POWER_OFF;
177	ret = i2c_smbus_write_byte_data(data->client,
178			APDS9300_CONTROL | APDS9300_CMD, cmd);
179	if (ret) {
180		dev_err(&data->client->dev,
181			"failed to set power state %d\n", state);
182		return ret;
183	}
184	data->power_state = state;
185
186	return 0;
187}
188
189static void apds9300_clear_intr(struct apds9300_data *data)
190{
191	int ret;
192
193	ret = i2c_smbus_write_byte(data->client, APDS9300_CLEAR | APDS9300_CMD);
194	if (ret < 0)
195		dev_err(&data->client->dev, "failed to clear interrupt\n");
196}
197
198static int apds9300_chip_init(struct apds9300_data *data)
199{
200	int ret;
201
202	/* Need to set power off to ensure that the chip is off */
203	ret = apds9300_set_power_state(data, 0);
204	if (ret < 0)
205		goto err;
206	/*
207	 * Probe the chip. To do so we try to power up the device and then to
208	 * read back the 0x03 code
209	 */
210	ret = apds9300_set_power_state(data, 1);
211	if (ret < 0)
212		goto err;
213	ret = i2c_smbus_read_byte_data(data->client,
214			APDS9300_CONTROL | APDS9300_CMD);
215	if (ret != APDS9300_POWER_ON) {
216		ret = -ENODEV;
217		goto err;
218	}
219	/*
220	 * Disable interrupt to ensure thai it is doesn't enable
221	 * i.e. after device soft reset
222	 */
223	ret = apds9300_set_intr_state(data, false);
224	if (ret < 0)
225		goto err;
226
227	return 0;
228
229err:
230	dev_err(&data->client->dev, "failed to init the chip\n");
231	return ret;
232}
233
234static int apds9300_read_raw(struct iio_dev *indio_dev,
235		struct iio_chan_spec const *chan, int *val, int *val2,
236		long mask)
237{
238	int ch0, ch1, ret = -EINVAL;
239	struct apds9300_data *data = iio_priv(indio_dev);
240
241	mutex_lock(&data->mutex);
242	switch (chan->type) {
243	case IIO_LIGHT:
244		ch0 = apds9300_get_adc_val(data, 0);
245		if (ch0 < 0) {
246			ret = ch0;
247			break;
248		}
249		ch1 = apds9300_get_adc_val(data, 1);
250		if (ch1 < 0) {
251			ret = ch1;
252			break;
253		}
254		*val = apds9300_calculate_lux(ch0, ch1);
255		ret = IIO_VAL_INT;
256		break;
257	case IIO_INTENSITY:
258		ret = apds9300_get_adc_val(data, chan->channel);
259		if (ret < 0)
260			break;
261		*val = ret;
262		ret = IIO_VAL_INT;
263		break;
264	default:
265		break;
266	}
267	mutex_unlock(&data->mutex);
268
269	return ret;
270}
271
272static int apds9300_read_thresh(struct iio_dev *indio_dev,
273		const struct iio_chan_spec *chan, enum iio_event_type type,
274		enum iio_event_direction dir, enum iio_event_info info,
275		int *val, int *val2)
276{
277	struct apds9300_data *data = iio_priv(indio_dev);
278
279	switch (dir) {
280	case IIO_EV_DIR_RISING:
281		*val = data->thresh_hi;
282		break;
283	case IIO_EV_DIR_FALLING:
284		*val = data->thresh_low;
285		break;
286	default:
287		return -EINVAL;
288	}
289
290	return IIO_VAL_INT;
291}
292
293static int apds9300_write_thresh(struct iio_dev *indio_dev,
294		const struct iio_chan_spec *chan, enum iio_event_type type,
295		enum iio_event_direction dir, enum iio_event_info info, int val,
296		int val2)
297{
298	struct apds9300_data *data = iio_priv(indio_dev);
299	int ret;
300
301	mutex_lock(&data->mutex);
302	if (dir == IIO_EV_DIR_RISING)
303		ret = apds9300_set_thresh_hi(data, val);
304	else
305		ret = apds9300_set_thresh_low(data, val);
306	mutex_unlock(&data->mutex);
307
308	return ret;
309}
310
311static int apds9300_read_interrupt_config(struct iio_dev *indio_dev,
312		const struct iio_chan_spec *chan,
313		enum iio_event_type type,
314		enum iio_event_direction dir)
315{
316	struct apds9300_data *data = iio_priv(indio_dev);
317
318	return data->intr_en;
319}
320
321static int apds9300_write_interrupt_config(struct iio_dev *indio_dev,
322		const struct iio_chan_spec *chan, enum iio_event_type type,
323		enum iio_event_direction dir, bool state)
324{
325	struct apds9300_data *data = iio_priv(indio_dev);
326	int ret;
327
328	mutex_lock(&data->mutex);
329	ret = apds9300_set_intr_state(data, state);
330	mutex_unlock(&data->mutex);
331
332	return ret;
333}
334
335static const struct iio_info apds9300_info_no_irq = {
336	.read_raw	= apds9300_read_raw,
337};
338
339static const struct iio_info apds9300_info = {
340	.read_raw		= apds9300_read_raw,
341	.read_event_value	= apds9300_read_thresh,
342	.write_event_value	= apds9300_write_thresh,
343	.read_event_config	= apds9300_read_interrupt_config,
344	.write_event_config	= apds9300_write_interrupt_config,
345};
346
347static const struct iio_event_spec apds9300_event_spec[] = {
348	{
349		.type = IIO_EV_TYPE_THRESH,
350		.dir = IIO_EV_DIR_RISING,
351		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
352			BIT(IIO_EV_INFO_ENABLE),
353	}, {
354		.type = IIO_EV_TYPE_THRESH,
355		.dir = IIO_EV_DIR_FALLING,
356		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
357			BIT(IIO_EV_INFO_ENABLE),
358	},
359};
360
361static const struct iio_chan_spec apds9300_channels[] = {
362	{
363		.type = IIO_LIGHT,
364		.channel = 0,
365		.indexed = true,
366		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
367	}, {
368		.type = IIO_INTENSITY,
369		.channel = 0,
370		.channel2 = IIO_MOD_LIGHT_BOTH,
371		.indexed = true,
372		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
373		.event_spec = apds9300_event_spec,
374		.num_event_specs = ARRAY_SIZE(apds9300_event_spec),
375	}, {
376		.type = IIO_INTENSITY,
377		.channel = 1,
378		.channel2 = IIO_MOD_LIGHT_IR,
379		.indexed = true,
380		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
381	},
382};
383
384static irqreturn_t apds9300_interrupt_handler(int irq, void *private)
385{
386	struct iio_dev *dev_info = private;
387	struct apds9300_data *data = iio_priv(dev_info);
388
389	iio_push_event(dev_info,
390		       IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
391					    IIO_EV_TYPE_THRESH,
392					    IIO_EV_DIR_EITHER),
393		       iio_get_time_ns(dev_info));
394
395	apds9300_clear_intr(data);
396
397	return IRQ_HANDLED;
398}
399
400static int apds9300_probe(struct i2c_client *client)
401{
402	struct apds9300_data *data;
403	struct iio_dev *indio_dev;
404	int ret;
405
406	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
407	if (!indio_dev)
408		return -ENOMEM;
409
410	data = iio_priv(indio_dev);
411	i2c_set_clientdata(client, indio_dev);
412	data->client = client;
413
414	ret = apds9300_chip_init(data);
415	if (ret < 0)
416		goto err;
417
418	mutex_init(&data->mutex);
419
420	indio_dev->channels = apds9300_channels;
421	indio_dev->num_channels = ARRAY_SIZE(apds9300_channels);
422	indio_dev->name = APDS9300_DRV_NAME;
423	indio_dev->modes = INDIO_DIRECT_MODE;
424
425	if (client->irq)
426		indio_dev->info = &apds9300_info;
427	else
428		indio_dev->info = &apds9300_info_no_irq;
429
430	if (client->irq) {
431		ret = devm_request_threaded_irq(&client->dev, client->irq,
432				NULL, apds9300_interrupt_handler,
433				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
434				"apds9300_event", indio_dev);
435		if (ret) {
436			dev_err(&client->dev, "irq request error %d\n", -ret);
437			goto err;
438		}
439	}
440
441	ret = iio_device_register(indio_dev);
442	if (ret < 0)
443		goto err;
444
445	return 0;
446
447err:
448	/* Ensure that power off in case of error */
449	apds9300_set_power_state(data, 0);
450	return ret;
451}
452
453static void apds9300_remove(struct i2c_client *client)
454{
455	struct iio_dev *indio_dev = i2c_get_clientdata(client);
456	struct apds9300_data *data = iio_priv(indio_dev);
457
458	iio_device_unregister(indio_dev);
459
460	/* Ensure that power off and interrupts are disabled */
461	apds9300_set_intr_state(data, false);
462	apds9300_set_power_state(data, false);
463}
464
465static int apds9300_suspend(struct device *dev)
466{
467	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
468	struct apds9300_data *data = iio_priv(indio_dev);
469	int ret;
470
471	mutex_lock(&data->mutex);
472	ret = apds9300_set_power_state(data, false);
473	mutex_unlock(&data->mutex);
474
475	return ret;
476}
477
478static int apds9300_resume(struct device *dev)
479{
480	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
481	struct apds9300_data *data = iio_priv(indio_dev);
482	int ret;
483
484	mutex_lock(&data->mutex);
485	ret = apds9300_set_power_state(data, true);
486	mutex_unlock(&data->mutex);
487
488	return ret;
489}
490
491static DEFINE_SIMPLE_DEV_PM_OPS(apds9300_pm_ops, apds9300_suspend,
492				apds9300_resume);
493
494static const struct i2c_device_id apds9300_id[] = {
495	{ APDS9300_DRV_NAME },
496	{ }
497};
498
499MODULE_DEVICE_TABLE(i2c, apds9300_id);
500
501static struct i2c_driver apds9300_driver = {
502	.driver = {
503		.name	= APDS9300_DRV_NAME,
504		.pm	= pm_sleep_ptr(&apds9300_pm_ops),
505	},
506	.probe		= apds9300_probe,
507	.remove		= apds9300_remove,
508	.id_table	= apds9300_id,
509};
510
511module_i2c_driver(apds9300_driver);
512
513MODULE_AUTHOR("Kravchenko Oleksandr <o.v.kravchenko@globallogic.com>");
514MODULE_AUTHOR("GlobalLogic inc.");
515MODULE_DESCRIPTION("APDS9300 ambient light photo sensor driver");
516MODULE_LICENSE("GPL");
~/ Read-only file preview.