18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2011 Jonathan Cameron 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Event handling elements of industrial I/O reference driver. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/kernel.h> 88c2ecf20Sopenharmony_ci#include <linux/slab.h> 98c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 108c2ecf20Sopenharmony_ci#include <linux/irq.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 138c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 148c2ecf20Sopenharmony_ci#include <linux/iio/events.h> 158c2ecf20Sopenharmony_ci#include "iio_simple_dummy.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* Evgen 'fakes' interrupt events for this example */ 188c2ecf20Sopenharmony_ci#include "iio_dummy_evgen.h" 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/** 218c2ecf20Sopenharmony_ci * iio_simple_dummy_read_event_config() - is event enabled? 228c2ecf20Sopenharmony_ci * @indio_dev: the device instance data 238c2ecf20Sopenharmony_ci * @chan: channel for the event whose state is being queried 248c2ecf20Sopenharmony_ci * @type: type of the event whose state is being queried 258c2ecf20Sopenharmony_ci * @dir: direction of the vent whose state is being queried 268c2ecf20Sopenharmony_ci * 278c2ecf20Sopenharmony_ci * This function would normally query the relevant registers or a cache to 288c2ecf20Sopenharmony_ci * discover if the event generation is enabled on the device. 298c2ecf20Sopenharmony_ci */ 308c2ecf20Sopenharmony_ciint iio_simple_dummy_read_event_config(struct iio_dev *indio_dev, 318c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 328c2ecf20Sopenharmony_ci enum iio_event_type type, 338c2ecf20Sopenharmony_ci enum iio_event_direction dir) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci return st->event_en; 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci/** 418c2ecf20Sopenharmony_ci * iio_simple_dummy_write_event_config() - set whether event is enabled 428c2ecf20Sopenharmony_ci * @indio_dev: the device instance data 438c2ecf20Sopenharmony_ci * @chan: channel for the event whose state is being set 448c2ecf20Sopenharmony_ci * @type: type of the event whose state is being set 458c2ecf20Sopenharmony_ci * @dir: direction of the vent whose state is being set 468c2ecf20Sopenharmony_ci * @state: whether to enable or disable the device. 478c2ecf20Sopenharmony_ci * 488c2ecf20Sopenharmony_ci * This function would normally set the relevant registers on the devices 498c2ecf20Sopenharmony_ci * so that it generates the specified event. Here it just sets up a cached 508c2ecf20Sopenharmony_ci * value. 518c2ecf20Sopenharmony_ci */ 528c2ecf20Sopenharmony_ciint iio_simple_dummy_write_event_config(struct iio_dev *indio_dev, 538c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 548c2ecf20Sopenharmony_ci enum iio_event_type type, 558c2ecf20Sopenharmony_ci enum iio_event_direction dir, 568c2ecf20Sopenharmony_ci int state) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci /* 618c2ecf20Sopenharmony_ci * Deliberately over the top code splitting to illustrate 628c2ecf20Sopenharmony_ci * how this is done when multiple events exist. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ci switch (chan->type) { 658c2ecf20Sopenharmony_ci case IIO_VOLTAGE: 668c2ecf20Sopenharmony_ci switch (type) { 678c2ecf20Sopenharmony_ci case IIO_EV_TYPE_THRESH: 688c2ecf20Sopenharmony_ci if (dir == IIO_EV_DIR_RISING) 698c2ecf20Sopenharmony_ci st->event_en = state; 708c2ecf20Sopenharmony_ci else 718c2ecf20Sopenharmony_ci return -EINVAL; 728c2ecf20Sopenharmony_ci break; 738c2ecf20Sopenharmony_ci default: 748c2ecf20Sopenharmony_ci return -EINVAL; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci break; 778c2ecf20Sopenharmony_ci case IIO_ACTIVITY: 788c2ecf20Sopenharmony_ci switch (type) { 798c2ecf20Sopenharmony_ci case IIO_EV_TYPE_THRESH: 808c2ecf20Sopenharmony_ci st->event_en = state; 818c2ecf20Sopenharmony_ci break; 828c2ecf20Sopenharmony_ci default: 838c2ecf20Sopenharmony_ci return -EINVAL; 848c2ecf20Sopenharmony_ci } 858c2ecf20Sopenharmony_ci break; 868c2ecf20Sopenharmony_ci case IIO_STEPS: 878c2ecf20Sopenharmony_ci switch (type) { 888c2ecf20Sopenharmony_ci case IIO_EV_TYPE_CHANGE: 898c2ecf20Sopenharmony_ci st->event_en = state; 908c2ecf20Sopenharmony_ci break; 918c2ecf20Sopenharmony_ci default: 928c2ecf20Sopenharmony_ci return -EINVAL; 938c2ecf20Sopenharmony_ci } 948c2ecf20Sopenharmony_ci break; 958c2ecf20Sopenharmony_ci default: 968c2ecf20Sopenharmony_ci return -EINVAL; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return 0; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci/** 1038c2ecf20Sopenharmony_ci * iio_simple_dummy_read_event_value() - get value associated with event 1048c2ecf20Sopenharmony_ci * @indio_dev: device instance specific data 1058c2ecf20Sopenharmony_ci * @chan: channel for the event whose value is being read 1068c2ecf20Sopenharmony_ci * @type: type of the event whose value is being read 1078c2ecf20Sopenharmony_ci * @dir: direction of the vent whose value is being read 1088c2ecf20Sopenharmony_ci * @info: info type of the event whose value is being read 1098c2ecf20Sopenharmony_ci * @val: value for the event code. 1108c2ecf20Sopenharmony_ci * @val2: unused 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * Many devices provide a large set of events of which only a subset may 1138c2ecf20Sopenharmony_ci * be enabled at a time, with value registers whose meaning changes depending 1148c2ecf20Sopenharmony_ci * on the event enabled. This often means that the driver must cache the values 1158c2ecf20Sopenharmony_ci * associated with each possible events so that the right value is in place when 1168c2ecf20Sopenharmony_ci * the enabled event is changed. 1178c2ecf20Sopenharmony_ci */ 1188c2ecf20Sopenharmony_ciint iio_simple_dummy_read_event_value(struct iio_dev *indio_dev, 1198c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 1208c2ecf20Sopenharmony_ci enum iio_event_type type, 1218c2ecf20Sopenharmony_ci enum iio_event_direction dir, 1228c2ecf20Sopenharmony_ci enum iio_event_info info, 1238c2ecf20Sopenharmony_ci int *val, int *val2) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci *val = st->event_val; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return IIO_VAL_INT; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/** 1338c2ecf20Sopenharmony_ci * iio_simple_dummy_write_event_value() - set value associate with event 1348c2ecf20Sopenharmony_ci * @indio_dev: device instance specific data 1358c2ecf20Sopenharmony_ci * @chan: channel for the event whose value is being set 1368c2ecf20Sopenharmony_ci * @type: type of the event whose value is being set 1378c2ecf20Sopenharmony_ci * @dir: direction of the vent whose value is being set 1388c2ecf20Sopenharmony_ci * @info: info type of the event whose value is being set 1398c2ecf20Sopenharmony_ci * @val: the value to be set. 1408c2ecf20Sopenharmony_ci * @val2: unused 1418c2ecf20Sopenharmony_ci */ 1428c2ecf20Sopenharmony_ciint iio_simple_dummy_write_event_value(struct iio_dev *indio_dev, 1438c2ecf20Sopenharmony_ci const struct iio_chan_spec *chan, 1448c2ecf20Sopenharmony_ci enum iio_event_type type, 1458c2ecf20Sopenharmony_ci enum iio_event_direction dir, 1468c2ecf20Sopenharmony_ci enum iio_event_info info, 1478c2ecf20Sopenharmony_ci int val, int val2) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci st->event_val = val; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci return 0; 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic irqreturn_t iio_simple_dummy_get_timestamp(int irq, void *private) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 1598c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci st->event_timestamp = iio_get_time_ns(indio_dev); 1628c2ecf20Sopenharmony_ci return IRQ_WAKE_THREAD; 1638c2ecf20Sopenharmony_ci} 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/** 1668c2ecf20Sopenharmony_ci * iio_simple_dummy_event_handler() - identify and pass on event 1678c2ecf20Sopenharmony_ci * @irq: irq of event line 1688c2ecf20Sopenharmony_ci * @private: pointer to device instance state. 1698c2ecf20Sopenharmony_ci * 1708c2ecf20Sopenharmony_ci * This handler is responsible for querying the device to find out what 1718c2ecf20Sopenharmony_ci * event occurred and for then pushing that event towards userspace. 1728c2ecf20Sopenharmony_ci * Here only one event occurs so we push that directly on with locally 1738c2ecf20Sopenharmony_ci * grabbed timestamp. 1748c2ecf20Sopenharmony_ci */ 1758c2ecf20Sopenharmony_cistatic irqreturn_t iio_simple_dummy_event_handler(int irq, void *private) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = private; 1788c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci dev_dbg(&indio_dev->dev, "id %x event %x\n", 1818c2ecf20Sopenharmony_ci st->regs->reg_id, st->regs->reg_data); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci switch (st->regs->reg_data) { 1848c2ecf20Sopenharmony_ci case 0: 1858c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 1868c2ecf20Sopenharmony_ci IIO_EVENT_CODE(IIO_VOLTAGE, 0, 0, 1878c2ecf20Sopenharmony_ci IIO_EV_DIR_RISING, 1888c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 0, 0, 0), 1898c2ecf20Sopenharmony_ci st->event_timestamp); 1908c2ecf20Sopenharmony_ci break; 1918c2ecf20Sopenharmony_ci case 1: 1928c2ecf20Sopenharmony_ci if (st->activity_running > st->event_val) 1938c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 1948c2ecf20Sopenharmony_ci IIO_EVENT_CODE(IIO_ACTIVITY, 0, 1958c2ecf20Sopenharmony_ci IIO_MOD_RUNNING, 1968c2ecf20Sopenharmony_ci IIO_EV_DIR_RISING, 1978c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 1988c2ecf20Sopenharmony_ci 0, 0, 0), 1998c2ecf20Sopenharmony_ci st->event_timestamp); 2008c2ecf20Sopenharmony_ci break; 2018c2ecf20Sopenharmony_ci case 2: 2028c2ecf20Sopenharmony_ci if (st->activity_walking < st->event_val) 2038c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 2048c2ecf20Sopenharmony_ci IIO_EVENT_CODE(IIO_ACTIVITY, 0, 2058c2ecf20Sopenharmony_ci IIO_MOD_WALKING, 2068c2ecf20Sopenharmony_ci IIO_EV_DIR_FALLING, 2078c2ecf20Sopenharmony_ci IIO_EV_TYPE_THRESH, 2088c2ecf20Sopenharmony_ci 0, 0, 0), 2098c2ecf20Sopenharmony_ci st->event_timestamp); 2108c2ecf20Sopenharmony_ci break; 2118c2ecf20Sopenharmony_ci case 3: 2128c2ecf20Sopenharmony_ci iio_push_event(indio_dev, 2138c2ecf20Sopenharmony_ci IIO_EVENT_CODE(IIO_STEPS, 0, IIO_NO_MOD, 2148c2ecf20Sopenharmony_ci IIO_EV_DIR_NONE, 2158c2ecf20Sopenharmony_ci IIO_EV_TYPE_CHANGE, 0, 0, 0), 2168c2ecf20Sopenharmony_ci st->event_timestamp); 2178c2ecf20Sopenharmony_ci break; 2188c2ecf20Sopenharmony_ci default: 2198c2ecf20Sopenharmony_ci break; 2208c2ecf20Sopenharmony_ci } 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/** 2268c2ecf20Sopenharmony_ci * iio_simple_dummy_events_register() - setup interrupt handling for events 2278c2ecf20Sopenharmony_ci * @indio_dev: device instance data 2288c2ecf20Sopenharmony_ci * 2298c2ecf20Sopenharmony_ci * This function requests the threaded interrupt to handle the events. 2308c2ecf20Sopenharmony_ci * Normally the irq is a hardware interrupt and the number comes 2318c2ecf20Sopenharmony_ci * from board configuration files. Here we get it from a companion 2328c2ecf20Sopenharmony_ci * module that fakes the interrupt for us. Note that module in 2338c2ecf20Sopenharmony_ci * no way forms part of this example. Just assume that events magically 2348c2ecf20Sopenharmony_ci * appear via the provided interrupt. 2358c2ecf20Sopenharmony_ci */ 2368c2ecf20Sopenharmony_ciint iio_simple_dummy_events_register(struct iio_dev *indio_dev) 2378c2ecf20Sopenharmony_ci{ 2388c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 2398c2ecf20Sopenharmony_ci int ret; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci /* Fire up event source - normally not present */ 2428c2ecf20Sopenharmony_ci st->event_irq = iio_dummy_evgen_get_irq(); 2438c2ecf20Sopenharmony_ci if (st->event_irq < 0) { 2448c2ecf20Sopenharmony_ci ret = st->event_irq; 2458c2ecf20Sopenharmony_ci goto error_ret; 2468c2ecf20Sopenharmony_ci } 2478c2ecf20Sopenharmony_ci st->regs = iio_dummy_evgen_get_regs(st->event_irq); 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci ret = request_threaded_irq(st->event_irq, 2508c2ecf20Sopenharmony_ci &iio_simple_dummy_get_timestamp, 2518c2ecf20Sopenharmony_ci &iio_simple_dummy_event_handler, 2528c2ecf20Sopenharmony_ci IRQF_ONESHOT, 2538c2ecf20Sopenharmony_ci "iio_simple_event", 2548c2ecf20Sopenharmony_ci indio_dev); 2558c2ecf20Sopenharmony_ci if (ret < 0) 2568c2ecf20Sopenharmony_ci goto error_free_evgen; 2578c2ecf20Sopenharmony_ci return 0; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cierror_free_evgen: 2608c2ecf20Sopenharmony_ci iio_dummy_evgen_release_irq(st->event_irq); 2618c2ecf20Sopenharmony_cierror_ret: 2628c2ecf20Sopenharmony_ci return ret; 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci/** 2668c2ecf20Sopenharmony_ci * iio_simple_dummy_events_unregister() - tidy up interrupt handling on remove 2678c2ecf20Sopenharmony_ci * @indio_dev: device instance data 2688c2ecf20Sopenharmony_ci */ 2698c2ecf20Sopenharmony_civoid iio_simple_dummy_events_unregister(struct iio_dev *indio_dev) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci struct iio_dummy_state *st = iio_priv(indio_dev); 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci free_irq(st->event_irq, indio_dev); 2748c2ecf20Sopenharmony_ci /* Not part of normal driver */ 2758c2ecf20Sopenharmony_ci iio_dummy_evgen_release_irq(st->event_irq); 2768c2ecf20Sopenharmony_ci} 277