blob: 2c096ccd53b0f5b592bf3e1fdc79bcc239cda70a [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>
Pavan Savoyec60d0a2011-02-04 02:23:10 -060033#include <linux/tty.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050034
Pavan Savoy5c88b022011-02-04 02:23:09 -060035#include <linux/skbuff.h>
Pavan Savoye5558672010-09-30 16:13:30 -040036#include <linux/ti_wilink_st.h>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040037
Pavan Savoyd0088ce2010-04-08 13:16:54 -050038
Pavan Savoydbd3a872010-08-19 14:08:51 -040039#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
Pavan Savoy73f12e82010-10-12 16:27:38 -040040static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050041
42/**********************************************************************/
43/* internal functions */
44
Pavan Savoy36b5aee2010-07-22 05:32:06 -050045/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040046 * st_get_plat_device -
47 * function which returns the reference to the platform device
48 * requested by id. As of now only 1 such device exists (id=0)
49 * the context requesting for reference can get the id to be
50 * requested by a. The protocol driver which is registering or
51 * b. the tty device which is opened.
52 */
53static struct platform_device *st_get_plat_device(int id)
54{
55 return st_kim_devices[id];
56}
57
58/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050059 * validate_firmware_response -
60 * function to return whether the firmware response was proper
61 * in case of error don't complete so that waiting for proper
62 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -050063 */
Pavan Savoy38d9df42010-07-08 08:55:17 -050064void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -050065{
Pavan Savoy38d9df42010-07-08 08:55:17 -050066 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -050067 if (unlikely(skb->data[5] != 0)) {
68 pr_err("no proper response during fw download");
69 pr_err("data6 %x", skb->data[5]);
70 return; /* keep waiting for the proper response */
71 }
72 /* becos of all the script being downloaded */
73 complete_all(&kim_gdata->kim_rcvd);
74 kfree_skb(skb);
75}
76
77/* check for data len received inside kim_int_recv
78 * most often hit the last case to update state to waiting for data
79 */
Pavan Savoy38d9df42010-07-08 08:55:17 -050080static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -050081{
82 register int room = skb_tailroom(kim_gdata->rx_skb);
83
Pavan Savoye6d9e642010-07-22 05:32:05 -050084 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050085
86 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -050087 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -050088 } else if (len > room) {
89 /* Received packet's payload length is larger.
90 * We can't accommodate it in created skb.
91 */
92 pr_err("Data length is too large len %d room %d", len,
93 room);
94 kfree_skb(kim_gdata->rx_skb);
95 } else {
96 /* Packet header has non-zero payload length and
97 * we have enough space in created skb. Lets read
98 * payload data */
Pavan Savoy5c88b022011-02-04 02:23:09 -060099 kim_gdata->rx_state = ST_W4_DATA;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500100 kim_gdata->rx_count = len;
101 return len;
102 }
103
104 /* Change ST LL state to continue to process next
105 * packet */
106 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
107 kim_gdata->rx_skb = NULL;
108 kim_gdata->rx_count = 0;
109
110 return 0;
111}
112
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500113/**
114 * kim_int_recv - receive function called during firmware download
115 * firmware download responses on different UART drivers
116 * have been observed to come in bursts of different
117 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500118 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500119void kim_int_recv(struct kim_data_s *kim_gdata,
120 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500121{
Pavan Savoy73f12e82010-10-12 16:27:38 -0400122 const unsigned char *ptr;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400123 int len = 0, type = 0;
Pavan Savoy5c88b022011-02-04 02:23:09 -0600124 unsigned char *plen;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500125
Pavan Savoye6d9e642010-07-22 05:32:05 -0500126 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500127 /* Decode received bytes here */
Pavan Savoy73f12e82010-10-12 16:27:38 -0400128 ptr = data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500129 if (unlikely(ptr == NULL)) {
130 pr_err(" received null from TTY ");
131 return;
132 }
Pavan Savoy73f12e82010-10-12 16:27:38 -0400133
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500134 while (count) {
135 if (kim_gdata->rx_count) {
136 len = min_t(unsigned int, kim_gdata->rx_count, count);
137 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
138 kim_gdata->rx_count -= len;
139 count -= len;
140 ptr += len;
141
142 if (kim_gdata->rx_count)
143 continue;
144
145 /* Check ST RX state machine , where are we? */
146 switch (kim_gdata->rx_state) {
147 /* Waiting for complete packet ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600148 case ST_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500149 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500150 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500151 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
152 kim_gdata->rx_skb = NULL;
153 continue;
154 /* Waiting for Bluetooth event header ? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600155 case ST_W4_HEADER:
156 plen =
157 (unsigned char *)&kim_gdata->rx_skb->data[1];
158 pr_debug("event hdr: plen 0x%02x\n", *plen);
159 kim_check_data_len(kim_gdata, *plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500160 continue;
161 } /* end of switch */
162 } /* end of if rx_state */
163 switch (*ptr) {
164 /* Bluetooth event packet? */
Pavan Savoy5c88b022011-02-04 02:23:09 -0600165 case 0x04:
166 kim_gdata->rx_state = ST_W4_HEADER;
167 kim_gdata->rx_count = 2;
168 type = *ptr;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500169 break;
170 default:
171 pr_info("unknown packet");
172 ptr++;
173 count--;
174 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500175 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500176 ptr++;
177 count--;
178 kim_gdata->rx_skb =
Pavan Savoy5c88b022011-02-04 02:23:09 -0600179 alloc_skb(1024+8, GFP_ATOMIC);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500180 if (!kim_gdata->rx_skb) {
181 pr_err("can't allocate mem for new packet");
182 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
183 kim_gdata->rx_count = 0;
184 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500185 }
Pavan Savoy5c88b022011-02-04 02:23:09 -0600186 skb_reserve(kim_gdata->rx_skb, 8);
187 kim_gdata->rx_skb->cb[0] = 4;
188 kim_gdata->rx_skb->cb[1] = 0;
189
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500190 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500191 return;
192}
193
Pavan Savoy38d9df42010-07-08 08:55:17 -0500194static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500195{
196 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400197 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500198
Pavan Savoye6d9e642010-07-22 05:32:05 -0500199 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500200
201 INIT_COMPLETION(kim_gdata->kim_rcvd);
202 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
203 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy70442662011-02-04 02:23:11 -0600204 return -EIO;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500205 }
206
207 if (!wait_for_completion_timeout
208 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
209 pr_err(" waiting for ver info- timed out ");
Pavan Savoy70442662011-02-04 02:23:11 -0600210 return -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500211 }
212
213 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500214 MAKEWORD(kim_gdata->resp_buffer[13],
215 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500216 chip = (version & 0x7C00) >> 10;
217 min_ver = (version & 0x007F);
218 maj_ver = (version & 0x0380) >> 7;
219
220 if (version & 0x8000)
221 maj_ver |= 0x0008;
222
223 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500224
225 /* to be accessed later via sysfs entry */
226 kim_gdata->version.full = version;
227 kim_gdata->version.chip = chip;
228 kim_gdata->version.maj_ver = maj_ver;
229 kim_gdata->version.min_ver = min_ver;
230
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500231 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500232 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500233}
234
Pavan Savoyef04d122011-02-04 02:23:13 -0600235void skip_change_remote_baud(unsigned char **ptr, long *len)
236{
237 unsigned char *nxt_action, *cur_action;
238 cur_action = *ptr;
239
240 nxt_action = cur_action + sizeof(struct bts_action) +
241 ((struct bts_action *) cur_action)->size;
242
243 if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
244 pr_err("invalid action after change remote baud command");
245 } else {
246 *ptr = *ptr + sizeof(struct bts_action) +
247 ((struct bts_action *)nxt_action)->size;
248 *len = *len - (sizeof(struct bts_action) +
249 ((struct bts_action *)nxt_action)->size);
250 /* warn user on not commenting these in firmware */
251 pr_warn("skipping the wait event of change remote baud");
252 }
253}
254
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500255/**
256 * download_firmware -
257 * internal function which parses through the .bts firmware
258 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500259 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500260static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500261{
Pavan Savoy320920c2010-07-14 08:21:12 -0500262 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500263 long len = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400264 unsigned char *ptr = NULL;
265 unsigned char *action_ptr = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500266 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
Pavan Savoyef04d122011-02-04 02:23:13 -0600267 int wr_room_space;
268 int cmd_size;
269 unsigned long timeout;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500270
Pavan Savoy38d9df42010-07-08 08:55:17 -0500271 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500272 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500273 pr_err("kim: failed to read local ver");
274 return err;
275 }
276 err =
277 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
278 &kim_gdata->kim_pdev->dev);
279 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
280 (kim_gdata->fw_entry->size == 0))) {
281 pr_err(" request_firmware failed(errno %ld) for %s", err,
282 bts_scr_name);
Pavan Savoy70442662011-02-04 02:23:11 -0600283 return -EINVAL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500284 }
285 ptr = (void *)kim_gdata->fw_entry->data;
286 len = kim_gdata->fw_entry->size;
287 /* bts_header to remove out magic number and
288 * version
289 */
290 ptr += sizeof(struct bts_header);
291 len -= sizeof(struct bts_header);
292
293 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500294 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500295 ((struct bts_action *)ptr)->size,
296 ((struct bts_action *)ptr)->type);
297
298 switch (((struct bts_action *)ptr)->type) {
299 case ACTION_SEND_COMMAND: /* action send */
300 action_ptr = &(((struct bts_action *)ptr)->data[0]);
301 if (unlikely
302 (((struct hci_command *)action_ptr)->opcode ==
303 0xFF36)) {
304 /* ignore remote change
305 * baud rate HCI VS command */
Pavan Savoyef04d122011-02-04 02:23:13 -0600306 pr_warn("change remote baud"
Pavan Savoye6d9e642010-07-22 05:32:05 -0500307 " rate command in firmware");
Pavan Savoyef04d122011-02-04 02:23:13 -0600308 skip_change_remote_baud(&ptr, &len);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500309 break;
310 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600311 /*
312 * Make sure we have enough free space in uart
313 * tx buffer to write current firmware command
314 */
315 cmd_size = ((struct bts_action *)ptr)->size;
316 timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
317 do {
318 wr_room_space =
319 st_get_uart_wr_room(kim_gdata->core_data);
320 if (wr_room_space < 0) {
321 pr_err("Unable to get free "
322 "space info from uart tx buffer");
323 release_firmware(kim_gdata->fw_entry);
324 return wr_room_space;
325 }
326 mdelay(1); /* wait 1ms before checking room */
327 } while ((wr_room_space < cmd_size) &&
328 time_before(jiffies, timeout));
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500329
Pavan Savoyef04d122011-02-04 02:23:13 -0600330 /* Timeout happened ? */
331 if (time_after_eq(jiffies, timeout)) {
332 pr_err("Timeout while waiting for free "
333 "free space in uart tx buffer");
334 release_firmware(kim_gdata->fw_entry);
335 return -ETIMEDOUT;
336 }
337
338 /*
339 * Free space found in uart buffer, call st_int_write
340 * to send current firmware command to the uart tx
341 * buffer.
342 */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500343 err = st_int_write(kim_gdata->core_data,
344 ((struct bts_action_send *)action_ptr)->data,
345 ((struct bts_action *)ptr)->size);
346 if (unlikely(err < 0)) {
347 release_firmware(kim_gdata->fw_entry);
Pavan Savoy70442662011-02-04 02:23:11 -0600348 return err;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500349 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600350 /*
351 * Check number of bytes written to the uart tx buffer
352 * and requested command write size
353 */
354 if (err != cmd_size) {
355 pr_err("Number of bytes written to uart "
356 "tx buffer are not matching with "
357 "requested cmd write size");
358 release_firmware(kim_gdata->fw_entry);
359 return -EIO;
360 }
361 break;
362 case ACTION_WAIT_EVENT: /* wait */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500363 if (!wait_for_completion_timeout
Pavan Savoyef04d122011-02-04 02:23:13 -0600364 (&kim_gdata->kim_rcvd,
365 msecs_to_jiffies(CMD_RESP_TIME))) {
366 pr_err("response timeout during fw download ");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500367 /* timed out */
368 release_firmware(kim_gdata->fw_entry);
Pavan Savoy70442662011-02-04 02:23:11 -0600369 return -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500370 }
Pavan Savoyef04d122011-02-04 02:23:13 -0600371 INIT_COMPLETION(kim_gdata->kim_rcvd);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500372 break;
373 case ACTION_DELAY: /* sleep */
374 pr_info("sleep command in scr");
375 action_ptr = &(((struct bts_action *)ptr)->data[0]);
376 mdelay(((struct bts_action_delay *)action_ptr)->msec);
377 break;
378 }
379 len =
380 len - (sizeof(struct bts_action) +
381 ((struct bts_action *)ptr)->size);
382 ptr =
383 ptr + sizeof(struct bts_action) +
384 ((struct bts_action *)ptr)->size;
385 }
386 /* fw download complete */
387 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500388 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500389}
390
391/**********************************************************************/
392/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500393/* function to toggle the GPIO
394 * needs to know whether the GPIO is active high or active low
395 */
396void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
397{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500398 struct platform_device *kim_pdev;
399 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500400 pr_info(" %s ", __func__);
401
Pavan Savoydbd3a872010-08-19 14:08:51 -0400402 kim_pdev = st_get_plat_device(0);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500403 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
404
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500405 if (kim_gdata->gpios[type] == -1) {
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600406 pr_info("gpio not requested for protocol %d", type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500407 return;
408 }
409 switch (type) {
410 case ST_BT:
411 /*Do Nothing */
412 break;
413
414 case ST_FM:
415 if (state == KIM_GPIO_ACTIVE)
416 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
417 else
418 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
419 break;
420
421 case ST_GPS:
422 if (state == KIM_GPIO_ACTIVE)
423 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
424 else
425 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
426 break;
427
Pavan Savoy5c88b022011-02-04 02:23:09 -0600428 case ST_MAX_CHANNELS:
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500429 default:
430 break;
431 }
432
433 return;
434}
435
436/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
437 * can be because of
438 * 1. response to read local version
439 * 2. during send/recv's of firmware download
440 */
441void st_kim_recv(void *disc_data, const unsigned char *data, long count)
442{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500443 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
444 struct kim_data_s *kim_gdata = st_gdata->kim_data;
445
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500446 /* copy to local buffer */
447 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
448 /* must be the read_ver_cmd */
449 memcpy(kim_gdata->resp_buffer, data, count);
450 complete_all(&kim_gdata->kim_rcvd);
451 return;
452 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500453 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500454 /* either completes or times out */
455 }
456 return;
457}
458
459/* to signal completion of line discipline installation
460 * called from ST Core, upon tty_open
461 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500462void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500463{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500464 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500465 complete(&kim_gdata->ldisc_installed);
466}
467
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500468/**
469 * st_kim_start - called from ST Core upon 1st registration
470 * This involves toggling the chip enable gpio, reading
471 * the firmware version from chip, forming the fw file name
472 * based on the chip version, requesting the fw, parsing it
473 * and perform download(send/recv).
474 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500475long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500476{
Pavan Savoy320920c2010-07-14 08:21:12 -0500477 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500478 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500479 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
480
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500481 pr_info(" %s", __func__);
482
483 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500484 /* Configure BT nShutdown to HIGH state */
485 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
486 mdelay(5); /* FIXME: a proper toggle */
487 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
488 mdelay(100);
489 /* re-initialize the completion */
490 INIT_COMPLETION(kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600491 /* send notification to UIM */
492 kim_gdata->ldisc_install = 1;
493 pr_info("ldisc_install = 1");
494 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
495 NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500496 /* wait for ldisc to be installed */
497 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
498 msecs_to_jiffies(LDISC_TIME));
499 if (!err) { /* timeout */
500 pr_err("line disc installation timed out ");
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600501 kim_gdata->ldisc_install = 0;
502 pr_info("ldisc_install = 0");
503 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
504 NULL, "install");
Pavan Savoy70442662011-02-04 02:23:11 -0600505 err = -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500506 continue;
507 } else {
508 /* ldisc installed now */
509 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500510 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500511 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500512 pr_err("download firmware failed");
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600513 kim_gdata->ldisc_install = 0;
514 pr_info("ldisc_install = 0");
515 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
516 NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500517 continue;
518 } else { /* on success don't retry */
519 break;
520 }
521 }
522 } while (retry--);
523 return err;
524}
525
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500526/**
527 * st_kim_stop - called from ST Core, on the last un-registration
528 * toggle low the chip enable gpio
529 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500530long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500531{
Pavan Savoy320920c2010-07-14 08:21:12 -0500532 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500533 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500534
535 INIT_COMPLETION(kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600536
537 /* Flush any pending characters in the driver and discipline. */
538 tty_ldisc_flush(kim_gdata->core_data->tty);
539 tty_driver_flush_buffer(kim_gdata->core_data->tty);
540
541 /* send uninstall notification to UIM */
542 pr_info("ldisc_install = 0");
543 kim_gdata->ldisc_install = 0;
544 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500545
546 /* wait for ldisc to be un-installed */
547 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
548 msecs_to_jiffies(LDISC_TIME));
549 if (!err) { /* timeout */
550 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy70442662011-02-04 02:23:11 -0600551 return -ETIMEDOUT;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500552 }
553
554 /* By default configure BT nShutdown to LOW state */
555 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
556 mdelay(1);
557 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
558 mdelay(1);
559 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
560 return err;
561}
562
563/**********************************************************************/
564/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500565/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500566
Pavan Savoyc1afac12010-07-28 02:25:59 -0500567static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500568{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500569 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
570 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500571 kim_gdata->version.chip, kim_gdata->version.maj_ver,
572 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500573 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500574}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500575
Pavan Savoyc1afac12010-07-28 02:25:59 -0500576static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500577{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500578 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
579 kim_st_list_protocols(kim_gdata->core_data, s);
580 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500581}
582
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600583static ssize_t show_install(struct device *dev,
584 struct device_attribute *attr, char *buf)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500585{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600586 struct kim_data_s *kim_data = dev_get_drvdata(dev);
587 return sprintf(buf, "%d\n", kim_data->ldisc_install);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500588}
589
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600590static ssize_t show_dev_name(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, "%s\n", kim_data->dev_name);
595}
596
597static ssize_t show_baud_rate(struct device *dev,
598 struct device_attribute *attr, char *buf)
599{
600 struct kim_data_s *kim_data = dev_get_drvdata(dev);
601 return sprintf(buf, "%ld\n", kim_data->baud_rate);
602}
603
604static ssize_t show_flow_cntrl(struct device *dev,
605 struct device_attribute *attr, char *buf)
606{
607 struct kim_data_s *kim_data = dev_get_drvdata(dev);
608 return sprintf(buf, "%d\n", kim_data->flow_cntrl);
609}
610
611/* structures specific for sysfs entries */
612static struct kobj_attribute ldisc_install =
613__ATTR(install, 0444, (void *)show_install, NULL);
614
615static struct kobj_attribute uart_dev_name =
616__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
617
618static struct kobj_attribute uart_baud_rate =
619__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
620
621static struct kobj_attribute uart_flow_cntrl =
622__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
623
624static struct attribute *uim_attrs[] = {
625 &ldisc_install.attr,
626 &uart_dev_name.attr,
627 &uart_baud_rate.attr,
628 &uart_flow_cntrl.attr,
629 NULL,
630};
631
632static struct attribute_group uim_attr_grp = {
633 .attrs = uim_attrs,
634};
635
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500636/**
637 * st_kim_ref - reference the core's data
638 * This references the per-ST platform device in the arch/xx/
639 * board-xx.c file.
640 * This would enable multiple such platform devices to exist
641 * on a given platform
642 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400643void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500644{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500645 struct platform_device *pdev;
646 struct kim_data_s *kim_gdata;
647 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400648 pdev = st_get_plat_device(id);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500649 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500650 *core_data = kim_gdata->core_data;
651}
652
Pavan Savoyc1afac12010-07-28 02:25:59 -0500653static int kim_version_open(struct inode *i, struct file *f)
654{
655 return single_open(f, show_version, i->i_private);
656}
657
658static int kim_list_open(struct inode *i, struct file *f)
659{
660 return single_open(f, show_list, i->i_private);
661}
662
663static const struct file_operations version_debugfs_fops = {
664 /* version info */
665 .open = kim_version_open,
666 .read = seq_read,
667 .llseek = seq_lseek,
668 .release = single_release,
669};
670static const struct file_operations list_debugfs_fops = {
671 /* protocols info */
672 .open = kim_list_open,
673 .read = seq_read,
674 .llseek = seq_lseek,
675 .release = single_release,
676};
677
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500678/**********************************************************************/
679/* functions called from platform device driver subsystem
680 * need to have a relevant platform device entry in the platform's
681 * board-*.c file
682 */
683
Pavan Savoyc1afac12010-07-28 02:25:59 -0500684struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500685static int kim_probe(struct platform_device *pdev)
686{
687 long status;
688 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500689 struct kim_data_s *kim_gdata;
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600690 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
691 long *gpios = pdata->gpios;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500692
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400693 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
694 /* multiple devices could exist */
695 st_kim_devices[pdev->id] = pdev;
696 } else {
697 /* platform's sure about existance of 1 device */
698 st_kim_devices[0] = pdev;
699 }
700
Pavan Savoy38d9df42010-07-08 08:55:17 -0500701 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
702 if (!kim_gdata) {
703 pr_err("no mem to allocate");
704 return -ENOMEM;
705 }
706 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500707
708 status = st_core_init(&kim_gdata->core_data);
709 if (status != 0) {
710 pr_err(" ST core init failed");
Pavan Savoy70442662011-02-04 02:23:11 -0600711 return -EIO;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500712 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500713 /* refer to itself */
714 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500715
Pavan Savoy5c88b022011-02-04 02:23:09 -0600716 for (proto = 0; proto < ST_MAX_CHANNELS; proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500717 kim_gdata->gpios[proto] = gpios[proto];
718 pr_info(" %ld gpio to be requested", gpios[proto]);
719 }
720
Pavan Savoy5c88b022011-02-04 02:23:09 -0600721 for (proto = 0; (proto < ST_MAX_CHANNELS)
722 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500723 /* Claim the Bluetooth/FM/GPIO
724 * nShutdown gpio from the system
725 */
726 status = gpio_request(gpios[proto], "kim");
727 if (unlikely(status)) {
728 pr_err(" gpio %ld request failed ", gpios[proto]);
729 proto -= 1;
730 while (proto >= 0) {
731 if (gpios[proto] != -1)
732 gpio_free(gpios[proto]);
733 }
734 return status;
735 }
736
737 /* Configure nShutdown GPIO as output=0 */
738 status =
739 gpio_direction_output(gpios[proto], 0);
740 if (unlikely(status)) {
741 pr_err(" unable to configure gpio %ld",
742 gpios[proto]);
743 proto -= 1;
744 while (proto >= 0) {
745 if (gpios[proto] != -1)
746 gpio_free(gpios[proto]);
747 }
748 return status;
749 }
750 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500751 /* get reference of pdev for request_firmware
752 */
753 kim_gdata->kim_pdev = pdev;
754 init_completion(&kim_gdata->kim_rcvd);
755 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500756
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600757 status = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
758 if (status) {
759 pr_err("failed to create sysfs entries");
760 return status;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500761 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500762
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600763 /* copying platform data */
764 strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
765 kim_gdata->flow_cntrl = pdata->flow_cntrl;
766 kim_gdata->baud_rate = pdata->baud_rate;
767 pr_info("sysfs entries created\n");
768
Pavan Savoyc1afac12010-07-28 02:25:59 -0500769 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
770 if (IS_ERR(kim_debugfs_dir)) {
771 pr_err(" debugfs entries creation failed ");
772 kim_debugfs_dir = NULL;
Pavan Savoy70442662011-02-04 02:23:11 -0600773 return -EIO;
Naveen Jaine2a53282010-06-09 03:45:33 -0500774 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500775
776 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
777 kim_gdata, &version_debugfs_fops);
778 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
779 kim_gdata, &list_debugfs_fops);
780 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500781 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500782}
783
784static int kim_remove(struct platform_device *pdev)
785{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600786 /* free the GPIOs requested */
787 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
788 long *gpios = pdata->gpios;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500789 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500790 struct kim_data_s *kim_gdata;
791
792 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500793
Pavan Savoy5c88b022011-02-04 02:23:09 -0600794 for (proto = 0; (proto < ST_MAX_CHANNELS)
795 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500796 /* Claim the Bluetooth/FM/GPIO
797 * nShutdown gpio from the system
798 */
799 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500800 }
801 pr_info("kim: GPIO Freed");
Pavan Savoyc1afac12010-07-28 02:25:59 -0500802 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600803
804 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500805 kim_gdata->kim_pdev = NULL;
806 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500807
808 kfree(kim_gdata);
809 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500810 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500811}
812
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600813int kim_suspend(struct platform_device *pdev, pm_message_t state)
814{
815 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
816
817 if (pdata->suspend)
818 return pdata->suspend(pdev, state);
819
820 return -EOPNOTSUPP;
821}
822
823int kim_resume(struct platform_device *pdev)
824{
825 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
826
827 if (pdata->resume)
828 return pdata->resume(pdev);
829
830 return -EOPNOTSUPP;
831}
832
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500833/**********************************************************************/
834/* entry point for ST KIM module, called in from ST Core */
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600835static struct platform_driver kim_platform_driver = {
836 .probe = kim_probe,
837 .remove = kim_remove,
838 .suspend = kim_suspend,
839 .resume = kim_resume,
840 .driver = {
841 .name = "kim",
842 .owner = THIS_MODULE,
843 },
844};
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500845
846static int __init st_kim_init(void)
847{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600848 return platform_driver_register(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500849}
850
851static void __exit st_kim_deinit(void)
852{
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500853 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500854}
855
856
857module_init(st_kim_init);
858module_exit(st_kim_deinit);
859MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
860MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
861MODULE_LICENSE("GPL");