blob: a7fda814175892f2b79cd63aa83bd97124e9732a [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 Savoy320920c2010-07-14 08:21:12 -0500204 return -1;
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 Savoy320920c2010-07-14 08:21:12 -0500210 return -1;
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 Savoy36b5aee2010-07-22 05:32:06 -0500235/**
236 * download_firmware -
237 * internal function which parses through the .bts firmware
238 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500239 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500240static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500241{
Pavan Savoy320920c2010-07-14 08:21:12 -0500242 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500243 long len = 0;
Pavan Savoy73f12e82010-10-12 16:27:38 -0400244 unsigned char *ptr = NULL;
245 unsigned char *action_ptr = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500246 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
247
Pavan Savoy38d9df42010-07-08 08:55:17 -0500248 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500249 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500250 pr_err("kim: failed to read local ver");
251 return err;
252 }
253 err =
254 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
255 &kim_gdata->kim_pdev->dev);
256 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
257 (kim_gdata->fw_entry->size == 0))) {
258 pr_err(" request_firmware failed(errno %ld) for %s", err,
259 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500260 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500261 }
262 ptr = (void *)kim_gdata->fw_entry->data;
263 len = kim_gdata->fw_entry->size;
264 /* bts_header to remove out magic number and
265 * version
266 */
267 ptr += sizeof(struct bts_header);
268 len -= sizeof(struct bts_header);
269
270 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500271 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500272 ((struct bts_action *)ptr)->size,
273 ((struct bts_action *)ptr)->type);
274
275 switch (((struct bts_action *)ptr)->type) {
276 case ACTION_SEND_COMMAND: /* action send */
277 action_ptr = &(((struct bts_action *)ptr)->data[0]);
278 if (unlikely
279 (((struct hci_command *)action_ptr)->opcode ==
280 0xFF36)) {
281 /* ignore remote change
282 * baud rate HCI VS command */
283 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500284 (" change remote baud"
285 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500286 break;
287 }
288
289 INIT_COMPLETION(kim_gdata->kim_rcvd);
290 err = st_int_write(kim_gdata->core_data,
291 ((struct bts_action_send *)action_ptr)->data,
292 ((struct bts_action *)ptr)->size);
293 if (unlikely(err < 0)) {
294 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500295 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500296 }
297 if (!wait_for_completion_timeout
298 (&kim_gdata->kim_rcvd,
299 msecs_to_jiffies(CMD_RESP_TIME))) {
300 pr_err
301 (" response timeout during fw download ");
302 /* timed out */
303 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500304 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500305 }
306 break;
307 case ACTION_DELAY: /* sleep */
308 pr_info("sleep command in scr");
309 action_ptr = &(((struct bts_action *)ptr)->data[0]);
310 mdelay(((struct bts_action_delay *)action_ptr)->msec);
311 break;
312 }
313 len =
314 len - (sizeof(struct bts_action) +
315 ((struct bts_action *)ptr)->size);
316 ptr =
317 ptr + sizeof(struct bts_action) +
318 ((struct bts_action *)ptr)->size;
319 }
320 /* fw download complete */
321 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500322 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500323}
324
325/**********************************************************************/
326/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500327/* function to toggle the GPIO
328 * needs to know whether the GPIO is active high or active low
329 */
330void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
331{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500332 struct platform_device *kim_pdev;
333 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500334 pr_info(" %s ", __func__);
335
Pavan Savoydbd3a872010-08-19 14:08:51 -0400336 kim_pdev = st_get_plat_device(0);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500337 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
338
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500339 if (kim_gdata->gpios[type] == -1) {
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600340 pr_info("gpio not requested for protocol %d", type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500341 return;
342 }
343 switch (type) {
344 case ST_BT:
345 /*Do Nothing */
346 break;
347
348 case ST_FM:
349 if (state == KIM_GPIO_ACTIVE)
350 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
351 else
352 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
353 break;
354
355 case ST_GPS:
356 if (state == KIM_GPIO_ACTIVE)
357 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
358 else
359 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
360 break;
361
Pavan Savoy5c88b022011-02-04 02:23:09 -0600362 case ST_MAX_CHANNELS:
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500363 default:
364 break;
365 }
366
367 return;
368}
369
370/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
371 * can be because of
372 * 1. response to read local version
373 * 2. during send/recv's of firmware download
374 */
375void st_kim_recv(void *disc_data, const unsigned char *data, long count)
376{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500377 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
378 struct kim_data_s *kim_gdata = st_gdata->kim_data;
379
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500380 /* copy to local buffer */
381 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
382 /* must be the read_ver_cmd */
383 memcpy(kim_gdata->resp_buffer, data, count);
384 complete_all(&kim_gdata->kim_rcvd);
385 return;
386 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500387 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500388 /* either completes or times out */
389 }
390 return;
391}
392
393/* to signal completion of line discipline installation
394 * called from ST Core, upon tty_open
395 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500396void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500397{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500398 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500399 complete(&kim_gdata->ldisc_installed);
400}
401
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500402/**
403 * st_kim_start - called from ST Core upon 1st registration
404 * This involves toggling the chip enable gpio, reading
405 * the firmware version from chip, forming the fw file name
406 * based on the chip version, requesting the fw, parsing it
407 * and perform download(send/recv).
408 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500409long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500410{
Pavan Savoy320920c2010-07-14 08:21:12 -0500411 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500412 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500413 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
414
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500415 pr_info(" %s", __func__);
416
417 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500418 /* Configure BT nShutdown to HIGH state */
419 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
420 mdelay(5); /* FIXME: a proper toggle */
421 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
422 mdelay(100);
423 /* re-initialize the completion */
424 INIT_COMPLETION(kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600425 /* send notification to UIM */
426 kim_gdata->ldisc_install = 1;
427 pr_info("ldisc_install = 1");
428 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
429 NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500430 /* wait for ldisc to be installed */
431 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
432 msecs_to_jiffies(LDISC_TIME));
433 if (!err) { /* timeout */
434 pr_err("line disc installation timed out ");
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600435 kim_gdata->ldisc_install = 0;
436 pr_info("ldisc_install = 0");
437 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
438 NULL, "install");
Pavan Savoy320920c2010-07-14 08:21:12 -0500439 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500440 continue;
441 } else {
442 /* ldisc installed now */
443 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500444 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500445 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500446 pr_err("download firmware failed");
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600447 kim_gdata->ldisc_install = 0;
448 pr_info("ldisc_install = 0");
449 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
450 NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500451 continue;
452 } else { /* on success don't retry */
453 break;
454 }
455 }
456 } while (retry--);
457 return err;
458}
459
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500460/**
461 * st_kim_stop - called from ST Core, on the last un-registration
462 * toggle low the chip enable gpio
463 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500464long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500465{
Pavan Savoy320920c2010-07-14 08:21:12 -0500466 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500467 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500468
469 INIT_COMPLETION(kim_gdata->ldisc_installed);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600470
471 /* Flush any pending characters in the driver and discipline. */
472 tty_ldisc_flush(kim_gdata->core_data->tty);
473 tty_driver_flush_buffer(kim_gdata->core_data->tty);
474
475 /* send uninstall notification to UIM */
476 pr_info("ldisc_install = 0");
477 kim_gdata->ldisc_install = 0;
478 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500479
480 /* wait for ldisc to be un-installed */
481 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
482 msecs_to_jiffies(LDISC_TIME));
483 if (!err) { /* timeout */
484 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500485 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500486 }
487
488 /* By default configure BT nShutdown to LOW state */
489 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
490 mdelay(1);
491 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
492 mdelay(1);
493 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
494 return err;
495}
496
497/**********************************************************************/
498/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500499/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500500
Pavan Savoyc1afac12010-07-28 02:25:59 -0500501static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500502{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500503 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
504 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500505 kim_gdata->version.chip, kim_gdata->version.maj_ver,
506 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500507 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500508}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500509
Pavan Savoyc1afac12010-07-28 02:25:59 -0500510static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500511{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500512 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
513 kim_st_list_protocols(kim_gdata->core_data, s);
514 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500515}
516
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600517static ssize_t show_install(struct device *dev,
518 struct device_attribute *attr, char *buf)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500519{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600520 struct kim_data_s *kim_data = dev_get_drvdata(dev);
521 return sprintf(buf, "%d\n", kim_data->ldisc_install);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500522}
523
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600524static ssize_t show_dev_name(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
527 struct kim_data_s *kim_data = dev_get_drvdata(dev);
528 return sprintf(buf, "%s\n", kim_data->dev_name);
529}
530
531static ssize_t show_baud_rate(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
534 struct kim_data_s *kim_data = dev_get_drvdata(dev);
535 return sprintf(buf, "%ld\n", kim_data->baud_rate);
536}
537
538static ssize_t show_flow_cntrl(struct device *dev,
539 struct device_attribute *attr, char *buf)
540{
541 struct kim_data_s *kim_data = dev_get_drvdata(dev);
542 return sprintf(buf, "%d\n", kim_data->flow_cntrl);
543}
544
545/* structures specific for sysfs entries */
546static struct kobj_attribute ldisc_install =
547__ATTR(install, 0444, (void *)show_install, NULL);
548
549static struct kobj_attribute uart_dev_name =
550__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
551
552static struct kobj_attribute uart_baud_rate =
553__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
554
555static struct kobj_attribute uart_flow_cntrl =
556__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
557
558static struct attribute *uim_attrs[] = {
559 &ldisc_install.attr,
560 &uart_dev_name.attr,
561 &uart_baud_rate.attr,
562 &uart_flow_cntrl.attr,
563 NULL,
564};
565
566static struct attribute_group uim_attr_grp = {
567 .attrs = uim_attrs,
568};
569
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500570/**
571 * st_kim_ref - reference the core's data
572 * This references the per-ST platform device in the arch/xx/
573 * board-xx.c file.
574 * This would enable multiple such platform devices to exist
575 * on a given platform
576 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400577void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500578{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500579 struct platform_device *pdev;
580 struct kim_data_s *kim_gdata;
581 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400582 pdev = st_get_plat_device(id);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500583 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500584 *core_data = kim_gdata->core_data;
585}
586
Pavan Savoyc1afac12010-07-28 02:25:59 -0500587static int kim_version_open(struct inode *i, struct file *f)
588{
589 return single_open(f, show_version, i->i_private);
590}
591
592static int kim_list_open(struct inode *i, struct file *f)
593{
594 return single_open(f, show_list, i->i_private);
595}
596
597static const struct file_operations version_debugfs_fops = {
598 /* version info */
599 .open = kim_version_open,
600 .read = seq_read,
601 .llseek = seq_lseek,
602 .release = single_release,
603};
604static const struct file_operations list_debugfs_fops = {
605 /* protocols info */
606 .open = kim_list_open,
607 .read = seq_read,
608 .llseek = seq_lseek,
609 .release = single_release,
610};
611
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500612/**********************************************************************/
613/* functions called from platform device driver subsystem
614 * need to have a relevant platform device entry in the platform's
615 * board-*.c file
616 */
617
Pavan Savoyc1afac12010-07-28 02:25:59 -0500618struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500619static int kim_probe(struct platform_device *pdev)
620{
621 long status;
622 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500623 struct kim_data_s *kim_gdata;
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600624 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
625 long *gpios = pdata->gpios;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500626
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400627 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
628 /* multiple devices could exist */
629 st_kim_devices[pdev->id] = pdev;
630 } else {
631 /* platform's sure about existance of 1 device */
632 st_kim_devices[0] = pdev;
633 }
634
Pavan Savoy38d9df42010-07-08 08:55:17 -0500635 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
636 if (!kim_gdata) {
637 pr_err("no mem to allocate");
638 return -ENOMEM;
639 }
640 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500641
642 status = st_core_init(&kim_gdata->core_data);
643 if (status != 0) {
644 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500645 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500646 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500647 /* refer to itself */
648 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500649
Pavan Savoy5c88b022011-02-04 02:23:09 -0600650 for (proto = 0; proto < ST_MAX_CHANNELS; proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500651 kim_gdata->gpios[proto] = gpios[proto];
652 pr_info(" %ld gpio to be requested", gpios[proto]);
653 }
654
Pavan Savoy5c88b022011-02-04 02:23:09 -0600655 for (proto = 0; (proto < ST_MAX_CHANNELS)
656 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500657 /* Claim the Bluetooth/FM/GPIO
658 * nShutdown gpio from the system
659 */
660 status = gpio_request(gpios[proto], "kim");
661 if (unlikely(status)) {
662 pr_err(" gpio %ld request failed ", gpios[proto]);
663 proto -= 1;
664 while (proto >= 0) {
665 if (gpios[proto] != -1)
666 gpio_free(gpios[proto]);
667 }
668 return status;
669 }
670
671 /* Configure nShutdown GPIO as output=0 */
672 status =
673 gpio_direction_output(gpios[proto], 0);
674 if (unlikely(status)) {
675 pr_err(" unable to configure gpio %ld",
676 gpios[proto]);
677 proto -= 1;
678 while (proto >= 0) {
679 if (gpios[proto] != -1)
680 gpio_free(gpios[proto]);
681 }
682 return status;
683 }
684 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500685 /* get reference of pdev for request_firmware
686 */
687 kim_gdata->kim_pdev = pdev;
688 init_completion(&kim_gdata->kim_rcvd);
689 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500690
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600691 status = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
692 if (status) {
693 pr_err("failed to create sysfs entries");
694 return status;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500695 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500696
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600697 /* copying platform data */
698 strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
699 kim_gdata->flow_cntrl = pdata->flow_cntrl;
700 kim_gdata->baud_rate = pdata->baud_rate;
701 pr_info("sysfs entries created\n");
702
Pavan Savoyc1afac12010-07-28 02:25:59 -0500703 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
704 if (IS_ERR(kim_debugfs_dir)) {
705 pr_err(" debugfs entries creation failed ");
706 kim_debugfs_dir = NULL;
Naveen Jaine2a53282010-06-09 03:45:33 -0500707 return -1;
708 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500709
710 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
711 kim_gdata, &version_debugfs_fops);
712 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
713 kim_gdata, &list_debugfs_fops);
714 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500715 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500716}
717
718static int kim_remove(struct platform_device *pdev)
719{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600720 /* free the GPIOs requested */
721 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
722 long *gpios = pdata->gpios;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500723 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500724 struct kim_data_s *kim_gdata;
725
726 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500727
Pavan Savoy5c88b022011-02-04 02:23:09 -0600728 for (proto = 0; (proto < ST_MAX_CHANNELS)
729 && (gpios[proto] != -1); proto++) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500730 /* Claim the Bluetooth/FM/GPIO
731 * nShutdown gpio from the system
732 */
733 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500734 }
735 pr_info("kim: GPIO Freed");
Pavan Savoyc1afac12010-07-28 02:25:59 -0500736 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600737
738 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500739 kim_gdata->kim_pdev = NULL;
740 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500741
742 kfree(kim_gdata);
743 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500744 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500745}
746
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600747int kim_suspend(struct platform_device *pdev, pm_message_t state)
748{
749 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
750
751 if (pdata->suspend)
752 return pdata->suspend(pdev, state);
753
754 return -EOPNOTSUPP;
755}
756
757int kim_resume(struct platform_device *pdev)
758{
759 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
760
761 if (pdata->resume)
762 return pdata->resume(pdev);
763
764 return -EOPNOTSUPP;
765}
766
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500767/**********************************************************************/
768/* entry point for ST KIM module, called in from ST Core */
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600769static struct platform_driver kim_platform_driver = {
770 .probe = kim_probe,
771 .remove = kim_remove,
772 .suspend = kim_suspend,
773 .resume = kim_resume,
774 .driver = {
775 .name = "kim",
776 .owner = THIS_MODULE,
777 },
778};
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500779
780static int __init st_kim_init(void)
781{
Pavan Savoyec60d0a2011-02-04 02:23:10 -0600782 return platform_driver_register(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500783}
784
785static void __exit st_kim_deinit(void)
786{
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500787 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500788}
789
790
791module_init(st_kim_init);
792module_exit(st_kim_deinit);
793MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
794MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
795MODULE_LICENSE("GPL");