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