Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 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> |
Mark Brown | 4d1ac94 | 2011-02-11 09:22:40 -0800 | [diff] [blame] | 13 | #include <linux/pm.h> |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 14 | #include <linux/types.h> |
| 15 | #include "adxl34x.h" |
| 16 | |
| 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)) |
| 25 | |
| 26 | static int adxl34x_spi_read(struct device *dev, unsigned char reg) |
| 27 | { |
| 28 | struct spi_device *spi = to_spi_device(dev); |
| 29 | unsigned char cmd; |
| 30 | |
| 31 | cmd = ADXL34X_READCMD(reg); |
| 32 | |
| 33 | return spi_w8r8(spi, cmd); |
| 34 | } |
| 35 | |
| 36 | static int adxl34x_spi_write(struct device *dev, |
| 37 | unsigned char reg, unsigned char val) |
| 38 | { |
| 39 | struct spi_device *spi = to_spi_device(dev); |
| 40 | unsigned char buf[2]; |
| 41 | |
| 42 | buf[0] = ADXL34X_WRITECMD(reg); |
| 43 | buf[1] = val; |
| 44 | |
| 45 | return spi_write(spi, buf, sizeof(buf)); |
| 46 | } |
| 47 | |
| 48 | static int adxl34x_spi_read_block(struct device *dev, |
| 49 | unsigned char reg, int count, |
| 50 | void *buf) |
| 51 | { |
| 52 | struct spi_device *spi = to_spi_device(dev); |
| 53 | ssize_t status; |
| 54 | |
| 55 | reg = ADXL34X_READMB_CMD(reg); |
| 56 | status = spi_write_then_read(spi, ®, 1, buf, count); |
| 57 | |
| 58 | return (status < 0) ? status : 0; |
| 59 | } |
| 60 | |
Mark Brown | 3dcab3b | 2011-02-11 08:53:04 -0800 | [diff] [blame] | 61 | static const struct adxl34x_bus_ops adxl34x_spi_bops = { |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 62 | .bustype = BUS_SPI, |
| 63 | .write = adxl34x_spi_write, |
| 64 | .read = adxl34x_spi_read, |
| 65 | .read_block = adxl34x_spi_read_block, |
| 66 | }; |
| 67 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 68 | static int adxl34x_spi_probe(struct spi_device *spi) |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 69 | { |
| 70 | struct adxl34x *ac; |
| 71 | |
| 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; |
| 76 | } |
| 77 | |
| 78 | ac = adxl34x_probe(&spi->dev, spi->irq, |
| 79 | spi->max_speed_hz > MAX_FREQ_NO_FIFODELAY, |
Mark Brown | 3dcab3b | 2011-02-11 08:53:04 -0800 | [diff] [blame] | 80 | &adxl34x_spi_bops); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 81 | |
| 82 | if (IS_ERR(ac)) |
| 83 | return PTR_ERR(ac); |
| 84 | |
| 85 | spi_set_drvdata(spi, ac); |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 90 | static int adxl34x_spi_remove(struct spi_device *spi) |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 91 | { |
Jingoo Han | 7a24677 | 2013-04-07 20:52:16 -0700 | [diff] [blame] | 92 | struct adxl34x *ac = spi_get_drvdata(spi); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 93 | |
| 94 | return adxl34x_remove(ac); |
| 95 | } |
| 96 | |
Jingoo Han | 97a652a | 2014-11-02 00:02:46 -0700 | [diff] [blame] | 97 | static int __maybe_unused adxl34x_spi_suspend(struct device *dev) |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 98 | { |
Mark Brown | 4d1ac94 | 2011-02-11 09:22:40 -0800 | [diff] [blame] | 99 | struct spi_device *spi = to_spi_device(dev); |
Jingoo Han | 7a24677 | 2013-04-07 20:52:16 -0700 | [diff] [blame] | 100 | struct adxl34x *ac = spi_get_drvdata(spi); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 101 | |
Dmitry Torokhov | af6e1d9 | 2010-07-01 09:07:33 -0700 | [diff] [blame] | 102 | adxl34x_suspend(ac); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Jingoo Han | 97a652a | 2014-11-02 00:02:46 -0700 | [diff] [blame] | 107 | static int __maybe_unused adxl34x_spi_resume(struct device *dev) |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 108 | { |
Mark Brown | 4d1ac94 | 2011-02-11 09:22:40 -0800 | [diff] [blame] | 109 | struct spi_device *spi = to_spi_device(dev); |
Jingoo Han | 7a24677 | 2013-04-07 20:52:16 -0700 | [diff] [blame] | 110 | struct adxl34x *ac = spi_get_drvdata(spi); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 111 | |
Dmitry Torokhov | af6e1d9 | 2010-07-01 09:07:33 -0700 | [diff] [blame] | 112 | adxl34x_resume(ac); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 116 | |
Mark Brown | 4d1ac94 | 2011-02-11 09:22:40 -0800 | [diff] [blame] | 117 | static SIMPLE_DEV_PM_OPS(adxl34x_spi_pm, adxl34x_spi_suspend, |
| 118 | adxl34x_spi_resume); |
| 119 | |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 120 | static struct spi_driver adxl34x_driver = { |
| 121 | .driver = { |
| 122 | .name = "adxl34x", |
Mark Brown | 4d1ac94 | 2011-02-11 09:22:40 -0800 | [diff] [blame] | 123 | .pm = &adxl34x_spi_pm, |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 124 | }, |
| 125 | .probe = adxl34x_spi_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 126 | .remove = adxl34x_spi_remove, |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 127 | }; |
| 128 | |
Axel Lin | ca83922 | 2012-03-16 23:05:26 -0700 | [diff] [blame] | 129 | module_spi_driver(adxl34x_driver); |
Michael Hennerich | e27c729 | 2010-06-25 08:44:10 -0700 | [diff] [blame] | 130 | |
| 131 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
| 132 | MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer SPI Bus Driver"); |
| 133 | MODULE_LICENSE("GPL"); |