blob: 059d964772c73e8e3a19d7d04169478787a9e40d [file] [log] [blame]
srinivas pandruvadaed5514c2012-09-05 13:56:00 +01001/*
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>
Srinivas Pandruvada371a1232014-04-19 00:22:00 +010025#include <linux/delay.h>
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010026#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>
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010032#include "../common/hid-sensors/hid-sensor-trigger.h"
33
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -080034enum {
35 CHANNEL_SCAN_INDEX_INTENSITY = 0,
36 CHANNEL_SCAN_INDEX_ILLUM = 1,
37 CHANNEL_SCAN_INDEX_MAX
38};
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010039
40struct als_state {
41 struct hid_sensor_hub_callbacks callbacks;
Alexander Hollere07c6d12012-12-15 12:45:00 +000042 struct hid_sensor_common common_attributes;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010043 struct hid_sensor_hub_attribute_info als_illum;
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -080044 u32 illum[CHANNEL_SCAN_INDEX_MAX];
Srinivas Pandruvada36783d02014-04-19 00:22:00 +010045 int scale_pre_decml;
46 int scale_post_decml;
47 int scale_precision;
48 int value_offset;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010049};
50
51/* Channel definitions */
52static const struct iio_chan_spec als_channels[] = {
53 {
54 .type = IIO_INTENSITY,
55 .modified = 1,
56 .channel2 = IIO_MOD_LIGHT_BOTH,
Srinivas Pandruvada371a1232014-04-19 00:22:00 +010057 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
Jonathan Cameronecbe18f2013-02-27 19:03:04 +000058 .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),
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -080062 .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),
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010071 .scan_index = CHANNEL_SCAN_INDEX_ILLUM,
72 }
73};
74
75/* Adjust channel real bits based on report descriptor */
76static 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 */
87static 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;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +010095 int ret_type;
96
97 *val = 0;
98 *val2 = 0;
99 switch (mask) {
100 case 0:
101 switch (chan->scan_index) {
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -0800102 case CHANNEL_SCAN_INDEX_INTENSITY:
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100103 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 }
Srinivas Pandruvada371a1232014-04-19 00:22:00 +0100112 if (report_id >= 0) {
Srinivas Pandruvada371a1232014-04-19 00:22:00 +0100113 hid_sensor_power_state(&als_state->common_attributes,
114 true);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100115 *val = sensor_hub_input_attr_get_raw_value(
Srinivas Pandruvada371a1232014-04-19 00:22:00 +0100116 als_state->common_attributes.hsdev,
117 HID_USAGE_SENSOR_ALS, address,
Srinivas Pandruvadab3f47372015-02-19 15:33:56 -0800118 report_id,
119 SENSOR_HUB_SYNC);
Srinivas Pandruvada371a1232014-04-19 00:22:00 +0100120 hid_sensor_power_state(&als_state->common_attributes,
121 false);
122 } else {
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100123 *val = 0;
124 return -EINVAL;
125 }
126 ret_type = IIO_VAL_INT;
127 break;
128 case IIO_CHAN_INFO_SCALE:
Srinivas Pandruvada36783d02014-04-19 00:22:00 +0100129 *val = als_state->scale_pre_decml;
130 *val2 = als_state->scale_post_decml;
131 ret_type = als_state->scale_precision;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100132 break;
133 case IIO_CHAN_INFO_OFFSET:
Srinivas Pandruvada36783d02014-04-19 00:22:00 +0100134 *val = als_state->value_offset;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100135 ret_type = IIO_VAL_INT;
136 break;
137 case IIO_CHAN_INFO_SAMP_FREQ:
Sachin Kamat5d24b0b2014-03-07 07:44:00 +0000138 ret_type = hid_sensor_read_samp_freq_value(
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100139 &als_state->common_attributes, val, val2);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100140 break;
141 case IIO_CHAN_INFO_HYSTERESIS:
Sachin Kamat5d24b0b2014-03-07 07:44:00 +0000142 ret_type = hid_sensor_read_raw_hyst_value(
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100143 &als_state->common_attributes, val, val2);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100144 break;
145 default:
146 ret_type = -EINVAL;
147 break;
148 }
149
150 return ret_type;
151}
152
153/* Channel write_raw handler */
154static 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
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100179static const struct iio_info als_info = {
180 .driver_module = THIS_MODULE,
181 .read_raw = &als_read_raw,
182 .write_raw = &als_write_raw,
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100183};
184
185/* Function to push data to buffer */
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100186static void hid_sensor_push_data(struct iio_dev *indio_dev, const void *data,
187 int len)
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100188{
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100189 dev_dbg(&indio_dev->dev, "hid_sensor_push_data\n");
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100190 iio_push_to_buffers(indio_dev, data);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100191}
192
193/* Callback handler to send event after all samples are received and captured */
194static int als_proc_event(struct hid_sensor_hub_device *hsdev,
195 unsigned usage_id,
196 void *priv)
197{
198 struct iio_dev *indio_dev = platform_get_drvdata(priv);
199 struct als_state *als_state = iio_priv(indio_dev);
200
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100201 dev_dbg(&indio_dev->dev, "als_proc_event\n");
202 if (atomic_read(&als_state->common_attributes.data_ready))
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100203 hid_sensor_push_data(indio_dev,
Lars-Peter Clausen8c60c7e2013-09-15 17:50:00 +0100204 &als_state->illum,
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100205 sizeof(als_state->illum));
206
207 return 0;
208}
209
210/* Capture samples in local storage */
211static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
212 unsigned usage_id,
213 size_t raw_len, char *raw_data,
214 void *priv)
215{
216 struct iio_dev *indio_dev = platform_get_drvdata(priv);
217 struct als_state *als_state = iio_priv(indio_dev);
218 int ret = -EINVAL;
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -0800219 u32 sample_data = *(u32 *)raw_data;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100220
221 switch (usage_id) {
222 case HID_USAGE_SENSOR_LIGHT_ILLUM:
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -0800223 als_state->illum[CHANNEL_SCAN_INDEX_INTENSITY] = sample_data;
224 als_state->illum[CHANNEL_SCAN_INDEX_ILLUM] = sample_data;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100225 ret = 0;
226 break;
227 default:
228 break;
229 }
230
231 return ret;
232}
233
234/* Parse report which is specific to an usage id*/
235static int als_parse_report(struct platform_device *pdev,
236 struct hid_sensor_hub_device *hsdev,
237 struct iio_chan_spec *channels,
238 unsigned usage_id,
239 struct als_state *st)
240{
241 int ret;
242
243 ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT,
244 usage_id,
245 HID_USAGE_SENSOR_LIGHT_ILLUM,
246 &st->als_illum);
247 if (ret < 0)
248 return ret;
Kweh, Hock Leong578f8d72016-12-15 13:48:21 -0800249 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_INTENSITY,
250 st->als_illum.size);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100251 als_adjust_channel_bit_mask(channels, CHANNEL_SCAN_INDEX_ILLUM,
252 st->als_illum.size);
253
254 dev_dbg(&pdev->dev, "als %x:%x\n", st->als_illum.index,
255 st->als_illum.report_id);
256
Srinivas Pandruvada36783d02014-04-19 00:22:00 +0100257 st->scale_precision = hid_sensor_format_scale(
258 HID_USAGE_SENSOR_ALS,
259 &st->als_illum,
260 &st->scale_pre_decml, &st->scale_post_decml);
261
Srinivas Pandruvadaf87ee1b2013-10-30 22:48:00 +0000262 /* Set Sensitivity field ids, when there is no individual modifier */
263 if (st->common_attributes.sensitivity.index < 0) {
264 sensor_hub_input_get_attribute_info(hsdev,
265 HID_FEATURE_REPORT, usage_id,
266 HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS |
267 HID_USAGE_SENSOR_DATA_LIGHT,
268 &st->common_attributes.sensitivity);
269 dev_dbg(&pdev->dev, "Sensitivity index:report %d:%d\n",
270 st->common_attributes.sensitivity.index,
271 st->common_attributes.sensitivity.report_id);
272 }
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100273 return ret;
274}
275
276/* Function to initialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800277static int hid_als_probe(struct platform_device *pdev)
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100278{
279 int ret = 0;
280 static const char *name = "als";
281 struct iio_dev *indio_dev;
282 struct als_state *als_state;
283 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100284
Sachin Kamat47aff922013-07-30 09:44:00 +0100285 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct als_state));
286 if (!indio_dev)
287 return -ENOMEM;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100288 platform_set_drvdata(pdev, indio_dev);
289
290 als_state = iio_priv(indio_dev);
291 als_state->common_attributes.hsdev = hsdev;
292 als_state->common_attributes.pdev = pdev;
293
294 ret = hid_sensor_parse_common_attributes(hsdev, HID_USAGE_SENSOR_ALS,
295 &als_state->common_attributes);
296 if (ret) {
297 dev_err(&pdev->dev, "failed to setup common attributes\n");
Sachin Kamat47aff922013-07-30 09:44:00 +0100298 return ret;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100299 }
300
Fabio Estevam9ecdbed2015-05-13 15:06:11 -0300301 indio_dev->channels = kmemdup(als_channels,
302 sizeof(als_channels), GFP_KERNEL);
303 if (!indio_dev->channels) {
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100304 dev_err(&pdev->dev, "failed to duplicate channels\n");
Sachin Kamat47aff922013-07-30 09:44:00 +0100305 return -ENOMEM;
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100306 }
307
Fabio Estevam9ecdbed2015-05-13 15:06:11 -0300308 ret = als_parse_report(pdev, hsdev,
309 (struct iio_chan_spec *)indio_dev->channels,
310 HID_USAGE_SENSOR_ALS, als_state);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100311 if (ret) {
312 dev_err(&pdev->dev, "failed to setup attributes\n");
313 goto error_free_dev_mem;
314 }
315
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100316 indio_dev->num_channels =
317 ARRAY_SIZE(als_channels);
318 indio_dev->dev.parent = &pdev->dev;
319 indio_dev->info = &als_info;
320 indio_dev->name = name;
321 indio_dev->modes = INDIO_DIRECT_MODE;
322
323 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
324 NULL, NULL);
325 if (ret) {
326 dev_err(&pdev->dev, "failed to initialize trigger buffer\n");
327 goto error_free_dev_mem;
328 }
Srinivas Pandruvada56ff6be2014-04-19 00:22:00 +0100329 atomic_set(&als_state->common_attributes.data_ready, 0);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100330 ret = hid_sensor_setup_trigger(indio_dev, name,
331 &als_state->common_attributes);
332 if (ret < 0) {
333 dev_err(&pdev->dev, "trigger setup failed\n");
334 goto error_unreg_buffer_funcs;
335 }
336
337 ret = iio_device_register(indio_dev);
338 if (ret) {
339 dev_err(&pdev->dev, "device register failed\n");
340 goto error_remove_trigger;
341 }
342
343 als_state->callbacks.send_event = als_proc_event;
344 als_state->callbacks.capture_sample = als_capture_sample;
345 als_state->callbacks.pdev = pdev;
346 ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_ALS,
347 &als_state->callbacks);
348 if (ret < 0) {
349 dev_err(&pdev->dev, "callback reg failed\n");
350 goto error_iio_unreg;
351 }
352
353 return ret;
354
355error_iio_unreg:
356 iio_device_unregister(indio_dev);
357error_remove_trigger:
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000358 hid_sensor_remove_trigger(&als_state->common_attributes);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100359error_unreg_buffer_funcs:
360 iio_triggered_buffer_cleanup(indio_dev);
361error_free_dev_mem:
362 kfree(indio_dev->channels);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100363 return ret;
364}
365
366/* Function to deinitialize the processing for usage id */
Greg Kroah-Hartmanfc526922012-12-21 13:21:43 -0800367static int hid_als_remove(struct platform_device *pdev)
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100368{
369 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data;
370 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000371 struct als_state *als_state = iio_priv(indio_dev);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100372
373 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS);
374 iio_device_unregister(indio_dev);
Srinivas Pandruvadaec7f68e2013-10-30 22:48:00 +0000375 hid_sensor_remove_trigger(&als_state->common_attributes);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100376 iio_triggered_buffer_cleanup(indio_dev);
377 kfree(indio_dev->channels);
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100378
379 return 0;
380}
381
Krzysztof Kozlowski4e617fc2015-05-02 00:53:33 +0900382static const struct platform_device_id hid_als_ids[] = {
Alexander Holler5b812ea2013-07-10 09:31:00 +0100383 {
384 /* Format: HID-SENSOR-usage_id_in_hex_lowercase */
385 .name = "HID-SENSOR-200041",
386 },
387 { /* sentinel */ }
388};
389MODULE_DEVICE_TABLE(platform, hid_als_ids);
390
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100391static struct platform_driver hid_als_platform_driver = {
Alexander Holler5b812ea2013-07-10 09:31:00 +0100392 .id_table = hid_als_ids,
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100393 .driver = {
Alexander Holler5b812ea2013-07-10 09:31:00 +0100394 .name = KBUILD_MODNAME,
Srinivas Pandruvada2c615af2015-01-07 10:55:56 -0800395 .pm = &hid_sensor_pm_ops,
srinivas pandruvadaed5514c2012-09-05 13:56:00 +0100396 },
397 .probe = hid_als_probe,
398 .remove = hid_als_remove,
399};
400module_platform_driver(hid_als_platform_driver);
401
402MODULE_DESCRIPTION("HID Sensor ALS");
403MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
404MODULE_LICENSE("GPL");