Initial commit
[kernel/linux-3.0.git] / drivers / misc / pn544.c
1 /*
2  * Copyright (C) 2010 Trusted Logic S.A.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/fs.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/i2c.h>
27 #include <linux/irq.h>
28 #include <linux/jiffies.h>
29 #include <linux/uaccess.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/io.h>
33 #include <linux/platform_device.h>
34 #include <linux/gpio.h>
35 #include <linux/miscdevice.h>
36 #include <linux/spinlock.h>
37 #include <linux/pn544.h>
38
39 #define MAX_BUFFER_SIZE 512
40 #define READ_IRQ_MODIFY /* DY_TEST */
41
42 #ifdef READ_IRQ_MODIFY /* DY_TEST */
43 bool do_reading;
44 static bool cancle_read;
45 #endif
46 #define NFC_DEBUG 0
47
48 struct pn544_dev {
49         wait_queue_head_t read_wq;
50         struct mutex read_mutex;
51         struct i2c_client *client;
52         struct miscdevice pn544_device;
53         unsigned int ven_gpio;
54         unsigned int firm_gpio;
55         unsigned int irq_gpio;
56         bool irq_enabled;
57         spinlock_t irq_enabled_lock;
58 };
59
60 bool do_reading;
61
62 static struct pn544_dev *pn544_dev_imsi;
63
64 static void pn544_disable_irq(struct pn544_dev *pn544_dev)
65 {
66         /* unsigned long flags; */
67         /* spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags); */
68         if (pn544_dev->irq_enabled) {
69                 pn544_dev->irq_enabled = false;
70                 disable_irq_nosync(pn544_dev->client->irq);
71         }
72         /* spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags); */
73 }
74
75 static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id)
76 {
77         struct pn544_dev *pn544_dev = dev_id;
78
79         if (!gpio_get_value_cansleep(pn544_dev->irq_gpio)) {
80                 pr_info("pn544 : pn544_dev_irq_handler error\n");
81                 return IRQ_HANDLED;
82         }
83         /* pn544_disable_irq(pn544_dev); */
84 #ifdef READ_IRQ_MODIFY /* DY_TEST */
85         do_reading = 1;
86 #endif
87         /* Wake up waiting readers */
88         wake_up(&pn544_dev->read_wq);
89
90 #if NFC_DEBUG
91         pr_info("pn544 : call\n");
92 #endif
93
94         return IRQ_HANDLED;
95 }
96
97 static ssize_t pn544_dev_read(struct file *filp, char __user *buf,
98                 size_t count, loff_t *offset)
99 {
100         struct pn544_dev *pn544_dev = filp->private_data;
101         char tmp[MAX_BUFFER_SIZE];
102         int ret;
103
104         if (count > MAX_BUFFER_SIZE)
105                 count = MAX_BUFFER_SIZE;
106
107         pr_debug("%s : reading %zu bytes. irq=%s\n", __func__, count,
108                  gpio_get_value_cansleep(pn544_dev->irq_gpio) ? "1" : "0");
109
110 #if NFC_DEBUG
111         pr_info("pn544 : + r\n");
112 #endif
113
114         mutex_lock(&pn544_dev->read_mutex);
115
116  /* wait_irq: */
117         if (!gpio_get_value_cansleep(pn544_dev->irq_gpio)) {
118                 if (filp->f_flags & O_NONBLOCK) {
119                         pr_info("%s : O_NONBLOCK\n", __func__);
120                         ret = -EAGAIN;
121                         goto fail;
122                 }
123
124                 pn544_dev->irq_enabled = true;
125 #ifdef READ_IRQ_MODIFY /* DY_TEST */
126                 do_reading = 0;
127 #endif
128                 enable_irq(pn544_dev->client->irq);
129
130                 if (gpio_get_value_cansleep(pn544_dev->irq_gpio)) {
131                         pn544_disable_irq(pn544_dev);
132                 } else {
133 #ifdef READ_IRQ_MODIFY
134                         ret = wait_event_interruptible(pn544_dev->read_wq,
135                                 do_reading);
136 #else
137                         ret = wait_event_interruptible(pn544_dev->read_wq,
138                                 gpio_get_value(pn544_dev->irq_gpio));
139 #endif
140                         /* gpio_get_value(pn544_dev->irq_gpio)); */
141                         /* pr_info("pn544 : h\n"); */
142                         pn544_disable_irq(pn544_dev);
143
144 #if NFC_DEBUG
145                         pr_info("pn544: h\n");
146 #endif
147 #ifdef READ_IRQ_MODIFY /* DY_TEST */
148                         if (cancle_read == true) {
149                                 cancle_read = false;
150                                 ret = -1;
151                                 goto fail;
152                         }
153 #endif
154
155                         if (ret)
156                                 goto fail;
157                 }
158         }
159         /* Read data */
160         ret = i2c_master_recv(pn544_dev->client, tmp, count);
161         mutex_unlock(&pn544_dev->read_mutex);
162
163         if (ret < 0) {
164                 pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
165                 return ret;
166         }
167         if (ret > count) {
168                 pr_err("%s: received too many bytes from i2c (%d)\n",
169                         __func__, ret);
170                 return -EIO;
171         }
172         if (copy_to_user(buf, tmp, ret)) {
173                 pr_err("%s : failed to copy to user space\n", __func__);
174                 return -EFAULT;
175         }
176
177         return ret;
178
179  fail:
180         mutex_unlock(&pn544_dev->read_mutex);
181         pr_err("%s : wait_event_interruptible fail\n", __func__);
182
183         return ret;
184 }
185
186 static ssize_t pn544_dev_write(struct file *filp, const char __user *buf,
187                 size_t count, loff_t *offset)
188 {
189         struct pn544_dev *pn544_dev;
190         char tmp[MAX_BUFFER_SIZE];
191         int ret;
192
193         pn544_dev = filp->private_data;
194
195 #if NFC_DEBUG
196         pr_info("pn544 : + w\n");
197 #endif
198
199         if (count > MAX_BUFFER_SIZE)
200                 count = MAX_BUFFER_SIZE;
201
202         if (copy_from_user(tmp, buf, count)) {
203                 pr_err("%s : failed to copy from user space\n", __func__);
204                 return -EFAULT;
205         }
206
207         pr_debug("%s : writing %zu bytes.\n", __func__, count);
208         /* Write data */
209         ret = i2c_master_send(pn544_dev->client, tmp, count);
210
211 #if NFC_DEBUG
212         pr_info("pn544 : - w\n");
213 #endif
214
215         if (ret != count) {
216                 pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
217                 ret = -EIO;
218         }
219
220         return ret;
221 }
222
223 static int pn544_dev_open(struct inode *inode, struct file *filp)
224 {
225 /*
226         struct pn544_dev *pn544_dev = container_of(filp->private_data,
227                                                 struct pn544_dev,
228                                                 pn544_device);
229 */
230         filp->private_data = pn544_dev_imsi;
231
232         pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));
233
234         return 0;
235 }
236
237 static long pn544_dev_ioctl(struct file *filp,
238                 unsigned int cmd, unsigned long arg)
239 {
240         struct pn544_dev *pn544_dev = filp->private_data;
241
242         switch (cmd) {
243         case PN544_SET_PWR:
244                 if (arg == 2) {
245                         /* power on with firmware download (requires hw reset)
246                          */
247                         pr_info("%s power on with firmware\n", __func__);
248                         gpio_set_value_cansleep(pn544_dev->ven_gpio, 1);
249                         gpio_set_value(pn544_dev->firm_gpio, 1);
250                         usleep_range(10000, 10000);
251                         gpio_set_value_cansleep(pn544_dev->ven_gpio, 0);
252                         usleep_range(10000, 10000);
253                         gpio_set_value_cansleep(pn544_dev->ven_gpio, 1);
254                         usleep_range(10000, 10000);
255                 } else if (arg == 1) {
256                         /* power on */
257                         pr_info("%s power on\n", __func__);
258                         gpio_set_value(pn544_dev->firm_gpio, 0);
259                         gpio_set_value_cansleep(pn544_dev->ven_gpio, 1);
260                         usleep_range(10000, 10000);
261                 } else if (arg == 0) {
262                         /* power off */
263                         pr_info("%s power off\n", __func__);
264                         /* printk("irq_gpio :%x ,firm_gpio %x\n",
265                            pn544_dev->irq_gpio, pn544_dev->firm_gpio); */
266                         gpio_set_value(pn544_dev->firm_gpio, 0);
267                         gpio_set_value_cansleep(pn544_dev->ven_gpio, 0);
268                         usleep_range(10000, 10000);
269 #ifdef READ_IRQ_MODIFY
270                 } else if (arg == 3) { /* DY_TEST */
271                         pr_info("%s Read Cancle\n", __func__);
272                         cancle_read = true;
273                         do_reading = 1;
274                         wake_up(&pn544_dev->read_wq);
275 #endif
276                 } else {
277                         /* pr_err("%s bad arg %l\n", __func__, arg); */
278                         return -EINVAL;
279                 }
280                 break;
281         default:
282                 pr_err("%s bad ioctl %u\n", __func__, cmd);
283                 return -EINVAL;
284         }
285
286         return 0;
287 }
288
289 static const struct file_operations pn544_dev_fops = {
290         .owner = THIS_MODULE,
291         .llseek = no_llseek,
292         .read = pn544_dev_read,
293         .write = pn544_dev_write,
294         .open = pn544_dev_open,
295         .unlocked_ioctl = pn544_dev_ioctl,
296 };
297
298 static int pn544_probe(struct i2c_client *client,
299                 const struct i2c_device_id *id)
300 {
301         int ret;
302         int irq_num;
303         struct pn544_i2c_platform_data *platform_data;
304         struct pn544_dev *pn544_dev;
305
306         platform_data = client->dev.platform_data;
307
308         if (platform_data == NULL) {
309                 pr_err("%s : nfc probe fail\n", __func__);
310                 return -ENODEV;
311         }
312
313         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
314                 pr_err("%s : need I2C_FUNC_I2C\n", __func__);
315                 return -ENODEV;
316         }
317
318         ret = gpio_request(platform_data->irq_gpio, "nfc_int");
319         if (ret)
320                 return -ENODEV;
321         ret = gpio_request(platform_data->ven_gpio, "nfc_ven");
322         if (ret)
323                 goto err_ven;
324         ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
325         if (ret)
326                 goto err_firm;
327
328         pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL);
329         if (pn544_dev == NULL) {
330                 dev_err(&client->dev,
331                         "failed to allocate memory for module data\n");
332                 ret = -ENOMEM;
333                 goto err_exit;
334         }
335
336         pr_info("%s : IRQ num %d\n", __func__, client->irq);
337
338         pn544_dev->irq_gpio = platform_data->irq_gpio;
339         pn544_dev->ven_gpio = platform_data->ven_gpio;
340         pn544_dev->firm_gpio = platform_data->firm_gpio;
341         pn544_dev->client = client;
342
343         /* printk("irq_gpio :%x ,firm_gpio %x\n",pn544_dev->irq_gpio,
344                 pn544_dev->firm_gpio); */
345
346         /* init mutex and queues */
347         init_waitqueue_head(&pn544_dev->read_wq);
348         mutex_init(&pn544_dev->read_mutex);
349         spin_lock_init(&pn544_dev->irq_enabled_lock);
350
351         pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR;
352         pn544_dev->pn544_device.name = "pn544";
353         pn544_dev->pn544_device.fops = &pn544_dev_fops;
354
355         ret = misc_register(&pn544_dev->pn544_device);
356         if (ret) {
357                 pr_err("%s : misc_register failed\n", __FILE__);
358                 goto err_misc_register;
359         }
360
361         /* request irq.  the irq is set whenever the chip has data available
362          * for reading.  it is cleared when all data has been read.
363          */
364         pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
365         pn544_dev->irq_enabled = true;
366         gpio_direction_input(pn544_dev->irq_gpio);
367         gpio_direction_output(pn544_dev->ven_gpio, 0);
368         gpio_direction_output(pn544_dev->firm_gpio, 0);
369         irq_num = gpio_to_irq(pn544_dev->irq_gpio);
370         client->irq = irq_num;
371
372         ret = request_threaded_irq(irq_num, NULL, pn544_dev_irq_handler,
373                         IRQF_TRIGGER_RISING, "pn544", pn544_dev);
374         if (ret) {
375                 dev_err(&client->dev, "request_irq failed\n");
376                 goto err_request_irq_failed;
377         }
378         pn544_disable_irq(pn544_dev);
379 #if defined(CONFIG_TARGET_LOCALE_EUR_U1_NFC)
380         enable_irq_wake(client->irq);
381 #endif
382         i2c_set_clientdata(client, pn544_dev);
383         pn544_dev_imsi = pn544_dev;
384         /* printk("pn544 prove success\n"); */
385
386         return 0;
387
388  err_request_irq_failed:
389         misc_deregister(&pn544_dev->pn544_device);
390  err_misc_register:
391         mutex_destroy(&pn544_dev->read_mutex);
392         kfree(pn544_dev);
393  err_exit:
394         gpio_free(platform_data->firm_gpio);
395  err_firm:
396         gpio_free(platform_data->ven_gpio);
397  err_ven:
398         gpio_free(platform_data->irq_gpio);
399         return ret;
400 }
401
402 static int pn544_remove(struct i2c_client *client)
403 {
404         struct pn544_dev *pn544_dev;
405
406         pn544_dev = i2c_get_clientdata(client);
407
408         free_irq(client->irq, pn544_dev);
409         misc_deregister(&pn544_dev->pn544_device);
410         mutex_destroy(&pn544_dev->read_mutex);
411         gpio_free(pn544_dev->irq_gpio);
412         gpio_free(pn544_dev->ven_gpio);
413         gpio_free(pn544_dev->firm_gpio);
414         kfree(pn544_dev);
415
416         return 0;
417 }
418
419 static void pn544_shutdown(struct i2c_client *client)
420 {
421         struct pn544_dev *pn544_dev;
422
423         pn544_dev = i2c_get_clientdata(client);
424         pr_info("%s\n", __func__);
425         gpio_set_value_cansleep(pn544_dev->ven_gpio, 0);
426
427         free_irq(client->irq, pn544_dev);
428         misc_deregister(&pn544_dev->pn544_device);
429         mutex_destroy(&pn544_dev->read_mutex);
430         gpio_free(pn544_dev->irq_gpio);
431         gpio_free(pn544_dev->ven_gpio);
432         gpio_free(pn544_dev->firm_gpio);
433         kfree(pn544_dev);
434
435 }
436
437 static const struct i2c_device_id pn544_id[] = {
438         {"pn544", 0},
439         {}
440 };
441
442 static struct i2c_driver pn544_driver = {
443         .id_table = pn544_id,
444         .probe = pn544_probe,
445         .remove = pn544_remove,
446         .shutdown = pn544_shutdown,
447         .driver = {
448                         .owner = THIS_MODULE,
449                         .name = "pn544",
450         },
451 };
452
453 /*
454  * module load/unload record keeping
455  */
456
457 static int __init pn544_dev_init(void)
458 {
459         pr_info("Loading pn544 driver\n");
460         return i2c_add_driver(&pn544_driver);
461 }
462
463 module_init(pn544_dev_init);
464
465 static void __exit pn544_dev_exit(void)
466 {
467         pr_info("Unloading pn544 driver\n");
468         i2c_del_driver(&pn544_driver);
469 }
470
471 module_exit(pn544_dev_exit);
472
473 MODULE_AUTHOR("Sylvain Fonteneau");
474 MODULE_DESCRIPTION("NFC PN544 driver");
475 MODULE_LICENSE("GPL");