Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * ACPI Ambient Light Sensor Driver
  4 *
  5 * Based on ALS driver:
  6 * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com>
  7 *
  8 * Rework for IIO subsystem:
  9 * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com>
 10 *
 11 * Final cleanup and debugging:
 12 * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de>
 13 * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 14 */
 15
 16#include <linux/module.h>
 17#include <linux/acpi.h>
 18#include <linux/err.h>
 19#include <linux/irq.h>
 20#include <linux/mutex.h>
 21
 22#include <linux/iio/iio.h>
 23#include <linux/iio/buffer.h>
 24#include <linux/iio/trigger.h>
 25#include <linux/iio/triggered_buffer.h>
 26#include <linux/iio/trigger_consumer.h>
 27
 28#define ACPI_ALS_CLASS			"als"
 29#define ACPI_ALS_DEVICE_NAME		"acpi-als"
 30#define ACPI_ALS_NOTIFY_ILLUMINANCE	0x80
 31
 
 
 32/*
 33 * So far, there's only one channel in here, but the specification for
 34 * ACPI0008 says there can be more to what the block can report. Like
 35 * chromaticity and such. We are ready for incoming additions!
 36 */
 37static const struct iio_chan_spec acpi_als_channels[] = {
 38	{
 39		.type		= IIO_LIGHT,
 40		.scan_type	= {
 41			.sign		= 's',
 42			.realbits	= 32,
 43			.storagebits	= 32,
 44		},
 45		/* _RAW is here for backward ABI compatibility */
 46		.info_mask_separate	= BIT(IIO_CHAN_INFO_RAW) |
 47					  BIT(IIO_CHAN_INFO_PROCESSED),
 48	},
 49	IIO_CHAN_SOFT_TIMESTAMP(1),
 50};
 51
 52/*
 53 * The event buffer contains timestamp and all the data from
 54 * the ACPI0008 block. There are multiple, but so far we only
 55 * support _ALI (illuminance): One channel, padding and timestamp.
 
 
 56 */
 
 57#define ACPI_ALS_EVT_BUFFER_SIZE		\
 58	(sizeof(s32) + sizeof(s32) + sizeof(s64))
 59
 60struct acpi_als {
 61	struct acpi_device	*device;
 62	struct mutex		lock;
 63	struct iio_trigger	*trig;
 64
 65	s32 evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE / sizeof(s32)]  __aligned(8);
 66};
 67
 68/*
 69 * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
 70 * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
 71 * special.
 72 *
 73 * The _ALR property returns tables that can be used to fine-tune the values
 74 * reported by the other props based on the particular hardware type and it's
 75 * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
 76 *
 77 * So far, we support only ALI (illuminance).
 78 */
 79#define ACPI_ALS_ILLUMINANCE	"_ALI"
 80#define ACPI_ALS_CHROMATICITY	"_ALC"
 81#define ACPI_ALS_COLOR_TEMP	"_ALT"
 82#define ACPI_ALS_POLLING	"_ALP"
 83#define ACPI_ALS_TABLES		"_ALR"
 84
 85static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
 86{
 87	unsigned long long temp_val;
 88	acpi_status status;
 89
 90	status = acpi_evaluate_integer(als->device->handle, prop, NULL,
 91				       &temp_val);
 92
 93	if (ACPI_FAILURE(status)) {
 94		acpi_evaluation_failure_warn(als->device->handle, prop, status);
 95		return -EIO;
 96	}
 97
 98	*val = temp_val;
 99
100	return 0;
101}
102
103static void acpi_als_notify(struct acpi_device *device, u32 event)
104{
105	struct iio_dev *indio_dev = acpi_driver_data(device);
106	struct acpi_als *als = iio_priv(indio_dev);
 
 
 
 
 
 
 
 
107
108	if (iio_buffer_enabled(indio_dev) && iio_trigger_using_own(indio_dev)) {
109		switch (event) {
110		case ACPI_ALS_NOTIFY_ILLUMINANCE:
111			iio_trigger_poll_nested(als->trig);
112			break;
113		default:
114			/* Unhandled event */
115			dev_dbg(&device->dev,
116				"Unhandled ACPI ALS event (%08x)!\n",
117				event);
118		}
 
119	}
 
 
 
 
 
120}
121
122static int acpi_als_read_raw(struct iio_dev *indio_dev,
123			     struct iio_chan_spec const *chan, int *val,
124			     int *val2, long mask)
125{
126	struct acpi_als *als = iio_priv(indio_dev);
127	s32 temp_val;
128	int ret;
129
130	if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
131		return -EINVAL;
132
133	/* we support only illumination (_ALI) so far. */
134	if (chan->type != IIO_LIGHT)
135		return -EINVAL;
136
137	ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
138	if (ret < 0)
139		return ret;
140
141	*val = temp_val;
142
143	return IIO_VAL_INT;
144}
145
146static const struct iio_info acpi_als_info = {
147	.read_raw		= acpi_als_read_raw,
148};
149
150static irqreturn_t acpi_als_trigger_handler(int irq, void *p)
151{
152	struct iio_poll_func *pf = p;
153	struct iio_dev *indio_dev = pf->indio_dev;
154	struct acpi_als *als = iio_priv(indio_dev);
155	s32 *buffer = als->evt_buffer;
156	s32 val;
157	int ret;
158
159	mutex_lock(&als->lock);
160
161	ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
162	if (ret < 0)
163		goto out;
164	*buffer = val;
165
166	/*
167	 * When coming from own trigger via polls, set polling function
168	 * timestamp here. Given ACPI notifier is already in a thread and call
169	 * function directly, there is no need to set the timestamp in the
170	 * notify function.
171	 *
172	 * If the timestamp was actually 0, the timestamp is set one more time.
173	 */
174	if (!pf->timestamp)
175		pf->timestamp = iio_get_time_ns(indio_dev);
176
177	iio_push_to_buffers_with_timestamp(indio_dev, buffer, pf->timestamp);
178out:
179	mutex_unlock(&als->lock);
180	iio_trigger_notify_done(indio_dev->trig);
181
182	return IRQ_HANDLED;
183}
184
185static int acpi_als_add(struct acpi_device *device)
186{
187	struct device *dev = &device->dev;
188	struct iio_dev *indio_dev;
189	struct acpi_als *als;
190	int ret;
 
191
192	indio_dev = devm_iio_device_alloc(dev, sizeof(*als));
193	if (!indio_dev)
194		return -ENOMEM;
195
196	als = iio_priv(indio_dev);
197
198	device->driver_data = indio_dev;
199	als->device = device;
200	mutex_init(&als->lock);
201
202	indio_dev->name = ACPI_ALS_DEVICE_NAME;
 
203	indio_dev->info = &acpi_als_info;
 
204	indio_dev->channels = acpi_als_channels;
205	indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
206
207	als->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", indio_dev->name,
208					   iio_device_id(indio_dev));
209	if (!als->trig)
210		return -ENOMEM;
211
212	ret = devm_iio_trigger_register(dev, als->trig);
213	if (ret)
214		return ret;
215	/*
216	 * Set hardware trigger by default to let events flow when
217	 * BIOS support notification.
218	 */
219	indio_dev->trig = iio_trigger_get(als->trig);
220
221	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
222					      iio_pollfunc_store_time,
223					      acpi_als_trigger_handler,
224					      NULL);
225	if (ret)
226		return ret;
227
228	return devm_iio_device_register(dev, indio_dev);
229}
230
231static const struct acpi_device_id acpi_als_device_ids[] = {
232	{"ACPI0008", 0},
233	{},
234};
235
236MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
237
238static struct acpi_driver acpi_als_driver = {
239	.name	= "acpi_als",
240	.class	= ACPI_ALS_CLASS,
241	.ids	= acpi_als_device_ids,
242	.ops = {
243		.add	= acpi_als_add,
244		.notify	= acpi_als_notify,
245	},
246};
247
248module_acpi_driver(acpi_als_driver);
249
250MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
251MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
252MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
253MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
254MODULE_LICENSE("GPL");
v4.17
 
  1/*
  2 * ACPI Ambient Light Sensor Driver
  3 *
  4 * Based on ALS driver:
  5 * Copyright (C) 2009 Zhang Rui <rui.zhang@intel.com>
  6 *
  7 * Rework for IIO subsystem:
  8 * Copyright (C) 2012-2013 Martin Liska <marxin.liska@gmail.com>
  9 *
 10 * Final cleanup and debugging:
 11 * Copyright (C) 2013-2014 Marek Vasut <marex@denx.de>
 12 * Copyright (C) 2015 Gabriele Mazzotta <gabriele.mzt@gmail.com>
 13 *
 14 * This program is free software; you can redistribute it and/or modify it
 15 * under the terms of the GNU General Public License as published by the
 16 * Free Software Foundation; either version 2 of the License, or (at your
 17 * option) any later version.
 18 *
 19 * This program is distributed in the hope that it will be useful, but
 20 * WITHOUT ANY WARRANTY; without even the implied warranty of
 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 22 * General Public License for more details.
 23 *
 24 * You should have received a copy of the GNU General Public License along
 25 * with this program; if not, write to the Free Software Foundation, Inc.,
 26 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 27 */
 28
 29#include <linux/module.h>
 30#include <linux/acpi.h>
 31#include <linux/err.h>
 
 32#include <linux/mutex.h>
 33
 34#include <linux/iio/iio.h>
 35#include <linux/iio/buffer.h>
 36#include <linux/iio/kfifo_buf.h>
 
 
 37
 38#define ACPI_ALS_CLASS			"als"
 39#define ACPI_ALS_DEVICE_NAME		"acpi-als"
 40#define ACPI_ALS_NOTIFY_ILLUMINANCE	0x80
 41
 42ACPI_MODULE_NAME("acpi-als");
 43
 44/*
 45 * So far, there's only one channel in here, but the specification for
 46 * ACPI0008 says there can be more to what the block can report. Like
 47 * chromaticity and such. We are ready for incoming additions!
 48 */
 49static const struct iio_chan_spec acpi_als_channels[] = {
 50	{
 51		.type		= IIO_LIGHT,
 52		.scan_type	= {
 53			.sign		= 's',
 54			.realbits	= 32,
 55			.storagebits	= 32,
 56		},
 57		/* _RAW is here for backward ABI compatibility */
 58		.info_mask_separate	= BIT(IIO_CHAN_INFO_RAW) |
 59					  BIT(IIO_CHAN_INFO_PROCESSED),
 60	},
 
 61};
 62
 63/*
 64 * The event buffer contains timestamp and all the data from
 65 * the ACPI0008 block. There are multiple, but so far we only
 66 * support _ALI (illuminance). Once someone adds new channels
 67 * to acpi_als_channels[], the evt_buffer below will grow
 68 * automatically.
 69 */
 70#define ACPI_ALS_EVT_NR_SOURCES		ARRAY_SIZE(acpi_als_channels)
 71#define ACPI_ALS_EVT_BUFFER_SIZE		\
 72	(sizeof(s64) + (ACPI_ALS_EVT_NR_SOURCES * sizeof(s32)))
 73
 74struct acpi_als {
 75	struct acpi_device	*device;
 76	struct mutex		lock;
 
 77
 78	s32			evt_buffer[ACPI_ALS_EVT_BUFFER_SIZE];
 79};
 80
 81/*
 82 * All types of properties the ACPI0008 block can report. The ALI, ALC, ALT
 83 * and ALP can all be handled by acpi_als_read_value() below, while the ALR is
 84 * special.
 85 *
 86 * The _ALR property returns tables that can be used to fine-tune the values
 87 * reported by the other props based on the particular hardware type and it's
 88 * location (it contains tables for "rainy", "bright inhouse lighting" etc.).
 89 *
 90 * So far, we support only ALI (illuminance).
 91 */
 92#define ACPI_ALS_ILLUMINANCE	"_ALI"
 93#define ACPI_ALS_CHROMATICITY	"_ALC"
 94#define ACPI_ALS_COLOR_TEMP	"_ALT"
 95#define ACPI_ALS_POLLING	"_ALP"
 96#define ACPI_ALS_TABLES		"_ALR"
 97
 98static int acpi_als_read_value(struct acpi_als *als, char *prop, s32 *val)
 99{
100	unsigned long long temp_val;
101	acpi_status status;
102
103	status = acpi_evaluate_integer(als->device->handle, prop, NULL,
104				       &temp_val);
105
106	if (ACPI_FAILURE(status)) {
107		ACPI_EXCEPTION((AE_INFO, status, "Error reading ALS %s", prop));
108		return -EIO;
109	}
110
111	*val = temp_val;
112
113	return 0;
114}
115
116static void acpi_als_notify(struct acpi_device *device, u32 event)
117{
118	struct iio_dev *indio_dev = acpi_driver_data(device);
119	struct acpi_als *als = iio_priv(indio_dev);
120	s32 *buffer = als->evt_buffer;
121	s64 time_ns = iio_get_time_ns(indio_dev);
122	s32 val;
123	int ret;
124
125	mutex_lock(&als->lock);
126
127	memset(buffer, 0, ACPI_ALS_EVT_BUFFER_SIZE);
128
129	switch (event) {
130	case ACPI_ALS_NOTIFY_ILLUMINANCE:
131		ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &val);
132		if (ret < 0)
133			goto out;
134		*buffer++ = val;
135		break;
136	default:
137		/* Unhandled event */
138		dev_dbg(&device->dev, "Unhandled ACPI ALS event (%08x)!\n",
139			event);
140		goto out;
141	}
142
143	iio_push_to_buffers_with_timestamp(indio_dev, als->evt_buffer, time_ns);
144
145out:
146	mutex_unlock(&als->lock);
147}
148
149static int acpi_als_read_raw(struct iio_dev *indio_dev,
150			     struct iio_chan_spec const *chan, int *val,
151			     int *val2, long mask)
152{
153	struct acpi_als *als = iio_priv(indio_dev);
154	s32 temp_val;
155	int ret;
156
157	if ((mask != IIO_CHAN_INFO_PROCESSED) && (mask != IIO_CHAN_INFO_RAW))
158		return -EINVAL;
159
160	/* we support only illumination (_ALI) so far. */
161	if (chan->type != IIO_LIGHT)
162		return -EINVAL;
163
164	ret = acpi_als_read_value(als, ACPI_ALS_ILLUMINANCE, &temp_val);
165	if (ret < 0)
166		return ret;
167
168	*val = temp_val;
169
170	return IIO_VAL_INT;
171}
172
173static const struct iio_info acpi_als_info = {
174	.read_raw		= acpi_als_read_raw,
175};
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177static int acpi_als_add(struct acpi_device *device)
178{
 
 
179	struct acpi_als *als;
180	struct iio_dev *indio_dev;
181	struct iio_buffer *buffer;
182
183	indio_dev = devm_iio_device_alloc(&device->dev, sizeof(*als));
184	if (!indio_dev)
185		return -ENOMEM;
186
187	als = iio_priv(indio_dev);
188
189	device->driver_data = indio_dev;
190	als->device = device;
191	mutex_init(&als->lock);
192
193	indio_dev->name = ACPI_ALS_DEVICE_NAME;
194	indio_dev->dev.parent = &device->dev;
195	indio_dev->info = &acpi_als_info;
196	indio_dev->modes = INDIO_BUFFER_SOFTWARE;
197	indio_dev->channels = acpi_als_channels;
198	indio_dev->num_channels = ARRAY_SIZE(acpi_als_channels);
199
200	buffer = devm_iio_kfifo_allocate(&device->dev);
201	if (!buffer)
 
202		return -ENOMEM;
203
204	iio_device_attach_buffer(indio_dev, buffer);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
206	return devm_iio_device_register(&device->dev, indio_dev);
207}
208
209static const struct acpi_device_id acpi_als_device_ids[] = {
210	{"ACPI0008", 0},
211	{},
212};
213
214MODULE_DEVICE_TABLE(acpi, acpi_als_device_ids);
215
216static struct acpi_driver acpi_als_driver = {
217	.name	= "acpi_als",
218	.class	= ACPI_ALS_CLASS,
219	.ids	= acpi_als_device_ids,
220	.ops = {
221		.add	= acpi_als_add,
222		.notify	= acpi_als_notify,
223	},
224};
225
226module_acpi_driver(acpi_als_driver);
227
228MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
229MODULE_AUTHOR("Martin Liska <marxin.liska@gmail.com>");
230MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
231MODULE_DESCRIPTION("ACPI Ambient Light Sensor Driver");
232MODULE_LICENSE("GPL");