staging, iio, mpu: repack mpu driver's communicate interface
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / imu / inv_mpu / inv_mpu_i2c.c
1 /*
2 * Copyright (C) 2012 Invensense, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 */
14
15 /**
16  *  @addtogroup  DRIVERS
17  *  @brief       Hardware drivers.
18  *
19  *  @{
20  *      @file    inv_mpu_core.c
21  *      @brief   A sysfs device driver for Invensense devices
22  *      @details This driver currently works for the
23  *               MPU3050/MPU6050/MPU9150/MPU6500/MPU9250 devices.
24  */
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/err.h>
32 #include <linux/delay.h>
33 #include <linux/sysfs.h>
34 #include <linux/jiffies.h>
35 #include <linux/irq.h>
36 #include <linux/interrupt.h>
37 #include <linux/kfifo.h>
38 #include <linux/poll.h>
39 #include <linux/miscdevice.h>
40 #include <linux/spinlock.h>
41
42 #include "inv_mpu_iio.h"
43 #ifdef INV_KERNEL_3_10
44 #include <linux/iio/sysfs.h>
45 #else
46 #include "sysfs.h"
47 #endif
48 #include "inv_counters.h"
49
50 /**
51  *  inv_i2c_read_base() - Read one or more bytes from the device registers.
52  *  @st:        Device driver instance.
53  *  @reg:       First device register to be read from.
54  *  @length:    Number of bytes to read.
55  *  @data:      Data read from device.
56  *  NOTE:This is not re-implementation of i2c_smbus_read because i2c
57  *       address could be specified in this case. We could have two different
58  *       i2c address due to secondary i2c interface.
59  */
60 int inv_i2c_read_base(struct inv_mpu_iio_s *st, u16 i2c_addr,
61         u8 reg, u16 length, u8 *data)
62 {
63         struct i2c_msg msgs[2];
64         int res;
65
66         if (!data)
67                 return -EINVAL;
68
69         msgs[0].addr = i2c_addr;
70         msgs[0].flags = 0;      /* write */
71         msgs[0].buf = &reg;
72         msgs[0].len = 1;
73         /* msgs[0].scl_rate = 200*1000; */
74
75         msgs[1].addr = i2c_addr;
76         msgs[1].flags = I2C_M_RD;
77         msgs[1].buf = data;
78         msgs[1].len = length;
79         /* msgs[1].scl_rate = 200*1000; */
80
81         res = i2c_transfer(st->sl_handle, msgs, 2);
82
83         if (res < 2) {
84                 if (res >= 0)
85                         res = -EIO;
86         } else
87                 res = 0;
88
89         INV_I2C_INC_MPUWRITE(3);
90         INV_I2C_INC_MPUREAD(length);
91         {
92                 char *read = 0;
93                 pr_debug("%s RD%02X%02X%02X -> %s%s\n", st->hw->name,
94                          i2c_addr, reg, length,
95                          wr_pr_debug_begin(data, length, read),
96                          wr_pr_debug_end(read));
97         }
98         return res;
99 }
100
101 /**
102  *  inv_i2c_single_write_base() - Write a byte to a device register.
103  *  @st:        Device driver instance.
104  *  @reg:       Device register to be written to.
105  *  @data:      Byte to write to device.
106  *  NOTE:This is not re-implementation of i2c_smbus_write because i2c
107  *       address could be specified in this case. We could have two different
108  *       i2c address due to secondary i2c interface.
109  */
110 int inv_i2c_single_write_base(struct inv_mpu_iio_s *st,
111         u16 i2c_addr, u8 reg, u8 data)
112 {
113         u8 tmp[2];
114         struct i2c_msg msg;
115         int res;
116         tmp[0] = reg;
117         tmp[1] = data;
118
119         msg.addr = i2c_addr;
120         msg.flags = 0;  /* write */
121         msg.buf = tmp;
122         msg.len = 2;
123         /* msg.scl_rate = 200*1000; */
124
125         pr_debug("%s WR%02X%02X%02X\n", st->hw->name, i2c_addr, reg, data);
126         INV_I2C_INC_MPUWRITE(3);
127
128         res = i2c_transfer(st->sl_handle, &msg, 1);
129         if (res < 1) {
130                 if (res == 0)
131                         res = -EIO;
132                 return res;
133         } else
134                 return 0;
135 }
136
137 int inv_plat_single_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
138 {
139         return inv_i2c_single_write_base(st, st->i2c_addr, reg, data);
140 }
141
142 int inv_plat_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
143 {
144         return inv_i2c_read_base(st, st->i2c_addr, reg, len, data);
145 }
146
147 int inv_secondary_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
148 {
149         return inv_i2c_read_base(st, st->plat_data.secondary_i2c_addr, reg, len, data);
150 }
151
152 int inv_secondary_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
153 {
154         return inv_i2c_single_write_base(st, st->plat_data.secondary_i2c_addr, reg, data);
155 }
156
157 int mpu_memory_write(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
158                      u32 len, u8 const *data)
159 {
160         u8 bank[2];
161         u8 addr[2];
162         u8 buf[513];
163
164         struct i2c_msg msg;
165         int res;
166
167         if (!data || !st)
168                 return -EINVAL;
169
170         if (len >= (sizeof(buf) - 1))
171                 return -ENOMEM;
172
173         bank[0] = REG_BANK_SEL;
174         bank[1] = mem_addr >> 8;
175
176         addr[0] = REG_MEM_START_ADDR;
177         addr[1] = mem_addr & 0xFF;
178
179         buf[0] = REG_MEM_RW;
180         memcpy(buf + 1, data, len);
181
182         /* write message */
183         msg.addr = mpu_addr;
184         msg.flags = 0;
185         msg.buf = bank;
186         msg.len = sizeof(bank);
187         /* msg.scl_rate = 200*1000; */
188         INV_I2C_INC_MPUWRITE(3);
189         res = i2c_transfer(st->sl_handle, &msg, 1);
190         if (res < 1) {
191                 if (res >= 0)
192                         res = -EIO;
193                 return res;
194         }
195
196         msg.addr = mpu_addr;
197         msg.flags = 0;
198         msg.buf = addr;
199         msg.len = sizeof(addr);
200         /* msg.scl_rate = 200*1000; */
201         INV_I2C_INC_MPUWRITE(3);
202         res = i2c_transfer(st->sl_handle, &msg, 1);
203         if (res < 1) {
204                 if (res >= 0)
205                         res = -EIO;
206                 return res;
207         }
208
209         msg.addr = mpu_addr;
210         msg.flags = 0;
211         msg.buf = (u8 *)buf;
212         msg.len = len + 1;
213         /* msg.scl_rate = 200*1000; */
214         INV_I2C_INC_MPUWRITE(2+len);
215         res = i2c_transfer(st->sl_handle, &msg, 1);
216         if (res < 1) {
217                 if (res >= 0)
218                         res = -EIO;
219                 return res;
220         }
221
222         {
223                 char *write = 0;
224                 pr_debug("%s WM%02X%02X%02X%s%s - %d\n", st->hw->name,
225                          mpu_addr, bank[1], addr[1],
226                          wr_pr_debug_begin(data, len, write),
227                          wr_pr_debug_end(write),
228                          len);
229         }
230         return 0;
231 }
232
233 int mpu_memory_read(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
234                     u32 len, u8 *data)
235 {
236         u8 bank[2];
237         u8 addr[2];
238         u8 buf;
239
240         struct i2c_msg msg;
241         int res;
242
243         if (!data || !st)
244                 return -EINVAL;
245
246         bank[0] = REG_BANK_SEL;
247         bank[1] = mem_addr >> 8;
248
249         addr[0] = REG_MEM_START_ADDR;
250         addr[1] = mem_addr & 0xFF;
251
252         buf = REG_MEM_RW;
253
254         /* write message */
255         msg.addr = mpu_addr;
256         msg.flags = 0;
257         msg.buf = bank;
258         msg.len = sizeof(bank);
259         /* msg.scl_rate = 200*1000; */
260         INV_I2C_INC_MPUWRITE(3);
261         res = i2c_transfer(st->sl_handle, &msg, 1);
262         if (res < 1) {
263                 if (res >= 0)
264                         res = -EIO;
265                 return res;
266         }
267
268         msg.addr = mpu_addr;
269         msg.flags = 0;
270         msg.buf = addr;
271         msg.len = sizeof(addr);
272         /* msg.scl_rate = 200*1000; */
273         INV_I2C_INC_MPUWRITE(3);
274         res = i2c_transfer(st->sl_handle, &msg, 1);
275         if (res < 1) {
276                 if (res >= 0)
277                         res = -EIO;
278                 return res;
279         }
280
281         msg.addr = mpu_addr;
282         msg.flags = 0;
283         msg.buf = &buf;
284         msg.len = 1;
285         /* msg.scl_rate = 200*1000; */
286         INV_I2C_INC_MPUWRITE(3);
287         res = i2c_transfer(st->sl_handle, &msg, 1);
288         if (res < 1) {
289                 if (res >= 0)
290                         res = -EIO;
291                 return res;
292         }
293
294         msg.addr = mpu_addr;
295         msg.flags = I2C_M_RD;
296         msg.buf = data;
297         msg.len = len;
298         /* msg.scl_rate = 200*1000; */
299         INV_I2C_INC_MPUREAD(len);
300         res = i2c_transfer(st->sl_handle, &msg, 1);
301         if (res < 1) {
302                 if (res >= 0)
303                         res = -EIO;
304                 return res;
305         }
306         {
307                 char *read = 0;
308                 pr_debug("%s RM%02X%02X%02X%02X - %s%s\n", st->hw->name,
309                          mpu_addr, bank[1], addr[1], len,
310                          wr_pr_debug_begin(data, len, read),
311                          wr_pr_debug_end(read));
312         }
313
314         return 0;
315 }
316
317 #include <linux/gpio.h>
318 #include <linux/of_gpio.h>
319 #include <linux/of.h>
320
321 static struct mpu_platform_data mpu_data = {
322         .int_config  = 0x00,
323         .level_shifter = 0,
324         .orientation = {
325                         0,  1,  0,
326                         -1,  0,  0,
327                         0,  0, -1,
328         },
329 /*
330         .key = {
331                 221,  22, 205,   7, 217, 186, 151, 55,
332                 206, 254,  35, 144, 225, 102,  47, 50,
333         },
334 */
335 };
336
337 static int of_inv_parse_platform_data(struct i2c_client *client,
338                                       struct mpu_platform_data *pdata)
339 {
340         struct device_node *np = client->dev.of_node;
341         unsigned long irq_flags;
342         int irq_pin;
343         int gpio_pin;
344
345         gpio_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
346         gpio_request(gpio_pin, "mpu6500");
347         irq_pin = gpio_to_irq(gpio_pin);
348         client->irq = irq_pin;
349         i2c_set_clientdata(client, &mpu_data);
350
351         pr_info("%s: %s, %x, %x\n", __func__, client->name, client->addr, client->irq);
352
353         return 0;
354 }
355
356 /**
357  *  inv_mpu_probe() - probe function.
358  */
359 static int inv_mpu_probe(struct i2c_client *client,
360         const struct i2c_device_id *id)
361 {
362         struct inv_mpu_iio_s *st;
363         struct iio_dev *indio_dev;
364         int result;
365
366         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
367                 result = -ENOSYS;
368                 pr_err("I2c function error\n");
369                 goto out_no_free;
370         }
371 #ifdef INV_KERNEL_3_10
372     indio_dev = iio_device_alloc(sizeof(*st));
373 #else
374         indio_dev = iio_allocate_device(sizeof(*st));
375 #endif
376         if (indio_dev == NULL) {
377                 pr_err("memory allocation failed\n");
378                 result =  -ENOMEM;
379                 goto out_no_free;
380         }
381         st = iio_priv(indio_dev);
382         st->client = client;
383         st->sl_handle = client->adapter;
384         st->i2c_addr = client->addr;
385         if (client->dev.of_node) {
386                 result = of_inv_parse_platform_data(client, &st->plat_data);
387                 if (result)
388                         goto out_free;
389                 st->plat_data = mpu_data;
390                 pr_info("secondary_i2c_addr=%x\n", st->plat_data.secondary_i2c_addr);
391         } else
392                 st->plat_data =
393                         *(struct mpu_platform_data *)dev_get_platdata(&client->dev);
394         /* power is turned on inside check chip type*/
395         result = inv_check_chip_type(st, id->name);
396         if (result)
397                 goto out_free;
398
399         result = st->init_config(indio_dev);
400         if (result) {
401                 dev_err(&client->adapter->dev,
402                         "Could not initialize device.\n");
403                 goto out_free;
404         }
405         result = st->set_power_state(st, false);
406         if (result) {
407                 dev_err(&client->adapter->dev,
408                         "%s could not be turned off.\n", st->hw->name);
409                 goto out_free;
410         }
411
412         /* Make state variables available to all _show and _store functions. */
413         i2c_set_clientdata(client, indio_dev);
414         indio_dev->dev.parent = &client->dev;
415         if (!strcmp(id->name, "mpu6xxx"))
416                 indio_dev->name = st->name;
417         else
418                 indio_dev->name = id->name;
419
420         inv_set_iio_info(st, indio_dev);
421
422         result = inv_mpu_configure_ring(indio_dev);
423         if (result) {
424                 pr_err("configure ring buffer fail\n");
425                 goto out_free;
426         }
427         st->irq = client->irq;
428         result = inv_mpu_probe_trigger(indio_dev);
429         if (result) {
430                 pr_err("trigger probe fail\n");
431                 goto out_unreg_ring;
432         }
433
434         /* Tell the i2c counter, we have an IRQ */
435         INV_I2C_SETIRQ(IRQ_MPU, client->irq);
436
437         result = iio_device_register(indio_dev);
438         if (result) {
439                 pr_err("IIO device register fail\n");
440                 goto out_remove_trigger;
441         }
442
443         if (INV_MPU6050 == st->chip_type ||
444             INV_MPU6500 == st->chip_type) {
445                 result = inv_create_dmp_sysfs(indio_dev);
446                 if (result) {
447                         pr_err("create dmp sysfs failed\n");
448                         goto out_unreg_iio;
449                 }
450         }
451
452         INIT_KFIFO(st->timestamps);
453         spin_lock_init(&st->time_stamp_lock);
454         dev_info(&client->dev, "%s is ready to go!\n",
455                                         indio_dev->name);
456
457         return 0;
458 out_unreg_iio:
459         iio_device_unregister(indio_dev);
460 out_remove_trigger:
461         if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
462                 inv_mpu_remove_trigger(indio_dev);
463 out_unreg_ring:
464         inv_mpu_unconfigure_ring(indio_dev);
465 out_free:
466 #ifdef INV_KERNEL_3_10
467     iio_device_free(indio_dev);
468 #else
469         iio_free_device(indio_dev);
470 #endif
471 out_no_free:
472         dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
473
474         return -EIO;
475 }
476
477 static void inv_mpu_shutdown(struct i2c_client *client)
478 {
479         struct iio_dev *indio_dev = i2c_get_clientdata(client);
480         struct inv_mpu_iio_s *st = iio_priv(indio_dev);
481         struct inv_reg_map_s *reg;
482         int result;
483
484         reg = &st->reg;
485         dev_dbg(&client->adapter->dev, "Shutting down %s...\n", st->hw->name);
486
487         /* reset to make sure previous state are not there */
488         result = inv_plat_single_write(st, reg->pwr_mgmt_1, BIT_H_RESET);
489         if (result)
490                 dev_err(&client->adapter->dev, "Failed to reset %s\n",
491                         st->hw->name);
492         msleep(POWER_UP_TIME);
493         /* turn off power to ensure gyro engine is off */
494         result = st->set_power_state(st, false);
495         if (result)
496                 dev_err(&client->adapter->dev, "Failed to turn off %s\n",
497                         st->hw->name);
498 }
499
500 /**
501  *  inv_mpu_remove() - remove function.
502  */
503 static int inv_mpu_remove(struct i2c_client *client)
504 {
505         struct iio_dev *indio_dev = i2c_get_clientdata(client);
506         struct inv_mpu_iio_s *st = iio_priv(indio_dev);
507         kfifo_free(&st->timestamps);
508         iio_device_unregister(indio_dev);
509         if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
510                 inv_mpu_remove_trigger(indio_dev);
511         inv_mpu_unconfigure_ring(indio_dev);
512 #ifdef INV_KERNEL_3_10
513     iio_device_free(indio_dev);
514 #else
515         iio_free_device(indio_dev);
516 #endif
517
518         dev_info(&client->adapter->dev, "inv-mpu-iio module removed.\n");
519
520         return 0;
521 }
522
523 #ifdef CONFIG_PM
524 static int inv_mpu_resume(struct device *dev)
525 {
526         struct inv_mpu_iio_s *st =
527                         iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
528         pr_debug("%s inv_mpu_resume\n", st->hw->name);
529         return st->set_power_state(st, true);
530 }
531
532 static int inv_mpu_suspend(struct device *dev)
533 {
534         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
535         struct inv_mpu_iio_s *st = iio_priv(indio_dev);
536         int result;
537
538         pr_debug("%s inv_mpu_suspend\n", st->hw->name);
539         mutex_lock(&indio_dev->mlock);
540         result = 0;
541         if ((!st->chip_config.dmp_on) ||
542                 (!st->chip_config.enable) ||
543                 (!st->chip_config.dmp_event_int_on))
544                 result = st->set_power_state(st, false);
545         mutex_unlock(&indio_dev->mlock);
546
547         return result;
548 }
549 static const struct dev_pm_ops inv_mpu_pmops = {
550         SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
551 };
552 #define INV_MPU_PMOPS (&inv_mpu_pmops)
553 #else
554 #define INV_MPU_PMOPS NULL
555 #endif /* CONFIG_PM */
556
557 static const u16 normal_i2c[] = { I2C_CLIENT_END };
558 /* device id table is used to identify what device can be
559  * supported by this driver
560  */
561 static const struct i2c_device_id inv_mpu_id[] = {
562         {"itg3500", INV_ITG3500},
563         {"mpu3050", INV_MPU3050},
564         {"mpu6050", INV_MPU6050},
565         {"mpu9150", INV_MPU9150},
566         {"mpu6500", INV_MPU6500},
567         {"mpu9250", INV_MPU9250},
568         {"mpu6xxx", INV_MPU6XXX},
569         {"mpu6515", INV_MPU6515},
570         {}
571 };
572
573 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
574
575 static const struct of_device_id inv_mpu_of_match[] = {
576         { .compatible = "invensense,itg3500", },
577         { .compatible = "invensense,mpu3050", },
578         { .compatible = "invensense,mpu6050", },
579         { .compatible = "invensense,mpu9150", },
580         { .compatible = "invensense,mpu6500", },
581         { .compatible = "invensense,mpu9250", },
582         { .compatible = "invensense,mpu6xxx", },
583         { .compatible = "invensense,mpu9350", },
584         { .compatible = "invensense,mpu6515", },
585         {}
586 };
587
588 MODULE_DEVICE_TABLE(of, inv_mpu_of_match);
589
590 static struct i2c_driver inv_mpu_driver = {
591         .class = I2C_CLASS_HWMON,
592         .probe          =       inv_mpu_probe,
593         .remove         =       inv_mpu_remove,
594         .shutdown       =       inv_mpu_shutdown,
595         .id_table       =       inv_mpu_id,
596         .driver = {
597                 .owner  =       THIS_MODULE,
598                 .name   =       "inv-mpu-iio",
599                 .pm     =       INV_MPU_PMOPS,
600                 .of_match_table = of_match_ptr(inv_mpu_of_match),
601         },
602         .address_list = normal_i2c,
603 };
604
605 static int __init inv_mpu_init(void)
606 {
607         int result = i2c_add_driver(&inv_mpu_driver);
608     pr_info("%s:%d\n", __func__, __LINE__);
609         if (result) {
610                 pr_err("failed\n");
611                 return result;
612         }
613         return 0;
614 }
615
616 static void __exit inv_mpu_exit(void)
617 {
618         i2c_del_driver(&inv_mpu_driver);
619 }
620
621 module_init(inv_mpu_init);
622 module_exit(inv_mpu_exit);
623
624 MODULE_AUTHOR("Invensense Corporation");
625 MODULE_DESCRIPTION("Invensense device driver");
626 MODULE_LICENSE("GPL");
627 MODULE_ALIAS("inv-mpu-iio");
628
629 /**
630  *  @}
631  */