Nojh commited on
Commit
04c0823
·
verified ·
1 Parent(s): 8733463

Upload 4 files

Browse files
Files changed (4) hide show
  1. ais328dq.h +145 -0
  2. ais328dq_acc.c +1224 -0
  3. ais328dq_i2c.c +203 -0
  4. ais328dq_spi.c +215 -0
ais328dq.h ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright (C) 2014 STMicroelectronics
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ *
15
+ * File Name : ais328dq.h
16
+ * Authors : VMA - Volume Mems & Analog Division
17
+ * : Matteo Dameno (matteo.dameno@st.com)
18
+ * : Author is willing to be considered the contact
19
+ * : and update point for the driver.
20
+ * Version : V 1.0.1
21
+ * Date : 2012/10/07
22
+ * Description : AIS328DQ 3D accelerometer sensor LDD
23
+ *
24
+ */
25
+
26
+ #ifndef __AIS328DQ_H__
27
+ #define __AIS328DQ_H__
28
+
29
+ #define AIS328DQ_ACC_I2C_SAD_L (0x18)
30
+ #define AIS328DQ_ACC_I2C_SAD_H (0x19)
31
+ #define AIS328DQ_ACC_DEV_NAME "ais328dq_acc"
32
+
33
+
34
+
35
+ /************************************************/
36
+ /* Accelerometer section defines */
37
+ /************************************************/
38
+
39
+ /* Accelerometer Sensor Full Scale */
40
+ #define AIS328DQ_ACC_FS_MASK 0x30
41
+ #define AIS328DQ_ACC_G_2G 0x00
42
+ #define AIS328DQ_ACC_G_4G 0x10
43
+ #define AIS328DQ_ACC_G_8G 0x30
44
+
45
+ /* Accelerometer Sensor Operating Mode */
46
+ #define AIS328DQ_ACC_ENABLE 0x01
47
+ #define AIS328DQ_ACC_DISABLE 0x00
48
+ #define AIS328DQ_ACC_PM_NORMAL 0x20
49
+ #define AIS328DQ_ACC_PM_OFF AIS328DQ_ACC_DISABLE
50
+
51
+ #ifdef __KERNEL__
52
+
53
+ #if defined(CONFIG_INPUT_AIS328DQ_SPI) || \
54
+ defined(CONFIG_INPUT_AIS328DQ_SPI_MODULE)
55
+ #define AIS328DQ_RX_MAX_LENGTH 500
56
+ #define AIS328DQ_TX_MAX_LENGTH 500
57
+
58
+ struct ais328dq_transfer_buffer {
59
+ u8 rx_buf[AIS328DQ_RX_MAX_LENGTH];
60
+ u8 tx_buf[AIS328DQ_TX_MAX_LENGTH] ____cacheline_aligned;
61
+ };
62
+ #endif
63
+
64
+ struct ais328dq_transfer_function {
65
+ int (*write)(struct device *dev, u8 reg_addr, int len, u8 *data);
66
+ int (*read)(struct device *dev, u8 addr, int len, u8 *data);
67
+ };
68
+ struct ais328dq_acc_platform_data {
69
+
70
+ int poll_interval;
71
+ int min_interval;
72
+
73
+ u8 g_range;
74
+
75
+ u8 axis_map_x;
76
+ u8 axis_map_y;
77
+ u8 axis_map_z;
78
+
79
+ u8 negate_x;
80
+ u8 negate_y;
81
+ u8 negate_z;
82
+
83
+ int (*init)(void);
84
+ void (*exit)(void);
85
+ int (*power_on)(void);
86
+ int (*power_off)(void);
87
+
88
+ /* set gpio_int[1,2] either to the choosen gpio pin number or to -EINVAL
89
+ * if leaved unconnected
90
+ */
91
+ int gpio_int1;
92
+ int gpio_int2;
93
+ };
94
+
95
+ #define RESUME_ENTRIES 12
96
+
97
+ struct ais328dq_acc_data {
98
+ const char *name;
99
+ struct device *dev;
100
+ u16 bus_type;
101
+ struct ais328dq_acc_platform_data *pdata;
102
+
103
+ struct mutex lock;
104
+ s64 timestamp, delta_ts;
105
+ struct hrtimer hr_timer;
106
+ struct workqueue_struct *work_queue;
107
+ struct work_struct poll_work;
108
+ struct input_dev *input_dev;
109
+
110
+ int hw_initialized;
111
+ /* hw_working=-1 means not tested yet */
112
+ int hw_working;
113
+ int selftest_enabled;
114
+
115
+ atomic_t enabled;
116
+
117
+ u8 sensitivity;
118
+
119
+ u8 resume_state[RESUME_ENTRIES];
120
+
121
+ int irq1;
122
+ struct work_struct irq1_work;
123
+ struct workqueue_struct *irq1_work_queue;
124
+ int irq2;
125
+ struct work_struct irq2_work;
126
+ struct workqueue_struct *irq2_work_queue;
127
+
128
+ #ifdef AIS328DQ_DEBUG
129
+ u8 reg_addr;
130
+ #endif
131
+ #if defined(CONFIG_INPUT_AIS328DQ_SPI) || \
132
+ defined(CONFIG_INPUT_AIS328DQ_SPI_MODULE)
133
+ struct ais328dq_transfer_buffer tb;
134
+ #endif
135
+ struct ais328dq_transfer_function *tf;
136
+ };
137
+
138
+ int ais328dq_acc_probe(struct ais328dq_acc_data *acc);
139
+ int ais328dq_acc_remove(struct ais328dq_acc_data *acc);
140
+ int ais328dq_acc_enable(struct ais328dq_acc_data *acc);
141
+ int ais328dq_acc_disable(struct ais328dq_acc_data *acc);
142
+
143
+ #endif /* __KERNEL__ */
144
+
145
+ #endif /* __AIS328DQ_H__ */
ais328dq_acc.c ADDED
@@ -0,0 +1,1224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Copyright (C) 2016 STMicroelectronics
2
+ *
3
+ * Licensed under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License.
5
+ * You may obtain a copy of the License at
6
+ *
7
+ * http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ * See the License for the specific language governing permissions and
13
+ * limitations under the License.
14
+ *
15
+ *
16
+ * File Name : ais328dq_acc.c
17
+ * Authors : VMA - Volume Mems & Analog Division
18
+ * : Matteo Dameno (matteo.dameno@st.com)
19
+ * : Lorenzo Bianconi (lorenzo.bianconi@st.com)
20
+ * : Author is willing to be considered the contact
21
+ * : and update point for the driver.
22
+ * Version : V 1.0.1
23
+ * Date : 2016/05/27
24
+ * Description : AIS328DQ 3D accelerometer sensor LDD
25
+ *
26
+ ******************************************************************************
27
+ * Revision 1.0.0 2011/03/02
28
+ * first release
29
+ * Revision 1.0.1 2012/10/07
30
+ * corrects default permissions on sysfs files
31
+ *****************************************************************************/
32
+
33
+ #include <linux/err.h>
34
+ #include <linux/errno.h>
35
+ #include <linux/delay.h>
36
+ #include <linux/fs.h>
37
+ #include <linux/input.h>
38
+ #include <linux/uaccess.h>
39
+ #include <linux/workqueue.h>
40
+ #include <linux/irq.h>
41
+ #include <linux/gpio.h>
42
+ #include <linux/interrupt.h>
43
+ #include <linux/slab.h>
44
+ #include <linux/module.h>
45
+
46
+ #include "ais328dq.h"
47
+
48
+ #define SENSITIVITY_2G 1 /** mg/LSB */
49
+ #define SENSITIVITY_4G 2 /** mg/LSB */
50
+ #define SENSITIVITY_8G 4 /** mg/LSB */
51
+
52
+ #define AXISDATA_REG 0x28
53
+ #define WHOAMI_AIS328DQ_ACC 0x32 /* Expctd content for WAI */
54
+
55
+ /* CONTROL REGISTERS ADDRESSES */
56
+ #define WHO_AM_I 0x0F /* WhoAmI register */
57
+ #define CTRL_REG1 0x20 /* */
58
+ #define CTRL_REG2 0x21 /* */
59
+ #define CTRL_REG3 0x22 /* */
60
+ #define CTRL_REG4 0x23 /* */
61
+ #define CTRL_REG5 0x24 /* */
62
+
63
+ #define INT_CFG1 0x30 /* interrupt 1 config */
64
+ #define INT_SRC1 0x31 /* interrupt 1 source */
65
+ #define INT_THS1 0x32 /* interrupt 1 threshold */
66
+ #define INT_DUR1 0x33 /* interrupt 1 duration */
67
+
68
+ #define INT_CFG2 0x34 /* interrupt 2 config */
69
+ #define INT_SRC2 0x35 /* interrupt 2 source */
70
+ #define INT_THS2 0x36 /* interrupt 2 threshold */
71
+ #define INT_DUR2 0x37 /* interrupt 2 duration */
72
+ /* end CONTROL REGISTRES ADDRESSES */
73
+
74
+ #define AIS328DQ_ACC_ENABLE_ALL_AXES 0x07
75
+ #define AIS328DQ_SELFTEST_EN 0x02
76
+ #define AIS328DQ_SELFTEST_DIS 0x00
77
+ #define AIS328DQ_SELFTEST_POS 0x00
78
+ #define AIS328DQ_SELFTEST_NEG 0x08
79
+
80
+ #define AIS328DQ_ACC_BDU_EN 0x80
81
+
82
+ /* Accelerometer output data rate */
83
+ #define AIS328DQ_ACC_ODRHALF 0x40 /* 0.5Hz output data rate */
84
+ #define AIS328DQ_ACC_ODR1 0x60 /* 1Hz output data rate */
85
+ #define AIS328DQ_ACC_ODR2 0x80 /* 2Hz output data rate */
86
+ #define AIS328DQ_ACC_ODR5 0xA0 /* 5Hz output data rate */
87
+ #define AIS328DQ_ACC_ODR10 0xC0 /* 10Hz output data rate */
88
+ #define AIS328DQ_ACC_ODR50 0x00 /* 50Hz output data rate */
89
+ #define AIS328DQ_ACC_ODR100 0x08 /* 100Hz output data rate */
90
+ #define AIS328DQ_ACC_ODR400 0x10 /* 400Hz output data rate */
91
+ #define AIS328DQ_ACC_ODR1000 0x18 /* 1000Hz output data rate */
92
+
93
+ /* RESUME STATE INDICES */
94
+ #define RES_CTRL_REG1 0
95
+ #define RES_CTRL_REG2 1
96
+ #define RES_CTRL_REG3 2
97
+ #define RES_CTRL_REG4 3
98
+ #define RES_CTRL_REG5 4
99
+ #define RES_REFERENCE 5
100
+
101
+ #define RES_INT_CFG1 6
102
+ #define RES_INT_THS1 7
103
+ #define RES_INT_DUR1 8
104
+ #define RES_INT_CFG2 9
105
+ #define RES_INT_THS2 10
106
+ #define RES_INT_DUR2 11
107
+
108
+ /* end RESUME STATE INDICES */
109
+
110
+ #define INPUT_EVENT_TYPE EV_MSC
111
+ #define INPUT_EVENT_X MSC_SERIAL
112
+ #define INPUT_EVENT_Y MSC_PULSELED
113
+ #define INPUT_EVENT_Z MSC_GESTURE
114
+ #define INPUT_EVENT_TIME_MSB MSC_SCAN
115
+ #define INPUT_EVENT_TIME_LSB MSC_MAX
116
+
117
+ #define MS_TO_NS(x) ((x)*1000000L)
118
+
119
+ static struct {
120
+ unsigned int cutoff_ms;
121
+ unsigned int mask;
122
+ } ais328dq_acc_odr_table[] = {
123
+ {1, AIS328DQ_ACC_PM_NORMAL | AIS328DQ_ACC_ODR1000},
124
+ {3, AIS328DQ_ACC_PM_NORMAL | AIS328DQ_ACC_ODR400},
125
+ {10, AIS328DQ_ACC_PM_NORMAL | AIS328DQ_ACC_ODR100},
126
+ {20, AIS328DQ_ACC_PM_NORMAL | AIS328DQ_ACC_ODR50},
127
+ /* low power settings, max low pass filter cut-off freq */
128
+ {100, AIS328DQ_ACC_ODR10 | AIS328DQ_ACC_ODR1000},
129
+ {200, AIS328DQ_ACC_ODR5 | AIS328DQ_ACC_ODR1000},
130
+ {5000, AIS328DQ_ACC_ODR2 | AIS328DQ_ACC_ODR1000 },
131
+ {1000, AIS328DQ_ACC_ODR1 | AIS328DQ_ACC_ODR1000 },
132
+ {2000, AIS328DQ_ACC_ODRHALF | AIS328DQ_ACC_ODR1000 },
133
+ };
134
+
135
+ static struct ais328dq_acc_platform_data def_pdata = {
136
+ .poll_interval = 100,
137
+ .min_interval = 1,
138
+ .g_range = 2,
139
+
140
+ .axis_map_x = 0,
141
+ .axis_map_y = 1,
142
+ .axis_map_z = 2,
143
+
144
+ .gpio_int1 = -1,
145
+ .gpio_int2 = -1,
146
+ };
147
+
148
+ static inline s64 ais328dq_get_time_ns(void)
149
+ {
150
+ return ktime_to_ns(ktime_get_boottime());
151
+ }
152
+
153
+ static int ais328dq_acc_hw_init(struct ais328dq_acc_data *acc)
154
+ {
155
+ int err = -1;
156
+ u8 buf[4];
157
+
158
+ printk(KERN_INFO "%s: hw init start\n", AIS328DQ_ACC_DEV_NAME);
159
+
160
+ err = acc->tf->read(acc->dev, WHO_AM_I, 1, buf);
161
+ if (err < 0){
162
+ dev_warn(acc->dev, "Error reading WHO_AM_I: is device "
163
+ "available/working?\n");
164
+ goto err_firstread;
165
+ } else
166
+ acc->hw_working = 1;
167
+ if (buf[0] != WHOAMI_AIS328DQ_ACC) {
168
+ dev_err(acc->dev,
169
+ "device unknown. Expected: 0x%x,"
170
+ " Replies: 0x%x\n", WHOAMI_AIS328DQ_ACC, buf[0]);
171
+ err = -1; /* choose the right coded error */
172
+ goto err_unknown_device;
173
+ }
174
+
175
+ buf[0] = acc->resume_state[RES_CTRL_REG1];
176
+ err = acc->tf->write(acc->dev, CTRL_REG1, 1, buf);
177
+ if (err < 0)
178
+ goto err_resume_state;
179
+
180
+ buf[0] = acc->resume_state[RES_INT_THS1];
181
+ buf[1] = acc->resume_state[RES_INT_DUR1];
182
+ err = acc->tf->write(acc->dev, INT_THS1, 2, buf);
183
+ if (err < 0)
184
+ goto err_resume_state;
185
+
186
+ buf[0] = acc->resume_state[RES_INT_CFG1];
187
+ err = acc->tf->write(acc->dev, INT_CFG1, 1, buf);
188
+ if (err < 0)
189
+ goto err_resume_state;
190
+
191
+ buf[0] = acc->resume_state[RES_INT_THS2];
192
+ buf[1] = acc->resume_state[RES_INT_DUR2];
193
+ err = acc->tf->write(acc->dev, INT_THS2, 2, buf);
194
+ if (err < 0)
195
+ goto err_resume_state;
196
+
197
+ buf[0] = acc->resume_state[RES_INT_CFG2];
198
+ err = acc->tf->write(acc->dev, INT_CFG2, 1, buf);
199
+ if (err < 0)
200
+ goto err_resume_state;
201
+
202
+ buf[0] = acc->resume_state[RES_CTRL_REG2];
203
+ buf[1] = acc->resume_state[RES_CTRL_REG3];
204
+ buf[2] = acc->resume_state[RES_CTRL_REG4];
205
+ buf[3] = acc->resume_state[RES_CTRL_REG5];
206
+ err = acc->tf->write(acc->dev, CTRL_REG2, 4, buf);
207
+ if (err < 0)
208
+ goto err_resume_state;
209
+
210
+ acc->hw_initialized = 1;
211
+ printk(KERN_INFO "%s: hw init done\n", AIS328DQ_ACC_DEV_NAME);
212
+ return 0;
213
+
214
+ err_firstread:
215
+ acc->hw_working = 0;
216
+ err_unknown_device:
217
+ err_resume_state:
218
+ acc->hw_initialized = 0;
219
+ dev_err(acc->dev, "hw init error 0x%x,0x%x: %d\n", buf[0],
220
+ buf[1], err);
221
+ return err;
222
+ }
223
+
224
+ static void ais328dq_acc_device_power_off(struct ais328dq_acc_data *acc)
225
+ {
226
+ int err;
227
+ u8 buf[1] = {AIS328DQ_ACC_PM_OFF};
228
+
229
+ err = acc->tf->write(acc->dev, CTRL_REG1, 1, buf);
230
+ if (err < 0)
231
+ dev_err(acc->dev, "soft power off failed: %d\n", err);
232
+
233
+ if (acc->pdata->power_off) {
234
+ if(acc->pdata->gpio_int1 >= 0)
235
+ disable_irq_nosync(acc->irq1);
236
+ if(acc->pdata->gpio_int2 >= 0)
237
+ disable_irq_nosync(acc->irq2);
238
+ acc->pdata->power_off();
239
+ acc->hw_initialized = 0;
240
+ }
241
+ if (acc->hw_initialized) {
242
+ if(acc->pdata->gpio_int1 >= 0)
243
+ disable_irq_nosync(acc->irq1);
244
+ if(acc->pdata->gpio_int2 >= 0)
245
+ disable_irq_nosync(acc->irq2);
246
+ acc->hw_initialized = 0;
247
+ }
248
+
249
+ }
250
+
251
+ static int ais328dq_acc_device_power_on(struct ais328dq_acc_data *acc)
252
+ {
253
+ int err = -1;
254
+
255
+ if (acc->pdata->power_on) {
256
+ err = acc->pdata->power_on();
257
+ if (err < 0) {
258
+ dev_err(acc->dev,
259
+ "power_on failed: %d\n", err);
260
+ return err;
261
+ }
262
+ if(acc->pdata->gpio_int1 >= 0)
263
+ enable_irq(acc->irq1);
264
+ if(acc->pdata->gpio_int2 >= 0)
265
+ enable_irq(acc->irq2);
266
+ }
267
+
268
+ if (!acc->hw_initialized) {
269
+ err = ais328dq_acc_hw_init(acc);
270
+ if (acc->hw_working == 1 && err < 0) {
271
+ ais328dq_acc_device_power_off(acc);
272
+ return err;
273
+ }
274
+ }
275
+
276
+ if (acc->hw_initialized) {
277
+ if(acc->pdata->gpio_int1 >= 0)
278
+ enable_irq(acc->irq1);
279
+ if(acc->pdata->gpio_int2 >= 0)
280
+ enable_irq(acc->irq2);
281
+ }
282
+ return 0;
283
+ }
284
+
285
+ static irqreturn_t ais328dq_acc_isr1(int irq, void *dev)
286
+ {
287
+ struct ais328dq_acc_data *acc = dev;
288
+
289
+ disable_irq_nosync(irq);
290
+ queue_work(acc->irq1_work_queue, &acc->irq1_work);
291
+ printk(KERN_INFO "%s: isr1 queued\n", AIS328DQ_ACC_DEV_NAME);
292
+
293
+ return IRQ_HANDLED;
294
+ }
295
+
296
+ static irqreturn_t ais328dq_acc_isr2(int irq, void *dev)
297
+ {
298
+ struct ais328dq_acc_data *acc = dev;
299
+
300
+ disable_irq_nosync(irq);
301
+ queue_work(acc->irq2_work_queue, &acc->irq2_work);
302
+ printk(KERN_INFO "%s: isr2 queued\n", AIS328DQ_ACC_DEV_NAME);
303
+
304
+ return IRQ_HANDLED;
305
+ }
306
+
307
+ static void ais328dq_acc_irq1_work_func(struct work_struct *work)
308
+ {
309
+
310
+ struct ais328dq_acc_data *acc =
311
+ container_of(work, struct ais328dq_acc_data, irq1_work);
312
+ /* TODO add interrupt service procedure.
313
+ ie:ais328dq_acc_get_int1_source(acc); */
314
+ ;
315
+ /* */
316
+ enable_irq(acc->irq1);
317
+ }
318
+
319
+ static void ais328dq_acc_irq2_work_func(struct work_struct *work)
320
+ {
321
+
322
+ struct ais328dq_acc_data *acc =
323
+ container_of(work, struct ais328dq_acc_data, irq2_work);
324
+ /* TODO add interrupt service procedure.
325
+ ie:ais328dq_acc_get_tap_source(acc); */
326
+ ;
327
+ /* */
328
+
329
+ enable_irq(acc->irq2);
330
+ }
331
+
332
+ int ais328dq_acc_update_g_range(struct ais328dq_acc_data *acc, u8 range)
333
+ {
334
+ int err = -1;
335
+
336
+ u8 sensitivity;
337
+ u8 buf[1];
338
+ u8 updated_val;
339
+ u8 init_val;
340
+ u8 new_val, new_g_range;
341
+ u8 mask = AIS328DQ_ACC_FS_MASK;
342
+
343
+ switch (range) {
344
+ case 2:
345
+ new_g_range = AIS328DQ_ACC_G_2G;
346
+ sensitivity = SENSITIVITY_2G;
347
+ break;
348
+ case 4:
349
+ new_g_range = AIS328DQ_ACC_G_4G;
350
+ sensitivity = SENSITIVITY_4G;
351
+ break;
352
+ case 8:
353
+ new_g_range = AIS328DQ_ACC_G_8G;
354
+ sensitivity = SENSITIVITY_8G;
355
+ break;
356
+ default:
357
+ dev_err(acc->dev, "invalid g range requested: %u\n", range);
358
+ return -EINVAL;
359
+ }
360
+
361
+ /* Set configuration register 4, which contains g range setting
362
+ * NOTE: this is a straight overwrite because this driver does
363
+ * not use any of the other configuration bits in this
364
+ * register. Should this become untrue, we will have to read
365
+ * out the value and only change the relevant bits --XX----
366
+ * (marked by X) */
367
+ err = acc->tf->read(acc->dev, CTRL_REG4, 1, buf);
368
+ if (err < 0)
369
+ goto error;
370
+
371
+ init_val = buf[0];
372
+ new_val = new_g_range;
373
+ updated_val = ((mask & new_val) | ((~mask) & init_val));
374
+
375
+ if (atomic_read(&acc->enabled)) {
376
+ buf[0] = updated_val;
377
+ err = acc->tf->write(acc->dev, CTRL_REG4, 1, buf);
378
+ if (err < 0)
379
+ goto error;
380
+ }
381
+
382
+ acc->resume_state[RES_CTRL_REG4] = updated_val;
383
+ acc->sensitivity = sensitivity;
384
+
385
+ return 0;
386
+
387
+ error:
388
+ dev_err(acc->dev, "update g range failed 0x%x: %d\n",
389
+ buf[0], err);
390
+
391
+ return err;
392
+ }
393
+
394
+ int ais328dq_acc_update_odr(struct ais328dq_acc_data *acc,
395
+ int poll_interval_ms)
396
+ {
397
+ int err = -1;
398
+ int i;
399
+ u8 config[1];
400
+
401
+ /* Following, looks for the longest possible odr interval scrolling the
402
+ * odr_table vector from the end (shortest interval) backward (longest
403
+ * interval), to support the poll_interval requested by the system.
404
+ * It must be the longest interval lower then the poll interval.*/
405
+ for (i = ARRAY_SIZE(ais328dq_acc_odr_table) - 1; i >= 0; i--) {
406
+ if (ais328dq_acc_odr_table[i].cutoff_ms <= poll_interval_ms)
407
+ break;
408
+ }
409
+
410
+ config[0] = ais328dq_acc_odr_table[i].mask;
411
+ config[0] |= AIS328DQ_ACC_ENABLE_ALL_AXES;
412
+
413
+ /* If device is currently enabled, we need to write new
414
+ * configuration out to it */
415
+ if (atomic_read(&acc->enabled)) {
416
+ err = acc->tf->write(acc->dev, CTRL_REG1, 1, config);
417
+ if (err < 0)
418
+ goto error;
419
+ }
420
+ acc->resume_state[RES_CTRL_REG1] = config[0];
421
+ acc->delta_ts = MS_TO_NS(poll_interval_ms);
422
+
423
+ return err;
424
+
425
+ error:
426
+ dev_err(acc->dev, "update odr failed 0x%x: %d\n",
427
+ config[0], err);
428
+
429
+ return err;
430
+ }
431
+
432
+ static int ais328dq_acc_register_update(struct ais328dq_acc_data *acc,
433
+ u8 *buf, u8 reg_address, u8 mask, u8 new_bit_values)
434
+ {
435
+ int err = -1;
436
+ u8 init_val;
437
+ u8 updated_val;
438
+ err = acc->tf->read(acc->dev, reg_address, 1, buf);
439
+ if (!(err < 0)) {
440
+ init_val = buf[1];
441
+ updated_val = ((mask & new_bit_values) | (~mask & init_val));
442
+ err = acc->tf->write(acc->dev, reg_address, 1, &updated_val);
443
+ }
444
+
445
+ return err;
446
+ }
447
+
448
+
449
+
450
+ static int ais328dq_acc_selftest(struct ais328dq_acc_data *acc, u8 enable)
451
+ {
452
+ int err = -1;
453
+ u8 buf[2]={0x00,0x00};
454
+ char reg_address, mask, bit_values;
455
+
456
+ reg_address = CTRL_REG4;
457
+ mask = 0x0A;
458
+ if (enable > 0)
459
+ bit_values = AIS328DQ_SELFTEST_EN |
460
+ AIS328DQ_SELFTEST_POS;
461
+ else
462
+ bit_values = AIS328DQ_SELFTEST_DIS |
463
+ AIS328DQ_SELFTEST_POS;
464
+ if (atomic_read(&acc->enabled)) {
465
+ mutex_lock(&acc->lock);
466
+ err = ais328dq_acc_register_update(acc, buf, reg_address,
467
+ mask, bit_values);
468
+ acc->selftest_enabled = enable;
469
+ mutex_unlock(&acc->lock);
470
+ if (err < 0)
471
+ return err;
472
+ acc->resume_state[RES_CTRL_REG4] = ((mask & bit_values) |
473
+ ( ~mask & acc->resume_state[RES_CTRL_REG4]));
474
+ }
475
+ return err;
476
+ }
477
+
478
+ static int ais328dq_acc_get_acceleration_data(struct ais328dq_acc_data *acc,
479
+ int *xyz)
480
+ {
481
+ int err = -1;
482
+ /* Data bytes from hardware xL, xH, yL, yH, zL, zH */
483
+ u8 acc_data[6];
484
+ /* x,y,z hardware data */
485
+ s16 hw_d[3] = { 0 };
486
+
487
+ err = acc->tf->read(acc->dev, AXISDATA_REG, 6, acc_data);
488
+ if (err < 0)
489
+ return err;
490
+
491
+ hw_d[0] = (((s16) ((acc_data[1] << 8) | acc_data[0])) >> 4);
492
+ hw_d[1] = (((s16) ((acc_data[3] << 8) | acc_data[2])) >> 4);
493
+ hw_d[2] = (((s16) ((acc_data[5] << 8) | acc_data[4])) >> 4);
494
+
495
+ hw_d[0] = hw_d[0] * acc->sensitivity;
496
+ hw_d[1] = hw_d[1] * acc->sensitivity;
497
+ hw_d[2] = hw_d[2] * acc->sensitivity;
498
+
499
+ xyz[0] = ((acc->pdata->negate_x) ? (-hw_d[acc->pdata->axis_map_x])
500
+ : (hw_d[acc->pdata->axis_map_x]));
501
+ xyz[1] = ((acc->pdata->negate_y) ? (-hw_d[acc->pdata->axis_map_y])
502
+ : (hw_d[acc->pdata->axis_map_y]));
503
+ xyz[2] = ((acc->pdata->negate_z) ? (-hw_d[acc->pdata->axis_map_z])
504
+ : (hw_d[acc->pdata->axis_map_z]));
505
+
506
+ return err;
507
+ }
508
+
509
+ static void ais328dq_acc_report_values(struct ais328dq_acc_data *acc,
510
+ int *xyz)
511
+ {
512
+ input_event(acc->input_dev, INPUT_EVENT_TYPE, INPUT_EVENT_X,
513
+ xyz[0]);
514
+ input_event(acc->input_dev, INPUT_EVENT_TYPE, INPUT_EVENT_Y,
515
+ xyz[1]);
516
+ input_event(acc->input_dev, INPUT_EVENT_TYPE, INPUT_EVENT_Z,
517
+ xyz[2]);
518
+ input_event(acc->input_dev, INPUT_EVENT_TYPE, INPUT_EVENT_TIME_MSB,
519
+ acc->timestamp >> 32);
520
+ input_event(acc->input_dev, INPUT_EVENT_TYPE, INPUT_EVENT_TIME_LSB,
521
+ acc->timestamp & 0xffffffff);
522
+ input_sync(acc->input_dev);
523
+ }
524
+
525
+ int ais328dq_acc_enable(struct ais328dq_acc_data *acc)
526
+ {
527
+ if (!atomic_cmpxchg(&acc->enabled, 0, 1)) {
528
+ int err;
529
+
530
+ mutex_lock(&acc->lock);
531
+ err = ais328dq_acc_device_power_on(acc);
532
+ if (err < 0) {
533
+ atomic_set(&acc->enabled, 0);
534
+ mutex_unlock(&acc->lock);
535
+ return err;
536
+ }
537
+
538
+ hrtimer_start(&acc->hr_timer, ktime_set(0, acc->delta_ts),
539
+ HRTIMER_MODE_REL);
540
+
541
+ mutex_unlock(&acc->lock);
542
+ }
543
+
544
+ return 0;
545
+ }
546
+ EXPORT_SYMBOL(ais328dq_acc_enable);
547
+
548
+ int ais328dq_acc_disable(struct ais328dq_acc_data *acc)
549
+ {
550
+ if (atomic_cmpxchg(&acc->enabled, 1, 0)) {
551
+ mutex_lock(&acc->lock);
552
+ ais328dq_acc_device_power_off(acc);
553
+ mutex_unlock(&acc->lock);
554
+
555
+ cancel_work_sync(&acc->poll_work);
556
+ hrtimer_cancel(&acc->hr_timer);
557
+ }
558
+
559
+ return 0;
560
+ }
561
+ EXPORT_SYMBOL(ais328dq_acc_disable);
562
+
563
+ static ssize_t read_single_reg(struct device *dev, char *buf, u8 reg)
564
+ {
565
+ u8 data;
566
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
567
+ int rc = 0;
568
+
569
+ mutex_lock(&acc->lock);
570
+ rc = acc->tf->read(acc->dev, reg, 1, &data);
571
+ mutex_unlock(&acc->lock);
572
+
573
+ /*TODO: error need to be managed */
574
+ return sprintf(buf, "0x%02x\n", data);
575
+
576
+ }
577
+
578
+ static int write_reg(struct device *dev, const char *buf, u8 reg)
579
+ {
580
+ int rc = 0;
581
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
582
+ u8 x[1];
583
+ unsigned long val;
584
+
585
+ if (kstrtoul(buf, 16, &val))
586
+ return -EINVAL;
587
+
588
+ mutex_lock(&acc->lock);
589
+ x[0] = val;
590
+ rc = acc->tf->write(acc->dev, reg, 1, x);
591
+ mutex_unlock(&acc->lock);
592
+
593
+ /*TODO: error need to be managed */
594
+ return rc;
595
+ }
596
+
597
+ static ssize_t attr_get_polling_rate(struct device *dev,
598
+ struct device_attribute *attr,
599
+ char *buf)
600
+ {
601
+ int val;
602
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
603
+ mutex_lock(&acc->lock);
604
+ val = acc->pdata->poll_interval;
605
+ mutex_unlock(&acc->lock);
606
+ return sprintf(buf, "%d\n", val);
607
+ }
608
+
609
+ static ssize_t attr_set_polling_rate(struct device *dev,
610
+ struct device_attribute *attr,
611
+ const char *buf, size_t size)
612
+ {
613
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
614
+ unsigned long interval_ms;
615
+
616
+ if (kstrtoul(buf, 10, &interval_ms))
617
+ return -EINVAL;
618
+ if (!interval_ms)
619
+ return -EINVAL;
620
+ mutex_lock(&acc->lock);
621
+ acc->pdata->poll_interval = interval_ms;
622
+ ais328dq_acc_update_odr(acc, interval_ms);
623
+ mutex_unlock(&acc->lock);
624
+
625
+ return size;
626
+ }
627
+
628
+ static ssize_t attr_get_range(struct device *dev,
629
+ struct device_attribute *attr, char *buf)
630
+ {
631
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
632
+ char range = 2;
633
+
634
+ mutex_lock(&acc->lock);
635
+ range = acc->pdata->g_range ;
636
+ mutex_unlock(&acc->lock);
637
+
638
+ return sprintf(buf, "%d\n", range);
639
+ }
640
+
641
+ static ssize_t attr_set_range(struct device *dev,
642
+ struct device_attribute *attr,
643
+ const char *buf, size_t size)
644
+ {
645
+ int err;
646
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
647
+ unsigned long val;
648
+
649
+ if (kstrtoul(buf, 10, &val))
650
+ return -EINVAL;
651
+
652
+ mutex_lock(&acc->lock);
653
+ err = ais328dq_acc_update_g_range(acc, val);
654
+ if (err == 0)
655
+ acc->pdata->g_range = val;
656
+ mutex_unlock(&acc->lock);
657
+
658
+ return !err ? size : err;
659
+ }
660
+
661
+ static ssize_t attr_get_enable(struct device *dev,
662
+ struct device_attribute *attr, char *buf)
663
+ {
664
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
665
+ int val = atomic_read(&acc->enabled);
666
+ return sprintf(buf, "%d\n", val);
667
+ }
668
+
669
+ static ssize_t attr_set_enable(struct device *dev,
670
+ struct device_attribute *attr,
671
+ const char *buf, size_t size)
672
+ {
673
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
674
+ unsigned long val;
675
+
676
+ if (kstrtoul(buf, 10, &val))
677
+ return -EINVAL;
678
+
679
+ if (val)
680
+ ais328dq_acc_enable(acc);
681
+ else
682
+ ais328dq_acc_disable(acc);
683
+
684
+ return size;
685
+ }
686
+
687
+ static ssize_t attr_get_selftest(struct device *dev,
688
+ struct device_attribute *attr, char *buf)
689
+ {
690
+ int val;
691
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
692
+ mutex_lock(&acc->lock);
693
+ val = acc->selftest_enabled;
694
+ mutex_unlock(&acc->lock);
695
+ return sprintf(buf, "%d\n", val);
696
+ }
697
+
698
+ static ssize_t attr_set_selftest(struct device *dev,
699
+ struct device_attribute *attr,
700
+ const char *buf, size_t size)
701
+ {
702
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
703
+ unsigned long val;
704
+
705
+ if (kstrtoul(buf, 10, &val))
706
+ return -EINVAL;
707
+
708
+ ais328dq_acc_selftest(acc, val);
709
+
710
+ return size;
711
+ }
712
+
713
+ static ssize_t attr_set_intconfig1(struct device *dev,
714
+ struct device_attribute *attr, const char *buf, size_t size)
715
+ {
716
+ return write_reg(dev, buf, INT_CFG1);
717
+ }
718
+
719
+ static ssize_t attr_get_intconfig1(struct device *dev,
720
+ struct device_attribute *attr, char *buf)
721
+ {
722
+ return read_single_reg(dev, buf, INT_CFG1);
723
+ }
724
+
725
+ static ssize_t attr_set_duration1(struct device *dev,
726
+ struct device_attribute *attr, const char *buf, size_t size)
727
+ {
728
+ return write_reg(dev, buf, INT_DUR1);
729
+ }
730
+
731
+ static ssize_t attr_get_duration1(struct device *dev,
732
+ struct device_attribute *attr, char *buf)
733
+ {
734
+ return read_single_reg(dev, buf, INT_DUR1);
735
+ }
736
+
737
+ static ssize_t attr_set_thresh1(struct device *dev,
738
+ struct device_attribute *attr, const char *buf, size_t size)
739
+ {
740
+ return write_reg(dev, buf, INT_THS1);
741
+ }
742
+
743
+ static ssize_t attr_get_thresh1(struct device *dev,
744
+ struct device_attribute *attr, char *buf)
745
+ {
746
+ return read_single_reg(dev, buf, INT_THS1);
747
+ }
748
+
749
+ static ssize_t attr_get_source1(struct device *dev,
750
+ struct device_attribute *attr, char *buf)
751
+ {
752
+ return read_single_reg(dev, buf, INT_SRC1);
753
+ }
754
+
755
+ static ssize_t attr_set_intconfig2(struct device *dev,
756
+ struct device_attribute *attr, const char *buf, size_t size)
757
+ {
758
+ return write_reg(dev, buf, INT_CFG2);
759
+ }
760
+
761
+ static ssize_t attr_get_intconfig2(struct device *dev,
762
+ struct device_attribute *attr, char *buf)
763
+ {
764
+ return read_single_reg(dev, buf, INT_CFG2);
765
+ }
766
+
767
+ static ssize_t attr_set_duration2(struct device *dev,
768
+ struct device_attribute *attr, const char *buf, size_t size)
769
+ {
770
+ return write_reg(dev, buf, INT_DUR2);
771
+ }
772
+
773
+ static ssize_t attr_get_duration2(struct device *dev,
774
+ struct device_attribute *attr, char *buf)
775
+ {
776
+ return read_single_reg(dev, buf, INT_DUR2);
777
+ }
778
+
779
+ static ssize_t attr_set_thresh2(struct device *dev,
780
+ struct device_attribute *attr, const char *buf, size_t size)
781
+ {
782
+ return write_reg(dev, buf, INT_THS2);
783
+ }
784
+
785
+ static ssize_t attr_get_thresh2(struct device *dev,
786
+ struct device_attribute *attr, char *buf)
787
+ {
788
+ return read_single_reg(dev, buf, INT_THS2);
789
+ }
790
+ static ssize_t attr_get_source2(struct device *dev,
791
+ struct device_attribute *attr, char *buf)
792
+ {
793
+ return read_single_reg(dev, buf, INT_SRC2);
794
+ }
795
+
796
+
797
+
798
+
799
+ #ifdef AIS328DQ_DEBUG
800
+ /* PAY ATTENTION: These AIS328DQ_DEBUG funtions don't manage resume_state */
801
+ static ssize_t attr_reg_set(struct device *dev, struct device_attribute *attr,
802
+ const char *buf, size_t size)
803
+ {
804
+ int rc;
805
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
806
+ u8 x[2];
807
+ unsigned long val;
808
+
809
+ if (kstrtoul(buf, 16, &val))
810
+ return -EINVAL;
811
+ mutex_lock(&acc->lock);
812
+ x[0] = val;
813
+ rc = acc->tf->write(acc->dev, acc->reg_addr, 1, x);
814
+ mutex_unlock(&acc->lock);
815
+
816
+ /*TODO: error need to be managed */
817
+ return size;
818
+ }
819
+
820
+ static ssize_t attr_reg_get(struct device *dev, struct device_attribute *attr,
821
+ char *buf)
822
+ {
823
+ ssize_t ret;
824
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
825
+ int rc;
826
+ u8 data;
827
+
828
+ mutex_lock(&acc->lock);
829
+ err = acc->tf->read(acc->dev, acc->reg_addr, 1, &data);
830
+ mutex_unlock(&acc->lock);
831
+
832
+ /*TODO: error need to be managed */
833
+ ret = sprintf(buf, "0x%02x\n", data);
834
+ return ret;
835
+ }
836
+
837
+ static ssize_t attr_addr_set(struct device *dev, struct device_attribute *attr,
838
+ const char *buf, size_t size)
839
+ {
840
+ struct ais328dq_acc_data *acc = dev_get_drvdata(dev);
841
+ unsigned long val;
842
+ if (kstrtoul(buf, 16, &val))
843
+ return -EINVAL;
844
+ mutex_lock(&acc->lock);
845
+ acc->reg_addr = val;
846
+ mutex_unlock(&acc->lock);
847
+ return size;
848
+ }
849
+ #endif
850
+
851
+ static struct device_attribute attributes[] = {
852
+
853
+ __ATTR(pollrate_ms, 0664, attr_get_polling_rate, attr_set_polling_rate),
854
+ __ATTR(range, 0664, attr_get_range, attr_set_range),
855
+ __ATTR(enable_device, 0664, attr_get_enable, attr_set_enable),
856
+ __ATTR(enable_selftest, 0664, attr_get_selftest, attr_set_selftest),
857
+ __ATTR(int1_config, 0664, attr_get_intconfig1, attr_set_intconfig1),
858
+ __ATTR(int1_duration, 0664, attr_get_duration1, attr_set_duration1),
859
+ __ATTR(int1_threshold, 0664, attr_get_thresh1, attr_set_thresh1),
860
+ __ATTR(int1_source, 0444, attr_get_source1, NULL),
861
+ __ATTR(int2_config, 0664, attr_get_intconfig2, attr_set_intconfig2),
862
+ __ATTR(int2_duration, 0664, attr_get_duration2, attr_set_duration2),
863
+ __ATTR(int2_threshold, 0664, attr_get_thresh2, attr_set_thresh2),
864
+ __ATTR(int2_source, 0444, attr_get_source2, NULL),
865
+ #ifdef AIS328DQ_DEBUG
866
+ __ATTR(reg_value, 0600, attr_reg_get, attr_reg_set),
867
+ __ATTR(reg_addr, 0200, NULL, attr_addr_set),
868
+ #endif
869
+ };
870
+
871
+ static int create_sysfs_interfaces(struct device *dev)
872
+ {
873
+ int i;
874
+ for (i = 0; i < ARRAY_SIZE(attributes); i++)
875
+ if (device_create_file(dev, attributes + i))
876
+ goto error;
877
+ return 0;
878
+
879
+ error:
880
+ for ( ; i >= 0; i--)
881
+ device_remove_file(dev, attributes + i);
882
+ dev_err(dev, "%s:Unable to create interface\n", __func__);
883
+ return -1;
884
+ }
885
+
886
+ static int remove_sysfs_interfaces(struct device *dev)
887
+ {
888
+ int i;
889
+ for (i = 0; i < ARRAY_SIZE(attributes); i++)
890
+ device_remove_file(dev, attributes + i);
891
+ return 0;
892
+ }
893
+
894
+ static enum hrtimer_restart ais328dq_timer_poll(struct hrtimer *timer)
895
+ {
896
+ struct ais328dq_acc_data *acc;
897
+
898
+ acc = container_of((struct hrtimer *)timer, struct ais328dq_acc_data,
899
+ hr_timer);
900
+
901
+ acc->timestamp = ais328dq_get_time_ns();
902
+
903
+ queue_work(acc->work_queue, &acc->poll_work);
904
+
905
+ return HRTIMER_NORESTART;
906
+ }
907
+
908
+ static void ais328dq_poll_work_func(struct work_struct *work)
909
+ {
910
+ struct ais328dq_acc_data *acc;
911
+ int xyz[3] = { 0 };
912
+ int err;
913
+ s64 ts, delta;
914
+
915
+ acc = container_of((struct work_struct *)work,
916
+ struct ais328dq_acc_data, poll_work);
917
+
918
+ ts = ais328dq_get_time_ns();
919
+ delta = acc->delta_ts - (ts - acc->timestamp);
920
+ hrtimer_start(&acc->hr_timer, ktime_set(0, delta),
921
+ HRTIMER_MODE_REL);
922
+
923
+ mutex_lock(&acc->lock);
924
+ err = ais328dq_acc_get_acceleration_data(acc, xyz);
925
+ if (err < 0)
926
+ dev_err(acc->dev, "get_acceleration_data failed\n");
927
+ else
928
+ ais328dq_acc_report_values(acc, xyz);
929
+
930
+ mutex_unlock(&acc->lock);
931
+ }
932
+
933
+ static int ais328dq_acc_validate_pdata(struct ais328dq_acc_data *acc)
934
+ {
935
+ acc->pdata->poll_interval = max(acc->pdata->poll_interval,
936
+ acc->pdata->min_interval);
937
+
938
+ if (acc->pdata->axis_map_x > 2 ||
939
+ acc->pdata->axis_map_y > 2 || acc->pdata->axis_map_z > 2) {
940
+ dev_err(acc->dev,
941
+ "invalid axis_map value x:%u y:%u z%u\n",
942
+ acc->pdata->axis_map_x, acc->pdata->axis_map_y,
943
+ acc->pdata->axis_map_z);
944
+ return -EINVAL;
945
+ }
946
+
947
+ /* Only allow 0 and 1 for negation boolean flag */
948
+ if (acc->pdata->negate_x > 1 || acc->pdata->negate_y > 1 ||
949
+ acc->pdata->negate_z > 1) {
950
+ dev_err(acc->dev,
951
+ "invalid negate value x:%u y:%u z:%u\n",
952
+ acc->pdata->negate_x, acc->pdata->negate_y,
953
+ acc->pdata->negate_z);
954
+ return -EINVAL;
955
+ }
956
+
957
+ /* Enforce minimum polling interval */
958
+ if (acc->pdata->poll_interval < acc->pdata->min_interval) {
959
+ dev_err(acc->dev, "minimum poll interval violated\n");
960
+ return -EINVAL;
961
+ }
962
+
963
+ return 0;
964
+ }
965
+
966
+ static int ais328dq_acc_input_init(struct ais328dq_acc_data *acc)
967
+ {
968
+ int err;
969
+
970
+ acc->work_queue = create_workqueue("ais328dq_wq");
971
+ if (!acc->work_queue)
972
+ return -ENOMEM;
973
+
974
+ hrtimer_init(&acc->hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
975
+ acc->hr_timer.function = &ais328dq_timer_poll;
976
+
977
+ INIT_WORK(&acc->poll_work, ais328dq_poll_work_func);
978
+
979
+ acc->input_dev = input_allocate_device();
980
+ if (!acc->input_dev) {
981
+ err = -ENOMEM;
982
+ dev_err(acc->dev, "input device allocation failed\n");
983
+ goto err0;
984
+ }
985
+
986
+ acc->input_dev->name = AIS328DQ_ACC_DEV_NAME;
987
+ acc->input_dev->id.bustype = acc->bus_type;
988
+ acc->input_dev->dev.parent = acc->dev;
989
+
990
+ input_set_drvdata(acc->input_dev, acc);
991
+
992
+ set_bit(INPUT_EVENT_TYPE, acc->input_dev->evbit);
993
+ set_bit(INPUT_EVENT_X, acc->input_dev->mscbit);
994
+ set_bit(INPUT_EVENT_Y, acc->input_dev->mscbit);
995
+ set_bit(INPUT_EVENT_Z, acc->input_dev->mscbit);
996
+ set_bit(INPUT_EVENT_TIME_MSB, acc->input_dev->mscbit);
997
+ set_bit(INPUT_EVENT_TIME_LSB, acc->input_dev->mscbit);
998
+
999
+
1000
+ err = input_register_device(acc->input_dev);
1001
+ if (err) {
1002
+ dev_err(acc->dev,
1003
+ "unable to register input device %s\n",
1004
+ acc->input_dev->name);
1005
+ goto err1;
1006
+ }
1007
+
1008
+ return 0;
1009
+
1010
+ err1:
1011
+ input_free_device(acc->input_dev);
1012
+ err0:
1013
+ return err;
1014
+ }
1015
+
1016
+ static void ais328dq_acc_input_cleanup(struct ais328dq_acc_data *acc)
1017
+ {
1018
+ if (acc->work_queue) {
1019
+ flush_workqueue(acc->work_queue);
1020
+ destroy_workqueue(acc->work_queue);
1021
+ acc->work_queue = NULL;
1022
+ }
1023
+ input_unregister_device(acc->input_dev);
1024
+ input_free_device(acc->input_dev);
1025
+ }
1026
+
1027
+ int ais328dq_acc_probe(struct ais328dq_acc_data *acc)
1028
+ {
1029
+ int err = -1;
1030
+
1031
+ if (acc->dev->platform_data == NULL) {
1032
+ dev_info(acc->dev,
1033
+ "platform data is NULL. using default one.\n");
1034
+ acc->dev->platform_data = &def_pdata;
1035
+ }
1036
+
1037
+ mutex_init(&acc->lock);
1038
+ mutex_lock(&acc->lock);
1039
+
1040
+ acc->pdata = kmalloc(sizeof(*acc->pdata), GFP_KERNEL);
1041
+ if (acc->pdata == NULL) {
1042
+ err = -ENOMEM;
1043
+ dev_err(acc->dev,
1044
+ "failed to allocate memory for pdata: %d\n",
1045
+ err);
1046
+ goto err_mutexunlock;
1047
+ }
1048
+
1049
+ memcpy(acc->pdata, acc->dev->platform_data, sizeof(*acc->pdata));
1050
+
1051
+ err = ais328dq_acc_validate_pdata(acc);
1052
+ if (err < 0) {
1053
+ dev_err(acc->dev, "failed to validate platform data\n");
1054
+ goto exit_kfree_pdata;
1055
+ }
1056
+
1057
+ if (acc->pdata->init) {
1058
+ err = acc->pdata->init();
1059
+ if (err < 0) {
1060
+ dev_err(acc->dev, "init failed: %d\n", err);
1061
+ goto err_pdata_init;
1062
+ }
1063
+ }
1064
+
1065
+ if(acc->pdata->gpio_int1 >= 0){
1066
+ acc->irq1 = gpio_to_irq(acc->pdata->gpio_int1);
1067
+ printk(KERN_INFO "%s: %s has set irq1 to irq: %d "
1068
+ "mapped on gpio:%d\n",
1069
+ AIS328DQ_ACC_DEV_NAME, __func__, acc->irq1,
1070
+ acc->pdata->gpio_int1);
1071
+ }
1072
+
1073
+ if(acc->pdata->gpio_int2 >= 0){
1074
+ acc->irq2 = gpio_to_irq(acc->pdata->gpio_int2);
1075
+ printk(KERN_INFO "%s: %s has set irq2 to irq: %d "
1076
+ "mapped on gpio:%d\n",
1077
+ AIS328DQ_ACC_DEV_NAME, __func__, acc->irq2,
1078
+ acc->pdata->gpio_int2);
1079
+ }
1080
+
1081
+ memset(acc->resume_state, 0, ARRAY_SIZE(acc->resume_state));
1082
+
1083
+ acc->resume_state[RES_CTRL_REG1] = AIS328DQ_ACC_ENABLE_ALL_AXES;
1084
+ acc->resume_state[RES_CTRL_REG4] = AIS328DQ_ACC_BDU_EN;
1085
+
1086
+ err = ais328dq_acc_device_power_on(acc);
1087
+ if (err < 0) {
1088
+ dev_err(acc->dev, "power on failed: %d\n", err);
1089
+ goto err_pdata_init;
1090
+ }
1091
+
1092
+ atomic_set(&acc->enabled, 1);
1093
+
1094
+ err = ais328dq_acc_update_g_range(acc, acc->pdata->g_range);
1095
+ if (err < 0) {
1096
+ dev_err(acc->dev, "update_g_range failed\n");
1097
+ goto err_power_off;
1098
+ }
1099
+
1100
+ err = ais328dq_acc_update_odr(acc, acc->pdata->poll_interval);
1101
+ if (err < 0) {
1102
+ dev_err(acc->dev, "update_odr failed\n");
1103
+ goto err_power_off;
1104
+ }
1105
+
1106
+ err = ais328dq_acc_input_init(acc);
1107
+ if (err < 0) {
1108
+ dev_err(acc->dev, "input init failed\n");
1109
+ goto err_power_off;
1110
+ }
1111
+
1112
+
1113
+ err = create_sysfs_interfaces(acc->dev);
1114
+ if (err < 0) {
1115
+ dev_err(acc->dev,
1116
+ "device AIS328DQ_ACC_DEV_NAME "
1117
+ "sysfs register failed\n");
1118
+ goto err_input_cleanup;
1119
+ }
1120
+
1121
+ ais328dq_acc_device_power_off(acc);
1122
+
1123
+ /* As default, do not report information */
1124
+ atomic_set(&acc->enabled, 0);
1125
+
1126
+ if(acc->pdata->gpio_int1 >= 0){
1127
+ INIT_WORK(&acc->irq1_work, ais328dq_acc_irq1_work_func);
1128
+ acc->irq1_work_queue =
1129
+ create_singlethread_workqueue("ais328dq_acc_wq1");
1130
+ if (!acc->irq1_work_queue) {
1131
+ err = -ENOMEM;
1132
+ dev_err(acc->dev,
1133
+ "cannot create work queue1: %d\n", err);
1134
+ goto err_remove_sysfs_int;
1135
+ }
1136
+ err = request_irq(acc->irq1, ais328dq_acc_isr1,
1137
+ IRQF_TRIGGER_RISING, "ais328dq_acc_irq1", acc);
1138
+ if (err < 0) {
1139
+ dev_err(acc->dev, "request irq1 failed: %d\n", err);
1140
+ goto err_destoyworkqueue1;
1141
+ }
1142
+ disable_irq_nosync(acc->irq1);
1143
+ }
1144
+
1145
+ if(acc->pdata->gpio_int2 >= 0){
1146
+ INIT_WORK(&acc->irq2_work, ais328dq_acc_irq2_work_func);
1147
+ acc->irq2_work_queue =
1148
+ create_singlethread_workqueue("ais328dq_acc_wq2");
1149
+ if (!acc->irq2_work_queue) {
1150
+ err = -ENOMEM;
1151
+ dev_err(acc->dev,
1152
+ "cannot create work queue2: %d\n", err);
1153
+ goto err_free_irq1;
1154
+ }
1155
+ err = request_irq(acc->irq2, ais328dq_acc_isr2,
1156
+ IRQF_TRIGGER_RISING, "ais328dq_acc_irq2", acc);
1157
+ if (err < 0) {
1158
+ dev_err(acc->dev, "request irq2 failed: %d\n", err);
1159
+ goto err_destoyworkqueue2;
1160
+ }
1161
+ disable_irq_nosync(acc->irq2);
1162
+ }
1163
+
1164
+ mutex_unlock(&acc->lock);
1165
+
1166
+ return 0;
1167
+
1168
+ err_destoyworkqueue2:
1169
+ if(acc->pdata->gpio_int2 >= 0)
1170
+ destroy_workqueue(acc->irq2_work_queue);
1171
+ err_free_irq1:
1172
+ free_irq(acc->irq1, acc);
1173
+ err_destoyworkqueue1:
1174
+ if(acc->pdata->gpio_int1 >= 0)
1175
+ destroy_workqueue(acc->irq1_work_queue);
1176
+ err_remove_sysfs_int:
1177
+ remove_sysfs_interfaces(acc->dev);
1178
+ err_input_cleanup:
1179
+ ais328dq_acc_input_cleanup(acc);
1180
+ err_power_off:
1181
+ ais328dq_acc_device_power_off(acc);
1182
+ err_pdata_init:
1183
+ if (acc->pdata->exit)
1184
+ acc->pdata->exit();
1185
+ exit_kfree_pdata:
1186
+ kfree(acc->pdata);
1187
+ err_mutexunlock:
1188
+ mutex_unlock(&acc->lock);
1189
+
1190
+ return err;
1191
+ }
1192
+ EXPORT_SYMBOL(ais328dq_acc_probe);
1193
+
1194
+ int ais328dq_acc_remove(struct ais328dq_acc_data *acc)
1195
+ {
1196
+ if (acc->pdata->gpio_int1 >= 0){
1197
+ free_irq(acc->irq1, acc);
1198
+ gpio_free(acc->pdata->gpio_int1);
1199
+ destroy_workqueue(acc->irq1_work_queue);
1200
+ }
1201
+
1202
+ if (acc->pdata->gpio_int2 >= 0){
1203
+ free_irq(acc->irq2, acc);
1204
+ gpio_free(acc->pdata->gpio_int2);
1205
+ destroy_workqueue(acc->irq2_work_queue);
1206
+ }
1207
+
1208
+ ais328dq_acc_input_cleanup(acc);
1209
+ ais328dq_acc_device_power_off(acc);
1210
+ remove_sysfs_interfaces(acc->dev);
1211
+
1212
+ if (acc->pdata->exit)
1213
+ acc->pdata->exit();
1214
+ kfree(acc->pdata);
1215
+
1216
+ return 0;
1217
+ }
1218
+ EXPORT_SYMBOL(ais328dq_acc_remove);
1219
+
1220
+ MODULE_DESCRIPTION("ais328dq accelerometer sysfs driver");
1221
+ MODULE_AUTHOR("Matteo Dameno, STMicroelectronics");
1222
+ MODULE_AUTHOR("Lorenzo Bianconi, STMicroelectronics");
1223
+ MODULE_LICENSE("GPL v2");
1224
+
ais328dq_i2c.c ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
2
+ *
3
+ * File Name : ais328dq_i2c.c
4
+ * Authors : AMS - Motion Mems Division - Application Team - Application Team
5
+ * : Lorenzo Bianconi <lorenzo.bianconi@st.com>
6
+ * : Author is willing to be considered the contact and update
7
+ * Version : V.1.0.0
8
+ * Date : 2016/May/27
9
+ *
10
+ ********************************************************************************
11
+ *
12
+ * This program is free software; you can redistribute it and/or modify
13
+ * it under the terms of the GNU General Public License version 2 as
14
+ * published by the Free Software Foundation.
15
+ *
16
+ * THE PRESENT SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
17
+ * OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, FOR THE SOLE
18
+ * PURPOSE TO SUPPORT YOUR APPLICATION DEVELOPMENT.
19
+ * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
20
+ * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
21
+ * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
22
+ * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
23
+ *
24
+ *******************************************************************************/
25
+
26
+ #include <linux/version.h>
27
+ #include <linux/module.h>
28
+ #include <linux/slab.h>
29
+ #include <linux/i2c.h>
30
+ #include <linux/types.h>
31
+ #include <linux/input.h>
32
+ #include <linux/delay.h>
33
+
34
+ #include "ais328dq.h"
35
+
36
+ #define I2C_AUTO_INCREMENT 0x80
37
+ #define I2C_RETRIES 5
38
+ #define I2C_RETRY_DELAY 5
39
+
40
+ static int ais328dq_i2c_read(struct device *dev, u8 reg_addr,
41
+ int len, u8 *data)
42
+ {
43
+ int err = 0, tries = 0;
44
+ struct i2c_msg msg[2];
45
+ struct i2c_client *client = to_i2c_client(dev);
46
+
47
+ reg_addr |= ((len > 1) ? I2C_AUTO_INCREMENT : 0);
48
+
49
+ msg[0].addr = client->addr;
50
+ msg[0].flags = client->flags & I2C_M_TEN;
51
+ msg[0].len = 1;
52
+ msg[0].buf = &reg_addr;
53
+
54
+ msg[1].addr = client->addr;
55
+ msg[1].flags = (client->flags &I2C_M_TEN) | I2C_M_RD;
56
+ msg[1].len = len;
57
+ msg[1].buf = data;
58
+
59
+ do {
60
+ err = i2c_transfer(client->adapter, msg, 2);
61
+ if (err != 2)
62
+ msleep_interruptible(I2C_RETRY_DELAY);
63
+ } while (err != 2 && ++tries < I2C_RETRIES);
64
+
65
+ return (err != 2) ? -EIO : err;
66
+ }
67
+
68
+ static int ais328dq_i2c_write(struct device *dev, u8 reg_addr,
69
+ int len, u8 *data)
70
+ {
71
+ int err = 0, tries = 0;
72
+ u8 send[len + 1];
73
+ struct i2c_msg msg;
74
+ struct i2c_client *client = to_i2c_client(dev);
75
+
76
+ reg_addr |= ((len > 1) ? I2C_AUTO_INCREMENT : 0);
77
+ send[0] = reg_addr;
78
+ memcpy(&send[1], data, len * sizeof(u8));
79
+ len++;
80
+
81
+ msg.addr = client->addr;
82
+ msg.flags = client->flags & I2C_M_TEN;
83
+ msg.len = len;
84
+ msg.buf = send;
85
+
86
+ do {
87
+ err = i2c_transfer(client->adapter, &msg, 1);
88
+ if (err != 1)
89
+ msleep_interruptible(I2C_RETRY_DELAY);
90
+ } while (err != 1 && ++tries < I2C_RETRIES);
91
+
92
+ return (err != 1) ? -EIO : err;
93
+ }
94
+
95
+ static struct ais328dq_transfer_function ais328dq_tf_i2c = {
96
+ .write = ais328dq_i2c_write,
97
+ .read = ais328dq_i2c_read,
98
+ };
99
+
100
+ static int ais328dq_i2c_probe(struct i2c_client *client,
101
+ const struct i2c_device_id *id)
102
+ {
103
+ int err;
104
+ struct ais328dq_acc_data *acc;
105
+
106
+ acc = kmalloc(sizeof(struct ais328dq_acc_data), GFP_KERNEL);
107
+ if (!acc)
108
+ return -ENOMEM;
109
+
110
+ acc->dev = &client->dev;
111
+ acc->name = client->name;
112
+ acc->bus_type = BUS_I2C;
113
+ acc->tf = &ais328dq_tf_i2c;
114
+ i2c_set_clientdata(client, acc);
115
+
116
+ err = ais328dq_acc_probe(acc);
117
+ if (err < 0)
118
+ goto free_data;
119
+
120
+ return 0;
121
+
122
+ free_data:
123
+ kfree(acc);
124
+ return err;
125
+ }
126
+
127
+ #if KERNEL_VERSION(6, 1, 0) <= LINUX_VERSION_CODE
128
+ static void ais328dq_i2c_remove(struct i2c_client *client)
129
+ {
130
+ struct ais328dq_acc_data *acc = i2c_get_clientdata(client);
131
+
132
+ ais328dq_acc_remove(acc);
133
+ kfree(acc);
134
+ }
135
+ #else
136
+ static int ais328dq_i2c_remove(struct i2c_client *client)
137
+ {
138
+ struct ais328dq_acc_data *acc = i2c_get_clientdata(client);
139
+
140
+ ais328dq_acc_remove(acc);
141
+ kfree(acc);
142
+
143
+ return 0;
144
+ }
145
+ #endif
146
+
147
+ #ifdef CONFIG_PM_SLEEP
148
+ static int ais328dq_suspend(struct device *dev)
149
+ {
150
+ struct ais328dq_acc_data *acc = i2c_get_clientdata(to_i2c_client(dev));
151
+
152
+ return ais328dq_acc_disable(acc);
153
+ }
154
+
155
+ static int ais328dq_resume(struct device *dev)
156
+ {
157
+ struct ais328dq_acc_data *acc = i2c_get_clientdata(to_i2c_client(dev));
158
+
159
+ return ais328dq_acc_enable(acc);
160
+ }
161
+
162
+ static SIMPLE_DEV_PM_OPS(ais328dq_pm_ops,
163
+ ais328dq_suspend,
164
+ ais328dq_resume);
165
+
166
+ #define AIS328DQ_PM_OPS (&ais328dq_pm_ops)
167
+ #else /* CONFIG_PM_SLEEP */
168
+ #define AIS328DQ_PM_OPS NULL
169
+ #endif /* CONFIG_PM_SLEEP */
170
+
171
+ static const struct i2c_device_id ais328dq_ids[] = {
172
+ { AIS328DQ_ACC_DEV_NAME, 0 },
173
+ { }
174
+ };
175
+ MODULE_DEVICE_TABLE(i2c, ais328dq_ids);
176
+
177
+ #ifdef CONFIG_OF
178
+ static const struct of_device_id ais328dq_id_table[] = {
179
+ { .compatible = "st,ais328dq", },
180
+ { },
181
+ };
182
+ MODULE_DEVICE_TABLE(of, ais328dq_id_table);
183
+ #endif
184
+
185
+ static struct i2c_driver ais328dq_i2c_driver = {
186
+ .driver = {
187
+ .owner = THIS_MODULE,
188
+ .name = AIS328DQ_ACC_DEV_NAME,
189
+ .pm = AIS328DQ_PM_OPS,
190
+ #ifdef CONFIG_OF
191
+ .of_match_table = ais328dq_id_table,
192
+ #endif
193
+ },
194
+ .remove = ais328dq_i2c_remove,
195
+ .probe = ais328dq_i2c_probe,
196
+ .id_table = ais328dq_ids,
197
+ };
198
+
199
+ module_i2c_driver(ais328dq_i2c_driver);
200
+
201
+ MODULE_DESCRIPTION("STMicroelectronics ais328dq i2c driver");
202
+ MODULE_AUTHOR("Lorenzo Bianconi");
203
+ MODULE_LICENSE("GPL v2");
ais328dq_spi.c ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * STMicroelectronics ais328dq_spi driver
3
+ *
4
+ * Copyright 2016 STMicroelectronics Inc.
5
+ *
6
+ * Lorenzo Bianconi <lorenzo.bianconi@st.com>
7
+ *
8
+ * Licensed under the GPL-2.
9
+ */
10
+ #include <linux/slab.h>
11
+ #include <linux/spi/spi.h>
12
+ #include <linux/input.h>
13
+ #include <linux/module.h>
14
+ #include <linux/version.h>
15
+
16
+ #include "ais328dq.h"
17
+
18
+ #define SENSORS_SPI_READ 0x80
19
+ #define SPI_AUTO_INCREMENT 0x40
20
+
21
+ /* XXX: caller must hold cdata->lock */
22
+ static int ais328dq_spi_read(struct device *device, u8 addr,
23
+ int len, u8 *data)
24
+ {
25
+ int err;
26
+ struct spi_message msg;
27
+ struct spi_device *spi = to_spi_device(device);
28
+ struct ais328dq_acc_data *acc = spi_get_drvdata(spi);
29
+
30
+ struct spi_transfer xfers[] = {
31
+ {
32
+ .tx_buf = acc->tb.tx_buf,
33
+ .bits_per_word = 8,
34
+ .len = 1,
35
+ },
36
+ {
37
+ .rx_buf = acc->tb.rx_buf,
38
+ .bits_per_word = 8,
39
+ .len = len,
40
+ }
41
+ };
42
+
43
+ if (len > 1)
44
+ addr |= SPI_AUTO_INCREMENT;
45
+
46
+ acc->tb.tx_buf[0] = addr | SENSORS_SPI_READ;
47
+
48
+ spi_message_init(&msg);
49
+ spi_message_add_tail(&xfers[0], &msg);
50
+ spi_message_add_tail(&xfers[1], &msg);
51
+
52
+ err = spi_sync(spi, &msg);
53
+ if (err)
54
+ return err;
55
+
56
+ memcpy(data, acc->tb.rx_buf, len * sizeof(u8));
57
+
58
+ return len;
59
+ }
60
+
61
+ /* XXX: caller must hold cdata->lock */
62
+ static int ais328dq_spi_write(struct device *device, u8 addr,
63
+ int len, u8 *data)
64
+ {
65
+ struct spi_message msg;
66
+ struct spi_device *spi = to_spi_device(device);
67
+ struct ais328dq_acc_data *acc = spi_get_drvdata(spi);
68
+
69
+ struct spi_transfer xfers = {
70
+ .tx_buf = acc->tb.tx_buf,
71
+ .bits_per_word = 8,
72
+ .len = len + 1,
73
+ };
74
+
75
+ if (len >= AIS328DQ_TX_MAX_LENGTH)
76
+ return -ENOMEM;
77
+
78
+ if (len > 1)
79
+ addr |= SPI_AUTO_INCREMENT;
80
+
81
+ acc->tb.tx_buf[0] = addr;
82
+
83
+ memcpy(&acc->tb.tx_buf[1], data, len);
84
+
85
+ spi_message_init(&msg);
86
+ spi_message_add_tail(&xfers, &msg);
87
+ return spi_sync(spi, &msg);
88
+ }
89
+
90
+ static struct ais328dq_transfer_function ais328dq_spi_tf = {
91
+ .write = ais328dq_spi_write,
92
+ .read = ais328dq_spi_read,
93
+ };
94
+
95
+ #ifdef CONFIG_PM_SLEEP
96
+ static int ais328dq_resume(struct device *device)
97
+ {
98
+ struct spi_device *spi = to_spi_device(device);
99
+ struct ais328dq_acc_data *acc = spi_get_drvdata(spi);
100
+
101
+ return ais328dq_acc_enable(acc);
102
+ }
103
+
104
+ static int ais328dq_suspend(struct device *device)
105
+ {
106
+ struct spi_device *spi = to_spi_device(device);
107
+ struct ais328dq_acc_data *acc = spi_get_drvdata(spi);
108
+
109
+ return ais328dq_acc_disable(acc);
110
+ }
111
+
112
+ static SIMPLE_DEV_PM_OPS(ais328dq_pm_ops,
113
+ ais328dq_suspend,
114
+ ais328dq_resume);
115
+
116
+ #define AIS328DQ_PM_OPS (&ais328dq_pm_ops)
117
+ #else /* CONFIG_PM_SLEEP */
118
+ #define AIS328DQ_PM_OPS NULL
119
+ #endif /* CONFIG_PM_SLEEP */
120
+
121
+ static int ais328dq_spi_probe(struct spi_device *spi)
122
+ {
123
+ int err;
124
+ struct ais328dq_acc_data *acc;
125
+
126
+ #ifdef AIS328DQ_DEBUG
127
+ dev_info(&spi->dev, "probe start.\n");
128
+ #endif
129
+
130
+ /* Alloc Common data structure */
131
+ acc = kzalloc(sizeof(struct ais328dq_acc_data), GFP_KERNEL);
132
+ if (!acc) {
133
+ dev_err(&spi->dev, "failed to allocate module data\n");
134
+ return -ENOMEM;
135
+ }
136
+
137
+ acc->name = spi->modalias;
138
+ acc->bus_type = BUS_SPI;
139
+ acc->tf = &ais328dq_spi_tf;
140
+ acc->dev = &spi->dev;
141
+ spi_set_drvdata(spi, acc);
142
+
143
+ mutex_init(&acc->lock);
144
+
145
+ err = ais328dq_acc_probe(acc);
146
+ if (err < 0) {
147
+ kfree(acc);
148
+ return err;
149
+ }
150
+
151
+ return 0;
152
+ }
153
+
154
+ #if KERNEL_VERSION(5, 18, 0) <= LINUX_VERSION_CODE
155
+ static void ais328dq_spi_remove(struct spi_device *spi)
156
+ {
157
+ struct ais328dq_acc_data *acc = spi_get_drvdata(spi);
158
+
159
+ #ifdef AIS328DQ_DEBUG
160
+ dev_info(acc->dev, "driver removing\n");
161
+ #endif
162
+
163
+ ais328dq_acc_remove(acc);
164
+ kfree(acc);
165
+ }
166
+ #else
167
+ static int ais328dq_spi_remove(struct spi_device *spi)
168
+ {
169
+ struct ais328dq_acc_data *acc = spi_get_drvdata(spi);
170
+
171
+ #ifdef AIS328DQ_DEBUG
172
+ dev_info(acc->dev, "driver removing\n");
173
+ #endif
174
+
175
+ ais328dq_acc_remove(acc);
176
+ kfree(acc);
177
+
178
+ return 0;
179
+ }
180
+ #endif
181
+
182
+ static const struct spi_device_id ais328dq_spi_id[] = {
183
+ { "ais328dq", 0 },
184
+ { },
185
+ };
186
+ MODULE_DEVICE_TABLE(spi, ais328dq_spi_id);
187
+
188
+ #ifdef CONFIG_OF
189
+ static const struct of_device_id ais328dq_spi_id_table[] = {
190
+ { .compatible = "st,ais328dq" },
191
+ { },
192
+ };
193
+ MODULE_DEVICE_TABLE(of, ais328dq_spi_id_table);
194
+ #endif /* CONFIG_OF */
195
+
196
+ static struct spi_driver ais328dq_spi_driver = {
197
+ .driver = {
198
+ .owner = THIS_MODULE,
199
+ .name = "ais328dq_spi",
200
+ .pm = AIS328DQ_PM_OPS,
201
+ #ifdef CONFIG_OF
202
+ .of_match_table = ais328dq_spi_id_table,
203
+ #endif /* CONFIG_OF */
204
+ },
205
+ .probe = ais328dq_spi_probe,
206
+ .remove = ais328dq_spi_remove,
207
+ .id_table = ais328dq_spi_id,
208
+ };
209
+
210
+ module_spi_driver(ais328dq_spi_driver);
211
+
212
+ MODULE_DESCRIPTION("ais328dq acc spi driver");
213
+ MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
214
+ MODULE_LICENSE("GPL v2");
215
+