Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / accel / ssp_accel_sensor.c
blobdd6ece8f92390c6077ae6dbd889d5c0714b567c4
1 /*
2 * Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/iio/common/ssp_sensors.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/kfifo_buf.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include "../common/ssp_sensors/ssp_iio_sensor.h"
25 #define SSP_CHANNEL_COUNT 3
27 #define SSP_ACCEL_NAME "ssp-accelerometer"
28 static const char ssp_accel_device_name[] = SSP_ACCEL_NAME;
30 enum ssp_accel_3d_channel {
31 SSP_CHANNEL_SCAN_INDEX_X,
32 SSP_CHANNEL_SCAN_INDEX_Y,
33 SSP_CHANNEL_SCAN_INDEX_Z,
34 SSP_CHANNEL_SCAN_INDEX_TIME,
37 static int ssp_accel_read_raw(struct iio_dev *indio_dev,
38 struct iio_chan_spec const *chan, int *val,
39 int *val2, long mask)
41 u32 t;
42 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
44 switch (mask) {
45 case IIO_CHAN_INFO_SAMP_FREQ:
46 t = ssp_get_sensor_delay(data, SSP_ACCELEROMETER_SENSOR);
47 ssp_convert_to_freq(t, val, val2);
48 return IIO_VAL_INT_PLUS_MICRO;
49 default:
50 break;
53 return -EINVAL;
56 static int ssp_accel_write_raw(struct iio_dev *indio_dev,
57 struct iio_chan_spec const *chan, int val,
58 int val2, long mask)
60 int ret;
61 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
63 switch (mask) {
64 case IIO_CHAN_INFO_SAMP_FREQ:
65 ret = ssp_convert_to_time(val, val2);
66 ret = ssp_change_delay(data, SSP_ACCELEROMETER_SENSOR, ret);
67 if (ret < 0)
68 dev_err(&indio_dev->dev, "accel sensor enable fail\n");
70 return ret;
71 default:
72 break;
75 return -EINVAL;
78 static const struct iio_info ssp_accel_iio_info = {
79 .read_raw = &ssp_accel_read_raw,
80 .write_raw = &ssp_accel_write_raw,
83 static const unsigned long ssp_accel_scan_mask[] = { 0x7, 0, };
85 static const struct iio_chan_spec ssp_acc_channels[] = {
86 SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_X, SSP_CHANNEL_SCAN_INDEX_X),
87 SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Y, SSP_CHANNEL_SCAN_INDEX_Y),
88 SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Z, SSP_CHANNEL_SCAN_INDEX_Z),
89 SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME),
92 static int ssp_process_accel_data(struct iio_dev *indio_dev, void *buf,
93 int64_t timestamp)
95 return ssp_common_process_data(indio_dev, buf, SSP_ACCELEROMETER_SIZE,
96 timestamp);
99 static const struct iio_buffer_setup_ops ssp_accel_buffer_ops = {
100 .postenable = &ssp_common_buffer_postenable,
101 .postdisable = &ssp_common_buffer_postdisable,
104 static int ssp_accel_probe(struct platform_device *pdev)
106 int ret;
107 struct iio_dev *indio_dev;
108 struct ssp_sensor_data *spd;
109 struct iio_buffer *buffer;
111 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
112 if (!indio_dev)
113 return -ENOMEM;
115 spd = iio_priv(indio_dev);
117 spd->process_data = ssp_process_accel_data;
118 spd->type = SSP_ACCELEROMETER_SENSOR;
120 indio_dev->name = ssp_accel_device_name;
121 indio_dev->dev.parent = &pdev->dev;
122 indio_dev->dev.of_node = pdev->dev.of_node;
123 indio_dev->info = &ssp_accel_iio_info;
124 indio_dev->modes = INDIO_BUFFER_SOFTWARE;
125 indio_dev->channels = ssp_acc_channels;
126 indio_dev->num_channels = ARRAY_SIZE(ssp_acc_channels);
127 indio_dev->available_scan_masks = ssp_accel_scan_mask;
129 buffer = devm_iio_kfifo_allocate(&pdev->dev);
130 if (!buffer)
131 return -ENOMEM;
133 iio_device_attach_buffer(indio_dev, buffer);
135 indio_dev->setup_ops = &ssp_accel_buffer_ops;
137 platform_set_drvdata(pdev, indio_dev);
139 ret = devm_iio_device_register(&pdev->dev, indio_dev);
140 if (ret < 0)
141 return ret;
143 /* ssp registering should be done after all iio setup */
144 ssp_register_consumer(indio_dev, SSP_ACCELEROMETER_SENSOR);
146 return 0;
149 static struct platform_driver ssp_accel_driver = {
150 .driver = {
151 .name = SSP_ACCEL_NAME,
153 .probe = ssp_accel_probe,
156 module_platform_driver(ssp_accel_driver);
158 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
159 MODULE_DESCRIPTION("Samsung sensorhub accelerometers driver");
160 MODULE_LICENSE("GPL");