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