GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / iio / accel / ssp_accel_sensor.c
1 /*
2  *  Copyright (C) 2014, Samsung Electronics Co. Ltd. All Rights Reserved.
3  *
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.
8  *
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.
13  *
14  */
15
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"
24
25 #define SSP_CHANNEL_COUNT 3
26
27 #define SSP_ACCEL_NAME "ssp-accelerometer"
28 static const char ssp_accel_device_name[] = SSP_ACCEL_NAME;
29
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,
35 };
36
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)
40 {
41         u32 t;
42         struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
43
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;
51         }
52
53         return -EINVAL;
54 }
55
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)
59 {
60         int ret;
61         struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
62
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");
69
70                 return ret;
71         default:
72                 break;
73         }
74
75         return -EINVAL;
76 }
77
78 static const struct iio_info ssp_accel_iio_info = {
79         .read_raw = &ssp_accel_read_raw,
80         .write_raw = &ssp_accel_write_raw,
81 };
82
83 static const unsigned long ssp_accel_scan_mask[] = { 0x7, 0, };
84
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),
90 };
91
92 static int ssp_process_accel_data(struct iio_dev *indio_dev, void *buf,
93                                   int64_t timestamp)
94 {
95         return ssp_common_process_data(indio_dev, buf, SSP_ACCELEROMETER_SIZE,
96                                        timestamp);
97 }
98
99 static const struct iio_buffer_setup_ops ssp_accel_buffer_ops = {
100         .postenable = &ssp_common_buffer_postenable,
101         .postdisable = &ssp_common_buffer_postdisable,
102 };
103
104 static int ssp_accel_probe(struct platform_device *pdev)
105 {
106         int ret;
107         struct iio_dev *indio_dev;
108         struct ssp_sensor_data *spd;
109         struct iio_buffer *buffer;
110
111         indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
112         if (!indio_dev)
113                 return -ENOMEM;
114
115         spd = iio_priv(indio_dev);
116
117         spd->process_data = ssp_process_accel_data;
118         spd->type = SSP_ACCELEROMETER_SENSOR;
119
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;
128
129         buffer = devm_iio_kfifo_allocate(&pdev->dev);
130         if (!buffer)
131                 return -ENOMEM;
132
133         iio_device_attach_buffer(indio_dev, buffer);
134
135         indio_dev->setup_ops = &ssp_accel_buffer_ops;
136
137         platform_set_drvdata(pdev, indio_dev);
138
139         ret = devm_iio_device_register(&pdev->dev, indio_dev);
140         if (ret < 0)
141                 return ret;
142
143         /* ssp registering should be done after all iio setup */
144         ssp_register_consumer(indio_dev, SSP_ACCELEROMETER_SENSOR);
145
146         return 0;
147 }
148
149 static struct platform_driver ssp_accel_driver = {
150         .driver = {
151                 .name = SSP_ACCEL_NAME,
152         },
153         .probe = ssp_accel_probe,
154 };
155
156 module_platform_driver(ssp_accel_driver);
157
158 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
159 MODULE_DESCRIPTION("Samsung sensorhub accelerometers driver");
160 MODULE_LICENSE("GPL");