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> |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 19 | #include <linux/pm.h> |
Daniel Mack | 4bcdea8 | 2012-09-26 13:58:19 -0700 | [diff] [blame^] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_platform.h> |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 22 | |
| 23 | #include "lis3lv02d.h" |
| 24 | |
| 25 | #define DRV_NAME "lis3lv02d_spi" |
| 26 | #define LIS3_SPI_READ 0x80 |
| 27 | |
| 28 | static int lis3_spi_read(struct lis3lv02d *lis3, int reg, u8 *v) |
| 29 | { |
| 30 | struct spi_device *spi = lis3->bus_priv; |
| 31 | int ret = spi_w8r8(spi, reg | LIS3_SPI_READ); |
| 32 | if (ret < 0) |
| 33 | return -EINVAL; |
| 34 | |
| 35 | *v = (u8) ret; |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int lis3_spi_write(struct lis3lv02d *lis3, int reg, u8 val) |
| 40 | { |
| 41 | u8 tmp[2] = { reg, val }; |
| 42 | struct spi_device *spi = lis3->bus_priv; |
| 43 | return spi_write(spi, tmp, sizeof(tmp)); |
| 44 | } |
| 45 | |
| 46 | static int lis3_spi_init(struct lis3lv02d *lis3) |
| 47 | { |
| 48 | u8 reg; |
| 49 | int ret; |
| 50 | |
| 51 | /* power up the device */ |
| 52 | ret = lis3->read(lis3, CTRL_REG1, ®); |
| 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | |
Samu Onkalo | ed37d7f | 2010-10-22 07:57:28 -0400 | [diff] [blame] | 56 | reg |= CTRL1_PD0 | CTRL1_Xen | CTRL1_Yen | CTRL1_Zen; |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 57 | return lis3->write(lis3, CTRL_REG1, reg); |
| 58 | } |
| 59 | |
Takashi Iwai | 2ee3214 | 2010-10-01 17:14:25 -0400 | [diff] [blame] | 60 | static union axis_conversion lis3lv02d_axis_normal = |
| 61 | { .as_array = { 1, 2, 3 } }; |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 62 | |
Daniel Mack | 4bcdea8 | 2012-09-26 13:58:19 -0700 | [diff] [blame^] | 63 | #ifdef CONFIG_OF |
| 64 | static struct of_device_id lis302dl_spi_dt_ids[] = { |
| 65 | { .compatible = "st,lis302dl-spi" }, |
| 66 | {} |
| 67 | }; |
| 68 | MODULE_DEVICE_TABLE(of, lis302dl_spi_dt_ids); |
| 69 | #endif |
| 70 | |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 71 | static int __devinit lis302dl_spi_probe(struct spi_device *spi) |
| 72 | { |
| 73 | int ret; |
| 74 | |
| 75 | spi->bits_per_word = 8; |
| 76 | spi->mode = SPI_MODE_0; |
| 77 | ret = spi_setup(spi); |
| 78 | if (ret < 0) |
| 79 | return ret; |
| 80 | |
Daniel Mack | dc791f8 | 2009-09-21 17:04:45 -0700 | [diff] [blame] | 81 | lis3_dev.bus_priv = spi; |
| 82 | lis3_dev.init = lis3_spi_init; |
| 83 | lis3_dev.read = lis3_spi_read; |
| 84 | lis3_dev.write = lis3_spi_write; |
| 85 | lis3_dev.irq = spi->irq; |
| 86 | lis3_dev.ac = lis3lv02d_axis_normal; |
| 87 | lis3_dev.pdata = spi->dev.platform_data; |
Daniel Mack | 4bcdea8 | 2012-09-26 13:58:19 -0700 | [diff] [blame^] | 88 | |
| 89 | #ifdef CONFIG_OF |
| 90 | if (of_match_device(lis302dl_spi_dt_ids, &spi->dev)) |
| 91 | lis3_dev.of_node = spi->dev.of_node; |
| 92 | #endif |
| 93 | |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 94 | spi_set_drvdata(spi, &lis3_dev); |
| 95 | |
Daniel Mack | dc791f8 | 2009-09-21 17:04:45 -0700 | [diff] [blame] | 96 | return lis3lv02d_init_device(&lis3_dev); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static int __devexit lis302dl_spi_remove(struct spi_device *spi) |
| 100 | { |
| 101 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
Éric Piel | e1e5687 | 2011-10-31 17:11:02 -0700 | [diff] [blame] | 102 | lis3lv02d_joystick_disable(lis3); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 103 | lis3lv02d_poweroff(lis3); |
Samu Onkalo | d82e23dc | 2009-10-07 16:32:35 -0700 | [diff] [blame] | 104 | |
| 105 | return lis3lv02d_remove_fs(&lis3_dev); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 108 | #ifdef CONFIG_PM_SLEEP |
| 109 | static int lis3lv02d_spi_suspend(struct device *dev) |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 110 | { |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 111 | struct spi_device *spi = to_spi_device(dev); |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 112 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 113 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 114 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 115 | lis3lv02d_poweroff(&lis3_dev); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 120 | static int lis3lv02d_spi_resume(struct device *dev) |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 121 | { |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 122 | struct spi_device *spi = to_spi_device(dev); |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 123 | struct lis3lv02d *lis3 = spi_get_drvdata(spi); |
| 124 | |
Kuninori Morimoto | 5facb09 | 2010-09-17 17:24:10 +0200 | [diff] [blame] | 125 | if (!lis3->pdata || !lis3->pdata->wakeup_flags) |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 126 | lis3lv02d_poweron(lis3); |
| 127 | |
| 128 | return 0; |
| 129 | } |
Daniel Mack | 2cd9645 | 2009-09-21 17:04:44 -0700 | [diff] [blame] | 130 | #endif |
| 131 | |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 132 | static SIMPLE_DEV_PM_OPS(lis3lv02d_spi_pm, lis3lv02d_spi_suspend, |
| 133 | lis3lv02d_spi_resume); |
| 134 | |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 135 | static struct spi_driver lis302dl_spi_driver = { |
| 136 | .driver = { |
| 137 | .name = DRV_NAME, |
| 138 | .owner = THIS_MODULE, |
Mark Brown | 4e9be65 | 2011-01-20 21:27:56 +0000 | [diff] [blame] | 139 | .pm = &lis3lv02d_spi_pm, |
Daniel Mack | 4bcdea8 | 2012-09-26 13:58:19 -0700 | [diff] [blame^] | 140 | .of_match_table = of_match_ptr(lis302dl_spi_dt_ids), |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 141 | }, |
| 142 | .probe = lis302dl_spi_probe, |
| 143 | .remove = __devexit_p(lis302dl_spi_remove), |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Axel Lin | a3dc3c9 | 2012-01-22 15:38:22 +0800 | [diff] [blame] | 146 | module_spi_driver(lis302dl_spi_driver); |
Daniel Mack | bb233fd | 2009-03-31 15:24:33 -0700 | [diff] [blame] | 147 | |
| 148 | MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>"); |
| 149 | MODULE_DESCRIPTION("lis3lv02d SPI glue layer"); |
| 150 | MODULE_LICENSE("GPL"); |
Anton Vorontsov | e0626e3 | 2009-09-22 16:46:08 -0700 | [diff] [blame] | 151 | MODULE_ALIAS("spi:" DRV_NAME); |