aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/inv_mpu/accel/mma8450.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-22 10:38:37 -0500
commitfcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch)
treea57612d1888735a2ec7972891b68c1ac5ec8faea /drivers/misc/inv_mpu/accel/mma8450.c
parent8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff)
Added missing tegra files.HEADmaster
Diffstat (limited to 'drivers/misc/inv_mpu/accel/mma8450.c')
-rw-r--r--drivers/misc/inv_mpu/accel/mma8450.c804
1 files changed, 804 insertions, 0 deletions
diff --git a/drivers/misc/inv_mpu/accel/mma8450.c b/drivers/misc/inv_mpu/accel/mma8450.c
new file mode 100644
index 00000000000..f698ee98bf5
--- /dev/null
+++ b/drivers/misc/inv_mpu/accel/mma8450.c
@@ -0,0 +1,804 @@
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 mma8450.c
26 * @brief Accelerometer setup and handling methods for Freescale MMA8450.
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/* full scale setting - register & mask */
48#define ACCEL_MMA8450_XYZ_DATA_CFG (0x16)
49
50#define ACCEL_MMA8450_CTRL_REG1 (0x38)
51#define ACCEL_MMA8450_CTRL_REG2 (0x39)
52#define ACCEL_MMA8450_CTRL_REG4 (0x3B)
53#define ACCEL_MMA8450_CTRL_REG5 (0x3C)
54
55#define ACCEL_MMA8450_CTRL_REG (0x38)
56#define ACCEL_MMA8450_CTRL_MASK (0x03)
57
58#define ACCEL_MMA8450_SLEEP_MASK (0x03)
59
60/* -------------------------------------------------------------------------- */
61
62struct mma8450_config {
63 unsigned int odr;
64 unsigned int fsr; /** < full scale range mg */
65 unsigned int ths; /** < Motion no-motion thseshold mg */
66 unsigned int dur; /** < Motion no-motion duration ms */
67 unsigned char reg_ths;
68 unsigned char reg_dur;
69 unsigned char ctrl_reg1;
70 unsigned char irq_type;
71 unsigned char mot_int1_cfg;
72};
73
74struct mma8450_private_data {
75 struct mma8450_config suspend;
76 struct mma8450_config resume;
77};
78
79
80/* -------------------------------------------------------------------------- */
81
82static int mma8450_set_ths(void *mlsl_handle,
83 struct ext_slave_platform_data *pdata,
84 struct mma8450_config *config,
85 int apply,
86 long ths)
87{
88 return INV_ERROR_FEATURE_NOT_IMPLEMENTED;
89}
90
91static int mma8450_set_dur(void *mlsl_handle,
92 struct ext_slave_platform_data *pdata,
93 struct mma8450_config *config,
94 int apply,
95 long dur)
96{
97 return INV_ERROR_FEATURE_NOT_IMPLEMENTED;
98}
99
100/**
101 * @brief Sets the IRQ to fire when one of the IRQ events occur.
102 * Threshold and duration will not be used unless the type is MOT or
103 * NMOT.
104 *
105 * @param mlsl_handle
106 * the handle to the serial channel the device is connected to.
107 * @param pdata
108 * a pointer to the slave platform data.
109 * @param config
110 * configuration to apply to, suspend or resume
111 * @param apply
112 * whether to apply immediately or save the settings to be applied
113 * at the next resume.
114 * @param irq_type
115 * the type of IRQ. Valid values are
116 * - MPU_SLAVE_IRQ_TYPE_NONE
117 * - MPU_SLAVE_IRQ_TYPE_MOTION
118 * - MPU_SLAVE_IRQ_TYPE_DATA_READY
119 *
120 * @return INV_SUCCESS if successful or a non-zero error code.
121 */
122static int mma8450_set_irq(void *mlsl_handle,
123 struct ext_slave_platform_data *pdata,
124 struct mma8450_config *config,
125 int apply,
126 long irq_type)
127{
128 int result = INV_SUCCESS;
129 unsigned char reg1;
130 unsigned char reg2;
131 unsigned char reg3;
132
133 config->irq_type = (unsigned char)irq_type;
134 if (irq_type == MPU_SLAVE_IRQ_TYPE_DATA_READY) {
135 reg1 = 0x01;
136 reg2 = 0x01;
137 reg3 = 0x07;
138 } else if (irq_type == MPU_SLAVE_IRQ_TYPE_NONE) {
139 reg1 = 0x00;
140 reg2 = 0x00;
141 reg3 = 0x00;
142 } else {
143 return INV_ERROR_FEATURE_NOT_IMPLEMENTED;
144 }
145
146 if (apply) {
147 /* XYZ_DATA_CFG: event flag enabled on Z axis */
148 result = inv_serial_single_write(mlsl_handle, pdata->address,
149 ACCEL_MMA8450_XYZ_DATA_CFG, reg3);
150 if (result) {
151 LOG_RESULT_LOCATION(result);
152 return result;
153 }
154 result = inv_serial_single_write(mlsl_handle, pdata->address,
155 ACCEL_MMA8450_CTRL_REG4, reg1);
156 if (result) {
157 LOG_RESULT_LOCATION(result);
158 return result;
159 }
160 result = inv_serial_single_write(mlsl_handle, pdata->address,
161 ACCEL_MMA8450_CTRL_REG5, reg2);
162 if (result) {
163 LOG_RESULT_LOCATION(result);
164 return result;
165 }
166 }
167
168 return result;
169}
170
171/**
172 * @brief Set the output data rate for the particular configuration.
173 *
174 * @param mlsl_handle
175 * the handle to the serial channel the device is connected to.
176 * @param pdata
177 * a pointer to the slave platform data.
178 * @param config
179 * Config to modify with new ODR.
180 * @param apply
181 * whether to apply immediately or save the settings to be applied
182 * at the next resume.
183 * @param odr
184 * Output data rate in units of 1/1000Hz (mHz).
185 *
186 * @return INV_SUCCESS if successful or a non-zero error code.
187 */
188static int mma8450_set_odr(void *mlsl_handle,
189 struct ext_slave_platform_data *pdata,
190 struct mma8450_config *config,
191 int apply,
192 long odr)
193{
194 unsigned char bits;
195 int result = INV_SUCCESS;
196
197 if (odr > 200000) {
198 config->odr = 400000;
199 bits = 0x00;
200 } else if (odr > 100000) {
201 config->odr = 200000;
202 bits = 0x04;
203 } else if (odr > 50000) {
204 config->odr = 100000;
205 bits = 0x08;
206 } else if (odr > 25000) {
207 config->odr = 50000;
208 bits = 0x0C;
209 } else if (odr > 12500) {
210 config->odr = 25000;
211 bits = 0x40; /* Sleep -> Auto wake mode */
212 } else if (odr > 1563) {
213 config->odr = 12500;
214 bits = 0x10;
215 } else if (odr > 0) {
216 config->odr = 1563;
217 bits = 0x14;
218 } else {
219 config->ctrl_reg1 = 0; /* Set FS1.FS2 to Standby */
220 config->odr = 0;
221 bits = 0;
222 }
223
224 config->ctrl_reg1 = bits | (config->ctrl_reg1 & 0x3);
225 if (apply) {
226 result = inv_serial_single_write(mlsl_handle, pdata->address,
227 ACCEL_MMA8450_CTRL_REG1, 0);
228 if (result) {
229 LOG_RESULT_LOCATION(result);
230 return result;
231 }
232 result = inv_serial_single_write(mlsl_handle, pdata->address,
233 ACCEL_MMA8450_CTRL_REG1, config->ctrl_reg1);
234 if (result) {
235 LOG_RESULT_LOCATION(result);
236 return result;
237 }
238 MPL_LOGV("ODR: %d mHz, 0x%02x\n",
239 config->odr, (int)config->ctrl_reg1);
240 }
241 return result;
242}
243
244/**
245 * @brief Set the full scale range of the accels
246 *
247 * @param mlsl_handle
248 * the handle to the serial channel the device is connected to.
249 * @param pdata
250 * a pointer to the slave platform data.
251 * @param config
252 * pointer to configuration.
253 * @param apply
254 * whether to apply immediately or save the settings to be applied
255 * at the next resume.
256 * @param fsr
257 * requested full scale range.
258 *
259 * @return INV_SUCCESS if successful or a non-zero error code.
260 */
261static int mma8450_set_fsr(void *mlsl_handle,
262 struct ext_slave_platform_data *pdata,
263 struct mma8450_config *config,
264 int apply,
265 long fsr)
266{
267 unsigned char bits;
268 int result = INV_SUCCESS;
269
270 if (fsr <= 2000) {
271 bits = 0x01;
272 config->fsr = 2000;
273 } else if (fsr <= 4000) {
274 bits = 0x02;
275 config->fsr = 4000;
276 } else {
277 bits = 0x03;
278 config->fsr = 8000;
279 }
280
281 config->ctrl_reg1 = bits | (config->ctrl_reg1 & 0xFC);
282 if (apply) {
283 result = inv_serial_single_write(mlsl_handle, pdata->address,
284 ACCEL_MMA8450_CTRL_REG1, config->ctrl_reg1);
285 if (result) {
286 LOG_RESULT_LOCATION(result);
287 return result;
288 }
289 MPL_LOGV("FSR: %d mg\n", config->fsr);
290 }
291 return result;
292}
293
294/**
295 * @brief suspends the device to put it in its lowest power mode.
296 *
297 * @param mlsl_handle
298 * the handle to the serial channel the device is connected to.
299 * @param slave
300 * a pointer to the slave descriptor data structure.
301 * @param pdata
302 * a pointer to the slave platform data.
303 *
304 * @return INV_SUCCESS if successful or a non-zero error code.
305 */
306static int mma8450_suspend(void *mlsl_handle,
307 struct ext_slave_descr *slave,
308 struct ext_slave_platform_data *pdata)
309{
310 int result;
311 struct mma8450_private_data *private_data = pdata->private_data;
312
313 if (private_data->suspend.fsr == 4000)
314 slave->range.mantissa = 4;
315 else if (private_data->suspend.fsr == 8000)
316 slave->range.mantissa = 8;
317 else
318 slave->range.mantissa = 2;
319 slave->range.fraction = 0;
320
321 result = inv_serial_single_write(mlsl_handle, pdata->address,
322 ACCEL_MMA8450_CTRL_REG1, 0);
323 if (result) {
324 LOG_RESULT_LOCATION(result);
325 return result;
326 }
327 if (private_data->suspend.ctrl_reg1) {
328 result = inv_serial_single_write(mlsl_handle, pdata->address,
329 ACCEL_MMA8450_CTRL_REG1,
330 private_data->suspend.ctrl_reg1);
331 if (result) {
332 LOG_RESULT_LOCATION(result);
333 return result;
334 }
335 }
336
337 result = mma8450_set_irq(mlsl_handle, pdata,
338 &private_data->suspend,
339 true, private_data->suspend.irq_type);
340 if (result) {
341 LOG_RESULT_LOCATION(result);
342 return result;
343 }
344 return result;
345}
346
347/**
348 * @brief resume the device in the proper power state given the configuration
349 * chosen.
350 *
351 * @param mlsl_handle
352 * the handle to the serial channel the device is connected to.
353 * @param slave
354 * a pointer to the slave descriptor data structure.
355 * @param pdata
356 * a pointer to the slave platform data.
357 *
358 * @return INV_SUCCESS if successful or a non-zero error code.
359 */
360static int mma8450_resume(void *mlsl_handle,
361 struct ext_slave_descr *slave,
362 struct ext_slave_platform_data *pdata)
363{
364 int result = INV_SUCCESS;
365 struct mma8450_private_data *private_data = pdata->private_data;
366
367 /* Full Scale */
368 if (private_data->resume.fsr == 4000)
369 slave->range.mantissa = 4;
370 else if (private_data->resume.fsr == 8000)
371 slave->range.mantissa = 8;
372 else
373 slave->range.mantissa = 2;
374 slave->range.fraction = 0;
375
376 if (result) {
377 LOG_RESULT_LOCATION(result);
378 return result;
379 }
380 result = inv_serial_single_write(mlsl_handle, pdata->address,
381 ACCEL_MMA8450_CTRL_REG1, 0);
382 if (result) {
383 LOG_RESULT_LOCATION(result);
384 return result;
385 }
386 if (private_data->resume.ctrl_reg1) {
387 result = inv_serial_single_write(mlsl_handle, pdata->address,
388 ACCEL_MMA8450_CTRL_REG1,
389 private_data->resume.ctrl_reg1);
390 if (result) {
391 LOG_RESULT_LOCATION(result);
392 return result;
393 }
394 }
395 result = mma8450_set_irq(mlsl_handle, pdata,
396 &private_data->resume,
397 true, private_data->resume.irq_type);
398 if (result) {
399 LOG_RESULT_LOCATION(result);
400 return result;
401 }
402
403 return result;
404}
405
406/**
407 * @brief read the sensor data from the device.
408 *
409 * @param mlsl_handle
410 * the handle to the serial channel the device is connected to.
411 * @param slave
412 * a pointer to the slave descriptor data structure.
413 * @param pdata
414 * a pointer to the slave platform data.
415 * @param data
416 * a buffer to store the data read.
417 *
418 * @return INV_SUCCESS if successful or a non-zero error code.
419 */
420static int mma8450_read(void *mlsl_handle,
421 struct ext_slave_descr *slave,
422 struct ext_slave_platform_data *pdata, unsigned char *data)
423{
424 int result;
425 unsigned char local_data[4]; /* Status register + 3 bytes data */
426 result = inv_serial_read(mlsl_handle, pdata->address,
427 0x00, sizeof(local_data), local_data);
428 if (result) {
429 LOG_RESULT_LOCATION(result);
430 return result;
431 }
432 memcpy(data, &local_data[1], (slave->read_len) - 1);
433
434 MPL_LOGV("Data Not Ready: %02x %02x %02x %02x\n",
435 local_data[0], local_data[1],
436 local_data[2], local_data[3]);
437
438 return result;
439}
440
441/**
442 * @brief one-time device driver initialization function.
443 * If the driver is built as a kernel module, this function will be
444 * called when the module is loaded in the kernel.
445 * If the driver is built-in in the kernel, this function will be
446 * called at boot time.
447 *
448 * @param mlsl_handle
449 * the handle to the serial channel the device is connected to.
450 * @param slave
451 * a pointer to the slave descriptor data structure.
452 * @param pdata
453 * a pointer to the slave platform data.
454 *
455 * @return INV_SUCCESS if successful or a non-zero error code.
456 */
457static int mma8450_init(void *mlsl_handle,
458 struct ext_slave_descr *slave,
459 struct ext_slave_platform_data *pdata)
460{
461 struct mma8450_private_data *private_data;
462 private_data = (struct mma8450_private_data *)
463 kzalloc(sizeof(struct mma8450_private_data), GFP_KERNEL);
464
465 if (!private_data)
466 return INV_ERROR_MEMORY_EXAUSTED;
467
468 pdata->private_data = private_data;
469
470 mma8450_set_odr(mlsl_handle, pdata, &private_data->suspend,
471 false, 0);
472 mma8450_set_odr(mlsl_handle, pdata, &private_data->resume,
473 false, 200000);
474 mma8450_set_fsr(mlsl_handle, pdata, &private_data->suspend,
475 false, 2000);
476 mma8450_set_fsr(mlsl_handle, pdata, &private_data->resume,
477 false, 2000);
478 mma8450_set_irq(mlsl_handle, pdata, &private_data->suspend,
479 false,
480 MPU_SLAVE_IRQ_TYPE_NONE);
481 mma8450_set_irq(mlsl_handle, pdata, &private_data->resume,
482 false,
483 MPU_SLAVE_IRQ_TYPE_NONE);
484 return INV_SUCCESS;
485}
486
487/**
488 * @brief one-time device driver exit function.
489 * If the driver is built as a kernel module, this function will be
490 * called when the module is removed from the kernel.
491 *
492 * @param mlsl_handle
493 * the handle to the serial channel the device is connected to.
494 * @param slave
495 * a pointer to the slave descriptor data structure.
496 * @param pdata
497 * a pointer to the slave platform data.
498 *
499 * @return INV_SUCCESS if successful or a non-zero error code.
500 */
501static int mma8450_exit(void *mlsl_handle,
502 struct ext_slave_descr *slave,
503 struct ext_slave_platform_data *pdata)
504{
505 kfree(pdata->private_data);
506 return INV_SUCCESS;
507}
508
509/**
510 * @brief device configuration facility.
511 *
512 * @param mlsl_handle
513 * the handle to the serial channel the device is connected to.
514 * @param slave
515 * a pointer to the slave descriptor data structure.
516 * @param pdata
517 * a pointer to the slave platform data.
518 * @param data
519 * a pointer to the configuration data structure.
520 *
521 * @return INV_SUCCESS if successful or a non-zero error code.
522 */
523static int mma8450_config(void *mlsl_handle,
524 struct ext_slave_descr *slave,
525 struct ext_slave_platform_data *pdata,
526 struct ext_slave_config *data)
527{
528 struct mma8450_private_data *private_data = pdata->private_data;
529 if (!data->data)
530 return INV_ERROR_INVALID_PARAMETER;
531
532 switch (data->key) {
533 case MPU_SLAVE_CONFIG_ODR_SUSPEND:
534 return mma8450_set_odr(mlsl_handle, pdata,
535 &private_data->suspend,
536 data->apply,
537 *((long *)data->data));
538 case MPU_SLAVE_CONFIG_ODR_RESUME:
539 return mma8450_set_odr(mlsl_handle, pdata,
540 &private_data->resume,
541 data->apply,
542 *((long *)data->data));
543 case MPU_SLAVE_CONFIG_FSR_SUSPEND:
544 return mma8450_set_fsr(mlsl_handle, pdata,
545 &private_data->suspend,
546 data->apply,
547 *((long *)data->data));
548 case MPU_SLAVE_CONFIG_FSR_RESUME:
549 return mma8450_set_fsr(mlsl_handle, pdata,
550 &private_data->resume,
551 data->apply,
552 *((long *)data->data));
553 case MPU_SLAVE_CONFIG_MOT_THS:
554 return mma8450_set_ths(mlsl_handle, pdata,
555 &private_data->suspend,
556 data->apply,
557 *((long *)data->data));
558 case MPU_SLAVE_CONFIG_NMOT_THS:
559 return mma8450_set_ths(mlsl_handle, pdata,
560 &private_data->resume,
561 data->apply,
562 *((long *)data->data));
563 case MPU_SLAVE_CONFIG_MOT_DUR:
564 return mma8450_set_dur(mlsl_handle, pdata,
565 &private_data->suspend,
566 data->apply,
567 *((long *)data->data));
568 case MPU_SLAVE_CONFIG_NMOT_DUR:
569 return mma8450_set_dur(mlsl_handle, pdata,
570 &private_data->resume,
571 data->apply,
572 *((long *)data->data));
573 case MPU_SLAVE_CONFIG_IRQ_SUSPEND:
574 return mma8450_set_irq(mlsl_handle, pdata,
575 &private_data->suspend,
576 data->apply,
577 *((long *)data->data));
578 case MPU_SLAVE_CONFIG_IRQ_RESUME:
579 return mma8450_set_irq(mlsl_handle, pdata,
580 &private_data->resume,
581 data->apply,
582 *((long *)data->data));
583 default:
584 LOG_RESULT_LOCATION(INV_ERROR_FEATURE_NOT_IMPLEMENTED);
585 return INV_ERROR_FEATURE_NOT_IMPLEMENTED;
586 };
587
588 return INV_SUCCESS;
589}
590
591/**
592 * @brief facility to retrieve the device configuration.
593 *
594 * @param mlsl_handle
595 * the handle to the serial channel the device is connected to.
596 * @param slave
597 * a pointer to the slave descriptor data structure.
598 * @param pdata
599 * a pointer to the slave platform data.
600 * @param data
601 * a pointer to store the returned configuration data structure.
602 *
603 * @return INV_SUCCESS if successful or a non-zero error code.
604 */
605static int mma8450_get_config(void *mlsl_handle,
606 struct ext_slave_descr *slave,
607 struct ext_slave_platform_data *pdata,
608 struct ext_slave_config *data)
609{
610 struct mma8450_private_data *private_data = pdata->private_data;
611 if (!data->data)
612 return INV_ERROR_INVALID_PARAMETER;
613
614 switch (data->key) {
615 case MPU_SLAVE_CONFIG_ODR_SUSPEND:
616 (*(unsigned long *)data->data) =
617 (unsigned long) private_data->suspend.odr;
618 break;
619 case MPU_SLAVE_CONFIG_ODR_RESUME:
620 (*(unsigned long *)data->data) =
621 (unsigned long) private_data->resume.odr;
622 break;
623 case MPU_SLAVE_CONFIG_FSR_SUSPEND:
624 (*(unsigned long *)data->data) =
625 (unsigned long) private_data->suspend.fsr;
626 break;
627 case MPU_SLAVE_CONFIG_FSR_RESUME:
628 (*(unsigned long *)data->data) =
629 (unsigned long) private_data->resume.fsr;
630 break;
631 case MPU_SLAVE_CONFIG_MOT_THS:
632 (*(unsigned long *)data->data) =
633 (unsigned long) private_data->suspend.ths;
634 break;
635 case MPU_SLAVE_CONFIG_NMOT_THS:
636 (*(unsigned long *)data->data) =
637 (unsigned long) private_data->resume.ths;
638 break;
639 case MPU_SLAVE_CONFIG_MOT_DUR:
640 (*(unsigned long *)data->data) =
641 (unsigned long) private_data->suspend.dur;
642 break;
643 case MPU_SLAVE_CONFIG_NMOT_DUR:
644 (*(unsigned long *)data->data) =
645 (unsigned long) private_data->resume.dur;
646 break;
647 case MPU_SLAVE_CONFIG_IRQ_SUSPEND:
648 (*(unsigned long *)data->data) =
649 (unsigned long) private_data->suspend.irq_type;
650 break;
651 case MPU_SLAVE_CONFIG_IRQ_RESUME:
652 (*(unsigned long *)data->data) =
653 (unsigned long) private_data->resume.irq_type;
654 break;
655 default:
656 LOG_RESULT_LOCATION(INV_ERROR_FEATURE_NOT_IMPLEMENTED);
657 return INV_ERROR_FEATURE_NOT_IMPLEMENTED;
658 };
659
660 return INV_SUCCESS;
661}
662
663static struct ext_slave_descr mma8450_descr = {
664 .init = mma8450_init,
665 .exit = mma8450_exit,
666 .suspend = mma8450_suspend,
667 .resume = mma8450_resume,
668 .read = mma8450_read,
669 .config = mma8450_config,
670 .get_config = mma8450_get_config,
671 .name = "mma8450",
672 .type = EXT_SLAVE_TYPE_ACCEL,
673 .id = ACCEL_ID_MMA8450,
674 .read_reg = 0x00,
675 .read_len = 4,
676 .endian = EXT_SLAVE_FS8_BIG_ENDIAN,
677 .range = {2, 0},
678 .trigger = NULL,
679};
680
681static
682struct ext_slave_descr *mma8450_get_slave_descr(void)
683{
684 return &mma8450_descr;
685}
686
687/* -------------------------------------------------------------------------- */
688struct mma8450_mod_private_data {
689 struct i2c_client *client;
690 struct ext_slave_platform_data *pdata;
691};
692
693static unsigned short normal_i2c[] = { I2C_CLIENT_END };
694
695static int mma8450_mod_probe(struct i2c_client *client,
696 const struct i2c_device_id *devid)
697{
698 struct ext_slave_platform_data *pdata;
699 struct mma8450_mod_private_data *private_data;
700 int result = 0;
701
702 dev_info(&client->adapter->dev, "%s: %s\n", __func__, devid->name);
703
704 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
705 result = -ENODEV;
706 goto out_no_free;
707 }
708
709 pdata = client->dev.platform_data;
710 if (!pdata) {
711 dev_err(&client->adapter->dev,
712 "Missing platform data for slave %s\n", devid->name);
713 result = -EFAULT;
714 goto out_no_free;
715 }
716
717 private_data = kzalloc(sizeof(*private_data), GFP_KERNEL);
718 if (!private_data) {
719 result = -ENOMEM;
720 goto out_no_free;
721 }
722
723 i2c_set_clientdata(client, private_data);
724 private_data->client = client;
725 private_data->pdata = pdata;
726
727 result = inv_mpu_register_slave(THIS_MODULE, client, pdata,
728 mma8450_get_slave_descr);
729 if (result) {
730 dev_err(&client->adapter->dev,
731 "Slave registration failed: %s, %d\n",
732 devid->name, result);
733 goto out_free_memory;
734 }
735
736 return result;
737
738out_free_memory:
739 kfree(private_data);
740out_no_free:
741 dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
742 return result;
743
744}
745
746static int mma8450_mod_remove(struct i2c_client *client)
747{
748 struct mma8450_mod_private_data *private_data =
749 i2c_get_clientdata(client);
750
751 dev_dbg(&client->adapter->dev, "%s\n", __func__);
752
753 inv_mpu_unregister_slave(client, private_data->pdata,
754 mma8450_get_slave_descr);
755
756 kfree(private_data);
757 return 0;
758}
759
760static const struct i2c_device_id mma8450_mod_id[] = {
761 { "mma8450", ACCEL_ID_MMA8450 },
762 {}
763};
764
765MODULE_DEVICE_TABLE(i2c, mma8450_mod_id);
766
767static struct i2c_driver mma8450_mod_driver = {
768 .class = I2C_CLASS_HWMON,
769 .probe = mma8450_mod_probe,
770 .remove = mma8450_mod_remove,
771 .id_table = mma8450_mod_id,
772 .driver = {
773 .owner = THIS_MODULE,
774 .name = "mma8450_mod",
775 },
776 .address_list = normal_i2c,
777};
778
779static int __init mma8450_mod_init(void)
780{
781 int res = i2c_add_driver(&mma8450_mod_driver);
782 pr_info("%s: Probe name %s\n", __func__, "mma8450_mod");
783 if (res)
784 pr_err("%s failed\n", __func__);
785 return res;
786}
787
788static void __exit mma8450_mod_exit(void)
789{
790 pr_info("%s\n", __func__);
791 i2c_del_driver(&mma8450_mod_driver);
792}
793
794module_init(mma8450_mod_init);
795module_exit(mma8450_mod_exit);
796
797MODULE_AUTHOR("Invensense Corporation");
798MODULE_DESCRIPTION("Driver to integrate MMA8450 sensor with the MPU");
799MODULE_LICENSE("GPL");
800MODULE_ALIAS("mma8450_mod");
801
802/**
803 * @}
804 */