srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * HID Sensors Driver |
| 3 | * Copyright (c) 2012, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | * |
| 18 | */ |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/hid-sensor-hub.h> |
| 26 | #include <linux/iio/iio.h> |
| 27 | #include <linux/iio/trigger.h> |
| 28 | #include <linux/iio/sysfs.h> |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 29 | #include "hid-sensor-trigger.h" |
| 30 | |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 31 | int hid_sensor_power_state(struct hid_sensor_common *st, bool state) |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 32 | { |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 33 | int state_val; |
Srinivas Pandruvada | 751d17e | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 34 | int report_val; |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 35 | |
Srinivas Pandruvada | 7d3c192d | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 36 | if (state) { |
| 37 | if (sensor_hub_device_open(st->hsdev)) |
| 38 | return -EIO; |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 39 | |
| 40 | atomic_inc(&st->data_ready); |
| 41 | |
Srinivas Pandruvada | 1a214ae | 2014-01-23 18:50:22 -0800 | [diff] [blame] | 42 | state_val = hid_sensor_get_usage_index(st->hsdev, |
| 43 | st->power_state.report_id, |
| 44 | st->power_state.index, |
| 45 | HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM); |
| 46 | report_val = hid_sensor_get_usage_index(st->hsdev, |
| 47 | st->report_state.report_id, |
| 48 | st->report_state.index, |
| 49 | HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM); |
Srinivas Pandruvada | 751d17e | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 50 | } else { |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 51 | if (!atomic_dec_and_test(&st->data_ready)) |
| 52 | return 0; |
Srinivas Pandruvada | 751d17e | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 53 | sensor_hub_device_close(st->hsdev); |
Srinivas Pandruvada | 1a214ae | 2014-01-23 18:50:22 -0800 | [diff] [blame] | 54 | state_val = hid_sensor_get_usage_index(st->hsdev, |
| 55 | st->power_state.report_id, |
| 56 | st->power_state.index, |
| 57 | HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM); |
| 58 | report_val = hid_sensor_get_usage_index(st->hsdev, |
| 59 | st->report_state.report_id, |
| 60 | st->report_state.index, |
| 61 | HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM); |
Srinivas Pandruvada | 751d17e | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 62 | } |
Srinivas Pandruvada | 1a214ae | 2014-01-23 18:50:22 -0800 | [diff] [blame] | 63 | |
| 64 | if (state_val >= 0) { |
| 65 | state_val += st->power_state.logical_minimum; |
| 66 | sensor_hub_set_feature(st->hsdev, st->power_state.report_id, |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 67 | st->power_state.index, |
| 68 | (s32)state_val); |
Srinivas Pandruvada | 1a214ae | 2014-01-23 18:50:22 -0800 | [diff] [blame] | 69 | } |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 70 | |
Srinivas Pandruvada | 1a214ae | 2014-01-23 18:50:22 -0800 | [diff] [blame] | 71 | if (report_val >= 0) { |
| 72 | report_val += st->report_state.logical_minimum; |
| 73 | sensor_hub_set_feature(st->hsdev, st->report_state.report_id, |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 74 | st->report_state.index, |
Srinivas Pandruvada | 751d17e | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 75 | (s32)report_val); |
Srinivas Pandruvada | 1a214ae | 2014-01-23 18:50:22 -0800 | [diff] [blame] | 76 | } |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
Srinivas Pandruvada | 56ff6be | 2014-04-19 00:22:00 +0100 | [diff] [blame] | 80 | EXPORT_SYMBOL(hid_sensor_power_state); |
| 81 | |
| 82 | static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig, |
| 83 | bool state) |
| 84 | { |
| 85 | return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state); |
| 86 | } |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 87 | |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 88 | void hid_sensor_remove_trigger(struct hid_sensor_common *attrb) |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 89 | { |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 90 | iio_trigger_unregister(attrb->trigger); |
| 91 | iio_trigger_free(attrb->trigger); |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 92 | } |
| 93 | EXPORT_SYMBOL(hid_sensor_remove_trigger); |
| 94 | |
| 95 | static const struct iio_trigger_ops hid_sensor_trigger_ops = { |
| 96 | .owner = THIS_MODULE, |
| 97 | .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state, |
| 98 | }; |
| 99 | |
| 100 | int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, |
Alexander Holler | e07c6d1 | 2012-12-15 12:45:00 +0000 | [diff] [blame] | 101 | struct hid_sensor_common *attrb) |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 102 | { |
| 103 | int ret; |
| 104 | struct iio_trigger *trig; |
| 105 | |
| 106 | trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id); |
| 107 | if (trig == NULL) { |
| 108 | dev_err(&indio_dev->dev, "Trigger Allocate Failed\n"); |
| 109 | ret = -ENOMEM; |
| 110 | goto error_ret; |
| 111 | } |
| 112 | |
| 113 | trig->dev.parent = indio_dev->dev.parent; |
Lars-Peter Clausen | 1e9663c | 2013-03-25 08:58:00 +0000 | [diff] [blame] | 114 | iio_trigger_set_drvdata(trig, attrb); |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 115 | trig->ops = &hid_sensor_trigger_ops; |
| 116 | ret = iio_trigger_register(trig); |
| 117 | |
| 118 | if (ret) { |
| 119 | dev_err(&indio_dev->dev, "Trigger Register Failed\n"); |
| 120 | goto error_free_trig; |
| 121 | } |
Srinivas Pandruvada | ec7f68e | 2013-10-30 22:48:00 +0000 | [diff] [blame] | 122 | indio_dev->trig = attrb->trigger = trig; |
srinivas pandruvada | 73c6768 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 123 | |
| 124 | return ret; |
| 125 | |
| 126 | error_free_trig: |
| 127 | iio_trigger_free(trig); |
| 128 | error_ret: |
| 129 | return ret; |
| 130 | } |
| 131 | EXPORT_SYMBOL(hid_sensor_setup_trigger); |
| 132 | |
| 133 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); |
| 134 | MODULE_DESCRIPTION("HID Sensor trigger processing"); |
| 135 | MODULE_LICENSE("GPL"); |