1 /*
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 #include <linux/pm.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/of_device.h>
23 
24 #include "lis3lv02d.h"
25 
26 #define DRV_NAME 	"lis3lv02d_spi"
27 #define LIS3_SPI_READ	0x80
28 
29 static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v)
30 {
31 	struct spi_device *spi = lis3->bus_priv;
32 	int ret = spi_w8r8(spi, reg | LIS3_SPI_READ);
33 	if (ret < 0)
34 		return -EINVAL;
35 
36 	*v = (u8) ret;
37 	return 0;
38 }
39 
40 static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val)
41 {
42 	u8 tmp[2] = { reg, val };
43 	struct spi_device *spi = lis3->bus_priv;
44 	return spi_write(spi, tmp, sizeof(tmp));
45 }
46 
47 static int lis3_spi_init(struct lis3lv02d *lis3)
48 {
49 	u8 reg;
50 	int ret;
51 
52 	/* power up the device */
53 	ret = lis3->read(lis3, CTRL_REG1, &reg);
54 	if (ret < 0)
55 		return ret;
56 
57 	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
58 	return lis3->write(lis3, CTRL_REG1, reg);
59 }
60 
61 static union axis_conversion lis3lv02d_axis_normal =
62 	{ .as_array = { 1, 2, 3 } };
63 
64 #ifdef CONFIG_OF
65 static struct of_device_id lis302dl_spi_dt_ids[] = {
66 	{ .compatible = "st,lis302dl-spi" },
67 	{}
68 };
69 MODULE_DEVICE_TABLE(of, lis302dl_spi_dt_ids);
70 #endif
71 
72 static int lis302dl_spi_probe(struct spi_device *spi)
73 {
74 	int ret;
75 
76 	spi->bits_per_word = 8;
77 	spi->mode = SPI_MODE_0;
78 	ret = spi_setup(spi);
79 	if (ret < 0)
80 		return ret;
81 
82 	lis3_dev.bus_priv	= spi;
83 	lis3_dev.init		= lis3_spi_init;
84 	lis3_dev.read		= lis3_spi_read;
85 	lis3_dev.write		= lis3_spi_write;
86 	lis3_dev.irq		= spi->irq;
87 	lis3_dev.ac		= lis3lv02d_axis_normal;
88 	lis3_dev.pdata		= spi->dev.platform_data;
89 
90 #ifdef CONFIG_OF
91 	if (of_match_device(lis302dl_spi_dt_ids, &spi->dev)) {
92 		lis3_dev.of_node = spi->dev.of_node;
93 		ret = lis3lv02d_init_dt(&lis3_dev);
94 		if (ret)
95 			return ret;
96 	}
97 #endif
98 	spi_set_drvdata(spi, &lis3_dev);
99 
100 	return lis3lv02d_init_device(&lis3_dev);
101 }
102 
103 static int lis302dl_spi_remove(struct spi_device *spi)
104 {
105 	struct lis3lv02d *lis3 = spi_get_drvdata(spi);
106 	lis3lv02d_joystick_disable(lis3);
107 	lis3lv02d_poweroff(lis3);
108 
109 	return lis3lv02d_remove_fs(&lis3_dev);
110 }
111 
112 #ifdef CONFIG_PM_SLEEP
113 static int lis3lv02d_spi_suspend(struct device *dev)
114 {
115 	struct spi_device *spi = to_spi_device(dev);
116 	struct lis3lv02d *lis3 = spi_get_drvdata(spi);
117 
118 	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
119 		lis3lv02d_poweroff(&lis3_dev);
120 
121 	return 0;
122 }
123 
124 static int lis3lv02d_spi_resume(struct device *dev)
125 {
126 	struct spi_device *spi = to_spi_device(dev);
127 	struct lis3lv02d *lis3 = spi_get_drvdata(spi);
128 
129 	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
130 		lis3lv02d_poweron(lis3);
131 
132 	return 0;
133 }
134 #endif
135 
136 static SIMPLE_DEV_PM_OPS(lis3lv02d_spi_pm, lis3lv02d_spi_suspend,
137 			 lis3lv02d_spi_resume);
138 
139 static struct spi_driver lis302dl_spi_driver = {
140 	.driver	 = {
141 		.name   = DRV_NAME,
142 		.owner  = THIS_MODULE,
143 		.pm	= &lis3lv02d_spi_pm,
144 		.of_match_table = of_match_ptr(lis302dl_spi_dt_ids),
145 	},
146 	.probe	= lis302dl_spi_probe,
147 	.remove	= lis302dl_spi_remove,
148 };
149 
150 module_spi_driver(lis302dl_spi_driver);
151 
152 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
153 MODULE_DESCRIPTION("lis3lv02d SPI glue layer");
154 MODULE_LICENSE("GPL");
155 MODULE_ALIAS("spi:" DRV_NAME);
156