blob: d719409eaef3d8c919a0c09913978af63bd1916c [file] [log] [blame]
wongab.jeona2d272e2012-07-18 21:49:59 +09001/*
2 * Copyright (C) 2012 Broadcom Corporation.
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/poll.h>
38#include <linux/version.h>
39
40#include <linux/nfc/bcm2079x.h>
41
42/* do not change below */
43#define MAX_BUFFER_SIZE 780
44
45/* Read data */
46#define PACKET_HEADER_SIZE_NCI (4)
47#define PACKET_HEADER_SIZE_HCI (3)
48#define PACKET_TYPE_NCI (16)
49#define PACKET_TYPE_HCIEV (4)
50#define MAX_PACKET_SIZE (PACKET_HEADER_SIZE_NCI + 255)
51
52struct bcm2079x_dev {
53 wait_queue_head_t read_wq;
54 struct mutex read_mutex;
55 struct i2c_client *client;
56 struct miscdevice bcm2079x_device;
57 unsigned int wake_gpio;
58 unsigned int en_gpio;
59 unsigned int irq_gpio;
60 bool irq_enabled;
61 spinlock_t irq_enabled_lock;
62 unsigned int count_irq;
63};
64
65static void bcm2079x_init_stat(struct bcm2079x_dev *bcm2079x_dev)
66{
67 bcm2079x_dev->count_irq = 0;
68}
69
70static void bcm2079x_disable_irq(struct bcm2079x_dev *bcm2079x_dev)
71{
72 unsigned long flags;
73 spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
74 if (bcm2079x_dev->irq_enabled) {
75 disable_irq_nosync(bcm2079x_dev->client->irq);
76 bcm2079x_dev->irq_enabled = false;
77 }
78 spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
79}
80
81static void bcm2079x_enable_irq(struct bcm2079x_dev *bcm2079x_dev)
82{
83 unsigned long flags;
84 spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
85 if (!bcm2079x_dev->irq_enabled) {
86 bcm2079x_dev->irq_enabled = true;
87 enable_irq(bcm2079x_dev->client->irq);
88 }
89 spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
90}
91
92/*
93 The alias address 0x79, when sent as a 7-bit address from the host processor
94 will match the first byte (highest 2 bits) of the default client address
95 (0x1FA) that is programmed in bcm20791.
96 When used together with the first byte (0xFA) of the byte sequence below,
97 it can be used to address the bcm20791 in a system that does not support
98 10-bit address and change the default address to 0x38.
99 the new address can be changed by changing the CLIENT_ADDRESS below if 0x38
100 conflicts with other device on the same i2c bus.
101 */
102#define ALIAS_ADDRESS 0x79
103
104static void change_client_addr(struct bcm2079x_dev *bcm2079x_dev, int addr)
105{
106 struct i2c_client *client;
107 int ret;
108 int i;
109 int offset = 0;
110 char addr_data[] = {
111 0xFA, 0xF2, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x2A
112 };
113
114 client = bcm2079x_dev->client;
115 client->addr = ALIAS_ADDRESS;
116 client->flags &= ~I2C_CLIENT_TEN;
117 offset = 0;
118
119 addr_data[5] = addr & 0xFF;
120 ret = 0;
121 for (i = 1; i < sizeof(addr_data) - 1; ++i)
122 ret += addr_data[i];
123 addr_data[sizeof(addr_data) - 1] = (ret & 0xFF);
124 dev_info(&client->dev,
125 "Change client device from (0x%04X) flag = "\
126 "%04x, addr_data[%d] = %02x\n",
127 client->addr, client->flags, sizeof(addr_data) - 1,
128 addr_data[sizeof(addr_data) - 1]);
129 ret = i2c_master_send(client, addr_data+offset, sizeof(addr_data)-offset);
130 if (ret != sizeof(addr_data)-offset) {
131 client->addr = ALIAS_ADDRESS;
132 client->flags &= ~I2C_CLIENT_TEN;
133 dev_info(&client->dev,
134 "Change client device from (0x%04X) flag = "\
135 "%04x, addr_data[%d] = %02x\n",
136 client->addr, client->flags, sizeof(addr_data) - 1,
137 addr_data[sizeof(addr_data) - 1]);
138 ret = i2c_master_send(client, addr_data, sizeof(addr_data));
139 }
140 client->addr = addr_data[5];
141
142 dev_info(&client->dev,
143 "Change client device changed to (0x%04X) flag = %04x, ret = %d\n",
144 client->addr, client->flags, ret);
145}
146
147static irqreturn_t bcm2079x_dev_irq_handler(int irq, void *dev_id)
148{
149 struct bcm2079x_dev *bcm2079x_dev = dev_id;
150 unsigned long flags;
151
152 spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
153 bcm2079x_dev->count_irq++;
154 spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
155 wake_up(&bcm2079x_dev->read_wq);
156
157 return IRQ_HANDLED;
158}
159
160static unsigned int bcm2079x_dev_poll(struct file *filp, poll_table *wait)
161{
162 struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
163 unsigned int mask = 0;
164 unsigned long flags;
165
166 poll_wait(filp, &bcm2079x_dev->read_wq, wait);
167
168 spin_lock_irqsave(&bcm2079x_dev->irq_enabled_lock, flags);
169 if (bcm2079x_dev->count_irq > 0)
170 {
171 bcm2079x_dev->count_irq--;
172 mask |= POLLIN | POLLRDNORM;
173 }
174 spin_unlock_irqrestore(&bcm2079x_dev->irq_enabled_lock, flags);
175
176 return mask;
177}
178
179static ssize_t bcm2079x_dev_read(struct file *filp, char __user *buf,
180 size_t count, loff_t *offset)
181{
182 struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
183 unsigned char tmp[MAX_BUFFER_SIZE];
184 int total, len, ret;
185
186 total = 0;
187 len = 0;
188
189 if (count > MAX_BUFFER_SIZE)
190 count = MAX_BUFFER_SIZE;
191
192 mutex_lock(&bcm2079x_dev->read_mutex);
193
194 /** Read the first 4 bytes to include the length of the NCI or HCI packet.
195 **/
196 ret = i2c_master_recv(bcm2079x_dev->client, tmp, 4);
197 if (ret == 4) {
198 total = ret;
199 /** First byte is the packet type
200 **/
201 switch(tmp[0]) {
202 case PACKET_TYPE_NCI:
203 len = tmp[PACKET_HEADER_SIZE_NCI-1];
204 break;
205
206 case PACKET_TYPE_HCIEV:
207 len = tmp[PACKET_HEADER_SIZE_HCI-1];
208 if (len == 0)
209 total--;/*Since payload is 0, decrement total size (from 4 to 3) */
210 else
211 len--;/*First byte of payload is in tmp[3] already */
212 break;
213
214 default:
215 len = 0;/*Unknown packet byte */
216 break;
217 } /* switch*/
218
219 /** make sure full packet fits in the buffer
220 **/
221 if (len > 0 && (len + total) <= count) {
222 /** read the remainder of the packet.
223 **/
224 ret = i2c_master_recv(bcm2079x_dev->client, tmp+total, len);
225 if (ret == len)
226 total += len;
227 } /* if */
228 } /* if */
229
230 mutex_unlock(&bcm2079x_dev->read_mutex);
231
232 if (total > count || copy_to_user(buf, tmp, total)) {
233 dev_err(&bcm2079x_dev->client->dev,
234 "failed to copy to user space, total = %d\n", total);
235 total = -EFAULT;
236 }
237
238 return total;
239}
240
241static ssize_t bcm2079x_dev_write(struct file *filp, const char __user *buf,
242 size_t count, loff_t *offset)
243{
244 struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
245 char tmp[MAX_BUFFER_SIZE];
246 int ret;
247
248 if (count > MAX_BUFFER_SIZE) {
249 dev_err(&bcm2079x_dev->client->dev, "out of memory\n");
250 return -ENOMEM;
251 }
252
253 if (copy_from_user(tmp, buf, count)) {
254 dev_err(&bcm2079x_dev->client->dev,
255 "failed to copy from user space\n");
256 return -EFAULT;
257 }
258
259 mutex_lock(&bcm2079x_dev->read_mutex);
260 /* Write data */
261
262 ret = i2c_master_send(bcm2079x_dev->client, tmp, count);
263 if (ret != count) {
264 dev_err(&bcm2079x_dev->client->dev,
265 "failed to write %d\n", ret);
266 ret = -EIO;
267 }
268 mutex_unlock(&bcm2079x_dev->read_mutex);
269
270 return ret;
271}
272
273static int bcm2079x_dev_open(struct inode *inode, struct file *filp)
274{
275 int ret = 0;
276
277 struct bcm2079x_dev *bcm2079x_dev = container_of(filp->private_data,
278 struct bcm2079x_dev,
279 bcm2079x_device);
280 filp->private_data = bcm2079x_dev;
281 bcm2079x_init_stat(bcm2079x_dev);
282 bcm2079x_enable_irq(bcm2079x_dev);
283 dev_info(&bcm2079x_dev->client->dev,
284 "%d,%d\n", imajor(inode), iminor(inode));
285
286 return ret;
287}
288
289static long bcm2079x_dev_unlocked_ioctl(struct file *filp,
290 unsigned int cmd, unsigned long arg)
291{
292 struct bcm2079x_dev *bcm2079x_dev = filp->private_data;
293
294 switch (cmd) {
295 case BCMNFC_READ_FULL_PACKET:
296 break;
297 case BCMNFC_READ_MULTI_PACKETS:
298 break;
299 case BCMNFC_CHANGE_ADDR:
300 dev_info(&bcm2079x_dev->client->dev,
301 "%s, BCMNFC_CHANGE_ADDR (%x, %lx):\n", __func__, cmd,
302 arg);
303 change_client_addr(bcm2079x_dev, arg);
304 break;
305 case BCMNFC_POWER_CTL:
306 gpio_set_value(bcm2079x_dev->en_gpio, arg);
307 break;
308 case BCMNFC_WAKE_CTL:
309 gpio_set_value(bcm2079x_dev->wake_gpio, arg);
310 break;
311 default:
312 dev_err(&bcm2079x_dev->client->dev,
313 "%s, unknown cmd (%x, %lx)\n", __func__, cmd, arg);
314 return 0;
315 }
316
317 return 0;
318}
319
320static const struct file_operations bcm2079x_dev_fops = {
321 .owner = THIS_MODULE,
322 .llseek = no_llseek,
323 .poll = bcm2079x_dev_poll,
324 .read = bcm2079x_dev_read,
325 .write = bcm2079x_dev_write,
326 .open = bcm2079x_dev_open,
327 .unlocked_ioctl = bcm2079x_dev_unlocked_ioctl
328};
329
330static int bcm2079x_probe(struct i2c_client *client,
331 const struct i2c_device_id *id)
332{
333 int ret;
334 struct bcm2079x_platform_data *platform_data;
335 struct bcm2079x_dev *bcm2079x_dev;
336
337 platform_data = client->dev.platform_data;
338
339 dev_info(&client->dev, "%s, probing bcm2079x driver flags = %x\n", __func__, client->flags);
340 if (platform_data == NULL) {
341 dev_err(&client->dev, "nfc probe fail\n");
342 return -ENODEV;
343 }
344
345 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
346 dev_err(&client->dev, "need I2C_FUNC_I2C\n");
347 return -ENODEV;
348 }
349
350 ret = gpio_request_one(platform_data->irq_gpio, GPIOF_IN, "nfc_int");
351 if (ret)
352 return -ENODEV;
353 ret = gpio_request_one(platform_data->en_gpio, GPIOF_OUT_INIT_LOW, "nfc_ven");
354 if (ret)
355 goto err_en;
356 ret = gpio_request_one(platform_data->wake_gpio, GPIOF_OUT_INIT_LOW,"nfc_firm");
357 if (ret)
358 goto err_firm;
359
360 gpio_set_value(platform_data->en_gpio, 0);
361 gpio_set_value(platform_data->wake_gpio, 0);
362
363 bcm2079x_dev = kzalloc(sizeof(*bcm2079x_dev), GFP_KERNEL);
364 if (bcm2079x_dev == NULL) {
365 dev_err(&client->dev,
366 "failed to allocate memory for module data\n");
367 ret = -ENOMEM;
368 goto err_exit;
369 }
370
371 bcm2079x_dev->wake_gpio = platform_data->wake_gpio;
372 bcm2079x_dev->irq_gpio = platform_data->irq_gpio;
373 bcm2079x_dev->en_gpio = platform_data->en_gpio;
374 bcm2079x_dev->client = client;
375
376 /* init mutex and queues */
377 init_waitqueue_head(&bcm2079x_dev->read_wq);
378 mutex_init(&bcm2079x_dev->read_mutex);
379 spin_lock_init(&bcm2079x_dev->irq_enabled_lock);
380
381 bcm2079x_dev->bcm2079x_device.minor = MISC_DYNAMIC_MINOR;
382 bcm2079x_dev->bcm2079x_device.name = "bcm2079x-i2c";
383 bcm2079x_dev->bcm2079x_device.fops = &bcm2079x_dev_fops;
384
385 ret = misc_register(&bcm2079x_dev->bcm2079x_device);
386 if (ret) {
387 dev_err(&client->dev, "misc_register failed\n");
388 goto err_misc_register;
389 }
390
391 /* request irq. the irq is set whenever the chip has data available
392 * for reading. it is cleared when all data has been read.
393 */
394 dev_info(&client->dev, "requesting IRQ %d\n", client->irq);
395 bcm2079x_dev->irq_enabled = true;
396 ret = request_irq(client->irq, bcm2079x_dev_irq_handler,
397 IRQF_TRIGGER_RISING, client->name, bcm2079x_dev);
398 if (ret) {
399 dev_err(&client->dev, "request_irq failed\n");
400 goto err_request_irq_failed;
401 }
402 bcm2079x_disable_irq(bcm2079x_dev);
403 i2c_set_clientdata(client, bcm2079x_dev);
404 dev_info(&client->dev,
405 "%s, probing bcm2079x driver exited successfully\n",
406 __func__);
407 return 0;
408
409err_request_irq_failed:
410 misc_deregister(&bcm2079x_dev->bcm2079x_device);
411err_misc_register:
412 mutex_destroy(&bcm2079x_dev->read_mutex);
413 kfree(bcm2079x_dev);
414err_exit:
415 gpio_free(platform_data->wake_gpio);
416err_firm:
417 gpio_free(platform_data->en_gpio);
418err_en:
419 gpio_free(platform_data->irq_gpio);
420 return ret;
421}
422
423static int bcm2079x_remove(struct i2c_client *client)
424{
425 struct bcm2079x_dev *bcm2079x_dev;
426
427 bcm2079x_dev = i2c_get_clientdata(client);
428 free_irq(client->irq, bcm2079x_dev);
429 misc_deregister(&bcm2079x_dev->bcm2079x_device);
430 mutex_destroy(&bcm2079x_dev->read_mutex);
431 gpio_free(bcm2079x_dev->irq_gpio);
432 gpio_free(bcm2079x_dev->en_gpio);
433 gpio_free(bcm2079x_dev->wake_gpio);
434 kfree(bcm2079x_dev);
435
436 return 0;
437}
438
439static const struct i2c_device_id bcm2079x_id[] = {
440 {"bcm2079x-i2c", 0},
441 {}
442};
443
444static struct i2c_driver bcm2079x_driver = {
445 .id_table = bcm2079x_id,
446 .probe = bcm2079x_probe,
447 .remove = bcm2079x_remove,
448 .driver = {
449 .owner = THIS_MODULE,
450 .name = "bcm2079x-i2c",
451 },
452};
453
454/*
455 * module load/unload record keeping
456 */
457
458static int __init bcm2079x_dev_init(void)
459{
460 return i2c_add_driver(&bcm2079x_driver);
461}
462module_init(bcm2079x_dev_init);
463
464static void __exit bcm2079x_dev_exit(void)
465{
466 i2c_del_driver(&bcm2079x_driver);
467}
468module_exit(bcm2079x_dev_exit);
469
470MODULE_AUTHOR("Broadcom");
471MODULE_DESCRIPTION("NFC bcm2079x driver");
472MODULE_LICENSE("GPL");