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/sysfs.h> 29 #include <linux/iio/buffer.h> 30 #include <linux/iio/trigger_consumer.h> 31 #include <linux/iio/triggered_buffer.h> 32 #include "../common/hid-sensors/hid-sensor-trigger.h" 33 34 enum { 35 CHANNEL_SCAN_INDEX_INTENSITY = 0, 36 CHANNEL_SCAN_INDEX_ILLUM = 1, 37 CHANNEL_SCAN_INDEX_MAX 38 }; 39 40 struct als_state { 41 struct hid_sensor_hub_callbacks callbacks; 42 struct hid_sensor_common common_attributes; 43 struct hid_sensor_hub_attribute_info als_illum; 44 u32 illum[CHANNEL_SCAN_INDEX_MAX]; 45 int scale_pre_decml; 46 int scale_post_decml; 47 int scale_precision; 48 int value_offset; 49 }; 50 51 /* Channel definitions */ 52 static const struct iio_chan_spec als_channels[] = { 53 { 54 .type = IIO_INTENSITY, 55 .modified = 1, 56 .channel2 = IIO_MOD_LIGHT_BOTH, 57 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 58 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 59 BIT(IIO_CHAN_INFO_SCALE) | 60 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 61 BIT(IIO_CHAN_INFO_HYSTERESIS), 62 .scan_index = CHANNEL_SCAN_INDEX_INTENSITY, 63 }, 64 { 65 .type = IIO_LIGHT, 66 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 67 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 68 BIT(IIO_CHAN_INFO_SCALE) | 69 BIT(IIO_CHAN_INFO_SAMP_FREQ) | 70 BIT(IIO_CHAN_INFO_HYSTERESIS), 71 .scan_index = CHANNEL_SCAN_INDEX_ILLUM, 72 } 73 }; 74 75 /* Adjust channel real bits based on report descriptor */ 76 static void als_adjust_channel_bit_mask(struct iio_chan_spec *channels, 77 int channel, int size) 78 { 79 channels[channel].scan_type.sign = 's'; 80 /* Real storage bits will change based on the report desc. */ 81 channels[channel].scan_type.realbits = size * 8; 82 /* Maximum size of a sample to capture is u32 */ 83 channels[channel].scan_type.storagebits = sizeof(u32) * 8; 84 } 85 86 /* Channel read_raw handler */ 87 static int als_read_raw(struct iio_dev *indio_dev, 88 struct iio_chan_spec const *chan, 89 int *val, int *val2, 90 long mask) 91 { 92 struct als_state *als_state = iio_priv(indio_dev); 93 int report_id = -1; 94 u32 address; 95 int ret_type; 96 97 *val = 0; 98 *val2 = 0; 99 switch (mask) { 100 case 0: 101 switch (chan->scan_index) { 102 case CHANNEL_SCAN_INDEX_INTENSITY: 103 case CHANNEL_SCAN_INDEX_ILLUM: 104 report_id = als_state->als_illum.report_id; 105 address = 106 HID_USAGE_SENSOR_LIGHT_ILLUM; 107 break; 108 default: 109 report_id = -1; 110 break; 111 } 112 if (report_id >= 0) { 113 hid_sensor_power_state(&als_state->common_attributes, 114 true); 115 *val = sensor_hub_input_attr_get_raw_value( 116 als_state->common_attributes.hsdev, 117 HID_USAGE_SENSOR_ALS, address, 118 report_id, 119 SENSOR_HUB_SYNC); 120 hid_sensor_power_state(&als_state->common_attributes, 121 false); 122 } else { 123 *val = 0; 124 return -EINVAL; 125 } 126 ret_type = IIO_VAL_INT; 127 break; 128 case IIO_CHAN_INFO_SCALE: 129 *val = als_state->scale_pre_decml; 130 *val2 = als_state->scale_post_decml; 131 ret_type = als_state->scale_precision; 132 break; 133 case IIO_CHAN_INFO_OFFSET: 134 *val = als_state->value_offset; 135 ret_type = IIO_VAL_INT; 136 break; 137 case IIO_CHAN_INFO_SAMP_FREQ: 138 ret_type = hid_sensor_read_samp_freq_value( 139 &als_state->common_attributes, val, val2); 140 break; 141 case IIO_CHAN_INFO_HYSTERESIS: 142 ret_type = hid_sensor_read_raw_hyst_value( 143 &als_state->common_attributes, val, val2); 144 break; 145 default: 146 ret_type = -EINVAL; 147 break; 148 } 149 150 return ret_type; 151 } 152 153 /* Channel write_raw handler */ 154 static int als_write_raw(struct iio_dev *indio_dev, 155 struct iio_chan_spec const *chan, 156 int val, 157 int val2, 158 long mask) 159 { 160 struct als_state *als_state = iio_priv(indio_dev); 161 int ret = 0; 162 163 switch (mask) { 164 case IIO_CHAN_INFO_SAMP_FREQ: 165 ret = hid_sensor_write_samp_freq_value( 166 &als_state->common_attributes, val, val2); 167 break; 168 case IIO_CHAN_INFO_HYSTERESIS: 169 ret = hid_sensor_write_raw_hyst_value( 170 &als_state->common_attributes, val, val2); 171 break; 172 default: 173 ret = -EINVAL; 174 } 175 176 return ret; 177 } 178 179 static const struct iio_info als_info = { 180 .read_raw = &als_read_raw, 181 .write_raw = &als_write_raw, 182 }; 183 184 /* Function to push data to buffer */ 185 static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data, 186 int len) 187 { 188 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n"); 189 iio_push_to_buffers(indio_dev, data); 190 } 191 192 /* Callback handler to send event after all samples are received and captured */ 193 static int als_proc_event(struct hid_sensor_hub_device *hsdev, 194 unsigned usage_id, 195 void *priv) 196 { 197 struct iio_dev *indio_dev = platform_get_drvdata(priv); 198 struct als_state *als_state = iio_priv(indio_dev); 199 200 dev_dbg(&indio_dev->dev, "als_proc_event\n"); 201 if (atomic_read(&als_state->common_attributes.data_ready)) 202 hid_sensor_push_data(indio_dev, 203 &als_state->illum, 204 sizeof(als_state->illum)); 205 206 return 0; 207 } 208 209 /* Capture samples in local storage */ 210 static int als_capture_sample(struct hid_sensor_hub_device *hsdev, 211 unsigned usage_id, 212 size_t raw_len, char *raw_data, 213 void *priv) 214 { 215 struct iio_dev *indio_dev = platform_get_drvdata(priv); 216 struct als_state *als_state = iio_priv(indio_dev); 217 int ret = -EINVAL; 218 u32 sample_data = *(u32 *)raw_data; 219 220 switch (usage_id) { 221 case HID_USAGE_SENSOR_LIGHT_ILLUM: 222 als_state->illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data; 223 als_state->illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data; 224 ret = 0; 225 break; 226 default: 227 break; 228 } 229 230 return ret; 231 } 232 233 /* Parse report which is specific to an usage id*/ 234 static int als_parse_report(struct platform_device *pdev, 235 struct hid_sensor_hub_device *hsdev, 236 struct iio_chan_spec *channels, 237 unsigned usage_id, 238 struct als_state *st) 239 { 240 int ret; 241 242 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT, 243 usage_id, 244 HID_USAGE_SENSOR_LIGHT_ILLUM, 245 &st->als_illum); 246 if (ret < 0) 247 return ret; 248 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_INTENSITY, 249 st->als_illum.size); 250 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM, 251 st->als_illum.size); 252 253 dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index, 254 st->als_illum.report_id); 255 256 st->scale_precision = hid_sensor_format_scale( 257 HID_USAGE_SENSOR_ALS, 258 &st->als_illum, 259 &st->scale_pre_decml, &st->scale_post_decml); 260 261 /* Set Sensitivity field ids, when there is no individual modifier */ 262 if (st->common_attributes.sensitivity.index < 0) { 263 sensor_hub_input_get_attribute_info(hsdev, 264 HID_FEATURE_REPORT, usage_id, 265 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 266 HID_USAGE_SENSOR_DATA_LIGHT, 267 &st->common_attributes.sensitivity); 268 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n", 269 st->common_attributes.sensitivity.index, 270 st->common_attributes.sensitivity.report_id); 271 } 272 return ret; 273 } 274 275 /* Function to initialize the processing for usage id */ 276 static int hid_als_probe(struct platform_device *pdev) 277 { 278 int ret = 0; 279 static const char *name = "als"; 280 struct iio_dev *indio_dev; 281 struct als_state *als_state; 282 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 283 284 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state)); 285 if (!indio_dev) 286 return -ENOMEM; 287 platform_set_drvdata(pdev, indio_dev); 288 289 als_state = iio_priv(indio_dev); 290 als_state->common_attributes.hsdev = hsdev; 291 als_state->common_attributes.pdev = pdev; 292 293 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS, 294 &als_state->common_attributes); 295 if (ret) { 296 dev_err(&pdev->dev, "failed to setup common attributes\n"); 297 return ret; 298 } 299 300 indio_dev->channels = kmemdup(als_channels, 301 sizeof(als_channels), GFP_KERNEL); 302 if (!indio_dev->channels) { 303 dev_err(&pdev->dev, "failed to duplicate channels\n"); 304 return -ENOMEM; 305 } 306 307 ret = als_parse_report(pdev, hsdev, 308 (struct iio_chan_spec *)indio_dev->channels, 309 HID_USAGE_SENSOR_ALS, als_state); 310 if (ret) { 311 dev_err(&pdev->dev, "failed to setup attributes\n"); 312 goto error_free_dev_mem; 313 } 314 315 indio_dev->num_channels = 316 ARRAY_SIZE(als_channels); 317 indio_dev->dev.parent = &pdev->dev; 318 indio_dev->info = &als_info; 319 indio_dev->name = name; 320 indio_dev->modes = INDIO_DIRECT_MODE; 321 322 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 323 NULL, NULL); 324 if (ret) { 325 dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 326 goto error_free_dev_mem; 327 } 328 atomic_set(&als_state->common_attributes.data_ready, 0); 329 ret = hid_sensor_setup_trigger(indio_dev, name, 330 &als_state->common_attributes); 331 if (ret < 0) { 332 dev_err(&pdev->dev, "trigger setup failed\n"); 333 goto error_unreg_buffer_funcs; 334 } 335 336 ret = iio_device_register(indio_dev); 337 if (ret) { 338 dev_err(&pdev->dev, "device register failed\n"); 339 goto error_remove_trigger; 340 } 341 342 als_state->callbacks.send_event = als_proc_event; 343 als_state->callbacks.capture_sample = als_capture_sample; 344 als_state->callbacks.pdev = pdev; 345 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS, 346 &als_state->callbacks); 347 if (ret < 0) { 348 dev_err(&pdev->dev, "callback reg failed\n"); 349 goto error_iio_unreg; 350 } 351 352 return ret; 353 354 error_iio_unreg: 355 iio_device_unregister(indio_dev); 356 error_remove_trigger: 357 hid_sensor_remove_trigger(&als_state->common_attributes); 358 error_unreg_buffer_funcs: 359 iio_triggered_buffer_cleanup(indio_dev); 360 error_free_dev_mem: 361 kfree(indio_dev->channels); 362 return ret; 363 } 364 365 /* Function to deinitialize the processing for usage id */ 366 static int hid_als_remove(struct platform_device *pdev) 367 { 368 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 369 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 370 struct als_state *als_state = iio_priv(indio_dev); 371 372 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS); 373 iio_device_unregister(indio_dev); 374 hid_sensor_remove_trigger(&als_state->common_attributes); 375 iio_triggered_buffer_cleanup(indio_dev); 376 kfree(indio_dev->channels); 377 378 return 0; 379 } 380 381 static const struct platform_device_id hid_als_ids[] = { 382 { 383 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 384 .name = "HID-SENSOR-200041", 385 }, 386 { /* sentinel */ } 387 }; 388 MODULE_DEVICE_TABLE(platform, hid_als_ids); 389 390 static struct platform_driver hid_als_platform_driver = { 391 .id_table = hid_als_ids, 392 .driver = { 393 .name = KBUILD_MODNAME, 394 .pm = &hid_sensor_pm_ops, 395 }, 396 .probe = hid_als_probe, 397 .remove = hid_als_remove, 398 }; 399 module_platform_driver(hid_als_platform_driver); 400 401 MODULE_DESCRIPTION("HID Sensor ALS"); 402 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); 403 MODULE_LICENSE("GPL"); 404