Flemmard | c36b867 | 2013-10-23 17:43:54 +0200 | [diff] [blame^] | 1 | /* linux/driver/spi/spi_oj.c |
| 2 | * |
| 3 | * |
| 4 | * Copyright (C) 2009 HTC Corporation. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * |
| 16 | * |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/version.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/spi/spi.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/gpio.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | |
| 29 | #define tSRAD 160 |
| 30 | #define tSWW 30 |
| 31 | |
| 32 | #define OJ_MOTION (26) |
| 33 | #define OJ_SHUTDOWN (35) |
| 34 | #define OJ_RST (36) |
| 35 | |
| 36 | // #define OJ_DEBUG 1 |
| 37 | static struct spi_device *spidev; |
| 38 | struct work_struct oj_work; |
| 39 | |
| 40 | u8 oj_reg_read(u8 reg) |
| 41 | { |
| 42 | u8 val; |
| 43 | struct spi_msg msg; |
| 44 | int err = 0; |
| 45 | if (!spidev) |
| 46 | { |
| 47 | printk(KERN_ERR "%s: spidev = 0\n", __func__); |
| 48 | return 0; |
| 49 | } |
| 50 | udelay(1); |
| 51 | |
| 52 | msg.buffer[0] = reg; |
| 53 | msg.len = 0; |
| 54 | err += spi_read_write_lock(spidev, &msg, NULL, 1, 1); |
| 55 | udelay(tSRAD); |
| 56 | err += spi_read_write_lock(spidev, NULL, &val, 1, 0); |
| 57 | #ifdef OJ_DEBUG |
| 58 | if (err != 0) |
| 59 | printk("oj_reg_read fail\n"); |
| 60 | #endif |
| 61 | return val; |
| 62 | } |
| 63 | EXPORT_SYMBOL(oj_reg_read); |
| 64 | |
| 65 | int oj_reg_write(u8 reg, u8 val) |
| 66 | { |
| 67 | struct spi_msg msg; |
| 68 | int err = 0; |
| 69 | if (!spidev) |
| 70 | return 0; |
| 71 | #ifdef OJ_DEBUG |
| 72 | printk(KERN_INFO "write %x to reg%x\n", val, reg); |
| 73 | #endif |
| 74 | msg.buffer[0] = reg | 0x80; |
| 75 | msg.buffer[1] = val; |
| 76 | msg.len = 0; |
| 77 | udelay(1); |
| 78 | err += spi_read_write_lock(spidev, &msg, NULL, 2, 1); |
| 79 | #ifdef OJ_DEBUG |
| 80 | if (err != 0) |
| 81 | printk("oj_reg_write fail\n"); |
| 82 | #endif |
| 83 | return 0; |
| 84 | } |
| 85 | EXPORT_SYMBOL(oj_reg_write); |
| 86 | |
| 87 | int oj_burst_read(char *buf, int len) |
| 88 | { |
| 89 | struct spi_msg msg; |
| 90 | int err = 0; |
| 91 | if (!spidev) |
| 92 | return 0; |
| 93 | |
| 94 | msg.buffer[0] = 0x02; |
| 95 | msg.len = 0; |
| 96 | udelay(1); |
| 97 | err += spi_read_write_lock(spidev, &msg, NULL, 1, 1); |
| 98 | udelay(tSRAD); |
| 99 | err += spi_read_write_lock(spidev, NULL, buf, len, 0); |
| 100 | #ifdef OJ_DEBUG |
| 101 | if (err != 0) |
| 102 | printk("oj_burst_read fail\n"); |
| 103 | #endif |
| 104 | return 0; |
| 105 | } |
| 106 | EXPORT_SYMBOL(oj_burst_read); |
| 107 | |
| 108 | #ifdef OJ_DEBUG |
| 109 | static irqreturn_t oj_interrupt(int irq, void *data) |
| 110 | { |
| 111 | int motion; |
| 112 | /* printk("oj interrupt\n");*/ |
| 113 | motion = gpio_get_value(OJ_MOTION); |
| 114 | if (motion) |
| 115 | return IRQ_HANDLED; |
| 116 | schedule_work(&oj_work); |
| 117 | return IRQ_HANDLED; |
| 118 | } |
| 119 | |
| 120 | static void oj_do_work(struct work_struct *w) |
| 121 | { |
| 122 | char motion_val; |
| 123 | printk(KERN_INFO "%s\n", __func__); |
| 124 | printk(KERN_INFO "reg02 = 0x%x\n", oj_reg_read(0x02)); |
| 125 | printk(KERN_INFO "reg03 = 0x%x\n", oj_reg_read(0x03)); |
| 126 | printk(KERN_INFO "reg04 = 0x%x\n", oj_reg_read(0x04)); |
| 127 | motion_val = oj_reg_read(0x02); |
| 128 | if (motion_val & (1 << 7)) { |
| 129 | printk(KERN_INFO "reg02 = 0x%x\n", oj_reg_read(0x02)); |
| 130 | printk(KERN_INFO "reg03 = 0x%x\n", oj_reg_read(0x03)); |
| 131 | printk(KERN_INFO "reg04 = 0x%x\n", oj_reg_read(0x04)); |
| 132 | } |
| 133 | |
| 134 | printk(KERN_INFO "gpio26 = %d\n", gpio_get_value(26)); |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | static int oj_spi_probe(struct spi_device *spi) |
| 139 | { |
| 140 | #ifdef OJ_DEBUG |
| 141 | int irq_oj; |
| 142 | int ret; |
| 143 | #endif |
| 144 | printk(KERN_INFO "%s \n", __func__); |
| 145 | spidev = spi; |
| 146 | |
| 147 | #ifdef OJ_DEBUG |
| 148 | gpio_set_value(OJ_RST, 0); |
| 149 | udelay(20); |
| 150 | gpio_set_value(OJ_RST, 1); |
| 151 | mdelay(10); |
| 152 | oj_reg_write(0x3a, 0x5a); |
| 153 | mdelay(23); |
| 154 | printk(KERN_INFO "reg02 = 0x%x\n", oj_reg_read(0x02)); |
| 155 | printk(KERN_INFO "reg03 = 0x%x\n", oj_reg_read(0x03)); |
| 156 | printk(KERN_INFO "reg04 = 0x%x\n", oj_reg_read(0x04)); |
| 157 | printk(KERN_INFO "reg00 = 0x%x\n", oj_reg_read(0x00)); |
| 158 | printk(KERN_INFO "gpio26 = %d\n", gpio_get_value(26)); |
| 159 | |
| 160 | ret = gpio_request(OJ_MOTION, "OJ irq"); |
| 161 | if (ret < 0) { |
| 162 | printk(KERN_ERR "gpio_request error\n"); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | ret = gpio_direction_input(OJ_MOTION); |
| 167 | if (ret < 0) { |
| 168 | printk(KERN_ERR "gpio_drection_input eror\n"); |
| 169 | gpio_free(OJ_MOTION); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | irq_oj = gpio_to_irq(OJ_MOTION); |
| 174 | if (irq_oj < 0) { |
| 175 | printk(KERN_ERR "gpio_to_irq error\n"); |
| 176 | gpio_free(OJ_MOTION); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | ret = request_irq(irq_oj, oj_interrupt, |
| 181 | IRQF_TRIGGER_FALLING, "OJ irq", NULL); |
| 182 | if (ret < 0) { |
| 183 | printk(KERN_ERR "%s(): request_irq fail\n", __func__); |
| 184 | gpio_free(OJ_MOTION); |
| 185 | } |
| 186 | INIT_WORK(&oj_work, oj_do_work); |
| 187 | #endif |
| 188 | return 0 ; |
| 189 | } |
| 190 | |
| 191 | static int __exit oj_spi_remove(struct spi_device *spi) |
| 192 | { |
| 193 | spidev = NULL; |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | |
| 198 | static struct spi_driver spi_oj = { |
| 199 | .driver = { |
| 200 | .name = "spi_oj", |
| 201 | .owner = THIS_MODULE, |
| 202 | }, |
| 203 | .probe = oj_spi_probe, |
| 204 | .remove = __exit_p(oj_spi_remove), |
| 205 | }; |
| 206 | |
| 207 | static int __init spi_oj_init(void) |
| 208 | { |
| 209 | int rc; |
| 210 | rc = spi_register_driver(&spi_oj); |
| 211 | return rc; |
| 212 | } |
| 213 | module_init(spi_oj_init); |
| 214 | |
| 215 | static void __exit spi_oj_exit(void) |
| 216 | { |
| 217 | spi_unregister_driver(&spi_oj); |
| 218 | } |
| 219 | module_exit(spi_oj_exit); |
| 220 | |
| 221 | #ifdef OJ_DEBUG |
| 222 | static int oj_set_debug(const char *val, struct kernel_param *kp) |
| 223 | { |
| 224 | printk(KERN_INFO "reg02 = 0x%x\n", oj_reg_read(0x02)); |
| 225 | printk(KERN_INFO "reg03 = 0x%x\n", oj_reg_read(0x03)); |
| 226 | printk(KERN_INFO "reg04 = 0x%x\n", oj_reg_read(0x04)); |
| 227 | printk(KERN_INFO "reg00 = 0x%x\n", oj_reg_read(0x00)); |
| 228 | printk(KERN_INFO "gpio26 = %d\n", gpio_get_value(26)); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int oj_get_debug(char *buffer, struct kernel_param *kp) |
| 233 | { |
| 234 | oj_reg_write(0x02, 0); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | //module_param_call(debug, oj_set_debug, oj_get_debug, 0, 0664); |
| 239 | #endif /*OJ_DEBUG*/ |