st_kim.c 26.0 KB
Newer Older
1 2 3 4
/*
 *  Shared Transport Line discipline driver Core
 *	Init Manager module responsible for GPIO control
 *	and firmware download
5 6
 *  Copyright (C) 2009-2010 Texas Instruments
 *  Author: Pavan Savoy <pavan_savoy@ti.com>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#define pr_fmt(fmt) "(stk) :" fmt
#include <linux/platform_device.h>
#include <linux/jiffies.h>
#include <linux/firmware.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/gpio.h>
30 31
#include <linux/debugfs.h>
#include <linux/seq_file.h>
32
#include <linux/sched.h>
R
Randy Dunlap 已提交
33
#include <linux/sysfs.h>
34
#include <linux/tty.h>
35

36
#include <linux/skbuff.h>
37
#include <linux/ti_wilink_st.h>
38
#include <linux/module.h>
G
Gigi Joseph 已提交
39 40
#include <linux/of.h>
#include <linux/of_device.h>
41

42
#define MAX_ST_DEVICES	3	/* Imagine 1 on each UART for now */
43
static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
44 45 46 47

/**********************************************************************/
/* internal functions */

G
Gigi Joseph 已提交
48 49 50
struct ti_st_plat_data	*dt_pdata;
static struct ti_st_plat_data *get_platform_data(struct device *dev);

51 52 53 54 55 56 57 58 59 60 61 62 63
/**
 * st_get_plat_device -
 *	function which returns the reference to the platform device
 *	requested by id. As of now only 1 such device exists (id=0)
 *	the context requesting for reference can get the id to be
 *	requested by a. The protocol driver which is registering or
 *	b. the tty device which is opened.
 */
static struct platform_device *st_get_plat_device(int id)
{
	return st_kim_devices[id];
}

64 65 66 67 68
/**
 * validate_firmware_response -
 *	function to return whether the firmware response was proper
 *	in case of error don't complete so that waiting for proper
 *	response times out
69
 */
70
static void validate_firmware_response(struct kim_data_s *kim_gdata)
71
{
72
	struct sk_buff *skb = kim_gdata->rx_skb;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
	if (!skb)
		return;

	/* these magic numbers are the position in the response buffer which
	 * allows us to distinguish whether the response is for the read
	 * version info. command
	 */
	if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
			skb->data[4] == 0x10 && skb->data[5] == 0x00) {
		/* fw version response */
		memcpy(kim_gdata->resp_buffer,
				kim_gdata->rx_skb->data,
				kim_gdata->rx_skb->len);
		complete_all(&kim_gdata->kim_rcvd);
		kim_gdata->rx_state = ST_W4_PACKET_TYPE;
		kim_gdata->rx_skb = NULL;
		kim_gdata->rx_count = 0;
	} else if (unlikely(skb->data[5] != 0)) {
91 92
		pr_err("no proper response during fw download");
		pr_err("data6 %x", skb->data[5]);
93
		kfree_skb(skb);
94 95 96 97 98 99 100 101 102 103
		return;		/* keep waiting for the proper response */
	}
	/* becos of all the script being downloaded */
	complete_all(&kim_gdata->kim_rcvd);
	kfree_skb(skb);
}

/* check for data len received inside kim_int_recv
 * most often hit the last case to update state to waiting for data
 */
104
static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
105 106 107
{
	register int room = skb_tailroom(kim_gdata->rx_skb);

108
	pr_debug("len %d room %d", len, room);
109 110

	if (!len) {
111
		validate_firmware_response(kim_gdata);
112 113 114 115 116 117 118 119 120 121 122
	} else if (len > room) {
		/* Received packet's payload length is larger.
		 * We can't accommodate it in created skb.
		 */
		pr_err("Data length is too large len %d room %d", len,
			   room);
		kfree_skb(kim_gdata->rx_skb);
	} else {
		/* Packet header has non-zero payload length and
		 * we have enough space in created skb. Lets read
		 * payload data */
123
		kim_gdata->rx_state = ST_W4_DATA;
124 125 126 127 128 129 130 131 132 133 134 135 136
		kim_gdata->rx_count = len;
		return len;
	}

	/* Change ST LL state to continue to process next
	 * packet */
	kim_gdata->rx_state = ST_W4_PACKET_TYPE;
	kim_gdata->rx_skb = NULL;
	kim_gdata->rx_count = 0;

	return 0;
}

137 138 139 140 141
/**
 * kim_int_recv - receive function called during firmware download
 *	firmware download responses on different UART drivers
 *	have been observed to come in bursts of different
 *	tty_receive and hence the logic
142
 */
143
static void kim_int_recv(struct kim_data_s *kim_gdata,
144
	const unsigned char *data, long count)
145
{
146 147
	const unsigned char *ptr;
	int len = 0, type = 0;
148
	unsigned char *plen;
149

150
	pr_debug("%s", __func__);
151
	/* Decode received bytes here */
152
	ptr = data;
153 154 155 156
	if (unlikely(ptr == NULL)) {
		pr_err(" received null from TTY ");
		return;
	}
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171
	while (count) {
		if (kim_gdata->rx_count) {
			len = min_t(unsigned int, kim_gdata->rx_count, count);
			memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
			kim_gdata->rx_count -= len;
			count -= len;
			ptr += len;

			if (kim_gdata->rx_count)
				continue;

			/* Check ST RX state machine , where are we? */
			switch (kim_gdata->rx_state) {
				/* Waiting for complete packet ? */
172
			case ST_W4_DATA:
173
				pr_debug("Complete pkt received");
174
				validate_firmware_response(kim_gdata);
175 176 177 178
				kim_gdata->rx_state = ST_W4_PACKET_TYPE;
				kim_gdata->rx_skb = NULL;
				continue;
				/* Waiting for Bluetooth event header ? */
179 180 181 182 183
			case ST_W4_HEADER:
				plen =
				(unsigned char *)&kim_gdata->rx_skb->data[1];
				pr_debug("event hdr: plen 0x%02x\n", *plen);
				kim_check_data_len(kim_gdata, *plen);
184 185 186 187 188
				continue;
			}	/* end of switch */
		}		/* end of if rx_state */
		switch (*ptr) {
			/* Bluetooth event packet? */
189 190 191 192
		case 0x04:
			kim_gdata->rx_state = ST_W4_HEADER;
			kim_gdata->rx_count = 2;
			type = *ptr;
193 194 195 196 197 198
			break;
		default:
			pr_info("unknown packet");
			ptr++;
			count--;
			continue;
199
		}
200 201 202
		ptr++;
		count--;
		kim_gdata->rx_skb =
203
			alloc_skb(1024+8, GFP_ATOMIC);
204 205 206 207 208
		if (!kim_gdata->rx_skb) {
			pr_err("can't allocate mem for new packet");
			kim_gdata->rx_state = ST_W4_PACKET_TYPE;
			kim_gdata->rx_count = 0;
			return;
209
		}
210 211 212 213
		skb_reserve(kim_gdata->rx_skb, 8);
		kim_gdata->rx_skb->cb[0] = 4;
		kim_gdata->rx_skb->cb[1] = 0;

214
	}
215 216 217
	return;
}

218
static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
219 220
{
	unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
221
	const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
222

223
	pr_debug("%s", __func__);
224

225
	reinit_completion(&kim_gdata->kim_rcvd);
226 227
	if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
		pr_err("kim: couldn't write 4 bytes");
228
		return -EIO;
229 230
	}

231 232
	if (!wait_for_completion_interruptible_timeout(
		&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
233
		pr_err(" waiting for ver info- timed out ");
234
		return -ETIMEDOUT;
235
	}
236
	reinit_completion(&kim_gdata->kim_rcvd);
237 238 239
	/* the positions 12 & 13 in the response buffer provide with the
	 * chip, major & minor numbers
	 */
240 241

	version =
242 243
		MAKEWORD(kim_gdata->resp_buffer[12],
				kim_gdata->resp_buffer[13]);
244 245 246 247 248 249 250
	chip = (version & 0x7C00) >> 10;
	min_ver = (version & 0x007F);
	maj_ver = (version & 0x0380) >> 7;

	if (version & 0x8000)
		maj_ver |= 0x0008;

251 252
	sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
		chip, maj_ver, min_ver);
253 254 255 256 257 258 259

	/* to be accessed later via sysfs entry */
	kim_gdata->version.full = version;
	kim_gdata->version.chip = chip;
	kim_gdata->version.maj_ver = maj_ver;
	kim_gdata->version.min_ver = min_ver;

260
	pr_info("%s", bts_scr_name);
261
	return 0;
262 263
}

264
static void skip_change_remote_baud(unsigned char **ptr, long *len)
265 266 267 268 269 270 271 272 273 274 275
{
	unsigned char *nxt_action, *cur_action;
	cur_action = *ptr;

	nxt_action = cur_action + sizeof(struct bts_action) +
		((struct bts_action *) cur_action)->size;

	if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
		pr_err("invalid action after change remote baud command");
	} else {
		*ptr = *ptr + sizeof(struct bts_action) +
276
			((struct bts_action *)cur_action)->size;
277
		*len = *len - (sizeof(struct bts_action) +
278
				((struct bts_action *)cur_action)->size);
279 280 281 282 283
		/* warn user on not commenting these in firmware */
		pr_warn("skipping the wait event of change remote baud");
	}
}

284 285 286 287
/**
 * download_firmware -
 *	internal function which parses through the .bts firmware
 *	script file intreprets SEND, DELAY actions only as of now
288
 */
289
static long download_firmware(struct kim_data_s *kim_gdata)
290
{
291
	long err = 0;
292
	long len = 0;
293 294
	unsigned char *ptr = NULL;
	unsigned char *action_ptr = NULL;
295
	unsigned char bts_scr_name[40] = { 0 };	/* 40 char long bts scr name? */
296 297 298
	int wr_room_space;
	int cmd_size;
	unsigned long timeout;
299

300
	err = read_local_version(kim_gdata, bts_scr_name);
301
	if (err != 0) {
302 303 304 305 306 307 308 309 310 311
		pr_err("kim: failed to read local ver");
		return err;
	}
	err =
	    request_firmware(&kim_gdata->fw_entry, bts_scr_name,
			     &kim_gdata->kim_pdev->dev);
	if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
		     (kim_gdata->fw_entry->size == 0))) {
		pr_err(" request_firmware failed(errno %ld) for %s", err,
			   bts_scr_name);
312
		return -EINVAL;
313 314 315 316 317 318 319 320 321 322
	}
	ptr = (void *)kim_gdata->fw_entry->data;
	len = kim_gdata->fw_entry->size;
	/* bts_header to remove out magic number and
	 * version
	 */
	ptr += sizeof(struct bts_header);
	len -= sizeof(struct bts_header);

	while (len > 0 && ptr) {
323
		pr_debug(" action size %d, type %d ",
324 325 326 327 328
			   ((struct bts_action *)ptr)->size,
			   ((struct bts_action *)ptr)->type);

		switch (((struct bts_action *)ptr)->type) {
		case ACTION_SEND_COMMAND:	/* action send */
329
			pr_debug("S");
330 331 332 333 334 335
			action_ptr = &(((struct bts_action *)ptr)->data[0]);
			if (unlikely
			    (((struct hci_command *)action_ptr)->opcode ==
			     0xFF36)) {
				/* ignore remote change
				 * baud rate HCI VS command */
336
				pr_warn("change remote baud"
337
				    " rate command in firmware");
338
				skip_change_remote_baud(&ptr, &len);
339 340
				break;
			}
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
			/*
			 * Make sure we have enough free space in uart
			 * tx buffer to write current firmware command
			 */
			cmd_size = ((struct bts_action *)ptr)->size;
			timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
			do {
				wr_room_space =
					st_get_uart_wr_room(kim_gdata->core_data);
				if (wr_room_space < 0) {
					pr_err("Unable to get free "
							"space info from uart tx buffer");
					release_firmware(kim_gdata->fw_entry);
					return wr_room_space;
				}
				mdelay(1); /* wait 1ms before checking room */
			} while ((wr_room_space < cmd_size) &&
					time_before(jiffies, timeout));

			/* Timeout happened ? */
			if (time_after_eq(jiffies, timeout)) {
				pr_err("Timeout while waiting for free "
						"free space in uart tx buffer");
				release_firmware(kim_gdata->fw_entry);
				return -ETIMEDOUT;
			}
367 368 369
			/* reinit completion before sending for the
			 * relevant wait
			 */
370
			reinit_completion(&kim_gdata->kim_rcvd);
371

372 373 374 375 376
			/*
			 * Free space found in uart buffer, call st_int_write
			 * to send current firmware command to the uart tx
			 * buffer.
			 */
377 378 379 380 381
			err = st_int_write(kim_gdata->core_data,
			((struct bts_action_send *)action_ptr)->data,
					   ((struct bts_action *)ptr)->size);
			if (unlikely(err < 0)) {
				release_firmware(kim_gdata->fw_entry);
382
				return err;
383
			}
384 385 386 387 388 389 390 391 392 393 394 395 396
			/*
			 * Check number of bytes written to the uart tx buffer
			 * and requested command write size
			 */
			if (err != cmd_size) {
				pr_err("Number of bytes written to uart "
						"tx buffer are not matching with "
						"requested cmd write size");
				release_firmware(kim_gdata->fw_entry);
				return -EIO;
			}
			break;
		case ACTION_WAIT_EVENT:  /* wait */
397
			pr_debug("W");
398 399 400
			if (!wait_for_completion_interruptible_timeout(
					&kim_gdata->kim_rcvd,
					msecs_to_jiffies(CMD_RESP_TIME))) {
401
				pr_err("response timeout during fw download ");
402 403
				/* timed out */
				release_firmware(kim_gdata->fw_entry);
404
				return -ETIMEDOUT;
405
			}
406
			reinit_completion(&kim_gdata->kim_rcvd);
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
			break;
		case ACTION_DELAY:	/* sleep */
			pr_info("sleep command in scr");
			action_ptr = &(((struct bts_action *)ptr)->data[0]);
			mdelay(((struct bts_action_delay *)action_ptr)->msec);
			break;
		}
		len =
		    len - (sizeof(struct bts_action) +
			   ((struct bts_action *)ptr)->size);
		ptr =
		    ptr + sizeof(struct bts_action) +
		    ((struct bts_action *)ptr)->size;
	}
	/* fw download complete */
	release_firmware(kim_gdata->fw_entry);
423
	return 0;
424 425 426 427 428 429 430 431 432 433 434
}

/**********************************************************************/
/* functions called from ST core */
/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
 * can be because of
 * 1. response to read local version
 * 2. during send/recv's of firmware download
 */
void st_kim_recv(void *disc_data, const unsigned char *data, long count)
{
435 436 437
	struct st_data_s	*st_gdata = (struct st_data_s *)disc_data;
	struct kim_data_s	*kim_gdata = st_gdata->kim_data;

438 439 440 441
	/* proceed to gather all data and distinguish read fw version response
	 * from other fw responses when data gathering is complete
	 */
	kim_int_recv(kim_gdata, data, count);
442 443 444 445 446 447
	return;
}

/* to signal completion of line discipline installation
 * called from ST Core, upon tty_open
 */
448
void st_kim_complete(void *kim_data)
449
{
450
	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
451 452 453
	complete(&kim_gdata->ldisc_installed);
}

454 455 456 457 458 459 460
/**
 * st_kim_start - called from ST Core upon 1st registration
 *	This involves toggling the chip enable gpio, reading
 *	the firmware version from chip, forming the fw file name
 *	based on the chip version, requesting the fw, parsing it
 *	and perform download(send/recv).
 */
461
long st_kim_start(void *kim_data)
462
{
463
	long err = 0;
464
	long retry = POR_RETRY_COUNT;
465
	struct ti_st_plat_data	*pdata;
466 467
	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;

468
	pr_info(" %s", __func__);
G
Gigi Joseph 已提交
469 470 471 472 473 474
	if (kim_gdata->kim_pdev->dev.of_node) {
		pr_debug("use device tree data");
		pdata = dt_pdata;
	} else {
		pdata = kim_gdata->kim_pdev->dev.platform_data;
	}
475 476

	do {
477 478 479 480
		/* platform specific enabling code here */
		if (pdata->chip_enable)
			pdata->chip_enable(kim_gdata);

481 482 483 484 485
		/* Configure BT nShutdown to HIGH state */
		gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
		mdelay(5);	/* FIXME: a proper toggle */
		gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
		mdelay(100);
486
		/* re-initialize the completion */
487
		reinit_completion(&kim_gdata->ldisc_installed);
488 489 490 491 492
		/* send notification to UIM */
		kim_gdata->ldisc_install = 1;
		pr_info("ldisc_install = 1");
		sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
				NULL, "install");
493
		/* wait for ldisc to be installed */
494 495
		err = wait_for_completion_interruptible_timeout(
			&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
496 497 498 499 500
		if (!err) {
			/* ldisc installation timeout,
			 * flush uart, power cycle BT_EN */
			pr_err("ldisc installation timeout");
			err = st_kim_stop(kim_gdata);
501 502 503
			continue;
		} else {
			/* ldisc installed now */
504
			pr_info("line discipline installed");
505
			err = download_firmware(kim_gdata);
506
			if (err != 0) {
507 508
				/* ldisc installed but fw download failed,
				 * flush uart & power cycle BT_EN */
509
				pr_err("download firmware failed");
510
				err = st_kim_stop(kim_gdata);
511 512 513 514 515 516 517 518 519
				continue;
			} else {	/* on success don't retry */
				break;
			}
		}
	} while (retry--);
	return err;
}

520
/**
521 522 523 524 525 526
 * st_kim_stop - stop communication with chip.
 *	This can be called from ST Core/KIM, on the-
 *	(a) last un-register when chip need not be powered there-after,
 *	(b) upon failure to either install ldisc or download firmware.
 *	The function is responsible to (a) notify UIM about un-installation,
 *	(b) flush UART if the ldisc was installed.
527 528
 *	(c) reset BT_EN - pull down nshutdown at the end.
 *	(d) invoke platform's chip disabling routine.
529
 */
530
long st_kim_stop(void *kim_data)
531
{
532
	long err = 0;
533
	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
G
Gigi Joseph 已提交
534
	struct ti_st_plat_data	*pdata;
535
	struct tty_struct	*tty = kim_gdata->core_data->tty;
536

537
	reinit_completion(&kim_gdata->ldisc_installed);
538

G
Gigi Joseph 已提交
539 540 541 542 543 544 545
	if (kim_gdata->kim_pdev->dev.of_node) {
		pr_debug("use device tree data");
		pdata = dt_pdata;
	} else
		pdata = kim_gdata->kim_pdev->dev.platform_data;


546 547 548 549 550
	if (tty) {	/* can be called before ldisc is installed */
		/* Flush any pending characters in the driver and discipline. */
		tty_ldisc_flush(tty);
		tty_driver_flush_buffer(tty);
	}
551 552 553 554 555

	/* send uninstall notification to UIM */
	pr_info("ldisc_install = 0");
	kim_gdata->ldisc_install = 0;
	sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
556 557

	/* wait for ldisc to be un-installed */
558 559
	err = wait_for_completion_interruptible_timeout(
		&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
560 561
	if (!err) {		/* timeout */
		pr_err(" timed out waiting for ldisc to be un-installed");
562
		err = -ETIMEDOUT;
563 564
	}

565 566 567 568 569 570 571
	/* By default configure BT nShutdown to LOW state */
	gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
	mdelay(1);
	gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
	mdelay(1);
	gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);

572 573 574
	/* platform specific disable */
	if (pdata->chip_disable)
		pdata->chip_disable(kim_gdata);
575 576 577 578 579
	return err;
}

/**********************************************************************/
/* functions called from subsystems */
580
/* called when debugfs entry is read from */
581

582
static int show_version(struct seq_file *s, void *unused)
583
{
584 585
	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
	seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
586 587
			kim_gdata->version.chip, kim_gdata->version.maj_ver,
			kim_gdata->version.min_ver);
588
	return 0;
589 590
}

591
static int show_list(struct seq_file *s, void *unused)
592
{
593 594 595
	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
	kim_st_list_protocols(kim_gdata->core_data, s);
	return 0;
596 597
}

598 599
static ssize_t show_install(struct device *dev,
		struct device_attribute *attr, char *buf)
600
{
601 602 603
	struct kim_data_s *kim_data = dev_get_drvdata(dev);
	return sprintf(buf, "%d\n", kim_data->ldisc_install);
}
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
#ifdef DEBUG
static ssize_t store_dev_name(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct kim_data_s *kim_data = dev_get_drvdata(dev);
	pr_debug("storing dev name >%s<", buf);
	strncpy(kim_data->dev_name, buf, count);
	pr_debug("stored dev name >%s<", kim_data->dev_name);
	return count;
}

static ssize_t store_baud_rate(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct kim_data_s *kim_data = dev_get_drvdata(dev);
	pr_debug("storing baud rate >%s<", buf);
	sscanf(buf, "%ld", &kim_data->baud_rate);
	pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
	return count;
}
#endif	/* if DEBUG */

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
static ssize_t show_dev_name(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct kim_data_s *kim_data = dev_get_drvdata(dev);
	return sprintf(buf, "%s\n", kim_data->dev_name);
}

static ssize_t show_baud_rate(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct kim_data_s *kim_data = dev_get_drvdata(dev);
	return sprintf(buf, "%ld\n", kim_data->baud_rate);
}

static ssize_t show_flow_cntrl(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct kim_data_s *kim_data = dev_get_drvdata(dev);
	return sprintf(buf, "%d\n", kim_data->flow_cntrl);
646 647
}

648 649 650 651 652
/* structures specific for sysfs entries */
static struct kobj_attribute ldisc_install =
__ATTR(install, 0444, (void *)show_install, NULL);

static struct kobj_attribute uart_dev_name =
653 654 655
#ifdef DEBUG	/* TODO: move this to debug-fs if possible */
__ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
#else
656
__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
657
#endif
658 659

static struct kobj_attribute uart_baud_rate =
660 661 662
#ifdef DEBUG	/* TODO: move to debugfs */
__ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
#else
663
__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
664
#endif
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

static struct kobj_attribute uart_flow_cntrl =
__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);

static struct attribute *uim_attrs[] = {
	&ldisc_install.attr,
	&uart_dev_name.attr,
	&uart_baud_rate.attr,
	&uart_flow_cntrl.attr,
	NULL,
};

static struct attribute_group uim_attr_grp = {
	.attrs = uim_attrs,
};

681 682 683 684 685 686 687
/**
 * st_kim_ref - reference the core's data
 *	This references the per-ST platform device in the arch/xx/
 *	board-xx.c file.
 *	This would enable multiple such platform devices to exist
 *	on a given platform
 */
688
void st_kim_ref(struct st_data_s **core_data, int id)
689
{
690 691 692
	struct platform_device	*pdev;
	struct kim_data_s	*kim_gdata;
	/* get kim_gdata reference from platform device */
693
	pdev = st_get_plat_device(id);
694 695
	if (!pdev)
		goto err;
696
	kim_gdata = platform_get_drvdata(pdev);
697 698 699
	if (!kim_gdata)
		goto err;

700
	*core_data = kim_gdata->core_data;
701 702 703
	return;
err:
	*core_data = NULL;
704 705
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
static int kim_version_open(struct inode *i, struct file *f)
{
	return single_open(f, show_version, i->i_private);
}

static int kim_list_open(struct inode *i, struct file *f)
{
	return single_open(f, show_list, i->i_private);
}

static const struct file_operations version_debugfs_fops = {
	/* version info */
	.open = kim_version_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};
static const struct file_operations list_debugfs_fops = {
	/* protocols info */
	.open = kim_list_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = single_release,
};

731 732 733 734 735 736
/**********************************************************************/
/* functions called from platform device driver subsystem
 * need to have a relevant platform device entry in the platform's
 * board-*.c file
 */

G
Gigi Joseph 已提交
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
static const struct of_device_id kim_of_match[] = {
{
	.compatible = "kim",
	},
	{}
};
MODULE_DEVICE_TABLE(of, kim_of_match);

static struct ti_st_plat_data *get_platform_data(struct device *dev)
{
	struct device_node *np = dev->of_node;
	const u32 *dt_property;
	int len;

	dt_pdata = kzalloc(sizeof(*dt_pdata), GFP_KERNEL);

	if (!dt_pdata)
		pr_err("Can't allocate device_tree platform data\n");

	dt_property = of_get_property(np, "dev_name", &len);
	if (dt_property)
		memcpy(&dt_pdata->dev_name, dt_property, len);
	of_property_read_u32(np, "nshutdown_gpio",
			     (u32 *)&dt_pdata->nshutdown_gpio);
	of_property_read_u32(np, "flow_cntrl", (u32 *)&dt_pdata->flow_cntrl);
	of_property_read_u32(np, "baud_rate", (u32 *)&dt_pdata->baud_rate);

	return dt_pdata;
}

767
static struct dentry *kim_debugfs_dir;
768 769
static int kim_probe(struct platform_device *pdev)
{
770
	struct kim_data_s	*kim_gdata;
G
Gigi Joseph 已提交
771
	struct ti_st_plat_data	*pdata;
772
	int err;
773

G
Gigi Joseph 已提交
774 775 776 777 778 779 780 781 782 783
	if (pdev->dev.of_node)
		pdata = get_platform_data(&pdev->dev);
	else
		pdata = pdev->dev.platform_data;

	if (pdata == NULL) {
		dev_err(&pdev->dev, "Platform Data is missing\n");
		return -ENXIO;
	}

784 785 786 787
	if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
		/* multiple devices could exist */
		st_kim_devices[pdev->id] = pdev;
	} else {
L
Lucas De Marchi 已提交
788
		/* platform's sure about existence of 1 device */
789 790 791
		st_kim_devices[0] = pdev;
	}

792 793 794 795 796
	kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
	if (!kim_gdata) {
		pr_err("no mem to allocate");
		return -ENOMEM;
	}
797
	platform_set_drvdata(pdev, kim_gdata);
798

799 800
	err = st_core_init(&kim_gdata->core_data);
	if (err != 0) {
801
		pr_err(" ST core init failed");
802 803
		err = -EIO;
		goto err_core_init;
804
	}
805 806
	/* refer to itself */
	kim_gdata->core_data->kim_data = kim_gdata;
807

808 809 810 811 812 813 814 815 816 817 818 819 820 821
	/* Claim the chip enable nShutdown gpio from the system */
	kim_gdata->nshutdown = pdata->nshutdown_gpio;
	err = gpio_request(kim_gdata->nshutdown, "kim");
	if (unlikely(err)) {
		pr_err(" gpio %ld request failed ", kim_gdata->nshutdown);
		return err;
	}

	/* Configure nShutdown GPIO as output=0 */
	err = gpio_direction_output(kim_gdata->nshutdown, 0);
	if (unlikely(err)) {
		pr_err(" unable to configure gpio %ld", kim_gdata->nshutdown);
		return err;
	}
822 823 824 825 826
	/* get reference of pdev for request_firmware
	 */
	kim_gdata->kim_pdev = pdev;
	init_completion(&kim_gdata->kim_rcvd);
	init_completion(&kim_gdata->ldisc_installed);
N
Naveen Jain 已提交
827

828 829
	err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
	if (err) {
830
		pr_err("failed to create sysfs entries");
831
		goto err_sysfs_group;
832
	}
833

834 835 836 837 838 839
	/* copying platform data */
	strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
	kim_gdata->flow_cntrl = pdata->flow_cntrl;
	kim_gdata->baud_rate = pdata->baud_rate;
	pr_info("sysfs entries created\n");

840
	kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
841
	if (!kim_debugfs_dir) {
842
		pr_err(" debugfs entries creation failed ");
843
		return 0;
844
	}
845 846 847 848 849

	debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
				kim_gdata, &version_debugfs_fops);
	debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
				kim_gdata, &list_debugfs_fops);
850
	return 0;
851 852 853 854 855 856 857 858

err_sysfs_group:
	st_core_exit(kim_gdata->core_data);

err_core_init:
	kfree(kim_gdata);

	return err;
859 860 861 862
}

static int kim_remove(struct platform_device *pdev)
{
863
	/* free the GPIOs requested */
G
Gigi Joseph 已提交
864
	struct ti_st_plat_data	*pdata;
865 866
	struct kim_data_s	*kim_gdata;

G
Gigi Joseph 已提交
867 868 869 870 871 872 873
	if (pdev->dev.of_node) {
		pr_debug("use device tree data");
		pdata = dt_pdata;
	} else {
		pdata = pdev->dev.platform_data;
	}

874
	kim_gdata = platform_get_drvdata(pdev);
875

876 877 878 879 880 881
	/* Free the Bluetooth/FM/GPIO
	 * nShutdown gpio from the system
	 */
	gpio_free(pdata->nshutdown_gpio);
	pr_info("nshutdown GPIO Freed");

882
	debugfs_remove_recursive(kim_debugfs_dir);
883
	sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
884 885
	pr_info("sysfs entries removed");

886 887
	kim_gdata->kim_pdev = NULL;
	st_core_exit(kim_gdata->core_data);
888 889 890

	kfree(kim_gdata);
	kim_gdata = NULL;
G
Gigi Joseph 已提交
891 892 893
	kfree(dt_pdata);
	dt_pdata = NULL;

894
	return 0;
895 896
}

897
static int kim_suspend(struct platform_device *pdev, pm_message_t state)
898
{
G
Gigi Joseph 已提交
899 900 901 902 903 904 905 906
	struct ti_st_plat_data	*pdata;

	if (pdev->dev.of_node) {
		pr_debug("use device tree data");
		pdata = dt_pdata;
	} else {
		pdata = pdev->dev.platform_data;
	}
907 908 909 910

	if (pdata->suspend)
		return pdata->suspend(pdev, state);

911
	return 0;
912 913
}

914
static int kim_resume(struct platform_device *pdev)
915
{
G
Gigi Joseph 已提交
916 917 918 919 920 921 922 923
	struct ti_st_plat_data	*pdata;

	if (pdev->dev.of_node) {
		pr_debug("use device tree data");
		pdata = dt_pdata;
	} else {
		pdata = pdev->dev.platform_data;
	}
924 925 926 927

	if (pdata->resume)
		return pdata->resume(pdev);

928
	return 0;
929 930
}

931 932
/**********************************************************************/
/* entry point for ST KIM module, called in from ST Core */
933 934 935 936 937 938 939
static struct platform_driver kim_platform_driver = {
	.probe = kim_probe,
	.remove = kim_remove,
	.suspend = kim_suspend,
	.resume = kim_resume,
	.driver = {
		.name = "kim",
G
Gigi Joseph 已提交
940 941
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(kim_of_match),
942 943
	},
};
944

945
module_platform_driver(kim_platform_driver);
946 947 948 949

MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
MODULE_LICENSE("GPL");