Commit f2a71ebd authored by Srinivas Pandruvada's avatar Srinivas Pandruvada Committed by Jonathan Cameron

iio: hid-sensor-als: Assign channels dynamically

Instead of assuming that every channel defined statically by
als_channels[] is present, assign dynamically based on presence of the
respective usage id in the descriptor. This will allow to register ALS
with limited channel support. Append the timestamp as the last channel.

Update available_scan_mask to specify all channels which are present.

There is no intentional function changes done.
Signed-off-by: default avatarSrinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Link: https://lore.kernel.org/r/20240205185926.3030521-2-srinivas.pandruvada@linux.intel.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent ca2f16c3
...@@ -25,6 +25,7 @@ struct als_state { ...@@ -25,6 +25,7 @@ struct als_state {
struct hid_sensor_hub_callbacks callbacks; struct hid_sensor_hub_callbacks callbacks;
struct hid_sensor_common common_attributes; struct hid_sensor_common common_attributes;
struct hid_sensor_hub_attribute_info als[CHANNEL_SCAN_INDEX_MAX]; struct hid_sensor_hub_attribute_info als[CHANNEL_SCAN_INDEX_MAX];
struct iio_chan_spec channels[CHANNEL_SCAN_INDEX_MAX + 1];
struct { struct {
u32 illum[CHANNEL_SCAN_INDEX_MAX]; u32 illum[CHANNEL_SCAN_INDEX_MAX];
u64 timestamp __aligned(8); u64 timestamp __aligned(8);
...@@ -33,7 +34,15 @@ struct als_state { ...@@ -33,7 +34,15 @@ struct als_state {
int scale_post_decml; int scale_post_decml;
int scale_precision; int scale_precision;
int value_offset; int value_offset;
int num_channels;
s64 timestamp; s64 timestamp;
unsigned long als_scan_mask[2];
};
/* The order of usage ids must match scan index starting from CHANNEL_SCAN_INDEX_INTENSITY */
static const u32 als_usage_ids[] = {
HID_USAGE_SENSOR_LIGHT_ILLUM,
HID_USAGE_SENSOR_LIGHT_ILLUM,
}; };
static const u32 als_sensitivity_addresses[] = { static const u32 als_sensitivity_addresses[] = {
...@@ -237,27 +246,38 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev, ...@@ -237,27 +246,38 @@ static int als_capture_sample(struct hid_sensor_hub_device *hsdev,
/* Parse report which is specific to an usage id*/ /* Parse report which is specific to an usage id*/
static int als_parse_report(struct platform_device *pdev, static int als_parse_report(struct platform_device *pdev,
struct hid_sensor_hub_device *hsdev, struct hid_sensor_hub_device *hsdev,
struct iio_chan_spec *channels,
unsigned usage_id, unsigned usage_id,
struct als_state *st) struct als_state *st)
{ {
int ret; struct iio_chan_spec *channels;
int ret, index = 0;
int i; int i;
for (i = 0; i <= CHANNEL_SCAN_INDEX_ILLUM; ++i) { channels = st->channels;
for (i = 0; i < CHANNEL_SCAN_INDEX_MAX; ++i) {
ret = sensor_hub_input_get_attribute_info(hsdev, ret = sensor_hub_input_get_attribute_info(hsdev,
HID_INPUT_REPORT, HID_INPUT_REPORT,
usage_id, usage_id,
HID_USAGE_SENSOR_LIGHT_ILLUM, als_usage_ids[i],
&st->als[i]); &st->als[i]);
if (ret < 0) if (ret < 0)
return ret; continue;
als_adjust_channel_bit_mask(channels, i, st->als[i].size);
channels[index] = als_channels[i];
st->als_scan_mask[0] |= BIT(i);
als_adjust_channel_bit_mask(channels, index, st->als[i].size);
++index;
dev_dbg(&pdev->dev, "als %x:%x\n", st->als[i].index, dev_dbg(&pdev->dev, "als %x:%x\n", st->als[i].index,
st->als[i].report_id); st->als[i].report_id);
} }
st->num_channels = index;
/* Return success even if one usage id is present */
if (index)
ret = 0;
st->scale_precision = hid_sensor_format_scale(usage_id, st->scale_precision = hid_sensor_format_scale(usage_id,
&st->als[CHANNEL_SCAN_INDEX_INTENSITY], &st->als[CHANNEL_SCAN_INDEX_INTENSITY],
&st->scale_pre_decml, &st->scale_post_decml); &st->scale_pre_decml, &st->scale_post_decml);
...@@ -293,15 +313,7 @@ static int hid_als_probe(struct platform_device *pdev) ...@@ -293,15 +313,7 @@ static int hid_als_probe(struct platform_device *pdev)
return ret; return ret;
} }
indio_dev->channels = devm_kmemdup(&pdev->dev, als_channels,
sizeof(als_channels), GFP_KERNEL);
if (!indio_dev->channels) {
dev_err(&pdev->dev, "failed to duplicate channels\n");
return -ENOMEM;
}
ret = als_parse_report(pdev, hsdev, ret = als_parse_report(pdev, hsdev,
(struct iio_chan_spec *)indio_dev->channels,
hsdev->usage, hsdev->usage,
als_state); als_state);
if (ret) { if (ret) {
...@@ -309,8 +321,15 @@ static int hid_als_probe(struct platform_device *pdev) ...@@ -309,8 +321,15 @@ static int hid_als_probe(struct platform_device *pdev)
return ret; return ret;
} }
indio_dev->num_channels = /* Add timestamp channel */
ARRAY_SIZE(als_channels); als_state->channels[als_state->num_channels] = als_channels[CHANNEL_SCAN_INDEX_TIMESTAMP];
/* +1 for adding timestamp channel */
indio_dev->num_channels = als_state->num_channels + 1;
indio_dev->channels = als_state->channels;
indio_dev->available_scan_masks = als_state->als_scan_mask;
indio_dev->info = &als_info; indio_dev->info = &als_info;
indio_dev->name = name; indio_dev->name = name;
indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->modes = INDIO_DIRECT_MODE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment