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 (!atomic_read(&st->user_requested_state)) 40 return 0; 41 if (sensor_hub_device_open(st->hsdev)) 42 return -EIO; 43 44 atomic_inc(&st->data_ready); 45 46 state_val = hid_sensor_get_usage_index(st->hsdev, 47 st->power_state.report_id, 48 st->power_state.index, 49 HID_USAGE_SENSOR_PROP_POWER_STATE_D0_FULL_POWER_ENUM); 50 report_val = hid_sensor_get_usage_index(st->hsdev, 51 st->report_state.report_id, 52 st->report_state.index, 53 HID_USAGE_SENSOR_PROP_REPORTING_STATE_ALL_EVENTS_ENUM); 54 55 poll_value = hid_sensor_read_poll_value(st); 56 } else { 57 int val; 58 59 val = atomic_dec_if_positive(&st->data_ready); 60 if (val < 0) 61 return 0; 62 63 sensor_hub_device_close(st->hsdev); 64 state_val = hid_sensor_get_usage_index(st->hsdev, 65 st->power_state.report_id, 66 st->power_state.index, 67 HID_USAGE_SENSOR_PROP_POWER_STATE_D4_POWER_OFF_ENUM); 68 report_val = hid_sensor_get_usage_index(st->hsdev, 69 st->report_state.report_id, 70 st->report_state.index, 71 HID_USAGE_SENSOR_PROP_REPORTING_STATE_NO_EVENTS_ENUM); 72 } 73 74 if (state_val >= 0) { 75 state_val += st->power_state.logical_minimum; 76 sensor_hub_set_feature(st->hsdev, st->power_state.report_id, 77 st->power_state.index, sizeof(state_val), 78 &state_val); 79 } 80 81 if (report_val >= 0) { 82 report_val += st->report_state.logical_minimum; 83 sensor_hub_set_feature(st->hsdev, st->report_state.report_id, 84 st->report_state.index, 85 sizeof(report_val), 86 &report_val); 87 } 88 89 sensor_hub_get_feature(st->hsdev, st->power_state.report_id, 90 st->power_state.index, 91 sizeof(state_val), &state_val); 92 if (state && poll_value) 93 msleep_interruptible(poll_value * 2); 94 95 return 0; 96 } 97 EXPORT_SYMBOL(hid_sensor_power_state); 98 99 int hid_sensor_power_state(struct hid_sensor_common *st, bool state) 100 { 101 102 #ifdef CONFIG_PM 103 int ret; 104 105 atomic_set(&st->user_requested_state, state); 106 if (state) 107 ret = pm_runtime_get_sync(&st->pdev->dev); 108 else { 109 pm_runtime_mark_last_busy(&st->pdev->dev); 110 ret = pm_runtime_put_autosuspend(&st->pdev->dev); 111 } 112 if (ret < 0) { 113 if (state) 114 pm_runtime_put_noidle(&st->pdev->dev); 115 return ret; 116 } 117 118 return 0; 119 #else 120 atomic_set(&st->user_requested_state, state); 121 return _hid_sensor_power_state(st, state); 122 #endif 123 } 124 125 static void hid_sensor_set_power_work(struct work_struct *work) 126 { 127 struct hid_sensor_common *attrb = container_of(work, 128 struct hid_sensor_common, 129 work); 130 131 if (attrb->poll_interval >= 0) 132 sensor_hub_set_feature(attrb->hsdev, attrb->poll.report_id, 133 attrb->poll.index, 134 sizeof(attrb->poll_interval), 135 &attrb->poll_interval); 136 137 if (attrb->raw_hystersis >= 0) 138 sensor_hub_set_feature(attrb->hsdev, 139 attrb->sensitivity.report_id, 140 attrb->sensitivity.index, 141 sizeof(attrb->raw_hystersis), 142 &attrb->raw_hystersis); 143 144 _hid_sensor_power_state(attrb, true); 145 } 146 147 static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig, 148 bool state) 149 { 150 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state); 151 } 152 153 void hid_sensor_remove_trigger(struct hid_sensor_common *attrb) 154 { 155 pm_runtime_disable(&attrb->pdev->dev); 156 pm_runtime_set_suspended(&attrb->pdev->dev); 157 pm_runtime_put_noidle(&attrb->pdev->dev); 158 159 cancel_work_sync(&attrb->work); 160 iio_trigger_unregister(attrb->trigger); 161 iio_trigger_free(attrb->trigger); 162 } 163 EXPORT_SYMBOL(hid_sensor_remove_trigger); 164 165 static const struct iio_trigger_ops hid_sensor_trigger_ops = { 166 .owner = THIS_MODULE, 167 .set_trigger_state = &hid_sensor_data_rdy_trigger_set_state, 168 }; 169 170 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 171 struct hid_sensor_common *attrb) 172 { 173 int ret; 174 struct iio_trigger *trig; 175 176 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id); 177 if (trig == NULL) { 178 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n"); 179 ret = -ENOMEM; 180 goto error_ret; 181 } 182 183 trig->dev.parent = indio_dev->dev.parent; 184 iio_trigger_set_drvdata(trig, attrb); 185 trig->ops = &hid_sensor_trigger_ops; 186 ret = iio_trigger_register(trig); 187 188 if (ret) { 189 dev_err(&indio_dev->dev, "Trigger Register Failed\n"); 190 goto error_free_trig; 191 } 192 attrb->trigger = trig; 193 indio_dev->trig = iio_trigger_get(trig); 194 195 ret = pm_runtime_set_active(&indio_dev->dev); 196 if (ret) 197 goto error_unreg_trigger; 198 199 iio_device_set_drvdata(indio_dev, attrb); 200 201 INIT_WORK(&attrb->work, hid_sensor_set_power_work); 202 203 pm_suspend_ignore_children(&attrb->pdev->dev, true); 204 pm_runtime_enable(&attrb->pdev->dev); 205 /* Default to 3 seconds, but can be changed from sysfs */ 206 pm_runtime_set_autosuspend_delay(&attrb->pdev->dev, 207 3000); 208 pm_runtime_use_autosuspend(&attrb->pdev->dev); 209 210 return ret; 211 error_unreg_trigger: 212 iio_trigger_unregister(trig); 213 error_free_trig: 214 iio_trigger_free(trig); 215 error_ret: 216 return ret; 217 } 218 EXPORT_SYMBOL(hid_sensor_setup_trigger); 219 220 static int __maybe_unused hid_sensor_suspend(struct device *dev) 221 { 222 struct platform_device *pdev = to_platform_device(dev); 223 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 224 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 225 226 return _hid_sensor_power_state(attrb, false); 227 } 228 229 static int __maybe_unused hid_sensor_resume(struct device *dev) 230 { 231 struct platform_device *pdev = to_platform_device(dev); 232 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 233 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 234 schedule_work(&attrb->work); 235 return 0; 236 } 237 238 static int __maybe_unused hid_sensor_runtime_resume(struct device *dev) 239 { 240 struct platform_device *pdev = to_platform_device(dev); 241 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 242 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 243 return _hid_sensor_power_state(attrb, true); 244 } 245 246 const struct dev_pm_ops hid_sensor_pm_ops = { 247 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume) 248 SET_RUNTIME_PM_OPS(hid_sensor_suspend, 249 hid_sensor_runtime_resume, NULL) 250 }; 251 EXPORT_SYMBOL(hid_sensor_pm_ops); 252 253 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 254 MODULE_DESCRIPTION("HID Sensor trigger processing"); 255 MODULE_LICENSE("GPL"); 256