Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / input / misc / adxl34x-spi.c
blob76dc0679d3b14a3c7f9cf21e6473162fc6c10142
1 /*
2 * ADLX345/346 Three-Axis Digital Accelerometers (SPI Interface)
4 * Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
8 */
10 #include <linux/input.h> /* BUS_SPI */
11 #include <linux/module.h>
12 #include <linux/spi/spi.h>
13 #include <linux/pm.h>
14 #include <linux/types.h>
15 #include "adxl34x.h"
17 #define MAX_SPI_FREQ_HZ 5000000
18 #define MAX_FREQ_NO_FIFODELAY 1500000
19 #define ADXL34X_CMD_MULTB (1 << 6)
20 #define ADXL34X_CMD_READ (1 << 7)
21 #define ADXL34X_WRITECMD(reg) (reg & 0x3F)
22 #define ADXL34X_READCMD(reg) (ADXL34X_CMD_READ | (reg & 0x3F))
23 #define ADXL34X_READMB_CMD(reg) (ADXL34X_CMD_READ | ADXL34X_CMD_MULTB \
24 | (reg & 0x3F))
26 static int adxl34x_spi_read(struct device *dev, unsigned char reg)
28 struct spi_device *spi = to_spi_device(dev);
29 unsigned char cmd;
31 cmd = ADXL34X_READCMD(reg);
33 return spi_w8r8(spi, cmd);
36 static int adxl34x_spi_write(struct device *dev,
37 unsigned char reg, unsigned char val)
39 struct spi_device *spi = to_spi_device(dev);
40 unsigned char buf[2];
42 buf[0] = ADXL34X_WRITECMD(reg);
43 buf[1] = val;
45 return spi_write(spi, buf, sizeof(buf));
48 static int adxl34x_spi_read_block(struct device *dev,
49 unsigned char reg, int count,
50 void *buf)
52 struct spi_device *spi = to_spi_device(dev);
53 ssize_t status;
55 reg = ADXL34X_READMB_CMD(reg);
56 status = spi_write_then_read(spi, &reg, 1, buf, count);
58 return (status < 0) ? status : 0;
61 static const struct adxl34x_bus_ops adxl34x_spi_bops = {
62 .bustype = BUS_SPI,
63 .write = adxl34x_spi_write,
64 .read = adxl34x_spi_read,
65 .read_block = adxl34x_spi_read_block,
68 static int adxl34x_spi_probe(struct spi_device *spi)
70 struct adxl34x *ac;
72 /* don't exceed max specified SPI CLK frequency */
73 if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
74 dev_err(&spi->dev, "SPI CLK %d Hz too fast\n", spi->max_speed_hz);
75 return -EINVAL;
78 ac = adxl34x_probe(&spi->dev, spi->irq,
79 spi->max_speed_hz > MAX_FREQ_NO_FIFODELAY,
80 &adxl34x_spi_bops);
82 if (IS_ERR(ac))
83 return PTR_ERR(ac);
85 spi_set_drvdata(spi, ac);
87 return 0;
90 static int adxl34x_spi_remove(struct spi_device *spi)
92 struct adxl34x *ac = spi_get_drvdata(spi);
94 return adxl34x_remove(ac);
97 #ifdef CONFIG_PM_SLEEP
98 static int adxl34x_spi_suspend(struct device *dev)
100 struct spi_device *spi = to_spi_device(dev);
101 struct adxl34x *ac = spi_get_drvdata(spi);
103 adxl34x_suspend(ac);
105 return 0;
108 static int adxl34x_spi_resume(struct device *dev)
110 struct spi_device *spi = to_spi_device(dev);
111 struct adxl34x *ac = spi_get_drvdata(spi);
113 adxl34x_resume(ac);
115 return 0;
117 #endif
119 static SIMPLE_DEV_PM_OPS(adxl34x_spi_pm, adxl34x_spi_suspend,
120 adxl34x_spi_resume);
122 static struct spi_driver adxl34x_driver = {
123 .driver = {
124 .name = "adxl34x",
125 .owner = THIS_MODULE,
126 .pm = &adxl34x_spi_pm,
128 .probe = adxl34x_spi_probe,
129 .remove = adxl34x_spi_remove,
132 module_spi_driver(adxl34x_driver);
134 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
135 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer SPI Bus Driver");
136 MODULE_LICENSE("GPL");