diff options
Diffstat (limited to 'drivers/nfc/pn544.c')
-rw-r--r-- | drivers/nfc/pn544.c | 400 |
1 files changed, 400 insertions, 0 deletions
diff --git a/drivers/nfc/pn544.c b/drivers/nfc/pn544.c new file mode 100644 index 00000000000..91216e465e1 --- /dev/null +++ b/drivers/nfc/pn544.c | |||
@@ -0,0 +1,400 @@ | |||
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/nfc/pn544.h> | ||
38 | |||
39 | #define MAX_BUFFER_SIZE 512 | ||
40 | |||
41 | struct pn544_dev { | ||
42 | wait_queue_head_t read_wq; | ||
43 | struct mutex read_mutex; | ||
44 | struct i2c_client *client; | ||
45 | struct miscdevice pn544_device; | ||
46 | unsigned int ven_gpio; | ||
47 | unsigned int firm_gpio; | ||
48 | unsigned int irq_gpio; | ||
49 | bool irq_enabled; | ||
50 | spinlock_t irq_enabled_lock; | ||
51 | }; | ||
52 | |||
53 | static void pn544_disable_irq(struct pn544_dev *pn544_dev) | ||
54 | { | ||
55 | unsigned long flags; | ||
56 | |||
57 | spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags); | ||
58 | if (pn544_dev->irq_enabled) { | ||
59 | disable_irq_nosync(pn544_dev->client->irq); | ||
60 | pn544_dev->irq_enabled = false; | ||
61 | } | ||
62 | spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags); | ||
63 | } | ||
64 | |||
65 | static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id) | ||
66 | { | ||
67 | struct pn544_dev *pn544_dev = dev_id; | ||
68 | |||
69 | pn544_disable_irq(pn544_dev); | ||
70 | |||
71 | /* Wake up waiting readers */ | ||
72 | wake_up(&pn544_dev->read_wq); | ||
73 | |||
74 | return IRQ_HANDLED; | ||
75 | } | ||
76 | |||
77 | static ssize_t pn544_dev_read(struct file *filp, char __user *buf, | ||
78 | size_t count, loff_t *offset) | ||
79 | { | ||
80 | struct pn544_dev *pn544_dev = filp->private_data; | ||
81 | char tmp[MAX_BUFFER_SIZE]; | ||
82 | int ret; | ||
83 | |||
84 | if (count > MAX_BUFFER_SIZE) | ||
85 | count = MAX_BUFFER_SIZE; | ||
86 | |||
87 | pr_debug("%s : reading %zu bytes.\n", __func__, count); | ||
88 | |||
89 | mutex_lock(&pn544_dev->read_mutex); | ||
90 | |||
91 | if (!gpio_get_value(pn544_dev->irq_gpio)) { | ||
92 | if (filp->f_flags & O_NONBLOCK) { | ||
93 | ret = -EAGAIN; | ||
94 | goto fail; | ||
95 | } | ||
96 | |||
97 | pn544_dev->irq_enabled = true; | ||
98 | enable_irq(pn544_dev->client->irq); | ||
99 | ret = wait_event_interruptible(pn544_dev->read_wq, | ||
100 | gpio_get_value(pn544_dev->irq_gpio)); | ||
101 | |||
102 | pn544_disable_irq(pn544_dev); | ||
103 | |||
104 | if (ret) | ||
105 | goto fail; | ||
106 | |||
107 | } | ||
108 | |||
109 | /* Read data */ | ||
110 | ret = i2c_master_recv(pn544_dev->client, tmp, count); | ||
111 | |||
112 | mutex_unlock(&pn544_dev->read_mutex); | ||
113 | |||
114 | /* pn544 seems to be slow in handling I2C read requests | ||
115 | * so add 1ms delay after recv operation */ | ||
116 | udelay(1000); | ||
117 | |||
118 | if (ret < 0) { | ||
119 | pr_err("%s: i2c_master_recv returned %d\n", __func__, ret); | ||
120 | return ret; | ||
121 | } | ||
122 | if (ret > count) { | ||
123 | pr_err("%s: received too many bytes from i2c (%d)\n", | ||
124 | __func__, ret); | ||
125 | return -EIO; | ||
126 | } | ||
127 | if (copy_to_user(buf, tmp, ret)) { | ||
128 | pr_warning("%s : failed to copy to user space\n", __func__); | ||
129 | return -EFAULT; | ||
130 | } | ||
131 | return ret; | ||
132 | |||
133 | fail: | ||
134 | mutex_unlock(&pn544_dev->read_mutex); | ||
135 | return ret; | ||
136 | } | ||
137 | |||
138 | static ssize_t pn544_dev_write(struct file *filp, const char __user *buf, | ||
139 | size_t count, loff_t *offset) | ||
140 | { | ||
141 | struct pn544_dev *pn544_dev; | ||
142 | char tmp[MAX_BUFFER_SIZE]; | ||
143 | int ret; | ||
144 | |||
145 | pn544_dev = filp->private_data; | ||
146 | |||
147 | if (count > MAX_BUFFER_SIZE) | ||
148 | count = MAX_BUFFER_SIZE; | ||
149 | |||
150 | if (copy_from_user(tmp, buf, count)) { | ||
151 | pr_err("%s : failed to copy from user space\n", __func__); | ||
152 | return -EFAULT; | ||
153 | } | ||
154 | |||
155 | pr_debug("%s : writing %zu bytes.\n", __func__, count); | ||
156 | /* Write data */ | ||
157 | ret = i2c_master_send(pn544_dev->client, tmp, count); | ||
158 | if (ret != count) { | ||
159 | pr_err("%s : i2c_master_send returned %d\n", __func__, ret); | ||
160 | ret = -EIO; | ||
161 | } | ||
162 | |||
163 | /* pn544 seems to be slow in handling I2C write requests | ||
164 | * so add 1ms delay after I2C send oparation */ | ||
165 | udelay(1000); | ||
166 | |||
167 | return ret; | ||
168 | } | ||
169 | |||
170 | static int pn544_dev_open(struct inode *inode, struct file *filp) | ||
171 | { | ||
172 | struct pn544_dev *pn544_dev = container_of(filp->private_data, | ||
173 | struct pn544_dev, | ||
174 | pn544_device); | ||
175 | |||
176 | filp->private_data = pn544_dev; | ||
177 | |||
178 | pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode)); | ||
179 | |||
180 | return 0; | ||
181 | } | ||
182 | |||
183 | static long pn544_dev_ioctl(struct file *filp, unsigned int cmd, | ||
184 | unsigned long arg) | ||
185 | { | ||
186 | struct pn544_dev *pn544_dev = filp->private_data; | ||
187 | |||
188 | switch (cmd) { | ||
189 | case PN544_SET_PWR: | ||
190 | if (arg == 2) { | ||
191 | /* power on with firmware download (requires hw reset) | ||
192 | */ | ||
193 | pr_info("%s power on with firmware\n", __func__); | ||
194 | gpio_set_value(pn544_dev->ven_gpio, 1); | ||
195 | msleep(20); | ||
196 | if (pn544_dev->firm_gpio) | ||
197 | gpio_set_value(pn544_dev->firm_gpio, 1); | ||
198 | msleep(20); | ||
199 | gpio_set_value(pn544_dev->ven_gpio, 0); | ||
200 | msleep(100); | ||
201 | gpio_set_value(pn544_dev->ven_gpio, 1); | ||
202 | msleep(20); | ||
203 | } else if (arg == 1) { | ||
204 | /* power on */ | ||
205 | pr_info("%s power on\n", __func__); | ||
206 | if (pn544_dev->firm_gpio) | ||
207 | gpio_set_value(pn544_dev->firm_gpio, 0); | ||
208 | gpio_set_value(pn544_dev->ven_gpio, 1); | ||
209 | msleep(100); | ||
210 | } else if (arg == 0) { | ||
211 | /* power off */ | ||
212 | pr_info("%s power off\n", __func__); | ||
213 | if (pn544_dev->firm_gpio) | ||
214 | gpio_set_value(pn544_dev->firm_gpio, 0); | ||
215 | gpio_set_value(pn544_dev->ven_gpio, 0); | ||
216 | msleep(100); | ||
217 | } else { | ||
218 | pr_err("%s bad arg %lu\n", __func__, arg); | ||
219 | return -EINVAL; | ||
220 | } | ||
221 | break; | ||
222 | default: | ||
223 | pr_err("%s bad ioctl %u\n", __func__, cmd); | ||
224 | return -EINVAL; | ||
225 | } | ||
226 | |||
227 | return 0; | ||
228 | } | ||
229 | |||
230 | static const struct file_operations pn544_dev_fops = { | ||
231 | .owner = THIS_MODULE, | ||
232 | .llseek = no_llseek, | ||
233 | .read = pn544_dev_read, | ||
234 | .write = pn544_dev_write, | ||
235 | .open = pn544_dev_open, | ||
236 | .unlocked_ioctl = pn544_dev_ioctl, | ||
237 | }; | ||
238 | |||
239 | static int pn544_probe(struct i2c_client *client, | ||
240 | const struct i2c_device_id *id) | ||
241 | { | ||
242 | int ret; | ||
243 | struct pn544_i2c_platform_data *platform_data; | ||
244 | struct pn544_dev *pn544_dev; | ||
245 | |||
246 | platform_data = client->dev.platform_data; | ||
247 | |||
248 | if (platform_data == NULL) { | ||
249 | pr_err("%s : nfc probe fail\n", __func__); | ||
250 | return -ENODEV; | ||
251 | } | ||
252 | |||
253 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
254 | pr_err("%s : need I2C_FUNC_I2C\n", __func__); | ||
255 | return -ENODEV; | ||
256 | } | ||
257 | |||
258 | ret = gpio_request(platform_data->irq_gpio, "nfc_int"); | ||
259 | if (ret) | ||
260 | return -ENODEV; | ||
261 | ret = gpio_request(platform_data->ven_gpio, "nfc_ven"); | ||
262 | if (ret) | ||
263 | goto err_ven; | ||
264 | if (platform_data->firm_gpio) { | ||
265 | ret = gpio_request(platform_data->firm_gpio, "nfc_firm"); | ||
266 | if (ret) | ||
267 | goto err_firm; | ||
268 | } | ||
269 | |||
270 | pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL); | ||
271 | if (pn544_dev == NULL) { | ||
272 | dev_err(&client->dev, | ||
273 | "failed to allocate memory for module data\n"); | ||
274 | ret = -ENOMEM; | ||
275 | goto err_exit; | ||
276 | } | ||
277 | |||
278 | pn544_dev->irq_gpio = platform_data->irq_gpio; | ||
279 | pn544_dev->ven_gpio = platform_data->ven_gpio; | ||
280 | pn544_dev->firm_gpio = platform_data->firm_gpio; | ||
281 | pn544_dev->client = client; | ||
282 | |||
283 | ret = gpio_direction_input(pn544_dev->irq_gpio); | ||
284 | if (ret < 0) { | ||
285 | pr_err("%s :not able to set irq_gpio as input\n", __func__); | ||
286 | goto err_ven; | ||
287 | } | ||
288 | ret = gpio_direction_output(pn544_dev->ven_gpio, 0); | ||
289 | if (ret < 0) { | ||
290 | pr_err("%s : not able to set ven_gpio as output\n", __func__); | ||
291 | goto err_firm; | ||
292 | } | ||
293 | if (platform_data->firm_gpio) { | ||
294 | ret = gpio_direction_output(pn544_dev->firm_gpio, 0); | ||
295 | if (ret < 0) { | ||
296 | pr_err("%s : not able to set firm_gpio as output\n", | ||
297 | __func__); | ||
298 | goto err_exit; | ||
299 | } | ||
300 | } | ||
301 | |||
302 | /* init mutex and queues */ | ||
303 | init_waitqueue_head(&pn544_dev->read_wq); | ||
304 | mutex_init(&pn544_dev->read_mutex); | ||
305 | spin_lock_init(&pn544_dev->irq_enabled_lock); | ||
306 | |||
307 | pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR; | ||
308 | pn544_dev->pn544_device.name = "pn544"; | ||
309 | pn544_dev->pn544_device.fops = &pn544_dev_fops; | ||
310 | |||
311 | ret = misc_register(&pn544_dev->pn544_device); | ||
312 | if (ret) { | ||
313 | pr_err("%s : misc_register failed\n", __FILE__); | ||
314 | goto err_misc_register; | ||
315 | } | ||
316 | |||
317 | /* request irq. the irq is set whenever the chip has data available | ||
318 | * for reading. it is cleared when all data has been read. | ||
319 | */ | ||
320 | pr_info("%s : requesting IRQ %d\n", __func__, client->irq); | ||
321 | pn544_dev->irq_enabled = true; | ||
322 | ret = request_irq(client->irq, pn544_dev_irq_handler, | ||
323 | IRQF_TRIGGER_HIGH, client->name, pn544_dev); | ||
324 | if (ret) { | ||
325 | dev_err(&client->dev, "request_irq failed\n"); | ||
326 | goto err_request_irq_failed; | ||
327 | } | ||
328 | pn544_disable_irq(pn544_dev); | ||
329 | i2c_set_clientdata(client, pn544_dev); | ||
330 | |||
331 | return 0; | ||
332 | |||
333 | err_request_irq_failed: | ||
334 | misc_deregister(&pn544_dev->pn544_device); | ||
335 | err_misc_register: | ||
336 | mutex_destroy(&pn544_dev->read_mutex); | ||
337 | kfree(pn544_dev); | ||
338 | err_exit: | ||
339 | if (pn544_dev->firm_gpio) | ||
340 | gpio_free(platform_data->firm_gpio); | ||
341 | err_firm: | ||
342 | gpio_free(platform_data->ven_gpio); | ||
343 | err_ven: | ||
344 | gpio_free(platform_data->irq_gpio); | ||
345 | return ret; | ||
346 | } | ||
347 | |||
348 | static int pn544_remove(struct i2c_client *client) | ||
349 | { | ||
350 | struct pn544_dev *pn544_dev; | ||
351 | |||
352 | pn544_dev = i2c_get_clientdata(client); | ||
353 | free_irq(client->irq, pn544_dev); | ||
354 | misc_deregister(&pn544_dev->pn544_device); | ||
355 | mutex_destroy(&pn544_dev->read_mutex); | ||
356 | gpio_free(pn544_dev->irq_gpio); | ||
357 | gpio_free(pn544_dev->ven_gpio); | ||
358 | if (pn544_dev->firm_gpio) | ||
359 | gpio_free(pn544_dev->firm_gpio); | ||
360 | kfree(pn544_dev); | ||
361 | |||
362 | return 0; | ||
363 | } | ||
364 | |||
365 | static const struct i2c_device_id pn544_id[] = { | ||
366 | { "pn544", 0 }, | ||
367 | { } | ||
368 | }; | ||
369 | |||
370 | static struct i2c_driver pn544_driver = { | ||
371 | .id_table = pn544_id, | ||
372 | .probe = pn544_probe, | ||
373 | .remove = pn544_remove, | ||
374 | .driver = { | ||
375 | .owner = THIS_MODULE, | ||
376 | .name = "pn544", | ||
377 | }, | ||
378 | }; | ||
379 | |||
380 | /* | ||
381 | * module load/unload record keeping | ||
382 | */ | ||
383 | |||
384 | static int __init pn544_dev_init(void) | ||
385 | { | ||
386 | pr_info("Loading pn544 driver\n"); | ||
387 | return i2c_add_driver(&pn544_driver); | ||
388 | } | ||
389 | module_init(pn544_dev_init); | ||
390 | |||
391 | static void __exit pn544_dev_exit(void) | ||
392 | { | ||
393 | pr_info("Unloading pn544 driver\n"); | ||
394 | i2c_del_driver(&pn544_driver); | ||
395 | } | ||
396 | module_exit(pn544_dev_exit); | ||
397 | |||
398 | MODULE_AUTHOR("Sylvain Fonteneau"); | ||
399 | MODULE_DESCRIPTION("NFC PN544 driver"); | ||
400 | MODULE_LICENSE("GPL"); | ||