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/delay.h> 26 #include <linux/hid-sensor-hub.h> 27 #include <linux/iio/iio.h> 28 #include <linux/iio/trigger.h> 29 #include <linux/iio/sysfs.h> 30 #include "hid-sensor-trigger.h" 31 32 static int _hid_sensor_power_state(struct hid_sensor_common *st, bool state) 33 { 34 int state_val; 35 int report_val; 36 s32 poll_value = 0; 37 38 if (state) { 39 if (sensor_hub_device_open(st->hsdev)) 40 return -EIO; 41 42 atomic_inc(&st->data_ready); 43 44 state_val = hid_sensor_get_usage_index(st->hsdev, 45 st->power_state.report_id, 46 st->power_state.index, 47 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM); 48 report_val = hid_sensor_get_usage_index(st->hsdev, 49 st->report_state.report_id, 50 st->report_state.index, 51 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM); 52 53 poll_value = hid_sensor_read_poll_value(st); 54 } else { 55 if (!atomic_dec_and_test(&st->data_ready)) 56 return 0; 57 sensor_hub_device_close(st->hsdev); 58 state_val = hid_sensor_get_usage_index(st->hsdev, 59 st->power_state.report_id, 60 st->power_state.index, 61 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM); 62 report_val = hid_sensor_get_usage_index(st->hsdev, 63 st->report_state.report_id, 64 st->report_state.index, 65 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM); 66 } 67 68 if (state_val >= 0) { 69 state_val += st->power_state.logical_minimum; 70 sensor_hub_set_feature(st->hsdev, st->power_state.report_id, 71 st->power_state.index, sizeof(state_val), 72 &state_val); 73 } 74 75 if (report_val >= 0) { 76 report_val += st->report_state.logical_minimum; 77 sensor_hub_set_feature(st->hsdev, st->report_state.report_id, 78 st->report_state.index, 79 sizeof(report_val), 80 &report_val); 81 } 82 83 sensor_hub_get_feature(st->hsdev, st->power_state.report_id, 84 st->power_state.index, 85 sizeof(state_val), &state_val); 86 if (state && poll_value) 87 msleep_interruptible(poll_value * 2); 88 89 return 0; 90 } 91 EXPORT_SYMBOL(hid_sensor_power_state); 92 93 int hid_sensor_power_state(struct hid_sensor_common *st, bool state) 94 { 95 #ifdef CONFIG_PM 96 int ret; 97 98 if (state) 99 ret = pm_runtime_get_sync(&st->pdev->dev); 100 else { 101 pm_runtime_mark_last_busy(&st->pdev->dev); 102 ret = pm_runtime_put_autosuspend(&st->pdev->dev); 103 } 104 if (ret < 0) { 105 if (state) 106 pm_runtime_put_noidle(&st->pdev->dev); 107 return ret; 108 } 109 110 return 0; 111 #else 112 return _hid_sensor_power_state(st, state); 113 #endif 114 } 115 116 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig, 117 bool state) 118 { 119 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state); 120 } 121 122 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb) 123 { 124 iio_trigger_unregister(attrb->trigger); 125 iio_trigger_free(attrb->trigger); 126 } 127 EXPORT_SYMBOL(hid_sensor_remove_trigger); 128 129 static const struct iio_trigger_ops hid_sensor_trigger_ops = { 130 .owner = THIS_MODULE, 131 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state, 132 }; 133 134 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 135 struct hid_sensor_common *attrb) 136 { 137 int ret; 138 struct iio_trigger *trig; 139 140 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id); 141 if (trig == NULL) { 142 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n"); 143 ret = -ENOMEM; 144 goto error_ret; 145 } 146 147 trig->dev.parent = indio_dev->dev.parent; 148 iio_trigger_set_drvdata(trig, attrb); 149 trig->ops = &hid_sensor_trigger_ops; 150 ret = iio_trigger_register(trig); 151 152 if (ret) { 153 dev_err(&indio_dev->dev, "Trigger Register Failed\n"); 154 goto error_free_trig; 155 } 156 attrb->trigger = trig; 157 indio_dev->trig = iio_trigger_get(trig); 158 159 ret = pm_runtime_set_active(&indio_dev->dev); 160 if (ret) 161 goto error_unreg_trigger; 162 163 iio_device_set_drvdata(indio_dev, attrb); 164 pm_suspend_ignore_children(&attrb->pdev->dev, true); 165 pm_runtime_enable(&attrb->pdev->dev); 166 /* Default to 3 seconds, but can be changed from sysfs */ 167 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev, 168 3000); 169 pm_runtime_use_autosuspend(&attrb->pdev->dev); 170 171 return ret; 172 error_unreg_trigger: 173 iio_trigger_unregister(trig); 174 error_free_trig: 175 iio_trigger_free(trig); 176 error_ret: 177 return ret; 178 } 179 EXPORT_SYMBOL(hid_sensor_setup_trigger); 180 181 #ifdef CONFIG_PM 182 static int hid_sensor_suspend(struct device *dev) 183 { 184 struct platform_device *pdev = to_platform_device(dev); 185 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 186 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 187 188 return _hid_sensor_power_state(attrb, false); 189 } 190 191 static int hid_sensor_resume(struct device *dev) 192 { 193 struct platform_device *pdev = to_platform_device(dev); 194 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 195 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 196 197 return _hid_sensor_power_state(attrb, true); 198 } 199 200 #endif 201 202 const struct dev_pm_ops hid_sensor_pm_ops = { 203 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume) 204 SET_RUNTIME_PM_OPS(hid_sensor_suspend, 205 hid_sensor_resume, NULL) 206 }; 207 EXPORT_SYMBOL(hid_sensor_pm_ops); 208 209 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 210 MODULE_DESCRIPTION("HID Sensor trigger processing"); 211 MODULE_LICENSE("GPL"); 212