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 | |
Takashi Iwai | 2ee3214 | 2010-10-01 17:14:25 -0400 | [diff] [blame^] | 57 | static union axis_conversion lis3lv02d_axis_normal = |
| 58 | { .as_array = { 1, 2, 3 } }; |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 59 | |
| 60 | static int __devinit lis302dl_spi_probe(struct spi_device *spi) |
| 61 | { |
| 62 | int ret; |
| 63 | |
| 64 | spi->bits_per_word = 8; |
| 65 | spi->mode = SPI_MODE_0; |
| 66 | ret = spi_setup(spi); |
| 67 | if (ret < 0) |
| 68 | return ret; |
| 69 | |
Daniel Mack | dc791f8 | 2009-09-21 17:04:45 -0700 | [diff] [blame] | 70 | lis3_dev.bus_priv = spi; |
| 71 | lis3_dev.init = lis3_spi_init; |
| 72 | lis3_dev.read = lis3_spi_read; |
| 73 | lis3_dev.write = lis3_spi_write; |
| 74 | lis3_dev.irq = spi->irq; |
| 75 | lis3_dev.ac = lis3lv02d_axis_normal; |
| 76 | lis3_dev.pdata = spi->dev.platform_data; |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 77 | spi_set_drvdata(spi, &lis3_dev); |
| 78 | |
Daniel Mack | dc791f8 | 2009-09-21 17:04:45 -0700 | [diff] [blame] | 79 | return lis3lv02d_init_device(&lis3_dev); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int __devexit lis302dl_spi_remove(struct spi_device *spi) |
| 83 | { |
| 84 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 85 | lis3lv02d_joystick_disable(); |
| 86 | lis3lv02d_poweroff(lis3); |
Samu Onkalo | d82e23dc | 2009-10-07 16:32:35 -0700 | [diff] [blame] | 87 | |
| 88 | return lis3lv02d_remove_fs(&lis3_dev); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 91 | #ifdef CONFIG_PM |
| 92 | static int lis3lv02d_spi_suspend(struct spi_device *spi, pm_message_t mesg) |
| 93 | { |
| 94 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 95 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 96 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 97 | lis3lv02d_poweroff(&lis3_dev); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int lis3lv02d_spi_resume(struct spi_device *spi) |
| 103 | { |
| 104 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 105 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 106 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 107 | lis3lv02d_poweron(lis3); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | #else |
| 113 | #define lis3lv02d_spi_suspend NULL |
| 114 | #define lis3lv02d_spi_resume NULL |
| 115 | #endif |
| 116 | |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 117 | static struct spi_driver lis302dl_spi_driver = { |
| 118 | .driver = { |
| 119 | .name = DRV_NAME, |
| 120 | .owner = THIS_MODULE, |
| 121 | }, |
| 122 | .probe = lis302dl_spi_probe, |
| 123 | .remove = __devexit_p(lis302dl_spi_remove), |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 124 | .suspend = lis3lv02d_spi_suspend, |
| 125 | .resume = lis3lv02d_spi_resume, |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static int __init lis302dl_init(void) |
| 129 | { |
| 130 | return spi_register_driver(&lis302dl_spi_driver); |
| 131 | } |
| 132 | |
| 133 | static void __exit lis302dl_exit(void) |
| 134 | { |
| 135 | spi_unregister_driver(&lis302dl_spi_driver); |
| 136 | } |
| 137 | |
| 138 | module_init(lis302dl_init); |
| 139 | module_exit(lis302dl_exit); |
| 140 | |
| 141 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 142 | MODULE_DESCRIPTION("lis3lv02d SPI glue layer"); |
| 143 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 144 | MODULE_ALIAS("spi:" DRV_NAME); |