1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/i2c.h>
17#include <linux/err.h>
18#include <linux/delay.h>
19#include <linux/sysfs.h>
20#include <linux/jiffies.h>
21#include <linux/irq.h>
22#include <linux/interrupt.h>
23#include <linux/kfifo.h>
24#include <linux/spinlock.h>
25#include <linux/iio/iio.h>
26#include <linux/i2c-mux.h>
27#include <linux/acpi.h>
28#include "inv_mpu_iio.h"
29
30
31
32
33
34static const int gyro_scale_6050[] = {133090, 266181, 532362, 1064724};
35
36
37
38
39
40static const int accel_scale[] = {598, 1196, 2392, 4785};
41
42static const struct inv_mpu6050_reg_map reg_set_6050 = {
43 .sample_rate_div = INV_MPU6050_REG_SAMPLE_RATE_DIV,
44 .lpf = INV_MPU6050_REG_CONFIG,
45 .user_ctrl = INV_MPU6050_REG_USER_CTRL,
46 .fifo_en = INV_MPU6050_REG_FIFO_EN,
47 .gyro_config = INV_MPU6050_REG_GYRO_CONFIG,
48 .accl_config = INV_MPU6050_REG_ACCEL_CONFIG,
49 .fifo_count_h = INV_MPU6050_REG_FIFO_COUNT_H,
50 .fifo_r_w = INV_MPU6050_REG_FIFO_R_W,
51 .raw_gyro = INV_MPU6050_REG_RAW_GYRO,
52 .raw_accl = INV_MPU6050_REG_RAW_ACCEL,
53 .temperature = INV_MPU6050_REG_TEMPERATURE,
54 .int_enable = INV_MPU6050_REG_INT_ENABLE,
55 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
56 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
57 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
58};
59
60static const struct inv_mpu6050_chip_config chip_config_6050 = {
61 .fsr = INV_MPU6050_FSR_2000DPS,
62 .lpf = INV_MPU6050_FILTER_20HZ,
63 .fifo_rate = INV_MPU6050_INIT_FIFO_RATE,
64 .gyro_fifo_enable = false,
65 .accl_fifo_enable = false,
66 .accl_fs = INV_MPU6050_FS_02G,
67};
68
69static const struct inv_mpu6050_hw hw_info[INV_NUM_PARTS] = {
70 {
71 .num_reg = 117,
72 .name = "MPU6050",
73 .reg = ®_set_6050,
74 .config = &chip_config_6050,
75 },
76};
77
78int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 d)
79{
80 return i2c_smbus_write_i2c_block_data(st->client, reg, 1, &d);
81}
82
83
84
85
86
87
88
89static int inv_mpu6050_write_reg_unlocked(struct inv_mpu6050_state *st,
90 u8 reg, u8 d)
91{
92 int ret;
93 u8 buf[2];
94 struct i2c_msg msg[1] = {
95 {
96 .addr = st->client->addr,
97 .flags = 0,
98 .len = sizeof(buf),
99 .buf = buf,
100 }
101 };
102
103 buf[0] = reg;
104 buf[1] = d;
105 ret = __i2c_transfer(st->client->adapter, msg, 1);
106 if (ret != 1)
107 return ret;
108
109 return 0;
110}
111
112static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv,
113 u32 chan_id)
114{
115 struct iio_dev *indio_dev = mux_priv;
116 struct inv_mpu6050_state *st = iio_priv(indio_dev);
117 int ret = 0;
118
119
120 mutex_lock(&indio_dev->mlock);
121 if (!st->powerup_count) {
122 ret = inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
123 0);
124 if (ret)
125 goto write_error;
126
127 msleep(INV_MPU6050_REG_UP_TIME);
128 }
129 if (!ret) {
130 st->powerup_count++;
131 ret = inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
132 st->client->irq |
133 INV_MPU6050_BIT_BYPASS_EN);
134 }
135write_error:
136 mutex_unlock(&indio_dev->mlock);
137
138 return ret;
139}
140
141static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
142 void *mux_priv, u32 chan_id)
143{
144 struct iio_dev *indio_dev = mux_priv;
145 struct inv_mpu6050_state *st = iio_priv(indio_dev);
146
147 mutex_lock(&indio_dev->mlock);
148
149 inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
150 st->client->irq);
151 st->powerup_count--;
152 if (!st->powerup_count)
153 inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
154 INV_MPU6050_BIT_SLEEP);
155 mutex_unlock(&indio_dev->mlock);
156
157 return 0;
158}
159
160int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
161{
162 u8 d, mgmt_1;
163 int result;
164
165
166
167
168 if (INV_MPU6050_BIT_PWR_GYRO_STBY == mask) {
169 result = i2c_smbus_read_i2c_block_data(st->client,
170 st->reg->pwr_mgmt_1, 1, &mgmt_1);
171 if (result != 1)
172 return result;
173
174 mgmt_1 &= ~INV_MPU6050_BIT_CLK_MASK;
175 }
176
177 if ((INV_MPU6050_BIT_PWR_GYRO_STBY == mask) && (!en)) {
178
179
180 mgmt_1 |= INV_CLK_INTERNAL;
181 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1, mgmt_1);
182 if (result)
183 return result;
184 }
185
186 result = i2c_smbus_read_i2c_block_data(st->client,
187 st->reg->pwr_mgmt_2, 1, &d);
188 if (result != 1)
189 return result;
190 if (en)
191 d &= ~mask;
192 else
193 d |= mask;
194 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_2, d);
195 if (result)
196 return result;
197
198 if (en) {
199
200 msleep(INV_MPU6050_TEMP_UP_TIME);
201 if (INV_MPU6050_BIT_PWR_GYRO_STBY == mask) {
202
203 mgmt_1 |= INV_CLK_PLL;
204 result = inv_mpu6050_write_reg(st,
205 st->reg->pwr_mgmt_1, mgmt_1);
206 if (result)
207 return result;
208 }
209 }
210
211 return 0;
212}
213
214int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
215{
216 int result = 0;
217
218 if (power_on) {
219
220 if (!st->powerup_count)
221 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
222 0);
223 if (!result)
224 st->powerup_count++;
225 } else {
226 st->powerup_count--;
227 if (!st->powerup_count)
228 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
229 INV_MPU6050_BIT_SLEEP);
230 }
231
232 if (result)
233 return result;
234
235 if (power_on)
236 msleep(INV_MPU6050_REG_UP_TIME);
237
238 return 0;
239}
240
241
242
243
244
245
246
247
248
249
250static int inv_mpu6050_init_config(struct iio_dev *indio_dev)
251{
252 int result;
253 u8 d;
254 struct inv_mpu6050_state *st = iio_priv(indio_dev);
255
256 result = inv_mpu6050_set_power_itg(st, true);
257 if (result)
258 return result;
259 d = (INV_MPU6050_FSR_2000DPS << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
260 result = inv_mpu6050_write_reg(st, st->reg->gyro_config, d);
261 if (result)
262 return result;
263
264 d = INV_MPU6050_FILTER_20HZ;
265 result = inv_mpu6050_write_reg(st, st->reg->lpf, d);
266 if (result)
267 return result;
268
269 d = INV_MPU6050_ONE_K_HZ / INV_MPU6050_INIT_FIFO_RATE - 1;
270 result = inv_mpu6050_write_reg(st, st->reg->sample_rate_div, d);
271 if (result)
272 return result;
273
274 d = (INV_MPU6050_FS_02G << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
275 result = inv_mpu6050_write_reg(st, st->reg->accl_config, d);
276 if (result)
277 return result;
278
279 memcpy(&st->chip_config, hw_info[st->chip_type].config,
280 sizeof(struct inv_mpu6050_chip_config));
281 result = inv_mpu6050_set_power_itg(st, false);
282
283 return result;
284}
285
286static int inv_mpu6050_sensor_show(struct inv_mpu6050_state *st, int reg,
287 int axis, int *val)
288{
289 int ind, result;
290 __be16 d;
291
292 ind = (axis - IIO_MOD_X) * 2;
293 result = i2c_smbus_read_i2c_block_data(st->client, reg + ind, 2,
294 (u8 *)&d);
295 if (result != 2)
296 return -EINVAL;
297 *val = (short)be16_to_cpup(&d);
298
299 return IIO_VAL_INT;
300}
301
302static int inv_mpu6050_read_raw(struct iio_dev *indio_dev,
303 struct iio_chan_spec const *chan,
304 int *val,
305 int *val2,
306 long mask) {
307 struct inv_mpu6050_state *st = iio_priv(indio_dev);
308
309 switch (mask) {
310 case IIO_CHAN_INFO_RAW:
311 {
312 int ret, result;
313
314 ret = IIO_VAL_INT;
315 result = 0;
316 mutex_lock(&indio_dev->mlock);
317 if (!st->chip_config.enable) {
318 result = inv_mpu6050_set_power_itg(st, true);
319 if (result)
320 goto error_read_raw;
321 }
322
323 switch (chan->type) {
324 case IIO_ANGL_VEL:
325 if (!st->chip_config.gyro_fifo_enable ||
326 !st->chip_config.enable) {
327 result = inv_mpu6050_switch_engine(st, true,
328 INV_MPU6050_BIT_PWR_GYRO_STBY);
329 if (result)
330 goto error_read_raw;
331 }
332 ret = inv_mpu6050_sensor_show(st, st->reg->raw_gyro,
333 chan->channel2, val);
334 if (!st->chip_config.gyro_fifo_enable ||
335 !st->chip_config.enable) {
336 result = inv_mpu6050_switch_engine(st, false,
337 INV_MPU6050_BIT_PWR_GYRO_STBY);
338 if (result)
339 goto error_read_raw;
340 }
341 break;
342 case IIO_ACCEL:
343 if (!st->chip_config.accl_fifo_enable ||
344 !st->chip_config.enable) {
345 result = inv_mpu6050_switch_engine(st, true,
346 INV_MPU6050_BIT_PWR_ACCL_STBY);
347 if (result)
348 goto error_read_raw;
349 }
350 ret = inv_mpu6050_sensor_show(st, st->reg->raw_accl,
351 chan->channel2, val);
352 if (!st->chip_config.accl_fifo_enable ||
353 !st->chip_config.enable) {
354 result = inv_mpu6050_switch_engine(st, false,
355 INV_MPU6050_BIT_PWR_ACCL_STBY);
356 if (result)
357 goto error_read_raw;
358 }
359 break;
360 case IIO_TEMP:
361
362 msleep(INV_MPU6050_SENSOR_UP_TIME);
363 inv_mpu6050_sensor_show(st, st->reg->temperature,
364 IIO_MOD_X, val);
365 break;
366 default:
367 ret = -EINVAL;
368 break;
369 }
370error_read_raw:
371 if (!st->chip_config.enable)
372 result |= inv_mpu6050_set_power_itg(st, false);
373 mutex_unlock(&indio_dev->mlock);
374 if (result)
375 return result;
376
377 return ret;
378 }
379 case IIO_CHAN_INFO_SCALE:
380 switch (chan->type) {
381 case IIO_ANGL_VEL:
382 *val = 0;
383 *val2 = gyro_scale_6050[st->chip_config.fsr];
384
385 return IIO_VAL_INT_PLUS_NANO;
386 case IIO_ACCEL:
387 *val = 0;
388 *val2 = accel_scale[st->chip_config.accl_fs];
389
390 return IIO_VAL_INT_PLUS_MICRO;
391 case IIO_TEMP:
392 *val = 0;
393 *val2 = INV_MPU6050_TEMP_SCALE;
394
395 return IIO_VAL_INT_PLUS_MICRO;
396 default:
397 return -EINVAL;
398 }
399 case IIO_CHAN_INFO_OFFSET:
400 switch (chan->type) {
401 case IIO_TEMP:
402 *val = INV_MPU6050_TEMP_OFFSET;
403
404 return IIO_VAL_INT;
405 default:
406 return -EINVAL;
407 }
408 default:
409 return -EINVAL;
410 }
411}
412
413static int inv_mpu6050_write_gyro_scale(struct inv_mpu6050_state *st, int val)
414{
415 int result, i;
416 u8 d;
417
418 for (i = 0; i < ARRAY_SIZE(gyro_scale_6050); ++i) {
419 if (gyro_scale_6050[i] == val) {
420 d = (i << INV_MPU6050_GYRO_CONFIG_FSR_SHIFT);
421 result = inv_mpu6050_write_reg(st,
422 st->reg->gyro_config, d);
423 if (result)
424 return result;
425
426 st->chip_config.fsr = i;
427 return 0;
428 }
429 }
430
431 return -EINVAL;
432}
433
434static int inv_write_raw_get_fmt(struct iio_dev *indio_dev,
435 struct iio_chan_spec const *chan, long mask)
436{
437 switch (mask) {
438 case IIO_CHAN_INFO_SCALE:
439 switch (chan->type) {
440 case IIO_ANGL_VEL:
441 return IIO_VAL_INT_PLUS_NANO;
442 default:
443 return IIO_VAL_INT_PLUS_MICRO;
444 }
445 default:
446 return IIO_VAL_INT_PLUS_MICRO;
447 }
448
449 return -EINVAL;
450}
451static int inv_mpu6050_write_accel_scale(struct inv_mpu6050_state *st, int val)
452{
453 int result, i;
454 u8 d;
455
456 for (i = 0; i < ARRAY_SIZE(accel_scale); ++i) {
457 if (accel_scale[i] == val) {
458 d = (i << INV_MPU6050_ACCL_CONFIG_FSR_SHIFT);
459 result = inv_mpu6050_write_reg(st,
460 st->reg->accl_config, d);
461 if (result)
462 return result;
463
464 st->chip_config.accl_fs = i;
465 return 0;
466 }
467 }
468
469 return -EINVAL;
470}
471
472static int inv_mpu6050_write_raw(struct iio_dev *indio_dev,
473 struct iio_chan_spec const *chan,
474 int val,
475 int val2,
476 long mask) {
477 struct inv_mpu6050_state *st = iio_priv(indio_dev);
478 int result;
479
480 mutex_lock(&indio_dev->mlock);
481
482
483 if (st->chip_config.enable) {
484 result = -EBUSY;
485 goto error_write_raw;
486 }
487 result = inv_mpu6050_set_power_itg(st, true);
488 if (result)
489 goto error_write_raw;
490
491 switch (mask) {
492 case IIO_CHAN_INFO_SCALE:
493 switch (chan->type) {
494 case IIO_ANGL_VEL:
495 result = inv_mpu6050_write_gyro_scale(st, val2);
496 break;
497 case IIO_ACCEL:
498 result = inv_mpu6050_write_accel_scale(st, val2);
499 break;
500 default:
501 result = -EINVAL;
502 break;
503 }
504 break;
505 default:
506 result = -EINVAL;
507 break;
508 }
509
510error_write_raw:
511 result |= inv_mpu6050_set_power_itg(st, false);
512 mutex_unlock(&indio_dev->mlock);
513
514 return result;
515}
516
517
518
519
520
521
522
523
524
525
526static int inv_mpu6050_set_lpf(struct inv_mpu6050_state *st, int rate)
527{
528 const int hz[] = {188, 98, 42, 20, 10, 5};
529 const int d[] = {INV_MPU6050_FILTER_188HZ, INV_MPU6050_FILTER_98HZ,
530 INV_MPU6050_FILTER_42HZ, INV_MPU6050_FILTER_20HZ,
531 INV_MPU6050_FILTER_10HZ, INV_MPU6050_FILTER_5HZ};
532 int i, h, result;
533 u8 data;
534
535 h = (rate >> 1);
536 i = 0;
537 while ((h < hz[i]) && (i < ARRAY_SIZE(d) - 1))
538 i++;
539 data = d[i];
540 result = inv_mpu6050_write_reg(st, st->reg->lpf, data);
541 if (result)
542 return result;
543 st->chip_config.lpf = data;
544
545 return 0;
546}
547
548
549
550
551static ssize_t inv_mpu6050_fifo_rate_store(struct device *dev,
552 struct device_attribute *attr, const char *buf, size_t count)
553{
554 s32 fifo_rate;
555 u8 d;
556 int result;
557 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
558 struct inv_mpu6050_state *st = iio_priv(indio_dev);
559
560 if (kstrtoint(buf, 10, &fifo_rate))
561 return -EINVAL;
562 if (fifo_rate < INV_MPU6050_MIN_FIFO_RATE ||
563 fifo_rate > INV_MPU6050_MAX_FIFO_RATE)
564 return -EINVAL;
565 if (fifo_rate == st->chip_config.fifo_rate)
566 return count;
567
568 mutex_lock(&indio_dev->mlock);
569 if (st->chip_config.enable) {
570 result = -EBUSY;
571 goto fifo_rate_fail;
572 }
573 result = inv_mpu6050_set_power_itg(st, true);
574 if (result)
575 goto fifo_rate_fail;
576
577 d = INV_MPU6050_ONE_K_HZ / fifo_rate - 1;
578 result = inv_mpu6050_write_reg(st, st->reg->sample_rate_div, d);
579 if (result)
580 goto fifo_rate_fail;
581 st->chip_config.fifo_rate = fifo_rate;
582
583 result = inv_mpu6050_set_lpf(st, fifo_rate);
584 if (result)
585 goto fifo_rate_fail;
586
587fifo_rate_fail:
588 result |= inv_mpu6050_set_power_itg(st, false);
589 mutex_unlock(&indio_dev->mlock);
590 if (result)
591 return result;
592
593 return count;
594}
595
596
597
598
599static ssize_t inv_fifo_rate_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
601{
602 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
603
604 return sprintf(buf, "%d\n", st->chip_config.fifo_rate);
605}
606
607
608
609
610
611static ssize_t inv_attr_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
613{
614 struct inv_mpu6050_state *st = iio_priv(dev_to_iio_dev(dev));
615 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
616 s8 *m;
617
618 switch (this_attr->address) {
619
620
621 case ATTR_GYRO_MATRIX:
622 case ATTR_ACCL_MATRIX:
623 m = st->plat_data.orientation;
624
625 return sprintf(buf, "%d, %d, %d; %d, %d, %d; %d, %d, %d\n",
626 m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
627 default:
628 return -EINVAL;
629 }
630}
631
632
633
634
635
636
637
638
639
640
641static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev,
642 struct iio_trigger *trig)
643{
644 struct inv_mpu6050_state *st = iio_priv(indio_dev);
645
646 if (st->trig != trig)
647 return -EINVAL;
648
649 return 0;
650}
651
652#define INV_MPU6050_CHAN(_type, _channel2, _index) \
653 { \
654 .type = _type, \
655 .modified = 1, \
656 .channel2 = _channel2, \
657 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
658 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
659 .scan_index = _index, \
660 .scan_type = { \
661 .sign = 's', \
662 .realbits = 16, \
663 .storagebits = 16, \
664 .shift = 0 , \
665 .endianness = IIO_BE, \
666 }, \
667 }
668
669static const struct iio_chan_spec inv_mpu_channels[] = {
670 IIO_CHAN_SOFT_TIMESTAMP(INV_MPU6050_SCAN_TIMESTAMP),
671
672
673
674
675 {
676 .type = IIO_TEMP,
677 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW)
678 | BIT(IIO_CHAN_INFO_OFFSET)
679 | BIT(IIO_CHAN_INFO_SCALE),
680 .scan_index = -1,
681 },
682 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_X, INV_MPU6050_SCAN_GYRO_X),
683 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Y, INV_MPU6050_SCAN_GYRO_Y),
684 INV_MPU6050_CHAN(IIO_ANGL_VEL, IIO_MOD_Z, INV_MPU6050_SCAN_GYRO_Z),
685
686 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_X, INV_MPU6050_SCAN_ACCL_X),
687 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Y, INV_MPU6050_SCAN_ACCL_Y),
688 INV_MPU6050_CHAN(IIO_ACCEL, IIO_MOD_Z, INV_MPU6050_SCAN_ACCL_Z),
689};
690
691
692static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("10 20 50 100 200 500");
693static IIO_CONST_ATTR(in_anglvel_scale_available,
694 "0.000133090 0.000266181 0.000532362 0.001064724");
695static IIO_CONST_ATTR(in_accel_scale_available,
696 "0.000598 0.001196 0.002392 0.004785");
697static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show,
698 inv_mpu6050_fifo_rate_store);
699static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL,
700 ATTR_GYRO_MATRIX);
701static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL,
702 ATTR_ACCL_MATRIX);
703
704static struct attribute *inv_attributes[] = {
705 &iio_dev_attr_in_gyro_matrix.dev_attr.attr,
706 &iio_dev_attr_in_accel_matrix.dev_attr.attr,
707 &iio_dev_attr_sampling_frequency.dev_attr.attr,
708 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
709 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
710 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
711 NULL,
712};
713
714static const struct attribute_group inv_attribute_group = {
715 .attrs = inv_attributes
716};
717
718static const struct iio_info mpu_info = {
719 .driver_module = THIS_MODULE,
720 .read_raw = &inv_mpu6050_read_raw,
721 .write_raw = &inv_mpu6050_write_raw,
722 .write_raw_get_fmt = &inv_write_raw_get_fmt,
723 .attrs = &inv_attribute_group,
724 .validate_trigger = inv_mpu6050_validate_trigger,
725};
726
727
728
729
730static int inv_check_and_setup_chip(struct inv_mpu6050_state *st,
731 const struct i2c_device_id *id)
732{
733 int result;
734
735 st->chip_type = INV_MPU6050;
736 st->hw = &hw_info[st->chip_type];
737 st->reg = hw_info[st->chip_type].reg;
738
739
740 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
741 INV_MPU6050_BIT_H_RESET);
742 if (result)
743 return result;
744 msleep(INV_MPU6050_POWER_UP_TIME);
745
746
747
748
749 result = inv_mpu6050_set_power_itg(st, false);
750 if (result)
751 return result;
752 result = inv_mpu6050_set_power_itg(st, true);
753 if (result)
754 return result;
755
756 result = inv_mpu6050_switch_engine(st, false,
757 INV_MPU6050_BIT_PWR_ACCL_STBY);
758 if (result)
759 return result;
760 result = inv_mpu6050_switch_engine(st, false,
761 INV_MPU6050_BIT_PWR_GYRO_STBY);
762 if (result)
763 return result;
764
765 return 0;
766}
767
768
769
770
771
772
773
774
775static int inv_mpu_probe(struct i2c_client *client,
776 const struct i2c_device_id *id)
777{
778 struct inv_mpu6050_state *st;
779 struct iio_dev *indio_dev;
780 struct inv_mpu6050_platform_data *pdata;
781 int result;
782
783 if (!i2c_check_functionality(client->adapter,
784 I2C_FUNC_SMBUS_I2C_BLOCK))
785 return -ENOSYS;
786
787 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
788 if (!indio_dev)
789 return -ENOMEM;
790
791 st = iio_priv(indio_dev);
792 st->client = client;
793 st->powerup_count = 0;
794 pdata = dev_get_platdata(&client->dev);
795 if (pdata)
796 st->plat_data = *pdata;
797
798 result = inv_check_and_setup_chip(st, id);
799 if (result)
800 return result;
801
802 result = inv_mpu6050_init_config(indio_dev);
803 if (result) {
804 dev_err(&client->dev,
805 "Could not initialize device.\n");
806 return result;
807 }
808
809 i2c_set_clientdata(client, indio_dev);
810 indio_dev->dev.parent = &client->dev;
811
812 if (id)
813 indio_dev->name = (char *)id->name;
814 else
815 indio_dev->name = (char *)dev_name(&client->dev);
816 indio_dev->channels = inv_mpu_channels;
817 indio_dev->num_channels = ARRAY_SIZE(inv_mpu_channels);
818
819 indio_dev->info = &mpu_info;
820 indio_dev->modes = INDIO_BUFFER_TRIGGERED;
821
822 result = iio_triggered_buffer_setup(indio_dev,
823 inv_mpu6050_irq_handler,
824 inv_mpu6050_read_fifo,
825 NULL);
826 if (result) {
827 dev_err(&st->client->dev, "configure buffer fail %d\n",
828 result);
829 return result;
830 }
831 result = inv_mpu6050_probe_trigger(indio_dev);
832 if (result) {
833 dev_err(&st->client->dev, "trigger probe fail %d\n", result);
834 goto out_unreg_ring;
835 }
836
837 INIT_KFIFO(st->timestamps);
838 spin_lock_init(&st->time_stamp_lock);
839 result = iio_device_register(indio_dev);
840 if (result) {
841 dev_err(&st->client->dev, "IIO register fail %d\n", result);
842 goto out_remove_trigger;
843 }
844
845 st->mux_adapter = i2c_add_mux_adapter(client->adapter,
846 &client->dev,
847 indio_dev,
848 0, 0, 0,
849 inv_mpu6050_select_bypass,
850 inv_mpu6050_deselect_bypass);
851 if (!st->mux_adapter) {
852 result = -ENODEV;
853 goto out_unreg_device;
854 }
855
856 result = inv_mpu_acpi_create_mux_client(st);
857 if (result)
858 goto out_del_mux;
859
860 return 0;
861
862out_del_mux:
863 i2c_del_mux_adapter(st->mux_adapter);
864out_unreg_device:
865 iio_device_unregister(indio_dev);
866out_remove_trigger:
867 inv_mpu6050_remove_trigger(st);
868out_unreg_ring:
869 iio_triggered_buffer_cleanup(indio_dev);
870 return result;
871}
872
873static int inv_mpu_remove(struct i2c_client *client)
874{
875 struct iio_dev *indio_dev = i2c_get_clientdata(client);
876 struct inv_mpu6050_state *st = iio_priv(indio_dev);
877
878 inv_mpu_acpi_delete_mux_client(st);
879 i2c_del_mux_adapter(st->mux_adapter);
880 iio_device_unregister(indio_dev);
881 inv_mpu6050_remove_trigger(st);
882 iio_triggered_buffer_cleanup(indio_dev);
883
884 return 0;
885}
886#ifdef CONFIG_PM_SLEEP
887
888static int inv_mpu_resume(struct device *dev)
889{
890 return inv_mpu6050_set_power_itg(
891 iio_priv(i2c_get_clientdata(to_i2c_client(dev))), true);
892}
893
894static int inv_mpu_suspend(struct device *dev)
895{
896 return inv_mpu6050_set_power_itg(
897 iio_priv(i2c_get_clientdata(to_i2c_client(dev))), false);
898}
899static SIMPLE_DEV_PM_OPS(inv_mpu_pmops, inv_mpu_suspend, inv_mpu_resume);
900
901#define INV_MPU6050_PMOPS (&inv_mpu_pmops)
902#else
903#define INV_MPU6050_PMOPS NULL
904#endif
905
906
907
908
909
910static const struct i2c_device_id inv_mpu_id[] = {
911 {"mpu6050", INV_MPU6050},
912 {"mpu6500", INV_MPU6500},
913 {}
914};
915
916MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
917
918static const struct acpi_device_id inv_acpi_match[] = {
919 {"INVN6500", 0},
920 { },
921};
922
923MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
924
925static struct i2c_driver inv_mpu_driver = {
926 .probe = inv_mpu_probe,
927 .remove = inv_mpu_remove,
928 .id_table = inv_mpu_id,
929 .driver = {
930 .name = "inv-mpu6050",
931 .pm = INV_MPU6050_PMOPS,
932 .acpi_match_table = ACPI_PTR(inv_acpi_match),
933 },
934};
935
936module_i2c_driver(inv_mpu_driver);
937
938MODULE_AUTHOR("Invensense Corporation");
939MODULE_DESCRIPTION("Invensense device MPU6050 driver");
940MODULE_LICENSE("GPL");
941