xref: /openbmc/linux/drivers/misc/ti-st/st_kim.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
1 /*
2  *  Shared Transport Line discipline driver Core
3  *	Init Manager module responsible for GPIO control
4  *	and firmware download
5  *  Copyright (C) 2009-2010 Texas Instruments
6  *  Author: Pavan Savoy <pavan_savoy@ti.com>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22 
23 #define pr_fmt(fmt) "(stk) :" fmt
24 #include <linux/platform_device.h>
25 #include <linux/jiffies.h>
26 #include <linux/firmware.h>
27 #include <linux/delay.h>
28 #include <linux/wait.h>
29 #include <linux/gpio.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #include <linux/sched.h>
33 #include <linux/rfkill.h>
34 
35 /* understand BT events for fw response */
36 #include <net/bluetooth/bluetooth.h>
37 #include <net/bluetooth/hci_core.h>
38 #include <net/bluetooth/hci.h>
39 
40 #include <linux/ti_wilink_st.h>
41 
42 
43 static int kim_probe(struct platform_device *pdev);
44 static int kim_remove(struct platform_device *pdev);
45 
46 /* KIM platform device driver structure */
47 static struct platform_driver kim_platform_driver = {
48 	.probe = kim_probe,
49 	.remove = kim_remove,
50 	/* TODO: ST driver power management during suspend/resume ?
51 	 */
52 #if 0
53 	.suspend = kim_suspend,
54 	.resume = kim_resume,
55 #endif
56 	.driver = {
57 		   .name = "kim",
58 		   .owner = THIS_MODULE,
59 		   },
60 };
61 
62 static int kim_toggle_radio(void*, bool);
63 static const struct rfkill_ops kim_rfkill_ops = {
64 	.set_block = kim_toggle_radio,
65 };
66 
67 /* strings to be used for rfkill entries and by
68  * ST Core to be used for sysfs debug entry
69  */
70 #define PROTO_ENTRY(type, name)	name
71 const unsigned char *protocol_names[] = {
72 	PROTO_ENTRY(ST_BT, "Bluetooth"),
73 	PROTO_ENTRY(ST_FM, "FM"),
74 	PROTO_ENTRY(ST_GPS, "GPS"),
75 };
76 
77 #define MAX_ST_DEVICES	3	/* Imagine 1 on each UART for now */
78 static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
79 
80 /**********************************************************************/
81 /* internal functions */
82 
83 /**
84  * st_get_plat_device -
85  *	function which returns the reference to the platform device
86  *	requested by id. As of now only 1 such device exists (id=0)
87  *	the context requesting for reference can get the id to be
88  *	requested by a. The protocol driver which is registering or
89  *	b. the tty device which is opened.
90  */
91 static struct platform_device *st_get_plat_device(int id)
92 {
93 	return st_kim_devices[id];
94 }
95 
96 /**
97  * validate_firmware_response -
98  *	function to return whether the firmware response was proper
99  *	in case of error don't complete so that waiting for proper
100  *	response times out
101  */
102 void validate_firmware_response(struct kim_data_s *kim_gdata)
103 {
104 	struct sk_buff *skb = kim_gdata->rx_skb;
105 	if (unlikely(skb->data[5] != 0)) {
106 		pr_err("no proper response during fw download");
107 		pr_err("data6 %x", skb->data[5]);
108 		return;		/* keep waiting for the proper response */
109 	}
110 	/* becos of all the script being downloaded */
111 	complete_all(&kim_gdata->kim_rcvd);
112 	kfree_skb(skb);
113 }
114 
115 /* check for data len received inside kim_int_recv
116  * most often hit the last case to update state to waiting for data
117  */
118 static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
119 {
120 	register int room = skb_tailroom(kim_gdata->rx_skb);
121 
122 	pr_debug("len %d room %d", len, room);
123 
124 	if (!len) {
125 		validate_firmware_response(kim_gdata);
126 	} else if (len > room) {
127 		/* Received packet's payload length is larger.
128 		 * We can't accommodate it in created skb.
129 		 */
130 		pr_err("Data length is too large len %d room %d", len,
131 			   room);
132 		kfree_skb(kim_gdata->rx_skb);
133 	} else {
134 		/* Packet header has non-zero payload length and
135 		 * we have enough space in created skb. Lets read
136 		 * payload data */
137 		kim_gdata->rx_state = ST_BT_W4_DATA;
138 		kim_gdata->rx_count = len;
139 		return len;
140 	}
141 
142 	/* Change ST LL state to continue to process next
143 	 * packet */
144 	kim_gdata->rx_state = ST_W4_PACKET_TYPE;
145 	kim_gdata->rx_skb = NULL;
146 	kim_gdata->rx_count = 0;
147 
148 	return 0;
149 }
150 
151 /**
152  * kim_int_recv - receive function called during firmware download
153  *	firmware download responses on different UART drivers
154  *	have been observed to come in bursts of different
155  *	tty_receive and hence the logic
156  */
157 void kim_int_recv(struct kim_data_s *kim_gdata,
158 	const unsigned char *data, long count)
159 {
160 	const unsigned char *ptr;
161 	struct hci_event_hdr *eh;
162 	int len = 0, type = 0;
163 
164 	pr_debug("%s", __func__);
165 	/* Decode received bytes here */
166 	ptr = data;
167 	if (unlikely(ptr == NULL)) {
168 		pr_err(" received null from TTY ");
169 		return;
170 	}
171 
172 	while (count) {
173 		if (kim_gdata->rx_count) {
174 			len = min_t(unsigned int, kim_gdata->rx_count, count);
175 			memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
176 			kim_gdata->rx_count -= len;
177 			count -= len;
178 			ptr += len;
179 
180 			if (kim_gdata->rx_count)
181 				continue;
182 
183 			/* Check ST RX state machine , where are we? */
184 			switch (kim_gdata->rx_state) {
185 				/* Waiting for complete packet ? */
186 			case ST_BT_W4_DATA:
187 				pr_debug("Complete pkt received");
188 				validate_firmware_response(kim_gdata);
189 				kim_gdata->rx_state = ST_W4_PACKET_TYPE;
190 				kim_gdata->rx_skb = NULL;
191 				continue;
192 				/* Waiting for Bluetooth event header ? */
193 			case ST_BT_W4_EVENT_HDR:
194 				eh = (struct hci_event_hdr *)kim_gdata->
195 				    rx_skb->data;
196 				pr_debug("Event header: evt 0x%2.2x"
197 					   "plen %d", eh->evt, eh->plen);
198 				kim_check_data_len(kim_gdata, eh->plen);
199 				continue;
200 			}	/* end of switch */
201 		}		/* end of if rx_state */
202 		switch (*ptr) {
203 			/* Bluetooth event packet? */
204 		case HCI_EVENT_PKT:
205 			pr_info("Event packet");
206 			kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
207 			kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
208 			type = HCI_EVENT_PKT;
209 			break;
210 		default:
211 			pr_info("unknown packet");
212 			ptr++;
213 			count--;
214 			continue;
215 		}
216 		ptr++;
217 		count--;
218 		kim_gdata->rx_skb =
219 		    bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
220 		if (!kim_gdata->rx_skb) {
221 			pr_err("can't allocate mem for new packet");
222 			kim_gdata->rx_state = ST_W4_PACKET_TYPE;
223 			kim_gdata->rx_count = 0;
224 			return;
225 		}
226 		bt_cb(kim_gdata->rx_skb)->pkt_type = type;
227 	}
228 	pr_info("done %s", __func__);
229 	return;
230 }
231 
232 static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
233 {
234 	unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
235 	const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
236 
237 	pr_debug("%s", __func__);
238 
239 	INIT_COMPLETION(kim_gdata->kim_rcvd);
240 	if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
241 		pr_err("kim: couldn't write 4 bytes");
242 		return -1;
243 	}
244 
245 	if (!wait_for_completion_timeout
246 	    (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
247 		pr_err(" waiting for ver info- timed out ");
248 		return -1;
249 	}
250 
251 	version =
252 		MAKEWORD(kim_gdata->resp_buffer[13],
253 				kim_gdata->resp_buffer[14]);
254 	chip = (version & 0x7C00) >> 10;
255 	min_ver = (version & 0x007F);
256 	maj_ver = (version & 0x0380) >> 7;
257 
258 	if (version & 0x8000)
259 		maj_ver |= 0x0008;
260 
261 	sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
262 
263 	/* to be accessed later via sysfs entry */
264 	kim_gdata->version.full = version;
265 	kim_gdata->version.chip = chip;
266 	kim_gdata->version.maj_ver = maj_ver;
267 	kim_gdata->version.min_ver = min_ver;
268 
269 	pr_info("%s", bts_scr_name);
270 	return 0;
271 }
272 
273 /**
274  * download_firmware -
275  *	internal function which parses through the .bts firmware
276  *	script file intreprets SEND, DELAY actions only as of now
277  */
278 static long download_firmware(struct kim_data_s *kim_gdata)
279 {
280 	long err = 0;
281 	long len = 0;
282 	unsigned char *ptr = NULL;
283 	unsigned char *action_ptr = NULL;
284 	unsigned char bts_scr_name[30] = { 0 };	/* 30 char long bts scr name? */
285 
286 	err = read_local_version(kim_gdata, bts_scr_name);
287 	if (err != 0) {
288 		pr_err("kim: failed to read local ver");
289 		return err;
290 	}
291 	err =
292 	    request_firmware(&kim_gdata->fw_entry, bts_scr_name,
293 			     &kim_gdata->kim_pdev->dev);
294 	if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
295 		     (kim_gdata->fw_entry->size == 0))) {
296 		pr_err(" request_firmware failed(errno %ld) for %s", err,
297 			   bts_scr_name);
298 		return -1;
299 	}
300 	ptr = (void *)kim_gdata->fw_entry->data;
301 	len = kim_gdata->fw_entry->size;
302 	/* bts_header to remove out magic number and
303 	 * version
304 	 */
305 	ptr += sizeof(struct bts_header);
306 	len -= sizeof(struct bts_header);
307 
308 	while (len > 0 && ptr) {
309 		pr_debug(" action size %d, type %d ",
310 			   ((struct bts_action *)ptr)->size,
311 			   ((struct bts_action *)ptr)->type);
312 
313 		switch (((struct bts_action *)ptr)->type) {
314 		case ACTION_SEND_COMMAND:	/* action send */
315 			action_ptr = &(((struct bts_action *)ptr)->data[0]);
316 			if (unlikely
317 			    (((struct hci_command *)action_ptr)->opcode ==
318 			     0xFF36)) {
319 				/* ignore remote change
320 				 * baud rate HCI VS command */
321 				pr_err
322 				    (" change remote baud"
323 				    " rate command in firmware");
324 				break;
325 			}
326 
327 			INIT_COMPLETION(kim_gdata->kim_rcvd);
328 			err = st_int_write(kim_gdata->core_data,
329 			((struct bts_action_send *)action_ptr)->data,
330 					   ((struct bts_action *)ptr)->size);
331 			if (unlikely(err < 0)) {
332 				release_firmware(kim_gdata->fw_entry);
333 				return -1;
334 			}
335 			if (!wait_for_completion_timeout
336 			    (&kim_gdata->kim_rcvd,
337 			     msecs_to_jiffies(CMD_RESP_TIME))) {
338 				pr_err
339 				    (" response timeout during fw download ");
340 				/* timed out */
341 				release_firmware(kim_gdata->fw_entry);
342 				return -1;
343 			}
344 			break;
345 		case ACTION_DELAY:	/* sleep */
346 			pr_info("sleep command in scr");
347 			action_ptr = &(((struct bts_action *)ptr)->data[0]);
348 			mdelay(((struct bts_action_delay *)action_ptr)->msec);
349 			break;
350 		}
351 		len =
352 		    len - (sizeof(struct bts_action) +
353 			   ((struct bts_action *)ptr)->size);
354 		ptr =
355 		    ptr + sizeof(struct bts_action) +
356 		    ((struct bts_action *)ptr)->size;
357 	}
358 	/* fw download complete */
359 	release_firmware(kim_gdata->fw_entry);
360 	return 0;
361 }
362 
363 /**********************************************************************/
364 /* functions called from ST core */
365 /* function to toggle the GPIO
366  * needs to know whether the GPIO is active high or active low
367  */
368 void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
369 {
370 	struct platform_device	*kim_pdev;
371 	struct kim_data_s	*kim_gdata;
372 	pr_info(" %s ", __func__);
373 
374 	kim_pdev = st_get_plat_device(0);
375 	kim_gdata = dev_get_drvdata(&kim_pdev->dev);
376 
377 	if (kim_gdata->gpios[type] == -1) {
378 		pr_info(" gpio not requested for protocol %s",
379 			   protocol_names[type]);
380 		return;
381 	}
382 	switch (type) {
383 	case ST_BT:
384 		/*Do Nothing */
385 		break;
386 
387 	case ST_FM:
388 		if (state == KIM_GPIO_ACTIVE)
389 			gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
390 		else
391 			gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
392 		break;
393 
394 	case ST_GPS:
395 		if (state == KIM_GPIO_ACTIVE)
396 			gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
397 		else
398 			gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
399 		break;
400 
401 	case ST_MAX:
402 	default:
403 		break;
404 	}
405 
406 	return;
407 }
408 
409 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
410  * can be because of
411  * 1. response to read local version
412  * 2. during send/recv's of firmware download
413  */
414 void st_kim_recv(void *disc_data, const unsigned char *data, long count)
415 {
416 	struct st_data_s	*st_gdata = (struct st_data_s *)disc_data;
417 	struct kim_data_s	*kim_gdata = st_gdata->kim_data;
418 
419 	pr_info(" %s ", __func__);
420 	/* copy to local buffer */
421 	if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
422 		/* must be the read_ver_cmd */
423 		memcpy(kim_gdata->resp_buffer, data, count);
424 		complete_all(&kim_gdata->kim_rcvd);
425 		return;
426 	} else {
427 		kim_int_recv(kim_gdata, data, count);
428 		/* either completes or times out */
429 	}
430 	return;
431 }
432 
433 /* to signal completion of line discipline installation
434  * called from ST Core, upon tty_open
435  */
436 void st_kim_complete(void *kim_data)
437 {
438 	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
439 	complete(&kim_gdata->ldisc_installed);
440 }
441 
442 /**
443  * st_kim_start - called from ST Core upon 1st registration
444  *	This involves toggling the chip enable gpio, reading
445  *	the firmware version from chip, forming the fw file name
446  *	based on the chip version, requesting the fw, parsing it
447  *	and perform download(send/recv).
448  */
449 long st_kim_start(void *kim_data)
450 {
451 	long err = 0;
452 	long retry = POR_RETRY_COUNT;
453 	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
454 
455 	pr_info(" %s", __func__);
456 
457 	do {
458 		/* TODO: this is only because rfkill sub-system
459 		 * doesn't send events to user-space if the state
460 		 * isn't changed
461 		 */
462 		rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
463 		/* Configure BT nShutdown to HIGH state */
464 		gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
465 		mdelay(5);	/* FIXME: a proper toggle */
466 		gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
467 		mdelay(100);
468 		/* re-initialize the completion */
469 		INIT_COMPLETION(kim_gdata->ldisc_installed);
470 #if 0 /* older way of signalling user-space UIM */
471 		/* send signal to UIM */
472 		err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
473 		if (err != 0) {
474 			pr_info(" sending SIGUSR2 to uim failed %ld", err);
475 			err = -1;
476 			continue;
477 		}
478 #endif
479 		/* unblock and send event to UIM via /dev/rfkill */
480 		rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
481 		/* wait for ldisc to be installed */
482 		err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
483 				msecs_to_jiffies(LDISC_TIME));
484 		if (!err) {	/* timeout */
485 			pr_err("line disc installation timed out ");
486 			err = -1;
487 			continue;
488 		} else {
489 			/* ldisc installed now */
490 			pr_info(" line discipline installed ");
491 			err = download_firmware(kim_gdata);
492 			if (err != 0) {
493 				pr_err("download firmware failed");
494 				continue;
495 			} else {	/* on success don't retry */
496 				break;
497 			}
498 		}
499 	} while (retry--);
500 	return err;
501 }
502 
503 /**
504  * st_kim_stop - called from ST Core, on the last un-registration
505  *	toggle low the chip enable gpio
506  */
507 long st_kim_stop(void *kim_data)
508 {
509 	long err = 0;
510 	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
511 
512 	INIT_COMPLETION(kim_gdata->ldisc_installed);
513 #if 0 /* older way of signalling user-space UIM */
514 	/* send signal to UIM */
515 	err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
516 	if (err != 0) {
517 		pr_err("sending SIGUSR2 to uim failed %ld", err);
518 		return -1;
519 	}
520 #endif
521 	/* set BT rfkill to be blocked */
522 	err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
523 
524 	/* wait for ldisc to be un-installed */
525 	err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
526 			msecs_to_jiffies(LDISC_TIME));
527 	if (!err) {		/* timeout */
528 		pr_err(" timed out waiting for ldisc to be un-installed");
529 		return -1;
530 	}
531 
532 	/* By default configure BT nShutdown to LOW state */
533 	gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
534 	mdelay(1);
535 	gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
536 	mdelay(1);
537 	gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
538 	return err;
539 }
540 
541 /**********************************************************************/
542 /* functions called from subsystems */
543 /* called when debugfs entry is read from */
544 
545 static int show_version(struct seq_file *s, void *unused)
546 {
547 	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
548 	seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
549 			kim_gdata->version.chip, kim_gdata->version.maj_ver,
550 			kim_gdata->version.min_ver);
551 	return 0;
552 }
553 
554 static int show_list(struct seq_file *s, void *unused)
555 {
556 	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
557 	kim_st_list_protocols(kim_gdata->core_data, s);
558 	return 0;
559 }
560 
561 /* function called from rfkill subsystem, when someone from
562  * user space would write 0/1 on the sysfs entry
563  * /sys/class/rfkill/rfkill0,1,3/state
564  */
565 static int kim_toggle_radio(void *data, bool blocked)
566 {
567 	enum proto_type type = *((enum proto_type *)data);
568 	pr_debug(" %s: %d ", __func__, type);
569 
570 	switch (type) {
571 	case ST_BT:
572 		/* do nothing */
573 	break;
574 	case ST_FM:
575 	case ST_GPS:
576 		if (blocked)
577 			st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
578 		else
579 			st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
580 	break;
581 	case ST_MAX:
582 		pr_err(" wrong proto type ");
583 	break;
584 	}
585 	return 0;
586 }
587 
588 /**
589  * st_kim_ref - reference the core's data
590  *	This references the per-ST platform device in the arch/xx/
591  *	board-xx.c file.
592  *	This would enable multiple such platform devices to exist
593  *	on a given platform
594  */
595 void st_kim_ref(struct st_data_s **core_data, int id)
596 {
597 	struct platform_device	*pdev;
598 	struct kim_data_s	*kim_gdata;
599 	/* get kim_gdata reference from platform device */
600 	pdev = st_get_plat_device(id);
601 	kim_gdata = dev_get_drvdata(&pdev->dev);
602 	*core_data = kim_gdata->core_data;
603 }
604 
605 static int kim_version_open(struct inode *i, struct file *f)
606 {
607 	return single_open(f, show_version, i->i_private);
608 }
609 
610 static int kim_list_open(struct inode *i, struct file *f)
611 {
612 	return single_open(f, show_list, i->i_private);
613 }
614 
615 static const struct file_operations version_debugfs_fops = {
616 	/* version info */
617 	.open = kim_version_open,
618 	.read = seq_read,
619 	.llseek = seq_lseek,
620 	.release = single_release,
621 };
622 static const struct file_operations list_debugfs_fops = {
623 	/* protocols info */
624 	.open = kim_list_open,
625 	.read = seq_read,
626 	.llseek = seq_lseek,
627 	.release = single_release,
628 };
629 
630 /**********************************************************************/
631 /* functions called from platform device driver subsystem
632  * need to have a relevant platform device entry in the platform's
633  * board-*.c file
634  */
635 
636 struct dentry *kim_debugfs_dir;
637 static int kim_probe(struct platform_device *pdev)
638 {
639 	long status;
640 	long proto;
641 	long *gpios = pdev->dev.platform_data;
642 	struct kim_data_s	*kim_gdata;
643 
644 	if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
645 		/* multiple devices could exist */
646 		st_kim_devices[pdev->id] = pdev;
647 	} else {
648 		/* platform's sure about existance of 1 device */
649 		st_kim_devices[0] = pdev;
650 	}
651 
652 	kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
653 	if (!kim_gdata) {
654 		pr_err("no mem to allocate");
655 		return -ENOMEM;
656 	}
657 	dev_set_drvdata(&pdev->dev, kim_gdata);
658 
659 	status = st_core_init(&kim_gdata->core_data);
660 	if (status != 0) {
661 		pr_err(" ST core init failed");
662 		return -1;
663 	}
664 	/* refer to itself */
665 	kim_gdata->core_data->kim_data = kim_gdata;
666 
667 	for (proto = 0; proto < ST_MAX; proto++) {
668 		kim_gdata->gpios[proto] = gpios[proto];
669 		pr_info(" %ld gpio to be requested", gpios[proto]);
670 	}
671 
672 	for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
673 		/* Claim the Bluetooth/FM/GPIO
674 		 * nShutdown gpio from the system
675 		 */
676 		status = gpio_request(gpios[proto], "kim");
677 		if (unlikely(status)) {
678 			pr_err(" gpio %ld request failed ", gpios[proto]);
679 			proto -= 1;
680 			while (proto >= 0) {
681 				if (gpios[proto] != -1)
682 					gpio_free(gpios[proto]);
683 			}
684 			return status;
685 		}
686 
687 		/* Configure nShutdown GPIO as output=0 */
688 		status =
689 		    gpio_direction_output(gpios[proto], 0);
690 		if (unlikely(status)) {
691 			pr_err(" unable to configure gpio %ld",
692 				   gpios[proto]);
693 			proto -= 1;
694 			while (proto >= 0) {
695 				if (gpios[proto] != -1)
696 					gpio_free(gpios[proto]);
697 			}
698 			return status;
699 		}
700 	}
701 	/* get reference of pdev for request_firmware
702 	 */
703 	kim_gdata->kim_pdev = pdev;
704 	init_completion(&kim_gdata->kim_rcvd);
705 	init_completion(&kim_gdata->ldisc_installed);
706 
707 	for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
708 		/* TODO: should all types be rfkill_type_bt ? */
709 		kim_gdata->rf_protos[proto] = proto;
710 		kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
711 			&pdev->dev, RFKILL_TYPE_BLUETOOTH,
712 			&kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
713 		if (kim_gdata->rfkill[proto] == NULL) {
714 			pr_err("cannot create rfkill entry for gpio %ld",
715 				   gpios[proto]);
716 			continue;
717 		}
718 		/* block upon creation */
719 		rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
720 		status = rfkill_register(kim_gdata->rfkill[proto]);
721 		if (unlikely(status)) {
722 			pr_err("rfkill registration failed for gpio %ld",
723 				   gpios[proto]);
724 			rfkill_unregister(kim_gdata->rfkill[proto]);
725 			continue;
726 		}
727 		pr_info("rfkill entry created for %ld", gpios[proto]);
728 	}
729 
730 	kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
731 	if (IS_ERR(kim_debugfs_dir)) {
732 		pr_err(" debugfs entries creation failed ");
733 		kim_debugfs_dir = NULL;
734 		return -1;
735 	}
736 
737 	debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
738 				kim_gdata, &version_debugfs_fops);
739 	debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
740 				kim_gdata, &list_debugfs_fops);
741 	pr_info(" debugfs entries created ");
742 	return 0;
743 }
744 
745 static int kim_remove(struct platform_device *pdev)
746 {
747 	/* free the GPIOs requested
748 	 */
749 	long *gpios = pdev->dev.platform_data;
750 	long proto;
751 	struct kim_data_s	*kim_gdata;
752 
753 	kim_gdata = dev_get_drvdata(&pdev->dev);
754 
755 	for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
756 		/* Claim the Bluetooth/FM/GPIO
757 		 * nShutdown gpio from the system
758 		 */
759 		gpio_free(gpios[proto]);
760 		rfkill_unregister(kim_gdata->rfkill[proto]);
761 		rfkill_destroy(kim_gdata->rfkill[proto]);
762 		kim_gdata->rfkill[proto] = NULL;
763 	}
764 	pr_info("kim: GPIO Freed");
765 	debugfs_remove_recursive(kim_debugfs_dir);
766 	kim_gdata->kim_pdev = NULL;
767 	st_core_exit(kim_gdata->core_data);
768 
769 	kfree(kim_gdata);
770 	kim_gdata = NULL;
771 	return 0;
772 }
773 
774 /**********************************************************************/
775 /* entry point for ST KIM module, called in from ST Core */
776 
777 static int __init st_kim_init(void)
778 {
779 	long ret = 0;
780 	ret = platform_driver_register(&kim_platform_driver);
781 	if (ret != 0) {
782 		pr_err("platform drv registration failed");
783 		return -1;
784 	}
785 	return 0;
786 }
787 
788 static void __exit st_kim_deinit(void)
789 {
790 	/* the following returns void */
791 	platform_driver_unregister(&kim_platform_driver);
792 }
793 
794 
795 module_init(st_kim_init);
796 module_exit(st_kim_deinit);
797 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
798 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
799 MODULE_LICENSE("GPL");
800