Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * lis3lv02d_spi - SPI glue layer for lis3lv02d |
| 3 | * |
| 4 | * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * publishhed by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/input.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | #include <linux/spi/spi.h> |
| 19 | |
| 20 | #include "lis3lv02d.h" |
| 21 | |
| 22 | #define DRV_NAME "lis3lv02d_spi" |
| 23 | #define LIS3_SPI_READ 0x80 |
| 24 | |
| 25 | static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v) |
| 26 | { |
| 27 | struct spi_device *spi = lis3->bus_priv; |
| 28 | int ret = spi_w8r8(spi, reg | LIS3_SPI_READ); |
| 29 | if (ret < 0) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | *v = (u8) ret; |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val) |
| 37 | { |
| 38 | u8 tmp[2] = { reg, val }; |
| 39 | struct spi_device *spi = lis3->bus_priv; |
| 40 | return spi_write(spi, tmp, sizeof(tmp)); |
| 41 | } |
| 42 | |
| 43 | static int lis3_spi_init(struct lis3lv02d *lis3) |
| 44 | { |
| 45 | u8 reg; |
| 46 | int ret; |
| 47 | |
| 48 | /* power up the device */ |
| 49 | ret = lis3->read(lis3, CTRL_REG1, ®); |
| 50 | if (ret < 0) |
| 51 | return ret; |
| 52 | |
| 53 | reg |= CTRL1_PD0; |
| 54 | return lis3->write(lis3, CTRL_REG1, reg); |
| 55 | } |
| 56 | |
| 57 | static struct axis_conversion lis3lv02d_axis_normal = { 1, 2, 3 }; |
| 58 | |
| 59 | static int __devinit lis302dl_spi_probe(struct spi_device *spi) |
| 60 | { |
| 61 | int ret; |
| 62 | |
| 63 | spi->bits_per_word = 8; |
| 64 | spi->mode = SPI_MODE_0; |
| 65 | ret = spi_setup(spi); |
| 66 | if (ret < 0) |
| 67 | return ret; |
| 68 | |
Daniel Mack | dc791f8 | 2009-09-21 17:04:45 -0700 | [diff] [blame] | 69 | lis3_dev.bus_priv = spi; |
| 70 | lis3_dev.init = lis3_spi_init; |
| 71 | lis3_dev.read = lis3_spi_read; |
| 72 | lis3_dev.write = lis3_spi_write; |
| 73 | lis3_dev.irq = spi->irq; |
| 74 | lis3_dev.ac = lis3lv02d_axis_normal; |
| 75 | lis3_dev.pdata = spi->dev.platform_data; |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 76 | spi_set_drvdata(spi, &lis3_dev); |
| 77 | |
Daniel Mack | dc791f8 | 2009-09-21 17:04:45 -0700 | [diff] [blame] | 78 | return lis3lv02d_init_device(&lis3_dev); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static int __devexit lis302dl_spi_remove(struct spi_device *spi) |
| 82 | { |
| 83 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 84 | lis3lv02d_joystick_disable(); |
| 85 | lis3lv02d_poweroff(lis3); |
Samu Onkalo | d82e23dc | 2009-10-07 16:32:35 -0700 | [diff] [blame] | 86 | |
| 87 | return lis3lv02d_remove_fs(&lis3_dev); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 90 | #ifdef CONFIG_PM |
| 91 | static int lis3lv02d_spi_suspend(struct spi_device *spi, pm_message_t mesg) |
| 92 | { |
| 93 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 94 | |
| 95 | if (!lis3->pdata->wakeup_flags) |
| 96 | lis3lv02d_poweroff(&lis3_dev); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int lis3lv02d_spi_resume(struct spi_device *spi) |
| 102 | { |
| 103 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 104 | |
| 105 | if (!lis3->pdata->wakeup_flags) |
| 106 | lis3lv02d_poweron(lis3); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | #else |
| 112 | #define lis3lv02d_spi_suspend NULL |
| 113 | #define lis3lv02d_spi_resume NULL |
| 114 | #endif |
| 115 | |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 116 | static struct spi_driver lis302dl_spi_driver = { |
| 117 | .driver = { |
| 118 | .name = DRV_NAME, |
| 119 | .owner = THIS_MODULE, |
| 120 | }, |
| 121 | .probe = lis302dl_spi_probe, |
| 122 | .remove = __devexit_p(lis302dl_spi_remove), |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 123 | .suspend = lis3lv02d_spi_suspend, |
| 124 | .resume = lis3lv02d_spi_resume, |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static int __init lis302dl_init(void) |
| 128 | { |
| 129 | return spi_register_driver(&lis302dl_spi_driver); |
| 130 | } |
| 131 | |
| 132 | static void __exit lis302dl_exit(void) |
| 133 | { |
| 134 | spi_unregister_driver(&lis302dl_spi_driver); |
| 135 | } |
| 136 | |
| 137 | module_init(lis302dl_init); |
| 138 | module_exit(lis302dl_exit); |
| 139 | |
| 140 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 141 | MODULE_DESCRIPTION("lis3lv02d SPI glue layer"); |
| 142 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 143 | MODULE_ALIAS("spi:" DRV_NAME); |