diff options
Diffstat (limited to 'drivers/misc/inv_mpu/accel/bma150.c')
-rw-r--r-- | drivers/misc/inv_mpu/accel/bma150.c | 777 |
1 files changed, 777 insertions, 0 deletions
diff --git a/drivers/misc/inv_mpu/accel/bma150.c b/drivers/misc/inv_mpu/accel/bma150.c new file mode 100644 index 00000000000..745d90a6744 --- /dev/null +++ b/drivers/misc/inv_mpu/accel/bma150.c | |||
@@ -0,0 +1,777 @@ | |||
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 bma150.c | ||
26 | * @brief Accelerometer setup and handling methods for Bosch BMA150. | ||
27 | */ | ||
28 | |||
29 | /* -------------------------------------------------------------------------- */ | ||
30 | #include <linux/i2c.h> | ||
31 | #include <linux/module.h> | ||
32 | #include <linux/moduleparam.h> | ||
33 | #include <linux/kernel.h> | ||
34 | #include <linux/errno.h> | ||
35 | #include <linux/slab.h> | ||
36 | #include <linux/delay.h> | ||
37 | #include "mpu-dev.h" | ||
38 | |||
39 | #include <linux/mpu.h> | ||
40 | #include "mlsl.h" | ||
41 | #include "mldl_cfg.h" | ||
42 | |||
43 | /* -------------------------------------------------------------------------- */ | ||
44 | /* registers */ | ||
45 | #define BMA150_CTRL_REG (0x14) | ||
46 | #define BMA150_INT_REG (0x15) | ||
47 | #define BMA150_PWR_REG (0x0A) | ||
48 | |||
49 | /* masks */ | ||
50 | #define BMA150_CTRL_MASK (0x18) | ||
51 | #define BMA150_CTRL_MASK_ODR (0xF8) | ||
52 | #define BMA150_CTRL_MASK_FSR (0xE7) | ||
53 | #define BMA150_INT_MASK_WUP (0xF8) | ||
54 | #define BMA150_INT_MASK_IRQ (0xDF) | ||
55 | #define BMA150_PWR_MASK_SLEEP (0x01) | ||
56 | #define BMA150_PWR_MASK_SOFT_RESET (0x02) | ||
57 | |||
58 | /* -------------------------------------------------------------------------- */ | ||
59 | struct bma150_config { | ||
60 | unsigned int odr; /** < output data rate mHz */ | ||
61 | unsigned int fsr; /** < full scale range mgees */ | ||
62 | unsigned int irq_type; /** < type of IRQ, see bma150_set_irq */ | ||
63 | unsigned char ctrl_reg; /** < control register value */ | ||
64 | unsigned char int_reg; /** < interrupt control register value */ | ||
65 | }; | ||
66 | |||
67 | struct bma150_private_data { | ||
68 | struct bma150_config suspend; /** < suspend configuration */ | ||
69 | struct bma150_config resume; /** < resume configuration */ | ||
70 | }; | ||
71 | |||
72 | /** | ||
73 | * @brief Simply disables the IRQ since it is not usable on BMA150 devices. | ||
74 | * | ||
75 | * @param mlsl_handle | ||
76 | * the handle to the serial channel the device is connected to. | ||
77 | * @param pdata | ||
78 | * a pointer to the slave platform data. | ||
79 | * @param config | ||
80 | * configuration to apply to, suspend or resume | ||
81 | * @param apply | ||
82 | * whether to apply immediately or save the settings to be applied | ||
83 | * at the next resume. | ||
84 | * @param irq_type | ||
85 | * the type of IRQ. Valid values are | ||
86 | * - MPU_SLAVE_IRQ_TYPE_NONE | ||
87 | * - MPU_SLAVE_IRQ_TYPE_MOTION | ||
88 | * - MPU_SLAVE_IRQ_TYPE_DATA_READY | ||
89 | * The only supported IRQ type is MPU_SLAVE_IRQ_TYPE_NONE which | ||
90 | * corresponds to disabling the IRQ completely. | ||
91 | * | ||
92 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
93 | */ | ||
94 | static int bma150_set_irq(void *mlsl_handle, | ||
95 | struct ext_slave_platform_data *pdata, | ||
96 | struct bma150_config *config, | ||
97 | int apply, | ||
98 | long irq_type) | ||
99 | { | ||
100 | int result = INV_SUCCESS; | ||
101 | |||
102 | if (irq_type != MPU_SLAVE_IRQ_TYPE_NONE) | ||
103 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
104 | |||
105 | config->irq_type = MPU_SLAVE_IRQ_TYPE_NONE; | ||
106 | config->int_reg = 0x00; | ||
107 | |||
108 | if (apply) { | ||
109 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
110 | BMA150_CTRL_REG, config->ctrl_reg); | ||
111 | if (result) { | ||
112 | LOG_RESULT_LOCATION(result); | ||
113 | return result; | ||
114 | } | ||
115 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
116 | BMA150_INT_REG, config->int_reg); | ||
117 | if (result) { | ||
118 | LOG_RESULT_LOCATION(result); | ||
119 | return result; | ||
120 | } | ||
121 | } | ||
122 | return result; | ||
123 | } | ||
124 | |||
125 | /** | ||
126 | * @brief Set the output data rate for the particular configuration. | ||
127 | * | ||
128 | * @param mlsl_handle | ||
129 | * the handle to the serial channel the device is connected to. | ||
130 | * @param pdata | ||
131 | * a pointer to the slave platform data. | ||
132 | * @param config | ||
133 | * Config to modify with new ODR. | ||
134 | * @param apply | ||
135 | * whether to apply immediately or save the settings to be applied | ||
136 | * at the next resume. | ||
137 | * @param odr | ||
138 | * Output data rate in units of 1/1000Hz (mHz). | ||
139 | * | ||
140 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
141 | */ | ||
142 | static int bma150_set_odr(void *mlsl_handle, | ||
143 | struct ext_slave_platform_data *pdata, | ||
144 | struct bma150_config *config, | ||
145 | int apply, | ||
146 | long odr) | ||
147 | { | ||
148 | unsigned char odr_bits = 0; | ||
149 | unsigned char wup_bits = 0; | ||
150 | int result = INV_SUCCESS; | ||
151 | |||
152 | if (odr > 100000) { | ||
153 | config->odr = 190000; | ||
154 | odr_bits = 0x03; | ||
155 | } else if (odr > 50000) { | ||
156 | config->odr = 100000; | ||
157 | odr_bits = 0x02; | ||
158 | } else if (odr > 25000) { | ||
159 | config->odr = 50000; | ||
160 | odr_bits = 0x01; | ||
161 | } else if (odr > 0) { | ||
162 | config->odr = 25000; | ||
163 | odr_bits = 0x00; | ||
164 | } else { | ||
165 | config->odr = 0; | ||
166 | wup_bits = 0x00; | ||
167 | } | ||
168 | |||
169 | config->int_reg &= BMA150_INT_MASK_WUP; | ||
170 | config->ctrl_reg &= BMA150_CTRL_MASK_ODR; | ||
171 | config->ctrl_reg |= odr_bits; | ||
172 | |||
173 | MPL_LOGV("ODR: %d\n", config->odr); | ||
174 | if (apply) { | ||
175 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
176 | BMA150_CTRL_REG, config->ctrl_reg); | ||
177 | if (result) { | ||
178 | LOG_RESULT_LOCATION(result); | ||
179 | return result; | ||
180 | } | ||
181 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
182 | BMA150_INT_REG, config->int_reg); | ||
183 | if (result) { | ||
184 | LOG_RESULT_LOCATION(result); | ||
185 | return result; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | return result; | ||
190 | } | ||
191 | |||
192 | /** | ||
193 | * @brief Set the full scale range of the accels | ||
194 | * | ||
195 | * @param mlsl_handle | ||
196 | * the handle to the serial channel the device is connected to. | ||
197 | * @param pdata | ||
198 | * a pointer to the slave platform data. | ||
199 | * @param config | ||
200 | * pointer to configuration. | ||
201 | * @param apply | ||
202 | * whether to apply immediately or save the settings to be applied | ||
203 | * at the next resume. | ||
204 | * @param fsr | ||
205 | * requested full scale range. | ||
206 | * | ||
207 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
208 | */ | ||
209 | static int bma150_set_fsr(void *mlsl_handle, | ||
210 | struct ext_slave_platform_data *pdata, | ||
211 | struct bma150_config *config, | ||
212 | int apply, | ||
213 | long fsr) | ||
214 | { | ||
215 | unsigned char fsr_bits; | ||
216 | int result = INV_SUCCESS; | ||
217 | |||
218 | if (fsr <= 2048) { | ||
219 | fsr_bits = 0x00; | ||
220 | config->fsr = 2048; | ||
221 | } else if (fsr <= 4096) { | ||
222 | fsr_bits = 0x08; | ||
223 | config->fsr = 4096; | ||
224 | } else { | ||
225 | fsr_bits = 0x10; | ||
226 | config->fsr = 8192; | ||
227 | } | ||
228 | |||
229 | config->ctrl_reg &= BMA150_CTRL_MASK_FSR; | ||
230 | config->ctrl_reg |= fsr_bits; | ||
231 | |||
232 | MPL_LOGV("FSR: %d\n", config->fsr); | ||
233 | if (apply) { | ||
234 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
235 | BMA150_CTRL_REG, config->ctrl_reg); | ||
236 | if (result) { | ||
237 | LOG_RESULT_LOCATION(result); | ||
238 | return result; | ||
239 | } | ||
240 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
241 | BMA150_CTRL_REG, config->ctrl_reg); | ||
242 | if (result) { | ||
243 | LOG_RESULT_LOCATION(result); | ||
244 | return result; | ||
245 | } | ||
246 | } | ||
247 | return result; | ||
248 | } | ||
249 | |||
250 | /** | ||
251 | * @brief one-time device driver initialization function. | ||
252 | * If the driver is built as a kernel module, this function will be | ||
253 | * called when the module is loaded in the kernel. | ||
254 | * If the driver is built-in in the kernel, this function will be | ||
255 | * called at boot time. | ||
256 | * | ||
257 | * @param mlsl_handle | ||
258 | * the handle to the serial channel the device is connected to. | ||
259 | * @param slave | ||
260 | * a pointer to the slave descriptor data structure. | ||
261 | * @param pdata | ||
262 | * a pointer to the slave platform data. | ||
263 | * | ||
264 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
265 | */ | ||
266 | static int bma150_init(void *mlsl_handle, | ||
267 | struct ext_slave_descr *slave, | ||
268 | struct ext_slave_platform_data *pdata) | ||
269 | { | ||
270 | int result; | ||
271 | unsigned char reg; | ||
272 | long range; | ||
273 | |||
274 | struct bma150_private_data *private_data; | ||
275 | private_data = (struct bma150_private_data *) | ||
276 | kzalloc(sizeof(struct bma150_private_data), GFP_KERNEL); | ||
277 | |||
278 | if (!private_data) | ||
279 | return INV_ERROR_MEMORY_EXAUSTED; | ||
280 | |||
281 | pdata->private_data = private_data; | ||
282 | |||
283 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
284 | BMA150_PWR_REG, BMA150_PWR_MASK_SOFT_RESET); | ||
285 | if (result) { | ||
286 | LOG_RESULT_LOCATION(result); | ||
287 | return result; | ||
288 | } | ||
289 | msleep(1); | ||
290 | |||
291 | result = inv_serial_read(mlsl_handle, pdata->address, | ||
292 | BMA150_CTRL_REG, 1, ®); | ||
293 | if (result) { | ||
294 | LOG_RESULT_LOCATION(result); | ||
295 | return result; | ||
296 | } | ||
297 | private_data->resume.ctrl_reg = reg; | ||
298 | private_data->suspend.ctrl_reg = reg; | ||
299 | |||
300 | result = inv_serial_read(mlsl_handle, pdata->address, | ||
301 | BMA150_INT_REG, 1, ®); | ||
302 | if (result) { | ||
303 | LOG_RESULT_LOCATION(result); | ||
304 | return result; | ||
305 | } | ||
306 | private_data->resume.int_reg = reg; | ||
307 | private_data->suspend.int_reg = reg; | ||
308 | |||
309 | result = bma150_set_odr(mlsl_handle, pdata, &private_data->suspend, | ||
310 | false, 0); | ||
311 | if (result) { | ||
312 | LOG_RESULT_LOCATION(result); | ||
313 | return result; | ||
314 | } | ||
315 | result = bma150_set_odr(mlsl_handle, pdata, &private_data->resume, | ||
316 | false, 200000); | ||
317 | if (result) { | ||
318 | LOG_RESULT_LOCATION(result); | ||
319 | return result; | ||
320 | } | ||
321 | |||
322 | range = range_fixedpoint_to_long_mg(slave->range); | ||
323 | result = bma150_set_fsr(mlsl_handle, pdata, &private_data->suspend, | ||
324 | false, range); | ||
325 | result = bma150_set_fsr(mlsl_handle, pdata, &private_data->resume, | ||
326 | false, range); | ||
327 | if (result) { | ||
328 | LOG_RESULT_LOCATION(result); | ||
329 | return result; | ||
330 | } | ||
331 | |||
332 | result = bma150_set_irq(mlsl_handle, pdata, &private_data->suspend, | ||
333 | false, MPU_SLAVE_IRQ_TYPE_NONE); | ||
334 | if (result) { | ||
335 | LOG_RESULT_LOCATION(result); | ||
336 | return result; | ||
337 | } | ||
338 | result = bma150_set_irq(mlsl_handle, pdata, &private_data->resume, | ||
339 | false, MPU_SLAVE_IRQ_TYPE_NONE); | ||
340 | if (result) { | ||
341 | LOG_RESULT_LOCATION(result); | ||
342 | return result; | ||
343 | } | ||
344 | |||
345 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
346 | BMA150_PWR_REG, BMA150_PWR_MASK_SLEEP); | ||
347 | if (result) { | ||
348 | LOG_RESULT_LOCATION(result); | ||
349 | return result; | ||
350 | } | ||
351 | |||
352 | return result; | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * @brief one-time device driver exit function. | ||
357 | * If the driver is built as a kernel module, this function will be | ||
358 | * called when the module is removed from the kernel. | ||
359 | * | ||
360 | * @param mlsl_handle | ||
361 | * the handle to the serial channel the device is connected to. | ||
362 | * @param slave | ||
363 | * a pointer to the slave descriptor data structure. | ||
364 | * @param pdata | ||
365 | * a pointer to the slave platform data. | ||
366 | * | ||
367 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
368 | */ | ||
369 | static int bma150_exit(void *mlsl_handle, | ||
370 | struct ext_slave_descr *slave, | ||
371 | struct ext_slave_platform_data *pdata) | ||
372 | { | ||
373 | kfree(pdata->private_data); | ||
374 | return INV_SUCCESS; | ||
375 | } | ||
376 | |||
377 | /** | ||
378 | * @brief device configuration facility. | ||
379 | * | ||
380 | * @param mlsl_handle | ||
381 | * the handle to the serial channel the device is connected to. | ||
382 | * @param slave | ||
383 | * a pointer to the slave descriptor data structure. | ||
384 | * @param pdata | ||
385 | * a pointer to the slave platform data. | ||
386 | * @param data | ||
387 | * a pointer to the configuration data structure. | ||
388 | * | ||
389 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
390 | */ | ||
391 | static int bma150_config(void *mlsl_handle, | ||
392 | struct ext_slave_descr *slave, | ||
393 | struct ext_slave_platform_data *pdata, | ||
394 | struct ext_slave_config *data) | ||
395 | { | ||
396 | struct bma150_private_data *private_data = | ||
397 | (struct bma150_private_data *)(pdata->private_data); | ||
398 | |||
399 | if (!data->data) | ||
400 | return INV_ERROR_INVALID_PARAMETER; | ||
401 | |||
402 | switch (data->key) { | ||
403 | case MPU_SLAVE_CONFIG_ODR_SUSPEND: | ||
404 | return bma150_set_odr(mlsl_handle, pdata, | ||
405 | &private_data->suspend, | ||
406 | data->apply, | ||
407 | *((long *)data->data)); | ||
408 | case MPU_SLAVE_CONFIG_ODR_RESUME: | ||
409 | return bma150_set_odr(mlsl_handle, pdata, | ||
410 | &private_data->resume, | ||
411 | data->apply, | ||
412 | *((long *)data->data)); | ||
413 | case MPU_SLAVE_CONFIG_FSR_SUSPEND: | ||
414 | return bma150_set_fsr(mlsl_handle, pdata, | ||
415 | &private_data->suspend, | ||
416 | data->apply, | ||
417 | *((long *)data->data)); | ||
418 | case MPU_SLAVE_CONFIG_FSR_RESUME: | ||
419 | return bma150_set_fsr(mlsl_handle, pdata, | ||
420 | &private_data->resume, | ||
421 | data->apply, | ||
422 | *((long *)data->data)); | ||
423 | case MPU_SLAVE_CONFIG_IRQ_SUSPEND: | ||
424 | return bma150_set_irq(mlsl_handle, pdata, | ||
425 | &private_data->suspend, | ||
426 | data->apply, | ||
427 | *((long *)data->data)); | ||
428 | case MPU_SLAVE_CONFIG_IRQ_RESUME: | ||
429 | return bma150_set_irq(mlsl_handle, pdata, | ||
430 | &private_data->resume, | ||
431 | data->apply, | ||
432 | *((long *)data->data)); | ||
433 | default: | ||
434 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
435 | }; | ||
436 | return INV_SUCCESS; | ||
437 | } | ||
438 | |||
439 | /** | ||
440 | * @brief facility to retrieve the device configuration. | ||
441 | * | ||
442 | * @param mlsl_handle | ||
443 | * the handle to the serial channel the device is connected to. | ||
444 | * @param slave | ||
445 | * a pointer to the slave descriptor data structure. | ||
446 | * @param pdata | ||
447 | * a pointer to the slave platform data. | ||
448 | * @param data | ||
449 | * a pointer to store the returned configuration data structure. | ||
450 | * | ||
451 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
452 | */ | ||
453 | static int bma150_get_config(void *mlsl_handle, | ||
454 | struct ext_slave_descr *slave, | ||
455 | struct ext_slave_platform_data *pdata, | ||
456 | struct ext_slave_config *data) | ||
457 | { | ||
458 | struct bma150_private_data *private_data = | ||
459 | (struct bma150_private_data *)(pdata->private_data); | ||
460 | |||
461 | if (!data->data) | ||
462 | return INV_ERROR_INVALID_PARAMETER; | ||
463 | |||
464 | switch (data->key) { | ||
465 | case MPU_SLAVE_CONFIG_ODR_SUSPEND: | ||
466 | (*(unsigned long *)data->data) = | ||
467 | (unsigned long) private_data->suspend.odr; | ||
468 | break; | ||
469 | case MPU_SLAVE_CONFIG_ODR_RESUME: | ||
470 | (*(unsigned long *)data->data) = | ||
471 | (unsigned long) private_data->resume.odr; | ||
472 | break; | ||
473 | case MPU_SLAVE_CONFIG_FSR_SUSPEND: | ||
474 | (*(unsigned long *)data->data) = | ||
475 | (unsigned long) private_data->suspend.fsr; | ||
476 | break; | ||
477 | case MPU_SLAVE_CONFIG_FSR_RESUME: | ||
478 | (*(unsigned long *)data->data) = | ||
479 | (unsigned long) private_data->resume.fsr; | ||
480 | break; | ||
481 | case MPU_SLAVE_CONFIG_IRQ_SUSPEND: | ||
482 | (*(unsigned long *)data->data) = | ||
483 | (unsigned long) private_data->suspend.irq_type; | ||
484 | break; | ||
485 | case MPU_SLAVE_CONFIG_IRQ_RESUME: | ||
486 | (*(unsigned long *)data->data) = | ||
487 | (unsigned long) private_data->resume.irq_type; | ||
488 | break; | ||
489 | default: | ||
490 | return INV_ERROR_FEATURE_NOT_IMPLEMENTED; | ||
491 | }; | ||
492 | |||
493 | return INV_SUCCESS; | ||
494 | } | ||
495 | |||
496 | /** | ||
497 | * @brief suspends the device to put it in its lowest power mode. | ||
498 | * | ||
499 | * @param mlsl_handle | ||
500 | * the handle to the serial channel the device is connected to. | ||
501 | * @param slave | ||
502 | * a pointer to the slave descriptor data structure. | ||
503 | * @param pdata | ||
504 | * a pointer to the slave platform data. | ||
505 | * | ||
506 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
507 | */ | ||
508 | static int bma150_suspend(void *mlsl_handle, | ||
509 | struct ext_slave_descr *slave, | ||
510 | struct ext_slave_platform_data *pdata) | ||
511 | { | ||
512 | int result; | ||
513 | unsigned char ctrl_reg; | ||
514 | unsigned char int_reg; | ||
515 | |||
516 | struct bma150_private_data *private_data = | ||
517 | (struct bma150_private_data *)(pdata->private_data); | ||
518 | |||
519 | ctrl_reg = private_data->suspend.ctrl_reg; | ||
520 | int_reg = private_data->suspend.int_reg; | ||
521 | |||
522 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
523 | BMA150_PWR_REG, BMA150_PWR_MASK_SOFT_RESET); | ||
524 | if (result) { | ||
525 | LOG_RESULT_LOCATION(result); | ||
526 | return result; | ||
527 | } | ||
528 | msleep(1); | ||
529 | |||
530 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
531 | BMA150_CTRL_REG, ctrl_reg); | ||
532 | if (result) { | ||
533 | LOG_RESULT_LOCATION(result); | ||
534 | return result; | ||
535 | } | ||
536 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
537 | BMA150_INT_REG, int_reg); | ||
538 | if (result) { | ||
539 | LOG_RESULT_LOCATION(result); | ||
540 | return result; | ||
541 | } | ||
542 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
543 | BMA150_PWR_REG, BMA150_PWR_MASK_SLEEP); | ||
544 | if (result) { | ||
545 | LOG_RESULT_LOCATION(result); | ||
546 | return result; | ||
547 | } | ||
548 | |||
549 | return result; | ||
550 | } | ||
551 | |||
552 | /** | ||
553 | * @brief resume the device in the proper power state given the configuration | ||
554 | * chosen. | ||
555 | * | ||
556 | * @param mlsl_handle | ||
557 | * the handle to the serial channel the device is connected to. | ||
558 | * @param slave | ||
559 | * a pointer to the slave descriptor data structure. | ||
560 | * @param pdata | ||
561 | * a pointer to the slave platform data. | ||
562 | * | ||
563 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
564 | */ | ||
565 | static int bma150_resume(void *mlsl_handle, | ||
566 | struct ext_slave_descr *slave, | ||
567 | struct ext_slave_platform_data *pdata) | ||
568 | { | ||
569 | int result; | ||
570 | unsigned char ctrl_reg; | ||
571 | unsigned char int_reg; | ||
572 | |||
573 | struct bma150_private_data *private_data = | ||
574 | (struct bma150_private_data *)(pdata->private_data); | ||
575 | |||
576 | ctrl_reg = private_data->resume.ctrl_reg; | ||
577 | int_reg = private_data->resume.int_reg; | ||
578 | |||
579 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
580 | BMA150_PWR_REG, BMA150_PWR_MASK_SOFT_RESET); | ||
581 | if (result) { | ||
582 | LOG_RESULT_LOCATION(result); | ||
583 | return result; | ||
584 | } | ||
585 | msleep(1); | ||
586 | |||
587 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
588 | BMA150_CTRL_REG, ctrl_reg); | ||
589 | if (result) { | ||
590 | LOG_RESULT_LOCATION(result); | ||
591 | return result; | ||
592 | } | ||
593 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
594 | BMA150_INT_REG, int_reg); | ||
595 | if (result) { | ||
596 | LOG_RESULT_LOCATION(result); | ||
597 | return result; | ||
598 | } | ||
599 | |||
600 | result = inv_serial_single_write(mlsl_handle, pdata->address, | ||
601 | BMA150_PWR_REG, 0x00); | ||
602 | if (result) { | ||
603 | LOG_RESULT_LOCATION(result); | ||
604 | return result; | ||
605 | } | ||
606 | |||
607 | return result; | ||
608 | } | ||
609 | |||
610 | /** | ||
611 | * @brief read the sensor data from the device. | ||
612 | * | ||
613 | * @param mlsl_handle | ||
614 | * the handle to the serial channel the device is connected to. | ||
615 | * @param slave | ||
616 | * a pointer to the slave descriptor data structure. | ||
617 | * @param pdata | ||
618 | * a pointer to the slave platform data. | ||
619 | * @param data | ||
620 | * a buffer to store the data read. | ||
621 | * | ||
622 | * @return INV_SUCCESS if successful or a non-zero error code. | ||
623 | */ | ||
624 | static int bma150_read(void *mlsl_handle, | ||
625 | struct ext_slave_descr *slave, | ||
626 | struct ext_slave_platform_data *pdata, | ||
627 | unsigned char *data) | ||
628 | { | ||
629 | return inv_serial_read(mlsl_handle, pdata->address, | ||
630 | slave->read_reg, slave->read_len, data); | ||
631 | } | ||
632 | |||
633 | static struct ext_slave_descr bma150_descr = { | ||
634 | .init = bma150_init, | ||
635 | .exit = bma150_exit, | ||
636 | .suspend = bma150_suspend, | ||
637 | .resume = bma150_resume, | ||
638 | .read = bma150_read, | ||
639 | .config = bma150_config, | ||
640 | .get_config = bma150_get_config, | ||
641 | .name = "bma150", | ||
642 | .type = EXT_SLAVE_TYPE_ACCEL, | ||
643 | .id = ACCEL_ID_BMA150, | ||
644 | .read_reg = 0x02, | ||
645 | .read_len = 6, | ||
646 | .endian = EXT_SLAVE_LITTLE_ENDIAN, | ||
647 | .range = {2, 0}, | ||
648 | .trigger = NULL, | ||
649 | }; | ||
650 | |||
651 | static | ||
652 | struct ext_slave_descr *bma150_get_slave_descr(void) | ||
653 | { | ||
654 | return &bma150_descr; | ||
655 | } | ||
656 | |||
657 | /* -------------------------------------------------------------------------- */ | ||
658 | |||
659 | /* Platform data for the MPU */ | ||
660 | struct bma150_mod_private_data { | ||
661 | struct i2c_client *client; | ||
662 | struct ext_slave_platform_data *pdata; | ||
663 | }; | ||
664 | |||
665 | static unsigned short normal_i2c[] = { I2C_CLIENT_END }; | ||
666 | |||
667 | static int bma150_mod_probe(struct i2c_client *client, | ||
668 | const struct i2c_device_id *devid) | ||
669 | { | ||
670 | struct ext_slave_platform_data *pdata; | ||
671 | struct bma150_mod_private_data *private_data; | ||
672 | int result = 0; | ||
673 | |||
674 | dev_info(&client->adapter->dev, "%s: %s\n", __func__, devid->name); | ||
675 | |||
676 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
677 | result = -ENODEV; | ||
678 | goto out_no_free; | ||
679 | } | ||
680 | |||
681 | pdata = client->dev.platform_data; | ||
682 | if (!pdata) { | ||
683 | dev_err(&client->adapter->dev, | ||
684 | "Missing platform data for slave %s\n", devid->name); | ||
685 | result = -EFAULT; | ||
686 | goto out_no_free; | ||
687 | } | ||
688 | |||
689 | private_data = kzalloc(sizeof(*private_data), GFP_KERNEL); | ||
690 | if (!private_data) { | ||
691 | result = -ENOMEM; | ||
692 | goto out_no_free; | ||
693 | } | ||
694 | |||
695 | i2c_set_clientdata(client, private_data); | ||
696 | private_data->client = client; | ||
697 | private_data->pdata = pdata; | ||
698 | |||
699 | result = inv_mpu_register_slave(THIS_MODULE, client, pdata, | ||
700 | bma150_get_slave_descr); | ||
701 | if (result) { | ||
702 | dev_err(&client->adapter->dev, | ||
703 | "Slave registration failed: %s, %d\n", | ||
704 | devid->name, result); | ||
705 | goto out_free_memory; | ||
706 | } | ||
707 | |||
708 | return result; | ||
709 | |||
710 | out_free_memory: | ||
711 | kfree(private_data); | ||
712 | out_no_free: | ||
713 | dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result); | ||
714 | return result; | ||
715 | |||
716 | } | ||
717 | |||
718 | static int bma150_mod_remove(struct i2c_client *client) | ||
719 | { | ||
720 | struct bma150_mod_private_data *private_data = | ||
721 | i2c_get_clientdata(client); | ||
722 | |||
723 | dev_dbg(&client->adapter->dev, "%s\n", __func__); | ||
724 | |||
725 | inv_mpu_unregister_slave(client, private_data->pdata, | ||
726 | bma150_get_slave_descr); | ||
727 | |||
728 | kfree(private_data); | ||
729 | return 0; | ||
730 | } | ||
731 | |||
732 | static const struct i2c_device_id bma150_mod_id[] = { | ||
733 | { "bma150", ACCEL_ID_BMA150 }, | ||
734 | {} | ||
735 | }; | ||
736 | |||
737 | MODULE_DEVICE_TABLE(i2c, bma150_mod_id); | ||
738 | |||
739 | static struct i2c_driver bma150_mod_driver = { | ||
740 | .class = I2C_CLASS_HWMON, | ||
741 | .probe = bma150_mod_probe, | ||
742 | .remove = bma150_mod_remove, | ||
743 | .id_table = bma150_mod_id, | ||
744 | .driver = { | ||
745 | .owner = THIS_MODULE, | ||
746 | .name = "bma150_mod", | ||
747 | }, | ||
748 | .address_list = normal_i2c, | ||
749 | }; | ||
750 | |||
751 | static int __init bma150_mod_init(void) | ||
752 | { | ||
753 | int res = i2c_add_driver(&bma150_mod_driver); | ||
754 | pr_info("%s: Probe name %s\n", __func__, "bma150_mod"); | ||
755 | if (res) | ||
756 | pr_err("%s failed\n", __func__); | ||
757 | return res; | ||
758 | } | ||
759 | |||
760 | static void __exit bma150_mod_exit(void) | ||
761 | { | ||
762 | pr_info("%s\n", __func__); | ||
763 | i2c_del_driver(&bma150_mod_driver); | ||
764 | } | ||
765 | |||
766 | module_init(bma150_mod_init); | ||
767 | module_exit(bma150_mod_exit); | ||
768 | |||
769 | MODULE_AUTHOR("Invensense Corporation"); | ||
770 | MODULE_DESCRIPTION("Driver to integrate BMA150 sensor with the MPU"); | ||
771 | MODULE_LICENSE("GPL"); | ||
772 | MODULE_ALIAS("bma150_mod"); | ||
773 | |||
774 | /** | ||
775 | * @} | ||
776 | */ | ||
777 | |||