ia64/PCI: Use common struct resource_entry to replace struct iospace_resource
[linux-2.6/btrfs-unstable.git] / drivers / iio / accel / ssp_accel_sensor.c
blob4ae05fce9f24bc4c5ee106ae2bb8361596ba34de
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/kfifo_buf.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include "../common/ssp_sensors/ssp_iio_sensor.h"
24 #define SSP_CHANNEL_COUNT 3
26 #define SSP_ACCEL_NAME "ssp-accelerometer"
27 static const char ssp_accel_device_name[] = SSP_ACCEL_NAME;
29 enum ssp_accel_3d_channel {
30 SSP_CHANNEL_SCAN_INDEX_X,
31 SSP_CHANNEL_SCAN_INDEX_Y,
32 SSP_CHANNEL_SCAN_INDEX_Z,
33 SSP_CHANNEL_SCAN_INDEX_TIME,
36 static int ssp_accel_read_raw(struct iio_dev *indio_dev,
37 struct iio_chan_spec const *chan, int *val,
38 int *val2, long mask)
40 u32 t;
41 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
43 switch (mask) {
44 case IIO_CHAN_INFO_SAMP_FREQ:
45 t = ssp_get_sensor_delay(data, SSP_ACCELEROMETER_SENSOR);
46 ssp_convert_to_freq(t, val, val2);
47 return IIO_VAL_INT_PLUS_MICRO;
48 default:
49 break;
52 return -EINVAL;
55 static int ssp_accel_write_raw(struct iio_dev *indio_dev,
56 struct iio_chan_spec const *chan, int val,
57 int val2, long mask)
59 int ret;
60 struct ssp_data *data = dev_get_drvdata(indio_dev->dev.parent->parent);
62 switch (mask) {
63 case IIO_CHAN_INFO_SAMP_FREQ:
64 ret = ssp_convert_to_time(val, val2);
65 ret = ssp_change_delay(data, SSP_ACCELEROMETER_SENSOR, ret);
66 if (ret < 0)
67 dev_err(&indio_dev->dev, "accel sensor enable fail\n");
69 return ret;
70 default:
71 break;
74 return -EINVAL;
77 static struct iio_info ssp_accel_iio_info = {
78 .read_raw = &ssp_accel_read_raw,
79 .write_raw = &ssp_accel_write_raw,
82 static const unsigned long ssp_accel_scan_mask[] = { 0x7, 0, };
84 static const struct iio_chan_spec ssp_acc_channels[] = {
85 SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_X, SSP_CHANNEL_SCAN_INDEX_X),
86 SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Y, SSP_CHANNEL_SCAN_INDEX_Y),
87 SSP_CHANNEL_AG(IIO_ACCEL, IIO_MOD_Z, SSP_CHANNEL_SCAN_INDEX_Z),
88 SSP_CHAN_TIMESTAMP(SSP_CHANNEL_SCAN_INDEX_TIME),
91 static int ssp_process_accel_data(struct iio_dev *indio_dev, void *buf,
92 int64_t timestamp)
94 return ssp_common_process_data(indio_dev, buf, SSP_ACCELEROMETER_SIZE,
95 timestamp);
98 static const struct iio_buffer_setup_ops ssp_accel_buffer_ops = {
99 .postenable = &ssp_common_buffer_postenable,
100 .postdisable = &ssp_common_buffer_postdisable,
103 static int ssp_accel_probe(struct platform_device *pdev)
105 int ret;
106 struct iio_dev *indio_dev;
107 struct ssp_sensor_data *spd;
108 struct iio_buffer *buffer;
110 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*spd));
111 if (!indio_dev)
112 return -ENOMEM;
114 spd = iio_priv(indio_dev);
116 spd->process_data = ssp_process_accel_data;
117 spd->type = SSP_ACCELEROMETER_SENSOR;
119 indio_dev->name = ssp_accel_device_name;
120 indio_dev->dev.parent = &pdev->dev;
121 indio_dev->dev.of_node = pdev->dev.of_node;
122 indio_dev->info = &ssp_accel_iio_info;
123 indio_dev->modes = INDIO_BUFFER_SOFTWARE;
124 indio_dev->channels = ssp_acc_channels;
125 indio_dev->num_channels = ARRAY_SIZE(ssp_acc_channels);
126 indio_dev->available_scan_masks = ssp_accel_scan_mask;
128 buffer = devm_iio_kfifo_allocate(&pdev->dev);
129 if (!buffer)
130 return -ENOMEM;
132 iio_device_attach_buffer(indio_dev, buffer);
134 indio_dev->setup_ops = &ssp_accel_buffer_ops;
136 platform_set_drvdata(pdev, indio_dev);
138 ret = iio_device_register(indio_dev);
139 if (ret < 0)
140 return ret;
142 /* ssp registering should be done after all iio setup */
143 ssp_register_consumer(indio_dev, SSP_ACCELEROMETER_SENSOR);
145 return 0;
148 static int ssp_accel_remove(struct platform_device *pdev)
150 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
152 iio_device_unregister(indio_dev);
154 return 0;
157 static struct platform_driver ssp_accel_driver = {
158 .driver = {
159 .name = SSP_ACCEL_NAME,
161 .probe = ssp_accel_probe,
162 .remove = ssp_accel_remove,
165 module_platform_driver(ssp_accel_driver);
167 MODULE_AUTHOR("Karol Wrona <k.wrona@samsung.com>");
168 MODULE_DESCRIPTION("Samsung sensorhub accelerometers driver");
169 MODULE_LICENSE("GPL");