xref: /linux/drivers/input/misc/adxl34x-spi.c (revision e27c729219ad24c8ac9a4b34cf192e56917565c5)
1 /*
2  * ADLX345/346 Three-Axis Digital Accelerometers (SPI Interface)
3  *
4  * Enter bugs at http://blackfin.uclinux.org/
5  *
6  * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
7  * Licensed under the GPL-2 or later.
8  */
9 
10 #include <linux/input.h>	/* BUS_SPI */
11 #include <linux/module.h>
12 #include <linux/spi/spi.h>
13 #include <linux/types.h>
14 #include "adxl34x.h"
15 
16 #define MAX_SPI_FREQ_HZ		5000000
17 #define MAX_FREQ_NO_FIFODELAY	1500000
18 #define ADXL34X_CMD_MULTB	(1 << 6)
19 #define ADXL34X_CMD_READ	(1 << 7)
20 #define ADXL34X_WRITECMD(reg)	(reg & 0x3F)
21 #define ADXL34X_READCMD(reg)	(ADXL34X_CMD_READ | (reg & 0x3F))
22 #define ADXL34X_READMB_CMD(reg) (ADXL34X_CMD_READ | ADXL34X_CMD_MULTB \
23 					| (reg & 0x3F))
24 
25 static int adxl34x_spi_read(struct device *dev, unsigned char reg)
26 {
27 	struct spi_device *spi = to_spi_device(dev);
28 	unsigned char cmd;
29 
30 	cmd = ADXL34X_READCMD(reg);
31 
32 	return spi_w8r8(spi, cmd);
33 }
34 
35 static int adxl34x_spi_write(struct device *dev,
36 			     unsigned char reg, unsigned char val)
37 {
38 	struct spi_device *spi = to_spi_device(dev);
39 	unsigned char buf[2];
40 
41 	buf[0] = ADXL34X_WRITECMD(reg);
42 	buf[1] = val;
43 
44 	return spi_write(spi, buf, sizeof(buf));
45 }
46 
47 static int adxl34x_spi_read_block(struct device *dev,
48 				  unsigned char reg, int count,
49 				  void *buf)
50 {
51 	struct spi_device *spi = to_spi_device(dev);
52 	ssize_t status;
53 
54 	reg = ADXL34X_READMB_CMD(reg);
55 	status = spi_write_then_read(spi, &reg, 1, buf, count);
56 
57 	return (status < 0) ? status : 0;
58 }
59 
60 static const struct adxl34x_bus_ops adx134x_spi_bops = {
61 	.bustype	= BUS_SPI,
62 	.write		= adxl34x_spi_write,
63 	.read		= adxl34x_spi_read,
64 	.read_block	= adxl34x_spi_read_block,
65 };
66 
67 static int __devinit adxl34x_spi_probe(struct spi_device *spi)
68 {
69 	struct adxl34x *ac;
70 
71 	/* don't exceed max specified SPI CLK frequency */
72 	if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
73 		dev_err(&spi->dev, "SPI CLK %d Hz too fast\n", spi->max_speed_hz);
74 		return -EINVAL;
75 	}
76 
77 	ac = adxl34x_probe(&spi->dev, spi->irq,
78 			   spi->max_speed_hz > MAX_FREQ_NO_FIFODELAY,
79 			   &adx134x_spi_bops);
80 
81 	if (IS_ERR(ac))
82 		return PTR_ERR(ac);
83 
84 	spi_set_drvdata(spi, ac);
85 
86 	return 0;
87 }
88 
89 static int __devexit adxl34x_spi_remove(struct spi_device *spi)
90 {
91 	struct adxl34x *ac = dev_get_drvdata(&spi->dev);
92 
93 	return adxl34x_remove(ac);
94 }
95 
96 #ifdef CONFIG_PM
97 static int adxl34x_suspend(struct spi_device *spi, pm_message_t message)
98 {
99 	struct adxl34x *ac = dev_get_drvdata(&spi->dev);
100 
101 	adxl34x_disable(ac);
102 
103 	return 0;
104 }
105 
106 static int adxl34x_resume(struct spi_device *spi)
107 {
108 	struct adxl34x *ac = dev_get_drvdata(&spi->dev);
109 
110 	adxl34x_enable(ac);
111 
112 	return 0;
113 }
114 #else
115 # define adxl34x_suspend NULL
116 # define adxl34x_resume  NULL
117 #endif
118 
119 static struct spi_driver adxl34x_driver = {
120 	.driver = {
121 		.name = "adxl34x",
122 		.bus = &spi_bus_type,
123 		.owner = THIS_MODULE,
124 	},
125 	.probe   = adxl34x_spi_probe,
126 	.remove  = __devexit_p(adxl34x_spi_remove),
127 	.suspend = adxl34x_suspend,
128 	.resume  = adxl34x_resume,
129 };
130 
131 static int __init adxl34x_spi_init(void)
132 {
133 	return spi_register_driver(&adxl34x_driver);
134 }
135 module_init(adxl34x_spi_init);
136 
137 static void __exit adxl34x_spi_exit(void)
138 {
139 	spi_unregister_driver(&adxl34x_driver);
140 }
141 module_exit(adxl34x_spi_exit);
142 
143 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
144 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer SPI Bus Driver");
145 MODULE_LICENSE("GPL");
146