blob: e575475123c838e0ed2fe2a1dc51832ea9c2c873 [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>
Daniel Mackbb233fd2009-03-31 15:24:33 -070013#include <linux/err.h>
14#include <linux/input.h>
15#include <linux/interrupt.h>
16#include <linux/workqueue.h>
17#include <linux/spi/spi.h>
Mark Brown4e9be652011-01-20 21:27:56 +000018#include <linux/pm.h>
Daniel Mack4bcdea82012-09-26 13:58:19 -070019#include <linux/of.h>
20#include <linux/of_platform.h>
AnilKumar Ch79df8d22012-09-26 13:58:24 -070021#include <linux/of_device.h>
Daniel Mackbb233fd2009-03-31 15:24:33 -070022
23#include "lis3lv02d.h"
24
25#define DRV_NAME "lis3lv02d_spi"
26#define LIS3_SPI_READ 0x80
27
28static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v)
29{
30 struct spi_device *spi = lis3->bus_priv;
31 int ret = spi_w8r8(spi, reg | LIS3_SPI_READ);
32 if (ret < 0)
33 return -EINVAL;
34
35 *v = (u8) ret;
36 return 0;
37}
38
39static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val)
40{
41 u8 tmp[2] = { reg, val };
42 struct spi_device *spi = lis3->bus_priv;
43 return spi_write(spi, tmp, sizeof(tmp));
44}
45
46static int lis3_spi_init(struct lis3lv02d *lis3)
47{
48 u8 reg;
49 int ret;
50
51 /* power up the device */
52 ret = lis3->read(lis3, CTRL_REG1, &reg);
53 if (ret < 0)
54 return ret;
55
Samu Onkaloed37d7f2010-10-22 07:57:28 -040056 reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
Daniel Mackbb233fd2009-03-31 15:24:33 -070057 return lis3->write(lis3, CTRL_REG1, reg);
58}
59
Takashi Iwai2ee32142010-10-01 17:14:25 -040060static union axis_conversion lis3lv02d_axis_normal =
61 { .as_array = { 1, 2, 3 } };
Daniel Mackbb233fd2009-03-31 15:24:33 -070062
Daniel Mack4bcdea82012-09-26 13:58:19 -070063#ifdef CONFIG_OF
Fabian Frederick6893d9b2015-03-16 20:20:26 +010064static const struct of_device_id lis302dl_spi_dt_ids[] = {
Daniel Mack4bcdea82012-09-26 13:58:19 -070065 { .compatible = "st,lis302dl-spi" },
66 {}
67};
68MODULE_DEVICE_TABLE(of, lis302dl_spi_dt_ids);
69#endif
70
Bill Pemberton80c8ae22012-11-19 13:23:05 -050071static int lis302dl_spi_probe(struct spi_device *spi)
Daniel Mackbb233fd2009-03-31 15:24:33 -070072{
73 int ret;
74
75 spi->bits_per_word = 8;
76 spi->mode = SPI_MODE_0;
77 ret = spi_setup(spi);
78 if (ret < 0)
79 return ret;
80
Daniel Mackdc791f82009-09-21 17:04:45 -070081 lis3_dev.bus_priv = spi;
82 lis3_dev.init = lis3_spi_init;
83 lis3_dev.read = lis3_spi_read;
84 lis3_dev.write = lis3_spi_write;
85 lis3_dev.irq = spi->irq;
86 lis3_dev.ac = lis3lv02d_axis_normal;
87 lis3_dev.pdata = spi->dev.platform_data;
Daniel Mack4bcdea82012-09-26 13:58:19 -070088
89#ifdef CONFIG_OF
AnilKumar Ch79df8d22012-09-26 13:58:24 -070090 if (of_match_device(lis302dl_spi_dt_ids, &spi->dev)) {
Daniel Mack4bcdea82012-09-26 13:58:19 -070091 lis3_dev.of_node = spi->dev.of_node;
AnilKumar Ch79df8d22012-09-26 13:58:24 -070092 ret = lis3lv02d_init_dt(&lis3_dev);
93 if (ret)
94 return ret;
95 }
Daniel Mack4bcdea82012-09-26 13:58:19 -070096#endif
Daniel Mackbb233fd2009-03-31 15:24:33 -070097 spi_set_drvdata(spi, &lis3_dev);
98
Daniel Mackdc791f82009-09-21 17:04:45 -070099 return lis3lv02d_init_device(&lis3_dev);
Daniel Mackbb233fd2009-03-31 15:24:33 -0700100}
101
Bill Pemberton486a5c22012-11-19 13:26:02 -0500102static int lis302dl_spi_remove(struct spi_device *spi)
Daniel Mackbb233fd2009-03-31 15:24:33 -0700103{
104 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
Éric Piele1e56872011-10-31 17:11:02 -0700105 lis3lv02d_joystick_disable(lis3);
Daniel Mackbb233fd2009-03-31 15:24:33 -0700106 lis3lv02d_poweroff(lis3);
Samu Onkalod82e23dc2009-10-07 16:32:35 -0700107
108 return lis3lv02d_remove_fs(&lis3_dev);
Daniel Mackbb233fd2009-03-31 15:24:33 -0700109}
110
Mark Brown4e9be652011-01-20 21:27:56 +0000111#ifdef CONFIG_PM_SLEEP
112static int lis3lv02d_spi_suspend(struct device *dev)
Daniel Mack2cd96452009-09-21 17:04:44 -0700113{
Mark Brown4e9be652011-01-20 21:27:56 +0000114 struct spi_device *spi = to_spi_device(dev);
Daniel Mack2cd96452009-09-21 17:04:44 -0700115 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
116
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200117 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Daniel Mack2cd96452009-09-21 17:04:44 -0700118 lis3lv02d_poweroff(&lis3_dev);
119
120 return 0;
121}
122
Mark Brown4e9be652011-01-20 21:27:56 +0000123static int lis3lv02d_spi_resume(struct device *dev)
Daniel Mack2cd96452009-09-21 17:04:44 -0700124{
Mark Brown4e9be652011-01-20 21:27:56 +0000125 struct spi_device *spi = to_spi_device(dev);
Daniel Mack2cd96452009-09-21 17:04:44 -0700126 struct lis3lv02d *lis3 = spi_get_drvdata(spi);
127
Kuninori Morimoto5facb092010-09-17 17:24:10 +0200128 if (!lis3->pdata || !lis3->pdata->wakeup_flags)
Daniel Mack2cd96452009-09-21 17:04:44 -0700129 lis3lv02d_poweron(lis3);
130
131 return 0;
132}
Daniel Mack2cd96452009-09-21 17:04:44 -0700133#endif
134
Mark Brown4e9be652011-01-20 21:27:56 +0000135static SIMPLE_DEV_PM_OPS(lis3lv02d_spi_pm, lis3lv02d_spi_suspend,
136 lis3lv02d_spi_resume);
137
Daniel Mackbb233fd2009-03-31 15:24:33 -0700138static struct spi_driver lis302dl_spi_driver = {
139 .driver = {
140 .name = DRV_NAME,
Mark Brown4e9be652011-01-20 21:27:56 +0000141 .pm = &lis3lv02d_spi_pm,
Daniel Mack4bcdea82012-09-26 13:58:19 -0700142 .of_match_table = of_match_ptr(lis302dl_spi_dt_ids),
Daniel Mackbb233fd2009-03-31 15:24:33 -0700143 },
144 .probe = lis302dl_spi_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500145 .remove = lis302dl_spi_remove,
Daniel Mackbb233fd2009-03-31 15:24:33 -0700146};
147
Axel Lina3dc3c92012-01-22 15:38:22 +0800148module_spi_driver(lis302dl_spi_driver);
Daniel Mackbb233fd2009-03-31 15:24:33 -0700149
150MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
151MODULE_DESCRIPTION("lis3lv02d SPI glue layer");
152MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700153MODULE_ALIAS("spi:" DRV_NAME);