]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/iio/imu/inv_mpu/inv530/inv_mpu_i2c.c
iio: imu: Invensense: Update to official 5.3.0.K-52 code base.
[sojka/nv-tegra/linux-3.10.git] / drivers / iio / imu / inv_mpu / inv530 / 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 #define pr_fmt(fmt) "inv_mpu: " fmt
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/i2c.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
21 #include <linux/sysfs.h>
22 #include <linux/jiffies.h>
23 #include <linux/irq.h>
24 #include <linux/interrupt.h>
25 #include <linux/kfifo.h>
26 #include <linux/poll.h>
27 #include <linux/miscdevice.h>
28 #include <linux/spinlock.h>
29
30 #include "inv_mpu_iio.h"
31 #include "sysfs.h"
32 #include "inv_test/inv_counters.h"
33
34 #ifdef CONFIG_DTS_INV_MPU_IIO
35 #include "inv_mpu_dts.h"
36 #endif
37
38 /**
39  *  inv_i2c_read_base() - Read one or more bytes from the device registers.
40  *  @st:        Device driver instance.
41  *  @i2c_addr:  i2c address of device.
42  *  @reg:       First device register to be read from.
43  *  @length:    Number of bytes to read.
44  *  @data:      Data read from device.
45  *  NOTE:This is not re-implementation of i2c_smbus_read because i2c
46  *       address could be specified in this case. We could have two different
47  *       i2c address due to secondary i2c interface.
48  */
49 int inv_i2c_read_base(struct inv_mpu_state *st, u16 i2c_addr,
50         u8 reg, u16 length, u8 *data)
51 {
52         struct i2c_msg msgs[2];
53         int res;
54
55         if (!data)
56                 return -EINVAL;
57
58         msgs[0].addr = i2c_addr;
59         msgs[0].flags = 0;      /* write */
60         msgs[0].buf = &reg;
61         msgs[0].len = 1;
62
63         msgs[1].addr = i2c_addr;
64         msgs[1].flags = I2C_M_RD;
65         msgs[1].buf = data;
66         msgs[1].len = length;
67
68         res = i2c_transfer(st->sl_handle, msgs, 2);
69
70         if (res < 2) {
71                 if (res >= 0)
72                         res = -EIO;
73         } else
74                 res = 0;
75
76         INV_I2C_INC_MPUWRITE(3);
77         INV_I2C_INC_MPUREAD(length);
78
79         return res;
80 }
81
82 /**
83  *  inv_i2c_single_write_base() - Write a byte to a device register.
84  *  @st:        Device driver instance.
85  *  @i2c_addr:  I2C address of the device.
86  *  @reg:       Device register to be written to.
87  *  @data:      Byte to write to device.
88  *  NOTE:This is not re-implementation of i2c_smbus_write because i2c
89  *       address could be specified in this case. We could have two different
90  *       i2c address due to secondary i2c interface.
91  */
92 int inv_i2c_single_write_base(struct inv_mpu_state *st,
93         u16 i2c_addr, u8 reg, u8 data)
94 {
95         u8 tmp[2];
96         struct i2c_msg msg;
97         int res;
98
99         tmp[0] = reg;
100         tmp[1] = data;
101
102         msg.addr = i2c_addr;
103         msg.flags = 0;  /* write */
104         msg.buf = tmp;
105         msg.len = 2;
106
107         INV_I2C_INC_MPUWRITE(3);
108
109         res = i2c_transfer(st->sl_handle, &msg, 1);
110         if (res < 1) {
111                 if (res == 0)
112                         res = -EIO;
113                 return res;
114         } else
115                 return 0;
116 }
117
118 int inv_plat_single_write(struct inv_mpu_state *st, u8 reg, u8 data)
119 {
120         return inv_i2c_single_write_base(st, st->i2c_addr, reg, data);
121 }
122 int inv_plat_read(struct inv_mpu_state *st, u8 reg, int len, u8 *data)
123 {
124         return inv_i2c_read_base(st, st->i2c_addr, reg, len, data);
125 }
126 int mpu_memory_write(struct inv_mpu_state *st, u8 mpu_addr, u16 mem_addr,
127                      u32 len, u8 const *data)
128 {
129         u8 bank[2];
130         u8 addr[2];
131         u8 buf[513];
132
133         struct i2c_msg msgs[3];
134         int res;
135
136         if (!data || !st)
137                 return -EINVAL;
138
139         if (len >= (sizeof(buf) - 1))
140                 return -ENOMEM;
141
142         bank[0] = REG_MEM_BANK_SEL;
143         bank[1] = mem_addr >> 8;
144
145         addr[0] = REG_MEM_START_ADDR;
146         addr[1] = mem_addr & 0xFF;
147
148         buf[0] = REG_MEM_R_W;
149         memcpy(buf + 1, data, len);
150
151         /* write message */
152         msgs[0].addr = mpu_addr;
153         msgs[0].flags = 0;
154         msgs[0].buf = bank;
155         msgs[0].len = sizeof(bank);
156
157         msgs[1].addr = mpu_addr;
158         msgs[1].flags = 0;
159         msgs[1].buf = addr;
160         msgs[1].len = sizeof(addr);
161
162         msgs[2].addr = mpu_addr;
163         msgs[2].flags = 0;
164         msgs[2].buf = (u8 *)buf;
165         msgs[2].len = len + 1;
166
167         INV_I2C_INC_MPUWRITE(3 + 3 + (2 + len));
168 #if CONFIG_DYNAMIC_DEBUG
169         {
170                 char *write = 0;
171                 pr_debug("%s WM%02X%02X%02X%s%s - %d\n", st->hw->name,
172                          mpu_addr, bank[1], addr[1],
173                          wr_pr_debug_begin(data, len, write),
174                          wr_pr_debug_end(write),
175                          len);
176         }
177 #endif
178
179         res = i2c_transfer(st->sl_handle, msgs, 3);
180         if (res != 3) {
181                 if (res >= 0)
182                         res = -EIO;
183                 return res;
184         } else {
185                 return 0;
186         }
187 }
188
189 int mpu_memory_read(struct inv_mpu_state *st, u8 mpu_addr, u16 mem_addr,
190                     u32 len, u8 *data)
191 {
192         u8 bank[2];
193         u8 addr[2];
194         u8 buf;
195
196         struct i2c_msg msgs[4];
197         int res;
198
199         if (!data || !st)
200                 return -EINVAL;
201
202         bank[0] = REG_MEM_BANK_SEL;
203         bank[1] = mem_addr >> 8;
204
205         addr[0] = REG_MEM_START_ADDR;
206         addr[1] = mem_addr & 0xFF;
207
208         buf = REG_MEM_R_W;
209
210         /* write message */
211         msgs[0].addr = mpu_addr;
212         msgs[0].flags = 0;
213         msgs[0].buf = bank;
214         msgs[0].len = sizeof(bank);
215
216         msgs[1].addr = mpu_addr;
217         msgs[1].flags = 0;
218         msgs[1].buf = addr;
219         msgs[1].len = sizeof(addr);
220
221         msgs[2].addr = mpu_addr;
222         msgs[2].flags = 0;
223         msgs[2].buf = &buf;
224         msgs[2].len = 1;
225
226         msgs[3].addr = mpu_addr;
227         msgs[3].flags = I2C_M_RD;
228         msgs[3].buf = data;
229         msgs[3].len = len;
230
231         res = i2c_transfer(st->sl_handle, msgs, 4);
232         if (res != 4) {
233                 if (res >= 0)
234                         res = -EIO;
235         } else
236                 res = 0;
237
238         INV_I2C_INC_MPUWRITE(3 + 3 + 3);
239         INV_I2C_INC_MPUREAD(len);
240 #if CONFIG_DYNAMIC_DEBUG
241         {
242                 char *read = 0;
243                 pr_debug("%s RM%02X%02X%02X%02X - %s%s\n", st->hw->name,
244                          mpu_addr, bank[1], addr[1], len,
245                          wr_pr_debug_begin(data, len, read),
246                          wr_pr_debug_end(read));
247         }
248 #endif
249
250         return res;
251 }
252
253 /*
254  *  inv_mpu_probe() - probe function.
255  */
256 static int inv_mpu_probe(struct i2c_client *client,
257                                                 const struct i2c_device_id *id)
258 {
259         struct inv_mpu_state *st;
260         struct iio_dev *indio_dev;
261         int result;
262
263 #ifdef CONFIG_DTS_INV_MPU_IIO
264         enable_irq_wake(client->irq);
265 #endif
266
267         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
268                 result = -ENOSYS;
269                 pr_err("I2c function error\n");
270                 goto out_no_free;
271         }
272         indio_dev = iio_allocate_device(sizeof(*st));
273         if (indio_dev == NULL) {
274                 pr_err("memory allocation failed\n");
275                 result =  -ENOMEM;
276                 goto out_no_free;
277         }
278         st = iio_priv(indio_dev);
279         st->client = client;
280         st->sl_handle = client->adapter;
281         st->i2c_addr = client->addr;
282 #ifdef CONFIG_DTS_INV_MPU_IIO
283         result = invensense_mpu_parse_dt(&client->dev, &st->plat_data);
284         if (result)
285                 goto out_free;
286
287         /*Power on device.*/
288         if (st->plat_data.power_on) {
289                 result = st->plat_data.power_on(&st->plat_data);
290                 if (result < 0) {
291                         dev_err(&client->dev,
292                                         "power_on failed: %d\n", result);
293                         return result;
294                 }
295         pr_info("%s: power on here.\n", __func__);
296         }
297         pr_info("%s: power on.\n", __func__);
298
299 msleep(100);
300 #else
301         st->plat_data =
302                 *(struct mpu_platform_data *)dev_get_platdata(&client->dev);
303 #endif
304         /* power is turned on inside check chip type*/
305         result = inv_check_chip_type(indio_dev, id->name);
306         if (result)
307                 goto out_free;
308
309         /* Make state variables available to all _show and _store functions. */
310         i2c_set_clientdata(client, indio_dev);
311         indio_dev->dev.parent = &client->dev;
312         st->dev = &client->dev;
313         indio_dev->name = id->name;
314         st->irq = client->irq;
315
316         result = inv_mpu_configure_ring(indio_dev);
317         if (result) {
318                 pr_err("configure ring buffer fail\n");
319                 goto out_free;
320         }
321         result = iio_buffer_register(indio_dev, indio_dev->channels,
322                                         indio_dev->num_channels);
323         if (result) {
324                 pr_err("ring buffer register fail\n");
325                 goto out_unreg_ring;
326         }
327         result = inv_mpu_probe_trigger(indio_dev);
328         if (result) {
329                 pr_err("trigger probe fail\n");
330                 goto out_remove_ring;
331         }
332
333         /* Tell the i2c counter, we have an IRQ */
334         INV_I2C_SETIRQ(IRQ_MPU, client->irq);
335
336         result = iio_device_register(indio_dev);
337         if (result) {
338                 pr_err("IIO device register fail\n");
339                 goto out_remove_trigger;
340         }
341
342         result = inv_create_dmp_sysfs(indio_dev);
343         if (result) {
344                 pr_err("create dmp sysfs failed\n");
345                 goto out_unreg_iio;
346         }
347
348         mutex_init(&st->suspend_resume_lock);
349         inv_init_sensor_struct(st);
350         dev_info(&client->dev, "%s is ready to go!\n", indio_dev->name);
351
352         return 0;
353 out_unreg_iio:
354         iio_device_unregister(indio_dev);
355 out_remove_trigger:
356         if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
357                 inv_mpu_remove_trigger(indio_dev);
358 out_remove_ring:
359         iio_buffer_unregister(indio_dev);
360 out_unreg_ring:
361         inv_mpu_unconfigure_ring(indio_dev);
362 out_free:
363         iio_free_device(indio_dev);
364 out_no_free:
365         dev_err(&client->adapter->dev, "%s failed %d\n", __func__, result);
366
367         return -EIO;
368 }
369
370 static void inv_mpu_shutdown(struct i2c_client *client)
371 {
372         struct iio_dev *indio_dev = i2c_get_clientdata(client);
373         struct inv_mpu_state *st = iio_priv(indio_dev);
374         int result;
375
376         mutex_lock(&indio_dev->mlock);
377         dev_dbg(&client->adapter->dev, "Shutting down %s...\n", st->hw->name);
378
379         /* reset to make sure previous state are not there */
380         result = inv_plat_single_write(st, REG_PWR_MGMT_1, BIT_H_RESET);
381         if (result)
382                 dev_err(&client->adapter->dev, "Failed to reset %s\n",
383                         st->hw->name);
384         msleep(POWER_UP_TIME);
385         /* turn off power to ensure gyro engine is off */
386         result = inv_set_power(st, false);
387         if (result)
388                 dev_err(&client->adapter->dev, "Failed to turn off %s\n",
389                         st->hw->name);
390         mutex_unlock(&indio_dev->mlock);
391 }
392
393 /*
394  *  inv_mpu_remove() - remove function.
395  */
396 static int inv_mpu_remove(struct i2c_client *client)
397 {
398         struct iio_dev *indio_dev = i2c_get_clientdata(client);
399
400         iio_device_unregister(indio_dev);
401         if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
402                 inv_mpu_remove_trigger(indio_dev);
403         iio_buffer_unregister(indio_dev);
404         inv_mpu_unconfigure_ring(indio_dev);
405         iio_free_device(indio_dev);
406
407         dev_info(&client->adapter->dev, "inv-mpu-iio module removed.\n");
408
409         return 0;
410 }
411
412
413 #ifdef CONFIG_PM
414 /*
415  * inv_mpu_resume(): resume method for this driver.
416  *    This method can be modified according to the request of different
417  *    customers. It basically undo everything suspend_noirq is doing
418  *    and recover the chip to what it was before suspend.
419  */
420 static int inv_mpu_resume(struct device *dev)
421 {
422         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
423         struct inv_mpu_state *st = iio_priv(indio_dev);
424
425         /* add code according to different request Start */
426         pr_debug("%s inv_mpu_resume\n", st->hw->name);
427         mutex_lock(&indio_dev->mlock);
428
429         if (st->chip_config.dmp_on) {
430                 if (st->batch.on) {
431                         inv_switch_power_in_lp(st, true);
432                         write_be32_to_mem(st, st->batch.counter, BM_BATCH_THLD);
433                         inv_plat_single_write(st, REG_INT_ENABLE_2,
434                                                         BIT_FIFO_OVERFLOW_EN_0);
435                         inv_switch_power_in_lp(st, false);
436                 }
437         } else {
438                 inv_switch_power_in_lp(st, true);
439         }
440         st->suspend_state = false;
441         mutex_unlock(&indio_dev->mlock);
442         inv_read_fifo(0, (void *)st);
443         /* add code according to different request End */
444
445         return 0;
446 }
447
448 /*
449  * inv_mpu_suspend(): suspend method for this driver.
450  *    This method can be modified according to the request of different
451  *    customers. If customer want some events, such as SMD to wake up the CPU,
452  *    then data interrupt should be disabled in this interrupt to avoid
453  *    unnecessary interrupts. If customer want pedometer running while CPU is
454  *    asleep, then pedometer should be turned on while pedometer interrupt
455  *    should be turned off.
456  */
457 static int inv_mpu_suspend(struct device *dev)
458 {
459         struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
460         struct inv_mpu_state *st = iio_priv(indio_dev);
461
462         /* add code according to different request Start */
463         pr_debug("%s inv_mpu_suspend\n", st->hw->name);
464         mutex_lock(&indio_dev->mlock);
465         st->suspend_state = true;
466
467         if (st->chip_config.dmp_on) {
468                 if (st->batch.on) {
469                         inv_switch_power_in_lp(st, true);
470                         write_be32_to_mem(st, INT_MAX, BM_BATCH_THLD);
471                         inv_plat_single_write(st, REG_INT_ENABLE_2, 0);
472                         inv_switch_power_in_lp(st, false);
473                 }
474         } else {
475                 /* in non DMP case, just turn off the power */
476                 inv_set_power(st, false);
477         }
478         mutex_unlock(&indio_dev->mlock);
479
480         /* add code according to different request End */
481
482         return 0;
483 }
484
485 static const struct dev_pm_ops inv_mpu_pmops = {
486         .suspend       = inv_mpu_suspend,
487         .resume        = inv_mpu_resume,
488 };
489 #define INV_MPU_PMOPS (&inv_mpu_pmops)
490 #else
491 #define INV_MPU_PMOPS NULL
492 #endif /* CONFIG_PM */
493
494 static const u16 normal_i2c[] = { I2C_CLIENT_END };
495 /* device id table is used to identify what device can be
496  * supported by this driver
497  */
498 static const struct i2c_device_id inv_mpu_id[] = {
499         {"mpu7400", ICM20628},
500         {"icm20728", ICM20728},
501         {"icm20628", ICM20628},
502         {"icm20645", ICM20628},
503         {"icm20630", ICM20628},
504         {}
505 };
506
507 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
508
509 static struct i2c_driver inv_mpu_driver = {
510         .class = I2C_CLASS_HWMON,
511         .probe          =       inv_mpu_probe,
512         .remove         =       inv_mpu_remove,
513         .shutdown       =       inv_mpu_shutdown,
514         .id_table       =       inv_mpu_id,
515         .driver = {
516                 .owner  =       THIS_MODULE,
517                 .name   =       "inv-mpu-iio",
518                 .pm     =       INV_MPU_PMOPS,
519         },
520         .address_list = normal_i2c,
521 };
522
523 static int __init inv_mpu_init(void)
524 {
525         int result = i2c_add_driver(&inv_mpu_driver);
526         if (result) {
527                 pr_err("failed\n");
528                 return result;
529         }
530         return 0;
531 }
532
533 static void __exit inv_mpu_exit(void)
534 {
535         i2c_del_driver(&inv_mpu_driver);
536 }
537
538 module_init(inv_mpu_init);
539 module_exit(inv_mpu_exit);
540
541 MODULE_AUTHOR("Invensense Corporation");
542 MODULE_DESCRIPTION("Invensense device driver");
543 MODULE_LICENSE("GPL");
544 MODULE_ALIAS("inv-mpu-iio");