diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/misc/inv_mpu/accel/adxl34x.c | |
parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) |
Diffstat (limited to 'drivers/misc/inv_mpu/accel/adxl34x.c')
-rw-r--r-- | drivers/misc/inv_mpu/accel/adxl34x.c | 728 |
1 files changed, 728 insertions, 0 deletions
diff --git a/drivers/misc/inv_mpu/accel/adxl34x.c b/drivers/misc/inv_mpu/accel/adxl34x.c new file mode 100644 index 00000000000..f2bff8a7592 --- /dev/null +++ b/drivers/misc/inv_mpu/accel/adxl34x.c | |||
@@ -0,0 +1,728 @@ | |||
1 | /* | ||
2 | $License: | ||
3 | Copyright (C) 2011 InvenSense Corporation, All Rights Reserved. | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | $ | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @addtogroup ACCELDL | ||
22 | * @brief Provides the interface to setup and handle an accelerometer. | ||
23 | * | ||
24 | * @{ | ||
25 | * @file adxl34x.c | ||
26 | * @brief Accelerometer setup and handling methods for AD adxl345 and | ||
27 | * adxl346. | ||
28 | */ | ||
29 | |||
30 | /* -------------------------------------------------------------------------- */ | ||
31 | |||
32 | #include <linux/i2c.h> | ||
33 | #include <linux/module.h> | ||
34 | #include <linux/moduleparam.h> | ||
35 | #include <linux/kernel.h> | ||
36 | #include <linux/errno.h> | ||
37 | #include <linux/slab.h> | ||
38 | #include <linux/delay.h> | ||
39 | #include "mpu-dev.h" | ||
40 | |||
41 | #include <log.h> | ||
42 | |||
43 | #include <linux/mpu.h> | ||
44 | #include "mlsl.h" | ||
45 | #include "mldl_cfg.h" | ||
46 | |||
47 | #undef MPL_LOG_TAG | ||
48 | #define MPL_LOG_TAG "MPL-acc" | ||
49 | |||
50 | /* -------------------------------------------------------------------------- */ | ||
51 | |||
52 | /* registers */ | ||
53 | #define ADXL34X_ODR_REG (0x2C) | ||
54 | #define ADXL34X_PWR_REG (0x2D) | ||
55 | #define ADXL34X_DATAFORMAT_REG (0x31) | ||
56 | |||
57 | /* masks */ | ||
58 | #define ADXL34X_ODR_MASK (0x0F) | ||
59 | #define ADXL34X_PWR_SLEEP_MASK (0x04) | ||
60 | #define ADXL34X_PWR_MEAS_MASK (0x08) | ||
61 | #define ADXL34X_DATAFORMAT_JUSTIFY_MASK (0x04) | ||
62 | #define ADXL34X_DATAFORMAT_FSR_MASK (0x03) | ||
63 | |||
64 | /* -------------------------------------------------------------------------- */ | ||
65 | |||
66 | struct adxl34x_config { | ||
67 | unsigned int odr; /** < output data rate in mHz */ | ||
68 | unsigned int fsr; /** < full scale range mg */ | ||
69 | unsigned int fsr_reg_mask; /** < register setting for fsr */ | ||
70 | }; | ||
71 | |||
72 | struct adxl34x_private_data { | ||
73 | struct adxl34x_config suspend; /** < suspend configuration */ | ||
74 | struct adxl34x_config resume; /** < resume configuration */ | ||
75 | }; | ||
76 | |||
77 | /** | ||
78 | * @brief Set the output data rate for the particular configuration. | ||
79 | * | ||
80 | * @param mlsl_handle | ||
81 | * the handle to the serial channel the device is connected to. | ||
82 | * @param pdata | ||
83 | * a pointer to the slave platform data. | ||
84 | * @param config | ||
85 | * Config to modify with new ODR. | ||
86 | * @param apply | ||
87 | * whether to apply immediately or save the settings to be applied | ||
88 | * at the next resume. | ||
89 | * @param odr | ||
90 | * Output data rate in units of 1/1000Hz (mHz). | ||
91 | * | ||
92 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
93 | */ | ||
94 | static int adxl34x_set_odr(void *mlsl_handle, | ||
95 | struct ext_slave_platform_data *pdata, | ||
96 | struct adxl34x_config *config, | ||
97 | int apply, | ||
98 | long odr) | ||
99 | { | ||
100 | int result = INV_SUCCESS; | ||
101 | unsigned char new_odr_mask; | ||
102 | |||
103 | /* ADXL346 (Rev. A) pages 13, 24 */ | ||
104 | if (odr >= 3200000) { | ||
105 | new_odr_mask = 0x0F; | ||
106 | config->odr = 3200000; | ||
107 | } else if (odr >= 1600000) { | ||
108 | new_odr_mask = 0x0E; | ||
109 | config->odr = 1600000; | ||
110 | } else if (odr >= 800000) { | ||
111 | new_odr_mask = 0x0D; | ||
112 | config->odr = 800000; | ||
113 | } else if (odr >= 400000) { | ||
114 | new_odr_mask = 0x0C; | ||
115 | config->odr = 400000; | ||
116 | } else if (odr >= 200000) { | ||
117 | new_odr_mask = 0x0B; | ||
118 | config->odr = 200000; | ||
119 | } else if (odr >= 100000) { | ||
120 | new_odr_mask = 0x0A; | ||
121 | config->odr = 100000; | ||
122 | } else if (odr >= 50000) { | ||
123 | new_odr_mask = 0x09; | ||
124 | config->odr = 50000; | ||
125 | } else if (odr >= 25000) { | ||
126 | new_odr_mask = 0x08; | ||
127 | config->odr = 25000; | ||
128 | } else if (odr >= 12500) { | ||
129 | new_odr_mask = 0x07; | ||
130 | config->odr = 12500; | ||
131 | } else if (odr >= 6250) { | ||
132 | new_odr_mask = 0x06; | ||
133 | config->odr = 6250; | ||
134 | } else if (odr >= 3130) { | ||
135 | new_odr_mask = 0x05; | ||
136 | config->odr = 3130; | ||
137 | } else if (odr >= 1560) { | ||
138 | new_odr_mask = 0x04; | ||
139 | config->odr = 1560; | ||
140 | } else if (odr >= 780) { | ||
141 | new_odr_mask = 0x03; | ||
142 | config->odr = 780; | ||
143 | } else if (odr >= 390) { | ||
144 | new_odr_mask = 0x02; | ||
145 | config->odr = 390; | ||
146 | } else if (odr >= 200) { | ||
147 | new_odr_mask = 0x01; | ||
148 | config->odr = 200; | ||
149 | } else { | ||
150 | new_odr_mask = 0x00; | ||
151 | config->odr = 100; | ||
152 | } | ||
153 | |||
154 | if (apply) { | ||
155 | unsigned char reg_odr; | ||
156 | result = inv_serial_read(mlsl_handle, pdata->address, | ||
157 | ADXL34X_ODR_REG, 1, ®_odr); | ||
158 | if (result) { | ||
159 | LOG_RESULT_LOCATION(result); | ||
160 | return result; | ||
161 | } | ||
162 | reg_odr &= ~ADXL34X_ODR_MASK; | ||
163 | reg_odr |= new_odr_mask; | ||
164 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
165 | ADXL34X_ODR_REG, reg_odr); | ||
166 | if (result) { | ||
167 | LOG_RESULT_LOCATION(result); | ||
168 | return result; | ||
169 | } | ||
170 | MPL_LOGV("ODR: %d mHz\n", config->odr); | ||
171 | } | ||
172 | return result; | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * @brief Set the full scale range of the accels | ||
177 | * | ||
178 | * @param mlsl_handle | ||
179 | * the handle to the serial channel the device is connected to. | ||
180 | * @param pdata | ||
181 | * a pointer to the slave platform data. | ||
182 | * @param config | ||
183 | * pointer to configuration. | ||
184 | * @param apply | ||
185 | * whether to apply immediately or save the settings to be applied | ||
186 | * at the next resume. | ||
187 | * @param fsr | ||
188 | * requested full scale range in milli gees (mg). | ||
189 | * | ||
190 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
191 | */ | ||
192 | static int adxl34x_set_fsr(void *mlsl_handle, | ||
193 | struct ext_slave_platform_data *pdata, | ||
194 | struct adxl34x_config *config, | ||
195 | int apply, | ||
196 | long fsr) | ||
197 | { | ||
198 | int result = INV_SUCCESS; | ||
199 | |||
200 | if (fsr <= 2000) { | ||
201 | config->fsr_reg_mask = 0x00; | ||
202 | config->fsr = 2000; | ||
203 | } else if (fsr <= 4000) { | ||
204 | config->fsr_reg_mask = 0x01; | ||
205 | config->fsr = 4000; | ||
206 | } else if (fsr <= 8000) { | ||
207 | config->fsr_reg_mask = 0x02; | ||
208 | config->fsr = 8000; | ||
209 | } else { /* 8001 -> oo */ | ||
210 | config->fsr_reg_mask = 0x03; | ||
211 | config->fsr = 16000; | ||
212 | } | ||
213 | |||
214 | if (apply) { | ||
215 | unsigned char reg_df; | ||
216 | result = inv_serial_read(mlsl_handle, pdata->address, | ||
217 | ADXL34X_DATAFORMAT_REG, 1, ®_df); | ||
218 | reg_df &= ~ADXL34X_DATAFORMAT_FSR_MASK; | ||
219 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
220 | ADXL34X_DATAFORMAT_REG, | ||
221 | reg_df | config->fsr_reg_mask); | ||
222 | if (result) { | ||
223 | LOG_RESULT_LOCATION(result); | ||
224 | return result; | ||
225 | } | ||
226 | MPL_LOGV("FSR: %d mg\n", config->fsr); | ||
227 | } | ||
228 | return result; | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * @brief facility to retrieve the device configuration. | ||
233 | * | ||
234 | * @param mlsl_handle | ||
235 | * the handle to the serial channel the device is connected to. | ||
236 | * @param slave | ||
237 | * a pointer to the slave descriptor data structure. | ||
238 | * @param pdata | ||
239 | * a pointer to the slave platform data. | ||
240 | * @param data | ||
241 | * a pointer to store the returned configuration data structure. | ||
242 | * | ||
243 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
244 | */ | ||
245 | static int adxl34x_get_config(void *mlsl_handle, | ||
246 | struct ext_slave_descr *slave, | ||
247 | struct ext_slave_platform_data *pdata, | ||
248 | struct ext_slave_config *data) | ||
249 | { | ||
250 | struct adxl34x_private_data *private_data = | ||
251 | (struct adxl34x_private_data *)(pdata->private_data); | ||
252 | |||
253 | if (!data->data) | ||
254 | return INV_ERROR_INVALID_PARAMETER; | ||
255 | |||
256 | switch (data->key) { | ||
257 | case MPU_SLAVE_CONFIG_ODR_SUSPEND: | ||
258 | (*(unsigned long *)data->data) = | ||
259 | (unsigned long) private_data->suspend.odr; | ||
260 | break; | ||
261 | case MPU_SLAVE_CONFIG_ODR_RESUME: | ||
262 | (*(unsigned long *)data->data) = | ||
263 | (unsigned long) private_data->resume.odr; | ||
264 | break; | ||
265 | case MPU_SLAVE_CONFIG_FSR_SUSPEND: | ||
266 | (*(unsigned long *)data->data) = | ||
267 | (unsigned long) private_data->suspend.fsr; | ||
268 | break; | ||
269 | case MPU_SLAVE_CONFIG_FSR_RESUME: | ||
270 | (*(unsigned long *)data->data) = | ||
271 | (unsigned long) private_data->resume.fsr; | ||
272 | break; | ||
273 | case MPU_SLAVE_CONFIG_IRQ_SUSPEND: | ||
274 | case MPU_SLAVE_CONFIG_IRQ_RESUME: | ||
275 | default: | ||
276 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
277 | }; | ||
278 | |||
279 | return INV_SUCCESS; | ||
280 | } | ||
281 | |||
282 | /** | ||
283 | * @brief device configuration facility. | ||
284 | * | ||
285 | * @param mlsl_handle | ||
286 | * the handle to the serial channel the device is connected to. | ||
287 | * @param slave | ||
288 | * a pointer to the slave descriptor data structure. | ||
289 | * @param pdata | ||
290 | * a pointer to the slave platform data. | ||
291 | * @param data | ||
292 | * a pointer to the configuration data structure. | ||
293 | * | ||
294 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
295 | */ | ||
296 | static int adxl34x_config(void *mlsl_handle, | ||
297 | struct ext_slave_descr *slave, | ||
298 | struct ext_slave_platform_data *pdata, | ||
299 | struct ext_slave_config *data) | ||
300 | { | ||
301 | struct adxl34x_private_data *private_data = | ||
302 | (struct adxl34x_private_data *)(pdata->private_data); | ||
303 | |||
304 | if (!data->data) | ||
305 | return INV_ERROR_INVALID_PARAMETER; | ||
306 | |||
307 | switch (data->key) { | ||
308 | case MPU_SLAVE_CONFIG_ODR_SUSPEND: | ||
309 | return adxl34x_set_odr(mlsl_handle, pdata, | ||
310 | &private_data->suspend, | ||
311 | data->apply, | ||
312 | *((long *)data->data)); | ||
313 | case MPU_SLAVE_CONFIG_ODR_RESUME: | ||
314 | return adxl34x_set_odr(mlsl_handle, pdata, | ||
315 | &private_data->resume, | ||
316 | data->apply, | ||
317 | *((long *)data->data)); | ||
318 | case MPU_SLAVE_CONFIG_FSR_SUSPEND: | ||
319 | return adxl34x_set_fsr(mlsl_handle, pdata, | ||
320 | &private_data->suspend, | ||
321 | data->apply, | ||
322 | *((long *)data->data)); | ||
323 | case MPU_SLAVE_CONFIG_FSR_RESUME: | ||
324 | return adxl34x_set_fsr(mlsl_handle, pdata, | ||
325 | &private_data->resume, | ||
326 | data->apply, | ||
327 | *((long *)data->data)); | ||
328 | case MPU_SLAVE_CONFIG_IRQ_SUSPEND: | ||
329 | case MPU_SLAVE_CONFIG_IRQ_RESUME: | ||
330 | default: | ||
331 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
332 | }; | ||
333 | return INV_SUCCESS; | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * @brief suspends the device to put it in its lowest power mode. | ||
338 | * | ||
339 | * @param mlsl_handle | ||
340 | * the handle to the serial channel the device is connected to. | ||
341 | * @param slave | ||
342 | * a pointer to the slave descriptor data structure. | ||
343 | * @param pdata | ||
344 | * a pointer to the slave platform data. | ||
345 | * | ||
346 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
347 | */ | ||
348 | static int adxl34x_suspend(void *mlsl_handle, | ||
349 | struct ext_slave_descr *slave, | ||
350 | struct ext_slave_platform_data *pdata) | ||
351 | { | ||
352 | int result; | ||
353 | |||
354 | /* | ||
355 | struct adxl34x_config *suspend_config = | ||
356 | &((struct adxl34x_private_data *)pdata->private_data)->suspend; | ||
357 | |||
358 | result = adxl34x_set_odr(mlsl_handle, pdata, suspend_config, | ||
359 | true, suspend_config->odr); | ||
360 | if (result) { | ||
361 | LOG_RESULT_LOCATION(result); | ||
362 | return result; | ||
363 | } | ||
364 | result = adxl34x_set_fsr(mlsl_handle, pdata, suspend_config, | ||
365 | true, suspend_config->fsr); | ||
366 | if (result) { | ||
367 | LOG_RESULT_LOCATION(result); | ||
368 | return result; | ||
369 | } | ||
370 | */ | ||
371 | |||
372 | /* | ||
373 | Page 25 | ||
374 | When clearing the sleep bit, it is recommended that the part | ||
375 | be placed into standby mode and then set back to measurement mode | ||
376 | with a subsequent write. | ||
377 | This is done to ensure that the device is properly biased if sleep | ||
378 | mode is manually disabled; otherwise, the first few samples of data | ||
379 | after the sleep bit is cleared may have additional noise, | ||
380 | especially if the device was asleep when the bit was cleared. */ | ||
381 | |||
382 | /* go in standy-by mode (suspends measurements) */ | ||
383 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
384 | ADXL34X_PWR_REG, ADXL34X_PWR_MEAS_MASK); | ||
385 | if (result) { | ||
386 | LOG_RESULT_LOCATION(result); | ||
387 | return result; | ||
388 | } | ||
389 | /* and then in sleep */ | ||
390 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
391 | ADXL34X_PWR_REG, | ||
392 | ADXL34X_PWR_MEAS_MASK | ADXL34X_PWR_SLEEP_MASK); | ||
393 | if (result) { | ||
394 | LOG_RESULT_LOCATION(result); | ||
395 | return result; | ||
396 | } | ||
397 | return result; | ||
398 | } | ||
399 | |||
400 | /** | ||
401 | * @brief resume the device in the proper power state given the configuration | ||
402 | * chosen. | ||
403 | * | ||
404 | * @param mlsl_handle | ||
405 | * the handle to the serial channel the device is connected to. | ||
406 | * @param slave | ||
407 | * a pointer to the slave descriptor data structure. | ||
408 | * @param pdata | ||
409 | * a pointer to the slave platform data. | ||
410 | * | ||
411 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
412 | */ | ||
413 | static int adxl34x_resume(void *mlsl_handle, | ||
414 | struct ext_slave_descr *slave, | ||
415 | struct ext_slave_platform_data *pdata) | ||
416 | { | ||
417 | int result = INV_SUCCESS; | ||
418 | struct adxl34x_config *resume_config = | ||
419 | &((struct adxl34x_private_data *)pdata->private_data)->resume; | ||
420 | unsigned char reg; | ||
421 | |||
422 | /* | ||
423 | Page 25 | ||
424 | When clearing the sleep bit, it is recommended that the part | ||
425 | be placed into standby mode and then set back to measurement mode | ||
426 | with a subsequent write. | ||
427 | This is done to ensure that the device is properly biased if sleep | ||
428 | mode is manually disabled; otherwise, the first few samples of data | ||
429 | after the sleep bit is cleared may have additional noise, | ||
430 | especially if the device was asleep when the bit was cleared. */ | ||
431 | |||
432 | /* remove sleep, but leave in stand-by */ | ||
433 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
434 | ADXL34X_PWR_REG, ADXL34X_PWR_MEAS_MASK); | ||
435 | if (result) { | ||
436 | LOG_RESULT_LOCATION(result); | ||
437 | return result; | ||
438 | } | ||
439 | |||
440 | result = adxl34x_set_odr(mlsl_handle, pdata, resume_config, | ||
441 | true, resume_config->odr); | ||
442 | if (result) { | ||
443 | LOG_RESULT_LOCATION(result); | ||
444 | return result; | ||
445 | } | ||
446 | |||
447 | /* | ||
448 | -> FSR | ||
449 | -> Justiy bit for Big endianess | ||
450 | -> resulution to 10 bits | ||
451 | */ | ||
452 | reg = ADXL34X_DATAFORMAT_JUSTIFY_MASK; | ||
453 | reg |= resume_config->fsr_reg_mask; | ||
454 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
455 | ADXL34X_DATAFORMAT_REG, reg); | ||
456 | if (result) { | ||
457 | LOG_RESULT_LOCATION(result); | ||
458 | return result; | ||
459 | } | ||
460 | |||
461 | /* go in measurement mode */ | ||
462 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
463 | ADXL34X_PWR_REG, 0x00); | ||
464 | if (result) { | ||
465 | LOG_RESULT_LOCATION(result); | ||
466 | return result; | ||
467 | } | ||
468 | |||
469 | /* DATA_FORMAT: full resolution of +/-2g; data is left justified */ | ||
470 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
471 | 0x31, reg); | ||
472 | |||
473 | return result; | ||
474 | } | ||
475 | |||
476 | /** | ||
477 | * @brief one-time device driver initialization function. | ||
478 | * If the driver is built as a kernel module, this function will be | ||
479 | * called when the module is loaded in the kernel. | ||
480 | * If the driver is built-in in the kernel, this function will be | ||
481 | * called at boot time. | ||
482 | * | ||
483 | * @param mlsl_handle | ||
484 | * the handle to the serial channel the device is connected to. | ||
485 | * @param slave | ||
486 | * a pointer to the slave descriptor data structure. | ||
487 | * @param pdata | ||
488 | * a pointer to the slave platform data. | ||
489 | * | ||
490 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
491 | */ | ||
492 | static int adxl34x_init(void *mlsl_handle, | ||
493 | struct ext_slave_descr *slave, | ||
494 | struct ext_slave_platform_data *pdata) | ||
495 | { | ||
496 | int result; | ||
497 | long range; | ||
498 | |||
499 | struct adxl34x_private_data *private_data; | ||
500 | private_data = (struct adxl34x_private_data *) | ||
501 | kzalloc(sizeof(struct adxl34x_private_data), GFP_KERNEL); | ||
502 | |||
503 | if (!private_data) | ||
504 | return INV_ERROR_MEMORY_EXAUSTED; | ||
505 | |||
506 | pdata->private_data = private_data; | ||
507 | |||
508 | result = adxl34x_set_odr(mlsl_handle, pdata, &private_data->suspend, | ||
509 | false, 0); | ||
510 | if (result) { | ||
511 | LOG_RESULT_LOCATION(result); | ||
512 | return result; | ||
513 | } | ||
514 | result = adxl34x_set_odr(mlsl_handle, pdata, &private_data->resume, | ||
515 | false, 200000); | ||
516 | if (result) { | ||
517 | LOG_RESULT_LOCATION(result); | ||
518 | return result; | ||
519 | } | ||
520 | |||
521 | range = range_fixedpoint_to_long_mg(slave->range); | ||
522 | result = adxl34x_set_fsr(mlsl_handle, pdata, &private_data->suspend, | ||
523 | false, range); | ||
524 | result = adxl34x_set_fsr(mlsl_handle, pdata, &private_data->resume, | ||
525 | false, range); | ||
526 | if (result) { | ||
527 | LOG_RESULT_LOCATION(result); | ||
528 | return result; | ||
529 | } | ||
530 | |||
531 | result = adxl34x_suspend(mlsl_handle, slave, pdata); | ||
532 | if (result) { | ||
533 | LOG_RESULT_LOCATION(result); | ||
534 | return result; | ||
535 | } | ||
536 | |||
537 | return result; | ||
538 | } | ||
539 | |||
540 | /** | ||
541 | * @brief one-time device driver exit function. | ||
542 | * If the driver is built as a kernel module, this function will be | ||
543 | * called when the module is removed from the kernel. | ||
544 | * | ||
545 | * @param mlsl_handle | ||
546 | * the handle to the serial channel the device is connected to. | ||
547 | * @param slave | ||
548 | * a pointer to the slave descriptor data structure. | ||
549 | * @param pdata | ||
550 | * a pointer to the slave platform data. | ||
551 | * | ||
552 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
553 | */ | ||
554 | static int adxl34x_exit(void *mlsl_handle, | ||
555 | struct ext_slave_descr *slave, | ||
556 | struct ext_slave_platform_data *pdata) | ||
557 | { | ||
558 | kfree(pdata->private_data); | ||
559 | return INV_SUCCESS; | ||
560 | } | ||
561 | |||
562 | /** | ||
563 | * @brief read the sensor data from the device. | ||
564 | * | ||
565 | * @param mlsl_handle | ||
566 | * the handle to the serial channel the device is connected to. | ||
567 | * @param slave | ||
568 | * a pointer to the slave descriptor data structure. | ||
569 | * @param pdata | ||
570 | * a pointer to the slave platform data. | ||
571 | * @param data | ||
572 | * a buffer to store the data read. | ||
573 | * | ||
574 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
575 | */ | ||
576 | static int adxl34x_read(void *mlsl_handle, | ||
577 | struct ext_slave_descr *slave, | ||
578 | struct ext_slave_platform_data *pdata, | ||
579 | unsigned char *data) | ||
580 | { | ||
581 | int result; | ||
582 | result = inv_serial_read(mlsl_handle, pdata->address, | ||
583 | slave->read_reg, slave->read_len, data); | ||
584 | return result; | ||
585 | } | ||
586 | |||
587 | static struct ext_slave_descr adxl34x_descr = { | ||
588 | .init = adxl34x_init, | ||
589 | .exit = adxl34x_exit, | ||
590 | .suspend = adxl34x_suspend, | ||
591 | .resume = adxl34x_resume, | ||
592 | .read = adxl34x_read, | ||
593 | .config = adxl34x_config, | ||
594 | .get_config = adxl34x_get_config, | ||
595 | .name = "adxl34x", /* 5 or 6 */ | ||
596 | .type = EXT_SLAVE_TYPE_ACCEL, | ||
597 | .id = ACCEL_ID_ADXL34X, | ||
598 | .read_reg = 0x32, | ||
599 | .read_len = 6, | ||
600 | .endian = EXT_SLAVE_LITTLE_ENDIAN, | ||
601 | .range = {2, 0}, | ||
602 | .trigger = NULL, | ||
603 | }; | ||
604 | |||
605 | static | ||
606 | struct ext_slave_descr *adxl34x_get_slave_descr(void) | ||
607 | { | ||
608 | return &adxl34x_descr; | ||
609 | } | ||
610 | |||
611 | /* -------------------------------------------------------------------------- */ | ||
612 | struct adxl34x_mod_private_data { | ||
613 | struct i2c_client *client; | ||
614 | struct ext_slave_platform_data *pdata; | ||
615 | }; | ||
616 | |||
617 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
618 | |||
619 | static int adxl34x_mod_probe(struct i2c_client *client, | ||
620 | const struct i2c_device_id *devid) | ||
621 | { | ||
622 | struct ext_slave_platform_data *pdata; | ||
623 | struct adxl34x_mod_private_data *private_data; | ||
624 | int result = 0; | ||
625 | |||
626 | dev_info(&client->adapter->dev, "%s: %s\n", __func__, devid->name); | ||
627 | |||
628 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
629 | result = -ENODEV; | ||
630 | goto out_no_free; | ||
631 | } | ||
632 | |||
633 | pdata = client->dev.platform_data; | ||
634 | if (!pdata) { | ||
635 | dev_err(&client->adapter->dev, | ||
636 | "Missing platform data for slave %s\n", devid->name); | ||
637 | result = -EFAULT; | ||
638 | goto out_no_free; | ||
639 | } | ||
640 | |||
641 | private_data = kzalloc(sizeof(*private_data), GFP_KERNEL); | ||
642 | if (!private_data) { | ||
643 | result = -ENOMEM; | ||
644 | goto out_no_free; | ||
645 | } | ||
646 | |||
647 | i2c_set_clientdata(client, private_data); | ||
648 | private_data->client = client; | ||
649 | private_data->pdata = pdata; | ||
650 | |||
651 | result = inv_mpu_register_slave(THIS_MODULE, client, pdata, | ||
652 | adxl34x_get_slave_descr); | ||
653 | if (result) { | ||
654 | dev_err(&client->adapter->dev, | ||
655 | "Slave registration failed: %s, %d\n", | ||
656 | devid->name, result); | ||
657 | goto out_free_memory; | ||
658 | } | ||
659 | |||
660 | return result; | ||
661 | |||
662 | out_free_memory: | ||
663 | kfree(private_data); | ||
664 | out_no_free: | ||
665 | dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result); | ||
666 | return result; | ||
667 | |||
668 | } | ||
669 | |||
670 | static int adxl34x_mod_remove(struct i2c_client *client) | ||
671 | { | ||
672 | struct adxl34x_mod_private_data *private_data = | ||
673 | i2c_get_clientdata(client); | ||
674 | |||
675 | dev_dbg(&client->adapter->dev, "%s\n", __func__); | ||
676 | |||
677 | inv_mpu_unregister_slave(client, private_data->pdata, | ||
678 | adxl34x_get_slave_descr); | ||
679 | |||
680 | kfree(private_data); | ||
681 | return 0; | ||
682 | } | ||
683 | |||
684 | static const struct i2c_device_id adxl34x_mod_id[] = { | ||
685 | { "adxl34x", ACCEL_ID_ADXL34X }, | ||
686 | {} | ||
687 | }; | ||
688 | |||
689 | MODULE_DEVICE_TABLE(i2c, adxl34x_mod_id); | ||
690 | |||
691 | static struct i2c_driver adxl34x_mod_driver = { | ||
692 | .class = I2C_CLASS_HWMON, | ||
693 | .probe = adxl34x_mod_probe, | ||
694 | .remove = adxl34x_mod_remove, | ||
695 | .id_table = adxl34x_mod_id, | ||
696 | .driver = { | ||
697 | .owner = THIS_MODULE, | ||
698 | .name = "adxl34x_mod", | ||
699 | }, | ||
700 | .address_list = normal_i2c, | ||
701 | }; | ||
702 | |||
703 | static int __init adxl34x_mod_init(void) | ||
704 | { | ||
705 | int res = i2c_add_driver(&adxl34x_mod_driver); | ||
706 | pr_info("%s: Probe name %s\n", __func__, "adxl34x_mod"); | ||
707 | if (res) | ||
708 | pr_err("%s failed\n", __func__); | ||
709 | return res; | ||
710 | } | ||
711 | |||
712 | static void __exit adxl34x_mod_exit(void) | ||
713 | { | ||
714 | pr_info("%s\n", __func__); | ||
715 | i2c_del_driver(&adxl34x_mod_driver); | ||
716 | } | ||
717 | |||
718 | module_init(adxl34x_mod_init); | ||
719 | module_exit(adxl34x_mod_exit); | ||
720 | |||
721 | MODULE_AUTHOR("Invensense Corporation"); | ||
722 | MODULE_DESCRIPTION("Driver to integrate ADXL34X sensor with the MPU"); | ||
723 | MODULE_LICENSE("GPL"); | ||
724 | MODULE_ALIAS("adxl34x_mod"); | ||
725 | |||
726 | /** | ||
727 | * @} | ||
728 | */ | ||