blob: 2549de1de4e2baccadca7ed80d8fc642d65ca781 [file] [log] [blame]
Daniel Mackbb233fd2009-03-31 15:24:33 -07001/*
2 * lis3lv02d_spi - SPI glue layer for lis3lv02d
3 *
4 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * publishhed by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/err.h>
15#include <linux/input.h>
16#include <linux/interrupt.h>
17#include <linux/workqueue.h>
18#include <linux/spi/spi.h>
19
20#include "lis3lv02d.h"
21
22#define DRV_NAME "lis3lv02d_spi"
23#define LIS3_SPI_READ 0x80
24
25static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v)
26{
27 struct spi_device *spi = lis3->bus_priv;
28 int ret = spi_w8r8(spi, reg | LIS3_SPI_READ);
29 if (ret < 0)
30 return -EINVAL;
31
32 *v = (u8) ret;
33 return 0;
34}
35
36static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val)
37{
38 u8 tmp[2] = { reg, val };
39 struct spi_device *spi = lis3->bus_priv;
40 return spi_write(spi, tmp, sizeof(tmp));
41}
42
43static int lis3_spi_init(struct lis3lv02d *lis3)
44{
45 u8 reg;
46 int ret;
47
48 /* power up the device */
49 ret = lis3->read(lis3, CTRL_REG1, &reg);
50 if (ret < 0)
51 return ret;
52
Samu Onkaloed37d7f2010-10-22 07:57:28 -040053 reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
Daniel Mackbb233fd2009-03-31 15:24:33 -070054 return lis3->write(lis3, CTRL_REG1, reg);
55}
56
Takashi Iwai2ee32142010-10-01 17:14:25 -040057static union axis_conversion lis3lv02d_axis_normal =
58 { .as_array = { 1, 2, 3 } };
Daniel Mackbb233fd2009-03-31 15:24:33 -070059
60static int __devinit lis302dl_spi_probe(struct spi_device *spi)
61{
62 int ret;
63
64 spi->bits_per_word = 8;
65 spi->mode = SPI_MODE_0;
66 ret = spi_setup(spi);
67 if (ret < 0)
68 return ret;
69
Daniel Mackdc791f82009-09-21 17:04:45 -070070 lis3_dev.bus_priv = spi;
71 lis3_dev.init = lis3_spi_init;
72 lis3_dev.read = lis3_spi_read;
73 lis3_dev.write = lis3_spi_write;
74 lis3_dev.irq = spi->irq;
75 lis3_dev.ac = lis3lv02d_axis_normal;
76 lis3_dev.pdata = spi->dev.platform_data;
Daniel Mackbb233fd2009-03-31 15:24:33 -070077 spi_set_drvdata(spi, &lis3_dev);
78
Daniel Mackdc791f82009-09-21 17:04:45 -070079 return lis3lv02d_init_device(&lis3_dev);
Daniel Mackbb233fd2009-03-31 15:24:33 -070080}
81
82static int __devexit lis302dl_spi_remove(struct spi_device *spi)
83{
84 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
85 lis3lv02d_joystick_disable();
86 lis3lv02d_poweroff(lis3);
Samu Onkalod82e23dc2009-10-07 16:32:35 -070087
88 return lis3lv02d_remove_fs(&lis3_dev);
Daniel Mackbb233fd2009-03-31 15:24:33 -070089}
90
Daniel Mack2cd96452009-09-21 17:04:44 -070091#ifdef CONFIG_PM
92static int lis3lv02d_spi_suspend(struct spi_device *spi, pm_message_t mesg)
93{
94 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
95
Kuninori Morimoto5facb092010-09-17 17:24:10 +020096 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Daniel Mack2cd96452009-09-21 17:04:44 -070097 lis3lv02d_poweroff(&lis3_dev);
98
99 return 0;
100}
101
102static int lis3lv02d_spi_resume(struct spi_device *spi)
103{
104 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
105
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200106 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Daniel Mack2cd96452009-09-21 17:04:44 -0700107 lis3lv02d_poweron(lis3);
108
109 return 0;
110}
111
112#else
113#define lis3lv02d_spi_suspend NULL
114#define lis3lv02d_spi_resume NULL
115#endif
116
Daniel Mackbb233fd2009-03-31 15:24:33 -0700117static struct spi_driver lis302dl_spi_driver = {
118 .driver = {
119 .name = DRV_NAME,
120 .owner = THIS_MODULE,
121 },
122 .probe = lis302dl_spi_probe,
123 .remove = __devexit_p(lis302dl_spi_remove),
Daniel Mack2cd96452009-09-21 17:04:44 -0700124 .suspend = lis3lv02d_spi_suspend,
125 .resume = lis3lv02d_spi_resume,
Daniel Mackbb233fd2009-03-31 15:24:33 -0700126};
127
128static int __init lis302dl_init(void)
129{
130 return spi_register_driver(&lis302dl_spi_driver);
131}
132
133static void __exit lis302dl_exit(void)
134{
135 spi_unregister_driver(&lis302dl_spi_driver);
136}
137
138module_init(lis302dl_init);
139module_exit(lis302dl_exit);
140
141MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
142MODULE_DESCRIPTION("lis3lv02d SPI glue layer");
143MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700144MODULE_ALIAS("spi:" DRV_NAME);