Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ADLX345/346 Three-Axis Digital Accelerometers (SPI Interface)
0004  *
0005  * Enter bugs at http://blackfin.uclinux.org/
0006  *
0007  * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
0008  */
0009 
0010 #include <linux/input.h>    /* BUS_SPI */
0011 #include <linux/module.h>
0012 #include <linux/spi/spi.h>
0013 #include <linux/pm.h>
0014 #include <linux/types.h>
0015 #include "adxl34x.h"
0016 
0017 #define MAX_SPI_FREQ_HZ     5000000
0018 #define MAX_FREQ_NO_FIFODELAY   1500000
0019 #define ADXL34X_CMD_MULTB   (1 << 6)
0020 #define ADXL34X_CMD_READ    (1 << 7)
0021 #define ADXL34X_WRITECMD(reg)   (reg & 0x3F)
0022 #define ADXL34X_READCMD(reg)    (ADXL34X_CMD_READ | (reg & 0x3F))
0023 #define ADXL34X_READMB_CMD(reg) (ADXL34X_CMD_READ | ADXL34X_CMD_MULTB \
0024                     | (reg & 0x3F))
0025 
0026 static int adxl34x_spi_read(struct device *dev, unsigned char reg)
0027 {
0028     struct spi_device *spi = to_spi_device(dev);
0029     unsigned char cmd;
0030 
0031     cmd = ADXL34X_READCMD(reg);
0032 
0033     return spi_w8r8(spi, cmd);
0034 }
0035 
0036 static int adxl34x_spi_write(struct device *dev,
0037                  unsigned char reg, unsigned char val)
0038 {
0039     struct spi_device *spi = to_spi_device(dev);
0040     unsigned char buf[2];
0041 
0042     buf[0] = ADXL34X_WRITECMD(reg);
0043     buf[1] = val;
0044 
0045     return spi_write(spi, buf, sizeof(buf));
0046 }
0047 
0048 static int adxl34x_spi_read_block(struct device *dev,
0049                   unsigned char reg, int count,
0050                   void *buf)
0051 {
0052     struct spi_device *spi = to_spi_device(dev);
0053     ssize_t status;
0054 
0055     reg = ADXL34X_READMB_CMD(reg);
0056     status = spi_write_then_read(spi, &reg, 1, buf, count);
0057 
0058     return (status < 0) ? status : 0;
0059 }
0060 
0061 static const struct adxl34x_bus_ops adxl34x_spi_bops = {
0062     .bustype    = BUS_SPI,
0063     .write      = adxl34x_spi_write,
0064     .read       = adxl34x_spi_read,
0065     .read_block = adxl34x_spi_read_block,
0066 };
0067 
0068 static int adxl34x_spi_probe(struct spi_device *spi)
0069 {
0070     struct adxl34x *ac;
0071 
0072     /* don't exceed max specified SPI CLK frequency */
0073     if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
0074         dev_err(&spi->dev, "SPI CLK %d Hz too fast\n", spi->max_speed_hz);
0075         return -EINVAL;
0076     }
0077 
0078     ac = adxl34x_probe(&spi->dev, spi->irq,
0079                spi->max_speed_hz > MAX_FREQ_NO_FIFODELAY,
0080                &adxl34x_spi_bops);
0081 
0082     if (IS_ERR(ac))
0083         return PTR_ERR(ac);
0084 
0085     spi_set_drvdata(spi, ac);
0086 
0087     return 0;
0088 }
0089 
0090 static void adxl34x_spi_remove(struct spi_device *spi)
0091 {
0092     struct adxl34x *ac = spi_get_drvdata(spi);
0093 
0094     adxl34x_remove(ac);
0095 }
0096 
0097 static int __maybe_unused adxl34x_spi_suspend(struct device *dev)
0098 {
0099     struct spi_device *spi = to_spi_device(dev);
0100     struct adxl34x *ac = spi_get_drvdata(spi);
0101 
0102     adxl34x_suspend(ac);
0103 
0104     return 0;
0105 }
0106 
0107 static int __maybe_unused adxl34x_spi_resume(struct device *dev)
0108 {
0109     struct spi_device *spi = to_spi_device(dev);
0110     struct adxl34x *ac = spi_get_drvdata(spi);
0111 
0112     adxl34x_resume(ac);
0113 
0114     return 0;
0115 }
0116 
0117 static SIMPLE_DEV_PM_OPS(adxl34x_spi_pm, adxl34x_spi_suspend,
0118              adxl34x_spi_resume);
0119 
0120 static struct spi_driver adxl34x_driver = {
0121     .driver = {
0122         .name = "adxl34x",
0123         .pm = &adxl34x_spi_pm,
0124     },
0125     .probe   = adxl34x_spi_probe,
0126     .remove  = adxl34x_spi_remove,
0127 };
0128 
0129 module_spi_driver(adxl34x_driver);
0130 
0131 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
0132 MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer SPI Bus Driver");
0133 MODULE_LICENSE("GPL");