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, 72 (s32)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 (s32)report_val); 80 } 81 82 sensor_hub_get_feature(st->hsdev, st->power_state.report_id, 83 st->power_state.index, 84 &state_val); 85 if (state && poll_value) 86 msleep_interruptible(poll_value * 2); 87 88 return 0; 89 } 90 EXPORT_SYMBOL(hid_sensor_power_state); 91 92 int hid_sensor_power_state(struct hid_sensor_common *st, bool state) 93 { 94 #ifdef CONFIG_PM 95 int ret; 96 97 if (state) 98 ret = pm_runtime_get_sync(&st->pdev->dev); 99 else { 100 pm_runtime_mark_last_busy(&st->pdev->dev); 101 ret = pm_runtime_put_autosuspend(&st->pdev->dev); 102 } 103 if (ret < 0) { 104 if (state) 105 pm_runtime_put_noidle(&st->pdev->dev); 106 return ret; 107 } 108 109 return 0; 110 #else 111 return _hid_sensor_power_state(st, state); 112 #endif 113 } 114 115 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig, 116 bool state) 117 { 118 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state); 119 } 120 121 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb) 122 { 123 iio_trigger_unregister(attrb->trigger); 124 iio_trigger_free(attrb->trigger); 125 } 126 EXPORT_SYMBOL(hid_sensor_remove_trigger); 127 128 static const struct iio_trigger_ops hid_sensor_trigger_ops = { 129 .owner = THIS_MODULE, 130 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state, 131 }; 132 133 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 134 struct hid_sensor_common *attrb) 135 { 136 int ret; 137 struct iio_trigger *trig; 138 139 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id); 140 if (trig == NULL) { 141 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n"); 142 ret = -ENOMEM; 143 goto error_ret; 144 } 145 146 trig->dev.parent = indio_dev->dev.parent; 147 iio_trigger_set_drvdata(trig, attrb); 148 trig->ops = &hid_sensor_trigger_ops; 149 ret = iio_trigger_register(trig); 150 151 if (ret) { 152 dev_err(&indio_dev->dev, "Trigger Register Failed\n"); 153 goto error_free_trig; 154 } 155 attrb->trigger = trig; 156 indio_dev->trig = iio_trigger_get(trig); 157 158 ret = pm_runtime_set_active(&indio_dev->dev); 159 if (ret) 160 goto error_unreg_trigger; 161 162 iio_device_set_drvdata(indio_dev, attrb); 163 pm_suspend_ignore_children(&attrb->pdev->dev, true); 164 pm_runtime_enable(&attrb->pdev->dev); 165 /* Default to 3 seconds, but can be changed from sysfs */ 166 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev, 167 3000); 168 pm_runtime_use_autosuspend(&attrb->pdev->dev); 169 170 return ret; 171 error_unreg_trigger: 172 iio_trigger_unregister(trig); 173 error_free_trig: 174 iio_trigger_free(trig); 175 error_ret: 176 return ret; 177 } 178 EXPORT_SYMBOL(hid_sensor_setup_trigger); 179 180 #ifdef CONFIG_PM 181 static int hid_sensor_suspend(struct device *dev) 182 { 183 struct platform_device *pdev = to_platform_device(dev); 184 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 185 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 186 187 return _hid_sensor_power_state(attrb, false); 188 } 189 190 static int hid_sensor_resume(struct device *dev) 191 { 192 struct platform_device *pdev = to_platform_device(dev); 193 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 194 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 195 196 return _hid_sensor_power_state(attrb, true); 197 } 198 199 #endif 200 201 const struct dev_pm_ops hid_sensor_pm_ops = { 202 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume) 203 SET_RUNTIME_PM_OPS(hid_sensor_suspend, 204 hid_sensor_resume, NULL) 205 }; 206 EXPORT_SYMBOL(hid_sensor_pm_ops); 207 208 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 209 MODULE_DESCRIPTION("HID Sensor trigger processing"); 210 MODULE_LICENSE("GPL"); 211