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