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/mma845x.c | |
parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) |
Diffstat (limited to 'drivers/misc/inv_mpu/accel/mma845x.c')
-rw-r--r-- | drivers/misc/inv_mpu/accel/mma845x.c | 713 |
1 files changed, 713 insertions, 0 deletions
diff --git a/drivers/misc/inv_mpu/accel/mma845x.c b/drivers/misc/inv_mpu/accel/mma845x.c new file mode 100644 index 00000000000..5f62b22388b --- /dev/null +++ b/drivers/misc/inv_mpu/accel/mma845x.c | |||
@@ -0,0 +1,713 @@ | |||
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 mma845x.c | ||
26 | * @brief Accelerometer setup and handling methods for Freescale MMA845X | ||
27 | */ | ||
28 | |||
29 | /* -------------------------------------------------------------------------- */ | ||
30 | |||
31 | #include <linux/i2c.h> | ||
32 | #include <linux/module.h> | ||
33 | #include <linux/moduleparam.h> | ||
34 | #include <linux/kernel.h> | ||
35 | #include <linux/errno.h> | ||
36 | #include <linux/slab.h> | ||
37 | #include <linux/delay.h> | ||
38 | #include "mpu-dev.h" | ||
39 | |||
40 | #include <log.h> | ||
41 | #include <linux/mpu.h> | ||
42 | #include "mlsl.h" | ||
43 | #include "mldl_cfg.h" | ||
44 | #undef MPL_LOG_TAG | ||
45 | #define MPL_LOG_TAG "MPL-acc" | ||
46 | |||
47 | #define ACCEL_MMA845X_XYZ_DATA_CFG (0x0E) | ||
48 | #define ACCEL_MMA845X_CTRL_REG1 (0x2A) | ||
49 | #define ACCEL_MMA845X_CTRL_REG4 (0x2D) | ||
50 | #define ACCEL_MMA845X_CTRL_REG5 (0x2E) | ||
51 | |||
52 | #define ACCEL_MMA845X_SLEEP_MASK (0x01) | ||
53 | |||
54 | /* full scale setting - register & mask */ | ||
55 | #define ACCEL_MMA845X_CFG_REG (0x0E) | ||
56 | #define ACCEL_MMA845X_CTRL_MASK (0x03) | ||
57 | |||
58 | /* -------------------------------------------------------------------------- */ | ||
59 | |||
60 | struct mma845x_config { | ||
61 | unsigned int odr; | ||
62 | unsigned int fsr; /** < full scale range mg */ | ||
63 | unsigned int ths; /** < Motion no-motion thseshold mg */ | ||
64 | unsigned int dur; /** < Motion no-motion duration ms */ | ||
65 | unsigned char reg_ths; | ||
66 | unsigned char reg_dur; | ||
67 | unsigned char ctrl_reg1; | ||
68 | unsigned char irq_type; | ||
69 | unsigned char mot_int1_cfg; | ||
70 | }; | ||
71 | |||
72 | struct mma845x_private_data { | ||
73 | struct mma845x_config suspend; | ||
74 | struct mma845x_config resume; | ||
75 | }; | ||
76 | |||
77 | /* -------------------------------------------------------------------------- */ | ||
78 | |||
79 | static int mma845x_set_ths(void *mlsl_handle, | ||
80 | struct ext_slave_platform_data *pdata, | ||
81 | struct mma845x_config *config, | ||
82 | int apply, | ||
83 | long ths) | ||
84 | { | ||
85 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
86 | } | ||
87 | |||
88 | static int mma845x_set_dur(void *mlsl_handle, | ||
89 | struct ext_slave_platform_data *pdata, | ||
90 | struct mma845x_config *config, | ||
91 | int apply, | ||
92 | long dur) | ||
93 | { | ||
94 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
95 | } | ||
96 | |||
97 | /** | ||
98 | * @brief Sets the IRQ to fire when one of the IRQ events occur. | ||
99 | * Threshold and duration will not be used unless the type is MOT or | ||
100 | * NMOT. | ||
101 | * | ||
102 | * @param mlsl_handle | ||
103 | * the handle to the serial channel the device is connected to. | ||
104 | * @param pdata | ||
105 | * a pointer to the slave platform data. | ||
106 | * @param config | ||
107 | * configuration to apply to, suspend or resume | ||
108 | * @param apply | ||
109 | * whether to apply immediately or save the settings to be applied | ||
110 | * at the next resume. | ||
111 | * @param irq_type | ||
112 | * the type of IRQ. Valid values are | ||
113 | * - MPU_SLAVE_IRQ_TYPE_NONE | ||
114 | * - MPU_SLAVE_IRQ_TYPE_MOTION | ||
115 | * - MPU_SLAVE_IRQ_TYPE_DATA_READY | ||
116 | * | ||
117 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
118 | */ | ||
119 | static int mma845x_set_irq(void *mlsl_handle, | ||
120 | struct ext_slave_platform_data *pdata, | ||
121 | struct mma845x_config *config, | ||
122 | int apply, | ||
123 | long irq_type) | ||
124 | { | ||
125 | int result = INV_SUCCESS; | ||
126 | unsigned char reg1; | ||
127 | unsigned char reg2; | ||
128 | |||
129 | config->irq_type = (unsigned char)irq_type; | ||
130 | if (irq_type == MPU_SLAVE_IRQ_TYPE_DATA_READY) { | ||
131 | reg1 = 0x01; | ||
132 | reg2 = 0x01; | ||
133 | } else if (irq_type == MPU_SLAVE_IRQ_TYPE_NONE) { | ||
134 | reg1 = 0x00; | ||
135 | reg2 = 0x00; | ||
136 | } else { | ||
137 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
138 | } | ||
139 | |||
140 | if (apply) { | ||
141 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
142 | ACCEL_MMA845X_CTRL_REG4, reg1); | ||
143 | if (result) { | ||
144 | LOG_RESULT_LOCATION(result); | ||
145 | return result; | ||
146 | } | ||
147 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
148 | ACCEL_MMA845X_CTRL_REG5, reg2); | ||
149 | if (result) { | ||
150 | LOG_RESULT_LOCATION(result); | ||
151 | return result; | ||
152 | } | ||
153 | } | ||
154 | |||
155 | return result; | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * @brief Set the output data rate for the particular configuration. | ||
160 | * | ||
161 | * @param mlsl_handle | ||
162 | * the handle to the serial channel the device is connected to. | ||
163 | * @param pdata | ||
164 | * a pointer to the slave platform data. | ||
165 | * @param config | ||
166 | * Config to modify with new ODR. | ||
167 | * @param apply | ||
168 | * whether to apply immediately or save the settings to be applied | ||
169 | * at the next resume. | ||
170 | * @param odr | ||
171 | * Output data rate in units of 1/1000Hz (mHz). | ||
172 | * | ||
173 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
174 | */ | ||
175 | static int mma845x_set_odr(void *mlsl_handle, | ||
176 | struct ext_slave_platform_data *pdata, | ||
177 | struct mma845x_config *config, | ||
178 | int apply, | ||
179 | long odr) | ||
180 | { | ||
181 | unsigned char bits; | ||
182 | int result = INV_SUCCESS; | ||
183 | |||
184 | if (odr > 400000) { | ||
185 | config->odr = 800000; | ||
186 | bits = 0x01; | ||
187 | } else if (odr > 200000) { | ||
188 | config->odr = 400000; | ||
189 | bits = 0x09; | ||
190 | } else if (odr > 100000) { | ||
191 | config->odr = 200000; | ||
192 | bits = 0x11; | ||
193 | } else if (odr > 50000) { | ||
194 | config->odr = 100000; | ||
195 | bits = 0x19; | ||
196 | } else if (odr > 12500) { | ||
197 | config->odr = 50000; | ||
198 | bits = 0x21; | ||
199 | } else if (odr > 6250) { | ||
200 | config->odr = 12500; | ||
201 | bits = 0x29; | ||
202 | } else if (odr > 1560) { | ||
203 | config->odr = 6250; | ||
204 | bits = 0x31; | ||
205 | } else if (odr > 0) { | ||
206 | config->odr = 1560; | ||
207 | bits = 0x39; | ||
208 | } else { | ||
209 | config->ctrl_reg1 = 0; /* Set FS1.FS2 to Standby */ | ||
210 | config->odr = 0; | ||
211 | bits = 0; | ||
212 | } | ||
213 | |||
214 | config->ctrl_reg1 = bits; | ||
215 | if (apply) { | ||
216 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
217 | ACCEL_MMA845X_CTRL_REG1, | ||
218 | config->ctrl_reg1); | ||
219 | if (result) { | ||
220 | LOG_RESULT_LOCATION(result); | ||
221 | return result; | ||
222 | } | ||
223 | MPL_LOGV("ODR: %d mHz, 0x%02x\n", config->odr, | ||
224 | (int)config->ctrl_reg1); | ||
225 | } | ||
226 | return result; | ||
227 | } | ||
228 | |||
229 | /** | ||
230 | * @brief Set the full scale range of the accels | ||
231 | * | ||
232 | * @param mlsl_handle | ||
233 | * the handle to the serial channel the device is connected to. | ||
234 | * @param pdata | ||
235 | * a pointer to the slave platform data. | ||
236 | * @param config | ||
237 | * pointer to configuration. | ||
238 | * @param apply | ||
239 | * whether to apply immediately or save the settings to be applied | ||
240 | * at the next resume. | ||
241 | * @param fsr | ||
242 | * requested full scale range. | ||
243 | * | ||
244 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
245 | */ | ||
246 | static int mma845x_set_fsr(void *mlsl_handle, | ||
247 | struct ext_slave_platform_data *pdata, | ||
248 | struct mma845x_config *config, | ||
249 | int apply, | ||
250 | long fsr) | ||
251 | { | ||
252 | unsigned char bits; | ||
253 | int result = INV_SUCCESS; | ||
254 | |||
255 | if (fsr <= 2000) { | ||
256 | bits = 0x00; | ||
257 | config->fsr = 2000; | ||
258 | } else if (fsr <= 4000) { | ||
259 | bits = 0x01; | ||
260 | config->fsr = 4000; | ||
261 | } else { | ||
262 | bits = 0x02; | ||
263 | config->fsr = 8000; | ||
264 | } | ||
265 | |||
266 | if (apply) { | ||
267 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
268 | ACCEL_MMA845X_XYZ_DATA_CFG, | ||
269 | bits); | ||
270 | if (result) { | ||
271 | LOG_RESULT_LOCATION(result); | ||
272 | return result; | ||
273 | } | ||
274 | MPL_LOGV("FSR: %d mg\n", config->fsr); | ||
275 | } | ||
276 | return result; | ||
277 | } | ||
278 | |||
279 | /** | ||
280 | * @brief suspends the device to put it in its lowest power mode. | ||
281 | * | ||
282 | * @param mlsl_handle | ||
283 | * the handle to the serial channel the device is connected to. | ||
284 | * @param slave | ||
285 | * a pointer to the slave descriptor data structure. | ||
286 | * @param pdata | ||
287 | * a pointer to the slave platform data. | ||
288 | * | ||
289 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
290 | */ | ||
291 | static int mma845x_suspend(void *mlsl_handle, | ||
292 | struct ext_slave_descr *slave, | ||
293 | struct ext_slave_platform_data *pdata) | ||
294 | { | ||
295 | int result; | ||
296 | struct mma845x_private_data *private_data = pdata->private_data; | ||
297 | |||
298 | /* Full Scale */ | ||
299 | if (private_data->suspend.fsr == 4000) | ||
300 | slave->range.mantissa = 4; | ||
301 | else if (private_data->suspend.fsr == 8000) | ||
302 | slave->range.mantissa = 8; | ||
303 | else | ||
304 | slave->range.mantissa = 2; | ||
305 | |||
306 | slave->range.fraction = 0; | ||
307 | |||
308 | result = mma845x_set_fsr(mlsl_handle, pdata, | ||
309 | &private_data->suspend, | ||
310 | true, private_data->suspend.fsr); | ||
311 | if (result) { | ||
312 | LOG_RESULT_LOCATION(result); | ||
313 | return result; | ||
314 | } | ||
315 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
316 | ACCEL_MMA845X_CTRL_REG1, | ||
317 | private_data->suspend.ctrl_reg1); | ||
318 | if (result) { | ||
319 | LOG_RESULT_LOCATION(result); | ||
320 | return result; | ||
321 | } | ||
322 | |||
323 | return result; | ||
324 | } | ||
325 | |||
326 | /** | ||
327 | * @brief resume the device in the proper power state given the configuration | ||
328 | * chosen. | ||
329 | * | ||
330 | * @param mlsl_handle | ||
331 | * the handle to the serial channel the device is connected to. | ||
332 | * @param slave | ||
333 | * a pointer to the slave descriptor data structure. | ||
334 | * @param pdata | ||
335 | * a pointer to the slave platform data. | ||
336 | * | ||
337 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
338 | */ | ||
339 | static int mma845x_resume(void *mlsl_handle, | ||
340 | struct ext_slave_descr *slave, | ||
341 | struct ext_slave_platform_data *pdata) | ||
342 | { | ||
343 | int result = INV_SUCCESS; | ||
344 | struct mma845x_private_data *private_data = pdata->private_data; | ||
345 | |||
346 | /* Full Scale */ | ||
347 | if (private_data->resume.fsr == 4000) | ||
348 | slave->range.mantissa = 4; | ||
349 | else if (private_data->resume.fsr == 8000) | ||
350 | slave->range.mantissa = 8; | ||
351 | else | ||
352 | slave->range.mantissa = 2; | ||
353 | |||
354 | slave->range.fraction = 0; | ||
355 | |||
356 | result = mma845x_set_fsr(mlsl_handle, pdata, | ||
357 | &private_data->resume, | ||
358 | true, private_data->resume.fsr); | ||
359 | if (result) { | ||
360 | LOG_RESULT_LOCATION(result); | ||
361 | return result; | ||
362 | } | ||
363 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
364 | ACCEL_MMA845X_CTRL_REG1, | ||
365 | private_data->resume.ctrl_reg1); | ||
366 | if (result) { | ||
367 | LOG_RESULT_LOCATION(result); | ||
368 | return result; | ||
369 | } | ||
370 | |||
371 | return result; | ||
372 | } | ||
373 | |||
374 | /** | ||
375 | * @brief read the sensor data from the device. | ||
376 | * | ||
377 | * @param mlsl_handle | ||
378 | * the handle to the serial channel the device is connected to. | ||
379 | * @param slave | ||
380 | * a pointer to the slave descriptor data structure. | ||
381 | * @param pdata | ||
382 | * a pointer to the slave platform data. | ||
383 | * @param data | ||
384 | * a buffer to store the data read. | ||
385 | * | ||
386 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
387 | */ | ||
388 | static int mma845x_read(void *mlsl_handle, | ||
389 | struct ext_slave_descr *slave, | ||
390 | struct ext_slave_platform_data *pdata, unsigned char *data) | ||
391 | { | ||
392 | int result; | ||
393 | unsigned char local_data[7]; /* Status register + 6 bytes data */ | ||
394 | result = inv_serial_read(mlsl_handle, pdata->address, | ||
395 | slave->read_reg, sizeof(local_data), | ||
396 | local_data); | ||
397 | if (result) { | ||
398 | LOG_RESULT_LOCATION(result); | ||
399 | return result; | ||
400 | } | ||
401 | memcpy(data, &local_data[1], slave->read_len); | ||
402 | return result; | ||
403 | } | ||
404 | |||
405 | static int mma845x_init(void *mlsl_handle, | ||
406 | struct ext_slave_descr *slave, | ||
407 | struct ext_slave_platform_data *pdata) | ||
408 | { | ||
409 | long range; | ||
410 | struct mma845x_private_data *private_data; | ||
411 | private_data = (struct mma845x_private_data *) | ||
412 | kzalloc(sizeof(struct mma845x_private_data), GFP_KERNEL); | ||
413 | |||
414 | if (!private_data) | ||
415 | return INV_ERROR_MEMORY_EXAUSTED; | ||
416 | |||
417 | pdata->private_data = private_data; | ||
418 | |||
419 | mma845x_set_odr(mlsl_handle, pdata, &private_data->suspend, | ||
420 | false, 0); | ||
421 | mma845x_set_odr(mlsl_handle, pdata, &private_data->resume, | ||
422 | false, 200000); | ||
423 | |||
424 | range = range_fixedpoint_to_long_mg(slave->range); | ||
425 | mma845x_set_fsr(mlsl_handle, pdata, &private_data->suspend, | ||
426 | false, range); | ||
427 | mma845x_set_fsr(mlsl_handle, pdata, &private_data->resume, | ||
428 | false, range); | ||
429 | |||
430 | mma845x_set_irq(mlsl_handle, pdata, &private_data->suspend, | ||
431 | false, MPU_SLAVE_IRQ_TYPE_NONE); | ||
432 | mma845x_set_irq(mlsl_handle, pdata, &private_data->resume, | ||
433 | false, MPU_SLAVE_IRQ_TYPE_NONE); | ||
434 | return INV_SUCCESS; | ||
435 | } | ||
436 | |||
437 | static int mma845x_exit(void *mlsl_handle, | ||
438 | struct ext_slave_descr *slave, | ||
439 | struct ext_slave_platform_data *pdata) | ||
440 | { | ||
441 | kfree(pdata->private_data); | ||
442 | return INV_SUCCESS; | ||
443 | } | ||
444 | |||
445 | static int mma845x_config(void *mlsl_handle, | ||
446 | struct ext_slave_descr *slave, | ||
447 | struct ext_slave_platform_data *pdata, | ||
448 | struct ext_slave_config *data) | ||
449 | { | ||
450 | struct mma845x_private_data *private_data = pdata->private_data; | ||
451 | if (!data->data) | ||
452 | return INV_ERROR_INVALID_PARAMETER; | ||
453 | |||
454 | switch (data->key) { | ||
455 | case MPU_SLAVE_CONFIG_ODR_SUSPEND: | ||
456 | return mma845x_set_odr(mlsl_handle, pdata, | ||
457 | &private_data->suspend, | ||
458 | data->apply, | ||
459 | *((long *)data->data)); | ||
460 | case MPU_SLAVE_CONFIG_ODR_RESUME: | ||
461 | return mma845x_set_odr(mlsl_handle, pdata, | ||
462 | &private_data->resume, | ||
463 | data->apply, | ||
464 | *((long *)data->data)); | ||
465 | case MPU_SLAVE_CONFIG_FSR_SUSPEND: | ||
466 | return mma845x_set_fsr(mlsl_handle, pdata, | ||
467 | &private_data->suspend, | ||
468 | data->apply, | ||
469 | *((long *)data->data)); | ||
470 | case MPU_SLAVE_CONFIG_FSR_RESUME: | ||
471 | return mma845x_set_fsr(mlsl_handle, pdata, | ||
472 | &private_data->resume, | ||
473 | data->apply, | ||
474 | *((long *)data->data)); | ||
475 | case MPU_SLAVE_CONFIG_MOT_THS: | ||
476 | return mma845x_set_ths(mlsl_handle, pdata, | ||
477 | &private_data->suspend, | ||
478 | data->apply, | ||
479 | *((long *)data->data)); | ||
480 | case MPU_SLAVE_CONFIG_NMOT_THS: | ||
481 | return mma845x_set_ths(mlsl_handle, pdata, | ||
482 | &private_data->resume, | ||
483 | data->apply, | ||
484 | *((long *)data->data)); | ||
485 | case MPU_SLAVE_CONFIG_MOT_DUR: | ||
486 | return mma845x_set_dur(mlsl_handle, pdata, | ||
487 | &private_data->suspend, | ||
488 | data->apply, | ||
489 | *((long *)data->data)); | ||
490 | case MPU_SLAVE_CONFIG_NMOT_DUR: | ||
491 | return mma845x_set_dur(mlsl_handle, pdata, | ||
492 | &private_data->resume, | ||
493 | data->apply, | ||
494 | *((long *)data->data)); | ||
495 | case MPU_SLAVE_CONFIG_IRQ_SUSPEND: | ||
496 | return mma845x_set_irq(mlsl_handle, pdata, | ||
497 | &private_data->suspend, | ||
498 | data->apply, | ||
499 | *((long *)data->data)); | ||
500 | case MPU_SLAVE_CONFIG_IRQ_RESUME: | ||
501 | return mma845x_set_irq(mlsl_handle, pdata, | ||
502 | &private_data->resume, | ||
503 | data->apply, | ||
504 | *((long *)data->data)); | ||
505 | default: | ||
506 | LOG_RESULT_LOCATION(INV_ERROR_FEATURE_NOT_IMPLEMENTED); | ||
507 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
508 | }; | ||
509 | |||
510 | return INV_SUCCESS; | ||
511 | } | ||
512 | |||
513 | static int mma845x_get_config(void *mlsl_handle, | ||
514 | struct ext_slave_descr *slave, | ||
515 | struct ext_slave_platform_data *pdata, | ||
516 | struct ext_slave_config *data) | ||
517 | { | ||
518 | struct mma845x_private_data *private_data = pdata->private_data; | ||
519 | if (!data->data) | ||
520 | return INV_ERROR_INVALID_PARAMETER; | ||
521 | |||
522 | switch (data->key) { | ||
523 | case MPU_SLAVE_CONFIG_ODR_SUSPEND: | ||
524 | (*(unsigned long *)data->data) = | ||
525 | (unsigned long) private_data->suspend.odr; | ||
526 | break; | ||
527 | case MPU_SLAVE_CONFIG_ODR_RESUME: | ||
528 | (*(unsigned long *)data->data) = | ||
529 | (unsigned long) private_data->resume.odr; | ||
530 | break; | ||
531 | case MPU_SLAVE_CONFIG_FSR_SUSPEND: | ||
532 | (*(unsigned long *)data->data) = | ||
533 | (unsigned long) private_data->suspend.fsr; | ||
534 | break; | ||
535 | case MPU_SLAVE_CONFIG_FSR_RESUME: | ||
536 | (*(unsigned long *)data->data) = | ||
537 | (unsigned long) private_data->resume.fsr; | ||
538 | break; | ||
539 | case MPU_SLAVE_CONFIG_MOT_THS: | ||
540 | (*(unsigned long *)data->data) = | ||
541 | (unsigned long) private_data->suspend.ths; | ||
542 | break; | ||
543 | case MPU_SLAVE_CONFIG_NMOT_THS: | ||
544 | (*(unsigned long *)data->data) = | ||
545 | (unsigned long) private_data->resume.ths; | ||
546 | break; | ||
547 | case MPU_SLAVE_CONFIG_MOT_DUR: | ||
548 | (*(unsigned long *)data->data) = | ||
549 | (unsigned long) private_data->suspend.dur; | ||
550 | break; | ||
551 | case MPU_SLAVE_CONFIG_NMOT_DUR: | ||
552 | (*(unsigned long *)data->data) = | ||
553 | (unsigned long) private_data->resume.dur; | ||
554 | break; | ||
555 | case MPU_SLAVE_CONFIG_IRQ_SUSPEND: | ||
556 | (*(unsigned long *)data->data) = | ||
557 | (unsigned long) private_data->suspend.irq_type; | ||
558 | break; | ||
559 | case MPU_SLAVE_CONFIG_IRQ_RESUME: | ||
560 | (*(unsigned long *)data->data) = | ||
561 | (unsigned long) private_data->resume.irq_type; | ||
562 | break; | ||
563 | default: | ||
564 | LOG_RESULT_LOCATION(INV_ERROR_FEATURE_NOT_IMPLEMENTED); | ||
565 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
566 | }; | ||
567 | |||
568 | return INV_SUCCESS; | ||
569 | } | ||
570 | |||
571 | static struct ext_slave_descr mma845x_descr = { | ||
572 | .init = mma845x_init, | ||
573 | .exit = mma845x_exit, | ||
574 | .suspend = mma845x_suspend, | ||
575 | .resume = mma845x_resume, | ||
576 | .read = mma845x_read, | ||
577 | .config = mma845x_config, | ||
578 | .get_config = mma845x_get_config, | ||
579 | .name = "mma845x", | ||
580 | .type = EXT_SLAVE_TYPE_ACCEL, | ||
581 | .id = ACCEL_ID_MMA845X, | ||
582 | .read_reg = 0x00, | ||
583 | .read_len = 6, | ||
584 | .endian = EXT_SLAVE_FS16_BIG_ENDIAN, | ||
585 | .range = {2, 0}, | ||
586 | .trigger = NULL, | ||
587 | }; | ||
588 | |||
589 | static | ||
590 | struct ext_slave_descr *mma845x_get_slave_descr(void) | ||
591 | { | ||
592 | return &mma845x_descr; | ||
593 | } | ||
594 | |||
595 | /* -------------------------------------------------------------------------- */ | ||
596 | struct mma845x_mod_private_data { | ||
597 | struct i2c_client *client; | ||
598 | struct ext_slave_platform_data *pdata; | ||
599 | }; | ||
600 | |||
601 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
602 | |||
603 | static int mma845x_mod_probe(struct i2c_client *client, | ||
604 | const struct i2c_device_id *devid) | ||
605 | { | ||
606 | struct ext_slave_platform_data *pdata; | ||
607 | struct mma845x_mod_private_data *private_data; | ||
608 | int result = 0; | ||
609 | |||
610 | dev_info(&client->adapter->dev, "%s: %s\n", __func__, devid->name); | ||
611 | |||
612 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
613 | result = -ENODEV; | ||
614 | goto out_no_free; | ||
615 | } | ||
616 | |||
617 | pdata = client->dev.platform_data; | ||
618 | if (!pdata) { | ||
619 | dev_err(&client->adapter->dev, | ||
620 | "Missing platform data for slave %s\n", devid->name); | ||
621 | result = -EFAULT; | ||
622 | goto out_no_free; | ||
623 | } | ||
624 | |||
625 | private_data = kzalloc(sizeof(*private_data), GFP_KERNEL); | ||
626 | if (!private_data) { | ||
627 | result = -ENOMEM; | ||
628 | goto out_no_free; | ||
629 | } | ||
630 | |||
631 | i2c_set_clientdata(client, private_data); | ||
632 | private_data->client = client; | ||
633 | private_data->pdata = pdata; | ||
634 | |||
635 | result = inv_mpu_register_slave(THIS_MODULE, client, pdata, | ||
636 | mma845x_get_slave_descr); | ||
637 | if (result) { | ||
638 | dev_err(&client->adapter->dev, | ||
639 | "Slave registration failed: %s, %d\n", | ||
640 | devid->name, result); | ||
641 | goto out_free_memory; | ||
642 | } | ||
643 | |||
644 | return result; | ||
645 | |||
646 | out_free_memory: | ||
647 | kfree(private_data); | ||
648 | out_no_free: | ||
649 | dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result); | ||
650 | return result; | ||
651 | |||
652 | } | ||
653 | |||
654 | static int mma845x_mod_remove(struct i2c_client *client) | ||
655 | { | ||
656 | struct mma845x_mod_private_data *private_data = | ||
657 | i2c_get_clientdata(client); | ||
658 | |||
659 | dev_dbg(&client->adapter->dev, "%s\n", __func__); | ||
660 | |||
661 | inv_mpu_unregister_slave(client, private_data->pdata, | ||
662 | mma845x_get_slave_descr); | ||
663 | |||
664 | kfree(private_data); | ||
665 | return 0; | ||
666 | } | ||
667 | |||
668 | static const struct i2c_device_id mma845x_mod_id[] = { | ||
669 | { "mma845x", ACCEL_ID_MMA845X }, | ||
670 | {} | ||
671 | }; | ||
672 | |||
673 | MODULE_DEVICE_TABLE(i2c, mma845x_mod_id); | ||
674 | |||
675 | static struct i2c_driver mma845x_mod_driver = { | ||
676 | .class = I2C_CLASS_HWMON, | ||
677 | .probe = mma845x_mod_probe, | ||
678 | .remove = mma845x_mod_remove, | ||
679 | .id_table = mma845x_mod_id, | ||
680 | .driver = { | ||
681 | .owner = THIS_MODULE, | ||
682 | .name = "mma845x_mod", | ||
683 | }, | ||
684 | .address_list = normal_i2c, | ||
685 | }; | ||
686 | |||
687 | static int __init mma845x_mod_init(void) | ||
688 | { | ||
689 | int res = i2c_add_driver(&mma845x_mod_driver); | ||
690 | pr_info("%s: Probe name %s\n", __func__, "mma845x_mod"); | ||
691 | if (res) | ||
692 | pr_err("%s failed\n", __func__); | ||
693 | return res; | ||
694 | } | ||
695 | |||
696 | static void __exit mma845x_mod_exit(void) | ||
697 | { | ||
698 | pr_info("%s\n", __func__); | ||
699 | i2c_del_driver(&mma845x_mod_driver); | ||
700 | } | ||
701 | |||
702 | module_init(mma845x_mod_init); | ||
703 | module_exit(mma845x_mod_exit); | ||
704 | |||
705 | MODULE_AUTHOR("Invensense Corporation"); | ||
706 | MODULE_DESCRIPTION("Driver to integrate MMA845X sensor with the MPU"); | ||
707 | MODULE_LICENSE("GPL"); | ||
708 | MODULE_ALIAS("mma845x_mod"); | ||
709 | |||
710 | |||
711 | /** | ||
712 | * @} | ||
713 | */ | ||