xref: /linux/drivers/misc/ti-st/st_kim.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a0cc2f3bSPavan Savoy /*
3a0cc2f3bSPavan Savoy  *  Shared Transport Line discipline driver Core
4a0cc2f3bSPavan Savoy  *	Init Manager module responsible for GPIO control
5a0cc2f3bSPavan Savoy  *	and firmware download
6a0cc2f3bSPavan Savoy  *  Copyright (C) 2009-2010 Texas Instruments
7a0cc2f3bSPavan Savoy  *  Author: Pavan Savoy <pavan_savoy@ti.com>
8a0cc2f3bSPavan Savoy  */
9a0cc2f3bSPavan Savoy 
10a0cc2f3bSPavan Savoy #define pr_fmt(fmt) "(stk) :" fmt
11a0cc2f3bSPavan Savoy #include <linux/platform_device.h>
12a0cc2f3bSPavan Savoy #include <linux/jiffies.h>
13a0cc2f3bSPavan Savoy #include <linux/firmware.h>
14a0cc2f3bSPavan Savoy #include <linux/delay.h>
15a0cc2f3bSPavan Savoy #include <linux/wait.h>
16a0cc2f3bSPavan Savoy #include <linux/gpio.h>
17a0cc2f3bSPavan Savoy #include <linux/debugfs.h>
18a0cc2f3bSPavan Savoy #include <linux/seq_file.h>
19a0cc2f3bSPavan Savoy #include <linux/sched.h>
20773d6790SRandy Dunlap #include <linux/sysfs.h>
21ec60d0adSPavan Savoy #include <linux/tty.h>
22a0cc2f3bSPavan Savoy 
235c88b021SPavan Savoy #include <linux/skbuff.h>
24a0cc2f3bSPavan Savoy #include <linux/ti_wilink_st.h>
25eb12a679SPaul Gortmaker #include <linux/module.h>
26a0cc2f3bSPavan Savoy 
27a0cc2f3bSPavan Savoy #define MAX_ST_DEVICES	3	/* Imagine 1 on each UART for now */
2873f12e8dSPavan Savoy static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
29a0cc2f3bSPavan Savoy 
30a0cc2f3bSPavan Savoy /**********************************************************************/
31a0cc2f3bSPavan Savoy /* internal functions */
32a0cc2f3bSPavan Savoy 
333caf1b48SLee Jones /*
34a0cc2f3bSPavan Savoy  * st_get_plat_device -
35a0cc2f3bSPavan Savoy  *	function which returns the reference to the platform device
36a0cc2f3bSPavan Savoy  *	requested by id. As of now only 1 such device exists (id=0)
37a0cc2f3bSPavan Savoy  *	the context requesting for reference can get the id to be
38a0cc2f3bSPavan Savoy  *	requested by a. The protocol driver which is registering or
39a0cc2f3bSPavan Savoy  *	b. the tty device which is opened.
40a0cc2f3bSPavan Savoy  */
st_get_plat_device(int id)41a0cc2f3bSPavan Savoy static struct platform_device *st_get_plat_device(int id)
42a0cc2f3bSPavan Savoy {
43a0cc2f3bSPavan Savoy 	return st_kim_devices[id];
44a0cc2f3bSPavan Savoy }
45a0cc2f3bSPavan Savoy 
463caf1b48SLee Jones /*
47a0cc2f3bSPavan Savoy  * validate_firmware_response -
48a0cc2f3bSPavan Savoy  *	function to return whether the firmware response was proper
49a0cc2f3bSPavan Savoy  *	in case of error don't complete so that waiting for proper
50a0cc2f3bSPavan Savoy  *	response times out
51a0cc2f3bSPavan Savoy  */
validate_firmware_response(struct kim_data_s * kim_gdata)5227712b39SPavan Savoy static void validate_firmware_response(struct kim_data_s *kim_gdata)
53a0cc2f3bSPavan Savoy {
54a0cc2f3bSPavan Savoy 	struct sk_buff *skb = kim_gdata->rx_skb;
558565adbcSPavan Savoy 	if (!skb)
568565adbcSPavan Savoy 		return;
578565adbcSPavan Savoy 
583caf1b48SLee Jones 	/*
593caf1b48SLee Jones 	 * these magic numbers are the position in the response buffer which
608565adbcSPavan Savoy 	 * allows us to distinguish whether the response is for the read
618565adbcSPavan Savoy 	 * version info. command
628565adbcSPavan Savoy 	 */
638565adbcSPavan Savoy 	if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
648565adbcSPavan Savoy 			skb->data[4] == 0x10 && skb->data[5] == 0x00) {
658565adbcSPavan Savoy 		/* fw version response */
668565adbcSPavan Savoy 		memcpy(kim_gdata->resp_buffer,
678565adbcSPavan Savoy 				kim_gdata->rx_skb->data,
688565adbcSPavan Savoy 				kim_gdata->rx_skb->len);
698565adbcSPavan Savoy 		kim_gdata->rx_state = ST_W4_PACKET_TYPE;
708565adbcSPavan Savoy 		kim_gdata->rx_skb = NULL;
718565adbcSPavan Savoy 		kim_gdata->rx_count = 0;
728565adbcSPavan Savoy 	} else if (unlikely(skb->data[5] != 0)) {
73a0cc2f3bSPavan Savoy 		pr_err("no proper response during fw download");
74a0cc2f3bSPavan Savoy 		pr_err("data6 %x", skb->data[5]);
7576ff0e64SPavan Savoy 		kfree_skb(skb);
76a0cc2f3bSPavan Savoy 		return;		/* keep waiting for the proper response */
77a0cc2f3bSPavan Savoy 	}
78a0cc2f3bSPavan Savoy 	/* becos of all the script being downloaded */
79a0cc2f3bSPavan Savoy 	complete_all(&kim_gdata->kim_rcvd);
80a0cc2f3bSPavan Savoy 	kfree_skb(skb);
81a0cc2f3bSPavan Savoy }
82a0cc2f3bSPavan Savoy 
833caf1b48SLee Jones /*
843caf1b48SLee Jones  * check for data len received inside kim_int_recv
85a0cc2f3bSPavan Savoy  * most often hit the last case to update state to waiting for data
86a0cc2f3bSPavan Savoy  */
kim_check_data_len(struct kim_data_s * kim_gdata,int len)87a0cc2f3bSPavan Savoy static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
88a0cc2f3bSPavan Savoy {
89a0cc2f3bSPavan Savoy 	register int room = skb_tailroom(kim_gdata->rx_skb);
90a0cc2f3bSPavan Savoy 
91a0cc2f3bSPavan Savoy 	pr_debug("len %d room %d", len, room);
92a0cc2f3bSPavan Savoy 
93a0cc2f3bSPavan Savoy 	if (!len) {
94a0cc2f3bSPavan Savoy 		validate_firmware_response(kim_gdata);
95a0cc2f3bSPavan Savoy 	} else if (len > room) {
963caf1b48SLee Jones 		/*
973caf1b48SLee Jones 		 * Received packet's payload length is larger.
98a0cc2f3bSPavan Savoy 		 * We can't accommodate it in created skb.
99a0cc2f3bSPavan Savoy 		 */
100a0cc2f3bSPavan Savoy 		pr_err("Data length is too large len %d room %d", len,
101a0cc2f3bSPavan Savoy 			   room);
102a0cc2f3bSPavan Savoy 		kfree_skb(kim_gdata->rx_skb);
103a0cc2f3bSPavan Savoy 	} else {
1043caf1b48SLee Jones 		/*
1053caf1b48SLee Jones 		 * Packet header has non-zero payload length and
106a0cc2f3bSPavan Savoy 		 * we have enough space in created skb. Lets read
107a0cc2f3bSPavan Savoy 		 * payload data */
1085c88b021SPavan Savoy 		kim_gdata->rx_state = ST_W4_DATA;
109a0cc2f3bSPavan Savoy 		kim_gdata->rx_count = len;
110a0cc2f3bSPavan Savoy 		return len;
111a0cc2f3bSPavan Savoy 	}
112a0cc2f3bSPavan Savoy 
1133caf1b48SLee Jones 	/*
1143caf1b48SLee Jones 	 * Change ST LL state to continue to process next
1153caf1b48SLee Jones 	 * packet
1163caf1b48SLee Jones 	 */
117a0cc2f3bSPavan Savoy 	kim_gdata->rx_state = ST_W4_PACKET_TYPE;
118a0cc2f3bSPavan Savoy 	kim_gdata->rx_skb = NULL;
119a0cc2f3bSPavan Savoy 	kim_gdata->rx_count = 0;
120a0cc2f3bSPavan Savoy 
121a0cc2f3bSPavan Savoy 	return 0;
122a0cc2f3bSPavan Savoy }
123a0cc2f3bSPavan Savoy 
1243caf1b48SLee Jones /*
125a0cc2f3bSPavan Savoy  * kim_int_recv - receive function called during firmware download
126a0cc2f3bSPavan Savoy  *	firmware download responses on different UART drivers
127a0cc2f3bSPavan Savoy  *	have been observed to come in bursts of different
128a0cc2f3bSPavan Savoy  *	tty_receive and hence the logic
129a0cc2f3bSPavan Savoy  */
kim_int_recv(struct kim_data_s * kim_gdata,const u8 * ptr,size_t count)130ead03e72SJiri Slaby (SUSE) static void kim_int_recv(struct kim_data_s *kim_gdata, const u8 *ptr,
131ead03e72SJiri Slaby (SUSE) 			 size_t count)
132a0cc2f3bSPavan Savoy {
13353e44782SColin Ian King 	int len = 0;
1345c88b021SPavan Savoy 	unsigned char *plen;
135a0cc2f3bSPavan Savoy 
136a0cc2f3bSPavan Savoy 	pr_debug("%s", __func__);
137a0cc2f3bSPavan Savoy 	/* Decode received bytes here */
138a0cc2f3bSPavan Savoy 	while (count) {
139a0cc2f3bSPavan Savoy 		if (kim_gdata->rx_count) {
140a0cc2f3bSPavan Savoy 			len = min_t(unsigned int, kim_gdata->rx_count, count);
14159ae1d12SJohannes Berg 			skb_put_data(kim_gdata->rx_skb, ptr, len);
142a0cc2f3bSPavan Savoy 			kim_gdata->rx_count -= len;
143a0cc2f3bSPavan Savoy 			count -= len;
144a0cc2f3bSPavan Savoy 			ptr += len;
145a0cc2f3bSPavan Savoy 
146a0cc2f3bSPavan Savoy 			if (kim_gdata->rx_count)
147a0cc2f3bSPavan Savoy 				continue;
148a0cc2f3bSPavan Savoy 
149a0cc2f3bSPavan Savoy 			/* Check ST RX state machine , where are we? */
150a0cc2f3bSPavan Savoy 			switch (kim_gdata->rx_state) {
151a0cc2f3bSPavan Savoy 				/* Waiting for complete packet ? */
1525c88b021SPavan Savoy 			case ST_W4_DATA:
153a0cc2f3bSPavan Savoy 				pr_debug("Complete pkt received");
154a0cc2f3bSPavan Savoy 				validate_firmware_response(kim_gdata);
155a0cc2f3bSPavan Savoy 				kim_gdata->rx_state = ST_W4_PACKET_TYPE;
156a0cc2f3bSPavan Savoy 				kim_gdata->rx_skb = NULL;
157a0cc2f3bSPavan Savoy 				continue;
158a0cc2f3bSPavan Savoy 				/* Waiting for Bluetooth event header ? */
1595c88b021SPavan Savoy 			case ST_W4_HEADER:
1605c88b021SPavan Savoy 				plen =
1615c88b021SPavan Savoy 				(unsigned char *)&kim_gdata->rx_skb->data[1];
1625c88b021SPavan Savoy 				pr_debug("event hdr: plen 0x%02x\n", *plen);
1635c88b021SPavan Savoy 				kim_check_data_len(kim_gdata, *plen);
164a0cc2f3bSPavan Savoy 				continue;
165a0cc2f3bSPavan Savoy 			}	/* end of switch */
166a0cc2f3bSPavan Savoy 		}		/* end of if rx_state */
167a0cc2f3bSPavan Savoy 		switch (*ptr) {
168a0cc2f3bSPavan Savoy 			/* Bluetooth event packet? */
1695c88b021SPavan Savoy 		case 0x04:
1705c88b021SPavan Savoy 			kim_gdata->rx_state = ST_W4_HEADER;
1715c88b021SPavan Savoy 			kim_gdata->rx_count = 2;
172a0cc2f3bSPavan Savoy 			break;
173a0cc2f3bSPavan Savoy 		default:
174a0cc2f3bSPavan Savoy 			pr_info("unknown packet");
175a0cc2f3bSPavan Savoy 			ptr++;
176a0cc2f3bSPavan Savoy 			count--;
177a0cc2f3bSPavan Savoy 			continue;
178a0cc2f3bSPavan Savoy 		}
179a0cc2f3bSPavan Savoy 		ptr++;
180a0cc2f3bSPavan Savoy 		count--;
181a0cc2f3bSPavan Savoy 		kim_gdata->rx_skb =
1825c88b021SPavan Savoy 			alloc_skb(1024+8, GFP_ATOMIC);
183a0cc2f3bSPavan Savoy 		if (!kim_gdata->rx_skb) {
184a0cc2f3bSPavan Savoy 			pr_err("can't allocate mem for new packet");
185a0cc2f3bSPavan Savoy 			kim_gdata->rx_state = ST_W4_PACKET_TYPE;
186a0cc2f3bSPavan Savoy 			kim_gdata->rx_count = 0;
187a0cc2f3bSPavan Savoy 			return;
188a0cc2f3bSPavan Savoy 		}
1895c88b021SPavan Savoy 		skb_reserve(kim_gdata->rx_skb, 8);
1905c88b021SPavan Savoy 		kim_gdata->rx_skb->cb[0] = 4;
1915c88b021SPavan Savoy 		kim_gdata->rx_skb->cb[1] = 0;
1925c88b021SPavan Savoy 
193a0cc2f3bSPavan Savoy 	}
194a0cc2f3bSPavan Savoy 	return;
195a0cc2f3bSPavan Savoy }
196a0cc2f3bSPavan Savoy 
read_local_version(struct kim_data_s * kim_gdata,char * bts_scr_name)197a0cc2f3bSPavan Savoy static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
198a0cc2f3bSPavan Savoy {
199a0cc2f3bSPavan Savoy 	unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
2004b4bdfe7SColin Ian King 	static const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
201*d97fb257SWolfram Sang 	long time_left;
202a0cc2f3bSPavan Savoy 
203a0cc2f3bSPavan Savoy 	pr_debug("%s", __func__);
204a0cc2f3bSPavan Savoy 
20516735d02SWolfram Sang 	reinit_completion(&kim_gdata->kim_rcvd);
206a0cc2f3bSPavan Savoy 	if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
207a0cc2f3bSPavan Savoy 		pr_err("kim: couldn't write 4 bytes");
20870442664SPavan Savoy 		return -EIO;
209a0cc2f3bSPavan Savoy 	}
210a0cc2f3bSPavan Savoy 
211*d97fb257SWolfram Sang 	time_left = wait_for_completion_interruptible_timeout(
2123f46d81aSNicholas Mc Guire 		&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME));
213*d97fb257SWolfram Sang 	if (time_left <= 0) {
2143f46d81aSNicholas Mc Guire 		pr_err(" waiting for ver info- timed out or received signal");
215*d97fb257SWolfram Sang 		return time_left ? -ERESTARTSYS : -ETIMEDOUT;
216a0cc2f3bSPavan Savoy 	}
21716735d02SWolfram Sang 	reinit_completion(&kim_gdata->kim_rcvd);
2183caf1b48SLee Jones 	/*
2193caf1b48SLee Jones 	 * the positions 12 & 13 in the response buffer provide with the
2208565adbcSPavan Savoy 	 * chip, major & minor numbers
2218565adbcSPavan Savoy 	 */
222a0cc2f3bSPavan Savoy 
223a0cc2f3bSPavan Savoy 	version =
2248565adbcSPavan Savoy 		MAKEWORD(kim_gdata->resp_buffer[12],
2258565adbcSPavan Savoy 				kim_gdata->resp_buffer[13]);
226a0cc2f3bSPavan Savoy 	chip = (version & 0x7C00) >> 10;
227a0cc2f3bSPavan Savoy 	min_ver = (version & 0x007F);
228a0cc2f3bSPavan Savoy 	maj_ver = (version & 0x0380) >> 7;
229a0cc2f3bSPavan Savoy 
230a0cc2f3bSPavan Savoy 	if (version & 0x8000)
231a0cc2f3bSPavan Savoy 		maj_ver |= 0x0008;
232a0cc2f3bSPavan Savoy 
2334dcc2ab3SEnric Balletbo i Serra 	sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
2344dcc2ab3SEnric Balletbo i Serra 		chip, maj_ver, min_ver);
235a0cc2f3bSPavan Savoy 
236a0cc2f3bSPavan Savoy 	/* to be accessed later via sysfs entry */
237a0cc2f3bSPavan Savoy 	kim_gdata->version.full = version;
238a0cc2f3bSPavan Savoy 	kim_gdata->version.chip = chip;
239a0cc2f3bSPavan Savoy 	kim_gdata->version.maj_ver = maj_ver;
240a0cc2f3bSPavan Savoy 	kim_gdata->version.min_ver = min_ver;
241a0cc2f3bSPavan Savoy 
242a0cc2f3bSPavan Savoy 	pr_info("%s", bts_scr_name);
243a0cc2f3bSPavan Savoy 	return 0;
244a0cc2f3bSPavan Savoy }
245a0cc2f3bSPavan Savoy 
skip_change_remote_baud(unsigned char ** ptr,long * len)24627712b39SPavan Savoy static void skip_change_remote_baud(unsigned char **ptr, long *len)
247ef04d121SPavan Savoy {
248ef04d121SPavan Savoy 	unsigned char *nxt_action, *cur_action;
249ef04d121SPavan Savoy 	cur_action = *ptr;
250ef04d121SPavan Savoy 
251ef04d121SPavan Savoy 	nxt_action = cur_action + sizeof(struct bts_action) +
252ef04d121SPavan Savoy 		((struct bts_action *) cur_action)->size;
253ef04d121SPavan Savoy 
254ef04d121SPavan Savoy 	if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
255ef04d121SPavan Savoy 		pr_err("invalid action after change remote baud command");
256ef04d121SPavan Savoy 	} else {
257ef04d121SPavan Savoy 		*ptr = *ptr + sizeof(struct bts_action) +
2589d031d94SShahar Lev 			((struct bts_action *)cur_action)->size;
259ef04d121SPavan Savoy 		*len = *len - (sizeof(struct bts_action) +
2609d031d94SShahar Lev 				((struct bts_action *)cur_action)->size);
261ef04d121SPavan Savoy 		/* warn user on not commenting these in firmware */
262ef04d121SPavan Savoy 		pr_warn("skipping the wait event of change remote baud");
263ef04d121SPavan Savoy 	}
264ef04d121SPavan Savoy }
265ef04d121SPavan Savoy 
2663caf1b48SLee Jones /*
267a0cc2f3bSPavan Savoy  * download_firmware -
268a0cc2f3bSPavan Savoy  *	internal function which parses through the .bts firmware
269a0cc2f3bSPavan Savoy  *	script file intreprets SEND, DELAY actions only as of now
270a0cc2f3bSPavan Savoy  */
download_firmware(struct kim_data_s * kim_gdata)271a0cc2f3bSPavan Savoy static long download_firmware(struct kim_data_s *kim_gdata)
272a0cc2f3bSPavan Savoy {
273a0cc2f3bSPavan Savoy 	long err = 0;
274a0cc2f3bSPavan Savoy 	long len = 0;
27573f12e8dSPavan Savoy 	unsigned char *ptr = NULL;
27673f12e8dSPavan Savoy 	unsigned char *action_ptr = NULL;
2774dcc2ab3SEnric Balletbo i Serra 	unsigned char bts_scr_name[40] = { 0 };	/* 40 char long bts scr name? */
278ef04d121SPavan Savoy 	int wr_room_space;
279ef04d121SPavan Savoy 	int cmd_size;
280ef04d121SPavan Savoy 	unsigned long timeout;
281a0cc2f3bSPavan Savoy 
282a0cc2f3bSPavan Savoy 	err = read_local_version(kim_gdata, bts_scr_name);
283a0cc2f3bSPavan Savoy 	if (err != 0) {
284a0cc2f3bSPavan Savoy 		pr_err("kim: failed to read local ver");
285a0cc2f3bSPavan Savoy 		return err;
286a0cc2f3bSPavan Savoy 	}
287a0cc2f3bSPavan Savoy 	err =
288a0cc2f3bSPavan Savoy 	    request_firmware(&kim_gdata->fw_entry, bts_scr_name,
289a0cc2f3bSPavan Savoy 			     &kim_gdata->kim_pdev->dev);
290a0cc2f3bSPavan Savoy 	if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
291a0cc2f3bSPavan Savoy 		     (kim_gdata->fw_entry->size == 0))) {
292a0cc2f3bSPavan Savoy 		pr_err(" request_firmware failed(errno %ld) for %s", err,
293a0cc2f3bSPavan Savoy 			   bts_scr_name);
29470442664SPavan Savoy 		return -EINVAL;
295a0cc2f3bSPavan Savoy 	}
296a0cc2f3bSPavan Savoy 	ptr = (void *)kim_gdata->fw_entry->data;
297a0cc2f3bSPavan Savoy 	len = kim_gdata->fw_entry->size;
2983caf1b48SLee Jones 	/*
2993caf1b48SLee Jones 	 * bts_header to remove out magic number and
300a0cc2f3bSPavan Savoy 	 * version
301a0cc2f3bSPavan Savoy 	 */
302a0cc2f3bSPavan Savoy 	ptr += sizeof(struct bts_header);
303a0cc2f3bSPavan Savoy 	len -= sizeof(struct bts_header);
304a0cc2f3bSPavan Savoy 
305a0cc2f3bSPavan Savoy 	while (len > 0 && ptr) {
306a0cc2f3bSPavan Savoy 		pr_debug(" action size %d, type %d ",
307a0cc2f3bSPavan Savoy 			   ((struct bts_action *)ptr)->size,
308a0cc2f3bSPavan Savoy 			   ((struct bts_action *)ptr)->type);
309a0cc2f3bSPavan Savoy 
310a0cc2f3bSPavan Savoy 		switch (((struct bts_action *)ptr)->type) {
311a0cc2f3bSPavan Savoy 		case ACTION_SEND_COMMAND:	/* action send */
3122f81a02cSPavan Savoy 			pr_debug("S");
313a0cc2f3bSPavan Savoy 			action_ptr = &(((struct bts_action *)ptr)->data[0]);
314a0cc2f3bSPavan Savoy 			if (unlikely
315a0cc2f3bSPavan Savoy 			    (((struct hci_command *)action_ptr)->opcode ==
316a0cc2f3bSPavan Savoy 			     0xFF36)) {
3173caf1b48SLee Jones 				/*
3183caf1b48SLee Jones 				 * ignore remote change
3193caf1b48SLee Jones 				 * baud rate HCI VS command
3203caf1b48SLee Jones 				 */
321ef04d121SPavan Savoy 				pr_warn("change remote baud"
322a0cc2f3bSPavan Savoy 				    " rate command in firmware");
323ef04d121SPavan Savoy 				skip_change_remote_baud(&ptr, &len);
324a0cc2f3bSPavan Savoy 				break;
325a0cc2f3bSPavan Savoy 			}
326ef04d121SPavan Savoy 			/*
327ef04d121SPavan Savoy 			 * Make sure we have enough free space in uart
328ef04d121SPavan Savoy 			 * tx buffer to write current firmware command
329ef04d121SPavan Savoy 			 */
330ef04d121SPavan Savoy 			cmd_size = ((struct bts_action *)ptr)->size;
331ef04d121SPavan Savoy 			timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
332ef04d121SPavan Savoy 			do {
333ef04d121SPavan Savoy 				wr_room_space =
334ef04d121SPavan Savoy 					st_get_uart_wr_room(kim_gdata->core_data);
335ef04d121SPavan Savoy 				if (wr_room_space < 0) {
336ef04d121SPavan Savoy 					pr_err("Unable to get free "
337ef04d121SPavan Savoy 							"space info from uart tx buffer");
338ef04d121SPavan Savoy 					release_firmware(kim_gdata->fw_entry);
339ef04d121SPavan Savoy 					return wr_room_space;
340ef04d121SPavan Savoy 				}
341ef04d121SPavan Savoy 				mdelay(1); /* wait 1ms before checking room */
342ef04d121SPavan Savoy 			} while ((wr_room_space < cmd_size) &&
343ef04d121SPavan Savoy 					time_before(jiffies, timeout));
344a0cc2f3bSPavan Savoy 
345ef04d121SPavan Savoy 			/* Timeout happened ? */
346ef04d121SPavan Savoy 			if (time_after_eq(jiffies, timeout)) {
347ef04d121SPavan Savoy 				pr_err("Timeout while waiting for free "
348ef04d121SPavan Savoy 						"free space in uart tx buffer");
349ef04d121SPavan Savoy 				release_firmware(kim_gdata->fw_entry);
350ef04d121SPavan Savoy 				return -ETIMEDOUT;
351ef04d121SPavan Savoy 			}
3523caf1b48SLee Jones 			/*
3533caf1b48SLee Jones 			 * reinit completion before sending for the
3542f81a02cSPavan Savoy 			 * relevant wait
3552f81a02cSPavan Savoy 			 */
35616735d02SWolfram Sang 			reinit_completion(&kim_gdata->kim_rcvd);
357ef04d121SPavan Savoy 
358ef04d121SPavan Savoy 			/*
359ef04d121SPavan Savoy 			 * Free space found in uart buffer, call st_int_write
360ef04d121SPavan Savoy 			 * to send current firmware command to the uart tx
361ef04d121SPavan Savoy 			 * buffer.
362ef04d121SPavan Savoy 			 */
363a0cc2f3bSPavan Savoy 			err = st_int_write(kim_gdata->core_data,
364a0cc2f3bSPavan Savoy 			((struct bts_action_send *)action_ptr)->data,
365a0cc2f3bSPavan Savoy 					   ((struct bts_action *)ptr)->size);
366a0cc2f3bSPavan Savoy 			if (unlikely(err < 0)) {
367a0cc2f3bSPavan Savoy 				release_firmware(kim_gdata->fw_entry);
36870442664SPavan Savoy 				return err;
369a0cc2f3bSPavan Savoy 			}
370ef04d121SPavan Savoy 			/*
371ef04d121SPavan Savoy 			 * Check number of bytes written to the uart tx buffer
372ef04d121SPavan Savoy 			 * and requested command write size
373ef04d121SPavan Savoy 			 */
374ef04d121SPavan Savoy 			if (err != cmd_size) {
375ef04d121SPavan Savoy 				pr_err("Number of bytes written to uart "
376ef04d121SPavan Savoy 						"tx buffer are not matching with "
377ef04d121SPavan Savoy 						"requested cmd write size");
378ef04d121SPavan Savoy 				release_firmware(kim_gdata->fw_entry);
379ef04d121SPavan Savoy 				return -EIO;
380ef04d121SPavan Savoy 			}
381ef04d121SPavan Savoy 			break;
382ef04d121SPavan Savoy 		case ACTION_WAIT_EVENT:  /* wait */
3832f81a02cSPavan Savoy 			pr_debug("W");
3843f46d81aSNicholas Mc Guire 			err = wait_for_completion_interruptible_timeout(
38553702358SPavan Savoy 					&kim_gdata->kim_rcvd,
3863f46d81aSNicholas Mc Guire 					msecs_to_jiffies(CMD_RESP_TIME));
3873f46d81aSNicholas Mc Guire 			if (err <= 0) {
3883f46d81aSNicholas Mc Guire 				pr_err("response timeout/signaled during fw download ");
389a0cc2f3bSPavan Savoy 				/* timed out */
390a0cc2f3bSPavan Savoy 				release_firmware(kim_gdata->fw_entry);
3913f46d81aSNicholas Mc Guire 				return err ? -ERESTARTSYS : -ETIMEDOUT;
392a0cc2f3bSPavan Savoy 			}
39316735d02SWolfram Sang 			reinit_completion(&kim_gdata->kim_rcvd);
394a0cc2f3bSPavan Savoy 			break;
395a0cc2f3bSPavan Savoy 		case ACTION_DELAY:	/* sleep */
396a0cc2f3bSPavan Savoy 			pr_info("sleep command in scr");
397a0cc2f3bSPavan Savoy 			action_ptr = &(((struct bts_action *)ptr)->data[0]);
398a0cc2f3bSPavan Savoy 			mdelay(((struct bts_action_delay *)action_ptr)->msec);
399a0cc2f3bSPavan Savoy 			break;
400a0cc2f3bSPavan Savoy 		}
401a0cc2f3bSPavan Savoy 		len =
402a0cc2f3bSPavan Savoy 		    len - (sizeof(struct bts_action) +
403a0cc2f3bSPavan Savoy 			   ((struct bts_action *)ptr)->size);
404a0cc2f3bSPavan Savoy 		ptr =
405a0cc2f3bSPavan Savoy 		    ptr + sizeof(struct bts_action) +
406a0cc2f3bSPavan Savoy 		    ((struct bts_action *)ptr)->size;
407a0cc2f3bSPavan Savoy 	}
408a0cc2f3bSPavan Savoy 	/* fw download complete */
409a0cc2f3bSPavan Savoy 	release_firmware(kim_gdata->fw_entry);
410a0cc2f3bSPavan Savoy 	return 0;
411a0cc2f3bSPavan Savoy }
412a0cc2f3bSPavan Savoy 
413a0cc2f3bSPavan Savoy /**********************************************************************/
414a0cc2f3bSPavan Savoy /* functions called from ST core */
415a0cc2f3bSPavan Savoy /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
416a0cc2f3bSPavan Savoy  * can be because of
417a0cc2f3bSPavan Savoy  * 1. response to read local version
418a0cc2f3bSPavan Savoy  * 2. during send/recv's of firmware download
419a0cc2f3bSPavan Savoy  */
st_kim_recv(void * disc_data,const u8 * data,size_t count)420ead03e72SJiri Slaby (SUSE) void st_kim_recv(void *disc_data, const u8 *data, size_t count)
421a0cc2f3bSPavan Savoy {
422a0cc2f3bSPavan Savoy 	struct st_data_s	*st_gdata = (struct st_data_s *)disc_data;
423a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata = st_gdata->kim_data;
424a0cc2f3bSPavan Savoy 
4253caf1b48SLee Jones 	/*
4263caf1b48SLee Jones 	 * proceed to gather all data and distinguish read fw version response
4278565adbcSPavan Savoy 	 * from other fw responses when data gathering is complete
4288565adbcSPavan Savoy 	 */
429a0cc2f3bSPavan Savoy 	kim_int_recv(kim_gdata, data, count);
430a0cc2f3bSPavan Savoy 	return;
431a0cc2f3bSPavan Savoy }
432a0cc2f3bSPavan Savoy 
4333caf1b48SLee Jones /*
4343caf1b48SLee Jones  * to signal completion of line discipline installation
435a0cc2f3bSPavan Savoy  * called from ST Core, upon tty_open
436a0cc2f3bSPavan Savoy  */
st_kim_complete(void * kim_data)437a0cc2f3bSPavan Savoy void st_kim_complete(void *kim_data)
438a0cc2f3bSPavan Savoy {
439a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
440a0cc2f3bSPavan Savoy 	complete(&kim_gdata->ldisc_installed);
441a0cc2f3bSPavan Savoy }
442a0cc2f3bSPavan Savoy 
4433caf1b48SLee Jones /*
444a0cc2f3bSPavan Savoy  * st_kim_start - called from ST Core upon 1st registration
445a0cc2f3bSPavan Savoy  *	This involves toggling the chip enable gpio, reading
446a0cc2f3bSPavan Savoy  *	the firmware version from chip, forming the fw file name
447a0cc2f3bSPavan Savoy  *	based on the chip version, requesting the fw, parsing it
448a0cc2f3bSPavan Savoy  *	and perform download(send/recv).
449a0cc2f3bSPavan Savoy  */
st_kim_start(void * kim_data)450a0cc2f3bSPavan Savoy long st_kim_start(void *kim_data)
451a0cc2f3bSPavan Savoy {
452a0cc2f3bSPavan Savoy 	long err = 0;
453a0cc2f3bSPavan Savoy 	long retry = POR_RETRY_COUNT;
4540d7c5f25SPavan Savoy 	struct ti_st_plat_data	*pdata;
455a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
456a0cc2f3bSPavan Savoy 
457a0cc2f3bSPavan Savoy 	pr_info(" %s", __func__);
4580d7c5f25SPavan Savoy 	pdata = kim_gdata->kim_pdev->dev.platform_data;
459a0cc2f3bSPavan Savoy 
460a0cc2f3bSPavan Savoy 	do {
4610d7c5f25SPavan Savoy 		/* platform specific enabling code here */
4620d7c5f25SPavan Savoy 		if (pdata->chip_enable)
4630d7c5f25SPavan Savoy 			pdata->chip_enable(kim_gdata);
4640d7c5f25SPavan Savoy 
465a7e2ca17SLuciano Coelho 		/* Configure BT nShutdown to HIGH state */
466513da46dSJürg Billeter 		gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
467a7e2ca17SLuciano Coelho 		mdelay(5);	/* FIXME: a proper toggle */
468513da46dSJürg Billeter 		gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
469a7e2ca17SLuciano Coelho 		mdelay(100);
470a0cc2f3bSPavan Savoy 		/* re-initialize the completion */
47116735d02SWolfram Sang 		reinit_completion(&kim_gdata->ldisc_installed);
472ec60d0adSPavan Savoy 		/* send notification to UIM */
473ec60d0adSPavan Savoy 		kim_gdata->ldisc_install = 1;
474ec60d0adSPavan Savoy 		pr_info("ldisc_install = 1");
475ec60d0adSPavan Savoy 		sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
476ec60d0adSPavan Savoy 				NULL, "install");
477a0cc2f3bSPavan Savoy 		/* wait for ldisc to be installed */
47853702358SPavan Savoy 		err = wait_for_completion_interruptible_timeout(
47953702358SPavan Savoy 			&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
48018ccecf9SPavan Savoy 		if (!err) {
4813caf1b48SLee Jones 			/*
4823caf1b48SLee Jones 			 * ldisc installation timeout,
4833caf1b48SLee Jones 			 * flush uart, power cycle BT_EN
4843caf1b48SLee Jones 			 */
48518ccecf9SPavan Savoy 			pr_err("ldisc installation timeout");
48618ccecf9SPavan Savoy 			err = st_kim_stop(kim_gdata);
487a0cc2f3bSPavan Savoy 			continue;
488a0cc2f3bSPavan Savoy 		} else {
489a0cc2f3bSPavan Savoy 			/* ldisc installed now */
490a0cc2f3bSPavan Savoy 			pr_info("line discipline installed");
491a0cc2f3bSPavan Savoy 			err = download_firmware(kim_gdata);
492a0cc2f3bSPavan Savoy 			if (err != 0) {
4933caf1b48SLee Jones 				/*
4943caf1b48SLee Jones 				 * ldisc installed but fw download failed,
4953caf1b48SLee Jones 				 * flush uart & power cycle BT_EN
4963caf1b48SLee Jones 				 */
497a0cc2f3bSPavan Savoy 				pr_err("download firmware failed");
49818ccecf9SPavan Savoy 				err = st_kim_stop(kim_gdata);
499a0cc2f3bSPavan Savoy 				continue;
500a0cc2f3bSPavan Savoy 			} else {	/* on success don't retry */
501a0cc2f3bSPavan Savoy 				break;
502a0cc2f3bSPavan Savoy 			}
503a0cc2f3bSPavan Savoy 		}
504a0cc2f3bSPavan Savoy 	} while (retry--);
505a0cc2f3bSPavan Savoy 	return err;
506a0cc2f3bSPavan Savoy }
507a0cc2f3bSPavan Savoy 
5083caf1b48SLee Jones /*
50918ccecf9SPavan Savoy  * st_kim_stop - stop communication with chip.
51018ccecf9SPavan Savoy  *	This can be called from ST Core/KIM, on the-
51118ccecf9SPavan Savoy  *	(a) last un-register when chip need not be powered there-after,
51218ccecf9SPavan Savoy  *	(b) upon failure to either install ldisc or download firmware.
51318ccecf9SPavan Savoy  *	The function is responsible to (a) notify UIM about un-installation,
51418ccecf9SPavan Savoy  *	(b) flush UART if the ldisc was installed.
515a7e2ca17SLuciano Coelho  *	(c) reset BT_EN - pull down nshutdown at the end.
516a7e2ca17SLuciano Coelho  *	(d) invoke platform's chip disabling routine.
517a0cc2f3bSPavan Savoy  */
st_kim_stop(void * kim_data)518a0cc2f3bSPavan Savoy long st_kim_stop(void *kim_data)
519a0cc2f3bSPavan Savoy {
520a0cc2f3bSPavan Savoy 	long err = 0;
521a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
522c0bd1b9eSRob Herring 	struct ti_st_plat_data	*pdata =
523c0bd1b9eSRob Herring 		kim_gdata->kim_pdev->dev.platform_data;
52418ccecf9SPavan Savoy 	struct tty_struct	*tty = kim_gdata->core_data->tty;
525a0cc2f3bSPavan Savoy 
52616735d02SWolfram Sang 	reinit_completion(&kim_gdata->ldisc_installed);
527ec60d0adSPavan Savoy 
52818ccecf9SPavan Savoy 	if (tty) {	/* can be called before ldisc is installed */
529ec60d0adSPavan Savoy 		/* Flush any pending characters in the driver and discipline. */
53018ccecf9SPavan Savoy 		tty_ldisc_flush(tty);
53118ccecf9SPavan Savoy 		tty_driver_flush_buffer(tty);
53218ccecf9SPavan Savoy 	}
533ec60d0adSPavan Savoy 
534ec60d0adSPavan Savoy 	/* send uninstall notification to UIM */
535ec60d0adSPavan Savoy 	pr_info("ldisc_install = 0");
536ec60d0adSPavan Savoy 	kim_gdata->ldisc_install = 0;
537ec60d0adSPavan Savoy 	sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
538a0cc2f3bSPavan Savoy 
539a0cc2f3bSPavan Savoy 	/* wait for ldisc to be un-installed */
54053702358SPavan Savoy 	err = wait_for_completion_interruptible_timeout(
54153702358SPavan Savoy 		&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
542a0cc2f3bSPavan Savoy 	if (!err) {		/* timeout */
543a0cc2f3bSPavan Savoy 		pr_err(" timed out waiting for ldisc to be un-installed");
544b64365a5SPavan Savoy 		err = -ETIMEDOUT;
545a0cc2f3bSPavan Savoy 	}
546a0cc2f3bSPavan Savoy 
547a7e2ca17SLuciano Coelho 	/* By default configure BT nShutdown to LOW state */
548513da46dSJürg Billeter 	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
549a7e2ca17SLuciano Coelho 	mdelay(1);
550513da46dSJürg Billeter 	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
551a7e2ca17SLuciano Coelho 	mdelay(1);
552513da46dSJürg Billeter 	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
553a7e2ca17SLuciano Coelho 
5540d7c5f25SPavan Savoy 	/* platform specific disable */
5550d7c5f25SPavan Savoy 	if (pdata->chip_disable)
5560d7c5f25SPavan Savoy 		pdata->chip_disable(kim_gdata);
557a0cc2f3bSPavan Savoy 	return err;
558a0cc2f3bSPavan Savoy }
559a0cc2f3bSPavan Savoy 
560a0cc2f3bSPavan Savoy /**********************************************************************/
561a0cc2f3bSPavan Savoy /* functions called from subsystems */
562a0cc2f3bSPavan Savoy /* called when debugfs entry is read from */
563a0cc2f3bSPavan Savoy 
version_show(struct seq_file * s,void * unused)5642796b43fSYangtao Li static int version_show(struct seq_file *s, void *unused)
565a0cc2f3bSPavan Savoy {
5665f8fcfc3SAtin Bainada 	struct kim_data_s *kim_gdata = s->private;
567a0cc2f3bSPavan Savoy 	seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
568a0cc2f3bSPavan Savoy 			kim_gdata->version.chip, kim_gdata->version.maj_ver,
569a0cc2f3bSPavan Savoy 			kim_gdata->version.min_ver);
570a0cc2f3bSPavan Savoy 	return 0;
571a0cc2f3bSPavan Savoy }
572a0cc2f3bSPavan Savoy 
list_show(struct seq_file * s,void * unused)5732796b43fSYangtao Li static int list_show(struct seq_file *s, void *unused)
574a0cc2f3bSPavan Savoy {
5755f8fcfc3SAtin Bainada 	struct kim_data_s *kim_gdata = s->private;
576a0cc2f3bSPavan Savoy 	kim_st_list_protocols(kim_gdata->core_data, s);
577a0cc2f3bSPavan Savoy 	return 0;
578a0cc2f3bSPavan Savoy }
579a0cc2f3bSPavan Savoy 
show_install(struct device * dev,struct device_attribute * attr,char * buf)580ec60d0adSPavan Savoy static ssize_t show_install(struct device *dev,
581ec60d0adSPavan Savoy 		struct device_attribute *attr, char *buf)
582a0cc2f3bSPavan Savoy {
583ec60d0adSPavan Savoy 	struct kim_data_s *kim_data = dev_get_drvdata(dev);
584ec60d0adSPavan Savoy 	return sprintf(buf, "%d\n", kim_data->ldisc_install);
585ec60d0adSPavan Savoy }
586a0cc2f3bSPavan Savoy 
587933aae54SPavan Savoy #ifdef DEBUG
store_dev_name(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)588933aae54SPavan Savoy static ssize_t store_dev_name(struct device *dev,
589933aae54SPavan Savoy 		struct device_attribute *attr, const char *buf, size_t count)
590933aae54SPavan Savoy {
591933aae54SPavan Savoy 	struct kim_data_s *kim_data = dev_get_drvdata(dev);
592933aae54SPavan Savoy 	pr_debug("storing dev name >%s<", buf);
59314388ec0SJustin Stitt 	strscpy(kim_data->dev_name, buf, sizeof(kim_data->dev_name));
594933aae54SPavan Savoy 	pr_debug("stored dev name >%s<", kim_data->dev_name);
595933aae54SPavan Savoy 	return count;
596933aae54SPavan Savoy }
597933aae54SPavan Savoy 
store_baud_rate(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)598933aae54SPavan Savoy static ssize_t store_baud_rate(struct device *dev,
599933aae54SPavan Savoy 		struct device_attribute *attr, const char *buf, size_t count)
600933aae54SPavan Savoy {
601933aae54SPavan Savoy 	struct kim_data_s *kim_data = dev_get_drvdata(dev);
602933aae54SPavan Savoy 	pr_debug("storing baud rate >%s<", buf);
603933aae54SPavan Savoy 	sscanf(buf, "%ld", &kim_data->baud_rate);
604933aae54SPavan Savoy 	pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
605933aae54SPavan Savoy 	return count;
606933aae54SPavan Savoy }
607933aae54SPavan Savoy #endif	/* if DEBUG */
608933aae54SPavan Savoy 
show_dev_name(struct device * dev,struct device_attribute * attr,char * buf)609ec60d0adSPavan Savoy static ssize_t show_dev_name(struct device *dev,
610ec60d0adSPavan Savoy 		struct device_attribute *attr, char *buf)
611ec60d0adSPavan Savoy {
612ec60d0adSPavan Savoy 	struct kim_data_s *kim_data = dev_get_drvdata(dev);
613ec60d0adSPavan Savoy 	return sprintf(buf, "%s\n", kim_data->dev_name);
614a0cc2f3bSPavan Savoy }
615ec60d0adSPavan Savoy 
show_baud_rate(struct device * dev,struct device_attribute * attr,char * buf)616ec60d0adSPavan Savoy static ssize_t show_baud_rate(struct device *dev,
617ec60d0adSPavan Savoy 		struct device_attribute *attr, char *buf)
618ec60d0adSPavan Savoy {
619ec60d0adSPavan Savoy 	struct kim_data_s *kim_data = dev_get_drvdata(dev);
62063e144c9SDan Carpenter 	return sprintf(buf, "%d\n", kim_data->baud_rate);
621a0cc2f3bSPavan Savoy }
622a0cc2f3bSPavan Savoy 
show_flow_cntrl(struct device * dev,struct device_attribute * attr,char * buf)623ec60d0adSPavan Savoy static ssize_t show_flow_cntrl(struct device *dev,
624ec60d0adSPavan Savoy 		struct device_attribute *attr, char *buf)
625ec60d0adSPavan Savoy {
626ec60d0adSPavan Savoy 	struct kim_data_s *kim_data = dev_get_drvdata(dev);
627ec60d0adSPavan Savoy 	return sprintf(buf, "%d\n", kim_data->flow_cntrl);
628ec60d0adSPavan Savoy }
629ec60d0adSPavan Savoy 
630ec60d0adSPavan Savoy /* structures specific for sysfs entries */
631ec60d0adSPavan Savoy static struct kobj_attribute ldisc_install =
632ec60d0adSPavan Savoy __ATTR(install, 0444, (void *)show_install, NULL);
633ec60d0adSPavan Savoy 
634ec60d0adSPavan Savoy static struct kobj_attribute uart_dev_name =
635933aae54SPavan Savoy #ifdef DEBUG	/* TODO: move this to debug-fs if possible */
636933aae54SPavan Savoy __ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
637933aae54SPavan Savoy #else
638ec60d0adSPavan Savoy __ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
639933aae54SPavan Savoy #endif
640ec60d0adSPavan Savoy 
641ec60d0adSPavan Savoy static struct kobj_attribute uart_baud_rate =
642933aae54SPavan Savoy #ifdef DEBUG	/* TODO: move to debugfs */
643933aae54SPavan Savoy __ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
644933aae54SPavan Savoy #else
645ec60d0adSPavan Savoy __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
646933aae54SPavan Savoy #endif
647ec60d0adSPavan Savoy 
648ec60d0adSPavan Savoy static struct kobj_attribute uart_flow_cntrl =
649ec60d0adSPavan Savoy __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
650ec60d0adSPavan Savoy 
651ec60d0adSPavan Savoy static struct attribute *uim_attrs[] = {
652ec60d0adSPavan Savoy 	&ldisc_install.attr,
653ec60d0adSPavan Savoy 	&uart_dev_name.attr,
654ec60d0adSPavan Savoy 	&uart_baud_rate.attr,
655ec60d0adSPavan Savoy 	&uart_flow_cntrl.attr,
656ec60d0adSPavan Savoy 	NULL,
657ec60d0adSPavan Savoy };
658ec60d0adSPavan Savoy 
6599045f182SArvind Yadav static const struct attribute_group uim_attr_grp = {
660ec60d0adSPavan Savoy 	.attrs = uim_attrs,
661ec60d0adSPavan Savoy };
662ec60d0adSPavan Savoy 
6633caf1b48SLee Jones /*
664a0cc2f3bSPavan Savoy  * st_kim_ref - reference the core's data
665a0cc2f3bSPavan Savoy  *	This references the per-ST platform device in the arch/xx/
666a0cc2f3bSPavan Savoy  *	board-xx.c file.
667a0cc2f3bSPavan Savoy  *	This would enable multiple such platform devices to exist
668a0cc2f3bSPavan Savoy  *	on a given platform
669a0cc2f3bSPavan Savoy  */
st_kim_ref(struct st_data_s ** core_data,int id)670a0cc2f3bSPavan Savoy void st_kim_ref(struct st_data_s **core_data, int id)
671a0cc2f3bSPavan Savoy {
672a0cc2f3bSPavan Savoy 	struct platform_device	*pdev;
673a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata;
674a0cc2f3bSPavan Savoy 	/* get kim_gdata reference from platform device */
675a0cc2f3bSPavan Savoy 	pdev = st_get_plat_device(id);
676868eba8eSGigi Joseph 	if (!pdev)
677868eba8eSGigi Joseph 		goto err;
6789093ca88SJingoo Han 	kim_gdata = platform_get_drvdata(pdev);
679868eba8eSGigi Joseph 	if (!kim_gdata)
680868eba8eSGigi Joseph 		goto err;
681868eba8eSGigi Joseph 
682a0cc2f3bSPavan Savoy 	*core_data = kim_gdata->core_data;
683868eba8eSGigi Joseph 	return;
684868eba8eSGigi Joseph err:
685868eba8eSGigi Joseph 	*core_data = NULL;
686a0cc2f3bSPavan Savoy }
687a0cc2f3bSPavan Savoy 
6882796b43fSYangtao Li DEFINE_SHOW_ATTRIBUTE(version);
6892796b43fSYangtao Li DEFINE_SHOW_ATTRIBUTE(list);
690a0cc2f3bSPavan Savoy 
691a0cc2f3bSPavan Savoy /**********************************************************************/
692a0cc2f3bSPavan Savoy /* functions called from platform device driver subsystem
693a0cc2f3bSPavan Savoy  * need to have a relevant platform device entry in the platform's
694a0cc2f3bSPavan Savoy  * board-*.c file
695a0cc2f3bSPavan Savoy  */
696a0cc2f3bSPavan Savoy 
69727712b39SPavan Savoy static struct dentry *kim_debugfs_dir;
kim_probe(struct platform_device * pdev)698a0cc2f3bSPavan Savoy static int kim_probe(struct platform_device *pdev)
699a0cc2f3bSPavan Savoy {
700a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata;
701c0bd1b9eSRob Herring 	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
702b2997387SMatthias Kaehlcke 	int err;
703a0cc2f3bSPavan Savoy 
704a0cc2f3bSPavan Savoy 	if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
705a0cc2f3bSPavan Savoy 		/* multiple devices could exist */
706a0cc2f3bSPavan Savoy 		st_kim_devices[pdev->id] = pdev;
707a0cc2f3bSPavan Savoy 	} else {
70825985edcSLucas De Marchi 		/* platform's sure about existence of 1 device */
709a0cc2f3bSPavan Savoy 		st_kim_devices[0] = pdev;
710a0cc2f3bSPavan Savoy 	}
711a0cc2f3bSPavan Savoy 
71224b6d3eeSJia-Ju Bai 	kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_KERNEL);
713a0cc2f3bSPavan Savoy 	if (!kim_gdata) {
714a0cc2f3bSPavan Savoy 		pr_err("no mem to allocate");
715a0cc2f3bSPavan Savoy 		return -ENOMEM;
716a0cc2f3bSPavan Savoy 	}
7179093ca88SJingoo Han 	platform_set_drvdata(pdev, kim_gdata);
718a0cc2f3bSPavan Savoy 
719b2997387SMatthias Kaehlcke 	err = st_core_init(&kim_gdata->core_data);
720b2997387SMatthias Kaehlcke 	if (err != 0) {
721a0cc2f3bSPavan Savoy 		pr_err(" ST core init failed");
722b2997387SMatthias Kaehlcke 		err = -EIO;
723b2997387SMatthias Kaehlcke 		goto err_core_init;
724a0cc2f3bSPavan Savoy 	}
725a0cc2f3bSPavan Savoy 	/* refer to itself */
726a0cc2f3bSPavan Savoy 	kim_gdata->core_data->kim_data = kim_gdata;
727a0cc2f3bSPavan Savoy 
728a7e2ca17SLuciano Coelho 	/* Claim the chip enable nShutdown gpio from the system */
729a7e2ca17SLuciano Coelho 	kim_gdata->nshutdown = pdata->nshutdown_gpio;
730a7e2ca17SLuciano Coelho 	err = gpio_request(kim_gdata->nshutdown, "kim");
731a7e2ca17SLuciano Coelho 	if (unlikely(err)) {
73263e144c9SDan Carpenter 		pr_err(" gpio %d request failed ", kim_gdata->nshutdown);
73381ae962dSAnton Vasilyev 		goto err_sysfs_group;
734a7e2ca17SLuciano Coelho 	}
735a7e2ca17SLuciano Coelho 
736a7e2ca17SLuciano Coelho 	/* Configure nShutdown GPIO as output=0 */
737a7e2ca17SLuciano Coelho 	err = gpio_direction_output(kim_gdata->nshutdown, 0);
738a7e2ca17SLuciano Coelho 	if (unlikely(err)) {
73963e144c9SDan Carpenter 		pr_err(" unable to configure gpio %d", kim_gdata->nshutdown);
74081ae962dSAnton Vasilyev 		goto err_sysfs_group;
741a7e2ca17SLuciano Coelho 	}
7423caf1b48SLee Jones 	/* get reference of pdev for request_firmware */
743a0cc2f3bSPavan Savoy 	kim_gdata->kim_pdev = pdev;
744a0cc2f3bSPavan Savoy 	init_completion(&kim_gdata->kim_rcvd);
745a0cc2f3bSPavan Savoy 	init_completion(&kim_gdata->ldisc_installed);
746a0cc2f3bSPavan Savoy 
747b2997387SMatthias Kaehlcke 	err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
748b2997387SMatthias Kaehlcke 	if (err) {
749ec60d0adSPavan Savoy 		pr_err("failed to create sysfs entries");
750b2997387SMatthias Kaehlcke 		goto err_sysfs_group;
751a0cc2f3bSPavan Savoy 	}
752ec60d0adSPavan Savoy 
753ec60d0adSPavan Savoy 	/* copying platform data */
75414388ec0SJustin Stitt 	strscpy(kim_gdata->dev_name, pdata->dev_name,
75514388ec0SJustin Stitt 		sizeof(kim_gdata->dev_name));
756ec60d0adSPavan Savoy 	kim_gdata->flow_cntrl = pdata->flow_cntrl;
757ec60d0adSPavan Savoy 	kim_gdata->baud_rate = pdata->baud_rate;
758ec60d0adSPavan Savoy 	pr_info("sysfs entries created\n");
759a0cc2f3bSPavan Savoy 
760a0cc2f3bSPavan Savoy 	kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
761a0cc2f3bSPavan Savoy 
762a0cc2f3bSPavan Savoy 	debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
7632796b43fSYangtao Li 				kim_gdata, &version_fops);
764a0cc2f3bSPavan Savoy 	debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
7652796b43fSYangtao Li 				kim_gdata, &list_fops);
766a0cc2f3bSPavan Savoy 	return 0;
767b2997387SMatthias Kaehlcke 
768b2997387SMatthias Kaehlcke err_sysfs_group:
769b2997387SMatthias Kaehlcke 	st_core_exit(kim_gdata->core_data);
770b2997387SMatthias Kaehlcke 
771b2997387SMatthias Kaehlcke err_core_init:
772b2997387SMatthias Kaehlcke 	kfree(kim_gdata);
773b2997387SMatthias Kaehlcke 
774b2997387SMatthias Kaehlcke 	return err;
775a0cc2f3bSPavan Savoy }
776a0cc2f3bSPavan Savoy 
kim_remove(struct platform_device * pdev)777f1a70d68SUwe Kleine-König static void kim_remove(struct platform_device *pdev)
778a0cc2f3bSPavan Savoy {
779a7e2ca17SLuciano Coelho 	/* free the GPIOs requested */
780c0bd1b9eSRob Herring 	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
781a0cc2f3bSPavan Savoy 	struct kim_data_s	*kim_gdata;
782a0cc2f3bSPavan Savoy 
7839093ca88SJingoo Han 	kim_gdata = platform_get_drvdata(pdev);
784a0cc2f3bSPavan Savoy 
7853caf1b48SLee Jones 	/*
7863caf1b48SLee Jones 	 * Free the Bluetooth/FM/GPIO
787a7e2ca17SLuciano Coelho 	 * nShutdown gpio from the system
788a7e2ca17SLuciano Coelho 	 */
789a7e2ca17SLuciano Coelho 	gpio_free(pdata->nshutdown_gpio);
790a7e2ca17SLuciano Coelho 	pr_info("nshutdown GPIO Freed");
791a7e2ca17SLuciano Coelho 
792781a7395SPavan Savoy 	debugfs_remove_recursive(kim_debugfs_dir);
793ec60d0adSPavan Savoy 	sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
794781a7395SPavan Savoy 	pr_info("sysfs entries removed");
795781a7395SPavan Savoy 
796a0cc2f3bSPavan Savoy 	kim_gdata->kim_pdev = NULL;
797a0cc2f3bSPavan Savoy 	st_core_exit(kim_gdata->core_data);
798a0cc2f3bSPavan Savoy 
799a0cc2f3bSPavan Savoy 	kfree(kim_gdata);
800a0cc2f3bSPavan Savoy 	kim_gdata = NULL;
801a0cc2f3bSPavan Savoy }
802a0cc2f3bSPavan Savoy 
kim_suspend(struct platform_device * pdev,pm_message_t state)80327712b39SPavan Savoy static int kim_suspend(struct platform_device *pdev, pm_message_t state)
804ec60d0adSPavan Savoy {
805c0bd1b9eSRob Herring 	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
806ec60d0adSPavan Savoy 
807ec60d0adSPavan Savoy 	if (pdata->suspend)
808ec60d0adSPavan Savoy 		return pdata->suspend(pdev, state);
809ec60d0adSPavan Savoy 
8104b4aa3abSGigi Joseph 	return 0;
811ec60d0adSPavan Savoy }
812ec60d0adSPavan Savoy 
kim_resume(struct platform_device * pdev)81327712b39SPavan Savoy static int kim_resume(struct platform_device *pdev)
814ec60d0adSPavan Savoy {
815c0bd1b9eSRob Herring 	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
816ec60d0adSPavan Savoy 
817ec60d0adSPavan Savoy 	if (pdata->resume)
818ec60d0adSPavan Savoy 		return pdata->resume(pdev);
819ec60d0adSPavan Savoy 
8204b4aa3abSGigi Joseph 	return 0;
821ec60d0adSPavan Savoy }
822ec60d0adSPavan Savoy 
823a0cc2f3bSPavan Savoy /**********************************************************************/
824a0cc2f3bSPavan Savoy /* entry point for ST KIM module, called in from ST Core */
825ec60d0adSPavan Savoy static struct platform_driver kim_platform_driver = {
826ec60d0adSPavan Savoy 	.probe = kim_probe,
827f1a70d68SUwe Kleine-König 	.remove_new = kim_remove,
828ec60d0adSPavan Savoy 	.suspend = kim_suspend,
829ec60d0adSPavan Savoy 	.resume = kim_resume,
830ec60d0adSPavan Savoy 	.driver = {
831ec60d0adSPavan Savoy 		.name = "kim",
832ec60d0adSPavan Savoy 	},
833ec60d0adSPavan Savoy };
834a0cc2f3bSPavan Savoy 
835b00e126fSAxel Lin module_platform_driver(kim_platform_driver);
836a0cc2f3bSPavan Savoy 
837a0cc2f3bSPavan Savoy MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
838a0cc2f3bSPavan Savoy MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
839a0cc2f3bSPavan Savoy MODULE_LICENSE("GPL");
840