blob: bc8a5718c0d78ec580bf28d5f1bfa5412fc1426d [file] [log] [blame]
Pavan Savoyd0088ce2010-04-08 13:16:54 -05001/*
2 * Shared Transport Line discipline driver Core
3 * Init Manager module responsible for GPIO control
4 * and firmware download
Pavan Savoya0cc2f32010-10-06 12:18:14 -04005 * Copyright (C) 2009-2010 Texas Instruments
6 * Author: Pavan Savoy <pavan_savoy@ti.com>
Pavan Savoyd0088ce2010-04-08 13:16:54 -05007 *
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>
Pavan Savoyc1afac12010-07-28 02:25:59 -050030#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050032#include <linux/sched.h>
Randy Dunlap773d6792011-04-26 09:18:51 -070033#include <linux/sysfs.h>
Pavan Savoyec60d0a2011-02-04 02:23:10 -060034#include <linux/tty.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050035
Pavan Savoy5c88b022011-02-04 02:23:09 -060036#include <linux/skbuff.h>
Pavan Savoye5558672010-09-30 16:13:30 -040037#include <linux/ti_wilink_st.h>
Paul Gortmakereb12a672011-07-03 15:14:56 -040038#include <linux/module.h>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040039
Pavan Savoyd0088ce2010-04-08 13:16:54 -050040
Pavan Savoydbd3a872010-08-19 14:08:51 -040041#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
Pavan Savoy73f12e82010-10-12 16:27:38 -040042static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050043
44/**********************************************************************/
45/* internal functions */
46
Pavan Savoy36b5aee2010-07-22 05:32:06 -050047/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040048 * st_get_plat_device -
49 * function which returns the reference to the platform device
50 * requested by id. As of now only 1 such device exists (id=0)
51 * the context requesting for reference can get the id to be
52 * requested by a. The protocol driver which is registering or
53 * b. the tty device which is opened.
54 */
55static struct platform_device *st_get_plat_device(int id)
56{
57 return st_kim_devices[id];
58}
59
60/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050061 * validate_firmware_response -
62 * function to return whether the firmware response was proper
63 * in case of error don't complete so that waiting for proper
64 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -050065 */
Pavan Savoy38d9df42010-07-08 08:55:17 -050066void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -050067{
Pavan Savoy38d9df42010-07-08 08:55:17 -050068 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -050069 if (unlikely(skb->data[5] != 0)) {
70 pr_err("no proper response during fw download");
71 pr_err("data6 %x", skb->data[5]);
Pavan Savoy76ff0e62011-08-10 10:18:36 -050072 kfree_skb(skb);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050073 return; /* keep waiting for the proper response */
74 }
75 /* becos of all the script being downloaded */
76 complete_all(&kim_gdata->kim_rcvd);
77 kfree_skb(skb);
78}
79
80/* check for data len received inside kim_int_recv
81 * most often hit the last case to update state to waiting for data
82 */
Pavan Savoy38d9df42010-07-08 08:55:17 -050083static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -050084{
85 register int room = skb_tailroom(kim_gdata->rx_skb);
86
Pavan Savoye6d9e642010-07-22 05:32:05 -050087 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050088
89 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -050090 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050091 } else if (len > room) {
92 /* Received packet's payload length is larger.
93 * We can't accommodate it in created skb.
94 */
95 pr_err("Data length is too large len %d room %d", len,
96 room);
97 kfree_skb(kim_gdata->rx_skb);
98 } else {
99 /* Packet header has non-zero payload length and
100 * we have enough space in created skb. Lets read
101 * payload data */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600102 kim_gdata->rx_state = ST_W4_DATA;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500103 kim_gdata->rx_count = len;
104 return len;
105 }
106
107 /* Change ST LL state to continue to process next
108 * packet */
109 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
110 kim_gdata->rx_skb = NULL;
111 kim_gdata->rx_count = 0;
112
113 return 0;
114}
115
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500116/**
117 * kim_int_recv - receive function called during firmware download
118 * firmware download responses on different UART drivers
119 * have been observed to come in bursts of different
120 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500121 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500122void kim_int_recv(struct kim_data_s *kim_gdata,
123 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500124{
Pavan Savoy73f12e82010-10-12 16:27:38 -0400125 const unsigned char *ptr;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400126 int len = 0, type = 0;
Pavan Savoy5c88b022011-02-04 02:23:09 -0600127 unsigned char *plen;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500128
Pavan Savoye6d9e642010-07-22 05:32:05 -0500129 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500130 /* Decode received bytes here */
Pavan Savoy73f12e82010-10-12 16:27:38 -0400131 ptr = data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500132 if (unlikely(ptr == NULL)) {
133 pr_err(" received null from TTY ");
134 return;
135 }
Pavan Savoy73f12e82010-10-12 16:27:38 -0400136
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500137 while (count) {
138 if (kim_gdata->rx_count) {
139 len = min_t(unsigned int, kim_gdata->rx_count, count);
140 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
141 kim_gdata->rx_count -= len;
142 count -= len;
143 ptr += len;
144
145 if (kim_gdata->rx_count)
146 continue;
147
148 /* Check ST RX state machine , where are we? */
149 switch (kim_gdata->rx_state) {
150 /* Waiting for complete packet ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600151 case ST_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500152 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500153 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500154 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
155 kim_gdata->rx_skb = NULL;
156 continue;
157 /* Waiting for Bluetooth event header ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600158 case ST_W4_HEADER:
159 plen =
160 (unsigned char *)&kim_gdata->rx_skb->data[1];
161 pr_debug("event hdr: plen 0x%02x\n", *plen);
162 kim_check_data_len(kim_gdata, *plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500163 continue;
164 } /* end of switch */
165 } /* end of if rx_state */
166 switch (*ptr) {
167 /* Bluetooth event packet? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600168 case 0x04:
169 kim_gdata->rx_state = ST_W4_HEADER;
170 kim_gdata->rx_count = 2;
171 type = *ptr;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500172 break;
173 default:
174 pr_info("unknown packet");
175 ptr++;
176 count--;
177 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500178 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500179 ptr++;
180 count--;
181 kim_gdata->rx_skb =
Pavan Savoy5c88b022011-02-04 02:23:09 -0600182 alloc_skb(1024+8, GFP_ATOMIC);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500183 if (!kim_gdata->rx_skb) {
184 pr_err("can't allocate mem for new packet");
185 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
186 kim_gdata->rx_count = 0;
187 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500188 }
Pavan Savoy5c88b022011-02-04 02:23:09 -0600189 skb_reserve(kim_gdata->rx_skb, 8);
190 kim_gdata->rx_skb->cb[0] = 4;
191 kim_gdata->rx_skb->cb[1] = 0;
192
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500193 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500194 return;
195}
196
Pavan Savoy38d9df42010-07-08 08:55:17 -0500197static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500198{
199 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400200 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500201
Pavan Savoye6d9e642010-07-22 05:32:05 -0500202 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500203
204 INIT_COMPLETION(kim_gdata->kim_rcvd);
205 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
206 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy70442662011-02-04 02:23:11 -0600207 return -EIO;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500208 }
209
210 if (!wait_for_completion_timeout
211 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
212 pr_err(" waiting for ver info- timed out ");
Pavan Savoy70442662011-02-04 02:23:11 -0600213 return -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500214 }
Pavan Savoy74a4fcf2011-08-10 10:18:32 -0500215 INIT_COMPLETION(kim_gdata->kim_rcvd);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500216
217 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500218 MAKEWORD(kim_gdata->resp_buffer[13],
219 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500220 chip = (version & 0x7C00) >> 10;
221 min_ver = (version & 0x007F);
222 maj_ver = (version & 0x0380) >> 7;
223
224 if (version & 0x8000)
225 maj_ver |= 0x0008;
226
227 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500228
229 /* to be accessed later via sysfs entry */
230 kim_gdata->version.full = version;
231 kim_gdata->version.chip = chip;
232 kim_gdata->version.maj_ver = maj_ver;
233 kim_gdata->version.min_ver = min_ver;
234
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500235 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500236 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500237}
238
Pavan Savoyef04d122011-02-04 02:23:13 -0600239void skip_change_remote_baud(unsigned char **ptr, long *len)
240{
241 unsigned char *nxt_action, *cur_action;
242 cur_action = *ptr;
243
244 nxt_action = cur_action + sizeof(struct bts_action) +
245 ((struct bts_action *) cur_action)->size;
246
247 if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
248 pr_err("invalid action after change remote baud command");
249 } else {
250 *ptr = *ptr + sizeof(struct bts_action) +
Shahar Lev9d031d92011-05-23 11:36:11 +0300251 ((struct bts_action *)cur_action)->size;
Pavan Savoyef04d122011-02-04 02:23:13 -0600252 *len = *len - (sizeof(struct bts_action) +
Shahar Lev9d031d92011-05-23 11:36:11 +0300253 ((struct bts_action *)cur_action)->size);
Pavan Savoyef04d122011-02-04 02:23:13 -0600254 /* warn user on not commenting these in firmware */
255 pr_warn("skipping the wait event of change remote baud");
256 }
257}
258
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500259/**
260 * download_firmware -
261 * internal function which parses through the .bts firmware
262 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500263 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500264static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500265{
Pavan Savoy320920c2010-07-14 08:21:12 -0500266 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500267 long len = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400268 unsigned char *ptr = NULL;
269 unsigned char *action_ptr = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500270 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
Pavan Savoyef04d122011-02-04 02:23:13 -0600271 int wr_room_space;
272 int cmd_size;
273 unsigned long timeout;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500274
Pavan Savoy38d9df42010-07-08 08:55:17 -0500275 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500276 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500277 pr_err("kim: failed to read local ver");
278 return err;
279 }
280 err =
281 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
282 &kim_gdata->kim_pdev->dev);
283 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
284 (kim_gdata->fw_entry->size == 0))) {
285 pr_err(" request_firmware failed(errno %ld) for %s", err,
286 bts_scr_name);
Pavan Savoy70442662011-02-04 02:23:11 -0600287 return -EINVAL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500288 }
289 ptr = (void *)kim_gdata->fw_entry->data;
290 len = kim_gdata->fw_entry->size;
291 /* bts_header to remove out magic number and
292 * version
293 */
294 ptr += sizeof(struct bts_header);
295 len -= sizeof(struct bts_header);
296
297 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500298 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500299 ((struct bts_action *)ptr)->size,
300 ((struct bts_action *)ptr)->type);
301
302 switch (((struct bts_action *)ptr)->type) {
303 case ACTION_SEND_COMMAND: /* action send */
Pavan Savoy2f81a022011-08-10 10:18:34 -0500304 pr_debug("S");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500305 action_ptr = &(((struct bts_action *)ptr)->data[0]);
306 if (unlikely
307 (((struct hci_command *)action_ptr)->opcode ==
308 0xFF36)) {
309 /* ignore remote change
310 * baud rate HCI VS command */
Pavan Savoyef04d122011-02-04 02:23:13 -0600311 pr_warn("change remote baud"
Pavan Savoye6d9e642010-07-22 05:32:05 -0500312 " rate command in firmware");
Pavan Savoyef04d122011-02-04 02:23:13 -0600313 skip_change_remote_baud(&ptr, &len);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500314 break;
315 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600316 /*
317 * Make sure we have enough free space in uart
318 * tx buffer to write current firmware command
319 */
320 cmd_size = ((struct bts_action *)ptr)->size;
321 timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
322 do {
323 wr_room_space =
324 st_get_uart_wr_room(kim_gdata->core_data);
325 if (wr_room_space < 0) {
326 pr_err("Unable to get free "
327 "space info from uart tx buffer");
328 release_firmware(kim_gdata->fw_entry);
329 return wr_room_space;
330 }
331 mdelay(1); /* wait 1ms before checking room */
332 } while ((wr_room_space < cmd_size) &&
333 time_before(jiffies, timeout));
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500334
Pavan Savoyef04d122011-02-04 02:23:13 -0600335 /* Timeout happened ? */
336 if (time_after_eq(jiffies, timeout)) {
337 pr_err("Timeout while waiting for free "
338 "free space in uart tx buffer");
339 release_firmware(kim_gdata->fw_entry);
340 return -ETIMEDOUT;
341 }
Pavan Savoy2f81a022011-08-10 10:18:34 -0500342 /* reinit completion before sending for the
343 * relevant wait
344 */
345 INIT_COMPLETION(kim_gdata->kim_rcvd);
Pavan Savoyef04d122011-02-04 02:23:13 -0600346
347 /*
348 * Free space found in uart buffer, call st_int_write
349 * to send current firmware command to the uart tx
350 * buffer.
351 */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500352 err = st_int_write(kim_gdata->core_data,
353 ((struct bts_action_send *)action_ptr)->data,
354 ((struct bts_action *)ptr)->size);
355 if (unlikely(err < 0)) {
356 release_firmware(kim_gdata->fw_entry);
Pavan Savoy70442662011-02-04 02:23:11 -0600357 return err;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500358 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600359 /*
360 * Check number of bytes written to the uart tx buffer
361 * and requested command write size
362 */
363 if (err != cmd_size) {
364 pr_err("Number of bytes written to uart "
365 "tx buffer are not matching with "
366 "requested cmd write size");
367 release_firmware(kim_gdata->fw_entry);
368 return -EIO;
369 }
370 break;
371 case ACTION_WAIT_EVENT: /* wait */
Pavan Savoy2f81a022011-08-10 10:18:34 -0500372 pr_debug("W");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500373 if (!wait_for_completion_timeout
Pavan Savoyef04d122011-02-04 02:23:13 -0600374 (&kim_gdata->kim_rcvd,
375 msecs_to_jiffies(CMD_RESP_TIME))) {
376 pr_err("response timeout during fw download ");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500377 /* timed out */
378 release_firmware(kim_gdata->fw_entry);
Pavan Savoy70442662011-02-04 02:23:11 -0600379 return -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500380 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600381 INIT_COMPLETION(kim_gdata->kim_rcvd);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500382 break;
383 case ACTION_DELAY: /* sleep */
384 pr_info("sleep command in scr");
385 action_ptr = &(((struct bts_action *)ptr)->data[0]);
386 mdelay(((struct bts_action_delay *)action_ptr)->msec);
387 break;
388 }
389 len =
390 len - (sizeof(struct bts_action) +
391 ((struct bts_action *)ptr)->size);
392 ptr =
393 ptr + sizeof(struct bts_action) +
394 ((struct bts_action *)ptr)->size;
395 }
396 /* fw download complete */
397 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500398 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500399}
400
401/**********************************************************************/
402/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500403/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
404 * can be because of
405 * 1. response to read local version
406 * 2. during send/recv's of firmware download
407 */
408void st_kim_recv(void *disc_data, const unsigned char *data, long count)
409{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500410 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
411 struct kim_data_s *kim_gdata = st_gdata->kim_data;
412
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500413 /* copy to local buffer */
414 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
415 /* must be the read_ver_cmd */
416 memcpy(kim_gdata->resp_buffer, data, count);
417 complete_all(&kim_gdata->kim_rcvd);
418 return;
419 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500420 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500421 /* either completes or times out */
422 }
423 return;
424}
425
426/* to signal completion of line discipline installation
427 * called from ST Core, upon tty_open
428 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500429void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500430{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500431 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500432 complete(&kim_gdata->ldisc_installed);
433}
434
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500435/**
436 * st_kim_start - called from ST Core upon 1st registration
437 * This involves toggling the chip enable gpio, reading
438 * the firmware version from chip, forming the fw file name
439 * based on the chip version, requesting the fw, parsing it
440 * and perform download(send/recv).
441 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500442long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500443{
Pavan Savoy320920c2010-07-14 08:21:12 -0500444 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500445 long retry = POR_RETRY_COUNT;
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500446 struct ti_st_plat_data *pdata;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500447 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
448
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500449 pr_info(" %s", __func__);
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500450 pdata = kim_gdata->kim_pdev->dev.platform_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500451
452 do {
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500453 /* platform specific enabling code here */
454 if (pdata->chip_enable)
455 pdata->chip_enable(kim_gdata);
456
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500457 /* Configure BT nShutdown to HIGH state */
Pavan Savoy781a7392011-02-04 02:23:15 -0600458 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500459 mdelay(5); /* FIXME: a proper toggle */
Pavan Savoy781a7392011-02-04 02:23:15 -0600460 gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500461 mdelay(100);
462 /* re-initialize the completion */
463 INIT_COMPLETION(kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600464 /* send notification to UIM */
465 kim_gdata->ldisc_install = 1;
466 pr_info("ldisc_install = 1");
467 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
468 NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500469 /* wait for ldisc to be installed */
470 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
471 msecs_to_jiffies(LDISC_TIME));
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600472 if (!err) {
473 /* ldisc installation timeout,
474 * flush uart, power cycle BT_EN */
475 pr_err("ldisc installation timeout");
476 err = st_kim_stop(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500477 continue;
478 } else {
479 /* ldisc installed now */
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600480 pr_info("line discipline installed");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500481 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500482 if (err != 0) {
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600483 /* ldisc installed but fw download failed,
484 * flush uart & power cycle BT_EN */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500485 pr_err("download firmware failed");
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600486 err = st_kim_stop(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500487 continue;
488 } else { /* on success don't retry */
489 break;
490 }
491 }
492 } while (retry--);
493 return err;
494}
495
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500496/**
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600497 * st_kim_stop - stop communication with chip.
498 * This can be called from ST Core/KIM, on the-
499 * (a) last un-register when chip need not be powered there-after,
500 * (b) upon failure to either install ldisc or download firmware.
501 * The function is responsible to (a) notify UIM about un-installation,
502 * (b) flush UART if the ldisc was installed.
503 * (c) reset BT_EN - pull down nshutdown at the end.
504 * (d) invoke platform's chip disabling routine.
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500505 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500506long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500507{
Pavan Savoy320920c2010-07-14 08:21:12 -0500508 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500509 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500510 struct ti_st_plat_data *pdata =
511 kim_gdata->kim_pdev->dev.platform_data;
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600512 struct tty_struct *tty = kim_gdata->core_data->tty;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500513
514 INIT_COMPLETION(kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600515
Pavan Savoy18ccecf2011-12-15 10:38:21 -0600516 if (tty) { /* can be called before ldisc is installed */
517 /* Flush any pending characters in the driver and discipline. */
518 tty_ldisc_flush(tty);
519 tty_driver_flush_buffer(tty);
520 tty->ops->flush_buffer(tty);
521 }
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600522
523 /* send uninstall notification to UIM */
524 pr_info("ldisc_install = 0");
525 kim_gdata->ldisc_install = 0;
526 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500527
528 /* wait for ldisc to be un-installed */
529 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
530 msecs_to_jiffies(LDISC_TIME));
531 if (!err) { /* timeout */
532 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy70442662011-02-04 02:23:11 -0600533 return -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500534 }
535
536 /* By default configure BT nShutdown to LOW state */
Pavan Savoy781a7392011-02-04 02:23:15 -0600537 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500538 mdelay(1);
Pavan Savoy781a7392011-02-04 02:23:15 -0600539 gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500540 mdelay(1);
Pavan Savoy781a7392011-02-04 02:23:15 -0600541 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
Pavan Savoy0d7c5f22011-08-10 10:18:31 -0500542
543 /* platform specific disable */
544 if (pdata->chip_disable)
545 pdata->chip_disable(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500546 return err;
547}
548
549/**********************************************************************/
550/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500551/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500552
Pavan Savoyc1afac12010-07-28 02:25:59 -0500553static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500554{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500555 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
556 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500557 kim_gdata->version.chip, kim_gdata->version.maj_ver,
558 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500559 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500560}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500561
Pavan Savoyc1afac12010-07-28 02:25:59 -0500562static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500563{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500564 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
565 kim_st_list_protocols(kim_gdata->core_data, s);
566 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500567}
568
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600569static ssize_t show_install(struct device *dev,
570 struct device_attribute *attr, char *buf)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500571{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600572 struct kim_data_s *kim_data = dev_get_drvdata(dev);
573 return sprintf(buf, "%d\n", kim_data->ldisc_install);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500574}
575
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600576static ssize_t show_dev_name(struct device *dev,
577 struct device_attribute *attr, char *buf)
578{
579 struct kim_data_s *kim_data = dev_get_drvdata(dev);
580 return sprintf(buf, "%s\n", kim_data->dev_name);
581}
582
583static ssize_t show_baud_rate(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
586 struct kim_data_s *kim_data = dev_get_drvdata(dev);
587 return sprintf(buf, "%ld\n", kim_data->baud_rate);
588}
589
590static ssize_t show_flow_cntrl(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
593 struct kim_data_s *kim_data = dev_get_drvdata(dev);
594 return sprintf(buf, "%d\n", kim_data->flow_cntrl);
595}
596
597/* structures specific for sysfs entries */
598static struct kobj_attribute ldisc_install =
599__ATTR(install, 0444, (void *)show_install, NULL);
600
601static struct kobj_attribute uart_dev_name =
602__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
603
604static struct kobj_attribute uart_baud_rate =
605__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
606
607static struct kobj_attribute uart_flow_cntrl =
608__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
609
610static struct attribute *uim_attrs[] = {
611 &ldisc_install.attr,
612 &uart_dev_name.attr,
613 &uart_baud_rate.attr,
614 &uart_flow_cntrl.attr,
615 NULL,
616};
617
618static struct attribute_group uim_attr_grp = {
619 .attrs = uim_attrs,
620};
621
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500622/**
623 * st_kim_ref - reference the core's data
624 * This references the per-ST platform device in the arch/xx/
625 * board-xx.c file.
626 * This would enable multiple such platform devices to exist
627 * on a given platform
628 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400629void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500630{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500631 struct platform_device *pdev;
632 struct kim_data_s *kim_gdata;
633 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400634 pdev = st_get_plat_device(id);
Steven Rostedt7316a9f2011-05-23 16:45:32 -0400635 if (!pdev) {
636 *core_data = NULL;
637 return;
638 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500639 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500640 *core_data = kim_gdata->core_data;
641}
642
Pavan Savoyc1afac12010-07-28 02:25:59 -0500643static int kim_version_open(struct inode *i, struct file *f)
644{
645 return single_open(f, show_version, i->i_private);
646}
647
648static int kim_list_open(struct inode *i, struct file *f)
649{
650 return single_open(f, show_list, i->i_private);
651}
652
653static const struct file_operations version_debugfs_fops = {
654 /* version info */
655 .open = kim_version_open,
656 .read = seq_read,
657 .llseek = seq_lseek,
658 .release = single_release,
659};
660static const struct file_operations list_debugfs_fops = {
661 /* protocols info */
662 .open = kim_list_open,
663 .read = seq_read,
664 .llseek = seq_lseek,
665 .release = single_release,
666};
667
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500668/**********************************************************************/
669/* functions called from platform device driver subsystem
670 * need to have a relevant platform device entry in the platform's
671 * board-*.c file
672 */
673
Pavan Savoyc1afac12010-07-28 02:25:59 -0500674struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500675static int kim_probe(struct platform_device *pdev)
676{
677 long status;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500678 struct kim_data_s *kim_gdata;
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600679 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500680
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400681 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
682 /* multiple devices could exist */
683 st_kim_devices[pdev->id] = pdev;
684 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300685 /* platform's sure about existence of 1 device */
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400686 st_kim_devices[0] = pdev;
687 }
688
Pavan Savoy38d9df42010-07-08 08:55:17 -0500689 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
690 if (!kim_gdata) {
691 pr_err("no mem to allocate");
692 return -ENOMEM;
693 }
694 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500695
696 status = st_core_init(&kim_gdata->core_data);
697 if (status != 0) {
698 pr_err(" ST core init failed");
Pavan Savoy70442662011-02-04 02:23:11 -0600699 return -EIO;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500700 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500701 /* refer to itself */
702 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500703
Pavan Savoy781a7392011-02-04 02:23:15 -0600704 /* Claim the chip enable nShutdown gpio from the system */
705 kim_gdata->nshutdown = pdata->nshutdown_gpio;
706 status = gpio_request(kim_gdata->nshutdown, "kim");
707 if (unlikely(status)) {
708 pr_err(" gpio %ld request failed ", kim_gdata->nshutdown);
709 return status;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500710 }
711
Pavan Savoy781a7392011-02-04 02:23:15 -0600712 /* Configure nShutdown GPIO as output=0 */
713 status = gpio_direction_output(kim_gdata->nshutdown, 0);
714 if (unlikely(status)) {
715 pr_err(" unable to configure gpio %ld", kim_gdata->nshutdown);
716 return status;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500717 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500718 /* get reference of pdev for request_firmware
719 */
720 kim_gdata->kim_pdev = pdev;
721 init_completion(&kim_gdata->kim_rcvd);
722 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500723
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600724 status = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
725 if (status) {
726 pr_err("failed to create sysfs entries");
727 return status;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500728 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500729
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600730 /* copying platform data */
731 strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
732 kim_gdata->flow_cntrl = pdata->flow_cntrl;
733 kim_gdata->baud_rate = pdata->baud_rate;
734 pr_info("sysfs entries created\n");
735
Pavan Savoyc1afac12010-07-28 02:25:59 -0500736 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
737 if (IS_ERR(kim_debugfs_dir)) {
738 pr_err(" debugfs entries creation failed ");
739 kim_debugfs_dir = NULL;
Pavan Savoy70442662011-02-04 02:23:11 -0600740 return -EIO;
Naveen Jaine2a53282010-06-09 03:45:33 -0500741 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500742
743 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
744 kim_gdata, &version_debugfs_fops);
745 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
746 kim_gdata, &list_debugfs_fops);
747 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500748 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500749}
750
751static int kim_remove(struct platform_device *pdev)
752{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600753 /* free the GPIOs requested */
754 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500755 struct kim_data_s *kim_gdata;
756
757 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500758
Pavan Savoy781a7392011-02-04 02:23:15 -0600759 /* Free the Bluetooth/FM/GPIO
760 * nShutdown gpio from the system
761 */
762 gpio_free(pdata->nshutdown_gpio);
763 pr_info("nshutdown GPIO Freed");
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600764
Pavan Savoy781a7392011-02-04 02:23:15 -0600765 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600766 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoy781a7392011-02-04 02:23:15 -0600767 pr_info("sysfs entries removed");
768
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500769 kim_gdata->kim_pdev = NULL;
770 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500771
772 kfree(kim_gdata);
773 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500774 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500775}
776
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600777int kim_suspend(struct platform_device *pdev, pm_message_t state)
778{
779 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
780
781 if (pdata->suspend)
782 return pdata->suspend(pdev, state);
783
784 return -EOPNOTSUPP;
785}
786
787int kim_resume(struct platform_device *pdev)
788{
789 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
790
791 if (pdata->resume)
792 return pdata->resume(pdev);
793
794 return -EOPNOTSUPP;
795}
796
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500797/**********************************************************************/
798/* entry point for ST KIM module, called in from ST Core */
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600799static struct platform_driver kim_platform_driver = {
800 .probe = kim_probe,
801 .remove = kim_remove,
802 .suspend = kim_suspend,
803 .resume = kim_resume,
804 .driver = {
805 .name = "kim",
806 .owner = THIS_MODULE,
807 },
808};
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500809
810static int __init st_kim_init(void)
811{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600812 return platform_driver_register(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500813}
814
815static void __exit st_kim_deinit(void)
816{
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500817 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500818}
819
820
821module_init(st_kim_init);
822module_exit(st_kim_deinit);
823MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
824MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
825MODULE_LICENSE("GPL");