blob: 09e2c97562be5adc06570b89fcbcca8a9457cb0e [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
5 * Copyright (C) 2009 Texas Instruments
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#define pr_fmt(fmt) "(stk) :" fmt
23#include <linux/platform_device.h>
24#include <linux/jiffies.h>
25#include <linux/firmware.h>
26#include <linux/delay.h>
27#include <linux/wait.h>
28#include <linux/gpio.h>
Pavan Savoyc1afac12010-07-28 02:25:59 -050029#include <linux/debugfs.h>
30#include <linux/seq_file.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050031#include <linux/sched.h>
Pavan Savoy83ef41f2010-09-17 12:06:11 -040032#include <linux/rfkill.h>
Pavan Savoyd0088ce2010-04-08 13:16:54 -050033
Pavan Savoyd0088ce2010-04-08 13:16:54 -050034/* understand BT events for fw response */
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37#include <net/bluetooth/hci.h>
38
Pavan Savoy83ef41f2010-09-17 12:06:11 -040039#include "ti_wilink_st.h"
40
Pavan Savoyd0088ce2010-04-08 13:16:54 -050041
42static int kim_probe(struct platform_device *pdev);
43static int kim_remove(struct platform_device *pdev);
44
45/* KIM platform device driver structure */
46static struct platform_driver kim_platform_driver = {
47 .probe = kim_probe,
48 .remove = kim_remove,
49 /* TODO: ST driver power management during suspend/resume ?
50 */
51#if 0
52 .suspend = kim_suspend,
53 .resume = kim_resume,
54#endif
55 .driver = {
56 .name = "kim",
57 .owner = THIS_MODULE,
58 },
59};
60
Pavan Savoyd0088ce2010-04-08 13:16:54 -050061static int kim_toggle_radio(void*, bool);
62static const struct rfkill_ops kim_rfkill_ops = {
63 .set_block = kim_toggle_radio,
64};
Pavan Savoyd0088ce2010-04-08 13:16:54 -050065
66/* strings to be used for rfkill entries and by
67 * ST Core to be used for sysfs debug entry
68 */
69#define PROTO_ENTRY(type, name) name
70const unsigned char *protocol_names[] = {
71 PROTO_ENTRY(ST_BT, "Bluetooth"),
72 PROTO_ENTRY(ST_FM, "FM"),
73 PROTO_ENTRY(ST_GPS, "GPS"),
74};
75
Pavan Savoydbd3a872010-08-19 14:08:51 -040076#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
77struct platform_device *st_kim_devices[MAX_ST_DEVICES];
Pavan Savoyd0088ce2010-04-08 13:16:54 -050078
79/**********************************************************************/
80/* internal functions */
81
Pavan Savoy36b5aee2010-07-22 05:32:06 -050082/**
Pavan Savoydbd3a872010-08-19 14:08:51 -040083 * st_get_plat_device -
84 * function which returns the reference to the platform device
85 * requested by id. As of now only 1 such device exists (id=0)
86 * the context requesting for reference can get the id to be
87 * requested by a. The protocol driver which is registering or
88 * b. the tty device which is opened.
89 */
90static struct platform_device *st_get_plat_device(int id)
91{
92 return st_kim_devices[id];
93}
94
95/**
Pavan Savoy36b5aee2010-07-22 05:32:06 -050096 * validate_firmware_response -
97 * function to return whether the firmware response was proper
98 * in case of error don't complete so that waiting for proper
99 * response times out
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500100 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500101void validate_firmware_response(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500102{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500103 struct sk_buff *skb = kim_gdata->rx_skb;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500104 if (unlikely(skb->data[5] != 0)) {
105 pr_err("no proper response during fw download");
106 pr_err("data6 %x", skb->data[5]);
107 return; /* keep waiting for the proper response */
108 }
109 /* becos of all the script being downloaded */
110 complete_all(&kim_gdata->kim_rcvd);
111 kfree_skb(skb);
112}
113
114/* check for data len received inside kim_int_recv
115 * most often hit the last case to update state to waiting for data
116 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500117static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500118{
119 register int room = skb_tailroom(kim_gdata->rx_skb);
120
Pavan Savoye6d9e642010-07-22 05:32:05 -0500121 pr_debug("len %d room %d", len, room);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500122
123 if (!len) {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500124 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500125 } else if (len > room) {
126 /* Received packet's payload length is larger.
127 * We can't accommodate it in created skb.
128 */
129 pr_err("Data length is too large len %d room %d", len,
130 room);
131 kfree_skb(kim_gdata->rx_skb);
132 } else {
133 /* Packet header has non-zero payload length and
134 * we have enough space in created skb. Lets read
135 * payload data */
136 kim_gdata->rx_state = ST_BT_W4_DATA;
137 kim_gdata->rx_count = len;
138 return len;
139 }
140
141 /* Change ST LL state to continue to process next
142 * packet */
143 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
144 kim_gdata->rx_skb = NULL;
145 kim_gdata->rx_count = 0;
146
147 return 0;
148}
149
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500150/**
151 * kim_int_recv - receive function called during firmware download
152 * firmware download responses on different UART drivers
153 * have been observed to come in bursts of different
154 * tty_receive and hence the logic
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500155 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500156void kim_int_recv(struct kim_data_s *kim_gdata,
157 const unsigned char *data, long count)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500158{
159 register char *ptr;
160 struct hci_event_hdr *eh;
161 register int len = 0, type = 0;
162
Pavan Savoye6d9e642010-07-22 05:32:05 -0500163 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500164 /* Decode received bytes here */
165 ptr = (char *)data;
166 if (unlikely(ptr == NULL)) {
167 pr_err(" received null from TTY ");
168 return;
169 }
170 while (count) {
171 if (kim_gdata->rx_count) {
172 len = min_t(unsigned int, kim_gdata->rx_count, count);
173 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
174 kim_gdata->rx_count -= len;
175 count -= len;
176 ptr += len;
177
178 if (kim_gdata->rx_count)
179 continue;
180
181 /* Check ST RX state machine , where are we? */
182 switch (kim_gdata->rx_state) {
183 /* Waiting for complete packet ? */
184 case ST_BT_W4_DATA:
Pavan Savoye6d9e642010-07-22 05:32:05 -0500185 pr_debug("Complete pkt received");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500186 validate_firmware_response(kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500187 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
188 kim_gdata->rx_skb = NULL;
189 continue;
190 /* Waiting for Bluetooth event header ? */
191 case ST_BT_W4_EVENT_HDR:
192 eh = (struct hci_event_hdr *)kim_gdata->
193 rx_skb->data;
Pavan Savoye6d9e642010-07-22 05:32:05 -0500194 pr_debug("Event header: evt 0x%2.2x"
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500195 "plen %d", eh->evt, eh->plen);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500196 kim_check_data_len(kim_gdata, eh->plen);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500197 continue;
198 } /* end of switch */
199 } /* end of if rx_state */
200 switch (*ptr) {
201 /* Bluetooth event packet? */
202 case HCI_EVENT_PKT:
203 pr_info("Event packet");
204 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
205 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
206 type = HCI_EVENT_PKT;
207 break;
208 default:
209 pr_info("unknown packet");
210 ptr++;
211 count--;
212 continue;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500213 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500214 ptr++;
215 count--;
216 kim_gdata->rx_skb =
217 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
218 if (!kim_gdata->rx_skb) {
219 pr_err("can't allocate mem for new packet");
220 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
221 kim_gdata->rx_count = 0;
222 return;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500223 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500224 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500225 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500226 pr_info("done %s", __func__);
227 return;
228}
229
Pavan Savoy38d9df42010-07-08 08:55:17 -0500230static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500231{
232 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
233 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
234
Pavan Savoye6d9e642010-07-22 05:32:05 -0500235 pr_debug("%s", __func__);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500236
237 INIT_COMPLETION(kim_gdata->kim_rcvd);
238 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
239 pr_err("kim: couldn't write 4 bytes");
Pavan Savoy320920c2010-07-14 08:21:12 -0500240 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500241 }
242
243 if (!wait_for_completion_timeout
244 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
245 pr_err(" waiting for ver info- timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500246 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500247 }
248
249 version =
Naveen Jaine2a53282010-06-09 03:45:33 -0500250 MAKEWORD(kim_gdata->resp_buffer[13],
251 kim_gdata->resp_buffer[14]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500252 chip = (version & 0x7C00) >> 10;
253 min_ver = (version & 0x007F);
254 maj_ver = (version & 0x0380) >> 7;
255
256 if (version & 0x8000)
257 maj_ver |= 0x0008;
258
259 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
Naveen Jaine2a53282010-06-09 03:45:33 -0500260
261 /* to be accessed later via sysfs entry */
262 kim_gdata->version.full = version;
263 kim_gdata->version.chip = chip;
264 kim_gdata->version.maj_ver = maj_ver;
265 kim_gdata->version.min_ver = min_ver;
266
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500267 pr_info("%s", bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500268 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500269}
270
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500271/**
272 * download_firmware -
273 * internal function which parses through the .bts firmware
274 * script file intreprets SEND, DELAY actions only as of now
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500275 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500276static long download_firmware(struct kim_data_s *kim_gdata)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500277{
Pavan Savoy320920c2010-07-14 08:21:12 -0500278 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500279 long len = 0;
280 register unsigned char *ptr = NULL;
281 register unsigned char *action_ptr = NULL;
282 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
283
Pavan Savoy38d9df42010-07-08 08:55:17 -0500284 err = read_local_version(kim_gdata, bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500285 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500286 pr_err("kim: failed to read local ver");
287 return err;
288 }
289 err =
290 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
291 &kim_gdata->kim_pdev->dev);
292 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
293 (kim_gdata->fw_entry->size == 0))) {
294 pr_err(" request_firmware failed(errno %ld) for %s", err,
295 bts_scr_name);
Pavan Savoy320920c2010-07-14 08:21:12 -0500296 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500297 }
298 ptr = (void *)kim_gdata->fw_entry->data;
299 len = kim_gdata->fw_entry->size;
300 /* bts_header to remove out magic number and
301 * version
302 */
303 ptr += sizeof(struct bts_header);
304 len -= sizeof(struct bts_header);
305
306 while (len > 0 && ptr) {
Pavan Savoye6d9e642010-07-22 05:32:05 -0500307 pr_debug(" action size %d, type %d ",
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500308 ((struct bts_action *)ptr)->size,
309 ((struct bts_action *)ptr)->type);
310
311 switch (((struct bts_action *)ptr)->type) {
312 case ACTION_SEND_COMMAND: /* action send */
313 action_ptr = &(((struct bts_action *)ptr)->data[0]);
314 if (unlikely
315 (((struct hci_command *)action_ptr)->opcode ==
316 0xFF36)) {
317 /* ignore remote change
318 * baud rate HCI VS command */
319 pr_err
Pavan Savoye6d9e642010-07-22 05:32:05 -0500320 (" change remote baud"
321 " rate command in firmware");
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500322 break;
323 }
324
325 INIT_COMPLETION(kim_gdata->kim_rcvd);
326 err = st_int_write(kim_gdata->core_data,
327 ((struct bts_action_send *)action_ptr)->data,
328 ((struct bts_action *)ptr)->size);
329 if (unlikely(err < 0)) {
330 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500331 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500332 }
333 if (!wait_for_completion_timeout
334 (&kim_gdata->kim_rcvd,
335 msecs_to_jiffies(CMD_RESP_TIME))) {
336 pr_err
337 (" response timeout during fw download ");
338 /* timed out */
339 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500340 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500341 }
342 break;
343 case ACTION_DELAY: /* sleep */
344 pr_info("sleep command in scr");
345 action_ptr = &(((struct bts_action *)ptr)->data[0]);
346 mdelay(((struct bts_action_delay *)action_ptr)->msec);
347 break;
348 }
349 len =
350 len - (sizeof(struct bts_action) +
351 ((struct bts_action *)ptr)->size);
352 ptr =
353 ptr + sizeof(struct bts_action) +
354 ((struct bts_action *)ptr)->size;
355 }
356 /* fw download complete */
357 release_firmware(kim_gdata->fw_entry);
Pavan Savoy320920c2010-07-14 08:21:12 -0500358 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500359}
360
361/**********************************************************************/
362/* functions called from ST core */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500363/* function to toggle the GPIO
364 * needs to know whether the GPIO is active high or active low
365 */
366void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
367{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500368 struct platform_device *kim_pdev;
369 struct kim_data_s *kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500370 pr_info(" %s ", __func__);
371
Pavan Savoydbd3a872010-08-19 14:08:51 -0400372 kim_pdev = st_get_plat_device(0);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500373 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
374
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500375 if (kim_gdata->gpios[type] == -1) {
376 pr_info(" gpio not requested for protocol %s",
377 protocol_names[type]);
378 return;
379 }
380 switch (type) {
381 case ST_BT:
382 /*Do Nothing */
383 break;
384
385 case ST_FM:
386 if (state == KIM_GPIO_ACTIVE)
387 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
388 else
389 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
390 break;
391
392 case ST_GPS:
393 if (state == KIM_GPIO_ACTIVE)
394 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
395 else
396 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
397 break;
398
399 case ST_MAX:
400 default:
401 break;
402 }
403
404 return;
405}
406
407/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
408 * can be because of
409 * 1. response to read local version
410 * 2. during send/recv's of firmware download
411 */
412void st_kim_recv(void *disc_data, const unsigned char *data, long count)
413{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500414 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
415 struct kim_data_s *kim_gdata = st_gdata->kim_data;
416
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500417 pr_info(" %s ", __func__);
418 /* copy to local buffer */
419 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
420 /* must be the read_ver_cmd */
421 memcpy(kim_gdata->resp_buffer, data, count);
422 complete_all(&kim_gdata->kim_rcvd);
423 return;
424 } else {
Pavan Savoy38d9df42010-07-08 08:55:17 -0500425 kim_int_recv(kim_gdata, data, count);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500426 /* either completes or times out */
427 }
428 return;
429}
430
431/* to signal completion of line discipline installation
432 * called from ST Core, upon tty_open
433 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500434void st_kim_complete(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500435{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500436 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500437 complete(&kim_gdata->ldisc_installed);
438}
439
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500440/**
441 * st_kim_start - called from ST Core upon 1st registration
442 * This involves toggling the chip enable gpio, reading
443 * the firmware version from chip, forming the fw file name
444 * based on the chip version, requesting the fw, parsing it
445 * and perform download(send/recv).
446 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500447long st_kim_start(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500448{
Pavan Savoy320920c2010-07-14 08:21:12 -0500449 long err = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500450 long retry = POR_RETRY_COUNT;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500451 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
452
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500453 pr_info(" %s", __func__);
454
455 do {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500456 /* TODO: this is only because rfkill sub-system
457 * doesn't send events to user-space if the state
458 * isn't changed
459 */
460 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500461 /* Configure BT nShutdown to HIGH state */
462 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
463 mdelay(5); /* FIXME: a proper toggle */
464 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
465 mdelay(100);
466 /* re-initialize the completion */
467 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500468#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500469 /* send signal to UIM */
470 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
471 if (err != 0) {
472 pr_info(" sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500473 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500474 continue;
475 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500476#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500477 /* unblock and send event to UIM via /dev/rfkill */
478 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500479 /* wait for ldisc to be installed */
480 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
481 msecs_to_jiffies(LDISC_TIME));
482 if (!err) { /* timeout */
483 pr_err("line disc installation timed out ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500484 err = -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500485 continue;
486 } else {
487 /* ldisc installed now */
488 pr_info(" line discipline installed ");
Pavan Savoy38d9df42010-07-08 08:55:17 -0500489 err = download_firmware(kim_gdata);
Pavan Savoy320920c2010-07-14 08:21:12 -0500490 if (err != 0) {
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500491 pr_err("download firmware failed");
492 continue;
493 } else { /* on success don't retry */
494 break;
495 }
496 }
497 } while (retry--);
498 return err;
499}
500
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500501/**
502 * st_kim_stop - called from ST Core, on the last un-registration
503 * toggle low the chip enable gpio
504 */
Pavan Savoy38d9df42010-07-08 08:55:17 -0500505long st_kim_stop(void *kim_data)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500506{
Pavan Savoy320920c2010-07-14 08:21:12 -0500507 long err = 0;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500508 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500509
510 INIT_COMPLETION(kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500511#if 0 /* older way of signalling user-space UIM */
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500512 /* send signal to UIM */
513 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
514 if (err != 0) {
515 pr_err("sending SIGUSR2 to uim failed %ld", err);
Pavan Savoy320920c2010-07-14 08:21:12 -0500516 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500517 }
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500518#endif
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500519 /* set BT rfkill to be blocked */
520 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500521
522 /* wait for ldisc to be un-installed */
523 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
524 msecs_to_jiffies(LDISC_TIME));
525 if (!err) { /* timeout */
526 pr_err(" timed out waiting for ldisc to be un-installed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500527 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500528 }
529
530 /* By default configure BT nShutdown to LOW state */
531 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
532 mdelay(1);
533 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
534 mdelay(1);
535 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
536 return err;
537}
538
539/**********************************************************************/
540/* functions called from subsystems */
Pavan Savoyc1afac12010-07-28 02:25:59 -0500541/* called when debugfs entry is read from */
Naveen Jaine2a53282010-06-09 03:45:33 -0500542
Pavan Savoyc1afac12010-07-28 02:25:59 -0500543static int show_version(struct seq_file *s, void *unused)
Naveen Jaine2a53282010-06-09 03:45:33 -0500544{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500545 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
546 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
Naveen Jaine2a53282010-06-09 03:45:33 -0500547 kim_gdata->version.chip, kim_gdata->version.maj_ver,
548 kim_gdata->version.min_ver);
Pavan Savoyc1afac12010-07-28 02:25:59 -0500549 return 0;
Naveen Jaine2a53282010-06-09 03:45:33 -0500550}
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500551
Pavan Savoyc1afac12010-07-28 02:25:59 -0500552static int show_list(struct seq_file *s, void *unused)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500553{
Pavan Savoyc1afac12010-07-28 02:25:59 -0500554 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
555 kim_st_list_protocols(kim_gdata->core_data, s);
556 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500557}
558
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500559/* function called from rfkill subsystem, when someone from
560 * user space would write 0/1 on the sysfs entry
561 * /sys/class/rfkill/rfkill0,1,3/state
562 */
563static int kim_toggle_radio(void *data, bool blocked)
564{
565 enum proto_type type = *((enum proto_type *)data);
Pavan Savoye6d9e642010-07-22 05:32:05 -0500566 pr_debug(" %s: %d ", __func__, type);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500567
568 switch (type) {
569 case ST_BT:
570 /* do nothing */
571 break;
572 case ST_FM:
573 case ST_GPS:
574 if (blocked)
575 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
576 else
577 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
578 break;
579 case ST_MAX:
580 pr_err(" wrong proto type ");
581 break;
582 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500583 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500584}
585
Pavan Savoy36b5aee2010-07-22 05:32:06 -0500586/**
587 * st_kim_ref - reference the core's data
588 * This references the per-ST platform device in the arch/xx/
589 * board-xx.c file.
590 * This would enable multiple such platform devices to exist
591 * on a given platform
592 */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400593void st_kim_ref(struct st_data_s **core_data, int id)
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500594{
Pavan Savoy38d9df42010-07-08 08:55:17 -0500595 struct platform_device *pdev;
596 struct kim_data_s *kim_gdata;
597 /* get kim_gdata reference from platform device */
Pavan Savoydbd3a872010-08-19 14:08:51 -0400598 pdev = st_get_plat_device(id);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500599 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500600 *core_data = kim_gdata->core_data;
601}
602
Pavan Savoyc1afac12010-07-28 02:25:59 -0500603static int kim_version_open(struct inode *i, struct file *f)
604{
605 return single_open(f, show_version, i->i_private);
606}
607
608static int kim_list_open(struct inode *i, struct file *f)
609{
610 return single_open(f, show_list, i->i_private);
611}
612
613static const struct file_operations version_debugfs_fops = {
614 /* version info */
615 .open = kim_version_open,
616 .read = seq_read,
617 .llseek = seq_lseek,
618 .release = single_release,
619};
620static const struct file_operations list_debugfs_fops = {
621 /* protocols info */
622 .open = kim_list_open,
623 .read = seq_read,
624 .llseek = seq_lseek,
625 .release = single_release,
626};
627
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500628/**********************************************************************/
629/* functions called from platform device driver subsystem
630 * need to have a relevant platform device entry in the platform's
631 * board-*.c file
632 */
633
Pavan Savoyc1afac12010-07-28 02:25:59 -0500634struct dentry *kim_debugfs_dir;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500635static int kim_probe(struct platform_device *pdev)
636{
637 long status;
638 long proto;
639 long *gpios = pdev->dev.platform_data;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500640 struct kim_data_s *kim_gdata;
641
Pavan Savoydfb7ef72010-09-10 15:58:55 -0400642 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
643 /* multiple devices could exist */
644 st_kim_devices[pdev->id] = pdev;
645 } else {
646 /* platform's sure about existance of 1 device */
647 st_kim_devices[0] = pdev;
648 }
649
Pavan Savoy38d9df42010-07-08 08:55:17 -0500650 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
651 if (!kim_gdata) {
652 pr_err("no mem to allocate");
653 return -ENOMEM;
654 }
655 dev_set_drvdata(&pdev->dev, kim_gdata);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500656
657 status = st_core_init(&kim_gdata->core_data);
658 if (status != 0) {
659 pr_err(" ST core init failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500660 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500661 }
Pavan Savoy38d9df42010-07-08 08:55:17 -0500662 /* refer to itself */
663 kim_gdata->core_data->kim_data = kim_gdata;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500664
665 for (proto = 0; proto < ST_MAX; proto++) {
666 kim_gdata->gpios[proto] = gpios[proto];
667 pr_info(" %ld gpio to be requested", gpios[proto]);
668 }
669
670 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
671 /* Claim the Bluetooth/FM/GPIO
672 * nShutdown gpio from the system
673 */
674 status = gpio_request(gpios[proto], "kim");
675 if (unlikely(status)) {
676 pr_err(" gpio %ld request failed ", 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
685 /* Configure nShutdown GPIO as output=0 */
686 status =
687 gpio_direction_output(gpios[proto], 0);
688 if (unlikely(status)) {
689 pr_err(" unable to configure gpio %ld",
690 gpios[proto]);
691 proto -= 1;
692 while (proto >= 0) {
693 if (gpios[proto] != -1)
694 gpio_free(gpios[proto]);
695 }
696 return status;
697 }
698 }
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500699 /* get reference of pdev for request_firmware
700 */
701 kim_gdata->kim_pdev = pdev;
702 init_completion(&kim_gdata->kim_rcvd);
703 init_completion(&kim_gdata->ldisc_installed);
Naveen Jainb38fc2d2010-06-09 03:45:32 -0500704
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500705 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
706 /* TODO: should all types be rfkill_type_bt ? */
707 kim_gdata->rf_protos[proto] = proto;
708 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
709 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
710 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
711 if (kim_gdata->rfkill[proto] == NULL) {
712 pr_err("cannot create rfkill entry for gpio %ld",
713 gpios[proto]);
714 continue;
715 }
716 /* block upon creation */
717 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
718 status = rfkill_register(kim_gdata->rfkill[proto]);
719 if (unlikely(status)) {
720 pr_err("rfkill registration failed for gpio %ld",
721 gpios[proto]);
722 rfkill_unregister(kim_gdata->rfkill[proto]);
723 continue;
724 }
725 pr_info("rfkill entry created for %ld", gpios[proto]);
726 }
Naveen Jaine2a53282010-06-09 03:45:33 -0500727
Pavan Savoyc1afac12010-07-28 02:25:59 -0500728 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
729 if (IS_ERR(kim_debugfs_dir)) {
730 pr_err(" debugfs entries creation failed ");
731 kim_debugfs_dir = NULL;
Naveen Jaine2a53282010-06-09 03:45:33 -0500732 return -1;
733 }
Pavan Savoyc1afac12010-07-28 02:25:59 -0500734
735 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
736 kim_gdata, &version_debugfs_fops);
737 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
738 kim_gdata, &list_debugfs_fops);
739 pr_info(" debugfs entries created ");
Pavan Savoy320920c2010-07-14 08:21:12 -0500740 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500741}
742
743static int kim_remove(struct platform_device *pdev)
744{
745 /* free the GPIOs requested
746 */
747 long *gpios = pdev->dev.platform_data;
748 long proto;
Pavan Savoy38d9df42010-07-08 08:55:17 -0500749 struct kim_data_s *kim_gdata;
750
751 kim_gdata = dev_get_drvdata(&pdev->dev);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500752
753 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
754 /* Claim the Bluetooth/FM/GPIO
755 * nShutdown gpio from the system
756 */
757 gpio_free(gpios[proto]);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500758 rfkill_unregister(kim_gdata->rfkill[proto]);
759 rfkill_destroy(kim_gdata->rfkill[proto]);
760 kim_gdata->rfkill[proto] = NULL;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500761 }
762 pr_info("kim: GPIO Freed");
Pavan Savoyc1afac12010-07-28 02:25:59 -0500763 debugfs_remove_recursive(kim_debugfs_dir);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500764 kim_gdata->kim_pdev = NULL;
765 st_core_exit(kim_gdata->core_data);
Pavan Savoy38d9df42010-07-08 08:55:17 -0500766
767 kfree(kim_gdata);
768 kim_gdata = NULL;
Pavan Savoy320920c2010-07-14 08:21:12 -0500769 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500770}
771
772/**********************************************************************/
773/* entry point for ST KIM module, called in from ST Core */
774
775static int __init st_kim_init(void)
776{
Pavan Savoy320920c2010-07-14 08:21:12 -0500777 long ret = 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500778 ret = platform_driver_register(&kim_platform_driver);
779 if (ret != 0) {
780 pr_err("platform drv registration failed");
Pavan Savoy320920c2010-07-14 08:21:12 -0500781 return -1;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500782 }
Pavan Savoy320920c2010-07-14 08:21:12 -0500783 return 0;
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500784}
785
786static void __exit st_kim_deinit(void)
787{
788 /* the following returns void */
789 platform_driver_unregister(&kim_platform_driver);
Pavan Savoyd0088ce2010-04-08 13:16:54 -0500790}
791
792
793module_init(st_kim_init);
794module_exit(st_kim_deinit);
795MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
796MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
797MODULE_LICENSE("GPL");