5642d2715020647b11354034456bee182283ce24
[firefly-linux-kernel-4.4.55.git] / drivers / staging / iio / imu / inv_mpu / inv_mpu_spi.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/err.h>
22 #include <linux/delay.h>
23 #include <linux/sysfs.h>
24 #include <linux/jiffies.h>
25 #include <linux/irq.h>
26 #include <linux/interrupt.h>
27 #include <linux/kfifo.h>
28 #include <linux/poll.h>
29 #include <linux/miscdevice.h>
30 #include <linux/spinlock.h>
31
32 #include "inv_mpu_iio.h"
33 #ifdef INV_KERNEL_3_10
34 #include <linux/iio/sysfs.h>
35 #else
36 #include "sysfs.h"
37 #endif
38 #include "inv_counters.h"
39
40 #define INV_SPI_READ 0x80
41 static int inv_spi_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
42 {
43         struct spi_message msg;
44         int res;
45         u8 d[2];
46         struct spi_device *spi = to_spi_device(st->dev);
47         struct spi_transfer xfers[] = {
48                 {
49                         .tx_buf = d,
50                         .bits_per_word = 8,
51                         .len = 1,
52                 },
53                 {
54                         .rx_buf = data,
55                         .bits_per_word = 8,
56                         .len = len,
57                 }
58         };
59
60         if (!data)
61                 return -EINVAL;
62
63         d[0] = (reg | INV_SPI_READ);
64
65         if ((reg == REG_FIFO_R_W) || (reg == FIFO_COUNT_BYTE))
66                 spi->max_speed_hz = 20000000;
67         else
68                 spi->max_speed_hz = 1000000;
69
70         spi_message_init(&msg);
71         spi_message_add_tail(&xfers[0], &msg);
72         spi_message_add_tail(&xfers[1], &msg);
73         res = spi_sync(spi, &msg);
74
75         return res;
76 }
77
78 static int inv_spi_single_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
79 {
80         struct spi_message msg;
81         int res;
82         u8 d[2];
83         struct spi_device *spi = to_spi_device(st->dev);
84         struct spi_transfer xfers = {
85                 .tx_buf = d,
86                 .bits_per_word = 8,
87                 .len = 2,
88         };
89
90         d[0] = reg;
91         d[1] = data;
92         spi->max_speed_hz = 1000000;
93         spi_message_init(&msg);
94         spi_message_add_tail(&xfers, &msg);
95         res = spi_sync(to_spi_device(st->dev), &msg);
96
97         return res;
98 }
99
100 static int inv_spi_secondary_read(struct inv_mpu_iio_s *st, u8 reg, int len, u8 *data)
101 {
102         return 0;
103 }
104
105 static int inv_spi_secondary_write(struct inv_mpu_iio_s *st, u8 reg, u8 data)
106 {
107         return 0;
108 }
109
110 static int mpu_spi_memory_write(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
111                      u32 len, u8 const *data)
112 {
113         struct spi_message msg;
114         u8 buf[513];
115         int res;
116
117         struct spi_transfer xfers = {
118                 .tx_buf = buf,
119                 .bits_per_word = 8,
120                 .len = len + 1,
121         };
122
123         if (!data || !st)
124                 return -EINVAL;
125
126         if (len > (sizeof(buf) - 1))
127                 return -ENOMEM;
128
129         inv_plat_single_write(st, REG_BANK_SEL, mem_addr >> 8);
130         inv_plat_single_write(st, REG_MEM_START_ADDR, mem_addr & 0xFF);
131
132         buf[0] = REG_MEM_RW;
133         memcpy(buf + 1, data, len);
134         spi_message_init(&msg);
135         spi_message_add_tail(&xfers, &msg);
136         res = spi_sync(to_spi_device(st->dev), &msg);
137
138         return res;
139 }
140
141 static int mpu_spi_memory_read(struct inv_mpu_iio_s *st, u8 mpu_addr, u16 mem_addr,
142                     u32 len, u8 *data)
143 {
144         int res;
145
146         if (!data || !st)
147                 return -EINVAL;
148
149         res = inv_plat_single_write(st, REG_BANK_SEL, mem_addr >> 8);
150         res = inv_plat_single_write(st, REG_MEM_START_ADDR, mem_addr & 0xFF);
151         res = inv_plat_read(st, REG_MEM_RW, len, data);
152
153         return res;
154 }
155
156 #include <linux/gpio.h>
157 #include <linux/of_gpio.h>
158 #include <linux/of.h>
159
160 static struct mpu_platform_data mpu_data = {
161         .int_config  = 0x00,
162         .level_shifter = 0,
163         .orientation = {
164                         0,  1,  0,
165                         -1,  0,  0,
166                         0,  0, -1,
167         },
168 /*
169         .sec_slave_type = SECONDARY_SLAVE_TYPE_COMPASS,
170         .sec_slave_id = COMPASS_ID_AK8963,
171         .secondary_i2c_addr = 0x0d,
172         .secondary_orientation = {
173                         -1,  0,  0,
174                         0,  1,  0,
175                         0,  0, -1,
176         },
177         .key = {
178                 221,  22, 205,   7, 217, 186, 151, 55,
179                 206, 254,  35, 144, 225, 102,  47, 50,
180         },
181 */
182 };
183
184 static int of_inv_parse_platform_data(struct spi_device *spi,
185                                       struct mpu_platform_data *pdata)
186 {
187         int ret;
188         int length = 0, size = 0;
189         struct property *prop;
190         u32 orientation[9];
191         int orig_x, orig_y, orig_z;
192         int i;
193         struct device_node *np = spi->dev.of_node;
194         unsigned long irq_flags;
195         int irq_pin;
196         int gpio_pin;
197         int debug;
198
199         gpio_pin = of_get_named_gpio_flags(np, "irq-gpio", 0, (enum of_gpio_flags *)&irq_flags);
200         gpio_request(gpio_pin, "mpu6500");
201         irq_pin = gpio_to_irq(gpio_pin);
202         spi->irq = irq_pin;
203
204         ret = of_property_read_u8(np, "mpu-int_config", &mpu_data.int_config);
205         if (ret != 0) {
206                 dev_err(&spi->dev, "get mpu-int_config error\n");
207                 return -EIO;
208         }
209
210         ret = of_property_read_u8(np, "mpu-level_shifter", &mpu_data.level_shifter);
211         if (ret != 0) {
212                 dev_err(&spi->dev, "get mpu-level_shifter error\n");
213                 return -EIO;
214         }
215
216         prop = of_find_property(np, "mpu-orientation", &length);
217         if (!prop) {
218                 dev_err(&spi->dev, "get mpu-orientation length error\n");
219                 return -EINVAL;
220         }
221         size = length / sizeof(u32);
222         if ((size > 0) && (size < 10)) {
223                 ret = of_property_read_u32_array(np, "mpu-orientation", orientation, size);
224                 if (ret < 0) {
225                         dev_err(&spi->dev, "get mpu-orientation data error\n");
226                         return -EINVAL;
227                 }
228                 for (i = 0; i < 9; i++)
229                         mpu_data.orientation[i] = orientation[i];
230         } else {
231                 dev_info(&spi->dev, "use default orientation\n");
232         }
233
234         ret = of_property_read_u32(np, "orientation-x", &orig_x);
235         if (ret != 0) {
236                 dev_err(&spi->dev, "get orientation-x error\n");
237                 return -EIO;
238         }
239         if (orig_x > 0) {
240                 for (i = 0; i < 3; i++)
241                         if (mpu_data.orientation[i])
242                                 mpu_data.orientation[i] = -1;
243         }
244
245         ret = of_property_read_u32(np, "orientation-y", &orig_y);
246         if (ret != 0) {
247                 dev_err(&spi->dev, "get orientation-y error\n");
248                 return -EIO;
249         }
250         if (orig_y > 0) {
251                 for (i = 3; i < 6; i++)
252                         if (mpu_data.orientation[i])
253                                 mpu_data.orientation[i] = -1;
254         }
255
256         ret = of_property_read_u32(np, "orientation-z", &orig_z);
257         if (ret != 0) {
258                 dev_err(&spi->dev, "get orientation-z error\n");
259                 return -EIO;
260         }
261         if (orig_z > 0) {
262                 for (i = 6; i < 9; i++)
263                         if (mpu_data.orientation[i])
264                                 mpu_data.orientation[i] = -1;
265         }
266
267         ret = of_property_read_u32(np, "mpu-debug", &debug);
268         if (ret != 0) {
269                 dev_err(&spi->dev, "get mpu-debug error\n");
270                 return -EINVAL;
271         }
272         if (debug) {
273                 dev_info(&spi->dev, "int_config=%d,level_shifter=%d,irq=%x\n",
274                                                         mpu_data.int_config,
275                                                         mpu_data.level_shifter,
276                                                         spi->irq);
277                 for (i = 0; i < size; i++)
278                         dev_info(&spi->dev, "%d ", mpu_data.orientation[i]);
279                 dev_info(&spi->dev, "\n");
280         }
281
282         return 0;
283 }
284
285 /**
286  *  inv_mpu_probe() - probe function.
287  */
288 static int inv_mpu_probe(struct spi_device *spi)
289 {
290         struct inv_mpu_iio_s *st;
291         struct iio_dev *indio_dev;
292         int result;
293
294         if (!spi)
295                 return -ENOMEM;
296
297         spi->bits_per_word = 8;
298         result = spi_setup(spi);
299         if (result < 0) {
300                 dev_err(&spi->dev, "ERR: fail to setup spi\n");
301                 goto out_no_free;
302         }
303
304 #ifdef INV_KERNEL_3_10
305     indio_dev = iio_device_alloc(sizeof(*st));
306 #else
307         indio_dev = iio_allocate_device(sizeof(*st));
308 #endif
309         if (indio_dev == NULL) {
310                 pr_err("memory allocation failed\n");
311                 result =  -ENOMEM;
312                 goto out_no_free;
313         }
314         st = iio_priv(indio_dev);
315         if (spi->dev.of_node) {
316                 result = of_inv_parse_platform_data(spi, &st->plat_data);
317                 if (result)
318                         goto out_free;
319                 st->plat_data = mpu_data;
320                 pr_info("secondary_i2c_addr=%x\n", st->plat_data.secondary_i2c_addr);
321         } else
322                 st->plat_data =
323                         *(struct mpu_platform_data *)dev_get_platdata(&spi->dev);
324
325         /* Make state variables available to all _show and _store functions. */
326         spi_set_drvdata(spi, indio_dev);
327         indio_dev->dev.parent = &spi->dev;
328         st->dev = &spi->dev;
329         st->irq = spi->irq;
330         st->i2c_dis = BIT_I2C_IF_DIS;
331         if (!strcmp(spi->modalias, "mpu6xxx"))
332                 indio_dev->name = st->name;
333         else
334                 indio_dev->name = spi->modalias;
335
336         st->plat_read = inv_spi_read;
337         st->plat_single_write = inv_spi_single_write;
338         st->secondary_read = inv_spi_secondary_read;
339         st->secondary_write = inv_spi_secondary_write;
340         st->memory_read = mpu_spi_memory_read;
341         st->memory_write = mpu_spi_memory_write;
342
343         /* power is turned on inside check chip type*/
344         result = inv_check_chip_type(st, indio_dev->name);
345         if (result)
346                 goto out_free;
347
348         result = st->init_config(indio_dev);
349         if (result) {
350                 dev_err(&spi->dev,
351                         "Could not initialize device.\n");
352                 goto out_free;
353         }
354         result = st->set_power_state(st, false);
355         if (result) {
356                 dev_err(&spi->dev,
357                         "%s could not be turned off.\n", st->hw->name);
358                 goto out_free;
359         }
360
361         inv_set_iio_info(st, indio_dev);
362
363         result = inv_mpu_configure_ring(indio_dev);
364         if (result) {
365                 pr_err("configure ring buffer fail\n");
366                 goto out_free;
367         }
368
369         result = inv_mpu_probe_trigger(indio_dev);
370         if (result) {
371                 pr_err("trigger probe fail\n");
372                 goto out_unreg_ring;
373         }
374
375         result = iio_device_register(indio_dev);
376         if (result) {
377                 pr_err("IIO device register fail\n");
378                 goto out_remove_trigger;
379         }
380
381         if (INV_MPU6050 == st->chip_type ||
382             INV_MPU6500 == st->chip_type) {
383                 result = inv_create_dmp_sysfs(indio_dev);
384                 if (result) {
385                         pr_err("create dmp sysfs failed\n");
386                         goto out_unreg_iio;
387                 }
388         }
389
390         INIT_KFIFO(st->timestamps);
391         spin_lock_init(&st->time_stamp_lock);
392         dev_info(&spi->dev, "%s is ready to go!\n",
393                                         indio_dev->name);
394
395         return 0;
396 out_unreg_iio:
397         iio_device_unregister(indio_dev);
398 out_remove_trigger:
399         if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
400                 inv_mpu_remove_trigger(indio_dev);
401 out_unreg_ring:
402         inv_mpu_unconfigure_ring(indio_dev);
403 out_free:
404 #ifdef INV_KERNEL_3_10
405     iio_device_free(indio_dev);
406 #else
407         iio_free_device(indio_dev);
408 #endif
409 out_no_free:
410         dev_err(&spi->dev, "%s failed %d\n", __func__, result);
411
412         return -EIO;
413 }
414
415 static void inv_mpu_shutdown(struct spi_device *spi)
416 {
417         struct iio_dev *indio_dev = spi_get_drvdata(spi);
418         struct inv_mpu_iio_s *st = iio_priv(indio_dev);
419         struct inv_reg_map_s *reg;
420         int result;
421
422         reg = &st->reg;
423         dev_dbg(&spi->dev, "Shutting down %s...\n", st->hw->name);
424
425         /* reset to make sure previous state are not there */
426         result = inv_plat_single_write(st, reg->pwr_mgmt_1, BIT_H_RESET);
427         if (result)
428                 dev_err(&spi->dev, "Failed to reset %s\n",
429                         st->hw->name);
430         msleep(POWER_UP_TIME);
431         /* turn off power to ensure gyro engine is off */
432         result = st->set_power_state(st, false);
433         if (result)
434                 dev_err(&spi->dev, "Failed to turn off %s\n",
435                         st->hw->name);
436 }
437
438 /**
439  *  inv_mpu_remove() - remove function.
440  */
441 static int inv_mpu_remove(struct spi_device *spi)
442 {
443         struct iio_dev *indio_dev = spi_get_drvdata(spi);
444         struct inv_mpu_iio_s *st = iio_priv(indio_dev);
445         kfifo_free(&st->timestamps);
446         iio_device_unregister(indio_dev);
447         if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
448                 inv_mpu_remove_trigger(indio_dev);
449         inv_mpu_unconfigure_ring(indio_dev);
450 #ifdef INV_KERNEL_3_10
451     iio_device_free(indio_dev);
452 #else
453         iio_free_device(indio_dev);
454 #endif
455
456         dev_info(&spi->dev, "inv-mpu-iio module removed.\n");
457
458         return 0;
459 }
460
461 #ifdef CONFIG_PM
462 static int inv_mpu_resume(struct device *dev)
463 {
464         struct inv_mpu_iio_s *st =
465                         iio_priv(spi_get_drvdata(to_spi_device(dev)));
466         pr_debug("%s inv_mpu_resume\n", st->hw->name);
467         return st->set_power_state(st, true);
468 }
469
470 static int inv_mpu_suspend(struct device *dev)
471 {
472         struct iio_dev *indio_dev = spi_get_drvdata(to_spi_device(dev));
473         struct inv_mpu_iio_s *st = iio_priv(indio_dev);
474         int result;
475
476         pr_debug("%s inv_mpu_suspend\n", st->hw->name);
477         mutex_lock(&indio_dev->mlock);
478         result = 0;
479         if ((!st->chip_config.dmp_on) ||
480                 (!st->chip_config.enable) ||
481                 (!st->chip_config.dmp_event_int_on))
482                 result = st->set_power_state(st, false);
483         mutex_unlock(&indio_dev->mlock);
484
485         return result;
486 }
487 static const struct dev_pm_ops inv_mpu_pmops = {
488         SET_SYSTEM_SLEEP_PM_OPS(inv_mpu_suspend, inv_mpu_resume)
489 };
490 #define INV_MPU_PMOPS (&inv_mpu_pmops)
491 #else
492 #define INV_MPU_PMOPS NULL
493 #endif /* CONFIG_PM */
494
495 /* device id table is used to identify what device can be
496  * supported by this driver
497  */
498 static const struct spi_device_id inv_mpu_id[] = {
499         {"itg3500", INV_ITG3500},
500         {"mpu3050", INV_MPU3050},
501         {"mpu6050", INV_MPU6050},
502         {"mpu9150", INV_MPU9150},
503         {"mpu6500", INV_MPU6500},
504         {"mpu9250", INV_MPU9250},
505         {"mpu6xxx", INV_MPU6XXX},
506         {"mpu6515", INV_MPU6515},
507         {}
508 };
509
510 MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
511
512 static const struct of_device_id inv_mpu_of_match[] = {
513         { .compatible = "inv-spi,itg3500", },
514         { .compatible = "inv-spi,mpu3050", },
515         { .compatible = "inv-spi,mpu6050", },
516         { .compatible = "inv-spi,mpu9150", },
517         { .compatible = "inv-spi,mpu6500", },
518         { .compatible = "inv-spi,mpu9250", },
519         { .compatible = "inv-spi,mpu6xxx", },
520         { .compatible = "inv-spi,mpu9350", },
521         { .compatible = "inv-spi,mpu6515", },
522         {}
523 };
524
525 MODULE_DEVICE_TABLE(of, inv_mpu_of_match);
526
527 static struct spi_driver inv_mpu_driver_spi = {
528         .driver = {
529                 .owner = THIS_MODULE,
530                 .name = "inv_mpu_iio_spi",
531                 .pm   =       INV_MPU_PMOPS,
532                 .of_match_table = of_match_ptr(inv_mpu_of_match),
533         },
534         .probe = inv_mpu_probe,
535         .remove = inv_mpu_remove,
536         .id_table = inv_mpu_id,
537         .shutdown = inv_mpu_shutdown,
538 };
539
540 static int __init inv_mpu_init(void)
541 {
542         int result = spi_register_driver(&inv_mpu_driver_spi);
543
544         if (result) {
545                 pr_err("failed\n");
546                 return result;
547         }
548         return 0;
549 }
550
551 static void __exit inv_mpu_exit(void)
552 {
553         spi_unregister_driver(&inv_mpu_driver_spi);
554 }
555
556 module_init(inv_mpu_init);
557 module_exit(inv_mpu_exit);
558
559 MODULE_AUTHOR("Invensense Corporation");
560 MODULE_DESCRIPTION("Invensense device driver");
561 MODULE_LICENSE("GPL");
562 MODULE_ALIAS("inv-mpu-iio-spi");
563
564 /**
565  *  @}
566  */