lis3lv02d_spi.c 3.2 KB
Newer Older
D
Daniel Mack 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * lis3lv02d_spi - SPI glue layer for lis3lv02d
 *
 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  publishhed by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/err.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>
#include <linux/spi/spi.h>
19
#include <linux/pm.h>
D
Daniel Mack 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#include "lis3lv02d.h"

#define DRV_NAME 	"lis3lv02d_spi"
#define LIS3_SPI_READ	0x80

static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v)
{
	struct spi_device *spi = lis3->bus_priv;
	int ret = spi_w8r8(spi, reg | LIS3_SPI_READ);
	if (ret < 0)
		return -EINVAL;

	*v = (u8) ret;
	return 0;
}

static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val)
{
	u8 tmp[2] = { reg, val };
	struct spi_device *spi = lis3->bus_priv;
	return spi_write(spi, tmp, sizeof(tmp));
}

static int lis3_spi_init(struct lis3lv02d *lis3)
{
	u8 reg;
	int ret;

	/* power up the device */
	ret = lis3->read(lis3, CTRL_REG1, &reg);
	if (ret < 0)
		return ret;

54
	reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen;
D
Daniel Mack 已提交
55 56 57
	return lis3->write(lis3, CTRL_REG1, reg);
}

58 59
static union axis_conversion lis3lv02d_axis_normal =
	{ .as_array = { 1, 2, 3 } };
D
Daniel Mack 已提交
60 61 62 63 64 65 66 67 68 69 70

static int __devinit lis302dl_spi_probe(struct spi_device *spi)
{
	int ret;

	spi->bits_per_word = 8;
	spi->mode = SPI_MODE_0;
	ret = spi_setup(spi);
	if (ret < 0)
		return ret;

D
Daniel Mack 已提交
71 72 73 74 75 76 77
	lis3_dev.bus_priv	= spi;
	lis3_dev.init		= lis3_spi_init;
	lis3_dev.read		= lis3_spi_read;
	lis3_dev.write		= lis3_spi_write;
	lis3_dev.irq		= spi->irq;
	lis3_dev.ac		= lis3lv02d_axis_normal;
	lis3_dev.pdata		= spi->dev.platform_data;
D
Daniel Mack 已提交
78 79
	spi_set_drvdata(spi, &lis3_dev);

D
Daniel Mack 已提交
80
	return lis3lv02d_init_device(&lis3_dev);
D
Daniel Mack 已提交
81 82 83 84 85
}

static int __devexit lis302dl_spi_remove(struct spi_device *spi)
{
	struct lis3lv02d *lis3 = spi_get_drvdata(spi);
86
	lis3lv02d_joystick_disable(lis3);
D
Daniel Mack 已提交
87
	lis3lv02d_poweroff(lis3);
88 89

	return lis3lv02d_remove_fs(&lis3_dev);
D
Daniel Mack 已提交
90 91
}

92 93
#ifdef CONFIG_PM_SLEEP
static int lis3lv02d_spi_suspend(struct device *dev)
94
{
95
	struct spi_device *spi = to_spi_device(dev);
96 97
	struct lis3lv02d *lis3 = spi_get_drvdata(spi);

98
	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
99 100 101 102 103
		lis3lv02d_poweroff(&lis3_dev);

	return 0;
}

104
static int lis3lv02d_spi_resume(struct device *dev)
105
{
106
	struct spi_device *spi = to_spi_device(dev);
107 108
	struct lis3lv02d *lis3 = spi_get_drvdata(spi);

109
	if (!lis3->pdata || !lis3->pdata->wakeup_flags)
110 111 112 113 114 115
		lis3lv02d_poweron(lis3);

	return 0;
}
#endif

116 117 118
static SIMPLE_DEV_PM_OPS(lis3lv02d_spi_pm, lis3lv02d_spi_suspend,
			 lis3lv02d_spi_resume);

D
Daniel Mack 已提交
119 120 121 122
static struct spi_driver lis302dl_spi_driver = {
	.driver	 = {
		.name   = DRV_NAME,
		.owner  = THIS_MODULE,
123
		.pm	= &lis3lv02d_spi_pm,
D
Daniel Mack 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	},
	.probe	= lis302dl_spi_probe,
	.remove	= __devexit_p(lis302dl_spi_remove),
};

static int __init lis302dl_init(void)
{
	return spi_register_driver(&lis302dl_spi_driver);
}

static void __exit lis302dl_exit(void)
{
	spi_unregister_driver(&lis302dl_spi_driver);
}

module_init(lis302dl_init);
module_exit(lis302dl_exit);

MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
MODULE_DESCRIPTION("lis3lv02d SPI glue layer");
MODULE_LICENSE("GPL");
145
MODULE_ALIAS("spi:" DRV_NAME);